Linux 4.19.133
[linux/fpc-iii.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_fence.c
blob869ff624b108c5c6241470427bd5ff5bb19d6ca1
1 /*
2 * Copyright 2009 Jerome Glisse.
3 * All Rights Reserved.
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:
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.
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.
27 * Authors:
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Dave Airlie
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 <drm/drmP.h>
38 #include "amdgpu.h"
39 #include "amdgpu_trace.h"
42 * Fences
43 * Fences mark an event in the GPUs pipeline and are used
44 * for GPU/CPU synchronization. When the fence is written,
45 * it is expected that all buffers associated with that fence
46 * are no longer in use by the associated ring on the GPU and
47 * that the the relevant GPU caches have been flushed.
50 struct amdgpu_fence {
51 struct dma_fence base;
53 /* RB, DMA, etc. */
54 struct amdgpu_ring *ring;
57 static struct kmem_cache *amdgpu_fence_slab;
59 int amdgpu_fence_slab_init(void)
61 amdgpu_fence_slab = kmem_cache_create(
62 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
63 SLAB_HWCACHE_ALIGN, NULL);
64 if (!amdgpu_fence_slab)
65 return -ENOMEM;
66 return 0;
69 void amdgpu_fence_slab_fini(void)
71 rcu_barrier();
72 kmem_cache_destroy(amdgpu_fence_slab);
75 * Cast helper
77 static const struct dma_fence_ops amdgpu_fence_ops;
78 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
80 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
82 if (__f->base.ops == &amdgpu_fence_ops)
83 return __f;
85 return NULL;
88 /**
89 * amdgpu_fence_write - write a fence value
91 * @ring: ring the fence is associated with
92 * @seq: sequence number to write
94 * Writes a fence value to memory (all asics).
96 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
98 struct amdgpu_fence_driver *drv = &ring->fence_drv;
100 if (drv->cpu_addr)
101 *drv->cpu_addr = cpu_to_le32(seq);
105 * amdgpu_fence_read - read a fence value
107 * @ring: ring the fence is associated with
109 * Reads a fence value from memory (all asics).
110 * Returns the value of the fence read from memory.
112 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
114 struct amdgpu_fence_driver *drv = &ring->fence_drv;
115 u32 seq = 0;
117 if (drv->cpu_addr)
118 seq = le32_to_cpu(*drv->cpu_addr);
119 else
120 seq = atomic_read(&drv->last_seq);
122 return seq;
126 * amdgpu_fence_emit - emit a fence on the requested ring
128 * @ring: ring the fence is associated with
129 * @f: resulting fence object
131 * Emits a fence command on the requested ring (all asics).
132 * Returns 0 on success, -ENOMEM on failure.
134 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
135 unsigned flags)
137 struct amdgpu_device *adev = ring->adev;
138 struct amdgpu_fence *fence;
139 struct dma_fence __rcu **ptr;
140 uint32_t seq;
141 int r;
143 fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
144 if (fence == NULL)
145 return -ENOMEM;
147 seq = ++ring->fence_drv.sync_seq;
148 fence->ring = ring;
149 dma_fence_init(&fence->base, &amdgpu_fence_ops,
150 &ring->fence_drv.lock,
151 adev->fence_context + ring->idx,
152 seq);
153 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
154 seq, flags | AMDGPU_FENCE_FLAG_INT);
156 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
157 if (unlikely(rcu_dereference_protected(*ptr, 1))) {
158 struct dma_fence *old;
160 rcu_read_lock();
161 old = dma_fence_get_rcu_safe(ptr);
162 rcu_read_unlock();
164 if (old) {
165 r = dma_fence_wait(old, false);
166 dma_fence_put(old);
167 if (r)
168 return r;
172 /* This function can't be called concurrently anyway, otherwise
173 * emitting the fence would mess up the hardware ring buffer.
175 rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
177 *f = &fence->base;
179 return 0;
183 * amdgpu_fence_emit_polling - emit a fence on the requeste ring
185 * @ring: ring the fence is associated with
186 * @s: resulting sequence number
188 * Emits a fence command on the requested ring (all asics).
189 * Used For polling fence.
190 * Returns 0 on success, -ENOMEM on failure.
192 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s)
194 uint32_t seq;
196 if (!s)
197 return -EINVAL;
199 seq = ++ring->fence_drv.sync_seq;
200 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
201 seq, 0);
203 *s = seq;
205 return 0;
209 * amdgpu_fence_schedule_fallback - schedule fallback check
211 * @ring: pointer to struct amdgpu_ring
213 * Start a timer as fallback to our interrupts.
215 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
217 mod_timer(&ring->fence_drv.fallback_timer,
218 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
222 * amdgpu_fence_process - check for fence activity
224 * @ring: pointer to struct amdgpu_ring
226 * Checks the current fence value and calculates the last
227 * signalled fence value. Wakes the fence queue if the
228 * sequence number has increased.
230 void amdgpu_fence_process(struct amdgpu_ring *ring)
232 struct amdgpu_fence_driver *drv = &ring->fence_drv;
233 uint32_t seq, last_seq;
234 int r;
236 do {
237 last_seq = atomic_read(&ring->fence_drv.last_seq);
238 seq = amdgpu_fence_read(ring);
240 } while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
242 if (seq != ring->fence_drv.sync_seq)
243 amdgpu_fence_schedule_fallback(ring);
245 if (unlikely(seq == last_seq))
246 return;
248 last_seq &= drv->num_fences_mask;
249 seq &= drv->num_fences_mask;
251 do {
252 struct dma_fence *fence, **ptr;
254 ++last_seq;
255 last_seq &= drv->num_fences_mask;
256 ptr = &drv->fences[last_seq];
258 /* There is always exactly one thread signaling this fence slot */
259 fence = rcu_dereference_protected(*ptr, 1);
260 RCU_INIT_POINTER(*ptr, NULL);
262 if (!fence)
263 continue;
265 r = dma_fence_signal(fence);
266 if (!r)
267 DMA_FENCE_TRACE(fence, "signaled from irq context\n");
268 else
269 BUG();
271 dma_fence_put(fence);
272 } while (last_seq != seq);
276 * amdgpu_fence_fallback - fallback for hardware interrupts
278 * @work: delayed work item
280 * Checks for fence activity.
282 static void amdgpu_fence_fallback(struct timer_list *t)
284 struct amdgpu_ring *ring = from_timer(ring, t,
285 fence_drv.fallback_timer);
287 amdgpu_fence_process(ring);
291 * amdgpu_fence_wait_empty - wait for all fences to signal
293 * @adev: amdgpu device pointer
294 * @ring: ring index the fence is associated with
296 * Wait for all fences on the requested ring to signal (all asics).
297 * Returns 0 if the fences have passed, error for all other cases.
299 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
301 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
302 struct dma_fence *fence, **ptr;
303 int r;
305 if (!seq)
306 return 0;
308 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
309 rcu_read_lock();
310 fence = rcu_dereference(*ptr);
311 if (!fence || !dma_fence_get_rcu(fence)) {
312 rcu_read_unlock();
313 return 0;
315 rcu_read_unlock();
317 r = dma_fence_wait(fence, false);
318 dma_fence_put(fence);
319 return r;
323 * amdgpu_fence_wait_polling - busy wait for givn sequence number
325 * @ring: ring index the fence is associated with
326 * @wait_seq: sequence number to wait
327 * @timeout: the timeout for waiting in usecs
329 * Wait for all fences on the requested ring to signal (all asics).
330 * Returns left time if no timeout, 0 or minus if timeout.
332 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
333 uint32_t wait_seq,
334 signed long timeout)
336 uint32_t seq;
338 do {
339 seq = amdgpu_fence_read(ring);
340 udelay(5);
341 timeout -= 5;
342 } while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);
344 return timeout > 0 ? timeout : 0;
347 * amdgpu_fence_count_emitted - get the count of emitted fences
349 * @ring: ring the fence is associated with
351 * Get the number of fences emitted on the requested ring (all asics).
352 * Returns the number of emitted fences on the ring. Used by the
353 * dynpm code to ring track activity.
355 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
357 uint64_t emitted;
359 /* We are not protected by ring lock when reading the last sequence
360 * but it's ok to report slightly wrong fence count here.
362 amdgpu_fence_process(ring);
363 emitted = 0x100000000ull;
364 emitted -= atomic_read(&ring->fence_drv.last_seq);
365 emitted += READ_ONCE(ring->fence_drv.sync_seq);
366 return lower_32_bits(emitted);
370 * amdgpu_fence_driver_start_ring - make the fence driver
371 * ready for use on the requested ring.
373 * @ring: ring to start the fence driver on
374 * @irq_src: interrupt source to use for this ring
375 * @irq_type: interrupt type to use for this ring
377 * Make the fence driver ready for processing (all asics).
378 * Not all asics have all rings, so each asic will only
379 * start the fence driver on the rings it has.
380 * Returns 0 for success, errors for failure.
382 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
383 struct amdgpu_irq_src *irq_src,
384 unsigned irq_type)
386 struct amdgpu_device *adev = ring->adev;
387 uint64_t index;
389 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
390 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
391 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
392 } else {
393 /* put fence directly behind firmware */
394 index = ALIGN(adev->uvd.fw->size, 8);
395 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
396 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
398 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
399 amdgpu_irq_get(adev, irq_src, irq_type);
401 ring->fence_drv.irq_src = irq_src;
402 ring->fence_drv.irq_type = irq_type;
403 ring->fence_drv.initialized = true;
405 dev_dbg(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
406 "cpu addr 0x%p\n", ring->idx,
407 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
408 return 0;
412 * amdgpu_fence_driver_init_ring - init the fence driver
413 * for the requested ring.
415 * @ring: ring to init the fence driver on
416 * @num_hw_submission: number of entries on the hardware queue
418 * Init the fence driver for the requested ring (all asics).
419 * Helper function for amdgpu_fence_driver_init().
421 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
422 unsigned num_hw_submission)
424 long timeout;
425 int r;
427 /* Check that num_hw_submission is a power of two */
428 if ((num_hw_submission & (num_hw_submission - 1)) != 0)
429 return -EINVAL;
431 ring->fence_drv.cpu_addr = NULL;
432 ring->fence_drv.gpu_addr = 0;
433 ring->fence_drv.sync_seq = 0;
434 atomic_set(&ring->fence_drv.last_seq, 0);
435 ring->fence_drv.initialized = false;
437 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
439 ring->fence_drv.num_fences_mask = num_hw_submission * 2 - 1;
440 spin_lock_init(&ring->fence_drv.lock);
441 ring->fence_drv.fences = kcalloc(num_hw_submission * 2, sizeof(void *),
442 GFP_KERNEL);
443 if (!ring->fence_drv.fences)
444 return -ENOMEM;
446 /* No need to setup the GPU scheduler for KIQ ring */
447 if (ring->funcs->type != AMDGPU_RING_TYPE_KIQ) {
448 /* for non-sriov case, no timeout enforce on compute ring */
449 if ((ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
450 && !amdgpu_sriov_vf(ring->adev))
451 timeout = MAX_SCHEDULE_TIMEOUT;
452 else
453 timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
455 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
456 num_hw_submission, amdgpu_job_hang_limit,
457 timeout, ring->name);
458 if (r) {
459 DRM_ERROR("Failed to create scheduler on ring %s.\n",
460 ring->name);
461 return r;
465 return 0;
469 * amdgpu_fence_driver_init - init the fence driver
470 * for all possible rings.
472 * @adev: amdgpu device pointer
474 * Init the fence driver for all possible rings (all asics).
475 * Not all asics have all rings, so each asic will only
476 * start the fence driver on the rings it has using
477 * amdgpu_fence_driver_start_ring().
478 * Returns 0 for success.
480 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
482 if (amdgpu_debugfs_fence_init(adev))
483 dev_err(adev->dev, "fence debugfs file creation failed\n");
485 return 0;
489 * amdgpu_fence_driver_fini - tear down the fence driver
490 * for all possible rings.
492 * @adev: amdgpu device pointer
494 * Tear down the fence driver for all possible rings (all asics).
496 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
498 unsigned i, j;
499 int r;
501 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
502 struct amdgpu_ring *ring = adev->rings[i];
504 if (!ring || !ring->fence_drv.initialized)
505 continue;
506 r = amdgpu_fence_wait_empty(ring);
507 if (r) {
508 /* no need to trigger GPU reset as we are unloading */
509 amdgpu_fence_driver_force_completion(ring);
511 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
512 ring->fence_drv.irq_type);
513 drm_sched_fini(&ring->sched);
514 del_timer_sync(&ring->fence_drv.fallback_timer);
515 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
516 dma_fence_put(ring->fence_drv.fences[j]);
517 kfree(ring->fence_drv.fences);
518 ring->fence_drv.fences = NULL;
519 ring->fence_drv.initialized = false;
524 * amdgpu_fence_driver_suspend - suspend the fence driver
525 * for all possible rings.
527 * @adev: amdgpu device pointer
529 * Suspend the fence driver for all possible rings (all asics).
531 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
533 int i, r;
535 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
536 struct amdgpu_ring *ring = adev->rings[i];
537 if (!ring || !ring->fence_drv.initialized)
538 continue;
540 /* wait for gpu to finish processing current batch */
541 r = amdgpu_fence_wait_empty(ring);
542 if (r) {
543 /* delay GPU reset to resume */
544 amdgpu_fence_driver_force_completion(ring);
547 /* disable the interrupt */
548 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
549 ring->fence_drv.irq_type);
554 * amdgpu_fence_driver_resume - resume the fence driver
555 * for all possible rings.
557 * @adev: amdgpu device pointer
559 * Resume the fence driver for all possible rings (all asics).
560 * Not all asics have all rings, so each asic will only
561 * start the fence driver on the rings it has using
562 * amdgpu_fence_driver_start_ring().
563 * Returns 0 for success.
565 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
567 int i;
569 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
570 struct amdgpu_ring *ring = adev->rings[i];
571 if (!ring || !ring->fence_drv.initialized)
572 continue;
574 /* enable the interrupt */
575 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
576 ring->fence_drv.irq_type);
581 * amdgpu_fence_driver_force_completion - force signal latest fence of ring
583 * @ring: fence of the ring to signal
586 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
588 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
589 amdgpu_fence_process(ring);
593 * Common fence implementation
596 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
598 return "amdgpu";
601 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
603 struct amdgpu_fence *fence = to_amdgpu_fence(f);
604 return (const char *)fence->ring->name;
608 * amdgpu_fence_enable_signaling - enable signalling on fence
609 * @fence: fence
611 * This function is called with fence_queue lock held, and adds a callback
612 * to fence_queue that checks if this fence is signaled, and if so it
613 * signals the fence and removes itself.
615 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
617 struct amdgpu_fence *fence = to_amdgpu_fence(f);
618 struct amdgpu_ring *ring = fence->ring;
620 if (!timer_pending(&ring->fence_drv.fallback_timer))
621 amdgpu_fence_schedule_fallback(ring);
623 DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
625 return true;
629 * amdgpu_fence_free - free up the fence memory
631 * @rcu: RCU callback head
633 * Free up the fence memory after the RCU grace period.
635 static void amdgpu_fence_free(struct rcu_head *rcu)
637 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
638 struct amdgpu_fence *fence = to_amdgpu_fence(f);
639 kmem_cache_free(amdgpu_fence_slab, fence);
643 * amdgpu_fence_release - callback that fence can be freed
645 * @fence: fence
647 * This function is called when the reference count becomes zero.
648 * It just RCU schedules freeing up the fence.
650 static void amdgpu_fence_release(struct dma_fence *f)
652 call_rcu(&f->rcu, amdgpu_fence_free);
655 static const struct dma_fence_ops amdgpu_fence_ops = {
656 .get_driver_name = amdgpu_fence_get_driver_name,
657 .get_timeline_name = amdgpu_fence_get_timeline_name,
658 .enable_signaling = amdgpu_fence_enable_signaling,
659 .release = amdgpu_fence_release,
663 * Fence debugfs
665 #if defined(CONFIG_DEBUG_FS)
666 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
668 struct drm_info_node *node = (struct drm_info_node *)m->private;
669 struct drm_device *dev = node->minor->dev;
670 struct amdgpu_device *adev = dev->dev_private;
671 int i;
673 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
674 struct amdgpu_ring *ring = adev->rings[i];
675 if (!ring || !ring->fence_drv.initialized)
676 continue;
678 amdgpu_fence_process(ring);
680 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
681 seq_printf(m, "Last signaled fence 0x%08x\n",
682 atomic_read(&ring->fence_drv.last_seq));
683 seq_printf(m, "Last emitted 0x%08x\n",
684 ring->fence_drv.sync_seq);
686 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
687 continue;
689 /* set in CP_VMID_PREEMPT and preemption occurred */
690 seq_printf(m, "Last preempted 0x%08x\n",
691 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
692 /* set in CP_VMID_RESET and reset occurred */
693 seq_printf(m, "Last reset 0x%08x\n",
694 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
695 /* Both preemption and reset occurred */
696 seq_printf(m, "Last both 0x%08x\n",
697 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
699 return 0;
703 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
705 * Manually trigger a gpu reset at the next fence wait.
707 static int amdgpu_debugfs_gpu_recover(struct seq_file *m, void *data)
709 struct drm_info_node *node = (struct drm_info_node *) m->private;
710 struct drm_device *dev = node->minor->dev;
711 struct amdgpu_device *adev = dev->dev_private;
713 seq_printf(m, "gpu recover\n");
714 amdgpu_device_gpu_recover(adev, NULL, true);
716 return 0;
719 static const struct drm_info_list amdgpu_debugfs_fence_list[] = {
720 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
721 {"amdgpu_gpu_recover", &amdgpu_debugfs_gpu_recover, 0, NULL}
724 static const struct drm_info_list amdgpu_debugfs_fence_list_sriov[] = {
725 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
727 #endif
729 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
731 #if defined(CONFIG_DEBUG_FS)
732 if (amdgpu_sriov_vf(adev))
733 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list_sriov, 1);
734 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
735 #else
736 return 0;
737 #endif