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