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