OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [mmu/] [dmmu.c] - Blame information for rev 1350

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 62 lampret
/* dmmu.c -- Data MMU simulation
2 6 lampret
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* DMMU model (not functional yet, currently just copy of data cache). */
21
 
22 1350 nogj
#include "config.h"
23
 
24
#ifdef HAVE_INTTYPES_H
25
#include <inttypes.h>
26
#endif
27
 
28
#include "port.h"
29
#include "arch.h"
30 6 lampret
#include "dmmu.h"
31
#include "abstract.h"
32 1344 nogj
#include "opcode/or32.h"
33 6 lampret
#include "stats.h"
34 62 lampret
#include "sprs.h"
35
#include "except.h"
36 425 markom
#include "sim-config.h"
37 1308 phoenix
#include "debug.h"
38 6 lampret
 
39 62 lampret
extern int cont_run;
40
 
41 6 lampret
/* Data MMU */
42
 
43 1350 nogj
inline oraddr_t dmmu_simulate_tlb(oraddr_t virtaddr, int write_access)
44 6 lampret
{
45 430 markom
  int set, way = -1;
46
  int i;
47 1350 nogj
  oraddr_t tagaddr;
48
  oraddr_t vpn, ppn;
49 572 simons
 
50 638 simons
  if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
51
    data_ci = (virtaddr >= 0x80000000);
52 430 markom
    return virtaddr;
53 638 simons
  }
54 430 markom
 
55
  /* Which set to check out? */
56
  set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
57
  tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
58 456 simons
  vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
59
 
60 430 markom
  /* Scan all ways and try to find a matching way. */
61
  for (i = 0; i < config.dmmu.nways; i++)
62 456 simons
    if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
63 430 markom
        testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
64
      way = i;
65 456 simons
 
66
   /* Did we find our tlb entry? */
67 430 markom
  if (way >= 0) { /* Yes, we did. */
68
    dmmu_stats.loads_tlbhit++;
69 1350 nogj
    debug(5, "DTLB hit (virtaddr=%"PRIxADDR").\n", virtaddr);
70 430 markom
 
71
    /* Test for page fault */
72 600 simons
    if (mfspr (SPR_SR) & SPR_SR_SM) {
73 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
74
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
75 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
76
    } else {
77 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
78
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
79 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
80
    }
81
 
82
    /* Set LRUs */
83
    for (i = 0; i < config.dmmu.nways; i++)
84
      if (testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
85
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
86 886 simons
    setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, config.dmmu.nsets - 1);
87 430 markom
 
88 638 simons
    /* Check if page is cache inhibited */
89
    data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
90
 
91 884 markom
    runtime.sim.mem_cycles += config.dmmu.hitdelay;
92 456 simons
    ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
93
    return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
94 430 markom
  }
95
  else {  /* No, we didn't. */
96
    dmmu_stats.loads_tlbmiss++;
97
#if 0
98
    for (i = 0; i < config.dmmu.nways; i++)
99
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
100
        minway = i;
101
 
102
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
103
    for (i = 0; i < config.dmmu.nways; i++)
104
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
105
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
106
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
107
    setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
108
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
109
#endif
110
    except_handle(EXCEPT_DTLBMISS, virtaddr);
111
    /* if tlb refill implemented in HW */
112
    /* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * config.dmmu.pagesize + (virtaddr % config.dmmu.pagesize); */
113 884 markom
    runtime.sim.mem_cycles += config.dmmu.missdelay;
114 430 markom
    return 0;
115
  }
116
}
117
 
118 1240 phoenix
/* DESC: try to find EA -> PA transaltion without changing
119
 *       any of precessor states. if this is not passible gives up
120
 *       (without triggering exceptions)
121
 *
122
 * PRMS: virtaddr     - EA for which to find translation
123
 *
124
 *       write_access - 0 ignore testing for write access
125
 *                      1 test for write access, if fails
126
 *                        do not return translation
127
 *
128
 *       through_dc   - 1 go through data cache
129
 *                      0 ignore data cache
130
 *
131
 * RTRN: 0            - no DMMU, DMMU disabled or ITLB miss
132
 *       else         - appropriate PA (note it DMMU is not present
133
 *                      PA === EA)
134
 */
135 1350 nogj
oraddr_t peek_into_dtlb(oraddr_t virtaddr, int write_access, int through_dc)
136 1240 phoenix
{
137
  int set, way = -1;
138
  int i;
139 1350 nogj
  oraddr_t tagaddr;
140
  oraddr_t vpn, ppn;
141 1240 phoenix
 
142
  if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
143
    if (through_dc)
144
      data_ci = (virtaddr >= 0x80000000);
145
    return virtaddr;
146
  }
