Home | History | Annotate | Line # | Download | only in sh3
sh_mmu.cpp revision 1.6.70.1
      1  1.6.70.1  yamt /*	$NetBSD: sh_mmu.cpp,v 1.6.70.1 2008/05/18 12:32:02 yamt Exp $	*/
      2       1.1   uch 
      3       1.1   uch /*-
      4       1.2   uch  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
      5       1.1   uch  * All rights reserved.
      6       1.1   uch  *
      7       1.1   uch  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1   uch  * by UCHIYAMA Yasushi.
      9       1.1   uch  *
     10       1.1   uch  * Redistribution and use in source and binary forms, with or without
     11       1.1   uch  * modification, are permitted provided that the following conditions
     12       1.1   uch  * are met:
     13       1.1   uch  * 1. Redistributions of source code must retain the above copyright
     14       1.1   uch  *    notice, this list of conditions and the following disclaimer.
     15       1.1   uch  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   uch  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   uch  *    documentation and/or other materials provided with the distribution.
     18       1.1   uch  *
     19       1.1   uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   uch  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   uch  */
     31       1.1   uch 
     32       1.1   uch #include <sh3/sh_arch.h>
     33       1.1   uch #include <sh3/sh_mmu.h>
     34       1.1   uch 
     35       1.3   uch #include <sh3/cpu/sh3.h>
     36       1.3   uch #include <sh3/cpu/sh4.h>
     37       1.1   uch 
     38       1.3   uch //
     39       1.3   uch // Get physical address from memory mapped TLB.
     40       1.3   uch // SH3 version. SH4 can't do this method. because address/data array must be
     41       1.3   uch // accessed from P2.
     42       1.3   uch //
     43       1.1   uch paddr_t
     44       1.1   uch MemoryManager_SHMMU::searchPage(vaddr_t vaddr)
     45       1.1   uch {
     46       1.6   uwe 	uint32_t vpn, idx, s, dum, aae, dae, entry_idx, asid;
     47       1.1   uch 	paddr_t paddr = ~0;
     48       1.3   uch 	int way, kmode;
     49       1.1   uch 
     50       1.2   uch 	vpn = vaddr & SH3_PAGE_MASK;
     51       1.1   uch 	// Windows CE uses VPN-only index-mode.
     52       1.3   uch 	idx = vaddr & SH3_MMU_VPN_MASK;
     53       1.3   uch 
     54       1.3   uch 	kmode = SetKMode(1);
     55       1.4   uch 	// Get current ASID
     56       1.3   uch 	asid = _reg_read_4(SH3_PTEH) & SH3_PTEH_ASID_MASK;
     57       1.1   uch 
     58       1.1   uch 	// to avoid another TLB access, disable external interrupt.
     59       1.1   uch 	s = suspendIntr();
     60       1.1   uch 
     61       1.1   uch 	do {
     62       1.1   uch 		// load target address page to TLB
     63       1.3   uch 		dum = _reg_read_4(vaddr);
     64       1.3   uch 		_reg_write_4(vaddr, dum);
     65       1.1   uch 
     66       1.3   uch 		for (way = 0; way < SH3_MMU_WAY; way++) {
     67       1.3   uch 			entry_idx = idx | (way << SH3_MMU_WAY_SHIFT);
     68       1.1   uch 			// inquire MMU address array.
     69       1.3   uch 			aae = _reg_read_4(SH3_MMUAA | entry_idx);
     70       1.4   uch 
     71       1.3   uch 			if (!(aae & SH3_MMU_D_VALID) ||
     72       1.3   uch 			    ((aae & SH3_MMUAA_D_ASID_MASK) != asid) ||
     73       1.2   uch 			    (((aae | idx) & SH3_PAGE_MASK) != vpn))
     74       1.1   uch 				continue;
     75       1.1   uch 
     76       1.1   uch 			// entry found.
     77       1.1   uch 			// inquire MMU data array to get its physical address.
     78       1.3   uch 			dae = _reg_read_4(SH3_MMUDA | entry_idx);
     79       1.2   uch 			paddr = (dae & SH3_PAGE_MASK) | (vaddr & ~SH3_PAGE_MASK);
     80       1.1   uch 			break;
     81       1.1   uch 		}
     82       1.1   uch 	} while (paddr == ~0);
     83       1.1   uch 
     84       1.1   uch 	resumeIntr(s);
     85       1.3   uch 	SetKMode(kmode);
     86       1.1   uch 
     87       1.1   uch 	return paddr;
     88       1.1   uch }
     89       1.1   uch 
     90       1.3   uch void
     91       1.3   uch MemoryManager_SHMMU::CacheDump()
     92       1.3   uch {
     93       1.3   uch 	static const char *able[] = {"dis", "en" };
     94       1.3   uch 	int write_through_p0_u0_p3;
     95       1.3   uch 	int write_through_p1;
     96       1.6   uwe 	uint32_t r;
     97       1.3   uch 	int kmode;
     98       1.3   uch 
     99       1.3   uch 	DPRINTF_SETUP();
    100       1.3   uch 
    101       1.3   uch 	kmode = SetKMode(1);
    102       1.3   uch 	switch (SHArchitecture::cpu_type()) {
    103       1.3   uch 	default:
    104       1.4   uch 		DPRINTF((TEXT("unknown architecture.\n")));
    105       1.3   uch 		SetKMode(kmode);
    106       1.3   uch 		return;
    107       1.3   uch 	case 3:
    108       1.3   uch 		r = _reg_read_4(SH3_CCR);
    109       1.3   uch 		DPRINTF((TEXT("cache %Sabled"),
    110       1.3   uch 		    able[(r & SH3_CCR_CE ? 1 : 0)]));
    111       1.3   uch 		if (r & SH3_CCR_RA)
    112       1.3   uch 			DPRINTF((TEXT(" ram-mode")));
    113       1.3   uch 
    114       1.3   uch 		write_through_p0_u0_p3 = r & SH3_CCR_WT;
    115       1.3   uch 		write_through_p1 = !(r & SH3_CCR_CB);
    116       1.3   uch 		break;
    117       1.3   uch 	case 4:
    118       1.3   uch 		r = _reg_read_4(SH4_CCR);
    119       1.3   uch 		DPRINTF((TEXT("I-cache %Sabled"),
    120       1.3   uch 		    able[(r & SH4_CCR_ICE) ? 1 : 0]));
    121       1.3   uch 		if (r & SH4_CCR_IIX)
    122       1.3   uch 			DPRINTF((TEXT(" index-mode ")));
    123       1.3   uch 		DPRINTF((TEXT(" D-cache %Sabled"),
    124       1.3   uch 		    able[(r & SH4_CCR_OCE) ? 1 : 0]));
    125       1.3   uch 		if (r & SH4_CCR_OIX)
    126       1.3   uch 			DPRINTF((TEXT(" index-mode")));
    127       1.3   uch 		if (r & SH4_CCR_ORA)
    128       1.3   uch 			DPRINTF((TEXT(" ram-mode")));
    129       1.3   uch 
    130       1.3   uch 		write_through_p0_u0_p3 = r & SH4_CCR_WT;
    131       1.3   uch 		write_through_p1 = !(r & SH4_CCR_CB);
    132       1.3   uch 		break;
    133       1.3   uch 	}
    134       1.3   uch 	DPRINTF((TEXT(".")));
    135       1.3   uch 
    136       1.4   uch 	// Write-through/back
    137       1.3   uch 	DPRINTF((TEXT(" P0, U0, P3 write-%S P1 write-%S\n"),
    138       1.3   uch 	    write_through_p0_u0_p3 ? "through" : "back",
    139       1.3   uch 	    write_through_p1 ? "through" : "back"));
    140       1.3   uch 
    141       1.3   uch 	SetKMode(kmode);
    142       1.3   uch }
    143       1.3   uch 
    144       1.3   uch void
    145       1.3   uch MemoryManager_SHMMU::MMUDump()
    146       1.3   uch {
    147       1.4   uch #define	ON(x, c)	((x) & (c) ? '|' : '.')
    148       1.6   uwe 	uint32_t r, e, a;
    149       1.3   uch 	int i, kmode;
    150       1.3   uch 
    151       1.3   uch 	DPRINTF_SETUP();
    152       1.3   uch 
    153       1.3   uch 	kmode = SetKMode(1);
    154       1.3   uch 	DPRINTF((TEXT("MMU:\n")));
    155       1.3   uch 	switch (SHArchitecture::cpu_type()) {
    156       1.3   uch 	default:
    157       1.4   uch 		DPRINTF((TEXT("unknown architecture.\n")));
    158       1.3   uch 		SetKMode(kmode);
    159       1.3   uch 		return;
    160       1.3   uch 	case 3:
    161       1.3   uch 		r = _reg_read_4(SH3_MMUCR);
    162       1.3   uch 		if (!(r & SH3_MMUCR_AT))
    163       1.3   uch 			goto disabled;
    164       1.3   uch 
    165       1.3   uch 		// MMU configuration.
    166       1.3   uch 		DPRINTF((TEXT("%s index-mode, %s virtual storage mode\n"),
    167       1.3   uch 		    r & SH3_MMUCR_IX
    168       1.3   uch 		    ? TEXT("ASID + VPN") : TEXT("VPN only"),
    169       1.3   uch 		    r & SH3_MMUCR_SV ? TEXT("single") : TEXT("multiple")));
    170       1.3   uch 
    171       1.3   uch 		// Dump TLB.
    172       1.3   uch 		DPRINTF((TEXT("---TLB---\n")));
    173       1.3   uch 		DPRINTF((TEXT("   VPN    ASID    PFN     VDCG PR SZ\n")));
    174       1.3   uch 		for (i = 0; i < SH3_MMU_WAY; i++) {
    175       1.3   uch 			DPRINTF((TEXT(" [way %d]\n"), i));
    176       1.3   uch 			for (e = 0; e < SH3_MMU_ENTRY; e++) {
    177       1.3   uch 				// address/data array common offset.
    178       1.3   uch 				a = (e << SH3_MMU_VPN_SHIFT) |
    179       1.3   uch 				    (i << SH3_MMU_WAY_SHIFT);
    180       1.3   uch 
    181       1.3   uch 				r = _reg_read_4(SH3_MMUAA | a);
    182       1.3   uch 				DPRINTF((TEXT("0x%08x %3d"),
    183       1.3   uch 				    r & SH3_MMUAA_D_VPN_MASK,
    184       1.3   uch 				    r & SH3_MMUAA_D_ASID_MASK));
    185       1.3   uch 				r = _reg_read_4(SH3_MMUDA | a);
    186       1.3   uch 				DPRINTF((TEXT(" 0x%08x %c%c%c%c  %d %dK\n"),
    187       1.3   uch 				    r & SH3_MMUDA_D_PPN_MASK,
    188       1.3   uch 				    ON(r, SH3_MMUDA_D_V),
    189       1.3   uch 				    ON(r, SH3_MMUDA_D_D),
    190       1.3   uch 				    ON(r, SH3_MMUDA_D_C),
    191       1.3   uch 				    ON(r, SH3_MMUDA_D_SH),
    192       1.3   uch 				    (r & SH3_MMUDA_D_PR_MASK) >>
    193       1.3   uch 				    SH3_MMUDA_D_PR_SHIFT,
    194       1.3   uch 				    r & SH3_MMUDA_D_SZ ? 4 : 1));
    195       1.3   uch 			}
    196       1.3   uch 		}
    197       1.3   uch 
    198       1.3   uch 		break;
    199       1.3   uch 	case 4:
    200       1.3   uch 		r = _reg_read_4(SH4_MMUCR);
    201       1.3   uch 		if (!(r & SH4_MMUCR_AT))
    202       1.3   uch 			goto disabled;
    203       1.4   uch 		DPRINTF((TEXT("%s virtual storage mode,"),
    204       1.3   uch 		    r & SH3_MMUCR_SV ? TEXT("single") : TEXT("multiple")));
    205       1.3   uch 		DPRINTF((TEXT(" SQ access: (priviledge%S)"),
    206       1.3   uch 		    r & SH4_MMUCR_SQMD ? "" : "/user"));
    207       1.3   uch 		DPRINTF((TEXT("\n")));
    208       1.3   uch #if sample_code
    209       1.3   uch 		//
    210       1.4   uch 		// Memory mapped TLB accessing program must run on P2.
    211       1.3   uch 		// This is sample code.
    212       1.4   uch 		//
    213       1.3   uch 		// Dump ITLB
    214       1.3   uch 		DPRINTF((TEXT("---ITLB---\n")));
    215       1.3   uch 		for (i = 0; i < 4; i++) {
    216       1.3   uch 			e = i << SH4_ITLB_E_SHIFT;
    217       1.3   uch 			r = _reg_read_4(SH4_ITLB_AA | e);
    218       1.3   uch 			DPRINTF((TEXT("%08x %3d _%c"),
    219       1.3   uch 			    r & SH4_ITLB_AA_VPN_MASK,
    220       1.3   uch 			    r & SH4_ITLB_AA_ASID_MASK,
    221       1.3   uch 			    ON(r, SH4_ITLB_AA_V)));
    222       1.3   uch 			r = _reg_read_4(SH4_ITLB_DA1 | e);
    223       1.3   uch 			DPRINTF((TEXT(" %08x %c%c_%c_ %1d"),
    224       1.3   uch 			    r & SH4_ITLB_DA1_PPN_MASK,
    225       1.3   uch 			    ON(r, SH4_ITLB_DA1_V),
    226       1.3   uch 			    ON(r, SH4_ITLB_DA1_C),
    227       1.3   uch 			    ON(r, SH4_ITLB_DA1_SH),
    228       1.3   uch 			    (r & SH4_ITLB_DA1_PR) >> SH4_UTLB_DA1_PR_SHIFT
    229       1.3   uch 			    ));
    230       1.3   uch 			r = _reg_read_4(SH4_ITLB_DA2 | e);
    231       1.3   uch 			DPRINTF((TEXT(" %c%d\n"),
    232       1.4   uch 			    ON(r, SH4_ITLB_DA2_TC),
    233       1.3   uch 			    r & SH4_ITLB_DA2_SA_MASK));
    234       1.3   uch 		}
    235       1.3   uch 		// Dump UTLB
    236       1.3   uch 		DPRINTF((TEXT("---UTLB---\n")));
    237       1.3   uch 		for (i = 0; i < 64; i++) {
    238       1.3   uch 			e = i << SH4_UTLB_E_SHIFT;
    239       1.3   uch 			r = _reg_read_4(SH4_UTLB_AA | e);
    240       1.3   uch 			DPRINTF((TEXT("%08x %3d %c%c"),
    241       1.3   uch 			    r & SH4_UTLB_AA_VPN_MASK,
    242       1.3   uch 			    ON(r, SH4_UTLB_AA_D),
    243       1.3   uch 			    ON(r, SH4_UTLB_AA_V),
    244       1.3   uch 			    r & SH4_UTLB_AA_ASID_MASK));
    245       1.3   uch 			r = _reg_read_4(SH4_UTLB_DA1 | e);
    246       1.3   uch 			DPRINTF((TEXT(" %08x %c%c%c%c%c %1d"),
    247       1.3   uch 			    r & SH4_UTLB_DA1_PPN_MASK,
    248       1.3   uch 			    ON(r, SH4_UTLB_DA1_V),
    249       1.3   uch 			    ON(r, SH4_UTLB_DA1_C),
    250       1.3   uch 			    ON(r, SH4_UTLB_DA1_D),
    251       1.3   uch 			    ON(r, SH4_UTLB_DA1_SH),
    252       1.3   uch 			    ON(r, SH4_UTLB_DA1_WT),
    253       1.3   uch 			    (r & SH4_UTLB_DA1_PR_MASK) >> SH4_UTLB_DA1_PR_SHIFT
    254       1.3   uch 			    ));
    255       1.3   uch 			r = _reg_read_4(SH4_UTLB_DA2 | e);
    256       1.3   uch 			DPRINTF((TEXT(" %c%d\n"),
    257       1.3   uch 			    ON(r, SH4_UTLB_DA2_TC),
    258       1.3   uch 			    r & SH4_UTLB_DA2_SA_MASK));
    259       1.3   uch 		}
    260       1.3   uch #endif //sample_code
    261       1.3   uch 		break;
    262       1.3   uch 	}
    263       1.3   uch 
    264       1.3   uch 	SetKMode(kmode);
    265       1.3   uch 	return;
    266       1.3   uch 
    267       1.3   uch  disabled:
    268       1.3   uch 	DPRINTF((TEXT("disabled.\n")));
    269       1.3   uch 	SetKMode(kmode);
    270       1.3   uch #undef ON
    271       1.3   uch }
    272