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 24/** @file drm_vm.c 25 * Support code for mmaping of DRM maps. 26 */ 27 28#include "drmP.h" 29#include "drm.h" 30 31#if defined(__FreeBSD__) 32int drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr, 33 int prot) 34#elif defined(__NetBSD__) 35paddr_t drm_mmap(dev_t kdev, off_t offset, int prot) 36#endif 37{ 38 struct drm_device *dev = drm_get_device_from_kdev(kdev); 39 struct drm_file *file_priv = NULL; 40 drm_local_map_t *map; 41 enum drm_map_type type; 42#if defined(__FreeBSD__) 43 vm_paddr_t phys; 44 int error; 45#elif defined(__NetBSD__) 46 paddr_t phys; 47 unsigned long map_offs; 48 int flags = 0; 49#endif 50 51#if defined(__FreeBSD__) 52 /* d_mmap gets called twice, we can only reference file_priv during 53 * the first call. We need to assume that if error is EBADF the 54 * call was succesful and the client is authenticated. 55 */ 56 error = devfs_get_cdevpriv((void **)&file_priv); 57 if (error == ENOENT) { 58 DRM_ERROR("Could not find authenticator!\n"); 59 return EINVAL; 60 } 61#elif defined(__NetBSD__) 62 DRM_LOCK(); 63 file_priv = drm_find_file_by_proc(dev, DRM_CURPROC); 64 DRM_UNLOCK(); 65 if (file_priv == NULL) { 66 DRM_ERROR("Could not find authenticator!\n"); 67 return -1; 68 } 69#endif 70 71 if (file_priv && !file_priv->authenticated) 72#if defined(__NetBSD__) 73 return -1; 74#else 75 return EACCES; 76#endif 77 78 if (dev->dma && offset >= 0 && offset < ptoa(dev->dma->page_count)) { 79 drm_device_dma_t *dma = dev->dma; 80 81 DRM_SPINLOCK(&dev->dma_lock); 82 83 if (dma->pagelist != NULL) { 84 unsigned long page = offset >> PAGE_SHIFT; 85 unsigned long physaddr = dma->pagelist[page]; 86 87 DRM_SPINUNLOCK(&dev->dma_lock); 88#if defined(__FreeBSD__) 89 *paddr = physaddr; 90 return 0; 91#elif defined(__NetBSD__) 92 return atop(physaddr); 93#endif 94 } else { 95 DRM_SPINUNLOCK(&dev->dma_lock); 96 return -1; 97 } 98 } 99 100 /* A sequential search of a linked list is 101 fine here because: 1) there will only be 102 about 5-10 entries in the list and, 2) a 103 DRI client only has to do this mapping 104 once, so it doesn't have to be optimized 105 for performance, even if the list was a 106 bit longer. */ 107 DRM_LOCK(); 108 TAILQ_FOREACH(map, &dev->maplist, link) { 109 if (offset >= map->offset && offset < map->offset + map->size) 110 break; 111 } 112 113 if (map == NULL) { 114 DRM_DEBUG("Can't find map, requested offset = %" PRIx64 "\n", 115 offset); 116 TAILQ_FOREACH(map, &dev->maplist, link) { 117 DRM_DEBUG("map offset = %016lx, handle = %016lx\n", 118 map->offset, (unsigned long)map->handle); 119 } 120 DRM_UNLOCK(); 121 return -1; 122 } 123 if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) { 124 DRM_UNLOCK(); 125 DRM_DEBUG("restricted map\n"); 126 return -1; 127 } 128 type = map->type; 129#if defined(__NetBSD__) 130 map_offs = map->offset; 131#endif 132 DRM_UNLOCK(); 133 134 switch (type) { 135 case _DRM_FRAME_BUFFER: 136 case _DRM_REGISTERS: 137 case _DRM_AGP: 138#if defined(__NetBSD__) 139 flags = BUS_SPACE_MAP_LINEAR; 140 if (type == _DRM_FRAME_BUFFER || type == _DRM_AGP) 141 flags |= BUS_SPACE_MAP_PREFETCHABLE; 142 phys = bus_space_mmap(dev->pa.pa_memt, map->offset, 143 offset - map->offset, prot, flags); 144 if (phys == -1) { 145 DRM_ERROR("bus_space_mmap for %" PRIx64 " failed\n", offset); 146 return -1; 147 } 148 return phys; 149#else 150 phys = offset; 151#endif 152 break; 153 case _DRM_CONSISTENT: 154 phys = vtophys((vaddr_t)((char *)map->handle + (offset - map->offset))); 155 break; 156 case _DRM_SCATTER_GATHER: 157 case _DRM_SHM: 158 phys = vtophys(offset); 159 break; 160 default: 161 DRM_ERROR("bad map type %d\n", type); 162 return -1; /* This should never happen. */ 163 } 164 165#if defined(__FreeBSD__) 166 *paddr = phys; 167 return 0; 168#elif defined(__NetBSD__) 169 return atop(phys); 170#endif 171} 172 173