xref: /linux/drivers/gpu/drm/xe/xe_sync.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include "xe_sync.h"
7 
8 #include <linux/dma-fence-array.h>
9 #include <linux/dma-fence-unwrap.h>
10 #include <linux/error-injection.h>
11 #include <linux/kthread.h>
12 #include <linux/sched/mm.h>
13 #include <linux/uaccess.h>
14 
15 #include <drm/drm_print.h>
16 #include <drm/drm_syncobj.h>
17 #include <uapi/drm/xe_drm.h>
18 
19 #include "xe_device.h"
20 #include "xe_exec_queue.h"
21 #include "xe_macros.h"
22 #include "xe_sched_job_types.h"
23 
24 struct xe_user_fence {
25 	struct xe_device *xe;
26 	struct kref refcount;
27 	struct dma_fence_cb cb;
28 	struct work_struct worker;
29 	struct mm_struct *mm;
30 	u64 __user *addr;
31 	u64 value;
32 	int signalled;
33 };
34 
35 static void user_fence_destroy(struct kref *kref)
36 {
37 	struct xe_user_fence *ufence = container_of(kref, struct xe_user_fence,
38 						 refcount);
39 
40 	mmdrop(ufence->mm);
41 	kfree(ufence);
42 }
43 
44 static void user_fence_get(struct xe_user_fence *ufence)
45 {
46 	kref_get(&ufence->refcount);
47 }
48 
49 static void user_fence_put(struct xe_user_fence *ufence)
50 {
51 	kref_put(&ufence->refcount, user_fence_destroy);
52 }
53 
54 static struct xe_user_fence *user_fence_create(struct xe_device *xe, u64 addr,
55 					       u64 value)
56 {
57 	struct xe_user_fence *ufence;
58 	u64 __user *ptr = u64_to_user_ptr(addr);
59 	u64 __maybe_unused prefetch_val;
60 
61 	if (get_user(prefetch_val, ptr))
62 		return ERR_PTR(-EFAULT);
63 
64 	ufence = kzalloc_obj(*ufence);
65 	if (!ufence)
66 		return ERR_PTR(-ENOMEM);
67 
68 	ufence->xe = xe;
69 	kref_init(&ufence->refcount);
70 	ufence->addr = ptr;
71 	ufence->value = value;
72 	ufence->mm = current->mm;
73 	mmgrab(ufence->mm);
74 
75 	return ufence;
76 }
77 
78 static void user_fence_worker(struct work_struct *w)
79 {
80 	struct xe_user_fence *ufence = container_of(w, struct xe_user_fence, worker);
81 
82 	WRITE_ONCE(ufence->signalled, 1);
83 	if (mmget_not_zero(ufence->mm)) {
84 		kthread_use_mm(ufence->mm);
85 		if (copy_to_user(ufence->addr, &ufence->value, sizeof(ufence->value)))
86 			XE_WARN_ON("Copy to user failed");
87 		kthread_unuse_mm(ufence->mm);
88 		mmput(ufence->mm);
89 	} else {
90 		drm_dbg(&ufence->xe->drm, "mmget_not_zero() failed, ufence wasn't signaled\n");
91 	}
92 
93 	/*
94 	 * Wake up waiters only after updating the ufence state, allowing the UMD
95 	 * to safely reuse the same ufence without encountering -EBUSY errors.
96 	 */
97 	wake_up_all(&ufence->xe->ufence_wq);
98 	user_fence_put(ufence);
99 }
100 
101 static void kick_ufence(struct xe_user_fence *ufence, struct dma_fence *fence)
102 {
103 	INIT_WORK(&ufence->worker, user_fence_worker);
104 	queue_work(ufence->xe->ordered_wq, &ufence->worker);
105 	dma_fence_put(fence);
106 }
107 
108 static void user_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
109 {
110 	struct xe_user_fence *ufence = container_of(cb, struct xe_user_fence, cb);
111 
112 	kick_ufence(ufence, fence);
113 }
114 
115 int xe_sync_entry_parse(struct xe_device *xe, struct xe_file *xef,
116 			struct xe_sync_entry *sync,
117 			struct drm_xe_sync __user *sync_user,
118 			struct drm_syncobj *ufence_syncobj,
119 			u64 ufence_timeline_value,
120 			unsigned int flags)
121 {
122 	struct drm_xe_sync sync_in;
123 	int err;
124 	bool exec = flags & SYNC_PARSE_FLAG_EXEC;
125 	bool in_lr_mode = flags & SYNC_PARSE_FLAG_LR_MODE;
126 	bool disallow_user_fence = flags & SYNC_PARSE_FLAG_DISALLOW_USER_FENCE;
127 	bool signal;
128 
129 	if (copy_from_user(&sync_in, sync_user, sizeof(*sync_user)))
130 		return -EFAULT;
131 
132 	if (XE_IOCTL_DBG(xe, sync_in.flags & ~DRM_XE_SYNC_FLAG_SIGNAL) ||
133 	    XE_IOCTL_DBG(xe, sync_in.reserved[0] || sync_in.reserved[1]))
134 		return -EINVAL;
135 
136 	signal = sync_in.flags & DRM_XE_SYNC_FLAG_SIGNAL;
137 	switch (sync_in.type) {
138 	case DRM_XE_SYNC_TYPE_SYNCOBJ:
139 		if (XE_IOCTL_DBG(xe, in_lr_mode && signal))
140 			return -EOPNOTSUPP;
141 
142 		if (XE_IOCTL_DBG(xe, upper_32_bits(sync_in.addr)))
143 			return -EINVAL;
144 
145 		sync->syncobj = drm_syncobj_find(xef->drm, sync_in.handle);
146 		if (XE_IOCTL_DBG(xe, !sync->syncobj))
147 			return -ENOENT;
148 
149 		if (!signal) {
150 			sync->fence = drm_syncobj_fence_get(sync->syncobj);
151 			if (XE_IOCTL_DBG(xe, !sync->fence)) {
152 				err = -EINVAL;
153 				goto free_sync;
154 			}
155 		}
156 		break;
157 
158 	case DRM_XE_SYNC_TYPE_TIMELINE_SYNCOBJ:
159 		if (XE_IOCTL_DBG(xe, in_lr_mode && signal))
160 			return -EOPNOTSUPP;
161 
162 		if (XE_IOCTL_DBG(xe, upper_32_bits(sync_in.addr)))
163 			return -EINVAL;
164 
165 		if (XE_IOCTL_DBG(xe, sync_in.timeline_value == 0))
166 			return -EINVAL;
167 
168 		sync->syncobj = drm_syncobj_find(xef->drm, sync_in.handle);
169 		if (XE_IOCTL_DBG(xe, !sync->syncobj))
170 			return -ENOENT;
171 
172 		if (signal) {
173 			sync->chain_fence = dma_fence_chain_alloc();
174 			if (!sync->chain_fence) {
175 				err = -ENOMEM;
176 				goto free_sync;
177 			}
178 		} else {
179 			sync->fence = drm_syncobj_fence_get(sync->syncobj);
180 			if (XE_IOCTL_DBG(xe, !sync->fence)) {
181 				err = -EINVAL;
182 				goto free_sync;
183 			}
184 
185 			err = dma_fence_chain_find_seqno(&sync->fence,
186 							 sync_in.timeline_value);
187 			if (err)
188 				goto free_sync;
189 		}
190 		break;
191 
192 	case DRM_XE_SYNC_TYPE_USER_FENCE:
193 		if (XE_IOCTL_DBG(xe, disallow_user_fence))
194 			return -EOPNOTSUPP;
195 
196 		if (XE_IOCTL_DBG(xe, !signal))
197 			return -EOPNOTSUPP;
198 
199 		if (XE_IOCTL_DBG(xe, sync_in.addr & 0x7))
200 			return -EINVAL;
201 
202 		if (exec) {
203 			sync->addr = sync_in.addr;
204 		} else {
205 			sync->ufence_timeline_value = ufence_timeline_value;
206 			sync->ufence = user_fence_create(xe, sync_in.addr,
207 							 sync_in.timeline_value);
208 			if (XE_IOCTL_DBG(xe, IS_ERR(sync->ufence)))
209 				return PTR_ERR(sync->ufence);
210 			sync->ufence_chain_fence = dma_fence_chain_alloc();
211 			if (!sync->ufence_chain_fence) {
212 				err = -ENOMEM;
213 				goto free_sync;
214 			}
215 			sync->ufence_syncobj = ufence_syncobj;
216 		}
217 
218 		break;
219 
220 	default:
221 		return -EINVAL;
222 	}
223 
224 	sync->type = sync_in.type;
225 	sync->flags = sync_in.flags;
226 	sync->timeline_value = sync_in.timeline_value;
227 
228 	return 0;
229 
230 free_sync:
231 	xe_sync_entry_cleanup(sync);
232 	return err;
233 }
234 ALLOW_ERROR_INJECTION(xe_sync_entry_parse, ERRNO);
235 
236 int xe_sync_entry_add_deps(struct xe_sync_entry *sync, struct xe_sched_job *job)
237 {
238 	struct dma_fence_unwrap iter;
239 	struct dma_fence *fence;
240 	int err = 0;
241 
242 	if (!sync->fence)
243 		return 0;
244 
245 	/*
246 	 * Unwrap fence containers (e.g. dma_fence_chain from a timeline
247 	 * syncobj) so the scheduler sees the underlying fences. That lets it
248 	 * drop dependencies on jobs from the same entity and only wait for
249 	 * jobs on the same scheduler to be scheduled, rather than waiting for
250 	 * the container, which forces a full GPU completion wait and drains
251 	 * the queue.
252 	 */
253 	dma_fence_unwrap_for_each(fence, &iter, sync->fence) {
254 		/*
255 		 * There is no dma_fence_unwrap_stop() helper to drop the
256 		 * reference the iterator holds, so keep iterating without
257 		 * doing anything once an error has occurred.
258 		 */
259 		if (err || dma_fence_is_signaled(fence))
260 			continue;
261 
262 		err = drm_sched_job_add_dependency(&job->drm,
263 						   dma_fence_get(fence));
264 	}
265 
266 	return err;
267 }
268 
269 /**
270  * xe_sync_entry_wait() - Wait on in-sync
271  * @sync: Sync object
272  *
273  * If the sync is in an in-sync, wait on the sync to signal.
274  *
275  * Return: 0 on success, -ERESTARTSYS on failure (interruption)
276  */
277 int xe_sync_entry_wait(struct xe_sync_entry *sync)
278 {
279 	return xe_sync_needs_wait(sync) ?
280 		dma_fence_wait(sync->fence, true) : 0;
281 }
282 
283 /**
284  * xe_sync_needs_wait() - Sync needs a wait (input dma-fence not signaled)
285  * @sync: Sync object
286  *
287  * Return: True if sync needs a wait, False otherwise
288  */
289 bool xe_sync_needs_wait(struct xe_sync_entry *sync)
290 {
291 	return sync->fence &&
292 	       !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &sync->fence->flags);
293 }
294 
295 void xe_sync_entry_signal(struct xe_sync_entry *sync, struct dma_fence *fence)
296 {
297 	if (!(sync->flags & DRM_XE_SYNC_FLAG_SIGNAL))
298 		return;
299 
300 	if (sync->chain_fence) {
301 		drm_syncobj_add_point(sync->syncobj, sync->chain_fence,
302 				      fence, sync->timeline_value);
303 		/*
304 		 * The chain's ownership is transferred to the
305 		 * timeline.
306 		 */
307 		sync->chain_fence = NULL;
308 	} else if (sync->syncobj) {
309 		drm_syncobj_replace_fence(sync->syncobj, fence);
310 	} else if (sync->ufence) {
311 		int err;
312 
313 		drm_syncobj_add_point(sync->ufence_syncobj,
314 				      sync->ufence_chain_fence,
315 				      fence, sync->ufence_timeline_value);
316 		sync->ufence_chain_fence = NULL;
317 
318 		fence = drm_syncobj_fence_get(sync->ufence_syncobj);
319 		user_fence_get(sync->ufence);
320 		err = dma_fence_add_callback(fence, &sync->ufence->cb,
321 					     user_fence_cb);
322 		if (err == -ENOENT) {
323 			kick_ufence(sync->ufence, fence);
324 		} else if (err) {
325 			XE_WARN_ON("failed to add user fence");
326 			user_fence_put(sync->ufence);
327 			dma_fence_put(fence);
328 		}
329 	}
330 }
331 
332 void xe_sync_entry_cleanup(struct xe_sync_entry *sync)
333 {
334 	if (sync->syncobj)
335 		drm_syncobj_put(sync->syncobj);
336 	dma_fence_put(sync->fence);
337 	dma_fence_chain_free(sync->chain_fence);
338 	dma_fence_chain_free(sync->ufence_chain_fence);
339 	if (!IS_ERR_OR_NULL(sync->ufence))
340 		user_fence_put(sync->ufence);
341 }
342 
343 /**
344  * xe_sync_in_fence_get() - Get a fence from syncs, exec queue, and VM
345  * @sync: input syncs
346  * @num_sync: number of syncs
347  * @q: exec queue
348  * @vm: VM
349  *
350  * Get a fence from syncs, exec queue, and VM. If syncs contain in-fences create
351  * and return a composite fence of all in-fences + last fence. If no in-fences
352  * return last fence on  input exec queue. Caller must drop reference to
353  * returned fence.
354  *
355  * Return: fence on success, ERR_PTR(-ENOMEM) on failure
356  */
357 struct dma_fence *
358 xe_sync_in_fence_get(struct xe_sync_entry *sync, int num_sync,
359 		     struct xe_exec_queue *q, struct xe_vm *vm)
360 {
361 	struct dma_fence **fences = NULL;
362 	struct dma_fence_array *cf = NULL;
363 	struct dma_fence *fence;
364 	int i, num_fence = 0, current_fence = 0;
365 
366 	lockdep_assert_held(&vm->lock);
367 
368 	/* Reject in fences */
369 	for (i = 0; i < num_sync; ++i)
370 		if (sync[i].fence)
371 			return ERR_PTR(-EOPNOTSUPP);
372 
373 	if (q->flags & EXEC_QUEUE_FLAG_VM) {
374 		num_fence++;
375 		for_each_tlb_inval(q, i)
376 			num_fence++;
377 
378 		fences = kmalloc_objs(*fences, num_fence);
379 		if (!fences)
380 			return ERR_PTR(-ENOMEM);
381 
382 		fences[current_fence++] =
383 			xe_exec_queue_last_fence_get(q, vm);
384 		for_each_tlb_inval(q, i)
385 			fences[current_fence++] =
386 				xe_exec_queue_tlb_inval_last_fence_get(q, vm, i);
387 
388 		xe_assert(vm->xe, current_fence == num_fence);
389 		cf = dma_fence_array_create(num_fence, fences,
390 					    dma_fence_context_alloc(1),
391 					    1);
392 		if (!cf)
393 			goto err_out;
394 
395 		return &cf->base;
396 	}
397 
398 	fence = xe_exec_queue_last_fence_get(q, vm);
399 	return fence;
400 
401 err_out:
402 	while (current_fence)
403 		dma_fence_put(fences[--current_fence]);
404 	kfree(fences);
405 
406 	return ERR_PTR(-ENOMEM);
407 }
408 
409 /**
410  * __xe_sync_ufence_get() - Get user fence from user fence
411  * @ufence: input user fence
412  *
413  * Get a user fence reference from user fence
414  *
415  * Return: xe_user_fence pointer with reference
416  */
417 struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence)
418 {
419 	user_fence_get(ufence);
420 
421 	return ufence;
422 }
423 
424 /**
425  * xe_sync_ufence_get() - Get user fence from sync
426  * @sync: input sync
427  *
428  * Get a user fence reference from sync.
429  *
430  * Return: xe_user_fence pointer with reference
431  */
432 struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync)
433 {
434 	user_fence_get(sync->ufence);
435 
436 	return sync->ufence;
437 }
438 
439 /**
440  * xe_sync_ufence_put() - Put user fence reference
441  * @ufence: user fence reference
442  *
443  */
444 void xe_sync_ufence_put(struct xe_user_fence *ufence)
445 {
446 	user_fence_put(ufence);
447 }
448 
449 /**
450  * xe_sync_ufence_get_status() - Get user fence status
451  * @ufence: user fence
452  *
453  * Return: 1 if signalled, 0 not signalled, <0 on error
454  */
455 int xe_sync_ufence_get_status(struct xe_user_fence *ufence)
456 {
457 	return READ_ONCE(ufence->signalled);
458 }
459