160727d8bSWarner Losh /*- 25244eac9SBenno Rice * Copyright (c) 2001 The NetBSD Foundation, Inc. 35244eac9SBenno Rice * All rights reserved. 45244eac9SBenno Rice * 55244eac9SBenno Rice * This code is derived from software contributed to The NetBSD Foundation 65244eac9SBenno Rice * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc. 75244eac9SBenno Rice * 85244eac9SBenno Rice * Redistribution and use in source and binary forms, with or without 95244eac9SBenno Rice * modification, are permitted provided that the following conditions 105244eac9SBenno Rice * are met: 115244eac9SBenno Rice * 1. Redistributions of source code must retain the above copyright 125244eac9SBenno Rice * notice, this list of conditions and the following disclaimer. 135244eac9SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 145244eac9SBenno Rice * notice, this list of conditions and the following disclaimer in the 155244eac9SBenno Rice * documentation and/or other materials provided with the distribution. 165244eac9SBenno Rice * 3. All advertising materials mentioning features or use of this software 175244eac9SBenno Rice * must display the following acknowledgement: 185244eac9SBenno Rice * This product includes software developed by the NetBSD 195244eac9SBenno Rice * Foundation, Inc. and its contributors. 205244eac9SBenno Rice * 4. Neither the name of The NetBSD Foundation nor the names of its 215244eac9SBenno Rice * contributors may be used to endorse or promote products derived 225244eac9SBenno Rice * from this software without specific prior written permission. 235244eac9SBenno Rice * 245244eac9SBenno Rice * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 255244eac9SBenno Rice * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 265244eac9SBenno Rice * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 275244eac9SBenno Rice * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 285244eac9SBenno Rice * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 295244eac9SBenno Rice * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 305244eac9SBenno Rice * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 315244eac9SBenno Rice * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 325244eac9SBenno Rice * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 335244eac9SBenno Rice * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 345244eac9SBenno Rice * POSSIBILITY OF SUCH DAMAGE. 355244eac9SBenno Rice */ 3660727d8bSWarner Losh /*- 37f9bac91bSBenno Rice * Copyright (C) 1995, 1996 Wolfgang Solfrank. 38f9bac91bSBenno Rice * Copyright (C) 1995, 1996 TooLs GmbH. 39f9bac91bSBenno Rice * All rights reserved. 40f9bac91bSBenno Rice * 41f9bac91bSBenno Rice * Redistribution and use in source and binary forms, with or without 42f9bac91bSBenno Rice * modification, are permitted provided that the following conditions 43f9bac91bSBenno Rice * are met: 44f9bac91bSBenno Rice * 1. Redistributions of source code must retain the above copyright 45f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer. 46f9bac91bSBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 47f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer in the 48f9bac91bSBenno Rice * documentation and/or other materials provided with the distribution. 49f9bac91bSBenno Rice * 3. All advertising materials mentioning features or use of this software 50f9bac91bSBenno Rice * must display the following acknowledgement: 51f9bac91bSBenno Rice * This product includes software developed by TooLs GmbH. 52f9bac91bSBenno Rice * 4. The name of TooLs GmbH may not be used to endorse or promote products 53f9bac91bSBenno Rice * derived from this software without specific prior written permission. 54f9bac91bSBenno Rice * 55f9bac91bSBenno Rice * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 56f9bac91bSBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 57f9bac91bSBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 58f9bac91bSBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 59f9bac91bSBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 60f9bac91bSBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 61f9bac91bSBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 62f9bac91bSBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 63f9bac91bSBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 64f9bac91bSBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 65f9bac91bSBenno Rice * 66111c77dcSBenno Rice * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $ 67f9bac91bSBenno Rice */ 6860727d8bSWarner Losh /*- 69f9bac91bSBenno Rice * Copyright (C) 2001 Benno Rice. 70f9bac91bSBenno Rice * All rights reserved. 71f9bac91bSBenno Rice * 72f9bac91bSBenno Rice * Redistribution and use in source and binary forms, with or without 73f9bac91bSBenno Rice * modification, are permitted provided that the following conditions 74f9bac91bSBenno Rice * are met: 75f9bac91bSBenno Rice * 1. Redistributions of source code must retain the above copyright 76f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer. 77f9bac91bSBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 78f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer in the 79f9bac91bSBenno Rice * documentation and/or other materials provided with the distribution. 80f9bac91bSBenno Rice * 81f9bac91bSBenno Rice * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 82f9bac91bSBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 83f9bac91bSBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 84f9bac91bSBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 85f9bac91bSBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 86f9bac91bSBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 87f9bac91bSBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 88f9bac91bSBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 89f9bac91bSBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 90f9bac91bSBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 91f9bac91bSBenno Rice */ 92f9bac91bSBenno Rice 938368cf8fSDavid E. O'Brien #include <sys/cdefs.h> 948368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$"); 95f9bac91bSBenno Rice 965244eac9SBenno Rice /* 975244eac9SBenno Rice * Manages physical address maps. 985244eac9SBenno Rice * 995244eac9SBenno Rice * Since the information managed by this module is also stored by the 1005244eac9SBenno Rice * logical address mapping module, this module may throw away valid virtual 1015244eac9SBenno Rice * to physical mappings at almost any time. However, invalidations of 1025244eac9SBenno Rice * mappings must be done as requested. 1035244eac9SBenno Rice * 1045244eac9SBenno Rice * In order to cope with hardware architectures which make virtual to 1055244eac9SBenno Rice * physical map invalidates expensive, this module may delay invalidate 1065244eac9SBenno Rice * reduced protection operations until such time as they are actually 1075244eac9SBenno Rice * necessary. This module is given full information as to which processors 1085244eac9SBenno Rice * are currently using which maps, and to when physical maps must be made 1095244eac9SBenno Rice * correct. 1105244eac9SBenno Rice */ 1115244eac9SBenno Rice 112ad7a226fSPeter Wemm #include "opt_kstack_pages.h" 113ad7a226fSPeter Wemm 114f9bac91bSBenno Rice #include <sys/param.h> 1150b27d710SPeter Wemm #include <sys/kernel.h> 116c47dd3dbSAttilio Rao #include <sys/queue.h> 117c47dd3dbSAttilio Rao #include <sys/cpuset.h> 1185244eac9SBenno Rice #include <sys/ktr.h> 11994e0b85eSMark Peek #include <sys/lock.h> 1205244eac9SBenno Rice #include <sys/msgbuf.h> 121f9bac91bSBenno Rice #include <sys/mutex.h> 1225244eac9SBenno Rice #include <sys/proc.h> 1233653f5cbSAlan Cox #include <sys/rwlock.h> 124c47dd3dbSAttilio Rao #include <sys/sched.h> 1255244eac9SBenno Rice #include <sys/sysctl.h> 1265244eac9SBenno Rice #include <sys/systm.h> 1275244eac9SBenno Rice #include <sys/vmmeter.h> 1285244eac9SBenno Rice 1295244eac9SBenno Rice #include <dev/ofw/openfirm.h> 130f9bac91bSBenno Rice 131f9bac91bSBenno Rice #include <vm/vm.h> 132f9bac91bSBenno Rice #include <vm/vm_param.h> 133f9bac91bSBenno Rice #include <vm/vm_kern.h> 134f9bac91bSBenno Rice #include <vm/vm_page.h> 135f9bac91bSBenno Rice #include <vm/vm_map.h> 136f9bac91bSBenno Rice #include <vm/vm_object.h> 137f9bac91bSBenno Rice #include <vm/vm_extern.h> 138f9bac91bSBenno Rice #include <vm/vm_pageout.h> 139378862a7SJeff Roberson #include <vm/uma.h> 140f9bac91bSBenno Rice 1417c277971SPeter Grehan #include <machine/cpu.h> 142b40ce02aSNathan Whitehorn #include <machine/platform.h> 143d699b539SMark Peek #include <machine/bat.h> 1445244eac9SBenno Rice #include <machine/frame.h> 1455244eac9SBenno Rice #include <machine/md_var.h> 1465244eac9SBenno Rice #include <machine/psl.h> 147f9bac91bSBenno Rice #include <machine/pte.h> 14812640815SMarcel Moolenaar #include <machine/smp.h> 1495244eac9SBenno Rice #include <machine/sr.h> 15059276937SPeter Grehan #include <machine/mmuvar.h> 151e347e23bSNathan Whitehorn #include <machine/trap_aim.h> 152f9bac91bSBenno Rice 15359276937SPeter Grehan #include "mmu_if.h" 15459276937SPeter Grehan 15559276937SPeter Grehan #define MOEA_DEBUG 156f9bac91bSBenno Rice 1575244eac9SBenno Rice #define TODO panic("%s: not implemented", __func__); 158f9bac91bSBenno Rice 1595244eac9SBenno Rice #define VSID_MAKE(sr, hash) ((sr) | (((hash) & 0xfffff) << 4)) 1605244eac9SBenno Rice #define VSID_TO_SR(vsid) ((vsid) & 0xf) 1615244eac9SBenno Rice #define VSID_TO_HASH(vsid) (((vsid) >> 4) & 0xfffff) 1625244eac9SBenno Rice 1635244eac9SBenno Rice struct ofw_map { 1645244eac9SBenno Rice vm_offset_t om_va; 1655244eac9SBenno Rice vm_size_t om_len; 1665244eac9SBenno Rice vm_offset_t om_pa; 1675244eac9SBenno Rice u_int om_mode; 1685244eac9SBenno Rice }; 169f9bac91bSBenno Rice 170afd9cb6cSJustin Hibbits extern unsigned char _etext[]; 171afd9cb6cSJustin Hibbits extern unsigned char _end[]; 172afd9cb6cSJustin Hibbits 173afd9cb6cSJustin Hibbits extern int dumpsys_minidump; 174afd9cb6cSJustin Hibbits 1755244eac9SBenno Rice /* 1765244eac9SBenno Rice * Map of physical memory regions. 1775244eac9SBenno Rice */ 17831c82d03SBenno Rice static struct mem_region *regions; 17931c82d03SBenno Rice static struct mem_region *pregions; 180c3e289e1SNathan Whitehorn static u_int phys_avail_count; 181c3e289e1SNathan Whitehorn static int regions_sz, pregions_sz; 182aa39961eSBenno Rice static struct ofw_map *translations; 1835244eac9SBenno Rice 184f9bac91bSBenno Rice /* 185f489bf21SAlan Cox * Lock for the pteg and pvo tables. 186f489bf21SAlan Cox */ 18759276937SPeter Grehan struct mtx moea_table_mutex; 188e9b5f218SNathan Whitehorn struct mtx moea_vsid_mutex; 189f489bf21SAlan Cox 190e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */ 191e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx; 192e4f72b32SMarcel Moolenaar 193f489bf21SAlan Cox /* 1945244eac9SBenno Rice * PTEG data. 195f9bac91bSBenno Rice */ 19659276937SPeter Grehan static struct pteg *moea_pteg_table; 19759276937SPeter Grehan u_int moea_pteg_count; 19859276937SPeter Grehan u_int moea_pteg_mask; 1995244eac9SBenno Rice 2005244eac9SBenno Rice /* 2015244eac9SBenno Rice * PVO data. 2025244eac9SBenno Rice */ 20359276937SPeter Grehan struct pvo_head *moea_pvo_table; /* pvo entries by pteg index */ 20459276937SPeter Grehan struct pvo_head moea_pvo_kunmanaged = 20559276937SPeter Grehan LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged); /* list of unmanaged pages */ 2065244eac9SBenno Rice 207cfedf924SAttilio Rao static struct rwlock_padalign pvh_global_lock; 2083653f5cbSAlan Cox 20959276937SPeter Grehan uma_zone_t moea_upvo_zone; /* zone for pvo entries for unmanaged pages */ 21059276937SPeter Grehan uma_zone_t moea_mpvo_zone; /* zone for pvo entries for managed pages */ 2115244eac9SBenno Rice 2120d290675SBenno Rice #define BPVO_POOL_SIZE 32768 21359276937SPeter Grehan static struct pvo_entry *moea_bpvo_pool; 21459276937SPeter Grehan static int moea_bpvo_pool_index = 0; 2155244eac9SBenno Rice 2165244eac9SBenno Rice #define VSID_NBPW (sizeof(u_int32_t) * 8) 21759276937SPeter Grehan static u_int moea_vsid_bitmap[NPMAPS / VSID_NBPW]; 2185244eac9SBenno Rice 21959276937SPeter Grehan static boolean_t moea_initialized = FALSE; 2205244eac9SBenno Rice 2215244eac9SBenno Rice /* 2225244eac9SBenno Rice * Statistics. 2235244eac9SBenno Rice */ 22459276937SPeter Grehan u_int moea_pte_valid = 0; 22559276937SPeter Grehan u_int moea_pte_overflow = 0; 22659276937SPeter Grehan u_int moea_pte_replacements = 0; 22759276937SPeter Grehan u_int moea_pvo_entries = 0; 22859276937SPeter Grehan u_int moea_pvo_enter_calls = 0; 22959276937SPeter Grehan u_int moea_pvo_remove_calls = 0; 23059276937SPeter Grehan u_int moea_pte_spills = 0; 23159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid, 2325244eac9SBenno Rice 0, ""); 23359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD, 23459276937SPeter Grehan &moea_pte_overflow, 0, ""); 23559276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD, 23659276937SPeter Grehan &moea_pte_replacements, 0, ""); 23759276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries, 2385244eac9SBenno Rice 0, ""); 23959276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD, 24059276937SPeter Grehan &moea_pvo_enter_calls, 0, ""); 24159276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD, 24259276937SPeter Grehan &moea_pvo_remove_calls, 0, ""); 24359276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD, 24459276937SPeter Grehan &moea_pte_spills, 0, ""); 2455244eac9SBenno Rice 2465244eac9SBenno Rice /* 24759276937SPeter Grehan * Allocate physical memory for use in moea_bootstrap. 2485244eac9SBenno Rice */ 24959276937SPeter Grehan static vm_offset_t moea_bootstrap_alloc(vm_size_t, u_int); 2505244eac9SBenno Rice 2515244eac9SBenno Rice /* 2525244eac9SBenno Rice * PTE calls. 2535244eac9SBenno Rice */ 25459276937SPeter Grehan static int moea_pte_insert(u_int, struct pte *); 2555244eac9SBenno Rice 2565244eac9SBenno Rice /* 2575244eac9SBenno Rice * PVO calls. 2585244eac9SBenno Rice */ 25959276937SPeter Grehan static int moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *, 2605244eac9SBenno Rice vm_offset_t, vm_offset_t, u_int, int); 26159276937SPeter Grehan static void moea_pvo_remove(struct pvo_entry *, int); 26259276937SPeter Grehan static struct pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *); 26359276937SPeter Grehan static struct pte *moea_pvo_to_pte(const struct pvo_entry *, int); 2645244eac9SBenno Rice 2655244eac9SBenno Rice /* 2665244eac9SBenno Rice * Utility routines. 2675244eac9SBenno Rice */ 268ce142d9eSAlan Cox static void moea_enter_locked(pmap_t, vm_offset_t, vm_page_t, 269ce142d9eSAlan Cox vm_prot_t, boolean_t); 27059276937SPeter Grehan static void moea_syncicache(vm_offset_t, vm_size_t); 27159276937SPeter Grehan static boolean_t moea_query_bit(vm_page_t, int); 272ce186587SAlan Cox static u_int moea_clear_bit(vm_page_t, int); 27359276937SPeter Grehan static void moea_kremove(mmu_t, vm_offset_t); 27459276937SPeter Grehan int moea_pte_spill(vm_offset_t); 27559276937SPeter Grehan 27659276937SPeter Grehan /* 27759276937SPeter Grehan * Kernel MMU interface 27859276937SPeter Grehan */ 27959276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t); 28059276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t); 28159276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t); 28259276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t); 283e8a4a618SKonstantin Belousov void moea_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset, 284e8a4a618SKonstantin Belousov vm_page_t *mb, vm_offset_t b_offset, int xfersize); 28559276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t); 286ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t, 287ce142d9eSAlan Cox vm_prot_t); 2882053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t); 28959276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t); 29059276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t); 29159276937SPeter Grehan void moea_init(mmu_t); 29259276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t); 293e396eb60SAlan Cox boolean_t moea_is_prefaultable(mmu_t, pmap_t, vm_offset_t); 2947b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t); 2958d9e6d9fSAlan Cox int moea_ts_referenced(mmu_t, vm_page_t); 29620b79612SRafal Jaworowski vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_paddr_t, vm_paddr_t, int); 29759276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t); 29859677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t); 29959276937SPeter Grehan void moea_pinit(mmu_t, pmap_t); 30059276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t); 30159276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t); 30259276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int); 30359276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int); 30459276937SPeter Grehan void moea_release(mmu_t, pmap_t); 30559276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t); 30659276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t); 30778985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t); 30859276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t); 30959276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int); 31059276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t); 31159276937SPeter Grehan void moea_activate(mmu_t, struct thread *); 31259276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *); 3131c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int); 31459276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t); 31520b79612SRafal Jaworowski void *moea_mapdev(mmu_t, vm_paddr_t, vm_size_t); 316c1f4123bSNathan Whitehorn void *moea_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t); 31759276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t); 31820b79612SRafal Jaworowski vm_paddr_t moea_kextract(mmu_t, vm_offset_t); 319c1f4123bSNathan Whitehorn void moea_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t); 32020b79612SRafal Jaworowski void moea_kenter(mmu_t, vm_offset_t, vm_paddr_t); 321c1f4123bSNathan Whitehorn void moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma); 32220b79612SRafal Jaworowski boolean_t moea_dev_direct_mapped(mmu_t, vm_paddr_t, vm_size_t); 3231a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t); 324afd9cb6cSJustin Hibbits vm_offset_t moea_dumpsys_map(mmu_t mmu, struct pmap_md *md, vm_size_t ofs, 325afd9cb6cSJustin Hibbits vm_size_t *sz); 326afd9cb6cSJustin Hibbits struct pmap_md * moea_scan_md(mmu_t mmu, struct pmap_md *prev); 32759276937SPeter Grehan 32859276937SPeter Grehan static mmu_method_t moea_methods[] = { 32959276937SPeter Grehan MMUMETHOD(mmu_change_wiring, moea_change_wiring), 33059276937SPeter Grehan MMUMETHOD(mmu_clear_modify, moea_clear_modify), 33159276937SPeter Grehan MMUMETHOD(mmu_clear_reference, moea_clear_reference), 33259276937SPeter Grehan MMUMETHOD(mmu_copy_page, moea_copy_page), 333e8a4a618SKonstantin Belousov MMUMETHOD(mmu_copy_pages, moea_copy_pages), 33459276937SPeter Grehan MMUMETHOD(mmu_enter, moea_enter), 335ce142d9eSAlan Cox MMUMETHOD(mmu_enter_object, moea_enter_object), 33659276937SPeter Grehan MMUMETHOD(mmu_enter_quick, moea_enter_quick), 33759276937SPeter Grehan MMUMETHOD(mmu_extract, moea_extract), 33859276937SPeter Grehan MMUMETHOD(mmu_extract_and_hold, moea_extract_and_hold), 33959276937SPeter Grehan MMUMETHOD(mmu_init, moea_init), 34059276937SPeter Grehan MMUMETHOD(mmu_is_modified, moea_is_modified), 341e396eb60SAlan Cox MMUMETHOD(mmu_is_prefaultable, moea_is_prefaultable), 3427b85f591SAlan Cox MMUMETHOD(mmu_is_referenced, moea_is_referenced), 34359276937SPeter Grehan MMUMETHOD(mmu_ts_referenced, moea_ts_referenced), 34459276937SPeter Grehan MMUMETHOD(mmu_map, moea_map), 34559276937SPeter Grehan MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick), 34659677d3cSAlan Cox MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings), 34759276937SPeter Grehan MMUMETHOD(mmu_pinit, moea_pinit), 34859276937SPeter Grehan MMUMETHOD(mmu_pinit0, moea_pinit0), 34959276937SPeter Grehan MMUMETHOD(mmu_protect, moea_protect), 35059276937SPeter Grehan MMUMETHOD(mmu_qenter, moea_qenter), 35159276937SPeter Grehan MMUMETHOD(mmu_qremove, moea_qremove), 35259276937SPeter Grehan MMUMETHOD(mmu_release, moea_release), 35359276937SPeter Grehan MMUMETHOD(mmu_remove, moea_remove), 35459276937SPeter Grehan MMUMETHOD(mmu_remove_all, moea_remove_all), 35578985e42SAlan Cox MMUMETHOD(mmu_remove_write, moea_remove_write), 3561a4fcaebSMarcel Moolenaar MMUMETHOD(mmu_sync_icache, moea_sync_icache), 35759276937SPeter Grehan MMUMETHOD(mmu_zero_page, moea_zero_page), 35859276937SPeter Grehan MMUMETHOD(mmu_zero_page_area, moea_zero_page_area), 35959276937SPeter Grehan MMUMETHOD(mmu_zero_page_idle, moea_zero_page_idle), 36059276937SPeter Grehan MMUMETHOD(mmu_activate, moea_activate), 36159276937SPeter Grehan MMUMETHOD(mmu_deactivate, moea_deactivate), 362c1f4123bSNathan Whitehorn MMUMETHOD(mmu_page_set_memattr, moea_page_set_memattr), 36359276937SPeter Grehan 36459276937SPeter Grehan /* Internal interfaces */ 36559276937SPeter Grehan MMUMETHOD(mmu_bootstrap, moea_bootstrap), 3661c96bdd1SNathan Whitehorn MMUMETHOD(mmu_cpu_bootstrap, moea_cpu_bootstrap), 367c1f4123bSNathan Whitehorn MMUMETHOD(mmu_mapdev_attr, moea_mapdev_attr), 36859276937SPeter Grehan MMUMETHOD(mmu_mapdev, moea_mapdev), 36959276937SPeter Grehan MMUMETHOD(mmu_unmapdev, moea_unmapdev), 37059276937SPeter Grehan MMUMETHOD(mmu_kextract, moea_kextract), 37159276937SPeter Grehan MMUMETHOD(mmu_kenter, moea_kenter), 372c1f4123bSNathan Whitehorn MMUMETHOD(mmu_kenter_attr, moea_kenter_attr), 37359276937SPeter Grehan MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped), 374afd9cb6cSJustin Hibbits MMUMETHOD(mmu_scan_md, moea_scan_md), 375afd9cb6cSJustin Hibbits MMUMETHOD(mmu_dumpsys_map, moea_dumpsys_map), 37659276937SPeter Grehan 37759276937SPeter Grehan { 0, 0 } 37859276937SPeter Grehan }; 37959276937SPeter Grehan 38033529b98SPeter Grehan MMU_DEF(oea_mmu, MMU_TYPE_OEA, moea_methods, 0); 38133529b98SPeter Grehan 382c1f4123bSNathan Whitehorn static __inline uint32_t 383c1f4123bSNathan Whitehorn moea_calc_wimg(vm_offset_t pa, vm_memattr_t ma) 384c1f4123bSNathan Whitehorn { 385c1f4123bSNathan Whitehorn uint32_t pte_lo; 386c1f4123bSNathan Whitehorn int i; 387c1f4123bSNathan Whitehorn 388c1f4123bSNathan Whitehorn if (ma != VM_MEMATTR_DEFAULT) { 389c1f4123bSNathan Whitehorn switch (ma) { 390c1f4123bSNathan Whitehorn case VM_MEMATTR_UNCACHEABLE: 391c1f4123bSNathan Whitehorn return (PTE_I | PTE_G); 392c1f4123bSNathan Whitehorn case VM_MEMATTR_WRITE_COMBINING: 393c1f4123bSNathan Whitehorn case VM_MEMATTR_WRITE_BACK: 394c1f4123bSNathan Whitehorn case VM_MEMATTR_PREFETCHABLE: 395c1f4123bSNathan Whitehorn return (PTE_I); 396c1f4123bSNathan Whitehorn case VM_MEMATTR_WRITE_THROUGH: 397c1f4123bSNathan Whitehorn return (PTE_W | PTE_M); 398c1f4123bSNathan Whitehorn } 399c1f4123bSNathan Whitehorn } 400c1f4123bSNathan Whitehorn 401c1f4123bSNathan Whitehorn /* 402c1f4123bSNathan Whitehorn * Assume the page is cache inhibited and access is guarded unless 403c1f4123bSNathan Whitehorn * it's in our available memory array. 404c1f4123bSNathan Whitehorn */ 405c1f4123bSNathan Whitehorn pte_lo = PTE_I | PTE_G; 406c1f4123bSNathan Whitehorn for (i = 0; i < pregions_sz; i++) { 407c1f4123bSNathan Whitehorn if ((pa >= pregions[i].mr_start) && 408c1f4123bSNathan Whitehorn (pa < (pregions[i].mr_start + pregions[i].mr_size))) { 409c1f4123bSNathan Whitehorn pte_lo = PTE_M; 410c1f4123bSNathan Whitehorn break; 411c1f4123bSNathan Whitehorn } 412c1f4123bSNathan Whitehorn } 413c1f4123bSNathan Whitehorn 414c1f4123bSNathan Whitehorn return pte_lo; 415c1f4123bSNathan Whitehorn } 41659276937SPeter Grehan 417e4f72b32SMarcel Moolenaar static void 418e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va) 419e4f72b32SMarcel Moolenaar { 420e4f72b32SMarcel Moolenaar 421e4f72b32SMarcel Moolenaar mtx_lock_spin(&tlbie_mtx); 42294363f53SNathan Whitehorn __asm __volatile("ptesync"); 423e4f72b32SMarcel Moolenaar __asm __volatile("tlbie %0" :: "r"(va)); 42494363f53SNathan Whitehorn __asm __volatile("eieio; tlbsync; ptesync"); 425e4f72b32SMarcel Moolenaar mtx_unlock_spin(&tlbie_mtx); 426e4f72b32SMarcel Moolenaar } 427e4f72b32SMarcel Moolenaar 428e4f72b32SMarcel Moolenaar static void 429e4f72b32SMarcel Moolenaar tlbia(void) 430e4f72b32SMarcel Moolenaar { 431e4f72b32SMarcel Moolenaar vm_offset_t va; 432e4f72b32SMarcel Moolenaar 433e4f72b32SMarcel Moolenaar for (va = 0; va < 0x00040000; va += 0x00001000) { 434e4f72b32SMarcel Moolenaar __asm __volatile("tlbie %0" :: "r"(va)); 435e4f72b32SMarcel Moolenaar powerpc_sync(); 436e4f72b32SMarcel Moolenaar } 437e4f72b32SMarcel Moolenaar __asm __volatile("tlbsync"); 438e4f72b32SMarcel Moolenaar powerpc_sync(); 439e4f72b32SMarcel Moolenaar } 4405244eac9SBenno Rice 4415244eac9SBenno Rice static __inline int 4425244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va) 4435244eac9SBenno Rice { 4445244eac9SBenno Rice return (sr[(uintptr_t)va >> ADDR_SR_SHFT]); 4455244eac9SBenno Rice } 4465244eac9SBenno Rice 4475244eac9SBenno Rice static __inline u_int 4485244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr) 4495244eac9SBenno Rice { 4505244eac9SBenno Rice u_int hash; 4515244eac9SBenno Rice 4525244eac9SBenno Rice hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >> 4535244eac9SBenno Rice ADDR_PIDX_SHFT); 45459276937SPeter Grehan return (hash & moea_pteg_mask); 4555244eac9SBenno Rice } 4565244eac9SBenno Rice 4575244eac9SBenno Rice static __inline struct pvo_head * 4585244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m) 459f9bac91bSBenno Rice { 460f9bac91bSBenno Rice 4615244eac9SBenno Rice return (&m->md.mdpg_pvoh); 462f9bac91bSBenno Rice } 463f9bac91bSBenno Rice 464f9bac91bSBenno Rice static __inline void 46559276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit) 466f9bac91bSBenno Rice { 467f9bac91bSBenno Rice 4683653f5cbSAlan Cox rw_assert(&pvh_global_lock, RA_WLOCKED); 4695244eac9SBenno Rice m->md.mdpg_attrs &= ~ptebit; 4705244eac9SBenno Rice } 4715244eac9SBenno Rice 4725244eac9SBenno Rice static __inline int 47359276937SPeter Grehan moea_attr_fetch(vm_page_t m) 4745244eac9SBenno Rice { 4755244eac9SBenno Rice 4765244eac9SBenno Rice return (m->md.mdpg_attrs); 477f9bac91bSBenno Rice } 478f9bac91bSBenno Rice 479f9bac91bSBenno Rice static __inline void 48059276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit) 481f9bac91bSBenno Rice { 482f9bac91bSBenno Rice 4833653f5cbSAlan Cox rw_assert(&pvh_global_lock, RA_WLOCKED); 4845244eac9SBenno Rice m->md.mdpg_attrs |= ptebit; 485f9bac91bSBenno Rice } 486f9bac91bSBenno Rice 487f9bac91bSBenno Rice static __inline int 48859276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt) 489f9bac91bSBenno Rice { 4905244eac9SBenno Rice if (pt->pte_hi == pvo_pt->pte_hi) 4915244eac9SBenno Rice return (1); 492f9bac91bSBenno Rice 4935244eac9SBenno Rice return (0); 494f9bac91bSBenno Rice } 495f9bac91bSBenno Rice 496f9bac91bSBenno Rice static __inline int 49759276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which) 498f9bac91bSBenno Rice { 4995244eac9SBenno Rice return (pt->pte_hi & ~PTE_VALID) == 5005244eac9SBenno Rice (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) | 5015244eac9SBenno Rice ((va >> ADDR_API_SHFT) & PTE_API) | which); 502f9bac91bSBenno Rice } 503f9bac91bSBenno Rice 5045244eac9SBenno Rice static __inline void 50559276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo) 506f9bac91bSBenno Rice { 507d644a0b7SAlan Cox 508d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 509d644a0b7SAlan Cox 510f9bac91bSBenno Rice /* 5115244eac9SBenno Rice * Construct a PTE. Default to IMB initially. Valid bit only gets 5125244eac9SBenno Rice * set when the real pte is set in memory. 513f9bac91bSBenno Rice * 514f9bac91bSBenno Rice * Note: Don't set the valid bit for correct operation of tlb update. 515f9bac91bSBenno Rice */ 5165244eac9SBenno Rice pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) | 5175244eac9SBenno Rice (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API); 5185244eac9SBenno Rice pt->pte_lo = pte_lo; 519f9bac91bSBenno Rice } 520f9bac91bSBenno Rice 5215244eac9SBenno Rice static __inline void 52259276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt) 523f9bac91bSBenno Rice { 524f9bac91bSBenno Rice 525d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 5265244eac9SBenno Rice pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG); 527f9bac91bSBenno Rice } 528f9bac91bSBenno Rice 5295244eac9SBenno Rice static __inline void 53059276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit) 531f9bac91bSBenno Rice { 5325244eac9SBenno Rice 533d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 534d644a0b7SAlan Cox 5355244eac9SBenno Rice /* 5365244eac9SBenno Rice * As shown in Section 7.6.3.2.3 5375244eac9SBenno Rice */ 5385244eac9SBenno Rice pt->pte_lo &= ~ptebit; 539e4f72b32SMarcel Moolenaar tlbie(va); 5405244eac9SBenno Rice } 5415244eac9SBenno Rice 5425244eac9SBenno Rice static __inline void 54359276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt) 5445244eac9SBenno Rice { 5455244eac9SBenno Rice 546d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 5475244eac9SBenno Rice pvo_pt->pte_hi |= PTE_VALID; 5485244eac9SBenno Rice 5495244eac9SBenno Rice /* 5505244eac9SBenno Rice * Update the PTE as defined in section 7.6.3.1. 5515244eac9SBenno Rice * Note that the REF/CHG bits are from pvo_pt and thus should havce 5525244eac9SBenno Rice * been saved so this routine can restore them (if desired). 5535244eac9SBenno Rice */ 5545244eac9SBenno Rice pt->pte_lo = pvo_pt->pte_lo; 555e4f72b32SMarcel Moolenaar powerpc_sync(); 5565244eac9SBenno Rice pt->pte_hi = pvo_pt->pte_hi; 557e4f72b32SMarcel Moolenaar powerpc_sync(); 55859276937SPeter Grehan moea_pte_valid++; 5595244eac9SBenno Rice } 5605244eac9SBenno Rice 5615244eac9SBenno Rice static __inline void 56259276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va) 5635244eac9SBenno Rice { 5645244eac9SBenno Rice 565d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 5665244eac9SBenno Rice pvo_pt->pte_hi &= ~PTE_VALID; 5675244eac9SBenno Rice 5685244eac9SBenno Rice /* 5695244eac9SBenno Rice * Force the reg & chg bits back into the PTEs. 5705244eac9SBenno Rice */ 571e4f72b32SMarcel Moolenaar powerpc_sync(); 5725244eac9SBenno Rice 5735244eac9SBenno Rice /* 5745244eac9SBenno Rice * Invalidate the pte. 5755244eac9SBenno Rice */ 5765244eac9SBenno Rice pt->pte_hi &= ~PTE_VALID; 5775244eac9SBenno Rice 578e4f72b32SMarcel Moolenaar tlbie(va); 5795244eac9SBenno Rice 5805244eac9SBenno Rice /* 5815244eac9SBenno Rice * Save the reg & chg bits. 5825244eac9SBenno Rice */ 58359276937SPeter Grehan moea_pte_synch(pt, pvo_pt); 58459276937SPeter Grehan moea_pte_valid--; 5855244eac9SBenno Rice } 5865244eac9SBenno Rice 5875244eac9SBenno Rice static __inline void 58859276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va) 5895244eac9SBenno Rice { 5905244eac9SBenno Rice 5915244eac9SBenno Rice /* 5925244eac9SBenno Rice * Invalidate the PTE 5935244eac9SBenno Rice */ 59459276937SPeter Grehan moea_pte_unset(pt, pvo_pt, va); 59559276937SPeter Grehan moea_pte_set(pt, pvo_pt); 596f9bac91bSBenno Rice } 597f9bac91bSBenno Rice 598f9bac91bSBenno Rice /* 5995244eac9SBenno Rice * Quick sort callout for comparing memory regions. 600f9bac91bSBenno Rice */ 6015244eac9SBenno Rice static int om_cmp(const void *a, const void *b); 6025244eac9SBenno Rice 6035244eac9SBenno Rice static int 6045244eac9SBenno Rice om_cmp(const void *a, const void *b) 6055244eac9SBenno Rice { 6065244eac9SBenno Rice const struct ofw_map *mapa; 6075244eac9SBenno Rice const struct ofw_map *mapb; 6085244eac9SBenno Rice 6095244eac9SBenno Rice mapa = a; 6105244eac9SBenno Rice mapb = b; 6115244eac9SBenno Rice if (mapa->om_pa < mapb->om_pa) 6125244eac9SBenno Rice return (-1); 6135244eac9SBenno Rice else if (mapa->om_pa > mapb->om_pa) 6145244eac9SBenno Rice return (1); 6155244eac9SBenno Rice else 6165244eac9SBenno Rice return (0); 617f9bac91bSBenno Rice } 618f9bac91bSBenno Rice 619f9bac91bSBenno Rice void 6201c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap) 62112640815SMarcel Moolenaar { 62212640815SMarcel Moolenaar u_int sdr; 62312640815SMarcel Moolenaar int i; 62412640815SMarcel Moolenaar 62512640815SMarcel Moolenaar if (ap) { 626e4f72b32SMarcel Moolenaar powerpc_sync(); 62712640815SMarcel Moolenaar __asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu)); 62812640815SMarcel Moolenaar __asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl)); 62912640815SMarcel Moolenaar isync(); 63012640815SMarcel Moolenaar __asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu)); 63112640815SMarcel Moolenaar __asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl)); 63212640815SMarcel Moolenaar isync(); 63312640815SMarcel Moolenaar } 63412640815SMarcel Moolenaar 635aef8ef51SAdrian Chadd #ifdef WII 636aef8ef51SAdrian Chadd /* 637aef8ef51SAdrian Chadd * Special case for the Wii: don't install the PCI BAT. 638aef8ef51SAdrian Chadd */ 639aef8ef51SAdrian Chadd if (strcmp(installed_platform(), "wii") != 0) { 640aef8ef51SAdrian Chadd #endif 64101d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu)); 64201d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl)); 643aef8ef51SAdrian Chadd #ifdef WII 644aef8ef51SAdrian Chadd } 645aef8ef51SAdrian Chadd #endif 64612640815SMarcel Moolenaar isync(); 64712640815SMarcel Moolenaar 64801d8aa0dSMarcel Moolenaar __asm __volatile("mtibatu 1,%0" :: "r"(0)); 64901d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatu 2,%0" :: "r"(0)); 65001d8aa0dSMarcel Moolenaar __asm __volatile("mtibatu 2,%0" :: "r"(0)); 65101d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatu 3,%0" :: "r"(0)); 65201d8aa0dSMarcel Moolenaar __asm __volatile("mtibatu 3,%0" :: "r"(0)); 65312640815SMarcel Moolenaar isync(); 65412640815SMarcel Moolenaar 65512640815SMarcel Moolenaar for (i = 0; i < 16; i++) 656fe3b4685SNathan Whitehorn mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]); 657e4f72b32SMarcel Moolenaar powerpc_sync(); 65812640815SMarcel Moolenaar 65912640815SMarcel Moolenaar sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10); 66012640815SMarcel Moolenaar __asm __volatile("mtsdr1 %0" :: "r"(sdr)); 66112640815SMarcel Moolenaar isync(); 66212640815SMarcel Moolenaar 66386c1fb4cSMarcel Moolenaar tlbia(); 66412640815SMarcel Moolenaar } 66512640815SMarcel Moolenaar 66612640815SMarcel Moolenaar void 66759276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend) 668f9bac91bSBenno Rice { 66931c82d03SBenno Rice ihandle_t mmui; 6705244eac9SBenno Rice phandle_t chosen, mmu; 6715244eac9SBenno Rice int sz; 6725244eac9SBenno Rice int i, j; 673e2f6d6e2SPeter Grehan vm_size_t size, physsz, hwphyssz; 6745244eac9SBenno Rice vm_offset_t pa, va, off; 67550c202c5SJeff Roberson void *dpcpu; 676976cc697SNathan Whitehorn register_t msr; 677f9bac91bSBenno Rice 678f9bac91bSBenno Rice /* 67932bc7846SPeter Grehan * Set up BAT0 to map the lowest 256 MB area 6800d290675SBenno Rice */ 6810d290675SBenno Rice battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW); 6820d290675SBenno Rice battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs); 6830d290675SBenno Rice 6840d290675SBenno Rice /* 6850d290675SBenno Rice * Map PCI memory space. 6860d290675SBenno Rice */ 6870d290675SBenno Rice battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW); 6880d290675SBenno Rice battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs); 6890d290675SBenno Rice 6900d290675SBenno Rice battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW); 6910d290675SBenno Rice battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs); 6920d290675SBenno Rice 6930d290675SBenno Rice battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW); 6940d290675SBenno Rice battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs); 6950d290675SBenno Rice 6960d290675SBenno Rice battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW); 6970d290675SBenno Rice battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs); 6980d290675SBenno Rice 6990d290675SBenno Rice /* 7000d290675SBenno Rice * Map obio devices. 7010d290675SBenno Rice */ 7020d290675SBenno Rice battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW); 7030d290675SBenno Rice battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs); 7040d290675SBenno Rice 7050d290675SBenno Rice /* 7065244eac9SBenno Rice * Use an IBAT and a DBAT to map the bottom segment of memory 707976cc697SNathan Whitehorn * where we are. Turn off instruction relocation temporarily 708976cc697SNathan Whitehorn * to prevent faults while reprogramming the IBAT. 709f9bac91bSBenno Rice */ 710976cc697SNathan Whitehorn msr = mfmsr(); 711976cc697SNathan Whitehorn mtmsr(msr & ~PSL_IR); 71259276937SPeter Grehan __asm (".balign 32; \n" 71372ed3108SPeter Grehan "mtibatu 0,%0; mtibatl 0,%1; isync; \n" 7145d64cf91SPeter Grehan "mtdbatu 0,%0; mtdbatl 0,%1; isync" 71512640815SMarcel Moolenaar :: "r"(battable[0].batu), "r"(battable[0].batl)); 716976cc697SNathan Whitehorn mtmsr(msr); 7170d290675SBenno Rice 718aef8ef51SAdrian Chadd #ifdef WII 719aef8ef51SAdrian Chadd if (strcmp(installed_platform(), "wii") != 0) { 720aef8ef51SAdrian Chadd #endif 7210d290675SBenno Rice /* map pci space */ 72212640815SMarcel Moolenaar __asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu)); 72312640815SMarcel Moolenaar __asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl)); 724aef8ef51SAdrian Chadd #ifdef WII 725aef8ef51SAdrian Chadd } 726aef8ef51SAdrian Chadd #endif 72712640815SMarcel Moolenaar isync(); 728f9bac91bSBenno Rice 7291c96bdd1SNathan Whitehorn /* set global direct map flag */ 7301c96bdd1SNathan Whitehorn hw_direct_map = 1; 7311c96bdd1SNathan Whitehorn 73231c82d03SBenno Rice mem_regions(&pregions, &pregions_sz, ®ions, ®ions_sz); 73359276937SPeter Grehan CTR0(KTR_PMAP, "moea_bootstrap: physical memory"); 73431c82d03SBenno Rice 73531c82d03SBenno Rice for (i = 0; i < pregions_sz; i++) { 73632bc7846SPeter Grehan vm_offset_t pa; 73732bc7846SPeter Grehan vm_offset_t end; 73832bc7846SPeter Grehan 73931c82d03SBenno Rice CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)", 74031c82d03SBenno Rice pregions[i].mr_start, 74131c82d03SBenno Rice pregions[i].mr_start + pregions[i].mr_size, 74231c82d03SBenno Rice pregions[i].mr_size); 74332bc7846SPeter Grehan /* 74432bc7846SPeter Grehan * Install entries into the BAT table to allow all 74532bc7846SPeter Grehan * of physmem to be convered by on-demand BAT entries. 74632bc7846SPeter Grehan * The loop will sometimes set the same battable element 74732bc7846SPeter Grehan * twice, but that's fine since they won't be used for 74832bc7846SPeter Grehan * a while yet. 74932bc7846SPeter Grehan */ 75032bc7846SPeter Grehan pa = pregions[i].mr_start & 0xf0000000; 75132bc7846SPeter Grehan end = pregions[i].mr_start + pregions[i].mr_size; 75232bc7846SPeter Grehan do { 75332bc7846SPeter Grehan u_int n = pa >> ADDR_SR_SHFT; 75432bc7846SPeter Grehan 75532bc7846SPeter Grehan battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW); 75632bc7846SPeter Grehan battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs); 75732bc7846SPeter Grehan pa += SEGMENT_LENGTH; 75832bc7846SPeter Grehan } while (pa < end); 75931c82d03SBenno Rice } 76031c82d03SBenno Rice 76131c82d03SBenno Rice if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz) 76259276937SPeter Grehan panic("moea_bootstrap: phys_avail too small"); 76397f7cde4SNathan Whitehorn 7645244eac9SBenno Rice phys_avail_count = 0; 765d2c1f576SBenno Rice physsz = 0; 766b0c21309SPeter Grehan hwphyssz = 0; 767b0c21309SPeter Grehan TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz); 76831c82d03SBenno Rice for (i = 0, j = 0; i < regions_sz; i++, j += 2) { 7695244eac9SBenno Rice CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start, 7705244eac9SBenno Rice regions[i].mr_start + regions[i].mr_size, 7715244eac9SBenno Rice regions[i].mr_size); 772e2f6d6e2SPeter Grehan if (hwphyssz != 0 && 773e2f6d6e2SPeter Grehan (physsz + regions[i].mr_size) >= hwphyssz) { 774e2f6d6e2SPeter Grehan if (physsz < hwphyssz) { 775e2f6d6e2SPeter Grehan phys_avail[j] = regions[i].mr_start; 776e2f6d6e2SPeter Grehan phys_avail[j + 1] = regions[i].mr_start + 777e2f6d6e2SPeter Grehan hwphyssz - physsz; 778e2f6d6e2SPeter Grehan physsz = hwphyssz; 779e2f6d6e2SPeter Grehan phys_avail_count++; 780e2f6d6e2SPeter Grehan } 781e2f6d6e2SPeter Grehan break; 782e2f6d6e2SPeter Grehan } 7835244eac9SBenno Rice phys_avail[j] = regions[i].mr_start; 7845244eac9SBenno Rice phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size; 7855244eac9SBenno Rice phys_avail_count++; 786d2c1f576SBenno Rice physsz += regions[i].mr_size; 787f9bac91bSBenno Rice } 788e347e23bSNathan Whitehorn 789e347e23bSNathan Whitehorn /* Check for overlap with the kernel and exception vectors */ 790e347e23bSNathan Whitehorn for (j = 0; j < 2*phys_avail_count; j+=2) { 791e347e23bSNathan Whitehorn if (phys_avail[j] < EXC_LAST) 792e347e23bSNathan Whitehorn phys_avail[j] += EXC_LAST; 793e347e23bSNathan Whitehorn 794e347e23bSNathan Whitehorn if (kernelstart >= phys_avail[j] && 795e347e23bSNathan Whitehorn kernelstart < phys_avail[j+1]) { 796e347e23bSNathan Whitehorn if (kernelend < phys_avail[j+1]) { 797e347e23bSNathan Whitehorn phys_avail[2*phys_avail_count] = 798e347e23bSNathan Whitehorn (kernelend & ~PAGE_MASK) + PAGE_SIZE; 799e347e23bSNathan Whitehorn phys_avail[2*phys_avail_count + 1] = 800e347e23bSNathan Whitehorn phys_avail[j+1]; 801e347e23bSNathan Whitehorn phys_avail_count++; 802e347e23bSNathan Whitehorn } 803e347e23bSNathan Whitehorn 804e347e23bSNathan Whitehorn phys_avail[j+1] = kernelstart & ~PAGE_MASK; 805e347e23bSNathan Whitehorn } 806e347e23bSNathan Whitehorn 807e347e23bSNathan Whitehorn if (kernelend >= phys_avail[j] && 808e347e23bSNathan Whitehorn kernelend < phys_avail[j+1]) { 809e347e23bSNathan Whitehorn if (kernelstart > phys_avail[j]) { 810e347e23bSNathan Whitehorn phys_avail[2*phys_avail_count] = phys_avail[j]; 811e347e23bSNathan Whitehorn phys_avail[2*phys_avail_count + 1] = 812e347e23bSNathan Whitehorn kernelstart & ~PAGE_MASK; 813e347e23bSNathan Whitehorn phys_avail_count++; 814e347e23bSNathan Whitehorn } 815e347e23bSNathan Whitehorn 816e347e23bSNathan Whitehorn phys_avail[j] = (kernelend & ~PAGE_MASK) + PAGE_SIZE; 817e347e23bSNathan Whitehorn } 818e347e23bSNathan Whitehorn } 819e347e23bSNathan Whitehorn 820d2c1f576SBenno Rice physmem = btoc(physsz); 821f9bac91bSBenno Rice 822f9bac91bSBenno Rice /* 8235244eac9SBenno Rice * Allocate PTEG table. 824f9bac91bSBenno Rice */ 8255244eac9SBenno Rice #ifdef PTEGCOUNT 82659276937SPeter Grehan moea_pteg_count = PTEGCOUNT; 8275244eac9SBenno Rice #else 82859276937SPeter Grehan moea_pteg_count = 0x1000; 829f9bac91bSBenno Rice 83059276937SPeter Grehan while (moea_pteg_count < physmem) 83159276937SPeter Grehan moea_pteg_count <<= 1; 832f9bac91bSBenno Rice 83359276937SPeter Grehan moea_pteg_count >>= 1; 8345244eac9SBenno Rice #endif /* PTEGCOUNT */ 835f9bac91bSBenno Rice 83659276937SPeter Grehan size = moea_pteg_count * sizeof(struct pteg); 83759276937SPeter Grehan CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count, 8385244eac9SBenno Rice size); 83959276937SPeter Grehan moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size); 84059276937SPeter Grehan CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table); 84159276937SPeter Grehan bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg)); 84259276937SPeter Grehan moea_pteg_mask = moea_pteg_count - 1; 843f9bac91bSBenno Rice 8445244eac9SBenno Rice /* 845864bc520SBenno Rice * Allocate pv/overflow lists. 8465244eac9SBenno Rice */ 84759276937SPeter Grehan size = sizeof(struct pvo_head) * moea_pteg_count; 84859276937SPeter Grehan moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size, 8495244eac9SBenno Rice PAGE_SIZE); 85059276937SPeter Grehan CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table); 85159276937SPeter Grehan for (i = 0; i < moea_pteg_count; i++) 85259276937SPeter Grehan LIST_INIT(&moea_pvo_table[i]); 8535244eac9SBenno Rice 8545244eac9SBenno Rice /* 855f489bf21SAlan Cox * Initialize the lock that synchronizes access to the pteg and pvo 856f489bf21SAlan Cox * tables. 857f489bf21SAlan Cox */ 858d644a0b7SAlan Cox mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF | 859d644a0b7SAlan Cox MTX_RECURSE); 860e9b5f218SNathan Whitehorn mtx_init(&moea_vsid_mutex, "VSID table", NULL, MTX_DEF); 861f489bf21SAlan Cox 862e4f72b32SMarcel Moolenaar mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN); 863e4f72b32SMarcel Moolenaar 864f489bf21SAlan Cox /* 8655244eac9SBenno Rice * Initialise the unmanaged pvo pool. 8665244eac9SBenno Rice */ 86759276937SPeter Grehan moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc( 8680d290675SBenno Rice BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0); 86959276937SPeter Grehan moea_bpvo_pool_index = 0; 8705244eac9SBenno Rice 8715244eac9SBenno Rice /* 8725244eac9SBenno Rice * Make sure kernel vsid is allocated as well as VSID 0. 8735244eac9SBenno Rice */ 87459276937SPeter Grehan moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW] 8755244eac9SBenno Rice |= 1 << (KERNEL_VSIDBITS % VSID_NBPW); 87659276937SPeter Grehan moea_vsid_bitmap[0] |= 1; 8775244eac9SBenno Rice 8785244eac9SBenno Rice /* 879fe3b4685SNathan Whitehorn * Initialize the kernel pmap (which is statically allocated). 8805244eac9SBenno Rice */ 881fe3b4685SNathan Whitehorn PMAP_LOCK_INIT(kernel_pmap); 882fe3b4685SNathan Whitehorn for (i = 0; i < 16; i++) 883fe3b4685SNathan Whitehorn kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i; 884c47dd3dbSAttilio Rao CPU_FILL(&kernel_pmap->pm_active); 885ccc4a5c7SNathan Whitehorn RB_INIT(&kernel_pmap->pmap_pvo); 886fe3b4685SNathan Whitehorn 887fe3b4685SNathan Whitehorn /* 8883653f5cbSAlan Cox * Initialize the global pv list lock. 8893653f5cbSAlan Cox */ 8903653f5cbSAlan Cox rw_init(&pvh_global_lock, "pmap pv global"); 8913653f5cbSAlan Cox 8923653f5cbSAlan Cox /* 893fe3b4685SNathan Whitehorn * Set up the Open Firmware mappings 894fe3b4685SNathan Whitehorn */ 895e347e23bSNathan Whitehorn chosen = OF_finddevice("/chosen"); 896e347e23bSNathan Whitehorn if (chosen != -1 && OF_getprop(chosen, "mmu", &mmui, 4) != -1 && 897e347e23bSNathan Whitehorn (mmu = OF_instance_to_package(mmui)) != -1 && 898e347e23bSNathan Whitehorn (sz = OF_getproplen(mmu, "translations")) != -1) { 899aa39961eSBenno Rice translations = NULL; 9006cc1cdf4SPeter Grehan for (i = 0; phys_avail[i] != 0; i += 2) { 9016cc1cdf4SPeter Grehan if (phys_avail[i + 1] >= sz) { 902aa39961eSBenno Rice translations = (struct ofw_map *)phys_avail[i]; 9036cc1cdf4SPeter Grehan break; 9046cc1cdf4SPeter Grehan } 905aa39961eSBenno Rice } 906aa39961eSBenno Rice if (translations == NULL) 90759276937SPeter Grehan panic("moea_bootstrap: no space to copy translations"); 9085244eac9SBenno Rice bzero(translations, sz); 9095244eac9SBenno Rice if (OF_getprop(mmu, "translations", translations, sz) == -1) 91059276937SPeter Grehan panic("moea_bootstrap: can't get ofw translations"); 91159276937SPeter Grehan CTR0(KTR_PMAP, "moea_bootstrap: translations"); 91231c82d03SBenno Rice sz /= sizeof(*translations); 9135244eac9SBenno Rice qsort(translations, sz, sizeof (*translations), om_cmp); 914ed1e1e2aSNathan Whitehorn for (i = 0; i < sz; i++) { 9155244eac9SBenno Rice CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x", 9165244eac9SBenno Rice translations[i].om_pa, translations[i].om_va, 9175244eac9SBenno Rice translations[i].om_len); 9185244eac9SBenno Rice 91932bc7846SPeter Grehan /* 920e347e23bSNathan Whitehorn * If the mapping is 1:1, let the RAM and device 921e347e23bSNathan Whitehorn * on-demand BAT tables take care of the translation. 92232bc7846SPeter Grehan */ 92332bc7846SPeter Grehan if (translations[i].om_va == translations[i].om_pa) 92432bc7846SPeter Grehan continue; 9255244eac9SBenno Rice 92632bc7846SPeter Grehan /* Enter the pages */ 927e347e23bSNathan Whitehorn for (off = 0; off < translations[i].om_len; 928e347e23bSNathan Whitehorn off += PAGE_SIZE) 929fe3b4685SNathan Whitehorn moea_kenter(mmup, translations[i].om_va + off, 930fe3b4685SNathan Whitehorn translations[i].om_pa + off); 931f9bac91bSBenno Rice } 932e347e23bSNathan Whitehorn } 933014ffa99SMarcel Moolenaar 934014ffa99SMarcel Moolenaar /* 935014ffa99SMarcel Moolenaar * Calculate the last available physical address. 936014ffa99SMarcel Moolenaar */ 937014ffa99SMarcel Moolenaar for (i = 0; phys_avail[i + 2] != 0; i += 2) 938014ffa99SMarcel Moolenaar ; 939014ffa99SMarcel Moolenaar Maxmem = powerpc_btop(phys_avail[i + 1]); 9405244eac9SBenno Rice 9411c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmup,0); 9425244eac9SBenno Rice 9435244eac9SBenno Rice pmap_bootstrapped++; 944014ffa99SMarcel Moolenaar 945014ffa99SMarcel Moolenaar /* 946014ffa99SMarcel Moolenaar * Set the start and end of kva. 947014ffa99SMarcel Moolenaar */ 948014ffa99SMarcel Moolenaar virtual_avail = VM_MIN_KERNEL_ADDRESS; 949ab739706SNathan Whitehorn virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS; 950014ffa99SMarcel Moolenaar 951014ffa99SMarcel Moolenaar /* 952014ffa99SMarcel Moolenaar * Allocate a kernel stack with a guard page for thread0 and map it 953014ffa99SMarcel Moolenaar * into the kernel page map. 954014ffa99SMarcel Moolenaar */ 955014ffa99SMarcel Moolenaar pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE); 956014ffa99SMarcel Moolenaar va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE; 957014ffa99SMarcel Moolenaar virtual_avail = va + KSTACK_PAGES * PAGE_SIZE; 958014ffa99SMarcel Moolenaar CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va); 959014ffa99SMarcel Moolenaar thread0.td_kstack = va; 960014ffa99SMarcel Moolenaar thread0.td_kstack_pages = KSTACK_PAGES; 961014ffa99SMarcel Moolenaar for (i = 0; i < KSTACK_PAGES; i++) { 962c2ede4b3SMartin Blapp moea_kenter(mmup, va, pa); 963014ffa99SMarcel Moolenaar pa += PAGE_SIZE; 964014ffa99SMarcel Moolenaar va += PAGE_SIZE; 965014ffa99SMarcel Moolenaar } 966014ffa99SMarcel Moolenaar 967014ffa99SMarcel Moolenaar /* 968014ffa99SMarcel Moolenaar * Allocate virtual address space for the message buffer. 969014ffa99SMarcel Moolenaar */ 9704053b05bSSergey Kandaurov pa = msgbuf_phys = moea_bootstrap_alloc(msgbufsize, PAGE_SIZE); 971014ffa99SMarcel Moolenaar msgbufp = (struct msgbuf *)virtual_avail; 972014ffa99SMarcel Moolenaar va = virtual_avail; 9734053b05bSSergey Kandaurov virtual_avail += round_page(msgbufsize); 974014ffa99SMarcel Moolenaar while (va < virtual_avail) { 975c2ede4b3SMartin Blapp moea_kenter(mmup, va, pa); 976014ffa99SMarcel Moolenaar pa += PAGE_SIZE; 977014ffa99SMarcel Moolenaar va += PAGE_SIZE; 978014ffa99SMarcel Moolenaar } 97950c202c5SJeff Roberson 98050c202c5SJeff Roberson /* 98150c202c5SJeff Roberson * Allocate virtual address space for the dynamic percpu area. 98250c202c5SJeff Roberson */ 98350c202c5SJeff Roberson pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE); 98450c202c5SJeff Roberson dpcpu = (void *)virtual_avail; 98550c202c5SJeff Roberson va = virtual_avail; 98650c202c5SJeff Roberson virtual_avail += DPCPU_SIZE; 98750c202c5SJeff Roberson while (va < virtual_avail) { 988c2ede4b3SMartin Blapp moea_kenter(mmup, va, pa); 98950c202c5SJeff Roberson pa += PAGE_SIZE; 99050c202c5SJeff Roberson va += PAGE_SIZE; 99150c202c5SJeff Roberson } 99250c202c5SJeff Roberson dpcpu_init(dpcpu, 0); 9935244eac9SBenno Rice } 9945244eac9SBenno Rice 9955244eac9SBenno Rice /* 9965244eac9SBenno Rice * Activate a user pmap. The pmap must be activated before it's address 9975244eac9SBenno Rice * space can be accessed in any way. 998f9bac91bSBenno Rice */ 999f9bac91bSBenno Rice void 100059276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td) 1001f9bac91bSBenno Rice { 10028207b362SBenno Rice pmap_t pm, pmr; 1003f9bac91bSBenno Rice 1004f9bac91bSBenno Rice /* 100532bc7846SPeter Grehan * Load all the data we need up front to encourage the compiler to 10065244eac9SBenno Rice * not issue any loads while we have interrupts disabled below. 1007f9bac91bSBenno Rice */ 10085244eac9SBenno Rice pm = &td->td_proc->p_vmspace->vm_pmap; 100952a7870dSNathan Whitehorn pmr = pm->pmap_phys; 10108207b362SBenno Rice 1011c7c2767eSAttilio Rao CPU_SET(PCPU_GET(cpuid), &pm->pm_active); 10128207b362SBenno Rice PCPU_SET(curpmap, pmr); 1013ac6ba8bdSBenno Rice } 1014ac6ba8bdSBenno Rice 1015ac6ba8bdSBenno Rice void 101659276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td) 1017ac6ba8bdSBenno Rice { 1018ac6ba8bdSBenno Rice pmap_t pm; 1019ac6ba8bdSBenno Rice 1020ac6ba8bdSBenno Rice pm = &td->td_proc->p_vmspace->vm_pmap; 1021c7c2767eSAttilio Rao CPU_CLR(PCPU_GET(cpuid), &pm->pm_active); 10228207b362SBenno Rice PCPU_SET(curpmap, NULL); 1023f9bac91bSBenno Rice } 1024f9bac91bSBenno Rice 1025f9bac91bSBenno Rice void 102659276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired) 1027f9bac91bSBenno Rice { 10280f92104cSBenno Rice struct pvo_entry *pvo; 10290f92104cSBenno Rice 103048d0b1a0SAlan Cox PMAP_LOCK(pm); 103159276937SPeter Grehan pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL); 10320f92104cSBenno Rice 10330f92104cSBenno Rice if (pvo != NULL) { 10340f92104cSBenno Rice if (wired) { 10350f92104cSBenno Rice if ((pvo->pvo_vaddr & PVO_WIRED) == 0) 10360f92104cSBenno Rice pm->pm_stats.wired_count++; 10370f92104cSBenno Rice pvo->pvo_vaddr |= PVO_WIRED; 10380f92104cSBenno Rice } else { 10390f92104cSBenno Rice if ((pvo->pvo_vaddr & PVO_WIRED) != 0) 10400f92104cSBenno Rice pm->pm_stats.wired_count--; 10410f92104cSBenno Rice pvo->pvo_vaddr &= ~PVO_WIRED; 10420f92104cSBenno Rice } 10430f92104cSBenno Rice } 104448d0b1a0SAlan Cox PMAP_UNLOCK(pm); 1045f9bac91bSBenno Rice } 1046f9bac91bSBenno Rice 1047f9bac91bSBenno Rice void 104859276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst) 1049f9bac91bSBenno Rice { 105025e2288dSBenno Rice vm_offset_t dst; 105125e2288dSBenno Rice vm_offset_t src; 105225e2288dSBenno Rice 105325e2288dSBenno Rice dst = VM_PAGE_TO_PHYS(mdst); 105425e2288dSBenno Rice src = VM_PAGE_TO_PHYS(msrc); 105525e2288dSBenno Rice 1056e3c2930dSNathan Whitehorn bcopy((void *)src, (void *)dst, PAGE_SIZE); 1057f9bac91bSBenno Rice } 1058111c77dcSBenno Rice 1059e8a4a618SKonstantin Belousov void 1060e8a4a618SKonstantin Belousov moea_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset, 1061e8a4a618SKonstantin Belousov vm_page_t *mb, vm_offset_t b_offset, int xfersize) 1062e8a4a618SKonstantin Belousov { 1063e8a4a618SKonstantin Belousov void *a_cp, *b_cp; 1064e8a4a618SKonstantin Belousov vm_offset_t a_pg_offset, b_pg_offset; 1065e8a4a618SKonstantin Belousov int cnt; 1066e8a4a618SKonstantin Belousov 1067e8a4a618SKonstantin Belousov while (xfersize > 0) { 1068e8a4a618SKonstantin Belousov a_pg_offset = a_offset & PAGE_MASK; 1069e8a4a618SKonstantin Belousov cnt = min(xfersize, PAGE_SIZE - a_pg_offset); 1070e8a4a618SKonstantin Belousov a_cp = (char *)VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT]) + 1071e8a4a618SKonstantin Belousov a_pg_offset; 1072e8a4a618SKonstantin Belousov b_pg_offset = b_offset & PAGE_MASK; 1073e8a4a618SKonstantin Belousov cnt = min(cnt, PAGE_SIZE - b_pg_offset); 1074e8a4a618SKonstantin Belousov b_cp = (char *)VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT]) + 1075e8a4a618SKonstantin Belousov b_pg_offset; 1076e8a4a618SKonstantin Belousov bcopy(a_cp, b_cp, cnt); 1077e8a4a618SKonstantin Belousov a_offset += cnt; 1078e8a4a618SKonstantin Belousov b_offset += cnt; 1079e8a4a618SKonstantin Belousov xfersize -= cnt; 1080e8a4a618SKonstantin Belousov } 1081e8a4a618SKonstantin Belousov } 1082e8a4a618SKonstantin Belousov 1083111c77dcSBenno Rice /* 10845244eac9SBenno Rice * Zero a page of physical memory by temporarily mapping it into the tlb. 10855244eac9SBenno Rice */ 10865244eac9SBenno Rice void 108759276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m) 10885244eac9SBenno Rice { 10891a87a0daSPeter Wemm vm_offset_t pa = VM_PAGE_TO_PHYS(m); 10905b43c63dSMarcel Moolenaar void *va = (void *)pa; 10915244eac9SBenno Rice 10925244eac9SBenno Rice bzero(va, PAGE_SIZE); 10935244eac9SBenno Rice } 10945244eac9SBenno Rice 10955244eac9SBenno Rice void 109659276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size) 10975244eac9SBenno Rice { 10983495845eSBenno Rice vm_offset_t pa = VM_PAGE_TO_PHYS(m); 10995b43c63dSMarcel Moolenaar void *va = (void *)(pa + off); 11003495845eSBenno Rice 11015b43c63dSMarcel Moolenaar bzero(va, size); 11025244eac9SBenno Rice } 11035244eac9SBenno Rice 1104a58b3a68SPeter Wemm void 110559276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m) 1106a58b3a68SPeter Wemm { 11075b43c63dSMarcel Moolenaar vm_offset_t pa = VM_PAGE_TO_PHYS(m); 11085b43c63dSMarcel Moolenaar void *va = (void *)pa; 1109a58b3a68SPeter Wemm 11105b43c63dSMarcel Moolenaar bzero(va, PAGE_SIZE); 1111a58b3a68SPeter Wemm } 1112a58b3a68SPeter Wemm 11135244eac9SBenno Rice /* 11145244eac9SBenno Rice * Map the given physical page at the specified virtual address in the 11155244eac9SBenno Rice * target pmap with the protection requested. If specified the page 11165244eac9SBenno Rice * will be wired down. 11175244eac9SBenno Rice */ 11185244eac9SBenno Rice void 111959276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, 11205244eac9SBenno Rice boolean_t wired) 11215244eac9SBenno Rice { 1122ce142d9eSAlan Cox 11233653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 1124ce142d9eSAlan Cox PMAP_LOCK(pmap); 112567c867eeSAlan Cox moea_enter_locked(pmap, va, m, prot, wired); 11263653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 1127ce142d9eSAlan Cox PMAP_UNLOCK(pmap); 1128ce142d9eSAlan Cox } 1129ce142d9eSAlan Cox 1130ce142d9eSAlan Cox /* 1131ce142d9eSAlan Cox * Map the given physical page at the specified virtual address in the 1132ce142d9eSAlan Cox * target pmap with the protection requested. If specified the page 1133ce142d9eSAlan Cox * will be wired down. 1134ce142d9eSAlan Cox * 1135ce142d9eSAlan Cox * The page queues and pmap must be locked. 1136ce142d9eSAlan Cox */ 1137ce142d9eSAlan Cox static void 1138ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, 1139ce142d9eSAlan Cox boolean_t wired) 1140ce142d9eSAlan Cox { 11415244eac9SBenno Rice struct pvo_head *pvo_head; 1142378862a7SJeff Roberson uma_zone_t zone; 11438207b362SBenno Rice vm_page_t pg; 114457bd5cceSNathan Whitehorn u_int pte_lo, pvo_flags; 11455244eac9SBenno Rice int error; 11465244eac9SBenno Rice 114759276937SPeter Grehan if (!moea_initialized) { 114859276937SPeter Grehan pvo_head = &moea_pvo_kunmanaged; 114959276937SPeter Grehan zone = moea_upvo_zone; 11505244eac9SBenno Rice pvo_flags = 0; 11518207b362SBenno Rice pg = NULL; 11525244eac9SBenno Rice } else { 115303b6e025SPeter Grehan pvo_head = vm_page_to_pvoh(m); 115403b6e025SPeter Grehan pg = m; 115559276937SPeter Grehan zone = moea_mpvo_zone; 11565244eac9SBenno Rice pvo_flags = PVO_MANAGED; 11575244eac9SBenno Rice } 1158f489bf21SAlan Cox if (pmap_bootstrapped) 11593653f5cbSAlan Cox rw_assert(&pvh_global_lock, RA_WLOCKED); 1160ce142d9eSAlan Cox PMAP_LOCK_ASSERT(pmap, MA_OWNED); 1161dc1558d1SAttilio Rao if ((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) == 0) 1162658f180bSAlan Cox VM_OBJECT_ASSERT_LOCKED(m->object); 11635244eac9SBenno Rice 11644dba5df1SPeter Grehan /* XXX change the pvo head for fake pages */ 1165d98d0ce2SKonstantin Belousov if ((m->oflags & VPO_UNMANAGED) != 0) { 1166a130b35fSNathan Whitehorn pvo_flags &= ~PVO_MANAGED; 116759276937SPeter Grehan pvo_head = &moea_pvo_kunmanaged; 1168a130b35fSNathan Whitehorn zone = moea_upvo_zone; 1169a130b35fSNathan Whitehorn } 11704dba5df1SPeter Grehan 1171cd6a97f0SNathan Whitehorn pte_lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m)); 11725244eac9SBenno Rice 117344b8bd66SAlan Cox if (prot & VM_PROT_WRITE) { 11745244eac9SBenno Rice pte_lo |= PTE_BW; 11752368a371SAlan Cox if (pmap_bootstrapped && 1176d98d0ce2SKonstantin Belousov (m->oflags & VPO_UNMANAGED) == 0) 11773407fefeSKonstantin Belousov vm_page_aflag_set(m, PGA_WRITEABLE); 117844b8bd66SAlan Cox } else 11795244eac9SBenno Rice pte_lo |= PTE_BR; 11805244eac9SBenno Rice 11814dba5df1SPeter Grehan if (prot & VM_PROT_EXECUTE) 11824dba5df1SPeter Grehan pvo_flags |= PVO_EXECUTABLE; 11835244eac9SBenno Rice 11845244eac9SBenno Rice if (wired) 11855244eac9SBenno Rice pvo_flags |= PVO_WIRED; 11865244eac9SBenno Rice 118759276937SPeter Grehan error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m), 11888207b362SBenno Rice pte_lo, pvo_flags); 11895244eac9SBenno Rice 11908207b362SBenno Rice /* 119157bd5cceSNathan Whitehorn * Flush the real page from the instruction cache. This has be done 119257bd5cceSNathan Whitehorn * for all user mappings to prevent information leakage via the 1193805bee55SNathan Whitehorn * instruction cache. moea_pvo_enter() returns ENOENT for the first 1194805bee55SNathan Whitehorn * mapping for a page. 11958207b362SBenno Rice */ 1196805bee55SNathan Whitehorn if (pmap != kernel_pmap && error == ENOENT && 1197805bee55SNathan Whitehorn (pte_lo & (PTE_I | PTE_G)) == 0) 119859276937SPeter Grehan moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE); 1199ce142d9eSAlan Cox } 1200ce142d9eSAlan Cox 1201ce142d9eSAlan Cox /* 1202ce142d9eSAlan Cox * Maps a sequence of resident pages belonging to the same object. 1203ce142d9eSAlan Cox * The sequence begins with the given page m_start. This page is 1204ce142d9eSAlan Cox * mapped at the given virtual address start. Each subsequent page is 1205ce142d9eSAlan Cox * mapped at a virtual address that is offset from start by the same 1206ce142d9eSAlan Cox * amount as the page is offset from m_start within the object. The 1207ce142d9eSAlan Cox * last page in the sequence is the page with the largest offset from 1208ce142d9eSAlan Cox * m_start that can be mapped at a virtual address less than the given 1209ce142d9eSAlan Cox * virtual address end. Not every virtual page between start and end 1210ce142d9eSAlan Cox * is mapped; only those for which a resident page exists with the 1211ce142d9eSAlan Cox * corresponding offset from m_start are mapped. 1212ce142d9eSAlan Cox */ 1213ce142d9eSAlan Cox void 1214ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end, 1215ce142d9eSAlan Cox vm_page_t m_start, vm_prot_t prot) 1216ce142d9eSAlan Cox { 1217ce142d9eSAlan Cox vm_page_t m; 1218ce142d9eSAlan Cox vm_pindex_t diff, psize; 1219ce142d9eSAlan Cox 1220*9af6d512SAttilio Rao VM_OBJECT_ASSERT_LOCKED(m_start->object); 1221*9af6d512SAttilio Rao 1222ce142d9eSAlan Cox psize = atop(end - start); 1223ce142d9eSAlan Cox m = m_start; 12243653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 1225ce142d9eSAlan Cox PMAP_LOCK(pm); 1226ce142d9eSAlan Cox while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) { 1227ce142d9eSAlan Cox moea_enter_locked(pm, start + ptoa(diff), m, prot & 1228ce142d9eSAlan Cox (VM_PROT_READ | VM_PROT_EXECUTE), FALSE); 1229ce142d9eSAlan Cox m = TAILQ_NEXT(m, listq); 1230ce142d9eSAlan Cox } 12313653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 1232ce142d9eSAlan Cox PMAP_UNLOCK(pm); 12335244eac9SBenno Rice } 12345244eac9SBenno Rice 12352053c127SStephan Uphoff void 123659276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m, 12372053c127SStephan Uphoff vm_prot_t prot) 1238dca96f1aSAlan Cox { 1239dca96f1aSAlan Cox 12403653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 1241ce142d9eSAlan Cox PMAP_LOCK(pm); 1242ce142d9eSAlan Cox moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE), 124359276937SPeter Grehan FALSE); 12443653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 1245ce142d9eSAlan Cox PMAP_UNLOCK(pm); 1246dca96f1aSAlan Cox } 1247dca96f1aSAlan Cox 124856b09388SAlan Cox vm_paddr_t 124959276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va) 12505244eac9SBenno Rice { 12510f92104cSBenno Rice struct pvo_entry *pvo; 125248d0b1a0SAlan Cox vm_paddr_t pa; 12530f92104cSBenno Rice 125448d0b1a0SAlan Cox PMAP_LOCK(pm); 125559276937SPeter Grehan pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL); 125648d0b1a0SAlan Cox if (pvo == NULL) 125748d0b1a0SAlan Cox pa = 0; 125848d0b1a0SAlan Cox else 125952a7870dSNathan Whitehorn pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF); 126048d0b1a0SAlan Cox PMAP_UNLOCK(pm); 126148d0b1a0SAlan Cox return (pa); 12625244eac9SBenno Rice } 12635244eac9SBenno Rice 12645244eac9SBenno Rice /* 126584792e72SPeter Grehan * Atomically extract and hold the physical page with the given 126684792e72SPeter Grehan * pmap and virtual address pair if that mapping permits the given 126784792e72SPeter Grehan * protection. 126884792e72SPeter Grehan */ 126984792e72SPeter Grehan vm_page_t 127059276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot) 127184792e72SPeter Grehan { 1272ab50a262SAlan Cox struct pvo_entry *pvo; 127384792e72SPeter Grehan vm_page_t m; 12742965a453SKip Macy vm_paddr_t pa; 127584792e72SPeter Grehan 127684792e72SPeter Grehan m = NULL; 12772965a453SKip Macy pa = 0; 127848d0b1a0SAlan Cox PMAP_LOCK(pmap); 12792965a453SKip Macy retry: 128059276937SPeter Grehan pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL); 128152a7870dSNathan Whitehorn if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) && 128252a7870dSNathan Whitehorn ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW || 1283ab50a262SAlan Cox (prot & VM_PROT_WRITE) == 0)) { 12842965a453SKip Macy if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa)) 12852965a453SKip Macy goto retry; 128652a7870dSNathan Whitehorn m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN); 128784792e72SPeter Grehan vm_page_hold(m); 128884792e72SPeter Grehan } 12892965a453SKip Macy PA_UNLOCK_COND(pa); 129048d0b1a0SAlan Cox PMAP_UNLOCK(pmap); 129184792e72SPeter Grehan return (m); 129284792e72SPeter Grehan } 129384792e72SPeter Grehan 12945244eac9SBenno Rice void 129559276937SPeter Grehan moea_init(mmu_t mmu) 12965244eac9SBenno Rice { 12975244eac9SBenno Rice 129859276937SPeter Grehan moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry), 12990ee6dbd7SPeter Grehan NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 13000ee6dbd7SPeter Grehan UMA_ZONE_VM | UMA_ZONE_NOFREE); 130159276937SPeter Grehan moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry), 13020ee6dbd7SPeter Grehan NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 13030ee6dbd7SPeter Grehan UMA_ZONE_VM | UMA_ZONE_NOFREE); 130459276937SPeter Grehan moea_initialized = TRUE; 13055244eac9SBenno Rice } 13065244eac9SBenno Rice 13075244eac9SBenno Rice boolean_t 13087b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m) 13097b85f591SAlan Cox { 13108d9e6d9fSAlan Cox boolean_t rv; 13117b85f591SAlan Cox 1312d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1313c46b90e9SAlan Cox ("moea_is_referenced: page %p is not managed", m)); 13148d9e6d9fSAlan Cox rw_wlock(&pvh_global_lock); 13158d9e6d9fSAlan Cox rv = moea_query_bit(m, PTE_REF); 13168d9e6d9fSAlan Cox rw_wunlock(&pvh_global_lock); 13178d9e6d9fSAlan Cox return (rv); 13187b85f591SAlan Cox } 13197b85f591SAlan Cox 13207b85f591SAlan Cox boolean_t 132159276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m) 13225244eac9SBenno Rice { 13238d9e6d9fSAlan Cox boolean_t rv; 13240f92104cSBenno Rice 1325d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1326567e51e1SAlan Cox ("moea_is_modified: page %p is not managed", m)); 1327567e51e1SAlan Cox 1328567e51e1SAlan Cox /* 13293407fefeSKonstantin Belousov * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be 13303407fefeSKonstantin Belousov * concurrently set while the object is locked. Thus, if PGA_WRITEABLE 1331567e51e1SAlan Cox * is clear, no PTEs can have PTE_CHG set. 1332567e51e1SAlan Cox */ 133389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(m->object); 1334567e51e1SAlan Cox if ((m->oflags & VPO_BUSY) == 0 && 13353407fefeSKonstantin Belousov (m->aflags & PGA_WRITEABLE) == 0) 13360f92104cSBenno Rice return (FALSE); 13378d9e6d9fSAlan Cox rw_wlock(&pvh_global_lock); 13388d9e6d9fSAlan Cox rv = moea_query_bit(m, PTE_CHG); 13398d9e6d9fSAlan Cox rw_wunlock(&pvh_global_lock); 13408d9e6d9fSAlan Cox return (rv); 1341566526a9SAlan Cox } 1342566526a9SAlan Cox 1343e396eb60SAlan Cox boolean_t 1344e396eb60SAlan Cox moea_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va) 1345e396eb60SAlan Cox { 1346e396eb60SAlan Cox struct pvo_entry *pvo; 1347e396eb60SAlan Cox boolean_t rv; 1348e396eb60SAlan Cox 1349e396eb60SAlan Cox PMAP_LOCK(pmap); 1350e396eb60SAlan Cox pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL); 1351e396eb60SAlan Cox rv = pvo == NULL || (pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0; 1352e396eb60SAlan Cox PMAP_UNLOCK(pmap); 1353e396eb60SAlan Cox return (rv); 1354e396eb60SAlan Cox } 1355e396eb60SAlan Cox 13565244eac9SBenno Rice void 135759276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m) 13585244eac9SBenno Rice { 135903b6e025SPeter Grehan 1360d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1361567e51e1SAlan Cox ("moea_clear_reference: page %p is not managed", m)); 13628d9e6d9fSAlan Cox rw_wlock(&pvh_global_lock); 1363ce186587SAlan Cox moea_clear_bit(m, PTE_REF); 13648d9e6d9fSAlan Cox rw_wunlock(&pvh_global_lock); 136503b6e025SPeter Grehan } 136603b6e025SPeter Grehan 136703b6e025SPeter Grehan void 136859276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m) 136903b6e025SPeter Grehan { 137003b6e025SPeter Grehan 1371d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1372567e51e1SAlan Cox ("moea_clear_modify: page %p is not managed", m)); 137389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(m->object); 1374567e51e1SAlan Cox KASSERT((m->oflags & VPO_BUSY) == 0, 1375567e51e1SAlan Cox ("moea_clear_modify: page %p is busy", m)); 1376567e51e1SAlan Cox 1377567e51e1SAlan Cox /* 13783407fefeSKonstantin Belousov * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_CHG 1379567e51e1SAlan Cox * set. If the object containing the page is locked and the page is 13803407fefeSKonstantin Belousov * not VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set. 1381567e51e1SAlan Cox */ 13823407fefeSKonstantin Belousov if ((m->aflags & PGA_WRITEABLE) == 0) 138303b6e025SPeter Grehan return; 13848d9e6d9fSAlan Cox rw_wlock(&pvh_global_lock); 1385ce186587SAlan Cox moea_clear_bit(m, PTE_CHG); 13868d9e6d9fSAlan Cox rw_wunlock(&pvh_global_lock); 13875244eac9SBenno Rice } 13885244eac9SBenno Rice 13897f3a4093SMike Silbersack /* 139078985e42SAlan Cox * Clear the write and modified bits in each of the given page's mappings. 139178985e42SAlan Cox */ 139278985e42SAlan Cox void 139378985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m) 139478985e42SAlan Cox { 139578985e42SAlan Cox struct pvo_entry *pvo; 139678985e42SAlan Cox struct pte *pt; 139778985e42SAlan Cox pmap_t pmap; 139878985e42SAlan Cox u_int lo; 139978985e42SAlan Cox 1400d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 14019ab6032fSAlan Cox ("moea_remove_write: page %p is not managed", m)); 14029ab6032fSAlan Cox 14039ab6032fSAlan Cox /* 14043407fefeSKonstantin Belousov * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by 14053407fefeSKonstantin Belousov * another thread while the object is locked. Thus, if PGA_WRITEABLE 14069ab6032fSAlan Cox * is clear, no page table entries need updating. 14079ab6032fSAlan Cox */ 140889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(m->object); 14099ab6032fSAlan Cox if ((m->oflags & VPO_BUSY) == 0 && 14103407fefeSKonstantin Belousov (m->aflags & PGA_WRITEABLE) == 0) 141178985e42SAlan Cox return; 14123653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 141378985e42SAlan Cox lo = moea_attr_fetch(m); 1414e4f72b32SMarcel Moolenaar powerpc_sync(); 141578985e42SAlan Cox LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 141678985e42SAlan Cox pmap = pvo->pvo_pmap; 141778985e42SAlan Cox PMAP_LOCK(pmap); 141852a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) { 141978985e42SAlan Cox pt = moea_pvo_to_pte(pvo, -1); 142052a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~PTE_PP; 142152a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo |= PTE_BR; 142278985e42SAlan Cox if (pt != NULL) { 142352a7870dSNathan Whitehorn moea_pte_synch(pt, &pvo->pvo_pte.pte); 142452a7870dSNathan Whitehorn lo |= pvo->pvo_pte.pte.pte_lo; 142552a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG; 142652a7870dSNathan Whitehorn moea_pte_change(pt, &pvo->pvo_pte.pte, 142778985e42SAlan Cox pvo->pvo_vaddr); 142878985e42SAlan Cox mtx_unlock(&moea_table_mutex); 142978985e42SAlan Cox } 143078985e42SAlan Cox } 143178985e42SAlan Cox PMAP_UNLOCK(pmap); 143278985e42SAlan Cox } 143378985e42SAlan Cox if ((lo & PTE_CHG) != 0) { 143478985e42SAlan Cox moea_attr_clear(m, PTE_CHG); 143578985e42SAlan Cox vm_page_dirty(m); 143678985e42SAlan Cox } 14373407fefeSKonstantin Belousov vm_page_aflag_clear(m, PGA_WRITEABLE); 14383653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 143978985e42SAlan Cox } 144078985e42SAlan Cox 144178985e42SAlan Cox /* 144259276937SPeter Grehan * moea_ts_referenced: 14437f3a4093SMike Silbersack * 14447f3a4093SMike Silbersack * Return a count of reference bits for a page, clearing those bits. 14457f3a4093SMike Silbersack * It is not necessary for every reference bit to be cleared, but it 14467f3a4093SMike Silbersack * is necessary that 0 only be returned when there are truly no 14477f3a4093SMike Silbersack * reference bits set. 14487f3a4093SMike Silbersack * 14497f3a4093SMike Silbersack * XXX: The exact number of bits to check and clear is a matter that 14507f3a4093SMike Silbersack * should be tested and standardized at some point in the future for 14517f3a4093SMike Silbersack * optimal aging of shared pages. 14527f3a4093SMike Silbersack */ 14538d9e6d9fSAlan Cox int 145459276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m) 14555244eac9SBenno Rice { 14568d9e6d9fSAlan Cox int count; 145703b6e025SPeter Grehan 1458d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1459ce186587SAlan Cox ("moea_ts_referenced: page %p is not managed", m)); 14608d9e6d9fSAlan Cox rw_wlock(&pvh_global_lock); 14618d9e6d9fSAlan Cox count = moea_clear_bit(m, PTE_REF); 14628d9e6d9fSAlan Cox rw_wunlock(&pvh_global_lock); 14638d9e6d9fSAlan Cox return (count); 14645244eac9SBenno Rice } 14655244eac9SBenno Rice 14665244eac9SBenno Rice /* 1467c1f4123bSNathan Whitehorn * Modify the WIMG settings of all mappings for a page. 1468c1f4123bSNathan Whitehorn */ 1469c1f4123bSNathan Whitehorn void 1470c1f4123bSNathan Whitehorn moea_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma) 1471c1f4123bSNathan Whitehorn { 1472c1f4123bSNathan Whitehorn struct pvo_entry *pvo; 1473cd6a97f0SNathan Whitehorn struct pvo_head *pvo_head; 1474c1f4123bSNathan Whitehorn struct pte *pt; 1475c1f4123bSNathan Whitehorn pmap_t pmap; 1476c1f4123bSNathan Whitehorn u_int lo; 1477c1f4123bSNathan Whitehorn 1478d98d0ce2SKonstantin Belousov if ((m->oflags & VPO_UNMANAGED) != 0) { 1479cd6a97f0SNathan Whitehorn m->md.mdpg_cache_attrs = ma; 1480cd6a97f0SNathan Whitehorn return; 1481cd6a97f0SNathan Whitehorn } 1482cd6a97f0SNathan Whitehorn 14833653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 1484cd6a97f0SNathan Whitehorn pvo_head = vm_page_to_pvoh(m); 1485c1f4123bSNathan Whitehorn lo = moea_calc_wimg(VM_PAGE_TO_PHYS(m), ma); 1486cd6a97f0SNathan Whitehorn 1487cd6a97f0SNathan Whitehorn LIST_FOREACH(pvo, pvo_head, pvo_vlink) { 1488c1f4123bSNathan Whitehorn pmap = pvo->pvo_pmap; 1489c1f4123bSNathan Whitehorn PMAP_LOCK(pmap); 1490c1f4123bSNathan Whitehorn pt = moea_pvo_to_pte(pvo, -1); 1491c1f4123bSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~PTE_WIMG; 1492c1f4123bSNathan Whitehorn pvo->pvo_pte.pte.pte_lo |= lo; 1493c1f4123bSNathan Whitehorn if (pt != NULL) { 1494c1f4123bSNathan Whitehorn moea_pte_change(pt, &pvo->pvo_pte.pte, 1495c1f4123bSNathan Whitehorn pvo->pvo_vaddr); 1496c1f4123bSNathan Whitehorn if (pvo->pvo_pmap == kernel_pmap) 1497c1f4123bSNathan Whitehorn isync(); 1498c1f4123bSNathan Whitehorn } 1499c1f4123bSNathan Whitehorn mtx_unlock(&moea_table_mutex); 1500c1f4123bSNathan Whitehorn PMAP_UNLOCK(pmap); 1501c1f4123bSNathan Whitehorn } 1502c1f4123bSNathan Whitehorn m->md.mdpg_cache_attrs = ma; 15033653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 1504c1f4123bSNathan Whitehorn } 1505c1f4123bSNathan Whitehorn 1506c1f4123bSNathan Whitehorn /* 15075244eac9SBenno Rice * Map a wired page into kernel virtual address space. 15085244eac9SBenno Rice */ 15095244eac9SBenno Rice void 151020b79612SRafal Jaworowski moea_kenter(mmu_t mmu, vm_offset_t va, vm_paddr_t pa) 15115244eac9SBenno Rice { 1512c1f4123bSNathan Whitehorn 1513c1f4123bSNathan Whitehorn moea_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT); 1514c1f4123bSNathan Whitehorn } 1515c1f4123bSNathan Whitehorn 1516c1f4123bSNathan Whitehorn void 1517c1f4123bSNathan Whitehorn moea_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma) 1518c1f4123bSNathan Whitehorn { 15195244eac9SBenno Rice u_int pte_lo; 15205244eac9SBenno Rice int error; 15215244eac9SBenno Rice 15225244eac9SBenno Rice #if 0 15235244eac9SBenno Rice if (va < VM_MIN_KERNEL_ADDRESS) 152459276937SPeter Grehan panic("moea_kenter: attempt to enter non-kernel address %#x", 15255244eac9SBenno Rice va); 15265244eac9SBenno Rice #endif 15275244eac9SBenno Rice 1528c1f4123bSNathan Whitehorn pte_lo = moea_calc_wimg(pa, ma); 15295244eac9SBenno Rice 15304711f8d7SAlan Cox PMAP_LOCK(kernel_pmap); 153159276937SPeter Grehan error = moea_pvo_enter(kernel_pmap, moea_upvo_zone, 153259276937SPeter Grehan &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED); 15335244eac9SBenno Rice 15345244eac9SBenno Rice if (error != 0 && error != ENOENT) 153559276937SPeter Grehan panic("moea_kenter: failed to enter va %#x pa %#x: %d", va, 15365244eac9SBenno Rice pa, error); 15375244eac9SBenno Rice 15384711f8d7SAlan Cox PMAP_UNLOCK(kernel_pmap); 15395244eac9SBenno Rice } 15405244eac9SBenno Rice 1541e79f59e8SBenno Rice /* 1542e79f59e8SBenno Rice * Extract the physical page address associated with the given kernel virtual 1543e79f59e8SBenno Rice * address. 1544e79f59e8SBenno Rice */ 154520b79612SRafal Jaworowski vm_paddr_t 154659276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va) 15475244eac9SBenno Rice { 1548e79f59e8SBenno Rice struct pvo_entry *pvo; 154948d0b1a0SAlan Cox vm_paddr_t pa; 1550e79f59e8SBenno Rice 15510efd0097SPeter Grehan /* 155252a7870dSNathan Whitehorn * Allow direct mappings on 32-bit OEA 15530efd0097SPeter Grehan */ 15540efd0097SPeter Grehan if (va < VM_MIN_KERNEL_ADDRESS) { 15550efd0097SPeter Grehan return (va); 15560efd0097SPeter Grehan } 15570efd0097SPeter Grehan 155848d0b1a0SAlan Cox PMAP_LOCK(kernel_pmap); 155959276937SPeter Grehan pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL); 156059276937SPeter Grehan KASSERT(pvo != NULL, ("moea_kextract: no addr found")); 156152a7870dSNathan Whitehorn pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF); 156248d0b1a0SAlan Cox PMAP_UNLOCK(kernel_pmap); 156348d0b1a0SAlan Cox return (pa); 1564e79f59e8SBenno Rice } 1565e79f59e8SBenno Rice 156688afb2a3SBenno Rice /* 156788afb2a3SBenno Rice * Remove a wired page from kernel virtual address space. 156888afb2a3SBenno Rice */ 15695244eac9SBenno Rice void 157059276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va) 15715244eac9SBenno Rice { 157288afb2a3SBenno Rice 157359276937SPeter Grehan moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE); 15745244eac9SBenno Rice } 15755244eac9SBenno Rice 15765244eac9SBenno Rice /* 15775244eac9SBenno Rice * Map a range of physical addresses into kernel virtual address space. 15785244eac9SBenno Rice * 15795244eac9SBenno Rice * The value passed in *virt is a suggested virtual address for the mapping. 15805244eac9SBenno Rice * Architectures which can support a direct-mapped physical to virtual region 15815244eac9SBenno Rice * can return the appropriate address within that region, leaving '*virt' 15825244eac9SBenno Rice * unchanged. We cannot and therefore do not; *virt is updated with the 15835244eac9SBenno Rice * first usable address after the mapped region. 15845244eac9SBenno Rice */ 15855244eac9SBenno Rice vm_offset_t 158620b79612SRafal Jaworowski moea_map(mmu_t mmu, vm_offset_t *virt, vm_paddr_t pa_start, 158720b79612SRafal Jaworowski vm_paddr_t pa_end, int prot) 15885244eac9SBenno Rice { 15895244eac9SBenno Rice vm_offset_t sva, va; 15905244eac9SBenno Rice 15915244eac9SBenno Rice sva = *virt; 15925244eac9SBenno Rice va = sva; 15935244eac9SBenno Rice for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE) 159459276937SPeter Grehan moea_kenter(mmu, va, pa_start); 15955244eac9SBenno Rice *virt = va; 15965244eac9SBenno Rice return (sva); 15975244eac9SBenno Rice } 15985244eac9SBenno Rice 15995244eac9SBenno Rice /* 16007f3a4093SMike Silbersack * Returns true if the pmap's pv is one of the first 16017f3a4093SMike Silbersack * 16 pvs linked to from this page. This count may 16027f3a4093SMike Silbersack * be changed upwards or downwards in the future; it 16037f3a4093SMike Silbersack * is only necessary that true be returned for a small 16047f3a4093SMike Silbersack * subset of pmaps for proper page aging. 16057f3a4093SMike Silbersack */ 16065244eac9SBenno Rice boolean_t 160759276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m) 16085244eac9SBenno Rice { 160903b6e025SPeter Grehan int loops; 161003b6e025SPeter Grehan struct pvo_entry *pvo; 1611ce186587SAlan Cox boolean_t rv; 161203b6e025SPeter Grehan 1613d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1614ce186587SAlan Cox ("moea_page_exists_quick: page %p is not managed", m)); 161503b6e025SPeter Grehan loops = 0; 1616ce186587SAlan Cox rv = FALSE; 16173653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 161803b6e025SPeter Grehan LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 1619ce186587SAlan Cox if (pvo->pvo_pmap == pmap) { 1620ce186587SAlan Cox rv = TRUE; 1621ce186587SAlan Cox break; 1622ce186587SAlan Cox } 162303b6e025SPeter Grehan if (++loops >= 16) 162403b6e025SPeter Grehan break; 162503b6e025SPeter Grehan } 16263653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 1627ce186587SAlan Cox return (rv); 16285244eac9SBenno Rice } 16295244eac9SBenno Rice 163059677d3cSAlan Cox /* 163159677d3cSAlan Cox * Return the number of managed mappings to the given physical page 163259677d3cSAlan Cox * that are wired. 163359677d3cSAlan Cox */ 163459677d3cSAlan Cox int 163559677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m) 163659677d3cSAlan Cox { 163759677d3cSAlan Cox struct pvo_entry *pvo; 163859677d3cSAlan Cox int count; 163959677d3cSAlan Cox 164059677d3cSAlan Cox count = 0; 1641d98d0ce2SKonstantin Belousov if ((m->oflags & VPO_UNMANAGED) != 0) 164259677d3cSAlan Cox return (count); 16433653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 164459677d3cSAlan Cox LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) 164559677d3cSAlan Cox if ((pvo->pvo_vaddr & PVO_WIRED) != 0) 164659677d3cSAlan Cox count++; 16473653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 164859677d3cSAlan Cox return (count); 164959677d3cSAlan Cox } 165059677d3cSAlan Cox 165159276937SPeter Grehan static u_int moea_vsidcontext; 16525244eac9SBenno Rice 16535244eac9SBenno Rice void 165459276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap) 16555244eac9SBenno Rice { 16565244eac9SBenno Rice int i, mask; 16575244eac9SBenno Rice u_int entropy; 16585244eac9SBenno Rice 165959276937SPeter Grehan KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap")); 166048d0b1a0SAlan Cox PMAP_LOCK_INIT(pmap); 1661ccc4a5c7SNathan Whitehorn RB_INIT(&pmap->pmap_pvo); 16624daf20b2SPeter Grehan 16635244eac9SBenno Rice entropy = 0; 16645244eac9SBenno Rice __asm __volatile("mftb %0" : "=r"(entropy)); 16655244eac9SBenno Rice 166652a7870dSNathan Whitehorn if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap)) 166752a7870dSNathan Whitehorn == NULL) { 166852a7870dSNathan Whitehorn pmap->pmap_phys = pmap; 166952a7870dSNathan Whitehorn } 167052a7870dSNathan Whitehorn 167152a7870dSNathan Whitehorn 1672e9b5f218SNathan Whitehorn mtx_lock(&moea_vsid_mutex); 16735244eac9SBenno Rice /* 16745244eac9SBenno Rice * Allocate some segment registers for this pmap. 16755244eac9SBenno Rice */ 16765244eac9SBenno Rice for (i = 0; i < NPMAPS; i += VSID_NBPW) { 16775244eac9SBenno Rice u_int hash, n; 16785244eac9SBenno Rice 16795244eac9SBenno Rice /* 16805244eac9SBenno Rice * Create a new value by mutiplying by a prime and adding in 16815244eac9SBenno Rice * entropy from the timebase register. This is to make the 16825244eac9SBenno Rice * VSID more random so that the PT hash function collides 16835244eac9SBenno Rice * less often. (Note that the prime casues gcc to do shifts 16845244eac9SBenno Rice * instead of a multiply.) 16855244eac9SBenno Rice */ 168659276937SPeter Grehan moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy; 168759276937SPeter Grehan hash = moea_vsidcontext & (NPMAPS - 1); 16885244eac9SBenno Rice if (hash == 0) /* 0 is special, avoid it */ 16895244eac9SBenno Rice continue; 16905244eac9SBenno Rice n = hash >> 5; 16915244eac9SBenno Rice mask = 1 << (hash & (VSID_NBPW - 1)); 169259276937SPeter Grehan hash = (moea_vsidcontext & 0xfffff); 169359276937SPeter Grehan if (moea_vsid_bitmap[n] & mask) { /* collision? */ 16945244eac9SBenno Rice /* anything free in this bucket? */ 169559276937SPeter Grehan if (moea_vsid_bitmap[n] == 0xffffffff) { 169659276937SPeter Grehan entropy = (moea_vsidcontext >> 20); 16975244eac9SBenno Rice continue; 16985244eac9SBenno Rice } 16990dfddf6eSNathan Whitehorn i = ffs(~moea_vsid_bitmap[n]) - 1; 17005244eac9SBenno Rice mask = 1 << i; 17015244eac9SBenno Rice hash &= 0xfffff & ~(VSID_NBPW - 1); 17025244eac9SBenno Rice hash |= i; 17035244eac9SBenno Rice } 170446e93cbbSNathan Whitehorn KASSERT(!(moea_vsid_bitmap[n] & mask), 170546e93cbbSNathan Whitehorn ("Allocating in-use VSID group %#x\n", hash)); 170659276937SPeter Grehan moea_vsid_bitmap[n] |= mask; 17075244eac9SBenno Rice for (i = 0; i < 16; i++) 17085244eac9SBenno Rice pmap->pm_sr[i] = VSID_MAKE(i, hash); 1709e9b5f218SNathan Whitehorn mtx_unlock(&moea_vsid_mutex); 17105244eac9SBenno Rice return; 17115244eac9SBenno Rice } 17125244eac9SBenno Rice 1713e9b5f218SNathan Whitehorn mtx_unlock(&moea_vsid_mutex); 171459276937SPeter Grehan panic("moea_pinit: out of segments"); 17155244eac9SBenno Rice } 17165244eac9SBenno Rice 17175244eac9SBenno Rice /* 17185244eac9SBenno Rice * Initialize the pmap associated with process 0. 17195244eac9SBenno Rice */ 17205244eac9SBenno Rice void 172159276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm) 17225244eac9SBenno Rice { 17235244eac9SBenno Rice 172459276937SPeter Grehan moea_pinit(mmu, pm); 17255244eac9SBenno Rice bzero(&pm->pm_stats, sizeof(pm->pm_stats)); 17265244eac9SBenno Rice } 17275244eac9SBenno Rice 1728e79f59e8SBenno Rice /* 1729e79f59e8SBenno Rice * Set the physical protection on the specified range of this map as requested. 1730e79f59e8SBenno Rice */ 17315244eac9SBenno Rice void 173259276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva, 173359276937SPeter Grehan vm_prot_t prot) 17345244eac9SBenno Rice { 1735ccc4a5c7SNathan Whitehorn struct pvo_entry *pvo, *tpvo, key; 1736e79f59e8SBenno Rice struct pte *pt; 1737e79f59e8SBenno Rice 1738e79f59e8SBenno Rice KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap, 173959276937SPeter Grehan ("moea_protect: non current pmap")); 1740e79f59e8SBenno Rice 1741e79f59e8SBenno Rice if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 174259276937SPeter Grehan moea_remove(mmu, pm, sva, eva); 1743e79f59e8SBenno Rice return; 1744e79f59e8SBenno Rice } 1745e79f59e8SBenno Rice 17463653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 174748d0b1a0SAlan Cox PMAP_LOCK(pm); 1748ccc4a5c7SNathan Whitehorn key.pvo_vaddr = sva; 1749ccc4a5c7SNathan Whitehorn for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key); 1750ccc4a5c7SNathan Whitehorn pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) { 1751ccc4a5c7SNathan Whitehorn tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo); 1752e79f59e8SBenno Rice if ((prot & VM_PROT_EXECUTE) == 0) 1753e79f59e8SBenno Rice pvo->pvo_vaddr &= ~PVO_EXECUTABLE; 1754e79f59e8SBenno Rice 1755e79f59e8SBenno Rice /* 1756e79f59e8SBenno Rice * Grab the PTE pointer before we diddle with the cached PTE 1757e79f59e8SBenno Rice * copy. 1758e79f59e8SBenno Rice */ 1759ccc4a5c7SNathan Whitehorn pt = moea_pvo_to_pte(pvo, -1); 1760e79f59e8SBenno Rice /* 1761e79f59e8SBenno Rice * Change the protection of the page. 1762e79f59e8SBenno Rice */ 176352a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~PTE_PP; 176452a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo |= PTE_BR; 1765e79f59e8SBenno Rice 1766e79f59e8SBenno Rice /* 1767e79f59e8SBenno Rice * If the PVO is in the page table, update that pte as well. 1768e79f59e8SBenno Rice */ 1769d644a0b7SAlan Cox if (pt != NULL) { 177052a7870dSNathan Whitehorn moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr); 1771d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 1772d644a0b7SAlan Cox } 1773e79f59e8SBenno Rice } 17743653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 177548d0b1a0SAlan Cox PMAP_UNLOCK(pm); 17765244eac9SBenno Rice } 17775244eac9SBenno Rice 177888afb2a3SBenno Rice /* 177988afb2a3SBenno Rice * Map a list of wired pages into kernel virtual address space. This is 178088afb2a3SBenno Rice * intended for temporary mappings which do not need page modification or 178188afb2a3SBenno Rice * references recorded. Existing mappings in the region are overwritten. 178288afb2a3SBenno Rice */ 17835244eac9SBenno Rice void 178459276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count) 17855244eac9SBenno Rice { 178603b6e025SPeter Grehan vm_offset_t va; 17875244eac9SBenno Rice 178803b6e025SPeter Grehan va = sva; 178903b6e025SPeter Grehan while (count-- > 0) { 179059276937SPeter Grehan moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m)); 179103b6e025SPeter Grehan va += PAGE_SIZE; 179203b6e025SPeter Grehan m++; 179303b6e025SPeter Grehan } 17945244eac9SBenno Rice } 17955244eac9SBenno Rice 179688afb2a3SBenno Rice /* 179788afb2a3SBenno Rice * Remove page mappings from kernel virtual address space. Intended for 179859276937SPeter Grehan * temporary mappings entered by moea_qenter. 179988afb2a3SBenno Rice */ 18005244eac9SBenno Rice void 180159276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count) 18025244eac9SBenno Rice { 180303b6e025SPeter Grehan vm_offset_t va; 180488afb2a3SBenno Rice 180503b6e025SPeter Grehan va = sva; 180603b6e025SPeter Grehan while (count-- > 0) { 180759276937SPeter Grehan moea_kremove(mmu, va); 180803b6e025SPeter Grehan va += PAGE_SIZE; 180903b6e025SPeter Grehan } 18105244eac9SBenno Rice } 18115244eac9SBenno Rice 18125244eac9SBenno Rice void 181359276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap) 18145244eac9SBenno Rice { 181532bc7846SPeter Grehan int idx, mask; 181632bc7846SPeter Grehan 181732bc7846SPeter Grehan /* 181832bc7846SPeter Grehan * Free segment register's VSID 181932bc7846SPeter Grehan */ 182032bc7846SPeter Grehan if (pmap->pm_sr[0] == 0) 182159276937SPeter Grehan panic("moea_release"); 182232bc7846SPeter Grehan 1823e9b5f218SNathan Whitehorn mtx_lock(&moea_vsid_mutex); 182432bc7846SPeter Grehan idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1); 182532bc7846SPeter Grehan mask = 1 << (idx % VSID_NBPW); 182632bc7846SPeter Grehan idx /= VSID_NBPW; 182759276937SPeter Grehan moea_vsid_bitmap[idx] &= ~mask; 1828e9b5f218SNathan Whitehorn mtx_unlock(&moea_vsid_mutex); 182948d0b1a0SAlan Cox PMAP_LOCK_DESTROY(pmap); 18305244eac9SBenno Rice } 18315244eac9SBenno Rice 183288afb2a3SBenno Rice /* 183388afb2a3SBenno Rice * Remove the given range of addresses from the specified map. 183488afb2a3SBenno Rice */ 18355244eac9SBenno Rice void 183659276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva) 18375244eac9SBenno Rice { 1838ccc4a5c7SNathan Whitehorn struct pvo_entry *pvo, *tpvo, key; 183988afb2a3SBenno Rice 18403653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 184148d0b1a0SAlan Cox PMAP_LOCK(pm); 1842ccc4a5c7SNathan Whitehorn key.pvo_vaddr = sva; 1843ccc4a5c7SNathan Whitehorn for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key); 1844ccc4a5c7SNathan Whitehorn pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) { 1845ccc4a5c7SNathan Whitehorn tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo); 1846598d99ddSNathan Whitehorn moea_pvo_remove(pvo, -1); 1847598d99ddSNathan Whitehorn } 184848d0b1a0SAlan Cox PMAP_UNLOCK(pm); 18493653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 18505244eac9SBenno Rice } 18515244eac9SBenno Rice 1852e79f59e8SBenno Rice /* 185359276937SPeter Grehan * Remove physical page from all pmaps in which it resides. moea_pvo_remove() 185403b6e025SPeter Grehan * will reflect changes in pte's back to the vm_page. 185503b6e025SPeter Grehan */ 185603b6e025SPeter Grehan void 185759276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m) 185803b6e025SPeter Grehan { 185903b6e025SPeter Grehan struct pvo_head *pvo_head; 186003b6e025SPeter Grehan struct pvo_entry *pvo, *next_pvo; 186148d0b1a0SAlan Cox pmap_t pmap; 186203b6e025SPeter Grehan 18633653f5cbSAlan Cox rw_wlock(&pvh_global_lock); 186403b6e025SPeter Grehan pvo_head = vm_page_to_pvoh(m); 186503b6e025SPeter Grehan for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) { 186603b6e025SPeter Grehan next_pvo = LIST_NEXT(pvo, pvo_vlink); 186703b6e025SPeter Grehan 186848d0b1a0SAlan Cox pmap = pvo->pvo_pmap; 186948d0b1a0SAlan Cox PMAP_LOCK(pmap); 187059276937SPeter Grehan moea_pvo_remove(pvo, -1); 187148d0b1a0SAlan Cox PMAP_UNLOCK(pmap); 187203b6e025SPeter Grehan } 18738d9e6d9fSAlan Cox if ((m->aflags & PGA_WRITEABLE) && moea_query_bit(m, PTE_CHG)) { 1874c668b5b4SNathan Whitehorn moea_attr_clear(m, PTE_CHG); 1875062c8f4cSNathan Whitehorn vm_page_dirty(m); 1876062c8f4cSNathan Whitehorn } 18773407fefeSKonstantin Belousov vm_page_aflag_clear(m, PGA_WRITEABLE); 18783653f5cbSAlan Cox rw_wunlock(&pvh_global_lock); 187903b6e025SPeter Grehan } 188003b6e025SPeter Grehan 188103b6e025SPeter Grehan /* 18825244eac9SBenno Rice * Allocate a physical page of memory directly from the phys_avail map. 188359276937SPeter Grehan * Can only be called from moea_bootstrap before avail start and end are 18845244eac9SBenno Rice * calculated. 18855244eac9SBenno Rice */ 18865244eac9SBenno Rice static vm_offset_t 188759276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align) 18885244eac9SBenno Rice { 18895244eac9SBenno Rice vm_offset_t s, e; 18905244eac9SBenno Rice int i, j; 18915244eac9SBenno Rice 18925244eac9SBenno Rice size = round_page(size); 18935244eac9SBenno Rice for (i = 0; phys_avail[i + 1] != 0; i += 2) { 18945244eac9SBenno Rice if (align != 0) 18955244eac9SBenno Rice s = (phys_avail[i] + align - 1) & ~(align - 1); 18965244eac9SBenno Rice else 18975244eac9SBenno Rice s = phys_avail[i]; 18985244eac9SBenno Rice e = s + size; 18995244eac9SBenno Rice 19005244eac9SBenno Rice if (s < phys_avail[i] || e > phys_avail[i + 1]) 19015244eac9SBenno Rice continue; 19025244eac9SBenno Rice 19035244eac9SBenno Rice if (s == phys_avail[i]) { 19045244eac9SBenno Rice phys_avail[i] += size; 19055244eac9SBenno Rice } else if (e == phys_avail[i + 1]) { 19065244eac9SBenno Rice phys_avail[i + 1] -= size; 19075244eac9SBenno Rice } else { 19085244eac9SBenno Rice for (j = phys_avail_count * 2; j > i; j -= 2) { 19095244eac9SBenno Rice phys_avail[j] = phys_avail[j - 2]; 19105244eac9SBenno Rice phys_avail[j + 1] = phys_avail[j - 1]; 19115244eac9SBenno Rice } 19125244eac9SBenno Rice 19135244eac9SBenno Rice phys_avail[i + 3] = phys_avail[i + 1]; 19145244eac9SBenno Rice phys_avail[i + 1] = s; 19155244eac9SBenno Rice phys_avail[i + 2] = e; 19165244eac9SBenno Rice phys_avail_count++; 19175244eac9SBenno Rice } 19185244eac9SBenno Rice 19195244eac9SBenno Rice return (s); 19205244eac9SBenno Rice } 192159276937SPeter Grehan panic("moea_bootstrap_alloc: could not allocate memory"); 19225244eac9SBenno Rice } 19235244eac9SBenno Rice 19245244eac9SBenno Rice static void 192559276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len) 19265244eac9SBenno Rice { 19275244eac9SBenno Rice __syncicache((void *)pa, len); 19285244eac9SBenno Rice } 19295244eac9SBenno Rice 19305244eac9SBenno Rice static int 193159276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head, 19325244eac9SBenno Rice vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags) 19335244eac9SBenno Rice { 19345244eac9SBenno Rice struct pvo_entry *pvo; 19355244eac9SBenno Rice u_int sr; 19365244eac9SBenno Rice int first; 19375244eac9SBenno Rice u_int ptegidx; 19385244eac9SBenno Rice int i; 193932bc7846SPeter Grehan int bootstrap; 19405244eac9SBenno Rice 194159276937SPeter Grehan moea_pvo_enter_calls++; 19428207b362SBenno Rice first = 0; 194332bc7846SPeter Grehan bootstrap = 0; 194432bc7846SPeter Grehan 19455244eac9SBenno Rice /* 19465244eac9SBenno Rice * Compute the PTE Group index. 19475244eac9SBenno Rice */ 19485244eac9SBenno Rice va &= ~ADDR_POFF; 19495244eac9SBenno Rice sr = va_to_sr(pm->pm_sr, va); 19505244eac9SBenno Rice ptegidx = va_to_pteg(sr, va); 19515244eac9SBenno Rice 19525244eac9SBenno Rice /* 19535244eac9SBenno Rice * Remove any existing mapping for this page. Reuse the pvo entry if 19545244eac9SBenno Rice * there is a mapping. 19555244eac9SBenno Rice */ 195659276937SPeter Grehan mtx_lock(&moea_table_mutex); 195759276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) { 19585244eac9SBenno Rice if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) { 195952a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa && 196052a7870dSNathan Whitehorn (pvo->pvo_pte.pte.pte_lo & PTE_PP) == 1961fafc7362SBenno Rice (pte_lo & PTE_PP)) { 196259276937SPeter Grehan mtx_unlock(&moea_table_mutex); 196349f8f727SBenno Rice return (0); 1964fafc7362SBenno Rice } 196559276937SPeter Grehan moea_pvo_remove(pvo, -1); 19665244eac9SBenno Rice break; 19675244eac9SBenno Rice } 19685244eac9SBenno Rice } 19695244eac9SBenno Rice 19705244eac9SBenno Rice /* 19715244eac9SBenno Rice * If we aren't overwriting a mapping, try to allocate. 19725244eac9SBenno Rice */ 197359276937SPeter Grehan if (moea_initialized) { 1974378862a7SJeff Roberson pvo = uma_zalloc(zone, M_NOWAIT); 197549f8f727SBenno Rice } else { 197659276937SPeter Grehan if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) { 197759276937SPeter Grehan panic("moea_enter: bpvo pool exhausted, %d, %d, %d", 197859276937SPeter Grehan moea_bpvo_pool_index, BPVO_POOL_SIZE, 19790d290675SBenno Rice BPVO_POOL_SIZE * sizeof(struct pvo_entry)); 198049f8f727SBenno Rice } 198159276937SPeter Grehan pvo = &moea_bpvo_pool[moea_bpvo_pool_index]; 198259276937SPeter Grehan moea_bpvo_pool_index++; 198332bc7846SPeter Grehan bootstrap = 1; 198449f8f727SBenno Rice } 19855244eac9SBenno Rice 19865244eac9SBenno Rice if (pvo == NULL) { 198759276937SPeter Grehan mtx_unlock(&moea_table_mutex); 19885244eac9SBenno Rice return (ENOMEM); 19895244eac9SBenno Rice } 19905244eac9SBenno Rice 199159276937SPeter Grehan moea_pvo_entries++; 19925244eac9SBenno Rice pvo->pvo_vaddr = va; 19935244eac9SBenno Rice pvo->pvo_pmap = pm; 199459276937SPeter Grehan LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink); 19955244eac9SBenno Rice pvo->pvo_vaddr &= ~ADDR_POFF; 19965244eac9SBenno Rice if (flags & VM_PROT_EXECUTE) 19975244eac9SBenno Rice pvo->pvo_vaddr |= PVO_EXECUTABLE; 19985244eac9SBenno Rice if (flags & PVO_WIRED) 19995244eac9SBenno Rice pvo->pvo_vaddr |= PVO_WIRED; 200059276937SPeter Grehan if (pvo_head != &moea_pvo_kunmanaged) 20015244eac9SBenno Rice pvo->pvo_vaddr |= PVO_MANAGED; 200232bc7846SPeter Grehan if (bootstrap) 200332bc7846SPeter Grehan pvo->pvo_vaddr |= PVO_BOOTSTRAP; 20044dba5df1SPeter Grehan 200552a7870dSNathan Whitehorn moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo); 20065244eac9SBenno Rice 20075244eac9SBenno Rice /* 2008598d99ddSNathan Whitehorn * Add to pmap list 2009598d99ddSNathan Whitehorn */ 2010ccc4a5c7SNathan Whitehorn RB_INSERT(pvo_tree, &pm->pmap_pvo, pvo); 2011598d99ddSNathan Whitehorn 2012598d99ddSNathan Whitehorn /* 20135244eac9SBenno Rice * Remember if the list was empty and therefore will be the first 20145244eac9SBenno Rice * item. 20155244eac9SBenno Rice */ 20168207b362SBenno Rice if (LIST_FIRST(pvo_head) == NULL) 20178207b362SBenno Rice first = 1; 20185244eac9SBenno Rice LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink); 20194dba5df1SPeter Grehan 202052a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED) 2021c3d11d22SAlan Cox pm->pm_stats.wired_count++; 2022c3d11d22SAlan Cox pm->pm_stats.resident_count++; 20235244eac9SBenno Rice 20245244eac9SBenno Rice /* 20255244eac9SBenno Rice * We hope this succeeds but it isn't required. 20265244eac9SBenno Rice */ 202752a7870dSNathan Whitehorn i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte); 20285244eac9SBenno Rice if (i >= 0) { 20295244eac9SBenno Rice PVO_PTEGIDX_SET(pvo, i); 20305244eac9SBenno Rice } else { 203159276937SPeter Grehan panic("moea_pvo_enter: overflow"); 203259276937SPeter Grehan moea_pte_overflow++; 20335244eac9SBenno Rice } 203459276937SPeter Grehan mtx_unlock(&moea_table_mutex); 20354dba5df1SPeter Grehan 20365244eac9SBenno Rice return (first ? ENOENT : 0); 20375244eac9SBenno Rice } 20385244eac9SBenno Rice 20395244eac9SBenno Rice static void 204059276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx) 20415244eac9SBenno Rice { 20425244eac9SBenno Rice struct pte *pt; 20435244eac9SBenno Rice 20445244eac9SBenno Rice /* 20455244eac9SBenno Rice * If there is an active pte entry, we need to deactivate it (and 20465244eac9SBenno Rice * save the ref & cfg bits). 20475244eac9SBenno Rice */ 204859276937SPeter Grehan pt = moea_pvo_to_pte(pvo, pteidx); 20495244eac9SBenno Rice if (pt != NULL) { 205052a7870dSNathan Whitehorn moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr); 2051d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 20525244eac9SBenno Rice PVO_PTEGIDX_CLR(pvo); 20535244eac9SBenno Rice } else { 205459276937SPeter Grehan moea_pte_overflow--; 20555244eac9SBenno Rice } 20565244eac9SBenno Rice 20575244eac9SBenno Rice /* 20585244eac9SBenno Rice * Update our statistics. 20595244eac9SBenno Rice */ 20605244eac9SBenno Rice pvo->pvo_pmap->pm_stats.resident_count--; 206152a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED) 20625244eac9SBenno Rice pvo->pvo_pmap->pm_stats.wired_count--; 20635244eac9SBenno Rice 20645244eac9SBenno Rice /* 20655244eac9SBenno Rice * Save the REF/CHG bits into their cache if the page is managed. 20665244eac9SBenno Rice */ 2067d98d0ce2SKonstantin Belousov if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED) { 20685244eac9SBenno Rice struct vm_page *pg; 20695244eac9SBenno Rice 207052a7870dSNathan Whitehorn pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN); 20715244eac9SBenno Rice if (pg != NULL) { 207252a7870dSNathan Whitehorn moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo & 20735244eac9SBenno Rice (PTE_REF | PTE_CHG)); 20745244eac9SBenno Rice } 20755244eac9SBenno Rice } 20765244eac9SBenno Rice 20775244eac9SBenno Rice /* 2078598d99ddSNathan Whitehorn * Remove this PVO from the PV and pmap lists. 20795244eac9SBenno Rice */ 20805244eac9SBenno Rice LIST_REMOVE(pvo, pvo_vlink); 2081ccc4a5c7SNathan Whitehorn RB_REMOVE(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo); 20825244eac9SBenno Rice 20835244eac9SBenno Rice /* 20845244eac9SBenno Rice * Remove this from the overflow list and return it to the pool 20855244eac9SBenno Rice * if we aren't going to reuse it. 20865244eac9SBenno Rice */ 20875244eac9SBenno Rice LIST_REMOVE(pvo, pvo_olink); 208849f8f727SBenno Rice if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP)) 208959276937SPeter Grehan uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone : 209059276937SPeter Grehan moea_upvo_zone, pvo); 209159276937SPeter Grehan moea_pvo_entries--; 209259276937SPeter Grehan moea_pvo_remove_calls++; 20935244eac9SBenno Rice } 20945244eac9SBenno Rice 20955244eac9SBenno Rice static __inline int 209659276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx) 20975244eac9SBenno Rice { 20985244eac9SBenno Rice int pteidx; 20995244eac9SBenno Rice 21005244eac9SBenno Rice /* 21015244eac9SBenno Rice * We can find the actual pte entry without searching by grabbing 21025244eac9SBenno Rice * the PTEG index from 3 unused bits in pte_lo[11:9] and by 21035244eac9SBenno Rice * noticing the HID bit. 21045244eac9SBenno Rice */ 21055244eac9SBenno Rice pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo); 210652a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_hi & PTE_HID) 210759276937SPeter Grehan pteidx ^= moea_pteg_mask * 8; 21085244eac9SBenno Rice 21095244eac9SBenno Rice return (pteidx); 21105244eac9SBenno Rice } 21115244eac9SBenno Rice 21125244eac9SBenno Rice static struct pvo_entry * 211359276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p) 21145244eac9SBenno Rice { 21155244eac9SBenno Rice struct pvo_entry *pvo; 21165244eac9SBenno Rice int ptegidx; 21175244eac9SBenno Rice u_int sr; 21185244eac9SBenno Rice 21195244eac9SBenno Rice va &= ~ADDR_POFF; 21205244eac9SBenno Rice sr = va_to_sr(pm->pm_sr, va); 21215244eac9SBenno Rice ptegidx = va_to_pteg(sr, va); 21225244eac9SBenno Rice 212359276937SPeter Grehan mtx_lock(&moea_table_mutex); 212459276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) { 21255244eac9SBenno Rice if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) { 21265244eac9SBenno Rice if (pteidx_p) 212759276937SPeter Grehan *pteidx_p = moea_pvo_pte_index(pvo, ptegidx); 2128f489bf21SAlan Cox break; 21295244eac9SBenno Rice } 21305244eac9SBenno Rice } 213159276937SPeter Grehan mtx_unlock(&moea_table_mutex); 21325244eac9SBenno Rice 2133f489bf21SAlan Cox return (pvo); 21345244eac9SBenno Rice } 21355244eac9SBenno Rice 21365244eac9SBenno Rice static struct pte * 213759276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx) 21385244eac9SBenno Rice { 21395244eac9SBenno Rice struct pte *pt; 21405244eac9SBenno Rice 21415244eac9SBenno Rice /* 21425244eac9SBenno Rice * If we haven't been supplied the ptegidx, calculate it. 21435244eac9SBenno Rice */ 21445244eac9SBenno Rice if (pteidx == -1) { 21455244eac9SBenno Rice int ptegidx; 21465244eac9SBenno Rice u_int sr; 21475244eac9SBenno Rice 21485244eac9SBenno Rice sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr); 21495244eac9SBenno Rice ptegidx = va_to_pteg(sr, pvo->pvo_vaddr); 215059276937SPeter Grehan pteidx = moea_pvo_pte_index(pvo, ptegidx); 21515244eac9SBenno Rice } 21525244eac9SBenno Rice 215359276937SPeter Grehan pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7]; 2154d644a0b7SAlan Cox mtx_lock(&moea_table_mutex); 21555244eac9SBenno Rice 215652a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) { 215759276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no " 21585244eac9SBenno Rice "valid pte index", pvo); 21595244eac9SBenno Rice } 21605244eac9SBenno Rice 216152a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) { 216259276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo " 21635244eac9SBenno Rice "pvo but no valid pte", pvo); 21645244eac9SBenno Rice } 21655244eac9SBenno Rice 216652a7870dSNathan Whitehorn if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) { 216752a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) { 216859276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has valid pte in " 216959276937SPeter Grehan "moea_pteg_table %p but invalid in pvo", pvo, pt); 21705244eac9SBenno Rice } 21715244eac9SBenno Rice 217252a7870dSNathan Whitehorn if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF)) 21735244eac9SBenno Rice != 0) { 217459276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p pte does not match " 217559276937SPeter Grehan "pte %p in moea_pteg_table", pvo, pt); 21765244eac9SBenno Rice } 21775244eac9SBenno Rice 2178d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 21795244eac9SBenno Rice return (pt); 21805244eac9SBenno Rice } 21815244eac9SBenno Rice 218252a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) { 218359276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has invalid pte %p in " 218459276937SPeter Grehan "moea_pteg_table but valid in pvo", pvo, pt); 21855244eac9SBenno Rice } 21865244eac9SBenno Rice 2187d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 21885244eac9SBenno Rice return (NULL); 21895244eac9SBenno Rice } 21905244eac9SBenno Rice 21915244eac9SBenno Rice /* 21925244eac9SBenno Rice * XXX: THIS STUFF SHOULD BE IN pte.c? 21935244eac9SBenno Rice */ 21945244eac9SBenno Rice int 219559276937SPeter Grehan moea_pte_spill(vm_offset_t addr) 21965244eac9SBenno Rice { 21975244eac9SBenno Rice struct pvo_entry *source_pvo, *victim_pvo; 21985244eac9SBenno Rice struct pvo_entry *pvo; 21995244eac9SBenno Rice int ptegidx, i, j; 22005244eac9SBenno Rice u_int sr; 22015244eac9SBenno Rice struct pteg *pteg; 22025244eac9SBenno Rice struct pte *pt; 22035244eac9SBenno Rice 220459276937SPeter Grehan moea_pte_spills++; 22055244eac9SBenno Rice 2206d080d5fdSBenno Rice sr = mfsrin(addr); 22075244eac9SBenno Rice ptegidx = va_to_pteg(sr, addr); 22085244eac9SBenno Rice 22095244eac9SBenno Rice /* 22105244eac9SBenno Rice * Have to substitute some entry. Use the primary hash for this. 22115244eac9SBenno Rice * Use low bits of timebase as random generator. 22125244eac9SBenno Rice */ 221359276937SPeter Grehan pteg = &moea_pteg_table[ptegidx]; 221459276937SPeter Grehan mtx_lock(&moea_table_mutex); 22155244eac9SBenno Rice __asm __volatile("mftb %0" : "=r"(i)); 22165244eac9SBenno Rice i &= 7; 22175244eac9SBenno Rice pt = &pteg->pt[i]; 22185244eac9SBenno Rice 22195244eac9SBenno Rice source_pvo = NULL; 22205244eac9SBenno Rice victim_pvo = NULL; 222159276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) { 22225244eac9SBenno Rice /* 22235244eac9SBenno Rice * We need to find a pvo entry for this address. 22245244eac9SBenno Rice */ 22255244eac9SBenno Rice if (source_pvo == NULL && 222652a7870dSNathan Whitehorn moea_pte_match(&pvo->pvo_pte.pte, sr, addr, 222752a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_hi & PTE_HID)) { 22285244eac9SBenno Rice /* 22295244eac9SBenno Rice * Now found an entry to be spilled into the pteg. 22305244eac9SBenno Rice * The PTE is now valid, so we know it's active. 22315244eac9SBenno Rice */ 223252a7870dSNathan Whitehorn j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte); 22335244eac9SBenno Rice 22345244eac9SBenno Rice if (j >= 0) { 22355244eac9SBenno Rice PVO_PTEGIDX_SET(pvo, j); 223659276937SPeter Grehan moea_pte_overflow--; 223759276937SPeter Grehan mtx_unlock(&moea_table_mutex); 22385244eac9SBenno Rice return (1); 22395244eac9SBenno Rice } 22405244eac9SBenno Rice 22415244eac9SBenno Rice source_pvo = pvo; 22425244eac9SBenno Rice 22435244eac9SBenno Rice if (victim_pvo != NULL) 22445244eac9SBenno Rice break; 22455244eac9SBenno Rice } 22465244eac9SBenno Rice 22475244eac9SBenno Rice /* 22485244eac9SBenno Rice * We also need the pvo entry of the victim we are replacing 22495244eac9SBenno Rice * so save the R & C bits of the PTE. 22505244eac9SBenno Rice */ 22515244eac9SBenno Rice if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL && 225252a7870dSNathan Whitehorn moea_pte_compare(pt, &pvo->pvo_pte.pte)) { 22535244eac9SBenno Rice victim_pvo = pvo; 22545244eac9SBenno Rice if (source_pvo != NULL) 22555244eac9SBenno Rice break; 22565244eac9SBenno Rice } 22575244eac9SBenno Rice } 22585244eac9SBenno Rice 2259f489bf21SAlan Cox if (source_pvo == NULL) { 226059276937SPeter Grehan mtx_unlock(&moea_table_mutex); 22615244eac9SBenno Rice return (0); 2262f489bf21SAlan Cox } 22635244eac9SBenno Rice 22645244eac9SBenno Rice if (victim_pvo == NULL) { 22655244eac9SBenno Rice if ((pt->pte_hi & PTE_HID) == 0) 226659276937SPeter Grehan panic("moea_pte_spill: victim p-pte (%p) has no pvo" 22675244eac9SBenno Rice "entry", pt); 22685244eac9SBenno Rice 22695244eac9SBenno Rice /* 22705244eac9SBenno Rice * If this is a secondary PTE, we need to search it's primary 22715244eac9SBenno Rice * pvo bucket for the matching PVO. 22725244eac9SBenno Rice */ 227359276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask], 22745244eac9SBenno Rice pvo_olink) { 22755244eac9SBenno Rice /* 22765244eac9SBenno Rice * We also need the pvo entry of the victim we are 22775244eac9SBenno Rice * replacing so save the R & C bits of the PTE. 22785244eac9SBenno Rice */ 227952a7870dSNathan Whitehorn if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) { 22805244eac9SBenno Rice victim_pvo = pvo; 22815244eac9SBenno Rice break; 22825244eac9SBenno Rice } 22835244eac9SBenno Rice } 22845244eac9SBenno Rice 22855244eac9SBenno Rice if (victim_pvo == NULL) 228659276937SPeter Grehan panic("moea_pte_spill: victim s-pte (%p) has no pvo" 22875244eac9SBenno Rice "entry", pt); 22885244eac9SBenno Rice } 22895244eac9SBenno Rice 22905244eac9SBenno Rice /* 22915244eac9SBenno Rice * We are invalidating the TLB entry for the EA we are replacing even 22925244eac9SBenno Rice * though it's valid. If we don't, we lose any ref/chg bit changes 22935244eac9SBenno Rice * contained in the TLB entry. 22945244eac9SBenno Rice */ 229552a7870dSNathan Whitehorn source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID; 22965244eac9SBenno Rice 229752a7870dSNathan Whitehorn moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr); 229852a7870dSNathan Whitehorn moea_pte_set(pt, &source_pvo->pvo_pte.pte); 22995244eac9SBenno Rice 23005244eac9SBenno Rice PVO_PTEGIDX_CLR(victim_pvo); 23015244eac9SBenno Rice PVO_PTEGIDX_SET(source_pvo, i); 230259276937SPeter Grehan moea_pte_replacements++; 23035244eac9SBenno Rice 230459276937SPeter Grehan mtx_unlock(&moea_table_mutex); 23055244eac9SBenno Rice return (1); 23065244eac9SBenno Rice } 23075244eac9SBenno Rice 23085244eac9SBenno Rice static int 230959276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt) 23105244eac9SBenno Rice { 23115244eac9SBenno Rice struct pte *pt; 23125244eac9SBenno Rice int i; 23135244eac9SBenno Rice 2314d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 2315d644a0b7SAlan Cox 23165244eac9SBenno Rice /* 23175244eac9SBenno Rice * First try primary hash. 23185244eac9SBenno Rice */ 231959276937SPeter Grehan for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) { 23205244eac9SBenno Rice if ((pt->pte_hi & PTE_VALID) == 0) { 23215244eac9SBenno Rice pvo_pt->pte_hi &= ~PTE_HID; 232259276937SPeter Grehan moea_pte_set(pt, pvo_pt); 23235244eac9SBenno Rice return (i); 23245244eac9SBenno Rice } 23255244eac9SBenno Rice } 23265244eac9SBenno Rice 23275244eac9SBenno Rice /* 23285244eac9SBenno Rice * Now try secondary hash. 23295244eac9SBenno Rice */ 233059276937SPeter Grehan ptegidx ^= moea_pteg_mask; 2331bd8e6f87SPeter Grehan 233259276937SPeter Grehan for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) { 23335244eac9SBenno Rice if ((pt->pte_hi & PTE_VALID) == 0) { 23345244eac9SBenno Rice pvo_pt->pte_hi |= PTE_HID; 233559276937SPeter Grehan moea_pte_set(pt, pvo_pt); 23365244eac9SBenno Rice return (i); 23375244eac9SBenno Rice } 23385244eac9SBenno Rice } 23395244eac9SBenno Rice 234059276937SPeter Grehan panic("moea_pte_insert: overflow"); 23415244eac9SBenno Rice return (-1); 23425244eac9SBenno Rice } 23435244eac9SBenno Rice 23445244eac9SBenno Rice static boolean_t 234559276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit) 23465244eac9SBenno Rice { 23475244eac9SBenno Rice struct pvo_entry *pvo; 23485244eac9SBenno Rice struct pte *pt; 23495244eac9SBenno Rice 23508d9e6d9fSAlan Cox rw_assert(&pvh_global_lock, RA_WLOCKED); 235159276937SPeter Grehan if (moea_attr_fetch(m) & ptebit) 23525244eac9SBenno Rice return (TRUE); 23535244eac9SBenno Rice 23545244eac9SBenno Rice LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 23555244eac9SBenno Rice 23565244eac9SBenno Rice /* 23575244eac9SBenno Rice * See if we saved the bit off. If so, cache it and return 23585244eac9SBenno Rice * success. 23595244eac9SBenno Rice */ 236052a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & ptebit) { 236159276937SPeter Grehan moea_attr_save(m, ptebit); 23625244eac9SBenno Rice return (TRUE); 23635244eac9SBenno Rice } 23645244eac9SBenno Rice } 23655244eac9SBenno Rice 23665244eac9SBenno Rice /* 23675244eac9SBenno Rice * No luck, now go through the hard part of looking at the PTEs 23685244eac9SBenno Rice * themselves. Sync so that any pending REF/CHG bits are flushed to 23695244eac9SBenno Rice * the PTEs. 23705244eac9SBenno Rice */ 2371e4f72b32SMarcel Moolenaar powerpc_sync(); 23725244eac9SBenno Rice LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 23735244eac9SBenno Rice 23745244eac9SBenno Rice /* 23755244eac9SBenno Rice * See if this pvo has a valid PTE. if so, fetch the 23765244eac9SBenno Rice * REF/CHG bits from the valid PTE. If the appropriate 23775244eac9SBenno Rice * ptebit is set, cache it and return success. 23785244eac9SBenno Rice */ 237959276937SPeter Grehan pt = moea_pvo_to_pte(pvo, -1); 23805244eac9SBenno Rice if (pt != NULL) { 238152a7870dSNathan Whitehorn moea_pte_synch(pt, &pvo->pvo_pte.pte); 2382d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 238352a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & ptebit) { 238459276937SPeter Grehan moea_attr_save(m, ptebit); 23855244eac9SBenno Rice return (TRUE); 23865244eac9SBenno Rice } 23875244eac9SBenno Rice } 23885244eac9SBenno Rice } 23895244eac9SBenno Rice 23904f7daed0SAndrew Gallatin return (FALSE); 23915244eac9SBenno Rice } 23925244eac9SBenno Rice 239303b6e025SPeter Grehan static u_int 2394ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit) 23955244eac9SBenno Rice { 239603b6e025SPeter Grehan u_int count; 23975244eac9SBenno Rice struct pvo_entry *pvo; 23985244eac9SBenno Rice struct pte *pt; 2399ce186587SAlan Cox 24008d9e6d9fSAlan Cox rw_assert(&pvh_global_lock, RA_WLOCKED); 24015244eac9SBenno Rice 24025244eac9SBenno Rice /* 24035244eac9SBenno Rice * Clear the cached value. 24045244eac9SBenno Rice */ 240559276937SPeter Grehan moea_attr_clear(m, ptebit); 24065244eac9SBenno Rice 24075244eac9SBenno Rice /* 24085244eac9SBenno Rice * Sync so that any pending REF/CHG bits are flushed to the PTEs (so 24095244eac9SBenno Rice * we can reset the right ones). note that since the pvo entries and 24105244eac9SBenno Rice * list heads are accessed via BAT0 and are never placed in the page 24115244eac9SBenno Rice * table, we don't have to worry about further accesses setting the 24125244eac9SBenno Rice * REF/CHG bits. 24135244eac9SBenno Rice */ 2414e4f72b32SMarcel Moolenaar powerpc_sync(); 24155244eac9SBenno Rice 24165244eac9SBenno Rice /* 24175244eac9SBenno Rice * For each pvo entry, clear the pvo's ptebit. If this pvo has a 24185244eac9SBenno Rice * valid pte clear the ptebit from the valid pte. 24195244eac9SBenno Rice */ 242003b6e025SPeter Grehan count = 0; 24215244eac9SBenno Rice LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 242259276937SPeter Grehan pt = moea_pvo_to_pte(pvo, -1); 24235244eac9SBenno Rice if (pt != NULL) { 242452a7870dSNathan Whitehorn moea_pte_synch(pt, &pvo->pvo_pte.pte); 242552a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & ptebit) { 242603b6e025SPeter Grehan count++; 242759276937SPeter Grehan moea_pte_clear(pt, PVO_VADDR(pvo), ptebit); 24285244eac9SBenno Rice } 2429d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 243003b6e025SPeter Grehan } 243152a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~ptebit; 24325244eac9SBenno Rice } 24335244eac9SBenno Rice 243403b6e025SPeter Grehan return (count); 2435bdf71f56SBenno Rice } 24368bbfa33aSBenno Rice 24378bbfa33aSBenno Rice /* 243832bc7846SPeter Grehan * Return true if the physical range is encompassed by the battable[idx] 243932bc7846SPeter Grehan */ 244032bc7846SPeter Grehan static int 244159276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size) 244232bc7846SPeter Grehan { 244332bc7846SPeter Grehan u_int prot; 244432bc7846SPeter Grehan u_int32_t start; 244532bc7846SPeter Grehan u_int32_t end; 244632bc7846SPeter Grehan u_int32_t bat_ble; 244732bc7846SPeter Grehan 244832bc7846SPeter Grehan /* 244932bc7846SPeter Grehan * Return immediately if not a valid mapping 245032bc7846SPeter Grehan */ 2451c4bcebedSNathan Whitehorn if (!(battable[idx].batu & BAT_Vs)) 245232bc7846SPeter Grehan return (EINVAL); 245332bc7846SPeter Grehan 245432bc7846SPeter Grehan /* 245532bc7846SPeter Grehan * The BAT entry must be cache-inhibited, guarded, and r/w 245632bc7846SPeter Grehan * so it can function as an i/o page 245732bc7846SPeter Grehan */ 245832bc7846SPeter Grehan prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW); 245932bc7846SPeter Grehan if (prot != (BAT_I|BAT_G|BAT_PP_RW)) 246032bc7846SPeter Grehan return (EPERM); 246132bc7846SPeter Grehan 246232bc7846SPeter Grehan /* 246332bc7846SPeter Grehan * The address should be within the BAT range. Assume that the 246432bc7846SPeter Grehan * start address in the BAT has the correct alignment (thus 246532bc7846SPeter Grehan * not requiring masking) 246632bc7846SPeter Grehan */ 246732bc7846SPeter Grehan start = battable[idx].batl & BAT_PBS; 246832bc7846SPeter Grehan bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03; 246932bc7846SPeter Grehan end = start | (bat_ble << 15) | 0x7fff; 247032bc7846SPeter Grehan 247132bc7846SPeter Grehan if ((pa < start) || ((pa + size) > end)) 247232bc7846SPeter Grehan return (ERANGE); 247332bc7846SPeter Grehan 247432bc7846SPeter Grehan return (0); 247532bc7846SPeter Grehan } 247632bc7846SPeter Grehan 247759276937SPeter Grehan boolean_t 247820b79612SRafal Jaworowski moea_dev_direct_mapped(mmu_t mmu, vm_paddr_t pa, vm_size_t size) 2479c0763d37SSuleiman Souhlal { 2480c0763d37SSuleiman Souhlal int i; 2481c0763d37SSuleiman Souhlal 2482c0763d37SSuleiman Souhlal /* 2483c0763d37SSuleiman Souhlal * This currently does not work for entries that 2484c0763d37SSuleiman Souhlal * overlap 256M BAT segments. 2485c0763d37SSuleiman Souhlal */ 2486c0763d37SSuleiman Souhlal 2487c0763d37SSuleiman Souhlal for(i = 0; i < 16; i++) 248859276937SPeter Grehan if (moea_bat_mapped(i, pa, size) == 0) 2489c0763d37SSuleiman Souhlal return (0); 2490c0763d37SSuleiman Souhlal 2491c0763d37SSuleiman Souhlal return (EFAULT); 2492c0763d37SSuleiman Souhlal } 249332bc7846SPeter Grehan 249432bc7846SPeter Grehan /* 24958bbfa33aSBenno Rice * Map a set of physical memory pages into the kernel virtual 24968bbfa33aSBenno Rice * address space. Return a pointer to where it is mapped. This 24978bbfa33aSBenno Rice * routine is intended to be used for mapping device memory, 24988bbfa33aSBenno Rice * NOT real memory. 24998bbfa33aSBenno Rice */ 25008bbfa33aSBenno Rice void * 250120b79612SRafal Jaworowski moea_mapdev(mmu_t mmu, vm_paddr_t pa, vm_size_t size) 25028bbfa33aSBenno Rice { 2503c1f4123bSNathan Whitehorn 2504c1f4123bSNathan Whitehorn return (moea_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT)); 2505c1f4123bSNathan Whitehorn } 2506c1f4123bSNathan Whitehorn 2507c1f4123bSNathan Whitehorn void * 2508c1f4123bSNathan Whitehorn moea_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma) 2509c1f4123bSNathan Whitehorn { 251032bc7846SPeter Grehan vm_offset_t va, tmpva, ppa, offset; 251132bc7846SPeter Grehan int i; 25128bbfa33aSBenno Rice 251332bc7846SPeter Grehan ppa = trunc_page(pa); 25148bbfa33aSBenno Rice offset = pa & PAGE_MASK; 25158bbfa33aSBenno Rice size = roundup(offset + size, PAGE_SIZE); 25168bbfa33aSBenno Rice 251732bc7846SPeter Grehan /* 251832bc7846SPeter Grehan * If the physical address lies within a valid BAT table entry, 251932bc7846SPeter Grehan * return the 1:1 mapping. This currently doesn't work 252032bc7846SPeter Grehan * for regions that overlap 256M BAT segments. 252132bc7846SPeter Grehan */ 252232bc7846SPeter Grehan for (i = 0; i < 16; i++) { 252359276937SPeter Grehan if (moea_bat_mapped(i, pa, size) == 0) 252432bc7846SPeter Grehan return ((void *) pa); 252532bc7846SPeter Grehan } 252632bc7846SPeter Grehan 2527e53f32acSAlan Cox va = kmem_alloc_nofault(kernel_map, size); 25288bbfa33aSBenno Rice if (!va) 252959276937SPeter Grehan panic("moea_mapdev: Couldn't alloc kernel virtual memory"); 25308bbfa33aSBenno Rice 25318bbfa33aSBenno Rice for (tmpva = va; size > 0;) { 2532c1f4123bSNathan Whitehorn moea_kenter_attr(mmu, tmpva, ppa, ma); 2533e4f72b32SMarcel Moolenaar tlbie(tmpva); 25348bbfa33aSBenno Rice size -= PAGE_SIZE; 25358bbfa33aSBenno Rice tmpva += PAGE_SIZE; 253632bc7846SPeter Grehan ppa += PAGE_SIZE; 25378bbfa33aSBenno Rice } 25388bbfa33aSBenno Rice 25398bbfa33aSBenno Rice return ((void *)(va + offset)); 25408bbfa33aSBenno Rice } 25418bbfa33aSBenno Rice 25428bbfa33aSBenno Rice void 254359276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size) 25448bbfa33aSBenno Rice { 25458bbfa33aSBenno Rice vm_offset_t base, offset; 25468bbfa33aSBenno Rice 254732bc7846SPeter Grehan /* 254832bc7846SPeter Grehan * If this is outside kernel virtual space, then it's a 254932bc7846SPeter Grehan * battable entry and doesn't require unmapping 255032bc7846SPeter Grehan */ 2551ab739706SNathan Whitehorn if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) { 25528bbfa33aSBenno Rice base = trunc_page(va); 25538bbfa33aSBenno Rice offset = va & PAGE_MASK; 25548bbfa33aSBenno Rice size = roundup(offset + size, PAGE_SIZE); 25558bbfa33aSBenno Rice kmem_free(kernel_map, base, size); 25568bbfa33aSBenno Rice } 255732bc7846SPeter Grehan } 25581a4fcaebSMarcel Moolenaar 25591a4fcaebSMarcel Moolenaar static void 25601a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz) 25611a4fcaebSMarcel Moolenaar { 25621a4fcaebSMarcel Moolenaar struct pvo_entry *pvo; 25631a4fcaebSMarcel Moolenaar vm_offset_t lim; 25641a4fcaebSMarcel Moolenaar vm_paddr_t pa; 25651a4fcaebSMarcel Moolenaar vm_size_t len; 25661a4fcaebSMarcel Moolenaar 25671a4fcaebSMarcel Moolenaar PMAP_LOCK(pm); 25681a4fcaebSMarcel Moolenaar while (sz > 0) { 25691a4fcaebSMarcel Moolenaar lim = round_page(va); 25701a4fcaebSMarcel Moolenaar len = MIN(lim - va, sz); 25711a4fcaebSMarcel Moolenaar pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL); 25721a4fcaebSMarcel Moolenaar if (pvo != NULL) { 25731a4fcaebSMarcel Moolenaar pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | 25741a4fcaebSMarcel Moolenaar (va & ADDR_POFF); 25751a4fcaebSMarcel Moolenaar moea_syncicache(pa, len); 25761a4fcaebSMarcel Moolenaar } 25771a4fcaebSMarcel Moolenaar va += len; 25781a4fcaebSMarcel Moolenaar sz -= len; 25791a4fcaebSMarcel Moolenaar } 25801a4fcaebSMarcel Moolenaar PMAP_UNLOCK(pm); 25811a4fcaebSMarcel Moolenaar } 2582afd9cb6cSJustin Hibbits 2583afd9cb6cSJustin Hibbits vm_offset_t 2584afd9cb6cSJustin Hibbits moea_dumpsys_map(mmu_t mmu, struct pmap_md *md, vm_size_t ofs, 2585afd9cb6cSJustin Hibbits vm_size_t *sz) 2586afd9cb6cSJustin Hibbits { 2587afd9cb6cSJustin Hibbits if (md->md_vaddr == ~0UL) 2588afd9cb6cSJustin Hibbits return (md->md_paddr + ofs); 2589afd9cb6cSJustin Hibbits else 2590afd9cb6cSJustin Hibbits return (md->md_vaddr + ofs); 2591afd9cb6cSJustin Hibbits } 2592afd9cb6cSJustin Hibbits 2593afd9cb6cSJustin Hibbits struct pmap_md * 2594afd9cb6cSJustin Hibbits moea_scan_md(mmu_t mmu, struct pmap_md *prev) 2595afd9cb6cSJustin Hibbits { 2596afd9cb6cSJustin Hibbits static struct pmap_md md; 2597afd9cb6cSJustin Hibbits struct pvo_entry *pvo; 2598afd9cb6cSJustin Hibbits vm_offset_t va; 2599afd9cb6cSJustin Hibbits 2600afd9cb6cSJustin Hibbits if (dumpsys_minidump) { 2601afd9cb6cSJustin Hibbits md.md_paddr = ~0UL; /* Minidumps use virtual addresses. */ 2602afd9cb6cSJustin Hibbits if (prev == NULL) { 2603afd9cb6cSJustin Hibbits /* 1st: kernel .data and .bss. */ 2604afd9cb6cSJustin Hibbits md.md_index = 1; 2605afd9cb6cSJustin Hibbits md.md_vaddr = trunc_page((uintptr_t)_etext); 2606afd9cb6cSJustin Hibbits md.md_size = round_page((uintptr_t)_end) - md.md_vaddr; 2607afd9cb6cSJustin Hibbits return (&md); 2608afd9cb6cSJustin Hibbits } 2609afd9cb6cSJustin Hibbits switch (prev->md_index) { 2610afd9cb6cSJustin Hibbits case 1: 2611afd9cb6cSJustin Hibbits /* 2nd: msgbuf and tables (see pmap_bootstrap()). */ 2612afd9cb6cSJustin Hibbits md.md_index = 2; 2613afd9cb6cSJustin Hibbits md.md_vaddr = (vm_offset_t)msgbufp->msg_ptr; 2614afd9cb6cSJustin Hibbits md.md_size = round_page(msgbufp->msg_size); 2615afd9cb6cSJustin Hibbits break; 2616afd9cb6cSJustin Hibbits case 2: 2617afd9cb6cSJustin Hibbits /* 3rd: kernel VM. */ 2618afd9cb6cSJustin Hibbits va = prev->md_vaddr + prev->md_size; 2619afd9cb6cSJustin Hibbits /* Find start of next chunk (from va). */ 2620afd9cb6cSJustin Hibbits while (va < virtual_end) { 2621afd9cb6cSJustin Hibbits /* Don't dump the buffer cache. */ 2622afd9cb6cSJustin Hibbits if (va >= kmi.buffer_sva && 2623afd9cb6cSJustin Hibbits va < kmi.buffer_eva) { 2624afd9cb6cSJustin Hibbits va = kmi.buffer_eva; 2625afd9cb6cSJustin Hibbits continue; 2626afd9cb6cSJustin Hibbits } 2627afd9cb6cSJustin Hibbits pvo = moea_pvo_find_va(kernel_pmap, 2628afd9cb6cSJustin Hibbits va & ~ADDR_POFF, NULL); 2629afd9cb6cSJustin Hibbits if (pvo != NULL && 2630afd9cb6cSJustin Hibbits (pvo->pvo_pte.pte.pte_hi & PTE_VALID)) 2631afd9cb6cSJustin Hibbits break; 2632afd9cb6cSJustin Hibbits va += PAGE_SIZE; 2633afd9cb6cSJustin Hibbits } 2634afd9cb6cSJustin Hibbits if (va < virtual_end) { 2635afd9cb6cSJustin Hibbits md.md_vaddr = va; 2636afd9cb6cSJustin Hibbits va += PAGE_SIZE; 2637afd9cb6cSJustin Hibbits /* Find last page in chunk. */ 2638afd9cb6cSJustin Hibbits while (va < virtual_end) { 2639afd9cb6cSJustin Hibbits /* Don't run into the buffer cache. */ 2640afd9cb6cSJustin Hibbits if (va == kmi.buffer_sva) 2641afd9cb6cSJustin Hibbits break; 2642afd9cb6cSJustin Hibbits pvo = moea_pvo_find_va(kernel_pmap, 2643afd9cb6cSJustin Hibbits va & ~ADDR_POFF, NULL); 2644afd9cb6cSJustin Hibbits if (pvo == NULL || 2645afd9cb6cSJustin Hibbits !(pvo->pvo_pte.pte.pte_hi & PTE_VALID)) 2646afd9cb6cSJustin Hibbits break; 2647afd9cb6cSJustin Hibbits va += PAGE_SIZE; 2648afd9cb6cSJustin Hibbits } 2649afd9cb6cSJustin Hibbits md.md_size = va - md.md_vaddr; 2650afd9cb6cSJustin Hibbits break; 2651afd9cb6cSJustin Hibbits } 2652afd9cb6cSJustin Hibbits md.md_index = 3; 2653afd9cb6cSJustin Hibbits /* FALLTHROUGH */ 2654afd9cb6cSJustin Hibbits default: 2655afd9cb6cSJustin Hibbits return (NULL); 2656afd9cb6cSJustin Hibbits } 2657afd9cb6cSJustin Hibbits } else { /* minidumps */ 2658afd9cb6cSJustin Hibbits mem_regions(&pregions, &pregions_sz, 2659afd9cb6cSJustin Hibbits ®ions, ®ions_sz); 2660afd9cb6cSJustin Hibbits 2661afd9cb6cSJustin Hibbits if (prev == NULL) { 2662afd9cb6cSJustin Hibbits /* first physical chunk. */ 2663afd9cb6cSJustin Hibbits md.md_paddr = pregions[0].mr_start; 2664afd9cb6cSJustin Hibbits md.md_size = pregions[0].mr_size; 2665afd9cb6cSJustin Hibbits md.md_vaddr = ~0UL; 2666afd9cb6cSJustin Hibbits md.md_index = 1; 2667afd9cb6cSJustin Hibbits } else if (md.md_index < pregions_sz) { 2668afd9cb6cSJustin Hibbits md.md_paddr = pregions[md.md_index].mr_start; 2669afd9cb6cSJustin Hibbits md.md_size = pregions[md.md_index].mr_size; 2670afd9cb6cSJustin Hibbits md.md_vaddr = ~0UL; 2671afd9cb6cSJustin Hibbits md.md_index++; 2672afd9cb6cSJustin Hibbits } else { 2673afd9cb6cSJustin Hibbits /* There's no next physical chunk. */ 2674afd9cb6cSJustin Hibbits return (NULL); 2675afd9cb6cSJustin Hibbits } 2676afd9cb6cSJustin Hibbits } 2677afd9cb6cSJustin Hibbits 2678afd9cb6cSJustin Hibbits return (&md); 2679afd9cb6cSJustin Hibbits } 2680