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 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
) {
168 TASK_NORMAL
, fence
->error
,
172 if (list_empty(&extra
))
175 list_splice_tail_init(&extra
, &x
->head
);
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
))
191 debug_fence_set_state(fence
, DEBUG_FENCE_IDLE
, DEBUG_FENCE_NOTIFY
);
193 if (__i915_sw_fence_notify(fence
, FENCE_COMPLETE
) != NOTIFY_DONE
)
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
)))
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
,
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);
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
)
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
))
270 if (fence
== signaler
)
273 list_for_each_entry(wq
, &fence
->wait
.head
, entry
) {
274 if (wq
->func
!= i915_sw_fence_wake
)
277 if (__i915_sw_fence_check_if_after(wq
->private, signaler
))
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
))
291 list_for_each_entry(wq
, &fence
->wait
.head
, entry
) {
292 if (wq
->func
!= i915_sw_fence_wake
)
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
)
305 if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG
))
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
);
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
)
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
);
331 debug_fence_assert(signaler
);
333 /* The dependency graph must be acyclic. */
334 if (unlikely(i915_sw_fence_check_if_after(fence
, signaler
)))
339 wq
= kmalloc(sizeof(*wq
), gfp
);
341 if (!gfpflags_allow_blocking(gfp
))
344 i915_sw_fence_wait(signaler
);
345 i915_sw_fence_set_error_once(fence
, signaler
->error
);
349 pending
|= I915_SW_FENCE_FLAG_ALLOC
;
352 INIT_LIST_HEAD(&wq
->entry
);
354 wq
->func
= i915_sw_fence_wake
;
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
);
364 i915_sw_fence_wake(wq
, 0, signaler
->error
, NULL
);
367 spin_unlock_irqrestore(&signaler
->wait
.lock
, flags
);
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
,
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
;
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
);
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
);
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
),
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
);
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
);
450 int i915_sw_fence_await_dma_fence(struct i915_sw_fence
*fence
,
451 struct dma_fence
*dma
,
452 unsigned long timeout
,
455 struct i915_sw_dma_fence_cb
*cb
;
456 dma_fence_func_t func
;
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
);
467 cb
= kmalloc(timeout
?
468 sizeof(struct i915_sw_dma_fence_cb_timer
) :
469 sizeof(struct i915_sw_dma_fence_cb
),
472 if (!gfpflags_allow_blocking(gfp
))
475 ret
= dma_fence_wait(dma
, false);
479 i915_sw_fence_set_error_once(fence
, dma
->error
);
484 i915_sw_fence_await(fence
);
486 func
= dma_i915_sw_fence_wake
;
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
);
505 func(dma
, &cb
->base
);
506 if (ret
== -ENOENT
) /* fence already signaled */
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
)
528 debug_fence_assert(fence
);
530 if (dma_fence_is_signaled(dma
)) {
531 i915_sw_fence_set_error_once(fence
, dma
->error
);
536 i915_sw_fence_await(fence
);
538 ret
= dma_fence_add_callback(dma
, &cb
->base
, __dma_i915_sw_fence_wake
);
542 __dma_i915_sw_fence_wake(dma
, &cb
->base
);
543 if (ret
== -ENOENT
) /* fence already signaled */
550 int i915_sw_fence_await_reservation(struct i915_sw_fence
*fence
,
551 struct dma_resv
*resv
,
552 const struct dma_fence_ops
*exclude
,
554 unsigned long timeout
,
557 struct dma_fence
*excl
;
558 int ret
= 0, pending
;
560 debug_fence_assert(fence
);
561 might_sleep_if(gfpflags_allow_blocking(gfp
));
564 struct dma_fence
**shared
;
565 unsigned int count
, i
;
567 ret
= dma_resv_get_fences_rcu(resv
, &excl
, &count
, &shared
);
571 for (i
= 0; i
< count
; i
++) {
572 if (shared
[i
]->ops
== exclude
)
575 pending
= i915_sw_fence_await_dma_fence(fence
,
587 for (i
= 0; i
< count
; i
++)
588 dma_fence_put(shared
[i
]);
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
,
610 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
611 #include "selftests/lib_sw_fence.c"
612 #include "selftests/i915_sw_fence.c"