xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision cfedf924d36debd2ea4fe146ebe33f3623f12cf5)
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  * Since the information managed by this module is also stored by the
1005244eac9SBenno Rice  * logical address mapping module, this module may throw away valid virtual
1015244eac9SBenno Rice  * to physical mappings at almost any time.  However, invalidations of
1025244eac9SBenno Rice  * mappings must be done as requested.
1035244eac9SBenno Rice  *
1045244eac9SBenno Rice  * In order to cope with hardware architectures which make virtual to
1055244eac9SBenno Rice  * physical map invalidates expensive, this module may delay invalidate
1065244eac9SBenno Rice  * reduced protection operations until such time as they are actually
1075244eac9SBenno Rice  * necessary.  This module is given full information as to which processors
1085244eac9SBenno Rice  * are currently using which maps, and to when physical maps must be made
1095244eac9SBenno Rice  * correct.
1105244eac9SBenno Rice  */
1115244eac9SBenno Rice 
112ad7a226fSPeter Wemm #include "opt_kstack_pages.h"
113ad7a226fSPeter Wemm 
114f9bac91bSBenno Rice #include <sys/param.h>
1150b27d710SPeter Wemm #include <sys/kernel.h>
116c47dd3dbSAttilio Rao #include <sys/queue.h>
117c47dd3dbSAttilio Rao #include <sys/cpuset.h>
1185244eac9SBenno Rice #include <sys/ktr.h>
11994e0b85eSMark Peek #include <sys/lock.h>
1205244eac9SBenno Rice #include <sys/msgbuf.h>
121f9bac91bSBenno Rice #include <sys/mutex.h>
1225244eac9SBenno Rice #include <sys/proc.h>
1233653f5cbSAlan Cox #include <sys/rwlock.h>
124c47dd3dbSAttilio Rao #include <sys/sched.h>
1255244eac9SBenno Rice #include <sys/sysctl.h>
1265244eac9SBenno Rice #include <sys/systm.h>
1275244eac9SBenno Rice #include <sys/vmmeter.h>
1285244eac9SBenno Rice 
1295244eac9SBenno Rice #include <dev/ofw/openfirm.h>
130f9bac91bSBenno Rice 
131f9bac91bSBenno Rice #include <vm/vm.h>
132f9bac91bSBenno Rice #include <vm/vm_param.h>
133f9bac91bSBenno Rice #include <vm/vm_kern.h>
134f9bac91bSBenno Rice #include <vm/vm_page.h>
135f9bac91bSBenno Rice #include <vm/vm_map.h>
136f9bac91bSBenno Rice #include <vm/vm_object.h>
137f9bac91bSBenno Rice #include <vm/vm_extern.h>
138f9bac91bSBenno Rice #include <vm/vm_pageout.h>
139f9bac91bSBenno Rice #include <vm/vm_pager.h>
140378862a7SJeff Roberson #include <vm/uma.h>
141f9bac91bSBenno Rice 
1427c277971SPeter Grehan #include <machine/cpu.h>
143b40ce02aSNathan Whitehorn #include <machine/platform.h>
144d699b539SMark Peek #include <machine/bat.h>
1455244eac9SBenno Rice #include <machine/frame.h>
1465244eac9SBenno Rice #include <machine/md_var.h>
1475244eac9SBenno Rice #include <machine/psl.h>
148f9bac91bSBenno Rice #include <machine/pte.h>
14912640815SMarcel Moolenaar #include <machine/smp.h>
1505244eac9SBenno Rice #include <machine/sr.h>
15159276937SPeter Grehan #include <machine/mmuvar.h>
152e347e23bSNathan Whitehorn #include <machine/trap_aim.h>
153f9bac91bSBenno Rice 
15459276937SPeter Grehan #include "mmu_if.h"
15559276937SPeter Grehan 
15659276937SPeter Grehan #define	MOEA_DEBUG
157f9bac91bSBenno Rice 
1585244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
159f9bac91bSBenno Rice 
1605244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1615244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1625244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1635244eac9SBenno Rice 
1645244eac9SBenno Rice struct ofw_map {
1655244eac9SBenno Rice 	vm_offset_t	om_va;
1665244eac9SBenno Rice 	vm_size_t	om_len;
1675244eac9SBenno Rice 	vm_offset_t	om_pa;
1685244eac9SBenno Rice 	u_int		om_mode;
1695244eac9SBenno Rice };
170f9bac91bSBenno Rice 
1715244eac9SBenno Rice /*
1725244eac9SBenno Rice  * Map of physical memory regions.
1735244eac9SBenno Rice  */
17431c82d03SBenno Rice static struct	mem_region *regions;
17531c82d03SBenno Rice static struct	mem_region *pregions;
176c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
177c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
178aa39961eSBenno Rice static struct	ofw_map *translations;
1795244eac9SBenno Rice 
180f9bac91bSBenno Rice /*
181f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
182f489bf21SAlan Cox  */
18359276937SPeter Grehan struct mtx	moea_table_mutex;
184e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
185f489bf21SAlan Cox 
186e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
187e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
188e4f72b32SMarcel Moolenaar 
189f489bf21SAlan Cox /*
1905244eac9SBenno Rice  * PTEG data.
191f9bac91bSBenno Rice  */
19259276937SPeter Grehan static struct	pteg *moea_pteg_table;
19359276937SPeter Grehan u_int		moea_pteg_count;
19459276937SPeter Grehan u_int		moea_pteg_mask;
1955244eac9SBenno Rice 
1965244eac9SBenno Rice /*
1975244eac9SBenno Rice  * PVO data.
1985244eac9SBenno Rice  */
19959276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
20059276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
20159276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
2025244eac9SBenno Rice 
203*cfedf924SAttilio Rao static struct rwlock_padalign pvh_global_lock;
2043653f5cbSAlan Cox 
20559276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
20659276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2075244eac9SBenno Rice 
2080d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
20959276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
21059276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2115244eac9SBenno Rice 
2125244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
21359276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2145244eac9SBenno Rice 
21559276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2165244eac9SBenno Rice 
2175244eac9SBenno Rice /*
2185244eac9SBenno Rice  * Statistics.
2195244eac9SBenno Rice  */
22059276937SPeter Grehan u_int	moea_pte_valid = 0;
22159276937SPeter Grehan u_int	moea_pte_overflow = 0;
22259276937SPeter Grehan u_int	moea_pte_replacements = 0;
22359276937SPeter Grehan u_int	moea_pvo_entries = 0;
22459276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
22559276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
22659276937SPeter Grehan u_int	moea_pte_spills = 0;
22759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2285244eac9SBenno Rice     0, "");
22959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
23059276937SPeter Grehan     &moea_pte_overflow, 0, "");
23159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
23259276937SPeter Grehan     &moea_pte_replacements, 0, "");
23359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2345244eac9SBenno Rice     0, "");
23559276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
23659276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
23759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
23859276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
23959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
24059276937SPeter Grehan     &moea_pte_spills, 0, "");
2415244eac9SBenno Rice 
2425244eac9SBenno Rice /*
24359276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2445244eac9SBenno Rice  */
24559276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2465244eac9SBenno Rice 
2475244eac9SBenno Rice /*
2485244eac9SBenno Rice  * PTE calls.
2495244eac9SBenno Rice  */
25059276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2515244eac9SBenno Rice 
2525244eac9SBenno Rice /*
2535244eac9SBenno Rice  * PVO calls.
2545244eac9SBenno Rice  */
25559276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2565244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
25759276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
25859276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
25959276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2605244eac9SBenno Rice 
2615244eac9SBenno Rice /*
2625244eac9SBenno Rice  * Utility routines.
2635244eac9SBenno Rice  */
264ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
265ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
26659276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
26759276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
268ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
26959276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
27059276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
27159276937SPeter Grehan 
27259276937SPeter Grehan /*
27359276937SPeter Grehan  * Kernel MMU interface
27459276937SPeter Grehan  */
27559276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
27659276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
27759276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t);
27859276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
27959276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
280ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
281ce142d9eSAlan Cox     vm_prot_t);
2822053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
28359276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
28459276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
28559276937SPeter Grehan void moea_init(mmu_t);
28659276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
287e396eb60SAlan Cox boolean_t moea_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
2887b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t);
2898d9e6d9fSAlan Cox int moea_ts_referenced(mmu_t, vm_page_t);
29020b79612SRafal Jaworowski vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_paddr_t, vm_paddr_t, int);
29159276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
29259677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
29359276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
29459276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
29559276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
29659276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
29759276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
29859276937SPeter Grehan void moea_release(mmu_t, pmap_t);
29959276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
30059276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
30178985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
30259276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
30359276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
30459276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
30559276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
30659276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
3071c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int);
30859276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
30920b79612SRafal Jaworowski void *moea_mapdev(mmu_t, vm_paddr_t, vm_size_t);
310c1f4123bSNathan Whitehorn void *moea_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t);
31159276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
31220b79612SRafal Jaworowski vm_paddr_t moea_kextract(mmu_t, vm_offset_t);
313c1f4123bSNathan Whitehorn void moea_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t);
31420b79612SRafal Jaworowski void moea_kenter(mmu_t, vm_offset_t, vm_paddr_t);
315c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma);
31620b79612SRafal Jaworowski boolean_t moea_dev_direct_mapped(mmu_t, vm_paddr_t, vm_size_t);
3171a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
31859276937SPeter Grehan 
31959276937SPeter Grehan static mmu_method_t moea_methods[] = {
32059276937SPeter Grehan 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
32159276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
32259276937SPeter Grehan 	MMUMETHOD(mmu_clear_reference,	moea_clear_reference),
32359276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
32459276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
325ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
32659276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
32759276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
32859276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
32959276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
33059276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
331e396eb60SAlan Cox 	MMUMETHOD(mmu_is_prefaultable,	moea_is_prefaultable),
3327b85f591SAlan Cox 	MMUMETHOD(mmu_is_referenced,	moea_is_referenced),
33359276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
33459276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
33559276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
33659677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
33759276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
33859276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
33959276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
34059276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
34159276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
34259276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
34359276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
34459276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
34578985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
3461a4fcaebSMarcel Moolenaar 	MMUMETHOD(mmu_sync_icache,	moea_sync_icache),
34759276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
34859276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
34959276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
35059276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
35159276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
352c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_page_set_memattr,	moea_page_set_memattr),
35359276937SPeter Grehan 
35459276937SPeter Grehan 	/* Internal interfaces */
35559276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
3561c96bdd1SNathan Whitehorn 	MMUMETHOD(mmu_cpu_bootstrap,   	moea_cpu_bootstrap),
357c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_mapdev_attr,	moea_mapdev_attr),
35859276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
35959276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
36059276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
36159276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
362c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_kenter_attr,	moea_kenter_attr),
36359276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
36459276937SPeter Grehan 
36559276937SPeter Grehan 	{ 0, 0 }
36659276937SPeter Grehan };
36759276937SPeter Grehan 
36833529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0);
36933529b98SPeter Grehan 
370c1f4123bSNathan Whitehorn static __inline uint32_t
371c1f4123bSNathan Whitehorn moea_calc_wimg(vm_offset_t pa, vm_memattr_t ma)
372c1f4123bSNathan Whitehorn {
373c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
374c1f4123bSNathan Whitehorn 	int i;
375c1f4123bSNathan Whitehorn 
376c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
377c1f4123bSNathan Whitehorn 		switch (ma) {
378c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
379c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
380c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
381c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
382c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
383c1f4123bSNathan Whitehorn 			return (PTE_I);
384c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
385c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
386c1f4123bSNathan Whitehorn 		}
387c1f4123bSNathan Whitehorn 	}
388c1f4123bSNathan Whitehorn 
389c1f4123bSNathan Whitehorn 	/*
390c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
391c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
392c1f4123bSNathan Whitehorn 	 */
393c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
394c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
395c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
396c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
397c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
398c1f4123bSNathan Whitehorn 			break;
399c1f4123bSNathan Whitehorn 		}
400c1f4123bSNathan Whitehorn 	}
401c1f4123bSNathan Whitehorn 
402c1f4123bSNathan Whitehorn 	return pte_lo;
403c1f4123bSNathan Whitehorn }
40459276937SPeter Grehan 
405e4f72b32SMarcel Moolenaar static void
406e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
407e4f72b32SMarcel Moolenaar {
408e4f72b32SMarcel Moolenaar 
409e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
41094363f53SNathan Whitehorn 	__asm __volatile("ptesync");
411e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
41294363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
413e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
414e4f72b32SMarcel Moolenaar }
415e4f72b32SMarcel Moolenaar 
416e4f72b32SMarcel Moolenaar static void
417e4f72b32SMarcel Moolenaar tlbia(void)
418e4f72b32SMarcel Moolenaar {
419e4f72b32SMarcel Moolenaar 	vm_offset_t va;
420e4f72b32SMarcel Moolenaar 
421e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
422e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
423e4f72b32SMarcel Moolenaar 		powerpc_sync();
424e4f72b32SMarcel Moolenaar 	}
425e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
426e4f72b32SMarcel Moolenaar 	powerpc_sync();
427e4f72b32SMarcel Moolenaar }
4285244eac9SBenno Rice 
4295244eac9SBenno Rice static __inline int
4305244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4315244eac9SBenno Rice {
4325244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4335244eac9SBenno Rice }
4345244eac9SBenno Rice 
4355244eac9SBenno Rice static __inline u_int
4365244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4375244eac9SBenno Rice {
4385244eac9SBenno Rice 	u_int hash;
4395244eac9SBenno Rice 
4405244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4415244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
44259276937SPeter Grehan 	return (hash & moea_pteg_mask);
4435244eac9SBenno Rice }
4445244eac9SBenno Rice 
4455244eac9SBenno Rice static __inline struct pvo_head *
4465244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
447f9bac91bSBenno Rice {
448f9bac91bSBenno Rice 
4495244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
450f9bac91bSBenno Rice }
451f9bac91bSBenno Rice 
452f9bac91bSBenno Rice static __inline void
45359276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
454f9bac91bSBenno Rice {
455f9bac91bSBenno Rice 
4563653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4575244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4585244eac9SBenno Rice }
4595244eac9SBenno Rice 
4605244eac9SBenno Rice static __inline int
46159276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4625244eac9SBenno Rice {
4635244eac9SBenno Rice 
4645244eac9SBenno Rice 	return (m->md.mdpg_attrs);
465f9bac91bSBenno Rice }
466f9bac91bSBenno Rice 
467f9bac91bSBenno Rice static __inline void
46859276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
469f9bac91bSBenno Rice {
470f9bac91bSBenno Rice 
4713653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4725244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
473f9bac91bSBenno Rice }
474f9bac91bSBenno Rice 
475f9bac91bSBenno Rice static __inline int
47659276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
477f9bac91bSBenno Rice {
4785244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4795244eac9SBenno Rice 		return (1);
480f9bac91bSBenno Rice 
4815244eac9SBenno Rice 	return (0);
482f9bac91bSBenno Rice }
483f9bac91bSBenno Rice 
484f9bac91bSBenno Rice static __inline int
48559276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
486f9bac91bSBenno Rice {
4875244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
4885244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4895244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
490f9bac91bSBenno Rice }
491f9bac91bSBenno Rice 
4925244eac9SBenno Rice static __inline void
49359276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
494f9bac91bSBenno Rice {
495d644a0b7SAlan Cox 
496d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
497d644a0b7SAlan Cox 
498f9bac91bSBenno Rice 	/*
4995244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5005244eac9SBenno Rice 	 * set when the real pte is set in memory.
501f9bac91bSBenno Rice 	 *
502f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
503f9bac91bSBenno Rice 	 */
5045244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5055244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5065244eac9SBenno Rice 	pt->pte_lo = pte_lo;
507f9bac91bSBenno Rice }
508f9bac91bSBenno Rice 
5095244eac9SBenno Rice static __inline void
51059276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
511f9bac91bSBenno Rice {
512f9bac91bSBenno Rice 
513d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5145244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
515f9bac91bSBenno Rice }
516f9bac91bSBenno Rice 
5175244eac9SBenno Rice static __inline void
51859276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
519f9bac91bSBenno Rice {
5205244eac9SBenno Rice 
521d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
522d644a0b7SAlan Cox 
5235244eac9SBenno Rice 	/*
5245244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5255244eac9SBenno Rice 	 */
5265244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
527e4f72b32SMarcel Moolenaar 	tlbie(va);
5285244eac9SBenno Rice }
5295244eac9SBenno Rice 
5305244eac9SBenno Rice static __inline void
53159276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5325244eac9SBenno Rice {
5335244eac9SBenno Rice 
534d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5355244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5365244eac9SBenno Rice 
5375244eac9SBenno Rice 	/*
5385244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
5395244eac9SBenno Rice 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
5405244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5415244eac9SBenno Rice 	 */
5425244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
543e4f72b32SMarcel Moolenaar 	powerpc_sync();
5445244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
545e4f72b32SMarcel Moolenaar 	powerpc_sync();
54659276937SPeter Grehan 	moea_pte_valid++;
5475244eac9SBenno Rice }
5485244eac9SBenno Rice 
5495244eac9SBenno Rice static __inline void
55059276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5515244eac9SBenno Rice {
5525244eac9SBenno Rice 
553d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5545244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5555244eac9SBenno Rice 
5565244eac9SBenno Rice 	/*
5575244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5585244eac9SBenno Rice 	 */
559e4f72b32SMarcel Moolenaar 	powerpc_sync();
5605244eac9SBenno Rice 
5615244eac9SBenno Rice 	/*
5625244eac9SBenno Rice 	 * Invalidate the pte.
5635244eac9SBenno Rice 	 */
5645244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5655244eac9SBenno Rice 
566e4f72b32SMarcel Moolenaar 	tlbie(va);
5675244eac9SBenno Rice 
5685244eac9SBenno Rice 	/*
5695244eac9SBenno Rice 	 * Save the reg & chg bits.
5705244eac9SBenno Rice 	 */
57159276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
57259276937SPeter Grehan 	moea_pte_valid--;
5735244eac9SBenno Rice }
5745244eac9SBenno Rice 
5755244eac9SBenno Rice static __inline void
57659276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5775244eac9SBenno Rice {
5785244eac9SBenno Rice 
5795244eac9SBenno Rice 	/*
5805244eac9SBenno Rice 	 * Invalidate the PTE
5815244eac9SBenno Rice 	 */
58259276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
58359276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
584f9bac91bSBenno Rice }
585f9bac91bSBenno Rice 
586f9bac91bSBenno Rice /*
5875244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
588f9bac91bSBenno Rice  */
5895244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
5905244eac9SBenno Rice 
5915244eac9SBenno Rice static int
5925244eac9SBenno Rice om_cmp(const void *a, const void *b)
5935244eac9SBenno Rice {
5945244eac9SBenno Rice 	const struct	ofw_map *mapa;
5955244eac9SBenno Rice 	const struct	ofw_map *mapb;
5965244eac9SBenno Rice 
5975244eac9SBenno Rice 	mapa = a;
5985244eac9SBenno Rice 	mapb = b;
5995244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6005244eac9SBenno Rice 		return (-1);
6015244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6025244eac9SBenno Rice 		return (1);
6035244eac9SBenno Rice 	else
6045244eac9SBenno Rice 		return (0);
605f9bac91bSBenno Rice }
606f9bac91bSBenno Rice 
607f9bac91bSBenno Rice void
6081c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap)
60912640815SMarcel Moolenaar {
61012640815SMarcel Moolenaar 	u_int sdr;
61112640815SMarcel Moolenaar 	int i;
61212640815SMarcel Moolenaar 
61312640815SMarcel Moolenaar 	if (ap) {
614e4f72b32SMarcel Moolenaar 		powerpc_sync();
61512640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
61612640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
61712640815SMarcel Moolenaar 		isync();
61812640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
61912640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
62012640815SMarcel Moolenaar 		isync();
62112640815SMarcel Moolenaar 	}
62212640815SMarcel Moolenaar 
62301d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
62401d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
62512640815SMarcel Moolenaar 	isync();
62612640815SMarcel Moolenaar 
62701d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
62801d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
62901d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
63001d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
63101d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
63212640815SMarcel Moolenaar 	isync();
63312640815SMarcel Moolenaar 
63412640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
635fe3b4685SNathan Whitehorn 		mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
636e4f72b32SMarcel Moolenaar 	powerpc_sync();
63712640815SMarcel Moolenaar 
63812640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
63912640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
64012640815SMarcel Moolenaar 	isync();
64112640815SMarcel Moolenaar 
64286c1fb4cSMarcel Moolenaar 	tlbia();
64312640815SMarcel Moolenaar }
64412640815SMarcel Moolenaar 
64512640815SMarcel Moolenaar void
64659276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
647f9bac91bSBenno Rice {
64831c82d03SBenno Rice 	ihandle_t	mmui;
6495244eac9SBenno Rice 	phandle_t	chosen, mmu;
6505244eac9SBenno Rice 	int		sz;
6515244eac9SBenno Rice 	int		i, j;
652e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6535244eac9SBenno Rice 	vm_offset_t	pa, va, off;
65450c202c5SJeff Roberson 	void		*dpcpu;
655976cc697SNathan Whitehorn 	register_t	msr;
656f9bac91bSBenno Rice 
657f9bac91bSBenno Rice         /*
65832bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6590d290675SBenno Rice          */
6600d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6610d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6620d290675SBenno Rice 
6630d290675SBenno Rice         /*
6640d290675SBenno Rice          * Map PCI memory space.
6650d290675SBenno Rice          */
6660d290675SBenno Rice         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6670d290675SBenno Rice         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6680d290675SBenno Rice 
6690d290675SBenno Rice         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6700d290675SBenno Rice         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6710d290675SBenno Rice 
6720d290675SBenno Rice         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6730d290675SBenno Rice         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6740d290675SBenno Rice 
6750d290675SBenno Rice         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6760d290675SBenno Rice         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6770d290675SBenno Rice 
6780d290675SBenno Rice         /*
6790d290675SBenno Rice          * Map obio devices.
6800d290675SBenno Rice          */
6810d290675SBenno Rice         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
6820d290675SBenno Rice         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
6830d290675SBenno Rice 
6840d290675SBenno Rice 	/*
6855244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
686976cc697SNathan Whitehorn 	 * where we are. Turn off instruction relocation temporarily
687976cc697SNathan Whitehorn 	 * to prevent faults while reprogramming the IBAT.
688f9bac91bSBenno Rice 	 */
689976cc697SNathan Whitehorn 	msr = mfmsr();
690976cc697SNathan Whitehorn 	mtmsr(msr & ~PSL_IR);
69159276937SPeter Grehan 	__asm (".balign 32; \n"
69272ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
6935d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
69412640815SMarcel Moolenaar 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
695976cc697SNathan Whitehorn 	mtmsr(msr);
6960d290675SBenno Rice 
6970d290675SBenno Rice 	/* map pci space */
69812640815SMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
69912640815SMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
70012640815SMarcel Moolenaar 	isync();
701f9bac91bSBenno Rice 
7021c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7031c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7041c96bdd1SNathan Whitehorn 
70531c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
70659276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
70731c82d03SBenno Rice 
70831c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
70932bc7846SPeter Grehan 		vm_offset_t pa;
71032bc7846SPeter Grehan 		vm_offset_t end;
71132bc7846SPeter Grehan 
71231c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
71331c82d03SBenno Rice 			pregions[i].mr_start,
71431c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
71531c82d03SBenno Rice 			pregions[i].mr_size);
71632bc7846SPeter Grehan 		/*
71732bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
71832bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
71932bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
72032bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
72132bc7846SPeter Grehan 		 * a while yet.
72232bc7846SPeter Grehan 		 */
72332bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
72432bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
72532bc7846SPeter Grehan 		do {
72632bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
72732bc7846SPeter Grehan 
72832bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
72932bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
73032bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
73132bc7846SPeter Grehan 		} while (pa < end);
73231c82d03SBenno Rice 	}
73331c82d03SBenno Rice 
73431c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
73559276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
73697f7cde4SNathan Whitehorn 
7375244eac9SBenno Rice 	phys_avail_count = 0;
738d2c1f576SBenno Rice 	physsz = 0;
739b0c21309SPeter Grehan 	hwphyssz = 0;
740b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
74131c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7425244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7435244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7445244eac9SBenno Rice 		    regions[i].mr_size);
745e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
746e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
747e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
748e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
749e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
750e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
751e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
752e2f6d6e2SPeter Grehan 				phys_avail_count++;
753e2f6d6e2SPeter Grehan 			}
754e2f6d6e2SPeter Grehan 			break;
755e2f6d6e2SPeter Grehan 		}
7565244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7575244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7585244eac9SBenno Rice 		phys_avail_count++;
759d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
760f9bac91bSBenno Rice 	}
761e347e23bSNathan Whitehorn 
762e347e23bSNathan Whitehorn 	/* Check for overlap with the kernel and exception vectors */
763e347e23bSNathan Whitehorn 	for (j = 0; j < 2*phys_avail_count; j+=2) {
764e347e23bSNathan Whitehorn 		if (phys_avail[j] < EXC_LAST)
765e347e23bSNathan Whitehorn 			phys_avail[j] += EXC_LAST;
766e347e23bSNathan Whitehorn 
767e347e23bSNathan Whitehorn 		if (kernelstart >= phys_avail[j] &&
768e347e23bSNathan Whitehorn 		    kernelstart < phys_avail[j+1]) {
769e347e23bSNathan Whitehorn 			if (kernelend < phys_avail[j+1]) {
770e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] =
771e347e23bSNathan Whitehorn 				    (kernelend & ~PAGE_MASK) + PAGE_SIZE;
772e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
773e347e23bSNathan Whitehorn 				    phys_avail[j+1];
774e347e23bSNathan Whitehorn 				phys_avail_count++;
775e347e23bSNathan Whitehorn 			}
776e347e23bSNathan Whitehorn 
777e347e23bSNathan Whitehorn 			phys_avail[j+1] = kernelstart & ~PAGE_MASK;
778e347e23bSNathan Whitehorn 		}
779e347e23bSNathan Whitehorn 
780e347e23bSNathan Whitehorn 		if (kernelend >= phys_avail[j] &&
781e347e23bSNathan Whitehorn 		    kernelend < phys_avail[j+1]) {
782e347e23bSNathan Whitehorn 			if (kernelstart > phys_avail[j]) {
783e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] = phys_avail[j];
784e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
785e347e23bSNathan Whitehorn 				    kernelstart & ~PAGE_MASK;
786e347e23bSNathan Whitehorn 				phys_avail_count++;
787e347e23bSNathan Whitehorn 			}
788e347e23bSNathan Whitehorn 
789e347e23bSNathan Whitehorn 			phys_avail[j] = (kernelend & ~PAGE_MASK) + PAGE_SIZE;
790e347e23bSNathan Whitehorn 		}
791e347e23bSNathan Whitehorn 	}
792e347e23bSNathan Whitehorn 
793d2c1f576SBenno Rice 	physmem = btoc(physsz);
794f9bac91bSBenno Rice 
795f9bac91bSBenno Rice 	/*
7965244eac9SBenno Rice 	 * Allocate PTEG table.
797f9bac91bSBenno Rice 	 */
7985244eac9SBenno Rice #ifdef PTEGCOUNT
79959276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
8005244eac9SBenno Rice #else
80159276937SPeter Grehan 	moea_pteg_count = 0x1000;
802f9bac91bSBenno Rice 
80359276937SPeter Grehan 	while (moea_pteg_count < physmem)
80459276937SPeter Grehan 		moea_pteg_count <<= 1;
805f9bac91bSBenno Rice 
80659276937SPeter Grehan 	moea_pteg_count >>= 1;
8075244eac9SBenno Rice #endif /* PTEGCOUNT */
808f9bac91bSBenno Rice 
80959276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
81059276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
8115244eac9SBenno Rice 	    size);
81259276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
81359276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
81459276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
81559276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
816f9bac91bSBenno Rice 
8175244eac9SBenno Rice 	/*
818864bc520SBenno Rice 	 * Allocate pv/overflow lists.
8195244eac9SBenno Rice 	 */
82059276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
82159276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
8225244eac9SBenno Rice 	    PAGE_SIZE);
82359276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
82459276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
82559276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
8265244eac9SBenno Rice 
8275244eac9SBenno Rice 	/*
828f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
829f489bf21SAlan Cox 	 * tables.
830f489bf21SAlan Cox 	 */
831d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
832d644a0b7SAlan Cox 	    MTX_RECURSE);
833e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
834f489bf21SAlan Cox 
835e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
836e4f72b32SMarcel Moolenaar 
837f489bf21SAlan Cox 	/*
8385244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8395244eac9SBenno Rice 	 */
84059276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8410d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
84259276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8435244eac9SBenno Rice 
8445244eac9SBenno Rice 	/*
8455244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8465244eac9SBenno Rice 	 */
84759276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8485244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
84959276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8505244eac9SBenno Rice 
8515244eac9SBenno Rice 	/*
852fe3b4685SNathan Whitehorn 	 * Initialize the kernel pmap (which is statically allocated).
8535244eac9SBenno Rice 	 */
854fe3b4685SNathan Whitehorn 	PMAP_LOCK_INIT(kernel_pmap);
855fe3b4685SNathan Whitehorn 	for (i = 0; i < 16; i++)
856fe3b4685SNathan Whitehorn 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
857c47dd3dbSAttilio Rao 	CPU_FILL(&kernel_pmap->pm_active);
858ccc4a5c7SNathan Whitehorn 	RB_INIT(&kernel_pmap->pmap_pvo);
859fe3b4685SNathan Whitehorn 
860fe3b4685SNathan Whitehorn  	/*
8613653f5cbSAlan Cox 	 * Initialize the global pv list lock.
8623653f5cbSAlan Cox 	 */
8633653f5cbSAlan Cox 	rw_init(&pvh_global_lock, "pmap pv global");
8643653f5cbSAlan Cox 
8653653f5cbSAlan Cox 	/*
866fe3b4685SNathan Whitehorn 	 * Set up the Open Firmware mappings
867fe3b4685SNathan Whitehorn 	 */
868e347e23bSNathan Whitehorn 	chosen = OF_finddevice("/chosen");
869e347e23bSNathan Whitehorn 	if (chosen != -1 && OF_getprop(chosen, "mmu", &mmui, 4) != -1 &&
870e347e23bSNathan Whitehorn 	    (mmu = OF_instance_to_package(mmui)) != -1 &&
871e347e23bSNathan Whitehorn 	    (sz = OF_getproplen(mmu, "translations")) != -1) {
872aa39961eSBenno Rice 		translations = NULL;
8736cc1cdf4SPeter Grehan 		for (i = 0; phys_avail[i] != 0; i += 2) {
8746cc1cdf4SPeter Grehan 			if (phys_avail[i + 1] >= sz) {
875aa39961eSBenno Rice 				translations = (struct ofw_map *)phys_avail[i];
8766cc1cdf4SPeter Grehan 				break;
8776cc1cdf4SPeter Grehan 			}
878aa39961eSBenno Rice 		}
879aa39961eSBenno Rice 		if (translations == NULL)
88059276937SPeter Grehan 			panic("moea_bootstrap: no space to copy translations");
8815244eac9SBenno Rice 		bzero(translations, sz);
8825244eac9SBenno Rice 		if (OF_getprop(mmu, "translations", translations, sz) == -1)
88359276937SPeter Grehan 			panic("moea_bootstrap: can't get ofw translations");
88459276937SPeter Grehan 		CTR0(KTR_PMAP, "moea_bootstrap: translations");
88531c82d03SBenno Rice 		sz /= sizeof(*translations);
8865244eac9SBenno Rice 		qsort(translations, sz, sizeof (*translations), om_cmp);
887ed1e1e2aSNathan Whitehorn 		for (i = 0; i < sz; i++) {
8885244eac9SBenno Rice 			CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
8895244eac9SBenno Rice 			    translations[i].om_pa, translations[i].om_va,
8905244eac9SBenno Rice 			    translations[i].om_len);
8915244eac9SBenno Rice 
89232bc7846SPeter Grehan 			/*
893e347e23bSNathan Whitehorn 			 * If the mapping is 1:1, let the RAM and device
894e347e23bSNathan Whitehorn 			 * on-demand BAT tables take care of the translation.
89532bc7846SPeter Grehan 			 */
89632bc7846SPeter Grehan 			if (translations[i].om_va == translations[i].om_pa)
89732bc7846SPeter Grehan 				continue;
8985244eac9SBenno Rice 
89932bc7846SPeter Grehan 			/* Enter the pages */
900e347e23bSNathan Whitehorn 			for (off = 0; off < translations[i].om_len;
901e347e23bSNathan Whitehorn 			    off += PAGE_SIZE)
902fe3b4685SNathan Whitehorn 				moea_kenter(mmup, translations[i].om_va + off,
903fe3b4685SNathan Whitehorn 					    translations[i].om_pa + off);
904f9bac91bSBenno Rice 		}
905e347e23bSNathan Whitehorn 	}
906014ffa99SMarcel Moolenaar 
907014ffa99SMarcel Moolenaar 	/*
908014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
909014ffa99SMarcel Moolenaar 	 */
910014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
911014ffa99SMarcel Moolenaar 		;
912014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
9135244eac9SBenno Rice 
9141c96bdd1SNathan Whitehorn 	moea_cpu_bootstrap(mmup,0);
9155244eac9SBenno Rice 
9165244eac9SBenno Rice 	pmap_bootstrapped++;
917014ffa99SMarcel Moolenaar 
918014ffa99SMarcel Moolenaar 	/*
919014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
920014ffa99SMarcel Moolenaar 	 */
921014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
922ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
923014ffa99SMarcel Moolenaar 
924014ffa99SMarcel Moolenaar 	/*
925014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
926014ffa99SMarcel Moolenaar 	 * into the kernel page map.
927014ffa99SMarcel Moolenaar 	 */
928014ffa99SMarcel Moolenaar 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
929014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
930014ffa99SMarcel Moolenaar 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
931014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
932014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
933014ffa99SMarcel Moolenaar 	thread0.td_kstack_pages = KSTACK_PAGES;
934014ffa99SMarcel Moolenaar 	for (i = 0; i < KSTACK_PAGES; i++) {
935c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
936014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
937014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
938014ffa99SMarcel Moolenaar 	}
939014ffa99SMarcel Moolenaar 
940014ffa99SMarcel Moolenaar 	/*
941014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
942014ffa99SMarcel Moolenaar 	 */
9434053b05bSSergey Kandaurov 	pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE);
944014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
945014ffa99SMarcel Moolenaar 	va = virtual_avail;
9464053b05bSSergey Kandaurov 	virtual_avail += round_page(msgbufsize);
947014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
948c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
949014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
950014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
951014ffa99SMarcel Moolenaar 	}
95250c202c5SJeff Roberson 
95350c202c5SJeff Roberson 	/*
95450c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
95550c202c5SJeff Roberson 	 */
95650c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
95750c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
95850c202c5SJeff Roberson 	va = virtual_avail;
95950c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
96050c202c5SJeff Roberson 	while (va < virtual_avail) {
961c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
96250c202c5SJeff Roberson 		pa += PAGE_SIZE;
96350c202c5SJeff Roberson 		va += PAGE_SIZE;
96450c202c5SJeff Roberson 	}
96550c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
9665244eac9SBenno Rice }
9675244eac9SBenno Rice 
9685244eac9SBenno Rice /*
9695244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9705244eac9SBenno Rice  * space can be accessed in any way.
971f9bac91bSBenno Rice  */
972f9bac91bSBenno Rice void
97359276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
974f9bac91bSBenno Rice {
9758207b362SBenno Rice 	pmap_t	pm, pmr;
976f9bac91bSBenno Rice 
977f9bac91bSBenno Rice 	/*
97832bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9795244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
980f9bac91bSBenno Rice 	 */
9815244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
98252a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
9838207b362SBenno Rice 
984c7c2767eSAttilio Rao 	CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
9858207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
986ac6ba8bdSBenno Rice }
987ac6ba8bdSBenno Rice 
988ac6ba8bdSBenno Rice void
98959276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
990ac6ba8bdSBenno Rice {
991ac6ba8bdSBenno Rice 	pmap_t	pm;
992ac6ba8bdSBenno Rice 
993ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
994c7c2767eSAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &pm->pm_active);
9958207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
996f9bac91bSBenno Rice }
997f9bac91bSBenno Rice 
998f9bac91bSBenno Rice void
99959276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
1000f9bac91bSBenno Rice {
10010f92104cSBenno Rice 	struct	pvo_entry *pvo;
10020f92104cSBenno Rice 
100348d0b1a0SAlan Cox 	PMAP_LOCK(pm);
100459276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
10050f92104cSBenno Rice 
10060f92104cSBenno Rice 	if (pvo != NULL) {
10070f92104cSBenno Rice 		if (wired) {
10080f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
10090f92104cSBenno Rice 				pm->pm_stats.wired_count++;
10100f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
10110f92104cSBenno Rice 		} else {
10120f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
10130f92104cSBenno Rice 				pm->pm_stats.wired_count--;
10140f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
10150f92104cSBenno Rice 		}
10160f92104cSBenno Rice 	}
101748d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
1018f9bac91bSBenno Rice }
1019f9bac91bSBenno Rice 
1020f9bac91bSBenno Rice void
102159276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
1022f9bac91bSBenno Rice {
102325e2288dSBenno Rice 	vm_offset_t	dst;
102425e2288dSBenno Rice 	vm_offset_t	src;
102525e2288dSBenno Rice 
102625e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
102725e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
102825e2288dSBenno Rice 
1029e3c2930dSNathan Whitehorn 	bcopy((void *)src, (void *)dst, PAGE_SIZE);
1030f9bac91bSBenno Rice }
1031111c77dcSBenno Rice 
1032111c77dcSBenno Rice /*
10335244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
10345244eac9SBenno Rice  */
10355244eac9SBenno Rice void
103659276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
10375244eac9SBenno Rice {
10381a87a0daSPeter Wemm 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10395b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
10405244eac9SBenno Rice 
10415244eac9SBenno Rice 	bzero(va, PAGE_SIZE);
10425244eac9SBenno Rice }
10435244eac9SBenno Rice 
10445244eac9SBenno Rice void
104559276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
10465244eac9SBenno Rice {
10473495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10485b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
10493495845eSBenno Rice 
10505b43c63dSMarcel Moolenaar 	bzero(va, size);
10515244eac9SBenno Rice }
10525244eac9SBenno Rice 
1053a58b3a68SPeter Wemm void
105459276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1055a58b3a68SPeter Wemm {
10565b43c63dSMarcel Moolenaar 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10575b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
1058a58b3a68SPeter Wemm 
10595b43c63dSMarcel Moolenaar 	bzero(va, PAGE_SIZE);
1060a58b3a68SPeter Wemm }
1061a58b3a68SPeter Wemm 
10625244eac9SBenno Rice /*
10635244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
10645244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
10655244eac9SBenno Rice  * will be wired down.
10665244eac9SBenno Rice  */
10675244eac9SBenno Rice void
106859276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
10695244eac9SBenno Rice 	   boolean_t wired)
10705244eac9SBenno Rice {
1071ce142d9eSAlan Cox 
10723653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1073ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
107467c867eeSAlan Cox 	moea_enter_locked(pmap, va, m, prot, wired);
10753653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1076ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1077ce142d9eSAlan Cox }
1078ce142d9eSAlan Cox 
1079ce142d9eSAlan Cox /*
1080ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1081ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1082ce142d9eSAlan Cox  * will be wired down.
1083ce142d9eSAlan Cox  *
1084ce142d9eSAlan Cox  * The page queues and pmap must be locked.
1085ce142d9eSAlan Cox  */
1086ce142d9eSAlan Cox static void
1087ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1088ce142d9eSAlan Cox     boolean_t wired)
1089ce142d9eSAlan Cox {
10905244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1091378862a7SJeff Roberson 	uma_zone_t	zone;
10928207b362SBenno Rice 	vm_page_t	pg;
109357bd5cceSNathan Whitehorn 	u_int		pte_lo, pvo_flags;
10945244eac9SBenno Rice 	int		error;
10955244eac9SBenno Rice 
109659276937SPeter Grehan 	if (!moea_initialized) {
109759276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
109859276937SPeter Grehan 		zone = moea_upvo_zone;
10995244eac9SBenno Rice 		pvo_flags = 0;
11008207b362SBenno Rice 		pg = NULL;
11015244eac9SBenno Rice 	} else {
110203b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
110303b6e025SPeter Grehan 		pg = m;
110459276937SPeter Grehan 		zone = moea_mpvo_zone;
11055244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
11065244eac9SBenno Rice 	}
1107f489bf21SAlan Cox 	if (pmap_bootstrapped)
11083653f5cbSAlan Cox 		rw_assert(&pvh_global_lock, RA_WLOCKED);
1109ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1110d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0 ||
1111d98d0ce2SKonstantin Belousov 	    VM_OBJECT_LOCKED(m->object),
11129ab6032fSAlan Cox 	    ("moea_enter_locked: page %p is not busy", m));
11135244eac9SBenno Rice 
11144dba5df1SPeter Grehan 	/* XXX change the pvo head for fake pages */
1115d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1116a130b35fSNathan Whitehorn 		pvo_flags &= ~PVO_MANAGED;
111759276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
1118a130b35fSNathan Whitehorn 		zone = moea_upvo_zone;
1119a130b35fSNathan Whitehorn 	}
11204dba5df1SPeter Grehan 
1121cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11225244eac9SBenno Rice 
112344b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11245244eac9SBenno Rice 		pte_lo |= PTE_BW;
11252368a371SAlan Cox 		if (pmap_bootstrapped &&
1126d98d0ce2SKonstantin Belousov 		    (m->oflags & VPO_UNMANAGED) == 0)
11273407fefeSKonstantin Belousov 			vm_page_aflag_set(m, PGA_WRITEABLE);
112844b8bd66SAlan Cox 	} else
11295244eac9SBenno Rice 		pte_lo |= PTE_BR;
11305244eac9SBenno Rice 
11314dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
11324dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
11335244eac9SBenno Rice 
11345244eac9SBenno Rice 	if (wired)
11355244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11365244eac9SBenno Rice 
113759276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11388207b362SBenno Rice 	    pte_lo, pvo_flags);
11395244eac9SBenno Rice 
11408207b362SBenno Rice 	/*
114157bd5cceSNathan Whitehorn 	 * Flush the real page from the instruction cache. This has be done
114257bd5cceSNathan Whitehorn 	 * for all user mappings to prevent information leakage via the
1143805bee55SNathan Whitehorn 	 * instruction cache. moea_pvo_enter() returns ENOENT for the first
1144805bee55SNathan Whitehorn 	 * mapping for a page.
11458207b362SBenno Rice 	 */
1146805bee55SNathan Whitehorn 	if (pmap != kernel_pmap && error == ENOENT &&
1147805bee55SNathan Whitehorn 	    (pte_lo & (PTE_I | PTE_G)) == 0)
114859276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1149ce142d9eSAlan Cox }
1150ce142d9eSAlan Cox 
1151ce142d9eSAlan Cox /*
1152ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1153ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1154ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1155ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1156ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1157ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1158ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1159ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1160ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1161ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1162ce142d9eSAlan Cox  */
1163ce142d9eSAlan Cox void
1164ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1165ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1166ce142d9eSAlan Cox {
1167ce142d9eSAlan Cox 	vm_page_t m;
1168ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1169ce142d9eSAlan Cox 
1170ce142d9eSAlan Cox 	psize = atop(end - start);
1171ce142d9eSAlan Cox 	m = m_start;
11723653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1173ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1174ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1175ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1176ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1177ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1178ce142d9eSAlan Cox 	}
11793653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1180ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
11815244eac9SBenno Rice }
11825244eac9SBenno Rice 
11832053c127SStephan Uphoff void
118459276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
11852053c127SStephan Uphoff     vm_prot_t prot)
1186dca96f1aSAlan Cox {
1187dca96f1aSAlan Cox 
11883653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1189ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1190ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
119159276937SPeter Grehan 	    FALSE);
11923653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1193ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1194dca96f1aSAlan Cox }
1195dca96f1aSAlan Cox 
119656b09388SAlan Cox vm_paddr_t
119759276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
11985244eac9SBenno Rice {
11990f92104cSBenno Rice 	struct	pvo_entry *pvo;
120048d0b1a0SAlan Cox 	vm_paddr_t pa;
12010f92104cSBenno Rice 
120248d0b1a0SAlan Cox 	PMAP_LOCK(pm);
120359276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
120448d0b1a0SAlan Cox 	if (pvo == NULL)
120548d0b1a0SAlan Cox 		pa = 0;
120648d0b1a0SAlan Cox 	else
120752a7870dSNathan Whitehorn 		pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
120848d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
120948d0b1a0SAlan Cox 	return (pa);
12105244eac9SBenno Rice }
12115244eac9SBenno Rice 
12125244eac9SBenno Rice /*
121384792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
121484792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
121584792e72SPeter Grehan  * protection.
121684792e72SPeter Grehan  */
121784792e72SPeter Grehan vm_page_t
121859276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
121984792e72SPeter Grehan {
1220ab50a262SAlan Cox 	struct	pvo_entry *pvo;
122184792e72SPeter Grehan 	vm_page_t m;
12222965a453SKip Macy         vm_paddr_t pa;
122384792e72SPeter Grehan 
122484792e72SPeter Grehan 	m = NULL;
12252965a453SKip Macy 	pa = 0;
122648d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
12272965a453SKip Macy retry:
122859276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
122952a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
123052a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1231ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
12322965a453SKip Macy 		if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa))
12332965a453SKip Macy 			goto retry;
123452a7870dSNathan Whitehorn 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
123584792e72SPeter Grehan 		vm_page_hold(m);
123684792e72SPeter Grehan 	}
12372965a453SKip Macy 	PA_UNLOCK_COND(pa);
123848d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
123984792e72SPeter Grehan 	return (m);
124084792e72SPeter Grehan }
124184792e72SPeter Grehan 
12425244eac9SBenno Rice void
124359276937SPeter Grehan moea_init(mmu_t mmu)
12445244eac9SBenno Rice {
12455244eac9SBenno Rice 
124659276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12470ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12480ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
124959276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12500ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12510ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
125259276937SPeter Grehan 	moea_initialized = TRUE;
12535244eac9SBenno Rice }
12545244eac9SBenno Rice 
12555244eac9SBenno Rice boolean_t
12567b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m)
12577b85f591SAlan Cox {
12588d9e6d9fSAlan Cox 	boolean_t rv;
12597b85f591SAlan Cox 
1260d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1261c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
12628d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
12638d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_REF);
12648d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
12658d9e6d9fSAlan Cox 	return (rv);
12667b85f591SAlan Cox }
12677b85f591SAlan Cox 
12687b85f591SAlan Cox boolean_t
126959276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
12705244eac9SBenno Rice {
12718d9e6d9fSAlan Cox 	boolean_t rv;
12720f92104cSBenno Rice 
1273d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1274567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1275567e51e1SAlan Cox 
1276567e51e1SAlan Cox 	/*
12773407fefeSKonstantin Belousov 	 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be
12783407fefeSKonstantin Belousov 	 * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
1279567e51e1SAlan Cox 	 * is clear, no PTEs can have PTE_CHG set.
1280567e51e1SAlan Cox 	 */
1281567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1282567e51e1SAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
12833407fefeSKonstantin Belousov 	    (m->aflags & PGA_WRITEABLE) == 0)
12840f92104cSBenno Rice 		return (FALSE);
12858d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
12868d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_CHG);
12878d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
12888d9e6d9fSAlan Cox 	return (rv);
1289566526a9SAlan Cox }
1290566526a9SAlan Cox 
1291e396eb60SAlan Cox boolean_t
1292e396eb60SAlan Cox moea_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1293e396eb60SAlan Cox {
1294e396eb60SAlan Cox 	struct pvo_entry *pvo;
1295e396eb60SAlan Cox 	boolean_t rv;
1296e396eb60SAlan Cox 
1297e396eb60SAlan Cox 	PMAP_LOCK(pmap);
1298e396eb60SAlan Cox 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1299e396eb60SAlan Cox 	rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0;
1300e396eb60SAlan Cox 	PMAP_UNLOCK(pmap);
1301e396eb60SAlan Cox 	return (rv);
1302e396eb60SAlan Cox }
1303e396eb60SAlan Cox 
13045244eac9SBenno Rice void
130559276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m)
13065244eac9SBenno Rice {
130703b6e025SPeter Grehan 
1308d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1309567e51e1SAlan Cox 	    ("moea_clear_reference: page %p is not managed", m));
13108d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1311ce186587SAlan Cox 	moea_clear_bit(m, PTE_REF);
13128d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
131303b6e025SPeter Grehan }
131403b6e025SPeter Grehan 
131503b6e025SPeter Grehan void
131659276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
131703b6e025SPeter Grehan {
131803b6e025SPeter Grehan 
1319d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1320567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
1321567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1322567e51e1SAlan Cox 	KASSERT((m->oflags & VPO_BUSY) == 0,
1323567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is busy", m));
1324567e51e1SAlan Cox 
1325567e51e1SAlan Cox 	/*
13263407fefeSKonstantin Belousov 	 * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_CHG
1327567e51e1SAlan Cox 	 * set.  If the object containing the page is locked and the page is
13283407fefeSKonstantin Belousov 	 * not VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set.
1329567e51e1SAlan Cox 	 */
13303407fefeSKonstantin Belousov 	if ((m->aflags & PGA_WRITEABLE) == 0)
133103b6e025SPeter Grehan 		return;
13328d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1333ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
13348d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13355244eac9SBenno Rice }
13365244eac9SBenno Rice 
13377f3a4093SMike Silbersack /*
133878985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
133978985e42SAlan Cox  */
134078985e42SAlan Cox void
134178985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
134278985e42SAlan Cox {
134378985e42SAlan Cox 	struct	pvo_entry *pvo;
134478985e42SAlan Cox 	struct	pte *pt;
134578985e42SAlan Cox 	pmap_t	pmap;
134678985e42SAlan Cox 	u_int	lo;
134778985e42SAlan Cox 
1348d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
13499ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
13509ab6032fSAlan Cox 
13519ab6032fSAlan Cox 	/*
13523407fefeSKonstantin Belousov 	 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by
13533407fefeSKonstantin Belousov 	 * another thread while the object is locked.  Thus, if PGA_WRITEABLE
13549ab6032fSAlan Cox 	 * is clear, no page table entries need updating.
13559ab6032fSAlan Cox 	 */
13569ab6032fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
13579ab6032fSAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
13583407fefeSKonstantin Belousov 	    (m->aflags & PGA_WRITEABLE) == 0)
135978985e42SAlan Cox 		return;
13603653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
136178985e42SAlan Cox 	lo = moea_attr_fetch(m);
1362e4f72b32SMarcel Moolenaar 	powerpc_sync();
136378985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
136478985e42SAlan Cox 		pmap = pvo->pvo_pmap;
136578985e42SAlan Cox 		PMAP_LOCK(pmap);
136652a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
136778985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
136852a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
136952a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
137078985e42SAlan Cox 			if (pt != NULL) {
137152a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
137252a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
137352a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
137452a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
137578985e42SAlan Cox 				    pvo->pvo_vaddr);
137678985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
137778985e42SAlan Cox 			}
137878985e42SAlan Cox 		}
137978985e42SAlan Cox 		PMAP_UNLOCK(pmap);
138078985e42SAlan Cox 	}
138178985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
138278985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
138378985e42SAlan Cox 		vm_page_dirty(m);
138478985e42SAlan Cox 	}
13853407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
13863653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
138778985e42SAlan Cox }
138878985e42SAlan Cox 
138978985e42SAlan Cox /*
139059276937SPeter Grehan  *	moea_ts_referenced:
13917f3a4093SMike Silbersack  *
13927f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
13937f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
13947f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
13957f3a4093SMike Silbersack  *	reference bits set.
13967f3a4093SMike Silbersack  *
13977f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
13987f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
13997f3a4093SMike Silbersack  *	optimal aging of shared pages.
14007f3a4093SMike Silbersack  */
14018d9e6d9fSAlan Cox int
140259276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
14035244eac9SBenno Rice {
14048d9e6d9fSAlan Cox 	int count;
140503b6e025SPeter Grehan 
1406d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1407ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
14088d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
14098d9e6d9fSAlan Cox 	count = moea_clear_bit(m, PTE_REF);
14108d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
14118d9e6d9fSAlan Cox 	return (count);
14125244eac9SBenno Rice }
14135244eac9SBenno Rice 
14145244eac9SBenno Rice /*
1415c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1416c1f4123bSNathan Whitehorn  */
1417c1f4123bSNathan Whitehorn void
1418c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma)
1419c1f4123bSNathan Whitehorn {
1420c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1421cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1422c1f4123bSNathan Whitehorn 	struct	pte *pt;
1423c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1424c1f4123bSNathan Whitehorn 	u_int	lo;
1425c1f4123bSNathan Whitehorn 
1426d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1427cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1428cd6a97f0SNathan Whitehorn 		return;
1429cd6a97f0SNathan Whitehorn 	}
1430cd6a97f0SNathan Whitehorn 
14313653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1432cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1433c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1434cd6a97f0SNathan Whitehorn 
1435cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1436c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1437c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1438c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1439c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1440c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1441c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1442c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1443c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1444c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1445c1f4123bSNathan Whitehorn 				isync();
1446c1f4123bSNathan Whitehorn 		}
1447c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1448c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1449c1f4123bSNathan Whitehorn 	}
1450c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
14513653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1452c1f4123bSNathan Whitehorn }
1453c1f4123bSNathan Whitehorn 
1454c1f4123bSNathan Whitehorn /*
14555244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
14565244eac9SBenno Rice  */
14575244eac9SBenno Rice void
145820b79612SRafal Jaworowski moea_kenter(mmu_t mmu, vm_offset_t va, vm_paddr_t pa)
14595244eac9SBenno Rice {
1460c1f4123bSNathan Whitehorn 
1461c1f4123bSNathan Whitehorn 	moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1462c1f4123bSNathan Whitehorn }
1463c1f4123bSNathan Whitehorn 
1464c1f4123bSNathan Whitehorn void
1465c1f4123bSNathan Whitehorn moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma)
1466c1f4123bSNathan Whitehorn {
14675244eac9SBenno Rice 	u_int		pte_lo;
14685244eac9SBenno Rice 	int		error;
14695244eac9SBenno Rice 
14705244eac9SBenno Rice #if 0
14715244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
147259276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
14735244eac9SBenno Rice 		    va);
14745244eac9SBenno Rice #endif
14755244eac9SBenno Rice 
1476c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
14775244eac9SBenno Rice 
14784711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
147959276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
148059276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
14815244eac9SBenno Rice 
14825244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
148359276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
14845244eac9SBenno Rice 		    pa, error);
14855244eac9SBenno Rice 
14864711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
14875244eac9SBenno Rice }
14885244eac9SBenno Rice 
1489e79f59e8SBenno Rice /*
1490e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1491e79f59e8SBenno Rice  * address.
1492e79f59e8SBenno Rice  */
149320b79612SRafal Jaworowski vm_paddr_t
149459276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
14955244eac9SBenno Rice {
1496e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
149748d0b1a0SAlan Cox 	vm_paddr_t pa;
1498e79f59e8SBenno Rice 
14990efd0097SPeter Grehan 	/*
150052a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
15010efd0097SPeter Grehan 	 */
15020efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
15030efd0097SPeter Grehan 		return (va);
15040efd0097SPeter Grehan 	}
15050efd0097SPeter Grehan 
150648d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
150759276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
150859276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
150952a7870dSNathan Whitehorn 	pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
151048d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
151148d0b1a0SAlan Cox 	return (pa);
1512e79f59e8SBenno Rice }
1513e79f59e8SBenno Rice 
151488afb2a3SBenno Rice /*
151588afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
151688afb2a3SBenno Rice  */
15175244eac9SBenno Rice void
151859276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
15195244eac9SBenno Rice {
152088afb2a3SBenno Rice 
152159276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
15225244eac9SBenno Rice }
15235244eac9SBenno Rice 
15245244eac9SBenno Rice /*
15255244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
15265244eac9SBenno Rice  *
15275244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
15285244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
15295244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
15305244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
15315244eac9SBenno Rice  * first usable address after the mapped region.
15325244eac9SBenno Rice  */
15335244eac9SBenno Rice vm_offset_t
153420b79612SRafal Jaworowski moea_map(mmu_t mmu, vm_offset_t *virt, vm_paddr_t pa_start,
153520b79612SRafal Jaworowski     vm_paddr_t pa_end, int prot)
15365244eac9SBenno Rice {
15375244eac9SBenno Rice 	vm_offset_t	sva, va;
15385244eac9SBenno Rice 
15395244eac9SBenno Rice 	sva = *virt;
15405244eac9SBenno Rice 	va = sva;
15415244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
154259276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
15435244eac9SBenno Rice 	*virt = va;
15445244eac9SBenno Rice 	return (sva);
15455244eac9SBenno Rice }
15465244eac9SBenno Rice 
15475244eac9SBenno Rice /*
15487f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
15497f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
15507f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
15517f3a4093SMike Silbersack  * is only necessary that true be returned for a small
15527f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
15537f3a4093SMike Silbersack  */
15545244eac9SBenno Rice boolean_t
155559276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
15565244eac9SBenno Rice {
155703b6e025SPeter Grehan         int loops;
155803b6e025SPeter Grehan 	struct pvo_entry *pvo;
1559ce186587SAlan Cox 	boolean_t rv;
156003b6e025SPeter Grehan 
1561d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1562ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
156303b6e025SPeter Grehan 	loops = 0;
1564ce186587SAlan Cox 	rv = FALSE;
15653653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
156603b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1567ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1568ce186587SAlan Cox 			rv = TRUE;
1569ce186587SAlan Cox 			break;
1570ce186587SAlan Cox 		}
157103b6e025SPeter Grehan 		if (++loops >= 16)
157203b6e025SPeter Grehan 			break;
157303b6e025SPeter Grehan 	}
15743653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1575ce186587SAlan Cox 	return (rv);
15765244eac9SBenno Rice }
15775244eac9SBenno Rice 
157859677d3cSAlan Cox /*
157959677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
158059677d3cSAlan Cox  * that are wired.
158159677d3cSAlan Cox  */
158259677d3cSAlan Cox int
158359677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
158459677d3cSAlan Cox {
158559677d3cSAlan Cox 	struct pvo_entry *pvo;
158659677d3cSAlan Cox 	int count;
158759677d3cSAlan Cox 
158859677d3cSAlan Cox 	count = 0;
1589d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0)
159059677d3cSAlan Cox 		return (count);
15913653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
159259677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
159359677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
159459677d3cSAlan Cox 			count++;
15953653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
159659677d3cSAlan Cox 	return (count);
159759677d3cSAlan Cox }
159859677d3cSAlan Cox 
159959276937SPeter Grehan static u_int	moea_vsidcontext;
16005244eac9SBenno Rice 
16015244eac9SBenno Rice void
160259276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
16035244eac9SBenno Rice {
16045244eac9SBenno Rice 	int	i, mask;
16055244eac9SBenno Rice 	u_int	entropy;
16065244eac9SBenno Rice 
160759276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
160848d0b1a0SAlan Cox 	PMAP_LOCK_INIT(pmap);
1609ccc4a5c7SNathan Whitehorn 	RB_INIT(&pmap->pmap_pvo);
16104daf20b2SPeter Grehan 
16115244eac9SBenno Rice 	entropy = 0;
16125244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
16135244eac9SBenno Rice 
161452a7870dSNathan Whitehorn 	if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
161552a7870dSNathan Whitehorn 	    == NULL) {
161652a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
161752a7870dSNathan Whitehorn 	}
161852a7870dSNathan Whitehorn 
161952a7870dSNathan Whitehorn 
1620e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
16215244eac9SBenno Rice 	/*
16225244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
16235244eac9SBenno Rice 	 */
16245244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
16255244eac9SBenno Rice 		u_int	hash, n;
16265244eac9SBenno Rice 
16275244eac9SBenno Rice 		/*
16285244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
16295244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
16305244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
16315244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
16325244eac9SBenno Rice 		 * instead of a multiply.)
16335244eac9SBenno Rice 		 */
163459276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
163559276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
16365244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
16375244eac9SBenno Rice 			continue;
16385244eac9SBenno Rice 		n = hash >> 5;
16395244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
164059276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
164159276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
16425244eac9SBenno Rice 			/* anything free in this bucket? */
164359276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
164459276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
16455244eac9SBenno Rice 				continue;
16465244eac9SBenno Rice 			}
16470dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
16485244eac9SBenno Rice 			mask = 1 << i;
16495244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
16505244eac9SBenno Rice 			hash |= i;
16515244eac9SBenno Rice 		}
165246e93cbbSNathan Whitehorn 		KASSERT(!(moea_vsid_bitmap[n] & mask),
165346e93cbbSNathan Whitehorn 		    ("Allocating in-use VSID group %#x\n", hash));
165459276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
16555244eac9SBenno Rice 		for (i = 0; i < 16; i++)
16565244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1657e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
16585244eac9SBenno Rice 		return;
16595244eac9SBenno Rice 	}
16605244eac9SBenno Rice 
1661e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
166259276937SPeter Grehan 	panic("moea_pinit: out of segments");
16635244eac9SBenno Rice }
16645244eac9SBenno Rice 
16655244eac9SBenno Rice /*
16665244eac9SBenno Rice  * Initialize the pmap associated with process 0.
16675244eac9SBenno Rice  */
16685244eac9SBenno Rice void
166959276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
16705244eac9SBenno Rice {
16715244eac9SBenno Rice 
167259276937SPeter Grehan 	moea_pinit(mmu, pm);
16735244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
16745244eac9SBenno Rice }
16755244eac9SBenno Rice 
1676e79f59e8SBenno Rice /*
1677e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1678e79f59e8SBenno Rice  */
16795244eac9SBenno Rice void
168059276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
168159276937SPeter Grehan     vm_prot_t prot)
16825244eac9SBenno Rice {
1683ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
1684e79f59e8SBenno Rice 	struct	pte *pt;
1685e79f59e8SBenno Rice 
1686e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
168759276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1688e79f59e8SBenno Rice 
1689e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
169059276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1691e79f59e8SBenno Rice 		return;
1692e79f59e8SBenno Rice 	}
1693e79f59e8SBenno Rice 
16943653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
169548d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1696ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1697ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1698ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1699ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1700e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1701e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1702e79f59e8SBenno Rice 
1703e79f59e8SBenno Rice 		/*
1704e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1705e79f59e8SBenno Rice 		 * copy.
1706e79f59e8SBenno Rice 		 */
1707ccc4a5c7SNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1708e79f59e8SBenno Rice 		/*
1709e79f59e8SBenno Rice 		 * Change the protection of the page.
1710e79f59e8SBenno Rice 		 */
171152a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
171252a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1713e79f59e8SBenno Rice 
1714e79f59e8SBenno Rice 		/*
1715e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1716e79f59e8SBenno Rice 		 */
1717d644a0b7SAlan Cox 		if (pt != NULL) {
171852a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1719d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1720d644a0b7SAlan Cox 		}
1721e79f59e8SBenno Rice 	}
17223653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
172348d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
17245244eac9SBenno Rice }
17255244eac9SBenno Rice 
172688afb2a3SBenno Rice /*
172788afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
172888afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
172988afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
173088afb2a3SBenno Rice  */
17315244eac9SBenno Rice void
173259276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
17335244eac9SBenno Rice {
173403b6e025SPeter Grehan 	vm_offset_t va;
17355244eac9SBenno Rice 
173603b6e025SPeter Grehan 	va = sva;
173703b6e025SPeter Grehan 	while (count-- > 0) {
173859276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
173903b6e025SPeter Grehan 		va += PAGE_SIZE;
174003b6e025SPeter Grehan 		m++;
174103b6e025SPeter Grehan 	}
17425244eac9SBenno Rice }
17435244eac9SBenno Rice 
174488afb2a3SBenno Rice /*
174588afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
174659276937SPeter Grehan  * temporary mappings entered by moea_qenter.
174788afb2a3SBenno Rice  */
17485244eac9SBenno Rice void
174959276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
17505244eac9SBenno Rice {
175103b6e025SPeter Grehan 	vm_offset_t va;
175288afb2a3SBenno Rice 
175303b6e025SPeter Grehan 	va = sva;
175403b6e025SPeter Grehan 	while (count-- > 0) {
175559276937SPeter Grehan 		moea_kremove(mmu, va);
175603b6e025SPeter Grehan 		va += PAGE_SIZE;
175703b6e025SPeter Grehan 	}
17585244eac9SBenno Rice }
17595244eac9SBenno Rice 
17605244eac9SBenno Rice void
176159276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
17625244eac9SBenno Rice {
176332bc7846SPeter Grehan         int idx, mask;
176432bc7846SPeter Grehan 
176532bc7846SPeter Grehan 	/*
176632bc7846SPeter Grehan 	 * Free segment register's VSID
176732bc7846SPeter Grehan 	 */
176832bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
176959276937SPeter Grehan                 panic("moea_release");
177032bc7846SPeter Grehan 
1771e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
177232bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
177332bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
177432bc7846SPeter Grehan         idx /= VSID_NBPW;
177559276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1776e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
177748d0b1a0SAlan Cox 	PMAP_LOCK_DESTROY(pmap);
17785244eac9SBenno Rice }
17795244eac9SBenno Rice 
178088afb2a3SBenno Rice /*
178188afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
178288afb2a3SBenno Rice  */
17835244eac9SBenno Rice void
178459276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
17855244eac9SBenno Rice {
1786ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
178788afb2a3SBenno Rice 
17883653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
178948d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1790ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1791ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1792ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1793ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1794598d99ddSNathan Whitehorn 		moea_pvo_remove(pvo, -1);
1795598d99ddSNathan Whitehorn 	}
179648d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
17973653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
17985244eac9SBenno Rice }
17995244eac9SBenno Rice 
1800e79f59e8SBenno Rice /*
180159276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
180203b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
180303b6e025SPeter Grehan  */
180403b6e025SPeter Grehan void
180559276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
180603b6e025SPeter Grehan {
180703b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
180803b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
180948d0b1a0SAlan Cox 	pmap_t	pmap;
181003b6e025SPeter Grehan 
18113653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
181203b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
181303b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
181403b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
181503b6e025SPeter Grehan 
181648d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
181748d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
181859276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
181948d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
182003b6e025SPeter Grehan 	}
18218d9e6d9fSAlan Cox 	if ((m->aflags & PGA_WRITEABLE) && moea_query_bit(m, PTE_CHG)) {
1822c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1823062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1824062c8f4cSNathan Whitehorn 	}
18253407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
18263653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
182703b6e025SPeter Grehan }
182803b6e025SPeter Grehan 
182903b6e025SPeter Grehan /*
18305244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
183159276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
18325244eac9SBenno Rice  * calculated.
18335244eac9SBenno Rice  */
18345244eac9SBenno Rice static vm_offset_t
183559276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
18365244eac9SBenno Rice {
18375244eac9SBenno Rice 	vm_offset_t	s, e;
18385244eac9SBenno Rice 	int		i, j;
18395244eac9SBenno Rice 
18405244eac9SBenno Rice 	size = round_page(size);
18415244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
18425244eac9SBenno Rice 		if (align != 0)
18435244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
18445244eac9SBenno Rice 		else
18455244eac9SBenno Rice 			s = phys_avail[i];
18465244eac9SBenno Rice 		e = s + size;
18475244eac9SBenno Rice 
18485244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
18495244eac9SBenno Rice 			continue;
18505244eac9SBenno Rice 
18515244eac9SBenno Rice 		if (s == phys_avail[i]) {
18525244eac9SBenno Rice 			phys_avail[i] += size;
18535244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
18545244eac9SBenno Rice 			phys_avail[i + 1] -= size;
18555244eac9SBenno Rice 		} else {
18565244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
18575244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
18585244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
18595244eac9SBenno Rice 			}
18605244eac9SBenno Rice 
18615244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
18625244eac9SBenno Rice 			phys_avail[i + 1] = s;
18635244eac9SBenno Rice 			phys_avail[i + 2] = e;
18645244eac9SBenno Rice 			phys_avail_count++;
18655244eac9SBenno Rice 		}
18665244eac9SBenno Rice 
18675244eac9SBenno Rice 		return (s);
18685244eac9SBenno Rice 	}
186959276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
18705244eac9SBenno Rice }
18715244eac9SBenno Rice 
18725244eac9SBenno Rice static void
187359276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
18745244eac9SBenno Rice {
18755244eac9SBenno Rice 	__syncicache((void *)pa, len);
18765244eac9SBenno Rice }
18775244eac9SBenno Rice 
18785244eac9SBenno Rice static int
187959276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
18805244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
18815244eac9SBenno Rice {
18825244eac9SBenno Rice 	struct	pvo_entry *pvo;
18835244eac9SBenno Rice 	u_int	sr;
18845244eac9SBenno Rice 	int	first;
18855244eac9SBenno Rice 	u_int	ptegidx;
18865244eac9SBenno Rice 	int	i;
188732bc7846SPeter Grehan 	int     bootstrap;
18885244eac9SBenno Rice 
188959276937SPeter Grehan 	moea_pvo_enter_calls++;
18908207b362SBenno Rice 	first = 0;
189132bc7846SPeter Grehan 	bootstrap = 0;
189232bc7846SPeter Grehan 
18935244eac9SBenno Rice 	/*
18945244eac9SBenno Rice 	 * Compute the PTE Group index.
18955244eac9SBenno Rice 	 */
18965244eac9SBenno Rice 	va &= ~ADDR_POFF;
18975244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
18985244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
18995244eac9SBenno Rice 
19005244eac9SBenno Rice 	/*
19015244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
19025244eac9SBenno Rice 	 * there is a mapping.
19035244eac9SBenno Rice 	 */
190459276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
190559276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19065244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
190752a7870dSNathan Whitehorn 			if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
190852a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1909fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
191059276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
191149f8f727SBenno Rice 				return (0);
1912fafc7362SBenno Rice 			}
191359276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
19145244eac9SBenno Rice 			break;
19155244eac9SBenno Rice 		}
19165244eac9SBenno Rice 	}
19175244eac9SBenno Rice 
19185244eac9SBenno Rice 	/*
19195244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
19205244eac9SBenno Rice 	 */
192159276937SPeter Grehan 	if (moea_initialized) {
1922378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
192349f8f727SBenno Rice 	} else {
192459276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
192559276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
192659276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
19270d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
192849f8f727SBenno Rice 		}
192959276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
193059276937SPeter Grehan 		moea_bpvo_pool_index++;
193132bc7846SPeter Grehan 		bootstrap = 1;
193249f8f727SBenno Rice 	}
19335244eac9SBenno Rice 
19345244eac9SBenno Rice 	if (pvo == NULL) {
193559276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
19365244eac9SBenno Rice 		return (ENOMEM);
19375244eac9SBenno Rice 	}
19385244eac9SBenno Rice 
193959276937SPeter Grehan 	moea_pvo_entries++;
19405244eac9SBenno Rice 	pvo->pvo_vaddr = va;
19415244eac9SBenno Rice 	pvo->pvo_pmap = pm;
194259276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
19435244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
19445244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
19455244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
19465244eac9SBenno Rice 	if (flags & PVO_WIRED)
19475244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
194859276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
19495244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
195032bc7846SPeter Grehan 	if (bootstrap)
195132bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
19524dba5df1SPeter Grehan 
195352a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
19545244eac9SBenno Rice 
19555244eac9SBenno Rice 	/*
1956598d99ddSNathan Whitehorn 	 * Add to pmap list
1957598d99ddSNathan Whitehorn 	 */
1958ccc4a5c7SNathan Whitehorn 	RB_INSERT(pvo_tree, &pm->pmap_pvo, pvo);
1959598d99ddSNathan Whitehorn 
1960598d99ddSNathan Whitehorn 	/*
19615244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
19625244eac9SBenno Rice 	 * item.
19635244eac9SBenno Rice 	 */
19648207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
19658207b362SBenno Rice 		first = 1;
19665244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
19674dba5df1SPeter Grehan 
196852a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
1969c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
1970c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
19715244eac9SBenno Rice 
19725244eac9SBenno Rice 	/*
19735244eac9SBenno Rice 	 * We hope this succeeds but it isn't required.
19745244eac9SBenno Rice 	 */
197552a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
19765244eac9SBenno Rice 	if (i >= 0) {
19775244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
19785244eac9SBenno Rice 	} else {
197959276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
198059276937SPeter Grehan 		moea_pte_overflow++;
19815244eac9SBenno Rice 	}
198259276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
19834dba5df1SPeter Grehan 
19845244eac9SBenno Rice 	return (first ? ENOENT : 0);
19855244eac9SBenno Rice }
19865244eac9SBenno Rice 
19875244eac9SBenno Rice static void
198859276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
19895244eac9SBenno Rice {
19905244eac9SBenno Rice 	struct	pte *pt;
19915244eac9SBenno Rice 
19925244eac9SBenno Rice 	/*
19935244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
19945244eac9SBenno Rice 	 * save the ref & cfg bits).
19955244eac9SBenno Rice 	 */
199659276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
19975244eac9SBenno Rice 	if (pt != NULL) {
199852a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1999d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
20005244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
20015244eac9SBenno Rice 	} else {
200259276937SPeter Grehan 		moea_pte_overflow--;
20035244eac9SBenno Rice 	}
20045244eac9SBenno Rice 
20055244eac9SBenno Rice 	/*
20065244eac9SBenno Rice 	 * Update our statistics.
20075244eac9SBenno Rice 	 */
20085244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
200952a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
20105244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
20115244eac9SBenno Rice 
20125244eac9SBenno Rice 	/*
20135244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
20145244eac9SBenno Rice 	 */
2015d98d0ce2SKonstantin Belousov 	if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED) {
20165244eac9SBenno Rice 		struct	vm_page *pg;
20175244eac9SBenno Rice 
201852a7870dSNathan Whitehorn 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
20195244eac9SBenno Rice 		if (pg != NULL) {
202052a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
20215244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
20225244eac9SBenno Rice 		}
20235244eac9SBenno Rice 	}
20245244eac9SBenno Rice 
20255244eac9SBenno Rice 	/*
2026598d99ddSNathan Whitehorn 	 * Remove this PVO from the PV and pmap lists.
20275244eac9SBenno Rice 	 */
20285244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
2029ccc4a5c7SNathan Whitehorn 	RB_REMOVE(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo);
20305244eac9SBenno Rice 
20315244eac9SBenno Rice 	/*
20325244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
20335244eac9SBenno Rice 	 * if we aren't going to reuse it.
20345244eac9SBenno Rice 	 */
20355244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
203649f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
203759276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
203859276937SPeter Grehan 		    moea_upvo_zone, pvo);
203959276937SPeter Grehan 	moea_pvo_entries--;
204059276937SPeter Grehan 	moea_pvo_remove_calls++;
20415244eac9SBenno Rice }
20425244eac9SBenno Rice 
20435244eac9SBenno Rice static __inline int
204459276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
20455244eac9SBenno Rice {
20465244eac9SBenno Rice 	int	pteidx;
20475244eac9SBenno Rice 
20485244eac9SBenno Rice 	/*
20495244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
20505244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
20515244eac9SBenno Rice 	 * noticing the HID bit.
20525244eac9SBenno Rice 	 */
20535244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
205452a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
205559276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
20565244eac9SBenno Rice 
20575244eac9SBenno Rice 	return (pteidx);
20585244eac9SBenno Rice }
20595244eac9SBenno Rice 
20605244eac9SBenno Rice static struct pvo_entry *
206159276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
20625244eac9SBenno Rice {
20635244eac9SBenno Rice 	struct	pvo_entry *pvo;
20645244eac9SBenno Rice 	int	ptegidx;
20655244eac9SBenno Rice 	u_int	sr;
20665244eac9SBenno Rice 
20675244eac9SBenno Rice 	va &= ~ADDR_POFF;
20685244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
20695244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
20705244eac9SBenno Rice 
207159276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
207259276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20735244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
20745244eac9SBenno Rice 			if (pteidx_p)
207559276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2076f489bf21SAlan Cox 			break;
20775244eac9SBenno Rice 		}
20785244eac9SBenno Rice 	}
207959276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20805244eac9SBenno Rice 
2081f489bf21SAlan Cox 	return (pvo);
20825244eac9SBenno Rice }
20835244eac9SBenno Rice 
20845244eac9SBenno Rice static struct pte *
208559276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
20865244eac9SBenno Rice {
20875244eac9SBenno Rice 	struct	pte *pt;
20885244eac9SBenno Rice 
20895244eac9SBenno Rice 	/*
20905244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
20915244eac9SBenno Rice 	 */
20925244eac9SBenno Rice 	if (pteidx == -1) {
20935244eac9SBenno Rice 		int	ptegidx;
20945244eac9SBenno Rice 		u_int	sr;
20955244eac9SBenno Rice 
20965244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
20975244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
209859276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
20995244eac9SBenno Rice 	}
21005244eac9SBenno Rice 
210159276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2102d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
21035244eac9SBenno Rice 
210452a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
210559276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
21065244eac9SBenno Rice 		    "valid pte index", pvo);
21075244eac9SBenno Rice 	}
21085244eac9SBenno Rice 
210952a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
211059276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
21115244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
21125244eac9SBenno Rice 	}
21135244eac9SBenno Rice 
211452a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
211552a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
211659276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
211759276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
21185244eac9SBenno Rice 		}
21195244eac9SBenno Rice 
212052a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
21215244eac9SBenno Rice 		    != 0) {
212259276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
212359276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
21245244eac9SBenno Rice 		}
21255244eac9SBenno Rice 
2126d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
21275244eac9SBenno Rice 		return (pt);
21285244eac9SBenno Rice 	}
21295244eac9SBenno Rice 
213052a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
213159276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
213259276937SPeter Grehan 		    "moea_pteg_table but valid in pvo", pvo, pt);
21335244eac9SBenno Rice 	}
21345244eac9SBenno Rice 
2135d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
21365244eac9SBenno Rice 	return (NULL);
21375244eac9SBenno Rice }
21385244eac9SBenno Rice 
21395244eac9SBenno Rice /*
21405244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
21415244eac9SBenno Rice  */
21425244eac9SBenno Rice int
214359276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
21445244eac9SBenno Rice {
21455244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
21465244eac9SBenno Rice 	struct	pvo_entry *pvo;
21475244eac9SBenno Rice 	int	ptegidx, i, j;
21485244eac9SBenno Rice 	u_int	sr;
21495244eac9SBenno Rice 	struct	pteg *pteg;
21505244eac9SBenno Rice 	struct	pte *pt;
21515244eac9SBenno Rice 
215259276937SPeter Grehan 	moea_pte_spills++;
21535244eac9SBenno Rice 
2154d080d5fdSBenno Rice 	sr = mfsrin(addr);
21555244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
21565244eac9SBenno Rice 
21575244eac9SBenno Rice 	/*
21585244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
21595244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
21605244eac9SBenno Rice 	 */
216159276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
216259276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
21635244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
21645244eac9SBenno Rice 	i &= 7;
21655244eac9SBenno Rice 	pt = &pteg->pt[i];
21665244eac9SBenno Rice 
21675244eac9SBenno Rice 	source_pvo = NULL;
21685244eac9SBenno Rice 	victim_pvo = NULL;
216959276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21705244eac9SBenno Rice 		/*
21715244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
21725244eac9SBenno Rice 		 */
21735244eac9SBenno Rice 		if (source_pvo == NULL &&
217452a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
217552a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
21765244eac9SBenno Rice 			/*
21775244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
21785244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
21795244eac9SBenno Rice 			 */
218052a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
21815244eac9SBenno Rice 
21825244eac9SBenno Rice 			if (j >= 0) {
21835244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
218459276937SPeter Grehan 				moea_pte_overflow--;
218559276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
21865244eac9SBenno Rice 				return (1);
21875244eac9SBenno Rice 			}
21885244eac9SBenno Rice 
21895244eac9SBenno Rice 			source_pvo = pvo;
21905244eac9SBenno Rice 
21915244eac9SBenno Rice 			if (victim_pvo != NULL)
21925244eac9SBenno Rice 				break;
21935244eac9SBenno Rice 		}
21945244eac9SBenno Rice 
21955244eac9SBenno Rice 		/*
21965244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
21975244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
21985244eac9SBenno Rice 		 */
21995244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
220052a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22015244eac9SBenno Rice 			victim_pvo = pvo;
22025244eac9SBenno Rice 			if (source_pvo != NULL)
22035244eac9SBenno Rice 				break;
22045244eac9SBenno Rice 		}
22055244eac9SBenno Rice 	}
22065244eac9SBenno Rice 
2207f489bf21SAlan Cox 	if (source_pvo == NULL) {
220859276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
22095244eac9SBenno Rice 		return (0);
2210f489bf21SAlan Cox 	}
22115244eac9SBenno Rice 
22125244eac9SBenno Rice 	if (victim_pvo == NULL) {
22135244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
221459276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
22155244eac9SBenno Rice 			    "entry", pt);
22165244eac9SBenno Rice 
22175244eac9SBenno Rice 		/*
22185244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
22195244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
22205244eac9SBenno Rice 		 */
222159276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
22225244eac9SBenno Rice 		    pvo_olink) {
22235244eac9SBenno Rice 			/*
22245244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
22255244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
22265244eac9SBenno Rice 			 */
222752a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22285244eac9SBenno Rice 				victim_pvo = pvo;
22295244eac9SBenno Rice 				break;
22305244eac9SBenno Rice 			}
22315244eac9SBenno Rice 		}
22325244eac9SBenno Rice 
22335244eac9SBenno Rice 		if (victim_pvo == NULL)
223459276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
22355244eac9SBenno Rice 			    "entry", pt);
22365244eac9SBenno Rice 	}
22375244eac9SBenno Rice 
22385244eac9SBenno Rice 	/*
22395244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
22405244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
22415244eac9SBenno Rice 	 * contained in the TLB entry.
22425244eac9SBenno Rice 	 */
224352a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
22445244eac9SBenno Rice 
224552a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
224652a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
22475244eac9SBenno Rice 
22485244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
22495244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
225059276937SPeter Grehan 	moea_pte_replacements++;
22515244eac9SBenno Rice 
225259276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22535244eac9SBenno Rice 	return (1);
22545244eac9SBenno Rice }
22555244eac9SBenno Rice 
22565244eac9SBenno Rice static int
225759276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
22585244eac9SBenno Rice {
22595244eac9SBenno Rice 	struct	pte *pt;
22605244eac9SBenno Rice 	int	i;
22615244eac9SBenno Rice 
2262d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2263d644a0b7SAlan Cox 
22645244eac9SBenno Rice 	/*
22655244eac9SBenno Rice 	 * First try primary hash.
22665244eac9SBenno Rice 	 */
226759276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22685244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22695244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
227059276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22715244eac9SBenno Rice 			return (i);
22725244eac9SBenno Rice 		}
22735244eac9SBenno Rice 	}
22745244eac9SBenno Rice 
22755244eac9SBenno Rice 	/*
22765244eac9SBenno Rice 	 * Now try secondary hash.
22775244eac9SBenno Rice 	 */
227859276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2279bd8e6f87SPeter Grehan 
228059276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22815244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22825244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
228359276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22845244eac9SBenno Rice 			return (i);
22855244eac9SBenno Rice 		}
22865244eac9SBenno Rice 	}
22875244eac9SBenno Rice 
228859276937SPeter Grehan 	panic("moea_pte_insert: overflow");
22895244eac9SBenno Rice 	return (-1);
22905244eac9SBenno Rice }
22915244eac9SBenno Rice 
22925244eac9SBenno Rice static boolean_t
229359276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
22945244eac9SBenno Rice {
22955244eac9SBenno Rice 	struct	pvo_entry *pvo;
22965244eac9SBenno Rice 	struct	pte *pt;
22975244eac9SBenno Rice 
22988d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
229959276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
23005244eac9SBenno Rice 		return (TRUE);
23015244eac9SBenno Rice 
23025244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
23035244eac9SBenno Rice 
23045244eac9SBenno Rice 		/*
23055244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
23065244eac9SBenno Rice 		 * success.
23075244eac9SBenno Rice 		 */
230852a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
230959276937SPeter Grehan 			moea_attr_save(m, ptebit);
23105244eac9SBenno Rice 			return (TRUE);
23115244eac9SBenno Rice 		}
23125244eac9SBenno Rice 	}
23135244eac9SBenno Rice 
23145244eac9SBenno Rice 	/*
23155244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
23165244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
23175244eac9SBenno Rice 	 * the PTEs.
23185244eac9SBenno Rice 	 */
2319e4f72b32SMarcel Moolenaar 	powerpc_sync();
23205244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
23215244eac9SBenno Rice 
23225244eac9SBenno Rice 		/*
23235244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
23245244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
23255244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
23265244eac9SBenno Rice 		 */
232759276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23285244eac9SBenno Rice 		if (pt != NULL) {
232952a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2330d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
233152a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
233259276937SPeter Grehan 				moea_attr_save(m, ptebit);
23335244eac9SBenno Rice 				return (TRUE);
23345244eac9SBenno Rice 			}
23355244eac9SBenno Rice 		}
23365244eac9SBenno Rice 	}
23375244eac9SBenno Rice 
23384f7daed0SAndrew Gallatin 	return (FALSE);
23395244eac9SBenno Rice }
23405244eac9SBenno Rice 
234103b6e025SPeter Grehan static u_int
2342ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
23435244eac9SBenno Rice {
234403b6e025SPeter Grehan 	u_int	count;
23455244eac9SBenno Rice 	struct	pvo_entry *pvo;
23465244eac9SBenno Rice 	struct	pte *pt;
2347ce186587SAlan Cox 
23488d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
23495244eac9SBenno Rice 
23505244eac9SBenno Rice 	/*
23515244eac9SBenno Rice 	 * Clear the cached value.
23525244eac9SBenno Rice 	 */
235359276937SPeter Grehan 	moea_attr_clear(m, ptebit);
23545244eac9SBenno Rice 
23555244eac9SBenno Rice 	/*
23565244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
23575244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
23585244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
23595244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
23605244eac9SBenno Rice 	 * REF/CHG bits.
23615244eac9SBenno Rice 	 */
2362e4f72b32SMarcel Moolenaar 	powerpc_sync();
23635244eac9SBenno Rice 
23645244eac9SBenno Rice 	/*
23655244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
23665244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
23675244eac9SBenno Rice 	 */
236803b6e025SPeter Grehan 	count = 0;
23695244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
237059276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23715244eac9SBenno Rice 		if (pt != NULL) {
237252a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
237352a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
237403b6e025SPeter Grehan 				count++;
237559276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
23765244eac9SBenno Rice 			}
2377d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
237803b6e025SPeter Grehan 		}
237952a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
23805244eac9SBenno Rice 	}
23815244eac9SBenno Rice 
238203b6e025SPeter Grehan 	return (count);
2383bdf71f56SBenno Rice }
23848bbfa33aSBenno Rice 
23858bbfa33aSBenno Rice /*
238632bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
238732bc7846SPeter Grehan  */
238832bc7846SPeter Grehan static int
238959276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
239032bc7846SPeter Grehan {
239132bc7846SPeter Grehan 	u_int prot;
239232bc7846SPeter Grehan 	u_int32_t start;
239332bc7846SPeter Grehan 	u_int32_t end;
239432bc7846SPeter Grehan 	u_int32_t bat_ble;
239532bc7846SPeter Grehan 
239632bc7846SPeter Grehan 	/*
239732bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
239832bc7846SPeter Grehan 	 */
2399c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
240032bc7846SPeter Grehan 		return (EINVAL);
240132bc7846SPeter Grehan 
240232bc7846SPeter Grehan 	/*
240332bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
240432bc7846SPeter Grehan 	 * so it can function as an i/o page
240532bc7846SPeter Grehan 	 */
240632bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
240732bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
240832bc7846SPeter Grehan 		return (EPERM);
240932bc7846SPeter Grehan 
241032bc7846SPeter Grehan 	/*
241132bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
241232bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
241332bc7846SPeter Grehan 	 * not requiring masking)
241432bc7846SPeter Grehan 	 */
241532bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
241632bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
241732bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
241832bc7846SPeter Grehan 
241932bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
242032bc7846SPeter Grehan 		return (ERANGE);
242132bc7846SPeter Grehan 
242232bc7846SPeter Grehan 	return (0);
242332bc7846SPeter Grehan }
242432bc7846SPeter Grehan 
242559276937SPeter Grehan boolean_t
242620b79612SRafal Jaworowski moea_dev_direct_mapped(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2427c0763d37SSuleiman Souhlal {
2428c0763d37SSuleiman Souhlal 	int i;
2429c0763d37SSuleiman Souhlal 
2430c0763d37SSuleiman Souhlal 	/*
2431c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2432c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2433c0763d37SSuleiman Souhlal 	 */
2434c0763d37SSuleiman Souhlal 
2435c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
243659276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2437c0763d37SSuleiman Souhlal 			return (0);
2438c0763d37SSuleiman Souhlal 
2439c0763d37SSuleiman Souhlal 	return (EFAULT);
2440c0763d37SSuleiman Souhlal }
244132bc7846SPeter Grehan 
244232bc7846SPeter Grehan /*
24438bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
24448bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
24458bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
24468bbfa33aSBenno Rice  * NOT real memory.
24478bbfa33aSBenno Rice  */
24488bbfa33aSBenno Rice void *
244920b79612SRafal Jaworowski moea_mapdev(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
24508bbfa33aSBenno Rice {
2451c1f4123bSNathan Whitehorn 
2452c1f4123bSNathan Whitehorn 	return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2453c1f4123bSNathan Whitehorn }
2454c1f4123bSNathan Whitehorn 
2455c1f4123bSNathan Whitehorn void *
2456c1f4123bSNathan Whitehorn moea_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma)
2457c1f4123bSNathan Whitehorn {
245832bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
245932bc7846SPeter Grehan 	int i;
24608bbfa33aSBenno Rice 
246132bc7846SPeter Grehan 	ppa = trunc_page(pa);
24628bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
24638bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
24648bbfa33aSBenno Rice 
246532bc7846SPeter Grehan 	/*
246632bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
246732bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
246832bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
246932bc7846SPeter Grehan 	 */
247032bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
247159276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
247232bc7846SPeter Grehan 			return ((void *) pa);
247332bc7846SPeter Grehan 	}
247432bc7846SPeter Grehan 
2475e53f32acSAlan Cox 	va = kmem_alloc_nofault(kernel_map, size);
24768bbfa33aSBenno Rice 	if (!va)
247759276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
24788bbfa33aSBenno Rice 
24798bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
2480c1f4123bSNathan Whitehorn 		moea_kenter_attr(mmu, tmpva, ppa, ma);
2481e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
24828bbfa33aSBenno Rice 		size -= PAGE_SIZE;
24838bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
248432bc7846SPeter Grehan 		ppa += PAGE_SIZE;
24858bbfa33aSBenno Rice 	}
24868bbfa33aSBenno Rice 
24878bbfa33aSBenno Rice 	return ((void *)(va + offset));
24888bbfa33aSBenno Rice }
24898bbfa33aSBenno Rice 
24908bbfa33aSBenno Rice void
249159276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
24928bbfa33aSBenno Rice {
24938bbfa33aSBenno Rice 	vm_offset_t base, offset;
24948bbfa33aSBenno Rice 
249532bc7846SPeter Grehan 	/*
249632bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
249732bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
249832bc7846SPeter Grehan 	 */
2499ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
25008bbfa33aSBenno Rice 		base = trunc_page(va);
25018bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
25028bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
25038bbfa33aSBenno Rice 		kmem_free(kernel_map, base, size);
25048bbfa33aSBenno Rice 	}
250532bc7846SPeter Grehan }
25061a4fcaebSMarcel Moolenaar 
25071a4fcaebSMarcel Moolenaar static void
25081a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
25091a4fcaebSMarcel Moolenaar {
25101a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
25111a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
25121a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
25131a4fcaebSMarcel Moolenaar 	vm_size_t len;
25141a4fcaebSMarcel Moolenaar 
25151a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
25161a4fcaebSMarcel Moolenaar 	while (sz > 0) {
25171a4fcaebSMarcel Moolenaar 		lim = round_page(va);
25181a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
25191a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
25201a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
25211a4fcaebSMarcel Moolenaar 			pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
25221a4fcaebSMarcel Moolenaar 			    (va & ADDR_POFF);
25231a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
25241a4fcaebSMarcel Moolenaar 		}
25251a4fcaebSMarcel Moolenaar 		va += len;
25261a4fcaebSMarcel Moolenaar 		sz -= len;
25271a4fcaebSMarcel Moolenaar 	}
25281a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
25291a4fcaebSMarcel Moolenaar }
2530