This source file includes following definitions.
- dma_fence_stub_get_name
- dma_fence_get_stub
- dma_fence_context_alloc
- dma_fence_signal_locked
- dma_fence_signal
- dma_fence_wait_timeout
- dma_fence_release
- dma_fence_free
- dma_fence_enable_sw_signaling
- dma_fence_add_callback
- dma_fence_get_status
- dma_fence_remove_callback
- dma_fence_default_wait_cb
- dma_fence_default_wait
- dma_fence_test_signaled_any
- dma_fence_wait_any_timeout
- dma_fence_init
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include <linux/atomic.h>
16 #include <linux/dma-fence.h>
17 #include <linux/sched/signal.h>
18
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/dma_fence.h>
21
22 EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
23 EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
24 EXPORT_TRACEPOINT_SYMBOL(dma_fence_signaled);
25
26 static DEFINE_SPINLOCK(dma_fence_stub_lock);
27 static struct dma_fence dma_fence_stub;
28
29
30
31
32
33
34
35 static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1);
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 static const char *dma_fence_stub_get_name(struct dma_fence *fence)
68 {
69 return "stub";
70 }
71
72 static const struct dma_fence_ops dma_fence_stub_ops = {
73 .get_driver_name = dma_fence_stub_get_name,
74 .get_timeline_name = dma_fence_stub_get_name,
75 };
76
77
78
79
80
81
82 struct dma_fence *dma_fence_get_stub(void)
83 {
84 spin_lock(&dma_fence_stub_lock);
85 if (!dma_fence_stub.ops) {
86 dma_fence_init(&dma_fence_stub,
87 &dma_fence_stub_ops,
88 &dma_fence_stub_lock,
89 0, 0);
90 dma_fence_signal_locked(&dma_fence_stub);
91 }
92 spin_unlock(&dma_fence_stub_lock);
93
94 return dma_fence_get(&dma_fence_stub);
95 }
96 EXPORT_SYMBOL(dma_fence_get_stub);
97
98
99
100
101
102
103
104
105
106 u64 dma_fence_context_alloc(unsigned num)
107 {
108 WARN_ON(!num);
109 return atomic64_add_return(num, &dma_fence_context_counter) - num;
110 }
111 EXPORT_SYMBOL(dma_fence_context_alloc);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 int dma_fence_signal_locked(struct dma_fence *fence)
130 {
131 struct dma_fence_cb *cur, *tmp;
132 struct list_head cb_list;
133
134 lockdep_assert_held(fence->lock);
135
136 if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
137 &fence->flags)))
138 return -EINVAL;
139
140
141 list_replace(&fence->cb_list, &cb_list);
142
143 fence->timestamp = ktime_get();
144 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
145 trace_dma_fence_signaled(fence);
146
147 list_for_each_entry_safe(cur, tmp, &cb_list, node) {
148 INIT_LIST_HEAD(&cur->node);
149 cur->func(fence, cur);
150 }
151
152 return 0;
153 }
154 EXPORT_SYMBOL(dma_fence_signal_locked);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 int dma_fence_signal(struct dma_fence *fence)
170 {
171 unsigned long flags;
172 int ret;
173
174 if (!fence)
175 return -EINVAL;
176
177 spin_lock_irqsave(fence->lock, flags);
178 ret = dma_fence_signal_locked(fence);
179 spin_unlock_irqrestore(fence->lock, flags);
180
181 return ret;
182 }
183 EXPORT_SYMBOL(dma_fence_signal);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 signed long
204 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
205 {
206 signed long ret;
207
208 if (WARN_ON(timeout < 0))
209 return -EINVAL;
210
211 trace_dma_fence_wait_start(fence);
212 if (fence->ops->wait)
213 ret = fence->ops->wait(fence, intr, timeout);
214 else
215 ret = dma_fence_default_wait(fence, intr, timeout);
216 trace_dma_fence_wait_end(fence);
217 return ret;
218 }
219 EXPORT_SYMBOL(dma_fence_wait_timeout);
220
221
222
223
224
225
226
227
228 void dma_fence_release(struct kref *kref)
229 {
230 struct dma_fence *fence =
231 container_of(kref, struct dma_fence, refcount);
232
233 trace_dma_fence_destroy(fence);
234
235 if (WARN(!list_empty(&fence->cb_list) &&
236 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags),
237 "Fence %s:%s:%llx:%llx released with pending signals!\n",
238 fence->ops->get_driver_name(fence),
239 fence->ops->get_timeline_name(fence),
240 fence->context, fence->seqno)) {
241 unsigned long flags;
242
243
244
245
246
247
248
249
250 spin_lock_irqsave(fence->lock, flags);
251 fence->error = -EDEADLK;
252 dma_fence_signal_locked(fence);
253 spin_unlock_irqrestore(fence->lock, flags);
254 }
255
256 if (fence->ops->release)
257 fence->ops->release(fence);
258 else
259 dma_fence_free(fence);
260 }
261 EXPORT_SYMBOL(dma_fence_release);
262
263
264
265
266
267
268
269
270 void dma_fence_free(struct dma_fence *fence)
271 {
272 kfree_rcu(fence, rcu);
273 }
274 EXPORT_SYMBOL(dma_fence_free);
275
276
277
278
279
280
281
282
283
284 void dma_fence_enable_sw_signaling(struct dma_fence *fence)
285 {
286 unsigned long flags;
287
288 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
289 &fence->flags) &&
290 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) &&
291 fence->ops->enable_signaling) {
292 trace_dma_fence_enable_signal(fence);
293
294 spin_lock_irqsave(fence->lock, flags);
295
296 if (!fence->ops->enable_signaling(fence))
297 dma_fence_signal_locked(fence);
298
299 spin_unlock_irqrestore(fence->lock, flags);
300 }
301 }
302 EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
330 dma_fence_func_t func)
331 {
332 unsigned long flags;
333 int ret = 0;
334 bool was_set;
335
336 if (WARN_ON(!fence || !func))
337 return -EINVAL;
338
339 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
340 INIT_LIST_HEAD(&cb->node);
341 return -ENOENT;
342 }
343
344 spin_lock_irqsave(fence->lock, flags);
345
346 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
347 &fence->flags);
348
349 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
350 ret = -ENOENT;
351 else if (!was_set && fence->ops->enable_signaling) {
352 trace_dma_fence_enable_signal(fence);
353
354 if (!fence->ops->enable_signaling(fence)) {
355 dma_fence_signal_locked(fence);
356 ret = -ENOENT;
357 }
358 }
359
360 if (!ret) {
361 cb->func = func;
362 list_add_tail(&cb->node, &fence->cb_list);
363 } else
364 INIT_LIST_HEAD(&cb->node);
365 spin_unlock_irqrestore(fence->lock, flags);
366
367 return ret;
368 }
369 EXPORT_SYMBOL(dma_fence_add_callback);
370
371
372
373
374
375
376
377
378
379
380
381
382
383 int dma_fence_get_status(struct dma_fence *fence)
384 {
385 unsigned long flags;
386 int status;
387
388 spin_lock_irqsave(fence->lock, flags);
389 status = dma_fence_get_status_locked(fence);
390 spin_unlock_irqrestore(fence->lock, flags);
391
392 return status;
393 }
394 EXPORT_SYMBOL(dma_fence_get_status);
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 bool
415 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
416 {
417 unsigned long flags;
418 bool ret;
419
420 spin_lock_irqsave(fence->lock, flags);
421
422 ret = !list_empty(&cb->node);
423 if (ret)
424 list_del_init(&cb->node);
425
426 spin_unlock_irqrestore(fence->lock, flags);
427
428 return ret;
429 }
430 EXPORT_SYMBOL(dma_fence_remove_callback);
431
432 struct default_wait_cb {
433 struct dma_fence_cb base;
434 struct task_struct *task;
435 };
436
437 static void
438 dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
439 {
440 struct default_wait_cb *wait =
441 container_of(cb, struct default_wait_cb, base);
442
443 wake_up_state(wait->task, TASK_NORMAL);
444 }
445
446
447
448
449
450
451
452
453
454
455
456
457
458 signed long
459 dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
460 {
461 struct default_wait_cb cb;
462 unsigned long flags;
463 signed long ret = timeout ? timeout : 1;
464 bool was_set;
465
466 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
467 return ret;
468
469 spin_lock_irqsave(fence->lock, flags);
470
471 if (intr && signal_pending(current)) {
472 ret = -ERESTARTSYS;
473 goto out;
474 }
475
476 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
477 &fence->flags);
478
479 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
480 goto out;
481
482 if (!was_set && fence->ops->enable_signaling) {
483 trace_dma_fence_enable_signal(fence);
484
485 if (!fence->ops->enable_signaling(fence)) {
486 dma_fence_signal_locked(fence);
487 goto out;
488 }
489 }
490
491 if (!timeout) {
492 ret = 0;
493 goto out;
494 }
495
496 cb.base.func = dma_fence_default_wait_cb;
497 cb.task = current;
498 list_add(&cb.base.node, &fence->cb_list);
499
500 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
501 if (intr)
502 __set_current_state(TASK_INTERRUPTIBLE);
503 else
504 __set_current_state(TASK_UNINTERRUPTIBLE);
505 spin_unlock_irqrestore(fence->lock, flags);
506
507 ret = schedule_timeout(ret);
508
509 spin_lock_irqsave(fence->lock, flags);
510 if (ret > 0 && intr && signal_pending(current))
511 ret = -ERESTARTSYS;
512 }
513
514 if (!list_empty(&cb.base.node))
515 list_del(&cb.base.node);
516 __set_current_state(TASK_RUNNING);
517
518 out:
519 spin_unlock_irqrestore(fence->lock, flags);
520 return ret;
521 }
522 EXPORT_SYMBOL(dma_fence_default_wait);
523
524 static bool
525 dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
526 uint32_t *idx)
527 {
528 int i;
529
530 for (i = 0; i < count; ++i) {
531 struct dma_fence *fence = fences[i];
532 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
533 if (idx)
534 *idx = i;
535 return true;
536 }
537 }
538 return false;
539 }
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561 signed long
562 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
563 bool intr, signed long timeout, uint32_t *idx)
564 {
565 struct default_wait_cb *cb;
566 signed long ret = timeout;
567 unsigned i;
568
569 if (WARN_ON(!fences || !count || timeout < 0))
570 return -EINVAL;
571
572 if (timeout == 0) {
573 for (i = 0; i < count; ++i)
574 if (dma_fence_is_signaled(fences[i])) {
575 if (idx)
576 *idx = i;
577 return 1;
578 }
579
580 return 0;
581 }
582
583 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
584 if (cb == NULL) {
585 ret = -ENOMEM;
586 goto err_free_cb;
587 }
588
589 for (i = 0; i < count; ++i) {
590 struct dma_fence *fence = fences[i];
591
592 cb[i].task = current;
593 if (dma_fence_add_callback(fence, &cb[i].base,
594 dma_fence_default_wait_cb)) {
595
596 if (idx)
597 *idx = i;
598 goto fence_rm_cb;
599 }
600 }
601
602 while (ret > 0) {
603 if (intr)
604 set_current_state(TASK_INTERRUPTIBLE);
605 else
606 set_current_state(TASK_UNINTERRUPTIBLE);
607
608 if (dma_fence_test_signaled_any(fences, count, idx))
609 break;
610
611 ret = schedule_timeout(ret);
612
613 if (ret > 0 && intr && signal_pending(current))
614 ret = -ERESTARTSYS;
615 }
616
617 __set_current_state(TASK_RUNNING);
618
619 fence_rm_cb:
620 while (i-- > 0)
621 dma_fence_remove_callback(fences[i], &cb[i].base);
622
623 err_free_cb:
624 kfree(cb);
625
626 return ret;
627 }
628 EXPORT_SYMBOL(dma_fence_wait_any_timeout);
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645 void
646 dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
647 spinlock_t *lock, u64 context, u64 seqno)
648 {
649 BUG_ON(!lock);
650 BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name);
651
652 kref_init(&fence->refcount);
653 fence->ops = ops;
654 INIT_LIST_HEAD(&fence->cb_list);
655 fence->lock = lock;
656 fence->context = context;
657 fence->seqno = seqno;
658 fence->flags = 0UL;
659 fence->error = 0;
660
661 trace_dma_fence_init(fence);
662 }
663 EXPORT_SYMBOL(dma_fence_init);