1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * i915_sw_fence.h - library routines for N:M synchronisation points
5  *
6  * Copyright (C) 2016 Intel Corporation
7  */
8 
9 #ifndef _I915_SW_FENCE_H_
10 #define _I915_SW_FENCE_H_
11 
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>
17 
18 struct completion;
19 struct dma_resv;
20 struct i915_sw_fence;
21 
22 enum i915_sw_fence_notify {
23 	FENCE_COMPLETE,
24 	FENCE_FREE
25 };
26 
27 typedef int (*i915_sw_fence_notify_t)(struct i915_sw_fence *,
28 				      enum i915_sw_fence_notify state);
29 
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
34 	unsigned long flags;
35 #endif
36 	atomic_t pending;
37 	int error;
38 };
39 
40 #define I915_SW_FENCE_CHECKED_BIT	0 /* used internally for DAG checking */
41 
42 void __i915_sw_fence_init(struct i915_sw_fence *fence,
43 			  i915_sw_fence_notify_t fn,
44 			  const char *name,
45 			  struct lock_class_key *key);
46 #ifdef CONFIG_LOCKDEP
47 #define i915_sw_fence_init(fence, fn)				\
48 do {								\
49 	static struct lock_class_key __key;			\
50 								\
51 	__i915_sw_fence_init((fence), (fn), #fence, &__key);	\
52 } while (0)
53 #else
54 #define i915_sw_fence_init(fence, fn)				\
55 	__i915_sw_fence_init((fence), (fn), NULL, NULL)
56 #endif
57 
58 void i915_sw_fence_reinit(struct i915_sw_fence *fence);
59 
60 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
61 void i915_sw_fence_fini(struct i915_sw_fence *fence);
62 #else
63 static inline void i915_sw_fence_fini(struct i915_sw_fence *fence) {}
64 #endif
65 
66 void i915_sw_fence_commit(struct i915_sw_fence *fence);
67 
68 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
69 				 struct i915_sw_fence *after,
70 				 wait_queue_entry_t *wq);
71 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
72 				     struct i915_sw_fence *after,
73 				     gfp_t gfp);
74 
75 struct i915_sw_dma_fence_cb {
76 	struct dma_fence_cb base;
77 	struct i915_sw_fence *fence;
78 };
79 
80 int __i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
81 				    struct dma_fence *dma,
82 				    struct i915_sw_dma_fence_cb *cb);
83 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
84 				  struct dma_fence *dma,
85 				  unsigned long timeout,
86 				  gfp_t gfp);
87 
88 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
89 				    struct dma_resv *resv,
90 				    const struct dma_fence_ops *exclude,
91 				    bool write,
92 				    unsigned long timeout,
93 				    gfp_t gfp);
94 
95 bool i915_sw_fence_await(struct i915_sw_fence *fence);
96 void i915_sw_fence_complete(struct i915_sw_fence *fence);
97 
98 static inline bool i915_sw_fence_signaled(const struct i915_sw_fence *fence)
99 {
100 	return atomic_read(&fence->pending) <= 0;
101 }
102 
103 static inline bool i915_sw_fence_done(const struct i915_sw_fence *fence)
104 {
105 	return atomic_read(&fence->pending) < 0;
106 }
107 
108 static inline void i915_sw_fence_wait(struct i915_sw_fence *fence)
109 {
110 	wait_event(fence->wait, i915_sw_fence_done(fence));
111 }
112 
113 static inline void
114 i915_sw_fence_set_error_once(struct i915_sw_fence *fence, int error)
115 {
116 	if (unlikely(error))
117 		cmpxchg(&fence->error, 0, error);
118 }
119 
120 #endif /* _I915_SW_FENCE_H_ */
121