blob: 7c9b78cf2a7ed2fa0a2e92adf07747251494ed1c [file] [log] [blame]
Maarten Lankhorste9417592014-07-01 12:57:14 +02001/*
2 * Fence mechanism for dma-buf 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#ifndef __LINUX_FENCE_H
22#define __LINUX_FENCE_H
23
24#include <linux/err.h>
25#include <linux/wait.h>
26#include <linux/list.h>
27#include <linux/bitops.h>
28#include <linux/kref.h>
29#include <linux/sched.h>
30#include <linux/printk.h>
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +020031#include <linux/rcupdate.h>
Maarten Lankhorste9417592014-07-01 12:57:14 +020032
33struct fence;
34struct fence_ops;
35struct fence_cb;
36
37/**
38 * struct fence - software synchronization primitive
39 * @refcount: refcount for this fence
40 * @ops: fence_ops associated with this fence
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +020041 * @rcu: used for releasing fence with kfree_rcu
Maarten Lankhorste9417592014-07-01 12:57:14 +020042 * @cb_list: list of all callbacks to call
43 * @lock: spin_lock_irqsave used for locking
44 * @context: execution context this fence belongs to, returned by
45 * fence_context_alloc()
46 * @seqno: the sequence number of this fence inside the execution context,
47 * can be compared to decide which fence would be signaled later.
48 * @flags: A mask of FENCE_FLAG_* defined below
49 * @timestamp: Timestamp when the fence was signaled.
Chris Wilsonf5e07242017-01-04 14:12:22 +000050 * @error: Optional, only valid if < 0, must be set before calling
Maarten Lankhorste9417592014-07-01 12:57:14 +020051 * fence_signal, indicates that the fence has completed with an error.
52 *
53 * the flags member must be manipulated and read using the appropriate
54 * atomic ops (bit_*), so taking the spinlock will not be needed most
55 * of the time.
56 *
57 * FENCE_FLAG_SIGNALED_BIT - fence is already signaled
58 * FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called*
59 * FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
60 * implementer of the fence for its own purposes. Can be used in different
61 * ways by different fence implementers, so do not rely on this.
62 *
Sumit Semwal3590d502016-08-11 16:17:58 +053063 * Since atomic bitops are used, this is not guaranteed to be the case.
Maarten Lankhorste9417592014-07-01 12:57:14 +020064 * Particularly, if the bit was set, but fence_signal was called right
65 * before this bit was set, it would have been able to set the
66 * FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
67 * Adding a check for FENCE_FLAG_SIGNALED_BIT after setting
68 * FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
69 * after fence_signal was called, any enable_signaling call will have either
70 * been completed, or never called at all.
71 */
72struct fence {
73 struct kref refcount;
74 const struct fence_ops *ops;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +020075 struct rcu_head rcu;
Maarten Lankhorste9417592014-07-01 12:57:14 +020076 struct list_head cb_list;
77 spinlock_t *lock;
Christian König76bf0db2016-06-01 15:10:02 +020078 u64 context;
79 unsigned seqno;
Maarten Lankhorste9417592014-07-01 12:57:14 +020080 unsigned long flags;
81 ktime_t timestamp;
Chris Wilsonf5e07242017-01-04 14:12:22 +000082 int error;
Maarten Lankhorste9417592014-07-01 12:57:14 +020083};
84
85enum fence_flag_bits {
86 FENCE_FLAG_SIGNALED_BIT,
87 FENCE_FLAG_ENABLE_SIGNAL_BIT,
88 FENCE_FLAG_USER_BITS, /* must always be last member */
89};
90
91typedef void (*fence_func_t)(struct fence *fence, struct fence_cb *cb);
92
93/**
94 * struct fence_cb - callback for fence_add_callback
95 * @node: used by fence_add_callback to append this struct to fence::cb_list
96 * @func: fence_func_t to call
97 *
98 * This struct will be initialized by fence_add_callback, additional
99 * data can be passed along by embedding fence_cb in another struct.
100 */
101struct fence_cb {
102 struct list_head node;
103 fence_func_t func;
104};
105
106/**
107 * struct fence_ops - operations implemented for fence
108 * @get_driver_name: returns the driver name.
109 * @get_timeline_name: return the name of the context this fence belongs to.
110 * @enable_signaling: enable software signaling of fence.
Dmitry Torokhov2a061a82015-09-08 17:30:52 -0700111 * @disable_signaling: disable software signaling of fence (optional).
Maarten Lankhorste9417592014-07-01 12:57:14 +0200112 * @signaled: [optional] peek whether the fence is signaled, can be null.
113 * @wait: custom wait implementation, or fence_default_wait.
114 * @release: [optional] called on destruction of fence, can be null
115 * @fill_driver_data: [optional] callback to fill in free-form debug info
116 * Returns amount of bytes filled, or -errno.
117 * @fence_value_str: [optional] fills in the value of the fence as a string
118 * @timeline_value_str: [optional] fills in the current value of the timeline
119 * as a string
120 *
121 * Notes on enable_signaling:
122 * For fence implementations that have the capability for hw->hw
123 * signaling, they can implement this op to enable the necessary
124 * irqs, or insert commands into cmdstream, etc. This is called
125 * in the first wait() or add_callback() path to let the fence
126 * implementation know that there is another driver waiting on
127 * the signal (ie. hw->sw case).
128 *
129 * This function can be called called from atomic context, but not
130 * from irq context, so normal spinlocks can be used.
131 *
132 * A return value of false indicates the fence already passed,
Masanari Iidaf353d712014-10-22 00:00:14 +0900133 * or some failure occurred that made it impossible to enable
134 * signaling. True indicates successful enabling.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200135 *
Chris Wilsonf5e07242017-01-04 14:12:22 +0000136 * fence->error may be set in enable_signaling, but only when false is
Maarten Lankhorste9417592014-07-01 12:57:14 +0200137 * returned.
138 *
139 * Calling fence_signal before enable_signaling is called allows
140 * for a tiny race window in which enable_signaling is called during,
141 * before, or after fence_signal. To fight this, it is recommended
142 * that before enable_signaling returns true an extra reference is
143 * taken on the fence, to be released when the fence is signaled.
144 * This will mean fence_signal will still be called twice, but
145 * the second time will be a noop since it was already signaled.
146 *
147 * Notes on signaled:
Chris Wilsonf5e07242017-01-04 14:12:22 +0000148 * May set fence->error if returning true.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200149 *
150 * Notes on wait:
151 * Must not be NULL, set to fence_default_wait for default implementation.
152 * the fence_default_wait implementation should work for any fence, as long
153 * as enable_signaling works correctly.
154 *
155 * Must return -ERESTARTSYS if the wait is intr = true and the wait was
156 * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
157 * timed out. Can also return other error values on custom implementations,
158 * which should be treated as if the fence is signaled. For example a hardware
159 * lockup could be reported like that.
160 *
161 * Notes on release:
162 * Can be NULL, this function allows additional commands to run on
163 * destruction of the fence. Can be called from irq context.
164 * If pointer is set to NULL, kfree will get called instead.
165 */
166
167struct fence_ops {
168 const char * (*get_driver_name)(struct fence *fence);
169 const char * (*get_timeline_name)(struct fence *fence);
170 bool (*enable_signaling)(struct fence *fence);
Dmitry Torokhov2a061a82015-09-08 17:30:52 -0700171 void (*disable_signaling)(struct fence *fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200172 bool (*signaled)(struct fence *fence);
173 signed long (*wait)(struct fence *fence, bool intr, signed long timeout);
174 void (*release)(struct fence *fence);
175
176 int (*fill_driver_data)(struct fence *fence, void *data, int size);
177 void (*fence_value_str)(struct fence *fence, char *str, int size);
178 void (*timeline_value_str)(struct fence *fence, char *str, int size);
179};
180
181void fence_init(struct fence *fence, const struct fence_ops *ops,
Christian König76bf0db2016-06-01 15:10:02 +0200182 spinlock_t *lock, u64 context, unsigned seqno);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200183
184void fence_release(struct kref *kref);
185void fence_free(struct fence *fence);
186
187/**
Chris Wilson941055b2016-08-29 08:08:29 +0100188 * fence_put - decreases refcount of the fence
189 * @fence: [in] fence to reduce refcount of
190 */
191static inline void fence_put(struct fence *fence)
192{
193 if (fence)
194 kref_put(&fence->refcount, fence_release);
195}
196
197/**
Maarten Lankhorste9417592014-07-01 12:57:14 +0200198 * fence_get - increases refcount of the fence
199 * @fence: [in] fence to increase refcount of
200 *
201 * Returns the same fence, with refcount increased by 1.
202 */
203static inline struct fence *fence_get(struct fence *fence)
204{
205 if (fence)
206 kref_get(&fence->refcount);
207 return fence;
208}
209
210/**
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200211 * fence_get_rcu - get a fence from a reservation_object_list with rcu read lock
212 * @fence: [in] fence to increase refcount of
213 *
214 * Function returns NULL if no refcount could be obtained, or the fence.
215 */
216static inline struct fence *fence_get_rcu(struct fence *fence)
217{
218 if (kref_get_unless_zero(&fence->refcount))
219 return fence;
220 else
221 return NULL;
222}
223
224/**
Chris Wilson941055b2016-08-29 08:08:29 +0100225 * fence_get_rcu_safe - acquire a reference to an RCU tracked fence
226 * @fence: [in] pointer to fence to increase refcount of
227 *
228 * Function returns NULL if no refcount could be obtained, or the fence.
229 * This function handles acquiring a reference to a fence that may be
230 * reallocated within the RCU grace period (such as with SLAB_DESTROY_BY_RCU),
231 * so long as the caller is using RCU on the pointer to the fence.
232 *
233 * An alternative mechanism is to employ a seqlock to protect a bunch of
234 * fences, such as used by struct reservation_object. When using a seqlock,
235 * the seqlock must be taken before and checked after a reference to the
236 * fence is acquired (as shown here).
237 *
238 * The caller is required to hold the RCU read lock.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200239 */
Chris Wilson941055b2016-08-29 08:08:29 +0100240static inline struct fence *fence_get_rcu_safe(struct fence * __rcu *fencep)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200241{
Chris Wilson941055b2016-08-29 08:08:29 +0100242 do {
243 struct fence *fence;
244
245 fence = rcu_dereference(*fencep);
246 if (!fence || !fence_get_rcu(fence))
247 return NULL;
248
249 /* The atomic_inc_not_zero() inside fence_get_rcu()
250 * provides a full memory barrier upon success (such as now).
251 * This is paired with the write barrier from assigning
252 * to the __rcu protected fence pointer so that if that
253 * pointer still matches the current fence, we know we
254 * have successfully acquire a reference to it. If it no
255 * longer matches, we are holding a reference to some other
256 * reallocated pointer. This is possible if the allocator
257 * is using a freelist like SLAB_DESTROY_BY_RCU where the
258 * fence remains valid for the RCU grace period, but it
259 * may be reallocated. When using such allocators, we are
260 * responsible for ensuring the reference we get is to
261 * the right fence, as below.
262 */
263 if (fence == rcu_access_pointer(*fencep))
264 return rcu_pointer_handoff(fence);
265
266 fence_put(fence);
267 } while (1);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200268}
269
270int fence_signal(struct fence *fence);
271int fence_signal_locked(struct fence *fence);
272signed long fence_default_wait(struct fence *fence, bool intr, signed long timeout);
273int fence_add_callback(struct fence *fence, struct fence_cb *cb,
274 fence_func_t func);
275bool fence_remove_callback(struct fence *fence, struct fence_cb *cb);
276void fence_enable_sw_signaling(struct fence *fence);
277
278/**
279 * fence_is_signaled_locked - Return an indication if the fence is signaled yet.
280 * @fence: [in] the fence to check
281 *
282 * Returns true if the fence was already signaled, false if not. Since this
283 * function doesn't enable signaling, it is not guaranteed to ever return
284 * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
285 * haven't been called before.
286 *
287 * This function requires fence->lock to be held.
288 */
289static inline bool
290fence_is_signaled_locked(struct fence *fence)
291{
292 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
293 return true;
294
295 if (fence->ops->signaled && fence->ops->signaled(fence)) {
296 fence_signal_locked(fence);
297 return true;
298 }
299
300 return false;
301}
302
303/**
304 * fence_is_signaled - Return an indication if the fence is signaled yet.
305 * @fence: [in] the fence to check
306 *
307 * Returns true if the fence was already signaled, false if not. Since this
308 * function doesn't enable signaling, it is not guaranteed to ever return
309 * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
310 * haven't been called before.
311 *
312 * It's recommended for seqno fences to call fence_signal when the
313 * operation is complete, it makes it possible to prevent issues from
314 * wraparound between time of issue and time of use by checking the return
315 * value of this function before calling hardware-specific wait instructions.
316 */
317static inline bool
318fence_is_signaled(struct fence *fence)
319{
320 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
321 return true;
322
323 if (fence->ops->signaled && fence->ops->signaled(fence)) {
324 fence_signal(fence);
325 return true;
326 }
327
328 return false;
329}
330
331/**
Chris Wilsonb53525e2017-06-29 13:59:24 +0100332 * __fence_is_later - return if f1 is chronologically later than f2
333 * @f1: [in] the first fence's seqno
334 * @f2: [in] the second fence's seqno from the same context
335 *
336 * Returns true if f1 is chronologically later than f2. Both fences must be
337 * from the same context, since a seqno is not common across contexts.
338 */
339static inline bool __fence_is_later(u32 f1, u32 f2)
340{
341 return (int)(f1 - f2) > 0;
342}
343
344/**
Christian König6c455ac2015-10-21 12:58:17 +0200345 * fence_is_later - return if f1 is chronologically later than f2
346 * @f1: [in] the first fence from the same context
347 * @f2: [in] the second fence from the same context
348 *
349 * Returns true if f1 is chronologically later than f2. Both fences must be
350 * from the same context, since a seqno is not re-used across contexts.
351 */
352static inline bool fence_is_later(struct fence *f1, struct fence *f2)
353{
354 if (WARN_ON(f1->context != f2->context))
355 return false;
356
Chris Wilsonb53525e2017-06-29 13:59:24 +0100357 return __fence_is_later(f1->seqno, f2->seqno);
Christian König6c455ac2015-10-21 12:58:17 +0200358}
359
360/**
Maarten Lankhorste9417592014-07-01 12:57:14 +0200361 * fence_later - return the chronologically later fence
362 * @f1: [in] the first fence from the same context
363 * @f2: [in] the second fence from the same context
364 *
365 * Returns NULL if both fences are signaled, otherwise the fence that would be
366 * signaled last. Both fences must be from the same context, since a seqno is
367 * not re-used across contexts.
368 */
369static inline struct fence *fence_later(struct fence *f1, struct fence *f2)
370{
371 if (WARN_ON(f1->context != f2->context))
372 return NULL;
373
374 /*
375 * can't check just FENCE_FLAG_SIGNALED_BIT here, it may never have been
376 * set if enable_signaling wasn't called, and enabling that here is
377 * overkill.
378 */
Christian König6c455ac2015-10-21 12:58:17 +0200379 if (fence_is_later(f1, f2))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200380 return fence_is_signaled(f1) ? NULL : f1;
Christian König6c455ac2015-10-21 12:58:17 +0200381 else
382 return fence_is_signaled(f2) ? NULL : f2;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200383}
384
Chris Wilsond3b029a2017-01-04 14:12:21 +0000385/**
386 * fence_get_status_locked - returns the status upon completion
387 * @fence: [in] the fence to query
388 *
389 * Drivers can supply an optional error status condition before they signal
390 * the fence (to indicate whether the fence was completed due to an error
391 * rather than success). The value of the status condition is only valid
392 * if the fence has been signaled, fence_get_status_locked() first checks
393 * the signal state before reporting the error status.
394 *
395 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
396 * been signaled without an error condition, or a negative error code
397 * if the fence has been completed in err.
398 */
399static inline int fence_get_status_locked(struct fence *fence)
400{
401 if (fence_is_signaled_locked(fence))
Chris Wilsonf5e07242017-01-04 14:12:22 +0000402 return fence->error ?: 1;
Chris Wilsond3b029a2017-01-04 14:12:21 +0000403 else
404 return 0;
405}
406
407int fence_get_status(struct fence *fence);
408
Chris Wilsonf5e07242017-01-04 14:12:22 +0000409/**
410 * fence_set_error - flag an error condition on the fence
411 * @fence: [in] the fence
412 * @error: [in] the error to store
413 *
414 * Drivers can supply an optional error status condition before they signal
415 * the fence, to indicate that the fence was completed due to an error
416 * rather than success. This must be set before signaling (so that the value
417 * is visible before any waiters on the signal callback are woken). This
418 * helper exists to help catching erroneous setting of #fence.error.
419 */
420static inline void fence_set_error(struct fence *fence,
421 int error)
422{
423 BUG_ON(test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags));
424 BUG_ON(error >= 0 || error < -MAX_ERRNO);
425
426 fence->error = error;
427}
428
Maarten Lankhorste9417592014-07-01 12:57:14 +0200429signed long fence_wait_timeout(struct fence *, bool intr, signed long timeout);
Christian Königa5194352015-10-20 16:34:16 +0200430signed long fence_wait_any_timeout(struct fence **fences, uint32_t count,
431 bool intr, signed long timeout);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200432
433/**
434 * fence_wait - sleep until the fence gets signaled
435 * @fence: [in] the fence to wait on
436 * @intr: [in] if true, do an interruptible wait
437 *
438 * This function will return -ERESTARTSYS if interrupted by a signal,
439 * or 0 if the fence was signaled. Other error values may be
440 * returned on custom implementations.
441 *
442 * Performs a synchronous wait on this fence. It is assumed the caller
443 * directly or indirectly holds a reference to the fence, otherwise the
444 * fence might be freed before return, resulting in undefined behavior.
445 */
446static inline signed long fence_wait(struct fence *fence, bool intr)
447{
448 signed long ret;
449
450 /* Since fence_wait_timeout cannot timeout with
451 * MAX_SCHEDULE_TIMEOUT, only valid return values are
452 * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
453 */
454 ret = fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
455
456 return ret < 0 ? ret : 0;
457}
458
Christian König76bf0db2016-06-01 15:10:02 +0200459u64 fence_context_alloc(unsigned num);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200460
461#define FENCE_TRACE(f, fmt, args...) \
462 do { \
463 struct fence *__ff = (f); \
Masahiro Yamada97f26452016-08-03 13:45:50 -0700464 if (IS_ENABLED(CONFIG_FENCE_TRACE)) \
Christian König76bf0db2016-06-01 15:10:02 +0200465 pr_info("f %llu#%u: " fmt, \
Maarten Lankhorste9417592014-07-01 12:57:14 +0200466 __ff->context, __ff->seqno, ##args); \
467 } while (0)
468
469#define FENCE_WARN(f, fmt, args...) \
470 do { \
471 struct fence *__ff = (f); \
Christian König76bf0db2016-06-01 15:10:02 +0200472 pr_warn("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
Maarten Lankhorste9417592014-07-01 12:57:14 +0200473 ##args); \
474 } while (0)
475
476#define FENCE_ERR(f, fmt, args...) \
477 do { \
478 struct fence *__ff = (f); \
Christian König76bf0db2016-06-01 15:10:02 +0200479 pr_err("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
Maarten Lankhorste9417592014-07-01 12:57:14 +0200480 ##args); \
481 } while (0)
482
483#endif /* __LINUX_FENCE_H */