xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision f259d24fa5d3341710da5aa785968fd89e5fc3ed)
160727d8bSWarner Losh /*-
271e3c308SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-4-Clause
371e3c308SPedro F. Giffuni  *
45244eac9SBenno Rice  * Copyright (c) 2001 The NetBSD Foundation, Inc.
55244eac9SBenno Rice  * All rights reserved.
65244eac9SBenno Rice  *
75244eac9SBenno Rice  * This code is derived from software contributed to The NetBSD Foundation
85244eac9SBenno Rice  * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
95244eac9SBenno Rice  *
105244eac9SBenno Rice  * Redistribution and use in source and binary forms, with or without
115244eac9SBenno Rice  * modification, are permitted provided that the following conditions
125244eac9SBenno Rice  * are met:
135244eac9SBenno Rice  * 1. Redistributions of source code must retain the above copyright
145244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer.
155244eac9SBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
165244eac9SBenno Rice  *    notice, this list of conditions and the following disclaimer in the
175244eac9SBenno Rice  *    documentation and/or other materials provided with the distribution.
185244eac9SBenno Rice  *
195244eac9SBenno Rice  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
205244eac9SBenno Rice  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
215244eac9SBenno Rice  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225244eac9SBenno Rice  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
235244eac9SBenno Rice  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
245244eac9SBenno Rice  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255244eac9SBenno Rice  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265244eac9SBenno Rice  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
275244eac9SBenno Rice  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285244eac9SBenno Rice  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295244eac9SBenno Rice  * POSSIBILITY OF SUCH DAMAGE.
305244eac9SBenno Rice  */
3160727d8bSWarner Losh /*-
32f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
33f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 TooLs GmbH.
34f9bac91bSBenno Rice  * All rights reserved.
35f9bac91bSBenno Rice  *
36f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
37f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
38f9bac91bSBenno Rice  * are met:
39f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
40f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
41f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
42f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
43f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
44f9bac91bSBenno Rice  * 3. All advertising materials mentioning features or use of this software
45f9bac91bSBenno Rice  *    must display the following acknowledgement:
46f9bac91bSBenno Rice  *	This product includes software developed by TooLs GmbH.
47f9bac91bSBenno Rice  * 4. The name of TooLs GmbH may not be used to endorse or promote products
48f9bac91bSBenno Rice  *    derived from this software without specific prior written permission.
49f9bac91bSBenno Rice  *
50f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
51f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
56f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
57f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
58f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
59f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60f9bac91bSBenno Rice  *
61111c77dcSBenno Rice  * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
62f9bac91bSBenno Rice  */
6360727d8bSWarner Losh /*-
64f9bac91bSBenno Rice  * Copyright (C) 2001 Benno Rice.
65f9bac91bSBenno Rice  * All rights reserved.
66f9bac91bSBenno Rice  *
67f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
68f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
69f9bac91bSBenno Rice  * are met:
70f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
71f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
72f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
73f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
74f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
75f9bac91bSBenno Rice  *
76f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
77f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
78f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
79f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
80f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
81f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
82f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
83f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
84f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
85f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86f9bac91bSBenno Rice  */
87f9bac91bSBenno Rice 
888368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
898368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
90f9bac91bSBenno Rice 
915244eac9SBenno Rice /*
925244eac9SBenno Rice  * Manages physical address maps.
935244eac9SBenno Rice  *
945244eac9SBenno Rice  * Since the information managed by this module is also stored by the
955244eac9SBenno Rice  * logical address mapping module, this module may throw away valid virtual
965244eac9SBenno Rice  * to physical mappings at almost any time.  However, invalidations of
975244eac9SBenno Rice  * mappings must be done as requested.
985244eac9SBenno Rice  *
995244eac9SBenno Rice  * In order to cope with hardware architectures which make virtual to
1005244eac9SBenno Rice  * physical map invalidates expensive, this module may delay invalidate
1015244eac9SBenno Rice  * reduced protection operations until such time as they are actually
1025244eac9SBenno Rice  * necessary.  This module is given full information as to which processors
1035244eac9SBenno Rice  * are currently using which maps, and to when physical maps must be made
1045244eac9SBenno Rice  * correct.
1055244eac9SBenno Rice  */
1065244eac9SBenno Rice 
107ad7a226fSPeter Wemm #include "opt_kstack_pages.h"
108ad7a226fSPeter Wemm 
109f9bac91bSBenno Rice #include <sys/param.h>
1100b27d710SPeter Wemm #include <sys/kernel.h>
111bdb9ab0dSMark Johnston #include <sys/conf.h>
112c47dd3dbSAttilio Rao #include <sys/queue.h>
113c47dd3dbSAttilio Rao #include <sys/cpuset.h>
114bdb9ab0dSMark Johnston #include <sys/kerneldump.h>
1155244eac9SBenno Rice #include <sys/ktr.h>
11694e0b85eSMark Peek #include <sys/lock.h>
11743d3fc80SBrandon Bergren #include <sys/mman.h>
1185244eac9SBenno Rice #include <sys/msgbuf.h>
119f9bac91bSBenno Rice #include <sys/mutex.h>
1205244eac9SBenno Rice #include <sys/proc.h>
1213653f5cbSAlan Cox #include <sys/rwlock.h>
122c47dd3dbSAttilio Rao #include <sys/sched.h>
1235244eac9SBenno Rice #include <sys/sysctl.h>
1245244eac9SBenno Rice #include <sys/systm.h>
1255244eac9SBenno Rice #include <sys/vmmeter.h>
1265244eac9SBenno Rice 
1275244eac9SBenno Rice #include <dev/ofw/openfirm.h>
128f9bac91bSBenno Rice 
129f9bac91bSBenno Rice #include <vm/vm.h>
13043d3fc80SBrandon Bergren #include <vm/pmap.h>
131f9bac91bSBenno Rice #include <vm/vm_param.h>
132f9bac91bSBenno Rice #include <vm/vm_kern.h>
133f9bac91bSBenno Rice #include <vm/vm_page.h>
134f9bac91bSBenno Rice #include <vm/vm_map.h>
135f9bac91bSBenno Rice #include <vm/vm_object.h>
136f9bac91bSBenno Rice #include <vm/vm_extern.h>
13721943937SJeff Roberson #include <vm/vm_page.h>
13821943937SJeff Roberson #include <vm/vm_phys.h>
139f9bac91bSBenno Rice #include <vm/vm_pageout.h>
140378862a7SJeff Roberson #include <vm/uma.h>
141f9bac91bSBenno Rice 
1427c277971SPeter Grehan #include <machine/cpu.h>
143b40ce02aSNathan Whitehorn #include <machine/platform.h>
144d699b539SMark Peek #include <machine/bat.h>
1455244eac9SBenno Rice #include <machine/frame.h>
1465244eac9SBenno Rice #include <machine/md_var.h>
1475244eac9SBenno Rice #include <machine/psl.h>
148f9bac91bSBenno Rice #include <machine/pte.h>
14912640815SMarcel Moolenaar #include <machine/smp.h>
1505244eac9SBenno Rice #include <machine/sr.h>
15159276937SPeter Grehan #include <machine/mmuvar.h>
152258dbffeSNathan Whitehorn #include <machine/trap.h>
153f9bac91bSBenno Rice 
15459276937SPeter Grehan #define	MOEA_DEBUG
155f9bac91bSBenno Rice 
1565244eac9SBenno Rice #define TODO	panic("%s: not implemented", __func__);
157f9bac91bSBenno Rice 
1585244eac9SBenno Rice #define	VSID_MAKE(sr, hash)	((sr) | (((hash) & 0xfffff) << 4))
1595244eac9SBenno Rice #define	VSID_TO_SR(vsid)	((vsid) & 0xf)
1605244eac9SBenno Rice #define	VSID_TO_HASH(vsid)	(((vsid) >> 4) & 0xfffff)
1615244eac9SBenno Rice 
1627be655c2SBrandon Bergren /* Get physical address from PVO. */
1637be655c2SBrandon Bergren #define PVO_PADDR(pvo)		((pvo)->pvo_pte.pte.pte_lo & PTE_RPGN)
1647be655c2SBrandon Bergren 
1655244eac9SBenno Rice struct ofw_map {
1665244eac9SBenno Rice 	vm_offset_t	om_va;
1675244eac9SBenno Rice 	vm_size_t	om_len;
1685244eac9SBenno Rice 	vm_offset_t	om_pa;
1695244eac9SBenno Rice 	u_int		om_mode;
1705244eac9SBenno Rice };
171f9bac91bSBenno Rice 
172afd9cb6cSJustin Hibbits extern unsigned char _etext[];
173afd9cb6cSJustin Hibbits extern unsigned char _end[];
174afd9cb6cSJustin Hibbits 
1755244eac9SBenno Rice /*
1765244eac9SBenno Rice  * Map of physical memory regions.
1775244eac9SBenno Rice  */
17831c82d03SBenno Rice static struct	mem_region *regions;
17931c82d03SBenno Rice static struct	mem_region *pregions;
180c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
181c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
182aa39961eSBenno Rice static struct	ofw_map *translations;
1835244eac9SBenno Rice 
184f9bac91bSBenno Rice /*
185f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
186f489bf21SAlan Cox  */
18759276937SPeter Grehan struct mtx	moea_table_mutex;
188e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
189f489bf21SAlan Cox 
190e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
191e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
192e4f72b32SMarcel Moolenaar 
193f489bf21SAlan Cox /*
1945244eac9SBenno Rice  * PTEG data.
195f9bac91bSBenno Rice  */
19659276937SPeter Grehan static struct	pteg *moea_pteg_table;
19759276937SPeter Grehan u_int		moea_pteg_count;
19859276937SPeter Grehan u_int		moea_pteg_mask;
1995244eac9SBenno Rice 
2005244eac9SBenno Rice /*
2015244eac9SBenno Rice  * PVO data.
2025244eac9SBenno Rice  */
20359276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
20459276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
20559276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
2065244eac9SBenno Rice 
207cfedf924SAttilio Rao static struct rwlock_padalign pvh_global_lock;
2083653f5cbSAlan Cox 
20959276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
21059276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2115244eac9SBenno Rice 
2120d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
21359276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
21459276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2155244eac9SBenno Rice 
2165244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
21759276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2185244eac9SBenno Rice 
21959276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2205244eac9SBenno Rice 
2215244eac9SBenno Rice /*
2225244eac9SBenno Rice  * Statistics.
2235244eac9SBenno Rice  */
22459276937SPeter Grehan u_int	moea_pte_valid = 0;
22559276937SPeter Grehan u_int	moea_pte_overflow = 0;
22659276937SPeter Grehan u_int	moea_pte_replacements = 0;
22759276937SPeter Grehan u_int	moea_pvo_entries = 0;
22859276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
22959276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
23059276937SPeter Grehan u_int	moea_pte_spills = 0;
23159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2325244eac9SBenno Rice     0, "");
23359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
23459276937SPeter Grehan     &moea_pte_overflow, 0, "");
23559276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
23659276937SPeter Grehan     &moea_pte_replacements, 0, "");
23759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2385244eac9SBenno Rice     0, "");
23959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
24059276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
24159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
24259276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
24359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
24459276937SPeter Grehan     &moea_pte_spills, 0, "");
2455244eac9SBenno Rice 
2465244eac9SBenno Rice /*
24759276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2485244eac9SBenno Rice  */
24959276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2505244eac9SBenno Rice 
2515244eac9SBenno Rice /*
2525244eac9SBenno Rice  * PTE calls.
2535244eac9SBenno Rice  */
25459276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2555244eac9SBenno Rice 
2565244eac9SBenno Rice /*
2575244eac9SBenno Rice  * PVO calls.
2585244eac9SBenno Rice  */
25959276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2600936003eSJustin Hibbits 		    vm_offset_t, vm_paddr_t, u_int, int);
26159276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
26259276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
26359276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2645244eac9SBenno Rice 
2655244eac9SBenno Rice /*
2665244eac9SBenno Rice  * Utility routines.
2675244eac9SBenno Rice  */
26839ffa8c1SKonstantin Belousov static int		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
26939ffa8c1SKonstantin Belousov 			    vm_prot_t, u_int, int8_t);
2700936003eSJustin Hibbits static void		moea_syncicache(vm_paddr_t, vm_size_t);
27159276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
272ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
27345b69dd6SJustin Hibbits static void		moea_kremove(vm_offset_t);
27459276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
27559276937SPeter Grehan 
27659276937SPeter Grehan /*
27759276937SPeter Grehan  * Kernel MMU interface
27859276937SPeter Grehan  */
27945b69dd6SJustin Hibbits void moea_clear_modify(vm_page_t);
28045b69dd6SJustin Hibbits void moea_copy_page(vm_page_t, vm_page_t);
28145b69dd6SJustin Hibbits void moea_copy_pages(vm_page_t *ma, vm_offset_t a_offset,
282e8a4a618SKonstantin Belousov     vm_page_t *mb, vm_offset_t b_offset, int xfersize);
28345b69dd6SJustin Hibbits int moea_enter(pmap_t, vm_offset_t, vm_page_t, vm_prot_t, u_int,
28439ffa8c1SKonstantin Belousov     int8_t);
28545b69dd6SJustin Hibbits void moea_enter_object(pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
286ce142d9eSAlan Cox     vm_prot_t);
28745b69dd6SJustin Hibbits void moea_enter_quick(pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
28845b69dd6SJustin Hibbits vm_paddr_t moea_extract(pmap_t, vm_offset_t);
28945b69dd6SJustin Hibbits vm_page_t moea_extract_and_hold(pmap_t, vm_offset_t, vm_prot_t);
29045b69dd6SJustin Hibbits void moea_init(void);
29145b69dd6SJustin Hibbits boolean_t moea_is_modified(vm_page_t);
29245b69dd6SJustin Hibbits boolean_t moea_is_prefaultable(pmap_t, vm_offset_t);
29345b69dd6SJustin Hibbits boolean_t moea_is_referenced(vm_page_t);
29445b69dd6SJustin Hibbits int moea_ts_referenced(vm_page_t);
29545b69dd6SJustin Hibbits vm_offset_t moea_map(vm_offset_t *, vm_paddr_t, vm_paddr_t, int);
29643d3fc80SBrandon Bergren static int moea_mincore(pmap_t, vm_offset_t, vm_paddr_t *);
29745b69dd6SJustin Hibbits boolean_t moea_page_exists_quick(pmap_t, vm_page_t);
29845b69dd6SJustin Hibbits void moea_page_init(vm_page_t);
29945b69dd6SJustin Hibbits int moea_page_wired_mappings(vm_page_t);
30045b69dd6SJustin Hibbits int moea_pinit(pmap_t);
30145b69dd6SJustin Hibbits void moea_pinit0(pmap_t);
30245b69dd6SJustin Hibbits void moea_protect(pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
30345b69dd6SJustin Hibbits void moea_qenter(vm_offset_t, vm_page_t *, int);
30445b69dd6SJustin Hibbits void moea_qremove(vm_offset_t, int);
30545b69dd6SJustin Hibbits void moea_release(pmap_t);
30645b69dd6SJustin Hibbits void moea_remove(pmap_t, vm_offset_t, vm_offset_t);
30745b69dd6SJustin Hibbits void moea_remove_all(vm_page_t);
30845b69dd6SJustin Hibbits void moea_remove_write(vm_page_t);
30945b69dd6SJustin Hibbits void moea_unwire(pmap_t, vm_offset_t, vm_offset_t);
31045b69dd6SJustin Hibbits void moea_zero_page(vm_page_t);
31145b69dd6SJustin Hibbits void moea_zero_page_area(vm_page_t, int, int);
31245b69dd6SJustin Hibbits void moea_activate(struct thread *);
31345b69dd6SJustin Hibbits void moea_deactivate(struct thread *);
31445b69dd6SJustin Hibbits void moea_cpu_bootstrap(int);
31545b69dd6SJustin Hibbits void moea_bootstrap(vm_offset_t, vm_offset_t);
31645b69dd6SJustin Hibbits void *moea_mapdev(vm_paddr_t, vm_size_t);
31745b69dd6SJustin Hibbits void *moea_mapdev_attr(vm_paddr_t, vm_size_t, vm_memattr_t);
31845b69dd6SJustin Hibbits void moea_unmapdev(vm_offset_t, vm_size_t);
31945b69dd6SJustin Hibbits vm_paddr_t moea_kextract(vm_offset_t);
32045b69dd6SJustin Hibbits void moea_kenter_attr(vm_offset_t, vm_paddr_t, vm_memattr_t);
32145b69dd6SJustin Hibbits void moea_kenter(vm_offset_t, vm_paddr_t);
32245b69dd6SJustin Hibbits void moea_page_set_memattr(vm_page_t m, vm_memattr_t ma);
32345b69dd6SJustin Hibbits boolean_t moea_dev_direct_mapped(vm_paddr_t, vm_size_t);
32445b69dd6SJustin Hibbits static void moea_sync_icache(pmap_t, vm_offset_t, vm_size_t);
32545b69dd6SJustin Hibbits void moea_dumpsys_map(vm_paddr_t pa, size_t sz, void **va);
32645b69dd6SJustin Hibbits void moea_scan_init(void);
32745b69dd6SJustin Hibbits vm_offset_t moea_quick_enter_page(vm_page_t m);
32845b69dd6SJustin Hibbits void moea_quick_remove_page(vm_offset_t addr);
32945b69dd6SJustin Hibbits boolean_t moea_page_is_mapped(vm_page_t m);
3308dc8feb5SJason A. Harmening bool moea_ps_enabled(pmap_t pmap);
33145b69dd6SJustin Hibbits static int moea_map_user_ptr(pmap_t pm,
33204329fa7SNathan Whitehorn     volatile const void *uaddr, void **kaddr, size_t ulen, size_t *klen);
33345b69dd6SJustin Hibbits static int moea_decode_kernel_ptr(vm_offset_t addr,
334eb1baf72SNathan Whitehorn     int *is_user, vm_offset_t *decoded_addr);
33504329fa7SNathan Whitehorn 
33645b69dd6SJustin Hibbits static struct pmap_funcs moea_methods = {
33745b69dd6SJustin Hibbits 	.clear_modify = moea_clear_modify,
33845b69dd6SJustin Hibbits 	.copy_page = moea_copy_page,
33945b69dd6SJustin Hibbits 	.copy_pages = moea_copy_pages,
34045b69dd6SJustin Hibbits 	.enter = moea_enter,
34145b69dd6SJustin Hibbits 	.enter_object = moea_enter_object,
34245b69dd6SJustin Hibbits 	.enter_quick = moea_enter_quick,
34345b69dd6SJustin Hibbits 	.extract = moea_extract,
34445b69dd6SJustin Hibbits 	.extract_and_hold = moea_extract_and_hold,
34545b69dd6SJustin Hibbits 	.init = moea_init,
34645b69dd6SJustin Hibbits 	.is_modified = moea_is_modified,
34745b69dd6SJustin Hibbits 	.is_prefaultable = moea_is_prefaultable,
34845b69dd6SJustin Hibbits 	.is_referenced = moea_is_referenced,
34945b69dd6SJustin Hibbits 	.ts_referenced = moea_ts_referenced,
35045b69dd6SJustin Hibbits 	.map =      		moea_map,
35145b69dd6SJustin Hibbits 	.page_exists_quick = moea_page_exists_quick,
35245b69dd6SJustin Hibbits 	.page_init = moea_page_init,
35345b69dd6SJustin Hibbits 	.page_wired_mappings = moea_page_wired_mappings,
35445b69dd6SJustin Hibbits 	.pinit = moea_pinit,
35545b69dd6SJustin Hibbits 	.pinit0 = moea_pinit0,
35645b69dd6SJustin Hibbits 	.protect = moea_protect,
35745b69dd6SJustin Hibbits 	.qenter = moea_qenter,
35845b69dd6SJustin Hibbits 	.qremove = moea_qremove,
35945b69dd6SJustin Hibbits 	.release = moea_release,
36045b69dd6SJustin Hibbits 	.remove = moea_remove,
36145b69dd6SJustin Hibbits 	.remove_all = moea_remove_all,
36243d3fc80SBrandon Bergren 	.mincore = moea_mincore,
36345b69dd6SJustin Hibbits 	.remove_write = moea_remove_write,
36445b69dd6SJustin Hibbits 	.sync_icache = moea_sync_icache,
36545b69dd6SJustin Hibbits 	.unwire = moea_unwire,
36645b69dd6SJustin Hibbits 	.zero_page =        	moea_zero_page,
36745b69dd6SJustin Hibbits 	.zero_page_area = moea_zero_page_area,
36845b69dd6SJustin Hibbits 	.activate = moea_activate,
36945b69dd6SJustin Hibbits 	.deactivate =       	moea_deactivate,
37045b69dd6SJustin Hibbits 	.page_set_memattr = moea_page_set_memattr,
37145b69dd6SJustin Hibbits 	.quick_enter_page =  moea_quick_enter_page,
37245b69dd6SJustin Hibbits 	.quick_remove_page =  moea_quick_remove_page,
37345b69dd6SJustin Hibbits 	.page_is_mapped = moea_page_is_mapped,
3748dc8feb5SJason A. Harmening 	.ps_enabled = moea_ps_enabled,
37559276937SPeter Grehan 
37659276937SPeter Grehan 	/* Internal interfaces */
37745b69dd6SJustin Hibbits 	.bootstrap =        	moea_bootstrap,
37845b69dd6SJustin Hibbits 	.cpu_bootstrap =    	moea_cpu_bootstrap,
37945b69dd6SJustin Hibbits 	.mapdev_attr = moea_mapdev_attr,
38045b69dd6SJustin Hibbits 	.mapdev = moea_mapdev,
38145b69dd6SJustin Hibbits 	.unmapdev = moea_unmapdev,
38245b69dd6SJustin Hibbits 	.kextract = moea_kextract,
38345b69dd6SJustin Hibbits 	.kenter = moea_kenter,
38445b69dd6SJustin Hibbits 	.kenter_attr = moea_kenter_attr,
38545b69dd6SJustin Hibbits 	.dev_direct_mapped = moea_dev_direct_mapped,
38645b69dd6SJustin Hibbits 	.dumpsys_pa_init = moea_scan_init,
38745b69dd6SJustin Hibbits 	.dumpsys_map_chunk = moea_dumpsys_map,
38845b69dd6SJustin Hibbits 	.map_user_ptr = moea_map_user_ptr,
38945b69dd6SJustin Hibbits 	.decode_kernel_ptr =  moea_decode_kernel_ptr,
39059276937SPeter Grehan };
39159276937SPeter Grehan 
39245b69dd6SJustin Hibbits MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods);
39333529b98SPeter Grehan 
394c1f4123bSNathan Whitehorn static __inline uint32_t
3950936003eSJustin Hibbits moea_calc_wimg(vm_paddr_t pa, vm_memattr_t ma)
396c1f4123bSNathan Whitehorn {
397c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
398c1f4123bSNathan Whitehorn 	int i;
399c1f4123bSNathan Whitehorn 
400c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
401c1f4123bSNathan Whitehorn 		switch (ma) {
402c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
403c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
40454ac2713SJustin Hibbits 		case VM_MEMATTR_CACHEABLE:
40554ac2713SJustin Hibbits 			return (PTE_M);
406c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
407c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
408c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
409c1f4123bSNathan Whitehorn 			return (PTE_I);
410c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
411c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
412c1f4123bSNathan Whitehorn 		}
413c1f4123bSNathan Whitehorn 	}
414c1f4123bSNathan Whitehorn 
415c1f4123bSNathan Whitehorn 	/*
416c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
417c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
418c1f4123bSNathan Whitehorn 	 */
419c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
420c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
421c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
422c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
423c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
424c1f4123bSNathan Whitehorn 			break;
425c1f4123bSNathan Whitehorn 		}
426c1f4123bSNathan Whitehorn 	}
427c1f4123bSNathan Whitehorn 
428c1f4123bSNathan Whitehorn 	return pte_lo;
429c1f4123bSNathan Whitehorn }
43059276937SPeter Grehan 
431f10baa40SBrandon Bergren /*
432f10baa40SBrandon Bergren  * Translate OFW translations into VM attributes.
433f10baa40SBrandon Bergren  */
434f10baa40SBrandon Bergren static __inline vm_memattr_t
435f10baa40SBrandon Bergren moea_bootstrap_convert_wimg(uint32_t mode)
436f10baa40SBrandon Bergren {
437f10baa40SBrandon Bergren 
438f10baa40SBrandon Bergren 	switch (mode) {
439f10baa40SBrandon Bergren 	case (PTE_I | PTE_G):
440f10baa40SBrandon Bergren 		/* PCI device memory */
441f10baa40SBrandon Bergren 		return VM_MEMATTR_UNCACHEABLE;
442f10baa40SBrandon Bergren 	case (PTE_M):
443f10baa40SBrandon Bergren 		/* Explicitly coherent */
444f10baa40SBrandon Bergren 		return VM_MEMATTR_CACHEABLE;
445f10baa40SBrandon Bergren 	case 0: /* Default claim */
446f10baa40SBrandon Bergren 	case 2: /* Alternate PP bits set by OF for the original payload */
447f10baa40SBrandon Bergren 		/* "Normal" memory. */
448f10baa40SBrandon Bergren 		return VM_MEMATTR_DEFAULT;
449f10baa40SBrandon Bergren 
450f10baa40SBrandon Bergren 	default:
451f10baa40SBrandon Bergren 		/* Err on the side of caution for unknowns */
452f10baa40SBrandon Bergren 		/* XXX should we panic instead? */
453f10baa40SBrandon Bergren 		return VM_MEMATTR_UNCACHEABLE;
454f10baa40SBrandon Bergren 	}
455f10baa40SBrandon Bergren }
456f10baa40SBrandon Bergren 
457e4f72b32SMarcel Moolenaar static void
458e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
459e4f72b32SMarcel Moolenaar {
460e4f72b32SMarcel Moolenaar 
461e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
46294363f53SNathan Whitehorn 	__asm __volatile("ptesync");
463e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
46494363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
465e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
466e4f72b32SMarcel Moolenaar }
467e4f72b32SMarcel Moolenaar 
468e4f72b32SMarcel Moolenaar static void
469e4f72b32SMarcel Moolenaar tlbia(void)
470e4f72b32SMarcel Moolenaar {
471e4f72b32SMarcel Moolenaar 	vm_offset_t va;
472e4f72b32SMarcel Moolenaar 
473e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
474e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
475e4f72b32SMarcel Moolenaar 		powerpc_sync();
476e4f72b32SMarcel Moolenaar 	}
477e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
478e4f72b32SMarcel Moolenaar 	powerpc_sync();
479e4f72b32SMarcel Moolenaar }
4805244eac9SBenno Rice 
4815244eac9SBenno Rice static __inline int
4825244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4835244eac9SBenno Rice {
4845244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4855244eac9SBenno Rice }
4865244eac9SBenno Rice 
4875244eac9SBenno Rice static __inline u_int
4885244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4895244eac9SBenno Rice {
4905244eac9SBenno Rice 	u_int hash;
4915244eac9SBenno Rice 
4925244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4935244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
49459276937SPeter Grehan 	return (hash & moea_pteg_mask);
4955244eac9SBenno Rice }
4965244eac9SBenno Rice 
4975244eac9SBenno Rice static __inline struct pvo_head *
4985244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
499f9bac91bSBenno Rice {
500f9bac91bSBenno Rice 
5015244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
502f9bac91bSBenno Rice }
503f9bac91bSBenno Rice 
504f9bac91bSBenno Rice static __inline void
50559276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
506f9bac91bSBenno Rice {
507f9bac91bSBenno Rice 
5083653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
5095244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
5105244eac9SBenno Rice }
5115244eac9SBenno Rice 
5125244eac9SBenno Rice static __inline int
51359276937SPeter Grehan moea_attr_fetch(vm_page_t m)
5145244eac9SBenno Rice {
5155244eac9SBenno Rice 
5165244eac9SBenno Rice 	return (m->md.mdpg_attrs);
517f9bac91bSBenno Rice }
518f9bac91bSBenno Rice 
519f9bac91bSBenno Rice static __inline void
52059276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
521f9bac91bSBenno Rice {
522f9bac91bSBenno Rice 
5233653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
5245244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
525f9bac91bSBenno Rice }
526f9bac91bSBenno Rice 
527f9bac91bSBenno Rice static __inline int
52859276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
529f9bac91bSBenno Rice {
5305244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
5315244eac9SBenno Rice 		return (1);
532f9bac91bSBenno Rice 
5335244eac9SBenno Rice 	return (0);
534f9bac91bSBenno Rice }
535f9bac91bSBenno Rice 
536f9bac91bSBenno Rice static __inline int
53759276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
538f9bac91bSBenno Rice {
5395244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
5405244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5415244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
542f9bac91bSBenno Rice }
543f9bac91bSBenno Rice 
5445244eac9SBenno Rice static __inline void
54559276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
546f9bac91bSBenno Rice {
547d644a0b7SAlan Cox 
548d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
549d644a0b7SAlan Cox 
550f9bac91bSBenno Rice 	/*
5515244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5525244eac9SBenno Rice 	 * set when the real pte is set in memory.
553f9bac91bSBenno Rice 	 *
554f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
555f9bac91bSBenno Rice 	 */
5565244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5575244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5585244eac9SBenno Rice 	pt->pte_lo = pte_lo;
559f9bac91bSBenno Rice }
560f9bac91bSBenno Rice 
5615244eac9SBenno Rice static __inline void
56259276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
563f9bac91bSBenno Rice {
564f9bac91bSBenno Rice 
565d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5665244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
567f9bac91bSBenno Rice }
568f9bac91bSBenno Rice 
5695244eac9SBenno Rice static __inline void
57059276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
571f9bac91bSBenno Rice {
5725244eac9SBenno Rice 
573d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
574d644a0b7SAlan Cox 
5755244eac9SBenno Rice 	/*
5765244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5775244eac9SBenno Rice 	 */
5785244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
579e4f72b32SMarcel Moolenaar 	tlbie(va);
5805244eac9SBenno Rice }
5815244eac9SBenno Rice 
5825244eac9SBenno Rice static __inline void
58359276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5845244eac9SBenno Rice {
5855244eac9SBenno Rice 
586d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5875244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5885244eac9SBenno Rice 
5895244eac9SBenno Rice 	/*
5905244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
591804d1cc1SJustin Hibbits 	 * Note that the REF/CHG bits are from pvo_pt and thus should have
5925244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5935244eac9SBenno Rice 	 */
5945244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
595e4f72b32SMarcel Moolenaar 	powerpc_sync();
5965244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
597e4f72b32SMarcel Moolenaar 	powerpc_sync();
59859276937SPeter Grehan 	moea_pte_valid++;
5995244eac9SBenno Rice }
6005244eac9SBenno Rice 
6015244eac9SBenno Rice static __inline void
60259276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
6035244eac9SBenno Rice {
6045244eac9SBenno Rice 
605d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
6065244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
6075244eac9SBenno Rice 
6085244eac9SBenno Rice 	/*
6095244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
6105244eac9SBenno Rice 	 */
611e4f72b32SMarcel Moolenaar 	powerpc_sync();
6125244eac9SBenno Rice 
6135244eac9SBenno Rice 	/*
6145244eac9SBenno Rice 	 * Invalidate the pte.
6155244eac9SBenno Rice 	 */
6165244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
6175244eac9SBenno Rice 
618e4f72b32SMarcel Moolenaar 	tlbie(va);
6195244eac9SBenno Rice 
6205244eac9SBenno Rice 	/*
6215244eac9SBenno Rice 	 * Save the reg & chg bits.
6225244eac9SBenno Rice 	 */
62359276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
62459276937SPeter Grehan 	moea_pte_valid--;
6255244eac9SBenno Rice }
6265244eac9SBenno Rice 
6275244eac9SBenno Rice static __inline void
62859276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
6295244eac9SBenno Rice {
6305244eac9SBenno Rice 
6315244eac9SBenno Rice 	/*
6325244eac9SBenno Rice 	 * Invalidate the PTE
6335244eac9SBenno Rice 	 */
63459276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
63559276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
636f9bac91bSBenno Rice }
637f9bac91bSBenno Rice 
638f9bac91bSBenno Rice /*
6395244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
640f9bac91bSBenno Rice  */
6415244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
6425244eac9SBenno Rice 
6435244eac9SBenno Rice static int
6445244eac9SBenno Rice om_cmp(const void *a, const void *b)
6455244eac9SBenno Rice {
6465244eac9SBenno Rice 	const struct	ofw_map *mapa;
6475244eac9SBenno Rice 	const struct	ofw_map *mapb;
6485244eac9SBenno Rice 
6495244eac9SBenno Rice 	mapa = a;
6505244eac9SBenno Rice 	mapb = b;
6515244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6525244eac9SBenno Rice 		return (-1);
6535244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6545244eac9SBenno Rice 		return (1);
6555244eac9SBenno Rice 	else
6565244eac9SBenno Rice 		return (0);
657f9bac91bSBenno Rice }
658f9bac91bSBenno Rice 
659f9bac91bSBenno Rice void
66045b69dd6SJustin Hibbits moea_cpu_bootstrap(int ap)
66112640815SMarcel Moolenaar {
66212640815SMarcel Moolenaar 	u_int sdr;
66312640815SMarcel Moolenaar 	int i;
66412640815SMarcel Moolenaar 
66512640815SMarcel Moolenaar 	if (ap) {
666e4f72b32SMarcel Moolenaar 		powerpc_sync();
66712640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
66812640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
66912640815SMarcel Moolenaar 		isync();
67012640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
67112640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
67212640815SMarcel Moolenaar 		isync();
67312640815SMarcel Moolenaar 	}
67412640815SMarcel Moolenaar 
67501d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
67601d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
67712640815SMarcel Moolenaar 	isync();
67812640815SMarcel Moolenaar 
67901d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
68001d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
68101d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
68201d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
68301d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
68412640815SMarcel Moolenaar 	isync();
68512640815SMarcel Moolenaar 
68612640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
687fe3b4685SNathan Whitehorn 		mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
688e4f72b32SMarcel Moolenaar 	powerpc_sync();
68912640815SMarcel Moolenaar 
69012640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
69112640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
69212640815SMarcel Moolenaar 	isync();
69312640815SMarcel Moolenaar 
69486c1fb4cSMarcel Moolenaar 	tlbia();
69512640815SMarcel Moolenaar }
69612640815SMarcel Moolenaar 
69712640815SMarcel Moolenaar void
69845b69dd6SJustin Hibbits moea_bootstrap(vm_offset_t kernelstart, vm_offset_t kernelend)
699f9bac91bSBenno Rice {
70031c82d03SBenno Rice 	ihandle_t	mmui;
7015244eac9SBenno Rice 	phandle_t	chosen, mmu;
7025244eac9SBenno Rice 	int		sz;
7035244eac9SBenno Rice 	int		i, j;
704e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
7055244eac9SBenno Rice 	vm_offset_t	pa, va, off;
70650c202c5SJeff Roberson 	void		*dpcpu;
7070d290675SBenno Rice 
7080d290675SBenno Rice 	/*
7090d290675SBenno Rice 	 * Map PCI memory space.
7100d290675SBenno Rice 	 */
7110d290675SBenno Rice 	battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
7120d290675SBenno Rice 	battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
7130d290675SBenno Rice 
7140d290675SBenno Rice 	battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
7150d290675SBenno Rice 	battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
7160d290675SBenno Rice 
7170d290675SBenno Rice 	battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
7180d290675SBenno Rice 	battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
7190d290675SBenno Rice 
7200d290675SBenno Rice 	battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
7210d290675SBenno Rice 	battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
7220d290675SBenno Rice 
723f10baa40SBrandon Bergren 	powerpc_sync();
7240d290675SBenno Rice 
7250d290675SBenno Rice 	/* map pci space */
72612640815SMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
72712640815SMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
72812640815SMarcel Moolenaar 	isync();
729f9bac91bSBenno Rice 
7301c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7311c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7321c96bdd1SNathan Whitehorn 
73331c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
73459276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
73531c82d03SBenno Rice 
73631c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
73732bc7846SPeter Grehan 		vm_offset_t pa;
73832bc7846SPeter Grehan 		vm_offset_t end;
73932bc7846SPeter Grehan 
74031c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
74131c82d03SBenno Rice 			pregions[i].mr_start,
74231c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
74331c82d03SBenno Rice 			pregions[i].mr_size);
74432bc7846SPeter Grehan 		/*
74532bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
74632bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
74732bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
74832bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
74932bc7846SPeter Grehan 		 * a while yet.
75032bc7846SPeter Grehan 		 */
75132bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
75232bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
75332bc7846SPeter Grehan 		do {
75432bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
75532bc7846SPeter Grehan 
75632bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
75732bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
75832bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
75932bc7846SPeter Grehan 		} while (pa < end);
76031c82d03SBenno Rice 	}
76131c82d03SBenno Rice 
76221943937SJeff Roberson 	if (PHYS_AVAIL_ENTRIES < regions_sz)
76359276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
76497f7cde4SNathan Whitehorn 
7655244eac9SBenno Rice 	phys_avail_count = 0;
766d2c1f576SBenno Rice 	physsz = 0;
767b0c21309SPeter Grehan 	hwphyssz = 0;
768b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
76931c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7705244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7715244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7725244eac9SBenno Rice 		    regions[i].mr_size);
773e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
774e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
775e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
776e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
777e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
778e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
779e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
780e2f6d6e2SPeter Grehan 				phys_avail_count++;
781e2f6d6e2SPeter Grehan 			}
782e2f6d6e2SPeter Grehan 			break;
783e2f6d6e2SPeter Grehan 		}
7845244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7855244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7865244eac9SBenno Rice 		phys_avail_count++;
787d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
788f9bac91bSBenno Rice 	}
789e347e23bSNathan Whitehorn 
790e347e23bSNathan Whitehorn 	/* Check for overlap with the kernel and exception vectors */
791e347e23bSNathan Whitehorn 	for (j = 0; j < 2*phys_avail_count; j+=2) {
792e347e23bSNathan Whitehorn 		if (phys_avail[j] < EXC_LAST)
793e347e23bSNathan Whitehorn 			phys_avail[j] += EXC_LAST;
794e347e23bSNathan Whitehorn 
795e347e23bSNathan Whitehorn 		if (kernelstart >= phys_avail[j] &&
796e347e23bSNathan Whitehorn 		    kernelstart < phys_avail[j+1]) {
797e347e23bSNathan Whitehorn 			if (kernelend < phys_avail[j+1]) {
798e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] =
799e347e23bSNathan Whitehorn 				    (kernelend & ~PAGE_MASK) + PAGE_SIZE;
800e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
801e347e23bSNathan Whitehorn 				    phys_avail[j+1];
802e347e23bSNathan Whitehorn 				phys_avail_count++;
803e347e23bSNathan Whitehorn 			}
804e347e23bSNathan Whitehorn 
805e347e23bSNathan Whitehorn 			phys_avail[j+1] = kernelstart & ~PAGE_MASK;
806e347e23bSNathan Whitehorn 		}
807e347e23bSNathan Whitehorn 
808e347e23bSNathan Whitehorn 		if (kernelend >= phys_avail[j] &&
809e347e23bSNathan Whitehorn 		    kernelend < phys_avail[j+1]) {
810e347e23bSNathan Whitehorn 			if (kernelstart > phys_avail[j]) {
811e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] = phys_avail[j];
812e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
813e347e23bSNathan Whitehorn 				    kernelstart & ~PAGE_MASK;
814e347e23bSNathan Whitehorn 				phys_avail_count++;
815e347e23bSNathan Whitehorn 			}
816e347e23bSNathan Whitehorn 
817e347e23bSNathan Whitehorn 			phys_avail[j] = (kernelend & ~PAGE_MASK) + PAGE_SIZE;
818e347e23bSNathan Whitehorn 		}
819e347e23bSNathan Whitehorn 	}
820e347e23bSNathan Whitehorn 
821d2c1f576SBenno Rice 	physmem = btoc(physsz);
822f9bac91bSBenno Rice 
823f9bac91bSBenno Rice 	/*
8245244eac9SBenno Rice 	 * Allocate PTEG table.
825f9bac91bSBenno Rice 	 */
8265244eac9SBenno Rice #ifdef PTEGCOUNT
82759276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
8285244eac9SBenno Rice #else
82959276937SPeter Grehan 	moea_pteg_count = 0x1000;
830f9bac91bSBenno Rice 
83159276937SPeter Grehan 	while (moea_pteg_count < physmem)
83259276937SPeter Grehan 		moea_pteg_count <<= 1;
833f9bac91bSBenno Rice 
83459276937SPeter Grehan 	moea_pteg_count >>= 1;
8355244eac9SBenno Rice #endif /* PTEGCOUNT */
836f9bac91bSBenno Rice 
83759276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
83859276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
8395244eac9SBenno Rice 	    size);
84059276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
84159276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
84259276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
84359276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
844f9bac91bSBenno Rice 
8455244eac9SBenno Rice 	/*
846864bc520SBenno Rice 	 * Allocate pv/overflow lists.
8475244eac9SBenno Rice 	 */
84859276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
84959276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
8505244eac9SBenno Rice 	    PAGE_SIZE);
85159276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
85259276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
85359276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
8545244eac9SBenno Rice 
8555244eac9SBenno Rice 	/*
856f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
857f489bf21SAlan Cox 	 * tables.
858f489bf21SAlan Cox 	 */
859d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
860d644a0b7SAlan Cox 	    MTX_RECURSE);
861e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
862f489bf21SAlan Cox 
863e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
864e4f72b32SMarcel Moolenaar 
865f489bf21SAlan Cox 	/*
8665244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8675244eac9SBenno Rice 	 */
86859276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8690d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
87059276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8715244eac9SBenno Rice 
8725244eac9SBenno Rice 	/*
8735244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8745244eac9SBenno Rice 	 */
87559276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8765244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
87759276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8785244eac9SBenno Rice 
8795244eac9SBenno Rice 	/*
880fe3b4685SNathan Whitehorn 	 * Initialize the kernel pmap (which is statically allocated).
8815244eac9SBenno Rice 	 */
882fe3b4685SNathan Whitehorn 	PMAP_LOCK_INIT(kernel_pmap);
883fe3b4685SNathan Whitehorn 	for (i = 0; i < 16; i++)
884fe3b4685SNathan Whitehorn 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
885c47dd3dbSAttilio Rao 	CPU_FILL(&kernel_pmap->pm_active);
886ccc4a5c7SNathan Whitehorn 	RB_INIT(&kernel_pmap->pmap_pvo);
887fe3b4685SNathan Whitehorn 
888fe3b4685SNathan Whitehorn  	/*
8893653f5cbSAlan Cox 	 * Initialize the global pv list lock.
8903653f5cbSAlan Cox 	 */
8913653f5cbSAlan Cox 	rw_init(&pvh_global_lock, "pmap pv global");
8923653f5cbSAlan Cox 
8933653f5cbSAlan Cox 	/*
894fe3b4685SNathan Whitehorn 	 * Set up the Open Firmware mappings
895fe3b4685SNathan Whitehorn 	 */
896e347e23bSNathan Whitehorn 	chosen = OF_finddevice("/chosen");
897e347e23bSNathan Whitehorn 	if (chosen != -1 && OF_getprop(chosen, "mmu", &mmui, 4) != -1 &&
898e347e23bSNathan Whitehorn 	    (mmu = OF_instance_to_package(mmui)) != -1 &&
899e347e23bSNathan Whitehorn 	    (sz = OF_getproplen(mmu, "translations")) != -1) {
900aa39961eSBenno Rice 		translations = NULL;
9016cc1cdf4SPeter Grehan 		for (i = 0; phys_avail[i] != 0; i += 2) {
9026cc1cdf4SPeter Grehan 			if (phys_avail[i + 1] >= sz) {
903aa39961eSBenno Rice 				translations = (struct ofw_map *)phys_avail[i];
9046cc1cdf4SPeter Grehan 				break;
9056cc1cdf4SPeter Grehan 			}
906aa39961eSBenno Rice 		}
907aa39961eSBenno Rice 		if (translations == NULL)
90859276937SPeter Grehan 			panic("moea_bootstrap: no space to copy translations");
9095244eac9SBenno Rice 		bzero(translations, sz);
9105244eac9SBenno Rice 		if (OF_getprop(mmu, "translations", translations, sz) == -1)
91159276937SPeter Grehan 			panic("moea_bootstrap: can't get ofw translations");
91259276937SPeter Grehan 		CTR0(KTR_PMAP, "moea_bootstrap: translations");
91331c82d03SBenno Rice 		sz /= sizeof(*translations);
9145244eac9SBenno Rice 		qsort(translations, sz, sizeof (*translations), om_cmp);
915ed1e1e2aSNathan Whitehorn 		for (i = 0; i < sz; i++) {
9165244eac9SBenno Rice 			CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
9175244eac9SBenno Rice 			    translations[i].om_pa, translations[i].om_va,
9185244eac9SBenno Rice 			    translations[i].om_len);
9195244eac9SBenno Rice 
92032bc7846SPeter Grehan 			/*
921e347e23bSNathan Whitehorn 			 * If the mapping is 1:1, let the RAM and device
922e347e23bSNathan Whitehorn 			 * on-demand BAT tables take care of the translation.
923f10baa40SBrandon Bergren 			 *
924f10baa40SBrandon Bergren 			 * However, always enter mappings for segment 16,
925f10baa40SBrandon Bergren 			 * which is mixed-protection and therefore not
926f10baa40SBrandon Bergren 			 * compatible with a BAT entry.
92732bc7846SPeter Grehan 			 */
928f10baa40SBrandon Bergren 			if ((translations[i].om_va >> ADDR_SR_SHFT) != 0xf &&
929f10baa40SBrandon Bergren 				translations[i].om_va == translations[i].om_pa)
93032bc7846SPeter Grehan 					continue;
9315244eac9SBenno Rice 
93232bc7846SPeter Grehan 			/* Enter the pages */
933e347e23bSNathan Whitehorn 			for (off = 0; off < translations[i].om_len;
934e347e23bSNathan Whitehorn 			    off += PAGE_SIZE)
935f10baa40SBrandon Bergren 				moea_kenter_attr(translations[i].om_va + off,
936f10baa40SBrandon Bergren 				    translations[i].om_pa + off,
937f10baa40SBrandon Bergren 				    moea_bootstrap_convert_wimg(translations[i].om_mode));
938f9bac91bSBenno Rice 		}
939e347e23bSNathan Whitehorn 	}
940014ffa99SMarcel Moolenaar 
941014ffa99SMarcel Moolenaar 	/*
942014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
943014ffa99SMarcel Moolenaar 	 */
944014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
945014ffa99SMarcel Moolenaar 		;
946014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
9475244eac9SBenno Rice 
94845b69dd6SJustin Hibbits 	moea_cpu_bootstrap(0);
9490081393dSNathan Whitehorn 	mtmsr(mfmsr() | PSL_DR | PSL_IR);
9505244eac9SBenno Rice 	pmap_bootstrapped++;
951014ffa99SMarcel Moolenaar 
952014ffa99SMarcel Moolenaar 	/*
953014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
954014ffa99SMarcel Moolenaar 	 */
955014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
956ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
957014ffa99SMarcel Moolenaar 
958014ffa99SMarcel Moolenaar 	/*
959014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
960014ffa99SMarcel Moolenaar 	 * into the kernel page map.
961014ffa99SMarcel Moolenaar 	 */
962edc82223SKonstantin Belousov 	pa = moea_bootstrap_alloc(kstack_pages * PAGE_SIZE, PAGE_SIZE);
963014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
964edc82223SKonstantin Belousov 	virtual_avail = va + kstack_pages * PAGE_SIZE;
965014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
966014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
967edc82223SKonstantin Belousov 	thread0.td_kstack_pages = kstack_pages;
968edc82223SKonstantin Belousov 	for (i = 0; i < kstack_pages; i++) {
96945b69dd6SJustin Hibbits 		moea_kenter(va, pa);
970014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
971014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
972014ffa99SMarcel Moolenaar 	}
973014ffa99SMarcel Moolenaar 
974014ffa99SMarcel Moolenaar 	/*
975014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
976014ffa99SMarcel Moolenaar 	 */
9774053b05bSSergey Kandaurov 	pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE);
978014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
979014ffa99SMarcel Moolenaar 	va = virtual_avail;
9804053b05bSSergey Kandaurov 	virtual_avail += round_page(msgbufsize);
981014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
98245b69dd6SJustin Hibbits 		moea_kenter(va, pa);
983014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
984014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
985014ffa99SMarcel Moolenaar 	}
98650c202c5SJeff Roberson 
98750c202c5SJeff Roberson 	/*
98850c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
98950c202c5SJeff Roberson 	 */
99050c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
99150c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
99250c202c5SJeff Roberson 	va = virtual_avail;
99350c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
99450c202c5SJeff Roberson 	while (va < virtual_avail) {
99545b69dd6SJustin Hibbits 		moea_kenter(va, pa);
99650c202c5SJeff Roberson 		pa += PAGE_SIZE;
99750c202c5SJeff Roberson 		va += PAGE_SIZE;
99850c202c5SJeff Roberson 	}
99950c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
10005244eac9SBenno Rice }
10015244eac9SBenno Rice 
10025244eac9SBenno Rice /*
10035244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
10045244eac9SBenno Rice  * space can be accessed in any way.
1005f9bac91bSBenno Rice  */
1006f9bac91bSBenno Rice void
100745b69dd6SJustin Hibbits moea_activate(struct thread *td)
1008f9bac91bSBenno Rice {
10098207b362SBenno Rice 	pmap_t	pm, pmr;
1010f9bac91bSBenno Rice 
1011f9bac91bSBenno Rice 	/*
101232bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
10135244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
1014f9bac91bSBenno Rice 	 */
10155244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
101652a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
10178207b362SBenno Rice 
1018c7c2767eSAttilio Rao 	CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
10198207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
1020d1295abdSNathan Whitehorn 
1021d1295abdSNathan Whitehorn 	mtsrin(USER_SR << ADDR_SR_SHFT, td->td_pcb->pcb_cpu.aim.usr_vsid);
1022ac6ba8bdSBenno Rice }
1023ac6ba8bdSBenno Rice 
1024ac6ba8bdSBenno Rice void
102545b69dd6SJustin Hibbits moea_deactivate(struct thread *td)
1026ac6ba8bdSBenno Rice {
1027ac6ba8bdSBenno Rice 	pmap_t	pm;
1028ac6ba8bdSBenno Rice 
1029ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
1030c7c2767eSAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &pm->pm_active);
10318207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
1032f9bac91bSBenno Rice }
1033f9bac91bSBenno Rice 
1034f9bac91bSBenno Rice void
103545b69dd6SJustin Hibbits moea_unwire(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1036a844c68fSAlan Cox {
1037a844c68fSAlan Cox 	struct	pvo_entry key, *pvo;
1038a844c68fSAlan Cox 
1039a844c68fSAlan Cox 	PMAP_LOCK(pm);
1040a844c68fSAlan Cox 	key.pvo_vaddr = sva;
1041a844c68fSAlan Cox 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1042a844c68fSAlan Cox 	    pvo != NULL && PVO_VADDR(pvo) < eva;
1043a844c68fSAlan Cox 	    pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
1044a844c68fSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
1045a844c68fSAlan Cox 			panic("moea_unwire: pvo %p is missing PVO_WIRED", pvo);
1046a844c68fSAlan Cox 		pvo->pvo_vaddr &= ~PVO_WIRED;
1047a844c68fSAlan Cox 		pm->pm_stats.wired_count--;
1048a844c68fSAlan Cox 	}
1049a844c68fSAlan Cox 	PMAP_UNLOCK(pm);
1050a844c68fSAlan Cox }
1051a844c68fSAlan Cox 
1052a844c68fSAlan Cox void
105345b69dd6SJustin Hibbits moea_copy_page(vm_page_t msrc, vm_page_t mdst)
1054f9bac91bSBenno Rice {
105525e2288dSBenno Rice 	vm_offset_t	dst;
105625e2288dSBenno Rice 	vm_offset_t	src;
105725e2288dSBenno Rice 
105825e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
105925e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
106025e2288dSBenno Rice 
1061e3c2930dSNathan Whitehorn 	bcopy((void *)src, (void *)dst, PAGE_SIZE);
1062f9bac91bSBenno Rice }
1063111c77dcSBenno Rice 
1064e8a4a618SKonstantin Belousov void
106545b69dd6SJustin Hibbits moea_copy_pages(vm_page_t *ma, vm_offset_t a_offset,
1066e8a4a618SKonstantin Belousov     vm_page_t *mb, vm_offset_t b_offset, int xfersize)
1067e8a4a618SKonstantin Belousov {
1068e8a4a618SKonstantin Belousov 	void *a_cp, *b_cp;
1069e8a4a618SKonstantin Belousov 	vm_offset_t a_pg_offset, b_pg_offset;
1070e8a4a618SKonstantin Belousov 	int cnt;
1071e8a4a618SKonstantin Belousov 
1072e8a4a618SKonstantin Belousov 	while (xfersize > 0) {
1073e8a4a618SKonstantin Belousov 		a_pg_offset = a_offset & PAGE_MASK;
1074e8a4a618SKonstantin Belousov 		cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
1075e8a4a618SKonstantin Belousov 		a_cp = (char *)VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT]) +
1076e8a4a618SKonstantin Belousov 		    a_pg_offset;
1077e8a4a618SKonstantin Belousov 		b_pg_offset = b_offset & PAGE_MASK;
1078e8a4a618SKonstantin Belousov 		cnt = min(cnt, PAGE_SIZE - b_pg_offset);
1079e8a4a618SKonstantin Belousov 		b_cp = (char *)VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT]) +
1080e8a4a618SKonstantin Belousov 		    b_pg_offset;
1081e8a4a618SKonstantin Belousov 		bcopy(a_cp, b_cp, cnt);
1082e8a4a618SKonstantin Belousov 		a_offset += cnt;
1083e8a4a618SKonstantin Belousov 		b_offset += cnt;
1084e8a4a618SKonstantin Belousov 		xfersize -= cnt;
1085e8a4a618SKonstantin Belousov 	}
1086e8a4a618SKonstantin Belousov }
1087e8a4a618SKonstantin Belousov 
1088111c77dcSBenno Rice /*
10895244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
10905244eac9SBenno Rice  */
10915244eac9SBenno Rice void
109245b69dd6SJustin Hibbits moea_zero_page(vm_page_t m)
10935244eac9SBenno Rice {
1094fe938c08SJustin Hibbits 	vm_offset_t off, pa = VM_PAGE_TO_PHYS(m);
10955244eac9SBenno Rice 
1096fe938c08SJustin Hibbits 	for (off = 0; off < PAGE_SIZE; off += cacheline_size)
1097fe938c08SJustin Hibbits 		__asm __volatile("dcbz 0,%0" :: "r"(pa + off));
10985244eac9SBenno Rice }
10995244eac9SBenno Rice 
11005244eac9SBenno Rice void
110145b69dd6SJustin Hibbits moea_zero_page_area(vm_page_t m, int off, int size)
11025244eac9SBenno Rice {
11033495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
11045b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
11053495845eSBenno Rice 
11065b43c63dSMarcel Moolenaar 	bzero(va, size);
11075244eac9SBenno Rice }
11085244eac9SBenno Rice 
1109713841afSJason A. Harmening vm_offset_t
111045b69dd6SJustin Hibbits moea_quick_enter_page(vm_page_t m)
1111713841afSJason A. Harmening {
1112713841afSJason A. Harmening 
1113713841afSJason A. Harmening 	return (VM_PAGE_TO_PHYS(m));
1114713841afSJason A. Harmening }
1115713841afSJason A. Harmening 
1116713841afSJason A. Harmening void
111745b69dd6SJustin Hibbits moea_quick_remove_page(vm_offset_t addr)
1118713841afSJason A. Harmening {
1119713841afSJason A. Harmening }
1120713841afSJason A. Harmening 
112165bbba25SJustin Hibbits boolean_t
112245b69dd6SJustin Hibbits moea_page_is_mapped(vm_page_t m)
112365bbba25SJustin Hibbits {
112465bbba25SJustin Hibbits 	return (!LIST_EMPTY(&(m)->md.mdpg_pvoh));
112565bbba25SJustin Hibbits }
112665bbba25SJustin Hibbits 
11278dc8feb5SJason A. Harmening bool
11288dc8feb5SJason A. Harmening moea_ps_enabled(pmap_t pmap __unused)
11298dc8feb5SJason A. Harmening {
11308dc8feb5SJason A. Harmening 	return (false);
11318dc8feb5SJason A. Harmening }
11328dc8feb5SJason A. Harmening 
11335244eac9SBenno Rice /*
11345244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
11355244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
11365244eac9SBenno Rice  * will be wired down.
11375244eac9SBenno Rice  */
113839ffa8c1SKonstantin Belousov int
113945b69dd6SJustin Hibbits moea_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
114039ffa8c1SKonstantin Belousov     u_int flags, int8_t psind)
11415244eac9SBenno Rice {
114239ffa8c1SKonstantin Belousov 	int error;
1143ce142d9eSAlan Cox 
114439ffa8c1SKonstantin Belousov 	for (;;) {
11453653f5cbSAlan Cox 		rw_wlock(&pvh_global_lock);
1146ce142d9eSAlan Cox 		PMAP_LOCK(pmap);
114739ffa8c1SKonstantin Belousov 		error = moea_enter_locked(pmap, va, m, prot, flags, psind);
11483653f5cbSAlan Cox 		rw_wunlock(&pvh_global_lock);
1149ce142d9eSAlan Cox 		PMAP_UNLOCK(pmap);
115039ffa8c1SKonstantin Belousov 		if (error != ENOMEM)
115139ffa8c1SKonstantin Belousov 			return (KERN_SUCCESS);
115239ffa8c1SKonstantin Belousov 		if ((flags & PMAP_ENTER_NOSLEEP) != 0)
115339ffa8c1SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
115439ffa8c1SKonstantin Belousov 		VM_OBJECT_ASSERT_UNLOCKED(m->object);
11552c0f13aaSKonstantin Belousov 		vm_wait(NULL);
115639ffa8c1SKonstantin Belousov 	}
1157ce142d9eSAlan Cox }
1158ce142d9eSAlan Cox 
1159ce142d9eSAlan Cox /*
1160ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1161ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1162ce142d9eSAlan Cox  * will be wired down.
1163ce142d9eSAlan Cox  *
1164f26bcf99SAlan Cox  * The global pvh and pmap must be locked.
1165ce142d9eSAlan Cox  */
116639ffa8c1SKonstantin Belousov static int
1167ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
116839ffa8c1SKonstantin Belousov     u_int flags, int8_t psind __unused)
1169ce142d9eSAlan Cox {
11705244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1171378862a7SJeff Roberson 	uma_zone_t	zone;
117257bd5cceSNathan Whitehorn 	u_int		pte_lo, pvo_flags;
11735244eac9SBenno Rice 	int		error;
11745244eac9SBenno Rice 
1175081b8e20SAlan Cox 	if (pmap_bootstrapped)
1176081b8e20SAlan Cox 		rw_assert(&pvh_global_lock, RA_WLOCKED);
1177081b8e20SAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
11782a499f92SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) == 0) {
11792a499f92SKonstantin Belousov 		if ((flags & PMAP_ENTER_QUICK_LOCKED) == 0)
1180205be21dSJeff Roberson 			VM_PAGE_OBJECT_BUSY_ASSERT(m);
11812a499f92SKonstantin Belousov 		else
11822a499f92SKonstantin Belousov 			VM_OBJECT_ASSERT_LOCKED(m->object);
11832a499f92SKonstantin Belousov 	}
1184081b8e20SAlan Cox 
1185081b8e20SAlan Cox 	if ((m->oflags & VPO_UNMANAGED) != 0 || !moea_initialized) {
118659276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
118759276937SPeter Grehan 		zone = moea_upvo_zone;
11885244eac9SBenno Rice 		pvo_flags = 0;
11895244eac9SBenno Rice 	} else {
119003b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
119159276937SPeter Grehan 		zone = moea_mpvo_zone;
11925244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
11935244eac9SBenno Rice 	}
11944dba5df1SPeter Grehan 
1195cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11965244eac9SBenno Rice 
119744b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11985244eac9SBenno Rice 		pte_lo |= PTE_BW;
11992368a371SAlan Cox 		if (pmap_bootstrapped &&
1200d98d0ce2SKonstantin Belousov 		    (m->oflags & VPO_UNMANAGED) == 0)
12013407fefeSKonstantin Belousov 			vm_page_aflag_set(m, PGA_WRITEABLE);
120244b8bd66SAlan Cox 	} else
12035244eac9SBenno Rice 		pte_lo |= PTE_BR;
12045244eac9SBenno Rice 
120539ffa8c1SKonstantin Belousov 	if ((flags & PMAP_ENTER_WIRED) != 0)
12065244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
12075244eac9SBenno Rice 
120859276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
12098207b362SBenno Rice 	    pte_lo, pvo_flags);
12105244eac9SBenno Rice 
12118207b362SBenno Rice 	/*
121257bd5cceSNathan Whitehorn 	 * Flush the real page from the instruction cache. This has be done
121357bd5cceSNathan Whitehorn 	 * for all user mappings to prevent information leakage via the
1214805bee55SNathan Whitehorn 	 * instruction cache. moea_pvo_enter() returns ENOENT for the first
1215805bee55SNathan Whitehorn 	 * mapping for a page.
12168207b362SBenno Rice 	 */
1217805bee55SNathan Whitehorn 	if (pmap != kernel_pmap && error == ENOENT &&
1218805bee55SNathan Whitehorn 	    (pte_lo & (PTE_I | PTE_G)) == 0)
121959276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
122039ffa8c1SKonstantin Belousov 
122139ffa8c1SKonstantin Belousov 	return (error);
1222ce142d9eSAlan Cox }
1223ce142d9eSAlan Cox 
1224ce142d9eSAlan Cox /*
1225ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1226ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1227ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1228ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1229ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1230ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1231ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1232ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1233ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1234ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1235ce142d9eSAlan Cox  */
1236ce142d9eSAlan Cox void
123745b69dd6SJustin Hibbits moea_enter_object(pmap_t pm, vm_offset_t start, vm_offset_t end,
1238ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1239ce142d9eSAlan Cox {
1240ce142d9eSAlan Cox 	vm_page_t m;
1241ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1242ce142d9eSAlan Cox 
12439af6d512SAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(m_start->object);
12449af6d512SAttilio Rao 
1245ce142d9eSAlan Cox 	psize = atop(end - start);
1246ce142d9eSAlan Cox 	m = m_start;
12473653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1248ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1249ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1250ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
12512a499f92SKonstantin Belousov 		    (VM_PROT_READ | VM_PROT_EXECUTE), PMAP_ENTER_QUICK_LOCKED,
12522a499f92SKonstantin Belousov 		    0);
1253ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1254ce142d9eSAlan Cox 	}
12553653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1256ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
12575244eac9SBenno Rice }
12585244eac9SBenno Rice 
12592053c127SStephan Uphoff void
126045b69dd6SJustin Hibbits moea_enter_quick(pmap_t pm, vm_offset_t va, vm_page_t m,
12612053c127SStephan Uphoff     vm_prot_t prot)
1262dca96f1aSAlan Cox {
1263dca96f1aSAlan Cox 
12643653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1265ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1266ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
12672a499f92SKonstantin Belousov 	    PMAP_ENTER_QUICK_LOCKED, 0);
12683653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1269ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1270dca96f1aSAlan Cox }
1271dca96f1aSAlan Cox 
127256b09388SAlan Cox vm_paddr_t
127345b69dd6SJustin Hibbits moea_extract(pmap_t pm, vm_offset_t va)
12745244eac9SBenno Rice {
12750f92104cSBenno Rice 	struct	pvo_entry *pvo;
127648d0b1a0SAlan Cox 	vm_paddr_t pa;
12770f92104cSBenno Rice 
127848d0b1a0SAlan Cox 	PMAP_LOCK(pm);
127959276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
128048d0b1a0SAlan Cox 	if (pvo == NULL)
128148d0b1a0SAlan Cox 		pa = 0;
128248d0b1a0SAlan Cox 	else
12837be655c2SBrandon Bergren 		pa = PVO_PADDR(pvo) | (va & ADDR_POFF);
128448d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
128548d0b1a0SAlan Cox 	return (pa);
12865244eac9SBenno Rice }
12875244eac9SBenno Rice 
12885244eac9SBenno Rice /*
128984792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
129084792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
129184792e72SPeter Grehan  * protection.
129284792e72SPeter Grehan  */
129384792e72SPeter Grehan vm_page_t
129445b69dd6SJustin Hibbits moea_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
129584792e72SPeter Grehan {
1296ab50a262SAlan Cox 	struct	pvo_entry *pvo;
129784792e72SPeter Grehan 	vm_page_t m;
129884792e72SPeter Grehan 
129984792e72SPeter Grehan 	m = NULL;
130048d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
130159276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
130252a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
130352a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1304ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
13057be655c2SBrandon Bergren 		m = PHYS_TO_VM_PAGE(PVO_PADDR(pvo));
1306fee2a2faSMark Johnston 		if (!vm_page_wire_mapped(m))
1307fee2a2faSMark Johnston 			m = NULL;
130884792e72SPeter Grehan 	}
130948d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
131084792e72SPeter Grehan 	return (m);
131184792e72SPeter Grehan }
131284792e72SPeter Grehan 
13135244eac9SBenno Rice void
131445b69dd6SJustin Hibbits moea_init()
13155244eac9SBenno Rice {
13165244eac9SBenno Rice 
131759276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
13180ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
13190ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
132059276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
13210ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
13220ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
132359276937SPeter Grehan 	moea_initialized = TRUE;
13245244eac9SBenno Rice }
13255244eac9SBenno Rice 
13265244eac9SBenno Rice boolean_t
132745b69dd6SJustin Hibbits moea_is_referenced(vm_page_t m)
13287b85f591SAlan Cox {
13298d9e6d9fSAlan Cox 	boolean_t rv;
13307b85f591SAlan Cox 
1331d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1332c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
13338d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
13348d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_REF);
13358d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13368d9e6d9fSAlan Cox 	return (rv);
13377b85f591SAlan Cox }
13387b85f591SAlan Cox 
13397b85f591SAlan Cox boolean_t
134045b69dd6SJustin Hibbits moea_is_modified(vm_page_t m)
13415244eac9SBenno Rice {
13428d9e6d9fSAlan Cox 	boolean_t rv;
13430f92104cSBenno Rice 
1344d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1345567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1346567e51e1SAlan Cox 
1347567e51e1SAlan Cox 	/*
1348638f8678SJeff Roberson 	 * If the page is not busied then this check is racy.
1349567e51e1SAlan Cox 	 */
1350638f8678SJeff Roberson 	if (!pmap_page_is_write_mapped(m))
13510f92104cSBenno Rice 		return (FALSE);
1352638f8678SJeff Roberson 
13538d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
13548d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_CHG);
13558d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13568d9e6d9fSAlan Cox 	return (rv);
1357566526a9SAlan Cox }
1358566526a9SAlan Cox 
1359e396eb60SAlan Cox boolean_t
136045b69dd6SJustin Hibbits moea_is_prefaultable(pmap_t pmap, vm_offset_t va)
1361e396eb60SAlan Cox {
1362e396eb60SAlan Cox 	struct pvo_entry *pvo;
1363e396eb60SAlan Cox 	boolean_t rv;
1364e396eb60SAlan Cox 
1365e396eb60SAlan Cox 	PMAP_LOCK(pmap);
1366e396eb60SAlan Cox 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1367e396eb60SAlan Cox 	rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0;
1368e396eb60SAlan Cox 	PMAP_UNLOCK(pmap);
1369e396eb60SAlan Cox 	return (rv);
1370e396eb60SAlan Cox }
1371e396eb60SAlan Cox 
13725244eac9SBenno Rice void
137345b69dd6SJustin Hibbits moea_clear_modify(vm_page_t m)
137403b6e025SPeter Grehan {
137503b6e025SPeter Grehan 
1376d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1377567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
1378638f8678SJeff Roberson 	vm_page_assert_busied(m);
1379567e51e1SAlan Cox 
1380638f8678SJeff Roberson 	if (!pmap_page_is_write_mapped(m))
138103b6e025SPeter Grehan 		return;
13828d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1383ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
13848d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13855244eac9SBenno Rice }
13865244eac9SBenno Rice 
13877f3a4093SMike Silbersack /*
138878985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
138978985e42SAlan Cox  */
139078985e42SAlan Cox void
139145b69dd6SJustin Hibbits moea_remove_write(vm_page_t m)
139278985e42SAlan Cox {
139378985e42SAlan Cox 	struct	pvo_entry *pvo;
139478985e42SAlan Cox 	struct	pte *pt;
139578985e42SAlan Cox 	pmap_t	pmap;
139678985e42SAlan Cox 	u_int	lo;
139778985e42SAlan Cox 
1398d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
13999ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
1400638f8678SJeff Roberson 	vm_page_assert_busied(m);
14019ab6032fSAlan Cox 
1402638f8678SJeff Roberson 	if (!pmap_page_is_write_mapped(m))
140378985e42SAlan Cox 		return;
14043653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
140578985e42SAlan Cox 	lo = moea_attr_fetch(m);
1406e4f72b32SMarcel Moolenaar 	powerpc_sync();
140778985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
140878985e42SAlan Cox 		pmap = pvo->pvo_pmap;
140978985e42SAlan Cox 		PMAP_LOCK(pmap);
141052a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
141178985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
141252a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
141352a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
141478985e42SAlan Cox 			if (pt != NULL) {
141552a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
141652a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
141752a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
141852a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
141978985e42SAlan Cox 				    pvo->pvo_vaddr);
142078985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
142178985e42SAlan Cox 			}
142278985e42SAlan Cox 		}
142378985e42SAlan Cox 		PMAP_UNLOCK(pmap);
142478985e42SAlan Cox 	}
142578985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
142678985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
142778985e42SAlan Cox 		vm_page_dirty(m);
142878985e42SAlan Cox 	}
14293407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
14303653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
143178985e42SAlan Cox }
143278985e42SAlan Cox 
143378985e42SAlan Cox /*
143459276937SPeter Grehan  *	moea_ts_referenced:
14357f3a4093SMike Silbersack  *
14367f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
14377f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
14387f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
14397f3a4093SMike Silbersack  *	reference bits set.
14407f3a4093SMike Silbersack  *
14417f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
14427f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
14437f3a4093SMike Silbersack  *	optimal aging of shared pages.
14447f3a4093SMike Silbersack  */
14458d9e6d9fSAlan Cox int
144645b69dd6SJustin Hibbits moea_ts_referenced(vm_page_t m)
14475244eac9SBenno Rice {
14488d9e6d9fSAlan Cox 	int count;
144903b6e025SPeter Grehan 
1450d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1451ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
14528d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
14538d9e6d9fSAlan Cox 	count = moea_clear_bit(m, PTE_REF);
14548d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
14558d9e6d9fSAlan Cox 	return (count);
14565244eac9SBenno Rice }
14575244eac9SBenno Rice 
14585244eac9SBenno Rice /*
1459c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1460c1f4123bSNathan Whitehorn  */
1461c1f4123bSNathan Whitehorn void
146245b69dd6SJustin Hibbits moea_page_set_memattr(vm_page_t m, vm_memattr_t ma)
1463c1f4123bSNathan Whitehorn {
1464c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1465cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1466c1f4123bSNathan Whitehorn 	struct	pte *pt;
1467c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1468c1f4123bSNathan Whitehorn 	u_int	lo;
1469c1f4123bSNathan Whitehorn 
1470d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1471cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1472cd6a97f0SNathan Whitehorn 		return;
1473cd6a97f0SNathan Whitehorn 	}
1474cd6a97f0SNathan Whitehorn 
14753653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1476cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1477c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1478cd6a97f0SNathan Whitehorn 
1479cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1480c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1481c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1482c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1483c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1484c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1485c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1486c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1487c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1488c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1489c1f4123bSNathan Whitehorn 				isync();
1490c1f4123bSNathan Whitehorn 		}
1491c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1492c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1493c1f4123bSNathan Whitehorn 	}
1494c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
14953653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1496c1f4123bSNathan Whitehorn }
1497c1f4123bSNathan Whitehorn 
1498c1f4123bSNathan Whitehorn /*
14995244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
15005244eac9SBenno Rice  */
15015244eac9SBenno Rice void
150245b69dd6SJustin Hibbits moea_kenter(vm_offset_t va, vm_paddr_t pa)
15035244eac9SBenno Rice {
1504c1f4123bSNathan Whitehorn 
150545b69dd6SJustin Hibbits 	moea_kenter_attr(va, pa, VM_MEMATTR_DEFAULT);
1506c1f4123bSNathan Whitehorn }
1507c1f4123bSNathan Whitehorn 
1508c1f4123bSNathan Whitehorn void
150945b69dd6SJustin Hibbits moea_kenter_attr(vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma)
1510c1f4123bSNathan Whitehorn {
15115244eac9SBenno Rice 	u_int		pte_lo;
15125244eac9SBenno Rice 	int		error;
15135244eac9SBenno Rice 
15145244eac9SBenno Rice #if 0
15155244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
151659276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
15175244eac9SBenno Rice 		    va);
15185244eac9SBenno Rice #endif
15195244eac9SBenno Rice 
1520c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
15215244eac9SBenno Rice 
15224711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
152359276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
152459276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
15255244eac9SBenno Rice 
15265244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
152759276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
15285244eac9SBenno Rice 		    pa, error);
15295244eac9SBenno Rice 
15304711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
15315244eac9SBenno Rice }
15325244eac9SBenno Rice 
1533e79f59e8SBenno Rice /*
1534e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1535e79f59e8SBenno Rice  * address.
1536e79f59e8SBenno Rice  */
153720b79612SRafal Jaworowski vm_paddr_t
153845b69dd6SJustin Hibbits moea_kextract(vm_offset_t va)
15395244eac9SBenno Rice {
1540e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
154148d0b1a0SAlan Cox 	vm_paddr_t pa;
1542e79f59e8SBenno Rice 
15430efd0097SPeter Grehan 	/*
154452a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
15450efd0097SPeter Grehan 	 */
15460efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
15470efd0097SPeter Grehan 		return (va);
15480efd0097SPeter Grehan 	}
15490efd0097SPeter Grehan 
155048d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
155159276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
155259276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
15537be655c2SBrandon Bergren 	pa = PVO_PADDR(pvo) | (va & ADDR_POFF);
155448d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
155548d0b1a0SAlan Cox 	return (pa);
1556e79f59e8SBenno Rice }
1557e79f59e8SBenno Rice 
155888afb2a3SBenno Rice /*
155988afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
156088afb2a3SBenno Rice  */
15615244eac9SBenno Rice void
156245b69dd6SJustin Hibbits moea_kremove(vm_offset_t va)
15635244eac9SBenno Rice {
156488afb2a3SBenno Rice 
156545b69dd6SJustin Hibbits 	moea_remove(kernel_pmap, va, va + PAGE_SIZE);
15665244eac9SBenno Rice }
15675244eac9SBenno Rice 
15685244eac9SBenno Rice /*
156904329fa7SNathan Whitehorn  * Provide a kernel pointer corresponding to a given userland pointer.
157004329fa7SNathan Whitehorn  * The returned pointer is valid until the next time this function is
157104329fa7SNathan Whitehorn  * called in this thread. This is used internally in copyin/copyout.
157204329fa7SNathan Whitehorn  */
157304329fa7SNathan Whitehorn int
157445b69dd6SJustin Hibbits moea_map_user_ptr(pmap_t pm, volatile const void *uaddr,
157504329fa7SNathan Whitehorn     void **kaddr, size_t ulen, size_t *klen)
157604329fa7SNathan Whitehorn {
157704329fa7SNathan Whitehorn 	size_t l;
157804329fa7SNathan Whitehorn 	register_t vsid;
157904329fa7SNathan Whitehorn 
158004329fa7SNathan Whitehorn 	*kaddr = (char *)USER_ADDR + ((uintptr_t)uaddr & ~SEGMENT_MASK);
158104329fa7SNathan Whitehorn 	l = ((char *)USER_ADDR + SEGMENT_LENGTH) - (char *)(*kaddr);
158204329fa7SNathan Whitehorn 	if (l > ulen)
158304329fa7SNathan Whitehorn 		l = ulen;
158404329fa7SNathan Whitehorn 	if (klen)
158504329fa7SNathan Whitehorn 		*klen = l;
158604329fa7SNathan Whitehorn 	else if (l != ulen)
158704329fa7SNathan Whitehorn 		return (EFAULT);
158804329fa7SNathan Whitehorn 
158904329fa7SNathan Whitehorn 	vsid = va_to_vsid(pm, (vm_offset_t)uaddr);
159004329fa7SNathan Whitehorn 
159104329fa7SNathan Whitehorn 	/* Mark segment no-execute */
159204329fa7SNathan Whitehorn 	vsid |= SR_N;
159304329fa7SNathan Whitehorn 
159404329fa7SNathan Whitehorn 	/* If we have already set this VSID, we can just return */
159504329fa7SNathan Whitehorn 	if (curthread->td_pcb->pcb_cpu.aim.usr_vsid == vsid)
159604329fa7SNathan Whitehorn 		return (0);
159704329fa7SNathan Whitehorn 
159804329fa7SNathan Whitehorn 	__asm __volatile("isync");
159904329fa7SNathan Whitehorn 	curthread->td_pcb->pcb_cpu.aim.usr_segm =
160004329fa7SNathan Whitehorn 	    (uintptr_t)uaddr >> ADDR_SR_SHFT;
160104329fa7SNathan Whitehorn 	curthread->td_pcb->pcb_cpu.aim.usr_vsid = vsid;
160204329fa7SNathan Whitehorn 	__asm __volatile("mtsr %0,%1; isync" :: "n"(USER_SR), "r"(vsid));
160304329fa7SNathan Whitehorn 
160404329fa7SNathan Whitehorn 	return (0);
160504329fa7SNathan Whitehorn }
160604329fa7SNathan Whitehorn 
160704329fa7SNathan Whitehorn /*
1608eb1baf72SNathan Whitehorn  * Figure out where a given kernel pointer (usually in a fault) points
1609eb1baf72SNathan Whitehorn  * to from the VM's perspective, potentially remapping into userland's
1610eb1baf72SNathan Whitehorn  * address space.
1611eb1baf72SNathan Whitehorn  */
1612eb1baf72SNathan Whitehorn static int
161345b69dd6SJustin Hibbits moea_decode_kernel_ptr(vm_offset_t addr, int *is_user,
1614eb1baf72SNathan Whitehorn     vm_offset_t *decoded_addr)
1615eb1baf72SNathan Whitehorn {
1616eb1baf72SNathan Whitehorn 	vm_offset_t user_sr;
1617eb1baf72SNathan Whitehorn 
1618eb1baf72SNathan Whitehorn 	if ((addr >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
1619eb1baf72SNathan Whitehorn 		user_sr = curthread->td_pcb->pcb_cpu.aim.usr_segm;
1620eb1baf72SNathan Whitehorn 		addr &= ADDR_PIDX | ADDR_POFF;
1621eb1baf72SNathan Whitehorn 		addr |= user_sr << ADDR_SR_SHFT;
1622eb1baf72SNathan Whitehorn 		*decoded_addr = addr;
1623eb1baf72SNathan Whitehorn 		*is_user = 1;
1624eb1baf72SNathan Whitehorn 	} else {
1625eb1baf72SNathan Whitehorn 		*decoded_addr = addr;
1626eb1baf72SNathan Whitehorn 		*is_user = 0;
1627eb1baf72SNathan Whitehorn 	}
1628eb1baf72SNathan Whitehorn 
1629eb1baf72SNathan Whitehorn 	return (0);
1630eb1baf72SNathan Whitehorn }
1631eb1baf72SNathan Whitehorn 
1632eb1baf72SNathan Whitehorn /*
16335244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
16345244eac9SBenno Rice  *
16355244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
16365244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
16375244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
16385244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
16395244eac9SBenno Rice  * first usable address after the mapped region.
16405244eac9SBenno Rice  */
16415244eac9SBenno Rice vm_offset_t
164245b69dd6SJustin Hibbits moea_map(vm_offset_t *virt, vm_paddr_t pa_start,
164320b79612SRafal Jaworowski     vm_paddr_t pa_end, int prot)
16445244eac9SBenno Rice {
16455244eac9SBenno Rice 	vm_offset_t	sva, va;
16465244eac9SBenno Rice 
16475244eac9SBenno Rice 	sva = *virt;
16485244eac9SBenno Rice 	va = sva;
16495244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
165045b69dd6SJustin Hibbits 		moea_kenter(va, pa_start);
16515244eac9SBenno Rice 	*virt = va;
16525244eac9SBenno Rice 	return (sva);
16535244eac9SBenno Rice }
16545244eac9SBenno Rice 
16555244eac9SBenno Rice /*
16567f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
16577f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
16587f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
16597f3a4093SMike Silbersack  * is only necessary that true be returned for a small
16607f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
16617f3a4093SMike Silbersack  */
16625244eac9SBenno Rice boolean_t
166345b69dd6SJustin Hibbits moea_page_exists_quick(pmap_t pmap, vm_page_t m)
16645244eac9SBenno Rice {
166503b6e025SPeter Grehan         int loops;
166603b6e025SPeter Grehan 	struct pvo_entry *pvo;
1667ce186587SAlan Cox 	boolean_t rv;
166803b6e025SPeter Grehan 
1669d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1670ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
167103b6e025SPeter Grehan 	loops = 0;
1672ce186587SAlan Cox 	rv = FALSE;
16733653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
167403b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1675ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1676ce186587SAlan Cox 			rv = TRUE;
1677ce186587SAlan Cox 			break;
1678ce186587SAlan Cox 		}
167903b6e025SPeter Grehan 		if (++loops >= 16)
168003b6e025SPeter Grehan 			break;
168103b6e025SPeter Grehan 	}
16823653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1683ce186587SAlan Cox 	return (rv);
16845244eac9SBenno Rice }
16855244eac9SBenno Rice 
1686b999e9c8SMark Johnston void
168745b69dd6SJustin Hibbits moea_page_init(vm_page_t m)
1688b999e9c8SMark Johnston {
1689b999e9c8SMark Johnston 
1690b999e9c8SMark Johnston 	m->md.mdpg_attrs = 0;
1691b999e9c8SMark Johnston 	m->md.mdpg_cache_attrs = VM_MEMATTR_DEFAULT;
1692b999e9c8SMark Johnston 	LIST_INIT(&m->md.mdpg_pvoh);
1693b999e9c8SMark Johnston }
1694b999e9c8SMark Johnston 
169559677d3cSAlan Cox /*
169659677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
169759677d3cSAlan Cox  * that are wired.
169859677d3cSAlan Cox  */
169959677d3cSAlan Cox int
170045b69dd6SJustin Hibbits moea_page_wired_mappings(vm_page_t m)
170159677d3cSAlan Cox {
170259677d3cSAlan Cox 	struct pvo_entry *pvo;
170359677d3cSAlan Cox 	int count;
170459677d3cSAlan Cox 
170559677d3cSAlan Cox 	count = 0;
1706d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0)
170759677d3cSAlan Cox 		return (count);
17083653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
170959677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
171059677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
171159677d3cSAlan Cox 			count++;
17123653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
171359677d3cSAlan Cox 	return (count);
171459677d3cSAlan Cox }
171559677d3cSAlan Cox 
171659276937SPeter Grehan static u_int	moea_vsidcontext;
17175244eac9SBenno Rice 
171845b69dd6SJustin Hibbits int
171945b69dd6SJustin Hibbits moea_pinit(pmap_t pmap)
17205244eac9SBenno Rice {
17215244eac9SBenno Rice 	int	i, mask;
17225244eac9SBenno Rice 	u_int	entropy;
17235244eac9SBenno Rice 
1724ccc4a5c7SNathan Whitehorn 	RB_INIT(&pmap->pmap_pvo);
17254daf20b2SPeter Grehan 
17265244eac9SBenno Rice 	entropy = 0;
17275244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
17285244eac9SBenno Rice 
172945b69dd6SJustin Hibbits 	if ((pmap->pmap_phys = (pmap_t)moea_kextract((vm_offset_t)pmap))
173052a7870dSNathan Whitehorn 	    == NULL) {
173152a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
173252a7870dSNathan Whitehorn 	}
173352a7870dSNathan Whitehorn 
1734e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
17355244eac9SBenno Rice 	/*
17365244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
17375244eac9SBenno Rice 	 */
17385244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
17395244eac9SBenno Rice 		u_int	hash, n;
17405244eac9SBenno Rice 
17415244eac9SBenno Rice 		/*
1742*f259d24fSGordon Bergling 		 * Create a new value by multiplying by a prime and adding in
17435244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
17445244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
17455244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
17465244eac9SBenno Rice 		 * instead of a multiply.)
17475244eac9SBenno Rice 		 */
174859276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
174959276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
17505244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
17515244eac9SBenno Rice 			continue;
17525244eac9SBenno Rice 		n = hash >> 5;
17535244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
175459276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
175559276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
17565244eac9SBenno Rice 			/* anything free in this bucket? */
175759276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
175859276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
17595244eac9SBenno Rice 				continue;
17605244eac9SBenno Rice 			}
17610dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
17625244eac9SBenno Rice 			mask = 1 << i;
1763d9c9c81cSPedro F. Giffuni 			hash &= rounddown2(0xfffff, VSID_NBPW);
17645244eac9SBenno Rice 			hash |= i;
17655244eac9SBenno Rice 		}
176646e93cbbSNathan Whitehorn 		KASSERT(!(moea_vsid_bitmap[n] & mask),
176746e93cbbSNathan Whitehorn 		    ("Allocating in-use VSID group %#x\n", hash));
176859276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
17695244eac9SBenno Rice 		for (i = 0; i < 16; i++)
17705244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1771e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
177245b69dd6SJustin Hibbits 		return (1);
17735244eac9SBenno Rice 	}
17745244eac9SBenno Rice 
1775e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
177659276937SPeter Grehan 	panic("moea_pinit: out of segments");
17775244eac9SBenno Rice }
17785244eac9SBenno Rice 
17795244eac9SBenno Rice /*
17805244eac9SBenno Rice  * Initialize the pmap associated with process 0.
17815244eac9SBenno Rice  */
17825244eac9SBenno Rice void
178345b69dd6SJustin Hibbits moea_pinit0(pmap_t pm)
17845244eac9SBenno Rice {
17855244eac9SBenno Rice 
1786e68c64f0SKonstantin Belousov 	PMAP_LOCK_INIT(pm);
178745b69dd6SJustin Hibbits 	moea_pinit(pm);
17885244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
17895244eac9SBenno Rice }
17905244eac9SBenno Rice 
1791e79f59e8SBenno Rice /*
1792e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1793e79f59e8SBenno Rice  */
17945244eac9SBenno Rice void
179545b69dd6SJustin Hibbits moea_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva,
179659276937SPeter Grehan     vm_prot_t prot)
17975244eac9SBenno Rice {
1798ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
1799e79f59e8SBenno Rice 	struct	pte *pt;
1800e79f59e8SBenno Rice 
1801e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
180259276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1803e79f59e8SBenno Rice 
1804e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
180545b69dd6SJustin Hibbits 		moea_remove(pm, sva, eva);
1806e79f59e8SBenno Rice 		return;
1807e79f59e8SBenno Rice 	}
1808e79f59e8SBenno Rice 
18093653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
181048d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1811ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1812ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1813ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1814ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1815e79f59e8SBenno Rice 
1816e79f59e8SBenno Rice 		/*
1817e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1818e79f59e8SBenno Rice 		 * copy.
1819e79f59e8SBenno Rice 		 */
1820ccc4a5c7SNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1821e79f59e8SBenno Rice 		/*
1822e79f59e8SBenno Rice 		 * Change the protection of the page.
1823e79f59e8SBenno Rice 		 */
182452a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
182552a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1826e79f59e8SBenno Rice 
1827e79f59e8SBenno Rice 		/*
1828e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1829e79f59e8SBenno Rice 		 */
1830d644a0b7SAlan Cox 		if (pt != NULL) {
183152a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1832d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1833d644a0b7SAlan Cox 		}
1834e79f59e8SBenno Rice 	}
18353653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
183648d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
18375244eac9SBenno Rice }
18385244eac9SBenno Rice 
183988afb2a3SBenno Rice /*
184088afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
184188afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
184288afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
184388afb2a3SBenno Rice  */
18445244eac9SBenno Rice void
184545b69dd6SJustin Hibbits moea_qenter(vm_offset_t sva, vm_page_t *m, int count)
18465244eac9SBenno Rice {
184703b6e025SPeter Grehan 	vm_offset_t va;
18485244eac9SBenno Rice 
184903b6e025SPeter Grehan 	va = sva;
185003b6e025SPeter Grehan 	while (count-- > 0) {
185145b69dd6SJustin Hibbits 		moea_kenter(va, VM_PAGE_TO_PHYS(*m));
185203b6e025SPeter Grehan 		va += PAGE_SIZE;
185303b6e025SPeter Grehan 		m++;
185403b6e025SPeter Grehan 	}
18555244eac9SBenno Rice }
18565244eac9SBenno Rice 
185788afb2a3SBenno Rice /*
185888afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
185959276937SPeter Grehan  * temporary mappings entered by moea_qenter.
186088afb2a3SBenno Rice  */
18615244eac9SBenno Rice void
186245b69dd6SJustin Hibbits moea_qremove(vm_offset_t sva, int count)
18635244eac9SBenno Rice {
186403b6e025SPeter Grehan 	vm_offset_t va;
186588afb2a3SBenno Rice 
186603b6e025SPeter Grehan 	va = sva;
186703b6e025SPeter Grehan 	while (count-- > 0) {
186845b69dd6SJustin Hibbits 		moea_kremove(va);
186903b6e025SPeter Grehan 		va += PAGE_SIZE;
187003b6e025SPeter Grehan 	}
18715244eac9SBenno Rice }
18725244eac9SBenno Rice 
18735244eac9SBenno Rice void
187445b69dd6SJustin Hibbits moea_release(pmap_t pmap)
18755244eac9SBenno Rice {
187632bc7846SPeter Grehan         int idx, mask;
187732bc7846SPeter Grehan 
187832bc7846SPeter Grehan 	/*
187932bc7846SPeter Grehan 	 * Free segment register's VSID
188032bc7846SPeter Grehan 	 */
188132bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
188259276937SPeter Grehan                 panic("moea_release");
188332bc7846SPeter Grehan 
1884e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
188532bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
188632bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
188732bc7846SPeter Grehan         idx /= VSID_NBPW;
188859276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1889e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
18905244eac9SBenno Rice }
18915244eac9SBenno Rice 
189288afb2a3SBenno Rice /*
189388afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
189488afb2a3SBenno Rice  */
18955244eac9SBenno Rice void
189645b69dd6SJustin Hibbits moea_remove(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
18975244eac9SBenno Rice {
1898ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
189988afb2a3SBenno Rice 
19003653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
190148d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1902ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1903ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1904ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1905ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1906598d99ddSNathan Whitehorn 		moea_pvo_remove(pvo, -1);
1907598d99ddSNathan Whitehorn 	}
190848d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
19093653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
19105244eac9SBenno Rice }
19115244eac9SBenno Rice 
1912e79f59e8SBenno Rice /*
191359276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
191403b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
191503b6e025SPeter Grehan  */
191603b6e025SPeter Grehan void
191745b69dd6SJustin Hibbits moea_remove_all(vm_page_t m)
191803b6e025SPeter Grehan {
191903b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
192003b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
192148d0b1a0SAlan Cox 	pmap_t	pmap;
192203b6e025SPeter Grehan 
19233653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
192403b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
192503b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
192603b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
192703b6e025SPeter Grehan 
192848d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
192948d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
193059276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
193148d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
193203b6e025SPeter Grehan 	}
19335cff1f4dSMark Johnston 	if ((m->a.flags & PGA_WRITEABLE) && moea_query_bit(m, PTE_CHG)) {
1934c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1935062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1936062c8f4cSNathan Whitehorn 	}
19373407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
19383653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
193903b6e025SPeter Grehan }
194003b6e025SPeter Grehan 
194143d3fc80SBrandon Bergren static int
194243d3fc80SBrandon Bergren moea_mincore(pmap_t pm, vm_offset_t va, vm_paddr_t *pap)
194343d3fc80SBrandon Bergren {
194443d3fc80SBrandon Bergren 	struct pvo_entry *pvo;
194543d3fc80SBrandon Bergren 	vm_paddr_t pa;
194643d3fc80SBrandon Bergren 	vm_page_t m;
194743d3fc80SBrandon Bergren 	int val;
194843d3fc80SBrandon Bergren 	bool managed;
194943d3fc80SBrandon Bergren 
195043d3fc80SBrandon Bergren 	PMAP_LOCK(pm);
195143d3fc80SBrandon Bergren 
195243d3fc80SBrandon Bergren 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
195343d3fc80SBrandon Bergren 	if (pvo != NULL) {
195443d3fc80SBrandon Bergren 		pa = PVO_PADDR(pvo);
195543d3fc80SBrandon Bergren 		m = PHYS_TO_VM_PAGE(pa);
195643d3fc80SBrandon Bergren 		managed = (pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED;
195743d3fc80SBrandon Bergren 		val = MINCORE_INCORE;
195843d3fc80SBrandon Bergren 	} else {
195943d3fc80SBrandon Bergren 		PMAP_UNLOCK(pm);
196043d3fc80SBrandon Bergren 		return (0);
196143d3fc80SBrandon Bergren 	}
196243d3fc80SBrandon Bergren 
196343d3fc80SBrandon Bergren 	PMAP_UNLOCK(pm);
196443d3fc80SBrandon Bergren 
196543d3fc80SBrandon Bergren 	if (m == NULL)
196643d3fc80SBrandon Bergren 		return (0);
196743d3fc80SBrandon Bergren 
196843d3fc80SBrandon Bergren 	if (managed) {
196943d3fc80SBrandon Bergren 		if (moea_is_modified(m))
197043d3fc80SBrandon Bergren 			val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
197143d3fc80SBrandon Bergren 
197243d3fc80SBrandon Bergren 		if (moea_is_referenced(m))
197343d3fc80SBrandon Bergren 			val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
197443d3fc80SBrandon Bergren 	}
197543d3fc80SBrandon Bergren 
197643d3fc80SBrandon Bergren 	if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
197743d3fc80SBrandon Bergren 	    (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
197843d3fc80SBrandon Bergren 	    managed) {
197943d3fc80SBrandon Bergren 		*pap = pa;
198043d3fc80SBrandon Bergren 	}
198143d3fc80SBrandon Bergren 
198243d3fc80SBrandon Bergren 	return (val);
198343d3fc80SBrandon Bergren }
198443d3fc80SBrandon Bergren 
198503b6e025SPeter Grehan /*
19865244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
198759276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
19885244eac9SBenno Rice  * calculated.
19895244eac9SBenno Rice  */
19905244eac9SBenno Rice static vm_offset_t
199159276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
19925244eac9SBenno Rice {
19935244eac9SBenno Rice 	vm_offset_t	s, e;
19945244eac9SBenno Rice 	int		i, j;
19955244eac9SBenno Rice 
19965244eac9SBenno Rice 	size = round_page(size);
19975244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
19985244eac9SBenno Rice 		if (align != 0)
1999d9c9c81cSPedro F. Giffuni 			s = roundup2(phys_avail[i], align);
20005244eac9SBenno Rice 		else
20015244eac9SBenno Rice 			s = phys_avail[i];
20025244eac9SBenno Rice 		e = s + size;
20035244eac9SBenno Rice 
20045244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
20055244eac9SBenno Rice 			continue;
20065244eac9SBenno Rice 
20075244eac9SBenno Rice 		if (s == phys_avail[i]) {
20085244eac9SBenno Rice 			phys_avail[i] += size;
20095244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
20105244eac9SBenno Rice 			phys_avail[i + 1] -= size;
20115244eac9SBenno Rice 		} else {
20125244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
20135244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
20145244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
20155244eac9SBenno Rice 			}
20165244eac9SBenno Rice 
20175244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
20185244eac9SBenno Rice 			phys_avail[i + 1] = s;
20195244eac9SBenno Rice 			phys_avail[i + 2] = e;
20205244eac9SBenno Rice 			phys_avail_count++;
20215244eac9SBenno Rice 		}
20225244eac9SBenno Rice 
20235244eac9SBenno Rice 		return (s);
20245244eac9SBenno Rice 	}
202559276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
20265244eac9SBenno Rice }
20275244eac9SBenno Rice 
20285244eac9SBenno Rice static void
20290936003eSJustin Hibbits moea_syncicache(vm_paddr_t pa, vm_size_t len)
20305244eac9SBenno Rice {
20315244eac9SBenno Rice 	__syncicache((void *)pa, len);
20325244eac9SBenno Rice }
20335244eac9SBenno Rice 
20345244eac9SBenno Rice static int
203559276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
20360936003eSJustin Hibbits     vm_offset_t va, vm_paddr_t pa, u_int pte_lo, int flags)
20375244eac9SBenno Rice {
20385244eac9SBenno Rice 	struct	pvo_entry *pvo;
20395244eac9SBenno Rice 	u_int	sr;
20405244eac9SBenno Rice 	int	first;
20415244eac9SBenno Rice 	u_int	ptegidx;
20425244eac9SBenno Rice 	int	i;
204332bc7846SPeter Grehan 	int     bootstrap;
20445244eac9SBenno Rice 
204559276937SPeter Grehan 	moea_pvo_enter_calls++;
20468207b362SBenno Rice 	first = 0;
204732bc7846SPeter Grehan 	bootstrap = 0;
204832bc7846SPeter Grehan 
20495244eac9SBenno Rice 	/*
20505244eac9SBenno Rice 	 * Compute the PTE Group index.
20515244eac9SBenno Rice 	 */
20525244eac9SBenno Rice 	va &= ~ADDR_POFF;
20535244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
20545244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
20555244eac9SBenno Rice 
20565244eac9SBenno Rice 	/*
20575244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
20585244eac9SBenno Rice 	 * there is a mapping.
20595244eac9SBenno Rice 	 */
206059276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
206159276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
20625244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
20637be655c2SBrandon Bergren 			if (PVO_PADDR(pvo) == pa &&
206452a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
2065fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
2066add03590SAlan Cox 				/*
2067add03590SAlan Cox 				 * The PTE is not changing.  Instead, this may
2068add03590SAlan Cox 				 * be a request to change the mapping's wired
2069add03590SAlan Cox 				 * attribute.
2070add03590SAlan Cox 				 */
207159276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
2072add03590SAlan Cox 				if ((flags & PVO_WIRED) != 0 &&
2073add03590SAlan Cox 				    (pvo->pvo_vaddr & PVO_WIRED) == 0) {
2074add03590SAlan Cox 					pvo->pvo_vaddr |= PVO_WIRED;
2075add03590SAlan Cox 					pm->pm_stats.wired_count++;
2076add03590SAlan Cox 				} else if ((flags & PVO_WIRED) == 0 &&
2077add03590SAlan Cox 				    (pvo->pvo_vaddr & PVO_WIRED) != 0) {
2078add03590SAlan Cox 					pvo->pvo_vaddr &= ~PVO_WIRED;
2079add03590SAlan Cox 					pm->pm_stats.wired_count--;
2080add03590SAlan Cox 				}
208149f8f727SBenno Rice 				return (0);
2082fafc7362SBenno Rice 			}
208359276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
20845244eac9SBenno Rice 			break;
20855244eac9SBenno Rice 		}
20865244eac9SBenno Rice 	}
20875244eac9SBenno Rice 
20885244eac9SBenno Rice 	/*
20895244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
20905244eac9SBenno Rice 	 */
209159276937SPeter Grehan 	if (moea_initialized) {
2092378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
209349f8f727SBenno Rice 	} else {
209459276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
209559276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
209659276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
20970d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
209849f8f727SBenno Rice 		}
209959276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
210059276937SPeter Grehan 		moea_bpvo_pool_index++;
210132bc7846SPeter Grehan 		bootstrap = 1;
210249f8f727SBenno Rice 	}
21035244eac9SBenno Rice 
21045244eac9SBenno Rice 	if (pvo == NULL) {
210559276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
21065244eac9SBenno Rice 		return (ENOMEM);
21075244eac9SBenno Rice 	}
21085244eac9SBenno Rice 
210959276937SPeter Grehan 	moea_pvo_entries++;
21105244eac9SBenno Rice 	pvo->pvo_vaddr = va;
21115244eac9SBenno Rice 	pvo->pvo_pmap = pm;
211259276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
21135244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
21145244eac9SBenno Rice 	if (flags & PVO_WIRED)
21155244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
211659276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
21175244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
211832bc7846SPeter Grehan 	if (bootstrap)
211932bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
21204dba5df1SPeter Grehan 
212152a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
21225244eac9SBenno Rice 
21235244eac9SBenno Rice 	/*
2124598d99ddSNathan Whitehorn 	 * Add to pmap list
2125598d99ddSNathan Whitehorn 	 */
2126ccc4a5c7SNathan Whitehorn 	RB_INSERT(pvo_tree, &pm->pmap_pvo, pvo);
2127598d99ddSNathan Whitehorn 
2128598d99ddSNathan Whitehorn 	/*
21295244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
21305244eac9SBenno Rice 	 * item.
21315244eac9SBenno Rice 	 */
21328207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
21338207b362SBenno Rice 		first = 1;
21345244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
21354dba5df1SPeter Grehan 
2136bfc30490SAlan Cox 	if (pvo->pvo_vaddr & PVO_WIRED)
2137c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
2138c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
21395244eac9SBenno Rice 
214052a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
2141804d1cc1SJustin Hibbits 	KASSERT(i < 8, ("Invalid PTE index"));
21425244eac9SBenno Rice 	if (i >= 0) {
21435244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
21445244eac9SBenno Rice 	} else {
214559276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
214659276937SPeter Grehan 		moea_pte_overflow++;
21475244eac9SBenno Rice 	}
214859276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
21494dba5df1SPeter Grehan 
21505244eac9SBenno Rice 	return (first ? ENOENT : 0);
21515244eac9SBenno Rice }
21525244eac9SBenno Rice 
21535244eac9SBenno Rice static void
215459276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
21555244eac9SBenno Rice {
21565244eac9SBenno Rice 	struct	pte *pt;
21575244eac9SBenno Rice 
21585244eac9SBenno Rice 	/*
21595244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
21605244eac9SBenno Rice 	 * save the ref & cfg bits).
21615244eac9SBenno Rice 	 */
216259276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
21635244eac9SBenno Rice 	if (pt != NULL) {
216452a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
2165d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
21665244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
21675244eac9SBenno Rice 	} else {
216859276937SPeter Grehan 		moea_pte_overflow--;
21695244eac9SBenno Rice 	}
21705244eac9SBenno Rice 
21715244eac9SBenno Rice 	/*
21725244eac9SBenno Rice 	 * Update our statistics.
21735244eac9SBenno Rice 	 */
21745244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
2175bfc30490SAlan Cox 	if (pvo->pvo_vaddr & PVO_WIRED)
21765244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
21775244eac9SBenno Rice 
21785244eac9SBenno Rice 	/*
2179b4efea53SMark Johnston 	 * Remove this PVO from the PV and pmap lists.
2180b4efea53SMark Johnston 	 */
2181b4efea53SMark Johnston 	LIST_REMOVE(pvo, pvo_vlink);
2182b4efea53SMark Johnston 	RB_REMOVE(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo);
2183b4efea53SMark Johnston 
2184b4efea53SMark Johnston 	/*
21855244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
2186b4efea53SMark Johnston 	 * Clear PGA_WRITEABLE if all mappings of the page have been removed.
21875244eac9SBenno Rice 	 */
2188d98d0ce2SKonstantin Belousov 	if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED) {
21895244eac9SBenno Rice 		struct vm_page *pg;
21905244eac9SBenno Rice 
21917be655c2SBrandon Bergren 		pg = PHYS_TO_VM_PAGE(PVO_PADDR(pvo));
21925244eac9SBenno Rice 		if (pg != NULL) {
219352a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
21945244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
2195b4efea53SMark Johnston 			if (LIST_EMPTY(&pg->md.mdpg_pvoh))
2196b4efea53SMark Johnston 				vm_page_aflag_clear(pg, PGA_WRITEABLE);
21975244eac9SBenno Rice 		}
21985244eac9SBenno Rice 	}
21995244eac9SBenno Rice 
22005244eac9SBenno Rice 	/*
22015244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
22025244eac9SBenno Rice 	 * if we aren't going to reuse it.
22035244eac9SBenno Rice 	 */
22045244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
220549f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
220659276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
220759276937SPeter Grehan 		    moea_upvo_zone, pvo);
220859276937SPeter Grehan 	moea_pvo_entries--;
220959276937SPeter Grehan 	moea_pvo_remove_calls++;
22105244eac9SBenno Rice }
22115244eac9SBenno Rice 
22125244eac9SBenno Rice static __inline int
221359276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
22145244eac9SBenno Rice {
22155244eac9SBenno Rice 	int	pteidx;
22165244eac9SBenno Rice 
22175244eac9SBenno Rice 	/*
22185244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
22195244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
22205244eac9SBenno Rice 	 * noticing the HID bit.
22215244eac9SBenno Rice 	 */
22225244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
222352a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
222459276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
22255244eac9SBenno Rice 
22265244eac9SBenno Rice 	return (pteidx);
22275244eac9SBenno Rice }
22285244eac9SBenno Rice 
22295244eac9SBenno Rice static struct pvo_entry *
223059276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
22315244eac9SBenno Rice {
22325244eac9SBenno Rice 	struct	pvo_entry *pvo;
22335244eac9SBenno Rice 	int	ptegidx;
22345244eac9SBenno Rice 	u_int	sr;
22355244eac9SBenno Rice 
22365244eac9SBenno Rice 	va &= ~ADDR_POFF;
22375244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
22385244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
22395244eac9SBenno Rice 
224059276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
224159276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
22425244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
22435244eac9SBenno Rice 			if (pteidx_p)
224459276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2245f489bf21SAlan Cox 			break;
22465244eac9SBenno Rice 		}
22475244eac9SBenno Rice 	}
224859276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
22495244eac9SBenno Rice 
2250f489bf21SAlan Cox 	return (pvo);
22515244eac9SBenno Rice }
22525244eac9SBenno Rice 
22535244eac9SBenno Rice static struct pte *
225459276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
22555244eac9SBenno Rice {
22565244eac9SBenno Rice 	struct	pte *pt;
22575244eac9SBenno Rice 
22585244eac9SBenno Rice 	/*
22595244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
22605244eac9SBenno Rice 	 */
22615244eac9SBenno Rice 	if (pteidx == -1) {
22625244eac9SBenno Rice 		int	ptegidx;
22635244eac9SBenno Rice 		u_int	sr;
22645244eac9SBenno Rice 
22655244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
22665244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
226759276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
22685244eac9SBenno Rice 	}
22695244eac9SBenno Rice 
227059276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2271d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
22725244eac9SBenno Rice 
227352a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
227459276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
22755244eac9SBenno Rice 		    "valid pte index", pvo);
22765244eac9SBenno Rice 	}
22775244eac9SBenno Rice 
227852a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
227959276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
22805244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
22815244eac9SBenno Rice 	}
22825244eac9SBenno Rice 
228352a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
228452a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
228559276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
228659276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
22875244eac9SBenno Rice 		}
22885244eac9SBenno Rice 
228952a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
22905244eac9SBenno Rice 		    != 0) {
229159276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
229259276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
22935244eac9SBenno Rice 		}
22945244eac9SBenno Rice 
2295d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
22965244eac9SBenno Rice 		return (pt);
22975244eac9SBenno Rice 	}
22985244eac9SBenno Rice 
229952a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
230059276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
2301804d1cc1SJustin Hibbits 		    "moea_pteg_table but valid in pvo: %8x, %8x", pvo, pt, pvo->pvo_pte.pte.pte_hi, pt->pte_hi);
23025244eac9SBenno Rice 	}
23035244eac9SBenno Rice 
2304d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
23055244eac9SBenno Rice 	return (NULL);
23065244eac9SBenno Rice }
23075244eac9SBenno Rice 
23085244eac9SBenno Rice /*
23095244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
23105244eac9SBenno Rice  */
23115244eac9SBenno Rice int
231259276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
23135244eac9SBenno Rice {
23145244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
23155244eac9SBenno Rice 	struct	pvo_entry *pvo;
23165244eac9SBenno Rice 	int	ptegidx, i, j;
23175244eac9SBenno Rice 	u_int	sr;
23185244eac9SBenno Rice 	struct	pteg *pteg;
23195244eac9SBenno Rice 	struct	pte *pt;
23205244eac9SBenno Rice 
232159276937SPeter Grehan 	moea_pte_spills++;
23225244eac9SBenno Rice 
2323d080d5fdSBenno Rice 	sr = mfsrin(addr);
23245244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
23255244eac9SBenno Rice 
23265244eac9SBenno Rice 	/*
23275244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
23285244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
23295244eac9SBenno Rice 	 */
233059276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
233159276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
23325244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
23335244eac9SBenno Rice 	i &= 7;
23345244eac9SBenno Rice 	pt = &pteg->pt[i];
23355244eac9SBenno Rice 
23365244eac9SBenno Rice 	source_pvo = NULL;
23375244eac9SBenno Rice 	victim_pvo = NULL;
233859276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
23395244eac9SBenno Rice 		/*
23405244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
23415244eac9SBenno Rice 		 */
23425244eac9SBenno Rice 		if (source_pvo == NULL &&
234352a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
234452a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
23455244eac9SBenno Rice 			/*
23465244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
23475244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
23485244eac9SBenno Rice 			 */
234952a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
23505244eac9SBenno Rice 
23515244eac9SBenno Rice 			if (j >= 0) {
23525244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
235359276937SPeter Grehan 				moea_pte_overflow--;
235459276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
23555244eac9SBenno Rice 				return (1);
23565244eac9SBenno Rice 			}
23575244eac9SBenno Rice 
23585244eac9SBenno Rice 			source_pvo = pvo;
23595244eac9SBenno Rice 
23605244eac9SBenno Rice 			if (victim_pvo != NULL)
23615244eac9SBenno Rice 				break;
23625244eac9SBenno Rice 		}
23635244eac9SBenno Rice 
23645244eac9SBenno Rice 		/*
23655244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
23665244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
23675244eac9SBenno Rice 		 */
23685244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
236952a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
23705244eac9SBenno Rice 			victim_pvo = pvo;
23715244eac9SBenno Rice 			if (source_pvo != NULL)
23725244eac9SBenno Rice 				break;
23735244eac9SBenno Rice 		}
23745244eac9SBenno Rice 	}
23755244eac9SBenno Rice 
2376f489bf21SAlan Cox 	if (source_pvo == NULL) {
237759276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
23785244eac9SBenno Rice 		return (0);
2379f489bf21SAlan Cox 	}
23805244eac9SBenno Rice 
23815244eac9SBenno Rice 	if (victim_pvo == NULL) {
23825244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
238359276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
23845244eac9SBenno Rice 			    "entry", pt);
23855244eac9SBenno Rice 
23865244eac9SBenno Rice 		/*
23875244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
23885244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
23895244eac9SBenno Rice 		 */
239059276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
23915244eac9SBenno Rice 		    pvo_olink) {
23925244eac9SBenno Rice 			/*
23935244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
23945244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
23955244eac9SBenno Rice 			 */
239652a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
23975244eac9SBenno Rice 				victim_pvo = pvo;
23985244eac9SBenno Rice 				break;
23995244eac9SBenno Rice 			}
24005244eac9SBenno Rice 		}
24015244eac9SBenno Rice 
24025244eac9SBenno Rice 		if (victim_pvo == NULL)
240359276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
24045244eac9SBenno Rice 			    "entry", pt);
24055244eac9SBenno Rice 	}
24065244eac9SBenno Rice 
24075244eac9SBenno Rice 	/*
24085244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
24095244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
24105244eac9SBenno Rice 	 * contained in the TLB entry.
24115244eac9SBenno Rice 	 */
241252a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
24135244eac9SBenno Rice 
241452a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
241552a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
24165244eac9SBenno Rice 
24175244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
24185244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
241959276937SPeter Grehan 	moea_pte_replacements++;
24205244eac9SBenno Rice 
242159276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
24225244eac9SBenno Rice 	return (1);
24235244eac9SBenno Rice }
24245244eac9SBenno Rice 
2425804d1cc1SJustin Hibbits static __inline struct pvo_entry *
2426804d1cc1SJustin Hibbits moea_pte_spillable_ident(u_int ptegidx)
2427804d1cc1SJustin Hibbits {
2428804d1cc1SJustin Hibbits 	struct	pte *pt;
2429804d1cc1SJustin Hibbits 	struct	pvo_entry *pvo_walk, *pvo = NULL;
2430804d1cc1SJustin Hibbits 
2431804d1cc1SJustin Hibbits 	LIST_FOREACH(pvo_walk, &moea_pvo_table[ptegidx], pvo_olink) {
2432804d1cc1SJustin Hibbits 		if (pvo_walk->pvo_vaddr & PVO_WIRED)
2433804d1cc1SJustin Hibbits 			continue;
2434804d1cc1SJustin Hibbits 
2435804d1cc1SJustin Hibbits 		if (!(pvo_walk->pvo_pte.pte.pte_hi & PTE_VALID))
2436804d1cc1SJustin Hibbits 			continue;
2437804d1cc1SJustin Hibbits 
2438804d1cc1SJustin Hibbits 		pt = moea_pvo_to_pte(pvo_walk, -1);
2439804d1cc1SJustin Hibbits 
2440804d1cc1SJustin Hibbits 		if (pt == NULL)
2441804d1cc1SJustin Hibbits 			continue;
2442804d1cc1SJustin Hibbits 
2443804d1cc1SJustin Hibbits 		pvo = pvo_walk;
2444804d1cc1SJustin Hibbits 
2445804d1cc1SJustin Hibbits 		mtx_unlock(&moea_table_mutex);
2446804d1cc1SJustin Hibbits 		if (!(pt->pte_lo & PTE_REF))
2447804d1cc1SJustin Hibbits 			return (pvo_walk);
2448804d1cc1SJustin Hibbits 	}
2449804d1cc1SJustin Hibbits 
2450804d1cc1SJustin Hibbits 	return (pvo);
2451804d1cc1SJustin Hibbits }
2452804d1cc1SJustin Hibbits 
24535244eac9SBenno Rice static int
245459276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
24555244eac9SBenno Rice {
24565244eac9SBenno Rice 	struct	pte *pt;
2457804d1cc1SJustin Hibbits 	struct	pvo_entry *victim_pvo;
24585244eac9SBenno Rice 	int	i;
2459804d1cc1SJustin Hibbits 	int	victim_idx;
2460804d1cc1SJustin Hibbits 	u_int	pteg_bkpidx = ptegidx;
24615244eac9SBenno Rice 
2462d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2463d644a0b7SAlan Cox 
24645244eac9SBenno Rice 	/*
24655244eac9SBenno Rice 	 * First try primary hash.
24665244eac9SBenno Rice 	 */
246759276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
24685244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
24695244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
247059276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
24715244eac9SBenno Rice 			return (i);
24725244eac9SBenno Rice 		}
24735244eac9SBenno Rice 	}
24745244eac9SBenno Rice 
24755244eac9SBenno Rice 	/*
24765244eac9SBenno Rice 	 * Now try secondary hash.
24775244eac9SBenno Rice 	 */
247859276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2479bd8e6f87SPeter Grehan 
248059276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
24815244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
24825244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
248359276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
24845244eac9SBenno Rice 			return (i);
24855244eac9SBenno Rice 		}
24865244eac9SBenno Rice 	}
24875244eac9SBenno Rice 
2488804d1cc1SJustin Hibbits 	/* Try again, but this time try to force a PTE out. */
2489804d1cc1SJustin Hibbits 	ptegidx = pteg_bkpidx;
2490804d1cc1SJustin Hibbits 
2491804d1cc1SJustin Hibbits 	victim_pvo = moea_pte_spillable_ident(ptegidx);
2492804d1cc1SJustin Hibbits 	if (victim_pvo == NULL) {
2493804d1cc1SJustin Hibbits 		ptegidx ^= moea_pteg_mask;
2494804d1cc1SJustin Hibbits 		victim_pvo = moea_pte_spillable_ident(ptegidx);
2495804d1cc1SJustin Hibbits 	}
2496804d1cc1SJustin Hibbits 
2497804d1cc1SJustin Hibbits 	if (victim_pvo == NULL) {
249859276937SPeter Grehan 		panic("moea_pte_insert: overflow");
24995244eac9SBenno Rice 		return (-1);
25005244eac9SBenno Rice 	}
25015244eac9SBenno Rice 
2502804d1cc1SJustin Hibbits 	victim_idx = moea_pvo_pte_index(victim_pvo, ptegidx);
2503804d1cc1SJustin Hibbits 
2504804d1cc1SJustin Hibbits 	if (pteg_bkpidx == ptegidx)
2505804d1cc1SJustin Hibbits 		pvo_pt->pte_hi &= ~PTE_HID;
2506804d1cc1SJustin Hibbits 	else
2507804d1cc1SJustin Hibbits 		pvo_pt->pte_hi |= PTE_HID;
2508804d1cc1SJustin Hibbits 
2509804d1cc1SJustin Hibbits 	/*
2510804d1cc1SJustin Hibbits 	 * Synchronize the sacrifice PTE with its PVO, then mark both
2511804d1cc1SJustin Hibbits 	 * invalid. The PVO will be reused when/if the VM system comes
2512804d1cc1SJustin Hibbits 	 * here after a fault.
2513804d1cc1SJustin Hibbits 	 */
2514804d1cc1SJustin Hibbits 	pt = &moea_pteg_table[victim_idx >> 3].pt[victim_idx & 7];
2515804d1cc1SJustin Hibbits 
2516804d1cc1SJustin Hibbits 	if (pt->pte_hi != victim_pvo->pvo_pte.pte.pte_hi)
2517804d1cc1SJustin Hibbits 	    panic("Victim PVO doesn't match PTE! PVO: %8x, PTE: %8x", victim_pvo->pvo_pte.pte.pte_hi, pt->pte_hi);
2518804d1cc1SJustin Hibbits 
2519804d1cc1SJustin Hibbits 	/*
2520804d1cc1SJustin Hibbits 	 * Set the new PTE.
2521804d1cc1SJustin Hibbits 	 */
2522804d1cc1SJustin Hibbits 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
2523804d1cc1SJustin Hibbits 	PVO_PTEGIDX_CLR(victim_pvo);
2524804d1cc1SJustin Hibbits 	moea_pte_overflow++;
2525804d1cc1SJustin Hibbits 	moea_pte_set(pt, pvo_pt);
2526804d1cc1SJustin Hibbits 
2527804d1cc1SJustin Hibbits 	return (victim_idx & 7);
2528804d1cc1SJustin Hibbits }
2529804d1cc1SJustin Hibbits 
25305244eac9SBenno Rice static boolean_t
253159276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
25325244eac9SBenno Rice {
25335244eac9SBenno Rice 	struct	pvo_entry *pvo;
25345244eac9SBenno Rice 	struct	pte *pt;
25355244eac9SBenno Rice 
25368d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
253759276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
25385244eac9SBenno Rice 		return (TRUE);
25395244eac9SBenno Rice 
25405244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
25415244eac9SBenno Rice 		/*
25425244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
25435244eac9SBenno Rice 		 * success.
25445244eac9SBenno Rice 		 */
254552a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
254659276937SPeter Grehan 			moea_attr_save(m, ptebit);
25475244eac9SBenno Rice 			return (TRUE);
25485244eac9SBenno Rice 		}
25495244eac9SBenno Rice 	}
25505244eac9SBenno Rice 
25515244eac9SBenno Rice 	/*
25525244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
25535244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
25545244eac9SBenno Rice 	 * the PTEs.
25555244eac9SBenno Rice 	 */
2556e4f72b32SMarcel Moolenaar 	powerpc_sync();
25575244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
25585244eac9SBenno Rice 		/*
25595244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
25605244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
25615244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
25625244eac9SBenno Rice 		 */
256359276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
25645244eac9SBenno Rice 		if (pt != NULL) {
256552a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2566d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
256752a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
256859276937SPeter Grehan 				moea_attr_save(m, ptebit);
25695244eac9SBenno Rice 				return (TRUE);
25705244eac9SBenno Rice 			}
25715244eac9SBenno Rice 		}
25725244eac9SBenno Rice 	}
25735244eac9SBenno Rice 
25744f7daed0SAndrew Gallatin 	return (FALSE);
25755244eac9SBenno Rice }
25765244eac9SBenno Rice 
257703b6e025SPeter Grehan static u_int
2578ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
25795244eac9SBenno Rice {
258003b6e025SPeter Grehan 	u_int	count;
25815244eac9SBenno Rice 	struct	pvo_entry *pvo;
25825244eac9SBenno Rice 	struct	pte *pt;
2583ce186587SAlan Cox 
25848d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
25855244eac9SBenno Rice 
25865244eac9SBenno Rice 	/*
25875244eac9SBenno Rice 	 * Clear the cached value.
25885244eac9SBenno Rice 	 */
258959276937SPeter Grehan 	moea_attr_clear(m, ptebit);
25905244eac9SBenno Rice 
25915244eac9SBenno Rice 	/*
25925244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
25935244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
25945244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
25955244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
25965244eac9SBenno Rice 	 * REF/CHG bits.
25975244eac9SBenno Rice 	 */
2598e4f72b32SMarcel Moolenaar 	powerpc_sync();
25995244eac9SBenno Rice 
26005244eac9SBenno Rice 	/*
26015244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
26025244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
26035244eac9SBenno Rice 	 */
260403b6e025SPeter Grehan 	count = 0;
26055244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
260659276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
26075244eac9SBenno Rice 		if (pt != NULL) {
260852a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
260952a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
261003b6e025SPeter Grehan 				count++;
261159276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
26125244eac9SBenno Rice 			}
2613d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
261403b6e025SPeter Grehan 		}
261552a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
26165244eac9SBenno Rice 	}
26175244eac9SBenno Rice 
261803b6e025SPeter Grehan 	return (count);
2619bdf71f56SBenno Rice }
26208bbfa33aSBenno Rice 
26218bbfa33aSBenno Rice /*
262232bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
262332bc7846SPeter Grehan  */
262432bc7846SPeter Grehan static int
26250936003eSJustin Hibbits moea_bat_mapped(int idx, vm_paddr_t pa, vm_size_t size)
262632bc7846SPeter Grehan {
262732bc7846SPeter Grehan 	u_int prot;
262832bc7846SPeter Grehan 	u_int32_t start;
262932bc7846SPeter Grehan 	u_int32_t end;
263032bc7846SPeter Grehan 	u_int32_t bat_ble;
263132bc7846SPeter Grehan 
263232bc7846SPeter Grehan 	/*
263332bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
263432bc7846SPeter Grehan 	 */
2635c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
263632bc7846SPeter Grehan 		return (EINVAL);
263732bc7846SPeter Grehan 
263832bc7846SPeter Grehan 	/*
263932bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
264032bc7846SPeter Grehan 	 * so it can function as an i/o page
264132bc7846SPeter Grehan 	 */
264232bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
264332bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
264432bc7846SPeter Grehan 		return (EPERM);
264532bc7846SPeter Grehan 
264632bc7846SPeter Grehan 	/*
264732bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
264832bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
264932bc7846SPeter Grehan 	 * not requiring masking)
265032bc7846SPeter Grehan 	 */
265132bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
265232bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
265332bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
265432bc7846SPeter Grehan 
265532bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
265632bc7846SPeter Grehan 		return (ERANGE);
265732bc7846SPeter Grehan 
265832bc7846SPeter Grehan 	return (0);
265932bc7846SPeter Grehan }
266032bc7846SPeter Grehan 
266159276937SPeter Grehan boolean_t
266245b69dd6SJustin Hibbits moea_dev_direct_mapped(vm_paddr_t pa, vm_size_t size)
2663c0763d37SSuleiman Souhlal {
2664c0763d37SSuleiman Souhlal 	int i;
2665c0763d37SSuleiman Souhlal 
2666c0763d37SSuleiman Souhlal 	/*
2667c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2668c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2669c0763d37SSuleiman Souhlal 	 */
2670c0763d37SSuleiman Souhlal 
2671c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
267259276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2673c0763d37SSuleiman Souhlal 			return (0);
2674c0763d37SSuleiman Souhlal 
2675c0763d37SSuleiman Souhlal 	return (EFAULT);
2676c0763d37SSuleiman Souhlal }
267732bc7846SPeter Grehan 
267832bc7846SPeter Grehan /*
26798bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
26808bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
26818bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
26828bbfa33aSBenno Rice  * NOT real memory.
26838bbfa33aSBenno Rice  */
26848bbfa33aSBenno Rice void *
268545b69dd6SJustin Hibbits moea_mapdev(vm_paddr_t pa, vm_size_t size)
26868bbfa33aSBenno Rice {
2687c1f4123bSNathan Whitehorn 
268845b69dd6SJustin Hibbits 	return (moea_mapdev_attr(pa, size, VM_MEMATTR_DEFAULT));
2689c1f4123bSNathan Whitehorn }
2690c1f4123bSNathan Whitehorn 
2691c1f4123bSNathan Whitehorn void *
269245b69dd6SJustin Hibbits moea_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
2693c1f4123bSNathan Whitehorn {
269432bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
269532bc7846SPeter Grehan 	int i;
26968bbfa33aSBenno Rice 
269732bc7846SPeter Grehan 	ppa = trunc_page(pa);
26988bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
26998bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
27008bbfa33aSBenno Rice 
270132bc7846SPeter Grehan 	/*
270232bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
270332bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
270432bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
270532bc7846SPeter Grehan 	 */
270632bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
270759276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
270832bc7846SPeter Grehan 			return ((void *) pa);
270932bc7846SPeter Grehan 	}
271032bc7846SPeter Grehan 
27115df87b21SJeff Roberson 	va = kva_alloc(size);
27128bbfa33aSBenno Rice 	if (!va)
271359276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
27148bbfa33aSBenno Rice 
27158bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
271645b69dd6SJustin Hibbits 		moea_kenter_attr(tmpva, ppa, ma);
2717e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
27188bbfa33aSBenno Rice 		size -= PAGE_SIZE;
27198bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
272032bc7846SPeter Grehan 		ppa += PAGE_SIZE;
27218bbfa33aSBenno Rice 	}
27228bbfa33aSBenno Rice 
27238bbfa33aSBenno Rice 	return ((void *)(va + offset));
27248bbfa33aSBenno Rice }
27258bbfa33aSBenno Rice 
27268bbfa33aSBenno Rice void
272745b69dd6SJustin Hibbits moea_unmapdev(vm_offset_t va, vm_size_t size)
27288bbfa33aSBenno Rice {
27298bbfa33aSBenno Rice 	vm_offset_t base, offset;
27308bbfa33aSBenno Rice 
273132bc7846SPeter Grehan 	/*
273232bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
273332bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
273432bc7846SPeter Grehan 	 */
2735ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
27368bbfa33aSBenno Rice 		base = trunc_page(va);
27378bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
27388bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
27394ae224c6SConrad Meyer 		moea_qremove(base, atop(size));
27405df87b21SJeff Roberson 		kva_free(base, size);
27418bbfa33aSBenno Rice 	}
274232bc7846SPeter Grehan }
27431a4fcaebSMarcel Moolenaar 
27441a4fcaebSMarcel Moolenaar static void
274545b69dd6SJustin Hibbits moea_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
27461a4fcaebSMarcel Moolenaar {
27471a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
27481a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
27491a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
27501a4fcaebSMarcel Moolenaar 	vm_size_t len;
27511a4fcaebSMarcel Moolenaar 
27521a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
27531a4fcaebSMarcel Moolenaar 	while (sz > 0) {
2754a11dc32eSJustin Hibbits 		lim = round_page(va + 1);
27551a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
27561a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
27571a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
27587be655c2SBrandon Bergren 			pa = PVO_PADDR(pvo) | (va & ADDR_POFF);
27591a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
27601a4fcaebSMarcel Moolenaar 		}
27611a4fcaebSMarcel Moolenaar 		va += len;
27621a4fcaebSMarcel Moolenaar 		sz -= len;
27631a4fcaebSMarcel Moolenaar 	}
27641a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
27651a4fcaebSMarcel Moolenaar }
2766afd9cb6cSJustin Hibbits 
2767bdb9ab0dSMark Johnston void
276845b69dd6SJustin Hibbits moea_dumpsys_map(vm_paddr_t pa, size_t sz, void **va)
2769afd9cb6cSJustin Hibbits {
2770bdb9ab0dSMark Johnston 
2771bdb9ab0dSMark Johnston 	*va = (void *)pa;
2772afd9cb6cSJustin Hibbits }
2773afd9cb6cSJustin Hibbits 
2774bdb9ab0dSMark Johnston extern struct dump_pa dump_map[PHYS_AVAIL_SZ + 1];
2775bdb9ab0dSMark Johnston 
2776bdb9ab0dSMark Johnston void
277745b69dd6SJustin Hibbits moea_scan_init()
2778afd9cb6cSJustin Hibbits {
2779afd9cb6cSJustin Hibbits 	struct pvo_entry *pvo;
2780afd9cb6cSJustin Hibbits 	vm_offset_t va;
2781bdb9ab0dSMark Johnston 	int i;
2782afd9cb6cSJustin Hibbits 
2783bdb9ab0dSMark Johnston 	if (!do_minidump) {
2784bdb9ab0dSMark Johnston 		/* Initialize phys. segments for dumpsys(). */
2785bdb9ab0dSMark Johnston 		memset(&dump_map, 0, sizeof(dump_map));
2786bdb9ab0dSMark Johnston 		mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
2787bdb9ab0dSMark Johnston 		for (i = 0; i < pregions_sz; i++) {
2788bdb9ab0dSMark Johnston 			dump_map[i].pa_start = pregions[i].mr_start;
2789bdb9ab0dSMark Johnston 			dump_map[i].pa_size = pregions[i].mr_size;
2790afd9cb6cSJustin Hibbits 		}
2791bdb9ab0dSMark Johnston 		return;
2792bdb9ab0dSMark Johnston 	}
2793bdb9ab0dSMark Johnston 
2794bdb9ab0dSMark Johnston 	/* Virtual segments for minidumps: */
2795bdb9ab0dSMark Johnston 	memset(&dump_map, 0, sizeof(dump_map));
2796bdb9ab0dSMark Johnston 
2797bdb9ab0dSMark Johnston 	/* 1st: kernel .data and .bss. */
2798bdb9ab0dSMark Johnston 	dump_map[0].pa_start = trunc_page((uintptr_t)_etext);
2799bdb9ab0dSMark Johnston 	dump_map[0].pa_size =
2800bdb9ab0dSMark Johnston 	    round_page((uintptr_t)_end) - dump_map[0].pa_start;
2801bdb9ab0dSMark Johnston 
2802afd9cb6cSJustin Hibbits 	/* 2nd: msgbuf and tables (see pmap_bootstrap()). */
2803bdb9ab0dSMark Johnston 	dump_map[1].pa_start = (vm_paddr_t)msgbufp->msg_ptr;
2804bdb9ab0dSMark Johnston 	dump_map[1].pa_size = round_page(msgbufp->msg_size);
2805bdb9ab0dSMark Johnston 
2806afd9cb6cSJustin Hibbits 	/* 3rd: kernel VM. */
2807bdb9ab0dSMark Johnston 	va = dump_map[1].pa_start + dump_map[1].pa_size;
2808afd9cb6cSJustin Hibbits 	/* Find start of next chunk (from va). */
2809afd9cb6cSJustin Hibbits 	while (va < virtual_end) {
2810afd9cb6cSJustin Hibbits 		/* Don't dump the buffer cache. */
2811bdb9ab0dSMark Johnston 		if (va >= kmi.buffer_sva && va < kmi.buffer_eva) {
2812afd9cb6cSJustin Hibbits 			va = kmi.buffer_eva;
2813afd9cb6cSJustin Hibbits 			continue;
2814afd9cb6cSJustin Hibbits 		}
2815bdb9ab0dSMark Johnston 		pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
2816bdb9ab0dSMark Johnston 		if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID))
2817afd9cb6cSJustin Hibbits 			break;
2818afd9cb6cSJustin Hibbits 		va += PAGE_SIZE;
2819afd9cb6cSJustin Hibbits 	}
2820afd9cb6cSJustin Hibbits 	if (va < virtual_end) {
2821bdb9ab0dSMark Johnston 		dump_map[2].pa_start = va;
2822afd9cb6cSJustin Hibbits 		va += PAGE_SIZE;
2823afd9cb6cSJustin Hibbits 		/* Find last page in chunk. */
2824afd9cb6cSJustin Hibbits 		while (va < virtual_end) {
2825afd9cb6cSJustin Hibbits 			/* Don't run into the buffer cache. */
2826afd9cb6cSJustin Hibbits 			if (va == kmi.buffer_sva)
2827afd9cb6cSJustin Hibbits 				break;
2828bdb9ab0dSMark Johnston 			pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF,
2829bdb9ab0dSMark Johnston 			    NULL);
2830afd9cb6cSJustin Hibbits 			if (pvo == NULL ||
2831afd9cb6cSJustin Hibbits 			    !(pvo->pvo_pte.pte.pte_hi & PTE_VALID))
2832afd9cb6cSJustin Hibbits 				break;
2833afd9cb6cSJustin Hibbits 			va += PAGE_SIZE;
2834afd9cb6cSJustin Hibbits 		}
2835bdb9ab0dSMark Johnston 		dump_map[2].pa_size = va - dump_map[2].pa_start;
2836afd9cb6cSJustin Hibbits 	}
2837afd9cb6cSJustin Hibbits }
2838