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 1780

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

powered by: WebSVN 2.1.0

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