drm_scatter.c revision 1.6 1 /* $NetBSD: drm_scatter.c,v 1.6 2021/12/19 00:28:20 riastradh 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_scatter.c,v 1.6 2021/12/19 00:28:20 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/bus.h>
37 #include <sys/errno.h>
38 #include <sys/systm.h>
39
40 #include <linux/slab.h>
41
42 #include <drm/drmP.h>
43
44 #include "../dist/drm/drm_internal.h"
45 #include "../dist/drm/drm_legacy.h"
46
47 static int drm_sg_alloc_mem(struct drm_device *, size_t,
48 struct drm_sg_mem **);
49 static void drm_sg_free_mem(struct drm_device *, struct drm_sg_mem *);
50
51 int
52 drm_legacy_sg_alloc(struct drm_device *dev, void *data,
53 struct drm_file *file __unused)
54 {
55 struct drm_scatter_gather *const request = data;
56 struct drm_sg_mem *sg = NULL;
57 int error;
58
59 /*
60 * XXX Should not hold this mutex during drm_sg_alloc. For
61 * now, we'll just use non-blocking allocations.
62 */
63 KASSERT(mutex_is_locked(&drm_global_mutex));
64
65 /*
66 * Sanity-check the inputs.
67 */
68 if (!drm_core_check_feature(dev, DRIVER_SG))
69 return -ENODEV;
70 if (dev->sg != NULL)
71 return -EBUSY;
72 if (request->size > 0xffffffffUL)
73 return -ENOMEM;
74
75 /*
76 * Do the allocation.
77 *
78 * XXX Would it be safe to drop drm_global_mutex here to avoid
79 * holding it during allocation?
80 */
81 error = drm_sg_alloc_mem(dev, request->size, &sg);
82 if (error)
83 return error;
84
85 /* Set it up to be the device's scatter-gather memory. */
86 dev->sg = sg;
87
88 /* Success! */
89 request->handle = sg->handle;
90 return 0;
91 }
92
93 int
94 drm_legacy_sg_free(struct drm_device *dev, void *data, struct drm_file *file)
95 {
96 struct drm_scatter_gather *const request = data;
97 struct drm_sg_mem *sg;
98
99 KASSERT(mutex_is_locked(&drm_global_mutex));
100
101 /*
102 * Sanity-check the inputs.
103 */
104 if (!drm_core_check_feature(dev, DRIVER_SG))
105 return -ENODEV;
106
107 sg = dev->sg;
108 if (sg == NULL)
109 return -ENXIO;
110 if (sg->handle != request->handle)
111 return -EINVAL;
112
113 /* Remove dev->sg. */
114 dev->sg = NULL;
115
116 /* Free it. */
117 drm_sg_free_mem(dev, sg);
118
119 /* Success! */
120 return 0;
121 }
122
123 void
124 drm_legacy_sg_cleanup(struct drm_device *dev)
125 {
126
127 if (!drm_core_check_feature(dev, DRIVER_SG))
128 return;
129 if (dev->sg == NULL)
130 return;
131 if (drm_core_check_feature(dev, DRIVER_MODESET))
132 return;
133
134 drm_sg_free_mem(dev, dev->sg);
135 }
136
137 static int
138 drm_sg_alloc_mem(struct drm_device *dev, size_t size, struct drm_sg_mem **sgp)
139 {
140 int nsegs;
141 int error;
142
143 KASSERT(drm_core_check_feature(dev, DRIVER_SG));
144
145 KASSERT(size <= (size_t)0xffffffffUL); /* XXX 32-bit sizes only? */
146 const size_t nbytes = PAGE_ALIGN(size);
147 const size_t npages = nbytes >> PAGE_SHIFT;
148 KASSERT(npages <= (size_t)INT_MAX);
149
150 /*
151 * Allocate a drm_sg_mem record.
152 */
153 struct drm_sg_mem *const sg =
154 kmem_zalloc(offsetof(struct drm_sg_mem, sg_segs[npages]),
155 KM_NOSLEEP);
156 if (sg == NULL)
157 return -ENOMEM;
158 sg->sg_tag = dev->dmat;
159 sg->sg_nsegs_max = (unsigned int)npages;
160
161 /*
162 * Allocate the requested amount of DMA-safe memory.
163 */
164 KASSERT(sg->sg_nsegs_max <= (unsigned int)INT_MAX);
165 /* XXX errno NetBSD->Linux */
166 error = -bus_dmamem_alloc(sg->sg_tag, nbytes, PAGE_SIZE, 0,
167 sg->sg_segs, (int)sg->sg_nsegs_max, &nsegs, BUS_DMA_NOWAIT);
168 if (error)
169 goto fail0;
170 KASSERT(0 <= nsegs);
171 sg->sg_nsegs = (unsigned int)nsegs;
172
173 /*
174 * XXX Old drm passed DRM_BUS_NOWAIT below but BUS_DMA_WAITOK
175 * above. WTF?
176 */
177
178 /*
179 * Map the DMA-safe memory into kernel virtual address space.
180 */
181 /* XXX errno NetBSD->Linux */
182 error = -bus_dmamem_map(sg->sg_tag, sg->sg_segs, nsegs, nbytes,
183 &sg->virtual,
184 (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
185 if (error)
186 goto fail1;
187 sg->sg_size = nbytes;
188
189 /*
190 * Create a map for DMA transfers.
191 */
192 /* XXX errno NetBSD->Linux */
193 error = -bus_dmamap_create(sg->sg_tag, nbytes, nsegs, nbytes, 0,
194 BUS_DMA_NOWAIT, &sg->sg_map);
195 if (error)
196 goto fail2;
197
198 /*
199 * Load the kva buffer into the map for DMA transfers.
200 */
201 /* XXX errno NetBSD->Linux */
202 error = -bus_dmamap_load(sg->sg_tag, sg->sg_map, sg->virtual, nbytes,
203 NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
204 if (error)
205 goto fail3;
206
207 /*
208 * Use the kernel virtual address as the userland handle.
209 *
210 * XXX This leaks some information about kernel virtual
211 * addresses to userland; should we use an idr instead?
212 */
213 sg->handle = (unsigned long)(uintptr_t)sg->virtual;
214 KASSERT(sg->virtual == (void *)(uintptr_t)sg->handle);
215
216 /* Success! */
217 *sgp = sg;
218 return 0;
219
220 fail3: bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
221 fail2: bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
222 fail1: KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
223 bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
224 fail0: sg->sg_tag = NULL; /* XXX paranoia */
225 kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
226 return error;
227 }
228
229 static void
230 drm_sg_free_mem(struct drm_device *dev, struct drm_sg_mem *sg)
231 {
232
233 bus_dmamap_unload(sg->sg_tag, sg->sg_map);
234 bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
235 bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
236 KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
237 bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
238 sg->sg_tag = NULL; /* XXX paranoia */
239 kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
240 }
241