1 /*- 2 * Copyright 2003 Eric Anholt 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * $FreeBSD: head/sys/dev/drm2/drm_vm.c 235783 2012-05-22 11:07:44Z kib $" 24 */ 25 26 /** @file drm_vm.c 27 * Support code for mmaping of DRM maps. 28 */ 29 30 #include <drm/drmP.h> 31 #include <linux/export.h> 32 #include <linux/seq_file.h> 33 #if defined(__ia64__) 34 #include <linux/efi.h> 35 #include <linux/slab.h> 36 #endif 37 #include <asm/pgtable.h> 38 #include "drm_internal.h" 39 #include "drm_legacy.h" 40 41 #include <sys/mutex2.h> 42 43 int drm_mmap(struct dev_mmap_args *ap) 44 { 45 struct file *filp = ap->a_fp; 46 struct drm_file *priv = filp->private_data; 47 struct cdev *kdev = ap->a_head.a_dev; 48 vm_offset_t offset = ap->a_offset; 49 struct drm_device *dev = drm_get_device_from_kdev(kdev); 50 struct drm_local_map *map = NULL; 51 struct drm_hash_item *hash; 52 53 enum drm_map_type type; 54 vm_paddr_t phys; 55 56 if (!priv->authenticated) 57 return -EACCES; 58 59 DRM_DEBUG("called with offset %016jx\n", (uintmax_t)offset); 60 if (dev->dma && offset < ptoa(dev->dma->page_count)) { 61 struct drm_device_dma *dma = dev->dma; 62 63 if (dma->pagelist != NULL) { 64 unsigned long page = offset >> PAGE_SHIFT; 65 unsigned long phys = dma->pagelist[page]; 66 67 // XXX *paddr = phys; 68 ap->a_result = phys; 69 return 0; 70 } else { 71 return -1; 72 } 73 } 74 75 /* A sequential search of a linked list is 76 fine here because: 1) there will only be 77 about 5-10 entries in the list and, 2) a 78 DRI client only has to do this mapping 79 once, so it doesn't have to be optimized 80 for performance, even if the list was a 81 bit longer. 82 */ 83 DRM_LOCK(dev); 84 85 if (drm_ht_find_item(&dev->map_hash, offset, &hash)) { 86 DRM_ERROR("Could not find map\n"); 87 return -EINVAL; 88 } 89 90 map = drm_hash_entry(hash, struct drm_map_list, hash)->map; 91 if (map == NULL) { 92 DRM_DEBUG("Can't find map, request offset = %016jx\n", 93 (uintmax_t)offset); 94 DRM_UNLOCK(dev); 95 return -1; 96 } 97 if (((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) { 98 DRM_UNLOCK(dev); 99 DRM_DEBUG("restricted map\n"); 100 return -1; 101 } 102 103 type = map->type; 104 DRM_UNLOCK(dev); 105 106 switch (type) { 107 case _DRM_FRAME_BUFFER: 108 case _DRM_AGP: 109 #if 0 /* XXX */ 110 *memattr = VM_MEMATTR_WRITE_COMBINING; 111 #endif 112 /* FALLTHROUGH */ 113 case _DRM_REGISTERS: 114 phys = map->offset + offset; 115 break; 116 case _DRM_SCATTER_GATHER: 117 #if 0 /* XXX */ 118 *memattr = VM_MEMATTR_WRITE_COMBINING; 119 #endif 120 /* FALLTHROUGH */ 121 case _DRM_CONSISTENT: 122 case _DRM_SHM: 123 phys = vtophys((char *)map->handle + offset); 124 break; 125 default: 126 DRM_ERROR("bad map type %d\n", type); 127 return -1; /* This should never happen. */ 128 } 129 130 ap->a_result = atop(phys); 131 return 0; 132 } 133 134 /* XXX The following is just temporary hack to replace the 135 * vm_phys_fictitious functions available on FreeBSD 136 */ 137 #define VM_PHYS_FICTITIOUS_NSEGS 8 138 static struct vm_phys_fictitious_seg { 139 vm_paddr_t start; 140 vm_paddr_t end; 141 vm_page_t first_page; 142 } vm_phys_fictitious_segs[VM_PHYS_FICTITIOUS_NSEGS]; 143 static struct mtx vm_phys_fictitious_reg_mtx = MTX_INITIALIZER("vmphy"); 144 145 vm_page_t 146 vm_phys_fictitious_to_vm_page(vm_paddr_t pa) 147 { 148 struct vm_phys_fictitious_seg *seg; 149 vm_page_t m; 150 int segind; 151 152 m = NULL; 153 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) { 154 seg = &vm_phys_fictitious_segs[segind]; 155 if (pa >= seg->start && pa < seg->end) { 156 m = &seg->first_page[atop(pa - seg->start)]; 157 KASSERT((m->flags & PG_FICTITIOUS) != 0, 158 ("%p not fictitious", m)); 159 break; 160 } 161 } 162 return (m); 163 } 164 165 int 166 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end, 167 vm_memattr_t memattr) 168 { 169 struct vm_phys_fictitious_seg *seg; 170 vm_page_t fp; 171 long i, page_count; 172 int segind; 173 174 page_count = (end - start) / PAGE_SIZE; 175 176 fp = kmalloc(page_count * sizeof(struct vm_page), M_DRM, 177 M_WAITOK | M_ZERO); 178 179 for (i = 0; i < page_count; i++) { 180 vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr); 181 atomic_clear_int(&fp[i].busy_count, PBUSY_LOCKED); 182 } 183 mtx_lock(&vm_phys_fictitious_reg_mtx); 184 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) { 185 seg = &vm_phys_fictitious_segs[segind]; 186 if (seg->start == 0 && seg->end == 0) { 187 seg->start = start; 188 seg->end = end; 189 seg->first_page = fp; 190 mtx_unlock(&vm_phys_fictitious_reg_mtx); 191 return (0); 192 } 193 } 194 mtx_unlock(&vm_phys_fictitious_reg_mtx); 195 kfree(fp); 196 return (EBUSY); 197 } 198 199 void 200 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end) 201 { 202 struct vm_phys_fictitious_seg *seg; 203 vm_page_t fp; 204 int segind; 205 206 mtx_lock(&vm_phys_fictitious_reg_mtx); 207 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) { 208 seg = &vm_phys_fictitious_segs[segind]; 209 if (seg->start == start && seg->end == end) { 210 seg->start = seg->end = 0; 211 fp = seg->first_page; 212 seg->first_page = NULL; 213 mtx_unlock(&vm_phys_fictitious_reg_mtx); 214 kfree(fp); 215 return; 216 } 217 } 218 mtx_unlock(&vm_phys_fictitious_reg_mtx); 219 KASSERT(0, ("Unregistering not registered fictitious range")); 220 } 221