/* immu.c -- Instruction MMU simulation
|
/* immu.c -- Instruction MMU simulation
|
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
|
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
|
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 2 of the License, or
|
the Free Software Foundation; either version 2 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
/* IMMU model (not functional yet, currently just copy of data cache). */
|
/* IMMU model (not functional yet, currently just copy of data cache). */
|
|
|
#include "immu.h"
|
#include "immu.h"
|
#include "abstract.h"
|
#include "abstract.h"
|
#include "stats.h"
|
#include "stats.h"
|
#include "sprs.h"
|
#include "sprs.h"
|
#include "except.h"
|
#include "except.h"
|
#include "sim-config.h"
|
#include "sim-config.h"
|
|
|
extern int cont_run;
|
extern int cont_run;
|
|
|
/* Insn MMU */
|
/* Insn MMU */
|
|
|
static inline unsigned long immu_simulate_tlb(unsigned long virtaddr)
|
static inline unsigned long immu_simulate_tlb(unsigned long virtaddr)
|
{
|
{
|
int set, way = -1;
|
int set, way = -1;
|
int i;
|
int i;
|
unsigned long tagaddr;
|
unsigned long tagaddr;
|
unsigned long vpn, ppn;
|
unsigned long vpn, ppn;
|
extern int mem_cycles;
|
extern int mem_cycles;
|
|
|
if (!(mfspr(SPR_SR) & SPR_SR_IME) || !(testsprbits(SPR_UPR, SPR_UPR_IMP))) {
|
if (!(mfspr(SPR_SR) & SPR_SR_IME) || !(testsprbits(SPR_UPR, SPR_UPR_IMP))) {
|
insn_ci = (virtaddr >= 0x80000000);
|
insn_ci = (virtaddr >= 0x80000000);
|
return virtaddr;
|
return virtaddr;
|
}
|
}
|
|
|
/* Which set to check out? */
|
/* Which set to check out? */
|
set = (virtaddr / config.immu.pagesize) % config.immu.nsets;
|
set = (virtaddr / config.immu.pagesize) % config.immu.nsets;
|
tagaddr = (virtaddr / config.immu.pagesize) / config.immu.nsets;
|
tagaddr = (virtaddr / config.immu.pagesize) / config.immu.nsets;
|
vpn = virtaddr / (config.immu.pagesize * config.immu.nsets);
|
vpn = virtaddr / (config.immu.pagesize * config.immu.nsets);
|
|
|
/* Scan all ways and try to find a matching way. */
|
/* Scan all ways and try to find a matching way. */
|
for (i = 0; i < config.immu.nways; i++)
|
for (i = 0; i < config.immu.nways; i++)
|
if (((mfspr(SPR_ITLBMR_BASE(i) + set) / (config.immu.pagesize * config.immu.nsets)) == vpn) &&
|
if (((mfspr(SPR_ITLBMR_BASE(i) + set) / (config.immu.pagesize * config.immu.nsets)) == vpn) &&
|
testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
|
testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
|
way = i;
|
way = i;
|
|
|
/* Did we find our tlb entry? */
|
/* Did we find our tlb entry? */
|
if (way >= 0) { /* Yes, we did. */
|
if (way >= 0) { /* Yes, we did. */
|
immu_stats.fetch_tlbhit++;
|
immu_stats.fetch_tlbhit++;
|
debug(5, "ITLB hit (virtaddr=%x).\n", virtaddr);
|
debug(5, "ITLB hit (virtaddr=%x).\n", virtaddr);
|
|
|
/* Test for page fault */
|
/* Test for page fault */
|
if (mfspr (SPR_SR) & SPR_SR_SM) {
|
if (mfspr (SPR_SR) & SPR_SR_SM) {
|
if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_SXE))
|
if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_SXE))
|
except_handle(EXCEPT_IPF, virtaddr);
|
except_handle(EXCEPT_IPF, virtaddr);
|
} else {
|
} else {
|
if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_UXE))
|
if (!(mfspr (SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_UXE))
|
except_handle(EXCEPT_IPF, virtaddr);
|
except_handle(EXCEPT_IPF, virtaddr);
|
}
|
}
|
|
|
/* Set LRUs */
|
/* Set LRUs */
|
for (i = 0; i < config.immu.nways; i++)
|
for (i = 0; i < config.immu.nways; i++)
|
if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
|
if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
|
setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
|
setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
|
setsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU, config.immu.ustates - 1);
|
setsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU, config.immu.ustates - 1);
|
|
|
/* Check if page is cache inhibited */
|
/* Check if page is cache inhibited */
|
insn_ci = (mfspr(SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_CI) == SPR_ITLBTR_CI;
|
insn_ci = (mfspr(SPR_ITLBTR_BASE(way) + set) & SPR_ITLBTR_CI) == SPR_ITLBTR_CI;
|
|
|
mem_cycles += config.immu.hitdelay;
|
mem_cycles += config.immu.hitdelay;
|
ppn = mfspr(SPR_ITLBTR_BASE(way) + set) / config.immu.pagesize;
|
ppn = mfspr(SPR_ITLBTR_BASE(way) + set) / config.immu.pagesize;
|
return (ppn * config.immu.pagesize) + (virtaddr % config.immu.pagesize);
|
return (ppn * config.immu.pagesize) + (virtaddr % config.immu.pagesize);
|
}
|
}
|
else { /* No, we didn't. */
|
else { /* No, we didn't. */
|
int minlru = config.immu.ustates - 1;
|
int minlru = config.immu.ustates - 1;
|
int minway = 0;
|
int minway = 0;
|
|
|
immu_stats.fetch_tlbmiss++;
|
immu_stats.fetch_tlbmiss++;
|
#if 0
|
#if 0
|
for (i = 0; i < config.immu.nways; i++)
|
for (i = 0; i < config.immu.nways; i++)
|
if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) < minlru)
|
if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) < minlru)
|
minway = i;
|
minway = i;
|
|
|
setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_VPN, vpn);
|
setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_VPN, vpn);
|
for (i = 0; i < config.immu.nways; i++)
|
for (i = 0; i < config.immu.nways; i++)
|
if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
|
if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
|
setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
|
setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
|
setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_LRU, config.immu.ustates - 1);
|
setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_LRU, config.immu.ustates - 1);
|
setsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN, vpn); /* 1 to 1 */
|
setsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN, vpn); /* 1 to 1 */
|
setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_V, 1);
|
setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_V, 1);
|
#endif
|
#endif
|
except_handle(EXCEPT_ITLBMISS, virtaddr);
|
except_handle(EXCEPT_ITLBMISS, virtaddr);
|
/* if tlb refill implemented in HW */
|
/* if tlb refill implemented in HW */
|
/* return getsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN) * config.immu.pagesize + (virtaddr % config.immu.pagesize); */
|
/* return getsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN) * config.immu.pagesize + (virtaddr % config.immu.pagesize); */
|
mem_cycles += config.immu.missdelay;
|
mem_cycles += config.immu.missdelay;
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
|
|
unsigned long immu_translate(unsigned long virtaddr)
|
unsigned long immu_translate(unsigned long virtaddr)
|
{
|
{
|
unsigned long phyaddr = immu_simulate_tlb(virtaddr);
|
unsigned long phyaddr = immu_simulate_tlb(virtaddr);
|
|
|
/* printf("IMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
|
/* printf("IMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
|
return phyaddr;
|
return phyaddr;
|
}
|
}
|
|
|
void itlb_info()
|
void itlb_info()
|
{
|
{
|
if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
|
if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
|
printf("IMMU not implemented. Set UPR[IMP].\n");
|
printf("IMMU not implemented. Set UPR[IMP].\n");
|
return;
|
return;
|
}
|
}
|
|
|
printf("Insn MMU %dKB: ", config.immu.nsets * config.immu.entrysize * config.immu.nways / 1024);
|
printf("Insn MMU %dKB: ", config.immu.nsets * config.immu.entrysize * config.immu.nways / 1024);
|
printf("%d ways, %d sets, entry size %d bytes\n", config.immu.nways, config.immu.nsets, config.immu.entrysize);
|
printf("%d ways, %d sets, entry size %d bytes\n", config.immu.nways, config.immu.nsets, config.immu.entrysize);
|
}
|
}
|
|
|
/* First check if virtual address is covered by ITLB and if it is:
|
/* First check if virtual address is covered by ITLB and if it is:
|
- increment ITLB read hit stats,
|
- increment ITLB read hit stats,
|
- set 'lru' at this way to config.immu.ustates - 1 and
|
- set 'lru' at this way to config.immu.ustates - 1 and
|
decrement 'lru' of other ways unless they have reached 0,
|
decrement 'lru' of other ways unless they have reached 0,
|
- check page access attributes and invoke IMMU page fault exception
|
- check page access attributes and invoke IMMU page fault exception
|
handler if necessary
|
handler if necessary
|
and if not:
|
and if not:
|
- increment ITLB read miss stats
|
- increment ITLB read miss stats
|
- find lru way and entry and invoke ITLB miss exception handler
|
- find lru way and entry and invoke ITLB miss exception handler
|
- set 'lru' with config.immu.ustates - 1 and decrement 'lru' of other
|
- set 'lru' with config.immu.ustates - 1 and decrement 'lru' of other
|
ways unless they have reached 0
|
ways unless they have reached 0
|
*/
|
*/
|
|
|
void itlb_status(int start_set)
|
void itlb_status(int start_set)
|
{
|
{
|
int set;
|
int set;
|
int way;
|
int way;
|
int end_set = config.immu.nsets;
|
int end_set = config.immu.nsets;
|
|
|
if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
|
if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
|
printf("IMMU not implemented. Set UPR[IMP].\n");
|
printf("IMMU not implemented. Set UPR[IMP].\n");
|
return;
|
return;
|
}
|
}
|
|
|
if ((start_set >= 0) && (start_set < end_set))
|
if ((start_set >= 0) && (start_set < end_set))
|
end_set = start_set + 1;
|
end_set = start_set + 1;
|
else
|
else
|
start_set = 0;
|
start_set = 0;
|
|
|
if (start_set < end_set) printf("\nIMMU: ");
|
if (start_set < end_set) printf("\nIMMU: ");
|
/* Scan set(s) and way(s). */
|
/* Scan set(s) and way(s). */
|
for (set = start_set; set < end_set; set++) {
|
for (set = start_set; set < end_set; set++) {
|
printf("\nSet %x: ", set);
|
printf("\nSet %x: ", set);
|
for (way = 0; way < config.immu.nways; way++) {
|
for (way = 0; way < config.immu.nways; way++) {
|
printf(" way %d: ", way);
|
printf(" way %d: ", way);
|
printf("vpn=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_VPN));
|
printf("vpn=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_VPN));
|
printf("lru=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU));
|
printf("lru=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU));
|
printf("pl1=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_PL1));
|
printf("pl1=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_PL1));
|
printf("v=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_V));
|
printf("v=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_V));
|
|
|
printf("a=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_A));
|
printf("a=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_A));
|
printf("d=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_D));
|
printf("d=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_D));
|
printf("uxe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_UXE));
|
printf("uxe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_UXE));
|
printf("sxe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SXE));
|
printf("sxe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SXE));
|
printf("ppn=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN));
|
printf("ppn=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN));
|
}
|
}
|
}
|
}
|
if (start_set < end_set) printf("\n");
|
if (start_set < end_set) printf("\n");
|
}
|
}
|
|
|