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 1308

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

powered by: WebSVN 2.1.0

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