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