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