1 /*
2 * Copyright 2009 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26 /*
27 * Authors:
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Dave Airlie
30 */
31 #include <linux/seq_file.h>
32 #include <linux/atomic.h>
33 #include <linux/wait.h>
34 #include <linux/kref.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <linux/pm_runtime.h>
38
39 #include <drm/drm_drv.h>
40 #include "amdgpu.h"
41 #include "amdgpu_trace.h"
42 #include "amdgpu_reset.h"
43
44 static struct kmem_cache *amdgpu_fence_slab;
45
amdgpu_fence_slab_init(void)46 int amdgpu_fence_slab_init(void)
47 {
48 amdgpu_fence_slab = kmem_cache_create(
49 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
50 SLAB_HWCACHE_ALIGN, NULL);
51 if (!amdgpu_fence_slab)
52 return -ENOMEM;
53 return 0;
54 }
55
amdgpu_fence_slab_fini(void)56 void amdgpu_fence_slab_fini(void)
57 {
58 rcu_barrier();
59 kmem_cache_destroy(amdgpu_fence_slab);
60 }
61 /*
62 * Cast helper
63 */
64 static const struct dma_fence_ops amdgpu_fence_ops;
65 static const struct dma_fence_ops amdgpu_job_fence_ops;
to_amdgpu_fence(struct dma_fence * f)66 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
67 {
68 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
69
70 if (__f->base.ops == &amdgpu_fence_ops ||
71 __f->base.ops == &amdgpu_job_fence_ops)
72 return __f;
73
74 return NULL;
75 }
76
77 /**
78 * amdgpu_fence_write - write a fence value
79 *
80 * @ring: ring the fence is associated with
81 * @seq: sequence number to write
82 *
83 * Writes a fence value to memory (all asics).
84 */
amdgpu_fence_write(struct amdgpu_ring * ring,u32 seq)85 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
86 {
87 struct amdgpu_fence_driver *drv = &ring->fence_drv;
88
89 if (drv->cpu_addr)
90 *drv->cpu_addr = cpu_to_le32(seq);
91 }
92
93 /**
94 * amdgpu_fence_read - read a fence value
95 *
96 * @ring: ring the fence is associated with
97 *
98 * Reads a fence value from memory (all asics).
99 * Returns the value of the fence read from memory.
100 */
amdgpu_fence_read(struct amdgpu_ring * ring)101 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
102 {
103 struct amdgpu_fence_driver *drv = &ring->fence_drv;
104 u32 seq = 0;
105
106 if (drv->cpu_addr)
107 seq = le32_to_cpu(*drv->cpu_addr);
108 else
109 seq = atomic_read(&drv->last_seq);
110
111 return seq;
112 }
113
114 /**
115 * amdgpu_fence_emit - emit a fence on the requested ring
116 *
117 * @ring: ring the fence is associated with
118 * @f: resulting fence object
119 * @job: job the fence is embedded in
120 * @flags: flags to pass into the subordinate .emit_fence() call
121 *
122 * Emits a fence command on the requested ring (all asics).
123 * Returns 0 on success, -ENOMEM on failure.
124 */
amdgpu_fence_emit(struct amdgpu_ring * ring,struct dma_fence ** f,struct amdgpu_job * job,unsigned int flags)125 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amdgpu_job *job,
126 unsigned int flags)
127 {
128 struct amdgpu_device *adev = ring->adev;
129 struct dma_fence *fence;
130 struct amdgpu_fence *am_fence;
131 struct dma_fence __rcu **ptr;
132 uint32_t seq;
133 int r;
134
135 if (job == NULL) {
136 /* create a sperate hw fence */
137 am_fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_ATOMIC);
138 if (am_fence == NULL)
139 return -ENOMEM;
140 } else {
141 /* take use of job-embedded fence */
142 am_fence = &job->hw_fence;
143 }
144 fence = &am_fence->base;
145 am_fence->ring = ring;
146
147 seq = ++ring->fence_drv.sync_seq;
148 if (job && job->job_run_counter) {
149 /* reinit seq for resubmitted jobs */
150 fence->seqno = seq;
151 /* TO be inline with external fence creation and other drivers */
152 dma_fence_get(fence);
153 } else {
154 if (job) {
155 dma_fence_init(fence, &amdgpu_job_fence_ops,
156 &ring->fence_drv.lock,
157 adev->fence_context + ring->idx, seq);
158 /* Against remove in amdgpu_job_{free, free_cb} */
159 dma_fence_get(fence);
160 } else {
161 dma_fence_init(fence, &amdgpu_fence_ops,
162 &ring->fence_drv.lock,
163 adev->fence_context + ring->idx, seq);
164 }
165 }
166
167 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
168 seq, flags | AMDGPU_FENCE_FLAG_INT);
169 pm_runtime_get_noresume(adev_to_drm(adev)->dev);
170 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
171 if (unlikely(rcu_dereference_protected(*ptr, 1))) {
172 struct dma_fence *old;
173
174 rcu_read_lock();
175 old = dma_fence_get_rcu_safe(ptr);
176 rcu_read_unlock();
177
178 if (old) {
179 r = dma_fence_wait(old, false);
180 dma_fence_put(old);
181 if (r)
182 return r;
183 }
184 }
185
186 to_amdgpu_fence(fence)->start_timestamp = ktime_get();
187
188 /* This function can't be called concurrently anyway, otherwise
189 * emitting the fence would mess up the hardware ring buffer.
190 */
191 rcu_assign_pointer(*ptr, dma_fence_get(fence));
192
193 *f = fence;
194
195 return 0;
196 }
197
198 /**
199 * amdgpu_fence_emit_polling - emit a fence on the requeste ring
200 *
201 * @ring: ring the fence is associated with
202 * @s: resulting sequence number
203 * @timeout: the timeout for waiting in usecs
204 *
205 * Emits a fence command on the requested ring (all asics).
206 * Used For polling fence.
207 * Returns 0 on success, -ENOMEM on failure.
208 */
amdgpu_fence_emit_polling(struct amdgpu_ring * ring,uint32_t * s,uint32_t timeout)209 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s,
210 uint32_t timeout)
211 {
212 uint32_t seq;
213 signed long r;
214
215 if (!s)
216 return -EINVAL;
217
218 seq = ++ring->fence_drv.sync_seq;
219 r = amdgpu_fence_wait_polling(ring,
220 seq - ring->fence_drv.num_fences_mask,
221 timeout);
222 if (r < 1)
223 return -ETIMEDOUT;
224
225 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
226 seq, 0);
227
228 *s = seq;
229
230 return 0;
231 }
232
233 /**
234 * amdgpu_fence_schedule_fallback - schedule fallback check
235 *
236 * @ring: pointer to struct amdgpu_ring
237 *
238 * Start a timer as fallback to our interrupts.
239 */
amdgpu_fence_schedule_fallback(struct amdgpu_ring * ring)240 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
241 {
242 mod_timer(&ring->fence_drv.fallback_timer,
243 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
244 }
245
246 /**
247 * amdgpu_fence_process - check for fence activity
248 *
249 * @ring: pointer to struct amdgpu_ring
250 *
251 * Checks the current fence value and calculates the last
252 * signalled fence value. Wakes the fence queue if the
253 * sequence number has increased.
254 *
255 * Returns true if fence was processed
256 */
amdgpu_fence_process(struct amdgpu_ring * ring)257 bool amdgpu_fence_process(struct amdgpu_ring *ring)
258 {
259 struct amdgpu_fence_driver *drv = &ring->fence_drv;
260 struct amdgpu_device *adev = ring->adev;
261 uint32_t seq, last_seq;
262
263 do {
264 last_seq = atomic_read(&ring->fence_drv.last_seq);
265 seq = amdgpu_fence_read(ring);
266
267 } while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
268
269 if (del_timer(&ring->fence_drv.fallback_timer) &&
270 seq != ring->fence_drv.sync_seq)
271 amdgpu_fence_schedule_fallback(ring);
272
273 if (unlikely(seq == last_seq))
274 return false;
275
276 last_seq &= drv->num_fences_mask;
277 seq &= drv->num_fences_mask;
278
279 do {
280 struct dma_fence *fence, **ptr;
281
282 ++last_seq;
283 last_seq &= drv->num_fences_mask;
284 ptr = &drv->fences[last_seq];
285
286 /* There is always exactly one thread signaling this fence slot */
287 fence = rcu_dereference_protected(*ptr, 1);
288 RCU_INIT_POINTER(*ptr, NULL);
289
290 if (!fence)
291 continue;
292
293 dma_fence_signal(fence);
294 dma_fence_put(fence);
295 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
296 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
297 } while (last_seq != seq);
298
299 return true;
300 }
301
302 /**
303 * amdgpu_fence_fallback - fallback for hardware interrupts
304 *
305 * @t: timer context used to obtain the pointer to ring structure
306 *
307 * Checks for fence activity.
308 */
amdgpu_fence_fallback(struct timer_list * t)309 static void amdgpu_fence_fallback(struct timer_list *t)
310 {
311 struct amdgpu_ring *ring = from_timer(ring, t,
312 fence_drv.fallback_timer);
313
314 if (amdgpu_fence_process(ring))
315 DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name);
316 }
317
318 /**
319 * amdgpu_fence_wait_empty - wait for all fences to signal
320 *
321 * @ring: ring index the fence is associated with
322 *
323 * Wait for all fences on the requested ring to signal (all asics).
324 * Returns 0 if the fences have passed, error for all other cases.
325 */
amdgpu_fence_wait_empty(struct amdgpu_ring * ring)326 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
327 {
328 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
329 struct dma_fence *fence, **ptr;
330 int r;
331
332 if (!seq)
333 return 0;
334
335 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
336 rcu_read_lock();
337 fence = rcu_dereference(*ptr);
338 if (!fence || !dma_fence_get_rcu(fence)) {
339 rcu_read_unlock();
340 return 0;
341 }
342 rcu_read_unlock();
343
344 r = dma_fence_wait(fence, false);
345 dma_fence_put(fence);
346 return r;
347 }
348
349 /**
350 * amdgpu_fence_wait_polling - busy wait for givn sequence number
351 *
352 * @ring: ring index the fence is associated with
353 * @wait_seq: sequence number to wait
354 * @timeout: the timeout for waiting in usecs
355 *
356 * Wait for all fences on the requested ring to signal (all asics).
357 * Returns left time if no timeout, 0 or minus if timeout.
358 */
amdgpu_fence_wait_polling(struct amdgpu_ring * ring,uint32_t wait_seq,signed long timeout)359 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
360 uint32_t wait_seq,
361 signed long timeout)
362 {
363
364 while ((int32_t)(wait_seq - amdgpu_fence_read(ring)) > 0 && timeout > 0) {
365 udelay(2);
366 timeout -= 2;
367 }
368 return timeout > 0 ? timeout : 0;
369 }
370 /**
371 * amdgpu_fence_count_emitted - get the count of emitted fences
372 *
373 * @ring: ring the fence is associated with
374 *
375 * Get the number of fences emitted on the requested ring (all asics).
376 * Returns the number of emitted fences on the ring. Used by the
377 * dynpm code to ring track activity.
378 */
amdgpu_fence_count_emitted(struct amdgpu_ring * ring)379 unsigned int amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
380 {
381 uint64_t emitted;
382
383 /* We are not protected by ring lock when reading the last sequence
384 * but it's ok to report slightly wrong fence count here.
385 */
386 emitted = 0x100000000ull;
387 emitted -= atomic_read(&ring->fence_drv.last_seq);
388 emitted += READ_ONCE(ring->fence_drv.sync_seq);
389 return lower_32_bits(emitted);
390 }
391
392 /**
393 * amdgpu_fence_last_unsignaled_time_us - the time fence emitted until now
394 * @ring: ring the fence is associated with
395 *
396 * Find the earliest fence unsignaled until now, calculate the time delta
397 * between the time fence emitted and now.
398 */
amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring * ring)399 u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
400 {
401 struct amdgpu_fence_driver *drv = &ring->fence_drv;
402 struct dma_fence *fence;
403 uint32_t last_seq, sync_seq;
404
405 last_seq = atomic_read(&ring->fence_drv.last_seq);
406 sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
407 if (last_seq == sync_seq)
408 return 0;
409
410 ++last_seq;
411 last_seq &= drv->num_fences_mask;
412 fence = drv->fences[last_seq];
413 if (!fence)
414 return 0;
415
416 return ktime_us_delta(ktime_get(),
417 to_amdgpu_fence(fence)->start_timestamp);
418 }
419
420 /**
421 * amdgpu_fence_update_start_timestamp - update the timestamp of the fence
422 * @ring: ring the fence is associated with
423 * @seq: the fence seq number to update.
424 * @timestamp: the start timestamp to update.
425 *
426 * The function called at the time the fence and related ib is about to
427 * resubmit to gpu in MCBP scenario. Thus we do not consider race condition
428 * with amdgpu_fence_process to modify the same fence.
429 */
amdgpu_fence_update_start_timestamp(struct amdgpu_ring * ring,uint32_t seq,ktime_t timestamp)430 void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq, ktime_t timestamp)
431 {
432 struct amdgpu_fence_driver *drv = &ring->fence_drv;
433 struct dma_fence *fence;
434
435 seq &= drv->num_fences_mask;
436 fence = drv->fences[seq];
437 if (!fence)
438 return;
439
440 to_amdgpu_fence(fence)->start_timestamp = timestamp;
441 }
442
443 /**
444 * amdgpu_fence_driver_start_ring - make the fence driver
445 * ready for use on the requested ring.
446 *
447 * @ring: ring to start the fence driver on
448 * @irq_src: interrupt source to use for this ring
449 * @irq_type: interrupt type to use for this ring
450 *
451 * Make the fence driver ready for processing (all asics).
452 * Not all asics have all rings, so each asic will only
453 * start the fence driver on the rings it has.
454 * Returns 0 for success, errors for failure.
455 */
amdgpu_fence_driver_start_ring(struct amdgpu_ring * ring,struct amdgpu_irq_src * irq_src,unsigned int irq_type)456 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
457 struct amdgpu_irq_src *irq_src,
458 unsigned int irq_type)
459 {
460 struct amdgpu_device *adev = ring->adev;
461 uint64_t index;
462
463 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
464 ring->fence_drv.cpu_addr = ring->fence_cpu_addr;
465 ring->fence_drv.gpu_addr = ring->fence_gpu_addr;
466 } else {
467 /* put fence directly behind firmware */
468 index = ALIGN(adev->uvd.fw->size, 8);
469 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
470 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
471 }
472 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
473
474 ring->fence_drv.irq_src = irq_src;
475 ring->fence_drv.irq_type = irq_type;
476 ring->fence_drv.initialized = true;
477
478 DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr 0x%016llx\n",
479 ring->name, ring->fence_drv.gpu_addr);
480 return 0;
481 }
482
483 /**
484 * amdgpu_fence_driver_init_ring - init the fence driver
485 * for the requested ring.
486 *
487 * @ring: ring to init the fence driver on
488 *
489 * Init the fence driver for the requested ring (all asics).
490 * Helper function for amdgpu_fence_driver_init().
491 */
amdgpu_fence_driver_init_ring(struct amdgpu_ring * ring)492 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
493 {
494 struct amdgpu_device *adev = ring->adev;
495
496 if (!adev)
497 return -EINVAL;
498
499 if (!is_power_of_2(ring->num_hw_submission))
500 return -EINVAL;
501
502 ring->fence_drv.cpu_addr = NULL;
503 ring->fence_drv.gpu_addr = 0;
504 ring->fence_drv.sync_seq = 0;
505 atomic_set(&ring->fence_drv.last_seq, 0);
506 ring->fence_drv.initialized = false;
507
508 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
509
510 ring->fence_drv.num_fences_mask = ring->num_hw_submission * 2 - 1;
511 spin_lock_init(&ring->fence_drv.lock);
512 ring->fence_drv.fences = kcalloc(ring->num_hw_submission * 2, sizeof(void *),
513 GFP_KERNEL);
514
515 if (!ring->fence_drv.fences)
516 return -ENOMEM;
517
518 return 0;
519 }
520
521 /**
522 * amdgpu_fence_driver_sw_init - init the fence driver
523 * for all possible rings.
524 *
525 * @adev: amdgpu device pointer
526 *
527 * Init the fence driver for all possible rings (all asics).
528 * Not all asics have all rings, so each asic will only
529 * start the fence driver on the rings it has using
530 * amdgpu_fence_driver_start_ring().
531 * Returns 0 for success.
532 */
amdgpu_fence_driver_sw_init(struct amdgpu_device * adev)533 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev)
534 {
535 return 0;
536 }
537
538 /**
539 * amdgpu_fence_need_ring_interrupt_restore - helper function to check whether
540 * fence driver interrupts need to be restored.
541 *
542 * @ring: ring that to be checked
543 *
544 * Interrupts for rings that belong to GFX IP don't need to be restored
545 * when the target power state is s0ix.
546 *
547 * Return true if need to restore interrupts, false otherwise.
548 */
amdgpu_fence_need_ring_interrupt_restore(struct amdgpu_ring * ring)549 static bool amdgpu_fence_need_ring_interrupt_restore(struct amdgpu_ring *ring)
550 {
551 struct amdgpu_device *adev = ring->adev;
552 bool is_gfx_power_domain = false;
553
554 switch (ring->funcs->type) {
555 case AMDGPU_RING_TYPE_SDMA:
556 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */
557 if (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(5, 0, 0))
558 is_gfx_power_domain = true;
559 break;
560 case AMDGPU_RING_TYPE_GFX:
561 case AMDGPU_RING_TYPE_COMPUTE:
562 case AMDGPU_RING_TYPE_KIQ:
563 case AMDGPU_RING_TYPE_MES:
564 is_gfx_power_domain = true;
565 break;
566 default:
567 break;
568 }
569
570 return !(adev->in_s0ix && is_gfx_power_domain);
571 }
572
573 /**
574 * amdgpu_fence_driver_hw_fini - tear down the fence driver
575 * for all possible rings.
576 *
577 * @adev: amdgpu device pointer
578 *
579 * Tear down the fence driver for all possible rings (all asics).
580 */
amdgpu_fence_driver_hw_fini(struct amdgpu_device * adev)581 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev)
582 {
583 int i, r;
584
585 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
586 struct amdgpu_ring *ring = adev->rings[i];
587
588 if (!ring || !ring->fence_drv.initialized)
589 continue;
590
591 /* You can't wait for HW to signal if it's gone */
592 if (!drm_dev_is_unplugged(adev_to_drm(adev)))
593 r = amdgpu_fence_wait_empty(ring);
594 else
595 r = -ENODEV;
596 /* no need to trigger GPU reset as we are unloading */
597 if (r)
598 amdgpu_fence_driver_force_completion(ring);
599
600 if (!drm_dev_is_unplugged(adev_to_drm(adev)) &&
601 ring->fence_drv.irq_src &&
602 amdgpu_fence_need_ring_interrupt_restore(ring))
603 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
604 ring->fence_drv.irq_type);
605
606 del_timer_sync(&ring->fence_drv.fallback_timer);
607 }
608 }
609
610 /* Will either stop and flush handlers for amdgpu interrupt or reanble it */
amdgpu_fence_driver_isr_toggle(struct amdgpu_device * adev,bool stop)611 void amdgpu_fence_driver_isr_toggle(struct amdgpu_device *adev, bool stop)
612 {
613 int i;
614
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 || !ring->fence_drv.irq_src)
619 continue;
620
621 if (stop)
622 disable_irq(adev->irq.irq);
623 else
624 enable_irq(adev->irq.irq);
625 }
626 }
627
amdgpu_fence_driver_sw_fini(struct amdgpu_device * adev)628 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev)
629 {
630 unsigned int i, j;
631
632 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
633 struct amdgpu_ring *ring = adev->rings[i];
634
635 if (!ring || !ring->fence_drv.initialized)
636 continue;
637
638 /*
639 * Notice we check for sched.ops since there's some
640 * override on the meaning of sched.ready by amdgpu.
641 * The natural check would be sched.ready, which is
642 * set as drm_sched_init() finishes...
643 */
644 if (ring->sched.ops)
645 drm_sched_fini(&ring->sched);
646
647 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
648 dma_fence_put(ring->fence_drv.fences[j]);
649 kfree(ring->fence_drv.fences);
650 ring->fence_drv.fences = NULL;
651 ring->fence_drv.initialized = false;
652 }
653 }
654
655 /**
656 * amdgpu_fence_driver_hw_init - enable the fence driver
657 * for all possible rings.
658 *
659 * @adev: amdgpu device pointer
660 *
661 * Enable the fence driver for all possible rings (all asics).
662 * Not all asics have all rings, so each asic will only
663 * start the fence driver on the rings it has using
664 * amdgpu_fence_driver_start_ring().
665 * Returns 0 for success.
666 */
amdgpu_fence_driver_hw_init(struct amdgpu_device * adev)667 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev)
668 {
669 int i;
670
671 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
672 struct amdgpu_ring *ring = adev->rings[i];
673
674 if (!ring || !ring->fence_drv.initialized)
675 continue;
676
677 /* enable the interrupt */
678 if (ring->fence_drv.irq_src &&
679 amdgpu_fence_need_ring_interrupt_restore(ring))
680 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
681 ring->fence_drv.irq_type);
682 }
683 }
684
685 /**
686 * amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring
687 *
688 * @ring: fence of the ring to be cleared
689 *
690 */
amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring * ring)691 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring)
692 {
693 int i;
694 struct dma_fence *old, **ptr;
695
696 for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) {
697 ptr = &ring->fence_drv.fences[i];
698 old = rcu_dereference_protected(*ptr, 1);
699 if (old && old->ops == &amdgpu_job_fence_ops) {
700 struct amdgpu_job *job;
701
702 /* For non-scheduler bad job, i.e. failed ib test, we need to signal
703 * it right here or we won't be able to track them in fence_drv
704 * and they will remain unsignaled during sa_bo free.
705 */
706 job = container_of(old, struct amdgpu_job, hw_fence.base);
707 if (!job->base.s_fence && !dma_fence_is_signaled(old))
708 dma_fence_signal(old);
709 RCU_INIT_POINTER(*ptr, NULL);
710 dma_fence_put(old);
711 }
712 }
713 }
714
715 /**
716 * amdgpu_fence_driver_set_error - set error code on fences
717 * @ring: the ring which contains the fences
718 * @error: the error code to set
719 *
720 * Set an error code to all the fences pending on the ring.
721 */
amdgpu_fence_driver_set_error(struct amdgpu_ring * ring,int error)722 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error)
723 {
724 struct amdgpu_fence_driver *drv = &ring->fence_drv;
725 unsigned long flags;
726
727 spin_lock_irqsave(&drv->lock, flags);
728 for (unsigned int i = 0; i <= drv->num_fences_mask; ++i) {
729 struct dma_fence *fence;
730
731 fence = rcu_dereference_protected(drv->fences[i],
732 lockdep_is_held(&drv->lock));
733 if (fence && !dma_fence_is_signaled_locked(fence))
734 dma_fence_set_error(fence, error);
735 }
736 spin_unlock_irqrestore(&drv->lock, flags);
737 }
738
739 /**
740 * amdgpu_fence_driver_force_completion - force signal latest fence of ring
741 *
742 * @ring: fence of the ring to signal
743 *
744 */
amdgpu_fence_driver_force_completion(struct amdgpu_ring * ring)745 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
746 {
747 amdgpu_fence_driver_set_error(ring, -ECANCELED);
748 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
749 amdgpu_fence_process(ring);
750 }
751
752 /*
753 * Common fence implementation
754 */
755
amdgpu_fence_get_driver_name(struct dma_fence * fence)756 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
757 {
758 return "amdgpu";
759 }
760
amdgpu_fence_get_timeline_name(struct dma_fence * f)761 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
762 {
763 return (const char *)to_amdgpu_fence(f)->ring->name;
764 }
765
amdgpu_job_fence_get_timeline_name(struct dma_fence * f)766 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
767 {
768 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence.base);
769
770 return (const char *)to_amdgpu_ring(job->base.sched)->name;
771 }
772
773 /**
774 * amdgpu_fence_enable_signaling - enable signalling on fence
775 * @f: fence
776 *
777 * This function is called with fence_queue lock held, and adds a callback
778 * to fence_queue that checks if this fence is signaled, and if so it
779 * signals the fence and removes itself.
780 */
amdgpu_fence_enable_signaling(struct dma_fence * f)781 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
782 {
783 if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
784 amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
785
786 return true;
787 }
788
789 /**
790 * amdgpu_job_fence_enable_signaling - enable signalling on job fence
791 * @f: fence
792 *
793 * This is the simliar function with amdgpu_fence_enable_signaling above, it
794 * only handles the job embedded fence.
795 */
amdgpu_job_fence_enable_signaling(struct dma_fence * f)796 static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f)
797 {
798 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence.base);
799
800 if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer))
801 amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched));
802
803 return true;
804 }
805
806 /**
807 * amdgpu_fence_free - free up the fence memory
808 *
809 * @rcu: RCU callback head
810 *
811 * Free up the fence memory after the RCU grace period.
812 */
amdgpu_fence_free(struct rcu_head * rcu)813 static void amdgpu_fence_free(struct rcu_head *rcu)
814 {
815 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
816
817 /* free fence_slab if it's separated fence*/
818 kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f));
819 }
820
821 /**
822 * amdgpu_job_fence_free - free up the job with embedded fence
823 *
824 * @rcu: RCU callback head
825 *
826 * Free up the job with embedded fence after the RCU grace period.
827 */
amdgpu_job_fence_free(struct rcu_head * rcu)828 static void amdgpu_job_fence_free(struct rcu_head *rcu)
829 {
830 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
831
832 /* free job if fence has a parent job */
833 kfree(container_of(f, struct amdgpu_job, hw_fence.base));
834 }
835
836 /**
837 * amdgpu_fence_release - callback that fence can be freed
838 *
839 * @f: fence
840 *
841 * This function is called when the reference count becomes zero.
842 * It just RCU schedules freeing up the fence.
843 */
amdgpu_fence_release(struct dma_fence * f)844 static void amdgpu_fence_release(struct dma_fence *f)
845 {
846 call_rcu(&f->rcu, amdgpu_fence_free);
847 }
848
849 /**
850 * amdgpu_job_fence_release - callback that job embedded fence can be freed
851 *
852 * @f: fence
853 *
854 * This is the simliar function with amdgpu_fence_release above, it
855 * only handles the job embedded fence.
856 */
amdgpu_job_fence_release(struct dma_fence * f)857 static void amdgpu_job_fence_release(struct dma_fence *f)
858 {
859 call_rcu(&f->rcu, amdgpu_job_fence_free);
860 }
861
862 static const struct dma_fence_ops amdgpu_fence_ops = {
863 .get_driver_name = amdgpu_fence_get_driver_name,
864 .get_timeline_name = amdgpu_fence_get_timeline_name,
865 .enable_signaling = amdgpu_fence_enable_signaling,
866 .release = amdgpu_fence_release,
867 };
868
869 static const struct dma_fence_ops amdgpu_job_fence_ops = {
870 .get_driver_name = amdgpu_fence_get_driver_name,
871 .get_timeline_name = amdgpu_job_fence_get_timeline_name,
872 .enable_signaling = amdgpu_job_fence_enable_signaling,
873 .release = amdgpu_job_fence_release,
874 };
875
876 /*
877 * Fence debugfs
878 */
879 #if defined(CONFIG_DEBUG_FS)
amdgpu_debugfs_fence_info_show(struct seq_file * m,void * unused)880 static int amdgpu_debugfs_fence_info_show(struct seq_file *m, void *unused)
881 {
882 struct amdgpu_device *adev = m->private;
883 int i;
884
885 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
886 struct amdgpu_ring *ring = adev->rings[i];
887
888 if (!ring || !ring->fence_drv.initialized)
889 continue;
890
891 amdgpu_fence_process(ring);
892
893 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
894 seq_printf(m, "Last signaled fence 0x%08x\n",
895 atomic_read(&ring->fence_drv.last_seq));
896 seq_printf(m, "Last emitted 0x%08x\n",
897 ring->fence_drv.sync_seq);
898
899 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
900 ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
901 seq_printf(m, "Last signaled trailing fence 0x%08x\n",
902 le32_to_cpu(*ring->trail_fence_cpu_addr));
903 seq_printf(m, "Last emitted 0x%08x\n",
904 ring->trail_seq);
905 }
906
907 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
908 continue;
909
910 /* set in CP_VMID_PREEMPT and preemption occurred */
911 seq_printf(m, "Last preempted 0x%08x\n",
912 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
913 /* set in CP_VMID_RESET and reset occurred */
914 seq_printf(m, "Last reset 0x%08x\n",
915 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
916 /* Both preemption and reset occurred */
917 seq_printf(m, "Last both 0x%08x\n",
918 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
919 }
920 return 0;
921 }
922
923 /*
924 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
925 *
926 * Manually trigger a gpu reset at the next fence wait.
927 */
gpu_recover_get(void * data,u64 * val)928 static int gpu_recover_get(void *data, u64 *val)
929 {
930 struct amdgpu_device *adev = (struct amdgpu_device *)data;
931 struct drm_device *dev = adev_to_drm(adev);
932 int r;
933
934 r = pm_runtime_get_sync(dev->dev);
935 if (r < 0) {
936 pm_runtime_put_autosuspend(dev->dev);
937 return 0;
938 }
939
940 if (amdgpu_reset_domain_schedule(adev->reset_domain, &adev->reset_work))
941 flush_work(&adev->reset_work);
942
943 *val = atomic_read(&adev->reset_domain->reset_res);
944
945 pm_runtime_mark_last_busy(dev->dev);
946 pm_runtime_put_autosuspend(dev->dev);
947
948 return 0;
949 }
950
951 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_fence_info);
952 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gpu_recover_fops, gpu_recover_get, NULL,
953 "%lld\n");
954
amdgpu_debugfs_reset_work(struct work_struct * work)955 static void amdgpu_debugfs_reset_work(struct work_struct *work)
956 {
957 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
958 reset_work);
959
960 struct amdgpu_reset_context reset_context;
961
962 memset(&reset_context, 0, sizeof(reset_context));
963
964 reset_context.method = AMD_RESET_METHOD_NONE;
965 reset_context.reset_req_dev = adev;
966 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
967
968 amdgpu_device_gpu_recover(adev, NULL, &reset_context);
969 }
970
971 #endif
972
amdgpu_debugfs_fence_init(struct amdgpu_device * adev)973 void amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
974 {
975 #if defined(CONFIG_DEBUG_FS)
976 struct drm_minor *minor = adev_to_drm(adev)->primary;
977 struct dentry *root = minor->debugfs_root;
978
979 debugfs_create_file("amdgpu_fence_info", 0444, root, adev,
980 &amdgpu_debugfs_fence_info_fops);
981
982 if (!amdgpu_sriov_vf(adev)) {
983
984 INIT_WORK(&adev->reset_work, amdgpu_debugfs_reset_work);
985 debugfs_create_file("amdgpu_gpu_recover", 0444, root, adev,
986 &amdgpu_debugfs_gpu_recover_fops);
987 }
988 #endif
989 }
990
991