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 1412

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

powered by: WebSVN 2.1.0

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