18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2008 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci * Copyright 2008 Red Hat Inc.
48c2ecf20Sopenharmony_ci * Copyright 2009 Jerome Glisse.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
88c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
98c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
108c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
118c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
148c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
178c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
198c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
208c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
218c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
228c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Authors: Dave Airlie
258c2ecf20Sopenharmony_ci *          Alex Deucher
268c2ecf20Sopenharmony_ci *          Jerome Glisse
278c2ecf20Sopenharmony_ci *          Christian König
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <drm/drm_debugfs.h>
318c2ecf20Sopenharmony_ci#include <drm/drm_device.h>
328c2ecf20Sopenharmony_ci#include <drm/drm_file.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include "radeon.h"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Rings
388c2ecf20Sopenharmony_ci * Most engines on the GPU are fed via ring buffers.  Ring
398c2ecf20Sopenharmony_ci * buffers are areas of GPU accessible memory that the host
408c2ecf20Sopenharmony_ci * writes commands into and the GPU reads commands out of.
418c2ecf20Sopenharmony_ci * There is a rptr (read pointer) that determines where the
428c2ecf20Sopenharmony_ci * GPU is currently reading, and a wptr (write pointer)
438c2ecf20Sopenharmony_ci * which determines where the host has written.  When the
448c2ecf20Sopenharmony_ci * pointers are equal, the ring is idle.  When the host
458c2ecf20Sopenharmony_ci * writes commands to the ring buffer, it increments the
468c2ecf20Sopenharmony_ci * wptr.  The GPU then starts fetching commands and executes
478c2ecf20Sopenharmony_ci * them until the pointers are equal again.
488c2ecf20Sopenharmony_ci */
498c2ecf20Sopenharmony_cistatic int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * radeon_ring_supports_scratch_reg - check if the ring supports
538c2ecf20Sopenharmony_ci * writing to scratch registers
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
568c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Check if a specific ring supports writing to scratch registers (all asics).
598c2ecf20Sopenharmony_ci * Returns true if the ring supports writing to scratch regs, false if not.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_cibool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
628c2ecf20Sopenharmony_ci				      struct radeon_ring *ring)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	switch (ring->idx) {
658c2ecf20Sopenharmony_ci	case RADEON_RING_TYPE_GFX_INDEX:
668c2ecf20Sopenharmony_ci	case CAYMAN_RING_TYPE_CP1_INDEX:
678c2ecf20Sopenharmony_ci	case CAYMAN_RING_TYPE_CP2_INDEX:
688c2ecf20Sopenharmony_ci		return true;
698c2ecf20Sopenharmony_ci	default:
708c2ecf20Sopenharmony_ci		return false;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * radeon_ring_free_size - update the free size
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
788c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * Update the free dw slots in the ring buffer (all asics).
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_civoid radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* This works because ring_size is a power of 2 */
878c2ecf20Sopenharmony_ci	ring->ring_free_dw = rptr + (ring->ring_size / 4);
888c2ecf20Sopenharmony_ci	ring->ring_free_dw -= ring->wptr;
898c2ecf20Sopenharmony_ci	ring->ring_free_dw &= ring->ptr_mask;
908c2ecf20Sopenharmony_ci	if (!ring->ring_free_dw) {
918c2ecf20Sopenharmony_ci		/* this is an empty ring */
928c2ecf20Sopenharmony_ci		ring->ring_free_dw = ring->ring_size / 4;
938c2ecf20Sopenharmony_ci		/*  update lockup info to avoid false positive */
948c2ecf20Sopenharmony_ci		radeon_ring_lockup_update(rdev, ring);
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/**
998c2ecf20Sopenharmony_ci * radeon_ring_alloc - allocate space on the ring buffer
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1028c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
1038c2ecf20Sopenharmony_ci * @ndw: number of dwords to allocate in the ring buffer
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * Allocate @ndw dwords in the ring buffer (all asics).
1068c2ecf20Sopenharmony_ci * Returns 0 on success, error on failure.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ciint radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	int r;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/* make sure we aren't trying to allocate more space than there is on the ring */
1138c2ecf20Sopenharmony_ci	if (ndw > (ring->ring_size / 4))
1148c2ecf20Sopenharmony_ci		return -ENOMEM;
1158c2ecf20Sopenharmony_ci	/* Align requested size with padding so unlock_commit can
1168c2ecf20Sopenharmony_ci	 * pad safely */
1178c2ecf20Sopenharmony_ci	radeon_ring_free_size(rdev, ring);
1188c2ecf20Sopenharmony_ci	ndw = (ndw + ring->align_mask) & ~ring->align_mask;
1198c2ecf20Sopenharmony_ci	while (ndw > (ring->ring_free_dw - 1)) {
1208c2ecf20Sopenharmony_ci		radeon_ring_free_size(rdev, ring);
1218c2ecf20Sopenharmony_ci		if (ndw < ring->ring_free_dw) {
1228c2ecf20Sopenharmony_ci			break;
1238c2ecf20Sopenharmony_ci		}
1248c2ecf20Sopenharmony_ci		r = radeon_fence_wait_next(rdev, ring->idx);
1258c2ecf20Sopenharmony_ci		if (r)
1268c2ecf20Sopenharmony_ci			return r;
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci	ring->count_dw = ndw;
1298c2ecf20Sopenharmony_ci	ring->wptr_old = ring->wptr;
1308c2ecf20Sopenharmony_ci	return 0;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/**
1348c2ecf20Sopenharmony_ci * radeon_ring_lock - lock the ring and allocate space on it
1358c2ecf20Sopenharmony_ci *
1368c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1378c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
1388c2ecf20Sopenharmony_ci * @ndw: number of dwords to allocate in the ring buffer
1398c2ecf20Sopenharmony_ci *
1408c2ecf20Sopenharmony_ci * Lock the ring and allocate @ndw dwords in the ring buffer
1418c2ecf20Sopenharmony_ci * (all asics).
1428c2ecf20Sopenharmony_ci * Returns 0 on success, error on failure.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_ciint radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	int r;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	mutex_lock(&rdev->ring_lock);
1498c2ecf20Sopenharmony_ci	r = radeon_ring_alloc(rdev, ring, ndw);
1508c2ecf20Sopenharmony_ci	if (r) {
1518c2ecf20Sopenharmony_ci		mutex_unlock(&rdev->ring_lock);
1528c2ecf20Sopenharmony_ci		return r;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci	return 0;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/**
1588c2ecf20Sopenharmony_ci * radeon_ring_commit - tell the GPU to execute the new
1598c2ecf20Sopenharmony_ci * commands on the ring buffer
1608c2ecf20Sopenharmony_ci *
1618c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1628c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
1638c2ecf20Sopenharmony_ci * @hdp_flush: Whether or not to perform an HDP cache flush
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * Update the wptr (write pointer) to tell the GPU to
1668c2ecf20Sopenharmony_ci * execute new commands on the ring buffer (all asics).
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_civoid radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring,
1698c2ecf20Sopenharmony_ci			bool hdp_flush)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	/* If we are emitting the HDP flush via the ring buffer, we need to
1728c2ecf20Sopenharmony_ci	 * do it before padding.
1738c2ecf20Sopenharmony_ci	 */
1748c2ecf20Sopenharmony_ci	if (hdp_flush && rdev->asic->ring[ring->idx]->hdp_flush)
1758c2ecf20Sopenharmony_ci		rdev->asic->ring[ring->idx]->hdp_flush(rdev, ring);
1768c2ecf20Sopenharmony_ci	/* We pad to match fetch size */
1778c2ecf20Sopenharmony_ci	while (ring->wptr & ring->align_mask) {
1788c2ecf20Sopenharmony_ci		radeon_ring_write(ring, ring->nop);
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci	mb();
1818c2ecf20Sopenharmony_ci	/* If we are emitting the HDP flush via MMIO, we need to do it after
1828c2ecf20Sopenharmony_ci	 * all CPU writes to VRAM finished.
1838c2ecf20Sopenharmony_ci	 */
1848c2ecf20Sopenharmony_ci	if (hdp_flush && rdev->asic->mmio_hdp_flush)
1858c2ecf20Sopenharmony_ci		rdev->asic->mmio_hdp_flush(rdev);
1868c2ecf20Sopenharmony_ci	radeon_ring_set_wptr(rdev, ring);
1878c2ecf20Sopenharmony_ci	mmiowb(); /* Make sure wptr is up-to-date for hw */
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/**
1918c2ecf20Sopenharmony_ci * radeon_ring_unlock_commit - tell the GPU to execute the new
1928c2ecf20Sopenharmony_ci * commands on the ring buffer and unlock it
1938c2ecf20Sopenharmony_ci *
1948c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1958c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
1968c2ecf20Sopenharmony_ci * @hdp_flush: Whether or not to perform an HDP cache flush
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * Call radeon_ring_commit() then unlock the ring (all asics).
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_civoid radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring,
2018c2ecf20Sopenharmony_ci			       bool hdp_flush)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	radeon_ring_commit(rdev, ring, hdp_flush);
2048c2ecf20Sopenharmony_ci	mutex_unlock(&rdev->ring_lock);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/**
2088c2ecf20Sopenharmony_ci * radeon_ring_undo - reset the wptr
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
2118c2ecf20Sopenharmony_ci *
2128c2ecf20Sopenharmony_ci * Reset the driver's copy of the wptr (all asics).
2138c2ecf20Sopenharmony_ci */
2148c2ecf20Sopenharmony_civoid radeon_ring_undo(struct radeon_ring *ring)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	ring->wptr = ring->wptr_old;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/**
2208c2ecf20Sopenharmony_ci * radeon_ring_unlock_undo - reset the wptr and unlock the ring
2218c2ecf20Sopenharmony_ci *
2228c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
2238c2ecf20Sopenharmony_ci *
2248c2ecf20Sopenharmony_ci * Call radeon_ring_undo() then unlock the ring (all asics).
2258c2ecf20Sopenharmony_ci */
2268c2ecf20Sopenharmony_civoid radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	radeon_ring_undo(ring);
2298c2ecf20Sopenharmony_ci	mutex_unlock(&rdev->ring_lock);
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/**
2338c2ecf20Sopenharmony_ci * radeon_ring_lockup_update - update lockup variables
2348c2ecf20Sopenharmony_ci *
2358c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
2368c2ecf20Sopenharmony_ci *
2378c2ecf20Sopenharmony_ci * Update the last rptr value and timestamp (all asics).
2388c2ecf20Sopenharmony_ci */
2398c2ecf20Sopenharmony_civoid radeon_ring_lockup_update(struct radeon_device *rdev,
2408c2ecf20Sopenharmony_ci			       struct radeon_ring *ring)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring));
2438c2ecf20Sopenharmony_ci	atomic64_set(&ring->last_activity, jiffies_64);
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/**
2478c2ecf20Sopenharmony_ci * radeon_ring_test_lockup() - check if ring is lockedup by recording information
2488c2ecf20Sopenharmony_ci * @rdev:       radeon device structure
2498c2ecf20Sopenharmony_ci * @ring:       radeon_ring structure holding ring information
2508c2ecf20Sopenharmony_ci *
2518c2ecf20Sopenharmony_ci */
2528c2ecf20Sopenharmony_cibool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
2558c2ecf20Sopenharmony_ci	uint64_t last = atomic64_read(&ring->last_activity);
2568c2ecf20Sopenharmony_ci	uint64_t elapsed;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	if (rptr != atomic_read(&ring->last_rptr)) {
2598c2ecf20Sopenharmony_ci		/* ring is still working, no lockup */
2608c2ecf20Sopenharmony_ci		radeon_ring_lockup_update(rdev, ring);
2618c2ecf20Sopenharmony_ci		return false;
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	elapsed = jiffies_to_msecs(jiffies_64 - last);
2658c2ecf20Sopenharmony_ci	if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
2668c2ecf20Sopenharmony_ci		dev_err(rdev->dev, "ring %d stalled for more than %llumsec\n",
2678c2ecf20Sopenharmony_ci			ring->idx, elapsed);
2688c2ecf20Sopenharmony_ci		return true;
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci	/* give a chance to the GPU ... */
2718c2ecf20Sopenharmony_ci	return false;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/**
2758c2ecf20Sopenharmony_ci * radeon_ring_backup - Back up the content of a ring
2768c2ecf20Sopenharmony_ci *
2778c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
2788c2ecf20Sopenharmony_ci * @ring: the ring we want to back up
2798c2ecf20Sopenharmony_ci *
2808c2ecf20Sopenharmony_ci * Saves all unprocessed commits from a ring, returns the number of dwords saved.
2818c2ecf20Sopenharmony_ci */
2828c2ecf20Sopenharmony_ciunsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
2838c2ecf20Sopenharmony_ci			    uint32_t **data)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	unsigned size, ptr, i;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* just in case lock the ring */
2888c2ecf20Sopenharmony_ci	mutex_lock(&rdev->ring_lock);
2898c2ecf20Sopenharmony_ci	*data = NULL;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	if (ring->ring_obj == NULL) {
2928c2ecf20Sopenharmony_ci		mutex_unlock(&rdev->ring_lock);
2938c2ecf20Sopenharmony_ci		return 0;
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* it doesn't make sense to save anything if all fences are signaled */
2978c2ecf20Sopenharmony_ci	if (!radeon_fence_count_emitted(rdev, ring->idx)) {
2988c2ecf20Sopenharmony_ci		mutex_unlock(&rdev->ring_lock);
2998c2ecf20Sopenharmony_ci		return 0;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/* calculate the number of dw on the ring */
3038c2ecf20Sopenharmony_ci	if (ring->rptr_save_reg)
3048c2ecf20Sopenharmony_ci		ptr = RREG32(ring->rptr_save_reg);
3058c2ecf20Sopenharmony_ci	else if (rdev->wb.enabled)
3068c2ecf20Sopenharmony_ci		ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
3078c2ecf20Sopenharmony_ci	else {
3088c2ecf20Sopenharmony_ci		/* no way to read back the next rptr */
3098c2ecf20Sopenharmony_ci		mutex_unlock(&rdev->ring_lock);
3108c2ecf20Sopenharmony_ci		return 0;
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	size = ring->wptr + (ring->ring_size / 4);
3148c2ecf20Sopenharmony_ci	size -= ptr;
3158c2ecf20Sopenharmony_ci	size &= ring->ptr_mask;
3168c2ecf20Sopenharmony_ci	if (size == 0) {
3178c2ecf20Sopenharmony_ci		mutex_unlock(&rdev->ring_lock);
3188c2ecf20Sopenharmony_ci		return 0;
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/* and then save the content of the ring */
3228c2ecf20Sopenharmony_ci	*data = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
3238c2ecf20Sopenharmony_ci	if (!*data) {
3248c2ecf20Sopenharmony_ci		mutex_unlock(&rdev->ring_lock);
3258c2ecf20Sopenharmony_ci		return 0;
3268c2ecf20Sopenharmony_ci	}
3278c2ecf20Sopenharmony_ci	for (i = 0; i < size; ++i) {
3288c2ecf20Sopenharmony_ci		(*data)[i] = ring->ring[ptr++];
3298c2ecf20Sopenharmony_ci		ptr &= ring->ptr_mask;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	mutex_unlock(&rdev->ring_lock);
3338c2ecf20Sopenharmony_ci	return size;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci/**
3378c2ecf20Sopenharmony_ci * radeon_ring_restore - append saved commands to the ring again
3388c2ecf20Sopenharmony_ci *
3398c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
3408c2ecf20Sopenharmony_ci * @ring: ring to append commands to
3418c2ecf20Sopenharmony_ci * @size: number of dwords we want to write
3428c2ecf20Sopenharmony_ci * @data: saved commands
3438c2ecf20Sopenharmony_ci *
3448c2ecf20Sopenharmony_ci * Allocates space on the ring and restore the previously saved commands.
3458c2ecf20Sopenharmony_ci */
3468c2ecf20Sopenharmony_ciint radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
3478c2ecf20Sopenharmony_ci			unsigned size, uint32_t *data)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	int i, r;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if (!size || !data)
3528c2ecf20Sopenharmony_ci		return 0;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* restore the saved ring content */
3558c2ecf20Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, size);
3568c2ecf20Sopenharmony_ci	if (r)
3578c2ecf20Sopenharmony_ci		return r;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	for (i = 0; i < size; ++i) {
3608c2ecf20Sopenharmony_ci		radeon_ring_write(ring, data[i]);
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
3648c2ecf20Sopenharmony_ci	kvfree(data);
3658c2ecf20Sopenharmony_ci	return 0;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci/**
3698c2ecf20Sopenharmony_ci * radeon_ring_init - init driver ring struct.
3708c2ecf20Sopenharmony_ci *
3718c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
3728c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
3738c2ecf20Sopenharmony_ci * @ring_size: size of the ring
3748c2ecf20Sopenharmony_ci * @rptr_offs: offset of the rptr writeback location in the WB buffer
3758c2ecf20Sopenharmony_ci * @nop: nop packet for this ring
3768c2ecf20Sopenharmony_ci *
3778c2ecf20Sopenharmony_ci * Initialize the driver information for the selected ring (all asics).
3788c2ecf20Sopenharmony_ci * Returns 0 on success, error on failure.
3798c2ecf20Sopenharmony_ci */
3808c2ecf20Sopenharmony_ciint radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
3818c2ecf20Sopenharmony_ci		     unsigned rptr_offs, u32 nop)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	int r;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	ring->ring_size = ring_size;
3868c2ecf20Sopenharmony_ci	ring->rptr_offs = rptr_offs;
3878c2ecf20Sopenharmony_ci	ring->nop = nop;
3888c2ecf20Sopenharmony_ci	/* Allocate ring buffer */
3898c2ecf20Sopenharmony_ci	if (ring->ring_obj == NULL) {
3908c2ecf20Sopenharmony_ci		r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
3918c2ecf20Sopenharmony_ci				     RADEON_GEM_DOMAIN_GTT, 0, NULL,
3928c2ecf20Sopenharmony_ci				     NULL, &ring->ring_obj);
3938c2ecf20Sopenharmony_ci		if (r) {
3948c2ecf20Sopenharmony_ci			dev_err(rdev->dev, "(%d) ring create failed\n", r);
3958c2ecf20Sopenharmony_ci			return r;
3968c2ecf20Sopenharmony_ci		}
3978c2ecf20Sopenharmony_ci		r = radeon_bo_reserve(ring->ring_obj, false);
3988c2ecf20Sopenharmony_ci		if (unlikely(r != 0))
3998c2ecf20Sopenharmony_ci			return r;
4008c2ecf20Sopenharmony_ci		r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
4018c2ecf20Sopenharmony_ci					&ring->gpu_addr);
4028c2ecf20Sopenharmony_ci		if (r) {
4038c2ecf20Sopenharmony_ci			radeon_bo_unreserve(ring->ring_obj);
4048c2ecf20Sopenharmony_ci			dev_err(rdev->dev, "(%d) ring pin failed\n", r);
4058c2ecf20Sopenharmony_ci			return r;
4068c2ecf20Sopenharmony_ci		}
4078c2ecf20Sopenharmony_ci		r = radeon_bo_kmap(ring->ring_obj,
4088c2ecf20Sopenharmony_ci				       (void **)&ring->ring);
4098c2ecf20Sopenharmony_ci		radeon_bo_unreserve(ring->ring_obj);
4108c2ecf20Sopenharmony_ci		if (r) {
4118c2ecf20Sopenharmony_ci			dev_err(rdev->dev, "(%d) ring map failed\n", r);
4128c2ecf20Sopenharmony_ci			return r;
4138c2ecf20Sopenharmony_ci		}
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci	ring->ptr_mask = (ring->ring_size / 4) - 1;
4168c2ecf20Sopenharmony_ci	ring->ring_free_dw = ring->ring_size / 4;
4178c2ecf20Sopenharmony_ci	if (rdev->wb.enabled) {
4188c2ecf20Sopenharmony_ci		u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
4198c2ecf20Sopenharmony_ci		ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
4208c2ecf20Sopenharmony_ci		ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci	if (radeon_debugfs_ring_init(rdev, ring)) {
4238c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to register debugfs file for rings !\n");
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci	radeon_ring_lockup_update(rdev, ring);
4268c2ecf20Sopenharmony_ci	return 0;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci/**
4308c2ecf20Sopenharmony_ci * radeon_ring_fini - tear down the driver ring struct.
4318c2ecf20Sopenharmony_ci *
4328c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
4338c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
4348c2ecf20Sopenharmony_ci *
4358c2ecf20Sopenharmony_ci * Tear down the driver information for the selected ring (all asics).
4368c2ecf20Sopenharmony_ci */
4378c2ecf20Sopenharmony_civoid radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	int r;
4408c2ecf20Sopenharmony_ci	struct radeon_bo *ring_obj;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	mutex_lock(&rdev->ring_lock);
4438c2ecf20Sopenharmony_ci	ring_obj = ring->ring_obj;
4448c2ecf20Sopenharmony_ci	ring->ready = false;
4458c2ecf20Sopenharmony_ci	ring->ring = NULL;
4468c2ecf20Sopenharmony_ci	ring->ring_obj = NULL;
4478c2ecf20Sopenharmony_ci	mutex_unlock(&rdev->ring_lock);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (ring_obj) {
4508c2ecf20Sopenharmony_ci		r = radeon_bo_reserve(ring_obj, false);
4518c2ecf20Sopenharmony_ci		if (likely(r == 0)) {
4528c2ecf20Sopenharmony_ci			radeon_bo_kunmap(ring_obj);
4538c2ecf20Sopenharmony_ci			radeon_bo_unpin(ring_obj);
4548c2ecf20Sopenharmony_ci			radeon_bo_unreserve(ring_obj);
4558c2ecf20Sopenharmony_ci		}
4568c2ecf20Sopenharmony_ci		radeon_bo_unref(&ring_obj);
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci/*
4618c2ecf20Sopenharmony_ci * Debugfs info
4628c2ecf20Sopenharmony_ci */
4638c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic int radeon_debugfs_ring_info(struct seq_file *m, void *data)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	struct drm_info_node *node = (struct drm_info_node *) m->private;
4688c2ecf20Sopenharmony_ci	struct drm_device *dev = node->minor->dev;
4698c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
4708c2ecf20Sopenharmony_ci	int ridx = *(int*)node->info_ent->data;
4718c2ecf20Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ridx];
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	uint32_t rptr, wptr, rptr_next;
4748c2ecf20Sopenharmony_ci	unsigned count, i, j;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	radeon_ring_free_size(rdev, ring);
4778c2ecf20Sopenharmony_ci	count = (ring->ring_size / 4) - ring->ring_free_dw;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	wptr = radeon_ring_get_wptr(rdev, ring);
4808c2ecf20Sopenharmony_ci	seq_printf(m, "wptr: 0x%08x [%5d]\n",
4818c2ecf20Sopenharmony_ci		   wptr, wptr);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	rptr = radeon_ring_get_rptr(rdev, ring);
4848c2ecf20Sopenharmony_ci	seq_printf(m, "rptr: 0x%08x [%5d]\n",
4858c2ecf20Sopenharmony_ci		   rptr, rptr);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	if (ring->rptr_save_reg) {
4888c2ecf20Sopenharmony_ci		rptr_next = RREG32(ring->rptr_save_reg);
4898c2ecf20Sopenharmony_ci		seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n",
4908c2ecf20Sopenharmony_ci			   ring->rptr_save_reg, rptr_next, rptr_next);
4918c2ecf20Sopenharmony_ci	} else
4928c2ecf20Sopenharmony_ci		rptr_next = ~0;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
4958c2ecf20Sopenharmony_ci		   ring->wptr, ring->wptr);
4968c2ecf20Sopenharmony_ci	seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
4978c2ecf20Sopenharmony_ci		   ring->last_semaphore_signal_addr);
4988c2ecf20Sopenharmony_ci	seq_printf(m, "last semaphore wait addr   : 0x%016llx\n",
4998c2ecf20Sopenharmony_ci		   ring->last_semaphore_wait_addr);
5008c2ecf20Sopenharmony_ci	seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
5018c2ecf20Sopenharmony_ci	seq_printf(m, "%u dwords in ring\n", count);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (!ring->ring)
5048c2ecf20Sopenharmony_ci		return 0;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* print 8 dw before current rptr as often it's the last executed
5078c2ecf20Sopenharmony_ci	 * packet that is the root issue
5088c2ecf20Sopenharmony_ci	 */
5098c2ecf20Sopenharmony_ci	i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
5108c2ecf20Sopenharmony_ci	for (j = 0; j <= (count + 32); j++) {
5118c2ecf20Sopenharmony_ci		seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
5128c2ecf20Sopenharmony_ci		if (rptr == i)
5138c2ecf20Sopenharmony_ci			seq_puts(m, " *");
5148c2ecf20Sopenharmony_ci		if (rptr_next == i)
5158c2ecf20Sopenharmony_ci			seq_puts(m, " #");
5168c2ecf20Sopenharmony_ci		seq_puts(m, "\n");
5178c2ecf20Sopenharmony_ci		i = (i + 1) & ring->ptr_mask;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci	return 0;
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
5238c2ecf20Sopenharmony_cistatic int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
5248c2ecf20Sopenharmony_cistatic int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
5258c2ecf20Sopenharmony_cistatic int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
5268c2ecf20Sopenharmony_cistatic int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
5278c2ecf20Sopenharmony_cistatic int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
5288c2ecf20Sopenharmony_cistatic int si_vce1_index = TN_RING_TYPE_VCE1_INDEX;
5298c2ecf20Sopenharmony_cistatic int si_vce2_index = TN_RING_TYPE_VCE2_INDEX;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cistatic struct drm_info_list radeon_debugfs_ring_info_list[] = {
5328c2ecf20Sopenharmony_ci	{"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
5338c2ecf20Sopenharmony_ci	{"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
5348c2ecf20Sopenharmony_ci	{"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
5358c2ecf20Sopenharmony_ci	{"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
5368c2ecf20Sopenharmony_ci	{"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
5378c2ecf20Sopenharmony_ci	{"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
5388c2ecf20Sopenharmony_ci	{"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index},
5398c2ecf20Sopenharmony_ci	{"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index},
5408c2ecf20Sopenharmony_ci};
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci#endif
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistatic int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
5478c2ecf20Sopenharmony_ci	unsigned i;
5488c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
5498c2ecf20Sopenharmony_ci		struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
5508c2ecf20Sopenharmony_ci		int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
5518c2ecf20Sopenharmony_ci		unsigned r;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci		if (&rdev->ring[ridx] != ring)
5548c2ecf20Sopenharmony_ci			continue;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci		r = radeon_debugfs_add_files(rdev, info, 1);
5578c2ecf20Sopenharmony_ci		if (r)
5588c2ecf20Sopenharmony_ci			return r;
5598c2ecf20Sopenharmony_ci	}
5608c2ecf20Sopenharmony_ci#endif
5618c2ecf20Sopenharmony_ci	return 0;
5628c2ecf20Sopenharmony_ci}
563