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 "trinityd.h"
2662306a36Sopenharmony_ci#include "trinity_dpm.h"
2762306a36Sopenharmony_ci#include "ppsmc.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic int trinity_notify_message_to_smu(struct radeon_device *rdev, u32 id)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	int i;
3262306a36Sopenharmony_ci	u32 v = 0;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	WREG32(SMC_MESSAGE_0, id);
3562306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
3662306a36Sopenharmony_ci		if (RREG32(SMC_RESP_0) != 0)
3762306a36Sopenharmony_ci			break;
3862306a36Sopenharmony_ci		udelay(1);
3962306a36Sopenharmony_ci	}
4062306a36Sopenharmony_ci	v = RREG32(SMC_RESP_0);
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	if (v != 1) {
4362306a36Sopenharmony_ci		if (v == 0xFF) {
4462306a36Sopenharmony_ci			DRM_ERROR("SMC failed to handle the message!\n");
4562306a36Sopenharmony_ci			return -EINVAL;
4662306a36Sopenharmony_ci		} else if (v == 0xFE) {
4762306a36Sopenharmony_ci			DRM_ERROR("Unknown SMC message!\n");
4862306a36Sopenharmony_ci			return -EINVAL;
4962306a36Sopenharmony_ci		}
5062306a36Sopenharmony_ci	}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	return 0;
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciint trinity_dpm_bapm_enable(struct radeon_device *rdev, bool enable)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	if (enable)
5862306a36Sopenharmony_ci		return trinity_notify_message_to_smu(rdev, PPSMC_MSG_EnableBAPM);
5962306a36Sopenharmony_ci	else
6062306a36Sopenharmony_ci		return trinity_notify_message_to_smu(rdev, PPSMC_MSG_DisableBAPM);
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciint trinity_dpm_config(struct radeon_device *rdev, bool enable)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	if (enable)
6662306a36Sopenharmony_ci		WREG32_SMC(SMU_SCRATCH0, 1);
6762306a36Sopenharmony_ci	else
6862306a36Sopenharmony_ci		WREG32_SMC(SMU_SCRATCH0, 0);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	return trinity_notify_message_to_smu(rdev, PPSMC_MSG_DPM_Config);
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ciint trinity_dpm_force_state(struct radeon_device *rdev, u32 n)
7462306a36Sopenharmony_ci{
7562306a36Sopenharmony_ci	WREG32_SMC(SMU_SCRATCH0, n);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	return trinity_notify_message_to_smu(rdev, PPSMC_MSG_DPM_ForceState);
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ciint trinity_dpm_n_levels_disabled(struct radeon_device *rdev, u32 n)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	WREG32_SMC(SMU_SCRATCH0, n);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	return trinity_notify_message_to_smu(rdev, PPSMC_MSG_DPM_N_LevelsDisabled);
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciint trinity_uvd_dpm_config(struct radeon_device *rdev)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	return trinity_notify_message_to_smu(rdev, PPSMC_MSG_UVD_DPM_Config);
9062306a36Sopenharmony_ci}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciint trinity_dpm_no_forced_level(struct radeon_device *rdev)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	return trinity_notify_message_to_smu(rdev, PPSMC_MSG_NoForcedLevel);
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciint trinity_dce_enable_voltage_adjustment(struct radeon_device *rdev,
9862306a36Sopenharmony_ci					  bool enable)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	if (enable)
10162306a36Sopenharmony_ci		return trinity_notify_message_to_smu(rdev, PPSMC_MSG_DCE_AllowVoltageAdjustment);
10262306a36Sopenharmony_ci	else
10362306a36Sopenharmony_ci		return trinity_notify_message_to_smu(rdev, PPSMC_MSG_DCE_RemoveVoltageAdjustment);
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciint trinity_gfx_dynamic_mgpg_config(struct radeon_device *rdev)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	return trinity_notify_message_to_smu(rdev, PPSMC_MSG_PG_SIMD_Config);
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_civoid trinity_acquire_mutex(struct radeon_device *rdev)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	int i;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	WREG32(SMC_INT_REQ, 1);
11662306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
11762306a36Sopenharmony_ci		if ((RREG32(SMC_INT_REQ) & 0xffff) == 1)
11862306a36Sopenharmony_ci			break;
11962306a36Sopenharmony_ci		udelay(1);
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_civoid trinity_release_mutex(struct radeon_device *rdev)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	WREG32(SMC_INT_REQ, 0);
12662306a36Sopenharmony_ci}
127