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 vmp->addr = (void *)off; 61 vmp->npages = count; 62 pmap_qenter(off, (struct vm_page **)pages, count); 63 SLIST_INSERT_HEAD(&vmap_list, vmp, vm_vmaps); 64 65 return (void *)off; 66 } 67 68 void 69 vunmap(const void *addr) 70 { 71 struct vmap *vmp, *tmp_vmp; 72 size_t size; 73 74 SLIST_FOREACH_MUTABLE(vmp, &vmap_list, vm_vmaps, tmp_vmp) { 75 if (vmp->addr == addr) { 76 size = vmp->npages * PAGE_SIZE; 77 78 pmap_qremove((vm_offset_t)addr, vmp->npages); 79 kmem_free(&kernel_map, (vm_offset_t)addr, size); 80 goto found; 81 } 82 } 83 84 found: 85 SLIST_REMOVE(&vmap_list, vmp, vmap, vm_vmaps); 86 kfree(vmp); 87 } 88 89 int 90 is_vmalloc_addr(const void *x) 91 { 92 struct vmap *vmp, *tmp_vmp; 93 94 SLIST_FOREACH_MUTABLE(vmp, &vmap_list, vm_vmaps, tmp_vmp) { 95 if (vmp->addr == x) 96 return 1; 97 } 98 99 return false; 100 } 101 102 /* allocate zeroed virtually contiguous memory for userspace */ 103 void * 104 vmalloc_user(unsigned long size) 105 { 106 return kmalloc(size, M_DRM, M_WAITOK | M_ZERO); 107 } 108 109 void 110 vfree(const void *addr) 111 { 112 void *nc_addr; 113 114 memcpy(&nc_addr, &addr, sizeof(void *)); 115 kfree(nc_addr); 116 } 117