1 /*
2  * i915_sw_fence.h - library routines for N:M synchronisation points
3  *
4  * Copyright (C) 2016 Intel Corporation
5  *
6  * This file is released under the GPLv2.
7  *
8  */
9 
10 #ifndef _I915_SW_FENCE_H_
11 #define _I915_SW_FENCE_H_
12 
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_fence;
20 struct dma_fence_ops;
21 struct reservation_object;
22 
23 struct i915_sw_fence {
24 	wait_queue_head_t wait;
25 	unsigned long flags;
26 	atomic_t pending;
27 };
28 
29 #define I915_SW_FENCE_CHECKED_BIT	0 /* used internally for DAG checking */
30 #define I915_SW_FENCE_PRIVATE_BIT	1 /* available for use by owner */
31 #define I915_SW_FENCE_MASK		(~3)
32 
33 enum i915_sw_fence_notify {
34 	FENCE_COMPLETE,
35 	FENCE_FREE
36 };
37 
38 typedef int (*i915_sw_fence_notify_t)(struct i915_sw_fence *,
39 				      enum i915_sw_fence_notify state);
40 #define __i915_sw_fence_call __aligned(4)
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 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
59 void i915_sw_fence_fini(struct i915_sw_fence *fence);
60 #else
61 static inline void i915_sw_fence_fini(struct i915_sw_fence *fence) {}
62 #endif
63 
64 void i915_sw_fence_commit(struct i915_sw_fence *fence);
65 
66 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
67 				 struct i915_sw_fence *after,
68 				 wait_queue_entry_t *wq);
69 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
70 				     struct i915_sw_fence *after,
71 				     gfp_t gfp);
72 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
73 				  struct dma_fence *dma,
74 				  unsigned long timeout,
75 				  gfp_t gfp);
76 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
77 				    struct reservation_object *resv,
78 				    const struct dma_fence_ops *exclude,
79 				    bool write,
80 				    unsigned long timeout,
81 				    gfp_t gfp);
82 
83 static inline bool i915_sw_fence_signaled(const struct i915_sw_fence *fence)
84 {
85 	return atomic_read(&fence->pending) <= 0;
86 }
87 
88 static inline bool i915_sw_fence_done(const struct i915_sw_fence *fence)
89 {
90 	return atomic_read(&fence->pending) < 0;
91 }
92 
93 static inline void i915_sw_fence_wait(struct i915_sw_fence *fence)
94 {
95 	wait_event(fence->wait, i915_sw_fence_done(fence));
96 }
97 
98 #endif /* _I915_SW_FENCE_H_ */
99