drm_vm.c revision 145132
1/*-
2 * Copyright 2003 Eric Anholt
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * $FreeBSD: head/sys/dev/drm/drm_vm.c 145132 2005-04-16 03:44:47Z anholt $
24 */
25
26#include "dev/drm/drmP.h"
27#include "dev/drm/drm.h"
28
29#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
30int drm_mmap(struct cdev *kdev, vm_offset_t offset, vm_paddr_t *paddr,
31    int prot)
32#elif defined(__FreeBSD__)
33int drm_mmap(dev_t kdev, vm_offset_t offset, int prot)
34#elif defined(__NetBSD__) || defined(__OpenBSD__)
35paddr_t drm_mmap(dev_t kdev, off_t offset, int prot)
36#endif
37{
38	DRM_DEVICE;
39	drm_local_map_t *map;
40	drm_file_t *priv;
41	drm_map_type_t type;
42
43	DRM_LOCK();
44	priv = drm_find_file_by_proc(dev, DRM_CURPROC);
45	DRM_UNLOCK();
46	if (priv == NULL) {
47		DRM_ERROR("can't find authenticator\n");
48		return EINVAL;
49	}
50
51	if (!priv->authenticated)
52		return DRM_ERR(EACCES);
53
54	DRM_SPINLOCK(&dev->dma_lock);
55	if (dev->dma && offset >= 0 && offset < ptoa(dev->dma->page_count)) {
56		drm_device_dma_t *dma = dev->dma;
57
58		if (dma->pagelist != NULL) {
59			unsigned long page = offset >> PAGE_SHIFT;
60			unsigned long phys = dma->pagelist[page];
61
62#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
63			*paddr = phys;
64			DRM_SPINUNLOCK(&dev->dma_lock);
65			return 0;
66#else
67			return atop(phys);
68#endif
69		} else {
70			DRM_SPINUNLOCK(&dev->dma_lock);
71			return -1;
72		}
73	}
74	DRM_SPINUNLOCK(&dev->dma_lock);
75
76				/* A sequential search of a linked list is
77				   fine here because: 1) there will only be
78				   about 5-10 entries in the list and, 2) a
79				   DRI client only has to do this mapping
80				   once, so it doesn't have to be optimized
81				   for performance, even if the list was a
82				   bit longer. */
83	DRM_LOCK();
84	TAILQ_FOREACH(map, &dev->maplist, link) {
85		if (offset >= map->offset && offset < map->offset + map->size)
86			break;
87	}
88
89	if (map == NULL) {
90		DRM_UNLOCK();
91		DRM_DEBUG("can't find map\n");
92		return -1;
93	}
94	if (((map->flags&_DRM_RESTRICTED) && DRM_SUSER(DRM_CURPROC))) {
95		DRM_UNLOCK();
96		DRM_DEBUG("restricted map\n");
97		return -1;
98	}
99	type = map->type;
100	DRM_UNLOCK();
101
102	switch (type) {
103	case _DRM_FRAME_BUFFER:
104	case _DRM_REGISTERS:
105	case _DRM_AGP:
106#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
107		*paddr = offset;
108		return 0;
109#else
110		return atop(offset);
111#endif
112	case _DRM_SCATTER_GATHER:
113	case _DRM_SHM:
114#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
115		*paddr = vtophys(offset);
116		return 0;
117#else
118		return atop(vtophys(offset));
119#endif
120	default:
121		return -1;	/* This should never happen. */
122	}
123	DRM_DEBUG("bailing out\n");
124
125	return -1;
126}
127
128