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: releng/10.2/sys/dev/drm/drm_vm.c 207066 2010-04-22 18:21:25Z 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 57207066Srnoland DRM_DEBUG("called with offset %016jx\n", offset); 58190170Srnoland if (dev->dma && offset < ptoa(dev->dma->page_count)) { 59145132Sanholt drm_device_dma_t *dma = dev->dma; 60145132Sanholt 61152909Sanholt DRM_SPINLOCK(&dev->dma_lock); 62152909Sanholt 63145132Sanholt if (dma->pagelist != NULL) { 64145132Sanholt unsigned long page = offset >> PAGE_SHIFT; 65145132Sanholt unsigned long phys = dma->pagelist[page]; 66145132Sanholt 67182080Srnoland DRM_SPINUNLOCK(&dev->dma_lock); 68145132Sanholt *paddr = phys; 69145132Sanholt return 0; 70145132Sanholt } else { 71145132Sanholt DRM_SPINUNLOCK(&dev->dma_lock); 72145132Sanholt return -1; 73145132Sanholt } 74145132Sanholt } 75145132Sanholt 76207066Srnoland /* A sequential search of a linked list is 77207066Srnoland fine here because: 1) there will only be 78207066Srnoland about 5-10 entries in the list and, 2) a 79207066Srnoland DRI client only has to do this mapping 80207066Srnoland once, so it doesn't have to be optimized 81207066Srnoland for performance, even if the list was a 82207066Srnoland bit longer. 83207066Srnoland */ 84145132Sanholt DRM_LOCK(); 85145132Sanholt TAILQ_FOREACH(map, &dev->maplist, link) { 86207066Srnoland if (offset >> DRM_MAP_HANDLE_SHIFT == 87207066Srnoland (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT) 88145132Sanholt break; 89145132Sanholt } 90145132Sanholt 91145132Sanholt if (map == NULL) { 92207066Srnoland DRM_DEBUG("Can't find map, request offset = %016jx\n", offset); 93189914Srnoland TAILQ_FOREACH(map, &dev->maplist, link) { 94189914Srnoland DRM_DEBUG("map offset = %016lx, handle = %016lx\n", 95207066Srnoland map->offset, (unsigned long)map->handle); 96189914Srnoland } 97145132Sanholt DRM_UNLOCK(); 98145132Sanholt return -1; 99145132Sanholt } 100207066Srnoland if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) { 101145132Sanholt DRM_UNLOCK(); 102145132Sanholt DRM_DEBUG("restricted map\n"); 103145132Sanholt return -1; 104145132Sanholt } 105145132Sanholt type = map->type; 106145132Sanholt DRM_UNLOCK(); 107145132Sanholt 108207066Srnoland offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1); 109207066Srnoland 110145132Sanholt switch (type) { 111145132Sanholt case _DRM_FRAME_BUFFER: 112207066Srnoland case _DRM_AGP: 113207066Srnoland *memattr = VM_MEMATTR_WRITE_COMBINING; 114207066Srnoland /* FALLTHROUGH */ 115145132Sanholt case _DRM_REGISTERS: 116207066Srnoland phys = map->offset + offset; 117152909Sanholt break; 118207066Srnoland case _DRM_SCATTER_GATHER: 119207066Srnoland *memattr = VM_MEMATTR_WRITE_COMBINING; 120207066Srnoland /* FALLTHROUGH */ 121152909Sanholt case _DRM_CONSISTENT: 122145132Sanholt case _DRM_SHM: 123207066Srnoland phys = vtophys((char *)map->virtual + offset); 124152909Sanholt break; 125145132Sanholt default: 126152909Sanholt DRM_ERROR("bad map type %d\n", type); 127145132Sanholt return -1; /* This should never happen. */ 128145132Sanholt } 129145132Sanholt 130152909Sanholt *paddr = phys; 131152909Sanholt return 0; 132145132Sanholt} 133145132Sanholt 134