1 /* $NetBSD: subr_physmap.c,v 1.7 2026/01/04 03:20:11 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 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. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(1, "$NetBSD: subr_physmap.c,v 1.7 2026/01/04 03:20:11 riastradh Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 38 #include <sys/kmem.h> 39 #include <sys/physmap.h> 40 #include <sys/sdt.h> 41 42 #include <uvm/uvm_extern.h> 43 #include <uvm/uvm_page.h> 44 45 #include <dev/mm.h> 46 47 /* 48 * This file contain support routines used to create and destroy lists of 49 * physical pages from lists of pages or ranges of virtual address. By using 50 * these physical maps, the kernel can avoid mapping physical I/O in the 51 * kernel's address space in most cases. 52 */ 53 54 typedef struct { 55 physmap_t *pc_physmap; 56 physmap_segment_t *pc_segs; 57 vsize_t pc_offset; 58 vsize_t pc_klen; 59 vaddr_t pc_kva; 60 u_int pc_nsegs; 61 vm_prot_t pc_prot; 62 bool pc_direct_mapped; 63 } physmap_cookie_t; 64 65 /* 66 * Allocate a physmap structure that requires "maxsegs" segments. 67 */ 68 static physmap_t * 69 physmap_alloc(size_t maxsegs) 70 { 71 const size_t mapsize = offsetof(physmap_t, pm_segs[maxsegs]); 72 73 KASSERT(maxsegs > 0); 74 75 physmap_t * const map = kmem_zalloc(mapsize, KM_SLEEP); 76 map->pm_maxsegs = maxsegs; 77 78 return map; 79 } 80 81 static int 82 physmap_fill(physmap_t *map, pmap_t pmap, vaddr_t va, vsize_t len) 83 { 84 size_t nsegs = map->pm_nsegs; 85 physmap_segment_t *ps = &map->pm_segs[nsegs]; 86 vsize_t offset = va - trunc_page(va); 87 88 if (nsegs == 0) { 89 if (!pmap_extract(pmap, va, &ps->ps_addr)) { 90 return SET_ERROR(EFAULT); 91 } 92 ps->ps_len = MIN(len, PAGE_SIZE - offset); 93 if (ps->ps_len == len) { 94 map->pm_nsegs = 1; 95 return 0; 96 } 97 offset = 0; 98 } else { 99 /* 100 * Backup to the last segment since we have to see if we can 101 * merge virtual addresses that are physically contiguous into 102 * as few segments as possible. 103 */ 104 ps--; 105 nsegs--; 106 } 107 108 paddr_t lastaddr = ps->ps_addr + ps->ps_len; 109 for (;;) { 110 paddr_t curaddr; 111 if (!pmap_extract(pmap, va, &curaddr)) { 112 return SET_ERROR(EFAULT); 113 } 114 if (curaddr != lastaddr) { 115 ps++; 116 nsegs++; 117 KASSERT(nsegs < map->pm_maxsegs); 118 ps->ps_addr = curaddr; 119 lastaddr = curaddr; 120 } 121 if (offset + len > PAGE_SIZE) { 122 ps->ps_len += PAGE_SIZE - offset; 123 lastaddr = ps->ps_addr + ps->ps_len; 124 len -= PAGE_SIZE - offset; 125 lastaddr += PAGE_SIZE - offset; 126 offset = 0; 127 } else { 128 ps->ps_len += len; 129 map->pm_nsegs = nsegs + 1; 130 return 0; 131 } 132 } 133 } 134 135 /* 136 * Create a physmap and populate it with the pages that are used to mapped 137 * linear range of virtual addresses. It is assumed that uvm_vslock has been 138 * called to lock these pages into memory. 139 */ 140 int 141 physmap_create_linear(physmap_t **map_p, const struct vmspace *vs, vaddr_t va, 142 vsize_t len) 143 { 144 const size_t maxsegs = atop(round_page(va + len) - trunc_page(va)); 145 physmap_t * const map = physmap_alloc(maxsegs); 146 int error = physmap_fill(map, vs->vm_map.pmap, va, len); 147 if (error) { 148 physmap_destroy(map); 149 *map_p = NULL; 150 return error; 151 } 152 *map_p = map; 153 return 0; 154 } 155 156 /* 157 * Create a physmap and populate it with the pages that are contained in an 158 * iovec array. It is assumed that uvm_vslock has been called to lock these 159 * pages into memory. 160 */ 161 int 162 physmap_create_iov(physmap_t **map_p, const struct vmspace *vs, 163 struct iovec *iov, size_t iovlen) 164 { 165 size_t maxsegs = 0; 166 for (size_t i = 0; i < iovlen; i++) { 167 const vaddr_t start = (vaddr_t) iov[i].iov_base; 168 const vaddr_t end = start + iov[i].iov_len; 169 maxsegs += atop(round_page(end) - trunc_page(start)); 170 } 171 physmap_t * const map = physmap_alloc(maxsegs); 172 173 for (size_t i = 0; i < iovlen; i++) { 174 int error = physmap_fill(map, vs->vm_map.pmap, 175 (vaddr_t) iov[i].iov_base, iov[i].iov_len); 176 if (error) { 177 physmap_destroy(map); 178 *map_p = NULL; 179 return error; 180 } 181 } 182 *map_p = map; 183 return 0; 184 } 185 186 /* 187 * This uses a list of vm_page structure to create a physmap. 188 */ 189 physmap_t * 190 physmap_create_pagelist(struct vm_page **pgs, size_t npgs) 191 { 192 physmap_t * const map = physmap_alloc(npgs); 193 194 physmap_segment_t *ps = map->pm_segs; 195 196 /* 197 * Initialize the first segment. 198 */ 199 paddr_t lastaddr = VM_PAGE_TO_PHYS(pgs[0]); 200 ps->ps_addr = lastaddr; 201 ps->ps_len = PAGE_SIZE; 202 203 for (pgs++; npgs-- > 1; pgs++) { 204 /* 205 * lastaddr needs to be increased by a page. 206 */ 207 lastaddr += PAGE_SIZE; 208 paddr_t curaddr = VM_PAGE_TO_PHYS(*pgs); 209 if (curaddr != lastaddr) { 210 /* 211 * If the addresses are not the same, we need to use 212 * a new segment. Set its address and update lastaddr. 213 */ 214 ps++; 215 ps->ps_addr = curaddr; 216 lastaddr = curaddr; 217 } 218 /* 219 * Increase this segment's length by a page 220 */ 221 ps->ps_len += PAGE_SIZE; 222 } 223 224 map->pm_nsegs = ps + 1 - map->pm_segs; 225 return map; 226 } 227 228 void 229 physmap_destroy(physmap_t *map) 230 { 231 const size_t mapsize = offsetof(physmap_t, pm_segs[map->pm_maxsegs]); 232 233 kmem_free(map, mapsize); 234 } 235 236 void * 237 physmap_map_init(physmap_t *map, size_t offset, vm_prot_t prot) 238 { 239 physmap_cookie_t * const pc = kmem_zalloc(sizeof(*pc), KM_SLEEP); 240 241 KASSERT(prot == VM_PROT_READ || prot == (VM_PROT_READ|VM_PROT_WRITE)); 242 243 pc->pc_physmap = map; 244 pc->pc_segs = map->pm_segs; 245 pc->pc_nsegs = map->pm_nsegs; 246 pc->pc_prot = prot; 247 pc->pc_klen = 0; 248 pc->pc_kva = 0; 249 pc->pc_direct_mapped = false; 250 251 /* 252 * Skip to the first segment we are interested in. 253 */ 254 while (offset >= pc->pc_segs->ps_len) { 255 offset -= pc->pc_segs->ps_len; 256 pc->pc_segs++; 257 pc->pc_nsegs--; 258 } 259 260 pc->pc_offset = offset; 261 262 return pc; 263 } 264 265 size_t 266 physmap_map(void *cookie, vaddr_t *kvap) 267 { 268 physmap_cookie_t * const pc = cookie; 269 270 /* 271 * If there is currently a non-direct mapped KVA region allocated, 272 * free it now. 273 */ 274 if (pc->pc_kva != 0 && !pc->pc_direct_mapped) { 275 pmap_kremove(pc->pc_kva, pc->pc_klen); 276 pmap_update(pmap_kernel()); 277 uvm_km_free(kernel_map, pc->pc_kva, pc->pc_klen, 278 UVM_KMF_VAONLY); 279 } 280 281 /* 282 * If there are no more segments to process, return 0 indicating 283 * we are done. 284 */ 285 if (pc->pc_nsegs == 0) { 286 return 0; 287 } 288 289 /* 290 * Get starting physical address of this segment and its length. 291 */ 292 paddr_t pa = pc->pc_segs->ps_addr + pc->pc_offset; 293 const size_t koff = pa & PAGE_MASK; 294 const size_t len = pc->pc_segs->ps_len - pc->pc_offset; 295 296 /* 297 * Now that we have the starting offset in the page, reset to the 298 * beginning of the page. 299 */ 300 pa = trunc_page(pa); 301 302 /* 303 * We are now done with this segment; advance to the next one. 304 */ 305 pc->pc_segs++; 306 pc->pc_nsegs--; 307 pc->pc_offset = 0; 308 309 /* 310 * Find out how many pages we are mapping. 311 */ 312 pc->pc_klen = round_page(len); 313 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS 314 /* 315 * Always try to direct map it since that's nearly zero cost. 316 */ 317 pc->pc_direct_mapped = mm_md_direct_mapped_phys(pa, &pc->pc_kva); 318 #endif 319 if (!pc->pc_direct_mapped) { 320 /* 321 * If we can't direct map it, we have to allocate some KVA 322 * so we map it via the kernel_map. 323 */ 324 pc->pc_kva = uvm_km_alloc(kernel_map, pc->pc_klen, 325 atop(pa) & uvmexp.colormask, 326 UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH); 327 KASSERT(pc->pc_kva != 0); 328 329 /* 330 * Setup mappings for this segment. 331 */ 332 for (size_t poff = 0; poff < pc->pc_klen; poff += PAGE_SIZE) { 333 pmap_kenter_pa(pc->pc_kva + poff, pa + poff, 334 pc->pc_prot, 0); 335 } 336 /* 337 * Make them real. 338 */ 339 pmap_update(pmap_kernel()); 340 } 341 /* 342 * Return the starting KVA (including offset into the page) and 343 * the length of this segment. 344 */ 345 *kvap = pc->pc_kva + koff; 346 return len; 347 } 348 349 void 350 physmap_map_fini(void *cookie) 351 { 352 physmap_cookie_t * const pc = cookie; 353 354 /* 355 * If there is currently a non-direct mapped KVA region allocated, 356 * free it now. 357 */ 358 if (pc->pc_kva != 0 && !pc->pc_direct_mapped) { 359 pmap_kremove(pc->pc_kva, pc->pc_klen); 360 pmap_update(pmap_kernel()); 361 uvm_km_free(kernel_map, pc->pc_kva, pc->pc_klen, 362 UVM_KMF_VAONLY); 363 } 364 365 /* 366 * Free the cookie. 367 */ 368 kmem_free(pc, sizeof(*pc)); 369 } 370 371 /* 372 * genio needs to zero pages past the EOF or without backing storage (think 373 * sparse files). But since we are using physmaps, there is no kva to use with 374 * memset so we need a helper to obtain a kva and memset the desired memory. 375 */ 376 void 377 physmap_zero(physmap_t *map, size_t offset, size_t len) 378 { 379 void * const cookie = physmap_map_init(map, offset, 380 VM_PROT_READ|VM_PROT_WRITE); 381 382 for (;;) { 383 vaddr_t kva; 384 size_t seglen = physmap_map(cookie, &kva); 385 KASSERT(seglen != 0); 386 if (seglen > len) 387 seglen = len; 388 memset((void *)kva, 0, seglen); 389 if (seglen == len) 390 break; 391 } 392 393 physmap_map_fini(cookie); 394 } 395