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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0/] [or1ksim/] [mmu/] [dmmu.c] - Blame information for rev 1539

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

powered by: WebSVN 2.1.0

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