Merge remote-tracking branch 'airlied/drm-next' into topic/drm-misc
[linux-2.6-block.git] / drivers / dma-buf / fence.c
CommitLineData
e941759c
ML
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
e9f3b796 32/*
e941759c
ML
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 */
76bf0db5 38static atomic64_t fence_context_counter = ATOMIC64_INIT(0);
e941759c
ML
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 */
76bf0db5 47u64 fence_context_alloc(unsigned num)
e941759c
ML
48{
49 BUG_ON(!num);
76bf0db5 50 return atomic64_add_return(num, &fence_context_counter) - num;
e941759c
ML
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
78010cd9
RC
71 lockdep_assert_held(fence->lock);
72
e941759c
ML
73 if (WARN_ON(!fence))
74 return -EINVAL;
75
76 if (!ktime_to_ns(fence->timestamp)) {
77 fence->timestamp = ktime_get();
78 smp_mb__before_atomic();
79 }
80
81 if (test_and_set_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
82 ret = -EINVAL;
83
84 /*
85 * we might have raced with the unlocked fence_signal,
86 * still run through all callbacks
87 */
88 } else
89 trace_fence_signaled(fence);
90
91 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
92 list_del_init(&cur->node);
93 cur->func(fence, cur);
94 }
95 return ret;
96}
97EXPORT_SYMBOL(fence_signal_locked);
98
99/**
100 * fence_signal - signal completion of a fence
101 * @fence: the fence to signal
102 *
103 * Signal completion for software callbacks on a fence, this will unblock
104 * fence_wait() calls and run all the callbacks added with
105 * fence_add_callback(). Can be called multiple times, but since a fence
106 * can only go from unsignaled to signaled state, it will only be effective
107 * the first time.
108 */
109int fence_signal(struct fence *fence)
110{
111 unsigned long flags;
112
113 if (!fence)
114 return -EINVAL;
115
116 if (!ktime_to_ns(fence->timestamp)) {
117 fence->timestamp = ktime_get();
118 smp_mb__before_atomic();
119 }
120
121 if (test_and_set_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
122 return -EINVAL;
123
124 trace_fence_signaled(fence);
125
126 if (test_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
127 struct fence_cb *cur, *tmp;
128
129 spin_lock_irqsave(fence->lock, flags);
130 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
131 list_del_init(&cur->node);
132 cur->func(fence, cur);
133 }
134 spin_unlock_irqrestore(fence->lock, flags);
135 }
136 return 0;
137}
138EXPORT_SYMBOL(fence_signal);
139
140/**
141 * fence_wait_timeout - sleep until the fence gets signaled
142 * or until timeout elapses
143 * @fence: [in] the fence to wait on
144 * @intr: [in] if true, do an interruptible wait
145 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
146 *
147 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
148 * remaining timeout in jiffies on success. Other error values may be
149 * returned on custom implementations.
150 *
151 * Performs a synchronous wait on this fence. It is assumed the caller
152 * directly or indirectly (buf-mgr between reservation and committing)
153 * holds a reference to the fence, otherwise the fence might be
154 * freed before return, resulting in undefined behavior.
155 */
156signed long
157fence_wait_timeout(struct fence *fence, bool intr, signed long timeout)
158{
159 signed long ret;
160
161 if (WARN_ON(timeout < 0))
162 return -EINVAL;
163
847b19a3
JZ
164 if (timeout == 0)
165 return fence_is_signaled(fence);
166
e941759c
ML
167 trace_fence_wait_start(fence);
168 ret = fence->ops->wait(fence, intr, timeout);
169 trace_fence_wait_end(fence);
170 return ret;
171}
172EXPORT_SYMBOL(fence_wait_timeout);
173
174void fence_release(struct kref *kref)
175{
176 struct fence *fence =
177 container_of(kref, struct fence, refcount);
178
179 trace_fence_destroy(fence);
180
181 BUG_ON(!list_empty(&fence->cb_list));
182
183 if (fence->ops->release)
184 fence->ops->release(fence);
185 else
186 fence_free(fence);
187}
188EXPORT_SYMBOL(fence_release);
189
190void fence_free(struct fence *fence)
191{
3c3b177a 192 kfree_rcu(fence, rcu);
e941759c
ML
193}
194EXPORT_SYMBOL(fence_free);
195
196/**
197 * fence_enable_sw_signaling - enable signaling on fence
198 * @fence: [in] the fence to enable
199 *
200 * this will request for sw signaling to be enabled, to make the fence
201 * complete as soon as possible
202 */
203void fence_enable_sw_signaling(struct fence *fence)
204{
205 unsigned long flags;
206
207 if (!test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags) &&
208 !test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
209 trace_fence_enable_signal(fence);
210
211 spin_lock_irqsave(fence->lock, flags);
212
213 if (!fence->ops->enable_signaling(fence))
214 fence_signal_locked(fence);
215
216 spin_unlock_irqrestore(fence->lock, flags);
217 }
218}
219EXPORT_SYMBOL(fence_enable_sw_signaling);
220
221/**
222 * fence_add_callback - add a callback to be called when the fence
223 * is signaled
224 * @fence: [in] the fence to wait on
225 * @cb: [in] the callback to register
226 * @func: [in] the function to call
227 *
228 * cb will be initialized by fence_add_callback, no initialization
229 * by the caller is required. Any number of callbacks can be registered
230 * to a fence, but a callback can only be registered to one fence at a time.
231 *
232 * Note that the callback can be called from an atomic context. If
233 * fence is already signaled, this function will return -ENOENT (and
234 * *not* call the callback)
235 *
236 * Add a software callback to the fence. Same restrictions apply to
237 * refcount as it does to fence_wait, however the caller doesn't need to
238 * keep a refcount to fence afterwards: when software access is enabled,
239 * the creator of the fence is required to keep the fence alive until
240 * after it signals with fence_signal. The callback itself can be called
241 * from irq context.
242 *
243 */
244int fence_add_callback(struct fence *fence, struct fence_cb *cb,
245 fence_func_t func)
246{
247 unsigned long flags;
248 int ret = 0;
249 bool was_set;
250
251 if (WARN_ON(!fence || !func))
252 return -EINVAL;
253
254 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
255 INIT_LIST_HEAD(&cb->node);
256 return -ENOENT;
257 }
258
259 spin_lock_irqsave(fence->lock, flags);
260
261 was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
262
263 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
264 ret = -ENOENT;
265 else if (!was_set) {
266 trace_fence_enable_signal(fence);
267
268 if (!fence->ops->enable_signaling(fence)) {
269 fence_signal_locked(fence);
270 ret = -ENOENT;
271 }
272 }
273
274 if (!ret) {
275 cb->func = func;
276 list_add_tail(&cb->node, &fence->cb_list);
277 } else
278 INIT_LIST_HEAD(&cb->node);
279 spin_unlock_irqrestore(fence->lock, flags);
280
281 return ret;
282}
283EXPORT_SYMBOL(fence_add_callback);
284
285/**
286 * fence_remove_callback - remove a callback from the signaling list
287 * @fence: [in] the fence to wait on
288 * @cb: [in] the callback to remove
289 *
290 * Remove a previously queued callback from the fence. This function returns
f353d71f 291 * true if the callback is successfully removed, or false if the fence has
e941759c
ML
292 * already been signaled.
293 *
294 * *WARNING*:
295 * Cancelling a callback should only be done if you really know what you're
296 * doing, since deadlocks and race conditions could occur all too easily. For
297 * this reason, it should only ever be done on hardware lockup recovery,
298 * with a reference held to the fence.
299 */
300bool
301fence_remove_callback(struct fence *fence, struct fence_cb *cb)
302{
303 unsigned long flags;
304 bool ret;
305
306 spin_lock_irqsave(fence->lock, flags);
307
308 ret = !list_empty(&cb->node);
309 if (ret)
310 list_del_init(&cb->node);
311
312 spin_unlock_irqrestore(fence->lock, flags);
313
314 return ret;
315}
316EXPORT_SYMBOL(fence_remove_callback);
317
318struct default_wait_cb {
319 struct fence_cb base;
320 struct task_struct *task;
321};
322
323static void
324fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
325{
326 struct default_wait_cb *wait =
327 container_of(cb, struct default_wait_cb, base);
328
329 wake_up_state(wait->task, TASK_NORMAL);
330}
331
332/**
333 * fence_default_wait - default sleep until the fence gets signaled
334 * or until timeout elapses
335 * @fence: [in] the fence to wait on
336 * @intr: [in] if true, do an interruptible wait
337 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
338 *
339 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
340 * remaining timeout in jiffies on success.
341 */
342signed long
343fence_default_wait(struct fence *fence, bool intr, signed long timeout)
344{
345 struct default_wait_cb cb;
346 unsigned long flags;
347 signed long ret = timeout;
348 bool was_set;
349
350 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
351 return timeout;
352
353 spin_lock_irqsave(fence->lock, flags);
354
355 if (intr && signal_pending(current)) {
356 ret = -ERESTARTSYS;
357 goto out;
358 }
359
360 was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
361
362 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
363 goto out;
364
365 if (!was_set) {
366 trace_fence_enable_signal(fence);
367
368 if (!fence->ops->enable_signaling(fence)) {
369 fence_signal_locked(fence);
370 goto out;
371 }
372 }
373
374 cb.base.func = fence_default_wait_cb;
375 cb.task = current;
376 list_add(&cb.base.node, &fence->cb_list);
377
378 while (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
379 if (intr)
380 __set_current_state(TASK_INTERRUPTIBLE);
381 else
382 __set_current_state(TASK_UNINTERRUPTIBLE);
383 spin_unlock_irqrestore(fence->lock, flags);
384
385 ret = schedule_timeout(ret);
386
387 spin_lock_irqsave(fence->lock, flags);
388 if (ret > 0 && intr && signal_pending(current))
389 ret = -ERESTARTSYS;
390 }
391
392 if (!list_empty(&cb.base.node))
393 list_del(&cb.base.node);
394 __set_current_state(TASK_RUNNING);
395
396out:
397 spin_unlock_irqrestore(fence->lock, flags);
398 return ret;
399}
400EXPORT_SYMBOL(fence_default_wait);
401
a519435a
CK
402static bool
403fence_test_signaled_any(struct fence **fences, uint32_t count)
404{
405 int i;
406
407 for (i = 0; i < count; ++i) {
408 struct fence *fence = fences[i];
409 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
410 return true;
411 }
412 return false;
413}
414
415/**
416 * fence_wait_any_timeout - sleep until any fence gets signaled
417 * or until timeout elapses
418 * @fences: [in] array of fences to wait on
419 * @count: [in] number of fences to wait on
420 * @intr: [in] if true, do an interruptible wait
421 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
422 *
423 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
424 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
425 * on success.
426 *
427 * Synchronous waits for the first fence in the array to be signaled. The
428 * caller needs to hold a reference to all fences in the array, otherwise a
429 * fence might be freed before return, resulting in undefined behavior.
430 */
431signed long
432fence_wait_any_timeout(struct fence **fences, uint32_t count,
433 bool intr, signed long timeout)
434{
435 struct default_wait_cb *cb;
436 signed long ret = timeout;
437 unsigned i;
438
439 if (WARN_ON(!fences || !count || timeout < 0))
440 return -EINVAL;
441
442 if (timeout == 0) {
443 for (i = 0; i < count; ++i)
444 if (fence_is_signaled(fences[i]))
445 return 1;
446
447 return 0;
448 }
449
450 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
451 if (cb == NULL) {
452 ret = -ENOMEM;
453 goto err_free_cb;
454 }
455
456 for (i = 0; i < count; ++i) {
457 struct fence *fence = fences[i];
458
459 if (fence->ops->wait != fence_default_wait) {
460 ret = -EINVAL;
461 goto fence_rm_cb;
462 }
463
464 cb[i].task = current;
465 if (fence_add_callback(fence, &cb[i].base,
466 fence_default_wait_cb)) {
467 /* This fence is already signaled */
468 goto fence_rm_cb;
469 }
470 }
471
472 while (ret > 0) {
473 if (intr)
474 set_current_state(TASK_INTERRUPTIBLE);
475 else
476 set_current_state(TASK_UNINTERRUPTIBLE);
477
478 if (fence_test_signaled_any(fences, count))
479 break;
480
481 ret = schedule_timeout(ret);
482
483 if (ret > 0 && intr && signal_pending(current))
484 ret = -ERESTARTSYS;
485 }
486
487 __set_current_state(TASK_RUNNING);
488
489fence_rm_cb:
490 while (i-- > 0)
491 fence_remove_callback(fences[i], &cb[i].base);
492
493err_free_cb:
494 kfree(cb);
495
496 return ret;
497}
498EXPORT_SYMBOL(fence_wait_any_timeout);
499
e941759c
ML
500/**
501 * fence_init - Initialize a custom fence.
502 * @fence: [in] the fence to initialize
503 * @ops: [in] the fence_ops for operations on this fence
504 * @lock: [in] the irqsafe spinlock to use for locking this fence
505 * @context: [in] the execution context this fence is run on
506 * @seqno: [in] a linear increasing sequence number for this context
507 *
508 * Initializes an allocated fence, the caller doesn't have to keep its
509 * refcount after committing with this fence, but it will need to hold a
510 * refcount again if fence_ops.enable_signaling gets called. This can
511 * be used for other implementing other types of fence.
512 *
513 * context and seqno are used for easy comparison between fences, allowing
514 * to check which fence is later by simply using fence_later.
515 */
516void
517fence_init(struct fence *fence, const struct fence_ops *ops,
76bf0db5 518 spinlock_t *lock, u64 context, unsigned seqno)
e941759c
ML
519{
520 BUG_ON(!lock);
521 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
522 !ops->get_driver_name || !ops->get_timeline_name);
523
524 kref_init(&fence->refcount);
525 fence->ops = ops;
526 INIT_LIST_HEAD(&fence->cb_list);
527 fence->lock = lock;
528 fence->context = context;
529 fence->seqno = seqno;
530 fence->flags = 0UL;
531
532 trace_fence_init(fence);
533}
534EXPORT_SYMBOL(fence_init);