1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * fence-chain: chain fences together in a timeline
4 *
5 * Copyright (C) 2018 Advanced Micro Devices, Inc.
6 * Authors:
7 * Christian König <christian.koenig@amd.com>
8 */
9
10 #ifndef __LINUX_DMA_FENCE_CHAIN_H
11 #define __LINUX_DMA_FENCE_CHAIN_H
12
13 #include <linux/dma-fence.h>
14 #include <linux/irq_work.h>
15 #include <linux/slab.h>
16
17 /**
18 * struct dma_fence_chain - fence to represent an node of a fence chain
19 * @base: fence base class
20 * @prev: previous fence of the chain
21 * @prev_seqno: original previous seqno before garbage collection
22 * @fence: encapsulated fence
23 * @lock: spinlock for fence handling
24 */
25 struct dma_fence_chain {
26 struct dma_fence base;
27 struct dma_fence __rcu *prev;
28 u64 prev_seqno;
29 struct dma_fence *fence;
30 union {
31 /**
32 * @cb: callback for signaling
33 *
34 * This is used to add the callback for signaling the
35 * complection of the fence chain. Never used at the same time
36 * as the irq work.
37 */
38 struct dma_fence_cb cb;
39
40 /**
41 * @work: irq work item for signaling
42 *
43 * Irq work structure to allow us to add the callback without
44 * running into lock inversion. Never used at the same time as
45 * the callback.
46 */
47 struct irq_work work;
48 };
49 spinlock_t lock;
50 };
51
52
53 /**
54 * to_dma_fence_chain - cast a fence to a dma_fence_chain
55 * @fence: fence to cast to a dma_fence_array
56 *
57 * Returns NULL if the fence is not a dma_fence_chain,
58 * or the dma_fence_chain otherwise.
59 */
60 static inline struct dma_fence_chain *
to_dma_fence_chain(struct dma_fence * fence)61 to_dma_fence_chain(struct dma_fence *fence)
62 {
63 if (!fence || !dma_fence_is_chain(fence))
64 return NULL;
65
66 return container_of(fence, struct dma_fence_chain, base);
67 }
68
69 /**
70 * dma_fence_chain_contained - return the contained fence
71 * @fence: the fence to test
72 *
73 * If the fence is a dma_fence_chain the function returns the fence contained
74 * inside the chain object, otherwise it returns the fence itself.
75 */
76 static inline struct dma_fence *
dma_fence_chain_contained(struct dma_fence * fence)77 dma_fence_chain_contained(struct dma_fence *fence)
78 {
79 struct dma_fence_chain *chain = to_dma_fence_chain(fence);
80
81 return chain ? chain->fence : fence;
82 }
83
84 /**
85 * dma_fence_chain_alloc
86 *
87 * Returns a new struct dma_fence_chain object or NULL on failure.
88 *
89 * This specialized allocator has to be a macro for its allocations to be
90 * accounted separately (to have a separate alloc_tag). The typecast is
91 * intentional to enforce typesafety.
92 */
93 #define dma_fence_chain_alloc() \
94 ((struct dma_fence_chain *)kmalloc(sizeof(struct dma_fence_chain), GFP_KERNEL))
95
96 /**
97 * dma_fence_chain_free
98 * @chain: chain node to free
99 *
100 * Frees up an allocated but not used struct dma_fence_chain object. This
101 * doesn't need an RCU grace period since the fence was never initialized nor
102 * published. After dma_fence_chain_init() has been called the fence must be
103 * released by calling dma_fence_put(), and not through this function.
104 */
dma_fence_chain_free(struct dma_fence_chain * chain)105 static inline void dma_fence_chain_free(struct dma_fence_chain *chain)
106 {
107 kfree(chain);
108 };
109
110 /**
111 * dma_fence_chain_for_each - iterate over all fences in chain
112 * @iter: current fence
113 * @head: starting point
114 *
115 * Iterate over all fences in the chain. We keep a reference to the current
116 * fence while inside the loop which must be dropped when breaking out.
117 *
118 * For a deep dive iterator see dma_fence_unwrap_for_each().
119 */
120 #define dma_fence_chain_for_each(iter, head) \
121 for (iter = dma_fence_get(head); iter; \
122 iter = dma_fence_chain_walk(iter))
123
124 struct dma_fence *dma_fence_chain_walk(struct dma_fence *fence);
125 int dma_fence_chain_find_seqno(struct dma_fence **pfence, uint64_t seqno);
126 void dma_fence_chain_init(struct dma_fence_chain *chain,
127 struct dma_fence *prev,
128 struct dma_fence *fence,
129 uint64_t seqno);
130
131 #endif /* __LINUX_DMA_FENCE_CHAIN_H */
132