drm_vm.c revision 1.9 1 /* $NetBSD: drm_vm.c,v 1.9 2018/08/27 07:51:06 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_vm.c,v 1.9 2018/08/27 07:51:06 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/conf.h>
37
38 #include <uvm/uvm_extern.h>
39 #include <uvm/uvm_device.h>
40
41 #include <drm/drmP.h>
42 #include <drm/drm_legacy.h>
43
44 static paddr_t drm_legacy_mmap_paddr_locked(struct drm_device *, off_t, int);
45 static paddr_t drm_legacy_mmap_dma_paddr(struct drm_device *, off_t, int);
46 static paddr_t drm_legacy_mmap_map_paddr(struct drm_device *,
47 struct drm_local_map *, off_t, int);
48
49 int
50 drm_legacy_mmap_object(struct drm_device *dev, off_t offset, size_t size,
51 int prot, struct uvm_object **uobjp, voff_t *uoffsetp,
52 struct file *file __unused)
53 {
54 devmajor_t maj = cdevsw_lookup_major(&drm_cdevsw);
55 dev_t devno = makedev(maj, dev->primary->index);
56 struct uvm_object *uobj;
57
58 KASSERT(offset == (offset & ~(PAGE_SIZE-1)));
59
60 /*
61 * Attach the device. The size and offset are used only for
62 * access checks; offset does not become a base address for the
63 * subsequent uvm_map, hence we set *uoffsetp to offset, not 0.
64 */
65 uobj = udv_attach(devno, prot, offset, size);
66 if (uobj == NULL)
67 return -EINVAL;
68
69 *uobjp = uobj;
70 *uoffsetp = offset;
71 return 0;
72 }
73
74 paddr_t
75 drm_legacy_mmap_paddr(struct drm_device *dev, off_t byte_offset, int prot)
76 {
77 paddr_t paddr;
78
79 if (byte_offset != (byte_offset & ~(PAGE_SIZE-1)))
80 return -1;
81
82 mutex_lock(&dev->struct_mutex);
83 paddr = drm_legacy_mmap_paddr_locked(dev, byte_offset, prot);
84 mutex_unlock(&dev->struct_mutex);
85
86 return paddr;
87 }
88
89 static paddr_t
90 drm_legacy_mmap_paddr_locked(struct drm_device *dev, off_t byte_offset,
91 int prot)
92 {
93 const off_t page_offset = (byte_offset >> PAGE_SHIFT);
94 struct drm_hash_item *hash;
95
96 KASSERT(mutex_is_locked(&dev->struct_mutex));
97 KASSERT(byte_offset == (byte_offset & ~(PAGE_SIZE-1)));
98
99 if ((dev->dma != NULL) &&
100 (0 <= byte_offset) &&
101 (page_offset <= dev->dma->page_count))
102 return drm_legacy_mmap_dma_paddr(dev, byte_offset, prot);
103
104 if (drm_ht_find_item(&dev->map_hash, page_offset, &hash))
105 return -1;
106
107 struct drm_local_map *const map = drm_hash_entry(hash,
108 struct drm_map_list, hash)->map;
109 if (map == NULL)
110 return -1;
111
112 /*
113 * XXX FreeBSD drops the mutex at this point, which would be
114 * nice, to allow sleeping in bus_dma cruft, but I don't know
115 * what guarantees the map will continue to exist.
116 */
117
118 if (ISSET(map->flags, _DRM_RESTRICTED) && !DRM_SUSER())
119 return -1;
120
121 if (!(map->offset <= byte_offset))
122 return -1;
123 if (map->size < (map->offset - byte_offset))
124 return -1;
125
126 return drm_legacy_mmap_map_paddr(dev, map, (byte_offset - map->offset),
127 prot);
128 }
129
130 static paddr_t
131 drm_legacy_mmap_dma_paddr(struct drm_device *dev, off_t byte_offset, int prot)
132 {
133 const off_t page_offset = (byte_offset >> PAGE_SHIFT);
134
135 KASSERT(mutex_is_locked(&dev->struct_mutex));
136 KASSERT(byte_offset == (byte_offset & ~(PAGE_SIZE-1)));
137 KASSERT(page_offset <= dev->dma->page_count);
138
139 if (dev->dma->pagelist == NULL)
140 return (paddr_t)-1;
141
142 return dev->dma->pagelist[page_offset];
143 }
144
145 static paddr_t
146 drm_legacy_mmap_map_paddr(struct drm_device *dev, struct drm_local_map *map,
147 off_t byte_offset, int prot)
148 {
149 int flags = 0;
150
151 KASSERT(byte_offset <= map->size);
152
153 switch (map->type) {
154 case _DRM_FRAME_BUFFER:
155 case _DRM_AGP:
156 flags |= BUS_SPACE_MAP_PREFETCHABLE;
157 /* Fall through. */
158
159 case _DRM_REGISTERS:
160 flags |= BUS_SPACE_MAP_LINEAR; /* XXX Why? */
161
162 return bus_space_mmap(map->lm_data.bus_space.bst, map->offset,
163 byte_offset, prot, flags);
164
165 case _DRM_CONSISTENT: {
166 struct drm_dma_handle *const dmah = map->lm_data.dmah;
167
168 return bus_dmamem_mmap(dev->dmat, &dmah->dmah_seg, 1,
169 byte_offset, prot,
170 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */
171 /* XXX What else? BUS_DMA_COHERENT? */
172 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
173 }
174
175 case _DRM_SCATTER_GATHER: {
176 struct drm_sg_mem *const sg = dev->sg;
177
178 #if 0 /* XXX */
179 KASSERT(sg == map->lm_data.sg);
180 #endif
181
182 return bus_dmamem_mmap(dev->dmat, sg->sg_segs, sg->sg_nsegs,
183 byte_offset, prot,
184 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */
185 /* XXX What else? BUS_DMA_COHERENT? */
186 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
187 }
188
189 case _DRM_SHM:
190 default:
191 return (paddr_t)-1;
192 }
193 }
194