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