Blame view

drivers/dma-buf/dma-fence.c 15.7 KB
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*
   * Fence mechanism for dma-buf and to allow for asynchronous dma access
   *
   * Copyright (C) 2012 Canonical Ltd
   * Copyright (C) 2012 Texas Instruments
   *
   * Authors:
   * Rob Clark <robdclark@gmail.com>
   * Maarten Lankhorst <maarten.lankhorst@canonical.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published by
   * the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   */
  
  #include <linux/slab.h>
  #include <linux/export.h>
  #include <linux/atomic.h>
f54d18670   Chris Wilson   dma-buf: Rename s...
24
  #include <linux/dma-fence.h>
174cd4b1e   Ingo Molnar   sched/headers: Pr...
25
  #include <linux/sched/signal.h>
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
26
27
  
  #define CREATE_TRACE_POINTS
f54d18670   Chris Wilson   dma-buf: Rename s...
28
  #include <trace/events/dma_fence.h>
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
29

f54d18670   Chris Wilson   dma-buf: Rename s...
30
31
  EXPORT_TRACEPOINT_SYMBOL(dma_fence_annotate_wait_on);
  EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
8c96c6780   Chris Wilson   dma/fence: Export...
32
  EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
33

e9f3b7964   Thierry Reding   dma-buf/fence: Fi...
34
  /*
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
35
36
37
38
39
   * fence context counter: each execution context should have its own
   * fence context, this allows checking if fences belong to the same
   * context or not. One device can have multiple separate contexts,
   * and they're used if some engine can run independently of another.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
40
  static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
41
42
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
43
   * dma_fence_context_alloc - allocate an array of fence contexts
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
44
45
46
47
48
   * @num:	[in]	amount of contexts to allocate
   *
   * This function will return the first index of the number of fences allocated.
   * The fence context is used for setting fence->context to a unique number.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
49
  u64 dma_fence_context_alloc(unsigned num)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
50
  {
6ce31263c   Daniel Vetter   dma-fence: Don't ...
51
  	WARN_ON(!num);
f54d18670   Chris Wilson   dma-buf: Rename s...
52
  	return atomic64_add_return(num, &dma_fence_context_counter) - num;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
53
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
54
  EXPORT_SYMBOL(dma_fence_context_alloc);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
55
56
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
57
   * dma_fence_signal_locked - signal completion of a fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
58
59
60
   * @fence: the fence to signal
   *
   * Signal completion for software callbacks on a fence, this will unblock
f54d18670   Chris Wilson   dma-buf: Rename s...
61
62
   * dma_fence_wait() calls and run all the callbacks added with
   * dma_fence_add_callback(). Can be called multiple times, but since a fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
63
64
65
   * can only go from unsignaled to signaled state, it will only be effective
   * the first time.
   *
f54d18670   Chris Wilson   dma-buf: Rename s...
66
   * Unlike dma_fence_signal, this function must be called with fence->lock held.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
67
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
68
  int dma_fence_signal_locked(struct dma_fence *fence)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
69
  {
f54d18670   Chris Wilson   dma-buf: Rename s...
70
  	struct dma_fence_cb *cur, *tmp;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
71
  	int ret = 0;
78010cd97   Rob Clark   dma-buf/fence: ad...
72
  	lockdep_assert_held(fence->lock);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
73
74
  	if (WARN_ON(!fence))
  		return -EINVAL;
f54d18670   Chris Wilson   dma-buf: Rename s...
75
  	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
76
77
78
  		ret = -EINVAL;
  
  		/*
f54d18670   Chris Wilson   dma-buf: Rename s...
79
  		 * we might have raced with the unlocked dma_fence_signal,
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
80
81
  		 * still run through all callbacks
  		 */
76250f2b7   Chris Wilson   dma-buf/fence: Av...
82
83
84
  	} else {
  		fence->timestamp = ktime_get();
  		set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
f54d18670   Chris Wilson   dma-buf: Rename s...
85
  		trace_dma_fence_signaled(fence);
76250f2b7   Chris Wilson   dma-buf/fence: Av...
86
  	}
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
87
88
89
90
91
92
93
  
  	list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
  		list_del_init(&cur->node);
  		cur->func(fence, cur);
  	}
  	return ret;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
