1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2022 Advanced Micro Devices, Inc.
5 * Christian König <christian.koenig@amd.com>
8 #ifndef __LINUX_DMA_FENCE_UNWRAP_H
9 #define __LINUX_DMA_FENCE_UNWRAP_H
14 * struct dma_fence_unwrap - cursor into the container structure
16 * Should be used with dma_fence_unwrap_for_each() iterator macro.
18 struct dma_fence_unwrap {
20 * @chain: potential dma_fence_chain, but can be other fence as well
22 struct dma_fence *chain;
24 * @array: potential dma_fence_array, but can be other fence as well
26 struct dma_fence *array;
28 * @index: last returned index if @array is really a dma_fence_array
33 struct dma_fence *dma_fence_unwrap_first(struct dma_fence *head,
34 struct dma_fence_unwrap *cursor);
35 struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor);
38 * dma_fence_unwrap_for_each - iterate over all fences in containers
39 * @fence: current fence
40 * @cursor: current position inside the containers
41 * @head: starting point for the iterator
43 * Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all
44 * potential fences in them. If @head is just a normal fence only that one is
47 #define dma_fence_unwrap_for_each(fence, cursor, head) \
48 for (fence = dma_fence_unwrap_first(head, cursor); fence; \
49 fence = dma_fence_unwrap_next(cursor))
51 struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
52 struct dma_fence **fences,
53 struct dma_fence_unwrap *cursors);
56 * dma_fence_unwrap_merge - unwrap and merge fences
58 * All fences given as parameters are unwrapped and merged back together as flat
59 * dma_fence_array. Useful if multiple containers need to be merged together.
61 * Implemented as a macro to allocate the necessary arrays on the stack and
62 * account the stack frame size to the caller.
64 * Returns NULL on memory allocation failure, a dma_fence object representing
65 * all the given fences otherwise.
67 #define dma_fence_unwrap_merge(...) \
69 struct dma_fence *__f[] = { __VA_ARGS__ }; \
70 struct dma_fence_unwrap __c[ARRAY_SIZE(__f)]; \
72 __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c); \