| 1 | /* $NetBSD: pmap.h,v 1.24.2.2 2026/04/02 18:13:22 martin Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2014, 2019, 2021 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Matt Thomas (of 3am Software Foundry), Maxime Villard, and |
| 9 | * Nick Hudson. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #ifndef _RISCV_PMAP_H_ |
| 34 | #define	_RISCV_PMAP_H_ |
| 35 | |
| 36 | #ifdef _KERNEL_OPT |
| 37 | #include "opt_modular.h" |
| 38 | #endif |
| 39 | |
| 40 | #if !defined(_MODULE) |
| 41 | |
| 42 | #include <sys/cdefs.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/pool.h> |
| 45 | #include <sys/evcnt.h> |
| 46 | |
| 47 | #include <uvm/uvm_physseg.h> |
| 48 | #include <uvm/pmap/vmpagemd.h> |
| 49 | |
| 50 | #include <riscv/pte.h> |
| 51 | #include <riscv/sysreg.h> |
| 52 | |
| 53 | #define	PMAP_SEGTABSIZE	NPTEPG |
| 54 | #define	PMAP_PDETABSIZE	NPTEPG |
| 55 | |
| 56 | #ifdef _LP64 |
| 57 | #define	PTPSHIFT	3 |
| 58 | /* This is SV57. */ |
| 59 | //#define	XSEGSHIFT	(SEGSHIFT + SEGLENGTH + SEGLENGTH + SEGLENGTH) |
| 60 | |
| 61 | /* This is SV48. */ |
| 62 | //#define	XSEGSHIFT	(SEGSHIFT + SEGLENGTH + SEGLENGTH) |
| 63 | |
| 64 | /* This is SV39. */ |
| 65 | #define	XSEGSHIFT	(SEGSHIFT + SEGLENGTH) |
| 66 | #define	NBXSEG		(1ULL << XSEGSHIFT) |
| 67 | #define	XSEGOFSET	(NBXSEG - 1)		/* byte offset into xsegment */ |
| 68 | #define	XSEGLENGTH	(PGSHIFT - 3) |
| 69 | #define	NXSEGPG		(1 << XSEGLENGTH) |
| 70 | #else |
| 71 | #define	PTPSHIFT	2 |
| 72 | #define	XSEGSHIFT	SEGSHIFT |
| 73 | #endif |
| 74 | |
| 75 | #define	SEGLENGTH	(PGSHIFT - PTPSHIFT) |
| 76 | #define	SEGSHIFT	(SEGLENGTH + PGSHIFT) |
| 77 | #define	NBSEG		(1 << SEGSHIFT)		/* bytes/segment */ |
| 78 | #define	SEGOFSET	(NBSEG - 1)		/* byte offset into segment */ |
| 79 | |
| 80 | #define	KERNEL_PID	0 |
| 81 | |
| 82 | #define	PMAP_HWPAGEWALKER		1 |
| 83 | #define	PMAP_TLB_MAX			1 |
| 84 | #define	PMAP_TLB_ALWAYS_ASIDS		false |
| 85 | #ifdef _LP64 |
| 86 | #define	PMAP_INVALID_PDETAB_ADDRESS	((pmap_pdetab_t *)(VM_MIN_KERNEL_ADDRESS - PAGE_SIZE)) |
| 87 | #define	PMAP_INVALID_SEGTAB_ADDRESS	((pmap_segtab_t *)(VM_MIN_KERNEL_ADDRESS - PAGE_SIZE)) |
| 88 | #else |
| 89 | #define	PMAP_INVALID_PDETAB_ADDRESS	((pmap_pdetab_t *)0xdeadbeef) |
| 90 | #define	PMAP_INVALID_SEGTAB_ADDRESS	((pmap_segtab_t *)0xdeadbeef) |
| 91 | #endif |
| 92 | #define	PMAP_TLB_NUM_PIDS		(__SHIFTOUT_MASK(SATP_ASID) + 1) |
| 93 | #define	PMAP_TLB_BITMAP_LENGTH		PMAP_TLB_NUM_PIDS |
| 94 | #define	PMAP_TLB_FLUSH_ASID_ON_RESET	true |
| 95 | |
| 96 | #define	pmap_phys_address(x)		(x) |
| 97 | |
| 98 | #ifndef __BSD_PTENTRY_T__ |
| 99 | #define	__BSD_PTENTRY_T__ |
| 100 | #ifdef _LP64 |
| 101 | #define	PRIxPTE		PRIx64 |
| 102 | #else |
| 103 | #define	PRIxPTE		PRIx32 |
| 104 | #endif |
| 105 | #endif /* __BSD_PTENTRY_T__ */ |
| 106 | |
| 107 | #define	PMAP_NEED_PROCWR |
| 108 | static inline void |
| 109 | pmap_procwr(struct proc *p, vaddr_t va, vsize_t len) |
| 110 | { |
| 111 | 	__asm __volatile("fence\trw,rw; fence.i" ::: "memory"); |
| 112 | } |
| 113 | |
| 114 | #include <uvm/pmap/tlb.h> |
| 115 | #include <uvm/pmap/pmap_devmap.h> |
| 116 | #include <uvm/pmap/pmap_tlb.h> |
| 117 | #include <uvm/pmap/pmap_synci.h> |
| 118 | |
| 119 | #define	PMAP_GROWKERNEL |
| 120 | #define	PMAP_STEAL_MEMORY |
| 121 | |
| 122 | #ifdef _KERNEL |
| 123 | |
| 124 | #define	__HAVE_PMAP_MD |
| 125 | struct pmap_md { |
| 126 | 	paddr_t md_ppn; |
| 127 | }; |
| 128 | |
| 129 | static inline void |
| 130 | pmap_md_icache_sync_all(void) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | static inline void |
| 135 | pmap_md_icache_sync_range_index(vaddr_t va, vsize_t size) |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | struct vm_page * |
| 140 | 	pmap_md_alloc_poolpage(int); |
| 141 | vaddr_t	pmap_md_map_poolpage(paddr_t, vsize_t); |
| 142 | void	pmap_md_unmap_poolpage(vaddr_t, vsize_t); |
| 143 | |
| 144 | bool	pmap_md_direct_mapped_vaddr_p(vaddr_t); |
| 145 | paddr_t	pmap_md_direct_mapped_vaddr_to_paddr(vaddr_t); |
| 146 | vaddr_t	pmap_md_direct_map_paddr(paddr_t); |
| 147 | void	pmap_md_init(void); |
| 148 | bool	pmap_md_io_vaddr_p(vaddr_t); |
| 149 | bool	pmap_md_ok_to_steal_p(const uvm_physseg_t, size_t); |
| 150 | void	pmap_md_pdetab_init(struct pmap *); |
| 151 | void	pmap_md_pdetab_fini(struct pmap *); |
| 152 | void	pmap_md_tlb_info_attach(struct pmap_tlb_info *, struct cpu_info *); |
| 153 | void	pmap_md_xtab_activate(struct pmap *, struct lwp *); |
| 154 | void	pmap_md_xtab_deactivate(struct pmap *); |
| 155 | |
| 156 | void	pmap_pte_xmae(void); |
| 157 | void	pmap_bootstrap(vaddr_t, vaddr_t); |
| 158 | |
| 159 | vsize_t	pmap_kenter_range(vaddr_t, paddr_t, vsize_t, vm_prot_t, u_int); |
| 160 | |
| 161 | #ifdef _LP64 |
| 162 | extern vaddr_t pmap_direct_base; |
| 163 | extern vaddr_t pmap_direct_end; |
| 164 | #define	PMAP_DIRECT_MAP(pa)	RISCV_PA_TO_KVA(pa) |
| 165 | #define	PMAP_DIRECT_UNMAP(va)	RISCV_KVA_TO_PA(va) |
| 166 | |
| 167 | /* |
| 168 | * Other hooks for the pool allocator. |
| 169 | */ |
| 170 | #define	POOL_PHYSTOV(pa)	RISCV_PA_TO_KVA((paddr_t)(pa)) |
| 171 | #define	POOL_VTOPHYS(va)	RISCV_KVA_TO_PA((vaddr_t)(va)) |
| 172 | |
| 173 | #endif	/* _LP64 */ |
| 174 | |
| 175 | #define	MEGAPAGE_TRUNC(x)	((x) & ~SEGOFSET) |
| 176 | #define	MEGAPAGE_ROUND(x)	MEGAPAGE_TRUNC((x) + SEGOFSET) |
| 177 | |
| 178 | #define	PMAP_DEV		__BIT(29)	/* 0x2000_0000 */ |
| 179 | |
| 180 | #define	DEVMAP_ALIGN(x)		MEGAPAGE_TRUNC((x)) |
| 181 | #define	DEVMAP_SIZE(x)		MEGAPAGE_ROUND((x)) |
| 182 | #define	DEVMAP_FLAGS		PMAP_DEV |
| 183 | |
| 184 | #ifdef __PMAP_PRIVATE |
| 185 | |
| 186 | static inline bool |
| 187 | pmap_md_tlb_check_entry(void *ctx, vaddr_t va, tlb_asid_t asid, pt_entry_t pte) |
| 188 | { |
| 189 | 	// TLB not walked and so not called. |
| 190 | 	return false; |
| 191 | } |
| 192 | |
| 193 | static inline void |
| 194 | pmap_md_page_syncicache(struct vm_page_md *mdpg, const kcpuset_t *onproc) |
| 195 | { |
| 196 | 	__asm __volatile("fence\trw,rw; fence.i" ::: "memory"); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Virtual Cache Alias helper routines. Not a problem for RISCV CPUs. |
| 201 | */ |
| 202 | static inline bool |
| 203 | pmap_md_vca_add(struct vm_page_md *mdpg, vaddr_t va, pt_entry_t *nptep) |
| 204 | { |
| 205 | 	return false; |
| 206 | } |
| 207 | |
| 208 | static inline void |
| 209 | pmap_md_vca_remove(struct vm_page_md *mdpg, vaddr_t va) |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | static inline void |
| 214 | pmap_md_vca_clean(struct vm_page_md *mdpg, vaddr_t va, int op) |
| 215 | { |
| 216 | } |
| 217 | |
| 218 | static inline size_t |
| 219 | pmap_md_tlb_asid_max(void) |
| 220 | { |
| 221 | 	const register_t satp = csr_satp_read(); |
| 222 | 	const register_t test = satp | SATP_ASID; |
| 223 | |
| 224 | 	csr_satp_write(test); |
| 225 | |
| 226 | 	const register_t ret = __SHIFTOUT(csr_satp_read(), SATP_ASID); |
| 227 | 	csr_satp_write(satp); |
| 228 | |
| 229 | 	KASSERT(ret < PMAP_TLB_NUM_PIDS); |
| 230 | 	return ret; |
| 231 | } |
| 232 | |
| 233 | static inline pt_entry_t * |
| 234 | pmap_md_nptep(pt_entry_t *ptep) |
| 235 | { |
| 236 | 	return ptep + 1; |
| 237 | } |
| 238 | |
| 239 | #endif /* __PMAP_PRIVATE */ |
| 240 | #endif /* _KERNEL */ |
| 241 | |
| 242 | #include <uvm/pmap/pmap.h> |
| 243 | |
| 244 | #endif /* !_MODULE */ |
| 245 | |
| 246 | #if defined(MODULAR) || defined(_MODULE) |
| 247 | /* |
| 248 | * Define a compatible vm_page_md so that struct vm_page is the same size |
| 249 | * whether we are using modules or not. |
| 250 | */ |
| 251 | #ifndef __HAVE_VM_PAGE_MD |
| 252 | #define	__HAVE_VM_PAGE_MD |
| 253 | |
| 254 | struct vm_page_md { |
| 255 | 	uintptr_t mdpg_dummy[3]; |
| 256 | }; |
| 257 | __CTASSERT(sizeof(struct vm_page_md) == sizeof(uintptr_t)*3); |
| 258 | |
| 259 | #endif /* !__HAVE_VM_PAGE_MD */ |
| 260 | |
| 261 | #endif /* MODULAR || _MODULE */ |
| 262 | |
| 263 | #endif /* !_RISCV_PMAP_H_ */ |