Home | History | Annotate | Line # | Download | only in drm
drm_prime.c revision 1.17
      1  1.17  riastrad /*	$NetBSD: drm_prime.c,v 1.17 2021/12/19 11:34:56 riastradh Exp $	*/
      2   1.2  riastrad 
      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.2  riastrad #include <sys/cdefs.h>
     32  1.17  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_prime.c,v 1.17 2021/12/19 11:34:56 riastradh Exp $");
     33   1.2  riastrad 
     34   1.1  riastrad #include <linux/export.h>
     35   1.1  riastrad #include <linux/dma-buf.h>
     36  1.10  riastrad #include <linux/rbtree.h>
     37  1.10  riastrad 
     38  1.10  riastrad #include <drm/drm.h>
     39  1.10  riastrad #include <drm/drm_drv.h>
     40  1.10  riastrad #include <drm/drm_file.h>
     41  1.10  riastrad #include <drm/drm_framebuffer.h>
     42   1.2  riastrad #include <drm/drm_gem.h>
     43  1.10  riastrad #include <drm/drm_prime.h>
     44   1.2  riastrad 
     45   1.2  riastrad #include "drm_internal.h"
     46   1.1  riastrad 
     47   1.4  riastrad #ifdef __NetBSD__
     48   1.4  riastrad 
     49  1.14  riastrad #include <sys/file.h>
     50  1.14  riastrad 
     51   1.5  riastrad #include <drm/bus_dma_hacks.h>
     52   1.5  riastrad 
     53   1.8  riastrad #include <linux/nbsd-namespace.h>
     54   1.8  riastrad 
     55   1.4  riastrad #endif	/* __NetBSD__ */
     56   1.4  riastrad 
     57  1.10  riastrad /**
     58  1.10  riastrad  * DOC: overview and lifetime rules
     59   1.1  riastrad  *
     60  1.10  riastrad  * Similar to GEM global names, PRIME file descriptors are also used to share
     61  1.10  riastrad  * buffer objects across processes. They offer additional security: as file
     62  1.10  riastrad  * descriptors must be explicitly sent over UNIX domain sockets to be shared
     63  1.10  riastrad  * between applications, they can't be guessed like the globally unique GEM
     64  1.10  riastrad  * names.
     65  1.10  riastrad  *
     66  1.10  riastrad  * Drivers that support the PRIME API implement the
     67  1.10  riastrad  * &drm_driver.prime_handle_to_fd and &drm_driver.prime_fd_to_handle operations.
     68  1.10  riastrad  * GEM based drivers must use drm_gem_prime_handle_to_fd() and
     69  1.10  riastrad  * drm_gem_prime_fd_to_handle() to implement these. For GEM based drivers the
     70  1.10  riastrad  * actual driver interfaces is provided through the &drm_gem_object_funcs.export
     71  1.10  riastrad  * and &drm_driver.gem_prime_import hooks.
     72  1.10  riastrad  *
     73  1.10  riastrad  * &dma_buf_ops implementations for GEM drivers are all individually exported
     74  1.10  riastrad  * for drivers which need to overwrite or reimplement some of them.
     75  1.10  riastrad  *
     76  1.10  riastrad  * Reference Counting for GEM Drivers
     77  1.10  riastrad  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     78  1.10  riastrad  *
     79  1.10  riastrad  * On the export the &dma_buf holds a reference to the exported buffer object,
     80  1.10  riastrad  * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD
     81  1.10  riastrad  * IOCTL, when it first calls &drm_gem_object_funcs.export
     82  1.10  riastrad  * and stores the exporting GEM object in the &dma_buf.priv field. This
     83  1.10  riastrad  * reference needs to be released when the final reference to the &dma_buf
     84  1.10  riastrad  * itself is dropped and its &dma_buf_ops.release function is called.  For
     85  1.10  riastrad  * GEM-based drivers, the &dma_buf should be exported using
     86  1.10  riastrad  * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release().
     87  1.10  riastrad  *
     88  1.10  riastrad  * Thus the chain of references always flows in one direction, avoiding loops:
     89  1.10  riastrad  * importing GEM object -> dma-buf -> exported GEM bo. A further complication
     90  1.10  riastrad  * are the lookup caches for import and export. These are required to guarantee
     91  1.10  riastrad  * that any given object will always have only one uniqe userspace handle. This
     92  1.10  riastrad  * is required to allow userspace to detect duplicated imports, since some GEM
     93  1.10  riastrad  * drivers do fail command submissions if a given buffer object is listed more
     94  1.10  riastrad  * than once. These import and export caches in &drm_prime_file_private only
     95  1.10  riastrad  * retain a weak reference, which is cleaned up when the corresponding object is
     96  1.10  riastrad  * released.
     97  1.10  riastrad  *
     98  1.10  riastrad  * Self-importing: If userspace is using PRIME as a replacement for flink then
     99  1.10  riastrad  * it will get a fd->handle request for a GEM object that it created.  Drivers
    100  1.10  riastrad  * should detect this situation and return back the underlying object from the
    101  1.10  riastrad  * dma-buf private. For GEM based drivers this is handled in
    102  1.10  riastrad  * drm_gem_prime_import() already.
    103   1.1  riastrad  */
    104   1.1  riastrad 
    105   1.1  riastrad struct drm_prime_member {
    106   1.1  riastrad 	struct dma_buf *dma_buf;
    107   1.1  riastrad 	uint32_t handle;
    108   1.1  riastrad 
    109  1.10  riastrad 	struct rb_node dmabuf_rb;
    110  1.10  riastrad 	struct rb_node handle_rb;
    111   1.2  riastrad };
    112   1.2  riastrad 
    113  1.11  riastrad #ifdef __NetBSD__
    114  1.11  riastrad static int
    115  1.11  riastrad compare_dmabufs(void *cookie, const void *va, const void *vb)
    116  1.11  riastrad {
    117  1.11  riastrad 	const struct drm_prime_member *ma = va;
    118  1.11  riastrad 	const struct drm_prime_member *mb = vb;
    119  1.11  riastrad 
    120  1.11  riastrad 	if (ma->dma_buf < mb->dma_buf)
    121  1.11  riastrad 		return -1;
    122  1.11  riastrad 	if (ma->dma_buf > mb->dma_buf)
    123  1.11  riastrad 		return +1;
    124  1.11  riastrad 	return 0;
    125  1.11  riastrad }
    126  1.11  riastrad 
    127  1.11  riastrad static int
    128  1.11  riastrad compare_dmabuf_key(void *cookie, const void *vm, const void *vk)
    129  1.11  riastrad {
    130  1.11  riastrad 	const struct drm_prime_member *m = vm;
    131  1.11  riastrad 	const struct dma_buf *const *kp = vk;
    132  1.11  riastrad 
    133  1.11  riastrad 	if (m->dma_buf < *kp)
    134  1.11  riastrad 		return -1;
    135  1.11  riastrad 	if (m->dma_buf > *kp)
    136  1.11  riastrad 		return +1;
    137  1.11  riastrad 	return 0;
    138  1.11  riastrad }
    139  1.11  riastrad 
    140  1.11  riastrad static int
    141  1.11  riastrad compare_handles(void *cookie, const void *va, const void *vb)
    142  1.11  riastrad {
    143  1.11  riastrad 	const struct drm_prime_member *ma = va;
    144  1.11  riastrad 	const struct drm_prime_member *mb = vb;
    145  1.11  riastrad 
    146  1.11  riastrad 	if (ma->handle < mb->handle)
    147  1.11  riastrad 		return -1;
    148  1.11  riastrad 	if (ma->handle > mb->handle)
    149  1.11  riastrad 		return +1;
    150  1.11  riastrad 	return 0;
    151  1.11  riastrad }
    152  1.11  riastrad 
    153  1.11  riastrad static int
    154  1.11  riastrad compare_handle_key(void *cookie, const void *vm, const void *vk)
    155  1.11  riastrad {
    156  1.11  riastrad 	const struct drm_prime_member *m = vm;
    157  1.11  riastrad 	const uint32_t *kp = vk;
    158  1.11  riastrad 
    159  1.11  riastrad 	if (m->handle < *kp)
    160  1.11  riastrad 		return -1;
    161  1.11  riastrad 	if (m->handle > *kp)
    162  1.11  riastrad 		return +1;
    163  1.11  riastrad 	return 0;
    164  1.11  riastrad }
    165  1.11  riastrad 
    166  1.11  riastrad static const rb_tree_ops_t dmabuf_ops = {
    167  1.11  riastrad 	.rbto_compare_nodes = compare_dmabufs,
    168  1.11  riastrad 	.rbto_compare_key = compare_dmabuf_key,
    169  1.11  riastrad 	.rbto_node_offset = offsetof(struct drm_prime_member, dmabuf_rb),
    170  1.11  riastrad };
    171  1.11  riastrad 
    172  1.11  riastrad static const rb_tree_ops_t handle_ops = {
    173  1.11  riastrad 	.rbto_compare_nodes = compare_handles,
    174  1.11  riastrad 	.rbto_compare_key = compare_handle_key,
    175  1.11  riastrad 	.rbto_node_offset = offsetof(struct drm_prime_member, handle_rb),
    176  1.11  riastrad };
    177  1.11  riastrad #endif
    178  1.11  riastrad 
    179   1.2  riastrad static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
    180   1.2  riastrad 				    struct dma_buf *dma_buf, uint32_t handle)
    181   1.2  riastrad {
    182   1.2  riastrad 	struct drm_prime_member *member;
    183  1.11  riastrad #ifdef __NetBSD__
    184  1.11  riastrad 	struct drm_prime_member *collision __diagused;
    185  1.11  riastrad #else
    186  1.10  riastrad 	struct rb_node **p, *rb;
    187  1.11  riastrad #endif
    188   1.2  riastrad 
    189   1.2  riastrad 	member = kmalloc(sizeof(*member), GFP_KERNEL);
    190   1.2  riastrad 	if (!member)
    191   1.2  riastrad 		return -ENOMEM;
    192   1.2  riastrad 
    193   1.2  riastrad 	get_dma_buf(dma_buf);
    194   1.2  riastrad 	member->dma_buf = dma_buf;
    195   1.2  riastrad 	member->handle = handle;
    196  1.10  riastrad 
    197  1.11  riastrad #ifdef __NetBSD__
    198  1.11  riastrad 	collision = rb_tree_insert_node(&prime_fpriv->dmabufs.rbr_tree,
    199  1.11  riastrad 	    member);
    200  1.17  riastrad 	KASSERT(collision == member);
    201  1.11  riastrad #else
    202  1.10  riastrad 	rb = NULL;
    203  1.10  riastrad 	p = &prime_fpriv->dmabufs.rb_node;
    204  1.10  riastrad 	while (*p) {
    205  1.10  riastrad 		struct drm_prime_member *pos;
    206  1.10  riastrad 
    207  1.10  riastrad 		rb = *p;
    208  1.10  riastrad 		pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
    209  1.10  riastrad 		if (dma_buf > pos->dma_buf)
    210  1.10  riastrad 			p = &rb->rb_right;
    211  1.10  riastrad 		else
    212  1.10  riastrad 			p = &rb->rb_left;
    213  1.10  riastrad 	}
    214  1.10  riastrad 	rb_link_node(&member->dmabuf_rb, rb, p);
    215  1.10  riastrad 	rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
    216  1.11  riastrad #endif
    217  1.10  riastrad 
    218  1.11  riastrad #ifdef __NetBSD__
    219  1.11  riastrad 	collision = rb_tree_insert_node(&prime_fpriv->handles.rbr_tree,
    220  1.11  riastrad 	    member);
    221  1.17  riastrad 	KASSERT(collision == member);
    222  1.11  riastrad #else
    223  1.10  riastrad 	rb = NULL;
    224  1.10  riastrad 	p = &prime_fpriv->handles.rb_node;
    225  1.10  riastrad 	while (*p) {
    226  1.10  riastrad 		struct drm_prime_member *pos;
    227  1.10  riastrad 
    228  1.10  riastrad 		rb = *p;
    229  1.10  riastrad 		pos = rb_entry(rb, struct drm_prime_member, handle_rb);
    230  1.10  riastrad 		if (handle > pos->handle)
    231  1.10  riastrad 			p = &rb->rb_right;
    232  1.10  riastrad 		else
    233  1.10  riastrad 			p = &rb->rb_left;
    234  1.10  riastrad 	}
    235  1.10  riastrad 	rb_link_node(&member->handle_rb, rb, p);
    236  1.10  riastrad 	rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
    237  1.11  riastrad #endif
    238  1.10  riastrad 
    239   1.2  riastrad 	return 0;
    240   1.2  riastrad }
    241   1.2  riastrad 
    242   1.2  riastrad static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
    243   1.2  riastrad 						      uint32_t handle)
    244   1.2  riastrad {
    245  1.11  riastrad #ifdef __NetBSD__
    246  1.11  riastrad 	return rb_tree_find_node(&prime_fpriv->handles.rbr_tree, &handle);
    247  1.11  riastrad #else
    248  1.10  riastrad 	struct rb_node *rb;
    249  1.10  riastrad 
    250  1.10  riastrad 	rb = prime_fpriv->handles.rb_node;
    251  1.10  riastrad 	while (rb) {
    252  1.10  riastrad 		struct drm_prime_member *member;
    253   1.2  riastrad 
    254  1.10  riastrad 		member = rb_entry(rb, struct drm_prime_member, handle_rb);
    255   1.2  riastrad 		if (member->handle == handle)
    256   1.2  riastrad 			return member->dma_buf;
    257  1.10  riastrad 		else if (member->handle < handle)
    258  1.10  riastrad 			rb = rb->rb_right;
    259  1.10  riastrad 		else
    260  1.10  riastrad 			rb = rb->rb_left;
    261   1.2  riastrad 	}
    262   1.2  riastrad 
    263   1.2  riastrad 	return NULL;
    264  1.11  riastrad #endif
    265   1.2  riastrad }
    266   1.2  riastrad 
    267   1.2  riastrad static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
    268   1.2  riastrad 				       struct dma_buf *dma_buf,
    269   1.2  riastrad 				       uint32_t *handle)
    270   1.2  riastrad {
    271  1.11  riastrad #ifdef __NetBSD__
    272  1.11  riastrad 	struct drm_prime_member *member;
    273  1.11  riastrad 
    274  1.11  riastrad 	member = rb_tree_find_node(&prime_fpriv->dmabufs.rbr_tree, &dma_buf);
    275  1.11  riastrad 	if (member == NULL)
    276  1.11  riastrad 		return -ENOENT;
    277  1.11  riastrad 	*handle = member->handle;
    278  1.11  riastrad 	return 0;
    279  1.11  riastrad #else
    280  1.10  riastrad 	struct rb_node *rb;
    281  1.10  riastrad 
    282  1.10  riastrad 	rb = prime_fpriv->dmabufs.rb_node;
    283  1.10  riastrad 	while (rb) {
    284  1.10  riastrad 		struct drm_prime_member *member;
    285   1.2  riastrad 
    286  1.10  riastrad 		member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
    287   1.2  riastrad 		if (member->dma_buf == dma_buf) {
    288   1.2  riastrad 			*handle = member->handle;
    289   1.2  riastrad 			return 0;
    290  1.10  riastrad 		} else if (member->dma_buf < dma_buf) {
    291  1.10  riastrad 			rb = rb->rb_right;
    292  1.10  riastrad 		} else {
    293  1.10  riastrad 			rb = rb->rb_left;
    294   1.2  riastrad 		}
    295   1.2  riastrad 	}
    296  1.10  riastrad 
    297   1.2  riastrad 	return -ENOENT;
    298  1.11  riastrad #endif
    299   1.2  riastrad }
    300   1.2  riastrad 
    301   1.2  riastrad void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
    302   1.2  riastrad 					struct dma_buf *dma_buf)
    303   1.2  riastrad {
    304  1.11  riastrad #ifdef __NetBSD__
    305  1.11  riastrad 	struct drm_prime_member *member;
    306  1.11  riastrad 
    307  1.11  riastrad 	member = rb_tree_find_node(&prime_fpriv->dmabufs.rbr_tree, &dma_buf);
    308  1.11  riastrad 	if (member != NULL) {
    309  1.11  riastrad 		rb_tree_remove_node(&prime_fpriv->handles.rbr_tree, member);
    310  1.11  riastrad 		rb_tree_remove_node(&prime_fpriv->dmabufs.rbr_tree, member);
    311  1.11  riastrad 	}
    312  1.11  riastrad #else
    313  1.10  riastrad 	struct rb_node *rb;
    314   1.2  riastrad 
    315  1.10  riastrad 	rb = prime_fpriv->dmabufs.rb_node;
    316  1.10  riastrad 	while (rb) {
    317  1.10  riastrad 		struct drm_prime_member *member;
    318  1.10  riastrad 
    319  1.10  riastrad 		member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
    320   1.2  riastrad 		if (member->dma_buf == dma_buf) {
    321  1.10  riastrad 			rb_erase(&member->handle_rb, &prime_fpriv->handles);
    322  1.10  riastrad 			rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
    323  1.10  riastrad 
    324   1.2  riastrad 			dma_buf_put(dma_buf);
    325   1.2  riastrad 			kfree(member);
    326  1.10  riastrad 			return;
    327  1.10  riastrad 		} else if (member->dma_buf < dma_buf) {
    328  1.10  riastrad 			rb = rb->rb_right;
    329  1.10  riastrad 		} else {
    330  1.10  riastrad 			rb = rb->rb_left;
    331   1.2  riastrad 		}
    332   1.2  riastrad 	}
    333  1.11  riastrad #endif
    334   1.2  riastrad }
    335   1.2  riastrad 
    336  1.10  riastrad void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
    337   1.2  riastrad {
    338  1.11  riastrad #ifdef __NetBSD__
    339  1.11  riastrad 	linux_mutex_init(&prime_fpriv->lock);
    340  1.11  riastrad #else
    341  1.10  riastrad 	mutex_init(&prime_fpriv->lock);
    342  1.11  riastrad #endif
    343  1.11  riastrad #ifdef __NetBSD__
    344  1.11  riastrad 	rb_tree_init(&prime_fpriv->dmabufs.rbr_tree, &dmabuf_ops);
    345  1.11  riastrad 	rb_tree_init(&prime_fpriv->handles.rbr_tree, &handle_ops);
    346  1.11  riastrad #else
    347  1.10  riastrad 	prime_fpriv->dmabufs = RB_ROOT;
    348  1.10  riastrad 	prime_fpriv->handles = RB_ROOT;
    349  1.11  riastrad #endif
    350  1.10  riastrad }
    351   1.2  riastrad 
    352  1.10  riastrad void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
    353  1.10  riastrad {
    354  1.11  riastrad #ifdef __NetBSD__ /* XXX post-merge linux doesn't destroy it's lock now? */
    355  1.11  riastrad 	linux_mutex_destroy(&prime_fpriv->lock);
    356  1.11  riastrad #endif
    357  1.10  riastrad 	/* by now drm_gem_release should've made sure the list is empty */
    358  1.10  riastrad 	WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
    359  1.10  riastrad }
    360   1.2  riastrad 
    361  1.10  riastrad /**
    362  1.10  riastrad  * drm_gem_dmabuf_export - &dma_buf export implementation for GEM
    363  1.10  riastrad  * @dev: parent device for the exported dmabuf
    364  1.10  riastrad  * @exp_info: the export information used by dma_buf_export()
    365  1.10  riastrad  *
    366  1.10  riastrad  * This wraps dma_buf_export() for use by generic GEM drivers that are using
    367  1.10  riastrad  * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
    368  1.10  riastrad  * a reference to the &drm_device and the exported &drm_gem_object (stored in
    369  1.10  riastrad  * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
    370  1.10  riastrad  *
    371  1.10  riastrad  * Returns the new dmabuf.
    372  1.10  riastrad  */
    373  1.10  riastrad struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
    374  1.10  riastrad 				      struct dma_buf_export_info *exp_info)
    375  1.10  riastrad {
    376  1.10  riastrad 	struct drm_gem_object *obj = exp_info->priv;
    377  1.10  riastrad 	struct dma_buf *dma_buf;
    378   1.2  riastrad 
    379  1.10  riastrad 	dma_buf = dma_buf_export(exp_info);
    380  1.10  riastrad 	if (IS_ERR(dma_buf))
    381  1.10  riastrad 		return dma_buf;
    382   1.2  riastrad 
    383  1.10  riastrad 	drm_dev_get(dev);
    384  1.10  riastrad 	drm_gem_object_get(obj);
    385  1.14  riastrad #ifndef __NetBSD__		/* XXX dmabuf share */
    386  1.10  riastrad 	dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping;
    387  1.14  riastrad #endif
    388   1.2  riastrad 
    389  1.10  riastrad 	return dma_buf;
    390   1.2  riastrad }
    391  1.10  riastrad EXPORT_SYMBOL(drm_gem_dmabuf_export);
    392   1.2  riastrad 
    393   1.2  riastrad /**
    394  1.10  riastrad  * drm_gem_dmabuf_release - &dma_buf release implementation for GEM
    395   1.2  riastrad  * @dma_buf: buffer to be released
    396   1.2  riastrad  *
    397   1.2  riastrad  * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
    398  1.10  riastrad  * must use this in their &dma_buf_ops structure as the release callback.
    399  1.10  riastrad  * drm_gem_dmabuf_release() should be used in conjunction with
    400  1.10  riastrad  * drm_gem_dmabuf_export().
    401   1.2  riastrad  */
    402   1.2  riastrad void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
    403   1.2  riastrad {
    404   1.2  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    405  1.10  riastrad 	struct drm_device *dev = obj->dev;
    406   1.2  riastrad 
    407   1.2  riastrad 	/* drop the reference on the export fd holds */
    408  1.10  riastrad 	drm_gem_object_put_unlocked(obj);
    409  1.10  riastrad 
    410  1.10  riastrad 	drm_dev_put(dev);
    411   1.2  riastrad }
    412   1.2  riastrad EXPORT_SYMBOL(drm_gem_dmabuf_release);
    413   1.2  riastrad 
    414  1.10  riastrad /**
    415  1.10  riastrad  * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
    416  1.10  riastrad  * @dev: dev to export the buffer from
    417  1.10  riastrad  * @file_priv: drm file-private structure
    418  1.10  riastrad  * @prime_fd: fd id of the dma-buf which should be imported
    419  1.10  riastrad  * @handle: pointer to storage for the handle of the imported buffer object
    420  1.10  riastrad  *
    421  1.10  riastrad  * This is the PRIME import function which must be used mandatorily by GEM
    422  1.10  riastrad  * drivers to ensure correct lifetime management of the underlying GEM object.
    423  1.10  riastrad  * The actual importing of GEM object from the dma-buf is done through the
    424  1.10  riastrad  * &drm_driver.gem_prime_import driver callback.
    425  1.10  riastrad  *
    426  1.10  riastrad  * Returns 0 on success or a negative error code on failure.
    427  1.10  riastrad  */
    428  1.10  riastrad int drm_gem_prime_fd_to_handle(struct drm_device *dev,
    429  1.10  riastrad 			       struct drm_file *file_priv, int prime_fd,
    430  1.10  riastrad 			       uint32_t *handle)
    431   1.2  riastrad {
    432  1.10  riastrad 	struct dma_buf *dma_buf;
    433  1.10  riastrad 	struct drm_gem_object *obj;
    434  1.10  riastrad 	int ret;
    435   1.2  riastrad 
    436  1.10  riastrad 	dma_buf = dma_buf_get(prime_fd);
    437  1.10  riastrad 	if (IS_ERR(dma_buf))
    438  1.10  riastrad 		return PTR_ERR(dma_buf);
    439   1.2  riastrad 
    440  1.10  riastrad 	mutex_lock(&file_priv->prime.lock);
    441   1.2  riastrad 
    442  1.10  riastrad 	ret = drm_prime_lookup_buf_handle(&file_priv->prime,
    443  1.10  riastrad 			dma_buf, handle);
    444  1.10  riastrad 	if (ret == 0)
    445  1.10  riastrad 		goto out_put;
    446   1.2  riastrad 
    447  1.10  riastrad 	/* never seen this one, need to import */
    448  1.10  riastrad 	mutex_lock(&dev->object_name_lock);
    449  1.10  riastrad 	if (dev->driver->gem_prime_import)
    450  1.10  riastrad 		obj = dev->driver->gem_prime_import(dev, dma_buf);
    451  1.10  riastrad 	else
    452  1.10  riastrad 		obj = drm_gem_prime_import(dev, dma_buf);
    453  1.10  riastrad 	if (IS_ERR(obj)) {
    454  1.10  riastrad 		ret = PTR_ERR(obj);
    455  1.10  riastrad 		goto out_unlock;
    456  1.10  riastrad 	}
    457   1.2  riastrad 
    458  1.10  riastrad 	if (obj->dma_buf) {
    459  1.10  riastrad 		WARN_ON(obj->dma_buf != dma_buf);
    460  1.10  riastrad 	} else {
    461  1.10  riastrad 		obj->dma_buf = dma_buf;
    462  1.10  riastrad 		get_dma_buf(dma_buf);
    463  1.10  riastrad 	}
    464   1.2  riastrad 
    465  1.10  riastrad 	/* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
    466  1.10  riastrad 	ret = drm_gem_handle_create_tail(file_priv, obj, handle);
    467  1.10  riastrad 	drm_gem_object_put_unlocked(obj);
    468  1.10  riastrad 	if (ret)
    469  1.10  riastrad 		goto out_put;
    470   1.2  riastrad 
    471  1.10  riastrad 	ret = drm_prime_add_buf_handle(&file_priv->prime,
    472  1.10  riastrad 			dma_buf, *handle);
    473  1.10  riastrad 	mutex_unlock(&file_priv->prime.lock);
    474  1.10  riastrad 	if (ret)
    475  1.10  riastrad 		goto fail;
    476   1.2  riastrad 
    477  1.10  riastrad 	dma_buf_put(dma_buf);
    478   1.2  riastrad 
    479  1.10  riastrad 	return 0;
    480   1.2  riastrad 
    481  1.10  riastrad fail:
    482  1.10  riastrad 	/* hmm, if driver attached, we are relying on the free-object path
    483  1.10  riastrad 	 * to detach.. which seems ok..
    484  1.10  riastrad 	 */
    485  1.10  riastrad 	drm_gem_handle_delete(file_priv, *handle);
    486  1.10  riastrad 	dma_buf_put(dma_buf);
    487  1.10  riastrad 	return ret;
    488   1.2  riastrad 
    489  1.10  riastrad out_unlock:
    490  1.10  riastrad 	mutex_unlock(&dev->object_name_lock);
    491  1.10  riastrad out_put:
    492  1.10  riastrad 	mutex_unlock(&file_priv->prime.lock);
    493  1.10  riastrad 	dma_buf_put(dma_buf);
    494  1.10  riastrad 	return ret;
    495   1.2  riastrad }
    496  1.10  riastrad EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
    497   1.2  riastrad 
    498  1.10  riastrad int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
    499  1.10  riastrad 				 struct drm_file *file_priv)
    500   1.2  riastrad {
    501  1.10  riastrad 	struct drm_prime_handle *args = data;
    502   1.2  riastrad 
    503  1.10  riastrad 	if (!dev->driver->prime_fd_to_handle)
    504  1.10  riastrad 		return -ENOSYS;
    505   1.2  riastrad 
    506  1.10  riastrad 	return dev->driver->prime_fd_to_handle(dev, file_priv,
    507  1.10  riastrad 			args->fd, &args->handle);
    508   1.2  riastrad }
    509   1.2  riastrad 
    510   1.2  riastrad static struct dma_buf *export_and_register_object(struct drm_device *dev,
    511   1.2  riastrad 						  struct drm_gem_object *obj,
    512   1.2  riastrad 						  uint32_t flags)
    513   1.2  riastrad {
    514   1.2  riastrad 	struct dma_buf *dmabuf;
    515   1.2  riastrad 
    516   1.2  riastrad 	/* prevent races with concurrent gem_close. */
    517   1.2  riastrad 	if (obj->handle_count == 0) {
    518   1.2  riastrad 		dmabuf = ERR_PTR(-ENOENT);
    519   1.2  riastrad 		return dmabuf;
    520   1.2  riastrad 	}
    521   1.2  riastrad 
    522  1.10  riastrad 	if (obj->funcs && obj->funcs->export)
    523  1.10  riastrad 		dmabuf = obj->funcs->export(obj, flags);
    524  1.10  riastrad 	else if (dev->driver->gem_prime_export)
    525  1.10  riastrad 		dmabuf = dev->driver->gem_prime_export(obj, flags);
    526  1.10  riastrad 	else
    527  1.10  riastrad 		dmabuf = drm_gem_prime_export(obj, flags);
    528   1.2  riastrad 	if (IS_ERR(dmabuf)) {
    529   1.2  riastrad 		/* normally the created dma-buf takes ownership of the ref,
    530   1.2  riastrad 		 * but if that fails then drop the ref
    531   1.2  riastrad 		 */
    532   1.2  riastrad 		return dmabuf;
    533   1.2  riastrad 	}
    534   1.2  riastrad 
    535   1.2  riastrad 	/*
    536   1.2  riastrad 	 * Note that callers do not need to clean up the export cache
    537   1.2  riastrad 	 * since the check for obj->handle_count guarantees that someone
    538   1.2  riastrad 	 * will clean it up.
    539   1.2  riastrad 	 */
    540   1.2  riastrad 	obj->dma_buf = dmabuf;
    541   1.2  riastrad 	get_dma_buf(obj->dma_buf);
    542   1.2  riastrad 
    543   1.2  riastrad 	return dmabuf;
    544   1.2  riastrad }
    545   1.2  riastrad 
    546   1.2  riastrad /**
    547   1.2  riastrad  * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
    548   1.2  riastrad  * @dev: dev to export the buffer from
    549   1.2  riastrad  * @file_priv: drm file-private structure
    550   1.2  riastrad  * @handle: buffer handle to export
    551   1.2  riastrad  * @flags: flags like DRM_CLOEXEC
    552   1.2  riastrad  * @prime_fd: pointer to storage for the fd id of the create dma-buf
    553   1.2  riastrad  *
    554   1.2  riastrad  * This is the PRIME export function which must be used mandatorily by GEM
    555   1.2  riastrad  * drivers to ensure correct lifetime management of the underlying GEM object.
    556   1.2  riastrad  * The actual exporting from GEM object to a dma-buf is done through the
    557  1.10  riastrad  * &drm_driver.gem_prime_export driver callback.
    558   1.2  riastrad  */
    559   1.1  riastrad int drm_gem_prime_handle_to_fd(struct drm_device *dev,
    560   1.2  riastrad 			       struct drm_file *file_priv, uint32_t handle,
    561   1.2  riastrad 			       uint32_t flags,
    562   1.2  riastrad 			       int *prime_fd)
    563   1.1  riastrad {
    564   1.1  riastrad 	struct drm_gem_object *obj;
    565   1.2  riastrad 	int ret = 0;
    566   1.2  riastrad 	struct dma_buf *dmabuf;
    567   1.1  riastrad 
    568   1.2  riastrad 	mutex_lock(&file_priv->prime.lock);
    569  1.10  riastrad 	obj = drm_gem_object_lookup(file_priv, handle);
    570   1.2  riastrad 	if (!obj)  {
    571   1.2  riastrad 		ret = -ENOENT;
    572   1.2  riastrad 		goto out_unlock;
    573   1.2  riastrad 	}
    574   1.2  riastrad 
    575   1.2  riastrad 	dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
    576   1.2  riastrad 	if (dmabuf) {
    577   1.2  riastrad 		get_dma_buf(dmabuf);
    578   1.2  riastrad 		goto out_have_handle;
    579   1.2  riastrad 	}
    580   1.1  riastrad 
    581   1.2  riastrad 	mutex_lock(&dev->object_name_lock);
    582   1.1  riastrad 	/* re-export the original imported object */
    583   1.1  riastrad 	if (obj->import_attach) {
    584   1.2  riastrad 		dmabuf = obj->import_attach->dmabuf;
    585   1.2  riastrad 		get_dma_buf(dmabuf);
    586   1.2  riastrad 		goto out_have_obj;
    587   1.2  riastrad 	}
    588   1.2  riastrad 
    589   1.2  riastrad 	if (obj->dma_buf) {
    590   1.2  riastrad 		get_dma_buf(obj->dma_buf);
    591   1.2  riastrad 		dmabuf = obj->dma_buf;
    592   1.2  riastrad 		goto out_have_obj;
    593   1.2  riastrad 	}
    594   1.2  riastrad 
    595   1.2  riastrad 	dmabuf = export_and_register_object(dev, obj, flags);
    596   1.2  riastrad 	if (IS_ERR(dmabuf)) {
    597   1.2  riastrad 		/* normally the created dma-buf takes ownership of the ref,
    598   1.2  riastrad 		 * but if that fails then drop the ref
    599   1.2  riastrad 		 */
    600   1.2  riastrad 		ret = PTR_ERR(dmabuf);
    601   1.2  riastrad 		mutex_unlock(&dev->object_name_lock);
    602   1.2  riastrad 		goto out;
    603   1.1  riastrad 	}
    604   1.1  riastrad 
    605   1.2  riastrad out_have_obj:
    606   1.2  riastrad 	/*
    607   1.2  riastrad 	 * If we've exported this buffer then cheat and add it to the import list
    608   1.2  riastrad 	 * so we get the correct handle back. We must do this under the
    609   1.2  riastrad 	 * protection of dev->object_name_lock to ensure that a racing gem close
    610   1.2  riastrad 	 * ioctl doesn't miss to remove this buffer handle from the cache.
    611   1.2  riastrad 	 */
    612   1.2  riastrad 	ret = drm_prime_add_buf_handle(&file_priv->prime,
    613   1.2  riastrad 				       dmabuf, handle);
    614   1.2  riastrad 	mutex_unlock(&dev->object_name_lock);
    615   1.2  riastrad 	if (ret)
    616   1.2  riastrad 		goto fail_put_dmabuf;
    617   1.2  riastrad 
    618   1.2  riastrad out_have_handle:
    619   1.2  riastrad 	ret = dma_buf_fd(dmabuf, flags);
    620   1.2  riastrad 	/*
    621   1.2  riastrad 	 * We must _not_ remove the buffer from the handle cache since the newly
    622   1.2  riastrad 	 * created dma buf is already linked in the global obj->dma_buf pointer,
    623   1.2  riastrad 	 * and that is invariant as long as a userspace gem handle exists.
    624   1.2  riastrad 	 * Closing the handle will clean out the cache anyway, so we don't leak.
    625   1.2  riastrad 	 */
    626   1.2  riastrad 	if (ret < 0) {
    627   1.2  riastrad 		goto fail_put_dmabuf;
    628   1.1  riastrad 	} else {
    629   1.2  riastrad 		*prime_fd = ret;
    630   1.2  riastrad 		ret = 0;
    631   1.2  riastrad 	}
    632   1.2  riastrad 
    633   1.2  riastrad 	goto out;
    634   1.2  riastrad 
    635   1.2  riastrad fail_put_dmabuf:
    636   1.2  riastrad 	dma_buf_put(dmabuf);
    637   1.2  riastrad out:
    638  1.10  riastrad 	drm_gem_object_put_unlocked(obj);
    639   1.2  riastrad out_unlock:
    640   1.2  riastrad 	mutex_unlock(&file_priv->prime.lock);
    641   1.2  riastrad 
    642   1.2  riastrad 	return ret;
    643   1.2  riastrad }
    644   1.2  riastrad EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
    645   1.2  riastrad 
    646  1.10  riastrad int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
    647  1.10  riastrad 				 struct drm_file *file_priv)
    648  1.10  riastrad {
    649  1.10  riastrad 	struct drm_prime_handle *args = data;
    650  1.10  riastrad 
    651  1.10  riastrad 	if (!dev->driver->prime_handle_to_fd)
    652  1.10  riastrad 		return -ENOSYS;
    653  1.10  riastrad 
    654  1.10  riastrad 	/* check flags are valid */
    655  1.10  riastrad 	if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
    656  1.10  riastrad 		return -EINVAL;
    657  1.10  riastrad 
    658  1.10  riastrad 	return dev->driver->prime_handle_to_fd(dev, file_priv,
    659  1.10  riastrad 			args->handle, args->flags, &args->fd);
    660  1.10  riastrad }
    661  1.10  riastrad 
    662  1.10  riastrad /**
    663  1.10  riastrad  * DOC: PRIME Helpers
    664  1.10  riastrad  *
    665  1.10  riastrad  * Drivers can implement &drm_gem_object_funcs.export and
    666  1.10  riastrad  * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper
    667  1.10  riastrad  * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions
    668  1.10  riastrad  * implement dma-buf support in terms of some lower-level helpers, which are
    669  1.10  riastrad  * again exported for drivers to use individually:
    670  1.10  riastrad  *
    671  1.10  riastrad  * Exporting buffers
    672  1.10  riastrad  * ~~~~~~~~~~~~~~~~~
    673  1.10  riastrad  *
    674  1.10  riastrad  * Optional pinning of buffers is handled at dma-buf attach and detach time in
    675  1.10  riastrad  * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
    676  1.10  riastrad  * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
    677  1.10  riastrad  * &drm_gem_object_funcs.get_sg_table.
    678  1.10  riastrad  *
    679  1.10  riastrad  * For kernel-internal access there's drm_gem_dmabuf_vmap() and
    680  1.10  riastrad  * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
    681  1.10  riastrad  * drm_gem_dmabuf_mmap().
    682  1.10  riastrad  *
    683  1.10  riastrad  * Note that these export helpers can only be used if the underlying backing
    684  1.10  riastrad  * storage is fully coherent and either permanently pinned, or it is safe to pin
    685  1.10  riastrad  * it indefinitely.
    686  1.10  riastrad  *
    687  1.10  riastrad  * FIXME: The underlying helper functions are named rather inconsistently.
    688  1.10  riastrad  *
    689  1.10  riastrad  * Exporting buffers
    690  1.10  riastrad  * ~~~~~~~~~~~~~~~~~
    691  1.10  riastrad  *
    692  1.10  riastrad  * Importing dma-bufs using drm_gem_prime_import() relies on
    693  1.10  riastrad  * &drm_driver.gem_prime_import_sg_table.
    694  1.10  riastrad  *
    695  1.10  riastrad  * Note that similarly to the export helpers this permanently pins the
    696  1.10  riastrad  * underlying backing storage. Which is ok for scanout, but is not the best
    697  1.10  riastrad  * option for sharing lots of buffers for rendering.
    698  1.10  riastrad  */
    699  1.10  riastrad 
    700   1.2  riastrad /**
    701  1.10  riastrad  * drm_gem_map_attach - dma_buf attach implementation for GEM
    702  1.10  riastrad  * @dma_buf: buffer to attach device to
    703  1.10  riastrad  * @attach: buffer attachment data
    704  1.10  riastrad  *
    705  1.10  riastrad  * Calls &drm_gem_object_funcs.pin for device specific handling. This can be
    706  1.10  riastrad  * used as the &dma_buf_ops.attach callback. Must be used together with
    707  1.10  riastrad  * drm_gem_map_detach().
    708   1.2  riastrad  *
    709  1.10  riastrad  * Returns 0 on success, negative error code on failure.
    710  1.10  riastrad  */
    711  1.10  riastrad int drm_gem_map_attach(struct dma_buf *dma_buf,
    712  1.10  riastrad 		       struct dma_buf_attachment *attach)
    713  1.10  riastrad {
    714  1.10  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    715  1.10  riastrad 
    716  1.10  riastrad 	return drm_gem_pin(obj);
    717  1.10  riastrad }
    718  1.10  riastrad EXPORT_SYMBOL(drm_gem_map_attach);
    719  1.10  riastrad 
    720  1.10  riastrad /**
    721  1.10  riastrad  * drm_gem_map_detach - dma_buf detach implementation for GEM
    722  1.10  riastrad  * @dma_buf: buffer to detach from
    723  1.10  riastrad  * @attach: attachment to be detached
    724  1.10  riastrad  *
    725  1.10  riastrad  * Calls &drm_gem_object_funcs.pin for device specific handling.  Cleans up
    726  1.10  riastrad  * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the
    727  1.10  riastrad  * &dma_buf_ops.detach callback.
    728   1.2  riastrad  */
    729  1.10  riastrad void drm_gem_map_detach(struct dma_buf *dma_buf,
    730  1.10  riastrad 			struct dma_buf_attachment *attach)
    731   1.2  riastrad {
    732  1.10  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    733   1.2  riastrad 
    734  1.10  riastrad 	drm_gem_unpin(obj);
    735  1.10  riastrad }
    736  1.10  riastrad EXPORT_SYMBOL(drm_gem_map_detach);
    737  1.10  riastrad 
    738  1.10  riastrad /**
    739  1.10  riastrad  * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
    740  1.10  riastrad  * @attach: attachment whose scatterlist is to be returned
    741  1.10  riastrad  * @dir: direction of DMA transfer
    742  1.10  riastrad  *
    743  1.10  riastrad  * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
    744  1.10  riastrad  * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
    745  1.10  riastrad  * with drm_gem_unmap_dma_buf().
    746  1.10  riastrad  *
    747  1.10  riastrad  * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
    748  1.10  riastrad  * on error. May return -EINTR if it is interrupted by a signal.
    749  1.10  riastrad  */
    750  1.10  riastrad struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
    751  1.10  riastrad 				     enum dma_data_direction dir)
    752  1.10  riastrad {
    753  1.10  riastrad 	struct drm_gem_object *obj = attach->dmabuf->priv;
    754  1.10  riastrad 	struct sg_table *sgt;
    755   1.2  riastrad 
    756  1.10  riastrad 	if (WARN_ON(dir == DMA_NONE))
    757   1.2  riastrad 		return ERR_PTR(-EINVAL);
    758   1.2  riastrad 
    759  1.10  riastrad 	if (obj->funcs)
    760  1.10  riastrad 		sgt = obj->funcs->get_sg_table(obj);
    761  1.10  riastrad 	else
    762  1.10  riastrad 		sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
    763   1.2  riastrad 
    764  1.10  riastrad 	if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
    765  1.10  riastrad 			      DMA_ATTR_SKIP_CPU_SYNC)) {
    766  1.10  riastrad 		sg_free_table(sgt);
    767  1.10  riastrad 		kfree(sgt);
    768  1.10  riastrad 		sgt = ERR_PTR(-ENOMEM);
    769  1.10  riastrad 	}
    770   1.2  riastrad 
    771  1.10  riastrad 	return sgt;
    772  1.10  riastrad }
    773  1.10  riastrad EXPORT_SYMBOL(drm_gem_map_dma_buf);
    774   1.2  riastrad 
    775  1.10  riastrad /**
    776  1.10  riastrad  * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
    777  1.10  riastrad  * @attach: attachment to unmap buffer from
    778  1.10  riastrad  * @sgt: scatterlist info of the buffer to unmap
    779  1.10  riastrad  * @dir: direction of DMA transfer
    780  1.10  riastrad  *
    781  1.10  riastrad  * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
    782  1.10  riastrad  */
    783  1.10  riastrad void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
    784  1.10  riastrad 			   struct sg_table *sgt,
    785  1.10  riastrad 			   enum dma_data_direction dir)
    786  1.10  riastrad {
    787  1.10  riastrad 	if (!sgt)
    788  1.10  riastrad 		return;
    789   1.1  riastrad 
    790  1.10  riastrad 	dma_unmap_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
    791  1.10  riastrad 			   DMA_ATTR_SKIP_CPU_SYNC);
    792  1.10  riastrad 	sg_free_table(sgt);
    793  1.10  riastrad 	kfree(sgt);
    794  1.10  riastrad }
    795  1.10  riastrad EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
    796   1.2  riastrad 
    797  1.10  riastrad /**
    798  1.10  riastrad  * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
    799  1.10  riastrad  * @dma_buf: buffer to be mapped
    800  1.10  riastrad  *
    801  1.10  riastrad  * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
    802  1.10  riastrad  * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling.
    803  1.10  riastrad  *
    804  1.10  riastrad  * Returns the kernel virtual address or NULL on failure.
    805  1.10  riastrad  */
    806  1.10  riastrad void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
    807  1.10  riastrad {
    808  1.10  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    809  1.10  riastrad 	void *vaddr;
    810   1.2  riastrad 
    811  1.10  riastrad 	vaddr = drm_gem_vmap(obj);
    812  1.10  riastrad 	if (IS_ERR(vaddr))
    813  1.10  riastrad 		vaddr = NULL;
    814   1.2  riastrad 
    815  1.10  riastrad 	return vaddr;
    816   1.1  riastrad }
    817  1.10  riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
    818   1.1  riastrad 
    819   1.2  riastrad /**
    820  1.10  riastrad  * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
    821  1.10  riastrad  * @dma_buf: buffer to be unmapped
    822  1.10  riastrad  * @vaddr: the virtual address of the buffer
    823   1.2  riastrad  *
    824  1.10  riastrad  * Releases a kernel virtual mapping. This can be used as the
    825  1.10  riastrad  * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling.
    826   1.2  riastrad  */
    827  1.10  riastrad void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
    828   1.1  riastrad {
    829  1.10  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    830   1.1  riastrad 
    831  1.10  riastrad 	drm_gem_vunmap(obj, vaddr);
    832  1.10  riastrad }
    833  1.10  riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
    834   1.1  riastrad 
    835  1.10  riastrad /**
    836  1.10  riastrad  * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
    837  1.10  riastrad  * @obj: GEM object
    838  1.10  riastrad  * @vma: Virtual address range
    839  1.10  riastrad  *
    840  1.10  riastrad  * This function sets up a userspace mapping for PRIME exported buffers using
    841  1.10  riastrad  * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
    842  1.10  riastrad  * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
    843  1.10  riastrad  * called to set up the mapping.
    844  1.10  riastrad  *
    845  1.10  riastrad  * Drivers can use this as their &drm_driver.gem_prime_mmap callback.
    846  1.10  riastrad  */
    847  1.14  riastrad #ifdef __NetBSD__
    848  1.14  riastrad int drm_gem_prime_mmap(struct drm_gem_object *obj, off_t *offp, size_t size,
    849  1.14  riastrad     int prot, int *flagsp, int *advicep, struct uvm_object **uobjp,
    850  1.14  riastrad     int *maxprotp)
    851  1.14  riastrad #else
    852  1.10  riastrad int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
    853  1.14  riastrad #endif
    854  1.10  riastrad {
    855  1.10  riastrad 	struct drm_file *priv;
    856  1.10  riastrad 	struct file *fil;
    857  1.10  riastrad 	int ret;
    858   1.1  riastrad 
    859  1.10  riastrad 	/* Add the fake offset */
    860  1.14  riastrad #ifdef __NetBSD__
    861  1.14  riastrad 	*offp += drm_vma_node_start(&obj->vma_node);
    862  1.14  riastrad #else
    863  1.10  riastrad 	vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
    864  1.14  riastrad #endif
    865   1.1  riastrad 
    866  1.10  riastrad 	if (obj->funcs && obj->funcs->mmap) {
    867  1.14  riastrad #ifdef __NetBSD__
    868  1.14  riastrad 		ret = obj->funcs->mmap(obj, offp, size, prot, flagsp, advicep,
    869  1.14  riastrad 		    uobjp, maxprotp);
    870  1.14  riastrad #else
    871  1.10  riastrad 		ret = obj->funcs->mmap(obj, vma);
    872  1.14  riastrad #endif
    873  1.10  riastrad 		if (ret)
    874  1.10  riastrad 			return ret;
    875  1.14  riastrad #ifndef __NetBSD__
    876  1.10  riastrad 		vma->vm_private_data = obj;
    877  1.14  riastrad #endif
    878  1.10  riastrad 		drm_gem_object_get(obj);
    879  1.10  riastrad 		return 0;
    880   1.2  riastrad 	}
    881   1.2  riastrad 
    882  1.10  riastrad 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
    883  1.10  riastrad 	fil = kzalloc(sizeof(*fil), GFP_KERNEL);
    884  1.10  riastrad 	if (!priv || !fil) {
    885  1.10  riastrad 		ret = -ENOMEM;
    886  1.10  riastrad 		goto out;
    887   1.1  riastrad 	}
    888   1.1  riastrad 
    889  1.10  riastrad 	/* Used by drm_gem_mmap() to lookup the GEM object */
    890  1.10  riastrad 	priv->minor = obj->dev->primary;
    891  1.14  riastrad #ifdef __NetBSD__
    892  1.14  riastrad 	fil->f_data = priv;
    893  1.14  riastrad #else
    894  1.10  riastrad 	fil->private_data = priv;
    895  1.14  riastrad #endif
    896   1.1  riastrad 
    897  1.10  riastrad 	ret = drm_vma_node_allow(&obj->vma_node, priv);
    898   1.1  riastrad 	if (ret)
    899  1.10  riastrad 		goto out;
    900   1.1  riastrad 
    901  1.14  riastrad #ifdef __NetBSD__
    902  1.14  riastrad 	ret = obj->dev->driver->mmap_object(obj->dev, *offp, size, prot, uobjp,
    903  1.14  riastrad 	    offp, fil);
    904  1.14  riastrad #else
    905  1.10  riastrad 	ret = obj->dev->driver->fops->mmap(fil, vma);
    906  1.14  riastrad #endif
    907   1.2  riastrad 
    908  1.10  riastrad 	drm_vma_node_revoke(&obj->vma_node, priv);
    909  1.10  riastrad out:
    910  1.10  riastrad 	kfree(priv);
    911  1.10  riastrad 	kfree(fil);
    912   1.2  riastrad 
    913   1.1  riastrad 	return ret;
    914   1.1  riastrad }
    915  1.10  riastrad EXPORT_SYMBOL(drm_gem_prime_mmap);
    916   1.1  riastrad 
    917  1.10  riastrad /**
    918  1.10  riastrad  * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
    919  1.10  riastrad  * @dma_buf: buffer to be mapped
    920  1.10  riastrad  * @vma: virtual address range
    921  1.10  riastrad  *
    922  1.10  riastrad  * Provides memory mapping for the buffer. This can be used as the
    923  1.10  riastrad  * &dma_buf_ops.mmap callback. It just forwards to &drm_driver.gem_prime_mmap,
    924  1.10  riastrad  * which should be set to drm_gem_prime_mmap().
    925  1.10  riastrad  *
    926  1.10  riastrad  * FIXME: There's really no point to this wrapper, drivers which need anything
    927  1.10  riastrad  * else but drm_gem_prime_mmap can roll their own &dma_buf_ops.mmap callback.
    928  1.10  riastrad  *
    929  1.10  riastrad  * Returns 0 on success or a negative error code on failure.
    930  1.10  riastrad  */
    931  1.10  riastrad #ifdef __NetBSD__
    932  1.13  riastrad int
    933  1.10  riastrad drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, off_t *offp, size_t size,
    934  1.10  riastrad     int prot, int *flagsp, int *advicep, struct uvm_object **uobjp,
    935  1.10  riastrad     int *maxprotp)
    936  1.10  riastrad #else
    937  1.10  riastrad int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
    938  1.10  riastrad #endif
    939   1.1  riastrad {
    940  1.10  riastrad 	struct drm_gem_object *obj = dma_buf->priv;
    941  1.10  riastrad 	struct drm_device *dev = obj->dev;
    942   1.1  riastrad 
    943  1.10  riastrad 	if (!dev->driver->gem_prime_mmap)
    944   1.1  riastrad 		return -ENOSYS;
    945   1.1  riastrad 
    946  1.10  riastrad #ifdef __NetBSD__
    947  1.10  riastrad 	return dev->driver->gem_prime_mmap(obj, offp, size, prot, flagsp,
    948  1.10  riastrad 	    advicep, uobjp, maxprotp);
    949  1.10  riastrad #else
    950  1.10  riastrad 	return dev->driver->gem_prime_mmap(obj, vma);
    951  1.10  riastrad #endif
    952   1.1  riastrad }
    953  1.10  riastrad EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
    954   1.1  riastrad 
    955  1.10  riastrad static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
    956  1.10  riastrad 	.cache_sgt_mapping = true,
    957  1.10  riastrad 	.attach = drm_gem_map_attach,
    958  1.10  riastrad 	.detach = drm_gem_map_detach,
    959  1.10  riastrad 	.map_dma_buf = drm_gem_map_dma_buf,
    960  1.10  riastrad 	.unmap_dma_buf = drm_gem_unmap_dma_buf,
    961  1.10  riastrad 	.release = drm_gem_dmabuf_release,
    962  1.10  riastrad 	.mmap = drm_gem_dmabuf_mmap,
    963  1.10  riastrad 	.vmap = drm_gem_dmabuf_vmap,
    964  1.10  riastrad 	.vunmap = drm_gem_dmabuf_vunmap,
    965  1.10  riastrad };
    966   1.1  riastrad 
    967   1.2  riastrad /**
    968   1.2  riastrad  * drm_prime_pages_to_sg - converts a page array into an sg list
    969   1.2  riastrad  * @pages: pointer to the array of page pointers to convert
    970   1.2  riastrad  * @nr_pages: length of the page vector
    971   1.1  riastrad  *
    972   1.2  riastrad  * This helper creates an sg table object from a set of pages
    973   1.1  riastrad  * the driver is responsible for mapping the pages into the
    974   1.2  riastrad  * importers address space for use with dma_buf itself.
    975  1.10  riastrad  *
    976  1.10  riastrad  * This is useful for implementing &drm_gem_object_funcs.get_sg_table.
    977   1.1  riastrad  */
    978   1.2  riastrad struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
    979   1.1  riastrad {
    980   1.1  riastrad 	struct sg_table *sg = NULL;
    981   1.1  riastrad 	int ret;
    982   1.1  riastrad 
    983   1.1  riastrad 	sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
    984   1.2  riastrad 	if (!sg) {
    985   1.2  riastrad 		ret = -ENOMEM;
    986   1.1  riastrad 		goto out;
    987   1.2  riastrad 	}
    988   1.1  riastrad 
    989   1.2  riastrad 	ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
    990   1.2  riastrad 				nr_pages << PAGE_SHIFT, GFP_KERNEL);
    991   1.1  riastrad 	if (ret)
    992   1.1  riastrad 		goto out;
    993   1.1  riastrad 
    994   1.1  riastrad 	return sg;
    995   1.1  riastrad out:
    996   1.1  riastrad 	kfree(sg);
    997   1.2  riastrad 	return ERR_PTR(ret);
    998   1.1  riastrad }
    999   1.1  riastrad EXPORT_SYMBOL(drm_prime_pages_to_sg);
   1000   1.1  riastrad 
   1001  1.10  riastrad /**
   1002  1.10  riastrad  * drm_gem_prime_export - helper library implementation of the export callback
   1003  1.10  riastrad  * @obj: GEM object to export
   1004  1.10  riastrad  * @flags: flags like DRM_CLOEXEC and DRM_RDWR
   1005  1.10  riastrad  *
   1006  1.10  riastrad  * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers
   1007  1.10  riastrad  * using the PRIME helpers. It is used as the default in
   1008  1.10  riastrad  * drm_gem_prime_handle_to_fd().
   1009  1.10  riastrad  */
   1010  1.10  riastrad struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
   1011  1.10  riastrad 				     int flags)
   1012  1.10  riastrad {
   1013  1.10  riastrad 	struct drm_device *dev = obj->dev;
   1014  1.10  riastrad 	struct dma_buf_export_info exp_info = {
   1015  1.10  riastrad #ifndef __NetBSD__
   1016  1.10  riastrad 		.exp_name = KBUILD_MODNAME, /* white lie for debug */
   1017  1.10  riastrad 		.owner = dev->driver->fops->owner,
   1018  1.10  riastrad #endif
   1019  1.10  riastrad 		.ops = &drm_gem_prime_dmabuf_ops,
   1020  1.10  riastrad 		.size = obj->size,
   1021  1.10  riastrad 		.flags = flags,
   1022  1.10  riastrad 		.priv = obj,
   1023  1.10  riastrad 		.resv = obj->resv,
   1024  1.10  riastrad 	};
   1025  1.10  riastrad 
   1026  1.10  riastrad 	return drm_gem_dmabuf_export(dev, &exp_info);
   1027  1.10  riastrad }
   1028  1.10  riastrad EXPORT_SYMBOL(drm_gem_prime_export);
   1029  1.10  riastrad 
   1030  1.10  riastrad /**
   1031  1.10  riastrad  * drm_gem_prime_import_dev - core implementation of the import callback
   1032  1.10  riastrad  * @dev: drm_device to import into
   1033  1.10  riastrad  * @dma_buf: dma-buf object to import
   1034  1.10  riastrad  * @attach_dev: struct device to dma_buf attach
   1035  1.10  riastrad  *
   1036  1.10  riastrad  * This is the core of drm_gem_prime_import(). It's designed to be called by
   1037  1.10  riastrad  * drivers who want to use a different device structure than &drm_device.dev for
   1038  1.10  riastrad  * attaching via dma_buf. This function calls
   1039  1.10  riastrad  * &drm_driver.gem_prime_import_sg_table internally.
   1040  1.10  riastrad  *
   1041  1.10  riastrad  * Drivers must arrange to call drm_prime_gem_destroy() from their
   1042  1.10  riastrad  * &drm_gem_object_funcs.free hook when using this function.
   1043  1.10  riastrad  */
   1044  1.16  riastrad #ifdef __NetBSD__
   1045  1.16  riastrad struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
   1046  1.16  riastrad 					    struct dma_buf *dma_buf,
   1047  1.16  riastrad 					    bus_dma_tag_t attach_dev)
   1048  1.16  riastrad #else
   1049  1.10  riastrad struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
   1050  1.10  riastrad 					    struct dma_buf *dma_buf,
   1051  1.10  riastrad 					    struct device *attach_dev)
   1052  1.16  riastrad #endif
   1053  1.10  riastrad {
   1054  1.10  riastrad 	struct dma_buf_attachment *attach;
   1055  1.10  riastrad 	struct sg_table *sgt;
   1056  1.10  riastrad 	struct drm_gem_object *obj;
   1057  1.10  riastrad 	int ret;
   1058  1.10  riastrad 
   1059  1.10  riastrad 	if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
   1060  1.10  riastrad 		obj = dma_buf->priv;
   1061  1.10  riastrad 		if (obj->dev == dev) {
   1062  1.10  riastrad 			/*
   1063  1.10  riastrad 			 * Importing dmabuf exported from out own gem increases
   1064  1.10  riastrad 			 * refcount on gem itself instead of f_count of dmabuf.
   1065  1.10  riastrad 			 */
   1066  1.10  riastrad 			drm_gem_object_get(obj);
   1067  1.10  riastrad 			return obj;
   1068  1.10  riastrad 		}
   1069  1.10  riastrad 	}
   1070  1.10  riastrad 
   1071  1.10  riastrad 	if (!dev->driver->gem_prime_import_sg_table)
   1072  1.10  riastrad 		return ERR_PTR(-EINVAL);
   1073  1.10  riastrad 
   1074  1.10  riastrad 	attach = dma_buf_attach(dma_buf, attach_dev);
   1075  1.10  riastrad 	if (IS_ERR(attach))
   1076  1.10  riastrad 		return ERR_CAST(attach);
   1077  1.10  riastrad 
   1078  1.10  riastrad 	get_dma_buf(dma_buf);
   1079  1.10  riastrad 
   1080  1.10  riastrad 	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
   1081  1.10  riastrad 	if (IS_ERR(sgt)) {
   1082  1.10  riastrad 		ret = PTR_ERR(sgt);
   1083  1.10  riastrad 		goto fail_detach;
   1084  1.10  riastrad 	}
   1085  1.10  riastrad 
   1086  1.10  riastrad 	obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
   1087  1.10  riastrad 	if (IS_ERR(obj)) {
   1088  1.10  riastrad 		ret = PTR_ERR(obj);
   1089  1.10  riastrad 		goto fail_unmap;
   1090  1.10  riastrad 	}
   1091  1.10  riastrad 
   1092  1.10  riastrad 	obj->import_attach = attach;
   1093  1.10  riastrad 	obj->resv = dma_buf->resv;
   1094  1.10  riastrad 
   1095  1.10  riastrad 	return obj;
   1096  1.10  riastrad 
   1097  1.10  riastrad fail_unmap:
   1098  1.10  riastrad 	dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
   1099  1.10  riastrad fail_detach:
   1100  1.10  riastrad 	dma_buf_detach(dma_buf, attach);
   1101  1.10  riastrad 	dma_buf_put(dma_buf);
   1102  1.10  riastrad 
   1103  1.10  riastrad 	return ERR_PTR(ret);
   1104  1.10  riastrad }
   1105  1.10  riastrad EXPORT_SYMBOL(drm_gem_prime_import_dev);
   1106  1.10  riastrad 
   1107  1.10  riastrad /**
   1108  1.10  riastrad  * drm_gem_prime_import - helper library implementation of the import callback
   1109  1.10  riastrad  * @dev: drm_device to import into
   1110  1.10  riastrad  * @dma_buf: dma-buf object to import
   1111  1.10  riastrad  *
   1112  1.10  riastrad  * This is the implementation of the gem_prime_import functions for GEM drivers
   1113  1.10  riastrad  * using the PRIME helpers. Drivers can use this as their
   1114  1.10  riastrad  * &drm_driver.gem_prime_import implementation. It is used as the default
   1115  1.10  riastrad  * implementation in drm_gem_prime_fd_to_handle().
   1116  1.10  riastrad  *
   1117  1.10  riastrad  * Drivers must arrange to call drm_prime_gem_destroy() from their
   1118  1.10  riastrad  * &drm_gem_object_funcs.free hook when using this function.
   1119  1.10  riastrad  */
   1120  1.10  riastrad struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
   1121  1.10  riastrad 					    struct dma_buf *dma_buf)
   1122  1.10  riastrad {
   1123  1.16  riastrad #ifdef __NetBSD__
   1124  1.16  riastrad 	return drm_gem_prime_import_dev(dev, dma_buf, dev->dmat);
   1125  1.16  riastrad #else
   1126  1.10  riastrad 	return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
   1127  1.16  riastrad #endif
   1128  1.10  riastrad }
   1129  1.10  riastrad EXPORT_SYMBOL(drm_gem_prime_import);
   1130  1.10  riastrad 
   1131  1.12  riastrad #ifdef __NetBSD__
   1132   1.3  riastrad 
   1133   1.3  riastrad struct sg_table *
   1134   1.5  riastrad drm_prime_bus_dmamem_to_sg(bus_dma_tag_t dmat, const bus_dma_segment_t *segs,
   1135   1.5  riastrad     int nsegs)
   1136   1.4  riastrad {
   1137   1.4  riastrad 	struct sg_table *sg;
   1138   1.4  riastrad 	int ret;
   1139   1.4  riastrad 
   1140   1.4  riastrad 	sg = kmalloc(sizeof(*sg), GFP_KERNEL);
   1141   1.4  riastrad 	if (sg == NULL) {
   1142   1.4  riastrad 		ret = -ENOMEM;
   1143   1.4  riastrad 		goto out;
   1144   1.4  riastrad 	}
   1145   1.4  riastrad 
   1146   1.5  riastrad 	ret = sg_alloc_table_from_bus_dmamem(sg, dmat, segs, nsegs,
   1147   1.5  riastrad 	    GFP_KERNEL);
   1148   1.4  riastrad 	if (ret)
   1149   1.4  riastrad 		goto out;
   1150   1.4  riastrad 
   1151   1.4  riastrad 	return sg;
   1152   1.4  riastrad out:
   1153   1.4  riastrad 	kfree(sg);
   1154   1.4  riastrad 	return ERR_PTR(ret);
   1155   1.4  riastrad }
   1156   1.4  riastrad 
   1157   1.4  riastrad bus_size_t
   1158   1.4  riastrad drm_prime_sg_size(struct sg_table *sg)
   1159   1.4  riastrad {
   1160   1.4  riastrad 
   1161  1.16  riastrad 	return sg->sgl->sg_npgs << PAGE_SHIFT;
   1162   1.4  riastrad }
   1163   1.4  riastrad 
   1164   1.3  riastrad void
   1165   1.3  riastrad drm_prime_sg_free(struct sg_table *sg)
   1166   1.3  riastrad {
   1167   1.3  riastrad 
   1168   1.3  riastrad 	sg_free_table(sg);
   1169   1.3  riastrad 	kfree(sg);
   1170   1.3  riastrad }
   1171   1.3  riastrad 
   1172   1.3  riastrad int
   1173   1.5  riastrad drm_prime_sg_to_bus_dmamem(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
   1174   1.5  riastrad     int nsegs, int *rsegs, const struct sg_table *sgt)
   1175   1.3  riastrad {
   1176   1.3  riastrad 
   1177   1.3  riastrad 	/* XXX errno NetBSD->Linux */
   1178  1.16  riastrad 	return -bus_dmamem_import_pages(dmat, segs, nsegs, rsegs,
   1179  1.16  riastrad 	    sgt->sgl->sg_pgs, sgt->sgl->sg_npgs);
   1180   1.3  riastrad }
   1181   1.3  riastrad 
   1182   1.4  riastrad int
   1183   1.5  riastrad drm_prime_bus_dmamap_load_sgt(bus_dma_tag_t dmat, bus_dmamap_t map,
   1184   1.5  riastrad     struct sg_table *sgt)
   1185   1.4  riastrad {
   1186   1.5  riastrad 	bus_dma_segment_t *segs;
   1187   1.5  riastrad 	bus_size_t size = drm_prime_sg_size(sgt);
   1188  1.16  riastrad 	int nsegs = sgt->sgl->sg_npgs;
   1189   1.5  riastrad 	int ret;
   1190   1.5  riastrad 
   1191  1.16  riastrad 	segs = kcalloc(sgt->sgl->sg_npgs, sizeof(segs[0]), GFP_KERNEL);
   1192   1.5  riastrad 	if (segs == NULL) {
   1193   1.5  riastrad 		ret = -ENOMEM;
   1194   1.5  riastrad 		goto out0;
   1195   1.5  riastrad 	}
   1196   1.5  riastrad 
   1197   1.5  riastrad 	ret = drm_prime_sg_to_bus_dmamem(dmat, segs, nsegs, &nsegs, sgt);
   1198   1.5  riastrad 	if (ret)
   1199   1.5  riastrad 		goto out1;
   1200  1.16  riastrad 	KASSERT(nsegs <= sgt->sgl->sg_npgs);
   1201   1.5  riastrad 
   1202   1.5  riastrad 	/* XXX errno NetBSD->Linux */
   1203   1.5  riastrad 	ret = -bus_dmamap_load_raw(dmat, map, segs, nsegs, size,
   1204   1.5  riastrad 	    BUS_DMA_NOWAIT);
   1205   1.5  riastrad 	if (ret)
   1206   1.5  riastrad 		goto out1;
   1207   1.4  riastrad 
   1208   1.5  riastrad out1:	kfree(segs);
   1209   1.5  riastrad out0:	return ret;
   1210   1.4  riastrad }
   1211   1.4  riastrad 
   1212   1.7  riastrad bool
   1213   1.7  riastrad drm_prime_sg_importable(bus_dma_tag_t dmat, struct sg_table *sgt)
   1214   1.7  riastrad {
   1215   1.7  riastrad 	unsigned i;
   1216   1.7  riastrad 
   1217  1.16  riastrad 	for (i = 0; i < sgt->sgl->sg_npgs; i++) {
   1218  1.16  riastrad 		if (bus_dmatag_bounces_paddr(dmat,
   1219  1.16  riastrad 			VM_PAGE_TO_PHYS(&sgt->sgl->sg_pgs[i]->p_vmp)))
   1220   1.7  riastrad 			return false;
   1221   1.7  riastrad 	}
   1222   1.7  riastrad 	return true;
   1223   1.7  riastrad }
   1224   1.7  riastrad 
   1225   1.3  riastrad #else  /* !__NetBSD__ */
   1226   1.3  riastrad 
   1227   1.2  riastrad /**
   1228   1.2  riastrad  * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
   1229   1.2  riastrad  * @sgt: scatter-gather table to convert
   1230  1.10  riastrad  * @pages: optional array of page pointers to store the page array in
   1231   1.2  riastrad  * @addrs: optional array to store the dma bus address of each page
   1232  1.10  riastrad  * @max_entries: size of both the passed-in arrays
   1233   1.2  riastrad  *
   1234   1.2  riastrad  * Exports an sg table into an array of pages and addresses. This is currently
   1235   1.2  riastrad  * required by the TTM driver in order to do correct fault handling.
   1236  1.10  riastrad  *
   1237  1.10  riastrad  * Drivers can use this in their &drm_driver.gem_prime_import_sg_table
   1238  1.10  riastrad  * implementation.
   1239   1.2  riastrad  */
   1240   1.1  riastrad int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
   1241  1.10  riastrad 				     dma_addr_t *addrs, int max_entries)
   1242   1.1  riastrad {
   1243   1.1  riastrad 	unsigned count;
   1244   1.1  riastrad 	struct scatterlist *sg;
   1245   1.1  riastrad 	struct page *page;
   1246  1.10  riastrad 	u32 len, index;
   1247   1.1  riastrad 	dma_addr_t addr;
   1248   1.1  riastrad 
   1249  1.10  riastrad 	index = 0;
   1250   1.1  riastrad 	for_each_sg(sgt->sgl, sg, sgt->nents, count) {
   1251   1.1  riastrad 		len = sg->length;
   1252   1.1  riastrad 		page = sg_page(sg);
   1253   1.1  riastrad 		addr = sg_dma_address(sg);
   1254   1.1  riastrad 
   1255   1.1  riastrad 		while (len > 0) {
   1256  1.10  riastrad 			if (WARN_ON(index >= max_entries))
   1257   1.1  riastrad 				return -1;
   1258  1.10  riastrad 			if (pages)
   1259  1.10  riastrad 				pages[index] = page;
   1260   1.1  riastrad 			if (addrs)
   1261  1.10  riastrad 				addrs[index] = addr;
   1262   1.1  riastrad 
   1263   1.1  riastrad 			page++;
   1264   1.1  riastrad 			addr += PAGE_SIZE;
   1265   1.1  riastrad 			len -= PAGE_SIZE;
   1266  1.10  riastrad 			index++;
   1267   1.1  riastrad 		}
   1268   1.1  riastrad 	}
   1269   1.1  riastrad 	return 0;
   1270   1.1  riastrad }
   1271   1.1  riastrad EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
   1272   1.2  riastrad 
   1273   1.3  riastrad #endif	/* __NetBSD__ */
   1274   1.3  riastrad 
   1275   1.2  riastrad /**
   1276   1.2  riastrad  * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
   1277   1.2  riastrad  * @obj: GEM object which was created from a dma-buf
   1278   1.2  riastrad  * @sg: the sg-table which was pinned at import time
   1279   1.2  riastrad  *
   1280   1.2  riastrad  * This is the cleanup functions which GEM drivers need to call when they use
   1281  1.10  riastrad  * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs.
   1282   1.2  riastrad  */
   1283   1.1  riastrad void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
   1284   1.1  riastrad {
   1285   1.1  riastrad 	struct dma_buf_attachment *attach;
   1286   1.1  riastrad 	struct dma_buf *dma_buf;
   1287   1.1  riastrad 	attach = obj->import_attach;
   1288   1.1  riastrad 	if (sg)
   1289   1.1  riastrad 		dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
   1290   1.1  riastrad 	dma_buf = attach->dmabuf;
   1291   1.1  riastrad 	dma_buf_detach(attach->dmabuf, attach);
   1292   1.1  riastrad 	/* remove the reference */
   1293   1.1  riastrad 	dma_buf_put(dma_buf);
   1294   1.1  riastrad }
   1295   1.1  riastrad EXPORT_SYMBOL(drm_prime_gem_destroy);
   1296