Home | History | Annotate | Line # | Download | only in vgem
      1  1.3  riastrad /*	$NetBSD: vgem_fence.c,v 1.3 2021/12/19 11:06:55 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2016 Intel Corporation
      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  * on the rights to use, copy, modify, merge, publish, distribute, sub
     10  1.1  riastrad  * license, and/or sell copies of the Software, and to permit persons to whom
     11  1.1  riastrad  * them 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 MERCHANTIBILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
     21  1.1  riastrad  * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
     22  1.1  riastrad  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  */
     24  1.1  riastrad 
     25  1.1  riastrad #include <sys/cdefs.h>
     26  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: vgem_fence.c,v 1.3 2021/12/19 11:06:55 riastradh Exp $");
     27  1.1  riastrad 
     28  1.1  riastrad #include <linux/dma-buf.h>
     29  1.1  riastrad #include <linux/dma-resv.h>
     30  1.1  riastrad 
     31  1.1  riastrad #include <drm/drm_file.h>
     32  1.1  riastrad 
     33  1.1  riastrad #include "vgem_drv.h"
     34  1.1  riastrad 
     35  1.1  riastrad #define VGEM_FENCE_TIMEOUT (10*HZ)
     36  1.1  riastrad 
     37  1.1  riastrad struct vgem_fence {
     38  1.1  riastrad 	struct dma_fence base;
     39  1.1  riastrad 	struct spinlock lock;
     40  1.1  riastrad 	struct timer_list timer;
     41  1.1  riastrad };
     42  1.1  riastrad 
     43  1.1  riastrad static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
     44  1.1  riastrad {
     45  1.1  riastrad 	return "vgem";
     46  1.1  riastrad }
     47  1.1  riastrad 
     48  1.1  riastrad static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
     49  1.1  riastrad {
     50  1.1  riastrad 	return "unbound";
     51  1.1  riastrad }
     52  1.1  riastrad 
     53  1.1  riastrad static void vgem_fence_release(struct dma_fence *base)
     54  1.1  riastrad {
     55  1.1  riastrad 	struct vgem_fence *fence = container_of(base, typeof(*fence), base);
     56  1.1  riastrad 
     57  1.1  riastrad 	del_timer_sync(&fence->timer);
     58  1.1  riastrad 	dma_fence_free(&fence->base);
     59  1.1  riastrad }
     60  1.1  riastrad 
     61  1.1  riastrad static void vgem_fence_value_str(struct dma_fence *fence, char *str, int size)
     62  1.1  riastrad {
     63  1.1  riastrad 	snprintf(str, size, "%llu", fence->seqno);
     64  1.1  riastrad }
     65  1.1  riastrad 
     66  1.1  riastrad static void vgem_fence_timeline_value_str(struct dma_fence *fence, char *str,
     67  1.1  riastrad 					  int size)
     68  1.1  riastrad {
     69  1.1  riastrad 	snprintf(str, size, "%llu",
     70  1.1  riastrad 		 dma_fence_is_signaled(fence) ? fence->seqno : 0);
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad static const struct dma_fence_ops vgem_fence_ops = {
     74  1.1  riastrad 	.get_driver_name = vgem_fence_get_driver_name,
     75  1.1  riastrad 	.get_timeline_name = vgem_fence_get_timeline_name,
     76  1.1  riastrad 	.release = vgem_fence_release,
     77  1.1  riastrad 
     78  1.1  riastrad 	.fence_value_str = vgem_fence_value_str,
     79  1.1  riastrad 	.timeline_value_str = vgem_fence_timeline_value_str,
     80  1.1  riastrad };
     81  1.1  riastrad 
     82  1.1  riastrad static void vgem_fence_timeout(struct timer_list *t)
     83  1.1  riastrad {
     84  1.1  riastrad 	struct vgem_fence *fence = from_timer(fence, t, timer);
     85  1.1  riastrad 
     86  1.1  riastrad 	dma_fence_signal(&fence->base);
     87  1.1  riastrad }
     88  1.1  riastrad 
     89  1.1  riastrad static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
     90  1.1  riastrad 					   unsigned int flags)
     91  1.1  riastrad {
     92  1.1  riastrad 	struct vgem_fence *fence;
     93  1.1  riastrad 
     94  1.1  riastrad 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
     95  1.1  riastrad 	if (!fence)
     96  1.1  riastrad 		return NULL;
     97  1.1  riastrad 
     98  1.1  riastrad 	spin_lock_init(&fence->lock);
     99  1.1  riastrad 	dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
    100  1.1  riastrad 		       dma_fence_context_alloc(1), 1);
    101  1.1  riastrad 
    102  1.1  riastrad 	timer_setup(&fence->timer, vgem_fence_timeout, 0);
    103  1.1  riastrad 
    104  1.1  riastrad 	/* We force the fence to expire within 10s to prevent driver hangs */
    105  1.1  riastrad 	mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
    106  1.1  riastrad 
    107  1.1  riastrad 	return &fence->base;
    108  1.1  riastrad }
    109  1.1  riastrad 
    110  1.1  riastrad /*
    111  1.1  riastrad  * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
    112  1.1  riastrad  *
    113  1.1  riastrad  * Create and attach a fence to the vGEM handle. This fence is then exposed
    114  1.1  riastrad  * via the dma-buf reservation object and visible to consumers of the exported
    115  1.1  riastrad  * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
    116  1.1  riastrad  * vGEM buffer is being written to by the client and is exposed as an exclusive
    117  1.1  riastrad  * fence, otherwise the fence indicates the client is current reading from the
    118  1.1  riastrad  * buffer and all future writes should wait for the client to signal its
    119  1.1  riastrad  * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
    120  1.1  riastrad  * an exclusive fence when adding a read, or any fence when adding a write),
    121  1.1  riastrad  * -EBUSY is reported. Serialisation between operations should be handled
    122  1.1  riastrad  * by waiting upon the dma-buf.
    123  1.1  riastrad  *
    124  1.1  riastrad  * This returns the handle for the new fence that must be signaled within 10
    125  1.1  riastrad  * seconds (or otherwise it will automatically expire). See
    126  1.1  riastrad  * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
    127  1.1  riastrad  *
    128  1.1  riastrad  * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
    129  1.1  riastrad  */
    130  1.1  riastrad int vgem_fence_attach_ioctl(struct drm_device *dev,
    131  1.1  riastrad 			    void *data,
    132  1.1  riastrad 			    struct drm_file *file)
    133  1.1  riastrad {
    134  1.1  riastrad 	struct drm_vgem_fence_attach *arg = data;
    135  1.1  riastrad 	struct vgem_file *vfile = file->driver_priv;
    136  1.1  riastrad 	struct dma_resv *resv;
    137  1.1  riastrad 	struct drm_gem_object *obj;
    138  1.1  riastrad 	struct dma_fence *fence;
    139  1.1  riastrad 	int ret;
    140  1.1  riastrad 
    141  1.1  riastrad 	if (arg->flags & ~VGEM_FENCE_WRITE)
    142  1.1  riastrad 		return -EINVAL;
    143  1.1  riastrad 
    144  1.1  riastrad 	if (arg->pad)
    145  1.1  riastrad 		return -EINVAL;
    146  1.1  riastrad 
    147  1.1  riastrad 	obj = drm_gem_object_lookup(file, arg->handle);
    148  1.1  riastrad 	if (!obj)
    149  1.1  riastrad 		return -ENOENT;
    150  1.1  riastrad 
    151  1.1  riastrad 	fence = vgem_fence_create(vfile, arg->flags);
    152  1.1  riastrad 	if (!fence) {
    153  1.1  riastrad 		ret = -ENOMEM;
    154  1.1  riastrad 		goto err;
    155  1.1  riastrad 	}
    156  1.1  riastrad 
    157  1.1  riastrad 	/* Check for a conflicting fence */
    158  1.1  riastrad 	resv = obj->resv;
    159  1.1  riastrad 	if (!dma_resv_test_signaled_rcu(resv,
    160  1.1  riastrad 						  arg->flags & VGEM_FENCE_WRITE)) {
    161  1.1  riastrad 		ret = -EBUSY;
    162  1.1  riastrad 		goto err_fence;
    163  1.1  riastrad 	}
    164  1.1  riastrad 
    165  1.1  riastrad 	/* Expose the fence via the dma-buf */
    166  1.1  riastrad 	ret = 0;
    167  1.1  riastrad 	dma_resv_lock(resv, NULL);
    168  1.1  riastrad 	if (arg->flags & VGEM_FENCE_WRITE)
    169  1.1  riastrad 		dma_resv_add_excl_fence(resv, fence);
    170  1.1  riastrad 	else if ((ret = dma_resv_reserve_shared(resv, 1)) == 0)
    171  1.1  riastrad 		dma_resv_add_shared_fence(resv, fence);
    172  1.1  riastrad 	dma_resv_unlock(resv);
    173  1.1  riastrad 
    174  1.1  riastrad 	/* Record the fence in our idr for later signaling */
    175  1.1  riastrad 	if (ret == 0) {
    176  1.3  riastrad 		idr_preload(GFP_KERNEL);
    177  1.1  riastrad 		mutex_lock(&vfile->fence_mutex);
    178  1.1  riastrad 		ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
    179  1.1  riastrad 		mutex_unlock(&vfile->fence_mutex);
    180  1.3  riastrad 		idr_preload_end();
    181  1.1  riastrad 		if (ret > 0) {
    182  1.1  riastrad 			arg->out_fence = ret;
    183  1.1  riastrad 			ret = 0;
    184  1.1  riastrad 		}
    185  1.1  riastrad 	}
    186  1.1  riastrad err_fence:
    187  1.1  riastrad 	if (ret) {
    188  1.1  riastrad 		dma_fence_signal(fence);
    189  1.1  riastrad 		dma_fence_put(fence);
    190  1.1  riastrad 	}
    191  1.1  riastrad err:
    192  1.1  riastrad 	drm_gem_object_put_unlocked(obj);
    193  1.1  riastrad 	return ret;
    194  1.1  riastrad }
    195  1.1  riastrad 
    196  1.1  riastrad /*
    197  1.1  riastrad  * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
    198  1.1  riastrad  *
    199  1.1  riastrad  * Signal and consume a fence ealier attached to a vGEM handle using
    200  1.1  riastrad  * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
    201  1.1  riastrad  *
    202  1.1  riastrad  * All fences must be signaled within 10s of attachment or otherwise they
    203  1.1  riastrad  * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
    204  1.1  riastrad  *
    205  1.1  riastrad  * Signaling a fence indicates to all consumers of the dma-buf that the
    206  1.1  riastrad  * client has completed the operation associated with the fence, and that the
    207  1.1  riastrad  * buffer is then ready for consumption.
    208  1.1  riastrad  *
    209  1.1  riastrad  * If the fence does not exist (or has already been signaled by the client),
    210  1.1  riastrad  * vgem_fence_signal_ioctl returns -ENOENT.
    211  1.1  riastrad  */
    212  1.1  riastrad int vgem_fence_signal_ioctl(struct drm_device *dev,
    213  1.1  riastrad 			    void *data,
    214  1.1  riastrad 			    struct drm_file *file)
    215  1.1  riastrad {
    216  1.1  riastrad 	struct vgem_file *vfile = file->driver_priv;
    217  1.1  riastrad 	struct drm_vgem_fence_signal *arg = data;
    218  1.1  riastrad 	struct dma_fence *fence;
    219  1.1  riastrad 	int ret = 0;
    220  1.1  riastrad 
    221  1.1  riastrad 	if (arg->flags)
    222  1.1  riastrad 		return -EINVAL;
    223  1.1  riastrad 
    224  1.1  riastrad 	mutex_lock(&vfile->fence_mutex);
    225  1.1  riastrad 	fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
    226  1.1  riastrad 	mutex_unlock(&vfile->fence_mutex);
    227  1.1  riastrad 	if (!fence)
    228  1.1  riastrad 		return -ENOENT;
    229  1.1  riastrad 	if (IS_ERR(fence))
    230  1.1  riastrad 		return PTR_ERR(fence);
    231  1.1  riastrad 
    232  1.1  riastrad 	if (dma_fence_is_signaled(fence))
    233  1.1  riastrad 		ret = -ETIMEDOUT;
    234  1.1  riastrad 
    235  1.1  riastrad 	dma_fence_signal(fence);
    236  1.1  riastrad 	dma_fence_put(fence);
    237  1.1  riastrad 	return ret;
    238  1.1  riastrad }
    239  1.1  riastrad 
    240  1.1  riastrad int vgem_fence_open(struct vgem_file *vfile)
    241  1.1  riastrad {
    242  1.1  riastrad 	mutex_init(&vfile->fence_mutex);
    243  1.1  riastrad 	idr_init(&vfile->fence_idr);
    244  1.1  riastrad 
    245  1.1  riastrad 	return 0;
    246  1.1  riastrad }
    247  1.1  riastrad 
    248  1.1  riastrad static int __vgem_fence_idr_fini(int id, void *p, void *data)
    249  1.1  riastrad {
    250  1.1  riastrad 	dma_fence_signal(p);
    251  1.1  riastrad 	dma_fence_put(p);
    252  1.1  riastrad 	return 0;
    253  1.1  riastrad }
    254  1.1  riastrad 
    255  1.1  riastrad void vgem_fence_close(struct vgem_file *vfile)
    256  1.1  riastrad {
    257  1.1  riastrad 	idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
    258  1.1  riastrad 	idr_destroy(&vfile->fence_idr);
    259  1.1  riastrad }
    260