Home | History | Annotate | Line # | Download | only in linux
linux_kmap.c revision 1.7
      1 /*	$NetBSD: linux_kmap.c,v 1.7 2014/08/27 16:09:16 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 Taylor R. Campbell.
      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(0, "$NetBSD: linux_kmap.c,v 1.7 2014/08/27 16:09:16 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/kmem.h>
     37 #include <sys/mutex.h>
     38 #include <sys/rbtree.h>
     39 
     40 #include <uvm/uvm_extern.h>
     41 
     42 #include <linux/highmem.h>
     43 
     44 /*
     45  * XXX Kludgerific implementation of Linux kmap_atomic, which is
     46  * required not to fail.  To accomodate this, we reserve one page of
     47  * kva at boot (or load) and limit the system to at most kmap_atomic in
     48  * use at a time.
     49  */
     50 
     51 /*
     52  * XXX Use direct-mapped physical pages where available, e.g. amd64.
     53  *
     54  * XXX ...or add an abstraction to uvm for this.  (uvm_emap?)
     55  */
     56 
     57 static kmutex_t linux_kmap_atomic_lock;
     58 static vaddr_t linux_kmap_atomic_vaddr;
     59 
     60 static kmutex_t linux_kmap_lock;
     61 static rb_tree_t linux_kmap_entries;
     62 
     63 struct linux_kmap_entry {
     64 	paddr_t		lke_paddr;
     65 	vaddr_t		lke_vaddr;
     66 	unsigned int	lke_refcnt;
     67 	rb_node_t	lke_node;
     68 };
     69 
     70 static int
     71 lke_compare_nodes(void *ctx __unused, const void *an, const void *bn)
     72 {
     73 	const struct linux_kmap_entry *const a = an;
     74 	const struct linux_kmap_entry *const b = bn;
     75 
     76 	if (a->lke_paddr < b->lke_paddr)
     77 		return -1;
     78 	else if (a->lke_paddr > b->lke_paddr)
     79 		return +1;
     80 	else
     81 		return 0;
     82 }
     83 
     84 static int
     85 lke_compare_key(void *ctx __unused, const void *node, const void *key)
     86 {
     87 	const struct linux_kmap_entry *const lke = node;
     88 	const paddr_t *const paddrp = key;
     89 
     90 	if (lke->lke_paddr < *paddrp)
     91 		return -1;
     92 	else if (lke->lke_paddr > *paddrp)
     93 		return +1;
     94 	else
     95 		return 0;
     96 }
     97 
     98 static const rb_tree_ops_t linux_kmap_entry_ops = {
     99 	.rbto_compare_nodes = &lke_compare_nodes,
    100 	.rbto_compare_key = &lke_compare_key,
    101 	.rbto_node_offset = offsetof(struct linux_kmap_entry, lke_node),
    102 	.rbto_context = NULL,
    103 };
    104 
    105 int
    106 linux_kmap_init(void)
    107 {
    108 
    109 	/* IPL_VM since interrupt handlers use kmap_atomic.  */
    110 	mutex_init(&linux_kmap_atomic_lock, MUTEX_DEFAULT, IPL_VM);
    111 
    112 	linux_kmap_atomic_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    113 	    (UVM_KMF_VAONLY | UVM_KMF_WAITVA));
    114 
    115 	KASSERT(linux_kmap_atomic_vaddr != 0);
    116 	KASSERT(!pmap_extract(pmap_kernel(), linux_kmap_atomic_vaddr, NULL));
    117 
    118 	mutex_init(&linux_kmap_lock, MUTEX_DEFAULT, IPL_NONE);
    119 	rb_tree_init(&linux_kmap_entries, &linux_kmap_entry_ops);
    120 
    121 	return 0;
    122 }
    123 
    124 void
    125 linux_kmap_fini(void)
    126 {
    127 
    128 	KASSERT(RB_TREE_MIN(&linux_kmap_entries) == NULL);
    129 #if 0				/* XXX no rb_tree_destroy */
    130 	rb_tree_destroy(&linux_kmap_entries);
    131 #endif
    132 	mutex_destroy(&linux_kmap_lock);
    133 
    134 	KASSERT(linux_kmap_atomic_vaddr != 0);
    135 	KASSERT(!pmap_extract(pmap_kernel(), linux_kmap_atomic_vaddr, NULL));
    136 
    137 	uvm_km_free(kernel_map, linux_kmap_atomic_vaddr, PAGE_SIZE,
    138 	    (UVM_KMF_VAONLY | UVM_KMF_WAITVA));
    139 
    140 	mutex_destroy(&linux_kmap_atomic_lock);
    141 }
    142 
    143 void *
    144 kmap_atomic(struct page *page)
    145 {
    146 	const paddr_t paddr = uvm_vm_page_to_phys(&page->p_vmp);
    147 
    148 	mutex_spin_enter(&linux_kmap_atomic_lock);
    149 
    150 	KASSERT(linux_kmap_atomic_vaddr != 0);
    151 	KASSERT(!pmap_extract(pmap_kernel(), linux_kmap_atomic_vaddr, NULL));
    152 
    153 	const vaddr_t vaddr = linux_kmap_atomic_vaddr;
    154 	const int prot = (VM_PROT_READ | VM_PROT_WRITE);
    155 	const int flags = 0;
    156 	pmap_kenter_pa(vaddr, paddr, prot, flags);
    157 	pmap_update(pmap_kernel());
    158 
    159 	return (void *)vaddr;
    160 }
    161 
    162 void
    163 kunmap_atomic(void *addr)
    164 {
    165 	const vaddr_t vaddr = (vaddr_t)addr;
    166 
    167 	KASSERT(mutex_owned(&linux_kmap_atomic_lock));
    168 	KASSERT(linux_kmap_atomic_vaddr == vaddr);
    169 	KASSERT(pmap_extract(pmap_kernel(), vaddr, NULL));
    170 
    171 	pmap_kremove(vaddr, PAGE_SIZE);
    172 	pmap_update(pmap_kernel());
    173 
    174 	mutex_spin_exit(&linux_kmap_atomic_lock);
    175 }
    176 
    177 void *
    178 kmap(struct page *page)
    179 {
    180 	const paddr_t paddr = VM_PAGE_TO_PHYS(&page->p_vmp);
    181 
    182 	ASSERT_SLEEPABLE();
    183 
    184 	const vaddr_t vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    185 	    (UVM_KMF_VAONLY | UVM_KMF_WAITVA));
    186 	KASSERT(vaddr != 0);
    187 
    188 	struct linux_kmap_entry *const lke = kmem_alloc(sizeof(*lke),
    189 	    KM_SLEEP);
    190 	lke->lke_paddr = paddr;
    191 	lke->lke_vaddr = vaddr;
    192 
    193 	mutex_enter(&linux_kmap_lock);
    194 	struct linux_kmap_entry *const collision __unused =
    195 	    rb_tree_insert_node(&linux_kmap_entries, lke);
    196 	KASSERT(collision == lke);
    197 	mutex_exit(&linux_kmap_lock);
    198 
    199 	KASSERT(!pmap_extract(pmap_kernel(), vaddr, NULL));
    200 	const int prot = (VM_PROT_READ | VM_PROT_WRITE);
    201 	const int flags = 0;
    202 	pmap_kenter_pa(vaddr, paddr, prot, flags);
    203 	pmap_update(pmap_kernel());
    204 
    205 	return (void *)vaddr;
    206 }
    207 
    208 void
    209 kunmap(struct page *page)
    210 {
    211 	const paddr_t paddr = VM_PAGE_TO_PHYS(&page->p_vmp);
    212 
    213 	ASSERT_SLEEPABLE();
    214 
    215 	mutex_enter(&linux_kmap_lock);
    216 	struct linux_kmap_entry *const lke =
    217 	    rb_tree_find_node(&linux_kmap_entries, &paddr);
    218 	KASSERT(lke != NULL);
    219 	rb_tree_remove_node(&linux_kmap_entries, lke);
    220 	mutex_exit(&linux_kmap_lock);
    221 
    222 	const vaddr_t vaddr = lke->lke_vaddr;
    223 	kmem_free(lke, sizeof(*lke));
    224 
    225 	KASSERT(pmap_extract(pmap_kernel(), vaddr, NULL));
    226 
    227 	pmap_kremove(vaddr, PAGE_SIZE);
    228 	pmap_update(pmap_kernel());
    229 
    230 	uvm_km_free(kernel_map, vaddr, PAGE_SIZE, UVM_KMF_VAONLY);
    231 }
    232