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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [mmu/] [immu.c] - Blame information for rev 1416

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

powered by: WebSVN 2.1.0

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