mmu.c revision 1.2 1 /* $NetBSD: mmu.c,v 1.2 2003/06/27 21:27:24 uwe 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->id_machine == 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) stba(va, ASI_SEGMAP, pmeg)
101
102 int pmap_map4(vaddr_t va, paddr_t pa, psize_t size)
103 {
104 u_int n = size >> PGSHIFT;
105 u_int pte;
106
107 if (sun4_mmu3l)
108 setregmap((va & -NBPRG), rcookie++);
109
110 if (boothowto & AB_VERBOSE)
111 printf("Mapping %lx -> %lx (%d pages)\n", va, pa, n);
112 setsegmap((va & -NBPSG), ++scookie);
113 while (n--) {
114 pte = PG_S | PG_V | PG_W | PG_NC | ((pa >> PGSHIFT) & PG_PFNUM);
115 setpte4(va, pte);
116 va += NBPG;
117 pa += NBPG;
118 if ((va & (NBPSG - 1)) == 0) {
119 if (boothowto & AB_VERBOSE)
120 printf("%d ", scookie);
121 setsegmap(va, ++scookie);
122 }
123 }
124 if (boothowto & AB_VERBOSE)
125 printf("\n");
126 return (0);
127 }
128
129 int pmap_extract4(vaddr_t va, paddr_t *ppa)
130 {
131 u_int pte;
132
133 va &= -NBPG;
134 pte = getpte4(va);
135 if ((pte & PG_V) == 0)
136 return (EFAULT);
137
138 *ppa = (pte & PG_PFNUM) << PGSHIFT;
139 return (0);
140 }
141
142
143 /*
144 * SRMMU VM routines.
145 * We use the PROM's Forth services to do all the hard work.
146 */
147 int pmap_map_srmmu(vaddr_t va, paddr_t pa, psize_t size)
148 {
149 char buf[64];
150 u_int n = size >> PGSHIFT;
151
152 if (boothowto & AB_VERBOSE)
153 printf("Mapping %lx -> %lx (%d pages)\n", va, pa, n);
154 while (n--) {
155 sprintf(buf, "%lx %x %lx map-page", pa, obmem, va);
156 prom_interpret(buf);
157 va += NBPG;
158 pa += NBPG;
159 if (boothowto & AB_VERBOSE && (va & (NBPSG - 1)) == 0)
160 printf("%d ", n);
161 }
162 if (boothowto & AB_VERBOSE)
163 printf("\n");
164 return (0);
165 }
166
167 int pmap_extract_srmmu(vaddr_t va, paddr_t *ppa)
168 {
169 char buf[32];
170 u_int pte;
171
172 va &= -NBPG;
173 sprintf(buf, "%lx pgmap@ %lx L!", va, (u_long)&pte);
174 prom_interpret(buf);
175 if ((pte & SRMMU_TETYPE) != SRMMU_TEPTE)
176 return (EFAULT);
177
178 *ppa = (pte & SRMMU_PPNMASK) << SRMMU_PPNPASHIFT;
179 return (0);
180 }
181