160727d8bSWarner Losh /*- 25244eac9SBenno Rice * Copyright (c) 2001 The NetBSD Foundation, Inc. 35244eac9SBenno Rice * All rights reserved. 45244eac9SBenno Rice * 55244eac9SBenno Rice * This code is derived from software contributed to The NetBSD Foundation 65244eac9SBenno Rice * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc. 75244eac9SBenno Rice * 85244eac9SBenno Rice * Redistribution and use in source and binary forms, with or without 95244eac9SBenno Rice * modification, are permitted provided that the following conditions 105244eac9SBenno Rice * are met: 115244eac9SBenno Rice * 1. Redistributions of source code must retain the above copyright 125244eac9SBenno Rice * notice, this list of conditions and the following disclaimer. 135244eac9SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 145244eac9SBenno Rice * notice, this list of conditions and the following disclaimer in the 155244eac9SBenno Rice * documentation and/or other materials provided with the distribution. 165244eac9SBenno Rice * 3. All advertising materials mentioning features or use of this software 175244eac9SBenno Rice * must display the following acknowledgement: 185244eac9SBenno Rice * This product includes software developed by the NetBSD 195244eac9SBenno Rice * Foundation, Inc. and its contributors. 205244eac9SBenno Rice * 4. Neither the name of The NetBSD Foundation nor the names of its 215244eac9SBenno Rice * contributors may be used to endorse or promote products derived 225244eac9SBenno Rice * from this software without specific prior written permission. 235244eac9SBenno Rice * 245244eac9SBenno Rice * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 255244eac9SBenno Rice * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 265244eac9SBenno Rice * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 275244eac9SBenno Rice * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 285244eac9SBenno Rice * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 295244eac9SBenno Rice * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 305244eac9SBenno Rice * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 315244eac9SBenno Rice * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 325244eac9SBenno Rice * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 335244eac9SBenno Rice * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 345244eac9SBenno Rice * POSSIBILITY OF SUCH DAMAGE. 355244eac9SBenno Rice */ 3660727d8bSWarner Losh /*- 37f9bac91bSBenno Rice * Copyright (C) 1995, 1996 Wolfgang Solfrank. 38f9bac91bSBenno Rice * Copyright (C) 1995, 1996 TooLs GmbH. 39f9bac91bSBenno Rice * All rights reserved. 40f9bac91bSBenno Rice * 41f9bac91bSBenno Rice * Redistribution and use in source and binary forms, with or without 42f9bac91bSBenno Rice * modification, are permitted provided that the following conditions 43f9bac91bSBenno Rice * are met: 44f9bac91bSBenno Rice * 1. Redistributions of source code must retain the above copyright 45f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer. 46f9bac91bSBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 47f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer in the 48f9bac91bSBenno Rice * documentation and/or other materials provided with the distribution. 49f9bac91bSBenno Rice * 3. All advertising materials mentioning features or use of this software 50f9bac91bSBenno Rice * must display the following acknowledgement: 51f9bac91bSBenno Rice * This product includes software developed by TooLs GmbH. 52f9bac91bSBenno Rice * 4. The name of TooLs GmbH may not be used to endorse or promote products 53f9bac91bSBenno Rice * derived from this software without specific prior written permission. 54f9bac91bSBenno Rice * 55f9bac91bSBenno Rice * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 56f9bac91bSBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 57f9bac91bSBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 58f9bac91bSBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 59f9bac91bSBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 60f9bac91bSBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 61f9bac91bSBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 62f9bac91bSBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 63f9bac91bSBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 64f9bac91bSBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 65f9bac91bSBenno Rice * 66111c77dcSBenno Rice * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $ 67f9bac91bSBenno Rice */ 6860727d8bSWarner Losh /*- 69f9bac91bSBenno Rice * Copyright (C) 2001 Benno Rice. 70f9bac91bSBenno Rice * All rights reserved. 71f9bac91bSBenno Rice * 72f9bac91bSBenno Rice * Redistribution and use in source and binary forms, with or without 73f9bac91bSBenno Rice * modification, are permitted provided that the following conditions 74f9bac91bSBenno Rice * are met: 75f9bac91bSBenno Rice * 1. Redistributions of source code must retain the above copyright 76f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer. 77f9bac91bSBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 78f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer in the 79f9bac91bSBenno Rice * documentation and/or other materials provided with the distribution. 80f9bac91bSBenno Rice * 81f9bac91bSBenno Rice * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 82f9bac91bSBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 83f9bac91bSBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 84f9bac91bSBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 85f9bac91bSBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 86f9bac91bSBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 87f9bac91bSBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 88f9bac91bSBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 89f9bac91bSBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 90f9bac91bSBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 91f9bac91bSBenno Rice */ 92f9bac91bSBenno Rice 938368cf8fSDavid E. O'Brien #include <sys/cdefs.h> 948368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$"); 95f9bac91bSBenno Rice 965244eac9SBenno Rice /* 975244eac9SBenno Rice * Manages physical address maps. 985244eac9SBenno Rice * 995244eac9SBenno Rice * In addition to hardware address maps, this module is called upon to 1005244eac9SBenno Rice * provide software-use-only maps which may or may not be stored in the 1015244eac9SBenno Rice * same form as hardware maps. These pseudo-maps are used to store 1025244eac9SBenno Rice * intermediate results from copy operations to and from address spaces. 1035244eac9SBenno Rice * 1045244eac9SBenno Rice * Since the information managed by this module is also stored by the 1055244eac9SBenno Rice * logical address mapping module, this module may throw away valid virtual 1065244eac9SBenno Rice * to physical mappings at almost any time. However, invalidations of 1075244eac9SBenno Rice * mappings must be done as requested. 1085244eac9SBenno Rice * 1095244eac9SBenno Rice * In order to cope with hardware architectures which make virtual to 1105244eac9SBenno Rice * physical map invalidates expensive, this module may delay invalidate 1115244eac9SBenno Rice * reduced protection operations until such time as they are actually 1125244eac9SBenno Rice * necessary. This module is given full information as to which processors 1135244eac9SBenno Rice * are currently using which maps, and to when physical maps must be made 1145244eac9SBenno Rice * correct. 1155244eac9SBenno Rice */ 1165244eac9SBenno Rice 117ad7a226fSPeter Wemm #include "opt_kstack_pages.h" 118ad7a226fSPeter Wemm 119f9bac91bSBenno Rice #include <sys/param.h> 1200b27d710SPeter Wemm #include <sys/kernel.h> 1215244eac9SBenno Rice #include <sys/ktr.h> 12294e0b85eSMark Peek #include <sys/lock.h> 1235244eac9SBenno Rice #include <sys/msgbuf.h> 124f9bac91bSBenno Rice #include <sys/mutex.h> 1255244eac9SBenno Rice #include <sys/proc.h> 1265244eac9SBenno Rice #include <sys/sysctl.h> 1275244eac9SBenno Rice #include <sys/systm.h> 1285244eac9SBenno Rice #include <sys/vmmeter.h> 1295244eac9SBenno Rice 1305244eac9SBenno Rice #include <dev/ofw/openfirm.h> 131f9bac91bSBenno Rice 132f9bac91bSBenno Rice #include <vm/vm.h> 133f9bac91bSBenno Rice #include <vm/vm_param.h> 134f9bac91bSBenno Rice #include <vm/vm_kern.h> 135f9bac91bSBenno Rice #include <vm/vm_page.h> 136f9bac91bSBenno Rice #include <vm/vm_map.h> 137f9bac91bSBenno Rice #include <vm/vm_object.h> 138f9bac91bSBenno Rice #include <vm/vm_extern.h> 139f9bac91bSBenno Rice #include <vm/vm_pageout.h> 140f9bac91bSBenno Rice #include <vm/vm_pager.h> 141378862a7SJeff Roberson #include <vm/uma.h> 142f9bac91bSBenno Rice 1437c277971SPeter Grehan #include <machine/cpu.h> 144b40ce02aSNathan Whitehorn #include <machine/platform.h> 145d699b539SMark Peek #include <machine/bat.h> 1465244eac9SBenno Rice #include <machine/frame.h> 1475244eac9SBenno Rice #include <machine/md_var.h> 1485244eac9SBenno Rice #include <machine/psl.h> 149f9bac91bSBenno Rice #include <machine/pte.h> 15012640815SMarcel Moolenaar #include <machine/smp.h> 1515244eac9SBenno Rice #include <machine/sr.h> 15259276937SPeter Grehan #include <machine/mmuvar.h> 153f9bac91bSBenno Rice 15459276937SPeter Grehan #include "mmu_if.h" 15559276937SPeter Grehan 15659276937SPeter Grehan #define MOEA_DEBUG 157f9bac91bSBenno Rice 1585244eac9SBenno Rice #define TODO panic("%s: not implemented", __func__); 159f9bac91bSBenno Rice 1605244eac9SBenno Rice #define VSID_MAKE(sr, hash) ((sr) | (((hash) & 0xfffff) << 4)) 1615244eac9SBenno Rice #define VSID_TO_SR(vsid) ((vsid) & 0xf) 1625244eac9SBenno Rice #define VSID_TO_HASH(vsid) (((vsid) >> 4) & 0xfffff) 1635244eac9SBenno Rice 1644dba5df1SPeter Grehan #define PVO_PTEGIDX_MASK 0x007 /* which PTEG slot */ 1654dba5df1SPeter Grehan #define PVO_PTEGIDX_VALID 0x008 /* slot is valid */ 1664dba5df1SPeter Grehan #define PVO_WIRED 0x010 /* PVO entry is wired */ 1674dba5df1SPeter Grehan #define PVO_MANAGED 0x020 /* PVO entry is managed */ 1684dba5df1SPeter Grehan #define PVO_EXECUTABLE 0x040 /* PVO entry is executable */ 1694dba5df1SPeter Grehan #define PVO_BOOTSTRAP 0x080 /* PVO entry allocated during 17049f8f727SBenno Rice bootstrap */ 1714dba5df1SPeter Grehan #define PVO_FAKE 0x100 /* fictitious phys page */ 1725244eac9SBenno Rice #define PVO_VADDR(pvo) ((pvo)->pvo_vaddr & ~ADDR_POFF) 1735244eac9SBenno Rice #define PVO_ISEXECUTABLE(pvo) ((pvo)->pvo_vaddr & PVO_EXECUTABLE) 1744dba5df1SPeter Grehan #define PVO_ISFAKE(pvo) ((pvo)->pvo_vaddr & PVO_FAKE) 1755244eac9SBenno Rice #define PVO_PTEGIDX_GET(pvo) ((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK) 1765244eac9SBenno Rice #define PVO_PTEGIDX_ISSET(pvo) ((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID) 1775244eac9SBenno Rice #define PVO_PTEGIDX_CLR(pvo) \ 1785244eac9SBenno Rice ((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK))) 1795244eac9SBenno Rice #define PVO_PTEGIDX_SET(pvo, i) \ 1805244eac9SBenno Rice ((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID)) 1815244eac9SBenno Rice 18259276937SPeter Grehan #define MOEA_PVO_CHECK(pvo) 1835244eac9SBenno Rice 1845244eac9SBenno Rice struct ofw_map { 1855244eac9SBenno Rice vm_offset_t om_va; 1865244eac9SBenno Rice vm_size_t om_len; 1875244eac9SBenno Rice vm_offset_t om_pa; 1885244eac9SBenno Rice u_int om_mode; 1895244eac9SBenno Rice }; 190f9bac91bSBenno Rice 1915244eac9SBenno Rice /* 1925244eac9SBenno Rice * Map of physical memory regions. 1935244eac9SBenno Rice */ 19431c82d03SBenno Rice static struct mem_region *regions; 19531c82d03SBenno Rice static struct mem_region *pregions; 19659276937SPeter Grehan u_int phys_avail_count; 19731c82d03SBenno Rice int regions_sz, pregions_sz; 198aa39961eSBenno Rice static struct ofw_map *translations; 1995244eac9SBenno Rice 2005244eac9SBenno Rice extern struct pmap ofw_pmap; 201f9bac91bSBenno Rice 202f9bac91bSBenno Rice /* 203f489bf21SAlan Cox * Lock for the pteg and pvo tables. 204f489bf21SAlan Cox */ 20559276937SPeter Grehan struct mtx moea_table_mutex; 206f489bf21SAlan Cox 207e4f72b32SMarcel Moolenaar /* tlbie instruction synchronization */ 208e4f72b32SMarcel Moolenaar static struct mtx tlbie_mtx; 209e4f72b32SMarcel Moolenaar 210f489bf21SAlan Cox /* 2115244eac9SBenno Rice * PTEG data. 212f9bac91bSBenno Rice */ 21359276937SPeter Grehan static struct pteg *moea_pteg_table; 21459276937SPeter Grehan u_int moea_pteg_count; 21559276937SPeter Grehan u_int moea_pteg_mask; 2165244eac9SBenno Rice 2175244eac9SBenno Rice /* 2185244eac9SBenno Rice * PVO data. 2195244eac9SBenno Rice */ 22059276937SPeter Grehan struct pvo_head *moea_pvo_table; /* pvo entries by pteg index */ 22159276937SPeter Grehan struct pvo_head moea_pvo_kunmanaged = 22259276937SPeter Grehan LIST_HEAD_INITIALIZER(moea_pvo_kunmanaged); /* list of unmanaged pages */ 22359276937SPeter Grehan struct pvo_head moea_pvo_unmanaged = 22459276937SPeter Grehan LIST_HEAD_INITIALIZER(moea_pvo_unmanaged); /* list of unmanaged pages */ 2255244eac9SBenno Rice 22659276937SPeter Grehan uma_zone_t moea_upvo_zone; /* zone for pvo entries for unmanaged pages */ 22759276937SPeter Grehan uma_zone_t moea_mpvo_zone; /* zone for pvo entries for managed pages */ 2285244eac9SBenno Rice 2290d290675SBenno Rice #define BPVO_POOL_SIZE 32768 23059276937SPeter Grehan static struct pvo_entry *moea_bpvo_pool; 23159276937SPeter Grehan static int moea_bpvo_pool_index = 0; 2325244eac9SBenno Rice 2335244eac9SBenno Rice #define VSID_NBPW (sizeof(u_int32_t) * 8) 23459276937SPeter Grehan static u_int moea_vsid_bitmap[NPMAPS / VSID_NBPW]; 2355244eac9SBenno Rice 23659276937SPeter Grehan static boolean_t moea_initialized = FALSE; 2375244eac9SBenno Rice 2385244eac9SBenno Rice /* 2395244eac9SBenno Rice * Statistics. 2405244eac9SBenno Rice */ 24159276937SPeter Grehan u_int moea_pte_valid = 0; 24259276937SPeter Grehan u_int moea_pte_overflow = 0; 24359276937SPeter Grehan u_int moea_pte_replacements = 0; 24459276937SPeter Grehan u_int moea_pvo_entries = 0; 24559276937SPeter Grehan u_int moea_pvo_enter_calls = 0; 24659276937SPeter Grehan u_int moea_pvo_remove_calls = 0; 24759276937SPeter Grehan u_int moea_pte_spills = 0; 24859276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_valid, CTLFLAG_RD, &moea_pte_valid, 2495244eac9SBenno Rice 0, ""); 25059276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_overflow, CTLFLAG_RD, 25159276937SPeter Grehan &moea_pte_overflow, 0, ""); 25259276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_replacements, CTLFLAG_RD, 25359276937SPeter Grehan &moea_pte_replacements, 0, ""); 25459276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_entries, CTLFLAG_RD, &moea_pvo_entries, 2555244eac9SBenno Rice 0, ""); 25659276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_enter_calls, CTLFLAG_RD, 25759276937SPeter Grehan &moea_pvo_enter_calls, 0, ""); 25859276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pvo_remove_calls, CTLFLAG_RD, 25959276937SPeter Grehan &moea_pvo_remove_calls, 0, ""); 26059276937SPeter Grehan SYSCTL_INT(_machdep, OID_AUTO, moea_pte_spills, CTLFLAG_RD, 26159276937SPeter Grehan &moea_pte_spills, 0, ""); 2625244eac9SBenno Rice 2635244eac9SBenno Rice /* 26459276937SPeter Grehan * Allocate physical memory for use in moea_bootstrap. 2655244eac9SBenno Rice */ 26659276937SPeter Grehan static vm_offset_t moea_bootstrap_alloc(vm_size_t, u_int); 2675244eac9SBenno Rice 2685244eac9SBenno Rice /* 2695244eac9SBenno Rice * PTE calls. 2705244eac9SBenno Rice */ 27159276937SPeter Grehan static int moea_pte_insert(u_int, struct pte *); 2725244eac9SBenno Rice 2735244eac9SBenno Rice /* 2745244eac9SBenno Rice * PVO calls. 2755244eac9SBenno Rice */ 27659276937SPeter Grehan static int moea_pvo_enter(pmap_t, uma_zone_t, struct pvo_head *, 2775244eac9SBenno Rice vm_offset_t, vm_offset_t, u_int, int); 27859276937SPeter Grehan static void moea_pvo_remove(struct pvo_entry *, int); 27959276937SPeter Grehan static struct pvo_entry *moea_pvo_find_va(pmap_t, vm_offset_t, int *); 28059276937SPeter Grehan static struct pte *moea_pvo_to_pte(const struct pvo_entry *, int); 2815244eac9SBenno Rice 2825244eac9SBenno Rice /* 2835244eac9SBenno Rice * Utility routines. 2845244eac9SBenno Rice */ 285ce142d9eSAlan Cox static void moea_enter_locked(pmap_t, vm_offset_t, vm_page_t, 286ce142d9eSAlan Cox vm_prot_t, boolean_t); 28759276937SPeter Grehan static void moea_syncicache(vm_offset_t, vm_size_t); 28859276937SPeter Grehan static boolean_t moea_query_bit(vm_page_t, int); 289*ce186587SAlan Cox static u_int moea_clear_bit(vm_page_t, int); 29059276937SPeter Grehan static void moea_kremove(mmu_t, vm_offset_t); 29159276937SPeter Grehan int moea_pte_spill(vm_offset_t); 29259276937SPeter Grehan 29359276937SPeter Grehan /* 29459276937SPeter Grehan * Kernel MMU interface 29559276937SPeter Grehan */ 29659276937SPeter Grehan void moea_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t); 29759276937SPeter Grehan void moea_clear_modify(mmu_t, vm_page_t); 29859276937SPeter Grehan void moea_clear_reference(mmu_t, vm_page_t); 29959276937SPeter Grehan void moea_copy_page(mmu_t, vm_page_t, vm_page_t); 30059276937SPeter Grehan void moea_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t); 301ce142d9eSAlan Cox void moea_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t, 302ce142d9eSAlan Cox vm_prot_t); 3032053c127SStephan Uphoff void moea_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t); 30459276937SPeter Grehan vm_paddr_t moea_extract(mmu_t, pmap_t, vm_offset_t); 30559276937SPeter Grehan vm_page_t moea_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t); 30659276937SPeter Grehan void moea_init(mmu_t); 30759276937SPeter Grehan boolean_t moea_is_modified(mmu_t, vm_page_t); 3087b85f591SAlan Cox boolean_t moea_is_referenced(mmu_t, vm_page_t); 30959276937SPeter Grehan boolean_t moea_ts_referenced(mmu_t, vm_page_t); 31059276937SPeter Grehan vm_offset_t moea_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int); 31159276937SPeter Grehan boolean_t moea_page_exists_quick(mmu_t, pmap_t, vm_page_t); 31259677d3cSAlan Cox int moea_page_wired_mappings(mmu_t, vm_page_t); 31359276937SPeter Grehan void moea_pinit(mmu_t, pmap_t); 31459276937SPeter Grehan void moea_pinit0(mmu_t, pmap_t); 31559276937SPeter Grehan void moea_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t); 31659276937SPeter Grehan void moea_qenter(mmu_t, vm_offset_t, vm_page_t *, int); 31759276937SPeter Grehan void moea_qremove(mmu_t, vm_offset_t, int); 31859276937SPeter Grehan void moea_release(mmu_t, pmap_t); 31959276937SPeter Grehan void moea_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t); 32059276937SPeter Grehan void moea_remove_all(mmu_t, vm_page_t); 32178985e42SAlan Cox void moea_remove_write(mmu_t, vm_page_t); 32259276937SPeter Grehan void moea_zero_page(mmu_t, vm_page_t); 32359276937SPeter Grehan void moea_zero_page_area(mmu_t, vm_page_t, int, int); 32459276937SPeter Grehan void moea_zero_page_idle(mmu_t, vm_page_t); 32559276937SPeter Grehan void moea_activate(mmu_t, struct thread *); 32659276937SPeter Grehan void moea_deactivate(mmu_t, struct thread *); 3271c96bdd1SNathan Whitehorn void moea_cpu_bootstrap(mmu_t, int); 32859276937SPeter Grehan void moea_bootstrap(mmu_t, vm_offset_t, vm_offset_t); 32959276937SPeter Grehan void *moea_mapdev(mmu_t, vm_offset_t, vm_size_t); 33059276937SPeter Grehan void moea_unmapdev(mmu_t, vm_offset_t, vm_size_t); 33159276937SPeter Grehan vm_offset_t moea_kextract(mmu_t, vm_offset_t); 33259276937SPeter Grehan void moea_kenter(mmu_t, vm_offset_t, vm_offset_t); 33359276937SPeter Grehan boolean_t moea_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t); 3341a4fcaebSMarcel Moolenaar static void moea_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t); 33559276937SPeter Grehan 33659276937SPeter Grehan static mmu_method_t moea_methods[] = { 33759276937SPeter Grehan MMUMETHOD(mmu_change_wiring, moea_change_wiring), 33859276937SPeter Grehan MMUMETHOD(mmu_clear_modify, moea_clear_modify), 33959276937SPeter Grehan MMUMETHOD(mmu_clear_reference, moea_clear_reference), 34059276937SPeter Grehan MMUMETHOD(mmu_copy_page, moea_copy_page), 34159276937SPeter Grehan MMUMETHOD(mmu_enter, moea_enter), 342ce142d9eSAlan Cox MMUMETHOD(mmu_enter_object, moea_enter_object), 34359276937SPeter Grehan MMUMETHOD(mmu_enter_quick, moea_enter_quick), 34459276937SPeter Grehan MMUMETHOD(mmu_extract, moea_extract), 34559276937SPeter Grehan MMUMETHOD(mmu_extract_and_hold, moea_extract_and_hold), 34659276937SPeter Grehan MMUMETHOD(mmu_init, moea_init), 34759276937SPeter Grehan MMUMETHOD(mmu_is_modified, moea_is_modified), 3487b85f591SAlan Cox MMUMETHOD(mmu_is_referenced, moea_is_referenced), 34959276937SPeter Grehan MMUMETHOD(mmu_ts_referenced, moea_ts_referenced), 35059276937SPeter Grehan MMUMETHOD(mmu_map, moea_map), 35159276937SPeter Grehan MMUMETHOD(mmu_page_exists_quick,moea_page_exists_quick), 35259677d3cSAlan Cox MMUMETHOD(mmu_page_wired_mappings,moea_page_wired_mappings), 35359276937SPeter Grehan MMUMETHOD(mmu_pinit, moea_pinit), 35459276937SPeter Grehan MMUMETHOD(mmu_pinit0, moea_pinit0), 35559276937SPeter Grehan MMUMETHOD(mmu_protect, moea_protect), 35659276937SPeter Grehan MMUMETHOD(mmu_qenter, moea_qenter), 35759276937SPeter Grehan MMUMETHOD(mmu_qremove, moea_qremove), 35859276937SPeter Grehan MMUMETHOD(mmu_release, moea_release), 35959276937SPeter Grehan MMUMETHOD(mmu_remove, moea_remove), 36059276937SPeter Grehan MMUMETHOD(mmu_remove_all, moea_remove_all), 36178985e42SAlan Cox MMUMETHOD(mmu_remove_write, moea_remove_write), 3621a4fcaebSMarcel Moolenaar MMUMETHOD(mmu_sync_icache, moea_sync_icache), 36359276937SPeter Grehan MMUMETHOD(mmu_zero_page, moea_zero_page), 36459276937SPeter Grehan MMUMETHOD(mmu_zero_page_area, moea_zero_page_area), 36559276937SPeter Grehan MMUMETHOD(mmu_zero_page_idle, moea_zero_page_idle), 36659276937SPeter Grehan MMUMETHOD(mmu_activate, moea_activate), 36759276937SPeter Grehan MMUMETHOD(mmu_deactivate, moea_deactivate), 36859276937SPeter Grehan 36959276937SPeter Grehan /* Internal interfaces */ 37059276937SPeter Grehan MMUMETHOD(mmu_bootstrap, moea_bootstrap), 3711c96bdd1SNathan Whitehorn MMUMETHOD(mmu_cpu_bootstrap, moea_cpu_bootstrap), 37259276937SPeter Grehan MMUMETHOD(mmu_mapdev, moea_mapdev), 37359276937SPeter Grehan MMUMETHOD(mmu_unmapdev, moea_unmapdev), 37459276937SPeter Grehan MMUMETHOD(mmu_kextract, moea_kextract), 37559276937SPeter Grehan MMUMETHOD(mmu_kenter, moea_kenter), 37659276937SPeter Grehan MMUMETHOD(mmu_dev_direct_mapped,moea_dev_direct_mapped), 37759276937SPeter Grehan 37859276937SPeter Grehan { 0, 0 } 37959276937SPeter Grehan }; 38059276937SPeter Grehan 38159276937SPeter Grehan static mmu_def_t oea_mmu = { 38259276937SPeter Grehan MMU_TYPE_OEA, 38359276937SPeter Grehan moea_methods, 38459276937SPeter Grehan 0 38559276937SPeter Grehan }; 38659276937SPeter Grehan MMU_DEF(oea_mmu); 38759276937SPeter Grehan 388e4f72b32SMarcel Moolenaar static void 389e4f72b32SMarcel Moolenaar tlbie(vm_offset_t va) 390e4f72b32SMarcel Moolenaar { 391e4f72b32SMarcel Moolenaar 392e4f72b32SMarcel Moolenaar mtx_lock_spin(&tlbie_mtx); 393e4f72b32SMarcel Moolenaar __asm __volatile("tlbie %0" :: "r"(va)); 394e4f72b32SMarcel Moolenaar __asm __volatile("tlbsync"); 395e4f72b32SMarcel Moolenaar powerpc_sync(); 396e4f72b32SMarcel Moolenaar mtx_unlock_spin(&tlbie_mtx); 397e4f72b32SMarcel Moolenaar } 398e4f72b32SMarcel Moolenaar 399e4f72b32SMarcel Moolenaar static void 400e4f72b32SMarcel Moolenaar tlbia(void) 401e4f72b32SMarcel Moolenaar { 402e4f72b32SMarcel Moolenaar vm_offset_t va; 403e4f72b32SMarcel Moolenaar 404e4f72b32SMarcel Moolenaar for (va = 0; va < 0x00040000; va += 0x00001000) { 405e4f72b32SMarcel Moolenaar __asm __volatile("tlbie %0" :: "r"(va)); 406e4f72b32SMarcel Moolenaar powerpc_sync(); 407e4f72b32SMarcel Moolenaar } 408e4f72b32SMarcel Moolenaar __asm __volatile("tlbsync"); 409e4f72b32SMarcel Moolenaar powerpc_sync(); 410e4f72b32SMarcel Moolenaar } 4115244eac9SBenno Rice 4125244eac9SBenno Rice static __inline int 4135244eac9SBenno Rice va_to_sr(u_int *sr, vm_offset_t va) 4145244eac9SBenno Rice { 4155244eac9SBenno Rice return (sr[(uintptr_t)va >> ADDR_SR_SHFT]); 4165244eac9SBenno Rice } 4175244eac9SBenno Rice 4185244eac9SBenno Rice static __inline u_int 4195244eac9SBenno Rice va_to_pteg(u_int sr, vm_offset_t addr) 4205244eac9SBenno Rice { 4215244eac9SBenno Rice u_int hash; 4225244eac9SBenno Rice 4235244eac9SBenno Rice hash = (sr & SR_VSID_MASK) ^ (((u_int)addr & ADDR_PIDX) >> 4245244eac9SBenno Rice ADDR_PIDX_SHFT); 42559276937SPeter Grehan return (hash & moea_pteg_mask); 4265244eac9SBenno Rice } 4275244eac9SBenno Rice 4285244eac9SBenno Rice static __inline struct pvo_head * 4298207b362SBenno Rice pa_to_pvoh(vm_offset_t pa, vm_page_t *pg_p) 4305244eac9SBenno Rice { 4315244eac9SBenno Rice struct vm_page *pg; 4325244eac9SBenno Rice 4335244eac9SBenno Rice pg = PHYS_TO_VM_PAGE(pa); 4345244eac9SBenno Rice 4358207b362SBenno Rice if (pg_p != NULL) 4368207b362SBenno Rice *pg_p = pg; 4378207b362SBenno Rice 4385244eac9SBenno Rice if (pg == NULL) 43959276937SPeter Grehan return (&moea_pvo_unmanaged); 4405244eac9SBenno Rice 4415244eac9SBenno Rice return (&pg->md.mdpg_pvoh); 4425244eac9SBenno Rice } 4435244eac9SBenno Rice 4445244eac9SBenno Rice static __inline struct pvo_head * 4455244eac9SBenno Rice vm_page_to_pvoh(vm_page_t m) 446f9bac91bSBenno Rice { 447f9bac91bSBenno Rice 4485244eac9SBenno Rice return (&m->md.mdpg_pvoh); 449f9bac91bSBenno Rice } 450f9bac91bSBenno Rice 451f9bac91bSBenno Rice static __inline void 45259276937SPeter Grehan moea_attr_clear(vm_page_t m, int ptebit) 453f9bac91bSBenno Rice { 454f9bac91bSBenno Rice 455d644a0b7SAlan Cox mtx_assert(&vm_page_queue_mtx, MA_OWNED); 4565244eac9SBenno Rice m->md.mdpg_attrs &= ~ptebit; 4575244eac9SBenno Rice } 4585244eac9SBenno Rice 4595244eac9SBenno Rice static __inline int 46059276937SPeter Grehan moea_attr_fetch(vm_page_t m) 4615244eac9SBenno Rice { 4625244eac9SBenno Rice 4635244eac9SBenno Rice return (m->md.mdpg_attrs); 464f9bac91bSBenno Rice } 465f9bac91bSBenno Rice 466f9bac91bSBenno Rice static __inline void 46759276937SPeter Grehan moea_attr_save(vm_page_t m, int ptebit) 468f9bac91bSBenno Rice { 469f9bac91bSBenno Rice 470d644a0b7SAlan Cox mtx_assert(&vm_page_queue_mtx, MA_OWNED); 4715244eac9SBenno Rice m->md.mdpg_attrs |= ptebit; 472f9bac91bSBenno Rice } 473f9bac91bSBenno Rice 474f9bac91bSBenno Rice static __inline int 47559276937SPeter Grehan moea_pte_compare(const struct pte *pt, const struct pte *pvo_pt) 476f9bac91bSBenno Rice { 4775244eac9SBenno Rice if (pt->pte_hi == pvo_pt->pte_hi) 4785244eac9SBenno Rice return (1); 479f9bac91bSBenno Rice 4805244eac9SBenno Rice return (0); 481f9bac91bSBenno Rice } 482f9bac91bSBenno Rice 483f9bac91bSBenno Rice static __inline int 48459276937SPeter Grehan moea_pte_match(struct pte *pt, u_int sr, vm_offset_t va, int which) 485f9bac91bSBenno Rice { 4865244eac9SBenno Rice return (pt->pte_hi & ~PTE_VALID) == 4875244eac9SBenno Rice (((sr & SR_VSID_MASK) << PTE_VSID_SHFT) | 4885244eac9SBenno Rice ((va >> ADDR_API_SHFT) & PTE_API) | which); 489f9bac91bSBenno Rice } 490f9bac91bSBenno Rice 4915244eac9SBenno Rice static __inline void 49259276937SPeter Grehan moea_pte_create(struct pte *pt, u_int sr, vm_offset_t va, u_int pte_lo) 493f9bac91bSBenno Rice { 494d644a0b7SAlan Cox 495d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 496d644a0b7SAlan Cox 497f9bac91bSBenno Rice /* 4985244eac9SBenno Rice * Construct a PTE. Default to IMB initially. Valid bit only gets 4995244eac9SBenno Rice * set when the real pte is set in memory. 500f9bac91bSBenno Rice * 501f9bac91bSBenno Rice * Note: Don't set the valid bit for correct operation of tlb update. 502f9bac91bSBenno Rice */ 5035244eac9SBenno Rice pt->pte_hi = ((sr & SR_VSID_MASK) << PTE_VSID_SHFT) | 5045244eac9SBenno Rice (((va & ADDR_PIDX) >> ADDR_API_SHFT) & PTE_API); 5055244eac9SBenno Rice pt->pte_lo = pte_lo; 506f9bac91bSBenno Rice } 507f9bac91bSBenno Rice 5085244eac9SBenno Rice static __inline void 50959276937SPeter Grehan moea_pte_synch(struct pte *pt, struct pte *pvo_pt) 510f9bac91bSBenno Rice { 511f9bac91bSBenno Rice 512d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 5135244eac9SBenno Rice pvo_pt->pte_lo |= pt->pte_lo & (PTE_REF | PTE_CHG); 514f9bac91bSBenno Rice } 515f9bac91bSBenno Rice 5165244eac9SBenno Rice static __inline void 51759276937SPeter Grehan moea_pte_clear(struct pte *pt, vm_offset_t va, int ptebit) 518f9bac91bSBenno Rice { 5195244eac9SBenno Rice 520d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 521d644a0b7SAlan Cox 5225244eac9SBenno Rice /* 5235244eac9SBenno Rice * As shown in Section 7.6.3.2.3 5245244eac9SBenno Rice */ 5255244eac9SBenno Rice pt->pte_lo &= ~ptebit; 526e4f72b32SMarcel Moolenaar tlbie(va); 5275244eac9SBenno Rice } 5285244eac9SBenno Rice 5295244eac9SBenno Rice static __inline void 53059276937SPeter Grehan moea_pte_set(struct pte *pt, struct pte *pvo_pt) 5315244eac9SBenno Rice { 5325244eac9SBenno Rice 533d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 5345244eac9SBenno Rice pvo_pt->pte_hi |= PTE_VALID; 5355244eac9SBenno Rice 5365244eac9SBenno Rice /* 5375244eac9SBenno Rice * Update the PTE as defined in section 7.6.3.1. 5385244eac9SBenno Rice * Note that the REF/CHG bits are from pvo_pt and thus should havce 5395244eac9SBenno Rice * been saved so this routine can restore them (if desired). 5405244eac9SBenno Rice */ 5415244eac9SBenno Rice pt->pte_lo = pvo_pt->pte_lo; 542e4f72b32SMarcel Moolenaar powerpc_sync(); 5435244eac9SBenno Rice pt->pte_hi = pvo_pt->pte_hi; 544e4f72b32SMarcel Moolenaar powerpc_sync(); 54559276937SPeter Grehan moea_pte_valid++; 5465244eac9SBenno Rice } 5475244eac9SBenno Rice 5485244eac9SBenno Rice static __inline void 54959276937SPeter Grehan moea_pte_unset(struct pte *pt, struct pte *pvo_pt, vm_offset_t va) 5505244eac9SBenno Rice { 5515244eac9SBenno Rice 552d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 5535244eac9SBenno Rice pvo_pt->pte_hi &= ~PTE_VALID; 5545244eac9SBenno Rice 5555244eac9SBenno Rice /* 5565244eac9SBenno Rice * Force the reg & chg bits back into the PTEs. 5575244eac9SBenno Rice */ 558e4f72b32SMarcel Moolenaar powerpc_sync(); 5595244eac9SBenno Rice 5605244eac9SBenno Rice /* 5615244eac9SBenno Rice * Invalidate the pte. 5625244eac9SBenno Rice */ 5635244eac9SBenno Rice pt->pte_hi &= ~PTE_VALID; 5645244eac9SBenno Rice 565e4f72b32SMarcel Moolenaar tlbie(va); 5665244eac9SBenno Rice 5675244eac9SBenno Rice /* 5685244eac9SBenno Rice * Save the reg & chg bits. 5695244eac9SBenno Rice */ 57059276937SPeter Grehan moea_pte_synch(pt, pvo_pt); 57159276937SPeter Grehan moea_pte_valid--; 5725244eac9SBenno Rice } 5735244eac9SBenno Rice 5745244eac9SBenno Rice static __inline void 57559276937SPeter Grehan moea_pte_change(struct pte *pt, struct pte *pvo_pt, vm_offset_t va) 5765244eac9SBenno Rice { 5775244eac9SBenno Rice 5785244eac9SBenno Rice /* 5795244eac9SBenno Rice * Invalidate the PTE 5805244eac9SBenno Rice */ 58159276937SPeter Grehan moea_pte_unset(pt, pvo_pt, va); 58259276937SPeter Grehan moea_pte_set(pt, pvo_pt); 583f9bac91bSBenno Rice } 584f9bac91bSBenno Rice 585f9bac91bSBenno Rice /* 5865244eac9SBenno Rice * Quick sort callout for comparing memory regions. 587f9bac91bSBenno Rice */ 5885244eac9SBenno Rice static int mr_cmp(const void *a, const void *b); 5895244eac9SBenno Rice static int om_cmp(const void *a, const void *b); 5905244eac9SBenno Rice 5915244eac9SBenno Rice static int 5925244eac9SBenno Rice mr_cmp(const void *a, const void *b) 593f9bac91bSBenno Rice { 5945244eac9SBenno Rice const struct mem_region *regiona; 5955244eac9SBenno Rice const struct mem_region *regionb; 596f9bac91bSBenno Rice 5975244eac9SBenno Rice regiona = a; 5985244eac9SBenno Rice regionb = b; 5995244eac9SBenno Rice if (regiona->mr_start < regionb->mr_start) 6005244eac9SBenno Rice return (-1); 6015244eac9SBenno Rice else if (regiona->mr_start > regionb->mr_start) 6025244eac9SBenno Rice return (1); 6035244eac9SBenno Rice else 604f9bac91bSBenno Rice return (0); 605f9bac91bSBenno Rice } 6065244eac9SBenno Rice 6075244eac9SBenno Rice static int 6085244eac9SBenno Rice om_cmp(const void *a, const void *b) 6095244eac9SBenno Rice { 6105244eac9SBenno Rice const struct ofw_map *mapa; 6115244eac9SBenno Rice const struct ofw_map *mapb; 6125244eac9SBenno Rice 6135244eac9SBenno Rice mapa = a; 6145244eac9SBenno Rice mapb = b; 6155244eac9SBenno Rice if (mapa->om_pa < mapb->om_pa) 6165244eac9SBenno Rice return (-1); 6175244eac9SBenno Rice else if (mapa->om_pa > mapb->om_pa) 6185244eac9SBenno Rice return (1); 6195244eac9SBenno Rice else 6205244eac9SBenno Rice return (0); 621f9bac91bSBenno Rice } 622f9bac91bSBenno Rice 623f9bac91bSBenno Rice void 6241c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmu_t mmup, int ap) 62512640815SMarcel Moolenaar { 62612640815SMarcel Moolenaar u_int sdr; 62712640815SMarcel Moolenaar int i; 62812640815SMarcel Moolenaar 62912640815SMarcel Moolenaar if (ap) { 630e4f72b32SMarcel Moolenaar powerpc_sync(); 63112640815SMarcel Moolenaar __asm __volatile("mtdbatu 0,%0" :: "r"(battable[0].batu)); 63212640815SMarcel Moolenaar __asm __volatile("mtdbatl 0,%0" :: "r"(battable[0].batl)); 63312640815SMarcel Moolenaar isync(); 63412640815SMarcel Moolenaar __asm __volatile("mtibatu 0,%0" :: "r"(battable[0].batu)); 63512640815SMarcel Moolenaar __asm __volatile("mtibatl 0,%0" :: "r"(battable[0].batl)); 63612640815SMarcel Moolenaar isync(); 63712640815SMarcel Moolenaar } 63812640815SMarcel Moolenaar 63901d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu)); 64001d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl)); 64112640815SMarcel Moolenaar isync(); 64212640815SMarcel Moolenaar 64301d8aa0dSMarcel Moolenaar __asm __volatile("mtibatu 1,%0" :: "r"(0)); 64401d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatu 2,%0" :: "r"(0)); 64501d8aa0dSMarcel Moolenaar __asm __volatile("mtibatu 2,%0" :: "r"(0)); 64601d8aa0dSMarcel Moolenaar __asm __volatile("mtdbatu 3,%0" :: "r"(0)); 64701d8aa0dSMarcel Moolenaar __asm __volatile("mtibatu 3,%0" :: "r"(0)); 64812640815SMarcel Moolenaar isync(); 64912640815SMarcel Moolenaar 65012640815SMarcel Moolenaar for (i = 0; i < 16; i++) 65112640815SMarcel Moolenaar mtsrin(i << ADDR_SR_SHFT, EMPTY_SEGMENT); 65212640815SMarcel Moolenaar 65312640815SMarcel Moolenaar __asm __volatile("mtsr %0,%1" :: "n"(KERNEL_SR), "r"(KERNEL_SEGMENT)); 65412640815SMarcel Moolenaar __asm __volatile("mtsr %0,%1" :: "n"(KERNEL2_SR), "r"(KERNEL2_SEGMENT)); 655e4f72b32SMarcel Moolenaar powerpc_sync(); 65612640815SMarcel Moolenaar 65712640815SMarcel Moolenaar sdr = (u_int)moea_pteg_table | (moea_pteg_mask >> 10); 65812640815SMarcel Moolenaar __asm __volatile("mtsdr1 %0" :: "r"(sdr)); 65912640815SMarcel Moolenaar isync(); 66012640815SMarcel Moolenaar 66186c1fb4cSMarcel Moolenaar tlbia(); 66212640815SMarcel Moolenaar } 66312640815SMarcel Moolenaar 66412640815SMarcel Moolenaar void 66559276937SPeter Grehan moea_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend) 666f9bac91bSBenno Rice { 66731c82d03SBenno Rice ihandle_t mmui; 6685244eac9SBenno Rice phandle_t chosen, mmu; 6695244eac9SBenno Rice int sz; 6705244eac9SBenno Rice int i, j; 67132bc7846SPeter Grehan int ofw_mappings; 672e2f6d6e2SPeter Grehan vm_size_t size, physsz, hwphyssz; 6735244eac9SBenno Rice vm_offset_t pa, va, off; 67450c202c5SJeff Roberson void *dpcpu; 675f9bac91bSBenno Rice 676f9bac91bSBenno Rice /* 67732bc7846SPeter Grehan * Set up BAT0 to map the lowest 256 MB area 6780d290675SBenno Rice */ 6790d290675SBenno Rice battable[0x0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW); 6800d290675SBenno Rice battable[0x0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs); 6810d290675SBenno Rice 6820d290675SBenno Rice /* 6830d290675SBenno Rice * Map PCI memory space. 6840d290675SBenno Rice */ 6850d290675SBenno Rice battable[0x8].batl = BATL(0x80000000, BAT_I|BAT_G, BAT_PP_RW); 6860d290675SBenno Rice battable[0x8].batu = BATU(0x80000000, BAT_BL_256M, BAT_Vs); 6870d290675SBenno Rice 6880d290675SBenno Rice battable[0x9].batl = BATL(0x90000000, BAT_I|BAT_G, BAT_PP_RW); 6890d290675SBenno Rice battable[0x9].batu = BATU(0x90000000, BAT_BL_256M, BAT_Vs); 6900d290675SBenno Rice 6910d290675SBenno Rice battable[0xa].batl = BATL(0xa0000000, BAT_I|BAT_G, BAT_PP_RW); 6920d290675SBenno Rice battable[0xa].batu = BATU(0xa0000000, BAT_BL_256M, BAT_Vs); 6930d290675SBenno Rice 6940d290675SBenno Rice battable[0xb].batl = BATL(0xb0000000, BAT_I|BAT_G, BAT_PP_RW); 6950d290675SBenno Rice battable[0xb].batu = BATU(0xb0000000, BAT_BL_256M, BAT_Vs); 6960d290675SBenno Rice 6970d290675SBenno Rice /* 6980d290675SBenno Rice * Map obio devices. 6990d290675SBenno Rice */ 7000d290675SBenno Rice battable[0xf].batl = BATL(0xf0000000, BAT_I|BAT_G, BAT_PP_RW); 7010d290675SBenno Rice battable[0xf].batu = BATU(0xf0000000, BAT_BL_256M, BAT_Vs); 7020d290675SBenno Rice 7030d290675SBenno Rice /* 7045244eac9SBenno Rice * Use an IBAT and a DBAT to map the bottom segment of memory 7055244eac9SBenno Rice * where we are. 706f9bac91bSBenno Rice */ 70759276937SPeter Grehan __asm (".balign 32; \n" 70872ed3108SPeter Grehan "mtibatu 0,%0; mtibatl 0,%1; isync; \n" 7095d64cf91SPeter Grehan "mtdbatu 0,%0; mtdbatl 0,%1; isync" 71012640815SMarcel Moolenaar :: "r"(battable[0].batu), "r"(battable[0].batl)); 7110d290675SBenno Rice 7120d290675SBenno Rice /* map pci space */ 71312640815SMarcel Moolenaar __asm __volatile("mtdbatu 1,%0" :: "r"(battable[8].batu)); 71412640815SMarcel Moolenaar __asm __volatile("mtdbatl 1,%0" :: "r"(battable[8].batl)); 71512640815SMarcel Moolenaar isync(); 716f9bac91bSBenno Rice 7171c96bdd1SNathan Whitehorn /* set global direct map flag */ 7181c96bdd1SNathan Whitehorn hw_direct_map = 1; 7191c96bdd1SNathan Whitehorn 72031c82d03SBenno Rice mem_regions(&pregions, &pregions_sz, ®ions, ®ions_sz); 72159276937SPeter Grehan CTR0(KTR_PMAP, "moea_bootstrap: physical memory"); 72231c82d03SBenno Rice 72331c82d03SBenno Rice qsort(pregions, pregions_sz, sizeof(*pregions), mr_cmp); 72431c82d03SBenno Rice for (i = 0; i < pregions_sz; i++) { 72532bc7846SPeter Grehan vm_offset_t pa; 72632bc7846SPeter Grehan vm_offset_t end; 72732bc7846SPeter Grehan 72831c82d03SBenno Rice CTR3(KTR_PMAP, "physregion: %#x - %#x (%#x)", 72931c82d03SBenno Rice pregions[i].mr_start, 73031c82d03SBenno Rice pregions[i].mr_start + pregions[i].mr_size, 73131c82d03SBenno Rice pregions[i].mr_size); 73232bc7846SPeter Grehan /* 73332bc7846SPeter Grehan * Install entries into the BAT table to allow all 73432bc7846SPeter Grehan * of physmem to be convered by on-demand BAT entries. 73532bc7846SPeter Grehan * The loop will sometimes set the same battable element 73632bc7846SPeter Grehan * twice, but that's fine since they won't be used for 73732bc7846SPeter Grehan * a while yet. 73832bc7846SPeter Grehan */ 73932bc7846SPeter Grehan pa = pregions[i].mr_start & 0xf0000000; 74032bc7846SPeter Grehan end = pregions[i].mr_start + pregions[i].mr_size; 74132bc7846SPeter Grehan do { 74232bc7846SPeter Grehan u_int n = pa >> ADDR_SR_SHFT; 74332bc7846SPeter Grehan 74432bc7846SPeter Grehan battable[n].batl = BATL(pa, BAT_M, BAT_PP_RW); 74532bc7846SPeter Grehan battable[n].batu = BATU(pa, BAT_BL_256M, BAT_Vs); 74632bc7846SPeter Grehan pa += SEGMENT_LENGTH; 74732bc7846SPeter Grehan } while (pa < end); 74831c82d03SBenno Rice } 74931c82d03SBenno Rice 75031c82d03SBenno Rice if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz) 75159276937SPeter Grehan panic("moea_bootstrap: phys_avail too small"); 75231c82d03SBenno Rice qsort(regions, regions_sz, sizeof(*regions), mr_cmp); 7535244eac9SBenno Rice phys_avail_count = 0; 754d2c1f576SBenno Rice physsz = 0; 755b0c21309SPeter Grehan hwphyssz = 0; 756b0c21309SPeter Grehan TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz); 75731c82d03SBenno Rice for (i = 0, j = 0; i < regions_sz; i++, j += 2) { 7585244eac9SBenno Rice CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start, 7595244eac9SBenno Rice regions[i].mr_start + regions[i].mr_size, 7605244eac9SBenno Rice regions[i].mr_size); 761e2f6d6e2SPeter Grehan if (hwphyssz != 0 && 762e2f6d6e2SPeter Grehan (physsz + regions[i].mr_size) >= hwphyssz) { 763e2f6d6e2SPeter Grehan if (physsz < hwphyssz) { 764e2f6d6e2SPeter Grehan phys_avail[j] = regions[i].mr_start; 765e2f6d6e2SPeter Grehan phys_avail[j + 1] = regions[i].mr_start + 766e2f6d6e2SPeter Grehan hwphyssz - physsz; 767e2f6d6e2SPeter Grehan physsz = hwphyssz; 768e2f6d6e2SPeter Grehan phys_avail_count++; 769e2f6d6e2SPeter Grehan } 770e2f6d6e2SPeter Grehan break; 771e2f6d6e2SPeter Grehan } 7725244eac9SBenno Rice phys_avail[j] = regions[i].mr_start; 7735244eac9SBenno Rice phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size; 7745244eac9SBenno Rice phys_avail_count++; 775d2c1f576SBenno Rice physsz += regions[i].mr_size; 776f9bac91bSBenno Rice } 777d2c1f576SBenno Rice physmem = btoc(physsz); 778f9bac91bSBenno Rice 779f9bac91bSBenno Rice /* 7805244eac9SBenno Rice * Allocate PTEG table. 781f9bac91bSBenno Rice */ 7825244eac9SBenno Rice #ifdef PTEGCOUNT 78359276937SPeter Grehan moea_pteg_count = PTEGCOUNT; 7845244eac9SBenno Rice #else 78559276937SPeter Grehan moea_pteg_count = 0x1000; 786f9bac91bSBenno Rice 78759276937SPeter Grehan while (moea_pteg_count < physmem) 78859276937SPeter Grehan moea_pteg_count <<= 1; 789f9bac91bSBenno Rice 79059276937SPeter Grehan moea_pteg_count >>= 1; 7915244eac9SBenno Rice #endif /* PTEGCOUNT */ 792f9bac91bSBenno Rice 79359276937SPeter Grehan size = moea_pteg_count * sizeof(struct pteg); 79459276937SPeter Grehan CTR2(KTR_PMAP, "moea_bootstrap: %d PTEGs, %d bytes", moea_pteg_count, 7955244eac9SBenno Rice size); 79659276937SPeter Grehan moea_pteg_table = (struct pteg *)moea_bootstrap_alloc(size, size); 79759276937SPeter Grehan CTR1(KTR_PMAP, "moea_bootstrap: PTEG table at %p", moea_pteg_table); 79859276937SPeter Grehan bzero((void *)moea_pteg_table, moea_pteg_count * sizeof(struct pteg)); 79959276937SPeter Grehan moea_pteg_mask = moea_pteg_count - 1; 800f9bac91bSBenno Rice 8015244eac9SBenno Rice /* 802864bc520SBenno Rice * Allocate pv/overflow lists. 8035244eac9SBenno Rice */ 80459276937SPeter Grehan size = sizeof(struct pvo_head) * moea_pteg_count; 80559276937SPeter Grehan moea_pvo_table = (struct pvo_head *)moea_bootstrap_alloc(size, 8065244eac9SBenno Rice PAGE_SIZE); 80759276937SPeter Grehan CTR1(KTR_PMAP, "moea_bootstrap: PVO table at %p", moea_pvo_table); 80859276937SPeter Grehan for (i = 0; i < moea_pteg_count; i++) 80959276937SPeter Grehan LIST_INIT(&moea_pvo_table[i]); 8105244eac9SBenno Rice 8115244eac9SBenno Rice /* 812f489bf21SAlan Cox * Initialize the lock that synchronizes access to the pteg and pvo 813f489bf21SAlan Cox * tables. 814f489bf21SAlan Cox */ 815d644a0b7SAlan Cox mtx_init(&moea_table_mutex, "pmap table", NULL, MTX_DEF | 816d644a0b7SAlan Cox MTX_RECURSE); 817f489bf21SAlan Cox 818e4f72b32SMarcel Moolenaar mtx_init(&tlbie_mtx, "tlbie", NULL, MTX_SPIN); 819e4f72b32SMarcel Moolenaar 820f489bf21SAlan Cox /* 8215244eac9SBenno Rice * Initialise the unmanaged pvo pool. 8225244eac9SBenno Rice */ 82359276937SPeter Grehan moea_bpvo_pool = (struct pvo_entry *)moea_bootstrap_alloc( 8240d290675SBenno Rice BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0); 82559276937SPeter Grehan moea_bpvo_pool_index = 0; 8265244eac9SBenno Rice 8275244eac9SBenno Rice /* 8285244eac9SBenno Rice * Make sure kernel vsid is allocated as well as VSID 0. 8295244eac9SBenno Rice */ 83059276937SPeter Grehan moea_vsid_bitmap[(KERNEL_VSIDBITS & (NPMAPS - 1)) / VSID_NBPW] 8315244eac9SBenno Rice |= 1 << (KERNEL_VSIDBITS % VSID_NBPW); 83259276937SPeter Grehan moea_vsid_bitmap[0] |= 1; 8335244eac9SBenno Rice 8345244eac9SBenno Rice /* 8355244eac9SBenno Rice * Set up the Open Firmware pmap and add it's mappings. 8365244eac9SBenno Rice */ 83759276937SPeter Grehan moea_pinit(mmup, &ofw_pmap); 8385244eac9SBenno Rice ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT; 8394daf20b2SPeter Grehan ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT; 8405244eac9SBenno Rice if ((chosen = OF_finddevice("/chosen")) == -1) 84159276937SPeter Grehan panic("moea_bootstrap: can't find /chosen"); 8425244eac9SBenno Rice OF_getprop(chosen, "mmu", &mmui, 4); 8435244eac9SBenno Rice if ((mmu = OF_instance_to_package(mmui)) == -1) 84459276937SPeter Grehan panic("moea_bootstrap: can't get mmu package"); 8455244eac9SBenno Rice if ((sz = OF_getproplen(mmu, "translations")) == -1) 84659276937SPeter Grehan panic("moea_bootstrap: can't get ofw translation count"); 847aa39961eSBenno Rice translations = NULL; 8486cc1cdf4SPeter Grehan for (i = 0; phys_avail[i] != 0; i += 2) { 8496cc1cdf4SPeter Grehan if (phys_avail[i + 1] >= sz) { 850aa39961eSBenno Rice translations = (struct ofw_map *)phys_avail[i]; 8516cc1cdf4SPeter Grehan break; 8526cc1cdf4SPeter Grehan } 853aa39961eSBenno Rice } 854aa39961eSBenno Rice if (translations == NULL) 85559276937SPeter Grehan panic("moea_bootstrap: no space to copy translations"); 8565244eac9SBenno Rice bzero(translations, sz); 8575244eac9SBenno Rice if (OF_getprop(mmu, "translations", translations, sz) == -1) 85859276937SPeter Grehan panic("moea_bootstrap: can't get ofw translations"); 85959276937SPeter Grehan CTR0(KTR_PMAP, "moea_bootstrap: translations"); 86031c82d03SBenno Rice sz /= sizeof(*translations); 8615244eac9SBenno Rice qsort(translations, sz, sizeof (*translations), om_cmp); 86232bc7846SPeter Grehan for (i = 0, ofw_mappings = 0; i < sz; i++) { 8635244eac9SBenno Rice CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x", 8645244eac9SBenno Rice translations[i].om_pa, translations[i].om_va, 8655244eac9SBenno Rice translations[i].om_len); 8665244eac9SBenno Rice 86732bc7846SPeter Grehan /* 86832bc7846SPeter Grehan * If the mapping is 1:1, let the RAM and device on-demand 86932bc7846SPeter Grehan * BAT tables take care of the translation. 87032bc7846SPeter Grehan */ 87132bc7846SPeter Grehan if (translations[i].om_va == translations[i].om_pa) 87232bc7846SPeter Grehan continue; 8735244eac9SBenno Rice 87432bc7846SPeter Grehan /* Enter the pages */ 8755244eac9SBenno Rice for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) { 8765244eac9SBenno Rice struct vm_page m; 8775244eac9SBenno Rice 8785244eac9SBenno Rice m.phys_addr = translations[i].om_pa + off; 8799ab6032fSAlan Cox m.oflags = VPO_BUSY; 8805ce609a3SRink Springer PMAP_LOCK(&ofw_pmap); 881ce142d9eSAlan Cox moea_enter_locked(&ofw_pmap, 88259276937SPeter Grehan translations[i].om_va + off, &m, 8835244eac9SBenno Rice VM_PROT_ALL, 1); 8845ce609a3SRink Springer PMAP_UNLOCK(&ofw_pmap); 88532bc7846SPeter Grehan ofw_mappings++; 886f9bac91bSBenno Rice } 887f9bac91bSBenno Rice } 888014ffa99SMarcel Moolenaar 889014ffa99SMarcel Moolenaar /* 890014ffa99SMarcel Moolenaar * Calculate the last available physical address. 891014ffa99SMarcel Moolenaar */ 892014ffa99SMarcel Moolenaar for (i = 0; phys_avail[i + 2] != 0; i += 2) 893014ffa99SMarcel Moolenaar ; 894014ffa99SMarcel Moolenaar Maxmem = powerpc_btop(phys_avail[i + 1]); 8955244eac9SBenno Rice 8965244eac9SBenno Rice /* 8975244eac9SBenno Rice * Initialize the kernel pmap (which is statically allocated). 8985244eac9SBenno Rice */ 89948d0b1a0SAlan Cox PMAP_LOCK_INIT(kernel_pmap); 9005244eac9SBenno Rice for (i = 0; i < 16; i++) { 9015244eac9SBenno Rice kernel_pmap->pm_sr[i] = EMPTY_SEGMENT; 902f9bac91bSBenno Rice } 9035244eac9SBenno Rice kernel_pmap->pm_sr[KERNEL_SR] = KERNEL_SEGMENT; 90422f2fe59SPeter Grehan kernel_pmap->pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT; 9055244eac9SBenno Rice kernel_pmap->pm_active = ~0; 9065244eac9SBenno Rice 9071c96bdd1SNathan Whitehorn moea_cpu_bootstrap(mmup,0); 9085244eac9SBenno Rice 9095244eac9SBenno Rice pmap_bootstrapped++; 910014ffa99SMarcel Moolenaar 911014ffa99SMarcel Moolenaar /* 912014ffa99SMarcel Moolenaar * Set the start and end of kva. 913014ffa99SMarcel Moolenaar */ 914014ffa99SMarcel Moolenaar virtual_avail = VM_MIN_KERNEL_ADDRESS; 915ab739706SNathan Whitehorn virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS; 916014ffa99SMarcel Moolenaar 917014ffa99SMarcel Moolenaar /* 918014ffa99SMarcel Moolenaar * Allocate a kernel stack with a guard page for thread0 and map it 919014ffa99SMarcel Moolenaar * into the kernel page map. 920014ffa99SMarcel Moolenaar */ 921014ffa99SMarcel Moolenaar pa = moea_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE); 922014ffa99SMarcel Moolenaar va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE; 923014ffa99SMarcel Moolenaar virtual_avail = va + KSTACK_PAGES * PAGE_SIZE; 924014ffa99SMarcel Moolenaar CTR2(KTR_PMAP, "moea_bootstrap: kstack0 at %#x (%#x)", pa, va); 925014ffa99SMarcel Moolenaar thread0.td_kstack = va; 926014ffa99SMarcel Moolenaar thread0.td_kstack_pages = KSTACK_PAGES; 927014ffa99SMarcel Moolenaar for (i = 0; i < KSTACK_PAGES; i++) { 928c2ede4b3SMartin Blapp moea_kenter(mmup, va, pa); 929014ffa99SMarcel Moolenaar pa += PAGE_SIZE; 930014ffa99SMarcel Moolenaar va += PAGE_SIZE; 931014ffa99SMarcel Moolenaar } 932014ffa99SMarcel Moolenaar 933014ffa99SMarcel Moolenaar /* 934014ffa99SMarcel Moolenaar * Allocate virtual address space for the message buffer. 935014ffa99SMarcel Moolenaar */ 936014ffa99SMarcel Moolenaar pa = msgbuf_phys = moea_bootstrap_alloc(MSGBUF_SIZE, PAGE_SIZE); 937014ffa99SMarcel Moolenaar msgbufp = (struct msgbuf *)virtual_avail; 938014ffa99SMarcel Moolenaar va = virtual_avail; 939014ffa99SMarcel Moolenaar virtual_avail += round_page(MSGBUF_SIZE); 940014ffa99SMarcel Moolenaar while (va < virtual_avail) { 941c2ede4b3SMartin Blapp moea_kenter(mmup, va, pa); 942014ffa99SMarcel Moolenaar pa += PAGE_SIZE; 943014ffa99SMarcel Moolenaar va += PAGE_SIZE; 944014ffa99SMarcel Moolenaar } 94550c202c5SJeff Roberson 94650c202c5SJeff Roberson /* 94750c202c5SJeff Roberson * Allocate virtual address space for the dynamic percpu area. 94850c202c5SJeff Roberson */ 94950c202c5SJeff Roberson pa = moea_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE); 95050c202c5SJeff Roberson dpcpu = (void *)virtual_avail; 95150c202c5SJeff Roberson va = virtual_avail; 95250c202c5SJeff Roberson virtual_avail += DPCPU_SIZE; 95350c202c5SJeff Roberson while (va < virtual_avail) { 954c2ede4b3SMartin Blapp moea_kenter(mmup, va, pa); 95550c202c5SJeff Roberson pa += PAGE_SIZE; 95650c202c5SJeff Roberson va += PAGE_SIZE; 95750c202c5SJeff Roberson } 95850c202c5SJeff Roberson dpcpu_init(dpcpu, 0); 9595244eac9SBenno Rice } 9605244eac9SBenno Rice 9615244eac9SBenno Rice /* 9625244eac9SBenno Rice * Activate a user pmap. The pmap must be activated before it's address 9635244eac9SBenno Rice * space can be accessed in any way. 964f9bac91bSBenno Rice */ 965f9bac91bSBenno Rice void 96659276937SPeter Grehan moea_activate(mmu_t mmu, struct thread *td) 967f9bac91bSBenno Rice { 9688207b362SBenno Rice pmap_t pm, pmr; 969f9bac91bSBenno Rice 970f9bac91bSBenno Rice /* 97132bc7846SPeter Grehan * Load all the data we need up front to encourage the compiler to 9725244eac9SBenno Rice * not issue any loads while we have interrupts disabled below. 973f9bac91bSBenno Rice */ 9745244eac9SBenno Rice pm = &td->td_proc->p_vmspace->vm_pmap; 97552a7870dSNathan Whitehorn pmr = pm->pmap_phys; 9768207b362SBenno Rice 9775244eac9SBenno Rice pm->pm_active |= PCPU_GET(cpumask); 9788207b362SBenno Rice PCPU_SET(curpmap, pmr); 979ac6ba8bdSBenno Rice } 980ac6ba8bdSBenno Rice 981ac6ba8bdSBenno Rice void 98259276937SPeter Grehan moea_deactivate(mmu_t mmu, struct thread *td) 983ac6ba8bdSBenno Rice { 984ac6ba8bdSBenno Rice pmap_t pm; 985ac6ba8bdSBenno Rice 986ac6ba8bdSBenno Rice pm = &td->td_proc->p_vmspace->vm_pmap; 987e4f72b32SMarcel Moolenaar pm->pm_active &= ~PCPU_GET(cpumask); 9888207b362SBenno Rice PCPU_SET(curpmap, NULL); 989f9bac91bSBenno Rice } 990f9bac91bSBenno Rice 991f9bac91bSBenno Rice void 99259276937SPeter Grehan moea_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired) 993f9bac91bSBenno Rice { 9940f92104cSBenno Rice struct pvo_entry *pvo; 9950f92104cSBenno Rice 99648d0b1a0SAlan Cox PMAP_LOCK(pm); 99759276937SPeter Grehan pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL); 9980f92104cSBenno Rice 9990f92104cSBenno Rice if (pvo != NULL) { 10000f92104cSBenno Rice if (wired) { 10010f92104cSBenno Rice if ((pvo->pvo_vaddr & PVO_WIRED) == 0) 10020f92104cSBenno Rice pm->pm_stats.wired_count++; 10030f92104cSBenno Rice pvo->pvo_vaddr |= PVO_WIRED; 10040f92104cSBenno Rice } else { 10050f92104cSBenno Rice if ((pvo->pvo_vaddr & PVO_WIRED) != 0) 10060f92104cSBenno Rice pm->pm_stats.wired_count--; 10070f92104cSBenno Rice pvo->pvo_vaddr &= ~PVO_WIRED; 10080f92104cSBenno Rice } 10090f92104cSBenno Rice } 101048d0b1a0SAlan Cox PMAP_UNLOCK(pm); 1011f9bac91bSBenno Rice } 1012f9bac91bSBenno Rice 1013f9bac91bSBenno Rice void 101459276937SPeter Grehan moea_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst) 1015f9bac91bSBenno Rice { 101625e2288dSBenno Rice vm_offset_t dst; 101725e2288dSBenno Rice vm_offset_t src; 101825e2288dSBenno Rice 101925e2288dSBenno Rice dst = VM_PAGE_TO_PHYS(mdst); 102025e2288dSBenno Rice src = VM_PAGE_TO_PHYS(msrc); 102125e2288dSBenno Rice 102225e2288dSBenno Rice kcopy((void *)src, (void *)dst, PAGE_SIZE); 1023f9bac91bSBenno Rice } 1024111c77dcSBenno Rice 1025111c77dcSBenno Rice /* 10265244eac9SBenno Rice * Zero a page of physical memory by temporarily mapping it into the tlb. 10275244eac9SBenno Rice */ 10285244eac9SBenno Rice void 102959276937SPeter Grehan moea_zero_page(mmu_t mmu, vm_page_t m) 10305244eac9SBenno Rice { 10311a87a0daSPeter Wemm vm_offset_t pa = VM_PAGE_TO_PHYS(m); 10325b43c63dSMarcel Moolenaar void *va = (void *)pa; 10335244eac9SBenno Rice 10345244eac9SBenno Rice bzero(va, PAGE_SIZE); 10355244eac9SBenno Rice } 10365244eac9SBenno Rice 10375244eac9SBenno Rice void 103859276937SPeter Grehan moea_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size) 10395244eac9SBenno Rice { 10403495845eSBenno Rice vm_offset_t pa = VM_PAGE_TO_PHYS(m); 10415b43c63dSMarcel Moolenaar void *va = (void *)(pa + off); 10423495845eSBenno Rice 10435b43c63dSMarcel Moolenaar bzero(va, size); 10445244eac9SBenno Rice } 10455244eac9SBenno Rice 1046a58b3a68SPeter Wemm void 104759276937SPeter Grehan moea_zero_page_idle(mmu_t mmu, vm_page_t m) 1048a58b3a68SPeter Wemm { 10495b43c63dSMarcel Moolenaar vm_offset_t pa = VM_PAGE_TO_PHYS(m); 10505b43c63dSMarcel Moolenaar void *va = (void *)pa; 1051a58b3a68SPeter Wemm 10525b43c63dSMarcel Moolenaar bzero(va, PAGE_SIZE); 1053a58b3a68SPeter Wemm } 1054a58b3a68SPeter Wemm 10555244eac9SBenno Rice /* 10565244eac9SBenno Rice * Map the given physical page at the specified virtual address in the 10575244eac9SBenno Rice * target pmap with the protection requested. If specified the page 10585244eac9SBenno Rice * will be wired down. 10595244eac9SBenno Rice */ 10605244eac9SBenno Rice void 106159276937SPeter Grehan moea_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, 10625244eac9SBenno Rice boolean_t wired) 10635244eac9SBenno Rice { 1064ce142d9eSAlan Cox 1065ce142d9eSAlan Cox vm_page_lock_queues(); 1066ce142d9eSAlan Cox PMAP_LOCK(pmap); 106767c867eeSAlan Cox moea_enter_locked(pmap, va, m, prot, wired); 1068ce142d9eSAlan Cox vm_page_unlock_queues(); 1069ce142d9eSAlan Cox PMAP_UNLOCK(pmap); 1070ce142d9eSAlan Cox } 1071ce142d9eSAlan Cox 1072ce142d9eSAlan Cox /* 1073ce142d9eSAlan Cox * Map the given physical page at the specified virtual address in the 1074ce142d9eSAlan Cox * target pmap with the protection requested. If specified the page 1075ce142d9eSAlan Cox * will be wired down. 1076ce142d9eSAlan Cox * 1077ce142d9eSAlan Cox * The page queues and pmap must be locked. 1078ce142d9eSAlan Cox */ 1079ce142d9eSAlan Cox static void 1080ce142d9eSAlan Cox moea_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, 1081ce142d9eSAlan Cox boolean_t wired) 1082ce142d9eSAlan Cox { 10835244eac9SBenno Rice struct pvo_head *pvo_head; 1084378862a7SJeff Roberson uma_zone_t zone; 10858207b362SBenno Rice vm_page_t pg; 10868207b362SBenno Rice u_int pte_lo, pvo_flags, was_exec, i; 10875244eac9SBenno Rice int error; 10885244eac9SBenno Rice 108959276937SPeter Grehan if (!moea_initialized) { 109059276937SPeter Grehan pvo_head = &moea_pvo_kunmanaged; 109159276937SPeter Grehan zone = moea_upvo_zone; 10925244eac9SBenno Rice pvo_flags = 0; 10938207b362SBenno Rice pg = NULL; 10948207b362SBenno Rice was_exec = PTE_EXEC; 10955244eac9SBenno Rice } else { 109603b6e025SPeter Grehan pvo_head = vm_page_to_pvoh(m); 109703b6e025SPeter Grehan pg = m; 109859276937SPeter Grehan zone = moea_mpvo_zone; 10995244eac9SBenno Rice pvo_flags = PVO_MANAGED; 11008207b362SBenno Rice was_exec = 0; 11015244eac9SBenno Rice } 1102f489bf21SAlan Cox if (pmap_bootstrapped) 1103ce142d9eSAlan Cox mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1104ce142d9eSAlan Cox PMAP_LOCK_ASSERT(pmap, MA_OWNED); 11059ab6032fSAlan Cox KASSERT((m->oflags & VPO_BUSY) != 0 || VM_OBJECT_LOCKED(m->object), 11069ab6032fSAlan Cox ("moea_enter_locked: page %p is not busy", m)); 11075244eac9SBenno Rice 11084dba5df1SPeter Grehan /* XXX change the pvo head for fake pages */ 1109a130b35fSNathan Whitehorn if ((m->flags & PG_FICTITIOUS) == PG_FICTITIOUS) { 1110a130b35fSNathan Whitehorn pvo_flags &= ~PVO_MANAGED; 111159276937SPeter Grehan pvo_head = &moea_pvo_kunmanaged; 1112a130b35fSNathan Whitehorn zone = moea_upvo_zone; 1113a130b35fSNathan Whitehorn } 11144dba5df1SPeter Grehan 11158207b362SBenno Rice /* 11168207b362SBenno Rice * If this is a managed page, and it's the first reference to the page, 11178207b362SBenno Rice * clear the execness of the page. Otherwise fetch the execness. 11188207b362SBenno Rice */ 11194dba5df1SPeter Grehan if ((pg != NULL) && ((m->flags & PG_FICTITIOUS) == 0)) { 11208207b362SBenno Rice if (LIST_EMPTY(pvo_head)) { 112159276937SPeter Grehan moea_attr_clear(pg, PTE_EXEC); 11228207b362SBenno Rice } else { 112359276937SPeter Grehan was_exec = moea_attr_fetch(pg) & PTE_EXEC; 11248207b362SBenno Rice } 11258207b362SBenno Rice } 11268207b362SBenno Rice 11278207b362SBenno Rice /* 11288207b362SBenno Rice * Assume the page is cache inhibited and access is guarded unless 11298207b362SBenno Rice * it's in our available memory array. 11308207b362SBenno Rice */ 11315244eac9SBenno Rice pte_lo = PTE_I | PTE_G; 113231c82d03SBenno Rice for (i = 0; i < pregions_sz; i++) { 113331c82d03SBenno Rice if ((VM_PAGE_TO_PHYS(m) >= pregions[i].mr_start) && 113431c82d03SBenno Rice (VM_PAGE_TO_PHYS(m) < 113531c82d03SBenno Rice (pregions[i].mr_start + pregions[i].mr_size))) { 1136e4f72b32SMarcel Moolenaar pte_lo = PTE_M; 11378207b362SBenno Rice break; 11388207b362SBenno Rice } 11398207b362SBenno Rice } 11405244eac9SBenno Rice 114144b8bd66SAlan Cox if (prot & VM_PROT_WRITE) { 11425244eac9SBenno Rice pte_lo |= PTE_BW; 11432368a371SAlan Cox if (pmap_bootstrapped && 11442368a371SAlan Cox (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) 114544b8bd66SAlan Cox vm_page_flag_set(m, PG_WRITEABLE); 114644b8bd66SAlan Cox } else 11475244eac9SBenno Rice pte_lo |= PTE_BR; 11485244eac9SBenno Rice 11494dba5df1SPeter Grehan if (prot & VM_PROT_EXECUTE) 11504dba5df1SPeter Grehan pvo_flags |= PVO_EXECUTABLE; 11515244eac9SBenno Rice 11525244eac9SBenno Rice if (wired) 11535244eac9SBenno Rice pvo_flags |= PVO_WIRED; 11545244eac9SBenno Rice 11554dba5df1SPeter Grehan if ((m->flags & PG_FICTITIOUS) != 0) 11564dba5df1SPeter Grehan pvo_flags |= PVO_FAKE; 11574dba5df1SPeter Grehan 115859276937SPeter Grehan error = moea_pvo_enter(pmap, zone, pvo_head, va, VM_PAGE_TO_PHYS(m), 11598207b362SBenno Rice pte_lo, pvo_flags); 11605244eac9SBenno Rice 11618207b362SBenno Rice /* 11628207b362SBenno Rice * Flush the real page from the instruction cache if this page is 11638207b362SBenno Rice * mapped executable and cacheable and was not previously mapped (or 11648207b362SBenno Rice * was not mapped executable). 11658207b362SBenno Rice */ 11668207b362SBenno Rice if (error == 0 && (pvo_flags & PVO_EXECUTABLE) && 11678207b362SBenno Rice (pte_lo & PTE_I) == 0 && was_exec == 0) { 11685244eac9SBenno Rice /* 11695244eac9SBenno Rice * Flush the real memory from the cache. 11705244eac9SBenno Rice */ 117159276937SPeter Grehan moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE); 11728207b362SBenno Rice if (pg != NULL) 117359276937SPeter Grehan moea_attr_save(pg, PTE_EXEC); 11745244eac9SBenno Rice } 117532bc7846SPeter Grehan 117632bc7846SPeter Grehan /* XXX syncicache always until problems are sorted */ 117759276937SPeter Grehan moea_syncicache(VM_PAGE_TO_PHYS(m), PAGE_SIZE); 1178ce142d9eSAlan Cox } 1179ce142d9eSAlan Cox 1180ce142d9eSAlan Cox /* 1181ce142d9eSAlan Cox * Maps a sequence of resident pages belonging to the same object. 1182ce142d9eSAlan Cox * The sequence begins with the given page m_start. This page is 1183ce142d9eSAlan Cox * mapped at the given virtual address start. Each subsequent page is 1184ce142d9eSAlan Cox * mapped at a virtual address that is offset from start by the same 1185ce142d9eSAlan Cox * amount as the page is offset from m_start within the object. The 1186ce142d9eSAlan Cox * last page in the sequence is the page with the largest offset from 1187ce142d9eSAlan Cox * m_start that can be mapped at a virtual address less than the given 1188ce142d9eSAlan Cox * virtual address end. Not every virtual page between start and end 1189ce142d9eSAlan Cox * is mapped; only those for which a resident page exists with the 1190ce142d9eSAlan Cox * corresponding offset from m_start are mapped. 1191ce142d9eSAlan Cox */ 1192ce142d9eSAlan Cox void 1193ce142d9eSAlan Cox moea_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end, 1194ce142d9eSAlan Cox vm_page_t m_start, vm_prot_t prot) 1195ce142d9eSAlan Cox { 1196ce142d9eSAlan Cox vm_page_t m; 1197ce142d9eSAlan Cox vm_pindex_t diff, psize; 1198ce142d9eSAlan Cox 1199ce142d9eSAlan Cox psize = atop(end - start); 1200ce142d9eSAlan Cox m = m_start; 1201c46b90e9SAlan Cox vm_page_lock_queues(); 1202ce142d9eSAlan Cox PMAP_LOCK(pm); 1203ce142d9eSAlan Cox while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) { 1204ce142d9eSAlan Cox moea_enter_locked(pm, start + ptoa(diff), m, prot & 1205ce142d9eSAlan Cox (VM_PROT_READ | VM_PROT_EXECUTE), FALSE); 1206ce142d9eSAlan Cox m = TAILQ_NEXT(m, listq); 1207ce142d9eSAlan Cox } 1208c46b90e9SAlan Cox vm_page_unlock_queues(); 1209ce142d9eSAlan Cox PMAP_UNLOCK(pm); 12105244eac9SBenno Rice } 12115244eac9SBenno Rice 12122053c127SStephan Uphoff void 121359276937SPeter Grehan moea_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m, 12142053c127SStephan Uphoff vm_prot_t prot) 1215dca96f1aSAlan Cox { 1216dca96f1aSAlan Cox 12173c4a2440SAlan Cox vm_page_lock_queues(); 1218ce142d9eSAlan Cox PMAP_LOCK(pm); 1219ce142d9eSAlan Cox moea_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE), 122059276937SPeter Grehan FALSE); 12213c4a2440SAlan Cox vm_page_unlock_queues(); 1222ce142d9eSAlan Cox PMAP_UNLOCK(pm); 1223dca96f1aSAlan Cox } 1224dca96f1aSAlan Cox 122556b09388SAlan Cox vm_paddr_t 122659276937SPeter Grehan moea_extract(mmu_t mmu, pmap_t pm, vm_offset_t va) 12275244eac9SBenno Rice { 12280f92104cSBenno Rice struct pvo_entry *pvo; 122948d0b1a0SAlan Cox vm_paddr_t pa; 12300f92104cSBenno Rice 123148d0b1a0SAlan Cox PMAP_LOCK(pm); 123259276937SPeter Grehan pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL); 123348d0b1a0SAlan Cox if (pvo == NULL) 123448d0b1a0SAlan Cox pa = 0; 123548d0b1a0SAlan Cox else 123652a7870dSNathan Whitehorn pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF); 123748d0b1a0SAlan Cox PMAP_UNLOCK(pm); 123848d0b1a0SAlan Cox return (pa); 12395244eac9SBenno Rice } 12405244eac9SBenno Rice 12415244eac9SBenno Rice /* 124284792e72SPeter Grehan * Atomically extract and hold the physical page with the given 124384792e72SPeter Grehan * pmap and virtual address pair if that mapping permits the given 124484792e72SPeter Grehan * protection. 124584792e72SPeter Grehan */ 124684792e72SPeter Grehan vm_page_t 124759276937SPeter Grehan moea_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot) 124884792e72SPeter Grehan { 1249ab50a262SAlan Cox struct pvo_entry *pvo; 125084792e72SPeter Grehan vm_page_t m; 12512965a453SKip Macy vm_paddr_t pa; 125284792e72SPeter Grehan 125384792e72SPeter Grehan m = NULL; 12542965a453SKip Macy pa = 0; 125548d0b1a0SAlan Cox PMAP_LOCK(pmap); 12562965a453SKip Macy retry: 125759276937SPeter Grehan pvo = moea_pvo_find_va(pmap, va & ~ADDR_POFF, NULL); 125852a7870dSNathan Whitehorn if (pvo != NULL && (pvo->pvo_pte.pte.pte_hi & PTE_VALID) && 125952a7870dSNathan Whitehorn ((pvo->pvo_pte.pte.pte_lo & PTE_PP) == PTE_RW || 1260ab50a262SAlan Cox (prot & VM_PROT_WRITE) == 0)) { 12612965a453SKip Macy if (vm_page_pa_tryrelock(pmap, pvo->pvo_pte.pte.pte_lo & PTE_RPGN, &pa)) 12622965a453SKip Macy goto retry; 126352a7870dSNathan Whitehorn m = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN); 126484792e72SPeter Grehan vm_page_hold(m); 126584792e72SPeter Grehan } 12662965a453SKip Macy PA_UNLOCK_COND(pa); 126748d0b1a0SAlan Cox PMAP_UNLOCK(pmap); 126884792e72SPeter Grehan return (m); 126984792e72SPeter Grehan } 127084792e72SPeter Grehan 12715244eac9SBenno Rice void 127259276937SPeter Grehan moea_init(mmu_t mmu) 12735244eac9SBenno Rice { 12745244eac9SBenno Rice 127559276937SPeter Grehan moea_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry), 12760ee6dbd7SPeter Grehan NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 12770ee6dbd7SPeter Grehan UMA_ZONE_VM | UMA_ZONE_NOFREE); 127859276937SPeter Grehan moea_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry), 12790ee6dbd7SPeter Grehan NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 12800ee6dbd7SPeter Grehan UMA_ZONE_VM | UMA_ZONE_NOFREE); 128159276937SPeter Grehan moea_initialized = TRUE; 12825244eac9SBenno Rice } 12835244eac9SBenno Rice 12845244eac9SBenno Rice boolean_t 12857b85f591SAlan Cox moea_is_referenced(mmu_t mmu, vm_page_t m) 12867b85f591SAlan Cox { 12877b85f591SAlan Cox 1288c46b90e9SAlan Cox KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0, 1289c46b90e9SAlan Cox ("moea_is_referenced: page %p is not managed", m)); 12907b85f591SAlan Cox return (moea_query_bit(m, PTE_REF)); 12917b85f591SAlan Cox } 12927b85f591SAlan Cox 12937b85f591SAlan Cox boolean_t 129459276937SPeter Grehan moea_is_modified(mmu_t mmu, vm_page_t m) 12955244eac9SBenno Rice { 12960f92104cSBenno Rice 1297567e51e1SAlan Cox KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0, 1298567e51e1SAlan Cox ("moea_is_modified: page %p is not managed", m)); 1299567e51e1SAlan Cox 1300567e51e1SAlan Cox /* 1301567e51e1SAlan Cox * If the page is not VPO_BUSY, then PG_WRITEABLE cannot be 1302567e51e1SAlan Cox * concurrently set while the object is locked. Thus, if PG_WRITEABLE 1303567e51e1SAlan Cox * is clear, no PTEs can have PTE_CHG set. 1304567e51e1SAlan Cox */ 1305567e51e1SAlan Cox VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1306567e51e1SAlan Cox if ((m->oflags & VPO_BUSY) == 0 && 1307567e51e1SAlan Cox (m->flags & PG_WRITEABLE) == 0) 13080f92104cSBenno Rice return (FALSE); 1309c46b90e9SAlan Cox return (moea_query_bit(m, PTE_CHG)); 1310566526a9SAlan Cox } 1311566526a9SAlan Cox 13125244eac9SBenno Rice void 131359276937SPeter Grehan moea_clear_reference(mmu_t mmu, vm_page_t m) 13145244eac9SBenno Rice { 131503b6e025SPeter Grehan 1316567e51e1SAlan Cox KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0, 1317567e51e1SAlan Cox ("moea_clear_reference: page %p is not managed", m)); 1318*ce186587SAlan Cox moea_clear_bit(m, PTE_REF); 131903b6e025SPeter Grehan } 132003b6e025SPeter Grehan 132103b6e025SPeter Grehan void 132259276937SPeter Grehan moea_clear_modify(mmu_t mmu, vm_page_t m) 132303b6e025SPeter Grehan { 132403b6e025SPeter Grehan 1325567e51e1SAlan Cox KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0, 1326567e51e1SAlan Cox ("moea_clear_modify: page %p is not managed", m)); 1327567e51e1SAlan Cox VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1328567e51e1SAlan Cox KASSERT((m->oflags & VPO_BUSY) == 0, 1329567e51e1SAlan Cox ("moea_clear_modify: page %p is busy", m)); 1330567e51e1SAlan Cox 1331567e51e1SAlan Cox /* 1332567e51e1SAlan Cox * If the page is not PG_WRITEABLE, then no PTEs can have PTE_CHG 1333567e51e1SAlan Cox * set. If the object containing the page is locked and the page is 1334567e51e1SAlan Cox * not VPO_BUSY, then PG_WRITEABLE cannot be concurrently set. 1335567e51e1SAlan Cox */ 1336567e51e1SAlan Cox if ((m->flags & PG_WRITEABLE) == 0) 133703b6e025SPeter Grehan return; 1338*ce186587SAlan Cox moea_clear_bit(m, PTE_CHG); 13395244eac9SBenno Rice } 13405244eac9SBenno Rice 13417f3a4093SMike Silbersack /* 134278985e42SAlan Cox * Clear the write and modified bits in each of the given page's mappings. 134378985e42SAlan Cox */ 134478985e42SAlan Cox void 134578985e42SAlan Cox moea_remove_write(mmu_t mmu, vm_page_t m) 134678985e42SAlan Cox { 134778985e42SAlan Cox struct pvo_entry *pvo; 134878985e42SAlan Cox struct pte *pt; 134978985e42SAlan Cox pmap_t pmap; 135078985e42SAlan Cox u_int lo; 135178985e42SAlan Cox 13529ab6032fSAlan Cox KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0, 13539ab6032fSAlan Cox ("moea_remove_write: page %p is not managed", m)); 13549ab6032fSAlan Cox 13559ab6032fSAlan Cox /* 13569ab6032fSAlan Cox * If the page is not VPO_BUSY, then PG_WRITEABLE cannot be set by 13579ab6032fSAlan Cox * another thread while the object is locked. Thus, if PG_WRITEABLE 13589ab6032fSAlan Cox * is clear, no page table entries need updating. 13599ab6032fSAlan Cox */ 13609ab6032fSAlan Cox VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 13619ab6032fSAlan Cox if ((m->oflags & VPO_BUSY) == 0 && 136278985e42SAlan Cox (m->flags & PG_WRITEABLE) == 0) 136378985e42SAlan Cox return; 13643c4a2440SAlan Cox vm_page_lock_queues(); 136578985e42SAlan Cox lo = moea_attr_fetch(m); 1366e4f72b32SMarcel Moolenaar powerpc_sync(); 136778985e42SAlan Cox LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 136878985e42SAlan Cox pmap = pvo->pvo_pmap; 136978985e42SAlan Cox PMAP_LOCK(pmap); 137052a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_lo & PTE_PP) != PTE_BR) { 137178985e42SAlan Cox pt = moea_pvo_to_pte(pvo, -1); 137252a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~PTE_PP; 137352a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo |= PTE_BR; 137478985e42SAlan Cox if (pt != NULL) { 137552a7870dSNathan Whitehorn moea_pte_synch(pt, &pvo->pvo_pte.pte); 137652a7870dSNathan Whitehorn lo |= pvo->pvo_pte.pte.pte_lo; 137752a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~PTE_CHG; 137852a7870dSNathan Whitehorn moea_pte_change(pt, &pvo->pvo_pte.pte, 137978985e42SAlan Cox pvo->pvo_vaddr); 138078985e42SAlan Cox mtx_unlock(&moea_table_mutex); 138178985e42SAlan Cox } 138278985e42SAlan Cox } 138378985e42SAlan Cox PMAP_UNLOCK(pmap); 138478985e42SAlan Cox } 138578985e42SAlan Cox if ((lo & PTE_CHG) != 0) { 138678985e42SAlan Cox moea_attr_clear(m, PTE_CHG); 138778985e42SAlan Cox vm_page_dirty(m); 138878985e42SAlan Cox } 138978985e42SAlan Cox vm_page_flag_clear(m, PG_WRITEABLE); 13903c4a2440SAlan Cox vm_page_unlock_queues(); 139178985e42SAlan Cox } 139278985e42SAlan Cox 139378985e42SAlan Cox /* 139459276937SPeter Grehan * moea_ts_referenced: 13957f3a4093SMike Silbersack * 13967f3a4093SMike Silbersack * Return a count of reference bits for a page, clearing those bits. 13977f3a4093SMike Silbersack * It is not necessary for every reference bit to be cleared, but it 13987f3a4093SMike Silbersack * is necessary that 0 only be returned when there are truly no 13997f3a4093SMike Silbersack * reference bits set. 14007f3a4093SMike Silbersack * 14017f3a4093SMike Silbersack * XXX: The exact number of bits to check and clear is a matter that 14027f3a4093SMike Silbersack * should be tested and standardized at some point in the future for 14037f3a4093SMike Silbersack * optimal aging of shared pages. 14047f3a4093SMike Silbersack */ 140559276937SPeter Grehan boolean_t 140659276937SPeter Grehan moea_ts_referenced(mmu_t mmu, vm_page_t m) 14075244eac9SBenno Rice { 140803b6e025SPeter Grehan 1409*ce186587SAlan Cox KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0, 1410*ce186587SAlan Cox ("moea_ts_referenced: page %p is not managed", m)); 1411*ce186587SAlan Cox return (moea_clear_bit(m, PTE_REF)); 14125244eac9SBenno Rice } 14135244eac9SBenno Rice 14145244eac9SBenno Rice /* 14155244eac9SBenno Rice * Map a wired page into kernel virtual address space. 14165244eac9SBenno Rice */ 14175244eac9SBenno Rice void 141859276937SPeter Grehan moea_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa) 14195244eac9SBenno Rice { 14205244eac9SBenno Rice u_int pte_lo; 14215244eac9SBenno Rice int error; 14225244eac9SBenno Rice int i; 14235244eac9SBenno Rice 14245244eac9SBenno Rice #if 0 14255244eac9SBenno Rice if (va < VM_MIN_KERNEL_ADDRESS) 142659276937SPeter Grehan panic("moea_kenter: attempt to enter non-kernel address %#x", 14275244eac9SBenno Rice va); 14285244eac9SBenno Rice #endif 14295244eac9SBenno Rice 143032bc7846SPeter Grehan pte_lo = PTE_I | PTE_G; 143132bc7846SPeter Grehan for (i = 0; i < pregions_sz; i++) { 143232bc7846SPeter Grehan if ((pa >= pregions[i].mr_start) && 143332bc7846SPeter Grehan (pa < (pregions[i].mr_start + pregions[i].mr_size))) { 1434e4f72b32SMarcel Moolenaar pte_lo = PTE_M; 14355244eac9SBenno Rice break; 14365244eac9SBenno Rice } 14375244eac9SBenno Rice } 14385244eac9SBenno Rice 14394711f8d7SAlan Cox PMAP_LOCK(kernel_pmap); 144059276937SPeter Grehan error = moea_pvo_enter(kernel_pmap, moea_upvo_zone, 144159276937SPeter Grehan &moea_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED); 14425244eac9SBenno Rice 14435244eac9SBenno Rice if (error != 0 && error != ENOENT) 144459276937SPeter Grehan panic("moea_kenter: failed to enter va %#x pa %#x: %d", va, 14455244eac9SBenno Rice pa, error); 14465244eac9SBenno Rice 14475244eac9SBenno Rice /* 14485244eac9SBenno Rice * Flush the real memory from the instruction cache. 14495244eac9SBenno Rice */ 14505244eac9SBenno Rice if ((pte_lo & (PTE_I | PTE_G)) == 0) { 145159276937SPeter Grehan moea_syncicache(pa, PAGE_SIZE); 14525244eac9SBenno Rice } 14534711f8d7SAlan Cox PMAP_UNLOCK(kernel_pmap); 14545244eac9SBenno Rice } 14555244eac9SBenno Rice 1456e79f59e8SBenno Rice /* 1457e79f59e8SBenno Rice * Extract the physical page address associated with the given kernel virtual 1458e79f59e8SBenno Rice * address. 1459e79f59e8SBenno Rice */ 14605244eac9SBenno Rice vm_offset_t 146159276937SPeter Grehan moea_kextract(mmu_t mmu, vm_offset_t va) 14625244eac9SBenno Rice { 1463e79f59e8SBenno Rice struct pvo_entry *pvo; 146448d0b1a0SAlan Cox vm_paddr_t pa; 1465e79f59e8SBenno Rice 14660efd0097SPeter Grehan /* 146752a7870dSNathan Whitehorn * Allow direct mappings on 32-bit OEA 14680efd0097SPeter Grehan */ 14690efd0097SPeter Grehan if (va < VM_MIN_KERNEL_ADDRESS) { 14700efd0097SPeter Grehan return (va); 14710efd0097SPeter Grehan } 14720efd0097SPeter Grehan 147348d0b1a0SAlan Cox PMAP_LOCK(kernel_pmap); 147459276937SPeter Grehan pvo = moea_pvo_find_va(kernel_pmap, va & ~ADDR_POFF, NULL); 147559276937SPeter Grehan KASSERT(pvo != NULL, ("moea_kextract: no addr found")); 147652a7870dSNathan Whitehorn pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | (va & ADDR_POFF); 147748d0b1a0SAlan Cox PMAP_UNLOCK(kernel_pmap); 147848d0b1a0SAlan Cox return (pa); 1479e79f59e8SBenno Rice } 1480e79f59e8SBenno Rice 148188afb2a3SBenno Rice /* 148288afb2a3SBenno Rice * Remove a wired page from kernel virtual address space. 148388afb2a3SBenno Rice */ 14845244eac9SBenno Rice void 148559276937SPeter Grehan moea_kremove(mmu_t mmu, vm_offset_t va) 14865244eac9SBenno Rice { 148788afb2a3SBenno Rice 148859276937SPeter Grehan moea_remove(mmu, kernel_pmap, va, va + PAGE_SIZE); 14895244eac9SBenno Rice } 14905244eac9SBenno Rice 14915244eac9SBenno Rice /* 14925244eac9SBenno Rice * Map a range of physical addresses into kernel virtual address space. 14935244eac9SBenno Rice * 14945244eac9SBenno Rice * The value passed in *virt is a suggested virtual address for the mapping. 14955244eac9SBenno Rice * Architectures which can support a direct-mapped physical to virtual region 14965244eac9SBenno Rice * can return the appropriate address within that region, leaving '*virt' 14975244eac9SBenno Rice * unchanged. We cannot and therefore do not; *virt is updated with the 14985244eac9SBenno Rice * first usable address after the mapped region. 14995244eac9SBenno Rice */ 15005244eac9SBenno Rice vm_offset_t 150159276937SPeter Grehan moea_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start, 150259276937SPeter Grehan vm_offset_t pa_end, int prot) 15035244eac9SBenno Rice { 15045244eac9SBenno Rice vm_offset_t sva, va; 15055244eac9SBenno Rice 15065244eac9SBenno Rice sva = *virt; 15075244eac9SBenno Rice va = sva; 15085244eac9SBenno Rice for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE) 150959276937SPeter Grehan moea_kenter(mmu, va, pa_start); 15105244eac9SBenno Rice *virt = va; 15115244eac9SBenno Rice return (sva); 15125244eac9SBenno Rice } 15135244eac9SBenno Rice 15145244eac9SBenno Rice /* 15157f3a4093SMike Silbersack * Returns true if the pmap's pv is one of the first 15167f3a4093SMike Silbersack * 16 pvs linked to from this page. This count may 15177f3a4093SMike Silbersack * be changed upwards or downwards in the future; it 15187f3a4093SMike Silbersack * is only necessary that true be returned for a small 15197f3a4093SMike Silbersack * subset of pmaps for proper page aging. 15207f3a4093SMike Silbersack */ 15215244eac9SBenno Rice boolean_t 152259276937SPeter Grehan moea_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m) 15235244eac9SBenno Rice { 152403b6e025SPeter Grehan int loops; 152503b6e025SPeter Grehan struct pvo_entry *pvo; 1526*ce186587SAlan Cox boolean_t rv; 152703b6e025SPeter Grehan 1528*ce186587SAlan Cox KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0, 1529*ce186587SAlan Cox ("moea_page_exists_quick: page %p is not managed", m)); 153003b6e025SPeter Grehan loops = 0; 1531*ce186587SAlan Cox rv = FALSE; 1532*ce186587SAlan Cox vm_page_lock_queues(); 153303b6e025SPeter Grehan LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 1534*ce186587SAlan Cox if (pvo->pvo_pmap == pmap) { 1535*ce186587SAlan Cox rv = TRUE; 1536*ce186587SAlan Cox break; 1537*ce186587SAlan Cox } 153803b6e025SPeter Grehan if (++loops >= 16) 153903b6e025SPeter Grehan break; 154003b6e025SPeter Grehan } 1541*ce186587SAlan Cox vm_page_unlock_queues(); 1542*ce186587SAlan Cox return (rv); 15435244eac9SBenno Rice } 15445244eac9SBenno Rice 154559677d3cSAlan Cox /* 154659677d3cSAlan Cox * Return the number of managed mappings to the given physical page 154759677d3cSAlan Cox * that are wired. 154859677d3cSAlan Cox */ 154959677d3cSAlan Cox int 155059677d3cSAlan Cox moea_page_wired_mappings(mmu_t mmu, vm_page_t m) 155159677d3cSAlan Cox { 155259677d3cSAlan Cox struct pvo_entry *pvo; 155359677d3cSAlan Cox int count; 155459677d3cSAlan Cox 155559677d3cSAlan Cox count = 0; 1556*ce186587SAlan Cox if ((m->flags & PG_FICTITIOUS) != 0) 155759677d3cSAlan Cox return (count); 15583c4a2440SAlan Cox vm_page_lock_queues(); 155959677d3cSAlan Cox LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) 156059677d3cSAlan Cox if ((pvo->pvo_vaddr & PVO_WIRED) != 0) 156159677d3cSAlan Cox count++; 15623c4a2440SAlan Cox vm_page_unlock_queues(); 156359677d3cSAlan Cox return (count); 156459677d3cSAlan Cox } 156559677d3cSAlan Cox 156659276937SPeter Grehan static u_int moea_vsidcontext; 15675244eac9SBenno Rice 15685244eac9SBenno Rice void 156959276937SPeter Grehan moea_pinit(mmu_t mmu, pmap_t pmap) 15705244eac9SBenno Rice { 15715244eac9SBenno Rice int i, mask; 15725244eac9SBenno Rice u_int entropy; 15735244eac9SBenno Rice 157459276937SPeter Grehan KASSERT((int)pmap < VM_MIN_KERNEL_ADDRESS, ("moea_pinit: virt pmap")); 157548d0b1a0SAlan Cox PMAP_LOCK_INIT(pmap); 15764daf20b2SPeter Grehan 15775244eac9SBenno Rice entropy = 0; 15785244eac9SBenno Rice __asm __volatile("mftb %0" : "=r"(entropy)); 15795244eac9SBenno Rice 158052a7870dSNathan Whitehorn if ((pmap->pmap_phys = (pmap_t)moea_kextract(mmu, (vm_offset_t)pmap)) 158152a7870dSNathan Whitehorn == NULL) { 158252a7870dSNathan Whitehorn pmap->pmap_phys = pmap; 158352a7870dSNathan Whitehorn } 158452a7870dSNathan Whitehorn 158552a7870dSNathan Whitehorn 15865244eac9SBenno Rice /* 15875244eac9SBenno Rice * Allocate some segment registers for this pmap. 15885244eac9SBenno Rice */ 15895244eac9SBenno Rice for (i = 0; i < NPMAPS; i += VSID_NBPW) { 15905244eac9SBenno Rice u_int hash, n; 15915244eac9SBenno Rice 15925244eac9SBenno Rice /* 15935244eac9SBenno Rice * Create a new value by mutiplying by a prime and adding in 15945244eac9SBenno Rice * entropy from the timebase register. This is to make the 15955244eac9SBenno Rice * VSID more random so that the PT hash function collides 15965244eac9SBenno Rice * less often. (Note that the prime casues gcc to do shifts 15975244eac9SBenno Rice * instead of a multiply.) 15985244eac9SBenno Rice */ 159959276937SPeter Grehan moea_vsidcontext = (moea_vsidcontext * 0x1105) + entropy; 160059276937SPeter Grehan hash = moea_vsidcontext & (NPMAPS - 1); 16015244eac9SBenno Rice if (hash == 0) /* 0 is special, avoid it */ 16025244eac9SBenno Rice continue; 16035244eac9SBenno Rice n = hash >> 5; 16045244eac9SBenno Rice mask = 1 << (hash & (VSID_NBPW - 1)); 160559276937SPeter Grehan hash = (moea_vsidcontext & 0xfffff); 160659276937SPeter Grehan if (moea_vsid_bitmap[n] & mask) { /* collision? */ 16075244eac9SBenno Rice /* anything free in this bucket? */ 160859276937SPeter Grehan if (moea_vsid_bitmap[n] == 0xffffffff) { 160959276937SPeter Grehan entropy = (moea_vsidcontext >> 20); 16105244eac9SBenno Rice continue; 16115244eac9SBenno Rice } 161259276937SPeter Grehan i = ffs(~moea_vsid_bitmap[i]) - 1; 16135244eac9SBenno Rice mask = 1 << i; 16145244eac9SBenno Rice hash &= 0xfffff & ~(VSID_NBPW - 1); 16155244eac9SBenno Rice hash |= i; 16165244eac9SBenno Rice } 161759276937SPeter Grehan moea_vsid_bitmap[n] |= mask; 16185244eac9SBenno Rice for (i = 0; i < 16; i++) 16195244eac9SBenno Rice pmap->pm_sr[i] = VSID_MAKE(i, hash); 16205244eac9SBenno Rice return; 16215244eac9SBenno Rice } 16225244eac9SBenno Rice 162359276937SPeter Grehan panic("moea_pinit: out of segments"); 16245244eac9SBenno Rice } 16255244eac9SBenno Rice 16265244eac9SBenno Rice /* 16275244eac9SBenno Rice * Initialize the pmap associated with process 0. 16285244eac9SBenno Rice */ 16295244eac9SBenno Rice void 163059276937SPeter Grehan moea_pinit0(mmu_t mmu, pmap_t pm) 16315244eac9SBenno Rice { 16325244eac9SBenno Rice 163359276937SPeter Grehan moea_pinit(mmu, pm); 16345244eac9SBenno Rice bzero(&pm->pm_stats, sizeof(pm->pm_stats)); 16355244eac9SBenno Rice } 16365244eac9SBenno Rice 1637e79f59e8SBenno Rice /* 1638e79f59e8SBenno Rice * Set the physical protection on the specified range of this map as requested. 1639e79f59e8SBenno Rice */ 16405244eac9SBenno Rice void 164159276937SPeter Grehan moea_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva, 164259276937SPeter Grehan vm_prot_t prot) 16435244eac9SBenno Rice { 1644e79f59e8SBenno Rice struct pvo_entry *pvo; 1645e79f59e8SBenno Rice struct pte *pt; 1646e79f59e8SBenno Rice int pteidx; 1647e79f59e8SBenno Rice 1648e79f59e8SBenno Rice KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap, 164959276937SPeter Grehan ("moea_protect: non current pmap")); 1650e79f59e8SBenno Rice 1651e79f59e8SBenno Rice if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 165259276937SPeter Grehan moea_remove(mmu, pm, sva, eva); 1653e79f59e8SBenno Rice return; 1654e79f59e8SBenno Rice } 1655e79f59e8SBenno Rice 16563d2e54c3SAlan Cox vm_page_lock_queues(); 165748d0b1a0SAlan Cox PMAP_LOCK(pm); 1658e79f59e8SBenno Rice for (; sva < eva; sva += PAGE_SIZE) { 165959276937SPeter Grehan pvo = moea_pvo_find_va(pm, sva, &pteidx); 1660e79f59e8SBenno Rice if (pvo == NULL) 1661e79f59e8SBenno Rice continue; 1662e79f59e8SBenno Rice 1663e79f59e8SBenno Rice if ((prot & VM_PROT_EXECUTE) == 0) 1664e79f59e8SBenno Rice pvo->pvo_vaddr &= ~PVO_EXECUTABLE; 1665e79f59e8SBenno Rice 1666e79f59e8SBenno Rice /* 1667e79f59e8SBenno Rice * Grab the PTE pointer before we diddle with the cached PTE 1668e79f59e8SBenno Rice * copy. 1669e79f59e8SBenno Rice */ 167059276937SPeter Grehan pt = moea_pvo_to_pte(pvo, pteidx); 1671e79f59e8SBenno Rice /* 1672e79f59e8SBenno Rice * Change the protection of the page. 1673e79f59e8SBenno Rice */ 167452a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~PTE_PP; 167552a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo |= PTE_BR; 1676e79f59e8SBenno Rice 1677e79f59e8SBenno Rice /* 1678e79f59e8SBenno Rice * If the PVO is in the page table, update that pte as well. 1679e79f59e8SBenno Rice */ 1680d644a0b7SAlan Cox if (pt != NULL) { 168152a7870dSNathan Whitehorn moea_pte_change(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr); 1682d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 1683d644a0b7SAlan Cox } 1684e79f59e8SBenno Rice } 16853d2e54c3SAlan Cox vm_page_unlock_queues(); 168648d0b1a0SAlan Cox PMAP_UNLOCK(pm); 16875244eac9SBenno Rice } 16885244eac9SBenno Rice 168988afb2a3SBenno Rice /* 169088afb2a3SBenno Rice * Map a list of wired pages into kernel virtual address space. This is 169188afb2a3SBenno Rice * intended for temporary mappings which do not need page modification or 169288afb2a3SBenno Rice * references recorded. Existing mappings in the region are overwritten. 169388afb2a3SBenno Rice */ 16945244eac9SBenno Rice void 169559276937SPeter Grehan moea_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count) 16965244eac9SBenno Rice { 169703b6e025SPeter Grehan vm_offset_t va; 16985244eac9SBenno Rice 169903b6e025SPeter Grehan va = sva; 170003b6e025SPeter Grehan while (count-- > 0) { 170159276937SPeter Grehan moea_kenter(mmu, va, VM_PAGE_TO_PHYS(*m)); 170203b6e025SPeter Grehan va += PAGE_SIZE; 170303b6e025SPeter Grehan m++; 170403b6e025SPeter Grehan } 17055244eac9SBenno Rice } 17065244eac9SBenno Rice 170788afb2a3SBenno Rice /* 170888afb2a3SBenno Rice * Remove page mappings from kernel virtual address space. Intended for 170959276937SPeter Grehan * temporary mappings entered by moea_qenter. 171088afb2a3SBenno Rice */ 17115244eac9SBenno Rice void 171259276937SPeter Grehan moea_qremove(mmu_t mmu, vm_offset_t sva, int count) 17135244eac9SBenno Rice { 171403b6e025SPeter Grehan vm_offset_t va; 171588afb2a3SBenno Rice 171603b6e025SPeter Grehan va = sva; 171703b6e025SPeter Grehan while (count-- > 0) { 171859276937SPeter Grehan moea_kremove(mmu, va); 171903b6e025SPeter Grehan va += PAGE_SIZE; 172003b6e025SPeter Grehan } 17215244eac9SBenno Rice } 17225244eac9SBenno Rice 17235244eac9SBenno Rice void 172459276937SPeter Grehan moea_release(mmu_t mmu, pmap_t pmap) 17255244eac9SBenno Rice { 172632bc7846SPeter Grehan int idx, mask; 172732bc7846SPeter Grehan 172832bc7846SPeter Grehan /* 172932bc7846SPeter Grehan * Free segment register's VSID 173032bc7846SPeter Grehan */ 173132bc7846SPeter Grehan if (pmap->pm_sr[0] == 0) 173259276937SPeter Grehan panic("moea_release"); 173332bc7846SPeter Grehan 173432bc7846SPeter Grehan idx = VSID_TO_HASH(pmap->pm_sr[0]) & (NPMAPS-1); 173532bc7846SPeter Grehan mask = 1 << (idx % VSID_NBPW); 173632bc7846SPeter Grehan idx /= VSID_NBPW; 173759276937SPeter Grehan moea_vsid_bitmap[idx] &= ~mask; 173848d0b1a0SAlan Cox PMAP_LOCK_DESTROY(pmap); 17395244eac9SBenno Rice } 17405244eac9SBenno Rice 174188afb2a3SBenno Rice /* 174288afb2a3SBenno Rice * Remove the given range of addresses from the specified map. 174388afb2a3SBenno Rice */ 17445244eac9SBenno Rice void 174559276937SPeter Grehan moea_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva) 17465244eac9SBenno Rice { 174788afb2a3SBenno Rice struct pvo_entry *pvo; 174888afb2a3SBenno Rice int pteidx; 174988afb2a3SBenno Rice 17503d2e54c3SAlan Cox vm_page_lock_queues(); 175148d0b1a0SAlan Cox PMAP_LOCK(pm); 175288afb2a3SBenno Rice for (; sva < eva; sva += PAGE_SIZE) { 175359276937SPeter Grehan pvo = moea_pvo_find_va(pm, sva, &pteidx); 175488afb2a3SBenno Rice if (pvo != NULL) { 175559276937SPeter Grehan moea_pvo_remove(pvo, pteidx); 175688afb2a3SBenno Rice } 175788afb2a3SBenno Rice } 175848d0b1a0SAlan Cox PMAP_UNLOCK(pm); 175994aa7aecSPeter Grehan vm_page_unlock_queues(); 17605244eac9SBenno Rice } 17615244eac9SBenno Rice 1762e79f59e8SBenno Rice /* 176359276937SPeter Grehan * Remove physical page from all pmaps in which it resides. moea_pvo_remove() 176403b6e025SPeter Grehan * will reflect changes in pte's back to the vm_page. 176503b6e025SPeter Grehan */ 176603b6e025SPeter Grehan void 176759276937SPeter Grehan moea_remove_all(mmu_t mmu, vm_page_t m) 176803b6e025SPeter Grehan { 176903b6e025SPeter Grehan struct pvo_head *pvo_head; 177003b6e025SPeter Grehan struct pvo_entry *pvo, *next_pvo; 177148d0b1a0SAlan Cox pmap_t pmap; 177203b6e025SPeter Grehan 17733c4a2440SAlan Cox vm_page_lock_queues(); 177403b6e025SPeter Grehan pvo_head = vm_page_to_pvoh(m); 177503b6e025SPeter Grehan for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) { 177603b6e025SPeter Grehan next_pvo = LIST_NEXT(pvo, pvo_vlink); 177703b6e025SPeter Grehan 177859276937SPeter Grehan MOEA_PVO_CHECK(pvo); /* sanity check */ 177948d0b1a0SAlan Cox pmap = pvo->pvo_pmap; 178048d0b1a0SAlan Cox PMAP_LOCK(pmap); 178159276937SPeter Grehan moea_pvo_remove(pvo, -1); 178248d0b1a0SAlan Cox PMAP_UNLOCK(pmap); 178303b6e025SPeter Grehan } 1784062c8f4cSNathan Whitehorn if ((m->flags & PG_WRITEABLE) && moea_is_modified(mmu, m)) { 1785c668b5b4SNathan Whitehorn moea_attr_clear(m, PTE_CHG); 1786062c8f4cSNathan Whitehorn vm_page_dirty(m); 1787062c8f4cSNathan Whitehorn } 178803b6e025SPeter Grehan vm_page_flag_clear(m, PG_WRITEABLE); 17893c4a2440SAlan Cox vm_page_unlock_queues(); 179003b6e025SPeter Grehan } 179103b6e025SPeter Grehan 179203b6e025SPeter Grehan /* 17935244eac9SBenno Rice * Allocate a physical page of memory directly from the phys_avail map. 179459276937SPeter Grehan * Can only be called from moea_bootstrap before avail start and end are 17955244eac9SBenno Rice * calculated. 17965244eac9SBenno Rice */ 17975244eac9SBenno Rice static vm_offset_t 179859276937SPeter Grehan moea_bootstrap_alloc(vm_size_t size, u_int align) 17995244eac9SBenno Rice { 18005244eac9SBenno Rice vm_offset_t s, e; 18015244eac9SBenno Rice int i, j; 18025244eac9SBenno Rice 18035244eac9SBenno Rice size = round_page(size); 18045244eac9SBenno Rice for (i = 0; phys_avail[i + 1] != 0; i += 2) { 18055244eac9SBenno Rice if (align != 0) 18065244eac9SBenno Rice s = (phys_avail[i] + align - 1) & ~(align - 1); 18075244eac9SBenno Rice else 18085244eac9SBenno Rice s = phys_avail[i]; 18095244eac9SBenno Rice e = s + size; 18105244eac9SBenno Rice 18115244eac9SBenno Rice if (s < phys_avail[i] || e > phys_avail[i + 1]) 18125244eac9SBenno Rice continue; 18135244eac9SBenno Rice 18145244eac9SBenno Rice if (s == phys_avail[i]) { 18155244eac9SBenno Rice phys_avail[i] += size; 18165244eac9SBenno Rice } else if (e == phys_avail[i + 1]) { 18175244eac9SBenno Rice phys_avail[i + 1] -= size; 18185244eac9SBenno Rice } else { 18195244eac9SBenno Rice for (j = phys_avail_count * 2; j > i; j -= 2) { 18205244eac9SBenno Rice phys_avail[j] = phys_avail[j - 2]; 18215244eac9SBenno Rice phys_avail[j + 1] = phys_avail[j - 1]; 18225244eac9SBenno Rice } 18235244eac9SBenno Rice 18245244eac9SBenno Rice phys_avail[i + 3] = phys_avail[i + 1]; 18255244eac9SBenno Rice phys_avail[i + 1] = s; 18265244eac9SBenno Rice phys_avail[i + 2] = e; 18275244eac9SBenno Rice phys_avail_count++; 18285244eac9SBenno Rice } 18295244eac9SBenno Rice 18305244eac9SBenno Rice return (s); 18315244eac9SBenno Rice } 183259276937SPeter Grehan panic("moea_bootstrap_alloc: could not allocate memory"); 18335244eac9SBenno Rice } 18345244eac9SBenno Rice 18355244eac9SBenno Rice static void 183659276937SPeter Grehan moea_syncicache(vm_offset_t pa, vm_size_t len) 18375244eac9SBenno Rice { 18385244eac9SBenno Rice __syncicache((void *)pa, len); 18395244eac9SBenno Rice } 18405244eac9SBenno Rice 18415244eac9SBenno Rice static int 184259276937SPeter Grehan moea_pvo_enter(pmap_t pm, uma_zone_t zone, struct pvo_head *pvo_head, 18435244eac9SBenno Rice vm_offset_t va, vm_offset_t pa, u_int pte_lo, int flags) 18445244eac9SBenno Rice { 18455244eac9SBenno Rice struct pvo_entry *pvo; 18465244eac9SBenno Rice u_int sr; 18475244eac9SBenno Rice int first; 18485244eac9SBenno Rice u_int ptegidx; 18495244eac9SBenno Rice int i; 185032bc7846SPeter Grehan int bootstrap; 18515244eac9SBenno Rice 185259276937SPeter Grehan moea_pvo_enter_calls++; 18538207b362SBenno Rice first = 0; 185432bc7846SPeter Grehan bootstrap = 0; 185532bc7846SPeter Grehan 18565244eac9SBenno Rice /* 18575244eac9SBenno Rice * Compute the PTE Group index. 18585244eac9SBenno Rice */ 18595244eac9SBenno Rice va &= ~ADDR_POFF; 18605244eac9SBenno Rice sr = va_to_sr(pm->pm_sr, va); 18615244eac9SBenno Rice ptegidx = va_to_pteg(sr, va); 18625244eac9SBenno Rice 18635244eac9SBenno Rice /* 18645244eac9SBenno Rice * Remove any existing mapping for this page. Reuse the pvo entry if 18655244eac9SBenno Rice * there is a mapping. 18665244eac9SBenno Rice */ 186759276937SPeter Grehan mtx_lock(&moea_table_mutex); 186859276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) { 18695244eac9SBenno Rice if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) { 187052a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_lo & PTE_RPGN) == pa && 187152a7870dSNathan Whitehorn (pvo->pvo_pte.pte.pte_lo & PTE_PP) == 1872fafc7362SBenno Rice (pte_lo & PTE_PP)) { 187359276937SPeter Grehan mtx_unlock(&moea_table_mutex); 187449f8f727SBenno Rice return (0); 1875fafc7362SBenno Rice } 187659276937SPeter Grehan moea_pvo_remove(pvo, -1); 18775244eac9SBenno Rice break; 18785244eac9SBenno Rice } 18795244eac9SBenno Rice } 18805244eac9SBenno Rice 18815244eac9SBenno Rice /* 18825244eac9SBenno Rice * If we aren't overwriting a mapping, try to allocate. 18835244eac9SBenno Rice */ 188459276937SPeter Grehan if (moea_initialized) { 1885378862a7SJeff Roberson pvo = uma_zalloc(zone, M_NOWAIT); 188649f8f727SBenno Rice } else { 188759276937SPeter Grehan if (moea_bpvo_pool_index >= BPVO_POOL_SIZE) { 188859276937SPeter Grehan panic("moea_enter: bpvo pool exhausted, %d, %d, %d", 188959276937SPeter Grehan moea_bpvo_pool_index, BPVO_POOL_SIZE, 18900d290675SBenno Rice BPVO_POOL_SIZE * sizeof(struct pvo_entry)); 189149f8f727SBenno Rice } 189259276937SPeter Grehan pvo = &moea_bpvo_pool[moea_bpvo_pool_index]; 189359276937SPeter Grehan moea_bpvo_pool_index++; 189432bc7846SPeter Grehan bootstrap = 1; 189549f8f727SBenno Rice } 18965244eac9SBenno Rice 18975244eac9SBenno Rice if (pvo == NULL) { 189859276937SPeter Grehan mtx_unlock(&moea_table_mutex); 18995244eac9SBenno Rice return (ENOMEM); 19005244eac9SBenno Rice } 19015244eac9SBenno Rice 190259276937SPeter Grehan moea_pvo_entries++; 19035244eac9SBenno Rice pvo->pvo_vaddr = va; 19045244eac9SBenno Rice pvo->pvo_pmap = pm; 190559276937SPeter Grehan LIST_INSERT_HEAD(&moea_pvo_table[ptegidx], pvo, pvo_olink); 19065244eac9SBenno Rice pvo->pvo_vaddr &= ~ADDR_POFF; 19075244eac9SBenno Rice if (flags & VM_PROT_EXECUTE) 19085244eac9SBenno Rice pvo->pvo_vaddr |= PVO_EXECUTABLE; 19095244eac9SBenno Rice if (flags & PVO_WIRED) 19105244eac9SBenno Rice pvo->pvo_vaddr |= PVO_WIRED; 191159276937SPeter Grehan if (pvo_head != &moea_pvo_kunmanaged) 19125244eac9SBenno Rice pvo->pvo_vaddr |= PVO_MANAGED; 191332bc7846SPeter Grehan if (bootstrap) 191432bc7846SPeter Grehan pvo->pvo_vaddr |= PVO_BOOTSTRAP; 19154dba5df1SPeter Grehan if (flags & PVO_FAKE) 19164dba5df1SPeter Grehan pvo->pvo_vaddr |= PVO_FAKE; 19174dba5df1SPeter Grehan 191852a7870dSNathan Whitehorn moea_pte_create(&pvo->pvo_pte.pte, sr, va, pa | pte_lo); 19195244eac9SBenno Rice 19205244eac9SBenno Rice /* 19215244eac9SBenno Rice * Remember if the list was empty and therefore will be the first 19225244eac9SBenno Rice * item. 19235244eac9SBenno Rice */ 19248207b362SBenno Rice if (LIST_FIRST(pvo_head) == NULL) 19258207b362SBenno Rice first = 1; 19265244eac9SBenno Rice LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink); 19274dba5df1SPeter Grehan 192852a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED) 1929c3d11d22SAlan Cox pm->pm_stats.wired_count++; 1930c3d11d22SAlan Cox pm->pm_stats.resident_count++; 19315244eac9SBenno Rice 19325244eac9SBenno Rice /* 19335244eac9SBenno Rice * We hope this succeeds but it isn't required. 19345244eac9SBenno Rice */ 193552a7870dSNathan Whitehorn i = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte); 19365244eac9SBenno Rice if (i >= 0) { 19375244eac9SBenno Rice PVO_PTEGIDX_SET(pvo, i); 19385244eac9SBenno Rice } else { 193959276937SPeter Grehan panic("moea_pvo_enter: overflow"); 194059276937SPeter Grehan moea_pte_overflow++; 19415244eac9SBenno Rice } 194259276937SPeter Grehan mtx_unlock(&moea_table_mutex); 19434dba5df1SPeter Grehan 19445244eac9SBenno Rice return (first ? ENOENT : 0); 19455244eac9SBenno Rice } 19465244eac9SBenno Rice 19475244eac9SBenno Rice static void 194859276937SPeter Grehan moea_pvo_remove(struct pvo_entry *pvo, int pteidx) 19495244eac9SBenno Rice { 19505244eac9SBenno Rice struct pte *pt; 19515244eac9SBenno Rice 19525244eac9SBenno Rice /* 19535244eac9SBenno Rice * If there is an active pte entry, we need to deactivate it (and 19545244eac9SBenno Rice * save the ref & cfg bits). 19555244eac9SBenno Rice */ 195659276937SPeter Grehan pt = moea_pvo_to_pte(pvo, pteidx); 19575244eac9SBenno Rice if (pt != NULL) { 195852a7870dSNathan Whitehorn moea_pte_unset(pt, &pvo->pvo_pte.pte, pvo->pvo_vaddr); 1959d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 19605244eac9SBenno Rice PVO_PTEGIDX_CLR(pvo); 19615244eac9SBenno Rice } else { 196259276937SPeter Grehan moea_pte_overflow--; 19635244eac9SBenno Rice } 19645244eac9SBenno Rice 19655244eac9SBenno Rice /* 19665244eac9SBenno Rice * Update our statistics. 19675244eac9SBenno Rice */ 19685244eac9SBenno Rice pvo->pvo_pmap->pm_stats.resident_count--; 196952a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & PVO_WIRED) 19705244eac9SBenno Rice pvo->pvo_pmap->pm_stats.wired_count--; 19715244eac9SBenno Rice 19725244eac9SBenno Rice /* 19735244eac9SBenno Rice * Save the REF/CHG bits into their cache if the page is managed. 19745244eac9SBenno Rice */ 19754dba5df1SPeter Grehan if ((pvo->pvo_vaddr & (PVO_MANAGED|PVO_FAKE)) == PVO_MANAGED) { 19765244eac9SBenno Rice struct vm_page *pg; 19775244eac9SBenno Rice 197852a7870dSNathan Whitehorn pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.pte.pte_lo & PTE_RPGN); 19795244eac9SBenno Rice if (pg != NULL) { 198052a7870dSNathan Whitehorn moea_attr_save(pg, pvo->pvo_pte.pte.pte_lo & 19815244eac9SBenno Rice (PTE_REF | PTE_CHG)); 19825244eac9SBenno Rice } 19835244eac9SBenno Rice } 19845244eac9SBenno Rice 19855244eac9SBenno Rice /* 19865244eac9SBenno Rice * Remove this PVO from the PV list. 19875244eac9SBenno Rice */ 19885244eac9SBenno Rice LIST_REMOVE(pvo, pvo_vlink); 19895244eac9SBenno Rice 19905244eac9SBenno Rice /* 19915244eac9SBenno Rice * Remove this from the overflow list and return it to the pool 19925244eac9SBenno Rice * if we aren't going to reuse it. 19935244eac9SBenno Rice */ 19945244eac9SBenno Rice LIST_REMOVE(pvo, pvo_olink); 199549f8f727SBenno Rice if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP)) 199659276937SPeter Grehan uma_zfree(pvo->pvo_vaddr & PVO_MANAGED ? moea_mpvo_zone : 199759276937SPeter Grehan moea_upvo_zone, pvo); 199859276937SPeter Grehan moea_pvo_entries--; 199959276937SPeter Grehan moea_pvo_remove_calls++; 20005244eac9SBenno Rice } 20015244eac9SBenno Rice 20025244eac9SBenno Rice static __inline int 200359276937SPeter Grehan moea_pvo_pte_index(const struct pvo_entry *pvo, int ptegidx) 20045244eac9SBenno Rice { 20055244eac9SBenno Rice int pteidx; 20065244eac9SBenno Rice 20075244eac9SBenno Rice /* 20085244eac9SBenno Rice * We can find the actual pte entry without searching by grabbing 20095244eac9SBenno Rice * the PTEG index from 3 unused bits in pte_lo[11:9] and by 20105244eac9SBenno Rice * noticing the HID bit. 20115244eac9SBenno Rice */ 20125244eac9SBenno Rice pteidx = ptegidx * 8 + PVO_PTEGIDX_GET(pvo); 201352a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_hi & PTE_HID) 201459276937SPeter Grehan pteidx ^= moea_pteg_mask * 8; 20155244eac9SBenno Rice 20165244eac9SBenno Rice return (pteidx); 20175244eac9SBenno Rice } 20185244eac9SBenno Rice 20195244eac9SBenno Rice static struct pvo_entry * 202059276937SPeter Grehan moea_pvo_find_va(pmap_t pm, vm_offset_t va, int *pteidx_p) 20215244eac9SBenno Rice { 20225244eac9SBenno Rice struct pvo_entry *pvo; 20235244eac9SBenno Rice int ptegidx; 20245244eac9SBenno Rice u_int sr; 20255244eac9SBenno Rice 20265244eac9SBenno Rice va &= ~ADDR_POFF; 20275244eac9SBenno Rice sr = va_to_sr(pm->pm_sr, va); 20285244eac9SBenno Rice ptegidx = va_to_pteg(sr, va); 20295244eac9SBenno Rice 203059276937SPeter Grehan mtx_lock(&moea_table_mutex); 203159276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) { 20325244eac9SBenno Rice if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) { 20335244eac9SBenno Rice if (pteidx_p) 203459276937SPeter Grehan *pteidx_p = moea_pvo_pte_index(pvo, ptegidx); 2035f489bf21SAlan Cox break; 20365244eac9SBenno Rice } 20375244eac9SBenno Rice } 203859276937SPeter Grehan mtx_unlock(&moea_table_mutex); 20395244eac9SBenno Rice 2040f489bf21SAlan Cox return (pvo); 20415244eac9SBenno Rice } 20425244eac9SBenno Rice 20435244eac9SBenno Rice static struct pte * 204459276937SPeter Grehan moea_pvo_to_pte(const struct pvo_entry *pvo, int pteidx) 20455244eac9SBenno Rice { 20465244eac9SBenno Rice struct pte *pt; 20475244eac9SBenno Rice 20485244eac9SBenno Rice /* 20495244eac9SBenno Rice * If we haven't been supplied the ptegidx, calculate it. 20505244eac9SBenno Rice */ 20515244eac9SBenno Rice if (pteidx == -1) { 20525244eac9SBenno Rice int ptegidx; 20535244eac9SBenno Rice u_int sr; 20545244eac9SBenno Rice 20555244eac9SBenno Rice sr = va_to_sr(pvo->pvo_pmap->pm_sr, pvo->pvo_vaddr); 20565244eac9SBenno Rice ptegidx = va_to_pteg(sr, pvo->pvo_vaddr); 205759276937SPeter Grehan pteidx = moea_pvo_pte_index(pvo, ptegidx); 20585244eac9SBenno Rice } 20595244eac9SBenno Rice 206059276937SPeter Grehan pt = &moea_pteg_table[pteidx >> 3].pt[pteidx & 7]; 2061d644a0b7SAlan Cox mtx_lock(&moea_table_mutex); 20625244eac9SBenno Rice 206352a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) && !PVO_PTEGIDX_ISSET(pvo)) { 206459276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has valid pte in pvo but no " 20655244eac9SBenno Rice "valid pte index", pvo); 20665244eac9SBenno Rice } 20675244eac9SBenno Rice 206852a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0 && PVO_PTEGIDX_ISSET(pvo)) { 206959276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has valid pte index in pvo " 20705244eac9SBenno Rice "pvo but no valid pte", pvo); 20715244eac9SBenno Rice } 20725244eac9SBenno Rice 207352a7870dSNathan Whitehorn if ((pt->pte_hi ^ (pvo->pvo_pte.pte.pte_hi & ~PTE_VALID)) == PTE_VALID) { 207452a7870dSNathan Whitehorn if ((pvo->pvo_pte.pte.pte_hi & PTE_VALID) == 0) { 207559276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has valid pte in " 207659276937SPeter Grehan "moea_pteg_table %p but invalid in pvo", pvo, pt); 20775244eac9SBenno Rice } 20785244eac9SBenno Rice 207952a7870dSNathan Whitehorn if (((pt->pte_lo ^ pvo->pvo_pte.pte.pte_lo) & ~(PTE_CHG|PTE_REF)) 20805244eac9SBenno Rice != 0) { 208159276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p pte does not match " 208259276937SPeter Grehan "pte %p in moea_pteg_table", pvo, pt); 20835244eac9SBenno Rice } 20845244eac9SBenno Rice 2085d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 20865244eac9SBenno Rice return (pt); 20875244eac9SBenno Rice } 20885244eac9SBenno Rice 208952a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_hi & PTE_VALID) { 209059276937SPeter Grehan panic("moea_pvo_to_pte: pvo %p has invalid pte %p in " 209159276937SPeter Grehan "moea_pteg_table but valid in pvo", pvo, pt); 20925244eac9SBenno Rice } 20935244eac9SBenno Rice 2094d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 20955244eac9SBenno Rice return (NULL); 20965244eac9SBenno Rice } 20975244eac9SBenno Rice 20985244eac9SBenno Rice /* 20995244eac9SBenno Rice * XXX: THIS STUFF SHOULD BE IN pte.c? 21005244eac9SBenno Rice */ 21015244eac9SBenno Rice int 210259276937SPeter Grehan moea_pte_spill(vm_offset_t addr) 21035244eac9SBenno Rice { 21045244eac9SBenno Rice struct pvo_entry *source_pvo, *victim_pvo; 21055244eac9SBenno Rice struct pvo_entry *pvo; 21065244eac9SBenno Rice int ptegidx, i, j; 21075244eac9SBenno Rice u_int sr; 21085244eac9SBenno Rice struct pteg *pteg; 21095244eac9SBenno Rice struct pte *pt; 21105244eac9SBenno Rice 211159276937SPeter Grehan moea_pte_spills++; 21125244eac9SBenno Rice 2113d080d5fdSBenno Rice sr = mfsrin(addr); 21145244eac9SBenno Rice ptegidx = va_to_pteg(sr, addr); 21155244eac9SBenno Rice 21165244eac9SBenno Rice /* 21175244eac9SBenno Rice * Have to substitute some entry. Use the primary hash for this. 21185244eac9SBenno Rice * Use low bits of timebase as random generator. 21195244eac9SBenno Rice */ 212059276937SPeter Grehan pteg = &moea_pteg_table[ptegidx]; 212159276937SPeter Grehan mtx_lock(&moea_table_mutex); 21225244eac9SBenno Rice __asm __volatile("mftb %0" : "=r"(i)); 21235244eac9SBenno Rice i &= 7; 21245244eac9SBenno Rice pt = &pteg->pt[i]; 21255244eac9SBenno Rice 21265244eac9SBenno Rice source_pvo = NULL; 21275244eac9SBenno Rice victim_pvo = NULL; 212859276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx], pvo_olink) { 21295244eac9SBenno Rice /* 21305244eac9SBenno Rice * We need to find a pvo entry for this address. 21315244eac9SBenno Rice */ 213259276937SPeter Grehan MOEA_PVO_CHECK(pvo); 21335244eac9SBenno Rice if (source_pvo == NULL && 213452a7870dSNathan Whitehorn moea_pte_match(&pvo->pvo_pte.pte, sr, addr, 213552a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_hi & PTE_HID)) { 21365244eac9SBenno Rice /* 21375244eac9SBenno Rice * Now found an entry to be spilled into the pteg. 21385244eac9SBenno Rice * The PTE is now valid, so we know it's active. 21395244eac9SBenno Rice */ 214052a7870dSNathan Whitehorn j = moea_pte_insert(ptegidx, &pvo->pvo_pte.pte); 21415244eac9SBenno Rice 21425244eac9SBenno Rice if (j >= 0) { 21435244eac9SBenno Rice PVO_PTEGIDX_SET(pvo, j); 214459276937SPeter Grehan moea_pte_overflow--; 214559276937SPeter Grehan MOEA_PVO_CHECK(pvo); 214659276937SPeter Grehan mtx_unlock(&moea_table_mutex); 21475244eac9SBenno Rice return (1); 21485244eac9SBenno Rice } 21495244eac9SBenno Rice 21505244eac9SBenno Rice source_pvo = pvo; 21515244eac9SBenno Rice 21525244eac9SBenno Rice if (victim_pvo != NULL) 21535244eac9SBenno Rice break; 21545244eac9SBenno Rice } 21555244eac9SBenno Rice 21565244eac9SBenno Rice /* 21575244eac9SBenno Rice * We also need the pvo entry of the victim we are replacing 21585244eac9SBenno Rice * so save the R & C bits of the PTE. 21595244eac9SBenno Rice */ 21605244eac9SBenno Rice if ((pt->pte_hi & PTE_HID) == 0 && victim_pvo == NULL && 216152a7870dSNathan Whitehorn moea_pte_compare(pt, &pvo->pvo_pte.pte)) { 21625244eac9SBenno Rice victim_pvo = pvo; 21635244eac9SBenno Rice if (source_pvo != NULL) 21645244eac9SBenno Rice break; 21655244eac9SBenno Rice } 21665244eac9SBenno Rice } 21675244eac9SBenno Rice 2168f489bf21SAlan Cox if (source_pvo == NULL) { 216959276937SPeter Grehan mtx_unlock(&moea_table_mutex); 21705244eac9SBenno Rice return (0); 2171f489bf21SAlan Cox } 21725244eac9SBenno Rice 21735244eac9SBenno Rice if (victim_pvo == NULL) { 21745244eac9SBenno Rice if ((pt->pte_hi & PTE_HID) == 0) 217559276937SPeter Grehan panic("moea_pte_spill: victim p-pte (%p) has no pvo" 21765244eac9SBenno Rice "entry", pt); 21775244eac9SBenno Rice 21785244eac9SBenno Rice /* 21795244eac9SBenno Rice * If this is a secondary PTE, we need to search it's primary 21805244eac9SBenno Rice * pvo bucket for the matching PVO. 21815244eac9SBenno Rice */ 218259276937SPeter Grehan LIST_FOREACH(pvo, &moea_pvo_table[ptegidx ^ moea_pteg_mask], 21835244eac9SBenno Rice pvo_olink) { 218459276937SPeter Grehan MOEA_PVO_CHECK(pvo); 21855244eac9SBenno Rice /* 21865244eac9SBenno Rice * We also need the pvo entry of the victim we are 21875244eac9SBenno Rice * replacing so save the R & C bits of the PTE. 21885244eac9SBenno Rice */ 218952a7870dSNathan Whitehorn if (moea_pte_compare(pt, &pvo->pvo_pte.pte)) { 21905244eac9SBenno Rice victim_pvo = pvo; 21915244eac9SBenno Rice break; 21925244eac9SBenno Rice } 21935244eac9SBenno Rice } 21945244eac9SBenno Rice 21955244eac9SBenno Rice if (victim_pvo == NULL) 219659276937SPeter Grehan panic("moea_pte_spill: victim s-pte (%p) has no pvo" 21975244eac9SBenno Rice "entry", pt); 21985244eac9SBenno Rice } 21995244eac9SBenno Rice 22005244eac9SBenno Rice /* 22015244eac9SBenno Rice * We are invalidating the TLB entry for the EA we are replacing even 22025244eac9SBenno Rice * though it's valid. If we don't, we lose any ref/chg bit changes 22035244eac9SBenno Rice * contained in the TLB entry. 22045244eac9SBenno Rice */ 220552a7870dSNathan Whitehorn source_pvo->pvo_pte.pte.pte_hi &= ~PTE_HID; 22065244eac9SBenno Rice 220752a7870dSNathan Whitehorn moea_pte_unset(pt, &victim_pvo->pvo_pte.pte, victim_pvo->pvo_vaddr); 220852a7870dSNathan Whitehorn moea_pte_set(pt, &source_pvo->pvo_pte.pte); 22095244eac9SBenno Rice 22105244eac9SBenno Rice PVO_PTEGIDX_CLR(victim_pvo); 22115244eac9SBenno Rice PVO_PTEGIDX_SET(source_pvo, i); 221259276937SPeter Grehan moea_pte_replacements++; 22135244eac9SBenno Rice 221459276937SPeter Grehan MOEA_PVO_CHECK(victim_pvo); 221559276937SPeter Grehan MOEA_PVO_CHECK(source_pvo); 22165244eac9SBenno Rice 221759276937SPeter Grehan mtx_unlock(&moea_table_mutex); 22185244eac9SBenno Rice return (1); 22195244eac9SBenno Rice } 22205244eac9SBenno Rice 22215244eac9SBenno Rice static int 222259276937SPeter Grehan moea_pte_insert(u_int ptegidx, struct pte *pvo_pt) 22235244eac9SBenno Rice { 22245244eac9SBenno Rice struct pte *pt; 22255244eac9SBenno Rice int i; 22265244eac9SBenno Rice 2227d644a0b7SAlan Cox mtx_assert(&moea_table_mutex, MA_OWNED); 2228d644a0b7SAlan Cox 22295244eac9SBenno Rice /* 22305244eac9SBenno Rice * First try primary hash. 22315244eac9SBenno Rice */ 223259276937SPeter Grehan for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) { 22335244eac9SBenno Rice if ((pt->pte_hi & PTE_VALID) == 0) { 22345244eac9SBenno Rice pvo_pt->pte_hi &= ~PTE_HID; 223559276937SPeter Grehan moea_pte_set(pt, pvo_pt); 22365244eac9SBenno Rice return (i); 22375244eac9SBenno Rice } 22385244eac9SBenno Rice } 22395244eac9SBenno Rice 22405244eac9SBenno Rice /* 22415244eac9SBenno Rice * Now try secondary hash. 22425244eac9SBenno Rice */ 224359276937SPeter Grehan ptegidx ^= moea_pteg_mask; 2244bd8e6f87SPeter Grehan 224559276937SPeter Grehan for (pt = moea_pteg_table[ptegidx].pt, i = 0; i < 8; i++, pt++) { 22465244eac9SBenno Rice if ((pt->pte_hi & PTE_VALID) == 0) { 22475244eac9SBenno Rice pvo_pt->pte_hi |= PTE_HID; 224859276937SPeter Grehan moea_pte_set(pt, pvo_pt); 22495244eac9SBenno Rice return (i); 22505244eac9SBenno Rice } 22515244eac9SBenno Rice } 22525244eac9SBenno Rice 225359276937SPeter Grehan panic("moea_pte_insert: overflow"); 22545244eac9SBenno Rice return (-1); 22555244eac9SBenno Rice } 22565244eac9SBenno Rice 22575244eac9SBenno Rice static boolean_t 225859276937SPeter Grehan moea_query_bit(vm_page_t m, int ptebit) 22595244eac9SBenno Rice { 22605244eac9SBenno Rice struct pvo_entry *pvo; 22615244eac9SBenno Rice struct pte *pt; 22625244eac9SBenno Rice 226359276937SPeter Grehan if (moea_attr_fetch(m) & ptebit) 22645244eac9SBenno Rice return (TRUE); 22655244eac9SBenno Rice 2266c46b90e9SAlan Cox vm_page_lock_queues(); 22675244eac9SBenno Rice LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 226859276937SPeter Grehan MOEA_PVO_CHECK(pvo); /* sanity check */ 22695244eac9SBenno Rice 22705244eac9SBenno Rice /* 22715244eac9SBenno Rice * See if we saved the bit off. If so, cache it and return 22725244eac9SBenno Rice * success. 22735244eac9SBenno Rice */ 227452a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & ptebit) { 227559276937SPeter Grehan moea_attr_save(m, ptebit); 227659276937SPeter Grehan MOEA_PVO_CHECK(pvo); /* sanity check */ 2277c46b90e9SAlan Cox vm_page_unlock_queues(); 22785244eac9SBenno Rice return (TRUE); 22795244eac9SBenno Rice } 22805244eac9SBenno Rice } 22815244eac9SBenno Rice 22825244eac9SBenno Rice /* 22835244eac9SBenno Rice * No luck, now go through the hard part of looking at the PTEs 22845244eac9SBenno Rice * themselves. Sync so that any pending REF/CHG bits are flushed to 22855244eac9SBenno Rice * the PTEs. 22865244eac9SBenno Rice */ 2287e4f72b32SMarcel Moolenaar powerpc_sync(); 22885244eac9SBenno Rice LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 228959276937SPeter Grehan MOEA_PVO_CHECK(pvo); /* sanity check */ 22905244eac9SBenno Rice 22915244eac9SBenno Rice /* 22925244eac9SBenno Rice * See if this pvo has a valid PTE. if so, fetch the 22935244eac9SBenno Rice * REF/CHG bits from the valid PTE. If the appropriate 22945244eac9SBenno Rice * ptebit is set, cache it and return success. 22955244eac9SBenno Rice */ 229659276937SPeter Grehan pt = moea_pvo_to_pte(pvo, -1); 22975244eac9SBenno Rice if (pt != NULL) { 229852a7870dSNathan Whitehorn moea_pte_synch(pt, &pvo->pvo_pte.pte); 2299d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 230052a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & ptebit) { 230159276937SPeter Grehan moea_attr_save(m, ptebit); 230259276937SPeter Grehan MOEA_PVO_CHECK(pvo); /* sanity check */ 2303c46b90e9SAlan Cox vm_page_unlock_queues(); 23045244eac9SBenno Rice return (TRUE); 23055244eac9SBenno Rice } 23065244eac9SBenno Rice } 23075244eac9SBenno Rice } 23085244eac9SBenno Rice 2309c46b90e9SAlan Cox vm_page_unlock_queues(); 23104f7daed0SAndrew Gallatin return (FALSE); 23115244eac9SBenno Rice } 23125244eac9SBenno Rice 231303b6e025SPeter Grehan static u_int 2314*ce186587SAlan Cox moea_clear_bit(vm_page_t m, int ptebit) 23155244eac9SBenno Rice { 231603b6e025SPeter Grehan u_int count; 23175244eac9SBenno Rice struct pvo_entry *pvo; 23185244eac9SBenno Rice struct pte *pt; 2319*ce186587SAlan Cox 2320*ce186587SAlan Cox vm_page_lock_queues(); 23215244eac9SBenno Rice 23225244eac9SBenno Rice /* 23235244eac9SBenno Rice * Clear the cached value. 23245244eac9SBenno Rice */ 232559276937SPeter Grehan moea_attr_clear(m, ptebit); 23265244eac9SBenno Rice 23275244eac9SBenno Rice /* 23285244eac9SBenno Rice * Sync so that any pending REF/CHG bits are flushed to the PTEs (so 23295244eac9SBenno Rice * we can reset the right ones). note that since the pvo entries and 23305244eac9SBenno Rice * list heads are accessed via BAT0 and are never placed in the page 23315244eac9SBenno Rice * table, we don't have to worry about further accesses setting the 23325244eac9SBenno Rice * REF/CHG bits. 23335244eac9SBenno Rice */ 2334e4f72b32SMarcel Moolenaar powerpc_sync(); 23355244eac9SBenno Rice 23365244eac9SBenno Rice /* 23375244eac9SBenno Rice * For each pvo entry, clear the pvo's ptebit. If this pvo has a 23385244eac9SBenno Rice * valid pte clear the ptebit from the valid pte. 23395244eac9SBenno Rice */ 234003b6e025SPeter Grehan count = 0; 23415244eac9SBenno Rice LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 234259276937SPeter Grehan MOEA_PVO_CHECK(pvo); /* sanity check */ 234359276937SPeter Grehan pt = moea_pvo_to_pte(pvo, -1); 23445244eac9SBenno Rice if (pt != NULL) { 234552a7870dSNathan Whitehorn moea_pte_synch(pt, &pvo->pvo_pte.pte); 234652a7870dSNathan Whitehorn if (pvo->pvo_pte.pte.pte_lo & ptebit) { 234703b6e025SPeter Grehan count++; 234859276937SPeter Grehan moea_pte_clear(pt, PVO_VADDR(pvo), ptebit); 23495244eac9SBenno Rice } 2350d644a0b7SAlan Cox mtx_unlock(&moea_table_mutex); 235103b6e025SPeter Grehan } 235252a7870dSNathan Whitehorn pvo->pvo_pte.pte.pte_lo &= ~ptebit; 235359276937SPeter Grehan MOEA_PVO_CHECK(pvo); /* sanity check */ 23545244eac9SBenno Rice } 23555244eac9SBenno Rice 2356*ce186587SAlan Cox vm_page_unlock_queues(); 235703b6e025SPeter Grehan return (count); 2358bdf71f56SBenno Rice } 23598bbfa33aSBenno Rice 23608bbfa33aSBenno Rice /* 236132bc7846SPeter Grehan * Return true if the physical range is encompassed by the battable[idx] 236232bc7846SPeter Grehan */ 236332bc7846SPeter Grehan static int 236459276937SPeter Grehan moea_bat_mapped(int idx, vm_offset_t pa, vm_size_t size) 236532bc7846SPeter Grehan { 236632bc7846SPeter Grehan u_int prot; 236732bc7846SPeter Grehan u_int32_t start; 236832bc7846SPeter Grehan u_int32_t end; 236932bc7846SPeter Grehan u_int32_t bat_ble; 237032bc7846SPeter Grehan 237132bc7846SPeter Grehan /* 237232bc7846SPeter Grehan * Return immediately if not a valid mapping 237332bc7846SPeter Grehan */ 237432bc7846SPeter Grehan if (!battable[idx].batu & BAT_Vs) 237532bc7846SPeter Grehan return (EINVAL); 237632bc7846SPeter Grehan 237732bc7846SPeter Grehan /* 237832bc7846SPeter Grehan * The BAT entry must be cache-inhibited, guarded, and r/w 237932bc7846SPeter Grehan * so it can function as an i/o page 238032bc7846SPeter Grehan */ 238132bc7846SPeter Grehan prot = battable[idx].batl & (BAT_I|BAT_G|BAT_PP_RW); 238232bc7846SPeter Grehan if (prot != (BAT_I|BAT_G|BAT_PP_RW)) 238332bc7846SPeter Grehan return (EPERM); 238432bc7846SPeter Grehan 238532bc7846SPeter Grehan /* 238632bc7846SPeter Grehan * The address should be within the BAT range. Assume that the 238732bc7846SPeter Grehan * start address in the BAT has the correct alignment (thus 238832bc7846SPeter Grehan * not requiring masking) 238932bc7846SPeter Grehan */ 239032bc7846SPeter Grehan start = battable[idx].batl & BAT_PBS; 239132bc7846SPeter Grehan bat_ble = (battable[idx].batu & ~(BAT_EBS)) | 0x03; 239232bc7846SPeter Grehan end = start | (bat_ble << 15) | 0x7fff; 239332bc7846SPeter Grehan 239432bc7846SPeter Grehan if ((pa < start) || ((pa + size) > end)) 239532bc7846SPeter Grehan return (ERANGE); 239632bc7846SPeter Grehan 239732bc7846SPeter Grehan return (0); 239832bc7846SPeter Grehan } 239932bc7846SPeter Grehan 240059276937SPeter Grehan boolean_t 240159276937SPeter Grehan moea_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size) 2402c0763d37SSuleiman Souhlal { 2403c0763d37SSuleiman Souhlal int i; 2404c0763d37SSuleiman Souhlal 2405c0763d37SSuleiman Souhlal /* 2406c0763d37SSuleiman Souhlal * This currently does not work for entries that 2407c0763d37SSuleiman Souhlal * overlap 256M BAT segments. 2408c0763d37SSuleiman Souhlal */ 2409c0763d37SSuleiman Souhlal 2410c0763d37SSuleiman Souhlal for(i = 0; i < 16; i++) 241159276937SPeter Grehan if (moea_bat_mapped(i, pa, size) == 0) 2412c0763d37SSuleiman Souhlal return (0); 2413c0763d37SSuleiman Souhlal 2414c0763d37SSuleiman Souhlal return (EFAULT); 2415c0763d37SSuleiman Souhlal } 241632bc7846SPeter Grehan 241732bc7846SPeter Grehan /* 24188bbfa33aSBenno Rice * Map a set of physical memory pages into the kernel virtual 24198bbfa33aSBenno Rice * address space. Return a pointer to where it is mapped. This 24208bbfa33aSBenno Rice * routine is intended to be used for mapping device memory, 24218bbfa33aSBenno Rice * NOT real memory. 24228bbfa33aSBenno Rice */ 24238bbfa33aSBenno Rice void * 242459276937SPeter Grehan moea_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size) 24258bbfa33aSBenno Rice { 242632bc7846SPeter Grehan vm_offset_t va, tmpva, ppa, offset; 242732bc7846SPeter Grehan int i; 24288bbfa33aSBenno Rice 242932bc7846SPeter Grehan ppa = trunc_page(pa); 24308bbfa33aSBenno Rice offset = pa & PAGE_MASK; 24318bbfa33aSBenno Rice size = roundup(offset + size, PAGE_SIZE); 24328bbfa33aSBenno Rice 24338bbfa33aSBenno Rice GIANT_REQUIRED; 24348bbfa33aSBenno Rice 243532bc7846SPeter Grehan /* 243632bc7846SPeter Grehan * If the physical address lies within a valid BAT table entry, 243732bc7846SPeter Grehan * return the 1:1 mapping. This currently doesn't work 243832bc7846SPeter Grehan * for regions that overlap 256M BAT segments. 243932bc7846SPeter Grehan */ 244032bc7846SPeter Grehan for (i = 0; i < 16; i++) { 244159276937SPeter Grehan if (moea_bat_mapped(i, pa, size) == 0) 244232bc7846SPeter Grehan return ((void *) pa); 244332bc7846SPeter Grehan } 244432bc7846SPeter Grehan 2445e53f32acSAlan Cox va = kmem_alloc_nofault(kernel_map, size); 24468bbfa33aSBenno Rice if (!va) 244759276937SPeter Grehan panic("moea_mapdev: Couldn't alloc kernel virtual memory"); 24488bbfa33aSBenno Rice 24498bbfa33aSBenno Rice for (tmpva = va; size > 0;) { 245059276937SPeter Grehan moea_kenter(mmu, tmpva, ppa); 2451e4f72b32SMarcel Moolenaar tlbie(tmpva); 24528bbfa33aSBenno Rice size -= PAGE_SIZE; 24538bbfa33aSBenno Rice tmpva += PAGE_SIZE; 245432bc7846SPeter Grehan ppa += PAGE_SIZE; 24558bbfa33aSBenno Rice } 24568bbfa33aSBenno Rice 24578bbfa33aSBenno Rice return ((void *)(va + offset)); 24588bbfa33aSBenno Rice } 24598bbfa33aSBenno Rice 24608bbfa33aSBenno Rice void 246159276937SPeter Grehan moea_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size) 24628bbfa33aSBenno Rice { 24638bbfa33aSBenno Rice vm_offset_t base, offset; 24648bbfa33aSBenno Rice 246532bc7846SPeter Grehan /* 246632bc7846SPeter Grehan * If this is outside kernel virtual space, then it's a 246732bc7846SPeter Grehan * battable entry and doesn't require unmapping 246832bc7846SPeter Grehan */ 2469ab739706SNathan Whitehorn if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= virtual_end)) { 24708bbfa33aSBenno Rice base = trunc_page(va); 24718bbfa33aSBenno Rice offset = va & PAGE_MASK; 24728bbfa33aSBenno Rice size = roundup(offset + size, PAGE_SIZE); 24738bbfa33aSBenno Rice kmem_free(kernel_map, base, size); 24748bbfa33aSBenno Rice } 247532bc7846SPeter Grehan } 24761a4fcaebSMarcel Moolenaar 24771a4fcaebSMarcel Moolenaar static void 24781a4fcaebSMarcel Moolenaar moea_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz) 24791a4fcaebSMarcel Moolenaar { 24801a4fcaebSMarcel Moolenaar struct pvo_entry *pvo; 24811a4fcaebSMarcel Moolenaar vm_offset_t lim; 24821a4fcaebSMarcel Moolenaar vm_paddr_t pa; 24831a4fcaebSMarcel Moolenaar vm_size_t len; 24841a4fcaebSMarcel Moolenaar 24851a4fcaebSMarcel Moolenaar PMAP_LOCK(pm); 24861a4fcaebSMarcel Moolenaar while (sz > 0) { 24871a4fcaebSMarcel Moolenaar lim = round_page(va); 24881a4fcaebSMarcel Moolenaar len = MIN(lim - va, sz); 24891a4fcaebSMarcel Moolenaar pvo = moea_pvo_find_va(pm, va & ~ADDR_POFF, NULL); 24901a4fcaebSMarcel Moolenaar if (pvo != NULL) { 24911a4fcaebSMarcel Moolenaar pa = (pvo->pvo_pte.pte.pte_lo & PTE_RPGN) | 24921a4fcaebSMarcel Moolenaar (va & ADDR_POFF); 24931a4fcaebSMarcel Moolenaar moea_syncicache(pa, len); 24941a4fcaebSMarcel Moolenaar } 24951a4fcaebSMarcel Moolenaar va += len; 24961a4fcaebSMarcel Moolenaar sz -= len; 24971a4fcaebSMarcel Moolenaar } 24981a4fcaebSMarcel Moolenaar PMAP_UNLOCK(pm); 24991a4fcaebSMarcel Moolenaar } 2500