1 |
62 |
lampret |
/* dmmu.c -- Data MMU simulation
|
2 |
6 |
lampret |
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 |
|
|
/* DMMU model (not functional yet, currently just copy of data cache). */
|
21 |
|
|
|
22 |
|
|
#include "dmmu.h"
|
23 |
|
|
#include "abstract.h"
|
24 |
1344 |
nogj |
#include "opcode/or32.h"
|
25 |
6 |
lampret |
#include "stats.h"
|
26 |
62 |
lampret |
#include "sprs.h"
|
27 |
|
|
#include "except.h"
|
28 |
425 |
markom |
#include "sim-config.h"
|
29 |
1308 |
phoenix |
#include "debug.h"
|
30 |
6 |
lampret |
|
31 |
62 |
lampret |
extern int cont_run;
|
32 |
|
|
|
33 |
6 |
lampret |
/* Data MMU */
|
34 |
|
|
|
35 |
430 |
markom |
inline unsigned long dmmu_simulate_tlb(unsigned long virtaddr, int write_access)
|
36 |
6 |
lampret |
{
|
37 |
430 |
markom |
int set, way = -1;
|
38 |
|
|
int i;
|
39 |
|
|
unsigned long tagaddr;
|
40 |
456 |
simons |
unsigned long vpn, ppn;
|
41 |
572 |
simons |
|
42 |
638 |
simons |
if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
43 |
|
|
data_ci = (virtaddr >= 0x80000000);
|
44 |
430 |
markom |
return virtaddr;
|
45 |
638 |
simons |
}
|
46 |
430 |
markom |
|
47 |
|
|
/* Which set to check out? */
|
48 |
|
|
set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
|
49 |
|
|
tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
|
50 |
456 |
simons |
vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
|
51 |
|
|
|
52 |
430 |
markom |
/* Scan all ways and try to find a matching way. */
|
53 |
|
|
for (i = 0; i < config.dmmu.nways; i++)
|
54 |
456 |
simons |
if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
|
55 |
430 |
markom |
testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
|
56 |
|
|
way = i;
|
57 |
456 |
simons |
|
58 |
|
|
/* Did we find our tlb entry? */
|
59 |
430 |
markom |
if (way >= 0) { /* Yes, we did. */
|
60 |
|
|
dmmu_stats.loads_tlbhit++;
|
61 |
|
|
debug(5, "DTLB hit (virtaddr=%x).\n", virtaddr);
|
62 |
|
|
|
63 |
|
|
/* Test for page fault */
|
64 |
600 |
simons |
if (mfspr (SPR_SR) & SPR_SR_SM) {
|
65 |
438 |
simons |
if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
|
66 |
|
|
|| !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
|
67 |
430 |
markom |
except_handle(EXCEPT_DPF, virtaddr);
|
68 |
|
|
} else {
|
69 |
438 |
simons |
if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
|
70 |
|
|
|| !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
|
71 |
430 |
markom |
except_handle(EXCEPT_DPF, virtaddr);
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
/* Set LRUs */
|
75 |
|
|
for (i = 0; i < config.dmmu.nways; i++)
|
76 |
|
|
if (testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
|
77 |
|
|
setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
|
78 |
886 |
simons |
setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, config.dmmu.nsets - 1);
|
79 |
430 |
markom |
|
80 |
638 |
simons |
/* Check if page is cache inhibited */
|
81 |
|
|
data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
|
82 |
|
|
|
83 |
884 |
markom |
runtime.sim.mem_cycles += config.dmmu.hitdelay;
|
84 |
456 |
simons |
ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
|
85 |
|
|
return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
|
86 |
430 |
markom |
}
|
87 |
|
|
else { /* No, we didn't. */
|
88 |
|
|
dmmu_stats.loads_tlbmiss++;
|
89 |
|
|
#if 0
|
90 |
|
|
for (i = 0; i < config.dmmu.nways; i++)
|
91 |
|
|
if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
|
92 |
|
|
minway = i;
|
93 |
|
|
|
94 |
|
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
|
95 |
|
|
for (i = 0; i < config.dmmu.nways; i++)
|
96 |
|
|
if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
|
97 |
|
|
setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
|
98 |
|
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
|
99 |
|
|
setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
|
100 |
|
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
|
101 |
|
|
#endif
|
102 |
|
|
except_handle(EXCEPT_DTLBMISS, virtaddr);
|
103 |
|
|
/* if tlb refill implemented in HW */
|
104 |
|
|
/* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * config.dmmu.pagesize + (virtaddr % config.dmmu.pagesize); */
|
105 |
884 |
markom |
runtime.sim.mem_cycles += config.dmmu.missdelay;
|
106 |
430 |
markom |
return 0;
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
1240 |
phoenix |
/* DESC: try to find EA -> PA transaltion without changing
|
111 |
|
|
* any of precessor states. if this is not passible gives up
|
112 |
|
|
* (without triggering exceptions)
|
113 |
|
|
*
|
114 |
|
|
* PRMS: virtaddr - EA for which to find translation
|
115 |
|
|
*
|
116 |
|
|
* write_access - 0 ignore testing for write access
|
117 |
|
|
* 1 test for write access, if fails
|
118 |
|
|
* do not return translation
|
119 |
|
|
*
|
120 |
|
|
* through_dc - 1 go through data cache
|
121 |
|
|
* 0 ignore data cache
|
122 |
|
|
*
|
123 |
|
|
* RTRN: 0 - no DMMU, DMMU disabled or ITLB miss
|
124 |
|
|
* else - appropriate PA (note it DMMU is not present
|
125 |
|
|
* PA === EA)
|
126 |
|
|
*/
|
127 |
|
|
unsigned long peek_into_dtlb(unsigned long virtaddr, int write_access,
|
128 |
|
|
int through_dc)
|
129 |
|
|
{
|
130 |
|
|
int set, way = -1;
|
131 |
|
|
int i;
|
132 |
|
|
unsigned long tagaddr;
|
133 |
|
|
unsigned long vpn, ppn;
|
134 |
|
|
|
135 |
|
|
if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
136 |
|
|
if (through_dc)
|
137 |
|
|
data_ci = (virtaddr >= 0x80000000);
|
138 |
|
|
return virtaddr;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
/* Which set to check out? */
|
142 |
|
|
set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
|
143 |
|
|
tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
|
144 |
|
|
vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
|
145 |
|
|
|
146 |
|
|
/* Scan all ways and try to find a matching way. */
|
147 |
|
|
for (i = 0; i < config.dmmu.nways; i++)
|
148 |
|
|
if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
|
149 |
|
|
testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
|
150 |
|
|
way = i;
|
151 |
|
|
|
152 |
|
|
/* Did we find our tlb entry? */
|
153 |
|
|
if (way >= 0) { /* Yes, we did. */
|
154 |
|
|
dmmu_stats.loads_tlbhit++;
|
155 |
|
|
debug(5, "DTLB hit (virtaddr=%x).\n", virtaddr);
|
156 |
|
|
|
157 |
|
|
/* Test for page fault */
|
158 |
|
|
if (mfspr (SPR_SR) & SPR_SR_SM) {
|
159 |
|
|
if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
|
160 |
|
|
|| !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
|
161 |
|
|
|
162 |
|
|
/* otherwise exception DPF would be raised */
|
163 |
|
|
return(0);
|
164 |
|
|
} else {
|
165 |
|
|
if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
|
166 |
|
|
|| !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
|
167 |
|
|
|
168 |
|
|
/* otherwise exception DPF would be raised */
|
169 |
|
|
return(0);
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
if (through_dc) {
|
173 |
|
|
/* Check if page is cache inhibited */
|
174 |
|
|
data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
|
178 |
|
|
return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
|
179 |
|
|
}
|
180 |
|
|
else { /* No, we didn't. */
|
181 |
|
|
return(0);
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
PRINTF("ERR, should never have happened\n");
|
185 |
|
|
return(0);
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
|
189 |
430 |
markom |
unsigned long dmmu_translate(unsigned long virtaddr, int write_access)
|
190 |
|
|
{
|
191 |
|
|
unsigned long phyaddr = dmmu_simulate_tlb(virtaddr, write_access);
|
192 |
429 |
markom |
|
193 |
997 |
markom |
/* PRINTF("DMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
|
194 |
429 |
markom |
return phyaddr;
|
195 |
6 |
lampret |
}
|
196 |
|
|
|
197 |
|
|
|
198 |
62 |
lampret |
void dtlb_info()
|
199 |
6 |
lampret |
{
|
200 |
429 |
markom |
if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
201 |
997 |
markom |
PRINTF("DMMU not implemented. Set UPR[DMP].\n");
|
202 |
429 |
markom |
return;
|
203 |
|
|
}
|
204 |
|
|
|
205 |
997 |
markom |
PRINTF("Data MMU %dKB: ", config.dmmu.nsets * config.dmmu.entrysize * config.dmmu.nways / 1024);
|
206 |
|
|
PRINTF("%d ways, %d sets, entry size %d bytes\n", config.dmmu.nways, config.dmmu.nsets, config.dmmu.entrysize);
|
207 |
6 |
lampret |
}
|
208 |
|
|
|
209 |
62 |
lampret |
/* First check if virtual address is covered by DTLB and if it is:
|
210 |
|
|
- increment DTLB read hit stats,
|
211 |
425 |
markom |
- set 'lru' at this way to config.dmmu.ustates - 1 and
|
212 |
6 |
lampret |
decrement 'lru' of other ways unless they have reached 0,
|
213 |
62 |
lampret |
- check page access attributes and invoke DMMU page fault exception
|
214 |
|
|
handler if necessary
|
215 |
6 |
lampret |
and if not:
|
216 |
62 |
lampret |
- increment DTLB read miss stats
|
217 |
|
|
- find lru way and entry and invoke DTLB miss exception handler
|
218 |
425 |
markom |
- set 'lru' with config.dmmu.ustates - 1 and decrement 'lru' of other
|
219 |
6 |
lampret |
ways unless they have reached 0
|
220 |
|
|
*/
|
221 |
|
|
|
222 |
102 |
lampret |
void dtlb_status(int start_set)
|
223 |
6 |
lampret |
{
|
224 |
429 |
markom |
int set;
|
225 |
|
|
int way;
|
226 |
|
|
int end_set = config.dmmu.nsets;
|
227 |
62 |
lampret |
|
228 |
429 |
markom |
if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
229 |
997 |
markom |
PRINTF("DMMU not implemented. Set UPR[DMP].\n");
|
230 |
429 |
markom |
return;
|
231 |
|
|
}
|
232 |
102 |
lampret |
|
233 |
429 |
markom |
if ((start_set >= 0) && (start_set < end_set))
|
234 |
|
|
end_set = start_set + 1;
|
235 |
|
|
else
|
236 |
|
|
start_set = 0;
|
237 |
62 |
lampret |
|
238 |
997 |
markom |
if (start_set < end_set) PRINTF("\nDMMU: ");
|
239 |
429 |
markom |
/* Scan set(s) and way(s). */
|
240 |
|
|
for (set = start_set; set < end_set; set++) {
|
241 |
997 |
markom |
PRINTF("\nSet %x: ", set);
|
242 |
429 |
markom |
for (way = 0; way < config.dmmu.nways; way++) {
|
243 |
997 |
markom |
PRINTF(" way %d: ", way);
|
244 |
1308 |
phoenix |
PRINTF("vpn=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
|
245 |
|
|
PRINTF("lru=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
|
246 |
|
|
PRINTF("pl1=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
|
247 |
|
|
PRINTF("v=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
|
248 |
429 |
markom |
|
249 |
1308 |
phoenix |
PRINTF("a=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
|
250 |
|
|
PRINTF("d=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
|
251 |
|
|
PRINTF("ure=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
|
252 |
|
|
PRINTF("uwe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
|
253 |
|
|
PRINTF("sre=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
|
254 |
|
|
PRINTF("swe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
|
255 |
|
|
PRINTF("ppn=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
|
256 |
429 |
markom |
}
|
257 |
|
|
}
|
258 |
997 |
markom |
if (start_set < end_set) PRINTF("\n");
|
259 |
6 |
lampret |
}
|