drm_scatter.c revision 1.3.4.2 1 1.3.4.2 tls /* $NetBSD: drm_scatter.c,v 1.3.4.2 2014/08/20 00:04:20 tls Exp $ */
2 1.3.4.2 tls
3 1.3.4.2 tls /*-
4 1.3.4.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.3.4.2 tls * All rights reserved.
6 1.3.4.2 tls *
7 1.3.4.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 tls * by Taylor R. Campbell.
9 1.3.4.2 tls *
10 1.3.4.2 tls * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 tls * modification, are permitted provided that the following conditions
12 1.3.4.2 tls * are met:
13 1.3.4.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 tls * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 tls * documentation and/or other materials provided with the distribution.
18 1.3.4.2 tls *
19 1.3.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.4.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.4.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.4.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.4.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.4.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.4.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.4.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.4.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.4.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.4.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.3.4.2 tls */
31 1.3.4.2 tls
32 1.3.4.2 tls #include <sys/cdefs.h>
33 1.3.4.2 tls __KERNEL_RCSID(0, "$NetBSD: drm_scatter.c,v 1.3.4.2 2014/08/20 00:04:20 tls Exp $");
34 1.3.4.2 tls
35 1.3.4.2 tls #include <sys/types.h>
36 1.3.4.2 tls #include <sys/bus.h>
37 1.3.4.2 tls #include <sys/errno.h>
38 1.3.4.2 tls #include <sys/systm.h>
39 1.3.4.2 tls
40 1.3.4.2 tls #include <linux/slab.h>
41 1.3.4.2 tls
42 1.3.4.2 tls #include <drm/drmP.h>
43 1.3.4.2 tls
44 1.3.4.2 tls static int drm_sg_alloc_mem(struct drm_device *, size_t,
45 1.3.4.2 tls struct drm_sg_mem **);
46 1.3.4.2 tls static void drm_sg_free_mem(struct drm_device *, struct drm_sg_mem *);
47 1.3.4.2 tls
48 1.3.4.2 tls int
49 1.3.4.2 tls drm_sg_alloc(struct drm_device *dev, void *data,
50 1.3.4.2 tls struct drm_file *file __unused)
51 1.3.4.2 tls {
52 1.3.4.2 tls struct drm_scatter_gather *const request = data;
53 1.3.4.2 tls struct drm_sg_mem *sg;
54 1.3.4.2 tls int error;
55 1.3.4.2 tls
56 1.3.4.2 tls /*
57 1.3.4.2 tls * XXX Should not hold this mutex during drm_sg_alloc. For
58 1.3.4.2 tls * now, we'll just use non-blocking allocations.
59 1.3.4.2 tls */
60 1.3.4.2 tls KASSERT(mutex_is_locked(&drm_global_mutex));
61 1.3.4.2 tls
62 1.3.4.2 tls /*
63 1.3.4.2 tls * Sanity-check the inputs.
64 1.3.4.2 tls */
65 1.3.4.2 tls if (!drm_core_check_feature(dev, DRIVER_SG))
66 1.3.4.2 tls return -ENODEV;
67 1.3.4.2 tls if (dev->sg != NULL)
68 1.3.4.2 tls return -EBUSY;
69 1.3.4.2 tls if (request->size > 0xffffffffUL)
70 1.3.4.2 tls return -ENOMEM;
71 1.3.4.2 tls
72 1.3.4.2 tls /*
73 1.3.4.2 tls * Do the allocation.
74 1.3.4.2 tls *
75 1.3.4.2 tls * XXX Would it be safe to drop drm_global_mutex here to avoid
76 1.3.4.2 tls * holding it during allocation?
77 1.3.4.2 tls */
78 1.3.4.2 tls error = drm_sg_alloc_mem(dev, request->size, &sg);
79 1.3.4.2 tls if (error)
80 1.3.4.2 tls return error;
81 1.3.4.2 tls
82 1.3.4.2 tls /* Set it up to be the device's scatter-gather memory. */
83 1.3.4.2 tls dev->sg = sg;
84 1.3.4.2 tls
85 1.3.4.2 tls /* Success! */
86 1.3.4.2 tls request->handle = sg->handle;
87 1.3.4.2 tls return 0;
88 1.3.4.2 tls }
89 1.3.4.2 tls
90 1.3.4.2 tls int
91 1.3.4.2 tls drm_sg_free(struct drm_device *dev, void *data, struct drm_file *file)
92 1.3.4.2 tls {
93 1.3.4.2 tls struct drm_scatter_gather *const request = data;
94 1.3.4.2 tls struct drm_sg_mem *sg;
95 1.3.4.2 tls
96 1.3.4.2 tls KASSERT(mutex_is_locked(&drm_global_mutex));
97 1.3.4.2 tls
98 1.3.4.2 tls /*
99 1.3.4.2 tls * Sanity-check the inputs.
100 1.3.4.2 tls */
101 1.3.4.2 tls if (!drm_core_check_feature(dev, DRIVER_SG))
102 1.3.4.2 tls return -ENODEV;
103 1.3.4.2 tls
104 1.3.4.2 tls sg = dev->sg;
105 1.3.4.2 tls if (sg == NULL)
106 1.3.4.2 tls return -ENXIO;
107 1.3.4.2 tls if (sg->handle != request->handle)
108 1.3.4.2 tls return -EINVAL;
109 1.3.4.2 tls
110 1.3.4.2 tls /* Remove dev->sg. */
111 1.3.4.2 tls dev->sg = NULL;
112 1.3.4.2 tls
113 1.3.4.2 tls /* Free it. */
114 1.3.4.2 tls drm_sg_free_mem(dev, sg);
115 1.3.4.2 tls
116 1.3.4.2 tls /* Success! */
117 1.3.4.2 tls return 0;
118 1.3.4.2 tls }
119 1.3.4.2 tls
120 1.3.4.2 tls void
121 1.3.4.2 tls drm_legacy_sg_cleanup(struct drm_device *dev)
122 1.3.4.2 tls {
123 1.3.4.2 tls
124 1.3.4.2 tls if (!drm_core_check_feature(dev, DRIVER_SG))
125 1.3.4.2 tls return;
126 1.3.4.2 tls if (dev->sg == NULL)
127 1.3.4.2 tls return;
128 1.3.4.2 tls if (drm_core_check_feature(dev, DRIVER_MODESET))
129 1.3.4.2 tls return;
130 1.3.4.2 tls
131 1.3.4.2 tls drm_sg_free_mem(dev, dev->sg);
132 1.3.4.2 tls }
133 1.3.4.2 tls
134 1.3.4.2 tls static int
135 1.3.4.2 tls drm_sg_alloc_mem(struct drm_device *dev, size_t size, struct drm_sg_mem **sgp)
136 1.3.4.2 tls {
137 1.3.4.2 tls int nsegs;
138 1.3.4.2 tls int error;
139 1.3.4.2 tls
140 1.3.4.2 tls KASSERT(drm_core_check_feature(dev, DRIVER_SG));
141 1.3.4.2 tls
142 1.3.4.2 tls KASSERT(size <= (size_t)0xffffffffUL); /* XXX 32-bit sizes only? */
143 1.3.4.2 tls const size_t nbytes = PAGE_ALIGN(size);
144 1.3.4.2 tls const size_t npages = nbytes >> PAGE_SHIFT;
145 1.3.4.2 tls KASSERT(npages <= (size_t)INT_MAX);
146 1.3.4.2 tls
147 1.3.4.2 tls /*
148 1.3.4.2 tls * Allocate a drm_sg_mem record.
149 1.3.4.2 tls */
150 1.3.4.2 tls struct drm_sg_mem *const sg =
151 1.3.4.2 tls kmem_zalloc(offsetof(struct drm_sg_mem, sg_segs[npages]),
152 1.3.4.2 tls KM_NOSLEEP);
153 1.3.4.2 tls if (sg == NULL)
154 1.3.4.2 tls return -ENOMEM;
155 1.3.4.2 tls sg->sg_tag = dev->dmat;
156 1.3.4.2 tls sg->sg_nsegs_max = (unsigned int)npages;
157 1.3.4.2 tls
158 1.3.4.2 tls /*
159 1.3.4.2 tls * Allocate the requested amount of DMA-safe memory.
160 1.3.4.2 tls */
161 1.3.4.2 tls KASSERT(sg->sg_nsegs_max <= (unsigned int)INT_MAX);
162 1.3.4.2 tls /* XXX errno NetBSD->Linux */
163 1.3.4.2 tls error = -bus_dmamem_alloc(sg->sg_tag, nbytes, PAGE_SIZE, 0,
164 1.3.4.2 tls sg->sg_segs, (int)sg->sg_nsegs_max, &nsegs, BUS_DMA_NOWAIT);
165 1.3.4.2 tls if (error)
166 1.3.4.2 tls goto fail0;
167 1.3.4.2 tls KASSERT(0 <= nsegs);
168 1.3.4.2 tls sg->sg_nsegs = (unsigned int)nsegs;
169 1.3.4.2 tls
170 1.3.4.2 tls /*
171 1.3.4.2 tls * XXX Old drm passed DRM_BUS_NOWAIT below but BUS_DMA_WAITOK
172 1.3.4.2 tls * above. WTF?
173 1.3.4.2 tls */
174 1.3.4.2 tls
175 1.3.4.2 tls /*
176 1.3.4.2 tls * Map the DMA-safe memory into kernel virtual address space.
177 1.3.4.2 tls */
178 1.3.4.2 tls /* XXX errno NetBSD->Linux */
179 1.3.4.2 tls error = -bus_dmamem_map(sg->sg_tag, sg->sg_segs, nsegs, nbytes,
180 1.3.4.2 tls &sg->virtual,
181 1.3.4.2 tls (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
182 1.3.4.2 tls if (error)
183 1.3.4.2 tls goto fail1;
184 1.3.4.2 tls sg->sg_size = nbytes;
185 1.3.4.2 tls
186 1.3.4.2 tls /*
187 1.3.4.2 tls * Create a map for DMA transfers.
188 1.3.4.2 tls */
189 1.3.4.2 tls /* XXX errno NetBSD->Linux */
190 1.3.4.2 tls error = -bus_dmamap_create(sg->sg_tag, nbytes, nsegs, nbytes, 0,
191 1.3.4.2 tls BUS_DMA_NOWAIT, &sg->sg_map);
192 1.3.4.2 tls if (error)
193 1.3.4.2 tls goto fail2;
194 1.3.4.2 tls
195 1.3.4.2 tls /*
196 1.3.4.2 tls * Load the kva buffer into the map for DMA transfers.
197 1.3.4.2 tls */
198 1.3.4.2 tls /* XXX errno NetBSD->Linux */
199 1.3.4.2 tls error = -bus_dmamap_load(sg->sg_tag, sg->sg_map, sg->virtual, nbytes,
200 1.3.4.2 tls NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
201 1.3.4.2 tls if (error)
202 1.3.4.2 tls goto fail3;
203 1.3.4.2 tls
204 1.3.4.2 tls /*
205 1.3.4.2 tls * Use the kernel virtual address as the userland handle.
206 1.3.4.2 tls *
207 1.3.4.2 tls * XXX This leaks some information about kernel virtual
208 1.3.4.2 tls * addresses to userland; should we use an idr instead?
209 1.3.4.2 tls */
210 1.3.4.2 tls sg->handle = (unsigned long)(uintptr_t)sg->virtual;
211 1.3.4.2 tls KASSERT(sg->virtual == (void *)(uintptr_t)sg->handle);
212 1.3.4.2 tls
213 1.3.4.2 tls /* Success! */
214 1.3.4.2 tls *sgp = sg;
215 1.3.4.2 tls return 0;
216 1.3.4.2 tls
217 1.3.4.2 tls fail3: bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
218 1.3.4.2 tls fail2: bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
219 1.3.4.2 tls fail1: KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
220 1.3.4.2 tls bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
221 1.3.4.2 tls fail0: sg->sg_tag = NULL; /* XXX paranoia */
222 1.3.4.2 tls kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
223 1.3.4.2 tls return error;
224 1.3.4.2 tls }
225 1.3.4.2 tls
226 1.3.4.2 tls static void
227 1.3.4.2 tls drm_sg_free_mem(struct drm_device *dev, struct drm_sg_mem *sg)
228 1.3.4.2 tls {
229 1.3.4.2 tls
230 1.3.4.2 tls bus_dmamap_unload(sg->sg_tag, sg->sg_map);
231 1.3.4.2 tls bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
232 1.3.4.2 tls bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
233 1.3.4.2 tls KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
234 1.3.4.2 tls bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
235 1.3.4.2 tls sg->sg_tag = NULL; /* XXX paranoia */
236 1.3.4.2 tls kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
237 1.3.4.2 tls }
238