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>
31 #include <linux/rbtree.h>
33 #include <drm/drm_gem.h>
35 #include "drm_internal.h"
38 * DMA-BUF/GEM Object references and lifetime overview:
40 * On the export the dma_buf holds a reference to the exporting GEM
41 * object. It takes this reference in handle_to_fd_ioctl, when it
42 * first calls .prime_export and stores the exporting GEM object in
43 * the dma_buf priv. This reference is released when the dma_buf
44 * object goes away in the driver .release function.
46 * On the import the importing GEM object holds a reference to the
47 * dma_buf (which in turn holds a ref to the exporting GEM object).
48 * It takes that reference in the fd_to_handle ioctl.
49 * It calls dma_buf_get, creates an attachment to it and stores the
50 * attachment in the GEM object. When this attachment is destroyed
51 * when the imported object is destroyed, we remove the attachment
52 * and drop the reference to the dma_buf.
54 * Thus the chain of references always flows in one direction
55 * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
57 * Self-importing: if userspace is using PRIME as a replacement for flink
58 * then it will get a fd->handle request for a GEM object that it created.
59 * Drivers should detect this situation and return back the gem object
60 * from the dma-buf private. Prime will do this automatically for drivers that
61 * use the drm_gem_prime_{import,export} helpers.
64 struct drm_prime_member {
65 struct dma_buf *dma_buf;
68 struct rb_node dmabuf_rb;
69 struct rb_node handle_rb;
72 struct drm_prime_attachment {
74 enum dma_data_direction dir;
78 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
79 struct dma_buf *dma_buf, uint32_t handle)
81 struct drm_prime_member *member;
82 struct rb_node **p, *rb;
84 member = kmalloc(sizeof(*member), GFP_KERNEL);
89 member->dma_buf = dma_buf;
90 member->handle = handle;
93 p = &prime_fpriv->dmabufs.rb_node;
95 struct drm_prime_member *pos;
98 pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
99 if (dma_buf > pos->dma_buf)
104 rb_link_node(&member->dmabuf_rb, rb, p);
105 rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
108 p = &prime_fpriv->handles.rb_node;
110 struct drm_prime_member *pos;
113 pos = rb_entry(rb, struct drm_prime_member, handle_rb);
114 if (handle > pos->handle)
119 rb_link_node(&member->handle_rb, rb, p);
120 rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
125 static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
130 rb = prime_fpriv->handles.rb_node;
132 struct drm_prime_member *member;
134 member = rb_entry(rb, struct drm_prime_member, handle_rb);
135 if (member->handle == handle)
136 return member->dma_buf;
137 else if (member->handle < handle)
146 static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
147 struct dma_buf *dma_buf,
152 rb = prime_fpriv->dmabufs.rb_node;
154 struct drm_prime_member *member;
156 member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
157 if (member->dma_buf == dma_buf) {
158 *handle = member->handle;
160 } else if (member->dma_buf < dma_buf) {
171 static int drm_gem_map_attach(struct dma_buf *dma_buf,
172 struct device *target_dev,
173 struct dma_buf_attachment *attach)
175 struct drm_prime_attachment *prime_attach;
176 struct drm_gem_object *obj = dma_buf->priv;
177 struct drm_device *dev = obj->dev;
179 prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
183 prime_attach->dir = DMA_NONE;
184 attach->priv = prime_attach;
186 if (!dev->driver->gem_prime_pin)
189 return dev->driver->gem_prime_pin(obj);
192 static void drm_gem_map_detach(struct dma_buf *dma_buf,
193 struct dma_buf_attachment *attach)
195 struct drm_prime_attachment *prime_attach = attach->priv;
196 struct drm_gem_object *obj = dma_buf->priv;
197 struct drm_device *dev = obj->dev;
198 struct sg_table *sgt;
200 if (dev->driver->gem_prime_unpin)
201 dev->driver->gem_prime_unpin(obj);
206 sgt = prime_attach->sgt;
208 if (prime_attach->dir != DMA_NONE)
209 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
220 void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
221 struct dma_buf *dma_buf)
225 rb = prime_fpriv->dmabufs.rb_node;
227 struct drm_prime_member *member;
229 member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
230 if (member->dma_buf == dma_buf) {
231 rb_erase(&member->handle_rb, &prime_fpriv->handles);
232 rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
234 dma_buf_put(dma_buf);
237 } else if (member->dma_buf < dma_buf) {
246 static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
247 enum dma_data_direction dir)
249 struct drm_prime_attachment *prime_attach = attach->priv;
250 struct drm_gem_object *obj = attach->dmabuf->priv;
251 struct sg_table *sgt;
253 if (WARN_ON(dir == DMA_NONE || !prime_attach))
254 return ERR_PTR(-EINVAL);
256 /* return the cached mapping when possible */
257 if (prime_attach->dir == dir)
258 return prime_attach->sgt;
261 * two mappings with different directions for the same attachment are
264 if (WARN_ON(prime_attach->dir != DMA_NONE))
265 return ERR_PTR(-EBUSY);
267 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
270 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
273 sgt = ERR_PTR(-ENOMEM);
275 prime_attach->sgt = sgt;
276 prime_attach->dir = dir;
283 static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
284 struct sg_table *sgt,
285 enum dma_data_direction dir)
287 /* nothing to be done here */
291 * drm_gem_dmabuf_export - dma_buf export implementation for GEM
292 * @dev: parent device for the exported dmabuf
293 * @exp_info: the export information used by dma_buf_export()
295 * This wraps dma_buf_export() for use by generic GEM drivers that are using
296 * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
297 * a reference to the drm_device which is released by drm_gem_dmabuf_release().
299 * Returns the new dmabuf.
301 struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
302 struct dma_buf_export_info *exp_info)
304 struct dma_buf *dma_buf;
306 dma_buf = dma_buf_export(exp_info);
307 if (!IS_ERR(dma_buf))
312 EXPORT_SYMBOL(drm_gem_dmabuf_export);
315 * drm_gem_dmabuf_release - dma_buf release implementation for GEM
316 * @dma_buf: buffer to be released
318 * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
319 * must use this in their dma_buf ops structure as the release callback.
320 * drm_gem_dmabuf_release() should be used in conjunction with
321 * drm_gem_dmabuf_export().
323 void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
325 struct drm_gem_object *obj = dma_buf->priv;
326 struct drm_device *dev = obj->dev;
328 /* drop the reference on the export fd holds */
329 drm_gem_object_unreference_unlocked(obj);
333 EXPORT_SYMBOL(drm_gem_dmabuf_release);
335 static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
337 struct drm_gem_object *obj = dma_buf->priv;
338 struct drm_device *dev = obj->dev;
340 return dev->driver->gem_prime_vmap(obj);
343 static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
345 struct drm_gem_object *obj = dma_buf->priv;
346 struct drm_device *dev = obj->dev;
348 dev->driver->gem_prime_vunmap(obj, vaddr);
351 static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
352 unsigned long page_num)
357 static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
358 unsigned long page_num, void *addr)
362 static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
363 unsigned long page_num)
368 static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
369 unsigned long page_num, void *addr)
374 static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
375 struct vm_area_struct *vma)
377 struct drm_gem_object *obj = dma_buf->priv;
378 struct drm_device *dev = obj->dev;
380 if (!dev->driver->gem_prime_mmap)
383 return dev->driver->gem_prime_mmap(obj, vma);
386 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
387 .attach = drm_gem_map_attach,
388 .detach = drm_gem_map_detach,
389 .map_dma_buf = drm_gem_map_dma_buf,
390 .unmap_dma_buf = drm_gem_unmap_dma_buf,
391 .release = drm_gem_dmabuf_release,
392 .kmap = drm_gem_dmabuf_kmap,
393 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
394 .kunmap = drm_gem_dmabuf_kunmap,
395 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
396 .mmap = drm_gem_dmabuf_mmap,
397 .vmap = drm_gem_dmabuf_vmap,
398 .vunmap = drm_gem_dmabuf_vunmap,
404 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
405 * simpler APIs by using the helper functions @drm_gem_prime_export and
406 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
407 * six lower-level driver callbacks:
411 * * @gem_prime_pin (optional): prepare a GEM object for exporting
412 * * @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
413 * * @gem_prime_vmap: vmap a buffer exported by your driver
414 * * @gem_prime_vunmap: vunmap a buffer exported by your driver
415 * * @gem_prime_mmap (optional): mmap a buffer exported by your driver
419 * * @gem_prime_import_sg_table (import): produce a GEM object from another
420 * driver's scatter/gather table
424 * drm_gem_prime_export - helper library implementation of the export callback
425 * @dev: drm_device to export from
426 * @obj: GEM object to export
427 * @flags: flags like DRM_CLOEXEC and DRM_RDWR
429 * This is the implementation of the gem_prime_export functions for GEM drivers
430 * using the PRIME helpers.
432 struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
433 struct drm_gem_object *obj,
436 struct dma_buf_export_info exp_info = {
438 .exp_name = KBUILD_MODNAME, /* white lie for debug */
439 .owner = dev->driver->fops->owner,
441 .ops = &drm_gem_prime_dmabuf_ops,
447 if (dev->driver->gem_prime_res_obj)
448 exp_info.resv = dev->driver->gem_prime_res_obj(obj);
450 return drm_gem_dmabuf_export(dev, &exp_info);
452 EXPORT_SYMBOL(drm_gem_prime_export);
455 static struct dma_buf *export_and_register_object(struct drm_device *dev,
456 struct drm_gem_object *obj,
459 struct dma_buf *dmabuf;
461 /* prevent races with concurrent gem_close. */
462 if (obj->handle_count == 0) {
463 dmabuf = ERR_PTR(-ENOENT);
467 dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
468 if (IS_ERR(dmabuf)) {
469 /* normally the created dma-buf takes ownership of the ref,
470 * but if that fails then drop the ref
476 * Note that callers do not need to clean up the export cache
477 * since the check for obj->handle_count guarantees that someone
480 obj->dma_buf = dmabuf;
481 get_dma_buf(obj->dma_buf);
482 /* Grab a new ref since the callers is now used by the dma-buf */
483 drm_gem_object_reference(obj);
490 * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
491 * @dev: dev to export the buffer from
492 * @file_priv: drm file-private structure
493 * @handle: buffer handle to export
494 * @flags: flags like DRM_CLOEXEC
495 * @prime_fd: pointer to storage for the fd id of the create dma-buf
497 * This is the PRIME export function which must be used mandatorily by GEM
498 * drivers to ensure correct lifetime management of the underlying GEM object.
499 * The actual exporting from GEM object to a dma-buf is done through the
500 * gem_prime_export driver callback.
502 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
503 struct drm_file *file_priv, uint32_t handle,
508 struct drm_gem_object *obj;
512 struct dma_buf *dmabuf;
514 mutex_lock(&file_priv->prime.lock);
515 obj = drm_gem_object_lookup(file_priv, handle);
523 dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
526 goto out_have_handle;
529 mutex_lock(&dev->object_name_lock);
530 /* re-export the original imported object */
531 if (obj->import_attach) {
532 dmabuf = obj->import_attach->dmabuf;
538 get_dma_buf(obj->dma_buf);
539 dmabuf = obj->dma_buf;
543 dmabuf = export_and_register_object(dev, obj, flags);
544 if (IS_ERR(dmabuf)) {
545 /* normally the created dma-buf takes ownership of the ref,
546 * but if that fails then drop the ref
548 ret = PTR_ERR(dmabuf);
549 mutex_unlock(&dev->object_name_lock);
555 * If we've exported this buffer then cheat and add it to the import list
556 * so we get the correct handle back. We must do this under the
557 * protection of dev->object_name_lock to ensure that a racing gem close
558 * ioctl doesn't miss to remove this buffer handle from the cache.
560 ret = drm_prime_add_buf_handle(&file_priv->prime,
562 mutex_unlock(&dev->object_name_lock);
564 goto fail_put_dmabuf;
567 ret = dma_buf_fd(dmabuf, flags);
569 * We must _not_ remove the buffer from the handle cache since the newly
570 * created dma buf is already linked in the global obj->dma_buf pointer,
571 * and that is invariant as long as a userspace gem handle exists.
572 * Closing the handle will clean out the cache anyway, so we don't leak.
575 goto fail_put_dmabuf;
586 drm_gem_object_unreference_unlocked(obj);
588 mutex_unlock(&file_priv->prime.lock);
593 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
596 * drm_gem_prime_import - helper library implementation of the import callback
597 * @dev: drm_device to import into
598 * @dma_buf: dma-buf object to import
600 * This is the implementation of the gem_prime_import functions for GEM drivers
601 * using the PRIME helpers.
603 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
604 struct dma_buf *dma_buf)
607 struct dma_buf_attachment *attach;
608 struct sg_table *sgt;
609 struct drm_gem_object *obj;
612 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
614 if (obj->dev == dev) {
616 * Importing dmabuf exported from out own gem increases
617 * refcount on gem itself instead of f_count of dmabuf.
619 drm_gem_object_reference(obj);
624 if (!dev->driver->gem_prime_import_sg_table)
626 return ERR_PTR(-EINVAL);
629 attach = dma_buf_attach(dma_buf, dev->dev);
631 return ERR_CAST(attach);
633 get_dma_buf(dma_buf);
635 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
641 obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
647 obj->import_attach = attach;
652 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
654 dma_buf_detach(dma_buf, attach);
655 dma_buf_put(dma_buf);
660 EXPORT_SYMBOL(drm_gem_prime_import);
663 * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
664 * @dev: dev to export the buffer from
665 * @file_priv: drm file-private structure
666 * @prime_fd: fd id of the dma-buf which should be imported
667 * @handle: pointer to storage for the handle of the imported buffer object
669 * This is the PRIME import function which must be used mandatorily by GEM
670 * drivers to ensure correct lifetime management of the underlying GEM object.
671 * The actual importing of GEM object from the dma-buf is done through the
672 * gem_import_export driver callback.
674 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
675 struct drm_file *file_priv, int prime_fd,
679 struct dma_buf *dma_buf;
680 struct drm_gem_object *obj;
683 dma_buf = dma_buf_get(prime_fd);
685 return PTR_ERR(dma_buf);
687 mutex_lock(&file_priv->prime.lock);
689 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
694 /* never seen this one, need to import */
695 mutex_lock(&dev->object_name_lock);
696 obj = dev->driver->gem_prime_import(dev, dma_buf);
703 WARN_ON(obj->dma_buf != dma_buf);
705 obj->dma_buf = dma_buf;
706 get_dma_buf(dma_buf);
709 /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
710 ret = drm_gem_handle_create_tail(file_priv, obj, handle);
711 drm_gem_object_unreference_unlocked(obj);
715 ret = drm_prime_add_buf_handle(&file_priv->prime,
717 mutex_unlock(&file_priv->prime.lock);
721 dma_buf_put(dma_buf);
726 /* hmm, if driver attached, we are relying on the free-object path
727 * to detach.. which seems ok..
729 drm_gem_handle_delete(file_priv, *handle);
730 dma_buf_put(dma_buf);
734 mutex_unlock(&dev->object_name_lock);
736 mutex_unlock(&file_priv->prime.lock);
737 dma_buf_put(dma_buf);
742 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
744 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
745 struct drm_file *file_priv)
747 struct drm_prime_handle *args = data;
749 if (!drm_core_check_feature(dev, DRIVER_PRIME))
752 if (!dev->driver->prime_handle_to_fd)
755 /* check flags are valid */
756 if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
759 return dev->driver->prime_handle_to_fd(dev, file_priv,
760 args->handle, args->flags, &args->fd);
763 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
764 struct drm_file *file_priv)
766 struct drm_prime_handle *args = data;
768 if (!drm_core_check_feature(dev, DRIVER_PRIME))
771 if (!dev->driver->prime_fd_to_handle)
774 return dev->driver->prime_fd_to_handle(dev, file_priv,
775 args->fd, &args->handle);
779 * drm_prime_pages_to_sg - converts a page array into an sg list
780 * @pages: pointer to the array of page pointers to convert
781 * @nr_pages: length of the page vector
783 * This helper creates an sg table object from a set of pages
784 * the driver is responsible for mapping the pages into the
785 * importers address space for use with dma_buf itself.
787 struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
790 struct sg_table *sg = NULL;
795 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
803 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
804 nr_pages << PAGE_SHIFT, GFP_KERNEL);
814 EXPORT_SYMBOL(drm_prime_pages_to_sg);
817 * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
818 * @sgt: scatter-gather table to convert
819 * @pages: array of page pointers to store the page array in
820 * @addrs: optional array to store the dma bus address of each page
821 * @max_pages: size of both the passed-in arrays
823 * Exports an sg table into an array of pages and addresses. This is currently
824 * required by the TTM driver in order to do correct fault handling.
826 int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
827 dma_addr_t *addrs, int max_pages)
830 struct scatterlist *sg;
837 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
840 addr = sg_dma_address(sg);
843 if (WARN_ON(pg_index >= max_pages))
845 pages[pg_index] = page;
847 addrs[pg_index] = addr;
857 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
860 * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
861 * @obj: GEM object which was created from a dma-buf
862 * @sg: the sg-table which was pinned at import time
864 * This is the cleanup functions which GEM drivers need to call when they use
865 * @drm_gem_prime_import to import dma-bufs.
867 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
869 struct dma_buf_attachment *attach;
870 struct dma_buf *dma_buf;
871 attach = obj->import_attach;
873 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
874 dma_buf = attach->dmabuf;
876 dma_buf_detach(attach->dmabuf, attach);
877 /* remove the reference */
878 dma_buf_put(dma_buf);
881 EXPORT_SYMBOL(drm_prime_gem_destroy);
883 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
885 lockinit(&prime_fpriv->lock, "drmpfpl", 0, LK_CANRECURSE);
886 prime_fpriv->dmabufs = LINUX_RB_ROOT;
887 prime_fpriv->handles = LINUX_RB_ROOT;
890 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
892 /* by now drm_gem_release should've made sure the list is empty */
893 WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));