File Index
Files and directories
Sign in
FILE

vgem_fence.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/vgem/vgem_fence.c C
  1/*
  2 * Copyright 2016 Intel Corporation
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software")
  6 * to deal in the software without restriction, including without limitation
  7 * on the rights to use, copy, modify, merge, publish, distribute, sub
  8 * license, and/or sell copies of the Software, and to permit persons to whom
  9 * them Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice (including the next
 12 * paragraph) shall be included in all copies or substantial portions of the
 13 * Software.
 14 *
 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 18 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
 19 * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
 20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21 */
 22
 23#include <linux/dma-buf.h>
 24#include <linux/dma-resv.h>
 25
 26#include <drm/drm_file.h>
 27
 28#include "vgem_drv.h"
 29
 30#define VGEM_FENCE_TIMEOUT (10*HZ)
 31
 32struct vgem_fence {
 33	struct dma_fence base;
 34	struct spinlock lock;
 35	struct timer_list timer;
 36};
 37
 38static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
 39{
 40	return "vgem";
 41}
 42
 43static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
 44{
 45	return "unbound";
 46}
 47
 48static void vgem_fence_release(struct dma_fence *base)
 49{
 50	struct vgem_fence *fence = container_of(base, typeof(*fence), base);
 51
 52	timer_delete_sync(&fence->timer);
 53	dma_fence_free(&fence->base);
 54}
 55
 56static const struct dma_fence_ops vgem_fence_ops = {
 57	.get_driver_name = vgem_fence_get_driver_name,
 58	.get_timeline_name = vgem_fence_get_timeline_name,
 59	.release = vgem_fence_release,
 60};
 61
 62static void vgem_fence_timeout(struct timer_list *t)
 63{
 64	struct vgem_fence *fence = timer_container_of(fence, t, timer);
 65
 66	dma_fence_signal(&fence->base);
 67}
 68
 69static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
 70					   unsigned int flags)
 71{
 72	struct vgem_fence *fence;
 73
 74	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
 75	if (!fence)
 76		return NULL;
 77
 78	spin_lock_init(&fence->lock);
 79	dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
 80		       dma_fence_context_alloc(1), 1);
 81
 82	timer_setup(&fence->timer, vgem_fence_timeout, TIMER_IRQSAFE);
 83
 84	/* We force the fence to expire within 10s to prevent driver hangs */
 85	mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
 86
 87	return &fence->base;
 88}
 89
 90/*
 91 * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
 92 *
 93 * Create and attach a fence to the vGEM handle. This fence is then exposed
 94 * via the dma-buf reservation object and visible to consumers of the exported
 95 * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
 96 * vGEM buffer is being written to by the client and is exposed as an exclusive
 97 * fence, otherwise the fence indicates the client is current reading from the
 98 * buffer and all future writes should wait for the client to signal its
 99 * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
100 * an exclusive fence when adding a read, or any fence when adding a write),
101 * -EBUSY is reported. Serialisation between operations should be handled
102 * by waiting upon the dma-buf.
103 *
104 * This returns the handle for the new fence that must be signaled within 10
105 * seconds (or otherwise it will automatically expire). See
106 * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
107 *
108 * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
109 */
110int vgem_fence_attach_ioctl(struct drm_device *dev,
111			    void *data,
112			    struct drm_file *file)
113{
114	struct drm_vgem_fence_attach *arg = data;
115	struct vgem_file *vfile = file->driver_priv;
116	struct dma_resv *resv;
117	struct drm_gem_object *obj;
118	enum dma_resv_usage usage;
119	struct dma_fence *fence;
120	int ret;
121
122	if (arg->flags & ~VGEM_FENCE_WRITE)
123		return -EINVAL;
124
125	if (arg->pad)
126		return -EINVAL;
127
128	obj = drm_gem_object_lookup(file, arg->handle);
129	if (!obj)
130		return -ENOENT;
131
132	fence = vgem_fence_create(vfile, arg->flags);
133	if (!fence) {
134		ret = -ENOMEM;
135		goto err;
136	}
137
138	/* Check for a conflicting fence */
139	resv = obj->resv;
140	usage = dma_resv_usage_rw(arg->flags & VGEM_FENCE_WRITE);
141	if (!dma_resv_test_signaled(resv, usage)) {
142		ret = -EBUSY;
143		goto err_fence;
144	}
145
146	/* Expose the fence via the dma-buf */
147	dma_resv_lock(resv, NULL);
148	ret = dma_resv_reserve_fences(resv, 1);
149	if (!ret)
150		dma_resv_add_fence(resv, fence, arg->flags & VGEM_FENCE_WRITE ?
151				   DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
152	dma_resv_unlock(resv);
153
154	/* Record the fence in our idr for later signaling */
155	if (ret == 0) {
156		mutex_lock(&vfile->fence_mutex);
157		ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
158		mutex_unlock(&vfile->fence_mutex);
159		if (ret > 0) {
160			arg->out_fence = ret;
161			ret = 0;
162		}
163	}
164err_fence:
165	if (ret) {
166		dma_fence_signal(fence);
167		dma_fence_put(fence);
168	}
169err:
170	drm_gem_object_put(obj);
171	return ret;
172}
173
174/*
175 * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
176 *
177 * Signal and consume a fence ealier attached to a vGEM handle using
178 * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
179 *
180 * All fences must be signaled within 10s of attachment or otherwise they
181 * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
182 *
183 * Signaling a fence indicates to all consumers of the dma-buf that the
184 * client has completed the operation associated with the fence, and that the
185 * buffer is then ready for consumption.
186 *
187 * If the fence does not exist (or has already been signaled by the client),
188 * vgem_fence_signal_ioctl returns -ENOENT.
189 */
190int vgem_fence_signal_ioctl(struct drm_device *dev,
191			    void *data,
192			    struct drm_file *file)
193{
194	struct vgem_file *vfile = file->driver_priv;
195	struct drm_vgem_fence_signal *arg = data;
196	struct dma_fence *fence;
197	int ret = 0;
198
199	if (arg->flags)
200		return -EINVAL;
201
202	mutex_lock(&vfile->fence_mutex);
203	fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
204	mutex_unlock(&vfile->fence_mutex);
205	if (!fence)
206		return -ENOENT;
207	if (IS_ERR(fence))
208		return PTR_ERR(fence);
209
210	if (dma_fence_is_signaled(fence))
211		ret = -ETIMEDOUT;
212
213	dma_fence_signal(fence);
214	dma_fence_put(fence);
215	return ret;
216}
217
218int vgem_fence_open(struct vgem_file *vfile)
219{
220	mutex_init(&vfile->fence_mutex);
221	idr_init_base(&vfile->fence_idr, 1);
222
223	return 0;
224}
225
226static int __vgem_fence_idr_fini(int id, void *p, void *data)
227{
228	dma_fence_signal(p);
229	dma_fence_put(p);
230	return 0;
231}
232
233void vgem_fence_close(struct vgem_file *vfile)
234{
235	idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
236	idr_destroy(&vfile->fence_idr);
237	mutex_destroy(&vfile->fence_mutex);
238}
~/ Read-only file preview.