blob: 8cc719a637286d7262386d21c6ed40453b36a1e5 [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.
50 * @status: Optional, only valid if < 0, must be set before calling
51 * fence_signal, indicates that the fence has completed with an error.
Luis de Bethencourt4320c2a2016-04-11 12:48:55 +010052 * @child_list: list of children fences
53 * @active_list: list of active fences
Maarten Lankhorste9417592014-07-01 12:57:14 +020054 *
55 * the flags member must be manipulated and read using the appropriate
56 * atomic ops (bit_*), so taking the spinlock will not be needed most
57 * of the time.
58 *
59 * FENCE_FLAG_SIGNALED_BIT - fence is already signaled
60 * FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called*
61 * FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
62 * implementer of the fence for its own purposes. Can be used in different
63 * ways by different fence implementers, so do not rely on this.
64 *
65 * *) Since atomic bitops are used, this is not guaranteed to be the case.
66 * Particularly, if the bit was set, but fence_signal was called right
67 * before this bit was set, it would have been able to set the
68 * FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
69 * Adding a check for FENCE_FLAG_SIGNALED_BIT after setting
70 * FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
71 * after fence_signal was called, any enable_signaling call will have either
72 * been completed, or never called at all.
73 */
74struct fence {
75 struct kref refcount;
76 const struct fence_ops *ops;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +020077 struct rcu_head rcu;
Maarten Lankhorste9417592014-07-01 12:57:14 +020078 struct list_head cb_list;
79 spinlock_t *lock;
Christian König76bf0db2016-06-01 15:10:02 +020080 u64 context;
81 unsigned seqno;
Maarten Lankhorste9417592014-07-01 12:57:14 +020082 unsigned long flags;
83 ktime_t timestamp;
84 int status;
85};
86
87enum fence_flag_bits {
88 FENCE_FLAG_SIGNALED_BIT,
89 FENCE_FLAG_ENABLE_SIGNAL_BIT,
90 FENCE_FLAG_USER_BITS, /* must always be last member */
91};
92
93typedef void (*fence_func_t)(struct fence *fence, struct fence_cb *cb);
94
95/**
96 * struct fence_cb - callback for fence_add_callback
97 * @node: used by fence_add_callback to append this struct to fence::cb_list
98 * @func: fence_func_t to call
99 *
100 * This struct will be initialized by fence_add_callback, additional
101 * data can be passed along by embedding fence_cb in another struct.
102 */
103struct fence_cb {
104 struct list_head node;
105 fence_func_t func;
106};
107
108/**
109 * struct fence_ops - operations implemented for fence
110 * @get_driver_name: returns the driver name.
111 * @get_timeline_name: return the name of the context this fence belongs to.
112 * @enable_signaling: enable software signaling of fence.
113 * @signaled: [optional] peek whether the fence is signaled, can be null.
114 * @wait: custom wait implementation, or fence_default_wait.
115 * @release: [optional] called on destruction of fence, can be null
116 * @fill_driver_data: [optional] callback to fill in free-form debug info
117 * Returns amount of bytes filled, or -errno.
118 * @fence_value_str: [optional] fills in the value of the fence as a string
119 * @timeline_value_str: [optional] fills in the current value of the timeline
120 * as a string
121 *
122 * Notes on enable_signaling:
123 * For fence implementations that have the capability for hw->hw
124 * signaling, they can implement this op to enable the necessary
125 * irqs, or insert commands into cmdstream, etc. This is called
126 * in the first wait() or add_callback() path to let the fence
127 * implementation know that there is another driver waiting on
128 * the signal (ie. hw->sw case).
129 *
130 * This function can be called called from atomic context, but not
131 * from irq context, so normal spinlocks can be used.
132 *
133 * A return value of false indicates the fence already passed,
Masanari Iidaf353d712014-10-22 00:00:14 +0900134 * or some failure occurred that made it impossible to enable
135 * signaling. True indicates successful enabling.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200136 *
137 * fence->status may be set in enable_signaling, but only when false is
138 * returned.
139 *
140 * Calling fence_signal before enable_signaling is called allows
141 * for a tiny race window in which enable_signaling is called during,
142 * before, or after fence_signal. To fight this, it is recommended
143 * that before enable_signaling returns true an extra reference is
144 * taken on the fence, to be released when the fence is signaled.
145 * This will mean fence_signal will still be called twice, but
146 * the second time will be a noop since it was already signaled.
147 *
148 * Notes on signaled:
149 * May set fence->status if returning true.
150 *
151 * Notes on wait:
152 * Must not be NULL, set to fence_default_wait for default implementation.
153 * the fence_default_wait implementation should work for any fence, as long
154 * as enable_signaling works correctly.
155 *
156 * Must return -ERESTARTSYS if the wait is intr = true and the wait was
157 * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
158 * timed out. Can also return other error values on custom implementations,
159 * which should be treated as if the fence is signaled. For example a hardware
160 * lockup could be reported like that.
161 *
162 * Notes on release:
163 * Can be NULL, this function allows additional commands to run on
164 * destruction of the fence. Can be called from irq context.
165 * If pointer is set to NULL, kfree will get called instead.
166 */
167
168struct fence_ops {
169 const char * (*get_driver_name)(struct fence *fence);
170 const char * (*get_timeline_name)(struct fence *fence);
171 bool (*enable_signaling)(struct fence *fence);
172 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/**
188 * fence_get - increases refcount of the fence
189 * @fence: [in] fence to increase refcount of
190 *
191 * Returns the same fence, with refcount increased by 1.
192 */
193static inline struct fence *fence_get(struct fence *fence)
194{
195 if (fence)
196 kref_get(&fence->refcount);
197 return fence;
198}
199
200/**
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200201 * fence_get_rcu - get a fence from a reservation_object_list with rcu read lock
202 * @fence: [in] fence to increase refcount of
203 *
204 * Function returns NULL if no refcount could be obtained, or the fence.
205 */
206static inline struct fence *fence_get_rcu(struct fence *fence)
207{
208 if (kref_get_unless_zero(&fence->refcount))
209 return fence;
210 else
211 return NULL;
212}
213
214/**
Maarten Lankhorste9417592014-07-01 12:57:14 +0200215 * fence_put - decreases refcount of the fence
216 * @fence: [in] fence to reduce refcount of
217 */
218static inline void fence_put(struct fence *fence)
219{
220 if (fence)
221 kref_put(&fence->refcount, fence_release);
222}
223
224int fence_signal(struct fence *fence);
225int fence_signal_locked(struct fence *fence);
226signed long fence_default_wait(struct fence *fence, bool intr, signed long timeout);
227int fence_add_callback(struct fence *fence, struct fence_cb *cb,
228 fence_func_t func);
229bool fence_remove_callback(struct fence *fence, struct fence_cb *cb);
230void fence_enable_sw_signaling(struct fence *fence);
231
232/**
233 * fence_is_signaled_locked - Return an indication if the fence is signaled yet.
234 * @fence: [in] the fence to check
235 *
236 * Returns true if the fence was already signaled, false if not. Since this
237 * function doesn't enable signaling, it is not guaranteed to ever return
238 * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
239 * haven't been called before.
240 *
241 * This function requires fence->lock to be held.
242 */
243static inline bool
244fence_is_signaled_locked(struct fence *fence)
245{
246 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
247 return true;
248
249 if (fence->ops->signaled && fence->ops->signaled(fence)) {
250 fence_signal_locked(fence);
251 return true;
252 }
253
254 return false;
255}
256
257/**
258 * fence_is_signaled - Return an indication if the fence is signaled yet.
259 * @fence: [in] the fence to check
260 *
261 * Returns true if the fence was already signaled, false if not. Since this
262 * function doesn't enable signaling, it is not guaranteed to ever return
263 * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
264 * haven't been called before.
265 *
266 * It's recommended for seqno fences to call fence_signal when the
267 * operation is complete, it makes it possible to prevent issues from
268 * wraparound between time of issue and time of use by checking the return
269 * value of this function before calling hardware-specific wait instructions.
270 */
271static inline bool
272fence_is_signaled(struct fence *fence)
273{
274 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
275 return true;
276
277 if (fence->ops->signaled && fence->ops->signaled(fence)) {
278 fence_signal(fence);
279 return true;
280 }
281
282 return false;
283}
284
285/**
Christian König6c455ac2015-10-21 12:58:17 +0200286 * fence_is_later - return if f1 is chronologically later than f2
287 * @f1: [in] the first fence from the same context
288 * @f2: [in] the second fence from the same context
289 *
290 * Returns true if f1 is chronologically later than f2. Both fences must be
291 * from the same context, since a seqno is not re-used across contexts.
292 */
293static inline bool fence_is_later(struct fence *f1, struct fence *f2)
294{
295 if (WARN_ON(f1->context != f2->context))
296 return false;
297
Christian Königb47bcb932016-02-29 10:53:11 -0500298 return (int)(f1->seqno - f2->seqno) > 0;
Christian König6c455ac2015-10-21 12:58:17 +0200299}
300
301/**
Maarten Lankhorste9417592014-07-01 12:57:14 +0200302 * fence_later - return the chronologically later fence
303 * @f1: [in] the first fence from the same context
304 * @f2: [in] the second fence from the same context
305 *
306 * Returns NULL if both fences are signaled, otherwise the fence that would be
307 * signaled last. Both fences must be from the same context, since a seqno is
308 * not re-used across contexts.
309 */
310static inline struct fence *fence_later(struct fence *f1, struct fence *f2)
311{
312 if (WARN_ON(f1->context != f2->context))
313 return NULL;
314
315 /*
316 * can't check just FENCE_FLAG_SIGNALED_BIT here, it may never have been
317 * set if enable_signaling wasn't called, and enabling that here is
318 * overkill.
319 */
Christian König6c455ac2015-10-21 12:58:17 +0200320 if (fence_is_later(f1, f2))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200321 return fence_is_signaled(f1) ? NULL : f1;
Christian König6c455ac2015-10-21 12:58:17 +0200322 else
323 return fence_is_signaled(f2) ? NULL : f2;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200324}
325
326signed long fence_wait_timeout(struct fence *, bool intr, signed long timeout);
Christian Königa5194352015-10-20 16:34:16 +0200327signed long fence_wait_any_timeout(struct fence **fences, uint32_t count,
328 bool intr, signed long timeout);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200329
330/**
331 * fence_wait - sleep until the fence gets signaled
332 * @fence: [in] the fence to wait on
333 * @intr: [in] if true, do an interruptible wait
334 *
335 * This function will return -ERESTARTSYS if interrupted by a signal,
336 * or 0 if the fence was signaled. Other error values may be
337 * returned on custom implementations.
338 *
339 * Performs a synchronous wait on this fence. It is assumed the caller
340 * directly or indirectly holds a reference to the fence, otherwise the
341 * fence might be freed before return, resulting in undefined behavior.
342 */
343static inline signed long fence_wait(struct fence *fence, bool intr)
344{
345 signed long ret;
346
347 /* Since fence_wait_timeout cannot timeout with
348 * MAX_SCHEDULE_TIMEOUT, only valid return values are
349 * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
350 */
351 ret = fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
352
353 return ret < 0 ? ret : 0;
354}
355
Christian König76bf0db2016-06-01 15:10:02 +0200356u64 fence_context_alloc(unsigned num);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200357
358#define FENCE_TRACE(f, fmt, args...) \
359 do { \
360 struct fence *__ff = (f); \
Masahiro Yamada97f26452016-08-03 13:45:50 -0700361 if (IS_ENABLED(CONFIG_FENCE_TRACE)) \
Christian König76bf0db2016-06-01 15:10:02 +0200362 pr_info("f %llu#%u: " fmt, \
Maarten Lankhorste9417592014-07-01 12:57:14 +0200363 __ff->context, __ff->seqno, ##args); \
364 } while (0)
365
366#define FENCE_WARN(f, fmt, args...) \
367 do { \
368 struct fence *__ff = (f); \
Christian König76bf0db2016-06-01 15:10:02 +0200369 pr_warn("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
Maarten Lankhorste9417592014-07-01 12:57:14 +0200370 ##args); \
371 } while (0)
372
373#define FENCE_ERR(f, fmt, args...) \
374 do { \
375 struct fence *__ff = (f); \
Christian König76bf0db2016-06-01 15:10:02 +0200376 pr_err("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
Maarten Lankhorste9417592014-07-01 12:57:14 +0200377 ##args); \
378 } while (0)
379
380#endif /* __LINUX_FENCE_H */