drm_memory.c revision 1.9 1 /* $NetBSD: drm_memory.c,v 1.9 2016/03/05 06:48:31 mlelstv 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_memory.c,v 1.9 2016/03/05 06:48:31 mlelstv Exp $");
34
35 #if defined(__i386__) || defined(__x86_64__)
36 #define HAS_AGP_I810 1
37 #else
38 #define HAS_AGP_I810 0
39 #endif
40
41 #ifdef _KERNEL_OPT
42 # include "agp.h"
43 # if NAGP == 0
44 # define NAGP_I810 0
45 # elif HAS_AGP_I810 > 0
46 # include "agp_i810.h"
47 # else
48 # define NAGP_I810 0
49 # endif
50 # include "genfb.h"
51 #else
52 # define NAGP_I810 HAS_AGP_I810 /* XXX WTF? */
53 # define NGENFB 0 /* XXX WTF? */
54 #endif
55
56 #include <sys/bus.h>
57
58 #if NAGP_I810 > 0
59 /* XXX include order botch -- shouldn't need to include pcivar.h */
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/agpvar.h>
62 #endif
63
64 #if NGENFB > 0
65 #include <dev/wsfb/genfbvar.h>
66 #endif
67
68 #include <drm/drmP.h>
69
70 /*
71 * XXX drm_bus_borrow is a horrible kludge!
72 */
73 static bool
74 drm_bus_borrow(bus_addr_t base, bus_size_t size, bus_space_handle_t *handlep)
75 {
76
77 #if NAGP_I810 > 0
78 if (agp_i810_borrow(base, size, handlep))
79 return true;
80 #endif
81
82 #if NGENFB > 0
83 if (genfb_borrow(base, handlep))
84 return true;
85 #endif
86
87 return false;
88 }
89
90 void
91 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
92 {
93 const bus_space_tag_t bst = dev->bst;
94 unsigned int unit;
95
96 /*
97 * Search dev's bus maps for a match.
98 */
99 for (unit = 0; unit < dev->bus_nmaps; unit++) {
100 struct drm_bus_map *const bm = &dev->bus_maps[unit];
101 int flags = bm->bm_flags;
102
103 /* Reject maps starting after the request. */
104 if (map->offset < bm->bm_base)
105 continue;
106
107 /* Reject maps smaller than the request. */
108 if (bm->bm_size < map->size)
109 continue;
110
111 /* Reject maps that the request doesn't fit in. */
112 if ((bm->bm_size - map->size) <
113 (map->offset - bm->bm_base))
114 continue;
115
116 /* Ensure we can map the space into virtual memory. */
117 if (!ISSET(flags, BUS_SPACE_MAP_LINEAR))
118 continue;
119
120 /* Reflect requested flags in the bus_space map. */
121 if (ISSET(map->flags, _DRM_WRITE_COMBINING))
122 flags |= BUS_SPACE_MAP_PREFETCHABLE;
123
124 /* Map it. */
125 if (bus_space_map(bst, map->offset, map->size, flags,
126 &map->lm_data.bus_space.bsh))
127 break;
128
129 map->lm_data.bus_space.bus_map = bm;
130 goto win;
131 }
132
133 /* Couldn't map it. Try borrowing from someone else. */
134 if (drm_bus_borrow(map->offset, map->size,
135 &map->lm_data.bus_space.bsh)) {
136 map->lm_data.bus_space.bus_map = NULL;
137 goto win;
138 }
139
140 /* Failure! */
141 return;
142
143 win: map->lm_data.bus_space.bst = bst;
144 map->handle = bus_space_vaddr(bst, map->lm_data.bus_space.bsh);
145 }
146
147 void
148 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
149 {
150 if (map->lm_data.bus_space.bus_map != NULL) {
151 bus_space_unmap(map->lm_data.bus_space.bst,
152 map->lm_data.bus_space.bsh, map->size);
153 map->lm_data.bus_space.bus_map = NULL;
154 map->handle = NULL;
155 }
156 }
157
158 /*
159 * Allocate a drm dma handle, allocate memory fit for DMA, and map it.
160 *
161 * XXX This is called drm_pci_alloc for hysterical raisins; it is not
162 * specific to PCI.
163 *
164 * XXX For now, we use non-blocking allocations because this is called
165 * by ioctls with the drm global mutex held.
166 *
167 * XXX Error information is lost because this returns NULL on failure,
168 * not even an error embedded in a pointer.
169 */
170 struct drm_dma_handle *
171 drm_pci_alloc(struct drm_device *dev, size_t size, size_t align)
172 {
173 int nsegs;
174 int error;
175
176 /*
177 * Allocate a drm_dma_handle record.
178 */
179 struct drm_dma_handle *const dmah = kmem_alloc(sizeof(*dmah),
180 KM_NOSLEEP);
181 if (dmah == NULL) {
182 error = -ENOMEM;
183 goto out;
184 }
185 dmah->dmah_tag = dev->dmat;
186
187 /*
188 * Allocate the requested amount of DMA-safe memory.
189 */
190 /* XXX errno NetBSD->Linux */
191 error = -bus_dmamem_alloc(dmah->dmah_tag, size, align, 0,
192 &dmah->dmah_seg, 1, &nsegs, BUS_DMA_NOWAIT);
193 if (error)
194 goto fail0;
195 KASSERT(nsegs == 1);
196
197 /*
198 * Map the DMA-safe memory into kernel virtual address space.
199 */
200 /* XXX errno NetBSD->Linux */
201 error = -bus_dmamem_map(dmah->dmah_tag, &dmah->dmah_seg, 1, size,
202 &dmah->vaddr,
203 (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
204 if (error)
205 goto fail1;
206 dmah->size = size;
207
208 /*
209 * Create a map for DMA transfers.
210 */
211 /* XXX errno NetBSD->Linux */
212 error = -bus_dmamap_create(dmah->dmah_tag, size, 1, size, 0,
213 BUS_DMA_NOWAIT, &dmah->dmah_map);
214 if (error)
215 goto fail2;
216
217 /*
218 * Load the kva buffer into the map for DMA transfers.
219 */
220 /* XXX errno NetBSD->Linux */
221 error = -bus_dmamap_load(dmah->dmah_tag, dmah->dmah_map, dmah->vaddr,
222 size, NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
223 if (error)
224 goto fail3;
225
226 /* Record the bus address for convenient reference. */
227 dmah->busaddr = dmah->dmah_map->dm_segs[0].ds_addr;
228
229 /* Zero the DMA buffer. XXX Yikes! Is this necessary? */
230 memset(dmah->vaddr, 0, size);
231
232 /* Success! */
233 return dmah;
234
235 fail3: bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
236 fail2: bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
237 fail1: bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
238 fail0: dmah->dmah_tag = NULL; /* XXX paranoia */
239 kmem_free(dmah, sizeof(*dmah));
240 out: DRM_DEBUG("drm_pci_alloc failed: %d\n", error);
241 return NULL;
242 }
243
244 /*
245 * Release the bus DMA mappings and memory in dmah, and deallocate it.
246 */
247 void
248 drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah)
249 {
250
251 bus_dmamap_unload(dmah->dmah_tag, dmah->dmah_map);
252 bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
253 bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
254 bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
255 dmah->dmah_tag = NULL; /* XXX paranoia */
256 kmem_free(dmah, sizeof(*dmah));
257 }
258
259 /*
260 * Make sure the DMA-safe memory allocated for dev lies between
261 * min_addr and max_addr. Can be used multiple times to restrict the
262 * bounds further, but never to expand the bounds again.
263 *
264 * XXX Caller must guarantee nobody has used the tag yet,
265 * i.e. allocated any DMA memory.
266 */
267 int
268 drm_limit_dma_space(struct drm_device *dev, resource_size_t min_addr,
269 resource_size_t max_addr)
270 {
271 int ret;
272
273 KASSERT(min_addr <= max_addr);
274
275 /*
276 * Limit it further if we have already limited it, and destroy
277 * the old subregion DMA tag.
278 */
279 if (dev->dmat_subregion_p) {
280 min_addr = MAX(min_addr, dev->dmat_subregion_min);
281 max_addr = MIN(max_addr, dev->dmat_subregion_max);
282 bus_dmatag_destroy(dev->dmat);
283 }
284
285 /*
286 * Create a DMA tag for a subregion from the bus's DMA tag. If
287 * that fails, restore dev->dmat to the whole region so that we
288 * need not worry about dev->dmat being uninitialized (not that
289 * the caller should try to allocate DMA-safe memory on failure
290 * anyway, but...paranoia).
291 */
292 /* XXX errno NetBSD->Linux */
293 ret = -bus_dmatag_subregion(dev->bus_dmat, min_addr, max_addr,
294 &dev->dmat, BUS_DMA_WAITOK);
295 if (ret) {
296 dev->dmat = dev->bus_dmat;
297 dev->dmat_subregion_p = false;
298 return ret;
299 }
300
301 /*
302 * Remember that we have a subregion tag so that we know to
303 * destroy it later, and record the bounds in case we need to
304 * limit them again.
305 */
306 dev->dmat_subregion_p = true;
307 dev->dmat_subregion_min = min_addr;
308 dev->dmat_subregion_max = max_addr;
309
310 /* Success! */
311 return 0;
312 }
313