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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [mmu/] [immu.c] - Blame information for rev 1538

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 74 lampret
/* immu.c -- Instruction MMU simulation
2
   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 1538 nogj
/* IMMU model, perfectly functional. */
21 74 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 74 lampret
#include "immu.h"
31
#include "abstract.h"
32 1344 nogj
#include "opcode/or32.h"
33 1432 nogj
#include "spr_defs.h"
34
#include "execute.h"
35 74 lampret
#include "stats.h"
36
#include "sprs.h"
37
#include "except.h"
38 425 markom
#include "sim-config.h"
39 1308 phoenix
#include "debug.h"
40 74 lampret
 
41 1416 nogj
DEFAULT_DEBUG_CHANNEL(immu);
42
 
43 74 lampret
/* Insn MMU */
44
 
45 1538 nogj
/* Precalculates some values for use during address translation */
46
void init_immu(void)
47 430 markom
{
48 1538 nogj
  config.immu.pagesize_log2 = log2(config.immu.pagesize);
49
  config.immu.page_offset_mask = config.immu.pagesize - 1;
50
  config.immu.page_mask = ~config.immu.page_offset_mask;
51
  config.immu.vpn_mask = ~((config.immu.pagesize * config.immu.nsets) - 1);
52
  config.immu.set_mask = config.immu.nsets - 1;
53
  config.immu.lru_reload = (config.immu.set_mask << 6) & SPR_ITLBMR_LRU;
54
}
55
 
56
inline uorreg_t *immu_find_tlbmr(oraddr_t virtaddr, uorreg_t **itlbmr_lru)
57
{
58
  int set;
59 430 markom
  int i;
60 1538 nogj
  oraddr_t vpn;
61
  uorreg_t *itlbmr;
62 884 markom
 
63 1538 nogj
  /* Which set to check out? */
64
  set = IADDR_PAGE(virtaddr) >> config.immu.pagesize_log2;
65
  set &= config.immu.set_mask;
66
  vpn = virtaddr & config.immu.vpn_mask;
67
 
68
  itlbmr = &cpu_state.sprs[SPR_ITLBMR_BASE(0) + set];
69
  *itlbmr_lru = itlbmr;
70
 
71
  /* Scan all ways and try to find a matching way. */
72
  /* FIXME: Should this be reversed? */
73
  for(i = config.immu.nways; i; i--, itlbmr += (128 * 2)) {
74
    if(((*itlbmr & config.immu.vpn_mask) == vpn) && (*itlbmr & SPR_ITLBMR_V))
75
      return itlbmr;
76
  }
77
 
78
  return NULL;
79
}
80
 
