xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision f26bcf99e0fd3ad05e31eea6fb2926484d70cafe)
160727d8bSWarner Losh /*-
25244eac9SBenno Rice  * Copyright (c) 2001 The NetBSD Foundation, Inc.
35244eac9SBenno Rice  * All rights reserved.
45244eac9SBenno Rice  *
55244eac9SBenno Rice  * This code is derived from software contributed to The NetBSD Foundation
65244eac9SBenno Rice  * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
75244eac9SBenno Rice  *
85244eac9SBenno Rice  * Redistribution and use in source and binary forms, with or without
95244eac9SBenno Rice  * modification, are permitted provided that the following conditions
105244eac9SBenno Rice  * are met:
115244eac9SBenno Rice  * 1. Redistributions of source code must retain the above copyright
125244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer.
135244eac9SBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
145244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer in the
155244eac9SBenno Rice  *    documentation and/or other materials provided with the distribution.
165244eac9SBenno Rice  *
175244eac9SBenno Rice  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
185244eac9SBenno Rice  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
195244eac9SBenno Rice  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
205244eac9SBenno Rice  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
215244eac9SBenno Rice  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
225244eac9SBenno Rice  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
235244eac9SBenno Rice  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245244eac9SBenno Rice  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
255244eac9SBenno Rice  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265244eac9SBenno Rice  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
275244eac9SBenno Rice  * POSSIBILITY OF SUCH DAMAGE.
285244eac9SBenno Rice  */
2960727d8bSWarner Losh /*-
30f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
31f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 TooLs GmbH.
32f9bac91bSBenno Rice  * All rights reserved.
33f9bac91bSBenno Rice  *
34f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
35f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
36f9bac91bSBenno Rice  * are met:
37f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
38f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
39f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
40f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
41f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
42f9bac91bSBenno Rice  * 3. All advertising materials mentioning features or use of this software
43f9bac91bSBenno Rice  *    must display the following acknowledgement:
44f9bac91bSBenno Rice  *	This product includes software developed by TooLs GmbH.
45f9bac91bSBenno Rice  * 4. The name of TooLs GmbH may not be used to endorse or promote products
46f9bac91bSBenno Rice  *    derived from this software without specific prior written permission.
47f9bac91bSBenno Rice  *
48f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
49f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
53f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
54f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
55f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
56f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
57f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58f9bac91bSBenno Rice  *
59111c77dcSBenno Rice  * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
60f9bac91bSBenno Rice  */
6160727d8bSWarner Losh /*-
62f9bac91bSBenno Rice  * Copyright (C) 2001 Benno Rice.
63f9bac91bSBenno Rice  * All rights reserved.
64f9bac91bSBenno Rice  *
65f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
66f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
67f9bac91bSBenno Rice  * are met:
68f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
69f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
70f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
71f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
72f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
73f9bac91bSBenno Rice  *
74f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
75f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
77f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
79f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
80f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
81f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
82f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
83f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
84f9bac91bSBenno Rice  */
85f9bac91bSBenno Rice 
868368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
878368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
88f9bac91bSBenno Rice 
895244eac9SBenno Rice /*
905244eac9SBenno Rice  * Manages physical address maps.
915244eac9SBenno Rice  *
925244eac9SBenno Rice  * Since the information managed by this module is also stored by the
935244eac9SBenno Rice  * logical address mapping module, this module may throw away valid virtual
945244eac9SBenno Rice  * to physical mappings at almost any time.  However, invalidations of
955244eac9SBenno Rice  * mappings must be done as requested.
965244eac9SBenno Rice  *
975244eac9SBenno Rice  * In order to cope with hardware architectures which make virtual to
985244eac9SBenno Rice  * physical map invalidates expensive, this module may delay invalidate
995244eac9SBenno Rice  * reduced protection operations until such time as they are actually
1005244eac9SBenno Rice  * necessary.  This module is given full information as to which processors
1015244eac9SBenno Rice  * are currently using which maps, and to when physical maps must be made
1025244eac9SBenno Rice  * correct.
1035244eac9SBenno Rice  */
1045244eac9SBenno Rice 
105ad7a226fSPeter Wemm #include "opt_kstack_pages.h"
106ad7a226fSPeter Wemm 
107f9bac91bSBenno Rice #include <sys/param.h>
1080b27d710SPeter Wemm #include <sys/kernel.h>
109c47dd3dbSAttilio Rao #include <sys/queue.h>
110c47dd3dbSAttilio Rao #include <sys/cpuset.h>
1115244eac9SBenno Rice #include <sys/ktr.h>
11294e0b85eSMark Peek #include <sys/lock.h>
1135244eac9SBenno Rice #include <sys/msgbuf.h>
114f9bac91bSBenno Rice #include <sys/mutex.h>
1155244eac9SBenno Rice #include <sys/proc.h>
1163653f5cbSAlan Cox #include <sys/rwlock.h>
117c47dd3dbSAttilio Rao #include <sys/sched.h>
1185244eac9SBenno Rice #include <sys/sysctl.h>
1195244eac9SBenno Rice #include <sys/systm.h>
1205244eac9SBenno Rice #include <sys/vmmeter.h>
1215244eac9SBenno Rice 
1225244eac9SBenno Rice #include <dev/ofw/openfirm.h>
123f9bac91bSBenno Rice 
124f9bac91bSBenno Rice #include <vm/vm.h>
125f9bac91bSBenno Rice #include <vm/vm_param.h>
126f9bac91bSBenno Rice #include <vm/vm_kern.h>
127f9bac91bSBenno Rice #include <vm/vm_page.h>
128f9bac91bSBenno Rice #include <vm/vm_map.h>
129f9bac91bSBenno Rice #include <vm/vm_object.h>
130f9bac91bSBenno Rice #include <vm/vm_extern.h>
131f9bac91bSBenno Rice #include <vm/vm_pageout.h>
132378862a7SJeff Roberson #include <vm/uma.h>
133f9bac91bSBenno Rice 
1347c277971SPeter Grehan #include <machine/cpu.h>
135b40ce02aSNathan Whitehorn #include <machine/platform.h>
136d699b539SMark Peek #include <machine/bat.h>
1375244eac9SBenno Rice #include <machine/frame.h>
1385244eac9SBenno Rice #include <machine/md_var.h>
1395244eac9SBenno Rice #include <machine/psl.h>
140f9bac91bSBenno Rice #include <machine/pte.h>
14112640815SMarcel Moolenaar #include <machine/smp.h>
1425244eac9SBenno Rice #include <machine/sr.h>
14359276937SPeter Grehan #include <machine/mmuvar.h>
144258dbffeSNathan Whitehorn #include <machine/trap.h>
145f9bac91bSBenno Rice 
14659276937SPeter Grehan #include "mmu_if.h"
14759276937SPeter Grehan 
14859276937SPeter Grehan #define	MOEA_DEBUG
149f9bac91bSBenno Rice 
1505244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
151f9bac91bSBenno Rice 
1525244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1535244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1545244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1555244eac9SBenno Rice 
1565244eac9SBenno Rice struct ofw_map {
1575244eac9SBenno Rice 	vm_offset_t	om_va;
1585244eac9SBenno Rice 	vm_size_t	om_len;
1595244eac9SBenno Rice 	vm_offset_t	om_pa;
1605244eac9SBenno Rice 	u_int		om_mode;
1615244eac9SBenno Rice };
162f9bac91bSBenno Rice 
163afd9cb6cSJustin Hibbits extern unsigned char _etext[];
164afd9cb6cSJustin Hibbits extern unsigned char _end[];
165afd9cb6cSJustin Hibbits 
166afd9cb6cSJustin Hibbits extern int dumpsys_minidump;
167afd9cb6cSJustin Hibbits 
1685244eac9SBenno Rice /*
1695244eac9SBenno Rice  * Map of physical memory regions.
1705244eac9SBenno Rice  */
17131c82d03SBenno Rice static struct	mem_region *regions;
17231c82d03SBenno Rice static struct	mem_region *pregions;
173c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
174c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
175aa39961eSBenno Rice static struct	ofw_map *translations;
1765244eac9SBenno Rice 
177f9bac91bSBenno Rice /*
178f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
179f489bf21SAlan Cox  */
18059276937SPeter Grehan struct mtx	moea_table_mutex;
181e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
182f489bf21SAlan Cox 
183e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
184e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
185e4f72b32SMarcel Moolenaar 
186f489bf21SAlan Cox /*
1875244eac9SBenno Rice  * PTEG data.
188f9bac91bSBenno Rice  */
18959276937SPeter Grehan static struct	pteg *moea_pteg_table;
19059276937SPeter Grehan u_int		moea_pteg_count;
19159276937SPeter Grehan u_int		moea_pteg_mask;
1925244eac9SBenno Rice 
1935244eac9SBenno Rice /*
1945244eac9SBenno Rice  * PVO data.
1955244eac9SBenno Rice  */
19659276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
19759276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
19859276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
1995244eac9SBenno Rice 
200cfedf924SAttilio Rao static struct rwlock_padalign pvh_global_lock;
2013653f5cbSAlan Cox 
20259276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
20359276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2045244eac9SBenno Rice 
2050d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
20659276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
20759276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2085244eac9SBenno Rice 
2095244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
21059276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2115244eac9SBenno Rice 
21259276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2135244eac9SBenno Rice 
2145244eac9SBenno Rice /*
2155244eac9SBenno Rice  * Statistics.
2165244eac9SBenno Rice  */
21759276937SPeter Grehan u_int	moea_pte_valid = 0;
21859276937SPeter Grehan u_int	moea_pte_overflow = 0;
21959276937SPeter Grehan u_int	moea_pte_replacements = 0;
22059276937SPeter Grehan u_int	moea_pvo_entries = 0;
22159276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
22259276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
22359276937SPeter Grehan u_int	moea_pte_spills = 0;
22459276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2255244eac9SBenno Rice     0, "");
22659276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
22759276937SPeter Grehan     &moea_pte_overflow, 0, "");
22859276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
22959276937SPeter Grehan     &moea_pte_replacements, 0, "");
23059276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2315244eac9SBenno Rice     0, "");
23259276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
23359276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
23459276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
23559276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
23659276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
23759276937SPeter Grehan     &moea_pte_spills, 0, "");
2385244eac9SBenno Rice 
2395244eac9SBenno Rice /*
24059276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2415244eac9SBenno Rice  */
24259276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2435244eac9SBenno Rice 
2445244eac9SBenno Rice /*
2455244eac9SBenno Rice  * PTE calls.
2465244eac9SBenno Rice  */
24759276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2485244eac9SBenno Rice 
2495244eac9SBenno Rice /*
2505244eac9SBenno Rice  * PVO calls.
2515244eac9SBenno Rice  */
25259276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2535244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
25459276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
25559276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
25659276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2575244eac9SBenno Rice 
2585244eac9SBenno Rice /*
2595244eac9SBenno Rice  * Utility routines.
2605244eac9SBenno Rice  */
261ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
262ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
26359276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
26459276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
265ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
26659276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
26759276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
26859276937SPeter Grehan 
26959276937SPeter Grehan /*
27059276937SPeter Grehan  * Kernel MMU interface
27159276937SPeter Grehan  */
27259276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
27359276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
27459276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
275e8a4a618SKonstantin Belousov void moea_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset,
276e8a4a618SKonstantin Belousov     vm_page_t *mb, vm_offset_t b_offset, int xfersize);
27759276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
278ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
279ce142d9eSAlan Cox     vm_prot_t);
2802053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
28159276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
28259276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
28359276937SPeter Grehan void moea_init(mmu_t);
28459276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
285e396eb60SAlan Cox boolean_t moea_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
2867b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t);
2878d9e6d9fSAlan Cox int moea_ts_referenced(mmu_t, vm_page_t);
28820b79612SRafal Jaworowski vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_paddr_t, vm_paddr_t, int);
28959276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
29059677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
29159276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
29259276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
29359276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
29459276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
29559276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
29659276937SPeter Grehan void moea_release(mmu_t, pmap_t);
29759276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
29859276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
29978985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
300a844c68fSAlan Cox void moea_unwire(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
30159276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
30259276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
30359276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
30459276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
30559276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
3061c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int);
30759276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
30820b79612SRafal Jaworowski void *moea_mapdev(mmu_t, vm_paddr_t, vm_size_t);
309c1f4123bSNathan Whitehorn void *moea_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t);
31059276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
31120b79612SRafal Jaworowski vm_paddr_t moea_kextract(mmu_t, vm_offset_t);
312c1f4123bSNathan Whitehorn void moea_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t);
31320b79612SRafal Jaworowski void moea_kenter(mmu_t, vm_offset_t, vm_paddr_t);
314c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma);
31520b79612SRafal Jaworowski boolean_t moea_dev_direct_mapped(mmu_t, vm_paddr_t, vm_size_t);
3161a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
317afd9cb6cSJustin Hibbits vm_offset_t moea_dumpsys_map(mmu_t mmu, struct pmap_md *md, vm_size_t ofs,
318afd9cb6cSJustin Hibbits     vm_size_t *sz);
319afd9cb6cSJustin Hibbits struct pmap_md * moea_scan_md(mmu_t mmu, struct pmap_md *prev);
32059276937SPeter Grehan 
32159276937SPeter Grehan static mmu_method_t moea_methods[] = {
32259276937SPeter Grehan 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
32359276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
32459276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
325e8a4a618SKonstantin Belousov 	MMUMETHOD(mmu_copy_pages,	moea_copy_pages),
32659276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
327ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
32859276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
32959276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
33059276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
33159276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
33259276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
333e396eb60SAlan Cox 	MMUMETHOD(mmu_is_prefaultable,	moea_is_prefaultable),
3347b85f591SAlan Cox 	MMUMETHOD(mmu_is_referenced,	moea_is_referenced),
33559276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
33659276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
33759276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
33859677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
33959276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
34059276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
34159276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
34259276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
34359276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
34459276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
34559276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
34659276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
34778985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
3481a4fcaebSMarcel Moolenaar 	MMUMETHOD(mmu_sync_icache,	moea_sync_icache),
349a844c68fSAlan Cox 	MMUMETHOD(mmu_unwire,		moea_unwire),
35059276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
35159276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
35259276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
35359276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
35459276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
355c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_page_set_memattr,	moea_page_set_memattr),
35659276937SPeter Grehan 
35759276937SPeter Grehan 	/* Internal interfaces */
35859276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
3591c96bdd1SNathan Whitehorn 	MMUMETHOD(mmu_cpu_bootstrap,   	moea_cpu_bootstrap),
360c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_mapdev_attr,	moea_mapdev_attr),
36159276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
36259276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
36359276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
36459276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
365c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_kenter_attr,	moea_kenter_attr),
36659276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
367afd9cb6cSJustin Hibbits 	MMUMETHOD(mmu_scan_md,		moea_scan_md),
368afd9cb6cSJustin Hibbits 	MMUMETHOD(mmu_dumpsys_map,	moea_dumpsys_map),
36959276937SPeter Grehan 
37059276937SPeter Grehan 	{ 0, 0 }
37159276937SPeter Grehan };
37259276937SPeter Grehan 
37333529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0);
37433529b98SPeter Grehan 
375c1f4123bSNathan Whitehorn static __inline uint32_t
376c1f4123bSNathan Whitehorn moea_calc_wimg(vm_offset_t pa, vm_memattr_t ma)
377c1f4123bSNathan Whitehorn {
378c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
379c1f4123bSNathan Whitehorn 	int i;
380c1f4123bSNathan Whitehorn 
381c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
382c1f4123bSNathan Whitehorn 		switch (ma) {
383c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
384c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
385c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
386c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
387c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
388c1f4123bSNathan Whitehorn 			return (PTE_I);
389c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
390c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
391c1f4123bSNathan Whitehorn 		}
392c1f4123bSNathan Whitehorn 	}
393c1f4123bSNathan Whitehorn 
394c1f4123bSNathan Whitehorn 	/*
395c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
396c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
397c1f4123bSNathan Whitehorn 	 */
398c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
399c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
400c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
401c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
402c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
403c1f4123bSNathan Whitehorn 			break;
404c1f4123bSNathan Whitehorn 		}
405c1f4123bSNathan Whitehorn 	}
406c1f4123bSNathan Whitehorn 
407c1f4123bSNathan Whitehorn 	return pte_lo;
408c1f4123bSNathan Whitehorn }
40959276937SPeter Grehan 
410e4f72b32SMarcel Moolenaar static void
411e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
412e4f72b32SMarcel Moolenaar {
413e4f72b32SMarcel Moolenaar 
414e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
41594363f53SNathan Whitehorn 	__asm __volatile("ptesync");
416e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
41794363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
418e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
419e4f72b32SMarcel Moolenaar }
420e4f72b32SMarcel Moolenaar 
421e4f72b32SMarcel Moolenaar static void
422e4f72b32SMarcel Moolenaar tlbia(void)
423e4f72b32SMarcel Moolenaar {
424e4f72b32SMarcel Moolenaar 	vm_offset_t va;
425e4f72b32SMarcel Moolenaar 
426e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
427e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
428e4f72b32SMarcel Moolenaar 		powerpc_sync();
429e4f72b32SMarcel Moolenaar 	}
430e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
431e4f72b32SMarcel Moolenaar 	powerpc_sync();
432e4f72b32SMarcel Moolenaar }
4335244eac9SBenno Rice 
4345244eac9SBenno Rice static __inline int
4355244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4365244eac9SBenno Rice {
4375244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4385244eac9SBenno Rice }
4395244eac9SBenno Rice 
4405244eac9SBenno Rice static __inline u_int
4415244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4425244eac9SBenno Rice {
4435244eac9SBenno Rice 	u_int hash;
4445244eac9SBenno Rice 
4455244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4465244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
44759276937SPeter Grehan 	return (hash & moea_pteg_mask);
4485244eac9SBenno Rice }
4495244eac9SBenno Rice 
4505244eac9SBenno Rice static __inline struct pvo_head *
4515244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
452f9bac91bSBenno Rice {
453f9bac91bSBenno Rice 
4545244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
455f9bac91bSBenno Rice }
456f9bac91bSBenno Rice 
457f9bac91bSBenno Rice static __inline void
45859276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
459f9bac91bSBenno Rice {
460f9bac91bSBenno Rice 
4613653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4625244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4635244eac9SBenno Rice }
4645244eac9SBenno Rice 
4655244eac9SBenno Rice static __inline int
46659276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4675244eac9SBenno Rice {
4685244eac9SBenno Rice 
4695244eac9SBenno Rice 	return (m->md.mdpg_attrs);
470f9bac91bSBenno Rice }
471f9bac91bSBenno Rice 
472f9bac91bSBenno Rice static __inline void
47359276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
474f9bac91bSBenno Rice {
475f9bac91bSBenno Rice 
4763653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4775244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
478f9bac91bSBenno Rice }
479f9bac91bSBenno Rice 
480f9bac91bSBenno Rice static __inline int
48159276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
482f9bac91bSBenno Rice {
4835244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4845244eac9SBenno Rice 		return (1);
485f9bac91bSBenno Rice 
4865244eac9SBenno Rice 	return (0);
487f9bac91bSBenno Rice }
488f9bac91bSBenno Rice 
489f9bac91bSBenno Rice static __inline int
49059276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
491f9bac91bSBenno Rice {
4925244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
4935244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4945244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
495f9bac91bSBenno Rice }
496f9bac91bSBenno Rice 
4975244eac9SBenno Rice static __inline void
49859276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
499f9bac91bSBenno Rice {
500d644a0b7SAlan Cox 
501d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
502d644a0b7SAlan Cox 
503f9bac91bSBenno Rice 	/*
5045244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5055244eac9SBenno Rice 	 * set when the real pte is set in memory.
506f9bac91bSBenno Rice 	 *
507f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
508f9bac91bSBenno Rice 	 */
5095244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5105244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5115244eac9SBenno Rice 	pt->pte_lo = pte_lo;
512f9bac91bSBenno Rice }
513f9bac91bSBenno Rice 
5145244eac9SBenno Rice static __inline void
51559276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
516f9bac91bSBenno Rice {
517f9bac91bSBenno Rice 
518d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5195244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
520f9bac91bSBenno Rice }
521f9bac91bSBenno Rice 
5225244eac9SBenno Rice static __inline void
52359276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
524f9bac91bSBenno Rice {
5255244eac9SBenno Rice 
526d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
527d644a0b7SAlan Cox 
5285244eac9SBenno Rice 	/*
5295244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5305244eac9SBenno Rice 	 */
5315244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
532e4f72b32SMarcel Moolenaar 	tlbie(va);
5335244eac9SBenno Rice }
5345244eac9SBenno Rice 
5355244eac9SBenno Rice static __inline void
53659276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5375244eac9SBenno Rice {
5385244eac9SBenno Rice 
539d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5405244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5415244eac9SBenno Rice 
5425244eac9SBenno Rice 	/*
5435244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
544804d1cc1SJustin Hibbits 	 * Note that the REF/CHG bits are from pvo_pt and thus should have
5455244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5465244eac9SBenno Rice 	 */
5475244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
548e4f72b32SMarcel Moolenaar 	powerpc_sync();
5495244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
550e4f72b32SMarcel Moolenaar 	powerpc_sync();
55159276937SPeter Grehan 	moea_pte_valid++;
5525244eac9SBenno Rice }
5535244eac9SBenno Rice 
5545244eac9SBenno Rice static __inline void
55559276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5565244eac9SBenno Rice {
5575244eac9SBenno Rice 
558d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5595244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5605244eac9SBenno Rice 
5615244eac9SBenno Rice 	/*
5625244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5635244eac9SBenno Rice 	 */
564e4f72b32SMarcel Moolenaar 	powerpc_sync();
5655244eac9SBenno Rice 
5665244eac9SBenno Rice 	/*
5675244eac9SBenno Rice 	 * Invalidate the pte.
5685244eac9SBenno Rice 	 */
5695244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5705244eac9SBenno Rice 
571e4f72b32SMarcel Moolenaar 	tlbie(va);
5725244eac9SBenno Rice 
5735244eac9SBenno Rice 	/*
5745244eac9SBenno Rice 	 * Save the reg & chg bits.
5755244eac9SBenno Rice 	 */
57659276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
57759276937SPeter Grehan 	moea_pte_valid--;
5785244eac9SBenno Rice }
5795244eac9SBenno Rice 
5805244eac9SBenno Rice static __inline void
58159276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5825244eac9SBenno Rice {
5835244eac9SBenno Rice 
5845244eac9SBenno Rice 	/*
5855244eac9SBenno Rice 	 * Invalidate the PTE
5865244eac9SBenno Rice 	 */
58759276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
58859276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
589f9bac91bSBenno Rice }
590f9bac91bSBenno Rice 
591f9bac91bSBenno Rice /*
5925244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
593f9bac91bSBenno Rice  */
5945244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
5955244eac9SBenno Rice 
5965244eac9SBenno Rice static int
5975244eac9SBenno Rice om_cmp(const void *a, const void *b)
5985244eac9SBenno Rice {
5995244eac9SBenno Rice 	const struct	ofw_map *mapa;
6005244eac9SBenno Rice 	const struct	ofw_map *mapb;
6015244eac9SBenno Rice 
6025244eac9SBenno Rice 	mapa = a;
6035244eac9SBenno Rice 	mapb = b;
6045244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6055244eac9SBenno Rice 		return (-1);
6065244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6075244eac9SBenno Rice 		return (1);
6085244eac9SBenno Rice 	else
6095244eac9SBenno Rice 		return (0);
610f9bac91bSBenno Rice }
611f9bac91bSBenno Rice 
612f9bac91bSBenno Rice void
6131c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap)
61412640815SMarcel Moolenaar {
61512640815SMarcel Moolenaar 	u_int sdr;
61612640815SMarcel Moolenaar 	int i;
61712640815SMarcel Moolenaar 
61812640815SMarcel Moolenaar 	if (ap) {
619e4f72b32SMarcel Moolenaar 		powerpc_sync();
62012640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
62112640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
62212640815SMarcel Moolenaar 		isync();
62312640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
62412640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
62512640815SMarcel Moolenaar 		isync();
62612640815SMarcel Moolenaar 	}
62712640815SMarcel Moolenaar 
628aef8ef51SAdrian Chadd #ifdef WII
629aef8ef51SAdrian Chadd 	/*
630aef8ef51SAdrian Chadd 	 * Special case for the Wii: don't install the PCI BAT.
631aef8ef51SAdrian Chadd 	 */
632aef8ef51SAdrian Chadd 	if (strcmp(installed_platform(), "wii") != 0) {
633aef8ef51SAdrian Chadd #endif
63401d8aa0dSMarcel Moolenaar 		__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
63501d8aa0dSMarcel Moolenaar 		__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
636aef8ef51SAdrian Chadd #ifdef WII
637aef8ef51SAdrian Chadd 	}
638aef8ef51SAdrian Chadd #endif
63912640815SMarcel Moolenaar 	isync();
64012640815SMarcel Moolenaar 
64101d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
64201d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
64301d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
64401d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
64501d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
64612640815SMarcel Moolenaar 	isync();
64712640815SMarcel Moolenaar 
64812640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
649fe3b4685SNathan Whitehorn 		mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
650e4f72b32SMarcel Moolenaar 	powerpc_sync();
65112640815SMarcel Moolenaar 
65212640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
65312640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
65412640815SMarcel Moolenaar 	isync();
65512640815SMarcel Moolenaar 
65686c1fb4cSMarcel Moolenaar 	tlbia();
65712640815SMarcel Moolenaar }
65812640815SMarcel Moolenaar 
65912640815SMarcel Moolenaar void
66059276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
661f9bac91bSBenno Rice {
66231c82d03SBenno Rice 	ihandle_t	mmui;
6635244eac9SBenno Rice 	phandle_t	chosen, mmu;
6645244eac9SBenno Rice 	int		sz;
6655244eac9SBenno Rice 	int		i, j;
666e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6675244eac9SBenno Rice 	vm_offset_t	pa, va, off;
66850c202c5SJeff Roberson 	void		*dpcpu;
669976cc697SNathan Whitehorn 	register_t	msr;
670f9bac91bSBenno Rice 
671f9bac91bSBenno Rice         /*
67232bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6730d290675SBenno Rice          */
6740d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6750d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6760d290675SBenno Rice 
6770d290675SBenno Rice 	/*
6780d290675SBenno Rice 	 * Map PCI memory space.
6790d290675SBenno Rice 	 */
6800d290675SBenno Rice 	battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6810d290675SBenno Rice 	battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6820d290675SBenno Rice 
6830d290675SBenno Rice 	battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6840d290675SBenno Rice 	battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6850d290675SBenno Rice 
6860d290675SBenno Rice 	battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6870d290675SBenno Rice 	battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6880d290675SBenno Rice 
6890d290675SBenno Rice 	battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6900d290675SBenno Rice 	battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6910d290675SBenno Rice 
6920d290675SBenno Rice 	/*
6930d290675SBenno Rice 	 * Map obio devices.
6940d290675SBenno Rice 	 */
6950d290675SBenno Rice 	battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
6960d290675SBenno Rice 	battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
6970d290675SBenno Rice 
6980d290675SBenno Rice 	/*
6995244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
700976cc697SNathan Whitehorn 	 * where we are. Turn off instruction relocation temporarily
701976cc697SNathan Whitehorn 	 * to prevent faults while reprogramming the IBAT.
702f9bac91bSBenno Rice 	 */
703976cc697SNathan Whitehorn 	msr = mfmsr();
704976cc697SNathan Whitehorn 	mtmsr(msr & ~PSL_IR);
70559276937SPeter Grehan 	__asm (".balign 32; \n"
70672ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
7075d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
70812640815SMarcel Moolenaar 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
709976cc697SNathan Whitehorn 	mtmsr(msr);
7100d290675SBenno Rice 
711aef8ef51SAdrian Chadd #ifdef WII
712aef8ef51SAdrian Chadd         if (strcmp(installed_platform(), "wii") != 0) {
713aef8ef51SAdrian Chadd #endif
7140d290675SBenno Rice 		/* map pci space */
71512640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
71612640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
717aef8ef51SAdrian Chadd #ifdef WII
718aef8ef51SAdrian Chadd 	}
719aef8ef51SAdrian Chadd #endif
72012640815SMarcel Moolenaar 	isync();
721f9bac91bSBenno Rice 
7221c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7231c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7241c96bdd1SNathan Whitehorn 
72531c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
72659276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
72731c82d03SBenno Rice 
72831c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
72932bc7846SPeter Grehan 		vm_offset_t pa;
73032bc7846SPeter Grehan 		vm_offset_t end;
73132bc7846SPeter Grehan 
73231c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
73331c82d03SBenno Rice 			pregions[i].mr_start,
73431c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
73531c82d03SBenno Rice 			pregions[i].mr_size);
73632bc7846SPeter Grehan 		/*
73732bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
73832bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
73932bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
74032bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
74132bc7846SPeter Grehan 		 * a while yet.
74232bc7846SPeter Grehan 		 */
74332bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
74432bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
74532bc7846SPeter Grehan 		do {
74632bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
74732bc7846SPeter Grehan 
74832bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
74932bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
75032bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
75132bc7846SPeter Grehan 		} while (pa < end);
75231c82d03SBenno Rice 	}
75331c82d03SBenno Rice 
75431c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
75559276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
75697f7cde4SNathan Whitehorn 
7575244eac9SBenno Rice 	phys_avail_count = 0;
758d2c1f576SBenno Rice 	physsz = 0;
759b0c21309SPeter Grehan 	hwphyssz = 0;
760b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
76131c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7625244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7635244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7645244eac9SBenno Rice 		    regions[i].mr_size);
765e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
766e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
767e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
768e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
769e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
770e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
771e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
772e2f6d6e2SPeter Grehan 				phys_avail_count++;
773e2f6d6e2SPeter Grehan 			}
774e2f6d6e2SPeter Grehan 			break;
775e2f6d6e2SPeter Grehan 		}
7765244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7775244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7785244eac9SBenno Rice 		phys_avail_count++;
779d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
780f9bac91bSBenno Rice 	}
781e347e23bSNathan Whitehorn 
782e347e23bSNathan Whitehorn 	/* Check for overlap with the kernel and exception vectors */
783e347e23bSNathan Whitehorn 	for (j = 0; j < 2*phys_avail_count; j+=2) {
784e347e23bSNathan Whitehorn 		if (phys_avail[j] < EXC_LAST)
785e347e23bSNathan Whitehorn 			phys_avail[j] += EXC_LAST;
786e347e23bSNathan Whitehorn 
787e347e23bSNathan Whitehorn 		if (kernelstart >= phys_avail[j] &&
788e347e23bSNathan Whitehorn 		    kernelstart < phys_avail[j+1]) {
789e347e23bSNathan Whitehorn 			if (kernelend < phys_avail[j+1]) {
790e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] =
791e347e23bSNathan Whitehorn 				    (kernelend & ~PAGE_MASK) + PAGE_SIZE;
792e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
793e347e23bSNathan Whitehorn 				    phys_avail[j+1];
794e347e23bSNathan Whitehorn 				phys_avail_count++;
795e347e23bSNathan Whitehorn 			}
796e347e23bSNathan Whitehorn 
797e347e23bSNathan Whitehorn 			phys_avail[j+1] = kernelstart & ~PAGE_MASK;
798e347e23bSNathan Whitehorn 		}
799e347e23bSNathan Whitehorn 
800e347e23bSNathan Whitehorn 		if (kernelend >= phys_avail[j] &&
801e347e23bSNathan Whitehorn 		    kernelend < phys_avail[j+1]) {
802e347e23bSNathan Whitehorn 			if (kernelstart > phys_avail[j]) {
803e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] = phys_avail[j];
804e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
805e347e23bSNathan Whitehorn 				    kernelstart & ~PAGE_MASK;
806e347e23bSNathan Whitehorn 				phys_avail_count++;
807e347e23bSNathan Whitehorn 			}
808e347e23bSNathan Whitehorn 
809e347e23bSNathan Whitehorn 			phys_avail[j] = (kernelend & ~PAGE_MASK) + PAGE_SIZE;
810e347e23bSNathan Whitehorn 		}
811e347e23bSNathan Whitehorn 	}
812e347e23bSNathan Whitehorn 
813d2c1f576SBenno Rice 	physmem = btoc(physsz);
814f9bac91bSBenno Rice 
815f9bac91bSBenno Rice 	/*
8165244eac9SBenno Rice 	 * Allocate PTEG table.
817f9bac91bSBenno Rice 	 */
8185244eac9SBenno Rice #ifdef PTEGCOUNT
81959276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
8205244eac9SBenno Rice #else
82159276937SPeter Grehan 	moea_pteg_count = 0x1000;
822f9bac91bSBenno Rice 
82359276937SPeter Grehan 	while (moea_pteg_count < physmem)
82459276937SPeter Grehan 		moea_pteg_count <<= 1;
825f9bac91bSBenno Rice 
82659276937SPeter Grehan 	moea_pteg_count >>= 1;
8275244eac9SBenno Rice #endif /* PTEGCOUNT */
828f9bac91bSBenno Rice 
82959276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
83059276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
8315244eac9SBenno Rice 	    size);
83259276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
83359276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
83459276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
83559276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
836f9bac91bSBenno Rice 
8375244eac9SBenno Rice 	/*
838864bc520SBenno Rice 	 * Allocate pv/overflow lists.
8395244eac9SBenno Rice 	 */
84059276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
84159276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
8425244eac9SBenno Rice 	    PAGE_SIZE);
84359276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
84459276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
84559276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
8465244eac9SBenno Rice 
8475244eac9SBenno Rice 	/*
848f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
849f489bf21SAlan Cox 	 * tables.
850f489bf21SAlan Cox 	 */
851d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
852d644a0b7SAlan Cox 	    MTX_RECURSE);
853e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
854f489bf21SAlan Cox 
855e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
856e4f72b32SMarcel Moolenaar 
857f489bf21SAlan Cox 	/*
8585244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8595244eac9SBenno Rice 	 */
86059276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8610d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
86259276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8635244eac9SBenno Rice 
8645244eac9SBenno Rice 	/*
8655244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8665244eac9SBenno Rice 	 */
86759276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8685244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
86959276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8705244eac9SBenno Rice 
8715244eac9SBenno Rice 	/*
872fe3b4685SNathan Whitehorn 	 * Initialize the kernel pmap (which is statically allocated).
8735244eac9SBenno Rice 	 */
874fe3b4685SNathan Whitehorn 	PMAP_LOCK_INIT(kernel_pmap);
875fe3b4685SNathan Whitehorn 	for (i = 0; i < 16; i++)
876fe3b4685SNathan Whitehorn 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
877c47dd3dbSAttilio Rao 	CPU_FILL(&kernel_pmap->pm_active);
878ccc4a5c7SNathan Whitehorn 	RB_INIT(&kernel_pmap->pmap_pvo);
879fe3b4685SNathan Whitehorn 
880fe3b4685SNathan Whitehorn  	/*
8813653f5cbSAlan Cox 	 * Initialize the global pv list lock.
8823653f5cbSAlan Cox 	 */
8833653f5cbSAlan Cox 	rw_init(&pvh_global_lock, "pmap pv global");
8843653f5cbSAlan Cox 
8853653f5cbSAlan Cox 	/*
886fe3b4685SNathan Whitehorn 	 * Set up the Open Firmware mappings
887fe3b4685SNathan Whitehorn 	 */
888e347e23bSNathan Whitehorn 	chosen = OF_finddevice("/chosen");
889e347e23bSNathan Whitehorn 	if (chosen != -1 && OF_getprop(chosen, "mmu", &mmui, 4) != -1 &&
890e347e23bSNathan Whitehorn 	    (mmu = OF_instance_to_package(mmui)) != -1 &&
891e347e23bSNathan Whitehorn 	    (sz = OF_getproplen(mmu, "translations")) != -1) {
892aa39961eSBenno Rice 		translations = NULL;
8936cc1cdf4SPeter Grehan 		for (i = 0; phys_avail[i] != 0; i += 2) {
8946cc1cdf4SPeter Grehan 			if (phys_avail[i + 1] >= sz) {
895aa39961eSBenno Rice 				translations = (struct ofw_map *)phys_avail[i];
8966cc1cdf4SPeter Grehan 				break;
8976cc1cdf4SPeter Grehan 			}
898aa39961eSBenno Rice 		}
899aa39961eSBenno Rice 		if (translations == NULL)
90059276937SPeter Grehan 			panic("moea_bootstrap: no space to copy translations");
9015244eac9SBenno Rice 		bzero(translations, sz);
9025244eac9SBenno Rice 		if (OF_getprop(mmu, "translations", translations, sz) == -1)
90359276937SPeter Grehan 			panic("moea_bootstrap: can't get ofw translations");
90459276937SPeter Grehan 		CTR0(KTR_PMAP, "moea_bootstrap: translations");
90531c82d03SBenno Rice 		sz /= sizeof(*translations);
9065244eac9SBenno Rice 		qsort(translations, sz, sizeof (*translations), om_cmp);
907ed1e1e2aSNathan Whitehorn 		for (i = 0; i < sz; i++) {
9085244eac9SBenno Rice 			CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
9095244eac9SBenno Rice 			    translations[i].om_pa, translations[i].om_va,
9105244eac9SBenno Rice 			    translations[i].om_len);
9115244eac9SBenno Rice 
91232bc7846SPeter Grehan 			/*
913e347e23bSNathan Whitehorn 			 * If the mapping is 1:1, let the RAM and device
914e347e23bSNathan Whitehorn 			 * on-demand BAT tables take care of the translation.
91532bc7846SPeter Grehan 			 */
91632bc7846SPeter Grehan 			if (translations[i].om_va == translations[i].om_pa)
91732bc7846SPeter Grehan 				continue;
9185244eac9SBenno Rice 
91932bc7846SPeter Grehan 			/* Enter the pages */
920e347e23bSNathan Whitehorn 			for (off = 0; off < translations[i].om_len;
921e347e23bSNathan Whitehorn 			    off += PAGE_SIZE)
922fe3b4685SNathan Whitehorn 				moea_kenter(mmup, translations[i].om_va + off,
923fe3b4685SNathan Whitehorn 					    translations[i].om_pa + off);
924f9bac91bSBenno Rice 		}
925e347e23bSNathan Whitehorn 	}
926014ffa99SMarcel Moolenaar 
927014ffa99SMarcel Moolenaar 	/*
928014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
929014ffa99SMarcel Moolenaar 	 */
930014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
931014ffa99SMarcel Moolenaar 		;
932014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
9335244eac9SBenno Rice 
9341c96bdd1SNathan Whitehorn 	moea_cpu_bootstrap(mmup,0);
9355244eac9SBenno Rice 
9365244eac9SBenno Rice 	pmap_bootstrapped++;
937014ffa99SMarcel Moolenaar 
938014ffa99SMarcel Moolenaar 	/*
939014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
940014ffa99SMarcel Moolenaar 	 */
941014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
942ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
943014ffa99SMarcel Moolenaar 
944014ffa99SMarcel Moolenaar 	/*
945014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
946014ffa99SMarcel Moolenaar 	 * into the kernel page map.
947014ffa99SMarcel Moolenaar 	 */
948014ffa99SMarcel Moolenaar 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
949014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
950014ffa99SMarcel Moolenaar 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
951014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
952014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
953014ffa99SMarcel Moolenaar 	thread0.td_kstack_pages = KSTACK_PAGES;
954014ffa99SMarcel Moolenaar 	for (i = 0; i < KSTACK_PAGES; i++) {
955c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
956014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
957014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
958014ffa99SMarcel Moolenaar 	}
959014ffa99SMarcel Moolenaar 
960014ffa99SMarcel Moolenaar 	/*
961014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
962014ffa99SMarcel Moolenaar 	 */
9634053b05bSSergey Kandaurov 	pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE);
964014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
965014ffa99SMarcel Moolenaar 	va = virtual_avail;
9664053b05bSSergey Kandaurov 	virtual_avail += round_page(msgbufsize);
967014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
968c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
969014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
970014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
971014ffa99SMarcel Moolenaar 	}
97250c202c5SJeff Roberson 
97350c202c5SJeff Roberson 	/*
97450c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
97550c202c5SJeff Roberson 	 */
97650c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
97750c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
97850c202c5SJeff Roberson 	va = virtual_avail;
97950c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
98050c202c5SJeff Roberson 	while (va < virtual_avail) {
981c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
98250c202c5SJeff Roberson 		pa += PAGE_SIZE;
98350c202c5SJeff Roberson 		va += PAGE_SIZE;
98450c202c5SJeff Roberson 	}
98550c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
9865244eac9SBenno Rice }
9875244eac9SBenno Rice 
9885244eac9SBenno Rice /*
9895244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9905244eac9SBenno Rice  * space can be accessed in any way.
991f9bac91bSBenno Rice  */
992f9bac91bSBenno Rice void
99359276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
994f9bac91bSBenno Rice {
9958207b362SBenno Rice 	pmap_t	pm, pmr;
996f9bac91bSBenno Rice 
997f9bac91bSBenno Rice 	/*
99832bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9995244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
1000f9bac91bSBenno Rice 	 */
10015244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
100252a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
10038207b362SBenno Rice 
1004c7c2767eSAttilio Rao 	CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
10058207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
1006ac6ba8bdSBenno Rice }
1007ac6ba8bdSBenno Rice 
1008ac6ba8bdSBenno Rice void
100959276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
1010ac6ba8bdSBenno Rice {
1011ac6ba8bdSBenno Rice 	pmap_t	pm;
1012ac6ba8bdSBenno Rice 
1013ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
1014c7c2767eSAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &pm->pm_active);
10158207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
1016f9bac91bSBenno Rice }
1017f9bac91bSBenno Rice 
1018f9bac91bSBenno Rice void
101959276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
1020f9bac91bSBenno Rice {
10210f92104cSBenno Rice 	struct	pvo_entry *pvo;
10220f92104cSBenno Rice 
102348d0b1a0SAlan Cox 	PMAP_LOCK(pm);
102459276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
10250f92104cSBenno Rice 
10260f92104cSBenno Rice 	if (pvo != NULL) {
10270f92104cSBenno Rice 		if (wired) {
10280f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
10290f92104cSBenno Rice 				pm->pm_stats.wired_count++;
10300f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
10310f92104cSBenno Rice 		} else {
10320f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
10330f92104cSBenno Rice 				pm->pm_stats.wired_count--;
10340f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
10350f92104cSBenno Rice 		}
10360f92104cSBenno Rice 	}
103748d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
1038f9bac91bSBenno Rice }
1039f9bac91bSBenno Rice 
1040f9bac91bSBenno Rice void
1041a844c68fSAlan Cox moea_unwire(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1042a844c68fSAlan Cox {
1043a844c68fSAlan Cox 	struct	pvo_entry key, *pvo;
1044a844c68fSAlan Cox 
1045a844c68fSAlan Cox 	PMAP_LOCK(pm);
1046a844c68fSAlan Cox 	key.pvo_vaddr = sva;
1047a844c68fSAlan Cox 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1048a844c68fSAlan Cox 	    pvo != NULL && PVO_VADDR(pvo) < eva;
1049a844c68fSAlan Cox 	    pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
1050a844c68fSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
1051a844c68fSAlan Cox 			panic("moea_unwire: pvo %p is missing PVO_WIRED", pvo);
1052a844c68fSAlan Cox 		pvo->pvo_vaddr &= ~PVO_WIRED;
1053a844c68fSAlan Cox 		pm->pm_stats.wired_count--;
1054a844c68fSAlan Cox 	}
1055a844c68fSAlan Cox 	PMAP_UNLOCK(pm);
1056a844c68fSAlan Cox }
1057a844c68fSAlan Cox 
1058a844c68fSAlan Cox void
105959276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
1060f9bac91bSBenno Rice {
106125e2288dSBenno Rice 	vm_offset_t	dst;
106225e2288dSBenno Rice 	vm_offset_t	src;
106325e2288dSBenno Rice 
106425e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
106525e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
106625e2288dSBenno Rice 
1067e3c2930dSNathan Whitehorn 	bcopy((void *)src, (void *)dst, PAGE_SIZE);
1068f9bac91bSBenno Rice }
1069111c77dcSBenno Rice 
1070e8a4a618SKonstantin Belousov void
1071e8a4a618SKonstantin Belousov moea_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset,
1072e8a4a618SKonstantin Belousov     vm_page_t *mb, vm_offset_t b_offset, int xfersize)
1073e8a4a618SKonstantin Belousov {
1074e8a4a618SKonstantin Belousov 	void *a_cp, *b_cp;
1075e8a4a618SKonstantin Belousov 	vm_offset_t a_pg_offset, b_pg_offset;
1076e8a4a618SKonstantin Belousov 	int cnt;
1077e8a4a618SKonstantin Belousov 
1078e8a4a618SKonstantin Belousov 	while (xfersize > 0) {
1079e8a4a618SKonstantin Belousov 		a_pg_offset = a_offset & PAGE_MASK;
1080e8a4a618SKonstantin Belousov 		cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
1081e8a4a618SKonstantin Belousov 		a_cp = (char *)VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT]) +
1082e8a4a618SKonstantin Belousov 		    a_pg_offset;
1083e8a4a618SKonstantin Belousov 		b_pg_offset = b_offset & PAGE_MASK;
1084e8a4a618SKonstantin Belousov 		cnt = min(cnt, PAGE_SIZE - b_pg_offset);
1085e8a4a618SKonstantin Belousov 		b_cp = (char *)VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT]) +
1086e8a4a618SKonstantin Belousov 		    b_pg_offset;
1087e8a4a618SKonstantin Belousov 		bcopy(a_cp, b_cp, cnt);
1088e8a4a618SKonstantin Belousov 		a_offset += cnt;
1089e8a4a618SKonstantin Belousov 		b_offset += cnt;
1090e8a4a618SKonstantin Belousov 		xfersize -= cnt;
1091e8a4a618SKonstantin Belousov 	}
1092e8a4a618SKonstantin Belousov }
1093e8a4a618SKonstantin Belousov 
1094111c77dcSBenno Rice /*
10955244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
10965244eac9SBenno Rice  */
10975244eac9SBenno Rice void
109859276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
10995244eac9SBenno Rice {
1100fe938c08SJustin Hibbits 	vm_offset_t off, pa = VM_PAGE_TO_PHYS(m);
11015244eac9SBenno Rice 
1102fe938c08SJustin Hibbits 	for (off = 0; off < PAGE_SIZE; off += cacheline_size)
1103fe938c08SJustin Hibbits 		__asm __volatile("dcbz 0,%0" :: "r"(pa + off));
11045244eac9SBenno Rice }
11055244eac9SBenno Rice 
11065244eac9SBenno Rice void
110759276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
11085244eac9SBenno Rice {
11093495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
11105b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
11113495845eSBenno Rice 
11125b43c63dSMarcel Moolenaar 	bzero(va, size);
11135244eac9SBenno Rice }
11145244eac9SBenno Rice 
1115a58b3a68SPeter Wemm void
111659276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1117a58b3a68SPeter Wemm {
1118a58b3a68SPeter Wemm 
1119fe938c08SJustin Hibbits 	moea_zero_page(mmu, m);
1120a58b3a68SPeter Wemm }
1121a58b3a68SPeter Wemm 
11225244eac9SBenno Rice /*
11235244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
11245244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
11255244eac9SBenno Rice  * will be wired down.
11265244eac9SBenno Rice  */
11275244eac9SBenno Rice void
112859276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
11295244eac9SBenno Rice 	   boolean_t wired)
11305244eac9SBenno Rice {
1131ce142d9eSAlan Cox 
11323653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1133ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
113467c867eeSAlan Cox 	moea_enter_locked(pmap, va, m, prot, wired);
11353653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1136ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1137ce142d9eSAlan Cox }
1138ce142d9eSAlan Cox 
1139ce142d9eSAlan Cox /*
1140ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1141ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1142ce142d9eSAlan Cox  * will be wired down.
1143ce142d9eSAlan Cox  *
1144*f26bcf99SAlan Cox  * The global pvh and pmap must be locked.
1145ce142d9eSAlan Cox  */
1146ce142d9eSAlan Cox static void
1147ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1148ce142d9eSAlan Cox     boolean_t wired)
1149ce142d9eSAlan Cox {
11505244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1151378862a7SJeff Roberson 	uma_zone_t	zone;
115257bd5cceSNathan Whitehorn 	u_int		pte_lo, pvo_flags;
11535244eac9SBenno Rice 	int		error;
11545244eac9SBenno Rice 
115559276937SPeter Grehan 	if (!moea_initialized) {
115659276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
115759276937SPeter Grehan 		zone = moea_upvo_zone;
11585244eac9SBenno Rice 		pvo_flags = 0;
11595244eac9SBenno Rice 	} else {
116003b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
116159276937SPeter Grehan 		zone = moea_mpvo_zone;
11625244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
11635244eac9SBenno Rice 	}
1164f489bf21SAlan Cox 	if (pmap_bootstrapped)
11653653f5cbSAlan Cox 		rw_assert(&pvh_global_lock, RA_WLOCKED);
1166ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1167c7aebda8SAttilio Rao 	if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
1168658f180bSAlan Cox 		VM_OBJECT_ASSERT_LOCKED(m->object);
11695244eac9SBenno Rice 
1170*f26bcf99SAlan Cox 	/* XXX change the pvo head for unmanaged pages */
1171d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1172a130b35fSNathan Whitehorn 		pvo_flags &= ~PVO_MANAGED;
117359276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
1174a130b35fSNathan Whitehorn 		zone = moea_upvo_zone;
1175a130b35fSNathan Whitehorn 	}
11764dba5df1SPeter Grehan 
1177cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11785244eac9SBenno Rice 
117944b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11805244eac9SBenno Rice 		pte_lo |= PTE_BW;
11812368a371SAlan Cox 		if (pmap_bootstrapped &&
1182d98d0ce2SKonstantin Belousov 		    (m->oflags & VPO_UNMANAGED) == 0)
11833407fefeSKonstantin Belousov 			vm_page_aflag_set(m, PGA_WRITEABLE);
118444b8bd66SAlan Cox 	} else
11855244eac9SBenno Rice 		pte_lo |= PTE_BR;
11865244eac9SBenno Rice 
11874dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
11884dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
11895244eac9SBenno Rice 
11905244eac9SBenno Rice 	if (wired)
11915244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11925244eac9SBenno Rice 
119359276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11948207b362SBenno Rice 	    pte_lo, pvo_flags);
11955244eac9SBenno Rice 
11968207b362SBenno Rice 	/*
119757bd5cceSNathan Whitehorn 	 * Flush the real page from the instruction cache. This has be done
119857bd5cceSNathan Whitehorn 	 * for all user mappings to prevent information leakage via the
1199805bee55SNathan Whitehorn 	 * instruction cache. moea_pvo_enter() returns ENOENT for the first
1200805bee55SNathan Whitehorn 	 * mapping for a page.
12018207b362SBenno Rice 	 */
1202805bee55SNathan Whitehorn 	if (pmap != kernel_pmap && error == ENOENT &&
1203805bee55SNathan Whitehorn 	    (pte_lo & (PTE_I | PTE_G)) == 0)
120459276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1205ce142d9eSAlan Cox }
1206ce142d9eSAlan Cox 
1207ce142d9eSAlan Cox /*
1208ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1209ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1210ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1211ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1212ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1213ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1214ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1215ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1216ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1217ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1218ce142d9eSAlan Cox  */
1219ce142d9eSAlan Cox void
1220ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1221ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1222ce142d9eSAlan Cox {
1223ce142d9eSAlan Cox 	vm_page_t m;
1224ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1225ce142d9eSAlan Cox 
12269af6d512SAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(m_start->object);
12279af6d512SAttilio Rao 
1228ce142d9eSAlan Cox 	psize = atop(end - start);
1229ce142d9eSAlan Cox 	m = m_start;
12303653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1231ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1232ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1233ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1234ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1235ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1236ce142d9eSAlan Cox 	}
12373653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1238ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
12395244eac9SBenno Rice }
12405244eac9SBenno Rice 
12412053c127SStephan Uphoff void
124259276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
12432053c127SStephan Uphoff     vm_prot_t prot)
1244dca96f1aSAlan Cox {
1245dca96f1aSAlan Cox 
12463653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1247ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1248ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
124959276937SPeter Grehan 	    FALSE);
12503653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1251ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1252dca96f1aSAlan Cox }
1253dca96f1aSAlan Cox 
125456b09388SAlan Cox vm_paddr_t
125559276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
12565244eac9SBenno Rice {
12570f92104cSBenno Rice 	struct	pvo_entry *pvo;
125848d0b1a0SAlan Cox 	vm_paddr_t pa;
12590f92104cSBenno Rice 
126048d0b1a0SAlan Cox 	PMAP_LOCK(pm);
126159276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
126248d0b1a0SAlan Cox 	if (pvo == NULL)
126348d0b1a0SAlan Cox 		pa = 0;
126448d0b1a0SAlan Cox 	else
126552a7870dSNathan Whitehorn 		pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
126648d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
126748d0b1a0SAlan Cox 	return (pa);
12685244eac9SBenno Rice }
12695244eac9SBenno Rice 
12705244eac9SBenno Rice /*
127184792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
127284792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
127384792e72SPeter Grehan  * protection.
127484792e72SPeter Grehan  */
127584792e72SPeter Grehan vm_page_t
127659276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
127784792e72SPeter Grehan {
1278ab50a262SAlan Cox 	struct	pvo_entry *pvo;
127984792e72SPeter Grehan 	vm_page_t m;
12802965a453SKip Macy         vm_paddr_t pa;
128184792e72SPeter Grehan 
128284792e72SPeter Grehan 	m = NULL;
12832965a453SKip Macy 	pa = 0;
128448d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
12852965a453SKip Macy retry:
128659276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
128752a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
128852a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1289ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
12902965a453SKip Macy 		if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa))
12912965a453SKip Macy 			goto retry;
129252a7870dSNathan Whitehorn 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
129384792e72SPeter Grehan 		vm_page_hold(m);
129484792e72SPeter Grehan 	}
12952965a453SKip Macy 	PA_UNLOCK_COND(pa);
129648d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
129784792e72SPeter Grehan 	return (m);
129884792e72SPeter Grehan }
129984792e72SPeter Grehan 
13005244eac9SBenno Rice void
130159276937SPeter Grehan moea_init(mmu_t mmu)
13025244eac9SBenno Rice {
13035244eac9SBenno Rice 
130459276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
13050ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
13060ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
130759276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
13080ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
13090ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
131059276937SPeter Grehan 	moea_initialized = TRUE;
13115244eac9SBenno Rice }
13125244eac9SBenno Rice 
13135244eac9SBenno Rice boolean_t
13147b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m)
13157b85f591SAlan Cox {
13168d9e6d9fSAlan Cox 	boolean_t rv;
13177b85f591SAlan Cox 
1318d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1319c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
13208d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
13218d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_REF);
13228d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13238d9e6d9fSAlan Cox 	return (rv);
13247b85f591SAlan Cox }
13257b85f591SAlan Cox 
13267b85f591SAlan Cox boolean_t
132759276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
13285244eac9SBenno Rice {
13298d9e6d9fSAlan Cox 	boolean_t rv;
13300f92104cSBenno Rice 
1331d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1332567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1333567e51e1SAlan Cox 
1334567e51e1SAlan Cox 	/*
1335c7aebda8SAttilio Rao 	 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
13363407fefeSKonstantin Belousov 	 * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
1337567e51e1SAlan Cox 	 * is clear, no PTEs can have PTE_CHG set.
1338567e51e1SAlan Cox 	 */
133989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(m->object);
1340c7aebda8SAttilio Rao 	if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
13410f92104cSBenno Rice 		return (FALSE);
13428d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
13438d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_CHG);
13448d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13458d9e6d9fSAlan Cox 	return (rv);
1346566526a9SAlan Cox }
1347566526a9SAlan Cox 
1348e396eb60SAlan Cox boolean_t
1349e396eb60SAlan Cox moea_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1350e396eb60SAlan Cox {
1351e396eb60SAlan Cox 	struct pvo_entry *pvo;
1352e396eb60SAlan Cox 	boolean_t rv;
1353e396eb60SAlan Cox 
1354e396eb60SAlan Cox 	PMAP_LOCK(pmap);
1355e396eb60SAlan Cox 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1356e396eb60SAlan Cox 	rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0;
1357e396eb60SAlan Cox 	PMAP_UNLOCK(pmap);
1358e396eb60SAlan Cox 	return (rv);
1359e396eb60SAlan Cox }
1360e396eb60SAlan Cox 
13615244eac9SBenno Rice void
136259276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
136303b6e025SPeter Grehan {
136403b6e025SPeter Grehan 
1365d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1366567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
136789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(m->object);
1368c7aebda8SAttilio Rao 	KASSERT(!vm_page_xbusied(m),
1369c7aebda8SAttilio Rao 	    ("moea_clear_modify: page %p is exclusive busy", m));
1370567e51e1SAlan Cox 
1371567e51e1SAlan Cox 	/*
13723407fefeSKonstantin Belousov 	 * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_CHG
1373567e51e1SAlan Cox 	 * set.  If the object containing the page is locked and the page is
1374c7aebda8SAttilio Rao 	 * not exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
1375567e51e1SAlan Cox 	 */
13763407fefeSKonstantin Belousov 	if ((m->aflags & PGA_WRITEABLE) == 0)
137703b6e025SPeter Grehan 		return;
13788d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1379ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
13808d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13815244eac9SBenno Rice }
13825244eac9SBenno Rice 
13837f3a4093SMike Silbersack /*
138478985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
138578985e42SAlan Cox  */
138678985e42SAlan Cox void
138778985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
138878985e42SAlan Cox {
138978985e42SAlan Cox 	struct	pvo_entry *pvo;
139078985e42SAlan Cox 	struct	pte *pt;
139178985e42SAlan Cox 	pmap_t	pmap;
139278985e42SAlan Cox 	u_int	lo;
139378985e42SAlan Cox 
1394d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
13959ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
13969ab6032fSAlan Cox 
13979ab6032fSAlan Cox 	/*
1398c7aebda8SAttilio Rao 	 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
1399c7aebda8SAttilio Rao 	 * set by another thread while the object is locked.  Thus,
1400c7aebda8SAttilio Rao 	 * if PGA_WRITEABLE is clear, no page table entries need updating.
14019ab6032fSAlan Cox 	 */
140289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(m->object);
1403c7aebda8SAttilio Rao 	if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
140478985e42SAlan Cox 		return;
14053653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
140678985e42SAlan Cox 	lo = moea_attr_fetch(m);
1407e4f72b32SMarcel Moolenaar 	powerpc_sync();
140878985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
140978985e42SAlan Cox 		pmap = pvo->pvo_pmap;
141078985e42SAlan Cox 		PMAP_LOCK(pmap);
141152a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
141278985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
141352a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
141452a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
141578985e42SAlan Cox 			if (pt != NULL) {
141652a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
141752a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
141852a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
141952a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
142078985e42SAlan Cox 				    pvo->pvo_vaddr);
142178985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
142278985e42SAlan Cox 			}
142378985e42SAlan Cox 		}
142478985e42SAlan Cox 		PMAP_UNLOCK(pmap);
142578985e42SAlan Cox 	}
142678985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
142778985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
142878985e42SAlan Cox 		vm_page_dirty(m);
142978985e42SAlan Cox 	}
14303407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
14313653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
143278985e42SAlan Cox }
143378985e42SAlan Cox 
143478985e42SAlan Cox /*
143559276937SPeter Grehan  *	moea_ts_referenced:
14367f3a4093SMike Silbersack  *
14377f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
14387f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
14397f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
14407f3a4093SMike Silbersack  *	reference bits set.
14417f3a4093SMike Silbersack  *
14427f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
14437f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
14447f3a4093SMike Silbersack  *	optimal aging of shared pages.
14457f3a4093SMike Silbersack  */
14468d9e6d9fSAlan Cox int
144759276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
14485244eac9SBenno Rice {
14498d9e6d9fSAlan Cox 	int count;
145003b6e025SPeter Grehan 
1451d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1452ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
14538d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
14548d9e6d9fSAlan Cox 	count = moea_clear_bit(m, PTE_REF);
14558d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
14568d9e6d9fSAlan Cox 	return (count);
14575244eac9SBenno Rice }
14585244eac9SBenno Rice 
14595244eac9SBenno Rice /*
1460c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1461c1f4123bSNathan Whitehorn  */
1462c1f4123bSNathan Whitehorn void
1463c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma)
1464c1f4123bSNathan Whitehorn {
1465c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1466cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1467c1f4123bSNathan Whitehorn 	struct	pte *pt;
1468c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1469c1f4123bSNathan Whitehorn 	u_int	lo;
1470c1f4123bSNathan Whitehorn 
1471d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1472cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1473cd6a97f0SNathan Whitehorn 		return;
1474cd6a97f0SNathan Whitehorn 	}
1475cd6a97f0SNathan Whitehorn 
14763653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1477cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1478c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1479cd6a97f0SNathan Whitehorn 
1480cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1481c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1482c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1483c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1484c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1485c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1486c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1487c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1488c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1489c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1490c1f4123bSNathan Whitehorn 				isync();
1491c1f4123bSNathan Whitehorn 		}
1492c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1493c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1494c1f4123bSNathan Whitehorn 	}
1495c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
14963653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1497c1f4123bSNathan Whitehorn }
1498c1f4123bSNathan Whitehorn 
1499c1f4123bSNathan Whitehorn /*
15005244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
15015244eac9SBenno Rice  */
15025244eac9SBenno Rice void
150320b79612SRafal Jaworowski moea_kenter(mmu_t mmu, vm_offset_t va, vm_paddr_t pa)
15045244eac9SBenno Rice {
1505c1f4123bSNathan Whitehorn 
1506c1f4123bSNathan Whitehorn 	moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1507c1f4123bSNathan Whitehorn }
1508c1f4123bSNathan Whitehorn 
1509c1f4123bSNathan Whitehorn void
1510c1f4123bSNathan Whitehorn moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma)
1511c1f4123bSNathan Whitehorn {
15125244eac9SBenno Rice 	u_int		pte_lo;
15135244eac9SBenno Rice 	int		error;
15145244eac9SBenno Rice 
15155244eac9SBenno Rice #if 0
15165244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
151759276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
15185244eac9SBenno Rice 		    va);
15195244eac9SBenno Rice #endif
15205244eac9SBenno Rice 
1521c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
15225244eac9SBenno Rice 
15234711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
152459276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
152559276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
15265244eac9SBenno Rice 
15275244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
152859276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
15295244eac9SBenno Rice 		    pa, error);
15305244eac9SBenno Rice 
15314711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
15325244eac9SBenno Rice }
15335244eac9SBenno Rice 
1534e79f59e8SBenno Rice /*
1535e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1536e79f59e8SBenno Rice  * address.
1537e79f59e8SBenno Rice  */
153820b79612SRafal Jaworowski vm_paddr_t
153959276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
15405244eac9SBenno Rice {
1541e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
154248d0b1a0SAlan Cox 	vm_paddr_t pa;
1543e79f59e8SBenno Rice 
15440efd0097SPeter Grehan 	/*
154552a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
15460efd0097SPeter Grehan 	 */
15470efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
15480efd0097SPeter Grehan 		return (va);
15490efd0097SPeter Grehan 	}
15500efd0097SPeter Grehan 
155148d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
155259276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
155359276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
155452a7870dSNathan Whitehorn 	pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
155548d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
155648d0b1a0SAlan Cox 	return (pa);
1557e79f59e8SBenno Rice }
1558e79f59e8SBenno Rice 
155988afb2a3SBenno Rice /*
156088afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
156188afb2a3SBenno Rice  */
15625244eac9SBenno Rice void
156359276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
15645244eac9SBenno Rice {
156588afb2a3SBenno Rice 
156659276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
15675244eac9SBenno Rice }
15685244eac9SBenno Rice 
15695244eac9SBenno Rice /*
15705244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
15715244eac9SBenno Rice  *
15725244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
15735244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
15745244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
15755244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
15765244eac9SBenno Rice  * first usable address after the mapped region.
15775244eac9SBenno Rice  */
15785244eac9SBenno Rice vm_offset_t
157920b79612SRafal Jaworowski moea_map(mmu_t mmu, vm_offset_t *virt, vm_paddr_t pa_start,
158020b79612SRafal Jaworowski     vm_paddr_t pa_end, int prot)
15815244eac9SBenno Rice {
15825244eac9SBenno Rice 	vm_offset_t	sva, va;
15835244eac9SBenno Rice 
15845244eac9SBenno Rice 	sva = *virt;
15855244eac9SBenno Rice 	va = sva;
15865244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
158759276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
15885244eac9SBenno Rice 	*virt = va;
15895244eac9SBenno Rice 	return (sva);
15905244eac9SBenno Rice }
15915244eac9SBenno Rice 
15925244eac9SBenno Rice /*
15937f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
15947f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
15957f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
15967f3a4093SMike Silbersack  * is only necessary that true be returned for a small
15977f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
15987f3a4093SMike Silbersack  */
15995244eac9SBenno Rice boolean_t
160059276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
16015244eac9SBenno Rice {
160203b6e025SPeter Grehan         int loops;
160303b6e025SPeter Grehan 	struct pvo_entry *pvo;
1604ce186587SAlan Cox 	boolean_t rv;
160503b6e025SPeter Grehan 
1606d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1607ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
160803b6e025SPeter Grehan 	loops = 0;
1609ce186587SAlan Cox 	rv = FALSE;
16103653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
161103b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1612ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1613ce186587SAlan Cox 			rv = TRUE;
1614ce186587SAlan Cox 			break;
1615ce186587SAlan Cox 		}
161603b6e025SPeter Grehan 		if (++loops >= 16)
161703b6e025SPeter Grehan 			break;
161803b6e025SPeter Grehan 	}
16193653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1620ce186587SAlan Cox 	return (rv);
16215244eac9SBenno Rice }
16225244eac9SBenno Rice 
162359677d3cSAlan Cox /*
162459677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
162559677d3cSAlan Cox  * that are wired.
162659677d3cSAlan Cox  */
162759677d3cSAlan Cox int
162859677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
162959677d3cSAlan Cox {
163059677d3cSAlan Cox 	struct pvo_entry *pvo;
163159677d3cSAlan Cox 	int count;
163259677d3cSAlan Cox 
163359677d3cSAlan Cox 	count = 0;
1634d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0)
163559677d3cSAlan Cox 		return (count);
16363653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
163759677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
163859677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
163959677d3cSAlan Cox 			count++;
16403653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
164159677d3cSAlan Cox 	return (count);
164259677d3cSAlan Cox }
164359677d3cSAlan Cox 
164459276937SPeter Grehan static u_int	moea_vsidcontext;
16455244eac9SBenno Rice 
16465244eac9SBenno Rice void
164759276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
16485244eac9SBenno Rice {
16495244eac9SBenno Rice 	int	i, mask;
16505244eac9SBenno Rice 	u_int	entropy;
16515244eac9SBenno Rice 
165259276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
1653ccc4a5c7SNathan Whitehorn 	RB_INIT(&pmap->pmap_pvo);
16544daf20b2SPeter Grehan 
16555244eac9SBenno Rice 	entropy = 0;
16565244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
16575244eac9SBenno Rice 
165852a7870dSNathan Whitehorn 	if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
165952a7870dSNathan Whitehorn 	    == NULL) {
166052a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
166152a7870dSNathan Whitehorn 	}
166252a7870dSNathan Whitehorn 
166352a7870dSNathan Whitehorn 
1664e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
16655244eac9SBenno Rice 	/*
16665244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
16675244eac9SBenno Rice 	 */
16685244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
16695244eac9SBenno Rice 		u_int	hash, n;
16705244eac9SBenno Rice 
16715244eac9SBenno Rice 		/*
16725244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
16735244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
16745244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
16755244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
16765244eac9SBenno Rice 		 * instead of a multiply.)
16775244eac9SBenno Rice 		 */
167859276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
167959276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
16805244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
16815244eac9SBenno Rice 			continue;
16825244eac9SBenno Rice 		n = hash >> 5;
16835244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
168459276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
168559276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
16865244eac9SBenno Rice 			/* anything free in this bucket? */
168759276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
168859276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
16895244eac9SBenno Rice 				continue;
16905244eac9SBenno Rice 			}
16910dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
16925244eac9SBenno Rice 			mask = 1 << i;
16935244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
16945244eac9SBenno Rice 			hash |= i;
16955244eac9SBenno Rice 		}
169646e93cbbSNathan Whitehorn 		KASSERT(!(moea_vsid_bitmap[n] & mask),
169746e93cbbSNathan Whitehorn 		    ("Allocating in-use VSID group %#x\n", hash));
169859276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
16995244eac9SBenno Rice 		for (i = 0; i < 16; i++)
17005244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1701e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
17025244eac9SBenno Rice 		return;
17035244eac9SBenno Rice 	}
17045244eac9SBenno Rice 
1705e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
170659276937SPeter Grehan 	panic("moea_pinit: out of segments");
17075244eac9SBenno Rice }
17085244eac9SBenno Rice 
17095244eac9SBenno Rice /*
17105244eac9SBenno Rice  * Initialize the pmap associated with process 0.
17115244eac9SBenno Rice  */
17125244eac9SBenno Rice void
171359276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
17145244eac9SBenno Rice {
17155244eac9SBenno Rice 
1716e68c64f0SKonstantin Belousov 	PMAP_LOCK_INIT(pm);
171759276937SPeter Grehan 	moea_pinit(mmu, pm);
17185244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
17195244eac9SBenno Rice }
17205244eac9SBenno Rice 
1721e79f59e8SBenno Rice /*
1722e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1723e79f59e8SBenno Rice  */
17245244eac9SBenno Rice void
172559276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
172659276937SPeter Grehan     vm_prot_t prot)
17275244eac9SBenno Rice {
1728ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
1729e79f59e8SBenno Rice 	struct	pte *pt;
1730e79f59e8SBenno Rice 
1731e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
173259276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1733e79f59e8SBenno Rice 
1734e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
173559276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1736e79f59e8SBenno Rice 		return;
1737e79f59e8SBenno Rice 	}
1738e79f59e8SBenno Rice 
17393653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
174048d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1741ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1742ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1743ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1744ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1745e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1746e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1747e79f59e8SBenno Rice 
1748e79f59e8SBenno Rice 		/*
1749e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1750e79f59e8SBenno Rice 		 * copy.
1751e79f59e8SBenno Rice 		 */
1752ccc4a5c7SNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1753e79f59e8SBenno Rice 		/*
1754e79f59e8SBenno Rice 		 * Change the protection of the page.
1755e79f59e8SBenno Rice 		 */
175652a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
175752a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1758e79f59e8SBenno Rice 
1759e79f59e8SBenno Rice 		/*
1760e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1761e79f59e8SBenno Rice 		 */
1762d644a0b7SAlan Cox 		if (pt != NULL) {
176352a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1764d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1765d644a0b7SAlan Cox 		}
1766e79f59e8SBenno Rice 	}
17673653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
176848d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
17695244eac9SBenno Rice }
17705244eac9SBenno Rice 
177188afb2a3SBenno Rice /*
177288afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
177388afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
177488afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
177588afb2a3SBenno Rice  */
17765244eac9SBenno Rice void
177759276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
17785244eac9SBenno Rice {
177903b6e025SPeter Grehan 	vm_offset_t va;
17805244eac9SBenno Rice 
178103b6e025SPeter Grehan 	va = sva;
178203b6e025SPeter Grehan 	while (count-- > 0) {
178359276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
178403b6e025SPeter Grehan 		va += PAGE_SIZE;
178503b6e025SPeter Grehan 		m++;
178603b6e025SPeter Grehan 	}
17875244eac9SBenno Rice }
17885244eac9SBenno Rice 
178988afb2a3SBenno Rice /*
179088afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
179159276937SPeter Grehan  * temporary mappings entered by moea_qenter.
179288afb2a3SBenno Rice  */
17935244eac9SBenno Rice void
179459276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
17955244eac9SBenno Rice {
179603b6e025SPeter Grehan 	vm_offset_t va;
179788afb2a3SBenno Rice 
179803b6e025SPeter Grehan 	va = sva;
179903b6e025SPeter Grehan 	while (count-- > 0) {
180059276937SPeter Grehan 		moea_kremove(mmu, va);
180103b6e025SPeter Grehan 		va += PAGE_SIZE;
180203b6e025SPeter Grehan 	}
18035244eac9SBenno Rice }
18045244eac9SBenno Rice 
18055244eac9SBenno Rice void
180659276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
18075244eac9SBenno Rice {
180832bc7846SPeter Grehan         int idx, mask;
180932bc7846SPeter Grehan 
181032bc7846SPeter Grehan 	/*
181132bc7846SPeter Grehan 	 * Free segment register's VSID
181232bc7846SPeter Grehan 	 */
181332bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
181459276937SPeter Grehan                 panic("moea_release");
181532bc7846SPeter Grehan 
1816e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
181732bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
181832bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
181932bc7846SPeter Grehan         idx /= VSID_NBPW;
182059276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1821e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
18225244eac9SBenno Rice }
18235244eac9SBenno Rice 
182488afb2a3SBenno Rice /*
182588afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
182688afb2a3SBenno Rice  */
18275244eac9SBenno Rice void
182859276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
18295244eac9SBenno Rice {
1830ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
183188afb2a3SBenno Rice 
18323653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
183348d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1834ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1835ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1836ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1837ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1838598d99ddSNathan Whitehorn 		moea_pvo_remove(pvo, -1);
1839598d99ddSNathan Whitehorn 	}
184048d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
18413653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
18425244eac9SBenno Rice }
18435244eac9SBenno Rice 
1844e79f59e8SBenno Rice /*
184559276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
184603b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
184703b6e025SPeter Grehan  */
184803b6e025SPeter Grehan void
184959276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
185003b6e025SPeter Grehan {
185103b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
185203b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
185348d0b1a0SAlan Cox 	pmap_t	pmap;
185403b6e025SPeter Grehan 
18553653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
185603b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
185703b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
185803b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
185903b6e025SPeter Grehan 
186048d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
186148d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
186259276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
186348d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
186403b6e025SPeter Grehan 	}
18658d9e6d9fSAlan Cox 	if ((m->aflags & PGA_WRITEABLE) && moea_query_bit(m, PTE_CHG)) {
1866c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1867062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1868062c8f4cSNathan Whitehorn 	}
18693407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
18703653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
187103b6e025SPeter Grehan }
187203b6e025SPeter Grehan 
187303b6e025SPeter Grehan /*
18745244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
187559276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
18765244eac9SBenno Rice  * calculated.
18775244eac9SBenno Rice  */
18785244eac9SBenno Rice static vm_offset_t
187959276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
18805244eac9SBenno Rice {
18815244eac9SBenno Rice 	vm_offset_t	s, e;
18825244eac9SBenno Rice 	int		i, j;
18835244eac9SBenno Rice 
18845244eac9SBenno Rice 	size = round_page(size);
18855244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
18865244eac9SBenno Rice 		if (align != 0)
18875244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
18885244eac9SBenno Rice 		else
18895244eac9SBenno Rice 			s = phys_avail[i];
18905244eac9SBenno Rice 		e = s + size;
18915244eac9SBenno Rice 
18925244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
18935244eac9SBenno Rice 			continue;
18945244eac9SBenno Rice 
18955244eac9SBenno Rice 		if (s == phys_avail[i]) {
18965244eac9SBenno Rice 			phys_avail[i] += size;
18975244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
18985244eac9SBenno Rice 			phys_avail[i + 1] -= size;
18995244eac9SBenno Rice 		} else {
19005244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
19015244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
19025244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
19035244eac9SBenno Rice 			}
19045244eac9SBenno Rice 
19055244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
19065244eac9SBenno Rice 			phys_avail[i + 1] = s;
19075244eac9SBenno Rice 			phys_avail[i + 2] = e;
19085244eac9SBenno Rice 			phys_avail_count++;
19095244eac9SBenno Rice 		}
19105244eac9SBenno Rice 
19115244eac9SBenno Rice 		return (s);
19125244eac9SBenno Rice 	}
191359276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
19145244eac9SBenno Rice }
19155244eac9SBenno Rice 
19165244eac9SBenno Rice static void
191759276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
19185244eac9SBenno Rice {
19195244eac9SBenno Rice 	__syncicache((void *)pa, len);
19205244eac9SBenno Rice }
19215244eac9SBenno Rice 
19225244eac9SBenno Rice static int
192359276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
19245244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
19255244eac9SBenno Rice {
19265244eac9SBenno Rice 	struct	pvo_entry *pvo;
19275244eac9SBenno Rice 	u_int	sr;
19285244eac9SBenno Rice 	int	first;
19295244eac9SBenno Rice 	u_int	ptegidx;
19305244eac9SBenno Rice 	int	i;
193132bc7846SPeter Grehan 	int     bootstrap;
19325244eac9SBenno Rice 
193359276937SPeter Grehan 	moea_pvo_enter_calls++;
19348207b362SBenno Rice 	first = 0;
193532bc7846SPeter Grehan 	bootstrap = 0;
193632bc7846SPeter Grehan 
19375244eac9SBenno Rice 	/*
19385244eac9SBenno Rice 	 * Compute the PTE Group index.
19395244eac9SBenno Rice 	 */
19405244eac9SBenno Rice 	va &= ~ADDR_POFF;
19415244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
19425244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
19435244eac9SBenno Rice 
19445244eac9SBenno Rice 	/*
19455244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
19465244eac9SBenno Rice 	 * there is a mapping.
19475244eac9SBenno Rice 	 */
194859276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
194959276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19505244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
195152a7870dSNathan Whitehorn 			if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
195252a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1953fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
195459276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
195549f8f727SBenno Rice 				return (0);
1956fafc7362SBenno Rice 			}
195759276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
19585244eac9SBenno Rice 			break;
19595244eac9SBenno Rice 		}
19605244eac9SBenno Rice 	}
19615244eac9SBenno Rice 
19625244eac9SBenno Rice 	/*
19635244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
19645244eac9SBenno Rice 	 */
196559276937SPeter Grehan 	if (moea_initialized) {
1966378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
196749f8f727SBenno Rice 	} else {
196859276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
196959276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
197059276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
19710d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
197249f8f727SBenno Rice 		}
197359276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
197459276937SPeter Grehan 		moea_bpvo_pool_index++;
197532bc7846SPeter Grehan 		bootstrap = 1;
197649f8f727SBenno Rice 	}
19775244eac9SBenno Rice 
19785244eac9SBenno Rice 	if (pvo == NULL) {
197959276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
19805244eac9SBenno Rice 		return (ENOMEM);
19815244eac9SBenno Rice 	}
19825244eac9SBenno Rice 
198359276937SPeter Grehan 	moea_pvo_entries++;
19845244eac9SBenno Rice 	pvo->pvo_vaddr = va;
19855244eac9SBenno Rice 	pvo->pvo_pmap = pm;
198659276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
19875244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
19885244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
19895244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
19905244eac9SBenno Rice 	if (flags & PVO_WIRED)
19915244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
199259276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
19935244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
199432bc7846SPeter Grehan 	if (bootstrap)
199532bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
19964dba5df1SPeter Grehan 
199752a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
19985244eac9SBenno Rice 
19995244eac9SBenno Rice 	/*
2000598d99ddSNathan Whitehorn 	 * Add to pmap list
2001598d99ddSNathan Whitehorn 	 */
2002ccc4a5c7SNathan Whitehorn 	RB_INSERT(pvo_tree, &pm->pmap_pvo, pvo);
2003598d99ddSNathan Whitehorn 
2004598d99ddSNathan Whitehorn 	/*
20055244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
20065244eac9SBenno Rice 	 * item.
20075244eac9SBenno Rice 	 */
20088207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
20098207b362SBenno Rice 		first = 1;
20105244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
20114dba5df1SPeter Grehan 
2012bfc30490SAlan Cox 	if (pvo->pvo_vaddr & PVO_WIRED)
2013c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
2014c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
20155244eac9SBenno Rice 
201652a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
2017804d1cc1SJustin Hibbits 	KASSERT(i < 8, ("Invalid PTE index"));
20185244eac9SBenno Rice 	if (i >= 0) {
20195244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
20205244eac9SBenno Rice 	} else {
202159276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
202259276937SPeter Grehan 		moea_pte_overflow++;
20235244eac9SBenno Rice 	}
202459276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20254dba5df1SPeter Grehan 
20265244eac9SBenno Rice 	return (first ? ENOENT : 0);
20275244eac9SBenno Rice }
20285244eac9SBenno Rice 
20295244eac9SBenno Rice static void
203059276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
20315244eac9SBenno Rice {
20325244eac9SBenno Rice 	struct	pte *pt;
20335244eac9SBenno Rice 
20345244eac9SBenno Rice 	/*
20355244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
20365244eac9SBenno Rice 	 * save the ref & cfg bits).
20375244eac9SBenno Rice 	 */
203859276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
20395244eac9SBenno Rice 	if (pt != NULL) {
204052a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
2041d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
20425244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
20435244eac9SBenno Rice 	} else {
204459276937SPeter Grehan 		moea_pte_overflow--;
20455244eac9SBenno Rice 	}
20465244eac9SBenno Rice 
20475244eac9SBenno Rice 	/*
20485244eac9SBenno Rice 	 * Update our statistics.
20495244eac9SBenno Rice 	 */
20505244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
2051bfc30490SAlan Cox 	if (pvo->pvo_vaddr & PVO_WIRED)
20525244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
20535244eac9SBenno Rice 
20545244eac9SBenno Rice 	/*
20555244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
20565244eac9SBenno Rice 	 */
2057d98d0ce2SKonstantin Belousov 	if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED) {
20585244eac9SBenno Rice 		struct	vm_page *pg;
20595244eac9SBenno Rice 
206052a7870dSNathan Whitehorn 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
20615244eac9SBenno Rice 		if (pg != NULL) {
206252a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
20635244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
20645244eac9SBenno Rice 		}
20655244eac9SBenno Rice 	}
20665244eac9SBenno Rice 
20675244eac9SBenno Rice 	/*
2068598d99ddSNathan Whitehorn 	 * Remove this PVO from the PV and pmap lists.
20695244eac9SBenno Rice 	 */
20705244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
2071ccc4a5c7SNathan Whitehorn 	RB_REMOVE(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo);
20725244eac9SBenno Rice 
20735244eac9SBenno Rice 	/*
20745244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
20755244eac9SBenno Rice 	 * if we aren't going to reuse it.
20765244eac9SBenno Rice 	 */
20775244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
207849f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
207959276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
208059276937SPeter Grehan 		    moea_upvo_zone, pvo);
208159276937SPeter Grehan 	moea_pvo_entries--;
208259276937SPeter Grehan 	moea_pvo_remove_calls++;
20835244eac9SBenno Rice }
20845244eac9SBenno Rice 
20855244eac9SBenno Rice static __inline int
208659276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
20875244eac9SBenno Rice {
20885244eac9SBenno Rice 	int	pteidx;
20895244eac9SBenno Rice 
20905244eac9SBenno Rice 	/*
20915244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
20925244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
20935244eac9SBenno Rice 	 * noticing the HID bit.
20945244eac9SBenno Rice 	 */
20955244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
209652a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
209759276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
20985244eac9SBenno Rice 
20995244eac9SBenno Rice 	return (pteidx);
21005244eac9SBenno Rice }
21015244eac9SBenno Rice 
21025244eac9SBenno Rice static struct pvo_entry *
210359276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
21045244eac9SBenno Rice {
21055244eac9SBenno Rice 	struct	pvo_entry *pvo;
21065244eac9SBenno Rice 	int	ptegidx;
21075244eac9SBenno Rice 	u_int	sr;
21085244eac9SBenno Rice 
21095244eac9SBenno Rice 	va &= ~ADDR_POFF;
21105244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
21115244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
21125244eac9SBenno Rice 
211359276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
211459276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21155244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
21165244eac9SBenno Rice 			if (pteidx_p)
211759276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2118f489bf21SAlan Cox 			break;
21195244eac9SBenno Rice 		}
21205244eac9SBenno Rice 	}
212159276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
21225244eac9SBenno Rice 
2123f489bf21SAlan Cox 	return (pvo);
21245244eac9SBenno Rice }
21255244eac9SBenno Rice 
21265244eac9SBenno Rice static struct pte *
212759276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
21285244eac9SBenno Rice {
21295244eac9SBenno Rice 	struct	pte *pt;
21305244eac9SBenno Rice 
21315244eac9SBenno Rice 	/*
21325244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
21335244eac9SBenno Rice 	 */
21345244eac9SBenno Rice 	if (pteidx == -1) {
21355244eac9SBenno Rice 		int	ptegidx;
21365244eac9SBenno Rice 		u_int	sr;
21375244eac9SBenno Rice 
21385244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
21395244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
214059276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
21415244eac9SBenno Rice 	}
21425244eac9SBenno Rice 
214359276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2144d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
21455244eac9SBenno Rice 
214652a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
214759276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
21485244eac9SBenno Rice 		    "valid pte index", pvo);
21495244eac9SBenno Rice 	}
21505244eac9SBenno Rice 
215152a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
215259276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
21535244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
21545244eac9SBenno Rice 	}
21555244eac9SBenno Rice 
215652a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
215752a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
215859276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
215959276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
21605244eac9SBenno Rice 		}
21615244eac9SBenno Rice 
216252a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
21635244eac9SBenno Rice 		    != 0) {
216459276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
216559276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
21665244eac9SBenno Rice 		}
21675244eac9SBenno Rice 
2168d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
21695244eac9SBenno Rice 		return (pt);
21705244eac9SBenno Rice 	}
21715244eac9SBenno Rice 
217252a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
217359276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
2174804d1cc1SJustin Hibbits 		    "moea_pteg_table but valid in pvo: %8x, %8x", pvo, pt, pvo->pvo_pte.pte.pte_hi, pt->pte_hi);
21755244eac9SBenno Rice 	}
21765244eac9SBenno Rice 
2177d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
21785244eac9SBenno Rice 	return (NULL);
21795244eac9SBenno Rice }
21805244eac9SBenno Rice 
21815244eac9SBenno Rice /*
21825244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
21835244eac9SBenno Rice  */
21845244eac9SBenno Rice int
218559276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
21865244eac9SBenno Rice {
21875244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
21885244eac9SBenno Rice 	struct	pvo_entry *pvo;
21895244eac9SBenno Rice 	int	ptegidx, i, j;
21905244eac9SBenno Rice 	u_int	sr;
21915244eac9SBenno Rice 	struct	pteg *pteg;
21925244eac9SBenno Rice 	struct	pte *pt;
21935244eac9SBenno Rice 
219459276937SPeter Grehan 	moea_pte_spills++;
21955244eac9SBenno Rice 
2196d080d5fdSBenno Rice 	sr = mfsrin(addr);
21975244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
21985244eac9SBenno Rice 
21995244eac9SBenno Rice 	/*
22005244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
22015244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
22025244eac9SBenno Rice 	 */
220359276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
220459276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
22055244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
22065244eac9SBenno Rice 	i &= 7;
22075244eac9SBenno Rice 	pt = &pteg->pt[i];
22085244eac9SBenno Rice 
22095244eac9SBenno Rice 	source_pvo = NULL;
22105244eac9SBenno Rice 	victim_pvo = NULL;
221159276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
22125244eac9SBenno Rice 		/*
22135244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
22145244eac9SBenno Rice 		 */
22155244eac9SBenno Rice 		if (source_pvo == NULL &&
221652a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
221752a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
22185244eac9SBenno Rice 			/*
22195244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
22205244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
22215244eac9SBenno Rice 			 */
222252a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
22235244eac9SBenno Rice 
22245244eac9SBenno Rice 			if (j >= 0) {
22255244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
222659276937SPeter Grehan 				moea_pte_overflow--;
222759276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
22285244eac9SBenno Rice 				return (1);
22295244eac9SBenno Rice 			}
22305244eac9SBenno Rice 
22315244eac9SBenno Rice 			source_pvo = pvo;
22325244eac9SBenno Rice 
22335244eac9SBenno Rice 			if (victim_pvo != NULL)
22345244eac9SBenno Rice 				break;
22355244eac9SBenno Rice 		}
22365244eac9SBenno Rice 
22375244eac9SBenno Rice 		/*
22385244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
22395244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
22405244eac9SBenno Rice 		 */
22415244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
224252a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22435244eac9SBenno Rice 			victim_pvo = pvo;
22445244eac9SBenno Rice 			if (source_pvo != NULL)
22455244eac9SBenno Rice 				break;
22465244eac9SBenno Rice 		}
22475244eac9SBenno Rice 	}
22485244eac9SBenno Rice 
2249f489bf21SAlan Cox 	if (source_pvo == NULL) {
225059276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
22515244eac9SBenno Rice 		return (0);
2252f489bf21SAlan Cox 	}
22535244eac9SBenno Rice 
22545244eac9SBenno Rice 	if (victim_pvo == NULL) {
22555244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
225659276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
22575244eac9SBenno Rice 			    "entry", pt);
22585244eac9SBenno Rice 
22595244eac9SBenno Rice 		/*
22605244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
22615244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
22625244eac9SBenno Rice 		 */
226359276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
22645244eac9SBenno Rice 		    pvo_olink) {
22655244eac9SBenno Rice 			/*
22665244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
22675244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
22685244eac9SBenno Rice 			 */
226952a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22705244eac9SBenno Rice 				victim_pvo = pvo;
22715244eac9SBenno Rice 				break;
22725244eac9SBenno Rice 			}
22735244eac9SBenno Rice 		}
22745244eac9SBenno Rice 
22755244eac9SBenno Rice 		if (victim_pvo == NULL)
227659276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
22775244eac9SBenno Rice 			    "entry", pt);
22785244eac9SBenno Rice 	}
22795244eac9SBenno Rice 
22805244eac9SBenno Rice 	/*
22815244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
22825244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
22835244eac9SBenno Rice 	 * contained in the TLB entry.
22845244eac9SBenno Rice 	 */
228552a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
22865244eac9SBenno Rice 
228752a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
228852a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
22895244eac9SBenno Rice 
22905244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
22915244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
229259276937SPeter Grehan 	moea_pte_replacements++;
22935244eac9SBenno Rice 
229459276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22955244eac9SBenno Rice 	return (1);
22965244eac9SBenno Rice }
22975244eac9SBenno Rice 
2298804d1cc1SJustin Hibbits static __inline struct pvo_entry *
2299804d1cc1SJustin Hibbits moea_pte_spillable_ident(u_int ptegidx)
2300804d1cc1SJustin Hibbits {
2301804d1cc1SJustin Hibbits 	struct	pte *pt;
2302804d1cc1SJustin Hibbits 	struct	pvo_entry *pvo_walk, *pvo = NULL;
2303804d1cc1SJustin Hibbits 
2304804d1cc1SJustin Hibbits 	LIST_FOREACH(pvo_walk, &moea_pvo_table[ptegidx], pvo_olink) {
2305804d1cc1SJustin Hibbits 		if (pvo_walk->pvo_vaddr & PVO_WIRED)
2306804d1cc1SJustin Hibbits 			continue;
2307804d1cc1SJustin Hibbits 
2308804d1cc1SJustin Hibbits 		if (!(pvo_walk->pvo_pte.pte.pte_hi & PTE_VALID))
2309804d1cc1SJustin Hibbits 			continue;
2310804d1cc1SJustin Hibbits 
2311804d1cc1SJustin Hibbits 		pt = moea_pvo_to_pte(pvo_walk, -1);
2312804d1cc1SJustin Hibbits 
2313804d1cc1SJustin Hibbits 		if (pt == NULL)
2314804d1cc1SJustin Hibbits 			continue;
2315804d1cc1SJustin Hibbits 
2316804d1cc1SJustin Hibbits 		pvo = pvo_walk;
2317804d1cc1SJustin Hibbits 
2318804d1cc1SJustin Hibbits 		mtx_unlock(&moea_table_mutex);
2319804d1cc1SJustin Hibbits 		if (!(pt->pte_lo & PTE_REF))
2320804d1cc1SJustin Hibbits 			return (pvo_walk);
2321804d1cc1SJustin Hibbits 	}
2322804d1cc1SJustin Hibbits 
2323804d1cc1SJustin Hibbits 	return (pvo);
2324804d1cc1SJustin Hibbits }
2325804d1cc1SJustin Hibbits 
23265244eac9SBenno Rice static int
232759276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
23285244eac9SBenno Rice {
23295244eac9SBenno Rice 	struct	pte *pt;
2330804d1cc1SJustin Hibbits 	struct	pvo_entry *victim_pvo;
23315244eac9SBenno Rice 	int	i;
2332804d1cc1SJustin Hibbits 	int	victim_idx;
2333804d1cc1SJustin Hibbits 	u_int	pteg_bkpidx = ptegidx;
23345244eac9SBenno Rice 
2335d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2336d644a0b7SAlan Cox 
23375244eac9SBenno Rice 	/*
23385244eac9SBenno Rice 	 * First try primary hash.
23395244eac9SBenno Rice 	 */
234059276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
23415244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
23425244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
234359276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
23445244eac9SBenno Rice 			return (i);
23455244eac9SBenno Rice 		}
23465244eac9SBenno Rice 	}
23475244eac9SBenno Rice 
23485244eac9SBenno Rice 	/*
23495244eac9SBenno Rice 	 * Now try secondary hash.
23505244eac9SBenno Rice 	 */
235159276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2352bd8e6f87SPeter Grehan 
235359276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
23545244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
23555244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
235659276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
23575244eac9SBenno Rice 			return (i);
23585244eac9SBenno Rice 		}
23595244eac9SBenno Rice 	}
23605244eac9SBenno Rice 
2361804d1cc1SJustin Hibbits 	/* Try again, but this time try to force a PTE out. */
2362804d1cc1SJustin Hibbits 	ptegidx = pteg_bkpidx;
2363804d1cc1SJustin Hibbits 
2364804d1cc1SJustin Hibbits 	victim_pvo = moea_pte_spillable_ident(ptegidx);
2365804d1cc1SJustin Hibbits 	if (victim_pvo == NULL) {
2366804d1cc1SJustin Hibbits 		ptegidx ^= moea_pteg_mask;
2367804d1cc1SJustin Hibbits 		victim_pvo = moea_pte_spillable_ident(ptegidx);
2368804d1cc1SJustin Hibbits 	}
2369804d1cc1SJustin Hibbits 
2370804d1cc1SJustin Hibbits 	if (victim_pvo == NULL) {
237159276937SPeter Grehan 		panic("moea_pte_insert: overflow");
23725244eac9SBenno Rice 		return (-1);
23735244eac9SBenno Rice 	}
23745244eac9SBenno Rice 
2375804d1cc1SJustin Hibbits 	victim_idx = moea_pvo_pte_index(victim_pvo, ptegidx);
2376804d1cc1SJustin Hibbits 
2377804d1cc1SJustin Hibbits 	if (pteg_bkpidx == ptegidx)
2378804d1cc1SJustin Hibbits 		pvo_pt->pte_hi &= ~PTE_HID;
2379804d1cc1SJustin Hibbits 	else
2380804d1cc1SJustin Hibbits 		pvo_pt->pte_hi |= PTE_HID;
2381804d1cc1SJustin Hibbits 
2382804d1cc1SJustin Hibbits 	/*
2383804d1cc1SJustin Hibbits 	 * Synchronize the sacrifice PTE with its PVO, then mark both
2384804d1cc1SJustin Hibbits 	 * invalid. The PVO will be reused when/if the VM system comes
2385804d1cc1SJustin Hibbits 	 * here after a fault.
2386804d1cc1SJustin Hibbits 	 */
2387804d1cc1SJustin Hibbits 	pt = &moea_pteg_table[victim_idx >> 3].pt[victim_idx & 7];
2388804d1cc1SJustin Hibbits 
2389804d1cc1SJustin Hibbits 	if (pt->pte_hi != victim_pvo->pvo_pte.pte.pte_hi)
2390804d1cc1SJustin Hibbits 	    panic("Victim PVO doesn't match PTE! PVO: %8x, PTE: %8x", victim_pvo->pvo_pte.pte.pte_hi, pt->pte_hi);
2391804d1cc1SJustin Hibbits 
2392804d1cc1SJustin Hibbits 	/*
2393804d1cc1SJustin Hibbits 	 * Set the new PTE.
2394804d1cc1SJustin Hibbits 	 */
2395804d1cc1SJustin Hibbits 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
2396804d1cc1SJustin Hibbits 	PVO_PTEGIDX_CLR(victim_pvo);
2397804d1cc1SJustin Hibbits 	moea_pte_overflow++;
2398804d1cc1SJustin Hibbits 	moea_pte_set(pt, pvo_pt);
2399804d1cc1SJustin Hibbits 
2400804d1cc1SJustin Hibbits 	return (victim_idx & 7);
2401804d1cc1SJustin Hibbits }
2402804d1cc1SJustin Hibbits 
24035244eac9SBenno Rice static boolean_t
240459276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
24055244eac9SBenno Rice {
24065244eac9SBenno Rice 	struct	pvo_entry *pvo;
24075244eac9SBenno Rice 	struct	pte *pt;
24085244eac9SBenno Rice 
24098d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
241059276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
24115244eac9SBenno Rice 		return (TRUE);
24125244eac9SBenno Rice 
24135244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
24145244eac9SBenno Rice 
24155244eac9SBenno Rice 		/*
24165244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
24175244eac9SBenno Rice 		 * success.
24185244eac9SBenno Rice 		 */
241952a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
242059276937SPeter Grehan 			moea_attr_save(m, ptebit);
24215244eac9SBenno Rice 			return (TRUE);
24225244eac9SBenno Rice 		}
24235244eac9SBenno Rice 	}
24245244eac9SBenno Rice 
24255244eac9SBenno Rice 	/*
24265244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
24275244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
24285244eac9SBenno Rice 	 * the PTEs.
24295244eac9SBenno Rice 	 */
2430e4f72b32SMarcel Moolenaar 	powerpc_sync();
24315244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
24325244eac9SBenno Rice 
24335244eac9SBenno Rice 		/*
24345244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
24355244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
24365244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
24375244eac9SBenno Rice 		 */
243859276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
24395244eac9SBenno Rice 		if (pt != NULL) {
244052a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2441d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
244252a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
244359276937SPeter Grehan 				moea_attr_save(m, ptebit);
24445244eac9SBenno Rice 				return (TRUE);
24455244eac9SBenno Rice 			}
24465244eac9SBenno Rice 		}
24475244eac9SBenno Rice 	}
24485244eac9SBenno Rice 
24494f7daed0SAndrew Gallatin 	return (FALSE);
24505244eac9SBenno Rice }
24515244eac9SBenno Rice 
245203b6e025SPeter Grehan static u_int
2453ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
24545244eac9SBenno Rice {
245503b6e025SPeter Grehan 	u_int	count;
24565244eac9SBenno Rice 	struct	pvo_entry *pvo;
24575244eac9SBenno Rice 	struct	pte *pt;
2458ce186587SAlan Cox 
24598d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
24605244eac9SBenno Rice 
24615244eac9SBenno Rice 	/*
24625244eac9SBenno Rice 	 * Clear the cached value.
24635244eac9SBenno Rice 	 */
246459276937SPeter Grehan 	moea_attr_clear(m, ptebit);
24655244eac9SBenno Rice 
24665244eac9SBenno Rice 	/*
24675244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
24685244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
24695244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
24705244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
24715244eac9SBenno Rice 	 * REF/CHG bits.
24725244eac9SBenno Rice 	 */
2473e4f72b32SMarcel Moolenaar 	powerpc_sync();
24745244eac9SBenno Rice 
24755244eac9SBenno Rice 	/*
24765244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
24775244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
24785244eac9SBenno Rice 	 */
247903b6e025SPeter Grehan 	count = 0;
24805244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
248159276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
24825244eac9SBenno Rice 		if (pt != NULL) {
248352a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
248452a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
248503b6e025SPeter Grehan 				count++;
248659276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
24875244eac9SBenno Rice 			}
2488d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
248903b6e025SPeter Grehan 		}
249052a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
24915244eac9SBenno Rice 	}
24925244eac9SBenno Rice 
249303b6e025SPeter Grehan 	return (count);
2494bdf71f56SBenno Rice }
24958bbfa33aSBenno Rice 
24968bbfa33aSBenno Rice /*
249732bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
249832bc7846SPeter Grehan  */
249932bc7846SPeter Grehan static int
250059276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
250132bc7846SPeter Grehan {
250232bc7846SPeter Grehan 	u_int prot;
250332bc7846SPeter Grehan 	u_int32_t start;
250432bc7846SPeter Grehan 	u_int32_t end;
250532bc7846SPeter Grehan 	u_int32_t bat_ble;
250632bc7846SPeter Grehan 
250732bc7846SPeter Grehan 	/*
250832bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
250932bc7846SPeter Grehan 	 */
2510c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
251132bc7846SPeter Grehan 		return (EINVAL);
251232bc7846SPeter Grehan 
251332bc7846SPeter Grehan 	/*
251432bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
251532bc7846SPeter Grehan 	 * so it can function as an i/o page
251632bc7846SPeter Grehan 	 */
251732bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
251832bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
251932bc7846SPeter Grehan 		return (EPERM);
252032bc7846SPeter Grehan 
252132bc7846SPeter Grehan 	/*
252232bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
252332bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
252432bc7846SPeter Grehan 	 * not requiring masking)
252532bc7846SPeter Grehan 	 */
252632bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
252732bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
252832bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
252932bc7846SPeter Grehan 
253032bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
253132bc7846SPeter Grehan 		return (ERANGE);
253232bc7846SPeter Grehan 
253332bc7846SPeter Grehan 	return (0);
253432bc7846SPeter Grehan }
253532bc7846SPeter Grehan 
253659276937SPeter Grehan boolean_t
253720b79612SRafal Jaworowski moea_dev_direct_mapped(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2538c0763d37SSuleiman Souhlal {
2539c0763d37SSuleiman Souhlal 	int i;
2540c0763d37SSuleiman Souhlal 
2541c0763d37SSuleiman Souhlal 	/*
2542c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2543c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2544c0763d37SSuleiman Souhlal 	 */
2545c0763d37SSuleiman Souhlal 
2546c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
254759276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2548c0763d37SSuleiman Souhlal 			return (0);
2549c0763d37SSuleiman Souhlal 
2550c0763d37SSuleiman Souhlal 	return (EFAULT);
2551c0763d37SSuleiman Souhlal }
255232bc7846SPeter Grehan 
255332bc7846SPeter Grehan /*
25548bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
25558bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
25568bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
25578bbfa33aSBenno Rice  * NOT real memory.
25588bbfa33aSBenno Rice  */
25598bbfa33aSBenno Rice void *
256020b79612SRafal Jaworowski moea_mapdev(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
25618bbfa33aSBenno Rice {
2562c1f4123bSNathan Whitehorn 
2563c1f4123bSNathan Whitehorn 	return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2564c1f4123bSNathan Whitehorn }
2565c1f4123bSNathan Whitehorn 
2566c1f4123bSNathan Whitehorn void *
2567c1f4123bSNathan Whitehorn moea_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma)
2568c1f4123bSNathan Whitehorn {
256932bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
257032bc7846SPeter Grehan 	int i;
25718bbfa33aSBenno Rice 
257232bc7846SPeter Grehan 	ppa = trunc_page(pa);
25738bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
25748bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
25758bbfa33aSBenno Rice 
257632bc7846SPeter Grehan 	/*
257732bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
257832bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
257932bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
258032bc7846SPeter Grehan 	 */
258132bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
258259276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
258332bc7846SPeter Grehan 			return ((void *) pa);
258432bc7846SPeter Grehan 	}
258532bc7846SPeter Grehan 
25865df87b21SJeff Roberson 	va = kva_alloc(size);
25878bbfa33aSBenno Rice 	if (!va)
258859276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
25898bbfa33aSBenno Rice 
25908bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
2591c1f4123bSNathan Whitehorn 		moea_kenter_attr(mmu, tmpva, ppa, ma);
2592e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
25938bbfa33aSBenno Rice 		size -= PAGE_SIZE;
25948bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
259532bc7846SPeter Grehan 		ppa += PAGE_SIZE;
25968bbfa33aSBenno Rice 	}
25978bbfa33aSBenno Rice 
25988bbfa33aSBenno Rice 	return ((void *)(va + offset));
25998bbfa33aSBenno Rice }
26008bbfa33aSBenno Rice 
26018bbfa33aSBenno Rice void
260259276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
26038bbfa33aSBenno Rice {
26048bbfa33aSBenno Rice 	vm_offset_t base, offset;
26058bbfa33aSBenno Rice 
260632bc7846SPeter Grehan 	/*
260732bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
260832bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
260932bc7846SPeter Grehan 	 */
2610ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
26118bbfa33aSBenno Rice 		base = trunc_page(va);
26128bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
26138bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
26145df87b21SJeff Roberson 		kva_free(base, size);
26158bbfa33aSBenno Rice 	}
261632bc7846SPeter Grehan }
26171a4fcaebSMarcel Moolenaar 
26181a4fcaebSMarcel Moolenaar static void
26191a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
26201a4fcaebSMarcel Moolenaar {
26211a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
26221a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
26231a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
26241a4fcaebSMarcel Moolenaar 	vm_size_t len;
26251a4fcaebSMarcel Moolenaar 
26261a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
26271a4fcaebSMarcel Moolenaar 	while (sz > 0) {
26281a4fcaebSMarcel Moolenaar 		lim = round_page(va);
26291a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
26301a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
26311a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
26321a4fcaebSMarcel Moolenaar 			pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
26331a4fcaebSMarcel Moolenaar 			    (va & ADDR_POFF);
26341a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
26351a4fcaebSMarcel Moolenaar 		}
26361a4fcaebSMarcel Moolenaar 		va += len;
26371a4fcaebSMarcel Moolenaar 		sz -= len;
26381a4fcaebSMarcel Moolenaar 	}
26391a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
26401a4fcaebSMarcel Moolenaar }
2641afd9cb6cSJustin Hibbits 
2642afd9cb6cSJustin Hibbits vm_offset_t
2643afd9cb6cSJustin Hibbits moea_dumpsys_map(mmu_t mmu, struct pmap_md *md, vm_size_t ofs,
2644afd9cb6cSJustin Hibbits     vm_size_t *sz)
2645afd9cb6cSJustin Hibbits {
2646afd9cb6cSJustin Hibbits 	if (md->md_vaddr == ~0UL)
2647afd9cb6cSJustin Hibbits 	    return (md->md_paddr + ofs);
2648afd9cb6cSJustin Hibbits 	else
2649afd9cb6cSJustin Hibbits 	    return (md->md_vaddr + ofs);
2650afd9cb6cSJustin Hibbits }
2651afd9cb6cSJustin Hibbits 
2652afd9cb6cSJustin Hibbits struct pmap_md *
2653afd9cb6cSJustin Hibbits moea_scan_md(mmu_t mmu, struct pmap_md *prev)
2654afd9cb6cSJustin Hibbits {
2655afd9cb6cSJustin Hibbits 	static struct pmap_md md;
2656afd9cb6cSJustin Hibbits 	struct pvo_entry *pvo;
2657afd9cb6cSJustin Hibbits 	vm_offset_t va;
2658afd9cb6cSJustin Hibbits 
2659afd9cb6cSJustin Hibbits 	if (dumpsys_minidump) {
2660afd9cb6cSJustin Hibbits 		md.md_paddr = ~0UL;	/* Minidumps use virtual addresses. */
2661afd9cb6cSJustin Hibbits 		if (prev == NULL) {
2662afd9cb6cSJustin Hibbits 			/* 1st: kernel .data and .bss. */
2663afd9cb6cSJustin Hibbits 			md.md_index = 1;
2664afd9cb6cSJustin Hibbits 			md.md_vaddr = trunc_page((uintptr_t)_etext);
2665afd9cb6cSJustin Hibbits 			md.md_size = round_page((uintptr_t)_end) - md.md_vaddr;
2666afd9cb6cSJustin Hibbits 			return (&md);
2667afd9cb6cSJustin Hibbits 		}
2668afd9cb6cSJustin Hibbits 		switch (prev->md_index) {
2669afd9cb6cSJustin Hibbits 		case 1:
2670afd9cb6cSJustin Hibbits 			/* 2nd: msgbuf and tables (see pmap_bootstrap()). */
2671afd9cb6cSJustin Hibbits 			md.md_index = 2;
2672afd9cb6cSJustin Hibbits 			md.md_vaddr = (vm_offset_t)msgbufp->msg_ptr;
2673afd9cb6cSJustin Hibbits 			md.md_size = round_page(msgbufp->msg_size);
2674afd9cb6cSJustin Hibbits 			break;
2675afd9cb6cSJustin Hibbits 		case 2:
2676afd9cb6cSJustin Hibbits 			/* 3rd: kernel VM. */
2677afd9cb6cSJustin Hibbits 			va = prev->md_vaddr + prev->md_size;
2678afd9cb6cSJustin Hibbits 			/* Find start of next chunk (from va). */
2679afd9cb6cSJustin Hibbits 			while (va < virtual_end) {
2680afd9cb6cSJustin Hibbits 				/* Don't dump the buffer cache. */
2681afd9cb6cSJustin Hibbits 				if (va >= kmi.buffer_sva &&
2682afd9cb6cSJustin Hibbits 				    va < kmi.buffer_eva) {
2683afd9cb6cSJustin Hibbits 					va = kmi.buffer_eva;
2684afd9cb6cSJustin Hibbits 					continue;
2685afd9cb6cSJustin Hibbits 				}
2686afd9cb6cSJustin Hibbits 				pvo = moea_pvo_find_va(kernel_pmap,
2687afd9cb6cSJustin Hibbits 				    va & ~ADDR_POFF, NULL);
2688afd9cb6cSJustin Hibbits 				if (pvo != NULL &&
2689afd9cb6cSJustin Hibbits 				    (pvo->pvo_pte.pte.pte_hi & PTE_VALID))
2690afd9cb6cSJustin Hibbits 					break;
2691afd9cb6cSJustin Hibbits 				va += PAGE_SIZE;
2692afd9cb6cSJustin Hibbits 			}
2693afd9cb6cSJustin Hibbits 			if (va < virtual_end) {
2694afd9cb6cSJustin Hibbits 				md.md_vaddr = va;
2695afd9cb6cSJustin Hibbits 				va += PAGE_SIZE;
2696afd9cb6cSJustin Hibbits 				/* Find last page in chunk. */
2697afd9cb6cSJustin Hibbits 				while (va < virtual_end) {
2698afd9cb6cSJustin Hibbits 					/* Don't run into the buffer cache. */
2699afd9cb6cSJustin Hibbits 					if (va == kmi.buffer_sva)
2700afd9cb6cSJustin Hibbits 						break;
2701afd9cb6cSJustin Hibbits 					pvo = moea_pvo_find_va(kernel_pmap,
2702afd9cb6cSJustin Hibbits 					    va & ~ADDR_POFF, NULL);
2703afd9cb6cSJustin Hibbits 					if (pvo == NULL ||
2704afd9cb6cSJustin Hibbits 					    !(pvo->pvo_pte.pte.pte_hi & PTE_VALID))
2705afd9cb6cSJustin Hibbits 						break;
2706afd9cb6cSJustin Hibbits 					va += PAGE_SIZE;
2707afd9cb6cSJustin Hibbits 				}
2708afd9cb6cSJustin Hibbits 				md.md_size = va - md.md_vaddr;
2709afd9cb6cSJustin Hibbits 				break;
2710afd9cb6cSJustin Hibbits 			}
2711afd9cb6cSJustin Hibbits 			md.md_index = 3;
2712afd9cb6cSJustin Hibbits 			/* FALLTHROUGH */
2713afd9cb6cSJustin Hibbits 		default:
2714afd9cb6cSJustin Hibbits 			return (NULL);
2715afd9cb6cSJustin Hibbits 		}
2716afd9cb6cSJustin Hibbits 	} else { /* minidumps */
2717afd9cb6cSJustin Hibbits 		mem_regions(&pregions, &pregions_sz,
2718afd9cb6cSJustin Hibbits 		    &regions, &regions_sz);
2719afd9cb6cSJustin Hibbits 
2720afd9cb6cSJustin Hibbits 		if (prev == NULL) {
2721afd9cb6cSJustin Hibbits 			/* first physical chunk. */
2722afd9cb6cSJustin Hibbits 			md.md_paddr = pregions[0].mr_start;
2723afd9cb6cSJustin Hibbits 			md.md_size = pregions[0].mr_size;
2724afd9cb6cSJustin Hibbits 			md.md_vaddr = ~0UL;
2725afd9cb6cSJustin Hibbits 			md.md_index = 1;
2726afd9cb6cSJustin Hibbits 		} else if (md.md_index < pregions_sz) {
2727afd9cb6cSJustin Hibbits 			md.md_paddr = pregions[md.md_index].mr_start;
2728afd9cb6cSJustin Hibbits 			md.md_size = pregions[md.md_index].mr_size;
2729afd9cb6cSJustin Hibbits 			md.md_vaddr = ~0UL;
2730afd9cb6cSJustin Hibbits 			md.md_index++;
2731afd9cb6cSJustin Hibbits 		} else {
2732afd9cb6cSJustin Hibbits 			/* There's no next physical chunk. */
2733afd9cb6cSJustin Hibbits 			return (NULL);
2734afd9cb6cSJustin Hibbits 		}
2735afd9cb6cSJustin Hibbits 	}
2736afd9cb6cSJustin Hibbits 
2737afd9cb6cSJustin Hibbits 	return (&md);
2738afd9cb6cSJustin Hibbits }
2739