94
  EXPORT_SYMBOL(dma_fence_signal_locked);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
95
96
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
97
   * dma_fence_signal - signal completion of a fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
98
99
100
   * @fence: the fence to signal
   *
   * Signal completion for software callbacks on a fence, this will unblock
f54d18670   Chris Wilson   dma-buf: Rename s...
101
102
   * dma_fence_wait() calls and run all the callbacks added with
   * dma_fence_add_callback(). Can be called multiple times, but since a fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
103
104
105
   * can only go from unsignaled to signaled state, it will only be effective
   * the first time.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
106
  int dma_fence_signal(struct dma_fence *fence)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
107
108
109
110
111
  {
  	unsigned long flags;
  
  	if (!fence)
  		return -EINVAL;
f54d18670   Chris Wilson   dma-buf: Rename s...
112
  	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
113
  		return -EINVAL;
76250f2b7   Chris Wilson   dma-buf/fence: Av...
114
115
  	fence->timestamp = ktime_get();
  	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
f54d18670   Chris Wilson   dma-buf: Rename s...
116
  	trace_dma_fence_signaled(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
117

f54d18670   Chris Wilson   dma-buf: Rename s...
118
119
  	if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
  		struct dma_fence_cb *cur, *tmp;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
120
121
122
123
124
125
126
127
128
129
  
  		spin_lock_irqsave(fence->lock, flags);
  		list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
  			list_del_init(&cur->node);
  			cur->func(fence, cur);
  		}
  		spin_unlock_irqrestore(fence->lock, flags);
  	}
  	return 0;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
130
  EXPORT_SYMBOL(dma_fence_signal);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
131
132
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
133
   * dma_fence_wait_timeout - sleep until the fence gets signaled
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
   * or until timeout elapses
   * @fence:	[in]	the fence to wait on
   * @intr:	[in]	if true, do an interruptible wait
   * @timeout:	[in]	timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
   *
   * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
   * remaining timeout in jiffies on success. Other error values may be
   * returned on custom implementations.
   *
   * Performs a synchronous wait on this fence. It is assumed the caller
   * directly or indirectly (buf-mgr between reservation and committing)
   * holds a reference to the fence, otherwise the fence might be
   * freed before return, resulting in undefined behavior.
   */
  signed long
f54d18670   Chris Wilson   dma-buf: Rename s...
149
  dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
