/* dmmu.c -- Data MMU simulation
|
/* dmmu.c -- Data 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. */
|
|
|
/* DMMU model (not functional yet, currently just copy of data cache). */
|
/* DMMU model (not functional yet, currently just copy of data cache). */
|
|
|
#include "dmmu.h"
|
#include "dmmu.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"
|
|
|
extern int cont_run;
|
extern int cont_run;
|
|
|
/* Data MMU */
|
/* Data MMU */
|
|
|
unsigned long dmmu_translate(unsigned long virtaddr)
|
unsigned long dmmu_translate(unsigned long virtaddr)
|
{
|
{
|
unsigned long phyaddr = dmmu_simulate_tlb(virtaddr);
|
unsigned long phyaddr = dmmu_simulate_tlb(virtaddr);
|
|
|
/* printf("DMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
|
/* printf("DMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
|
return phyaddr;
|
return phyaddr;
|
}
|
}
|
|
|
/* Number of DTLB sets used (power of 2, max is 256) */
|
/* Number of DTLB sets used (power of 2, max is 256) */
|
#define DTLB_SETS 16
|
#define DTLB_SETS 16
|
|
|
/* Entry size in bytes (8 == two singlewords) */
|
/* Entry size in bytes (8 == two singlewords) */
|
#define DTLB_ENTRY_SIZE 8
|
#define DTLB_ENTRY_SIZE 8
|
|
|
/* Number of DTLB ways (1, 2, 3 etc., max is 4). */
|
/* Number of DTLB ways (1, 2, 3 etc., max is 4). */
|
#define DTLB_WAYS 2
|
#define DTLB_WAYS 2
|
|
|
/* Number of usage states (2, 3, 4 etc., max is 4). */
|
/* Number of usage states (2, 3, 4 etc., max is 4). */
|
#define DTLB_USTATES 2
|
#define DTLB_USTATES 2
|
|
|
void dtlb_info()
|
void dtlb_info()
|
{
|
{
|
if (!getsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
printf("DMMU not implemented. Set UPR[DMP].\n");
|
printf("DMMU not implemented. Set UPR[DMP].\n");
|
return;
|
return;
|
}
|
}
|
|
|
printf("Data MMU %dKB: ", DTLB_SETS * DTLB_ENTRY_SIZE * DTLB_WAYS / 1024);
|
printf("Data MMU %dKB: ", DTLB_SETS * DTLB_ENTRY_SIZE * DTLB_WAYS / 1024);
|
printf("%d ways, %d sets, entry size %d bytes\n", DTLB_WAYS, DTLB_SETS, DTLB_ENTRY_SIZE);
|
printf("%d ways, %d sets, entry size %d bytes\n", DTLB_WAYS, DTLB_SETS, DTLB_ENTRY_SIZE);
|
}
|
}
|
|
|
/* First check if virtual address is covered by DTLB and if it is:
|
/* First check if virtual address is covered by DTLB and if it is:
|
- increment DTLB read hit stats,
|
- increment DTLB read hit stats,
|
- set 'lru' at this way to DTLB_USTATES - 1 and
|
- set 'lru' at this way to DTLB_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 DMMU page fault exception
|
- check page access attributes and invoke DMMU page fault exception
|
handler if necessary
|
handler if necessary
|
and if not:
|
and if not:
|
- increment DTLB read miss stats
|
- increment DTLB read miss stats
|
- find lru way and entry and invoke DTLB miss exception handler
|
- find lru way and entry and invoke DTLB miss exception handler
|
- set 'lru' with DTLB_USTATES - 1 and decrement 'lru' of other
|
- set 'lru' with DTLB_USTATES - 1 and decrement 'lru' of other
|
ways unless they have reached 0
|
ways unless they have reached 0
|
*/
|
*/
|
|
|
void dtlb_status(int start_set)
|
void dtlb_status(int start_set)
|
{
|
{
|
int set;
|
int set;
|
int way;
|
int way;
|
int end_set = DTLB_SETS;
|
int end_set = DTLB_SETS;
|
|
|
if (!getsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
|
printf("DMMU not implemented. Set UPR[DMP].\n");
|
printf("DMMU not implemented. Set UPR[DMP].\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;
|
|
|
printf("\nDMMU: ");
|
printf("\nDMMU: ");
|
/* 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 < DTLB_WAYS; way++) {
|
for (way = 0; way < DTLB_WAYS; way++) {
|
printf(" way %d: ", way);
|
printf(" way %d: ", way);
|
printf("vpn=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
|
printf("vpn=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
|
printf("lru=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
|
printf("lru=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
|
printf("pl1=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
|
printf("pl1=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
|
printf("v=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
|
printf("v=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
|
|
|
printf("a=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
|
printf("a=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
|
printf("d=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
|
printf("d=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
|
printf("ure=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
|
printf("ure=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
|
printf("uwe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
|
printf("uwe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
|
printf("sre=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
|
printf("sre=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
|
printf("swe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
|
printf("swe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
|
printf("ppn=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
|
printf("ppn=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
|
}
|
}
|
}
|
}
|
printf("\n");
|
printf("\n");
|
}
|
}
|
|
|
unsigned long dmmu_simulate_tlb(unsigned long virtaddr)
|
unsigned long dmmu_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;
|
unsigned long vpn;
|
|
|
if (!(mfspr(SPR_SR) & SPR_SR_DME) || (!getsprbits(SPR_SR, SPR_SR_DME)))
|
if (!(mfspr(SPR_SR) & SPR_SR_DME) || (!testsprbits(SPR_SR, SPR_SR_DME)))
|
return virtaddr;
|
return virtaddr;
|
|
|
/* Which set to check out? */
|
/* Which set to check out? */
|
set = (virtaddr / PAGE_SIZE) % DTLB_SETS;
|
set = (virtaddr / PAGE_SIZE) % DTLB_SETS;
|
tagaddr = (virtaddr / PAGE_SIZE) / DTLB_SETS;
|
tagaddr = (virtaddr / PAGE_SIZE) / DTLB_SETS;
|
vpn = virtaddr / PAGE_SIZE;
|
vpn = virtaddr / PAGE_SIZE;
|
|
|
/* Scan all ways and try to find a matching way. */
|
/* Scan all ways and try to find a matching way. */
|
for (i = 0; i < DTLB_WAYS; i++)
|
for (i = 0; i < DTLB_WAYS; i++)
|
if ((getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_VPN) == vpn) &&
|
if ((getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_VPN) == vpn) &&
|
getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
|
testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_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. */
|
dmmu_stats.loads_tlbhit++;
|
dmmu_stats.loads_tlbhit++;
|
debug("DTLB hit (virtaddr=%x).\n", virtaddr);
|
debug("DTLB hit (virtaddr=%x).\n", virtaddr);
|
|
|
/* Set LRUs */
|
/* Set LRUs */
|
for (i = 0; i < DTLB_WAYS; i++)
|
for (i = 0; i < DTLB_WAYS; i++)
|
if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
|
if (testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
|
setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
|
setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
|
setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
|
setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
|
|
|
return getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE);
|
return getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE);
|
}
|
}
|
else { /* No, we didn't. */
|
else { /* No, we didn't. */
|
int minlru = DTLB_USTATES - 1;
|
int minlru = DTLB_USTATES - 1;
|
int minway = 0;
|
int minway = 0;
|
|
|
dmmu_stats.loads_tlbmiss++;
|
dmmu_stats.loads_tlbmiss++;
|
#if 0
|
#if 0
|
for (i = 0; i < DTLB_WAYS; i++)
|
for (i = 0; i < DTLB_WAYS; i++)
|
if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
|
if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
|
minway = i;
|
minway = i;
|
|
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
|
for (i = 0; i < DTLB_WAYS; i++)
|
for (i = 0; i < DTLB_WAYS; i++)
|
if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
|
if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
|
setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
|
setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
|
setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
|
setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
|
setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
|
#endif
|
#endif
|
except_handle(EXCEPT_DTLBMISS, virtaddr);
|
except_handle(EXCEPT_DTLBMISS, virtaddr);
|
/* if tlb refill implemented in HW */
|
/* if tlb refill implemented in HW */
|
/* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE); */
|
/* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE); */
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
|
|