162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2012 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "radeon.h"
2562306a36Sopenharmony_ci#include "sumod.h"
2662306a36Sopenharmony_ci#include "sumo_dpm.h"
2762306a36Sopenharmony_ci#include "ppsmc.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define SUMO_SMU_SERVICE_ROUTINE_PG_INIT        1
3062306a36Sopenharmony_ci#define SUMO_SMU_SERVICE_ROUTINE_ALTVDDNB_NOTIFY  27
3162306a36Sopenharmony_ci#define SUMO_SMU_SERVICE_ROUTINE_GFX_SRV_ID_20  20
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic void sumo_send_msg_to_smu(struct radeon_device *rdev, u32 id)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	u32 gfx_int_req;
3662306a36Sopenharmony_ci	int i;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
3962306a36Sopenharmony_ci		if (RREG32(GFX_INT_STATUS) & INT_DONE)
4062306a36Sopenharmony_ci			break;
4162306a36Sopenharmony_ci		udelay(1);
4262306a36Sopenharmony_ci	}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	gfx_int_req = SERV_INDEX(id) | INT_REQ;
4562306a36Sopenharmony_ci	WREG32(GFX_INT_REQ, gfx_int_req);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
4862306a36Sopenharmony_ci		if (RREG32(GFX_INT_REQ) & INT_REQ)
4962306a36Sopenharmony_ci			break;
5062306a36Sopenharmony_ci		udelay(1);
5162306a36Sopenharmony_ci	}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
5462306a36Sopenharmony_ci		if (RREG32(GFX_INT_STATUS) & INT_ACK)
5562306a36Sopenharmony_ci			break;
5662306a36Sopenharmony_ci		udelay(1);
5762306a36Sopenharmony_ci	}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
6062306a36Sopenharmony_ci		if (RREG32(GFX_INT_STATUS) & INT_DONE)
6162306a36Sopenharmony_ci			break;
6262306a36Sopenharmony_ci		udelay(1);
6362306a36Sopenharmony_ci	}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	gfx_int_req &= ~INT_REQ;
6662306a36Sopenharmony_ci	WREG32(GFX_INT_REQ, gfx_int_req);
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_civoid sumo_initialize_m3_arb(struct radeon_device *rdev)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	struct sumo_power_info *pi = sumo_get_pi(rdev);
7262306a36Sopenharmony_ci	u32 i;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if (!pi->enable_dynamic_m3_arbiter)
7562306a36Sopenharmony_ci		return;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	for (i = 0; i < NUMBER_OF_M3ARB_PARAM_SETS; i++)
7862306a36Sopenharmony_ci		WREG32_RCU(MCU_M3ARB_PARAMS + (i * 4),
7962306a36Sopenharmony_ci			   pi->sys_info.csr_m3_arb_cntl_default[i]);
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	for (; i < NUMBER_OF_M3ARB_PARAM_SETS * 2; i++)
8262306a36Sopenharmony_ci		WREG32_RCU(MCU_M3ARB_PARAMS + (i * 4),
8362306a36Sopenharmony_ci			   pi->sys_info.csr_m3_arb_cntl_uvd[i % NUMBER_OF_M3ARB_PARAM_SETS]);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	for (; i < NUMBER_OF_M3ARB_PARAM_SETS * 3; i++)
8662306a36Sopenharmony_ci		WREG32_RCU(MCU_M3ARB_PARAMS + (i * 4),
8762306a36Sopenharmony_ci			   pi->sys_info.csr_m3_arb_cntl_fs3d[i % NUMBER_OF_M3ARB_PARAM_SETS]);
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic bool sumo_is_alt_vddnb_supported(struct radeon_device *rdev)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	struct sumo_power_info *pi = sumo_get_pi(rdev);
9362306a36Sopenharmony_ci	bool return_code = false;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	if (!pi->enable_alt_vddnb)
9662306a36Sopenharmony_ci		return return_code;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	if ((rdev->family == CHIP_SUMO) || (rdev->family == CHIP_SUMO2)) {
9962306a36Sopenharmony_ci		if (pi->fw_version >= 0x00010C00)
10062306a36Sopenharmony_ci			return_code = true;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	return return_code;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_civoid sumo_smu_notify_alt_vddnb_change(struct radeon_device *rdev,
10762306a36Sopenharmony_ci				      bool powersaving, bool force_nbps1)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	u32 param = 0;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	if (!sumo_is_alt_vddnb_supported(rdev))
11262306a36Sopenharmony_ci		return;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	if (powersaving)
11562306a36Sopenharmony_ci		param |= 1;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	if (force_nbps1)
11862306a36Sopenharmony_ci		param |= 2;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	WREG32_RCU(RCU_ALTVDDNB_NOTIFY, param);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	sumo_send_msg_to_smu(rdev, SUMO_SMU_SERVICE_ROUTINE_ALTVDDNB_NOTIFY);
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_civoid sumo_smu_pg_init(struct radeon_device *rdev)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	sumo_send_msg_to_smu(rdev, SUMO_SMU_SERVICE_ROUTINE_PG_INIT);
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic u32 sumo_power_of_4(u32 unit)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	u32 ret = 1;
13362306a36Sopenharmony_ci	u32 i;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	for (i = 0; i < unit; i++)
13662306a36Sopenharmony_ci		ret *= 4;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	return ret;
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_civoid sumo_enable_boost_timer(struct radeon_device *rdev)
14262306a36Sopenharmony_ci{
14362306a36Sopenharmony_ci	struct sumo_power_info *pi = sumo_get_pi(rdev);
14462306a36Sopenharmony_ci	u32 period, unit, timer_value;
14562306a36Sopenharmony_ci	u32 xclk = radeon_get_xclk(rdev);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	unit = (RREG32_RCU(RCU_LCLK_SCALING_CNTL) & LCLK_SCALING_TIMER_PRESCALER_MASK)
14862306a36Sopenharmony_ci		>> LCLK_SCALING_TIMER_PRESCALER_SHIFT;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	period = 100 * (xclk / 100 / sumo_power_of_4(unit));
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	timer_value = (period << 16) | (unit << 4);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	WREG32_RCU(RCU_GNB_PWR_REP_TIMER_CNTL, timer_value);
15562306a36Sopenharmony_ci	WREG32_RCU(RCU_BOOST_MARGIN, pi->sys_info.sclk_dpm_boost_margin);
15662306a36Sopenharmony_ci	WREG32_RCU(RCU_THROTTLE_MARGIN, pi->sys_info.sclk_dpm_throttle_margin);
15762306a36Sopenharmony_ci	WREG32_RCU(GNB_TDP_LIMIT, pi->sys_info.gnb_tdp_limit);
15862306a36Sopenharmony_ci	WREG32_RCU(RCU_SclkDpmTdpLimitPG, pi->sys_info.sclk_dpm_tdp_limit_pg);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	sumo_send_msg_to_smu(rdev, SUMO_SMU_SERVICE_ROUTINE_GFX_SRV_ID_20);
16162306a36Sopenharmony_ci}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_civoid sumo_set_tdp_limit(struct radeon_device *rdev, u32 index, u32 tdp_limit)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	u32 regoffset = 0;
16662306a36Sopenharmony_ci	u32 shift = 0;
16762306a36Sopenharmony_ci	u32 mask = 0xFFF;
16862306a36Sopenharmony_ci	u32 sclk_dpm_tdp_limit;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	switch (index) {
17162306a36Sopenharmony_ci	case 0:
17262306a36Sopenharmony_ci		regoffset = RCU_SclkDpmTdpLimit01;
17362306a36Sopenharmony_ci		shift = 16;
17462306a36Sopenharmony_ci		break;
17562306a36Sopenharmony_ci	case 1:
17662306a36Sopenharmony_ci		regoffset = RCU_SclkDpmTdpLimit01;
17762306a36Sopenharmony_ci		shift = 0;
17862306a36Sopenharmony_ci		break;
17962306a36Sopenharmony_ci	case 2:
18062306a36Sopenharmony_ci		regoffset = RCU_SclkDpmTdpLimit23;
18162306a36Sopenharmony_ci		shift = 16;
18262306a36Sopenharmony_ci		break;
18362306a36Sopenharmony_ci	case 3:
18462306a36Sopenharmony_ci		regoffset = RCU_SclkDpmTdpLimit23;
18562306a36Sopenharmony_ci		shift = 0;
18662306a36Sopenharmony_ci		break;
18762306a36Sopenharmony_ci	case 4:
18862306a36Sopenharmony_ci		regoffset = RCU_SclkDpmTdpLimit47;
18962306a36Sopenharmony_ci		shift = 16;
19062306a36Sopenharmony_ci		break;
19162306a36Sopenharmony_ci	case 7:
19262306a36Sopenharmony_ci		regoffset = RCU_SclkDpmTdpLimit47;
19362306a36Sopenharmony_ci		shift = 0;
19462306a36Sopenharmony_ci		break;
19562306a36Sopenharmony_ci	default:
19662306a36Sopenharmony_ci		break;
19762306a36Sopenharmony_ci	}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	sclk_dpm_tdp_limit = RREG32_RCU(regoffset);
20062306a36Sopenharmony_ci	sclk_dpm_tdp_limit &= ~(mask << shift);
20162306a36Sopenharmony_ci	sclk_dpm_tdp_limit |= (tdp_limit << shift);
20262306a36Sopenharmony_ci	WREG32_RCU(regoffset, sclk_dpm_tdp_limit);
20362306a36Sopenharmony_ci}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_civoid sumo_boost_state_enable(struct radeon_device *rdev, bool enable)
20662306a36Sopenharmony_ci{
20762306a36Sopenharmony_ci	u32 boost_disable = RREG32_RCU(RCU_GPU_BOOST_DISABLE);
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	boost_disable &= 0xFFFFFFFE;
21062306a36Sopenharmony_ci	boost_disable |= (enable ? 0 : 1);
21162306a36Sopenharmony_ci	WREG32_RCU(RCU_GPU_BOOST_DISABLE, boost_disable);
21262306a36Sopenharmony_ci}
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ciu32 sumo_get_running_fw_version(struct radeon_device *rdev)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	return RREG32_RCU(RCU_FW_VERSION);
21762306a36Sopenharmony_ci}
21862306a36Sopenharmony_ci
219