Subversion Repositories Kolibri OS

Rev

Rev 6082 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5270 serge 1
/*
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 
9
 * Maarten Lankhorst 
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 
25
#include 
26
#include 
27
#include 
28
#include 
29
#include 
30
#include 
31
#include 
32
 
33
struct fence;
34
struct fence_ops;
35
struct fence_cb;
36
 
37
/**
38
 * struct fence - software synchronization primitive
39
 * @refcount: refcount for this fence
40
 * @ops: fence_ops associated with this fence
41
 * @rcu: used for releasing fence with kfree_rcu
42
 * @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.
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
 *
63
 * *) Since atomic bitops are used, this is not guaranteed to be the case.
64
 * 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
 */
72
struct fence {
73
	struct kref refcount;
74
	const struct fence_ops *ops;
75
	struct rcu_head rcu;
76
	struct list_head cb_list;
77
	spinlock_t *lock;
78
	unsigned context, seqno;
79
	unsigned long flags;
6082 serge 80
	ktime_t timestamp;
5270 serge 81
	int status;
7143 serge 82
	struct list_head child_list;
83
	struct list_head active_list;
5270 serge 84
};
85
 
86
enum fence_flag_bits {
87
	FENCE_FLAG_SIGNALED_BIT,
88
	FENCE_FLAG_ENABLE_SIGNAL_BIT,
89
	FENCE_FLAG_USER_BITS, /* must always be last member */
90
};
91
 
92
typedef void (*fence_func_t)(struct fence *fence, struct fence_cb *cb);
93
 
94
/**
95
 * struct fence_cb - callback for fence_add_callback
96
 * @node: used by fence_add_callback to append this struct to fence::cb_list
97
 * @func: fence_func_t to call
98
 *
99
 * This struct will be initialized by fence_add_callback, additional
100
 * data can be passed along by embedding fence_cb in another struct.
101
 */
102
struct fence_cb {
103
	struct list_head node;
104
	fence_func_t func;
105
};
106
 
107
/**
108
 * struct fence_ops - operations implemented for fence
109
 * @get_driver_name: returns the driver name.
110
 * @get_timeline_name: return the name of the context this fence belongs to.
111
 * @enable_signaling: enable software signaling of fence.
112
 * @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,
133
 * or some failure occurred that made it impossible to enable
134
 * signaling. True indicates successful enabling.
135
 *
136
 * fence->status may be set in enable_signaling, but only when false is
137
 * 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:
148
 * May set fence->status if returning true.
149
 *
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
 
167
struct 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);
171
	bool (*signaled)(struct fence *fence);
172
	signed long (*wait)(struct fence *fence, bool intr, signed long timeout);
173
	void (*release)(struct fence *fence);
174
 
175
	int (*fill_driver_data)(struct fence *fence, void *data, int size);
176
	void (*fence_value_str)(struct fence *fence, char *str, int size);
177
	void (*timeline_value_str)(struct fence *fence, char *str, int size);
178
};
179
 
180
void fence_init(struct fence *fence, const struct fence_ops *ops,
181
		spinlock_t *lock, unsigned context, unsigned seqno);
182
 
183
void fence_release(struct kref *kref);
184
void fence_free(struct fence *fence);
185
 
186
/**
187
 * fence_get - increases refcount of the fence
188
 * @fence:	[in]	fence to increase refcount of
189
 *
190
 * Returns the same fence, with refcount increased by 1.
191
 */
192
static inline struct fence *fence_get(struct fence *fence)
193
{
194
	if (fence)
195
		kref_get(&fence->refcount);
196
	return fence;
197
}
198
 
199
/**
200
 * fence_get_rcu - get a fence from a reservation_object_list with rcu read lock
201
 * @fence:	[in]	fence to increase refcount of
202
 *
203
 * Function returns NULL if no refcount could be obtained, or the fence.
204
 */
205
static inline struct fence *fence_get_rcu(struct fence *fence)
206
{
207
	if (kref_get_unless_zero(&fence->refcount))
208
		return fence;
209
	else
210
		return NULL;
211
}
212
 
213
/**
214
 * fence_put - decreases refcount of the fence
215
 * @fence:	[in]	fence to reduce refcount of
216
 */
217
static inline void fence_put(struct fence *fence)
218
{
219
	if (fence)
220
		kref_put(&fence->refcount, fence_release);
221
}
222
 
223
int fence_signal(struct fence *fence);
224
int fence_signal_locked(struct fence *fence);
225
signed long fence_default_wait(struct fence *fence, bool intr, signed long timeout);
226
int fence_add_callback(struct fence *fence, struct fence_cb *cb,
227
		       fence_func_t func);
228
bool fence_remove_callback(struct fence *fence, struct fence_cb *cb);
229
void fence_enable_sw_signaling(struct fence *fence);
230
 
231
/**
232
 * fence_is_signaled_locked - Return an indication if the fence is signaled yet.
233
 * @fence:	[in]	the fence to check
234
 *
235
 * Returns true if the fence was already signaled, false if not. Since this
236
 * function doesn't enable signaling, it is not guaranteed to ever return
237
 * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
238
 * haven't been called before.
239
 *
240
 * This function requires fence->lock to be held.
241
 */
