xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision 598d99ddeeae89e5970946b6a1d5bc6c9468e82e)
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  * 3. All advertising materials mentioning features or use of this software
175244eac9SBenno Rice  *    must display the following acknowledgement:
185244eac9SBenno Rice  *        This product includes software developed by the NetBSD
195244eac9SBenno Rice  *        Foundation, Inc. and its contributors.
205244eac9SBenno Rice  * 4. Neither the name of The NetBSD Foundation nor the names of its
215244eac9SBenno Rice  *    contributors may be used to endorse or promote products derived
225244eac9SBenno Rice  *    from this software without specific prior written permission.
235244eac9SBenno Rice  *
245244eac9SBenno Rice  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
255244eac9SBenno Rice  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
265244eac9SBenno Rice  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
275244eac9SBenno Rice  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
285244eac9SBenno Rice  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
295244eac9SBenno Rice  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
305244eac9SBenno Rice  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
315244eac9SBenno Rice  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
325244eac9SBenno Rice  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
335244eac9SBenno Rice  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
345244eac9SBenno Rice  * POSSIBILITY OF SUCH DAMAGE.
355244eac9SBenno Rice  */
3660727d8bSWarner Losh /*-
37f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
38f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 TooLs GmbH.
39f9bac91bSBenno Rice  * All rights reserved.
40f9bac91bSBenno Rice  *
41f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
42f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
43f9bac91bSBenno Rice  * are met:
44f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
45f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
46f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
47f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
48f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
49f9bac91bSBenno Rice  * 3. All advertising materials mentioning features or use of this software
50f9bac91bSBenno Rice  *    must display the following acknowledgement:
51f9bac91bSBenno Rice  *	This product includes software developed by TooLs GmbH.
52f9bac91bSBenno Rice  * 4. The name of TooLs GmbH may not be used to endorse or promote products
53f9bac91bSBenno Rice  *    derived from this software without specific prior written permission.
54f9bac91bSBenno Rice  *
55f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
56f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
61f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
62f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
63f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
64f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65f9bac91bSBenno Rice  *
66111c77dcSBenno Rice  * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
67f9bac91bSBenno Rice  */
6860727d8bSWarner Losh /*-
69f9bac91bSBenno Rice  * Copyright (C) 2001 Benno Rice.
70f9bac91bSBenno Rice  * All rights reserved.
71f9bac91bSBenno Rice  *
72f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
73f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
74f9bac91bSBenno Rice  * are met:
75f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
76f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
77f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
78f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
79f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
80f9bac91bSBenno Rice  *
81f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
82f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
85f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
86f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
87f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
88f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
89f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
90f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91f9bac91bSBenno Rice  */
92f9bac91bSBenno Rice 
938368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
948368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
95f9bac91bSBenno Rice 
965244eac9SBenno Rice /*
975244eac9SBenno Rice  * Manages physical address maps.
985244eac9SBenno Rice  *
995244eac9SBenno Rice  * In addition to hardware address maps, this module is called upon to
1005244eac9SBenno Rice  * provide software-use-only maps which may or may not be stored in the
1015244eac9SBenno Rice  * same form as hardware maps.  These pseudo-maps are used to store
1025244eac9SBenno Rice  * intermediate results from copy operations to and from address spaces.
1035244eac9SBenno Rice  *
1045244eac9SBenno Rice  * Since the information managed by this module is also stored by the
1055244eac9SBenno Rice  * logical address mapping module, this module may throw away valid virtual
1065244eac9SBenno Rice  * to physical mappings at almost any time.  However, invalidations of
1075244eac9SBenno Rice  * mappings must be done as requested.
1085244eac9SBenno Rice  *
1095244eac9SBenno Rice  * In order to cope with hardware architectures which make virtual to
1105244eac9SBenno Rice  * physical map invalidates expensive, this module may delay invalidate
1115244eac9SBenno Rice  * reduced protection operations until such time as they are actually
1125244eac9SBenno Rice  * necessary.  This module is given full information as to which processors
1135244eac9SBenno Rice  * are currently using which maps, and to when physical maps must be made
1145244eac9SBenno Rice  * correct.
1155244eac9SBenno Rice  */
1165244eac9SBenno Rice 
117ad7a226fSPeter Wemm #include "opt_kstack_pages.h"
118ad7a226fSPeter Wemm 
119f9bac91bSBenno Rice #include <sys/param.h>
1200b27d710SPeter Wemm #include <sys/kernel.h>
121c47dd3dbSAttilio Rao #include <sys/queue.h>
122c47dd3dbSAttilio Rao #include <sys/cpuset.h>
1235244eac9SBenno Rice #include <sys/ktr.h>
12494e0b85eSMark Peek #include <sys/lock.h>
1255244eac9SBenno Rice #include <sys/msgbuf.h>
126f9bac91bSBenno Rice #include <sys/mutex.h>
1275244eac9SBenno Rice #include <sys/proc.h>
128c47dd3dbSAttilio Rao #include <sys/sched.h>
1295244eac9SBenno Rice #include <sys/sysctl.h>
1305244eac9SBenno Rice #include <sys/systm.h>
1315244eac9SBenno Rice #include <sys/vmmeter.h>
1325244eac9SBenno Rice 
1335244eac9SBenno Rice #include <dev/ofw/openfirm.h>
134f9bac91bSBenno Rice 
135f9bac91bSBenno Rice #include <vm/vm.h>
136f9bac91bSBenno Rice #include <vm/vm_param.h>
137f9bac91bSBenno Rice #include <vm/vm_kern.h>
138f9bac91bSBenno Rice #include <vm/vm_page.h>
139f9bac91bSBenno Rice #include <vm/vm_map.h>
140f9bac91bSBenno Rice #include <vm/vm_object.h>
141f9bac91bSBenno Rice #include <vm/vm_extern.h>
142f9bac91bSBenno Rice #include <vm/vm_pageout.h>
143f9bac91bSBenno Rice #include <vm/vm_pager.h>
144378862a7SJeff Roberson #include <vm/uma.h>
145f9bac91bSBenno Rice 
1467c277971SPeter Grehan #include <machine/cpu.h>
147b40ce02aSNathan Whitehorn #include <machine/platform.h>
148d699b539SMark Peek #include <machine/bat.h>
1495244eac9SBenno Rice #include <machine/frame.h>
1505244eac9SBenno Rice #include <machine/md_var.h>
1515244eac9SBenno Rice #include <machine/psl.h>
152f9bac91bSBenno Rice #include <machine/pte.h>
15312640815SMarcel Moolenaar #include <machine/smp.h>
1545244eac9SBenno Rice #include <machine/sr.h>
15559276937SPeter Grehan #include <machine/mmuvar.h>
156f9bac91bSBenno Rice 
15759276937SPeter Grehan #include "mmu_if.h"
15859276937SPeter Grehan 
15959276937SPeter Grehan #define	MOEA_DEBUG
160f9bac91bSBenno Rice 
1615244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
162f9bac91bSBenno Rice 
1635244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1645244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1655244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1665244eac9SBenno Rice 
1675244eac9SBenno Rice struct ofw_map {
1685244eac9SBenno Rice 	vm_offset_t	om_va;
1695244eac9SBenno Rice 	vm_size_t	om_len;
1705244eac9SBenno Rice 	vm_offset_t	om_pa;
1715244eac9SBenno Rice 	u_int		om_mode;
1725244eac9SBenno Rice };
173f9bac91bSBenno Rice 
1745244eac9SBenno Rice /*
1755244eac9SBenno Rice  * Map of physical memory regions.
1765244eac9SBenno Rice  */
17731c82d03SBenno Rice static struct	mem_region *regions;
17831c82d03SBenno Rice static struct	mem_region *pregions;
179c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
180c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
181aa39961eSBenno Rice static struct	ofw_map *translations;
1825244eac9SBenno Rice 
183f9bac91bSBenno Rice /*
184f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
185f489bf21SAlan Cox  */
18659276937SPeter Grehan struct mtx	moea_table_mutex;
187e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
188f489bf21SAlan Cox 
189e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
190e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
191e4f72b32SMarcel Moolenaar 
192f489bf21SAlan Cox /*
1935244eac9SBenno Rice  * PTEG data.
194f9bac91bSBenno Rice  */
19559276937SPeter Grehan static struct	pteg *moea_pteg_table;
19659276937SPeter Grehan u_int		moea_pteg_count;
19759276937SPeter Grehan u_int		moea_pteg_mask;
1985244eac9SBenno Rice 
1995244eac9SBenno Rice /*
2005244eac9SBenno Rice  * PVO data.
2015244eac9SBenno Rice  */
20259276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
20359276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
20459276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
2055244eac9SBenno Rice 
20659276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
20759276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2085244eac9SBenno Rice 
2090d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
21059276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
21159276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2125244eac9SBenno Rice 
2135244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
21459276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2155244eac9SBenno Rice 
21659276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2175244eac9SBenno Rice 
2185244eac9SBenno Rice /*
2195244eac9SBenno Rice  * Statistics.
2205244eac9SBenno Rice  */
22159276937SPeter Grehan u_int	moea_pte_valid = 0;
22259276937SPeter Grehan u_int	moea_pte_overflow = 0;
22359276937SPeter Grehan u_int	moea_pte_replacements = 0;
22459276937SPeter Grehan u_int	moea_pvo_entries = 0;
22559276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
22659276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
22759276937SPeter Grehan u_int	moea_pte_spills = 0;
22859276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2295244eac9SBenno Rice     0, "");
23059276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
23159276937SPeter Grehan     &moea_pte_overflow, 0, "");
23259276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
23359276937SPeter Grehan     &moea_pte_replacements, 0, "");
23459276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2355244eac9SBenno Rice     0, "");
23659276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
23759276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
23859276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
23959276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
24059276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
24159276937SPeter Grehan     &moea_pte_spills, 0, "");
2425244eac9SBenno Rice 
2435244eac9SBenno Rice /*
24459276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2455244eac9SBenno Rice  */
24659276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2475244eac9SBenno Rice 
2485244eac9SBenno Rice /*
2495244eac9SBenno Rice  * PTE calls.
2505244eac9SBenno Rice  */
25159276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2525244eac9SBenno Rice 
2535244eac9SBenno Rice /*
2545244eac9SBenno Rice  * PVO calls.
2555244eac9SBenno Rice  */
25659276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2575244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
25859276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
25959276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
26059276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2615244eac9SBenno Rice 
2625244eac9SBenno Rice /*
2635244eac9SBenno Rice  * Utility routines.
2645244eac9SBenno Rice  */
265ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
266ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
26759276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
26859276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
269ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
27059276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
27159276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
27259276937SPeter Grehan 
27359276937SPeter Grehan /*
27459276937SPeter Grehan  * Kernel MMU interface
27559276937SPeter Grehan  */
27659276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
27759276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
27859276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t);
27959276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
28059276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
281ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
282ce142d9eSAlan Cox     vm_prot_t);
2832053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
28459276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
28559276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
28659276937SPeter Grehan void moea_init(mmu_t);
28759276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
288e396eb60SAlan Cox boolean_t moea_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
2897b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t);
29059276937SPeter Grehan boolean_t moea_ts_referenced(mmu_t, vm_page_t);
29159276937SPeter Grehan vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int);
29259276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
29359677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
29459276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
29559276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
29659276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
29759276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
29859276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
29959276937SPeter Grehan void moea_release(mmu_t, pmap_t);
30059276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
30159276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
30278985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
30359276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
30459276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
30559276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
30659276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
30759276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
3081c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int);
30959276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
31059276937SPeter Grehan void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t);
311c1f4123bSNathan Whitehorn void *moea_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t);
31259276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
31359276937SPeter Grehan vm_offset_t moea_kextract(mmu_t, vm_offset_t);
314c1f4123bSNathan Whitehorn void moea_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t);
31559276937SPeter Grehan void moea_kenter(mmu_t, vm_offset_t, vm_offset_t);
316c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma);
31759276937SPeter Grehan boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
3181a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
31959276937SPeter Grehan 
32059276937SPeter Grehan static mmu_method_t moea_methods[] = {
32159276937SPeter Grehan 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
32259276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
32359276937SPeter Grehan 	MMUMETHOD(mmu_clear_reference,	moea_clear_reference),
32459276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
32559276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
326ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
32759276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
32859276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
32959276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
33059276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
33159276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
332e396eb60SAlan Cox 	MMUMETHOD(mmu_is_prefaultable,	moea_is_prefaultable),
3337b85f591SAlan Cox 	MMUMETHOD(mmu_is_referenced,	moea_is_referenced),
33459276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
33559276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
33659276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
33759677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
33859276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
33959276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
34059276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
34159276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
34259276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
34359276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
34459276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
34559276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
34678985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
3471a4fcaebSMarcel Moolenaar 	MMUMETHOD(mmu_sync_icache,	moea_sync_icache),
34859276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
34959276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
35059276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
35159276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
35259276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
353c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_page_set_memattr,	moea_page_set_memattr),
35459276937SPeter Grehan 
35559276937SPeter Grehan 	/* Internal interfaces */
35659276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
3571c96bdd1SNathan Whitehorn 	MMUMETHOD(mmu_cpu_bootstrap,   	moea_cpu_bootstrap),
358c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_mapdev_attr,	moea_mapdev_attr),
35959276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
36059276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
36159276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
36259276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
363c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_kenter_attr,	moea_kenter_attr),
36459276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
36559276937SPeter Grehan 
36659276937SPeter Grehan 	{ 0, 0 }
36759276937SPeter Grehan };
36859276937SPeter Grehan 
36933529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0);
37033529b98SPeter Grehan 
371c1f4123bSNathan Whitehorn static __inline uint32_t
372c1f4123bSNathan Whitehorn moea_calc_wimg(vm_offset_t pa, vm_memattr_t ma)
373c1f4123bSNathan Whitehorn {
374c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
375c1f4123bSNathan Whitehorn 	int i;
376c1f4123bSNathan Whitehorn 
377c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
378c1f4123bSNathan Whitehorn 		switch (ma) {
379c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
380c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
381c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
382c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
383c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
384c1f4123bSNathan Whitehorn 			return (PTE_I);
385c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
386c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
387c1f4123bSNathan Whitehorn 		}
388c1f4123bSNathan Whitehorn 	}
389c1f4123bSNathan Whitehorn 
390c1f4123bSNathan Whitehorn 	/*
391c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
392c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
393c1f4123bSNathan Whitehorn 	 */
394c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
395c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
396c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
397c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
398c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
399c1f4123bSNathan Whitehorn 			break;
400c1f4123bSNathan Whitehorn 		}
401c1f4123bSNathan Whitehorn 	}
402c1f4123bSNathan Whitehorn 
403c1f4123bSNathan Whitehorn 	return pte_lo;
404c1f4123bSNathan Whitehorn }
40559276937SPeter Grehan 
406e4f72b32SMarcel Moolenaar static void
407e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
408e4f72b32SMarcel Moolenaar {
409e4f72b32SMarcel Moolenaar 
410e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
41194363f53SNathan Whitehorn 	__asm __volatile("ptesync");
412e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
41394363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
414e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
415e4f72b32SMarcel Moolenaar }
416e4f72b32SMarcel Moolenaar 
417e4f72b32SMarcel Moolenaar static void
418e4f72b32SMarcel Moolenaar tlbia(void)
419e4f72b32SMarcel Moolenaar {
420e4f72b32SMarcel Moolenaar 	vm_offset_t va;
421e4f72b32SMarcel Moolenaar 
422e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
423e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
424e4f72b32SMarcel Moolenaar 		powerpc_sync();
425e4f72b32SMarcel Moolenaar 	}
426e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
427e4f72b32SMarcel Moolenaar 	powerpc_sync();
428e4f72b32SMarcel Moolenaar }
4295244eac9SBenno Rice 
4305244eac9SBenno Rice static __inline int
4315244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4325244eac9SBenno Rice {
4335244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4345244eac9SBenno Rice }
4355244eac9SBenno Rice 
4365244eac9SBenno Rice static __inline u_int
4375244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4385244eac9SBenno Rice {
4395244eac9SBenno Rice 	u_int hash;
4405244eac9SBenno Rice 
4415244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4425244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
44359276937SPeter Grehan 	return (hash & moea_pteg_mask);
4445244eac9SBenno Rice }
4455244eac9SBenno Rice 
4465244eac9SBenno Rice static __inline struct pvo_head *
4475244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
448f9bac91bSBenno Rice {
449f9bac91bSBenno Rice 
4505244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
451f9bac91bSBenno Rice }
452f9bac91bSBenno Rice 
453f9bac91bSBenno Rice static __inline void
45459276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
455f9bac91bSBenno Rice {
456f9bac91bSBenno Rice 
457d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4585244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4595244eac9SBenno Rice }
4605244eac9SBenno Rice 
4615244eac9SBenno Rice static __inline int
46259276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4635244eac9SBenno Rice {
4645244eac9SBenno Rice 
4655244eac9SBenno Rice 	return (m->md.mdpg_attrs);
466f9bac91bSBenno Rice }
467f9bac91bSBenno Rice 
468f9bac91bSBenno Rice static __inline void
46959276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
470f9bac91bSBenno Rice {
471f9bac91bSBenno Rice 
472d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4735244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
474f9bac91bSBenno Rice }
475f9bac91bSBenno Rice 
476f9bac91bSBenno Rice static __inline int
47759276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
478f9bac91bSBenno Rice {
4795244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4805244eac9SBenno Rice 		return (1);
481f9bac91bSBenno Rice 
4825244eac9SBenno Rice 	return (0);
483f9bac91bSBenno Rice }
484f9bac91bSBenno Rice 
485f9bac91bSBenno Rice static __inline int
48659276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
487f9bac91bSBenno Rice {
4885244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
4895244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4905244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
491f9bac91bSBenno Rice }
492f9bac91bSBenno Rice 
4935244eac9SBenno Rice static __inline void
49459276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
495f9bac91bSBenno Rice {
496d644a0b7SAlan Cox 
497d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
498d644a0b7SAlan Cox 
499f9bac91bSBenno Rice 	/*
5005244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5015244eac9SBenno Rice 	 * set when the real pte is set in memory.
502f9bac91bSBenno Rice 	 *
503f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
504f9bac91bSBenno Rice 	 */
5055244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5065244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5075244eac9SBenno Rice 	pt->pte_lo = pte_lo;
508f9bac91bSBenno Rice }
509f9bac91bSBenno Rice 
5105244eac9SBenno Rice static __inline void
51159276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
512f9bac91bSBenno Rice {
513f9bac91bSBenno Rice 
514d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5155244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
516f9bac91bSBenno Rice }
517f9bac91bSBenno Rice 
5185244eac9SBenno Rice static __inline void
51959276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
520f9bac91bSBenno Rice {
5215244eac9SBenno Rice 
522d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
523d644a0b7SAlan Cox 
5245244eac9SBenno Rice 	/*
5255244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5265244eac9SBenno Rice 	 */
5275244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
528e4f72b32SMarcel Moolenaar 	tlbie(va);
5295244eac9SBenno Rice }
5305244eac9SBenno Rice 
5315244eac9SBenno Rice static __inline void
53259276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5335244eac9SBenno Rice {
5345244eac9SBenno Rice 
535d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5365244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5375244eac9SBenno Rice 
5385244eac9SBenno Rice 	/*
5395244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
5405244eac9SBenno Rice 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
5415244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5425244eac9SBenno Rice 	 */
5435244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
544e4f72b32SMarcel Moolenaar 	powerpc_sync();
5455244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
546e4f72b32SMarcel Moolenaar 	powerpc_sync();
54759276937SPeter Grehan 	moea_pte_valid++;
5485244eac9SBenno Rice }
5495244eac9SBenno Rice 
5505244eac9SBenno Rice static __inline void
55159276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5525244eac9SBenno Rice {
5535244eac9SBenno Rice 
554d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5555244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5565244eac9SBenno Rice 
5575244eac9SBenno Rice 	/*
5585244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5595244eac9SBenno Rice 	 */
560e4f72b32SMarcel Moolenaar 	powerpc_sync();
5615244eac9SBenno Rice 
5625244eac9SBenno Rice 	/*
5635244eac9SBenno Rice 	 * Invalidate the pte.
5645244eac9SBenno Rice 	 */
5655244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5665244eac9SBenno Rice 
567e4f72b32SMarcel Moolenaar 	tlbie(va);
5685244eac9SBenno Rice 
5695244eac9SBenno Rice 	/*
5705244eac9SBenno Rice 	 * Save the reg & chg bits.
5715244eac9SBenno Rice 	 */
57259276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
57359276937SPeter Grehan 	moea_pte_valid--;
5745244eac9SBenno Rice }
5755244eac9SBenno Rice 
5765244eac9SBenno Rice static __inline void
57759276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5785244eac9SBenno Rice {
5795244eac9SBenno Rice 
5805244eac9SBenno Rice 	/*
5815244eac9SBenno Rice 	 * Invalidate the PTE
5825244eac9SBenno Rice 	 */
58359276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
58459276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
585f9bac91bSBenno Rice }
586f9bac91bSBenno Rice 
587f9bac91bSBenno Rice /*
5885244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
589f9bac91bSBenno Rice  */
5905244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
5915244eac9SBenno Rice 
5925244eac9SBenno Rice static int
5935244eac9SBenno Rice om_cmp(const void *a, const void *b)
5945244eac9SBenno Rice {
5955244eac9SBenno Rice 	const struct	ofw_map *mapa;
5965244eac9SBenno Rice 	const struct	ofw_map *mapb;
5975244eac9SBenno Rice 
5985244eac9SBenno Rice 	mapa = a;
5995244eac9SBenno Rice 	mapb = b;
6005244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6015244eac9SBenno Rice 		return (-1);
6025244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6035244eac9SBenno Rice 		return (1);
6045244eac9SBenno Rice 	else
6055244eac9SBenno Rice 		return (0);
606f9bac91bSBenno Rice }
607f9bac91bSBenno Rice 
608f9bac91bSBenno Rice void
6091c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap)
61012640815SMarcel Moolenaar {
61112640815SMarcel Moolenaar 	u_int sdr;
61212640815SMarcel Moolenaar 	int i;
61312640815SMarcel Moolenaar 
61412640815SMarcel Moolenaar 	if (ap) {
615e4f72b32SMarcel Moolenaar 		powerpc_sync();
61612640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
61712640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
61812640815SMarcel Moolenaar 		isync();
61912640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
62012640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
62112640815SMarcel Moolenaar 		isync();
62212640815SMarcel Moolenaar 	}
62312640815SMarcel Moolenaar 
62401d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
62501d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
62612640815SMarcel Moolenaar 	isync();
62712640815SMarcel Moolenaar 
62801d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
62901d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
63001d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
63101d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
63201d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
63312640815SMarcel Moolenaar 	isync();
63412640815SMarcel Moolenaar 
63512640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
636fe3b4685SNathan Whitehorn 		mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
637e4f72b32SMarcel Moolenaar 	powerpc_sync();
63812640815SMarcel Moolenaar 
63912640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
64012640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
64112640815SMarcel Moolenaar 	isync();
64212640815SMarcel Moolenaar 
64386c1fb4cSMarcel Moolenaar 	tlbia();
64412640815SMarcel Moolenaar }
64512640815SMarcel Moolenaar 
64612640815SMarcel Moolenaar void
64759276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
648f9bac91bSBenno Rice {
64931c82d03SBenno Rice 	ihandle_t	mmui;
6505244eac9SBenno Rice 	phandle_t	chosen, mmu;
6515244eac9SBenno Rice 	int		sz;
6525244eac9SBenno Rice 	int		i, j;
653e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6545244eac9SBenno Rice 	vm_offset_t	pa, va, off;
65550c202c5SJeff Roberson 	void		*dpcpu;
656976cc697SNathan Whitehorn 	register_t	msr;
657f9bac91bSBenno Rice 
658f9bac91bSBenno Rice         /*
65932bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6600d290675SBenno Rice          */
6610d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6620d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6630d290675SBenno Rice 
6640d290675SBenno Rice         /*
6650d290675SBenno Rice          * Map PCI memory space.
6660d290675SBenno Rice          */
6670d290675SBenno Rice         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6680d290675SBenno Rice         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6690d290675SBenno Rice 
6700d290675SBenno Rice         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6710d290675SBenno Rice         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6720d290675SBenno Rice 
6730d290675SBenno Rice         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6740d290675SBenno Rice         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6750d290675SBenno Rice 
6760d290675SBenno Rice         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6770d290675SBenno Rice         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6780d290675SBenno Rice 
6790d290675SBenno Rice         /*
6800d290675SBenno Rice          * Map obio devices.
6810d290675SBenno Rice          */
6820d290675SBenno Rice         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
6830d290675SBenno Rice         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
6840d290675SBenno Rice 
6850d290675SBenno Rice 	/*
6865244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
687976cc697SNathan Whitehorn 	 * where we are. Turn off instruction relocation temporarily
688976cc697SNathan Whitehorn 	 * to prevent faults while reprogramming the IBAT.
689f9bac91bSBenno Rice 	 */
690976cc697SNathan Whitehorn 	msr = mfmsr();
691976cc697SNathan Whitehorn 	mtmsr(msr & ~PSL_IR);
69259276937SPeter Grehan 	__asm (".balign 32; \n"
69372ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
6945d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
69512640815SMarcel Moolenaar 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
696976cc697SNathan Whitehorn 	mtmsr(msr);
6970d290675SBenno Rice 
6980d290675SBenno Rice 	/* map pci space */
69912640815SMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
70012640815SMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
70112640815SMarcel Moolenaar 	isync();
702f9bac91bSBenno Rice 
7031c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7041c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7051c96bdd1SNathan Whitehorn 
70631c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
70759276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
70831c82d03SBenno Rice 
70931c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
71032bc7846SPeter Grehan 		vm_offset_t pa;
71132bc7846SPeter Grehan 		vm_offset_t end;
71232bc7846SPeter Grehan 
71331c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
71431c82d03SBenno Rice 			pregions[i].mr_start,
71531c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
71631c82d03SBenno Rice 			pregions[i].mr_size);
71732bc7846SPeter Grehan 		/*
71832bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
71932bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
72032bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
72132bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
72232bc7846SPeter Grehan 		 * a while yet.
72332bc7846SPeter Grehan 		 */
72432bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
72532bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
72632bc7846SPeter Grehan 		do {
72732bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
72832bc7846SPeter Grehan 
72932bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
73032bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
73132bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
73232bc7846SPeter Grehan 		} while (pa < end);
73331c82d03SBenno Rice 	}
73431c82d03SBenno Rice 
73531c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
73659276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
73797f7cde4SNathan Whitehorn 
7385244eac9SBenno Rice 	phys_avail_count = 0;
739d2c1f576SBenno Rice 	physsz = 0;
740b0c21309SPeter Grehan 	hwphyssz = 0;
741b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
74231c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7435244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7445244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7455244eac9SBenno Rice 		    regions[i].mr_size);
746e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
747e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
748e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
749e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
750e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
751e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
752e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
753e2f6d6e2SPeter Grehan 				phys_avail_count++;
754e2f6d6e2SPeter Grehan 			}
755e2f6d6e2SPeter Grehan 			break;
756e2f6d6e2SPeter Grehan 		}
7575244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7585244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7595244eac9SBenno Rice 		phys_avail_count++;
760d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
761f9bac91bSBenno Rice 	}
762d2c1f576SBenno Rice 	physmem = btoc(physsz);
763f9bac91bSBenno Rice 
764f9bac91bSBenno Rice 	/*
7655244eac9SBenno Rice 	 * Allocate PTEG table.
766f9bac91bSBenno Rice 	 */
7675244eac9SBenno Rice #ifdef PTEGCOUNT
76859276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
7695244eac9SBenno Rice #else
77059276937SPeter Grehan 	moea_pteg_count = 0x1000;
771f9bac91bSBenno Rice 
77259276937SPeter Grehan 	while (moea_pteg_count < physmem)
77359276937SPeter Grehan 		moea_pteg_count <<= 1;
774f9bac91bSBenno Rice 
77559276937SPeter Grehan 	moea_pteg_count >>= 1;
7765244eac9SBenno Rice #endif /* PTEGCOUNT */
777f9bac91bSBenno Rice 
77859276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
77959276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
7805244eac9SBenno Rice 	    size);
78159276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
78259276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
78359276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
78459276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
785f9bac91bSBenno Rice 
7865244eac9SBenno Rice 	/*
787864bc520SBenno Rice 	 * Allocate pv/overflow lists.
7885244eac9SBenno Rice 	 */
78959276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
79059276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
7915244eac9SBenno Rice 	    PAGE_SIZE);
79259276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
79359276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
79459276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
7955244eac9SBenno Rice 
7965244eac9SBenno Rice 	/*
797f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
798f489bf21SAlan Cox 	 * tables.
799f489bf21SAlan Cox 	 */
800d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
801d644a0b7SAlan Cox 	    MTX_RECURSE);
802e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
803f489bf21SAlan Cox 
804e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
805e4f72b32SMarcel Moolenaar 
806f489bf21SAlan Cox 	/*
8075244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8085244eac9SBenno Rice 	 */
80959276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8100d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
81159276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8125244eac9SBenno Rice 
8135244eac9SBenno Rice 	/*
8145244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8155244eac9SBenno Rice 	 */
81659276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8175244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
81859276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8195244eac9SBenno Rice 
8205244eac9SBenno Rice 	/*
821fe3b4685SNathan Whitehorn 	 * Initialize the kernel pmap (which is statically allocated).
8225244eac9SBenno Rice 	 */
823fe3b4685SNathan Whitehorn 	PMAP_LOCK_INIT(kernel_pmap);
824fe3b4685SNathan Whitehorn 	for (i = 0; i < 16; i++)
825fe3b4685SNathan Whitehorn 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
826c47dd3dbSAttilio Rao 	CPU_FILL(&kernel_pmap->pm_active);
827*598d99ddSNathan Whitehorn 	LIST_INIT(&kernel_pmap->pmap_pvo);
828fe3b4685SNathan Whitehorn 
829fe3b4685SNathan Whitehorn 	/*
830fe3b4685SNathan Whitehorn 	 * Set up the Open Firmware mappings
831fe3b4685SNathan Whitehorn 	 */
8325244eac9SBenno Rice 	if ((chosen = OF_finddevice("/chosen")) == -1)
83359276937SPeter Grehan 		panic("moea_bootstrap: can't find /chosen");
8345244eac9SBenno Rice 	OF_getprop(chosen, "mmu", &mmui, 4);
8355244eac9SBenno Rice 	if ((mmu = OF_instance_to_package(mmui)) == -1)
83659276937SPeter Grehan 		panic("moea_bootstrap: can't get mmu package");
8375244eac9SBenno Rice 	if ((sz = OF_getproplen(mmu, "translations")) == -1)
83859276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translation count");
839aa39961eSBenno Rice 	translations = NULL;
8406cc1cdf4SPeter Grehan 	for (i = 0; phys_avail[i] != 0; i += 2) {
8416cc1cdf4SPeter Grehan 		if (phys_avail[i + 1] >= sz) {
842aa39961eSBenno Rice 			translations = (struct ofw_map *)phys_avail[i];
8436cc1cdf4SPeter Grehan 			break;
8446cc1cdf4SPeter Grehan 		}
845aa39961eSBenno Rice 	}
846aa39961eSBenno Rice 	if (translations == NULL)
84759276937SPeter Grehan 		panic("moea_bootstrap: no space to copy translations");
8485244eac9SBenno Rice 	bzero(translations, sz);
8495244eac9SBenno Rice 	if (OF_getprop(mmu, "translations", translations, sz) == -1)
85059276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translations");
85159276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: translations");
85231c82d03SBenno Rice 	sz /= sizeof(*translations);
8535244eac9SBenno Rice 	qsort(translations, sz, sizeof (*translations), om_cmp);
854ed1e1e2aSNathan Whitehorn 	for (i = 0; i < sz; i++) {
8555244eac9SBenno Rice 		CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
8565244eac9SBenno Rice 		    translations[i].om_pa, translations[i].om_va,
8575244eac9SBenno Rice 		    translations[i].om_len);
8585244eac9SBenno Rice 
85932bc7846SPeter Grehan 		/*
86032bc7846SPeter Grehan 		 * If the mapping is 1:1, let the RAM and device on-demand
86132bc7846SPeter Grehan 		 * BAT tables take care of the translation.
86232bc7846SPeter Grehan 		 */
86332bc7846SPeter Grehan 		if (translations[i].om_va == translations[i].om_pa)
86432bc7846SPeter Grehan 			continue;
8655244eac9SBenno Rice 
86632bc7846SPeter Grehan 		/* Enter the pages */
867ed1e1e2aSNathan Whitehorn 		for (off = 0; off < translations[i].om_len; off += PAGE_SIZE)
868fe3b4685SNathan Whitehorn 			moea_kenter(mmup, translations[i].om_va + off,
869fe3b4685SNathan Whitehorn 				    translations[i].om_pa + off);
870f9bac91bSBenno Rice 	}
871014ffa99SMarcel Moolenaar 
872014ffa99SMarcel Moolenaar 	/*
873014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
874014ffa99SMarcel Moolenaar 	 */
875014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
876014ffa99SMarcel Moolenaar 		;
877014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
8785244eac9SBenno Rice 
8791c96bdd1SNathan Whitehorn 	moea_cpu_bootstrap(mmup,0);
8805244eac9SBenno Rice 
8815244eac9SBenno Rice 	pmap_bootstrapped++;
882014ffa99SMarcel Moolenaar 
883014ffa99SMarcel Moolenaar 	/*
884014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
885014ffa99SMarcel Moolenaar 	 */
886014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
887ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
888014ffa99SMarcel Moolenaar 
889014ffa99SMarcel Moolenaar 	/*
890014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
891014ffa99SMarcel Moolenaar 	 * into the kernel page map.
892014ffa99SMarcel Moolenaar 	 */
893014ffa99SMarcel Moolenaar 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
894014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
895014ffa99SMarcel Moolenaar 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
896014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
897014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
898014ffa99SMarcel Moolenaar 	thread0.td_kstack_pages = KSTACK_PAGES;
899014ffa99SMarcel Moolenaar 	for (i = 0; i < KSTACK_PAGES; i++) {
900c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
901014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
902014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
903014ffa99SMarcel Moolenaar 	}
904014ffa99SMarcel Moolenaar 
905014ffa99SMarcel Moolenaar 	/*
906014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
907014ffa99SMarcel Moolenaar 	 */
9084053b05bSSergey Kandaurov 	pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE);
909014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
910014ffa99SMarcel Moolenaar 	va = virtual_avail;
9114053b05bSSergey Kandaurov 	virtual_avail += round_page(msgbufsize);
912014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
913c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
914014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
915014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
916014ffa99SMarcel Moolenaar 	}
91750c202c5SJeff Roberson 
91850c202c5SJeff Roberson 	/*
91950c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
92050c202c5SJeff Roberson 	 */
92150c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
92250c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
92350c202c5SJeff Roberson 	va = virtual_avail;
92450c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
92550c202c5SJeff Roberson 	while (va < virtual_avail) {
926c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
92750c202c5SJeff Roberson 		pa += PAGE_SIZE;
92850c202c5SJeff Roberson 		va += PAGE_SIZE;
92950c202c5SJeff Roberson 	}
93050c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
9315244eac9SBenno Rice }
9325244eac9SBenno Rice 
9335244eac9SBenno Rice /*
9345244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9355244eac9SBenno Rice  * space can be accessed in any way.
936f9bac91bSBenno Rice  */
937f9bac91bSBenno Rice void
93859276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
939f9bac91bSBenno Rice {
9408207b362SBenno Rice 	pmap_t	pm, pmr;
941f9bac91bSBenno Rice 
942f9bac91bSBenno Rice 	/*
94332bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9445244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
945f9bac91bSBenno Rice 	 */
9465244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
94752a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
9488207b362SBenno Rice 
949c7c2767eSAttilio Rao 	CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
9508207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
951ac6ba8bdSBenno Rice }
952ac6ba8bdSBenno Rice 
953ac6ba8bdSBenno Rice void
95459276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
955ac6ba8bdSBenno Rice {
956ac6ba8bdSBenno Rice 	pmap_t	pm;
957ac6ba8bdSBenno Rice 
958ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
959c7c2767eSAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &pm->pm_active);
9608207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
961f9bac91bSBenno Rice }
962f9bac91bSBenno Rice 
963f9bac91bSBenno Rice void
96459276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
965f9bac91bSBenno Rice {
9660f92104cSBenno Rice 	struct	pvo_entry *pvo;
9670f92104cSBenno Rice 
96848d0b1a0SAlan Cox 	PMAP_LOCK(pm);
96959276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
9700f92104cSBenno Rice 
9710f92104cSBenno Rice 	if (pvo != NULL) {
9720f92104cSBenno Rice 		if (wired) {
9730f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
9740f92104cSBenno Rice 				pm->pm_stats.wired_count++;
9750f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
9760f92104cSBenno Rice 		} else {
9770f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
9780f92104cSBenno Rice 				pm->pm_stats.wired_count--;
9790f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
9800f92104cSBenno Rice 		}
9810f92104cSBenno Rice 	}
98248d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
983f9bac91bSBenno Rice }
984f9bac91bSBenno Rice 
985f9bac91bSBenno Rice void
98659276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
987f9bac91bSBenno Rice {
98825e2288dSBenno Rice 	vm_offset_t	dst;
98925e2288dSBenno Rice 	vm_offset_t	src;
99025e2288dSBenno Rice 
99125e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
99225e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
99325e2288dSBenno Rice 
99425e2288dSBenno Rice 	kcopy((void *)src, (void *)dst, PAGE_SIZE);
995f9bac91bSBenno Rice }
996111c77dcSBenno Rice 
997111c77dcSBenno Rice /*
9985244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
9995244eac9SBenno Rice  */
10005244eac9SBenno Rice void
100159276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
10025244eac9SBenno Rice {
10031a87a0daSPeter Wemm 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10045b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
10055244eac9SBenno Rice 
10065244eac9SBenno Rice 	bzero(va, PAGE_SIZE);
10075244eac9SBenno Rice }
10085244eac9SBenno Rice 
10095244eac9SBenno Rice void
101059276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
10115244eac9SBenno Rice {
10123495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10135b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
10143495845eSBenno Rice 
10155b43c63dSMarcel Moolenaar 	bzero(va, size);
10165244eac9SBenno Rice }
10175244eac9SBenno Rice 
1018a58b3a68SPeter Wemm void
101959276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1020a58b3a68SPeter Wemm {
10215b43c63dSMarcel Moolenaar 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10225b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
1023a58b3a68SPeter Wemm 
10245b43c63dSMarcel Moolenaar 	bzero(va, PAGE_SIZE);
1025a58b3a68SPeter Wemm }
1026a58b3a68SPeter Wemm 
10275244eac9SBenno Rice /*
10285244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
10295244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
10305244eac9SBenno Rice  * will be wired down.
10315244eac9SBenno Rice  */
10325244eac9SBenno Rice void
103359276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
10345244eac9SBenno Rice 	   boolean_t wired)
10355244eac9SBenno Rice {
1036ce142d9eSAlan Cox 
1037ce142d9eSAlan Cox 	vm_page_lock_queues();
1038ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
103967c867eeSAlan Cox 	moea_enter_locked(pmap, va, m, prot, wired);
1040ce142d9eSAlan Cox 	vm_page_unlock_queues();
1041ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1042ce142d9eSAlan Cox }
1043ce142d9eSAlan Cox 
1044ce142d9eSAlan Cox /*
1045ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1046ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1047ce142d9eSAlan Cox  * will be wired down.
1048ce142d9eSAlan Cox  *
1049ce142d9eSAlan Cox  * The page queues and pmap must be locked.
1050ce142d9eSAlan Cox  */
1051ce142d9eSAlan Cox static void
1052ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1053ce142d9eSAlan Cox     boolean_t wired)
1054ce142d9eSAlan Cox {
10555244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1056378862a7SJeff Roberson 	uma_zone_t	zone;
10578207b362SBenno Rice 	vm_page_t	pg;
1058c1f4123bSNathan Whitehorn 	u_int		pte_lo, pvo_flags, was_exec;
10595244eac9SBenno Rice 	int		error;
10605244eac9SBenno Rice 
106159276937SPeter Grehan 	if (!moea_initialized) {
106259276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
106359276937SPeter Grehan 		zone = moea_upvo_zone;
10645244eac9SBenno Rice 		pvo_flags = 0;
10658207b362SBenno Rice 		pg = NULL;
10668207b362SBenno Rice 		was_exec = PTE_EXEC;
10675244eac9SBenno Rice 	} else {
106803b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
106903b6e025SPeter Grehan 		pg = m;
107059276937SPeter Grehan 		zone = moea_mpvo_zone;
10715244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
10728207b362SBenno Rice 		was_exec = 0;
10735244eac9SBenno Rice 	}
1074f489bf21SAlan Cox 	if (pmap_bootstrapped)
1075ce142d9eSAlan Cox 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1076ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1077d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0 ||
1078d98d0ce2SKonstantin Belousov 	    VM_OBJECT_LOCKED(m->object),
10799ab6032fSAlan Cox 	    ("moea_enter_locked: page %p is not busy", m));
10805244eac9SBenno Rice 
10814dba5df1SPeter Grehan 	/* XXX change the pvo head for fake pages */
1082d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1083a130b35fSNathan Whitehorn 		pvo_flags &= ~PVO_MANAGED;
108459276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
1085a130b35fSNathan Whitehorn 		zone = moea_upvo_zone;
1086a130b35fSNathan Whitehorn 	}
10874dba5df1SPeter Grehan 
10888207b362SBenno Rice 	/*
10898207b362SBenno Rice 	 * If this is a managed page, and it's the first reference to the page,
10908207b362SBenno Rice 	 * clear the execness of the page.  Otherwise fetch the execness.
10918207b362SBenno Rice 	 */
1092d98d0ce2SKonstantin Belousov 	if ((pg != NULL) && ((m->oflags & VPO_UNMANAGED) == 0)) {
10938207b362SBenno Rice 		if (LIST_EMPTY(pvo_head)) {
109459276937SPeter Grehan 			moea_attr_clear(pg, PTE_EXEC);
10958207b362SBenno Rice 		} else {
109659276937SPeter Grehan 			was_exec = moea_attr_fetch(pg) & PTE_EXEC;
10978207b362SBenno Rice 		}
10988207b362SBenno Rice 	}
10998207b362SBenno Rice 
1100cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11015244eac9SBenno Rice 
110244b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11035244eac9SBenno Rice 		pte_lo |= PTE_BW;
11042368a371SAlan Cox 		if (pmap_bootstrapped &&
1105d98d0ce2SKonstantin Belousov 		    (m->oflags & VPO_UNMANAGED) == 0)
11063407fefeSKonstantin Belousov 			vm_page_aflag_set(m, PGA_WRITEABLE);
110744b8bd66SAlan Cox 	} else
11085244eac9SBenno Rice 		pte_lo |= PTE_BR;
11095244eac9SBenno Rice 
11104dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
11114dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
11125244eac9SBenno Rice 
11135244eac9SBenno Rice 	if (wired)
11145244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11155244eac9SBenno Rice 
111659276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11178207b362SBenno Rice 	    pte_lo, pvo_flags);
11185244eac9SBenno Rice 
11198207b362SBenno Rice 	/*
11208207b362SBenno Rice 	 * Flush the real page from the instruction cache if this page is
11218207b362SBenno Rice 	 * mapped executable and cacheable and was not previously mapped (or
11228207b362SBenno Rice 	 * was not mapped executable).
11238207b362SBenno Rice 	 */
11248207b362SBenno Rice 	if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
11258207b362SBenno Rice 	    (pte_lo & PTE_I) == 0 && was_exec == 0) {
11265244eac9SBenno Rice 		/*
11275244eac9SBenno Rice 		 * Flush the real memory from the cache.
11285244eac9SBenno Rice 		 */
112959276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
11308207b362SBenno Rice 		if (pg != NULL)
113159276937SPeter Grehan 			moea_attr_save(pg, PTE_EXEC);
11325244eac9SBenno Rice 	}
113332bc7846SPeter Grehan 
113432bc7846SPeter Grehan 	/* XXX syncicache always until problems are sorted */
113559276937SPeter Grehan 	moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1136ce142d9eSAlan Cox }
1137ce142d9eSAlan Cox 
1138ce142d9eSAlan Cox /*
1139ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1140ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1141ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1142ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1143ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1144ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1145ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1146ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1147ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1148ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1149ce142d9eSAlan Cox  */
1150ce142d9eSAlan Cox void
1151ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1152ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1153ce142d9eSAlan Cox {
1154ce142d9eSAlan Cox 	vm_page_t m;
1155ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1156ce142d9eSAlan Cox 
1157ce142d9eSAlan Cox 	psize = atop(end - start);
1158ce142d9eSAlan Cox 	m = m_start;
1159c46b90e9SAlan Cox 	vm_page_lock_queues();
1160ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1161ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1162ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1163ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1164ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1165ce142d9eSAlan Cox 	}
1166c46b90e9SAlan Cox 	vm_page_unlock_queues();
1167ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
11685244eac9SBenno Rice }
11695244eac9SBenno Rice 
11702053c127SStephan Uphoff void
117159276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
11722053c127SStephan Uphoff     vm_prot_t prot)
1173dca96f1aSAlan Cox {
1174dca96f1aSAlan Cox 
11753c4a2440SAlan Cox 	vm_page_lock_queues();
1176ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1177ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
117859276937SPeter Grehan 	    FALSE);
11793c4a2440SAlan Cox 	vm_page_unlock_queues();
1180ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1181dca96f1aSAlan Cox }
1182dca96f1aSAlan Cox 
118356b09388SAlan Cox vm_paddr_t
118459276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
11855244eac9SBenno Rice {
11860f92104cSBenno Rice 	struct	pvo_entry *pvo;
118748d0b1a0SAlan Cox 	vm_paddr_t pa;
11880f92104cSBenno Rice 
118948d0b1a0SAlan Cox 	PMAP_LOCK(pm);
119059276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
119148d0b1a0SAlan Cox 	if (pvo == NULL)
119248d0b1a0SAlan Cox 		pa = 0;
119348d0b1a0SAlan Cox 	else
119452a7870dSNathan Whitehorn 		pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
119548d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
119648d0b1a0SAlan Cox 	return (pa);
11975244eac9SBenno Rice }
11985244eac9SBenno Rice 
11995244eac9SBenno Rice /*
120084792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
120184792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
120284792e72SPeter Grehan  * protection.
120384792e72SPeter Grehan  */
120484792e72SPeter Grehan vm_page_t
120559276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
120684792e72SPeter Grehan {
1207ab50a262SAlan Cox 	struct	pvo_entry *pvo;
120884792e72SPeter Grehan 	vm_page_t m;
12092965a453SKip Macy         vm_paddr_t pa;
121084792e72SPeter Grehan 
121184792e72SPeter Grehan 	m = NULL;
12122965a453SKip Macy 	pa = 0;
121348d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
12142965a453SKip Macy retry:
121559276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
121652a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
121752a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1218ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
12192965a453SKip Macy 		if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa))
12202965a453SKip Macy 			goto retry;
122152a7870dSNathan Whitehorn 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
122284792e72SPeter Grehan 		vm_page_hold(m);
122384792e72SPeter Grehan 	}
12242965a453SKip Macy 	PA_UNLOCK_COND(pa);
122548d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
122684792e72SPeter Grehan 	return (m);
122784792e72SPeter Grehan }
122884792e72SPeter Grehan 
12295244eac9SBenno Rice void
123059276937SPeter Grehan moea_init(mmu_t mmu)
12315244eac9SBenno Rice {
12325244eac9SBenno Rice 
123359276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12340ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12350ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
123659276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12370ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12380ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
123959276937SPeter Grehan 	moea_initialized = TRUE;
12405244eac9SBenno Rice }
12415244eac9SBenno Rice 
12425244eac9SBenno Rice boolean_t
12437b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m)
12447b85f591SAlan Cox {
12457b85f591SAlan Cox 
1246d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1247c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
12487b85f591SAlan Cox 	return (moea_query_bit(m, PTE_REF));
12497b85f591SAlan Cox }
12507b85f591SAlan Cox 
12517b85f591SAlan Cox boolean_t
125259276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
12535244eac9SBenno Rice {
12540f92104cSBenno Rice 
1255d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1256567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1257567e51e1SAlan Cox 
1258567e51e1SAlan Cox 	/*
12593407fefeSKonstantin Belousov 	 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be
12603407fefeSKonstantin Belousov 	 * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
1261567e51e1SAlan Cox 	 * is clear, no PTEs can have PTE_CHG set.
1262567e51e1SAlan Cox 	 */
1263567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1264567e51e1SAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
12653407fefeSKonstantin Belousov 	    (m->aflags & PGA_WRITEABLE) == 0)
12660f92104cSBenno Rice 		return (FALSE);
1267c46b90e9SAlan Cox 	return (moea_query_bit(m, PTE_CHG));
1268566526a9SAlan Cox }
1269566526a9SAlan Cox 
1270e396eb60SAlan Cox boolean_t
1271e396eb60SAlan Cox moea_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1272e396eb60SAlan Cox {
1273e396eb60SAlan Cox 	struct pvo_entry *pvo;
1274e396eb60SAlan Cox 	boolean_t rv;
1275e396eb60SAlan Cox 
1276e396eb60SAlan Cox 	PMAP_LOCK(pmap);
1277e396eb60SAlan Cox 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1278e396eb60SAlan Cox 	rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0;
1279e396eb60SAlan Cox 	PMAP_UNLOCK(pmap);
1280e396eb60SAlan Cox 	return (rv);
1281e396eb60SAlan Cox }
1282e396eb60SAlan Cox 
12835244eac9SBenno Rice void
128459276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m)
12855244eac9SBenno Rice {
128603b6e025SPeter Grehan 
1287d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1288567e51e1SAlan Cox 	    ("moea_clear_reference: page %p is not managed", m));
1289ce186587SAlan Cox 	moea_clear_bit(m, PTE_REF);
129003b6e025SPeter Grehan }
129103b6e025SPeter Grehan 
129203b6e025SPeter Grehan void
129359276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
129403b6e025SPeter Grehan {
129503b6e025SPeter Grehan 
1296d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1297567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
1298567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1299567e51e1SAlan Cox 	KASSERT((m->oflags & VPO_BUSY) == 0,
1300567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is busy", m));
1301567e51e1SAlan Cox 
1302567e51e1SAlan Cox 	/*
13033407fefeSKonstantin Belousov 	 * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_CHG
1304567e51e1SAlan Cox 	 * set.  If the object containing the page is locked and the page is
13053407fefeSKonstantin Belousov 	 * not VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set.
1306567e51e1SAlan Cox 	 */
13073407fefeSKonstantin Belousov 	if ((m->aflags & PGA_WRITEABLE) == 0)
130803b6e025SPeter Grehan 		return;
1309ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
13105244eac9SBenno Rice }
13115244eac9SBenno Rice 
13127f3a4093SMike Silbersack /*
131378985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
131478985e42SAlan Cox  */
131578985e42SAlan Cox void
131678985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
131778985e42SAlan Cox {
131878985e42SAlan Cox 	struct	pvo_entry *pvo;
131978985e42SAlan Cox 	struct	pte *pt;
132078985e42SAlan Cox 	pmap_t	pmap;
132178985e42SAlan Cox 	u_int	lo;
132278985e42SAlan Cox 
1323d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
13249ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
13259ab6032fSAlan Cox 
13269ab6032fSAlan Cox 	/*
13273407fefeSKonstantin Belousov 	 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by
13283407fefeSKonstantin Belousov 	 * another thread while the object is locked.  Thus, if PGA_WRITEABLE
13299ab6032fSAlan Cox 	 * is clear, no page table entries need updating.
13309ab6032fSAlan Cox 	 */
13319ab6032fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
13329ab6032fSAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
13333407fefeSKonstantin Belousov 	    (m->aflags & PGA_WRITEABLE) == 0)
133478985e42SAlan Cox 		return;
13353c4a2440SAlan Cox 	vm_page_lock_queues();
133678985e42SAlan Cox 	lo = moea_attr_fetch(m);
1337e4f72b32SMarcel Moolenaar 	powerpc_sync();
133878985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
133978985e42SAlan Cox 		pmap = pvo->pvo_pmap;
134078985e42SAlan Cox 		PMAP_LOCK(pmap);
134152a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
134278985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
134352a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
134452a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
134578985e42SAlan Cox 			if (pt != NULL) {
134652a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
134752a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
134852a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
134952a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
135078985e42SAlan Cox 				    pvo->pvo_vaddr);
135178985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
135278985e42SAlan Cox 			}
135378985e42SAlan Cox 		}
135478985e42SAlan Cox 		PMAP_UNLOCK(pmap);
135578985e42SAlan Cox 	}
135678985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
135778985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
135878985e42SAlan Cox 		vm_page_dirty(m);
135978985e42SAlan Cox 	}
13603407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
13613c4a2440SAlan Cox 	vm_page_unlock_queues();
136278985e42SAlan Cox }
136378985e42SAlan Cox 
136478985e42SAlan Cox /*
136559276937SPeter Grehan  *	moea_ts_referenced:
13667f3a4093SMike Silbersack  *
13677f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
13687f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
13697f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
13707f3a4093SMike Silbersack  *	reference bits set.
13717f3a4093SMike Silbersack  *
13727f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
13737f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
13747f3a4093SMike Silbersack  *	optimal aging of shared pages.
13757f3a4093SMike Silbersack  */
137659276937SPeter Grehan boolean_t
137759276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
13785244eac9SBenno Rice {
137903b6e025SPeter Grehan 
1380d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1381ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
1382ce186587SAlan Cox 	return (moea_clear_bit(m, PTE_REF));
13835244eac9SBenno Rice }
13845244eac9SBenno Rice 
13855244eac9SBenno Rice /*
1386c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1387c1f4123bSNathan Whitehorn  */
1388c1f4123bSNathan Whitehorn void
1389c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma)
1390c1f4123bSNathan Whitehorn {
1391c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1392cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1393c1f4123bSNathan Whitehorn 	struct	pte *pt;
1394c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1395c1f4123bSNathan Whitehorn 	u_int	lo;
1396c1f4123bSNathan Whitehorn 
1397d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1398cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1399cd6a97f0SNathan Whitehorn 		return;
1400cd6a97f0SNathan Whitehorn 	}
1401cd6a97f0SNathan Whitehorn 
1402c1f4123bSNathan Whitehorn 	vm_page_lock_queues();
1403cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1404c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1405cd6a97f0SNathan Whitehorn 
1406cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1407c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1408c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1409c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1410c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1411c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1412c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1413c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1414c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1415c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1416c1f4123bSNathan Whitehorn 				isync();
1417c1f4123bSNathan Whitehorn 		}
1418c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1419c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1420c1f4123bSNathan Whitehorn 	}
1421c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
1422c1f4123bSNathan Whitehorn 	vm_page_unlock_queues();
1423c1f4123bSNathan Whitehorn }
1424c1f4123bSNathan Whitehorn 
1425c1f4123bSNathan Whitehorn /*
14265244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
14275244eac9SBenno Rice  */
14285244eac9SBenno Rice void
142959276937SPeter Grehan moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
14305244eac9SBenno Rice {
1431c1f4123bSNathan Whitehorn 
1432c1f4123bSNathan Whitehorn 	moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1433c1f4123bSNathan Whitehorn }
1434c1f4123bSNathan Whitehorn 
1435c1f4123bSNathan Whitehorn void
1436c1f4123bSNathan Whitehorn moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma)
1437c1f4123bSNathan Whitehorn {
14385244eac9SBenno Rice 	u_int		pte_lo;
14395244eac9SBenno Rice 	int		error;
14405244eac9SBenno Rice 
14415244eac9SBenno Rice #if 0
14425244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
144359276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
14445244eac9SBenno Rice 		    va);
14455244eac9SBenno Rice #endif
14465244eac9SBenno Rice 
1447c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
14485244eac9SBenno Rice 
14494711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
145059276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
145159276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
14525244eac9SBenno Rice 
14535244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
145459276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
14555244eac9SBenno Rice 		    pa, error);
14565244eac9SBenno Rice 
14575244eac9SBenno Rice 	/*
14585244eac9SBenno Rice 	 * Flush the real memory from the instruction cache.
14595244eac9SBenno Rice 	 */
14605244eac9SBenno Rice 	if ((pte_lo & (PTE_I | PTE_G)) == 0) {
146159276937SPeter Grehan 		moea_syncicache(pa, PAGE_SIZE);
14625244eac9SBenno Rice 	}
14634711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
14645244eac9SBenno Rice }
14655244eac9SBenno Rice 
1466e79f59e8SBenno Rice /*
1467e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1468e79f59e8SBenno Rice  * address.
1469e79f59e8SBenno Rice  */
14705244eac9SBenno Rice vm_offset_t
147159276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
14725244eac9SBenno Rice {
1473e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
147448d0b1a0SAlan Cox 	vm_paddr_t pa;
1475e79f59e8SBenno Rice 
14760efd0097SPeter Grehan 	/*
147752a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
14780efd0097SPeter Grehan 	 */
14790efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
14800efd0097SPeter Grehan 		return (va);
14810efd0097SPeter Grehan 	}
14820efd0097SPeter Grehan 
148348d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
148459276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
148559276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
148652a7870dSNathan Whitehorn 	pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
148748d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
148848d0b1a0SAlan Cox 	return (pa);
1489e79f59e8SBenno Rice }
1490e79f59e8SBenno Rice 
149188afb2a3SBenno Rice /*
149288afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
149388afb2a3SBenno Rice  */
14945244eac9SBenno Rice void
149559276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
14965244eac9SBenno Rice {
149788afb2a3SBenno Rice 
149859276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
14995244eac9SBenno Rice }
15005244eac9SBenno Rice 
15015244eac9SBenno Rice /*
15025244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
15035244eac9SBenno Rice  *
15045244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
15055244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
15065244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
15075244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
15085244eac9SBenno Rice  * first usable address after the mapped region.
15095244eac9SBenno Rice  */
15105244eac9SBenno Rice vm_offset_t
151159276937SPeter Grehan moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
151259276937SPeter Grehan     vm_offset_t pa_end, int prot)
15135244eac9SBenno Rice {
15145244eac9SBenno Rice 	vm_offset_t	sva, va;
15155244eac9SBenno Rice 
15165244eac9SBenno Rice 	sva = *virt;
15175244eac9SBenno Rice 	va = sva;
15185244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
151959276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
15205244eac9SBenno Rice 	*virt = va;
15215244eac9SBenno Rice 	return (sva);
15225244eac9SBenno Rice }
15235244eac9SBenno Rice 
15245244eac9SBenno Rice /*
15257f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
15267f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
15277f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
15287f3a4093SMike Silbersack  * is only necessary that true be returned for a small
15297f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
15307f3a4093SMike Silbersack  */
15315244eac9SBenno Rice boolean_t
153259276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
15335244eac9SBenno Rice {
153403b6e025SPeter Grehan         int loops;
153503b6e025SPeter Grehan 	struct pvo_entry *pvo;
1536ce186587SAlan Cox 	boolean_t rv;
153703b6e025SPeter Grehan 
1538d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1539ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
154003b6e025SPeter Grehan 	loops = 0;
1541ce186587SAlan Cox 	rv = FALSE;
1542ce186587SAlan Cox 	vm_page_lock_queues();
154303b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1544ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1545ce186587SAlan Cox 			rv = TRUE;
1546ce186587SAlan Cox 			break;
1547ce186587SAlan Cox 		}
154803b6e025SPeter Grehan 		if (++loops >= 16)
154903b6e025SPeter Grehan 			break;
155003b6e025SPeter Grehan 	}
1551ce186587SAlan Cox 	vm_page_unlock_queues();
1552ce186587SAlan Cox 	return (rv);
15535244eac9SBenno Rice }
15545244eac9SBenno Rice 
155559677d3cSAlan Cox /*
155659677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
155759677d3cSAlan Cox  * that are wired.
155859677d3cSAlan Cox  */
155959677d3cSAlan Cox int
156059677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
156159677d3cSAlan Cox {
156259677d3cSAlan Cox 	struct pvo_entry *pvo;
156359677d3cSAlan Cox 	int count;
156459677d3cSAlan Cox 
156559677d3cSAlan Cox 	count = 0;
1566d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0)
156759677d3cSAlan Cox 		return (count);
15683c4a2440SAlan Cox 	vm_page_lock_queues();
156959677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
157059677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
157159677d3cSAlan Cox 			count++;
15723c4a2440SAlan Cox 	vm_page_unlock_queues();
157359677d3cSAlan Cox 	return (count);
157459677d3cSAlan Cox }
157559677d3cSAlan Cox 
157659276937SPeter Grehan static u_int	moea_vsidcontext;
15775244eac9SBenno Rice 
15785244eac9SBenno Rice void
157959276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
15805244eac9SBenno Rice {
15815244eac9SBenno Rice 	int	i, mask;
15825244eac9SBenno Rice 	u_int	entropy;
15835244eac9SBenno Rice 
158459276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
158548d0b1a0SAlan Cox 	PMAP_LOCK_INIT(pmap);
1586*598d99ddSNathan Whitehorn 	LIST_INIT(&pmap->pmap_pvo);
15874daf20b2SPeter Grehan 
15885244eac9SBenno Rice 	entropy = 0;
15895244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
15905244eac9SBenno Rice 
159152a7870dSNathan Whitehorn 	if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
159252a7870dSNathan Whitehorn 	    == NULL) {
159352a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
159452a7870dSNathan Whitehorn 	}
159552a7870dSNathan Whitehorn 
159652a7870dSNathan Whitehorn 
1597e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
15985244eac9SBenno Rice 	/*
15995244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
16005244eac9SBenno Rice 	 */
16015244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
16025244eac9SBenno Rice 		u_int	hash, n;
16035244eac9SBenno Rice 
16045244eac9SBenno Rice 		/*
16055244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
16065244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
16075244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
16085244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
16095244eac9SBenno Rice 		 * instead of a multiply.)
16105244eac9SBenno Rice 		 */
161159276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
161259276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
16135244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
16145244eac9SBenno Rice 			continue;
16155244eac9SBenno Rice 		n = hash >> 5;
16165244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
161759276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
161859276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
16195244eac9SBenno Rice 			/* anything free in this bucket? */
162059276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
162159276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
16225244eac9SBenno Rice 				continue;
16235244eac9SBenno Rice 			}
16240dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
16255244eac9SBenno Rice 			mask = 1 << i;
16265244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
16275244eac9SBenno Rice 			hash |= i;
16285244eac9SBenno Rice 		}
162946e93cbbSNathan Whitehorn 		KASSERT(!(moea_vsid_bitmap[n] & mask),
163046e93cbbSNathan Whitehorn 		    ("Allocating in-use VSID group %#x\n", hash));
163159276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
16325244eac9SBenno Rice 		for (i = 0; i < 16; i++)
16335244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1634e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
16355244eac9SBenno Rice 		return;
16365244eac9SBenno Rice 	}
16375244eac9SBenno Rice 
1638e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
163959276937SPeter Grehan 	panic("moea_pinit: out of segments");
16405244eac9SBenno Rice }
16415244eac9SBenno Rice 
16425244eac9SBenno Rice /*
16435244eac9SBenno Rice  * Initialize the pmap associated with process 0.
16445244eac9SBenno Rice  */
16455244eac9SBenno Rice void
164659276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
16475244eac9SBenno Rice {
16485244eac9SBenno Rice 
164959276937SPeter Grehan 	moea_pinit(mmu, pm);
16505244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
16515244eac9SBenno Rice }
16525244eac9SBenno Rice 
1653e79f59e8SBenno Rice /*
1654e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1655e79f59e8SBenno Rice  */
16565244eac9SBenno Rice void
165759276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
165859276937SPeter Grehan     vm_prot_t prot)
16595244eac9SBenno Rice {
1660e79f59e8SBenno Rice 	struct	pvo_entry *pvo;
1661e79f59e8SBenno Rice 	struct	pte *pt;
1662e79f59e8SBenno Rice 	int	pteidx;
1663e79f59e8SBenno Rice 
1664e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
166559276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1666e79f59e8SBenno Rice 
1667e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
166859276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1669e79f59e8SBenno Rice 		return;
1670e79f59e8SBenno Rice 	}
1671e79f59e8SBenno Rice 
16723d2e54c3SAlan Cox 	vm_page_lock_queues();
167348d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1674e79f59e8SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
167559276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
1676e79f59e8SBenno Rice 		if (pvo == NULL)
1677e79f59e8SBenno Rice 			continue;
1678e79f59e8SBenno Rice 
1679e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1680e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1681e79f59e8SBenno Rice 
1682e79f59e8SBenno Rice 		/*
1683e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1684e79f59e8SBenno Rice 		 * copy.
1685e79f59e8SBenno Rice 		 */
168659276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, pteidx);
1687e79f59e8SBenno Rice 		/*
1688e79f59e8SBenno Rice 		 * Change the protection of the page.
1689e79f59e8SBenno Rice 		 */
169052a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
169152a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1692e79f59e8SBenno Rice 
1693e79f59e8SBenno Rice 		/*
1694e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1695e79f59e8SBenno Rice 		 */
1696d644a0b7SAlan Cox 		if (pt != NULL) {
169752a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1698d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1699d644a0b7SAlan Cox 		}
1700e79f59e8SBenno Rice 	}
17013d2e54c3SAlan Cox 	vm_page_unlock_queues();
170248d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
17035244eac9SBenno Rice }
17045244eac9SBenno Rice 
170588afb2a3SBenno Rice /*
170688afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
170788afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
170888afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
170988afb2a3SBenno Rice  */
17105244eac9SBenno Rice void
171159276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
17125244eac9SBenno Rice {
171303b6e025SPeter Grehan 	vm_offset_t va;
17145244eac9SBenno Rice 
171503b6e025SPeter Grehan 	va = sva;
171603b6e025SPeter Grehan 	while (count-- > 0) {
171759276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
171803b6e025SPeter Grehan 		va += PAGE_SIZE;
171903b6e025SPeter Grehan 		m++;
172003b6e025SPeter Grehan 	}
17215244eac9SBenno Rice }
17225244eac9SBenno Rice 
172388afb2a3SBenno Rice /*
172488afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
172559276937SPeter Grehan  * temporary mappings entered by moea_qenter.
172688afb2a3SBenno Rice  */
17275244eac9SBenno Rice void
172859276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
17295244eac9SBenno Rice {
173003b6e025SPeter Grehan 	vm_offset_t va;
173188afb2a3SBenno Rice 
173203b6e025SPeter Grehan 	va = sva;
173303b6e025SPeter Grehan 	while (count-- > 0) {
173459276937SPeter Grehan 		moea_kremove(mmu, va);
173503b6e025SPeter Grehan 		va += PAGE_SIZE;
173603b6e025SPeter Grehan 	}
17375244eac9SBenno Rice }
17385244eac9SBenno Rice 
17395244eac9SBenno Rice void
174059276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
17415244eac9SBenno Rice {
174232bc7846SPeter Grehan         int idx, mask;
174332bc7846SPeter Grehan 
174432bc7846SPeter Grehan 	/*
174532bc7846SPeter Grehan 	 * Free segment register's VSID
174632bc7846SPeter Grehan 	 */
174732bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
174859276937SPeter Grehan                 panic("moea_release");
174932bc7846SPeter Grehan 
1750e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
175132bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
175232bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
175332bc7846SPeter Grehan         idx /= VSID_NBPW;
175459276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1755e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
175648d0b1a0SAlan Cox 	PMAP_LOCK_DESTROY(pmap);
17575244eac9SBenno Rice }
17585244eac9SBenno Rice 
175988afb2a3SBenno Rice /*
176088afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
176188afb2a3SBenno Rice  */
17625244eac9SBenno Rice void
176359276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
17645244eac9SBenno Rice {
176588afb2a3SBenno Rice 	struct	pvo_entry *pvo;
176688afb2a3SBenno Rice 	int	pteidx;
176788afb2a3SBenno Rice 
17683d2e54c3SAlan Cox 	vm_page_lock_queues();
176948d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1770*598d99ddSNathan Whitehorn 	if ((eva - sva)/PAGE_SIZE < 10) {
177188afb2a3SBenno Rice 		for (; sva < eva; sva += PAGE_SIZE) {
177259276937SPeter Grehan 			pvo = moea_pvo_find_va(pm, sva, &pteidx);
1773*598d99ddSNathan Whitehorn 			if (pvo != NULL)
177459276937SPeter Grehan 				moea_pvo_remove(pvo, pteidx);
177588afb2a3SBenno Rice 		}
1776*598d99ddSNathan Whitehorn 	} else {
1777*598d99ddSNathan Whitehorn 		LIST_FOREACH(pvo, &pm->pmap_pvo, pvo_plink) {
1778*598d99ddSNathan Whitehorn 			if (PVO_VADDR(pvo) < sva || PVO_VADDR(pvo) >= eva)
1779*598d99ddSNathan Whitehorn 				continue;
1780*598d99ddSNathan Whitehorn 			moea_pvo_remove(pvo, -1);
1781*598d99ddSNathan Whitehorn 		}
178288afb2a3SBenno Rice 	}
178348d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
178494aa7aecSPeter Grehan 	vm_page_unlock_queues();
17855244eac9SBenno Rice }
17865244eac9SBenno Rice 
1787e79f59e8SBenno Rice /*
178859276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
178903b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
179003b6e025SPeter Grehan  */
179103b6e025SPeter Grehan void
179259276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
179303b6e025SPeter Grehan {
179403b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
179503b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
179648d0b1a0SAlan Cox 	pmap_t	pmap;
179703b6e025SPeter Grehan 
17983c4a2440SAlan Cox 	vm_page_lock_queues();
179903b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
180003b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
180103b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
180203b6e025SPeter Grehan 
180348d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
180448d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
180559276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
180648d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
180703b6e025SPeter Grehan 	}
18083407fefeSKonstantin Belousov 	if ((m->aflags & PGA_WRITEABLE) && moea_is_modified(mmu, m)) {
1809c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1810062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1811062c8f4cSNathan Whitehorn 	}
18123407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
18133c4a2440SAlan Cox 	vm_page_unlock_queues();
181403b6e025SPeter Grehan }
181503b6e025SPeter Grehan 
181603b6e025SPeter Grehan /*
18175244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
181859276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
18195244eac9SBenno Rice  * calculated.
18205244eac9SBenno Rice  */
18215244eac9SBenno Rice static vm_offset_t
182259276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
18235244eac9SBenno Rice {
18245244eac9SBenno Rice 	vm_offset_t	s, e;
18255244eac9SBenno Rice 	int		i, j;
18265244eac9SBenno Rice 
18275244eac9SBenno Rice 	size = round_page(size);
18285244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
18295244eac9SBenno Rice 		if (align != 0)
18305244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
18315244eac9SBenno Rice 		else
18325244eac9SBenno Rice 			s = phys_avail[i];
18335244eac9SBenno Rice 		e = s + size;
18345244eac9SBenno Rice 
18355244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
18365244eac9SBenno Rice 			continue;
18375244eac9SBenno Rice 
18385244eac9SBenno Rice 		if (s == phys_avail[i]) {
18395244eac9SBenno Rice 			phys_avail[i] += size;
18405244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
18415244eac9SBenno Rice 			phys_avail[i + 1] -= size;
18425244eac9SBenno Rice 		} else {
18435244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
18445244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
18455244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
18465244eac9SBenno Rice 			}
18475244eac9SBenno Rice 
18485244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
18495244eac9SBenno Rice 			phys_avail[i + 1] = s;
18505244eac9SBenno Rice 			phys_avail[i + 2] = e;
18515244eac9SBenno Rice 			phys_avail_count++;
18525244eac9SBenno Rice 		}
18535244eac9SBenno Rice 
18545244eac9SBenno Rice 		return (s);
18555244eac9SBenno Rice 	}
185659276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
18575244eac9SBenno Rice }
18585244eac9SBenno Rice 
18595244eac9SBenno Rice static void
186059276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
18615244eac9SBenno Rice {
18625244eac9SBenno Rice 	__syncicache((void *)pa, len);
18635244eac9SBenno Rice }
18645244eac9SBenno Rice 
18655244eac9SBenno Rice static int
186659276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
18675244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
18685244eac9SBenno Rice {
18695244eac9SBenno Rice 	struct	pvo_entry *pvo;
18705244eac9SBenno Rice 	u_int	sr;
18715244eac9SBenno Rice 	int	first;
18725244eac9SBenno Rice 	u_int	ptegidx;
18735244eac9SBenno Rice 	int	i;
187432bc7846SPeter Grehan 	int     bootstrap;
18755244eac9SBenno Rice 
187659276937SPeter Grehan 	moea_pvo_enter_calls++;
18778207b362SBenno Rice 	first = 0;
187832bc7846SPeter Grehan 	bootstrap = 0;
187932bc7846SPeter Grehan 
18805244eac9SBenno Rice 	/*
18815244eac9SBenno Rice 	 * Compute the PTE Group index.
18825244eac9SBenno Rice 	 */
18835244eac9SBenno Rice 	va &= ~ADDR_POFF;
18845244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
18855244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
18865244eac9SBenno Rice 
18875244eac9SBenno Rice 	/*
18885244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
18895244eac9SBenno Rice 	 * there is a mapping.
18905244eac9SBenno Rice 	 */
189159276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
189259276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
18935244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
189452a7870dSNathan Whitehorn 			if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
189552a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1896fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
189759276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
189849f8f727SBenno Rice 				return (0);
1899fafc7362SBenno Rice 			}
190059276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
19015244eac9SBenno Rice 			break;
19025244eac9SBenno Rice 		}
19035244eac9SBenno Rice 	}
19045244eac9SBenno Rice 
19055244eac9SBenno Rice 	/*
19065244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
19075244eac9SBenno Rice 	 */
190859276937SPeter Grehan 	if (moea_initialized) {
1909378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
191049f8f727SBenno Rice 	} else {
191159276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
191259276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
191359276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
19140d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
191549f8f727SBenno Rice 		}
191659276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
191759276937SPeter Grehan 		moea_bpvo_pool_index++;
191832bc7846SPeter Grehan 		bootstrap = 1;
191949f8f727SBenno Rice 	}
19205244eac9SBenno Rice 
19215244eac9SBenno Rice 	if (pvo == NULL) {
192259276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
19235244eac9SBenno Rice 		return (ENOMEM);
19245244eac9SBenno Rice 	}
19255244eac9SBenno Rice 
192659276937SPeter Grehan 	moea_pvo_entries++;
19275244eac9SBenno Rice 	pvo->pvo_vaddr = va;
19285244eac9SBenno Rice 	pvo->pvo_pmap = pm;
192959276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
19305244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
19315244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
19325244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
19335244eac9SBenno Rice 	if (flags & PVO_WIRED)
19345244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
193559276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
19365244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
193732bc7846SPeter Grehan 	if (bootstrap)
193832bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
19394dba5df1SPeter Grehan 
194052a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
19415244eac9SBenno Rice 
19425244eac9SBenno Rice 	/*
1943*598d99ddSNathan Whitehorn 	 * Add to pmap list
1944*598d99ddSNathan Whitehorn 	 */
1945*598d99ddSNathan Whitehorn 	LIST_INSERT_HEAD(&pm->pmap_pvo, pvo, pvo_plink);
1946*598d99ddSNathan Whitehorn 
1947*598d99ddSNathan Whitehorn 	/*
19485244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
19495244eac9SBenno Rice 	 * item.
19505244eac9SBenno Rice 	 */
19518207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
19528207b362SBenno Rice 		first = 1;
19535244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
19544dba5df1SPeter Grehan 
195552a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
1956c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
1957c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
19585244eac9SBenno Rice 
19595244eac9SBenno Rice 	/*
19605244eac9SBenno Rice 	 * We hope this succeeds but it isn't required.
19615244eac9SBenno Rice 	 */
196252a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
19635244eac9SBenno Rice 	if (i >= 0) {
19645244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
19655244eac9SBenno Rice 	} else {
196659276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
196759276937SPeter Grehan 		moea_pte_overflow++;
19685244eac9SBenno Rice 	}
196959276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
19704dba5df1SPeter Grehan 
19715244eac9SBenno Rice 	return (first ? ENOENT : 0);
19725244eac9SBenno Rice }
19735244eac9SBenno Rice 
19745244eac9SBenno Rice static void
197559276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
19765244eac9SBenno Rice {
19775244eac9SBenno Rice 	struct	pte *pt;
19785244eac9SBenno Rice 
19795244eac9SBenno Rice 	/*
19805244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
19815244eac9SBenno Rice 	 * save the ref & cfg bits).
19825244eac9SBenno Rice 	 */
198359276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
19845244eac9SBenno Rice 	if (pt != NULL) {
198552a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1986d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
19875244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
19885244eac9SBenno Rice 	} else {
198959276937SPeter Grehan 		moea_pte_overflow--;
19905244eac9SBenno Rice 	}
19915244eac9SBenno Rice 
19925244eac9SBenno Rice 	/*
19935244eac9SBenno Rice 	 * Update our statistics.
19945244eac9SBenno Rice 	 */
19955244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
199652a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
19975244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
19985244eac9SBenno Rice 
19995244eac9SBenno Rice 	/*
20005244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
20015244eac9SBenno Rice 	 */
2002d98d0ce2SKonstantin Belousov 	if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED) {
20035244eac9SBenno Rice 		struct	vm_page *pg;
20045244eac9SBenno Rice 
200552a7870dSNathan Whitehorn 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
20065244eac9SBenno Rice 		if (pg != NULL) {
200752a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
20085244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
20095244eac9SBenno Rice 		}
20105244eac9SBenno Rice 	}
20115244eac9SBenno Rice 
20125244eac9SBenno Rice 	/*
2013*598d99ddSNathan Whitehorn 	 * Remove this PVO from the PV and pmap lists.
20145244eac9SBenno Rice 	 */
20155244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
2016*598d99ddSNathan Whitehorn 	LIST_REMOVE(pvo, pvo_plink);
20175244eac9SBenno Rice 
20185244eac9SBenno Rice 	/*
20195244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
20205244eac9SBenno Rice 	 * if we aren't going to reuse it.
20215244eac9SBenno Rice 	 */
20225244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
202349f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
202459276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
202559276937SPeter Grehan 		    moea_upvo_zone, pvo);
202659276937SPeter Grehan 	moea_pvo_entries--;
202759276937SPeter Grehan 	moea_pvo_remove_calls++;
20285244eac9SBenno Rice }
20295244eac9SBenno Rice 
20305244eac9SBenno Rice static __inline int
203159276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
20325244eac9SBenno Rice {
20335244eac9SBenno Rice 	int	pteidx;
20345244eac9SBenno Rice 
20355244eac9SBenno Rice 	/*
20365244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
20375244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
20385244eac9SBenno Rice 	 * noticing the HID bit.
20395244eac9SBenno Rice 	 */
20405244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
204152a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
204259276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
20435244eac9SBenno Rice 
20445244eac9SBenno Rice 	return (pteidx);
20455244eac9SBenno Rice }
20465244eac9SBenno Rice 
20475244eac9SBenno Rice static struct pvo_entry *
204859276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
20495244eac9SBenno Rice {
20505244eac9SBenno Rice 	struct	pvo_entry *pvo;
20515244eac9SBenno Rice 	int	ptegidx;
20525244eac9SBenno Rice 	u_int	sr;
20535244eac9SBenno Rice 
20545244eac9SBenno Rice 	va &= ~ADDR_POFF;
20555244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
20565244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
20575244eac9SBenno Rice 
205859276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
205959276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20605244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
20615244eac9SBenno Rice 			if (pteidx_p)
206259276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2063f489bf21SAlan Cox 			break;
20645244eac9SBenno Rice 		}
20655244eac9SBenno Rice 	}
206659276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20675244eac9SBenno Rice 
2068f489bf21SAlan Cox 	return (pvo);
20695244eac9SBenno Rice }
20705244eac9SBenno Rice 
20715244eac9SBenno Rice static struct pte *
207259276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
20735244eac9SBenno Rice {
20745244eac9SBenno Rice 	struct	pte *pt;
20755244eac9SBenno Rice 
20765244eac9SBenno Rice 	/*
20775244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
20785244eac9SBenno Rice 	 */
20795244eac9SBenno Rice 	if (pteidx == -1) {
20805244eac9SBenno Rice 		int	ptegidx;
20815244eac9SBenno Rice 		u_int	sr;
20825244eac9SBenno Rice 
20835244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
20845244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
208559276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
20865244eac9SBenno Rice 	}
20875244eac9SBenno Rice 
208859276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2089d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
20905244eac9SBenno Rice 
209152a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
209259276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
20935244eac9SBenno Rice 		    "valid pte index", pvo);
20945244eac9SBenno Rice 	}
20955244eac9SBenno Rice 
209652a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
209759276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
20985244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
20995244eac9SBenno Rice 	}
21005244eac9SBenno Rice 
210152a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
210252a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
210359276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
210459276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
21055244eac9SBenno Rice 		}
21065244eac9SBenno Rice 
210752a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
21085244eac9SBenno Rice 		    != 0) {
210959276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
211059276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
21115244eac9SBenno Rice 		}
21125244eac9SBenno Rice 
2113d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
21145244eac9SBenno Rice 		return (pt);
21155244eac9SBenno Rice 	}
21165244eac9SBenno Rice 
211752a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
211859276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
211959276937SPeter Grehan 		    "moea_pteg_table but valid in pvo", pvo, pt);
21205244eac9SBenno Rice 	}
21215244eac9SBenno Rice 
2122d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
21235244eac9SBenno Rice 	return (NULL);
21245244eac9SBenno Rice }
21255244eac9SBenno Rice 
21265244eac9SBenno Rice /*
21275244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
21285244eac9SBenno Rice  */
21295244eac9SBenno Rice int
213059276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
21315244eac9SBenno Rice {
21325244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
21335244eac9SBenno Rice 	struct	pvo_entry *pvo;
21345244eac9SBenno Rice 	int	ptegidx, i, j;
21355244eac9SBenno Rice 	u_int	sr;
21365244eac9SBenno Rice 	struct	pteg *pteg;
21375244eac9SBenno Rice 	struct	pte *pt;
21385244eac9SBenno Rice 
213959276937SPeter Grehan 	moea_pte_spills++;
21405244eac9SBenno Rice 
2141d080d5fdSBenno Rice 	sr = mfsrin(addr);
21425244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
21435244eac9SBenno Rice 
21445244eac9SBenno Rice 	/*
21455244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
21465244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
21475244eac9SBenno Rice 	 */
214859276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
214959276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
21505244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
21515244eac9SBenno Rice 	i &= 7;
21525244eac9SBenno Rice 	pt = &pteg->pt[i];
21535244eac9SBenno Rice 
21545244eac9SBenno Rice 	source_pvo = NULL;
21555244eac9SBenno Rice 	victim_pvo = NULL;
215659276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21575244eac9SBenno Rice 		/*
21585244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
21595244eac9SBenno Rice 		 */
21605244eac9SBenno Rice 		if (source_pvo == NULL &&
216152a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
216252a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
21635244eac9SBenno Rice 			/*
21645244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
21655244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
21665244eac9SBenno Rice 			 */
216752a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
21685244eac9SBenno Rice 
21695244eac9SBenno Rice 			if (j >= 0) {
21705244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
217159276937SPeter Grehan 				moea_pte_overflow--;
217259276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
21735244eac9SBenno Rice 				return (1);
21745244eac9SBenno Rice 			}
21755244eac9SBenno Rice 
21765244eac9SBenno Rice 			source_pvo = pvo;
21775244eac9SBenno Rice 
21785244eac9SBenno Rice 			if (victim_pvo != NULL)
21795244eac9SBenno Rice 				break;
21805244eac9SBenno Rice 		}
21815244eac9SBenno Rice 
21825244eac9SBenno Rice 		/*
21835244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
21845244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
21855244eac9SBenno Rice 		 */
21865244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
218752a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
21885244eac9SBenno Rice 			victim_pvo = pvo;
21895244eac9SBenno Rice 			if (source_pvo != NULL)
21905244eac9SBenno Rice 				break;
21915244eac9SBenno Rice 		}
21925244eac9SBenno Rice 	}
21935244eac9SBenno Rice 
2194f489bf21SAlan Cox 	if (source_pvo == NULL) {
219559276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
21965244eac9SBenno Rice 		return (0);
2197f489bf21SAlan Cox 	}
21985244eac9SBenno Rice 
21995244eac9SBenno Rice 	if (victim_pvo == NULL) {
22005244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
220159276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
22025244eac9SBenno Rice 			    "entry", pt);
22035244eac9SBenno Rice 
22045244eac9SBenno Rice 		/*
22055244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
22065244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
22075244eac9SBenno Rice 		 */
220859276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
22095244eac9SBenno Rice 		    pvo_olink) {
22105244eac9SBenno Rice 			/*
22115244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
22125244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
22135244eac9SBenno Rice 			 */
221452a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22155244eac9SBenno Rice 				victim_pvo = pvo;
22165244eac9SBenno Rice 				break;
22175244eac9SBenno Rice 			}
22185244eac9SBenno Rice 		}
22195244eac9SBenno Rice 
22205244eac9SBenno Rice 		if (victim_pvo == NULL)
222159276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
22225244eac9SBenno Rice 			    "entry", pt);
22235244eac9SBenno Rice 	}
22245244eac9SBenno Rice 
22255244eac9SBenno Rice 	/*
22265244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
22275244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
22285244eac9SBenno Rice 	 * contained in the TLB entry.
22295244eac9SBenno Rice 	 */
223052a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
22315244eac9SBenno Rice 
223252a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
223352a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
22345244eac9SBenno Rice 
22355244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
22365244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
223759276937SPeter Grehan 	moea_pte_replacements++;
22385244eac9SBenno Rice 
223959276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22405244eac9SBenno Rice 	return (1);
22415244eac9SBenno Rice }
22425244eac9SBenno Rice 
22435244eac9SBenno Rice static int
224459276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
22455244eac9SBenno Rice {
22465244eac9SBenno Rice 	struct	pte *pt;
22475244eac9SBenno Rice 	int	i;
22485244eac9SBenno Rice 
2249d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2250d644a0b7SAlan Cox 
22515244eac9SBenno Rice 	/*
22525244eac9SBenno Rice 	 * First try primary hash.
22535244eac9SBenno Rice 	 */
225459276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22555244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22565244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
225759276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22585244eac9SBenno Rice 			return (i);
22595244eac9SBenno Rice 		}
22605244eac9SBenno Rice 	}
22615244eac9SBenno Rice 
22625244eac9SBenno Rice 	/*
22635244eac9SBenno Rice 	 * Now try secondary hash.
22645244eac9SBenno Rice 	 */
226559276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2266bd8e6f87SPeter Grehan 
226759276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22685244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22695244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
227059276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22715244eac9SBenno Rice 			return (i);
22725244eac9SBenno Rice 		}
22735244eac9SBenno Rice 	}
22745244eac9SBenno Rice 
227559276937SPeter Grehan 	panic("moea_pte_insert: overflow");
22765244eac9SBenno Rice 	return (-1);
22775244eac9SBenno Rice }
22785244eac9SBenno Rice 
22795244eac9SBenno Rice static boolean_t
228059276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
22815244eac9SBenno Rice {
22825244eac9SBenno Rice 	struct	pvo_entry *pvo;
22835244eac9SBenno Rice 	struct	pte *pt;
22845244eac9SBenno Rice 
228559276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
22865244eac9SBenno Rice 		return (TRUE);
22875244eac9SBenno Rice 
2288c46b90e9SAlan Cox 	vm_page_lock_queues();
22895244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
22905244eac9SBenno Rice 
22915244eac9SBenno Rice 		/*
22925244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
22935244eac9SBenno Rice 		 * success.
22945244eac9SBenno Rice 		 */
229552a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
229659276937SPeter Grehan 			moea_attr_save(m, ptebit);
2297c46b90e9SAlan Cox 			vm_page_unlock_queues();
22985244eac9SBenno Rice 			return (TRUE);
22995244eac9SBenno Rice 		}
23005244eac9SBenno Rice 	}
23015244eac9SBenno Rice 
23025244eac9SBenno Rice 	/*
23035244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
23045244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
23055244eac9SBenno Rice 	 * the PTEs.
23065244eac9SBenno Rice 	 */
2307e4f72b32SMarcel Moolenaar 	powerpc_sync();
23085244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
23095244eac9SBenno Rice 
23105244eac9SBenno Rice 		/*
23115244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
23125244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
23135244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
23145244eac9SBenno Rice 		 */
231559276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23165244eac9SBenno Rice 		if (pt != NULL) {
231752a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2318d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
231952a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
232059276937SPeter Grehan 				moea_attr_save(m, ptebit);
2321c46b90e9SAlan Cox 				vm_page_unlock_queues();
23225244eac9SBenno Rice 				return (TRUE);
23235244eac9SBenno Rice 			}
23245244eac9SBenno Rice 		}
23255244eac9SBenno Rice 	}
23265244eac9SBenno Rice 
2327c46b90e9SAlan Cox 	vm_page_unlock_queues();
23284f7daed0SAndrew Gallatin 	return (FALSE);
23295244eac9SBenno Rice }
23305244eac9SBenno Rice 
233103b6e025SPeter Grehan static u_int
2332ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
23335244eac9SBenno Rice {
233403b6e025SPeter Grehan 	u_int	count;
23355244eac9SBenno Rice 	struct	pvo_entry *pvo;
23365244eac9SBenno Rice 	struct	pte *pt;
2337ce186587SAlan Cox 
2338ce186587SAlan Cox 	vm_page_lock_queues();
23395244eac9SBenno Rice 
23405244eac9SBenno Rice 	/*
23415244eac9SBenno Rice 	 * Clear the cached value.
23425244eac9SBenno Rice 	 */
234359276937SPeter Grehan 	moea_attr_clear(m, ptebit);
23445244eac9SBenno Rice 
23455244eac9SBenno Rice 	/*
23465244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
23475244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
23485244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
23495244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
23505244eac9SBenno Rice 	 * REF/CHG bits.
23515244eac9SBenno Rice 	 */
2352e4f72b32SMarcel Moolenaar 	powerpc_sync();
23535244eac9SBenno Rice 
23545244eac9SBenno Rice 	/*
23555244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
23565244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
23575244eac9SBenno Rice 	 */
235803b6e025SPeter Grehan 	count = 0;
23595244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
236059276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23615244eac9SBenno Rice 		if (pt != NULL) {
236252a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
236352a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
236403b6e025SPeter Grehan 				count++;
236559276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
23665244eac9SBenno Rice 			}
2367d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
236803b6e025SPeter Grehan 		}
236952a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
23705244eac9SBenno Rice 	}
23715244eac9SBenno Rice 
2372ce186587SAlan Cox 	vm_page_unlock_queues();
237303b6e025SPeter Grehan 	return (count);
2374bdf71f56SBenno Rice }
23758bbfa33aSBenno Rice 
23768bbfa33aSBenno Rice /*
237732bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
237832bc7846SPeter Grehan  */
237932bc7846SPeter Grehan static int
238059276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
238132bc7846SPeter Grehan {
238232bc7846SPeter Grehan 	u_int prot;
238332bc7846SPeter Grehan 	u_int32_t start;
238432bc7846SPeter Grehan 	u_int32_t end;
238532bc7846SPeter Grehan 	u_int32_t bat_ble;
238632bc7846SPeter Grehan 
238732bc7846SPeter Grehan 	/*
238832bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
238932bc7846SPeter Grehan 	 */
2390c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
239132bc7846SPeter Grehan 		return (EINVAL);
239232bc7846SPeter Grehan 
239332bc7846SPeter Grehan 	/*
239432bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
239532bc7846SPeter Grehan 	 * so it can function as an i/o page
239632bc7846SPeter Grehan 	 */
239732bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
239832bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
239932bc7846SPeter Grehan 		return (EPERM);
240032bc7846SPeter Grehan 
240132bc7846SPeter Grehan 	/*
240232bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
240332bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
240432bc7846SPeter Grehan 	 * not requiring masking)
240532bc7846SPeter Grehan 	 */
240632bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
240732bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
240832bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
240932bc7846SPeter Grehan 
241032bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
241132bc7846SPeter Grehan 		return (ERANGE);
241232bc7846SPeter Grehan 
241332bc7846SPeter Grehan 	return (0);
241432bc7846SPeter Grehan }
241532bc7846SPeter Grehan 
241659276937SPeter Grehan boolean_t
241759276937SPeter Grehan moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2418c0763d37SSuleiman Souhlal {
2419c0763d37SSuleiman Souhlal 	int i;
2420c0763d37SSuleiman Souhlal 
2421c0763d37SSuleiman Souhlal 	/*
2422c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2423c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2424c0763d37SSuleiman Souhlal 	 */
2425c0763d37SSuleiman Souhlal 
2426c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
242759276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2428c0763d37SSuleiman Souhlal 			return (0);
2429c0763d37SSuleiman Souhlal 
2430c0763d37SSuleiman Souhlal 	return (EFAULT);
2431c0763d37SSuleiman Souhlal }
243232bc7846SPeter Grehan 
243332bc7846SPeter Grehan /*
24348bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
24358bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
24368bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
24378bbfa33aSBenno Rice  * NOT real memory.
24388bbfa33aSBenno Rice  */
24398bbfa33aSBenno Rice void *
244059276937SPeter Grehan moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
24418bbfa33aSBenno Rice {
2442c1f4123bSNathan Whitehorn 
2443c1f4123bSNathan Whitehorn 	return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2444c1f4123bSNathan Whitehorn }
2445c1f4123bSNathan Whitehorn 
2446c1f4123bSNathan Whitehorn void *
2447c1f4123bSNathan Whitehorn moea_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma)
2448c1f4123bSNathan Whitehorn {
244932bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
245032bc7846SPeter Grehan 	int i;
24518bbfa33aSBenno Rice 
245232bc7846SPeter Grehan 	ppa = trunc_page(pa);
24538bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
24548bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
24558bbfa33aSBenno Rice 
245632bc7846SPeter Grehan 	/*
245732bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
245832bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
245932bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
246032bc7846SPeter Grehan 	 */
246132bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
246259276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
246332bc7846SPeter Grehan 			return ((void *) pa);
246432bc7846SPeter Grehan 	}
246532bc7846SPeter Grehan 
2466e53f32acSAlan Cox 	va = kmem_alloc_nofault(kernel_map, size);
24678bbfa33aSBenno Rice 	if (!va)
246859276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
24698bbfa33aSBenno Rice 
24708bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
2471c1f4123bSNathan Whitehorn 		moea_kenter_attr(mmu, tmpva, ppa, ma);
2472e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
24738bbfa33aSBenno Rice 		size -= PAGE_SIZE;
24748bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
247532bc7846SPeter Grehan 		ppa += PAGE_SIZE;
24768bbfa33aSBenno Rice 	}
24778bbfa33aSBenno Rice 
24788bbfa33aSBenno Rice 	return ((void *)(va + offset));
24798bbfa33aSBenno Rice }
24808bbfa33aSBenno Rice 
24818bbfa33aSBenno Rice void
248259276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
24838bbfa33aSBenno Rice {
24848bbfa33aSBenno Rice 	vm_offset_t base, offset;
24858bbfa33aSBenno Rice 
248632bc7846SPeter Grehan 	/*
248732bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
248832bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
248932bc7846SPeter Grehan 	 */
2490ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
24918bbfa33aSBenno Rice 		base = trunc_page(va);
24928bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
24938bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
24948bbfa33aSBenno Rice 		kmem_free(kernel_map, base, size);
24958bbfa33aSBenno Rice 	}
249632bc7846SPeter Grehan }
24971a4fcaebSMarcel Moolenaar 
24981a4fcaebSMarcel Moolenaar static void
24991a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
25001a4fcaebSMarcel Moolenaar {
25011a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
25021a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
25031a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
25041a4fcaebSMarcel Moolenaar 	vm_size_t len;
25051a4fcaebSMarcel Moolenaar 
25061a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
25071a4fcaebSMarcel Moolenaar 	while (sz > 0) {
25081a4fcaebSMarcel Moolenaar 		lim = round_page(va);
25091a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
25101a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
25111a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
25121a4fcaebSMarcel Moolenaar 			pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
25131a4fcaebSMarcel Moolenaar 			    (va & ADDR_POFF);
25141a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
25151a4fcaebSMarcel Moolenaar 		}
25161a4fcaebSMarcel Moolenaar 		va += len;
25171a4fcaebSMarcel Moolenaar 		sz -= len;
25181a4fcaebSMarcel Moolenaar 	}
25191a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
25201a4fcaebSMarcel Moolenaar }
2521