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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc3/] [or1ksim/] [mmu/] [immu.c] - Blame information for rev 1654

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

powered by: WebSVN 2.1.0

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