Home | History | Annotate | Line # | Download | only in drm
drm_prime.c revision 1.1
      1  1.1  riastrad /*
      2  1.1  riastrad  * Copyright  2012 Red Hat
      3  1.1  riastrad  *
      4  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      5  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      6  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      7  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
      9  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     10  1.1  riastrad  *
     11  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     12  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     13  1.1  riastrad  * Software.
     14  1.1  riastrad  *
     15  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  1.1  riastrad  * IN THE SOFTWARE.
     22  1.1  riastrad  *
     23  1.1  riastrad  * Authors:
     24  1.1  riastrad  *      Dave Airlie <airlied (at) redhat.com>
     25  1.1  riastrad  *      Rob Clark <rob.clark (at) linaro.org>
     26  1.1  riastrad  *
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/export.h>
     30  1.1  riastrad #include <linux/dma-buf.h>
     31  1.1  riastrad #include <drm/drmP.h>
     32  1.1  riastrad 
     33  1.1  riastrad /*
     34  1.1  riastrad  * DMA-BUF/GEM Object references and lifetime overview:
     35  1.1  riastrad  *
     36  1.1  riastrad  * On the export the dma_buf holds a reference to the exporting GEM
     37  1.1  riastrad  * object. It takes this reference in handle_to_fd_ioctl, when it
     38  1.1  riastrad  * first calls .prime_export and stores the exporting GEM object in
     39  1.1  riastrad  * the dma_buf priv. This reference is released when the dma_buf
     40  1.1  riastrad  * object goes away in the driver .release function.
     41  1.1  riastrad  *
     42  1.1  riastrad  * On the import the importing GEM object holds a reference to the
     43  1.1  riastrad  * dma_buf (which in turn holds a ref to the exporting GEM object).
     44  1.1  riastrad  * It takes that reference in the fd_to_handle ioctl.
     45  1.1  riastrad  * It calls dma_buf_get, creates an attachment to it and stores the
     46  1.1  riastrad  * attachment in the GEM object. When this attachment is destroyed
     47  1.1  riastrad  * when the imported object is destroyed, we remove the attachment
     48  1.1  riastrad  * and drop the reference to the dma_buf.
     49  1.1  riastrad  *
     50  1.1  riastrad  * Thus the chain of references always flows in one direction
     51  1.1  riastrad  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
     52  1.1  riastrad  *
     53  1.1  riastrad  * Self-importing: if userspace is using PRIME as a replacement for flink
     54  1.1  riastrad  * then it will get a fd->handle request for a GEM object that it created.
     55  1.1  riastrad  * Drivers should detect this situation and return back the gem object
     56  1.1  riastrad  * from the dma-buf private.
     57  1.1  riastrad  */
     58  1.1  riastrad 
     59  1.1  riastrad struct drm_prime_member {
     60  1.1  riastrad 	struct list_head entry;
     61  1.1  riastrad 	struct dma_buf *dma_buf;
     62  1.1  riastrad 	uint32_t handle;
     63  1.1  riastrad };
     64  1.1  riastrad 
     65  1.1  riastrad int drm_gem_prime_handle_to_fd(struct drm_device *dev,
     66  1.1  riastrad 		struct drm_file *file_priv, uint32_t handle, uint32_t flags,
     67  1.1  riastrad 		int *prime_fd)
     68  1.1  riastrad {
     69  1.1  riastrad 	struct drm_gem_object *obj;
     70  1.1  riastrad 	void *buf;
     71  1.1  riastrad 	int ret;
     72  1.1  riastrad 
     73  1.1  riastrad 	obj = drm_gem_object_lookup(dev, file_priv, handle);
     74  1.1  riastrad 	if (!obj)
     75  1.1  riastrad 		return -ENOENT;
     76  1.1  riastrad 
     77  1.1  riastrad 	mutex_lock(&file_priv->prime.lock);
     78  1.1  riastrad 	/* re-export the original imported object */
     79  1.1  riastrad 	if (obj->import_attach) {
     80  1.1  riastrad 		get_dma_buf(obj->import_attach->dmabuf);
     81  1.1  riastrad 		*prime_fd = dma_buf_fd(obj->import_attach->dmabuf, flags);
     82  1.1  riastrad 		drm_gem_object_unreference_unlocked(obj);
     83  1.1  riastrad 		mutex_unlock(&file_priv->prime.lock);
     84  1.1  riastrad 		return 0;
     85  1.1  riastrad 	}
     86  1.1  riastrad 
     87  1.1  riastrad 	if (obj->export_dma_buf) {
     88  1.1  riastrad 		get_dma_buf(obj->export_dma_buf);
     89  1.1  riastrad 		*prime_fd = dma_buf_fd(obj->export_dma_buf, flags);
     90  1.1  riastrad 		drm_gem_object_unreference_unlocked(obj);
     91  1.1  riastrad 	} else {
     92  1.1  riastrad 		buf = dev->driver->gem_prime_export(dev, obj, flags);
     93  1.1  riastrad 		if (IS_ERR(buf)) {
     94  1.1  riastrad 			/* normally the created dma-buf takes ownership of the ref,
     95  1.1  riastrad 			 * but if that fails then drop the ref
     96  1.1  riastrad 			 */
     97  1.1  riastrad 			drm_gem_object_unreference_unlocked(obj);
     98  1.1  riastrad 			mutex_unlock(&file_priv->prime.lock);
     99  1.1  riastrad 			return PTR_ERR(buf);
    100  1.1  riastrad 		}
    101  1.1  riastrad 		obj->export_dma_buf = buf;
    102  1.1  riastrad 		*prime_fd = dma_buf_fd(buf, flags);
    103  1.1  riastrad 	}
    104  1.1  riastrad 	/* if we've exported this buffer the cheat and add it to the import list
    105  1.1  riastrad 	 * so we get the correct handle back
    106  1.1  riastrad 	 */
    107  1.1  riastrad 	ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
    108  1.1  riastrad 			obj->export_dma_buf, handle);
    109  1.1  riastrad 	if (ret) {
    110  1.1  riastrad 		drm_gem_object_unreference_unlocked(obj);
    111  1.1  riastrad 		mutex_unlock(&file_priv->prime.lock);
    112  1.1  riastrad 		return ret;
    113  1.1  riastrad 	}
    114  1.1  riastrad 
    115  1.1  riastrad 	mutex_unlock(&file_priv->prime.lock);
    116  1.1  riastrad 	return 0;
    117  1.1  riastrad }
    118  1.1  riastrad EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
    119  1.1  riastrad 
    120  1.1  riastrad int drm_gem_prime_fd_to_handle(struct drm_device *dev,
    121  1.1  riastrad 		struct drm_file *file_priv, int prime_fd, uint32_t *handle)
    122  1.1  riastrad {
    123  1.1  riastrad 	struct dma_buf *dma_buf;
    124  1.1  riastrad 	struct drm_gem_object *obj;
    125  1.1  riastrad 	int ret;
    126  1.1  riastrad 
    127  1.1  riastrad 	dma_buf = dma_buf_get(prime_fd);
    128  1.1  riastrad 	if (IS_ERR(dma_buf))
    129  1.1  riastrad 		return PTR_ERR(dma_buf);
    130  1.1  riastrad 
    131  1.1  riastrad 	mutex_lock(&file_priv->prime.lock);
    132  1.1  riastrad 
    133  1.1  riastrad 	ret = drm_prime_lookup_imported_buf_handle(&file_priv->prime,
    134  1.1  riastrad 			dma_buf, handle);
    135  1.1  riastrad 	if (!ret) {
    136  1.1  riastrad 		ret = 0;
    137  1.1  riastrad 		goto out_put;
    138  1.1  riastrad 	}
    139  1.1  riastrad 
    140  1.1  riastrad 	/* never seen this one, need to import */
    141  1.1  riastrad 	obj = dev->driver->gem_prime_import(dev, dma_buf);
    142  1.1  riastrad 	if (IS_ERR(obj)) {
    143  1.1  riastrad 		ret = PTR_ERR(obj);
    144  1.1  riastrad 		goto out_put;
    145  1.1  riastrad 	}
    146  1.1  riastrad 
    147  1.1  riastrad 	ret = drm_gem_handle_create(file_priv, obj, handle);
    148  1.1  riastrad 	drm_gem_object_unreference_unlocked(obj);
    149  1.1  riastrad 	if (ret)
    150  1.1  riastrad 		goto out_put;
    151  1.1  riastrad 
    152  1.1  riastrad 	ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
    153  1.1  riastrad 			dma_buf, *handle);
    154  1.1  riastrad 	if (ret)
    155  1.1  riastrad 		goto fail;
    156  1.1  riastrad 
    157  1.1  riastrad 	mutex_unlock(&file_priv->prime.lock);
    158  1.1  riastrad 	return 0;
    159  1.1  riastrad 
    160  1.1  riastrad fail:
    161  1.1  riastrad 	/* hmm, if driver attached, we are relying on the free-object path
    162  1.1  riastrad 	 * to detach.. which seems ok..
    163  1.1  riastrad 	 */
    164  1.1  riastrad 	drm_gem_object_handle_unreference_unlocked(obj);
    165  1.1  riastrad out_put:
    166  1.1  riastrad 	dma_buf_put(dma_buf);
    167  1.1  riastrad 	mutex_unlock(&file_priv->prime.lock);
    168  1.1  riastrad 	return ret;
    169  1.1  riastrad }
    170  1.1  riastrad EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
    171  1.1  riastrad 
    172  1.1  riastrad int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
    173  1.1  riastrad 				 struct drm_file *file_priv)
    174  1.1  riastrad {
    175  1.1  riastrad 	struct drm_prime_handle *args = data;
    176  1.1  riastrad 	uint32_t flags;
    177  1.1  riastrad 
    178  1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_PRIME))
    179  1.1  riastrad 		return -EINVAL;
    180  1.1  riastrad 
    181  1.1  riastrad 	if (!dev->driver->prime_handle_to_fd)
    182  1.1  riastrad 		return -ENOSYS;
    183  1.1  riastrad 
    184  1.1  riastrad 	/* check flags are valid */
    185  1.1  riastrad 	if (args->flags & ~DRM_CLOEXEC)
    186  1.1  riastrad 		return -EINVAL;
    187  1.1  riastrad 
    188  1.1  riastrad 	/* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
    189  1.1  riastrad 	flags = args->flags & DRM_CLOEXEC;
    190  1.1  riastrad 
    191  1.1  riastrad 	return dev->driver->prime_handle_to_fd(dev, file_priv,
    192  1.1  riastrad 			args->handle, flags, &args->fd);
    193  1.1  riastrad }
    194  1.1  riastrad 
    195  1.1  riastrad int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
    196  1.1  riastrad 				 struct drm_file *file_priv)
    197  1.1  riastrad {
    198  1.1  riastrad 	struct drm_prime_handle *args = data;
    199  1.1  riastrad 
    200  1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_PRIME))
    201  1.1  riastrad 		return -EINVAL;
    202  1.1  riastrad 
    203  1.1  riastrad 	if (!dev->driver->prime_fd_to_handle)
    204  1.1  riastrad 		return -ENOSYS;
    205  1.1  riastrad 
    206  1.1  riastrad 	return dev->driver->prime_fd_to_handle(dev, file_priv,
    207  1.1  riastrad 			args->fd, &args->handle);
    208  1.1  riastrad }
    209  1.1  riastrad 
    210  1.1  riastrad /*
    211  1.1  riastrad  * drm_prime_pages_to_sg
    212  1.1  riastrad  *
    213  1.1  riastrad  * this helper creates an sg table object from a set of pages
    214  1.1  riastrad  * the driver is responsible for mapping the pages into the
    215  1.1  riastrad  * importers address space
    216  1.1  riastrad  */
    217  1.1  riastrad struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
    218  1.1  riastrad {
    219  1.1  riastrad 	struct sg_table *sg = NULL;
    220  1.1  riastrad 	struct scatterlist *iter;
    221  1.1  riastrad 	int i;
    222  1.1  riastrad 	int ret;
    223  1.1  riastrad 
    224  1.1  riastrad 	sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
    225  1.1  riastrad 	if (!sg)
    226  1.1  riastrad 		goto out;
    227  1.1  riastrad 
    228  1.1  riastrad 	ret = sg_alloc_table(sg, nr_pages, GFP_KERNEL);
    229  1.1  riastrad 	if (ret)
    230  1.1  riastrad 		goto out;
    231  1.1  riastrad 
    232  1.1  riastrad 	for_each_sg(sg->sgl, iter, nr_pages, i)
    233  1.1  riastrad 		sg_set_page(iter, pages[i], PAGE_SIZE, 0);
    234  1.1  riastrad 
    235  1.1  riastrad 	return sg;
    236  1.1  riastrad out:
    237  1.1  riastrad 	kfree(sg);
    238  1.1  riastrad 	return NULL;
    239  1.1  riastrad }
    240  1.1  riastrad EXPORT_SYMBOL(drm_prime_pages_to_sg);
    241  1.1  riastrad 
    242  1.1  riastrad /* export an sg table into an array of pages and addresses
    243  1.1  riastrad    this is currently required by the TTM driver in order to do correct fault
    244  1.1  riastrad    handling */
    245  1.1  riastrad int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
    246  1.1  riastrad 				     dma_addr_t *addrs, int max_pages)
    247  1.1  riastrad {
    248  1.1  riastrad 	unsigned count;
    249  1.1  riastrad 	struct scatterlist *sg;
    250  1.1  riastrad 	struct page *page;
    251  1.1  riastrad 	u32 len, offset;
    252  1.1  riastrad 	int pg_index;
    253  1.1  riastrad 	dma_addr_t addr;
    254  1.1  riastrad 
    255  1.1  riastrad 	pg_index = 0;
    256  1.1  riastrad 	for_each_sg(sgt->sgl, sg, sgt->nents, count) {
    257  1.1  riastrad 		len = sg->length;
    258  1.1  riastrad 		offset = sg->offset;
    259  1.1  riastrad 		page = sg_page(sg);
    260  1.1  riastrad 		addr = sg_dma_address(sg);
    261  1.1  riastrad 
    262  1.1  riastrad 		while (len > 0) {
    263  1.1  riastrad 			if (WARN_ON(pg_index >= max_pages))
    264  1.1  riastrad 				return -1;
    265  1.1  riastrad 			pages[pg_index] = page;
    266  1.1  riastrad 			if (addrs)
    267  1.1  riastrad 				addrs[pg_index] = addr;
    268  1.1  riastrad 
    269  1.1  riastrad 			page++;
    270  1.1  riastrad 			addr += PAGE_SIZE;
    271  1.1  riastrad 			len -= PAGE_SIZE;
    272  1.1  riastrad 			pg_index++;
    273  1.1  riastrad 		}
    274  1.1  riastrad 	}
    275  1.1  riastrad 	return 0;
    276  1.1  riastrad }
    277  1.1  riastrad EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
    278  1.1  riastrad /* helper function to cleanup a GEM/prime object */
    279  1.1  riastrad void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
    280  1.1  riastrad {
    281  1.1  riastrad 	struct dma_buf_attachment *attach;
    282  1.1  riastrad 	struct dma_buf *dma_buf;
    283  1.1  riastrad 	attach = obj->import_attach;
    284  1.1  riastrad 	if (sg)
    285  1.1  riastrad 		dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
    286  1.1  riastrad 	dma_buf = attach->dmabuf;
    287  1.1  riastrad 	dma_buf_detach(attach->dmabuf, attach);
    288  1.1  riastrad 	/* remove the reference */
    289  1.1  riastrad 	dma_buf_put(dma_buf);
    290  1.1  riastrad }
    291  1.1  riastrad EXPORT_SYMBOL(drm_prime_gem_destroy);
    292  1.1  riastrad 
    293  1.1  riastrad void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
    294  1.1  riastrad {
    295  1.1  riastrad 	INIT_LIST_HEAD(&prime_fpriv->head);
    296  1.1  riastrad 	mutex_init(&prime_fpriv->lock);
    297  1.1  riastrad }
    298  1.1  riastrad EXPORT_SYMBOL(drm_prime_init_file_private);
    299  1.1  riastrad 
    300  1.1  riastrad void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
    301  1.1  riastrad {
    302  1.1  riastrad 	struct drm_prime_member *member, *safe;
    303  1.1  riastrad 	list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
    304  1.1  riastrad 		list_del(&member->entry);
    305  1.1  riastrad 		kfree(member);
    306  1.1  riastrad 	}
    307  1.1  riastrad }
    308  1.1  riastrad EXPORT_SYMBOL(drm_prime_destroy_file_private);
    309  1.1  riastrad 
    310  1.1  riastrad int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
    311  1.1  riastrad {
    312  1.1  riastrad 	struct drm_prime_member *member;
    313  1.1  riastrad 
    314  1.1  riastrad 	member = kmalloc(sizeof(*member), GFP_KERNEL);
    315  1.1  riastrad 	if (!member)
    316  1.1  riastrad 		return -ENOMEM;
    317  1.1  riastrad 
    318  1.1  riastrad 	member->dma_buf = dma_buf;
    319  1.1  riastrad 	member->handle = handle;
    320  1.1  riastrad 	list_add(&member->entry, &prime_fpriv->head);
    321  1.1  riastrad 	return 0;
    322  1.1  riastrad }
    323  1.1  riastrad EXPORT_SYMBOL(drm_prime_add_imported_buf_handle);
    324  1.1  riastrad 
    325  1.1  riastrad int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
    326  1.1  riastrad {
    327  1.1  riastrad 	struct drm_prime_member *member;
    328  1.1  riastrad 
    329  1.1  riastrad 	list_for_each_entry(member, &prime_fpriv->head, entry) {
    330  1.1  riastrad 		if (member->dma_buf == dma_buf) {
    331  1.1  riastrad 			*handle = member->handle;
    332  1.1  riastrad 			return 0;
    333  1.1  riastrad 		}
    334  1.1  riastrad 	}
    335  1.1  riastrad 	return -ENOENT;
    336  1.1  riastrad }
    337  1.1  riastrad EXPORT_SYMBOL(drm_prime_lookup_imported_buf_handle);
    338  1.1  riastrad 
    339  1.1  riastrad void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
    340  1.1  riastrad {
    341  1.1  riastrad 	struct drm_prime_member *member, *safe;
    342  1.1  riastrad 
    343  1.1  riastrad 	mutex_lock(&prime_fpriv->lock);
    344  1.1  riastrad 	list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
    345  1.1  riastrad 		if (member->dma_buf == dma_buf) {
    346  1.1  riastrad 			list_del(&member->entry);
    347  1.1  riastrad 			kfree(member);
    348  1.1  riastrad 		}
    349  1.1  riastrad 	}
    350  1.1  riastrad 	mutex_unlock(&prime_fpriv->lock);
    351  1.1  riastrad }
    352  1.1  riastrad EXPORT_SYMBOL(drm_prime_remove_imported_buf_handle);
    353