linux_dma_resv.c revision 1.5 1 /* $NetBSD: linux_dma_resv.c,v 1.5 2021/12/19 11:52:55 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_dma_resv.c,v 1.5 2021/12/19 11:52:55 riastradh Exp $");
34
35 #include <sys/param.h>
36 #include <sys/poll.h>
37 #include <sys/select.h>
38
39 #include <linux/dma-fence.h>
40 #include <linux/dma-resv.h>
41 #include <linux/seqlock.h>
42 #include <linux/ww_mutex.h>
43
44 DEFINE_WW_CLASS(reservation_ww_class __cacheline_aligned);
45
46 static struct dma_resv_list *
47 objlist_tryalloc(uint32_t n)
48 {
49 struct dma_resv_list *list;
50
51 list = kmem_alloc(offsetof(typeof(*list), shared[n]), KM_NOSLEEP);
52 if (list == NULL)
53 return NULL;
54 list->shared_max = n;
55
56 return list;
57 }
58
59 static void
60 objlist_free(struct dma_resv_list *list)
61 {
62 uint32_t n = list->shared_max;
63
64 kmem_free(list, offsetof(typeof(*list), shared[n]));
65 }
66
67 static void
68 objlist_free_cb(struct rcu_head *rcu)
69 {
70 struct dma_resv_list *list = container_of(rcu,
71 struct dma_resv_list, rol_rcu);
72
73 objlist_free(list);
74 }
75
76 static void
77 objlist_defer_free(struct dma_resv_list *list)
78 {
79
80 call_rcu(&list->rol_rcu, objlist_free_cb);
81 }
82
83 /*
84 * dma_resv_init(robj)
85 *
86 * Initialize a reservation object. Caller must later destroy it
87 * with dma_resv_fini.
88 */
89 void
90 dma_resv_init(struct dma_resv *robj)
91 {
92
93 ww_mutex_init(&robj->lock, &reservation_ww_class);
94 seqcount_init(&robj->seq);
95 robj->fence_excl = NULL;
96 robj->fence = NULL;
97 robj->robj_prealloc = NULL;
98 }
99
100 /*
101 * dma_resv_fini(robj)
102 *
103 * Destroy a reservation object, freeing any memory that had been
104 * allocated for it. Caller must have exclusive access to it.
105 */
106 void
107 dma_resv_fini(struct dma_resv *robj)
108 {
109 unsigned i;
110
111 if (robj->robj_prealloc)
112 objlist_free(robj->robj_prealloc);
113 if (robj->fence) {
114 for (i = 0; i < robj->fence->shared_count; i++)
115 dma_fence_put(robj->fence->shared[i]);
116 objlist_free(robj->fence);
117 }
118 if (robj->fence_excl)
119 dma_fence_put(robj->fence_excl);
120 ww_mutex_destroy(&robj->lock);
121 }
122
123 /*
124 * dma_resv_lock(robj, ctx)
125 *
126 * Acquire a reservation object's lock. Return 0 on success,
127 * -EALREADY if caller already holds it, -EDEADLK if a
128 * higher-priority owner holds it and the caller must back out and
129 * retry.
130 */
131 int
132 dma_resv_lock(struct dma_resv *robj,
133 struct ww_acquire_ctx *ctx)
134 {
135
136 return ww_mutex_lock(&robj->lock, ctx);
137 }
138
139 /*
140 * dma_resv_lock_slow(robj, ctx)
141 *
142 * Acquire a reservation object's lock. Caller must not hold
143 * this lock or any others -- this is to be used in slow paths
144 * after dma_resv_lock or dma_resv_lock_interruptible has failed
145 * and the caller has backed out all other locks.
146 */
147 void
148 dma_resv_lock_slow(struct dma_resv *robj,
149 struct ww_acquire_ctx *ctx)
150 {
151
152 ww_mutex_lock_slow(&robj->lock, ctx);
153 }
154
155 /*
156 * dma_resv_lock_interruptible(robj, ctx)
157 *
158 * Acquire a reservation object's lock. Return 0 on success,
159 * -EALREADY if caller already holds it, -EDEADLK if a
160 * higher-priority owner holds it and the caller must back out and
161 * retry, -ERESTART/-EINTR if interrupted.
162 */
163 int
164 dma_resv_lock_interruptible(struct dma_resv *robj,
165 struct ww_acquire_ctx *ctx)
166 {
167
168 return ww_mutex_lock_interruptible(&robj->lock, ctx);
169 }
170
171 /*
172 * dma_resv_lock_slow_interruptible(robj, ctx)
173 *
174 * Acquire a reservation object's lock. Caller must not hold
175 * this lock or any others -- this is to be used in slow paths
176 * after dma_resv_lock or dma_resv_lock_interruptible has failed
177 * and the caller has backed out all other locks. Return 0 on
178 * success, -ERESTART/-EINTR if interrupted.
179 */
180 int
181 dma_resv_lock_slow_interruptible(struct dma_resv *robj,
182 struct ww_acquire_ctx *ctx)
183 {
184
185 return ww_mutex_lock_slow_interruptible(&robj->lock, ctx);
186 }
187
188 /*
189 * dma_resv_trylock(robj)
190 *
191 * Try to acquire a reservation object's lock without blocking.
192 * Return true on success, false on failure.
193 */
194 bool
195 dma_resv_trylock(struct dma_resv *robj)
196 {
197
198 return ww_mutex_trylock(&robj->lock);
199 }
200
201 /*
202 * dma_resv_locking_ctx(robj)
203 *
204 * Return a pointer to the ww_acquire_ctx used by the owner of
205 * the reservation object's lock, or NULL if it is either not
206 * owned or if it is locked without context.
207 */
208 struct ww_acquire_ctx *
209 dma_resv_locking_ctx(struct dma_resv *robj)
210 {
211
212 return ww_mutex_locking_ctx(&robj->lock);
213 }
214
215 /*
216 * dma_resv_unlock(robj)
217 *
218 * Release a reservation object's lock.
219 */
220 void
221 dma_resv_unlock(struct dma_resv *robj)
222 {
223
224 return ww_mutex_unlock(&robj->lock);
225 }
226
227 /*
228 * dma_resv_held(robj)
229 *
230 * True if robj is locked.
231 */
232 bool
233 dma_resv_held(struct dma_resv *robj)
234 {
235
236 return ww_mutex_is_locked(&robj->lock);
237 }
238
239 /*
240 * dma_resv_assert_held(robj)
241 *
242 * Panic if robj is not held, in DIAGNOSTIC builds.
243 */
244 void
245 dma_resv_assert_held(struct dma_resv *robj)
246 {
247
248 KASSERT(dma_resv_held(robj));
249 }
250
251 /*
252 * dma_resv_get_excl(robj)
253 *
254 * Return a pointer to the exclusive fence of the reservation
255 * object robj.
256 *
257 * Caller must have robj locked.
258 */
259 struct dma_fence *
260 dma_resv_get_excl(struct dma_resv *robj)
261 {
262
263 KASSERT(dma_resv_held(robj));
264 return robj->fence_excl;
265 }
266
267 /*
268 * dma_resv_get_list(robj)
269 *
270 * Return a pointer to the shared fence list of the reservation
271 * object robj.
272 *
273 * Caller must have robj locked.
274 */
275 struct dma_resv_list *
276 dma_resv_get_list(struct dma_resv *robj)
277 {
278
279 KASSERT(dma_resv_held(robj));
280 return robj->fence;
281 }
282
283 /*
284 * dma_resv_reserve_shared(robj)
285 *
286 * Reserve space in robj to add a shared fence. To be used only
287 * once before calling dma_resv_add_shared_fence.
288 *
289 * Caller must have robj locked.
290 *
291 * Internally, we start with room for four entries and double if
292 * we don't have enough. This is not guaranteed.
293 */
294 int
295 dma_resv_reserve_shared(struct dma_resv *robj, unsigned int num_fences)
296 {
297 struct dma_resv_list *list, *prealloc;
298 uint32_t n, nalloc;
299
300 KASSERT(dma_resv_held(robj));
301 KASSERT(num_fences == 1);
302
303 list = robj->fence;
304 prealloc = robj->robj_prealloc;
305
306 /* If there's an existing list, check it for space. */
307 if (list) {
308 /* If there's too many already, give up. */
309 if (list->shared_count == UINT32_MAX)
310 return -ENOMEM;
311
312 /* Add one more. */
313 n = list->shared_count + 1;
314
315 /* If there's enough for one more, we're done. */
316 if (n <= list->shared_max)
317 return 0;
318 } else {
319 /* No list already. We need space for 1. */
320 n = 1;
321 }
322
323 /* If not, maybe there's a preallocated list ready. */
324 if (prealloc != NULL) {
325 /* If there's enough room in it, stop here. */
326 if (n <= prealloc->shared_max)
327 return 0;
328
329 /* Try to double its capacity. */
330 nalloc = n > UINT32_MAX/2 ? UINT32_MAX : 2*n;
331 prealloc = objlist_tryalloc(nalloc);
332 if (prealloc == NULL)
333 return -ENOMEM;
334
335 /* Swap the new preallocated list and free the old one. */
336 objlist_free(robj->robj_prealloc);
337 robj->robj_prealloc = prealloc;
338 } else {
339 /* Start with some spare. */
340 nalloc = n > UINT32_MAX/2 ? UINT32_MAX : MAX(2*n, 4);
341 prealloc = objlist_tryalloc(nalloc);
342 if (prealloc == NULL)
343 return -ENOMEM;
344 /* Save the new preallocated list. */
345 robj->robj_prealloc = prealloc;
346 }
347
348 /* Success! */
349 return 0;
350 }
351
352 struct dma_resv_write_ticket {
353 };
354
355 /*
356 * dma_resv_write_begin(robj, ticket)
357 *
358 * Begin an atomic batch of writes to robj, and initialize opaque
359 * ticket for it. The ticket must be passed to
360 * dma_resv_write_commit to commit the writes.
361 *
362 * Caller must have robj locked.
363 *
364 * Implies membar_producer, i.e. store-before-store barrier. Does
365 * NOT serve as an acquire operation, however.
366 */
367 static void
368 dma_resv_write_begin(struct dma_resv *robj,
369 struct dma_resv_write_ticket *ticket)
370 {
371
372 KASSERT(dma_resv_held(robj));
373
374 write_seqcount_begin(&robj->seq);
375 }
376
377 /*
378 * dma_resv_write_commit(robj, ticket)
379 *
380 * Commit an atomic batch of writes to robj begun with the call to
381 * dma_resv_write_begin that returned ticket.
382 *
383 * Caller must have robj locked.
384 *
385 * Implies membar_producer, i.e. store-before-store barrier. Does
386 * NOT serve as a release operation, however.
387 */
388 static void
389 dma_resv_write_commit(struct dma_resv *robj,
390 struct dma_resv_write_ticket *ticket)
391 {
392
393 KASSERT(dma_resv_held(robj));
394
395 write_seqcount_end(&robj->seq);
396 }
397
398 struct dma_resv_read_ticket {
399 unsigned version;
400 };
401
402 /*
403 * dma_resv_read_begin(robj, ticket)
404 *
405 * Begin a read section, and initialize opaque ticket for it. The
406 * ticket must be passed to dma_resv_read_exit, and the
407 * caller must be prepared to retry reading if it fails.
408 */
409 static void
410 dma_resv_read_begin(const struct dma_resv *robj,
411 struct dma_resv_read_ticket *ticket)
412 {
413
414 ticket->version = read_seqcount_begin(&robj->seq);
415 }
416
417 /*
418 * dma_resv_read_valid(robj, ticket)
419 *
420 * Test whether the read sections are valid. Return true on
421 * success, or false on failure if the read ticket has been
422 * invalidated.
423 */
424 static bool
425 dma_resv_read_valid(const struct dma_resv *robj,
426 struct dma_resv_read_ticket *ticket)
427 {
428
429 return !read_seqcount_retry(&robj->seq, ticket->version);
430 }
431
432 /*
433 * dma_resv_add_excl_fence(robj, fence)
434 *
435 * Empty and release all of robj's shared fences, and clear and
436 * release its exclusive fence. If fence is nonnull, acquire a
437 * reference to it and save it as robj's exclusive fence.
438 *
439 * Caller must have robj locked.
440 */
441 void
442 dma_resv_add_excl_fence(struct dma_resv *robj,
443 struct dma_fence *fence)
444 {
445 struct dma_fence *old_fence = robj->fence_excl;
446 struct dma_resv_list *old_list = robj->fence;
447 uint32_t old_shared_count;
448 struct dma_resv_write_ticket ticket;
449
450 KASSERT(dma_resv_held(robj));
451
452 /*
453 * If we are setting rather than just removing a fence, acquire
454 * a reference for ourselves.
455 */
456 if (fence)
457 (void)dma_fence_get(fence);
458
459 /* If there are any shared fences, remember how many. */
460 if (old_list)
461 old_shared_count = old_list->shared_count;
462
463 /* Begin an update. */
464 dma_resv_write_begin(robj, &ticket);
465
466 /* Replace the fence and zero the shared count. */
467 robj->fence_excl = fence;
468 if (old_list)
469 old_list->shared_count = 0;
470
471 /* Commit the update. */
472 dma_resv_write_commit(robj, &ticket);
473
474 /* Release the old exclusive fence, if any. */
475 if (old_fence)
476 dma_fence_put(old_fence);
477
478 /* Release any old shared fences. */
479 if (old_list) {
480 while (old_shared_count--)
481 dma_fence_put(old_list->shared[old_shared_count]);
482 }
483 }
484
485 /*
486 * dma_resv_add_shared_fence(robj, fence)
487 *
488 * Acquire a reference to fence and add it to robj's shared list.
489 * If any fence was already added with the same context number,
490 * release it and replace it by this one.
491 *
492 * Caller must have robj locked, and must have preceded with a
493 * call to dma_resv_reserve_shared for each shared fence
494 * added.
495 */
496 void
497 dma_resv_add_shared_fence(struct dma_resv *robj,
498 struct dma_fence *fence)
499 {
500 struct dma_resv_list *list = robj->fence;
501 struct dma_resv_list *prealloc = robj->robj_prealloc;
502 struct dma_resv_write_ticket ticket;
503 struct dma_fence *replace = NULL;
504 uint32_t i;
505
506 KASSERT(dma_resv_held(robj));
507
508 /* Acquire a reference to the fence. */
509 KASSERT(fence != NULL);
510 (void)dma_fence_get(fence);
511
512 /* Check for a preallocated replacement list. */
513 if (prealloc == NULL) {
514 /*
515 * If there is no preallocated replacement list, then
516 * there must be room in the current list.
517 */
518 KASSERT(list != NULL);
519 KASSERT(list->shared_count < list->shared_max);
520
521 /* Begin an update. Implies membar_producer for fence. */
522 dma_resv_write_begin(robj, &ticket);
523
524 /* Find a fence with the same context number. */
525 for (i = 0; i < list->shared_count; i++) {
526 if (list->shared[i]->context == fence->context) {
527 replace = list->shared[i];
528 list->shared[i] = fence;
529 break;
530 }
531 }
532
533 /* If we didn't find one, add it at the end. */
534 if (i == list->shared_count)
535 list->shared[list->shared_count++] = fence;
536
537 /* Commit the update. */
538 dma_resv_write_commit(robj, &ticket);
539 } else {
540 /*
541 * There is a preallocated replacement list. There may
542 * not be a current list. If not, treat it as a zero-
543 * length list.
544 */
545 uint32_t shared_count = (list == NULL? 0 : list->shared_count);
546
547 /* There had better be room in the preallocated list. */
548 KASSERT(shared_count < prealloc->shared_max);
549
550 /*
551 * Copy the fences over, but replace if we find one
552 * with the same context number.
553 */
554 for (i = 0; i < shared_count; i++) {
555 if (replace == NULL &&
556 list->shared[i]->context == fence->context) {
557 replace = list->shared[i];
558 prealloc->shared[i] = fence;
559 } else {
560 prealloc->shared[i] = list->shared[i];
561 }
562 }
563 prealloc->shared_count = shared_count;
564
565 /* If we didn't find one, add it at the end. */
566 if (replace == NULL)
567 prealloc->shared[prealloc->shared_count++] = fence;
568
569 /*
570 * Now ready to replace the list. Begin an update.
571 * Implies membar_producer for fence and prealloc.
572 */
573 dma_resv_write_begin(robj, &ticket);
574
575 /* Replace the list. */
576 robj->fence = prealloc;
577 robj->robj_prealloc = NULL;
578
579 /* Commit the update. */
580 dma_resv_write_commit(robj, &ticket);
581
582 /*
583 * If there is an old list, free it when convenient.
584 * (We are not in a position at this point to sleep
585 * waiting for activity on all CPUs.)
586 */
587 if (list)
588 objlist_defer_free(list);
589 }
590
591 /* Release a fence if we replaced it. */
592 if (replace)
593 dma_fence_put(replace);
594 }
595
596 /*
597 * dma_resv_get_excl_rcu(robj)
598 *
599 * Note: Caller need not call this from an RCU read section.
600 */
601 struct dma_fence *
602 dma_resv_get_excl_rcu(const struct dma_resv *robj)
603 {
604 struct dma_fence *fence;
605
606 rcu_read_lock();
607 fence = dma_fence_get_rcu_safe(&robj->fence_excl);
608 rcu_read_unlock();
609
610 return fence;
611 }
612
613 /*
614 * dma_resv_get_fences_rcu(robj, fencep, nsharedp, sharedp)
615 */
616 int
617 dma_resv_get_fences_rcu(const struct dma_resv *robj,
618 struct dma_fence **fencep, unsigned *nsharedp, struct dma_fence ***sharedp)
619 {
620 const struct dma_resv_list *list;
621 struct dma_fence *fence;
622 struct dma_fence **shared = NULL;
623 unsigned shared_alloc, shared_count, i;
624 struct dma_resv_read_ticket ticket;
625
626 top:
627 /* Enter an RCU read section and get a read ticket. */
628 rcu_read_lock();
629 dma_resv_read_begin(robj, &ticket);
630
631 /* If there is a shared list, grab it. */
632 list = robj->fence;
633 __insn_barrier();
634 if (list) {
635 /* Make sure the content of the list has been published. */
636 membar_datadep_consumer();
637
638 /* Check whether we have a buffer. */
639 if (shared == NULL) {
640 /*
641 * We don't have a buffer yet. Try to allocate
642 * one without waiting.
643 */
644 shared_alloc = list->shared_max;
645 __insn_barrier();
646 shared = kcalloc(shared_alloc, sizeof(shared[0]),
647 GFP_NOWAIT);
648 if (shared == NULL) {
649 /*
650 * Couldn't do it immediately. Back
651 * out of RCU and allocate one with
652 * waiting.
653 */
654 rcu_read_unlock();
655 shared = kcalloc(shared_alloc,
656 sizeof(shared[0]), GFP_KERNEL);
657 if (shared == NULL)
658 return -ENOMEM;
659 goto top;
660 }
661 } else if (shared_alloc < list->shared_max) {
662 /*
663 * We have a buffer but it's too small. We're
664 * already racing in this case, so just back
665 * out and wait to allocate a bigger one.
666 */
667 shared_alloc = list->shared_max;
668 __insn_barrier();
669 rcu_read_unlock();
670 kfree(shared);
671 shared = kcalloc(shared_alloc, sizeof(shared[0]),
672 GFP_KERNEL);
673 if (shared == NULL)
674 return -ENOMEM;
675 }
676
677 /*
678 * We got a buffer large enough. Copy into the buffer
679 * and record the number of elements.
680 */
681 memcpy(shared, list->shared, shared_alloc * sizeof(shared[0]));
682 shared_count = list->shared_count;
683 } else {
684 /* No shared list: shared count is zero. */
685 shared_count = 0;
686 }
687
688 /* If there is an exclusive fence, grab it. */
689 fence = robj->fence_excl;
690 __insn_barrier();
691 if (fence) {
692 /* Make sure the content of the fence has been published. */
693 membar_datadep_consumer();
694 }
695
696 /*
697 * We are done reading from robj and list. Validate our
698 * parking ticket. If it's invalid, do not pass go and do not
699 * collect $200.
700 */
701 if (!dma_resv_read_valid(robj, &ticket))
702 goto restart;
703
704 /*
705 * Try to get a reference to the exclusive fence, if there is
706 * one. If we can't, start over.
707 */
708 if (fence) {
709 if (dma_fence_get_rcu(fence) == NULL)
710 goto restart;
711 }
712
713 /*
714 * Try to get a reference to all of the shared fences.
715 */
716 for (i = 0; i < shared_count; i++) {
717 if (dma_fence_get_rcu(shared[i]) == NULL)
718 goto put_restart;
719 }
720
721 /* Success! */
722 rcu_read_unlock();
723 *fencep = fence;
724 *nsharedp = shared_count;
725 *sharedp = shared;
726 return 0;
727
728 put_restart:
729 /* Back out. */
730 while (i --> 0) {
731 dma_fence_put(shared[i]);
732 shared[i] = NULL; /* paranoia */
733 }
734 if (fence) {
735 dma_fence_put(fence);
736 fence = NULL; /* paranoia */
737 }
738
739 restart:
740 rcu_read_unlock();
741 goto top;
742 }
743
744 /*
745 * dma_resv_copy_fences(dst, src)
746 *
747 * Copy the exclusive fence and all the shared fences from src to
748 * dst.
749 *
750 * Caller must have dst locked.
751 */
752 int
753 dma_resv_copy_fences(struct dma_resv *dst_robj,
754 const struct dma_resv *src_robj)
755 {
756 const struct dma_resv_list *src_list;
757 struct dma_resv_list *dst_list = NULL;
758 struct dma_resv_list *old_list;
759 struct dma_fence *fence = NULL;
760 struct dma_fence *old_fence;
761 uint32_t shared_count, i;
762 struct dma_resv_read_ticket read_ticket;
763 struct dma_resv_write_ticket write_ticket;
764
765 KASSERT(dma_resv_held(dst_robj));
766
767 top:
768 /* Enter an RCU read section and get a read ticket. */
769 rcu_read_lock();
770 dma_resv_read_begin(src_robj, &read_ticket);
771
772 /* Get the shared list. */
773 src_list = src_robj->fence;
774 __insn_barrier();
775 if (src_list) {
776 /* Make sure the content of the list has been published. */
777 membar_datadep_consumer();
778
779 /* Find out how long it is. */
780 shared_count = src_list->shared_count;
781
782 /*
783 * Make sure we saw a consistent snapshot of the list
784 * pointer and length.
785 */
786 if (!dma_resv_read_valid(src_robj, &read_ticket))
787 goto restart;
788
789 /* Allocate a new list. */
790 dst_list = objlist_tryalloc(shared_count);
791 if (dst_list == NULL)
792 return -ENOMEM;
793
794 /* Copy over all fences that are not yet signalled. */
795 dst_list->shared_count = 0;
796 for (i = 0; i < shared_count; i++) {
797 if ((fence = dma_fence_get_rcu(src_list->shared[i]))
798 != NULL)
799 goto restart;
800 if (dma_fence_is_signaled(fence)) {
801 dma_fence_put(fence);
802 fence = NULL;
803 continue;
804 }
805 dst_list->shared[dst_list->shared_count++] = fence;
806 fence = NULL;
807 }
808 }
809
810 /* Get the exclusive fence. */
811 fence = src_robj->fence_excl;
812 __insn_barrier();
813 if (fence != NULL) {
814 /* Make sure the content of the fence has been published. */
815 membar_datadep_consumer();
816
817 /*
818 * Make sure we saw a consistent snapshot of the fence.
819 *
820 * XXX I'm not actually sure this is necessary since
821 * pointer writes are supposed to be atomic.
822 */
823 if (!dma_resv_read_valid(src_robj, &read_ticket)) {
824 fence = NULL;
825 goto restart;
826 }
827
828 /*
829 * If it is going away, restart. Otherwise, acquire a
830 * reference to it.
831 */
832 if (!dma_fence_get_rcu(fence)) {
833 fence = NULL;
834 goto restart;
835 }
836 }
837
838 /* All done with src; exit the RCU read section. */
839 rcu_read_unlock();
840
841 /*
842 * We now have a snapshot of the shared and exclusive fences of
843 * src_robj and we have acquired references to them so they
844 * won't go away. Transfer them over to dst_robj, releasing
845 * references to any that were there.
846 */
847
848 /* Get the old shared and exclusive fences, if any. */
849 old_list = dst_robj->fence;
850 old_fence = dst_robj->fence_excl;
851
852 /* Begin an update. */
853 dma_resv_write_begin(dst_robj, &write_ticket);
854
855 /* Replace the fences. */
856 dst_robj->fence = dst_list;
857 dst_robj->fence_excl = fence;
858
859 /* Commit the update. */
860 dma_resv_write_commit(dst_robj, &write_ticket);
861
862 /* Release the old exclusive fence, if any. */
863 if (old_fence)
864 dma_fence_put(old_fence);
865
866 /* Release any old shared fences. */
867 if (old_list) {
868 for (i = old_list->shared_count; i --> 0;)
869 dma_fence_put(old_list->shared[i]);
870 }
871
872 /* Success! */
873 return 0;
874
875 restart:
876 rcu_read_unlock();
877 if (dst_list) {
878 for (i = dst_list->shared_count; i --> 0;) {
879 dma_fence_put(dst_list->shared[i]);
880 dst_list->shared[i] = NULL;
881 }
882 objlist_free(dst_list);
883 dst_list = NULL;
884 }
885 if (fence) {
886 dma_fence_put(fence);
887 fence = NULL;
888 }
889 goto top;
890 }
891
892 /*
893 * dma_resv_test_signaled_rcu(robj, shared)
894 *
895 * If shared is true, test whether all of the shared fences are
896 * signalled, or if there are none, test whether the exclusive
897 * fence is signalled. If shared is false, test only whether the
898 * exclusive fence is signalled.
899 *
900 * XXX Why does this _not_ test the exclusive fence if shared is
901 * true only if there are no shared fences? This makes no sense.
902 */
903 bool
904 dma_resv_test_signaled_rcu(const struct dma_resv *robj,
905 bool shared)
906 {
907 struct dma_resv_read_ticket ticket;
908 struct dma_resv_list *list;
909 struct dma_fence *fence;
910 uint32_t i, shared_count;
911 bool signaled = true;
912
913 top:
914 /* Enter an RCU read section and get a read ticket. */
915 rcu_read_lock();
916 dma_resv_read_begin(robj, &ticket);
917
918 /* If shared is requested and there is a shared list, test it. */
919 if (!shared)
920 goto excl;
921 list = robj->fence;
922 __insn_barrier();
923 if (list) {
924 /* Make sure the content of the list has been published. */
925 membar_datadep_consumer();
926
927 /* Find out how long it is. */
928 shared_count = list->shared_count;
929
930 /*
931 * Make sure we saw a consistent snapshot of the list
932 * pointer and length.
933 */
934 if (!dma_resv_read_valid(robj, &ticket))
935 goto restart;
936
937 /*
938 * For each fence, if it is going away, restart.
939 * Otherwise, acquire a reference to it to test whether
940 * it is signalled. Stop if we find any that is not
941 * signalled.
942 */
943 for (i = 0; i < shared_count; i++) {
944 fence = dma_fence_get_rcu(list->shared[i]);
945 if (fence == NULL)
946 goto restart;
947 signaled &= dma_fence_is_signaled(fence);
948 dma_fence_put(fence);
949 if (!signaled)
950 goto out;
951 }
952 }
953
954 excl:
955 /* If there is an exclusive fence, test it. */
956 fence = robj->fence_excl;
957 __insn_barrier();
958 if (fence) {
959 /* Make sure the content of the fence has been published. */
960 membar_datadep_consumer();
961
962 /*
963 * Make sure we saw a consistent snapshot of the fence.
964 *
965 * XXX I'm not actually sure this is necessary since
966 * pointer writes are supposed to be atomic.
967 */
968 if (!dma_resv_read_valid(robj, &ticket))
969 goto restart;
970
971 /*
972 * If it is going away, restart. Otherwise, acquire a
973 * reference to it to test whether it is signalled.
974 */
975 if ((fence = dma_fence_get_rcu(fence)) == NULL)
976 goto restart;
977 signaled &= dma_fence_is_signaled(fence);
978 dma_fence_put(fence);
979 if (!signaled)
980 goto out;
981 }
982
983 out: rcu_read_unlock();
984 return signaled;
985
986 restart:
987 rcu_read_unlock();
988 goto top;
989 }
990
991 /*
992 * dma_resv_wait_timeout_rcu(robj, shared, intr, timeout)
993 *
994 * If shared is true, wait for all of the shared fences to be
995 * signalled, or if there are none, wait for the exclusive fence
996 * to be signalled. If shared is false, wait only for the
997 * exclusive fence to be signalled. If timeout is zero, don't
998 * wait, only test.
999 *
1000 * XXX Why does this _not_ wait for the exclusive fence if shared
1001 * is true only if there are no shared fences? This makes no
1002 * sense.
1003 */
1004 long
1005 dma_resv_wait_timeout_rcu(const struct dma_resv *robj,
1006 bool shared, bool intr, unsigned long timeout)
1007 {
1008 struct dma_resv_read_ticket ticket;
1009 struct dma_resv_list *list;
1010 struct dma_fence *fence;
1011 uint32_t i, shared_count;
1012 long ret;
1013
1014 if (timeout == 0)
1015 return dma_resv_test_signaled_rcu(robj, shared);
1016
1017 top:
1018 /* Enter an RCU read section and get a read ticket. */
1019 rcu_read_lock();
1020 dma_resv_read_begin(robj, &ticket);
1021
1022 /* If shared is requested and there is a shared list, wait on it. */
1023 if (!shared)
1024 goto excl;
1025 list = robj->fence;
1026 __insn_barrier();
1027 if (list) {
1028 /* Make sure the content of the list has been published. */
1029 membar_datadep_consumer();
1030
1031 /* Find out how long it is. */
1032 shared_count = list->shared_count;
1033
1034 /*
1035 * Make sure we saw a consistent snapshot of the list
1036 * pointer and length.
1037 */
1038 if (!dma_resv_read_valid(robj, &ticket))
1039 goto restart;
1040
1041 /*
1042 * For each fence, if it is going away, restart.
1043 * Otherwise, acquire a reference to it to test whether
1044 * it is signalled. Stop and wait if we find any that
1045 * is not signalled.
1046 */
1047 for (i = 0; i < shared_count; i++) {
1048 fence = dma_fence_get_rcu(list->shared[i]);
1049 if (fence == NULL)
1050 goto restart;
1051 if (!dma_fence_is_signaled(fence))
1052 goto wait;
1053 dma_fence_put(fence);
1054 }
1055 }
1056
1057 excl:
1058 /* If there is an exclusive fence, test it. */
1059 fence = robj->fence_excl;
1060 __insn_barrier();
1061 if (fence) {
1062 /* Make sure the content of the fence has been published. */
1063 membar_datadep_consumer();
1064
1065 /*
1066 * Make sure we saw a consistent snapshot of the fence.
1067 *
1068 * XXX I'm not actually sure this is necessary since
1069 * pointer writes are supposed to be atomic.
1070 */
1071 if (!dma_resv_read_valid(robj, &ticket))
1072 goto restart;
1073
1074 /*
1075 * If it is going away, restart. Otherwise, acquire a
1076 * reference to it to test whether it is signalled. If
1077 * not, wait for it.
1078 */
1079 if ((fence = dma_fence_get_rcu(fence)) == NULL)
1080 goto restart;
1081 if (!dma_fence_is_signaled(fence))
1082 goto wait;
1083 dma_fence_put(fence);
1084 }
1085
1086 /* Success! Return the number of ticks left. */
1087 rcu_read_unlock();
1088 return timeout;
1089
1090 restart:
1091 rcu_read_unlock();
1092 goto top;
1093
1094 wait:
1095 /*
1096 * Exit the RCU read section, wait for it, and release the
1097 * fence when we're done. If we time out or fail, bail.
1098 * Otherwise, go back to the top.
1099 */
1100 KASSERT(fence != NULL);
1101 rcu_read_unlock();
1102 ret = dma_fence_wait_timeout(fence, intr, timeout);
1103 dma_fence_put(fence);
1104 if (ret <= 0)
1105 return ret;
1106 KASSERT(ret <= timeout);
1107 timeout = ret;
1108 goto top;
1109 }
1110
1111 /*
1112 * dma_resv_poll_init(rpoll, lock)
1113 *
1114 * Initialize reservation poll state.
1115 */
1116 void
1117 dma_resv_poll_init(struct dma_resv_poll *rpoll)
1118 {
1119
1120 mutex_init(&rpoll->rp_lock, MUTEX_DEFAULT, IPL_VM);
1121 selinit(&rpoll->rp_selq);
1122 rpoll->rp_claimed = 0;
1123 }
1124
1125 /*
1126 * dma_resv_poll_fini(rpoll)
1127 *
1128 * Release any resource associated with reservation poll state.
1129 */
1130 void
1131 dma_resv_poll_fini(struct dma_resv_poll *rpoll)
1132 {
1133
1134 KASSERT(rpoll->rp_claimed == 0);
1135 seldestroy(&rpoll->rp_selq);
1136 mutex_destroy(&rpoll->rp_lock);
1137 }
1138
1139 /*
1140 * dma_resv_poll_cb(fence, fcb)
1141 *
1142 * Callback to notify a reservation poll that a fence has
1143 * completed. Notify any waiters and allow the next poller to
1144 * claim the callback.
1145 *
1146 * If one thread is waiting for the exclusive fence only, and we
1147 * spuriously notify them about a shared fence, tough.
1148 */
1149 static void
1150 dma_resv_poll_cb(struct dma_fence *fence, struct dma_fence_cb *fcb)
1151 {
1152 struct dma_resv_poll *rpoll = container_of(fcb,
1153 struct dma_resv_poll, rp_fcb);
1154
1155 mutex_enter(&rpoll->rp_lock);
1156 selnotify(&rpoll->rp_selq, 0, NOTE_SUBMIT);
1157 rpoll->rp_claimed = 0;
1158 mutex_exit(&rpoll->rp_lock);
1159 }
1160
1161 /*
1162 * dma_resv_do_poll(robj, events, rpoll)
1163 *
1164 * Poll for reservation object events using the reservation poll
1165 * state in rpoll:
1166 *
1167 * - POLLOUT wait for all fences shared and exclusive
1168 * - POLLIN wait for the exclusive fence
1169 *
1170 * Return the subset of events in events that are ready. If any
1171 * are requested but not ready, arrange to be notified with
1172 * selnotify when they are.
1173 */
1174 int
1175 dma_resv_do_poll(const struct dma_resv *robj, int events,
1176 struct dma_resv_poll *rpoll)
1177 {
1178 struct dma_resv_read_ticket ticket;
1179 struct dma_resv_list *list;
1180 struct dma_fence *fence;
1181 uint32_t i, shared_count;
1182 int revents;
1183 bool recorded = false; /* curlwp is on the selq */
1184 bool claimed = false; /* we claimed the callback */
1185 bool callback = false; /* we requested a callback */
1186
1187 /*
1188 * Start with the maximal set of events that could be ready.
1189 * We will eliminate the events that are definitely not ready
1190 * as we go at the same time as we add callbacks to notify us
1191 * that they may be ready.
1192 */
1193 revents = events & (POLLIN|POLLOUT);
1194 if (revents == 0)
1195 return 0;
1196
1197 top:
1198 /* Enter an RCU read section and get a read ticket. */
1199 rcu_read_lock();
1200 dma_resv_read_begin(robj, &ticket);
1201
1202 /* If we want to wait for all fences, get the shared list. */
1203 if (!(events & POLLOUT))
1204 goto excl;
1205 list = robj->fence;
1206 __insn_barrier();
1207 if (list) do {
1208 /* Make sure the content of the list has been published. */
1209 membar_datadep_consumer();
1210
1211 /* Find out how long it is. */
1212 shared_count = list->shared_count;
1213
1214 /*
1215 * Make sure we saw a consistent snapshot of the list
1216 * pointer and length.
1217 */
1218 if (!dma_resv_read_valid(robj, &ticket))
1219 goto restart;
1220
1221 /*
1222 * For each fence, if it is going away, restart.
1223 * Otherwise, acquire a reference to it to test whether
1224 * it is signalled. Stop and request a callback if we
1225 * find any that is not signalled.
1226 */
1227 for (i = 0; i < shared_count; i++) {
1228 fence = dma_fence_get_rcu(list->shared[i]);
1229 if (fence == NULL)
1230 goto restart;
1231 if (!dma_fence_is_signaled(fence)) {
1232 dma_fence_put(fence);
1233 break;
1234 }
1235 dma_fence_put(fence);
1236 }
1237
1238 /* If all shared fences have been signalled, move on. */
1239 if (i == shared_count)
1240 break;
1241
1242 /* Put ourselves on the selq if we haven't already. */
1243 if (!recorded)
1244 goto record;
1245
1246 /*
1247 * If someone else claimed the callback, or we already
1248 * requested it, we're guaranteed to be notified, so
1249 * assume the event is not ready.
1250 */
1251 if (!claimed || callback) {
1252 revents &= ~POLLOUT;
1253 break;
1254 }
1255
1256 /*
1257 * Otherwise, find the first fence that is not
1258 * signalled, request the callback, and clear POLLOUT
1259 * from the possible ready events. If they are all
1260 * signalled, leave POLLOUT set; we will simulate the
1261 * callback later.
1262 */
1263 for (i = 0; i < shared_count; i++) {
1264 fence = dma_fence_get_rcu(list->shared[i]);
1265 if (fence == NULL)
1266 goto restart;
1267 if (!dma_fence_add_callback(fence, &rpoll->rp_fcb,
1268 dma_resv_poll_cb)) {
1269 dma_fence_put(fence);
1270 revents &= ~POLLOUT;
1271 callback = true;
1272 break;
1273 }
1274 dma_fence_put(fence);
1275 }
1276 } while (0);
1277
1278 excl:
1279 /* We always wait for at least the exclusive fence, so get it. */
1280 fence = robj->fence_excl;
1281 __insn_barrier();
1282 if (fence) do {
1283 /* Make sure the content of the fence has been published. */
1284 membar_datadep_consumer();
1285
1286 /*
1287 * Make sure we saw a consistent snapshot of the fence.
1288 *
1289 * XXX I'm not actually sure this is necessary since
1290 * pointer writes are supposed to be atomic.
1291 */
1292 if (!dma_resv_read_valid(robj, &ticket))
1293 goto restart;
1294
1295 /*
1296 * If it is going away, restart. Otherwise, acquire a
1297 * reference to it to test whether it is signalled. If
1298 * not, stop and request a callback.
1299 */
1300 if ((fence = dma_fence_get_rcu(fence)) == NULL)
1301 goto restart;
1302 if (dma_fence_is_signaled(fence)) {
1303 dma_fence_put(fence);
1304 break;
1305 }
1306
1307 /* Put ourselves on the selq if we haven't already. */
1308 if (!recorded) {
1309 dma_fence_put(fence);
1310 goto record;
1311 }
1312
1313 /*
1314 * If someone else claimed the callback, or we already
1315 * requested it, we're guaranteed to be notified, so
1316 * assume the event is not ready.
1317 */
1318 if (!claimed || callback) {
1319 dma_fence_put(fence);
1320 revents = 0;
1321 break;
1322 }
1323
1324 /*
1325 * Otherwise, try to request the callback, and clear
1326 * all possible ready events. If the fence has been
1327 * signalled in the interim, leave the events set; we
1328 * will simulate the callback later.
1329 */
1330 if (!dma_fence_add_callback(fence, &rpoll->rp_fcb,
1331 dma_resv_poll_cb)) {
1332 dma_fence_put(fence);
1333 revents = 0;
1334 callback = true;
1335 break;
1336 }
1337 dma_fence_put(fence);
1338 } while (0);
1339
1340 /* All done reading the fences. */
1341 rcu_read_unlock();
1342
1343 if (claimed && !callback) {
1344 /*
1345 * We claimed the callback but we didn't actually
1346 * request it because a fence was signalled while we
1347 * were claiming it. Call it ourselves now. The
1348 * callback doesn't use the fence nor rely on holding
1349 * any of the fence locks, so this is safe.
1350 */
1351 dma_resv_poll_cb(NULL, &rpoll->rp_fcb);
1352 }
1353 return revents;
1354
1355 restart:
1356 rcu_read_unlock();
1357 goto top;
1358
1359 record:
1360 rcu_read_unlock();
1361 mutex_enter(&rpoll->rp_lock);
1362 selrecord(curlwp, &rpoll->rp_selq);
1363 if (!rpoll->rp_claimed)
1364 claimed = rpoll->rp_claimed = true;
1365 mutex_exit(&rpoll->rp_lock);
1366 recorded = true;
1367 goto top;
1368 }
1369
1370 /*
1371 * dma_resv_kqfilter(robj, kn, rpoll)
1372 *
1373 * Kqueue filter for reservation objects. Currently not
1374 * implemented because the logic to implement it is nontrivial,
1375 * and userland will presumably never use it, so it would be
1376 * dangerous to add never-tested complex code paths to the kernel.
1377 */
1378 int
1379 dma_resv_kqfilter(const struct dma_resv *robj,
1380 struct knote *kn, struct dma_resv_poll *rpoll)
1381 {
1382
1383 return EINVAL;
1384 }
1385