242
static inline bool
243
fence_is_signaled_locked(struct fence *fence)
244
{
245
	if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
246
		return true;
247
 
248
	if (fence->ops->signaled && fence->ops->signaled(fence)) {
249
		fence_signal_locked(fence);
250
		return true;
251
	}
252
 
253
	return false;
254
}
255
 
256
/**
257
 * fence_is_signaled - Return an indication if the fence is signaled yet.
258
 * @fence:	[in]	the fence to check
259
 *
260
 * Returns true if the fence was already signaled, false if not. Since this
261
 * function doesn't enable signaling, it is not guaranteed to ever return
262
 * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
263
 * haven't been called before.
264
 *
265
 * It's recommended for seqno fences to call fence_signal when the
266
 * operation is complete, it makes it possible to prevent issues from
267
 * wraparound between time of issue and time of use by checking the return
268
 * value of this function before calling hardware-specific wait instructions.
269
 */
270
static inline bool
271
fence_is_signaled(struct fence *fence)
272
{
273
	if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
274
		return true;
275
 
276
	if (fence->ops->signaled && fence->ops->signaled(fence)) {
277
		fence_signal(fence);
278
		return true;
279
	}
280
 
281
	return false;
282
}
283
 
284
/**
6082 serge 285
 * fence_is_later - return if f1 is chronologically later than f2
286
 * @f1:	[in]	the first fence from the same context
287
 * @f2:	[in]	the second fence from the same context
288
 *
289
 * Returns true if f1 is chronologically later than f2. Both fences must be
290
 * from the same context, since a seqno is not re-used across contexts.
291
 */
292
static inline bool fence_is_later(struct fence *f1, struct fence *f2)
293
{
294
	if (WARN_ON(f1->context != f2->context))
295
		return false;
296
 
7143 serge 297
	return (int)(f1->seqno - f2->seqno) > 0;
6082 serge 298
}
299
 
300
/**
5270 serge 301
 * fence_later - return the chronologically later fence
302
 * @f1:	[in]	the first fence from the same context
303
 * @f2:	[in]	the second fence from the same context
304
 *
305
 * Returns NULL if both fences are signaled, otherwise the fence that would be
306
 * signaled last. Both fences must be from the same context, since a seqno is
307
 * not re-used across contexts.
308
 */
309
static inline struct fence *fence_later(struct fence *f1, struct fence *f2)
310
{
311
	if (WARN_ON(f1->context != f2->context))
312
		return NULL;
313
 
314
	/*
315
	 * can't check just FENCE_FLAG_SIGNALED_BIT here, it may never have been
316
	 * set if enable_signaling wasn't called, and enabling that here is
317
	 * overkill.
318
	 */
6082 serge 319
	if (fence_is_later(f1, f2))
320
		return fence_is_signaled(f1) ? NULL : f1;
321
	else
5270 serge 322
		return fence_is_signaled(f2) ? NULL : f2;
323
}
324
 
325
signed long fence_wait_timeout(struct fence *, bool intr, signed long timeout);
6082 serge 326
signed long fence_wait_any_timeout(struct fence **fences, uint32_t count,
327
				   bool intr, signed long timeout);
5270 serge 328
 
329
/**
330
 * fence_wait - sleep until the fence gets signaled
331
 * @fence:	[in]	the fence to wait on
332
 * @intr:	[in]	if true, do an interruptible wait
333
 *
334
 * This function will return -ERESTARTSYS if interrupted by a signal,
335
 * or 0 if the fence was signaled. Other error values may be
336
 * returned on custom implementations.
337
 *
338
 * Performs a synchronous wait on this fence. It is assumed the caller
339
 * directly or indirectly holds a reference to the fence, otherwise the
340
 * fence might be freed before return, resulting in undefined behavior.
341
 */
342
static inline signed long fence_wait(struct fence *fence, bool intr)
343
{
344
	signed long ret;
345
 
346
	/* Since fence_wait_timeout cannot timeout with
347
	 * MAX_SCHEDULE_TIMEOUT, only valid return values are
348
	 * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
349
	 */
350
	ret = fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
351
 
352
	return ret < 0 ? ret : 0;
353
}
354
 
355
unsigned fence_context_alloc(unsigned num);
356
 
357
#define FENCE_TRACE(f, fmt, args...) \
358
	do {								\
359
		struct fence *__ff = (f);				\
360
	} while (0)
361
 
362
#define FENCE_WARN(f, fmt, args...) \
363
	do {								\
364
		struct fence *__ff = (f);				\
365
		pr_warn("f %u#%u: " fmt, __ff->context, __ff->seqno,	\
366
			 ##args);					\
367
	} while (0)
368
 
369
#define FENCE_ERR(f, fmt, args...) \
370
	do {								\
371
		struct fence *__ff = (f);				\
372
		pr_err("f %u#%u: " fmt, __ff->context, __ff->seqno,	\
373
			##args);					\
374
	} while (0)
375
 
376
#endif /* __LINUX_FENCE_H */