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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [mmu/] [immu.c] - Blame information for rev 1506

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

powered by: WebSVN 2.1.0

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