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