Home | History | Annotate | Line # | Download | only in drm
drm_prime.c revision 1.1.1.2.30.1
      1  1.1.1.2.30.1  christos /*	$NetBSD: drm_prime.c,v 1.1.1.2.30.1 2019/06/10 22:07:57 christos Exp $	*/
      2  1.1.1.2.30.1  christos 
      3           1.1  riastrad /*
      4           1.1  riastrad  * Copyright  2012 Red Hat
      5           1.1  riastrad  *
      6           1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7           1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8           1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9           1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10           1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11           1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12           1.1  riastrad  *
     13           1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14           1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15           1.1  riastrad  * Software.
     16           1.1  riastrad  *
     17           1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18           1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19           1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20           1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21           1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22           1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23           1.1  riastrad  * IN THE SOFTWARE.
     24           1.1  riastrad  *
     25           1.1  riastrad  * Authors:
     26           1.1  riastrad  *      Dave Airlie <airlied (at) redhat.com>
     27           1.1  riastrad  *      Rob Clark <rob.clark (at) linaro.org>
     28           1.1  riastrad  *
     29           1.1  riastrad  */
     30           1.1  riastrad 
     31  1.1.1.2.30.1  christos #include <sys/cdefs.h>
     32  1.1.1.2.30.1  christos __KERNEL_RCSID(0, "$NetBSD: drm_prime.c,v 1.1.1.2.30.1 2019/06/10 22:07:57 christos Exp $");
     33  1.1.1.2.30.1  christos 
     34           1.1  riastrad #include <linux/export.h>
     35           1.1  riastrad #include <linux/dma-buf.h>
     36           1.1  riastrad #include <drm/drmP.h>
     37  1.1.1.2.30.1  christos #include <drm/drm_gem.h>
     38  1.1.1.2.30.1  christos 
     39  1.1.1.2.30.1  christos #include "drm_internal.h"
     40  1.1.1.2.30.1  christos 
     41  1.1.1.2.30.1  christos #ifdef __NetBSD__
     42  1.1.1.2.30.1  christos 
     43  1.1.1.2.30.1  christos #include <drm/bus_dma_hacks.h>
     44  1.1.1.2.30.1  christos 
     45  1.1.1.2.30.1  christos /*
     46  1.1.1.2.30.1  christos  * We use struct sg_table just to pass around an array of pages from
     47  1.1.1.2.30.1  christos  * one device to another in drm prime.  Since this is _not_ a complete
     48  1.1.1.2.30.1  christos  * implementation of Linux's sg table abstraction (e.g., it does not
     49  1.1.1.2.30.1  christos  * remember DMA addresses and RAM pages separately, and it doesn't
     50  1.1.1.2.30.1  christos  * support the nested chained iteration of Linux scatterlists), we
     51  1.1.1.2.30.1  christos  * isolate it to this file and make all callers go through a few extra
     52  1.1.1.2.30.1  christos  * subroutines (drm_prime_sg_size, drm_prime_sg_free, &c.) to use it.
     53  1.1.1.2.30.1  christos  * Don't use this outside drm prime!
     54  1.1.1.2.30.1  christos  */
     55  1.1.1.2.30.1  christos 
     56  1.1.1.2.30.1  christos struct sg_table {
     57  1.1.1.2.30.1  christos 	paddr_t		*sgt_pgs;
     58  1.1.1.2.30.1  christos 	unsigned	sgt_npgs;
     59  1.1.1.2.30.1  christos };
     60  1.1.1.2.30.1  christos 
     61  1.1.1.2.30.1  christos static int
     62  1.1.1.2.30.1  christos sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
     63  1.1.1.2.30.1  christos     unsigned npages, bus_size_t offset, bus_size_t size, gfp_t gfp)
     64  1.1.1.2.30.1  christos {
     65  1.1.1.2.30.1  christos 	unsigned i;
     66  1.1.1.2.30.1  christos 
     67  1.1.1.2.30.1  christos 	KASSERT(offset == 0);
     68  1.1.1.2.30.1  christos 	KASSERT(size == npages << PAGE_SHIFT);
     69  1.1.1.2.30.1  christos 
     70  1.1.1.2.30.1  christos 	sgt->sgt_pgs = kcalloc(npages, sizeof(sgt->sgt_pgs[0]), gfp);
     71  1.1.1.2.30.1  christos 	if (sgt->sgt_pgs == NULL)
     72  1.1.1.2.30.1  christos 		return -ENOMEM;
     73  1.1.1.2.30.1  christos 	sgt->sgt_npgs = npages;
     74  1.1.1.2.30.1  christos 
     75  1.1.1.2.30.1  christos 	for (i = 0; i < npages; i++)
     76  1.1.1.2.30.1  christos 		sgt->sgt_pgs[i] = VM_PAGE_TO_PHYS(&pages[i]->p_vmp);
     77  1.1.1.2.30.1  christos 
     78  1.1.1.2.30.1  christos 	return 0;
     79  1.1.1.2.30.1  christos }
     80  1.1.1.2.30.1  christos 
     81  1.1.1.2.30.1  christos static int
     82  1.1.1.2.30.1  christos sg_alloc_table_from_pglist(struct sg_table *sgt, const struct pglist *pglist,
     83  1.1.1.2.30.1  christos     unsigned npages, bus_size_t offset, bus_size_t size, gfp_t gfp)
     84  1.1.1.2.30.1  christos {
     85  1.1.1.2.30.1  christos 	struct vm_page *pg;
     86  1.1.1.2.30.1  christos 	unsigned i;
     87  1.1.1.2.30.1  christos 
     88  1.1.1.2.30.1  christos 	KASSERT(offset == 0);
     89  1.1.1.2.30.1  christos 	KASSERT(size == npages << PAGE_SHIFT);
     90  1.1.1.2.30.1  christos 
     91  1.1.1.2.30.1  christos 	sgt->sgt_pgs = kcalloc(npages, sizeof(sgt->sgt_pgs[0]), gfp);
     92  1.1.1.2.30.1  christos 	if (sgt->sgt_pgs == NULL)
     93  1.1.1.2.30.1  christos 		return -ENOMEM;
     94  1.1.1.2.30.1  christos 	sgt->sgt_npgs = npages;
     95  1.1.1.2.30.1  christos 
     96  1.1.1.2.30.1  christos 	i = 0;
     97  1.1.1.2.30.1  christos 	TAILQ_FOREACH(pg, pglist, pageq.queue) {
     98  1.1.1.2.30.1  christos 		KASSERT(i < npages);
     99  1.1.1.2.30.1  christos 		sgt->sgt_pgs[i] = VM_PAGE_TO_PHYS(pg);
    100  1.1.1.2.30.1  christos 	}
    101  1.1.1.2.30.1  christos 	KASSERT(i == npages);
    102  1.1.1.2.30.1  christos 
    103  1.1.1.2.30.1  christos 	return 0;
    104  1.1.1.2.30.1  christos }
    105  1.1.1.2.30.1  christos 
    106  1.1.1.2.30.1  christos static int
    107  1.1.1.2.30.1  christos sg_alloc_table_from_bus_dmamem(struct sg_table *sgt, bus_dma_tag_t dmat,
    108  1.1.1.2.30.1  christos     const bus_dma_segment_t *segs, int nsegs, gfp_t gfp)
    109  1.1.1.2.30.1  christos {
    110  1.1.1.2.30.1  christos 	int ret;
    111  1.1.1.2.30.1  christos 
    112  1.1.1.2.30.1  christos 	KASSERT(nsegs > 0);
    113  1.1.1.2.30.1  christos 	sgt->sgt_pgs = kcalloc(nsegs, sizeof(sgt->sgt_pgs[0]), gfp);
    114  1.1.1.2.30.1  christos 	if (sgt->sgt_pgs == NULL)
    115  1.1.1.2.30.1  christos 		return -ENOMEM;
    116  1.1.1.2.30.1  christos 	sgt->sgt_npgs = nsegs;
    117  1.1.1.2.30.1  christos 
    118  1.1.1.2.30.1  christos 	/* XXX errno NetBSD->Linux */
    119  1.1.1.2.30.1  christos 	ret = -bus_dmamem_export_pages(dmat, segs, nsegs, sgt->sgt_pgs,
    120  1.1.1.2.30.1  christos 	    sgt->sgt_npgs);
    121  1.1.1.2.30.1  christos 	if (ret)
    122  1.1.1.2.30.1  christos 		return ret;
    123  1.1.1.2.30.1  christos 
    124  1.1.1.2.30.1  christos 	return 0;
    125  1.1.1.2.30.1  christos }
    126  1.1.1.2.30.1  christos 
    127  1.1.1.2.30.1  christos static void
    128  1.1.1.2.30.1  christos sg_free_table(struct sg_table *sgt)
    129  1.1.1.2.30.1  christos {
    130  1.1.1.2.30.1  christos 
    131  1.1.1.2.30.1  christos 	kfree(sgt->sgt_pgs);
    132  1.1.1.2.30.1  christos 	sgt->sgt_pgs = NULL;
    133  1.1.1.2.30.1  christos 	sgt->sgt_npgs = 0;
    134  1.1.1.2.30.1  christos }
    135  1.1.1.2.30.1  christos 
    136  1.1.1.2.30.1  christos #endif	/* __NetBSD__ */
    137           1.1  riastrad 
    138           1.1  riastrad /*
    139           1.1  riastrad  * DMA-BUF/GEM Object references and lifetime overview:
    140           1.1  riastrad  *
    141           1.1  riastrad  * On the export the dma_buf holds a reference to the exporting GEM
    142           1.1  riastrad  * object. It takes this reference in handle_to_fd_ioctl, when it
    143           1.1  riastrad  * first calls .prime_export and stores the exporting GEM object in
    144           1.1  riastrad  * the dma_buf priv. This reference is released when the dma_buf
    145           1.1  riastrad  * object goes away in the driver .release function.
    146           1.1  riastrad  *
    147           1.1  riastrad  * On the import the importing GEM object holds a reference to the
    148           1.1  riastrad  * dma_buf (which in turn holds a ref to the exporting GEM object).
    149           1.1  riastrad  * It takes that reference in the fd_to_handle ioctl.
    150           1.1  riastrad  * It calls dma_buf_get, creates an attachment to it and stores the
    151           1.1  riastrad  * attachment in the GEM object. When this attachment is destroyed
    152           1.1  riastrad  * when the imported object is destroyed, we remove the attachment
    153           1.1  riastrad  * and drop the reference to the dma_buf.
    154           1.1  riastrad  *
    155           1.1  riastrad  * Thus the chain of references always flows in one direction
    156           1.1  riastrad  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
    157           1.1  riastrad  *
    158           1.1  riastrad  * Self-importing: if userspace is using PRIME as a replacement for flink
    159           1.1  riastrad  * then it will get a fd->handle request for a GEM object that it created.
    160           1.1  riastrad  * Drivers should detect this situation and return back the gem object
    161       1.1.1.2  riastrad  * from the dma-buf private.  Prime will do this automatically for drivers that
    162       1.1.1.2  riastrad  * use the drm_gem_prime_{import,export} helpers.
    163           1.1  riastrad  */
    164           1.1  riastrad 
    165           1.1  riastrad struct drm_prime_member {
    166           1.1  riastrad 	struct list_head entry;
    167           1.1  riastrad 	struct dma_buf *dma_buf;
    168           1.1  riastrad 	uint32_t handle;
    169           1.1  riastrad };
    170           1.1  riastrad 
    171       1.1.1.2  riastrad struct drm_prime_attachment {
    172       1.1.1.2  riastrad 	struct sg_table *sgt;
    173       1.1.1.2  riastrad 	enum dma_data_direction dir;
    174       1.1.1.2  riastrad };
    175       1.1.1.2  riastrad 
    176       1.1.1.2  riastrad static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
    177       1.1.1.2  riastrad 				    struct dma_buf *dma_buf, uint32_t handle)
    178       1.1.1.2  riastrad {
    179       1.1.1.2  riastrad 	struct drm_prime_member *member;
    180       1.1.1.2  riastrad 
    181       1.1.1.2  riastrad 	member = kmalloc(sizeof(*member), GFP_KERNEL);
    182       1.1.1.2  riastrad 	if (!member)
    183       1.1.1.2  riastrad 		return -ENOMEM;
    184       1.1.1.2  riastrad 
    185       1.1.1.2  riastrad 	get_dma_buf(dma_buf);
    186       1.1.1.2  riastrad 	member->dma_buf = dma_buf;
    187       1.1.1.2  riastrad 	member->handle = handle;
    188       1.1.1.2  riastrad 	list_add(&member->entry, &prime_fpriv->head);
    189       1.1.1.2  riastrad 	return 0;
    190       1.1.1.2  riastrad }
    191       1.1.1.2  riastrad 
    192       1.1.1.2  riastrad static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
    193       1.1.1.2  riastrad 						      uint32_t handle)
    194       1.1.1.2  riastrad {
    195       1.1.1.2  riastrad 	struct drm_prime_member *member;
    196       1.1.1.2  riastrad 
    197       1.1.1.2  riastrad 	list_for_each_entry(member, &prime_fpriv->head, entry) {
    198       1.1.1.2  riastrad 		if (member->handle == handle)
    199       1.1.1.2  riastrad 			return member->dma_buf;
    200       1.1.1.2  riastrad 	}
    201       1.1.1.2  riastrad 
    202       1.1.1.2  riastrad 	return NULL;
    203       1.1.1.2  riastrad }
    204       1.1.1.2  riastrad 
    205       1.1.1.2  riastrad static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
    206       1.1.1.2  riastrad 				       struct dma_buf *dma_buf,
    207       1.1.1.2  riastrad 				       uint32_t *handle)
    208       1.1.1.2  riastrad {
    209       1.1.1.2  riastrad 	struct drm_prime_member *member;
    210       1.1.1.2  riastrad 
    211       1.1.1.2  riastrad 	list_for_each_entry(member, &prime_fpriv->head, entry) {
    212       1.1.1.2  riastrad 		if (member->dma_buf == dma_buf) {
    213       1.1.1.2  riastrad 			*handle = member->handle;
    214       1.1.1.2  riastrad 			return 0;
    215       1.1.1.2  riastrad 		}
    216       1.1.1.2  riastrad 	}
    217       1.1.1.2  riastrad 	return -ENOENT;
    218       1.1.1.2  riastrad }
    219       1.1.1.2  riastrad 
    220       1.1.1.2  riastrad static int drm_gem_map_attach(struct dma_buf *dma_buf,
    221       1.1.1.2  riastrad 			      struct device *target_dev,
    222       1.1.1.2  riastrad 			      struct dma_buf_attachment *attach)
    223       1.1.1.2  riastrad {
    224       1.1.1.2  riastrad 	struct drm_prime_attachment *prime_attach;
    225       1.1.1.2  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    226       1.1.1.2  riastrad 	struct drm_device *dev = obj->dev;
    227       1.1.1.2  riastrad 
    228       1.1.1.2  riastrad 	prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
    229       1.1.1.2  riastrad 	if (!prime_attach)
    230       1.1.1.2  riastrad 		return -ENOMEM;
    231       1.1.1.2  riastrad 
    232       1.1.1.2  riastrad 	prime_attach->dir = DMA_NONE;
    233       1.1.1.2  riastrad 	attach->priv = prime_attach;
    234       1.1.1.2  riastrad 
    235       1.1.1.2  riastrad 	if (!dev->driver->gem_prime_pin)
    236       1.1.1.2  riastrad 		return 0;
    237       1.1.1.2  riastrad 
    238       1.1.1.2  riastrad 	return dev->driver->gem_prime_pin(obj);
    239       1.1.1.2  riastrad }
    240       1.1.1.2  riastrad 
    241       1.1.1.2  riastrad static void drm_gem_map_detach(struct dma_buf *dma_buf,
    242       1.1.1.2  riastrad 			       struct dma_buf_attachment *attach)
    243       1.1.1.2  riastrad {
    244       1.1.1.2  riastrad 	struct drm_prime_attachment *prime_attach = attach->priv;
    245       1.1.1.2  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    246       1.1.1.2  riastrad 	struct drm_device *dev = obj->dev;
    247       1.1.1.2  riastrad 	struct sg_table *sgt;
    248       1.1.1.2  riastrad 
    249       1.1.1.2  riastrad 	if (dev->driver->gem_prime_unpin)
    250       1.1.1.2  riastrad 		dev->driver->gem_prime_unpin(obj);
    251       1.1.1.2  riastrad 
    252       1.1.1.2  riastrad 	if (!prime_attach)
    253       1.1.1.2  riastrad 		return;
    254       1.1.1.2  riastrad 
    255       1.1.1.2  riastrad 	sgt = prime_attach->sgt;
    256       1.1.1.2  riastrad 	if (sgt) {
    257  1.1.1.2.30.1  christos #ifndef __NetBSD__		/* We map/unmap elsewhere.  */
    258       1.1.1.2  riastrad 		if (prime_attach->dir != DMA_NONE)
    259       1.1.1.2  riastrad 			dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
    260       1.1.1.2  riastrad 					prime_attach->dir);
    261  1.1.1.2.30.1  christos #endif
    262       1.1.1.2  riastrad 		sg_free_table(sgt);
    263       1.1.1.2  riastrad 	}
    264       1.1.1.2  riastrad 
    265       1.1.1.2  riastrad 	kfree(sgt);
    266       1.1.1.2  riastrad 	kfree(prime_attach);
    267       1.1.1.2  riastrad 	attach->priv = NULL;
    268       1.1.1.2  riastrad }
    269       1.1.1.2  riastrad 
    270       1.1.1.2  riastrad void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
    271       1.1.1.2  riastrad 					struct dma_buf *dma_buf)
    272       1.1.1.2  riastrad {
    273       1.1.1.2  riastrad 	struct drm_prime_member *member, *safe;
    274       1.1.1.2  riastrad 
    275       1.1.1.2  riastrad 	list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
    276       1.1.1.2  riastrad 		if (member->dma_buf == dma_buf) {
    277       1.1.1.2  riastrad 			dma_buf_put(dma_buf);
    278       1.1.1.2  riastrad 			list_del(&member->entry);
    279       1.1.1.2  riastrad 			kfree(member);
    280       1.1.1.2  riastrad 		}
    281       1.1.1.2  riastrad 	}
    282       1.1.1.2  riastrad }
    283       1.1.1.2  riastrad 
    284       1.1.1.2  riastrad static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
    285  1.1.1.2.30.1  christos 						enum dma_data_direction dir)
    286       1.1.1.2  riastrad {
    287       1.1.1.2  riastrad 	struct drm_prime_attachment *prime_attach = attach->priv;
    288       1.1.1.2  riastrad 	struct drm_gem_object *obj = attach->dmabuf->priv;
    289       1.1.1.2  riastrad 	struct sg_table *sgt;
    290       1.1.1.2  riastrad 
    291       1.1.1.2  riastrad 	if (WARN_ON(dir == DMA_NONE || !prime_attach))
    292       1.1.1.2  riastrad 		return ERR_PTR(-EINVAL);
    293       1.1.1.2  riastrad 
    294       1.1.1.2  riastrad 	/* return the cached mapping when possible */
    295       1.1.1.2  riastrad 	if (prime_attach->dir == dir)
    296       1.1.1.2  riastrad 		return prime_attach->sgt;
    297       1.1.1.2  riastrad 
    298       1.1.1.2  riastrad 	/*
    299       1.1.1.2  riastrad 	 * two mappings with different directions for the same attachment are
    300       1.1.1.2  riastrad 	 * not allowed
    301       1.1.1.2  riastrad 	 */
    302       1.1.1.2  riastrad 	if (WARN_ON(prime_attach->dir != DMA_NONE))
    303       1.1.1.2  riastrad 		return ERR_PTR(-EBUSY);
    304       1.1.1.2  riastrad 
    305       1.1.1.2  riastrad 	sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
    306       1.1.1.2  riastrad 	if (!IS_ERR(sgt)) {
    307  1.1.1.2.30.1  christos #ifdef __NetBSD__		/* We map/unmap elsewhere.  */
    308  1.1.1.2.30.1  christos 		prime_attach->sgt = sgt;
    309  1.1.1.2.30.1  christos 		prime_attach->dir = dir;
    310  1.1.1.2.30.1  christos #else
    311       1.1.1.2  riastrad 		if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
    312       1.1.1.2  riastrad 			sg_free_table(sgt);
    313       1.1.1.2  riastrad 			kfree(sgt);
    314       1.1.1.2  riastrad 			sgt = ERR_PTR(-ENOMEM);
    315       1.1.1.2  riastrad 		} else {
    316       1.1.1.2  riastrad 			prime_attach->sgt = sgt;
    317       1.1.1.2  riastrad 			prime_attach->dir = dir;
    318       1.1.1.2  riastrad 		}
    319  1.1.1.2.30.1  christos #endif
    320       1.1.1.2  riastrad 	}
    321       1.1.1.2  riastrad 
    322       1.1.1.2  riastrad 	return sgt;
    323       1.1.1.2  riastrad }
    324       1.1.1.2  riastrad 
    325       1.1.1.2  riastrad static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
    326       1.1.1.2  riastrad 				  struct sg_table *sgt,
    327       1.1.1.2  riastrad 				  enum dma_data_direction dir)
    328       1.1.1.2  riastrad {
    329       1.1.1.2  riastrad 	/* nothing to be done here */
    330       1.1.1.2  riastrad }
    331       1.1.1.2  riastrad 
    332       1.1.1.2  riastrad /**
    333       1.1.1.2  riastrad  * drm_gem_dmabuf_release - dma_buf release implementation for GEM
    334       1.1.1.2  riastrad  * @dma_buf: buffer to be released
    335       1.1.1.2  riastrad  *
    336       1.1.1.2  riastrad  * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
    337       1.1.1.2  riastrad  * must use this in their dma_buf ops structure as the release callback.
    338       1.1.1.2  riastrad  */
    339       1.1.1.2  riastrad void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
    340       1.1.1.2  riastrad {
    341       1.1.1.2  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    342       1.1.1.2  riastrad 
    343       1.1.1.2  riastrad 	/* drop the reference on the export fd holds */
    344       1.1.1.2  riastrad 	drm_gem_object_unreference_unlocked(obj);
    345       1.1.1.2  riastrad }
    346       1.1.1.2  riastrad EXPORT_SYMBOL(drm_gem_dmabuf_release);
    347       1.1.1.2  riastrad 
    348       1.1.1.2  riastrad static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
    349       1.1.1.2  riastrad {
    350       1.1.1.2  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    351       1.1.1.2  riastrad 	struct drm_device *dev = obj->dev;
    352       1.1.1.2  riastrad 
    353       1.1.1.2  riastrad 	return dev->driver->gem_prime_vmap(obj);
    354       1.1.1.2  riastrad }
    355       1.1.1.2  riastrad 
    356       1.1.1.2  riastrad static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
    357       1.1.1.2  riastrad {
    358       1.1.1.2  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    359       1.1.1.2  riastrad 	struct drm_device *dev = obj->dev;
    360       1.1.1.2  riastrad 
    361       1.1.1.2  riastrad 	dev->driver->gem_prime_vunmap(obj, vaddr);
    362       1.1.1.2  riastrad }
    363       1.1.1.2  riastrad 
    364       1.1.1.2  riastrad static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
    365       1.1.1.2  riastrad 					unsigned long page_num)
    366       1.1.1.2  riastrad {
    367       1.1.1.2  riastrad 	return NULL;
    368       1.1.1.2  riastrad }
    369       1.1.1.2  riastrad 
    370       1.1.1.2  riastrad static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
    371       1.1.1.2  riastrad 					 unsigned long page_num, void *addr)
    372       1.1.1.2  riastrad {
    373       1.1.1.2  riastrad 
    374       1.1.1.2  riastrad }
    375       1.1.1.2  riastrad static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
    376       1.1.1.2  riastrad 				 unsigned long page_num)
    377       1.1.1.2  riastrad {
    378       1.1.1.2  riastrad 	return NULL;
    379       1.1.1.2  riastrad }
    380       1.1.1.2  riastrad 
    381       1.1.1.2  riastrad static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
    382       1.1.1.2  riastrad 				  unsigned long page_num, void *addr)
    383       1.1.1.2  riastrad {
    384       1.1.1.2  riastrad 
    385       1.1.1.2  riastrad }
    386       1.1.1.2  riastrad 
    387  1.1.1.2.30.1  christos #ifdef __NetBSD__
    388  1.1.1.2.30.1  christos static int
    389  1.1.1.2.30.1  christos drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, off_t *offp, size_t size,
    390  1.1.1.2.30.1  christos     int prot, int *flagsp, int *advicep, struct uvm_object **uobjp,
    391  1.1.1.2.30.1  christos     int *maxprotp)
    392  1.1.1.2.30.1  christos #else
    393       1.1.1.2  riastrad static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
    394       1.1.1.2  riastrad 			       struct vm_area_struct *vma)
    395  1.1.1.2.30.1  christos #endif
    396       1.1.1.2  riastrad {
    397       1.1.1.2  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    398       1.1.1.2  riastrad 	struct drm_device *dev = obj->dev;
    399       1.1.1.2  riastrad 
    400       1.1.1.2  riastrad 	if (!dev->driver->gem_prime_mmap)
    401       1.1.1.2  riastrad 		return -ENOSYS;
    402       1.1.1.2  riastrad 
    403  1.1.1.2.30.1  christos #ifdef __NetBSD__
    404  1.1.1.2.30.1  christos 	return dev->driver->gem_prime_mmap(obj, offp, size, prot, flagsp,
    405  1.1.1.2.30.1  christos 	    advicep, uobjp, maxprotp);
    406  1.1.1.2.30.1  christos #else
    407       1.1.1.2  riastrad 	return dev->driver->gem_prime_mmap(obj, vma);
    408  1.1.1.2.30.1  christos #endif
    409       1.1.1.2  riastrad }
    410       1.1.1.2  riastrad 
    411       1.1.1.2  riastrad static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
    412       1.1.1.2  riastrad 	.attach = drm_gem_map_attach,
    413       1.1.1.2  riastrad 	.detach = drm_gem_map_detach,
    414       1.1.1.2  riastrad 	.map_dma_buf = drm_gem_map_dma_buf,
    415       1.1.1.2  riastrad 	.unmap_dma_buf = drm_gem_unmap_dma_buf,
    416       1.1.1.2  riastrad 	.release = drm_gem_dmabuf_release,
    417       1.1.1.2  riastrad 	.kmap = drm_gem_dmabuf_kmap,
    418       1.1.1.2  riastrad 	.kmap_atomic = drm_gem_dmabuf_kmap_atomic,
    419       1.1.1.2  riastrad 	.kunmap = drm_gem_dmabuf_kunmap,
    420       1.1.1.2  riastrad 	.kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
    421       1.1.1.2  riastrad 	.mmap = drm_gem_dmabuf_mmap,
    422       1.1.1.2  riastrad 	.vmap = drm_gem_dmabuf_vmap,
    423       1.1.1.2  riastrad 	.vunmap = drm_gem_dmabuf_vunmap,
    424       1.1.1.2  riastrad };
    425       1.1.1.2  riastrad 
    426       1.1.1.2  riastrad /**
    427       1.1.1.2  riastrad  * DOC: PRIME Helpers
    428       1.1.1.2  riastrad  *
    429       1.1.1.2  riastrad  * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
    430       1.1.1.2  riastrad  * simpler APIs by using the helper functions @drm_gem_prime_export and
    431       1.1.1.2  riastrad  * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
    432  1.1.1.2.30.1  christos  * six lower-level driver callbacks:
    433       1.1.1.2  riastrad  *
    434       1.1.1.2  riastrad  * Export callbacks:
    435       1.1.1.2  riastrad  *
    436       1.1.1.2  riastrad  *  - @gem_prime_pin (optional): prepare a GEM object for exporting
    437       1.1.1.2  riastrad  *
    438       1.1.1.2  riastrad  *  - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
    439       1.1.1.2  riastrad  *
    440       1.1.1.2  riastrad  *  - @gem_prime_vmap: vmap a buffer exported by your driver
    441       1.1.1.2  riastrad  *
    442       1.1.1.2  riastrad  *  - @gem_prime_vunmap: vunmap a buffer exported by your driver
    443       1.1.1.2  riastrad  *
    444  1.1.1.2.30.1  christos  *  - @gem_prime_mmap (optional): mmap a buffer exported by your driver
    445  1.1.1.2.30.1  christos  *
    446       1.1.1.2  riastrad  * Import callback:
    447       1.1.1.2  riastrad  *
    448       1.1.1.2  riastrad  *  - @gem_prime_import_sg_table (import): produce a GEM object from another
    449       1.1.1.2  riastrad  *    driver's scatter/gather table
    450       1.1.1.2  riastrad  */
    451       1.1.1.2  riastrad 
    452       1.1.1.2  riastrad /**
    453  1.1.1.2.30.1  christos  * drm_gem_prime_export - helper library implementation of the export callback
    454       1.1.1.2  riastrad  * @dev: drm_device to export from
    455       1.1.1.2  riastrad  * @obj: GEM object to export
    456       1.1.1.2  riastrad  * @flags: flags like DRM_CLOEXEC
    457       1.1.1.2  riastrad  *
    458       1.1.1.2  riastrad  * This is the implementation of the gem_prime_export functions for GEM drivers
    459       1.1.1.2  riastrad  * using the PRIME helpers.
    460       1.1.1.2  riastrad  */
    461       1.1.1.2  riastrad struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
    462  1.1.1.2.30.1  christos 				     struct drm_gem_object *obj,
    463  1.1.1.2.30.1  christos 				     int flags)
    464       1.1.1.2  riastrad {
    465  1.1.1.2.30.1  christos 	struct dma_buf_export_info exp_info = {
    466  1.1.1.2.30.1  christos #ifndef __NetBSD__
    467  1.1.1.2.30.1  christos 		.exp_name = KBUILD_MODNAME, /* white lie for debug */
    468  1.1.1.2.30.1  christos 		.owner = dev->driver->fops->owner,
    469  1.1.1.2.30.1  christos #endif
    470  1.1.1.2.30.1  christos 		.ops = &drm_gem_prime_dmabuf_ops,
    471  1.1.1.2.30.1  christos 		.size = obj->size,
    472  1.1.1.2.30.1  christos 		.flags = flags,
    473  1.1.1.2.30.1  christos 		.priv = obj,
    474  1.1.1.2.30.1  christos 	};
    475  1.1.1.2.30.1  christos 
    476  1.1.1.2.30.1  christos 	if (dev->driver->gem_prime_res_obj)
    477  1.1.1.2.30.1  christos 		exp_info.resv = dev->driver->gem_prime_res_obj(obj);
    478  1.1.1.2.30.1  christos 
    479  1.1.1.2.30.1  christos 	return dma_buf_export(&exp_info);
    480       1.1.1.2  riastrad }
    481       1.1.1.2  riastrad EXPORT_SYMBOL(drm_gem_prime_export);
    482       1.1.1.2  riastrad 
    483       1.1.1.2  riastrad static struct dma_buf *export_and_register_object(struct drm_device *dev,
    484       1.1.1.2  riastrad 						  struct drm_gem_object *obj,
    485       1.1.1.2  riastrad 						  uint32_t flags)
    486       1.1.1.2  riastrad {
    487       1.1.1.2  riastrad 	struct dma_buf *dmabuf;
    488       1.1.1.2  riastrad 
    489       1.1.1.2  riastrad 	/* prevent races with concurrent gem_close. */
    490       1.1.1.2  riastrad 	if (obj->handle_count == 0) {
    491       1.1.1.2  riastrad 		dmabuf = ERR_PTR(-ENOENT);
    492       1.1.1.2  riastrad 		return dmabuf;
    493       1.1.1.2  riastrad 	}
    494       1.1.1.2  riastrad 
    495       1.1.1.2  riastrad 	dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
    496       1.1.1.2  riastrad 	if (IS_ERR(dmabuf)) {
    497       1.1.1.2  riastrad 		/* normally the created dma-buf takes ownership of the ref,
    498       1.1.1.2  riastrad 		 * but if that fails then drop the ref
    499       1.1.1.2  riastrad 		 */
    500       1.1.1.2  riastrad 		return dmabuf;
    501       1.1.1.2  riastrad 	}
    502       1.1.1.2  riastrad 
    503       1.1.1.2  riastrad 	/*
    504       1.1.1.2  riastrad 	 * Note that callers do not need to clean up the export cache
    505       1.1.1.2  riastrad 	 * since the check for obj->handle_count guarantees that someone
    506       1.1.1.2  riastrad 	 * will clean it up.
    507       1.1.1.2  riastrad 	 */
    508       1.1.1.2  riastrad 	obj->dma_buf = dmabuf;
    509       1.1.1.2  riastrad 	get_dma_buf(obj->dma_buf);
    510       1.1.1.2  riastrad 	/* Grab a new ref since the callers is now used by the dma-buf */
    511       1.1.1.2  riastrad 	drm_gem_object_reference(obj);
    512       1.1.1.2  riastrad 
    513       1.1.1.2  riastrad 	return dmabuf;
    514       1.1.1.2  riastrad }
    515       1.1.1.2  riastrad 
    516       1.1.1.2  riastrad /**
    517       1.1.1.2  riastrad  * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
    518       1.1.1.2  riastrad  * @dev: dev to export the buffer from
    519       1.1.1.2  riastrad  * @file_priv: drm file-private structure
    520       1.1.1.2  riastrad  * @handle: buffer handle to export
    521       1.1.1.2  riastrad  * @flags: flags like DRM_CLOEXEC
    522       1.1.1.2  riastrad  * @prime_fd: pointer to storage for the fd id of the create dma-buf
    523       1.1.1.2  riastrad  *
    524       1.1.1.2  riastrad  * This is the PRIME export function which must be used mandatorily by GEM
    525       1.1.1.2  riastrad  * drivers to ensure correct lifetime management of the underlying GEM object.
    526       1.1.1.2  riastrad  * The actual exporting from GEM object to a dma-buf is done through the
    527       1.1.1.2  riastrad  * gem_prime_export driver callback.
    528       1.1.1.2  riastrad  */
    529           1.1  riastrad int drm_gem_prime_handle_to_fd(struct drm_device *dev,
    530       1.1.1.2  riastrad 			       struct drm_file *file_priv, uint32_t handle,
    531       1.1.1.2  riastrad 			       uint32_t flags,
    532       1.1.1.2  riastrad 			       int *prime_fd)
    533           1.1  riastrad {
    534           1.1  riastrad 	struct drm_gem_object *obj;
    535       1.1.1.2  riastrad 	int ret = 0;
    536       1.1.1.2  riastrad 	struct dma_buf *dmabuf;
    537           1.1  riastrad 
    538       1.1.1.2  riastrad 	mutex_lock(&file_priv->prime.lock);
    539           1.1  riastrad 	obj = drm_gem_object_lookup(dev, file_priv, handle);
    540       1.1.1.2  riastrad 	if (!obj)  {
    541       1.1.1.2  riastrad 		ret = -ENOENT;
    542       1.1.1.2  riastrad 		goto out_unlock;
    543       1.1.1.2  riastrad 	}
    544           1.1  riastrad 
    545       1.1.1.2  riastrad 	dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
    546       1.1.1.2  riastrad 	if (dmabuf) {
    547       1.1.1.2  riastrad 		get_dma_buf(dmabuf);
    548       1.1.1.2  riastrad 		goto out_have_handle;
    549       1.1.1.2  riastrad 	}
    550       1.1.1.2  riastrad 
    551       1.1.1.2  riastrad 	mutex_lock(&dev->object_name_lock);
    552           1.1  riastrad 	/* re-export the original imported object */
    553           1.1  riastrad 	if (obj->import_attach) {
    554       1.1.1.2  riastrad 		dmabuf = obj->import_attach->dmabuf;
    555       1.1.1.2  riastrad 		get_dma_buf(dmabuf);
    556       1.1.1.2  riastrad 		goto out_have_obj;
    557           1.1  riastrad 	}
    558           1.1  riastrad 
    559       1.1.1.2  riastrad 	if (obj->dma_buf) {
    560       1.1.1.2  riastrad 		get_dma_buf(obj->dma_buf);
    561       1.1.1.2  riastrad 		dmabuf = obj->dma_buf;
    562       1.1.1.2  riastrad 		goto out_have_obj;
    563           1.1  riastrad 	}
    564       1.1.1.2  riastrad 
    565       1.1.1.2  riastrad 	dmabuf = export_and_register_object(dev, obj, flags);
    566       1.1.1.2  riastrad 	if (IS_ERR(dmabuf)) {
    567       1.1.1.2  riastrad 		/* normally the created dma-buf takes ownership of the ref,
    568       1.1.1.2  riastrad 		 * but if that fails then drop the ref
    569       1.1.1.2  riastrad 		 */
    570       1.1.1.2  riastrad 		ret = PTR_ERR(dmabuf);
    571       1.1.1.2  riastrad 		mutex_unlock(&dev->object_name_lock);
    572       1.1.1.2  riastrad 		goto out;
    573       1.1.1.2  riastrad 	}
    574       1.1.1.2  riastrad 
    575       1.1.1.2  riastrad out_have_obj:
    576       1.1.1.2  riastrad 	/*
    577       1.1.1.2  riastrad 	 * If we've exported this buffer then cheat and add it to the import list
    578       1.1.1.2  riastrad 	 * so we get the correct handle back. We must do this under the
    579       1.1.1.2  riastrad 	 * protection of dev->object_name_lock to ensure that a racing gem close
    580       1.1.1.2  riastrad 	 * ioctl doesn't miss to remove this buffer handle from the cache.
    581       1.1.1.2  riastrad 	 */
    582       1.1.1.2  riastrad 	ret = drm_prime_add_buf_handle(&file_priv->prime,
    583       1.1.1.2  riastrad 				       dmabuf, handle);
    584       1.1.1.2  riastrad 	mutex_unlock(&dev->object_name_lock);
    585       1.1.1.2  riastrad 	if (ret)
    586       1.1.1.2  riastrad 		goto fail_put_dmabuf;
    587       1.1.1.2  riastrad 
    588       1.1.1.2  riastrad out_have_handle:
    589       1.1.1.2  riastrad 	ret = dma_buf_fd(dmabuf, flags);
    590       1.1.1.2  riastrad 	/*
    591       1.1.1.2  riastrad 	 * We must _not_ remove the buffer from the handle cache since the newly
    592       1.1.1.2  riastrad 	 * created dma buf is already linked in the global obj->dma_buf pointer,
    593       1.1.1.2  riastrad 	 * and that is invariant as long as a userspace gem handle exists.
    594       1.1.1.2  riastrad 	 * Closing the handle will clean out the cache anyway, so we don't leak.
    595           1.1  riastrad 	 */
    596       1.1.1.2  riastrad 	if (ret < 0) {
    597       1.1.1.2  riastrad 		goto fail_put_dmabuf;
    598       1.1.1.2  riastrad 	} else {
    599       1.1.1.2  riastrad 		*prime_fd = ret;
    600       1.1.1.2  riastrad 		ret = 0;
    601           1.1  riastrad 	}
    602           1.1  riastrad 
    603       1.1.1.2  riastrad 	goto out;
    604       1.1.1.2  riastrad 
    605       1.1.1.2  riastrad fail_put_dmabuf:
    606       1.1.1.2  riastrad 	dma_buf_put(dmabuf);
    607       1.1.1.2  riastrad out:
    608       1.1.1.2  riastrad 	drm_gem_object_unreference_unlocked(obj);
    609       1.1.1.2  riastrad out_unlock:
    610           1.1  riastrad 	mutex_unlock(&file_priv->prime.lock);
    611       1.1.1.2  riastrad 
    612       1.1.1.2  riastrad 	return ret;
    613           1.1  riastrad }
    614           1.1  riastrad EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
    615           1.1  riastrad 
    616       1.1.1.2  riastrad /**
    617  1.1.1.2.30.1  christos  * drm_gem_prime_import - helper library implementation of the import callback
    618       1.1.1.2  riastrad  * @dev: drm_device to import into
    619       1.1.1.2  riastrad  * @dma_buf: dma-buf object to import
    620       1.1.1.2  riastrad  *
    621       1.1.1.2  riastrad  * This is the implementation of the gem_prime_import functions for GEM drivers
    622       1.1.1.2  riastrad  * using the PRIME helpers.
    623       1.1.1.2  riastrad  */
    624       1.1.1.2  riastrad struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
    625       1.1.1.2  riastrad 					    struct dma_buf *dma_buf)
    626       1.1.1.2  riastrad {
    627       1.1.1.2  riastrad 	struct dma_buf_attachment *attach;
    628       1.1.1.2  riastrad 	struct sg_table *sgt;
    629       1.1.1.2  riastrad 	struct drm_gem_object *obj;
    630       1.1.1.2  riastrad 	int ret;
    631       1.1.1.2  riastrad 
    632       1.1.1.2  riastrad 	if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
    633       1.1.1.2  riastrad 		obj = dma_buf->priv;
    634       1.1.1.2  riastrad 		if (obj->dev == dev) {
    635       1.1.1.2  riastrad 			/*
    636       1.1.1.2  riastrad 			 * Importing dmabuf exported from out own gem increases
    637       1.1.1.2  riastrad 			 * refcount on gem itself instead of f_count of dmabuf.
    638       1.1.1.2  riastrad 			 */
    639       1.1.1.2  riastrad 			drm_gem_object_reference(obj);
    640       1.1.1.2  riastrad 			return obj;
    641       1.1.1.2  riastrad 		}
    642       1.1.1.2  riastrad 	}
    643       1.1.1.2  riastrad 
    644  1.1.1.2.30.1  christos 	if (!dev->driver->gem_prime_import_sg_table)
    645  1.1.1.2.30.1  christos 		return ERR_PTR(-EINVAL);
    646  1.1.1.2.30.1  christos 
    647       1.1.1.2  riastrad 	attach = dma_buf_attach(dma_buf, dev->dev);
    648       1.1.1.2  riastrad 	if (IS_ERR(attach))
    649       1.1.1.2  riastrad 		return ERR_CAST(attach);
    650       1.1.1.2  riastrad 
    651       1.1.1.2  riastrad 	get_dma_buf(dma_buf);
    652       1.1.1.2  riastrad 
    653       1.1.1.2  riastrad 	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
    654       1.1.1.2  riastrad 	if (IS_ERR(sgt)) {
    655       1.1.1.2  riastrad 		ret = PTR_ERR(sgt);
    656       1.1.1.2  riastrad 		goto fail_detach;
    657       1.1.1.2  riastrad 	}
    658       1.1.1.2  riastrad 
    659  1.1.1.2.30.1  christos 	obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
    660       1.1.1.2  riastrad 	if (IS_ERR(obj)) {
    661       1.1.1.2  riastrad 		ret = PTR_ERR(obj);
    662       1.1.1.2  riastrad 		goto fail_unmap;
    663       1.1.1.2  riastrad 	}
    664       1.1.1.2  riastrad 
    665       1.1.1.2  riastrad 	obj->import_attach = attach;
    666       1.1.1.2  riastrad 
    667       1.1.1.2  riastrad 	return obj;
    668       1.1.1.2  riastrad 
    669       1.1.1.2  riastrad fail_unmap:
    670       1.1.1.2  riastrad 	dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
    671       1.1.1.2  riastrad fail_detach:
    672       1.1.1.2  riastrad 	dma_buf_detach(dma_buf, attach);
    673       1.1.1.2  riastrad 	dma_buf_put(dma_buf);
    674       1.1.1.2  riastrad 
    675       1.1.1.2  riastrad 	return ERR_PTR(ret);
    676       1.1.1.2  riastrad }
    677       1.1.1.2  riastrad EXPORT_SYMBOL(drm_gem_prime_import);
    678       1.1.1.2  riastrad 
    679       1.1.1.2  riastrad /**
    680       1.1.1.2  riastrad  * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
    681       1.1.1.2  riastrad  * @dev: dev to export the buffer from
    682       1.1.1.2  riastrad  * @file_priv: drm file-private structure
    683       1.1.1.2  riastrad  * @prime_fd: fd id of the dma-buf which should be imported
    684       1.1.1.2  riastrad  * @handle: pointer to storage for the handle of the imported buffer object
    685       1.1.1.2  riastrad  *
    686       1.1.1.2  riastrad  * This is the PRIME import function which must be used mandatorily by GEM
    687       1.1.1.2  riastrad  * drivers to ensure correct lifetime management of the underlying GEM object.
    688       1.1.1.2  riastrad  * The actual importing of GEM object from the dma-buf is done through the
    689       1.1.1.2  riastrad  * gem_import_export driver callback.
    690       1.1.1.2  riastrad  */
    691           1.1  riastrad int drm_gem_prime_fd_to_handle(struct drm_device *dev,
    692       1.1.1.2  riastrad 			       struct drm_file *file_priv, int prime_fd,
    693       1.1.1.2  riastrad 			       uint32_t *handle)
    694           1.1  riastrad {
    695           1.1  riastrad 	struct dma_buf *dma_buf;
    696           1.1  riastrad 	struct drm_gem_object *obj;
    697           1.1  riastrad 	int ret;
    698           1.1  riastrad 
    699           1.1  riastrad 	dma_buf = dma_buf_get(prime_fd);
    700           1.1  riastrad 	if (IS_ERR(dma_buf))
    701           1.1  riastrad 		return PTR_ERR(dma_buf);
    702           1.1  riastrad 
    703           1.1  riastrad 	mutex_lock(&file_priv->prime.lock);
    704           1.1  riastrad 
    705       1.1.1.2  riastrad 	ret = drm_prime_lookup_buf_handle(&file_priv->prime,
    706           1.1  riastrad 			dma_buf, handle);
    707       1.1.1.2  riastrad 	if (ret == 0)
    708           1.1  riastrad 		goto out_put;
    709           1.1  riastrad 
    710           1.1  riastrad 	/* never seen this one, need to import */
    711       1.1.1.2  riastrad 	mutex_lock(&dev->object_name_lock);
    712           1.1  riastrad 	obj = dev->driver->gem_prime_import(dev, dma_buf);
    713           1.1  riastrad 	if (IS_ERR(obj)) {
    714           1.1  riastrad 		ret = PTR_ERR(obj);
    715       1.1.1.2  riastrad 		goto out_unlock;
    716           1.1  riastrad 	}
    717           1.1  riastrad 
    718       1.1.1.2  riastrad 	if (obj->dma_buf) {
    719       1.1.1.2  riastrad 		WARN_ON(obj->dma_buf != dma_buf);
    720       1.1.1.2  riastrad 	} else {
    721       1.1.1.2  riastrad 		obj->dma_buf = dma_buf;
    722       1.1.1.2  riastrad 		get_dma_buf(dma_buf);
    723       1.1.1.2  riastrad 	}
    724       1.1.1.2  riastrad 
    725       1.1.1.2  riastrad 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
    726       1.1.1.2  riastrad 	ret = drm_gem_handle_create_tail(file_priv, obj, handle);
    727           1.1  riastrad 	drm_gem_object_unreference_unlocked(obj);
    728           1.1  riastrad 	if (ret)
    729           1.1  riastrad 		goto out_put;
    730           1.1  riastrad 
    731       1.1.1.2  riastrad 	ret = drm_prime_add_buf_handle(&file_priv->prime,
    732           1.1  riastrad 			dma_buf, *handle);
    733           1.1  riastrad 	if (ret)
    734           1.1  riastrad 		goto fail;
    735           1.1  riastrad 
    736           1.1  riastrad 	mutex_unlock(&file_priv->prime.lock);
    737       1.1.1.2  riastrad 
    738       1.1.1.2  riastrad 	dma_buf_put(dma_buf);
    739       1.1.1.2  riastrad 
    740           1.1  riastrad 	return 0;
    741           1.1  riastrad 
    742           1.1  riastrad fail:
    743           1.1  riastrad 	/* hmm, if driver attached, we are relying on the free-object path
    744           1.1  riastrad 	 * to detach.. which seems ok..
    745           1.1  riastrad 	 */
    746       1.1.1.2  riastrad 	drm_gem_handle_delete(file_priv, *handle);
    747       1.1.1.2  riastrad out_unlock:
    748       1.1.1.2  riastrad 	mutex_unlock(&dev->object_name_lock);
    749           1.1  riastrad out_put:
    750           1.1  riastrad 	dma_buf_put(dma_buf);
    751           1.1  riastrad 	mutex_unlock(&file_priv->prime.lock);
    752           1.1  riastrad 	return ret;
    753           1.1  riastrad }
    754           1.1  riastrad EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
    755           1.1  riastrad 
    756           1.1  riastrad int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
    757           1.1  riastrad 				 struct drm_file *file_priv)
    758           1.1  riastrad {
    759           1.1  riastrad 	struct drm_prime_handle *args = data;
    760           1.1  riastrad 	uint32_t flags;
    761           1.1  riastrad 
    762           1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_PRIME))
    763           1.1  riastrad 		return -EINVAL;
    764           1.1  riastrad 
    765           1.1  riastrad 	if (!dev->driver->prime_handle_to_fd)
    766           1.1  riastrad 		return -ENOSYS;
    767           1.1  riastrad 
    768           1.1  riastrad 	/* check flags are valid */
    769           1.1  riastrad 	if (args->flags & ~DRM_CLOEXEC)
    770           1.1  riastrad 		return -EINVAL;
    771           1.1  riastrad 
    772           1.1  riastrad 	/* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
    773           1.1  riastrad 	flags = args->flags & DRM_CLOEXEC;
    774           1.1  riastrad 
    775           1.1  riastrad 	return dev->driver->prime_handle_to_fd(dev, file_priv,
    776           1.1  riastrad 			args->handle, flags, &args->fd);
    777           1.1  riastrad }
    778           1.1  riastrad 
    779           1.1  riastrad int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
    780           1.1  riastrad 				 struct drm_file *file_priv)
    781           1.1  riastrad {
    782           1.1  riastrad 	struct drm_prime_handle *args = data;
    783           1.1  riastrad 
    784           1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_PRIME))
    785           1.1  riastrad 		return -EINVAL;
    786           1.1  riastrad 
    787           1.1  riastrad 	if (!dev->driver->prime_fd_to_handle)
    788           1.1  riastrad 		return -ENOSYS;
    789           1.1  riastrad 
    790           1.1  riastrad 	return dev->driver->prime_fd_to_handle(dev, file_priv,
    791           1.1  riastrad 			args->fd, &args->handle);
    792           1.1  riastrad }
    793           1.1  riastrad 
    794       1.1.1.2  riastrad /**
    795       1.1.1.2  riastrad  * drm_prime_pages_to_sg - converts a page array into an sg list
    796       1.1.1.2  riastrad  * @pages: pointer to the array of page pointers to convert
    797       1.1.1.2  riastrad  * @nr_pages: length of the page vector
    798           1.1  riastrad  *
    799       1.1.1.2  riastrad  * This helper creates an sg table object from a set of pages
    800           1.1  riastrad  * the driver is responsible for mapping the pages into the
    801       1.1.1.2  riastrad  * importers address space for use with dma_buf itself.
    802           1.1  riastrad  */
    803  1.1.1.2.30.1  christos struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
    804           1.1  riastrad {
    805           1.1  riastrad 	struct sg_table *sg = NULL;
    806           1.1  riastrad 	int ret;
    807           1.1  riastrad 
    808           1.1  riastrad 	sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
    809       1.1.1.2  riastrad 	if (!sg) {
    810       1.1.1.2  riastrad 		ret = -ENOMEM;
    811           1.1  riastrad 		goto out;
    812       1.1.1.2  riastrad 	}
    813           1.1  riastrad 
    814       1.1.1.2  riastrad 	ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
    815       1.1.1.2  riastrad 				nr_pages << PAGE_SHIFT, GFP_KERNEL);
    816           1.1  riastrad 	if (ret)
    817           1.1  riastrad 		goto out;
    818           1.1  riastrad 
    819           1.1  riastrad 	return sg;
    820           1.1  riastrad out:
    821           1.1  riastrad 	kfree(sg);
    822       1.1.1.2  riastrad 	return ERR_PTR(ret);
    823           1.1  riastrad }
    824           1.1  riastrad EXPORT_SYMBOL(drm_prime_pages_to_sg);
    825           1.1  riastrad 
    826  1.1.1.2.30.1  christos #ifdef __NetBSD__
    827  1.1.1.2.30.1  christos 
    828  1.1.1.2.30.1  christos struct sg_table *
    829  1.1.1.2.30.1  christos drm_prime_bus_dmamem_to_sg(bus_dma_tag_t dmat, const bus_dma_segment_t *segs,
    830  1.1.1.2.30.1  christos     int nsegs)
    831  1.1.1.2.30.1  christos {
    832  1.1.1.2.30.1  christos 	struct sg_table *sg;
    833  1.1.1.2.30.1  christos 	int ret;
    834  1.1.1.2.30.1  christos 
    835  1.1.1.2.30.1  christos 	sg = kmalloc(sizeof(*sg), GFP_KERNEL);
    836  1.1.1.2.30.1  christos 	if (sg == NULL) {
    837  1.1.1.2.30.1  christos 		ret = -ENOMEM;
    838  1.1.1.2.30.1  christos 		goto out;
    839  1.1.1.2.30.1  christos 	}
    840  1.1.1.2.30.1  christos 
    841  1.1.1.2.30.1  christos 	ret = sg_alloc_table_from_bus_dmamem(sg, dmat, segs, nsegs,
    842  1.1.1.2.30.1  christos 	    GFP_KERNEL);
    843  1.1.1.2.30.1  christos 	if (ret)
    844  1.1.1.2.30.1  christos 		goto out;
    845  1.1.1.2.30.1  christos 
    846  1.1.1.2.30.1  christos 	return sg;
    847  1.1.1.2.30.1  christos out:
    848  1.1.1.2.30.1  christos 	kfree(sg);
    849  1.1.1.2.30.1  christos 	return ERR_PTR(ret);
    850  1.1.1.2.30.1  christos }
    851  1.1.1.2.30.1  christos 
    852  1.1.1.2.30.1  christos struct sg_table *
    853  1.1.1.2.30.1  christos drm_prime_pglist_to_sg(struct pglist *pglist, unsigned npages)
    854  1.1.1.2.30.1  christos {
    855  1.1.1.2.30.1  christos 	struct sg_table *sg;
    856  1.1.1.2.30.1  christos 	int ret;
    857  1.1.1.2.30.1  christos 
    858  1.1.1.2.30.1  christos 	sg = kmalloc(sizeof(*sg), GFP_KERNEL);
    859  1.1.1.2.30.1  christos 	if (sg == NULL) {
    860  1.1.1.2.30.1  christos 		ret = -ENOMEM;
    861  1.1.1.2.30.1  christos 		goto out;
    862  1.1.1.2.30.1  christos 	}
    863  1.1.1.2.30.1  christos 
    864  1.1.1.2.30.1  christos 	ret = sg_alloc_table_from_pglist(sg, pglist, 0, npages << PAGE_SHIFT,
    865  1.1.1.2.30.1  christos 	    npages, GFP_KERNEL);
    866  1.1.1.2.30.1  christos 	if (ret)
    867  1.1.1.2.30.1  christos 		goto out;
    868  1.1.1.2.30.1  christos 
    869  1.1.1.2.30.1  christos 	return sg;
    870  1.1.1.2.30.1  christos 
    871  1.1.1.2.30.1  christos out:
    872  1.1.1.2.30.1  christos 	kfree(sg);
    873  1.1.1.2.30.1  christos 	return ERR_PTR(ret);
    874  1.1.1.2.30.1  christos }
    875  1.1.1.2.30.1  christos 
    876  1.1.1.2.30.1  christos bus_size_t
    877  1.1.1.2.30.1  christos drm_prime_sg_size(struct sg_table *sg)
    878  1.1.1.2.30.1  christos {
    879  1.1.1.2.30.1  christos 
    880  1.1.1.2.30.1  christos 	return sg->sgt_npgs << PAGE_SHIFT;
    881  1.1.1.2.30.1  christos }
    882  1.1.1.2.30.1  christos 
    883  1.1.1.2.30.1  christos void
    884  1.1.1.2.30.1  christos drm_prime_sg_free(struct sg_table *sg)
    885  1.1.1.2.30.1  christos {
    886  1.1.1.2.30.1  christos 
    887  1.1.1.2.30.1  christos 	sg_free_table(sg);
    888  1.1.1.2.30.1  christos 	kfree(sg);
    889  1.1.1.2.30.1  christos }
    890  1.1.1.2.30.1  christos 
    891  1.1.1.2.30.1  christos int
    892  1.1.1.2.30.1  christos drm_prime_sg_to_bus_dmamem(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
    893  1.1.1.2.30.1  christos     int nsegs, int *rsegs, const struct sg_table *sgt)
    894  1.1.1.2.30.1  christos {
    895  1.1.1.2.30.1  christos 
    896  1.1.1.2.30.1  christos 	/* XXX errno NetBSD->Linux */
    897  1.1.1.2.30.1  christos 	return -bus_dmamem_import_pages(dmat, segs, nsegs, rsegs, sgt->sgt_pgs,
    898  1.1.1.2.30.1  christos 	    sgt->sgt_npgs);
    899  1.1.1.2.30.1  christos }
    900  1.1.1.2.30.1  christos 
    901  1.1.1.2.30.1  christos int
    902  1.1.1.2.30.1  christos drm_prime_bus_dmamap_load_sgt(bus_dma_tag_t dmat, bus_dmamap_t map,
    903  1.1.1.2.30.1  christos     struct sg_table *sgt)
    904  1.1.1.2.30.1  christos {
    905  1.1.1.2.30.1  christos 	bus_dma_segment_t *segs;
    906  1.1.1.2.30.1  christos 	bus_size_t size = drm_prime_sg_size(sgt);
    907  1.1.1.2.30.1  christos 	int nsegs = sgt->sgt_npgs;
    908  1.1.1.2.30.1  christos 	int ret;
    909  1.1.1.2.30.1  christos 
    910  1.1.1.2.30.1  christos 	segs = kcalloc(sgt->sgt_npgs, sizeof(segs[0]), GFP_KERNEL);
    911  1.1.1.2.30.1  christos 	if (segs == NULL) {
    912  1.1.1.2.30.1  christos 		ret = -ENOMEM;
    913  1.1.1.2.30.1  christos 		goto out0;
    914  1.1.1.2.30.1  christos 	}
    915  1.1.1.2.30.1  christos 
    916  1.1.1.2.30.1  christos 	ret = drm_prime_sg_to_bus_dmamem(dmat, segs, nsegs, &nsegs, sgt);
    917  1.1.1.2.30.1  christos 	if (ret)
    918  1.1.1.2.30.1  christos 		goto out1;
    919  1.1.1.2.30.1  christos 	KASSERT(nsegs <= sgt->sgt_npgs);
    920  1.1.1.2.30.1  christos 
    921  1.1.1.2.30.1  christos 	/* XXX errno NetBSD->Linux */
    922  1.1.1.2.30.1  christos 	ret = -bus_dmamap_load_raw(dmat, map, segs, nsegs, size,
    923  1.1.1.2.30.1  christos 	    BUS_DMA_NOWAIT);
    924  1.1.1.2.30.1  christos 	if (ret)
    925  1.1.1.2.30.1  christos 		goto out1;
    926  1.1.1.2.30.1  christos 
    927  1.1.1.2.30.1  christos out1:	kfree(segs);
    928  1.1.1.2.30.1  christos out0:	return ret;
    929  1.1.1.2.30.1  christos }
    930  1.1.1.2.30.1  christos 
    931  1.1.1.2.30.1  christos bool
    932  1.1.1.2.30.1  christos drm_prime_sg_importable(bus_dma_tag_t dmat, struct sg_table *sgt)
    933  1.1.1.2.30.1  christos {
    934  1.1.1.2.30.1  christos 	unsigned i;
    935  1.1.1.2.30.1  christos 
    936  1.1.1.2.30.1  christos 	for (i = 0; i < sgt->sgt_npgs; i++) {
    937  1.1.1.2.30.1  christos 		if (bus_dmatag_bounces_paddr(dmat, sgt->sgt_pgs[i]))
    938  1.1.1.2.30.1  christos 			return false;
    939  1.1.1.2.30.1  christos 	}
    940  1.1.1.2.30.1  christos 	return true;
    941  1.1.1.2.30.1  christos }
    942  1.1.1.2.30.1  christos 
    943  1.1.1.2.30.1  christos #else  /* !__NetBSD__ */
    944  1.1.1.2.30.1  christos 
    945       1.1.1.2  riastrad /**
    946       1.1.1.2  riastrad  * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
    947       1.1.1.2  riastrad  * @sgt: scatter-gather table to convert
    948       1.1.1.2  riastrad  * @pages: array of page pointers to store the page array in
    949       1.1.1.2  riastrad  * @addrs: optional array to store the dma bus address of each page
    950       1.1.1.2  riastrad  * @max_pages: size of both the passed-in arrays
    951       1.1.1.2  riastrad  *
    952       1.1.1.2  riastrad  * Exports an sg table into an array of pages and addresses. This is currently
    953       1.1.1.2  riastrad  * required by the TTM driver in order to do correct fault handling.
    954       1.1.1.2  riastrad  */
    955           1.1  riastrad int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
    956           1.1  riastrad 				     dma_addr_t *addrs, int max_pages)
    957           1.1  riastrad {
    958           1.1  riastrad 	unsigned count;
    959           1.1  riastrad 	struct scatterlist *sg;
    960           1.1  riastrad 	struct page *page;
    961       1.1.1.2  riastrad 	u32 len;
    962           1.1  riastrad 	int pg_index;
    963           1.1  riastrad 	dma_addr_t addr;
    964           1.1  riastrad 
    965           1.1  riastrad 	pg_index = 0;
    966           1.1  riastrad 	for_each_sg(sgt->sgl, sg, sgt->nents, count) {
    967           1.1  riastrad 		len = sg->length;
    968           1.1  riastrad 		page = sg_page(sg);
    969           1.1  riastrad 		addr = sg_dma_address(sg);
    970           1.1  riastrad 
    971           1.1  riastrad 		while (len > 0) {
    972           1.1  riastrad 			if (WARN_ON(pg_index >= max_pages))
    973           1.1  riastrad 				return -1;
    974           1.1  riastrad 			pages[pg_index] = page;
    975           1.1  riastrad 			if (addrs)
    976           1.1  riastrad 				addrs[pg_index] = addr;
    977           1.1  riastrad 
    978           1.1  riastrad 			page++;
    979           1.1  riastrad 			addr += PAGE_SIZE;
    980           1.1  riastrad 			len -= PAGE_SIZE;
    981           1.1  riastrad 			pg_index++;
    982           1.1  riastrad 		}
    983           1.1  riastrad 	}
    984           1.1  riastrad 	return 0;
    985           1.1  riastrad }
    986           1.1  riastrad EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
    987       1.1.1.2  riastrad 
    988  1.1.1.2.30.1  christos #endif	/* __NetBSD__ */
    989  1.1.1.2.30.1  christos 
    990       1.1.1.2  riastrad /**
    991       1.1.1.2  riastrad  * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
    992       1.1.1.2  riastrad  * @obj: GEM object which was created from a dma-buf
    993       1.1.1.2  riastrad  * @sg: the sg-table which was pinned at import time
    994       1.1.1.2  riastrad  *
    995       1.1.1.2  riastrad  * This is the cleanup functions which GEM drivers need to call when they use
    996       1.1.1.2  riastrad  * @drm_gem_prime_import to import dma-bufs.
    997       1.1.1.2  riastrad  */
    998           1.1  riastrad void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
    999           1.1  riastrad {
   1000           1.1  riastrad 	struct dma_buf_attachment *attach;
   1001           1.1  riastrad 	struct dma_buf *dma_buf;
   1002           1.1  riastrad 	attach = obj->import_attach;
   1003           1.1  riastrad 	if (sg)
   1004           1.1  riastrad 		dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
   1005           1.1  riastrad 	dma_buf = attach->dmabuf;
   1006           1.1  riastrad 	dma_buf_detach(attach->dmabuf, attach);
   1007           1.1  riastrad 	/* remove the reference */
   1008           1.1  riastrad 	dma_buf_put(dma_buf);
   1009           1.1  riastrad }
   1010           1.1  riastrad EXPORT_SYMBOL(drm_prime_gem_destroy);
   1011           1.1  riastrad 
   1012           1.1  riastrad void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
   1013           1.1  riastrad {
   1014           1.1  riastrad 	INIT_LIST_HEAD(&prime_fpriv->head);
   1015  1.1.1.2.30.1  christos #ifdef __NetBSD__
   1016  1.1.1.2.30.1  christos 	linux_mutex_init(&prime_fpriv->lock);
   1017  1.1.1.2.30.1  christos #else
   1018           1.1  riastrad 	mutex_init(&prime_fpriv->lock);
   1019  1.1.1.2.30.1  christos #endif
   1020           1.1  riastrad }
   1021           1.1  riastrad 
   1022           1.1  riastrad void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
   1023           1.1  riastrad {
   1024       1.1.1.2  riastrad 	/* by now drm_gem_release should've made sure the list is empty */
   1025       1.1.1.2  riastrad 	WARN_ON(!list_empty(&prime_fpriv->head));
   1026  1.1.1.2.30.1  christos #ifdef __NetBSD__
   1027  1.1.1.2.30.1  christos 	linux_mutex_destroy(&prime_fpriv->lock);
   1028  1.1.1.2.30.1  christos #else
   1029  1.1.1.2.30.1  christos 	mutex_destroy(&prime_fpriv->lock);
   1030  1.1.1.2.30.1  christos #endif
   1031           1.1  riastrad }
   1032