1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * dma-fence-array: aggregate fences to be waited together
4  *
5  * Copyright (C) 2016 Collabora Ltd
6  * Copyright (C) 2016 Advanced Micro Devices, Inc.
7  * Authors:
8  *	Gustavo Padovan <gustavo@padovan.org>
9  *	Christian König <christian.koenig@amd.com>
10  */
11 
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/dma-fence-array.h>
15 
16 #define PENDING_ERROR 1
17 
dma_fence_array_get_driver_name(struct dma_fence * fence)18 static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
19 {
20 	return "dma_fence_array";
21 }
22 
dma_fence_array_get_timeline_name(struct dma_fence * fence)23 static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
24 {
25 	return "unbound";
26 }
27 
dma_fence_array_set_pending_error(struct dma_fence_array * array,int error)28 static void dma_fence_array_set_pending_error(struct dma_fence_array *array,
29 					      int error)
30 {
31 	/*
32 	 * Propagate the first error reported by any of our fences, but only
33 	 * before we ourselves are signaled.
34 	 */
35 	if (error)
36 		cmpxchg(&array->base.error, PENDING_ERROR, error);
37 }
38 
dma_fence_array_clear_pending_error(struct dma_fence_array * array)39 static void dma_fence_array_clear_pending_error(struct dma_fence_array *array)
40 {
41 	/* Clear the error flag if not actually set. */
42 	cmpxchg(&array->base.error, PENDING_ERROR, 0);
43 }
44 
irq_dma_fence_array_work(struct irq_work * wrk)45 static void irq_dma_fence_array_work(struct irq_work *wrk)
46 {
47 	struct dma_fence_array *array = container_of(wrk, typeof(*array), work);
48 
49 	dma_fence_array_clear_pending_error(array);
50 
51 	dma_fence_signal(&array->base);
52 	dma_fence_put(&array->base);
53 }
54 
dma_fence_array_cb_func(struct dma_fence * f,struct dma_fence_cb * cb)55 static void dma_fence_array_cb_func(struct dma_fence *f,
56 				    struct dma_fence_cb *cb)
57 {
58 	struct dma_fence_array_cb *array_cb =
59 		container_of(cb, struct dma_fence_array_cb, cb);
60 	struct dma_fence_array *array = array_cb->array;
61 
62 	dma_fence_array_set_pending_error(array, f->error);
63 
64 	if (atomic_dec_and_test(&array->num_pending))
65 		irq_work_queue(&array->work);
66 	else
67 		dma_fence_put(&array->base);
68 }
69 
dma_fence_array_enable_signaling(struct dma_fence * fence)70 static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
71 {
72 	struct dma_fence_array *array = to_dma_fence_array(fence);
73 	struct dma_fence_array_cb *cb = (void *)(&array[1]);
74 	unsigned i;
75 
76 	for (i = 0; i < array->num_fences; ++i) {
77 		cb[i].array = array;
78 		/*
79 		 * As we may report that the fence is signaled before all
80 		 * callbacks are complete, we need to take an additional
81 		 * reference count on the array so that we do not free it too
82 		 * early. The core fence handling will only hold the reference
83 		 * until we signal the array as complete (but that is now
84 		 * insufficient).
85 		 */
86 		dma_fence_get(&array->base);
87 		if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
88 					   dma_fence_array_cb_func)) {
89 			int error = array->fences[i]->error;
90 
91 			dma_fence_array_set_pending_error(array, error);
92 			dma_fence_put(&array->base);
93 			if (atomic_dec_and_test(&array->num_pending)) {
94 				dma_fence_array_clear_pending_error(array);
95 				return false;
96 			}
97 		}
98 	}
99 
100 	return true;
101 }
102 
dma_fence_array_signaled(struct dma_fence * fence)103 static bool dma_fence_array_signaled(struct dma_fence *fence)
104 {
105 	struct dma_fence_array *array = to_dma_fence_array(fence);
106 	int num_pending;
107 	unsigned int i;
108 
109 	/*
110 	 * We need to read num_pending before checking the enable_signal bit
111 	 * to avoid racing with the enable_signaling() implementation, which
112 	 * might decrement the counter, and cause a partial check.
113 	 * atomic_read_acquire() pairs with atomic_dec_and_test() in
114 	 * dma_fence_array_enable_signaling()
115 	 *
116 	 * The !--num_pending check is here to account for the any_signaled case
117 	 * if we race with enable_signaling(), that means the !num_pending check
118 	 * in the is_signalling_enabled branch might be outdated (num_pending
119 	 * might have been decremented), but that's fine. The user will get the
120 	 * right value when testing again later.
121 	 */
122 	num_pending = atomic_read_acquire(&array->num_pending);
123 	if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &array->base.flags)) {
124 		if (num_pending <= 0)
125 			goto signal;
126 		return false;
127 	}
128 
129 	for (i = 0; i < array->num_fences; ++i) {
130 		if (dma_fence_is_signaled(array->fences[i]) && !--num_pending)
131 			goto signal;
132 	}
133 	return false;
134 
135 signal:
136 	dma_fence_array_clear_pending_error(array);
137 	return true;
138 }
139 
dma_fence_array_release(struct dma_fence * fence)140 static void dma_fence_array_release(struct dma_fence *fence)
141 {
142 	struct dma_fence_array *array = to_dma_fence_array(fence);
143 	unsigned i;
144 
145 	for (i = 0; i < array->num_fences; ++i)
146 		dma_fence_put(array->fences[i]);
147 
148 	kfree(array->fences);
149 	dma_fence_free(fence);
150 }
151 
dma_fence_array_set_deadline(struct dma_fence * fence,ktime_t deadline)152 static void dma_fence_array_set_deadline(struct dma_fence *fence,
153 					 ktime_t deadline)
154 {
155 	struct dma_fence_array *array = to_dma_fence_array(fence);
156 	unsigned i;
157 
158 	for (i = 0; i < array->num_fences; ++i)
159 		dma_fence_set_deadline(array->fences[i], deadline);
160 }
161 
162 const struct dma_fence_ops dma_fence_array_ops = {
163 	.get_driver_name = dma_fence_array_get_driver_name,
164 	.get_timeline_name = dma_fence_array_get_timeline_name,
165 	.enable_signaling = dma_fence_array_enable_signaling,
166 	.signaled = dma_fence_array_signaled,
167 	.release = dma_fence_array_release,
168 	.set_deadline = dma_fence_array_set_deadline,
169 };
170 EXPORT_SYMBOL(dma_fence_array_ops);
171 
172 /**
173  * dma_fence_array_create - Create a custom fence array
174  * @num_fences:		[in]	number of fences to add in the array
175  * @fences:		[in]	array containing the fences
176  * @context:		[in]	fence context to use
177  * @seqno:		[in]	sequence number to use
178  * @signal_on_any:	[in]	signal on any fence in the array
179  *
180  * Allocate a dma_fence_array object and initialize the base fence with
181  * dma_fence_init().
182  * In case of error it returns NULL.
183  *
184  * The caller should allocate the fences array with num_fences size
185  * and fill it with the fences it wants to add to the object. Ownership of this
186  * array is taken and dma_fence_put() is used on each fence on release.
187  *
188  * If @signal_on_any is true the fence array signals if any fence in the array
189  * signals, otherwise it signals when all fences in the array signal.
190  */
dma_fence_array_create(int num_fences,struct dma_fence ** fences,u64 context,unsigned seqno,bool signal_on_any)191 struct dma_fence_array *dma_fence_array_create(int num_fences,
192 					       struct dma_fence **fences,
193 					       u64 context, unsigned seqno,
194 					       bool signal_on_any)
195 {
196 	struct dma_fence_array *array;
197 	size_t size = sizeof(*array);
198 
199 	WARN_ON(!num_fences || !fences);
200 
201 	/* Allocate the callback structures behind the array. */
202 	size += num_fences * sizeof(struct dma_fence_array_cb);
203 	array = kzalloc(size, GFP_KERNEL);
204 	if (!array)
205 		return NULL;
206 
207 	spin_lock_init(&array->lock);
208 	dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
209 		       context, seqno);
210 	init_irq_work(&array->work, irq_dma_fence_array_work);
211 
212 	array->num_fences = num_fences;
213 	atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
214 	array->fences = fences;
215 
216 	array->base.error = PENDING_ERROR;
217 
218 	/*
219 	 * dma_fence_array objects should never contain any other fence
220 	 * containers or otherwise we run into recursion and potential kernel
221 	 * stack overflow on operations on the dma_fence_array.
222 	 *
223 	 * The correct way of handling this is to flatten out the array by the
224 	 * caller instead.
225 	 *
226 	 * Enforce this here by checking that we don't create a dma_fence_array
227 	 * with any container inside.
228 	 */
229 	while (num_fences--)
230 		WARN_ON(dma_fence_is_container(fences[num_fences]));
231 
232 	return array;
233 }
234 EXPORT_SYMBOL(dma_fence_array_create);
235 
236 /**
237  * dma_fence_match_context - Check if all fences are from the given context
238  * @fence:		[in]	fence or fence array
239  * @context:		[in]	fence context to check all fences against
240  *
241  * Checks the provided fence or, for a fence array, all fences in the array
242  * against the given context. Returns false if any fence is from a different
243  * context.
244  */
dma_fence_match_context(struct dma_fence * fence,u64 context)245 bool dma_fence_match_context(struct dma_fence *fence, u64 context)
246 {
247 	struct dma_fence_array *array = to_dma_fence_array(fence);
248 	unsigned i;
249 
250 	if (!dma_fence_is_array(fence))
251 		return fence->context == context;
252 
253 	for (i = 0; i < array->num_fences; i++) {
254 		if (array->fences[i]->context != context)
255 			return false;
256 	}
257 
258 	return true;
259 }
260 EXPORT_SYMBOL(dma_fence_match_context);
261 
dma_fence_array_first(struct dma_fence * head)262 struct dma_fence *dma_fence_array_first(struct dma_fence *head)
263 {
264 	struct dma_fence_array *array;
265 
266 	if (!head)
267 		return NULL;
268 
269 	array = to_dma_fence_array(head);
270 	if (!array)
271 		return head;
272 
273 	if (!array->num_fences)
274 		return NULL;
275 
276 	return array->fences[0];
277 }
278 EXPORT_SYMBOL(dma_fence_array_first);
279 
dma_fence_array_next(struct dma_fence * head,unsigned int index)280 struct dma_fence *dma_fence_array_next(struct dma_fence *head,
281 				       unsigned int index)
282 {
283 	struct dma_fence_array *array = to_dma_fence_array(head);
284 
285 	if (!array || index >= array->num_fences)
286 		return NULL;
287 
288 	return array->fences[index];
289 }
290 EXPORT_SYMBOL(dma_fence_array_next);
291