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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_69/] [or1ksim/] [mmu/] [dmmu.c] - Blame information for rev 1765

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 1432 nogj
#include "spr_defs.h"
34
#include "execute.h"
35 6 lampret
#include "stats.h"
36 62 lampret
#include "sprs.h"
37
#include "except.h"
38 425 markom
#include "sim-config.h"
39 1308 phoenix
#include "debug.h"
40 6 lampret
 
41 1412 nogj
DEFAULT_DEBUG_CHANNEL(dmmu);
42
 
43 62 lampret
extern int cont_run;
44
 
45 6 lampret
/* Data MMU */
46
 
47 1350 nogj
inline oraddr_t dmmu_simulate_tlb(oraddr_t virtaddr, int write_access)
48 6 lampret
{
49 430 markom
  int set, way = -1;
50
  int i;
51 1350 nogj
  oraddr_t tagaddr;
52
  oraddr_t vpn, ppn;
53 572 simons
 
54 638 simons
  if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
55
    data_ci = (virtaddr >= 0x80000000);
56 430 markom
    return virtaddr;
57 638 simons
  }
58 430 markom
 
59
  /* Which set to check out? */
60
  set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
61
  tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
62 456 simons
  vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
63
 
64 430 markom
  /* Scan all ways and try to find a matching way. */
65
  for (i = 0; i < config.dmmu.nways; i++)
66 456 simons
    if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
67 430 markom
        testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
68
      way = i;
69 456 simons
 
70
   /* Did we find our tlb entry? */
71 430 markom
  if (way >= 0) { /* Yes, we did. */
72
    dmmu_stats.loads_tlbhit++;
73 1412 nogj
    TRACE("DTLB hit (virtaddr=%"PRIxADDR") at %lli.\n", virtaddr,
74
          runtime.sim.cycles);
75 430 markom
 
76 1414 nogj
    /* Set LRUs */
77
    for (i = 0; i < config.dmmu.nways; i++)
78
      if (testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
79
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
80
    setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, config.dmmu.nsets - 1);
81
 
82
    /* Check if page is cache inhibited */
83
    data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
84
 
85
    runtime.sim.mem_cycles += config.dmmu.hitdelay;
86
    ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
87
 
88 430 markom
    /* Test for page fault */
89 600 simons
    if (mfspr (SPR_SR) & SPR_SR_SM) {
90 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
91
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
92 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
93
    } else {
94 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
95
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
96 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
97
    }
98
 
99 456 simons
    return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
100 430 markom
  }
101
  else {  /* No, we didn't. */
102
    dmmu_stats.loads_tlbmiss++;
103
#if 0
104
    for (i = 0; i < config.dmmu.nways; i++)
105
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
106
        minway = i;
107
 
108
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
109
    for (i = 0; i < config.dmmu.nways; i++)
110
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
111
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
112
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
113
    setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
114
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
115
#endif
116 1412 nogj
    TRACE("DTLB miss (virtaddr=%"PRIxADDR") at %lli.\n", virtaddr,
117
          runtime.sim.cycles);
118 1414 nogj
    runtime.sim.mem_cycles += config.dmmu.missdelay;
119 430 markom
    /* if tlb refill implemented in HW */
120
    /* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * config.dmmu.pagesize + (virtaddr % config.dmmu.pagesize); */
121 1414 nogj
    except_handle(EXCEPT_DTLBMISS, virtaddr);
122 430 markom
    return 0;
123
  }
124
}
125
 
126 1240 phoenix
/* DESC: try to find EA -> PA transaltion without changing
127
 *       any of precessor states. if this is not passible gives up
128
 *       (without triggering exceptions)
129
 *
130
 * PRMS: virtaddr     - EA for which to find translation
131
 *
132
 *       write_access - 0 ignore testing for write access
133
 *                      1 test for write access, if fails
134
 *                        do not return translation
135
 *
136
 *       through_dc   - 1 go through data cache
137
 *                      0 ignore data cache
138
 *
139
 * RTRN: 0            - no DMMU, DMMU disabled or ITLB miss
140
 *       else         - appropriate PA (note it DMMU is not present
141
 *                      PA === EA)
142
 */
