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>
32 #include <drm/drm_gem.h>
34 #include "drm_internal.h"
37 * DMA-BUF/GEM Object references and lifetime overview:
39 * On the export the dma_buf holds a reference to the exporting GEM
40 * object. It takes this reference in handle_to_fd_ioctl, when it
41 * first calls .prime_export and stores the exporting GEM object in
42 * the dma_buf priv. This reference is released when the dma_buf
43 * object goes away in the driver .release function.
45 * On the import the importing GEM object holds a reference to the
46 * dma_buf (which in turn holds a ref to the exporting GEM object).
47 * It takes that reference in the fd_to_handle ioctl.
48 * It calls dma_buf_get, creates an attachment to it and stores the
49 * attachment in the GEM object. When this attachment is destroyed
50 * when the imported object is destroyed, we remove the attachment
51 * and drop the reference to the dma_buf.
53 * Thus the chain of references always flows in one direction
54 * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
56 * Self-importing: if userspace is using PRIME as a replacement for flink
57 * then it will get a fd->handle request for a GEM object that it created.
58 * Drivers should detect this situation and return back the gem object
59 * from the dma-buf private. Prime will do this automatically for drivers that
60 * use the drm_gem_prime_{import,export} helpers.
63 struct drm_prime_member {
64 struct list_head entry;
65 struct dma_buf *dma_buf;
69 struct drm_prime_attachment {
71 enum dma_data_direction dir;
74 struct drm_prime_callback_data {
75 struct drm_gem_object *obj;
79 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
80 struct dma_buf *dma_buf, uint32_t handle)
82 struct drm_prime_member *member;
84 member = kmalloc(sizeof(*member), GFP_KERNEL);
89 member->dma_buf = dma_buf;
90 member->handle = handle;
91 list_add(&member->entry, &prime_fpriv->head);
95 static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
98 struct drm_prime_member *member;
100 list_for_each_entry(member, &prime_fpriv->head, entry) {
101 if (member->handle == handle)
102 return member->dma_buf;
108 static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
109 struct dma_buf *dma_buf,
112 struct drm_prime_member *member;
114 list_for_each_entry(member, &prime_fpriv->head, entry) {
115 if (member->dma_buf == dma_buf) {
116 *handle = member->handle;
123 static int drm_gem_map_attach(struct dma_buf *dma_buf,
124 struct device *target_dev,
125 struct dma_buf_attachment *attach)
127 struct drm_prime_attachment *prime_attach;
128 struct drm_gem_object *obj = dma_buf->priv;
129 struct drm_device *dev = obj->dev;
131 prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
135 prime_attach->dir = DMA_NONE;
136 attach->priv = prime_attach;
138 if (!dev->driver->gem_prime_pin)
141 return dev->driver->gem_prime_pin(obj);
144 static void drm_gem_map_detach(struct dma_buf *dma_buf,
145 struct dma_buf_attachment *attach)
147 struct drm_prime_attachment *prime_attach = attach->priv;
148 struct drm_gem_object *obj = dma_buf->priv;
149 struct drm_device *dev = obj->dev;
150 struct sg_table *sgt;
152 if (dev->driver->gem_prime_unpin)
153 dev->driver->gem_prime_unpin(obj);
158 sgt = prime_attach->sgt;
160 if (prime_attach->dir != DMA_NONE)
161 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
171 void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
172 struct dma_buf *dma_buf)
174 struct drm_prime_member *member, *safe;
176 list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
177 if (member->dma_buf == dma_buf) {
178 dma_buf_put(dma_buf);
179 list_del(&member->entry);
185 static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
186 enum dma_data_direction dir)
188 struct drm_prime_attachment *prime_attach = attach->priv;
189 struct drm_gem_object *obj = attach->dmabuf->priv;
190 struct sg_table *sgt;
192 if (WARN_ON(dir == DMA_NONE || !prime_attach))
193 return ERR_PTR(-EINVAL);
195 /* return the cached mapping when possible */
196 if (prime_attach->dir == dir)
197 return prime_attach->sgt;
200 * two mappings with different directions for the same attachment are
203 if (WARN_ON(prime_attach->dir != DMA_NONE))
204 return ERR_PTR(-EBUSY);
206 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
209 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
212 sgt = ERR_PTR(-ENOMEM);
214 prime_attach->sgt = sgt;
215 prime_attach->dir = dir;
222 static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
223 struct sg_table *sgt,
224 enum dma_data_direction dir)
226 /* nothing to be done here */
230 * drm_gem_dmabuf_release - dma_buf release implementation for GEM
231 * @dma_buf: buffer to be released
233 * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
234 * must use this in their dma_buf ops structure as the release callback.
236 void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
238 struct drm_gem_object *obj = dma_buf->priv;
240 /* drop the reference on the export fd holds */
241 drm_gem_object_unreference_unlocked(obj);
243 EXPORT_SYMBOL(drm_gem_dmabuf_release);
245 static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
247 struct drm_gem_object *obj = dma_buf->priv;
248 struct drm_device *dev = obj->dev;
250 return dev->driver->gem_prime_vmap(obj);
253 static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
255 struct drm_gem_object *obj = dma_buf->priv;
256 struct drm_device *dev = obj->dev;
258 dev->driver->gem_prime_vunmap(obj, vaddr);
261 static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
262 unsigned long page_num)
267 static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
268 unsigned long page_num, void *addr)
272 static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
273 unsigned long page_num)
278 static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
279 unsigned long page_num, void *addr)
284 static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
285 struct vm_area_struct *vma)
287 struct drm_gem_object *obj = dma_buf->priv;
288 struct drm_device *dev = obj->dev;
290 if (!dev->driver->gem_prime_mmap)
293 return dev->driver->gem_prime_mmap(obj, vma);
296 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
297 .attach = drm_gem_map_attach,
298 .detach = drm_gem_map_detach,
299 .map_dma_buf = drm_gem_map_dma_buf,
300 .unmap_dma_buf = drm_gem_unmap_dma_buf,
301 .release = drm_gem_dmabuf_release,
302 .kmap = drm_gem_dmabuf_kmap,
303 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
304 .kunmap = drm_gem_dmabuf_kunmap,
305 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
306 .mmap = drm_gem_dmabuf_mmap,
307 .vmap = drm_gem_dmabuf_vmap,
308 .vunmap = drm_gem_dmabuf_vunmap,
314 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
315 * simpler APIs by using the helper functions @drm_gem_prime_export and
316 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
317 * six lower-level driver callbacks:
321 * - @gem_prime_pin (optional): prepare a GEM object for exporting
323 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
325 * - @gem_prime_vmap: vmap a buffer exported by your driver
327 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
329 * - @gem_prime_mmap (optional): mmap a buffer exported by your driver
333 * - @gem_prime_import_sg_table (import): produce a GEM object from another
334 * driver's scatter/gather table
338 * drm_gem_prime_export - helper library implementation of the export callback
339 * @dev: drm_device to export from
340 * @obj: GEM object to export
341 * @flags: flags like DRM_CLOEXEC and DRM_RDWR
343 * This is the implementation of the gem_prime_export functions for GEM drivers
344 * using the PRIME helpers.
346 struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
347 struct drm_gem_object *obj,
350 struct dma_buf_export_info exp_info = {
351 .exp_name = KBUILD_MODNAME, /* white lie for debug */
352 .owner = dev->driver->fops->owner,
353 .ops = &drm_gem_prime_dmabuf_ops,
359 if (dev->driver->gem_prime_res_obj)
360 exp_info.resv = dev->driver->gem_prime_res_obj(obj);
362 return dma_buf_export(&exp_info);
364 EXPORT_SYMBOL(drm_gem_prime_export);
366 static struct dma_buf *export_and_register_object(struct drm_device *dev,
367 struct drm_gem_object *obj,
370 struct dma_buf *dmabuf;
372 /* prevent races with concurrent gem_close. */
373 if (obj->handle_count == 0) {
374 dmabuf = ERR_PTR(-ENOENT);
378 dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
379 if (IS_ERR(dmabuf)) {
380 /* normally the created dma-buf takes ownership of the ref,
381 * but if that fails then drop the ref
387 * Note that callers do not need to clean up the export cache
388 * since the check for obj->handle_count guarantees that someone
391 obj->dma_buf = dmabuf;
392 get_dma_buf(obj->dma_buf);
393 /* Grab a new ref since the callers is now used by the dma-buf */
394 drm_gem_object_reference(obj);
400 * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
401 * @dev: dev to export the buffer from
402 * @file_priv: drm file-private structure
403 * @handle: buffer handle to export
404 * @flags: flags like DRM_CLOEXEC
405 * @prime_fd: pointer to storage for the fd id of the create dma-buf
407 * This is the PRIME export function which must be used mandatorily by GEM
408 * drivers to ensure correct lifetime management of the underlying GEM object.
409 * The actual exporting from GEM object to a dma-buf is done through the
410 * gem_prime_export driver callback.
412 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
413 struct drm_file *file_priv, uint32_t handle,
417 struct drm_gem_object *obj;
419 struct dma_buf *dmabuf;
421 mutex_lock(&file_priv->prime.lock);
422 obj = drm_gem_object_lookup(dev, file_priv, handle);
428 dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
431 goto out_have_handle;
434 mutex_lock(&dev->object_name_lock);
435 /* re-export the original imported object */
436 if (obj->import_attach) {
437 dmabuf = obj->import_attach->dmabuf;
443 get_dma_buf(obj->dma_buf);
444 dmabuf = obj->dma_buf;
448 dmabuf = export_and_register_object(dev, obj, flags);
449 if (IS_ERR(dmabuf)) {
450 /* normally the created dma-buf takes ownership of the ref,
451 * but if that fails then drop the ref
453 ret = PTR_ERR(dmabuf);
454 mutex_unlock(&dev->object_name_lock);
460 * If we've exported this buffer then cheat and add it to the import list
461 * so we get the correct handle back. We must do this under the
462 * protection of dev->object_name_lock to ensure that a racing gem close
463 * ioctl doesn't miss to remove this buffer handle from the cache.
465 ret = drm_prime_add_buf_handle(&file_priv->prime,
467 mutex_unlock(&dev->object_name_lock);
469 goto fail_put_dmabuf;
472 ret = dma_buf_fd(dmabuf, flags);
474 * We must _not_ remove the buffer from the handle cache since the newly
475 * created dma buf is already linked in the global obj->dma_buf pointer,
476 * and that is invariant as long as a userspace gem handle exists.
477 * Closing the handle will clean out the cache anyway, so we don't leak.
480 goto fail_put_dmabuf;
491 drm_gem_object_unreference_unlocked(obj);
493 mutex_unlock(&file_priv->prime.lock);
497 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
499 static void drm_gem_prime_dmabuf_release_callback(void *data)
501 struct drm_prime_callback_data *cb_data = data;
503 if (cb_data && cb_data->obj && cb_data->obj->import_attach) {
504 struct dma_buf_attachment *attach = cb_data->obj->import_attach;
505 struct sg_table *sgt = cb_data->sgt;
508 dma_buf_unmap_attachment(attach, sgt,
510 dma_buf_detach(attach->dmabuf, attach);
511 drm_gem_object_unreference_unlocked(cb_data->obj);
517 * drm_gem_prime_import - helper library implementation of the import callback
518 * @dev: drm_device to import into
519 * @dma_buf: dma-buf object to import
521 * This is the implementation of the gem_prime_import functions for GEM drivers
522 * using the PRIME helpers.
524 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
525 struct dma_buf *dma_buf)
527 struct dma_buf_attachment *attach;
528 struct sg_table *sgt;
529 struct drm_gem_object *obj;
530 struct drm_prime_callback_data *cb_data;
533 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
535 if (obj->dev == dev) {
537 * Importing dmabuf exported from out own gem increases
538 * refcount on gem itself instead of f_count of dmabuf.
540 drm_gem_object_reference(obj);
545 cb_data = dma_buf_get_release_callback_data(dma_buf,
546 drm_gem_prime_dmabuf_release_callback);
547 if (cb_data && cb_data->obj && cb_data->obj->dev == dev) {
548 drm_gem_object_reference(cb_data->obj);
552 if (!dev->driver->gem_prime_import_sg_table)
553 return ERR_PTR(-EINVAL);
555 attach = dma_buf_attach(dma_buf, dev->dev);
557 return ERR_CAST(attach);
559 get_dma_buf(dma_buf);
560 cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
566 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
572 obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
577 obj->import_attach = attach;
580 dma_buf_set_release_callback(dma_buf,
581 drm_gem_prime_dmabuf_release_callback, cb_data);
582 dma_buf_put(dma_buf);
583 drm_gem_object_reference(obj);
588 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
592 dma_buf_detach(dma_buf, attach);
593 dma_buf_put(dma_buf);
597 EXPORT_SYMBOL(drm_gem_prime_import);
600 * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
601 * @dev: dev to export the buffer from
602 * @file_priv: drm file-private structure
603 * @prime_fd: fd id of the dma-buf which should be imported
604 * @handle: pointer to storage for the handle of the imported buffer object
606 * This is the PRIME import function which must be used mandatorily by GEM
607 * drivers to ensure correct lifetime management of the underlying GEM object.
608 * The actual importing of GEM object from the dma-buf is done through the
609 * gem_import_export driver callback.
611 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
612 struct drm_file *file_priv, int prime_fd,
615 struct dma_buf *dma_buf;
616 struct drm_gem_object *obj;
619 dma_buf = dma_buf_get(prime_fd);
621 return PTR_ERR(dma_buf);
623 mutex_lock(&file_priv->prime.lock);
625 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
630 /* never seen this one, need to import */
631 mutex_lock(&dev->object_name_lock);
632 obj = dev->driver->gem_prime_import(dev, dma_buf);
639 WARN_ON(obj->dma_buf != dma_buf);
641 obj->dma_buf = dma_buf;
642 get_dma_buf(dma_buf);
645 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
646 ret = drm_gem_handle_create_tail(file_priv, obj, handle);
647 drm_gem_object_unreference_unlocked(obj);
651 ret = drm_prime_add_buf_handle(&file_priv->prime,
656 mutex_unlock(&file_priv->prime.lock);
658 dma_buf_put(dma_buf);
663 /* hmm, if driver attached, we are relying on the free-object path
664 * to detach.. which seems ok..
666 drm_gem_handle_delete(file_priv, *handle);
668 mutex_unlock(&dev->object_name_lock);
670 dma_buf_put(dma_buf);
671 mutex_unlock(&file_priv->prime.lock);
674 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
676 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
677 struct drm_file *file_priv)
679 struct drm_prime_handle *args = data;
681 if (!drm_core_check_feature(dev, DRIVER_PRIME))
684 if (!dev->driver->prime_handle_to_fd)
687 /* check flags are valid */
688 if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
691 return dev->driver->prime_handle_to_fd(dev, file_priv,
692 args->handle, args->flags, &args->fd);
695 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
696 struct drm_file *file_priv)
698 struct drm_prime_handle *args = data;
700 if (!drm_core_check_feature(dev, DRIVER_PRIME))
703 if (!dev->driver->prime_fd_to_handle)
706 return dev->driver->prime_fd_to_handle(dev, file_priv,
707 args->fd, &args->handle);
711 * drm_prime_pages_to_sg - converts a page array into an sg list
712 * @pages: pointer to the array of page pointers to convert
713 * @nr_pages: length of the page vector
715 * This helper creates an sg table object from a set of pages
716 * the driver is responsible for mapping the pages into the
717 * importers address space for use with dma_buf itself.
719 struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
721 struct sg_table *sg = NULL;
724 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
730 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
731 nr_pages << PAGE_SHIFT, GFP_KERNEL);
740 EXPORT_SYMBOL(drm_prime_pages_to_sg);
743 * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
744 * @sgt: scatter-gather table to convert
745 * @pages: array of page pointers to store the page array in
746 * @addrs: optional array to store the dma bus address of each page
747 * @max_pages: size of both the passed-in arrays
749 * Exports an sg table into an array of pages and addresses. This is currently
750 * required by the TTM driver in order to do correct fault handling.
752 int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
753 dma_addr_t *addrs, int max_pages)
756 struct scatterlist *sg;
763 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
766 addr = sg_dma_address(sg);
769 if (WARN_ON(pg_index >= max_pages))
771 pages[pg_index] = page;
773 addrs[pg_index] = addr;
783 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
786 * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
787 * @obj: GEM object which was created from a dma-buf
788 * @sg: the sg-table which was pinned at import time
790 * This is the cleanup functions which GEM drivers need to call when they use
791 * @drm_gem_prime_import to import dma-bufs.
793 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
795 struct dma_buf_attachment *attach;
796 struct dma_buf *dma_buf;
797 attach = obj->import_attach;
799 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
800 dma_buf = attach->dmabuf;
801 dma_buf_detach(attach->dmabuf, attach);
802 /* remove the reference */
803 dma_buf_put(dma_buf);
805 EXPORT_SYMBOL(drm_prime_gem_destroy);
807 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
809 INIT_LIST_HEAD(&prime_fpriv->head);
810 mutex_init(&prime_fpriv->lock);
813 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
815 /* by now drm_gem_release should've made sure the list is empty */
816 WARN_ON(!list_empty(&prime_fpriv->head));