drm_vm.c revision 183573
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 183573 2008-10-03 16:59:11Z 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
34145132Sanholtint drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr,
35145132Sanholt    int prot)
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
57145132Sanholt	if (dev->dma && offset >= 0 && 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) {
89145132Sanholt		DRM_UNLOCK();
90145132Sanholt		DRM_DEBUG("can't find map\n");
91145132Sanholt		return -1;
92145132Sanholt	}
93152909Sanholt	if (((map->flags&_DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
94145132Sanholt		DRM_UNLOCK();
95145132Sanholt		DRM_DEBUG("restricted map\n");
96145132Sanholt		return -1;
97145132Sanholt	}
98145132Sanholt	type = map->type;
99145132Sanholt	DRM_UNLOCK();
100145132Sanholt
101145132Sanholt	switch (type) {
102145132Sanholt	case _DRM_FRAME_BUFFER:
103145132Sanholt	case _DRM_REGISTERS:
104145132Sanholt	case _DRM_AGP:
105152909Sanholt		phys = offset;
106152909Sanholt		break;
107152909Sanholt	case _DRM_CONSISTENT:
108152909Sanholt		phys = vtophys((char *)map->handle + (offset - map->offset));
109152909Sanholt		break;
110145132Sanholt	case _DRM_SCATTER_GATHER:
111145132Sanholt	case _DRM_SHM:
112152909Sanholt		phys = vtophys(offset);
113152909Sanholt		break;
114145132Sanholt	default:
115152909Sanholt		DRM_ERROR("bad map type %d\n", type);
116145132Sanholt		return -1;	/* This should never happen. */
117145132Sanholt	}
118145132Sanholt
119152909Sanholt	*paddr = phys;
120152909Sanholt	return 0;
121145132Sanholt}
122145132Sanholt
123