2 * SPDX-License-Identifier: MIT
4 * (C) Copyright 2016 Intel Corporation
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)
18 #define I915_SW_FENCE_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
21 #define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
23 static DEFINE_SPINLOCK(i915_sw_fence_lock
);
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
,
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
);
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
,
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
)
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
);
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
;
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
);
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
);
160 list_move_tail(&pos
->entry
, continuation
);
166 list_for_each_entry_safe(pos
, next
, &x
->head
, entry
) {
169 wake_flags
= fence
->error
;
170 if (pos
->func
== autoremove_wake_function
)
173 pos
->func(pos
, TASK_NORMAL
, wake_flags
, &extra
);
176 if (list_empty(&extra
))
179 list_splice_tail_init(&extra
, &x
->head
);
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
))
195 debug_fence_set_state(fence
, DEBUG_FENCE_IDLE
, DEBUG_FENCE_NOTIFY
);
197 if (__i915_sw_fence_notify(fence
, FENCE_COMPLETE
) != NOTIFY_DONE
)
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
)))
215 __i915_sw_fence_complete(fence
, NULL
);
218 bool i915_sw_fence_await(struct i915_sw_fence
*fence
)
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
);
230 } while (!atomic_try_cmpxchg(&fence
->pending
, &pending
, pending
+ 1));
235 void __i915_sw_fence_init(struct i915_sw_fence
*fence
,
236 i915_sw_fence_notify_t fn
,
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);
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
)
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
))
285 if (fence
== signaler
)
288 list_for_each_entry(wq
, &fence
->wait
.head
, entry
) {
289 if (wq
->func
!= i915_sw_fence_wake
)
292 if (__i915_sw_fence_check_if_after(wq
->private, signaler
))
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
))
306 list_for_each_entry(wq
, &fence
->wait
.head
, entry
) {
307 if (wq
->func
!= i915_sw_fence_wake
)
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
)
320 if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG
))
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
);
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
)
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
);
346 debug_fence_assert(signaler
);
348 /* The dependency graph must be acyclic. */
349 if (unlikely(i915_sw_fence_check_if_after(fence
, signaler
)))
354 wq
= kmalloc(sizeof(*wq
), gfp
);
356 if (!gfpflags_allow_blocking(gfp
))
359 i915_sw_fence_wait(signaler
);
360 i915_sw_fence_set_error_once(fence
, signaler
->error
);
364 pending
|= I915_SW_FENCE_FLAG_ALLOC
;
367 INIT_LIST_HEAD(&wq
->entry
);
369 wq
->func
= i915_sw_fence_wake
;
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
);
379 i915_sw_fence_wake(wq
, 0, signaler
->error
, NULL
);
382 spin_unlock_irqrestore(&signaler
->wait
.lock
, flags
);
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
,
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
;
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
);
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
);
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
),
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
);
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
);
465 int i915_sw_fence_await_dma_fence(struct i915_sw_fence
*fence
,
466 struct dma_fence
*dma
,
467 unsigned long timeout
,
470 struct i915_sw_dma_fence_cb
*cb
;
471 dma_fence_func_t func
;
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
);
482 cb
= kmalloc(timeout
?
483 sizeof(struct i915_sw_dma_fence_cb_timer
) :
484 sizeof(struct i915_sw_dma_fence_cb
),
487 if (!gfpflags_allow_blocking(gfp
))
490 ret
= dma_fence_wait(dma
, false);
494 i915_sw_fence_set_error_once(fence
, dma
->error
);
499 i915_sw_fence_await(fence
);
501 func
= dma_i915_sw_fence_wake
;
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
);
520 func(dma
, &cb
->base
);
521 if (ret
== -ENOENT
) /* fence already signaled */
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
)
543 debug_fence_assert(fence
);
545 if (dma_fence_is_signaled(dma
)) {
546 i915_sw_fence_set_error_once(fence
, dma
->error
);
551 i915_sw_fence_await(fence
);
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
);
563 int i915_sw_fence_await_reservation(struct i915_sw_fence
*fence
,
564 struct dma_resv
*resv
,
565 const struct dma_fence_ops
*exclude
,
567 unsigned long timeout
,
570 struct dma_fence
*excl
;
571 int ret
= 0, pending
;
573 debug_fence_assert(fence
);
574 might_sleep_if(gfpflags_allow_blocking(gfp
));
577 struct dma_fence
**shared
;
578 unsigned int count
, i
;
580 ret
= dma_resv_get_fences_rcu(resv
, &excl
, &count
, &shared
);
584 for (i
= 0; i
< count
; i
++) {
585 if (shared
[i
]->ops
== exclude
)
588 pending
= i915_sw_fence_await_dma_fence(fence
,
600 for (i
= 0; i
< count
; i
++)
601 dma_fence_put(shared
[i
]);
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
,
623 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
624 #include "selftests/lib_sw_fence.c"
625 #include "selftests/i915_sw_fence.c"