0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/export.h>
0013 #include <linux/slab.h>
0014 #include <linux/dma-fence-array.h>
0015
0016 #define PENDING_ERROR 1
0017
0018 static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
0019 {
0020 return "dma_fence_array";
0021 }
0022
0023 static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
0024 {
0025 return "unbound";
0026 }
0027
0028 static void dma_fence_array_set_pending_error(struct dma_fence_array *array,
0029 int error)
0030 {
0031
0032
0033
0034
0035 if (error)
0036 cmpxchg(&array->base.error, PENDING_ERROR, error);
0037 }
0038
0039 static void dma_fence_array_clear_pending_error(struct dma_fence_array *array)
0040 {
0041
0042 cmpxchg(&array->base.error, PENDING_ERROR, 0);
0043 }
0044
0045 static void irq_dma_fence_array_work(struct irq_work *wrk)
0046 {
0047 struct dma_fence_array *array = container_of(wrk, typeof(*array), work);
0048
0049 dma_fence_array_clear_pending_error(array);
0050
0051 dma_fence_signal(&array->base);
0052 dma_fence_put(&array->base);
0053 }
0054
0055 static void dma_fence_array_cb_func(struct dma_fence *f,
0056 struct dma_fence_cb *cb)
0057 {
0058 struct dma_fence_array_cb *array_cb =
0059 container_of(cb, struct dma_fence_array_cb, cb);
0060 struct dma_fence_array *array = array_cb->array;
0061
0062 dma_fence_array_set_pending_error(array, f->error);
0063
0064 if (atomic_dec_and_test(&array->num_pending))
0065 irq_work_queue(&array->work);
0066 else
0067 dma_fence_put(&array->base);
0068 }
0069
0070 static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
0071 {
0072 struct dma_fence_array *array = to_dma_fence_array(fence);
0073 struct dma_fence_array_cb *cb = (void *)(&array[1]);
0074 unsigned i;
0075
0076 for (i = 0; i < array->num_fences; ++i) {
0077 cb[i].array = array;
0078
0079
0080
0081
0082
0083
0084
0085
0086 dma_fence_get(&array->base);
0087 if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
0088 dma_fence_array_cb_func)) {
0089 int error = array->fences[i]->error;
0090
0091 dma_fence_array_set_pending_error(array, error);
0092 dma_fence_put(&array->base);
0093 if (atomic_dec_and_test(&array->num_pending)) {
0094 dma_fence_array_clear_pending_error(array);
0095 return false;
0096 }
0097 }
0098 }
0099
0100 return true;
0101 }
0102
0103 static bool dma_fence_array_signaled(struct dma_fence *fence)
0104 {
0105 struct dma_fence_array *array = to_dma_fence_array(fence);
0106
0107 if (atomic_read(&array->num_pending) > 0)
0108 return false;
0109
0110 dma_fence_array_clear_pending_error(array);
0111 return true;
0112 }
0113
0114 static void dma_fence_array_release(struct dma_fence *fence)
0115 {
0116 struct dma_fence_array *array = to_dma_fence_array(fence);
0117 unsigned i;
0118
0119 for (i = 0; i < array->num_fences; ++i)
0120 dma_fence_put(array->fences[i]);
0121
0122 kfree(array->fences);
0123 dma_fence_free(fence);
0124 }
0125
0126 const struct dma_fence_ops dma_fence_array_ops = {
0127 .get_driver_name = dma_fence_array_get_driver_name,
0128 .get_timeline_name = dma_fence_array_get_timeline_name,
0129 .enable_signaling = dma_fence_array_enable_signaling,
0130 .signaled = dma_fence_array_signaled,
0131 .release = dma_fence_array_release,
0132 };
0133 EXPORT_SYMBOL(dma_fence_array_ops);
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 struct dma_fence_array *dma_fence_array_create(int num_fences,
0155 struct dma_fence **fences,
0156 u64 context, unsigned seqno,
0157 bool signal_on_any)
0158 {
0159 struct dma_fence_array *array;
0160 size_t size = sizeof(*array);
0161
0162 WARN_ON(!num_fences || !fences);
0163
0164
0165 size += num_fences * sizeof(struct dma_fence_array_cb);
0166 array = kzalloc(size, GFP_KERNEL);
0167 if (!array)
0168 return NULL;
0169
0170 spin_lock_init(&array->lock);
0171 dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
0172 context, seqno);
0173 init_irq_work(&array->work, irq_dma_fence_array_work);
0174
0175 array->num_fences = num_fences;
0176 atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
0177 array->fences = fences;
0178
0179 array->base.error = PENDING_ERROR;
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 while (num_fences--)
0193 WARN_ON(dma_fence_is_container(fences[num_fences]));
0194
0195 return array;
0196 }
0197 EXPORT_SYMBOL(dma_fence_array_create);
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208 bool dma_fence_match_context(struct dma_fence *fence, u64 context)
0209 {
0210 struct dma_fence_array *array = to_dma_fence_array(fence);
0211 unsigned i;
0212
0213 if (!dma_fence_is_array(fence))
0214 return fence->context == context;
0215
0216 for (i = 0; i < array->num_fences; i++) {
0217 if (array->fences[i]->context != context)
0218 return false;
0219 }
0220
0221 return true;
0222 }
0223 EXPORT_SYMBOL(dma_fence_match_context);
0224
0225 struct dma_fence *dma_fence_array_first(struct dma_fence *head)
0226 {
0227 struct dma_fence_array *array;
0228
0229 if (!head)
0230 return NULL;
0231
0232 array = to_dma_fence_array(head);
0233 if (!array)
0234 return head;
0235
0236 if (!array->num_fences)
0237 return NULL;
0238
0239 return array->fences[0];
0240 }
0241 EXPORT_SYMBOL(dma_fence_array_first);
0242
0243 struct dma_fence *dma_fence_array_next(struct dma_fence *head,
0244 unsigned int index)
0245 {
0246 struct dma_fence_array *array = to_dma_fence_array(head);
0247
0248 if (!array || index >= array->num_fences)
0249 return NULL;
0250
0251 return array->fences[index];
0252 }
0253 EXPORT_SYMBOL(dma_fence_array_next);