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