81
oraddr_t immu_translate(oraddr_t virtaddr)
82
{
83
  int i;
84
  uorreg_t *itlbmr;
85
  uorreg_t *itlbtr;
86
  uorreg_t *itlbmr_lru;
87
 
88 1506 nogj
  if (!(cpu_state.sprs[SPR_SR] & SPR_SR_IME) ||
89
      !(cpu_state.sprs[SPR_UPR] & SPR_UPR_IMP)) {
90 638 simons
    insn_ci = (virtaddr >= 0x80000000);
91 430 markom
    return virtaddr;
92 638 simons
  }
93 430 markom
 
94 1538 nogj
  itlbmr = immu_find_tlbmr(virtaddr, &itlbmr_lru);
95 1416 nogj
 
96 430 markom
  /* Did we find our tlb entry? */
97 1538 nogj
  if(itlbmr) { /* Yes, we did. */
98 430 markom
    immu_stats.fetch_tlbhit++;
99 1416 nogj
    TRACE("ITLB hit (virtaddr=%"PRIxADDR").\n", virtaddr);
100 430 markom
 
101 1538 nogj
    itlbtr = itlbmr + 128;
102
 
103 430 markom
    /* Set LRUs */
104 1538 nogj
    for(i = 0; i < config.immu.nways; i++, itlbmr_lru += (128 * 2)) {
105
      if(*itlbmr_lru & SPR_ITLBMR_LRU)
106
        *itlbmr_lru = (*itlbmr_lru & ~SPR_ITLBMR_LRU) |
107
                                        ((*itlbmr_lru & SPR_ITLBMR_LRU) - 0x40);
108 1506 nogj
    }
109
 
110 1538 nogj
    /* This is not necessary `*itlbmr &= ~SPR_ITLBMR_LRU;' since SPR_DTLBMR_LRU
111
     * is always decremented and the number of sets is always a power of two and
112
     * as such lru_reload has all bits set that get touched during decrementing
113
     * SPR_DTLBMR_LRU */
114
    *itlbmr |= config.immu.lru_reload;
115
 
116 638 simons
    /* Check if page is cache inhibited */
117 1538 nogj
    insn_ci = *itlbtr & SPR_ITLBTR_CI;
118 638 simons
 
119 884 markom
    runtime.sim.mem_cycles += config.immu.hitdelay;
120 1418 nogj
 
121
    /* Test for page fault */
122 1508 nogj
    if (cpu_state.sprs[SPR_SR] & SPR_SR_SM) {
123 1538 nogj
      if (!(*itlbtr & SPR_ITLBTR_SXE))
124 1418 nogj
        except_handle(EXCEPT_IPF, virtaddr);
125
    } else {
126 1538 nogj
      if (!(*itlbtr & SPR_ITLBTR_UXE))
127 1418 nogj
        except_handle(EXCEPT_IPF, virtaddr);
128
    }
129
 
130 1538 nogj
    TRACE("Returning physical address %"PRIxADDR"\n",
131
          (*itlbtr & SPR_ITLBTR_PPN) | (virtaddr &
132
                                               (config.immu.page_offset_mask)));
133
    return (*itlbtr & SPR_ITLBTR_PPN) | (virtaddr &
134
                                                (config.immu.page_offset_mask));
135 430 markom
  }
136 1538 nogj
 
137
  /* No, we didn't. */
138
  immu_stats.fetch_tlbmiss++;
139 430 markom
#if 0
140 1538 nogj
  for (i = 0; i < config.immu.nways; i++)
141
    if (((cpu_state.sprs[SPR_ITLBMR_BASE(i) + set] & SPR_ITLBMR_LRU) >> 6) < minlru)
142
      minway = i;
143
 
144
  cpu_state.sprs[SPR_ITLBMR_BASE(minway) + set] &= ~SPR_ITLBMR_VPN;
145
  cpu_state.sprs[SPR_ITLBMR_BASE(minway) + set] |= vpn << 12;
146
  for (i = 0; i < config.immu.nways; i++) {
147
    uorreg_t lru = cpu_state.sprs[SPR_ITLBMR_BASE(i) + set];
148
    if (lru & SPR_ITLBMR_LRU) {
149
      lru = (lru & ~SPR_ITLBMR_LRU) | ((lru & SPR_ITLBMR_LRU) - 0x40);
150
      cpu_state.sprs[SPR_ITLBMR_BASE(i) + set] = lru;
151 1506 nogj
    }
152 1538 nogj
  }
153
  cpu_state.sprs[SPR_ITLBMR_BASE(way) + set] &= ~SPR_ITLBMR_LRU;
154
  cpu_state.sprs[SPR_ITLBMR_BASE(way) + set] |= (config.immu.nsets - 1) << 6;
155 1506 nogj
 
156 1538 nogj
  /* 1 to 1 mapping */
157
  cpu_state.sprs[SPR_ITLBTR_BASE(minway) + set] &= ~SPR_ITLBTR_PPN;
158
  cpu_state.sprs[SPR_ITLBTR_BASE(minway) + set] |= vpn << 12;
159 1506 nogj
 
160 1538 nogj
  cpu_state.sprs[SPR_ITLBMR_BASE(minway) + set] |= SPR_ITLBMR_V;
161 430 markom
#endif
162 1418 nogj
 
163 1538 nogj
  /* if tlb refill implemented in HW */
164
  /* return ((cpu_state.sprs[SPR_ITLBTR_BASE(minway) + set] & SPR_ITLBTR_PPN) >> 12) * config.immu.pagesize + (virtaddr % config.immu.pagesize); */
165
  runtime.sim.mem_cycles += config.immu.missdelay;
166 1418 nogj
 
167 1538 nogj
  except_handle(EXCEPT_ITLBMISS, virtaddr);
168
  return 0;
169 430 markom
}
170
 
