OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [mmu/] [immu.c] - Rev 344

Go to most recent revision | Compare with Previous | Blame | View Log

/* immu.c -- Instruction MMU simulation
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
 
This file is part of OpenRISC 1000 Architectural Simulator. 
 
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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
/* IMMU model (not functional yet, currently just copy of data cache). */
 
#include "immu.h"
#include "abstract.h"
#include "stats.h"
#include "sprs.h"
#include "except.h"
 
extern int cont_run;
 
/* Insn MMU */
 
unsigned long immu_translate(unsigned long virtaddr)
{
	unsigned long phyaddr = immu_simulate_tlb(virtaddr);
 
/*	printf("IMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
	return phyaddr;
}
 
/* Number of ITLB sets used (power of 2, max is 256) */
#define ITLB_SETS 16
 
/* Entry size in bytes (8 == two singlewords) */
#define ITLB_ENTRY_SIZE 8
 
/* Number of ITLB ways (1, 2, 3 etc., max is 4). */
#define ITLB_WAYS 2
 
/* Number of usage states (2, 3, 4 etc., max is 4). */
#define ITLB_USTATES 2
 
void itlb_info()
{
	if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
		printf("IMMU not implemented. Set UPR[IMP].\n");
		return;
	}
 
	printf("Insn MMU %dKB: ", ITLB_SETS * ITLB_ENTRY_SIZE * ITLB_WAYS / 1024);
	printf("%d ways, %d sets, entry size %d bytes\n", ITLB_WAYS, ITLB_SETS, ITLB_ENTRY_SIZE);
}
 
/* First check if virtual address is covered by ITLB and if it is:
    - increment ITLB read hit stats,
    - set 'lru' at this way to ITLB_USTATES - 1 and
      decrement 'lru' of other ways unless they have reached 0,
    - check page access attributes and invoke IMMU page fault exception
      handler if necessary
   and if not:
    - increment ITLB read miss stats
    - find lru way and entry and invoke ITLB miss exception handler
    - set 'lru' with ITLB_USTATES - 1 and decrement 'lru' of other
      ways unless they have reached 0
*/
 
void itlb_status(int start_set)
{
	int set;
	int way;
	int end_set = ITLB_SETS;
 
	if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
		printf("IMMU not implemented. Set UPR[IMP].\n");
		return;
	}
 
	if ((start_set >= 0) && (start_set < end_set))
		end_set = start_set + 1;
	else
		start_set = 0;
 
	printf("\nIMMU: ");
	/* Scan set(s) and way(s). */
	for (set = start_set; set < end_set; set++) {
		printf("\nSet %x: ", set);
		for (way = 0; way < ITLB_WAYS; way++) {
			printf("  way %d: ", way);
			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("pl1=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_PL1));
			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("d=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_D));
			printf("ure=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_URE));
			printf("uwe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_UWE));
			printf("sre=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SRE));
			printf("swe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SWE));
			printf("ppn=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN));
		}
	}
	printf("\n");
}
 
unsigned long immu_simulate_tlb(unsigned long virtaddr)
{
	int set, way = -1;
	int i;
	unsigned long tagaddr;
	unsigned long vpn;
 
	if (!(mfspr(SPR_SR) & SPR_SR_IME) || !(testsprbits(SPR_UPR, SPR_UPR_IMP)))
		return virtaddr;
 
	/* Which set to check out? */
	set = (virtaddr / PAGE_SIZE) % ITLB_SETS;
	tagaddr = (virtaddr / PAGE_SIZE) / ITLB_SETS;
	vpn = virtaddr / PAGE_SIZE;
 
	/* Scan all ways and try to find a matching way. */
	for (i = 0; i < ITLB_WAYS; i++)
		if ((getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_VPN) == vpn) &&
		    testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
			way = i;
 
	/* Did we find our tlb entry? */
	if (way >= 0) { /* Yes, we did. */
		immu_stats.fetch_tlbhit++;
		debug(5, "ITLB hit (virtaddr=%x).\n", virtaddr);
 
		/* Set LRUs */
		for (i = 0; i < ITLB_WAYS; i++)
			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(way) + set, SPR_ITLBMR_LRU, ITLB_USTATES - 1);
 
		return getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE);
	}
	else {	/* No, we didn't. */
		int minlru = ITLB_USTATES - 1;
		int minway = 0;
 
		immu_stats.fetch_tlbmiss++;
		cont_run=0;
#if 0
		for (i = 0; i < ITLB_WAYS; i++)
			if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) < minlru)
				minway = i;
 
		setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_VPN, vpn);
		for (i = 0; i < ITLB_WAYS; i++)
			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(minway) + set, SPR_ITLBMR_LRU, ITLB_USTATES - 1);
		setsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN, vpn); /* 1 to 1 */
		setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_V, 1);
#endif
		except_handle(EXCEPT_ITLBMISS, virtaddr);
		/* if tlb refill implemented in HW */
		/* return getsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE); */
		return 0;
	}
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

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