18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2009 Jerome Glisse.
38c2ecf20Sopenharmony_ci * All Rights Reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
68c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the
78c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
88c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
98c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
108c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
118c2ecf20Sopenharmony_ci * the following conditions:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
148c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
158c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
168c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
178c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
188c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
198c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
228c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
238c2ecf20Sopenharmony_ci * of the Software.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * Authors:
288c2ecf20Sopenharmony_ci *    Jerome Glisse <glisse@freedesktop.org>
298c2ecf20Sopenharmony_ci *    Dave Airlie
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include <linux/atomic.h>
338c2ecf20Sopenharmony_ci#include <linux/firmware.h>
348c2ecf20Sopenharmony_ci#include <linux/kref.h>
358c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
368c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
378c2ecf20Sopenharmony_ci#include <linux/slab.h>
388c2ecf20Sopenharmony_ci#include <linux/wait.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <drm/drm_debugfs.h>
418c2ecf20Sopenharmony_ci#include <drm/drm_device.h>
428c2ecf20Sopenharmony_ci#include <drm/drm_file.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "radeon.h"
458c2ecf20Sopenharmony_ci#include "radeon_reg.h"
468c2ecf20Sopenharmony_ci#include "radeon_trace.h"
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * Fences
508c2ecf20Sopenharmony_ci * Fences mark an event in the GPUs pipeline and are used
518c2ecf20Sopenharmony_ci * for GPU/CPU synchronization.  When the fence is written,
528c2ecf20Sopenharmony_ci * it is expected that all buffers associated with that fence
538c2ecf20Sopenharmony_ci * are no longer in use by the associated ring on the GPU and
548c2ecf20Sopenharmony_ci * that the the relevant GPU caches have been flushed.  Whether
558c2ecf20Sopenharmony_ci * we use a scratch register or memory location depends on the asic
568c2ecf20Sopenharmony_ci * and whether writeback is enabled.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci * radeon_fence_write - write a fence value
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
638c2ecf20Sopenharmony_ci * @seq: sequence number to write
648c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * Writes a fence value to memory or a scratch register (all asics).
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistatic void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
718c2ecf20Sopenharmony_ci	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
728c2ecf20Sopenharmony_ci		if (drv->cpu_addr) {
738c2ecf20Sopenharmony_ci			*drv->cpu_addr = cpu_to_le32(seq);
748c2ecf20Sopenharmony_ci		}
758c2ecf20Sopenharmony_ci	} else {
768c2ecf20Sopenharmony_ci		WREG32(drv->scratch_reg, seq);
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/**
818c2ecf20Sopenharmony_ci * radeon_fence_read - read a fence value
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
848c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * Reads a fence value from memory or a scratch register (all asics).
878c2ecf20Sopenharmony_ci * Returns the value of the fence read from memory or register.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_cistatic u32 radeon_fence_read(struct radeon_device *rdev, int ring)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
928c2ecf20Sopenharmony_ci	u32 seq = 0;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
958c2ecf20Sopenharmony_ci		if (drv->cpu_addr) {
968c2ecf20Sopenharmony_ci			seq = le32_to_cpu(*drv->cpu_addr);
978c2ecf20Sopenharmony_ci		} else {
988c2ecf20Sopenharmony_ci			seq = lower_32_bits(atomic64_read(&drv->last_seq));
998c2ecf20Sopenharmony_ci		}
1008c2ecf20Sopenharmony_ci	} else {
1018c2ecf20Sopenharmony_ci		seq = RREG32(drv->scratch_reg);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	return seq;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/**
1078c2ecf20Sopenharmony_ci * radeon_fence_schedule_check - schedule lockup check
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1108c2ecf20Sopenharmony_ci * @ring: ring index we should work with
1118c2ecf20Sopenharmony_ci *
1128c2ecf20Sopenharmony_ci * Queues a delayed work item to check for lockups.
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_cistatic void radeon_fence_schedule_check(struct radeon_device *rdev, int ring)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	/*
1178c2ecf20Sopenharmony_ci	 * Do not reset the timer here with mod_delayed_work,
1188c2ecf20Sopenharmony_ci	 * this can livelock in an interaction with TTM delayed destroy.
1198c2ecf20Sopenharmony_ci	 */
1208c2ecf20Sopenharmony_ci	queue_delayed_work(system_power_efficient_wq,
1218c2ecf20Sopenharmony_ci			   &rdev->fence_drv[ring].lockup_work,
1228c2ecf20Sopenharmony_ci			   RADEON_FENCE_JIFFIES_TIMEOUT);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/**
1268c2ecf20Sopenharmony_ci * radeon_fence_emit - emit a fence on the requested ring
1278c2ecf20Sopenharmony_ci *
1288c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1298c2ecf20Sopenharmony_ci * @fence: radeon fence object
1308c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
1318c2ecf20Sopenharmony_ci *
1328c2ecf20Sopenharmony_ci * Emits a fence command on the requested ring (all asics).
1338c2ecf20Sopenharmony_ci * Returns 0 on success, -ENOMEM on failure.
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_ciint radeon_fence_emit(struct radeon_device *rdev,
1368c2ecf20Sopenharmony_ci		      struct radeon_fence **fence,
1378c2ecf20Sopenharmony_ci		      int ring)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	u64 seq;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* we are protected by the ring emission mutex */
1428c2ecf20Sopenharmony_ci	*fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
1438c2ecf20Sopenharmony_ci	if ((*fence) == NULL) {
1448c2ecf20Sopenharmony_ci		return -ENOMEM;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci	(*fence)->rdev = rdev;
1478c2ecf20Sopenharmony_ci	(*fence)->seq = seq = ++rdev->fence_drv[ring].sync_seq[ring];
1488c2ecf20Sopenharmony_ci	(*fence)->ring = ring;
1498c2ecf20Sopenharmony_ci	(*fence)->is_vm_update = false;
1508c2ecf20Sopenharmony_ci	dma_fence_init(&(*fence)->base, &radeon_fence_ops,
1518c2ecf20Sopenharmony_ci		       &rdev->fence_queue.lock,
1528c2ecf20Sopenharmony_ci		       rdev->fence_context + ring,
1538c2ecf20Sopenharmony_ci		       seq);
1548c2ecf20Sopenharmony_ci	radeon_fence_ring_emit(rdev, ring, *fence);
1558c2ecf20Sopenharmony_ci	trace_radeon_fence_emit(rdev->ddev, ring, (*fence)->seq);
1568c2ecf20Sopenharmony_ci	radeon_fence_schedule_check(rdev, ring);
1578c2ecf20Sopenharmony_ci	return 0;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/**
1618c2ecf20Sopenharmony_ci * radeon_fence_check_signaled - callback from fence_queue
1628c2ecf20Sopenharmony_ci *
1638c2ecf20Sopenharmony_ci * this function is called with fence_queue lock held, which is also used
1648c2ecf20Sopenharmony_ci * for the fence locking itself, so unlocked variants are used for
1658c2ecf20Sopenharmony_ci * fence_signal, and remove_wait_queue.
1668c2ecf20Sopenharmony_ci */
1678c2ecf20Sopenharmony_cistatic int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode, int flags, void *key)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct radeon_fence *fence;
1708c2ecf20Sopenharmony_ci	u64 seq;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	fence = container_of(wait, struct radeon_fence, fence_wake);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/*
1758c2ecf20Sopenharmony_ci	 * We cannot use radeon_fence_process here because we're already
1768c2ecf20Sopenharmony_ci	 * in the waitqueue, in a call from wake_up_all.
1778c2ecf20Sopenharmony_ci	 */
1788c2ecf20Sopenharmony_ci	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
1798c2ecf20Sopenharmony_ci	if (seq >= fence->seq) {
1808c2ecf20Sopenharmony_ci		int ret = dma_fence_signal_locked(&fence->base);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci		if (!ret)
1838c2ecf20Sopenharmony_ci			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
1848c2ecf20Sopenharmony_ci		else
1858c2ecf20Sopenharmony_ci			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
1888c2ecf20Sopenharmony_ci		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
1898c2ecf20Sopenharmony_ci		dma_fence_put(&fence->base);
1908c2ecf20Sopenharmony_ci	} else
1918c2ecf20Sopenharmony_ci		DMA_FENCE_TRACE(&fence->base, "pending\n");
1928c2ecf20Sopenharmony_ci	return 0;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/**
1968c2ecf20Sopenharmony_ci * radeon_fence_activity - check for fence activity
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1998c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
2008c2ecf20Sopenharmony_ci *
2018c2ecf20Sopenharmony_ci * Checks the current fence value and calculates the last
2028c2ecf20Sopenharmony_ci * signalled fence value. Returns true if activity occured
2038c2ecf20Sopenharmony_ci * on the ring, and the fence_queue should be waken up.
2048c2ecf20Sopenharmony_ci */
2058c2ecf20Sopenharmony_cistatic bool radeon_fence_activity(struct radeon_device *rdev, int ring)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	uint64_t seq, last_seq, last_emitted;
2088c2ecf20Sopenharmony_ci	unsigned count_loop = 0;
2098c2ecf20Sopenharmony_ci	bool wake = false;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/* Note there is a scenario here for an infinite loop but it's
2128c2ecf20Sopenharmony_ci	 * very unlikely to happen. For it to happen, the current polling
2138c2ecf20Sopenharmony_ci	 * process need to be interrupted by another process and another
2148c2ecf20Sopenharmony_ci	 * process needs to update the last_seq btw the atomic read and
2158c2ecf20Sopenharmony_ci	 * xchg of the current process.
2168c2ecf20Sopenharmony_ci	 *
2178c2ecf20Sopenharmony_ci	 * More over for this to go in infinite loop there need to be
2188c2ecf20Sopenharmony_ci	 * continuously new fence signaled ie radeon_fence_read needs
2198c2ecf20Sopenharmony_ci	 * to return a different value each time for both the currently
2208c2ecf20Sopenharmony_ci	 * polling process and the other process that xchg the last_seq
2218c2ecf20Sopenharmony_ci	 * btw atomic read and xchg of the current process. And the
2228c2ecf20Sopenharmony_ci	 * value the other process set as last seq must be higher than
2238c2ecf20Sopenharmony_ci	 * the seq value we just read. Which means that current process
2248c2ecf20Sopenharmony_ci	 * need to be interrupted after radeon_fence_read and before
2258c2ecf20Sopenharmony_ci	 * atomic xchg.
2268c2ecf20Sopenharmony_ci	 *
2278c2ecf20Sopenharmony_ci	 * To be even more safe we count the number of time we loop and
2288c2ecf20Sopenharmony_ci	 * we bail after 10 loop just accepting the fact that we might
2298c2ecf20Sopenharmony_ci	 * have temporarly set the last_seq not to the true real last
2308c2ecf20Sopenharmony_ci	 * seq but to an older one.
2318c2ecf20Sopenharmony_ci	 */
2328c2ecf20Sopenharmony_ci	last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
2338c2ecf20Sopenharmony_ci	do {
2348c2ecf20Sopenharmony_ci		last_emitted = rdev->fence_drv[ring].sync_seq[ring];
2358c2ecf20Sopenharmony_ci		seq = radeon_fence_read(rdev, ring);
2368c2ecf20Sopenharmony_ci		seq |= last_seq & 0xffffffff00000000LL;
2378c2ecf20Sopenharmony_ci		if (seq < last_seq) {
2388c2ecf20Sopenharmony_ci			seq &= 0xffffffff;
2398c2ecf20Sopenharmony_ci			seq |= last_emitted & 0xffffffff00000000LL;
2408c2ecf20Sopenharmony_ci		}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci		if (seq <= last_seq || seq > last_emitted) {
2438c2ecf20Sopenharmony_ci			break;
2448c2ecf20Sopenharmony_ci		}
2458c2ecf20Sopenharmony_ci		/* If we loop over we don't want to return without
2468c2ecf20Sopenharmony_ci		 * checking if a fence is signaled as it means that the
2478c2ecf20Sopenharmony_ci		 * seq we just read is different from the previous on.
2488c2ecf20Sopenharmony_ci		 */
2498c2ecf20Sopenharmony_ci		wake = true;
2508c2ecf20Sopenharmony_ci		last_seq = seq;
2518c2ecf20Sopenharmony_ci		if ((count_loop++) > 10) {
2528c2ecf20Sopenharmony_ci			/* We looped over too many time leave with the
2538c2ecf20Sopenharmony_ci			 * fact that we might have set an older fence
2548c2ecf20Sopenharmony_ci			 * seq then the current real last seq as signaled
2558c2ecf20Sopenharmony_ci			 * by the hw.
2568c2ecf20Sopenharmony_ci			 */
2578c2ecf20Sopenharmony_ci			break;
2588c2ecf20Sopenharmony_ci		}
2598c2ecf20Sopenharmony_ci	} while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if (seq < last_emitted)
2628c2ecf20Sopenharmony_ci		radeon_fence_schedule_check(rdev, ring);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	return wake;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/**
2688c2ecf20Sopenharmony_ci * radeon_fence_check_lockup - check for hardware lockup
2698c2ecf20Sopenharmony_ci *
2708c2ecf20Sopenharmony_ci * @work: delayed work item
2718c2ecf20Sopenharmony_ci *
2728c2ecf20Sopenharmony_ci * Checks for fence activity and if there is none probe
2738c2ecf20Sopenharmony_ci * the hardware if a lockup occured.
2748c2ecf20Sopenharmony_ci */
2758c2ecf20Sopenharmony_cistatic void radeon_fence_check_lockup(struct work_struct *work)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct radeon_fence_driver *fence_drv;
2788c2ecf20Sopenharmony_ci	struct radeon_device *rdev;
2798c2ecf20Sopenharmony_ci	int ring;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	fence_drv = container_of(work, struct radeon_fence_driver,
2828c2ecf20Sopenharmony_ci				 lockup_work.work);
2838c2ecf20Sopenharmony_ci	rdev = fence_drv->rdev;
2848c2ecf20Sopenharmony_ci	ring = fence_drv - &rdev->fence_drv[0];
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (!down_read_trylock(&rdev->exclusive_lock)) {
2878c2ecf20Sopenharmony_ci		/* just reschedule the check if a reset is going on */
2888c2ecf20Sopenharmony_ci		radeon_fence_schedule_check(rdev, ring);
2898c2ecf20Sopenharmony_ci		return;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (fence_drv->delayed_irq && rdev->ddev->irq_enabled) {
2938c2ecf20Sopenharmony_ci		unsigned long irqflags;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		fence_drv->delayed_irq = false;
2968c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rdev->irq.lock, irqflags);
2978c2ecf20Sopenharmony_ci		radeon_irq_set(rdev);
2988c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	if (radeon_fence_activity(rdev, ring))
3028c2ecf20Sopenharmony_ci		wake_up_all(&rdev->fence_queue);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	else if (radeon_ring_is_lockup(rdev, ring, &rdev->ring[ring])) {
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci		/* good news we believe it's a lockup */
3078c2ecf20Sopenharmony_ci		dev_warn(rdev->dev, "GPU lockup (current fence id "
3088c2ecf20Sopenharmony_ci			 "0x%016llx last fence id 0x%016llx on ring %d)\n",
3098c2ecf20Sopenharmony_ci			 (uint64_t)atomic64_read(&fence_drv->last_seq),
3108c2ecf20Sopenharmony_ci			 fence_drv->sync_seq[ring], ring);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci		/* remember that we need an reset */
3138c2ecf20Sopenharmony_ci		rdev->needs_reset = true;
3148c2ecf20Sopenharmony_ci		wake_up_all(&rdev->fence_queue);
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci	up_read(&rdev->exclusive_lock);
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci/**
3208c2ecf20Sopenharmony_ci * radeon_fence_process - process a fence
3218c2ecf20Sopenharmony_ci *
3228c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
3238c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
3248c2ecf20Sopenharmony_ci *
3258c2ecf20Sopenharmony_ci * Checks the current fence value and wakes the fence queue
3268c2ecf20Sopenharmony_ci * if the sequence number has increased (all asics).
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_civoid radeon_fence_process(struct radeon_device *rdev, int ring)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	if (radeon_fence_activity(rdev, ring))
3318c2ecf20Sopenharmony_ci		wake_up_all(&rdev->fence_queue);
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci/**
3358c2ecf20Sopenharmony_ci * radeon_fence_seq_signaled - check if a fence sequence number has signaled
3368c2ecf20Sopenharmony_ci *
3378c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
3388c2ecf20Sopenharmony_ci * @seq: sequence number
3398c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
3408c2ecf20Sopenharmony_ci *
3418c2ecf20Sopenharmony_ci * Check if the last signaled fence sequnce number is >= the requested
3428c2ecf20Sopenharmony_ci * sequence number (all asics).
3438c2ecf20Sopenharmony_ci * Returns true if the fence has signaled (current fence value
3448c2ecf20Sopenharmony_ci * is >= requested value) or false if it has not (current fence
3458c2ecf20Sopenharmony_ci * value is < the requested value.  Helper function for
3468c2ecf20Sopenharmony_ci * radeon_fence_signaled().
3478c2ecf20Sopenharmony_ci */
3488c2ecf20Sopenharmony_cistatic bool radeon_fence_seq_signaled(struct radeon_device *rdev,
3498c2ecf20Sopenharmony_ci				      u64 seq, unsigned ring)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
3528c2ecf20Sopenharmony_ci		return true;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci	/* poll new last sequence at least once */
3558c2ecf20Sopenharmony_ci	radeon_fence_process(rdev, ring);
3568c2ecf20Sopenharmony_ci	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
3578c2ecf20Sopenharmony_ci		return true;
3588c2ecf20Sopenharmony_ci	}
3598c2ecf20Sopenharmony_ci	return false;
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic bool radeon_fence_is_signaled(struct dma_fence *f)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct radeon_fence *fence = to_radeon_fence(f);
3658c2ecf20Sopenharmony_ci	struct radeon_device *rdev = fence->rdev;
3668c2ecf20Sopenharmony_ci	unsigned ring = fence->ring;
3678c2ecf20Sopenharmony_ci	u64 seq = fence->seq;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
3708c2ecf20Sopenharmony_ci		return true;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (down_read_trylock(&rdev->exclusive_lock)) {
3748c2ecf20Sopenharmony_ci		radeon_fence_process(rdev, ring);
3758c2ecf20Sopenharmony_ci		up_read(&rdev->exclusive_lock);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci		if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
3788c2ecf20Sopenharmony_ci			return true;
3798c2ecf20Sopenharmony_ci		}
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci	return false;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci/**
3858c2ecf20Sopenharmony_ci * radeon_fence_enable_signaling - enable signalling on fence
3868c2ecf20Sopenharmony_ci * @fence: fence
3878c2ecf20Sopenharmony_ci *
3888c2ecf20Sopenharmony_ci * This function is called with fence_queue lock held, and adds a callback
3898c2ecf20Sopenharmony_ci * to fence_queue that checks if this fence is signaled, and if so it
3908c2ecf20Sopenharmony_ci * signals the fence and removes itself.
3918c2ecf20Sopenharmony_ci */
3928c2ecf20Sopenharmony_cistatic bool radeon_fence_enable_signaling(struct dma_fence *f)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	struct radeon_fence *fence = to_radeon_fence(f);
3958c2ecf20Sopenharmony_ci	struct radeon_device *rdev = fence->rdev;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq)
3988c2ecf20Sopenharmony_ci		return false;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	if (down_read_trylock(&rdev->exclusive_lock)) {
4018c2ecf20Sopenharmony_ci		radeon_irq_kms_sw_irq_get(rdev, fence->ring);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci		if (radeon_fence_activity(rdev, fence->ring))
4048c2ecf20Sopenharmony_ci			wake_up_all_locked(&rdev->fence_queue);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		/* did fence get signaled after we enabled the sw irq? */
4078c2ecf20Sopenharmony_ci		if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq) {
4088c2ecf20Sopenharmony_ci			radeon_irq_kms_sw_irq_put(rdev, fence->ring);
4098c2ecf20Sopenharmony_ci			up_read(&rdev->exclusive_lock);
4108c2ecf20Sopenharmony_ci			return false;
4118c2ecf20Sopenharmony_ci		}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci		up_read(&rdev->exclusive_lock);
4148c2ecf20Sopenharmony_ci	} else {
4158c2ecf20Sopenharmony_ci		/* we're probably in a lockup, lets not fiddle too much */
4168c2ecf20Sopenharmony_ci		if (radeon_irq_kms_sw_irq_get_delayed(rdev, fence->ring))
4178c2ecf20Sopenharmony_ci			rdev->fence_drv[fence->ring].delayed_irq = true;
4188c2ecf20Sopenharmony_ci		radeon_fence_schedule_check(rdev, fence->ring);
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	fence->fence_wake.flags = 0;
4228c2ecf20Sopenharmony_ci	fence->fence_wake.private = NULL;
4238c2ecf20Sopenharmony_ci	fence->fence_wake.func = radeon_fence_check_signaled;
4248c2ecf20Sopenharmony_ci	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
4258c2ecf20Sopenharmony_ci	dma_fence_get(f);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
4288c2ecf20Sopenharmony_ci	return true;
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci/**
4328c2ecf20Sopenharmony_ci * radeon_fence_signaled - check if a fence has signaled
4338c2ecf20Sopenharmony_ci *
4348c2ecf20Sopenharmony_ci * @fence: radeon fence object
4358c2ecf20Sopenharmony_ci *
4368c2ecf20Sopenharmony_ci * Check if the requested fence has signaled (all asics).
4378c2ecf20Sopenharmony_ci * Returns true if the fence has signaled or false if it has not.
4388c2ecf20Sopenharmony_ci */
4398c2ecf20Sopenharmony_cibool radeon_fence_signaled(struct radeon_fence *fence)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	if (!fence)
4428c2ecf20Sopenharmony_ci		return true;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
4458c2ecf20Sopenharmony_ci		int ret;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci		ret = dma_fence_signal(&fence->base);
4488c2ecf20Sopenharmony_ci		if (!ret)
4498c2ecf20Sopenharmony_ci			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
4508c2ecf20Sopenharmony_ci		return true;
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ci	return false;
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci/**
4568c2ecf20Sopenharmony_ci * radeon_fence_any_seq_signaled - check if any sequence number is signaled
4578c2ecf20Sopenharmony_ci *
4588c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
4598c2ecf20Sopenharmony_ci * @seq: sequence numbers
4608c2ecf20Sopenharmony_ci *
4618c2ecf20Sopenharmony_ci * Check if the last signaled fence sequnce number is >= the requested
4628c2ecf20Sopenharmony_ci * sequence number (all asics).
4638c2ecf20Sopenharmony_ci * Returns true if any has signaled (current value is >= requested value)
4648c2ecf20Sopenharmony_ci * or false if it has not. Helper function for radeon_fence_wait_seq.
4658c2ecf20Sopenharmony_ci */
4668c2ecf20Sopenharmony_cistatic bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	unsigned i;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
4718c2ecf20Sopenharmony_ci		if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i))
4728c2ecf20Sopenharmony_ci			return true;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci	return false;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci/**
4788c2ecf20Sopenharmony_ci * radeon_fence_wait_seq_timeout - wait for a specific sequence numbers
4798c2ecf20Sopenharmony_ci *
4808c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
4818c2ecf20Sopenharmony_ci * @target_seq: sequence number(s) we want to wait for
4828c2ecf20Sopenharmony_ci * @intr: use interruptable sleep
4838c2ecf20Sopenharmony_ci * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
4848c2ecf20Sopenharmony_ci *
4858c2ecf20Sopenharmony_ci * Wait for the requested sequence number(s) to be written by any ring
4868c2ecf20Sopenharmony_ci * (all asics).  Sequnce number array is indexed by ring id.
4878c2ecf20Sopenharmony_ci * @intr selects whether to use interruptable (true) or non-interruptable
4888c2ecf20Sopenharmony_ci * (false) sleep when waiting for the sequence number.  Helper function
4898c2ecf20Sopenharmony_ci * for radeon_fence_wait_*().
4908c2ecf20Sopenharmony_ci * Returns remaining time if the sequence number has passed, 0 when
4918c2ecf20Sopenharmony_ci * the wait timeout, or an error for all other cases.
4928c2ecf20Sopenharmony_ci * -EDEADLK is returned when a GPU lockup has been detected.
4938c2ecf20Sopenharmony_ci */
4948c2ecf20Sopenharmony_cistatic long radeon_fence_wait_seq_timeout(struct radeon_device *rdev,
4958c2ecf20Sopenharmony_ci					  u64 *target_seq, bool intr,
4968c2ecf20Sopenharmony_ci					  long timeout)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	long r;
4998c2ecf20Sopenharmony_ci	int i;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	if (radeon_fence_any_seq_signaled(rdev, target_seq))
5028c2ecf20Sopenharmony_ci		return timeout;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	/* enable IRQs and tracing */
5058c2ecf20Sopenharmony_ci	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
5068c2ecf20Sopenharmony_ci		if (!target_seq[i])
5078c2ecf20Sopenharmony_ci			continue;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci		trace_radeon_fence_wait_begin(rdev->ddev, i, target_seq[i]);
5108c2ecf20Sopenharmony_ci		radeon_irq_kms_sw_irq_get(rdev, i);
5118c2ecf20Sopenharmony_ci	}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	if (intr) {
5148c2ecf20Sopenharmony_ci		r = wait_event_interruptible_timeout(rdev->fence_queue, (
5158c2ecf20Sopenharmony_ci			radeon_fence_any_seq_signaled(rdev, target_seq)
5168c2ecf20Sopenharmony_ci			 || rdev->needs_reset), timeout);
5178c2ecf20Sopenharmony_ci	} else {
5188c2ecf20Sopenharmony_ci		r = wait_event_timeout(rdev->fence_queue, (
5198c2ecf20Sopenharmony_ci			radeon_fence_any_seq_signaled(rdev, target_seq)
5208c2ecf20Sopenharmony_ci			 || rdev->needs_reset), timeout);
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	if (rdev->needs_reset)
5248c2ecf20Sopenharmony_ci		r = -EDEADLK;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
5278c2ecf20Sopenharmony_ci		if (!target_seq[i])
5288c2ecf20Sopenharmony_ci			continue;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci		radeon_irq_kms_sw_irq_put(rdev, i);
5318c2ecf20Sopenharmony_ci		trace_radeon_fence_wait_end(rdev->ddev, i, target_seq[i]);
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	return r;
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci/**
5388c2ecf20Sopenharmony_ci * radeon_fence_wait_timeout - wait for a fence to signal with timeout
5398c2ecf20Sopenharmony_ci *
5408c2ecf20Sopenharmony_ci * @fence: radeon fence object
5418c2ecf20Sopenharmony_ci * @intr: use interruptible sleep
5428c2ecf20Sopenharmony_ci *
5438c2ecf20Sopenharmony_ci * Wait for the requested fence to signal (all asics).
5448c2ecf20Sopenharmony_ci * @intr selects whether to use interruptable (true) or non-interruptable
5458c2ecf20Sopenharmony_ci * (false) sleep when waiting for the fence.
5468c2ecf20Sopenharmony_ci * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
5478c2ecf20Sopenharmony_ci * Returns remaining time if the sequence number has passed, 0 when
5488c2ecf20Sopenharmony_ci * the wait timeout, or an error for all other cases.
5498c2ecf20Sopenharmony_ci */
5508c2ecf20Sopenharmony_cilong radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeout)
5518c2ecf20Sopenharmony_ci{
5528c2ecf20Sopenharmony_ci	uint64_t seq[RADEON_NUM_RINGS] = {};
5538c2ecf20Sopenharmony_ci	long r;
5548c2ecf20Sopenharmony_ci	int r_sig;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	/*
5578c2ecf20Sopenharmony_ci	 * This function should not be called on !radeon fences.
5588c2ecf20Sopenharmony_ci	 * If this is the case, it would mean this function can
5598c2ecf20Sopenharmony_ci	 * also be called on radeon fences belonging to another card.
5608c2ecf20Sopenharmony_ci	 * exclusive_lock is not held in that case.
5618c2ecf20Sopenharmony_ci	 */
5628c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!to_radeon_fence(&fence->base)))
5638c2ecf20Sopenharmony_ci		return dma_fence_wait(&fence->base, intr);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	seq[fence->ring] = fence->seq;
5668c2ecf20Sopenharmony_ci	r = radeon_fence_wait_seq_timeout(fence->rdev, seq, intr, timeout);
5678c2ecf20Sopenharmony_ci	if (r <= 0) {
5688c2ecf20Sopenharmony_ci		return r;
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	r_sig = dma_fence_signal(&fence->base);
5728c2ecf20Sopenharmony_ci	if (!r_sig)
5738c2ecf20Sopenharmony_ci		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
5748c2ecf20Sopenharmony_ci	return r;
5758c2ecf20Sopenharmony_ci}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci/**
5788c2ecf20Sopenharmony_ci * radeon_fence_wait - wait for a fence to signal
5798c2ecf20Sopenharmony_ci *
5808c2ecf20Sopenharmony_ci * @fence: radeon fence object
5818c2ecf20Sopenharmony_ci * @intr: use interruptible sleep
5828c2ecf20Sopenharmony_ci *
5838c2ecf20Sopenharmony_ci * Wait for the requested fence to signal (all asics).
5848c2ecf20Sopenharmony_ci * @intr selects whether to use interruptable (true) or non-interruptable
5858c2ecf20Sopenharmony_ci * (false) sleep when waiting for the fence.
5868c2ecf20Sopenharmony_ci * Returns 0 if the fence has passed, error for all other cases.
5878c2ecf20Sopenharmony_ci */
5888c2ecf20Sopenharmony_ciint radeon_fence_wait(struct radeon_fence *fence, bool intr)
5898c2ecf20Sopenharmony_ci{
5908c2ecf20Sopenharmony_ci	long r = radeon_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
5918c2ecf20Sopenharmony_ci	if (r > 0) {
5928c2ecf20Sopenharmony_ci		return 0;
5938c2ecf20Sopenharmony_ci	} else {
5948c2ecf20Sopenharmony_ci		return r;
5958c2ecf20Sopenharmony_ci	}
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci/**
5998c2ecf20Sopenharmony_ci * radeon_fence_wait_any - wait for a fence to signal on any ring
6008c2ecf20Sopenharmony_ci *
6018c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
6028c2ecf20Sopenharmony_ci * @fences: radeon fence object(s)
6038c2ecf20Sopenharmony_ci * @intr: use interruptable sleep
6048c2ecf20Sopenharmony_ci *
6058c2ecf20Sopenharmony_ci * Wait for any requested fence to signal (all asics).  Fence
6068c2ecf20Sopenharmony_ci * array is indexed by ring id.  @intr selects whether to use
6078c2ecf20Sopenharmony_ci * interruptable (true) or non-interruptable (false) sleep when
6088c2ecf20Sopenharmony_ci * waiting for the fences. Used by the suballocator.
6098c2ecf20Sopenharmony_ci * Returns 0 if any fence has passed, error for all other cases.
6108c2ecf20Sopenharmony_ci */
6118c2ecf20Sopenharmony_ciint radeon_fence_wait_any(struct radeon_device *rdev,
6128c2ecf20Sopenharmony_ci			  struct radeon_fence **fences,
6138c2ecf20Sopenharmony_ci			  bool intr)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	uint64_t seq[RADEON_NUM_RINGS];
6168c2ecf20Sopenharmony_ci	unsigned i, num_rings = 0;
6178c2ecf20Sopenharmony_ci	long r;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
6208c2ecf20Sopenharmony_ci		seq[i] = 0;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		if (!fences[i]) {
6238c2ecf20Sopenharmony_ci			continue;
6248c2ecf20Sopenharmony_ci		}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci		seq[i] = fences[i]->seq;
6278c2ecf20Sopenharmony_ci		++num_rings;
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	/* nothing to wait for ? */
6318c2ecf20Sopenharmony_ci	if (num_rings == 0)
6328c2ecf20Sopenharmony_ci		return -ENOENT;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	r = radeon_fence_wait_seq_timeout(rdev, seq, intr, MAX_SCHEDULE_TIMEOUT);
6358c2ecf20Sopenharmony_ci	if (r < 0) {
6368c2ecf20Sopenharmony_ci		return r;
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci	return 0;
6398c2ecf20Sopenharmony_ci}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci/**
6428c2ecf20Sopenharmony_ci * radeon_fence_wait_next - wait for the next fence to signal
6438c2ecf20Sopenharmony_ci *
6448c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
6458c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
6468c2ecf20Sopenharmony_ci *
6478c2ecf20Sopenharmony_ci * Wait for the next fence on the requested ring to signal (all asics).
6488c2ecf20Sopenharmony_ci * Returns 0 if the next fence has passed, error for all other cases.
6498c2ecf20Sopenharmony_ci * Caller must hold ring lock.
6508c2ecf20Sopenharmony_ci */
6518c2ecf20Sopenharmony_ciint radeon_fence_wait_next(struct radeon_device *rdev, int ring)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	uint64_t seq[RADEON_NUM_RINGS] = {};
6548c2ecf20Sopenharmony_ci	long r;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	seq[ring] = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
6578c2ecf20Sopenharmony_ci	if (seq[ring] >= rdev->fence_drv[ring].sync_seq[ring]) {
6588c2ecf20Sopenharmony_ci		/* nothing to wait for, last_seq is
6598c2ecf20Sopenharmony_ci		   already the last emited fence */
6608c2ecf20Sopenharmony_ci		return -ENOENT;
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci	r = radeon_fence_wait_seq_timeout(rdev, seq, false, MAX_SCHEDULE_TIMEOUT);
6638c2ecf20Sopenharmony_ci	if (r < 0)
6648c2ecf20Sopenharmony_ci		return r;
6658c2ecf20Sopenharmony_ci	return 0;
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci/**
6698c2ecf20Sopenharmony_ci * radeon_fence_wait_empty - wait for all fences to signal
6708c2ecf20Sopenharmony_ci *
6718c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
6728c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
6738c2ecf20Sopenharmony_ci *
6748c2ecf20Sopenharmony_ci * Wait for all fences on the requested ring to signal (all asics).
6758c2ecf20Sopenharmony_ci * Returns 0 if the fences have passed, error for all other cases.
6768c2ecf20Sopenharmony_ci * Caller must hold ring lock.
6778c2ecf20Sopenharmony_ci */
6788c2ecf20Sopenharmony_ciint radeon_fence_wait_empty(struct radeon_device *rdev, int ring)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	uint64_t seq[RADEON_NUM_RINGS] = {};
6818c2ecf20Sopenharmony_ci	long r;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	seq[ring] = rdev->fence_drv[ring].sync_seq[ring];
6848c2ecf20Sopenharmony_ci	if (!seq[ring])
6858c2ecf20Sopenharmony_ci		return 0;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	r = radeon_fence_wait_seq_timeout(rdev, seq, false, MAX_SCHEDULE_TIMEOUT);
6888c2ecf20Sopenharmony_ci	if (r < 0) {
6898c2ecf20Sopenharmony_ci		if (r == -EDEADLK)
6908c2ecf20Sopenharmony_ci			return -EDEADLK;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci		dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
6938c2ecf20Sopenharmony_ci			ring, r);
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci	return 0;
6968c2ecf20Sopenharmony_ci}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci/**
6998c2ecf20Sopenharmony_ci * radeon_fence_ref - take a ref on a fence
7008c2ecf20Sopenharmony_ci *
7018c2ecf20Sopenharmony_ci * @fence: radeon fence object
7028c2ecf20Sopenharmony_ci *
7038c2ecf20Sopenharmony_ci * Take a reference on a fence (all asics).
7048c2ecf20Sopenharmony_ci * Returns the fence.
7058c2ecf20Sopenharmony_ci */
7068c2ecf20Sopenharmony_cistruct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	dma_fence_get(&fence->base);
7098c2ecf20Sopenharmony_ci	return fence;
7108c2ecf20Sopenharmony_ci}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci/**
7138c2ecf20Sopenharmony_ci * radeon_fence_unref - remove a ref on a fence
7148c2ecf20Sopenharmony_ci *
7158c2ecf20Sopenharmony_ci * @fence: radeon fence object
7168c2ecf20Sopenharmony_ci *
7178c2ecf20Sopenharmony_ci * Remove a reference on a fence (all asics).
7188c2ecf20Sopenharmony_ci */
7198c2ecf20Sopenharmony_civoid radeon_fence_unref(struct radeon_fence **fence)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	struct radeon_fence *tmp = *fence;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	*fence = NULL;
7248c2ecf20Sopenharmony_ci	if (tmp) {
7258c2ecf20Sopenharmony_ci		dma_fence_put(&tmp->base);
7268c2ecf20Sopenharmony_ci	}
7278c2ecf20Sopenharmony_ci}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci/**
7308c2ecf20Sopenharmony_ci * radeon_fence_count_emitted - get the count of emitted fences
7318c2ecf20Sopenharmony_ci *
7328c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
7338c2ecf20Sopenharmony_ci * @ring: ring index the fence is associated with
7348c2ecf20Sopenharmony_ci *
7358c2ecf20Sopenharmony_ci * Get the number of fences emitted on the requested ring (all asics).
7368c2ecf20Sopenharmony_ci * Returns the number of emitted fences on the ring.  Used by the
7378c2ecf20Sopenharmony_ci * dynpm code to ring track activity.
7388c2ecf20Sopenharmony_ci */
7398c2ecf20Sopenharmony_ciunsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
7408c2ecf20Sopenharmony_ci{
7418c2ecf20Sopenharmony_ci	uint64_t emitted;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	/* We are not protected by ring lock when reading the last sequence
7448c2ecf20Sopenharmony_ci	 * but it's ok to report slightly wrong fence count here.
7458c2ecf20Sopenharmony_ci	 */
7468c2ecf20Sopenharmony_ci	radeon_fence_process(rdev, ring);
7478c2ecf20Sopenharmony_ci	emitted = rdev->fence_drv[ring].sync_seq[ring]
7488c2ecf20Sopenharmony_ci		- atomic64_read(&rdev->fence_drv[ring].last_seq);
7498c2ecf20Sopenharmony_ci	/* to avoid 32bits warp around */
7508c2ecf20Sopenharmony_ci	if (emitted > 0x10000000) {
7518c2ecf20Sopenharmony_ci		emitted = 0x10000000;
7528c2ecf20Sopenharmony_ci	}
7538c2ecf20Sopenharmony_ci	return (unsigned)emitted;
7548c2ecf20Sopenharmony_ci}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci/**
7578c2ecf20Sopenharmony_ci * radeon_fence_need_sync - do we need a semaphore
7588c2ecf20Sopenharmony_ci *
7598c2ecf20Sopenharmony_ci * @fence: radeon fence object
7608c2ecf20Sopenharmony_ci * @dst_ring: which ring to check against
7618c2ecf20Sopenharmony_ci *
7628c2ecf20Sopenharmony_ci * Check if the fence needs to be synced against another ring
7638c2ecf20Sopenharmony_ci * (all asics).  If so, we need to emit a semaphore.
7648c2ecf20Sopenharmony_ci * Returns true if we need to sync with another ring, false if
7658c2ecf20Sopenharmony_ci * not.
7668c2ecf20Sopenharmony_ci */
7678c2ecf20Sopenharmony_cibool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	struct radeon_fence_driver *fdrv;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	if (!fence) {
7728c2ecf20Sopenharmony_ci		return false;
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	if (fence->ring == dst_ring) {
7768c2ecf20Sopenharmony_ci		return false;
7778c2ecf20Sopenharmony_ci	}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	/* we are protected by the ring mutex */
7808c2ecf20Sopenharmony_ci	fdrv = &fence->rdev->fence_drv[dst_ring];
7818c2ecf20Sopenharmony_ci	if (fence->seq <= fdrv->sync_seq[fence->ring]) {
7828c2ecf20Sopenharmony_ci		return false;
7838c2ecf20Sopenharmony_ci	}
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	return true;
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci/**
7898c2ecf20Sopenharmony_ci * radeon_fence_note_sync - record the sync point
7908c2ecf20Sopenharmony_ci *
7918c2ecf20Sopenharmony_ci * @fence: radeon fence object
7928c2ecf20Sopenharmony_ci * @dst_ring: which ring to check against
7938c2ecf20Sopenharmony_ci *
7948c2ecf20Sopenharmony_ci * Note the sequence number at which point the fence will
7958c2ecf20Sopenharmony_ci * be synced with the requested ring (all asics).
7968c2ecf20Sopenharmony_ci */
7978c2ecf20Sopenharmony_civoid radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	struct radeon_fence_driver *dst, *src;
8008c2ecf20Sopenharmony_ci	unsigned i;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	if (!fence) {
8038c2ecf20Sopenharmony_ci		return;
8048c2ecf20Sopenharmony_ci	}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	if (fence->ring == dst_ring) {
8078c2ecf20Sopenharmony_ci		return;
8088c2ecf20Sopenharmony_ci	}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	/* we are protected by the ring mutex */
8118c2ecf20Sopenharmony_ci	src = &fence->rdev->fence_drv[fence->ring];
8128c2ecf20Sopenharmony_ci	dst = &fence->rdev->fence_drv[dst_ring];
8138c2ecf20Sopenharmony_ci	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
8148c2ecf20Sopenharmony_ci		if (i == dst_ring) {
8158c2ecf20Sopenharmony_ci			continue;
8168c2ecf20Sopenharmony_ci		}
8178c2ecf20Sopenharmony_ci		dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
8188c2ecf20Sopenharmony_ci	}
8198c2ecf20Sopenharmony_ci}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci/**
8228c2ecf20Sopenharmony_ci * radeon_fence_driver_start_ring - make the fence driver
8238c2ecf20Sopenharmony_ci * ready for use on the requested ring.
8248c2ecf20Sopenharmony_ci *
8258c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
8268c2ecf20Sopenharmony_ci * @ring: ring index to start the fence driver on
8278c2ecf20Sopenharmony_ci *
8288c2ecf20Sopenharmony_ci * Make the fence driver ready for processing (all asics).
8298c2ecf20Sopenharmony_ci * Not all asics have all rings, so each asic will only
8308c2ecf20Sopenharmony_ci * start the fence driver on the rings it has.
8318c2ecf20Sopenharmony_ci * Returns 0 for success, errors for failure.
8328c2ecf20Sopenharmony_ci */
8338c2ecf20Sopenharmony_ciint radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	uint64_t index;
8368c2ecf20Sopenharmony_ci	int r;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
8398c2ecf20Sopenharmony_ci	if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
8408c2ecf20Sopenharmony_ci		rdev->fence_drv[ring].scratch_reg = 0;
8418c2ecf20Sopenharmony_ci		if (ring != R600_RING_TYPE_UVD_INDEX) {
8428c2ecf20Sopenharmony_ci			index = R600_WB_EVENT_OFFSET + ring * 4;
8438c2ecf20Sopenharmony_ci			rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
8448c2ecf20Sopenharmony_ci			rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr +
8458c2ecf20Sopenharmony_ci							 index;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci		} else {
8488c2ecf20Sopenharmony_ci			/* put fence directly behind firmware */
8498c2ecf20Sopenharmony_ci			index = ALIGN(rdev->uvd_fw->size, 8);
8508c2ecf20Sopenharmony_ci			rdev->fence_drv[ring].cpu_addr = rdev->uvd.cpu_addr + index;
8518c2ecf20Sopenharmony_ci			rdev->fence_drv[ring].gpu_addr = rdev->uvd.gpu_addr + index;
8528c2ecf20Sopenharmony_ci		}
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	} else {
8558c2ecf20Sopenharmony_ci		r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
8568c2ecf20Sopenharmony_ci		if (r) {
8578c2ecf20Sopenharmony_ci			dev_err(rdev->dev, "fence failed to get scratch register\n");
8588c2ecf20Sopenharmony_ci			return r;
8598c2ecf20Sopenharmony_ci		}
8608c2ecf20Sopenharmony_ci		index = RADEON_WB_SCRATCH_OFFSET +
8618c2ecf20Sopenharmony_ci			rdev->fence_drv[ring].scratch_reg -
8628c2ecf20Sopenharmony_ci			rdev->scratch.reg_base;
8638c2ecf20Sopenharmony_ci		rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
8648c2ecf20Sopenharmony_ci		rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
8658c2ecf20Sopenharmony_ci	}
8668c2ecf20Sopenharmony_ci	radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
8678c2ecf20Sopenharmony_ci	rdev->fence_drv[ring].initialized = true;
8688c2ecf20Sopenharmony_ci	dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016llx\n",
8698c2ecf20Sopenharmony_ci		 ring, rdev->fence_drv[ring].gpu_addr);
8708c2ecf20Sopenharmony_ci	return 0;
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci/**
8748c2ecf20Sopenharmony_ci * radeon_fence_driver_init_ring - init the fence driver
8758c2ecf20Sopenharmony_ci * for the requested ring.
8768c2ecf20Sopenharmony_ci *
8778c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
8788c2ecf20Sopenharmony_ci * @ring: ring index to start the fence driver on
8798c2ecf20Sopenharmony_ci *
8808c2ecf20Sopenharmony_ci * Init the fence driver for the requested ring (all asics).
8818c2ecf20Sopenharmony_ci * Helper function for radeon_fence_driver_init().
8828c2ecf20Sopenharmony_ci */
8838c2ecf20Sopenharmony_cistatic void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
8848c2ecf20Sopenharmony_ci{
8858c2ecf20Sopenharmony_ci	int i;
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	rdev->fence_drv[ring].scratch_reg = -1;
8888c2ecf20Sopenharmony_ci	rdev->fence_drv[ring].cpu_addr = NULL;
8898c2ecf20Sopenharmony_ci	rdev->fence_drv[ring].gpu_addr = 0;
8908c2ecf20Sopenharmony_ci	for (i = 0; i < RADEON_NUM_RINGS; ++i)
8918c2ecf20Sopenharmony_ci		rdev->fence_drv[ring].sync_seq[i] = 0;
8928c2ecf20Sopenharmony_ci	atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
8938c2ecf20Sopenharmony_ci	rdev->fence_drv[ring].initialized = false;
8948c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&rdev->fence_drv[ring].lockup_work,
8958c2ecf20Sopenharmony_ci			  radeon_fence_check_lockup);
8968c2ecf20Sopenharmony_ci	rdev->fence_drv[ring].rdev = rdev;
8978c2ecf20Sopenharmony_ci}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci/**
9008c2ecf20Sopenharmony_ci * radeon_fence_driver_init - init the fence driver
9018c2ecf20Sopenharmony_ci * for all possible rings.
9028c2ecf20Sopenharmony_ci *
9038c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
9048c2ecf20Sopenharmony_ci *
9058c2ecf20Sopenharmony_ci * Init the fence driver for all possible rings (all asics).
9068c2ecf20Sopenharmony_ci * Not all asics have all rings, so each asic will only
9078c2ecf20Sopenharmony_ci * start the fence driver on the rings it has using
9088c2ecf20Sopenharmony_ci * radeon_fence_driver_start_ring().
9098c2ecf20Sopenharmony_ci * Returns 0 for success.
9108c2ecf20Sopenharmony_ci */
9118c2ecf20Sopenharmony_ciint radeon_fence_driver_init(struct radeon_device *rdev)
9128c2ecf20Sopenharmony_ci{
9138c2ecf20Sopenharmony_ci	int ring;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	init_waitqueue_head(&rdev->fence_queue);
9168c2ecf20Sopenharmony_ci	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
9178c2ecf20Sopenharmony_ci		radeon_fence_driver_init_ring(rdev, ring);
9188c2ecf20Sopenharmony_ci	}
9198c2ecf20Sopenharmony_ci	if (radeon_debugfs_fence_init(rdev)) {
9208c2ecf20Sopenharmony_ci		dev_err(rdev->dev, "fence debugfs file creation failed\n");
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci	return 0;
9238c2ecf20Sopenharmony_ci}
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci/**
9268c2ecf20Sopenharmony_ci * radeon_fence_driver_fini - tear down the fence driver
9278c2ecf20Sopenharmony_ci * for all possible rings.
9288c2ecf20Sopenharmony_ci *
9298c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
9308c2ecf20Sopenharmony_ci *
9318c2ecf20Sopenharmony_ci * Tear down the fence driver for all possible rings (all asics).
9328c2ecf20Sopenharmony_ci */
9338c2ecf20Sopenharmony_civoid radeon_fence_driver_fini(struct radeon_device *rdev)
9348c2ecf20Sopenharmony_ci{
9358c2ecf20Sopenharmony_ci	int ring, r;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	mutex_lock(&rdev->ring_lock);
9388c2ecf20Sopenharmony_ci	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
9398c2ecf20Sopenharmony_ci		if (!rdev->fence_drv[ring].initialized)
9408c2ecf20Sopenharmony_ci			continue;
9418c2ecf20Sopenharmony_ci		r = radeon_fence_wait_empty(rdev, ring);
9428c2ecf20Sopenharmony_ci		if (r) {
9438c2ecf20Sopenharmony_ci			/* no need to trigger GPU reset as we are unloading */
9448c2ecf20Sopenharmony_ci			radeon_fence_driver_force_completion(rdev, ring);
9458c2ecf20Sopenharmony_ci		}
9468c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&rdev->fence_drv[ring].lockup_work);
9478c2ecf20Sopenharmony_ci		wake_up_all(&rdev->fence_queue);
9488c2ecf20Sopenharmony_ci		radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
9498c2ecf20Sopenharmony_ci		rdev->fence_drv[ring].initialized = false;
9508c2ecf20Sopenharmony_ci	}
9518c2ecf20Sopenharmony_ci	mutex_unlock(&rdev->ring_lock);
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci/**
9558c2ecf20Sopenharmony_ci * radeon_fence_driver_force_completion - force all fence waiter to complete
9568c2ecf20Sopenharmony_ci *
9578c2ecf20Sopenharmony_ci * @rdev: radeon device pointer
9588c2ecf20Sopenharmony_ci * @ring: the ring to complete
9598c2ecf20Sopenharmony_ci *
9608c2ecf20Sopenharmony_ci * In case of GPU reset failure make sure no process keep waiting on fence
9618c2ecf20Sopenharmony_ci * that will never complete.
9628c2ecf20Sopenharmony_ci */
9638c2ecf20Sopenharmony_civoid radeon_fence_driver_force_completion(struct radeon_device *rdev, int ring)
9648c2ecf20Sopenharmony_ci{
9658c2ecf20Sopenharmony_ci	if (rdev->fence_drv[ring].initialized) {
9668c2ecf20Sopenharmony_ci		radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
9678c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&rdev->fence_drv[ring].lockup_work);
9688c2ecf20Sopenharmony_ci	}
9698c2ecf20Sopenharmony_ci}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci/*
9738c2ecf20Sopenharmony_ci * Fence debugfs
9748c2ecf20Sopenharmony_ci */
9758c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
9768c2ecf20Sopenharmony_cistatic int radeon_debugfs_fence_info(struct seq_file *m, void *data)
9778c2ecf20Sopenharmony_ci{
9788c2ecf20Sopenharmony_ci	struct drm_info_node *node = (struct drm_info_node *)m->private;
9798c2ecf20Sopenharmony_ci	struct drm_device *dev = node->minor->dev;
9808c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
9818c2ecf20Sopenharmony_ci	int i, j;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
9848c2ecf20Sopenharmony_ci		if (!rdev->fence_drv[i].initialized)
9858c2ecf20Sopenharmony_ci			continue;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci		radeon_fence_process(rdev, i);
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci		seq_printf(m, "--- ring %d ---\n", i);
9908c2ecf20Sopenharmony_ci		seq_printf(m, "Last signaled fence 0x%016llx\n",
9918c2ecf20Sopenharmony_ci			   (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
9928c2ecf20Sopenharmony_ci		seq_printf(m, "Last emitted        0x%016llx\n",
9938c2ecf20Sopenharmony_ci			   rdev->fence_drv[i].sync_seq[i]);
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci		for (j = 0; j < RADEON_NUM_RINGS; ++j) {
9968c2ecf20Sopenharmony_ci			if (i != j && rdev->fence_drv[j].initialized)
9978c2ecf20Sopenharmony_ci				seq_printf(m, "Last sync to ring %d 0x%016llx\n",
9988c2ecf20Sopenharmony_ci					   j, rdev->fence_drv[i].sync_seq[j]);
9998c2ecf20Sopenharmony_ci		}
10008c2ecf20Sopenharmony_ci	}
10018c2ecf20Sopenharmony_ci	return 0;
10028c2ecf20Sopenharmony_ci}
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci/**
10058c2ecf20Sopenharmony_ci * radeon_debugfs_gpu_reset - manually trigger a gpu reset
10068c2ecf20Sopenharmony_ci *
10078c2ecf20Sopenharmony_ci * Manually trigger a gpu reset at the next fence wait.
10088c2ecf20Sopenharmony_ci */
10098c2ecf20Sopenharmony_cistatic int radeon_debugfs_gpu_reset(struct seq_file *m, void *data)
10108c2ecf20Sopenharmony_ci{
10118c2ecf20Sopenharmony_ci	struct drm_info_node *node = (struct drm_info_node *) m->private;
10128c2ecf20Sopenharmony_ci	struct drm_device *dev = node->minor->dev;
10138c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	down_read(&rdev->exclusive_lock);
10168c2ecf20Sopenharmony_ci	seq_printf(m, "%d\n", rdev->needs_reset);
10178c2ecf20Sopenharmony_ci	rdev->needs_reset = true;
10188c2ecf20Sopenharmony_ci	wake_up_all(&rdev->fence_queue);
10198c2ecf20Sopenharmony_ci	up_read(&rdev->exclusive_lock);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	return 0;
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_cistatic struct drm_info_list radeon_debugfs_fence_list[] = {
10258c2ecf20Sopenharmony_ci	{"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
10268c2ecf20Sopenharmony_ci	{"radeon_gpu_reset", &radeon_debugfs_gpu_reset, 0, NULL}
10278c2ecf20Sopenharmony_ci};
10288c2ecf20Sopenharmony_ci#endif
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ciint radeon_debugfs_fence_init(struct radeon_device *rdev)
10318c2ecf20Sopenharmony_ci{
10328c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
10338c2ecf20Sopenharmony_ci	return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 2);
10348c2ecf20Sopenharmony_ci#else
10358c2ecf20Sopenharmony_ci	return 0;
10368c2ecf20Sopenharmony_ci#endif
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cistatic const char *radeon_fence_get_driver_name(struct dma_fence *fence)
10408c2ecf20Sopenharmony_ci{
10418c2ecf20Sopenharmony_ci	return "radeon";
10428c2ecf20Sopenharmony_ci}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_cistatic const char *radeon_fence_get_timeline_name(struct dma_fence *f)
10458c2ecf20Sopenharmony_ci{
10468c2ecf20Sopenharmony_ci	struct radeon_fence *fence = to_radeon_fence(f);
10478c2ecf20Sopenharmony_ci	switch (fence->ring) {
10488c2ecf20Sopenharmony_ci	case RADEON_RING_TYPE_GFX_INDEX: return "radeon.gfx";
10498c2ecf20Sopenharmony_ci	case CAYMAN_RING_TYPE_CP1_INDEX: return "radeon.cp1";
10508c2ecf20Sopenharmony_ci	case CAYMAN_RING_TYPE_CP2_INDEX: return "radeon.cp2";
10518c2ecf20Sopenharmony_ci	case R600_RING_TYPE_DMA_INDEX: return "radeon.dma";
10528c2ecf20Sopenharmony_ci	case CAYMAN_RING_TYPE_DMA1_INDEX: return "radeon.dma1";
10538c2ecf20Sopenharmony_ci	case R600_RING_TYPE_UVD_INDEX: return "radeon.uvd";
10548c2ecf20Sopenharmony_ci	case TN_RING_TYPE_VCE1_INDEX: return "radeon.vce1";
10558c2ecf20Sopenharmony_ci	case TN_RING_TYPE_VCE2_INDEX: return "radeon.vce2";
10568c2ecf20Sopenharmony_ci	default: WARN_ON_ONCE(1); return "radeon.unk";
10578c2ecf20Sopenharmony_ci	}
10588c2ecf20Sopenharmony_ci}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_cistatic inline bool radeon_test_signaled(struct radeon_fence *fence)
10618c2ecf20Sopenharmony_ci{
10628c2ecf20Sopenharmony_ci	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
10638c2ecf20Sopenharmony_ci}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_cistruct radeon_wait_cb {
10668c2ecf20Sopenharmony_ci	struct dma_fence_cb base;
10678c2ecf20Sopenharmony_ci	struct task_struct *task;
10688c2ecf20Sopenharmony_ci};
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_cistatic void
10718c2ecf20Sopenharmony_ciradeon_fence_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	struct radeon_wait_cb *wait =
10748c2ecf20Sopenharmony_ci		container_of(cb, struct radeon_wait_cb, base);
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	wake_up_process(wait->task);
10778c2ecf20Sopenharmony_ci}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_cistatic signed long radeon_fence_default_wait(struct dma_fence *f, bool intr,
10808c2ecf20Sopenharmony_ci					     signed long t)
10818c2ecf20Sopenharmony_ci{
10828c2ecf20Sopenharmony_ci	struct radeon_fence *fence = to_radeon_fence(f);
10838c2ecf20Sopenharmony_ci	struct radeon_device *rdev = fence->rdev;
10848c2ecf20Sopenharmony_ci	struct radeon_wait_cb cb;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	cb.task = current;
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	if (dma_fence_add_callback(f, &cb.base, radeon_fence_wait_cb))
10898c2ecf20Sopenharmony_ci		return t;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	while (t > 0) {
10928c2ecf20Sopenharmony_ci		if (intr)
10938c2ecf20Sopenharmony_ci			set_current_state(TASK_INTERRUPTIBLE);
10948c2ecf20Sopenharmony_ci		else
10958c2ecf20Sopenharmony_ci			set_current_state(TASK_UNINTERRUPTIBLE);
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci		/*
10988c2ecf20Sopenharmony_ci		 * radeon_test_signaled must be called after
10998c2ecf20Sopenharmony_ci		 * set_current_state to prevent a race with wake_up_process
11008c2ecf20Sopenharmony_ci		 */
11018c2ecf20Sopenharmony_ci		if (radeon_test_signaled(fence))
11028c2ecf20Sopenharmony_ci			break;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci		if (rdev->needs_reset) {
11058c2ecf20Sopenharmony_ci			t = -EDEADLK;
11068c2ecf20Sopenharmony_ci			break;
11078c2ecf20Sopenharmony_ci		}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci		t = schedule_timeout(t);
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci		if (t > 0 && intr && signal_pending(current))
11128c2ecf20Sopenharmony_ci			t = -ERESTARTSYS;
11138c2ecf20Sopenharmony_ci	}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	__set_current_state(TASK_RUNNING);
11168c2ecf20Sopenharmony_ci	dma_fence_remove_callback(f, &cb.base);
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	return t;
11198c2ecf20Sopenharmony_ci}
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ciconst struct dma_fence_ops radeon_fence_ops = {
11228c2ecf20Sopenharmony_ci	.get_driver_name = radeon_fence_get_driver_name,
11238c2ecf20Sopenharmony_ci	.get_timeline_name = radeon_fence_get_timeline_name,
11248c2ecf20Sopenharmony_ci	.enable_signaling = radeon_fence_enable_signaling,
11258c2ecf20Sopenharmony_ci	.signaled = radeon_fence_is_signaled,
11268c2ecf20Sopenharmony_ci	.wait = radeon_fence_default_wait,
11278c2ecf20Sopenharmony_ci	.release = NULL,
11288c2ecf20Sopenharmony_ci};
1129