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 1344

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
#include "immu.h"
23
#include "abstract.h"
24 1344 nogj
#include "opcode/or32.h"
25 74 lampret
#include "stats.h"
26
#include "sprs.h"
27
#include "except.h"
28 425 markom
#include "sim-config.h"
29 1308 phoenix
#include "debug.h"
30 74 lampret
 
31
extern int cont_run;
32
 
33
/* Insn MMU */
34
 
35 713 markom
static inline unsigned long immu_simulate_tlb(unsigned long virtaddr)
36 430 markom
{
37
  int set, way = -1;
38
  int i;
39
  unsigned long tagaddr;
40 456 simons
  unsigned long vpn, ppn;
41 884 markom
 
42 638 simons
  if (!(mfspr(SPR_SR) & SPR_SR_IME) || !(testsprbits(SPR_UPR, SPR_UPR_IMP))) {
43
    insn_ci = (virtaddr >= 0x80000000);
44 430 markom
    return virtaddr;
45 638 simons
  }
46 430 markom
 
47
  /* Which set to check out? */
48
  set = (virtaddr / config.immu.pagesize) % config.immu.nsets;
49
  tagaddr = (virtaddr / config.immu.pagesize) / config.immu.nsets;
50 456 simons
  vpn = virtaddr / (config.immu.pagesize * config.immu.nsets);
51 430 markom
 
52
  /* Scan all ways and try to find a matching way. */
53
  for (i = 0; i < config.immu.nways; i++)
54 456 simons
    if (((mfspr(SPR_ITLBMR_BASE(i) + set) / (config.immu.pagesize * config.immu.nsets)) == vpn) &&
55 430 markom
        testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
56
      way = i;
57 456 simons
 
58 430 markom
  /* Did we find our tlb entry? */
59
  if (way >= 0) { /* Yes, we did. */
60
    immu_stats.fetch_tlbhit++;
61
    debug(5, "ITLB hit (virtaddr=%x).\n", virtaddr);
62
 
63
    /* Test for page fault */
64 600 simons
    if (mfspr (SPR_SR) & SPR_SR_SM) {
65 446 simons
      if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_SXE))
66 430 markom
        except_handle(EXCEPT_IPF, virtaddr);
67
    } else {
68 446 simons
      if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_UXE))
69 430 markom
        except_handle(EXCEPT_IPF, virtaddr);
70
    }
71
 
72
    /* Set LRUs */
73
    for (i = 0; i < config.immu.nways; i++)
74
      if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
75
        setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
76 886 simons
    setsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU, config.immu.nsets - 1);
77 541 markom
 
78 638 simons
    /* Check if page is cache inhibited */
79
    insn_ci = (mfspr(SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_CI) == SPR_ITLBTR_CI;
80
 
81 884 markom
    runtime.sim.mem_cycles += config.immu.hitdelay;
82 456 simons
    ppn = mfspr(SPR_ITLBTR_BASE(way) + set) / config.immu.pagesize;
83
    return (ppn * config.immu.pagesize) + (virtaddr % config.immu.pagesize);
84 430 markom
  }
85
  else {  /* No, we didn't. */
86
    immu_stats.fetch_tlbmiss++;
87
#if 0
88
    for (i = 0; i < config.immu.nways; i++)
89
      if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) < minlru)
90
        minway = i;
91
 
92
    setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_VPN, vpn);
93
    for (i = 0; i < config.immu.nways; i++)
94
      if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
95
        setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
96
    setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_LRU, config.immu.ustates - 1);
97
    setsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN, vpn); /* 1 to 1 */
98
    setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_V, 1);
99
#endif
100
    except_handle(EXCEPT_ITLBMISS, virtaddr);
101
    /* if tlb refill implemented in HW */
102
    /* return getsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN) * config.immu.pagesize + (virtaddr % config.immu.pagesize); */
103 884 markom
    runtime.sim.mem_cycles += config.immu.missdelay;
104 430 markom
    return 0;
105
  }
106
}
107
 
108 1174 phoenix
/* DESC: try to find EA -> PA transaltion without changing
109
 *       any of precessor states. if this is not passible gives up
110
 *       (without triggering exceptions)
111
 *
112
 * PRMS: virtaddr  - EA for which to find translation
113
 *
114
 * RTRN: 0         - no IMMU, IMMU disabled or ITLB miss
115
 *       else      - appropriate PA (note it IMMU is not present
116
 *                   PA === EA)
117
 */