171 1174 phoenix
/* DESC: try to find EA -> PA transaltion without changing
172
 *       any of precessor states. if this is not passible gives up
173 1446 nogj
 *       (without triggering exceptions).
174 1174 phoenix
 *
175
 * PRMS: virtaddr  - EA for which to find translation
176
 *
177
 * RTRN: 0         - no IMMU, IMMU disabled or ITLB miss
178
 *       else      - appropriate PA (note it IMMU is not present
179
 *                   PA === EA)
180
 */
181 1350 nogj
oraddr_t peek_into_itlb(oraddr_t virtaddr)
182 1174 phoenix
{
183 1538 nogj
  uorreg_t *itlbmr;
184
  uorreg_t *itlbtr;
185
  uorreg_t *itlbmr_lru;
186 1174 phoenix
 
187 1506 nogj
  if (!(cpu_state.sprs[SPR_SR] & SPR_SR_IME) ||
188
      !(cpu_state.sprs[SPR_UPR] & SPR_UPR_IMP)) {
189 1174 phoenix
     return(virtaddr);
190
  }
191
 
192 1538 nogj
  itlbmr = immu_find_tlbmr(virtaddr, &itlbmr_lru);
193 1174 phoenix
 
194
  /* Did we find our tlb entry? */
195 1538 nogj
  if(itlbmr) { /* Yes, we did. */
196
    itlbtr = itlbmr + 128;
197
 
198 1174 phoenix
    /* Test for page fault */
199 1508 nogj
    if (cpu_state.sprs[SPR_SR] & SPR_SR_SM) {
200 1538 nogj
      if (!(*itlbtr & SPR_ITLBTR_SXE)) {
201 1174 phoenix
        /* no luck, giving up */
202
        return(0);
203
      }
204
    } else {
205 1538 nogj
      if (!(*itlbtr & SPR_ITLBTR_UXE)) {
206 1174 phoenix
        /* no luck, giving up */
207
        return(0);
208
      }
209
    }
210
 
211 1538 nogj
    return (*itlbtr & SPR_ITLBTR_PPN) | (virtaddr &
212
                                                (config.immu.page_offset_mask));
213 1174 phoenix
  }
214 1538 nogj
 
215 1174 phoenix
  return(0);
216
}
217
 
218
 
219 1506 nogj
void itlb_info(void)
220 74 lampret
{
221 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_IMP)) {
222 997 markom
    PRINTF("IMMU not implemented. Set UPR[IMP].\n");
223 429 markom
    return;
224
  }
225 102 lampret
 
226 997 markom
  PRINTF("Insn MMU %dKB: ", config.immu.nsets * config.immu.entrysize * config.immu.nways / 1024);
227
  PRINTF("%d ways, %d sets, entry size %d bytes\n", config.immu.nways, config.immu.nsets, config.immu.entrysize);
228 74 lampret
}
229
 
230
/* First check if virtual address is covered by ITLB and if it is:
231
    - increment ITLB read hit stats,
232 425 markom
    - set 'lru' at this way to config.immu.ustates - 1 and
233 74 lampret
      decrement 'lru' of other ways unless they have reached 0,
234
    - check page access attributes and invoke IMMU page fault exception
235
      handler if necessary
236
   and if not:
237
    - increment ITLB read miss stats
238
    - find lru way and entry and invoke ITLB miss exception handler
239 425 markom
    - set 'lru' with config.immu.ustates - 1 and decrement 'lru' of other
240 74 lampret
      ways unless they have reached 0
241
*/
242
 
243 102 lampret
void itlb_status(int start_set)
244 74 lampret
{
245 429 markom
  int set;
246
  int way;
247
  int end_set = config.immu.nsets;
248 74 lampret
 
249 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_IMP)) {
250 997 markom
    PRINTF("IMMU not implemented. Set UPR[IMP].\n");
251 429 markom
    return;
252
  }
253 102 lampret
 
254 429 markom
  if ((start_set >= 0) && (start_set < end_set))
255
    end_set = start_set + 1;
256
  else
257
    start_set = 0;
258 74 lampret
 
259 997 markom
  if (start_set < end_set) PRINTF("\nIMMU: ");
260 429 markom
  /* Scan set(s) and way(s). */
