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 1174

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

powered by: WebSVN 2.1.0

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