Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / dma-buf / st-dma-fence-unwrap.c
blobf0cee984b6c74cf16bd394ba674a3ac8cb59434b
1 // SPDX-License-Identifier: MIT
3 /*
4 * Copyright (C) 2022 Advanced Micro Devices, Inc.
5 */
7 #include <linux/dma-fence.h>
8 #include <linux/dma-fence-array.h>
9 #include <linux/dma-fence-chain.h>
10 #include <linux/dma-fence-unwrap.h>
12 #include "selftest.h"
14 #define CHAIN_SZ (4 << 10)
16 struct mock_fence {
17 struct dma_fence base;
18 spinlock_t lock;
21 static const char *mock_name(struct dma_fence *f)
23 return "mock";
26 static const struct dma_fence_ops mock_ops = {
27 .get_driver_name = mock_name,
28 .get_timeline_name = mock_name,
31 static struct dma_fence *mock_fence(void)
33 struct mock_fence *f;
35 f = kmalloc(sizeof(*f), GFP_KERNEL);
36 if (!f)
37 return NULL;
39 spin_lock_init(&f->lock);
40 dma_fence_init(&f->base, &mock_ops, &f->lock,
41 dma_fence_context_alloc(1), 1);
43 return &f->base;
46 static struct dma_fence *mock_array(unsigned int num_fences, ...)
48 struct dma_fence_array *array;
49 struct dma_fence **fences;
50 va_list valist;
51 int i;
53 fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
54 if (!fences)
55 goto error_put;
57 va_start(valist, num_fences);
58 for (i = 0; i < num_fences; ++i)
59 fences[i] = va_arg(valist, typeof(*fences));
60 va_end(valist);
62 array = dma_fence_array_create(num_fences, fences,
63 dma_fence_context_alloc(1),
64 1, false);
65 if (!array)
66 goto error_free;
67 return &array->base;
69 error_free:
70 kfree(fences);
72 error_put:
73 va_start(valist, num_fences);
74 for (i = 0; i < num_fences; ++i)
75 dma_fence_put(va_arg(valist, typeof(*fences)));
76 va_end(valist);
77 return NULL;
80 static struct dma_fence *mock_chain(struct dma_fence *prev,
81 struct dma_fence *fence)
83 struct dma_fence_chain *f;
85 f = dma_fence_chain_alloc();
86 if (!f) {
87 dma_fence_put(prev);
88 dma_fence_put(fence);
89 return NULL;
92 dma_fence_chain_init(f, prev, fence, 1);
93 return &f->base;
96 static int sanitycheck(void *arg)
98 struct dma_fence *f, *chain, *array;
99 int err = 0;
101 f = mock_fence();
102 if (!f)
103 return -ENOMEM;
105 dma_fence_enable_sw_signaling(f);
107 array = mock_array(1, f);
108 if (!array)
109 return -ENOMEM;
111 chain = mock_chain(NULL, array);
112 if (!chain)
113 return -ENOMEM;
115 dma_fence_put(chain);
116 return err;
119 static int unwrap_array(void *arg)
121 struct dma_fence *fence, *f1, *f2, *array;
122 struct dma_fence_unwrap iter;
123 int err = 0;
125 f1 = mock_fence();
126 if (!f1)
127 return -ENOMEM;
129 dma_fence_enable_sw_signaling(f1);
131 f2 = mock_fence();
132 if (!f2) {
133 dma_fence_put(f1);
134 return -ENOMEM;
137 dma_fence_enable_sw_signaling(f2);
139 array = mock_array(2, f1, f2);
140 if (!array)
141 return -ENOMEM;
143 dma_fence_unwrap_for_each(fence, &iter, array) {
144 if (fence == f1) {
145 f1 = NULL;
146 } else if (fence == f2) {
147 f2 = NULL;
148 } else {
149 pr_err("Unexpected fence!\n");
150 err = -EINVAL;
154 if (f1 || f2) {
155 pr_err("Not all fences seen!\n");
156 err = -EINVAL;
159 dma_fence_put(array);
160 return err;
163 static int unwrap_chain(void *arg)
165 struct dma_fence *fence, *f1, *f2, *chain;
166 struct dma_fence_unwrap iter;
167 int err = 0;
169 f1 = mock_fence();
170 if (!f1)
171 return -ENOMEM;
173 dma_fence_enable_sw_signaling(f1);
175 f2 = mock_fence();
176 if (!f2) {
177 dma_fence_put(f1);
178 return -ENOMEM;
181 dma_fence_enable_sw_signaling(f2);
183 chain = mock_chain(f1, f2);
184 if (!chain)
185 return -ENOMEM;
187 dma_fence_unwrap_for_each(fence, &iter, chain) {
188 if (fence == f1) {
189 f1 = NULL;
190 } else if (fence == f2) {
191 f2 = NULL;
192 } else {
193 pr_err("Unexpected fence!\n");
194 err = -EINVAL;
198 if (f1 || f2) {
199 pr_err("Not all fences seen!\n");
200 err = -EINVAL;
203 dma_fence_put(chain);
204 return err;
207 static int unwrap_chain_array(void *arg)
209 struct dma_fence *fence, *f1, *f2, *array, *chain;
210 struct dma_fence_unwrap iter;
211 int err = 0;
213 f1 = mock_fence();
214 if (!f1)
215 return -ENOMEM;
217 dma_fence_enable_sw_signaling(f1);
219 f2 = mock_fence();
220 if (!f2) {
221 dma_fence_put(f1);
222 return -ENOMEM;
225 dma_fence_enable_sw_signaling(f2);
227 array = mock_array(2, f1, f2);
228 if (!array)
229 return -ENOMEM;
231 chain = mock_chain(NULL, array);
232 if (!chain)
233 return -ENOMEM;
235 dma_fence_unwrap_for_each(fence, &iter, chain) {
236 if (fence == f1) {
237 f1 = NULL;
238 } else if (fence == f2) {
239 f2 = NULL;
240 } else {
241 pr_err("Unexpected fence!\n");
242 err = -EINVAL;
246 if (f1 || f2) {
247 pr_err("Not all fences seen!\n");
248 err = -EINVAL;
251 dma_fence_put(chain);
252 return err;
255 static int unwrap_merge(void *arg)
257 struct dma_fence *fence, *f1, *f2, *f3;
258 struct dma_fence_unwrap iter;
259 int err = 0;
261 f1 = mock_fence();
262 if (!f1)
263 return -ENOMEM;
265 dma_fence_enable_sw_signaling(f1);
267 f2 = mock_fence();
268 if (!f2) {
269 err = -ENOMEM;
270 goto error_put_f1;
273 dma_fence_enable_sw_signaling(f2);
275 f3 = dma_fence_unwrap_merge(f1, f2);
276 if (!f3) {
277 err = -ENOMEM;
278 goto error_put_f2;
281 dma_fence_unwrap_for_each(fence, &iter, f3) {
282 if (fence == f1) {
283 dma_fence_put(f1);
284 f1 = NULL;
285 } else if (fence == f2) {
286 dma_fence_put(f2);
287 f2 = NULL;
288 } else {
289 pr_err("Unexpected fence!\n");
290 err = -EINVAL;
294 if (f1 || f2) {
295 pr_err("Not all fences seen!\n");
296 err = -EINVAL;
299 dma_fence_put(f3);
300 error_put_f2:
301 dma_fence_put(f2);
302 error_put_f1:
303 dma_fence_put(f1);
304 return err;
307 static int unwrap_merge_complex(void *arg)
309 struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5;
310 struct dma_fence_unwrap iter;
311 int err = -ENOMEM;
313 f1 = mock_fence();
314 if (!f1)
315 return -ENOMEM;
317 dma_fence_enable_sw_signaling(f1);
319 f2 = mock_fence();
320 if (!f2)
321 goto error_put_f1;
323 dma_fence_enable_sw_signaling(f2);
325 f3 = dma_fence_unwrap_merge(f1, f2);
326 if (!f3)
327 goto error_put_f2;
329 /* The resulting array has the fences in reverse */
330 f4 = dma_fence_unwrap_merge(f2, f1);
331 if (!f4)
332 goto error_put_f3;
334 /* Signaled fences should be filtered, the two arrays merged. */
335 f5 = dma_fence_unwrap_merge(f3, f4, dma_fence_get_stub());
336 if (!f5)
337 goto error_put_f4;
339 err = 0;
340 dma_fence_unwrap_for_each(fence, &iter, f5) {
341 if (fence == f1) {
342 dma_fence_put(f1);
343 f1 = NULL;
344 } else if (fence == f2) {
345 dma_fence_put(f2);
346 f2 = NULL;
347 } else {
348 pr_err("Unexpected fence!\n");
349 err = -EINVAL;
353 if (f1 || f2) {
354 pr_err("Not all fences seen!\n");
355 err = -EINVAL;
358 dma_fence_put(f5);
359 error_put_f4:
360 dma_fence_put(f4);
361 error_put_f3:
362 dma_fence_put(f3);
363 error_put_f2:
364 dma_fence_put(f2);
365 error_put_f1:
366 dma_fence_put(f1);
367 return err;
370 int dma_fence_unwrap(void)
372 static const struct subtest tests[] = {
373 SUBTEST(sanitycheck),
374 SUBTEST(unwrap_array),
375 SUBTEST(unwrap_chain),
376 SUBTEST(unwrap_chain_array),
377 SUBTEST(unwrap_merge),
378 SUBTEST(unwrap_merge_complex),
381 return subtests(tests, NULL);