xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision c4bcebed17116bbe16ca12e73e98ca1827ee0822)
160727d8bSWarner Losh /*-
25244eac9SBenno Rice  * Copyright (c) 2001 The NetBSD Foundation, Inc.
35244eac9SBenno Rice  * All rights reserved.
45244eac9SBenno Rice  *
55244eac9SBenno Rice  * This code is derived from software contributed to The NetBSD Foundation
65244eac9SBenno Rice  * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
75244eac9SBenno Rice  *
85244eac9SBenno Rice  * Redistribution and use in source and binary forms, with or without
95244eac9SBenno Rice  * modification, are permitted provided that the following conditions
105244eac9SBenno Rice  * are met:
115244eac9SBenno Rice  * 1. Redistributions of source code must retain the above copyright
125244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer.
135244eac9SBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
145244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer in the
155244eac9SBenno Rice  *    documentation and/or other materials provided with the distribution.
165244eac9SBenno Rice  * 3. All advertising materials mentioning features or use of this software
175244eac9SBenno Rice  *    must display the following acknowledgement:
185244eac9SBenno Rice  *        This product includes software developed by the NetBSD
195244eac9SBenno Rice  *        Foundation, Inc. and its contributors.
205244eac9SBenno Rice  * 4. Neither the name of The NetBSD Foundation nor the names of its
215244eac9SBenno Rice  *    contributors may be used to endorse or promote products derived
225244eac9SBenno Rice  *    from this software without specific prior written permission.
235244eac9SBenno Rice  *
245244eac9SBenno Rice  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
255244eac9SBenno Rice  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
265244eac9SBenno Rice  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
275244eac9SBenno Rice  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
285244eac9SBenno Rice  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
295244eac9SBenno Rice  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
305244eac9SBenno Rice  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
315244eac9SBenno Rice  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
325244eac9SBenno Rice  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
335244eac9SBenno Rice  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
345244eac9SBenno Rice  * POSSIBILITY OF SUCH DAMAGE.
355244eac9SBenno Rice  */
3660727d8bSWarner Losh /*-
37f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
38f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 TooLs GmbH.
39f9bac91bSBenno Rice  * All rights reserved.
40f9bac91bSBenno Rice  *
41f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
42f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
43f9bac91bSBenno Rice  * are met:
44f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
45f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
46f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
47f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
48f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
49f9bac91bSBenno Rice  * 3. All advertising materials mentioning features or use of this software
50f9bac91bSBenno Rice  *    must display the following acknowledgement:
51f9bac91bSBenno Rice  *	This product includes software developed by TooLs GmbH.
52f9bac91bSBenno Rice  * 4. The name of TooLs GmbH may not be used to endorse or promote products
53f9bac91bSBenno Rice  *    derived from this software without specific prior written permission.
54f9bac91bSBenno Rice  *
55f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
56f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
61f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
62f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
63f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
64f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65f9bac91bSBenno Rice  *
66111c77dcSBenno Rice  * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
67f9bac91bSBenno Rice  */
6860727d8bSWarner Losh /*-
69f9bac91bSBenno Rice  * Copyright (C) 2001 Benno Rice.
70f9bac91bSBenno Rice  * All rights reserved.
71f9bac91bSBenno Rice  *
72f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
73f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
74f9bac91bSBenno Rice  * are met:
75f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
76f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
77f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
78f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
79f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
80f9bac91bSBenno Rice  *
81f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
82f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
85f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
86f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
87f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
88f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
89f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
90f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91f9bac91bSBenno Rice  */
92f9bac91bSBenno Rice 
938368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
948368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
95f9bac91bSBenno Rice 
965244eac9SBenno Rice /*
975244eac9SBenno Rice  * Manages physical address maps.
985244eac9SBenno Rice  *
995244eac9SBenno Rice  * In addition to hardware address maps, this module is called upon to
1005244eac9SBenno Rice  * provide software-use-only maps which may or may not be stored in the
1015244eac9SBenno Rice  * same form as hardware maps.  These pseudo-maps are used to store
1025244eac9SBenno Rice  * intermediate results from copy operations to and from address spaces.
1035244eac9SBenno Rice  *
1045244eac9SBenno Rice  * Since the information managed by this module is also stored by the
1055244eac9SBenno Rice  * logical address mapping module, this module may throw away valid virtual
1065244eac9SBenno Rice  * to physical mappings at almost any time.  However, invalidations of
1075244eac9SBenno Rice  * mappings must be done as requested.
1085244eac9SBenno Rice  *
1095244eac9SBenno Rice  * In order to cope with hardware architectures which make virtual to
1105244eac9SBenno Rice  * physical map invalidates expensive, this module may delay invalidate
1115244eac9SBenno Rice  * reduced protection operations until such time as they are actually
1125244eac9SBenno Rice  * necessary.  This module is given full information as to which processors
1135244eac9SBenno Rice  * are currently using which maps, and to when physical maps must be made
1145244eac9SBenno Rice  * correct.
1155244eac9SBenno Rice  */
1165244eac9SBenno Rice 
117ad7a226fSPeter Wemm #include "opt_kstack_pages.h"
118ad7a226fSPeter Wemm 
119f9bac91bSBenno Rice #include <sys/param.h>
1200b27d710SPeter Wemm #include <sys/kernel.h>
1215244eac9SBenno Rice #include <sys/ktr.h>
12294e0b85eSMark Peek #include <sys/lock.h>
1235244eac9SBenno Rice #include <sys/msgbuf.h>
124f9bac91bSBenno Rice #include <sys/mutex.h>
1255244eac9SBenno Rice #include <sys/proc.h>
1265244eac9SBenno Rice #include <sys/sysctl.h>
1275244eac9SBenno Rice #include <sys/systm.h>
1285244eac9SBenno Rice #include <sys/vmmeter.h>
1295244eac9SBenno Rice 
1305244eac9SBenno Rice #include <dev/ofw/openfirm.h>
131f9bac91bSBenno Rice 
132f9bac91bSBenno Rice #include <vm/vm.h>
133f9bac91bSBenno Rice #include <vm/vm_param.h>
134f9bac91bSBenno Rice #include <vm/vm_kern.h>
135f9bac91bSBenno Rice #include <vm/vm_page.h>
136f9bac91bSBenno Rice #include <vm/vm_map.h>
137f9bac91bSBenno Rice #include <vm/vm_object.h>
138f9bac91bSBenno Rice #include <vm/vm_extern.h>
139f9bac91bSBenno Rice #include <vm/vm_pageout.h>
140f9bac91bSBenno Rice #include <vm/vm_pager.h>
141378862a7SJeff Roberson #include <vm/uma.h>
142f9bac91bSBenno Rice 
1437c277971SPeter Grehan #include <machine/cpu.h>
144b40ce02aSNathan Whitehorn #include <machine/platform.h>
145d699b539SMark Peek #include <machine/bat.h>
1465244eac9SBenno Rice #include <machine/frame.h>
1475244eac9SBenno Rice #include <machine/md_var.h>
1485244eac9SBenno Rice #include <machine/psl.h>
149f9bac91bSBenno Rice #include <machine/pte.h>
15012640815SMarcel Moolenaar #include <machine/smp.h>
1515244eac9SBenno Rice #include <machine/sr.h>
15259276937SPeter Grehan #include <machine/mmuvar.h>
153f9bac91bSBenno Rice 
15459276937SPeter Grehan #include "mmu_if.h"
15559276937SPeter Grehan 
15659276937SPeter Grehan #define	MOEA_DEBUG
157f9bac91bSBenno Rice 
1585244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
159f9bac91bSBenno Rice 
1605244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1615244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1625244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1635244eac9SBenno Rice 
1644dba5df1SPeter Grehan #define	PVO_PTEGIDX_MASK	0x007		/* which PTEG slot */
1654dba5df1SPeter Grehan #define	PVO_PTEGIDX_VALID	0x008		/* slot is valid */
1664dba5df1SPeter Grehan #define	PVO_WIRED		0x010		/* PVO entry is wired */
1674dba5df1SPeter Grehan #define	PVO_MANAGED		0x020		/* PVO entry is managed */
1684dba5df1SPeter Grehan #define	PVO_EXECUTABLE		0x040		/* PVO entry is executable */
1694dba5df1SPeter Grehan #define	PVO_BOOTSTRAP		0x080		/* PVO entry allocated during
17049f8f727SBenno Rice 						   bootstrap */
1714dba5df1SPeter Grehan #define PVO_FAKE		0x100		/* fictitious phys page */
1725244eac9SBenno Rice #define	PVO_VADDR(pvo)		((pvo)->pvo_vaddr & ~ADDR_POFF)
1735244eac9SBenno Rice #define	PVO_ISEXECUTABLE(pvo)	((pvo)->pvo_vaddr & PVO_EXECUTABLE)
1744dba5df1SPeter Grehan #define PVO_ISFAKE(pvo)		((pvo)->pvo_vaddr & PVO_FAKE)
1755244eac9SBenno Rice #define	PVO_PTEGIDX_GET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK)
1765244eac9SBenno Rice #define	PVO_PTEGIDX_ISSET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID)
1775244eac9SBenno Rice #define	PVO_PTEGIDX_CLR(pvo)	\
1785244eac9SBenno Rice 	((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK)))
1795244eac9SBenno Rice #define	PVO_PTEGIDX_SET(pvo, i)	\
1805244eac9SBenno Rice 	((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID))
1815244eac9SBenno Rice 
18259276937SPeter Grehan #define	MOEA_PVO_CHECK(pvo)
1835244eac9SBenno Rice 
1845244eac9SBenno Rice struct ofw_map {
1855244eac9SBenno Rice 	vm_offset_t	om_va;
1865244eac9SBenno Rice 	vm_size_t	om_len;
1875244eac9SBenno Rice 	vm_offset_t	om_pa;
1885244eac9SBenno Rice 	u_int		om_mode;
1895244eac9SBenno Rice };
190f9bac91bSBenno Rice 
1915244eac9SBenno Rice /*
1925244eac9SBenno Rice  * Map of physical memory regions.
1935244eac9SBenno Rice  */
19431c82d03SBenno Rice static struct	mem_region *regions;
19531c82d03SBenno Rice static struct	mem_region *pregions;
196c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
197c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
198aa39961eSBenno Rice static struct	ofw_map *translations;
1995244eac9SBenno Rice 
2005244eac9SBenno Rice extern struct pmap ofw_pmap;
201f9bac91bSBenno Rice 
202f9bac91bSBenno Rice /*
203f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
204f489bf21SAlan Cox  */
20559276937SPeter Grehan struct mtx	moea_table_mutex;
206e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
207f489bf21SAlan Cox 
208e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
209e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
210e4f72b32SMarcel Moolenaar 
211f489bf21SAlan Cox /*
2125244eac9SBenno Rice  * PTEG data.
213f9bac91bSBenno Rice  */
21459276937SPeter Grehan static struct	pteg *moea_pteg_table;
21559276937SPeter Grehan u_int		moea_pteg_count;
21659276937SPeter Grehan u_int		moea_pteg_mask;
2175244eac9SBenno Rice 
2185244eac9SBenno Rice /*
2195244eac9SBenno Rice  * PVO data.
2205244eac9SBenno Rice  */
22159276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
22259276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
22359276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
2245244eac9SBenno Rice 
22559276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
22659276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2275244eac9SBenno Rice 
2280d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
22959276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
23059276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2315244eac9SBenno Rice 
2325244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
23359276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2345244eac9SBenno Rice 
23559276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2365244eac9SBenno Rice 
2375244eac9SBenno Rice /*
2385244eac9SBenno Rice  * Statistics.
2395244eac9SBenno Rice  */
24059276937SPeter Grehan u_int	moea_pte_valid = 0;
24159276937SPeter Grehan u_int	moea_pte_overflow = 0;
24259276937SPeter Grehan u_int	moea_pte_replacements = 0;
24359276937SPeter Grehan u_int	moea_pvo_entries = 0;
24459276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
24559276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
24659276937SPeter Grehan u_int	moea_pte_spills = 0;
24759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2485244eac9SBenno Rice     0, "");
24959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
25059276937SPeter Grehan     &moea_pte_overflow, 0, "");
25159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
25259276937SPeter Grehan     &moea_pte_replacements, 0, "");
25359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2545244eac9SBenno Rice     0, "");
25559276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
25659276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
25759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
25859276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
25959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
26059276937SPeter Grehan     &moea_pte_spills, 0, "");
2615244eac9SBenno Rice 
2625244eac9SBenno Rice /*
26359276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2645244eac9SBenno Rice  */
26559276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2665244eac9SBenno Rice 
2675244eac9SBenno Rice /*
2685244eac9SBenno Rice  * PTE calls.
2695244eac9SBenno Rice  */
27059276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2715244eac9SBenno Rice 
2725244eac9SBenno Rice /*
2735244eac9SBenno Rice  * PVO calls.
2745244eac9SBenno Rice  */
27559276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2765244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
27759276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
27859276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
27959276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2805244eac9SBenno Rice 
2815244eac9SBenno Rice /*
2825244eac9SBenno Rice  * Utility routines.
2835244eac9SBenno Rice  */
284ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
285ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
28659276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
28759276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
288ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
28959276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
29059276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
29159276937SPeter Grehan 
29259276937SPeter Grehan /*
29359276937SPeter Grehan  * Kernel MMU interface
29459276937SPeter Grehan  */
29559276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
29659276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
29759276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t);
29859276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
29959276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
300ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
301ce142d9eSAlan Cox     vm_prot_t);
3022053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
30359276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
30459276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
30559276937SPeter Grehan void moea_init(mmu_t);
30659276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
3077b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t);
30859276937SPeter Grehan boolean_t moea_ts_referenced(mmu_t, vm_page_t);
30959276937SPeter Grehan vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int);
31059276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
31159677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
31259276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
31359276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
31459276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
31559276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
31659276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
31759276937SPeter Grehan void moea_release(mmu_t, pmap_t);
31859276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
31959276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
32078985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
32159276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
32259276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
32359276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
32459276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
32559276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
3261c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int);
32759276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
32859276937SPeter Grehan void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t);
329c1f4123bSNathan Whitehorn void *moea_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t);
33059276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
33159276937SPeter Grehan vm_offset_t moea_kextract(mmu_t, vm_offset_t);
332c1f4123bSNathan Whitehorn void moea_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t);
33359276937SPeter Grehan void moea_kenter(mmu_t, vm_offset_t, vm_offset_t);
334c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma);
33559276937SPeter Grehan boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
3361a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
33759276937SPeter Grehan 
33859276937SPeter Grehan static mmu_method_t moea_methods[] = {
33959276937SPeter Grehan 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
34059276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
34159276937SPeter Grehan 	MMUMETHOD(mmu_clear_reference,	moea_clear_reference),
34259276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
34359276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
344ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
34559276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
34659276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
34759276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
34859276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
34959276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
3507b85f591SAlan Cox 	MMUMETHOD(mmu_is_referenced,	moea_is_referenced),
35159276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
35259276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
35359276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
35459677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
35559276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
35659276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
35759276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
35859276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
35959276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
36059276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
36159276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
36259276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
36378985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
3641a4fcaebSMarcel Moolenaar 	MMUMETHOD(mmu_sync_icache,	moea_sync_icache),
36559276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
36659276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
36759276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
36859276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
36959276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
370c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_page_set_memattr,	moea_page_set_memattr),
37159276937SPeter Grehan 
37259276937SPeter Grehan 	/* Internal interfaces */
37359276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
3741c96bdd1SNathan Whitehorn 	MMUMETHOD(mmu_cpu_bootstrap,   	moea_cpu_bootstrap),
375c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_mapdev_attr,	moea_mapdev_attr),
37659276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
37759276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
37859276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
37959276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
380c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_kenter_attr,	moea_kenter_attr),
38159276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
38259276937SPeter Grehan 
38359276937SPeter Grehan 	{ 0, 0 }
38459276937SPeter Grehan };
38559276937SPeter Grehan 
38633529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0);
38733529b98SPeter Grehan 
388c1f4123bSNathan Whitehorn static __inline uint32_t
389c1f4123bSNathan Whitehorn moea_calc_wimg(vm_offset_t pa, vm_memattr_t ma)
390c1f4123bSNathan Whitehorn {
391c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
392c1f4123bSNathan Whitehorn 	int i;
393c1f4123bSNathan Whitehorn 
394c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
395c1f4123bSNathan Whitehorn 		switch (ma) {
396c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
397c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
398c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
399c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
400c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
401c1f4123bSNathan Whitehorn 			return (PTE_I);
402c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
403c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
404c1f4123bSNathan Whitehorn 		}
405c1f4123bSNathan Whitehorn 	}
406c1f4123bSNathan Whitehorn 
407c1f4123bSNathan Whitehorn 	/*
408c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
409c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
410c1f4123bSNathan Whitehorn 	 */
411c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
412c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
413c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
414c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
415c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
416c1f4123bSNathan Whitehorn 			break;
417c1f4123bSNathan Whitehorn 		}
418c1f4123bSNathan Whitehorn 	}
419c1f4123bSNathan Whitehorn 
420c1f4123bSNathan Whitehorn 	return pte_lo;
421c1f4123bSNathan Whitehorn }
42259276937SPeter Grehan 
423e4f72b32SMarcel Moolenaar static void
424e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
425e4f72b32SMarcel Moolenaar {
426e4f72b32SMarcel Moolenaar 
427e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
42894363f53SNathan Whitehorn 	__asm __volatile("ptesync");
429e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
43094363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
431e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
432e4f72b32SMarcel Moolenaar }
433e4f72b32SMarcel Moolenaar 
434e4f72b32SMarcel Moolenaar static void
435e4f72b32SMarcel Moolenaar tlbia(void)
436e4f72b32SMarcel Moolenaar {
437e4f72b32SMarcel Moolenaar 	vm_offset_t va;
438e4f72b32SMarcel Moolenaar 
439e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
440e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
441e4f72b32SMarcel Moolenaar 		powerpc_sync();
442e4f72b32SMarcel Moolenaar 	}
443e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
444e4f72b32SMarcel Moolenaar 	powerpc_sync();
445e4f72b32SMarcel Moolenaar }
4465244eac9SBenno Rice 
4475244eac9SBenno Rice static __inline int
4485244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4495244eac9SBenno Rice {
4505244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4515244eac9SBenno Rice }
4525244eac9SBenno Rice 
4535244eac9SBenno Rice static __inline u_int
4545244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4555244eac9SBenno Rice {
4565244eac9SBenno Rice 	u_int hash;
4575244eac9SBenno Rice 
4585244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4595244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
46059276937SPeter Grehan 	return (hash & moea_pteg_mask);
4615244eac9SBenno Rice }
4625244eac9SBenno Rice 
4635244eac9SBenno Rice static __inline struct pvo_head *
4645244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
465f9bac91bSBenno Rice {
466f9bac91bSBenno Rice 
4675244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
468f9bac91bSBenno Rice }
469f9bac91bSBenno Rice 
470f9bac91bSBenno Rice static __inline void
47159276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
472f9bac91bSBenno Rice {
473f9bac91bSBenno Rice 
474d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4755244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4765244eac9SBenno Rice }
4775244eac9SBenno Rice 
4785244eac9SBenno Rice static __inline int
47959276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4805244eac9SBenno Rice {
4815244eac9SBenno Rice 
4825244eac9SBenno Rice 	return (m->md.mdpg_attrs);
483f9bac91bSBenno Rice }
484f9bac91bSBenno Rice 
485f9bac91bSBenno Rice static __inline void
48659276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
487f9bac91bSBenno Rice {
488f9bac91bSBenno Rice 
489d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4905244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
491f9bac91bSBenno Rice }
492f9bac91bSBenno Rice 
493f9bac91bSBenno Rice static __inline int
49459276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
495f9bac91bSBenno Rice {
4965244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4975244eac9SBenno Rice 		return (1);
498f9bac91bSBenno Rice 
4995244eac9SBenno Rice 	return (0);
500f9bac91bSBenno Rice }
501f9bac91bSBenno Rice 
502f9bac91bSBenno Rice static __inline int
50359276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
504f9bac91bSBenno Rice {
5055244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
5065244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5075244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
508f9bac91bSBenno Rice }
509f9bac91bSBenno Rice 
5105244eac9SBenno Rice static __inline void
51159276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
512f9bac91bSBenno Rice {
513d644a0b7SAlan Cox 
514d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
515d644a0b7SAlan Cox 
516f9bac91bSBenno Rice 	/*
5175244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5185244eac9SBenno Rice 	 * set when the real pte is set in memory.
519f9bac91bSBenno Rice 	 *
520f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
521f9bac91bSBenno Rice 	 */
5225244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5235244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5245244eac9SBenno Rice 	pt->pte_lo = pte_lo;
525f9bac91bSBenno Rice }
526f9bac91bSBenno Rice 
5275244eac9SBenno Rice static __inline void
52859276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
529f9bac91bSBenno Rice {
530f9bac91bSBenno Rice 
531d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5325244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
533f9bac91bSBenno Rice }
534f9bac91bSBenno Rice 
5355244eac9SBenno Rice static __inline void
53659276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
537f9bac91bSBenno Rice {
5385244eac9SBenno Rice 
539d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
540d644a0b7SAlan Cox 
5415244eac9SBenno Rice 	/*
5425244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5435244eac9SBenno Rice 	 */
5445244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
545e4f72b32SMarcel Moolenaar 	tlbie(va);
5465244eac9SBenno Rice }
5475244eac9SBenno Rice 
5485244eac9SBenno Rice static __inline void
54959276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5505244eac9SBenno Rice {
5515244eac9SBenno Rice 
552d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5535244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5545244eac9SBenno Rice 
5555244eac9SBenno Rice 	/*
5565244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
5575244eac9SBenno Rice 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
5585244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5595244eac9SBenno Rice 	 */
5605244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
561e4f72b32SMarcel Moolenaar 	powerpc_sync();
5625244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
563e4f72b32SMarcel Moolenaar 	powerpc_sync();
56459276937SPeter Grehan 	moea_pte_valid++;
5655244eac9SBenno Rice }
5665244eac9SBenno Rice 
5675244eac9SBenno Rice static __inline void
56859276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5695244eac9SBenno Rice {
5705244eac9SBenno Rice 
571d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5725244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5735244eac9SBenno Rice 
5745244eac9SBenno Rice 	/*
5755244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5765244eac9SBenno Rice 	 */
577e4f72b32SMarcel Moolenaar 	powerpc_sync();
5785244eac9SBenno Rice 
5795244eac9SBenno Rice 	/*
5805244eac9SBenno Rice 	 * Invalidate the pte.
5815244eac9SBenno Rice 	 */
5825244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5835244eac9SBenno Rice 
584e4f72b32SMarcel Moolenaar 	tlbie(va);
5855244eac9SBenno Rice 
5865244eac9SBenno Rice 	/*
5875244eac9SBenno Rice 	 * Save the reg & chg bits.
5885244eac9SBenno Rice 	 */
58959276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
59059276937SPeter Grehan 	moea_pte_valid--;
5915244eac9SBenno Rice }
5925244eac9SBenno Rice 
5935244eac9SBenno Rice static __inline void
59459276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5955244eac9SBenno Rice {
5965244eac9SBenno Rice 
5975244eac9SBenno Rice 	/*
5985244eac9SBenno Rice 	 * Invalidate the PTE
5995244eac9SBenno Rice 	 */
60059276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
60159276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
602f9bac91bSBenno Rice }
603f9bac91bSBenno Rice 
604f9bac91bSBenno Rice /*
6055244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
606f9bac91bSBenno Rice  */
6075244eac9SBenno Rice static int	mr_cmp(const void *a, const void *b);
6085244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
6095244eac9SBenno Rice 
6105244eac9SBenno Rice static int
6115244eac9SBenno Rice mr_cmp(const void *a, const void *b)
612f9bac91bSBenno Rice {
6135244eac9SBenno Rice 	const struct	mem_region *regiona;
6145244eac9SBenno Rice 	const struct	mem_region *regionb;
615f9bac91bSBenno Rice 
6165244eac9SBenno Rice 	regiona = a;
6175244eac9SBenno Rice 	regionb = b;
6185244eac9SBenno Rice 	if (regiona->mr_start < regionb->mr_start)
6195244eac9SBenno Rice 		return (-1);
6205244eac9SBenno Rice 	else if (regiona->mr_start > regionb->mr_start)
6215244eac9SBenno Rice 		return (1);
6225244eac9SBenno Rice 	else
623f9bac91bSBenno Rice 		return (0);
624f9bac91bSBenno Rice }
6255244eac9SBenno Rice 
6265244eac9SBenno Rice static int
6275244eac9SBenno Rice om_cmp(const void *a, const void *b)
6285244eac9SBenno Rice {
6295244eac9SBenno Rice 	const struct	ofw_map *mapa;
6305244eac9SBenno Rice 	const struct	ofw_map *mapb;
6315244eac9SBenno Rice 
6325244eac9SBenno Rice 	mapa = a;
6335244eac9SBenno Rice 	mapb = b;
6345244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6355244eac9SBenno Rice 		return (-1);
6365244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6375244eac9SBenno Rice 		return (1);
6385244eac9SBenno Rice 	else
6395244eac9SBenno Rice 		return (0);
640f9bac91bSBenno Rice }
641f9bac91bSBenno Rice 
642f9bac91bSBenno Rice void
6431c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap)
64412640815SMarcel Moolenaar {
64512640815SMarcel Moolenaar 	u_int sdr;
64612640815SMarcel Moolenaar 	int i;
64712640815SMarcel Moolenaar 
64812640815SMarcel Moolenaar 	if (ap) {
649e4f72b32SMarcel Moolenaar 		powerpc_sync();
65012640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
65112640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
65212640815SMarcel Moolenaar 		isync();
65312640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
65412640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
65512640815SMarcel Moolenaar 		isync();
65612640815SMarcel Moolenaar 	}
65712640815SMarcel Moolenaar 
65801d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
65901d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
66012640815SMarcel Moolenaar 	isync();
66112640815SMarcel Moolenaar 
66201d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
66301d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
66401d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
66501d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
66601d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
66712640815SMarcel Moolenaar 	isync();
66812640815SMarcel Moolenaar 
66912640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
67012640815SMarcel Moolenaar 		mtsrin(i << ADDR_SR_SHFT, EMPTY_SEGMENT);
67112640815SMarcel Moolenaar 
67212640815SMarcel Moolenaar 	__asm __volatile("mtsr %0,%1" :: "n"(KERNEL_SR), "r"(KERNEL_SEGMENT));
67312640815SMarcel Moolenaar 	__asm __volatile("mtsr %0,%1" :: "n"(KERNEL2_SR), "r"(KERNEL2_SEGMENT));
674e4f72b32SMarcel Moolenaar 	powerpc_sync();
67512640815SMarcel Moolenaar 
67612640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
67712640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
67812640815SMarcel Moolenaar 	isync();
67912640815SMarcel Moolenaar 
68086c1fb4cSMarcel Moolenaar 	tlbia();
68112640815SMarcel Moolenaar }
68212640815SMarcel Moolenaar 
68312640815SMarcel Moolenaar void
68459276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
685f9bac91bSBenno Rice {
68631c82d03SBenno Rice 	ihandle_t	mmui;
6875244eac9SBenno Rice 	phandle_t	chosen, mmu;
6885244eac9SBenno Rice 	int		sz;
6895244eac9SBenno Rice 	int		i, j;
69032bc7846SPeter Grehan 	int		ofw_mappings;
691e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6925244eac9SBenno Rice 	vm_offset_t	pa, va, off;
69350c202c5SJeff Roberson 	void		*dpcpu;
694976cc697SNathan Whitehorn 	register_t	msr;
695f9bac91bSBenno Rice 
696f9bac91bSBenno Rice         /*
69732bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6980d290675SBenno Rice          */
6990d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
7000d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
7010d290675SBenno Rice 
7020d290675SBenno Rice         /*
7030d290675SBenno Rice          * Map PCI memory space.
7040d290675SBenno Rice          */
7050d290675SBenno Rice         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
7060d290675SBenno Rice         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
7070d290675SBenno Rice 
7080d290675SBenno Rice         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
7090d290675SBenno Rice         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
7100d290675SBenno Rice 
7110d290675SBenno Rice         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
7120d290675SBenno Rice         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
7130d290675SBenno Rice 
7140d290675SBenno Rice         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
7150d290675SBenno Rice         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
7160d290675SBenno Rice 
7170d290675SBenno Rice         /*
7180d290675SBenno Rice          * Map obio devices.
7190d290675SBenno Rice          */
7200d290675SBenno Rice         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
7210d290675SBenno Rice         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
7220d290675SBenno Rice 
7230d290675SBenno Rice 	/*
7245244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
725976cc697SNathan Whitehorn 	 * where we are. Turn off instruction relocation temporarily
726976cc697SNathan Whitehorn 	 * to prevent faults while reprogramming the IBAT.
727f9bac91bSBenno Rice 	 */
728976cc697SNathan Whitehorn 	msr = mfmsr();
729976cc697SNathan Whitehorn 	mtmsr(msr & ~PSL_IR);
73059276937SPeter Grehan 	__asm (".balign 32; \n"
73172ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
7325d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
73312640815SMarcel Moolenaar 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
734976cc697SNathan Whitehorn 	mtmsr(msr);
7350d290675SBenno Rice 
7360d290675SBenno Rice 	/* map pci space */
73712640815SMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
73812640815SMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
73912640815SMarcel Moolenaar 	isync();
740f9bac91bSBenno Rice 
7411c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7421c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7431c96bdd1SNathan Whitehorn 
74431c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
74559276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
74631c82d03SBenno Rice 
74731c82d03SBenno Rice 	qsort(pregions, pregions_sz, sizeof(*pregions), mr_cmp);
74831c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
74932bc7846SPeter Grehan 		vm_offset_t pa;
75032bc7846SPeter Grehan 		vm_offset_t end;
75132bc7846SPeter Grehan 
75231c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
75331c82d03SBenno Rice 			pregions[i].mr_start,
75431c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
75531c82d03SBenno Rice 			pregions[i].mr_size);
75632bc7846SPeter Grehan 		/*
75732bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
75832bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
75932bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
76032bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
76132bc7846SPeter Grehan 		 * a while yet.
76232bc7846SPeter Grehan 		 */
76332bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
76432bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
76532bc7846SPeter Grehan 		do {
76632bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
76732bc7846SPeter Grehan 
76832bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
76932bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
77032bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
77132bc7846SPeter Grehan 		} while (pa < end);
77231c82d03SBenno Rice 	}
77331c82d03SBenno Rice 
77431c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
77559276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
77631c82d03SBenno Rice 	qsort(regions, regions_sz, sizeof(*regions), mr_cmp);
7775244eac9SBenno Rice 	phys_avail_count = 0;
778d2c1f576SBenno Rice 	physsz = 0;
779b0c21309SPeter Grehan 	hwphyssz = 0;
780b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
78131c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7825244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7835244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7845244eac9SBenno Rice 		    regions[i].mr_size);
785e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
786e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
787e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
788e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
789e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
790e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
791e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
792e2f6d6e2SPeter Grehan 				phys_avail_count++;
793e2f6d6e2SPeter Grehan 			}
794e2f6d6e2SPeter Grehan 			break;
795e2f6d6e2SPeter Grehan 		}
7965244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7975244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7985244eac9SBenno Rice 		phys_avail_count++;
799d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
800f9bac91bSBenno Rice 	}
801d2c1f576SBenno Rice 	physmem = btoc(physsz);
802f9bac91bSBenno Rice 
803f9bac91bSBenno Rice 	/*
8045244eac9SBenno Rice 	 * Allocate PTEG table.
805f9bac91bSBenno Rice 	 */
8065244eac9SBenno Rice #ifdef PTEGCOUNT
80759276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
8085244eac9SBenno Rice #else
80959276937SPeter Grehan 	moea_pteg_count = 0x1000;
810f9bac91bSBenno Rice 
81159276937SPeter Grehan 	while (moea_pteg_count < physmem)
81259276937SPeter Grehan 		moea_pteg_count <<= 1;
813f9bac91bSBenno Rice 
81459276937SPeter Grehan 	moea_pteg_count >>= 1;
8155244eac9SBenno Rice #endif /* PTEGCOUNT */
816f9bac91bSBenno Rice 
81759276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
81859276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
8195244eac9SBenno Rice 	    size);
82059276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
82159276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
82259276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
82359276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
824f9bac91bSBenno Rice 
8255244eac9SBenno Rice 	/*
826864bc520SBenno Rice 	 * Allocate pv/overflow lists.
8275244eac9SBenno Rice 	 */
82859276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
82959276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
8305244eac9SBenno Rice 	    PAGE_SIZE);
83159276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
83259276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
83359276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
8345244eac9SBenno Rice 
8355244eac9SBenno Rice 	/*
836f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
837f489bf21SAlan Cox 	 * tables.
838f489bf21SAlan Cox 	 */
839d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
840d644a0b7SAlan Cox 	    MTX_RECURSE);
841e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
842f489bf21SAlan Cox 
843e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
844e4f72b32SMarcel Moolenaar 
845f489bf21SAlan Cox 	/*
8465244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8475244eac9SBenno Rice 	 */
84859276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8490d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
85059276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8515244eac9SBenno Rice 
8525244eac9SBenno Rice 	/*
8535244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8545244eac9SBenno Rice 	 */
85559276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8565244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
85759276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8585244eac9SBenno Rice 
8595244eac9SBenno Rice 	/*
8605244eac9SBenno Rice 	 * Set up the Open Firmware pmap and add it's mappings.
8615244eac9SBenno Rice 	 */
86259276937SPeter Grehan 	moea_pinit(mmup, &ofw_pmap);
8635244eac9SBenno Rice 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
8644daf20b2SPeter Grehan 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
8655244eac9SBenno Rice 	if ((chosen = OF_finddevice("/chosen")) == -1)
86659276937SPeter Grehan 		panic("moea_bootstrap: can't find /chosen");
8675244eac9SBenno Rice 	OF_getprop(chosen, "mmu", &mmui, 4);
8685244eac9SBenno Rice 	if ((mmu = OF_instance_to_package(mmui)) == -1)
86959276937SPeter Grehan 		panic("moea_bootstrap: can't get mmu package");
8705244eac9SBenno Rice 	if ((sz = OF_getproplen(mmu, "translations")) == -1)
87159276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translation count");
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);
88732bc7846SPeter Grehan 	for (i = 0, ofw_mappings = 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 		/*
89332bc7846SPeter Grehan 		 * If the mapping is 1:1, let the RAM and device on-demand
89432bc7846SPeter Grehan 		 * 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 */
9005244eac9SBenno Rice 		for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) {
9015244eac9SBenno Rice 			struct	vm_page m;
9025244eac9SBenno Rice 
9035244eac9SBenno Rice 			m.phys_addr = translations[i].om_pa + off;
904cd6a97f0SNathan Whitehorn 			m.md.mdpg_cache_attrs = VM_MEMATTR_DEFAULT;
9059ab6032fSAlan Cox 			m.oflags = VPO_BUSY;
9065ce609a3SRink Springer 			PMAP_LOCK(&ofw_pmap);
907ce142d9eSAlan Cox 			moea_enter_locked(&ofw_pmap,
90859276937SPeter Grehan 				   translations[i].om_va + off, &m,
9095244eac9SBenno Rice 				   VM_PROT_ALL, 1);
9105ce609a3SRink Springer 			PMAP_UNLOCK(&ofw_pmap);
91132bc7846SPeter Grehan 			ofw_mappings++;
912f9bac91bSBenno Rice 		}
913f9bac91bSBenno Rice 	}
914014ffa99SMarcel Moolenaar 
915014ffa99SMarcel Moolenaar 	/*
916014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
917014ffa99SMarcel Moolenaar 	 */
918014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
919014ffa99SMarcel Moolenaar 		;
920014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
9215244eac9SBenno Rice 
9225244eac9SBenno Rice 	/*
9235244eac9SBenno Rice 	 * Initialize the kernel pmap (which is statically allocated).
9245244eac9SBenno Rice 	 */
92548d0b1a0SAlan Cox 	PMAP_LOCK_INIT(kernel_pmap);
9265244eac9SBenno Rice 	for (i = 0; i < 16; i++) {
9275244eac9SBenno Rice 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT;
928f9bac91bSBenno Rice 	}
9295244eac9SBenno Rice 	kernel_pmap->pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
93022f2fe59SPeter Grehan 	kernel_pmap->pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
9315244eac9SBenno Rice 	kernel_pmap->pm_active = ~0;
9325244eac9SBenno Rice 
9331c96bdd1SNathan Whitehorn 	moea_cpu_bootstrap(mmup,0);
9345244eac9SBenno Rice 
9355244eac9SBenno Rice 	pmap_bootstrapped++;
936014ffa99SMarcel Moolenaar 
937014ffa99SMarcel Moolenaar 	/*
938014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
939014ffa99SMarcel Moolenaar 	 */
940014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
941ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
942014ffa99SMarcel Moolenaar 
943014ffa99SMarcel Moolenaar 	/*
944014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
945014ffa99SMarcel Moolenaar 	 * into the kernel page map.
946014ffa99SMarcel Moolenaar 	 */
947014ffa99SMarcel Moolenaar 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
948014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
949014ffa99SMarcel Moolenaar 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
950014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
951014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
952014ffa99SMarcel Moolenaar 	thread0.td_kstack_pages = KSTACK_PAGES;
953014ffa99SMarcel Moolenaar 	for (i = 0; i < KSTACK_PAGES; i++) {
954c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
955014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
956014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
957014ffa99SMarcel Moolenaar 	}
958014ffa99SMarcel Moolenaar 
959014ffa99SMarcel Moolenaar 	/*
960014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
961014ffa99SMarcel Moolenaar 	 */
962014ffa99SMarcel Moolenaar 	pa = msgbuf_phys = moea_bootstrap_alloc(MSGBUF_SIZE, PAGE_SIZE);
963014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
964014ffa99SMarcel Moolenaar 	va = virtual_avail;
965014ffa99SMarcel Moolenaar 	virtual_avail += round_page(MSGBUF_SIZE);
966014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
967c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
968014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
969014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
970014ffa99SMarcel Moolenaar 	}
97150c202c5SJeff Roberson 
97250c202c5SJeff Roberson 	/*
97350c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
97450c202c5SJeff Roberson 	 */
97550c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
97650c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
97750c202c5SJeff Roberson 	va = virtual_avail;
97850c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
97950c202c5SJeff Roberson 	while (va < virtual_avail) {
980c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
98150c202c5SJeff Roberson 		pa += PAGE_SIZE;
98250c202c5SJeff Roberson 		va += PAGE_SIZE;
98350c202c5SJeff Roberson 	}
98450c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
9855244eac9SBenno Rice }
9865244eac9SBenno Rice 
9875244eac9SBenno Rice /*
9885244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9895244eac9SBenno Rice  * space can be accessed in any way.
990f9bac91bSBenno Rice  */
991f9bac91bSBenno Rice void
99259276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
993f9bac91bSBenno Rice {
9948207b362SBenno Rice 	pmap_t	pm, pmr;
995f9bac91bSBenno Rice 
996f9bac91bSBenno Rice 	/*
99732bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9985244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
999f9bac91bSBenno Rice 	 */
10005244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
100152a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
10028207b362SBenno Rice 
10035244eac9SBenno Rice 	pm->pm_active |= PCPU_GET(cpumask);
10048207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
1005ac6ba8bdSBenno Rice }
1006ac6ba8bdSBenno Rice 
1007ac6ba8bdSBenno Rice void
100859276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
1009ac6ba8bdSBenno Rice {
1010ac6ba8bdSBenno Rice 	pmap_t	pm;
1011ac6ba8bdSBenno Rice 
1012ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
1013e4f72b32SMarcel Moolenaar 	pm->pm_active &= ~PCPU_GET(cpumask);
10148207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
1015f9bac91bSBenno Rice }
1016f9bac91bSBenno Rice 
1017f9bac91bSBenno Rice void
101859276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
1019f9bac91bSBenno Rice {
10200f92104cSBenno Rice 	struct	pvo_entry *pvo;
10210f92104cSBenno Rice 
102248d0b1a0SAlan Cox 	PMAP_LOCK(pm);
102359276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
10240f92104cSBenno Rice 
10250f92104cSBenno Rice 	if (pvo != NULL) {
10260f92104cSBenno Rice 		if (wired) {
10270f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
10280f92104cSBenno Rice 				pm->pm_stats.wired_count++;
10290f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
10300f92104cSBenno Rice 		} else {
10310f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
10320f92104cSBenno Rice 				pm->pm_stats.wired_count--;
10330f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
10340f92104cSBenno Rice 		}
10350f92104cSBenno Rice 	}
103648d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
1037f9bac91bSBenno Rice }
1038f9bac91bSBenno Rice 
1039f9bac91bSBenno Rice void
104059276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
1041f9bac91bSBenno Rice {
104225e2288dSBenno Rice 	vm_offset_t	dst;
104325e2288dSBenno Rice 	vm_offset_t	src;
104425e2288dSBenno Rice 
104525e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
104625e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
104725e2288dSBenno Rice 
104825e2288dSBenno Rice 	kcopy((void *)src, (void *)dst, PAGE_SIZE);
1049f9bac91bSBenno Rice }
1050111c77dcSBenno Rice 
1051111c77dcSBenno Rice /*
10525244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
10535244eac9SBenno Rice  */
10545244eac9SBenno Rice void
105559276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
10565244eac9SBenno Rice {
10571a87a0daSPeter Wemm 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10585b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
10595244eac9SBenno Rice 
10605244eac9SBenno Rice 	bzero(va, PAGE_SIZE);
10615244eac9SBenno Rice }
10625244eac9SBenno Rice 
10635244eac9SBenno Rice void
106459276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
10655244eac9SBenno Rice {
10663495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10675b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
10683495845eSBenno Rice 
10695b43c63dSMarcel Moolenaar 	bzero(va, size);
10705244eac9SBenno Rice }
10715244eac9SBenno Rice 
1072a58b3a68SPeter Wemm void
107359276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1074a58b3a68SPeter Wemm {
10755b43c63dSMarcel Moolenaar 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10765b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
1077a58b3a68SPeter Wemm 
10785b43c63dSMarcel Moolenaar 	bzero(va, PAGE_SIZE);
1079a58b3a68SPeter Wemm }
1080a58b3a68SPeter Wemm 
10815244eac9SBenno Rice /*
10825244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
10835244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
10845244eac9SBenno Rice  * will be wired down.
10855244eac9SBenno Rice  */
10865244eac9SBenno Rice void
108759276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
10885244eac9SBenno Rice 	   boolean_t wired)
10895244eac9SBenno Rice {
1090ce142d9eSAlan Cox 
1091ce142d9eSAlan Cox 	vm_page_lock_queues();
1092ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
109367c867eeSAlan Cox 	moea_enter_locked(pmap, va, m, prot, wired);
1094ce142d9eSAlan Cox 	vm_page_unlock_queues();
1095ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1096ce142d9eSAlan Cox }
1097ce142d9eSAlan Cox 
1098ce142d9eSAlan Cox /*
1099ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1100ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1101ce142d9eSAlan Cox  * will be wired down.
1102ce142d9eSAlan Cox  *
1103ce142d9eSAlan Cox  * The page queues and pmap must be locked.
1104ce142d9eSAlan Cox  */
1105ce142d9eSAlan Cox static void
1106ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1107ce142d9eSAlan Cox     boolean_t wired)
1108ce142d9eSAlan Cox {
11095244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1110378862a7SJeff Roberson 	uma_zone_t	zone;
11118207b362SBenno Rice 	vm_page_t	pg;
1112c1f4123bSNathan Whitehorn 	u_int		pte_lo, pvo_flags, was_exec;
11135244eac9SBenno Rice 	int		error;
11145244eac9SBenno Rice 
111559276937SPeter Grehan 	if (!moea_initialized) {
111659276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
111759276937SPeter Grehan 		zone = moea_upvo_zone;
11185244eac9SBenno Rice 		pvo_flags = 0;
11198207b362SBenno Rice 		pg = NULL;
11208207b362SBenno Rice 		was_exec = PTE_EXEC;
11215244eac9SBenno Rice 	} else {
112203b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
112303b6e025SPeter Grehan 		pg = m;
112459276937SPeter Grehan 		zone = moea_mpvo_zone;
11255244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
11268207b362SBenno Rice 		was_exec = 0;
11275244eac9SBenno Rice 	}
1128f489bf21SAlan Cox 	if (pmap_bootstrapped)
1129ce142d9eSAlan Cox 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1130ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
11319124d0d6SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
11329124d0d6SAlan Cox 	    (m->oflags & VPO_BUSY) != 0 || VM_OBJECT_LOCKED(m->object),
11339ab6032fSAlan Cox 	    ("moea_enter_locked: page %p is not busy", m));
11345244eac9SBenno Rice 
11354dba5df1SPeter Grehan 	/* XXX change the pvo head for fake pages */
1136a130b35fSNathan Whitehorn 	if ((m->flags & PG_FICTITIOUS) == PG_FICTITIOUS) {
1137a130b35fSNathan Whitehorn 		pvo_flags &= ~PVO_MANAGED;
113859276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
1139a130b35fSNathan Whitehorn 		zone = moea_upvo_zone;
1140a130b35fSNathan Whitehorn 	}
11414dba5df1SPeter Grehan 
11428207b362SBenno Rice 	/*
11438207b362SBenno Rice 	 * If this is a managed page, and it's the first reference to the page,
11448207b362SBenno Rice 	 * clear the execness of the page.  Otherwise fetch the execness.
11458207b362SBenno Rice 	 */
11464dba5df1SPeter Grehan 	if ((pg != NULL) && ((m->flags & PG_FICTITIOUS) == 0)) {
11478207b362SBenno Rice 		if (LIST_EMPTY(pvo_head)) {
114859276937SPeter Grehan 			moea_attr_clear(pg, PTE_EXEC);
11498207b362SBenno Rice 		} else {
115059276937SPeter Grehan 			was_exec = moea_attr_fetch(pg) & PTE_EXEC;
11518207b362SBenno Rice 		}
11528207b362SBenno Rice 	}
11538207b362SBenno Rice 
1154cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11555244eac9SBenno Rice 
115644b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11575244eac9SBenno Rice 		pte_lo |= PTE_BW;
11582368a371SAlan Cox 		if (pmap_bootstrapped &&
11592368a371SAlan Cox 		    (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0)
116044b8bd66SAlan Cox 			vm_page_flag_set(m, PG_WRITEABLE);
116144b8bd66SAlan Cox 	} else
11625244eac9SBenno Rice 		pte_lo |= PTE_BR;
11635244eac9SBenno Rice 
11644dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
11654dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
11665244eac9SBenno Rice 
11675244eac9SBenno Rice 	if (wired)
11685244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11695244eac9SBenno Rice 
11704dba5df1SPeter Grehan 	if ((m->flags & PG_FICTITIOUS) != 0)
11714dba5df1SPeter Grehan 		pvo_flags |= PVO_FAKE;
11724dba5df1SPeter Grehan 
117359276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11748207b362SBenno Rice 	    pte_lo, pvo_flags);
11755244eac9SBenno Rice 
11768207b362SBenno Rice 	/*
11778207b362SBenno Rice 	 * Flush the real page from the instruction cache if this page is
11788207b362SBenno Rice 	 * mapped executable and cacheable and was not previously mapped (or
11798207b362SBenno Rice 	 * was not mapped executable).
11808207b362SBenno Rice 	 */
11818207b362SBenno Rice 	if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
11828207b362SBenno Rice 	    (pte_lo & PTE_I) == 0 && was_exec == 0) {
11835244eac9SBenno Rice 		/*
11845244eac9SBenno Rice 		 * Flush the real memory from the cache.
11855244eac9SBenno Rice 		 */
118659276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
11878207b362SBenno Rice 		if (pg != NULL)
118859276937SPeter Grehan 			moea_attr_save(pg, PTE_EXEC);
11895244eac9SBenno Rice 	}
119032bc7846SPeter Grehan 
119132bc7846SPeter Grehan 	/* XXX syncicache always until problems are sorted */
119259276937SPeter Grehan 	moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1193ce142d9eSAlan Cox }
1194ce142d9eSAlan Cox 
1195ce142d9eSAlan Cox /*
1196ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1197ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1198ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1199ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1200ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1201ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1202ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1203ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1204ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1205ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1206ce142d9eSAlan Cox  */
1207ce142d9eSAlan Cox void
1208ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1209ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1210ce142d9eSAlan Cox {
1211ce142d9eSAlan Cox 	vm_page_t m;
1212ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1213ce142d9eSAlan Cox 
1214ce142d9eSAlan Cox 	psize = atop(end - start);
1215ce142d9eSAlan Cox 	m = m_start;
1216c46b90e9SAlan Cox 	vm_page_lock_queues();
1217ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1218ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1219ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1220ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1221ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1222ce142d9eSAlan Cox 	}
1223c46b90e9SAlan Cox 	vm_page_unlock_queues();
1224ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
12255244eac9SBenno Rice }
12265244eac9SBenno Rice 
12272053c127SStephan Uphoff void
122859276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
12292053c127SStephan Uphoff     vm_prot_t prot)
1230dca96f1aSAlan Cox {
1231dca96f1aSAlan Cox 
12323c4a2440SAlan Cox 	vm_page_lock_queues();
1233ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1234ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
123559276937SPeter Grehan 	    FALSE);
12363c4a2440SAlan Cox 	vm_page_unlock_queues();
1237ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1238dca96f1aSAlan Cox }
1239dca96f1aSAlan Cox 
124056b09388SAlan Cox vm_paddr_t
124159276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
12425244eac9SBenno Rice {
12430f92104cSBenno Rice 	struct	pvo_entry *pvo;
124448d0b1a0SAlan Cox 	vm_paddr_t pa;
12450f92104cSBenno Rice 
124648d0b1a0SAlan Cox 	PMAP_LOCK(pm);
124759276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
124848d0b1a0SAlan Cox 	if (pvo == NULL)
124948d0b1a0SAlan Cox 		pa = 0;
125048d0b1a0SAlan Cox 	else
125152a7870dSNathan Whitehorn 		pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
125248d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
125348d0b1a0SAlan Cox 	return (pa);
12545244eac9SBenno Rice }
12555244eac9SBenno Rice 
12565244eac9SBenno Rice /*
125784792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
125884792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
125984792e72SPeter Grehan  * protection.
126084792e72SPeter Grehan  */
126184792e72SPeter Grehan vm_page_t
126259276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
126384792e72SPeter Grehan {
1264ab50a262SAlan Cox 	struct	pvo_entry *pvo;
126584792e72SPeter Grehan 	vm_page_t m;
12662965a453SKip Macy         vm_paddr_t pa;
126784792e72SPeter Grehan 
126884792e72SPeter Grehan 	m = NULL;
12692965a453SKip Macy 	pa = 0;
127048d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
12712965a453SKip Macy retry:
127259276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
127352a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
127452a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1275ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
12762965a453SKip Macy 		if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa))
12772965a453SKip Macy 			goto retry;
127852a7870dSNathan Whitehorn 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
127984792e72SPeter Grehan 		vm_page_hold(m);
128084792e72SPeter Grehan 	}
12812965a453SKip Macy 	PA_UNLOCK_COND(pa);
128248d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
128384792e72SPeter Grehan 	return (m);
128484792e72SPeter Grehan }
128584792e72SPeter Grehan 
12865244eac9SBenno Rice void
128759276937SPeter Grehan moea_init(mmu_t mmu)
12885244eac9SBenno Rice {
12895244eac9SBenno Rice 
129059276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12910ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12920ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
129359276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12940ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12950ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
129659276937SPeter Grehan 	moea_initialized = TRUE;
12975244eac9SBenno Rice }
12985244eac9SBenno Rice 
12995244eac9SBenno Rice boolean_t
13007b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m)
13017b85f591SAlan Cox {
13027b85f591SAlan Cox 
1303c46b90e9SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1304c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
13057b85f591SAlan Cox 	return (moea_query_bit(m, PTE_REF));
13067b85f591SAlan Cox }
13077b85f591SAlan Cox 
13087b85f591SAlan Cox boolean_t
130959276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
13105244eac9SBenno Rice {
13110f92104cSBenno Rice 
1312567e51e1SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1313567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1314567e51e1SAlan Cox 
1315567e51e1SAlan Cox 	/*
1316567e51e1SAlan Cox 	 * If the page is not VPO_BUSY, then PG_WRITEABLE cannot be
1317567e51e1SAlan Cox 	 * concurrently set while the object is locked.  Thus, if PG_WRITEABLE
1318567e51e1SAlan Cox 	 * is clear, no PTEs can have PTE_CHG set.
1319567e51e1SAlan Cox 	 */
1320567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1321567e51e1SAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
1322567e51e1SAlan Cox 	    (m->flags & PG_WRITEABLE) == 0)
13230f92104cSBenno Rice 		return (FALSE);
1324c46b90e9SAlan Cox 	return (moea_query_bit(m, PTE_CHG));
1325566526a9SAlan Cox }
1326566526a9SAlan Cox 
13275244eac9SBenno Rice void
132859276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m)
13295244eac9SBenno Rice {
133003b6e025SPeter Grehan 
1331567e51e1SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1332567e51e1SAlan Cox 	    ("moea_clear_reference: page %p is not managed", m));
1333ce186587SAlan Cox 	moea_clear_bit(m, PTE_REF);
133403b6e025SPeter Grehan }
133503b6e025SPeter Grehan 
133603b6e025SPeter Grehan void
133759276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
133803b6e025SPeter Grehan {
133903b6e025SPeter Grehan 
1340567e51e1SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1341567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
1342567e51e1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1343567e51e1SAlan Cox 	KASSERT((m->oflags & VPO_BUSY) == 0,
1344567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is busy", m));
1345567e51e1SAlan Cox 
1346567e51e1SAlan Cox 	/*
1347567e51e1SAlan Cox 	 * If the page is not PG_WRITEABLE, then no PTEs can have PTE_CHG
1348567e51e1SAlan Cox 	 * set.  If the object containing the page is locked and the page is
1349567e51e1SAlan Cox 	 * not VPO_BUSY, then PG_WRITEABLE cannot be concurrently set.
1350567e51e1SAlan Cox 	 */
1351567e51e1SAlan Cox 	if ((m->flags & PG_WRITEABLE) == 0)
135203b6e025SPeter Grehan 		return;
1353ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
13545244eac9SBenno Rice }
13555244eac9SBenno Rice 
13567f3a4093SMike Silbersack /*
135778985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
135878985e42SAlan Cox  */
135978985e42SAlan Cox void
136078985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
136178985e42SAlan Cox {
136278985e42SAlan Cox 	struct	pvo_entry *pvo;
136378985e42SAlan Cox 	struct	pte *pt;
136478985e42SAlan Cox 	pmap_t	pmap;
136578985e42SAlan Cox 	u_int	lo;
136678985e42SAlan Cox 
13679ab6032fSAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
13689ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
13699ab6032fSAlan Cox 
13709ab6032fSAlan Cox 	/*
13719ab6032fSAlan Cox 	 * If the page is not VPO_BUSY, then PG_WRITEABLE cannot be set by
13729ab6032fSAlan Cox 	 * another thread while the object is locked.  Thus, if PG_WRITEABLE
13739ab6032fSAlan Cox 	 * is clear, no page table entries need updating.
13749ab6032fSAlan Cox 	 */
13759ab6032fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
13769ab6032fSAlan Cox 	if ((m->oflags & VPO_BUSY) == 0 &&
137778985e42SAlan Cox 	    (m->flags & PG_WRITEABLE) == 0)
137878985e42SAlan Cox 		return;
13793c4a2440SAlan Cox 	vm_page_lock_queues();
138078985e42SAlan Cox 	lo = moea_attr_fetch(m);
1381e4f72b32SMarcel Moolenaar 	powerpc_sync();
138278985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
138378985e42SAlan Cox 		pmap = pvo->pvo_pmap;
138478985e42SAlan Cox 		PMAP_LOCK(pmap);
138552a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
138678985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
138752a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
138852a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
138978985e42SAlan Cox 			if (pt != NULL) {
139052a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
139152a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
139252a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
139352a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
139478985e42SAlan Cox 				    pvo->pvo_vaddr);
139578985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
139678985e42SAlan Cox 			}
139778985e42SAlan Cox 		}
139878985e42SAlan Cox 		PMAP_UNLOCK(pmap);
139978985e42SAlan Cox 	}
140078985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
140178985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
140278985e42SAlan Cox 		vm_page_dirty(m);
140378985e42SAlan Cox 	}
140478985e42SAlan Cox 	vm_page_flag_clear(m, PG_WRITEABLE);
14053c4a2440SAlan Cox 	vm_page_unlock_queues();
140678985e42SAlan Cox }
140778985e42SAlan Cox 
140878985e42SAlan Cox /*
140959276937SPeter Grehan  *	moea_ts_referenced:
14107f3a4093SMike Silbersack  *
14117f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
14127f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
14137f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
14147f3a4093SMike Silbersack  *	reference bits set.
14157f3a4093SMike Silbersack  *
14167f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
14177f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
14187f3a4093SMike Silbersack  *	optimal aging of shared pages.
14197f3a4093SMike Silbersack  */
142059276937SPeter Grehan boolean_t
142159276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
14225244eac9SBenno Rice {
142303b6e025SPeter Grehan 
1424ce186587SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1425ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
1426ce186587SAlan Cox 	return (moea_clear_bit(m, PTE_REF));
14275244eac9SBenno Rice }
14285244eac9SBenno Rice 
14295244eac9SBenno Rice /*
1430c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1431c1f4123bSNathan Whitehorn  */
1432c1f4123bSNathan Whitehorn void
1433c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma)
1434c1f4123bSNathan Whitehorn {
1435c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1436cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1437c1f4123bSNathan Whitehorn 	struct	pte *pt;
1438c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1439c1f4123bSNathan Whitehorn 	u_int	lo;
1440c1f4123bSNathan Whitehorn 
1441cd6a97f0SNathan Whitehorn 	if (m->flags & PG_FICTITIOUS) {
1442cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1443cd6a97f0SNathan Whitehorn 		return;
1444cd6a97f0SNathan Whitehorn 	}
1445cd6a97f0SNathan Whitehorn 
1446c1f4123bSNathan Whitehorn 	vm_page_lock_queues();
1447cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1448c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1449cd6a97f0SNathan Whitehorn 
1450cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1451c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1452c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1453c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1454c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1455c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1456c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1457c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1458c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1459c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1460c1f4123bSNathan Whitehorn 				isync();
1461c1f4123bSNathan Whitehorn 		}
1462c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1463c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1464c1f4123bSNathan Whitehorn 	}
1465c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
1466c1f4123bSNathan Whitehorn 	vm_page_unlock_queues();
1467c1f4123bSNathan Whitehorn }
1468c1f4123bSNathan Whitehorn 
1469c1f4123bSNathan Whitehorn /*
14705244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
14715244eac9SBenno Rice  */
14725244eac9SBenno Rice void
147359276937SPeter Grehan moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
14745244eac9SBenno Rice {
1475c1f4123bSNathan Whitehorn 
1476c1f4123bSNathan Whitehorn 	moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1477c1f4123bSNathan Whitehorn }
1478c1f4123bSNathan Whitehorn 
1479c1f4123bSNathan Whitehorn void
1480c1f4123bSNathan Whitehorn moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma)
1481c1f4123bSNathan Whitehorn {
14825244eac9SBenno Rice 	u_int		pte_lo;
14835244eac9SBenno Rice 	int		error;
14845244eac9SBenno Rice 
14855244eac9SBenno Rice #if 0
14865244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
148759276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
14885244eac9SBenno Rice 		    va);
14895244eac9SBenno Rice #endif
14905244eac9SBenno Rice 
1491c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
14925244eac9SBenno Rice 
14934711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
149459276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
149559276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
14965244eac9SBenno Rice 
14975244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
149859276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
14995244eac9SBenno Rice 		    pa, error);
15005244eac9SBenno Rice 
15015244eac9SBenno Rice 	/*
15025244eac9SBenno Rice 	 * Flush the real memory from the instruction cache.
15035244eac9SBenno Rice 	 */
15045244eac9SBenno Rice 	if ((pte_lo & (PTE_I | PTE_G)) == 0) {
150559276937SPeter Grehan 		moea_syncicache(pa, PAGE_SIZE);
15065244eac9SBenno Rice 	}
15074711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
15085244eac9SBenno Rice }
15095244eac9SBenno Rice 
1510e79f59e8SBenno Rice /*
1511e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1512e79f59e8SBenno Rice  * address.
1513e79f59e8SBenno Rice  */
15145244eac9SBenno Rice vm_offset_t
151559276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
15165244eac9SBenno Rice {
1517e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
151848d0b1a0SAlan Cox 	vm_paddr_t pa;
1519e79f59e8SBenno Rice 
15200efd0097SPeter Grehan 	/*
152152a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
15220efd0097SPeter Grehan 	 */
15230efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
15240efd0097SPeter Grehan 		return (va);
15250efd0097SPeter Grehan 	}
15260efd0097SPeter Grehan 
152748d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
152859276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
152959276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
153052a7870dSNathan Whitehorn 	pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
153148d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
153248d0b1a0SAlan Cox 	return (pa);
1533e79f59e8SBenno Rice }
1534e79f59e8SBenno Rice 
153588afb2a3SBenno Rice /*
153688afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
153788afb2a3SBenno Rice  */
15385244eac9SBenno Rice void
153959276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
15405244eac9SBenno Rice {
154188afb2a3SBenno Rice 
154259276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
15435244eac9SBenno Rice }
15445244eac9SBenno Rice 
15455244eac9SBenno Rice /*
15465244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
15475244eac9SBenno Rice  *
15485244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
15495244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
15505244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
15515244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
15525244eac9SBenno Rice  * first usable address after the mapped region.
15535244eac9SBenno Rice  */
15545244eac9SBenno Rice vm_offset_t
155559276937SPeter Grehan moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
155659276937SPeter Grehan     vm_offset_t pa_end, int prot)
15575244eac9SBenno Rice {
15585244eac9SBenno Rice 	vm_offset_t	sva, va;
15595244eac9SBenno Rice 
15605244eac9SBenno Rice 	sva = *virt;
15615244eac9SBenno Rice 	va = sva;
15625244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
156359276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
15645244eac9SBenno Rice 	*virt = va;
15655244eac9SBenno Rice 	return (sva);
15665244eac9SBenno Rice }
15675244eac9SBenno Rice 
15685244eac9SBenno Rice /*
15697f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
15707f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
15717f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
15727f3a4093SMike Silbersack  * is only necessary that true be returned for a small
15737f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
15747f3a4093SMike Silbersack  */
15755244eac9SBenno Rice boolean_t
157659276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
15775244eac9SBenno Rice {
157803b6e025SPeter Grehan         int loops;
157903b6e025SPeter Grehan 	struct pvo_entry *pvo;
1580ce186587SAlan Cox 	boolean_t rv;
158103b6e025SPeter Grehan 
1582ce186587SAlan Cox 	KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1583ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
158403b6e025SPeter Grehan 	loops = 0;
1585ce186587SAlan Cox 	rv = FALSE;
1586ce186587SAlan Cox 	vm_page_lock_queues();
158703b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1588ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1589ce186587SAlan Cox 			rv = TRUE;
1590ce186587SAlan Cox 			break;
1591ce186587SAlan Cox 		}
159203b6e025SPeter Grehan 		if (++loops >= 16)
159303b6e025SPeter Grehan 			break;
159403b6e025SPeter Grehan 	}
1595ce186587SAlan Cox 	vm_page_unlock_queues();
1596ce186587SAlan Cox 	return (rv);
15975244eac9SBenno Rice }
15985244eac9SBenno Rice 
159959677d3cSAlan Cox /*
160059677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
160159677d3cSAlan Cox  * that are wired.
160259677d3cSAlan Cox  */
160359677d3cSAlan Cox int
160459677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
160559677d3cSAlan Cox {
160659677d3cSAlan Cox 	struct pvo_entry *pvo;
160759677d3cSAlan Cox 	int count;
160859677d3cSAlan Cox 
160959677d3cSAlan Cox 	count = 0;
1610ce186587SAlan Cox 	if ((m->flags & PG_FICTITIOUS) != 0)
161159677d3cSAlan Cox 		return (count);
16123c4a2440SAlan Cox 	vm_page_lock_queues();
161359677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
161459677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
161559677d3cSAlan Cox 			count++;
16163c4a2440SAlan Cox 	vm_page_unlock_queues();
161759677d3cSAlan Cox 	return (count);
161859677d3cSAlan Cox }
161959677d3cSAlan Cox 
162059276937SPeter Grehan static u_int	moea_vsidcontext;
16215244eac9SBenno Rice 
16225244eac9SBenno Rice void
162359276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
16245244eac9SBenno Rice {
16255244eac9SBenno Rice 	int	i, mask;
16265244eac9SBenno Rice 	u_int	entropy;
16275244eac9SBenno Rice 
162859276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
162948d0b1a0SAlan Cox 	PMAP_LOCK_INIT(pmap);
16304daf20b2SPeter Grehan 
16315244eac9SBenno Rice 	entropy = 0;
16325244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
16335244eac9SBenno Rice 
163452a7870dSNathan Whitehorn 	if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
163552a7870dSNathan Whitehorn 	    == NULL) {
163652a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
163752a7870dSNathan Whitehorn 	}
163852a7870dSNathan Whitehorn 
163952a7870dSNathan Whitehorn 
1640e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
16415244eac9SBenno Rice 	/*
16425244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
16435244eac9SBenno Rice 	 */
16445244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
16455244eac9SBenno Rice 		u_int	hash, n;
16465244eac9SBenno Rice 
16475244eac9SBenno Rice 		/*
16485244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
16495244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
16505244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
16515244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
16525244eac9SBenno Rice 		 * instead of a multiply.)
16535244eac9SBenno Rice 		 */
165459276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
165559276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
16565244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
16575244eac9SBenno Rice 			continue;
16585244eac9SBenno Rice 		n = hash >> 5;
16595244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
166059276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
166159276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
16625244eac9SBenno Rice 			/* anything free in this bucket? */
166359276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
166459276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
16655244eac9SBenno Rice 				continue;
16665244eac9SBenno Rice 			}
16670dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
16685244eac9SBenno Rice 			mask = 1 << i;
16695244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
16705244eac9SBenno Rice 			hash |= i;
16715244eac9SBenno Rice 		}
167259276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
16735244eac9SBenno Rice 		for (i = 0; i < 16; i++)
16745244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1675e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
16765244eac9SBenno Rice 		return;
16775244eac9SBenno Rice 	}
16785244eac9SBenno Rice 
1679e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
168059276937SPeter Grehan 	panic("moea_pinit: out of segments");
16815244eac9SBenno Rice }
16825244eac9SBenno Rice 
16835244eac9SBenno Rice /*
16845244eac9SBenno Rice  * Initialize the pmap associated with process 0.
16855244eac9SBenno Rice  */
16865244eac9SBenno Rice void
168759276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
16885244eac9SBenno Rice {
16895244eac9SBenno Rice 
169059276937SPeter Grehan 	moea_pinit(mmu, pm);
16915244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
16925244eac9SBenno Rice }
16935244eac9SBenno Rice 
1694e79f59e8SBenno Rice /*
1695e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1696e79f59e8SBenno Rice  */
16975244eac9SBenno Rice void
169859276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
169959276937SPeter Grehan     vm_prot_t prot)
17005244eac9SBenno Rice {
1701e79f59e8SBenno Rice 	struct	pvo_entry *pvo;
1702e79f59e8SBenno Rice 	struct	pte *pt;
1703e79f59e8SBenno Rice 	int	pteidx;
1704e79f59e8SBenno Rice 
1705e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
170659276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1707e79f59e8SBenno Rice 
1708e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
170959276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1710e79f59e8SBenno Rice 		return;
1711e79f59e8SBenno Rice 	}
1712e79f59e8SBenno Rice 
17133d2e54c3SAlan Cox 	vm_page_lock_queues();
171448d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1715e79f59e8SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
171659276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
1717e79f59e8SBenno Rice 		if (pvo == NULL)
1718e79f59e8SBenno Rice 			continue;
1719e79f59e8SBenno Rice 
1720e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1721e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1722e79f59e8SBenno Rice 
1723e79f59e8SBenno Rice 		/*
1724e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1725e79f59e8SBenno Rice 		 * copy.
1726e79f59e8SBenno Rice 		 */
172759276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, pteidx);
1728e79f59e8SBenno Rice 		/*
1729e79f59e8SBenno Rice 		 * Change the protection of the page.
1730e79f59e8SBenno Rice 		 */
173152a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
173252a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1733e79f59e8SBenno Rice 
1734e79f59e8SBenno Rice 		/*
1735e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1736e79f59e8SBenno Rice 		 */
1737d644a0b7SAlan Cox 		if (pt != NULL) {
173852a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1739d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1740d644a0b7SAlan Cox 		}
1741e79f59e8SBenno Rice 	}
17423d2e54c3SAlan Cox 	vm_page_unlock_queues();
174348d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
17445244eac9SBenno Rice }
17455244eac9SBenno Rice 
174688afb2a3SBenno Rice /*
174788afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
174888afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
174988afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
175088afb2a3SBenno Rice  */
17515244eac9SBenno Rice void
175259276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
17535244eac9SBenno Rice {
175403b6e025SPeter Grehan 	vm_offset_t va;
17555244eac9SBenno Rice 
175603b6e025SPeter Grehan 	va = sva;
175703b6e025SPeter Grehan 	while (count-- > 0) {
175859276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
175903b6e025SPeter Grehan 		va += PAGE_SIZE;
176003b6e025SPeter Grehan 		m++;
176103b6e025SPeter Grehan 	}
17625244eac9SBenno Rice }
17635244eac9SBenno Rice 
176488afb2a3SBenno Rice /*
176588afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
176659276937SPeter Grehan  * temporary mappings entered by moea_qenter.
176788afb2a3SBenno Rice  */
17685244eac9SBenno Rice void
176959276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
17705244eac9SBenno Rice {
177103b6e025SPeter Grehan 	vm_offset_t va;
177288afb2a3SBenno Rice 
177303b6e025SPeter Grehan 	va = sva;
177403b6e025SPeter Grehan 	while (count-- > 0) {
177559276937SPeter Grehan 		moea_kremove(mmu, va);
177603b6e025SPeter Grehan 		va += PAGE_SIZE;
177703b6e025SPeter Grehan 	}
17785244eac9SBenno Rice }
17795244eac9SBenno Rice 
17805244eac9SBenno Rice void
178159276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
17825244eac9SBenno Rice {
178332bc7846SPeter Grehan         int idx, mask;
178432bc7846SPeter Grehan 
178532bc7846SPeter Grehan 	/*
178632bc7846SPeter Grehan 	 * Free segment register's VSID
178732bc7846SPeter Grehan 	 */
178832bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
178959276937SPeter Grehan                 panic("moea_release");
179032bc7846SPeter Grehan 
1791e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
179232bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
179332bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
179432bc7846SPeter Grehan         idx /= VSID_NBPW;
179559276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1796e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
179748d0b1a0SAlan Cox 	PMAP_LOCK_DESTROY(pmap);
17985244eac9SBenno Rice }
17995244eac9SBenno Rice 
180088afb2a3SBenno Rice /*
180188afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
180288afb2a3SBenno Rice  */
18035244eac9SBenno Rice void
180459276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
18055244eac9SBenno Rice {
180688afb2a3SBenno Rice 	struct	pvo_entry *pvo;
180788afb2a3SBenno Rice 	int	pteidx;
180888afb2a3SBenno Rice 
18093d2e54c3SAlan Cox 	vm_page_lock_queues();
181048d0b1a0SAlan Cox 	PMAP_LOCK(pm);
181188afb2a3SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
181259276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
181388afb2a3SBenno Rice 		if (pvo != NULL) {
181459276937SPeter Grehan 			moea_pvo_remove(pvo, pteidx);
181588afb2a3SBenno Rice 		}
181688afb2a3SBenno Rice 	}
181748d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
181894aa7aecSPeter Grehan 	vm_page_unlock_queues();
18195244eac9SBenno Rice }
18205244eac9SBenno Rice 
1821e79f59e8SBenno Rice /*
182259276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
182303b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
182403b6e025SPeter Grehan  */
182503b6e025SPeter Grehan void
182659276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
182703b6e025SPeter Grehan {
182803b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
182903b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
183048d0b1a0SAlan Cox 	pmap_t	pmap;
183103b6e025SPeter Grehan 
18323c4a2440SAlan Cox 	vm_page_lock_queues();
183303b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
183403b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
183503b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
183603b6e025SPeter Grehan 
183759276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
183848d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
183948d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
184059276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
184148d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
184203b6e025SPeter Grehan 	}
1843062c8f4cSNathan Whitehorn 	if ((m->flags & PG_WRITEABLE) && moea_is_modified(mmu, m)) {
1844c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1845062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1846062c8f4cSNathan Whitehorn 	}
184703b6e025SPeter Grehan 	vm_page_flag_clear(m, PG_WRITEABLE);
18483c4a2440SAlan Cox 	vm_page_unlock_queues();
184903b6e025SPeter Grehan }
185003b6e025SPeter Grehan 
185103b6e025SPeter Grehan /*
18525244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
185359276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
18545244eac9SBenno Rice  * calculated.
18555244eac9SBenno Rice  */
18565244eac9SBenno Rice static vm_offset_t
185759276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
18585244eac9SBenno Rice {
18595244eac9SBenno Rice 	vm_offset_t	s, e;
18605244eac9SBenno Rice 	int		i, j;
18615244eac9SBenno Rice 
18625244eac9SBenno Rice 	size = round_page(size);
18635244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
18645244eac9SBenno Rice 		if (align != 0)
18655244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
18665244eac9SBenno Rice 		else
18675244eac9SBenno Rice 			s = phys_avail[i];
18685244eac9SBenno Rice 		e = s + size;
18695244eac9SBenno Rice 
18705244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
18715244eac9SBenno Rice 			continue;
18725244eac9SBenno Rice 
18735244eac9SBenno Rice 		if (s == phys_avail[i]) {
18745244eac9SBenno Rice 			phys_avail[i] += size;
18755244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
18765244eac9SBenno Rice 			phys_avail[i + 1] -= size;
18775244eac9SBenno Rice 		} else {
18785244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
18795244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
18805244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
18815244eac9SBenno Rice 			}
18825244eac9SBenno Rice 
18835244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
18845244eac9SBenno Rice 			phys_avail[i + 1] = s;
18855244eac9SBenno Rice 			phys_avail[i + 2] = e;
18865244eac9SBenno Rice 			phys_avail_count++;
18875244eac9SBenno Rice 		}
18885244eac9SBenno Rice 
18895244eac9SBenno Rice 		return (s);
18905244eac9SBenno Rice 	}
189159276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
18925244eac9SBenno Rice }
18935244eac9SBenno Rice 
18945244eac9SBenno Rice static void
189559276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
18965244eac9SBenno Rice {
18975244eac9SBenno Rice 	__syncicache((void *)pa, len);
18985244eac9SBenno Rice }
18995244eac9SBenno Rice 
19005244eac9SBenno Rice static int
190159276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
19025244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
19035244eac9SBenno Rice {
19045244eac9SBenno Rice 	struct	pvo_entry *pvo;
19055244eac9SBenno Rice 	u_int	sr;
19065244eac9SBenno Rice 	int	first;
19075244eac9SBenno Rice 	u_int	ptegidx;
19085244eac9SBenno Rice 	int	i;
190932bc7846SPeter Grehan 	int     bootstrap;
19105244eac9SBenno Rice 
191159276937SPeter Grehan 	moea_pvo_enter_calls++;
19128207b362SBenno Rice 	first = 0;
191332bc7846SPeter Grehan 	bootstrap = 0;
191432bc7846SPeter Grehan 
19155244eac9SBenno Rice 	/*
19165244eac9SBenno Rice 	 * Compute the PTE Group index.
19175244eac9SBenno Rice 	 */
19185244eac9SBenno Rice 	va &= ~ADDR_POFF;
19195244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
19205244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
19215244eac9SBenno Rice 
19225244eac9SBenno Rice 	/*
19235244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
19245244eac9SBenno Rice 	 * there is a mapping.
19255244eac9SBenno Rice 	 */
192659276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
192759276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19285244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
192952a7870dSNathan Whitehorn 			if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
193052a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1931fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
193259276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
193349f8f727SBenno Rice 				return (0);
1934fafc7362SBenno Rice 			}
193559276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
19365244eac9SBenno Rice 			break;
19375244eac9SBenno Rice 		}
19385244eac9SBenno Rice 	}
19395244eac9SBenno Rice 
19405244eac9SBenno Rice 	/*
19415244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
19425244eac9SBenno Rice 	 */
194359276937SPeter Grehan 	if (moea_initialized) {
1944378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
194549f8f727SBenno Rice 	} else {
194659276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
194759276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
194859276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
19490d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
195049f8f727SBenno Rice 		}
195159276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
195259276937SPeter Grehan 		moea_bpvo_pool_index++;
195332bc7846SPeter Grehan 		bootstrap = 1;
195449f8f727SBenno Rice 	}
19555244eac9SBenno Rice 
19565244eac9SBenno Rice 	if (pvo == NULL) {
195759276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
19585244eac9SBenno Rice 		return (ENOMEM);
19595244eac9SBenno Rice 	}
19605244eac9SBenno Rice 
196159276937SPeter Grehan 	moea_pvo_entries++;
19625244eac9SBenno Rice 	pvo->pvo_vaddr = va;
19635244eac9SBenno Rice 	pvo->pvo_pmap = pm;
196459276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
19655244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
19665244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
19675244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
19685244eac9SBenno Rice 	if (flags & PVO_WIRED)
19695244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
197059276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
19715244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
197232bc7846SPeter Grehan 	if (bootstrap)
197332bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
19744dba5df1SPeter Grehan 	if (flags & PVO_FAKE)
19754dba5df1SPeter Grehan 		pvo->pvo_vaddr |= PVO_FAKE;
19764dba5df1SPeter Grehan 
197752a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
19785244eac9SBenno Rice 
19795244eac9SBenno Rice 	/*
19805244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
19815244eac9SBenno Rice 	 * item.
19825244eac9SBenno Rice 	 */
19838207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
19848207b362SBenno Rice 		first = 1;
19855244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
19864dba5df1SPeter Grehan 
198752a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
1988c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
1989c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
19905244eac9SBenno Rice 
19915244eac9SBenno Rice 	/*
19925244eac9SBenno Rice 	 * We hope this succeeds but it isn't required.
19935244eac9SBenno Rice 	 */
199452a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
19955244eac9SBenno Rice 	if (i >= 0) {
19965244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
19975244eac9SBenno Rice 	} else {
199859276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
199959276937SPeter Grehan 		moea_pte_overflow++;
20005244eac9SBenno Rice 	}
200159276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20024dba5df1SPeter Grehan 
20035244eac9SBenno Rice 	return (first ? ENOENT : 0);
20045244eac9SBenno Rice }
20055244eac9SBenno Rice 
20065244eac9SBenno Rice static void
200759276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
20085244eac9SBenno Rice {
20095244eac9SBenno Rice 	struct	pte *pt;
20105244eac9SBenno Rice 
20115244eac9SBenno Rice 	/*
20125244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
20135244eac9SBenno Rice 	 * save the ref & cfg bits).
20145244eac9SBenno Rice 	 */
201559276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
20165244eac9SBenno Rice 	if (pt != NULL) {
201752a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
2018d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
20195244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
20205244eac9SBenno Rice 	} else {
202159276937SPeter Grehan 		moea_pte_overflow--;
20225244eac9SBenno Rice 	}
20235244eac9SBenno Rice 
20245244eac9SBenno Rice 	/*
20255244eac9SBenno Rice 	 * Update our statistics.
20265244eac9SBenno Rice 	 */
20275244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
202852a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED)
20295244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
20305244eac9SBenno Rice 
20315244eac9SBenno Rice 	/*
20325244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
20335244eac9SBenno Rice 	 */
20344dba5df1SPeter Grehan 	if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) {
20355244eac9SBenno Rice 		struct	vm_page *pg;
20365244eac9SBenno Rice 
203752a7870dSNathan Whitehorn 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
20385244eac9SBenno Rice 		if (pg != NULL) {
203952a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
20405244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
20415244eac9SBenno Rice 		}
20425244eac9SBenno Rice 	}
20435244eac9SBenno Rice 
20445244eac9SBenno Rice 	/*
20455244eac9SBenno Rice 	 * Remove this PVO from the PV list.
20465244eac9SBenno Rice 	 */
20475244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
20485244eac9SBenno Rice 
20495244eac9SBenno Rice 	/*
20505244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
20515244eac9SBenno Rice 	 * if we aren't going to reuse it.
20525244eac9SBenno Rice 	 */
20535244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
205449f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
205559276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
205659276937SPeter Grehan 		    moea_upvo_zone, pvo);
205759276937SPeter Grehan 	moea_pvo_entries--;
205859276937SPeter Grehan 	moea_pvo_remove_calls++;
20595244eac9SBenno Rice }
20605244eac9SBenno Rice 
20615244eac9SBenno Rice static __inline int
206259276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
20635244eac9SBenno Rice {
20645244eac9SBenno Rice 	int	pteidx;
20655244eac9SBenno Rice 
20665244eac9SBenno Rice 	/*
20675244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
20685244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
20695244eac9SBenno Rice 	 * noticing the HID bit.
20705244eac9SBenno Rice 	 */
20715244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
207252a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
207359276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
20745244eac9SBenno Rice 
20755244eac9SBenno Rice 	return (pteidx);
20765244eac9SBenno Rice }
20775244eac9SBenno Rice 
20785244eac9SBenno Rice static struct pvo_entry *
207959276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
20805244eac9SBenno Rice {
20815244eac9SBenno Rice 	struct	pvo_entry *pvo;
20825244eac9SBenno Rice 	int	ptegidx;
20835244eac9SBenno Rice 	u_int	sr;
20845244eac9SBenno Rice 
20855244eac9SBenno Rice 	va &= ~ADDR_POFF;
20865244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
20875244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
20885244eac9SBenno Rice 
208959276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
209059276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20915244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
20925244eac9SBenno Rice 			if (pteidx_p)
209359276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2094f489bf21SAlan Cox 			break;
20955244eac9SBenno Rice 		}
20965244eac9SBenno Rice 	}
209759276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20985244eac9SBenno Rice 
2099f489bf21SAlan Cox 	return (pvo);
21005244eac9SBenno Rice }
21015244eac9SBenno Rice 
21025244eac9SBenno Rice static struct pte *
210359276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
21045244eac9SBenno Rice {
21055244eac9SBenno Rice 	struct	pte *pt;
21065244eac9SBenno Rice 
21075244eac9SBenno Rice 	/*
21085244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
21095244eac9SBenno Rice 	 */
21105244eac9SBenno Rice 	if (pteidx == -1) {
21115244eac9SBenno Rice 		int	ptegidx;
21125244eac9SBenno Rice 		u_int	sr;
21135244eac9SBenno Rice 
21145244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
21155244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
211659276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
21175244eac9SBenno Rice 	}
21185244eac9SBenno Rice 
211959276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2120d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
21215244eac9SBenno Rice 
212252a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
212359276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
21245244eac9SBenno Rice 		    "valid pte index", pvo);
21255244eac9SBenno Rice 	}
21265244eac9SBenno Rice 
212752a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
212859276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
21295244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
21305244eac9SBenno Rice 	}
21315244eac9SBenno Rice 
213252a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
213352a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
213459276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
213559276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
21365244eac9SBenno Rice 		}
21375244eac9SBenno Rice 
213852a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
21395244eac9SBenno Rice 		    != 0) {
214059276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
214159276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
21425244eac9SBenno Rice 		}
21435244eac9SBenno Rice 
2144d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
21455244eac9SBenno Rice 		return (pt);
21465244eac9SBenno Rice 	}
21475244eac9SBenno Rice 
214852a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
214959276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
215059276937SPeter Grehan 		    "moea_pteg_table but valid in pvo", pvo, pt);
21515244eac9SBenno Rice 	}
21525244eac9SBenno Rice 
2153d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
21545244eac9SBenno Rice 	return (NULL);
21555244eac9SBenno Rice }
21565244eac9SBenno Rice 
21575244eac9SBenno Rice /*
21585244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
21595244eac9SBenno Rice  */
21605244eac9SBenno Rice int
216159276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
21625244eac9SBenno Rice {
21635244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
21645244eac9SBenno Rice 	struct	pvo_entry *pvo;
21655244eac9SBenno Rice 	int	ptegidx, i, j;
21665244eac9SBenno Rice 	u_int	sr;
21675244eac9SBenno Rice 	struct	pteg *pteg;
21685244eac9SBenno Rice 	struct	pte *pt;
21695244eac9SBenno Rice 
217059276937SPeter Grehan 	moea_pte_spills++;
21715244eac9SBenno Rice 
2172d080d5fdSBenno Rice 	sr = mfsrin(addr);
21735244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
21745244eac9SBenno Rice 
21755244eac9SBenno Rice 	/*
21765244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
21775244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
21785244eac9SBenno Rice 	 */
217959276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
218059276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
21815244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
21825244eac9SBenno Rice 	i &= 7;
21835244eac9SBenno Rice 	pt = &pteg->pt[i];
21845244eac9SBenno Rice 
21855244eac9SBenno Rice 	source_pvo = NULL;
21865244eac9SBenno Rice 	victim_pvo = NULL;
218759276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21885244eac9SBenno Rice 		/*
21895244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
21905244eac9SBenno Rice 		 */
219159276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);
21925244eac9SBenno Rice 		if (source_pvo == NULL &&
219352a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
219452a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
21955244eac9SBenno Rice 			/*
21965244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
21975244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
21985244eac9SBenno Rice 			 */
219952a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
22005244eac9SBenno Rice 
22015244eac9SBenno Rice 			if (j >= 0) {
22025244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
220359276937SPeter Grehan 				moea_pte_overflow--;
220459276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);
220559276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
22065244eac9SBenno Rice 				return (1);
22075244eac9SBenno Rice 			}
22085244eac9SBenno Rice 
22095244eac9SBenno Rice 			source_pvo = pvo;
22105244eac9SBenno Rice 
22115244eac9SBenno Rice 			if (victim_pvo != NULL)
22125244eac9SBenno Rice 				break;
22135244eac9SBenno Rice 		}
22145244eac9SBenno Rice 
22155244eac9SBenno Rice 		/*
22165244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
22175244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
22185244eac9SBenno Rice 		 */
22195244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
222052a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22215244eac9SBenno Rice 			victim_pvo = pvo;
22225244eac9SBenno Rice 			if (source_pvo != NULL)
22235244eac9SBenno Rice 				break;
22245244eac9SBenno Rice 		}
22255244eac9SBenno Rice 	}
22265244eac9SBenno Rice 
2227f489bf21SAlan Cox 	if (source_pvo == NULL) {
222859276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
22295244eac9SBenno Rice 		return (0);
2230f489bf21SAlan Cox 	}
22315244eac9SBenno Rice 
22325244eac9SBenno Rice 	if (victim_pvo == NULL) {
22335244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
223459276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
22355244eac9SBenno Rice 			    "entry", pt);
22365244eac9SBenno Rice 
22375244eac9SBenno Rice 		/*
22385244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
22395244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
22405244eac9SBenno Rice 		 */
224159276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
22425244eac9SBenno Rice 		    pvo_olink) {
224359276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);
22445244eac9SBenno Rice 			/*
22455244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
22465244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
22475244eac9SBenno Rice 			 */
224852a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
22495244eac9SBenno Rice 				victim_pvo = pvo;
22505244eac9SBenno Rice 				break;
22515244eac9SBenno Rice 			}
22525244eac9SBenno Rice 		}
22535244eac9SBenno Rice 
22545244eac9SBenno Rice 		if (victim_pvo == NULL)
225559276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
22565244eac9SBenno Rice 			    "entry", pt);
22575244eac9SBenno Rice 	}
22585244eac9SBenno Rice 
22595244eac9SBenno Rice 	/*
22605244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
22615244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
22625244eac9SBenno Rice 	 * contained in the TLB entry.
22635244eac9SBenno Rice 	 */
226452a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
22655244eac9SBenno Rice 
226652a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
226752a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
22685244eac9SBenno Rice 
22695244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
22705244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
227159276937SPeter Grehan 	moea_pte_replacements++;
22725244eac9SBenno Rice 
227359276937SPeter Grehan 	MOEA_PVO_CHECK(victim_pvo);
227459276937SPeter Grehan 	MOEA_PVO_CHECK(source_pvo);
22755244eac9SBenno Rice 
227659276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22775244eac9SBenno Rice 	return (1);
22785244eac9SBenno Rice }
22795244eac9SBenno Rice 
22805244eac9SBenno Rice static int
228159276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
22825244eac9SBenno Rice {
22835244eac9SBenno Rice 	struct	pte *pt;
22845244eac9SBenno Rice 	int	i;
22855244eac9SBenno Rice 
2286d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2287d644a0b7SAlan Cox 
22885244eac9SBenno Rice 	/*
22895244eac9SBenno Rice 	 * First try primary hash.
22905244eac9SBenno Rice 	 */
229159276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22925244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22935244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
229459276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22955244eac9SBenno Rice 			return (i);
22965244eac9SBenno Rice 		}
22975244eac9SBenno Rice 	}
22985244eac9SBenno Rice 
22995244eac9SBenno Rice 	/*
23005244eac9SBenno Rice 	 * Now try secondary hash.
23015244eac9SBenno Rice 	 */
230259276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2303bd8e6f87SPeter Grehan 
230459276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
23055244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
23065244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
230759276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
23085244eac9SBenno Rice 			return (i);
23095244eac9SBenno Rice 		}
23105244eac9SBenno Rice 	}
23115244eac9SBenno Rice 
231259276937SPeter Grehan 	panic("moea_pte_insert: overflow");
23135244eac9SBenno Rice 	return (-1);
23145244eac9SBenno Rice }
23155244eac9SBenno Rice 
23165244eac9SBenno Rice static boolean_t
231759276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
23185244eac9SBenno Rice {
23195244eac9SBenno Rice 	struct	pvo_entry *pvo;
23205244eac9SBenno Rice 	struct	pte *pt;
23215244eac9SBenno Rice 
232259276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
23235244eac9SBenno Rice 		return (TRUE);
23245244eac9SBenno Rice 
2325c46b90e9SAlan Cox 	vm_page_lock_queues();
23265244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
232759276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23285244eac9SBenno Rice 
23295244eac9SBenno Rice 		/*
23305244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
23315244eac9SBenno Rice 		 * success.
23325244eac9SBenno Rice 		 */
233352a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
233459276937SPeter Grehan 			moea_attr_save(m, ptebit);
233559276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);	/* sanity check */
2336c46b90e9SAlan Cox 			vm_page_unlock_queues();
23375244eac9SBenno Rice 			return (TRUE);
23385244eac9SBenno Rice 		}
23395244eac9SBenno Rice 	}
23405244eac9SBenno Rice 
23415244eac9SBenno Rice 	/*
23425244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
23435244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
23445244eac9SBenno Rice 	 * the PTEs.
23455244eac9SBenno Rice 	 */
2346e4f72b32SMarcel Moolenaar 	powerpc_sync();
23475244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
234859276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23495244eac9SBenno Rice 
23505244eac9SBenno Rice 		/*
23515244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
23525244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
23535244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
23545244eac9SBenno Rice 		 */
235559276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23565244eac9SBenno Rice 		if (pt != NULL) {
235752a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2358d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
235952a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
236059276937SPeter Grehan 				moea_attr_save(m, ptebit);
236159276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);	/* sanity check */
2362c46b90e9SAlan Cox 				vm_page_unlock_queues();
23635244eac9SBenno Rice 				return (TRUE);
23645244eac9SBenno Rice 			}
23655244eac9SBenno Rice 		}
23665244eac9SBenno Rice 	}
23675244eac9SBenno Rice 
2368c46b90e9SAlan Cox 	vm_page_unlock_queues();
23694f7daed0SAndrew Gallatin 	return (FALSE);
23705244eac9SBenno Rice }
23715244eac9SBenno Rice 
237203b6e025SPeter Grehan static u_int
2373ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
23745244eac9SBenno Rice {
237503b6e025SPeter Grehan 	u_int	count;
23765244eac9SBenno Rice 	struct	pvo_entry *pvo;
23775244eac9SBenno Rice 	struct	pte *pt;
2378ce186587SAlan Cox 
2379ce186587SAlan Cox 	vm_page_lock_queues();
23805244eac9SBenno Rice 
23815244eac9SBenno Rice 	/*
23825244eac9SBenno Rice 	 * Clear the cached value.
23835244eac9SBenno Rice 	 */
238459276937SPeter Grehan 	moea_attr_clear(m, ptebit);
23855244eac9SBenno Rice 
23865244eac9SBenno Rice 	/*
23875244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
23885244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
23895244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
23905244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
23915244eac9SBenno Rice 	 * REF/CHG bits.
23925244eac9SBenno Rice 	 */
2393e4f72b32SMarcel Moolenaar 	powerpc_sync();
23945244eac9SBenno Rice 
23955244eac9SBenno Rice 	/*
23965244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
23975244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
23985244eac9SBenno Rice 	 */
239903b6e025SPeter Grehan 	count = 0;
24005244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
240159276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
240259276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
24035244eac9SBenno Rice 		if (pt != NULL) {
240452a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
240552a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
240603b6e025SPeter Grehan 				count++;
240759276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
24085244eac9SBenno Rice 			}
2409d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
241003b6e025SPeter Grehan 		}
241152a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
241259276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
24135244eac9SBenno Rice 	}
24145244eac9SBenno Rice 
2415ce186587SAlan Cox 	vm_page_unlock_queues();
241603b6e025SPeter Grehan 	return (count);
2417bdf71f56SBenno Rice }
24188bbfa33aSBenno Rice 
24198bbfa33aSBenno Rice /*
242032bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
242132bc7846SPeter Grehan  */
242232bc7846SPeter Grehan static int
242359276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
242432bc7846SPeter Grehan {
242532bc7846SPeter Grehan 	u_int prot;
242632bc7846SPeter Grehan 	u_int32_t start;
242732bc7846SPeter Grehan 	u_int32_t end;
242832bc7846SPeter Grehan 	u_int32_t bat_ble;
242932bc7846SPeter Grehan 
243032bc7846SPeter Grehan 	/*
243132bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
243232bc7846SPeter Grehan 	 */
2433*c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
243432bc7846SPeter Grehan 		return (EINVAL);
243532bc7846SPeter Grehan 
243632bc7846SPeter Grehan 	/*
243732bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
243832bc7846SPeter Grehan 	 * so it can function as an i/o page
243932bc7846SPeter Grehan 	 */
244032bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
244132bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
244232bc7846SPeter Grehan 		return (EPERM);
244332bc7846SPeter Grehan 
244432bc7846SPeter Grehan 	/*
244532bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
244632bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
244732bc7846SPeter Grehan 	 * not requiring masking)
244832bc7846SPeter Grehan 	 */
244932bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
245032bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
245132bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
245232bc7846SPeter Grehan 
245332bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
245432bc7846SPeter Grehan 		return (ERANGE);
245532bc7846SPeter Grehan 
245632bc7846SPeter Grehan 	return (0);
245732bc7846SPeter Grehan }
245832bc7846SPeter Grehan 
245959276937SPeter Grehan boolean_t
246059276937SPeter Grehan moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2461c0763d37SSuleiman Souhlal {
2462c0763d37SSuleiman Souhlal 	int i;
2463c0763d37SSuleiman Souhlal 
2464c0763d37SSuleiman Souhlal 	/*
2465c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2466c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2467c0763d37SSuleiman Souhlal 	 */
2468c0763d37SSuleiman Souhlal 
2469c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
247059276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2471c0763d37SSuleiman Souhlal 			return (0);
2472c0763d37SSuleiman Souhlal 
2473c0763d37SSuleiman Souhlal 	return (EFAULT);
2474c0763d37SSuleiman Souhlal }
247532bc7846SPeter Grehan 
247632bc7846SPeter Grehan /*
24778bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
24788bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
24798bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
24808bbfa33aSBenno Rice  * NOT real memory.
24818bbfa33aSBenno Rice  */
24828bbfa33aSBenno Rice void *
248359276937SPeter Grehan moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
24848bbfa33aSBenno Rice {
2485c1f4123bSNathan Whitehorn 
2486c1f4123bSNathan Whitehorn 	return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2487c1f4123bSNathan Whitehorn }
2488c1f4123bSNathan Whitehorn 
2489c1f4123bSNathan Whitehorn void *
2490c1f4123bSNathan Whitehorn moea_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma)
2491c1f4123bSNathan Whitehorn {
249232bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
249332bc7846SPeter Grehan 	int i;
24948bbfa33aSBenno Rice 
249532bc7846SPeter Grehan 	ppa = trunc_page(pa);
24968bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
24978bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
24988bbfa33aSBenno Rice 
249932bc7846SPeter Grehan 	/*
250032bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
250132bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
250232bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
250332bc7846SPeter Grehan 	 */
250432bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
250559276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
250632bc7846SPeter Grehan 			return ((void *) pa);
250732bc7846SPeter Grehan 	}
250832bc7846SPeter Grehan 
2509e53f32acSAlan Cox 	va = kmem_alloc_nofault(kernel_map, size);
25108bbfa33aSBenno Rice 	if (!va)
251159276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
25128bbfa33aSBenno Rice 
25138bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
2514c1f4123bSNathan Whitehorn 		moea_kenter_attr(mmu, tmpva, ppa, ma);
2515e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
25168bbfa33aSBenno Rice 		size -= PAGE_SIZE;
25178bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
251832bc7846SPeter Grehan 		ppa += PAGE_SIZE;
25198bbfa33aSBenno Rice 	}
25208bbfa33aSBenno Rice 
25218bbfa33aSBenno Rice 	return ((void *)(va + offset));
25228bbfa33aSBenno Rice }
25238bbfa33aSBenno Rice 
25248bbfa33aSBenno Rice void
252559276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
25268bbfa33aSBenno Rice {
25278bbfa33aSBenno Rice 	vm_offset_t base, offset;
25288bbfa33aSBenno Rice 
252932bc7846SPeter Grehan 	/*
253032bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
253132bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
253232bc7846SPeter Grehan 	 */
2533ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
25348bbfa33aSBenno Rice 		base = trunc_page(va);
25358bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
25368bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
25378bbfa33aSBenno Rice 		kmem_free(kernel_map, base, size);
25388bbfa33aSBenno Rice 	}
253932bc7846SPeter Grehan }
25401a4fcaebSMarcel Moolenaar 
25411a4fcaebSMarcel Moolenaar static void
25421a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
25431a4fcaebSMarcel Moolenaar {
25441a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
25451a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
25461a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
25471a4fcaebSMarcel Moolenaar 	vm_size_t len;
25481a4fcaebSMarcel Moolenaar 
25491a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
25501a4fcaebSMarcel Moolenaar 	while (sz > 0) {
25511a4fcaebSMarcel Moolenaar 		lim = round_page(va);
25521a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
25531a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
25541a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
25551a4fcaebSMarcel Moolenaar 			pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
25561a4fcaebSMarcel Moolenaar 			    (va & ADDR_POFF);
25571a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
25581a4fcaebSMarcel Moolenaar 		}
25591a4fcaebSMarcel Moolenaar 		va += len;
25601a4fcaebSMarcel Moolenaar 		sz -= len;
25611a4fcaebSMarcel Moolenaar 	}
25621a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
25631a4fcaebSMarcel Moolenaar }
2564