drm_vm.c revision 152909
1268896Sbapt/*- 2268896Sbapt * Copyright 2003 Eric Anholt 3268896Sbapt * All Rights Reserved. 4268896Sbapt * 5268896Sbapt * Permission is hereby granted, free of charge, to any person obtaining a 6268896Sbapt * copy of this software and associated documentation files (the "Software"), 7268896Sbapt * to deal in the Software without restriction, including without limitation 8268896Sbapt * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9268896Sbapt * and/or sell copies of the Software, and to permit persons to whom the 10268896Sbapt * Software is furnished to do so, subject to the following conditions: 11268896Sbapt * 12268896Sbapt * The above copyright notice and this permission notice (including the next 13268896Sbapt * paragraph) shall be included in all copies or substantial portions of the 14268896Sbapt * Software. 15268896Sbapt * 16268896Sbapt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17268896Sbapt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18268896Sbapt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19268896Sbapt * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20268896Sbapt * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21268896Sbapt * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22268896Sbapt */ 23268896Sbapt 24268896Sbapt#include <sys/cdefs.h> 25268896Sbapt__FBSDID("$FreeBSD: head/sys/dev/drm/drm_vm.c 152909 2005-11-28 23:13:57Z anholt $"); 26268896Sbapt 27268896Sbapt#include "dev/drm/drmP.h" 28268896Sbapt#include "dev/drm/drm.h" 29268896Sbapt 30268896Sbapt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102 31268896Sbaptint drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr, 32268896Sbapt int prot) 33268896Sbapt#elif defined(__FreeBSD__) 34268896Sbaptint drm_mmap(dev_t kdev, vm_offset_t offset, int prot) 35268896Sbapt#elif defined(__NetBSD__) || defined(__OpenBSD__) 36268896Sbaptpaddr_t drm_mmap(dev_t kdev, off_t offset, int prot) 37268896Sbapt#endif 38268896Sbapt{ 39268896Sbapt DRM_DEVICE; 40268896Sbapt drm_local_map_t *map; 41268896Sbapt drm_file_t *priv; 42268896Sbapt drm_map_type_t type; 43268896Sbapt#ifdef __FreeBSD__ 44268896Sbapt vm_paddr_t phys; 45268896Sbapt#else 46268896Sbapt paddr_t phys; 47268896Sbapt#endif 48268896Sbapt 49268896Sbapt DRM_LOCK(); 50268896Sbapt priv = drm_find_file_by_proc(dev, DRM_CURPROC); 51268896Sbapt DRM_UNLOCK(); 52268896Sbapt if (priv == NULL) { 53268896Sbapt DRM_ERROR("can't find authenticator\n"); 54268896Sbapt return EINVAL; 55268896Sbapt } 56268896Sbapt 57268896Sbapt if (!priv->authenticated) 58268896Sbapt return DRM_ERR(EACCES); 59268896Sbapt 60268896Sbapt if (dev->dma && offset >= 0 && offset < ptoa(dev->dma->page_count)) { 61268896Sbapt drm_device_dma_t *dma = dev->dma; 62268896Sbapt 63268896Sbapt DRM_SPINLOCK(&dev->dma_lock); 64268896Sbapt 65268896Sbapt if (dma->pagelist != NULL) { 66268896Sbapt unsigned long page = offset >> PAGE_SHIFT; 67268896Sbapt unsigned long phys = dma->pagelist[page]; 68268896Sbapt 69268896Sbapt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102 70268896Sbapt *paddr = phys; 71268896Sbapt DRM_SPINUNLOCK(&dev->dma_lock); 72268896Sbapt return 0; 73268896Sbapt#else 74268896Sbapt return atop(phys); 75268896Sbapt#endif 76268896Sbapt } else { 77268896Sbapt DRM_SPINUNLOCK(&dev->dma_lock); 78268896Sbapt return -1; 79268896Sbapt } 80268896Sbapt DRM_SPINUNLOCK(&dev->dma_lock); 81268896Sbapt } 82268896Sbapt 83268896Sbapt /* A sequential search of a linked list is 84268896Sbapt fine here because: 1) there will only be 85268896Sbapt about 5-10 entries in the list and, 2) a 86268896Sbapt DRI client only has to do this mapping 87268896Sbapt once, so it doesn't have to be optimized 88268896Sbapt for performance, even if the list was a 89268896Sbapt bit longer. */ 90268896Sbapt DRM_LOCK(); 91268896Sbapt TAILQ_FOREACH(map, &dev->maplist, link) { 92268896Sbapt if (offset >= map->offset && offset < map->offset + map->size) 93268896Sbapt break; 94268896Sbapt } 95268896Sbapt 96268896Sbapt if (map == NULL) { 97268896Sbapt DRM_UNLOCK(); 98268896Sbapt DRM_DEBUG("can't find map\n"); 99268896Sbapt return -1; 100268896Sbapt } 101268896Sbapt if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) { 102268896Sbapt DRM_UNLOCK(); 103268896Sbapt DRM_DEBUG("restricted map\n"); 104268896Sbapt return -1; 105268896Sbapt } 106268896Sbapt type = map->type; 107268896Sbapt DRM_UNLOCK(); 108268896Sbapt 109268896Sbapt switch (type) { 110268896Sbapt case _DRM_FRAME_BUFFER: 111268896Sbapt case _DRM_REGISTERS: 112268896Sbapt case _DRM_AGP: 113268896Sbapt 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