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 1418

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

powered by: WebSVN 2.1.0

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