drm_memory.c revision 1.6.4.3 1 1.6.4.2 tls /* $NetBSD: drm_memory.c,v 1.6.4.3 2017/12/03 11:37:58 jdolecek Exp $ */
2 1.6.4.2 tls
3 1.6.4.2 tls /*-
4 1.6.4.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.6.4.2 tls * All rights reserved.
6 1.6.4.2 tls *
7 1.6.4.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.6.4.2 tls * by Taylor R. Campbell.
9 1.6.4.2 tls *
10 1.6.4.2 tls * Redistribution and use in source and binary forms, with or without
11 1.6.4.2 tls * modification, are permitted provided that the following conditions
12 1.6.4.2 tls * are met:
13 1.6.4.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.6.4.2 tls * notice, this list of conditions and the following disclaimer.
15 1.6.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.6.4.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.6.4.2 tls * documentation and/or other materials provided with the distribution.
18 1.6.4.2 tls *
19 1.6.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.6.4.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.6.4.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.6.4.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.6.4.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.6.4.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.6.4.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.6.4.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.6.4.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.6.4.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.6.4.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.6.4.2 tls */
31 1.6.4.2 tls
32 1.6.4.2 tls #include <sys/cdefs.h>
33 1.6.4.2 tls __KERNEL_RCSID(0, "$NetBSD: drm_memory.c,v 1.6.4.3 2017/12/03 11:37:58 jdolecek Exp $");
34 1.6.4.2 tls
35 1.6.4.3 jdolecek #if defined(__i386__) || defined(__x86_64__)
36 1.6.4.3 jdolecek
37 1.6.4.3 jdolecek # ifdef _KERNEL_OPT
38 1.6.4.3 jdolecek # include "agp.h"
39 1.6.4.3 jdolecek # if NAGP > 0
40 1.6.4.3 jdolecek # include "agp_i810.h"
41 1.6.4.3 jdolecek # else
42 1.6.4.3 jdolecek # define NAGP_I810 0
43 1.6.4.3 jdolecek # endif
44 1.6.4.3 jdolecek # include "genfb.h"
45 1.6.4.3 jdolecek # else
46 1.6.4.3 jdolecek # define NAGP_I810 1
47 1.6.4.3 jdolecek # define NGENFB 0
48 1.6.4.3 jdolecek # endif
49 1.6.4.3 jdolecek
50 1.6.4.2 tls #else
51 1.6.4.3 jdolecek
52 1.6.4.3 jdolecek # ifdef _KERNEL_OPT
53 1.6.4.3 jdolecek # define NAGP_I810 0
54 1.6.4.3 jdolecek # include "genfb.h"
55 1.6.4.3 jdolecek # else
56 1.6.4.3 jdolecek # define NAGP_I810 0
57 1.6.4.3 jdolecek # define NGENFB 0
58 1.6.4.3 jdolecek # endif
59 1.6.4.3 jdolecek
60 1.6.4.2 tls #endif
61 1.6.4.2 tls
62 1.6.4.2 tls #include <sys/bus.h>
63 1.6.4.2 tls
64 1.6.4.2 tls #if NAGP_I810 > 0
65 1.6.4.2 tls /* XXX include order botch -- shouldn't need to include pcivar.h */
66 1.6.4.2 tls #include <dev/pci/pcivar.h>
67 1.6.4.2 tls #include <dev/pci/agpvar.h>
68 1.6.4.2 tls #endif
69 1.6.4.2 tls
70 1.6.4.2 tls #if NGENFB > 0
71 1.6.4.2 tls #include <dev/wsfb/genfbvar.h>
72 1.6.4.2 tls #endif
73 1.6.4.2 tls
74 1.6.4.2 tls #include <drm/drmP.h>
75 1.6.4.2 tls
76 1.6.4.2 tls /*
77 1.6.4.2 tls * XXX drm_bus_borrow is a horrible kludge!
78 1.6.4.2 tls */
79 1.6.4.2 tls static bool
80 1.6.4.2 tls drm_bus_borrow(bus_addr_t base, bus_size_t size, bus_space_handle_t *handlep)
81 1.6.4.2 tls {
82 1.6.4.2 tls
83 1.6.4.2 tls #if NAGP_I810 > 0
84 1.6.4.2 tls if (agp_i810_borrow(base, size, handlep))
85 1.6.4.2 tls return true;
86 1.6.4.2 tls #endif
87 1.6.4.2 tls
88 1.6.4.2 tls #if NGENFB > 0
89 1.6.4.2 tls if (genfb_borrow(base, handlep))
90 1.6.4.2 tls return true;
91 1.6.4.2 tls #endif
92 1.6.4.2 tls
93 1.6.4.2 tls return false;
94 1.6.4.2 tls }
95 1.6.4.2 tls
96 1.6.4.3 jdolecek void
97 1.6.4.3 jdolecek drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
98 1.6.4.2 tls {
99 1.6.4.2 tls const bus_space_tag_t bst = dev->bst;
100 1.6.4.2 tls unsigned int unit;
101 1.6.4.2 tls
102 1.6.4.2 tls /*
103 1.6.4.2 tls * Search dev's bus maps for a match.
104 1.6.4.2 tls */
105 1.6.4.2 tls for (unit = 0; unit < dev->bus_nmaps; unit++) {
106 1.6.4.2 tls struct drm_bus_map *const bm = &dev->bus_maps[unit];
107 1.6.4.2 tls int flags = bm->bm_flags;
108 1.6.4.2 tls
109 1.6.4.2 tls /* Reject maps starting after the request. */
110 1.6.4.2 tls if (map->offset < bm->bm_base)
111 1.6.4.2 tls continue;
112 1.6.4.2 tls
113 1.6.4.2 tls /* Reject maps smaller than the request. */
114 1.6.4.2 tls if (bm->bm_size < map->size)
115 1.6.4.2 tls continue;
116 1.6.4.2 tls
117 1.6.4.2 tls /* Reject maps that the request doesn't fit in. */
118 1.6.4.2 tls if ((bm->bm_size - map->size) <
119 1.6.4.2 tls (map->offset - bm->bm_base))
120 1.6.4.2 tls continue;
121 1.6.4.2 tls
122 1.6.4.2 tls /* Ensure we can map the space into virtual memory. */
123 1.6.4.2 tls if (!ISSET(flags, BUS_SPACE_MAP_LINEAR))
124 1.6.4.2 tls continue;
125 1.6.4.2 tls
126 1.6.4.2 tls /* Reflect requested flags in the bus_space map. */
127 1.6.4.2 tls if (ISSET(map->flags, _DRM_WRITE_COMBINING))
128 1.6.4.2 tls flags |= BUS_SPACE_MAP_PREFETCHABLE;
129 1.6.4.2 tls
130 1.6.4.2 tls /* Map it. */
131 1.6.4.2 tls if (bus_space_map(bst, map->offset, map->size, flags,
132 1.6.4.2 tls &map->lm_data.bus_space.bsh))
133 1.6.4.2 tls break;
134 1.6.4.2 tls
135 1.6.4.2 tls map->lm_data.bus_space.bus_map = bm;
136 1.6.4.2 tls goto win;
137 1.6.4.2 tls }
138 1.6.4.2 tls
139 1.6.4.2 tls /* Couldn't map it. Try borrowing from someone else. */
140 1.6.4.2 tls if (drm_bus_borrow(map->offset, map->size,
141 1.6.4.2 tls &map->lm_data.bus_space.bsh)) {
142 1.6.4.2 tls map->lm_data.bus_space.bus_map = NULL;
143 1.6.4.2 tls goto win;
144 1.6.4.2 tls }
145 1.6.4.2 tls
146 1.6.4.2 tls /* Failure! */
147 1.6.4.3 jdolecek return;
148 1.6.4.2 tls
149 1.6.4.2 tls win: map->lm_data.bus_space.bst = bst;
150 1.6.4.3 jdolecek map->handle = bus_space_vaddr(bst, map->lm_data.bus_space.bsh);
151 1.6.4.2 tls }
152 1.6.4.2 tls
153 1.6.4.2 tls void
154 1.6.4.3 jdolecek drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
155 1.6.4.2 tls {
156 1.6.4.2 tls if (map->lm_data.bus_space.bus_map != NULL) {
157 1.6.4.2 tls bus_space_unmap(map->lm_data.bus_space.bst,
158 1.6.4.2 tls map->lm_data.bus_space.bsh, map->size);
159 1.6.4.2 tls map->lm_data.bus_space.bus_map = NULL;
160 1.6.4.3 jdolecek map->handle = NULL;
161 1.6.4.2 tls }
162 1.6.4.2 tls }
163 1.6.4.2 tls
164 1.6.4.2 tls /*
165 1.6.4.2 tls * Allocate a drm dma handle, allocate memory fit for DMA, and map it.
166 1.6.4.2 tls *
167 1.6.4.2 tls * XXX This is called drm_pci_alloc for hysterical raisins; it is not
168 1.6.4.2 tls * specific to PCI.
169 1.6.4.2 tls *
170 1.6.4.2 tls * XXX For now, we use non-blocking allocations because this is called
171 1.6.4.2 tls * by ioctls with the drm global mutex held.
172 1.6.4.2 tls *
173 1.6.4.2 tls * XXX Error information is lost because this returns NULL on failure,
174 1.6.4.2 tls * not even an error embedded in a pointer.
175 1.6.4.2 tls */
176 1.6.4.2 tls struct drm_dma_handle *
177 1.6.4.2 tls drm_pci_alloc(struct drm_device *dev, size_t size, size_t align)
178 1.6.4.2 tls {
179 1.6.4.2 tls int nsegs;
180 1.6.4.2 tls int error;
181 1.6.4.2 tls
182 1.6.4.2 tls /*
183 1.6.4.2 tls * Allocate a drm_dma_handle record.
184 1.6.4.2 tls */
185 1.6.4.2 tls struct drm_dma_handle *const dmah = kmem_alloc(sizeof(*dmah),
186 1.6.4.2 tls KM_NOSLEEP);
187 1.6.4.2 tls if (dmah == NULL) {
188 1.6.4.2 tls error = -ENOMEM;
189 1.6.4.2 tls goto out;
190 1.6.4.2 tls }
191 1.6.4.2 tls dmah->dmah_tag = dev->dmat;
192 1.6.4.2 tls
193 1.6.4.2 tls /*
194 1.6.4.2 tls * Allocate the requested amount of DMA-safe memory.
195 1.6.4.2 tls */
196 1.6.4.2 tls /* XXX errno NetBSD->Linux */
197 1.6.4.2 tls error = -bus_dmamem_alloc(dmah->dmah_tag, size, align, 0,
198 1.6.4.2 tls &dmah->dmah_seg, 1, &nsegs, BUS_DMA_NOWAIT);
199 1.6.4.2 tls if (error)
200 1.6.4.2 tls goto fail0;
201 1.6.4.2 tls KASSERT(nsegs == 1);
202 1.6.4.2 tls
203 1.6.4.2 tls /*
204 1.6.4.2 tls * Map the DMA-safe memory into kernel virtual address space.
205 1.6.4.2 tls */
206 1.6.4.2 tls /* XXX errno NetBSD->Linux */
207 1.6.4.2 tls error = -bus_dmamem_map(dmah->dmah_tag, &dmah->dmah_seg, 1, size,
208 1.6.4.2 tls &dmah->vaddr,
209 1.6.4.2 tls (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
210 1.6.4.2 tls if (error)
211 1.6.4.2 tls goto fail1;
212 1.6.4.2 tls dmah->size = size;
213 1.6.4.2 tls
214 1.6.4.2 tls /*
215 1.6.4.2 tls * Create a map for DMA transfers.
216 1.6.4.2 tls */
217 1.6.4.2 tls /* XXX errno NetBSD->Linux */
218 1.6.4.2 tls error = -bus_dmamap_create(dmah->dmah_tag, size, 1, size, 0,
219 1.6.4.2 tls BUS_DMA_NOWAIT, &dmah->dmah_map);
220 1.6.4.2 tls if (error)
221 1.6.4.2 tls goto fail2;
222 1.6.4.2 tls
223 1.6.4.2 tls /*
224 1.6.4.2 tls * Load the kva buffer into the map for DMA transfers.
225 1.6.4.2 tls */
226 1.6.4.2 tls /* XXX errno NetBSD->Linux */
227 1.6.4.2 tls error = -bus_dmamap_load(dmah->dmah_tag, dmah->dmah_map, dmah->vaddr,
228 1.6.4.2 tls size, NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
229 1.6.4.2 tls if (error)
230 1.6.4.2 tls goto fail3;
231 1.6.4.2 tls
232 1.6.4.2 tls /* Record the bus address for convenient reference. */
233 1.6.4.2 tls dmah->busaddr = dmah->dmah_map->dm_segs[0].ds_addr;
234 1.6.4.2 tls
235 1.6.4.2 tls /* Zero the DMA buffer. XXX Yikes! Is this necessary? */
236 1.6.4.2 tls memset(dmah->vaddr, 0, size);
237 1.6.4.2 tls
238 1.6.4.2 tls /* Success! */
239 1.6.4.2 tls return dmah;
240 1.6.4.2 tls
241 1.6.4.2 tls fail3: bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
242 1.6.4.2 tls fail2: bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
243 1.6.4.2 tls fail1: bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
244 1.6.4.2 tls fail0: dmah->dmah_tag = NULL; /* XXX paranoia */
245 1.6.4.2 tls kmem_free(dmah, sizeof(*dmah));
246 1.6.4.2 tls out: DRM_DEBUG("drm_pci_alloc failed: %d\n", error);
247 1.6.4.2 tls return NULL;
248 1.6.4.2 tls }
249 1.6.4.2 tls
250 1.6.4.2 tls /*
251 1.6.4.2 tls * Release the bus DMA mappings and memory in dmah, and deallocate it.
252 1.6.4.2 tls */
253 1.6.4.2 tls void
254 1.6.4.2 tls drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah)
255 1.6.4.2 tls {
256 1.6.4.2 tls
257 1.6.4.2 tls bus_dmamap_unload(dmah->dmah_tag, dmah->dmah_map);
258 1.6.4.2 tls bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
259 1.6.4.2 tls bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
260 1.6.4.2 tls bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
261 1.6.4.2 tls dmah->dmah_tag = NULL; /* XXX paranoia */
262 1.6.4.2 tls kmem_free(dmah, sizeof(*dmah));
263 1.6.4.2 tls }
264 1.6.4.2 tls
265 1.6.4.2 tls /*
266 1.6.4.2 tls * Make sure the DMA-safe memory allocated for dev lies between
267 1.6.4.2 tls * min_addr and max_addr. Can be used multiple times to restrict the
268 1.6.4.2 tls * bounds further, but never to expand the bounds again.
269 1.6.4.2 tls *
270 1.6.4.2 tls * XXX Caller must guarantee nobody has used the tag yet,
271 1.6.4.2 tls * i.e. allocated any DMA memory.
272 1.6.4.2 tls */
273 1.6.4.2 tls int
274 1.6.4.2 tls drm_limit_dma_space(struct drm_device *dev, resource_size_t min_addr,
275 1.6.4.2 tls resource_size_t max_addr)
276 1.6.4.2 tls {
277 1.6.4.2 tls int ret;
278 1.6.4.2 tls
279 1.6.4.2 tls KASSERT(min_addr <= max_addr);
280 1.6.4.2 tls
281 1.6.4.2 tls /*
282 1.6.4.2 tls * Limit it further if we have already limited it, and destroy
283 1.6.4.2 tls * the old subregion DMA tag.
284 1.6.4.2 tls */
285 1.6.4.2 tls if (dev->dmat_subregion_p) {
286 1.6.4.2 tls min_addr = MAX(min_addr, dev->dmat_subregion_min);
287 1.6.4.2 tls max_addr = MIN(max_addr, dev->dmat_subregion_max);
288 1.6.4.2 tls bus_dmatag_destroy(dev->dmat);
289 1.6.4.2 tls }
290 1.6.4.2 tls
291 1.6.4.2 tls /*
292 1.6.4.2 tls * Create a DMA tag for a subregion from the bus's DMA tag. If
293 1.6.4.2 tls * that fails, restore dev->dmat to the whole region so that we
294 1.6.4.2 tls * need not worry about dev->dmat being uninitialized (not that
295 1.6.4.2 tls * the caller should try to allocate DMA-safe memory on failure
296 1.6.4.2 tls * anyway, but...paranoia).
297 1.6.4.2 tls */
298 1.6.4.2 tls /* XXX errno NetBSD->Linux */
299 1.6.4.2 tls ret = -bus_dmatag_subregion(dev->bus_dmat, min_addr, max_addr,
300 1.6.4.2 tls &dev->dmat, BUS_DMA_WAITOK);
301 1.6.4.2 tls if (ret) {
302 1.6.4.2 tls dev->dmat = dev->bus_dmat;
303 1.6.4.2 tls dev->dmat_subregion_p = false;
304 1.6.4.2 tls return ret;
305 1.6.4.2 tls }
306 1.6.4.2 tls
307 1.6.4.2 tls /*
308 1.6.4.2 tls * Remember that we have a subregion tag so that we know to
309 1.6.4.2 tls * destroy it later, and record the bounds in case we need to
310 1.6.4.2 tls * limit them again.
311 1.6.4.2 tls */
312 1.6.4.2 tls dev->dmat_subregion_p = true;
313 1.6.4.2 tls dev->dmat_subregion_min = min_addr;
314 1.6.4.2 tls dev->dmat_subregion_max = max_addr;
315 1.6.4.2 tls
316 1.6.4.2 tls /* Success! */
317 1.6.4.2 tls return 0;
318 1.6.4.2 tls }
319