147
 
148
  /* Which set to check out? */
149
  set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
150
  tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
151
  vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
152
 
153
  /* Scan all ways and try to find a matching way. */
154
  for (i = 0; i < config.dmmu.nways; i++)
155
    if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
156
        testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
157
      way = i;
158
 
159
   /* Did we find our tlb entry? */
160
  if (way >= 0) { /* Yes, we did. */
161
    dmmu_stats.loads_tlbhit++;
162 1350 nogj
    debug(5, "DTLB hit (virtaddr=%"PRIxADDR").\n", virtaddr);
163 1240 phoenix
 
164
    /* Test for page fault */
165
    if (mfspr (SPR_SR) & SPR_SR_SM) {
166
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
167
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
168
 
169
        /* otherwise exception DPF would be raised */
170
        return(0);
171
    } else {
172
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
173
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
174
 
175
        /* otherwise exception DPF would be raised */
176
        return(0);
177
    }
178
 
179
    if (through_dc) {
180
      /* Check if page is cache inhibited */
181
      data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
182
    }
183
 
184
    ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
185
    return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
186
  }
187
  else {  /* No, we didn't. */
188
    return(0);
189
  }
190
 
191
  PRINTF("ERR, should never have happened\n");
192
  return(0);
193
}
194
 
195
 
196 1350 nogj
oraddr_t dmmu_translate(oraddr_t virtaddr, int write_access)
197 430 markom
{
198 1350 nogj
  oraddr_t phyaddr = dmmu_simulate_tlb(virtaddr, write_access);
199 429 markom
 
200 1350 nogj
/*  PRINTF("DMMU translate(%"PRIxADDR") = %"PRIxADDR"\n", virtaddr, phyaddr);*/
201 429 markom
  return phyaddr;
202 6 lampret
}
203
 
204
 
205 62 lampret
void dtlb_info()
206 6 lampret
{
207 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
208 997 markom
    PRINTF("DMMU not implemented. Set UPR[DMP].\n");
209 429 markom
    return;
210
  }
211
 
212 997 markom
  PRINTF("Data MMU %dKB: ", config.dmmu.nsets * config.dmmu.entrysize * config.dmmu.nways / 1024);
213
  PRINTF("%d ways, %d sets, entry size %d bytes\n", config.dmmu.nways, config.dmmu.nsets, config.dmmu.entrysize);
214 6 lampret
}
215
 
216 62 lampret
/* First check if virtual address is covered by DTLB and if it is:
217
    - increment DTLB read hit stats,
218 425 markom
    - set 'lru' at this way to config.dmmu.ustates - 1 and
219 6 lampret
      decrement 'lru' of other ways unless they have reached 0,
220 62 lampret
    - check page access attributes and invoke DMMU page fault exception
221
      handler if necessary
222 6 lampret
   and if not:
223 62 lampret
    - increment DTLB read miss stats
224
    - find lru way and entry and invoke DTLB miss exception handler
225 425 markom
    - set 'lru' with config.dmmu.ustates - 1 and decrement 'lru' of other
226 6 lampret
      ways unless they have reached 0
227
*/
228
 
229 102 lampret
void dtlb_status(int start_set)
230 6 lampret
{
231 429 markom
  int set;
232
  int way;
233
  int end_set = config.dmmu.nsets;
234 62 lampret
 
235 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
236 997 markom
    PRINTF("DMMU not implemented. Set UPR[DMP].\n");
237 429 markom
    return;
238
  }
239 102 lampret
 
240 429 markom
  if ((start_set >= 0) && (start_set < end_set))
241
    end_set = start_set + 1;
242
  else
243
    start_set = 0;
244 62 lampret
 
245 997 markom
  if (start_set < end_set) PRINTF("\nDMMU: ");
246 429 markom
  /* Scan set(s) and way(s). */
247
  for (set = start_set; set < end_set; set++) {
248 997 markom
    PRINTF("\nSet %x: ", set);
249 429 markom
    for (way = 0; way < config.dmmu.nways; way++) {
250 997 markom
      PRINTF("  way %d: ", way);
251 1308 phoenix
      PRINTF("vpn=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
252
      PRINTF("lru=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
253
      PRINTF("pl1=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
254
      PRINTF("v=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
255 429 markom
 
256 1308 phoenix
      PRINTF("a=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
257
      PRINTF("d=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
258
      PRINTF("ure=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
259
      PRINTF("uwe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
260
      PRINTF("sre=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
261
      PRINTF("swe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
262
      PRINTF("ppn=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
263 429 markom
    }
264
  }
265 997 markom
  if (start_set < end_set) PRINTF("\n");
266 6 lampret
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.