2 * Copyright © 2012 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Dave Airlie <airlied@redhat.com>
25 * Rob Clark <rob.clark@linaro.org>
29 #include <linux/export.h>
30 #include <linux/dma-buf.h>
34 * DMA-BUF/GEM Object references and lifetime overview:
36 * On the export the dma_buf holds a reference to the exporting GEM
37 * object. It takes this reference in handle_to_fd_ioctl, when it
38 * first calls .prime_export and stores the exporting GEM object in
39 * the dma_buf priv. This reference is released when the dma_buf
40 * object goes away in the driver .release function.
42 * On the import the importing GEM object holds a reference to the
43 * dma_buf (which in turn holds a ref to the exporting GEM object).
44 * It takes that reference in the fd_to_handle ioctl.
45 * It calls dma_buf_get, creates an attachment to it and stores the
46 * attachment in the GEM object. When this attachment is destroyed
47 * when the imported object is destroyed, we remove the attachment
48 * and drop the reference to the dma_buf.
50 * Thus the chain of references always flows in one direction
51 * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
53 * Self-importing: if userspace is using PRIME as a replacement for flink
54 * then it will get a fd->handle request for a GEM object that it created.
55 * Drivers should detect this situation and return back the gem object
56 * from the dma-buf private. Prime will do this automatically for drivers that
57 * use the drm_gem_prime_{import,export} helpers.
60 struct drm_prime_member
{
61 struct list_head entry
;
62 struct dma_buf
*dma_buf
;
66 struct drm_prime_attachment
{
68 enum dma_data_direction dir
;
71 static int drm_prime_add_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
, uint32_t handle
)
73 struct drm_prime_member
*member
;
75 member
= kmalloc(sizeof(*member
), GFP_KERNEL
);
80 member
->dma_buf
= dma_buf
;
81 member
->handle
= handle
;
82 list_add(&member
->entry
, &prime_fpriv
->head
);
86 static int drm_gem_map_attach(struct dma_buf
*dma_buf
,
87 struct device
*target_dev
,
88 struct dma_buf_attachment
*attach
)
90 struct drm_prime_attachment
*prime_attach
;
91 struct drm_gem_object
*obj
= dma_buf
->priv
;
92 struct drm_device
*dev
= obj
->dev
;
94 prime_attach
= kzalloc(sizeof(*prime_attach
), GFP_KERNEL
);
98 prime_attach
->dir
= DMA_NONE
;
99 attach
->priv
= prime_attach
;
101 if (!dev
->driver
->gem_prime_pin
)
104 return dev
->driver
->gem_prime_pin(obj
);
107 static void drm_gem_map_detach(struct dma_buf
*dma_buf
,
108 struct dma_buf_attachment
*attach
)
110 struct drm_prime_attachment
*prime_attach
= attach
->priv
;
111 struct drm_gem_object
*obj
= dma_buf
->priv
;
112 struct drm_device
*dev
= obj
->dev
;
113 struct sg_table
*sgt
;
115 if (dev
->driver
->gem_prime_unpin
)
116 dev
->driver
->gem_prime_unpin(obj
);
121 sgt
= prime_attach
->sgt
;
123 if (prime_attach
->dir
!= DMA_NONE
)
124 dma_unmap_sg(attach
->dev
, sgt
->sgl
, sgt
->nents
,
134 static void drm_prime_remove_buf_handle_locked(
135 struct drm_prime_file_private
*prime_fpriv
,
136 struct dma_buf
*dma_buf
)
138 struct drm_prime_member
*member
, *safe
;
140 list_for_each_entry_safe(member
, safe
, &prime_fpriv
->head
, entry
) {
141 if (member
->dma_buf
== dma_buf
) {
142 dma_buf_put(dma_buf
);
143 list_del(&member
->entry
);
149 static struct sg_table
*drm_gem_map_dma_buf(struct dma_buf_attachment
*attach
,
150 enum dma_data_direction dir
)
152 struct drm_prime_attachment
*prime_attach
= attach
->priv
;
153 struct drm_gem_object
*obj
= attach
->dmabuf
->priv
;
154 struct sg_table
*sgt
;
156 if (WARN_ON(dir
== DMA_NONE
|| !prime_attach
))
157 return ERR_PTR(-EINVAL
);
159 /* return the cached mapping when possible */
160 if (prime_attach
->dir
== dir
)
161 return prime_attach
->sgt
;
164 * two mappings with different directions for the same attachment are
167 if (WARN_ON(prime_attach
->dir
!= DMA_NONE
))
168 return ERR_PTR(-EBUSY
);
170 sgt
= obj
->dev
->driver
->gem_prime_get_sg_table(obj
);
173 if (!dma_map_sg(attach
->dev
, sgt
->sgl
, sgt
->nents
, dir
)) {
176 sgt
= ERR_PTR(-ENOMEM
);
178 prime_attach
->sgt
= sgt
;
179 prime_attach
->dir
= dir
;
186 static void drm_gem_unmap_dma_buf(struct dma_buf_attachment
*attach
,
187 struct sg_table
*sgt
, enum dma_data_direction dir
)
189 /* nothing to be done here */
192 void drm_gem_dmabuf_release(struct dma_buf
*dma_buf
)
194 struct drm_gem_object
*obj
= dma_buf
->priv
;
196 /* drop the reference on the export fd holds */
197 drm_gem_object_unreference_unlocked(obj
);
199 EXPORT_SYMBOL(drm_gem_dmabuf_release
);
201 static void *drm_gem_dmabuf_vmap(struct dma_buf
*dma_buf
)
203 struct drm_gem_object
*obj
= dma_buf
->priv
;
204 struct drm_device
*dev
= obj
->dev
;
206 return dev
->driver
->gem_prime_vmap(obj
);
209 static void drm_gem_dmabuf_vunmap(struct dma_buf
*dma_buf
, void *vaddr
)
211 struct drm_gem_object
*obj
= dma_buf
->priv
;
212 struct drm_device
*dev
= obj
->dev
;
214 dev
->driver
->gem_prime_vunmap(obj
, vaddr
);
217 static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf
*dma_buf
,
218 unsigned long page_num
)
223 static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf
*dma_buf
,
224 unsigned long page_num
, void *addr
)
228 static void *drm_gem_dmabuf_kmap(struct dma_buf
*dma_buf
,
229 unsigned long page_num
)
234 static void drm_gem_dmabuf_kunmap(struct dma_buf
*dma_buf
,
235 unsigned long page_num
, void *addr
)
240 static int drm_gem_dmabuf_mmap(struct dma_buf
*dma_buf
,
241 struct vm_area_struct
*vma
)
243 struct drm_gem_object
*obj
= dma_buf
->priv
;
244 struct drm_device
*dev
= obj
->dev
;
246 if (!dev
->driver
->gem_prime_mmap
)
249 return dev
->driver
->gem_prime_mmap(obj
, vma
);
252 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops
= {
253 .attach
= drm_gem_map_attach
,
254 .detach
= drm_gem_map_detach
,
255 .map_dma_buf
= drm_gem_map_dma_buf
,
256 .unmap_dma_buf
= drm_gem_unmap_dma_buf
,
257 .release
= drm_gem_dmabuf_release
,
258 .kmap
= drm_gem_dmabuf_kmap
,
259 .kmap_atomic
= drm_gem_dmabuf_kmap_atomic
,
260 .kunmap
= drm_gem_dmabuf_kunmap
,
261 .kunmap_atomic
= drm_gem_dmabuf_kunmap_atomic
,
262 .mmap
= drm_gem_dmabuf_mmap
,
263 .vmap
= drm_gem_dmabuf_vmap
,
264 .vunmap
= drm_gem_dmabuf_vunmap
,
270 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
271 * simpler APIs by using the helper functions @drm_gem_prime_export and
272 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
273 * five lower-level driver callbacks:
277 * - @gem_prime_pin (optional): prepare a GEM object for exporting
279 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
281 * - @gem_prime_vmap: vmap a buffer exported by your driver
283 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
287 * - @gem_prime_import_sg_table (import): produce a GEM object from another
288 * driver's scatter/gather table
291 struct dma_buf
*drm_gem_prime_export(struct drm_device
*dev
,
292 struct drm_gem_object
*obj
, int flags
)
294 return dma_buf_export(obj
, &drm_gem_prime_dmabuf_ops
, obj
->size
, flags
);
296 EXPORT_SYMBOL(drm_gem_prime_export
);
298 static struct dma_buf
*export_and_register_object(struct drm_device
*dev
,
299 struct drm_gem_object
*obj
,
302 struct dma_buf
*dmabuf
;
304 /* prevent races with concurrent gem_close. */
305 if (obj
->handle_count
== 0) {
306 dmabuf
= ERR_PTR(-ENOENT
);
310 dmabuf
= dev
->driver
->gem_prime_export(dev
, obj
, flags
);
311 if (IS_ERR(dmabuf
)) {
312 /* normally the created dma-buf takes ownership of the ref,
313 * but if that fails then drop the ref
319 * Note that callers do not need to clean up the export cache
320 * since the check for obj->handle_count guarantees that someone
323 obj
->dma_buf
= dmabuf
;
324 get_dma_buf(obj
->dma_buf
);
329 int drm_gem_prime_handle_to_fd(struct drm_device
*dev
,
330 struct drm_file
*file_priv
, uint32_t handle
, uint32_t flags
,
333 struct drm_gem_object
*obj
;
335 struct dma_buf
*dmabuf
;
337 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
341 /* re-export the original imported object */
342 if (obj
->import_attach
) {
343 dmabuf
= obj
->import_attach
->dmabuf
;
348 mutex_lock(&dev
->object_name_lock
);
350 get_dma_buf(obj
->dma_buf
);
351 dmabuf
= obj
->dma_buf
;
352 mutex_unlock(&dev
->object_name_lock
);
356 dmabuf
= export_and_register_object(dev
, obj
, flags
);
357 mutex_unlock(&dev
->object_name_lock
);
358 if (IS_ERR(dmabuf
)) {
359 /* normally the created dma-buf takes ownership of the ref,
360 * but if that fails then drop the ref
362 ret
= PTR_ERR(dmabuf
);
366 mutex_lock(&file_priv
->prime
.lock
);
367 /* if we've exported this buffer the cheat and add it to the import list
368 * so we get the correct handle back
370 ret
= drm_prime_add_buf_handle(&file_priv
->prime
,
373 goto fail_put_dmabuf
;
375 ret
= dma_buf_fd(dmabuf
, flags
);
380 mutex_unlock(&file_priv
->prime
.lock
);
384 ret
= dma_buf_fd(dmabuf
, flags
);
395 drm_prime_remove_buf_handle_locked(&file_priv
->prime
,
397 mutex_unlock(&file_priv
->prime
.lock
);
401 drm_gem_object_unreference_unlocked(obj
);
404 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd
);
406 struct drm_gem_object
*drm_gem_prime_import(struct drm_device
*dev
,
407 struct dma_buf
*dma_buf
)
409 struct dma_buf_attachment
*attach
;
410 struct sg_table
*sgt
;
411 struct drm_gem_object
*obj
;
414 if (!dev
->driver
->gem_prime_import_sg_table
)
415 return ERR_PTR(-EINVAL
);
417 if (dma_buf
->ops
== &drm_gem_prime_dmabuf_ops
) {
419 if (obj
->dev
== dev
) {
421 * Importing dmabuf exported from out own gem increases
422 * refcount on gem itself instead of f_count of dmabuf.
424 drm_gem_object_reference(obj
);
429 attach
= dma_buf_attach(dma_buf
, dev
->dev
);
431 return ERR_CAST(attach
);
433 get_dma_buf(dma_buf
);
435 sgt
= dma_buf_map_attachment(attach
, DMA_BIDIRECTIONAL
);
436 if (IS_ERR_OR_NULL(sgt
)) {
441 obj
= dev
->driver
->gem_prime_import_sg_table(dev
, dma_buf
->size
, sgt
);
447 obj
->import_attach
= attach
;
452 dma_buf_unmap_attachment(attach
, sgt
, DMA_BIDIRECTIONAL
);
454 dma_buf_detach(dma_buf
, attach
);
455 dma_buf_put(dma_buf
);
459 EXPORT_SYMBOL(drm_gem_prime_import
);
461 int drm_gem_prime_fd_to_handle(struct drm_device
*dev
,
462 struct drm_file
*file_priv
, int prime_fd
, uint32_t *handle
)
464 struct dma_buf
*dma_buf
;
465 struct drm_gem_object
*obj
;
468 dma_buf
= dma_buf_get(prime_fd
);
470 return PTR_ERR(dma_buf
);
472 mutex_lock(&file_priv
->prime
.lock
);
474 ret
= drm_prime_lookup_buf_handle(&file_priv
->prime
,
479 /* never seen this one, need to import */
480 mutex_lock(&dev
->object_name_lock
);
481 obj
= dev
->driver
->gem_prime_import(dev
, dma_buf
);
488 WARN_ON(obj
->dma_buf
!= dma_buf
);
490 obj
->dma_buf
= dma_buf
;
491 get_dma_buf(dma_buf
);
494 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
495 ret
= drm_gem_handle_create_tail(file_priv
, obj
, handle
);
496 drm_gem_object_unreference_unlocked(obj
);
500 ret
= drm_prime_add_buf_handle(&file_priv
->prime
,
505 mutex_unlock(&file_priv
->prime
.lock
);
507 dma_buf_put(dma_buf
);
512 /* hmm, if driver attached, we are relying on the free-object path
513 * to detach.. which seems ok..
515 drm_gem_handle_delete(file_priv
, *handle
);
517 mutex_lock(&dev
->object_name_lock
);
519 dma_buf_put(dma_buf
);
520 mutex_unlock(&file_priv
->prime
.lock
);
523 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle
);
525 int drm_prime_handle_to_fd_ioctl(struct drm_device
*dev
, void *data
,
526 struct drm_file
*file_priv
)
528 struct drm_prime_handle
*args
= data
;
531 if (!drm_core_check_feature(dev
, DRIVER_PRIME
))
534 if (!dev
->driver
->prime_handle_to_fd
)
537 /* check flags are valid */
538 if (args
->flags
& ~DRM_CLOEXEC
)
541 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
542 flags
= args
->flags
& DRM_CLOEXEC
;
544 return dev
->driver
->prime_handle_to_fd(dev
, file_priv
,
545 args
->handle
, flags
, &args
->fd
);
548 int drm_prime_fd_to_handle_ioctl(struct drm_device
*dev
, void *data
,
549 struct drm_file
*file_priv
)
551 struct drm_prime_handle
*args
= data
;
553 if (!drm_core_check_feature(dev
, DRIVER_PRIME
))
556 if (!dev
->driver
->prime_fd_to_handle
)
559 return dev
->driver
->prime_fd_to_handle(dev
, file_priv
,
560 args
->fd
, &args
->handle
);
564 * drm_prime_pages_to_sg
566 * this helper creates an sg table object from a set of pages
567 * the driver is responsible for mapping the pages into the
568 * importers address space
570 struct sg_table
*drm_prime_pages_to_sg(struct page
**pages
, int nr_pages
)
572 struct sg_table
*sg
= NULL
;
575 sg
= kmalloc(sizeof(struct sg_table
), GFP_KERNEL
);
581 ret
= sg_alloc_table_from_pages(sg
, pages
, nr_pages
, 0,
582 nr_pages
<< PAGE_SHIFT
, GFP_KERNEL
);
591 EXPORT_SYMBOL(drm_prime_pages_to_sg
);
593 /* export an sg table into an array of pages and addresses
594 this is currently required by the TTM driver in order to do correct fault
596 int drm_prime_sg_to_page_addr_arrays(struct sg_table
*sgt
, struct page
**pages
,
597 dma_addr_t
*addrs
, int max_pages
)
600 struct scatterlist
*sg
;
607 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, count
) {
611 addr
= sg_dma_address(sg
);
614 if (WARN_ON(pg_index
>= max_pages
))
616 pages
[pg_index
] = page
;
618 addrs
[pg_index
] = addr
;
628 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays
);
629 /* helper function to cleanup a GEM/prime object */
630 void drm_prime_gem_destroy(struct drm_gem_object
*obj
, struct sg_table
*sg
)
632 struct dma_buf_attachment
*attach
;
633 struct dma_buf
*dma_buf
;
634 attach
= obj
->import_attach
;
636 dma_buf_unmap_attachment(attach
, sg
, DMA_BIDIRECTIONAL
);
637 dma_buf
= attach
->dmabuf
;
638 dma_buf_detach(attach
->dmabuf
, attach
);
639 /* remove the reference */
640 dma_buf_put(dma_buf
);
642 EXPORT_SYMBOL(drm_prime_gem_destroy
);
644 void drm_prime_init_file_private(struct drm_prime_file_private
*prime_fpriv
)
646 INIT_LIST_HEAD(&prime_fpriv
->head
);
647 mutex_init(&prime_fpriv
->lock
);
649 EXPORT_SYMBOL(drm_prime_init_file_private
);
651 void drm_prime_destroy_file_private(struct drm_prime_file_private
*prime_fpriv
)
653 /* by now drm_gem_release should've made sure the list is empty */
654 WARN_ON(!list_empty(&prime_fpriv
->head
));
656 EXPORT_SYMBOL(drm_prime_destroy_file_private
);
658 int drm_prime_lookup_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
, uint32_t *handle
)
660 struct drm_prime_member
*member
;
662 list_for_each_entry(member
, &prime_fpriv
->head
, entry
) {
663 if (member
->dma_buf
== dma_buf
) {
664 *handle
= member
->handle
;
670 EXPORT_SYMBOL(drm_prime_lookup_buf_handle
);
672 void drm_prime_remove_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
)
674 mutex_lock(&prime_fpriv
->lock
);
675 drm_prime_remove_buf_handle_locked(prime_fpriv
, dma_buf
);
676 mutex_unlock(&prime_fpriv
->lock
);
678 EXPORT_SYMBOL(drm_prime_remove_buf_handle
);