162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2016 Intel Corporation
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software")
662306a36Sopenharmony_ci * to deal in the software without restriction, including without limitation
762306a36Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
862306a36Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
962306a36Sopenharmony_ci * them Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the next
1262306a36Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
1362306a36Sopenharmony_ci * Software.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1662306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
1762306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
1862306a36Sopenharmony_ci * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
1962306a36Sopenharmony_ci * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
2062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#include <linux/dma-buf.h>
2462306a36Sopenharmony_ci#include <linux/dma-resv.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include <drm/drm_file.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include "vgem_drv.h"
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define VGEM_FENCE_TIMEOUT (10*HZ)
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistruct vgem_fence {
3362306a36Sopenharmony_ci	struct dma_fence base;
3462306a36Sopenharmony_ci	struct spinlock lock;
3562306a36Sopenharmony_ci	struct timer_list timer;
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic const char *vgem_fence_get_driver_name(struct dma_fence *fence)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	return "vgem";
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
4462306a36Sopenharmony_ci{
4562306a36Sopenharmony_ci	return "unbound";
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic void vgem_fence_release(struct dma_fence *base)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	struct vgem_fence *fence = container_of(base, typeof(*fence), base);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	del_timer_sync(&fence->timer);
5362306a36Sopenharmony_ci	dma_fence_free(&fence->base);
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic void vgem_fence_value_str(struct dma_fence *fence, char *str, int size)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	snprintf(str, size, "%llu", fence->seqno);
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cistatic void vgem_fence_timeline_value_str(struct dma_fence *fence, char *str,
6262306a36Sopenharmony_ci					  int size)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	snprintf(str, size, "%llu",
6562306a36Sopenharmony_ci		 dma_fence_is_signaled(fence) ? fence->seqno : 0);
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic const struct dma_fence_ops vgem_fence_ops = {
6962306a36Sopenharmony_ci	.get_driver_name = vgem_fence_get_driver_name,
7062306a36Sopenharmony_ci	.get_timeline_name = vgem_fence_get_timeline_name,
7162306a36Sopenharmony_ci	.release = vgem_fence_release,
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	.fence_value_str = vgem_fence_value_str,
7462306a36Sopenharmony_ci	.timeline_value_str = vgem_fence_timeline_value_str,
7562306a36Sopenharmony_ci};
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cistatic void vgem_fence_timeout(struct timer_list *t)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	struct vgem_fence *fence = from_timer(fence, t, timer);
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	dma_fence_signal(&fence->base);
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
8562306a36Sopenharmony_ci					   unsigned int flags)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	struct vgem_fence *fence;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
9062306a36Sopenharmony_ci	if (!fence)
9162306a36Sopenharmony_ci		return NULL;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	spin_lock_init(&fence->lock);
9462306a36Sopenharmony_ci	dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
9562306a36Sopenharmony_ci		       dma_fence_context_alloc(1), 1);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	timer_setup(&fence->timer, vgem_fence_timeout, 0);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	/* We force the fence to expire within 10s to prevent driver hangs */
10062306a36Sopenharmony_ci	mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	return &fence->base;
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci/*
10662306a36Sopenharmony_ci * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * Create and attach a fence to the vGEM handle. This fence is then exposed
10962306a36Sopenharmony_ci * via the dma-buf reservation object and visible to consumers of the exported
11062306a36Sopenharmony_ci * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
11162306a36Sopenharmony_ci * vGEM buffer is being written to by the client and is exposed as an exclusive
11262306a36Sopenharmony_ci * fence, otherwise the fence indicates the client is current reading from the
11362306a36Sopenharmony_ci * buffer and all future writes should wait for the client to signal its
11462306a36Sopenharmony_ci * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
11562306a36Sopenharmony_ci * an exclusive fence when adding a read, or any fence when adding a write),
11662306a36Sopenharmony_ci * -EBUSY is reported. Serialisation between operations should be handled
11762306a36Sopenharmony_ci * by waiting upon the dma-buf.
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci * This returns the handle for the new fence that must be signaled within 10
12062306a36Sopenharmony_ci * seconds (or otherwise it will automatically expire). See
12162306a36Sopenharmony_ci * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
12262306a36Sopenharmony_ci *
12362306a36Sopenharmony_ci * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
12462306a36Sopenharmony_ci */
12562306a36Sopenharmony_ciint vgem_fence_attach_ioctl(struct drm_device *dev,
12662306a36Sopenharmony_ci			    void *data,
12762306a36Sopenharmony_ci			    struct drm_file *file)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	struct drm_vgem_fence_attach *arg = data;
13062306a36Sopenharmony_ci	struct vgem_file *vfile = file->driver_priv;
13162306a36Sopenharmony_ci	struct dma_resv *resv;
13262306a36Sopenharmony_ci	struct drm_gem_object *obj;
13362306a36Sopenharmony_ci	enum dma_resv_usage usage;
13462306a36Sopenharmony_ci	struct dma_fence *fence;
13562306a36Sopenharmony_ci	int ret;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	if (arg->flags & ~VGEM_FENCE_WRITE)
13862306a36Sopenharmony_ci		return -EINVAL;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	if (arg->pad)
14162306a36Sopenharmony_ci		return -EINVAL;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	obj = drm_gem_object_lookup(file, arg->handle);
14462306a36Sopenharmony_ci	if (!obj)
14562306a36Sopenharmony_ci		return -ENOENT;
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	fence = vgem_fence_create(vfile, arg->flags);
14862306a36Sopenharmony_ci	if (!fence) {
14962306a36Sopenharmony_ci		ret = -ENOMEM;
15062306a36Sopenharmony_ci		goto err;
15162306a36Sopenharmony_ci	}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	/* Check for a conflicting fence */
15462306a36Sopenharmony_ci	resv = obj->resv;
15562306a36Sopenharmony_ci	usage = dma_resv_usage_rw(arg->flags & VGEM_FENCE_WRITE);
15662306a36Sopenharmony_ci	if (!dma_resv_test_signaled(resv, usage)) {
15762306a36Sopenharmony_ci		ret = -EBUSY;
15862306a36Sopenharmony_ci		goto err_fence;
15962306a36Sopenharmony_ci	}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	/* Expose the fence via the dma-buf */
16262306a36Sopenharmony_ci	dma_resv_lock(resv, NULL);
16362306a36Sopenharmony_ci	ret = dma_resv_reserve_fences(resv, 1);
16462306a36Sopenharmony_ci	if (!ret)
16562306a36Sopenharmony_ci		dma_resv_add_fence(resv, fence, arg->flags & VGEM_FENCE_WRITE ?
16662306a36Sopenharmony_ci				   DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
16762306a36Sopenharmony_ci	dma_resv_unlock(resv);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	/* Record the fence in our idr for later signaling */
17062306a36Sopenharmony_ci	if (ret == 0) {
17162306a36Sopenharmony_ci		mutex_lock(&vfile->fence_mutex);
17262306a36Sopenharmony_ci		ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
17362306a36Sopenharmony_ci		mutex_unlock(&vfile->fence_mutex);
17462306a36Sopenharmony_ci		if (ret > 0) {
17562306a36Sopenharmony_ci			arg->out_fence = ret;
17662306a36Sopenharmony_ci			ret = 0;
17762306a36Sopenharmony_ci		}
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_cierr_fence:
18062306a36Sopenharmony_ci	if (ret) {
18162306a36Sopenharmony_ci		dma_fence_signal(fence);
18262306a36Sopenharmony_ci		dma_fence_put(fence);
18362306a36Sopenharmony_ci	}
18462306a36Sopenharmony_cierr:
18562306a36Sopenharmony_ci	drm_gem_object_put(obj);
18662306a36Sopenharmony_ci	return ret;
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/*
19062306a36Sopenharmony_ci * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
19162306a36Sopenharmony_ci *
19262306a36Sopenharmony_ci * Signal and consume a fence ealier attached to a vGEM handle using
19362306a36Sopenharmony_ci * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
19462306a36Sopenharmony_ci *
19562306a36Sopenharmony_ci * All fences must be signaled within 10s of attachment or otherwise they
19662306a36Sopenharmony_ci * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
19762306a36Sopenharmony_ci *
19862306a36Sopenharmony_ci * Signaling a fence indicates to all consumers of the dma-buf that the
19962306a36Sopenharmony_ci * client has completed the operation associated with the fence, and that the
20062306a36Sopenharmony_ci * buffer is then ready for consumption.
20162306a36Sopenharmony_ci *
20262306a36Sopenharmony_ci * If the fence does not exist (or has already been signaled by the client),
20362306a36Sopenharmony_ci * vgem_fence_signal_ioctl returns -ENOENT.
20462306a36Sopenharmony_ci */
20562306a36Sopenharmony_ciint vgem_fence_signal_ioctl(struct drm_device *dev,
20662306a36Sopenharmony_ci			    void *data,
20762306a36Sopenharmony_ci			    struct drm_file *file)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	struct vgem_file *vfile = file->driver_priv;
21062306a36Sopenharmony_ci	struct drm_vgem_fence_signal *arg = data;
21162306a36Sopenharmony_ci	struct dma_fence *fence;
21262306a36Sopenharmony_ci	int ret = 0;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	if (arg->flags)
21562306a36Sopenharmony_ci		return -EINVAL;
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	mutex_lock(&vfile->fence_mutex);
21862306a36Sopenharmony_ci	fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
21962306a36Sopenharmony_ci	mutex_unlock(&vfile->fence_mutex);
22062306a36Sopenharmony_ci	if (!fence)
22162306a36Sopenharmony_ci		return -ENOENT;
22262306a36Sopenharmony_ci	if (IS_ERR(fence))
22362306a36Sopenharmony_ci		return PTR_ERR(fence);
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	if (dma_fence_is_signaled(fence))
22662306a36Sopenharmony_ci		ret = -ETIMEDOUT;
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	dma_fence_signal(fence);
22962306a36Sopenharmony_ci	dma_fence_put(fence);
23062306a36Sopenharmony_ci	return ret;
23162306a36Sopenharmony_ci}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ciint vgem_fence_open(struct vgem_file *vfile)
23462306a36Sopenharmony_ci{
23562306a36Sopenharmony_ci	mutex_init(&vfile->fence_mutex);
23662306a36Sopenharmony_ci	idr_init_base(&vfile->fence_idr, 1);
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	return 0;
23962306a36Sopenharmony_ci}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_cistatic int __vgem_fence_idr_fini(int id, void *p, void *data)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	dma_fence_signal(p);
24462306a36Sopenharmony_ci	dma_fence_put(p);
24562306a36Sopenharmony_ci	return 0;
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_civoid vgem_fence_close(struct vgem_file *vfile)
24962306a36Sopenharmony_ci{
25062306a36Sopenharmony_ci	idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
25162306a36Sopenharmony_ci	idr_destroy(&vfile->fence_idr);
25262306a36Sopenharmony_ci	mutex_destroy(&vfile->fence_mutex);
25362306a36Sopenharmony_ci}
254