1 /* $NetBSD: mmu.c,v 1.7 2005/12/11 12:19:08 christos Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
);
58 if (CPU_ISSUN4
|| CPU_ISSUN4C
) {
60 pmap_extract
= pmap_extract4
;
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
)
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.
75 } else if (CPU_ISSUN4M
|| CPU_ISSUN4D
) {
77 pmap_map
= pmap_map_srmmu
;
78 pmap_extract
= pmap_extract_srmmu
;
79 sprintf(buf
, "obmem %lx L!", (u_long
)&obmem
);
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
;
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
);
109 pte
= PG_S
| PG_V
| PG_W
| PG_NC
| ((pa
>> PGSHIFT
) & PG_PFNUM
);
113 if ((va
& (NBPSG
- 1)) == 0) {
114 if (boothowto
& AB_VERBOSE
)
115 printf("%d ", scookie
);
116 setsegmap(va
, ++scookie
);
119 if (boothowto
& AB_VERBOSE
)
124 int pmap_extract4(vaddr_t va
, paddr_t
*ppa
)
130 if ((pte
& PG_V
) == 0)
133 *ppa
= (pte
& PG_PFNUM
) << PGSHIFT
;
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
)
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
);
155 int pmap_extract_srmmu(vaddr_t va
, paddr_t
*ppa
)
161 sprintf(buf
, "%lx pgmap@ %lx L!", va
, (u_long
)&pte
);
163 if ((pte
& SRMMU_TETYPE
) != SRMMU_TEPTE
)
166 *ppa
= (pte
& SRMMU_PPNMASK
) << SRMMU_PPNPASHIFT
;