amdgpu_fence.c revision 1.7 1 /* $NetBSD: amdgpu_fence.c,v 1.7 2021/12/18 23:44:58 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.7 2021/12/18 23:44:58 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 #ifdef __NetBSD__
294 static int amdgpu_fence_check_signaled(struct amdgpu_fence *);
295
296 static void
297 amdgpu_fence_wakeup_locked(struct amdgpu_ring *ring)
298 {
299 struct amdgpu_fence *fence, *next;
300
301 BUG_ON(!spin_is_locked(&ring->fence_drv.fence_lock));
302 DRM_SPIN_WAKEUP_ALL(&ring->fence_drv.fence_queue,
303 &ring->fence_drv.fence_lock);
304 TAILQ_FOREACH_SAFE(fence, &ring->fence_drv.fence_check, fence_check,
305 next) {
306 amdgpu_fence_check_signaled(fence);
307 }
308 }
309 #endif
310
311
312 /**
313 * amdgpu_fence_fallback - fallback for hardware interrupts
314 *
315 * @work: delayed work item
316 *
317 * Checks for fence activity.
318 */
319 static void amdgpu_fence_fallback(struct timer_list *t)
320 {
321 struct amdgpu_ring *ring = from_timer(ring, t,
322 fence_drv.fallback_timer);
323
324 if (amdgpu_fence_process(ring))
325 DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name);
326 }
327
328 /**
329 * amdgpu_fence_wait_empty - wait for all fences to signal
330 *
331 * @adev: amdgpu device pointer
332 * @ring: ring index the fence is associated with
333 *
334 * Wait for all fences on the requested ring to signal (all asics).
335 * Returns 0 if the fences have passed, error for all other cases.
336 */
337 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
338 {
339 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
340 struct dma_fence *fence, **ptr;
341 int r;
342
343 if (!seq)
344 return 0;
345
346 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
347 rcu_read_lock();
348 fence = rcu_dereference(*ptr);
349 if (!fence || !dma_fence_get_rcu(fence)) {
350 rcu_read_unlock();
351 return 0;
352 }
353 rcu_read_unlock();
354
355 r = dma_fence_wait(fence, false);
356 dma_fence_put(fence);
357 return r;
358 }
359
360 /**
361 * amdgpu_fence_wait_polling - busy wait for givn sequence number
362 *
363 * @ring: ring index the fence is associated with
364 * @wait_seq: sequence number to wait
365 * @timeout: the timeout for waiting in usecs
366 *
367 * Wait for all fences on the requested ring to signal (all asics).
368 * Returns left time if no timeout, 0 or minus if timeout.
369 */
370 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
371 uint32_t wait_seq,
372 signed long timeout)
373 {
374 uint32_t seq;
375
376 do {
377 seq = amdgpu_fence_read(ring);
378 udelay(5);
379 timeout -= 5;
380 } while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);
381
382 return timeout > 0 ? timeout : 0;
383 }
384 /**
385 * amdgpu_fence_count_emitted - get the count of emitted fences
386 *
387 * @ring: ring the fence is associated with
388 *
389 * Get the number of fences emitted on the requested ring (all asics).
390 * Returns the number of emitted fences on the ring. Used by the
391 * dynpm code to ring track activity.
392 */
393 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
394 {
395 uint64_t emitted;
396
397 /* We are not protected by ring lock when reading the last sequence
398 * but it's ok to report slightly wrong fence count here.
399 */
400 amdgpu_fence_process(ring);
401 emitted = 0x100000000ull;
402 emitted -= atomic_read(&ring->fence_drv.last_seq);
403 emitted += READ_ONCE(ring->fence_drv.sync_seq);
404 return lower_32_bits(emitted);
405 }
406
407 /**
408 * amdgpu_fence_driver_start_ring - make the fence driver
409 * ready for use on the requested ring.
410 *
411 * @ring: ring to start the fence driver on
412 * @irq_src: interrupt source to use for this ring
413 * @irq_type: interrupt type to use for this ring
414 *
415 * Make the fence driver ready for processing (all asics).
416 * Not all asics have all rings, so each asic will only
417 * start the fence driver on the rings it has.
418 * Returns 0 for success, errors for failure.
419 */
420 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
421 struct amdgpu_irq_src *irq_src,
422 unsigned irq_type)
423 {
424 struct amdgpu_device *adev = ring->adev;
425 uint64_t index;
426
427 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
428 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
429 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
430 } else {
431 /* put fence directly behind firmware */
432 index = ALIGN(adev->uvd.fw->size, 8);
433 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
434 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
435 }
436 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
437 amdgpu_irq_get(adev, irq_src, irq_type);
438
439 ring->fence_drv.irq_src = irq_src;
440 ring->fence_drv.irq_type = irq_type;
441 ring->fence_drv.initialized = true;
442
443 DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr "
444 "0x%016"RPIx64", cpu addr 0x%p\n", ring->name,
445 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
446 return 0;
447 }
448
449 /**
450 * amdgpu_fence_driver_init_ring - init the fence driver
451 * for the requested ring.
452 *
453 * @ring: ring to init the fence driver on
454 * @num_hw_submission: number of entries on the hardware queue
455 *
456 * Init the fence driver for the requested ring (all asics).
457 * Helper function for amdgpu_fence_driver_init().
458 */
459 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
460 unsigned num_hw_submission)
461 {
462 struct amdgpu_device *adev = ring->adev;
463 long timeout;
464 int r;
465
466 if (!adev)
467 return -EINVAL;
468
469 /* Check that num_hw_submission is a power of two */
470 if ((num_hw_submission & (num_hw_submission - 1)) != 0)
471 return -EINVAL;
472
473 ring->fence_drv.cpu_addr = NULL;
474 ring->fence_drv.gpu_addr = 0;
475 ring->fence_drv.sync_seq = 0;
476 atomic_set(&ring->fence_drv.last_seq, 0);
477 ring->fence_drv.initialized = false;
478
479 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
480
481 ring->fence_drv.num_fences_mask = num_hw_submission * 2 - 1;
482 spin_lock_init(&ring->fence_drv.lock);
483 ring->fence_drv.fences = kcalloc(num_hw_submission * 2, sizeof(void *),
484 GFP_KERNEL);
485 if (!ring->fence_drv.fences)
486 return -ENOMEM;
487
488 /* No need to setup the GPU scheduler for KIQ ring */
489 if (ring->funcs->type != AMDGPU_RING_TYPE_KIQ) {
490 switch (ring->funcs->type) {
491 case AMDGPU_RING_TYPE_GFX:
492 timeout = adev->gfx_timeout;
493 break;
494 case AMDGPU_RING_TYPE_COMPUTE:
495 timeout = adev->compute_timeout;
496 break;
497 case AMDGPU_RING_TYPE_SDMA:
498 timeout = adev->sdma_timeout;
499 break;
500 default:
501 timeout = adev->video_timeout;
502 break;
503 }
504
505 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
506 num_hw_submission, amdgpu_job_hang_limit,
507 timeout, ring->name);
508 if (r) {
509 DRM_ERROR("Failed to create scheduler on ring %s.\n",
510 ring->name);
511 return r;
512 }
513 }
514
515 return 0;
516 }
517
518 /**
519 * amdgpu_fence_driver_init - init the fence driver
520 * for all possible rings.
521 *
522 * @adev: amdgpu device pointer
523 *
524 * Init the fence driver for all possible rings (all asics).
525 * Not all asics have all rings, so each asic will only
526 * start the fence driver on the rings it has using
527 * amdgpu_fence_driver_start_ring().
528 * Returns 0 for success.
529 */
530 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
531 {
532 if (amdgpu_debugfs_fence_init(adev))
533 dev_err(adev->dev, "fence debugfs file creation failed\n");
534
535 return 0;
536 }
537
538 /**
539 * amdgpu_fence_driver_fini - tear down the fence driver
540 * for all possible rings.
541 *
542 * @adev: amdgpu device pointer
543 *
544 * Tear down the fence driver for all possible rings (all asics).
545 */
546 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
547 {
548 unsigned i, j;
549 int r;
550
551 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
552 struct amdgpu_ring *ring = adev->rings[i];
553
554 if (!ring || !ring->fence_drv.initialized)
555 continue;
556 r = amdgpu_fence_wait_empty(ring);
557 if (r) {
558 /* no need to trigger GPU reset as we are unloading */
559 amdgpu_fence_driver_force_completion(ring);
560 }
561 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
562 ring->fence_drv.irq_type);
563 drm_sched_fini(&ring->sched);
564 del_timer_sync(&ring->fence_drv.fallback_timer);
565 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
566 dma_fence_put(ring->fence_drv.fences[j]);
567 kfree(ring->fence_drv.fences);
568 ring->fence_drv.fences = NULL;
569 ring->fence_drv.initialized = false;
570 #ifdef __NetBSD__
571 BUG_ON(!TAILQ_EMPTY(&ring->fence_drv.fence_check));
572 DRM_DESTROY_WAITQUEUE(&ring->fence_drv.fence_queue);
573 spin_lock_destroy(&ring->fence_drv.fence_lock);
574 #endif
575 }
576 }
577
578 /**
579 * amdgpu_fence_driver_suspend - suspend the fence driver
580 * for all possible rings.
581 *
582 * @adev: amdgpu device pointer
583 *
584 * Suspend the fence driver for all possible rings (all asics).
585 */
586 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
587 {
588 int i, r;
589
590 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
591 struct amdgpu_ring *ring = adev->rings[i];
592 if (!ring || !ring->fence_drv.initialized)
593 continue;
594
595 /* wait for gpu to finish processing current batch */
596 r = amdgpu_fence_wait_empty(ring);
597 if (r) {
598 /* delay GPU reset to resume */
599 amdgpu_fence_driver_force_completion(ring);
600 }
601
602 /* disable the interrupt */
603 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
604 ring->fence_drv.irq_type);
605 }
606 }
607
608 /**
609 * amdgpu_fence_driver_resume - resume the fence driver
610 * for all possible rings.
611 *
612 * @adev: amdgpu device pointer
613 *
614 * Resume the fence driver for all possible rings (all asics).
615 * Not all asics have all rings, so each asic will only
616 * start the fence driver on the rings it has using
617 * amdgpu_fence_driver_start_ring().
618 * Returns 0 for success.
619 */
620 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
621 {
622 int i;
623
624 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
625 struct amdgpu_ring *ring = adev->rings[i];
626 if (!ring || !ring->fence_drv.initialized)
627 continue;
628
629 /* enable the interrupt */
630 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
631 ring->fence_drv.irq_type);
632 }
633 }
634
635 /**
636 * amdgpu_fence_driver_force_completion - force signal latest fence of ring
637 *
638 * @ring: fence of the ring to signal
639 *
640 */
641 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
642 {
643 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
644 amdgpu_fence_process(ring);
645 }
646
647 /*
648 * Common fence implementation
649 */
650
651 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
652 {
653 return "amdgpu";
654 }
655
656 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
657 {
658 struct amdgpu_fence *fence = to_amdgpu_fence(f);
659 return (const char *)fence->ring->name;
660 }
661
662 /**
663 * amdgpu_fence_enable_signaling - enable signalling on fence
664 * @fence: fence
665 *
666 * This function is called with fence_queue lock held, and adds a callback
667 * to fence_queue that checks if this fence is signaled, and if so it
668 * signals the fence and removes itself.
669 */
670 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
671 {
672 struct amdgpu_fence *fence = to_amdgpu_fence(f);
673 struct amdgpu_ring *ring = fence->ring;
674
675 if (!timer_pending(&ring->fence_drv.fallback_timer))
676 amdgpu_fence_schedule_fallback(ring);
677
678 DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
679
680 return true;
681 }
682
683 /**
684 * amdgpu_fence_free - free up the fence memory
685 *
686 * @rcu: RCU callback head
687 *
688 * Free up the fence memory after the RCU grace period.
689 */
690 static void amdgpu_fence_free(struct rcu_head *rcu)
691 {
692 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
693 struct amdgpu_fence *fence = to_amdgpu_fence(f);
694 kmem_cache_free(amdgpu_fence_slab, fence);
695 }
696
697 /**
698 * amdgpu_fence_release - callback that fence can be freed
699 *
700 * @fence: fence
701 *
702 * This function is called when the reference count becomes zero.
703 * It just RCU schedules freeing up the fence.
704 */
705 static void amdgpu_fence_release(struct dma_fence *f)
706 {
707 call_rcu(&f->rcu, amdgpu_fence_free);
708 }
709
710 static const struct dma_fence_ops amdgpu_fence_ops = {
711 .get_driver_name = amdgpu_fence_get_driver_name,
712 .get_timeline_name = amdgpu_fence_get_timeline_name,
713 .enable_signaling = amdgpu_fence_enable_signaling,
714 .release = amdgpu_fence_release,
715 };
716
717 /*
718 * Fence debugfs
719 */
720 #if defined(CONFIG_DEBUG_FS)
721 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
722 {
723 struct drm_info_node *node = (struct drm_info_node *)m->private;
724 struct drm_device *dev = node->minor->dev;
725 struct amdgpu_device *adev = dev->dev_private;
726 int i;
727
728 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
729 struct amdgpu_ring *ring = adev->rings[i];
730 if (!ring || !ring->fence_drv.initialized)
731 continue;
732
733 amdgpu_fence_process(ring);
734
735 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
736 seq_printf(m, "Last signaled fence 0x%08x\n",
737 atomic_read(&ring->fence_drv.last_seq));
738 seq_printf(m, "Last emitted 0x%08x\n",
739 ring->fence_drv.sync_seq);
740
741 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
742 ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
743 seq_printf(m, "Last signaled trailing fence 0x%08x\n",
744 le32_to_cpu(*ring->trail_fence_cpu_addr));
745 seq_printf(m, "Last emitted 0x%08x\n",
746 ring->trail_seq);
747 }
748
749 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
750 continue;
751
752 /* set in CP_VMID_PREEMPT and preemption occurred */
753 seq_printf(m, "Last preempted 0x%08x\n",
754 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
755 /* set in CP_VMID_RESET and reset occurred */
756 seq_printf(m, "Last reset 0x%08x\n",
757 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
758 /* Both preemption and reset occurred */
759 seq_printf(m, "Last both 0x%08x\n",
760 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
761 }
762 return 0;
763 }
764
765 /**
766 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
767 *
768 * Manually trigger a gpu reset at the next fence wait.
769 */
770 static int amdgpu_debugfs_gpu_recover(struct seq_file *m, void *data)
771 {
772 struct drm_info_node *node = (struct drm_info_node *) m->private;
773 struct drm_device *dev = node->minor->dev;
774 struct amdgpu_device *adev = dev->dev_private;
775 int r;
776
777 r = pm_runtime_get_sync(dev->dev);
778 if (r < 0)
779 return 0;
780
781 seq_printf(m, "gpu recover\n");
782 amdgpu_device_gpu_recover(adev, NULL);
783
784 pm_runtime_mark_last_busy(dev->dev);
785 pm_runtime_put_autosuspend(dev->dev);
786
787 return 0;
788 }
789
790 static const struct drm_info_list amdgpu_debugfs_fence_list[] = {
791 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
792 {"amdgpu_gpu_recover", &amdgpu_debugfs_gpu_recover, 0, NULL}
793 };
794
795 static const struct drm_info_list amdgpu_debugfs_fence_list_sriov[] = {
796 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
797 };
798 #endif
799
800 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
801 {
802 #if defined(CONFIG_DEBUG_FS)
803 if (amdgpu_sriov_vf(adev))
804 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list_sriov, 1);
805 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
806 #else
807 return 0;
808 #endif
809 }
810
811