1 // SPDX-License-Identifier: GPL-2.0-only
3 * dma-fence-array: aggregate fences to be waited together
5 * Copyright (C) 2016 Collabora Ltd
6 * Copyright (C) 2016 Advanced Micro Devices, Inc.
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/dma-fence-array.h>
16 static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
18 return "dma_fence_array";
21 static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
26 static void irq_dma_fence_array_work(struct irq_work *wrk)
28 struct dma_fence_array *array = container_of(wrk, typeof(*array), work);
30 dma_fence_signal(&array->base);
31 dma_fence_put(&array->base);
34 static void dma_fence_array_cb_func(struct dma_fence *f,
35 struct dma_fence_cb *cb)
37 struct dma_fence_array_cb *array_cb =
38 container_of(cb, struct dma_fence_array_cb, cb);
39 struct dma_fence_array *array = array_cb->array;
41 if (atomic_dec_and_test(&array->num_pending))
42 irq_work_queue(&array->work);
44 dma_fence_put(&array->base);
47 static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
49 struct dma_fence_array *array = to_dma_fence_array(fence);
50 struct dma_fence_array_cb *cb = (void *)(&array[1]);
53 for (i = 0; i < array->num_fences; ++i) {
56 * As we may report that the fence is signaled before all
57 * callbacks are complete, we need to take an additional
58 * reference count on the array so that we do not free it too
59 * early. The core fence handling will only hold the reference
60 * until we signal the array as complete (but that is now
63 dma_fence_get(&array->base);
64 if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
65 dma_fence_array_cb_func)) {
66 dma_fence_put(&array->base);
67 if (atomic_dec_and_test(&array->num_pending))
75 static bool dma_fence_array_signaled(struct dma_fence *fence)
77 struct dma_fence_array *array = to_dma_fence_array(fence);
79 return atomic_read(&array->num_pending) <= 0;
82 static void dma_fence_array_release(struct dma_fence *fence)
84 struct dma_fence_array *array = to_dma_fence_array(fence);
87 for (i = 0; i < array->num_fences; ++i)
88 dma_fence_put(array->fences[i]);
91 dma_fence_free(fence);
94 const struct dma_fence_ops dma_fence_array_ops = {
95 .get_driver_name = dma_fence_array_get_driver_name,
96 .get_timeline_name = dma_fence_array_get_timeline_name,
97 .enable_signaling = dma_fence_array_enable_signaling,
98 .signaled = dma_fence_array_signaled,
99 .release = dma_fence_array_release,
101 EXPORT_SYMBOL(dma_fence_array_ops);
104 * dma_fence_array_create - Create a custom fence array
105 * @num_fences: [in] number of fences to add in the array
106 * @fences: [in] array containing the fences
107 * @context: [in] fence context to use
108 * @seqno: [in] sequence number to use
109 * @signal_on_any: [in] signal on any fence in the array
111 * Allocate a dma_fence_array object and initialize the base fence with
113 * In case of error it returns NULL.
115 * The caller should allocate the fences array with num_fences size
116 * and fill it with the fences it wants to add to the object. Ownership of this
117 * array is taken and dma_fence_put() is used on each fence on release.
119 * If @signal_on_any is true the fence array signals if any fence in the array
120 * signals, otherwise it signals when all fences in the array signal.
122 struct dma_fence_array *dma_fence_array_create(int num_fences,
123 struct dma_fence **fences,
124 u64 context, unsigned seqno,
127 struct dma_fence_array *array;
128 size_t size = sizeof(*array);
130 /* Allocate the callback structures behind the array. */
131 size += num_fences * sizeof(struct dma_fence_array_cb);
132 array = kzalloc(size, GFP_KERNEL);
136 spin_lock_init(&array->lock);
137 dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
139 init_irq_work(&array->work, irq_dma_fence_array_work);
141 array->num_fences = num_fences;
142 atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
143 array->fences = fences;
147 EXPORT_SYMBOL(dma_fence_array_create);
150 * dma_fence_match_context - Check if all fences are from the given context
151 * @fence: [in] fence or fence array
152 * @context: [in] fence context to check all fences against
154 * Checks the provided fence or, for a fence array, all fences in the array
155 * against the given context. Returns false if any fence is from a different
158 bool dma_fence_match_context(struct dma_fence *fence, u64 context)
160 struct dma_fence_array *array = to_dma_fence_array(fence);
163 if (!dma_fence_is_array(fence))
164 return fence->context == context;
166 for (i = 0; i < array->num_fences; i++) {
167 if (array->fences[i]->context != context)
173 EXPORT_SYMBOL(dma_fence_match_context);