xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision 5b43c63ded98041e53837554a7afc8a2d5bf782b)
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>
14431c82d03SBenno Rice #include <machine/powerpc.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>
1505244eac9SBenno Rice #include <machine/sr.h>
15159276937SPeter Grehan #include <machine/mmuvar.h>
152f9bac91bSBenno Rice 
15359276937SPeter Grehan #include "mmu_if.h"
15459276937SPeter Grehan 
15559276937SPeter Grehan #define	MOEA_DEBUG
156f9bac91bSBenno Rice 
1575244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
158f9bac91bSBenno Rice 
1595244eac9SBenno Rice #define	TLBIE(va)	__asm __volatile("tlbie %0" :: "r"(va))
1605244eac9SBenno Rice #define	TLBSYNC()	__asm __volatile("tlbsync");
1615244eac9SBenno Rice #define	SYNC()		__asm __volatile("sync");
1625244eac9SBenno Rice #define	EIEIO()		__asm __volatile("eieio");
1635244eac9SBenno Rice 
1645244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1655244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1665244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1675244eac9SBenno Rice 
1684dba5df1SPeter Grehan #define	PVO_PTEGIDX_MASK	0x007		/* which PTEG slot */
1694dba5df1SPeter Grehan #define	PVO_PTEGIDX_VALID	0x008		/* slot is valid */
1704dba5df1SPeter Grehan #define	PVO_WIRED		0x010		/* PVO entry is wired */
1714dba5df1SPeter Grehan #define	PVO_MANAGED		0x020		/* PVO entry is managed */
1724dba5df1SPeter Grehan #define	PVO_EXECUTABLE		0x040		/* PVO entry is executable */
1734dba5df1SPeter Grehan #define	PVO_BOOTSTRAP		0x080		/* PVO entry allocated during
17449f8f727SBenno Rice 						   bootstrap */
1754dba5df1SPeter Grehan #define PVO_FAKE		0x100		/* fictitious phys page */
1765244eac9SBenno Rice #define	PVO_VADDR(pvo)		((pvo)->pvo_vaddr & ~ADDR_POFF)
1775244eac9SBenno Rice #define	PVO_ISEXECUTABLE(pvo)	((pvo)->pvo_vaddr & PVO_EXECUTABLE)
1784dba5df1SPeter Grehan #define PVO_ISFAKE(pvo)		((pvo)->pvo_vaddr & PVO_FAKE)
1795244eac9SBenno Rice #define	PVO_PTEGIDX_GET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK)
1805244eac9SBenno Rice #define	PVO_PTEGIDX_ISSET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID)
1815244eac9SBenno Rice #define	PVO_PTEGIDX_CLR(pvo)	\
1825244eac9SBenno Rice 	((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK)))
1835244eac9SBenno Rice #define	PVO_PTEGIDX_SET(pvo, i)	\
1845244eac9SBenno Rice 	((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID))
1855244eac9SBenno Rice 
18659276937SPeter Grehan #define	MOEA_PVO_CHECK(pvo)
1875244eac9SBenno Rice 
1885244eac9SBenno Rice struct ofw_map {
1895244eac9SBenno Rice 	vm_offset_t	om_va;
1905244eac9SBenno Rice 	vm_size_t	om_len;
1915244eac9SBenno Rice 	vm_offset_t	om_pa;
1925244eac9SBenno Rice 	u_int		om_mode;
1935244eac9SBenno Rice };
194f9bac91bSBenno Rice 
1955244eac9SBenno Rice /*
1965244eac9SBenno Rice  * Map of physical memory regions.
1975244eac9SBenno Rice  */
19831c82d03SBenno Rice static struct	mem_region *regions;
19931c82d03SBenno Rice static struct	mem_region *pregions;
20059276937SPeter Grehan u_int           phys_avail_count;
20131c82d03SBenno Rice int		regions_sz, pregions_sz;
202aa39961eSBenno Rice static struct	ofw_map *translations;
2035244eac9SBenno Rice 
2045244eac9SBenno Rice extern struct pmap ofw_pmap;
205f9bac91bSBenno Rice 
20659276937SPeter Grehan 
20759276937SPeter Grehan 
208f9bac91bSBenno Rice /*
209f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
210f489bf21SAlan Cox  */
21159276937SPeter Grehan struct mtx	moea_table_mutex;
212f489bf21SAlan Cox 
213f489bf21SAlan Cox /*
2145244eac9SBenno Rice  * PTEG data.
215f9bac91bSBenno Rice  */
21659276937SPeter Grehan static struct	pteg *moea_pteg_table;
21759276937SPeter Grehan u_int		moea_pteg_count;
21859276937SPeter Grehan u_int		moea_pteg_mask;
2195244eac9SBenno Rice 
2205244eac9SBenno Rice /*
2215244eac9SBenno Rice  * PVO data.
2225244eac9SBenno Rice  */
22359276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
22459276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
22559276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
22659276937SPeter Grehan struct	pvo_head moea_pvo_unmanaged =
22759276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_unmanaged);	/* list of unmanaged pages */
2285244eac9SBenno Rice 
22959276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
23059276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2315244eac9SBenno Rice 
2320d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
23359276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
23459276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2355244eac9SBenno Rice 
2365244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
23759276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2385244eac9SBenno Rice 
23959276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2405244eac9SBenno Rice 
2415244eac9SBenno Rice /*
2425244eac9SBenno Rice  * Statistics.
2435244eac9SBenno Rice  */
24459276937SPeter Grehan u_int	moea_pte_valid = 0;
24559276937SPeter Grehan u_int	moea_pte_overflow = 0;
24659276937SPeter Grehan u_int	moea_pte_replacements = 0;
24759276937SPeter Grehan u_int	moea_pvo_entries = 0;
24859276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
24959276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
25059276937SPeter Grehan u_int	moea_pte_spills = 0;
25159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2525244eac9SBenno Rice     0, "");
25359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
25459276937SPeter Grehan     &moea_pte_overflow, 0, "");
25559276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
25659276937SPeter Grehan     &moea_pte_replacements, 0, "");
25759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2585244eac9SBenno Rice     0, "");
25959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
26059276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
26159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
26259276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
26359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
26459276937SPeter Grehan     &moea_pte_spills, 0, "");
2655244eac9SBenno Rice 
2665244eac9SBenno Rice /*
26759276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2685244eac9SBenno Rice  */
26959276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2705244eac9SBenno Rice 
2715244eac9SBenno Rice /*
2725244eac9SBenno Rice  * PTE calls.
2735244eac9SBenno Rice  */
27459276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2755244eac9SBenno Rice 
2765244eac9SBenno Rice /*
2775244eac9SBenno Rice  * PVO calls.
2785244eac9SBenno Rice  */
27959276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2805244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
28159276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
28259276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
28359276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2845244eac9SBenno Rice 
2855244eac9SBenno Rice /*
2865244eac9SBenno Rice  * Utility routines.
2875244eac9SBenno Rice  */
288ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
289ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
29059276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
29159276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
29259276937SPeter Grehan static u_int		moea_clear_bit(vm_page_t, int, int *);
29359276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
2945244eac9SBenno Rice static void		tlbia(void);
29559276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
29659276937SPeter Grehan 
29759276937SPeter Grehan /*
29859276937SPeter Grehan  * Kernel MMU interface
29959276937SPeter Grehan  */
30059276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
30159276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
30259276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t);
30359276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
30459276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
305ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
306ce142d9eSAlan Cox     vm_prot_t);
3072053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
30859276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
30959276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
31059276937SPeter Grehan void moea_init(mmu_t);
31159276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
31259276937SPeter Grehan boolean_t moea_ts_referenced(mmu_t, vm_page_t);
31359276937SPeter Grehan vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int);
31459276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
31559677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
31659276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
31759276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
31859276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
31959276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
32059276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
32159276937SPeter Grehan void moea_release(mmu_t, pmap_t);
32259276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
32359276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
32478985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
32559276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
32659276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
32759276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
32859276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
32959276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
33059276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
33159276937SPeter Grehan void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t);
33259276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
33359276937SPeter Grehan vm_offset_t moea_kextract(mmu_t, vm_offset_t);
33459276937SPeter Grehan void moea_kenter(mmu_t, vm_offset_t, vm_offset_t);
33559276937SPeter Grehan boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
3366e4f008cSPeter Grehan boolean_t moea_page_executable(mmu_t, vm_page_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),
35059276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
35159276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
35259276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
35359677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
35459276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
35559276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
35659276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
35759276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
35859276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
35959276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
36059276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
36159276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
36278985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
36359276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
36459276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
36559276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
36659276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
36759276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
36859276937SPeter Grehan 
36959276937SPeter Grehan 	/* Internal interfaces */
37059276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
37159276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
37259276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
37359276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
37459276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
37559276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
3766e4f008cSPeter Grehan 	MMUMETHOD(mmu_page_executable,	moea_page_executable),
37759276937SPeter Grehan 
37859276937SPeter Grehan 	{ 0, 0 }
37959276937SPeter Grehan };
38059276937SPeter Grehan 
38159276937SPeter Grehan static mmu_def_t oea_mmu = {
38259276937SPeter Grehan 	MMU_TYPE_OEA,
38359276937SPeter Grehan 	moea_methods,
38459276937SPeter Grehan 	0
38559276937SPeter Grehan };
38659276937SPeter Grehan MMU_DEF(oea_mmu);
38759276937SPeter Grehan 
3885244eac9SBenno Rice 
3895244eac9SBenno Rice static __inline int
3905244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
3915244eac9SBenno Rice {
3925244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
3935244eac9SBenno Rice }
3945244eac9SBenno Rice 
3955244eac9SBenno Rice static __inline u_int
3965244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
3975244eac9SBenno Rice {
3985244eac9SBenno Rice 	u_int hash;
3995244eac9SBenno Rice 
4005244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4015244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
40259276937SPeter Grehan 	return (hash & moea_pteg_mask);
4035244eac9SBenno Rice }
4045244eac9SBenno Rice 
4055244eac9SBenno Rice static __inline struct pvo_head *
4068207b362SBenno Rice pa_to_pvoh(vm_offset_t pa, vm_page_t *pg_p)
4075244eac9SBenno Rice {
4085244eac9SBenno Rice 	struct	vm_page *pg;
4095244eac9SBenno Rice 
4105244eac9SBenno Rice 	pg = PHYS_TO_VM_PAGE(pa);
4115244eac9SBenno Rice 
4128207b362SBenno Rice 	if (pg_p != NULL)
4138207b362SBenno Rice 		*pg_p = pg;
4148207b362SBenno Rice 
4155244eac9SBenno Rice 	if (pg == NULL)
41659276937SPeter Grehan 		return (&moea_pvo_unmanaged);
4175244eac9SBenno Rice 
4185244eac9SBenno Rice 	return (&pg->md.mdpg_pvoh);
4195244eac9SBenno Rice }
4205244eac9SBenno Rice 
4215244eac9SBenno Rice static __inline struct pvo_head *
4225244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
423f9bac91bSBenno Rice {
424f9bac91bSBenno Rice 
4255244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
426f9bac91bSBenno Rice }
427f9bac91bSBenno Rice 
428f9bac91bSBenno Rice static __inline void
42959276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
430f9bac91bSBenno Rice {
431f9bac91bSBenno Rice 
432d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4335244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4345244eac9SBenno Rice }
4355244eac9SBenno Rice 
4365244eac9SBenno Rice static __inline int
43759276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4385244eac9SBenno Rice {
4395244eac9SBenno Rice 
4405244eac9SBenno Rice 	return (m->md.mdpg_attrs);
441f9bac91bSBenno Rice }
442f9bac91bSBenno Rice 
443f9bac91bSBenno Rice static __inline void
44459276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
445f9bac91bSBenno Rice {
446f9bac91bSBenno Rice 
447d644a0b7SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4485244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
449f9bac91bSBenno Rice }
450f9bac91bSBenno Rice 
451f9bac91bSBenno Rice static __inline int
45259276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
453f9bac91bSBenno Rice {
4545244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4555244eac9SBenno Rice 		return (1);
456f9bac91bSBenno Rice 
4575244eac9SBenno Rice 	return (0);
458f9bac91bSBenno Rice }
459f9bac91bSBenno Rice 
460f9bac91bSBenno Rice static __inline int
46159276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
462f9bac91bSBenno Rice {
4635244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
4645244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4655244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
466f9bac91bSBenno Rice }
467f9bac91bSBenno Rice 
4685244eac9SBenno Rice static __inline void
46959276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
470f9bac91bSBenno Rice {
471d644a0b7SAlan Cox 
472d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
473d644a0b7SAlan Cox 
474f9bac91bSBenno Rice 	/*
4755244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
4765244eac9SBenno Rice 	 * set when the real pte is set in memory.
477f9bac91bSBenno Rice 	 *
478f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
479f9bac91bSBenno Rice 	 */
4805244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4815244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
4825244eac9SBenno Rice 	pt->pte_lo = pte_lo;
483f9bac91bSBenno Rice }
484f9bac91bSBenno Rice 
4855244eac9SBenno Rice static __inline void
48659276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
487f9bac91bSBenno Rice {
488f9bac91bSBenno Rice 
489d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
4905244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
491f9bac91bSBenno Rice }
492f9bac91bSBenno Rice 
4935244eac9SBenno Rice static __inline void
49459276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
495f9bac91bSBenno Rice {
4965244eac9SBenno Rice 
497d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
498d644a0b7SAlan Cox 
4995244eac9SBenno Rice 	/*
5005244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5015244eac9SBenno Rice 	 */
5025244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
5035244eac9SBenno Rice 	TLBIE(va);
5045244eac9SBenno Rice 	EIEIO();
5055244eac9SBenno Rice 	TLBSYNC();
5065244eac9SBenno Rice 	SYNC();
5075244eac9SBenno Rice }
5085244eac9SBenno Rice 
5095244eac9SBenno Rice static __inline void
51059276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5115244eac9SBenno Rice {
5125244eac9SBenno Rice 
513d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5145244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5155244eac9SBenno Rice 
5165244eac9SBenno Rice 	/*
5175244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
5185244eac9SBenno Rice 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
5195244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5205244eac9SBenno Rice 	 */
5215244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
5225244eac9SBenno Rice 	EIEIO();
5235244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
5245244eac9SBenno Rice 	SYNC();
52559276937SPeter Grehan 	moea_pte_valid++;
5265244eac9SBenno Rice }
5275244eac9SBenno Rice 
5285244eac9SBenno Rice static __inline void
52959276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5305244eac9SBenno Rice {
5315244eac9SBenno Rice 
532d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5335244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5345244eac9SBenno Rice 
5355244eac9SBenno Rice 	/*
5365244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5375244eac9SBenno Rice 	 */
5385244eac9SBenno Rice 	SYNC();
5395244eac9SBenno Rice 
5405244eac9SBenno Rice 	/*
5415244eac9SBenno Rice 	 * Invalidate the pte.
5425244eac9SBenno Rice 	 */
5435244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5445244eac9SBenno Rice 
5455244eac9SBenno Rice 	SYNC();
5465244eac9SBenno Rice 	TLBIE(va);
5475244eac9SBenno Rice 	EIEIO();
5485244eac9SBenno Rice 	TLBSYNC();
5495244eac9SBenno Rice 	SYNC();
5505244eac9SBenno Rice 
5515244eac9SBenno Rice 	/*
5525244eac9SBenno Rice 	 * Save the reg & chg bits.
5535244eac9SBenno Rice 	 */
55459276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
55559276937SPeter Grehan 	moea_pte_valid--;
5565244eac9SBenno Rice }
5575244eac9SBenno Rice 
5585244eac9SBenno Rice static __inline void
55959276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5605244eac9SBenno Rice {
5615244eac9SBenno Rice 
5625244eac9SBenno Rice 	/*
5635244eac9SBenno Rice 	 * Invalidate the PTE
5645244eac9SBenno Rice 	 */
56559276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
56659276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
567f9bac91bSBenno Rice }
568f9bac91bSBenno Rice 
569f9bac91bSBenno Rice /*
5705244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
571f9bac91bSBenno Rice  */
5725244eac9SBenno Rice static int	mr_cmp(const void *a, const void *b);
5735244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
5745244eac9SBenno Rice 
5755244eac9SBenno Rice static int
5765244eac9SBenno Rice mr_cmp(const void *a, const void *b)
577f9bac91bSBenno Rice {
5785244eac9SBenno Rice 	const struct	mem_region *regiona;
5795244eac9SBenno Rice 	const struct	mem_region *regionb;
580f9bac91bSBenno Rice 
5815244eac9SBenno Rice 	regiona = a;
5825244eac9SBenno Rice 	regionb = b;
5835244eac9SBenno Rice 	if (regiona->mr_start < regionb->mr_start)
5845244eac9SBenno Rice 		return (-1);
5855244eac9SBenno Rice 	else if (regiona->mr_start > regionb->mr_start)
5865244eac9SBenno Rice 		return (1);
5875244eac9SBenno Rice 	else
588f9bac91bSBenno Rice 		return (0);
589f9bac91bSBenno Rice }
5905244eac9SBenno Rice 
5915244eac9SBenno Rice static int
5925244eac9SBenno Rice om_cmp(const void *a, const void *b)
5935244eac9SBenno Rice {
5945244eac9SBenno Rice 	const struct	ofw_map *mapa;
5955244eac9SBenno Rice 	const struct	ofw_map *mapb;
5965244eac9SBenno Rice 
5975244eac9SBenno Rice 	mapa = a;
5985244eac9SBenno Rice 	mapb = b;
5995244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6005244eac9SBenno Rice 		return (-1);
6015244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6025244eac9SBenno Rice 		return (1);
6035244eac9SBenno Rice 	else
6045244eac9SBenno Rice 		return (0);
605f9bac91bSBenno Rice }
606f9bac91bSBenno Rice 
607f9bac91bSBenno Rice void
60859276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
609f9bac91bSBenno Rice {
61031c82d03SBenno Rice 	ihandle_t	mmui;
6115244eac9SBenno Rice 	phandle_t	chosen, mmu;
6125244eac9SBenno Rice 	int		sz;
6135244eac9SBenno Rice 	int		i, j;
61432bc7846SPeter Grehan 	int		ofw_mappings;
615e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6165244eac9SBenno Rice 	vm_offset_t	pa, va, off;
6175244eac9SBenno Rice 	u_int		batl, batu;
618f9bac91bSBenno Rice 
619f9bac91bSBenno Rice         /*
62032bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6210d290675SBenno Rice          */
6220d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6230d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6240d290675SBenno Rice 
6250d290675SBenno Rice         /*
6260d290675SBenno Rice          * Map PCI memory space.
6270d290675SBenno Rice          */
6280d290675SBenno Rice         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6290d290675SBenno Rice         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6300d290675SBenno Rice 
6310d290675SBenno Rice         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6320d290675SBenno Rice         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6330d290675SBenno Rice 
6340d290675SBenno Rice         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6350d290675SBenno Rice         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6360d290675SBenno Rice 
6370d290675SBenno Rice         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6380d290675SBenno Rice         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6390d290675SBenno Rice 
6400d290675SBenno Rice         /*
6410d290675SBenno Rice          * Map obio devices.
6420d290675SBenno Rice          */
6430d290675SBenno Rice         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
6440d290675SBenno Rice         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
6450d290675SBenno Rice 
6460d290675SBenno Rice 	/*
6475244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
6485244eac9SBenno Rice 	 * where we are.
649f9bac91bSBenno Rice 	 */
6505244eac9SBenno Rice 	batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6515244eac9SBenno Rice 	batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
65259276937SPeter Grehan 	__asm (".balign 32; \n"
65372ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
6545d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
6555244eac9SBenno Rice 	    :: "r"(batu), "r"(batl));
6560d290675SBenno Rice 
6570d290675SBenno Rice 	/* map pci space */
6585244eac9SBenno Rice 	batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6590d290675SBenno Rice 	batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6605d64cf91SPeter Grehan 	__asm ("mtdbatu 1,%0; mtdbatl 1,%1; isync"
6615244eac9SBenno Rice 	    :: "r"(batu), "r"(batl));
662f9bac91bSBenno Rice 
66331c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
66459276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
66531c82d03SBenno Rice 
66631c82d03SBenno Rice 	qsort(pregions, pregions_sz, sizeof(*pregions), mr_cmp);
66731c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
66832bc7846SPeter Grehan 		vm_offset_t pa;
66932bc7846SPeter Grehan 		vm_offset_t end;
67032bc7846SPeter Grehan 
67131c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
67231c82d03SBenno Rice 			pregions[i].mr_start,
67331c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
67431c82d03SBenno Rice 			pregions[i].mr_size);
67532bc7846SPeter Grehan 		/*
67632bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
67732bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
67832bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
67932bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
68032bc7846SPeter Grehan 		 * a while yet.
68132bc7846SPeter Grehan 		 */
68232bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
68332bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
68432bc7846SPeter Grehan 		do {
68532bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
68632bc7846SPeter Grehan 
68732bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
68832bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
68932bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
69032bc7846SPeter Grehan 		} while (pa < end);
69131c82d03SBenno Rice 	}
69231c82d03SBenno Rice 
69331c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
69459276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
69531c82d03SBenno Rice 	qsort(regions, regions_sz, sizeof(*regions), mr_cmp);
6965244eac9SBenno Rice 	phys_avail_count = 0;
697d2c1f576SBenno Rice 	physsz = 0;
698b0c21309SPeter Grehan 	hwphyssz = 0;
699b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
70031c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7015244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7025244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7035244eac9SBenno Rice 		    regions[i].mr_size);
704e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
705e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
706e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
707e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
708e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
709e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
710e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
711e2f6d6e2SPeter Grehan 				phys_avail_count++;
712e2f6d6e2SPeter Grehan 			}
713e2f6d6e2SPeter Grehan 			break;
714e2f6d6e2SPeter Grehan 		}
7155244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7165244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7175244eac9SBenno Rice 		phys_avail_count++;
718d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
719f9bac91bSBenno Rice 	}
720d2c1f576SBenno Rice 	physmem = btoc(physsz);
721f9bac91bSBenno Rice 
722f9bac91bSBenno Rice 	/*
7235244eac9SBenno Rice 	 * Allocate PTEG table.
724f9bac91bSBenno Rice 	 */
7255244eac9SBenno Rice #ifdef PTEGCOUNT
72659276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
7275244eac9SBenno Rice #else
72859276937SPeter Grehan 	moea_pteg_count = 0x1000;
729f9bac91bSBenno Rice 
73059276937SPeter Grehan 	while (moea_pteg_count < physmem)
73159276937SPeter Grehan 		moea_pteg_count <<= 1;
732f9bac91bSBenno Rice 
73359276937SPeter Grehan 	moea_pteg_count >>= 1;
7345244eac9SBenno Rice #endif /* PTEGCOUNT */
735f9bac91bSBenno Rice 
73659276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
73759276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
7385244eac9SBenno Rice 	    size);
73959276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
74059276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
74159276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
74259276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
743f9bac91bSBenno Rice 
7445244eac9SBenno Rice 	/*
745864bc520SBenno Rice 	 * Allocate pv/overflow lists.
7465244eac9SBenno Rice 	 */
74759276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
74859276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
7495244eac9SBenno Rice 	    PAGE_SIZE);
75059276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
75159276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
75259276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
7535244eac9SBenno Rice 
7545244eac9SBenno Rice 	/*
755f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
756f489bf21SAlan Cox 	 * tables.
757f489bf21SAlan Cox 	 */
758d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
759d644a0b7SAlan Cox 	    MTX_RECURSE);
760f489bf21SAlan Cox 
761f489bf21SAlan Cox 	/*
7625244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
7635244eac9SBenno Rice 	 */
76459276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
7650d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
76659276937SPeter Grehan 	moea_bpvo_pool_index = 0;
7675244eac9SBenno Rice 
7685244eac9SBenno Rice 	/*
7695244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
7705244eac9SBenno Rice 	 */
77159276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
7725244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
77359276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
7745244eac9SBenno Rice 
7755244eac9SBenno Rice 	/*
7765244eac9SBenno Rice 	 * Set up the Open Firmware pmap and add it's mappings.
7775244eac9SBenno Rice 	 */
77859276937SPeter Grehan 	moea_pinit(mmup, &ofw_pmap);
7795244eac9SBenno Rice 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
7804daf20b2SPeter Grehan 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
7815244eac9SBenno Rice 	if ((chosen = OF_finddevice("/chosen")) == -1)
78259276937SPeter Grehan 		panic("moea_bootstrap: can't find /chosen");
7835244eac9SBenno Rice 	OF_getprop(chosen, "mmu", &mmui, 4);
7845244eac9SBenno Rice 	if ((mmu = OF_instance_to_package(mmui)) == -1)
78559276937SPeter Grehan 		panic("moea_bootstrap: can't get mmu package");
7865244eac9SBenno Rice 	if ((sz = OF_getproplen(mmu, "translations")) == -1)
78759276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translation count");
788aa39961eSBenno Rice 	translations = NULL;
7896cc1cdf4SPeter Grehan 	for (i = 0; phys_avail[i] != 0; i += 2) {
7906cc1cdf4SPeter Grehan 		if (phys_avail[i + 1] >= sz) {
791aa39961eSBenno Rice 			translations = (struct ofw_map *)phys_avail[i];
7926cc1cdf4SPeter Grehan 			break;
7936cc1cdf4SPeter Grehan 		}
794aa39961eSBenno Rice 	}
795aa39961eSBenno Rice 	if (translations == NULL)
79659276937SPeter Grehan 		panic("moea_bootstrap: no space to copy translations");
7975244eac9SBenno Rice 	bzero(translations, sz);
7985244eac9SBenno Rice 	if (OF_getprop(mmu, "translations", translations, sz) == -1)
79959276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translations");
80059276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: translations");
80131c82d03SBenno Rice 	sz /= sizeof(*translations);
8025244eac9SBenno Rice 	qsort(translations, sz, sizeof (*translations), om_cmp);
80332bc7846SPeter Grehan 	for (i = 0, ofw_mappings = 0; i < sz; i++) {
8045244eac9SBenno Rice 		CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
8055244eac9SBenno Rice 		    translations[i].om_pa, translations[i].om_va,
8065244eac9SBenno Rice 		    translations[i].om_len);
8075244eac9SBenno Rice 
80832bc7846SPeter Grehan 		/*
80932bc7846SPeter Grehan 		 * If the mapping is 1:1, let the RAM and device on-demand
81032bc7846SPeter Grehan 		 * BAT tables take care of the translation.
81132bc7846SPeter Grehan 		 */
81232bc7846SPeter Grehan 		if (translations[i].om_va == translations[i].om_pa)
81332bc7846SPeter Grehan 			continue;
8145244eac9SBenno Rice 
81532bc7846SPeter Grehan 		/* Enter the pages */
8165244eac9SBenno Rice 		for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) {
8175244eac9SBenno Rice 			struct	vm_page m;
8185244eac9SBenno Rice 
8195244eac9SBenno Rice 			m.phys_addr = translations[i].om_pa + off;
8205ce609a3SRink Springer 			PMAP_LOCK(&ofw_pmap);
821ce142d9eSAlan Cox 			moea_enter_locked(&ofw_pmap,
82259276937SPeter Grehan 				   translations[i].om_va + off, &m,
8235244eac9SBenno Rice 				   VM_PROT_ALL, 1);
8245ce609a3SRink Springer 			PMAP_UNLOCK(&ofw_pmap);
82532bc7846SPeter Grehan 			ofw_mappings++;
826f9bac91bSBenno Rice 		}
827f9bac91bSBenno Rice 	}
828014ffa99SMarcel Moolenaar 
829014ffa99SMarcel Moolenaar 	/*
830014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
831014ffa99SMarcel Moolenaar 	 */
832014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
833014ffa99SMarcel Moolenaar 		;
834014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
8355244eac9SBenno Rice 
8365244eac9SBenno Rice 	/*
8375244eac9SBenno Rice 	 * Initialize the kernel pmap (which is statically allocated).
8385244eac9SBenno Rice 	 */
83948d0b1a0SAlan Cox 	PMAP_LOCK_INIT(kernel_pmap);
8405244eac9SBenno Rice 	for (i = 0; i < 16; i++) {
8415244eac9SBenno Rice 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT;
842f9bac91bSBenno Rice 	}
8435244eac9SBenno Rice 	kernel_pmap->pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
84422f2fe59SPeter Grehan 	kernel_pmap->pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
8455244eac9SBenno Rice 	kernel_pmap->pm_active = ~0;
8465244eac9SBenno Rice 
8475244eac9SBenno Rice 	/*
8485244eac9SBenno Rice 	 * Initialize hardware.
8495244eac9SBenno Rice 	 */
8505244eac9SBenno Rice 	for (i = 0; i < 16; i++) {
851d080d5fdSBenno Rice 		mtsrin(i << ADDR_SR_SHFT, EMPTY_SEGMENT);
8525244eac9SBenno Rice 	}
8535244eac9SBenno Rice 	__asm __volatile ("mtsr %0,%1"
8545244eac9SBenno Rice 	    :: "n"(KERNEL_SR), "r"(KERNEL_SEGMENT));
85522f2fe59SPeter Grehan 	__asm __volatile ("mtsr %0,%1"
85622f2fe59SPeter Grehan 	    :: "n"(KERNEL2_SR), "r"(KERNEL2_SEGMENT));
8575244eac9SBenno Rice 	__asm __volatile ("sync; mtsdr1 %0; isync"
85859276937SPeter Grehan 	    :: "r"((u_int)moea_pteg_table | (moea_pteg_mask >> 10)));
8595244eac9SBenno Rice 	tlbia();
8605244eac9SBenno Rice 
8615244eac9SBenno Rice 	pmap_bootstrapped++;
862014ffa99SMarcel Moolenaar 
863014ffa99SMarcel Moolenaar 	/*
864014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
865014ffa99SMarcel Moolenaar 	 */
866014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
867014ffa99SMarcel Moolenaar 	virtual_end = VM_MAX_KERNEL_ADDRESS;
868014ffa99SMarcel Moolenaar 
869014ffa99SMarcel Moolenaar 	/*
870014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
871014ffa99SMarcel Moolenaar 	 * into the kernel page map.
872014ffa99SMarcel Moolenaar 	 */
873014ffa99SMarcel Moolenaar 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE);
874014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
875014ffa99SMarcel Moolenaar 	virtual_avail = va + KSTACK_PAGES * PAGE_SIZE;
876014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
877014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
878014ffa99SMarcel Moolenaar 	thread0.td_kstack_pages = KSTACK_PAGES;
879014ffa99SMarcel Moolenaar 	for (i = 0; i < KSTACK_PAGES; i++) {
880014ffa99SMarcel Moolenaar 		moea_kenter(mmup, va, pa);;
881014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
882014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
883014ffa99SMarcel Moolenaar 	}
884014ffa99SMarcel Moolenaar 
885014ffa99SMarcel Moolenaar 	/*
886014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
887014ffa99SMarcel Moolenaar 	 */
888014ffa99SMarcel Moolenaar 	pa = msgbuf_phys = moea_bootstrap_alloc(MSGBUF_SIZE, PAGE_SIZE);
889014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
890014ffa99SMarcel Moolenaar 	va = virtual_avail;
891014ffa99SMarcel Moolenaar 	virtual_avail += round_page(MSGBUF_SIZE);
892014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
893014ffa99SMarcel Moolenaar 		moea_kenter(mmup, va, pa);;
894014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
895014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
896014ffa99SMarcel Moolenaar 	}
8975244eac9SBenno Rice }
8985244eac9SBenno Rice 
8995244eac9SBenno Rice /*
9005244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9015244eac9SBenno Rice  * space can be accessed in any way.
902f9bac91bSBenno Rice  */
903f9bac91bSBenno Rice void
90459276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
905f9bac91bSBenno Rice {
9068207b362SBenno Rice 	pmap_t	pm, pmr;
907f9bac91bSBenno Rice 
908f9bac91bSBenno Rice 	/*
90932bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9105244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
911f9bac91bSBenno Rice 	 */
9125244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
913f9bac91bSBenno Rice 
91459276937SPeter Grehan 	if ((pmr = (pmap_t)moea_kextract(mmu, (vm_offset_t)pm)) == NULL)
9158207b362SBenno Rice 		pmr = pm;
9168207b362SBenno Rice 
9175244eac9SBenno Rice 	pm->pm_active |= PCPU_GET(cpumask);
9188207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
919ac6ba8bdSBenno Rice }
920ac6ba8bdSBenno Rice 
921ac6ba8bdSBenno Rice void
92259276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
923ac6ba8bdSBenno Rice {
924ac6ba8bdSBenno Rice 	pmap_t	pm;
925ac6ba8bdSBenno Rice 
926ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
927ac6ba8bdSBenno Rice 	pm->pm_active &= ~(PCPU_GET(cpumask));
9288207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
929f9bac91bSBenno Rice }
930f9bac91bSBenno Rice 
931f9bac91bSBenno Rice void
93259276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
933f9bac91bSBenno Rice {
9340f92104cSBenno Rice 	struct	pvo_entry *pvo;
9350f92104cSBenno Rice 
93648d0b1a0SAlan Cox 	PMAP_LOCK(pm);
93759276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
9380f92104cSBenno Rice 
9390f92104cSBenno Rice 	if (pvo != NULL) {
9400f92104cSBenno Rice 		if (wired) {
9410f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
9420f92104cSBenno Rice 				pm->pm_stats.wired_count++;
9430f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
9440f92104cSBenno Rice 		} else {
9450f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
9460f92104cSBenno Rice 				pm->pm_stats.wired_count--;
9470f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
9480f92104cSBenno Rice 		}
9490f92104cSBenno Rice 	}
95048d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
951f9bac91bSBenno Rice }
952f9bac91bSBenno Rice 
953f9bac91bSBenno Rice void
95459276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
955f9bac91bSBenno Rice {
95625e2288dSBenno Rice 	vm_offset_t	dst;
95725e2288dSBenno Rice 	vm_offset_t	src;
95825e2288dSBenno Rice 
95925e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
96025e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
96125e2288dSBenno Rice 
96225e2288dSBenno Rice 	kcopy((void *)src, (void *)dst, PAGE_SIZE);
963f9bac91bSBenno Rice }
964111c77dcSBenno Rice 
965111c77dcSBenno Rice /*
9665244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
9675244eac9SBenno Rice  */
9685244eac9SBenno Rice void
96959276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
9705244eac9SBenno Rice {
9711a87a0daSPeter Wemm 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
9725b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
9735244eac9SBenno Rice 
9745244eac9SBenno Rice 	bzero(va, PAGE_SIZE);
9755244eac9SBenno Rice }
9765244eac9SBenno Rice 
9775244eac9SBenno Rice void
97859276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
9795244eac9SBenno Rice {
9803495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
9815b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
9823495845eSBenno Rice 
9835b43c63dSMarcel Moolenaar 	bzero(va, size);
9845244eac9SBenno Rice }
9855244eac9SBenno Rice 
986a58b3a68SPeter Wemm void
98759276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
988a58b3a68SPeter Wemm {
9895b43c63dSMarcel Moolenaar 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
9905b43c63dSMarcel Moolenaar 	void *va = (void *)pa;
991a58b3a68SPeter Wemm 
9925b43c63dSMarcel Moolenaar 	bzero(va, PAGE_SIZE);
993a58b3a68SPeter Wemm }
994a58b3a68SPeter Wemm 
9955244eac9SBenno Rice /*
9965244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
9975244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
9985244eac9SBenno Rice  * will be wired down.
9995244eac9SBenno Rice  */
10005244eac9SBenno Rice void
100159276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
10025244eac9SBenno Rice 	   boolean_t wired)
10035244eac9SBenno Rice {
1004ce142d9eSAlan Cox 
1005ce142d9eSAlan Cox 	vm_page_lock_queues();
1006ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
100767c867eeSAlan Cox 	moea_enter_locked(pmap, va, m, prot, wired);
1008ce142d9eSAlan Cox 	vm_page_unlock_queues();
1009ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1010ce142d9eSAlan Cox }
1011ce142d9eSAlan Cox 
1012ce142d9eSAlan Cox /*
1013ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1014ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1015ce142d9eSAlan Cox  * will be wired down.
1016ce142d9eSAlan Cox  *
1017ce142d9eSAlan Cox  * The page queues and pmap must be locked.
1018ce142d9eSAlan Cox  */
1019ce142d9eSAlan Cox static void
1020ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1021ce142d9eSAlan Cox     boolean_t wired)
1022ce142d9eSAlan Cox {
10235244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1024378862a7SJeff Roberson 	uma_zone_t	zone;
10258207b362SBenno Rice 	vm_page_t	pg;
10268207b362SBenno Rice 	u_int		pte_lo, pvo_flags, was_exec, i;
10275244eac9SBenno Rice 	int		error;
10285244eac9SBenno Rice 
102959276937SPeter Grehan 	if (!moea_initialized) {
103059276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
103159276937SPeter Grehan 		zone = moea_upvo_zone;
10325244eac9SBenno Rice 		pvo_flags = 0;
10338207b362SBenno Rice 		pg = NULL;
10348207b362SBenno Rice 		was_exec = PTE_EXEC;
10355244eac9SBenno Rice 	} else {
103603b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
103703b6e025SPeter Grehan 		pg = m;
103859276937SPeter Grehan 		zone = moea_mpvo_zone;
10395244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
10408207b362SBenno Rice 		was_exec = 0;
10415244eac9SBenno Rice 	}
1042f489bf21SAlan Cox 	if (pmap_bootstrapped)
1043ce142d9eSAlan Cox 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1044ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
10455244eac9SBenno Rice 
10464dba5df1SPeter Grehan 	/* XXX change the pvo head for fake pages */
10474dba5df1SPeter Grehan 	if ((m->flags & PG_FICTITIOUS) == PG_FICTITIOUS)
104859276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
10494dba5df1SPeter Grehan 
10508207b362SBenno Rice 	/*
10518207b362SBenno Rice 	 * If this is a managed page, and it's the first reference to the page,
10528207b362SBenno Rice 	 * clear the execness of the page.  Otherwise fetch the execness.
10538207b362SBenno Rice 	 */
10544dba5df1SPeter Grehan 	if ((pg != NULL) && ((m->flags & PG_FICTITIOUS) == 0)) {
10558207b362SBenno Rice 		if (LIST_EMPTY(pvo_head)) {
105659276937SPeter Grehan 			moea_attr_clear(pg, PTE_EXEC);
10578207b362SBenno Rice 		} else {
105859276937SPeter Grehan 			was_exec = moea_attr_fetch(pg) & PTE_EXEC;
10598207b362SBenno Rice 		}
10608207b362SBenno Rice 	}
10618207b362SBenno Rice 
10628207b362SBenno Rice 	/*
10638207b362SBenno Rice 	 * Assume the page is cache inhibited and access is guarded unless
10648207b362SBenno Rice 	 * it's in our available memory array.
10658207b362SBenno Rice 	 */
10665244eac9SBenno Rice 	pte_lo = PTE_I | PTE_G;
106731c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
106831c82d03SBenno Rice 		if ((VM_PAGE_TO_PHYS(m) >= pregions[i].mr_start) &&
106931c82d03SBenno Rice 		    (VM_PAGE_TO_PHYS(m) <
107031c82d03SBenno Rice 			(pregions[i].mr_start + pregions[i].mr_size))) {
10718207b362SBenno Rice 			pte_lo &= ~(PTE_I | PTE_G);
10728207b362SBenno Rice 			break;
10738207b362SBenno Rice 		}
10748207b362SBenno Rice 	}
10755244eac9SBenno Rice 
107644b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
10775244eac9SBenno Rice 		pte_lo |= PTE_BW;
10789955cf96SPeter Grehan 		if (pmap_bootstrapped)
107944b8bd66SAlan Cox 			vm_page_flag_set(m, PG_WRITEABLE);
108044b8bd66SAlan Cox 	} else
10815244eac9SBenno Rice 		pte_lo |= PTE_BR;
10825244eac9SBenno Rice 
10834dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
10844dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
10855244eac9SBenno Rice 
10865244eac9SBenno Rice 	if (wired)
10875244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
10885244eac9SBenno Rice 
10894dba5df1SPeter Grehan 	if ((m->flags & PG_FICTITIOUS) != 0)
10904dba5df1SPeter Grehan 		pvo_flags |= PVO_FAKE;
10914dba5df1SPeter Grehan 
109259276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
10938207b362SBenno Rice 	    pte_lo, pvo_flags);
10945244eac9SBenno Rice 
10958207b362SBenno Rice 	/*
10968207b362SBenno Rice 	 * Flush the real page from the instruction cache if this page is
10978207b362SBenno Rice 	 * mapped executable and cacheable and was not previously mapped (or
10988207b362SBenno Rice 	 * was not mapped executable).
10998207b362SBenno Rice 	 */
11008207b362SBenno Rice 	if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
11018207b362SBenno Rice 	    (pte_lo & PTE_I) == 0 && was_exec == 0) {
11025244eac9SBenno Rice 		/*
11035244eac9SBenno Rice 		 * Flush the real memory from the cache.
11045244eac9SBenno Rice 		 */
110559276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
11068207b362SBenno Rice 		if (pg != NULL)
110759276937SPeter Grehan 			moea_attr_save(pg, PTE_EXEC);
11085244eac9SBenno Rice 	}
110932bc7846SPeter Grehan 
111032bc7846SPeter Grehan 	/* XXX syncicache always until problems are sorted */
111159276937SPeter Grehan 	moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1112ce142d9eSAlan Cox }
1113ce142d9eSAlan Cox 
1114ce142d9eSAlan Cox /*
1115ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1116ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1117ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1118ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1119ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1120ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1121ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1122ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1123ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1124ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1125ce142d9eSAlan Cox  */
1126ce142d9eSAlan Cox void
1127ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1128ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1129ce142d9eSAlan Cox {
1130ce142d9eSAlan Cox 	vm_page_t m;
1131ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1132ce142d9eSAlan Cox 
1133ce142d9eSAlan Cox 	psize = atop(end - start);
1134ce142d9eSAlan Cox 	m = m_start;
1135ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1136ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1137ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1138ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1139ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1140ce142d9eSAlan Cox 	}
1141ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
11425244eac9SBenno Rice }
11435244eac9SBenno Rice 
11442053c127SStephan Uphoff void
114559276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
11462053c127SStephan Uphoff     vm_prot_t prot)
1147dca96f1aSAlan Cox {
1148dca96f1aSAlan Cox 
1149ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1150ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
115159276937SPeter Grehan 	    FALSE);
1152ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
11532053c127SStephan Uphoff 
1154dca96f1aSAlan Cox }
1155dca96f1aSAlan Cox 
115656b09388SAlan Cox vm_paddr_t
115759276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
11585244eac9SBenno Rice {
11590f92104cSBenno Rice 	struct	pvo_entry *pvo;
116048d0b1a0SAlan Cox 	vm_paddr_t pa;
11610f92104cSBenno Rice 
116248d0b1a0SAlan Cox 	PMAP_LOCK(pm);
116359276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
116448d0b1a0SAlan Cox 	if (pvo == NULL)
116548d0b1a0SAlan Cox 		pa = 0;
116648d0b1a0SAlan Cox 	else
116748d0b1a0SAlan Cox 		pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
116848d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
116948d0b1a0SAlan Cox 	return (pa);
11705244eac9SBenno Rice }
11715244eac9SBenno Rice 
11725244eac9SBenno Rice /*
117384792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
117484792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
117584792e72SPeter Grehan  * protection.
117684792e72SPeter Grehan  */
117784792e72SPeter Grehan vm_page_t
117859276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
117984792e72SPeter Grehan {
1180ab50a262SAlan Cox 	struct	pvo_entry *pvo;
118184792e72SPeter Grehan 	vm_page_t m;
118284792e72SPeter Grehan 
118384792e72SPeter Grehan 	m = NULL;
118448d0b1a0SAlan Cox 	vm_page_lock_queues();
118548d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
118659276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1187ab50a262SAlan Cox 	if (pvo != NULL && (pvo->pvo_pte.pte_hi & PTE_VALID) &&
1188ab50a262SAlan Cox 	    ((pvo->pvo_pte.pte_lo & PTE_PP) == PTE_RW ||
1189ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
1190ab50a262SAlan Cox 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
119184792e72SPeter Grehan 		vm_page_hold(m);
119284792e72SPeter Grehan 	}
119348d0b1a0SAlan Cox 	vm_page_unlock_queues();
119448d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
119584792e72SPeter Grehan 	return (m);
119684792e72SPeter Grehan }
119784792e72SPeter Grehan 
11985244eac9SBenno Rice void
119959276937SPeter Grehan moea_init(mmu_t mmu)
12005244eac9SBenno Rice {
12015244eac9SBenno Rice 
120259276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_init");
12030d290675SBenno Rice 
120459276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12050ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12060ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
120759276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12080ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12090ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
121059276937SPeter Grehan 	moea_initialized = TRUE;
12115244eac9SBenno Rice }
12125244eac9SBenno Rice 
12135244eac9SBenno Rice boolean_t
121459276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
12155244eac9SBenno Rice {
12160f92104cSBenno Rice 
121703b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS |PG_UNMANAGED)) != 0)
12180f92104cSBenno Rice 		return (FALSE);
12190f92104cSBenno Rice 
122059276937SPeter Grehan 	return (moea_query_bit(m, PTE_CHG));
1221566526a9SAlan Cox }
1222566526a9SAlan Cox 
12235244eac9SBenno Rice void
122459276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m)
12255244eac9SBenno Rice {
122603b6e025SPeter Grehan 
122703b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
122803b6e025SPeter Grehan 		return;
122959276937SPeter Grehan 	moea_clear_bit(m, PTE_REF, NULL);
123003b6e025SPeter Grehan }
123103b6e025SPeter Grehan 
123203b6e025SPeter Grehan void
123359276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
123403b6e025SPeter Grehan {
123503b6e025SPeter Grehan 
123603b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
123703b6e025SPeter Grehan 		return;
123859276937SPeter Grehan 	moea_clear_bit(m, PTE_CHG, NULL);
12395244eac9SBenno Rice }
12405244eac9SBenno Rice 
12417f3a4093SMike Silbersack /*
124278985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
124378985e42SAlan Cox  */
124478985e42SAlan Cox void
124578985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
124678985e42SAlan Cox {
124778985e42SAlan Cox 	struct	pvo_entry *pvo;
124878985e42SAlan Cox 	struct	pte *pt;
124978985e42SAlan Cox 	pmap_t	pmap;
125078985e42SAlan Cox 	u_int	lo;
125178985e42SAlan Cox 
125278985e42SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
125378985e42SAlan Cox 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
125478985e42SAlan Cox 	    (m->flags & PG_WRITEABLE) == 0)
125578985e42SAlan Cox 		return;
125678985e42SAlan Cox 	lo = moea_attr_fetch(m);
125778985e42SAlan Cox 	SYNC();
125878985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
125978985e42SAlan Cox 		pmap = pvo->pvo_pmap;
126078985e42SAlan Cox 		PMAP_LOCK(pmap);
126178985e42SAlan Cox 		if ((pvo->pvo_pte.pte_lo & PTE_PP) != PTE_BR) {
126278985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
126378985e42SAlan Cox 			pvo->pvo_pte.pte_lo &= ~PTE_PP;
126478985e42SAlan Cox 			pvo->pvo_pte.pte_lo |= PTE_BR;
126578985e42SAlan Cox 			if (pt != NULL) {
126678985e42SAlan Cox 				moea_pte_synch(pt, &pvo->pvo_pte);
126778985e42SAlan Cox 				lo |= pvo->pvo_pte.pte_lo;
126878985e42SAlan Cox 				pvo->pvo_pte.pte_lo &= ~PTE_CHG;
126978985e42SAlan Cox 				moea_pte_change(pt, &pvo->pvo_pte,
127078985e42SAlan Cox 				    pvo->pvo_vaddr);
127178985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
127278985e42SAlan Cox 			}
127378985e42SAlan Cox 		}
127478985e42SAlan Cox 		PMAP_UNLOCK(pmap);
127578985e42SAlan Cox 	}
127678985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
127778985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
127878985e42SAlan Cox 		vm_page_dirty(m);
127978985e42SAlan Cox 	}
128078985e42SAlan Cox 	vm_page_flag_clear(m, PG_WRITEABLE);
128178985e42SAlan Cox }
128278985e42SAlan Cox 
128378985e42SAlan Cox /*
128459276937SPeter Grehan  *	moea_ts_referenced:
12857f3a4093SMike Silbersack  *
12867f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
12877f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
12887f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
12897f3a4093SMike Silbersack  *	reference bits set.
12907f3a4093SMike Silbersack  *
12917f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
12927f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
12937f3a4093SMike Silbersack  *	optimal aging of shared pages.
12947f3a4093SMike Silbersack  */
129559276937SPeter Grehan boolean_t
129659276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
12975244eac9SBenno Rice {
129803b6e025SPeter Grehan 	int count;
129903b6e025SPeter Grehan 
130003b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
13015244eac9SBenno Rice 		return (0);
130203b6e025SPeter Grehan 
130359276937SPeter Grehan 	count = moea_clear_bit(m, PTE_REF, NULL);
130403b6e025SPeter Grehan 
130503b6e025SPeter Grehan 	return (count);
13065244eac9SBenno Rice }
13075244eac9SBenno Rice 
13085244eac9SBenno Rice /*
13095244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
13105244eac9SBenno Rice  */
13115244eac9SBenno Rice void
131259276937SPeter Grehan moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
13135244eac9SBenno Rice {
13145244eac9SBenno Rice 	u_int		pte_lo;
13155244eac9SBenno Rice 	int		error;
13165244eac9SBenno Rice 	int		i;
13175244eac9SBenno Rice 
13185244eac9SBenno Rice #if 0
13195244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
132059276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
13215244eac9SBenno Rice 		    va);
13225244eac9SBenno Rice #endif
13235244eac9SBenno Rice 
132432bc7846SPeter Grehan 	pte_lo = PTE_I | PTE_G;
132532bc7846SPeter Grehan 	for (i = 0; i < pregions_sz; i++) {
132632bc7846SPeter Grehan 		if ((pa >= pregions[i].mr_start) &&
132732bc7846SPeter Grehan 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
13285244eac9SBenno Rice 			pte_lo &= ~(PTE_I | PTE_G);
13295244eac9SBenno Rice 			break;
13305244eac9SBenno Rice 		}
13315244eac9SBenno Rice 	}
13325244eac9SBenno Rice 
13334711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
133459276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
133559276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
13365244eac9SBenno Rice 
13375244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
133859276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
13395244eac9SBenno Rice 		    pa, error);
13405244eac9SBenno Rice 
13415244eac9SBenno Rice 	/*
13425244eac9SBenno Rice 	 * Flush the real memory from the instruction cache.
13435244eac9SBenno Rice 	 */
13445244eac9SBenno Rice 	if ((pte_lo & (PTE_I | PTE_G)) == 0) {
134559276937SPeter Grehan 		moea_syncicache(pa, PAGE_SIZE);
13465244eac9SBenno Rice 	}
13474711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
13485244eac9SBenno Rice }
13495244eac9SBenno Rice 
1350e79f59e8SBenno Rice /*
1351e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1352e79f59e8SBenno Rice  * address.
1353e79f59e8SBenno Rice  */
13545244eac9SBenno Rice vm_offset_t
135559276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
13565244eac9SBenno Rice {
1357e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
135848d0b1a0SAlan Cox 	vm_paddr_t pa;
1359e79f59e8SBenno Rice 
13600efd0097SPeter Grehan #ifdef UMA_MD_SMALL_ALLOC
13610efd0097SPeter Grehan 	/*
13620efd0097SPeter Grehan 	 * Allow direct mappings
13630efd0097SPeter Grehan 	 */
13640efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
13650efd0097SPeter Grehan 		return (va);
13660efd0097SPeter Grehan 	}
13670efd0097SPeter Grehan #endif
13680efd0097SPeter Grehan 
136948d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
137059276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
137159276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
137248d0b1a0SAlan Cox 	pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
137348d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
137448d0b1a0SAlan Cox 	return (pa);
1375e79f59e8SBenno Rice }
1376e79f59e8SBenno Rice 
137788afb2a3SBenno Rice /*
137888afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
137988afb2a3SBenno Rice  */
13805244eac9SBenno Rice void
138159276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
13825244eac9SBenno Rice {
138388afb2a3SBenno Rice 
138459276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
13855244eac9SBenno Rice }
13865244eac9SBenno Rice 
13875244eac9SBenno Rice /*
13885244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
13895244eac9SBenno Rice  *
13905244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
13915244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
13925244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
13935244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
13945244eac9SBenno Rice  * first usable address after the mapped region.
13955244eac9SBenno Rice  */
13965244eac9SBenno Rice vm_offset_t
139759276937SPeter Grehan moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
139859276937SPeter Grehan     vm_offset_t pa_end, int prot)
13995244eac9SBenno Rice {
14005244eac9SBenno Rice 	vm_offset_t	sva, va;
14015244eac9SBenno Rice 
14025244eac9SBenno Rice 	sva = *virt;
14035244eac9SBenno Rice 	va = sva;
14045244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
140559276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
14065244eac9SBenno Rice 	*virt = va;
14075244eac9SBenno Rice 	return (sva);
14085244eac9SBenno Rice }
14095244eac9SBenno Rice 
14105244eac9SBenno Rice /*
14117f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
14127f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
14137f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
14147f3a4093SMike Silbersack  * is only necessary that true be returned for a small
14157f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
14167f3a4093SMike Silbersack  */
14175244eac9SBenno Rice boolean_t
141859276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
14195244eac9SBenno Rice {
142003b6e025SPeter Grehan         int loops;
142103b6e025SPeter Grehan 	struct pvo_entry *pvo;
142203b6e025SPeter Grehan 
142359276937SPeter Grehan         if (!moea_initialized || (m->flags & PG_FICTITIOUS))
142403b6e025SPeter Grehan                 return FALSE;
142503b6e025SPeter Grehan 
142603b6e025SPeter Grehan 	loops = 0;
142703b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
142803b6e025SPeter Grehan 		if (pvo->pvo_pmap == pmap)
142903b6e025SPeter Grehan 			return (TRUE);
143003b6e025SPeter Grehan 		if (++loops >= 16)
143103b6e025SPeter Grehan 			break;
143203b6e025SPeter Grehan 	}
143303b6e025SPeter Grehan 
143403b6e025SPeter Grehan 	return (FALSE);
14355244eac9SBenno Rice }
14365244eac9SBenno Rice 
143759677d3cSAlan Cox /*
143859677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
143959677d3cSAlan Cox  * that are wired.
144059677d3cSAlan Cox  */
144159677d3cSAlan Cox int
144259677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
144359677d3cSAlan Cox {
144459677d3cSAlan Cox 	struct pvo_entry *pvo;
144559677d3cSAlan Cox 	int count;
144659677d3cSAlan Cox 
144759677d3cSAlan Cox 	count = 0;
144859677d3cSAlan Cox 	if (!moea_initialized || (m->flags & PG_FICTITIOUS) != 0)
144959677d3cSAlan Cox 		return (count);
145059677d3cSAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
145159677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
145259677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
145359677d3cSAlan Cox 			count++;
145459677d3cSAlan Cox 	return (count);
145559677d3cSAlan Cox }
145659677d3cSAlan Cox 
145759276937SPeter Grehan static u_int	moea_vsidcontext;
14585244eac9SBenno Rice 
14595244eac9SBenno Rice void
146059276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
14615244eac9SBenno Rice {
14625244eac9SBenno Rice 	int	i, mask;
14635244eac9SBenno Rice 	u_int	entropy;
14645244eac9SBenno Rice 
146559276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
146648d0b1a0SAlan Cox 	PMAP_LOCK_INIT(pmap);
14674daf20b2SPeter Grehan 
14685244eac9SBenno Rice 	entropy = 0;
14695244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
14705244eac9SBenno Rice 
14715244eac9SBenno Rice 	/*
14725244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
14735244eac9SBenno Rice 	 */
14745244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
14755244eac9SBenno Rice 		u_int	hash, n;
14765244eac9SBenno Rice 
14775244eac9SBenno Rice 		/*
14785244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
14795244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
14805244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
14815244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
14825244eac9SBenno Rice 		 * instead of a multiply.)
14835244eac9SBenno Rice 		 */
148459276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
148559276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
14865244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
14875244eac9SBenno Rice 			continue;
14885244eac9SBenno Rice 		n = hash >> 5;
14895244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
149059276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
149159276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
14925244eac9SBenno Rice 			/* anything free in this bucket? */
149359276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
149459276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
14955244eac9SBenno Rice 				continue;
14965244eac9SBenno Rice 			}
149759276937SPeter Grehan 			i = ffs(~moea_vsid_bitmap[i]) - 1;
14985244eac9SBenno Rice 			mask = 1 << i;
14995244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
15005244eac9SBenno Rice 			hash |= i;
15015244eac9SBenno Rice 		}
150259276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
15035244eac9SBenno Rice 		for (i = 0; i < 16; i++)
15045244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
15055244eac9SBenno Rice 		return;
15065244eac9SBenno Rice 	}
15075244eac9SBenno Rice 
150859276937SPeter Grehan 	panic("moea_pinit: out of segments");
15095244eac9SBenno Rice }
15105244eac9SBenno Rice 
15115244eac9SBenno Rice /*
15125244eac9SBenno Rice  * Initialize the pmap associated with process 0.
15135244eac9SBenno Rice  */
15145244eac9SBenno Rice void
151559276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
15165244eac9SBenno Rice {
15175244eac9SBenno Rice 
151859276937SPeter Grehan 	moea_pinit(mmu, pm);
15195244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
15205244eac9SBenno Rice }
15215244eac9SBenno Rice 
1522e79f59e8SBenno Rice /*
1523e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1524e79f59e8SBenno Rice  */
15255244eac9SBenno Rice void
152659276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
152759276937SPeter Grehan     vm_prot_t prot)
15285244eac9SBenno Rice {
1529e79f59e8SBenno Rice 	struct	pvo_entry *pvo;
1530e79f59e8SBenno Rice 	struct	pte *pt;
1531e79f59e8SBenno Rice 	int	pteidx;
1532e79f59e8SBenno Rice 
153359276937SPeter Grehan 	CTR4(KTR_PMAP, "moea_protect: pm=%p sva=%#x eva=%#x prot=%#x", pm, sva,
1534e79f59e8SBenno Rice 	    eva, prot);
1535e79f59e8SBenno Rice 
1536e79f59e8SBenno Rice 
1537e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
153859276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1539e79f59e8SBenno Rice 
1540e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
154159276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1542e79f59e8SBenno Rice 		return;
1543e79f59e8SBenno Rice 	}
1544e79f59e8SBenno Rice 
15453d2e54c3SAlan Cox 	vm_page_lock_queues();
154648d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1547e79f59e8SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
154859276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
1549e79f59e8SBenno Rice 		if (pvo == NULL)
1550e79f59e8SBenno Rice 			continue;
1551e79f59e8SBenno Rice 
1552e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1553e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1554e79f59e8SBenno Rice 
1555e79f59e8SBenno Rice 		/*
1556e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1557e79f59e8SBenno Rice 		 * copy.
1558e79f59e8SBenno Rice 		 */
155959276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, pteidx);
1560e79f59e8SBenno Rice 		/*
1561e79f59e8SBenno Rice 		 * Change the protection of the page.
1562e79f59e8SBenno Rice 		 */
1563e79f59e8SBenno Rice 		pvo->pvo_pte.pte_lo &= ~PTE_PP;
1564e79f59e8SBenno Rice 		pvo->pvo_pte.pte_lo |= PTE_BR;
1565e79f59e8SBenno Rice 
1566e79f59e8SBenno Rice 		/*
1567e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1568e79f59e8SBenno Rice 		 */
1569d644a0b7SAlan Cox 		if (pt != NULL) {
157059276937SPeter Grehan 			moea_pte_change(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1571d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1572d644a0b7SAlan Cox 		}
1573e79f59e8SBenno Rice 	}
15743d2e54c3SAlan Cox 	vm_page_unlock_queues();
157548d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
15765244eac9SBenno Rice }
15775244eac9SBenno Rice 
157888afb2a3SBenno Rice /*
157988afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
158088afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
158188afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
158288afb2a3SBenno Rice  */
15835244eac9SBenno Rice void
158459276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
15855244eac9SBenno Rice {
158603b6e025SPeter Grehan 	vm_offset_t va;
15875244eac9SBenno Rice 
158803b6e025SPeter Grehan 	va = sva;
158903b6e025SPeter Grehan 	while (count-- > 0) {
159059276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
159103b6e025SPeter Grehan 		va += PAGE_SIZE;
159203b6e025SPeter Grehan 		m++;
159303b6e025SPeter Grehan 	}
15945244eac9SBenno Rice }
15955244eac9SBenno Rice 
159688afb2a3SBenno Rice /*
159788afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
159859276937SPeter Grehan  * temporary mappings entered by moea_qenter.
159988afb2a3SBenno Rice  */
16005244eac9SBenno Rice void
160159276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
16025244eac9SBenno Rice {
160303b6e025SPeter Grehan 	vm_offset_t va;
160488afb2a3SBenno Rice 
160503b6e025SPeter Grehan 	va = sva;
160603b6e025SPeter Grehan 	while (count-- > 0) {
160759276937SPeter Grehan 		moea_kremove(mmu, va);
160803b6e025SPeter Grehan 		va += PAGE_SIZE;
160903b6e025SPeter Grehan 	}
16105244eac9SBenno Rice }
16115244eac9SBenno Rice 
16125244eac9SBenno Rice void
161359276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
16145244eac9SBenno Rice {
161532bc7846SPeter Grehan         int idx, mask;
161632bc7846SPeter Grehan 
161732bc7846SPeter Grehan 	/*
161832bc7846SPeter Grehan 	 * Free segment register's VSID
161932bc7846SPeter Grehan 	 */
162032bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
162159276937SPeter Grehan                 panic("moea_release");
162232bc7846SPeter Grehan 
162332bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
162432bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
162532bc7846SPeter Grehan         idx /= VSID_NBPW;
162659276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
162748d0b1a0SAlan Cox 	PMAP_LOCK_DESTROY(pmap);
16285244eac9SBenno Rice }
16295244eac9SBenno Rice 
163088afb2a3SBenno Rice /*
163188afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
163288afb2a3SBenno Rice  */
16335244eac9SBenno Rice void
163459276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
16355244eac9SBenno Rice {
163688afb2a3SBenno Rice 	struct	pvo_entry *pvo;
163788afb2a3SBenno Rice 	int	pteidx;
163888afb2a3SBenno Rice 
16393d2e54c3SAlan Cox 	vm_page_lock_queues();
164048d0b1a0SAlan Cox 	PMAP_LOCK(pm);
164188afb2a3SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
164259276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
164388afb2a3SBenno Rice 		if (pvo != NULL) {
164459276937SPeter Grehan 			moea_pvo_remove(pvo, pteidx);
164588afb2a3SBenno Rice 		}
164688afb2a3SBenno Rice 	}
164748d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
164894aa7aecSPeter Grehan 	vm_page_unlock_queues();
16495244eac9SBenno Rice }
16505244eac9SBenno Rice 
1651e79f59e8SBenno Rice /*
165259276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
165303b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
165403b6e025SPeter Grehan  */
165503b6e025SPeter Grehan void
165659276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
165703b6e025SPeter Grehan {
165803b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
165903b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
166048d0b1a0SAlan Cox 	pmap_t	pmap;
166103b6e025SPeter Grehan 
166284792e72SPeter Grehan 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
166303b6e025SPeter Grehan 
166403b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
166503b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
166603b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
166703b6e025SPeter Grehan 
166859276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
166948d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
167048d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
167159276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
167248d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
167303b6e025SPeter Grehan 	}
167403b6e025SPeter Grehan 	vm_page_flag_clear(m, PG_WRITEABLE);
167503b6e025SPeter Grehan }
167603b6e025SPeter Grehan 
167703b6e025SPeter Grehan /*
16785244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
167959276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
16805244eac9SBenno Rice  * calculated.
16815244eac9SBenno Rice  */
16825244eac9SBenno Rice static vm_offset_t
168359276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
16845244eac9SBenno Rice {
16855244eac9SBenno Rice 	vm_offset_t	s, e;
16865244eac9SBenno Rice 	int		i, j;
16875244eac9SBenno Rice 
16885244eac9SBenno Rice 	size = round_page(size);
16895244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
16905244eac9SBenno Rice 		if (align != 0)
16915244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
16925244eac9SBenno Rice 		else
16935244eac9SBenno Rice 			s = phys_avail[i];
16945244eac9SBenno Rice 		e = s + size;
16955244eac9SBenno Rice 
16965244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
16975244eac9SBenno Rice 			continue;
16985244eac9SBenno Rice 
16995244eac9SBenno Rice 		if (s == phys_avail[i]) {
17005244eac9SBenno Rice 			phys_avail[i] += size;
17015244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
17025244eac9SBenno Rice 			phys_avail[i + 1] -= size;
17035244eac9SBenno Rice 		} else {
17045244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
17055244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
17065244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
17075244eac9SBenno Rice 			}
17085244eac9SBenno Rice 
17095244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
17105244eac9SBenno Rice 			phys_avail[i + 1] = s;
17115244eac9SBenno Rice 			phys_avail[i + 2] = e;
17125244eac9SBenno Rice 			phys_avail_count++;
17135244eac9SBenno Rice 		}
17145244eac9SBenno Rice 
17155244eac9SBenno Rice 		return (s);
17165244eac9SBenno Rice 	}
171759276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
17185244eac9SBenno Rice }
17195244eac9SBenno Rice 
17205244eac9SBenno Rice static void
172159276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
17225244eac9SBenno Rice {
17235244eac9SBenno Rice 	__syncicache((void *)pa, len);
17245244eac9SBenno Rice }
17255244eac9SBenno Rice 
17265244eac9SBenno Rice static void
17275244eac9SBenno Rice tlbia(void)
17285244eac9SBenno Rice {
17295244eac9SBenno Rice 	caddr_t	i;
17305244eac9SBenno Rice 
17315244eac9SBenno Rice 	SYNC();
17325244eac9SBenno Rice 	for (i = 0; i < (caddr_t)0x00040000; i += 0x00001000) {
17335244eac9SBenno Rice 		TLBIE(i);
17345244eac9SBenno Rice 		EIEIO();
17355244eac9SBenno Rice 	}
17365244eac9SBenno Rice 	TLBSYNC();
17375244eac9SBenno Rice 	SYNC();
17385244eac9SBenno Rice }
17395244eac9SBenno Rice 
17405244eac9SBenno Rice static int
174159276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
17425244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
17435244eac9SBenno Rice {
17445244eac9SBenno Rice 	struct	pvo_entry *pvo;
17455244eac9SBenno Rice 	u_int	sr;
17465244eac9SBenno Rice 	int	first;
17475244eac9SBenno Rice 	u_int	ptegidx;
17485244eac9SBenno Rice 	int	i;
174932bc7846SPeter Grehan 	int     bootstrap;
17505244eac9SBenno Rice 
175159276937SPeter Grehan 	moea_pvo_enter_calls++;
17528207b362SBenno Rice 	first = 0;
175332bc7846SPeter Grehan 	bootstrap = 0;
175432bc7846SPeter Grehan 
17555244eac9SBenno Rice 	/*
17565244eac9SBenno Rice 	 * Compute the PTE Group index.
17575244eac9SBenno Rice 	 */
17585244eac9SBenno Rice 	va &= ~ADDR_POFF;
17595244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
17605244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
17615244eac9SBenno Rice 
17625244eac9SBenno Rice 	/*
17635244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
17645244eac9SBenno Rice 	 * there is a mapping.
17655244eac9SBenno Rice 	 */
176659276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
176759276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
17685244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
1769fafc7362SBenno Rice 			if ((pvo->pvo_pte.pte_lo & PTE_RPGN) == pa &&
1770fafc7362SBenno Rice 			    (pvo->pvo_pte.pte_lo & PTE_PP) ==
1771fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
177259276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
177349f8f727SBenno Rice 				return (0);
1774fafc7362SBenno Rice 			}
177559276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
17765244eac9SBenno Rice 			break;
17775244eac9SBenno Rice 		}
17785244eac9SBenno Rice 	}
17795244eac9SBenno Rice 
17805244eac9SBenno Rice 	/*
17815244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
17825244eac9SBenno Rice 	 */
178359276937SPeter Grehan 	if (moea_initialized) {
1784378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
178549f8f727SBenno Rice 	} else {
178659276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
178759276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
178859276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
17890d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
179049f8f727SBenno Rice 		}
179159276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
179259276937SPeter Grehan 		moea_bpvo_pool_index++;
179332bc7846SPeter Grehan 		bootstrap = 1;
179449f8f727SBenno Rice 	}
17955244eac9SBenno Rice 
17965244eac9SBenno Rice 	if (pvo == NULL) {
179759276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
17985244eac9SBenno Rice 		return (ENOMEM);
17995244eac9SBenno Rice 	}
18005244eac9SBenno Rice 
180159276937SPeter Grehan 	moea_pvo_entries++;
18025244eac9SBenno Rice 	pvo->pvo_vaddr = va;
18035244eac9SBenno Rice 	pvo->pvo_pmap = pm;
180459276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
18055244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
18065244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
18075244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
18085244eac9SBenno Rice 	if (flags & PVO_WIRED)
18095244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
181059276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
18115244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
181232bc7846SPeter Grehan 	if (bootstrap)
181332bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
18144dba5df1SPeter Grehan 	if (flags & PVO_FAKE)
18154dba5df1SPeter Grehan 		pvo->pvo_vaddr |= PVO_FAKE;
18164dba5df1SPeter Grehan 
181759276937SPeter Grehan 	moea_pte_create(&pvo->pvo_pte, sr, va, pa | pte_lo);
18185244eac9SBenno Rice 
18195244eac9SBenno Rice 	/*
18205244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
18215244eac9SBenno Rice 	 * item.
18225244eac9SBenno Rice 	 */
18238207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
18248207b362SBenno Rice 		first = 1;
18255244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
18264dba5df1SPeter Grehan 
18275244eac9SBenno Rice 	if (pvo->pvo_pte.pte_lo & PVO_WIRED)
1828c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
1829c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
18305244eac9SBenno Rice 
18315244eac9SBenno Rice 	/*
18325244eac9SBenno Rice 	 * We hope this succeeds but it isn't required.
18335244eac9SBenno Rice 	 */
183459276937SPeter Grehan 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte);
18355244eac9SBenno Rice 	if (i >= 0) {
18365244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
18375244eac9SBenno Rice 	} else {
183859276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
183959276937SPeter Grehan 		moea_pte_overflow++;
18405244eac9SBenno Rice 	}
184159276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
18424dba5df1SPeter Grehan 
18435244eac9SBenno Rice 	return (first ? ENOENT : 0);
18445244eac9SBenno Rice }
18455244eac9SBenno Rice 
18465244eac9SBenno Rice static void
184759276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
18485244eac9SBenno Rice {
18495244eac9SBenno Rice 	struct	pte *pt;
18505244eac9SBenno Rice 
18515244eac9SBenno Rice 	/*
18525244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
18535244eac9SBenno Rice 	 * save the ref & cfg bits).
18545244eac9SBenno Rice 	 */
185559276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
18565244eac9SBenno Rice 	if (pt != NULL) {
185759276937SPeter Grehan 		moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1858d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
18595244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
18605244eac9SBenno Rice 	} else {
186159276937SPeter Grehan 		moea_pte_overflow--;
18625244eac9SBenno Rice 	}
18635244eac9SBenno Rice 
18645244eac9SBenno Rice 	/*
18655244eac9SBenno Rice 	 * Update our statistics.
18665244eac9SBenno Rice 	 */
18675244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
18685244eac9SBenno Rice 	if (pvo->pvo_pte.pte_lo & PVO_WIRED)
18695244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
18705244eac9SBenno Rice 
18715244eac9SBenno Rice 	/*
18725244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
18735244eac9SBenno Rice 	 */
18744dba5df1SPeter Grehan 	if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) {
18755244eac9SBenno Rice 		struct	vm_page *pg;
18765244eac9SBenno Rice 
18778862232dSBenno Rice 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
18785244eac9SBenno Rice 		if (pg != NULL) {
187959276937SPeter Grehan 			moea_attr_save(pg, pvo->pvo_pte.pte_lo &
18805244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
18815244eac9SBenno Rice 		}
18825244eac9SBenno Rice 	}
18835244eac9SBenno Rice 
18845244eac9SBenno Rice 	/*
18855244eac9SBenno Rice 	 * Remove this PVO from the PV list.
18865244eac9SBenno Rice 	 */
18875244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
18885244eac9SBenno Rice 
18895244eac9SBenno Rice 	/*
18905244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
18915244eac9SBenno Rice 	 * if we aren't going to reuse it.
18925244eac9SBenno Rice 	 */
18935244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
189449f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
189559276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
189659276937SPeter Grehan 		    moea_upvo_zone, pvo);
189759276937SPeter Grehan 	moea_pvo_entries--;
189859276937SPeter Grehan 	moea_pvo_remove_calls++;
18995244eac9SBenno Rice }
19005244eac9SBenno Rice 
19015244eac9SBenno Rice static __inline int
190259276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
19035244eac9SBenno Rice {
19045244eac9SBenno Rice 	int	pteidx;
19055244eac9SBenno Rice 
19065244eac9SBenno Rice 	/*
19075244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
19085244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
19095244eac9SBenno Rice 	 * noticing the HID bit.
19105244eac9SBenno Rice 	 */
19115244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
19125244eac9SBenno Rice 	if (pvo->pvo_pte.pte_hi & PTE_HID)
191359276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
19145244eac9SBenno Rice 
19155244eac9SBenno Rice 	return (pteidx);
19165244eac9SBenno Rice }
19175244eac9SBenno Rice 
19185244eac9SBenno Rice static struct pvo_entry *
191959276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
19205244eac9SBenno Rice {
19215244eac9SBenno Rice 	struct	pvo_entry *pvo;
19225244eac9SBenno Rice 	int	ptegidx;
19235244eac9SBenno Rice 	u_int	sr;
19245244eac9SBenno Rice 
19255244eac9SBenno Rice 	va &= ~ADDR_POFF;
19265244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
19275244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
19285244eac9SBenno Rice 
192959276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
193059276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19315244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
19325244eac9SBenno Rice 			if (pteidx_p)
193359276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
1934f489bf21SAlan Cox 			break;
19355244eac9SBenno Rice 		}
19365244eac9SBenno Rice 	}
193759276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
19385244eac9SBenno Rice 
1939f489bf21SAlan Cox 	return (pvo);
19405244eac9SBenno Rice }
19415244eac9SBenno Rice 
19425244eac9SBenno Rice static struct pte *
194359276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
19445244eac9SBenno Rice {
19455244eac9SBenno Rice 	struct	pte *pt;
19465244eac9SBenno Rice 
19475244eac9SBenno Rice 	/*
19485244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
19495244eac9SBenno Rice 	 */
19505244eac9SBenno Rice 	if (pteidx == -1) {
19515244eac9SBenno Rice 		int	ptegidx;
19525244eac9SBenno Rice 		u_int	sr;
19535244eac9SBenno Rice 
19545244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
19555244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
195659276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
19575244eac9SBenno Rice 	}
19585244eac9SBenno Rice 
195959276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
1960d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
19615244eac9SBenno Rice 
19625244eac9SBenno Rice 	if ((pvo->pvo_pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
196359276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
19645244eac9SBenno Rice 		    "valid pte index", pvo);
19655244eac9SBenno Rice 	}
19665244eac9SBenno Rice 
19675244eac9SBenno Rice 	if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
196859276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
19695244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
19705244eac9SBenno Rice 	}
19715244eac9SBenno Rice 
19725244eac9SBenno Rice 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
19735244eac9SBenno Rice 		if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0) {
197459276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
197559276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
19765244eac9SBenno Rice 		}
19775244eac9SBenno Rice 
19785244eac9SBenno Rice 		if (((pt->pte_lo ^ pvo->pvo_pte.pte_lo) & ~(PTE_CHG|PTE_REF))
19795244eac9SBenno Rice 		    != 0) {
198059276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
198159276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
19825244eac9SBenno Rice 		}
19835244eac9SBenno Rice 
1984d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
19855244eac9SBenno Rice 		return (pt);
19865244eac9SBenno Rice 	}
19875244eac9SBenno Rice 
19885244eac9SBenno Rice 	if (pvo->pvo_pte.pte_hi & PTE_VALID) {
198959276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
199059276937SPeter Grehan 		    "moea_pteg_table but valid in pvo", pvo, pt);
19915244eac9SBenno Rice 	}
19925244eac9SBenno Rice 
1993d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
19945244eac9SBenno Rice 	return (NULL);
19955244eac9SBenno Rice }
19965244eac9SBenno Rice 
19975244eac9SBenno Rice /*
19985244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
19995244eac9SBenno Rice  */
20005244eac9SBenno Rice int
200159276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
20025244eac9SBenno Rice {
20035244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
20045244eac9SBenno Rice 	struct	pvo_entry *pvo;
20055244eac9SBenno Rice 	int	ptegidx, i, j;
20065244eac9SBenno Rice 	u_int	sr;
20075244eac9SBenno Rice 	struct	pteg *pteg;
20085244eac9SBenno Rice 	struct	pte *pt;
20095244eac9SBenno Rice 
201059276937SPeter Grehan 	moea_pte_spills++;
20115244eac9SBenno Rice 
2012d080d5fdSBenno Rice 	sr = mfsrin(addr);
20135244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
20145244eac9SBenno Rice 
20155244eac9SBenno Rice 	/*
20165244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
20175244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
20185244eac9SBenno Rice 	 */
201959276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
202059276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
20215244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
20225244eac9SBenno Rice 	i &= 7;
20235244eac9SBenno Rice 	pt = &pteg->pt[i];
20245244eac9SBenno Rice 
20255244eac9SBenno Rice 	source_pvo = NULL;
20265244eac9SBenno Rice 	victim_pvo = NULL;
202759276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20285244eac9SBenno Rice 		/*
20295244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
20305244eac9SBenno Rice 		 */
203159276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);
20325244eac9SBenno Rice 		if (source_pvo == NULL &&
203359276937SPeter Grehan 		    moea_pte_match(&pvo->pvo_pte, sr, addr,
20345244eac9SBenno Rice 		    pvo->pvo_pte.pte_hi & PTE_HID)) {
20355244eac9SBenno Rice 			/*
20365244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
20375244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
20385244eac9SBenno Rice 			 */
203959276937SPeter Grehan 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte);
20405244eac9SBenno Rice 
20415244eac9SBenno Rice 			if (j >= 0) {
20425244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
204359276937SPeter Grehan 				moea_pte_overflow--;
204459276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);
204559276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
20465244eac9SBenno Rice 				return (1);
20475244eac9SBenno Rice 			}
20485244eac9SBenno Rice 
20495244eac9SBenno Rice 			source_pvo = pvo;
20505244eac9SBenno Rice 
20515244eac9SBenno Rice 			if (victim_pvo != NULL)
20525244eac9SBenno Rice 				break;
20535244eac9SBenno Rice 		}
20545244eac9SBenno Rice 
20555244eac9SBenno Rice 		/*
20565244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
20575244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
20585244eac9SBenno Rice 		 */
20595244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
206059276937SPeter Grehan 		    moea_pte_compare(pt, &pvo->pvo_pte)) {
20615244eac9SBenno Rice 			victim_pvo = pvo;
20625244eac9SBenno Rice 			if (source_pvo != NULL)
20635244eac9SBenno Rice 				break;
20645244eac9SBenno Rice 		}
20655244eac9SBenno Rice 	}
20665244eac9SBenno Rice 
2067f489bf21SAlan Cox 	if (source_pvo == NULL) {
206859276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
20695244eac9SBenno Rice 		return (0);
2070f489bf21SAlan Cox 	}
20715244eac9SBenno Rice 
20725244eac9SBenno Rice 	if (victim_pvo == NULL) {
20735244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
207459276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
20755244eac9SBenno Rice 			    "entry", pt);
20765244eac9SBenno Rice 
20775244eac9SBenno Rice 		/*
20785244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
20795244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
20805244eac9SBenno Rice 		 */
208159276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
20825244eac9SBenno Rice 		    pvo_olink) {
208359276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);
20845244eac9SBenno Rice 			/*
20855244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
20865244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
20875244eac9SBenno Rice 			 */
208859276937SPeter Grehan 			if (moea_pte_compare(pt, &pvo->pvo_pte)) {
20895244eac9SBenno Rice 				victim_pvo = pvo;
20905244eac9SBenno Rice 				break;
20915244eac9SBenno Rice 			}
20925244eac9SBenno Rice 		}
20935244eac9SBenno Rice 
20945244eac9SBenno Rice 		if (victim_pvo == NULL)
209559276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
20965244eac9SBenno Rice 			    "entry", pt);
20975244eac9SBenno Rice 	}
20985244eac9SBenno Rice 
20995244eac9SBenno Rice 	/*
21005244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
21015244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
21025244eac9SBenno Rice 	 * contained in the TLB entry.
21035244eac9SBenno Rice 	 */
21045244eac9SBenno Rice 	source_pvo->pvo_pte.pte_hi &= ~PTE_HID;
21055244eac9SBenno Rice 
210659276937SPeter Grehan 	moea_pte_unset(pt, &victim_pvo->pvo_pte, victim_pvo->pvo_vaddr);
210759276937SPeter Grehan 	moea_pte_set(pt, &source_pvo->pvo_pte);
21085244eac9SBenno Rice 
21095244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
21105244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
211159276937SPeter Grehan 	moea_pte_replacements++;
21125244eac9SBenno Rice 
211359276937SPeter Grehan 	MOEA_PVO_CHECK(victim_pvo);
211459276937SPeter Grehan 	MOEA_PVO_CHECK(source_pvo);
21155244eac9SBenno Rice 
211659276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
21175244eac9SBenno Rice 	return (1);
21185244eac9SBenno Rice }
21195244eac9SBenno Rice 
21205244eac9SBenno Rice static int
212159276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
21225244eac9SBenno Rice {
21235244eac9SBenno Rice 	struct	pte *pt;
21245244eac9SBenno Rice 	int	i;
21255244eac9SBenno Rice 
2126d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2127d644a0b7SAlan Cox 
21285244eac9SBenno Rice 	/*
21295244eac9SBenno Rice 	 * First try primary hash.
21305244eac9SBenno Rice 	 */
213159276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
21325244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
21335244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
213459276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
21355244eac9SBenno Rice 			return (i);
21365244eac9SBenno Rice 		}
21375244eac9SBenno Rice 	}
21385244eac9SBenno Rice 
21395244eac9SBenno Rice 	/*
21405244eac9SBenno Rice 	 * Now try secondary hash.
21415244eac9SBenno Rice 	 */
214259276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2143bd8e6f87SPeter Grehan 
214459276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
21455244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
21465244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
214759276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
21485244eac9SBenno Rice 			return (i);
21495244eac9SBenno Rice 		}
21505244eac9SBenno Rice 	}
21515244eac9SBenno Rice 
215259276937SPeter Grehan 	panic("moea_pte_insert: overflow");
21535244eac9SBenno Rice 	return (-1);
21545244eac9SBenno Rice }
21555244eac9SBenno Rice 
21565244eac9SBenno Rice static boolean_t
215759276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
21585244eac9SBenno Rice {
21595244eac9SBenno Rice 	struct	pvo_entry *pvo;
21605244eac9SBenno Rice 	struct	pte *pt;
21615244eac9SBenno Rice 
21627b33c6efSPeter Grehan #if 0
216359276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
21645244eac9SBenno Rice 		return (TRUE);
21657b33c6efSPeter Grehan #endif
21665244eac9SBenno Rice 
21675244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
216859276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
21695244eac9SBenno Rice 
21705244eac9SBenno Rice 		/*
21715244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
21725244eac9SBenno Rice 		 * success.
21735244eac9SBenno Rice 		 */
21745244eac9SBenno Rice 		if (pvo->pvo_pte.pte_lo & ptebit) {
217559276937SPeter Grehan 			moea_attr_save(m, ptebit);
217659276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);	/* sanity check */
21775244eac9SBenno Rice 			return (TRUE);
21785244eac9SBenno Rice 		}
21795244eac9SBenno Rice 	}
21805244eac9SBenno Rice 
21815244eac9SBenno Rice 	/*
21825244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
21835244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
21845244eac9SBenno Rice 	 * the PTEs.
21855244eac9SBenno Rice 	 */
21865244eac9SBenno Rice 	SYNC();
21875244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
218859276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
21895244eac9SBenno Rice 
21905244eac9SBenno Rice 		/*
21915244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
21925244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
21935244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
21945244eac9SBenno Rice 		 */
219559276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
21965244eac9SBenno Rice 		if (pt != NULL) {
219759276937SPeter Grehan 			moea_pte_synch(pt, &pvo->pvo_pte);
2198d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
21995244eac9SBenno Rice 			if (pvo->pvo_pte.pte_lo & ptebit) {
220059276937SPeter Grehan 				moea_attr_save(m, ptebit);
220159276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);	/* sanity check */
22025244eac9SBenno Rice 				return (TRUE);
22035244eac9SBenno Rice 			}
22045244eac9SBenno Rice 		}
22055244eac9SBenno Rice 	}
22065244eac9SBenno Rice 
22074f7daed0SAndrew Gallatin 	return (FALSE);
22085244eac9SBenno Rice }
22095244eac9SBenno Rice 
221003b6e025SPeter Grehan static u_int
221159276937SPeter Grehan moea_clear_bit(vm_page_t m, int ptebit, int *origbit)
22125244eac9SBenno Rice {
221303b6e025SPeter Grehan 	u_int	count;
22145244eac9SBenno Rice 	struct	pvo_entry *pvo;
22155244eac9SBenno Rice 	struct	pte *pt;
22165244eac9SBenno Rice 	int	rv;
22175244eac9SBenno Rice 
22185244eac9SBenno Rice 	/*
22195244eac9SBenno Rice 	 * Clear the cached value.
22205244eac9SBenno Rice 	 */
222159276937SPeter Grehan 	rv = moea_attr_fetch(m);
222259276937SPeter Grehan 	moea_attr_clear(m, ptebit);
22235244eac9SBenno Rice 
22245244eac9SBenno Rice 	/*
22255244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
22265244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
22275244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
22285244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
22295244eac9SBenno Rice 	 * REF/CHG bits.
22305244eac9SBenno Rice 	 */
22315244eac9SBenno Rice 	SYNC();
22325244eac9SBenno Rice 
22335244eac9SBenno Rice 	/*
22345244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
22355244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
22365244eac9SBenno Rice 	 */
223703b6e025SPeter Grehan 	count = 0;
22385244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
223959276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
224059276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
22415244eac9SBenno Rice 		if (pt != NULL) {
224259276937SPeter Grehan 			moea_pte_synch(pt, &pvo->pvo_pte);
224303b6e025SPeter Grehan 			if (pvo->pvo_pte.pte_lo & ptebit) {
224403b6e025SPeter Grehan 				count++;
224559276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
22465244eac9SBenno Rice 			}
2247d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
224803b6e025SPeter Grehan 		}
22495244eac9SBenno Rice 		rv |= pvo->pvo_pte.pte_lo;
22505244eac9SBenno Rice 		pvo->pvo_pte.pte_lo &= ~ptebit;
225159276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
22525244eac9SBenno Rice 	}
22535244eac9SBenno Rice 
225403b6e025SPeter Grehan 	if (origbit != NULL) {
225503b6e025SPeter Grehan 		*origbit = rv;
225603b6e025SPeter Grehan 	}
225703b6e025SPeter Grehan 
225803b6e025SPeter Grehan 	return (count);
2259bdf71f56SBenno Rice }
22608bbfa33aSBenno Rice 
22618bbfa33aSBenno Rice /*
226232bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
226332bc7846SPeter Grehan  */
226432bc7846SPeter Grehan static int
226559276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
226632bc7846SPeter Grehan {
226732bc7846SPeter Grehan 	u_int prot;
226832bc7846SPeter Grehan 	u_int32_t start;
226932bc7846SPeter Grehan 	u_int32_t end;
227032bc7846SPeter Grehan 	u_int32_t bat_ble;
227132bc7846SPeter Grehan 
227232bc7846SPeter Grehan 	/*
227332bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
227432bc7846SPeter Grehan 	 */
227532bc7846SPeter Grehan 	if (!battable[idx].batu & BAT_Vs)
227632bc7846SPeter Grehan 		return (EINVAL);
227732bc7846SPeter Grehan 
227832bc7846SPeter Grehan 	/*
227932bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
228032bc7846SPeter Grehan 	 * so it can function as an i/o page
228132bc7846SPeter Grehan 	 */
228232bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
228332bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
228432bc7846SPeter Grehan 		return (EPERM);
228532bc7846SPeter Grehan 
228632bc7846SPeter Grehan 	/*
228732bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
228832bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
228932bc7846SPeter Grehan 	 * not requiring masking)
229032bc7846SPeter Grehan 	 */
229132bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
229232bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
229332bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
229432bc7846SPeter Grehan 
229532bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
229632bc7846SPeter Grehan 		return (ERANGE);
229732bc7846SPeter Grehan 
229832bc7846SPeter Grehan 	return (0);
229932bc7846SPeter Grehan }
230032bc7846SPeter Grehan 
230159276937SPeter Grehan boolean_t
230259276937SPeter Grehan moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2303c0763d37SSuleiman Souhlal {
2304c0763d37SSuleiman Souhlal 	int i;
2305c0763d37SSuleiman Souhlal 
2306c0763d37SSuleiman Souhlal 	/*
2307c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2308c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2309c0763d37SSuleiman Souhlal 	 */
2310c0763d37SSuleiman Souhlal 
2311c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
231259276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2313c0763d37SSuleiman Souhlal 			return (0);
2314c0763d37SSuleiman Souhlal 
2315c0763d37SSuleiman Souhlal 	return (EFAULT);
2316c0763d37SSuleiman Souhlal }
231732bc7846SPeter Grehan 
23186e4f008cSPeter Grehan boolean_t
23196e4f008cSPeter Grehan moea_page_executable(mmu_t mmu, vm_page_t pg)
23206e4f008cSPeter Grehan {
23216e4f008cSPeter Grehan 	return ((moea_attr_fetch(pg) & PTE_EXEC) == PTE_EXEC);
23226e4f008cSPeter Grehan }
23236e4f008cSPeter Grehan 
232432bc7846SPeter Grehan /*
23258bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
23268bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
23278bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
23288bbfa33aSBenno Rice  * NOT real memory.
23298bbfa33aSBenno Rice  */
23308bbfa33aSBenno Rice void *
233159276937SPeter Grehan moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
23328bbfa33aSBenno Rice {
233332bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
233432bc7846SPeter Grehan 	int i;
23358bbfa33aSBenno Rice 
233632bc7846SPeter Grehan 	ppa = trunc_page(pa);
23378bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
23388bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
23398bbfa33aSBenno Rice 
23408bbfa33aSBenno Rice 	GIANT_REQUIRED;
23418bbfa33aSBenno Rice 
234232bc7846SPeter Grehan 	/*
234332bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
234432bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
234532bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
234632bc7846SPeter Grehan 	 */
234732bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
234859276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
234932bc7846SPeter Grehan 			return ((void *) pa);
235032bc7846SPeter Grehan 	}
235132bc7846SPeter Grehan 
2352e53f32acSAlan Cox 	va = kmem_alloc_nofault(kernel_map, size);
23538bbfa33aSBenno Rice 	if (!va)
235459276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
23558bbfa33aSBenno Rice 
23568bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
235759276937SPeter Grehan 		moea_kenter(mmu, tmpva, ppa);
23588bbfa33aSBenno Rice 		TLBIE(tmpva); /* XXX or should it be invalidate-all ? */
23598bbfa33aSBenno Rice 		size -= PAGE_SIZE;
23608bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
236132bc7846SPeter Grehan 		ppa += PAGE_SIZE;
23628bbfa33aSBenno Rice 	}
23638bbfa33aSBenno Rice 
23648bbfa33aSBenno Rice 	return ((void *)(va + offset));
23658bbfa33aSBenno Rice }
23668bbfa33aSBenno Rice 
23678bbfa33aSBenno Rice void
236859276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
23698bbfa33aSBenno Rice {
23708bbfa33aSBenno Rice 	vm_offset_t base, offset;
23718bbfa33aSBenno Rice 
237232bc7846SPeter Grehan 	/*
237332bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
237432bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
237532bc7846SPeter Grehan 	 */
237632bc7846SPeter Grehan 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
23778bbfa33aSBenno Rice 		base = trunc_page(va);
23788bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
23798bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
23808bbfa33aSBenno Rice 		kmem_free(kernel_map, base, size);
23818bbfa33aSBenno Rice 	}
238232bc7846SPeter Grehan }
2383