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

Subversion Repositories or1k

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

powered by: WebSVN 2.1.0

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