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 1358

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

powered by: WebSVN 2.1.0

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