drm_vm.c revision 182080
1145132Sanholt/*- 2145132Sanholt * Copyright 2003 Eric Anholt 3145132Sanholt * All Rights Reserved. 4145132Sanholt * 5145132Sanholt * Permission is hereby granted, free of charge, to any person obtaining a 6145132Sanholt * copy of this software and associated documentation files (the "Software"), 7145132Sanholt * to deal in the Software without restriction, including without limitation 8145132Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9145132Sanholt * and/or sell copies of the Software, and to permit persons to whom the 10145132Sanholt * Software is furnished to do so, subject to the following conditions: 11145132Sanholt * 12145132Sanholt * The above copyright notice and this permission notice (including the next 13145132Sanholt * paragraph) shall be included in all copies or substantial portions of the 14145132Sanholt * Software. 15145132Sanholt * 16145132Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17145132Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18145132Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19145132Sanholt * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20145132Sanholt * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21145132Sanholt * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22145132Sanholt */ 23145132Sanholt 24152909Sanholt#include <sys/cdefs.h> 25152909Sanholt__FBSDID("$FreeBSD: head/sys/dev/drm/drm_vm.c 182080 2008-08-23 20:59:12Z rnoland $"); 26152909Sanholt 27182080Srnoland/** @file drm_vm.c 28182080Srnoland * Support code for mmaping of DRM maps. 29182080Srnoland */ 30182080Srnoland 31145132Sanholt#include "dev/drm/drmP.h" 32145132Sanholt#include "dev/drm/drm.h" 33145132Sanholt 34145132Sanholt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102 35145132Sanholtint drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr, 36145132Sanholt int prot) 37145132Sanholt#elif defined(__FreeBSD__) 38145132Sanholtint drm_mmap(dev_t kdev, vm_offset_t offset, int prot) 39145132Sanholt#elif defined(__NetBSD__) || defined(__OpenBSD__) 40145132Sanholtpaddr_t drm_mmap(dev_t kdev, off_t offset, int prot) 41145132Sanholt#endif 42145132Sanholt{ 43182080Srnoland struct drm_device *dev = drm_get_device_from_kdev(kdev); 44145132Sanholt drm_local_map_t *map; 45145132Sanholt drm_file_t *priv; 46145132Sanholt drm_map_type_t type; 47152909Sanholt#ifdef __FreeBSD__ 48152909Sanholt vm_paddr_t phys; 49152909Sanholt#else 50152909Sanholt paddr_t phys; 51152909Sanholt#endif 52145132Sanholt 53145132Sanholt DRM_LOCK(); 54145132Sanholt priv = drm_find_file_by_proc(dev, DRM_CURPROC); 55145132Sanholt DRM_UNLOCK(); 56145132Sanholt if (priv == NULL) { 57145132Sanholt DRM_ERROR("can't find authenticator\n"); 58145132Sanholt return EINVAL; 59145132Sanholt } 60145132Sanholt 61145132Sanholt if (!priv->authenticated) 62182080Srnoland return EACCES; 63145132Sanholt 64145132Sanholt if (dev->dma && offset >= 0 && offset < ptoa(dev->dma->page_count)) { 65145132Sanholt drm_device_dma_t *dma = dev->dma; 66145132Sanholt 67152909Sanholt DRM_SPINLOCK(&dev->dma_lock); 68152909Sanholt 69145132Sanholt if (dma->pagelist != NULL) { 70145132Sanholt unsigned long page = offset >> PAGE_SHIFT; 71145132Sanholt unsigned long phys = dma->pagelist[page]; 72145132Sanholt 73182080Srnoland DRM_SPINUNLOCK(&dev->dma_lock); 74145132Sanholt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102 75145132Sanholt *paddr = phys; 76145132Sanholt return 0; 77145132Sanholt#else 78145132Sanholt return atop(phys); 79145132Sanholt#endif 80145132Sanholt } else { 81145132Sanholt DRM_SPINUNLOCK(&dev->dma_lock); 82145132Sanholt return -1; 83145132Sanholt } 84145132Sanholt } 85145132Sanholt 86145132Sanholt /* A sequential search of a linked list is 87145132Sanholt fine here because: 1) there will only be 88145132Sanholt about 5-10 entries in the list and, 2) a 89145132Sanholt DRI client only has to do this mapping 90145132Sanholt once, so it doesn't have to be optimized 91145132Sanholt for performance, even if the list was a 92145132Sanholt bit longer. */ 93145132Sanholt DRM_LOCK(); 94145132Sanholt TAILQ_FOREACH(map, &dev->maplist, link) { 95145132Sanholt if (offset >= map->offset && offset < map->offset + map->size) 96145132Sanholt break; 97145132Sanholt } 98145132Sanholt 99145132Sanholt if (map == NULL) { 100145132Sanholt DRM_UNLOCK(); 101145132Sanholt DRM_DEBUG("can't find map\n"); 102145132Sanholt return -1; 103145132Sanholt } 104152909Sanholt if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) { 105145132Sanholt DRM_UNLOCK(); 106145132Sanholt DRM_DEBUG("restricted map\n"); 107145132Sanholt return -1; 108145132Sanholt } 109145132Sanholt type = map->type; 110145132Sanholt DRM_UNLOCK(); 111145132Sanholt 112145132Sanholt switch (type) { 113145132Sanholt case _DRM_FRAME_BUFFER: 114145132Sanholt case _DRM_REGISTERS: 115145132Sanholt case _DRM_AGP: 116152909Sanholt phys = offset; 117152909Sanholt break; 118152909Sanholt case _DRM_CONSISTENT: 119152909Sanholt phys = vtophys((char *)map->handle + (offset - map->offset)); 120152909Sanholt break; 121145132Sanholt case _DRM_SCATTER_GATHER: 122145132Sanholt case _DRM_SHM: 123152909Sanholt phys = vtophys(offset); 124152909Sanholt break; 125145132Sanholt default: 126152909Sanholt DRM_ERROR("bad map type %d\n", type); 127145132Sanholt return -1; /* This should never happen. */ 128145132Sanholt } 129145132Sanholt 130152909Sanholt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102 131152909Sanholt *paddr = phys; 132152909Sanholt return 0; 133152909Sanholt#else 134152909Sanholt return atop(phys); 135152909Sanholt#endif 136145132Sanholt} 137145132Sanholt 138