xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision 8d9e6d9f931fb38af14283f451f482c928d3de2d)
160727d8bSWarner Losh /*-
25244eac9SBenno Rice  * Copyright (c) 2001 The NetBSD Foundation, Inc.
35244eac9SBenno Rice  * All rights reserved.
45244eac9SBenno Rice  *
55244eac9SBenno Rice  * This code is derived from software contributed to The NetBSD Foundation
65244eac9SBenno Rice  * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
75244eac9SBenno Rice  *
85244eac9SBenno Rice  * Redistribution and use in source and binary forms, with or without
95244eac9SBenno Rice  * modification, are permitted provided that the following conditions
105244eac9SBenno Rice  * are met:
115244eac9SBenno Rice  * 1. Redistributions of source code must retain the above copyright
125244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer.
135244eac9SBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
145244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer in the
155244eac9SBenno Rice  *    documentation and/or other materials provided with the distribution.
165244eac9SBenno Rice  * 3. All advertising materials mentioning features or use of this software
175244eac9SBenno Rice  *    must display the following acknowledgement:
185244eac9SBenno Rice  *        This product includes software developed by the NetBSD
195244eac9SBenno Rice  *        Foundation, Inc. and its contributors.
205244eac9SBenno Rice  * 4. Neither the name of The NetBSD Foundation nor the names of its
215244eac9SBenno Rice  *    contributors may be used to endorse or promote products derived
225244eac9SBenno Rice  *    from this software without specific prior written permission.
235244eac9SBenno Rice  *
245244eac9SBenno Rice  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
255244eac9SBenno Rice  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
265244eac9SBenno Rice  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
275244eac9SBenno Rice  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
285244eac9SBenno Rice  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
295244eac9SBenno Rice  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
305244eac9SBenno Rice  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
315244eac9SBenno Rice  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
325244eac9SBenno Rice  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
335244eac9SBenno Rice  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
345244eac9SBenno Rice  * POSSIBILITY OF SUCH DAMAGE.
355244eac9SBenno Rice  */
3660727d8bSWarner Losh /*-
37f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
38f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 TooLs GmbH.
39f9bac91bSBenno Rice  * All rights reserved.
40f9bac91bSBenno Rice  *
41f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
42f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
43f9bac91bSBenno Rice  * are met:
44f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
45f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
46f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
47f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
48f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
49f9bac91bSBenno Rice  * 3. All advertising materials mentioning features or use of this software
50f9bac91bSBenno Rice  *    must display the following acknowledgement:
51f9bac91bSBenno Rice  *	This product includes software developed by TooLs GmbH.
52f9bac91bSBenno Rice  * 4. The name of TooLs GmbH may not be used to endorse or promote products
53f9bac91bSBenno Rice  *    derived from this software without specific prior written permission.
54f9bac91bSBenno Rice  *
55f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
56f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
61f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
62f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
63f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
64f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65f9bac91bSBenno Rice  *
66111c77dcSBenno Rice  * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
67f9bac91bSBenno Rice  */
6860727d8bSWarner Losh /*-
69f9bac91bSBenno Rice  * Copyright (C) 2001 Benno Rice.
70f9bac91bSBenno Rice  * All rights reserved.
71f9bac91bSBenno Rice  *
72f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
73f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
74f9bac91bSBenno Rice  * are met:
75f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
76f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
77f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
78f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
79f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
80f9bac91bSBenno Rice  *
81f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
82f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
85f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
86f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
87f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
88f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
89f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
90f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91f9bac91bSBenno Rice  */
92f9bac91bSBenno Rice 
938368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
948368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
95f9bac91bSBenno Rice 
965244eac9SBenno Rice /*
975244eac9SBenno Rice  * Manages physical address maps.
985244eac9SBenno Rice  *
995244eac9SBenno Rice  * In addition to hardware address maps, this module is called upon to
1005244eac9SBenno Rice  * provide software-use-only maps which may or may not be stored in the
1015244eac9SBenno Rice  * same form as hardware maps.  These pseudo-maps are used to store
1025244eac9SBenno Rice  * intermediate results from copy operations to and from address spaces.
1035244eac9SBenno Rice  *
1045244eac9SBenno Rice  * Since the information managed by this module is also stored by the
1055244eac9SBenno Rice  * logical address mapping module, this module may throw away valid virtual
1065244eac9SBenno Rice  * to physical mappings at almost any time.  However, invalidations of
1075244eac9SBenno Rice  * mappings must be done as requested.
1085244eac9SBenno Rice  *
1095244eac9SBenno Rice  * In order to cope with hardware architectures which make virtual to
1105244eac9SBenno Rice  * physical map invalidates expensive, this module may delay invalidate
1115244eac9SBenno Rice  * reduced protection operations until such time as they are actually
1125244eac9SBenno Rice  * necessary.  This module is given full information as to which processors
1135244eac9SBenno Rice  * are currently using which maps, and to when physical maps must be made
1145244eac9SBenno Rice  * correct.
1155244eac9SBenno Rice  */
1165244eac9SBenno Rice 
117ad7a226fSPeter Wemm #include "opt_kstack_pages.h"
118ad7a226fSPeter Wemm 
119f9bac91bSBenno Rice #include <sys/param.h>
1200b27d710SPeter Wemm #include <sys/kernel.h>
121c47dd3dbSAttilio Rao #include <sys/queue.h>
122c47dd3dbSAttilio Rao #include <sys/cpuset.h>
1235244eac9SBenno Rice #include <sys/ktr.h>
12494e0b85eSMark Peek #include <sys/lock.h>
1255244eac9SBenno Rice #include <sys/msgbuf.h>
126f9bac91bSBenno Rice #include <sys/mutex.h>
1275244eac9SBenno Rice #include <sys/proc.h>
1283653f5cbSAlan Cox #include <sys/rwlock.h>
129c47dd3dbSAttilio Rao #include <sys/sched.h>
1305244eac9SBenno Rice #include <sys/sysctl.h>
1315244eac9SBenno Rice #include <sys/systm.h>
1325244eac9SBenno Rice #include <sys/vmmeter.h>
1335244eac9SBenno Rice 
1345244eac9SBenno Rice #include <dev/ofw/openfirm.h>
135f9bac91bSBenno Rice 
136f9bac91bSBenno Rice #include <vm/vm.h>
137f9bac91bSBenno Rice #include <vm/vm_param.h>
138f9bac91bSBenno Rice #include <vm/vm_kern.h>
139f9bac91bSBenno Rice #include <vm/vm_page.h>
140f9bac91bSBenno Rice #include <vm/vm_map.h>
141f9bac91bSBenno Rice #include <vm/vm_object.h>
142f9bac91bSBenno Rice #include <vm/vm_extern.h>
143f9bac91bSBenno Rice #include <vm/vm_pageout.h>
144f9bac91bSBenno Rice #include <vm/vm_pager.h>
145378862a7SJeff Roberson #include <vm/uma.h>
146f9bac91bSBenno Rice 
1477c277971SPeter Grehan #include <machine/cpu.h>
148b40ce02aSNathan Whitehorn #include <machine/platform.h>
149d699b539SMark Peek #include <machine/bat.h>
1505244eac9SBenno Rice #include <machine/frame.h>
1515244eac9SBenno Rice #include <machine/md_var.h>
1525244eac9SBenno Rice #include <machine/psl.h>
153f9bac91bSBenno Rice #include <machine/pte.h>
15412640815SMarcel Moolenaar #include <machine/smp.h>
1555244eac9SBenno Rice #include <machine/sr.h>
15659276937SPeter Grehan #include <machine/mmuvar.h>
157e347e23bSNathan Whitehorn #include <machine/trap_aim.h>
158f9bac91bSBenno Rice 
15959276937SPeter Grehan #include "mmu_if.h"
16059276937SPeter Grehan 
16159276937SPeter Grehan #define	MOEA_DEBUG
162f9bac91bSBenno Rice 
1635244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
164f9bac91bSBenno Rice 
1655244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1665244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1675244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1685244eac9SBenno Rice 
1695244eac9SBenno Rice struct ofw_map {
1705244eac9SBenno Rice 	vm_offset_t	om_va;
1715244eac9SBenno Rice 	vm_size_t	om_len;
1725244eac9SBenno Rice 	vm_offset_t	om_pa;
1735244eac9SBenno Rice 	u_int		om_mode;
1745244eac9SBenno Rice };
175f9bac91bSBenno Rice 
1765244eac9SBenno Rice /*
1775244eac9SBenno Rice  * Map of physical memory regions.
1785244eac9SBenno Rice  */
17931c82d03SBenno Rice static struct	mem_region *regions;
18031c82d03SBenno Rice static struct	mem_region *pregions;
181c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
182c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
183aa39961eSBenno Rice static struct	ofw_map *translations;
1845244eac9SBenno Rice 
185f9bac91bSBenno Rice /*
186f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
187f489bf21SAlan Cox  */
18859276937SPeter Grehan struct mtx	moea_table_mutex;
189e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
190f489bf21SAlan Cox 
191e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
192e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
193e4f72b32SMarcel Moolenaar 
194f489bf21SAlan Cox /*
1955244eac9SBenno Rice  * PTEG data.
196f9bac91bSBenno Rice  */
19759276937SPeter Grehan static struct	pteg *moea_pteg_table;
19859276937SPeter Grehan u_int		moea_pteg_count;
19959276937SPeter Grehan u_int		moea_pteg_mask;
2005244eac9SBenno Rice 
2015244eac9SBenno Rice /*
2025244eac9SBenno Rice  * PVO data.
2035244eac9SBenno Rice  */
20459276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
20559276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
20659276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
2075244eac9SBenno Rice 
2083653f5cbSAlan Cox /*
2093653f5cbSAlan Cox  * Isolate the global pv list lock from data and other locks to prevent false
2103653f5cbSAlan Cox  * sharing within the cache.
2113653f5cbSAlan Cox  */
2123653f5cbSAlan Cox static struct {
2133653f5cbSAlan Cox 	struct rwlock	lock;
2143653f5cbSAlan Cox 	char		padding[CACHE_LINE_SIZE - sizeof(struct rwlock)];
2153653f5cbSAlan Cox } pvh_global __aligned(CACHE_LINE_SIZE);
2163653f5cbSAlan Cox 
2173653f5cbSAlan Cox #define	pvh_global_lock	pvh_global.lock
2183653f5cbSAlan Cox 
21959276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
22059276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2215244eac9SBenno Rice 
2220d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
22359276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
22459276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2255244eac9SBenno Rice 
2265244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
22759276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2285244eac9SBenno Rice 
22959276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2305244eac9SBenno Rice 
2315244eac9SBenno Rice /*
2325244eac9SBenno Rice  * Statistics.
2335244eac9SBenno Rice  */
23459276937SPeter Grehan u_int	moea_pte_valid = 0;
23559276937SPeter Grehan u_int	moea_pte_overflow = 0;
23659276937SPeter Grehan u_int	moea_pte_replacements = 0;
23759276937SPeter Grehan u_int	moea_pvo_entries = 0;
23859276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
23959276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
24059276937SPeter Grehan u_int	moea_pte_spills = 0;
24159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2425244eac9SBenno Rice     0, "");
24359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
24459276937SPeter Grehan     &moea_pte_overflow, 0, "");
24559276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
24659276937SPeter Grehan     &moea_pte_replacements, 0, "");
24759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2485244eac9SBenno Rice     0, "");
24959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
25059276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
25159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
25259276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
25359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
25459276937SPeter Grehan     &moea_pte_spills, 0, "");
2555244eac9SBenno Rice 
2565244eac9SBenno Rice /*
25759276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2585244eac9SBenno Rice  */
25959276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2605244eac9SBenno Rice 
2615244eac9SBenno Rice /*
2625244eac9SBenno Rice  * PTE calls.
2635244eac9SBenno Rice  */
26459276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2655244eac9SBenno Rice 
2665244eac9SBenno Rice /*
2675244eac9SBenno Rice  * PVO calls.
2685244eac9SBenno Rice  */
26959276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2705244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
27159276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
27259276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
27359276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2745244eac9SBenno Rice 
2755244eac9SBenno Rice /*
2765244eac9SBenno Rice  * Utility routines.
2775244eac9SBenno Rice  */
278ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
279ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
28059276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
28159276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
282ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
28359276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
28459276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
28559276937SPeter Grehan 
28659276937SPeter Grehan /*
28759276937SPeter Grehan  * Kernel MMU interface
28859276937SPeter Grehan  */
28959276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
29059276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
29159276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t);
29259276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
29359276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
294ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
295ce142d9eSAlan Cox     vm_prot_t);
2962053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
29759276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
29859276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
29959276937SPeter Grehan void moea_init(mmu_t);
30059276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
301e396eb60SAlan Cox boolean_t moea_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
3027b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t);
303*8d9e6d9fSAlan Cox int moea_ts_referenced(mmu_t, vm_page_t);
30420b79612SRafal Jaworowski vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_paddr_t, vm_paddr_t, int);
30559276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
30659677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
30759276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
30859276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
30959276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
31059276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
31159276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
31259276937SPeter Grehan void moea_release(mmu_t, pmap_t);
31359276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
31459276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
31578985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
31659276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
31759276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
31859276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
31959276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
32059276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
3211c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int);
32259276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
32320b79612SRafal Jaworowski void *moea_mapdev(mmu_t, vm_paddr_t, vm_size_t);
324c1f4123bSNathan Whitehorn void *moea_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t);
32559276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
32620b79612SRafal Jaworowski vm_paddr_t moea_kextract(mmu_t, vm_offset_t);
327c1f4123bSNathan Whitehorn void moea_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t);
32820b79612SRafal Jaworowski void moea_kenter(mmu_t, vm_offset_t, vm_paddr_t);
329c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma);
33020b79612SRafal Jaworowski boolean_t moea_dev_direct_mapped(mmu_t, vm_paddr_t, vm_size_t);
3311a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
33259276937SPeter Grehan 
33359276937SPeter Grehan static mmu_method_t moea_methods[] = {
33459276937SPeter Grehan 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
33559276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
33659276937SPeter Grehan 	MMUMETHOD(mmu_clear_reference,	moea_clear_reference),
33759276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
33859276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
339ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
34059276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
34159276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
34259276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
34359276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
34459276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
345e396eb60SAlan Cox 	MMUMETHOD(mmu_is_prefaultable,	moea_is_prefaultable),
3467b85f591SAlan Cox 	MMUMETHOD(mmu_is_referenced,	moea_is_referenced),
34759276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
34859276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
34959276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
35059677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
35159276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
35259276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
35359276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
35459276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
35559276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
35659276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
35759276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
35859276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
35978985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
3601a4fcaebSMarcel Moolenaar 	MMUMETHOD(mmu_sync_icache,	moea_sync_icache),
36159276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
36259276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
36359276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
36459276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
36559276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
366c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_page_set_memattr,	moea_page_set_memattr),
36759276937SPeter Grehan 
36859276937SPeter Grehan 	/* Internal interfaces */
36959276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
3701c96bdd1SNathan Whitehorn 	MMUMETHOD(mmu_cpu_bootstrap,   	moea_cpu_bootstrap),
371c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_mapdev_attr,	moea_mapdev_attr),
37259276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
37359276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
37459276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
37559276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
376c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_kenter_attr,	moea_kenter_attr),
37759276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
37859276937SPeter Grehan 
37959276937SPeter Grehan 	{ 0, 0 }
38059276937SPeter Grehan };
38159276937SPeter Grehan 
38233529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0);
38333529b98SPeter Grehan 
384c1f4123bSNathan Whitehorn static __inline uint32_t
385c1f4123bSNathan Whitehorn moea_calc_wimg(vm_offset_t pa, vm_memattr_t ma)
386c1f4123bSNathan Whitehorn {
387c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
388c1f4123bSNathan Whitehorn 	int i;
389c1f4123bSNathan Whitehorn 
390c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
391c1f4123bSNathan Whitehorn 		switch (ma) {
392c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
393c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
394c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
395c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
396c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
397c1f4123bSNathan Whitehorn 			return (PTE_I);
398c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
399c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
400c1f4123bSNathan Whitehorn 		}
401c1f4123bSNathan Whitehorn 	}
402c1f4123bSNathan Whitehorn 
403c1f4123bSNathan Whitehorn 	/*
404c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
405c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
406c1f4123bSNathan Whitehorn 	 */
407c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
408c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
409c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
410c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
411c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
412c1f4123bSNathan Whitehorn 			break;
413c1f4123bSNathan Whitehorn 		}
414c1f4123bSNathan Whitehorn 	}
415c1f4123bSNathan Whitehorn 
416c1f4123bSNathan Whitehorn 	return pte_lo;
417c1f4123bSNathan Whitehorn }
41859276937SPeter Grehan 
419e4f72b32SMarcel Moolenaar static void
420e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
421e4f72b32SMarcel Moolenaar {
422e4f72b32SMarcel Moolenaar 
423e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
42494363f53SNathan Whitehorn 	__asm __volatile("ptesync");
425e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
42694363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
427e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
428e4f72b32SMarcel Moolenaar }
429e4f72b32SMarcel Moolenaar 
430e4f72b32SMarcel Moolenaar static void
431e4f72b32SMarcel Moolenaar tlbia(void)
432e4f72b32SMarcel Moolenaar {
433e4f72b32SMarcel Moolenaar 	vm_offset_t va;
434e4f72b32SMarcel Moolenaar 
435e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
436e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
437e4f72b32SMarcel Moolenaar 		powerpc_sync();
438e4f72b32SMarcel Moolenaar 	}
439e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
440e4f72b32SMarcel Moolenaar 	powerpc_sync();
441e4f72b32SMarcel Moolenaar }
4425244eac9SBenno Rice 
4435244eac9SBenno Rice static __inline int
4445244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4455244eac9SBenno Rice {
4465244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4475244eac9SBenno Rice }
4485244eac9SBenno Rice 
4495244eac9SBenno Rice static __inline u_int
4505244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4515244eac9SBenno Rice {
4525244eac9SBenno Rice 	u_int hash;
4535244eac9SBenno Rice 
4545244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4555244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
45659276937SPeter Grehan 	return (hash & moea_pteg_mask);
4575244eac9SBenno Rice }
4585244eac9SBenno Rice 
4595244eac9SBenno Rice static __inline struct pvo_head *
4605244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
461f9bac91bSBenno Rice {
462f9bac91bSBenno Rice 
4635244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
464f9bac91bSBenno Rice }
465f9bac91bSBenno Rice 
466f9bac91bSBenno Rice static __inline void
46759276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
468f9bac91bSBenno Rice {
469f9bac91bSBenno Rice 
4703653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4715244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4725244eac9SBenno Rice }
4735244eac9SBenno Rice 
4745244eac9SBenno Rice static __inline int
47559276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4765244eac9SBenno Rice {
4775244eac9SBenno Rice 
4785244eac9SBenno Rice 	return (m->md.mdpg_attrs);
479f9bac91bSBenno Rice }
480f9bac91bSBenno Rice 
481f9bac91bSBenno Rice static __inline void
48259276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
483f9bac91bSBenno Rice {
484f9bac91bSBenno Rice 
4853653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4865244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
487f9bac91bSBenno Rice }
488f9bac91bSBenno Rice 
489f9bac91bSBenno Rice static __inline int
49059276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
491f9bac91bSBenno Rice {
4925244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4935244eac9SBenno Rice 		return (1);
494f9bac91bSBenno Rice 
4955244eac9SBenno Rice 	return (0);
496f9bac91bSBenno Rice }
497f9bac91bSBenno Rice 
498f9bac91bSBenno Rice static __inline int
49959276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
500f9bac91bSBenno Rice {
5015244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
5025244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5035244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
504f9bac91bSBenno Rice }
505f9bac91bSBenno Rice 
5065244eac9SBenno Rice static __inline void
50759276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
508f9bac91bSBenno Rice {
509d644a0b7SAlan Cox 
510d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
511d644a0b7SAlan Cox 
512f9bac91bSBenno Rice 	/*
5135244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5145244eac9SBenno Rice 	 * set when the real pte is set in memory.
515f9bac91bSBenno Rice 	 *
516f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
517f9bac91bSBenno Rice 	 */
5185244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5195244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5205244eac9SBenno Rice 	pt->pte_lo = pte_lo;
521f9bac91bSBenno Rice }
522f9bac91bSBenno Rice 
5235244eac9SBenno Rice static __inline void
52459276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
525f9bac91bSBenno Rice {
526f9bac91bSBenno Rice 
527d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5285244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
529f9bac91bSBenno Rice }
530f9bac91bSBenno Rice 
5315244eac9SBenno Rice static __inline void
53259276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
533f9bac91bSBenno Rice {
5345244eac9SBenno Rice 
535d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
536d644a0b7SAlan Cox 
5375244eac9SBenno Rice 	/*
5385244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5395244eac9SBenno Rice 	 */
5405244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
541e4f72b32SMarcel Moolenaar 	tlbie(va);
5425244eac9SBenno Rice }
5435244eac9SBenno Rice 
5445244eac9SBenno Rice static __inline void
54559276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5465244eac9SBenno Rice {
5475244eac9SBenno Rice 
548d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5495244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5505244eac9SBenno Rice 
5515244eac9SBenno Rice 	/*
5525244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
5535244eac9SBenno Rice 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
5545244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5555244eac9SBenno Rice 	 */
5565244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
557e4f72b32SMarcel Moolenaar 	powerpc_sync();
5585244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
559e4f72b32SMarcel Moolenaar 	powerpc_sync();
56059276937SPeter Grehan 	moea_pte_valid++;
5615244eac9SBenno Rice }
5625244eac9SBenno Rice 
5635244eac9SBenno Rice static __inline void
56459276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5655244eac9SBenno Rice {
5665244eac9SBenno Rice 
567d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5685244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5695244eac9SBenno Rice 
5705244eac9SBenno Rice 	/*
5715244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5725244eac9SBenno Rice 	 */
573e4f72b32SMarcel Moolenaar 	powerpc_sync();
5745244eac9SBenno Rice 
5755244eac9SBenno Rice 	/*
5765244eac9SBenno Rice 	 * Invalidate the pte.
5775244eac9SBenno Rice 	 */
5785244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5795244eac9SBenno Rice 
580e4f72b32SMarcel Moolenaar 	tlbie(va);
5815244eac9SBenno Rice 
5825244eac9SBenno Rice 	/*
5835244eac9SBenno Rice 	 * Save the reg & chg bits.
5845244eac9SBenno Rice 	 */
58559276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
58659276937SPeter Grehan 	moea_pte_valid--;
5875244eac9SBenno Rice }
5885244eac9SBenno Rice 
5895244eac9SBenno Rice static __inline void
59059276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5915244eac9SBenno Rice {
5925244eac9SBenno Rice 
5935244eac9SBenno Rice 	/*
5945244eac9SBenno Rice 	 * Invalidate the PTE
5955244eac9SBenno Rice 	 */
59659276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
59759276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
598f9bac91bSBenno Rice }
599f9bac91bSBenno Rice 
600f9bac91bSBenno Rice /*
6015244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
602f9bac91bSBenno Rice  */
6035244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
6045244eac9SBenno Rice 
6055244eac9SBenno Rice static int
6065244eac9SBenno Rice om_cmp(const void *a, const void *b)
6075244eac9SBenno Rice {
6085244eac9SBenno Rice 	const struct	ofw_map *mapa;
6095244eac9SBenno Rice 	const struct	ofw_map *mapb;
6105244eac9SBenno Rice 
6115244eac9SBenno Rice 	mapa = a;
6125244eac9SBenno Rice 	mapb = b;
6135244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6145244eac9SBenno Rice 		return (-1);
6155244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6165244eac9SBenno Rice 		return (1);
6175244eac9SBenno Rice 	else
6185244eac9SBenno Rice 		return (0);
619f9bac91bSBenno Rice }
620f9bac91bSBenno Rice 
621f9bac91bSBenno Rice void
6221c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap)
62312640815SMarcel Moolenaar {
62412640815SMarcel Moolenaar 	u_int sdr;
62512640815SMarcel Moolenaar 	int i;
62612640815SMarcel Moolenaar 
62712640815SMarcel Moolenaar 	if (ap) {
628e4f72b32SMarcel Moolenaar 		powerpc_sync();
62912640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
63012640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
63112640815SMarcel Moolenaar 		isync();
63212640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
63312640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
63412640815SMarcel Moolenaar 		isync();
63512640815SMarcel Moolenaar 	}
63612640815SMarcel Moolenaar 
63701d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
63801d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
63912640815SMarcel Moolenaar 	isync();
64012640815SMarcel Moolenaar 
64101d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
64201d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
64301d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
64401d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
64501d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
64612640815SMarcel Moolenaar 	isync();
64712640815SMarcel Moolenaar 
64812640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
649fe3b4685SNathan Whitehorn 		mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
650e4f72b32SMarcel Moolenaar 	powerpc_sync();
65112640815SMarcel Moolenaar 
65212640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
65312640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
65412640815SMarcel Moolenaar 	isync();
65512640815SMarcel Moolenaar 
65686c1fb4cSMarcel Moolenaar 	tlbia();
65712640815SMarcel Moolenaar }
65812640815SMarcel Moolenaar 
65912640815SMarcel Moolenaar void
66059276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
661f9bac91bSBenno Rice {
66231c82d03SBenno Rice 	ihandle_t	mmui;
6635244eac9SBenno Rice 	phandle_t	chosen, mmu;
6645244eac9SBenno Rice 	int		sz;
6655244eac9SBenno Rice 	int		i, j;
666e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6675244eac9SBenno Rice 	vm_offset_t	pa, va, off;
66850c202c5SJeff Roberson 	void		*dpcpu;
669976cc697SNathan Whitehorn 	register_t	msr;
670f9bac91bSBenno Rice 
671f9bac91bSBenno Rice         /*
67232bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6730d290675SBenno Rice          */
6740d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6750d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6760d290675SBenno Rice 
6770d290675SBenno Rice         /*
6780d290675SBenno Rice          * Map PCI memory space.
6790d290675SBenno Rice          */
6800d290675SBenno Rice         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6810d290675SBenno Rice         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6820d290675SBenno Rice 
6830d290675SBenno Rice         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6840d290675SBenno Rice         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6850d290675SBenno Rice 
6860d290675SBenno Rice         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6870d290675SBenno Rice         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6880d290675SBenno Rice 
6890d290675SBenno Rice         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6900d290675SBenno Rice         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6910d290675SBenno Rice 
6920d290675SBenno Rice         /*
6930d290675SBenno Rice          * Map obio devices.
6940d290675SBenno Rice          */
6950d290675SBenno Rice         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
6960d290675SBenno Rice         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
6970d290675SBenno Rice 
6980d290675SBenno Rice 	/*
6995244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
700976cc697SNathan Whitehorn 	 * where we are. Turn off instruction relocation temporarily
701976cc697SNathan Whitehorn 	 * to prevent faults while reprogramming the IBAT.
702f9bac91bSBenno Rice 	 */
703976cc697SNathan Whitehorn 	msr = mfmsr();
704976cc697SNathan Whitehorn 	mtmsr(msr & ~PSL_IR);
70559276937SPeter Grehan 	__asm (".balign 32; \n"
70672ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
7075d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
70812640815SMarcel Moolenaar 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
709976cc697SNathan Whitehorn 	mtmsr(msr);
7100d290675SBenno Rice 
7110d290675SBenno Rice 	/* map pci space */
71212640815SMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
71312640815SMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
71412640815SMarcel Moolenaar 	isync();
715f9bac91bSBenno Rice 
7161c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7171c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7181c96bdd1SNathan Whitehorn 
71931c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
72059276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
72131c82d03SBenno Rice 
72231c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
72332bc7846SPeter Grehan 		vm_offset_t pa;
72432bc7846SPeter Grehan 		vm_offset_t end;
72532bc7846SPeter Grehan 
72631c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
72731c82d03SBenno Rice 			pregions[i].mr_start,
72831c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
72931c82d03SBenno Rice 			pregions[i].mr_size);
73032bc7846SPeter Grehan 		/*
73132bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
73232bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
73332bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
73432bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
73532bc7846SPeter Grehan 		 * a while yet.
73632bc7846SPeter Grehan 		 */
73732bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
73832bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
73932bc7846SPeter Grehan 		do {
74032bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
74132bc7846SPeter Grehan 
74232bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
74332bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
74432bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
74532bc7846SPeter Grehan 		} while (pa < end);
74631c82d03SBenno Rice 	}
74731c82d03SBenno Rice 
74831c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
74959276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
75097f7cde4SNathan Whitehorn 
7515244eac9SBenno Rice 	phys_avail_count = 0;
752d2c1f576SBenno Rice 	physsz = 0;
753b0c21309SPeter Grehan 	hwphyssz = 0;
754b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
75531c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7565244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7575244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7585244eac9SBenno Rice 		    regions[i].mr_size);
759e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
760e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
761e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
762e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
763e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
764e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
765e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
766e2f6d6e2SPeter Grehan 				phys_avail_count++;
767e2f6d6e2SPeter Grehan 			}
768e2f6d6e2SPeter Grehan 			break;
769e2f6d6e2SPeter Grehan 		}
7705244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7715244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7725244eac9SBenno Rice 		phys_avail_count++;
773d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
774f9bac91bSBenno Rice 	}
775e347e23bSNathan Whitehorn 
776e347e23bSNathan Whitehorn 	/* Check for overlap with the kernel and exception vectors */
777e347e23bSNathan Whitehorn 	for (j = 0; j < 2*phys_avail_count; j+=2) {
778e347e23bSNathan Whitehorn 		if (phys_avail[j] < EXC_LAST)
779e347e23bSNathan Whitehorn 			phys_avail[j] += EXC_LAST;
780e347e23bSNathan Whitehorn 
781e347e23bSNathan Whitehorn 		if (kernelstart >= phys_avail[j] &&
782e347e23bSNathan Whitehorn 		    kernelstart < phys_avail[j+1]) {
783e347e23bSNathan Whitehorn 			if (kernelend < phys_avail[j+1]) {
784e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] =
785e347e23bSNathan Whitehorn 				    (kernelend & ~PAGE_MASK) + PAGE_SIZE;
786e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
787e347e23bSNathan Whitehorn 				    phys_avail[j+1];
788e347e23bSNathan Whitehorn 				phys_avail_count++;
789e347e23bSNathan Whitehorn 			}
790e347e23bSNathan Whitehorn 
791e347e23bSNathan Whitehorn 			phys_avail[j+1] = kernelstart & ~PAGE_MASK;
792e347e23bSNathan Whitehorn 		}
793e347e23bSNathan Whitehorn 
794e347e23bSNathan Whitehorn 		if (kernelend >= phys_avail[j] &&
795e347e23bSNathan Whitehorn 		    kernelend < phys_avail[j+1]) {
796e347e23bSNathan Whitehorn 			if (kernelstart > phys_avail[j]) {
797e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] = phys_avail[j];
798e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
799e347e23bSNathan Whitehorn 				    kernelstart & ~PAGE_MASK;
800e347e23bSNathan Whitehorn 				phys_avail_count++;
801e347e23bSNathan Whitehorn 			}
802e347e23bSNathan Whitehorn 
803e347e23bSNathan Whitehorn 			phys_avail[j] = (kernelend & ~PAGE_MASK) + PAGE_SIZE;
804e347e23bSNathan Whitehorn 		}
805e347e23bSNathan Whitehorn 	}
806e347e23bSNathan Whitehorn 
807d2c1f576SBenno Rice 	physmem = btoc(physsz);
808f9bac91bSBenno Rice 
809f9bac91bSBenno Rice 	/*
8105244eac9SBenno Rice 	 * Allocate PTEG table.
811f9bac91bSBenno Rice 	 */
8125244eac9SBenno Rice #ifdef PTEGCOUNT
81359276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
8145244eac9SBenno Rice #else
81559276937SPeter Grehan 	moea_pteg_count = 0x1000;
816f9bac91bSBenno Rice 
81759276937SPeter Grehan 	while (moea_pteg_count < physmem)
81859276937SPeter Grehan 		moea_pteg_count <<= 1;
819f9bac91bSBenno Rice 
82059276937SPeter Grehan 	moea_pteg_count >>= 1;
8215244eac9SBenno Rice #endif /* PTEGCOUNT */
822f9bac91bSBenno Rice 
82359276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
82459276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
8255244eac9SBenno Rice 	    size);
82659276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
82759276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
82859276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
82959276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
830f9bac91bSBenno Rice 
8315244eac9SBenno Rice 	/*
832864bc520SBenno Rice 	 * Allocate pv/overflow lists.
8335244eac9SBenno Rice 	 */
83459276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
83559276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
8365244eac9SBenno Rice 	    PAGE_SIZE);
83759276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
83859276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
83959276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
8405244eac9SBenno Rice 
8415244eac9SBenno Rice 	/*
842f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
843f489bf21SAlan Cox 	 * tables.
844f489bf21SAlan Cox 	 */
845d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
846d644a0b7SAlan Cox 	    MTX_RECURSE);
847e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
848f489bf21SAlan Cox 
849e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
850e4f72b32SMarcel Moolenaar 
851f489bf21SAlan Cox 	/*
8525244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8535244eac9SBenno Rice 	 */
85459276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8550d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
85659276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8575244eac9SBenno Rice 
8585244eac9SBenno Rice 	/*
8595244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8605244eac9SBenno Rice 	 */
86159276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8625244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
86359276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8645244eac9SBenno Rice 
8655244eac9SBenno Rice 	/*
866fe3b4685SNathan Whitehorn 	 * Initialize the kernel pmap (which is statically allocated).
8675244eac9SBenno Rice 	 */
868fe3b4685SNathan Whitehorn 	PMAP_LOCK_INIT(kernel_pmap);
869fe3b4685SNathan Whitehorn 	for (i = 0; i < 16; i++)
870fe3b4685SNathan Whitehorn 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
871c47dd3dbSAttilio Rao 	CPU_FILL(&kernel_pmap->pm_active);
872ccc4a5c7SNathan Whitehorn 	RB_INIT(&kernel_pmap->pmap_pvo);
873fe3b4685SNathan Whitehorn 
874fe3b4685SNathan Whitehorn  	/*
8753653f5cbSAlan Cox 	 * Initialize the global pv list lock.
8763653f5cbSAlan Cox 	 */
8773653f5cbSAlan Cox 	rw_init(&pvh_global_lock, "pmap pv global");
8783653f5cbSAlan Cox 
8793653f5cbSAlan Cox 	/*
880fe3b4685SNathan Whitehorn 	 * Set up the Open Firmware mappings
881fe3b4685SNathan Whitehorn 	 */
882e347e23bSNathan Whitehorn 	chosen = OF_finddevice("/chosen");
883e347e23bSNathan Whitehorn 	if (chosen != -1 && OF_getprop(chosen, "mmu", &mmui, 4) != -1 &&
884e347e23bSNathan Whitehorn 	    (mmu = OF_instance_to_package(mmui)) != -1 &&
885e347e23bSNathan Whitehorn 	    (sz = OF_getproplen(mmu, "translations")) != -1) {
886aa39961eSBenno Rice 		translations = NULL;
8876cc1cdf4SPeter Grehan 		for (i = 0; phys_avail[i] != 0; i += 2) {
8886cc1cdf4SPeter Grehan 			if (phys_avail[i + 1] >= sz) {
889aa39961eSBenno Rice 				translations = (struct ofw_map *)phys_avail[i];
8906cc1cdf4SPeter Grehan 				break;
8916cc1cdf4SPeter Grehan 			}
892aa39961eSBenno Rice 		}
893aa39961eSBenno Rice 		if (translations == NULL)
89459276937SPeter Grehan 			panic("moea_bootstrap: no space to copy translations");
8955244eac9SBenno Rice 		bzero(translations, sz);
8965244eac9SBenno Rice 		if (OF_getprop(mmu, "translations", translations, sz) == -1)
89759276937SPeter Grehan 			panic("moea_bootstrap: can't get ofw translations");
89859276937SPeter Grehan 		CTR0(KTR_PMAP, "moea_bootstrap: translations");
89931c82d03SBenno Rice 		sz /= sizeof(*translations);
9005244eac9SBenno Rice 		qsort(translations, sz, sizeof (*translations), om_cmp);
901ed1e1e2aSNathan Whitehorn 		for (i = 0; i < sz; i++) {
9025244eac9SBenno Rice 			CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
9035244eac9SBenno Rice 			    translations[i].om_pa, translations[i].om_va,
9045244eac9SBenno Rice 			    translations[i].om_len);
9055244eac9SBenno Rice 
90632bc7846SPeter Grehan 			/*
907e347e23bSNathan Whitehorn 			 * If the mapping is 1:1, let the RAM and device
908e347e23bSNathan Whitehorn 			 * on-demand BAT tables take care of the translation.
90932bc7846SPeter Grehan 			 */
91032bc7846SPeter Grehan 			if (translations[i].om_va == translations[i].om_pa)
91132bc7846SPeter Grehan 				continue;
9125244eac9SBenno Rice 
91332bc7846SPeter Grehan 			/* Enter the pages */
914e347e23bSNathan Whitehorn 			for (off = 0; off < translations[i].om_len;
915e347e23bSNathan Whitehorn 			    off += PAGE_SIZE)
916fe3b4685SNathan Whitehorn 				moea_kenter(mmup, translations[i].om_va + off,
917fe3b4685SNathan Whitehorn 					    translations[i].om_pa + off);
918f9bac91bSBenno Rice 		}
919e347e23bSNathan Whitehorn 	}
920014ffa99SMarcel Moolenaar 
921014ffa99SMarcel Moolenaar 	/*
922014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
923014ffa99SMarcel Moolenaar 	 */
924014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
925014ffa99SMarcel Moolenaar 		;
926014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
9275244eac9SBenno Rice 
9281c96bdd1SNathan Whitehorn 	moea_cpu_bootstrap(mmup,0);
9295244eac9SBenno Rice 
9305244eac9SBenno Rice 	pmap_bootstrapped++;
931014ffa99SMarcel Moolenaar 
932014ffa99SMarcel Moolenaar 	/*
933014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
934014ffa99SMarcel Moolenaar 	 */
935014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
936ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
937014ffa99SMarcel Moolenaar 
938014ffa99SMarcel Moolenaar 	/*
939014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
940014ffa99SMarcel Moolenaar 	 * into the kernel page map.
941014ffa99SMarcel Moolenaar 	 */
942014ffa99SMarcel Moolenaar 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
943014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
944014ffa99SMarcel Moolenaar 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
945014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
946014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
947014ffa99SMarcel Moolenaar 	thread0.td_kstack_pages = KSTACK_PAGES;
948014ffa99SMarcel Moolenaar 	for (i = 0; i < KSTACK_PAGES; i++) {
949c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
950014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
951014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
952014ffa99SMarcel Moolenaar 	}
953014ffa99SMarcel Moolenaar 
954014ffa99SMarcel Moolenaar 	/*
955014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
956014ffa99SMarcel Moolenaar 	 */
9574053b05bSSergey Kandaurov 	pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE);
958014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
959014ffa99SMarcel Moolenaar 	va = virtual_avail;
9604053b05bSSergey Kandaurov 	virtual_avail += round_page(msgbufsize);
961014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
962c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
963014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
964014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
965014ffa99SMarcel Moolenaar 	}
96650c202c5SJeff Roberson 
96750c202c5SJeff Roberson 	/*
96850c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
96950c202c5SJeff Roberson 	 */
97050c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
97150c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
97250c202c5SJeff Roberson 	va = virtual_avail;
97350c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
97450c202c5SJeff Roberson 	while (va < virtual_avail) {
975c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
97650c202c5SJeff Roberson 		pa += PAGE_SIZE;
97750c202c5SJeff Roberson 		va += PAGE_SIZE;
97850c202c5SJeff Roberson 	}
97950c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
9805244eac9SBenno Rice }
9815244eac9SBenno Rice 
9825244eac9SBenno Rice /*
9835244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9845244eac9SBenno Rice  * space can be accessed in any way.
985f9bac91bSBenno Rice  */
986f9bac91bSBenno Rice void
98759276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
988f9bac91bSBenno Rice {
9898207b362SBenno Rice 	pmap_t	pm, pmr;
990f9bac91bSBenno Rice 
991f9bac91bSBenno Rice 	/*
99232bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9935244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
994f9bac91bSBenno Rice 	 */
9955244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
99652a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
9978207b362SBenno Rice 
998c7c2767eSAttilio Rao 	CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
9998207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
1000ac6ba8bdSBenno Rice }
1001ac6ba8bdSBenno Rice 
1002ac6ba8bdSBenno Rice void
100359276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
1004ac6ba8bdSBenno Rice {
1005ac6ba8bdSBenno Rice 	pmap_t	pm;
1006ac6ba8bdSBenno Rice 
1007ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
1008c7c2767eSAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &pm->pm_active);
10098207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
1010f9bac91bSBenno Rice }
1011f9bac91bSBenno Rice 
1012f9bac91bSBenno Rice void
101359276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
1014f9bac91bSBenno Rice {
10150f92104cSBenno Rice 	struct	pvo_entry *pvo;
10160f92104cSBenno Rice 
101748d0b1a0SAlan Cox 	PMAP_LOCK(pm);
101859276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
10190f92104cSBenno Rice 
10200f92104cSBenno Rice 	if (pvo != NULL) {
10210f92104cSBenno Rice 		if (wired) {
10220f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
10230f92104cSBenno Rice 				pm->pm_stats.wired_count++;
10240f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
10250f92104cSBenno Rice 		} else {
10260f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
10270f92104cSBenno Rice 				pm->pm_stats.wired_count--;
10280f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
10290f92104cSBenno Rice 		}
10300f92104cSBenno Rice 	}
103148d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
1032f9bac91bSBenno Rice }
1033f9bac91bSBenno Rice 
1034f9bac91bSBenno Rice void
103559276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
1036f9bac91bSBenno Rice {
103725e2288dSBenno Rice 	vm_offset_t	dst;
103825e2288dSBenno Rice 	vm_offset_t	src;
103925e2288dSBenno Rice 
104025e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
104125e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
104225e2288dSBenno Rice 
1043e3c2930dSNathan Whitehorn 	bcopy((void *)src, (void *)dst, PAGE_SIZE);
1044f9bac91bSBenno Rice }
1045111c77dcSBenno Rice 
1046111c77dcSBenno Rice /*
10475244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
10485244eac9SBenno Rice  */
10495244eac9SBenno Rice void
105059276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
10515244eac9SBenno Rice {
10521a87a0daSPeter Wemm 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10535b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
10545244eac9SBenno Rice 
10555244eac9SBenno Rice 	bzero(va, PAGE_SIZE);
10565244eac9SBenno Rice }
10575244eac9SBenno Rice 
10585244eac9SBenno Rice void
105959276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
10605244eac9SBenno Rice {
10613495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10625b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
10633495845eSBenno Rice 
10645b43c63dSMarcel Moolenaar 	bzero(va, size);
10655244eac9SBenno Rice }
10665244eac9SBenno Rice 
1067a58b3a68SPeter Wemm void
106859276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1069a58b3a68SPeter Wemm {
10705b43c63dSMarcel Moolenaar 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10715b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
1072a58b3a68SPeter Wemm 
10735b43c63dSMarcel Moolenaar 	bzero(va, PAGE_SIZE);
1074a58b3a68SPeter Wemm }
1075a58b3a68SPeter Wemm 
10765244eac9SBenno Rice /*
10775244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
10785244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
10795244eac9SBenno Rice  * will be wired down.
10805244eac9SBenno Rice  */
10815244eac9SBenno Rice void
108259276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
10835244eac9SBenno Rice 	   boolean_t wired)
10845244eac9SBenno Rice {
1085ce142d9eSAlan Cox 
10863653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1087ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
108867c867eeSAlan Cox 	moea_enter_locked(pmap, va, m, prot, wired);
10893653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1090ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1091ce142d9eSAlan Cox }
1092ce142d9eSAlan Cox 
1093ce142d9eSAlan Cox /*
1094ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1095ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1096ce142d9eSAlan Cox  * will be wired down.
1097ce142d9eSAlan Cox  *
1098ce142d9eSAlan Cox  * The page queues and pmap must be locked.
1099ce142d9eSAlan Cox  */
1100ce142d9eSAlan Cox static void
1101ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1102ce142d9eSAlan Cox     boolean_t wired)
1103ce142d9eSAlan Cox {
11045244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1105378862a7SJeff Roberson 	uma_zone_t	zone;
11068207b362SBenno Rice 	vm_page_t	pg;
110757bd5cceSNathan Whitehorn 	u_int		pte_lo, pvo_flags;
11085244eac9SBenno Rice 	int		error;
11095244eac9SBenno Rice 
111059276937SPeter Grehan 	if (!moea_initialized) {
111159276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
111259276937SPeter Grehan 		zone = moea_upvo_zone;
11135244eac9SBenno Rice 		pvo_flags = 0;
11148207b362SBenno Rice 		pg = NULL;
11155244eac9SBenno Rice 	} else {
111603b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
111703b6e025SPeter Grehan 		pg = m;
111859276937SPeter Grehan 		zone = moea_mpvo_zone;
11195244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
11205244eac9SBenno Rice 	}
1121f489bf21SAlan Cox 	if (pmap_bootstrapped)
11223653f5cbSAlan Cox 		rw_assert(&pvh_global_lock, RA_WLOCKED);
1123ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1124d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0 ||
1125d98d0ce2SKonstantin Belousov 	    VM_OBJECT_LOCKED(m->object),
11269ab6032fSAlan Cox 	    ("moea_enter_locked: page %p is not busy", m));
11275244eac9SBenno Rice 
11284dba5df1SPeter Grehan 	/* XXX change the pvo head for fake pages */
1129d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1130a130b35fSNathan Whitehorn 		pvo_flags &= ~PVO_MANAGED;
113159276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
1132a130b35fSNathan Whitehorn 		zone = moea_upvo_zone;
1133a130b35fSNathan Whitehorn 	}
11344dba5df1SPeter Grehan 
1135cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11365244eac9SBenno Rice 
113744b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11385244eac9SBenno Rice 		pte_lo |= PTE_BW;
11392368a371SAlan Cox 		if (pmap_bootstrapped &&
1140d98d0ce2SKonstantin Belousov 		    (m->oflags & VPO_UNMANAGED) == 0)
11413407fefeSKonstantin Belousov 			vm_page_aflag_set(m, PGA_WRITEABLE);
114244b8bd66SAlan Cox 	} else
11435244eac9SBenno Rice 		pte_lo |= PTE_BR;
11445244eac9SBenno Rice 
11454dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
11464dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
11475244eac9SBenno Rice 
11485244eac9SBenno Rice 	if (wired)
11495244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11505244eac9SBenno Rice 
115159276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11528207b362SBenno Rice 	    pte_lo, pvo_flags);
11535244eac9SBenno Rice 
11548207b362SBenno Rice 	/*
115557bd5cceSNathan Whitehorn 	 * Flush the real page from the instruction cache. This has be done
115657bd5cceSNathan Whitehorn 	 * for all user mappings to prevent information leakage via the
1157805bee55SNathan Whitehorn 	 * instruction cache. moea_pvo_enter() returns ENOENT for the first
1158805bee55SNathan Whitehorn 	 * mapping for a page.
11598207b362SBenno Rice 	 */
1160805bee55SNathan Whitehorn 	if (pmap != kernel_pmap && error == ENOENT &&
1161805bee55SNathan Whitehorn 	    (pte_lo & (PTE_I | PTE_G)) == 0)
116259276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1163ce142d9eSAlan Cox }
1164ce142d9eSAlan Cox 
1165ce142d9eSAlan Cox /*
1166ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1167ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1168ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1169ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1170ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1171ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1172ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1173ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1174ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1175ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1176ce142d9eSAlan Cox  */
1177ce142d9eSAlan Cox void
1178ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1179ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1180ce142d9eSAlan Cox {
1181ce142d9eSAlan Cox 	vm_page_t m;
1182ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1183ce142d9eSAlan Cox 
1184ce142d9eSAlan Cox 	psize = atop(end - start);
1185ce142d9eSAlan Cox 	m = m_start;
11863653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1187ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1188ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1189ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1190ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1191ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1192ce142d9eSAlan Cox 	}
11933653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1194ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
11955244eac9SBenno Rice }
11965244eac9SBenno Rice 
11972053c127SStephan Uphoff void
119859276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
11992053c127SStephan Uphoff     vm_prot_t prot)
1200dca96f1aSAlan Cox {
1201dca96f1aSAlan Cox 
12023653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1203ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1204ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
120559276937SPeter Grehan 	    FALSE);
12063653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1207ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1208dca96f1aSAlan Cox }
1209dca96f1aSAlan Cox 
121056b09388SAlan Cox vm_paddr_t
121159276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
12125244eac9SBenno Rice {
12130f92104cSBenno Rice 	struct	pvo_entry *pvo;
121448d0b1a0SAlan Cox 	vm_paddr_t pa;
12150f92104cSBenno Rice 
121648d0b1a0SAlan Cox 	PMAP_LOCK(pm);
121759276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
121848d0b1a0SAlan Cox 	if (pvo == NULL)
121948d0b1a0SAlan Cox 		pa = 0;
122048d0b1a0SAlan Cox 	else
122152a7870dSNathan Whitehorn 		pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
122248d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
122348d0b1a0SAlan Cox 	return (pa);
12245244eac9SBenno Rice }
12255244eac9SBenno Rice 
12265244eac9SBenno Rice /*
122784792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
122884792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
122984792e72SPeter Grehan  * protection.
123084792e72SPeter Grehan  */
123184792e72SPeter Grehan vm_page_t
123259276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
123384792e72SPeter Grehan {
1234ab50a262SAlan Cox 	struct	pvo_entry *pvo;
123584792e72SPeter Grehan 	vm_page_t m;
12362965a453SKip Macy         vm_paddr_t pa;
123784792e72SPeter Grehan 
123884792e72SPeter Grehan 	m = NULL;
12392965a453SKip Macy 	pa = 0;
124048d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
12412965a453SKip Macy retry:
124259276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
124352a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
124452a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1245ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
12462965a453SKip Macy 		if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa))
12472965a453SKip Macy 			goto retry;
124852a7870dSNathan Whitehorn 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
124984792e72SPeter Grehan 		vm_page_hold(m);
125084792e72SPeter Grehan 	}
12512965a453SKip Macy 	PA_UNLOCK_COND(pa);
125248d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
125384792e72SPeter Grehan 	return (m);
125484792e72SPeter Grehan }
125584792e72SPeter Grehan 
12565244eac9SBenno Rice void
125759276937SPeter Grehan moea_init(mmu_t mmu)
12585244eac9SBenno Rice {
12595244eac9SBenno Rice 
126059276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12610ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12620ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
126359276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12640ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12650ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
126659276937SPeter Grehan 	moea_initialized = TRUE;
12675244eac9SBenno Rice }
12685244eac9SBenno Rice 
12695244eac9SBenno Rice boolean_t
12707b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m)
12717b85f591SAlan Cox {
1272*8d9e6d9fSAlan Cox 	boolean_t rv;
12737b85f591SAlan Cox 
1274d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1275c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
1276*8d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1277*8d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_REF);
1278*8d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
1279*8d9e6d9fSAlan Cox 	return (rv);
12807b85f591SAlan Cox }
12817b85f591SAlan Cox 
12827b85f591SAlan Cox boolean_t
128359276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
12845244eac9SBenno Rice {
1285*8d9e6d9fSAlan Cox 	boolean_t rv;
12860f92104cSBenno Rice 
1287d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1288567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1289567e51e1SAlan Cox 
1290567e51e1SAlan Cox 	/*
12913407fefeSKonstantin Belousov 	 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be
12923407fefeSKonstantin Belousov 	 * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
1293567e51e1SAlan Cox 	 * is clear, no PTEs can have PTE_CHG set.
1294567e51e1SAlan Cox 	 */
1295567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1296567e51e1SAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
12973407fefeSKonstantin Belousov 	    (m->aflags & PGA_WRITEABLE) == 0)
12980f92104cSBenno Rice 		return (FALSE);
1299*8d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1300*8d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_CHG);
1301*8d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
1302*8d9e6d9fSAlan Cox 	return (rv);
1303566526a9SAlan Cox }
1304566526a9SAlan Cox 
1305e396eb60SAlan Cox boolean_t
1306e396eb60SAlan Cox moea_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1307e396eb60SAlan Cox {
1308e396eb60SAlan Cox 	struct pvo_entry *pvo;
1309e396eb60SAlan Cox 	boolean_t rv;
1310e396eb60SAlan Cox 
1311e396eb60SAlan Cox 	PMAP_LOCK(pmap);
1312e396eb60SAlan Cox 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1313e396eb60SAlan Cox 	rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0;
1314e396eb60SAlan Cox 	PMAP_UNLOCK(pmap);
1315e396eb60SAlan Cox 	return (rv);
1316e396eb60SAlan Cox }
1317e396eb60SAlan Cox 
13185244eac9SBenno Rice void
131959276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m)
13205244eac9SBenno Rice {
132103b6e025SPeter Grehan 
1322d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1323567e51e1SAlan Cox 	    ("moea_clear_reference: page %p is not managed", m));
1324*8d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1325ce186587SAlan Cox 	moea_clear_bit(m, PTE_REF);
1326*8d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
132703b6e025SPeter Grehan }
132803b6e025SPeter Grehan 
132903b6e025SPeter Grehan void
133059276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
133103b6e025SPeter Grehan {
133203b6e025SPeter Grehan 
1333d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1334567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
1335567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1336567e51e1SAlan Cox 	KASSERT((m->oflags & VPO_BUSY) == 0,
1337567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is busy", m));
1338567e51e1SAlan Cox 
1339567e51e1SAlan Cox 	/*
13403407fefeSKonstantin Belousov 	 * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_CHG
1341567e51e1SAlan Cox 	 * set.  If the object containing the page is locked and the page is
13423407fefeSKonstantin Belousov 	 * not VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set.
1343567e51e1SAlan Cox 	 */
13443407fefeSKonstantin Belousov 	if ((m->aflags & PGA_WRITEABLE) == 0)
134503b6e025SPeter Grehan 		return;
1346*8d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1347ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
1348*8d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13495244eac9SBenno Rice }
13505244eac9SBenno Rice 
13517f3a4093SMike Silbersack /*
135278985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
135378985e42SAlan Cox  */
135478985e42SAlan Cox void
135578985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
135678985e42SAlan Cox {
135778985e42SAlan Cox 	struct	pvo_entry *pvo;
135878985e42SAlan Cox 	struct	pte *pt;
135978985e42SAlan Cox 	pmap_t	pmap;
136078985e42SAlan Cox 	u_int	lo;
136178985e42SAlan Cox 
1362d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
13639ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
13649ab6032fSAlan Cox 
13659ab6032fSAlan Cox 	/*
13663407fefeSKonstantin Belousov 	 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by
13673407fefeSKonstantin Belousov 	 * another thread while the object is locked.  Thus, if PGA_WRITEABLE
13689ab6032fSAlan Cox 	 * is clear, no page table entries need updating.
13699ab6032fSAlan Cox 	 */
13709ab6032fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
13719ab6032fSAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
13723407fefeSKonstantin Belousov 	    (m->aflags & PGA_WRITEABLE) == 0)
137378985e42SAlan Cox 		return;
13743653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
137578985e42SAlan Cox 	lo = moea_attr_fetch(m);
1376e4f72b32SMarcel Moolenaar 	powerpc_sync();
137778985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
137878985e42SAlan Cox 		pmap = pvo->pvo_pmap;
137978985e42SAlan Cox 		PMAP_LOCK(pmap);
138052a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
138178985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
138252a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
138352a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
138478985e42SAlan Cox 			if (pt != NULL) {
138552a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
138652a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
138752a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
138852a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
138978985e42SAlan Cox 				    pvo->pvo_vaddr);
139078985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
139178985e42SAlan Cox 			}
139278985e42SAlan Cox 		}
139378985e42SAlan Cox 		PMAP_UNLOCK(pmap);
139478985e42SAlan Cox 	}
139578985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
139678985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
139778985e42SAlan Cox 		vm_page_dirty(m);
139878985e42SAlan Cox 	}
13993407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
14003653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
140178985e42SAlan Cox }
140278985e42SAlan Cox 
140378985e42SAlan Cox /*
140459276937SPeter Grehan  *	moea_ts_referenced:
14057f3a4093SMike Silbersack  *
14067f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
14077f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
14087f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
14097f3a4093SMike Silbersack  *	reference bits set.
14107f3a4093SMike Silbersack  *
14117f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
14127f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
14137f3a4093SMike Silbersack  *	optimal aging of shared pages.
14147f3a4093SMike Silbersack  */
1415*8d9e6d9fSAlan Cox int
141659276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
14175244eac9SBenno Rice {
1418*8d9e6d9fSAlan Cox 	int count;
141903b6e025SPeter Grehan 
1420d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1421ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
1422*8d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1423*8d9e6d9fSAlan Cox 	count = moea_clear_bit(m, PTE_REF);
1424*8d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
1425*8d9e6d9fSAlan Cox 	return (count);
14265244eac9SBenno Rice }
14275244eac9SBenno Rice 
14285244eac9SBenno Rice /*
1429c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1430c1f4123bSNathan Whitehorn  */
1431c1f4123bSNathan Whitehorn void
1432c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma)
1433c1f4123bSNathan Whitehorn {
1434c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1435cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1436c1f4123bSNathan Whitehorn 	struct	pte *pt;
1437c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1438c1f4123bSNathan Whitehorn 	u_int	lo;
1439c1f4123bSNathan Whitehorn 
1440d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1441cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1442cd6a97f0SNathan Whitehorn 		return;
1443cd6a97f0SNathan Whitehorn 	}
1444cd6a97f0SNathan Whitehorn 
14453653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1446cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1447c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1448cd6a97f0SNathan Whitehorn 
1449cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1450c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1451c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1452c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1453c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1454c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1455c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1456c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1457c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1458c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1459c1f4123bSNathan Whitehorn 				isync();
1460c1f4123bSNathan Whitehorn 		}
1461c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1462c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1463c1f4123bSNathan Whitehorn 	}
1464c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
14653653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1466c1f4123bSNathan Whitehorn }
1467c1f4123bSNathan Whitehorn 
1468c1f4123bSNathan Whitehorn /*
14695244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
14705244eac9SBenno Rice  */
14715244eac9SBenno Rice void
147220b79612SRafal Jaworowski moea_kenter(mmu_t mmu, vm_offset_t va, vm_paddr_t pa)
14735244eac9SBenno Rice {
1474c1f4123bSNathan Whitehorn 
1475c1f4123bSNathan Whitehorn 	moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1476c1f4123bSNathan Whitehorn }
1477c1f4123bSNathan Whitehorn 
1478c1f4123bSNathan Whitehorn void
1479c1f4123bSNathan Whitehorn moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma)
1480c1f4123bSNathan Whitehorn {
14815244eac9SBenno Rice 	u_int		pte_lo;
14825244eac9SBenno Rice 	int		error;
14835244eac9SBenno Rice 
14845244eac9SBenno Rice #if 0
14855244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
148659276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
14875244eac9SBenno Rice 		    va);
14885244eac9SBenno Rice #endif
14895244eac9SBenno Rice 
1490c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
14915244eac9SBenno Rice 
14924711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
149359276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
149459276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
14955244eac9SBenno Rice 
14965244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
149759276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
14985244eac9SBenno Rice 		    pa, error);
14995244eac9SBenno Rice 
15004711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
15015244eac9SBenno Rice }
15025244eac9SBenno Rice 
1503e79f59e8SBenno Rice /*
1504e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1505e79f59e8SBenno Rice  * address.
1506e79f59e8SBenno Rice  */
150720b79612SRafal Jaworowski vm_paddr_t
150859276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
15095244eac9SBenno Rice {
1510e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
151148d0b1a0SAlan Cox 	vm_paddr_t pa;
1512e79f59e8SBenno Rice 
15130efd0097SPeter Grehan 	/*
151452a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
15150efd0097SPeter Grehan 	 */
15160efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
15170efd0097SPeter Grehan 		return (va);
15180efd0097SPeter Grehan 	}
15190efd0097SPeter Grehan 
152048d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
152159276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
152259276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
152352a7870dSNathan Whitehorn 	pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
152448d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
152548d0b1a0SAlan Cox 	return (pa);
1526e79f59e8SBenno Rice }
1527e79f59e8SBenno Rice 
152888afb2a3SBenno Rice /*
152988afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
153088afb2a3SBenno Rice  */
15315244eac9SBenno Rice void
153259276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
15335244eac9SBenno Rice {
153488afb2a3SBenno Rice 
153559276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
15365244eac9SBenno Rice }
15375244eac9SBenno Rice 
15385244eac9SBenno Rice /*
15395244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
15405244eac9SBenno Rice  *
15415244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
15425244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
15435244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
15445244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
15455244eac9SBenno Rice  * first usable address after the mapped region.
15465244eac9SBenno Rice  */
15475244eac9SBenno Rice vm_offset_t
154820b79612SRafal Jaworowski moea_map(mmu_t mmu, vm_offset_t *virt, vm_paddr_t pa_start,
154920b79612SRafal Jaworowski     vm_paddr_t pa_end, int prot)
15505244eac9SBenno Rice {
15515244eac9SBenno Rice 	vm_offset_t	sva, va;
15525244eac9SBenno Rice 
15535244eac9SBenno Rice 	sva = *virt;
15545244eac9SBenno Rice 	va = sva;
15555244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
155659276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
15575244eac9SBenno Rice 	*virt = va;
15585244eac9SBenno Rice 	return (sva);
15595244eac9SBenno Rice }
15605244eac9SBenno Rice 
15615244eac9SBenno Rice /*
15627f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
15637f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
15647f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
15657f3a4093SMike Silbersack  * is only necessary that true be returned for a small
15667f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
15677f3a4093SMike Silbersack  */
15685244eac9SBenno Rice boolean_t
156959276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
15705244eac9SBenno Rice {
157103b6e025SPeter Grehan         int loops;
157203b6e025SPeter Grehan 	struct pvo_entry *pvo;
1573ce186587SAlan Cox 	boolean_t rv;
157403b6e025SPeter Grehan 
1575d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1576ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
157703b6e025SPeter Grehan 	loops = 0;
1578ce186587SAlan Cox 	rv = FALSE;
15793653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
158003b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1581ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1582ce186587SAlan Cox 			rv = TRUE;
1583ce186587SAlan Cox 			break;
1584ce186587SAlan Cox 		}
158503b6e025SPeter Grehan 		if (++loops >= 16)
158603b6e025SPeter Grehan 			break;
158703b6e025SPeter Grehan 	}
15883653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1589ce186587SAlan Cox 	return (rv);
15905244eac9SBenno Rice }
15915244eac9SBenno Rice 
159259677d3cSAlan Cox /*
159359677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
159459677d3cSAlan Cox  * that are wired.
159559677d3cSAlan Cox  */
159659677d3cSAlan Cox int
159759677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
159859677d3cSAlan Cox {
159959677d3cSAlan Cox 	struct pvo_entry *pvo;
160059677d3cSAlan Cox 	int count;
160159677d3cSAlan Cox 
160259677d3cSAlan Cox 	count = 0;
1603d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0)
160459677d3cSAlan Cox 		return (count);
16053653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
160659677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
160759677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
160859677d3cSAlan Cox 			count++;
16093653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
161059677d3cSAlan Cox 	return (count);
161159677d3cSAlan Cox }
161259677d3cSAlan Cox 
161359276937SPeter Grehan static u_int	moea_vsidcontext;
16145244eac9SBenno Rice 
16155244eac9SBenno Rice void
161659276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
16175244eac9SBenno Rice {
16185244eac9SBenno Rice 	int	i, mask;
16195244eac9SBenno Rice 	u_int	entropy;
16205244eac9SBenno Rice 
162159276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
162248d0b1a0SAlan Cox 	PMAP_LOCK_INIT(pmap);
1623ccc4a5c7SNathan Whitehorn 	RB_INIT(&pmap->pmap_pvo);
16244daf20b2SPeter Grehan 
16255244eac9SBenno Rice 	entropy = 0;
16265244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
16275244eac9SBenno Rice 
162852a7870dSNathan Whitehorn 	if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
162952a7870dSNathan Whitehorn 	    == NULL) {
163052a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
163152a7870dSNathan Whitehorn 	}
163252a7870dSNathan Whitehorn 
163352a7870dSNathan Whitehorn 
1634e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
16355244eac9SBenno Rice 	/*
16365244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
16375244eac9SBenno Rice 	 */
16385244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
16395244eac9SBenno Rice 		u_int	hash, n;
16405244eac9SBenno Rice 
16415244eac9SBenno Rice 		/*
16425244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
16435244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
16445244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
16455244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
16465244eac9SBenno Rice 		 * instead of a multiply.)
16475244eac9SBenno Rice 		 */
164859276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
164959276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
16505244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
16515244eac9SBenno Rice 			continue;
16525244eac9SBenno Rice 		n = hash >> 5;
16535244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
165459276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
165559276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
16565244eac9SBenno Rice 			/* anything free in this bucket? */
165759276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
165859276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
16595244eac9SBenno Rice 				continue;
16605244eac9SBenno Rice 			}
16610dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
16625244eac9SBenno Rice 			mask = 1 << i;
16635244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
16645244eac9SBenno Rice 			hash |= i;
16655244eac9SBenno Rice 		}
166646e93cbbSNathan Whitehorn 		KASSERT(!(moea_vsid_bitmap[n] & mask),
166746e93cbbSNathan Whitehorn 		    ("Allocating in-use VSID group %#x\n", hash));
166859276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
16695244eac9SBenno Rice 		for (i = 0; i < 16; i++)
16705244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1671e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
16725244eac9SBenno Rice 		return;
16735244eac9SBenno Rice 	}
16745244eac9SBenno Rice 
1675e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
167659276937SPeter Grehan 	panic("moea_pinit: out of segments");
16775244eac9SBenno Rice }
16785244eac9SBenno Rice 
16795244eac9SBenno Rice /*
16805244eac9SBenno Rice  * Initialize the pmap associated with process 0.
16815244eac9SBenno Rice  */
16825244eac9SBenno Rice void
168359276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
16845244eac9SBenno Rice {
16855244eac9SBenno Rice 
168659276937SPeter Grehan 	moea_pinit(mmu, pm);
16875244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
16885244eac9SBenno Rice }
16895244eac9SBenno Rice 
1690e79f59e8SBenno Rice /*
1691e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1692e79f59e8SBenno Rice  */
16935244eac9SBenno Rice void
169459276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
169559276937SPeter Grehan     vm_prot_t prot)
16965244eac9SBenno Rice {
1697ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
1698e79f59e8SBenno Rice 	struct	pte *pt;
1699e79f59e8SBenno Rice 
1700e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
170159276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1702e79f59e8SBenno Rice 
1703e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
170459276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1705e79f59e8SBenno Rice 		return;
1706e79f59e8SBenno Rice 	}
1707e79f59e8SBenno Rice 
17083653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
170948d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1710ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1711ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1712ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1713ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1714e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1715e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1716e79f59e8SBenno Rice 
1717e79f59e8SBenno Rice 		/*
1718e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1719e79f59e8SBenno Rice 		 * copy.
1720e79f59e8SBenno Rice 		 */
1721ccc4a5c7SNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1722e79f59e8SBenno Rice 		/*
1723e79f59e8SBenno Rice 		 * Change the protection of the page.
1724e79f59e8SBenno Rice 		 */
172552a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
172652a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1727e79f59e8SBenno Rice 
1728e79f59e8SBenno Rice 		/*
1729e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1730e79f59e8SBenno Rice 		 */
1731d644a0b7SAlan Cox 		if (pt != NULL) {
173252a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1733d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1734d644a0b7SAlan Cox 		}
1735e79f59e8SBenno Rice 	}
17363653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
173748d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
17385244eac9SBenno Rice }
17395244eac9SBenno Rice 
174088afb2a3SBenno Rice /*
174188afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
174288afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
174388afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
174488afb2a3SBenno Rice  */
17455244eac9SBenno Rice void
174659276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
17475244eac9SBenno Rice {
174803b6e025SPeter Grehan 	vm_offset_t va;
17495244eac9SBenno Rice 
175003b6e025SPeter Grehan 	va = sva;
175103b6e025SPeter Grehan 	while (count-- > 0) {
175259276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
175303b6e025SPeter Grehan 		va += PAGE_SIZE;
175403b6e025SPeter Grehan 		m++;
175503b6e025SPeter Grehan 	}
17565244eac9SBenno Rice }
17575244eac9SBenno Rice 
175888afb2a3SBenno Rice /*
175988afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
176059276937SPeter Grehan  * temporary mappings entered by moea_qenter.
176188afb2a3SBenno Rice  */
17625244eac9SBenno Rice void
176359276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
17645244eac9SBenno Rice {
176503b6e025SPeter Grehan 	vm_offset_t va;
176688afb2a3SBenno Rice 
176703b6e025SPeter Grehan 	va = sva;
176803b6e025SPeter Grehan 	while (count-- > 0) {
176959276937SPeter Grehan 		moea_kremove(mmu, va);
177003b6e025SPeter Grehan 		va += PAGE_SIZE;
177103b6e025SPeter Grehan 	}
17725244eac9SBenno Rice }
17735244eac9SBenno Rice 
17745244eac9SBenno Rice void
177559276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
17765244eac9SBenno Rice {
177732bc7846SPeter Grehan         int idx, mask;
177832bc7846SPeter Grehan 
177932bc7846SPeter Grehan 	/*
178032bc7846SPeter Grehan 	 * Free segment register's VSID
178132bc7846SPeter Grehan 	 */
178232bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
178359276937SPeter Grehan                 panic("moea_release");
178432bc7846SPeter Grehan 
1785e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
178632bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
178732bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
178832bc7846SPeter Grehan         idx /= VSID_NBPW;
178959276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1790e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
179148d0b1a0SAlan Cox 	PMAP_LOCK_DESTROY(pmap);
17925244eac9SBenno Rice }
17935244eac9SBenno Rice 
179488afb2a3SBenno Rice /*
179588afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
179688afb2a3SBenno Rice  */
17975244eac9SBenno Rice void
179859276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
17995244eac9SBenno Rice {
1800ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
180188afb2a3SBenno Rice 
18023653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
180348d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1804ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1805ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1806ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1807ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1808598d99ddSNathan Whitehorn 		moea_pvo_remove(pvo, -1);
1809598d99ddSNathan Whitehorn 	}
181048d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
18113653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
18125244eac9SBenno Rice }
18135244eac9SBenno Rice 
1814e79f59e8SBenno Rice /*
181559276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
181603b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
181703b6e025SPeter Grehan  */
181803b6e025SPeter Grehan void
181959276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
182003b6e025SPeter Grehan {
182103b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
182203b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
182348d0b1a0SAlan Cox 	pmap_t	pmap;
182403b6e025SPeter Grehan 
18253653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
182603b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
182703b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
182803b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
182903b6e025SPeter Grehan 
183048d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
183148d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
183259276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
183348d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
183403b6e025SPeter Grehan 	}
1835*8d9e6d9fSAlan Cox 	if ((m->aflags & PGA_WRITEABLE) && moea_query_bit(m, PTE_CHG)) {
1836c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1837062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1838062c8f4cSNathan Whitehorn 	}
18393407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
18403653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
184103b6e025SPeter Grehan }
184203b6e025SPeter Grehan 
184303b6e025SPeter Grehan /*
18445244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
184559276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
18465244eac9SBenno Rice  * calculated.
18475244eac9SBenno Rice  */
18485244eac9SBenno Rice static vm_offset_t
184959276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
18505244eac9SBenno Rice {
18515244eac9SBenno Rice 	vm_offset_t	s, e;
18525244eac9SBenno Rice 	int		i, j;
18535244eac9SBenno Rice 
18545244eac9SBenno Rice 	size = round_page(size);
18555244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
18565244eac9SBenno Rice 		if (align != 0)
18575244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
18585244eac9SBenno Rice 		else
18595244eac9SBenno Rice 			s = phys_avail[i];
18605244eac9SBenno Rice 		e = s + size;
18615244eac9SBenno Rice 
18625244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
18635244eac9SBenno Rice 			continue;
18645244eac9SBenno Rice 
18655244eac9SBenno Rice 		if (s == phys_avail[i]) {
18665244eac9SBenno Rice 			phys_avail[i] += size;
18675244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
18685244eac9SBenno Rice 			phys_avail[i + 1] -= size;
18695244eac9SBenno Rice 		} else {
18705244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
18715244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
18725244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
18735244eac9SBenno Rice 			}
18745244eac9SBenno Rice 
18755244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
18765244eac9SBenno Rice 			phys_avail[i + 1] = s;
18775244eac9SBenno Rice 			phys_avail[i + 2] = e;
18785244eac9SBenno Rice 			phys_avail_count++;
18795244eac9SBenno Rice 		}
18805244eac9SBenno Rice 
18815244eac9SBenno Rice 		return (s);
18825244eac9SBenno Rice 	}
188359276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
18845244eac9SBenno Rice }
18855244eac9SBenno Rice 
18865244eac9SBenno Rice static void
188759276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
18885244eac9SBenno Rice {
18895244eac9SBenno Rice 	__syncicache((void *)pa, len);
18905244eac9SBenno Rice }
18915244eac9SBenno Rice 
18925244eac9SBenno Rice static int
189359276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
18945244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
18955244eac9SBenno Rice {
18965244eac9SBenno Rice 	struct	pvo_entry *pvo;
18975244eac9SBenno Rice 	u_int	sr;
18985244eac9SBenno Rice 	int	first;
18995244eac9SBenno Rice 	u_int	ptegidx;
19005244eac9SBenno Rice 	int	i;
190132bc7846SPeter Grehan 	int     bootstrap;
19025244eac9SBenno Rice 
190359276937SPeter Grehan 	moea_pvo_enter_calls++;
19048207b362SBenno Rice 	first = 0;
190532bc7846SPeter Grehan 	bootstrap = 0;
190632bc7846SPeter Grehan 
19075244eac9SBenno Rice 	/*
19085244eac9SBenno Rice 	 * Compute the PTE Group index.
19095244eac9SBenno Rice 	 */
19105244eac9SBenno Rice 	va &= ~ADDR_POFF;
19115244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
19125244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
19135244eac9SBenno Rice 
19145244eac9SBenno Rice 	/*
19155244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
19165244eac9SBenno Rice 	 * there is a mapping.
19175244eac9SBenno Rice 	 */
191859276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
191959276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19205244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
192152a7870dSNathan Whitehorn 			if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
192252a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1923fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
192459276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
192549f8f727SBenno Rice 				return (0);
1926fafc7362SBenno Rice 			}
192759276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
19285244eac9SBenno Rice 			break;
19295244eac9SBenno Rice 		}
19305244eac9SBenno Rice 	}
19315244eac9SBenno Rice 
19325244eac9SBenno Rice 	/*
19335244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
19345244eac9SBenno Rice 	 */
193559276937SPeter Grehan 	if (moea_initialized) {
1936378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
193749f8f727SBenno Rice 	} else {
193859276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
193959276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
194059276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
19410d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
194249f8f727SBenno Rice 		}
194359276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
194459276937SPeter Grehan 		moea_bpvo_pool_index++;
194532bc7846SPeter Grehan 		bootstrap = 1;
194649f8f727SBenno Rice 	}
19475244eac9SBenno Rice 
19485244eac9SBenno Rice 	if (pvo == NULL) {
194959276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
19505244eac9SBenno Rice 		return (ENOMEM);
19515244eac9SBenno Rice 	}
19525244eac9SBenno Rice 
195359276937SPeter Grehan 	moea_pvo_entries++;
19545244eac9SBenno Rice 	pvo->pvo_vaddr = va;
19555244eac9SBenno Rice 	pvo->pvo_pmap = pm;
195659276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
19575244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
19585244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
19595244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
19605244eac9SBenno Rice 	if (flags & PVO_WIRED)
19615244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
196259276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
19635244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
196432bc7846SPeter Grehan 	if (bootstrap)
196532bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
19664dba5df1SPeter Grehan 
196752a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
19685244eac9SBenno Rice 
19695244eac9SBenno Rice 	/*
1970598d99ddSNathan Whitehorn 	 * Add to pmap list
1971598d99ddSNathan Whitehorn 	 */
1972ccc4a5c7SNathan Whitehorn 	RB_INSERT(pvo_tree, &pm->pmap_pvo, pvo);
1973598d99ddSNathan Whitehorn 
1974598d99ddSNathan Whitehorn 	/*
19755244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
19765244eac9SBenno Rice 	 * item.
19775244eac9SBenno Rice 	 */
19788207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
19798207b362SBenno Rice 		first = 1;
19805244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
19814dba5df1SPeter Grehan 
198252a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
1983c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
1984c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
19855244eac9SBenno Rice 
19865244eac9SBenno Rice 	/*
19875244eac9SBenno Rice 	 * We hope this succeeds but it isn't required.
19885244eac9SBenno Rice 	 */
198952a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
19905244eac9SBenno Rice 	if (i >= 0) {
19915244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
19925244eac9SBenno Rice 	} else {
199359276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
199459276937SPeter Grehan 		moea_pte_overflow++;
19955244eac9SBenno Rice 	}
199659276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
19974dba5df1SPeter Grehan 
19985244eac9SBenno Rice 	return (first ? ENOENT : 0);
19995244eac9SBenno Rice }
20005244eac9SBenno Rice 
20015244eac9SBenno Rice static void
200259276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
20035244eac9SBenno Rice {
20045244eac9SBenno Rice 	struct	pte *pt;
20055244eac9SBenno Rice 
20065244eac9SBenno Rice 	/*
20075244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
20085244eac9SBenno Rice 	 * save the ref & cfg bits).
20095244eac9SBenno Rice 	 */
201059276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
20115244eac9SBenno Rice 	if (pt != NULL) {
201252a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
2013d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
20145244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
20155244eac9SBenno Rice 	} else {
201659276937SPeter Grehan 		moea_pte_overflow--;
20175244eac9SBenno Rice 	}
20185244eac9SBenno Rice 
20195244eac9SBenno Rice 	/*
20205244eac9SBenno Rice 	 * Update our statistics.
20215244eac9SBenno Rice 	 */
20225244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
202352a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
20245244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
20255244eac9SBenno Rice 
20265244eac9SBenno Rice 	/*
20275244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
20285244eac9SBenno Rice 	 */
2029d98d0ce2SKonstantin Belousov 	if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED) {
20305244eac9SBenno Rice 		struct	vm_page *pg;
20315244eac9SBenno Rice 
203252a7870dSNathan Whitehorn 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
20335244eac9SBenno Rice 		if (pg != NULL) {
203452a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
20355244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
20365244eac9SBenno Rice 		}
20375244eac9SBenno Rice 	}
20385244eac9SBenno Rice 
20395244eac9SBenno Rice 	/*
2040598d99ddSNathan Whitehorn 	 * Remove this PVO from the PV and pmap lists.
20415244eac9SBenno Rice 	 */
20425244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
2043ccc4a5c7SNathan Whitehorn 	RB_REMOVE(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo);
20445244eac9SBenno Rice 
20455244eac9SBenno Rice 	/*
20465244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
20475244eac9SBenno Rice 	 * if we aren't going to reuse it.
20485244eac9SBenno Rice 	 */
20495244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
205049f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
205159276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
205259276937SPeter Grehan 		    moea_upvo_zone, pvo);
205359276937SPeter Grehan 	moea_pvo_entries--;
205459276937SPeter Grehan 	moea_pvo_remove_calls++;
20555244eac9SBenno Rice }
20565244eac9SBenno Rice 
20575244eac9SBenno Rice static __inline int
205859276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
20595244eac9SBenno Rice {
20605244eac9SBenno Rice 	int	pteidx;
20615244eac9SBenno Rice 
20625244eac9SBenno Rice 	/*
20635244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
20645244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
20655244eac9SBenno Rice 	 * noticing the HID bit.
20665244eac9SBenno Rice 	 */
20675244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
206852a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
206959276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
20705244eac9SBenno Rice 
20715244eac9SBenno Rice 	return (pteidx);
20725244eac9SBenno Rice }
20735244eac9SBenno Rice 
20745244eac9SBenno Rice static struct pvo_entry *
207559276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
20765244eac9SBenno Rice {
20775244eac9SBenno Rice 	struct	pvo_entry *pvo;
20785244eac9SBenno Rice 	int	ptegidx;
20795244eac9SBenno Rice 	u_int	sr;
20805244eac9SBenno Rice 
20815244eac9SBenno Rice 	va &= ~ADDR_POFF;
20825244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
20835244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
20845244eac9SBenno Rice 
208559276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
208659276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20875244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
20885244eac9SBenno Rice 			if (pteidx_p)
208959276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2090f489bf21SAlan Cox 			break;
20915244eac9SBenno Rice 		}
20925244eac9SBenno Rice 	}
209359276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20945244eac9SBenno Rice 
2095f489bf21SAlan Cox 	return (pvo);
20965244eac9SBenno Rice }
20975244eac9SBenno Rice 
20985244eac9SBenno Rice static struct pte *
209959276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
21005244eac9SBenno Rice {
21015244eac9SBenno Rice 	struct	pte *pt;
21025244eac9SBenno Rice 
21035244eac9SBenno Rice 	/*
21045244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
21055244eac9SBenno Rice 	 */
21065244eac9SBenno Rice 	if (pteidx == -1) {
21075244eac9SBenno Rice 		int	ptegidx;
21085244eac9SBenno Rice 		u_int	sr;
21095244eac9SBenno Rice 
21105244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
21115244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
211259276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
21135244eac9SBenno Rice 	}
21145244eac9SBenno Rice 
211559276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2116d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
21175244eac9SBenno Rice 
211852a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
211959276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
21205244eac9SBenno Rice 		    "valid pte index", pvo);
21215244eac9SBenno Rice 	}
21225244eac9SBenno Rice 
212352a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
212459276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
21255244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
21265244eac9SBenno Rice 	}
21275244eac9SBenno Rice 
212852a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
212952a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
213059276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
213159276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
21325244eac9SBenno Rice 		}
21335244eac9SBenno Rice 
213452a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
21355244eac9SBenno Rice 		    != 0) {
213659276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
213759276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
21385244eac9SBenno Rice 		}
21395244eac9SBenno Rice 
2140d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
21415244eac9SBenno Rice 		return (pt);
21425244eac9SBenno Rice 	}
21435244eac9SBenno Rice 
214452a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
214559276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
214659276937SPeter Grehan 		    "moea_pteg_table but valid in pvo", pvo, pt);
21475244eac9SBenno Rice 	}
21485244eac9SBenno Rice 
2149d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
21505244eac9SBenno Rice 	return (NULL);
21515244eac9SBenno Rice }
21525244eac9SBenno Rice 
21535244eac9SBenno Rice /*
21545244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
21555244eac9SBenno Rice  */
21565244eac9SBenno Rice int
215759276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
21585244eac9SBenno Rice {
21595244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
21605244eac9SBenno Rice 	struct	pvo_entry *pvo;
21615244eac9SBenno Rice 	int	ptegidx, i, j;
21625244eac9SBenno Rice 	u_int	sr;
21635244eac9SBenno Rice 	struct	pteg *pteg;
21645244eac9SBenno Rice 	struct	pte *pt;
21655244eac9SBenno Rice 
216659276937SPeter Grehan 	moea_pte_spills++;
21675244eac9SBenno Rice 
2168d080d5fdSBenno Rice 	sr = mfsrin(addr);
21695244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
21705244eac9SBenno Rice 
21715244eac9SBenno Rice 	/*
21725244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
21735244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
21745244eac9SBenno Rice 	 */
217559276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
217659276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
21775244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
21785244eac9SBenno Rice 	i &= 7;
21795244eac9SBenno Rice 	pt = &pteg->pt[i];
21805244eac9SBenno Rice 
21815244eac9SBenno Rice 	source_pvo = NULL;
21825244eac9SBenno Rice 	victim_pvo = NULL;
218359276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21845244eac9SBenno Rice 		/*
21855244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
21865244eac9SBenno Rice 		 */
21875244eac9SBenno Rice 		if (source_pvo == NULL &&
218852a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
218952a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
21905244eac9SBenno Rice 			/*
21915244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
21925244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
21935244eac9SBenno Rice 			 */
219452a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
21955244eac9SBenno Rice 
21965244eac9SBenno Rice 			if (j >= 0) {
21975244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
219859276937SPeter Grehan 				moea_pte_overflow--;
219959276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
22005244eac9SBenno Rice 				return (1);
22015244eac9SBenno Rice 			}
22025244eac9SBenno Rice 
22035244eac9SBenno Rice 			source_pvo = pvo;
22045244eac9SBenno Rice 
22055244eac9SBenno Rice 			if (victim_pvo != NULL)
22065244eac9SBenno Rice 				break;
22075244eac9SBenno Rice 		}
22085244eac9SBenno Rice 
22095244eac9SBenno Rice 		/*
22105244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
22115244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
22125244eac9SBenno Rice 		 */
22135244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
221452a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22155244eac9SBenno Rice 			victim_pvo = pvo;
22165244eac9SBenno Rice 			if (source_pvo != NULL)
22175244eac9SBenno Rice 				break;
22185244eac9SBenno Rice 		}
22195244eac9SBenno Rice 	}
22205244eac9SBenno Rice 
2221f489bf21SAlan Cox 	if (source_pvo == NULL) {
222259276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
22235244eac9SBenno Rice 		return (0);
2224f489bf21SAlan Cox 	}
22255244eac9SBenno Rice 
22265244eac9SBenno Rice 	if (victim_pvo == NULL) {
22275244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
222859276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
22295244eac9SBenno Rice 			    "entry", pt);
22305244eac9SBenno Rice 
22315244eac9SBenno Rice 		/*
22325244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
22335244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
22345244eac9SBenno Rice 		 */
223559276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
22365244eac9SBenno Rice 		    pvo_olink) {
22375244eac9SBenno Rice 			/*
22385244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
22395244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
22405244eac9SBenno Rice 			 */
224152a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22425244eac9SBenno Rice 				victim_pvo = pvo;
22435244eac9SBenno Rice 				break;
22445244eac9SBenno Rice 			}
22455244eac9SBenno Rice 		}
22465244eac9SBenno Rice 
22475244eac9SBenno Rice 		if (victim_pvo == NULL)
224859276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
22495244eac9SBenno Rice 			    "entry", pt);
22505244eac9SBenno Rice 	}
22515244eac9SBenno Rice 
22525244eac9SBenno Rice 	/*
22535244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
22545244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
22555244eac9SBenno Rice 	 * contained in the TLB entry.
22565244eac9SBenno Rice 	 */
225752a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
22585244eac9SBenno Rice 
225952a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
226052a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
22615244eac9SBenno Rice 
22625244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
22635244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
226459276937SPeter Grehan 	moea_pte_replacements++;
22655244eac9SBenno Rice 
226659276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22675244eac9SBenno Rice 	return (1);
22685244eac9SBenno Rice }
22695244eac9SBenno Rice 
22705244eac9SBenno Rice static int
227159276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
22725244eac9SBenno Rice {
22735244eac9SBenno Rice 	struct	pte *pt;
22745244eac9SBenno Rice 	int	i;
22755244eac9SBenno Rice 
2276d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2277d644a0b7SAlan Cox 
22785244eac9SBenno Rice 	/*
22795244eac9SBenno Rice 	 * First try primary hash.
22805244eac9SBenno Rice 	 */
228159276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22825244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22835244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
228459276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22855244eac9SBenno Rice 			return (i);
22865244eac9SBenno Rice 		}
22875244eac9SBenno Rice 	}
22885244eac9SBenno Rice 
22895244eac9SBenno Rice 	/*
22905244eac9SBenno Rice 	 * Now try secondary hash.
22915244eac9SBenno Rice 	 */
229259276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2293bd8e6f87SPeter Grehan 
229459276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22955244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22965244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
229759276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22985244eac9SBenno Rice 			return (i);
22995244eac9SBenno Rice 		}
23005244eac9SBenno Rice 	}
23015244eac9SBenno Rice 
230259276937SPeter Grehan 	panic("moea_pte_insert: overflow");
23035244eac9SBenno Rice 	return (-1);
23045244eac9SBenno Rice }
23055244eac9SBenno Rice 
23065244eac9SBenno Rice static boolean_t
230759276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
23085244eac9SBenno Rice {
23095244eac9SBenno Rice 	struct	pvo_entry *pvo;
23105244eac9SBenno Rice 	struct	pte *pt;
23115244eac9SBenno Rice 
2312*8d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
231359276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
23145244eac9SBenno Rice 		return (TRUE);
23155244eac9SBenno Rice 
23165244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
23175244eac9SBenno Rice 
23185244eac9SBenno Rice 		/*
23195244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
23205244eac9SBenno Rice 		 * success.
23215244eac9SBenno Rice 		 */
232252a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
232359276937SPeter Grehan 			moea_attr_save(m, ptebit);
23245244eac9SBenno Rice 			return (TRUE);
23255244eac9SBenno Rice 		}
23265244eac9SBenno Rice 	}
23275244eac9SBenno Rice 
23285244eac9SBenno Rice 	/*
23295244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
23305244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
23315244eac9SBenno Rice 	 * the PTEs.
23325244eac9SBenno Rice 	 */
2333e4f72b32SMarcel Moolenaar 	powerpc_sync();
23345244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
23355244eac9SBenno Rice 
23365244eac9SBenno Rice 		/*
23375244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
23385244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
23395244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
23405244eac9SBenno Rice 		 */
234159276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23425244eac9SBenno Rice 		if (pt != NULL) {
234352a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2344d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
234552a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
234659276937SPeter Grehan 				moea_attr_save(m, ptebit);
23475244eac9SBenno Rice 				return (TRUE);
23485244eac9SBenno Rice 			}
23495244eac9SBenno Rice 		}
23505244eac9SBenno Rice 	}
23515244eac9SBenno Rice 
23524f7daed0SAndrew Gallatin 	return (FALSE);
23535244eac9SBenno Rice }
23545244eac9SBenno Rice 
235503b6e025SPeter Grehan static u_int
2356ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
23575244eac9SBenno Rice {
235803b6e025SPeter Grehan 	u_int	count;
23595244eac9SBenno Rice 	struct	pvo_entry *pvo;
23605244eac9SBenno Rice 	struct	pte *pt;
2361ce186587SAlan Cox 
2362*8d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
23635244eac9SBenno Rice 
23645244eac9SBenno Rice 	/*
23655244eac9SBenno Rice 	 * Clear the cached value.
23665244eac9SBenno Rice 	 */
236759276937SPeter Grehan 	moea_attr_clear(m, ptebit);
23685244eac9SBenno Rice 
23695244eac9SBenno Rice 	/*
23705244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
23715244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
23725244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
23735244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
23745244eac9SBenno Rice 	 * REF/CHG bits.
23755244eac9SBenno Rice 	 */
2376e4f72b32SMarcel Moolenaar 	powerpc_sync();
23775244eac9SBenno Rice 
23785244eac9SBenno Rice 	/*
23795244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
23805244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
23815244eac9SBenno Rice 	 */
238203b6e025SPeter Grehan 	count = 0;
23835244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
238459276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23855244eac9SBenno Rice 		if (pt != NULL) {
238652a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
238752a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
238803b6e025SPeter Grehan 				count++;
238959276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
23905244eac9SBenno Rice 			}
2391d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
239203b6e025SPeter Grehan 		}
239352a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
23945244eac9SBenno Rice 	}
23955244eac9SBenno Rice 
239603b6e025SPeter Grehan 	return (count);
2397bdf71f56SBenno Rice }
23988bbfa33aSBenno Rice 
23998bbfa33aSBenno Rice /*
240032bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
240132bc7846SPeter Grehan  */
240232bc7846SPeter Grehan static int
240359276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
240432bc7846SPeter Grehan {
240532bc7846SPeter Grehan 	u_int prot;
240632bc7846SPeter Grehan 	u_int32_t start;
240732bc7846SPeter Grehan 	u_int32_t end;
240832bc7846SPeter Grehan 	u_int32_t bat_ble;
240932bc7846SPeter Grehan 
241032bc7846SPeter Grehan 	/*
241132bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
241232bc7846SPeter Grehan 	 */
2413c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
241432bc7846SPeter Grehan 		return (EINVAL);
241532bc7846SPeter Grehan 
241632bc7846SPeter Grehan 	/*
241732bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
241832bc7846SPeter Grehan 	 * so it can function as an i/o page
241932bc7846SPeter Grehan 	 */
242032bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
242132bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
242232bc7846SPeter Grehan 		return (EPERM);
242332bc7846SPeter Grehan 
242432bc7846SPeter Grehan 	/*
242532bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
242632bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
242732bc7846SPeter Grehan 	 * not requiring masking)
242832bc7846SPeter Grehan 	 */
242932bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
243032bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
243132bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
243232bc7846SPeter Grehan 
243332bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
243432bc7846SPeter Grehan 		return (ERANGE);
243532bc7846SPeter Grehan 
243632bc7846SPeter Grehan 	return (0);
243732bc7846SPeter Grehan }
243832bc7846SPeter Grehan 
243959276937SPeter Grehan boolean_t
244020b79612SRafal Jaworowski moea_dev_direct_mapped(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2441c0763d37SSuleiman Souhlal {
2442c0763d37SSuleiman Souhlal 	int i;
2443c0763d37SSuleiman Souhlal 
2444c0763d37SSuleiman Souhlal 	/*
2445c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2446c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2447c0763d37SSuleiman Souhlal 	 */
2448c0763d37SSuleiman Souhlal 
2449c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
245059276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2451c0763d37SSuleiman Souhlal 			return (0);
2452c0763d37SSuleiman Souhlal 
2453c0763d37SSuleiman Souhlal 	return (EFAULT);
2454c0763d37SSuleiman Souhlal }
245532bc7846SPeter Grehan 
245632bc7846SPeter Grehan /*
24578bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
24588bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
24598bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
24608bbfa33aSBenno Rice  * NOT real memory.
24618bbfa33aSBenno Rice  */
24628bbfa33aSBenno Rice void *
246320b79612SRafal Jaworowski moea_mapdev(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
24648bbfa33aSBenno Rice {
2465c1f4123bSNathan Whitehorn 
2466c1f4123bSNathan Whitehorn 	return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2467c1f4123bSNathan Whitehorn }
2468c1f4123bSNathan Whitehorn 
2469c1f4123bSNathan Whitehorn void *
2470c1f4123bSNathan Whitehorn moea_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma)
2471c1f4123bSNathan Whitehorn {
247232bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
247332bc7846SPeter Grehan 	int i;
24748bbfa33aSBenno Rice 
247532bc7846SPeter Grehan 	ppa = trunc_page(pa);
24768bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
24778bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
24788bbfa33aSBenno Rice 
247932bc7846SPeter Grehan 	/*
248032bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
248132bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
248232bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
248332bc7846SPeter Grehan 	 */
248432bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
248559276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
248632bc7846SPeter Grehan 			return ((void *) pa);
248732bc7846SPeter Grehan 	}
248832bc7846SPeter Grehan 
2489e53f32acSAlan Cox 	va = kmem_alloc_nofault(kernel_map, size);
24908bbfa33aSBenno Rice 	if (!va)
249159276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
24928bbfa33aSBenno Rice 
24938bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
2494c1f4123bSNathan Whitehorn 		moea_kenter_attr(mmu, tmpva, ppa, ma);
2495e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
24968bbfa33aSBenno Rice 		size -= PAGE_SIZE;
24978bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
249832bc7846SPeter Grehan 		ppa += PAGE_SIZE;
24998bbfa33aSBenno Rice 	}
25008bbfa33aSBenno Rice 
25018bbfa33aSBenno Rice 	return ((void *)(va + offset));
25028bbfa33aSBenno Rice }
25038bbfa33aSBenno Rice 
25048bbfa33aSBenno Rice void
250559276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
25068bbfa33aSBenno Rice {
25078bbfa33aSBenno Rice 	vm_offset_t base, offset;
25088bbfa33aSBenno Rice 
250932bc7846SPeter Grehan 	/*
251032bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
251132bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
251232bc7846SPeter Grehan 	 */
2513ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
25148bbfa33aSBenno Rice 		base = trunc_page(va);
25158bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
25168bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
25178bbfa33aSBenno Rice 		kmem_free(kernel_map, base, size);
25188bbfa33aSBenno Rice 	}
251932bc7846SPeter Grehan }
25201a4fcaebSMarcel Moolenaar 
25211a4fcaebSMarcel Moolenaar static void
25221a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
25231a4fcaebSMarcel Moolenaar {
25241a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
25251a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
25261a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
25271a4fcaebSMarcel Moolenaar 	vm_size_t len;
25281a4fcaebSMarcel Moolenaar 
25291a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
25301a4fcaebSMarcel Moolenaar 	while (sz > 0) {
25311a4fcaebSMarcel Moolenaar 		lim = round_page(va);
25321a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
25331a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
25341a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
25351a4fcaebSMarcel Moolenaar 			pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
25361a4fcaebSMarcel Moolenaar 			    (va & ADDR_POFF);
25371a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
25381a4fcaebSMarcel Moolenaar 		}
25391a4fcaebSMarcel Moolenaar 		va += len;
25401a4fcaebSMarcel Moolenaar 		sz -= len;
25411a4fcaebSMarcel Moolenaar 	}
25421a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
25431a4fcaebSMarcel Moolenaar }
2544