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