2 * SPDX-License-Identifier: MIT
4 * i915_sw_fence.h - library routines for N:M synchronisation points
6 * Copyright (C) 2016 Intel Corporation
9 #ifndef _I915_SW_FENCE_H_
10 #define _I915_SW_FENCE_H_
12 #include <linux/dma-fence.h>
13 #include <linux/gfp.h>
14 #include <linux/kref.h>
15 #include <linux/notifier.h> /* for NOTIFY_DONE */
16 #include <linux/wait.h>
22 enum i915_sw_fence_notify {
27 typedef int (*i915_sw_fence_notify_t)(struct i915_sw_fence *,
28 enum i915_sw_fence_notify state);
30 struct i915_sw_fence {
31 wait_queue_head_t wait;
32 i915_sw_fence_notify_t fn;
33 #ifdef CONFIG_DRM_I915_SW_FENCE_CHECK_DAG
40 #define I915_SW_FENCE_CHECKED_BIT 0 /* used internally for DAG checking */
42 void __i915_sw_fence_init(struct i915_sw_fence *fence,
43 i915_sw_fence_notify_t fn,
45 struct lock_class_key *key);
47 #define i915_sw_fence_init(fence, fn) \
49 static struct lock_class_key __key; \
51 BUILD_BUG_ON((fn) == NULL); \
52 __i915_sw_fence_init((fence), (fn), #fence, &__key); \
55 #define i915_sw_fence_init(fence, fn) \
57 BUILD_BUG_ON((fn) == NULL); \
58 __i915_sw_fence_init((fence), (fn), NULL, NULL); \
62 void i915_sw_fence_reinit(struct i915_sw_fence *fence);
64 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
65 void i915_sw_fence_fini(struct i915_sw_fence *fence);
67 static inline void i915_sw_fence_fini(struct i915_sw_fence *fence) {}
70 void i915_sw_fence_commit(struct i915_sw_fence *fence);
72 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
73 struct i915_sw_fence *after,
74 wait_queue_entry_t *wq);
75 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
76 struct i915_sw_fence *after,
79 struct i915_sw_dma_fence_cb {
80 struct dma_fence_cb base;
81 struct i915_sw_fence *fence;
84 int __i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
85 struct dma_fence *dma,
86 struct i915_sw_dma_fence_cb *cb);
87 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
88 struct dma_fence *dma,
89 unsigned long timeout,
92 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
93 struct dma_resv *resv,
95 unsigned long timeout,
98 bool i915_sw_fence_await(struct i915_sw_fence *fence);
99 void i915_sw_fence_complete(struct i915_sw_fence *fence);
101 static inline bool i915_sw_fence_signaled(const struct i915_sw_fence *fence)
103 return atomic_read(&fence->pending) <= 0;
106 static inline bool i915_sw_fence_done(const struct i915_sw_fence *fence)
108 return atomic_read(&fence->pending) < 0;
111 static inline void i915_sw_fence_wait(struct i915_sw_fence *fence)
113 wait_event(fence->wait, i915_sw_fence_done(fence));
117 i915_sw_fence_set_error_once(struct i915_sw_fence *fence, int error)
120 cmpxchg(&fence->error, 0, error);
123 #endif /* _I915_SW_FENCE_H_ */