Home | History | Annotate | Line # | Download | only in sh3
sh_mmu.cpp revision 1.1.10.1
      1  1.1.10.1  nathanw /*	$NetBSD: sh_mmu.cpp,v 1.1.10.1 2002/02/28 04:09:46 nathanw Exp $	*/
      2       1.1      uch 
      3       1.1      uch /*-
      4  1.1.10.1  nathanw  * 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.1.10.1  nathanw #include <sh3/cpu/sh3.h>
     43  1.1.10.1  nathanw #include <sh3/cpu/sh4.h>
     44       1.1      uch 
     45  1.1.10.1  nathanw //
     46  1.1.10.1  nathanw // Get physical address from memory mapped TLB.
     47  1.1.10.1  nathanw // SH3 version. SH4 can't do this method. because address/data array must be
     48  1.1.10.1  nathanw // accessed from P2.
     49  1.1.10.1  nathanw //
     50       1.1      uch paddr_t
     51       1.1      uch MemoryManager_SHMMU::searchPage(vaddr_t vaddr)
     52       1.1      uch {
     53  1.1.10.1  nathanw 	u_int32_t vpn, idx, s, dum, aae, dae, entry_idx, asid;
     54       1.1      uch 	paddr_t paddr = ~0;
     55  1.1.10.1  nathanw 	int way, kmode;
     56       1.1      uch 
     57  1.1.10.1  nathanw 	vpn = vaddr & SH3_PAGE_MASK;
     58       1.1      uch 	// Windows CE uses VPN-only index-mode.
     59  1.1.10.1  nathanw 	idx = vaddr & SH3_MMU_VPN_MASK;
     60  1.1.10.1  nathanw 
     61  1.1.10.1  nathanw 	kmode = SetKMode(1);
     62  1.1.10.1  nathanw 	// Get current ASID
     63  1.1.10.1  nathanw 	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.1.10.1  nathanw 		dum = _reg_read_4(vaddr);
     71  1.1.10.1  nathanw 		_reg_write_4(vaddr, dum);
     72       1.1      uch 
     73  1.1.10.1  nathanw 		for (way = 0; way < SH3_MMU_WAY; way++) {
     74  1.1.10.1  nathanw 			entry_idx = idx | (way << SH3_MMU_WAY_SHIFT);
     75       1.1      uch 			// inquire MMU address array.
     76  1.1.10.1  nathanw 			aae = _reg_read_4(SH3_MMUAA | entry_idx);
     77       1.1      uch 
     78  1.1.10.1  nathanw 			if (!(aae & SH3_MMU_D_VALID) ||
     79  1.1.10.1  nathanw 			    ((aae & SH3_MMUAA_D_ASID_MASK) != asid) ||
     80  1.1.10.1  nathanw 			    (((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.1.10.1  nathanw 			dae = _reg_read_4(SH3_MMUDA | entry_idx);
     86  1.1.10.1  nathanw 			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.1.10.1  nathanw 	SetKMode(kmode);
     93       1.1      uch 
     94       1.1      uch 	return paddr;
     95       1.1      uch }
     96       1.1      uch 
     97  1.1.10.1  nathanw void
     98  1.1.10.1  nathanw MemoryManager_SHMMU::CacheDump()
     99  1.1.10.1  nathanw {
    100  1.1.10.1  nathanw 	static const char *able[] = {"dis", "en" };
    101  1.1.10.1  nathanw 	int write_through_p0_u0_p3;
    102  1.1.10.1  nathanw 	int write_through_p1;
    103  1.1.10.1  nathanw 	u_int32_t r;
    104  1.1.10.1  nathanw 	int kmode;
    105  1.1.10.1  nathanw 
    106  1.1.10.1  nathanw 	DPRINTF_SETUP();
    107  1.1.10.1  nathanw 
    108  1.1.10.1  nathanw 	kmode = SetKMode(1);
    109  1.1.10.1  nathanw 	switch (SHArchitecture::cpu_type()) {
    110  1.1.10.1  nathanw 	default:
    111  1.1.10.1  nathanw 		DPRINTF((TEXT("unknown architecture.\n")));
    112  1.1.10.1  nathanw 		SetKMode(kmode);
    113  1.1.10.1  nathanw 		return;
    114  1.1.10.1  nathanw 	case 3:
    115  1.1.10.1  nathanw 		r = _reg_read_4(SH3_CCR);
    116  1.1.10.1  nathanw 		DPRINTF((TEXT("cache %Sabled"),
    117  1.1.10.1  nathanw 		    able[(r & SH3_CCR_CE ? 1 : 0)]));
    118  1.1.10.1  nathanw 		if (r & SH3_CCR_RA)
    119  1.1.10.1  nathanw 			DPRINTF((TEXT(" ram-mode")));
    120  1.1.10.1  nathanw 
    121  1.1.10.1  nathanw 		write_through_p0_u0_p3 = r & SH3_CCR_WT;
    122  1.1.10.1  nathanw 		write_through_p1 = !(r & SH3_CCR_CB);
    123  1.1.10.1  nathanw 		break;
    124  1.1.10.1  nathanw 	case 4:
    125  1.1.10.1  nathanw 		r = _reg_read_4(SH4_CCR);
    126  1.1.10.1  nathanw 		DPRINTF((TEXT("I-cache %Sabled"),
    127  1.1.10.1  nathanw 		    able[(r & SH4_CCR_ICE) ? 1 : 0]));
    128  1.1.10.1  nathanw 		if (r & SH4_CCR_IIX)
    129  1.1.10.1  nathanw 			DPRINTF((TEXT(" index-mode ")));
    130  1.1.10.1  nathanw 		DPRINTF((TEXT(" D-cache %Sabled"),
    131  1.1.10.1  nathanw 		    able[(r & SH4_CCR_OCE) ? 1 : 0]));
    132  1.1.10.1  nathanw 		if (r & SH4_CCR_OIX)
    133  1.1.10.1  nathanw 			DPRINTF((TEXT(" index-mode")));
    134  1.1.10.1  nathanw 		if (r & SH4_CCR_ORA)
    135  1.1.10.1  nathanw 			DPRINTF((TEXT(" ram-mode")));
    136  1.1.10.1  nathanw 
    137  1.1.10.1  nathanw 		write_through_p0_u0_p3 = r & SH4_CCR_WT;
    138  1.1.10.1  nathanw 		write_through_p1 = !(r & SH4_CCR_CB);
    139  1.1.10.1  nathanw 		break;
    140  1.1.10.1  nathanw 	}
    141  1.1.10.1  nathanw 	DPRINTF((TEXT(".")));
    142  1.1.10.1  nathanw 
    143  1.1.10.1  nathanw 	// Write-through/back
    144  1.1.10.1  nathanw 	DPRINTF((TEXT(" P0, U0, P3 write-%S P1 write-%S\n"),
    145  1.1.10.1  nathanw 	    write_through_p0_u0_p3 ? "through" : "back",
    146  1.1.10.1  nathanw 	    write_through_p1 ? "through" : "back"));
    147  1.1.10.1  nathanw 
    148  1.1.10.1  nathanw 	SetKMode(kmode);
    149  1.1.10.1  nathanw }
    150  1.1.10.1  nathanw 
    151  1.1.10.1  nathanw void
    152  1.1.10.1  nathanw MemoryManager_SHMMU::MMUDump()
    153  1.1.10.1  nathanw {
    154  1.1.10.1  nathanw #define ON(x, c)	((x) & (c) ? '|' : '.')
    155  1.1.10.1  nathanw 	u_int32_t r, e, a;
    156  1.1.10.1  nathanw 	int i, kmode;
    157  1.1.10.1  nathanw 
    158  1.1.10.1  nathanw 	DPRINTF_SETUP();
    159  1.1.10.1  nathanw 
    160  1.1.10.1  nathanw 	kmode = SetKMode(1);
    161  1.1.10.1  nathanw 	DPRINTF((TEXT("MMU:\n")));
    162  1.1.10.1  nathanw 	switch (SHArchitecture::cpu_type()) {
    163  1.1.10.1  nathanw 	default:
    164  1.1.10.1  nathanw 		DPRINTF((TEXT("unknown architecture.\n")));
    165  1.1.10.1  nathanw 		SetKMode(kmode);
    166  1.1.10.1  nathanw 		return;
    167  1.1.10.1  nathanw 	case 3:
    168  1.1.10.1  nathanw 		r = _reg_read_4(SH3_MMUCR);
    169  1.1.10.1  nathanw 		if (!(r & SH3_MMUCR_AT))
    170  1.1.10.1  nathanw 			goto disabled;
    171  1.1.10.1  nathanw 
    172  1.1.10.1  nathanw 		// MMU configuration.
    173  1.1.10.1  nathanw 		DPRINTF((TEXT("%s index-mode, %s virtual storage mode\n"),
    174  1.1.10.1  nathanw 		    r & SH3_MMUCR_IX
    175  1.1.10.1  nathanw 		    ? TEXT("ASID + VPN") : TEXT("VPN only"),
    176  1.1.10.1  nathanw 		    r & SH3_MMUCR_SV ? TEXT("single") : TEXT("multiple")));
    177  1.1.10.1  nathanw 
    178  1.1.10.1  nathanw 		// Dump TLB.
    179  1.1.10.1  nathanw 		DPRINTF((TEXT("---TLB---\n")));
    180  1.1.10.1  nathanw 		DPRINTF((TEXT("   VPN    ASID    PFN     VDCG PR SZ\n")));
    181  1.1.10.1  nathanw 		for (i = 0; i < SH3_MMU_WAY; i++) {
    182  1.1.10.1  nathanw 			DPRINTF((TEXT(" [way %d]\n"), i));
    183  1.1.10.1  nathanw 			for (e = 0; e < SH3_MMU_ENTRY; e++) {
    184  1.1.10.1  nathanw 				// address/data array common offset.
    185  1.1.10.1  nathanw 				a = (e << SH3_MMU_VPN_SHIFT) |
    186  1.1.10.1  nathanw 				    (i << SH3_MMU_WAY_SHIFT);
    187  1.1.10.1  nathanw 
    188  1.1.10.1  nathanw 				r = _reg_read_4(SH3_MMUAA | a);
    189  1.1.10.1  nathanw 				DPRINTF((TEXT("0x%08x %3d"),
    190  1.1.10.1  nathanw 				    r & SH3_MMUAA_D_VPN_MASK,
    191  1.1.10.1  nathanw 				    r & SH3_MMUAA_D_ASID_MASK));
    192  1.1.10.1  nathanw 				r = _reg_read_4(SH3_MMUDA | a);
    193  1.1.10.1  nathanw 				DPRINTF((TEXT(" 0x%08x %c%c%c%c  %d %dK\n"),
    194  1.1.10.1  nathanw 				    r & SH3_MMUDA_D_PPN_MASK,
    195  1.1.10.1  nathanw 				    ON(r, SH3_MMUDA_D_V),
    196  1.1.10.1  nathanw 				    ON(r, SH3_MMUDA_D_D),
    197  1.1.10.1  nathanw 				    ON(r, SH3_MMUDA_D_C),
    198  1.1.10.1  nathanw 				    ON(r, SH3_MMUDA_D_SH),
    199  1.1.10.1  nathanw 				    (r & SH3_MMUDA_D_PR_MASK) >>
    200  1.1.10.1  nathanw 				    SH3_MMUDA_D_PR_SHIFT,
    201  1.1.10.1  nathanw 				    r & SH3_MMUDA_D_SZ ? 4 : 1));
    202  1.1.10.1  nathanw 			}
    203  1.1.10.1  nathanw 		}
    204  1.1.10.1  nathanw 
    205  1.1.10.1  nathanw 		break;
    206  1.1.10.1  nathanw 	case 4:
    207  1.1.10.1  nathanw 		r = _reg_read_4(SH4_MMUCR);
    208  1.1.10.1  nathanw 		if (!(r & SH4_MMUCR_AT))
    209  1.1.10.1  nathanw 			goto disabled;
    210  1.1.10.1  nathanw 		DPRINTF((TEXT("%s virtual storage mode,"),
    211  1.1.10.1  nathanw 		    r & SH3_MMUCR_SV ? TEXT("single") : TEXT("multiple")));
    212  1.1.10.1  nathanw 		DPRINTF((TEXT(" SQ access: (priviledge%S)"),
    213  1.1.10.1  nathanw 		    r & SH4_MMUCR_SQMD ? "" : "/user"));
    214  1.1.10.1  nathanw 		DPRINTF((TEXT("\n")));
    215  1.1.10.1  nathanw #if sample_code
    216  1.1.10.1  nathanw 		//
    217  1.1.10.1  nathanw 		// Memory mapped TLB accessing program must run on P2.
    218  1.1.10.1  nathanw 		// This is sample code.
    219  1.1.10.1  nathanw 		//
    220  1.1.10.1  nathanw 		// Dump ITLB
    221  1.1.10.1  nathanw 		DPRINTF((TEXT("---ITLB---\n")));
    222  1.1.10.1  nathanw 		for (i = 0; i < 4; i++) {
    223  1.1.10.1  nathanw 			e = i << SH4_ITLB_E_SHIFT;
    224  1.1.10.1  nathanw 			r = _reg_read_4(SH4_ITLB_AA | e);
    225  1.1.10.1  nathanw 			DPRINTF((TEXT("%08x %3d _%c"),
    226  1.1.10.1  nathanw 			    r & SH4_ITLB_AA_VPN_MASK,
    227  1.1.10.1  nathanw 			    r & SH4_ITLB_AA_ASID_MASK,
    228  1.1.10.1  nathanw 			    ON(r, SH4_ITLB_AA_V)));
    229  1.1.10.1  nathanw 			r = _reg_read_4(SH4_ITLB_DA1 | e);
    230  1.1.10.1  nathanw 			DPRINTF((TEXT(" %08x %c%c_%c_ %1d"),
    231  1.1.10.1  nathanw 			    r & SH4_ITLB_DA1_PPN_MASK,
    232  1.1.10.1  nathanw 			    ON(r, SH4_ITLB_DA1_V),
    233  1.1.10.1  nathanw 			    ON(r, SH4_ITLB_DA1_C),
    234  1.1.10.1  nathanw 			    ON(r, SH4_ITLB_DA1_SH),
    235  1.1.10.1  nathanw 			    (r & SH4_ITLB_DA1_PR) >> SH4_UTLB_DA1_PR_SHIFT
    236  1.1.10.1  nathanw 			    ));
    237  1.1.10.1  nathanw 			r = _reg_read_4(SH4_ITLB_DA2 | e);
    238  1.1.10.1  nathanw 			DPRINTF((TEXT(" %c%d\n"),
    239  1.1.10.1  nathanw 			    ON(r, SH4_ITLB_DA2_TC),
    240  1.1.10.1  nathanw 			    r & SH4_ITLB_DA2_SA_MASK));
    241  1.1.10.1  nathanw 		}
    242  1.1.10.1  nathanw 		// Dump UTLB
    243  1.1.10.1  nathanw 		DPRINTF((TEXT("---UTLB---\n")));
    244  1.1.10.1  nathanw 		for (i = 0; i < 64; i++) {
    245  1.1.10.1  nathanw 			e = i << SH4_UTLB_E_SHIFT;
    246  1.1.10.1  nathanw 			r = _reg_read_4(SH4_UTLB_AA | e);
    247  1.1.10.1  nathanw 			DPRINTF((TEXT("%08x %3d %c%c"),
    248  1.1.10.1  nathanw 			    r & SH4_UTLB_AA_VPN_MASK,
    249  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_AA_D),
    250  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_AA_V),
    251  1.1.10.1  nathanw 			    r & SH4_UTLB_AA_ASID_MASK));
    252  1.1.10.1  nathanw 			r = _reg_read_4(SH4_UTLB_DA1 | e);
    253  1.1.10.1  nathanw 			DPRINTF((TEXT(" %08x %c%c%c%c%c %1d"),
    254  1.1.10.1  nathanw 			    r & SH4_UTLB_DA1_PPN_MASK,
    255  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_DA1_V),
    256  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_DA1_C),
    257  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_DA1_D),
    258  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_DA1_SH),
    259  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_DA1_WT),
    260  1.1.10.1  nathanw 			    (r & SH4_UTLB_DA1_PR_MASK) >> SH4_UTLB_DA1_PR_SHIFT
    261  1.1.10.1  nathanw 			    ));
    262  1.1.10.1  nathanw 			r = _reg_read_4(SH4_UTLB_DA2 | e);
    263  1.1.10.1  nathanw 			DPRINTF((TEXT(" %c%d\n"),
    264  1.1.10.1  nathanw 			    ON(r, SH4_UTLB_DA2_TC),
    265  1.1.10.1  nathanw 			    r & SH4_UTLB_DA2_SA_MASK));
    266  1.1.10.1  nathanw 		}
    267  1.1.10.1  nathanw #endif //sample_code
    268  1.1.10.1  nathanw 		break;
    269  1.1.10.1  nathanw 	}
    270  1.1.10.1  nathanw 
    271  1.1.10.1  nathanw 	SetKMode(kmode);
    272  1.1.10.1  nathanw 	return;
    273  1.1.10.1  nathanw 
    274  1.1.10.1  nathanw  disabled:
    275  1.1.10.1  nathanw 	DPRINTF((TEXT("disabled.\n")));
    276  1.1.10.1  nathanw 	SetKMode(kmode);
    277  1.1.10.1  nathanw #undef ON
    278  1.1.10.1  nathanw }
    279