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