xref: /freebsd/sys/powerpc/aim/mmu_oea.c (revision 5cff1f4dc3b22c87bfba0a7ac91e8857dc091a60)
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>
1175244eac9SBenno Rice #include <sys/msgbuf.h>
118f9bac91bSBenno Rice #include <sys/mutex.h>
1195244eac9SBenno Rice #include <sys/proc.h>
1203653f5cbSAlan Cox #include <sys/rwlock.h>
121c47dd3dbSAttilio Rao #include <sys/sched.h>
1225244eac9SBenno Rice #include <sys/sysctl.h>
1235244eac9SBenno Rice #include <sys/systm.h>
1245244eac9SBenno Rice #include <sys/vmmeter.h>
1255244eac9SBenno Rice 
1265244eac9SBenno Rice #include <dev/ofw/openfirm.h>
127f9bac91bSBenno Rice 
128f9bac91bSBenno Rice #include <vm/vm.h>
129f9bac91bSBenno Rice #include <vm/vm_param.h>
130f9bac91bSBenno Rice #include <vm/vm_kern.h>
131f9bac91bSBenno Rice #include <vm/vm_page.h>
132f9bac91bSBenno Rice #include <vm/vm_map.h>
133f9bac91bSBenno Rice #include <vm/vm_object.h>
134f9bac91bSBenno Rice #include <vm/vm_extern.h>
13521943937SJeff Roberson #include <vm/vm_page.h>
13621943937SJeff Roberson #include <vm/vm_phys.h>
137f9bac91bSBenno Rice #include <vm/vm_pageout.h>
138378862a7SJeff Roberson #include <vm/uma.h>
139f9bac91bSBenno Rice 
1407c277971SPeter Grehan #include <machine/cpu.h>
141b40ce02aSNathan Whitehorn #include <machine/platform.h>
142d699b539SMark Peek #include <machine/bat.h>
1435244eac9SBenno Rice #include <machine/frame.h>
1445244eac9SBenno Rice #include <machine/md_var.h>
1455244eac9SBenno Rice #include <machine/psl.h>
146f9bac91bSBenno Rice #include <machine/pte.h>
14712640815SMarcel Moolenaar #include <machine/smp.h>
1485244eac9SBenno Rice #include <machine/sr.h>
14959276937SPeter Grehan #include <machine/mmuvar.h>
150258dbffeSNathan Whitehorn #include <machine/trap.h>
151f9bac91bSBenno Rice 
15259276937SPeter Grehan #include "mmu_if.h"
15359276937SPeter Grehan 
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 
1625244eac9SBenno Rice struct ofw_map {
1635244eac9SBenno Rice 	vm_offset_t	om_va;
1645244eac9SBenno Rice 	vm_size_t	om_len;
1655244eac9SBenno Rice 	vm_offset_t	om_pa;
1665244eac9SBenno Rice 	u_int		om_mode;
1675244eac9SBenno Rice };
168f9bac91bSBenno Rice 
169afd9cb6cSJustin Hibbits extern unsigned char _etext[];
170afd9cb6cSJustin Hibbits extern unsigned char _end[];
171afd9cb6cSJustin Hibbits 
1725244eac9SBenno Rice /*
1735244eac9SBenno Rice  * Map of physical memory regions.
1745244eac9SBenno Rice  */
17531c82d03SBenno Rice static struct	mem_region *regions;
17631c82d03SBenno Rice static struct	mem_region *pregions;
177c3e289e1SNathan Whitehorn static u_int    phys_avail_count;
178c3e289e1SNathan Whitehorn static int	regions_sz, pregions_sz;
179aa39961eSBenno Rice static struct	ofw_map *translations;
1805244eac9SBenno Rice 
181f9bac91bSBenno Rice /*
182f489bf21SAlan Cox  * Lock for the pteg and pvo tables.
183f489bf21SAlan Cox  */
18459276937SPeter Grehan struct mtx	moea_table_mutex;
185e9b5f218SNathan Whitehorn struct mtx	moea_vsid_mutex;
186f489bf21SAlan Cox 
187e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */
188e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx;
189e4f72b32SMarcel Moolenaar 
190f489bf21SAlan Cox /*
1915244eac9SBenno Rice  * PTEG data.
192f9bac91bSBenno Rice  */
19359276937SPeter Grehan static struct	pteg *moea_pteg_table;
19459276937SPeter Grehan u_int		moea_pteg_count;
19559276937SPeter Grehan u_int		moea_pteg_mask;
1965244eac9SBenno Rice 
1975244eac9SBenno Rice /*
1985244eac9SBenno Rice  * PVO data.
1995244eac9SBenno Rice  */
20059276937SPeter Grehan struct	pvo_head *moea_pvo_table;		/* pvo entries by pteg index */
20159276937SPeter Grehan struct	pvo_head moea_pvo_kunmanaged =
20259276937SPeter Grehan     LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged);	/* list of unmanaged pages */
2035244eac9SBenno Rice 
204cfedf924SAttilio Rao static struct rwlock_padalign pvh_global_lock;
2053653f5cbSAlan Cox 
20659276937SPeter Grehan uma_zone_t	moea_upvo_zone;	/* zone for pvo entries for unmanaged pages */
20759276937SPeter Grehan uma_zone_t	moea_mpvo_zone;	/* zone for pvo entries for managed pages */
2085244eac9SBenno Rice 
2090d290675SBenno Rice #define	BPVO_POOL_SIZE	32768
21059276937SPeter Grehan static struct	pvo_entry *moea_bpvo_pool;
21159276937SPeter Grehan static int	moea_bpvo_pool_index = 0;
2125244eac9SBenno Rice 
2135244eac9SBenno Rice #define	VSID_NBPW	(sizeof(u_int32_t) * 8)
21459276937SPeter Grehan static u_int	moea_vsid_bitmap[NPMAPS / VSID_NBPW];
2155244eac9SBenno Rice 
21659276937SPeter Grehan static boolean_t moea_initialized = FALSE;
2175244eac9SBenno Rice 
2185244eac9SBenno Rice /*
2195244eac9SBenno Rice  * Statistics.
2205244eac9SBenno Rice  */
22159276937SPeter Grehan u_int	moea_pte_valid = 0;
22259276937SPeter Grehan u_int	moea_pte_overflow = 0;
22359276937SPeter Grehan u_int	moea_pte_replacements = 0;
22459276937SPeter Grehan u_int	moea_pvo_entries = 0;
22559276937SPeter Grehan u_int	moea_pvo_enter_calls = 0;
22659276937SPeter Grehan u_int	moea_pvo_remove_calls = 0;
22759276937SPeter Grehan u_int	moea_pte_spills = 0;
22859276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid,
2295244eac9SBenno Rice     0, "");
23059276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD,
23159276937SPeter Grehan     &moea_pte_overflow, 0, "");
23259276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD,
23359276937SPeter Grehan     &moea_pte_replacements, 0, "");
23459276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries,
2355244eac9SBenno Rice     0, "");
23659276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD,
23759276937SPeter Grehan     &moea_pvo_enter_calls, 0, "");
23859276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD,
23959276937SPeter Grehan     &moea_pvo_remove_calls, 0, "");
24059276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD,
24159276937SPeter Grehan     &moea_pte_spills, 0, "");
2425244eac9SBenno Rice 
2435244eac9SBenno Rice /*
24459276937SPeter Grehan  * Allocate physical memory for use in moea_bootstrap.
2455244eac9SBenno Rice  */
24659276937SPeter Grehan static vm_offset_t	moea_bootstrap_alloc(vm_size_t, u_int);
2475244eac9SBenno Rice 
2485244eac9SBenno Rice /*
2495244eac9SBenno Rice  * PTE calls.
2505244eac9SBenno Rice  */
25159276937SPeter Grehan static int		moea_pte_insert(u_int, struct pte *);
2525244eac9SBenno Rice 
2535244eac9SBenno Rice /*
2545244eac9SBenno Rice  * PVO calls.
2555244eac9SBenno Rice  */
25659276937SPeter Grehan static int	moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *,
2570936003eSJustin Hibbits 		    vm_offset_t, vm_paddr_t, u_int, int);
25859276937SPeter Grehan static void	moea_pvo_remove(struct pvo_entry *, int);
25959276937SPeter Grehan static struct	pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *);
26059276937SPeter Grehan static struct	pte *moea_pvo_to_pte(const struct pvo_entry *, int);
2615244eac9SBenno Rice 
2625244eac9SBenno Rice /*
2635244eac9SBenno Rice  * Utility routines.
2645244eac9SBenno Rice  */
26539ffa8c1SKonstantin Belousov static int		moea_enter_locked(pmap_t, vm_offset_t, vm_page_t,
26639ffa8c1SKonstantin Belousov 			    vm_prot_t, u_int, int8_t);
2670936003eSJustin Hibbits static void		moea_syncicache(vm_paddr_t, vm_size_t);
26859276937SPeter Grehan static boolean_t	moea_query_bit(vm_page_t, int);
269ce186587SAlan Cox static u_int		moea_clear_bit(vm_page_t, int);
27059276937SPeter Grehan static void		moea_kremove(mmu_t, vm_offset_t);
27159276937SPeter Grehan int		moea_pte_spill(vm_offset_t);
27259276937SPeter Grehan 
27359276937SPeter Grehan /*
27459276937SPeter Grehan  * Kernel MMU interface
27559276937SPeter Grehan  */
27659276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t);
27759276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t);
278e8a4a618SKonstantin Belousov void moea_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset,
279e8a4a618SKonstantin Belousov     vm_page_t *mb, vm_offset_t b_offset, int xfersize);
28039ffa8c1SKonstantin Belousov int moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, u_int,
28139ffa8c1SKonstantin Belousov     int8_t);
282ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
283ce142d9eSAlan Cox     vm_prot_t);
2842053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
28559276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t);
28659276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t);
28759276937SPeter Grehan void moea_init(mmu_t);
28859276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t);
289e396eb60SAlan Cox boolean_t moea_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
2907b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t);
2918d9e6d9fSAlan Cox int moea_ts_referenced(mmu_t, vm_page_t);
29220b79612SRafal Jaworowski vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_paddr_t, vm_paddr_t, int);
29359276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t);
294b999e9c8SMark Johnston void moea_page_init(mmu_t, vm_page_t);
29559677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t);
29659276937SPeter Grehan void moea_pinit(mmu_t, pmap_t);
29759276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t);
29859276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
29959276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
30059276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int);
30159276937SPeter Grehan void moea_release(mmu_t, pmap_t);
30259276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
30359276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t);
30478985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t);
305a844c68fSAlan Cox void moea_unwire(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
30659276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t);
30759276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int);
30859276937SPeter Grehan void moea_activate(mmu_t, struct thread *);
30959276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *);
3101c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int);
31159276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
31220b79612SRafal Jaworowski void *moea_mapdev(mmu_t, vm_paddr_t, vm_size_t);
3130936003eSJustin Hibbits void *moea_mapdev_attr(mmu_t, vm_paddr_t, vm_size_t, vm_memattr_t);
31459276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t);
31520b79612SRafal Jaworowski vm_paddr_t moea_kextract(mmu_t, vm_offset_t);
3160936003eSJustin Hibbits void moea_kenter_attr(mmu_t, vm_offset_t, vm_paddr_t, vm_memattr_t);
31720b79612SRafal Jaworowski void moea_kenter(mmu_t, vm_offset_t, vm_paddr_t);
318c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma);
31920b79612SRafal Jaworowski boolean_t moea_dev_direct_mapped(mmu_t, vm_paddr_t, vm_size_t);
3201a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t);
321bdb9ab0dSMark Johnston void moea_dumpsys_map(mmu_t mmu, vm_paddr_t pa, size_t sz, void **va);
322bdb9ab0dSMark Johnston void moea_scan_init(mmu_t mmu);
323713841afSJason A. Harmening vm_offset_t moea_quick_enter_page(mmu_t mmu, vm_page_t m);
324713841afSJason A. Harmening void moea_quick_remove_page(mmu_t mmu, vm_offset_t addr);
32504329fa7SNathan Whitehorn static int moea_map_user_ptr(mmu_t mmu, pmap_t pm,
32604329fa7SNathan Whitehorn     volatile const void *uaddr, void **kaddr, size_t ulen, size_t *klen);
327eb1baf72SNathan Whitehorn static int moea_decode_kernel_ptr(mmu_t mmu, vm_offset_t addr,
328eb1baf72SNathan Whitehorn     int *is_user, vm_offset_t *decoded_addr);
32904329fa7SNathan Whitehorn 
33059276937SPeter Grehan 
33159276937SPeter Grehan static mmu_method_t moea_methods[] = {
33259276937SPeter Grehan 	MMUMETHOD(mmu_clear_modify,	moea_clear_modify),
33359276937SPeter Grehan 	MMUMETHOD(mmu_copy_page,	moea_copy_page),
334e8a4a618SKonstantin Belousov 	MMUMETHOD(mmu_copy_pages,	moea_copy_pages),
33559276937SPeter Grehan 	MMUMETHOD(mmu_enter,		moea_enter),
336ce142d9eSAlan Cox 	MMUMETHOD(mmu_enter_object,	moea_enter_object),
33759276937SPeter Grehan 	MMUMETHOD(mmu_enter_quick,	moea_enter_quick),
33859276937SPeter Grehan 	MMUMETHOD(mmu_extract,		moea_extract),
33959276937SPeter Grehan 	MMUMETHOD(mmu_extract_and_hold,	moea_extract_and_hold),
34059276937SPeter Grehan 	MMUMETHOD(mmu_init,		moea_init),
34159276937SPeter Grehan 	MMUMETHOD(mmu_is_modified,	moea_is_modified),
342e396eb60SAlan Cox 	MMUMETHOD(mmu_is_prefaultable,	moea_is_prefaultable),
3437b85f591SAlan Cox 	MMUMETHOD(mmu_is_referenced,	moea_is_referenced),
34459276937SPeter Grehan 	MMUMETHOD(mmu_ts_referenced,	moea_ts_referenced),
34559276937SPeter Grehan 	MMUMETHOD(mmu_map,     		moea_map),
34659276937SPeter Grehan 	MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick),
347b999e9c8SMark Johnston 	MMUMETHOD(mmu_page_init,	moea_page_init),
34859677d3cSAlan Cox 	MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings),
34959276937SPeter Grehan 	MMUMETHOD(mmu_pinit,		moea_pinit),
35059276937SPeter Grehan 	MMUMETHOD(mmu_pinit0,		moea_pinit0),
35159276937SPeter Grehan 	MMUMETHOD(mmu_protect,		moea_protect),
35259276937SPeter Grehan 	MMUMETHOD(mmu_qenter,		moea_qenter),
35359276937SPeter Grehan 	MMUMETHOD(mmu_qremove,		moea_qremove),
35459276937SPeter Grehan 	MMUMETHOD(mmu_release,		moea_release),
35559276937SPeter Grehan 	MMUMETHOD(mmu_remove,		moea_remove),
35659276937SPeter Grehan 	MMUMETHOD(mmu_remove_all,      	moea_remove_all),
35778985e42SAlan Cox 	MMUMETHOD(mmu_remove_write,	moea_remove_write),
3581a4fcaebSMarcel Moolenaar 	MMUMETHOD(mmu_sync_icache,	moea_sync_icache),
359a844c68fSAlan Cox 	MMUMETHOD(mmu_unwire,		moea_unwire),
36059276937SPeter Grehan 	MMUMETHOD(mmu_zero_page,       	moea_zero_page),
36159276937SPeter Grehan 	MMUMETHOD(mmu_zero_page_area,	moea_zero_page_area),
36259276937SPeter Grehan 	MMUMETHOD(mmu_activate,		moea_activate),
36359276937SPeter Grehan 	MMUMETHOD(mmu_deactivate,      	moea_deactivate),
364c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_page_set_memattr,	moea_page_set_memattr),
365713841afSJason A. Harmening 	MMUMETHOD(mmu_quick_enter_page, moea_quick_enter_page),
366713841afSJason A. Harmening 	MMUMETHOD(mmu_quick_remove_page, moea_quick_remove_page),
36759276937SPeter Grehan 
36859276937SPeter Grehan 	/* Internal interfaces */
36959276937SPeter Grehan 	MMUMETHOD(mmu_bootstrap,       	moea_bootstrap),
3701c96bdd1SNathan Whitehorn 	MMUMETHOD(mmu_cpu_bootstrap,   	moea_cpu_bootstrap),
371c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_mapdev_attr,	moea_mapdev_attr),
37259276937SPeter Grehan 	MMUMETHOD(mmu_mapdev,		moea_mapdev),
37359276937SPeter Grehan 	MMUMETHOD(mmu_unmapdev,		moea_unmapdev),
37459276937SPeter Grehan 	MMUMETHOD(mmu_kextract,		moea_kextract),
37559276937SPeter Grehan 	MMUMETHOD(mmu_kenter,		moea_kenter),
376c1f4123bSNathan Whitehorn 	MMUMETHOD(mmu_kenter_attr,	moea_kenter_attr),
37759276937SPeter Grehan 	MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped),
378bdb9ab0dSMark Johnston 	MMUMETHOD(mmu_scan_init,	moea_scan_init),
379afd9cb6cSJustin Hibbits 	MMUMETHOD(mmu_dumpsys_map,	moea_dumpsys_map),
38004329fa7SNathan Whitehorn 	MMUMETHOD(mmu_map_user_ptr,	moea_map_user_ptr),
381eb1baf72SNathan Whitehorn 	MMUMETHOD(mmu_decode_kernel_ptr, moea_decode_kernel_ptr),
38259276937SPeter Grehan 
38359276937SPeter Grehan 	{ 0, 0 }
38459276937SPeter Grehan };
38559276937SPeter Grehan 
38633529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0);
38733529b98SPeter Grehan 
388c1f4123bSNathan Whitehorn static __inline uint32_t
3890936003eSJustin Hibbits moea_calc_wimg(vm_paddr_t pa, vm_memattr_t ma)
390c1f4123bSNathan Whitehorn {
391c1f4123bSNathan Whitehorn 	uint32_t pte_lo;
392c1f4123bSNathan Whitehorn 	int i;
393c1f4123bSNathan Whitehorn 
394c1f4123bSNathan Whitehorn 	if (ma != VM_MEMATTR_DEFAULT) {
395c1f4123bSNathan Whitehorn 		switch (ma) {
396c1f4123bSNathan Whitehorn 		case VM_MEMATTR_UNCACHEABLE:
397c1f4123bSNathan Whitehorn 			return (PTE_I | PTE_G);
39854ac2713SJustin Hibbits 		case VM_MEMATTR_CACHEABLE:
39954ac2713SJustin Hibbits 			return (PTE_M);
400c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_COMBINING:
401c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_BACK:
402c1f4123bSNathan Whitehorn 		case VM_MEMATTR_PREFETCHABLE:
403c1f4123bSNathan Whitehorn 			return (PTE_I);
404c1f4123bSNathan Whitehorn 		case VM_MEMATTR_WRITE_THROUGH:
405c1f4123bSNathan Whitehorn 			return (PTE_W | PTE_M);
406c1f4123bSNathan Whitehorn 		}
407c1f4123bSNathan Whitehorn 	}
408c1f4123bSNathan Whitehorn 
409c1f4123bSNathan Whitehorn 	/*
410c1f4123bSNathan Whitehorn 	 * Assume the page is cache inhibited and access is guarded unless
411c1f4123bSNathan Whitehorn 	 * it's in our available memory array.
412c1f4123bSNathan Whitehorn 	 */
413c1f4123bSNathan Whitehorn 	pte_lo = PTE_I | PTE_G;
414c1f4123bSNathan Whitehorn 	for (i = 0; i < pregions_sz; i++) {
415c1f4123bSNathan Whitehorn 		if ((pa >= pregions[i].mr_start) &&
416c1f4123bSNathan Whitehorn 		    (pa < (pregions[i].mr_start + pregions[i].mr_size))) {
417c1f4123bSNathan Whitehorn 			pte_lo = PTE_M;
418c1f4123bSNathan Whitehorn 			break;
419c1f4123bSNathan Whitehorn 		}
420c1f4123bSNathan Whitehorn 	}
421c1f4123bSNathan Whitehorn 
422c1f4123bSNathan Whitehorn 	return pte_lo;
423c1f4123bSNathan Whitehorn }
42459276937SPeter Grehan 
425e4f72b32SMarcel Moolenaar static void
426e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va)
427e4f72b32SMarcel Moolenaar {
428e4f72b32SMarcel Moolenaar 
429e4f72b32SMarcel Moolenaar 	mtx_lock_spin(&tlbie_mtx);
43094363f53SNathan Whitehorn 	__asm __volatile("ptesync");
431e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbie %0" :: "r"(va));
43294363f53SNathan Whitehorn 	__asm __volatile("eieio; tlbsync; ptesync");
433e4f72b32SMarcel Moolenaar 	mtx_unlock_spin(&tlbie_mtx);
434e4f72b32SMarcel Moolenaar }
435e4f72b32SMarcel Moolenaar 
436e4f72b32SMarcel Moolenaar static void
437e4f72b32SMarcel Moolenaar tlbia(void)
438e4f72b32SMarcel Moolenaar {
439e4f72b32SMarcel Moolenaar 	vm_offset_t va;
440e4f72b32SMarcel Moolenaar 
441e4f72b32SMarcel Moolenaar 	for (va = 0; va < 0x00040000; va += 0x00001000) {
442e4f72b32SMarcel Moolenaar 		__asm __volatile("tlbie %0" :: "r"(va));
443e4f72b32SMarcel Moolenaar 		powerpc_sync();
444e4f72b32SMarcel Moolenaar 	}
445e4f72b32SMarcel Moolenaar 	__asm __volatile("tlbsync");
446e4f72b32SMarcel Moolenaar 	powerpc_sync();
447e4f72b32SMarcel Moolenaar }
4485244eac9SBenno Rice 
4495244eac9SBenno Rice static __inline int
4505244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va)
4515244eac9SBenno Rice {
4525244eac9SBenno Rice 	return (sr[(uintptr_t)va >> ADDR_SR_SHFT]);
4535244eac9SBenno Rice }
4545244eac9SBenno Rice 
4555244eac9SBenno Rice static __inline u_int
4565244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr)
4575244eac9SBenno Rice {
4585244eac9SBenno Rice 	u_int hash;
4595244eac9SBenno Rice 
4605244eac9SBenno Rice 	hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >>
4615244eac9SBenno Rice 	    ADDR_PIDX_SHFT);
46259276937SPeter Grehan 	return (hash & moea_pteg_mask);
4635244eac9SBenno Rice }
4645244eac9SBenno Rice 
4655244eac9SBenno Rice static __inline struct pvo_head *
4665244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m)
467f9bac91bSBenno Rice {
468f9bac91bSBenno Rice 
4695244eac9SBenno Rice 	return (&m->md.mdpg_pvoh);
470f9bac91bSBenno Rice }
471f9bac91bSBenno Rice 
472f9bac91bSBenno Rice static __inline void
47359276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit)
474f9bac91bSBenno Rice {
475f9bac91bSBenno Rice 
4763653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4775244eac9SBenno Rice 	m->md.mdpg_attrs &= ~ptebit;
4785244eac9SBenno Rice }
4795244eac9SBenno Rice 
4805244eac9SBenno Rice static __inline int
48159276937SPeter Grehan moea_attr_fetch(vm_page_t m)
4825244eac9SBenno Rice {
4835244eac9SBenno Rice 
4845244eac9SBenno Rice 	return (m->md.mdpg_attrs);
485f9bac91bSBenno Rice }
486f9bac91bSBenno Rice 
487f9bac91bSBenno Rice static __inline void
48859276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit)
489f9bac91bSBenno Rice {
490f9bac91bSBenno Rice 
4913653f5cbSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4925244eac9SBenno Rice 	m->md.mdpg_attrs |= ptebit;
493f9bac91bSBenno Rice }
494f9bac91bSBenno Rice 
495f9bac91bSBenno Rice static __inline int
49659276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt)
497f9bac91bSBenno Rice {
4985244eac9SBenno Rice 	if (pt->pte_hi == pvo_pt->pte_hi)
4995244eac9SBenno Rice 		return (1);
500f9bac91bSBenno Rice 
5015244eac9SBenno Rice 	return (0);
502f9bac91bSBenno Rice }
503f9bac91bSBenno Rice 
504f9bac91bSBenno Rice static __inline int
50559276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which)
506f9bac91bSBenno Rice {
5075244eac9SBenno Rice 	return (pt->pte_hi & ~PTE_VALID) ==
5085244eac9SBenno Rice 	    (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5095244eac9SBenno Rice 	    ((va >> ADDR_API_SHFT) & PTE_API) | which);
510f9bac91bSBenno Rice }
511f9bac91bSBenno Rice 
5125244eac9SBenno Rice static __inline void
51359276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo)
514f9bac91bSBenno Rice {
515d644a0b7SAlan Cox 
516d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
517d644a0b7SAlan Cox 
518f9bac91bSBenno Rice 	/*
5195244eac9SBenno Rice 	 * Construct a PTE.  Default to IMB initially.  Valid bit only gets
5205244eac9SBenno Rice 	 * set when the real pte is set in memory.
521f9bac91bSBenno Rice 	 *
522f9bac91bSBenno Rice 	 * Note: Don't set the valid bit for correct operation of tlb update.
523f9bac91bSBenno Rice 	 */
5245244eac9SBenno Rice 	pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) |
5255244eac9SBenno Rice 	    (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API);
5265244eac9SBenno Rice 	pt->pte_lo = pte_lo;
527f9bac91bSBenno Rice }
528f9bac91bSBenno Rice 
5295244eac9SBenno Rice static __inline void
53059276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt)
531f9bac91bSBenno Rice {
532f9bac91bSBenno Rice 
533d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5345244eac9SBenno Rice 	pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG);
535f9bac91bSBenno Rice }
536f9bac91bSBenno Rice 
5375244eac9SBenno Rice static __inline void
53859276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit)
539f9bac91bSBenno Rice {
5405244eac9SBenno Rice 
541d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
542d644a0b7SAlan Cox 
5435244eac9SBenno Rice 	/*
5445244eac9SBenno Rice 	 * As shown in Section 7.6.3.2.3
5455244eac9SBenno Rice 	 */
5465244eac9SBenno Rice 	pt->pte_lo &= ~ptebit;
547e4f72b32SMarcel Moolenaar 	tlbie(va);
5485244eac9SBenno Rice }
5495244eac9SBenno Rice 
5505244eac9SBenno Rice static __inline void
55159276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt)
5525244eac9SBenno Rice {
5535244eac9SBenno Rice 
554d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5555244eac9SBenno Rice 	pvo_pt->pte_hi |= PTE_VALID;
5565244eac9SBenno Rice 
5575244eac9SBenno Rice 	/*
5585244eac9SBenno Rice 	 * Update the PTE as defined in section 7.6.3.1.
559804d1cc1SJustin Hibbits 	 * Note that the REF/CHG bits are from pvo_pt and thus should have
5605244eac9SBenno Rice 	 * been saved so this routine can restore them (if desired).
5615244eac9SBenno Rice 	 */
5625244eac9SBenno Rice 	pt->pte_lo = pvo_pt->pte_lo;
563e4f72b32SMarcel Moolenaar 	powerpc_sync();
5645244eac9SBenno Rice 	pt->pte_hi = pvo_pt->pte_hi;
565e4f72b32SMarcel Moolenaar 	powerpc_sync();
56659276937SPeter Grehan 	moea_pte_valid++;
5675244eac9SBenno Rice }
5685244eac9SBenno Rice 
5695244eac9SBenno Rice static __inline void
57059276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5715244eac9SBenno Rice {
5725244eac9SBenno Rice 
573d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
5745244eac9SBenno Rice 	pvo_pt->pte_hi &= ~PTE_VALID;
5755244eac9SBenno Rice 
5765244eac9SBenno Rice 	/*
5775244eac9SBenno Rice 	 * Force the reg & chg bits back into the PTEs.
5785244eac9SBenno Rice 	 */
579e4f72b32SMarcel Moolenaar 	powerpc_sync();
5805244eac9SBenno Rice 
5815244eac9SBenno Rice 	/*
5825244eac9SBenno Rice 	 * Invalidate the pte.
5835244eac9SBenno Rice 	 */
5845244eac9SBenno Rice 	pt->pte_hi &= ~PTE_VALID;
5855244eac9SBenno Rice 
586e4f72b32SMarcel Moolenaar 	tlbie(va);
5875244eac9SBenno Rice 
5885244eac9SBenno Rice 	/*
5895244eac9SBenno Rice 	 * Save the reg & chg bits.
5905244eac9SBenno Rice 	 */
59159276937SPeter Grehan 	moea_pte_synch(pt, pvo_pt);
59259276937SPeter Grehan 	moea_pte_valid--;
5935244eac9SBenno Rice }
5945244eac9SBenno Rice 
5955244eac9SBenno Rice static __inline void
59659276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va)
5975244eac9SBenno Rice {
5985244eac9SBenno Rice 
5995244eac9SBenno Rice 	/*
6005244eac9SBenno Rice 	 * Invalidate the PTE
6015244eac9SBenno Rice 	 */
60259276937SPeter Grehan 	moea_pte_unset(pt, pvo_pt, va);
60359276937SPeter Grehan 	moea_pte_set(pt, pvo_pt);
604f9bac91bSBenno Rice }
605f9bac91bSBenno Rice 
606f9bac91bSBenno Rice /*
6075244eac9SBenno Rice  * Quick sort callout for comparing memory regions.
608f9bac91bSBenno Rice  */
6095244eac9SBenno Rice static int	om_cmp(const void *a, const void *b);
6105244eac9SBenno Rice 
6115244eac9SBenno Rice static int
6125244eac9SBenno Rice om_cmp(const void *a, const void *b)
6135244eac9SBenno Rice {
6145244eac9SBenno Rice 	const struct	ofw_map *mapa;
6155244eac9SBenno Rice 	const struct	ofw_map *mapb;
6165244eac9SBenno Rice 
6175244eac9SBenno Rice 	mapa = a;
6185244eac9SBenno Rice 	mapb = b;
6195244eac9SBenno Rice 	if (mapa->om_pa < mapb->om_pa)
6205244eac9SBenno Rice 		return (-1);
6215244eac9SBenno Rice 	else if (mapa->om_pa > mapb->om_pa)
6225244eac9SBenno Rice 		return (1);
6235244eac9SBenno Rice 	else
6245244eac9SBenno Rice 		return (0);
625f9bac91bSBenno Rice }
626f9bac91bSBenno Rice 
627f9bac91bSBenno Rice void
6281c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap)
62912640815SMarcel Moolenaar {
63012640815SMarcel Moolenaar 	u_int sdr;
63112640815SMarcel Moolenaar 	int i;
63212640815SMarcel Moolenaar 
63312640815SMarcel Moolenaar 	if (ap) {
634e4f72b32SMarcel Moolenaar 		powerpc_sync();
63512640815SMarcel Moolenaar 		__asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu));
63612640815SMarcel Moolenaar 		__asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl));
63712640815SMarcel Moolenaar 		isync();
63812640815SMarcel Moolenaar 		__asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu));
63912640815SMarcel Moolenaar 		__asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl));
64012640815SMarcel Moolenaar 		isync();
64112640815SMarcel Moolenaar 	}
64212640815SMarcel Moolenaar 
64301d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
64401d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
64512640815SMarcel Moolenaar 	isync();
64612640815SMarcel Moolenaar 
64701d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 1,%0" :: "r"(0));
64801d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 2,%0" :: "r"(0));
64901d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 2,%0" :: "r"(0));
65001d8aa0dSMarcel Moolenaar 	__asm __volatile("mtdbatu 3,%0" :: "r"(0));
65101d8aa0dSMarcel Moolenaar 	__asm __volatile("mtibatu 3,%0" :: "r"(0));
65212640815SMarcel Moolenaar 	isync();
65312640815SMarcel Moolenaar 
65412640815SMarcel Moolenaar 	for (i = 0; i < 16; i++)
655fe3b4685SNathan Whitehorn 		mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
656e4f72b32SMarcel Moolenaar 	powerpc_sync();
65712640815SMarcel Moolenaar 
65812640815SMarcel Moolenaar 	sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10);
65912640815SMarcel Moolenaar 	__asm __volatile("mtsdr1 %0" :: "r"(sdr));
66012640815SMarcel Moolenaar 	isync();
66112640815SMarcel Moolenaar 
66286c1fb4cSMarcel Moolenaar 	tlbia();
66312640815SMarcel Moolenaar }
66412640815SMarcel Moolenaar 
66512640815SMarcel Moolenaar void
66659276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
667f9bac91bSBenno Rice {
66831c82d03SBenno Rice 	ihandle_t	mmui;
6695244eac9SBenno Rice 	phandle_t	chosen, mmu;
6705244eac9SBenno Rice 	int		sz;
6715244eac9SBenno Rice 	int		i, j;
672e2f6d6e2SPeter Grehan 	vm_size_t	size, physsz, hwphyssz;
6735244eac9SBenno Rice 	vm_offset_t	pa, va, off;
67450c202c5SJeff Roberson 	void		*dpcpu;
675976cc697SNathan Whitehorn 	register_t	msr;
676f9bac91bSBenno Rice 
677f9bac91bSBenno Rice         /*
67832bc7846SPeter Grehan          * Set up BAT0 to map the lowest 256 MB area
6790d290675SBenno Rice          */
6800d290675SBenno Rice         battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
6810d290675SBenno Rice         battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
6820d290675SBenno Rice 
6830d290675SBenno Rice 	/*
6840d290675SBenno Rice 	 * Map PCI memory space.
6850d290675SBenno Rice 	 */
6860d290675SBenno Rice 	battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW);
6870d290675SBenno Rice 	battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs);
6880d290675SBenno Rice 
6890d290675SBenno Rice 	battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW);
6900d290675SBenno Rice 	battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs);
6910d290675SBenno Rice 
6920d290675SBenno Rice 	battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW);
6930d290675SBenno Rice 	battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs);
6940d290675SBenno Rice 
6950d290675SBenno Rice 	battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW);
6960d290675SBenno Rice 	battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs);
6970d290675SBenno Rice 
6980d290675SBenno Rice 	/*
6990d290675SBenno Rice 	 * Map obio devices.
7000d290675SBenno Rice 	 */
7010d290675SBenno Rice 	battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW);
7020d290675SBenno Rice 	battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs);
7030d290675SBenno Rice 
7040d290675SBenno Rice 	/*
7055244eac9SBenno Rice 	 * Use an IBAT and a DBAT to map the bottom segment of memory
706976cc697SNathan Whitehorn 	 * where we are. Turn off instruction relocation temporarily
707976cc697SNathan Whitehorn 	 * to prevent faults while reprogramming the IBAT.
708f9bac91bSBenno Rice 	 */
709976cc697SNathan Whitehorn 	msr = mfmsr();
710976cc697SNathan Whitehorn 	mtmsr(msr & ~PSL_IR);
71159276937SPeter Grehan 	__asm (".balign 32; \n"
71272ed3108SPeter Grehan 	       "mtibatu 0,%0; mtibatl 0,%1; isync; \n"
7135d64cf91SPeter Grehan 	       "mtdbatu 0,%0; mtdbatl 0,%1; isync"
71412640815SMarcel Moolenaar 	    :: "r"(battable[0].batu), "r"(battable[0].batl));
715976cc697SNathan Whitehorn 	mtmsr(msr);
7160d290675SBenno Rice 
7170d290675SBenno Rice 	/* map pci space */
71812640815SMarcel Moolenaar 	__asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu));
71912640815SMarcel Moolenaar 	__asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl));
72012640815SMarcel Moolenaar 	isync();
721f9bac91bSBenno Rice 
7221c96bdd1SNathan Whitehorn 	/* set global direct map flag */
7231c96bdd1SNathan Whitehorn 	hw_direct_map = 1;
7241c96bdd1SNathan Whitehorn 
72531c82d03SBenno Rice 	mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
72659276937SPeter Grehan 	CTR0(KTR_PMAP, "moea_bootstrap: physical memory");
72731c82d03SBenno Rice 
72831c82d03SBenno Rice 	for (i = 0; i < pregions_sz; i++) {
72932bc7846SPeter Grehan 		vm_offset_t pa;
73032bc7846SPeter Grehan 		vm_offset_t end;
73132bc7846SPeter Grehan 
73231c82d03SBenno Rice 		CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)",
73331c82d03SBenno Rice 			pregions[i].mr_start,
73431c82d03SBenno Rice 			pregions[i].mr_start + pregions[i].mr_size,
73531c82d03SBenno Rice 			pregions[i].mr_size);
73632bc7846SPeter Grehan 		/*
73732bc7846SPeter Grehan 		 * Install entries into the BAT table to allow all
73832bc7846SPeter Grehan 		 * of physmem to be convered by on-demand BAT entries.
73932bc7846SPeter Grehan 		 * The loop will sometimes set the same battable element
74032bc7846SPeter Grehan 		 * twice, but that's fine since they won't be used for
74132bc7846SPeter Grehan 		 * a while yet.
74232bc7846SPeter Grehan 		 */
74332bc7846SPeter Grehan 		pa = pregions[i].mr_start & 0xf0000000;
74432bc7846SPeter Grehan 		end = pregions[i].mr_start + pregions[i].mr_size;
74532bc7846SPeter Grehan 		do {
74632bc7846SPeter Grehan                         u_int n = pa >> ADDR_SR_SHFT;
74732bc7846SPeter Grehan 
74832bc7846SPeter Grehan 			battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW);
74932bc7846SPeter Grehan 			battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs);
75032bc7846SPeter Grehan 			pa += SEGMENT_LENGTH;
75132bc7846SPeter Grehan 		} while (pa < end);
75231c82d03SBenno Rice 	}
75331c82d03SBenno Rice 
75421943937SJeff Roberson 	if (PHYS_AVAIL_ENTRIES < regions_sz)
75559276937SPeter Grehan 		panic("moea_bootstrap: phys_avail too small");
75697f7cde4SNathan Whitehorn 
7575244eac9SBenno Rice 	phys_avail_count = 0;
758d2c1f576SBenno Rice 	physsz = 0;
759b0c21309SPeter Grehan 	hwphyssz = 0;
760b0c21309SPeter Grehan 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
76131c82d03SBenno Rice 	for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
7625244eac9SBenno Rice 		CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start,
7635244eac9SBenno Rice 		    regions[i].mr_start + regions[i].mr_size,
7645244eac9SBenno Rice 		    regions[i].mr_size);
765e2f6d6e2SPeter Grehan 		if (hwphyssz != 0 &&
766e2f6d6e2SPeter Grehan 		    (physsz + regions[i].mr_size) >= hwphyssz) {
767e2f6d6e2SPeter Grehan 			if (physsz < hwphyssz) {
768e2f6d6e2SPeter Grehan 				phys_avail[j] = regions[i].mr_start;
769e2f6d6e2SPeter Grehan 				phys_avail[j + 1] = regions[i].mr_start +
770e2f6d6e2SPeter Grehan 				    hwphyssz - physsz;
771e2f6d6e2SPeter Grehan 				physsz = hwphyssz;
772e2f6d6e2SPeter Grehan 				phys_avail_count++;
773e2f6d6e2SPeter Grehan 			}
774e2f6d6e2SPeter Grehan 			break;
775e2f6d6e2SPeter Grehan 		}
7765244eac9SBenno Rice 		phys_avail[j] = regions[i].mr_start;
7775244eac9SBenno Rice 		phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
7785244eac9SBenno Rice 		phys_avail_count++;
779d2c1f576SBenno Rice 		physsz += regions[i].mr_size;
780f9bac91bSBenno Rice 	}
781e347e23bSNathan Whitehorn 
782e347e23bSNathan Whitehorn 	/* Check for overlap with the kernel and exception vectors */
783e347e23bSNathan Whitehorn 	for (j = 0; j < 2*phys_avail_count; j+=2) {
784e347e23bSNathan Whitehorn 		if (phys_avail[j] < EXC_LAST)
785e347e23bSNathan Whitehorn 			phys_avail[j] += EXC_LAST;
786e347e23bSNathan Whitehorn 
787e347e23bSNathan Whitehorn 		if (kernelstart >= phys_avail[j] &&
788e347e23bSNathan Whitehorn 		    kernelstart < phys_avail[j+1]) {
789e347e23bSNathan Whitehorn 			if (kernelend < phys_avail[j+1]) {
790e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] =
791e347e23bSNathan Whitehorn 				    (kernelend & ~PAGE_MASK) + PAGE_SIZE;
792e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
793e347e23bSNathan Whitehorn 				    phys_avail[j+1];
794e347e23bSNathan Whitehorn 				phys_avail_count++;
795e347e23bSNathan Whitehorn 			}
796e347e23bSNathan Whitehorn 
797e347e23bSNathan Whitehorn 			phys_avail[j+1] = kernelstart & ~PAGE_MASK;
798e347e23bSNathan Whitehorn 		}
799e347e23bSNathan Whitehorn 
800e347e23bSNathan Whitehorn 		if (kernelend >= phys_avail[j] &&
801e347e23bSNathan Whitehorn 		    kernelend < phys_avail[j+1]) {
802e347e23bSNathan Whitehorn 			if (kernelstart > phys_avail[j]) {
803e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count] = phys_avail[j];
804e347e23bSNathan Whitehorn 				phys_avail[2*phys_avail_count + 1] =
805e347e23bSNathan Whitehorn 				    kernelstart & ~PAGE_MASK;
806e347e23bSNathan Whitehorn 				phys_avail_count++;
807e347e23bSNathan Whitehorn 			}
808e347e23bSNathan Whitehorn 
809e347e23bSNathan Whitehorn 			phys_avail[j] = (kernelend & ~PAGE_MASK) + PAGE_SIZE;
810e347e23bSNathan Whitehorn 		}
811e347e23bSNathan Whitehorn 	}
812e347e23bSNathan Whitehorn 
813d2c1f576SBenno Rice 	physmem = btoc(physsz);
814f9bac91bSBenno Rice 
815f9bac91bSBenno Rice 	/*
8165244eac9SBenno Rice 	 * Allocate PTEG table.
817f9bac91bSBenno Rice 	 */
8185244eac9SBenno Rice #ifdef PTEGCOUNT
81959276937SPeter Grehan 	moea_pteg_count = PTEGCOUNT;
8205244eac9SBenno Rice #else
82159276937SPeter Grehan 	moea_pteg_count = 0x1000;
822f9bac91bSBenno Rice 
82359276937SPeter Grehan 	while (moea_pteg_count < physmem)
82459276937SPeter Grehan 		moea_pteg_count <<= 1;
825f9bac91bSBenno Rice 
82659276937SPeter Grehan 	moea_pteg_count >>= 1;
8275244eac9SBenno Rice #endif /* PTEGCOUNT */
828f9bac91bSBenno Rice 
82959276937SPeter Grehan 	size = moea_pteg_count * sizeof(struct pteg);
83059276937SPeter Grehan 	CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count,
8315244eac9SBenno Rice 	    size);
83259276937SPeter Grehan 	moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size);
83359276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table);
83459276937SPeter Grehan 	bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg));
83559276937SPeter Grehan 	moea_pteg_mask = moea_pteg_count - 1;
836f9bac91bSBenno Rice 
8375244eac9SBenno Rice 	/*
838864bc520SBenno Rice 	 * Allocate pv/overflow lists.
8395244eac9SBenno Rice 	 */
84059276937SPeter Grehan 	size = sizeof(struct pvo_head) * moea_pteg_count;
84159276937SPeter Grehan 	moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size,
8425244eac9SBenno Rice 	    PAGE_SIZE);
84359276937SPeter Grehan 	CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table);
84459276937SPeter Grehan 	for (i = 0; i < moea_pteg_count; i++)
84559276937SPeter Grehan 		LIST_INIT(&moea_pvo_table[i]);
8465244eac9SBenno Rice 
8475244eac9SBenno Rice 	/*
848f489bf21SAlan Cox 	 * Initialize the lock that synchronizes access to the pteg and pvo
849f489bf21SAlan Cox 	 * tables.
850f489bf21SAlan Cox 	 */
851d644a0b7SAlan Cox 	mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF |
852d644a0b7SAlan Cox 	    MTX_RECURSE);
853e9b5f218SNathan Whitehorn 	mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF);
854f489bf21SAlan Cox 
855e4f72b32SMarcel Moolenaar 	mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN);
856e4f72b32SMarcel Moolenaar 
857f489bf21SAlan Cox 	/*
8585244eac9SBenno Rice 	 * Initialise the unmanaged pvo pool.
8595244eac9SBenno Rice 	 */
86059276937SPeter Grehan 	moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc(
8610d290675SBenno Rice 		BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0);
86259276937SPeter Grehan 	moea_bpvo_pool_index = 0;
8635244eac9SBenno Rice 
8645244eac9SBenno Rice 	/*
8655244eac9SBenno Rice 	 * Make sure kernel vsid is allocated as well as VSID 0.
8665244eac9SBenno Rice 	 */
86759276937SPeter Grehan 	moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW]
8685244eac9SBenno Rice 		|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
86959276937SPeter Grehan 	moea_vsid_bitmap[0] |= 1;
8705244eac9SBenno Rice 
8715244eac9SBenno Rice 	/*
872fe3b4685SNathan Whitehorn 	 * Initialize the kernel pmap (which is statically allocated).
8735244eac9SBenno Rice 	 */
874fe3b4685SNathan Whitehorn 	PMAP_LOCK_INIT(kernel_pmap);
875fe3b4685SNathan Whitehorn 	for (i = 0; i < 16; i++)
876fe3b4685SNathan Whitehorn 		kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
877c47dd3dbSAttilio Rao 	CPU_FILL(&kernel_pmap->pm_active);
878ccc4a5c7SNathan Whitehorn 	RB_INIT(&kernel_pmap->pmap_pvo);
879fe3b4685SNathan Whitehorn 
880fe3b4685SNathan Whitehorn  	/*
8813653f5cbSAlan Cox 	 * Initialize the global pv list lock.
8823653f5cbSAlan Cox 	 */
8833653f5cbSAlan Cox 	rw_init(&pvh_global_lock, "pmap pv global");
8843653f5cbSAlan Cox 
8853653f5cbSAlan Cox 	/*
886fe3b4685SNathan Whitehorn 	 * Set up the Open Firmware mappings
887fe3b4685SNathan Whitehorn 	 */
888e347e23bSNathan Whitehorn 	chosen = OF_finddevice("/chosen");
889e347e23bSNathan Whitehorn 	if (chosen != -1 && OF_getprop(chosen, "mmu", &mmui, 4) != -1 &&
890e347e23bSNathan Whitehorn 	    (mmu = OF_instance_to_package(mmui)) != -1 &&
891e347e23bSNathan Whitehorn 	    (sz = OF_getproplen(mmu, "translations")) != -1) {
892aa39961eSBenno Rice 		translations = NULL;
8936cc1cdf4SPeter Grehan 		for (i = 0; phys_avail[i] != 0; i += 2) {
8946cc1cdf4SPeter Grehan 			if (phys_avail[i + 1] >= sz) {
895aa39961eSBenno Rice 				translations = (struct ofw_map *)phys_avail[i];
8966cc1cdf4SPeter Grehan 				break;
8976cc1cdf4SPeter Grehan 			}
898aa39961eSBenno Rice 		}
899aa39961eSBenno Rice 		if (translations == NULL)
90059276937SPeter Grehan 			panic("moea_bootstrap: no space to copy translations");
9015244eac9SBenno Rice 		bzero(translations, sz);
9025244eac9SBenno Rice 		if (OF_getprop(mmu, "translations", translations, sz) == -1)
90359276937SPeter Grehan 			panic("moea_bootstrap: can't get ofw translations");
90459276937SPeter Grehan 		CTR0(KTR_PMAP, "moea_bootstrap: translations");
90531c82d03SBenno Rice 		sz /= sizeof(*translations);
9065244eac9SBenno Rice 		qsort(translations, sz, sizeof (*translations), om_cmp);
907ed1e1e2aSNathan Whitehorn 		for (i = 0; i < sz; i++) {
9085244eac9SBenno Rice 			CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x",
9095244eac9SBenno Rice 			    translations[i].om_pa, translations[i].om_va,
9105244eac9SBenno Rice 			    translations[i].om_len);
9115244eac9SBenno Rice 
91232bc7846SPeter Grehan 			/*
913e347e23bSNathan Whitehorn 			 * If the mapping is 1:1, let the RAM and device
914e347e23bSNathan Whitehorn 			 * on-demand BAT tables take care of the translation.
91532bc7846SPeter Grehan 			 */
91632bc7846SPeter Grehan 			if (translations[i].om_va == translations[i].om_pa)
91732bc7846SPeter Grehan 				continue;
9185244eac9SBenno Rice 
91932bc7846SPeter Grehan 			/* Enter the pages */
920e347e23bSNathan Whitehorn 			for (off = 0; off < translations[i].om_len;
921e347e23bSNathan Whitehorn 			    off += PAGE_SIZE)
922fe3b4685SNathan Whitehorn 				moea_kenter(mmup, translations[i].om_va + off,
923fe3b4685SNathan Whitehorn 					    translations[i].om_pa + off);
924f9bac91bSBenno Rice 		}
925e347e23bSNathan Whitehorn 	}
926014ffa99SMarcel Moolenaar 
927014ffa99SMarcel Moolenaar 	/*
928014ffa99SMarcel Moolenaar 	 * Calculate the last available physical address.
929014ffa99SMarcel Moolenaar 	 */
930014ffa99SMarcel Moolenaar 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
931014ffa99SMarcel Moolenaar 		;
932014ffa99SMarcel Moolenaar 	Maxmem = powerpc_btop(phys_avail[i + 1]);
9335244eac9SBenno Rice 
9341c96bdd1SNathan Whitehorn 	moea_cpu_bootstrap(mmup,0);
9350081393dSNathan Whitehorn 	mtmsr(mfmsr() | PSL_DR | PSL_IR);
9365244eac9SBenno Rice 	pmap_bootstrapped++;
937014ffa99SMarcel Moolenaar 
938014ffa99SMarcel Moolenaar 	/*
939014ffa99SMarcel Moolenaar 	 * Set the start and end of kva.
940014ffa99SMarcel Moolenaar 	 */
941014ffa99SMarcel Moolenaar 	virtual_avail = VM_MIN_KERNEL_ADDRESS;
942ab739706SNathan Whitehorn 	virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
943014ffa99SMarcel Moolenaar 
944014ffa99SMarcel Moolenaar 	/*
945014ffa99SMarcel Moolenaar 	 * Allocate a kernel stack with a guard page for thread0 and map it
946014ffa99SMarcel Moolenaar 	 * into the kernel page map.
947014ffa99SMarcel Moolenaar 	 */
948edc82223SKonstantin Belousov 	pa = moea_bootstrap_alloc(kstack_pages * PAGE_SIZE, PAGE_SIZE);
949014ffa99SMarcel Moolenaar 	va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
950edc82223SKonstantin Belousov 	virtual_avail = va + kstack_pages * PAGE_SIZE;
951014ffa99SMarcel Moolenaar 	CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va);
952014ffa99SMarcel Moolenaar 	thread0.td_kstack = va;
953edc82223SKonstantin Belousov 	thread0.td_kstack_pages = kstack_pages;
954edc82223SKonstantin Belousov 	for (i = 0; i < kstack_pages; i++) {
955c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
956014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
957014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
958014ffa99SMarcel Moolenaar 	}
959014ffa99SMarcel Moolenaar 
960014ffa99SMarcel Moolenaar 	/*
961014ffa99SMarcel Moolenaar 	 * Allocate virtual address space for the message buffer.
962014ffa99SMarcel Moolenaar 	 */
9634053b05bSSergey Kandaurov 	pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE);
964014ffa99SMarcel Moolenaar 	msgbufp = (struct msgbuf *)virtual_avail;
965014ffa99SMarcel Moolenaar 	va = virtual_avail;
9664053b05bSSergey Kandaurov 	virtual_avail += round_page(msgbufsize);
967014ffa99SMarcel Moolenaar 	while (va < virtual_avail) {
968c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
969014ffa99SMarcel Moolenaar 		pa += PAGE_SIZE;
970014ffa99SMarcel Moolenaar 		va += PAGE_SIZE;
971014ffa99SMarcel Moolenaar 	}
97250c202c5SJeff Roberson 
97350c202c5SJeff Roberson 	/*
97450c202c5SJeff Roberson 	 * Allocate virtual address space for the dynamic percpu area.
97550c202c5SJeff Roberson 	 */
97650c202c5SJeff Roberson 	pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
97750c202c5SJeff Roberson 	dpcpu = (void *)virtual_avail;
97850c202c5SJeff Roberson 	va = virtual_avail;
97950c202c5SJeff Roberson 	virtual_avail += DPCPU_SIZE;
98050c202c5SJeff Roberson 	while (va < virtual_avail) {
981c2ede4b3SMartin Blapp 		moea_kenter(mmup, va, pa);
98250c202c5SJeff Roberson 		pa += PAGE_SIZE;
98350c202c5SJeff Roberson 		va += PAGE_SIZE;
98450c202c5SJeff Roberson 	}
98550c202c5SJeff Roberson 	dpcpu_init(dpcpu, 0);
9865244eac9SBenno Rice }
9875244eac9SBenno Rice 
9885244eac9SBenno Rice /*
9895244eac9SBenno Rice  * Activate a user pmap.  The pmap must be activated before it's address
9905244eac9SBenno Rice  * space can be accessed in any way.
991f9bac91bSBenno Rice  */
992f9bac91bSBenno Rice void
99359276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td)
994f9bac91bSBenno Rice {
9958207b362SBenno Rice 	pmap_t	pm, pmr;
996f9bac91bSBenno Rice 
997f9bac91bSBenno Rice 	/*
99832bc7846SPeter Grehan 	 * Load all the data we need up front to encourage the compiler to
9995244eac9SBenno Rice 	 * not issue any loads while we have interrupts disabled below.
1000f9bac91bSBenno Rice 	 */
10015244eac9SBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
100252a7870dSNathan Whitehorn 	pmr = pm->pmap_phys;
10038207b362SBenno Rice 
1004c7c2767eSAttilio Rao 	CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
10058207b362SBenno Rice 	PCPU_SET(curpmap, pmr);
1006d1295abdSNathan Whitehorn 
1007d1295abdSNathan Whitehorn 	mtsrin(USER_SR << ADDR_SR_SHFT, td->td_pcb->pcb_cpu.aim.usr_vsid);
1008ac6ba8bdSBenno Rice }
1009ac6ba8bdSBenno Rice 
1010ac6ba8bdSBenno Rice void
101159276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td)
1012ac6ba8bdSBenno Rice {
1013ac6ba8bdSBenno Rice 	pmap_t	pm;
1014ac6ba8bdSBenno Rice 
1015ac6ba8bdSBenno Rice 	pm = &td->td_proc->p_vmspace->vm_pmap;
1016c7c2767eSAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &pm->pm_active);
10178207b362SBenno Rice 	PCPU_SET(curpmap, NULL);
1018f9bac91bSBenno Rice }
1019f9bac91bSBenno Rice 
1020f9bac91bSBenno Rice void
1021a844c68fSAlan Cox moea_unwire(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1022a844c68fSAlan Cox {
1023a844c68fSAlan Cox 	struct	pvo_entry key, *pvo;
1024a844c68fSAlan Cox 
1025a844c68fSAlan Cox 	PMAP_LOCK(pm);
1026a844c68fSAlan Cox 	key.pvo_vaddr = sva;
1027a844c68fSAlan Cox 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1028a844c68fSAlan Cox 	    pvo != NULL && PVO_VADDR(pvo) < eva;
1029a844c68fSAlan Cox 	    pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
1030a844c68fSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
1031a844c68fSAlan Cox 			panic("moea_unwire: pvo %p is missing PVO_WIRED", pvo);
1032a844c68fSAlan Cox 		pvo->pvo_vaddr &= ~PVO_WIRED;
1033a844c68fSAlan Cox 		pm->pm_stats.wired_count--;
1034a844c68fSAlan Cox 	}
1035a844c68fSAlan Cox 	PMAP_UNLOCK(pm);
1036a844c68fSAlan Cox }
1037a844c68fSAlan Cox 
1038a844c68fSAlan Cox void
103959276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst)
1040f9bac91bSBenno Rice {
104125e2288dSBenno Rice 	vm_offset_t	dst;
104225e2288dSBenno Rice 	vm_offset_t	src;
104325e2288dSBenno Rice 
104425e2288dSBenno Rice 	dst = VM_PAGE_TO_PHYS(mdst);
104525e2288dSBenno Rice 	src = VM_PAGE_TO_PHYS(msrc);
104625e2288dSBenno Rice 
1047e3c2930dSNathan Whitehorn 	bcopy((void *)src, (void *)dst, PAGE_SIZE);
1048f9bac91bSBenno Rice }
1049111c77dcSBenno Rice 
1050e8a4a618SKonstantin Belousov void
1051e8a4a618SKonstantin Belousov moea_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset,
1052e8a4a618SKonstantin Belousov     vm_page_t *mb, vm_offset_t b_offset, int xfersize)
1053e8a4a618SKonstantin Belousov {
1054e8a4a618SKonstantin Belousov 	void *a_cp, *b_cp;
1055e8a4a618SKonstantin Belousov 	vm_offset_t a_pg_offset, b_pg_offset;
1056e8a4a618SKonstantin Belousov 	int cnt;
1057e8a4a618SKonstantin Belousov 
1058e8a4a618SKonstantin Belousov 	while (xfersize > 0) {
1059e8a4a618SKonstantin Belousov 		a_pg_offset = a_offset & PAGE_MASK;
1060e8a4a618SKonstantin Belousov 		cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
1061e8a4a618SKonstantin Belousov 		a_cp = (char *)VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT]) +
1062e8a4a618SKonstantin Belousov 		    a_pg_offset;
1063e8a4a618SKonstantin Belousov 		b_pg_offset = b_offset & PAGE_MASK;
1064e8a4a618SKonstantin Belousov 		cnt = min(cnt, PAGE_SIZE - b_pg_offset);
1065e8a4a618SKonstantin Belousov 		b_cp = (char *)VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT]) +
1066e8a4a618SKonstantin Belousov 		    b_pg_offset;
1067e8a4a618SKonstantin Belousov 		bcopy(a_cp, b_cp, cnt);
1068e8a4a618SKonstantin Belousov 		a_offset += cnt;
1069e8a4a618SKonstantin Belousov 		b_offset += cnt;
1070e8a4a618SKonstantin Belousov 		xfersize -= cnt;
1071e8a4a618SKonstantin Belousov 	}
1072e8a4a618SKonstantin Belousov }
1073e8a4a618SKonstantin Belousov 
1074111c77dcSBenno Rice /*
10755244eac9SBenno Rice  * Zero a page of physical memory by temporarily mapping it into the tlb.
10765244eac9SBenno Rice  */
10775244eac9SBenno Rice void
107859276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m)
10795244eac9SBenno Rice {
1080fe938c08SJustin Hibbits 	vm_offset_t off, pa = VM_PAGE_TO_PHYS(m);
10815244eac9SBenno Rice 
1082fe938c08SJustin Hibbits 	for (off = 0; off < PAGE_SIZE; off += cacheline_size)
1083fe938c08SJustin Hibbits 		__asm __volatile("dcbz 0,%0" :: "r"(pa + off));
10845244eac9SBenno Rice }
10855244eac9SBenno Rice 
10865244eac9SBenno Rice void
108759276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
10885244eac9SBenno Rice {
10893495845eSBenno Rice 	vm_offset_t pa = VM_PAGE_TO_PHYS(m);
10905b43c63dSMarcel Moolenaar 	void *va = (void *)(pa + off);
10913495845eSBenno Rice 
10925b43c63dSMarcel Moolenaar 	bzero(va, size);
10935244eac9SBenno Rice }
10945244eac9SBenno Rice 
1095713841afSJason A. Harmening vm_offset_t
1096713841afSJason A. Harmening moea_quick_enter_page(mmu_t mmu, vm_page_t m)
1097713841afSJason A. Harmening {
1098713841afSJason A. Harmening 
1099713841afSJason A. Harmening 	return (VM_PAGE_TO_PHYS(m));
1100713841afSJason A. Harmening }
1101713841afSJason A. Harmening 
1102713841afSJason A. Harmening void
1103713841afSJason A. Harmening moea_quick_remove_page(mmu_t mmu, vm_offset_t addr)
1104713841afSJason A. Harmening {
1105713841afSJason A. Harmening }
1106713841afSJason A. Harmening 
11075244eac9SBenno Rice /*
11085244eac9SBenno Rice  * Map the given physical page at the specified virtual address in the
11095244eac9SBenno Rice  * target pmap with the protection requested.  If specified the page
11105244eac9SBenno Rice  * will be wired down.
11115244eac9SBenno Rice  */
111239ffa8c1SKonstantin Belousov int
111359276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
111439ffa8c1SKonstantin Belousov     u_int flags, int8_t psind)
11155244eac9SBenno Rice {
111639ffa8c1SKonstantin Belousov 	int error;
1117ce142d9eSAlan Cox 
111839ffa8c1SKonstantin Belousov 	for (;;) {
11193653f5cbSAlan Cox 		rw_wlock(&pvh_global_lock);
1120ce142d9eSAlan Cox 		PMAP_LOCK(pmap);
112139ffa8c1SKonstantin Belousov 		error = moea_enter_locked(pmap, va, m, prot, flags, psind);
11223653f5cbSAlan Cox 		rw_wunlock(&pvh_global_lock);
1123ce142d9eSAlan Cox 		PMAP_UNLOCK(pmap);
112439ffa8c1SKonstantin Belousov 		if (error != ENOMEM)
112539ffa8c1SKonstantin Belousov 			return (KERN_SUCCESS);
112639ffa8c1SKonstantin Belousov 		if ((flags & PMAP_ENTER_NOSLEEP) != 0)
112739ffa8c1SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
112839ffa8c1SKonstantin Belousov 		VM_OBJECT_ASSERT_UNLOCKED(m->object);
11292c0f13aaSKonstantin Belousov 		vm_wait(NULL);
113039ffa8c1SKonstantin Belousov 	}
1131ce142d9eSAlan Cox }
1132ce142d9eSAlan Cox 
1133ce142d9eSAlan Cox /*
1134ce142d9eSAlan Cox  * Map the given physical page at the specified virtual address in the
1135ce142d9eSAlan Cox  * target pmap with the protection requested.  If specified the page
1136ce142d9eSAlan Cox  * will be wired down.
1137ce142d9eSAlan Cox  *
1138f26bcf99SAlan Cox  * The global pvh and pmap must be locked.
1139ce142d9eSAlan Cox  */
114039ffa8c1SKonstantin Belousov static int
1141ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
114239ffa8c1SKonstantin Belousov     u_int flags, int8_t psind __unused)
1143ce142d9eSAlan Cox {
11445244eac9SBenno Rice 	struct		pvo_head *pvo_head;
1145378862a7SJeff Roberson 	uma_zone_t	zone;
114657bd5cceSNathan Whitehorn 	u_int		pte_lo, pvo_flags;
11475244eac9SBenno Rice 	int		error;
11485244eac9SBenno Rice 
1149081b8e20SAlan Cox 	if (pmap_bootstrapped)
1150081b8e20SAlan Cox 		rw_assert(&pvh_global_lock, RA_WLOCKED);
1151081b8e20SAlan Cox 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
11522a499f92SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) == 0) {
11532a499f92SKonstantin Belousov 		if ((flags & PMAP_ENTER_QUICK_LOCKED) == 0)
1154205be21dSJeff Roberson 			VM_PAGE_OBJECT_BUSY_ASSERT(m);
11552a499f92SKonstantin Belousov 		else
11562a499f92SKonstantin Belousov 			VM_OBJECT_ASSERT_LOCKED(m->object);
11572a499f92SKonstantin Belousov 	}
1158081b8e20SAlan Cox 
1159081b8e20SAlan Cox 	if ((m->oflags & VPO_UNMANAGED) != 0 || !moea_initialized) {
116059276937SPeter Grehan 		pvo_head = &moea_pvo_kunmanaged;
116159276937SPeter Grehan 		zone = moea_upvo_zone;
11625244eac9SBenno Rice 		pvo_flags = 0;
11635244eac9SBenno Rice 	} else {
116403b6e025SPeter Grehan 		pvo_head = vm_page_to_pvoh(m);
116559276937SPeter Grehan 		zone = moea_mpvo_zone;
11665244eac9SBenno Rice 		pvo_flags = PVO_MANAGED;
11675244eac9SBenno Rice 	}
11684dba5df1SPeter Grehan 
1169cd6a97f0SNathan Whitehorn 	pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m));
11705244eac9SBenno Rice 
117144b8bd66SAlan Cox 	if (prot & VM_PROT_WRITE) {
11725244eac9SBenno Rice 		pte_lo |= PTE_BW;
11732368a371SAlan Cox 		if (pmap_bootstrapped &&
1174d98d0ce2SKonstantin Belousov 		    (m->oflags & VPO_UNMANAGED) == 0)
11753407fefeSKonstantin Belousov 			vm_page_aflag_set(m, PGA_WRITEABLE);
117644b8bd66SAlan Cox 	} else
11775244eac9SBenno Rice 		pte_lo |= PTE_BR;
11785244eac9SBenno Rice 
117939ffa8c1SKonstantin Belousov 	if ((flags & PMAP_ENTER_WIRED) != 0)
11805244eac9SBenno Rice 		pvo_flags |= PVO_WIRED;
11815244eac9SBenno Rice 
118259276937SPeter Grehan 	error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m),
11838207b362SBenno Rice 	    pte_lo, pvo_flags);
11845244eac9SBenno Rice 
11858207b362SBenno Rice 	/*
118657bd5cceSNathan Whitehorn 	 * Flush the real page from the instruction cache. This has be done
118757bd5cceSNathan Whitehorn 	 * for all user mappings to prevent information leakage via the
1188805bee55SNathan Whitehorn 	 * instruction cache. moea_pvo_enter() returns ENOENT for the first
1189805bee55SNathan Whitehorn 	 * mapping for a page.
11908207b362SBenno Rice 	 */
1191805bee55SNathan Whitehorn 	if (pmap != kernel_pmap && error == ENOENT &&
1192805bee55SNathan Whitehorn 	    (pte_lo & (PTE_I | PTE_G)) == 0)
119359276937SPeter Grehan 		moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE);
119439ffa8c1SKonstantin Belousov 
119539ffa8c1SKonstantin Belousov 	return (error);
1196ce142d9eSAlan Cox }
1197ce142d9eSAlan Cox 
1198ce142d9eSAlan Cox /*
1199ce142d9eSAlan Cox  * Maps a sequence of resident pages belonging to the same object.
1200ce142d9eSAlan Cox  * The sequence begins with the given page m_start.  This page is
1201ce142d9eSAlan Cox  * mapped at the given virtual address start.  Each subsequent page is
1202ce142d9eSAlan Cox  * mapped at a virtual address that is offset from start by the same
1203ce142d9eSAlan Cox  * amount as the page is offset from m_start within the object.  The
1204ce142d9eSAlan Cox  * last page in the sequence is the page with the largest offset from
1205ce142d9eSAlan Cox  * m_start that can be mapped at a virtual address less than the given
1206ce142d9eSAlan Cox  * virtual address end.  Not every virtual page between start and end
1207ce142d9eSAlan Cox  * is mapped; only those for which a resident page exists with the
1208ce142d9eSAlan Cox  * corresponding offset from m_start are mapped.
1209ce142d9eSAlan Cox  */
1210ce142d9eSAlan Cox void
1211ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end,
1212ce142d9eSAlan Cox     vm_page_t m_start, vm_prot_t prot)
1213ce142d9eSAlan Cox {
1214ce142d9eSAlan Cox 	vm_page_t m;
1215ce142d9eSAlan Cox 	vm_pindex_t diff, psize;
1216ce142d9eSAlan Cox 
12179af6d512SAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(m_start->object);
12189af6d512SAttilio Rao 
1219ce142d9eSAlan Cox 	psize = atop(end - start);
1220ce142d9eSAlan Cox 	m = m_start;
12213653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1222ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1223ce142d9eSAlan Cox 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1224ce142d9eSAlan Cox 		moea_enter_locked(pm, start + ptoa(diff), m, prot &
12252a499f92SKonstantin Belousov 		    (VM_PROT_READ | VM_PROT_EXECUTE), PMAP_ENTER_QUICK_LOCKED,
12262a499f92SKonstantin Belousov 		    0);
1227ce142d9eSAlan Cox 		m = TAILQ_NEXT(m, listq);
1228ce142d9eSAlan Cox 	}
12293653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1230ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
12315244eac9SBenno Rice }
12325244eac9SBenno Rice 
12332053c127SStephan Uphoff void
123459276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m,
12352053c127SStephan Uphoff     vm_prot_t prot)
1236dca96f1aSAlan Cox {
1237dca96f1aSAlan Cox 
12383653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1239ce142d9eSAlan Cox 	PMAP_LOCK(pm);
1240ce142d9eSAlan Cox 	moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
12412a499f92SKonstantin Belousov 	    PMAP_ENTER_QUICK_LOCKED, 0);
12423653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1243ce142d9eSAlan Cox 	PMAP_UNLOCK(pm);
1244dca96f1aSAlan Cox }
1245dca96f1aSAlan Cox 
124656b09388SAlan Cox vm_paddr_t
124759276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va)
12485244eac9SBenno Rice {
12490f92104cSBenno Rice 	struct	pvo_entry *pvo;
125048d0b1a0SAlan Cox 	vm_paddr_t pa;
12510f92104cSBenno Rice 
125248d0b1a0SAlan Cox 	PMAP_LOCK(pm);
125359276937SPeter Grehan 	pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
125448d0b1a0SAlan Cox 	if (pvo == NULL)
125548d0b1a0SAlan Cox 		pa = 0;
125648d0b1a0SAlan Cox 	else
125752a7870dSNathan Whitehorn 		pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
125848d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
125948d0b1a0SAlan Cox 	return (pa);
12605244eac9SBenno Rice }
12615244eac9SBenno Rice 
12625244eac9SBenno Rice /*
126384792e72SPeter Grehan  * Atomically extract and hold the physical page with the given
126484792e72SPeter Grehan  * pmap and virtual address pair if that mapping permits the given
126584792e72SPeter Grehan  * protection.
126684792e72SPeter Grehan  */
126784792e72SPeter Grehan vm_page_t
126859276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot)
126984792e72SPeter Grehan {
1270ab50a262SAlan Cox 	struct	pvo_entry *pvo;
127184792e72SPeter Grehan 	vm_page_t m;
127284792e72SPeter Grehan 
127384792e72SPeter Grehan 	m = NULL;
127448d0b1a0SAlan Cox 	PMAP_LOCK(pmap);
127559276937SPeter Grehan 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
127652a7870dSNathan Whitehorn 	if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) &&
127752a7870dSNathan Whitehorn 	    ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW ||
1278ab50a262SAlan Cox 	     (prot & VM_PROT_WRITE) == 0)) {
127952a7870dSNathan Whitehorn 		m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
1280fee2a2faSMark Johnston 		if (!vm_page_wire_mapped(m))
1281fee2a2faSMark Johnston 			m = NULL;
128284792e72SPeter Grehan 	}
128348d0b1a0SAlan Cox 	PMAP_UNLOCK(pmap);
128484792e72SPeter Grehan 	return (m);
128584792e72SPeter Grehan }
128684792e72SPeter Grehan 
12875244eac9SBenno Rice void
128859276937SPeter Grehan moea_init(mmu_t mmu)
12895244eac9SBenno Rice {
12905244eac9SBenno Rice 
129159276937SPeter Grehan 	moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
12920ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12930ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
129459276937SPeter Grehan 	moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry),
12950ee6dbd7SPeter Grehan 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
12960ee6dbd7SPeter Grehan 	    UMA_ZONE_VM | UMA_ZONE_NOFREE);
129759276937SPeter Grehan 	moea_initialized = TRUE;
12985244eac9SBenno Rice }
12995244eac9SBenno Rice 
13005244eac9SBenno Rice boolean_t
13017b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m)
13027b85f591SAlan Cox {
13038d9e6d9fSAlan Cox 	boolean_t rv;
13047b85f591SAlan Cox 
1305d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1306c46b90e9SAlan Cox 	    ("moea_is_referenced: page %p is not managed", m));
13078d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
13088d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_REF);
13098d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13108d9e6d9fSAlan Cox 	return (rv);
13117b85f591SAlan Cox }
13127b85f591SAlan Cox 
13137b85f591SAlan Cox boolean_t
131459276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m)
13155244eac9SBenno Rice {
13168d9e6d9fSAlan Cox 	boolean_t rv;
13170f92104cSBenno Rice 
1318d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1319567e51e1SAlan Cox 	    ("moea_is_modified: page %p is not managed", m));
1320567e51e1SAlan Cox 
1321567e51e1SAlan Cox 	/*
1322638f8678SJeff Roberson 	 * If the page is not busied then this check is racy.
1323567e51e1SAlan Cox 	 */
1324638f8678SJeff Roberson 	if (!pmap_page_is_write_mapped(m))
13250f92104cSBenno Rice 		return (FALSE);
1326638f8678SJeff Roberson 
13278d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
13288d9e6d9fSAlan Cox 	rv = moea_query_bit(m, PTE_CHG);
13298d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13308d9e6d9fSAlan Cox 	return (rv);
1331566526a9SAlan Cox }
1332566526a9SAlan Cox 
1333e396eb60SAlan Cox boolean_t
1334e396eb60SAlan Cox moea_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1335e396eb60SAlan Cox {
1336e396eb60SAlan Cox 	struct pvo_entry *pvo;
1337e396eb60SAlan Cox 	boolean_t rv;
1338e396eb60SAlan Cox 
1339e396eb60SAlan Cox 	PMAP_LOCK(pmap);
1340e396eb60SAlan Cox 	pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL);
1341e396eb60SAlan Cox 	rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0;
1342e396eb60SAlan Cox 	PMAP_UNLOCK(pmap);
1343e396eb60SAlan Cox 	return (rv);
1344e396eb60SAlan Cox }
1345e396eb60SAlan Cox 
13465244eac9SBenno Rice void
134759276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m)
134803b6e025SPeter Grehan {
134903b6e025SPeter Grehan 
1350d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1351567e51e1SAlan Cox 	    ("moea_clear_modify: page %p is not managed", m));
1352638f8678SJeff Roberson 	vm_page_assert_busied(m);
1353567e51e1SAlan Cox 
1354638f8678SJeff Roberson 	if (!pmap_page_is_write_mapped(m))
135503b6e025SPeter Grehan 		return;
13568d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
1357ce186587SAlan Cox 	moea_clear_bit(m, PTE_CHG);
13588d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
13595244eac9SBenno Rice }
13605244eac9SBenno Rice 
13617f3a4093SMike Silbersack /*
136278985e42SAlan Cox  * Clear the write and modified bits in each of the given page's mappings.
136378985e42SAlan Cox  */
136478985e42SAlan Cox void
136578985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m)
136678985e42SAlan Cox {
136778985e42SAlan Cox 	struct	pvo_entry *pvo;
136878985e42SAlan Cox 	struct	pte *pt;
136978985e42SAlan Cox 	pmap_t	pmap;
137078985e42SAlan Cox 	u_int	lo;
137178985e42SAlan Cox 
1372d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
13739ab6032fSAlan Cox 	    ("moea_remove_write: page %p is not managed", m));
1374638f8678SJeff Roberson 	vm_page_assert_busied(m);
13759ab6032fSAlan Cox 
1376638f8678SJeff Roberson 	if (!pmap_page_is_write_mapped(m))
137778985e42SAlan Cox 		return;
13783653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
137978985e42SAlan Cox 	lo = moea_attr_fetch(m);
1380e4f72b32SMarcel Moolenaar 	powerpc_sync();
138178985e42SAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
138278985e42SAlan Cox 		pmap = pvo->pvo_pmap;
138378985e42SAlan Cox 		PMAP_LOCK(pmap);
138452a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) {
138578985e42SAlan Cox 			pt = moea_pvo_to_pte(pvo, -1);
138652a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
138752a7870dSNathan Whitehorn 			pvo->pvo_pte.pte.pte_lo |= PTE_BR;
138878985e42SAlan Cox 			if (pt != NULL) {
138952a7870dSNathan Whitehorn 				moea_pte_synch(pt, &pvo->pvo_pte.pte);
139052a7870dSNathan Whitehorn 				lo |= pvo->pvo_pte.pte.pte_lo;
139152a7870dSNathan Whitehorn 				pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG;
139252a7870dSNathan Whitehorn 				moea_pte_change(pt, &pvo->pvo_pte.pte,
139378985e42SAlan Cox 				    pvo->pvo_vaddr);
139478985e42SAlan Cox 				mtx_unlock(&moea_table_mutex);
139578985e42SAlan Cox 			}
139678985e42SAlan Cox 		}
139778985e42SAlan Cox 		PMAP_UNLOCK(pmap);
139878985e42SAlan Cox 	}
139978985e42SAlan Cox 	if ((lo & PTE_CHG) != 0) {
140078985e42SAlan Cox 		moea_attr_clear(m, PTE_CHG);
140178985e42SAlan Cox 		vm_page_dirty(m);
140278985e42SAlan Cox 	}
14033407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
14043653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
140578985e42SAlan Cox }
140678985e42SAlan Cox 
140778985e42SAlan Cox /*
140859276937SPeter Grehan  *	moea_ts_referenced:
14097f3a4093SMike Silbersack  *
14107f3a4093SMike Silbersack  *	Return a count of reference bits for a page, clearing those bits.
14117f3a4093SMike Silbersack  *	It is not necessary for every reference bit to be cleared, but it
14127f3a4093SMike Silbersack  *	is necessary that 0 only be returned when there are truly no
14137f3a4093SMike Silbersack  *	reference bits set.
14147f3a4093SMike Silbersack  *
14157f3a4093SMike Silbersack  *	XXX: The exact number of bits to check and clear is a matter that
14167f3a4093SMike Silbersack  *	should be tested and standardized at some point in the future for
14177f3a4093SMike Silbersack  *	optimal aging of shared pages.
14187f3a4093SMike Silbersack  */
14198d9e6d9fSAlan Cox int
142059276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m)
14215244eac9SBenno Rice {
14228d9e6d9fSAlan Cox 	int count;
142303b6e025SPeter Grehan 
1424d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1425ce186587SAlan Cox 	    ("moea_ts_referenced: page %p is not managed", m));
14268d9e6d9fSAlan Cox 	rw_wlock(&pvh_global_lock);
14278d9e6d9fSAlan Cox 	count = moea_clear_bit(m, PTE_REF);
14288d9e6d9fSAlan Cox 	rw_wunlock(&pvh_global_lock);
14298d9e6d9fSAlan Cox 	return (count);
14305244eac9SBenno Rice }
14315244eac9SBenno Rice 
14325244eac9SBenno Rice /*
1433c1f4123bSNathan Whitehorn  * Modify the WIMG settings of all mappings for a page.
1434c1f4123bSNathan Whitehorn  */
1435c1f4123bSNathan Whitehorn void
1436c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma)
1437c1f4123bSNathan Whitehorn {
1438c1f4123bSNathan Whitehorn 	struct	pvo_entry *pvo;
1439cd6a97f0SNathan Whitehorn 	struct	pvo_head *pvo_head;
1440c1f4123bSNathan Whitehorn 	struct	pte *pt;
1441c1f4123bSNathan Whitehorn 	pmap_t	pmap;
1442c1f4123bSNathan Whitehorn 	u_int	lo;
1443c1f4123bSNathan Whitehorn 
1444d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0) {
1445cd6a97f0SNathan Whitehorn 		m->md.mdpg_cache_attrs = ma;
1446cd6a97f0SNathan Whitehorn 		return;
1447cd6a97f0SNathan Whitehorn 	}
1448cd6a97f0SNathan Whitehorn 
14493653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
1450cd6a97f0SNathan Whitehorn 	pvo_head = vm_page_to_pvoh(m);
1451c1f4123bSNathan Whitehorn 	lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
1452cd6a97f0SNathan Whitehorn 
1453cd6a97f0SNathan Whitehorn 	LIST_FOREACH(pvo, pvo_head, pvo_vlink) {
1454c1f4123bSNathan Whitehorn 		pmap = pvo->pvo_pmap;
1455c1f4123bSNathan Whitehorn 		PMAP_LOCK(pmap);
1456c1f4123bSNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1457c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG;
1458c1f4123bSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= lo;
1459c1f4123bSNathan Whitehorn 		if (pt != NULL) {
1460c1f4123bSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte,
1461c1f4123bSNathan Whitehorn 			    pvo->pvo_vaddr);
1462c1f4123bSNathan Whitehorn 			if (pvo->pvo_pmap == kernel_pmap)
1463c1f4123bSNathan Whitehorn 				isync();
1464c1f4123bSNathan Whitehorn 		}
1465c1f4123bSNathan Whitehorn 		mtx_unlock(&moea_table_mutex);
1466c1f4123bSNathan Whitehorn 		PMAP_UNLOCK(pmap);
1467c1f4123bSNathan Whitehorn 	}
1468c1f4123bSNathan Whitehorn 	m->md.mdpg_cache_attrs = ma;
14693653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1470c1f4123bSNathan Whitehorn }
1471c1f4123bSNathan Whitehorn 
1472c1f4123bSNathan Whitehorn /*
14735244eac9SBenno Rice  * Map a wired page into kernel virtual address space.
14745244eac9SBenno Rice  */
14755244eac9SBenno Rice void
147620b79612SRafal Jaworowski moea_kenter(mmu_t mmu, vm_offset_t va, vm_paddr_t pa)
14775244eac9SBenno Rice {
1478c1f4123bSNathan Whitehorn 
1479c1f4123bSNathan Whitehorn 	moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1480c1f4123bSNathan Whitehorn }
1481c1f4123bSNathan Whitehorn 
1482c1f4123bSNathan Whitehorn void
14830936003eSJustin Hibbits moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma)
1484c1f4123bSNathan Whitehorn {
14855244eac9SBenno Rice 	u_int		pte_lo;
14865244eac9SBenno Rice 	int		error;
14875244eac9SBenno Rice 
14885244eac9SBenno Rice #if 0
14895244eac9SBenno Rice 	if (va < VM_MIN_KERNEL_ADDRESS)
149059276937SPeter Grehan 		panic("moea_kenter: attempt to enter non-kernel address %#x",
14915244eac9SBenno Rice 		    va);
14925244eac9SBenno Rice #endif
14935244eac9SBenno Rice 
1494c1f4123bSNathan Whitehorn 	pte_lo = moea_calc_wimg(pa, ma);
14955244eac9SBenno Rice 
14964711f8d7SAlan Cox 	PMAP_LOCK(kernel_pmap);
149759276937SPeter Grehan 	error = moea_pvo_enter(kernel_pmap, moea_upvo_zone,
149859276937SPeter Grehan 	    &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED);
14995244eac9SBenno Rice 
15005244eac9SBenno Rice 	if (error != 0 && error != ENOENT)
150159276937SPeter Grehan 		panic("moea_kenter: failed to enter va %#x pa %#x: %d", va,
15025244eac9SBenno Rice 		    pa, error);
15035244eac9SBenno Rice 
15044711f8d7SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
15055244eac9SBenno Rice }
15065244eac9SBenno Rice 
1507e79f59e8SBenno Rice /*
1508e79f59e8SBenno Rice  * Extract the physical page address associated with the given kernel virtual
1509e79f59e8SBenno Rice  * address.
1510e79f59e8SBenno Rice  */
151120b79612SRafal Jaworowski vm_paddr_t
151259276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va)
15135244eac9SBenno Rice {
1514e79f59e8SBenno Rice 	struct		pvo_entry *pvo;
151548d0b1a0SAlan Cox 	vm_paddr_t pa;
1516e79f59e8SBenno Rice 
15170efd0097SPeter Grehan 	/*
151852a7870dSNathan Whitehorn 	 * Allow direct mappings on 32-bit OEA
15190efd0097SPeter Grehan 	 */
15200efd0097SPeter Grehan 	if (va < VM_MIN_KERNEL_ADDRESS) {
15210efd0097SPeter Grehan 		return (va);
15220efd0097SPeter Grehan 	}
15230efd0097SPeter Grehan 
152448d0b1a0SAlan Cox 	PMAP_LOCK(kernel_pmap);
152559276937SPeter Grehan 	pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
152659276937SPeter Grehan 	KASSERT(pvo != NULL, ("moea_kextract: no addr found"));
152752a7870dSNathan Whitehorn 	pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF);
152848d0b1a0SAlan Cox 	PMAP_UNLOCK(kernel_pmap);
152948d0b1a0SAlan Cox 	return (pa);
1530e79f59e8SBenno Rice }
1531e79f59e8SBenno Rice 
153288afb2a3SBenno Rice /*
153388afb2a3SBenno Rice  * Remove a wired page from kernel virtual address space.
153488afb2a3SBenno Rice  */
15355244eac9SBenno Rice void
153659276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va)
15375244eac9SBenno Rice {
153888afb2a3SBenno Rice 
153959276937SPeter Grehan 	moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE);
15405244eac9SBenno Rice }
15415244eac9SBenno Rice 
15425244eac9SBenno Rice /*
154304329fa7SNathan Whitehorn  * Provide a kernel pointer corresponding to a given userland pointer.
154404329fa7SNathan Whitehorn  * The returned pointer is valid until the next time this function is
154504329fa7SNathan Whitehorn  * called in this thread. This is used internally in copyin/copyout.
154604329fa7SNathan Whitehorn  */
154704329fa7SNathan Whitehorn int
154804329fa7SNathan Whitehorn moea_map_user_ptr(mmu_t mmu, pmap_t pm, volatile const void *uaddr,
154904329fa7SNathan Whitehorn     void **kaddr, size_t ulen, size_t *klen)
155004329fa7SNathan Whitehorn {
155104329fa7SNathan Whitehorn 	size_t l;
155204329fa7SNathan Whitehorn 	register_t vsid;
155304329fa7SNathan Whitehorn 
155404329fa7SNathan Whitehorn 	*kaddr = (char *)USER_ADDR + ((uintptr_t)uaddr & ~SEGMENT_MASK);
155504329fa7SNathan Whitehorn 	l = ((char *)USER_ADDR + SEGMENT_LENGTH) - (char *)(*kaddr);
155604329fa7SNathan Whitehorn 	if (l > ulen)
155704329fa7SNathan Whitehorn 		l = ulen;
155804329fa7SNathan Whitehorn 	if (klen)
155904329fa7SNathan Whitehorn 		*klen = l;
156004329fa7SNathan Whitehorn 	else if (l != ulen)
156104329fa7SNathan Whitehorn 		return (EFAULT);
156204329fa7SNathan Whitehorn 
156304329fa7SNathan Whitehorn 	vsid = va_to_vsid(pm, (vm_offset_t)uaddr);
156404329fa7SNathan Whitehorn 
156504329fa7SNathan Whitehorn 	/* Mark segment no-execute */
156604329fa7SNathan Whitehorn 	vsid |= SR_N;
156704329fa7SNathan Whitehorn 
156804329fa7SNathan Whitehorn 	/* If we have already set this VSID, we can just return */
156904329fa7SNathan Whitehorn 	if (curthread->td_pcb->pcb_cpu.aim.usr_vsid == vsid)
157004329fa7SNathan Whitehorn 		return (0);
157104329fa7SNathan Whitehorn 
157204329fa7SNathan Whitehorn 	__asm __volatile("isync");
157304329fa7SNathan Whitehorn 	curthread->td_pcb->pcb_cpu.aim.usr_segm =
157404329fa7SNathan Whitehorn 	    (uintptr_t)uaddr >> ADDR_SR_SHFT;
157504329fa7SNathan Whitehorn 	curthread->td_pcb->pcb_cpu.aim.usr_vsid = vsid;
157604329fa7SNathan Whitehorn 	__asm __volatile("mtsr %0,%1; isync" :: "n"(USER_SR), "r"(vsid));
157704329fa7SNathan Whitehorn 
157804329fa7SNathan Whitehorn 	return (0);
157904329fa7SNathan Whitehorn }
158004329fa7SNathan Whitehorn 
158104329fa7SNathan Whitehorn /*
1582eb1baf72SNathan Whitehorn  * Figure out where a given kernel pointer (usually in a fault) points
1583eb1baf72SNathan Whitehorn  * to from the VM's perspective, potentially remapping into userland's
1584eb1baf72SNathan Whitehorn  * address space.
1585eb1baf72SNathan Whitehorn  */
1586eb1baf72SNathan Whitehorn static int
1587eb1baf72SNathan Whitehorn moea_decode_kernel_ptr(mmu_t mmu, vm_offset_t addr, int *is_user,
1588eb1baf72SNathan Whitehorn     vm_offset_t *decoded_addr)
1589eb1baf72SNathan Whitehorn {
1590eb1baf72SNathan Whitehorn 	vm_offset_t user_sr;
1591eb1baf72SNathan Whitehorn 
1592eb1baf72SNathan Whitehorn 	if ((addr >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
1593eb1baf72SNathan Whitehorn 		user_sr = curthread->td_pcb->pcb_cpu.aim.usr_segm;
1594eb1baf72SNathan Whitehorn 		addr &= ADDR_PIDX | ADDR_POFF;
1595eb1baf72SNathan Whitehorn 		addr |= user_sr << ADDR_SR_SHFT;
1596eb1baf72SNathan Whitehorn 		*decoded_addr = addr;
1597eb1baf72SNathan Whitehorn 		*is_user = 1;
1598eb1baf72SNathan Whitehorn 	} else {
1599eb1baf72SNathan Whitehorn 		*decoded_addr = addr;
1600eb1baf72SNathan Whitehorn 		*is_user = 0;
1601eb1baf72SNathan Whitehorn 	}
1602eb1baf72SNathan Whitehorn 
1603eb1baf72SNathan Whitehorn 	return (0);
1604eb1baf72SNathan Whitehorn }
1605eb1baf72SNathan Whitehorn 
1606eb1baf72SNathan Whitehorn /*
16075244eac9SBenno Rice  * Map a range of physical addresses into kernel virtual address space.
16085244eac9SBenno Rice  *
16095244eac9SBenno Rice  * The value passed in *virt is a suggested virtual address for the mapping.
16105244eac9SBenno Rice  * Architectures which can support a direct-mapped physical to virtual region
16115244eac9SBenno Rice  * can return the appropriate address within that region, leaving '*virt'
16125244eac9SBenno Rice  * unchanged.  We cannot and therefore do not; *virt is updated with the
16135244eac9SBenno Rice  * first usable address after the mapped region.
16145244eac9SBenno Rice  */
16155244eac9SBenno Rice vm_offset_t
161620b79612SRafal Jaworowski moea_map(mmu_t mmu, vm_offset_t *virt, vm_paddr_t pa_start,
161720b79612SRafal Jaworowski     vm_paddr_t pa_end, int prot)
16185244eac9SBenno Rice {
16195244eac9SBenno Rice 	vm_offset_t	sva, va;
16205244eac9SBenno Rice 
16215244eac9SBenno Rice 	sva = *virt;
16225244eac9SBenno Rice 	va = sva;
16235244eac9SBenno Rice 	for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
162459276937SPeter Grehan 		moea_kenter(mmu, va, pa_start);
16255244eac9SBenno Rice 	*virt = va;
16265244eac9SBenno Rice 	return (sva);
16275244eac9SBenno Rice }
16285244eac9SBenno Rice 
16295244eac9SBenno Rice /*
16307f3a4093SMike Silbersack  * Returns true if the pmap's pv is one of the first
16317f3a4093SMike Silbersack  * 16 pvs linked to from this page.  This count may
16327f3a4093SMike Silbersack  * be changed upwards or downwards in the future; it
16337f3a4093SMike Silbersack  * is only necessary that true be returned for a small
16347f3a4093SMike Silbersack  * subset of pmaps for proper page aging.
16357f3a4093SMike Silbersack  */
16365244eac9SBenno Rice boolean_t
163759276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
16385244eac9SBenno Rice {
163903b6e025SPeter Grehan         int loops;
164003b6e025SPeter Grehan 	struct pvo_entry *pvo;
1641ce186587SAlan Cox 	boolean_t rv;
164203b6e025SPeter Grehan 
1643d98d0ce2SKonstantin Belousov 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1644ce186587SAlan Cox 	    ("moea_page_exists_quick: page %p is not managed", m));
164503b6e025SPeter Grehan 	loops = 0;
1646ce186587SAlan Cox 	rv = FALSE;
16473653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
164803b6e025SPeter Grehan 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
1649ce186587SAlan Cox 		if (pvo->pvo_pmap == pmap) {
1650ce186587SAlan Cox 			rv = TRUE;
1651ce186587SAlan Cox 			break;
1652ce186587SAlan Cox 		}
165303b6e025SPeter Grehan 		if (++loops >= 16)
165403b6e025SPeter Grehan 			break;
165503b6e025SPeter Grehan 	}
16563653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
1657ce186587SAlan Cox 	return (rv);
16585244eac9SBenno Rice }
16595244eac9SBenno Rice 
1660b999e9c8SMark Johnston void
1661b999e9c8SMark Johnston moea_page_init(mmu_t mmu __unused, vm_page_t m)
1662b999e9c8SMark Johnston {
1663b999e9c8SMark Johnston 
1664b999e9c8SMark Johnston 	m->md.mdpg_attrs = 0;
1665b999e9c8SMark Johnston 	m->md.mdpg_cache_attrs = VM_MEMATTR_DEFAULT;
1666b999e9c8SMark Johnston 	LIST_INIT(&m->md.mdpg_pvoh);
1667b999e9c8SMark Johnston }
1668b999e9c8SMark Johnston 
166959677d3cSAlan Cox /*
167059677d3cSAlan Cox  * Return the number of managed mappings to the given physical page
167159677d3cSAlan Cox  * that are wired.
167259677d3cSAlan Cox  */
167359677d3cSAlan Cox int
167459677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m)
167559677d3cSAlan Cox {
167659677d3cSAlan Cox 	struct pvo_entry *pvo;
167759677d3cSAlan Cox 	int count;
167859677d3cSAlan Cox 
167959677d3cSAlan Cox 	count = 0;
1680d98d0ce2SKonstantin Belousov 	if ((m->oflags & VPO_UNMANAGED) != 0)
168159677d3cSAlan Cox 		return (count);
16823653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
168359677d3cSAlan Cox 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
168459677d3cSAlan Cox 		if ((pvo->pvo_vaddr & PVO_WIRED) != 0)
168559677d3cSAlan Cox 			count++;
16863653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
168759677d3cSAlan Cox 	return (count);
168859677d3cSAlan Cox }
168959677d3cSAlan Cox 
169059276937SPeter Grehan static u_int	moea_vsidcontext;
16915244eac9SBenno Rice 
16925244eac9SBenno Rice void
169359276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap)
16945244eac9SBenno Rice {
16955244eac9SBenno Rice 	int	i, mask;
16965244eac9SBenno Rice 	u_int	entropy;
16975244eac9SBenno Rice 
169859276937SPeter Grehan 	KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap"));
1699ccc4a5c7SNathan Whitehorn 	RB_INIT(&pmap->pmap_pvo);
17004daf20b2SPeter Grehan 
17015244eac9SBenno Rice 	entropy = 0;
17025244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(entropy));
17035244eac9SBenno Rice 
170452a7870dSNathan Whitehorn 	if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap))
170552a7870dSNathan Whitehorn 	    == NULL) {
170652a7870dSNathan Whitehorn 		pmap->pmap_phys = pmap;
170752a7870dSNathan Whitehorn 	}
170852a7870dSNathan Whitehorn 
170952a7870dSNathan Whitehorn 
1710e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
17115244eac9SBenno Rice 	/*
17125244eac9SBenno Rice 	 * Allocate some segment registers for this pmap.
17135244eac9SBenno Rice 	 */
17145244eac9SBenno Rice 	for (i = 0; i < NPMAPS; i += VSID_NBPW) {
17155244eac9SBenno Rice 		u_int	hash, n;
17165244eac9SBenno Rice 
17175244eac9SBenno Rice 		/*
17185244eac9SBenno Rice 		 * Create a new value by mutiplying by a prime and adding in
17195244eac9SBenno Rice 		 * entropy from the timebase register.  This is to make the
17205244eac9SBenno Rice 		 * VSID more random so that the PT hash function collides
17215244eac9SBenno Rice 		 * less often.  (Note that the prime casues gcc to do shifts
17225244eac9SBenno Rice 		 * instead of a multiply.)
17235244eac9SBenno Rice 		 */
172459276937SPeter Grehan 		moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy;
172559276937SPeter Grehan 		hash = moea_vsidcontext & (NPMAPS - 1);
17265244eac9SBenno Rice 		if (hash == 0)		/* 0 is special, avoid it */
17275244eac9SBenno Rice 			continue;
17285244eac9SBenno Rice 		n = hash >> 5;
17295244eac9SBenno Rice 		mask = 1 << (hash & (VSID_NBPW - 1));
173059276937SPeter Grehan 		hash = (moea_vsidcontext & 0xfffff);
173159276937SPeter Grehan 		if (moea_vsid_bitmap[n] & mask) {	/* collision? */
17325244eac9SBenno Rice 			/* anything free in this bucket? */
173359276937SPeter Grehan 			if (moea_vsid_bitmap[n] == 0xffffffff) {
173459276937SPeter Grehan 				entropy = (moea_vsidcontext >> 20);
17355244eac9SBenno Rice 				continue;
17365244eac9SBenno Rice 			}
17370dfddf6eSNathan Whitehorn 			i = ffs(~moea_vsid_bitmap[n]) - 1;
17385244eac9SBenno Rice 			mask = 1 << i;
1739d9c9c81cSPedro F. Giffuni 			hash &= rounddown2(0xfffff, VSID_NBPW);
17405244eac9SBenno Rice 			hash |= i;
17415244eac9SBenno Rice 		}
174246e93cbbSNathan Whitehorn 		KASSERT(!(moea_vsid_bitmap[n] & mask),
174346e93cbbSNathan Whitehorn 		    ("Allocating in-use VSID group %#x\n", hash));
174459276937SPeter Grehan 		moea_vsid_bitmap[n] |= mask;
17455244eac9SBenno Rice 		for (i = 0; i < 16; i++)
17465244eac9SBenno Rice 			pmap->pm_sr[i] = VSID_MAKE(i, hash);
1747e9b5f218SNathan Whitehorn 		mtx_unlock(&moea_vsid_mutex);
17485244eac9SBenno Rice 		return;
17495244eac9SBenno Rice 	}
17505244eac9SBenno Rice 
1751e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
175259276937SPeter Grehan 	panic("moea_pinit: out of segments");
17535244eac9SBenno Rice }
17545244eac9SBenno Rice 
17555244eac9SBenno Rice /*
17565244eac9SBenno Rice  * Initialize the pmap associated with process 0.
17575244eac9SBenno Rice  */
17585244eac9SBenno Rice void
175959276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm)
17605244eac9SBenno Rice {
17615244eac9SBenno Rice 
1762e68c64f0SKonstantin Belousov 	PMAP_LOCK_INIT(pm);
176359276937SPeter Grehan 	moea_pinit(mmu, pm);
17645244eac9SBenno Rice 	bzero(&pm->pm_stats, sizeof(pm->pm_stats));
17655244eac9SBenno Rice }
17665244eac9SBenno Rice 
1767e79f59e8SBenno Rice /*
1768e79f59e8SBenno Rice  * Set the physical protection on the specified range of this map as requested.
1769e79f59e8SBenno Rice  */
17705244eac9SBenno Rice void
177159276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva,
177259276937SPeter Grehan     vm_prot_t prot)
17735244eac9SBenno Rice {
1774ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
1775e79f59e8SBenno Rice 	struct	pte *pt;
1776e79f59e8SBenno Rice 
1777e79f59e8SBenno Rice 	KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
177859276937SPeter Grehan 	    ("moea_protect: non current pmap"));
1779e79f59e8SBenno Rice 
1780e79f59e8SBenno Rice 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
178159276937SPeter Grehan 		moea_remove(mmu, pm, sva, eva);
1782e79f59e8SBenno Rice 		return;
1783e79f59e8SBenno Rice 	}
1784e79f59e8SBenno Rice 
17853653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
178648d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1787ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1788ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1789ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1790ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1791e79f59e8SBenno Rice 
1792e79f59e8SBenno Rice 		/*
1793e79f59e8SBenno Rice 		 * Grab the PTE pointer before we diddle with the cached PTE
1794e79f59e8SBenno Rice 		 * copy.
1795e79f59e8SBenno Rice 		 */
1796ccc4a5c7SNathan Whitehorn 		pt = moea_pvo_to_pte(pvo, -1);
1797e79f59e8SBenno Rice 		/*
1798e79f59e8SBenno Rice 		 * Change the protection of the page.
1799e79f59e8SBenno Rice 		 */
180052a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~PTE_PP;
180152a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo |= PTE_BR;
1802e79f59e8SBenno Rice 
1803e79f59e8SBenno Rice 		/*
1804e79f59e8SBenno Rice 		 * If the PVO is in the page table, update that pte as well.
1805e79f59e8SBenno Rice 		 */
1806d644a0b7SAlan Cox 		if (pt != NULL) {
180752a7870dSNathan Whitehorn 			moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
1808d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
1809d644a0b7SAlan Cox 		}
1810e79f59e8SBenno Rice 	}
18113653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
181248d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
18135244eac9SBenno Rice }
18145244eac9SBenno Rice 
181588afb2a3SBenno Rice /*
181688afb2a3SBenno Rice  * Map a list of wired pages into kernel virtual address space.  This is
181788afb2a3SBenno Rice  * intended for temporary mappings which do not need page modification or
181888afb2a3SBenno Rice  * references recorded.  Existing mappings in the region are overwritten.
181988afb2a3SBenno Rice  */
18205244eac9SBenno Rice void
182159276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
18225244eac9SBenno Rice {
182303b6e025SPeter Grehan 	vm_offset_t va;
18245244eac9SBenno Rice 
182503b6e025SPeter Grehan 	va = sva;
182603b6e025SPeter Grehan 	while (count-- > 0) {
182759276937SPeter Grehan 		moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
182803b6e025SPeter Grehan 		va += PAGE_SIZE;
182903b6e025SPeter Grehan 		m++;
183003b6e025SPeter Grehan 	}
18315244eac9SBenno Rice }
18325244eac9SBenno Rice 
183388afb2a3SBenno Rice /*
183488afb2a3SBenno Rice  * Remove page mappings from kernel virtual address space.  Intended for
183559276937SPeter Grehan  * temporary mappings entered by moea_qenter.
183688afb2a3SBenno Rice  */
18375244eac9SBenno Rice void
183859276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count)
18395244eac9SBenno Rice {
184003b6e025SPeter Grehan 	vm_offset_t va;
184188afb2a3SBenno Rice 
184203b6e025SPeter Grehan 	va = sva;
184303b6e025SPeter Grehan 	while (count-- > 0) {
184459276937SPeter Grehan 		moea_kremove(mmu, va);
184503b6e025SPeter Grehan 		va += PAGE_SIZE;
184603b6e025SPeter Grehan 	}
18475244eac9SBenno Rice }
18485244eac9SBenno Rice 
18495244eac9SBenno Rice void
185059276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap)
18515244eac9SBenno Rice {
185232bc7846SPeter Grehan         int idx, mask;
185332bc7846SPeter Grehan 
185432bc7846SPeter Grehan 	/*
185532bc7846SPeter Grehan 	 * Free segment register's VSID
185632bc7846SPeter Grehan 	 */
185732bc7846SPeter Grehan         if (pmap->pm_sr[0] == 0)
185859276937SPeter Grehan                 panic("moea_release");
185932bc7846SPeter Grehan 
1860e9b5f218SNathan Whitehorn 	mtx_lock(&moea_vsid_mutex);
186132bc7846SPeter Grehan         idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1);
186232bc7846SPeter Grehan         mask = 1 << (idx % VSID_NBPW);
186332bc7846SPeter Grehan         idx /= VSID_NBPW;
186459276937SPeter Grehan         moea_vsid_bitmap[idx] &= ~mask;
1865e9b5f218SNathan Whitehorn 	mtx_unlock(&moea_vsid_mutex);
18665244eac9SBenno Rice }
18675244eac9SBenno Rice 
186888afb2a3SBenno Rice /*
186988afb2a3SBenno Rice  * Remove the given range of addresses from the specified map.
187088afb2a3SBenno Rice  */
18715244eac9SBenno Rice void
187259276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva)
18735244eac9SBenno Rice {
1874ccc4a5c7SNathan Whitehorn 	struct	pvo_entry *pvo, *tpvo, key;
187588afb2a3SBenno Rice 
18763653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
187748d0b1a0SAlan Cox 	PMAP_LOCK(pm);
1878ccc4a5c7SNathan Whitehorn 	key.pvo_vaddr = sva;
1879ccc4a5c7SNathan Whitehorn 	for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1880ccc4a5c7SNathan Whitehorn 	    pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
1881ccc4a5c7SNathan Whitehorn 		tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
1882598d99ddSNathan Whitehorn 		moea_pvo_remove(pvo, -1);
1883598d99ddSNathan Whitehorn 	}
188448d0b1a0SAlan Cox 	PMAP_UNLOCK(pm);
18853653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
18865244eac9SBenno Rice }
18875244eac9SBenno Rice 
1888e79f59e8SBenno Rice /*
188959276937SPeter Grehan  * Remove physical page from all pmaps in which it resides. moea_pvo_remove()
189003b6e025SPeter Grehan  * will reflect changes in pte's back to the vm_page.
189103b6e025SPeter Grehan  */
189203b6e025SPeter Grehan void
189359276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m)
189403b6e025SPeter Grehan {
189503b6e025SPeter Grehan 	struct  pvo_head *pvo_head;
189603b6e025SPeter Grehan 	struct	pvo_entry *pvo, *next_pvo;
189748d0b1a0SAlan Cox 	pmap_t	pmap;
189803b6e025SPeter Grehan 
18993653f5cbSAlan Cox 	rw_wlock(&pvh_global_lock);
190003b6e025SPeter Grehan 	pvo_head = vm_page_to_pvoh(m);
190103b6e025SPeter Grehan 	for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) {
190203b6e025SPeter Grehan 		next_pvo = LIST_NEXT(pvo, pvo_vlink);
190303b6e025SPeter Grehan 
190448d0b1a0SAlan Cox 		pmap = pvo->pvo_pmap;
190548d0b1a0SAlan Cox 		PMAP_LOCK(pmap);
190659276937SPeter Grehan 		moea_pvo_remove(pvo, -1);
190748d0b1a0SAlan Cox 		PMAP_UNLOCK(pmap);
190803b6e025SPeter Grehan 	}
1909*5cff1f4dSMark Johnston 	if ((m->a.flags & PGA_WRITEABLE) && moea_query_bit(m, PTE_CHG)) {
1910c668b5b4SNathan Whitehorn 		moea_attr_clear(m, PTE_CHG);
1911062c8f4cSNathan Whitehorn 		vm_page_dirty(m);
1912062c8f4cSNathan Whitehorn 	}
19133407fefeSKonstantin Belousov 	vm_page_aflag_clear(m, PGA_WRITEABLE);
19143653f5cbSAlan Cox 	rw_wunlock(&pvh_global_lock);
191503b6e025SPeter Grehan }
191603b6e025SPeter Grehan 
191703b6e025SPeter Grehan /*
19185244eac9SBenno Rice  * Allocate a physical page of memory directly from the phys_avail map.
191959276937SPeter Grehan  * Can only be called from moea_bootstrap before avail start and end are
19205244eac9SBenno Rice  * calculated.
19215244eac9SBenno Rice  */
19225244eac9SBenno Rice static vm_offset_t
192359276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align)
19245244eac9SBenno Rice {
19255244eac9SBenno Rice 	vm_offset_t	s, e;
19265244eac9SBenno Rice 	int		i, j;
19275244eac9SBenno Rice 
19285244eac9SBenno Rice 	size = round_page(size);
19295244eac9SBenno Rice 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
19305244eac9SBenno Rice 		if (align != 0)
1931d9c9c81cSPedro F. Giffuni 			s = roundup2(phys_avail[i], align);
19325244eac9SBenno Rice 		else
19335244eac9SBenno Rice 			s = phys_avail[i];
19345244eac9SBenno Rice 		e = s + size;
19355244eac9SBenno Rice 
19365244eac9SBenno Rice 		if (s < phys_avail[i] || e > phys_avail[i + 1])
19375244eac9SBenno Rice 			continue;
19385244eac9SBenno Rice 
19395244eac9SBenno Rice 		if (s == phys_avail[i]) {
19405244eac9SBenno Rice 			phys_avail[i] += size;
19415244eac9SBenno Rice 		} else if (e == phys_avail[i + 1]) {
19425244eac9SBenno Rice 			phys_avail[i + 1] -= size;
19435244eac9SBenno Rice 		} else {
19445244eac9SBenno Rice 			for (j = phys_avail_count * 2; j > i; j -= 2) {
19455244eac9SBenno Rice 				phys_avail[j] = phys_avail[j - 2];
19465244eac9SBenno Rice 				phys_avail[j + 1] = phys_avail[j - 1];
19475244eac9SBenno Rice 			}
19485244eac9SBenno Rice 
19495244eac9SBenno Rice 			phys_avail[i + 3] = phys_avail[i + 1];
19505244eac9SBenno Rice 			phys_avail[i + 1] = s;
19515244eac9SBenno Rice 			phys_avail[i + 2] = e;
19525244eac9SBenno Rice 			phys_avail_count++;
19535244eac9SBenno Rice 		}
19545244eac9SBenno Rice 
19555244eac9SBenno Rice 		return (s);
19565244eac9SBenno Rice 	}
195759276937SPeter Grehan 	panic("moea_bootstrap_alloc: could not allocate memory");
19585244eac9SBenno Rice }
19595244eac9SBenno Rice 
19605244eac9SBenno Rice static void
19610936003eSJustin Hibbits moea_syncicache(vm_paddr_t pa, vm_size_t len)
19625244eac9SBenno Rice {
19635244eac9SBenno Rice 	__syncicache((void *)pa, len);
19645244eac9SBenno Rice }
19655244eac9SBenno Rice 
19665244eac9SBenno Rice static int
196759276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head,
19680936003eSJustin Hibbits     vm_offset_t va, vm_paddr_t pa, u_int pte_lo, int flags)
19695244eac9SBenno Rice {
19705244eac9SBenno Rice 	struct	pvo_entry *pvo;
19715244eac9SBenno Rice 	u_int	sr;
19725244eac9SBenno Rice 	int	first;
19735244eac9SBenno Rice 	u_int	ptegidx;
19745244eac9SBenno Rice 	int	i;
197532bc7846SPeter Grehan 	int     bootstrap;
19765244eac9SBenno Rice 
197759276937SPeter Grehan 	moea_pvo_enter_calls++;
19788207b362SBenno Rice 	first = 0;
197932bc7846SPeter Grehan 	bootstrap = 0;
198032bc7846SPeter Grehan 
19815244eac9SBenno Rice 	/*
19825244eac9SBenno Rice 	 * Compute the PTE Group index.
19835244eac9SBenno Rice 	 */
19845244eac9SBenno Rice 	va &= ~ADDR_POFF;
19855244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
19865244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
19875244eac9SBenno Rice 
19885244eac9SBenno Rice 	/*
19895244eac9SBenno Rice 	 * Remove any existing mapping for this page.  Reuse the pvo entry if
19905244eac9SBenno Rice 	 * there is a mapping.
19915244eac9SBenno Rice 	 */
199259276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
199359276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
19945244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
199552a7870dSNathan Whitehorn 			if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa &&
199652a7870dSNathan Whitehorn 			    (pvo->pvo_pte.pte.pte_lo & PTE_PP) ==
1997fafc7362SBenno Rice 			    (pte_lo & PTE_PP)) {
1998add03590SAlan Cox 				/*
1999add03590SAlan Cox 				 * The PTE is not changing.  Instead, this may
2000add03590SAlan Cox 				 * be a request to change the mapping's wired
2001add03590SAlan Cox 				 * attribute.
2002add03590SAlan Cox 				 */
200359276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
2004add03590SAlan Cox 				if ((flags & PVO_WIRED) != 0 &&
2005add03590SAlan Cox 				    (pvo->pvo_vaddr & PVO_WIRED) == 0) {
2006add03590SAlan Cox 					pvo->pvo_vaddr |= PVO_WIRED;
2007add03590SAlan Cox 					pm->pm_stats.wired_count++;
2008add03590SAlan Cox 				} else if ((flags & PVO_WIRED) == 0 &&
2009add03590SAlan Cox 				    (pvo->pvo_vaddr & PVO_WIRED) != 0) {
2010add03590SAlan Cox 					pvo->pvo_vaddr &= ~PVO_WIRED;
2011add03590SAlan Cox 					pm->pm_stats.wired_count--;
2012add03590SAlan Cox 				}
201349f8f727SBenno Rice 				return (0);
2014fafc7362SBenno Rice 			}
201559276937SPeter Grehan 			moea_pvo_remove(pvo, -1);
20165244eac9SBenno Rice 			break;
20175244eac9SBenno Rice 		}
20185244eac9SBenno Rice 	}
20195244eac9SBenno Rice 
20205244eac9SBenno Rice 	/*
20215244eac9SBenno Rice 	 * If we aren't overwriting a mapping, try to allocate.
20225244eac9SBenno Rice 	 */
202359276937SPeter Grehan 	if (moea_initialized) {
2024378862a7SJeff Roberson 		pvo = uma_zalloc(zone, M_NOWAIT);
202549f8f727SBenno Rice 	} else {
202659276937SPeter Grehan 		if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) {
202759276937SPeter Grehan 			panic("moea_enter: bpvo pool exhausted, %d, %d, %d",
202859276937SPeter Grehan 			      moea_bpvo_pool_index, BPVO_POOL_SIZE,
20290d290675SBenno Rice 			      BPVO_POOL_SIZE * sizeof(struct pvo_entry));
203049f8f727SBenno Rice 		}
203159276937SPeter Grehan 		pvo = &moea_bpvo_pool[moea_bpvo_pool_index];
203259276937SPeter Grehan 		moea_bpvo_pool_index++;
203332bc7846SPeter Grehan 		bootstrap = 1;
203449f8f727SBenno Rice 	}
20355244eac9SBenno Rice 
20365244eac9SBenno Rice 	if (pvo == NULL) {
203759276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
20385244eac9SBenno Rice 		return (ENOMEM);
20395244eac9SBenno Rice 	}
20405244eac9SBenno Rice 
204159276937SPeter Grehan 	moea_pvo_entries++;
20425244eac9SBenno Rice 	pvo->pvo_vaddr = va;
20435244eac9SBenno Rice 	pvo->pvo_pmap = pm;
204459276937SPeter Grehan 	LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink);
20455244eac9SBenno Rice 	pvo->pvo_vaddr &= ~ADDR_POFF;
20465244eac9SBenno Rice 	if (flags & PVO_WIRED)
20475244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_WIRED;
204859276937SPeter Grehan 	if (pvo_head != &moea_pvo_kunmanaged)
20495244eac9SBenno Rice 		pvo->pvo_vaddr |= PVO_MANAGED;
205032bc7846SPeter Grehan 	if (bootstrap)
205132bc7846SPeter Grehan 		pvo->pvo_vaddr |= PVO_BOOTSTRAP;
20524dba5df1SPeter Grehan 
205352a7870dSNathan Whitehorn 	moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo);
20545244eac9SBenno Rice 
20555244eac9SBenno Rice 	/*
2056598d99ddSNathan Whitehorn 	 * Add to pmap list
2057598d99ddSNathan Whitehorn 	 */
2058ccc4a5c7SNathan Whitehorn 	RB_INSERT(pvo_tree, &pm->pmap_pvo, pvo);
2059598d99ddSNathan Whitehorn 
2060598d99ddSNathan Whitehorn 	/*
20615244eac9SBenno Rice 	 * Remember if the list was empty and therefore will be the first
20625244eac9SBenno Rice 	 * item.
20635244eac9SBenno Rice 	 */
20648207b362SBenno Rice 	if (LIST_FIRST(pvo_head) == NULL)
20658207b362SBenno Rice 		first = 1;
20665244eac9SBenno Rice 	LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
20674dba5df1SPeter Grehan 
2068bfc30490SAlan Cox 	if (pvo->pvo_vaddr & PVO_WIRED)
2069c3d11d22SAlan Cox 		pm->pm_stats.wired_count++;
2070c3d11d22SAlan Cox 	pm->pm_stats.resident_count++;
20715244eac9SBenno Rice 
207252a7870dSNathan Whitehorn 	i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
2073804d1cc1SJustin Hibbits 	KASSERT(i < 8, ("Invalid PTE index"));
20745244eac9SBenno Rice 	if (i >= 0) {
20755244eac9SBenno Rice 		PVO_PTEGIDX_SET(pvo, i);
20765244eac9SBenno Rice 	} else {
207759276937SPeter Grehan 		panic("moea_pvo_enter: overflow");
207859276937SPeter Grehan 		moea_pte_overflow++;
20795244eac9SBenno Rice 	}
208059276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
20814dba5df1SPeter Grehan 
20825244eac9SBenno Rice 	return (first ? ENOENT : 0);
20835244eac9SBenno Rice }
20845244eac9SBenno Rice 
20855244eac9SBenno Rice static void
208659276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx)
20875244eac9SBenno Rice {
20885244eac9SBenno Rice 	struct	pte *pt;
20895244eac9SBenno Rice 
20905244eac9SBenno Rice 	/*
20915244eac9SBenno Rice 	 * If there is an active pte entry, we need to deactivate it (and
20925244eac9SBenno Rice 	 * save the ref & cfg bits).
20935244eac9SBenno Rice 	 */
209459276937SPeter Grehan 	pt = moea_pvo_to_pte(pvo, pteidx);
20955244eac9SBenno Rice 	if (pt != NULL) {
209652a7870dSNathan Whitehorn 		moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr);
2097d644a0b7SAlan Cox 		mtx_unlock(&moea_table_mutex);
20985244eac9SBenno Rice 		PVO_PTEGIDX_CLR(pvo);
20995244eac9SBenno Rice 	} else {
210059276937SPeter Grehan 		moea_pte_overflow--;
21015244eac9SBenno Rice 	}
21025244eac9SBenno Rice 
21035244eac9SBenno Rice 	/*
21045244eac9SBenno Rice 	 * Update our statistics.
21055244eac9SBenno Rice 	 */
21065244eac9SBenno Rice 	pvo->pvo_pmap->pm_stats.resident_count--;
2107bfc30490SAlan Cox 	if (pvo->pvo_vaddr & PVO_WIRED)
21085244eac9SBenno Rice 		pvo->pvo_pmap->pm_stats.wired_count--;
21095244eac9SBenno Rice 
21105244eac9SBenno Rice 	/*
2111b4efea53SMark Johnston 	 * Remove this PVO from the PV and pmap lists.
2112b4efea53SMark Johnston 	 */
2113b4efea53SMark Johnston 	LIST_REMOVE(pvo, pvo_vlink);
2114b4efea53SMark Johnston 	RB_REMOVE(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo);
2115b4efea53SMark Johnston 
2116b4efea53SMark Johnston 	/*
21175244eac9SBenno Rice 	 * Save the REF/CHG bits into their cache if the page is managed.
2118b4efea53SMark Johnston 	 * Clear PGA_WRITEABLE if all mappings of the page have been removed.
21195244eac9SBenno Rice 	 */
2120d98d0ce2SKonstantin Belousov 	if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED) {
21215244eac9SBenno Rice 		struct vm_page *pg;
21225244eac9SBenno Rice 
212352a7870dSNathan Whitehorn 		pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN);
21245244eac9SBenno Rice 		if (pg != NULL) {
212552a7870dSNathan Whitehorn 			moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo &
21265244eac9SBenno Rice 			    (PTE_REF | PTE_CHG));
2127b4efea53SMark Johnston 			if (LIST_EMPTY(&pg->md.mdpg_pvoh))
2128b4efea53SMark Johnston 				vm_page_aflag_clear(pg, PGA_WRITEABLE);
21295244eac9SBenno Rice 		}
21305244eac9SBenno Rice 	}
21315244eac9SBenno Rice 
21325244eac9SBenno Rice 	/*
21335244eac9SBenno Rice 	 * Remove this from the overflow list and return it to the pool
21345244eac9SBenno Rice 	 * if we aren't going to reuse it.
21355244eac9SBenno Rice 	 */
21365244eac9SBenno Rice 	LIST_REMOVE(pvo, pvo_olink);
213749f8f727SBenno Rice 	if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
213859276937SPeter Grehan 		uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone :
213959276937SPeter Grehan 		    moea_upvo_zone, pvo);
214059276937SPeter Grehan 	moea_pvo_entries--;
214159276937SPeter Grehan 	moea_pvo_remove_calls++;
21425244eac9SBenno Rice }
21435244eac9SBenno Rice 
21445244eac9SBenno Rice static __inline int
214559276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx)
21465244eac9SBenno Rice {
21475244eac9SBenno Rice 	int	pteidx;
21485244eac9SBenno Rice 
21495244eac9SBenno Rice 	/*
21505244eac9SBenno Rice 	 * We can find the actual pte entry without searching by grabbing
21515244eac9SBenno Rice 	 * the PTEG index from 3 unused bits in pte_lo[11:9] and by
21525244eac9SBenno Rice 	 * noticing the HID bit.
21535244eac9SBenno Rice 	 */
21545244eac9SBenno Rice 	pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo);
215552a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_HID)
215659276937SPeter Grehan 		pteidx ^= moea_pteg_mask * 8;
21575244eac9SBenno Rice 
21585244eac9SBenno Rice 	return (pteidx);
21595244eac9SBenno Rice }
21605244eac9SBenno Rice 
21615244eac9SBenno Rice static struct pvo_entry *
216259276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p)
21635244eac9SBenno Rice {
21645244eac9SBenno Rice 	struct	pvo_entry *pvo;
21655244eac9SBenno Rice 	int	ptegidx;
21665244eac9SBenno Rice 	u_int	sr;
21675244eac9SBenno Rice 
21685244eac9SBenno Rice 	va &= ~ADDR_POFF;
21695244eac9SBenno Rice 	sr = va_to_sr(pm->pm_sr, va);
21705244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, va);
21715244eac9SBenno Rice 
217259276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
217359276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
21745244eac9SBenno Rice 		if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) {
21755244eac9SBenno Rice 			if (pteidx_p)
217659276937SPeter Grehan 				*pteidx_p = moea_pvo_pte_index(pvo, ptegidx);
2177f489bf21SAlan Cox 			break;
21785244eac9SBenno Rice 		}
21795244eac9SBenno Rice 	}
218059276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
21815244eac9SBenno Rice 
2182f489bf21SAlan Cox 	return (pvo);
21835244eac9SBenno Rice }
21845244eac9SBenno Rice 
21855244eac9SBenno Rice static struct pte *
218659276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx)
21875244eac9SBenno Rice {
21885244eac9SBenno Rice 	struct	pte *pt;
21895244eac9SBenno Rice 
21905244eac9SBenno Rice 	/*
21915244eac9SBenno Rice 	 * If we haven't been supplied the ptegidx, calculate it.
21925244eac9SBenno Rice 	 */
21935244eac9SBenno Rice 	if (pteidx == -1) {
21945244eac9SBenno Rice 		int	ptegidx;
21955244eac9SBenno Rice 		u_int	sr;
21965244eac9SBenno Rice 
21975244eac9SBenno Rice 		sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr);
21985244eac9SBenno Rice 		ptegidx = va_to_pteg(sr, pvo->pvo_vaddr);
219959276937SPeter Grehan 		pteidx = moea_pvo_pte_index(pvo, ptegidx);
22005244eac9SBenno Rice 	}
22015244eac9SBenno Rice 
220259276937SPeter Grehan 	pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7];
2203d644a0b7SAlan Cox 	mtx_lock(&moea_table_mutex);
22045244eac9SBenno Rice 
220552a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) {
220659276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no "
22075244eac9SBenno Rice 		    "valid pte index", pvo);
22085244eac9SBenno Rice 	}
22095244eac9SBenno Rice 
221052a7870dSNathan Whitehorn 	if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) {
221159276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo "
22125244eac9SBenno Rice 		    "pvo but no valid pte", pvo);
22135244eac9SBenno Rice 	}
22145244eac9SBenno Rice 
221552a7870dSNathan Whitehorn 	if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) {
221652a7870dSNathan Whitehorn 		if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) {
221759276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p has valid pte in "
221859276937SPeter Grehan 			    "moea_pteg_table %p but invalid in pvo", pvo, pt);
22195244eac9SBenno Rice 		}
22205244eac9SBenno Rice 
222152a7870dSNathan Whitehorn 		if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF))
22225244eac9SBenno Rice 		    != 0) {
222359276937SPeter Grehan 			panic("moea_pvo_to_pte: pvo %p pte does not match "
222459276937SPeter Grehan 			    "pte %p in moea_pteg_table", pvo, pt);
22255244eac9SBenno Rice 		}
22265244eac9SBenno Rice 
2227d644a0b7SAlan Cox 		mtx_assert(&moea_table_mutex, MA_OWNED);
22285244eac9SBenno Rice 		return (pt);
22295244eac9SBenno Rice 	}
22305244eac9SBenno Rice 
223152a7870dSNathan Whitehorn 	if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) {
223259276937SPeter Grehan 		panic("moea_pvo_to_pte: pvo %p has invalid pte %p in "
2233804d1cc1SJustin Hibbits 		    "moea_pteg_table but valid in pvo: %8x, %8x", pvo, pt, pvo->pvo_pte.pte.pte_hi, pt->pte_hi);
22345244eac9SBenno Rice 	}
22355244eac9SBenno Rice 
2236d644a0b7SAlan Cox 	mtx_unlock(&moea_table_mutex);
22375244eac9SBenno Rice 	return (NULL);
22385244eac9SBenno Rice }
22395244eac9SBenno Rice 
22405244eac9SBenno Rice /*
22415244eac9SBenno Rice  * XXX: THIS STUFF SHOULD BE IN pte.c?
22425244eac9SBenno Rice  */
22435244eac9SBenno Rice int
224459276937SPeter Grehan moea_pte_spill(vm_offset_t addr)
22455244eac9SBenno Rice {
22465244eac9SBenno Rice 	struct	pvo_entry *source_pvo, *victim_pvo;
22475244eac9SBenno Rice 	struct	pvo_entry *pvo;
22485244eac9SBenno Rice 	int	ptegidx, i, j;
22495244eac9SBenno Rice 	u_int	sr;
22505244eac9SBenno Rice 	struct	pteg *pteg;
22515244eac9SBenno Rice 	struct	pte *pt;
22525244eac9SBenno Rice 
225359276937SPeter Grehan 	moea_pte_spills++;
22545244eac9SBenno Rice 
2255d080d5fdSBenno Rice 	sr = mfsrin(addr);
22565244eac9SBenno Rice 	ptegidx = va_to_pteg(sr, addr);
22575244eac9SBenno Rice 
22585244eac9SBenno Rice 	/*
22595244eac9SBenno Rice 	 * Have to substitute some entry.  Use the primary hash for this.
22605244eac9SBenno Rice 	 * Use low bits of timebase as random generator.
22615244eac9SBenno Rice 	 */
226259276937SPeter Grehan 	pteg = &moea_pteg_table[ptegidx];
226359276937SPeter Grehan 	mtx_lock(&moea_table_mutex);
22645244eac9SBenno Rice 	__asm __volatile("mftb %0" : "=r"(i));
22655244eac9SBenno Rice 	i &= 7;
22665244eac9SBenno Rice 	pt = &pteg->pt[i];
22675244eac9SBenno Rice 
22685244eac9SBenno Rice 	source_pvo = NULL;
22695244eac9SBenno Rice 	victim_pvo = NULL;
227059276937SPeter Grehan 	LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) {
22715244eac9SBenno Rice 		/*
22725244eac9SBenno Rice 		 * We need to find a pvo entry for this address.
22735244eac9SBenno Rice 		 */
22745244eac9SBenno Rice 		if (source_pvo == NULL &&
227552a7870dSNathan Whitehorn 		    moea_pte_match(&pvo->pvo_pte.pte, sr, addr,
227652a7870dSNathan Whitehorn 		    pvo->pvo_pte.pte.pte_hi & PTE_HID)) {
22775244eac9SBenno Rice 			/*
22785244eac9SBenno Rice 			 * Now found an entry to be spilled into the pteg.
22795244eac9SBenno Rice 			 * The PTE is now valid, so we know it's active.
22805244eac9SBenno Rice 			 */
228152a7870dSNathan Whitehorn 			j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte);
22825244eac9SBenno Rice 
22835244eac9SBenno Rice 			if (j >= 0) {
22845244eac9SBenno Rice 				PVO_PTEGIDX_SET(pvo, j);
228559276937SPeter Grehan 				moea_pte_overflow--;
228659276937SPeter Grehan 				mtx_unlock(&moea_table_mutex);
22875244eac9SBenno Rice 				return (1);
22885244eac9SBenno Rice 			}
22895244eac9SBenno Rice 
22905244eac9SBenno Rice 			source_pvo = pvo;
22915244eac9SBenno Rice 
22925244eac9SBenno Rice 			if (victim_pvo != NULL)
22935244eac9SBenno Rice 				break;
22945244eac9SBenno Rice 		}
22955244eac9SBenno Rice 
22965244eac9SBenno Rice 		/*
22975244eac9SBenno Rice 		 * We also need the pvo entry of the victim we are replacing
22985244eac9SBenno Rice 		 * so save the R & C bits of the PTE.
22995244eac9SBenno Rice 		 */
23005244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL &&
230152a7870dSNathan Whitehorn 		    moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
23025244eac9SBenno Rice 			victim_pvo = pvo;
23035244eac9SBenno Rice 			if (source_pvo != NULL)
23045244eac9SBenno Rice 				break;
23055244eac9SBenno Rice 		}
23065244eac9SBenno Rice 	}
23075244eac9SBenno Rice 
2308f489bf21SAlan Cox 	if (source_pvo == NULL) {
230959276937SPeter Grehan 		mtx_unlock(&moea_table_mutex);
23105244eac9SBenno Rice 		return (0);
2311f489bf21SAlan Cox 	}
23125244eac9SBenno Rice 
23135244eac9SBenno Rice 	if (victim_pvo == NULL) {
23145244eac9SBenno Rice 		if ((pt->pte_hi & PTE_HID) == 0)
231559276937SPeter Grehan 			panic("moea_pte_spill: victim p-pte (%p) has no pvo"
23165244eac9SBenno Rice 			    "entry", pt);
23175244eac9SBenno Rice 
23185244eac9SBenno Rice 		/*
23195244eac9SBenno Rice 		 * If this is a secondary PTE, we need to search it's primary
23205244eac9SBenno Rice 		 * pvo bucket for the matching PVO.
23215244eac9SBenno Rice 		 */
232259276937SPeter Grehan 		LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask],
23235244eac9SBenno Rice 		    pvo_olink) {
23245244eac9SBenno Rice 			/*
23255244eac9SBenno Rice 			 * We also need the pvo entry of the victim we are
23265244eac9SBenno Rice 			 * replacing so save the R & C bits of the PTE.
23275244eac9SBenno Rice 			 */
232852a7870dSNathan Whitehorn 			if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) {
23295244eac9SBenno Rice 				victim_pvo = pvo;
23305244eac9SBenno Rice 				break;
23315244eac9SBenno Rice 			}
23325244eac9SBenno Rice 		}
23335244eac9SBenno Rice 
23345244eac9SBenno Rice 		if (victim_pvo == NULL)
233559276937SPeter Grehan 			panic("moea_pte_spill: victim s-pte (%p) has no pvo"
23365244eac9SBenno Rice 			    "entry", pt);
23375244eac9SBenno Rice 	}
23385244eac9SBenno Rice 
23395244eac9SBenno Rice 	/*
23405244eac9SBenno Rice 	 * We are invalidating the TLB entry for the EA we are replacing even
23415244eac9SBenno Rice 	 * though it's valid.  If we don't, we lose any ref/chg bit changes
23425244eac9SBenno Rice 	 * contained in the TLB entry.
23435244eac9SBenno Rice 	 */
234452a7870dSNathan Whitehorn 	source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID;
23455244eac9SBenno Rice 
234652a7870dSNathan Whitehorn 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
234752a7870dSNathan Whitehorn 	moea_pte_set(pt, &source_pvo->pvo_pte.pte);
23485244eac9SBenno Rice 
23495244eac9SBenno Rice 	PVO_PTEGIDX_CLR(victim_pvo);
23505244eac9SBenno Rice 	PVO_PTEGIDX_SET(source_pvo, i);
235159276937SPeter Grehan 	moea_pte_replacements++;
23525244eac9SBenno Rice 
235359276937SPeter Grehan 	mtx_unlock(&moea_table_mutex);
23545244eac9SBenno Rice 	return (1);
23555244eac9SBenno Rice }
23565244eac9SBenno Rice 
2357804d1cc1SJustin Hibbits static __inline struct pvo_entry *
2358804d1cc1SJustin Hibbits moea_pte_spillable_ident(u_int ptegidx)
2359804d1cc1SJustin Hibbits {
2360804d1cc1SJustin Hibbits 	struct	pte *pt;
2361804d1cc1SJustin Hibbits 	struct	pvo_entry *pvo_walk, *pvo = NULL;
2362804d1cc1SJustin Hibbits 
2363804d1cc1SJustin Hibbits 	LIST_FOREACH(pvo_walk, &moea_pvo_table[ptegidx], pvo_olink) {
2364804d1cc1SJustin Hibbits 		if (pvo_walk->pvo_vaddr & PVO_WIRED)
2365804d1cc1SJustin Hibbits 			continue;
2366804d1cc1SJustin Hibbits 
2367804d1cc1SJustin Hibbits 		if (!(pvo_walk->pvo_pte.pte.pte_hi & PTE_VALID))
2368804d1cc1SJustin Hibbits 			continue;
2369804d1cc1SJustin Hibbits 
2370804d1cc1SJustin Hibbits 		pt = moea_pvo_to_pte(pvo_walk, -1);
2371804d1cc1SJustin Hibbits 
2372804d1cc1SJustin Hibbits 		if (pt == NULL)
2373804d1cc1SJustin Hibbits 			continue;
2374804d1cc1SJustin Hibbits 
2375804d1cc1SJustin Hibbits 		pvo = pvo_walk;
2376804d1cc1SJustin Hibbits 
2377804d1cc1SJustin Hibbits 		mtx_unlock(&moea_table_mutex);
2378804d1cc1SJustin Hibbits 		if (!(pt->pte_lo & PTE_REF))
2379804d1cc1SJustin Hibbits 			return (pvo_walk);
2380804d1cc1SJustin Hibbits 	}
2381804d1cc1SJustin Hibbits 
2382804d1cc1SJustin Hibbits 	return (pvo);
2383804d1cc1SJustin Hibbits }
2384804d1cc1SJustin Hibbits 
23855244eac9SBenno Rice static int
238659276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt)
23875244eac9SBenno Rice {
23885244eac9SBenno Rice 	struct	pte *pt;
2389804d1cc1SJustin Hibbits 	struct	pvo_entry *victim_pvo;
23905244eac9SBenno Rice 	int	i;
2391804d1cc1SJustin Hibbits 	int	victim_idx;
2392804d1cc1SJustin Hibbits 	u_int	pteg_bkpidx = ptegidx;
23935244eac9SBenno Rice 
2394d644a0b7SAlan Cox 	mtx_assert(&moea_table_mutex, MA_OWNED);
2395d644a0b7SAlan Cox 
23965244eac9SBenno Rice 	/*
23975244eac9SBenno Rice 	 * First try primary hash.
23985244eac9SBenno Rice 	 */
239959276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
24005244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
24015244eac9SBenno Rice 			pvo_pt->pte_hi &= ~PTE_HID;
240259276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
24035244eac9SBenno Rice 			return (i);
24045244eac9SBenno Rice 		}
24055244eac9SBenno Rice 	}
24065244eac9SBenno Rice 
24075244eac9SBenno Rice 	/*
24085244eac9SBenno Rice 	 * Now try secondary hash.
24095244eac9SBenno Rice 	 */
241059276937SPeter Grehan 	ptegidx ^= moea_pteg_mask;
2411bd8e6f87SPeter Grehan 
241259276937SPeter Grehan 	for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) {
24135244eac9SBenno Rice 		if ((pt->pte_hi & PTE_VALID) == 0) {
24145244eac9SBenno Rice 			pvo_pt->pte_hi |= PTE_HID;
241559276937SPeter Grehan 			moea_pte_set(pt, pvo_pt);
24165244eac9SBenno Rice 			return (i);
24175244eac9SBenno Rice 		}
24185244eac9SBenno Rice 	}
24195244eac9SBenno Rice 
2420804d1cc1SJustin Hibbits 	/* Try again, but this time try to force a PTE out. */
2421804d1cc1SJustin Hibbits 	ptegidx = pteg_bkpidx;
2422804d1cc1SJustin Hibbits 
2423804d1cc1SJustin Hibbits 	victim_pvo = moea_pte_spillable_ident(ptegidx);
2424804d1cc1SJustin Hibbits 	if (victim_pvo == NULL) {
2425804d1cc1SJustin Hibbits 		ptegidx ^= moea_pteg_mask;
2426804d1cc1SJustin Hibbits 		victim_pvo = moea_pte_spillable_ident(ptegidx);
2427804d1cc1SJustin Hibbits 	}
2428804d1cc1SJustin Hibbits 
2429804d1cc1SJustin Hibbits 	if (victim_pvo == NULL) {
243059276937SPeter Grehan 		panic("moea_pte_insert: overflow");
24315244eac9SBenno Rice 		return (-1);
24325244eac9SBenno Rice 	}
24335244eac9SBenno Rice 
2434804d1cc1SJustin Hibbits 	victim_idx = moea_pvo_pte_index(victim_pvo, ptegidx);
2435804d1cc1SJustin Hibbits 
2436804d1cc1SJustin Hibbits 	if (pteg_bkpidx == ptegidx)
2437804d1cc1SJustin Hibbits 		pvo_pt->pte_hi &= ~PTE_HID;
2438804d1cc1SJustin Hibbits 	else
2439804d1cc1SJustin Hibbits 		pvo_pt->pte_hi |= PTE_HID;
2440804d1cc1SJustin Hibbits 
2441804d1cc1SJustin Hibbits 	/*
2442804d1cc1SJustin Hibbits 	 * Synchronize the sacrifice PTE with its PVO, then mark both
2443804d1cc1SJustin Hibbits 	 * invalid. The PVO will be reused when/if the VM system comes
2444804d1cc1SJustin Hibbits 	 * here after a fault.
2445804d1cc1SJustin Hibbits 	 */
2446804d1cc1SJustin Hibbits 	pt = &moea_pteg_table[victim_idx >> 3].pt[victim_idx & 7];
2447804d1cc1SJustin Hibbits 
2448804d1cc1SJustin Hibbits 	if (pt->pte_hi != victim_pvo->pvo_pte.pte.pte_hi)
2449804d1cc1SJustin Hibbits 	    panic("Victim PVO doesn't match PTE! PVO: %8x, PTE: %8x", victim_pvo->pvo_pte.pte.pte_hi, pt->pte_hi);
2450804d1cc1SJustin Hibbits 
2451804d1cc1SJustin Hibbits 	/*
2452804d1cc1SJustin Hibbits 	 * Set the new PTE.
2453804d1cc1SJustin Hibbits 	 */
2454804d1cc1SJustin Hibbits 	moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr);
2455804d1cc1SJustin Hibbits 	PVO_PTEGIDX_CLR(victim_pvo);
2456804d1cc1SJustin Hibbits 	moea_pte_overflow++;
2457804d1cc1SJustin Hibbits 	moea_pte_set(pt, pvo_pt);
2458804d1cc1SJustin Hibbits 
2459804d1cc1SJustin Hibbits 	return (victim_idx & 7);
2460804d1cc1SJustin Hibbits }
2461804d1cc1SJustin Hibbits 
24625244eac9SBenno Rice static boolean_t
246359276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit)
24645244eac9SBenno Rice {
24655244eac9SBenno Rice 	struct	pvo_entry *pvo;
24665244eac9SBenno Rice 	struct	pte *pt;
24675244eac9SBenno Rice 
24688d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
246959276937SPeter Grehan 	if (moea_attr_fetch(m) & ptebit)
24705244eac9SBenno Rice 		return (TRUE);
24715244eac9SBenno Rice 
24725244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
24735244eac9SBenno Rice 
24745244eac9SBenno Rice 		/*
24755244eac9SBenno Rice 		 * See if we saved the bit off.  If so, cache it and return
24765244eac9SBenno Rice 		 * success.
24775244eac9SBenno Rice 		 */
247852a7870dSNathan Whitehorn 		if (pvo->pvo_pte.pte.pte_lo & ptebit) {
247959276937SPeter Grehan 			moea_attr_save(m, ptebit);
24805244eac9SBenno Rice 			return (TRUE);
24815244eac9SBenno Rice 		}
24825244eac9SBenno Rice 	}
24835244eac9SBenno Rice 
24845244eac9SBenno Rice 	/*
24855244eac9SBenno Rice 	 * No luck, now go through the hard part of looking at the PTEs
24865244eac9SBenno Rice 	 * themselves.  Sync so that any pending REF/CHG bits are flushed to
24875244eac9SBenno Rice 	 * the PTEs.
24885244eac9SBenno Rice 	 */
2489e4f72b32SMarcel Moolenaar 	powerpc_sync();
24905244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
24915244eac9SBenno Rice 
24925244eac9SBenno Rice 		/*
24935244eac9SBenno Rice 		 * See if this pvo has a valid PTE.  if so, fetch the
24945244eac9SBenno Rice 		 * REF/CHG bits from the valid PTE.  If the appropriate
24955244eac9SBenno Rice 		 * ptebit is set, cache it and return success.
24965244eac9SBenno Rice 		 */
249759276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
24985244eac9SBenno Rice 		if (pt != NULL) {
249952a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
2500d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
250152a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
250259276937SPeter Grehan 				moea_attr_save(m, ptebit);
25035244eac9SBenno Rice 				return (TRUE);
25045244eac9SBenno Rice 			}
25055244eac9SBenno Rice 		}
25065244eac9SBenno Rice 	}
25075244eac9SBenno Rice 
25084f7daed0SAndrew Gallatin 	return (FALSE);
25095244eac9SBenno Rice }
25105244eac9SBenno Rice 
251103b6e025SPeter Grehan static u_int
2512ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit)
25135244eac9SBenno Rice {
251403b6e025SPeter Grehan 	u_int	count;
25155244eac9SBenno Rice 	struct	pvo_entry *pvo;
25165244eac9SBenno Rice 	struct	pte *pt;
2517ce186587SAlan Cox 
25188d9e6d9fSAlan Cox 	rw_assert(&pvh_global_lock, RA_WLOCKED);
25195244eac9SBenno Rice 
25205244eac9SBenno Rice 	/*
25215244eac9SBenno Rice 	 * Clear the cached value.
25225244eac9SBenno Rice 	 */
252359276937SPeter Grehan 	moea_attr_clear(m, ptebit);
25245244eac9SBenno Rice 
25255244eac9SBenno Rice 	/*
25265244eac9SBenno Rice 	 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so
25275244eac9SBenno Rice 	 * we can reset the right ones).  note that since the pvo entries and
25285244eac9SBenno Rice 	 * list heads are accessed via BAT0 and are never placed in the page
25295244eac9SBenno Rice 	 * table, we don't have to worry about further accesses setting the
25305244eac9SBenno Rice 	 * REF/CHG bits.
25315244eac9SBenno Rice 	 */
2532e4f72b32SMarcel Moolenaar 	powerpc_sync();
25335244eac9SBenno Rice 
25345244eac9SBenno Rice 	/*
25355244eac9SBenno Rice 	 * For each pvo entry, clear the pvo's ptebit.  If this pvo has a
25365244eac9SBenno Rice 	 * valid pte clear the ptebit from the valid pte.
25375244eac9SBenno Rice 	 */
253803b6e025SPeter Grehan 	count = 0;
25395244eac9SBenno Rice 	LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
254059276937SPeter Grehan 		pt = moea_pvo_to_pte(pvo, -1);
25415244eac9SBenno Rice 		if (pt != NULL) {
254252a7870dSNathan Whitehorn 			moea_pte_synch(pt, &pvo->pvo_pte.pte);
254352a7870dSNathan Whitehorn 			if (pvo->pvo_pte.pte.pte_lo & ptebit) {
254403b6e025SPeter Grehan 				count++;
254559276937SPeter Grehan 				moea_pte_clear(pt, PVO_VADDR(pvo), ptebit);
25465244eac9SBenno Rice 			}
2547d644a0b7SAlan Cox 			mtx_unlock(&moea_table_mutex);
254803b6e025SPeter Grehan 		}
254952a7870dSNathan Whitehorn 		pvo->pvo_pte.pte.pte_lo &= ~ptebit;
25505244eac9SBenno Rice 	}
25515244eac9SBenno Rice 
255203b6e025SPeter Grehan 	return (count);
2553bdf71f56SBenno Rice }
25548bbfa33aSBenno Rice 
25558bbfa33aSBenno Rice /*
255632bc7846SPeter Grehan  * Return true if the physical range is encompassed by the battable[idx]
255732bc7846SPeter Grehan  */
255832bc7846SPeter Grehan static int
25590936003eSJustin Hibbits moea_bat_mapped(int idx, vm_paddr_t pa, vm_size_t size)
256032bc7846SPeter Grehan {
256132bc7846SPeter Grehan 	u_int prot;
256232bc7846SPeter Grehan 	u_int32_t start;
256332bc7846SPeter Grehan 	u_int32_t end;
256432bc7846SPeter Grehan 	u_int32_t bat_ble;
256532bc7846SPeter Grehan 
256632bc7846SPeter Grehan 	/*
256732bc7846SPeter Grehan 	 * Return immediately if not a valid mapping
256832bc7846SPeter Grehan 	 */
2569c4bcebedSNathan Whitehorn 	if (!(battable[idx].batu & BAT_Vs))
257032bc7846SPeter Grehan 		return (EINVAL);
257132bc7846SPeter Grehan 
257232bc7846SPeter Grehan 	/*
257332bc7846SPeter Grehan 	 * The BAT entry must be cache-inhibited, guarded, and r/w
257432bc7846SPeter Grehan 	 * so it can function as an i/o page
257532bc7846SPeter Grehan 	 */
257632bc7846SPeter Grehan 	prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW);
257732bc7846SPeter Grehan 	if (prot != (BAT_I|BAT_G|BAT_PP_RW))
257832bc7846SPeter Grehan 		return (EPERM);
257932bc7846SPeter Grehan 
258032bc7846SPeter Grehan 	/*
258132bc7846SPeter Grehan 	 * The address should be within the BAT range. Assume that the
258232bc7846SPeter Grehan 	 * start address in the BAT has the correct alignment (thus
258332bc7846SPeter Grehan 	 * not requiring masking)
258432bc7846SPeter Grehan 	 */
258532bc7846SPeter Grehan 	start = battable[idx].batl & BAT_PBS;
258632bc7846SPeter Grehan 	bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03;
258732bc7846SPeter Grehan 	end = start | (bat_ble << 15) | 0x7fff;
258832bc7846SPeter Grehan 
258932bc7846SPeter Grehan 	if ((pa < start) || ((pa + size) > end))
259032bc7846SPeter Grehan 		return (ERANGE);
259132bc7846SPeter Grehan 
259232bc7846SPeter Grehan 	return (0);
259332bc7846SPeter Grehan }
259432bc7846SPeter Grehan 
259559276937SPeter Grehan boolean_t
259620b79612SRafal Jaworowski moea_dev_direct_mapped(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2597c0763d37SSuleiman Souhlal {
2598c0763d37SSuleiman Souhlal 	int i;
2599c0763d37SSuleiman Souhlal 
2600c0763d37SSuleiman Souhlal 	/*
2601c0763d37SSuleiman Souhlal 	 * This currently does not work for entries that
2602c0763d37SSuleiman Souhlal 	 * overlap 256M BAT segments.
2603c0763d37SSuleiman Souhlal 	 */
2604c0763d37SSuleiman Souhlal 
2605c0763d37SSuleiman Souhlal 	for(i = 0; i < 16; i++)
260659276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
2607c0763d37SSuleiman Souhlal 			return (0);
2608c0763d37SSuleiman Souhlal 
2609c0763d37SSuleiman Souhlal 	return (EFAULT);
2610c0763d37SSuleiman Souhlal }
261132bc7846SPeter Grehan 
261232bc7846SPeter Grehan /*
26138bbfa33aSBenno Rice  * Map a set of physical memory pages into the kernel virtual
26148bbfa33aSBenno Rice  * address space. Return a pointer to where it is mapped. This
26158bbfa33aSBenno Rice  * routine is intended to be used for mapping device memory,
26168bbfa33aSBenno Rice  * NOT real memory.
26178bbfa33aSBenno Rice  */
26188bbfa33aSBenno Rice void *
261920b79612SRafal Jaworowski moea_mapdev(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
26208bbfa33aSBenno Rice {
2621c1f4123bSNathan Whitehorn 
2622c1f4123bSNathan Whitehorn 	return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2623c1f4123bSNathan Whitehorn }
2624c1f4123bSNathan Whitehorn 
2625c1f4123bSNathan Whitehorn void *
26260936003eSJustin Hibbits moea_mapdev_attr(mmu_t mmu, vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
2627c1f4123bSNathan Whitehorn {
262832bc7846SPeter Grehan 	vm_offset_t va, tmpva, ppa, offset;
262932bc7846SPeter Grehan 	int i;
26308bbfa33aSBenno Rice 
263132bc7846SPeter Grehan 	ppa = trunc_page(pa);
26328bbfa33aSBenno Rice 	offset = pa & PAGE_MASK;
26338bbfa33aSBenno Rice 	size = roundup(offset + size, PAGE_SIZE);
26348bbfa33aSBenno Rice 
263532bc7846SPeter Grehan 	/*
263632bc7846SPeter Grehan 	 * If the physical address lies within a valid BAT table entry,
263732bc7846SPeter Grehan 	 * return the 1:1 mapping. This currently doesn't work
263832bc7846SPeter Grehan 	 * for regions that overlap 256M BAT segments.
263932bc7846SPeter Grehan 	 */
264032bc7846SPeter Grehan 	for (i = 0; i < 16; i++) {
264159276937SPeter Grehan 		if (moea_bat_mapped(i, pa, size) == 0)
264232bc7846SPeter Grehan 			return ((void *) pa);
264332bc7846SPeter Grehan 	}
264432bc7846SPeter Grehan 
26455df87b21SJeff Roberson 	va = kva_alloc(size);
26468bbfa33aSBenno Rice 	if (!va)
264759276937SPeter Grehan 		panic("moea_mapdev: Couldn't alloc kernel virtual memory");
26488bbfa33aSBenno Rice 
26498bbfa33aSBenno Rice 	for (tmpva = va; size > 0;) {
2650c1f4123bSNathan Whitehorn 		moea_kenter_attr(mmu, tmpva, ppa, ma);
2651e4f72b32SMarcel Moolenaar 		tlbie(tmpva);
26528bbfa33aSBenno Rice 		size -= PAGE_SIZE;
26538bbfa33aSBenno Rice 		tmpva += PAGE_SIZE;
265432bc7846SPeter Grehan 		ppa += PAGE_SIZE;
26558bbfa33aSBenno Rice 	}
26568bbfa33aSBenno Rice 
26578bbfa33aSBenno Rice 	return ((void *)(va + offset));
26588bbfa33aSBenno Rice }
26598bbfa33aSBenno Rice 
26608bbfa33aSBenno Rice void
266159276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
26628bbfa33aSBenno Rice {
26638bbfa33aSBenno Rice 	vm_offset_t base, offset;
26648bbfa33aSBenno Rice 
266532bc7846SPeter Grehan 	/*
266632bc7846SPeter Grehan 	 * If this is outside kernel virtual space, then it's a
266732bc7846SPeter Grehan 	 * battable entry and doesn't require unmapping
266832bc7846SPeter Grehan 	 */
2669ab739706SNathan Whitehorn 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) {
26708bbfa33aSBenno Rice 		base = trunc_page(va);
26718bbfa33aSBenno Rice 		offset = va & PAGE_MASK;
26728bbfa33aSBenno Rice 		size = roundup(offset + size, PAGE_SIZE);
26735df87b21SJeff Roberson 		kva_free(base, size);
26748bbfa33aSBenno Rice 	}
267532bc7846SPeter Grehan }
26761a4fcaebSMarcel Moolenaar 
26771a4fcaebSMarcel Moolenaar static void
26781a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
26791a4fcaebSMarcel Moolenaar {
26801a4fcaebSMarcel Moolenaar 	struct pvo_entry *pvo;
26811a4fcaebSMarcel Moolenaar 	vm_offset_t lim;
26821a4fcaebSMarcel Moolenaar 	vm_paddr_t pa;
26831a4fcaebSMarcel Moolenaar 	vm_size_t len;
26841a4fcaebSMarcel Moolenaar 
26851a4fcaebSMarcel Moolenaar 	PMAP_LOCK(pm);
26861a4fcaebSMarcel Moolenaar 	while (sz > 0) {
26871a4fcaebSMarcel Moolenaar 		lim = round_page(va);
26881a4fcaebSMarcel Moolenaar 		len = MIN(lim - va, sz);
26891a4fcaebSMarcel Moolenaar 		pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL);
26901a4fcaebSMarcel Moolenaar 		if (pvo != NULL) {
26911a4fcaebSMarcel Moolenaar 			pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) |
26921a4fcaebSMarcel Moolenaar 			    (va & ADDR_POFF);
26931a4fcaebSMarcel Moolenaar 			moea_syncicache(pa, len);
26941a4fcaebSMarcel Moolenaar 		}
26951a4fcaebSMarcel Moolenaar 		va += len;
26961a4fcaebSMarcel Moolenaar 		sz -= len;
26971a4fcaebSMarcel Moolenaar 	}
26981a4fcaebSMarcel Moolenaar 	PMAP_UNLOCK(pm);
26991a4fcaebSMarcel Moolenaar }
2700afd9cb6cSJustin Hibbits 
2701bdb9ab0dSMark Johnston void
2702bdb9ab0dSMark Johnston moea_dumpsys_map(mmu_t mmu, vm_paddr_t pa, size_t sz, void **va)
2703afd9cb6cSJustin Hibbits {
2704bdb9ab0dSMark Johnston 
2705bdb9ab0dSMark Johnston 	*va = (void *)pa;
2706afd9cb6cSJustin Hibbits }
2707afd9cb6cSJustin Hibbits 
2708bdb9ab0dSMark Johnston extern struct dump_pa dump_map[PHYS_AVAIL_SZ + 1];
2709bdb9ab0dSMark Johnston 
2710bdb9ab0dSMark Johnston void
2711bdb9ab0dSMark Johnston moea_scan_init(mmu_t mmu)
2712afd9cb6cSJustin Hibbits {
2713afd9cb6cSJustin Hibbits 	struct pvo_entry *pvo;
2714afd9cb6cSJustin Hibbits 	vm_offset_t va;
2715bdb9ab0dSMark Johnston 	int i;
2716afd9cb6cSJustin Hibbits 
2717bdb9ab0dSMark Johnston 	if (!do_minidump) {
2718bdb9ab0dSMark Johnston 		/* Initialize phys. segments for dumpsys(). */
2719bdb9ab0dSMark Johnston 		memset(&dump_map, 0, sizeof(dump_map));
2720bdb9ab0dSMark Johnston 		mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
2721bdb9ab0dSMark Johnston 		for (i = 0; i < pregions_sz; i++) {
2722bdb9ab0dSMark Johnston 			dump_map[i].pa_start = pregions[i].mr_start;
2723bdb9ab0dSMark Johnston 			dump_map[i].pa_size = pregions[i].mr_size;
2724afd9cb6cSJustin Hibbits 		}
2725bdb9ab0dSMark Johnston 		return;
2726bdb9ab0dSMark Johnston 	}
2727bdb9ab0dSMark Johnston 
2728bdb9ab0dSMark Johnston 	/* Virtual segments for minidumps: */
2729bdb9ab0dSMark Johnston 	memset(&dump_map, 0, sizeof(dump_map));
2730bdb9ab0dSMark Johnston 
2731bdb9ab0dSMark Johnston 	/* 1st: kernel .data and .bss. */
2732bdb9ab0dSMark Johnston 	dump_map[0].pa_start = trunc_page((uintptr_t)_etext);
2733bdb9ab0dSMark Johnston 	dump_map[0].pa_size =
2734bdb9ab0dSMark Johnston 	    round_page((uintptr_t)_end) - dump_map[0].pa_start;
2735bdb9ab0dSMark Johnston 
2736afd9cb6cSJustin Hibbits 	/* 2nd: msgbuf and tables (see pmap_bootstrap()). */
2737bdb9ab0dSMark Johnston 	dump_map[1].pa_start = (vm_paddr_t)msgbufp->msg_ptr;
2738bdb9ab0dSMark Johnston 	dump_map[1].pa_size = round_page(msgbufp->msg_size);
2739bdb9ab0dSMark Johnston 
2740afd9cb6cSJustin Hibbits 	/* 3rd: kernel VM. */
2741bdb9ab0dSMark Johnston 	va = dump_map[1].pa_start + dump_map[1].pa_size;
2742afd9cb6cSJustin Hibbits 	/* Find start of next chunk (from va). */
2743afd9cb6cSJustin Hibbits 	while (va < virtual_end) {
2744afd9cb6cSJustin Hibbits 		/* Don't dump the buffer cache. */
2745bdb9ab0dSMark Johnston 		if (va >= kmi.buffer_sva && va < kmi.buffer_eva) {
2746afd9cb6cSJustin Hibbits 			va = kmi.buffer_eva;
2747afd9cb6cSJustin Hibbits 			continue;
2748afd9cb6cSJustin Hibbits 		}
2749bdb9ab0dSMark Johnston 		pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL);
2750bdb9ab0dSMark Johnston 		if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID))
2751afd9cb6cSJustin Hibbits 			break;
2752afd9cb6cSJustin Hibbits 		va += PAGE_SIZE;
2753afd9cb6cSJustin Hibbits 	}
2754afd9cb6cSJustin Hibbits 	if (va < virtual_end) {
2755bdb9ab0dSMark Johnston 		dump_map[2].pa_start = va;
2756afd9cb6cSJustin Hibbits 		va += PAGE_SIZE;
2757afd9cb6cSJustin Hibbits 		/* Find last page in chunk. */
2758afd9cb6cSJustin Hibbits 		while (va < virtual_end) {
2759afd9cb6cSJustin Hibbits 			/* Don't run into the buffer cache. */
2760afd9cb6cSJustin Hibbits 			if (va == kmi.buffer_sva)
2761afd9cb6cSJustin Hibbits 				break;
2762bdb9ab0dSMark Johnston 			pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF,
2763bdb9ab0dSMark Johnston 			    NULL);
2764afd9cb6cSJustin Hibbits 			if (pvo == NULL ||
2765afd9cb6cSJustin Hibbits 			    !(pvo->pvo_pte.pte.pte_hi & PTE_VALID))
2766afd9cb6cSJustin Hibbits 				break;
2767afd9cb6cSJustin Hibbits 			va += PAGE_SIZE;
2768afd9cb6cSJustin Hibbits 		}
2769bdb9ab0dSMark Johnston 		dump_map[2].pa_size = va - dump_map[2].pa_start;
2770afd9cb6cSJustin Hibbits 	}
2771afd9cb6cSJustin Hibbits }
2772