drm_vm.c revision 201223
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 201223 2009-12-29 21:51:28Z 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 34201223Srnolandint drm_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr, 35201223Srnoland int prot, vm_memattr_t *memattr) 36145132Sanholt{ 37182080Srnoland struct drm_device *dev = drm_get_device_from_kdev(kdev); 38183573Srnoland struct drm_file *file_priv = NULL; 39145132Sanholt drm_local_map_t *map; 40183573Srnoland enum drm_map_type type; 41152909Sanholt vm_paddr_t phys; 42183573Srnoland int error; 43145132Sanholt 44183573Srnoland /* d_mmap gets called twice, we can only reference file_priv during 45183573Srnoland * the first call. We need to assume that if error is EBADF the 46183573Srnoland * call was succesful and the client is authenticated. 47183573Srnoland */ 48183573Srnoland error = devfs_get_cdevpriv((void **)&file_priv); 49183573Srnoland if (error == ENOENT) { 50183573Srnoland DRM_ERROR("Could not find authenticator!\n"); 51145132Sanholt return EINVAL; 52145132Sanholt } 53145132Sanholt 54183573Srnoland if (file_priv && !file_priv->authenticated) 55182080Srnoland return EACCES; 56145132Sanholt 57190170Srnoland if (dev->dma && offset < ptoa(dev->dma->page_count)) { 58145132Sanholt drm_device_dma_t *dma = dev->dma; 59145132Sanholt 60152909Sanholt DRM_SPINLOCK(&dev->dma_lock); 61152909Sanholt 62145132Sanholt if (dma->pagelist != NULL) { 63145132Sanholt unsigned long page = offset >> PAGE_SHIFT; 64145132Sanholt unsigned long phys = dma->pagelist[page]; 65145132Sanholt 66182080Srnoland DRM_SPINUNLOCK(&dev->dma_lock); 67145132Sanholt *paddr = phys; 68145132Sanholt return 0; 69145132Sanholt } else { 70145132Sanholt DRM_SPINUNLOCK(&dev->dma_lock); 71145132Sanholt return -1; 72145132Sanholt } 73145132Sanholt } 74145132Sanholt 75145132Sanholt /* A sequential search of a linked list is 76145132Sanholt fine here because: 1) there will only be 77145132Sanholt about 5-10 entries in the list and, 2) a 78145132Sanholt DRI client only has to do this mapping 79145132Sanholt once, so it doesn't have to be optimized 80145132Sanholt for performance, even if the list was a 81145132Sanholt bit longer. */ 82145132Sanholt DRM_LOCK(); 83145132Sanholt TAILQ_FOREACH(map, &dev->maplist, link) { 84145132Sanholt if (offset >= map->offset && offset < map->offset + map->size) 85145132Sanholt break; 86145132Sanholt } 87145132Sanholt 88145132Sanholt if (map == NULL) { 89189914Srnoland DRM_DEBUG("Can't find map, requested offset = %016lx\n", 90189916Srnoland (unsigned long)offset); 91189914Srnoland TAILQ_FOREACH(map, &dev->maplist, link) { 92189914Srnoland DRM_DEBUG("map offset = %016lx, handle = %016lx\n", 93189916Srnoland (unsigned long)map->offset, 94189916Srnoland (unsigned long)map->handle); 95189914Srnoland } 96145132Sanholt DRM_UNLOCK(); 97145132Sanholt return -1; 98145132Sanholt } 99152909Sanholt if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) { 100145132Sanholt DRM_UNLOCK(); 101145132Sanholt DRM_DEBUG("restricted map\n"); 102145132Sanholt return -1; 103145132Sanholt } 104145132Sanholt type = map->type; 105145132Sanholt DRM_UNLOCK(); 106145132Sanholt 107145132Sanholt switch (type) { 108145132Sanholt case _DRM_FRAME_BUFFER: 109145132Sanholt case _DRM_REGISTERS: 110145132Sanholt case _DRM_AGP: 111152909Sanholt phys = offset; 112152909Sanholt break; 113152909Sanholt case _DRM_CONSISTENT: 114152909Sanholt phys = vtophys((char *)map->handle + (offset - map->offset)); 115152909Sanholt break; 116145132Sanholt case _DRM_SCATTER_GATHER: 117145132Sanholt case _DRM_SHM: 118152909Sanholt phys = vtophys(offset); 119152909Sanholt break; 120145132Sanholt default: 121152909Sanholt DRM_ERROR("bad map type %d\n", type); 122145132Sanholt return -1; /* This should never happen. */ 123145132Sanholt } 124145132Sanholt 125152909Sanholt *paddr = phys; 126152909Sanholt return 0; 127145132Sanholt} 128145132Sanholt 129