xref: /openbsd-src/sys/dev/pci/drm/drm_vm.c (revision 850e275390052b330d93020bf619a739a3c277ac)
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 
24 /** @file drm_vm.c
25  * Support code for mmaping of DRM maps.
26  */
27 
28 #include "drmP.h"
29 #include "drm.h"
30 
31 paddr_t
32 drmmmap(dev_t kdev, off_t offset, int prot)
33 {
34 	struct drm_device	*dev = drm_get_device_from_kdev(kdev);
35 	drm_local_map_t		*map;
36 	struct drm_file		*priv;
37 	enum drm_map_type	 type;
38 	paddr_t			 phys;
39 
40 	DRM_LOCK();
41 	priv = drm_find_file_by_minor(dev, minor(kdev));
42 	DRM_UNLOCK();
43 	if (priv == NULL) {
44 		DRM_ERROR("can't find authenticator\n");
45 		return (EINVAL);
46 	}
47 
48 	if (!priv->authenticated)
49 		return (EACCES);
50 
51 	if (dev->dma && offset >= 0 && offset < ptoa(dev->dma->page_count)) {
52 		drm_device_dma_t *dma = dev->dma;
53 
54 		DRM_SPINLOCK(&dev->dma_lock);
55 
56 		if (dma->pagelist != NULL) {
57 			unsigned long page = offset >> PAGE_SHIFT;
58 			unsigned long phys = dma->pagelist[page];
59 
60 			DRM_SPINUNLOCK(&dev->dma_lock);
61 			return (atop(phys));
62 		} else {
63 			DRM_SPINUNLOCK(&dev->dma_lock);
64 			return (-1);
65 		}
66 	}
67 
68 	/*
69 	 * A sequential search of a linked list is
70  	 * fine here because: 1) there will only be
71 	 * about 5-10 entries in the list and, 2) a
72 	 * DRI client only has to do this mapping
73 	 * once, so it doesn't have to be optimized
74 	 * for performance, even if the list was a
75 	 * bit longer.
76 	 */
77 	DRM_LOCK();
78 	TAILQ_FOREACH(map, &dev->maplist, link) {
79 		if (offset >= map->ext &&
80 		    offset < map->ext + map->size) {
81 			offset -= map->ext;
82 			break;
83 		}
84 	}
85 
86 	if (map == NULL) {
87 		DRM_UNLOCK();
88 		DRM_DEBUG("can't find map\n");
89 		return (-1);
90 	}
91 	if (((map->flags & _DRM_RESTRICTED) && priv->master == 0)) {
92 		DRM_UNLOCK();
93 		DRM_DEBUG("restricted map\n");
94 		return (-1);
95 	}
96 	type = map->type;
97 	DRM_UNLOCK();
98 
99 	switch (type) {
100 	case _DRM_FRAME_BUFFER:
101 	case _DRM_REGISTERS:
102 	case _DRM_AGP:
103 		phys = offset + map->offset;
104 		break;
105 	/* XXX unify all the bus_dmamem_mmap bits */
106 	case _DRM_SCATTER_GATHER:
107 		return (bus_dmamem_mmap(dev->pa.pa_dmat, dev->sg->mem->sg_segs,
108 		    dev->sg->mem->sg_nsegs, map->offset - dev->sg->handle +
109 		    offset, prot, BUS_DMA_NOWAIT));
110 	case _DRM_SHM:
111 	case _DRM_CONSISTENT:
112 		return (bus_dmamem_mmap(dev->pa.pa_dmat, &map->dmah->seg, 1,
113 		    offset, prot, BUS_DMA_NOWAIT));
114 	default:
115 		DRM_ERROR("bad map type %d\n", type);
116 		return (-1);	/* This should never happen. */
117 	}
118 
119 	return (atop(phys));
120 }
121 
122