xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision ce142d9ec03272d3f8e183ce69788ff97be0879a)
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 
26659276937SPeter Grehan struct	pvo_entry *moea_pvo_zeropage;
2675244eac9SBenno Rice 
26859276937SPeter Grehan vm_offset_t	moea_rkva_start = VM_MIN_KERNEL_ADDRESS;
26959276937SPeter Grehan u_int		moea_rkva_count = 4;
2705244eac9SBenno Rice 
2715244eac9SBenno Rice /*
27259276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2735244eac9SBenno Rice  */
27459276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2755244eac9SBenno Rice 
2765244eac9SBenno Rice /*
2775244eac9SBenno Rice  * PTE calls.
2785244eac9SBenno Rice  */
27959276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2805244eac9SBenno Rice 
2815244eac9SBenno Rice /*
2825244eac9SBenno Rice  * PVO calls.
2835244eac9SBenno Rice  */
28459276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2855244eac9SBenno Rice 		    vm_offset_t, vm_offset_t, u_int, int);
28659276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
28759276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
28859276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2895244eac9SBenno Rice 
2905244eac9SBenno Rice /*
2915244eac9SBenno Rice  * Utility routines.
2925244eac9SBenno Rice  */
293ce142d9eSAlan Cox static void		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
294ce142d9eSAlan Cox 			    vm_prot_t, boolean_t);
29559276937SPeter Grehan static struct		pvo_entry *moea_rkva_alloc(mmu_t);
29659276937SPeter Grehan static void		moea_pa_map(struct pvo_entry *, vm_offset_t,
2975244eac9SBenno Rice 			    struct pte *, int *);
29859276937SPeter Grehan static void		moea_pa_unmap(struct pvo_entry *, struct pte *, int *);
29959276937SPeter Grehan static void		moea_syncicache(vm_offset_t, vm_size_t);
30059276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
30159276937SPeter Grehan static u_int		moea_clear_bit(vm_page_t, int, int *);
30259276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
3035244eac9SBenno Rice static void		tlbia(void);
30459276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
30559276937SPeter Grehan 
30659276937SPeter Grehan /*
30759276937SPeter Grehan  * Kernel MMU interface
30859276937SPeter Grehan  */
30959276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
31059276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
31159276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t);
31259276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
31359276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t);
314ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
315ce142d9eSAlan Cox     vm_prot_t);
31659276937SPeter Grehan vm_page_t moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t,
31759276937SPeter Grehan     vm_page_t);
31859276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
31959276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
32059276937SPeter Grehan void moea_init(mmu_t);
32159276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
32259276937SPeter Grehan boolean_t moea_ts_referenced(mmu_t, vm_page_t);
32359276937SPeter Grehan vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int);
32459276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
32559276937SPeter Grehan void moea_page_protect(mmu_t, vm_page_t, vm_prot_t);
32659276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
32759276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
32859276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
32959276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
33059276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
33159276937SPeter Grehan void moea_release(mmu_t, pmap_t);
33259276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
33359276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
33459276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
33559276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
33659276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t);
33759276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
33859276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
33959276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
34059276937SPeter Grehan void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t);
34159276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
34259276937SPeter Grehan vm_offset_t moea_kextract(mmu_t, vm_offset_t);
34359276937SPeter Grehan void moea_kenter(mmu_t, vm_offset_t, vm_offset_t);
34459276937SPeter Grehan boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
34559276937SPeter Grehan 
34659276937SPeter Grehan static mmu_method_t moea_methods[] = {
34759276937SPeter Grehan 	MMUMETHOD(mmu_change_wiring,	moea_change_wiring),
34859276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
34959276937SPeter Grehan 	MMUMETHOD(mmu_clear_reference,	moea_clear_reference),
35059276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
35159276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
352ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
35359276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
35459276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
35559276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
35659276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
35759276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
35859276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
35959276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
36059276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
36159276937SPeter Grehan 	MMUMETHOD(mmu_page_protect,    	moea_page_protect),
36259276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
36359276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
36459276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
36559276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
36659276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
36759276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
36859276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
36959276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
37059276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
37159276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
37259276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_idle,	moea_zero_page_idle),
37359276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
37459276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
37559276937SPeter Grehan 
37659276937SPeter Grehan 	/* Internal interfaces */
37759276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
37859276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
37959276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
38059276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
38159276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
38259276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
38359276937SPeter Grehan 
38459276937SPeter Grehan 	{ 0, 0 }
38559276937SPeter Grehan };
38659276937SPeter Grehan 
38759276937SPeter Grehan static mmu_def_t oea_mmu = {
38859276937SPeter Grehan 	MMU_TYPE_OEA,
38959276937SPeter Grehan 	moea_methods,
39059276937SPeter Grehan 	0
39159276937SPeter Grehan };
39259276937SPeter Grehan MMU_DEF(oea_mmu);
39359276937SPeter Grehan 
3945244eac9SBenno Rice 
3955244eac9SBenno Rice static __inline int
3965244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
3975244eac9SBenno Rice {
3985244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
3995244eac9SBenno Rice }
4005244eac9SBenno Rice 
4015244eac9SBenno Rice static __inline u_int
4025244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4035244eac9SBenno Rice {
4045244eac9SBenno Rice 	u_int hash;
4055244eac9SBenno Rice 
4065244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4075244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
40859276937SPeter Grehan 	return (hash & moea_pteg_mask);
4095244eac9SBenno Rice }
4105244eac9SBenno Rice 
4115244eac9SBenno Rice static __inline struct pvo_head *
4128207b362SBenno Rice pa_to_pvoh(vm_offset_t pa, vm_page_t *pg_p)
4135244eac9SBenno Rice {
4145244eac9SBenno Rice 	struct	vm_page *pg;
4155244eac9SBenno Rice 
4165244eac9SBenno Rice 	pg = PHYS_TO_VM_PAGE(pa);
4175244eac9SBenno Rice 
4188207b362SBenno Rice 	if (pg_p != NULL)
4198207b362SBenno Rice 		*pg_p = pg;
4208207b362SBenno Rice 
4215244eac9SBenno Rice 	if (pg == NULL)
42259276937SPeter Grehan 		return (&moea_pvo_unmanaged);
4235244eac9SBenno Rice 
4245244eac9SBenno Rice 	return (&pg->md.mdpg_pvoh);
4255244eac9SBenno Rice }
4265244eac9SBenno Rice 
4275244eac9SBenno Rice static __inline struct pvo_head *
4285244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
429f9bac91bSBenno Rice {
430f9bac91bSBenno Rice 
4315244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
432f9bac91bSBenno Rice }
433f9bac91bSBenno Rice 
434f9bac91bSBenno Rice static __inline void
43559276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
436f9bac91bSBenno Rice {
437f9bac91bSBenno Rice 
4385244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4395244eac9SBenno Rice }
4405244eac9SBenno Rice 
4415244eac9SBenno Rice static __inline int
44259276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4435244eac9SBenno Rice {
4445244eac9SBenno Rice 
4455244eac9SBenno Rice 	return (m->md.mdpg_attrs);
446f9bac91bSBenno Rice }
447f9bac91bSBenno Rice 
448f9bac91bSBenno Rice static __inline void
44959276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
450f9bac91bSBenno Rice {
451f9bac91bSBenno Rice 
4525244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
453f9bac91bSBenno Rice }
454f9bac91bSBenno Rice 
455f9bac91bSBenno Rice static __inline int
45659276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
457f9bac91bSBenno Rice {
4585244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4595244eac9SBenno Rice 		return (1);
460f9bac91bSBenno Rice 
4615244eac9SBenno Rice 	return (0);
462f9bac91bSBenno Rice }
463f9bac91bSBenno Rice 
464f9bac91bSBenno Rice static __inline int
46559276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
466f9bac91bSBenno Rice {
4675244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
4685244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4695244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
470f9bac91bSBenno Rice }
471f9bac91bSBenno Rice 
4725244eac9SBenno Rice static __inline void
47359276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
474f9bac91bSBenno Rice {
475f9bac91bSBenno Rice 	/*
4765244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
4775244eac9SBenno Rice 	 * set when the real pte is set in memory.
478f9bac91bSBenno Rice 	 *
479f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
480f9bac91bSBenno Rice 	 */
4815244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
4825244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
4835244eac9SBenno Rice 	pt->pte_lo = pte_lo;
484f9bac91bSBenno Rice }
485f9bac91bSBenno Rice 
4865244eac9SBenno Rice static __inline void
48759276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
488f9bac91bSBenno Rice {
489f9bac91bSBenno Rice 
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 
4975244eac9SBenno Rice 	/*
4985244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
4995244eac9SBenno Rice 	 */
5005244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
5015244eac9SBenno Rice 	TLBIE(va);
5025244eac9SBenno Rice 	EIEIO();
5035244eac9SBenno Rice 	TLBSYNC();
5045244eac9SBenno Rice 	SYNC();
5055244eac9SBenno Rice }
5065244eac9SBenno Rice 
5075244eac9SBenno Rice static __inline void
50859276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5095244eac9SBenno Rice {
5105244eac9SBenno Rice 
5115244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5125244eac9SBenno Rice 
5135244eac9SBenno Rice 	/*
5145244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
5155244eac9SBenno Rice 	 * Note that the REF/CHG bits are from pvo_pt and thus should havce
5165244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5175244eac9SBenno Rice 	 */
5185244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
5195244eac9SBenno Rice 	EIEIO();
5205244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
5215244eac9SBenno Rice 	SYNC();
52259276937SPeter Grehan 	moea_pte_valid++;
5235244eac9SBenno Rice }
5245244eac9SBenno Rice 
5255244eac9SBenno Rice static __inline void
52659276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5275244eac9SBenno Rice {
5285244eac9SBenno Rice 
5295244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5305244eac9SBenno Rice 
5315244eac9SBenno Rice 	/*
5325244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5335244eac9SBenno Rice 	 */
5345244eac9SBenno Rice 	SYNC();
5355244eac9SBenno Rice 
5365244eac9SBenno Rice 	/*
5375244eac9SBenno Rice 	 * Invalidate the pte.
5385244eac9SBenno Rice 	 */
5395244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5405244eac9SBenno Rice 
5415244eac9SBenno Rice 	SYNC();
5425244eac9SBenno Rice 	TLBIE(va);
5435244eac9SBenno Rice 	EIEIO();
5445244eac9SBenno Rice 	TLBSYNC();
5455244eac9SBenno Rice 	SYNC();
5465244eac9SBenno Rice 
5475244eac9SBenno Rice 	/*
5485244eac9SBenno Rice 	 * Save the reg & chg bits.
5495244eac9SBenno Rice 	 */
55059276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
55159276937SPeter Grehan 	moea_pte_valid--;
5525244eac9SBenno Rice }
5535244eac9SBenno Rice 
5545244eac9SBenno Rice static __inline void
55559276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5565244eac9SBenno Rice {
5575244eac9SBenno Rice 
5585244eac9SBenno Rice 	/*
5595244eac9SBenno Rice 	 * Invalidate the PTE
5605244eac9SBenno Rice 	 */
56159276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
56259276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
563f9bac91bSBenno Rice }
564f9bac91bSBenno Rice 
565f9bac91bSBenno Rice /*
5665244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
567f9bac91bSBenno Rice  */
5685244eac9SBenno Rice static int	mr_cmp(const void *a, const void *b);
5695244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
5705244eac9SBenno Rice 
5715244eac9SBenno Rice static int
5725244eac9SBenno Rice mr_cmp(const void *a, const void *b)
573f9bac91bSBenno Rice {
5745244eac9SBenno Rice 	const struct	mem_region *regiona;
5755244eac9SBenno Rice 	const struct	mem_region *regionb;
576f9bac91bSBenno Rice 
5775244eac9SBenno Rice 	regiona = a;
5785244eac9SBenno Rice 	regionb = b;
5795244eac9SBenno Rice 	if (regiona->mr_start < regionb->mr_start)
5805244eac9SBenno Rice 		return (-1);
5815244eac9SBenno Rice 	else if (regiona->mr_start > regionb->mr_start)
5825244eac9SBenno Rice 		return (1);
5835244eac9SBenno Rice 	else
584f9bac91bSBenno Rice 		return (0);
585f9bac91bSBenno Rice }
5865244eac9SBenno Rice 
5875244eac9SBenno Rice static int
5885244eac9SBenno Rice om_cmp(const void *a, const void *b)
5895244eac9SBenno Rice {
5905244eac9SBenno Rice 	const struct	ofw_map *mapa;
5915244eac9SBenno Rice 	const struct	ofw_map *mapb;
5925244eac9SBenno Rice 
5935244eac9SBenno Rice 	mapa = a;
5945244eac9SBenno Rice 	mapb = b;
5955244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
5965244eac9SBenno Rice 		return (-1);
5975244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
5985244eac9SBenno Rice 		return (1);
5995244eac9SBenno Rice 	else
6005244eac9SBenno Rice 		return (0);
601f9bac91bSBenno Rice }
602f9bac91bSBenno Rice 
603f9bac91bSBenno Rice void
60459276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
605f9bac91bSBenno Rice {
60631c82d03SBenno Rice 	ihandle_t	mmui;
6075244eac9SBenno Rice 	phandle_t	chosen, mmu;
6085244eac9SBenno Rice 	int		sz;
6095244eac9SBenno Rice 	int		i, j;
61032bc7846SPeter Grehan 	int		ofw_mappings;
611e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6125244eac9SBenno Rice 	vm_offset_t	pa, va, off;
6135244eac9SBenno Rice 	u_int		batl, batu;
614f9bac91bSBenno Rice 
615f9bac91bSBenno Rice         /*
61632bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6170d290675SBenno Rice          */
6180d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6190d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6200d290675SBenno Rice 
6210d290675SBenno Rice         /*
6220d290675SBenno Rice          * Map PCI memory space.
6230d290675SBenno Rice          */
6240d290675SBenno Rice         battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6250d290675SBenno Rice         battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6260d290675SBenno Rice 
6270d290675SBenno Rice         battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6280d290675SBenno Rice         battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6290d290675SBenno Rice 
6300d290675SBenno Rice         battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6310d290675SBenno Rice         battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6320d290675SBenno Rice 
6330d290675SBenno Rice         battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6340d290675SBenno Rice         battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6350d290675SBenno Rice 
6360d290675SBenno Rice         /*
6370d290675SBenno Rice          * Map obio devices.
6380d290675SBenno Rice          */
6390d290675SBenno Rice         battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
6400d290675SBenno Rice         battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
6410d290675SBenno Rice 
6420d290675SBenno Rice 	/*
6435244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
6445244eac9SBenno Rice 	 * where we are.
645f9bac91bSBenno Rice 	 */
6465244eac9SBenno Rice 	batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6475244eac9SBenno Rice 	batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
64859276937SPeter Grehan 	__asm (".balign 32; \n"
64972ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
6505d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
6515244eac9SBenno Rice 	    :: "r"(batu), "r"(batl));
6520d290675SBenno Rice 
6535244eac9SBenno Rice #if 0
6540d290675SBenno Rice 	/* map frame buffer */
6550d290675SBenno Rice 	batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6560d290675SBenno Rice 	batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6575d64cf91SPeter Grehan 	__asm ("mtdbatu 1,%0; mtdbatl 1,%1; isync"
6580d290675SBenno Rice 	    :: "r"(batu), "r"(batl));
6590d290675SBenno Rice #endif
6600d290675SBenno Rice 
6610d290675SBenno Rice #if 1
6620d290675SBenno Rice 	/* map pci space */
6635244eac9SBenno Rice 	batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6640d290675SBenno Rice 	batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6655d64cf91SPeter Grehan 	__asm ("mtdbatu 1,%0; mtdbatl 1,%1; isync"
6665244eac9SBenno Rice 	    :: "r"(batu), "r"(batl));
6675244eac9SBenno Rice #endif
668f9bac91bSBenno Rice 
669f9bac91bSBenno Rice 	/*
6705244eac9SBenno Rice 	 * Set the start and end of kva.
671f9bac91bSBenno Rice 	 */
6725244eac9SBenno Rice 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
6735244eac9SBenno Rice 	virtual_end = VM_MAX_KERNEL_ADDRESS;
674f9bac91bSBenno Rice 
67531c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
67659276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
67731c82d03SBenno Rice 
67831c82d03SBenno Rice 	qsort(pregions, pregions_sz, sizeof(*pregions), mr_cmp);
67931c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
68032bc7846SPeter Grehan 		vm_offset_t pa;
68132bc7846SPeter Grehan 		vm_offset_t end;
68232bc7846SPeter Grehan 
68331c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
68431c82d03SBenno Rice 			pregions[i].mr_start,
68531c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
68631c82d03SBenno Rice 			pregions[i].mr_size);
68732bc7846SPeter Grehan 		/*
68832bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
68932bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
69032bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
69132bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
69232bc7846SPeter Grehan 		 * a while yet.
69332bc7846SPeter Grehan 		 */
69432bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
69532bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
69632bc7846SPeter Grehan 		do {
69732bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
69832bc7846SPeter Grehan 
69932bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
70032bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
70132bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
70232bc7846SPeter Grehan 		} while (pa < end);
70331c82d03SBenno Rice 	}
70431c82d03SBenno Rice 
70531c82d03SBenno Rice 	if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz)
70659276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
70731c82d03SBenno Rice 	qsort(regions, regions_sz, sizeof(*regions), mr_cmp);
7085244eac9SBenno Rice 	phys_avail_count = 0;
709d2c1f576SBenno Rice 	physsz = 0;
710b0c21309SPeter Grehan 	hwphyssz = 0;
711b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
71231c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7135244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7145244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7155244eac9SBenno Rice 		    regions[i].mr_size);
716e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
717e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
718e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
719e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
720e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
721e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
722e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
723e2f6d6e2SPeter Grehan 				phys_avail_count++;
724e2f6d6e2SPeter Grehan 			}
725e2f6d6e2SPeter Grehan 			break;
726e2f6d6e2SPeter Grehan 		}
7275244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7285244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7295244eac9SBenno Rice 		phys_avail_count++;
730d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
731f9bac91bSBenno Rice 	}
732d2c1f576SBenno Rice 	physmem = btoc(physsz);
733f9bac91bSBenno Rice 
734f9bac91bSBenno Rice 	/*
7355244eac9SBenno Rice 	 * Allocate PTEG table.
736f9bac91bSBenno Rice 	 */
7375244eac9SBenno Rice #ifdef PTEGCOUNT
73859276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
7395244eac9SBenno Rice #else
74059276937SPeter Grehan 	moea_pteg_count = 0x1000;
741f9bac91bSBenno Rice 
74259276937SPeter Grehan 	while (moea_pteg_count < physmem)
74359276937SPeter Grehan 		moea_pteg_count <<= 1;
744f9bac91bSBenno Rice 
74559276937SPeter Grehan 	moea_pteg_count >>= 1;
7465244eac9SBenno Rice #endif /* PTEGCOUNT */
747f9bac91bSBenno Rice 
74859276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
74959276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
7505244eac9SBenno Rice 	    size);
75159276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
75259276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
75359276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
75459276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
755f9bac91bSBenno Rice 
7565244eac9SBenno Rice 	/*
757864bc520SBenno Rice 	 * Allocate pv/overflow lists.
7585244eac9SBenno Rice 	 */
75959276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
76059276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
7615244eac9SBenno Rice 	    PAGE_SIZE);
76259276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
76359276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
76459276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
7655244eac9SBenno Rice 
7665244eac9SBenno Rice 	/*
767f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
768f489bf21SAlan Cox 	 * tables.
769f489bf21SAlan Cox 	 */
77059276937SPeter Grehan 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF);
771f489bf21SAlan Cox 
772f489bf21SAlan Cox 	/*
7735244eac9SBenno Rice 	 * Allocate the message buffer.
7745244eac9SBenno Rice 	 */
77559276937SPeter Grehan 	msgbuf_phys = moea_bootstrap_alloc(MSGBUF_SIZE, 0);
7765244eac9SBenno Rice 
7775244eac9SBenno Rice 	/*
7785244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
7795244eac9SBenno Rice 	 */
78059276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
7810d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
78259276937SPeter Grehan 	moea_bpvo_pool_index = 0;
7835244eac9SBenno Rice 
7845244eac9SBenno Rice 	/*
7855244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
7865244eac9SBenno Rice 	 */
78759276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
7885244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
78959276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
7905244eac9SBenno Rice 
7915244eac9SBenno Rice 	/*
7925244eac9SBenno Rice 	 * Set up the Open Firmware pmap and add it's mappings.
7935244eac9SBenno Rice 	 */
79459276937SPeter Grehan 	moea_pinit(mmup, &ofw_pmap);
7955244eac9SBenno Rice 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
7964daf20b2SPeter Grehan 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
7975244eac9SBenno Rice 	if ((chosen = OF_finddevice("/chosen")) == -1)
79859276937SPeter Grehan 		panic("moea_bootstrap: can't find /chosen");
7995244eac9SBenno Rice 	OF_getprop(chosen, "mmu", &mmui, 4);
8005244eac9SBenno Rice 	if ((mmu = OF_instance_to_package(mmui)) == -1)
80159276937SPeter Grehan 		panic("moea_bootstrap: can't get mmu package");
8025244eac9SBenno Rice 	if ((sz = OF_getproplen(mmu, "translations")) == -1)
80359276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translation count");
804aa39961eSBenno Rice 	translations = NULL;
8056cc1cdf4SPeter Grehan 	for (i = 0; phys_avail[i] != 0; i += 2) {
8066cc1cdf4SPeter Grehan 		if (phys_avail[i + 1] >= sz) {
807aa39961eSBenno Rice 			translations = (struct ofw_map *)phys_avail[i];
8086cc1cdf4SPeter Grehan 			break;
8096cc1cdf4SPeter Grehan 		}
810aa39961eSBenno Rice 	}
811aa39961eSBenno Rice 	if (translations == NULL)
81259276937SPeter Grehan 		panic("moea_bootstrap: no space to copy translations");
8135244eac9SBenno Rice 	bzero(translations, sz);
8145244eac9SBenno Rice 	if (OF_getprop(mmu, "translations", translations, sz) == -1)
81559276937SPeter Grehan 		panic("moea_bootstrap: can't get ofw translations");
81659276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: translations");
81731c82d03SBenno Rice 	sz /= sizeof(*translations);
8185244eac9SBenno Rice 	qsort(translations, sz, sizeof (*translations), om_cmp);
81932bc7846SPeter Grehan 	for (i = 0, ofw_mappings = 0; i < sz; i++) {
8205244eac9SBenno Rice 		CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
8215244eac9SBenno Rice 		    translations[i].om_pa, translations[i].om_va,
8225244eac9SBenno Rice 		    translations[i].om_len);
8235244eac9SBenno Rice 
82432bc7846SPeter Grehan 		/*
82532bc7846SPeter Grehan 		 * If the mapping is 1:1, let the RAM and device on-demand
82632bc7846SPeter Grehan 		 * BAT tables take care of the translation.
82732bc7846SPeter Grehan 		 */
82832bc7846SPeter Grehan 		if (translations[i].om_va == translations[i].om_pa)
82932bc7846SPeter Grehan 			continue;
8305244eac9SBenno Rice 
83132bc7846SPeter Grehan 		/* Enter the pages */
8325244eac9SBenno Rice 		for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) {
8335244eac9SBenno Rice 			struct	vm_page m;
8345244eac9SBenno Rice 
8355244eac9SBenno Rice 			m.phys_addr = translations[i].om_pa + off;
836ce142d9eSAlan Cox 			moea_enter_locked(&ofw_pmap,
83759276937SPeter Grehan 				   translations[i].om_va + off, &m,
8385244eac9SBenno Rice 				   VM_PROT_ALL, 1);
83932bc7846SPeter Grehan 			ofw_mappings++;
840f9bac91bSBenno Rice 		}
841f9bac91bSBenno Rice 	}
8425244eac9SBenno Rice #ifdef SMP
8435244eac9SBenno Rice 	TLBSYNC();
8445244eac9SBenno Rice #endif
8455244eac9SBenno Rice 
8465244eac9SBenno Rice 	/*
8475244eac9SBenno Rice 	 * Initialize the kernel pmap (which is statically allocated).
8485244eac9SBenno Rice 	 */
84948d0b1a0SAlan Cox 	PMAP_LOCK_INIT(kernel_pmap);
8505244eac9SBenno Rice 	for (i = 0; i < 16; i++) {
8515244eac9SBenno Rice 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT;
852f9bac91bSBenno Rice 	}
8535244eac9SBenno Rice 	kernel_pmap->pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
85422f2fe59SPeter Grehan 	kernel_pmap->pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
8555244eac9SBenno Rice 	kernel_pmap->pm_active = ~0;
8565244eac9SBenno Rice 
8575244eac9SBenno Rice 	/*
8585244eac9SBenno Rice 	 * Allocate a kernel stack with a guard page for thread0 and map it
8595244eac9SBenno Rice 	 * into the kernel page map.
8605244eac9SBenno Rice 	 */
86159276937SPeter Grehan 	pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, 0);
8625244eac9SBenno Rice 	kstack0_phys = pa;
8635244eac9SBenno Rice 	kstack0 = virtual_avail + (KSTACK_GUARD_PAGES * PAGE_SIZE);
86459276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", kstack0_phys,
8655244eac9SBenno Rice 	    kstack0);
8665244eac9SBenno Rice 	virtual_avail += (KSTACK_PAGES + KSTACK_GUARD_PAGES) * PAGE_SIZE;
8675244eac9SBenno Rice 	for (i = 0; i < KSTACK_PAGES; i++) {
8685244eac9SBenno Rice 		pa = kstack0_phys + i * PAGE_SIZE;
8695244eac9SBenno Rice 		va = kstack0 + i * PAGE_SIZE;
87059276937SPeter Grehan 		moea_kenter(mmup, va, pa);
8715244eac9SBenno Rice 		TLBIE(va);
872f9bac91bSBenno Rice 	}
873f9bac91bSBenno Rice 
874f9bac91bSBenno Rice 	/*
875c8607538SAlan Cox 	 * Calculate the last available physical address.
8765244eac9SBenno Rice 	 */
8775244eac9SBenno Rice 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
8785244eac9SBenno Rice 		;
8791f51408aSAlan Cox 	Maxmem = powerpc_btop(phys_avail[i + 1]);
8805244eac9SBenno Rice 
8815244eac9SBenno Rice 	/*
8825244eac9SBenno Rice 	 * Allocate virtual address space for the message buffer.
8835244eac9SBenno Rice 	 */
8845244eac9SBenno Rice 	msgbufp = (struct msgbuf *)virtual_avail;
8855244eac9SBenno Rice 	virtual_avail += round_page(MSGBUF_SIZE);
8865244eac9SBenno Rice 
8875244eac9SBenno Rice 	/*
8885244eac9SBenno Rice 	 * Initialize hardware.
8895244eac9SBenno Rice 	 */
8905244eac9SBenno Rice 	for (i = 0; i < 16; i++) {
891d080d5fdSBenno Rice 		mtsrin(i << ADDR_SR_SHFT, EMPTY_SEGMENT);
8925244eac9SBenno Rice 	}
8935244eac9SBenno Rice 	__asm __volatile ("mtsr %0,%1"
8945244eac9SBenno Rice 	    :: "n"(KERNEL_SR), "r"(KERNEL_SEGMENT));
89522f2fe59SPeter Grehan 	__asm __volatile ("mtsr %0,%1"
89622f2fe59SPeter Grehan 	    :: "n"(KERNEL2_SR), "r"(KERNEL2_SEGMENT));
8975244eac9SBenno Rice 	__asm __volatile ("sync; mtsdr1 %0; isync"
89859276937SPeter Grehan 	    :: "r"((u_int)moea_pteg_table | (moea_pteg_mask >> 10)));
8995244eac9SBenno Rice 	tlbia();
9005244eac9SBenno Rice 
9015244eac9SBenno Rice 	pmap_bootstrapped++;
9025244eac9SBenno Rice }
9035244eac9SBenno Rice 
9045244eac9SBenno Rice /*
9055244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9065244eac9SBenno Rice  * space can be accessed in any way.
907f9bac91bSBenno Rice  */
908f9bac91bSBenno Rice void
90959276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
910f9bac91bSBenno Rice {
9118207b362SBenno Rice 	pmap_t	pm, pmr;
912f9bac91bSBenno Rice 
913f9bac91bSBenno Rice 	/*
91432bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9155244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
916f9bac91bSBenno Rice 	 */
9175244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
918f9bac91bSBenno Rice 
91959276937SPeter Grehan 	if ((pmr = (pmap_t)moea_kextract(mmu, (vm_offset_t)pm)) == NULL)
9208207b362SBenno Rice 		pmr = pm;
9218207b362SBenno Rice 
9225244eac9SBenno Rice 	pm->pm_active |= PCPU_GET(cpumask);
9238207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
924ac6ba8bdSBenno Rice }
925ac6ba8bdSBenno Rice 
926ac6ba8bdSBenno Rice void
92759276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
928ac6ba8bdSBenno Rice {
929ac6ba8bdSBenno Rice 	pmap_t	pm;
930ac6ba8bdSBenno Rice 
931ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
932ac6ba8bdSBenno Rice 	pm->pm_active &= ~(PCPU_GET(cpumask));
9338207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
934f9bac91bSBenno Rice }
935f9bac91bSBenno Rice 
936f9bac91bSBenno Rice void
93759276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired)
938f9bac91bSBenno Rice {
9390f92104cSBenno Rice 	struct	pvo_entry *pvo;
9400f92104cSBenno Rice 
94148d0b1a0SAlan Cox 	PMAP_LOCK(pm);
94259276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
9430f92104cSBenno Rice 
9440f92104cSBenno Rice 	if (pvo != NULL) {
9450f92104cSBenno Rice 		if (wired) {
9460f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
9470f92104cSBenno Rice 				pm->pm_stats.wired_count++;
9480f92104cSBenno Rice 			pvo->pvo_vaddr |= PVO_WIRED;
9490f92104cSBenno Rice 		} else {
9500f92104cSBenno Rice 			if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
9510f92104cSBenno Rice 				pm->pm_stats.wired_count--;
9520f92104cSBenno Rice 			pvo->pvo_vaddr &= ~PVO_WIRED;
9530f92104cSBenno Rice 		}
9540f92104cSBenno Rice 	}
95548d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
956f9bac91bSBenno Rice }
957f9bac91bSBenno Rice 
958f9bac91bSBenno Rice void
95959276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
960f9bac91bSBenno Rice {
96125e2288dSBenno Rice 	vm_offset_t	dst;
96225e2288dSBenno Rice 	vm_offset_t	src;
96325e2288dSBenno Rice 
96425e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
96525e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
96625e2288dSBenno Rice 
96725e2288dSBenno Rice 	kcopy((void *)src, (void *)dst, PAGE_SIZE);
968f9bac91bSBenno Rice }
969111c77dcSBenno Rice 
970111c77dcSBenno Rice /*
9715244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
9725244eac9SBenno Rice  */
9735244eac9SBenno Rice void
97459276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
9755244eac9SBenno Rice {
9761a87a0daSPeter Wemm 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
9775244eac9SBenno Rice 	caddr_t va;
9785244eac9SBenno Rice 
9795244eac9SBenno Rice 	if (pa < SEGMENT_LENGTH) {
9805244eac9SBenno Rice 		va = (caddr_t) pa;
98159276937SPeter Grehan 	} else if (moea_initialized) {
98259276937SPeter Grehan 		if (moea_pvo_zeropage == NULL)
98359276937SPeter Grehan 			moea_pvo_zeropage = moea_rkva_alloc(mmu);
98459276937SPeter Grehan 		moea_pa_map(moea_pvo_zeropage, pa, NULL, NULL);
98559276937SPeter Grehan 		va = (caddr_t)PVO_VADDR(moea_pvo_zeropage);
9865244eac9SBenno Rice 	} else {
98759276937SPeter Grehan 		panic("moea_zero_page: can't zero pa %#x", pa);
9885244eac9SBenno Rice 	}
9895244eac9SBenno Rice 
9905244eac9SBenno Rice 	bzero(va, PAGE_SIZE);
9915244eac9SBenno Rice 
9925244eac9SBenno Rice 	if (pa >= SEGMENT_LENGTH)
99359276937SPeter Grehan 		moea_pa_unmap(moea_pvo_zeropage, NULL, NULL);
9945244eac9SBenno Rice }
9955244eac9SBenno Rice 
9965244eac9SBenno Rice void
99759276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
9985244eac9SBenno Rice {
9993495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10003495845eSBenno Rice 	caddr_t va;
10013495845eSBenno Rice 
10023495845eSBenno Rice 	if (pa < SEGMENT_LENGTH) {
10033495845eSBenno Rice 		va = (caddr_t) pa;
100459276937SPeter Grehan 	} else if (moea_initialized) {
100559276937SPeter Grehan 		if (moea_pvo_zeropage == NULL)
100659276937SPeter Grehan 			moea_pvo_zeropage = moea_rkva_alloc(mmu);
100759276937SPeter Grehan 		moea_pa_map(moea_pvo_zeropage, pa, NULL, NULL);
100859276937SPeter Grehan 		va = (caddr_t)PVO_VADDR(moea_pvo_zeropage);
10093495845eSBenno Rice 	} else {
101059276937SPeter Grehan 		panic("moea_zero_page: can't zero pa %#x", pa);
10113495845eSBenno Rice 	}
10123495845eSBenno Rice 
101332bc7846SPeter Grehan 	bzero(va + off, size);
10143495845eSBenno Rice 
10153495845eSBenno Rice 	if (pa >= SEGMENT_LENGTH)
101659276937SPeter Grehan 		moea_pa_unmap(moea_pvo_zeropage, NULL, NULL);
10175244eac9SBenno Rice }
10185244eac9SBenno Rice 
1019a58b3a68SPeter Wemm void
102059276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m)
1021a58b3a68SPeter Wemm {
1022a58b3a68SPeter Wemm 
102359276937SPeter Grehan 	/* XXX this is called outside of Giant, is moea_zero_page safe? */
1024a58b3a68SPeter Wemm 	/* XXX maybe have a dedicated mapping for this to avoid the problem? */
1025a58b3a68SPeter Wemm 	mtx_lock(&Giant);
102659276937SPeter Grehan 	moea_zero_page(mmu, m);
1027a58b3a68SPeter Wemm 	mtx_unlock(&Giant);
1028a58b3a68SPeter Wemm }
1029a58b3a68SPeter Wemm 
10305244eac9SBenno Rice /*
10315244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
10325244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
10335244eac9SBenno Rice  * will be wired down.
10345244eac9SBenno Rice  */
10355244eac9SBenno Rice void
103659276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
10375244eac9SBenno Rice 	   boolean_t wired)
10385244eac9SBenno Rice {
1039ce142d9eSAlan Cox 
1040ce142d9eSAlan Cox 	vm_page_lock_queues();
1041ce142d9eSAlan Cox 	PMAP_LOCK(pmap);
1042ce142d9eSAlan Cox 	pmap_enter_locked(pmap, va, m, prot, wired);
1043ce142d9eSAlan Cox 	vm_page_unlock_queues();
1044ce142d9eSAlan Cox 	PMAP_UNLOCK(pmap);
1045ce142d9eSAlan Cox }
1046ce142d9eSAlan Cox 
1047ce142d9eSAlan Cox /*
1048ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1049ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1050ce142d9eSAlan Cox  * will be wired down.
1051ce142d9eSAlan Cox  *
1052ce142d9eSAlan Cox  * The page queues and pmap must be locked.
1053ce142d9eSAlan Cox  */
1054ce142d9eSAlan Cox static void
1055ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1056ce142d9eSAlan Cox     boolean_t wired)
1057ce142d9eSAlan Cox {
10585244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1059378862a7SJeff Roberson 	uma_zone_t	zone;
10608207b362SBenno Rice 	vm_page_t	pg;
10618207b362SBenno Rice 	u_int		pte_lo, pvo_flags, was_exec, i;
10625244eac9SBenno Rice 	int		error;
10635244eac9SBenno Rice 
106459276937SPeter Grehan 	if (!moea_initialized) {
106559276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
106659276937SPeter Grehan 		zone = moea_upvo_zone;
10675244eac9SBenno Rice 		pvo_flags = 0;
10688207b362SBenno Rice 		pg = NULL;
10698207b362SBenno Rice 		was_exec = PTE_EXEC;
10705244eac9SBenno Rice 	} else {
107103b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
107203b6e025SPeter Grehan 		pg = m;
107359276937SPeter Grehan 		zone = moea_mpvo_zone;
10745244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
10758207b362SBenno Rice 		was_exec = 0;
10765244eac9SBenno Rice 	}
1077f489bf21SAlan Cox 	if (pmap_bootstrapped)
1078ce142d9eSAlan Cox 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1079ce142d9eSAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
10805244eac9SBenno Rice 
10814dba5df1SPeter Grehan 	/* XXX change the pvo head for fake pages */
10824dba5df1SPeter Grehan 	if ((m->flags & PG_FICTITIOUS) == PG_FICTITIOUS)
108359276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
10844dba5df1SPeter Grehan 
10858207b362SBenno Rice 	/*
10868207b362SBenno Rice 	 * If this is a managed page, and it's the first reference to the page,
10878207b362SBenno Rice 	 * clear the execness of the page.  Otherwise fetch the execness.
10888207b362SBenno Rice 	 */
10894dba5df1SPeter Grehan 	if ((pg != NULL) && ((m->flags & PG_FICTITIOUS) == 0)) {
10908207b362SBenno Rice 		if (LIST_EMPTY(pvo_head)) {
109159276937SPeter Grehan 			moea_attr_clear(pg, PTE_EXEC);
10928207b362SBenno Rice 		} else {
109359276937SPeter Grehan 			was_exec = moea_attr_fetch(pg) & PTE_EXEC;
10948207b362SBenno Rice 		}
10958207b362SBenno Rice 	}
10968207b362SBenno Rice 
10978207b362SBenno Rice 	/*
10988207b362SBenno Rice 	 * Assume the page is cache inhibited and access is guarded unless
10998207b362SBenno Rice 	 * it's in our available memory array.
11008207b362SBenno Rice 	 */
11015244eac9SBenno Rice 	pte_lo = PTE_I | PTE_G;
110231c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
110331c82d03SBenno Rice 		if ((VM_PAGE_TO_PHYS(m) >= pregions[i].mr_start) &&
110431c82d03SBenno Rice 		    (VM_PAGE_TO_PHYS(m) <
110531c82d03SBenno Rice 			(pregions[i].mr_start + pregions[i].mr_size))) {
11068207b362SBenno Rice 			pte_lo &= ~(PTE_I | PTE_G);
11078207b362SBenno Rice 			break;
11088207b362SBenno Rice 		}
11098207b362SBenno Rice 	}
11105244eac9SBenno Rice 
11115244eac9SBenno Rice 	if (prot & VM_PROT_WRITE)
11125244eac9SBenno Rice 		pte_lo |= PTE_BW;
11135244eac9SBenno Rice 	else
11145244eac9SBenno Rice 		pte_lo |= PTE_BR;
11155244eac9SBenno Rice 
11164dba5df1SPeter Grehan 	if (prot & VM_PROT_EXECUTE)
11174dba5df1SPeter Grehan 		pvo_flags |= PVO_EXECUTABLE;
11185244eac9SBenno Rice 
11195244eac9SBenno Rice 	if (wired)
11205244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11215244eac9SBenno Rice 
11224dba5df1SPeter Grehan 	if ((m->flags & PG_FICTITIOUS) != 0)
11234dba5df1SPeter Grehan 		pvo_flags |= PVO_FAKE;
11244dba5df1SPeter Grehan 
112559276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11268207b362SBenno Rice 	    pte_lo, pvo_flags);
11275244eac9SBenno Rice 
11288207b362SBenno Rice 	/*
11298207b362SBenno Rice 	 * Flush the real page from the instruction cache if this page is
11308207b362SBenno Rice 	 * mapped executable and cacheable and was not previously mapped (or
11318207b362SBenno Rice 	 * was not mapped executable).
11328207b362SBenno Rice 	 */
11338207b362SBenno Rice 	if (error == 0 && (pvo_flags & PVO_EXECUTABLE) &&
11348207b362SBenno Rice 	    (pte_lo & PTE_I) == 0 && was_exec == 0) {
11355244eac9SBenno Rice 		/*
11365244eac9SBenno Rice 		 * Flush the real memory from the cache.
11375244eac9SBenno Rice 		 */
113859276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
11398207b362SBenno Rice 		if (pg != NULL)
114059276937SPeter Grehan 			moea_attr_save(pg, PTE_EXEC);
11415244eac9SBenno Rice 	}
114232bc7846SPeter Grehan 
114332bc7846SPeter Grehan 	/* XXX syncicache always until problems are sorted */
114459276937SPeter Grehan 	moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
1145ce142d9eSAlan Cox }
1146ce142d9eSAlan Cox 
1147ce142d9eSAlan Cox /*
1148ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1149ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1150ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1151ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1152ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1153ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1154ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1155ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1156ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1157ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1158ce142d9eSAlan Cox  */
1159ce142d9eSAlan Cox void
1160ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1161ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1162ce142d9eSAlan Cox {
1163ce142d9eSAlan Cox 	vm_page_t m;
1164ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1165ce142d9eSAlan Cox 
1166ce142d9eSAlan Cox 	psize = atop(end - start);
1167ce142d9eSAlan Cox 	m = m_start;
1168ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1169ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1170ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
1171ce142d9eSAlan Cox 		    (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1172ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1173ce142d9eSAlan Cox 	}
1174ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
11755244eac9SBenno Rice }
11765244eac9SBenno Rice 
1177dca96f1aSAlan Cox vm_page_t
117859276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
117959276937SPeter Grehan     vm_prot_t prot, vm_page_t mpte)
1180dca96f1aSAlan Cox {
1181dca96f1aSAlan Cox 
1182ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1183ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
118459276937SPeter Grehan 	    FALSE);
1185ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1186dca96f1aSAlan Cox 	return (NULL);
1187dca96f1aSAlan Cox }
1188dca96f1aSAlan Cox 
118956b09388SAlan Cox vm_paddr_t
119059276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
11915244eac9SBenno Rice {
11920f92104cSBenno Rice 	struct	pvo_entry *pvo;
119348d0b1a0SAlan Cox 	vm_paddr_t pa;
11940f92104cSBenno Rice 
119548d0b1a0SAlan Cox 	PMAP_LOCK(pm);
119659276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
119748d0b1a0SAlan Cox 	if (pvo == NULL)
119848d0b1a0SAlan Cox 		pa = 0;
119948d0b1a0SAlan Cox 	else
120048d0b1a0SAlan Cox 		pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
120148d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
120248d0b1a0SAlan Cox 	return (pa);
12035244eac9SBenno Rice }
12045244eac9SBenno Rice 
12055244eac9SBenno Rice /*
120684792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
120784792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
120884792e72SPeter Grehan  * protection.
120984792e72SPeter Grehan  */
121084792e72SPeter Grehan vm_page_t
121159276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
121284792e72SPeter Grehan {
1213ab50a262SAlan Cox 	struct	pvo_entry *pvo;
121484792e72SPeter Grehan 	vm_page_t m;
121584792e72SPeter Grehan 
121684792e72SPeter Grehan 	m = NULL;
121784792e72SPeter Grehan 	mtx_lock(&Giant);
121848d0b1a0SAlan Cox 	vm_page_lock_queues();
121948d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
122059276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1221ab50a262SAlan Cox 	if (pvo != NULL && (pvo->pvo_pte.pte_hi & PTE_VALID) &&
1222ab50a262SAlan Cox 	    ((pvo->pvo_pte.pte_lo & PTE_PP) == PTE_RW ||
1223ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
1224ab50a262SAlan Cox 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
122584792e72SPeter Grehan 		vm_page_hold(m);
122684792e72SPeter Grehan 	}
122748d0b1a0SAlan Cox 	vm_page_unlock_queues();
122848d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
122984792e72SPeter Grehan 	mtx_unlock(&Giant);
123084792e72SPeter Grehan 	return (m);
123184792e72SPeter Grehan }
123284792e72SPeter Grehan 
12335244eac9SBenno Rice void
123459276937SPeter Grehan moea_init(mmu_t mmu)
12355244eac9SBenno Rice {
12365244eac9SBenno Rice 
123759276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_init");
12380d290675SBenno Rice 
123959276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12400ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12410ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
124259276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12430ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12440ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
124559276937SPeter Grehan 	moea_initialized = TRUE;
12465244eac9SBenno Rice }
12475244eac9SBenno Rice 
12485244eac9SBenno Rice boolean_t
124959276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
12505244eac9SBenno Rice {
12510f92104cSBenno Rice 
125203b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS |PG_UNMANAGED)) != 0)
12530f92104cSBenno Rice 		return (FALSE);
12540f92104cSBenno Rice 
125559276937SPeter Grehan 	return (moea_query_bit(m, PTE_CHG));
1256566526a9SAlan Cox }
1257566526a9SAlan Cox 
12585244eac9SBenno Rice void
125959276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m)
12605244eac9SBenno Rice {
126103b6e025SPeter Grehan 
126203b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
126303b6e025SPeter Grehan 		return;
126459276937SPeter Grehan 	moea_clear_bit(m, PTE_REF, NULL);
126503b6e025SPeter Grehan }
126603b6e025SPeter Grehan 
126703b6e025SPeter Grehan void
126859276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
126903b6e025SPeter Grehan {
127003b6e025SPeter Grehan 
127103b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
127203b6e025SPeter Grehan 		return;
127359276937SPeter Grehan 	moea_clear_bit(m, PTE_CHG, NULL);
12745244eac9SBenno Rice }
12755244eac9SBenno Rice 
12767f3a4093SMike Silbersack /*
127759276937SPeter Grehan  *	moea_ts_referenced:
12787f3a4093SMike Silbersack  *
12797f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
12807f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
12817f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
12827f3a4093SMike Silbersack  *	reference bits set.
12837f3a4093SMike Silbersack  *
12847f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
12857f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
12867f3a4093SMike Silbersack  *	optimal aging of shared pages.
12877f3a4093SMike Silbersack  */
128859276937SPeter Grehan boolean_t
128959276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
12905244eac9SBenno Rice {
129103b6e025SPeter Grehan 	int count;
129203b6e025SPeter Grehan 
129303b6e025SPeter Grehan 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
12945244eac9SBenno Rice 		return (0);
129503b6e025SPeter Grehan 
129659276937SPeter Grehan 	count = moea_clear_bit(m, PTE_REF, NULL);
129703b6e025SPeter Grehan 
129803b6e025SPeter Grehan 	return (count);
12995244eac9SBenno Rice }
13005244eac9SBenno Rice 
13015244eac9SBenno Rice /*
13025244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
13035244eac9SBenno Rice  */
13045244eac9SBenno Rice void
130559276937SPeter Grehan moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
13065244eac9SBenno Rice {
13075244eac9SBenno Rice 	u_int		pte_lo;
13085244eac9SBenno Rice 	int		error;
13095244eac9SBenno Rice 	int		i;
13105244eac9SBenno Rice 
13115244eac9SBenno Rice #if 0
13125244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
131359276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
13145244eac9SBenno Rice 		    va);
13155244eac9SBenno Rice #endif
13165244eac9SBenno Rice 
131732bc7846SPeter Grehan 	pte_lo = PTE_I | PTE_G;
131832bc7846SPeter Grehan 	for (i = 0; i < pregions_sz; i++) {
131932bc7846SPeter Grehan 		if ((pa >= pregions[i].mr_start) &&
132032bc7846SPeter Grehan 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
13215244eac9SBenno Rice 			pte_lo &= ~(PTE_I | PTE_G);
13225244eac9SBenno Rice 			break;
13235244eac9SBenno Rice 		}
13245244eac9SBenno Rice 	}
13255244eac9SBenno Rice 
13264711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
132759276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
132859276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
13295244eac9SBenno Rice 
13305244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
133159276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
13325244eac9SBenno Rice 		    pa, error);
13335244eac9SBenno Rice 
13345244eac9SBenno Rice 	/*
13355244eac9SBenno Rice 	 * Flush the real memory from the instruction cache.
13365244eac9SBenno Rice 	 */
13375244eac9SBenno Rice 	if ((pte_lo & (PTE_I | PTE_G)) == 0) {
133859276937SPeter Grehan 		moea_syncicache(pa, PAGE_SIZE);
13395244eac9SBenno Rice 	}
13404711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
13415244eac9SBenno Rice }
13425244eac9SBenno Rice 
1343e79f59e8SBenno Rice /*
1344e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1345e79f59e8SBenno Rice  * address.
1346e79f59e8SBenno Rice  */
13475244eac9SBenno Rice vm_offset_t
134859276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
13495244eac9SBenno Rice {
1350e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
135148d0b1a0SAlan Cox 	vm_paddr_t pa;
1352e79f59e8SBenno Rice 
13530efd0097SPeter Grehan #ifdef UMA_MD_SMALL_ALLOC
13540efd0097SPeter Grehan 	/*
13550efd0097SPeter Grehan 	 * Allow direct mappings
13560efd0097SPeter Grehan 	 */
13570efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
13580efd0097SPeter Grehan 		return (va);
13590efd0097SPeter Grehan 	}
13600efd0097SPeter Grehan #endif
13610efd0097SPeter Grehan 
136248d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
136359276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
136459276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
136548d0b1a0SAlan Cox 	pa = (pvo->pvo_pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
136648d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
136748d0b1a0SAlan Cox 	return (pa);
1368e79f59e8SBenno Rice }
1369e79f59e8SBenno Rice 
137088afb2a3SBenno Rice /*
137188afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
137288afb2a3SBenno Rice  */
13735244eac9SBenno Rice void
137459276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
13755244eac9SBenno Rice {
137688afb2a3SBenno Rice 
137759276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
13785244eac9SBenno Rice }
13795244eac9SBenno Rice 
13805244eac9SBenno Rice /*
13815244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
13825244eac9SBenno Rice  *
13835244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
13845244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
13855244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
13865244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
13875244eac9SBenno Rice  * first usable address after the mapped region.
13885244eac9SBenno Rice  */
13895244eac9SBenno Rice vm_offset_t
139059276937SPeter Grehan moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
139159276937SPeter Grehan     vm_offset_t pa_end, int prot)
13925244eac9SBenno Rice {
13935244eac9SBenno Rice 	vm_offset_t	sva, va;
13945244eac9SBenno Rice 
13955244eac9SBenno Rice 	sva = *virt;
13965244eac9SBenno Rice 	va = sva;
13975244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
139859276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
13995244eac9SBenno Rice 	*virt = va;
14005244eac9SBenno Rice 	return (sva);
14015244eac9SBenno Rice }
14025244eac9SBenno Rice 
14035244eac9SBenno Rice /*
14045244eac9SBenno Rice  * Lower the permission for all mappings to a given page.
14055244eac9SBenno Rice  */
14065244eac9SBenno Rice void
140759276937SPeter Grehan moea_page_protect(mmu_t mmu, vm_page_t m, vm_prot_t prot)
14085244eac9SBenno Rice {
14095244eac9SBenno Rice 	struct	pvo_head *pvo_head;
14105244eac9SBenno Rice 	struct	pvo_entry *pvo, *next_pvo;
14115244eac9SBenno Rice 	struct	pte *pt;
141248d0b1a0SAlan Cox 	pmap_t	pmap;
14135244eac9SBenno Rice 
14145244eac9SBenno Rice 	/*
14155244eac9SBenno Rice 	 * Since the routine only downgrades protection, if the
14165244eac9SBenno Rice 	 * maximal protection is desired, there isn't any change
14175244eac9SBenno Rice 	 * to be made.
14185244eac9SBenno Rice 	 */
14195244eac9SBenno Rice 	if ((prot & (VM_PROT_READ|VM_PROT_WRITE)) ==
14205244eac9SBenno Rice 	    (VM_PROT_READ|VM_PROT_WRITE))
14215244eac9SBenno Rice 		return;
14225244eac9SBenno Rice 
14235244eac9SBenno Rice 	pvo_head = vm_page_to_pvoh(m);
14245244eac9SBenno Rice 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
14255244eac9SBenno Rice 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
142659276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
142748d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
142848d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
14295244eac9SBenno Rice 
14305244eac9SBenno Rice 		/*
14315244eac9SBenno Rice 		 * Downgrading to no mapping at all, we just remove the entry.
14325244eac9SBenno Rice 		 */
14335244eac9SBenno Rice 		if ((prot & VM_PROT_READ) == 0) {
143459276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
143548d0b1a0SAlan Cox 			PMAP_UNLOCK(pmap);
14365244eac9SBenno Rice 			continue;
14375244eac9SBenno Rice 		}
14385244eac9SBenno Rice 
14395244eac9SBenno Rice 		/*
14405244eac9SBenno Rice 		 * If EXEC permission is being revoked, just clear the flag
14415244eac9SBenno Rice 		 * in the PVO.
14425244eac9SBenno Rice 		 */
14435244eac9SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
14445244eac9SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
14455244eac9SBenno Rice 
14465244eac9SBenno Rice 		/*
14475244eac9SBenno Rice 		 * If this entry is already RO, don't diddle with the page
14485244eac9SBenno Rice 		 * table.
14495244eac9SBenno Rice 		 */
14505244eac9SBenno Rice 		if ((pvo->pvo_pte.pte_lo & PTE_PP) == PTE_BR) {
145148d0b1a0SAlan Cox 			PMAP_UNLOCK(pmap);
145259276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);
14535244eac9SBenno Rice 			continue;
14545244eac9SBenno Rice 		}
14555244eac9SBenno Rice 
14565244eac9SBenno Rice 		/*
14575244eac9SBenno Rice 		 * Grab the PTE before we diddle the bits so pvo_to_pte can
14585244eac9SBenno Rice 		 * verify the pte contents are as expected.
14595244eac9SBenno Rice 		 */
146059276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
14615244eac9SBenno Rice 		pvo->pvo_pte.pte_lo &= ~PTE_PP;
14625244eac9SBenno Rice 		pvo->pvo_pte.pte_lo |= PTE_BR;
14635244eac9SBenno Rice 		if (pt != NULL)
146459276937SPeter Grehan 			moea_pte_change(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
146548d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
146659276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
14675244eac9SBenno Rice 	}
14682184ddd1SPeter Grehan 
14692184ddd1SPeter Grehan 	/*
14702184ddd1SPeter Grehan 	 * Downgrading from writeable: clear the VM page flag
14712184ddd1SPeter Grehan 	 */
14722184ddd1SPeter Grehan 	if ((prot & VM_PROT_WRITE) != VM_PROT_WRITE)
14732184ddd1SPeter Grehan 		vm_page_flag_clear(m, PG_WRITEABLE);
14745244eac9SBenno Rice }
14755244eac9SBenno Rice 
14765244eac9SBenno Rice /*
14777f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
14787f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
14797f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
14807f3a4093SMike Silbersack  * is only necessary that true be returned for a small
14817f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
14827f3a4093SMike Silbersack  */
14835244eac9SBenno Rice boolean_t
148459276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
14855244eac9SBenno Rice {
148603b6e025SPeter Grehan         int loops;
148703b6e025SPeter Grehan 	struct pvo_entry *pvo;
148803b6e025SPeter Grehan 
148959276937SPeter Grehan         if (!moea_initialized || (m->flags & PG_FICTITIOUS))
149003b6e025SPeter Grehan                 return FALSE;
149103b6e025SPeter Grehan 
149203b6e025SPeter Grehan 	loops = 0;
149303b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
149403b6e025SPeter Grehan 		if (pvo->pvo_pmap == pmap)
149503b6e025SPeter Grehan 			return (TRUE);
149603b6e025SPeter Grehan 		if (++loops >= 16)
149703b6e025SPeter Grehan 			break;
149803b6e025SPeter Grehan 	}
149903b6e025SPeter Grehan 
150003b6e025SPeter Grehan 	return (FALSE);
15015244eac9SBenno Rice }
15025244eac9SBenno Rice 
150359276937SPeter Grehan static u_int	moea_vsidcontext;
15045244eac9SBenno Rice 
15055244eac9SBenno Rice void
150659276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
15075244eac9SBenno Rice {
15085244eac9SBenno Rice 	int	i, mask;
15095244eac9SBenno Rice 	u_int	entropy;
15105244eac9SBenno Rice 
151159276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
151248d0b1a0SAlan Cox 	PMAP_LOCK_INIT(pmap);
15134daf20b2SPeter Grehan 
15145244eac9SBenno Rice 	entropy = 0;
15155244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
15165244eac9SBenno Rice 
15175244eac9SBenno Rice 	/*
15185244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
15195244eac9SBenno Rice 	 */
15205244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
15215244eac9SBenno Rice 		u_int	hash, n;
15225244eac9SBenno Rice 
15235244eac9SBenno Rice 		/*
15245244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
15255244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
15265244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
15275244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
15285244eac9SBenno Rice 		 * instead of a multiply.)
15295244eac9SBenno Rice 		 */
153059276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
153159276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
15325244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
15335244eac9SBenno Rice 			continue;
15345244eac9SBenno Rice 		n = hash >> 5;
15355244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
153659276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
153759276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
15385244eac9SBenno Rice 			/* anything free in this bucket? */
153959276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
154059276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
15415244eac9SBenno Rice 				continue;
15425244eac9SBenno Rice 			}
154359276937SPeter Grehan 			i = ffs(~moea_vsid_bitmap[i]) - 1;
15445244eac9SBenno Rice 			mask = 1 << i;
15455244eac9SBenno Rice 			hash &= 0xfffff & ~(VSID_NBPW - 1);
15465244eac9SBenno Rice 			hash |= i;
15475244eac9SBenno Rice 		}
154859276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
15495244eac9SBenno Rice 		for (i = 0; i < 16; i++)
15505244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
15515244eac9SBenno Rice 		return;
15525244eac9SBenno Rice 	}
15535244eac9SBenno Rice 
155459276937SPeter Grehan 	panic("moea_pinit: out of segments");
15555244eac9SBenno Rice }
15565244eac9SBenno Rice 
15575244eac9SBenno Rice /*
15585244eac9SBenno Rice  * Initialize the pmap associated with process 0.
15595244eac9SBenno Rice  */
15605244eac9SBenno Rice void
156159276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
15625244eac9SBenno Rice {
15635244eac9SBenno Rice 
156459276937SPeter Grehan 	moea_pinit(mmu, pm);
15655244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
15665244eac9SBenno Rice }
15675244eac9SBenno Rice 
1568e79f59e8SBenno Rice /*
1569e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1570e79f59e8SBenno Rice  */
15715244eac9SBenno Rice void
157259276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
157359276937SPeter Grehan     vm_prot_t prot)
15745244eac9SBenno Rice {
1575e79f59e8SBenno Rice 	struct	pvo_entry *pvo;
1576e79f59e8SBenno Rice 	struct	pte *pt;
1577e79f59e8SBenno Rice 	int	pteidx;
1578e79f59e8SBenno Rice 
157959276937SPeter Grehan 	CTR4(KTR_PMAP, "moea_protect: pm=%p sva=%#x eva=%#x prot=%#x", pm, sva,
1580e79f59e8SBenno Rice 	    eva, prot);
1581e79f59e8SBenno Rice 
1582e79f59e8SBenno Rice 
1583e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
158459276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1585e79f59e8SBenno Rice 
1586e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
15879bb0e068SAlan Cox 		mtx_lock(&Giant);
158859276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
15899bb0e068SAlan Cox 		mtx_unlock(&Giant);
1590e79f59e8SBenno Rice 		return;
1591e79f59e8SBenno Rice 	}
1592e79f59e8SBenno Rice 
15939bb0e068SAlan Cox 	mtx_lock(&Giant);
15943d2e54c3SAlan Cox 	vm_page_lock_queues();
159548d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1596e79f59e8SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
159759276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
1598e79f59e8SBenno Rice 		if (pvo == NULL)
1599e79f59e8SBenno Rice 			continue;
1600e79f59e8SBenno Rice 
1601e79f59e8SBenno Rice 		if ((prot & VM_PROT_EXECUTE) == 0)
1602e79f59e8SBenno Rice 			pvo->pvo_vaddr &= ~PVO_EXECUTABLE;
1603e79f59e8SBenno Rice 
1604e79f59e8SBenno Rice 		/*
1605e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1606e79f59e8SBenno Rice 		 * copy.
1607e79f59e8SBenno Rice 		 */
160859276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, pteidx);
1609e79f59e8SBenno Rice 		/*
1610e79f59e8SBenno Rice 		 * Change the protection of the page.
1611e79f59e8SBenno Rice 		 */
1612e79f59e8SBenno Rice 		pvo->pvo_pte.pte_lo &= ~PTE_PP;
1613e79f59e8SBenno Rice 		pvo->pvo_pte.pte_lo |= PTE_BR;
1614e79f59e8SBenno Rice 
1615e79f59e8SBenno Rice 		/*
1616e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1617e79f59e8SBenno Rice 		 */
1618e79f59e8SBenno Rice 		if (pt != NULL)
161959276937SPeter Grehan 			moea_pte_change(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
1620e79f59e8SBenno Rice 	}
16213d2e54c3SAlan Cox 	vm_page_unlock_queues();
162248d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
16239bb0e068SAlan Cox 	mtx_unlock(&Giant);
16245244eac9SBenno Rice }
16255244eac9SBenno Rice 
162688afb2a3SBenno Rice /*
162788afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
162888afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
162988afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
163088afb2a3SBenno Rice  */
16315244eac9SBenno Rice void
163259276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
16335244eac9SBenno Rice {
163403b6e025SPeter Grehan 	vm_offset_t va;
16355244eac9SBenno Rice 
163603b6e025SPeter Grehan 	va = sva;
163703b6e025SPeter Grehan 	while (count-- > 0) {
163859276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
163903b6e025SPeter Grehan 		va += PAGE_SIZE;
164003b6e025SPeter Grehan 		m++;
164103b6e025SPeter Grehan 	}
16425244eac9SBenno Rice }
16435244eac9SBenno Rice 
164488afb2a3SBenno Rice /*
164588afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
164659276937SPeter Grehan  * temporary mappings entered by moea_qenter.
164788afb2a3SBenno Rice  */
16485244eac9SBenno Rice void
164959276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
16505244eac9SBenno Rice {
165103b6e025SPeter Grehan 	vm_offset_t va;
165288afb2a3SBenno Rice 
165303b6e025SPeter Grehan 	va = sva;
165403b6e025SPeter Grehan 	while (count-- > 0) {
165559276937SPeter Grehan 		moea_kremove(mmu, va);
165603b6e025SPeter Grehan 		va += PAGE_SIZE;
165703b6e025SPeter Grehan 	}
16585244eac9SBenno Rice }
16595244eac9SBenno Rice 
16605244eac9SBenno Rice void
166159276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
16625244eac9SBenno Rice {
166332bc7846SPeter Grehan         int idx, mask;
166432bc7846SPeter Grehan 
166532bc7846SPeter Grehan 	/*
166632bc7846SPeter Grehan 	 * Free segment register's VSID
166732bc7846SPeter Grehan 	 */
166832bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
166959276937SPeter Grehan                 panic("moea_release");
167032bc7846SPeter Grehan 
167132bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
167232bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
167332bc7846SPeter Grehan         idx /= VSID_NBPW;
167459276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
167548d0b1a0SAlan Cox 	PMAP_LOCK_DESTROY(pmap);
16765244eac9SBenno Rice }
16775244eac9SBenno Rice 
167888afb2a3SBenno Rice /*
167988afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
168088afb2a3SBenno Rice  */
16815244eac9SBenno Rice void
168259276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
16835244eac9SBenno Rice {
168488afb2a3SBenno Rice 	struct	pvo_entry *pvo;
168588afb2a3SBenno Rice 	int	pteidx;
168688afb2a3SBenno Rice 
16873d2e54c3SAlan Cox 	vm_page_lock_queues();
168848d0b1a0SAlan Cox 	PMAP_LOCK(pm);
168988afb2a3SBenno Rice 	for (; sva < eva; sva += PAGE_SIZE) {
169059276937SPeter Grehan 		pvo = moea_pvo_find_va(pm, sva, &pteidx);
169188afb2a3SBenno Rice 		if (pvo != NULL) {
169259276937SPeter Grehan 			moea_pvo_remove(pvo, pteidx);
169388afb2a3SBenno Rice 		}
169488afb2a3SBenno Rice 	}
169548d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
169694aa7aecSPeter Grehan 	vm_page_unlock_queues();
16975244eac9SBenno Rice }
16985244eac9SBenno Rice 
1699e79f59e8SBenno Rice /*
170059276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
170103b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
170203b6e025SPeter Grehan  */
170303b6e025SPeter Grehan void
170459276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
170503b6e025SPeter Grehan {
170603b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
170703b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
170848d0b1a0SAlan Cox 	pmap_t	pmap;
170903b6e025SPeter Grehan 
171084792e72SPeter Grehan 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
171103b6e025SPeter Grehan 
171203b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
171303b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
171403b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
171503b6e025SPeter Grehan 
171659276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
171748d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
171848d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
171959276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
172048d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
172103b6e025SPeter Grehan 	}
172203b6e025SPeter Grehan 	vm_page_flag_clear(m, PG_WRITEABLE);
172303b6e025SPeter Grehan }
172403b6e025SPeter Grehan 
172503b6e025SPeter Grehan /*
17265244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
172759276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
17285244eac9SBenno Rice  * calculated.
17295244eac9SBenno Rice  */
17305244eac9SBenno Rice static vm_offset_t
173159276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
17325244eac9SBenno Rice {
17335244eac9SBenno Rice 	vm_offset_t	s, e;
17345244eac9SBenno Rice 	int		i, j;
17355244eac9SBenno Rice 
17365244eac9SBenno Rice 	size = round_page(size);
17375244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
17385244eac9SBenno Rice 		if (align != 0)
17395244eac9SBenno Rice 			s = (phys_avail[i] + align - 1) & ~(align - 1);
17405244eac9SBenno Rice 		else
17415244eac9SBenno Rice 			s = phys_avail[i];
17425244eac9SBenno Rice 		e = s + size;
17435244eac9SBenno Rice 
17445244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
17455244eac9SBenno Rice 			continue;
17465244eac9SBenno Rice 
17475244eac9SBenno Rice 		if (s == phys_avail[i]) {
17485244eac9SBenno Rice 			phys_avail[i] += size;
17495244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
17505244eac9SBenno Rice 			phys_avail[i + 1] -= size;
17515244eac9SBenno Rice 		} else {
17525244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
17535244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
17545244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
17555244eac9SBenno Rice 			}
17565244eac9SBenno Rice 
17575244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
17585244eac9SBenno Rice 			phys_avail[i + 1] = s;
17595244eac9SBenno Rice 			phys_avail[i + 2] = e;
17605244eac9SBenno Rice 			phys_avail_count++;
17615244eac9SBenno Rice 		}
17625244eac9SBenno Rice 
17635244eac9SBenno Rice 		return (s);
17645244eac9SBenno Rice 	}
176559276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
17665244eac9SBenno Rice }
17675244eac9SBenno Rice 
17685244eac9SBenno Rice /*
17695244eac9SBenno Rice  * Return an unmapped pvo for a kernel virtual address.
17705244eac9SBenno Rice  * Used by pmap functions that operate on physical pages.
17715244eac9SBenno Rice  */
17725244eac9SBenno Rice static struct pvo_entry *
177359276937SPeter Grehan moea_rkva_alloc(mmu_t mmu)
17745244eac9SBenno Rice {
17755244eac9SBenno Rice 	struct		pvo_entry *pvo;
17765244eac9SBenno Rice 	struct		pte *pt;
17775244eac9SBenno Rice 	vm_offset_t	kva;
17785244eac9SBenno Rice 	int		pteidx;
17795244eac9SBenno Rice 
178059276937SPeter Grehan 	if (moea_rkva_count == 0)
178159276937SPeter Grehan 		panic("moea_rkva_alloc: no more reserved KVAs");
17825244eac9SBenno Rice 
178359276937SPeter Grehan 	kva = moea_rkva_start + (PAGE_SIZE * --moea_rkva_count);
178459276937SPeter Grehan 	moea_kenter(mmu, kva, 0);
17855244eac9SBenno Rice 
178659276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, kva, &pteidx);
17875244eac9SBenno Rice 
17885244eac9SBenno Rice 	if (pvo == NULL)
178959276937SPeter Grehan 		panic("moea_kva_alloc: moea_pvo_find_va failed");
17905244eac9SBenno Rice 
179159276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
17925244eac9SBenno Rice 
17935244eac9SBenno Rice 	if (pt == NULL)
179459276937SPeter Grehan 		panic("moea_kva_alloc: moea_pvo_to_pte failed");
17955244eac9SBenno Rice 
179659276937SPeter Grehan 	moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
17975244eac9SBenno Rice 	PVO_PTEGIDX_CLR(pvo);
17985244eac9SBenno Rice 
179959276937SPeter Grehan 	moea_pte_overflow++;
18005244eac9SBenno Rice 
18015244eac9SBenno Rice 	return (pvo);
18025244eac9SBenno Rice }
18035244eac9SBenno Rice 
18045244eac9SBenno Rice static void
180559276937SPeter Grehan moea_pa_map(struct pvo_entry *pvo, vm_offset_t pa, struct pte *saved_pt,
18065244eac9SBenno Rice     int *depth_p)
18075244eac9SBenno Rice {
18085244eac9SBenno Rice 	struct	pte *pt;
18095244eac9SBenno Rice 
18105244eac9SBenno Rice 	/*
18115244eac9SBenno Rice 	 * If this pvo already has a valid pte, we need to save it so it can
18125244eac9SBenno Rice 	 * be restored later.  We then just reload the new PTE over the old
18135244eac9SBenno Rice 	 * slot.
18145244eac9SBenno Rice 	 */
18155244eac9SBenno Rice 	if (saved_pt != NULL) {
181659276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
18175244eac9SBenno Rice 
18185244eac9SBenno Rice 		if (pt != NULL) {
181959276937SPeter Grehan 			moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
18205244eac9SBenno Rice 			PVO_PTEGIDX_CLR(pvo);
182159276937SPeter Grehan 			moea_pte_overflow++;
18225244eac9SBenno Rice 		}
18235244eac9SBenno Rice 
18245244eac9SBenno Rice 		*saved_pt = pvo->pvo_pte;
18255244eac9SBenno Rice 
18265244eac9SBenno Rice 		pvo->pvo_pte.pte_lo &= ~PTE_RPGN;
18275244eac9SBenno Rice 	}
18285244eac9SBenno Rice 
18295244eac9SBenno Rice 	pvo->pvo_pte.pte_lo |= pa;
18305244eac9SBenno Rice 
183159276937SPeter Grehan 	if (!moea_pte_spill(pvo->pvo_vaddr))
183259276937SPeter Grehan 		panic("moea_pa_map: could not spill pvo %p", pvo);
18335244eac9SBenno Rice 
18345244eac9SBenno Rice 	if (depth_p != NULL)
18355244eac9SBenno Rice 		(*depth_p)++;
18365244eac9SBenno Rice }
18375244eac9SBenno Rice 
18385244eac9SBenno Rice static void
183959276937SPeter Grehan moea_pa_unmap(struct pvo_entry *pvo, struct pte *saved_pt, int *depth_p)
18405244eac9SBenno Rice {
18415244eac9SBenno Rice 	struct	pte *pt;
18425244eac9SBenno Rice 
184359276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, -1);
18445244eac9SBenno Rice 
18455244eac9SBenno Rice 	if (pt != NULL) {
184659276937SPeter Grehan 		moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
18475244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
184859276937SPeter Grehan 		moea_pte_overflow++;
18495244eac9SBenno Rice 	}
18505244eac9SBenno Rice 
18515244eac9SBenno Rice 	pvo->pvo_pte.pte_lo &= ~PTE_RPGN;
18525244eac9SBenno Rice 
18535244eac9SBenno Rice 	/*
18545244eac9SBenno Rice 	 * If there is a saved PTE and it's valid, restore it and return.
18555244eac9SBenno Rice 	 */
18565244eac9SBenno Rice 	if (saved_pt != NULL && (saved_pt->pte_lo & PTE_RPGN) != 0) {
18575244eac9SBenno Rice 		if (depth_p != NULL && --(*depth_p) == 0)
185859276937SPeter Grehan 			panic("moea_pa_unmap: restoring but depth == 0");
18595244eac9SBenno Rice 
18605244eac9SBenno Rice 		pvo->pvo_pte = *saved_pt;
18615244eac9SBenno Rice 
186259276937SPeter Grehan 		if (!moea_pte_spill(pvo->pvo_vaddr))
186359276937SPeter Grehan 			panic("moea_pa_unmap: could not spill pvo %p", pvo);
18645244eac9SBenno Rice 	}
18655244eac9SBenno Rice }
18665244eac9SBenno Rice 
18675244eac9SBenno Rice static void
186859276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len)
18695244eac9SBenno Rice {
18705244eac9SBenno Rice 	__syncicache((void *)pa, len);
18715244eac9SBenno Rice }
18725244eac9SBenno Rice 
18735244eac9SBenno Rice static void
18745244eac9SBenno Rice tlbia(void)
18755244eac9SBenno Rice {
18765244eac9SBenno Rice 	caddr_t	i;
18775244eac9SBenno Rice 
18785244eac9SBenno Rice 	SYNC();
18795244eac9SBenno Rice 	for (i = 0; i < (caddr_t)0x00040000; i += 0x00001000) {
18805244eac9SBenno Rice 		TLBIE(i);
18815244eac9SBenno Rice 		EIEIO();
18825244eac9SBenno Rice 	}
18835244eac9SBenno Rice 	TLBSYNC();
18845244eac9SBenno Rice 	SYNC();
18855244eac9SBenno Rice }
18865244eac9SBenno Rice 
18875244eac9SBenno Rice static int
188859276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
18895244eac9SBenno Rice     vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags)
18905244eac9SBenno Rice {
18915244eac9SBenno Rice 	struct	pvo_entry *pvo;
18925244eac9SBenno Rice 	u_int	sr;
18935244eac9SBenno Rice 	int	first;
18945244eac9SBenno Rice 	u_int	ptegidx;
18955244eac9SBenno Rice 	int	i;
189632bc7846SPeter Grehan 	int     bootstrap;
18975244eac9SBenno Rice 
189859276937SPeter Grehan 	moea_pvo_enter_calls++;
18998207b362SBenno Rice 	first = 0;
190032bc7846SPeter Grehan 	bootstrap = 0;
190132bc7846SPeter Grehan 
19025244eac9SBenno Rice 	/*
19035244eac9SBenno Rice 	 * Compute the PTE Group index.
19045244eac9SBenno Rice 	 */
19055244eac9SBenno Rice 	va &= ~ADDR_POFF;
19065244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
19075244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
19085244eac9SBenno Rice 
19095244eac9SBenno Rice 	/*
19105244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
19115244eac9SBenno Rice 	 * there is a mapping.
19125244eac9SBenno Rice 	 */
191359276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
191459276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19155244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
1916fafc7362SBenno Rice 			if ((pvo->pvo_pte.pte_lo & PTE_RPGN) == pa &&
1917fafc7362SBenno Rice 			    (pvo->pvo_pte.pte_lo & PTE_PP) ==
1918fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
191959276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
192049f8f727SBenno Rice 				return (0);
1921fafc7362SBenno Rice 			}
192259276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
19235244eac9SBenno Rice 			break;
19245244eac9SBenno Rice 		}
19255244eac9SBenno Rice 	}
19265244eac9SBenno Rice 
19275244eac9SBenno Rice 	/*
19285244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
19295244eac9SBenno Rice 	 */
193059276937SPeter Grehan 	if (moea_initialized) {
1931378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
193249f8f727SBenno Rice 	} else {
193359276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
193459276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
193559276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
19360d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
193749f8f727SBenno Rice 		}
193859276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
193959276937SPeter Grehan 		moea_bpvo_pool_index++;
194032bc7846SPeter Grehan 		bootstrap = 1;
194149f8f727SBenno Rice 	}
19425244eac9SBenno Rice 
19435244eac9SBenno Rice 	if (pvo == NULL) {
194459276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
19455244eac9SBenno Rice 		return (ENOMEM);
19465244eac9SBenno Rice 	}
19475244eac9SBenno Rice 
194859276937SPeter Grehan 	moea_pvo_entries++;
19495244eac9SBenno Rice 	pvo->pvo_vaddr = va;
19505244eac9SBenno Rice 	pvo->pvo_pmap = pm;
195159276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
19525244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
19535244eac9SBenno Rice 	if (flags & VM_PROT_EXECUTE)
19545244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_EXECUTABLE;
19555244eac9SBenno Rice 	if (flags & PVO_WIRED)
19565244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
195759276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
19585244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
195932bc7846SPeter Grehan 	if (bootstrap)
196032bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
19614dba5df1SPeter Grehan 	if (flags & PVO_FAKE)
19624dba5df1SPeter Grehan 		pvo->pvo_vaddr |= PVO_FAKE;
19634dba5df1SPeter Grehan 
196459276937SPeter Grehan 	moea_pte_create(&pvo->pvo_pte, sr, va, pa | pte_lo);
19655244eac9SBenno Rice 
19665244eac9SBenno Rice 	/*
19675244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
19685244eac9SBenno Rice 	 * item.
19695244eac9SBenno Rice 	 */
19708207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
19718207b362SBenno Rice 		first = 1;
19725244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
19734dba5df1SPeter Grehan 
19745244eac9SBenno Rice 	if (pvo->pvo_pte.pte_lo & PVO_WIRED)
1975c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
1976c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
19775244eac9SBenno Rice 
19785244eac9SBenno Rice 	/*
19795244eac9SBenno Rice 	 * We hope this succeeds but it isn't required.
19805244eac9SBenno Rice 	 */
198159276937SPeter Grehan 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte);
19825244eac9SBenno Rice 	if (i >= 0) {
19835244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
19845244eac9SBenno Rice 	} else {
198559276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
198659276937SPeter Grehan 		moea_pte_overflow++;
19875244eac9SBenno Rice 	}
198859276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
19894dba5df1SPeter Grehan 
19905244eac9SBenno Rice 	return (first ? ENOENT : 0);
19915244eac9SBenno Rice }
19925244eac9SBenno Rice 
19935244eac9SBenno Rice static void
199459276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
19955244eac9SBenno Rice {
19965244eac9SBenno Rice 	struct	pte *pt;
19975244eac9SBenno Rice 
19985244eac9SBenno Rice 	/*
19995244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
20005244eac9SBenno Rice 	 * save the ref & cfg bits).
20015244eac9SBenno Rice 	 */
200259276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
20035244eac9SBenno Rice 	if (pt != NULL) {
200459276937SPeter Grehan 		moea_pte_unset(pt, &pvo->pvo_pte, pvo->pvo_vaddr);
20055244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
20065244eac9SBenno Rice 	} else {
200759276937SPeter Grehan 		moea_pte_overflow--;
20085244eac9SBenno Rice 	}
20095244eac9SBenno Rice 
20105244eac9SBenno Rice 	/*
20115244eac9SBenno Rice 	 * Update our statistics.
20125244eac9SBenno Rice 	 */
20135244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
20145244eac9SBenno Rice 	if (pvo->pvo_pte.pte_lo & PVO_WIRED)
20155244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
20165244eac9SBenno Rice 
20175244eac9SBenno Rice 	/*
20185244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
20195244eac9SBenno Rice 	 */
20204dba5df1SPeter Grehan 	if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) {
20215244eac9SBenno Rice 		struct	vm_page *pg;
20225244eac9SBenno Rice 
20238862232dSBenno Rice 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte_lo & PTE_RPGN);
20245244eac9SBenno Rice 		if (pg != NULL) {
202559276937SPeter Grehan 			moea_attr_save(pg, pvo->pvo_pte.pte_lo &
20265244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
20275244eac9SBenno Rice 		}
20285244eac9SBenno Rice 	}
20295244eac9SBenno Rice 
20305244eac9SBenno Rice 	/*
20315244eac9SBenno Rice 	 * Remove this PVO from the PV list.
20325244eac9SBenno Rice 	 */
20335244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_vlink);
20345244eac9SBenno Rice 
20355244eac9SBenno Rice 	/*
20365244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
20375244eac9SBenno Rice 	 * if we aren't going to reuse it.
20385244eac9SBenno Rice 	 */
20395244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
204049f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
204159276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
204259276937SPeter Grehan 		    moea_upvo_zone, pvo);
204359276937SPeter Grehan 	moea_pvo_entries--;
204459276937SPeter Grehan 	moea_pvo_remove_calls++;
20455244eac9SBenno Rice }
20465244eac9SBenno Rice 
20475244eac9SBenno Rice static __inline int
204859276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
20495244eac9SBenno Rice {
20505244eac9SBenno Rice 	int	pteidx;
20515244eac9SBenno Rice 
20525244eac9SBenno Rice 	/*
20535244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
20545244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
20555244eac9SBenno Rice 	 * noticing the HID bit.
20565244eac9SBenno Rice 	 */
20575244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
20585244eac9SBenno Rice 	if (pvo->pvo_pte.pte_hi & PTE_HID)
205959276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
20605244eac9SBenno Rice 
20615244eac9SBenno Rice 	return (pteidx);
20625244eac9SBenno Rice }
20635244eac9SBenno Rice 
20645244eac9SBenno Rice static struct pvo_entry *
206559276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
20665244eac9SBenno Rice {
20675244eac9SBenno Rice 	struct	pvo_entry *pvo;
20685244eac9SBenno Rice 	int	ptegidx;
20695244eac9SBenno Rice 	u_int	sr;
20705244eac9SBenno Rice 
20715244eac9SBenno Rice 	va &= ~ADDR_POFF;
20725244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
20735244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
20745244eac9SBenno Rice 
207559276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
207659276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20775244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
20785244eac9SBenno Rice 			if (pteidx_p)
207959276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2080f489bf21SAlan Cox 			break;
20815244eac9SBenno Rice 		}
20825244eac9SBenno Rice 	}
208359276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20845244eac9SBenno Rice 
2085f489bf21SAlan Cox 	return (pvo);
20865244eac9SBenno Rice }
20875244eac9SBenno Rice 
20885244eac9SBenno Rice static struct pte *
208959276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
20905244eac9SBenno Rice {
20915244eac9SBenno Rice 	struct	pte *pt;
20925244eac9SBenno Rice 
20935244eac9SBenno Rice 	/*
20945244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
20955244eac9SBenno Rice 	 */
20965244eac9SBenno Rice 	if (pteidx == -1) {
20975244eac9SBenno Rice 		int	ptegidx;
20985244eac9SBenno Rice 		u_int	sr;
20995244eac9SBenno Rice 
21005244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
21015244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
210259276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
21035244eac9SBenno Rice 	}
21045244eac9SBenno Rice 
210559276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
21065244eac9SBenno Rice 
21075244eac9SBenno Rice 	if ((pvo->pvo_pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
210859276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
21095244eac9SBenno Rice 		    "valid pte index", pvo);
21105244eac9SBenno Rice 	}
21115244eac9SBenno Rice 
21125244eac9SBenno Rice 	if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
211359276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
21145244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
21155244eac9SBenno Rice 	}
21165244eac9SBenno Rice 
21175244eac9SBenno Rice 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
21185244eac9SBenno Rice 		if ((pvo->pvo_pte.pte_hi & PTE_VALID) == 0) {
211959276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
212059276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
21215244eac9SBenno Rice 		}
21225244eac9SBenno Rice 
21235244eac9SBenno Rice 		if (((pt->pte_lo ^ pvo->pvo_pte.pte_lo) & ~(PTE_CHG|PTE_REF))
21245244eac9SBenno Rice 		    != 0) {
212559276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
212659276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
21275244eac9SBenno Rice 		}
21285244eac9SBenno Rice 
21295244eac9SBenno Rice 		return (pt);
21305244eac9SBenno Rice 	}
21315244eac9SBenno Rice 
21325244eac9SBenno Rice 	if (pvo->pvo_pte.pte_hi & PTE_VALID) {
213359276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
213459276937SPeter Grehan 		    "moea_pteg_table but valid in pvo", pvo, pt);
21355244eac9SBenno Rice 	}
21365244eac9SBenno Rice 
21375244eac9SBenno Rice 	return (NULL);
21385244eac9SBenno Rice }
21395244eac9SBenno Rice 
21405244eac9SBenno Rice /*
21415244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
21425244eac9SBenno Rice  */
21435244eac9SBenno Rice int
214459276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
21455244eac9SBenno Rice {
21465244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
21475244eac9SBenno Rice 	struct	pvo_entry *pvo;
21485244eac9SBenno Rice 	int	ptegidx, i, j;
21495244eac9SBenno Rice 	u_int	sr;
21505244eac9SBenno Rice 	struct	pteg *pteg;
21515244eac9SBenno Rice 	struct	pte *pt;
21525244eac9SBenno Rice 
215359276937SPeter Grehan 	moea_pte_spills++;
21545244eac9SBenno Rice 
2155d080d5fdSBenno Rice 	sr = mfsrin(addr);
21565244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
21575244eac9SBenno Rice 
21585244eac9SBenno Rice 	/*
21595244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
21605244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
21615244eac9SBenno Rice 	 */
216259276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
216359276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
21645244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
21655244eac9SBenno Rice 	i &= 7;
21665244eac9SBenno Rice 	pt = &pteg->pt[i];
21675244eac9SBenno Rice 
21685244eac9SBenno Rice 	source_pvo = NULL;
21695244eac9SBenno Rice 	victim_pvo = NULL;
217059276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21715244eac9SBenno Rice 		/*
21725244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
21735244eac9SBenno Rice 		 */
217459276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);
21755244eac9SBenno Rice 		if (source_pvo == NULL &&
217659276937SPeter Grehan 		    moea_pte_match(&pvo->pvo_pte, sr, addr,
21775244eac9SBenno Rice 		    pvo->pvo_pte.pte_hi & PTE_HID)) {
21785244eac9SBenno Rice 			/*
21795244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
21805244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
21815244eac9SBenno Rice 			 */
218259276937SPeter Grehan 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte);
21835244eac9SBenno Rice 
21845244eac9SBenno Rice 			if (j >= 0) {
21855244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
218659276937SPeter Grehan 				moea_pte_overflow--;
218759276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);
218859276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
21895244eac9SBenno Rice 				return (1);
21905244eac9SBenno Rice 			}
21915244eac9SBenno Rice 
21925244eac9SBenno Rice 			source_pvo = pvo;
21935244eac9SBenno Rice 
21945244eac9SBenno Rice 			if (victim_pvo != NULL)
21955244eac9SBenno Rice 				break;
21965244eac9SBenno Rice 		}
21975244eac9SBenno Rice 
21985244eac9SBenno Rice 		/*
21995244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
22005244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
22015244eac9SBenno Rice 		 */
22025244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
220359276937SPeter Grehan 		    moea_pte_compare(pt, &pvo->pvo_pte)) {
22045244eac9SBenno Rice 			victim_pvo = pvo;
22055244eac9SBenno Rice 			if (source_pvo != NULL)
22065244eac9SBenno Rice 				break;
22075244eac9SBenno Rice 		}
22085244eac9SBenno Rice 	}
22095244eac9SBenno Rice 
2210f489bf21SAlan Cox 	if (source_pvo == NULL) {
221159276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
22125244eac9SBenno Rice 		return (0);
2213f489bf21SAlan Cox 	}
22145244eac9SBenno Rice 
22155244eac9SBenno Rice 	if (victim_pvo == NULL) {
22165244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
221759276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
22185244eac9SBenno Rice 			    "entry", pt);
22195244eac9SBenno Rice 
22205244eac9SBenno Rice 		/*
22215244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
22225244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
22235244eac9SBenno Rice 		 */
222459276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
22255244eac9SBenno Rice 		    pvo_olink) {
222659276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);
22275244eac9SBenno Rice 			/*
22285244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
22295244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
22305244eac9SBenno Rice 			 */
223159276937SPeter Grehan 			if (moea_pte_compare(pt, &pvo->pvo_pte)) {
22325244eac9SBenno Rice 				victim_pvo = pvo;
22335244eac9SBenno Rice 				break;
22345244eac9SBenno Rice 			}
22355244eac9SBenno Rice 		}
22365244eac9SBenno Rice 
22375244eac9SBenno Rice 		if (victim_pvo == NULL)
223859276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
22395244eac9SBenno Rice 			    "entry", pt);
22405244eac9SBenno Rice 	}
22415244eac9SBenno Rice 
22425244eac9SBenno Rice 	/*
22435244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
22445244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
22455244eac9SBenno Rice 	 * contained in the TLB entry.
22465244eac9SBenno Rice 	 */
22475244eac9SBenno Rice 	source_pvo->pvo_pte.pte_hi &= ~PTE_HID;
22485244eac9SBenno Rice 
224959276937SPeter Grehan 	moea_pte_unset(pt, &victim_pvo->pvo_pte, victim_pvo->pvo_vaddr);
225059276937SPeter Grehan 	moea_pte_set(pt, &source_pvo->pvo_pte);
22515244eac9SBenno Rice 
22525244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
22535244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
225459276937SPeter Grehan 	moea_pte_replacements++;
22555244eac9SBenno Rice 
225659276937SPeter Grehan 	MOEA_PVO_CHECK(victim_pvo);
225759276937SPeter Grehan 	MOEA_PVO_CHECK(source_pvo);
22585244eac9SBenno Rice 
225959276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22605244eac9SBenno Rice 	return (1);
22615244eac9SBenno Rice }
22625244eac9SBenno Rice 
22635244eac9SBenno Rice static int
226459276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
22655244eac9SBenno Rice {
22665244eac9SBenno Rice 	struct	pte *pt;
22675244eac9SBenno Rice 	int	i;
22685244eac9SBenno Rice 
22695244eac9SBenno Rice 	/*
22705244eac9SBenno Rice 	 * First try primary hash.
22715244eac9SBenno Rice 	 */
227259276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22735244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22745244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
227559276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22765244eac9SBenno Rice 			return (i);
22775244eac9SBenno Rice 		}
22785244eac9SBenno Rice 	}
22795244eac9SBenno Rice 
22805244eac9SBenno Rice 	/*
22815244eac9SBenno Rice 	 * Now try secondary hash.
22825244eac9SBenno Rice 	 */
228359276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
22845244eac9SBenno Rice 	ptegidx++;
228559276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
22865244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
22875244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
228859276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
22895244eac9SBenno Rice 			return (i);
22905244eac9SBenno Rice 		}
22915244eac9SBenno Rice 	}
22925244eac9SBenno Rice 
229359276937SPeter Grehan 	panic("moea_pte_insert: overflow");
22945244eac9SBenno Rice 	return (-1);
22955244eac9SBenno Rice }
22965244eac9SBenno Rice 
22975244eac9SBenno Rice static boolean_t
229859276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
22995244eac9SBenno Rice {
23005244eac9SBenno Rice 	struct	pvo_entry *pvo;
23015244eac9SBenno Rice 	struct	pte *pt;
23025244eac9SBenno Rice 
23037b33c6efSPeter Grehan #if 0
230459276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
23055244eac9SBenno Rice 		return (TRUE);
23067b33c6efSPeter Grehan #endif
23075244eac9SBenno Rice 
23085244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
230959276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23105244eac9SBenno Rice 
23115244eac9SBenno Rice 		/*
23125244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
23135244eac9SBenno Rice 		 * success.
23145244eac9SBenno Rice 		 */
23155244eac9SBenno Rice 		if (pvo->pvo_pte.pte_lo & ptebit) {
231659276937SPeter Grehan 			moea_attr_save(m, ptebit);
231759276937SPeter Grehan 			MOEA_PVO_CHECK(pvo);	/* sanity check */
23185244eac9SBenno Rice 			return (TRUE);
23195244eac9SBenno Rice 		}
23205244eac9SBenno Rice 	}
23215244eac9SBenno Rice 
23225244eac9SBenno Rice 	/*
23235244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
23245244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
23255244eac9SBenno Rice 	 * the PTEs.
23265244eac9SBenno Rice 	 */
23275244eac9SBenno Rice 	SYNC();
23285244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
232959276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23305244eac9SBenno Rice 
23315244eac9SBenno Rice 		/*
23325244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
23335244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
23345244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
23355244eac9SBenno Rice 		 */
233659276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23375244eac9SBenno Rice 		if (pt != NULL) {
233859276937SPeter Grehan 			moea_pte_synch(pt, &pvo->pvo_pte);
23395244eac9SBenno Rice 			if (pvo->pvo_pte.pte_lo & ptebit) {
234059276937SPeter Grehan 				moea_attr_save(m, ptebit);
234159276937SPeter Grehan 				MOEA_PVO_CHECK(pvo);	/* sanity check */
23425244eac9SBenno Rice 				return (TRUE);
23435244eac9SBenno Rice 			}
23445244eac9SBenno Rice 		}
23455244eac9SBenno Rice 	}
23465244eac9SBenno Rice 
23474f7daed0SAndrew Gallatin 	return (FALSE);
23485244eac9SBenno Rice }
23495244eac9SBenno Rice 
235003b6e025SPeter Grehan static u_int
235159276937SPeter Grehan moea_clear_bit(vm_page_t m, int ptebit, int *origbit)
23525244eac9SBenno Rice {
235303b6e025SPeter Grehan 	u_int	count;
23545244eac9SBenno Rice 	struct	pvo_entry *pvo;
23555244eac9SBenno Rice 	struct	pte *pt;
23565244eac9SBenno Rice 	int	rv;
23575244eac9SBenno Rice 
23585244eac9SBenno Rice 	/*
23595244eac9SBenno Rice 	 * Clear the cached value.
23605244eac9SBenno Rice 	 */
236159276937SPeter Grehan 	rv = moea_attr_fetch(m);
236259276937SPeter Grehan 	moea_attr_clear(m, ptebit);
23635244eac9SBenno Rice 
23645244eac9SBenno Rice 	/*
23655244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
23665244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
23675244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
23685244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
23695244eac9SBenno Rice 	 * REF/CHG bits.
23705244eac9SBenno Rice 	 */
23715244eac9SBenno Rice 	SYNC();
23725244eac9SBenno Rice 
23735244eac9SBenno Rice 	/*
23745244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
23755244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
23765244eac9SBenno Rice 	 */
237703b6e025SPeter Grehan 	count = 0;
23785244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
237959276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
238059276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
23815244eac9SBenno Rice 		if (pt != NULL) {
238259276937SPeter Grehan 			moea_pte_synch(pt, &pvo->pvo_pte);
238303b6e025SPeter Grehan 			if (pvo->pvo_pte.pte_lo & ptebit) {
238403b6e025SPeter Grehan 				count++;
238559276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
23865244eac9SBenno Rice 			}
238703b6e025SPeter Grehan 		}
23885244eac9SBenno Rice 		rv |= pvo->pvo_pte.pte_lo;
23895244eac9SBenno Rice 		pvo->pvo_pte.pte_lo &= ~ptebit;
239059276937SPeter Grehan 		MOEA_PVO_CHECK(pvo);	/* sanity check */
23915244eac9SBenno Rice 	}
23925244eac9SBenno Rice 
239303b6e025SPeter Grehan 	if (origbit != NULL) {
239403b6e025SPeter Grehan 		*origbit = rv;
239503b6e025SPeter Grehan 	}
239603b6e025SPeter Grehan 
239703b6e025SPeter Grehan 	return (count);
2398bdf71f56SBenno Rice }
23998bbfa33aSBenno Rice 
24008bbfa33aSBenno Rice /*
240132bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
240232bc7846SPeter Grehan  */
240332bc7846SPeter Grehan static int
240459276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size)
240532bc7846SPeter Grehan {
240632bc7846SPeter Grehan 	u_int prot;
240732bc7846SPeter Grehan 	u_int32_t start;
240832bc7846SPeter Grehan 	u_int32_t end;
240932bc7846SPeter Grehan 	u_int32_t bat_ble;
241032bc7846SPeter Grehan 
241132bc7846SPeter Grehan 	/*
241232bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
241332bc7846SPeter Grehan 	 */
241432bc7846SPeter Grehan 	if (!battable[idx].batu & BAT_Vs)
241532bc7846SPeter Grehan 		return (EINVAL);
241632bc7846SPeter Grehan 
241732bc7846SPeter Grehan 	/*
241832bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
241932bc7846SPeter Grehan 	 * so it can function as an i/o page
242032bc7846SPeter Grehan 	 */
242132bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
242232bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
242332bc7846SPeter Grehan 		return (EPERM);
242432bc7846SPeter Grehan 
242532bc7846SPeter Grehan 	/*
242632bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
242732bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
242832bc7846SPeter Grehan 	 * not requiring masking)
242932bc7846SPeter Grehan 	 */
243032bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
243132bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
243232bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
243332bc7846SPeter Grehan 
243432bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
243532bc7846SPeter Grehan 		return (ERANGE);
243632bc7846SPeter Grehan 
243732bc7846SPeter Grehan 	return (0);
243832bc7846SPeter Grehan }
243932bc7846SPeter Grehan 
244059276937SPeter Grehan boolean_t
244159276937SPeter Grehan moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2442c0763d37SSuleiman Souhlal {
2443c0763d37SSuleiman Souhlal 	int i;
2444c0763d37SSuleiman Souhlal 
2445c0763d37SSuleiman Souhlal 	/*
2446c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2447c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2448c0763d37SSuleiman Souhlal 	 */
2449c0763d37SSuleiman Souhlal 
2450c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
245159276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2452c0763d37SSuleiman Souhlal 			return (0);
2453c0763d37SSuleiman Souhlal 
2454c0763d37SSuleiman Souhlal 	return (EFAULT);
2455c0763d37SSuleiman Souhlal }
245632bc7846SPeter Grehan 
245732bc7846SPeter Grehan /*
24588bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
24598bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
24608bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
24618bbfa33aSBenno Rice  * NOT real memory.
24628bbfa33aSBenno Rice  */
24638bbfa33aSBenno Rice void *
246459276937SPeter Grehan moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
24658bbfa33aSBenno Rice {
246632bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
246732bc7846SPeter Grehan 	int i;
24688bbfa33aSBenno Rice 
246932bc7846SPeter Grehan 	ppa = trunc_page(pa);
24708bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
24718bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
24728bbfa33aSBenno Rice 
24738bbfa33aSBenno Rice 	GIANT_REQUIRED;
24748bbfa33aSBenno Rice 
247532bc7846SPeter Grehan 	/*
247632bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
247732bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
247832bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
247932bc7846SPeter Grehan 	 */
248032bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
248159276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
248232bc7846SPeter Grehan 			return ((void *) pa);
248332bc7846SPeter Grehan 	}
248432bc7846SPeter Grehan 
2485e53f32acSAlan Cox 	va = kmem_alloc_nofault(kernel_map, size);
24868bbfa33aSBenno Rice 	if (!va)
248759276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
24888bbfa33aSBenno Rice 
24898bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
249059276937SPeter Grehan 		moea_kenter(mmu, tmpva, ppa);
24918bbfa33aSBenno Rice 		TLBIE(tmpva); /* XXX or should it be invalidate-all ? */
24928bbfa33aSBenno Rice 		size -= PAGE_SIZE;
24938bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
249432bc7846SPeter Grehan 		ppa += PAGE_SIZE;
24958bbfa33aSBenno Rice 	}
24968bbfa33aSBenno Rice 
24978bbfa33aSBenno Rice 	return ((void *)(va + offset));
24988bbfa33aSBenno Rice }
24998bbfa33aSBenno Rice 
25008bbfa33aSBenno Rice void
250159276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
25028bbfa33aSBenno Rice {
25038bbfa33aSBenno Rice 	vm_offset_t base, offset;
25048bbfa33aSBenno Rice 
250532bc7846SPeter Grehan 	/*
250632bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
250732bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
250832bc7846SPeter Grehan 	 */
250932bc7846SPeter Grehan 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
25108bbfa33aSBenno Rice 		base = trunc_page(va);
25118bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
25128bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
25138bbfa33aSBenno Rice 		kmem_free(kernel_map, base, size);
25148bbfa33aSBenno Rice 	}
251532bc7846SPeter Grehan }
2516