]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/aim/mmu_oea.c
Don't use vm_page_flag_set() if installing bootstrap page-table entries
[FreeBSD/FreeBSD.git] / sys / powerpc / aim / mmu_oea.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 <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                 if (pmap_bootstrapped)
1135                         vm_page_flag_set(m, PG_WRITEABLE);
1136         } else
1137                 pte_lo |= PTE_BR;
1138
1139         if (prot & VM_PROT_EXECUTE)
1140                 pvo_flags |= PVO_EXECUTABLE;
1141
1142         if (wired)
1143                 pvo_flags |= PVO_WIRED;
1144
1145         if ((m->flags & PG_FICTITIOUS) != 0)
1146                 pvo_flags |= PVO_FAKE;
1147
1148         error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
1149             pte_lo, pvo_flags);
1150
1151         /*
1152          * Flush the real page from the instruction cache if this page is
1153          * mapped executable and cacheable and was not previously mapped (or
1154          * was not mapped executable).
1155          */
1156         if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
1157             (pte_lo & PTE_I) == 0 && was_exec == 0) {
1158                 /*
1159                  * Flush the real memory from the cache.
1160                  */
1161                 moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1162                 if (pg != NULL)
1163                         moea_attr_save(pg, PTE_EXEC);
1164         }
1165
1166         /* XXX syncicache always until problems are sorted */
1167         moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1168 }
1169
1170 /*
1171  * Maps a sequence of resident pages belonging to the same object.
1172  * The sequence begins with the given page m_start.  This page is
1173  * mapped at the given virtual address start.  Each subsequent page is
1174  * mapped at a virtual address that is offset from start by the same
1175  * amount as the page is offset from m_start within the object.  The
1176  * last page in the sequence is the page with the largest offset from
1177  * m_start that can be mapped at a virtual address less than the given
1178  * virtual address end.  Not every virtual page between start and end
1179  * is mapped; only those for which a resident page exists with the
1180  * corresponding offset from m_start are mapped.
1181  */
1182 void
1183 moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1184     vm_page_t m_start, vm_prot_t prot)
1185 {
1186         vm_page_t m;
1187         vm_pindex_t diff, psize;
1188
1189         psize = atop(end - start);
1190         m = m_start;
1191         PMAP_LOCK(pm);
1192         while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1193                 moea_enter_locked(pm, start + ptoa(diff), m, prot &
1194                     (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1195                 m = TAILQ_NEXT(m, listq);
1196         }
1197         PMAP_UNLOCK(pm);
1198 }
1199
1200 void
1201 moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
1202     vm_prot_t prot)
1203 {
1204
1205         PMAP_LOCK(pm);
1206         moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1207             FALSE);
1208         PMAP_UNLOCK(pm);
1209
1210 }
1211
1212 vm_paddr_t
1213 moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
1214 {
1215         struct  pvo_entry *pvo;
1216         vm_paddr_t pa;
1217
1218         PMAP_LOCK(pm);
1219         pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
1220         if (pvo == NULL)
1221                 pa = 0;
1222         else
1223                 pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
1224         PMAP_UNLOCK(pm);
1225         return (pa);
1226 }
1227
1228 /*
1229  * Atomically extract and hold the physical page with the given
1230  * pmap and virtual address pair if that mapping permits the given
1231  * protection.
1232  */
1233 vm_page_t
1234 moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1235 {
1236         struct  pvo_entry *pvo;
1237         vm_page_t m;
1238         
1239         m = NULL;
1240         vm_page_lock_queues();
1241         PMAP_LOCK(pmap);
1242         pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1243         if (pvo != NULL && (pvo->pvo_pte.pte_hi & PTE_VALID) &&
1244             ((pvo->pvo_pte.pte_lo & PTE_PP) == PTE_RW ||
1245              (prot & VM_PROT_WRITE) == 0)) {
1246                 m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
1247                 vm_page_hold(m);
1248         }
1249         vm_page_unlock_queues();
1250         PMAP_UNLOCK(pmap);
1251         return (m);
1252 }
1253
1254 void
1255 moea_init(mmu_t mmu)
1256 {
1257
1258         CTR0(KTR_PMAP, "moea_init");
1259
1260         moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
1261             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1262             UMA_ZONE_VM | UMA_ZONE_NOFREE);
1263         moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
1264             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1265             UMA_ZONE_VM | UMA_ZONE_NOFREE);
1266         moea_initialized = TRUE;
1267 }
1268
1269 boolean_t
1270 moea_is_modified(mmu_t mmu, vm_page_t m)
1271 {
1272
1273         if ((m->flags & (PG_FICTITIOUS |PG_UNMANAGED)) != 0)
1274                 return (FALSE);
1275
1276         return (moea_query_bit(m, PTE_CHG));
1277 }
1278
1279 void
1280 moea_clear_reference(mmu_t mmu, vm_page_t m)
1281 {
1282
1283         if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1284                 return;
1285         moea_clear_bit(m, PTE_REF, NULL);
1286 }
1287
1288 void
1289 moea_clear_modify(mmu_t mmu, vm_page_t m)
1290 {
1291
1292         if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1293                 return;
1294         moea_clear_bit(m, PTE_CHG, NULL);
1295 }
1296
1297 /*
1298  * Clear the write and modified bits in each of the given page's mappings.
1299  */
1300 void
1301 moea_remove_write(mmu_t mmu, vm_page_t m)
1302 {
1303         struct  pvo_entry *pvo;
1304         struct  pte *pt;
1305         pmap_t  pmap;
1306         u_int   lo;
1307
1308         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1309         if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
1310             (m->flags & PG_WRITEABLE) == 0)
1311                 return;
1312         lo = moea_attr_fetch(m);
1313         SYNC();
1314         LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1315                 pmap = pvo->pvo_pmap;
1316                 PMAP_LOCK(pmap);
1317                 if ((pvo->pvo_pte.pte_lo & PTE_PP) != PTE_BR) {
1318                         pt = moea_pvo_to_pte(pvo, -1);
1319                         pvo->pvo_pte.pte_lo &= ~PTE_PP;
1320                         pvo->pvo_pte.pte_lo |= PTE_BR;
1321                         if (pt != NULL) {
1322                                 moea_pte_synch(pt, &pvo->pvo_pte);
1323                                 lo |= pvo->pvo_pte.pte_lo;
1324                                 pvo->pvo_pte.pte_lo &= ~PTE_CHG;
1325                                 moea_pte_change(pt, &pvo->pvo_pte,
1326                                     pvo->pvo_vaddr);
1327                                 mtx_unlock(&moea_table_mutex);
1328                         }
1329                 }
1330                 PMAP_UNLOCK(pmap);
1331         }
1332         if ((lo & PTE_CHG) != 0) {
1333                 moea_attr_clear(m, PTE_CHG);
1334                 vm_page_dirty(m);
1335         }
1336         vm_page_flag_clear(m, PG_WRITEABLE);
1337 }
1338
1339 /*
1340  *      moea_ts_referenced:
1341  *
1342  *      Return a count of reference bits for a page, clearing those bits.
1343  *      It is not necessary for every reference bit to be cleared, but it
1344  *      is necessary that 0 only be returned when there are truly no
1345  *      reference bits set.
1346  *
1347  *      XXX: The exact number of bits to check and clear is a matter that
1348  *      should be tested and standardized at some point in the future for
1349  *      optimal aging of shared pages.
1350  */
1351 boolean_t
1352 moea_ts_referenced(mmu_t mmu, vm_page_t m)
1353 {
1354         int count;
1355
1356         if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1357                 return (0);
1358
1359         count = moea_clear_bit(m, PTE_REF, NULL);
1360
1361         return (count);
1362 }
1363
1364 /*
1365  * Map a wired page into kernel virtual address space.
1366  */
1367 void
1368 moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
1369 {
1370         u_int           pte_lo;
1371         int             error;  
1372         int             i;
1373
1374 #if 0
1375         if (va < VM_MIN_KERNEL_ADDRESS)
1376                 panic("moea_kenter: attempt to enter non-kernel address %#x",
1377                     va);
1378 #endif
1379
1380         pte_lo = PTE_I | PTE_G;
1381         for (i = 0; i < pregions_sz; i++) {
1382                 if ((pa >= pregions[i].mr_start) &&
1383                     (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
1384                         pte_lo &= ~(PTE_I | PTE_G);
1385                         break;
1386                 }
1387         }       
1388
1389         PMAP_LOCK(kernel_pmap);
1390         error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
1391             &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
1392
1393         if (error != 0 && error != ENOENT)
1394                 panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
1395                     pa, error);
1396
1397         /*
1398          * Flush the real memory from the instruction cache.
1399          */
1400         if ((pte_lo & (PTE_I | PTE_G)) == 0) {
1401                 moea_syncicache(pa, PAGE_SIZE);
1402         }
1403         PMAP_UNLOCK(kernel_pmap);
1404 }
1405
1406 /*
1407  * Extract the physical page address associated with the given kernel virtual
1408  * address.
1409  */
1410 vm_offset_t
1411 moea_kextract(mmu_t mmu, vm_offset_t va)
1412 {
1413         struct          pvo_entry *pvo;
1414         vm_paddr_t pa;
1415
1416 #ifdef UMA_MD_SMALL_ALLOC
1417         /*
1418          * Allow direct mappings
1419          */
1420         if (va < VM_MIN_KERNEL_ADDRESS) {
1421                 return (va);
1422         }
1423 #endif
1424
1425         PMAP_LOCK(kernel_pmap);
1426         pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
1427         KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
1428         pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
1429         PMAP_UNLOCK(kernel_pmap);
1430         return (pa);
1431 }
1432
1433 /*
1434  * Remove a wired page from kernel virtual address space.
1435  */
1436 void
1437 moea_kremove(mmu_t mmu, vm_offset_t va)
1438 {
1439
1440         moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
1441 }
1442
1443 /*
1444  * Map a range of physical addresses into kernel virtual address space.
1445  *
1446  * The value passed in *virt is a suggested virtual address for the mapping.
1447  * Architectures which can support a direct-mapped physical to virtual region
1448  * can return the appropriate address within that region, leaving '*virt'
1449  * unchanged.  We cannot and therefore do not; *virt is updated with the
1450  * first usable address after the mapped region.
1451  */
1452 vm_offset_t
1453 moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
1454     vm_offset_t pa_end, int prot)
1455 {
1456         vm_offset_t     sva, va;
1457
1458         sva = *virt;
1459         va = sva;
1460         for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
1461                 moea_kenter(mmu, va, pa_start);
1462         *virt = va;
1463         return (sva);
1464 }
1465
1466 /*
1467  * Returns true if the pmap's pv is one of the first
1468  * 16 pvs linked to from this page.  This count may
1469  * be changed upwards or downwards in the future; it
1470  * is only necessary that true be returned for a small
1471  * subset of pmaps for proper page aging.
1472  */
1473 boolean_t
1474 moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
1475 {
1476         int loops;
1477         struct pvo_entry *pvo;
1478
1479         if (!moea_initialized || (m->flags & PG_FICTITIOUS))
1480                 return FALSE;
1481
1482         loops = 0;
1483         LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1484                 if (pvo->pvo_pmap == pmap)
1485                         return (TRUE);
1486                 if (++loops >= 16)
1487                         break;
1488         }
1489
1490         return (FALSE);
1491 }
1492
1493 static u_int    moea_vsidcontext;
1494
1495 void
1496 moea_pinit(mmu_t mmu, pmap_t pmap)
1497 {
1498         int     i, mask;
1499         u_int   entropy;
1500
1501         KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
1502         PMAP_LOCK_INIT(pmap);
1503
1504         entropy = 0;
1505         __asm __volatile("mftb %0" : "=r"(entropy));
1506
1507         /*
1508          * Allocate some segment registers for this pmap.
1509          */
1510         for (i = 0; i < NPMAPS; i += VSID_NBPW) {
1511                 u_int   hash, n;
1512
1513                 /*
1514                  * Create a new value by mutiplying by a prime and adding in
1515                  * entropy from the timebase register.  This is to make the
1516                  * VSID more random so that the PT hash function collides
1517                  * less often.  (Note that the prime casues gcc to do shifts
1518                  * instead of a multiply.)
1519                  */
1520                 moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
1521                 hash = moea_vsidcontext & (NPMAPS - 1);
1522                 if (hash == 0)          /* 0 is special, avoid it */
1523                         continue;
1524                 n = hash >> 5;
1525                 mask = 1 << (hash & (VSID_NBPW - 1));
1526                 hash = (moea_vsidcontext & 0xfffff);
1527                 if (moea_vsid_bitmap[n] & mask) {       /* collision? */
1528                         /* anything free in this bucket? */
1529                         if (moea_vsid_bitmap[n] == 0xffffffff) {
1530                                 entropy = (moea_vsidcontext >> 20);
1531                                 continue;
1532                         }
1533                         i = ffs(~moea_vsid_bitmap[i]) - 1;
1534                         mask = 1 << i;
1535                         hash &= 0xfffff & ~(VSID_NBPW - 1);
1536                         hash |= i;
1537                 }
1538                 moea_vsid_bitmap[n] |= mask;
1539                 for (i = 0; i < 16; i++)
1540                         pmap->pm_sr[i] = VSID_MAKE(i, hash);
1541                 return;
1542         }
1543
1544         panic("moea_pinit: out of segments");
1545 }
1546
1547 /*
1548  * Initialize the pmap associated with process 0.
1549  */
1550 void
1551 moea_pinit0(mmu_t mmu, pmap_t pm)
1552 {
1553
1554         moea_pinit(mmu, pm);
1555         bzero(&pm->pm_stats, sizeof(pm->pm_stats));
1556 }
1557
1558 /*
1559  * Set the physical protection on the specified range of this map as requested.
1560  */
1561 void
1562 moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
1563     vm_prot_t prot)
1564 {
1565         struct  pvo_entry *pvo;
1566         struct  pte *pt;
1567         int     pteidx;
1568
1569         CTR4(KTR_PMAP, "moea_protect: pm=%p sva=%#x eva=%#x prot=%#x", pm, sva,
1570             eva, prot);
1571
1572
1573         KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
1574             ("moea_protect: non current pmap"));
1575
1576         if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1577                 moea_remove(mmu, pm, sva, eva);
1578                 return;
1579         }
1580
1581         vm_page_lock_queues();
1582         PMAP_LOCK(pm);
1583         for (; sva < eva; sva += PAGE_SIZE) {
1584                 pvo = moea_pvo_find_va(pm, sva, &pteidx);
1585                 if (pvo == NULL)
1586                         continue;
1587
1588                 if ((prot & VM_PROT_EXECUTE) == 0)
1589                         pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1590
1591                 /*
1592                  * Grab the PTE pointer before we diddle with the cached PTE
1593                  * copy.
1594                  */
1595                 pt = moea_pvo_to_pte(pvo, pteidx);
1596                 /*
1597                  * Change the protection of the page.
1598                  */
1599                 pvo->pvo_pte.pte_lo &= ~PTE_PP;
1600                 pvo->pvo_pte.pte_lo |= PTE_BR;
1601
1602                 /*
1603                  * If the PVO is in the page table, update that pte as well.
1604                  */
1605                 if (pt != NULL) {
1606                         moea_pte_change(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1607                         mtx_unlock(&moea_table_mutex);
1608                 }
1609         }
1610         vm_page_unlock_queues();
1611         PMAP_UNLOCK(pm);
1612 }
1613
1614 /*
1615  * Map a list of wired pages into kernel virtual address space.  This is
1616  * intended for temporary mappings which do not need page modification or
1617  * references recorded.  Existing mappings in the region are overwritten.
1618  */
1619 void
1620 moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
1621 {
1622         vm_offset_t va;
1623
1624         va = sva;
1625         while (count-- > 0) {
1626                 moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
1627                 va += PAGE_SIZE;
1628                 m++;
1629         }
1630 }
1631
1632 /*
1633  * Remove page mappings from kernel virtual address space.  Intended for
1634  * temporary mappings entered by moea_qenter.
1635  */
1636 void
1637 moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
1638 {
1639         vm_offset_t va;
1640
1641         va = sva;
1642         while (count-- > 0) {
1643                 moea_kremove(mmu, va);
1644                 va += PAGE_SIZE;
1645         }
1646 }
1647
1648 void
1649 moea_release(mmu_t mmu, pmap_t pmap)
1650 {
1651         int idx, mask;
1652         
1653         /*
1654          * Free segment register's VSID
1655          */
1656         if (pmap->pm_sr[0] == 0)
1657                 panic("moea_release");
1658
1659         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
1660         mask = 1 << (idx % VSID_NBPW);
1661         idx /= VSID_NBPW;
1662         moea_vsid_bitmap[idx] &= ~mask;
1663         PMAP_LOCK_DESTROY(pmap);
1664 }
1665
1666 /*
1667  * Remove the given range of addresses from the specified map.
1668  */
1669 void
1670 moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1671 {
1672         struct  pvo_entry *pvo;
1673         int     pteidx;
1674
1675         vm_page_lock_queues();
1676         PMAP_LOCK(pm);
1677         for (; sva < eva; sva += PAGE_SIZE) {
1678                 pvo = moea_pvo_find_va(pm, sva, &pteidx);
1679                 if (pvo != NULL) {
1680                         moea_pvo_remove(pvo, pteidx);
1681                 }
1682         }
1683         PMAP_UNLOCK(pm);
1684         vm_page_unlock_queues();
1685 }
1686
1687 /*
1688  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
1689  * will reflect changes in pte's back to the vm_page.
1690  */
1691 void
1692 moea_remove_all(mmu_t mmu, vm_page_t m)
1693 {
1694         struct  pvo_head *pvo_head;
1695         struct  pvo_entry *pvo, *next_pvo;
1696         pmap_t  pmap;
1697
1698         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1699
1700         pvo_head = vm_page_to_pvoh(m);
1701         for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
1702                 next_pvo = LIST_NEXT(pvo, pvo_vlink);
1703
1704                 MOEA_PVO_CHECK(pvo);    /* sanity check */
1705                 pmap = pvo->pvo_pmap;
1706                 PMAP_LOCK(pmap);
1707                 moea_pvo_remove(pvo, -1);
1708                 PMAP_UNLOCK(pmap);
1709         }
1710         vm_page_flag_clear(m, PG_WRITEABLE);
1711 }
1712
1713 /*
1714  * Allocate a physical page of memory directly from the phys_avail map.
1715  * Can only be called from moea_bootstrap before avail start and end are
1716  * calculated.
1717  */
1718 static vm_offset_t
1719 moea_bootstrap_alloc(vm_size_t size, u_int align)
1720 {
1721         vm_offset_t     s, e;
1722         int             i, j;
1723
1724         size = round_page(size);
1725         for (i = 0; phys_avail[i + 1] != 0; i += 2) {
1726                 if (align != 0)
1727                         s = (phys_avail[i] + align - 1) & ~(align - 1);
1728                 else
1729                         s = phys_avail[i];
1730                 e = s + size;
1731
1732                 if (s < phys_avail[i] || e > phys_avail[i + 1])
1733                         continue;
1734
1735                 if (s == phys_avail[i]) {
1736                         phys_avail[i] += size;
1737                 } else if (e == phys_avail[i + 1]) {
1738                         phys_avail[i + 1] -= size;
1739                 } else {
1740                         for (j = phys_avail_count * 2; j > i; j -= 2) {
1741                                 phys_avail[j] = phys_avail[j - 2];
1742                                 phys_avail[j + 1] = phys_avail[j - 1];
1743                         }
1744
1745                         phys_avail[i + 3] = phys_avail[i + 1];
1746                         phys_avail[i + 1] = s;
1747                         phys_avail[i + 2] = e;
1748                         phys_avail_count++;
1749                 }
1750
1751                 return (s);
1752         }
1753         panic("moea_bootstrap_alloc: could not allocate memory");
1754 }
1755
1756 /*
1757  * Return an unmapped pvo for a kernel virtual address.
1758  * Used by pmap functions that operate on physical pages.
1759  */
1760 static struct pvo_entry *
1761 moea_rkva_alloc(mmu_t mmu)
1762 {
1763         struct          pvo_entry *pvo;
1764         struct          pte *pt;
1765         vm_offset_t     kva;
1766         int             pteidx;
1767
1768         if (moea_rkva_count == 0)
1769                 panic("moea_rkva_alloc: no more reserved KVAs");
1770
1771         kva = moea_rkva_start + (PAGE_SIZE * --moea_rkva_count);
1772         moea_kenter(mmu, kva, 0);
1773
1774         pvo = moea_pvo_find_va(kernel_pmap, kva, &pteidx);
1775
1776         if (pvo == NULL)
1777                 panic("moea_kva_alloc: moea_pvo_find_va failed");
1778
1779         pt = moea_pvo_to_pte(pvo, pteidx);
1780
1781         if (pt == NULL)
1782                 panic("moea_kva_alloc: moea_pvo_to_pte failed");
1783
1784         moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1785         mtx_unlock(&moea_table_mutex);
1786         PVO_PTEGIDX_CLR(pvo);
1787
1788         moea_pte_overflow++;
1789
1790         return (pvo);
1791 }
1792
1793 static void
1794 moea_pa_map(struct pvo_entry *pvo, vm_offset_t pa, struct pte *saved_pt,
1795     int *depth_p)
1796 {
1797         struct  pte *pt;
1798
1799         /*
1800          * If this pvo already has a valid pte, we need to save it so it can
1801          * be restored later.  We then just reload the new PTE over the old
1802          * slot.
1803          */
1804         if (saved_pt != NULL) {
1805                 pt = moea_pvo_to_pte(pvo, -1);
1806
1807                 if (pt != NULL) {
1808                         moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1809                         mtx_unlock(&moea_table_mutex);
1810                         PVO_PTEGIDX_CLR(pvo);
1811                         moea_pte_overflow++;
1812                 }
1813
1814                 *saved_pt = pvo->pvo_pte;
1815
1816                 pvo->pvo_pte.pte_lo &= ~PTE_RPGN;
1817         }
1818
1819         pvo->pvo_pte.pte_lo |= pa;
1820
1821         if (!moea_pte_spill(pvo->pvo_vaddr))
1822                 panic("moea_pa_map: could not spill pvo %p", pvo);
1823
1824         if (depth_p != NULL)
1825                 (*depth_p)++;
1826 }
1827
1828 static void
1829 moea_pa_unmap(struct pvo_entry *pvo, struct pte *saved_pt, int *depth_p)
1830 {
1831         struct  pte *pt;
1832
1833         pt = moea_pvo_to_pte(pvo, -1);
1834
1835         if (pt != NULL) {
1836                 moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1837                 mtx_unlock(&moea_table_mutex);
1838                 PVO_PTEGIDX_CLR(pvo);
1839                 moea_pte_overflow++;
1840         }
1841
1842         pvo->pvo_pte.pte_lo &= ~PTE_RPGN;
1843
1844         /*
1845          * If there is a saved PTE and it's valid, restore it and return.
1846          */
1847         if (saved_pt != NULL && (saved_pt->pte_lo & PTE_RPGN) != 0) {
1848                 if (depth_p != NULL && --(*depth_p) == 0)
1849                         panic("moea_pa_unmap: restoring but depth == 0");
1850
1851                 pvo->pvo_pte = *saved_pt;
1852
1853                 if (!moea_pte_spill(pvo->pvo_vaddr))
1854                         panic("moea_pa_unmap: could not spill pvo %p", pvo);
1855         }
1856 }
1857
1858 static void
1859 moea_syncicache(vm_offset_t pa, vm_size_t len)
1860 {
1861         __syncicache((void *)pa, len);
1862 }
1863
1864 static void
1865 tlbia(void)
1866 {
1867         caddr_t i;
1868
1869         SYNC();
1870         for (i = 0; i < (caddr_t)0x00040000; i += 0x00001000) {
1871                 TLBIE(i);
1872                 EIEIO();
1873         }
1874         TLBSYNC();
1875         SYNC();
1876 }
1877
1878 static int
1879 moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
1880     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
1881 {
1882         struct  pvo_entry *pvo;
1883         u_int   sr;
1884         int     first;
1885         u_int   ptegidx;
1886         int     i;
1887         int     bootstrap;
1888
1889         moea_pvo_enter_calls++;
1890         first = 0;
1891         bootstrap = 0;
1892
1893         /*
1894          * Compute the PTE Group index.
1895          */
1896         va &= ~ADDR_POFF;
1897         sr = va_to_sr(pm->pm_sr, va);
1898         ptegidx = va_to_pteg(sr, va);
1899
1900         /*
1901          * Remove any existing mapping for this page.  Reuse the pvo entry if
1902          * there is a mapping.
1903          */
1904         mtx_lock(&moea_table_mutex);
1905         LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
1906                 if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
1907                         if ((pvo->pvo_pte.pte_lo & PTE_RPGN) == pa &&
1908                             (pvo->pvo_pte.pte_lo & PTE_PP) ==
1909                             (pte_lo & PTE_PP)) {
1910                                 mtx_unlock(&moea_table_mutex);
1911                                 return (0);
1912                         }
1913                         moea_pvo_remove(pvo, -1);
1914                         break;
1915                 }
1916         }
1917
1918         /*
1919          * If we aren't overwriting a mapping, try to allocate.
1920          */
1921         if (moea_initialized) {
1922                 pvo = uma_zalloc(zone, M_NOWAIT);
1923         } else {
1924                 if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
1925                         panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
1926                               moea_bpvo_pool_index, BPVO_POOL_SIZE, 
1927                               BPVO_POOL_SIZE * sizeof(struct pvo_entry));
1928                 }
1929                 pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
1930                 moea_bpvo_pool_index++;
1931                 bootstrap = 1;
1932         }
1933
1934         if (pvo == NULL) {
1935                 mtx_unlock(&moea_table_mutex);
1936                 return (ENOMEM);
1937         }
1938
1939         moea_pvo_entries++;
1940         pvo->pvo_vaddr = va;
1941         pvo->pvo_pmap = pm;
1942         LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
1943         pvo->pvo_vaddr &= ~ADDR_POFF;
1944         if (flags & VM_PROT_EXECUTE)
1945                 pvo->pvo_vaddr |= PVO_EXECUTABLE;
1946         if (flags & PVO_WIRED)
1947                 pvo->pvo_vaddr |= PVO_WIRED;
1948         if (pvo_head != &moea_pvo_kunmanaged)
1949                 pvo->pvo_vaddr |= PVO_MANAGED;
1950         if (bootstrap)
1951                 pvo->pvo_vaddr |= PVO_BOOTSTRAP;
1952         if (flags & PVO_FAKE)
1953                 pvo->pvo_vaddr |= PVO_FAKE;
1954
1955         moea_pte_create(&pvo->pvo_pte, sr, va, pa | pte_lo);
1956
1957         /*
1958          * Remember if the list was empty and therefore will be the first
1959          * item.
1960          */
1961         if (LIST_FIRST(pvo_head) == NULL)
1962                 first = 1;
1963         LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
1964
1965         if (pvo->pvo_pte.pte_lo & PVO_WIRED)
1966                 pm->pm_stats.wired_count++;
1967         pm->pm_stats.resident_count++;
1968
1969         /*
1970          * We hope this succeeds but it isn't required.
1971          */
1972         i = moea_pte_insert(ptegidx, &pvo->pvo_pte);
1973         if (i >= 0) {
1974                 PVO_PTEGIDX_SET(pvo, i);
1975         } else {
1976                 panic("moea_pvo_enter: overflow");
1977                 moea_pte_overflow++;
1978         }
1979         mtx_unlock(&moea_table_mutex);
1980
1981         return (first ? ENOENT : 0);
1982 }
1983
1984 static void
1985 moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
1986 {
1987         struct  pte *pt;
1988
1989         /*
1990          * If there is an active pte entry, we need to deactivate it (and
1991          * save the ref & cfg bits).
1992          */
1993         pt = moea_pvo_to_pte(pvo, pteidx);
1994         if (pt != NULL) {
1995                 moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1996                 mtx_unlock(&moea_table_mutex);
1997                 PVO_PTEGIDX_CLR(pvo);
1998         } else {
1999                 moea_pte_overflow--;
2000         }
2001
2002         /*
2003          * Update our statistics.
2004          */
2005         pvo->pvo_pmap->pm_stats.resident_count--;
2006         if (pvo->pvo_pte.pte_lo & PVO_WIRED)
2007                 pvo->pvo_pmap->pm_stats.wired_count--;
2008
2009         /*
2010          * Save the REF/CHG bits into their cache if the page is managed.
2011          */
2012         if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) {
2013                 struct  vm_page *pg;
2014
2015                 pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
2016                 if (pg != NULL) {
2017                         moea_attr_save(pg, pvo->pvo_pte.pte_lo &
2018                             (PTE_REF | PTE_CHG));
2019                 }
2020         }
2021
2022         /*
2023          * Remove this PVO from the PV list.
2024          */
2025         LIST_REMOVE(pvo, pvo_vlink);
2026
2027         /*
2028          * Remove this from the overflow list and return it to the pool
2029          * if we aren't going to reuse it.
2030          */
2031         LIST_REMOVE(pvo, pvo_olink);
2032         if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
2033                 uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
2034                     moea_upvo_zone, pvo);
2035         moea_pvo_entries--;
2036         moea_pvo_remove_calls++;
2037 }
2038
2039 static __inline int
2040 moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
2041 {
2042         int     pteidx;
2043
2044         /*
2045          * We can find the actual pte entry without searching by grabbing
2046          * the PTEG index from 3 unused bits in pte_lo[11:9] and by
2047          * noticing the HID bit.
2048          */
2049         pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
2050         if (pvo->pvo_pte.pte_hi & PTE_HID)
2051                 pteidx ^= moea_pteg_mask * 8;
2052
2053         return (pteidx);
2054 }
2055
2056 static struct pvo_entry *
2057 moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
2058 {
2059         struct  pvo_entry *pvo;
2060         int     ptegidx;
2061         u_int   sr;
2062
2063         va &= ~ADDR_POFF;
2064         sr = va_to_sr(pm->pm_sr, va);
2065         ptegidx = va_to_pteg(sr, va);
2066
2067         mtx_lock(&moea_table_mutex);
2068         LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
2069                 if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
2070                         if (pteidx_p)
2071                                 *pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2072                         break;
2073                 }
2074         }
2075         mtx_unlock(&moea_table_mutex);
2076
2077         return (pvo);
2078 }
2079
2080 static struct pte *
2081 moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
2082 {
2083         struct  pte *pt;
2084
2085         /*
2086          * If we haven't been supplied the ptegidx, calculate it.
2087          */
2088         if (pteidx == -1) {
2089                 int     ptegidx;
2090                 u_int   sr;
2091
2092                 sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
2093                 ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
2094                 pteidx = moea_pvo_pte_index(pvo, ptegidx);
2095         }
2096
2097         pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2098         mtx_lock(&moea_table_mutex);
2099
2100         if ((pvo->pvo_pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
2101                 panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
2102                     "valid pte index", pvo);
2103         }
2104
2105         if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
2106                 panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
2107                     "pvo but no valid pte", pvo);
2108         }
2109
2110         if ((pt->pte_hi ^ (pvo->pvo_pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
2111                 if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0) {
2112                         panic("moea_pvo_to_pte: pvo %p has valid pte in "
2113                             "moea_pteg_table %p but invalid in pvo", pvo, pt);
2114                 }
2115
2116                 if (((pt->pte_lo ^ pvo->pvo_pte.pte_lo) & ~(PTE_CHG|PTE_REF))
2117                     != 0) {
2118                         panic("moea_pvo_to_pte: pvo %p pte does not match "
2119                             "pte %p in moea_pteg_table", pvo, pt);
2120                 }
2121
2122                 mtx_assert(&moea_table_mutex, MA_OWNED);
2123                 return (pt);
2124         }
2125
2126         if (pvo->pvo_pte.pte_hi & PTE_VALID) {
2127                 panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
2128                     "moea_pteg_table but valid in pvo", pvo, pt);
2129         }
2130
2131         mtx_unlock(&moea_table_mutex);
2132         return (NULL);
2133 }
2134
2135 /*
2136  * XXX: THIS STUFF SHOULD BE IN pte.c?
2137  */
2138 int
2139 moea_pte_spill(vm_offset_t addr)
2140 {
2141         struct  pvo_entry *source_pvo, *victim_pvo;
2142         struct  pvo_entry *pvo;
2143         int     ptegidx, i, j;
2144         u_int   sr;
2145         struct  pteg *pteg;
2146         struct  pte *pt;
2147
2148         moea_pte_spills++;
2149
2150         sr = mfsrin(addr);
2151         ptegidx = va_to_pteg(sr, addr);
2152
2153         /*
2154          * Have to substitute some entry.  Use the primary hash for this.
2155          * Use low bits of timebase as random generator.
2156          */
2157         pteg = &moea_pteg_table[ptegidx];
2158         mtx_lock(&moea_table_mutex);
2159         __asm __volatile("mftb %0" : "=r"(i));
2160         i &= 7;
2161         pt = &pteg->pt[i];
2162
2163         source_pvo = NULL;
2164         victim_pvo = NULL;
2165         LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
2166                 /*
2167                  * We need to find a pvo entry for this address.
2168                  */
2169                 MOEA_PVO_CHECK(pvo);
2170                 if (source_pvo == NULL &&
2171                     moea_pte_match(&pvo->pvo_pte, sr, addr,
2172                     pvo->pvo_pte.pte_hi & PTE_HID)) {
2173                         /*
2174                          * Now found an entry to be spilled into the pteg.
2175                          * The PTE is now valid, so we know it's active.
2176                          */
2177                         j = moea_pte_insert(ptegidx, &pvo->pvo_pte);
2178
2179                         if (j >= 0) {
2180                                 PVO_PTEGIDX_SET(pvo, j);
2181                                 moea_pte_overflow--;
2182                                 MOEA_PVO_CHECK(pvo);
2183                                 mtx_unlock(&moea_table_mutex);
2184                                 return (1);
2185                         }
2186
2187                         source_pvo = pvo;
2188
2189                         if (victim_pvo != NULL)
2190                                 break;
2191                 }
2192
2193                 /*
2194                  * We also need the pvo entry of the victim we are replacing
2195                  * so save the R & C bits of the PTE.
2196                  */
2197                 if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
2198                     moea_pte_compare(pt, &pvo->pvo_pte)) {
2199                         victim_pvo = pvo;
2200                         if (source_pvo != NULL)
2201                                 break;
2202                 }
2203         }
2204
2205         if (source_pvo == NULL) {
2206                 mtx_unlock(&moea_table_mutex);
2207                 return (0);
2208         }
2209
2210         if (victim_pvo == NULL) {
2211                 if ((pt->pte_hi & PTE_HID) == 0)
2212                         panic("moea_pte_spill: victim p-pte (%p) has no pvo"
2213                             "entry", pt);
2214
2215                 /*
2216                  * If this is a secondary PTE, we need to search it's primary
2217                  * pvo bucket for the matching PVO.
2218                  */
2219                 LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
2220                     pvo_olink) {
2221                         MOEA_PVO_CHECK(pvo);
2222                         /*
2223                          * We also need the pvo entry of the victim we are
2224                          * replacing so save the R & C bits of the PTE.
2225                          */
2226                         if (moea_pte_compare(pt, &pvo->pvo_pte)) {
2227                                 victim_pvo = pvo;
2228                                 break;
2229                         }
2230                 }
2231
2232                 if (victim_pvo == NULL)
2233                         panic("moea_pte_spill: victim s-pte (%p) has no pvo"
2234                             "entry", pt);
2235         }
2236
2237         /*
2238          * We are invalidating the TLB entry for the EA we are replacing even
2239          * though it's valid.  If we don't, we lose any ref/chg bit changes
2240          * contained in the TLB entry.
2241          */
2242         source_pvo->pvo_pte.pte_hi &= ~PTE_HID;
2243
2244         moea_pte_unset(pt, &victim_pvo->pvo_pte, victim_pvo->pvo_vaddr);
2245         moea_pte_set(pt, &source_pvo->pvo_pte);
2246
2247         PVO_PTEGIDX_CLR(victim_pvo);
2248         PVO_PTEGIDX_SET(source_pvo, i);
2249         moea_pte_replacements++;
2250
2251         MOEA_PVO_CHECK(victim_pvo);
2252         MOEA_PVO_CHECK(source_pvo);
2253
2254         mtx_unlock(&moea_table_mutex);
2255         return (1);
2256 }
2257
2258 static int
2259 moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
2260 {
2261         struct  pte *pt;
2262         int     i;
2263
2264         mtx_assert(&moea_table_mutex, MA_OWNED);
2265
2266         /*
2267          * First try primary hash.
2268          */
2269         for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
2270                 if ((pt->pte_hi & PTE_VALID) == 0) {
2271                         pvo_pt->pte_hi &= ~PTE_HID;
2272                         moea_pte_set(pt, pvo_pt);
2273                         return (i);
2274                 }
2275         }
2276
2277         /*
2278          * Now try secondary hash.
2279          */
2280         ptegidx ^= moea_pteg_mask;
2281         ptegidx++;
2282         for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
2283                 if ((pt->pte_hi & PTE_VALID) == 0) {
2284                         pvo_pt->pte_hi |= PTE_HID;
2285                         moea_pte_set(pt, pvo_pt);
2286                         return (i);
2287                 }
2288         }
2289
2290         panic("moea_pte_insert: overflow");
2291         return (-1);
2292 }
2293
2294 static boolean_t
2295 moea_query_bit(vm_page_t m, int ptebit)
2296 {
2297         struct  pvo_entry *pvo;
2298         struct  pte *pt;
2299
2300 #if 0
2301         if (moea_attr_fetch(m) & ptebit)
2302                 return (TRUE);
2303 #endif
2304
2305         LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2306                 MOEA_PVO_CHECK(pvo);    /* sanity check */
2307
2308                 /*
2309                  * See if we saved the bit off.  If so, cache it and return
2310                  * success.
2311                  */
2312                 if (pvo->pvo_pte.pte_lo & ptebit) {
2313                         moea_attr_save(m, ptebit);
2314                         MOEA_PVO_CHECK(pvo);    /* sanity check */
2315                         return (TRUE);
2316                 }
2317         }
2318
2319         /*
2320          * No luck, now go through the hard part of looking at the PTEs
2321          * themselves.  Sync so that any pending REF/CHG bits are flushed to
2322          * the PTEs.
2323          */
2324         SYNC();
2325         LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2326                 MOEA_PVO_CHECK(pvo);    /* sanity check */
2327
2328                 /*
2329                  * See if this pvo has a valid PTE.  if so, fetch the
2330                  * REF/CHG bits from the valid PTE.  If the appropriate
2331                  * ptebit is set, cache it and return success.
2332                  */
2333                 pt = moea_pvo_to_pte(pvo, -1);
2334                 if (pt != NULL) {
2335                         moea_pte_synch(pt, &pvo->pvo_pte);
2336                         mtx_unlock(&moea_table_mutex);
2337                         if (pvo->pvo_pte.pte_lo & ptebit) {
2338                                 moea_attr_save(m, ptebit);
2339                                 MOEA_PVO_CHECK(pvo);    /* sanity check */
2340                                 return (TRUE);
2341                         }
2342                 }
2343         }
2344
2345         return (FALSE);
2346 }
2347
2348 static u_int
2349 moea_clear_bit(vm_page_t m, int ptebit, int *origbit)
2350 {
2351         u_int   count;
2352         struct  pvo_entry *pvo;
2353         struct  pte *pt;
2354         int     rv;
2355
2356         /*
2357          * Clear the cached value.
2358          */
2359         rv = moea_attr_fetch(m);
2360         moea_attr_clear(m, ptebit);
2361
2362         /*
2363          * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
2364          * we can reset the right ones).  note that since the pvo entries and
2365          * list heads are accessed via BAT0 and are never placed in the page
2366          * table, we don't have to worry about further accesses setting the
2367          * REF/CHG bits.
2368          */
2369         SYNC();
2370
2371         /*
2372          * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
2373          * valid pte clear the ptebit from the valid pte.
2374          */
2375         count = 0;
2376         LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2377                 MOEA_PVO_CHECK(pvo);    /* sanity check */
2378                 pt = moea_pvo_to_pte(pvo, -1);
2379                 if (pt != NULL) {
2380                         moea_pte_synch(pt, &pvo->pvo_pte);
2381                         if (pvo->pvo_pte.pte_lo & ptebit) {
2382                                 count++;
2383                                 moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
2384                         }
2385                         mtx_unlock(&moea_table_mutex);
2386                 }
2387                 rv |= pvo->pvo_pte.pte_lo;
2388                 pvo->pvo_pte.pte_lo &= ~ptebit;
2389                 MOEA_PVO_CHECK(pvo);    /* sanity check */
2390         }
2391
2392         if (origbit != NULL) {
2393                 *origbit = rv;
2394         }
2395
2396         return (count);
2397 }
2398
2399 /*
2400  * Return true if the physical range is encompassed by the battable[idx]
2401  */
2402 static int
2403 moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
2404 {
2405         u_int prot;
2406         u_int32_t start;
2407         u_int32_t end;
2408         u_int32_t bat_ble;
2409
2410         /*
2411          * Return immediately if not a valid mapping
2412          */
2413         if (!battable[idx].batu & BAT_Vs)
2414                 return (EINVAL);
2415
2416         /*
2417          * The BAT entry must be cache-inhibited, guarded, and r/w
2418          * so it can function as an i/o page
2419          */
2420         prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
2421         if (prot != (BAT_I|BAT_G|BAT_PP_RW))
2422                 return (EPERM); 
2423
2424         /*
2425          * The address should be within the BAT range. Assume that the
2426          * start address in the BAT has the correct alignment (thus
2427          * not requiring masking)
2428          */
2429         start = battable[idx].batl & BAT_PBS;
2430         bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
2431         end = start | (bat_ble << 15) | 0x7fff;
2432
2433         if ((pa < start) || ((pa + size) > end))
2434                 return (ERANGE);
2435
2436         return (0);
2437 }
2438
2439 boolean_t
2440 moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2441 {
2442         int i;
2443
2444         /*
2445          * This currently does not work for entries that 
2446          * overlap 256M BAT segments.
2447          */
2448
2449         for(i = 0; i < 16; i++)
2450                 if (moea_bat_mapped(i, pa, size) == 0)
2451                         return (0);
2452
2453         return (EFAULT);
2454 }
2455
2456 /*
2457  * Map a set of physical memory pages into the kernel virtual
2458  * address space. Return a pointer to where it is mapped. This
2459  * routine is intended to be used for mapping device memory,
2460  * NOT real memory.
2461  */
2462 void *
2463 moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2464 {
2465         vm_offset_t va, tmpva, ppa, offset;
2466         int i;
2467
2468         ppa = trunc_page(pa);
2469         offset = pa & PAGE_MASK;
2470         size = roundup(offset + size, PAGE_SIZE);
2471         
2472         GIANT_REQUIRED;
2473
2474         /*
2475          * If the physical address lies within a valid BAT table entry,
2476          * return the 1:1 mapping. This currently doesn't work
2477          * for regions that overlap 256M BAT segments.
2478          */
2479         for (i = 0; i < 16; i++) {
2480                 if (moea_bat_mapped(i, pa, size) == 0)
2481                         return ((void *) pa);
2482         }
2483
2484         va = kmem_alloc_nofault(kernel_map, size);
2485         if (!va)
2486                 panic("moea_mapdev: Couldn't alloc kernel virtual memory");
2487
2488         for (tmpva = va; size > 0;) {
2489                 moea_kenter(mmu, tmpva, ppa);
2490                 TLBIE(tmpva); /* XXX or should it be invalidate-all ? */
2491                 size -= PAGE_SIZE;
2492                 tmpva += PAGE_SIZE;
2493                 ppa += PAGE_SIZE;
2494         }
2495
2496         return ((void *)(va + offset));
2497 }
2498
2499 void
2500 moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
2501 {
2502         vm_offset_t base, offset;
2503
2504         /*
2505          * If this is outside kernel virtual space, then it's a
2506          * battable entry and doesn't require unmapping
2507          */
2508         if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
2509                 base = trunc_page(va);
2510                 offset = va & PAGE_MASK;
2511                 size = roundup(offset + size, PAGE_SIZE);
2512                 kmem_free(kernel_map, base, size);
2513         }
2514 }