xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision 4053b05b9161cc3c080cdded40d10df0b91c86be)
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>
1215244eac9SBenno Rice #include <sys/ktr.h>
12294e0b85eSMark Peek #include <sys/lock.h>
1235244eac9SBenno Rice #include <sys/msgbuf.h>
124f9bac91bSBenno Rice #include <sys/mutex.h>
1255244eac9SBenno Rice #include <sys/proc.h>
1265244eac9SBenno Rice #include <sys/sysctl.h>
1275244eac9SBenno Rice #include <sys/systm.h>
1285244eac9SBenno Rice #include <sys/vmmeter.h>
1295244eac9SBenno Rice 
1305244eac9SBenno Rice #include <dev/ofw/openfirm.h>
131f9bac91bSBenno Rice 
132f9bac91bSBenno Rice #include <vm/vm.h>
133f9bac91bSBenno Rice #include <vm/vm_param.h>
134f9bac91bSBenno Rice #include <vm/vm_kern.h>
135f9bac91bSBenno Rice #include <vm/vm_page.h>
136f9bac91bSBenno Rice #include <vm/vm_map.h>
137f9bac91bSBenno Rice #include <vm/vm_object.h>
138f9bac91bSBenno Rice #include <vm/vm_extern.h>
139f9bac91bSBenno Rice #include <vm/vm_pageout.h>
140f9bac91bSBenno Rice #include <vm/vm_pager.h>
141378862a7SJeff Roberson #include <vm/uma.h>
142f9bac91bSBenno Rice 
1437c277971SPeter Grehan #include <machine/cpu.h>
144b40ce02aSNathan Whitehorn #include <machine/platform.h>
145d699b539SMark Peek #include <machine/bat.h>
1465244eac9SBenno Rice #include <machine/frame.h>
1475244eac9SBenno Rice #include <machine/md_var.h>
1485244eac9SBenno Rice #include <machine/psl.h>
149f9bac91bSBenno Rice #include <machine/pte.h>
15012640815SMarcel Moolenaar #include <machine/smp.h>
1515244eac9SBenno Rice #include <machine/sr.h>
15259276937SPeter Grehan #include <machine/mmuvar.h>
153f9bac91bSBenno Rice 
15459276937SPeter Grehan #include "mmu_if.h"
15559276937SPeter Grehan 
15659276937SPeter Grehan #define	MOEA_DEBUG
157f9bac91bSBenno Rice 
1585244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
159f9bac91bSBenno Rice 
1605244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1615244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1625244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1635244eac9SBenno Rice 
16459276937SPeter Grehan #define	MOEA_PVO_CHECK(pvo)
1655244eac9SBenno Rice 
1665244eac9SBenno Rice struct ofw_map {
1675244eac9SBenno Rice 	vm_offset_t	om_va;
1685244eac9SBenno Rice 	vm_size_t	om_len;
1695244eac9SBenno Rice 	vm_offset_t	om_pa;
1705244eac9SBenno Rice 	u_int		om_mode;
1715244eac9SBenno Rice };
172f9bac91bSBenno Rice 
1735244eac9SBenno Rice /*
1745244eac9SBenno Rice  * Map of physical memory regions.
1755244eac9SBenno Rice  */
17631c82d03SBenno Rice static struct	mem_region *regions;
17731c82d03SBenno Rice static struct	mem_region *pregions;
178c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
179c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
180aa39961eSBenno Rice static struct	ofw_map *translations;
1815244eac9SBenno Rice 
182f9bac91bSBenno Rice /*
183f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
184f489bf21SAlan Cox  */
18559276937SPeter Grehan struct mtx	moea_table_mutex;
186e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
187f489bf21SAlan Cox 
188e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
189e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
190e4f72b32SMarcel Moolenaar 
191f489bf21SAlan Cox /*
1925244eac9SBenno Rice  * PTEG data.
193f9bac91bSBenno Rice  */
19459276937SPeter Grehan static struct	pteg *moea_pteg_table;
19559276937SPeter Grehan u_int		moea_pteg_count;
19659276937SPeter Grehan u_int		moea_pteg_mask;
1975244eac9SBenno Rice 
1985244eac9SBenno Rice /*
1995244eac9SBenno Rice  * PVO data.
2005244eac9SBenno Rice  */
20159276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
20259276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
20359276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
2045244eac9SBenno Rice 
20559276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
20659276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2075244eac9SBenno Rice 
2080d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
20959276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
21059276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2115244eac9SBenno Rice 
2125244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
21359276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2145244eac9SBenno Rice 
21559276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2165244eac9SBenno Rice 
2175244eac9SBenno Rice /*
2185244eac9SBenno Rice  * Statistics.
2195244eac9SBenno Rice  */
22059276937SPeter Grehan u_int	moea_pte_valid = 0;
22159276937SPeter Grehan u_int	moea_pte_overflow = 0;
22259276937SPeter Grehan u_int	moea_pte_replacements = 0;
22359276937SPeter Grehan u_int	moea_pvo_entries = 0;
22459276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
22559276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
22659276937SPeter Grehan u_int	moea_pte_spills = 0;
22759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2285244eac9SBenno Rice     0, "");
22959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
23059276937SPeter Grehan     &moea_pte_overflow, 0, "");
23159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
23259276937SPeter Grehan     &moea_pte_replacements, 0, "");
23359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2345244eac9SBenno Rice     0, "");
23559276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
23659276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
23759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
23859276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
23959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
24059276937SPeter Grehan     &moea_pte_spills, 0, "");
2415244eac9SBenno Rice 
2425244eac9SBenno Rice /*
24359276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2445244eac9SBenno Rice  */
24559276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2465244eac9SBenno Rice 
2475244eac9SBenno Rice /*
2485244eac9SBenno Rice  * PTE calls.
2495244eac9SBenno Rice  */
25059276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2515244eac9SBenno Rice 
2525244eac9SBenno Rice /*
2535244eac9SBenno Rice  * PVO calls.
2545244eac9SBenno Rice  */
25559276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2565244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
25759276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
25859276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
25959276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2605244eac9SBenno Rice 
2615244eac9SBenno Rice /*
2625244eac9SBenno Rice  * Utility routines.
2635244eac9SBenno Rice  */
264ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
265ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
26659276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
26759276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
268ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
26959276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
27059276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
27159276937SPeter Grehan 
27259276937SPeter Grehan /*
27359276937SPeter Grehan  * Kernel MMU interface
27459276937SPeter Grehan  */
27559276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
27659276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
27759276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t);
27859276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
27959276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
280ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
281ce142d9eSAlan Cox     vm_prot_t);
2822053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
28359276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
28459276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
28559276937SPeter Grehan void moea_init(mmu_t);
28659276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
287e396eb60SAlan Cox boolean_t moea_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
2887b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t);
28959276937SPeter Grehan boolean_t moea_ts_referenced(mmu_t, vm_page_t);
29059276937SPeter Grehan vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int);
29159276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
29259677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
29359276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
29459276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
29559276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
29659276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
29759276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
29859276937SPeter Grehan void moea_release(mmu_t, pmap_t);
29959276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
30059276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
30178985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
30259276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
30359276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
30459276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
30559276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
30659276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
3071c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int);
30859276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
30959276937SPeter Grehan void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t);
310c1f4123bSNathan Whitehorn void *moea_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t);
31159276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
31259276937SPeter Grehan vm_offset_t moea_kextract(mmu_t, vm_offset_t);
313c1f4123bSNathan Whitehorn void moea_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t);
31459276937SPeter Grehan void moea_kenter(mmu_t, vm_offset_t, vm_offset_t);
315c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma);
31659276937SPeter Grehan boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
3171a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
31859276937SPeter Grehan 
31959276937SPeter Grehan static mmu_method_t moea_methods[] = {
32059276937SPeter Grehan 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
32159276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
32259276937SPeter Grehan 	MMUMETHOD(mmu_clear_reference,	moea_clear_reference),
32359276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
32459276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
325ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
32659276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
32759276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
32859276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
32959276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
33059276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
331e396eb60SAlan Cox 	MMUMETHOD(mmu_is_prefaultable,	moea_is_prefaultable),
3327b85f591SAlan Cox 	MMUMETHOD(mmu_is_referenced,	moea_is_referenced),
33359276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
33459276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
33559276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
33659677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
33759276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
33859276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
33959276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
34059276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
34159276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
34259276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
34359276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
34459276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
34578985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
3461a4fcaebSMarcel Moolenaar 	MMUMETHOD(mmu_sync_icache,	moea_sync_icache),
34759276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
34859276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
34959276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
35059276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
35159276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
352c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_page_set_memattr,	moea_page_set_memattr),
35359276937SPeter Grehan 
35459276937SPeter Grehan 	/* Internal interfaces */
35559276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
3561c96bdd1SNathan Whitehorn 	MMUMETHOD(mmu_cpu_bootstrap,   	moea_cpu_bootstrap),
357c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_mapdev_attr,	moea_mapdev_attr),
35859276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
35959276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
36059276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
36159276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
362c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_kenter_attr,	moea_kenter_attr),
36359276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
36459276937SPeter Grehan 
36559276937SPeter Grehan 	{ 0, 0 }
36659276937SPeter Grehan };
36759276937SPeter Grehan 
36833529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0);
36933529b98SPeter Grehan 
370c1f4123bSNathan Whitehorn static __inline uint32_t
371c1f4123bSNathan Whitehorn moea_calc_wimg(vm_offset_t pa, vm_memattr_t ma)
372c1f4123bSNathan Whitehorn {
373c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
374c1f4123bSNathan Whitehorn 	int i;
375c1f4123bSNathan Whitehorn 
376c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
377c1f4123bSNathan Whitehorn 		switch (ma) {
378c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
379c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
380c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
381c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
382c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
383c1f4123bSNathan Whitehorn 			return (PTE_I);
384c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
385c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
386c1f4123bSNathan Whitehorn 		}
387c1f4123bSNathan Whitehorn 	}
388c1f4123bSNathan Whitehorn 
389c1f4123bSNathan Whitehorn 	/*
390c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
391c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
392c1f4123bSNathan Whitehorn 	 */
393c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
394c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
395c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
396c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
397c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
398c1f4123bSNathan Whitehorn 			break;
399c1f4123bSNathan Whitehorn 		}
400c1f4123bSNathan Whitehorn 	}
401c1f4123bSNathan Whitehorn 
402c1f4123bSNathan Whitehorn 	return pte_lo;
403c1f4123bSNathan Whitehorn }
40459276937SPeter Grehan 
405e4f72b32SMarcel Moolenaar static void
406e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
407e4f72b32SMarcel Moolenaar {
408e4f72b32SMarcel Moolenaar 
409e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
41094363f53SNathan Whitehorn 	__asm __volatile("ptesync");
411e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
41294363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
413e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
414e4f72b32SMarcel Moolenaar }
415e4f72b32SMarcel Moolenaar 
416e4f72b32SMarcel Moolenaar static void
417e4f72b32SMarcel Moolenaar tlbia(void)
418e4f72b32SMarcel Moolenaar {
419e4f72b32SMarcel Moolenaar 	vm_offset_t va;
420e4f72b32SMarcel Moolenaar 
421e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
422e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
423e4f72b32SMarcel Moolenaar 		powerpc_sync();
424e4f72b32SMarcel Moolenaar 	}
425e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
426e4f72b32SMarcel Moolenaar 	powerpc_sync();
427e4f72b32SMarcel Moolenaar }
4285244eac9SBenno Rice 
4295244eac9SBenno Rice static __inline int
4305244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4315244eac9SBenno Rice {
4325244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4335244eac9SBenno Rice }
4345244eac9SBenno Rice 
4355244eac9SBenno Rice static __inline u_int
4365244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4375244eac9SBenno Rice {
4385244eac9SBenno Rice 	u_int hash;
4395244eac9SBenno Rice 
4405244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4415244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
44259276937SPeter Grehan 	return (hash & moea_pteg_mask);
4435244eac9SBenno Rice }
4445244eac9SBenno Rice 
4455244eac9SBenno Rice static __inline struct pvo_head *
4465244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
447f9bac91bSBenno Rice {
448f9bac91bSBenno Rice 
4495244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
450f9bac91bSBenno Rice }
451f9bac91bSBenno Rice 
452f9bac91bSBenno Rice static __inline void
45359276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
454f9bac91bSBenno Rice {
455f9bac91bSBenno Rice 
456d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4575244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4585244eac9SBenno Rice }
4595244eac9SBenno Rice 
4605244eac9SBenno Rice static __inline int
46159276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4625244eac9SBenno Rice {
4635244eac9SBenno Rice 
4645244eac9SBenno Rice 	return (m->md.mdpg_attrs);
465f9bac91bSBenno Rice }
466f9bac91bSBenno Rice 
467f9bac91bSBenno Rice static __inline void
46859276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
469f9bac91bSBenno Rice {
470f9bac91bSBenno Rice 
471d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4725244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
473f9bac91bSBenno Rice }
474f9bac91bSBenno Rice 
475f9bac91bSBenno Rice static __inline int
47659276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
477f9bac91bSBenno Rice {
4785244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4795244eac9SBenno Rice 		return (1);
480f9bac91bSBenno Rice 
4815244eac9SBenno Rice 	return (0);
482f9bac91bSBenno Rice }
483f9bac91bSBenno Rice 
484f9bac91bSBenno Rice static __inline int
48559276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
486f9bac91bSBenno Rice {
4875244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
4885244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4895244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
490f9bac91bSBenno Rice }
491f9bac91bSBenno Rice 
4925244eac9SBenno Rice static __inline void
49359276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
494f9bac91bSBenno Rice {
495d644a0b7SAlan Cox 
496d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
497d644a0b7SAlan Cox 
498f9bac91bSBenno Rice 	/*
4995244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5005244eac9SBenno Rice 	 * set when the real pte is set in memory.
501f9bac91bSBenno Rice 	 *
502f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
503f9bac91bSBenno Rice 	 */
5045244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5055244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5065244eac9SBenno Rice 	pt->pte_lo = pte_lo;
507f9bac91bSBenno Rice }
508f9bac91bSBenno Rice 
5095244eac9SBenno Rice static __inline void
51059276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
511f9bac91bSBenno Rice {
512f9bac91bSBenno Rice 
513d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5145244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
515f9bac91bSBenno Rice }
516f9bac91bSBenno Rice 
5175244eac9SBenno Rice static __inline void
51859276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
519f9bac91bSBenno Rice {
5205244eac9SBenno Rice 
521d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
522d644a0b7SAlan Cox 
5235244eac9SBenno Rice 	/*
5245244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5255244eac9SBenno Rice 	 */
5265244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
527e4f72b32SMarcel Moolenaar 	tlbie(va);
5285244eac9SBenno Rice }
5295244eac9SBenno Rice 
5305244eac9SBenno Rice static __inline void
53159276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5325244eac9SBenno Rice {
5335244eac9SBenno Rice 
534d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5355244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5365244eac9SBenno Rice 
5375244eac9SBenno Rice 	/*
5385244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
5395244eac9SBenno Rice 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
5405244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5415244eac9SBenno Rice 	 */
5425244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
543e4f72b32SMarcel Moolenaar 	powerpc_sync();
5445244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
545e4f72b32SMarcel Moolenaar 	powerpc_sync();
54659276937SPeter Grehan 	moea_pte_valid++;
5475244eac9SBenno Rice }
5485244eac9SBenno Rice 
5495244eac9SBenno Rice static __inline void
55059276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5515244eac9SBenno Rice {
5525244eac9SBenno Rice 
553d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5545244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5555244eac9SBenno Rice 
5565244eac9SBenno Rice 	/*
5575244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5585244eac9SBenno Rice 	 */
559e4f72b32SMarcel Moolenaar 	powerpc_sync();
5605244eac9SBenno Rice 
5615244eac9SBenno Rice 	/*
5625244eac9SBenno Rice 	 * Invalidate the pte.
5635244eac9SBenno Rice 	 */
5645244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5655244eac9SBenno Rice 
566e4f72b32SMarcel Moolenaar 	tlbie(va);
5675244eac9SBenno Rice 
5685244eac9SBenno Rice 	/*
5695244eac9SBenno Rice 	 * Save the reg & chg bits.
5705244eac9SBenno Rice 	 */
57159276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
57259276937SPeter Grehan 	moea_pte_valid--;
5735244eac9SBenno Rice }
5745244eac9SBenno Rice 
5755244eac9SBenno Rice static __inline void
57659276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5775244eac9SBenno Rice {
5785244eac9SBenno Rice 
5795244eac9SBenno Rice 	/*
5805244eac9SBenno Rice 	 * Invalidate the PTE
5815244eac9SBenno Rice 	 */
58259276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
58359276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
584f9bac91bSBenno Rice }
585f9bac91bSBenno Rice 
586f9bac91bSBenno Rice /*
5875244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
588f9bac91bSBenno Rice  */
5895244eac9SBenno Rice static int	mr_cmp(const void *a, const void *b);
5905244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
5915244eac9SBenno Rice 
5925244eac9SBenno Rice static int
5935244eac9SBenno Rice mr_cmp(const void *a, const void *b)
594f9bac91bSBenno Rice {
5955244eac9SBenno Rice 	const struct	mem_region *regiona;
5965244eac9SBenno Rice 	const struct	mem_region *regionb;
597f9bac91bSBenno Rice 
5985244eac9SBenno Rice 	regiona = a;
5995244eac9SBenno Rice 	regionb = b;
6005244eac9SBenno Rice 	if (regiona->mr_start < regionb->mr_start)
6015244eac9SBenno Rice 		return (-1);
6025244eac9SBenno Rice 	else if (regiona->mr_start > regionb->mr_start)
6035244eac9SBenno Rice 		return (1);
6045244eac9SBenno Rice 	else
605f9bac91bSBenno Rice 		return (0);
606f9bac91bSBenno Rice }
6075244eac9SBenno Rice 
6085244eac9SBenno Rice static int
6095244eac9SBenno Rice om_cmp(const void *a, const void *b)
6105244eac9SBenno Rice {
6115244eac9SBenno Rice 	const struct	ofw_map *mapa;
6125244eac9SBenno Rice 	const struct	ofw_map *mapb;
6135244eac9SBenno Rice 
6145244eac9SBenno Rice 	mapa = a;
6155244eac9SBenno Rice 	mapb = b;
6165244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6175244eac9SBenno Rice 		return (-1);
6185244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6195244eac9SBenno Rice 		return (1);
6205244eac9SBenno Rice 	else
6215244eac9SBenno Rice 		return (0);
622f9bac91bSBenno Rice }
623f9bac91bSBenno Rice 
624f9bac91bSBenno Rice void
6251c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap)
62612640815SMarcel Moolenaar {
62712640815SMarcel Moolenaar 	u_int sdr;
62812640815SMarcel Moolenaar 	int i;
62912640815SMarcel Moolenaar 
63012640815SMarcel Moolenaar 	if (ap) {
631e4f72b32SMarcel Moolenaar 		powerpc_sync();
63212640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
63312640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
63412640815SMarcel Moolenaar 		isync();
63512640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
63612640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
63712640815SMarcel Moolenaar 		isync();
63812640815SMarcel Moolenaar 	}
63912640815SMarcel Moolenaar 
64001d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
64101d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
64212640815SMarcel Moolenaar 	isync();
64312640815SMarcel Moolenaar 
64401d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
64501d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
64601d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
64701d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
64801d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
64912640815SMarcel Moolenaar 	isync();
65012640815SMarcel Moolenaar 
65112640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
652fe3b4685SNathan Whitehorn 		mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
653e4f72b32SMarcel Moolenaar 	powerpc_sync();
65412640815SMarcel Moolenaar 
65512640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
65612640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
65712640815SMarcel Moolenaar 	isync();
65812640815SMarcel Moolenaar 
65986c1fb4cSMarcel Moolenaar 	tlbia();
66012640815SMarcel Moolenaar }
66112640815SMarcel Moolenaar 
66212640815SMarcel Moolenaar void
66359276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
664f9bac91bSBenno Rice {
66531c82d03SBenno Rice 	ihandle_t	mmui;
6665244eac9SBenno Rice 	phandle_t	chosen, mmu;
6675244eac9SBenno Rice 	int		sz;
6685244eac9SBenno Rice 	int		i, j;
669e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6705244eac9SBenno Rice 	vm_offset_t	pa, va, off;
67150c202c5SJeff Roberson 	void		*dpcpu;
672976cc697SNathan Whitehorn 	register_t	msr;
673f9bac91bSBenno Rice 
674f9bac91bSBenno Rice         /*
67532bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6760d290675SBenno Rice          */
6770d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6780d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6790d290675SBenno Rice 
6800d290675SBenno Rice         /*
6810d290675SBenno Rice          * Map PCI memory space.
6820d290675SBenno Rice          */
6830d290675SBenno Rice         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6840d290675SBenno Rice         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6850d290675SBenno Rice 
6860d290675SBenno Rice         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6870d290675SBenno Rice         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6880d290675SBenno Rice 
6890d290675SBenno Rice         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6900d290675SBenno Rice         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6910d290675SBenno Rice 
6920d290675SBenno Rice         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6930d290675SBenno Rice         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6940d290675SBenno Rice 
6950d290675SBenno Rice         /*
6960d290675SBenno Rice          * Map obio devices.
6970d290675SBenno Rice          */
6980d290675SBenno Rice         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
6990d290675SBenno Rice         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
7000d290675SBenno Rice 
7010d290675SBenno Rice 	/*
7025244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
703976cc697SNathan Whitehorn 	 * where we are. Turn off instruction relocation temporarily
704976cc697SNathan Whitehorn 	 * to prevent faults while reprogramming the IBAT.
705f9bac91bSBenno Rice 	 */
706976cc697SNathan Whitehorn 	msr = mfmsr();
707976cc697SNathan Whitehorn 	mtmsr(msr & ~PSL_IR);
70859276937SPeter Grehan 	__asm (".balign 32; \n"
70972ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
7105d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
71112640815SMarcel Moolenaar 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
712976cc697SNathan Whitehorn 	mtmsr(msr);
7130d290675SBenno Rice 
7140d290675SBenno Rice 	/* map pci space */
71512640815SMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
71612640815SMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
71712640815SMarcel Moolenaar 	isync();
718f9bac91bSBenno Rice 
7191c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7201c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7211c96bdd1SNathan Whitehorn 
72231c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
72359276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
72431c82d03SBenno Rice 
72531c82d03SBenno Rice 	qsort(pregions, pregions_sz, sizeof(*pregions), mr_cmp);
72631c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
72732bc7846SPeter Grehan 		vm_offset_t pa;
72832bc7846SPeter Grehan 		vm_offset_t end;
72932bc7846SPeter Grehan 
73031c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
73131c82d03SBenno Rice 			pregions[i].mr_start,
73231c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
73331c82d03SBenno Rice 			pregions[i].mr_size);
73432bc7846SPeter Grehan 		/*
73532bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
73632bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
73732bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
73832bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
73932bc7846SPeter Grehan 		 * a while yet.
74032bc7846SPeter Grehan 		 */
74132bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
74232bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
74332bc7846SPeter Grehan 		do {
74432bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
74532bc7846SPeter Grehan 
74632bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
74732bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
74832bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
74932bc7846SPeter Grehan 		} while (pa < end);
75031c82d03SBenno Rice 	}
75131c82d03SBenno Rice 
75231c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
75359276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
75431c82d03SBenno Rice 	qsort(regions, regions_sz, sizeof(*regions), mr_cmp);
7555244eac9SBenno Rice 	phys_avail_count = 0;
756d2c1f576SBenno Rice 	physsz = 0;
757b0c21309SPeter Grehan 	hwphyssz = 0;
758b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
75931c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7605244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7615244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7625244eac9SBenno Rice 		    regions[i].mr_size);
763e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
764e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
765e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
766e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
767e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
768e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
769e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
770e2f6d6e2SPeter Grehan 				phys_avail_count++;
771e2f6d6e2SPeter Grehan 			}
772e2f6d6e2SPeter Grehan 			break;
773e2f6d6e2SPeter Grehan 		}
7745244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7755244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7765244eac9SBenno Rice 		phys_avail_count++;
777d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
778f9bac91bSBenno Rice 	}
779d2c1f576SBenno Rice 	physmem = btoc(physsz);
780f9bac91bSBenno Rice 
781f9bac91bSBenno Rice 	/*
7825244eac9SBenno Rice 	 * Allocate PTEG table.
783f9bac91bSBenno Rice 	 */
7845244eac9SBenno Rice #ifdef PTEGCOUNT
78559276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
7865244eac9SBenno Rice #else
78759276937SPeter Grehan 	moea_pteg_count = 0x1000;
788f9bac91bSBenno Rice 
78959276937SPeter Grehan 	while (moea_pteg_count < physmem)
79059276937SPeter Grehan 		moea_pteg_count <<= 1;
791f9bac91bSBenno Rice 
79259276937SPeter Grehan 	moea_pteg_count >>= 1;
7935244eac9SBenno Rice #endif /* PTEGCOUNT */
794f9bac91bSBenno Rice 
79559276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
79659276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
7975244eac9SBenno Rice 	    size);
79859276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
79959276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
80059276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
80159276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
802f9bac91bSBenno Rice 
8035244eac9SBenno Rice 	/*
804864bc520SBenno Rice 	 * Allocate pv/overflow lists.
8055244eac9SBenno Rice 	 */
80659276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
80759276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
8085244eac9SBenno Rice 	    PAGE_SIZE);
80959276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
81059276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
81159276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
8125244eac9SBenno Rice 
8135244eac9SBenno Rice 	/*
814f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
815f489bf21SAlan Cox 	 * tables.
816f489bf21SAlan Cox 	 */
817d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
818d644a0b7SAlan Cox 	    MTX_RECURSE);
819e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
820f489bf21SAlan Cox 
821e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
822e4f72b32SMarcel Moolenaar 
823f489bf21SAlan Cox 	/*
8245244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8255244eac9SBenno Rice 	 */
82659276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8270d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
82859276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8295244eac9SBenno Rice 
8305244eac9SBenno Rice 	/*
8315244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8325244eac9SBenno Rice 	 */
83359276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8345244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
83559276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8365244eac9SBenno Rice 
8375244eac9SBenno Rice 	/*
838fe3b4685SNathan Whitehorn 	 * Initialize the kernel pmap (which is statically allocated).
8395244eac9SBenno Rice 	 */
840fe3b4685SNathan Whitehorn 	PMAP_LOCK_INIT(kernel_pmap);
841fe3b4685SNathan Whitehorn 	for (i = 0; i < 16; i++)
842fe3b4685SNathan Whitehorn 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
843fe3b4685SNathan Whitehorn 	kernel_pmap->pm_active = ~0;
844fe3b4685SNathan Whitehorn 
845fe3b4685SNathan Whitehorn 	/*
846fe3b4685SNathan Whitehorn 	 * Set up the Open Firmware mappings
847fe3b4685SNathan Whitehorn 	 */
8485244eac9SBenno Rice 	if ((chosen = OF_finddevice("/chosen")) == -1)
84959276937SPeter Grehan 		panic("moea_bootstrap: can't find /chosen");
8505244eac9SBenno Rice 	OF_getprop(chosen, "mmu", &mmui, 4);
8515244eac9SBenno Rice 	if ((mmu = OF_instance_to_package(mmui)) == -1)
85259276937SPeter Grehan 		panic("moea_bootstrap: can't get mmu package");
8535244eac9SBenno Rice 	if ((sz = OF_getproplen(mmu, "translations")) == -1)
85459276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translation count");
855aa39961eSBenno Rice 	translations = NULL;
8566cc1cdf4SPeter Grehan 	for (i = 0; phys_avail[i] != 0; i += 2) {
8576cc1cdf4SPeter Grehan 		if (phys_avail[i + 1] >= sz) {
858aa39961eSBenno Rice 			translations = (struct ofw_map *)phys_avail[i];
8596cc1cdf4SPeter Grehan 			break;
8606cc1cdf4SPeter Grehan 		}
861aa39961eSBenno Rice 	}
862aa39961eSBenno Rice 	if (translations == NULL)
86359276937SPeter Grehan 		panic("moea_bootstrap: no space to copy translations");
8645244eac9SBenno Rice 	bzero(translations, sz);
8655244eac9SBenno Rice 	if (OF_getprop(mmu, "translations", translations, sz) == -1)
86659276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translations");
86759276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: translations");
86831c82d03SBenno Rice 	sz /= sizeof(*translations);
8695244eac9SBenno Rice 	qsort(translations, sz, sizeof (*translations), om_cmp);
870ed1e1e2aSNathan Whitehorn 	for (i = 0; i < sz; i++) {
8715244eac9SBenno Rice 		CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
8725244eac9SBenno Rice 		    translations[i].om_pa, translations[i].om_va,
8735244eac9SBenno Rice 		    translations[i].om_len);
8745244eac9SBenno Rice 
87532bc7846SPeter Grehan 		/*
87632bc7846SPeter Grehan 		 * If the mapping is 1:1, let the RAM and device on-demand
87732bc7846SPeter Grehan 		 * BAT tables take care of the translation.
87832bc7846SPeter Grehan 		 */
87932bc7846SPeter Grehan 		if (translations[i].om_va == translations[i].om_pa)
88032bc7846SPeter Grehan 			continue;
8815244eac9SBenno Rice 
88232bc7846SPeter Grehan 		/* Enter the pages */
883ed1e1e2aSNathan Whitehorn 		for (off = 0; off < translations[i].om_len; off += PAGE_SIZE)
884fe3b4685SNathan Whitehorn 			moea_kenter(mmup, translations[i].om_va + off,
885fe3b4685SNathan Whitehorn 				    translations[i].om_pa + off);
886f9bac91bSBenno Rice 	}
887014ffa99SMarcel Moolenaar 
888014ffa99SMarcel Moolenaar 	/*
889014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
890014ffa99SMarcel Moolenaar 	 */
891014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
892014ffa99SMarcel Moolenaar 		;
893014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
8945244eac9SBenno Rice 
8951c96bdd1SNathan Whitehorn 	moea_cpu_bootstrap(mmup,0);
8965244eac9SBenno Rice 
8975244eac9SBenno Rice 	pmap_bootstrapped++;
898014ffa99SMarcel Moolenaar 
899014ffa99SMarcel Moolenaar 	/*
900014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
901014ffa99SMarcel Moolenaar 	 */
902014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
903ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
904014ffa99SMarcel Moolenaar 
905014ffa99SMarcel Moolenaar 	/*
906014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
907014ffa99SMarcel Moolenaar 	 * into the kernel page map.
908014ffa99SMarcel Moolenaar 	 */
909014ffa99SMarcel Moolenaar 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
910014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
911014ffa99SMarcel Moolenaar 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
912014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
913014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
914014ffa99SMarcel Moolenaar 	thread0.td_kstack_pages = KSTACK_PAGES;
915014ffa99SMarcel Moolenaar 	for (i = 0; i < KSTACK_PAGES; i++) {
916c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
917014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
918014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
919014ffa99SMarcel Moolenaar 	}
920014ffa99SMarcel Moolenaar 
921014ffa99SMarcel Moolenaar 	/*
922014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
923014ffa99SMarcel Moolenaar 	 */
924*4053b05bSSergey Kandaurov 	pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE);
925014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
926014ffa99SMarcel Moolenaar 	va = virtual_avail;
927*4053b05bSSergey Kandaurov 	virtual_avail += round_page(msgbufsize);
928014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
929c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
930014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
931014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
932014ffa99SMarcel Moolenaar 	}
93350c202c5SJeff Roberson 
93450c202c5SJeff Roberson 	/*
93550c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
93650c202c5SJeff Roberson 	 */
93750c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
93850c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
93950c202c5SJeff Roberson 	va = virtual_avail;
94050c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
94150c202c5SJeff Roberson 	while (va < virtual_avail) {
942c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
94350c202c5SJeff Roberson 		pa += PAGE_SIZE;
94450c202c5SJeff Roberson 		va += PAGE_SIZE;
94550c202c5SJeff Roberson 	}
94650c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
9475244eac9SBenno Rice }
9485244eac9SBenno Rice 
9495244eac9SBenno Rice /*
9505244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9515244eac9SBenno Rice  * space can be accessed in any way.
952f9bac91bSBenno Rice  */
953f9bac91bSBenno Rice void
95459276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
955f9bac91bSBenno Rice {
9568207b362SBenno Rice 	pmap_t	pm, pmr;
957f9bac91bSBenno Rice 
958f9bac91bSBenno Rice 	/*
95932bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9605244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
961f9bac91bSBenno Rice 	 */
9625244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
96352a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
9648207b362SBenno Rice 
9655244eac9SBenno Rice 	pm->pm_active |= PCPU_GET(cpumask);
9668207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
967ac6ba8bdSBenno Rice }
968ac6ba8bdSBenno Rice 
969ac6ba8bdSBenno Rice void
97059276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
971ac6ba8bdSBenno Rice {
972ac6ba8bdSBenno Rice 	pmap_t	pm;
973ac6ba8bdSBenno Rice 
974ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
975e4f72b32SMarcel Moolenaar 	pm->pm_active &= ~PCPU_GET(cpumask);
9768207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
977f9bac91bSBenno Rice }
978f9bac91bSBenno Rice 
979f9bac91bSBenno Rice void
98059276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
981f9bac91bSBenno Rice {
9820f92104cSBenno Rice 	struct	pvo_entry *pvo;
9830f92104cSBenno Rice 
98448d0b1a0SAlan Cox 	PMAP_LOCK(pm);
98559276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
9860f92104cSBenno Rice 
9870f92104cSBenno Rice 	if (pvo != NULL) {
9880f92104cSBenno Rice 		if (wired) {
9890f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
9900f92104cSBenno Rice 				pm->pm_stats.wired_count++;
9910f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
9920f92104cSBenno Rice 		} else {
9930f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
9940f92104cSBenno Rice 				pm->pm_stats.wired_count--;
9950f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
9960f92104cSBenno Rice 		}
9970f92104cSBenno Rice 	}
99848d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
999f9bac91bSBenno Rice }
1000f9bac91bSBenno Rice 
1001f9bac91bSBenno Rice void
100259276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
1003f9bac91bSBenno Rice {
100425e2288dSBenno Rice 	vm_offset_t	dst;
100525e2288dSBenno Rice 	vm_offset_t	src;
100625e2288dSBenno Rice 
100725e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
100825e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
100925e2288dSBenno Rice 
101025e2288dSBenno Rice 	kcopy((void *)src, (void *)dst, PAGE_SIZE);
1011f9bac91bSBenno Rice }
1012111c77dcSBenno Rice 
1013111c77dcSBenno Rice /*
10145244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
10155244eac9SBenno Rice  */
10165244eac9SBenno Rice void
101759276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
10185244eac9SBenno Rice {
10191a87a0daSPeter Wemm 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10205b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
10215244eac9SBenno Rice 
10225244eac9SBenno Rice 	bzero(va, PAGE_SIZE);
10235244eac9SBenno Rice }
10245244eac9SBenno Rice 
10255244eac9SBenno Rice void
102659276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
10275244eac9SBenno Rice {
10283495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10295b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
10303495845eSBenno Rice 
10315b43c63dSMarcel Moolenaar 	bzero(va, size);
10325244eac9SBenno Rice }
10335244eac9SBenno Rice 
1034a58b3a68SPeter Wemm void
103559276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1036a58b3a68SPeter Wemm {
10375b43c63dSMarcel Moolenaar 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10385b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
1039a58b3a68SPeter Wemm 
10405b43c63dSMarcel Moolenaar 	bzero(va, PAGE_SIZE);
1041a58b3a68SPeter Wemm }
1042a58b3a68SPeter Wemm 
10435244eac9SBenno Rice /*
10445244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
10455244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
10465244eac9SBenno Rice  * will be wired down.
10475244eac9SBenno Rice  */
10485244eac9SBenno Rice void
104959276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
10505244eac9SBenno Rice 	   boolean_t wired)
10515244eac9SBenno Rice {
1052ce142d9eSAlan Cox 
1053ce142d9eSAlan Cox 	vm_page_lock_queues();
1054ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
105567c867eeSAlan Cox 	moea_enter_locked(pmap, va, m, prot, wired);
1056ce142d9eSAlan Cox 	vm_page_unlock_queues();
1057ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1058ce142d9eSAlan Cox }
1059ce142d9eSAlan Cox 
1060ce142d9eSAlan Cox /*
1061ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1062ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1063ce142d9eSAlan Cox  * will be wired down.
1064ce142d9eSAlan Cox  *
1065ce142d9eSAlan Cox  * The page queues and pmap must be locked.
1066ce142d9eSAlan Cox  */
1067ce142d9eSAlan Cox static void
1068ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1069ce142d9eSAlan Cox     boolean_t wired)
1070ce142d9eSAlan Cox {
10715244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1072378862a7SJeff Roberson 	uma_zone_t	zone;
10738207b362SBenno Rice 	vm_page_t	pg;
1074c1f4123bSNathan Whitehorn 	u_int		pte_lo, pvo_flags, was_exec;
10755244eac9SBenno Rice 	int		error;
10765244eac9SBenno Rice 
107759276937SPeter Grehan 	if (!moea_initialized) {
107859276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
107959276937SPeter Grehan 		zone = moea_upvo_zone;
10805244eac9SBenno Rice 		pvo_flags = 0;
10818207b362SBenno Rice 		pg = NULL;
10828207b362SBenno Rice 		was_exec = PTE_EXEC;
10835244eac9SBenno Rice 	} else {
108403b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
108503b6e025SPeter Grehan 		pg = m;
108659276937SPeter Grehan 		zone = moea_mpvo_zone;
10875244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
10888207b362SBenno Rice 		was_exec = 0;
10895244eac9SBenno Rice 	}
1090f489bf21SAlan Cox 	if (pmap_bootstrapped)
1091ce142d9eSAlan Cox 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1092ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
10939124d0d6SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
10949124d0d6SAlan Cox 	    (m->oflags & VPO_BUSY) != 0 || VM_OBJECT_LOCKED(m->object),
10959ab6032fSAlan Cox 	    ("moea_enter_locked: page %p is not busy", m));
10965244eac9SBenno Rice 
10974dba5df1SPeter Grehan 	/* XXX change the pvo head for fake pages */
1098a130b35fSNathan Whitehorn 	if ((m->flags & PG_FICTITIOUS) == PG_FICTITIOUS) {
1099a130b35fSNathan Whitehorn 		pvo_flags &= ~PVO_MANAGED;
110059276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
1101a130b35fSNathan Whitehorn 		zone = moea_upvo_zone;
1102a130b35fSNathan Whitehorn 	}
11034dba5df1SPeter Grehan 
11048207b362SBenno Rice 	/*
11058207b362SBenno Rice 	 * If this is a managed page, and it's the first reference to the page,
11068207b362SBenno Rice 	 * clear the execness of the page.  Otherwise fetch the execness.
11078207b362SBenno Rice 	 */
11084dba5df1SPeter Grehan 	if ((pg != NULL) && ((m->flags & PG_FICTITIOUS) == 0)) {
11098207b362SBenno Rice 		if (LIST_EMPTY(pvo_head)) {
111059276937SPeter Grehan 			moea_attr_clear(pg, PTE_EXEC);
11118207b362SBenno Rice 		} else {
111259276937SPeter Grehan 			was_exec = moea_attr_fetch(pg) & PTE_EXEC;
11138207b362SBenno Rice 		}
11148207b362SBenno Rice 	}
11158207b362SBenno Rice 
1116cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11175244eac9SBenno Rice 
111844b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11195244eac9SBenno Rice 		pte_lo |= PTE_BW;
11202368a371SAlan Cox 		if (pmap_bootstrapped &&
11212368a371SAlan Cox 		    (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0)
112244b8bd66SAlan Cox 			vm_page_flag_set(m, PG_WRITEABLE);
112344b8bd66SAlan Cox 	} else
11245244eac9SBenno Rice 		pte_lo |= PTE_BR;
11255244eac9SBenno Rice 
11264dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
11274dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
11285244eac9SBenno Rice 
11295244eac9SBenno Rice 	if (wired)
11305244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11315244eac9SBenno Rice 
11324dba5df1SPeter Grehan 	if ((m->flags & PG_FICTITIOUS) != 0)
11334dba5df1SPeter Grehan 		pvo_flags |= PVO_FAKE;
11344dba5df1SPeter Grehan 
113559276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11368207b362SBenno Rice 	    pte_lo, pvo_flags);
11375244eac9SBenno Rice 
11388207b362SBenno Rice 	/*
11398207b362SBenno Rice 	 * Flush the real page from the instruction cache if this page is
11408207b362SBenno Rice 	 * mapped executable and cacheable and was not previously mapped (or
11418207b362SBenno Rice 	 * was not mapped executable).
11428207b362SBenno Rice 	 */
11438207b362SBenno Rice 	if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
11448207b362SBenno Rice 	    (pte_lo & PTE_I) == 0 && was_exec == 0) {
11455244eac9SBenno Rice 		/*
11465244eac9SBenno Rice 		 * Flush the real memory from the cache.
11475244eac9SBenno Rice 		 */
114859276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
11498207b362SBenno Rice 		if (pg != NULL)
115059276937SPeter Grehan 			moea_attr_save(pg, PTE_EXEC);
11515244eac9SBenno Rice 	}
115232bc7846SPeter Grehan 
115332bc7846SPeter Grehan 	/* XXX syncicache always until problems are sorted */
115459276937SPeter Grehan 	moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1155ce142d9eSAlan Cox }
1156ce142d9eSAlan Cox 
1157ce142d9eSAlan Cox /*
1158ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1159ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1160ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1161ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1162ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1163ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1164ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1165ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1166ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1167ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1168ce142d9eSAlan Cox  */
1169ce142d9eSAlan Cox void
1170ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1171ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1172ce142d9eSAlan Cox {
1173ce142d9eSAlan Cox 	vm_page_t m;
1174ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1175ce142d9eSAlan Cox 
1176ce142d9eSAlan Cox 	psize = atop(end - start);
1177ce142d9eSAlan Cox 	m = m_start;
1178c46b90e9SAlan Cox 	vm_page_lock_queues();
1179ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1180ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1181ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1182ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1183ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1184ce142d9eSAlan Cox 	}
1185c46b90e9SAlan Cox 	vm_page_unlock_queues();
1186ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
11875244eac9SBenno Rice }
11885244eac9SBenno Rice 
11892053c127SStephan Uphoff void
119059276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
11912053c127SStephan Uphoff     vm_prot_t prot)
1192dca96f1aSAlan Cox {
1193dca96f1aSAlan Cox 
11943c4a2440SAlan Cox 	vm_page_lock_queues();
1195ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1196ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
119759276937SPeter Grehan 	    FALSE);
11983c4a2440SAlan Cox 	vm_page_unlock_queues();
1199ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1200dca96f1aSAlan Cox }
1201dca96f1aSAlan Cox 
120256b09388SAlan Cox vm_paddr_t
120359276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
12045244eac9SBenno Rice {
12050f92104cSBenno Rice 	struct	pvo_entry *pvo;
120648d0b1a0SAlan Cox 	vm_paddr_t pa;
12070f92104cSBenno Rice 
120848d0b1a0SAlan Cox 	PMAP_LOCK(pm);
120959276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
121048d0b1a0SAlan Cox 	if (pvo == NULL)
121148d0b1a0SAlan Cox 		pa = 0;
121248d0b1a0SAlan Cox 	else
121352a7870dSNathan Whitehorn 		pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
121448d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
121548d0b1a0SAlan Cox 	return (pa);
12165244eac9SBenno Rice }
12175244eac9SBenno Rice 
12185244eac9SBenno Rice /*
121984792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
122084792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
122184792e72SPeter Grehan  * protection.
122284792e72SPeter Grehan  */
122384792e72SPeter Grehan vm_page_t
122459276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
122584792e72SPeter Grehan {
1226ab50a262SAlan Cox 	struct	pvo_entry *pvo;
122784792e72SPeter Grehan 	vm_page_t m;
12282965a453SKip Macy         vm_paddr_t pa;
122984792e72SPeter Grehan 
123084792e72SPeter Grehan 	m = NULL;
12312965a453SKip Macy 	pa = 0;
123248d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
12332965a453SKip Macy retry:
123459276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
123552a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
123652a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1237ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
12382965a453SKip Macy 		if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa))
12392965a453SKip Macy 			goto retry;
124052a7870dSNathan Whitehorn 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
124184792e72SPeter Grehan 		vm_page_hold(m);
124284792e72SPeter Grehan 	}
12432965a453SKip Macy 	PA_UNLOCK_COND(pa);
124448d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
124584792e72SPeter Grehan 	return (m);
124684792e72SPeter Grehan }
124784792e72SPeter Grehan 
12485244eac9SBenno Rice void
124959276937SPeter Grehan moea_init(mmu_t mmu)
12505244eac9SBenno Rice {
12515244eac9SBenno Rice 
125259276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12530ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12540ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
125559276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12560ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12570ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
125859276937SPeter Grehan 	moea_initialized = TRUE;
12595244eac9SBenno Rice }
12605244eac9SBenno Rice 
12615244eac9SBenno Rice boolean_t
12627b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m)
12637b85f591SAlan Cox {
12647b85f591SAlan Cox 
1265c46b90e9SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1266c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
12677b85f591SAlan Cox 	return (moea_query_bit(m, PTE_REF));
12687b85f591SAlan Cox }
12697b85f591SAlan Cox 
12707b85f591SAlan Cox boolean_t
127159276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
12725244eac9SBenno Rice {
12730f92104cSBenno Rice 
1274567e51e1SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1275567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1276567e51e1SAlan Cox 
1277567e51e1SAlan Cox 	/*
1278567e51e1SAlan Cox 	 * If the page is not VPO_BUSY, then PG_WRITEABLE cannot be
1279567e51e1SAlan Cox 	 * concurrently set while the object is locked.  Thus, if PG_WRITEABLE
1280567e51e1SAlan Cox 	 * is clear, no PTEs can have PTE_CHG set.
1281567e51e1SAlan Cox 	 */
1282567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1283567e51e1SAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
1284567e51e1SAlan Cox 	    (m->flags & PG_WRITEABLE) == 0)
12850f92104cSBenno Rice 		return (FALSE);
1286c46b90e9SAlan Cox 	return (moea_query_bit(m, PTE_CHG));
1287566526a9SAlan Cox }
1288566526a9SAlan Cox 
1289e396eb60SAlan Cox boolean_t
1290e396eb60SAlan Cox moea_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1291e396eb60SAlan Cox {
1292e396eb60SAlan Cox 	struct pvo_entry *pvo;
1293e396eb60SAlan Cox 	boolean_t rv;
1294e396eb60SAlan Cox 
1295e396eb60SAlan Cox 	PMAP_LOCK(pmap);
1296e396eb60SAlan Cox 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1297e396eb60SAlan Cox 	rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0;
1298e396eb60SAlan Cox 	PMAP_UNLOCK(pmap);
1299e396eb60SAlan Cox 	return (rv);
1300e396eb60SAlan Cox }
1301e396eb60SAlan Cox 
13025244eac9SBenno Rice void
130359276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m)
13045244eac9SBenno Rice {
130503b6e025SPeter Grehan 
1306567e51e1SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1307567e51e1SAlan Cox 	    ("moea_clear_reference: page %p is not managed", m));
1308ce186587SAlan Cox 	moea_clear_bit(m, PTE_REF);
130903b6e025SPeter Grehan }
131003b6e025SPeter Grehan 
131103b6e025SPeter Grehan void
131259276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
131303b6e025SPeter Grehan {
131403b6e025SPeter Grehan 
1315567e51e1SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1316567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
1317567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1318567e51e1SAlan Cox 	KASSERT((m->oflags & VPO_BUSY) == 0,
1319567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is busy", m));
1320567e51e1SAlan Cox 
1321567e51e1SAlan Cox 	/*
1322567e51e1SAlan Cox 	 * If the page is not PG_WRITEABLE, then no PTEs can have PTE_CHG
1323567e51e1SAlan Cox 	 * set.  If the object containing the page is locked and the page is
1324567e51e1SAlan Cox 	 * not VPO_BUSY, then PG_WRITEABLE cannot be concurrently set.
1325567e51e1SAlan Cox 	 */
1326567e51e1SAlan Cox 	if ((m->flags & PG_WRITEABLE) == 0)
132703b6e025SPeter Grehan 		return;
1328ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
13295244eac9SBenno Rice }
13305244eac9SBenno Rice 
13317f3a4093SMike Silbersack /*
133278985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
133378985e42SAlan Cox  */
133478985e42SAlan Cox void
133578985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
133678985e42SAlan Cox {
133778985e42SAlan Cox 	struct	pvo_entry *pvo;
133878985e42SAlan Cox 	struct	pte *pt;
133978985e42SAlan Cox 	pmap_t	pmap;
134078985e42SAlan Cox 	u_int	lo;
134178985e42SAlan Cox 
13429ab6032fSAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
13439ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
13449ab6032fSAlan Cox 
13459ab6032fSAlan Cox 	/*
13469ab6032fSAlan Cox 	 * If the page is not VPO_BUSY, then PG_WRITEABLE cannot be set by
13479ab6032fSAlan Cox 	 * another thread while the object is locked.  Thus, if PG_WRITEABLE
13489ab6032fSAlan Cox 	 * is clear, no page table entries need updating.
13499ab6032fSAlan Cox 	 */
13509ab6032fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
13519ab6032fSAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
135278985e42SAlan Cox 	    (m->flags & PG_WRITEABLE) == 0)
135378985e42SAlan Cox 		return;
13543c4a2440SAlan Cox 	vm_page_lock_queues();
135578985e42SAlan Cox 	lo = moea_attr_fetch(m);
1356e4f72b32SMarcel Moolenaar 	powerpc_sync();
135778985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
135878985e42SAlan Cox 		pmap = pvo->pvo_pmap;
135978985e42SAlan Cox 		PMAP_LOCK(pmap);
136052a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
136178985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
136252a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
136352a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
136478985e42SAlan Cox 			if (pt != NULL) {
136552a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
136652a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
136752a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
136852a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
136978985e42SAlan Cox 				    pvo->pvo_vaddr);
137078985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
137178985e42SAlan Cox 			}
137278985e42SAlan Cox 		}
137378985e42SAlan Cox 		PMAP_UNLOCK(pmap);
137478985e42SAlan Cox 	}
137578985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
137678985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
137778985e42SAlan Cox 		vm_page_dirty(m);
137878985e42SAlan Cox 	}
137978985e42SAlan Cox 	vm_page_flag_clear(m, PG_WRITEABLE);
13803c4a2440SAlan Cox 	vm_page_unlock_queues();
138178985e42SAlan Cox }
138278985e42SAlan Cox 
138378985e42SAlan Cox /*
138459276937SPeter Grehan  *	moea_ts_referenced:
13857f3a4093SMike Silbersack  *
13867f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
13877f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
13887f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
13897f3a4093SMike Silbersack  *	reference bits set.
13907f3a4093SMike Silbersack  *
13917f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
13927f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
13937f3a4093SMike Silbersack  *	optimal aging of shared pages.
13947f3a4093SMike Silbersack  */
139559276937SPeter Grehan boolean_t
139659276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
13975244eac9SBenno Rice {
139803b6e025SPeter Grehan 
1399ce186587SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1400ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
1401ce186587SAlan Cox 	return (moea_clear_bit(m, PTE_REF));
14025244eac9SBenno Rice }
14035244eac9SBenno Rice 
14045244eac9SBenno Rice /*
1405c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1406c1f4123bSNathan Whitehorn  */
1407c1f4123bSNathan Whitehorn void
1408c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma)
1409c1f4123bSNathan Whitehorn {
1410c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1411cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1412c1f4123bSNathan Whitehorn 	struct	pte *pt;
1413c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1414c1f4123bSNathan Whitehorn 	u_int	lo;
1415c1f4123bSNathan Whitehorn 
1416cd6a97f0SNathan Whitehorn 	if (m->flags & PG_FICTITIOUS) {
1417cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1418cd6a97f0SNathan Whitehorn 		return;
1419cd6a97f0SNathan Whitehorn 	}
1420cd6a97f0SNathan Whitehorn 
1421c1f4123bSNathan Whitehorn 	vm_page_lock_queues();
1422cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1423c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1424cd6a97f0SNathan Whitehorn 
1425cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1426c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1427c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1428c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1429c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1430c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1431c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1432c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1433c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1434c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1435c1f4123bSNathan Whitehorn 				isync();
1436c1f4123bSNathan Whitehorn 		}
1437c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1438c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1439c1f4123bSNathan Whitehorn 	}
1440c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
1441c1f4123bSNathan Whitehorn 	vm_page_unlock_queues();
1442c1f4123bSNathan Whitehorn }
1443c1f4123bSNathan Whitehorn 
1444c1f4123bSNathan Whitehorn /*
14455244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
14465244eac9SBenno Rice  */
14475244eac9SBenno Rice void
144859276937SPeter Grehan moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
14495244eac9SBenno Rice {
1450c1f4123bSNathan Whitehorn 
1451c1f4123bSNathan Whitehorn 	moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1452c1f4123bSNathan Whitehorn }
1453c1f4123bSNathan Whitehorn 
1454c1f4123bSNathan Whitehorn void
1455c1f4123bSNathan Whitehorn moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma)
1456c1f4123bSNathan Whitehorn {
14575244eac9SBenno Rice 	u_int		pte_lo;
14585244eac9SBenno Rice 	int		error;
14595244eac9SBenno Rice 
14605244eac9SBenno Rice #if 0
14615244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
146259276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
14635244eac9SBenno Rice 		    va);
14645244eac9SBenno Rice #endif
14655244eac9SBenno Rice 
1466c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
14675244eac9SBenno Rice 
14684711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
146959276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
147059276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
14715244eac9SBenno Rice 
14725244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
147359276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
14745244eac9SBenno Rice 		    pa, error);
14755244eac9SBenno Rice 
14765244eac9SBenno Rice 	/*
14775244eac9SBenno Rice 	 * Flush the real memory from the instruction cache.
14785244eac9SBenno Rice 	 */
14795244eac9SBenno Rice 	if ((pte_lo & (PTE_I | PTE_G)) == 0) {
148059276937SPeter Grehan 		moea_syncicache(pa, PAGE_SIZE);
14815244eac9SBenno Rice 	}
14824711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
14835244eac9SBenno Rice }
14845244eac9SBenno Rice 
1485e79f59e8SBenno Rice /*
1486e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1487e79f59e8SBenno Rice  * address.
1488e79f59e8SBenno Rice  */
14895244eac9SBenno Rice vm_offset_t
149059276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
14915244eac9SBenno Rice {
1492e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
149348d0b1a0SAlan Cox 	vm_paddr_t pa;
1494e79f59e8SBenno Rice 
14950efd0097SPeter Grehan 	/*
149652a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
14970efd0097SPeter Grehan 	 */
14980efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
14990efd0097SPeter Grehan 		return (va);
15000efd0097SPeter Grehan 	}
15010efd0097SPeter Grehan 
150248d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
150359276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
150459276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
150552a7870dSNathan Whitehorn 	pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
150648d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
150748d0b1a0SAlan Cox 	return (pa);
1508e79f59e8SBenno Rice }
1509e79f59e8SBenno Rice 
151088afb2a3SBenno Rice /*
151188afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
151288afb2a3SBenno Rice  */
15135244eac9SBenno Rice void
151459276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
15155244eac9SBenno Rice {
151688afb2a3SBenno Rice 
151759276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
15185244eac9SBenno Rice }
15195244eac9SBenno Rice 
15205244eac9SBenno Rice /*
15215244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
15225244eac9SBenno Rice  *
15235244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
15245244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
15255244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
15265244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
15275244eac9SBenno Rice  * first usable address after the mapped region.
15285244eac9SBenno Rice  */
15295244eac9SBenno Rice vm_offset_t
153059276937SPeter Grehan moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
153159276937SPeter Grehan     vm_offset_t pa_end, int prot)
15325244eac9SBenno Rice {
15335244eac9SBenno Rice 	vm_offset_t	sva, va;
15345244eac9SBenno Rice 
15355244eac9SBenno Rice 	sva = *virt;
15365244eac9SBenno Rice 	va = sva;
15375244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
153859276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
15395244eac9SBenno Rice 	*virt = va;
15405244eac9SBenno Rice 	return (sva);
15415244eac9SBenno Rice }
15425244eac9SBenno Rice 
15435244eac9SBenno Rice /*
15447f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
15457f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
15467f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
15477f3a4093SMike Silbersack  * is only necessary that true be returned for a small
15487f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
15497f3a4093SMike Silbersack  */
15505244eac9SBenno Rice boolean_t
155159276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
15525244eac9SBenno Rice {
155303b6e025SPeter Grehan         int loops;
155403b6e025SPeter Grehan 	struct pvo_entry *pvo;
1555ce186587SAlan Cox 	boolean_t rv;
155603b6e025SPeter Grehan 
1557ce186587SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1558ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
155903b6e025SPeter Grehan 	loops = 0;
1560ce186587SAlan Cox 	rv = FALSE;
1561ce186587SAlan Cox 	vm_page_lock_queues();
156203b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1563ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1564ce186587SAlan Cox 			rv = TRUE;
1565ce186587SAlan Cox 			break;
1566ce186587SAlan Cox 		}
156703b6e025SPeter Grehan 		if (++loops >= 16)
156803b6e025SPeter Grehan 			break;
156903b6e025SPeter Grehan 	}
1570ce186587SAlan Cox 	vm_page_unlock_queues();
1571ce186587SAlan Cox 	return (rv);
15725244eac9SBenno Rice }
15735244eac9SBenno Rice 
157459677d3cSAlan Cox /*
157559677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
157659677d3cSAlan Cox  * that are wired.
157759677d3cSAlan Cox  */
157859677d3cSAlan Cox int
157959677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
158059677d3cSAlan Cox {
158159677d3cSAlan Cox 	struct pvo_entry *pvo;
158259677d3cSAlan Cox 	int count;
158359677d3cSAlan Cox 
158459677d3cSAlan Cox 	count = 0;
1585ce186587SAlan Cox 	if ((m->flags & PG_FICTITIOUS) != 0)
158659677d3cSAlan Cox 		return (count);
15873c4a2440SAlan Cox 	vm_page_lock_queues();
158859677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
158959677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
159059677d3cSAlan Cox 			count++;
15913c4a2440SAlan Cox 	vm_page_unlock_queues();
159259677d3cSAlan Cox 	return (count);
159359677d3cSAlan Cox }
159459677d3cSAlan Cox 
159559276937SPeter Grehan static u_int	moea_vsidcontext;
15965244eac9SBenno Rice 
15975244eac9SBenno Rice void
159859276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
15995244eac9SBenno Rice {
16005244eac9SBenno Rice 	int	i, mask;
16015244eac9SBenno Rice 	u_int	entropy;
16025244eac9SBenno Rice 
160359276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
160448d0b1a0SAlan Cox 	PMAP_LOCK_INIT(pmap);
16054daf20b2SPeter Grehan 
16065244eac9SBenno Rice 	entropy = 0;
16075244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
16085244eac9SBenno Rice 
160952a7870dSNathan Whitehorn 	if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
161052a7870dSNathan Whitehorn 	    == NULL) {
161152a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
161252a7870dSNathan Whitehorn 	}
161352a7870dSNathan Whitehorn 
161452a7870dSNathan Whitehorn 
1615e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
16165244eac9SBenno Rice 	/*
16175244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
16185244eac9SBenno Rice 	 */
16195244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
16205244eac9SBenno Rice 		u_int	hash, n;
16215244eac9SBenno Rice 
16225244eac9SBenno Rice 		/*
16235244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
16245244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
16255244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
16265244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
16275244eac9SBenno Rice 		 * instead of a multiply.)
16285244eac9SBenno Rice 		 */
162959276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
163059276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
16315244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
16325244eac9SBenno Rice 			continue;
16335244eac9SBenno Rice 		n = hash >> 5;
16345244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
163559276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
163659276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
16375244eac9SBenno Rice 			/* anything free in this bucket? */
163859276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
163959276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
16405244eac9SBenno Rice 				continue;
16415244eac9SBenno Rice 			}
16420dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
16435244eac9SBenno Rice 			mask = 1 << i;
16445244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
16455244eac9SBenno Rice 			hash |= i;
16465244eac9SBenno Rice 		}
164759276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
16485244eac9SBenno Rice 		for (i = 0; i < 16; i++)
16495244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1650e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
16515244eac9SBenno Rice 		return;
16525244eac9SBenno Rice 	}
16535244eac9SBenno Rice 
1654e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
165559276937SPeter Grehan 	panic("moea_pinit: out of segments");
16565244eac9SBenno Rice }
16575244eac9SBenno Rice 
16585244eac9SBenno Rice /*
16595244eac9SBenno Rice  * Initialize the pmap associated with process 0.
16605244eac9SBenno Rice  */
16615244eac9SBenno Rice void
166259276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
16635244eac9SBenno Rice {
16645244eac9SBenno Rice 
166559276937SPeter Grehan 	moea_pinit(mmu, pm);
16665244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
16675244eac9SBenno Rice }
16685244eac9SBenno Rice 
1669e79f59e8SBenno Rice /*
1670e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1671e79f59e8SBenno Rice  */
16725244eac9SBenno Rice void
167359276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
167459276937SPeter Grehan     vm_prot_t prot)
16755244eac9SBenno Rice {
1676e79f59e8SBenno Rice 	struct	pvo_entry *pvo;
1677e79f59e8SBenno Rice 	struct	pte *pt;
1678e79f59e8SBenno Rice 	int	pteidx;
1679e79f59e8SBenno Rice 
1680e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
168159276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1682e79f59e8SBenno Rice 
1683e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
168459276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1685e79f59e8SBenno Rice 		return;
1686e79f59e8SBenno Rice 	}
1687e79f59e8SBenno Rice 
16883d2e54c3SAlan Cox 	vm_page_lock_queues();
168948d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1690e79f59e8SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
169159276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
1692e79f59e8SBenno Rice 		if (pvo == NULL)
1693e79f59e8SBenno Rice 			continue;
1694e79f59e8SBenno Rice 
1695e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1696e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1697e79f59e8SBenno Rice 
1698e79f59e8SBenno Rice 		/*
1699e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1700e79f59e8SBenno Rice 		 * copy.
1701e79f59e8SBenno Rice 		 */
170259276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, pteidx);
1703e79f59e8SBenno Rice 		/*
1704e79f59e8SBenno Rice 		 * Change the protection of the page.
1705e79f59e8SBenno Rice 		 */
170652a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
170752a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1708e79f59e8SBenno Rice 
1709e79f59e8SBenno Rice 		/*
1710e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1711e79f59e8SBenno Rice 		 */
1712d644a0b7SAlan Cox 		if (pt != NULL) {
171352a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1714d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1715d644a0b7SAlan Cox 		}
1716e79f59e8SBenno Rice 	}
17173d2e54c3SAlan Cox 	vm_page_unlock_queues();
171848d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
17195244eac9SBenno Rice }
17205244eac9SBenno Rice 
172188afb2a3SBenno Rice /*
172288afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
172388afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
172488afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
172588afb2a3SBenno Rice  */
17265244eac9SBenno Rice void
172759276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
17285244eac9SBenno Rice {
172903b6e025SPeter Grehan 	vm_offset_t va;
17305244eac9SBenno Rice 
173103b6e025SPeter Grehan 	va = sva;
173203b6e025SPeter Grehan 	while (count-- > 0) {
173359276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
173403b6e025SPeter Grehan 		va += PAGE_SIZE;
173503b6e025SPeter Grehan 		m++;
173603b6e025SPeter Grehan 	}
17375244eac9SBenno Rice }
17385244eac9SBenno Rice 
173988afb2a3SBenno Rice /*
174088afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
174159276937SPeter Grehan  * temporary mappings entered by moea_qenter.
174288afb2a3SBenno Rice  */
17435244eac9SBenno Rice void
174459276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
17455244eac9SBenno Rice {
174603b6e025SPeter Grehan 	vm_offset_t va;
174788afb2a3SBenno Rice 
174803b6e025SPeter Grehan 	va = sva;
174903b6e025SPeter Grehan 	while (count-- > 0) {
175059276937SPeter Grehan 		moea_kremove(mmu, va);
175103b6e025SPeter Grehan 		va += PAGE_SIZE;
175203b6e025SPeter Grehan 	}
17535244eac9SBenno Rice }
17545244eac9SBenno Rice 
17555244eac9SBenno Rice void
175659276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
17575244eac9SBenno Rice {
175832bc7846SPeter Grehan         int idx, mask;
175932bc7846SPeter Grehan 
176032bc7846SPeter Grehan 	/*
176132bc7846SPeter Grehan 	 * Free segment register's VSID
176232bc7846SPeter Grehan 	 */
176332bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
176459276937SPeter Grehan                 panic("moea_release");
176532bc7846SPeter Grehan 
1766e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
176732bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
176832bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
176932bc7846SPeter Grehan         idx /= VSID_NBPW;
177059276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1771e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
177248d0b1a0SAlan Cox 	PMAP_LOCK_DESTROY(pmap);
17735244eac9SBenno Rice }
17745244eac9SBenno Rice 
177588afb2a3SBenno Rice /*
177688afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
177788afb2a3SBenno Rice  */
17785244eac9SBenno Rice void
177959276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
17805244eac9SBenno Rice {
178188afb2a3SBenno Rice 	struct	pvo_entry *pvo;
178288afb2a3SBenno Rice 	int	pteidx;
178388afb2a3SBenno Rice 
17843d2e54c3SAlan Cox 	vm_page_lock_queues();
178548d0b1a0SAlan Cox 	PMAP_LOCK(pm);
178688afb2a3SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
178759276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
178888afb2a3SBenno Rice 		if (pvo != NULL) {
178959276937SPeter Grehan 			moea_pvo_remove(pvo, pteidx);
179088afb2a3SBenno Rice 		}
179188afb2a3SBenno Rice 	}
179248d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
179394aa7aecSPeter Grehan 	vm_page_unlock_queues();
17945244eac9SBenno Rice }
17955244eac9SBenno Rice 
1796e79f59e8SBenno Rice /*
179759276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
179803b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
179903b6e025SPeter Grehan  */
180003b6e025SPeter Grehan void
180159276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
180203b6e025SPeter Grehan {
180303b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
180403b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
180548d0b1a0SAlan Cox 	pmap_t	pmap;
180603b6e025SPeter Grehan 
18073c4a2440SAlan Cox 	vm_page_lock_queues();
180803b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
180903b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
181003b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
181103b6e025SPeter Grehan 
181259276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
181348d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
181448d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
181559276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
181648d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
181703b6e025SPeter Grehan 	}
1818062c8f4cSNathan Whitehorn 	if ((m->flags & PG_WRITEABLE) && moea_is_modified(mmu, m)) {
1819c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1820062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1821062c8f4cSNathan Whitehorn 	}
182203b6e025SPeter Grehan 	vm_page_flag_clear(m, PG_WRITEABLE);
18233c4a2440SAlan Cox 	vm_page_unlock_queues();
182403b6e025SPeter Grehan }
182503b6e025SPeter Grehan 
182603b6e025SPeter Grehan /*
18275244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
182859276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
18295244eac9SBenno Rice  * calculated.
18305244eac9SBenno Rice  */
18315244eac9SBenno Rice static vm_offset_t
183259276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
18335244eac9SBenno Rice {
18345244eac9SBenno Rice 	vm_offset_t	s, e;
18355244eac9SBenno Rice 	int		i, j;
18365244eac9SBenno Rice 
18375244eac9SBenno Rice 	size = round_page(size);
18385244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
18395244eac9SBenno Rice 		if (align != 0)
18405244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
18415244eac9SBenno Rice 		else
18425244eac9SBenno Rice 			s = phys_avail[i];
18435244eac9SBenno Rice 		e = s + size;
18445244eac9SBenno Rice 
18455244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
18465244eac9SBenno Rice 			continue;
18475244eac9SBenno Rice 
18485244eac9SBenno Rice 		if (s == phys_avail[i]) {
18495244eac9SBenno Rice 			phys_avail[i] += size;
18505244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
18515244eac9SBenno Rice 			phys_avail[i + 1] -= size;
18525244eac9SBenno Rice 		} else {
18535244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
18545244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
18555244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
18565244eac9SBenno Rice 			}
18575244eac9SBenno Rice 
18585244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
18595244eac9SBenno Rice 			phys_avail[i + 1] = s;
18605244eac9SBenno Rice 			phys_avail[i + 2] = e;
18615244eac9SBenno Rice 			phys_avail_count++;
18625244eac9SBenno Rice 		}
18635244eac9SBenno Rice 
18645244eac9SBenno Rice 		return (s);
18655244eac9SBenno Rice 	}
186659276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
18675244eac9SBenno Rice }
18685244eac9SBenno Rice 
18695244eac9SBenno Rice static void
187059276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
18715244eac9SBenno Rice {
18725244eac9SBenno Rice 	__syncicache((void *)pa, len);
18735244eac9SBenno Rice }
18745244eac9SBenno Rice 
18755244eac9SBenno Rice static int
187659276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
18775244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
18785244eac9SBenno Rice {
18795244eac9SBenno Rice 	struct	pvo_entry *pvo;
18805244eac9SBenno Rice 	u_int	sr;
18815244eac9SBenno Rice 	int	first;
18825244eac9SBenno Rice 	u_int	ptegidx;
18835244eac9SBenno Rice 	int	i;
188432bc7846SPeter Grehan 	int     bootstrap;
18855244eac9SBenno Rice 
188659276937SPeter Grehan 	moea_pvo_enter_calls++;
18878207b362SBenno Rice 	first = 0;
188832bc7846SPeter Grehan 	bootstrap = 0;
188932bc7846SPeter Grehan 
18905244eac9SBenno Rice 	/*
18915244eac9SBenno Rice 	 * Compute the PTE Group index.
18925244eac9SBenno Rice 	 */
18935244eac9SBenno Rice 	va &= ~ADDR_POFF;
18945244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
18955244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
18965244eac9SBenno Rice 
18975244eac9SBenno Rice 	/*
18985244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
18995244eac9SBenno Rice 	 * there is a mapping.
19005244eac9SBenno Rice 	 */
190159276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
190259276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19035244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
190452a7870dSNathan Whitehorn 			if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
190552a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1906fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
190759276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
190849f8f727SBenno Rice 				return (0);
1909fafc7362SBenno Rice 			}
191059276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
19115244eac9SBenno Rice 			break;
19125244eac9SBenno Rice 		}
19135244eac9SBenno Rice 	}
19145244eac9SBenno Rice 
19155244eac9SBenno Rice 	/*
19165244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
19175244eac9SBenno Rice 	 */
191859276937SPeter Grehan 	if (moea_initialized) {
1919378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
192049f8f727SBenno Rice 	} else {
192159276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
192259276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
192359276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
19240d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
192549f8f727SBenno Rice 		}
192659276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
192759276937SPeter Grehan 		moea_bpvo_pool_index++;
192832bc7846SPeter Grehan 		bootstrap = 1;
192949f8f727SBenno Rice 	}
19305244eac9SBenno Rice 
19315244eac9SBenno Rice 	if (pvo == NULL) {
193259276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
19335244eac9SBenno Rice 		return (ENOMEM);
19345244eac9SBenno Rice 	}
19355244eac9SBenno Rice 
193659276937SPeter Grehan 	moea_pvo_entries++;
19375244eac9SBenno Rice 	pvo->pvo_vaddr = va;
19385244eac9SBenno Rice 	pvo->pvo_pmap = pm;
193959276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
19405244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
19415244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
19425244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
19435244eac9SBenno Rice 	if (flags & PVO_WIRED)
19445244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
194559276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
19465244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
194732bc7846SPeter Grehan 	if (bootstrap)
194832bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
19494dba5df1SPeter Grehan 	if (flags & PVO_FAKE)
19504dba5df1SPeter Grehan 		pvo->pvo_vaddr |= PVO_FAKE;
19514dba5df1SPeter Grehan 
195252a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
19535244eac9SBenno Rice 
19545244eac9SBenno Rice 	/*
19555244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
19565244eac9SBenno Rice 	 * item.
19575244eac9SBenno Rice 	 */
19588207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
19598207b362SBenno Rice 		first = 1;
19605244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
19614dba5df1SPeter Grehan 
196252a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
1963c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
1964c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
19655244eac9SBenno Rice 
19665244eac9SBenno Rice 	/*
19675244eac9SBenno Rice 	 * We hope this succeeds but it isn't required.
19685244eac9SBenno Rice 	 */
196952a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
19705244eac9SBenno Rice 	if (i >= 0) {
19715244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
19725244eac9SBenno Rice 	} else {
197359276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
197459276937SPeter Grehan 		moea_pte_overflow++;
19755244eac9SBenno Rice 	}
197659276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
19774dba5df1SPeter Grehan 
19785244eac9SBenno Rice 	return (first ? ENOENT : 0);
19795244eac9SBenno Rice }
19805244eac9SBenno Rice 
19815244eac9SBenno Rice static void
198259276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
19835244eac9SBenno Rice {
19845244eac9SBenno Rice 	struct	pte *pt;
19855244eac9SBenno Rice 
19865244eac9SBenno Rice 	/*
19875244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
19885244eac9SBenno Rice 	 * save the ref & cfg bits).
19895244eac9SBenno Rice 	 */
199059276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
19915244eac9SBenno Rice 	if (pt != NULL) {
199252a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1993d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
19945244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
19955244eac9SBenno Rice 	} else {
199659276937SPeter Grehan 		moea_pte_overflow--;
19975244eac9SBenno Rice 	}
19985244eac9SBenno Rice 
19995244eac9SBenno Rice 	/*
20005244eac9SBenno Rice 	 * Update our statistics.
20015244eac9SBenno Rice 	 */
20025244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
200352a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
20045244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
20055244eac9SBenno Rice 
20065244eac9SBenno Rice 	/*
20075244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
20085244eac9SBenno Rice 	 */
20094dba5df1SPeter Grehan 	if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) {
20105244eac9SBenno Rice 		struct	vm_page *pg;
20115244eac9SBenno Rice 
201252a7870dSNathan Whitehorn 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
20135244eac9SBenno Rice 		if (pg != NULL) {
201452a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
20155244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
20165244eac9SBenno Rice 		}
20175244eac9SBenno Rice 	}
20185244eac9SBenno Rice 
20195244eac9SBenno Rice 	/*
20205244eac9SBenno Rice 	 * Remove this PVO from the PV list.
20215244eac9SBenno Rice 	 */
20225244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
20235244eac9SBenno Rice 
20245244eac9SBenno Rice 	/*
20255244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
20265244eac9SBenno Rice 	 * if we aren't going to reuse it.
20275244eac9SBenno Rice 	 */
20285244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
202949f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
203059276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
203159276937SPeter Grehan 		    moea_upvo_zone, pvo);
203259276937SPeter Grehan 	moea_pvo_entries--;
203359276937SPeter Grehan 	moea_pvo_remove_calls++;
20345244eac9SBenno Rice }
20355244eac9SBenno Rice 
20365244eac9SBenno Rice static __inline int
203759276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
20385244eac9SBenno Rice {
20395244eac9SBenno Rice 	int	pteidx;
20405244eac9SBenno Rice 
20415244eac9SBenno Rice 	/*
20425244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
20435244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
20445244eac9SBenno Rice 	 * noticing the HID bit.
20455244eac9SBenno Rice 	 */
20465244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
204752a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
204859276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
20495244eac9SBenno Rice 
20505244eac9SBenno Rice 	return (pteidx);
20515244eac9SBenno Rice }
20525244eac9SBenno Rice 
20535244eac9SBenno Rice static struct pvo_entry *
205459276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
20555244eac9SBenno Rice {
20565244eac9SBenno Rice 	struct	pvo_entry *pvo;
20575244eac9SBenno Rice 	int	ptegidx;
20585244eac9SBenno Rice 	u_int	sr;
20595244eac9SBenno Rice 
20605244eac9SBenno Rice 	va &= ~ADDR_POFF;
20615244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
20625244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
20635244eac9SBenno Rice 
206459276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
206559276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20665244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
20675244eac9SBenno Rice 			if (pteidx_p)
206859276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2069f489bf21SAlan Cox 			break;
20705244eac9SBenno Rice 		}
20715244eac9SBenno Rice 	}
207259276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20735244eac9SBenno Rice 
2074f489bf21SAlan Cox 	return (pvo);
20755244eac9SBenno Rice }
20765244eac9SBenno Rice 
20775244eac9SBenno Rice static struct pte *
207859276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
20795244eac9SBenno Rice {
20805244eac9SBenno Rice 	struct	pte *pt;
20815244eac9SBenno Rice 
20825244eac9SBenno Rice 	/*
20835244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
20845244eac9SBenno Rice 	 */
20855244eac9SBenno Rice 	if (pteidx == -1) {
20865244eac9SBenno Rice 		int	ptegidx;
20875244eac9SBenno Rice 		u_int	sr;
20885244eac9SBenno Rice 
20895244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
20905244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
209159276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
20925244eac9SBenno Rice 	}
20935244eac9SBenno Rice 
209459276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2095d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
20965244eac9SBenno Rice 
209752a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
209859276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
20995244eac9SBenno Rice 		    "valid pte index", pvo);
21005244eac9SBenno Rice 	}
21015244eac9SBenno Rice 
210252a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
210359276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
21045244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
21055244eac9SBenno Rice 	}
21065244eac9SBenno Rice 
210752a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
210852a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
210959276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
211059276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
21115244eac9SBenno Rice 		}
21125244eac9SBenno Rice 
211352a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
21145244eac9SBenno Rice 		    != 0) {
211559276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
211659276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
21175244eac9SBenno Rice 		}
21185244eac9SBenno Rice 
2119d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
21205244eac9SBenno Rice 		return (pt);
21215244eac9SBenno Rice 	}
21225244eac9SBenno Rice 
212352a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
212459276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
212559276937SPeter Grehan 		    "moea_pteg_table but valid in pvo", pvo, pt);
21265244eac9SBenno Rice 	}
21275244eac9SBenno Rice 
2128d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
21295244eac9SBenno Rice 	return (NULL);
21305244eac9SBenno Rice }
21315244eac9SBenno Rice 
21325244eac9SBenno Rice /*
21335244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
21345244eac9SBenno Rice  */
21355244eac9SBenno Rice int
213659276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
21375244eac9SBenno Rice {
21385244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
21395244eac9SBenno Rice 	struct	pvo_entry *pvo;
21405244eac9SBenno Rice 	int	ptegidx, i, j;
21415244eac9SBenno Rice 	u_int	sr;
21425244eac9SBenno Rice 	struct	pteg *pteg;
21435244eac9SBenno Rice 	struct	pte *pt;
21445244eac9SBenno Rice 
214559276937SPeter Grehan 	moea_pte_spills++;
21465244eac9SBenno Rice 
2147d080d5fdSBenno Rice 	sr = mfsrin(addr);
21485244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
21495244eac9SBenno Rice 
21505244eac9SBenno Rice 	/*
21515244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
21525244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
21535244eac9SBenno Rice 	 */
215459276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
215559276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
21565244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
21575244eac9SBenno Rice 	i &= 7;
21585244eac9SBenno Rice 	pt = &pteg->pt[i];
21595244eac9SBenno Rice 
21605244eac9SBenno Rice 	source_pvo = NULL;
21615244eac9SBenno Rice 	victim_pvo = NULL;
216259276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21635244eac9SBenno Rice 		/*
21645244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
21655244eac9SBenno Rice 		 */
216659276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);
21675244eac9SBenno Rice 		if (source_pvo == NULL &&
216852a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
216952a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
21705244eac9SBenno Rice 			/*
21715244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
21725244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
21735244eac9SBenno Rice 			 */
217452a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
21755244eac9SBenno Rice 
21765244eac9SBenno Rice 			if (j >= 0) {
21775244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
217859276937SPeter Grehan 				moea_pte_overflow--;
217959276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);
218059276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
21815244eac9SBenno Rice 				return (1);
21825244eac9SBenno Rice 			}
21835244eac9SBenno Rice 
21845244eac9SBenno Rice 			source_pvo = pvo;
21855244eac9SBenno Rice 
21865244eac9SBenno Rice 			if (victim_pvo != NULL)
21875244eac9SBenno Rice 				break;
21885244eac9SBenno Rice 		}
21895244eac9SBenno Rice 
21905244eac9SBenno Rice 		/*
21915244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
21925244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
21935244eac9SBenno Rice 		 */
21945244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
219552a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
21965244eac9SBenno Rice 			victim_pvo = pvo;
21975244eac9SBenno Rice 			if (source_pvo != NULL)
21985244eac9SBenno Rice 				break;
21995244eac9SBenno Rice 		}
22005244eac9SBenno Rice 	}
22015244eac9SBenno Rice 
2202f489bf21SAlan Cox 	if (source_pvo == NULL) {
220359276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
22045244eac9SBenno Rice 		return (0);
2205f489bf21SAlan Cox 	}
22065244eac9SBenno Rice 
22075244eac9SBenno Rice 	if (victim_pvo == NULL) {
22085244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
220959276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
22105244eac9SBenno Rice 			    "entry", pt);
22115244eac9SBenno Rice 
22125244eac9SBenno Rice 		/*
22135244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
22145244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
22155244eac9SBenno Rice 		 */
221659276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
22175244eac9SBenno Rice 		    pvo_olink) {
221859276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);
22195244eac9SBenno Rice 			/*
22205244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
22215244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
22225244eac9SBenno Rice 			 */
222352a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22245244eac9SBenno Rice 				victim_pvo = pvo;
22255244eac9SBenno Rice 				break;
22265244eac9SBenno Rice 			}
22275244eac9SBenno Rice 		}
22285244eac9SBenno Rice 
22295244eac9SBenno Rice 		if (victim_pvo == NULL)
223059276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
22315244eac9SBenno Rice 			    "entry", pt);
22325244eac9SBenno Rice 	}
22335244eac9SBenno Rice 
22345244eac9SBenno Rice 	/*
22355244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
22365244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
22375244eac9SBenno Rice 	 * contained in the TLB entry.
22385244eac9SBenno Rice 	 */
223952a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
22405244eac9SBenno Rice 
224152a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
224252a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
22435244eac9SBenno Rice 
22445244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
22455244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
224659276937SPeter Grehan 	moea_pte_replacements++;
22475244eac9SBenno Rice 
224859276937SPeter Grehan 	MOEA_PVO_CHECK(victim_pvo);
224959276937SPeter Grehan 	MOEA_PVO_CHECK(source_pvo);
22505244eac9SBenno Rice 
225159276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22525244eac9SBenno Rice 	return (1);
22535244eac9SBenno Rice }
22545244eac9SBenno Rice 
22555244eac9SBenno Rice static int
225659276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
22575244eac9SBenno Rice {
22585244eac9SBenno Rice 	struct	pte *pt;
22595244eac9SBenno Rice 	int	i;
22605244eac9SBenno Rice 
2261d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2262d644a0b7SAlan Cox 
22635244eac9SBenno Rice 	/*
22645244eac9SBenno Rice 	 * First try primary hash.
22655244eac9SBenno Rice 	 */
226659276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22675244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22685244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
226959276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22705244eac9SBenno Rice 			return (i);
22715244eac9SBenno Rice 		}
22725244eac9SBenno Rice 	}
22735244eac9SBenno Rice 
22745244eac9SBenno Rice 	/*
22755244eac9SBenno Rice 	 * Now try secondary hash.
22765244eac9SBenno Rice 	 */
227759276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2278bd8e6f87SPeter Grehan 
227959276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22805244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22815244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
228259276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22835244eac9SBenno Rice 			return (i);
22845244eac9SBenno Rice 		}
22855244eac9SBenno Rice 	}
22865244eac9SBenno Rice 
228759276937SPeter Grehan 	panic("moea_pte_insert: overflow");
22885244eac9SBenno Rice 	return (-1);
22895244eac9SBenno Rice }
22905244eac9SBenno Rice 
22915244eac9SBenno Rice static boolean_t
229259276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
22935244eac9SBenno Rice {
22945244eac9SBenno Rice 	struct	pvo_entry *pvo;
22955244eac9SBenno Rice 	struct	pte *pt;
22965244eac9SBenno Rice 
229759276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
22985244eac9SBenno Rice 		return (TRUE);
22995244eac9SBenno Rice 
2300c46b90e9SAlan Cox 	vm_page_lock_queues();
23015244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
230259276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23035244eac9SBenno Rice 
23045244eac9SBenno Rice 		/*
23055244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
23065244eac9SBenno Rice 		 * success.
23075244eac9SBenno Rice 		 */
230852a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
230959276937SPeter Grehan 			moea_attr_save(m, ptebit);
231059276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);	/* sanity check */
2311c46b90e9SAlan Cox 			vm_page_unlock_queues();
23125244eac9SBenno Rice 			return (TRUE);
23135244eac9SBenno Rice 		}
23145244eac9SBenno Rice 	}
23155244eac9SBenno Rice 
23165244eac9SBenno Rice 	/*
23175244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
23185244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
23195244eac9SBenno Rice 	 * the PTEs.
23205244eac9SBenno Rice 	 */
2321e4f72b32SMarcel Moolenaar 	powerpc_sync();
23225244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
232359276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23245244eac9SBenno Rice 
23255244eac9SBenno Rice 		/*
23265244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
23275244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
23285244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
23295244eac9SBenno Rice 		 */
233059276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23315244eac9SBenno Rice 		if (pt != NULL) {
233252a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2333d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
233452a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
233559276937SPeter Grehan 				moea_attr_save(m, ptebit);
233659276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);	/* sanity check */
2337c46b90e9SAlan Cox 				vm_page_unlock_queues();
23385244eac9SBenno Rice 				return (TRUE);
23395244eac9SBenno Rice 			}
23405244eac9SBenno Rice 		}
23415244eac9SBenno Rice 	}
23425244eac9SBenno Rice 
2343c46b90e9SAlan Cox 	vm_page_unlock_queues();
23444f7daed0SAndrew Gallatin 	return (FALSE);
23455244eac9SBenno Rice }
23465244eac9SBenno Rice 
234703b6e025SPeter Grehan static u_int
2348ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
23495244eac9SBenno Rice {
235003b6e025SPeter Grehan 	u_int	count;
23515244eac9SBenno Rice 	struct	pvo_entry *pvo;
23525244eac9SBenno Rice 	struct	pte *pt;
2353ce186587SAlan Cox 
2354ce186587SAlan Cox 	vm_page_lock_queues();
23555244eac9SBenno Rice 
23565244eac9SBenno Rice 	/*
23575244eac9SBenno Rice 	 * Clear the cached value.
23585244eac9SBenno Rice 	 */
235959276937SPeter Grehan 	moea_attr_clear(m, ptebit);
23605244eac9SBenno Rice 
23615244eac9SBenno Rice 	/*
23625244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
23635244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
23645244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
23655244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
23665244eac9SBenno Rice 	 * REF/CHG bits.
23675244eac9SBenno Rice 	 */
2368e4f72b32SMarcel Moolenaar 	powerpc_sync();
23695244eac9SBenno Rice 
23705244eac9SBenno Rice 	/*
23715244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
23725244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
23735244eac9SBenno Rice 	 */
237403b6e025SPeter Grehan 	count = 0;
23755244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
237659276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
237759276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23785244eac9SBenno Rice 		if (pt != NULL) {
237952a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
238052a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
238103b6e025SPeter Grehan 				count++;
238259276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
23835244eac9SBenno Rice 			}
2384d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
238503b6e025SPeter Grehan 		}
238652a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
238759276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23885244eac9SBenno Rice 	}
23895244eac9SBenno Rice 
2390ce186587SAlan Cox 	vm_page_unlock_queues();
239103b6e025SPeter Grehan 	return (count);
2392bdf71f56SBenno Rice }
23938bbfa33aSBenno Rice 
23948bbfa33aSBenno Rice /*
239532bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
239632bc7846SPeter Grehan  */
239732bc7846SPeter Grehan static int
239859276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
239932bc7846SPeter Grehan {
240032bc7846SPeter Grehan 	u_int prot;
240132bc7846SPeter Grehan 	u_int32_t start;
240232bc7846SPeter Grehan 	u_int32_t end;
240332bc7846SPeter Grehan 	u_int32_t bat_ble;
240432bc7846SPeter Grehan 
240532bc7846SPeter Grehan 	/*
240632bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
240732bc7846SPeter Grehan 	 */
2408c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
240932bc7846SPeter Grehan 		return (EINVAL);
241032bc7846SPeter Grehan 
241132bc7846SPeter Grehan 	/*
241232bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
241332bc7846SPeter Grehan 	 * so it can function as an i/o page
241432bc7846SPeter Grehan 	 */
241532bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
241632bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
241732bc7846SPeter Grehan 		return (EPERM);
241832bc7846SPeter Grehan 
241932bc7846SPeter Grehan 	/*
242032bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
242132bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
242232bc7846SPeter Grehan 	 * not requiring masking)
242332bc7846SPeter Grehan 	 */
242432bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
242532bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
242632bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
242732bc7846SPeter Grehan 
242832bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
242932bc7846SPeter Grehan 		return (ERANGE);
243032bc7846SPeter Grehan 
243132bc7846SPeter Grehan 	return (0);
243232bc7846SPeter Grehan }
243332bc7846SPeter Grehan 
243459276937SPeter Grehan boolean_t
243559276937SPeter Grehan moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2436c0763d37SSuleiman Souhlal {
2437c0763d37SSuleiman Souhlal 	int i;
2438c0763d37SSuleiman Souhlal 
2439c0763d37SSuleiman Souhlal 	/*
2440c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2441c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2442c0763d37SSuleiman Souhlal 	 */
2443c0763d37SSuleiman Souhlal 
2444c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
244559276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2446c0763d37SSuleiman Souhlal 			return (0);
2447c0763d37SSuleiman Souhlal 
2448c0763d37SSuleiman Souhlal 	return (EFAULT);
2449c0763d37SSuleiman Souhlal }
245032bc7846SPeter Grehan 
245132bc7846SPeter Grehan /*
24528bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
24538bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
24548bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
24558bbfa33aSBenno Rice  * NOT real memory.
24568bbfa33aSBenno Rice  */
24578bbfa33aSBenno Rice void *
245859276937SPeter Grehan moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
24598bbfa33aSBenno Rice {
2460c1f4123bSNathan Whitehorn 
2461c1f4123bSNathan Whitehorn 	return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2462c1f4123bSNathan Whitehorn }
2463c1f4123bSNathan Whitehorn 
2464c1f4123bSNathan Whitehorn void *
2465c1f4123bSNathan Whitehorn moea_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma)
2466c1f4123bSNathan Whitehorn {
246732bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
246832bc7846SPeter Grehan 	int i;
24698bbfa33aSBenno Rice 
247032bc7846SPeter Grehan 	ppa = trunc_page(pa);
24718bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
24728bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
24738bbfa33aSBenno Rice 
247432bc7846SPeter Grehan 	/*
247532bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
247632bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
247732bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
247832bc7846SPeter Grehan 	 */
247932bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
248059276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
248132bc7846SPeter Grehan 			return ((void *) pa);
248232bc7846SPeter Grehan 	}
248332bc7846SPeter Grehan 
2484e53f32acSAlan Cox 	va = kmem_alloc_nofault(kernel_map, size);
24858bbfa33aSBenno Rice 	if (!va)
248659276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
24878bbfa33aSBenno Rice 
24888bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
2489c1f4123bSNathan Whitehorn 		moea_kenter_attr(mmu, tmpva, ppa, ma);
2490e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
24918bbfa33aSBenno Rice 		size -= PAGE_SIZE;
24928bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
249332bc7846SPeter Grehan 		ppa += PAGE_SIZE;
24948bbfa33aSBenno Rice 	}
24958bbfa33aSBenno Rice 
24968bbfa33aSBenno Rice 	return ((void *)(va + offset));
24978bbfa33aSBenno Rice }
24988bbfa33aSBenno Rice 
24998bbfa33aSBenno Rice void
250059276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
25018bbfa33aSBenno Rice {
25028bbfa33aSBenno Rice 	vm_offset_t base, offset;
25038bbfa33aSBenno Rice 
250432bc7846SPeter Grehan 	/*
250532bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
250632bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
250732bc7846SPeter Grehan 	 */
2508ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
25098bbfa33aSBenno Rice 		base = trunc_page(va);
25108bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
25118bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
25128bbfa33aSBenno Rice 		kmem_free(kernel_map, base, size);
25138bbfa33aSBenno Rice 	}
251432bc7846SPeter Grehan }
25151a4fcaebSMarcel Moolenaar 
25161a4fcaebSMarcel Moolenaar static void
25171a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
25181a4fcaebSMarcel Moolenaar {
25191a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
25201a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
25211a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
25221a4fcaebSMarcel Moolenaar 	vm_size_t len;
25231a4fcaebSMarcel Moolenaar 
25241a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
25251a4fcaebSMarcel Moolenaar 	while (sz > 0) {
25261a4fcaebSMarcel Moolenaar 		lim = round_page(va);
25271a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
25281a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
25291a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
25301a4fcaebSMarcel Moolenaar 			pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
25311a4fcaebSMarcel Moolenaar 			    (va & ADDR_POFF);
25321a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
25331a4fcaebSMarcel Moolenaar 		}
25341a4fcaebSMarcel Moolenaar 		va += len;
25351a4fcaebSMarcel Moolenaar 		sz -= len;
25361a4fcaebSMarcel Moolenaar 	}
25371a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
25381a4fcaebSMarcel Moolenaar }
2539