18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2013 Advanced Micro Devices, Inc.
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 * Authors: Christian König <christian.koenig@amd.com>
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/firmware.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "radeon.h"
318c2ecf20Sopenharmony_ci#include "radeon_asic.h"
328c2ecf20Sopenharmony_ci#include "cikd.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define VCE_V2_0_FW_SIZE	(256 * 1024)
358c2ecf20Sopenharmony_ci#define VCE_V2_0_STACK_SIZE	(64 * 1024)
368c2ecf20Sopenharmony_ci#define VCE_V2_0_DATA_SIZE	(23552 * RADEON_MAX_VCE_HANDLES)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic void vce_v2_0_set_sw_cg(struct radeon_device *rdev, bool gated)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	u32 tmp;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (gated) {
438c2ecf20Sopenharmony_ci		tmp = RREG32(VCE_CLOCK_GATING_B);
448c2ecf20Sopenharmony_ci		tmp |= 0xe70000;
458c2ecf20Sopenharmony_ci		WREG32(VCE_CLOCK_GATING_B, tmp);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci		tmp = RREG32(VCE_UENC_CLOCK_GATING);
488c2ecf20Sopenharmony_ci		tmp |= 0xff000000;
498c2ecf20Sopenharmony_ci		WREG32(VCE_UENC_CLOCK_GATING, tmp);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci		tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
528c2ecf20Sopenharmony_ci		tmp &= ~0x3fc;
538c2ecf20Sopenharmony_ci		WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci		WREG32(VCE_CGTT_CLK_OVERRIDE, 0);
568c2ecf20Sopenharmony_ci	} else {
578c2ecf20Sopenharmony_ci		tmp = RREG32(VCE_CLOCK_GATING_B);
588c2ecf20Sopenharmony_ci		tmp |= 0xe7;
598c2ecf20Sopenharmony_ci		tmp &= ~0xe70000;
608c2ecf20Sopenharmony_ci		WREG32(VCE_CLOCK_GATING_B, tmp);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci		tmp = RREG32(VCE_UENC_CLOCK_GATING);
638c2ecf20Sopenharmony_ci		tmp |= 0x1fe000;
648c2ecf20Sopenharmony_ci		tmp &= ~0xff000000;
658c2ecf20Sopenharmony_ci		WREG32(VCE_UENC_CLOCK_GATING, tmp);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci		tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
688c2ecf20Sopenharmony_ci		tmp |= 0x3fc;
698c2ecf20Sopenharmony_ci		WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void vce_v2_0_set_dyn_cg(struct radeon_device *rdev, bool gated)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	u32 orig, tmp;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	tmp = RREG32(VCE_CLOCK_GATING_B);
788c2ecf20Sopenharmony_ci	tmp &= ~0x00060006;
798c2ecf20Sopenharmony_ci	if (gated) {
808c2ecf20Sopenharmony_ci		tmp |= 0xe10000;
818c2ecf20Sopenharmony_ci	} else {
828c2ecf20Sopenharmony_ci		tmp |= 0xe1;
838c2ecf20Sopenharmony_ci		tmp &= ~0xe10000;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci	WREG32(VCE_CLOCK_GATING_B, tmp);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	orig = tmp = RREG32(VCE_UENC_CLOCK_GATING);
888c2ecf20Sopenharmony_ci	tmp &= ~0x1fe000;
898c2ecf20Sopenharmony_ci	tmp &= ~0xff000000;
908c2ecf20Sopenharmony_ci	if (tmp != orig)
918c2ecf20Sopenharmony_ci		WREG32(VCE_UENC_CLOCK_GATING, tmp);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	orig = tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
948c2ecf20Sopenharmony_ci	tmp &= ~0x3fc;
958c2ecf20Sopenharmony_ci	if (tmp != orig)
968c2ecf20Sopenharmony_ci		WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (gated)
998c2ecf20Sopenharmony_ci		WREG32(VCE_CGTT_CLK_OVERRIDE, 0);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic void vce_v2_0_disable_cg(struct radeon_device *rdev)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	WREG32(VCE_CGTT_CLK_OVERRIDE, 7);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/*
1088c2ecf20Sopenharmony_ci * Local variable sw_cg is used for debugging purposes, in case we
1098c2ecf20Sopenharmony_ci * ran into problems with dynamic clock gating. Don't remove it.
1108c2ecf20Sopenharmony_ci */
1118c2ecf20Sopenharmony_civoid vce_v2_0_enable_mgcg(struct radeon_device *rdev, bool enable)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	bool sw_cg = false;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_VCE_MGCG)) {
1168c2ecf20Sopenharmony_ci		if (sw_cg)
1178c2ecf20Sopenharmony_ci			vce_v2_0_set_sw_cg(rdev, true);
1188c2ecf20Sopenharmony_ci		else
1198c2ecf20Sopenharmony_ci			vce_v2_0_set_dyn_cg(rdev, true);
1208c2ecf20Sopenharmony_ci	} else {
1218c2ecf20Sopenharmony_ci		vce_v2_0_disable_cg(rdev);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		if (sw_cg)
1248c2ecf20Sopenharmony_ci			vce_v2_0_set_sw_cg(rdev, false);
1258c2ecf20Sopenharmony_ci		else
1268c2ecf20Sopenharmony_ci			vce_v2_0_set_dyn_cg(rdev, false);
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic void vce_v2_0_init_cg(struct radeon_device *rdev)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	u32 tmp;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	tmp = RREG32(VCE_CLOCK_GATING_A);
1358c2ecf20Sopenharmony_ci	tmp &= ~(CGC_CLK_GATE_DLY_TIMER_MASK | CGC_CLK_GATER_OFF_DLY_TIMER_MASK);
1368c2ecf20Sopenharmony_ci	tmp |= (CGC_CLK_GATE_DLY_TIMER(0) | CGC_CLK_GATER_OFF_DLY_TIMER(4));
1378c2ecf20Sopenharmony_ci	tmp |= CGC_UENC_WAIT_AWAKE;
1388c2ecf20Sopenharmony_ci	WREG32(VCE_CLOCK_GATING_A, tmp);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	tmp = RREG32(VCE_UENC_CLOCK_GATING);
1418c2ecf20Sopenharmony_ci	tmp &= ~(CLOCK_ON_DELAY_MASK | CLOCK_OFF_DELAY_MASK);
1428c2ecf20Sopenharmony_ci	tmp |= (CLOCK_ON_DELAY(0) | CLOCK_OFF_DELAY(4));
1438c2ecf20Sopenharmony_ci	WREG32(VCE_UENC_CLOCK_GATING, tmp);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	tmp = RREG32(VCE_CLOCK_GATING_B);
1468c2ecf20Sopenharmony_ci	tmp |= 0x10;
1478c2ecf20Sopenharmony_ci	tmp &= ~0x100000;
1488c2ecf20Sopenharmony_ci	WREG32(VCE_CLOCK_GATING_B, tmp);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ciunsigned vce_v2_0_bo_size(struct radeon_device *rdev)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	WARN_ON(rdev->vce_fw->size > VCE_V2_0_FW_SIZE);
1548c2ecf20Sopenharmony_ci	return VCE_V2_0_FW_SIZE + VCE_V2_0_STACK_SIZE + VCE_V2_0_DATA_SIZE;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciint vce_v2_0_resume(struct radeon_device *rdev)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	uint64_t addr = rdev->vce.gpu_addr;
1608c2ecf20Sopenharmony_ci	uint32_t size;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	WREG32_P(VCE_CLOCK_GATING_A, 0, ~(1 << 16));
1638c2ecf20Sopenharmony_ci	WREG32_P(VCE_UENC_CLOCK_GATING, 0x1FF000, ~0xFF9FF000);
1648c2ecf20Sopenharmony_ci	WREG32_P(VCE_UENC_REG_CLOCK_GATING, 0x3F, ~0x3F);
1658c2ecf20Sopenharmony_ci	WREG32(VCE_CLOCK_GATING_B, 0xf7);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	WREG32(VCE_LMI_CTRL, 0x00398000);
1688c2ecf20Sopenharmony_ci	WREG32_P(VCE_LMI_CACHE_CTRL, 0x0, ~0x1);
1698c2ecf20Sopenharmony_ci	WREG32(VCE_LMI_SWAP_CNTL, 0);
1708c2ecf20Sopenharmony_ci	WREG32(VCE_LMI_SWAP_CNTL1, 0);
1718c2ecf20Sopenharmony_ci	WREG32(VCE_LMI_VM_CTRL, 0);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	WREG32(VCE_LMI_VCPU_CACHE_40BIT_BAR, addr >> 8);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	addr &= 0xff;
1768c2ecf20Sopenharmony_ci	size = VCE_V2_0_FW_SIZE;
1778c2ecf20Sopenharmony_ci	WREG32(VCE_VCPU_CACHE_OFFSET0, addr & 0x7fffffff);
1788c2ecf20Sopenharmony_ci	WREG32(VCE_VCPU_CACHE_SIZE0, size);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	addr += size;
1818c2ecf20Sopenharmony_ci	size = VCE_V2_0_STACK_SIZE;
1828c2ecf20Sopenharmony_ci	WREG32(VCE_VCPU_CACHE_OFFSET1, addr & 0x7fffffff);
1838c2ecf20Sopenharmony_ci	WREG32(VCE_VCPU_CACHE_SIZE1, size);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	addr += size;
1868c2ecf20Sopenharmony_ci	size = VCE_V2_0_DATA_SIZE;
1878c2ecf20Sopenharmony_ci	WREG32(VCE_VCPU_CACHE_OFFSET2, addr & 0x7fffffff);
1888c2ecf20Sopenharmony_ci	WREG32(VCE_VCPU_CACHE_SIZE2, size);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	WREG32_P(VCE_LMI_CTRL2, 0x0, ~0x100);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	WREG32_P(VCE_SYS_INT_EN, VCE_SYS_INT_TRAP_INTERRUPT_EN,
1938c2ecf20Sopenharmony_ci		 ~VCE_SYS_INT_TRAP_INTERRUPT_EN);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	vce_v2_0_init_cg(rdev);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return 0;
1988c2ecf20Sopenharmony_ci}
199