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 */
24 struct dma_fence_chain {
25 struct dma_fence base;
26 struct dma_fence __rcu *prev;
27 u64 prev_seqno;
28 struct dma_fence *fence;
29 union {
30 /**
31 * @cb: callback for signaling
32 *
33 * This is used to add the callback for signaling the
34 * complection of the fence chain. Never used at the same time
35 * as the irq work.
36 */
37 struct dma_fence_cb cb;
38
39 /**
40 * @work: irq work item for signaling
41 *
42 * Irq work structure to allow us to add the callback without
43 * running into lock inversion. Never used at the same time as
44 * the callback.
45 */
46 struct irq_work work;
47 };
48 };
49
50
51 /**
52 * to_dma_fence_chain - cast a fence to a dma_fence_chain
53 * @fence: fence to cast to a dma_fence_array
54 *
55 * Returns NULL if the fence is not a dma_fence_chain,
56 * or the dma_fence_chain otherwise.
57 */
58 static inline struct dma_fence_chain *
to_dma_fence_chain(struct dma_fence * fence)59 to_dma_fence_chain(struct dma_fence *fence)
60 {
61 if (!fence || !dma_fence_is_chain(fence))
62 return NULL;
63
64 return container_of(fence, struct dma_fence_chain, base);
65 }
66
67 /**
68 * dma_fence_chain_contained - return the contained fence
69 * @fence: the fence to test
70 *
71 * If the fence is a dma_fence_chain the function returns the fence contained
72 * inside the chain object, otherwise it returns the fence itself.
73 */
74 static inline struct dma_fence *
dma_fence_chain_contained(struct dma_fence * fence)75 dma_fence_chain_contained(struct dma_fence *fence)
76 {
77 struct dma_fence_chain *chain = to_dma_fence_chain(fence);
78
79 return chain ? chain->fence : fence;
80 }
81
82 /**
83 * dma_fence_chain_alloc - Returns a new &struct dma_fence_chain object or
84 * %NULL on failure.
85 *
86 * This specialized allocator has to be a macro for its allocations to be
87 * accounted separately (to have a separate alloc_tag). The typecast is
88 * intentional to enforce typesafety.
89 */
90 #define dma_fence_chain_alloc() \
91 kmalloc_obj(struct dma_fence_chain)
92
93 /**
94 * dma_fence_chain_free - Frees an allocated but not used
95 * &struct dma_fence_chain object.
96 * @chain: chain node to free
97 *
98 * Frees up an allocated but not used struct dma_fence_chain object. This
99 * doesn't need an RCU grace period since the fence was never initialized nor
100 * published. After dma_fence_chain_init() has been called the fence must be
101 * released by calling dma_fence_put(), and not through this function.
102 */
dma_fence_chain_free(struct dma_fence_chain * chain)103 static inline void dma_fence_chain_free(struct dma_fence_chain *chain)
104 {
105 kfree(chain);
106 };
107
108 /**
109 * dma_fence_chain_for_each - iterate over all fences in chain
110 * @iter: current fence
111 * @head: starting point
112 *
113 * Iterate over all fences in the chain. We keep a reference to the current
114 * fence while inside the loop which must be dropped when breaking out.
115 *
116 * For a deep dive iterator see dma_fence_unwrap_for_each().
117 */
118 #define dma_fence_chain_for_each(iter, head) \
119 for (iter = dma_fence_get(head); iter; \
120 iter = dma_fence_chain_walk(iter))
121
122 struct dma_fence *dma_fence_chain_walk(struct dma_fence *fence);
123 int dma_fence_chain_find_seqno(struct dma_fence **pfence, uint64_t seqno);
124 void dma_fence_chain_init(struct dma_fence_chain *chain,
125 struct dma_fence *prev,
126 struct dma_fence *fence,
127 uint64_t seqno);
128
129 #endif /* __LINUX_DMA_FENCE_CHAIN_H */
130