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 * $DragonFly: src/sys/dev/drm/drm_vm.c,v 1.1 2008/04/05 18:12:29 hasso Exp $ 24 */ 25 26 /** @file drm_vm.c 27 * Support code for mmaping of DRM maps. 28 */ 29 30 #include "drmP.h" 31 #include "drm.h" 32 33 #if defined(__FreeBSD__) && __FreeBSD_version >= 500102 34 int drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr, 35 int prot) 36 #elif defined(__FreeBSD__) 37 int drm_mmap(dev_t kdev, vm_offset_t offset, int prot) 38 #elif defined(__NetBSD__) || defined(__OpenBSD__) 39 paddr_t drm_mmap(dev_t kdev, off_t offset, int prot) 40 #endif 41 #ifdef __DragonFly__ 42 int drm_mmap(struct dev_mmap_args *ap) 43 { 44 struct cdev *kdev = ap->a_head.a_dev; 45 vm_offset_t offset = ap->a_offset; 46 #else 47 { 48 #endif 49 drm_device_t *dev = drm_get_device_from_kdev(kdev); 50 drm_local_map_t *map; 51 drm_file_t *priv; 52 drm_map_type_t type; 53 #if defined(__FreeBSD__) || defined(__DragonFly__) 54 vm_paddr_t phys; 55 #else 56 paddr_t phys; 57 #endif 58 59 DRM_LOCK(); 60 priv = drm_find_file_by_proc(dev, DRM_CURPROC); 61 DRM_UNLOCK(); 62 if (priv == NULL) { 63 DRM_ERROR("can't find authenticator\n"); 64 return EINVAL; 65 } 66 67 if (!priv->authenticated) 68 return EACCES; 69 70 if (dev->dma && offset >= 0 && offset < ptoa(dev->dma->page_count)) { 71 drm_device_dma_t *dma = dev->dma; 72 73 DRM_SPINLOCK(&dev->dma_lock); 74 75 if (dma->pagelist != NULL) { 76 unsigned long page = offset >> PAGE_SHIFT; 77 unsigned long phys = dma->pagelist[page]; 78 79 #if defined(__FreeBSD__) && __FreeBSD_version >= 500102 80 *paddr = phys; 81 DRM_SPINUNLOCK(&dev->dma_lock); 82 return 0; 83 #elif defined(__DragonFly__) 84 ap->a_result = atop(phys); 85 DRM_SPINUNLOCK(&dev->dma_lock); 86 return (0); 87 #else 88 return atop(phys); 89 #endif 90 } else { 91 DRM_SPINUNLOCK(&dev->dma_lock); 92 return -1; 93 } 94 DRM_SPINUNLOCK(&dev->dma_lock); 95 } 96 97 /* A sequential search of a linked list is 98 fine here because: 1) there will only be 99 about 5-10 entries in the list and, 2) a 100 DRI client only has to do this mapping 101 once, so it doesn't have to be optimized 102 for performance, even if the list was a 103 bit longer. */ 104 DRM_LOCK(); 105 TAILQ_FOREACH(map, &dev->maplist, link) { 106 if (offset >= map->offset && offset < map->offset + map->size) 107 break; 108 } 109 110 if (map == NULL) { 111 DRM_UNLOCK(); 112 DRM_DEBUG("can't find map\n"); 113 return -1; 114 } 115 if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) { 116 DRM_UNLOCK(); 117 DRM_DEBUG("restricted map\n"); 118 return -1; 119 } 120 type = map->type; 121 DRM_UNLOCK(); 122 123 switch (type) { 124 case _DRM_FRAME_BUFFER: 125 case _DRM_REGISTERS: 126 case _DRM_AGP: 127 phys = offset; 128 break; 129 case _DRM_CONSISTENT: 130 phys = vtophys((char *)map->handle + (offset - map->offset)); 131 break; 132 case _DRM_SCATTER_GATHER: 133 case _DRM_SHM: 134 phys = vtophys(offset); 135 break; 136 default: 137 DRM_ERROR("bad map type %d\n", type); 138 return -1; /* This should never happen. */ 139 } 140 141 #if defined(__FreeBSD__) && __FreeBSD_version >= 500102 142 *paddr = phys; 143 return 0; 144 #elif defined(__DragonFly__) 145 ap->a_result = atop(phys); 146 return (0); 147 #else 148 return atop(phys); 149 #endif 150 } 151 152