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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [mmu/] [dmmu.c] - Blame information for rev 1716

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

powered by: WebSVN 2.1.0

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