drm_vm.c revision 145132
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 * $FreeBSD: head/sys/dev/drm/drm_vm.c 145132 2005-04-16 03:44:47Z anholt $
24145132Sanholt */
25145132Sanholt
26145132Sanholt#include "dev/drm/drmP.h"
27145132Sanholt#include "dev/drm/drm.h"
28145132Sanholt
29145132Sanholt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
30145132Sanholtint drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr,
31145132Sanholt    int prot)
32145132Sanholt#elif defined(__FreeBSD__)
33145132Sanholtint drm_mmap(dev_t kdev, vm_offset_t offset, int prot)
34145132Sanholt#elif defined(__NetBSD__) || defined(__OpenBSD__)
35145132Sanholtpaddr_t drm_mmap(dev_t kdev, off_t offset, int prot)
36145132Sanholt#endif
37145132Sanholt{
38145132Sanholt	DRM_DEVICE;
39145132Sanholt	drm_local_map_t *map;
40145132Sanholt	drm_file_t *priv;
41145132Sanholt	drm_map_type_t type;
42145132Sanholt
43145132Sanholt	DRM_LOCK();
44145132Sanholt	priv = drm_find_file_by_proc(dev, DRM_CURPROC);
45145132Sanholt	DRM_UNLOCK();
46145132Sanholt	if (priv == NULL) {
47145132Sanholt		DRM_ERROR("can't find authenticator\n");
48145132Sanholt		return EINVAL;
49145132Sanholt	}
50145132Sanholt
51145132Sanholt	if (!priv->authenticated)
52145132Sanholt		return DRM_ERR(EACCES);
53145132Sanholt
54145132Sanholt	DRM_SPINLOCK(&dev->dma_lock);
55145132Sanholt	if (dev->dma && offset >= 0 && offset < ptoa(dev->dma->page_count)) {
56145132Sanholt		drm_device_dma_t *dma = dev->dma;
57145132Sanholt
58145132Sanholt		if (dma->pagelist != NULL) {
59145132Sanholt			unsigned long page = offset >> PAGE_SHIFT;
60145132Sanholt			unsigned long phys = dma->pagelist[page];
61145132Sanholt
62145132Sanholt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
63145132Sanholt			*paddr = phys;
64145132Sanholt			DRM_SPINUNLOCK(&dev->dma_lock);
65145132Sanholt			return 0;
66145132Sanholt#else
67145132Sanholt			return atop(phys);
68145132Sanholt#endif
69145132Sanholt		} else {
70145132Sanholt			DRM_SPINUNLOCK(&dev->dma_lock);
71145132Sanholt			return -1;
72145132Sanholt		}
73145132Sanholt	}
74145132Sanholt	DRM_SPINUNLOCK(&dev->dma_lock);
75145132Sanholt
76145132Sanholt				/* A sequential search of a linked list is
77145132Sanholt				   fine here because: 1) there will only be
78145132Sanholt				   about 5-10 entries in the list and, 2) a
79145132Sanholt				   DRI client only has to do this mapping
80145132Sanholt				   once, so it doesn't have to be optimized
81145132Sanholt				   for performance, even if the list was a
82145132Sanholt				   bit longer. */
83145132Sanholt	DRM_LOCK();
84145132Sanholt	TAILQ_FOREACH(map, &dev->maplist, link) {
85145132Sanholt		if (offset >= map->offset && offset < map->offset + map->size)
86145132Sanholt			break;
87145132Sanholt	}
88145132Sanholt
89145132Sanholt	if (map == NULL) {
90145132Sanholt		DRM_UNLOCK();
91145132Sanholt		DRM_DEBUG("can't find map\n");
92145132Sanholt		return -1;
93145132Sanholt	}
94145132Sanholt	if (((map->flags&_DRM_RESTRICTED) && DRM_SUSER(DRM_CURPROC))) {
95145132Sanholt		DRM_UNLOCK();
96145132Sanholt		DRM_DEBUG("restricted map\n");
97145132Sanholt		return -1;
98145132Sanholt	}
99145132Sanholt	type = map->type;
100145132Sanholt	DRM_UNLOCK();
101145132Sanholt
102145132Sanholt	switch (type) {
103145132Sanholt	case _DRM_FRAME_BUFFER:
104145132Sanholt	case _DRM_REGISTERS:
105145132Sanholt	case _DRM_AGP:
106145132Sanholt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
107145132Sanholt		*paddr = offset;
108145132Sanholt		return 0;
109145132Sanholt#else
110145132Sanholt		return atop(offset);
111145132Sanholt#endif
112145132Sanholt	case _DRM_SCATTER_GATHER:
113145132Sanholt	case _DRM_SHM:
114145132Sanholt#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
115145132Sanholt		*paddr = vtophys(offset);
116145132Sanholt		return 0;
117145132Sanholt#else
118145132Sanholt		return atop(vtophys(offset));
119145132Sanholt#endif
120145132Sanholt	default:
121145132Sanholt		return -1;	/* This should never happen. */
122145132Sanholt	}
123145132Sanholt	DRM_DEBUG("bailing out\n");
124145132Sanholt
125145132Sanholt	return -1;
126145132Sanholt}
127145132Sanholt
128