1 /* 2 * Copyright (c) 2017-2018 François Tigeot <ftigeot@wolfpond.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/_null.h> 28 #include <sys/queue.h> 29 #include <vm/vm_extern.h> 30 31 #include <linux/vmalloc.h> 32 #include <linux/slab.h> 33 #include <linux/mm.h> 34 35 struct vmap { 36 void *addr; 37 int npages; 38 SLIST_ENTRY(vmap) vm_vmaps; 39 }; 40 41 SLIST_HEAD(vmap_list_head, vmap) vmap_list = SLIST_HEAD_INITIALIZER(vmap_list); 42 43 /* vmap: map an array of pages into virtually contiguous space */ 44 void * 45 vmap(struct page **pages, unsigned int count, 46 unsigned long flags, pgprot_t prot) 47 { 48 struct vmap *vmp; 49 vm_offset_t off; 50 size_t size; 51 52 vmp = kmalloc(sizeof(struct vmap), M_DRM, M_WAITOK | M_ZERO); 53 54 size = count * PAGE_SIZE; 55 off = kmem_alloc_nofault(&kernel_map, size, 56 VM_SUBSYS_DRM_VMAP, PAGE_SIZE); 57 if (off == 0) 58 return (NULL); 59 60 for (int i = 0; i < count; i++) { 61 pmap_page_set_memattr((struct vm_page *)pages[i], prot); 62 } 63 64 vmp->addr = (void *)off; 65 vmp->npages = count; 66 pmap_qenter(off, (struct vm_page **)pages, count); 67 SLIST_INSERT_HEAD(&vmap_list, vmp, vm_vmaps); 68 69 return (void *)off; 70 } 71 72 void 73 vunmap(const void *addr) 74 { 75 struct vmap *vmp, *tmp_vmp; 76 size_t size; 77 78 SLIST_FOREACH_MUTABLE(vmp, &vmap_list, vm_vmaps, tmp_vmp) { 79 if (vmp->addr == addr) { 80 size = vmp->npages * PAGE_SIZE; 81 82 pmap_qremove((vm_offset_t)addr, vmp->npages); 83 kmem_free(&kernel_map, (vm_offset_t)addr, size); 84 goto found; 85 } 86 } 87 88 found: 89 SLIST_REMOVE(&vmap_list, vmp, vmap, vm_vmaps); 90 kfree(vmp); 91 } 92 93 int 94 is_vmalloc_addr(const void *x) 95 { 96 struct vmap *vmp, *tmp_vmp; 97 98 SLIST_FOREACH_MUTABLE(vmp, &vmap_list, vm_vmaps, tmp_vmp) { 99 if (vmp->addr == x) 100 return 1; 101 } 102 103 return false; 104 } 105 106 /* allocate zeroed virtually contiguous memory for userspace */ 107 void * 108 vmalloc_user(unsigned long size) 109 { 110 return kmalloc(size, M_DRM, M_WAITOK | M_ZERO); 111 } 112 113 void 114 vfree(const void *addr) 115 { 116 void *nc_addr; 117 118 memcpy(&nc_addr, &addr, sizeof(void *)); 119 kfree(nc_addr); 120 } 121