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