1 /* $NetBSD: mmu.c,v 1.7 2005/12/11 12:19:08 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/boot_flag.h> 41 #include <lib/libsa/stand.h> 42 #include <machine/promlib.h> 43 #include <machine/ctlreg.h> 44 #include <machine/pte.h> 45 #include <sparc/sparc/asm.h> 46 #include <sparc/stand/common/promdev.h> 47 48 static int sun4_mmu3l; /* sun4 3-lvl MMU */ 49 static int rcookie; /* sun4 3-lvl MMU region resource */ 50 static int scookie; /* sun4/sun4c MMU segment resource */ 51 static int obmem; /* SRMMU on-board memory space */ 52 53 static int pmap_map4(vaddr_t va, paddr_t pa, psize_t size); 54 static int pmap_extract4(vaddr_t va, paddr_t *ppa); 55 static int pmap_map_srmmu(vaddr_t va, paddr_t pa, psize_t size); 56 static int pmap_extract_srmmu(vaddr_t va, paddr_t *ppa); 57 58 int (*pmap_map)(vaddr_t va, paddr_t pa, psize_t size); 59 int (*pmap_extract)(vaddr_t va, paddr_t *ppa); 60 61 extern int boothowto; 62 63 int mmu_init(void) 64 { 65 if (CPU_ISSUN4 || CPU_ISSUN4C) { 66 pmap_map = pmap_map4; 67 pmap_extract = pmap_extract4; 68 if (CPU_ISSUN4) { 69 /* Find out if we're on a 3-lvl sun4 MMU. */ 70 struct idprom *idp = prom_getidprom(); 71 if (idp->idp_machtype == ID_SUN4_400) 72 sun4_mmu3l = 1; 73 } 74 /* 75 * XXX - we just guess which MMU cookies are free: 76 * region 0 maps [0-16MB] 77 * segment 0-16 map [0-4MB] 78 * the PROM mappings use high-valued cookies. 79 */ 80 rcookie = 2; 81 scookie = 17; 82 } else if (CPU_ISSUN4M || CPU_ISSUN4D) { 83 char buf[32]; 84 pmap_map = pmap_map_srmmu; 85 pmap_extract = pmap_extract_srmmu; 86 sprintf(buf, "obmem %lx L!", (u_long)&obmem); 87 prom_interpret(buf); 88 } else 89 return (ENOTSUP); 90 91 return (0); 92 } 93 94 95 /* 96 * SUN4 & SUN4C VM routines. 97 * Limited functionality wrt. MMU resource management. 98 */ 99 #define setregmap(va, smeg) stha((va)+2, ASI_REGMAP, (smeg << 8)) 100 #define setsegmap(va, pmeg) (CPU_ISSUN4C \ 101 ? stba(va, ASI_SEGMAP, pmeg) \ 102 : stha(va, ASI_SEGMAP, pmeg)) 103 104 int pmap_map4(vaddr_t va, paddr_t pa, psize_t size) 105 { 106 u_int n = (size + NBPG - 1) >> PGSHIFT; 107 u_int pte; 108 109 if (sun4_mmu3l) 110 setregmap((va & -NBPRG), rcookie++); 111 112 if (boothowto & AB_VERBOSE) 113 printf("Mapping %lx -> %lx (%d pages)\n", va, pa, n); 114 setsegmap((va & -NBPSG), ++scookie); 115 while (n--) { 116 pte = PG_S | PG_V | PG_W | PG_NC | ((pa >> PGSHIFT) & PG_PFNUM); 117 setpte4(va, pte); 118 va += NBPG; 119 pa += NBPG; 120 if ((va & (NBPSG - 1)) == 0) { 121 if (boothowto & AB_VERBOSE) 122 printf("%d ", scookie); 123 setsegmap(va, ++scookie); 124 } 125 } 126 if (boothowto & AB_VERBOSE) 127 printf("\n"); 128 return (0); 129 } 130 131 int pmap_extract4(vaddr_t va, paddr_t *ppa) 132 { 133 u_int pte; 134 135 va &= -NBPG; 136 pte = getpte4(va); 137 if ((pte & PG_V) == 0) 138 return (EFAULT); 139 140 *ppa = (pte & PG_PFNUM) << PGSHIFT; 141 return (0); 142 } 143 144 145 /* 146 * SRMMU VM routines. 147 * We use the PROM's Forth services to do all the hard work. 148 */ 149 int pmap_map_srmmu(vaddr_t va, paddr_t pa, psize_t size) 150 { 151 char buf[64]; 152 153 sprintf(buf, "%lx %x %lx %lx map-pages", pa, obmem, va, size); 154 155 if (boothowto & AB_VERBOSE) 156 printf("Mapping kernel: %s\n", buf); 157 158 prom_interpret(buf); 159 return (0); 160 } 161 162 int pmap_extract_srmmu(vaddr_t va, paddr_t *ppa) 163 { 164 char buf[32]; 165 u_int pte; 166 167 va &= -NBPG; 168 sprintf(buf, "%lx pgmap@ %lx L!", va, (u_long)&pte); 169 prom_interpret(buf); 170 if ((pte & SRMMU_TETYPE) != SRMMU_TEPTE) 171 return (EFAULT); 172 173 *ppa = (pte & SRMMU_PPNMASK) << SRMMU_PPNPASHIFT; 174 return (0); 175 } 176