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