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/gfp.h>
13 #include <linux/kref.h>
14 #include <linux/notifier.h> /* for NOTIFY_DONE */
15 #include <linux/wait.h>
16 
17 struct completion;
18 struct dma_fence;
19 struct dma_fence_ops;
20 struct reservation_object;
21 
22 struct i915_sw_fence {
23 	wait_queue_head_t wait;
24 	unsigned long flags;
25 	atomic_t pending;
26 };
27 
28 #define I915_SW_FENCE_CHECKED_BIT	0 /* used internally for DAG checking */
29 #define I915_SW_FENCE_PRIVATE_BIT	1 /* available for use by owner */
30 #define I915_SW_FENCE_MASK		(~3)
31 
32 enum i915_sw_fence_notify {
33 	FENCE_COMPLETE,
34 	FENCE_FREE
35 };
36 
37 typedef int (*i915_sw_fence_notify_t)(struct i915_sw_fence *,
38 				      enum i915_sw_fence_notify state);
39 #define __i915_sw_fence_call __aligned(4)
40 
41 void __i915_sw_fence_init(struct i915_sw_fence *fence,
42 			  i915_sw_fence_notify_t fn,
43 			  const char *name,
44 			  struct lock_class_key *key);
45 #ifdef CONFIG_LOCKDEP
46 #define i915_sw_fence_init(fence, fn)				\
47 do {								\
48 	static struct lock_class_key __key;			\
49 								\
50 	__i915_sw_fence_init((fence), (fn), #fence, &__key);	\
51 } while (0)
52 #else
53 #define i915_sw_fence_init(fence, fn)				\
54 	__i915_sw_fence_init((fence), (fn), NULL, NULL)
55 #endif
56 
57 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
58 void i915_sw_fence_fini(struct i915_sw_fence *fence);
59 #else
60 static inline void i915_sw_fence_fini(struct i915_sw_fence *fence) {}
61 #endif
62 
63 void i915_sw_fence_commit(struct i915_sw_fence *fence);
64 
65 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
66 				 struct i915_sw_fence *after,
67 				 wait_queue_entry_t *wq);
68 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
69 				     struct i915_sw_fence *after,
70 				     gfp_t gfp);
71 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
72 				  struct dma_fence *dma,
73 				  unsigned long timeout,
74 				  gfp_t gfp);
75 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
76 				    struct reservation_object *resv,
77 				    const struct dma_fence_ops *exclude,
78 				    bool write,
79 				    unsigned long timeout,
80 				    gfp_t gfp);
81 
82 static inline bool i915_sw_fence_signaled(const struct i915_sw_fence *fence)
83 {
84 	return atomic_read(&fence->pending) <= 0;
85 }
86 
87 static inline bool i915_sw_fence_done(const struct i915_sw_fence *fence)
88 {
89 	return atomic_read(&fence->pending) < 0;
90 }
91 
92 static inline void i915_sw_fence_wait(struct i915_sw_fence *fence)
93 {
94 	wait_event(fence->wait, i915_sw_fence_done(fence));
95 }
96 
97 #endif /* _I915_SW_FENCE_H_ */
98