aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/dma-buf
diff options
context:
space:
mode:
authorMaarten Lankhorst <maarten.lankhorst@canonical.com>2014-07-01 06:57:14 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-07-08 15:18:56 -0400
commite941759c74a44d6ac2eed21bb0a38b21fe4559e2 (patch)
tree717acd3b5d198f22a04a61c97a7b806785ba3166 /drivers/dma-buf
parent35fac7e305dc5e0bd1e52f2505944674337de57c (diff)
fence: dma-buf cross-device synchronization (v18)
A fence can be attached to a buffer which is being filled or consumed by hw, to allow userspace to pass the buffer without waiting to another device. For example, userspace can call page_flip ioctl to display the next frame of graphics after kicking the GPU but while the GPU is still rendering. The display device sharing the buffer with the GPU would attach a callback to get notified when the GPU's rendering-complete IRQ fires, to update the scan-out address of the display, without having to wake up userspace. A driver must allocate a fence context for each execution ring that can run in parallel. The function for this takes an argument with how many contexts to allocate: + fence_context_alloc() A fence is transient, one-shot deal. It is allocated and attached to one or more dma-buf's. When the one that attached it is done, with the pending operation, it can signal the fence: + fence_signal() To have a rough approximation whether a fence is fired, call: + fence_is_signaled() The dma-buf-mgr handles tracking, and waiting on, the fences associated with a dma-buf. The one pending on the fence can add an async callback: + fence_add_callback() The callback can optionally be cancelled with: + fence_remove_callback() To wait synchronously, optionally with a timeout: + fence_wait() + fence_wait_timeout() When emitting a fence, call: + trace_fence_emit() To annotate that a fence is blocking on another fence, call: + trace_fence_annotate_wait_on(fence, on_fence) A default software-only implementation is provided, which can be used by drivers attaching a fence to a buffer when they have no other means for hw sync. But a memory backed fence is also envisioned, because it is common that GPU's can write to, or poll on some memory location for synchronization. For example: fence = custom_get_fence(...); if ((seqno_fence = to_seqno_fence(fence)) != NULL) { dma_buf *fence_buf = seqno_fence->sync_buf; get_dma_buf(fence_buf); ... tell the hw the memory location to wait ... custom_wait_on(fence_buf, seqno_fence->seqno_ofs, fence->seqno); } else { /* fall-back to sw sync * / fence_add_callback(fence, my_cb); } On SoC platforms, if some other hw mechanism is provided for synchronizing between IP blocks, it could be supported as an alternate implementation with it's own fence ops in a similar way. enable_signaling callback is used to provide sw signaling in case a cpu waiter is requested or no compatible hardware signaling could be used. The intention is to provide a userspace interface (presumably via eventfd) later, to be used in conjunction with dma-buf's mmap support for sw access to buffers (or for userspace apps that would prefer to do their own synchronization). v1: Original v2: After discussion w/ danvet and mlankhorst on #dri-devel, we decided that dma-fence didn't need to care about the sw->hw signaling path (it can be handled same as sw->sw case), and therefore the fence->ops can be simplified and more handled in the core. So remove the signal, add_callback, cancel_callback, and wait ops, and replace with a simple enable_signaling() op which can be used to inform a fence supporting hw->hw signaling that one or more devices which do not support hw signaling are waiting (and therefore it should enable an irq or do whatever is necessary in order that the CPU is notified when the fence is passed). v3: Fix locking fail in attach_fence() and get_fence() v4: Remove tie-in w/ dma-buf.. after discussion w/ danvet and mlankorst we decided that we need to be able to attach one fence to N dma-buf's, so using the list_head in dma-fence struct would be problematic. v5: [ Maarten Lankhorst ] Updated for dma-bikeshed-fence and dma-buf-manager. v6: [ Maarten Lankhorst ] I removed dma_fence_cancel_callback and some comments about checking if fence fired or not. This is broken by design. waitqueue_active during destruction is now fatal, since the signaller should be holding a reference in enable_signalling until it signalled the fence. Pass the original dma_fence_cb along, and call __remove_wait in the dma_fence_callback handler, so that no cleanup needs to be performed. v7: [ Maarten Lankhorst ] Set cb->func and only enable sw signaling if fence wasn't signaled yet, for example for hardware fences that may choose to signal blindly. v8: [ Maarten Lankhorst ] Tons of tiny fixes, moved __dma_fence_init to header and fixed include mess. dma-fence.h now includes dma-buf.h All members are now initialized, so kmalloc can be used for allocating a dma-fence. More documentation added. v9: Change compiler bitfields to flags, change return type of enable_signaling to bool. Rework dma_fence_wait. Added dma_fence_is_signaled and dma_fence_wait_timeout. s/dma// and change exports to non GPL. Added fence_is_signaled and fence_enable_sw_signaling calls, add ability to override default wait operation. v10: remove event_queue, use a custom list, export try_to_wake_up from scheduler. Remove fence lock and use a global spinlock instead, this should hopefully remove all the locking headaches I was having on trying to implement this. enable_signaling is called with this lock held. v11: Use atomic ops for flags, lifting the need for some spin_lock_irqsaves. However I kept the guarantee that after fence_signal returns, it is guaranteed that enable_signaling has either been called to completion, or will not be called any more. Add contexts and seqno to base fence implementation. This allows you to wait for less fences, by testing for seqno + signaled, and then only wait on the later fence. Add FENCE_TRACE, FENCE_WARN, and FENCE_ERR. This makes debugging easier. An CONFIG_DEBUG_FENCE will be added to turn off the FENCE_TRACE spam, and another runtime option can turn it off at runtime. v12: Add CONFIG_FENCE_TRACE. Add missing documentation for the fence->context and fence->seqno members. v13: Fixup CONFIG_FENCE_TRACE kconfig description. Move fence_context_alloc to fence. Simplify fence_later. Kill priv member to fence_cb. v14: Remove priv argument from fence_add_callback, oops! v15: Remove priv from documentation. Explicitly include linux/atomic.h. v16: Add trace events. Import changes required by android syncpoints. v17: Use wake_up_state instead of try_to_wake_up. (Colin Cross) Fix up commit description for seqno_fence. (Rob Clark) v18: Rename release_fence to fence_release. Move to drivers/dma-buf/. Rename __fence_is_signaled and __fence_signal to *_locked. Rename __fence_init to fence_init. Make fence_default_wait return a signed long, and fix wait ops too. Signed-off-by: Maarten Lankhorst <maarten.lankhorst@canonical.com> Signed-off-by: Thierry Reding <thierry.reding@gmail.com> #use smp_mb__before_atomic() Acked-by: Sumit Semwal <sumit.semwal@linaro.org> Acked-by: Daniel Vetter <daniel@ffwll.ch> Reviewed-by: Rob Clark <robdclark@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/dma-buf')
-rw-r--r--drivers/dma-buf/Makefile2
-rw-r--r--drivers/dma-buf/fence.c431
2 files changed, 432 insertions, 1 deletions
diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
index 4a4f4c9bacd0..d7825bfe630e 100644
--- a/drivers/dma-buf/Makefile
+++ b/drivers/dma-buf/Makefile
@@ -1 +1 @@
obj-y := dma-buf.o reservation.o obj-y := dma-buf.o fence.o reservation.o
diff --git a/drivers/dma-buf/fence.c b/drivers/dma-buf/fence.c
new file mode 100644
index 000000000000..948bf00d955e
--- /dev/null
+++ b/drivers/dma-buf/fence.c
@@ -0,0 +1,431 @@
1/*
2 * Fence mechanism for dma-buf and to allow for asynchronous dma access
3 *
4 * Copyright (C) 2012 Canonical Ltd
5 * Copyright (C) 2012 Texas Instruments
6 *
7 * Authors:
8 * Rob Clark <robdclark@gmail.com>
9 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published by
13 * the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 */
20
21#include <linux/slab.h>
22#include <linux/export.h>
23#include <linux/atomic.h>
24#include <linux/fence.h>
25
26#define CREATE_TRACE_POINTS
27#include <trace/events/fence.h>
28
29EXPORT_TRACEPOINT_SYMBOL(fence_annotate_wait_on);
30EXPORT_TRACEPOINT_SYMBOL(fence_emit);
31
32/**
33 * fence context counter: each execution context should have its own
34 * fence context, this allows checking if fences belong to the same
35 * context or not. One device can have multiple separate contexts,
36 * and they're used if some engine can run independently of another.
37 */
38static atomic_t fence_context_counter = ATOMIC_INIT(0);
39
40/**
41 * fence_context_alloc - allocate an array of fence contexts
42 * @num: [in] amount of contexts to allocate
43 *
44 * This function will return the first index of the number of fences allocated.
45 * The fence context is used for setting fence->context to a unique number.
46 */
47unsigned fence_context_alloc(unsigned num)
48{
49 BUG_ON(!num);
50 return atomic_add_return(num, &fence_context_counter) - num;
51}
52EXPORT_SYMBOL(fence_context_alloc);
53
54/**
55 * fence_signal_locked - signal completion of a fence
56 * @fence: the fence to signal
57 *
58 * Signal completion for software callbacks on a fence, this will unblock
59 * fence_wait() calls and run all the callbacks added with
60 * fence_add_callback(). Can be called multiple times, but since a fence
61 * can only go from unsignaled to signaled state, it will only be effective
62 * the first time.
63 *
64 * Unlike fence_signal, this function must be called with fence->lock held.
65 */
66int fence_signal_locked(struct fence *fence)
67{
68 struct fence_cb *cur, *tmp;
69 int ret = 0;
70
71 if (WARN_ON(!fence))
72 return -EINVAL;
73
74 if (!ktime_to_ns(fence->timestamp)) {
75 fence->timestamp = ktime_get();
76 smp_mb__before_atomic();
77 }
78
79 if (test_and_set_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
80 ret = -EINVAL;
81
82 /*
83 * we might have raced with the unlocked fence_signal,
84 * still run through all callbacks
85 */
86 } else
87 trace_fence_signaled(fence);
88
89 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
90 list_del_init(&cur->node);
91 cur->func(fence, cur);
92 }
93 return ret;
94}
95EXPORT_SYMBOL(fence_signal_locked);
96
97/**
98 * fence_signal - signal completion of a fence
99 * @fence: the fence to signal
100 *
101 * Signal completion for software callbacks on a fence, this will unblock
102 * fence_wait() calls and run all the callbacks added with
103 * fence_add_callback(). Can be called multiple times, but since a fence
104 * can only go from unsignaled to signaled state, it will only be effective
105 * the first time.
106 */
107int fence_signal(struct fence *fence)
108{
109 unsigned long flags;
110
111 if (!fence)
112 return -EINVAL;
113
114 if (!ktime_to_ns(fence->timestamp)) {
115 fence->timestamp = ktime_get();
116 smp_mb__before_atomic();
117 }
118
119 if (test_and_set_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
120 return -EINVAL;
121
122 trace_fence_signaled(fence);
123
124 if (test_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
125 struct fence_cb *cur, *tmp;
126
127 spin_lock_irqsave(fence->lock, flags);
128 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
129 list_del_init(&cur->node);
130 cur->func(fence, cur);
131 }
132 spin_unlock_irqrestore(fence->lock, flags);
133 }
134 return 0;
135}
136EXPORT_SYMBOL(fence_signal);
137
138/**
139 * fence_wait_timeout - sleep until the fence gets signaled
140 * or until timeout elapses
141 * @fence: [in] the fence to wait on
142 * @intr: [in] if true, do an interruptible wait
143 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
144 *
145 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
146 * remaining timeout in jiffies on success. Other error values may be
147 * returned on custom implementations.
148 *
149 * Performs a synchronous wait on this fence. It is assumed the caller
150 * directly or indirectly (buf-mgr between reservation and committing)
151 * holds a reference to the fence, otherwise the fence might be
152 * freed before return, resulting in undefined behavior.
153 */
154signed long
155fence_wait_timeout(struct fence *fence, bool intr, signed long timeout)
156{
157 signed long ret;
158
159 if (WARN_ON(timeout < 0))
160 return -EINVAL;
161
162 trace_fence_wait_start(fence);
163 ret = fence->ops->wait(fence, intr, timeout);
164 trace_fence_wait_end(fence);
165 return ret;
166}
167EXPORT_SYMBOL(fence_wait_timeout);
168
169void fence_release(struct kref *kref)
170{
171 struct fence *fence =
172 container_of(kref, struct fence, refcount);
173
174 trace_fence_destroy(fence);
175
176 BUG_ON(!list_empty(&fence->cb_list));
177
178 if (fence->ops->release)
179 fence->ops->release(fence);
180 else
181 fence_free(fence);
182}
183EXPORT_SYMBOL(fence_release);
184
185void fence_free(struct fence *fence)
186{
187 kfree(fence);
188}
189EXPORT_SYMBOL(fence_free);
190
191/**
192 * fence_enable_sw_signaling - enable signaling on fence
193 * @fence: [in] the fence to enable
194 *
195 * this will request for sw signaling to be enabled, to make the fence
196 * complete as soon as possible
197 */
198void fence_enable_sw_signaling(struct fence *fence)
199{
200 unsigned long flags;
201
202 if (!test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags) &&
203 !test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
204 trace_fence_enable_signal(fence);
205
206 spin_lock_irqsave(fence->lock, flags);
207
208 if (!fence->ops->enable_signaling(fence))
209 fence_signal_locked(fence);
210
211 spin_unlock_irqrestore(fence->lock, flags);
212 }
213}
214EXPORT_SYMBOL(fence_enable_sw_signaling);
215
216/**
217 * fence_add_callback - add a callback to be called when the fence
218 * is signaled
219 * @fence: [in] the fence to wait on
220 * @cb: [in] the callback to register
221 * @func: [in] the function to call
222 *
223 * cb will be initialized by fence_add_callback, no initialization
224 * by the caller is required. Any number of callbacks can be registered
225 * to a fence, but a callback can only be registered to one fence at a time.
226 *
227 * Note that the callback can be called from an atomic context. If
228 * fence is already signaled, this function will return -ENOENT (and
229 * *not* call the callback)
230 *
231 * Add a software callback to the fence. Same restrictions apply to
232 * refcount as it does to fence_wait, however the caller doesn't need to
233 * keep a refcount to fence afterwards: when software access is enabled,
234 * the creator of the fence is required to keep the fence alive until
235 * after it signals with fence_signal. The callback itself can be called
236 * from irq context.
237 *
238 */
239int fence_add_callback(struct fence *fence, struct fence_cb *cb,
240 fence_func_t func)
241{
242 unsigned long flags;
243 int ret = 0;
244 bool was_set;
245
246 if (WARN_ON(!fence || !func))
247 return -EINVAL;
248
249 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
250 INIT_LIST_HEAD(&cb->node);
251 return -ENOENT;
252 }
253
254 spin_lock_irqsave(fence->lock, flags);
255
256 was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
257
258 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
259 ret = -ENOENT;
260 else if (!was_set) {
261 trace_fence_enable_signal(fence);
262
263 if (!fence->ops->enable_signaling(fence)) {
264 fence_signal_locked(fence);
265 ret = -ENOENT;
266 }
267 }
268
269 if (!ret) {
270 cb->func = func;
271 list_add_tail(&cb->node, &fence->cb_list);
272 } else
273 INIT_LIST_HEAD(&cb->node);
274 spin_unlock_irqrestore(fence->lock, flags);
275
276 return ret;
277}
278EXPORT_SYMBOL(fence_add_callback);
279
280/**
281 * fence_remove_callback - remove a callback from the signaling list
282 * @fence: [in] the fence to wait on
283 * @cb: [in] the callback to remove
284 *
285 * Remove a previously queued callback from the fence. This function returns
286 * true if the callback is succesfully removed, or false if the fence has
287 * already been signaled.
288 *
289 * *WARNING*:
290 * Cancelling a callback should only be done if you really know what you're
291 * doing, since deadlocks and race conditions could occur all too easily. For
292 * this reason, it should only ever be done on hardware lockup recovery,
293 * with a reference held to the fence.
294 */
295bool
296fence_remove_callback(struct fence *fence, struct fence_cb *cb)
297{
298 unsigned long flags;
299 bool ret;
300
301 spin_lock_irqsave(fence->lock, flags);
302
303 ret = !list_empty(&cb->node);
304 if (ret)
305 list_del_init(&cb->node);
306
307 spin_unlock_irqrestore(fence->lock, flags);
308
309 return ret;
310}
311EXPORT_SYMBOL(fence_remove_callback);
312
313struct default_wait_cb {
314 struct fence_cb base;
315 struct task_struct *task;
316};
317
318static void
319fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
320{
321 struct default_wait_cb *wait =
322 container_of(cb, struct default_wait_cb, base);
323
324 wake_up_state(wait->task, TASK_NORMAL);
325}
326
327/**
328 * fence_default_wait - default sleep until the fence gets signaled
329 * or until timeout elapses
330 * @fence: [in] the fence to wait on
331 * @intr: [in] if true, do an interruptible wait
332 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
333 *
334 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
335 * remaining timeout in jiffies on success.
336 */
337signed long
338fence_default_wait(struct fence *fence, bool intr, signed long timeout)
339{
340 struct default_wait_cb cb;
341 unsigned long flags;
342 signed long ret = timeout;
343 bool was_set;
344
345 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
346 return timeout;
347
348 spin_lock_irqsave(fence->lock, flags);
349
350 if (intr && signal_pending(current)) {
351 ret = -ERESTARTSYS;
352 goto out;
353 }
354
355 was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
356
357 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
358 goto out;
359
360 if (!was_set) {
361 trace_fence_enable_signal(fence);
362
363 if (!fence->ops->enable_signaling(fence)) {
364 fence_signal_locked(fence);
365 goto out;
366 }
367 }
368
369 cb.base.func = fence_default_wait_cb;
370 cb.task = current;
371 list_add(&cb.base.node, &fence->cb_list);
372
373 while (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
374 if (intr)
375 __set_current_state(TASK_INTERRUPTIBLE);
376 else
377 __set_current_state(TASK_UNINTERRUPTIBLE);
378 spin_unlock_irqrestore(fence->lock, flags);
379
380 ret = schedule_timeout(ret);
381
382 spin_lock_irqsave(fence->lock, flags);
383 if (ret > 0 && intr && signal_pending(current))
384 ret = -ERESTARTSYS;
385 }
386
387 if (!list_empty(&cb.base.node))
388 list_del(&cb.base.node);
389 __set_current_state(TASK_RUNNING);
390
391out:
392 spin_unlock_irqrestore(fence->lock, flags);
393 return ret;
394}
395EXPORT_SYMBOL(fence_default_wait);
396
397/**
398 * fence_init - Initialize a custom fence.
399 * @fence: [in] the fence to initialize
400 * @ops: [in] the fence_ops for operations on this fence
401 * @lock: [in] the irqsafe spinlock to use for locking this fence
402 * @context: [in] the execution context this fence is run on
403 * @seqno: [in] a linear increasing sequence number for this context
404 *
405 * Initializes an allocated fence, the caller doesn't have to keep its
406 * refcount after committing with this fence, but it will need to hold a
407 * refcount again if fence_ops.enable_signaling gets called. This can
408 * be used for other implementing other types of fence.
409 *
410 * context and seqno are used for easy comparison between fences, allowing
411 * to check which fence is later by simply using fence_later.
412 */
413void
414fence_init(struct fence *fence, const struct fence_ops *ops,
415 spinlock_t *lock, unsigned context, unsigned seqno)
416{
417 BUG_ON(!lock);
418 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
419 !ops->get_driver_name || !ops->get_timeline_name);
420
421 kref_init(&fence->refcount);
422 fence->ops = ops;
423 INIT_LIST_HEAD(&fence->cb_list);
424 fence->lock = lock;
425 fence->context = context;
426 fence->seqno = seqno;
427 fence->flags = 0UL;
428
429 trace_fence_init(fence);
430}
431EXPORT_SYMBOL(fence_init);