xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision 8d8cca480cf154b46c59a691aed5abbaea4ef377)
1 /*-
2  * Copyright (c) 2001 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *        This product includes software developed by the NetBSD
19  *        Foundation, Inc. and its contributors.
20  * 4. Neither the name of The NetBSD Foundation nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 /*-
37  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
38  * Copyright (C) 1995, 1996 TooLs GmbH.
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *	This product includes software developed by TooLs GmbH.
52  * 4. The name of TooLs GmbH may not be used to endorse or promote products
53  *    derived from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
56  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
61  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
62  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
63  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
64  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
67  */
68 /*-
69  * Copyright (C) 2001 Benno Rice.
70  * All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  *
81  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
82  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
85  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
86  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
87  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
88  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
89  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
90  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91  */
92 
93 #include <sys/cdefs.h>
94 __FBSDID("$FreeBSD$");
95 
96 /*
97  * Manages physical address maps.
98  *
99  * In addition to hardware address maps, this module is called upon to
100  * provide software-use-only maps which may or may not be stored in the
101  * same form as hardware maps.  These pseudo-maps are used to store
102  * intermediate results from copy operations to and from address spaces.
103  *
104  * Since the information managed by this module is also stored by the
105  * logical address mapping module, this module may throw away valid virtual
106  * to physical mappings at almost any time.  However, invalidations of
107  * mappings must be done as requested.
108  *
109  * In order to cope with hardware architectures which make virtual to
110  * physical map invalidates expensive, this module may delay invalidate
111  * reduced protection operations until such time as they are actually
112  * necessary.  This module is given full information as to which processors
113  * are currently using which maps, and to when physical maps must be made
114  * correct.
115  */
116 
117 #include "opt_kstack_pages.h"
118 
119 #include <sys/param.h>
120 #include <sys/kernel.h>
121 #include <sys/ktr.h>
122 #include <sys/lock.h>
123 #include <sys/msgbuf.h>
124 #include <sys/mutex.h>
125 #include <sys/proc.h>
126 #include <sys/sysctl.h>
127 #include <sys/systm.h>
128 #include <sys/vmmeter.h>
129 
130 #include <dev/ofw/openfirm.h>
131 
132 #include <vm/vm.h>
133 #include <vm/vm_param.h>
134 #include <vm/vm_kern.h>
135 #include <vm/vm_page.h>
136 #include <vm/vm_map.h>
137 #include <vm/vm_object.h>
138 #include <vm/vm_extern.h>
139 #include <vm/vm_pageout.h>
140 #include <vm/vm_pager.h>
141 #include <vm/uma.h>
142 
143 #include <machine/cpu.h>
144 #include <machine/powerpc.h>
145 #include <machine/bat.h>
146 #include <machine/frame.h>
147 #include <machine/md_var.h>
148 #include <machine/psl.h>
149 #include <machine/pte.h>
150 #include <machine/smp.h>
151 #include <machine/sr.h>
152 #include <machine/mmuvar.h>
153 
154 #include "mmu_if.h"
155 
156 #define	MOEA_DEBUG
157 
158 #define TODO	panic("%s: not implemented", __func__);
159 
160 #define	TLBIE(va)	__asm __volatile("tlbie %0" :: "r"(va))
161 #define	TLBSYNC()	__asm __volatile("tlbsync");
162 #define	SYNC()		__asm __volatile("sync");
163 #define	EIEIO()		__asm __volatile("eieio");
164 
165 #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
166 #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
167 #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
168 
169 #define	PVO_PTEGIDX_MASK	0x007		/* which PTEG slot */
170 #define	PVO_PTEGIDX_VALID	0x008		/* slot is valid */
171 #define	PVO_WIRED		0x010		/* PVO entry is wired */
172 #define	PVO_MANAGED		0x020		/* PVO entry is managed */
173 #define	PVO_EXECUTABLE		0x040		/* PVO entry is executable */
174 #define	PVO_BOOTSTRAP		0x080		/* PVO entry allocated during
175 						   bootstrap */
176 #define PVO_FAKE		0x100		/* fictitious phys page */
177 #define	PVO_VADDR(pvo)		((pvo)->pvo_vaddr & ~ADDR_POFF)
178 #define	PVO_ISEXECUTABLE(pvo)	((pvo)->pvo_vaddr & PVO_EXECUTABLE)
179 #define PVO_ISFAKE(pvo)		((pvo)->pvo_vaddr & PVO_FAKE)
180 #define	PVO_PTEGIDX_GET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK)
181 #define	PVO_PTEGIDX_ISSET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID)
182 #define	PVO_PTEGIDX_CLR(pvo)	\
183 	((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK)))
184 #define	PVO_PTEGIDX_SET(pvo, i)	\
185 	((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID))
186 
187 #define	MOEA_PVO_CHECK(pvo)
188 
189 struct ofw_map {
190 	vm_offset_t	om_va;
191 	vm_size_t	om_len;
192 	vm_offset_t	om_pa;
193 	u_int		om_mode;
194 };
195 
196 /*
197  * Map of physical memory regions.
198  */
199 static struct	mem_region *regions;
200 static struct	mem_region *pregions;
201 u_int           phys_avail_count;
202 int		regions_sz, pregions_sz;
203 static struct	ofw_map *translations;
204 
205 extern struct pmap ofw_pmap;
206 
207 /*
208  * Lock for the pteg and pvo tables.
209  */
210 struct mtx	moea_table_mutex;
211 
212 /*
213  * PTEG data.
214  */
215 static struct	pteg *moea_pteg_table;
216 u_int		moea_pteg_count;
217 u_int		moea_pteg_mask;
218 
219 /*
220  * PVO data.
221  */
222 struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
223 struct	pvo_head moea_pvo_kunmanaged =
224     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
225 struct	pvo_head moea_pvo_unmanaged =
226     LIST_HEAD_INITIALIZER(moea_pvo_unmanaged);	/* list of unmanaged pages */
227 
228 uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
229 uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
230 
231 #define	BPVO_POOL_SIZE	32768
232 static struct	pvo_entry *moea_bpvo_pool;
233 static int	moea_bpvo_pool_index = 0;
234 
235 #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
236 static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
237 
238 static boolean_t moea_initialized = FALSE;
239 
240 /*
241  * Statistics.
242  */
243 u_int	moea_pte_valid = 0;
244 u_int	moea_pte_overflow = 0;
245 u_int	moea_pte_replacements = 0;
246 u_int	moea_pvo_entries = 0;
247 u_int	moea_pvo_enter_calls = 0;
248 u_int	moea_pvo_remove_calls = 0;
249 u_int	moea_pte_spills = 0;
250 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
251     0, "");
252 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
253     &moea_pte_overflow, 0, "");
254 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
255     &moea_pte_replacements, 0, "");
256 SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
257     0, "");
258 SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
259     &moea_pvo_enter_calls, 0, "");
260 SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
261     &moea_pvo_remove_calls, 0, "");
262 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
263     &moea_pte_spills, 0, "");
264 
265 /*
266  * Allocate physical memory for use in moea_bootstrap.
267  */
268 static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
269 
270 /*
271  * PTE calls.
272  */
273 static int		moea_pte_insert(u_int, struct pte *);
274 
275 /*
276  * PVO calls.
277  */
278 static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
279 		    vm_offset_t, vm_offset_t, u_int, int);
280 static void	moea_pvo_remove(struct pvo_entry *, int);
281 static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
282 static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
283 
284 /*
285  * Utility routines.
286  */
287 static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
288 			    vm_prot_t, boolean_t);
289 static void		moea_syncicache(vm_offset_t, vm_size_t);
290 static boolean_t	moea_query_bit(vm_page_t, int);
291 static u_int		moea_clear_bit(vm_page_t, int, int *);
292 static void		moea_kremove(mmu_t, vm_offset_t);
293 static void		tlbia(void);
294 int		moea_pte_spill(vm_offset_t);
295 
296 /*
297  * Kernel MMU interface
298  */
299 void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
300 void moea_clear_modify(mmu_t, vm_page_t);
301 void moea_clear_reference(mmu_t, vm_page_t);
302 void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
303 void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
304 void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
305     vm_prot_t);
306 void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
307 vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
308 vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
309 void moea_init(mmu_t);
310 boolean_t moea_is_modified(mmu_t, vm_page_t);
311 boolean_t moea_ts_referenced(mmu_t, vm_page_t);
312 vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int);
313 boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
314 int moea_page_wired_mappings(mmu_t, vm_page_t);
315 void moea_pinit(mmu_t, pmap_t);
316 void moea_pinit0(mmu_t, pmap_t);
317 void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
318 void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
319 void moea_qremove(mmu_t, vm_offset_t, int);
320 void moea_release(mmu_t, pmap_t);
321 void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
322 void moea_remove_all(mmu_t, vm_page_t);
323 void moea_remove_write(mmu_t, vm_page_t);
324 void moea_zero_page(mmu_t, vm_page_t);
325 void moea_zero_page_area(mmu_t, vm_page_t, int, int);
326 void moea_zero_page_idle(mmu_t, vm_page_t);
327 void moea_activate(mmu_t, struct thread *);
328 void moea_deactivate(mmu_t, struct thread *);
329 void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
330 void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t);
331 void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
332 vm_offset_t moea_kextract(mmu_t, vm_offset_t);
333 void moea_kenter(mmu_t, vm_offset_t, vm_offset_t);
334 boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
335 boolean_t moea_page_executable(mmu_t, vm_page_t);
336 
337 static mmu_method_t moea_methods[] = {
338 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
339 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
340 	MMUMETHOD(mmu_clear_reference,	moea_clear_reference),
341 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
342 	MMUMETHOD(mmu_enter,		moea_enter),
343 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
344 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
345 	MMUMETHOD(mmu_extract,		moea_extract),
346 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
347 	MMUMETHOD(mmu_init,		moea_init),
348 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
349 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
350 	MMUMETHOD(mmu_map,     		moea_map),
351 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
352 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
353 	MMUMETHOD(mmu_pinit,		moea_pinit),
354 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
355 	MMUMETHOD(mmu_protect,		moea_protect),
356 	MMUMETHOD(mmu_qenter,		moea_qenter),
357 	MMUMETHOD(mmu_qremove,		moea_qremove),
358 	MMUMETHOD(mmu_release,		moea_release),
359 	MMUMETHOD(mmu_remove,		moea_remove),
360 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
361 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
362 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
363 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
364 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
365 	MMUMETHOD(mmu_activate,		moea_activate),
366 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
367 
368 	/* Internal interfaces */
369 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
370 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
371 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
372 	MMUMETHOD(mmu_kextract,		moea_kextract),
373 	MMUMETHOD(mmu_kenter,		moea_kenter),
374 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
375 	MMUMETHOD(mmu_page_executable,	moea_page_executable),
376 
377 	{ 0, 0 }
378 };
379 
380 static mmu_def_t oea_mmu = {
381 	MMU_TYPE_OEA,
382 	moea_methods,
383 	0
384 };
385 MMU_DEF(oea_mmu);
386 
387 
388 static __inline int
389 va_to_sr(u_int *sr, vm_offset_t va)
390 {
391 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
392 }
393 
394 static __inline u_int
395 va_to_pteg(u_int sr, vm_offset_t addr)
396 {
397 	u_int hash;
398 
399 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
400 	    ADDR_PIDX_SHFT);
401 	return (hash & moea_pteg_mask);
402 }
403 
404 static __inline struct pvo_head *
405 pa_to_pvoh(vm_offset_t pa, vm_page_t *pg_p)
406 {
407 	struct	vm_page *pg;
408 
409 	pg = PHYS_TO_VM_PAGE(pa);
410 
411 	if (pg_p != NULL)
412 		*pg_p = pg;
413 
414 	if (pg == NULL)
415 		return (&moea_pvo_unmanaged);
416 
417 	return (&pg->md.mdpg_pvoh);
418 }
419 
420 static __inline struct pvo_head *
421 vm_page_to_pvoh(vm_page_t m)
422 {
423 
424 	return (&m->md.mdpg_pvoh);
425 }
426 
427 static __inline void
428 moea_attr_clear(vm_page_t m, int ptebit)
429 {
430 
431 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
432 	m->md.mdpg_attrs &= ~ptebit;
433 }
434 
435 static __inline int
436 moea_attr_fetch(vm_page_t m)
437 {
438 
439 	return (m->md.mdpg_attrs);
440 }
441 
442 static __inline void
443 moea_attr_save(vm_page_t m, int ptebit)
444 {
445 
446 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
447 	m->md.mdpg_attrs |= ptebit;
448 }
449 
450 static __inline int
451 moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
452 {
453 	if (pt->pte_hi == pvo_pt->pte_hi)
454 		return (1);
455 
456 	return (0);
457 }
458 
459 static __inline int
460 moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
461 {
462 	return (pt->pte_hi & ~PTE_VALID) ==
463 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
464 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
465 }
466 
467 static __inline void
468 moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
469 {
470 
471 	mtx_assert(&moea_table_mutex, MA_OWNED);
472 
473 	/*
474 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
475 	 * set when the real pte is set in memory.
476 	 *
477 	 * Note: Don't set the valid bit for correct operation of tlb update.
478 	 */
479 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
480 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
481 	pt->pte_lo = pte_lo;
482 }
483 
484 static __inline void
485 moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
486 {
487 
488 	mtx_assert(&moea_table_mutex, MA_OWNED);
489 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
490 }
491 
492 static __inline void
493 moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
494 {
495 
496 	mtx_assert(&moea_table_mutex, MA_OWNED);
497 
498 	/*
499 	 * As shown in Section 7.6.3.2.3
500 	 */
501 	pt->pte_lo &= ~ptebit;
502 	TLBIE(va);
503 	EIEIO();
504 	TLBSYNC();
505 	SYNC();
506 }
507 
508 static __inline void
509 moea_pte_set(struct pte *pt, struct pte *pvo_pt)
510 {
511 
512 	mtx_assert(&moea_table_mutex, MA_OWNED);
513 	pvo_pt->pte_hi |= PTE_VALID;
514 
515 	/*
516 	 * Update the PTE as defined in section 7.6.3.1.
517 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
518 	 * been saved so this routine can restore them (if desired).
519 	 */
520 	pt->pte_lo = pvo_pt->pte_lo;
521 	EIEIO();
522 	pt->pte_hi = pvo_pt->pte_hi;
523 	SYNC();
524 	moea_pte_valid++;
525 }
526 
527 static __inline void
528 moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
529 {
530 
531 	mtx_assert(&moea_table_mutex, MA_OWNED);
532 	pvo_pt->pte_hi &= ~PTE_VALID;
533 
534 	/*
535 	 * Force the reg & chg bits back into the PTEs.
536 	 */
537 	SYNC();
538 
539 	/*
540 	 * Invalidate the pte.
541 	 */
542 	pt->pte_hi &= ~PTE_VALID;
543 
544 	SYNC();
545 	TLBIE(va);
546 	EIEIO();
547 	TLBSYNC();
548 	SYNC();
549 
550 	/*
551 	 * Save the reg & chg bits.
552 	 */
553 	moea_pte_synch(pt, pvo_pt);
554 	moea_pte_valid--;
555 }
556 
557 static __inline void
558 moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
559 {
560 
561 	/*
562 	 * Invalidate the PTE
563 	 */
564 	moea_pte_unset(pt, pvo_pt, va);
565 	moea_pte_set(pt, pvo_pt);
566 }
567 
568 /*
569  * Quick sort callout for comparing memory regions.
570  */
571 static int	mr_cmp(const void *a, const void *b);
572 static int	om_cmp(const void *a, const void *b);
573 
574 static int
575 mr_cmp(const void *a, const void *b)
576 {
577 	const struct	mem_region *regiona;
578 	const struct	mem_region *regionb;
579 
580 	regiona = a;
581 	regionb = b;
582 	if (regiona->mr_start < regionb->mr_start)
583 		return (-1);
584 	else if (regiona->mr_start > regionb->mr_start)
585 		return (1);
586 	else
587 		return (0);
588 }
589 
590 static int
591 om_cmp(const void *a, const void *b)
592 {
593 	const struct	ofw_map *mapa;
594 	const struct	ofw_map *mapb;
595 
596 	mapa = a;
597 	mapb = b;
598 	if (mapa->om_pa < mapb->om_pa)
599 		return (-1);
600 	else if (mapa->om_pa > mapb->om_pa)
601 		return (1);
602 	else
603 		return (0);
604 }
605 
606 void
607 pmap_cpu_bootstrap(volatile uint32_t *trcp, int ap)
608 {
609 	u_int sdr;
610 	int i;
611 
612 	trcp[0] = 0x1000;
613 	trcp[1] = (uint32_t)&pmap_cpu_bootstrap;
614 
615 	if (ap) {
616 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
617 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
618 		isync();
619 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
620 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
621 		isync();
622 	}
623 
624 	trcp[0] = 0x1001;
625 
626 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
627 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
628 	isync();
629 
630 	trcp[0] = 0x1002;
631 
632 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
633 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
634 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
635 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
636 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
637 	isync();
638 
639 	trcp[0] = 0x1003;
640 
641 	for (i = 0; i < 16; i++)
642 		mtsrin(i << ADDR_SR_SHFT, EMPTY_SEGMENT);
643 
644 	trcp[0] = 0x1004;
645 
646 	__asm __volatile("mtsr %0,%1" :: "n"(KERNEL_SR), "r"(KERNEL_SEGMENT));
647 	__asm __volatile("mtsr %0,%1" :: "n"(KERNEL2_SR), "r"(KERNEL2_SEGMENT));
648 	__asm __volatile("sync");
649 
650 	trcp[0] = 0x1005;
651 
652 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
653 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
654 	isync();
655 
656 	trcp[0] = 0x1006;
657 	trcp[1] = sdr;
658 
659 	tlbia();
660 
661 	trcp[0] = 0x1007;
662 }
663 
664 void
665 moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
666 {
667 	ihandle_t	mmui;
668 	phandle_t	chosen, mmu;
669 	int		sz;
670 	int		i, j;
671 	int		ofw_mappings;
672 	uint32_t	trace[2];
673 	vm_size_t	size, physsz, hwphyssz;
674 	vm_offset_t	pa, va, off;
675 
676         /*
677          * Set up BAT0 to map the lowest 256 MB area
678          */
679         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
680         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
681 
682         /*
683          * Map PCI memory space.
684          */
685         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
686         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
687 
688         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
689         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
690 
691         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
692         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
693 
694         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
695         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
696 
697         /*
698          * Map obio devices.
699          */
700         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
701         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
702 
703 	/*
704 	 * Use an IBAT and a DBAT to map the bottom segment of memory
705 	 * where we are.
706 	 */
707 	__asm (".balign 32; \n"
708 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
709 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
710 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
711 
712 	/* map pci space */
713 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
714 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
715 	isync();
716 
717 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
718 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
719 
720 	qsort(pregions, pregions_sz, sizeof(*pregions), mr_cmp);
721 	for (i = 0; i < pregions_sz; i++) {
722 		vm_offset_t pa;
723 		vm_offset_t end;
724 
725 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
726 			pregions[i].mr_start,
727 			pregions[i].mr_start + pregions[i].mr_size,
728 			pregions[i].mr_size);
729 		/*
730 		 * Install entries into the BAT table to allow all
731 		 * of physmem to be convered by on-demand BAT entries.
732 		 * The loop will sometimes set the same battable element
733 		 * twice, but that's fine since they won't be used for
734 		 * a while yet.
735 		 */
736 		pa = pregions[i].mr_start & 0xf0000000;
737 		end = pregions[i].mr_start + pregions[i].mr_size;
738 		do {
739                         u_int n = pa >> ADDR_SR_SHFT;
740 
741 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
742 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
743 			pa += SEGMENT_LENGTH;
744 		} while (pa < end);
745 	}
746 
747 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
748 		panic("moea_bootstrap: phys_avail too small");
749 	qsort(regions, regions_sz, sizeof(*regions), mr_cmp);
750 	phys_avail_count = 0;
751 	physsz = 0;
752 	hwphyssz = 0;
753 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
754 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
755 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
756 		    regions[i].mr_start + regions[i].mr_size,
757 		    regions[i].mr_size);
758 		if (hwphyssz != 0 &&
759 		    (physsz + regions[i].mr_size) >= hwphyssz) {
760 			if (physsz < hwphyssz) {
761 				phys_avail[j] = regions[i].mr_start;
762 				phys_avail[j + 1] = regions[i].mr_start +
763 				    hwphyssz - physsz;
764 				physsz = hwphyssz;
765 				phys_avail_count++;
766 			}
767 			break;
768 		}
769 		phys_avail[j] = regions[i].mr_start;
770 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
771 		phys_avail_count++;
772 		physsz += regions[i].mr_size;
773 	}
774 	physmem = btoc(physsz);
775 
776 	/*
777 	 * Allocate PTEG table.
778 	 */
779 #ifdef PTEGCOUNT
780 	moea_pteg_count = PTEGCOUNT;
781 #else
782 	moea_pteg_count = 0x1000;
783 
784 	while (moea_pteg_count < physmem)
785 		moea_pteg_count <<= 1;
786 
787 	moea_pteg_count >>= 1;
788 #endif /* PTEGCOUNT */
789 
790 	size = moea_pteg_count * sizeof(struct pteg);
791 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
792 	    size);
793 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
794 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
795 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
796 	moea_pteg_mask = moea_pteg_count - 1;
797 
798 	/*
799 	 * Allocate pv/overflow lists.
800 	 */
801 	size = sizeof(struct pvo_head) * moea_pteg_count;
802 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
803 	    PAGE_SIZE);
804 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
805 	for (i = 0; i < moea_pteg_count; i++)
806 		LIST_INIT(&moea_pvo_table[i]);
807 
808 	/*
809 	 * Initialize the lock that synchronizes access to the pteg and pvo
810 	 * tables.
811 	 */
812 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
813 	    MTX_RECURSE);
814 
815 	/*
816 	 * Initialise the unmanaged pvo pool.
817 	 */
818 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
819 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
820 	moea_bpvo_pool_index = 0;
821 
822 	/*
823 	 * Make sure kernel vsid is allocated as well as VSID 0.
824 	 */
825 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
826 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
827 	moea_vsid_bitmap[0] |= 1;
828 
829 	/*
830 	 * Set up the Open Firmware pmap and add it's mappings.
831 	 */
832 	moea_pinit(mmup, &ofw_pmap);
833 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
834 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
835 	if ((chosen = OF_finddevice("/chosen")) == -1)
836 		panic("moea_bootstrap: can't find /chosen");
837 	OF_getprop(chosen, "mmu", &mmui, 4);
838 	if ((mmu = OF_instance_to_package(mmui)) == -1)
839 		panic("moea_bootstrap: can't get mmu package");
840 	if ((sz = OF_getproplen(mmu, "translations")) == -1)
841 		panic("moea_bootstrap: can't get ofw translation count");
842 	translations = NULL;
843 	for (i = 0; phys_avail[i] != 0; i += 2) {
844 		if (phys_avail[i + 1] >= sz) {
845 			translations = (struct ofw_map *)phys_avail[i];
846 			break;
847 		}
848 	}
849 	if (translations == NULL)
850 		panic("moea_bootstrap: no space to copy translations");
851 	bzero(translations, sz);
852 	if (OF_getprop(mmu, "translations", translations, sz) == -1)
853 		panic("moea_bootstrap: can't get ofw translations");
854 	CTR0(KTR_PMAP, "moea_bootstrap: translations");
855 	sz /= sizeof(*translations);
856 	qsort(translations, sz, sizeof (*translations), om_cmp);
857 	for (i = 0, ofw_mappings = 0; i < sz; i++) {
858 		CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
859 		    translations[i].om_pa, translations[i].om_va,
860 		    translations[i].om_len);
861 
862 		/*
863 		 * If the mapping is 1:1, let the RAM and device on-demand
864 		 * BAT tables take care of the translation.
865 		 */
866 		if (translations[i].om_va == translations[i].om_pa)
867 			continue;
868 
869 		/* Enter the pages */
870 		for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) {
871 			struct	vm_page m;
872 
873 			m.phys_addr = translations[i].om_pa + off;
874 			PMAP_LOCK(&ofw_pmap);
875 			moea_enter_locked(&ofw_pmap,
876 				   translations[i].om_va + off, &m,
877 				   VM_PROT_ALL, 1);
878 			PMAP_UNLOCK(&ofw_pmap);
879 			ofw_mappings++;
880 		}
881 	}
882 
883 	/*
884 	 * Calculate the last available physical address.
885 	 */
886 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
887 		;
888 	Maxmem = powerpc_btop(phys_avail[i + 1]);
889 
890 	/*
891 	 * Initialize the kernel pmap (which is statically allocated).
892 	 */
893 	PMAP_LOCK_INIT(kernel_pmap);
894 	for (i = 0; i < 16; i++) {
895 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT;
896 	}
897 	kernel_pmap->pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
898 	kernel_pmap->pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
899 	kernel_pmap->pm_active = ~0;
900 
901 	pmap_cpu_bootstrap(trace, 0);
902 
903 	pmap_bootstrapped++;
904 
905 	/*
906 	 * Set the start and end of kva.
907 	 */
908 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
909 	virtual_end = VM_MAX_KERNEL_ADDRESS;
910 
911 	/*
912 	 * Allocate a kernel stack with a guard page for thread0 and map it
913 	 * into the kernel page map.
914 	 */
915 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
916 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
917 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
918 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
919 	thread0.td_kstack = va;
920 	thread0.td_kstack_pages = KSTACK_PAGES;
921 	for (i = 0; i < KSTACK_PAGES; i++) {
922 		moea_kenter(mmup, va, pa);;
923 		pa += PAGE_SIZE;
924 		va += PAGE_SIZE;
925 	}
926 
927 	/*
928 	 * Allocate virtual address space for the message buffer.
929 	 */
930 	pa = msgbuf_phys = moea_bootstrap_alloc(MSGBUF_SIZE, PAGE_SIZE);
931 	msgbufp = (struct msgbuf *)virtual_avail;
932 	va = virtual_avail;
933 	virtual_avail += round_page(MSGBUF_SIZE);
934 	while (va < virtual_avail) {
935 		moea_kenter(mmup, va, pa);;
936 		pa += PAGE_SIZE;
937 		va += PAGE_SIZE;
938 	}
939 }
940 
941 /*
942  * Activate a user pmap.  The pmap must be activated before it's address
943  * space can be accessed in any way.
944  */
945 void
946 moea_activate(mmu_t mmu, struct thread *td)
947 {
948 	pmap_t	pm, pmr;
949 
950 	/*
951 	 * Load all the data we need up front to encourage the compiler to
952 	 * not issue any loads while we have interrupts disabled below.
953 	 */
954 	pm = &td->td_proc->p_vmspace->vm_pmap;
955 
956 	if ((pmr = (pmap_t)moea_kextract(mmu, (vm_offset_t)pm)) == NULL)
957 		pmr = pm;
958 
959 	pm->pm_active |= PCPU_GET(cpumask);
960 	PCPU_SET(curpmap, pmr);
961 }
962 
963 void
964 moea_deactivate(mmu_t mmu, struct thread *td)
965 {
966 	pmap_t	pm;
967 
968 	pm = &td->td_proc->p_vmspace->vm_pmap;
969 	pm->pm_active &= ~(PCPU_GET(cpumask));
970 	PCPU_SET(curpmap, NULL);
971 }
972 
973 void
974 moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
975 {
976 	struct	pvo_entry *pvo;
977 
978 	PMAP_LOCK(pm);
979 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
980 
981 	if (pvo != NULL) {
982 		if (wired) {
983 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
984 				pm->pm_stats.wired_count++;
985 			pvo->pvo_vaddr |= PVO_WIRED;
986 		} else {
987 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
988 				pm->pm_stats.wired_count--;
989 			pvo->pvo_vaddr &= ~PVO_WIRED;
990 		}
991 	}
992 	PMAP_UNLOCK(pm);
993 }
994 
995 void
996 moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
997 {
998 	vm_offset_t	dst;
999 	vm_offset_t	src;
1000 
1001 	dst = VM_PAGE_TO_PHYS(mdst);
1002 	src = VM_PAGE_TO_PHYS(msrc);
1003 
1004 	kcopy((void *)src, (void *)dst, PAGE_SIZE);
1005 }
1006 
1007 /*
1008  * Zero a page of physical memory by temporarily mapping it into the tlb.
1009  */
1010 void
1011 moea_zero_page(mmu_t mmu, vm_page_t m)
1012 {
1013 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
1014 	void *va = (void *)pa;
1015 
1016 	bzero(va, PAGE_SIZE);
1017 }
1018 
1019 void
1020 moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
1021 {
1022 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
1023 	void *va = (void *)(pa + off);
1024 
1025 	bzero(va, size);
1026 }
1027 
1028 void
1029 moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1030 {
1031 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
1032 	void *va = (void *)pa;
1033 
1034 	bzero(va, PAGE_SIZE);
1035 }
1036 
1037 /*
1038  * Map the given physical page at the specified virtual address in the
1039  * target pmap with the protection requested.  If specified the page
1040  * will be wired down.
1041  */
1042 void
1043 moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1044 	   boolean_t wired)
1045 {
1046 
1047 	vm_page_lock_queues();
1048 	PMAP_LOCK(pmap);
1049 	moea_enter_locked(pmap, va, m, prot, wired);
1050 	vm_page_unlock_queues();
1051 	PMAP_UNLOCK(pmap);
1052 }
1053 
1054 /*
1055  * Map the given physical page at the specified virtual address in the
1056  * target pmap with the protection requested.  If specified the page
1057  * will be wired down.
1058  *
1059  * The page queues and pmap must be locked.
1060  */
1061 static void
1062 moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1063     boolean_t wired)
1064 {
1065 	struct		pvo_head *pvo_head;
1066 	uma_zone_t	zone;
1067 	vm_page_t	pg;
1068 	u_int		pte_lo, pvo_flags, was_exec, i;
1069 	int		error;
1070 
1071 	if (!moea_initialized) {
1072 		pvo_head = &moea_pvo_kunmanaged;
1073 		zone = moea_upvo_zone;
1074 		pvo_flags = 0;
1075 		pg = NULL;
1076 		was_exec = PTE_EXEC;
1077 	} else {
1078 		pvo_head = vm_page_to_pvoh(m);
1079 		pg = m;
1080 		zone = moea_mpvo_zone;
1081 		pvo_flags = PVO_MANAGED;
1082 		was_exec = 0;
1083 	}
1084 	if (pmap_bootstrapped)
1085 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1086 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1087 
1088 	/* XXX change the pvo head for fake pages */
1089 	if ((m->flags & PG_FICTITIOUS) == PG_FICTITIOUS)
1090 		pvo_head = &moea_pvo_kunmanaged;
1091 
1092 	/*
1093 	 * If this is a managed page, and it's the first reference to the page,
1094 	 * clear the execness of the page.  Otherwise fetch the execness.
1095 	 */
1096 	if ((pg != NULL) && ((m->flags & PG_FICTITIOUS) == 0)) {
1097 		if (LIST_EMPTY(pvo_head)) {
1098 			moea_attr_clear(pg, PTE_EXEC);
1099 		} else {
1100 			was_exec = moea_attr_fetch(pg) & PTE_EXEC;
1101 		}
1102 	}
1103 
1104 	/*
1105 	 * Assume the page is cache inhibited and access is guarded unless
1106 	 * it's in our available memory array.
1107 	 */
1108 	pte_lo = PTE_I | PTE_G;
1109 	for (i = 0; i < pregions_sz; i++) {
1110 		if ((VM_PAGE_TO_PHYS(m) >= pregions[i].mr_start) &&
1111 		    (VM_PAGE_TO_PHYS(m) <
1112 			(pregions[i].mr_start + pregions[i].mr_size))) {
1113 			pte_lo &= ~(PTE_I | PTE_G);
1114 			break;
1115 		}
1116 	}
1117 
1118 	if (prot & VM_PROT_WRITE) {
1119 		pte_lo |= PTE_BW;
1120 		if (pmap_bootstrapped)
1121 			vm_page_flag_set(m, PG_WRITEABLE);
1122 	} else
1123 		pte_lo |= PTE_BR;
1124 
1125 	if (prot & VM_PROT_EXECUTE)
1126 		pvo_flags |= PVO_EXECUTABLE;
1127 
1128 	if (wired)
1129 		pvo_flags |= PVO_WIRED;
1130 
1131 	if ((m->flags & PG_FICTITIOUS) != 0)
1132 		pvo_flags |= PVO_FAKE;
1133 
1134 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
1135 	    pte_lo, pvo_flags);
1136 
1137 	/*
1138 	 * Flush the real page from the instruction cache if this page is
1139 	 * mapped executable and cacheable and was not previously mapped (or
1140 	 * was not mapped executable).
1141 	 */
1142 	if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
1143 	    (pte_lo & PTE_I) == 0 && was_exec == 0) {
1144 		/*
1145 		 * Flush the real memory from the cache.
1146 		 */
1147 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1148 		if (pg != NULL)
1149 			moea_attr_save(pg, PTE_EXEC);
1150 	}
1151 
1152 	/* XXX syncicache always until problems are sorted */
1153 	moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1154 }
1155 
1156 /*
1157  * Maps a sequence of resident pages belonging to the same object.
1158  * The sequence begins with the given page m_start.  This page is
1159  * mapped at the given virtual address start.  Each subsequent page is
1160  * mapped at a virtual address that is offset from start by the same
1161  * amount as the page is offset from m_start within the object.  The
1162  * last page in the sequence is the page with the largest offset from
1163  * m_start that can be mapped at a virtual address less than the given
1164  * virtual address end.  Not every virtual page between start and end
1165  * is mapped; only those for which a resident page exists with the
1166  * corresponding offset from m_start are mapped.
1167  */
1168 void
1169 moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1170     vm_page_t m_start, vm_prot_t prot)
1171 {
1172 	vm_page_t m;
1173 	vm_pindex_t diff, psize;
1174 
1175 	psize = atop(end - start);
1176 	m = m_start;
1177 	PMAP_LOCK(pm);
1178 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1179 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1180 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1181 		m = TAILQ_NEXT(m, listq);
1182 	}
1183 	PMAP_UNLOCK(pm);
1184 }
1185 
1186 void
1187 moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
1188     vm_prot_t prot)
1189 {
1190 
1191 	PMAP_LOCK(pm);
1192 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1193 	    FALSE);
1194 	PMAP_UNLOCK(pm);
1195 
1196 }
1197 
1198 vm_paddr_t
1199 moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
1200 {
1201 	struct	pvo_entry *pvo;
1202 	vm_paddr_t pa;
1203 
1204 	PMAP_LOCK(pm);
1205 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
1206 	if (pvo == NULL)
1207 		pa = 0;
1208 	else
1209 		pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
1210 	PMAP_UNLOCK(pm);
1211 	return (pa);
1212 }
1213 
1214 /*
1215  * Atomically extract and hold the physical page with the given
1216  * pmap and virtual address pair if that mapping permits the given
1217  * protection.
1218  */
1219 vm_page_t
1220 moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1221 {
1222 	struct	pvo_entry *pvo;
1223 	vm_page_t m;
1224 
1225 	m = NULL;
1226 	vm_page_lock_queues();
1227 	PMAP_LOCK(pmap);
1228 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1229 	if (pvo != NULL && (pvo->pvo_pte.pte_hi & PTE_VALID) &&
1230 	    ((pvo->pvo_pte.pte_lo & PTE_PP) == PTE_RW ||
1231 	     (prot & VM_PROT_WRITE) == 0)) {
1232 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
1233 		vm_page_hold(m);
1234 	}
1235 	vm_page_unlock_queues();
1236 	PMAP_UNLOCK(pmap);
1237 	return (m);
1238 }
1239 
1240 void
1241 moea_init(mmu_t mmu)
1242 {
1243 
1244 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
1245 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1246 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
1247 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
1248 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1249 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
1250 	moea_initialized = TRUE;
1251 }
1252 
1253 boolean_t
1254 moea_is_modified(mmu_t mmu, vm_page_t m)
1255 {
1256 
1257 	if ((m->flags & (PG_FICTITIOUS |PG_UNMANAGED)) != 0)
1258 		return (FALSE);
1259 
1260 	return (moea_query_bit(m, PTE_CHG));
1261 }
1262 
1263 void
1264 moea_clear_reference(mmu_t mmu, vm_page_t m)
1265 {
1266 
1267 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1268 		return;
1269 	moea_clear_bit(m, PTE_REF, NULL);
1270 }
1271 
1272 void
1273 moea_clear_modify(mmu_t mmu, vm_page_t m)
1274 {
1275 
1276 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1277 		return;
1278 	moea_clear_bit(m, PTE_CHG, NULL);
1279 }
1280 
1281 /*
1282  * Clear the write and modified bits in each of the given page's mappings.
1283  */
1284 void
1285 moea_remove_write(mmu_t mmu, vm_page_t m)
1286 {
1287 	struct	pvo_entry *pvo;
1288 	struct	pte *pt;
1289 	pmap_t	pmap;
1290 	u_int	lo;
1291 
1292 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1293 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
1294 	    (m->flags & PG_WRITEABLE) == 0)
1295 		return;
1296 	lo = moea_attr_fetch(m);
1297 	SYNC();
1298 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1299 		pmap = pvo->pvo_pmap;
1300 		PMAP_LOCK(pmap);
1301 		if ((pvo->pvo_pte.pte_lo & PTE_PP) != PTE_BR) {
1302 			pt = moea_pvo_to_pte(pvo, -1);
1303 			pvo->pvo_pte.pte_lo &= ~PTE_PP;
1304 			pvo->pvo_pte.pte_lo |= PTE_BR;
1305 			if (pt != NULL) {
1306 				moea_pte_synch(pt, &pvo->pvo_pte);
1307 				lo |= pvo->pvo_pte.pte_lo;
1308 				pvo->pvo_pte.pte_lo &= ~PTE_CHG;
1309 				moea_pte_change(pt, &pvo->pvo_pte,
1310 				    pvo->pvo_vaddr);
1311 				mtx_unlock(&moea_table_mutex);
1312 			}
1313 		}
1314 		PMAP_UNLOCK(pmap);
1315 	}
1316 	if ((lo & PTE_CHG) != 0) {
1317 		moea_attr_clear(m, PTE_CHG);
1318 		vm_page_dirty(m);
1319 	}
1320 	vm_page_flag_clear(m, PG_WRITEABLE);
1321 }
1322 
1323 /*
1324  *	moea_ts_referenced:
1325  *
1326  *	Return a count of reference bits for a page, clearing those bits.
1327  *	It is not necessary for every reference bit to be cleared, but it
1328  *	is necessary that 0 only be returned when there are truly no
1329  *	reference bits set.
1330  *
1331  *	XXX: The exact number of bits to check and clear is a matter that
1332  *	should be tested and standardized at some point in the future for
1333  *	optimal aging of shared pages.
1334  */
1335 boolean_t
1336 moea_ts_referenced(mmu_t mmu, vm_page_t m)
1337 {
1338 	int count;
1339 
1340 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1341 		return (0);
1342 
1343 	count = moea_clear_bit(m, PTE_REF, NULL);
1344 
1345 	return (count);
1346 }
1347 
1348 /*
1349  * Map a wired page into kernel virtual address space.
1350  */
1351 void
1352 moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
1353 {
1354 	u_int		pte_lo;
1355 	int		error;
1356 	int		i;
1357 
1358 #if 0
1359 	if (va < VM_MIN_KERNEL_ADDRESS)
1360 		panic("moea_kenter: attempt to enter non-kernel address %#x",
1361 		    va);
1362 #endif
1363 
1364 	pte_lo = PTE_I | PTE_G;
1365 	for (i = 0; i < pregions_sz; i++) {
1366 		if ((pa >= pregions[i].mr_start) &&
1367 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
1368 			pte_lo &= ~(PTE_I | PTE_G);
1369 			break;
1370 		}
1371 	}
1372 
1373 	PMAP_LOCK(kernel_pmap);
1374 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
1375 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
1376 
1377 	if (error != 0 && error != ENOENT)
1378 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
1379 		    pa, error);
1380 
1381 	/*
1382 	 * Flush the real memory from the instruction cache.
1383 	 */
1384 	if ((pte_lo & (PTE_I | PTE_G)) == 0) {
1385 		moea_syncicache(pa, PAGE_SIZE);
1386 	}
1387 	PMAP_UNLOCK(kernel_pmap);
1388 }
1389 
1390 /*
1391  * Extract the physical page address associated with the given kernel virtual
1392  * address.
1393  */
1394 vm_offset_t
1395 moea_kextract(mmu_t mmu, vm_offset_t va)
1396 {
1397 	struct		pvo_entry *pvo;
1398 	vm_paddr_t pa;
1399 
1400 #ifdef UMA_MD_SMALL_ALLOC
1401 	/*
1402 	 * Allow direct mappings
1403 	 */
1404 	if (va < VM_MIN_KERNEL_ADDRESS) {
1405 		return (va);
1406 	}
1407 #endif
1408 
1409 	PMAP_LOCK(kernel_pmap);
1410 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
1411 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
1412 	pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
1413 	PMAP_UNLOCK(kernel_pmap);
1414 	return (pa);
1415 }
1416 
1417 /*
1418  * Remove a wired page from kernel virtual address space.
1419  */
1420 void
1421 moea_kremove(mmu_t mmu, vm_offset_t va)
1422 {
1423 
1424 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
1425 }
1426 
1427 /*
1428  * Map a range of physical addresses into kernel virtual address space.
1429  *
1430  * The value passed in *virt is a suggested virtual address for the mapping.
1431  * Architectures which can support a direct-mapped physical to virtual region
1432  * can return the appropriate address within that region, leaving '*virt'
1433  * unchanged.  We cannot and therefore do not; *virt is updated with the
1434  * first usable address after the mapped region.
1435  */
1436 vm_offset_t
1437 moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
1438     vm_offset_t pa_end, int prot)
1439 {
1440 	vm_offset_t	sva, va;
1441 
1442 	sva = *virt;
1443 	va = sva;
1444 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
1445 		moea_kenter(mmu, va, pa_start);
1446 	*virt = va;
1447 	return (sva);
1448 }
1449 
1450 /*
1451  * Returns true if the pmap's pv is one of the first
1452  * 16 pvs linked to from this page.  This count may
1453  * be changed upwards or downwards in the future; it
1454  * is only necessary that true be returned for a small
1455  * subset of pmaps for proper page aging.
1456  */
1457 boolean_t
1458 moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
1459 {
1460         int loops;
1461 	struct pvo_entry *pvo;
1462 
1463         if (!moea_initialized || (m->flags & PG_FICTITIOUS))
1464                 return FALSE;
1465 
1466 	loops = 0;
1467 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1468 		if (pvo->pvo_pmap == pmap)
1469 			return (TRUE);
1470 		if (++loops >= 16)
1471 			break;
1472 	}
1473 
1474 	return (FALSE);
1475 }
1476 
1477 /*
1478  * Return the number of managed mappings to the given physical page
1479  * that are wired.
1480  */
1481 int
1482 moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
1483 {
1484 	struct pvo_entry *pvo;
1485 	int count;
1486 
1487 	count = 0;
1488 	if (!moea_initialized || (m->flags & PG_FICTITIOUS) != 0)
1489 		return (count);
1490 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1491 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
1492 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
1493 			count++;
1494 	return (count);
1495 }
1496 
1497 static u_int	moea_vsidcontext;
1498 
1499 void
1500 moea_pinit(mmu_t mmu, pmap_t pmap)
1501 {
1502 	int	i, mask;
1503 	u_int	entropy;
1504 
1505 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
1506 	PMAP_LOCK_INIT(pmap);
1507 
1508 	entropy = 0;
1509 	__asm __volatile("mftb %0" : "=r"(entropy));
1510 
1511 	/*
1512 	 * Allocate some segment registers for this pmap.
1513 	 */
1514 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
1515 		u_int	hash, n;
1516 
1517 		/*
1518 		 * Create a new value by mutiplying by a prime and adding in
1519 		 * entropy from the timebase register.  This is to make the
1520 		 * VSID more random so that the PT hash function collides
1521 		 * less often.  (Note that the prime casues gcc to do shifts
1522 		 * instead of a multiply.)
1523 		 */
1524 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
1525 		hash = moea_vsidcontext & (NPMAPS - 1);
1526 		if (hash == 0)		/* 0 is special, avoid it */
1527 			continue;
1528 		n = hash >> 5;
1529 		mask = 1 << (hash & (VSID_NBPW - 1));
1530 		hash = (moea_vsidcontext & 0xfffff);
1531 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
1532 			/* anything free in this bucket? */
1533 			if (moea_vsid_bitmap[n] == 0xffffffff) {
1534 				entropy = (moea_vsidcontext >> 20);
1535 				continue;
1536 			}
1537 			i = ffs(~moea_vsid_bitmap[i]) - 1;
1538 			mask = 1 << i;
1539 			hash &= 0xfffff & ~(VSID_NBPW - 1);
1540 			hash |= i;
1541 		}
1542 		moea_vsid_bitmap[n] |= mask;
1543 		for (i = 0; i < 16; i++)
1544 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1545 		return;
1546 	}
1547 
1548 	panic("moea_pinit: out of segments");
1549 }
1550 
1551 /*
1552  * Initialize the pmap associated with process 0.
1553  */
1554 void
1555 moea_pinit0(mmu_t mmu, pmap_t pm)
1556 {
1557 
1558 	moea_pinit(mmu, pm);
1559 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
1560 }
1561 
1562 /*
1563  * Set the physical protection on the specified range of this map as requested.
1564  */
1565 void
1566 moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
1567     vm_prot_t prot)
1568 {
1569 	struct	pvo_entry *pvo;
1570 	struct	pte *pt;
1571 	int	pteidx;
1572 
1573 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
1574 	    ("moea_protect: non current pmap"));
1575 
1576 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1577 		moea_remove(mmu, pm, sva, eva);
1578 		return;
1579 	}
1580 
1581 	vm_page_lock_queues();
1582 	PMAP_LOCK(pm);
1583 	for (; sva < eva; sva += PAGE_SIZE) {
1584 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
1585 		if (pvo == NULL)
1586 			continue;
1587 
1588 		if ((prot & VM_PROT_EXECUTE) == 0)
1589 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1590 
1591 		/*
1592 		 * Grab the PTE pointer before we diddle with the cached PTE
1593 		 * copy.
1594 		 */
1595 		pt = moea_pvo_to_pte(pvo, pteidx);
1596 		/*
1597 		 * Change the protection of the page.
1598 		 */
1599 		pvo->pvo_pte.pte_lo &= ~PTE_PP;
1600 		pvo->pvo_pte.pte_lo |= PTE_BR;
1601 
1602 		/*
1603 		 * If the PVO is in the page table, update that pte as well.
1604 		 */
1605 		if (pt != NULL) {
1606 			moea_pte_change(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1607 			mtx_unlock(&moea_table_mutex);
1608 		}
1609 	}
1610 	vm_page_unlock_queues();
1611 	PMAP_UNLOCK(pm);
1612 }
1613 
1614 /*
1615  * Map a list of wired pages into kernel virtual address space.  This is
1616  * intended for temporary mappings which do not need page modification or
1617  * references recorded.  Existing mappings in the region are overwritten.
1618  */
1619 void
1620 moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
1621 {
1622 	vm_offset_t va;
1623 
1624 	va = sva;
1625 	while (count-- > 0) {
1626 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
1627 		va += PAGE_SIZE;
1628 		m++;
1629 	}
1630 }
1631 
1632 /*
1633  * Remove page mappings from kernel virtual address space.  Intended for
1634  * temporary mappings entered by moea_qenter.
1635  */
1636 void
1637 moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
1638 {
1639 	vm_offset_t va;
1640 
1641 	va = sva;
1642 	while (count-- > 0) {
1643 		moea_kremove(mmu, va);
1644 		va += PAGE_SIZE;
1645 	}
1646 }
1647 
1648 void
1649 moea_release(mmu_t mmu, pmap_t pmap)
1650 {
1651         int idx, mask;
1652 
1653 	/*
1654 	 * Free segment register's VSID
1655 	 */
1656         if (pmap->pm_sr[0] == 0)
1657                 panic("moea_release");
1658 
1659         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
1660         mask = 1 << (idx % VSID_NBPW);
1661         idx /= VSID_NBPW;
1662         moea_vsid_bitmap[idx] &= ~mask;
1663 	PMAP_LOCK_DESTROY(pmap);
1664 }
1665 
1666 /*
1667  * Remove the given range of addresses from the specified map.
1668  */
1669 void
1670 moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1671 {
1672 	struct	pvo_entry *pvo;
1673 	int	pteidx;
1674 
1675 	vm_page_lock_queues();
1676 	PMAP_LOCK(pm);
1677 	for (; sva < eva; sva += PAGE_SIZE) {
1678 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
1679 		if (pvo != NULL) {
1680 			moea_pvo_remove(pvo, pteidx);
1681 		}
1682 	}
1683 	PMAP_UNLOCK(pm);
1684 	vm_page_unlock_queues();
1685 }
1686 
1687 /*
1688  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
1689  * will reflect changes in pte's back to the vm_page.
1690  */
1691 void
1692 moea_remove_all(mmu_t mmu, vm_page_t m)
1693 {
1694 	struct  pvo_head *pvo_head;
1695 	struct	pvo_entry *pvo, *next_pvo;
1696 	pmap_t	pmap;
1697 
1698 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1699 
1700 	pvo_head = vm_page_to_pvoh(m);
1701 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
1702 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
1703 
1704 		MOEA_PVO_CHECK(pvo);	/* sanity check */
1705 		pmap = pvo->pvo_pmap;
1706 		PMAP_LOCK(pmap);
1707 		moea_pvo_remove(pvo, -1);
1708 		PMAP_UNLOCK(pmap);
1709 	}
1710 	vm_page_flag_clear(m, PG_WRITEABLE);
1711 }
1712 
1713 /*
1714  * Allocate a physical page of memory directly from the phys_avail map.
1715  * Can only be called from moea_bootstrap before avail start and end are
1716  * calculated.
1717  */
1718 static vm_offset_t
1719 moea_bootstrap_alloc(vm_size_t size, u_int align)
1720 {
1721 	vm_offset_t	s, e;
1722 	int		i, j;
1723 
1724 	size = round_page(size);
1725 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
1726 		if (align != 0)
1727 			s = (phys_avail[i] + align - 1) & ~(align - 1);
1728 		else
1729 			s = phys_avail[i];
1730 		e = s + size;
1731 
1732 		if (s < phys_avail[i] || e > phys_avail[i + 1])
1733 			continue;
1734 
1735 		if (s == phys_avail[i]) {
1736 			phys_avail[i] += size;
1737 		} else if (e == phys_avail[i + 1]) {
1738 			phys_avail[i + 1] -= size;
1739 		} else {
1740 			for (j = phys_avail_count * 2; j > i; j -= 2) {
1741 				phys_avail[j] = phys_avail[j - 2];
1742 				phys_avail[j + 1] = phys_avail[j - 1];
1743 			}
1744 
1745 			phys_avail[i + 3] = phys_avail[i + 1];
1746 			phys_avail[i + 1] = s;
1747 			phys_avail[i + 2] = e;
1748 			phys_avail_count++;
1749 		}
1750 
1751 		return (s);
1752 	}
1753 	panic("moea_bootstrap_alloc: could not allocate memory");
1754 }
1755 
1756 static void
1757 moea_syncicache(vm_offset_t pa, vm_size_t len)
1758 {
1759 	__syncicache((void *)pa, len);
1760 }
1761 
1762 static void
1763 tlbia(void)
1764 {
1765 	caddr_t	i;
1766 
1767 	SYNC();
1768 	for (i = 0; i < (caddr_t)0x00040000; i += 0x00001000) {
1769 		TLBIE(i);
1770 		EIEIO();
1771 	}
1772 	TLBSYNC();
1773 	SYNC();
1774 }
1775 
1776 static int
1777 moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
1778     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
1779 {
1780 	struct	pvo_entry *pvo;
1781 	u_int	sr;
1782 	int	first;
1783 	u_int	ptegidx;
1784 	int	i;
1785 	int     bootstrap;
1786 
1787 	moea_pvo_enter_calls++;
1788 	first = 0;
1789 	bootstrap = 0;
1790 
1791 	/*
1792 	 * Compute the PTE Group index.
1793 	 */
1794 	va &= ~ADDR_POFF;
1795 	sr = va_to_sr(pm->pm_sr, va);
1796 	ptegidx = va_to_pteg(sr, va);
1797 
1798 	/*
1799 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
1800 	 * there is a mapping.
1801 	 */
1802 	mtx_lock(&moea_table_mutex);
1803 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
1804 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
1805 			if ((pvo->pvo_pte.pte_lo & PTE_RPGN) == pa &&
1806 			    (pvo->pvo_pte.pte_lo & PTE_PP) ==
1807 			    (pte_lo & PTE_PP)) {
1808 				mtx_unlock(&moea_table_mutex);
1809 				return (0);
1810 			}
1811 			moea_pvo_remove(pvo, -1);
1812 			break;
1813 		}
1814 	}
1815 
1816 	/*
1817 	 * If we aren't overwriting a mapping, try to allocate.
1818 	 */
1819 	if (moea_initialized) {
1820 		pvo = uma_zalloc(zone, M_NOWAIT);
1821 	} else {
1822 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
1823 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
1824 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
1825 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
1826 		}
1827 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
1828 		moea_bpvo_pool_index++;
1829 		bootstrap = 1;
1830 	}
1831 
1832 	if (pvo == NULL) {
1833 		mtx_unlock(&moea_table_mutex);
1834 		return (ENOMEM);
1835 	}
1836 
1837 	moea_pvo_entries++;
1838 	pvo->pvo_vaddr = va;
1839 	pvo->pvo_pmap = pm;
1840 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
1841 	pvo->pvo_vaddr &= ~ADDR_POFF;
1842 	if (flags & VM_PROT_EXECUTE)
1843 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
1844 	if (flags & PVO_WIRED)
1845 		pvo->pvo_vaddr |= PVO_WIRED;
1846 	if (pvo_head != &moea_pvo_kunmanaged)
1847 		pvo->pvo_vaddr |= PVO_MANAGED;
1848 	if (bootstrap)
1849 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
1850 	if (flags & PVO_FAKE)
1851 		pvo->pvo_vaddr |= PVO_FAKE;
1852 
1853 	moea_pte_create(&pvo->pvo_pte, sr, va, pa | pte_lo);
1854 
1855 	/*
1856 	 * Remember if the list was empty and therefore will be the first
1857 	 * item.
1858 	 */
1859 	if (LIST_FIRST(pvo_head) == NULL)
1860 		first = 1;
1861 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
1862 
1863 	if (pvo->pvo_pte.pte_lo & PVO_WIRED)
1864 		pm->pm_stats.wired_count++;
1865 	pm->pm_stats.resident_count++;
1866 
1867 	/*
1868 	 * We hope this succeeds but it isn't required.
1869 	 */
1870 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte);
1871 	if (i >= 0) {
1872 		PVO_PTEGIDX_SET(pvo, i);
1873 	} else {
1874 		panic("moea_pvo_enter: overflow");
1875 		moea_pte_overflow++;
1876 	}
1877 	mtx_unlock(&moea_table_mutex);
1878 
1879 	return (first ? ENOENT : 0);
1880 }
1881 
1882 static void
1883 moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
1884 {
1885 	struct	pte *pt;
1886 
1887 	/*
1888 	 * If there is an active pte entry, we need to deactivate it (and
1889 	 * save the ref & cfg bits).
1890 	 */
1891 	pt = moea_pvo_to_pte(pvo, pteidx);
1892 	if (pt != NULL) {
1893 		moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1894 		mtx_unlock(&moea_table_mutex);
1895 		PVO_PTEGIDX_CLR(pvo);
1896 	} else {
1897 		moea_pte_overflow--;
1898 	}
1899 
1900 	/*
1901 	 * Update our statistics.
1902 	 */
1903 	pvo->pvo_pmap->pm_stats.resident_count--;
1904 	if (pvo->pvo_pte.pte_lo & PVO_WIRED)
1905 		pvo->pvo_pmap->pm_stats.wired_count--;
1906 
1907 	/*
1908 	 * Save the REF/CHG bits into their cache if the page is managed.
1909 	 */
1910 	if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) {
1911 		struct	vm_page *pg;
1912 
1913 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
1914 		if (pg != NULL) {
1915 			moea_attr_save(pg, pvo->pvo_pte.pte_lo &
1916 			    (PTE_REF | PTE_CHG));
1917 		}
1918 	}
1919 
1920 	/*
1921 	 * Remove this PVO from the PV list.
1922 	 */
1923 	LIST_REMOVE(pvo, pvo_vlink);
1924 
1925 	/*
1926 	 * Remove this from the overflow list and return it to the pool
1927 	 * if we aren't going to reuse it.
1928 	 */
1929 	LIST_REMOVE(pvo, pvo_olink);
1930 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
1931 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
1932 		    moea_upvo_zone, pvo);
1933 	moea_pvo_entries--;
1934 	moea_pvo_remove_calls++;
1935 }
1936 
1937 static __inline int
1938 moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
1939 {
1940 	int	pteidx;
1941 
1942 	/*
1943 	 * We can find the actual pte entry without searching by grabbing
1944 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
1945 	 * noticing the HID bit.
1946 	 */
1947 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
1948 	if (pvo->pvo_pte.pte_hi & PTE_HID)
1949 		pteidx ^= moea_pteg_mask * 8;
1950 
1951 	return (pteidx);
1952 }
1953 
1954 static struct pvo_entry *
1955 moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
1956 {
1957 	struct	pvo_entry *pvo;
1958 	int	ptegidx;
1959 	u_int	sr;
1960 
1961 	va &= ~ADDR_POFF;
1962 	sr = va_to_sr(pm->pm_sr, va);
1963 	ptegidx = va_to_pteg(sr, va);
1964 
1965 	mtx_lock(&moea_table_mutex);
1966 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
1967 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
1968 			if (pteidx_p)
1969 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
1970 			break;
1971 		}
1972 	}
1973 	mtx_unlock(&moea_table_mutex);
1974 
1975 	return (pvo);
1976 }
1977 
1978 static struct pte *
1979 moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
1980 {
1981 	struct	pte *pt;
1982 
1983 	/*
1984 	 * If we haven't been supplied the ptegidx, calculate it.
1985 	 */
1986 	if (pteidx == -1) {
1987 		int	ptegidx;
1988 		u_int	sr;
1989 
1990 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
1991 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
1992 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
1993 	}
1994 
1995 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
1996 	mtx_lock(&moea_table_mutex);
1997 
1998 	if ((pvo->pvo_pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
1999 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
2000 		    "valid pte index", pvo);
2001 	}
2002 
2003 	if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
2004 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
2005 		    "pvo but no valid pte", pvo);
2006 	}
2007 
2008 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
2009 		if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0) {
2010 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
2011 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
2012 		}
2013 
2014 		if (((pt->pte_lo ^ pvo->pvo_pte.pte_lo) & ~(PTE_CHG|PTE_REF))
2015 		    != 0) {
2016 			panic("moea_pvo_to_pte: pvo %p pte does not match "
2017 			    "pte %p in moea_pteg_table", pvo, pt);
2018 		}
2019 
2020 		mtx_assert(&moea_table_mutex, MA_OWNED);
2021 		return (pt);
2022 	}
2023 
2024 	if (pvo->pvo_pte.pte_hi & PTE_VALID) {
2025 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
2026 		    "moea_pteg_table but valid in pvo", pvo, pt);
2027 	}
2028 
2029 	mtx_unlock(&moea_table_mutex);
2030 	return (NULL);
2031 }
2032 
2033 /*
2034  * XXX: THIS STUFF SHOULD BE IN pte.c?
2035  */
2036 int
2037 moea_pte_spill(vm_offset_t addr)
2038 {
2039 	struct	pvo_entry *source_pvo, *victim_pvo;
2040 	struct	pvo_entry *pvo;
2041 	int	ptegidx, i, j;
2042 	u_int	sr;
2043 	struct	pteg *pteg;
2044 	struct	pte *pt;
2045 
2046 	moea_pte_spills++;
2047 
2048 	sr = mfsrin(addr);
2049 	ptegidx = va_to_pteg(sr, addr);
2050 
2051 	/*
2052 	 * Have to substitute some entry.  Use the primary hash for this.
2053 	 * Use low bits of timebase as random generator.
2054 	 */
2055 	pteg = &moea_pteg_table[ptegidx];
2056 	mtx_lock(&moea_table_mutex);
2057 	__asm __volatile("mftb %0" : "=r"(i));
2058 	i &= 7;
2059 	pt = &pteg->pt[i];
2060 
2061 	source_pvo = NULL;
2062 	victim_pvo = NULL;
2063 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
2064 		/*
2065 		 * We need to find a pvo entry for this address.
2066 		 */
2067 		MOEA_PVO_CHECK(pvo);
2068 		if (source_pvo == NULL &&
2069 		    moea_pte_match(&pvo->pvo_pte, sr, addr,
2070 		    pvo->pvo_pte.pte_hi & PTE_HID)) {
2071 			/*
2072 			 * Now found an entry to be spilled into the pteg.
2073 			 * The PTE is now valid, so we know it's active.
2074 			 */
2075 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte);
2076 
2077 			if (j >= 0) {
2078 				PVO_PTEGIDX_SET(pvo, j);
2079 				moea_pte_overflow--;
2080 				MOEA_PVO_CHECK(pvo);
2081 				mtx_unlock(&moea_table_mutex);
2082 				return (1);
2083 			}
2084 
2085 			source_pvo = pvo;
2086 
2087 			if (victim_pvo != NULL)
2088 				break;
2089 		}
2090 
2091 		/*
2092 		 * We also need the pvo entry of the victim we are replacing
2093 		 * so save the R & C bits of the PTE.
2094 		 */
2095 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
2096 		    moea_pte_compare(pt, &pvo->pvo_pte)) {
2097 			victim_pvo = pvo;
2098 			if (source_pvo != NULL)
2099 				break;
2100 		}
2101 	}
2102 
2103 	if (source_pvo == NULL) {
2104 		mtx_unlock(&moea_table_mutex);
2105 		return (0);
2106 	}
2107 
2108 	if (victim_pvo == NULL) {
2109 		if ((pt->pte_hi & PTE_HID) == 0)
2110 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
2111 			    "entry", pt);
2112 
2113 		/*
2114 		 * If this is a secondary PTE, we need to search it's primary
2115 		 * pvo bucket for the matching PVO.
2116 		 */
2117 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
2118 		    pvo_olink) {
2119 			MOEA_PVO_CHECK(pvo);
2120 			/*
2121 			 * We also need the pvo entry of the victim we are
2122 			 * replacing so save the R & C bits of the PTE.
2123 			 */
2124 			if (moea_pte_compare(pt, &pvo->pvo_pte)) {
2125 				victim_pvo = pvo;
2126 				break;
2127 			}
2128 		}
2129 
2130 		if (victim_pvo == NULL)
2131 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
2132 			    "entry", pt);
2133 	}
2134 
2135 	/*
2136 	 * We are invalidating the TLB entry for the EA we are replacing even
2137 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
2138 	 * contained in the TLB entry.
2139 	 */
2140 	source_pvo->pvo_pte.pte_hi &= ~PTE_HID;
2141 
2142 	moea_pte_unset(pt, &victim_pvo->pvo_pte, victim_pvo->pvo_vaddr);
2143 	moea_pte_set(pt, &source_pvo->pvo_pte);
2144 
2145 	PVO_PTEGIDX_CLR(victim_pvo);
2146 	PVO_PTEGIDX_SET(source_pvo, i);
2147 	moea_pte_replacements++;
2148 
2149 	MOEA_PVO_CHECK(victim_pvo);
2150 	MOEA_PVO_CHECK(source_pvo);
2151 
2152 	mtx_unlock(&moea_table_mutex);
2153 	return (1);
2154 }
2155 
2156 static int
2157 moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
2158 {
2159 	struct	pte *pt;
2160 	int	i;
2161 
2162 	mtx_assert(&moea_table_mutex, MA_OWNED);
2163 
2164 	/*
2165 	 * First try primary hash.
2166 	 */
2167 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
2168 		if ((pt->pte_hi & PTE_VALID) == 0) {
2169 			pvo_pt->pte_hi &= ~PTE_HID;
2170 			moea_pte_set(pt, pvo_pt);
2171 			return (i);
2172 		}
2173 	}
2174 
2175 	/*
2176 	 * Now try secondary hash.
2177 	 */
2178 	ptegidx ^= moea_pteg_mask;
2179 
2180 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
2181 		if ((pt->pte_hi & PTE_VALID) == 0) {
2182 			pvo_pt->pte_hi |= PTE_HID;
2183 			moea_pte_set(pt, pvo_pt);
2184 			return (i);
2185 		}
2186 	}
2187 
2188 	panic("moea_pte_insert: overflow");
2189 	return (-1);
2190 }
2191 
2192 static boolean_t
2193 moea_query_bit(vm_page_t m, int ptebit)
2194 {
2195 	struct	pvo_entry *pvo;
2196 	struct	pte *pt;
2197 
2198 #if 0
2199 	if (moea_attr_fetch(m) & ptebit)
2200 		return (TRUE);
2201 #endif
2202 
2203 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2204 		MOEA_PVO_CHECK(pvo);	/* sanity check */
2205 
2206 		/*
2207 		 * See if we saved the bit off.  If so, cache it and return
2208 		 * success.
2209 		 */
2210 		if (pvo->pvo_pte.pte_lo & ptebit) {
2211 			moea_attr_save(m, ptebit);
2212 			MOEA_PVO_CHECK(pvo);	/* sanity check */
2213 			return (TRUE);
2214 		}
2215 	}
2216 
2217 	/*
2218 	 * No luck, now go through the hard part of looking at the PTEs
2219 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
2220 	 * the PTEs.
2221 	 */
2222 	SYNC();
2223 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2224 		MOEA_PVO_CHECK(pvo);	/* sanity check */
2225 
2226 		/*
2227 		 * See if this pvo has a valid PTE.  if so, fetch the
2228 		 * REF/CHG bits from the valid PTE.  If the appropriate
2229 		 * ptebit is set, cache it and return success.
2230 		 */
2231 		pt = moea_pvo_to_pte(pvo, -1);
2232 		if (pt != NULL) {
2233 			moea_pte_synch(pt, &pvo->pvo_pte);
2234 			mtx_unlock(&moea_table_mutex);
2235 			if (pvo->pvo_pte.pte_lo & ptebit) {
2236 				moea_attr_save(m, ptebit);
2237 				MOEA_PVO_CHECK(pvo);	/* sanity check */
2238 				return (TRUE);
2239 			}
2240 		}
2241 	}
2242 
2243 	return (FALSE);
2244 }
2245 
2246 static u_int
2247 moea_clear_bit(vm_page_t m, int ptebit, int *origbit)
2248 {
2249 	u_int	count;
2250 	struct	pvo_entry *pvo;
2251 	struct	pte *pt;
2252 	int	rv;
2253 
2254 	/*
2255 	 * Clear the cached value.
2256 	 */
2257 	rv = moea_attr_fetch(m);
2258 	moea_attr_clear(m, ptebit);
2259 
2260 	/*
2261 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
2262 	 * we can reset the right ones).  note that since the pvo entries and
2263 	 * list heads are accessed via BAT0 and are never placed in the page
2264 	 * table, we don't have to worry about further accesses setting the
2265 	 * REF/CHG bits.
2266 	 */
2267 	SYNC();
2268 
2269 	/*
2270 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
2271 	 * valid pte clear the ptebit from the valid pte.
2272 	 */
2273 	count = 0;
2274 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2275 		MOEA_PVO_CHECK(pvo);	/* sanity check */
2276 		pt = moea_pvo_to_pte(pvo, -1);
2277 		if (pt != NULL) {
2278 			moea_pte_synch(pt, &pvo->pvo_pte);
2279 			if (pvo->pvo_pte.pte_lo & ptebit) {
2280 				count++;
2281 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
2282 			}
2283 			mtx_unlock(&moea_table_mutex);
2284 		}
2285 		rv |= pvo->pvo_pte.pte_lo;
2286 		pvo->pvo_pte.pte_lo &= ~ptebit;
2287 		MOEA_PVO_CHECK(pvo);	/* sanity check */
2288 	}
2289 
2290 	if (origbit != NULL) {
2291 		*origbit = rv;
2292 	}
2293 
2294 	return (count);
2295 }
2296 
2297 /*
2298  * Return true if the physical range is encompassed by the battable[idx]
2299  */
2300 static int
2301 moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
2302 {
2303 	u_int prot;
2304 	u_int32_t start;
2305 	u_int32_t end;
2306 	u_int32_t bat_ble;
2307 
2308 	/*
2309 	 * Return immediately if not a valid mapping
2310 	 */
2311 	if (!battable[idx].batu & BAT_Vs)
2312 		return (EINVAL);
2313 
2314 	/*
2315 	 * The BAT entry must be cache-inhibited, guarded, and r/w
2316 	 * so it can function as an i/o page
2317 	 */
2318 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
2319 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
2320 		return (EPERM);
2321 
2322 	/*
2323 	 * The address should be within the BAT range. Assume that the
2324 	 * start address in the BAT has the correct alignment (thus
2325 	 * not requiring masking)
2326 	 */
2327 	start = battable[idx].batl & BAT_PBS;
2328 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
2329 	end = start | (bat_ble << 15) | 0x7fff;
2330 
2331 	if ((pa < start) || ((pa + size) > end))
2332 		return (ERANGE);
2333 
2334 	return (0);
2335 }
2336 
2337 boolean_t
2338 moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2339 {
2340 	int i;
2341 
2342 	/*
2343 	 * This currently does not work for entries that
2344 	 * overlap 256M BAT segments.
2345 	 */
2346 
2347 	for(i = 0; i < 16; i++)
2348 		if (moea_bat_mapped(i, pa, size) == 0)
2349 			return (0);
2350 
2351 	return (EFAULT);
2352 }
2353 
2354 boolean_t
2355 moea_page_executable(mmu_t mmu, vm_page_t pg)
2356 {
2357 	return ((moea_attr_fetch(pg) & PTE_EXEC) == PTE_EXEC);
2358 }
2359 
2360 /*
2361  * Map a set of physical memory pages into the kernel virtual
2362  * address space. Return a pointer to where it is mapped. This
2363  * routine is intended to be used for mapping device memory,
2364  * NOT real memory.
2365  */
2366 void *
2367 moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2368 {
2369 	vm_offset_t va, tmpva, ppa, offset;
2370 	int i;
2371 
2372 	ppa = trunc_page(pa);
2373 	offset = pa & PAGE_MASK;
2374 	size = roundup(offset + size, PAGE_SIZE);
2375 
2376 	GIANT_REQUIRED;
2377 
2378 	/*
2379 	 * If the physical address lies within a valid BAT table entry,
2380 	 * return the 1:1 mapping. This currently doesn't work
2381 	 * for regions that overlap 256M BAT segments.
2382 	 */
2383 	for (i = 0; i < 16; i++) {
2384 		if (moea_bat_mapped(i, pa, size) == 0)
2385 			return ((void *) pa);
2386 	}
2387 
2388 	va = kmem_alloc_nofault(kernel_map, size);
2389 	if (!va)
2390 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
2391 
2392 	for (tmpva = va; size > 0;) {
2393 		moea_kenter(mmu, tmpva, ppa);
2394 		TLBIE(tmpva); /* XXX or should it be invalidate-all ? */
2395 		size -= PAGE_SIZE;
2396 		tmpva += PAGE_SIZE;
2397 		ppa += PAGE_SIZE;
2398 	}
2399 
2400 	return ((void *)(va + offset));
2401 }
2402 
2403 void
2404 moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
2405 {
2406 	vm_offset_t base, offset;
2407 
2408 	/*
2409 	 * If this is outside kernel virtual space, then it's a
2410 	 * battable entry and doesn't require unmapping
2411 	 */
2412 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
2413 		base = trunc_page(va);
2414 		offset = va & PAGE_MASK;
2415 		size = roundup(offset + size, PAGE_SIZE);
2416 		kmem_free(kernel_map, base, size);
2417 	}
2418 }
2419