xref: /linux/include/linux/dma-fence-array.h (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * fence-array: aggregates fence 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 #ifndef __LINUX_DMA_FENCE_ARRAY_H
13 #define __LINUX_DMA_FENCE_ARRAY_H
14 
15 #include <linux/dma-fence.h>
16 #include <linux/irq_work.h>
17 
18 /**
19  * struct dma_fence_array_cb - callback helper for fence array
20  * @cb: fence callback structure for signaling
21  * @array: reference to the parent fence array object
22  */
23 struct dma_fence_array_cb {
24 	struct dma_fence_cb cb;
25 	struct dma_fence_array *array;
26 };
27 
28 /**
29  * struct dma_fence_array - fence to represent an array of fences
30  * @base: fence base class
31  * @num_fences: number of fences in the array
32  * @num_pending: fences in the array still pending
33  * @fences: array of the fences
34  * @work: internal irq_work function
35  * @callbacks: array of callback helpers
36  */
37 struct dma_fence_array {
38 	struct dma_fence base;
39 
40 	unsigned num_fences;
41 	atomic_t num_pending;
42 	struct dma_fence **fences;
43 
44 	struct irq_work work;
45 
46 	struct dma_fence_array_cb callbacks[] __counted_by(num_fences);
47 };
48 
49 /**
50  * to_dma_fence_array - cast a fence to a dma_fence_array
51  * @fence: fence to cast to a dma_fence_array
52  *
53  * Returns NULL if the fence is not a dma_fence_array,
54  * or the dma_fence_array otherwise.
55  */
56 static inline struct dma_fence_array *
to_dma_fence_array(struct dma_fence * fence)57 to_dma_fence_array(struct dma_fence *fence)
58 {
59 	if (!fence || !dma_fence_is_array(fence))
60 		return NULL;
61 
62 	return container_of(fence, struct dma_fence_array, base);
63 }
64 
65 /**
66  * dma_fence_array_for_each - iterate over all fences in array
67  * @fence: current fence
68  * @index: index into the array
69  * @head: potential dma_fence_array object
70  *
71  * Test if @array is a dma_fence_array object and if yes iterate over all fences
72  * in the array. If not just iterate over the fence in @array itself.
73  *
74  * For a deep dive iterator see dma_fence_unwrap_for_each().
75  */
76 #define dma_fence_array_for_each(fence, index, head)			\
77 	for (index = 0, fence = dma_fence_array_first(head); fence;	\
78 	     ++(index), fence = dma_fence_array_next(head, index))
79 
80 struct dma_fence_array *dma_fence_array_alloc(int num_fences);
81 void dma_fence_array_init(struct dma_fence_array *array,
82 			  int num_fences, struct dma_fence **fences,
83 			  u64 context, unsigned seqno);
84 
85 struct dma_fence_array *dma_fence_array_create(int num_fences,
86 					       struct dma_fence **fences,
87 					       u64 context, unsigned seqno);
88 
89 bool dma_fence_match_context(struct dma_fence *fence, u64 context);
90 
91 struct dma_fence *dma_fence_array_first(struct dma_fence *head);
92 struct dma_fence *dma_fence_array_next(struct dma_fence *head,
93 				       unsigned int index);
94 
95 #endif /* __LINUX_DMA_FENCE_ARRAY_H */
96