150
151
152
153
154
  {
  	signed long ret;
  
  	if (WARN_ON(timeout < 0))
  		return -EINVAL;
f54d18670   Chris Wilson   dma-buf: Rename s...
155
  	trace_dma_fence_wait_start(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
156
  	ret = fence->ops->wait(fence, intr, timeout);
f54d18670   Chris Wilson   dma-buf: Rename s...
157
  	trace_dma_fence_wait_end(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
158
159
  	return ret;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
160
  EXPORT_SYMBOL(dma_fence_wait_timeout);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
161

f54d18670   Chris Wilson   dma-buf: Rename s...
162
  void dma_fence_release(struct kref *kref)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
163
  {
f54d18670   Chris Wilson   dma-buf: Rename s...
164
165
  	struct dma_fence *fence =
  		container_of(kref, struct dma_fence, refcount);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
166

f54d18670   Chris Wilson   dma-buf: Rename s...
167
  	trace_dma_fence_destroy(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
168

6ce31263c   Daniel Vetter   dma-fence: Don't ...
169
  	WARN_ON(!list_empty(&fence->cb_list));
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
170
171
172
173
  
  	if (fence->ops->release)
  		fence->ops->release(fence);
  	else
f54d18670   Chris Wilson   dma-buf: Rename s...
174
  		dma_fence_free(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
175
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
176
  EXPORT_SYMBOL(dma_fence_release);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
177

f54d18670   Chris Wilson   dma-buf: Rename s...
178
  void dma_fence_free(struct dma_fence *fence)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
179
  {
3c3b177a9   Maarten Lankhorst   reservation: add ...
180
  	kfree_rcu(fence, rcu);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
181
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
182
  EXPORT_SYMBOL(dma_fence_free);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
183
184
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
185
   * dma_fence_enable_sw_signaling - enable signaling on fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
186
187
188
189
190
   * @fence:	[in]	the fence to enable
   *
   * this will request for sw signaling to be enabled, to make the fence
   * complete as soon as possible
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
191
  void dma_fence_enable_sw_signaling(struct dma_fence *fence)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
192
193
  {
  	unsigned long flags;
f54d18670   Chris Wilson   dma-buf: Rename s...
194
195
196
197
  	if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  			      &fence->flags) &&
  	    !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  		trace_dma_fence_enable_signal(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
198
199
200
201
  
  		spin_lock_irqsave(fence->lock, flags);
  
  		if (!fence->ops->enable_signaling(fence))
f54d18670   Chris Wilson   dma-buf: Rename s...
202
  			dma_fence_signal_locked(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
203
204
205
206
  
  		spin_unlock_irqrestore(fence->lock, flags);
  	}
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
207
  EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
208
209
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
210
   * dma_fence_add_callback - add a callback to be called when the fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
211
212
213
214
215
   * is signaled
   * @fence:	[in]	the fence to wait on
   * @cb:		[in]	the callback to register
   * @func:	[in]	the function to call
   *
f54d18670   Chris Wilson   dma-buf: Rename s...
216
   * cb will be initialized by dma_fence_add_callback, no initialization
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
217
218
219
220
221
222
223
224
   * by the caller is required. Any number of callbacks can be registered
   * to a fence, but a callback can only be registered to one fence at a time.
   *
   * Note that the callback can be called from an atomic context.  If
   * fence is already signaled, this function will return -ENOENT (and
   * *not* call the callback)
   *
   * Add a software callback to the fence. Same restrictions apply to
f54d18670   Chris Wilson   dma-buf: Rename s...
225
   * refcount as it does to dma_fence_wait, however the caller doesn't need to
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
226
227
   * keep a refcount to fence afterwards: when software access is enabled,
   * the creator of the fence is required to keep the fence alive until
f54d18670   Chris Wilson   dma-buf: Rename s...
228
   * after it signals with dma_fence_signal. The callback itself can be called
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
229
230
   * from irq context.
   *
f642de16c   Gustavo Padovan   dma-buf/dma-fence...
231
232
   * Returns 0 in case of success, -ENOENT if the fence is already signaled
   * and -EINVAL in case of error.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
233
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
234
235
  int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
  			   dma_fence_func_t func)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
236
237
238
239
240
241
242
  {
  	unsigned long flags;
  	int ret = 0;
  	bool was_set;
  
  	if (WARN_ON(!fence || !func))
  		return -EINVAL;
f54d18670   Chris Wilson   dma-buf: Rename s...
243
  	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
244
245
246
247
248
  		INIT_LIST_HEAD(&cb->node);
  		return -ENOENT;
  	}
  
  	spin_lock_irqsave(fence->lock, flags);
f54d18670   Chris Wilson   dma-buf: Rename s...
249
250
  	was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  				   &fence->flags);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
251

f54d18670   Chris Wilson   dma-buf: Rename s...
252
  	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
253
254
  		ret = -ENOENT;
  	else if (!was_set) {
f54d18670   Chris Wilson   dma-buf: Rename s...
255
  		trace_dma_fence_enable_signal(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
256
257
  
  		if (!fence->ops->enable_signaling(fence)) {
f54d18670   Chris Wilson   dma-buf: Rename s...
258
  			dma_fence_signal_locked(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
259
260
261
262
263
264
265
266
267
268
269
270
271
  			ret = -ENOENT;
  		}
  	}
  
  	if (!ret) {
  		cb->func = func;
  		list_add_tail(&cb->node, &fence->cb_list);
  	} else
  		INIT_LIST_HEAD(&cb->node);
  	spin_unlock_irqrestore(fence->lock, flags);
  
  	return ret;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
272
  EXPORT_SYMBOL(dma_fence_add_callback);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
273
274
  
  /**
d6c99f4bf   Chris Wilson   dma-fence: Wrap q...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
   * dma_fence_get_status - returns the status upon completion
   * @fence: [in]	the dma_fence to query
   *
   * This wraps dma_fence_get_status_locked() to return the error status
   * condition on a signaled fence. See dma_fence_get_status_locked() for more
   * details.
   *
   * Returns 0 if the fence has not yet been signaled, 1 if the fence has
   * been signaled without an error condition, or a negative error code
   * if the fence has been completed in err.
   */
  int dma_fence_get_status(struct dma_fence *fence)
  {
  	unsigned long flags;
  	int status;
  
  	spin_lock_irqsave(fence->lock, flags);
  	status = dma_fence_get_status_locked(fence);
  	spin_unlock_irqrestore(fence->lock, flags);
  
  	return status;
  }
  EXPORT_SYMBOL(dma_fence_get_status);
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
300
   * dma_fence_remove_callback - remove a callback from the signaling list
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
301
302
303
304
   * @fence:	[in]	the fence to wait on
   * @cb:		[in]	the callback to remove
   *
   * Remove a previously queued callback from the fence. This function returns
f353d71f7   Masanari Iida   treewide: Fix typ...
305
   * true if the callback is successfully removed, or false if the fence has
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
306
307
308
309
310
311
312
313
314
   * already been signaled.
   *
   * *WARNING*:
   * Cancelling a callback should only be done if you really know what you're
   * doing, since deadlocks and race conditions could occur all too easily. For
   * this reason, it should only ever be done on hardware lockup recovery,
   * with a reference held to the fence.
   */
  bool
f54d18670   Chris Wilson   dma-buf: Rename s...
315
  dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  {
  	unsigned long flags;
  	bool ret;
  
  	spin_lock_irqsave(fence->lock, flags);
  
  	ret = !list_empty(&cb->node);
  	if (ret)
  		list_del_init(&cb->node);
  
  	spin_unlock_irqrestore(fence->lock, flags);
  
  	return ret;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
330
  EXPORT_SYMBOL(dma_fence_remove_callback);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
331
332
  
  struct default_wait_cb {
f54d18670   Chris Wilson   dma-buf: Rename s...
333
  	struct dma_fence_cb base;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
334
335
336
337
  	struct task_struct *task;
  };
  
  static void
f54d18670   Chris Wilson   dma-buf: Rename s...
338
  dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
339
340
341
342
343
344
345
346
  {
  	struct default_wait_cb *wait =
  		container_of(cb, struct default_wait_cb, base);
  
  	wake_up_state(wait->task, TASK_NORMAL);
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
347
   * dma_fence_default_wait - default sleep until the fence gets signaled
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
348
349
350
351
352
353
   * or until timeout elapses
   * @fence:	[in]	the fence to wait on
   * @intr:	[in]	if true, do an interruptible wait
   * @timeout:	[in]	timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
   *
   * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
bcc004b62   Alex Deucher   dma-buf/fence: ma...
354
355
356
   * remaining timeout in jiffies on success. If timeout is zero the value one is
   * returned if the fence is already signaled for consistency with other
   * functions taking a jiffies timeout.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
357
358
   */
  signed long
f54d18670   Chris Wilson   dma-buf: Rename s...
359
  dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
360
361
362
  {
  	struct default_wait_cb cb;
  	unsigned long flags;
bcc004b62   Alex Deucher   dma-buf/fence: ma...
363
  	signed long ret = timeout ? timeout : 1;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
364
  	bool was_set;
f54d18670   Chris Wilson   dma-buf: Rename s...
365
  	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
bcc004b62   Alex Deucher   dma-buf/fence: ma...
366
  		return ret;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
367
368
369
370
371
372
373
  
  	spin_lock_irqsave(fence->lock, flags);
  
  	if (intr && signal_pending(current)) {
  		ret = -ERESTARTSYS;
  		goto out;
  	}
f54d18670   Chris Wilson   dma-buf: Rename s...
374
375
  	was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  				   &fence->flags);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
376

f54d18670   Chris Wilson   dma-buf: Rename s...
377
  	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
378
379
380
  		goto out;
  
  	if (!was_set) {
f54d18670   Chris Wilson   dma-buf: Rename s...
381
  		trace_dma_fence_enable_signal(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
382
383
  
  		if (!fence->ops->enable_signaling(fence)) {
f54d18670   Chris Wilson   dma-buf: Rename s...
384
  			dma_fence_signal_locked(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
385
386
387
  			goto out;
  		}
  	}
03c0c5f66   Andres Rodriguez   dma-buf: avoid sc...
388
389
390
391
  	if (!timeout) {
  		ret = 0;
  		goto out;
  	}
f54d18670   Chris Wilson   dma-buf: Rename s...
392
  	cb.base.func = dma_fence_default_wait_cb;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
393
394
  	cb.task = current;
  	list_add(&cb.base.node, &fence->cb_list);
f54d18670   Chris Wilson   dma-buf: Rename s...
395
  	while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
  		if (intr)
  			__set_current_state(TASK_INTERRUPTIBLE);
  		else
  			__set_current_state(TASK_UNINTERRUPTIBLE);
  		spin_unlock_irqrestore(fence->lock, flags);
  
  		ret = schedule_timeout(ret);
  
  		spin_lock_irqsave(fence->lock, flags);
  		if (ret > 0 && intr && signal_pending(current))
  			ret = -ERESTARTSYS;
  	}
  
  	if (!list_empty(&cb.base.node))
  		list_del(&cb.base.node);
  	__set_current_state(TASK_RUNNING);
  
  out:
  	spin_unlock_irqrestore(fence->lock, flags);
  	return ret;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
417
  EXPORT_SYMBOL(dma_fence_default_wait);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
418

a519435a9   Christian König   dma-buf/fence: ad...
419
  static bool
7392b4bb7   monk.liu   dma-buf: return i...
420
421
  dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
  			    uint32_t *idx)
a519435a9   Christian König   dma-buf/fence: ad...
422
423
424
425
  {
  	int i;
  
  	for (i = 0; i < count; ++i) {
f54d18670   Chris Wilson   dma-buf: Rename s...
426
  		struct dma_fence *fence = fences[i];
7392b4bb7   monk.liu   dma-buf: return i...
427
428
429
  		if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  			if (idx)
  				*idx = i;
a519435a9   Christian König   dma-buf/fence: ad...
430
  			return true;
7392b4bb7   monk.liu   dma-buf: return i...
431
  		}
a519435a9   Christian König   dma-buf/fence: ad...
432
433
434
435
436
  	}
  	return false;
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
437
   * dma_fence_wait_any_timeout - sleep until any fence gets signaled
a519435a9   Christian König   dma-buf/fence: ad...
438
439
440
441
442
   * or until timeout elapses
   * @fences:	[in]	array of fences to wait on
   * @count:	[in]	number of fences to wait on
   * @intr:	[in]	if true, do an interruptible wait
   * @timeout:	[in]	timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
7392b4bb7   monk.liu   dma-buf: return i...
443
444
   * @idx:       [out]	the first signaled fence index, meaningful only on
   *			positive return
a519435a9   Christian König   dma-buf/fence: ad...
445
446
447
448
449
450
451
452
453
454
   *
   * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
   * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
   * on success.
   *
   * Synchronous waits for the first fence in the array to be signaled. The
   * caller needs to hold a reference to all fences in the array, otherwise a
   * fence might be freed before return, resulting in undefined behavior.
   */
  signed long
f54d18670   Chris Wilson   dma-buf: Rename s...
455
  dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
7392b4bb7   monk.liu   dma-buf: return i...
456
  			   bool intr, signed long timeout, uint32_t *idx)
a519435a9   Christian König   dma-buf/fence: ad...
457
458
459
460
461
462
463
464
465
466
  {
  	struct default_wait_cb *cb;
  	signed long ret = timeout;
  	unsigned i;
  
  	if (WARN_ON(!fences || !count || timeout < 0))
  		return -EINVAL;
  
  	if (timeout == 0) {
  		for (i = 0; i < count; ++i)
7392b4bb7   monk.liu   dma-buf: return i...
467
468
469
  			if (dma_fence_is_signaled(fences[i])) {
  				if (idx)
  					*idx = i;
a519435a9   Christian König   dma-buf/fence: ad...
470
  				return 1;
7392b4bb7   monk.liu   dma-buf: return i...
471
  			}
a519435a9   Christian König   dma-buf/fence: ad...
472
473
474
475
476
477
478
479
480
481
482
  
  		return 0;
  	}
  
  	cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
  	if (cb == NULL) {
  		ret = -ENOMEM;
  		goto err_free_cb;
  	}
  
  	for (i = 0; i < count; ++i) {
f54d18670   Chris Wilson   dma-buf: Rename s...
483
  		struct dma_fence *fence = fences[i];
a519435a9   Christian König   dma-buf/fence: ad...
484

f54d18670   Chris Wilson   dma-buf: Rename s...
485
  		if (fence->ops->wait != dma_fence_default_wait) {
a519435a9   Christian König   dma-buf/fence: ad...
486
487
488
489
490
  			ret = -EINVAL;
  			goto fence_rm_cb;
  		}
  
  		cb[i].task = current;
f54d18670   Chris Wilson   dma-buf: Rename s...
491
492
  		if (dma_fence_add_callback(fence, &cb[i].base,
  					   dma_fence_default_wait_cb)) {
a519435a9   Christian König   dma-buf/fence: ad...
493
  			/* This fence is already signaled */
7392b4bb7   monk.liu   dma-buf: return i...
494
495
  			if (idx)
  				*idx = i;
a519435a9   Christian König   dma-buf/fence: ad...
496
497
498
499
500
501
502
503
504
  			goto fence_rm_cb;
  		}
  	}
  
  	while (ret > 0) {
  		if (intr)
  			set_current_state(TASK_INTERRUPTIBLE);
  		else
  			set_current_state(TASK_UNINTERRUPTIBLE);
7392b4bb7   monk.liu   dma-buf: return i...
505
  		if (dma_fence_test_signaled_any(fences, count, idx))
a519435a9   Christian König   dma-buf/fence: ad...
506
507
508
509
510
511
512
513
514
515
516
517
  			break;
  
  		ret = schedule_timeout(ret);
  
  		if (ret > 0 && intr && signal_pending(current))
  			ret = -ERESTARTSYS;
  	}
  
  	__set_current_state(TASK_RUNNING);
  
  fence_rm_cb:
  	while (i-- > 0)
f54d18670   Chris Wilson   dma-buf: Rename s...
518
  		dma_fence_remove_callback(fences[i], &cb[i].base);
a519435a9   Christian König   dma-buf/fence: ad...
519
520
521
522
523
524
  
  err_free_cb:
  	kfree(cb);
  
  	return ret;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
525
  EXPORT_SYMBOL(dma_fence_wait_any_timeout);
a519435a9   Christian König   dma-buf/fence: ad...
526

e941759c7   Maarten Lankhorst   fence: dma-buf cr...
527
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
528
   * dma_fence_init - Initialize a custom fence.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
529
   * @fence:	[in]	the fence to initialize
f54d18670   Chris Wilson   dma-buf: Rename s...
530
   * @ops:	[in]	the dma_fence_ops for operations on this fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
531
532
533
534
535
536
   * @lock:	[in]	the irqsafe spinlock to use for locking this fence
   * @context:	[in]	the execution context this fence is run on
   * @seqno:	[in]	a linear increasing sequence number for this context
   *
   * Initializes an allocated fence, the caller doesn't have to keep its
   * refcount after committing with this fence, but it will need to hold a
f54d18670   Chris Wilson   dma-buf: Rename s...
537
   * refcount again if dma_fence_ops.enable_signaling gets called. This can
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
538
539
540
   * be used for other implementing other types of fence.
   *
   * context and seqno are used for easy comparison between fences, allowing
f54d18670   Chris Wilson   dma-buf: Rename s...
541
   * to check which fence is later by simply using dma_fence_later.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
542
543
   */
  void
f54d18670   Chris Wilson   dma-buf: Rename s...
544
545
  dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
  	       spinlock_t *lock, u64 context, unsigned seqno)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
546
547
548
549
550
551
552
553
554
555
556
557
  {
  	BUG_ON(!lock);
  	BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
  	       !ops->get_driver_name || !ops->get_timeline_name);
  
  	kref_init(&fence->refcount);
  	fence->ops = ops;
  	INIT_LIST_HEAD(&fence->cb_list);
  	fence->lock = lock;
  	fence->context = context;
  	fence->seqno = seqno;
  	fence->flags = 0UL;
a009e975d   Chris Wilson   dma-fence: Introd...
558
  	fence->error = 0;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
559

f54d18670   Chris Wilson   dma-buf: Rename s...
560
  	trace_dma_fence_init(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
561
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
562
  EXPORT_SYMBOL(dma_fence_init);