1 /*
2  * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef _LINUX_DMA_FENCE_ARRAY_H_
28 #define _LINUX_DMA_FENCE_ARRAY_H_
29 
30 #include <linux/dma-fence.h>
31 #include <linux/irq_work.h>
32 
33 /**
34  * struct dma_fence_array - fence to represent an array of fences
35  * @base: fence base class
36  * @lock: spinlock for fence handling
37  * @num_fences: number of fences in the array
38  * @num_pending: fences in the array still pending
39  * @fences: array of the fences
40  * @work: internal irq_work function
41  */
42 struct dma_fence_array {
43         struct dma_fence base;
44 
45         spinlock_t lock;
46         unsigned num_fences;
47         atomic_t num_pending;
48         struct dma_fence **fences;
49 
50         struct irq_work work;
51 };
52 
53 extern const struct dma_fence_ops dma_fence_array_ops;
54 
55 /**
56  * struct dma_fence_array_cb - callback helper for fence array
57  * @cb: fence callback structure for signaling
58  * @array: reference to the parent fence array object
59  */
60 struct dma_fence_array_cb {
61         struct dma_fence_cb cb;
62         struct dma_fence_array *array;
63 };
64 
65 /**
66  * dma_fence_is_array - check if a fence is from the array subsclass
67  * @fence: fence to test
68  *
69  * Return true if it is a dma_fence_array and false otherwise.
70  */
71 static inline bool dma_fence_is_array(struct dma_fence *fence)
72 {
73         return fence->ops == &dma_fence_array_ops;
74 }
75 
76 /**
77  * to_dma_fence_array - cast a fence to a dma_fence_array
78  * @fence: fence to cast to a dma_fence_array
79  *
80  * Returns NULL if the fence is not a dma_fence_array,
81  * or the dma_fence_array otherwise.
82  */
83 static inline struct dma_fence_array *
84 to_dma_fence_array(struct dma_fence *fence)
85 {
86         if (fence->ops != &dma_fence_array_ops)
87                 return NULL;
88 
89         return container_of(fence, struct dma_fence_array, base);
90 }
91 
92 struct dma_fence_array *dma_fence_array_create(int num_fences,
93                                                struct dma_fence **fences,
94                                                u64 context, unsigned seqno,
95                                                bool signal_on_any);
96 
97 #endif	/* _LINUX_DMA_FENCE_ARRAY_H_ */
98