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