amdgpu_fence.c revision 1.8 1 /* $NetBSD: amdgpu_fence.c,v 1.8 2021/12/19 09:59:30 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.8 2021/12/19 09:59:30 riastradh Exp $");
35
36 #include <linux/seq_file.h>
37 #include <linux/atomic.h>
38 #include <linux/wait.h>
39 #include <linux/kref.h>
40 #include <linux/slab.h>
41 #include <linux/firmware.h>
42 #include <linux/pm_runtime.h>
43
44 #include <drm/drm_debugfs.h>
45
46 #include "amdgpu.h"
47 #include "amdgpu_trace.h"
48
49 #include <linux/nbsd-namespace.h>
50
51 /*
52 * Fences
53 * Fences mark an event in the GPUs pipeline and are used
54 * for GPU/CPU synchronization. When the fence is written,
55 * it is expected that all buffers associated with that fence
56 * are no longer in use by the associated ring on the GPU and
57 * that the the relevant GPU caches have been flushed.
58 */
59
60 struct amdgpu_fence {
61 struct dma_fence base;
62
63 /* RB, DMA, etc. */
64 struct amdgpu_ring *ring;
65 };
66
67 static struct kmem_cache *amdgpu_fence_slab;
68
69 int amdgpu_fence_slab_init(void)
70 {
71 amdgpu_fence_slab = kmem_cache_create(
72 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
73 SLAB_HWCACHE_ALIGN, NULL);
74 if (!amdgpu_fence_slab)
75 return -ENOMEM;
76 return 0;
77 }
78
79 void amdgpu_fence_slab_fini(void)
80 {
81 rcu_barrier();
82 kmem_cache_destroy(amdgpu_fence_slab);
83 }
84 /*
85 * Cast helper
86 */
87 static const struct dma_fence_ops amdgpu_fence_ops;
88 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
89 {
90 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
91
92 if (__f->base.ops == &amdgpu_fence_ops)
93 return __f;
94
95 return NULL;
96 }
97
98 /**
99 * amdgpu_fence_write - write a fence value
100 *
101 * @ring: ring the fence is associated with
102 * @seq: sequence number to write
103 *
104 * Writes a fence value to memory (all asics).
105 */
106 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
107 {
108 struct amdgpu_fence_driver *drv = &ring->fence_drv;
109
110 if (drv->cpu_addr)
111 *drv->cpu_addr = cpu_to_le32(seq);
112 }
113
114 /**
115 * amdgpu_fence_read - read a fence value
116 *
117 * @ring: ring the fence is associated with
118 *
119 * Reads a fence value from memory (all asics).
120 * Returns the value of the fence read from memory.
121 */
122 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
123 {
124 struct amdgpu_fence_driver *drv = &ring->fence_drv;
125 u32 seq = 0;
126
127 if (drv->cpu_addr)
128 seq = le32_to_cpu(*drv->cpu_addr);
129 else
130 seq = atomic_read(&drv->last_seq);
131
132 return seq;
133 }
134
135 /**
136 * amdgpu_fence_emit - emit a fence on the requested ring
137 *
138 * @ring: ring the fence is associated with
139 * @f: resulting fence object
140 *
141 * Emits a fence command on the requested ring (all asics).
142 * Returns 0 on success, -ENOMEM on failure.
143 */
144 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
145 unsigned flags)
146 {
147 struct amdgpu_device *adev = ring->adev;
148 struct amdgpu_fence *fence;
149 struct dma_fence __rcu **ptr;
150 uint32_t seq;
151 int r;
152
153 fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
154 if (fence == NULL)
155 return -ENOMEM;
156
157 seq = ++ring->fence_drv.sync_seq;
158 fence->ring = ring;
159 dma_fence_init(&fence->base, &amdgpu_fence_ops,
160 &ring->fence_drv.lock,
161 adev->fence_context + ring->idx,
162 seq);
163 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
164 seq, flags | AMDGPU_FENCE_FLAG_INT);
165 pm_runtime_get_noresume(adev->ddev->dev);
166 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
167 if (unlikely(rcu_dereference_protected(*ptr, 1))) {
168 struct dma_fence *old;
169
170 rcu_read_lock();
171 old = dma_fence_get_rcu_safe(ptr);
172 rcu_read_unlock();
173
174 if (old) {
175 r = dma_fence_wait(old, false);
176 dma_fence_put(old);
177 if (r)
178 return r;
179 }
180 }
181
182 /* This function can't be called concurrently anyway, otherwise
183 * emitting the fence would mess up the hardware ring buffer.
184 */
185 rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
186
187 *f = &fence->base;
188
189 return 0;
190 }
191
192 /**
193 * amdgpu_fence_emit_polling - emit a fence on the requeste ring
194 *
195 * @ring: ring the fence is associated with
196 * @s: resulting sequence number
197 *
198 * Emits a fence command on the requested ring (all asics).
199 * Used For polling fence.
200 * Returns 0 on success, -ENOMEM on failure.
201 */
202 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s)
203 {
204 uint32_t seq;
205
206 if (!s)
207 return -EINVAL;
208
209 seq = ++ring->fence_drv.sync_seq;
210 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
211 seq, 0);
212
213 *s = seq;
214
215 return 0;
216 }
217
218 /**
219 * amdgpu_fence_schedule_fallback - schedule fallback check
220 *
221 * @ring: pointer to struct amdgpu_ring
222 *
223 * Start a timer as fallback to our interrupts.
224 */
225 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
226 {
227 mod_timer(&ring->fence_drv.fallback_timer,
228 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
229 }
230
231 /**
232 * amdgpu_fence_process - check for fence activity
233 *
234 * @ring: pointer to struct amdgpu_ring
235 *
236 * Checks the current fence value and calculates the last
237 * signalled fence value. Wakes the fence queue if the
238 * sequence number has increased.
239 *
240 * Returns true if fence was processed
241 */
242 bool amdgpu_fence_process(struct amdgpu_ring *ring)
243 {
244 struct amdgpu_fence_driver *drv = &ring->fence_drv;
245 struct amdgpu_device *adev = ring->adev;
246 uint32_t seq, last_seq;
247 int r;
248
249 do {
250 last_seq = atomic_read(&ring->fence_drv.last_seq);
251 seq = amdgpu_fence_read(ring);
252
253 } while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
254
255 if (del_timer(&ring->fence_drv.fallback_timer) &&
256 seq != ring->fence_drv.sync_seq)
257 amdgpu_fence_schedule_fallback(ring);
258
259 if (unlikely(seq == last_seq))
260 return false;
261
262 last_seq &= drv->num_fences_mask;
263 seq &= drv->num_fences_mask;
264
265 do {
266 struct dma_fence *fence, **ptr;
267
268 ++last_seq;
269 last_seq &= drv->num_fences_mask;
270 ptr = &drv->fences[last_seq];
271
272 /* There is always exactly one thread signaling this fence slot */
273 fence = rcu_dereference_protected(*ptr, 1);
274 RCU_INIT_POINTER(*ptr, NULL);
275
276 if (!fence)
277 continue;
278
279 r = dma_fence_signal(fence);
280 if (!r)
281 DMA_FENCE_TRACE(fence, "signaled from irq context\n");
282 else
283 BUG();
284
285 dma_fence_put(fence);
286 pm_runtime_mark_last_busy(adev->ddev->dev);
287 pm_runtime_put_autosuspend(adev->ddev->dev);
288 } while (last_seq != seq);
289
290 return true;
291 }
292
293 /**
294 * amdgpu_fence_fallback - fallback for hardware interrupts
295 *
296 * @work: delayed work item
297 *
298 * Checks for fence activity.
299 */
300 static void amdgpu_fence_fallback(struct timer_list *t)
301 {
302 struct amdgpu_ring *ring = from_timer(ring, t,
303 fence_drv.fallback_timer);
304
305 if (amdgpu_fence_process(ring))
306 DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name);
307 }
308
309 /**
310 * amdgpu_fence_wait_empty - wait for all fences to signal
311 *
312 * @adev: amdgpu device pointer
313 * @ring: ring index the fence is associated with
314 *
315 * Wait for all fences on the requested ring to signal (all asics).
316 * Returns 0 if the fences have passed, error for all other cases.
317 */
318 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
319 {
320 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
321 struct dma_fence *fence, **ptr;
322 int r;
323
324 if (!seq)
325 return 0;
326
327 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
328 rcu_read_lock();
329 fence = rcu_dereference(*ptr);
330 if (!fence || !dma_fence_get_rcu(fence)) {
331 rcu_read_unlock();
332 return 0;
333 }
334 rcu_read_unlock();
335
336 r = dma_fence_wait(fence, false);
337 dma_fence_put(fence);
338 return r;
339 }
340
341 /**
342 * amdgpu_fence_wait_polling - busy wait for givn sequence number
343 *
344 * @ring: ring index the fence is associated with
345 * @wait_seq: sequence number to wait
346 * @timeout: the timeout for waiting in usecs
347 *
348 * Wait for all fences on the requested ring to signal (all asics).
349 * Returns left time if no timeout, 0 or minus if timeout.
350 */
351 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
352 uint32_t wait_seq,
353 signed long timeout)
354 {
355 uint32_t seq;
356
357 do {
358 seq = amdgpu_fence_read(ring);
359 udelay(5);
360 timeout -= 5;
361 } while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);
362
363 return timeout > 0 ? timeout : 0;
364 }
365 /**
366 * amdgpu_fence_count_emitted - get the count of emitted fences
367 *
368 * @ring: ring the fence is associated with
369 *
370 * Get the number of fences emitted on the requested ring (all asics).
371 * Returns the number of emitted fences on the ring. Used by the
372 * dynpm code to ring track activity.
373 */
374 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
375 {
376 uint64_t emitted;
377
378 /* We are not protected by ring lock when reading the last sequence
379 * but it's ok to report slightly wrong fence count here.
380 */
381 amdgpu_fence_process(ring);
382 emitted = 0x100000000ull;
383 emitted -= atomic_read(&ring->fence_drv.last_seq);
384 emitted += READ_ONCE(ring->fence_drv.sync_seq);
385 return lower_32_bits(emitted);
386 }
387
388 /**
389 * amdgpu_fence_driver_start_ring - make the fence driver
390 * ready for use on the requested ring.
391 *
392 * @ring: ring to start the fence driver on
393 * @irq_src: interrupt source to use for this ring
394 * @irq_type: interrupt type to use for this ring
395 *
396 * Make the fence driver ready for processing (all asics).
397 * Not all asics have all rings, so each asic will only
398 * start the fence driver on the rings it has.
399 * Returns 0 for success, errors for failure.
400 */
401 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
402 struct amdgpu_irq_src *irq_src,
403 unsigned irq_type)
404 {
405 struct amdgpu_device *adev = ring->adev;
406 uint64_t index;
407
408 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
409 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
410 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
411 } else {
412 /* put fence directly behind firmware */
413 index = ALIGN(adev->uvd.fw->size, 8);
414 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
415 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
416 }
417 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
418 amdgpu_irq_get(adev, irq_src, irq_type);
419
420 ring->fence_drv.irq_src = irq_src;
421 ring->fence_drv.irq_type = irq_type;
422 ring->fence_drv.initialized = true;
423
424 DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr "
425 "0x%016"RPIx64", cpu addr 0x%p\n", ring->name,
426 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
427 return 0;
428 }
429
430 /**
431 * amdgpu_fence_driver_init_ring - init the fence driver
432 * for the requested ring.
433 *
434 * @ring: ring to init the fence driver on
435 * @num_hw_submission: number of entries on the hardware queue
436 *
437 * Init the fence driver for the requested ring (all asics).
438 * Helper function for amdgpu_fence_driver_init().
439 */
440 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
441 unsigned num_hw_submission)
442 {
443 struct amdgpu_device *adev = ring->adev;
444 long timeout;
445 int r;
446
447 if (!adev)
448 return -EINVAL;
449
450 /* Check that num_hw_submission is a power of two */
451 if ((num_hw_submission & (num_hw_submission - 1)) != 0)
452 return -EINVAL;
453
454 ring->fence_drv.cpu_addr = NULL;
455 ring->fence_drv.gpu_addr = 0;
456 ring->fence_drv.sync_seq = 0;
457 atomic_set(&ring->fence_drv.last_seq, 0);
458 ring->fence_drv.initialized = false;
459
460 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
461
462 ring->fence_drv.num_fences_mask = num_hw_submission * 2 - 1;
463 spin_lock_init(&ring->fence_drv.lock);
464 ring->fence_drv.fences = kcalloc(num_hw_submission * 2, sizeof(void *),
465 GFP_KERNEL);
466 if (!ring->fence_drv.fences)
467 return -ENOMEM;
468
469 /* No need to setup the GPU scheduler for KIQ ring */
470 if (ring->funcs->type != AMDGPU_RING_TYPE_KIQ) {
471 switch (ring->funcs->type) {
472 case AMDGPU_RING_TYPE_GFX:
473 timeout = adev->gfx_timeout;
474 break;
475 case AMDGPU_RING_TYPE_COMPUTE:
476 timeout = adev->compute_timeout;
477 break;
478 case AMDGPU_RING_TYPE_SDMA:
479 timeout = adev->sdma_timeout;
480 break;
481 default:
482 timeout = adev->video_timeout;
483 break;
484 }
485
486 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
487 num_hw_submission, amdgpu_job_hang_limit,
488 timeout, ring->name);
489 if (r) {
490 DRM_ERROR("Failed to create scheduler on ring %s.\n",
491 ring->name);
492 return r;
493 }
494 }
495
496 return 0;
497 }
498
499 /**
500 * amdgpu_fence_driver_init - init the fence driver
501 * for all possible rings.
502 *
503 * @adev: amdgpu device pointer
504 *
505 * Init the fence driver for all possible rings (all asics).
506 * Not all asics have all rings, so each asic will only
507 * start the fence driver on the rings it has using
508 * amdgpu_fence_driver_start_ring().
509 * Returns 0 for success.
510 */
511 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
512 {
513 if (amdgpu_debugfs_fence_init(adev))
514 dev_err(adev->dev, "fence debugfs file creation failed\n");
515
516 return 0;
517 }
518
519 /**
520 * amdgpu_fence_driver_fini - tear down the fence driver
521 * for all possible rings.
522 *
523 * @adev: amdgpu device pointer
524 *
525 * Tear down the fence driver for all possible rings (all asics).
526 */
527 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
528 {
529 unsigned i, j;
530 int r;
531
532 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
533 struct amdgpu_ring *ring = adev->rings[i];
534
535 if (!ring || !ring->fence_drv.initialized)
536 continue;
537 r = amdgpu_fence_wait_empty(ring);
538 if (r) {
539 /* no need to trigger GPU reset as we are unloading */
540 amdgpu_fence_driver_force_completion(ring);
541 }
542 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
543 ring->fence_drv.irq_type);
544 drm_sched_fini(&ring->sched);
545 del_timer_sync(&ring->fence_drv.fallback_timer);
546 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
547 dma_fence_put(ring->fence_drv.fences[j]);
548 kfree(ring->fence_drv.fences);
549 ring->fence_drv.fences = NULL;
550 ring->fence_drv.initialized = false;
551 spin_lock_destroy(&ring->fence_drv.lock);
552 timer_teardown(&ring->fence_drv.fallback_timer);
553 }
554 }
555
556 /**
557 * amdgpu_fence_driver_suspend - suspend the fence driver
558 * for all possible rings.
559 *
560 * @adev: amdgpu device pointer
561 *
562 * Suspend the fence driver for all possible rings (all asics).
563 */
564 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
565 {
566 int i, r;
567
568 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
569 struct amdgpu_ring *ring = adev->rings[i];
570 if (!ring || !ring->fence_drv.initialized)
571 continue;
572
573 /* wait for gpu to finish processing current batch */
574 r = amdgpu_fence_wait_empty(ring);
575 if (r) {
576 /* delay GPU reset to resume */
577 amdgpu_fence_driver_force_completion(ring);
578 }
579
580 /* disable the interrupt */
581 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
582 ring->fence_drv.irq_type);
583 }
584 }
585
586 /**
587 * amdgpu_fence_driver_resume - resume the fence driver
588 * for all possible rings.
589 *
590 * @adev: amdgpu device pointer
591 *
592 * Resume the fence driver for all possible rings (all asics).
593 * Not all asics have all rings, so each asic will only
594 * start the fence driver on the rings it has using
595 * amdgpu_fence_driver_start_ring().
596 * Returns 0 for success.
597 */
598 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
599 {
600 int i;
601
602 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
603 struct amdgpu_ring *ring = adev->rings[i];
604 if (!ring || !ring->fence_drv.initialized)
605 continue;
606
607 /* enable the interrupt */
608 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
609 ring->fence_drv.irq_type);
610 }
611 }
612
613 /**
614 * amdgpu_fence_driver_force_completion - force signal latest fence of ring
615 *
616 * @ring: fence of the ring to signal
617 *
618 */
619 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
620 {
621 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
622 amdgpu_fence_process(ring);
623 }
624
625 /*
626 * Common fence implementation
627 */
628
629 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
630 {
631 return "amdgpu";
632 }
633
634 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
635 {
636 struct amdgpu_fence *fence = to_amdgpu_fence(f);
637 return (const char *)fence->ring->name;
638 }
639
640 /**
641 * amdgpu_fence_enable_signaling - enable signalling on fence
642 * @fence: fence
643 *
644 * This function is called with fence_queue lock held, and adds a callback
645 * to fence_queue that checks if this fence is signaled, and if so it
646 * signals the fence and removes itself.
647 */
648 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
649 {
650 struct amdgpu_fence *fence = to_amdgpu_fence(f);
651 struct amdgpu_ring *ring = fence->ring;
652
653 if (!timer_pending(&ring->fence_drv.fallback_timer))
654 amdgpu_fence_schedule_fallback(ring);
655
656 DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
657
658 return true;
659 }
660
661 /**
662 * amdgpu_fence_free - free up the fence memory
663 *
664 * @rcu: RCU callback head
665 *
666 * Free up the fence memory after the RCU grace period.
667 */
668 static void amdgpu_fence_free(struct rcu_head *rcu)
669 {
670 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
671 struct amdgpu_fence *fence = to_amdgpu_fence(f);
672 kmem_cache_free(amdgpu_fence_slab, fence);
673 }
674
675 /**
676 * amdgpu_fence_release - callback that fence can be freed
677 *
678 * @fence: fence
679 *
680 * This function is called when the reference count becomes zero.
681 * It just RCU schedules freeing up the fence.
682 */
683 static void amdgpu_fence_release(struct dma_fence *f)
684 {
685 call_rcu(&f->rcu, amdgpu_fence_free);
686 }
687
688 static const struct dma_fence_ops amdgpu_fence_ops = {
689 .get_driver_name = amdgpu_fence_get_driver_name,
690 .get_timeline_name = amdgpu_fence_get_timeline_name,
691 .enable_signaling = amdgpu_fence_enable_signaling,
692 .release = amdgpu_fence_release,
693 };
694
695 /*
696 * Fence debugfs
697 */
698 #if defined(CONFIG_DEBUG_FS)
699 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
700 {
701 struct drm_info_node *node = (struct drm_info_node *)m->private;
702 struct drm_device *dev = node->minor->dev;
703 struct amdgpu_device *adev = dev->dev_private;
704 int i;
705
706 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
707 struct amdgpu_ring *ring = adev->rings[i];
708 if (!ring || !ring->fence_drv.initialized)
709 continue;
710
711 amdgpu_fence_process(ring);
712
713 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
714 seq_printf(m, "Last signaled fence 0x%08x\n",
715 atomic_read(&ring->fence_drv.last_seq));
716 seq_printf(m, "Last emitted 0x%08x\n",
717 ring->fence_drv.sync_seq);
718
719 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
720 ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
721 seq_printf(m, "Last signaled trailing fence 0x%08x\n",
722 le32_to_cpu(*ring->trail_fence_cpu_addr));
723 seq_printf(m, "Last emitted 0x%08x\n",
724 ring->trail_seq);
725 }
726
727 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
728 continue;
729
730 /* set in CP_VMID_PREEMPT and preemption occurred */
731 seq_printf(m, "Last preempted 0x%08x\n",
732 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
733 /* set in CP_VMID_RESET and reset occurred */
734 seq_printf(m, "Last reset 0x%08x\n",
735 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
736 /* Both preemption and reset occurred */
737 seq_printf(m, "Last both 0x%08x\n",
738 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
739 }
740 return 0;
741 }
742
743 /**
744 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
745 *
746 * Manually trigger a gpu reset at the next fence wait.
747 */
748 static int amdgpu_debugfs_gpu_recover(struct seq_file *m, void *data)
749 {
750 struct drm_info_node *node = (struct drm_info_node *) m->private;
751 struct drm_device *dev = node->minor->dev;
752 struct amdgpu_device *adev = dev->dev_private;
753 int r;
754
755 r = pm_runtime_get_sync(dev->dev);
756 if (r < 0)
757 return 0;
758
759 seq_printf(m, "gpu recover\n");
760 amdgpu_device_gpu_recover(adev, NULL);
761
762 pm_runtime_mark_last_busy(dev->dev);
763 pm_runtime_put_autosuspend(dev->dev);
764
765 return 0;
766 }
767
768 static const struct drm_info_list amdgpu_debugfs_fence_list[] = {
769 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
770 {"amdgpu_gpu_recover", &amdgpu_debugfs_gpu_recover, 0, NULL}
771 };
772
773 static const struct drm_info_list amdgpu_debugfs_fence_list_sriov[] = {
774 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
775 };
776 #endif
777
778 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
779 {
780 #if defined(CONFIG_DEBUG_FS)
781 if (amdgpu_sriov_vf(adev))
782 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list_sriov, 1);
783 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
784 #else
785 return 0;
786 #endif
787 }
788
789