amdgpu_fence.c revision 1.5 1 /* $NetBSD: amdgpu_fence.c,v 1.5 2020/02/14 04:35:19 riastradh Exp $ */
2
3 /*
4 * Copyright 2009 Jerome Glisse.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 *
27 */
28 /*
29 * Authors:
30 * Jerome Glisse <glisse (at) freedesktop.org>
31 * Dave Airlie
32 */
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: amdgpu_fence.c,v 1.5 2020/02/14 04:35:19 riastradh Exp $");
35
36 #include <asm/byteorder.h>
37 #include <linux/seq_file.h>
38 #include <linux/atomic.h>
39 #include <linux/wait.h>
40 #include <linux/kref.h>
41 #include <linux/slab.h>
42 #include <linux/firmware.h>
43 #include <drm/drmP.h>
44 #include "amdgpu.h"
45 #include "amdgpu_trace.h"
46
47 #include <linux/nbsd-namespace.h>
48
49 /*
50 * Fences
51 * Fences mark an event in the GPUs pipeline and are used
52 * for GPU/CPU synchronization. When the fence is written,
53 * it is expected that all buffers associated with that fence
54 * are no longer in use by the associated ring on the GPU and
55 * that the the relevant GPU caches have been flushed.
56 */
57
58 static struct kmem_cache *amdgpu_fence_slab;
59 static atomic_t amdgpu_fence_slab_ref = ATOMIC_INIT(0);
60
61 /**
62 * amdgpu_fence_write - write a fence value
63 *
64 * @ring: ring the fence is associated with
65 * @seq: sequence number to write
66 *
67 * Writes a fence value to memory (all asics).
68 */
69 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
70 {
71 struct amdgpu_fence_driver *drv = &ring->fence_drv;
72
73 if (drv->cpu_addr)
74 *drv->cpu_addr = cpu_to_le32(seq);
75 }
76
77 /**
78 * amdgpu_fence_read - read a fence value
79 *
80 * @ring: ring the fence is associated with
81 *
82 * Reads a fence value from memory (all asics).
83 * Returns the value of the fence read from memory.
84 */
85 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
86 {
87 struct amdgpu_fence_driver *drv = &ring->fence_drv;
88 u32 seq = 0;
89
90 if (drv->cpu_addr)
91 seq = le32_to_cpu(*drv->cpu_addr);
92 else
93 seq = lower_32_bits(atomic64_read(&drv->last_seq));
94
95 return seq;
96 }
97
98 /**
99 * amdgpu_fence_emit - emit a fence on the requested ring
100 *
101 * @ring: ring the fence is associated with
102 * @owner: creator of the fence
103 * @fence: amdgpu fence object
104 *
105 * Emits a fence command on the requested ring (all asics).
106 * Returns 0 on success, -ENOMEM on failure.
107 */
108 int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
109 struct amdgpu_fence **fence)
110 {
111 struct amdgpu_device *adev = ring->adev;
112
113 /* we are protected by the ring emission mutex */
114 *fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
115 if ((*fence) == NULL) {
116 return -ENOMEM;
117 }
118 (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
119 (*fence)->ring = ring;
120 (*fence)->owner = owner;
121 fence_init(&(*fence)->base, &amdgpu_fence_ops,
122 #ifdef __NetBSD__
123 &ring->fence_drv.fence_lock,
124 #else
125 &ring->fence_drv.fence_queue.lock,
126 #endif
127 adev->fence_context + ring->idx,
128 (*fence)->seq);
129 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
130 (*fence)->seq,
131 AMDGPU_FENCE_FLAG_INT);
132 return 0;
133 }
134
135 /**
136 * amdgpu_fence_schedule_fallback - schedule fallback check
137 *
138 * @ring: pointer to struct amdgpu_ring
139 *
140 * Start a timer as fallback to our interrupts.
141 */
142 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
143 {
144 mod_timer(&ring->fence_drv.fallback_timer,
145 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
146 }
147
148 /**
149 * amdgpu_fence_activity - check for fence activity
150 *
151 * @ring: pointer to struct amdgpu_ring
152 *
153 * Checks the current fence value and calculates the last
154 * signalled fence value. Returns true if activity occured
155 * on the ring, and the fence_queue should be waken up.
156 */
157 static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
158 {
159 uint64_t seq, last_seq, last_emitted;
160 unsigned count_loop = 0;
161 bool wake = false;
162
163 BUG_ON(!spin_is_locked(&ring->fence_drv.fence_lock));
164
165 /* Note there is a scenario here for an infinite loop but it's
166 * very unlikely to happen. For it to happen, the current polling
167 * process need to be interrupted by another process and another
168 * process needs to update the last_seq btw the atomic read and
169 * xchg of the current process.
170 *
171 * More over for this to go in infinite loop there need to be
172 * continuously new fence signaled ie amdgpu_fence_read needs
173 * to return a different value each time for both the currently
174 * polling process and the other process that xchg the last_seq
175 * btw atomic read and xchg of the current process. And the
176 * value the other process set as last seq must be higher than
177 * the seq value we just read. Which means that current process
178 * need to be interrupted after amdgpu_fence_read and before
179 * atomic xchg.
180 *
181 * To be even more safe we count the number of time we loop and
182 * we bail after 10 loop just accepting the fact that we might
183 * have temporarly set the last_seq not to the true real last
184 * seq but to an older one.
185 */
186 last_seq = atomic64_read(&ring->fence_drv.last_seq);
187 do {
188 last_emitted = ring->fence_drv.sync_seq[ring->idx];
189 seq = amdgpu_fence_read(ring);
190 seq |= last_seq & 0xffffffff00000000LL;
191 if (seq < last_seq) {
192 seq &= 0xffffffff;
193 seq |= last_emitted & 0xffffffff00000000LL;
194 }
195
196 if (seq <= last_seq || seq > last_emitted) {
197 break;
198 }
199 /* If we loop over we don't want to return without
200 * checking if a fence is signaled as it means that the
201 * seq we just read is different from the previous on.
202 */
203 wake = true;
204 last_seq = seq;
205 if ((count_loop++) > 10) {
206 /* We looped over too many time leave with the
207 * fact that we might have set an older fence
208 * seq then the current real last seq as signaled
209 * by the hw.
210 */
211 break;
212 }
213 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
214
215 if (seq < last_emitted)
216 amdgpu_fence_schedule_fallback(ring);
217
218 return wake;
219 }
220
221 #ifdef __NetBSD__
222 static int amdgpu_fence_check_signaled(struct amdgpu_fence *);
223
224 static void
225 amdgpu_fence_wakeup_locked(struct amdgpu_ring *ring)
226 {
227 struct amdgpu_fence *fence, *next;
228
229 BUG_ON(!spin_is_locked(&ring->fence_drv.fence_lock));
230 DRM_SPIN_WAKEUP_ALL(&ring->fence_drv.fence_queue,
231 &ring->fence_drv.fence_lock);
232 TAILQ_FOREACH_SAFE(fence, &ring->fence_drv.fence_check, fence_check,
233 next) {
234 amdgpu_fence_check_signaled(fence);
235 }
236 }
237 #endif
238
239 /**
240 * amdgpu_fence_process - process a fence
241 *
242 * @adev: amdgpu_device pointer
243 * @ring: ring index the fence is associated with
244 *
245 * Checks the current fence value and wakes the fence queue
246 * if the sequence number has increased (all asics).
247 */
248 static void amdgpu_fence_process_locked(struct amdgpu_ring *ring)
249 {
250 if (amdgpu_fence_activity(ring))
251 #ifdef __NetBSD__
252 amdgpu_fence_wakeup_locked(ring);
253 #else
254 wake_up_all(&ring->fence_drv.fence_queue);
255 #endif
256 }
257
258 void amdgpu_fence_process(struct amdgpu_ring *ring)
259 {
260
261 spin_lock(&ring->fence_drv.fence_lock);
262 amdgpu_fence_process_locked(ring);
263 spin_unlock(&ring->fence_drv.fence_lock);
264 }
265
266 /**
267 * amdgpu_fence_fallback - fallback for hardware interrupts
268 *
269 * @work: delayed work item
270 *
271 * Checks for fence activity.
272 */
273 static void amdgpu_fence_fallback(unsigned long arg)
274 {
275 struct amdgpu_ring *ring = (void *)arg;
276
277 amdgpu_fence_process(ring);
278 }
279
280 /**
281 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
282 *
283 * @ring: ring the fence is associated with
284 * @seq: sequence number
285 *
286 * Check if the last signaled fence sequnce number is >= the requested
287 * sequence number (all asics).
288 * Returns true if the fence has signaled (current fence value
289 * is >= requested value) or false if it has not (current fence
290 * value is < the requested value. Helper function for
291 * amdgpu_fence_signaled().
292 */
293 static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
294 {
295 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
296 return true;
297
298 /* poll new last sequence at least once */
299 amdgpu_fence_process(ring);
300 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
301 return true;
302
303 return false;
304 }
305
306 /*
307 * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
308 * @ring: ring to wait on for the seq number
309 * @seq: seq number wait for
310 *
311 * return value:
312 * 0: seq signaled, and gpu not hang
313 * -EDEADL: GPU hang detected
314 * -EINVAL: some paramter is not valid
315 */
316 static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
317 {
318 bool signaled = false;
319
320 BUG_ON(!ring);
321 BUG_ON(!spin_is_locked(&ring->fence_drv.fence_lock));
322 if (seq > ring->fence_drv.sync_seq[ring->idx])
323 return -EINVAL;
324
325 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
326 return 0;
327
328 amdgpu_fence_schedule_fallback(ring);
329 #ifdef __NetBSD__
330 /* XXX How is this ever supposed to wake up in the EDEADLK case? */
331 int r __unused;
332 DRM_SPIN_WAIT_NOINTR_UNTIL(r, &ring->fence_drv.fence_queue,
333 &ring->fence_drv.fence_lock,
334 (signaled = amdgpu_fence_seq_signaled(ring, seq)));
335 #else
336 wait_event(ring->fence_drv.fence_queue, (
337 (signaled = amdgpu_fence_seq_signaled(ring, seq))));
338 #endif
339
340 if (signaled)
341 return 0;
342 else
343 return -EDEADLK;
344 }
345
346 /**
347 * amdgpu_fence_wait_next - wait for the next fence to signal
348 *
349 * @adev: amdgpu device pointer
350 * @ring: ring index the fence is associated with
351 *
352 * Wait for the next fence on the requested ring to signal (all asics).
353 * Returns 0 if the next fence has passed, error for all other cases.
354 * Caller must hold ring lock.
355 */
356 int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
357 {
358 uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
359
360 if (seq >= ring->fence_drv.sync_seq[ring->idx])
361 return -ENOENT;
362
363 return amdgpu_fence_ring_wait_seq(ring, seq);
364 }
365
366 /**
367 * amdgpu_fence_wait_empty - wait for all fences to signal
368 *
369 * @adev: amdgpu device pointer
370 * @ring: ring index the fence is associated with
371 *
372 * Wait for all fences on the requested ring to signal (all asics).
373 * Returns 0 if the fences have passed, error for all other cases.
374 * Caller must hold ring lock.
375 */
376 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
377 {
378 uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
379
380 if (!seq)
381 return 0;
382
383 return amdgpu_fence_ring_wait_seq(ring, seq);
384 }
385
386 /**
387 * amdgpu_fence_count_emitted - get the count of emitted fences
388 *
389 * @ring: ring the fence is associated with
390 *
391 * Get the number of fences emitted on the requested ring (all asics).
392 * Returns the number of emitted fences on the ring. Used by the
393 * dynpm code to ring track activity.
394 */
395 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
396 {
397 uint64_t emitted;
398
399 /* We are not protected by ring lock when reading the last sequence
400 * but it's ok to report slightly wrong fence count here.
401 */
402 amdgpu_fence_process(ring);
403 emitted = ring->fence_drv.sync_seq[ring->idx]
404 - atomic64_read(&ring->fence_drv.last_seq);
405 /* to avoid 32bits warp around */
406 if (emitted > 0x10000000)
407 emitted = 0x10000000;
408
409 return (unsigned)emitted;
410 }
411
412 /**
413 * amdgpu_fence_need_sync - do we need a semaphore
414 *
415 * @fence: amdgpu fence object
416 * @dst_ring: which ring to check against
417 *
418 * Check if the fence needs to be synced against another ring
419 * (all asics). If so, we need to emit a semaphore.
420 * Returns true if we need to sync with another ring, false if
421 * not.
422 */
423 bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
424 struct amdgpu_ring *dst_ring)
425 {
426 struct amdgpu_fence_driver *fdrv;
427
428 if (!fence)
429 return false;
430
431 if (fence->ring == dst_ring)
432 return false;
433
434 /* we are protected by the ring mutex */
435 fdrv = &dst_ring->fence_drv;
436 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
437 return false;
438
439 return true;
440 }
441
442 /**
443 * amdgpu_fence_note_sync - record the sync point
444 *
445 * @fence: amdgpu fence object
446 * @dst_ring: which ring to check against
447 *
448 * Note the sequence number at which point the fence will
449 * be synced with the requested ring (all asics).
450 */
451 void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
452 struct amdgpu_ring *dst_ring)
453 {
454 struct amdgpu_fence_driver *dst, *src;
455 unsigned i;
456
457 if (!fence)
458 return;
459
460 if (fence->ring == dst_ring)
461 return;
462
463 /* we are protected by the ring mutex */
464 src = &fence->ring->fence_drv;
465 dst = &dst_ring->fence_drv;
466 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
467 if (i == dst_ring->idx)
468 continue;
469
470 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
471 }
472 }
473
474 /**
475 * amdgpu_fence_driver_start_ring - make the fence driver
476 * ready for use on the requested ring.
477 *
478 * @ring: ring to start the fence driver on
479 * @irq_src: interrupt source to use for this ring
480 * @irq_type: interrupt type to use for this ring
481 *
482 * Make the fence driver ready for processing (all asics).
483 * Not all asics have all rings, so each asic will only
484 * start the fence driver on the rings it has.
485 * Returns 0 for success, errors for failure.
486 */
487 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
488 struct amdgpu_irq_src *irq_src,
489 unsigned irq_type)
490 {
491 struct amdgpu_device *adev = ring->adev;
492 uint64_t index;
493
494 if (ring != &adev->uvd.ring) {
495 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
496 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
497 } else {
498 /* put fence directly behind firmware */
499 index = ALIGN(adev->uvd.fw->size, 8);
500 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
501 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
502 }
503 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
504 amdgpu_irq_get(adev, irq_src, irq_type);
505
506 ring->fence_drv.irq_src = irq_src;
507 ring->fence_drv.irq_type = irq_type;
508 ring->fence_drv.initialized = true;
509
510 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016"PRIx64", "
511 "cpu addr 0x%p\n", ring->idx,
512 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
513 return 0;
514 }
515
516 /**
517 * amdgpu_fence_driver_init_ring - init the fence driver
518 * for the requested ring.
519 *
520 * @ring: ring to init the fence driver on
521 *
522 * Init the fence driver for the requested ring (all asics).
523 * Helper function for amdgpu_fence_driver_init().
524 */
525 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
526 {
527 int i, r;
528
529 ring->fence_drv.cpu_addr = NULL;
530 ring->fence_drv.gpu_addr = 0;
531 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
532 ring->fence_drv.sync_seq[i] = 0;
533
534 atomic64_set(&ring->fence_drv.last_seq, 0);
535 ring->fence_drv.initialized = false;
536
537 setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
538 (unsigned long)ring);
539
540 #ifdef __NetBSD__
541 spin_lock_init(&ring->fence_drv.fence_lock);
542 DRM_INIT_WAITQUEUE(&ring->fence_drv.fence_queue, "amdfence");
543 TAILQ_INIT(&ring->fence_drv.fence_check);
544 #else
545 init_waitqueue_head(&ring->fence_drv.fence_queue);
546 #endif
547
548 if (amdgpu_enable_scheduler) {
549 long timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
550 if (timeout == 0) {
551 /*
552 * FIXME:
553 * Delayed workqueue cannot use it directly,
554 * so the scheduler will not use delayed workqueue if
555 * MAX_SCHEDULE_TIMEOUT is set.
556 * Currently keep it simple and silly.
557 */
558 timeout = MAX_SCHEDULE_TIMEOUT;
559 }
560 r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
561 amdgpu_sched_hw_submission,
562 timeout, ring->name);
563 if (r) {
564 DRM_ERROR("Failed to create scheduler on ring %s.\n",
565 ring->name);
566 return r;
567 }
568 }
569
570 return 0;
571 }
572
573 /**
574 * amdgpu_fence_driver_init - init the fence driver
575 * for all possible rings.
576 *
577 * @adev: amdgpu device pointer
578 *
579 * Init the fence driver for all possible rings (all asics).
580 * Not all asics have all rings, so each asic will only
581 * start the fence driver on the rings it has using
582 * amdgpu_fence_driver_start_ring().
583 * Returns 0 for success.
584 */
585 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
586 {
587 if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
588 amdgpu_fence_slab = kmem_cache_create(
589 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
590 SLAB_HWCACHE_ALIGN, NULL);
591 if (!amdgpu_fence_slab)
592 return -ENOMEM;
593 }
594 if (amdgpu_debugfs_fence_init(adev))
595 dev_err(adev->dev, "fence debugfs file creation failed\n");
596
597 return 0;
598 }
599
600 /**
601 * amdgpu_fence_driver_fini - tear down the fence driver
602 * for all possible rings.
603 *
604 * @adev: amdgpu device pointer
605 *
606 * Tear down the fence driver for all possible rings (all asics).
607 */
608 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
609 {
610 int i, r;
611
612 if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
613 kmem_cache_destroy(amdgpu_fence_slab);
614 mutex_lock(&adev->ring_lock);
615 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
616 struct amdgpu_ring *ring = adev->rings[i];
617
618 if (!ring || !ring->fence_drv.initialized)
619 continue;
620 r = amdgpu_fence_wait_empty(ring);
621 if (r) {
622 /* no need to trigger GPU reset as we are unloading */
623 amdgpu_fence_driver_force_completion(adev);
624 }
625 #ifdef __NetBSD__
626 spin_lock(&ring->fence_drv.fence_lock);
627 amdgpu_fence_wakeup_locked(ring);
628 spin_unlock(&ring->fence_drv.fence_lock);
629 #else
630 wake_up_all(&ring->fence_drv.fence_queue);
631 #endif
632 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
633 ring->fence_drv.irq_type);
634 amd_sched_fini(&ring->sched);
635 del_timer_sync(&ring->fence_drv.fallback_timer);
636 ring->fence_drv.initialized = false;
637 #ifdef __NetBSD__
638 BUG_ON(!TAILQ_EMPTY(&ring->fence_drv.fence_check));
639 DRM_DESTROY_WAITQUEUE(&ring->fence_drv.fence_queue);
640 spin_lock_destroy(&ring->fence_drv.fence_lock);
641 #endif
642 }
643 mutex_unlock(&adev->ring_lock);
644 }
645
646 /**
647 * amdgpu_fence_driver_suspend - suspend the fence driver
648 * for all possible rings.
649 *
650 * @adev: amdgpu device pointer
651 *
652 * Suspend the fence driver for all possible rings (all asics).
653 */
654 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
655 {
656 int i, r;
657
658 mutex_lock(&adev->ring_lock);
659 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
660 struct amdgpu_ring *ring = adev->rings[i];
661 if (!ring || !ring->fence_drv.initialized)
662 continue;
663
664 /* wait for gpu to finish processing current batch */
665 r = amdgpu_fence_wait_empty(ring);
666 if (r) {
667 /* delay GPU reset to resume */
668 amdgpu_fence_driver_force_completion(adev);
669 }
670
671 /* disable the interrupt */
672 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
673 ring->fence_drv.irq_type);
674 }
675 mutex_unlock(&adev->ring_lock);
676 }
677
678 /**
679 * amdgpu_fence_driver_resume - resume the fence driver
680 * for all possible rings.
681 *
682 * @adev: amdgpu device pointer
683 *
684 * Resume the fence driver for all possible rings (all asics).
685 * Not all asics have all rings, so each asic will only
686 * start the fence driver on the rings it has using
687 * amdgpu_fence_driver_start_ring().
688 * Returns 0 for success.
689 */
690 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
691 {
692 int i;
693
694 mutex_lock(&adev->ring_lock);
695 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
696 struct amdgpu_ring *ring = adev->rings[i];
697 if (!ring || !ring->fence_drv.initialized)
698 continue;
699
700 /* enable the interrupt */
701 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
702 ring->fence_drv.irq_type);
703 }
704 mutex_unlock(&adev->ring_lock);
705 }
706
707 /**
708 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
709 *
710 * @adev: amdgpu device pointer
711 *
712 * In case of GPU reset failure make sure no process keep waiting on fence
713 * that will never complete.
714 */
715 void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
716 {
717 int i;
718
719 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
720 struct amdgpu_ring *ring = adev->rings[i];
721 if (!ring || !ring->fence_drv.initialized)
722 continue;
723
724 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
725 }
726 }
727
728 /*
729 * Common fence implementation
730 */
731
732 static const char *amdgpu_fence_get_driver_name(struct fence *fence)
733 {
734 return "amdgpu";
735 }
736
737 static const char *amdgpu_fence_get_timeline_name(struct fence *f)
738 {
739 struct amdgpu_fence *fence = to_amdgpu_fence(f);
740 return (const char *)fence->ring->name;
741 }
742
743 /**
744 * amdgpu_fence_is_signaled - test if fence is signaled
745 *
746 * @f: fence to test
747 *
748 * Test the fence sequence number if it is already signaled. If it isn't
749 * signaled start fence processing. Returns True if the fence is signaled.
750 */
751 static bool amdgpu_fence_is_signaled(struct fence *f)
752 {
753 struct amdgpu_fence *fence = to_amdgpu_fence(f);
754 struct amdgpu_ring *ring = fence->ring;
755
756 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
757 return true;
758
759 amdgpu_fence_process(ring);
760
761 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
762 return true;
763
764 return false;
765 }
766
767 /**
768 * amdgpu_fence_check_signaled - callback from fence_queue
769 *
770 * this function is called with fence_queue lock held, which is also used
771 * for the fence locking itself, so unlocked variants are used for
772 * fence_signal, and remove_wait_queue.
773 */
774 #ifdef __NetBSD__
775 static int amdgpu_fence_check_signaled(struct amdgpu_fence *fence)
776 #else
777 static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
778 #endif
779 {
780 #ifndef __NetBSD__
781 struct amdgpu_fence *fence;
782 #endif
783 u64 seq;
784 int ret;
785
786 #ifndef __NetBSD__
787 fence = container_of(wait, struct amdgpu_fence, fence_wake);
788 #endif
789 BUG_ON(!spin_is_locked(&fence->ring->fence_drv.fence_lock));
790
791 /*
792 * We cannot use amdgpu_fence_process here because we're already
793 * in the waitqueue, in a call from wake_up_all.
794 */
795 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
796 if (seq >= fence->seq) {
797 ret = fence_signal_locked(&fence->base);
798 if (!ret)
799 FENCE_TRACE(&fence->base, "signaled from irq context\n");
800 else
801 FENCE_TRACE(&fence->base, "was already signaled\n");
802
803 #ifdef __NetBSD__
804 TAILQ_REMOVE(&fence->ring->fence_drv.fence_check, fence,
805 fence_check);
806 #else
807 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
808 #endif
809 fence_put(&fence->base);
810 } else
811 FENCE_TRACE(&fence->base, "pending\n");
812 return 0;
813 }
814
815 /**
816 * amdgpu_fence_enable_signaling - enable signalling on fence
817 * @fence: fence
818 *
819 * This function is called with fence_queue lock held, and adds a callback
820 * to fence_queue that checks if this fence is signaled, and if so it
821 * signals the fence and removes itself.
822 */
823 static bool amdgpu_fence_enable_signaling(struct fence *f)
824 {
825 struct amdgpu_fence *fence = to_amdgpu_fence(f);
826 struct amdgpu_ring *ring = fence->ring;
827
828 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
829 return false;
830
831 #ifdef __NetBSD__
832 TAILQ_INSERT_TAIL(&ring->fence_drv.fence_check, fence, fence_check);
833 #else
834 fence->fence_wake.flags = 0;
835 fence->fence_wake.private = NULL;
836 fence->fence_wake.func = amdgpu_fence_check_signaled;
837 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
838 #endif
839 fence_get(f);
840 if (!timer_pending(&ring->fence_drv.fallback_timer))
841 amdgpu_fence_schedule_fallback(ring);
842 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
843 return true;
844 }
845
846 static void amdgpu_fence_release(struct fence *f)
847 {
848 struct amdgpu_fence *fence = to_amdgpu_fence(f);
849 kmem_cache_free(amdgpu_fence_slab, fence);
850 }
851
852 const struct fence_ops amdgpu_fence_ops = {
853 .get_driver_name = amdgpu_fence_get_driver_name,
854 .get_timeline_name = amdgpu_fence_get_timeline_name,
855 .enable_signaling = amdgpu_fence_enable_signaling,
856 .signaled = amdgpu_fence_is_signaled,
857 .wait = fence_default_wait,
858 .release = amdgpu_fence_release,
859 };
860
861 /*
862 * Fence debugfs
863 */
864 #if defined(CONFIG_DEBUG_FS)
865 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
866 {
867 struct drm_info_node *node = (struct drm_info_node *)m->private;
868 struct drm_device *dev = node->minor->dev;
869 struct amdgpu_device *adev = dev->dev_private;
870 int i, j;
871
872 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
873 struct amdgpu_ring *ring = adev->rings[i];
874 if (!ring || !ring->fence_drv.initialized)
875 continue;
876
877 amdgpu_fence_process(ring);
878
879 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
880 seq_printf(m, "Last signaled fence 0x%016llx\n",
881 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
882 seq_printf(m, "Last emitted 0x%016"PRIx64"\n",
883 ring->fence_drv.sync_seq[i]);
884
885 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
886 struct amdgpu_ring *other = adev->rings[j];
887 if (i != j && other && other->fence_drv.initialized &&
888 ring->fence_drv.sync_seq[j])
889 seq_printf(m, "Last sync to ring %d 0x%016"PRIx64"\n",
890 j, ring->fence_drv.sync_seq[j]);
891 }
892 }
893 return 0;
894 }
895
896 static struct drm_info_list amdgpu_debugfs_fence_list[] = {
897 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
898 };
899 #endif
900
901 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
902 {
903 #if defined(CONFIG_DEBUG_FS)
904 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
905 #else
906 return 0;
907 #endif
908 }
909
910