1 // SPDX-License-Identifier: MIT
4 * Copyright (C) 2022 Advanced Micro Devices, Inc.
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>
14 #define CHAIN_SZ (4 << 10)
17 struct dma_fence base;
21 static const char *mock_name(struct dma_fence *f)
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)
35 f = kmalloc(sizeof(*f), GFP_KERNEL);
39 spin_lock_init(&f->lock);
40 dma_fence_init(&f->base, &mock_ops, &f->lock,
41 dma_fence_context_alloc(1), 1);
46 static struct dma_fence *mock_array(unsigned int num_fences, ...)
48 struct dma_fence_array *array;
49 struct dma_fence **fences;
53 fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
57 va_start(valist, num_fences);
58 for (i = 0; i < num_fences; ++i)
59 fences[i] = va_arg(valist, typeof(*fences));
62 array = dma_fence_array_create(num_fences, fences,
63 dma_fence_context_alloc(1),
73 va_start(valist, num_fences);
74 for (i = 0; i < num_fences; ++i)
75 dma_fence_put(va_arg(valist, typeof(*fences)));
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();
92 dma_fence_chain_init(f, prev, fence, 1);
96 static int sanitycheck(void *arg)
98 struct dma_fence *f, *chain, *array;
105 dma_fence_enable_sw_signaling(f);
107 array = mock_array(1, f);
111 chain = mock_chain(NULL, array);
115 dma_fence_put(chain);
119 static int unwrap_array(void *arg)
121 struct dma_fence *fence, *f1, *f2, *array;
122 struct dma_fence_unwrap iter;
129 dma_fence_enable_sw_signaling(f1);
137 dma_fence_enable_sw_signaling(f2);
139 array = mock_array(2, f1, f2);
143 dma_fence_unwrap_for_each(fence, &iter, array) {
146 } else if (fence == f2) {
149 pr_err("Unexpected fence!\n");
155 pr_err("Not all fences seen!\n");
159 dma_fence_put(array);
163 static int unwrap_chain(void *arg)
165 struct dma_fence *fence, *f1, *f2, *chain;
166 struct dma_fence_unwrap iter;
173 dma_fence_enable_sw_signaling(f1);
181 dma_fence_enable_sw_signaling(f2);
183 chain = mock_chain(f1, f2);
187 dma_fence_unwrap_for_each(fence, &iter, chain) {
190 } else if (fence == f2) {
193 pr_err("Unexpected fence!\n");
199 pr_err("Not all fences seen!\n");
203 dma_fence_put(chain);
207 static int unwrap_chain_array(void *arg)
209 struct dma_fence *fence, *f1, *f2, *array, *chain;
210 struct dma_fence_unwrap iter;
217 dma_fence_enable_sw_signaling(f1);
225 dma_fence_enable_sw_signaling(f2);
227 array = mock_array(2, f1, f2);
231 chain = mock_chain(NULL, array);
235 dma_fence_unwrap_for_each(fence, &iter, chain) {
238 } else if (fence == f2) {
241 pr_err("Unexpected fence!\n");
247 pr_err("Not all fences seen!\n");
251 dma_fence_put(chain);
255 static int unwrap_merge(void *arg)
257 struct dma_fence *fence, *f1, *f2, *f3;
258 struct dma_fence_unwrap iter;
265 dma_fence_enable_sw_signaling(f1);
273 dma_fence_enable_sw_signaling(f2);
275 f3 = dma_fence_unwrap_merge(f1, f2);
281 dma_fence_unwrap_for_each(fence, &iter, f3) {
285 } else if (fence == f2) {
289 pr_err("Unexpected fence!\n");
295 pr_err("Not all fences seen!\n");
307 static int unwrap_merge_complex(void *arg)
309 struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5;
310 struct dma_fence_unwrap iter;
317 dma_fence_enable_sw_signaling(f1);
323 dma_fence_enable_sw_signaling(f2);
325 f3 = dma_fence_unwrap_merge(f1, f2);
329 /* The resulting array has the fences in reverse */
330 f4 = dma_fence_unwrap_merge(f2, f1);
334 /* Signaled fences should be filtered, the two arrays merged. */
335 f5 = dma_fence_unwrap_merge(f3, f4, dma_fence_get_stub());
340 dma_fence_unwrap_for_each(fence, &iter, f5) {
344 } else if (fence == f2) {
348 pr_err("Unexpected fence!\n");
354 pr_err("Not all fences seen!\n");
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);