18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2013 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Authors: Alex Deucher
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "radeon.h"
268c2ecf20Sopenharmony_ci#include "cikd.h"
278c2ecf20Sopenharmony_ci#include "kv_dpm.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciint kv_notify_message_to_smu(struct radeon_device *rdev, u32 id)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	u32 i;
328c2ecf20Sopenharmony_ci	u32 tmp = 0;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	WREG32(SMC_MESSAGE_0, id & SMC_MSG_MASK);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
378c2ecf20Sopenharmony_ci		if ((RREG32(SMC_RESP_0) & SMC_RESP_MASK) != 0)
388c2ecf20Sopenharmony_ci			break;
398c2ecf20Sopenharmony_ci		udelay(1);
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci	tmp = RREG32(SMC_RESP_0) & SMC_RESP_MASK;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (tmp != 1) {
448c2ecf20Sopenharmony_ci		if (tmp == 0xFF)
458c2ecf20Sopenharmony_ci			return -EINVAL;
468c2ecf20Sopenharmony_ci		else if (tmp == 0xFE)
478c2ecf20Sopenharmony_ci			return -EINVAL;
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciint kv_dpm_get_enable_mask(struct radeon_device *rdev, u32 *enable_mask)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	int ret;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	ret = kv_notify_message_to_smu(rdev, PPSMC_MSG_SCLKDPM_GetEnabledMask);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (ret == 0)
608c2ecf20Sopenharmony_ci		*enable_mask = RREG32_SMC(SMC_SYSCON_MSG_ARG_0);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	return ret;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciint kv_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
668c2ecf20Sopenharmony_ci				      PPSMC_Msg msg, u32 parameter)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	WREG32(SMC_MSG_ARG_0, parameter);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return kv_notify_message_to_smu(rdev, msg);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int kv_set_smc_sram_address(struct radeon_device *rdev,
758c2ecf20Sopenharmony_ci				   u32 smc_address, u32 limit)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	if (smc_address & 3)
788c2ecf20Sopenharmony_ci		return -EINVAL;
798c2ecf20Sopenharmony_ci	if ((smc_address + 3) > limit)
808c2ecf20Sopenharmony_ci		return -EINVAL;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	WREG32(SMC_IND_INDEX_0, smc_address);
838c2ecf20Sopenharmony_ci	WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ciint kv_read_smc_sram_dword(struct radeon_device *rdev, u32 smc_address,
898c2ecf20Sopenharmony_ci			   u32 *value, u32 limit)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ret = kv_set_smc_sram_address(rdev, smc_address, limit);
948c2ecf20Sopenharmony_ci	if (ret)
958c2ecf20Sopenharmony_ci		return ret;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	*value = RREG32(SMC_IND_DATA_0);
988c2ecf20Sopenharmony_ci	return 0;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciint kv_smc_dpm_enable(struct radeon_device *rdev, bool enable)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	if (enable)
1048c2ecf20Sopenharmony_ci		return kv_notify_message_to_smu(rdev, PPSMC_MSG_DPM_Enable);
1058c2ecf20Sopenharmony_ci	else
1068c2ecf20Sopenharmony_ci		return kv_notify_message_to_smu(rdev, PPSMC_MSG_DPM_Disable);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciint kv_smc_bapm_enable(struct radeon_device *rdev, bool enable)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	if (enable)
1128c2ecf20Sopenharmony_ci		return kv_notify_message_to_smu(rdev, PPSMC_MSG_EnableBAPM);
1138c2ecf20Sopenharmony_ci	else
1148c2ecf20Sopenharmony_ci		return kv_notify_message_to_smu(rdev, PPSMC_MSG_DisableBAPM);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciint kv_copy_bytes_to_smc(struct radeon_device *rdev,
1188c2ecf20Sopenharmony_ci			 u32 smc_start_address,
1198c2ecf20Sopenharmony_ci			 const u8 *src, u32 byte_count, u32 limit)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	int ret;
1228c2ecf20Sopenharmony_ci	u32 data, original_data, addr, extra_shift, t_byte, count, mask;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if ((smc_start_address + byte_count) > limit)
1258c2ecf20Sopenharmony_ci		return -EINVAL;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	addr = smc_start_address;
1288c2ecf20Sopenharmony_ci	t_byte = addr & 3;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/* RMW for the initial bytes */
1318c2ecf20Sopenharmony_ci	if  (t_byte != 0) {
1328c2ecf20Sopenharmony_ci		addr -= t_byte;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		ret = kv_set_smc_sram_address(rdev, addr, limit);
1358c2ecf20Sopenharmony_ci		if (ret)
1368c2ecf20Sopenharmony_ci			return ret;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		original_data = RREG32(SMC_IND_DATA_0);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		data = 0;
1418c2ecf20Sopenharmony_ci		mask = 0;
1428c2ecf20Sopenharmony_ci		count = 4;
1438c2ecf20Sopenharmony_ci		while (count > 0) {
1448c2ecf20Sopenharmony_ci			if (t_byte > 0) {
1458c2ecf20Sopenharmony_ci				mask = (mask << 8) | 0xff;
1468c2ecf20Sopenharmony_ci				t_byte--;
1478c2ecf20Sopenharmony_ci			} else if (byte_count > 0) {
1488c2ecf20Sopenharmony_ci				data = (data << 8) + *src++;
1498c2ecf20Sopenharmony_ci				byte_count--;
1508c2ecf20Sopenharmony_ci				mask <<= 8;
1518c2ecf20Sopenharmony_ci			} else {
1528c2ecf20Sopenharmony_ci				data <<= 8;
1538c2ecf20Sopenharmony_ci				mask = (mask << 8) | 0xff;
1548c2ecf20Sopenharmony_ci			}
1558c2ecf20Sopenharmony_ci			count--;
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		data |= original_data & mask;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		ret = kv_set_smc_sram_address(rdev, addr, limit);
1618c2ecf20Sopenharmony_ci		if (ret)
1628c2ecf20Sopenharmony_ci			return ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		WREG32(SMC_IND_DATA_0, data);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci		addr += 4;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	while (byte_count >= 4) {
1708c2ecf20Sopenharmony_ci		/* SMC address space is BE */
1718c2ecf20Sopenharmony_ci		data = (src[0] << 24) + (src[1] << 16) + (src[2] << 8) + src[3];
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci		ret = kv_set_smc_sram_address(rdev, addr, limit);
1748c2ecf20Sopenharmony_ci		if (ret)
1758c2ecf20Sopenharmony_ci			return ret;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci		WREG32(SMC_IND_DATA_0, data);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci		src += 4;
1808c2ecf20Sopenharmony_ci		byte_count -= 4;
1818c2ecf20Sopenharmony_ci		addr += 4;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* RMW for the final bytes */
1858c2ecf20Sopenharmony_ci	if (byte_count > 0) {
1868c2ecf20Sopenharmony_ci		data = 0;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci		ret = kv_set_smc_sram_address(rdev, addr, limit);
1898c2ecf20Sopenharmony_ci		if (ret)
1908c2ecf20Sopenharmony_ci			return ret;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci		original_data= RREG32(SMC_IND_DATA_0);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		extra_shift = 8 * (4 - byte_count);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		while (byte_count > 0) {
1978c2ecf20Sopenharmony_ci			/* SMC address space is BE */
1988c2ecf20Sopenharmony_ci			data = (data << 8) + *src++;
1998c2ecf20Sopenharmony_ci			byte_count--;
2008c2ecf20Sopenharmony_ci		}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci		data <<= extra_shift;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		data |= (original_data & ~((~0UL) << extra_shift));
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci		ret = kv_set_smc_sram_address(rdev, addr, limit);
2078c2ecf20Sopenharmony_ci		if (ret)
2088c2ecf20Sopenharmony_ci			return ret;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		WREG32(SMC_IND_DATA_0, data);
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
215