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