Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / sparc / stand / common / mmu.c
blob7bece822656b177e1b984f8d9126da48f26a8577
1 /* $NetBSD: mmu.c,v 1.7 2005/12/11 12:19:08 christos Exp $ */
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/boot_flag.h>
34 #include <lib/libsa/stand.h>
35 #include <machine/promlib.h>
36 #include <machine/ctlreg.h>
37 #include <machine/pte.h>
38 #include <sparc/sparc/asm.h>
39 #include <sparc/stand/common/promdev.h>
41 static int sun4_mmu3l; /* sun4 3-lvl MMU */
42 static int rcookie; /* sun4 3-lvl MMU region resource */
43 static int scookie; /* sun4/sun4c MMU segment resource */
44 static int obmem; /* SRMMU on-board memory space */
46 static int pmap_map4(vaddr_t va, paddr_t pa, psize_t size);
47 static int pmap_extract4(vaddr_t va, paddr_t *ppa);
48 static int pmap_map_srmmu(vaddr_t va, paddr_t pa, psize_t size);
49 static int pmap_extract_srmmu(vaddr_t va, paddr_t *ppa);
51 int (*pmap_map)(vaddr_t va, paddr_t pa, psize_t size);
52 int (*pmap_extract)(vaddr_t va, paddr_t *ppa);
54 extern int boothowto;
56 int mmu_init(void)
58 if (CPU_ISSUN4 || CPU_ISSUN4C) {
59 pmap_map = pmap_map4;
60 pmap_extract = pmap_extract4;
61 if (CPU_ISSUN4) {
62 /* Find out if we're on a 3-lvl sun4 MMU. */
63 struct idprom *idp = prom_getidprom();
64 if (idp->idp_machtype == ID_SUN4_400)
65 sun4_mmu3l = 1;
68 * XXX - we just guess which MMU cookies are free:
69 * region 0 maps [0-16MB]
70 * segment 0-16 map [0-4MB]
71 * the PROM mappings use high-valued cookies.
73 rcookie = 2;
74 scookie = 17;
75 } else if (CPU_ISSUN4M || CPU_ISSUN4D) {
76 char buf[32];
77 pmap_map = pmap_map_srmmu;
78 pmap_extract = pmap_extract_srmmu;
79 sprintf(buf, "obmem %lx L!", (u_long)&obmem);
80 prom_interpret(buf);
81 } else
82 return (ENOTSUP);
84 return (0);
89 * SUN4 & SUN4C VM routines.
90 * Limited functionality wrt. MMU resource management.
92 #define setregmap(va, smeg) stha((va)+2, ASI_REGMAP, (smeg << 8))
93 #define setsegmap(va, pmeg) (CPU_ISSUN4C \
94 ? stba(va, ASI_SEGMAP, pmeg) \
95 : stha(va, ASI_SEGMAP, pmeg))
97 int pmap_map4(vaddr_t va, paddr_t pa, psize_t size)
99 u_int n = (size + NBPG - 1) >> PGSHIFT;
100 u_int pte;
102 if (sun4_mmu3l)
103 setregmap((va & -NBPRG), rcookie++);
105 if (boothowto & AB_VERBOSE)
106 printf("Mapping %lx -> %lx (%d pages)\n", va, pa, n);
107 setsegmap((va & -NBPSG), ++scookie);
108 while (n--) {
109 pte = PG_S | PG_V | PG_W | PG_NC | ((pa >> PGSHIFT) & PG_PFNUM);
110 setpte4(va, pte);
111 va += NBPG;
112 pa += NBPG;
113 if ((va & (NBPSG - 1)) == 0) {
114 if (boothowto & AB_VERBOSE)
115 printf("%d ", scookie);
116 setsegmap(va, ++scookie);
119 if (boothowto & AB_VERBOSE)
120 printf("\n");
121 return (0);
124 int pmap_extract4(vaddr_t va, paddr_t *ppa)
126 u_int pte;
128 va &= -NBPG;
129 pte = getpte4(va);
130 if ((pte & PG_V) == 0)
131 return (EFAULT);
133 *ppa = (pte & PG_PFNUM) << PGSHIFT;
134 return (0);
139 * SRMMU VM routines.
140 * We use the PROM's Forth services to do all the hard work.
142 int pmap_map_srmmu(vaddr_t va, paddr_t pa, psize_t size)
144 char buf[64];
146 sprintf(buf, "%lx %x %lx %lx map-pages", pa, obmem, va, size);
148 if (boothowto & AB_VERBOSE)
149 printf("Mapping kernel: %s\n", buf);
151 prom_interpret(buf);
152 return (0);
155 int pmap_extract_srmmu(vaddr_t va, paddr_t *ppa)
157 char buf[32];
158 u_int pte;
160 va &= -NBPG;
161 sprintf(buf, "%lx pgmap@ %lx L!", va, (u_long)&pte);
162 prom_interpret(buf);
163 if ((pte & SRMMU_TETYPE) != SRMMU_TEPTE)
164 return (EFAULT);
166 *ppa = (pte & SRMMU_PPNMASK) << SRMMU_PPNPASHIFT;
167 return (0);