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