drm_prime.c revision 1.13 1 1.13 riastrad /* $NetBSD: drm_prime.c,v 1.13 2021/12/19 01:56:58 riastradh Exp $ */
2 1.2 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright 2012 Red Hat
5 1.1 riastrad *
6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
7 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
8 1.1 riastrad * to deal in the Software without restriction, including without limitation
9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
11 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
12 1.1 riastrad *
13 1.1 riastrad * The above copyright notice and this permission notice (including the next
14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
15 1.1 riastrad * Software.
16 1.1 riastrad *
17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 1.1 riastrad * IN THE SOFTWARE.
24 1.1 riastrad *
25 1.1 riastrad * Authors:
26 1.1 riastrad * Dave Airlie <airlied (at) redhat.com>
27 1.1 riastrad * Rob Clark <rob.clark (at) linaro.org>
28 1.1 riastrad *
29 1.1 riastrad */
30 1.1 riastrad
31 1.2 riastrad #include <sys/cdefs.h>
32 1.13 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_prime.c,v 1.13 2021/12/19 01:56:58 riastradh Exp $");
33 1.2 riastrad
34 1.1 riastrad #include <linux/export.h>
35 1.1 riastrad #include <linux/dma-buf.h>
36 1.10 riastrad #include <linux/rbtree.h>
37 1.10 riastrad
38 1.10 riastrad #include <drm/drm.h>
39 1.10 riastrad #include <drm/drm_drv.h>
40 1.10 riastrad #include <drm/drm_file.h>
41 1.10 riastrad #include <drm/drm_framebuffer.h>
42 1.2 riastrad #include <drm/drm_gem.h>
43 1.10 riastrad #include <drm/drm_prime.h>
44 1.2 riastrad
45 1.2 riastrad #include "drm_internal.h"
46 1.1 riastrad
47 1.4 riastrad #ifdef __NetBSD__
48 1.4 riastrad
49 1.5 riastrad #include <drm/bus_dma_hacks.h>
50 1.5 riastrad
51 1.8 riastrad #include <linux/nbsd-namespace.h>
52 1.8 riastrad
53 1.4 riastrad /*
54 1.5 riastrad * We use struct sg_table just to pass around an array of pages from
55 1.5 riastrad * one device to another in drm prime. Since this is _not_ a complete
56 1.5 riastrad * implementation of Linux's sg table abstraction (e.g., it does not
57 1.5 riastrad * remember DMA addresses and RAM pages separately, and it doesn't
58 1.5 riastrad * support the nested chained iteration of Linux scatterlists), we
59 1.5 riastrad * isolate it to this file and make all callers go through a few extra
60 1.5 riastrad * subroutines (drm_prime_sg_size, drm_prime_sg_free, &c.) to use it.
61 1.5 riastrad * Don't use this outside drm prime!
62 1.4 riastrad */
63 1.4 riastrad
64 1.3 riastrad struct sg_table {
65 1.6 riastrad paddr_t *sgt_pgs;
66 1.5 riastrad unsigned sgt_npgs;
67 1.3 riastrad };
68 1.3 riastrad
69 1.3 riastrad static int
70 1.3 riastrad sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
71 1.3 riastrad unsigned npages, bus_size_t offset, bus_size_t size, gfp_t gfp)
72 1.3 riastrad {
73 1.3 riastrad unsigned i;
74 1.3 riastrad
75 1.3 riastrad KASSERT(offset == 0);
76 1.3 riastrad KASSERT(size == npages << PAGE_SHIFT);
77 1.3 riastrad
78 1.5 riastrad sgt->sgt_pgs = kcalloc(npages, sizeof(sgt->sgt_pgs[0]), gfp);
79 1.5 riastrad if (sgt->sgt_pgs == NULL)
80 1.3 riastrad return -ENOMEM;
81 1.5 riastrad sgt->sgt_npgs = npages;
82 1.3 riastrad
83 1.5 riastrad for (i = 0; i < npages; i++)
84 1.6 riastrad sgt->sgt_pgs[i] = VM_PAGE_TO_PHYS(&pages[i]->p_vmp);
85 1.3 riastrad
86 1.3 riastrad return 0;
87 1.3 riastrad }
88 1.3 riastrad
89 1.3 riastrad static int
90 1.5 riastrad sg_alloc_table_from_pglist(struct sg_table *sgt, const struct pglist *pglist,
91 1.3 riastrad unsigned npages, bus_size_t offset, bus_size_t size, gfp_t gfp)
92 1.3 riastrad {
93 1.3 riastrad struct vm_page *pg;
94 1.3 riastrad unsigned i;
95 1.3 riastrad
96 1.3 riastrad KASSERT(offset == 0);
97 1.3 riastrad KASSERT(size == npages << PAGE_SHIFT);
98 1.3 riastrad
99 1.5 riastrad sgt->sgt_pgs = kcalloc(npages, sizeof(sgt->sgt_pgs[0]), gfp);
100 1.5 riastrad if (sgt->sgt_pgs == NULL)
101 1.3 riastrad return -ENOMEM;
102 1.5 riastrad sgt->sgt_npgs = npages;
103 1.3 riastrad
104 1.3 riastrad i = 0;
105 1.3 riastrad TAILQ_FOREACH(pg, pglist, pageq.queue) {
106 1.3 riastrad KASSERT(i < npages);
107 1.6 riastrad sgt->sgt_pgs[i] = VM_PAGE_TO_PHYS(pg);
108 1.3 riastrad }
109 1.3 riastrad KASSERT(i == npages);
110 1.3 riastrad
111 1.3 riastrad return 0;
112 1.3 riastrad }
113 1.3 riastrad
114 1.4 riastrad static int
115 1.5 riastrad sg_alloc_table_from_bus_dmamem(struct sg_table *sgt, bus_dma_tag_t dmat,
116 1.5 riastrad const bus_dma_segment_t *segs, int nsegs, gfp_t gfp)
117 1.4 riastrad {
118 1.5 riastrad int ret;
119 1.4 riastrad
120 1.4 riastrad KASSERT(nsegs > 0);
121 1.5 riastrad sgt->sgt_pgs = kcalloc(nsegs, sizeof(sgt->sgt_pgs[0]), gfp);
122 1.5 riastrad if (sgt->sgt_pgs == NULL)
123 1.4 riastrad return -ENOMEM;
124 1.5 riastrad sgt->sgt_npgs = nsegs;
125 1.4 riastrad
126 1.5 riastrad /* XXX errno NetBSD->Linux */
127 1.5 riastrad ret = -bus_dmamem_export_pages(dmat, segs, nsegs, sgt->sgt_pgs,
128 1.5 riastrad sgt->sgt_npgs);
129 1.5 riastrad if (ret)
130 1.5 riastrad return ret;
131 1.4 riastrad
132 1.4 riastrad return 0;
133 1.4 riastrad }
134 1.4 riastrad
135 1.3 riastrad static void
136 1.3 riastrad sg_free_table(struct sg_table *sgt)
137 1.3 riastrad {
138 1.3 riastrad
139 1.5 riastrad kfree(sgt->sgt_pgs);
140 1.5 riastrad sgt->sgt_pgs = NULL;
141 1.5 riastrad sgt->sgt_npgs = 0;
142 1.3 riastrad }
143 1.3 riastrad
144 1.4 riastrad #endif /* __NetBSD__ */
145 1.4 riastrad
146 1.10 riastrad /**
147 1.10 riastrad * DOC: overview and lifetime rules
148 1.1 riastrad *
149 1.10 riastrad * Similar to GEM global names, PRIME file descriptors are also used to share
150 1.10 riastrad * buffer objects across processes. They offer additional security: as file
151 1.10 riastrad * descriptors must be explicitly sent over UNIX domain sockets to be shared
152 1.10 riastrad * between applications, they can't be guessed like the globally unique GEM
153 1.10 riastrad * names.
154 1.10 riastrad *
155 1.10 riastrad * Drivers that support the PRIME API implement the
156 1.10 riastrad * &drm_driver.prime_handle_to_fd and &drm_driver.prime_fd_to_handle operations.
157 1.10 riastrad * GEM based drivers must use drm_gem_prime_handle_to_fd() and
158 1.10 riastrad * drm_gem_prime_fd_to_handle() to implement these. For GEM based drivers the
159 1.10 riastrad * actual driver interfaces is provided through the &drm_gem_object_funcs.export
160 1.10 riastrad * and &drm_driver.gem_prime_import hooks.
161 1.10 riastrad *
162 1.10 riastrad * &dma_buf_ops implementations for GEM drivers are all individually exported
163 1.10 riastrad * for drivers which need to overwrite or reimplement some of them.
164 1.10 riastrad *
165 1.10 riastrad * Reference Counting for GEM Drivers
166 1.10 riastrad * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167 1.10 riastrad *
168 1.10 riastrad * On the export the &dma_buf holds a reference to the exported buffer object,
169 1.10 riastrad * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD
170 1.10 riastrad * IOCTL, when it first calls &drm_gem_object_funcs.export
171 1.10 riastrad * and stores the exporting GEM object in the &dma_buf.priv field. This
172 1.10 riastrad * reference needs to be released when the final reference to the &dma_buf
173 1.10 riastrad * itself is dropped and its &dma_buf_ops.release function is called. For
174 1.10 riastrad * GEM-based drivers, the &dma_buf should be exported using
175 1.10 riastrad * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release().
176 1.10 riastrad *
177 1.10 riastrad * Thus the chain of references always flows in one direction, avoiding loops:
178 1.10 riastrad * importing GEM object -> dma-buf -> exported GEM bo. A further complication
179 1.10 riastrad * are the lookup caches for import and export. These are required to guarantee
180 1.10 riastrad * that any given object will always have only one uniqe userspace handle. This
181 1.10 riastrad * is required to allow userspace to detect duplicated imports, since some GEM
182 1.10 riastrad * drivers do fail command submissions if a given buffer object is listed more
183 1.10 riastrad * than once. These import and export caches in &drm_prime_file_private only
184 1.10 riastrad * retain a weak reference, which is cleaned up when the corresponding object is
185 1.10 riastrad * released.
186 1.10 riastrad *
187 1.10 riastrad * Self-importing: If userspace is using PRIME as a replacement for flink then
188 1.10 riastrad * it will get a fd->handle request for a GEM object that it created. Drivers
189 1.10 riastrad * should detect this situation and return back the underlying object from the
190 1.10 riastrad * dma-buf private. For GEM based drivers this is handled in
191 1.10 riastrad * drm_gem_prime_import() already.
192 1.1 riastrad */
193 1.1 riastrad
194 1.1 riastrad struct drm_prime_member {
195 1.1 riastrad struct dma_buf *dma_buf;
196 1.1 riastrad uint32_t handle;
197 1.1 riastrad
198 1.10 riastrad struct rb_node dmabuf_rb;
199 1.10 riastrad struct rb_node handle_rb;
200 1.2 riastrad };
201 1.2 riastrad
202 1.11 riastrad #ifdef __NetBSD__
203 1.11 riastrad static int
204 1.11 riastrad compare_dmabufs(void *cookie, const void *va, const void *vb)
205 1.11 riastrad {
206 1.11 riastrad const struct drm_prime_member *ma = va;
207 1.11 riastrad const struct drm_prime_member *mb = vb;
208 1.11 riastrad
209 1.11 riastrad if (ma->dma_buf < mb->dma_buf)
210 1.11 riastrad return -1;
211 1.11 riastrad if (ma->dma_buf > mb->dma_buf)
212 1.11 riastrad return +1;
213 1.11 riastrad return 0;
214 1.11 riastrad }
215 1.11 riastrad
216 1.11 riastrad static int
217 1.11 riastrad compare_dmabuf_key(void *cookie, const void *vm, const void *vk)
218 1.11 riastrad {
219 1.11 riastrad const struct drm_prime_member *m = vm;
220 1.11 riastrad const struct dma_buf *const *kp = vk;
221 1.11 riastrad
222 1.11 riastrad if (m->dma_buf < *kp)
223 1.11 riastrad return -1;
224 1.11 riastrad if (m->dma_buf > *kp)
225 1.11 riastrad return +1;
226 1.11 riastrad return 0;
227 1.11 riastrad }
228 1.11 riastrad
229 1.11 riastrad static int
230 1.11 riastrad compare_handles(void *cookie, const void *va, const void *vb)
231 1.11 riastrad {
232 1.11 riastrad const struct drm_prime_member *ma = va;
233 1.11 riastrad const struct drm_prime_member *mb = vb;
234 1.11 riastrad
235 1.11 riastrad if (ma->handle < mb->handle)
236 1.11 riastrad return -1;
237 1.11 riastrad if (ma->handle > mb->handle)
238 1.11 riastrad return +1;
239 1.11 riastrad return 0;
240 1.11 riastrad }
241 1.11 riastrad
242 1.11 riastrad static int
243 1.11 riastrad compare_handle_key(void *cookie, const void *vm, const void *vk)
244 1.11 riastrad {
245 1.11 riastrad const struct drm_prime_member *m = vm;
246 1.11 riastrad const uint32_t *kp = vk;
247 1.11 riastrad
248 1.11 riastrad if (m->handle < *kp)
249 1.11 riastrad return -1;
250 1.11 riastrad if (m->handle > *kp)
251 1.11 riastrad return +1;
252 1.11 riastrad return 0;
253 1.11 riastrad }
254 1.11 riastrad
255 1.11 riastrad static const rb_tree_ops_t dmabuf_ops = {
256 1.11 riastrad .rbto_compare_nodes = compare_dmabufs,
257 1.11 riastrad .rbto_compare_key = compare_dmabuf_key,
258 1.11 riastrad .rbto_node_offset = offsetof(struct drm_prime_member, dmabuf_rb),
259 1.11 riastrad };
260 1.11 riastrad
261 1.11 riastrad static const rb_tree_ops_t handle_ops = {
262 1.11 riastrad .rbto_compare_nodes = compare_handles,
263 1.11 riastrad .rbto_compare_key = compare_handle_key,
264 1.11 riastrad .rbto_node_offset = offsetof(struct drm_prime_member, handle_rb),
265 1.11 riastrad };
266 1.11 riastrad #endif
267 1.11 riastrad
268 1.2 riastrad static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
269 1.2 riastrad struct dma_buf *dma_buf, uint32_t handle)
270 1.2 riastrad {
271 1.2 riastrad struct drm_prime_member *member;
272 1.11 riastrad #ifdef __NetBSD__
273 1.11 riastrad struct drm_prime_member *collision __diagused;
274 1.11 riastrad #else
275 1.10 riastrad struct rb_node **p, *rb;
276 1.11 riastrad #endif
277 1.2 riastrad
278 1.2 riastrad member = kmalloc(sizeof(*member), GFP_KERNEL);
279 1.2 riastrad if (!member)
280 1.2 riastrad return -ENOMEM;
281 1.2 riastrad
282 1.2 riastrad get_dma_buf(dma_buf);
283 1.2 riastrad member->dma_buf = dma_buf;
284 1.2 riastrad member->handle = handle;
285 1.10 riastrad
286 1.11 riastrad #ifdef __NetBSD__
287 1.11 riastrad collision = rb_tree_insert_node(&prime_fpriv->dmabufs.rbr_tree,
288 1.11 riastrad member);
289 1.11 riastrad KASSERT(collision == NULL);
290 1.11 riastrad #else
291 1.10 riastrad rb = NULL;
292 1.10 riastrad p = &prime_fpriv->dmabufs.rb_node;
293 1.10 riastrad while (*p) {
294 1.10 riastrad struct drm_prime_member *pos;
295 1.10 riastrad
296 1.10 riastrad rb = *p;
297 1.10 riastrad pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
298 1.10 riastrad if (dma_buf > pos->dma_buf)
299 1.10 riastrad p = &rb->rb_right;
300 1.10 riastrad else
301 1.10 riastrad p = &rb->rb_left;
302 1.10 riastrad }
303 1.10 riastrad rb_link_node(&member->dmabuf_rb, rb, p);
304 1.10 riastrad rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
305 1.11 riastrad #endif
306 1.10 riastrad
307 1.11 riastrad #ifdef __NetBSD__
308 1.11 riastrad collision = rb_tree_insert_node(&prime_fpriv->handles.rbr_tree,
309 1.11 riastrad member);
310 1.11 riastrad KASSERT(collision == NULL);
311 1.11 riastrad #else
312 1.10 riastrad rb = NULL;
313 1.10 riastrad p = &prime_fpriv->handles.rb_node;
314 1.10 riastrad while (*p) {
315 1.10 riastrad struct drm_prime_member *pos;
316 1.10 riastrad
317 1.10 riastrad rb = *p;
318 1.10 riastrad pos = rb_entry(rb, struct drm_prime_member, handle_rb);
319 1.10 riastrad if (handle > pos->handle)
320 1.10 riastrad p = &rb->rb_right;
321 1.10 riastrad else
322 1.10 riastrad p = &rb->rb_left;
323 1.10 riastrad }
324 1.10 riastrad rb_link_node(&member->handle_rb, rb, p);
325 1.10 riastrad rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
326 1.11 riastrad #endif
327 1.10 riastrad
328 1.2 riastrad return 0;
329 1.2 riastrad }
330 1.2 riastrad
331 1.2 riastrad static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
332 1.2 riastrad uint32_t handle)
333 1.2 riastrad {
334 1.11 riastrad #ifdef __NetBSD__
335 1.11 riastrad return rb_tree_find_node(&prime_fpriv->handles.rbr_tree, &handle);
336 1.11 riastrad #else
337 1.10 riastrad struct rb_node *rb;
338 1.10 riastrad
339 1.10 riastrad rb = prime_fpriv->handles.rb_node;
340 1.10 riastrad while (rb) {
341 1.10 riastrad struct drm_prime_member *member;
342 1.2 riastrad
343 1.10 riastrad member = rb_entry(rb, struct drm_prime_member, handle_rb);
344 1.2 riastrad if (member->handle == handle)
345 1.2 riastrad return member->dma_buf;
346 1.10 riastrad else if (member->handle < handle)
347 1.10 riastrad rb = rb->rb_right;
348 1.10 riastrad else
349 1.10 riastrad rb = rb->rb_left;
350 1.2 riastrad }
351 1.2 riastrad
352 1.2 riastrad return NULL;
353 1.11 riastrad #endif
354 1.2 riastrad }
355 1.2 riastrad
356 1.2 riastrad static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
357 1.2 riastrad struct dma_buf *dma_buf,
358 1.2 riastrad uint32_t *handle)
359 1.2 riastrad {
360 1.11 riastrad #ifdef __NetBSD__
361 1.11 riastrad struct drm_prime_member *member;
362 1.11 riastrad
363 1.11 riastrad member = rb_tree_find_node(&prime_fpriv->dmabufs.rbr_tree, &dma_buf);
364 1.11 riastrad if (member == NULL)
365 1.11 riastrad return -ENOENT;
366 1.11 riastrad *handle = member->handle;
367 1.11 riastrad return 0;
368 1.11 riastrad #else
369 1.10 riastrad struct rb_node *rb;
370 1.10 riastrad
371 1.10 riastrad rb = prime_fpriv->dmabufs.rb_node;
372 1.10 riastrad while (rb) {
373 1.10 riastrad struct drm_prime_member *member;
374 1.2 riastrad
375 1.10 riastrad member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
376 1.2 riastrad if (member->dma_buf == dma_buf) {
377 1.2 riastrad *handle = member->handle;
378 1.2 riastrad return 0;
379 1.10 riastrad } else if (member->dma_buf < dma_buf) {
380 1.10 riastrad rb = rb->rb_right;
381 1.10 riastrad } else {
382 1.10 riastrad rb = rb->rb_left;
383 1.2 riastrad }
384 1.2 riastrad }
385 1.10 riastrad
386 1.2 riastrad return -ENOENT;
387 1.11 riastrad #endif
388 1.2 riastrad }
389 1.2 riastrad
390 1.2 riastrad void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
391 1.2 riastrad struct dma_buf *dma_buf)
392 1.2 riastrad {
393 1.11 riastrad #ifdef __NetBSD__
394 1.11 riastrad struct drm_prime_member *member;
395 1.11 riastrad
396 1.11 riastrad member = rb_tree_find_node(&prime_fpriv->dmabufs.rbr_tree, &dma_buf);
397 1.11 riastrad if (member != NULL) {
398 1.11 riastrad rb_tree_remove_node(&prime_fpriv->handles.rbr_tree, member);
399 1.11 riastrad rb_tree_remove_node(&prime_fpriv->dmabufs.rbr_tree, member);
400 1.11 riastrad }
401 1.11 riastrad #else
402 1.10 riastrad struct rb_node *rb;
403 1.2 riastrad
404 1.10 riastrad rb = prime_fpriv->dmabufs.rb_node;
405 1.10 riastrad while (rb) {
406 1.10 riastrad struct drm_prime_member *member;
407 1.10 riastrad
408 1.10 riastrad member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
409 1.2 riastrad if (member->dma_buf == dma_buf) {
410 1.10 riastrad rb_erase(&member->handle_rb, &prime_fpriv->handles);
411 1.10 riastrad rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
412 1.10 riastrad
413 1.2 riastrad dma_buf_put(dma_buf);
414 1.2 riastrad kfree(member);
415 1.10 riastrad return;
416 1.10 riastrad } else if (member->dma_buf < dma_buf) {
417 1.10 riastrad rb = rb->rb_right;
418 1.10 riastrad } else {
419 1.10 riastrad rb = rb->rb_left;
420 1.2 riastrad }
421 1.2 riastrad }
422 1.11 riastrad #endif
423 1.2 riastrad }
424 1.2 riastrad
425 1.10 riastrad void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
426 1.2 riastrad {
427 1.11 riastrad #ifdef __NetBSD__
428 1.11 riastrad linux_mutex_init(&prime_fpriv->lock);
429 1.11 riastrad #else
430 1.10 riastrad mutex_init(&prime_fpriv->lock);
431 1.11 riastrad #endif
432 1.11 riastrad #ifdef __NetBSD__
433 1.11 riastrad rb_tree_init(&prime_fpriv->dmabufs.rbr_tree, &dmabuf_ops);
434 1.11 riastrad rb_tree_init(&prime_fpriv->handles.rbr_tree, &handle_ops);
435 1.11 riastrad #else
436 1.10 riastrad prime_fpriv->dmabufs = RB_ROOT;
437 1.10 riastrad prime_fpriv->handles = RB_ROOT;
438 1.11 riastrad #endif
439 1.10 riastrad }
440 1.2 riastrad
441 1.10 riastrad void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
442 1.10 riastrad {
443 1.11 riastrad #ifdef __NetBSD__ /* XXX post-merge linux doesn't destroy it's lock now? */
444 1.11 riastrad linux_mutex_destroy(&prime_fpriv->lock);
445 1.11 riastrad #endif
446 1.10 riastrad /* by now drm_gem_release should've made sure the list is empty */
447 1.10 riastrad WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
448 1.10 riastrad }
449 1.2 riastrad
450 1.10 riastrad /**
451 1.10 riastrad * drm_gem_dmabuf_export - &dma_buf export implementation for GEM
452 1.10 riastrad * @dev: parent device for the exported dmabuf
453 1.10 riastrad * @exp_info: the export information used by dma_buf_export()
454 1.10 riastrad *
455 1.10 riastrad * This wraps dma_buf_export() for use by generic GEM drivers that are using
456 1.10 riastrad * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
457 1.10 riastrad * a reference to the &drm_device and the exported &drm_gem_object (stored in
458 1.10 riastrad * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
459 1.10 riastrad *
460 1.10 riastrad * Returns the new dmabuf.
461 1.10 riastrad */
462 1.10 riastrad struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
463 1.10 riastrad struct dma_buf_export_info *exp_info)
464 1.10 riastrad {
465 1.10 riastrad struct drm_gem_object *obj = exp_info->priv;
466 1.10 riastrad struct dma_buf *dma_buf;
467 1.2 riastrad
468 1.10 riastrad dma_buf = dma_buf_export(exp_info);
469 1.10 riastrad if (IS_ERR(dma_buf))
470 1.10 riastrad return dma_buf;
471 1.2 riastrad
472 1.10 riastrad drm_dev_get(dev);
473 1.10 riastrad drm_gem_object_get(obj);
474 1.10 riastrad dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping;
475 1.2 riastrad
476 1.10 riastrad return dma_buf;
477 1.2 riastrad }
478 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_export);
479 1.2 riastrad
480 1.2 riastrad /**
481 1.10 riastrad * drm_gem_dmabuf_release - &dma_buf release implementation for GEM
482 1.2 riastrad * @dma_buf: buffer to be released
483 1.2 riastrad *
484 1.2 riastrad * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
485 1.10 riastrad * must use this in their &dma_buf_ops structure as the release callback.
486 1.10 riastrad * drm_gem_dmabuf_release() should be used in conjunction with
487 1.10 riastrad * drm_gem_dmabuf_export().
488 1.2 riastrad */
489 1.2 riastrad void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
490 1.2 riastrad {
491 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
492 1.10 riastrad struct drm_device *dev = obj->dev;
493 1.2 riastrad
494 1.2 riastrad /* drop the reference on the export fd holds */
495 1.10 riastrad drm_gem_object_put_unlocked(obj);
496 1.10 riastrad
497 1.10 riastrad drm_dev_put(dev);
498 1.2 riastrad }
499 1.2 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_release);
500 1.2 riastrad
501 1.10 riastrad /**
502 1.10 riastrad * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
503 1.10 riastrad * @dev: dev to export the buffer from
504 1.10 riastrad * @file_priv: drm file-private structure
505 1.10 riastrad * @prime_fd: fd id of the dma-buf which should be imported
506 1.10 riastrad * @handle: pointer to storage for the handle of the imported buffer object
507 1.10 riastrad *
508 1.10 riastrad * This is the PRIME import function which must be used mandatorily by GEM
509 1.10 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
510 1.10 riastrad * The actual importing of GEM object from the dma-buf is done through the
511 1.10 riastrad * &drm_driver.gem_prime_import driver callback.
512 1.10 riastrad *
513 1.10 riastrad * Returns 0 on success or a negative error code on failure.
514 1.10 riastrad */
515 1.10 riastrad int drm_gem_prime_fd_to_handle(struct drm_device *dev,
516 1.10 riastrad struct drm_file *file_priv, int prime_fd,
517 1.10 riastrad uint32_t *handle)
518 1.2 riastrad {
519 1.10 riastrad struct dma_buf *dma_buf;
520 1.10 riastrad struct drm_gem_object *obj;
521 1.10 riastrad int ret;
522 1.2 riastrad
523 1.10 riastrad dma_buf = dma_buf_get(prime_fd);
524 1.10 riastrad if (IS_ERR(dma_buf))
525 1.10 riastrad return PTR_ERR(dma_buf);
526 1.2 riastrad
527 1.10 riastrad mutex_lock(&file_priv->prime.lock);
528 1.2 riastrad
529 1.10 riastrad ret = drm_prime_lookup_buf_handle(&file_priv->prime,
530 1.10 riastrad dma_buf, handle);
531 1.10 riastrad if (ret == 0)
532 1.10 riastrad goto out_put;
533 1.2 riastrad
534 1.10 riastrad /* never seen this one, need to import */
535 1.10 riastrad mutex_lock(&dev->object_name_lock);
536 1.10 riastrad if (dev->driver->gem_prime_import)
537 1.10 riastrad obj = dev->driver->gem_prime_import(dev, dma_buf);
538 1.10 riastrad else
539 1.10 riastrad obj = drm_gem_prime_import(dev, dma_buf);
540 1.10 riastrad if (IS_ERR(obj)) {
541 1.10 riastrad ret = PTR_ERR(obj);
542 1.10 riastrad goto out_unlock;
543 1.10 riastrad }
544 1.2 riastrad
545 1.10 riastrad if (obj->dma_buf) {
546 1.10 riastrad WARN_ON(obj->dma_buf != dma_buf);
547 1.10 riastrad } else {
548 1.10 riastrad obj->dma_buf = dma_buf;
549 1.10 riastrad get_dma_buf(dma_buf);
550 1.10 riastrad }
551 1.2 riastrad
552 1.10 riastrad /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
553 1.10 riastrad ret = drm_gem_handle_create_tail(file_priv, obj, handle);
554 1.10 riastrad drm_gem_object_put_unlocked(obj);
555 1.10 riastrad if (ret)
556 1.10 riastrad goto out_put;
557 1.2 riastrad
558 1.10 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
559 1.10 riastrad dma_buf, *handle);
560 1.10 riastrad mutex_unlock(&file_priv->prime.lock);
561 1.10 riastrad if (ret)
562 1.10 riastrad goto fail;
563 1.2 riastrad
564 1.10 riastrad dma_buf_put(dma_buf);
565 1.2 riastrad
566 1.10 riastrad return 0;
567 1.2 riastrad
568 1.10 riastrad fail:
569 1.10 riastrad /* hmm, if driver attached, we are relying on the free-object path
570 1.10 riastrad * to detach.. which seems ok..
571 1.10 riastrad */
572 1.10 riastrad drm_gem_handle_delete(file_priv, *handle);
573 1.10 riastrad dma_buf_put(dma_buf);
574 1.10 riastrad return ret;
575 1.2 riastrad
576 1.10 riastrad out_unlock:
577 1.10 riastrad mutex_unlock(&dev->object_name_lock);
578 1.10 riastrad out_put:
579 1.10 riastrad mutex_unlock(&file_priv->prime.lock);
580 1.10 riastrad dma_buf_put(dma_buf);
581 1.10 riastrad return ret;
582 1.2 riastrad }
583 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
584 1.2 riastrad
585 1.10 riastrad int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
586 1.10 riastrad struct drm_file *file_priv)
587 1.2 riastrad {
588 1.10 riastrad struct drm_prime_handle *args = data;
589 1.2 riastrad
590 1.10 riastrad if (!dev->driver->prime_fd_to_handle)
591 1.10 riastrad return -ENOSYS;
592 1.2 riastrad
593 1.10 riastrad return dev->driver->prime_fd_to_handle(dev, file_priv,
594 1.10 riastrad args->fd, &args->handle);
595 1.2 riastrad }
596 1.2 riastrad
597 1.2 riastrad static struct dma_buf *export_and_register_object(struct drm_device *dev,
598 1.2 riastrad struct drm_gem_object *obj,
599 1.2 riastrad uint32_t flags)
600 1.2 riastrad {
601 1.2 riastrad struct dma_buf *dmabuf;
602 1.2 riastrad
603 1.2 riastrad /* prevent races with concurrent gem_close. */
604 1.2 riastrad if (obj->handle_count == 0) {
605 1.2 riastrad dmabuf = ERR_PTR(-ENOENT);
606 1.2 riastrad return dmabuf;
607 1.2 riastrad }
608 1.2 riastrad
609 1.10 riastrad if (obj->funcs && obj->funcs->export)
610 1.10 riastrad dmabuf = obj->funcs->export(obj, flags);
611 1.10 riastrad else if (dev->driver->gem_prime_export)
612 1.10 riastrad dmabuf = dev->driver->gem_prime_export(obj, flags);
613 1.10 riastrad else
614 1.10 riastrad dmabuf = drm_gem_prime_export(obj, flags);
615 1.2 riastrad if (IS_ERR(dmabuf)) {
616 1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
617 1.2 riastrad * but if that fails then drop the ref
618 1.2 riastrad */
619 1.2 riastrad return dmabuf;
620 1.2 riastrad }
621 1.2 riastrad
622 1.2 riastrad /*
623 1.2 riastrad * Note that callers do not need to clean up the export cache
624 1.2 riastrad * since the check for obj->handle_count guarantees that someone
625 1.2 riastrad * will clean it up.
626 1.2 riastrad */
627 1.2 riastrad obj->dma_buf = dmabuf;
628 1.2 riastrad get_dma_buf(obj->dma_buf);
629 1.2 riastrad
630 1.2 riastrad return dmabuf;
631 1.2 riastrad }
632 1.2 riastrad
633 1.2 riastrad /**
634 1.2 riastrad * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
635 1.2 riastrad * @dev: dev to export the buffer from
636 1.2 riastrad * @file_priv: drm file-private structure
637 1.2 riastrad * @handle: buffer handle to export
638 1.2 riastrad * @flags: flags like DRM_CLOEXEC
639 1.2 riastrad * @prime_fd: pointer to storage for the fd id of the create dma-buf
640 1.2 riastrad *
641 1.2 riastrad * This is the PRIME export function which must be used mandatorily by GEM
642 1.2 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
643 1.2 riastrad * The actual exporting from GEM object to a dma-buf is done through the
644 1.10 riastrad * &drm_driver.gem_prime_export driver callback.
645 1.2 riastrad */
646 1.1 riastrad int drm_gem_prime_handle_to_fd(struct drm_device *dev,
647 1.2 riastrad struct drm_file *file_priv, uint32_t handle,
648 1.2 riastrad uint32_t flags,
649 1.2 riastrad int *prime_fd)
650 1.1 riastrad {
651 1.1 riastrad struct drm_gem_object *obj;
652 1.2 riastrad int ret = 0;
653 1.2 riastrad struct dma_buf *dmabuf;
654 1.1 riastrad
655 1.2 riastrad mutex_lock(&file_priv->prime.lock);
656 1.10 riastrad obj = drm_gem_object_lookup(file_priv, handle);
657 1.2 riastrad if (!obj) {
658 1.2 riastrad ret = -ENOENT;
659 1.2 riastrad goto out_unlock;
660 1.2 riastrad }
661 1.2 riastrad
662 1.2 riastrad dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
663 1.2 riastrad if (dmabuf) {
664 1.2 riastrad get_dma_buf(dmabuf);
665 1.2 riastrad goto out_have_handle;
666 1.2 riastrad }
667 1.1 riastrad
668 1.2 riastrad mutex_lock(&dev->object_name_lock);
669 1.1 riastrad /* re-export the original imported object */
670 1.1 riastrad if (obj->import_attach) {
671 1.2 riastrad dmabuf = obj->import_attach->dmabuf;
672 1.2 riastrad get_dma_buf(dmabuf);
673 1.2 riastrad goto out_have_obj;
674 1.2 riastrad }
675 1.2 riastrad
676 1.2 riastrad if (obj->dma_buf) {
677 1.2 riastrad get_dma_buf(obj->dma_buf);
678 1.2 riastrad dmabuf = obj->dma_buf;
679 1.2 riastrad goto out_have_obj;
680 1.2 riastrad }
681 1.2 riastrad
682 1.2 riastrad dmabuf = export_and_register_object(dev, obj, flags);
683 1.2 riastrad if (IS_ERR(dmabuf)) {
684 1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
685 1.2 riastrad * but if that fails then drop the ref
686 1.2 riastrad */
687 1.2 riastrad ret = PTR_ERR(dmabuf);
688 1.2 riastrad mutex_unlock(&dev->object_name_lock);
689 1.2 riastrad goto out;
690 1.1 riastrad }
691 1.1 riastrad
692 1.2 riastrad out_have_obj:
693 1.2 riastrad /*
694 1.2 riastrad * If we've exported this buffer then cheat and add it to the import list
695 1.2 riastrad * so we get the correct handle back. We must do this under the
696 1.2 riastrad * protection of dev->object_name_lock to ensure that a racing gem close
697 1.2 riastrad * ioctl doesn't miss to remove this buffer handle from the cache.
698 1.2 riastrad */
699 1.2 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
700 1.2 riastrad dmabuf, handle);
701 1.2 riastrad mutex_unlock(&dev->object_name_lock);
702 1.2 riastrad if (ret)
703 1.2 riastrad goto fail_put_dmabuf;
704 1.2 riastrad
705 1.2 riastrad out_have_handle:
706 1.2 riastrad ret = dma_buf_fd(dmabuf, flags);
707 1.2 riastrad /*
708 1.2 riastrad * We must _not_ remove the buffer from the handle cache since the newly
709 1.2 riastrad * created dma buf is already linked in the global obj->dma_buf pointer,
710 1.2 riastrad * and that is invariant as long as a userspace gem handle exists.
711 1.2 riastrad * Closing the handle will clean out the cache anyway, so we don't leak.
712 1.2 riastrad */
713 1.2 riastrad if (ret < 0) {
714 1.2 riastrad goto fail_put_dmabuf;
715 1.1 riastrad } else {
716 1.2 riastrad *prime_fd = ret;
717 1.2 riastrad ret = 0;
718 1.2 riastrad }
719 1.2 riastrad
720 1.2 riastrad goto out;
721 1.2 riastrad
722 1.2 riastrad fail_put_dmabuf:
723 1.2 riastrad dma_buf_put(dmabuf);
724 1.2 riastrad out:
725 1.10 riastrad drm_gem_object_put_unlocked(obj);
726 1.2 riastrad out_unlock:
727 1.2 riastrad mutex_unlock(&file_priv->prime.lock);
728 1.2 riastrad
729 1.2 riastrad return ret;
730 1.2 riastrad }
731 1.2 riastrad EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
732 1.2 riastrad
733 1.10 riastrad int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
734 1.10 riastrad struct drm_file *file_priv)
735 1.10 riastrad {
736 1.10 riastrad struct drm_prime_handle *args = data;
737 1.10 riastrad
738 1.10 riastrad if (!dev->driver->prime_handle_to_fd)
739 1.10 riastrad return -ENOSYS;
740 1.10 riastrad
741 1.10 riastrad /* check flags are valid */
742 1.10 riastrad if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
743 1.10 riastrad return -EINVAL;
744 1.10 riastrad
745 1.10 riastrad return dev->driver->prime_handle_to_fd(dev, file_priv,
746 1.10 riastrad args->handle, args->flags, &args->fd);
747 1.10 riastrad }
748 1.10 riastrad
749 1.10 riastrad /**
750 1.10 riastrad * DOC: PRIME Helpers
751 1.10 riastrad *
752 1.10 riastrad * Drivers can implement &drm_gem_object_funcs.export and
753 1.10 riastrad * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper
754 1.10 riastrad * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions
755 1.10 riastrad * implement dma-buf support in terms of some lower-level helpers, which are
756 1.10 riastrad * again exported for drivers to use individually:
757 1.10 riastrad *
758 1.10 riastrad * Exporting buffers
759 1.10 riastrad * ~~~~~~~~~~~~~~~~~
760 1.10 riastrad *
761 1.10 riastrad * Optional pinning of buffers is handled at dma-buf attach and detach time in
762 1.10 riastrad * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
763 1.10 riastrad * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
764 1.10 riastrad * &drm_gem_object_funcs.get_sg_table.
765 1.10 riastrad *
766 1.10 riastrad * For kernel-internal access there's drm_gem_dmabuf_vmap() and
767 1.10 riastrad * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
768 1.10 riastrad * drm_gem_dmabuf_mmap().
769 1.10 riastrad *
770 1.10 riastrad * Note that these export helpers can only be used if the underlying backing
771 1.10 riastrad * storage is fully coherent and either permanently pinned, or it is safe to pin
772 1.10 riastrad * it indefinitely.
773 1.10 riastrad *
774 1.10 riastrad * FIXME: The underlying helper functions are named rather inconsistently.
775 1.10 riastrad *
776 1.10 riastrad * Exporting buffers
777 1.10 riastrad * ~~~~~~~~~~~~~~~~~
778 1.10 riastrad *
779 1.10 riastrad * Importing dma-bufs using drm_gem_prime_import() relies on
780 1.10 riastrad * &drm_driver.gem_prime_import_sg_table.
781 1.10 riastrad *
782 1.10 riastrad * Note that similarly to the export helpers this permanently pins the
783 1.10 riastrad * underlying backing storage. Which is ok for scanout, but is not the best
784 1.10 riastrad * option for sharing lots of buffers for rendering.
785 1.10 riastrad */
786 1.10 riastrad
787 1.2 riastrad /**
788 1.10 riastrad * drm_gem_map_attach - dma_buf attach implementation for GEM
789 1.10 riastrad * @dma_buf: buffer to attach device to
790 1.10 riastrad * @attach: buffer attachment data
791 1.10 riastrad *
792 1.10 riastrad * Calls &drm_gem_object_funcs.pin for device specific handling. This can be
793 1.10 riastrad * used as the &dma_buf_ops.attach callback. Must be used together with
794 1.10 riastrad * drm_gem_map_detach().
795 1.2 riastrad *
796 1.10 riastrad * Returns 0 on success, negative error code on failure.
797 1.10 riastrad */
798 1.10 riastrad int drm_gem_map_attach(struct dma_buf *dma_buf,
799 1.10 riastrad struct dma_buf_attachment *attach)
800 1.10 riastrad {
801 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
802 1.10 riastrad
803 1.10 riastrad return drm_gem_pin(obj);
804 1.10 riastrad }
805 1.10 riastrad EXPORT_SYMBOL(drm_gem_map_attach);
806 1.10 riastrad
807 1.10 riastrad /**
808 1.10 riastrad * drm_gem_map_detach - dma_buf detach implementation for GEM
809 1.10 riastrad * @dma_buf: buffer to detach from
810 1.10 riastrad * @attach: attachment to be detached
811 1.10 riastrad *
812 1.10 riastrad * Calls &drm_gem_object_funcs.pin for device specific handling. Cleans up
813 1.10 riastrad * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the
814 1.10 riastrad * &dma_buf_ops.detach callback.
815 1.2 riastrad */
816 1.10 riastrad void drm_gem_map_detach(struct dma_buf *dma_buf,
817 1.10 riastrad struct dma_buf_attachment *attach)
818 1.2 riastrad {
819 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
820 1.2 riastrad
821 1.10 riastrad drm_gem_unpin(obj);
822 1.10 riastrad }
823 1.10 riastrad EXPORT_SYMBOL(drm_gem_map_detach);
824 1.10 riastrad
825 1.10 riastrad /**
826 1.10 riastrad * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
827 1.10 riastrad * @attach: attachment whose scatterlist is to be returned
828 1.10 riastrad * @dir: direction of DMA transfer
829 1.10 riastrad *
830 1.10 riastrad * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
831 1.10 riastrad * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
832 1.10 riastrad * with drm_gem_unmap_dma_buf().
833 1.10 riastrad *
834 1.10 riastrad * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
835 1.10 riastrad * on error. May return -EINTR if it is interrupted by a signal.
836 1.10 riastrad */
837 1.10 riastrad struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
838 1.10 riastrad enum dma_data_direction dir)
839 1.10 riastrad {
840 1.10 riastrad struct drm_gem_object *obj = attach->dmabuf->priv;
841 1.10 riastrad struct sg_table *sgt;
842 1.2 riastrad
843 1.10 riastrad if (WARN_ON(dir == DMA_NONE))
844 1.2 riastrad return ERR_PTR(-EINVAL);
845 1.2 riastrad
846 1.10 riastrad if (obj->funcs)
847 1.10 riastrad sgt = obj->funcs->get_sg_table(obj);
848 1.10 riastrad else
849 1.10 riastrad sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
850 1.2 riastrad
851 1.10 riastrad if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
852 1.10 riastrad DMA_ATTR_SKIP_CPU_SYNC)) {
853 1.10 riastrad sg_free_table(sgt);
854 1.10 riastrad kfree(sgt);
855 1.10 riastrad sgt = ERR_PTR(-ENOMEM);
856 1.10 riastrad }
857 1.2 riastrad
858 1.10 riastrad return sgt;
859 1.10 riastrad }
860 1.10 riastrad EXPORT_SYMBOL(drm_gem_map_dma_buf);
861 1.2 riastrad
862 1.10 riastrad /**
863 1.10 riastrad * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
864 1.10 riastrad * @attach: attachment to unmap buffer from
865 1.10 riastrad * @sgt: scatterlist info of the buffer to unmap
866 1.10 riastrad * @dir: direction of DMA transfer
867 1.10 riastrad *
868 1.10 riastrad * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
869 1.10 riastrad */
870 1.10 riastrad void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
871 1.10 riastrad struct sg_table *sgt,
872 1.10 riastrad enum dma_data_direction dir)
873 1.10 riastrad {
874 1.10 riastrad if (!sgt)
875 1.10 riastrad return;
876 1.1 riastrad
877 1.10 riastrad dma_unmap_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
878 1.10 riastrad DMA_ATTR_SKIP_CPU_SYNC);
879 1.10 riastrad sg_free_table(sgt);
880 1.10 riastrad kfree(sgt);
881 1.10 riastrad }
882 1.10 riastrad EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
883 1.2 riastrad
884 1.10 riastrad /**
885 1.10 riastrad * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
886 1.10 riastrad * @dma_buf: buffer to be mapped
887 1.10 riastrad *
888 1.10 riastrad * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
889 1.10 riastrad * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling.
890 1.10 riastrad *
891 1.10 riastrad * Returns the kernel virtual address or NULL on failure.
892 1.10 riastrad */
893 1.10 riastrad void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
894 1.10 riastrad {
895 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
896 1.10 riastrad void *vaddr;
897 1.2 riastrad
898 1.10 riastrad vaddr = drm_gem_vmap(obj);
899 1.10 riastrad if (IS_ERR(vaddr))
900 1.10 riastrad vaddr = NULL;
901 1.2 riastrad
902 1.10 riastrad return vaddr;
903 1.1 riastrad }
904 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
905 1.1 riastrad
906 1.2 riastrad /**
907 1.10 riastrad * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
908 1.10 riastrad * @dma_buf: buffer to be unmapped
909 1.10 riastrad * @vaddr: the virtual address of the buffer
910 1.2 riastrad *
911 1.10 riastrad * Releases a kernel virtual mapping. This can be used as the
912 1.10 riastrad * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling.
913 1.2 riastrad */
914 1.10 riastrad void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
915 1.1 riastrad {
916 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
917 1.1 riastrad
918 1.10 riastrad drm_gem_vunmap(obj, vaddr);
919 1.10 riastrad }
920 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
921 1.1 riastrad
922 1.10 riastrad /**
923 1.10 riastrad * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
924 1.10 riastrad * @obj: GEM object
925 1.10 riastrad * @vma: Virtual address range
926 1.10 riastrad *
927 1.10 riastrad * This function sets up a userspace mapping for PRIME exported buffers using
928 1.10 riastrad * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
929 1.10 riastrad * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
930 1.10 riastrad * called to set up the mapping.
931 1.10 riastrad *
932 1.10 riastrad * Drivers can use this as their &drm_driver.gem_prime_mmap callback.
933 1.10 riastrad */
934 1.10 riastrad int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
935 1.10 riastrad {
936 1.10 riastrad struct drm_file *priv;
937 1.10 riastrad struct file *fil;
938 1.10 riastrad int ret;
939 1.1 riastrad
940 1.10 riastrad /* Add the fake offset */
941 1.10 riastrad vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
942 1.1 riastrad
943 1.10 riastrad if (obj->funcs && obj->funcs->mmap) {
944 1.10 riastrad ret = obj->funcs->mmap(obj, vma);
945 1.10 riastrad if (ret)
946 1.10 riastrad return ret;
947 1.10 riastrad vma->vm_private_data = obj;
948 1.10 riastrad drm_gem_object_get(obj);
949 1.10 riastrad return 0;
950 1.2 riastrad }
951 1.2 riastrad
952 1.10 riastrad priv = kzalloc(sizeof(*priv), GFP_KERNEL);
953 1.10 riastrad fil = kzalloc(sizeof(*fil), GFP_KERNEL);
954 1.10 riastrad if (!priv || !fil) {
955 1.10 riastrad ret = -ENOMEM;
956 1.10 riastrad goto out;
957 1.1 riastrad }
958 1.1 riastrad
959 1.10 riastrad /* Used by drm_gem_mmap() to lookup the GEM object */
960 1.10 riastrad priv->minor = obj->dev->primary;
961 1.10 riastrad fil->private_data = priv;
962 1.1 riastrad
963 1.10 riastrad ret = drm_vma_node_allow(&obj->vma_node, priv);
964 1.1 riastrad if (ret)
965 1.10 riastrad goto out;
966 1.1 riastrad
967 1.10 riastrad ret = obj->dev->driver->fops->mmap(fil, vma);
968 1.2 riastrad
969 1.10 riastrad drm_vma_node_revoke(&obj->vma_node, priv);
970 1.10 riastrad out:
971 1.10 riastrad kfree(priv);
972 1.10 riastrad kfree(fil);
973 1.2 riastrad
974 1.1 riastrad return ret;
975 1.1 riastrad }
976 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_mmap);
977 1.1 riastrad
978 1.10 riastrad /**
979 1.10 riastrad * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
980 1.10 riastrad * @dma_buf: buffer to be mapped
981 1.10 riastrad * @vma: virtual address range
982 1.10 riastrad *
983 1.10 riastrad * Provides memory mapping for the buffer. This can be used as the
984 1.10 riastrad * &dma_buf_ops.mmap callback. It just forwards to &drm_driver.gem_prime_mmap,
985 1.10 riastrad * which should be set to drm_gem_prime_mmap().
986 1.10 riastrad *
987 1.10 riastrad * FIXME: There's really no point to this wrapper, drivers which need anything
988 1.10 riastrad * else but drm_gem_prime_mmap can roll their own &dma_buf_ops.mmap callback.
989 1.10 riastrad *
990 1.10 riastrad * Returns 0 on success or a negative error code on failure.
991 1.10 riastrad */
992 1.10 riastrad #ifdef __NetBSD__
993 1.13 riastrad int
994 1.10 riastrad drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, off_t *offp, size_t size,
995 1.10 riastrad int prot, int *flagsp, int *advicep, struct uvm_object **uobjp,
996 1.10 riastrad int *maxprotp)
997 1.10 riastrad #else
998 1.10 riastrad int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
999 1.10 riastrad #endif
1000 1.1 riastrad {
1001 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
1002 1.10 riastrad struct drm_device *dev = obj->dev;
1003 1.1 riastrad
1004 1.10 riastrad if (!dev->driver->gem_prime_mmap)
1005 1.1 riastrad return -ENOSYS;
1006 1.1 riastrad
1007 1.10 riastrad #ifdef __NetBSD__
1008 1.10 riastrad return dev->driver->gem_prime_mmap(obj, offp, size, prot, flagsp,
1009 1.10 riastrad advicep, uobjp, maxprotp);
1010 1.10 riastrad #else
1011 1.10 riastrad return dev->driver->gem_prime_mmap(obj, vma);
1012 1.10 riastrad #endif
1013 1.1 riastrad }
1014 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
1015 1.1 riastrad
1016 1.10 riastrad static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
1017 1.10 riastrad .cache_sgt_mapping = true,
1018 1.10 riastrad .attach = drm_gem_map_attach,
1019 1.10 riastrad .detach = drm_gem_map_detach,
1020 1.10 riastrad .map_dma_buf = drm_gem_map_dma_buf,
1021 1.10 riastrad .unmap_dma_buf = drm_gem_unmap_dma_buf,
1022 1.10 riastrad .release = drm_gem_dmabuf_release,
1023 1.10 riastrad .mmap = drm_gem_dmabuf_mmap,
1024 1.10 riastrad .vmap = drm_gem_dmabuf_vmap,
1025 1.10 riastrad .vunmap = drm_gem_dmabuf_vunmap,
1026 1.10 riastrad };
1027 1.1 riastrad
1028 1.2 riastrad /**
1029 1.2 riastrad * drm_prime_pages_to_sg - converts a page array into an sg list
1030 1.2 riastrad * @pages: pointer to the array of page pointers to convert
1031 1.2 riastrad * @nr_pages: length of the page vector
1032 1.1 riastrad *
1033 1.2 riastrad * This helper creates an sg table object from a set of pages
1034 1.1 riastrad * the driver is responsible for mapping the pages into the
1035 1.2 riastrad * importers address space for use with dma_buf itself.
1036 1.10 riastrad *
1037 1.10 riastrad * This is useful for implementing &drm_gem_object_funcs.get_sg_table.
1038 1.1 riastrad */
1039 1.2 riastrad struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
1040 1.1 riastrad {
1041 1.1 riastrad struct sg_table *sg = NULL;
1042 1.1 riastrad int ret;
1043 1.1 riastrad
1044 1.1 riastrad sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
1045 1.2 riastrad if (!sg) {
1046 1.2 riastrad ret = -ENOMEM;
1047 1.1 riastrad goto out;
1048 1.2 riastrad }
1049 1.1 riastrad
1050 1.2 riastrad ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
1051 1.2 riastrad nr_pages << PAGE_SHIFT, GFP_KERNEL);
1052 1.1 riastrad if (ret)
1053 1.1 riastrad goto out;
1054 1.1 riastrad
1055 1.1 riastrad return sg;
1056 1.1 riastrad out:
1057 1.1 riastrad kfree(sg);
1058 1.2 riastrad return ERR_PTR(ret);
1059 1.1 riastrad }
1060 1.1 riastrad EXPORT_SYMBOL(drm_prime_pages_to_sg);
1061 1.1 riastrad
1062 1.10 riastrad /**
1063 1.10 riastrad * drm_gem_prime_export - helper library implementation of the export callback
1064 1.10 riastrad * @obj: GEM object to export
1065 1.10 riastrad * @flags: flags like DRM_CLOEXEC and DRM_RDWR
1066 1.10 riastrad *
1067 1.10 riastrad * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers
1068 1.10 riastrad * using the PRIME helpers. It is used as the default in
1069 1.10 riastrad * drm_gem_prime_handle_to_fd().
1070 1.10 riastrad */
1071 1.10 riastrad struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
1072 1.10 riastrad int flags)
1073 1.10 riastrad {
1074 1.10 riastrad struct drm_device *dev = obj->dev;
1075 1.10 riastrad struct dma_buf_export_info exp_info = {
1076 1.10 riastrad #ifndef __NetBSD__
1077 1.10 riastrad .exp_name = KBUILD_MODNAME, /* white lie for debug */
1078 1.10 riastrad .owner = dev->driver->fops->owner,
1079 1.10 riastrad #endif
1080 1.10 riastrad .ops = &drm_gem_prime_dmabuf_ops,
1081 1.10 riastrad .size = obj->size,
1082 1.10 riastrad .flags = flags,
1083 1.10 riastrad .priv = obj,
1084 1.10 riastrad .resv = obj->resv,
1085 1.10 riastrad };
1086 1.10 riastrad
1087 1.10 riastrad return drm_gem_dmabuf_export(dev, &exp_info);
1088 1.10 riastrad }
1089 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_export);
1090 1.10 riastrad
1091 1.10 riastrad /**
1092 1.10 riastrad * drm_gem_prime_import_dev - core implementation of the import callback
1093 1.10 riastrad * @dev: drm_device to import into
1094 1.10 riastrad * @dma_buf: dma-buf object to import
1095 1.10 riastrad * @attach_dev: struct device to dma_buf attach
1096 1.10 riastrad *
1097 1.10 riastrad * This is the core of drm_gem_prime_import(). It's designed to be called by
1098 1.10 riastrad * drivers who want to use a different device structure than &drm_device.dev for
1099 1.10 riastrad * attaching via dma_buf. This function calls
1100 1.10 riastrad * &drm_driver.gem_prime_import_sg_table internally.
1101 1.10 riastrad *
1102 1.10 riastrad * Drivers must arrange to call drm_prime_gem_destroy() from their
1103 1.10 riastrad * &drm_gem_object_funcs.free hook when using this function.
1104 1.10 riastrad */
1105 1.10 riastrad struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
1106 1.10 riastrad struct dma_buf *dma_buf,
1107 1.10 riastrad struct device *attach_dev)
1108 1.10 riastrad {
1109 1.10 riastrad struct dma_buf_attachment *attach;
1110 1.10 riastrad struct sg_table *sgt;
1111 1.10 riastrad struct drm_gem_object *obj;
1112 1.10 riastrad int ret;
1113 1.10 riastrad
1114 1.10 riastrad if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
1115 1.10 riastrad obj = dma_buf->priv;
1116 1.10 riastrad if (obj->dev == dev) {
1117 1.10 riastrad /*
1118 1.10 riastrad * Importing dmabuf exported from out own gem increases
1119 1.10 riastrad * refcount on gem itself instead of f_count of dmabuf.
1120 1.10 riastrad */
1121 1.10 riastrad drm_gem_object_get(obj);
1122 1.10 riastrad return obj;
1123 1.10 riastrad }
1124 1.10 riastrad }
1125 1.10 riastrad
1126 1.10 riastrad if (!dev->driver->gem_prime_import_sg_table)
1127 1.10 riastrad return ERR_PTR(-EINVAL);
1128 1.10 riastrad
1129 1.10 riastrad attach = dma_buf_attach(dma_buf, attach_dev);
1130 1.10 riastrad if (IS_ERR(attach))
1131 1.10 riastrad return ERR_CAST(attach);
1132 1.10 riastrad
1133 1.10 riastrad get_dma_buf(dma_buf);
1134 1.10 riastrad
1135 1.10 riastrad sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
1136 1.10 riastrad if (IS_ERR(sgt)) {
1137 1.10 riastrad ret = PTR_ERR(sgt);
1138 1.10 riastrad goto fail_detach;
1139 1.10 riastrad }
1140 1.10 riastrad
1141 1.10 riastrad obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
1142 1.10 riastrad if (IS_ERR(obj)) {
1143 1.10 riastrad ret = PTR_ERR(obj);
1144 1.10 riastrad goto fail_unmap;
1145 1.10 riastrad }
1146 1.10 riastrad
1147 1.10 riastrad obj->import_attach = attach;
1148 1.10 riastrad obj->resv = dma_buf->resv;
1149 1.10 riastrad
1150 1.10 riastrad return obj;
1151 1.10 riastrad
1152 1.10 riastrad fail_unmap:
1153 1.10 riastrad dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
1154 1.10 riastrad fail_detach:
1155 1.10 riastrad dma_buf_detach(dma_buf, attach);
1156 1.10 riastrad dma_buf_put(dma_buf);
1157 1.10 riastrad
1158 1.10 riastrad return ERR_PTR(ret);
1159 1.10 riastrad }
1160 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_import_dev);
1161 1.10 riastrad
1162 1.10 riastrad /**
1163 1.10 riastrad * drm_gem_prime_import - helper library implementation of the import callback
1164 1.10 riastrad * @dev: drm_device to import into
1165 1.10 riastrad * @dma_buf: dma-buf object to import
1166 1.10 riastrad *
1167 1.10 riastrad * This is the implementation of the gem_prime_import functions for GEM drivers
1168 1.10 riastrad * using the PRIME helpers. Drivers can use this as their
1169 1.10 riastrad * &drm_driver.gem_prime_import implementation. It is used as the default
1170 1.10 riastrad * implementation in drm_gem_prime_fd_to_handle().
1171 1.10 riastrad *
1172 1.10 riastrad * Drivers must arrange to call drm_prime_gem_destroy() from their
1173 1.10 riastrad * &drm_gem_object_funcs.free hook when using this function.
1174 1.10 riastrad */
1175 1.10 riastrad struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1176 1.10 riastrad struct dma_buf *dma_buf)
1177 1.10 riastrad {
1178 1.10 riastrad return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
1179 1.10 riastrad }
1180 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_import);
1181 1.10 riastrad
1182 1.12 riastrad #ifdef __NetBSD__
1183 1.10 riastrad /**
1184 1.3 riastrad
1185 1.3 riastrad struct sg_table *
1186 1.5 riastrad drm_prime_bus_dmamem_to_sg(bus_dma_tag_t dmat, const bus_dma_segment_t *segs,
1187 1.5 riastrad int nsegs)
1188 1.4 riastrad {
1189 1.4 riastrad struct sg_table *sg;
1190 1.4 riastrad int ret;
1191 1.4 riastrad
1192 1.4 riastrad sg = kmalloc(sizeof(*sg), GFP_KERNEL);
1193 1.4 riastrad if (sg == NULL) {
1194 1.4 riastrad ret = -ENOMEM;
1195 1.4 riastrad goto out;
1196 1.4 riastrad }
1197 1.4 riastrad
1198 1.5 riastrad ret = sg_alloc_table_from_bus_dmamem(sg, dmat, segs, nsegs,
1199 1.5 riastrad GFP_KERNEL);
1200 1.4 riastrad if (ret)
1201 1.4 riastrad goto out;
1202 1.4 riastrad
1203 1.4 riastrad return sg;
1204 1.4 riastrad out:
1205 1.4 riastrad kfree(sg);
1206 1.4 riastrad return ERR_PTR(ret);
1207 1.4 riastrad }
1208 1.4 riastrad
1209 1.4 riastrad struct sg_table *
1210 1.3 riastrad drm_prime_pglist_to_sg(struct pglist *pglist, unsigned npages)
1211 1.3 riastrad {
1212 1.3 riastrad struct sg_table *sg;
1213 1.3 riastrad int ret;
1214 1.3 riastrad
1215 1.3 riastrad sg = kmalloc(sizeof(*sg), GFP_KERNEL);
1216 1.3 riastrad if (sg == NULL) {
1217 1.3 riastrad ret = -ENOMEM;
1218 1.3 riastrad goto out;
1219 1.3 riastrad }
1220 1.3 riastrad
1221 1.3 riastrad ret = sg_alloc_table_from_pglist(sg, pglist, 0, npages << PAGE_SHIFT,
1222 1.3 riastrad npages, GFP_KERNEL);
1223 1.3 riastrad if (ret)
1224 1.3 riastrad goto out;
1225 1.3 riastrad
1226 1.3 riastrad return sg;
1227 1.3 riastrad
1228 1.3 riastrad out:
1229 1.3 riastrad kfree(sg);
1230 1.3 riastrad return ERR_PTR(ret);
1231 1.3 riastrad }
1232 1.3 riastrad
1233 1.4 riastrad bus_size_t
1234 1.4 riastrad drm_prime_sg_size(struct sg_table *sg)
1235 1.4 riastrad {
1236 1.4 riastrad
1237 1.5 riastrad return sg->sgt_npgs << PAGE_SHIFT;
1238 1.4 riastrad }
1239 1.4 riastrad
1240 1.3 riastrad void
1241 1.3 riastrad drm_prime_sg_free(struct sg_table *sg)
1242 1.3 riastrad {
1243 1.3 riastrad
1244 1.3 riastrad sg_free_table(sg);
1245 1.3 riastrad kfree(sg);
1246 1.3 riastrad }
1247 1.3 riastrad
1248 1.3 riastrad int
1249 1.5 riastrad drm_prime_sg_to_bus_dmamem(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
1250 1.5 riastrad int nsegs, int *rsegs, const struct sg_table *sgt)
1251 1.3 riastrad {
1252 1.3 riastrad
1253 1.3 riastrad /* XXX errno NetBSD->Linux */
1254 1.5 riastrad return -bus_dmamem_import_pages(dmat, segs, nsegs, rsegs, sgt->sgt_pgs,
1255 1.5 riastrad sgt->sgt_npgs);
1256 1.3 riastrad }
1257 1.3 riastrad
1258 1.4 riastrad int
1259 1.5 riastrad drm_prime_bus_dmamap_load_sgt(bus_dma_tag_t dmat, bus_dmamap_t map,
1260 1.5 riastrad struct sg_table *sgt)
1261 1.4 riastrad {
1262 1.5 riastrad bus_dma_segment_t *segs;
1263 1.5 riastrad bus_size_t size = drm_prime_sg_size(sgt);
1264 1.5 riastrad int nsegs = sgt->sgt_npgs;
1265 1.5 riastrad int ret;
1266 1.5 riastrad
1267 1.5 riastrad segs = kcalloc(sgt->sgt_npgs, sizeof(segs[0]), GFP_KERNEL);
1268 1.5 riastrad if (segs == NULL) {
1269 1.5 riastrad ret = -ENOMEM;
1270 1.5 riastrad goto out0;
1271 1.5 riastrad }
1272 1.5 riastrad
1273 1.5 riastrad ret = drm_prime_sg_to_bus_dmamem(dmat, segs, nsegs, &nsegs, sgt);
1274 1.5 riastrad if (ret)
1275 1.5 riastrad goto out1;
1276 1.5 riastrad KASSERT(nsegs <= sgt->sgt_npgs);
1277 1.5 riastrad
1278 1.5 riastrad /* XXX errno NetBSD->Linux */
1279 1.5 riastrad ret = -bus_dmamap_load_raw(dmat, map, segs, nsegs, size,
1280 1.5 riastrad BUS_DMA_NOWAIT);
1281 1.5 riastrad if (ret)
1282 1.5 riastrad goto out1;
1283 1.4 riastrad
1284 1.5 riastrad out1: kfree(segs);
1285 1.5 riastrad out0: return ret;
1286 1.4 riastrad }
1287 1.4 riastrad
1288 1.7 riastrad bool
1289 1.7 riastrad drm_prime_sg_importable(bus_dma_tag_t dmat, struct sg_table *sgt)
1290 1.7 riastrad {
1291 1.7 riastrad unsigned i;
1292 1.7 riastrad
1293 1.7 riastrad for (i = 0; i < sgt->sgt_npgs; i++) {
1294 1.7 riastrad if (bus_dmatag_bounces_paddr(dmat, sgt->sgt_pgs[i]))
1295 1.7 riastrad return false;
1296 1.7 riastrad }
1297 1.7 riastrad return true;
1298 1.7 riastrad }
1299 1.7 riastrad
1300 1.3 riastrad #else /* !__NetBSD__ */
1301 1.3 riastrad
1302 1.2 riastrad /**
1303 1.2 riastrad * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
1304 1.2 riastrad * @sgt: scatter-gather table to convert
1305 1.10 riastrad * @pages: optional array of page pointers to store the page array in
1306 1.2 riastrad * @addrs: optional array to store the dma bus address of each page
1307 1.10 riastrad * @max_entries: size of both the passed-in arrays
1308 1.2 riastrad *
1309 1.2 riastrad * Exports an sg table into an array of pages and addresses. This is currently
1310 1.2 riastrad * required by the TTM driver in order to do correct fault handling.
1311 1.10 riastrad *
1312 1.10 riastrad * Drivers can use this in their &drm_driver.gem_prime_import_sg_table
1313 1.10 riastrad * implementation.
1314 1.2 riastrad */
1315 1.1 riastrad int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1316 1.10 riastrad dma_addr_t *addrs, int max_entries)
1317 1.1 riastrad {
1318 1.1 riastrad unsigned count;
1319 1.1 riastrad struct scatterlist *sg;
1320 1.1 riastrad struct page *page;
1321 1.10 riastrad u32 len, index;
1322 1.1 riastrad dma_addr_t addr;
1323 1.1 riastrad
1324 1.10 riastrad index = 0;
1325 1.1 riastrad for_each_sg(sgt->sgl, sg, sgt->nents, count) {
1326 1.1 riastrad len = sg->length;
1327 1.1 riastrad page = sg_page(sg);
1328 1.1 riastrad addr = sg_dma_address(sg);
1329 1.1 riastrad
1330 1.1 riastrad while (len > 0) {
1331 1.10 riastrad if (WARN_ON(index >= max_entries))
1332 1.1 riastrad return -1;
1333 1.10 riastrad if (pages)
1334 1.10 riastrad pages[index] = page;
1335 1.1 riastrad if (addrs)
1336 1.10 riastrad addrs[index] = addr;
1337 1.1 riastrad
1338 1.1 riastrad page++;
1339 1.1 riastrad addr += PAGE_SIZE;
1340 1.1 riastrad len -= PAGE_SIZE;
1341 1.10 riastrad index++;
1342 1.1 riastrad }
1343 1.1 riastrad }
1344 1.1 riastrad return 0;
1345 1.1 riastrad }
1346 1.1 riastrad EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
1347 1.2 riastrad
1348 1.3 riastrad #endif /* __NetBSD__ */
1349 1.3 riastrad
1350 1.2 riastrad /**
1351 1.2 riastrad * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
1352 1.2 riastrad * @obj: GEM object which was created from a dma-buf
1353 1.2 riastrad * @sg: the sg-table which was pinned at import time
1354 1.2 riastrad *
1355 1.2 riastrad * This is the cleanup functions which GEM drivers need to call when they use
1356 1.10 riastrad * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs.
1357 1.2 riastrad */
1358 1.1 riastrad void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
1359 1.1 riastrad {
1360 1.1 riastrad struct dma_buf_attachment *attach;
1361 1.1 riastrad struct dma_buf *dma_buf;
1362 1.1 riastrad attach = obj->import_attach;
1363 1.1 riastrad if (sg)
1364 1.1 riastrad dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
1365 1.1 riastrad dma_buf = attach->dmabuf;
1366 1.1 riastrad dma_buf_detach(attach->dmabuf, attach);
1367 1.1 riastrad /* remove the reference */
1368 1.1 riastrad dma_buf_put(dma_buf);
1369 1.1 riastrad }
1370 1.1 riastrad EXPORT_SYMBOL(drm_prime_gem_destroy);
1371