treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / i915_sw_fence.c
blob51ba97daf2a07c2f997ced72a16361db3fb070f1
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 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 pos->func(pos,
168 TASK_NORMAL, fence->error,
169 &extra);
172 if (list_empty(&extra))
173 break;
175 list_splice_tail_init(&extra, &x->head);
176 } while (1);
178 spin_unlock_irqrestore(&x->lock, flags);
180 debug_fence_assert(fence);
183 static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
184 struct list_head *continuation)
186 debug_fence_assert(fence);
188 if (!atomic_dec_and_test(&fence->pending))
189 return;
191 debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
193 if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
194 return;
196 debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
198 __i915_sw_fence_wake_up_all(fence, continuation);
200 debug_fence_destroy(fence);
201 __i915_sw_fence_notify(fence, FENCE_FREE);
204 void i915_sw_fence_complete(struct i915_sw_fence *fence)
206 debug_fence_assert(fence);
208 if (WARN_ON(i915_sw_fence_done(fence)))
209 return;
211 __i915_sw_fence_complete(fence, NULL);
214 void i915_sw_fence_await(struct i915_sw_fence *fence)
216 debug_fence_assert(fence);
217 WARN_ON(atomic_inc_return(&fence->pending) <= 1);
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)
225 BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
227 __init_waitqueue_head(&fence->wait, name, key);
228 fence->flags = (unsigned long)fn;
230 i915_sw_fence_reinit(fence);
233 void i915_sw_fence_reinit(struct i915_sw_fence *fence)
235 debug_fence_init(fence);
237 atomic_set(&fence->pending, 1);
238 fence->error = 0;
240 I915_SW_FENCE_BUG_ON(!fence->flags);
241 I915_SW_FENCE_BUG_ON(!list_empty(&fence->wait.head));
244 void i915_sw_fence_commit(struct i915_sw_fence *fence)
246 debug_fence_activate(fence);
247 i915_sw_fence_complete(fence);
250 static int i915_sw_fence_wake(wait_queue_entry_t *wq, unsigned mode, int flags, void *key)
252 i915_sw_fence_set_error_once(wq->private, flags);
254 list_del(&wq->entry);
255 __i915_sw_fence_complete(wq->private, key);
257 if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
258 kfree(wq);
259 return 0;
262 static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
263 const struct i915_sw_fence * const signaler)
265 wait_queue_entry_t *wq;
267 if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
268 return false;
270 if (fence == signaler)
271 return true;
273 list_for_each_entry(wq, &fence->wait.head, entry) {
274 if (wq->func != i915_sw_fence_wake)
275 continue;
277 if (__i915_sw_fence_check_if_after(wq->private, signaler))
278 return true;
281 return false;
284 static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
286 wait_queue_entry_t *wq;
288 if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
289 return;
291 list_for_each_entry(wq, &fence->wait.head, entry) {
292 if (wq->func != i915_sw_fence_wake)
293 continue;
295 __i915_sw_fence_clear_checked_bit(wq->private);
299 static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
300 const struct i915_sw_fence * const signaler)
302 unsigned long flags;
303 bool err;
305 if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
306 return false;
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);
313 return err;
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)
320 unsigned long flags;
321 int pending;
323 debug_fence_assert(fence);
324 might_sleep_if(gfpflags_allow_blocking(gfp));
326 if (i915_sw_fence_done(signaler)) {
327 i915_sw_fence_set_error_once(fence, signaler->error);
328 return 0;
331 debug_fence_assert(signaler);
333 /* The dependency graph must be acyclic. */
334 if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
335 return -EINVAL;
337 pending = 0;
338 if (!wq) {
339 wq = kmalloc(sizeof(*wq), gfp);
340 if (!wq) {
341 if (!gfpflags_allow_blocking(gfp))
342 return -ENOMEM;
344 i915_sw_fence_wait(signaler);
345 i915_sw_fence_set_error_once(fence, signaler->error);
346 return 0;
349 pending |= I915_SW_FENCE_FLAG_ALLOC;
352 INIT_LIST_HEAD(&wq->entry);
353 wq->flags = pending;
354 wq->func = i915_sw_fence_wake;
355 wq->private = fence;
357 i915_sw_fence_await(fence);
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;
367 spin_unlock_irqrestore(&signaler->wait.lock, flags);
369 return pending;
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)
376 return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
379 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
380 struct i915_sw_fence *signaler,
381 gfp_t gfp)
383 return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
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;
394 static void dma_i915_sw_fence_wake(struct dma_fence *dma,
395 struct dma_fence_cb *data)
397 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
399 i915_sw_fence_set_error_once(cb->fence, dma->error);
400 i915_sw_fence_complete(cb->fence);
401 kfree(cb);
404 static void timer_i915_sw_fence_wake(struct timer_list *t)
406 struct i915_sw_dma_fence_cb_timer *cb = from_timer(cb, t, timer);
407 struct i915_sw_fence *fence;
409 fence = xchg(&cb->base.fence, NULL);
410 if (!fence)
411 return;
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));
419 i915_sw_fence_set_error_once(fence, -ETIMEDOUT);
420 i915_sw_fence_complete(fence);
423 static void dma_i915_sw_fence_wake_timer(struct dma_fence *dma,
424 struct dma_fence_cb *data)
426 struct i915_sw_dma_fence_cb_timer *cb =
427 container_of(data, typeof(*cb), base.base);
428 struct i915_sw_fence *fence;
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);
436 irq_work_queue(&cb->work);
439 static void irq_i915_sw_fence_work(struct irq_work *wrk)
441 struct i915_sw_dma_fence_cb_timer *cb =
442 container_of(wrk, typeof(*cb), work);
444 del_timer_sync(&cb->timer);
445 dma_fence_put(cb->dma);
447 kfree_rcu(cb, rcu);
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)
455 struct i915_sw_dma_fence_cb *cb;
456 dma_fence_func_t func;
457 int ret;
459 debug_fence_assert(fence);
460 might_sleep_if(gfpflags_allow_blocking(gfp));
462 if (dma_fence_is_signaled(dma)) {
463 i915_sw_fence_set_error_once(fence, dma->error);
464 return 0;
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;
475 ret = dma_fence_wait(dma, false);
476 if (ret)
477 return ret;
479 i915_sw_fence_set_error_once(fence, dma->error);
480 return 0;
483 cb->fence = fence;
484 i915_sw_fence_await(fence);
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);
491 timer->dma = dma_fence_get(dma);
492 init_irq_work(&timer->work, irq_i915_sw_fence_work);
494 timer_setup(&timer->timer,
495 timer_i915_sw_fence_wake, TIMER_IRQSAFE);
496 mod_timer(&timer->timer, round_jiffies_up(jiffies + timeout));
498 func = dma_i915_sw_fence_wake_timer;
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;
510 return ret;
513 static void __dma_i915_sw_fence_wake(struct dma_fence *dma,
514 struct dma_fence_cb *data)
516 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
518 i915_sw_fence_set_error_once(cb->fence, dma->error);
519 i915_sw_fence_complete(cb->fence);
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)
526 int ret;
528 debug_fence_assert(fence);
530 if (dma_fence_is_signaled(dma)) {
531 i915_sw_fence_set_error_once(fence, dma->error);
532 return 0;
535 cb->fence = fence;
536 i915_sw_fence_await(fence);
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;
547 return ret;
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)
557 struct dma_fence *excl;
558 int ret = 0, pending;
560 debug_fence_assert(fence);
561 might_sleep_if(gfpflags_allow_blocking(gfp));
563 if (write) {
564 struct dma_fence **shared;
565 unsigned int count, i;
567 ret = dma_resv_get_fences_rcu(resv, &excl, &count, &shared);
568 if (ret)
569 return ret;
571 for (i = 0; i < count; i++) {
572 if (shared[i]->ops == exclude)
573 continue;
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;
584 ret |= pending;
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);
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;
605 dma_fence_put(excl);
607 return ret;
610 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
611 #include "selftests/lib_sw_fence.c"
612 #include "selftests/i915_sw_fence.c"
613 #endif