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