Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / i915_sw_fence.c
blob038d4c6884c5b708dc102d1b09fa663a17cee701
1 /*
2 * SPDX-License-Identifier: MIT
4 * (C) Copyright 2016 Intel Corporation
5 */
7 #include <linux/slab.h>
8 #include <linux/dma-fence.h>
9 #include <linux/irq_work.h>
10 #include <linux/dma-resv.h>
12 #include "i915_sw_fence.h"
13 #include "i915_selftest.h"
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
21 #define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
23 static DEFINE_SPINLOCK(i915_sw_fence_lock);
25 enum {
26 DEBUG_FENCE_IDLE = 0,
27 DEBUG_FENCE_NOTIFY,
30 static void *i915_sw_fence_debug_hint(void *addr)
32 return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
35 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
37 static const struct debug_obj_descr i915_sw_fence_debug_descr = {
38 .name = "i915_sw_fence",
39 .debug_hint = i915_sw_fence_debug_hint,
42 static inline void debug_fence_init(struct i915_sw_fence *fence)
44 debug_object_init(fence, &i915_sw_fence_debug_descr);
47 static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
49 debug_object_init_on_stack(fence, &i915_sw_fence_debug_descr);
52 static inline void debug_fence_activate(struct i915_sw_fence *fence)
54 debug_object_activate(fence, &i915_sw_fence_debug_descr);
57 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
58 int old, int new)
60 debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new);
63 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
65 debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
68 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
70 debug_object_destroy(fence, &i915_sw_fence_debug_descr);
73 static inline void debug_fence_free(struct i915_sw_fence *fence)
75 debug_object_free(fence, &i915_sw_fence_debug_descr);
76 smp_wmb(); /* flush the change in state before reallocation */
79 static inline void debug_fence_assert(struct i915_sw_fence *fence)
81 debug_object_assert_init(fence, &i915_sw_fence_debug_descr);
84 #else
86 static inline void debug_fence_init(struct i915_sw_fence *fence)
90 static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
94 static inline void debug_fence_activate(struct i915_sw_fence *fence)
98 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
99 int old, int new)
103 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
107 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
111 static inline void debug_fence_free(struct i915_sw_fence *fence)
115 static inline void debug_fence_assert(struct i915_sw_fence *fence)
119 #endif
121 static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
122 enum i915_sw_fence_notify state)
124 i915_sw_fence_notify_t fn;
126 fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
127 return fn(fence, state);
130 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
131 void i915_sw_fence_fini(struct i915_sw_fence *fence)
133 debug_fence_free(fence);
135 #endif
137 static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
138 struct list_head *continuation)
140 wait_queue_head_t *x = &fence->wait;
141 wait_queue_entry_t *pos, *next;
142 unsigned long flags;
144 debug_fence_deactivate(fence);
145 atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
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).
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);
162 } else {
163 LIST_HEAD(extra);
165 do {
166 list_for_each_entry_safe(pos, next, &x->head, entry) {
167 int wake_flags;
169 wake_flags = fence->error;
170 if (pos->func == autoremove_wake_function)
171 wake_flags = 0;
173 pos->func(pos, TASK_NORMAL, wake_flags, &extra);
176 if (list_empty(&extra))
177 break;
179 list_splice_tail_init(&extra, &x->head);
180 } while (1);
182 spin_unlock_irqrestore(&x->lock, flags);
184 debug_fence_assert(fence);
187 static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
188 struct list_head *continuation)
190 debug_fence_assert(fence);
192 if (!atomic_dec_and_test(&fence->pending))
193 return;
195 debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
197 if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
198 return;
200 debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
202 __i915_sw_fence_wake_up_all(fence, continuation);
204 debug_fence_destroy(fence);
205 __i915_sw_fence_notify(fence, FENCE_FREE);
208 void i915_sw_fence_complete(struct i915_sw_fence *fence)
210 debug_fence_assert(fence);
212 if (WARN_ON(i915_sw_fence_done(fence)))
213 return;
215 __i915_sw_fence_complete(fence, NULL);
218 bool i915_sw_fence_await(struct i915_sw_fence *fence)
220 int pending;
223 * It is only safe to add a new await to the fence while it has
224 * not yet been signaled (i.e. there are still existing signalers).
226 pending = atomic_read(&fence->pending);
227 do {
228 if (pending < 1)
229 return false;
230 } while (!atomic_try_cmpxchg(&fence->pending, &pending, pending + 1));
232 return true;
235 void __i915_sw_fence_init(struct i915_sw_fence *fence,
236 i915_sw_fence_notify_t fn,
237 const char *name,
238 struct lock_class_key *key)
240 BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
242 __init_waitqueue_head(&fence->wait, name, key);
243 fence->flags = (unsigned long)fn;
245 i915_sw_fence_reinit(fence);
248 void i915_sw_fence_reinit(struct i915_sw_fence *fence)
250 debug_fence_init(fence);
252 atomic_set(&fence->pending, 1);
253 fence->error = 0;
255 I915_SW_FENCE_BUG_ON(!fence->flags);
256 I915_SW_FENCE_BUG_ON(!list_empty(&fence->wait.head));
259 void i915_sw_fence_commit(struct i915_sw_fence *fence)
261 debug_fence_activate(fence);
262 i915_sw_fence_complete(fence);
265 static int i915_sw_fence_wake(wait_queue_entry_t *wq, unsigned mode, int flags, void *key)
267 i915_sw_fence_set_error_once(wq->private, flags);
269 list_del(&wq->entry);
270 __i915_sw_fence_complete(wq->private, key);
272 if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
273 kfree(wq);
274 return 0;
277 static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
278 const struct i915_sw_fence * const signaler)
280 wait_queue_entry_t *wq;
282 if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
283 return false;
285 if (fence == signaler)
286 return true;
288 list_for_each_entry(wq, &fence->wait.head, entry) {
289 if (wq->func != i915_sw_fence_wake)
290 continue;
292 if (__i915_sw_fence_check_if_after(wq->private, signaler))
293 return true;
296 return false;
299 static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
301 wait_queue_entry_t *wq;
303 if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
304 return;
306 list_for_each_entry(wq, &fence->wait.head, entry) {
307 if (wq->func != i915_sw_fence_wake)
308 continue;
310 __i915_sw_fence_clear_checked_bit(wq->private);
314 static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
315 const struct i915_sw_fence * const signaler)
317 unsigned long flags;
318 bool err;
320 if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
321 return false;
323 spin_lock_irqsave(&i915_sw_fence_lock, flags);
324 err = __i915_sw_fence_check_if_after(fence, signaler);
325 __i915_sw_fence_clear_checked_bit(fence);
326 spin_unlock_irqrestore(&i915_sw_fence_lock, flags);
328 return err;
331 static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
332 struct i915_sw_fence *signaler,
333 wait_queue_entry_t *wq, gfp_t gfp)
335 unsigned long flags;
336 int pending;
338 debug_fence_assert(fence);
339 might_sleep_if(gfpflags_allow_blocking(gfp));
341 if (i915_sw_fence_done(signaler)) {
342 i915_sw_fence_set_error_once(fence, signaler->error);
343 return 0;
346 debug_fence_assert(signaler);
348 /* The dependency graph must be acyclic. */
349 if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
350 return -EINVAL;
352 pending = 0;
353 if (!wq) {
354 wq = kmalloc(sizeof(*wq), gfp);
355 if (!wq) {
356 if (!gfpflags_allow_blocking(gfp))
357 return -ENOMEM;
359 i915_sw_fence_wait(signaler);
360 i915_sw_fence_set_error_once(fence, signaler->error);
361 return 0;
364 pending |= I915_SW_FENCE_FLAG_ALLOC;
367 INIT_LIST_HEAD(&wq->entry);
368 wq->flags = pending;
369 wq->func = i915_sw_fence_wake;
370 wq->private = fence;
372 i915_sw_fence_await(fence);
374 spin_lock_irqsave(&signaler->wait.lock, flags);
375 if (likely(!i915_sw_fence_done(signaler))) {
376 __add_wait_queue_entry_tail(&signaler->wait, wq);
377 pending = 1;
378 } else {
379 i915_sw_fence_wake(wq, 0, signaler->error, NULL);
380 pending = 0;
382 spin_unlock_irqrestore(&signaler->wait.lock, flags);
384 return pending;
387 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
388 struct i915_sw_fence *signaler,
389 wait_queue_entry_t *wq)
391 return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
394 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
395 struct i915_sw_fence *signaler,
396 gfp_t gfp)
398 return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
401 struct i915_sw_dma_fence_cb_timer {
402 struct i915_sw_dma_fence_cb base;
403 struct dma_fence *dma;
404 struct timer_list timer;
405 struct irq_work work;
406 struct rcu_head rcu;
409 static void dma_i915_sw_fence_wake(struct dma_fence *dma,
410 struct dma_fence_cb *data)
412 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
414 i915_sw_fence_set_error_once(cb->fence, dma->error);
415 i915_sw_fence_complete(cb->fence);
416 kfree(cb);
419 static void timer_i915_sw_fence_wake(struct timer_list *t)
421 struct i915_sw_dma_fence_cb_timer *cb = from_timer(cb, t, timer);
422 struct i915_sw_fence *fence;
424 fence = xchg(&cb->base.fence, NULL);
425 if (!fence)
426 return;
428 pr_notice("Asynchronous wait on fence %s:%s:%llx timed out (hint:%ps)\n",
429 cb->dma->ops->get_driver_name(cb->dma),
430 cb->dma->ops->get_timeline_name(cb->dma),
431 cb->dma->seqno,
432 i915_sw_fence_debug_hint(fence));
434 i915_sw_fence_set_error_once(fence, -ETIMEDOUT);
435 i915_sw_fence_complete(fence);
438 static void dma_i915_sw_fence_wake_timer(struct dma_fence *dma,
439 struct dma_fence_cb *data)
441 struct i915_sw_dma_fence_cb_timer *cb =
442 container_of(data, typeof(*cb), base.base);
443 struct i915_sw_fence *fence;
445 fence = xchg(&cb->base.fence, NULL);
446 if (fence) {
447 i915_sw_fence_set_error_once(fence, dma->error);
448 i915_sw_fence_complete(fence);
451 irq_work_queue(&cb->work);
454 static void irq_i915_sw_fence_work(struct irq_work *wrk)
456 struct i915_sw_dma_fence_cb_timer *cb =
457 container_of(wrk, typeof(*cb), work);
459 del_timer_sync(&cb->timer);
460 dma_fence_put(cb->dma);
462 kfree_rcu(cb, rcu);
465 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
466 struct dma_fence *dma,
467 unsigned long timeout,
468 gfp_t gfp)
470 struct i915_sw_dma_fence_cb *cb;
471 dma_fence_func_t func;
472 int ret;
474 debug_fence_assert(fence);
475 might_sleep_if(gfpflags_allow_blocking(gfp));
477 if (dma_fence_is_signaled(dma)) {
478 i915_sw_fence_set_error_once(fence, dma->error);
479 return 0;
482 cb = kmalloc(timeout ?
483 sizeof(struct i915_sw_dma_fence_cb_timer) :
484 sizeof(struct i915_sw_dma_fence_cb),
485 gfp);
486 if (!cb) {
487 if (!gfpflags_allow_blocking(gfp))
488 return -ENOMEM;
490 ret = dma_fence_wait(dma, false);
491 if (ret)
492 return ret;
494 i915_sw_fence_set_error_once(fence, dma->error);
495 return 0;
498 cb->fence = fence;
499 i915_sw_fence_await(fence);
501 func = dma_i915_sw_fence_wake;
502 if (timeout) {
503 struct i915_sw_dma_fence_cb_timer *timer =
504 container_of(cb, typeof(*timer), base);
506 timer->dma = dma_fence_get(dma);
507 init_irq_work(&timer->work, irq_i915_sw_fence_work);
509 timer_setup(&timer->timer,
510 timer_i915_sw_fence_wake, TIMER_IRQSAFE);
511 mod_timer(&timer->timer, round_jiffies_up(jiffies + timeout));
513 func = dma_i915_sw_fence_wake_timer;
516 ret = dma_fence_add_callback(dma, &cb->base, func);
517 if (ret == 0) {
518 ret = 1;
519 } else {
520 func(dma, &cb->base);
521 if (ret == -ENOENT) /* fence already signaled */
522 ret = 0;
525 return ret;
528 static void __dma_i915_sw_fence_wake(struct dma_fence *dma,
529 struct dma_fence_cb *data)
531 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
533 i915_sw_fence_set_error_once(cb->fence, dma->error);
534 i915_sw_fence_complete(cb->fence);
537 int __i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
538 struct dma_fence *dma,
539 struct i915_sw_dma_fence_cb *cb)
541 int ret;
543 debug_fence_assert(fence);
545 if (dma_fence_is_signaled(dma)) {
546 i915_sw_fence_set_error_once(fence, dma->error);
547 return 0;
550 cb->fence = fence;
551 i915_sw_fence_await(fence);
553 ret = 1;
554 if (dma_fence_add_callback(dma, &cb->base, __dma_i915_sw_fence_wake)) {
555 /* fence already signaled */
556 __dma_i915_sw_fence_wake(dma, &cb->base);
557 ret = 0;
560 return ret;
563 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
564 struct dma_resv *resv,
565 const struct dma_fence_ops *exclude,
566 bool write,
567 unsigned long timeout,
568 gfp_t gfp)
570 struct dma_fence *excl;
571 int ret = 0, pending;
573 debug_fence_assert(fence);
574 might_sleep_if(gfpflags_allow_blocking(gfp));
576 if (write) {
577 struct dma_fence **shared;
578 unsigned int count, i;
580 ret = dma_resv_get_fences_rcu(resv, &excl, &count, &shared);
581 if (ret)
582 return ret;
584 for (i = 0; i < count; i++) {
585 if (shared[i]->ops == exclude)
586 continue;
588 pending = i915_sw_fence_await_dma_fence(fence,
589 shared[i],
590 timeout,
591 gfp);
592 if (pending < 0) {
593 ret = pending;
594 break;
597 ret |= pending;
600 for (i = 0; i < count; i++)
601 dma_fence_put(shared[i]);
602 kfree(shared);
603 } else {
604 excl = dma_resv_get_excl_rcu(resv);
607 if (ret >= 0 && excl && excl->ops != exclude) {
608 pending = i915_sw_fence_await_dma_fence(fence,
609 excl,
610 timeout,
611 gfp);
612 if (pending < 0)
613 ret = pending;
614 else
615 ret |= pending;
618 dma_fence_put(excl);
620 return ret;
623 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
624 #include "selftests/lib_sw_fence.c"
625 #include "selftests/i915_sw_fence.c"
626 #endif