261
  for (set = start_set; set < end_set; set++) {
262 997 markom
    PRINTF("\nSet %x: ", set);
263 429 markom
    for (way = 0; way < config.immu.nways; way++) {
264 997 markom
      PRINTF("  way %d: ", way);
265 1532 nogj
      PRINTF("%s\n", dump_spr(SPR_ITLBMR_BASE(way) + set,
266
                              cpu_state.sprs[SPR_ITLBMR_BASE(way) + set]));
267
      PRINTF("%s\n", dump_spr(SPR_ITLBTR_BASE(way) + set,
268
                              cpu_state.sprs[SPR_ITLBTR_BASE(way) + set]));
269 429 markom
    }
270
  }
271 997 markom
  if (start_set < end_set) PRINTF("\n");
272 74 lampret
}
273 1358 nogj
 
274
/*---------------------------------------------------[ IMMU configuration ]---*/
275
void immu_enabled(union param_val val, void *dat)
276
{
277 1506 nogj
  if(val.int_val)
278
    cpu_state.sprs[SPR_UPR] |= SPR_UPR_IMP;
279
  else
280
    cpu_state.sprs[SPR_UPR] &= ~SPR_UPR_IMP;
281 1358 nogj
  config.immu.enabled = val.int_val;
282
}
283
 
284
void immu_nsets(union param_val val, void *dat)
285
{
286 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= 256) {
287 1358 nogj
    config.immu.nsets = val.int_val;
288 1506 nogj
    cpu_state.sprs[SPR_IMMUCFGR] &= ~SPR_IMMUCFGR_NTS;
289
    cpu_state.sprs[SPR_IMMUCFGR] |= log2(val.int_val) << 3;
290 1382 nogj
  }
291 1358 nogj
  else
292
    CONFIG_ERROR("value of power of two and lower or equal than 256 expected.");
293
}
294
 
295
void immu_nways(union param_val val, void *dat)
296
{
297 1382 nogj
  if (val.int_val >= 1 && val.int_val <= 4) {
298 1358 nogj
    config.immu.nways = val.int_val;
299 1506 nogj
    cpu_state.sprs[SPR_IMMUCFGR] &= ~SPR_IMMUCFGR_NTW;
300
    cpu_state.sprs[SPR_IMMUCFGR] |= val.int_val - 1;
301 1382 nogj
  }
302 1358 nogj
  else
303
    CONFIG_ERROR("value 1, 2, 3 or 4 expected.");
304
}
305
 
306
void immu_pagesize(union param_val val, void *dat)
307
{
308
  if (is_power2(val.int_val))
309
    config.immu.pagesize = val.int_val;
310
  else
311
    CONFIG_ERROR("value of power of two expected.");
312
}
313
 
314
void immu_entrysize(union param_val val, void *dat)
315
{
316
  if (is_power2(val.int_val))
317
    config.immu.entrysize = val.int_val;
318
  else
319
    CONFIG_ERROR("value of power of two expected.");
320
}
321
 
322
void immu_ustates(union param_val val, void *dat)
323
{
324
  if (val.int_val >= 2 && val.int_val <= 4)
325
    config.immu.ustates = val.int_val;
326
  else
327
    CONFIG_ERROR("invalid USTATE.");
328
}
329
 
330
void immu_missdelay(union param_val val, void *dat)
331
{
332
  config.immu.missdelay = val.int_val;
333
}
334
 
335
void immu_hitdelay(union param_val val, void *dat)
336
{
337
  config.immu.hitdelay = val.int_val;
338
}
339
 
340
void reg_immu_sec(void)
341
{
342
  struct config_section *sec = reg_config_sec("immu", NULL, NULL);
343
 
344
  reg_config_param(sec, "enabled", paramt_int, immu_enabled);
345
  reg_config_param(sec, "nsets", paramt_int, immu_nsets);
346
  reg_config_param(sec, "nways", paramt_int, immu_nways);
347
  reg_config_param(sec, "pagesize", paramt_int, immu_pagesize);
348
  reg_config_param(sec, "entrysize", paramt_int, immu_entrysize);
349
  reg_config_param(sec, "ustates", paramt_int, immu_ustates);
350
  reg_config_param(sec, "missdelay", paramt_int, immu_missdelay);
351
  reg_config_param(sec, "hitdelay", paramt_int, immu_hitdelay);
352
}

powered by: WebSVN 2.1.0

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