1f9bac91bSBenno Rice /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4ffb56695SRafal Jaworowski * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com> 5ffb56695SRafal Jaworowski * All rights reserved. 6ffb56695SRafal Jaworowski * 7ffb56695SRafal Jaworowski * Adapted for Freescale's e500 core CPUs. 8ffb56695SRafal Jaworowski * 9ffb56695SRafal Jaworowski * Redistribution and use in source and binary forms, with or without 10ffb56695SRafal Jaworowski * modification, are permitted provided that the following conditions 11ffb56695SRafal Jaworowski * are met: 12ffb56695SRafal Jaworowski * 1. Redistributions of source code must retain the above copyright 13ffb56695SRafal Jaworowski * notice, this list of conditions and the following disclaimer. 14ffb56695SRafal Jaworowski * 2. Redistributions in binary form must reproduce the above copyright 15ffb56695SRafal Jaworowski * notice, this list of conditions and the following disclaimer in the 16ffb56695SRafal Jaworowski * documentation and/or other materials provided with the distribution. 17ffb56695SRafal Jaworowski * 3. The name of the author may not be used to endorse or promote products 18ffb56695SRafal Jaworowski * derived from this software without specific prior written permission. 19ffb56695SRafal Jaworowski * 20ffb56695SRafal Jaworowski * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21ffb56695SRafal Jaworowski * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22ffb56695SRafal Jaworowski * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 23ffb56695SRafal Jaworowski * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24ffb56695SRafal Jaworowski * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 25ffb56695SRafal Jaworowski * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26ffb56695SRafal Jaworowski * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27ffb56695SRafal Jaworowski * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28ffb56695SRafal Jaworowski * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29ffb56695SRafal Jaworowski * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30ffb56695SRafal Jaworowski * 31ffb56695SRafal Jaworowski * $FreeBSD$ 32ffb56695SRafal Jaworowski */ 33ffb56695SRafal Jaworowski /*- 34f9bac91bSBenno Rice * Copyright (C) 1995, 1996 Wolfgang Solfrank. 35f9bac91bSBenno Rice * Copyright (C) 1995, 1996 TooLs GmbH. 36f9bac91bSBenno Rice * All rights reserved. 37f9bac91bSBenno Rice * 38f9bac91bSBenno Rice * Redistribution and use in source and binary forms, with or without 39f9bac91bSBenno Rice * modification, are permitted provided that the following conditions 40f9bac91bSBenno Rice * are met: 41f9bac91bSBenno Rice * 1. Redistributions of source code must retain the above copyright 42f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer. 43f9bac91bSBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 44f9bac91bSBenno Rice * notice, this list of conditions and the following disclaimer in the 45f9bac91bSBenno Rice * documentation and/or other materials provided with the distribution. 46f9bac91bSBenno Rice * 3. All advertising materials mentioning features or use of this software 47f9bac91bSBenno Rice * must display the following acknowledgement: 48f9bac91bSBenno Rice * This product includes software developed by TooLs GmbH. 49f9bac91bSBenno Rice * 4. The name of TooLs GmbH may not be used to endorse or promote products 50f9bac91bSBenno Rice * derived from this software without specific prior written permission. 51f9bac91bSBenno Rice * 52f9bac91bSBenno Rice * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 53f9bac91bSBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 54f9bac91bSBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55f9bac91bSBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 56f9bac91bSBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 57f9bac91bSBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 58f9bac91bSBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 59f9bac91bSBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 60f9bac91bSBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 61f9bac91bSBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62f9bac91bSBenno Rice * 63ffb56695SRafal Jaworowski * from: $NetBSD: pmap.h,v 1.17 2000/03/30 16:18:24 jdolecek Exp $ 64f9bac91bSBenno Rice */ 65f9bac91bSBenno Rice 66f9bac91bSBenno Rice #ifndef _MACHINE_PMAP_H_ 67f9bac91bSBenno Rice #define _MACHINE_PMAP_H_ 68f9bac91bSBenno Rice 6948d0b1a0SAlan Cox #include <sys/queue.h> 70c3e289e1SNathan Whitehorn #include <sys/tree.h> 71c47dd3dbSAttilio Rao #include <sys/_cpuset.h> 7248d0b1a0SAlan Cox #include <sys/_lock.h> 7348d0b1a0SAlan Cox #include <sys/_mutex.h> 745244eac9SBenno Rice #include <machine/sr.h> 757f89270bSPeter Grehan #include <machine/pte.h> 76c3e289e1SNathan Whitehorn #include <machine/slb.h> 77ffb56695SRafal Jaworowski #include <machine/tlb.h> 78ffb56695SRafal Jaworowski 794026b447SJustin Hibbits struct pmap; 804026b447SJustin Hibbits typedef struct pmap *pmap_t; 814026b447SJustin Hibbits 82ffb56695SRafal Jaworowski #if defined(AIM) 83f9bac91bSBenno Rice 847c277971SPeter Grehan #if !defined(NPMAPS) 857c277971SPeter Grehan #define NPMAPS 32768 867c277971SPeter Grehan #endif /* !defined(NPMAPS) */ 877c277971SPeter Grehan 8895fa3335SNathan Whitehorn struct slbtnode; 89f9bac91bSBenno Rice 905244eac9SBenno Rice struct pvo_entry { 915244eac9SBenno Rice LIST_ENTRY(pvo_entry) pvo_vlink; /* Link to common virt page */ 92827cc9b9SNathan Whitehorn #ifndef __powerpc64__ 935244eac9SBenno Rice LIST_ENTRY(pvo_entry) pvo_olink; /* Link to overflow entry */ 94827cc9b9SNathan Whitehorn #endif 95ccc4a5c7SNathan Whitehorn RB_ENTRY(pvo_entry) pvo_plink; /* Link to pmap entries */ 96827cc9b9SNathan Whitehorn struct { 97827cc9b9SNathan Whitehorn #ifndef __powerpc64__ 98827cc9b9SNathan Whitehorn /* 32-bit fields */ 99827cc9b9SNathan Whitehorn struct pte pte; 100827cc9b9SNathan Whitehorn #endif 101827cc9b9SNathan Whitehorn /* 64-bit fields */ 102827cc9b9SNathan Whitehorn uintptr_t slot; 103827cc9b9SNathan Whitehorn vm_paddr_t pa; 104827cc9b9SNathan Whitehorn vm_prot_t prot; 10552a7870dSNathan Whitehorn } pvo_pte; 1065244eac9SBenno Rice pmap_t pvo_pmap; /* Owning pmap */ 1075244eac9SBenno Rice vm_offset_t pvo_vaddr; /* VA of entry */ 108c3e289e1SNathan Whitehorn uint64_t pvo_vpn; /* Virtual page number */ 1095244eac9SBenno Rice }; 1105244eac9SBenno Rice LIST_HEAD(pvo_head, pvo_entry); 111ccc4a5c7SNathan Whitehorn RB_HEAD(pvo_tree, pvo_entry); 112ccc4a5c7SNathan Whitehorn int pvo_vaddr_compare(struct pvo_entry *, struct pvo_entry *); 113ccc4a5c7SNathan Whitehorn RB_PROTOTYPE(pvo_tree, pvo_entry, pvo_plink, pvo_vaddr_compare); 1145244eac9SBenno Rice 115827cc9b9SNathan Whitehorn /* Used by 32-bit PMAP */ 116bef5da7fSNathan Whitehorn #define PVO_PTEGIDX_MASK 0x007UL /* which PTEG slot */ 117bef5da7fSNathan Whitehorn #define PVO_PTEGIDX_VALID 0x008UL /* slot is valid */ 118827cc9b9SNathan Whitehorn /* Used by 64-bit PMAP */ 119827cc9b9SNathan Whitehorn #define PVO_HID 0x008UL /* PVO entry in alternate hash*/ 120827cc9b9SNathan Whitehorn /* Used by both */ 121bef5da7fSNathan Whitehorn #define PVO_WIRED 0x010UL /* PVO entry is wired */ 122bef5da7fSNathan Whitehorn #define PVO_MANAGED 0x020UL /* PVO entry is managed */ 123bef5da7fSNathan Whitehorn #define PVO_BOOTSTRAP 0x080UL /* PVO entry allocated during 124bef5da7fSNathan Whitehorn bootstrap */ 125827cc9b9SNathan Whitehorn #define PVO_DEAD 0x100UL /* waiting to be deleted */ 126bef5da7fSNathan Whitehorn #define PVO_LARGE 0x200UL /* large page */ 127bef5da7fSNathan Whitehorn #define PVO_VADDR(pvo) ((pvo)->pvo_vaddr & ~ADDR_POFF) 128bef5da7fSNathan Whitehorn #define PVO_PTEGIDX_GET(pvo) ((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK) 129bef5da7fSNathan Whitehorn #define PVO_PTEGIDX_ISSET(pvo) ((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID) 130bef5da7fSNathan Whitehorn #define PVO_PTEGIDX_CLR(pvo) \ 131bef5da7fSNathan Whitehorn ((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK))) 132bef5da7fSNathan Whitehorn #define PVO_PTEGIDX_SET(pvo, i) \ 133bef5da7fSNathan Whitehorn ((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID)) 134bef5da7fSNathan Whitehorn #define PVO_VSID(pvo) ((pvo)->pvo_vpn >> 16) 135bef5da7fSNathan Whitehorn 136598d99ddSNathan Whitehorn struct pmap { 1374026b447SJustin Hibbits struct pmap_statistics pm_stats; 138598d99ddSNathan Whitehorn struct mtx pm_mtx; 139598d99ddSNathan Whitehorn 140598d99ddSNathan Whitehorn #ifdef __powerpc64__ 141598d99ddSNathan Whitehorn struct slbtnode *pm_slb_tree_root; 142598d99ddSNathan Whitehorn struct slb **pm_slb; 143598d99ddSNathan Whitehorn int pm_slb_len; 144598d99ddSNathan Whitehorn #else 145598d99ddSNathan Whitehorn register_t pm_sr[16]; 146598d99ddSNathan Whitehorn #endif 147598d99ddSNathan Whitehorn cpuset_t pm_active; 148598d99ddSNathan Whitehorn 149598d99ddSNathan Whitehorn struct pmap *pmap_phys; 150ccc4a5c7SNathan Whitehorn struct pvo_tree pmap_pvo; 151598d99ddSNathan Whitehorn }; 152598d99ddSNathan Whitehorn 1535244eac9SBenno Rice struct md_page { 154827cc9b9SNathan Whitehorn volatile int32_t mdpg_attrs; 155c1f4123bSNathan Whitehorn vm_memattr_t mdpg_cache_attrs; 1565244eac9SBenno Rice struct pvo_head mdpg_pvoh; 1575244eac9SBenno Rice }; 1585244eac9SBenno Rice 159c1f4123bSNathan Whitehorn #define pmap_page_get_memattr(m) ((m)->md.mdpg_cache_attrs) 160ffb56695SRafal Jaworowski #define pmap_page_is_mapped(m) (!LIST_EMPTY(&(m)->md.mdpg_pvoh)) 161ffb56695SRafal Jaworowski 162c3e289e1SNathan Whitehorn /* 163c3e289e1SNathan Whitehorn * Return the VSID corresponding to a given virtual address. 164c3e289e1SNathan Whitehorn * If no VSID is currently defined, it will allocate one, and add 165c3e289e1SNathan Whitehorn * it to a free slot if available. 166c3e289e1SNathan Whitehorn * 167c3e289e1SNathan Whitehorn * NB: The PMAP MUST be locked already. 168c3e289e1SNathan Whitehorn */ 169c3e289e1SNathan Whitehorn uint64_t va_to_vsid(pmap_t pm, vm_offset_t va); 17095fa3335SNathan Whitehorn 17195fa3335SNathan Whitehorn /* Lock-free, non-allocating lookup routines */ 17295fa3335SNathan Whitehorn uint64_t kernel_va_to_slbv(vm_offset_t va); 17395fa3335SNathan Whitehorn struct slb *user_va_to_slb_entry(pmap_t pm, vm_offset_t va); 174c3e289e1SNathan Whitehorn 1756416b9a8SNathan Whitehorn uint64_t allocate_user_vsid(pmap_t pm, uint64_t esid, int large); 17695fa3335SNathan Whitehorn void free_vsid(pmap_t pm, uint64_t esid, int large); 1776416b9a8SNathan Whitehorn void slb_insert_user(pmap_t pm, struct slb *slb); 1786416b9a8SNathan Whitehorn void slb_insert_kernel(uint64_t slbe, uint64_t slbv); 17995fa3335SNathan Whitehorn 18095fa3335SNathan Whitehorn struct slbtnode *slb_alloc_tree(void); 18195fa3335SNathan Whitehorn void slb_free_tree(pmap_t pm); 1826416b9a8SNathan Whitehorn struct slb **slb_alloc_user_cache(void); 1836416b9a8SNathan Whitehorn void slb_free_user_cache(struct slb **); 184c3e289e1SNathan Whitehorn 185fcef00c3SJustin Hibbits #elif defined(BOOKE) 186ffb56695SRafal Jaworowski 187ffb56695SRafal Jaworowski struct pmap { 1884026b447SJustin Hibbits struct pmap_statistics pm_stats; /* pmap statistics */ 189ffb56695SRafal Jaworowski struct mtx pm_mtx; /* pmap mutex */ 190b2b734e7SRafal Jaworowski tlbtid_t pm_tid[MAXCPU]; /* TID to identify this pmap entries in TLB */ 191c47dd3dbSAttilio Rao cpuset_t pm_active; /* active on cpus */ 192ffb56695SRafal Jaworowski 193e683c328SJustin Hibbits #ifdef __powerpc64__ 194e683c328SJustin Hibbits /* Page table directory, array of pointers to page directories. */ 195e683c328SJustin Hibbits pte_t **pm_pp2d[PP2D_NENTRIES]; 196e683c328SJustin Hibbits 197e683c328SJustin Hibbits /* List of allocated pdir bufs (pdir kva regions). */ 198e683c328SJustin Hibbits TAILQ_HEAD(, ptbl_buf) pm_pdir_list; 199e683c328SJustin Hibbits #else 200ffb56695SRafal Jaworowski /* Page table directory, array of pointers to page tables. */ 201ffb56695SRafal Jaworowski pte_t *pm_pdir[PDIR_NENTRIES]; 202e683c328SJustin Hibbits #endif 203ffb56695SRafal Jaworowski 204ffb56695SRafal Jaworowski /* List of allocated ptbl bufs (ptbl kva regions). */ 205b2b734e7SRafal Jaworowski TAILQ_HEAD(, ptbl_buf) pm_ptbl_list; 206ffb56695SRafal Jaworowski }; 207ffb56695SRafal Jaworowski 208ffb56695SRafal Jaworowski struct pv_entry { 209ffb56695SRafal Jaworowski pmap_t pv_pmap; 210ffb56695SRafal Jaworowski vm_offset_t pv_va; 211ffb56695SRafal Jaworowski TAILQ_ENTRY(pv_entry) pv_link; 212ffb56695SRafal Jaworowski }; 213ffb56695SRafal Jaworowski typedef struct pv_entry *pv_entry_t; 214ffb56695SRafal Jaworowski 215ffb56695SRafal Jaworowski struct md_page { 216ffb56695SRafal Jaworowski TAILQ_HEAD(, pv_entry) pv_list; 217fcef00c3SJustin Hibbits int pv_tracked; 218ffb56695SRafal Jaworowski }; 219ffb56695SRafal Jaworowski 2203153e878SAlan Cox #define pmap_page_get_memattr(m) VM_MEMATTR_DEFAULT 221ffb56695SRafal Jaworowski #define pmap_page_is_mapped(m) (!TAILQ_EMPTY(&(m)->md.pv_list)) 222ffb56695SRafal Jaworowski 2234026b447SJustin Hibbits #else 2244026b447SJustin Hibbits /* 2254026b447SJustin Hibbits * Common pmap members between AIM and BOOKE. 2264026b447SJustin Hibbits * libkvm needs pm_stats at the same location between both, as it doesn't define 2274026b447SJustin Hibbits * AIM nor BOOKE, and is expected to work across all. 2284026b447SJustin Hibbits */ 2294026b447SJustin Hibbits struct pmap { 2304026b447SJustin Hibbits struct pmap_statistics pm_stats; /* pmap statistics */ 2314026b447SJustin Hibbits struct mtx pm_mtx; /* pmap mutex */ 2324026b447SJustin Hibbits }; 233ffb56695SRafal Jaworowski #endif /* AIM */ 234ffb56695SRafal Jaworowski 2355244eac9SBenno Rice extern struct pmap kernel_pmap_store; 2365244eac9SBenno Rice #define kernel_pmap (&kernel_pmap_store) 2375244eac9SBenno Rice 238f9bac91bSBenno Rice #ifdef _KERNEL 239f9bac91bSBenno Rice 24048d0b1a0SAlan Cox #define PMAP_LOCK(pmap) mtx_lock(&(pmap)->pm_mtx) 24148d0b1a0SAlan Cox #define PMAP_LOCK_ASSERT(pmap, type) \ 24248d0b1a0SAlan Cox mtx_assert(&(pmap)->pm_mtx, (type)) 24348d0b1a0SAlan Cox #define PMAP_LOCK_DESTROY(pmap) mtx_destroy(&(pmap)->pm_mtx) 244629e40e4SNathan Whitehorn #define PMAP_LOCK_INIT(pmap) mtx_init(&(pmap)->pm_mtx, \ 245629e40e4SNathan Whitehorn (pmap == kernel_pmap) ? "kernelpmap" : \ 246629e40e4SNathan Whitehorn "pmap", NULL, MTX_DEF) 24748d0b1a0SAlan Cox #define PMAP_LOCKED(pmap) mtx_owned(&(pmap)->pm_mtx) 24848d0b1a0SAlan Cox #define PMAP_MTX(pmap) (&(pmap)->pm_mtx) 24948d0b1a0SAlan Cox #define PMAP_TRYLOCK(pmap) mtx_trylock(&(pmap)->pm_mtx) 25048d0b1a0SAlan Cox #define PMAP_UNLOCK(pmap) mtx_unlock(&(pmap)->pm_mtx) 25148d0b1a0SAlan Cox 2526031c68dSAlan Cox #define pmap_page_is_write_mapped(m) (((m)->aflags & PGA_WRITEABLE) != 0) 2536031c68dSAlan Cox 2545244eac9SBenno Rice void pmap_bootstrap(vm_offset_t, vm_offset_t); 25520b79612SRafal Jaworowski void pmap_kenter(vm_offset_t va, vm_paddr_t pa); 256611aec25SJustin Hibbits void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, vm_memattr_t); 2575501d40bSJake Burkholder void pmap_kremove(vm_offset_t); 25820b79612SRafal Jaworowski void *pmap_mapdev(vm_paddr_t, vm_size_t); 2592109efd1SJustin Hibbits void *pmap_mapdev_attr(vm_paddr_t, vm_size_t, vm_memattr_t); 2608bbfa33aSBenno Rice void pmap_unmapdev(vm_offset_t, vm_size_t); 261c1f4123bSNathan Whitehorn void pmap_page_set_memattr(vm_page_t, vm_memattr_t); 2620f7aeab0SJustin Hibbits int pmap_change_attr(vm_offset_t, vm_size_t, vm_memattr_t); 263ac6ba8bdSBenno Rice void pmap_deactivate(struct thread *); 26420b79612SRafal Jaworowski vm_paddr_t pmap_kextract(vm_offset_t); 26520b79612SRafal Jaworowski int pmap_dev_direct_mapped(vm_paddr_t, vm_size_t); 266f9c702dbSPeter Grehan boolean_t pmap_mmu_install(char *name, int prio); 267f9c702dbSPeter Grehan 268696effb6SJohn Baldwin #define vtophys(va) pmap_kextract((vm_offset_t)(va)) 269a0889814SBenno Rice 27005e1c598SAndreas Tobler #define PHYS_AVAIL_SZ 256 /* Allows up to 16GB Ram on pSeries with 27105e1c598SAndreas Tobler * logical memory block size of 64MB. 27205e1c598SAndreas Tobler * For more Ram increase the lmb or this value. 27305e1c598SAndreas Tobler */ 27405e1c598SAndreas Tobler 275b3936ebeSJustin Hibbits extern vm_paddr_t phys_avail[PHYS_AVAIL_SZ]; 276f9bac91bSBenno Rice extern vm_offset_t virtual_avail; 277f9bac91bSBenno Rice extern vm_offset_t virtual_end; 278f9bac91bSBenno Rice 2795244eac9SBenno Rice extern vm_offset_t msgbuf_phys; 280f9bac91bSBenno Rice 281f9c702dbSPeter Grehan extern int pmap_bootstrapped; 282f9c702dbSPeter Grehan 28333724f17SNathan Whitehorn vm_offset_t pmap_early_io_map(vm_paddr_t pa, vm_size_t size); 28460152a40SJustin Hibbits void pmap_early_io_unmap(vm_offset_t va, vm_size_t size); 285b2f831c0SJustin Hibbits void pmap_track_page(pmap_t pmap, vm_offset_t va); 28633724f17SNathan Whitehorn 287f9bac91bSBenno Rice #endif 288f9bac91bSBenno Rice 2895244eac9SBenno Rice #endif /* !_MACHINE_PMAP_H_ */ 290