1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * (C) Copyright 2016 Intel Corporation
5  */
6 
7 #include <linux/slab.h>
8 #include <linux/dma-fence.h>
9 #include <linux/irq_work.h>
10 #include <linux/dma-resv.h>
11 
12 #include "i915_sw_fence.h"
13 #include "i915_selftest.h"
14 
15 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
16 #define I915_SW_FENCE_BUG_ON(expr) BUG_ON(expr)
17 #else
18 #define I915_SW_FENCE_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
19 #endif
20 
21 #define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
22 
23 static DEFINE_SPINLOCK(i915_sw_fence_lock);
24 
25 enum {
26 	DEBUG_FENCE_IDLE = 0,
27 	DEBUG_FENCE_NOTIFY,
28 };
29 
30 static void *i915_sw_fence_debug_hint(void *addr)
31 {
32 	return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
33 }
34 
35 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
36 
37 static struct debug_obj_descr i915_sw_fence_debug_descr = {
38 	.name = "i915_sw_fence",
39 	.debug_hint = i915_sw_fence_debug_hint,
40 };
41 
42 static inline void debug_fence_init(struct i915_sw_fence *fence)
43 {
44 	debug_object_init(fence, &i915_sw_fence_debug_descr);
45 }
46 
47 static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
48 {
49 	debug_object_init_on_stack(fence, &i915_sw_fence_debug_descr);
50 }
51 
52 static inline void debug_fence_activate(struct i915_sw_fence *fence)
53 {
54 	debug_object_activate(fence, &i915_sw_fence_debug_descr);
55 }
56 
57 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
58 					 int old, int new)
59 {
60 	debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new);
61 }
62 
63 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
64 {
65 	debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
66 }
67 
68 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
69 {
70 	debug_object_destroy(fence, &i915_sw_fence_debug_descr);
71 }
72 
73 static inline void debug_fence_free(struct i915_sw_fence *fence)
74 {
75 	debug_object_free(fence, &i915_sw_fence_debug_descr);
76 	smp_wmb(); /* flush the change in state before reallocation */
77 }
78 
79 static inline void debug_fence_assert(struct i915_sw_fence *fence)
80 {
81 	debug_object_assert_init(fence, &i915_sw_fence_debug_descr);
82 }
83 
84 #else
85 
86 static inline void debug_fence_init(struct i915_sw_fence *fence)
87 {
88 }
89 
90 static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
91 {
92 }
93 
94 static inline void debug_fence_activate(struct i915_sw_fence *fence)
95 {
96 }
97 
98 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
99 					 int old, int new)
100 {
101 }
102 
103 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
104 {
105 }
106 
107 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
108 {
109 }
110 
111 static inline void debug_fence_free(struct i915_sw_fence *fence)
112 {
113 }
114 
115 static inline void debug_fence_assert(struct i915_sw_fence *fence)
116 {
117 }
118 
119 #endif
120 
121 static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
122 				  enum i915_sw_fence_notify state)
123 {
124 	i915_sw_fence_notify_t fn;
125 
126 	fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
127 	return fn(fence, state);
128 }
129 
130 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
131 void i915_sw_fence_fini(struct i915_sw_fence *fence)
132 {
133 	debug_fence_free(fence);
134 }
135 #endif
136 
137 static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
138 					struct list_head *continuation)
139 {
140 	wait_queue_head_t *x = &fence->wait;
141 	wait_queue_entry_t *pos, *next;
142 	unsigned long flags;
143 
144 	debug_fence_deactivate(fence);
145 	atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
146 
147 	/*
148 	 * To prevent unbounded recursion as we traverse the graph of
149 	 * i915_sw_fences, we move the entry list from this, the next ready
150 	 * fence, to the tail of the original fence's entry list
151 	 * (and so added to the list to be woken).
152 	 */
153 
154 	spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
155 	if (continuation) {
156 		list_for_each_entry_safe(pos, next, &x->head, entry) {
157 			if (pos->func == autoremove_wake_function)
158 				pos->func(pos, TASK_NORMAL, 0, continuation);
159 			else
160 				list_move_tail(&pos->entry, continuation);
161 		}
162 	} else {
163 		LIST_HEAD(extra);
164 
165 		do {
166 			list_for_each_entry_safe(pos, next, &x->head, entry) {
167 				pos->func(pos,
168 					  TASK_NORMAL, fence->error,
169 					  &extra);
170 			}
171 
172 			if (list_empty(&extra))
173 				break;
174 
175 			list_splice_tail_init(&extra, &x->head);
176 		} while (1);
177 	}
178 	spin_unlock_irqrestore(&x->lock, flags);
179 
180 	debug_fence_assert(fence);
181 }
182 
183 static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
184 				     struct list_head *continuation)
185 {
186 	debug_fence_assert(fence);
187 
188 	if (!atomic_dec_and_test(&fence->pending))
189 		return;
190 
191 	debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
192 
193 	if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
194 		return;
195 
196 	debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
197 
198 	__i915_sw_fence_wake_up_all(fence, continuation);
199 
200 	debug_fence_destroy(fence);
201 	__i915_sw_fence_notify(fence, FENCE_FREE);
202 }
203 
204 void i915_sw_fence_complete(struct i915_sw_fence *fence)
205 {
206 	debug_fence_assert(fence);
207 
208 	if (WARN_ON(i915_sw_fence_done(fence)))
209 		return;
210 
211 	__i915_sw_fence_complete(fence, NULL);
212 }
213 
214 void i915_sw_fence_await(struct i915_sw_fence *fence)
215 {
216 	debug_fence_assert(fence);
217 	WARN_ON(atomic_inc_return(&fence->pending) <= 1);
218 }
219 
220 void __i915_sw_fence_init(struct i915_sw_fence *fence,
221 			  i915_sw_fence_notify_t fn,
222 			  const char *name,
223 			  struct lock_class_key *key)
224 {
225 	BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
226 
227 	__init_waitqueue_head(&fence->wait, name, key);
228 	fence->flags = (unsigned long)fn;
229 
230 	i915_sw_fence_reinit(fence);
231 }
232 
233 void i915_sw_fence_reinit(struct i915_sw_fence *fence)
234 {
235 	debug_fence_init(fence);
236 
237 	atomic_set(&fence->pending, 1);
238 	fence->error = 0;
239 
240 	I915_SW_FENCE_BUG_ON(!fence->flags);
241 	I915_SW_FENCE_BUG_ON(!list_empty(&fence->wait.head));
242 }
243 
244 void i915_sw_fence_commit(struct i915_sw_fence *fence)
245 {
246 	debug_fence_activate(fence);
247 	i915_sw_fence_complete(fence);
248 }
249 
250 static int i915_sw_fence_wake(wait_queue_entry_t *wq, unsigned mode, int flags, void *key)
251 {
252 	i915_sw_fence_set_error_once(wq->private, flags);
253 
254 	list_del(&wq->entry);
255 	__i915_sw_fence_complete(wq->private, key);
256 
257 	if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
258 		kfree(wq);
259 	return 0;
260 }
261 
262 static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
263 				    const struct i915_sw_fence * const signaler)
264 {
265 	wait_queue_entry_t *wq;
266 
267 	if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
268 		return false;
269 
270 	if (fence == signaler)
271 		return true;
272 
273 	list_for_each_entry(wq, &fence->wait.head, entry) {
274 		if (wq->func != i915_sw_fence_wake)
275 			continue;
276 
277 		if (__i915_sw_fence_check_if_after(wq->private, signaler))
278 			return true;
279 	}
280 
281 	return false;
282 }
283 
284 static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
285 {
286 	wait_queue_entry_t *wq;
287 
288 	if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
289 		return;
290 
291 	list_for_each_entry(wq, &fence->wait.head, entry) {
292 		if (wq->func != i915_sw_fence_wake)
293 			continue;
294 
295 		__i915_sw_fence_clear_checked_bit(wq->private);
296 	}
297 }
298 
299 static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
300 				  const struct i915_sw_fence * const signaler)
301 {
302 	unsigned long flags;
303 	bool err;
304 
305 	if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
306 		return false;
307 
308 	spin_lock_irqsave(&i915_sw_fence_lock, flags);
309 	err = __i915_sw_fence_check_if_after(fence, signaler);
310 	__i915_sw_fence_clear_checked_bit(fence);
311 	spin_unlock_irqrestore(&i915_sw_fence_lock, flags);
312 
313 	return err;
314 }
315 
316 static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
317 					  struct i915_sw_fence *signaler,
318 					  wait_queue_entry_t *wq, gfp_t gfp)
319 {
320 	unsigned long flags;
321 	int pending;
322 
323 	debug_fence_assert(fence);
324 	might_sleep_if(gfpflags_allow_blocking(gfp));
325 
326 	if (i915_sw_fence_done(signaler)) {
327 		i915_sw_fence_set_error_once(fence, signaler->error);
328 		return 0;
329 	}
330 
331 	debug_fence_assert(signaler);
332 
333 	/* The dependency graph must be acyclic. */
334 	if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
335 		return -EINVAL;
336 
337 	pending = 0;
338 	if (!wq) {
339 		wq = kmalloc(sizeof(*wq), gfp);
340 		if (!wq) {
341 			if (!gfpflags_allow_blocking(gfp))
342 				return -ENOMEM;
343 
344 			i915_sw_fence_wait(signaler);
345 			i915_sw_fence_set_error_once(fence, signaler->error);
346 			return 0;
347 		}
348 
349 		pending |= I915_SW_FENCE_FLAG_ALLOC;
350 	}
351 
352 	INIT_LIST_HEAD(&wq->entry);
353 	wq->flags = pending;
354 	wq->func = i915_sw_fence_wake;
355 	wq->private = fence;
356 
357 	i915_sw_fence_await(fence);
358 
359 	spin_lock_irqsave(&signaler->wait.lock, flags);
360 	if (likely(!i915_sw_fence_done(signaler))) {
361 		__add_wait_queue_entry_tail(&signaler->wait, wq);
362 		pending = 1;
363 	} else {
364 		i915_sw_fence_wake(wq, 0, signaler->error, NULL);
365 		pending = 0;
366 	}
367 	spin_unlock_irqrestore(&signaler->wait.lock, flags);
368 
369 	return pending;
370 }
371 
372 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
373 				 struct i915_sw_fence *signaler,
374 				 wait_queue_entry_t *wq)
375 {
376 	return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
377 }
378 
379 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
380 				     struct i915_sw_fence *signaler,
381 				     gfp_t gfp)
382 {
383 	return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
384 }
385 
386 struct i915_sw_dma_fence_cb_timer {
387 	struct i915_sw_dma_fence_cb base;
388 	struct dma_fence *dma;
389 	struct timer_list timer;
390 	struct irq_work work;
391 	struct rcu_head rcu;
392 };
393 
394 static void dma_i915_sw_fence_wake(struct dma_fence *dma,
395 				   struct dma_fence_cb *data)
396 {
397 	struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
398 
399 	i915_sw_fence_set_error_once(cb->fence, dma->error);
400 	i915_sw_fence_complete(cb->fence);
401 	kfree(cb);
402 }
403 
404 static void timer_i915_sw_fence_wake(struct timer_list *t)
405 {
406 	struct i915_sw_dma_fence_cb_timer *cb = from_timer(cb, t, timer);
407 	struct i915_sw_fence *fence;
408 
409 	fence = xchg(&cb->base.fence, NULL);
410 	if (!fence)
411 		return;
412 
413 	pr_notice("Asynchronous wait on fence %s:%s:%llx timed out (hint:%pS)\n",
414 		  cb->dma->ops->get_driver_name(cb->dma),
415 		  cb->dma->ops->get_timeline_name(cb->dma),
416 		  cb->dma->seqno,
417 		  i915_sw_fence_debug_hint(fence));
418 
419 	i915_sw_fence_set_error_once(fence, -ETIMEDOUT);
420 	i915_sw_fence_complete(fence);
421 }
422 
423 static void dma_i915_sw_fence_wake_timer(struct dma_fence *dma,
424 					 struct dma_fence_cb *data)
425 {
426 	struct i915_sw_dma_fence_cb_timer *cb =
427 		container_of(data, typeof(*cb), base.base);
428 	struct i915_sw_fence *fence;
429 
430 	fence = xchg(&cb->base.fence, NULL);
431 	if (fence) {
432 		i915_sw_fence_set_error_once(fence, dma->error);
433 		i915_sw_fence_complete(fence);
434 	}
435 
436 	irq_work_queue(&cb->work);
437 }
438 
439 static void irq_i915_sw_fence_work(struct irq_work *wrk)
440 {
441 	struct i915_sw_dma_fence_cb_timer *cb =
442 		container_of(wrk, typeof(*cb), work);
443 
444 	del_timer_sync(&cb->timer);
445 	dma_fence_put(cb->dma);
446 
447 	kfree_rcu(cb, rcu);
448 }
449 
450 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
451 				  struct dma_fence *dma,
452 				  unsigned long timeout,
453 				  gfp_t gfp)
454 {
455 	struct i915_sw_dma_fence_cb *cb;
456 	dma_fence_func_t func;
457 	int ret;
458 
459 	debug_fence_assert(fence);
460 	might_sleep_if(gfpflags_allow_blocking(gfp));
461 
462 	if (dma_fence_is_signaled(dma)) {
463 		i915_sw_fence_set_error_once(fence, dma->error);
464 		return 0;
465 	}
466 
467 	cb = kmalloc(timeout ?
468 		     sizeof(struct i915_sw_dma_fence_cb_timer) :
469 		     sizeof(struct i915_sw_dma_fence_cb),
470 		     gfp);
471 	if (!cb) {
472 		if (!gfpflags_allow_blocking(gfp))
473 			return -ENOMEM;
474 
475 		ret = dma_fence_wait(dma, false);
476 		if (ret)
477 			return ret;
478 
479 		i915_sw_fence_set_error_once(fence, dma->error);
480 		return 0;
481 	}
482 
483 	cb->fence = fence;
484 	i915_sw_fence_await(fence);
485 
486 	func = dma_i915_sw_fence_wake;
487 	if (timeout) {
488 		struct i915_sw_dma_fence_cb_timer *timer =
489 			container_of(cb, typeof(*timer), base);
490 
491 		timer->dma = dma_fence_get(dma);
492 		init_irq_work(&timer->work, irq_i915_sw_fence_work);
493 
494 		timer_setup(&timer->timer,
495 			    timer_i915_sw_fence_wake, TIMER_IRQSAFE);
496 		mod_timer(&timer->timer, round_jiffies_up(jiffies + timeout));
497 
498 		func = dma_i915_sw_fence_wake_timer;
499 	}
500 
501 	ret = dma_fence_add_callback(dma, &cb->base, func);
502 	if (ret == 0) {
503 		ret = 1;
504 	} else {
505 		func(dma, &cb->base);
506 		if (ret == -ENOENT) /* fence already signaled */
507 			ret = 0;
508 	}
509 
510 	return ret;
511 }
512 
513 static void __dma_i915_sw_fence_wake(struct dma_fence *dma,
514 				     struct dma_fence_cb *data)
515 {
516 	struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
517 
518 	i915_sw_fence_set_error_once(cb->fence, dma->error);
519 	i915_sw_fence_complete(cb->fence);
520 }
521 
522 int __i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
523 				    struct dma_fence *dma,
524 				    struct i915_sw_dma_fence_cb *cb)
525 {
526 	int ret;
527 
528 	debug_fence_assert(fence);
529 
530 	if (dma_fence_is_signaled(dma)) {
531 		i915_sw_fence_set_error_once(fence, dma->error);
532 		return 0;
533 	}
534 
535 	cb->fence = fence;
536 	i915_sw_fence_await(fence);
537 
538 	ret = dma_fence_add_callback(dma, &cb->base, __dma_i915_sw_fence_wake);
539 	if (ret == 0) {
540 		ret = 1;
541 	} else {
542 		__dma_i915_sw_fence_wake(dma, &cb->base);
543 		if (ret == -ENOENT) /* fence already signaled */
544 			ret = 0;
545 	}
546 
547 	return ret;
548 }
549 
550 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
551 				    struct dma_resv *resv,
552 				    const struct dma_fence_ops *exclude,
553 				    bool write,
554 				    unsigned long timeout,
555 				    gfp_t gfp)
556 {
557 	struct dma_fence *excl;
558 	int ret = 0, pending;
559 
560 	debug_fence_assert(fence);
561 	might_sleep_if(gfpflags_allow_blocking(gfp));
562 
563 	if (write) {
564 		struct dma_fence **shared;
565 		unsigned int count, i;
566 
567 		ret = dma_resv_get_fences_rcu(resv, &excl, &count, &shared);
568 		if (ret)
569 			return ret;
570 
571 		for (i = 0; i < count; i++) {
572 			if (shared[i]->ops == exclude)
573 				continue;
574 
575 			pending = i915_sw_fence_await_dma_fence(fence,
576 								shared[i],
577 								timeout,
578 								gfp);
579 			if (pending < 0) {
580 				ret = pending;
581 				break;
582 			}
583 
584 			ret |= pending;
585 		}
586 
587 		for (i = 0; i < count; i++)
588 			dma_fence_put(shared[i]);
589 		kfree(shared);
590 	} else {
591 		excl = dma_resv_get_excl_rcu(resv);
592 	}
593 
594 	if (ret >= 0 && excl && excl->ops != exclude) {
595 		pending = i915_sw_fence_await_dma_fence(fence,
596 							excl,
597 							timeout,
598 							gfp);
599 		if (pending < 0)
600 			ret = pending;
601 		else
602 			ret |= pending;
603 	}
604 
605 	dma_fence_put(excl);
606 
607 	return ret;
608 }
609 
610 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
611 #include "selftests/lib_sw_fence.c"
612 #include "selftests/i915_sw_fence.c"
613 #endif
614