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

Subversion Repositories or1k

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

powered by: WebSVN 2.1.0

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