118
unsigned long peek_into_itlb(unsigned long virtaddr)
119
{
120
  int set, way = -1;
121
  int i;
122
  unsigned long tagaddr;
123
  unsigned long vpn, ppn;
124
 
125
  if (!(mfspr(SPR_SR) & SPR_SR_IME) || !(testsprbits(SPR_UPR, SPR_UPR_IMP))) {
126
     return(virtaddr);
127
  }
128
 
129
  /* Which set to check out? */
130
  set = (virtaddr / config.immu.pagesize) % config.immu.nsets;
131
  tagaddr = (virtaddr / config.immu.pagesize) / config.immu.nsets;
132
  vpn = virtaddr / (config.immu.pagesize * config.immu.nsets);
133
 
134
  /* Scan all ways and try to find a matching way. */
135
  for (i = 0; i < config.immu.nways; i++)
136
    if (((mfspr(SPR_ITLBMR_BASE(i) + set) / (config.immu.pagesize * config.immu.nsets)) == vpn) &&
137
        testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
138
      way = i;
139
 
140
  /* Did we find our tlb entry? */
141
  if (way >= 0) { /* Yes, we did. */
142
 
143
    /* Test for page fault */
144
    if (mfspr (SPR_SR) & SPR_SR_SM) {
145
      if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_SXE)) {
146
        /* no luck, giving up */
147
        return(0);
148
      }
149
    } else {
150
      if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_UXE)) {
151
        /* no luck, giving up */
152
        return(0);
153
      }
154
    }
155
 
156
    ppn = mfspr(SPR_ITLBTR_BASE(way) + set) / config.immu.pagesize;
157
    return (ppn * config.immu.pagesize) + (virtaddr % config.immu.pagesize);
158
  }
159
  else {
160
    return(0);
161
  }
162
 
163
  PRINTF("ERR, should never have happened\n");
164
  return(0);
165
}
166
 
167
 
168 74 lampret
unsigned long immu_translate(unsigned long virtaddr)
169
{
170 429 markom
  unsigned long phyaddr = immu_simulate_tlb(virtaddr);
171
 
172 997 markom
/*  PRINTF("IMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
173 429 markom
  return phyaddr;
174 74 lampret
}
175
 
176
void itlb_info()
177
{
178 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
179 997 markom
    PRINTF("IMMU not implemented. Set UPR[IMP].\n");
180 429 markom
    return;
181
  }
182 102 lampret
 
183 997 markom
  PRINTF("Insn MMU %dKB: ", config.immu.nsets * config.immu.entrysize * config.immu.nways / 1024);
184
  PRINTF("%d ways, %d sets, entry size %d bytes\n", config.immu.nways, config.immu.nsets, config.immu.entrysize);
185 74 lampret
}
186
 
187
/* First check if virtual address is covered by ITLB and if it is:
188
    - increment ITLB read hit stats,
189 425 markom
    - set 'lru' at this way to config.immu.ustates - 1 and
190 74 lampret
      decrement 'lru' of other ways unless they have reached 0,
191
    - check page access attributes and invoke IMMU page fault exception
192
      handler if necessary
193
   and if not:
194
    - increment ITLB read miss stats
195
    - find lru way and entry and invoke ITLB miss exception handler
196 425 markom
    - set 'lru' with config.immu.ustates - 1 and decrement 'lru' of other
197 74 lampret
      ways unless they have reached 0
198
*/
199
 
200 102 lampret
void itlb_status(int start_set)
201 74 lampret
{
202 429 markom
  int set;
203
  int way;
204
  int end_set = config.immu.nsets;
205 74 lampret
 
206 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
207 997 markom
    PRINTF("IMMU not implemented. Set UPR[IMP].\n");
208 429 markom
    return;
209
  }
210 102 lampret
 
211 429 markom
  if ((start_set >= 0) && (start_set < end_set))
212
    end_set = start_set + 1;
213
  else
214
    start_set = 0;
215 74 lampret
 
216 997 markom
  if (start_set < end_set) PRINTF("\nIMMU: ");
217 429 markom
  /* Scan set(s) and way(s). */
218
  for (set = start_set; set < end_set; set++) {
219 997 markom
    PRINTF("\nSet %x: ", set);
220 429 markom
    for (way = 0; way < config.immu.nways; way++) {
221 997 markom
      PRINTF("  way %d: ", way);
222 1308 phoenix
      PRINTF("vpn=%lx ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_VPN));
223
      PRINTF("lru=%lx ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU));
224
      PRINTF("pl1=%lx ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_PL1));
225
      PRINTF("v=%lx ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_V));
226 429 markom
 
227 1308 phoenix
      PRINTF("a=%lx ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_A));
228
      PRINTF("d=%lx ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_D));
229
      PRINTF("uxe=%lx ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_UXE));
230
      PRINTF("sxe=%lx ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SXE));
231
      PRINTF("ppn=%lx ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN));
232 429 markom
    }
233
  }
234 997 markom
  if (start_set < end_set) PRINTF("\n");
235 74 lampret
}

powered by: WebSVN 2.1.0

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