143 1350 nogj
oraddr_t peek_into_dtlb(oraddr_t virtaddr, int write_access, int through_dc)
144 1240 phoenix
{
145
  int set, way = -1;
146
  int i;
147 1350 nogj
  oraddr_t tagaddr;
148
  oraddr_t vpn, ppn;
149 1240 phoenix
 
150
  if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
151
    if (through_dc)
152
      data_ci = (virtaddr >= 0x80000000);
153
    return virtaddr;
154
  }
155
 
156
  /* Which set to check out? */
157
  set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
158
  tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
159
  vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
160
 
161
  /* Scan all ways and try to find a matching way. */
162
  for (i = 0; i < config.dmmu.nways; i++)
163
    if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
164
        testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
165
      way = i;
166
 
167
   /* Did we find our tlb entry? */
168
  if (way >= 0) { /* Yes, we did. */
169
    dmmu_stats.loads_tlbhit++;
170 1412 nogj
    TRACE("DTLB hit (virtaddr=%"PRIxADDR") at %lli.\n", virtaddr,
171
          runtime.sim.cycles);
172 1240 phoenix
 
173
    /* Test for page fault */
174
    if (mfspr (SPR_SR) & SPR_SR_SM) {
175
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
176
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
177
 
178
        /* otherwise exception DPF would be raised */
179
        return(0);
180
    } else {
181
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
182
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
183
 
184
        /* otherwise exception DPF would be raised */
185
        return(0);
186
    }
187
 
188
    if (through_dc) {
189
      /* Check if page is cache inhibited */
190
      data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
191
    }
192
 
193
    ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
194
    return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
195
  }
196
  else {  /* No, we didn't. */
197
    return(0);
198
  }
199
 
200 1412 nogj
  ERR("ERR, should never have happened\n");
201 1240 phoenix
  return(0);
202
}
203
 
204
 
205 1350 nogj
oraddr_t dmmu_translate(oraddr_t virtaddr, int write_access)
206 430 markom
{
207 1350 nogj
  oraddr_t phyaddr = dmmu_simulate_tlb(virtaddr, write_access);
208 429 markom
 
209 1350 nogj
/*  PRINTF("DMMU translate(%"PRIxADDR") = %"PRIxADDR"\n", virtaddr, phyaddr);*/
210 429 markom
  return phyaddr;
211 6 lampret
}
212
 
213
 
214 62 lampret
void dtlb_info()
215 6 lampret
{
216 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
217 997 markom
    PRINTF("DMMU not implemented. Set UPR[DMP].\n");
218 429 markom
    return;
219
  }
220
 
221 997 markom
  PRINTF("Data MMU %dKB: ", config.dmmu.nsets * config.dmmu.entrysize * config.dmmu.nways / 1024);
222
  PRINTF("%d ways, %d sets, entry size %d bytes\n", config.dmmu.nways, config.dmmu.nsets, config.dmmu.entrysize);
223 6 lampret
}
224
 
225 62 lampret
/* First check if virtual address is covered by DTLB and if it is:
226
    - increment DTLB read hit stats,
227 425 markom
    - set 'lru' at this way to config.dmmu.ustates - 1 and
228 6 lampret
      decrement 'lru' of other ways unless they have reached 0,
229 62 lampret
    - check page access attributes and invoke DMMU page fault exception
230
      handler if necessary
231 6 lampret
   and if not:
232 62 lampret
    - increment DTLB read miss stats
233
    - find lru way and entry and invoke DTLB miss exception handler
234 425 markom
    - set 'lru' with config.dmmu.ustates - 1 and decrement 'lru' of other
235 6 lampret
      ways unless they have reached 0
236
*/
237
 
238 102 lampret
void dtlb_status(int start_set)
239 6 lampret
{
240 429 markom
  int set;
241
  int way;
242
  int end_set = config.dmmu.nsets;
243 62 lampret
 
244 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
245 997 markom
    PRINTF("DMMU not implemented. Set UPR[DMP].\n");
246 429 markom
    return;
247
  }
248 102 lampret
 
249 429 markom
  if ((start_set >= 0) && (start_set < end_set))
250
    end_set = start_set + 1;
251
  else
252
    start_set = 0;
253 62 lampret
 
254 997 markom
  if (start_set < end_set) PRINTF("\nDMMU: ");
