]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/dma-buf/dma-fence-array.c
UBUNTU: SAUCE: media: uvcvideo: Support realtek's UVC 1.5 device
[mirror_ubuntu-artful-kernel.git] / drivers / dma-buf / dma-fence-array.c
CommitLineData
b3dfbdf2 1/*
f54d1867 2 * dma-fence-array: aggregate fences to be waited together
b3dfbdf2
GP
3 *
4 * Copyright (C) 2016 Collabora Ltd
5 * Copyright (C) 2016 Advanced Micro Devices, Inc.
6 * Authors:
7 * Gustavo Padovan <gustavo@padovan.org>
8 * Christian König <christian.koenig@amd.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 */
19
20#include <linux/export.h>
21#include <linux/slab.h>
f54d1867 22#include <linux/dma-fence-array.h>
b3dfbdf2 23
f54d1867 24static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
b3dfbdf2 25{
f54d1867 26 return "dma_fence_array";
b3dfbdf2
GP
27}
28
f54d1867 29static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
b3dfbdf2
GP
30{
31 return "unbound";
32}
33
f54d1867
CW
34static void dma_fence_array_cb_func(struct dma_fence *f,
35 struct dma_fence_cb *cb)
b3dfbdf2 36{
f54d1867
CW
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;
b3dfbdf2
GP
40
41 if (atomic_dec_and_test(&array->num_pending))
f54d1867
CW
42 dma_fence_signal(&array->base);
43 dma_fence_put(&array->base);
b3dfbdf2
GP
44}
45
f54d1867 46static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
b3dfbdf2 47{
f54d1867
CW
48 struct dma_fence_array *array = to_dma_fence_array(fence);
49 struct dma_fence_array_cb *cb = (void *)(&array[1]);
b3dfbdf2
GP
50 unsigned i;
51
52 for (i = 0; i < array->num_fences; ++i) {
53 cb[i].array = array;
f7104568
CK
54 /*
55 * As we may report that the fence is signaled before all
56 * callbacks are complete, we need to take an additional
57 * reference count on the array so that we do not free it too
58 * early. The core fence handling will only hold the reference
59 * until we signal the array as complete (but that is now
60 * insufficient).
61 */
f54d1867
CW
62 dma_fence_get(&array->base);
63 if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
64 dma_fence_array_cb_func)) {
65 dma_fence_put(&array->base);
b3dfbdf2
GP
66 if (atomic_dec_and_test(&array->num_pending))
67 return false;
f7104568 68 }
b3dfbdf2
GP
69 }
70
71 return true;
72}
73
f54d1867 74static bool dma_fence_array_signaled(struct dma_fence *fence)
b3dfbdf2 75{
f54d1867 76 struct dma_fence_array *array = to_dma_fence_array(fence);
b3dfbdf2 77
f7104568 78 return atomic_read(&array->num_pending) <= 0;
b3dfbdf2
GP
79}
80
f54d1867 81static void dma_fence_array_release(struct dma_fence *fence)
b3dfbdf2 82{
f54d1867 83 struct dma_fence_array *array = to_dma_fence_array(fence);
b3dfbdf2
GP
84 unsigned i;
85
86 for (i = 0; i < array->num_fences; ++i)
f54d1867 87 dma_fence_put(array->fences[i]);
b3dfbdf2
GP
88
89 kfree(array->fences);
f54d1867 90 dma_fence_free(fence);
b3dfbdf2
GP
91}
92
f54d1867
CW
93const struct dma_fence_ops dma_fence_array_ops = {
94 .get_driver_name = dma_fence_array_get_driver_name,
95 .get_timeline_name = dma_fence_array_get_timeline_name,
96 .enable_signaling = dma_fence_array_enable_signaling,
97 .signaled = dma_fence_array_signaled,
98 .wait = dma_fence_default_wait,
99 .release = dma_fence_array_release,
b3dfbdf2 100};
f54d1867 101EXPORT_SYMBOL(dma_fence_array_ops);
b3dfbdf2
GP
102
103/**
f54d1867 104 * dma_fence_array_create - Create a custom fence array
f7104568
CK
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
68acb6af 109 * @signal_on_any: [in] signal on any fence in the array
b3dfbdf2 110 *
f54d1867
CW
111 * Allocate a dma_fence_array object and initialize the base fence with
112 * dma_fence_init().
b3dfbdf2
GP
113 * In case of error it returns NULL.
114 *
68acb6af 115 * The caller should allocate the fences array with num_fences size
b3dfbdf2 116 * and fill it with the fences it wants to add to the object. Ownership of this
f54d1867 117 * array is taken and dma_fence_put() is used on each fence on release.
f7104568
CK
118 *
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.
b3dfbdf2 121 */
f54d1867
CW
122struct dma_fence_array *dma_fence_array_create(int num_fences,
123 struct dma_fence **fences,
124 u64 context, unsigned seqno,
125 bool signal_on_any)
b3dfbdf2 126{
f54d1867 127 struct dma_fence_array *array;
b3dfbdf2
GP
128 size_t size = sizeof(*array);
129
130 /* Allocate the callback structures behind the array. */
f54d1867 131 size += num_fences * sizeof(struct dma_fence_array_cb);
b3dfbdf2
GP
132 array = kzalloc(size, GFP_KERNEL);
133 if (!array)
134 return NULL;
135
136 spin_lock_init(&array->lock);
f54d1867
CW
137 dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
138 context, seqno);
b3dfbdf2
GP
139
140 array->num_fences = num_fences;
f7104568 141 atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
b3dfbdf2
GP
142 array->fences = fences;
143
144 return array;
145}
f54d1867 146EXPORT_SYMBOL(dma_fence_array_create);
d5b72a21
PZ
147
148/**
149 * dma_fence_match_context - Check if all fences are from the given context
150 * @fence: [in] fence or fence array
151 * @context: [in] fence context to check all fences against
152 *
153 * Checks the provided fence or, for a fence array, all fences in the array
154 * against the given context. Returns false if any fence is from a different
155 * context.
156 */
157bool dma_fence_match_context(struct dma_fence *fence, u64 context)
158{
159 struct dma_fence_array *array = to_dma_fence_array(fence);
160 unsigned i;
161
162 if (!dma_fence_is_array(fence))
163 return fence->context == context;
164
165 for (i = 0; i < array->num_fences; i++) {
166 if (array->fences[i]->context != context)
167 return false;
168 }
169
170 return true;
171}
172EXPORT_SYMBOL(dma_fence_match_context);