2 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
37 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
38 * Copyright (C) 1995, 1996 TooLs GmbH.
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
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.
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.
66 * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
69 * Copyright (C) 2001 Benno Rice.
70 * All rights reserved.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
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.
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.
93 #include <sys/cdefs.h>
94 __FBSDID("$FreeBSD$");
97 * Manages physical address maps.
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.
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.
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
117 #include "opt_kstack_pages.h"
119 #include <sys/param.h>
120 #include <sys/kernel.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>
130 #include <dev/ofw/openfirm.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>
143 #include <machine/cpu.h>
144 #include <machine/platform.h>
145 #include <machine/bat.h>
146 #include <machine/frame.h>
147 #include <machine/md_var.h>
148 #include <machine/psl.h>
149 #include <machine/pte.h>
150 #include <machine/smp.h>
151 #include <machine/sr.h>
152 #include <machine/mmuvar.h>
158 #define TODO panic("%s: not implemented", __func__);
160 #define VSID_MAKE(sr, hash) ((sr) | (((hash) & 0xfffff) << 4))
161 #define VSID_TO_SR(vsid) ((vsid) & 0xf)
162 #define VSID_TO_HASH(vsid) (((vsid) >> 4) & 0xfffff)
164 #define PVO_PTEGIDX_MASK 0x007 /* which PTEG slot */
165 #define PVO_PTEGIDX_VALID 0x008 /* slot is valid */
166 #define PVO_WIRED 0x010 /* PVO entry is wired */
167 #define PVO_MANAGED 0x020 /* PVO entry is managed */
168 #define PVO_EXECUTABLE 0x040 /* PVO entry is executable */
169 #define PVO_BOOTSTRAP 0x080 /* PVO entry allocated during
171 #define PVO_FAKE 0x100 /* fictitious phys page */
172 #define PVO_VADDR(pvo) ((pvo)->pvo_vaddr & ~ADDR_POFF)
173 #define PVO_ISEXECUTABLE(pvo) ((pvo)->pvo_vaddr & PVO_EXECUTABLE)
174 #define PVO_ISFAKE(pvo) ((pvo)->pvo_vaddr & PVO_FAKE)
175 #define PVO_PTEGIDX_GET(pvo) ((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK)
176 #define PVO_PTEGIDX_ISSET(pvo) ((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID)
177 #define PVO_PTEGIDX_CLR(pvo) \
178 ((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK)))
179 #define PVO_PTEGIDX_SET(pvo, i) \
180 ((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID))
182 #define MOEA_PVO_CHECK(pvo)
192 * Map of physical memory regions.
194 static struct mem_region *regions;
195 static struct mem_region *pregions;
196 u_int phys_avail_count;
197 int regions_sz, pregions_sz;
198 static struct ofw_map *translations;
200 extern struct pmap ofw_pmap;
203 * Lock for the pteg and pvo tables.
205 struct mtx moea_table_mutex;
207 /* tlbie instruction synchronization */
208 static struct mtx tlbie_mtx;
213 static struct pteg *moea_pteg_table;
214 u_int moea_pteg_count;
215 u_int moea_pteg_mask;
220 struct pvo_head *moea_pvo_table; /* pvo entries by pteg index */
221 struct pvo_head moea_pvo_kunmanaged =
222 LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged); /* list of unmanaged pages */
223 struct pvo_head moea_pvo_unmanaged =
224 LIST_HEAD_INITIALIZER(moea_pvo_unmanaged); /* list of unmanaged pages */
226 uma_zone_t moea_upvo_zone; /* zone for pvo entries for unmanaged pages */
227 uma_zone_t moea_mpvo_zone; /* zone for pvo entries for managed pages */
229 #define BPVO_POOL_SIZE 32768
230 static struct pvo_entry *moea_bpvo_pool;
231 static int moea_bpvo_pool_index = 0;
233 #define VSID_NBPW (sizeof(u_int32_t) * 8)
234 static u_int moea_vsid_bitmap[NPMAPS / VSID_NBPW];
236 static boolean_t moea_initialized = FALSE;
241 u_int moea_pte_valid = 0;
242 u_int moea_pte_overflow = 0;
243 u_int moea_pte_replacements = 0;
244 u_int moea_pvo_entries = 0;
245 u_int moea_pvo_enter_calls = 0;
246 u_int moea_pvo_remove_calls = 0;
247 u_int moea_pte_spills = 0;
248 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
250 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
251 &moea_pte_overflow, 0, "");
252 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
253 &moea_pte_replacements, 0, "");
254 SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
256 SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
257 &moea_pvo_enter_calls, 0, "");
258 SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
259 &moea_pvo_remove_calls, 0, "");
260 SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
261 &moea_pte_spills, 0, "");
264 * Allocate physical memory for use in moea_bootstrap.
266 static vm_offset_t moea_bootstrap_alloc(vm_size_t, u_int);
271 static int moea_pte_insert(u_int, struct pte *);
276 static int moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
277 vm_offset_t, vm_offset_t, u_int, int);
278 static void moea_pvo_remove(struct pvo_entry *, int);
279 static struct pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
280 static struct pte *moea_pvo_to_pte(const struct pvo_entry *, int);
285 static void moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
286 vm_prot_t, boolean_t);
287 static void moea_syncicache(vm_offset_t, vm_size_t);
288 static boolean_t moea_query_bit(vm_page_t, int);
289 static u_int moea_clear_bit(vm_page_t, int, int *);
290 static void moea_kremove(mmu_t, vm_offset_t);
291 int moea_pte_spill(vm_offset_t);
294 * Kernel MMU interface
296 void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
297 void moea_clear_modify(mmu_t, vm_page_t);
298 void moea_clear_reference(mmu_t, vm_page_t);
299 void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
300 void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
301 void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
303 void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
304 vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
305 vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
306 void moea_init(mmu_t);
307 boolean_t moea_is_modified(mmu_t, vm_page_t);
308 boolean_t moea_ts_referenced(mmu_t, vm_page_t);
309 vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int);
310 boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
311 int moea_page_wired_mappings(mmu_t, vm_page_t);
312 void moea_pinit(mmu_t, pmap_t);
313 void moea_pinit0(mmu_t, pmap_t);
314 void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
315 void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
316 void moea_qremove(mmu_t, vm_offset_t, int);
317 void moea_release(mmu_t, pmap_t);
318 void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
319 void moea_remove_all(mmu_t, vm_page_t);
320 void moea_remove_write(mmu_t, vm_page_t);
321 void moea_zero_page(mmu_t, vm_page_t);
322 void moea_zero_page_area(mmu_t, vm_page_t, int, int);
323 void moea_zero_page_idle(mmu_t, vm_page_t);
324 void moea_activate(mmu_t, struct thread *);
325 void moea_deactivate(mmu_t, struct thread *);
326 void moea_cpu_bootstrap(mmu_t, int);
327 void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
328 void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t);
329 void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
330 vm_offset_t moea_kextract(mmu_t, vm_offset_t);
331 void moea_kenter(mmu_t, vm_offset_t, vm_offset_t);
332 boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
333 static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
335 static mmu_method_t moea_methods[] = {
336 MMUMETHOD(mmu_change_wiring, moea_change_wiring),
337 MMUMETHOD(mmu_clear_modify, moea_clear_modify),
338 MMUMETHOD(mmu_clear_reference, moea_clear_reference),
339 MMUMETHOD(mmu_copy_page, moea_copy_page),
340 MMUMETHOD(mmu_enter, moea_enter),
341 MMUMETHOD(mmu_enter_object, moea_enter_object),
342 MMUMETHOD(mmu_enter_quick, moea_enter_quick),
343 MMUMETHOD(mmu_extract, moea_extract),
344 MMUMETHOD(mmu_extract_and_hold, moea_extract_and_hold),
345 MMUMETHOD(mmu_init, moea_init),
346 MMUMETHOD(mmu_is_modified, moea_is_modified),
347 MMUMETHOD(mmu_ts_referenced, moea_ts_referenced),
348 MMUMETHOD(mmu_map, moea_map),
349 MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
350 MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
351 MMUMETHOD(mmu_pinit, moea_pinit),
352 MMUMETHOD(mmu_pinit0, moea_pinit0),
353 MMUMETHOD(mmu_protect, moea_protect),
354 MMUMETHOD(mmu_qenter, moea_qenter),
355 MMUMETHOD(mmu_qremove, moea_qremove),
356 MMUMETHOD(mmu_release, moea_release),
357 MMUMETHOD(mmu_remove, moea_remove),
358 MMUMETHOD(mmu_remove_all, moea_remove_all),
359 MMUMETHOD(mmu_remove_write, moea_remove_write),
360 MMUMETHOD(mmu_sync_icache, moea_sync_icache),
361 MMUMETHOD(mmu_zero_page, moea_zero_page),
362 MMUMETHOD(mmu_zero_page_area, moea_zero_page_area),
363 MMUMETHOD(mmu_zero_page_idle, moea_zero_page_idle),
364 MMUMETHOD(mmu_activate, moea_activate),
365 MMUMETHOD(mmu_deactivate, moea_deactivate),
367 /* Internal interfaces */
368 MMUMETHOD(mmu_bootstrap, moea_bootstrap),
369 MMUMETHOD(mmu_cpu_bootstrap, moea_cpu_bootstrap),
370 MMUMETHOD(mmu_mapdev, moea_mapdev),
371 MMUMETHOD(mmu_unmapdev, moea_unmapdev),
372 MMUMETHOD(mmu_kextract, moea_kextract),
373 MMUMETHOD(mmu_kenter, moea_kenter),
374 MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
379 static mmu_def_t oea_mmu = {
387 tlbie(vm_offset_t va)
390 mtx_lock_spin(&tlbie_mtx);
391 __asm __volatile("tlbie %0" :: "r"(va));
392 __asm __volatile("tlbsync");
394 mtx_unlock_spin(&tlbie_mtx);
402 for (va = 0; va < 0x00040000; va += 0x00001000) {
403 __asm __volatile("tlbie %0" :: "r"(va));
406 __asm __volatile("tlbsync");
411 va_to_sr(u_int *sr, vm_offset_t va)
413 return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
416 static __inline u_int
417 va_to_pteg(u_int sr, vm_offset_t addr)
421 hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
423 return (hash & moea_pteg_mask);
426 static __inline struct pvo_head *
427 pa_to_pvoh(vm_offset_t pa, vm_page_t *pg_p)
431 pg = PHYS_TO_VM_PAGE(pa);
437 return (&moea_pvo_unmanaged);
439 return (&pg->md.mdpg_pvoh);
442 static __inline struct pvo_head *
443 vm_page_to_pvoh(vm_page_t m)
446 return (&m->md.mdpg_pvoh);
450 moea_attr_clear(vm_page_t m, int ptebit)
453 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
454 m->md.mdpg_attrs &= ~ptebit;
458 moea_attr_fetch(vm_page_t m)
461 return (m->md.mdpg_attrs);
465 moea_attr_save(vm_page_t m, int ptebit)
468 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
469 m->md.mdpg_attrs |= ptebit;
473 moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
475 if (pt->pte_hi == pvo_pt->pte_hi)
482 moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
484 return (pt->pte_hi & ~PTE_VALID) ==
485 (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
486 ((va >> ADDR_API_SHFT) & PTE_API) | which);
490 moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
493 mtx_assert(&moea_table_mutex, MA_OWNED);
496 * Construct a PTE. Default to IMB initially. Valid bit only gets
497 * set when the real pte is set in memory.
499 * Note: Don't set the valid bit for correct operation of tlb update.
501 pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
502 (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
507 moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
510 mtx_assert(&moea_table_mutex, MA_OWNED);
511 pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
515 moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
518 mtx_assert(&moea_table_mutex, MA_OWNED);
521 * As shown in Section 7.6.3.2.3
523 pt->pte_lo &= ~ptebit;
528 moea_pte_set(struct pte *pt, struct pte *pvo_pt)
531 mtx_assert(&moea_table_mutex, MA_OWNED);
532 pvo_pt->pte_hi |= PTE_VALID;
535 * Update the PTE as defined in section 7.6.3.1.
536 * Note that the REF/CHG bits are from pvo_pt and thus should havce
537 * been saved so this routine can restore them (if desired).
539 pt->pte_lo = pvo_pt->pte_lo;
541 pt->pte_hi = pvo_pt->pte_hi;
547 moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
550 mtx_assert(&moea_table_mutex, MA_OWNED);
551 pvo_pt->pte_hi &= ~PTE_VALID;
554 * Force the reg & chg bits back into the PTEs.
559 * Invalidate the pte.
561 pt->pte_hi &= ~PTE_VALID;
566 * Save the reg & chg bits.
568 moea_pte_synch(pt, pvo_pt);
573 moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
579 moea_pte_unset(pt, pvo_pt, va);
580 moea_pte_set(pt, pvo_pt);
584 * Quick sort callout for comparing memory regions.
586 static int mr_cmp(const void *a, const void *b);
587 static int om_cmp(const void *a, const void *b);
590 mr_cmp(const void *a, const void *b)
592 const struct mem_region *regiona;
593 const struct mem_region *regionb;
597 if (regiona->mr_start < regionb->mr_start)
599 else if (regiona->mr_start > regionb->mr_start)
606 om_cmp(const void *a, const void *b)
608 const struct ofw_map *mapa;
609 const struct ofw_map *mapb;
613 if (mapa->om_pa < mapb->om_pa)
615 else if (mapa->om_pa > mapb->om_pa)
622 moea_cpu_bootstrap(mmu_t mmup, int ap)
629 __asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
630 __asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
632 __asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
633 __asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
637 __asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
638 __asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
641 __asm __volatile("mtibatu 1,%0" :: "r"(0));
642 __asm __volatile("mtdbatu 2,%0" :: "r"(0));
643 __asm __volatile("mtibatu 2,%0" :: "r"(0));
644 __asm __volatile("mtdbatu 3,%0" :: "r"(0));
645 __asm __volatile("mtibatu 3,%0" :: "r"(0));
648 for (i = 0; i < 16; i++)
649 mtsrin(i << ADDR_SR_SHFT, EMPTY_SEGMENT);
651 __asm __volatile("mtsr %0,%1" :: "n"(KERNEL_SR), "r"(KERNEL_SEGMENT));
652 __asm __volatile("mtsr %0,%1" :: "n"(KERNEL2_SR), "r"(KERNEL2_SEGMENT));
655 sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
656 __asm __volatile("mtsdr1 %0" :: "r"(sdr));
663 moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
666 phandle_t chosen, mmu;
670 vm_size_t size, physsz, hwphyssz;
671 vm_offset_t pa, va, off;
676 * Set up BAT0 to map the lowest 256 MB area
678 battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
679 battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
682 * Map PCI memory space.
684 battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
685 battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
687 battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
688 battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
690 battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
691 battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
693 battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
694 battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
699 battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
700 battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
703 * Use an IBAT and a DBAT to map the bottom segment of memory
704 * where we are. Turn off instruction relocation temporarily
705 * to prevent faults while reprogramming the IBAT.
708 mtmsr(msr & ~PSL_IR);
709 __asm (".balign 32; \n"
710 "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
711 "mtdbatu 0,%0; mtdbatl 0,%1; isync"
712 :: "r"(battable[0].batu), "r"(battable[0].batl));
716 __asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
717 __asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
720 /* set global direct map flag */
723 mem_regions(&pregions, &pregions_sz, ®ions, ®ions_sz);
724 CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
726 qsort(pregions, pregions_sz, sizeof(*pregions), mr_cmp);
727 for (i = 0; i < pregions_sz; i++) {
731 CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
732 pregions[i].mr_start,
733 pregions[i].mr_start + pregions[i].mr_size,
734 pregions[i].mr_size);
736 * Install entries into the BAT table to allow all
737 * of physmem to be convered by on-demand BAT entries.
738 * The loop will sometimes set the same battable element
739 * twice, but that's fine since they won't be used for
742 pa = pregions[i].mr_start & 0xf0000000;
743 end = pregions[i].mr_start + pregions[i].mr_size;
745 u_int n = pa >> ADDR_SR_SHFT;
747 battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
748 battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
749 pa += SEGMENT_LENGTH;
753 if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
754 panic("moea_bootstrap: phys_avail too small");
755 qsort(regions, regions_sz, sizeof(*regions), mr_cmp);
756 phys_avail_count = 0;
759 TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
760 for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
761 CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
762 regions[i].mr_start + regions[i].mr_size,
765 (physsz + regions[i].mr_size) >= hwphyssz) {
766 if (physsz < hwphyssz) {
767 phys_avail[j] = regions[i].mr_start;
768 phys_avail[j + 1] = regions[i].mr_start +
775 phys_avail[j] = regions[i].mr_start;
776 phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
778 physsz += regions[i].mr_size;
780 physmem = btoc(physsz);
783 * Allocate PTEG table.
786 moea_pteg_count = PTEGCOUNT;
788 moea_pteg_count = 0x1000;
790 while (moea_pteg_count < physmem)
791 moea_pteg_count <<= 1;
793 moea_pteg_count >>= 1;
794 #endif /* PTEGCOUNT */
796 size = moea_pteg_count * sizeof(struct pteg);
797 CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
799 moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
800 CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
801 bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
802 moea_pteg_mask = moea_pteg_count - 1;
805 * Allocate pv/overflow lists.
807 size = sizeof(struct pvo_head) * moea_pteg_count;
808 moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
810 CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
811 for (i = 0; i < moea_pteg_count; i++)
812 LIST_INIT(&moea_pvo_table[i]);
815 * Initialize the lock that synchronizes access to the pteg and pvo
818 mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
821 mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
824 * Initialise the unmanaged pvo pool.
826 moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
827 BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
828 moea_bpvo_pool_index = 0;
831 * Make sure kernel vsid is allocated as well as VSID 0.
833 moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
834 |= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
835 moea_vsid_bitmap[0] |= 1;
838 * Set up the Open Firmware pmap and add it's mappings.
840 moea_pinit(mmup, &ofw_pmap);
841 ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
842 ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
843 if ((chosen = OF_finddevice("/chosen")) == -1)
844 panic("moea_bootstrap: can't find /chosen");
845 OF_getprop(chosen, "mmu", &mmui, 4);
846 if ((mmu = OF_instance_to_package(mmui)) == -1)
847 panic("moea_bootstrap: can't get mmu package");
848 if ((sz = OF_getproplen(mmu, "translations")) == -1)
849 panic("moea_bootstrap: can't get ofw translation count");
851 for (i = 0; phys_avail[i] != 0; i += 2) {
852 if (phys_avail[i + 1] >= sz) {
853 translations = (struct ofw_map *)phys_avail[i];
857 if (translations == NULL)
858 panic("moea_bootstrap: no space to copy translations");
859 bzero(translations, sz);
860 if (OF_getprop(mmu, "translations", translations, sz) == -1)
861 panic("moea_bootstrap: can't get ofw translations");
862 CTR0(KTR_PMAP, "moea_bootstrap: translations");
863 sz /= sizeof(*translations);
864 qsort(translations, sz, sizeof (*translations), om_cmp);
865 for (i = 0, ofw_mappings = 0; i < sz; i++) {
866 CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
867 translations[i].om_pa, translations[i].om_va,
868 translations[i].om_len);
871 * If the mapping is 1:1, let the RAM and device on-demand
872 * BAT tables take care of the translation.
874 if (translations[i].om_va == translations[i].om_pa)
877 /* Enter the pages */
878 for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) {
881 m.phys_addr = translations[i].om_pa + off;
882 PMAP_LOCK(&ofw_pmap);
883 moea_enter_locked(&ofw_pmap,
884 translations[i].om_va + off, &m,
886 PMAP_UNLOCK(&ofw_pmap);
892 * Calculate the last available physical address.
894 for (i = 0; phys_avail[i + 2] != 0; i += 2)
896 Maxmem = powerpc_btop(phys_avail[i + 1]);
899 * Initialize the kernel pmap (which is statically allocated).
901 PMAP_LOCK_INIT(kernel_pmap);
902 for (i = 0; i < 16; i++) {
903 kernel_pmap->pm_sr[i] = EMPTY_SEGMENT;
905 kernel_pmap->pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
906 kernel_pmap->pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
907 kernel_pmap->pm_active = ~0;
909 moea_cpu_bootstrap(mmup,0);
914 * Set the start and end of kva.
916 virtual_avail = VM_MIN_KERNEL_ADDRESS;
917 virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
920 * Allocate a kernel stack with a guard page for thread0 and map it
921 * into the kernel page map.
923 pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
924 va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
925 virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
926 CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
927 thread0.td_kstack = va;
928 thread0.td_kstack_pages = KSTACK_PAGES;
929 for (i = 0; i < KSTACK_PAGES; i++) {
930 moea_kenter(mmup, va, pa);
936 * Allocate virtual address space for the message buffer.
938 pa = msgbuf_phys = moea_bootstrap_alloc(MSGBUF_SIZE, PAGE_SIZE);
939 msgbufp = (struct msgbuf *)virtual_avail;
941 virtual_avail += round_page(MSGBUF_SIZE);
942 while (va < virtual_avail) {
943 moea_kenter(mmup, va, pa);
949 * Allocate virtual address space for the dynamic percpu area.
951 pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
952 dpcpu = (void *)virtual_avail;
954 virtual_avail += DPCPU_SIZE;
955 while (va < virtual_avail) {
956 moea_kenter(mmup, va, pa);
960 dpcpu_init(dpcpu, 0);
964 * Activate a user pmap. The pmap must be activated before it's address
965 * space can be accessed in any way.
968 moea_activate(mmu_t mmu, struct thread *td)
973 * Load all the data we need up front to encourage the compiler to
974 * not issue any loads while we have interrupts disabled below.
976 pm = &td->td_proc->p_vmspace->vm_pmap;
979 pm->pm_active |= PCPU_GET(cpumask);
980 PCPU_SET(curpmap, pmr);
984 moea_deactivate(mmu_t mmu, struct thread *td)
988 pm = &td->td_proc->p_vmspace->vm_pmap;
989 pm->pm_active &= ~PCPU_GET(cpumask);
990 PCPU_SET(curpmap, NULL);
994 moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
996 struct pvo_entry *pvo;
999 pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
1003 if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
1004 pm->pm_stats.wired_count++;
1005 pvo->pvo_vaddr |= PVO_WIRED;
1007 if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
1008 pm->pm_stats.wired_count--;
1009 pvo->pvo_vaddr &= ~PVO_WIRED;
1016 moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
1021 dst = VM_PAGE_TO_PHYS(mdst);
1022 src = VM_PAGE_TO_PHYS(msrc);
1024 kcopy((void *)src, (void *)dst, PAGE_SIZE);
1028 * Zero a page of physical memory by temporarily mapping it into the tlb.
1031 moea_zero_page(mmu_t mmu, vm_page_t m)
1033 vm_offset_t pa = VM_PAGE_TO_PHYS(m);
1034 void *va = (void *)pa;
1036 bzero(va, PAGE_SIZE);
1040 moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
1042 vm_offset_t pa = VM_PAGE_TO_PHYS(m);
1043 void *va = (void *)(pa + off);
1049 moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1051 vm_offset_t pa = VM_PAGE_TO_PHYS(m);
1052 void *va = (void *)pa;
1054 bzero(va, PAGE_SIZE);
1058 * Map the given physical page at the specified virtual address in the
1059 * target pmap with the protection requested. If specified the page
1060 * will be wired down.
1063 moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1067 vm_page_lock_queues();
1069 moea_enter_locked(pmap, va, m, prot, wired);
1070 vm_page_unlock_queues();
1075 * Map the given physical page at the specified virtual address in the
1076 * target pmap with the protection requested. If specified the page
1077 * will be wired down.
1079 * The page queues and pmap must be locked.
1082 moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1085 struct pvo_head *pvo_head;
1088 u_int pte_lo, pvo_flags, was_exec, i;
1091 if (!moea_initialized) {
1092 pvo_head = &moea_pvo_kunmanaged;
1093 zone = moea_upvo_zone;
1096 was_exec = PTE_EXEC;
1098 pvo_head = vm_page_to_pvoh(m);
1100 zone = moea_mpvo_zone;
1101 pvo_flags = PVO_MANAGED;
1104 if (pmap_bootstrapped)
1105 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1106 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1108 /* XXX change the pvo head for fake pages */
1109 if ((m->flags & PG_FICTITIOUS) == PG_FICTITIOUS) {
1110 pvo_flags &= ~PVO_MANAGED;
1111 pvo_head = &moea_pvo_kunmanaged;
1112 zone = moea_upvo_zone;
1116 * If this is a managed page, and it's the first reference to the page,
1117 * clear the execness of the page. Otherwise fetch the execness.
1119 if ((pg != NULL) && ((m->flags & PG_FICTITIOUS) == 0)) {
1120 if (LIST_EMPTY(pvo_head)) {
1121 moea_attr_clear(pg, PTE_EXEC);
1123 was_exec = moea_attr_fetch(pg) & PTE_EXEC;
1128 * Assume the page is cache inhibited and access is guarded unless
1129 * it's in our available memory array.
1131 pte_lo = PTE_I | PTE_G;
1132 for (i = 0; i < pregions_sz; i++) {
1133 if ((VM_PAGE_TO_PHYS(m) >= pregions[i].mr_start) &&
1134 (VM_PAGE_TO_PHYS(m) <
1135 (pregions[i].mr_start + pregions[i].mr_size))) {
1141 if (prot & VM_PROT_WRITE) {
1143 if (pmap_bootstrapped)
1144 vm_page_flag_set(m, PG_WRITEABLE);
1148 if (prot & VM_PROT_EXECUTE)
1149 pvo_flags |= PVO_EXECUTABLE;
1152 pvo_flags |= PVO_WIRED;
1154 if ((m->flags & PG_FICTITIOUS) != 0)
1155 pvo_flags |= PVO_FAKE;
1157 error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
1161 * Flush the real page from the instruction cache if this page is
1162 * mapped executable and cacheable and was not previously mapped (or
1163 * was not mapped executable).
1165 if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
1166 (pte_lo & PTE_I) == 0 && was_exec == 0) {
1168 * Flush the real memory from the cache.
1170 moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1172 moea_attr_save(pg, PTE_EXEC);
1175 /* XXX syncicache always until problems are sorted */
1176 moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1180 * Maps a sequence of resident pages belonging to the same object.
1181 * The sequence begins with the given page m_start. This page is
1182 * mapped at the given virtual address start. Each subsequent page is
1183 * mapped at a virtual address that is offset from start by the same
1184 * amount as the page is offset from m_start within the object. The
1185 * last page in the sequence is the page with the largest offset from
1186 * m_start that can be mapped at a virtual address less than the given
1187 * virtual address end. Not every virtual page between start and end
1188 * is mapped; only those for which a resident page exists with the
1189 * corresponding offset from m_start are mapped.
1192 moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1193 vm_page_t m_start, vm_prot_t prot)
1196 vm_pindex_t diff, psize;
1198 psize = atop(end - start);
1201 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1202 moea_enter_locked(pm, start + ptoa(diff), m, prot &
1203 (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1204 m = TAILQ_NEXT(m, listq);
1210 moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
1215 moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1222 moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
1224 struct pvo_entry *pvo;
1228 pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
1232 pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
1238 * Atomically extract and hold the physical page with the given
1239 * pmap and virtual address pair if that mapping permits the given
1243 moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1245 struct pvo_entry *pvo;
1249 vm_page_lock_queues();
1251 pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1252 if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
1253 ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1254 (prot & VM_PROT_WRITE) == 0)) {
1255 m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
1258 vm_page_unlock_queues();
1264 moea_init(mmu_t mmu)
1267 moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
1268 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1269 UMA_ZONE_VM | UMA_ZONE_NOFREE);
1270 moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
1271 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1272 UMA_ZONE_VM | UMA_ZONE_NOFREE);
1273 moea_initialized = TRUE;
1277 moea_is_modified(mmu_t mmu, vm_page_t m)
1280 if ((m->flags & (PG_FICTITIOUS |PG_UNMANAGED)) != 0)
1283 return (moea_query_bit(m, PTE_CHG));
1287 moea_clear_reference(mmu_t mmu, vm_page_t m)
1290 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1292 moea_clear_bit(m, PTE_REF, NULL);
1296 moea_clear_modify(mmu_t mmu, vm_page_t m)
1299 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1301 moea_clear_bit(m, PTE_CHG, NULL);
1305 * Clear the write and modified bits in each of the given page's mappings.
1308 moea_remove_write(mmu_t mmu, vm_page_t m)
1310 struct pvo_entry *pvo;
1315 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1316 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
1317 (m->flags & PG_WRITEABLE) == 0)
1319 lo = moea_attr_fetch(m);
1321 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1322 pmap = pvo->pvo_pmap;
1324 if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
1325 pt = moea_pvo_to_pte(pvo, -1);
1326 pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
1327 pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1329 moea_pte_synch(pt, &pvo->pvo_pte.pte);
1330 lo |= pvo->pvo_pte.pte.pte_lo;
1331 pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
1332 moea_pte_change(pt, &pvo->pvo_pte.pte,
1334 mtx_unlock(&moea_table_mutex);
1339 if ((lo & PTE_CHG) != 0) {
1340 moea_attr_clear(m, PTE_CHG);
1343 vm_page_flag_clear(m, PG_WRITEABLE);
1347 * moea_ts_referenced:
1349 * Return a count of reference bits for a page, clearing those bits.
1350 * It is not necessary for every reference bit to be cleared, but it
1351 * is necessary that 0 only be returned when there are truly no
1352 * reference bits set.
1354 * XXX: The exact number of bits to check and clear is a matter that
1355 * should be tested and standardized at some point in the future for
1356 * optimal aging of shared pages.
1359 moea_ts_referenced(mmu_t mmu, vm_page_t m)
1363 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
1366 count = moea_clear_bit(m, PTE_REF, NULL);
1372 * Map a wired page into kernel virtual address space.
1375 moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
1382 if (va < VM_MIN_KERNEL_ADDRESS)
1383 panic("moea_kenter: attempt to enter non-kernel address %#x",
1387 pte_lo = PTE_I | PTE_G;
1388 for (i = 0; i < pregions_sz; i++) {
1389 if ((pa >= pregions[i].mr_start) &&
1390 (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
1396 PMAP_LOCK(kernel_pmap);
1397 error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
1398 &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
1400 if (error != 0 && error != ENOENT)
1401 panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
1405 * Flush the real memory from the instruction cache.
1407 if ((pte_lo & (PTE_I | PTE_G)) == 0) {
1408 moea_syncicache(pa, PAGE_SIZE);
1410 PMAP_UNLOCK(kernel_pmap);
1414 * Extract the physical page address associated with the given kernel virtual
1418 moea_kextract(mmu_t mmu, vm_offset_t va)
1420 struct pvo_entry *pvo;
1424 * Allow direct mappings on 32-bit OEA
1426 if (va < VM_MIN_KERNEL_ADDRESS) {
1430 PMAP_LOCK(kernel_pmap);
1431 pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
1432 KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
1433 pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
1434 PMAP_UNLOCK(kernel_pmap);
1439 * Remove a wired page from kernel virtual address space.
1442 moea_kremove(mmu_t mmu, vm_offset_t va)
1445 moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
1449 * Map a range of physical addresses into kernel virtual address space.
1451 * The value passed in *virt is a suggested virtual address for the mapping.
1452 * Architectures which can support a direct-mapped physical to virtual region
1453 * can return the appropriate address within that region, leaving '*virt'
1454 * unchanged. We cannot and therefore do not; *virt is updated with the
1455 * first usable address after the mapped region.
1458 moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
1459 vm_offset_t pa_end, int prot)
1461 vm_offset_t sva, va;
1465 for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
1466 moea_kenter(mmu, va, pa_start);
1472 * Returns true if the pmap's pv is one of the first
1473 * 16 pvs linked to from this page. This count may
1474 * be changed upwards or downwards in the future; it
1475 * is only necessary that true be returned for a small
1476 * subset of pmaps for proper page aging.
1479 moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
1482 struct pvo_entry *pvo;
1484 if (!moea_initialized || (m->flags & PG_FICTITIOUS))
1488 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1489 if (pvo->pvo_pmap == pmap)
1499 * Return the number of managed mappings to the given physical page
1503 moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
1505 struct pvo_entry *pvo;
1509 if (!moea_initialized || (m->flags & PG_FICTITIOUS) != 0)
1511 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1512 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
1513 if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
1518 static u_int moea_vsidcontext;
1521 moea_pinit(mmu_t mmu, pmap_t pmap)
1526 KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
1527 PMAP_LOCK_INIT(pmap);
1530 __asm __volatile("mftb %0" : "=r"(entropy));
1532 if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
1534 pmap->pmap_phys = pmap;
1539 * Allocate some segment registers for this pmap.
1541 for (i = 0; i < NPMAPS; i += VSID_NBPW) {
1545 * Create a new value by mutiplying by a prime and adding in
1546 * entropy from the timebase register. This is to make the
1547 * VSID more random so that the PT hash function collides
1548 * less often. (Note that the prime casues gcc to do shifts
1549 * instead of a multiply.)
1551 moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
1552 hash = moea_vsidcontext & (NPMAPS - 1);
1553 if (hash == 0) /* 0 is special, avoid it */
1556 mask = 1 << (hash & (VSID_NBPW - 1));
1557 hash = (moea_vsidcontext & 0xfffff);
1558 if (moea_vsid_bitmap[n] & mask) { /* collision? */
1559 /* anything free in this bucket? */
1560 if (moea_vsid_bitmap[n] == 0xffffffff) {
1561 entropy = (moea_vsidcontext >> 20);
1564 i = ffs(~moea_vsid_bitmap[i]) - 1;
1566 hash &= 0xfffff & ~(VSID_NBPW - 1);
1569 moea_vsid_bitmap[n] |= mask;
1570 for (i = 0; i < 16; i++)
1571 pmap->pm_sr[i] = VSID_MAKE(i, hash);
1575 panic("moea_pinit: out of segments");
1579 * Initialize the pmap associated with process 0.
1582 moea_pinit0(mmu_t mmu, pmap_t pm)
1585 moea_pinit(mmu, pm);
1586 bzero(&pm->pm_stats, sizeof(pm->pm_stats));
1590 * Set the physical protection on the specified range of this map as requested.
1593 moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
1596 struct pvo_entry *pvo;
1600 KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
1601 ("moea_protect: non current pmap"));
1603 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1604 moea_remove(mmu, pm, sva, eva);
1608 vm_page_lock_queues();
1610 for (; sva < eva; sva += PAGE_SIZE) {
1611 pvo = moea_pvo_find_va(pm, sva, &pteidx);
1615 if ((prot & VM_PROT_EXECUTE) == 0)
1616 pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1619 * Grab the PTE pointer before we diddle with the cached PTE
1622 pt = moea_pvo_to_pte(pvo, pteidx);
1624 * Change the protection of the page.
1626 pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
1627 pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1630 * If the PVO is in the page table, update that pte as well.
1633 moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1634 mtx_unlock(&moea_table_mutex);
1637 vm_page_unlock_queues();
1642 * Map a list of wired pages into kernel virtual address space. This is
1643 * intended for temporary mappings which do not need page modification or
1644 * references recorded. Existing mappings in the region are overwritten.
1647 moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
1652 while (count-- > 0) {
1653 moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
1660 * Remove page mappings from kernel virtual address space. Intended for
1661 * temporary mappings entered by moea_qenter.
1664 moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
1669 while (count-- > 0) {
1670 moea_kremove(mmu, va);
1676 moea_release(mmu_t mmu, pmap_t pmap)
1681 * Free segment register's VSID
1683 if (pmap->pm_sr[0] == 0)
1684 panic("moea_release");
1686 idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
1687 mask = 1 << (idx % VSID_NBPW);
1689 moea_vsid_bitmap[idx] &= ~mask;
1690 PMAP_LOCK_DESTROY(pmap);
1694 * Remove the given range of addresses from the specified map.
1697 moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1699 struct pvo_entry *pvo;
1702 vm_page_lock_queues();
1704 for (; sva < eva; sva += PAGE_SIZE) {
1705 pvo = moea_pvo_find_va(pm, sva, &pteidx);
1707 moea_pvo_remove(pvo, pteidx);
1711 vm_page_unlock_queues();
1715 * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
1716 * will reflect changes in pte's back to the vm_page.
1719 moea_remove_all(mmu_t mmu, vm_page_t m)
1721 struct pvo_head *pvo_head;
1722 struct pvo_entry *pvo, *next_pvo;
1725 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1727 pvo_head = vm_page_to_pvoh(m);
1728 for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
1729 next_pvo = LIST_NEXT(pvo, pvo_vlink);
1731 MOEA_PVO_CHECK(pvo); /* sanity check */
1732 pmap = pvo->pvo_pmap;
1734 moea_pvo_remove(pvo, -1);
1737 if ((m->flags & PG_WRITEABLE) && moea_is_modified(mmu, m)) {
1738 moea_attr_clear(m, LPTE_CHG);
1741 vm_page_flag_clear(m, PG_WRITEABLE);
1745 * Allocate a physical page of memory directly from the phys_avail map.
1746 * Can only be called from moea_bootstrap before avail start and end are
1750 moea_bootstrap_alloc(vm_size_t size, u_int align)
1755 size = round_page(size);
1756 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
1758 s = (phys_avail[i] + align - 1) & ~(align - 1);
1763 if (s < phys_avail[i] || e > phys_avail[i + 1])
1766 if (s == phys_avail[i]) {
1767 phys_avail[i] += size;
1768 } else if (e == phys_avail[i + 1]) {
1769 phys_avail[i + 1] -= size;
1771 for (j = phys_avail_count * 2; j > i; j -= 2) {
1772 phys_avail[j] = phys_avail[j - 2];
1773 phys_avail[j + 1] = phys_avail[j - 1];
1776 phys_avail[i + 3] = phys_avail[i + 1];
1777 phys_avail[i + 1] = s;
1778 phys_avail[i + 2] = e;
1784 panic("moea_bootstrap_alloc: could not allocate memory");
1788 moea_syncicache(vm_offset_t pa, vm_size_t len)
1790 __syncicache((void *)pa, len);
1794 moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
1795 vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
1797 struct pvo_entry *pvo;
1804 moea_pvo_enter_calls++;
1809 * Compute the PTE Group index.
1812 sr = va_to_sr(pm->pm_sr, va);
1813 ptegidx = va_to_pteg(sr, va);
1816 * Remove any existing mapping for this page. Reuse the pvo entry if
1817 * there is a mapping.
1819 mtx_lock(&moea_table_mutex);
1820 LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
1821 if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
1822 if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
1823 (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1824 (pte_lo & PTE_PP)) {
1825 mtx_unlock(&moea_table_mutex);
1828 moea_pvo_remove(pvo, -1);
1834 * If we aren't overwriting a mapping, try to allocate.
1836 if (moea_initialized) {
1837 pvo = uma_zalloc(zone, M_NOWAIT);
1839 if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
1840 panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
1841 moea_bpvo_pool_index, BPVO_POOL_SIZE,
1842 BPVO_POOL_SIZE * sizeof(struct pvo_entry));
1844 pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
1845 moea_bpvo_pool_index++;
1850 mtx_unlock(&moea_table_mutex);
1855 pvo->pvo_vaddr = va;
1857 LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
1858 pvo->pvo_vaddr &= ~ADDR_POFF;
1859 if (flags & VM_PROT_EXECUTE)
1860 pvo->pvo_vaddr |= PVO_EXECUTABLE;
1861 if (flags & PVO_WIRED)
1862 pvo->pvo_vaddr |= PVO_WIRED;
1863 if (pvo_head != &moea_pvo_kunmanaged)
1864 pvo->pvo_vaddr |= PVO_MANAGED;
1866 pvo->pvo_vaddr |= PVO_BOOTSTRAP;
1867 if (flags & PVO_FAKE)
1868 pvo->pvo_vaddr |= PVO_FAKE;
1870 moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
1873 * Remember if the list was empty and therefore will be the first
1876 if (LIST_FIRST(pvo_head) == NULL)
1878 LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
1880 if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
1881 pm->pm_stats.wired_count++;
1882 pm->pm_stats.resident_count++;
1885 * We hope this succeeds but it isn't required.
1887 i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
1889 PVO_PTEGIDX_SET(pvo, i);
1891 panic("moea_pvo_enter: overflow");
1892 moea_pte_overflow++;
1894 mtx_unlock(&moea_table_mutex);
1896 return (first ? ENOENT : 0);
1900 moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
1905 * If there is an active pte entry, we need to deactivate it (and
1906 * save the ref & cfg bits).
1908 pt = moea_pvo_to_pte(pvo, pteidx);
1910 moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1911 mtx_unlock(&moea_table_mutex);
1912 PVO_PTEGIDX_CLR(pvo);
1914 moea_pte_overflow--;
1918 * Update our statistics.
1920 pvo->pvo_pmap->pm_stats.resident_count--;
1921 if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
1922 pvo->pvo_pmap->pm_stats.wired_count--;
1925 * Save the REF/CHG bits into their cache if the page is managed.
1927 if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) {
1930 pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
1932 moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
1933 (PTE_REF | PTE_CHG));
1938 * Remove this PVO from the PV list.
1940 LIST_REMOVE(pvo, pvo_vlink);
1943 * Remove this from the overflow list and return it to the pool
1944 * if we aren't going to reuse it.
1946 LIST_REMOVE(pvo, pvo_olink);
1947 if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
1948 uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
1949 moea_upvo_zone, pvo);
1951 moea_pvo_remove_calls++;
1955 moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
1960 * We can find the actual pte entry without searching by grabbing
1961 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
1962 * noticing the HID bit.
1964 pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
1965 if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
1966 pteidx ^= moea_pteg_mask * 8;
1971 static struct pvo_entry *
1972 moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
1974 struct pvo_entry *pvo;
1979 sr = va_to_sr(pm->pm_sr, va);
1980 ptegidx = va_to_pteg(sr, va);
1982 mtx_lock(&moea_table_mutex);
1983 LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
1984 if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
1986 *pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
1990 mtx_unlock(&moea_table_mutex);
1996 moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
2001 * If we haven't been supplied the ptegidx, calculate it.
2007 sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
2008 ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
2009 pteidx = moea_pvo_pte_index(pvo, ptegidx);
2012 pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2013 mtx_lock(&moea_table_mutex);
2015 if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
2016 panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
2017 "valid pte index", pvo);
2020 if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
2021 panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
2022 "pvo but no valid pte", pvo);
2025 if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
2026 if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
2027 panic("moea_pvo_to_pte: pvo %p has valid pte in "
2028 "moea_pteg_table %p but invalid in pvo", pvo, pt);
2031 if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
2033 panic("moea_pvo_to_pte: pvo %p pte does not match "
2034 "pte %p in moea_pteg_table", pvo, pt);
2037 mtx_assert(&moea_table_mutex, MA_OWNED);
2041 if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
2042 panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
2043 "moea_pteg_table but valid in pvo", pvo, pt);
2046 mtx_unlock(&moea_table_mutex);
2051 * XXX: THIS STUFF SHOULD BE IN pte.c?
2054 moea_pte_spill(vm_offset_t addr)
2056 struct pvo_entry *source_pvo, *victim_pvo;
2057 struct pvo_entry *pvo;
2066 ptegidx = va_to_pteg(sr, addr);
2069 * Have to substitute some entry. Use the primary hash for this.
2070 * Use low bits of timebase as random generator.
2072 pteg = &moea_pteg_table[ptegidx];
2073 mtx_lock(&moea_table_mutex);
2074 __asm __volatile("mftb %0" : "=r"(i));
2080 LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
2082 * We need to find a pvo entry for this address.
2084 MOEA_PVO_CHECK(pvo);
2085 if (source_pvo == NULL &&
2086 moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
2087 pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
2089 * Now found an entry to be spilled into the pteg.
2090 * The PTE is now valid, so we know it's active.
2092 j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
2095 PVO_PTEGIDX_SET(pvo, j);
2096 moea_pte_overflow--;
2097 MOEA_PVO_CHECK(pvo);
2098 mtx_unlock(&moea_table_mutex);
2104 if (victim_pvo != NULL)
2109 * We also need the pvo entry of the victim we are replacing
2110 * so save the R & C bits of the PTE.
2112 if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
2113 moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
2115 if (source_pvo != NULL)
2120 if (source_pvo == NULL) {
2121 mtx_unlock(&moea_table_mutex);
2125 if (victim_pvo == NULL) {
2126 if ((pt->pte_hi & PTE_HID) == 0)
2127 panic("moea_pte_spill: victim p-pte (%p) has no pvo"
2131 * If this is a secondary PTE, we need to search it's primary
2132 * pvo bucket for the matching PVO.
2134 LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
2136 MOEA_PVO_CHECK(pvo);
2138 * We also need the pvo entry of the victim we are
2139 * replacing so save the R & C bits of the PTE.
2141 if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
2147 if (victim_pvo == NULL)
2148 panic("moea_pte_spill: victim s-pte (%p) has no pvo"
2153 * We are invalidating the TLB entry for the EA we are replacing even
2154 * though it's valid. If we don't, we lose any ref/chg bit changes
2155 * contained in the TLB entry.
2157 source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
2159 moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
2160 moea_pte_set(pt, &source_pvo->pvo_pte.pte);
2162 PVO_PTEGIDX_CLR(victim_pvo);
2163 PVO_PTEGIDX_SET(source_pvo, i);
2164 moea_pte_replacements++;
2166 MOEA_PVO_CHECK(victim_pvo);
2167 MOEA_PVO_CHECK(source_pvo);
2169 mtx_unlock(&moea_table_mutex);
2174 moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
2179 mtx_assert(&moea_table_mutex, MA_OWNED);
2182 * First try primary hash.
2184 for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
2185 if ((pt->pte_hi & PTE_VALID) == 0) {
2186 pvo_pt->pte_hi &= ~PTE_HID;
2187 moea_pte_set(pt, pvo_pt);
2193 * Now try secondary hash.
2195 ptegidx ^= moea_pteg_mask;
2197 for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
2198 if ((pt->pte_hi & PTE_VALID) == 0) {
2199 pvo_pt->pte_hi |= PTE_HID;
2200 moea_pte_set(pt, pvo_pt);
2205 panic("moea_pte_insert: overflow");
2210 moea_query_bit(vm_page_t m, int ptebit)
2212 struct pvo_entry *pvo;
2215 if (moea_attr_fetch(m) & ptebit)
2218 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2219 MOEA_PVO_CHECK(pvo); /* sanity check */
2222 * See if we saved the bit off. If so, cache it and return
2225 if (pvo->pvo_pte.pte.pte_lo & ptebit) {
2226 moea_attr_save(m, ptebit);
2227 MOEA_PVO_CHECK(pvo); /* sanity check */
2233 * No luck, now go through the hard part of looking at the PTEs
2234 * themselves. Sync so that any pending REF/CHG bits are flushed to
2238 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2239 MOEA_PVO_CHECK(pvo); /* sanity check */
2242 * See if this pvo has a valid PTE. if so, fetch the
2243 * REF/CHG bits from the valid PTE. If the appropriate
2244 * ptebit is set, cache it and return success.
2246 pt = moea_pvo_to_pte(pvo, -1);
2248 moea_pte_synch(pt, &pvo->pvo_pte.pte);
2249 mtx_unlock(&moea_table_mutex);
2250 if (pvo->pvo_pte.pte.pte_lo & ptebit) {
2251 moea_attr_save(m, ptebit);
2252 MOEA_PVO_CHECK(pvo); /* sanity check */
2262 moea_clear_bit(vm_page_t m, int ptebit, int *origbit)
2265 struct pvo_entry *pvo;
2270 * Clear the cached value.
2272 rv = moea_attr_fetch(m);
2273 moea_attr_clear(m, ptebit);
2276 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
2277 * we can reset the right ones). note that since the pvo entries and
2278 * list heads are accessed via BAT0 and are never placed in the page
2279 * table, we don't have to worry about further accesses setting the
2285 * For each pvo entry, clear the pvo's ptebit. If this pvo has a
2286 * valid pte clear the ptebit from the valid pte.
2289 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2290 MOEA_PVO_CHECK(pvo); /* sanity check */
2291 pt = moea_pvo_to_pte(pvo, -1);
2293 moea_pte_synch(pt, &pvo->pvo_pte.pte);
2294 if (pvo->pvo_pte.pte.pte_lo & ptebit) {
2296 moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
2298 mtx_unlock(&moea_table_mutex);
2300 rv |= pvo->pvo_pte.pte.pte_lo;
2301 pvo->pvo_pte.pte.pte_lo &= ~ptebit;
2302 MOEA_PVO_CHECK(pvo); /* sanity check */
2305 if (origbit != NULL) {
2313 * Return true if the physical range is encompassed by the battable[idx]
2316 moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
2324 * Return immediately if not a valid mapping
2326 if (!battable[idx].batu & BAT_Vs)
2330 * The BAT entry must be cache-inhibited, guarded, and r/w
2331 * so it can function as an i/o page
2333 prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
2334 if (prot != (BAT_I|BAT_G|BAT_PP_RW))
2338 * The address should be within the BAT range. Assume that the
2339 * start address in the BAT has the correct alignment (thus
2340 * not requiring masking)
2342 start = battable[idx].batl & BAT_PBS;
2343 bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
2344 end = start | (bat_ble << 15) | 0x7fff;
2346 if ((pa < start) || ((pa + size) > end))
2353 moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2358 * This currently does not work for entries that
2359 * overlap 256M BAT segments.
2362 for(i = 0; i < 16; i++)
2363 if (moea_bat_mapped(i, pa, size) == 0)
2370 * Map a set of physical memory pages into the kernel virtual
2371 * address space. Return a pointer to where it is mapped. This
2372 * routine is intended to be used for mapping device memory,
2376 moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2378 vm_offset_t va, tmpva, ppa, offset;
2381 ppa = trunc_page(pa);
2382 offset = pa & PAGE_MASK;
2383 size = roundup(offset + size, PAGE_SIZE);
2388 * If the physical address lies within a valid BAT table entry,
2389 * return the 1:1 mapping. This currently doesn't work
2390 * for regions that overlap 256M BAT segments.
2392 for (i = 0; i < 16; i++) {
2393 if (moea_bat_mapped(i, pa, size) == 0)
2394 return ((void *) pa);
2397 va = kmem_alloc_nofault(kernel_map, size);
2399 panic("moea_mapdev: Couldn't alloc kernel virtual memory");
2401 for (tmpva = va; size > 0;) {
2402 moea_kenter(mmu, tmpva, ppa);
2409 return ((void *)(va + offset));
2413 moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
2415 vm_offset_t base, offset;
2418 * If this is outside kernel virtual space, then it's a
2419 * battable entry and doesn't require unmapping
2421 if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
2422 base = trunc_page(va);
2423 offset = va & PAGE_MASK;
2424 size = roundup(offset + size, PAGE_SIZE);
2425 kmem_free(kernel_map, base, size);
2430 moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
2432 struct pvo_entry *pvo;
2439 lim = round_page(va);
2440 len = MIN(lim - va, sz);
2441 pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
2443 pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
2445 moea_syncicache(pa, len);