1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * fence-chain: chain fences together in a timeline
5 * Copyright (C) 2022 Advanced Micro Devices, Inc.
7 * Christian König <christian.koenig@amd.com>
10 #ifndef __LINUX_DMA_FENCE_UNWRAP_H
11 #define __LINUX_DMA_FENCE_UNWRAP_H
13 #include <linux/dma-fence-chain.h>
14 #include <linux/dma-fence-array.h>
17 * struct dma_fence_unwrap - cursor into the container structure
19 * Should be used with dma_fence_unwrap_for_each() iterator macro.
21 struct dma_fence_unwrap {
23 * @chain: potential dma_fence_chain, but can be other fence as well
25 struct dma_fence *chain;
27 * @array: potential dma_fence_array, but can be other fence as well
29 struct dma_fence *array;
31 * @index: last returned index if @array is really a dma_fence_array
36 /* Internal helper to start new array iteration, don't use directly */
37 static inline struct dma_fence *
38 __dma_fence_unwrap_array(struct dma_fence_unwrap * cursor)
40 cursor->array = dma_fence_chain_contained(cursor->chain);
42 return dma_fence_array_first(cursor->array);
46 * dma_fence_unwrap_first - return the first fence from fence containers
47 * @head: the entrypoint into the containers
48 * @cursor: current position inside the containers
50 * Unwraps potential dma_fence_chain/dma_fence_array containers and return the
53 static inline struct dma_fence *
54 dma_fence_unwrap_first(struct dma_fence *head, struct dma_fence_unwrap *cursor)
56 cursor->chain = dma_fence_get(head);
57 return __dma_fence_unwrap_array(cursor);
61 * dma_fence_unwrap_next - return the next fence from a fence containers
62 * @cursor: current position inside the containers
64 * Continue unwrapping the dma_fence_chain/dma_fence_array containers and return
65 * the next fence from them.
67 static inline struct dma_fence *
68 dma_fence_unwrap_next(struct dma_fence_unwrap *cursor)
70 struct dma_fence *tmp;
73 tmp = dma_fence_array_next(cursor->array, cursor->index);
77 cursor->chain = dma_fence_chain_walk(cursor->chain);
78 return __dma_fence_unwrap_array(cursor);
82 * dma_fence_unwrap_for_each - iterate over all fences in containers
83 * @fence: current fence
84 * @cursor: current position inside the containers
85 * @head: starting point for the iterator
87 * Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all
88 * potential fences in them. If @head is just a normal fence only that one is
91 #define dma_fence_unwrap_for_each(fence, cursor, head) \
92 for (fence = dma_fence_unwrap_first(head, cursor); fence; \
93 fence = dma_fence_unwrap_next(cursor))