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