blob: 094548bcb9cf6fa8bffe616e3c2e3f88cf72e4ee [file] [log] [blame]
Maarten Lankhorste9417592014-07-01 12:57:14 +02001/*
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
Thierry Redinge9f3b792014-08-08 12:42:32 +020032/*
Maarten Lankhorste9417592014-07-01 12:57:14 +020033 * 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 */
Christian König76bf0db2016-06-01 15:10:02 +020038static atomic64_t fence_context_counter = ATOMIC64_INIT(0);
Maarten Lankhorste9417592014-07-01 12:57:14 +020039
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 */
Christian König76bf0db2016-06-01 15:10:02 +020047u64 fence_context_alloc(unsigned num)
Maarten Lankhorste9417592014-07-01 12:57:14 +020048{
49 BUG_ON(!num);
Christian König76bf0db2016-06-01 15:10:02 +020050 return atomic64_add_return(num, &fence_context_counter) - num;
Maarten Lankhorste9417592014-07-01 12:57:14 +020051}
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
Rob Clarkce2a4b62016-10-24 15:57:10 -040071 lockdep_assert_held(fence->lock);
72
Maarten Lankhorste9417592014-07-01 12:57:14 +020073 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
164 trace_fence_wait_start(fence);
165 ret = fence->ops->wait(fence, intr, timeout);
166 trace_fence_wait_end(fence);
167 return ret;
168}
169EXPORT_SYMBOL(fence_wait_timeout);
170
171void fence_release(struct kref *kref)
172{
173 struct fence *fence =
174 container_of(kref, struct fence, refcount);
175
176 trace_fence_destroy(fence);
177
178 BUG_ON(!list_empty(&fence->cb_list));
179
180 if (fence->ops->release)
181 fence->ops->release(fence);
182 else
183 fence_free(fence);
184}
185EXPORT_SYMBOL(fence_release);
186
187void fence_free(struct fence *fence)
188{
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200189 kfree_rcu(fence, rcu);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200190}
191EXPORT_SYMBOL(fence_free);
192
193/**
194 * fence_enable_sw_signaling - enable signaling on fence
195 * @fence: [in] the fence to enable
196 *
197 * this will request for sw signaling to be enabled, to make the fence
198 * complete as soon as possible
199 */
200void fence_enable_sw_signaling(struct fence *fence)
201{
202 unsigned long flags;
203
204 if (!test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags) &&
205 !test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
206 trace_fence_enable_signal(fence);
207
208 spin_lock_irqsave(fence->lock, flags);
209
210 if (!fence->ops->enable_signaling(fence))
211 fence_signal_locked(fence);
212
213 spin_unlock_irqrestore(fence->lock, flags);
214 }
215}
216EXPORT_SYMBOL(fence_enable_sw_signaling);
217
218/**
219 * fence_add_callback - add a callback to be called when the fence
220 * is signaled
221 * @fence: [in] the fence to wait on
222 * @cb: [in] the callback to register
223 * @func: [in] the function to call
224 *
225 * cb will be initialized by fence_add_callback, no initialization
226 * by the caller is required. Any number of callbacks can be registered
227 * to a fence, but a callback can only be registered to one fence at a time.
228 *
229 * Note that the callback can be called from an atomic context. If
230 * fence is already signaled, this function will return -ENOENT (and
231 * *not* call the callback)
232 *
233 * Add a software callback to the fence. Same restrictions apply to
234 * refcount as it does to fence_wait, however the caller doesn't need to
235 * keep a refcount to fence afterwards: when software access is enabled,
236 * the creator of the fence is required to keep the fence alive until
237 * after it signals with fence_signal. The callback itself can be called
238 * from irq context.
239 *
240 */
241int fence_add_callback(struct fence *fence, struct fence_cb *cb,
242 fence_func_t func)
243{
244 unsigned long flags;
245 int ret = 0;
246 bool was_set;
247
248 if (WARN_ON(!fence || !func))
249 return -EINVAL;
250
251 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
252 INIT_LIST_HEAD(&cb->node);
253 return -ENOENT;
254 }
255
256 spin_lock_irqsave(fence->lock, flags);
257
258 was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
259
260 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
261 ret = -ENOENT;
262 else if (!was_set) {
263 trace_fence_enable_signal(fence);
264
265 if (!fence->ops->enable_signaling(fence)) {
266 fence_signal_locked(fence);
267 ret = -ENOENT;
268 }
269 }
270
271 if (!ret) {
272 cb->func = func;
273 list_add_tail(&cb->node, &fence->cb_list);
274 } else
275 INIT_LIST_HEAD(&cb->node);
276 spin_unlock_irqrestore(fence->lock, flags);
277
278 return ret;
279}
280EXPORT_SYMBOL(fence_add_callback);
281
282/**
283 * fence_remove_callback - remove a callback from the signaling list
284 * @fence: [in] the fence to wait on
285 * @cb: [in] the callback to remove
286 *
287 * Remove a previously queued callback from the fence. This function returns
Masanari Iidaf353d712014-10-22 00:00:14 +0900288 * true if the callback is successfully removed, or false if the fence has
Maarten Lankhorste9417592014-07-01 12:57:14 +0200289 * already been signaled.
290 *
291 * *WARNING*:
292 * Cancelling a callback should only be done if you really know what you're
293 * doing, since deadlocks and race conditions could occur all too easily. For
294 * this reason, it should only ever be done on hardware lockup recovery,
295 * with a reference held to the fence.
296 */
297bool
298fence_remove_callback(struct fence *fence, struct fence_cb *cb)
299{
300 unsigned long flags;
301 bool ret;
302
303 spin_lock_irqsave(fence->lock, flags);
304
305 ret = !list_empty(&cb->node);
Dmitry Torokhov2a061a82015-09-08 17:30:52 -0700306 if (ret) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200307 list_del_init(&cb->node);
Dmitry Torokhov2a061a82015-09-08 17:30:52 -0700308 if (list_empty(&fence->cb_list))
309 if (fence->ops->disable_signaling)
310 fence->ops->disable_signaling(fence);
311 }
Maarten Lankhorste9417592014-07-01 12:57:14 +0200312
313 spin_unlock_irqrestore(fence->lock, flags);
314
315 return ret;
316}
317EXPORT_SYMBOL(fence_remove_callback);
318
319struct default_wait_cb {
320 struct fence_cb base;
321 struct task_struct *task;
322};
323
324static void
325fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
326{
327 struct default_wait_cb *wait =
328 container_of(cb, struct default_wait_cb, base);
329
330 wake_up_state(wait->task, TASK_NORMAL);
331}
332
333/**
334 * fence_default_wait - default sleep until the fence gets signaled
335 * or until timeout elapses
336 * @fence: [in] the fence to wait on
337 * @intr: [in] if true, do an interruptible wait
338 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
339 *
340 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
341 * remaining timeout in jiffies on success.
342 */
343signed long
344fence_default_wait(struct fence *fence, bool intr, signed long timeout)
345{
346 struct default_wait_cb cb;
347 unsigned long flags;
348 signed long ret = timeout;
349 bool was_set;
350
351 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
352 return timeout;
353
354 spin_lock_irqsave(fence->lock, flags);
355
356 if (intr && signal_pending(current)) {
357 ret = -ERESTARTSYS;
358 goto out;
359 }
360
361 was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
362
363 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
364 goto out;
365
366 if (!was_set) {
367 trace_fence_enable_signal(fence);
368
369 if (!fence->ops->enable_signaling(fence)) {
370 fence_signal_locked(fence);
371 goto out;
372 }
373 }
374
375 cb.base.func = fence_default_wait_cb;
376 cb.task = current;
377 list_add(&cb.base.node, &fence->cb_list);
378
379 while (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
380 if (intr)
381 __set_current_state(TASK_INTERRUPTIBLE);
382 else
383 __set_current_state(TASK_UNINTERRUPTIBLE);
384 spin_unlock_irqrestore(fence->lock, flags);
385
386 ret = schedule_timeout(ret);
387
388 spin_lock_irqsave(fence->lock, flags);
389 if (ret > 0 && intr && signal_pending(current))
390 ret = -ERESTARTSYS;
391 }
392
393 if (!list_empty(&cb.base.node))
394 list_del(&cb.base.node);
395 __set_current_state(TASK_RUNNING);
396
397out:
398 spin_unlock_irqrestore(fence->lock, flags);
399 return ret;
400}
401EXPORT_SYMBOL(fence_default_wait);
402
Christian Königa5194352015-10-20 16:34:16 +0200403static bool
404fence_test_signaled_any(struct fence **fences, uint32_t count)
405{
406 int i;
407
408 for (i = 0; i < count; ++i) {
409 struct fence *fence = fences[i];
410 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
411 return true;
412 }
413 return false;
414}
415
416/**
417 * fence_wait_any_timeout - sleep until any fence gets signaled
418 * or until timeout elapses
419 * @fences: [in] array of fences to wait on
420 * @count: [in] number of fences to wait on
421 * @intr: [in] if true, do an interruptible wait
422 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
423 *
424 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
425 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
426 * on success.
427 *
428 * Synchronous waits for the first fence in the array to be signaled. The
429 * caller needs to hold a reference to all fences in the array, otherwise a
430 * fence might be freed before return, resulting in undefined behavior.
431 */
432signed long
433fence_wait_any_timeout(struct fence **fences, uint32_t count,
434 bool intr, signed long timeout)
435{
436 struct default_wait_cb *cb;
437 signed long ret = timeout;
438 unsigned i;
439
440 if (WARN_ON(!fences || !count || timeout < 0))
441 return -EINVAL;
442
443 if (timeout == 0) {
444 for (i = 0; i < count; ++i)
445 if (fence_is_signaled(fences[i]))
446 return 1;
447
448 return 0;
449 }
450
451 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
452 if (cb == NULL) {
453 ret = -ENOMEM;
454 goto err_free_cb;
455 }
456
457 for (i = 0; i < count; ++i) {
458 struct fence *fence = fences[i];
459
460 if (fence->ops->wait != fence_default_wait) {
461 ret = -EINVAL;
462 goto fence_rm_cb;
463 }
464
465 cb[i].task = current;
466 if (fence_add_callback(fence, &cb[i].base,
467 fence_default_wait_cb)) {
468 /* This fence is already signaled */
469 goto fence_rm_cb;
470 }
471 }
472
473 while (ret > 0) {
474 if (intr)
475 set_current_state(TASK_INTERRUPTIBLE);
476 else
477 set_current_state(TASK_UNINTERRUPTIBLE);
478
479 if (fence_test_signaled_any(fences, count))
480 break;
481
482 ret = schedule_timeout(ret);
483
484 if (ret > 0 && intr && signal_pending(current))
485 ret = -ERESTARTSYS;
486 }
487
488 __set_current_state(TASK_RUNNING);
489
490fence_rm_cb:
491 while (i-- > 0)
492 fence_remove_callback(fences[i], &cb[i].base);
493
494err_free_cb:
495 kfree(cb);
496
497 return ret;
498}
499EXPORT_SYMBOL(fence_wait_any_timeout);
500
Maarten Lankhorste9417592014-07-01 12:57:14 +0200501/**
502 * fence_init - Initialize a custom fence.
503 * @fence: [in] the fence to initialize
504 * @ops: [in] the fence_ops for operations on this fence
505 * @lock: [in] the irqsafe spinlock to use for locking this fence
506 * @context: [in] the execution context this fence is run on
507 * @seqno: [in] a linear increasing sequence number for this context
508 *
509 * Initializes an allocated fence, the caller doesn't have to keep its
510 * refcount after committing with this fence, but it will need to hold a
511 * refcount again if fence_ops.enable_signaling gets called. This can
512 * be used for other implementing other types of fence.
513 *
514 * context and seqno are used for easy comparison between fences, allowing
515 * to check which fence is later by simply using fence_later.
516 */
517void
518fence_init(struct fence *fence, const struct fence_ops *ops,
Christian König76bf0db2016-06-01 15:10:02 +0200519 spinlock_t *lock, u64 context, unsigned seqno)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200520{
521 BUG_ON(!lock);
522 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
523 !ops->get_driver_name || !ops->get_timeline_name);
524
525 kref_init(&fence->refcount);
526 fence->ops = ops;
527 INIT_LIST_HEAD(&fence->cb_list);
528 fence->lock = lock;
529 fence->context = context;
530 fence->seqno = seqno;
531 fence->flags = 0UL;
532
533 trace_fence_init(fence);
534}
535EXPORT_SYMBOL(fence_init);