255 429 markom
  /* Scan set(s) and way(s). */
256
  for (set = start_set; set < end_set; set++) {
257 997 markom
    PRINTF("\nSet %x: ", set);
258 429 markom
    for (way = 0; way < config.dmmu.nways; way++) {
259 997 markom
      PRINTF("  way %d: ", way);
260 1308 phoenix
      PRINTF("vpn=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
261
      PRINTF("lru=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
262
      PRINTF("pl1=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
263
      PRINTF("v=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
264 429 markom
 
265 1308 phoenix
      PRINTF("a=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
266
      PRINTF("d=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
267
      PRINTF("ure=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
268
      PRINTF("uwe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
269
      PRINTF("sre=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
270
      PRINTF("swe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
271
      PRINTF("ppn=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
272 429 markom
    }
273
  }
274 997 markom
  if (start_set < end_set) PRINTF("\n");
275 6 lampret
}
276 1358 nogj
 
277
/*---------------------------------------------------[ DMMU configuration ]---*/
278
void dmmu_enabled(union param_val val, void *dat)
279
{
280
  setsprbits (SPR_UPR, SPR_UPR_DMP, val.int_val ? 1 : 0);
281
  config.dmmu.enabled = val.int_val;
282
}
283
 
284
void dmmu_nsets(union param_val val, void *dat)
285
{
286 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= 256) {
287 1358 nogj
    config.dmmu.nsets = val.int_val;
288 1382 nogj
    setsprbits (SPR_DMMUCFGR, SPR_DMMUCFGR_NTS, log2(val.int_val));
289
  }
290 1358 nogj
  else
291
    CONFIG_ERROR("value of power of two and lower or equal than 256 expected.");
292
}
293
 
294
void dmmu_nways(union param_val val, void *dat)
295
{
296 1382 nogj
  if (val.int_val >= 1 && val.int_val <= 4) {
297 1358 nogj
    config.dmmu.nways = val.int_val;
298 1382 nogj
    setsprbits (SPR_DMMUCFGR, SPR_DMMUCFGR_NTW, val.int_val-1);
299
  }
300 1358 nogj
  else
301
    CONFIG_ERROR("value 1, 2, 3 or 4 expected.");
302
}
303
 
304
void dmmu_pagesize(union param_val val, void *dat)
305
{
306
  if (is_power2(val.int_val))
307
    config.dmmu.pagesize = val.int_val;
308
  else
309
    CONFIG_ERROR("value of power of two expected.");
310
}
311
 
312
void dmmu_entrysize(union param_val val, void *dat)
313
{
314
  if (is_power2(val.int_val))
315
    config.dmmu.entrysize = val.int_val;
316
  else
317
    CONFIG_ERROR("value of power of two expected.");
318
}
319
 
320
void dmmu_ustates(union param_val val, void *dat)
321
{
322
  if (val.int_val >= 2 && val.int_val <= 4)
323
    config.dmmu.ustates = val.int_val;
324
  else
325
    CONFIG_ERROR("invalid USTATE.");
326
}
327
 
328
void dmmu_missdelay(union param_val val, void *dat)
329
{
330
  config.dmmu.missdelay = val.int_val;
331
}
332
 
333
void dmmu_hitdelay(union param_val val, void *dat)
334
{
335
  config.immu.hitdelay = val.int_val;
336
}
337
 
338
void reg_dmmu_sec(void)
339
{
340
  struct config_section *sec = reg_config_sec("dmmu", NULL, NULL);
341
 
342
  reg_config_param(sec, "enabled", paramt_int, dmmu_enabled);
343
  reg_config_param(sec, "nsets", paramt_int, dmmu_nsets);
344
  reg_config_param(sec, "nways", paramt_int, dmmu_nways);
345
  reg_config_param(sec, "pagesize", paramt_int, dmmu_pagesize);
346
  reg_config_param(sec, "entrysize", paramt_int, dmmu_entrysize);
347
  reg_config_param(sec, "ustates", paramt_int, dmmu_ustates);
348
  reg_config_param(sec, "missdelay", paramt_int, dmmu_missdelay);
349
  reg_config_param(sec, "hitdelay", paramt_int, dmmu_hitdelay);
350
}

powered by: WebSVN 2.1.0

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