xref: /dflybsd-src/sys/dev/drm/amd/powerplay/smumgr/polaris10_smumgr.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2015 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev 
24b843c749SSergey Zigachev #include "pp_debug.h"
25b843c749SSergey Zigachev #include "smumgr.h"
26b843c749SSergey Zigachev #include "smu74.h"
27b843c749SSergey Zigachev #include "smu_ucode_xfer_vi.h"
28b843c749SSergey Zigachev #include "polaris10_smumgr.h"
29b843c749SSergey Zigachev #include "smu74_discrete.h"
30b843c749SSergey Zigachev #include "smu/smu_7_1_3_d.h"
31b843c749SSergey Zigachev #include "smu/smu_7_1_3_sh_mask.h"
32b843c749SSergey Zigachev #include "gmc/gmc_8_1_d.h"
33b843c749SSergey Zigachev #include "gmc/gmc_8_1_sh_mask.h"
34b843c749SSergey Zigachev #include "oss/oss_3_0_d.h"
35b843c749SSergey Zigachev #include "gca/gfx_8_0_d.h"
36b843c749SSergey Zigachev #include "bif/bif_5_0_d.h"
37b843c749SSergey Zigachev #include "bif/bif_5_0_sh_mask.h"
38b843c749SSergey Zigachev #include "ppatomctrl.h"
39b843c749SSergey Zigachev #include "cgs_common.h"
40b843c749SSergey Zigachev #include "smu7_ppsmc.h"
41b843c749SSergey Zigachev #include "smu7_smumgr.h"
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev #include "smu7_dyn_defaults.h"
44b843c749SSergey Zigachev 
45b843c749SSergey Zigachev #include "smu7_hwmgr.h"
46b843c749SSergey Zigachev #include "hardwaremanager.h"
47b843c749SSergey Zigachev #include "ppatomctrl.h"
48b843c749SSergey Zigachev #include "atombios.h"
49b843c749SSergey Zigachev #include "pppcielanes.h"
50b843c749SSergey Zigachev 
51b843c749SSergey Zigachev #include "dce/dce_10_0_d.h"
52b843c749SSergey Zigachev #include "dce/dce_10_0_sh_mask.h"
53b843c749SSergey Zigachev 
54b843c749SSergey Zigachev #define POLARIS10_SMC_SIZE 0x20000
55b843c749SSergey Zigachev #define POWERTUNE_DEFAULT_SET_MAX    1
56b843c749SSergey Zigachev #define VDDC_VDDCI_DELTA            200
57b843c749SSergey Zigachev #define MC_CG_ARB_FREQ_F1           0x0b
58b843c749SSergey Zigachev 
59b843c749SSergey Zigachev static const struct polaris10_pt_defaults polaris10_power_tune_data_set_array[POWERTUNE_DEFAULT_SET_MAX] = {
60b843c749SSergey Zigachev 	/* sviLoadLIneEn, SviLoadLineVddC, TDC_VDDC_ThrottleReleaseLimitPerc, TDC_MAWt,
61b843c749SSergey Zigachev 	 * TdcWaterfallCtl, DTEAmbientTempBase, DisplayCac, BAPM_TEMP_GRADIENT */
62b843c749SSergey Zigachev 	{ 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0xB0000,
63b843c749SSergey Zigachev 	{ 0x79, 0x253, 0x25D, 0xAE, 0x72, 0x80, 0x83, 0x86, 0x6F, 0xC8, 0xC9, 0xC9, 0x2F, 0x4D, 0x61},
64b843c749SSergey Zigachev 	{ 0x17C, 0x172, 0x180, 0x1BC, 0x1B3, 0x1BD, 0x206, 0x200, 0x203, 0x25D, 0x25A, 0x255, 0x2C3, 0x2C5, 0x2B4 } },
65b843c749SSergey Zigachev };
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev static const sclkFcwRange_t Range_Table[NUM_SCLK_RANGE] = {
68b843c749SSergey Zigachev 			{VCO_2_4, POSTDIV_DIV_BY_16,  75, 160, 112},
69b843c749SSergey Zigachev 			{VCO_3_6, POSTDIV_DIV_BY_16, 112, 224, 160},
70b843c749SSergey Zigachev 			{VCO_2_4, POSTDIV_DIV_BY_8,   75, 160, 112},
71b843c749SSergey Zigachev 			{VCO_3_6, POSTDIV_DIV_BY_8,  112, 224, 160},
72b843c749SSergey Zigachev 			{VCO_2_4, POSTDIV_DIV_BY_4,   75, 160, 112},
73b843c749SSergey Zigachev 			{VCO_3_6, POSTDIV_DIV_BY_4,  112, 216, 160},
74b843c749SSergey Zigachev 			{VCO_2_4, POSTDIV_DIV_BY_2,   75, 160, 108},
75b843c749SSergey Zigachev 			{VCO_3_6, POSTDIV_DIV_BY_2,  112, 216, 160} };
76b843c749SSergey Zigachev 
77b843c749SSergey Zigachev #define PPPOLARIS10_TARGETACTIVITY_DFLT                     50
78b843c749SSergey Zigachev 
79b843c749SSergey Zigachev static const SMU74_Discrete_GraphicsLevel avfs_graphics_level_polaris10[8] = {
80b843c749SSergey Zigachev 	/*  Min      pcie   DeepSleep Activity  CgSpll      CgSpll    CcPwr  CcPwr  Sclk         Enabled      Enabled                       Voltage    Power */
81b843c749SSergey Zigachev 	/* Voltage, DpmLevel, DivId,  Level,  FuncCntl3,  FuncCntl4,  DynRm, DynRm1 Did, Padding,ForActivity, ForThrottle, UpHyst, DownHyst, DownHyst, Throttle */
82b843c749SSergey Zigachev 	{ 0x100ea446, 0x00, 0x03, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0x30750000, 0x3000, 0, 0x2600, 0, 0, 0x0004, 0x8f02, 0xffff, 0x2f00, 0x300e, 0x2700 } },
83b843c749SSergey Zigachev 	{ 0x400ea446, 0x01, 0x04, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0x409c0000, 0x2000, 0, 0x1e00, 1, 1, 0x0004, 0x8300, 0xffff, 0x1f00, 0xcb5e, 0x1a00 } },
84b843c749SSergey Zigachev 	{ 0x740ea446, 0x01, 0x00, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0x50c30000, 0x2800, 0, 0x2000, 1, 1, 0x0004, 0x0c02, 0xffff, 0x2700, 0x6433, 0x2100 } },
85b843c749SSergey Zigachev 	{ 0xa40ea446, 0x01, 0x00, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0x60ea0000, 0x3000, 0, 0x2600, 1, 1, 0x0004, 0x8f02, 0xffff, 0x2f00, 0x300e, 0x2700 } },
86b843c749SSergey Zigachev 	{ 0xd80ea446, 0x01, 0x00, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0x70110100, 0x3800, 0, 0x2c00, 1, 1, 0x0004, 0x1203, 0xffff, 0x3600, 0xc9e2, 0x2e00 } },
87b843c749SSergey Zigachev 	{ 0x3c0fa446, 0x01, 0x00, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0x80380100, 0x2000, 0, 0x1e00, 2, 1, 0x0004, 0x8300, 0xffff, 0x1f00, 0xcb5e, 0x1a00 } },
88b843c749SSergey Zigachev 	{ 0x6c0fa446, 0x01, 0x00, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0x905f0100, 0x2400, 0, 0x1e00, 2, 1, 0x0004, 0x8901, 0xffff, 0x2300, 0x314c, 0x1d00 } },
89b843c749SSergey Zigachev 	{ 0xa00fa446, 0x01, 0x00, 0x3200, 0, 0, 0, 0, 0, 0, 0x01, 0x01, 0x0a, 0x00, 0x00, 0x00, { 0xa0860100, 0x2800, 0, 0x2000, 2, 1, 0x0004, 0x0c02, 0xffff, 0x2700, 0x6433, 0x2100 } }
90b843c749SSergey Zigachev };
91b843c749SSergey Zigachev 
92b843c749SSergey Zigachev static const SMU74_Discrete_MemoryLevel avfs_memory_level_polaris10 = {
93b843c749SSergey Zigachev 	0x100ea446, 0, 0x30750000, 0x01, 0x01, 0x01, 0x00, 0x00, 0x64, 0x00, 0x00, 0x1f00, 0x00, 0x00};
94b843c749SSergey Zigachev 
polaris10_perform_btc(struct pp_hwmgr * hwmgr)95b843c749SSergey Zigachev static int polaris10_perform_btc(struct pp_hwmgr *hwmgr)
96b843c749SSergey Zigachev {
97b843c749SSergey Zigachev 	int result = 0;
98b843c749SSergey Zigachev 	struct smu7_smumgr *smu_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
99b843c749SSergey Zigachev 
100b843c749SSergey Zigachev 	if (0 != smu_data->avfs_btc_param) {
101b843c749SSergey Zigachev 		if (0 != smu7_send_msg_to_smc_with_parameter(hwmgr, PPSMC_MSG_PerformBtc, smu_data->avfs_btc_param)) {
102b843c749SSergey Zigachev 			pr_info("[AVFS][SmuPolaris10_PerformBtc] PerformBTC SMU msg failed");
103b843c749SSergey Zigachev 			result = -1;
104b843c749SSergey Zigachev 		}
105b843c749SSergey Zigachev 	}
106b843c749SSergey Zigachev 	if (smu_data->avfs_btc_param > 1) {
107b843c749SSergey Zigachev 		/* Soft-Reset to reset the engine before loading uCode */
108b843c749SSergey Zigachev 		/* halt */
109b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmCP_MEC_CNTL, 0x50000000);
110b843c749SSergey Zigachev 		/* reset everything */
111b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmGRBM_SOFT_RESET, 0xffffffff);
112b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmGRBM_SOFT_RESET, 0);
113b843c749SSergey Zigachev 	}
114b843c749SSergey Zigachev 	return result;
115b843c749SSergey Zigachev }
116b843c749SSergey Zigachev 
117b843c749SSergey Zigachev 
polaris10_setup_graphics_level_structure(struct pp_hwmgr * hwmgr)118b843c749SSergey Zigachev static int polaris10_setup_graphics_level_structure(struct pp_hwmgr *hwmgr)
119b843c749SSergey Zigachev {
120b843c749SSergey Zigachev 	uint32_t vr_config;
121b843c749SSergey Zigachev 	uint32_t dpm_table_start;
122b843c749SSergey Zigachev 
123b843c749SSergey Zigachev 	uint16_t u16_boot_mvdd;
124b843c749SSergey Zigachev 	uint32_t graphics_level_address, vr_config_address, graphics_level_size;
125b843c749SSergey Zigachev 
126b843c749SSergey Zigachev 	graphics_level_size = sizeof(avfs_graphics_level_polaris10);
127b843c749SSergey Zigachev 	u16_boot_mvdd = PP_HOST_TO_SMC_US(1300 * VOLTAGE_SCALE);
128b843c749SSergey Zigachev 
129b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_read_smc_sram_dword(hwmgr,
130b843c749SSergey Zigachev 				SMU7_FIRMWARE_HEADER_LOCATION + offsetof(SMU74_Firmware_Header, DpmTable),
131b843c749SSergey Zigachev 				&dpm_table_start, 0x40000),
132b843c749SSergey Zigachev 			"[AVFS][Polaris10_SetupGfxLvlStruct] SMU could not communicate starting address of DPM table",
133b843c749SSergey Zigachev 			return -1);
134b843c749SSergey Zigachev 
135b843c749SSergey Zigachev 	/*  Default value for VRConfig = VR_MERGED_WITH_VDDC + VR_STATIC_VOLTAGE(VDDCI) */
136b843c749SSergey Zigachev 	vr_config = 0x01000500; /* Real value:0x50001 */
137b843c749SSergey Zigachev 
138b843c749SSergey Zigachev 	vr_config_address = dpm_table_start + offsetof(SMU74_Discrete_DpmTable, VRConfig);
139b843c749SSergey Zigachev 
140b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, vr_config_address,
141b843c749SSergey Zigachev 				(uint8_t *)&vr_config, sizeof(uint32_t), 0x40000),
142b843c749SSergey Zigachev 			"[AVFS][Polaris10_SetupGfxLvlStruct] Problems copying VRConfig value over to SMC",
143b843c749SSergey Zigachev 			return -1);
144b843c749SSergey Zigachev 
145b843c749SSergey Zigachev 	graphics_level_address = dpm_table_start + offsetof(SMU74_Discrete_DpmTable, GraphicsLevel);
146b843c749SSergey Zigachev 
147b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, graphics_level_address,
148b843c749SSergey Zigachev 				(uint8_t *)(&avfs_graphics_level_polaris10),
149b843c749SSergey Zigachev 				graphics_level_size, 0x40000),
150b843c749SSergey Zigachev 			"[AVFS][Polaris10_SetupGfxLvlStruct] Copying of SCLK DPM table failed!",
151b843c749SSergey Zigachev 			return -1);
152b843c749SSergey Zigachev 
153b843c749SSergey Zigachev 	graphics_level_address = dpm_table_start + offsetof(SMU74_Discrete_DpmTable, MemoryLevel);
154b843c749SSergey Zigachev 
155b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, graphics_level_address,
156b843c749SSergey Zigachev 				(uint8_t *)(&avfs_memory_level_polaris10), sizeof(avfs_memory_level_polaris10), 0x40000),
157b843c749SSergey Zigachev 				"[AVFS][Polaris10_SetupGfxLvlStruct] Copying of MCLK DPM table failed!",
158b843c749SSergey Zigachev 			return -1);
159b843c749SSergey Zigachev 
160b843c749SSergey Zigachev 	/* MVDD Boot value - neccessary for getting rid of the hang that occurs during Mclk DPM enablement */
161b843c749SSergey Zigachev 
162b843c749SSergey Zigachev 	graphics_level_address = dpm_table_start + offsetof(SMU74_Discrete_DpmTable, BootMVdd);
163b843c749SSergey Zigachev 
164b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, graphics_level_address,
165b843c749SSergey Zigachev 			(uint8_t *)(&u16_boot_mvdd), sizeof(u16_boot_mvdd), 0x40000),
166b843c749SSergey Zigachev 			"[AVFS][Polaris10_SetupGfxLvlStruct] Copying of DPM table failed!",
167b843c749SSergey Zigachev 			return -1);
168b843c749SSergey Zigachev 
169b843c749SSergey Zigachev 	return 0;
170b843c749SSergey Zigachev }
171b843c749SSergey Zigachev 
172b843c749SSergey Zigachev 
polaris10_avfs_event_mgr(struct pp_hwmgr * hwmgr)173b843c749SSergey Zigachev static int polaris10_avfs_event_mgr(struct pp_hwmgr *hwmgr)
174b843c749SSergey Zigachev {
175b843c749SSergey Zigachev 	struct smu7_smumgr *smu_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
176b843c749SSergey Zigachev 
177b843c749SSergey Zigachev 	if (!hwmgr->avfs_supported)
178b843c749SSergey Zigachev 		return 0;
179b843c749SSergey Zigachev 
180b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == polaris10_setup_graphics_level_structure(hwmgr),
181b843c749SSergey Zigachev 		"[AVFS][Polaris10_AVFSEventMgr] Could not Copy Graphics Level table over to SMU",
182b843c749SSergey Zigachev 		return -EINVAL);
183b843c749SSergey Zigachev 
184b843c749SSergey Zigachev 	if (smu_data->avfs_btc_param > 1) {
185b843c749SSergey Zigachev 		pr_info("[AVFS][Polaris10_AVFSEventMgr] AC BTC has not been successfully verified on Fiji. There may be in this setting.");
186b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == smu7_setup_pwr_virus(hwmgr),
187b843c749SSergey Zigachev 		"[AVFS][Polaris10_AVFSEventMgr] Could not setup Pwr Virus for AVFS ",
188b843c749SSergey Zigachev 		return -EINVAL);
189b843c749SSergey Zigachev 	}
190b843c749SSergey Zigachev 
191b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == polaris10_perform_btc(hwmgr),
192b843c749SSergey Zigachev 				"[AVFS][Polaris10_AVFSEventMgr] Failure at SmuPolaris10_PerformBTC. AVFS Disabled",
193b843c749SSergey Zigachev 			 return -EINVAL);
194b843c749SSergey Zigachev 
195b843c749SSergey Zigachev 	return 0;
196b843c749SSergey Zigachev }
197b843c749SSergey Zigachev 
polaris10_start_smu_in_protection_mode(struct pp_hwmgr * hwmgr)198b843c749SSergey Zigachev static int polaris10_start_smu_in_protection_mode(struct pp_hwmgr *hwmgr)
199b843c749SSergey Zigachev {
200b843c749SSergey Zigachev 	int result = 0;
201b843c749SSergey Zigachev 
202b843c749SSergey Zigachev 	/* Wait for smc boot up */
203b843c749SSergey Zigachev 	/* PHM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL(smumgr, SMC_IND, RCU_UC_EVENTS, boot_seq_done, 0) */
204b843c749SSergey Zigachev 
205b843c749SSergey Zigachev 	/* Assert reset */
206b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
207b843c749SSergey Zigachev 					SMC_SYSCON_RESET_CNTL, rst_reg, 1);
208b843c749SSergey Zigachev 
209b843c749SSergey Zigachev 	result = smu7_upload_smu_firmware_image(hwmgr);
210b843c749SSergey Zigachev 	if (result != 0)
211b843c749SSergey Zigachev 		return result;
212b843c749SSergey Zigachev 
213b843c749SSergey Zigachev 	/* Clear status */
214b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixSMU_STATUS, 0);
215b843c749SSergey Zigachev 
216b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
217b843c749SSergey Zigachev 					SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
218b843c749SSergey Zigachev 
219b843c749SSergey Zigachev 	/* De-assert reset */
220b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
221b843c749SSergey Zigachev 					SMC_SYSCON_RESET_CNTL, rst_reg, 0);
222b843c749SSergey Zigachev 
223b843c749SSergey Zigachev 
224b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND, RCU_UC_EVENTS, INTERRUPTS_ENABLED, 1);
225b843c749SSergey Zigachev 
226b843c749SSergey Zigachev 
227b843c749SSergey Zigachev 	/* Call Test SMU message with 0x20000 offset to trigger SMU start */
228b843c749SSergey Zigachev 	smu7_send_msg_to_smc_offset(hwmgr);
229b843c749SSergey Zigachev 
230b843c749SSergey Zigachev 	/* Wait done bit to be set */
231b843c749SSergey Zigachev 	/* Check pass/failed indicator */
232b843c749SSergey Zigachev 
233b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND, SMU_STATUS, SMU_DONE, 0);
234b843c749SSergey Zigachev 
235b843c749SSergey Zigachev 	if (1 != PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
236b843c749SSergey Zigachev 						SMU_STATUS, SMU_PASS))
237b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false, "SMU Firmware start failed!", return -1);
238b843c749SSergey Zigachev 
239b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixFIRMWARE_FLAGS, 0);
240b843c749SSergey Zigachev 
241b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
242b843c749SSergey Zigachev 					SMC_SYSCON_RESET_CNTL, rst_reg, 1);
243b843c749SSergey Zigachev 
244b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
245b843c749SSergey Zigachev 					SMC_SYSCON_RESET_CNTL, rst_reg, 0);
246b843c749SSergey Zigachev 
247b843c749SSergey Zigachev 	/* Wait for firmware to initialize */
248b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND, FIRMWARE_FLAGS, INTERRUPTS_ENABLED, 1);
249b843c749SSergey Zigachev 
250b843c749SSergey Zigachev 	return result;
251b843c749SSergey Zigachev }
252b843c749SSergey Zigachev 
polaris10_start_smu_in_non_protection_mode(struct pp_hwmgr * hwmgr)253b843c749SSergey Zigachev static int polaris10_start_smu_in_non_protection_mode(struct pp_hwmgr *hwmgr)
254b843c749SSergey Zigachev {
255b843c749SSergey Zigachev 	int result = 0;
256b843c749SSergey Zigachev 
257b843c749SSergey Zigachev 	/* wait for smc boot up */
258b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND, RCU_UC_EVENTS, boot_seq_done, 0);
259b843c749SSergey Zigachev 
260b843c749SSergey Zigachev 	/* Clear firmware interrupt enable flag */
261b843c749SSergey Zigachev 	/* PHM_WRITE_VFPF_INDIRECT_FIELD(pSmuMgr, SMC_IND, SMC_SYSCON_MISC_CNTL, pre_fetcher_en, 1); */
262b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
263b843c749SSergey Zigachev 				ixFIRMWARE_FLAGS, 0);
264b843c749SSergey Zigachev 
265b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
266b843c749SSergey Zigachev 					SMC_SYSCON_RESET_CNTL,
267b843c749SSergey Zigachev 					rst_reg, 1);
268b843c749SSergey Zigachev 
269b843c749SSergey Zigachev 	result = smu7_upload_smu_firmware_image(hwmgr);
270b843c749SSergey Zigachev 	if (result != 0)
271b843c749SSergey Zigachev 		return result;
272b843c749SSergey Zigachev 
273b843c749SSergey Zigachev 	/* Set smc instruct start point at 0x0 */
274b843c749SSergey Zigachev 	smu7_program_jump_on_start(hwmgr);
275b843c749SSergey Zigachev 
276b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
277b843c749SSergey Zigachev 					SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
278b843c749SSergey Zigachev 
279b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
280b843c749SSergey Zigachev 					SMC_SYSCON_RESET_CNTL, rst_reg, 0);
281b843c749SSergey Zigachev 
282b843c749SSergey Zigachev 	/* Wait for firmware to initialize */
283b843c749SSergey Zigachev 
284b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND,
285b843c749SSergey Zigachev 					FIRMWARE_FLAGS, INTERRUPTS_ENABLED, 1);
286b843c749SSergey Zigachev 
287b843c749SSergey Zigachev 	return result;
288b843c749SSergey Zigachev }
289b843c749SSergey Zigachev 
polaris10_start_smu(struct pp_hwmgr * hwmgr)290b843c749SSergey Zigachev static int polaris10_start_smu(struct pp_hwmgr *hwmgr)
291b843c749SSergey Zigachev {
292b843c749SSergey Zigachev 	int result = 0;
293b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
294b843c749SSergey Zigachev 
295b843c749SSergey Zigachev 	/* Only start SMC if SMC RAM is not running */
296b843c749SSergey Zigachev 	if (!smu7_is_smc_ram_running(hwmgr) && hwmgr->not_vf) {
297b843c749SSergey Zigachev 		smu_data->protected_mode = (uint8_t) (PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SMU_FIRMWARE, SMU_MODE));
298b843c749SSergey Zigachev 		smu_data->smu7_data.security_hard_key = (uint8_t) (PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SMU_FIRMWARE, SMU_SEL));
299b843c749SSergey Zigachev 
300b843c749SSergey Zigachev 		/* Check if SMU is running in protected mode */
301b843c749SSergey Zigachev 		if (smu_data->protected_mode == 0)
302b843c749SSergey Zigachev 			result = polaris10_start_smu_in_non_protection_mode(hwmgr);
303b843c749SSergey Zigachev 		else
304b843c749SSergey Zigachev 			result = polaris10_start_smu_in_protection_mode(hwmgr);
305b843c749SSergey Zigachev 
306b843c749SSergey Zigachev 		if (result != 0)
307b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(0, "Failed to load SMU ucode.", return result);
308b843c749SSergey Zigachev 
309b843c749SSergey Zigachev 		polaris10_avfs_event_mgr(hwmgr);
310b843c749SSergey Zigachev 	}
311b843c749SSergey Zigachev 
312b843c749SSergey Zigachev 	/* Setup SoftRegsStart here for register lookup in case DummyBackEnd is used and ProcessFirmwareHeader is not executed */
313b843c749SSergey Zigachev 	smu7_read_smc_sram_dword(hwmgr, SMU7_FIRMWARE_HEADER_LOCATION + offsetof(SMU74_Firmware_Header, SoftRegisters),
314b843c749SSergey Zigachev 					&(smu_data->smu7_data.soft_regs_start), 0x40000);
315b843c749SSergey Zigachev 
316b843c749SSergey Zigachev 	result = smu7_request_smu_load_fw(hwmgr);
317b843c749SSergey Zigachev 
318b843c749SSergey Zigachev 	return result;
319b843c749SSergey Zigachev }
320b843c749SSergey Zigachev 
polaris10_is_hw_avfs_present(struct pp_hwmgr * hwmgr)321b843c749SSergey Zigachev static bool polaris10_is_hw_avfs_present(struct pp_hwmgr *hwmgr)
322b843c749SSergey Zigachev {
323b843c749SSergey Zigachev 	uint32_t efuse;
324b843c749SSergey Zigachev 
325b843c749SSergey Zigachev 	efuse = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixSMU_EFUSE_0 + (49*4));
326b843c749SSergey Zigachev 	efuse &= 0x00000001;
327b843c749SSergey Zigachev 	if (efuse)
328b843c749SSergey Zigachev 		return true;
329b843c749SSergey Zigachev 
330b843c749SSergey Zigachev 	return false;
331b843c749SSergey Zigachev }
332b843c749SSergey Zigachev 
polaris10_smu_init(struct pp_hwmgr * hwmgr)333b843c749SSergey Zigachev static int polaris10_smu_init(struct pp_hwmgr *hwmgr)
334b843c749SSergey Zigachev {
335b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data;
336b843c749SSergey Zigachev 
337b843c749SSergey Zigachev 	smu_data = kzalloc(sizeof(struct polaris10_smumgr), GFP_KERNEL);
338b843c749SSergey Zigachev 	if (smu_data == NULL)
339b843c749SSergey Zigachev 		return -ENOMEM;
340b843c749SSergey Zigachev 
341b843c749SSergey Zigachev 	hwmgr->smu_backend = smu_data;
342b843c749SSergey Zigachev 
343b843c749SSergey Zigachev 	if (smu7_init(hwmgr)) {
344b843c749SSergey Zigachev 		kfree(smu_data);
345b843c749SSergey Zigachev 		return -EINVAL;
346b843c749SSergey Zigachev 	}
347b843c749SSergey Zigachev 
348b843c749SSergey Zigachev 	return 0;
349b843c749SSergey Zigachev }
350b843c749SSergey Zigachev 
polaris10_get_dependency_volt_by_clk(struct pp_hwmgr * hwmgr,struct phm_ppt_v1_clock_voltage_dependency_table * dep_table,uint32_t clock,SMU_VoltageLevel * voltage,uint32_t * mvdd)351b843c749SSergey Zigachev static int polaris10_get_dependency_volt_by_clk(struct pp_hwmgr *hwmgr,
352b843c749SSergey Zigachev 		struct phm_ppt_v1_clock_voltage_dependency_table *dep_table,
353b843c749SSergey Zigachev 		uint32_t clock, SMU_VoltageLevel *voltage, uint32_t *mvdd)
354b843c749SSergey Zigachev {
355b843c749SSergey Zigachev 	uint32_t i;
356b843c749SSergey Zigachev 	uint16_t vddci;
357b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
358b843c749SSergey Zigachev 
359b843c749SSergey Zigachev 	*voltage = *mvdd = 0;
360b843c749SSergey Zigachev 
361b843c749SSergey Zigachev 	/* clock - voltage dependency table is empty table */
362b843c749SSergey Zigachev 	if (dep_table->count == 0)
363b843c749SSergey Zigachev 		return -EINVAL;
364b843c749SSergey Zigachev 
365b843c749SSergey Zigachev 	for (i = 0; i < dep_table->count; i++) {
366b843c749SSergey Zigachev 		/* find first sclk bigger than request */
367b843c749SSergey Zigachev 		if (dep_table->entries[i].clk >= clock) {
368b843c749SSergey Zigachev 			*voltage |= (dep_table->entries[i].vddc *
369b843c749SSergey Zigachev 					VOLTAGE_SCALE) << VDDC_SHIFT;
370b843c749SSergey Zigachev 			if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
371b843c749SSergey Zigachev 				*voltage |= (data->vbios_boot_state.vddci_bootup_value *
372b843c749SSergey Zigachev 						VOLTAGE_SCALE) << VDDCI_SHIFT;
373b843c749SSergey Zigachev 			else if (dep_table->entries[i].vddci)
374b843c749SSergey Zigachev 				*voltage |= (dep_table->entries[i].vddci *
375b843c749SSergey Zigachev 						VOLTAGE_SCALE) << VDDCI_SHIFT;
376b843c749SSergey Zigachev 			else {
377b843c749SSergey Zigachev 				vddci = phm_find_closest_vddci(&(data->vddci_voltage_table),
378b843c749SSergey Zigachev 						(dep_table->entries[i].vddc -
379b843c749SSergey Zigachev 								(uint16_t)VDDC_VDDCI_DELTA));
380b843c749SSergey Zigachev 				*voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
381b843c749SSergey Zigachev 			}
382b843c749SSergey Zigachev 
383b843c749SSergey Zigachev 			if (SMU7_VOLTAGE_CONTROL_NONE == data->mvdd_control)
384b843c749SSergey Zigachev 				*mvdd = data->vbios_boot_state.mvdd_bootup_value *
385b843c749SSergey Zigachev 					VOLTAGE_SCALE;
386b843c749SSergey Zigachev 			else if (dep_table->entries[i].mvdd)
387b843c749SSergey Zigachev 				*mvdd = (uint32_t) dep_table->entries[i].mvdd *
388b843c749SSergey Zigachev 					VOLTAGE_SCALE;
389b843c749SSergey Zigachev 
390b843c749SSergey Zigachev 			*voltage |= 1 << PHASES_SHIFT;
391b843c749SSergey Zigachev 			return 0;
392b843c749SSergey Zigachev 		}
393b843c749SSergey Zigachev 	}
394b843c749SSergey Zigachev 
395b843c749SSergey Zigachev 	/* sclk is bigger than max sclk in the dependence table */
396b843c749SSergey Zigachev 	*voltage |= (dep_table->entries[i - 1].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
397b843c749SSergey Zigachev 
398b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
399b843c749SSergey Zigachev 		*voltage |= (data->vbios_boot_state.vddci_bootup_value *
400b843c749SSergey Zigachev 				VOLTAGE_SCALE) << VDDCI_SHIFT;
401b843c749SSergey Zigachev 	else if (dep_table->entries[i-1].vddci) {
402b843c749SSergey Zigachev 		vddci = phm_find_closest_vddci(&(data->vddci_voltage_table),
403b843c749SSergey Zigachev 				(dep_table->entries[i].vddc -
404b843c749SSergey Zigachev 						(uint16_t)VDDC_VDDCI_DELTA));
405b843c749SSergey Zigachev 		*voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
406b843c749SSergey Zigachev 	}
407b843c749SSergey Zigachev 
408b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE == data->mvdd_control)
409b843c749SSergey Zigachev 		*mvdd = data->vbios_boot_state.mvdd_bootup_value * VOLTAGE_SCALE;
410b843c749SSergey Zigachev 	else if (dep_table->entries[i].mvdd)
411b843c749SSergey Zigachev 		*mvdd = (uint32_t) dep_table->entries[i - 1].mvdd * VOLTAGE_SCALE;
412b843c749SSergey Zigachev 
413b843c749SSergey Zigachev 	return 0;
414b843c749SSergey Zigachev }
415b843c749SSergey Zigachev 
scale_fan_gain_settings(uint16_t raw_setting)416b843c749SSergey Zigachev static uint16_t scale_fan_gain_settings(uint16_t raw_setting)
417b843c749SSergey Zigachev {
418b843c749SSergey Zigachev 	uint32_t tmp;
419b843c749SSergey Zigachev 	tmp = raw_setting * 4096 / 100;
420b843c749SSergey Zigachev 	return (uint16_t)tmp;
421b843c749SSergey Zigachev }
422b843c749SSergey Zigachev 
polaris10_populate_bapm_parameters_in_dpm_table(struct pp_hwmgr * hwmgr)423b843c749SSergey Zigachev static int polaris10_populate_bapm_parameters_in_dpm_table(struct pp_hwmgr *hwmgr)
424b843c749SSergey Zigachev {
425b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
426b843c749SSergey Zigachev 
427b843c749SSergey Zigachev 	const struct polaris10_pt_defaults *defaults = smu_data->power_tune_defaults;
428b843c749SSergey Zigachev 	SMU74_Discrete_DpmTable  *table = &(smu_data->smc_state_table);
429b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
430b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
431b843c749SSergey Zigachev 	struct phm_cac_tdp_table *cac_dtp_table = table_info->cac_dtp_table;
432b843c749SSergey Zigachev 	struct pp_advance_fan_control_parameters *fan_table =
433b843c749SSergey Zigachev 			&hwmgr->thermal_controller.advanceFanControlParameters;
434b843c749SSergey Zigachev 	int i, j, k;
435b843c749SSergey Zigachev 	const uint16_t *pdef1;
436b843c749SSergey Zigachev 	const uint16_t *pdef2;
437b843c749SSergey Zigachev 
438b843c749SSergey Zigachev 	table->DefaultTdp = PP_HOST_TO_SMC_US((uint16_t)(cac_dtp_table->usTDP * 128));
439b843c749SSergey Zigachev 	table->TargetTdp  = PP_HOST_TO_SMC_US((uint16_t)(cac_dtp_table->usTDP * 128));
440b843c749SSergey Zigachev 
441b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(cac_dtp_table->usTargetOperatingTemp <= 255,
442b843c749SSergey Zigachev 				"Target Operating Temp is out of Range!",
443b843c749SSergey Zigachev 				);
444b843c749SSergey Zigachev 
445b843c749SSergey Zigachev 	table->TemperatureLimitEdge = PP_HOST_TO_SMC_US(
446b843c749SSergey Zigachev 			cac_dtp_table->usTargetOperatingTemp * 256);
447b843c749SSergey Zigachev 	table->TemperatureLimitHotspot = PP_HOST_TO_SMC_US(
448b843c749SSergey Zigachev 			cac_dtp_table->usTemperatureLimitHotspot * 256);
449b843c749SSergey Zigachev 	table->FanGainEdge = PP_HOST_TO_SMC_US(
450b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainEdge));
451b843c749SSergey Zigachev 	table->FanGainHotspot = PP_HOST_TO_SMC_US(
452b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainHotspot));
453b843c749SSergey Zigachev 
454b843c749SSergey Zigachev 	pdef1 = defaults->BAPMTI_R;
455b843c749SSergey Zigachev 	pdef2 = defaults->BAPMTI_RC;
456b843c749SSergey Zigachev 
457b843c749SSergey Zigachev 	for (i = 0; i < SMU74_DTE_ITERATIONS; i++) {
458b843c749SSergey Zigachev 		for (j = 0; j < SMU74_DTE_SOURCES; j++) {
459b843c749SSergey Zigachev 			for (k = 0; k < SMU74_DTE_SINKS; k++) {
460b843c749SSergey Zigachev 				table->BAPMTI_R[i][j][k] = PP_HOST_TO_SMC_US(*pdef1);
461b843c749SSergey Zigachev 				table->BAPMTI_RC[i][j][k] = PP_HOST_TO_SMC_US(*pdef2);
462b843c749SSergey Zigachev 				pdef1++;
463b843c749SSergey Zigachev 				pdef2++;
464b843c749SSergey Zigachev 			}
465b843c749SSergey Zigachev 		}
466b843c749SSergey Zigachev 	}
467b843c749SSergey Zigachev 
468b843c749SSergey Zigachev 	return 0;
469b843c749SSergey Zigachev }
470b843c749SSergey Zigachev 
polaris10_populate_svi_load_line(struct pp_hwmgr * hwmgr)471b843c749SSergey Zigachev static int polaris10_populate_svi_load_line(struct pp_hwmgr *hwmgr)
472b843c749SSergey Zigachev {
473b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
474b843c749SSergey Zigachev 	const struct polaris10_pt_defaults *defaults = smu_data->power_tune_defaults;
475b843c749SSergey Zigachev 
476b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineEn = defaults->SviLoadLineEn;
477b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineVddC = defaults->SviLoadLineVddC;
478b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineTrimVddC = 3;
479b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineOffsetVddC = 0;
480b843c749SSergey Zigachev 
481b843c749SSergey Zigachev 	return 0;
482b843c749SSergey Zigachev }
483b843c749SSergey Zigachev 
polaris10_populate_tdc_limit(struct pp_hwmgr * hwmgr)484b843c749SSergey Zigachev static int polaris10_populate_tdc_limit(struct pp_hwmgr *hwmgr)
485b843c749SSergey Zigachev {
486b843c749SSergey Zigachev 	uint16_t tdc_limit;
487b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
488b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
489b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
490b843c749SSergey Zigachev 	const struct polaris10_pt_defaults *defaults = smu_data->power_tune_defaults;
491b843c749SSergey Zigachev 
492b843c749SSergey Zigachev 	tdc_limit = (uint16_t)(table_info->cac_dtp_table->usTDC * 128);
493b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_VDDC_PkgLimit =
494b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(tdc_limit);
495b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
496b843c749SSergey Zigachev 			defaults->TDC_VDDC_ThrottleReleaseLimitPerc;
497b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_MAWt = defaults->TDC_MAWt;
498b843c749SSergey Zigachev 
499b843c749SSergey Zigachev 	return 0;
500b843c749SSergey Zigachev }
501b843c749SSergey Zigachev 
polaris10_populate_dw8(struct pp_hwmgr * hwmgr,uint32_t fuse_table_offset)502b843c749SSergey Zigachev static int polaris10_populate_dw8(struct pp_hwmgr *hwmgr, uint32_t fuse_table_offset)
503b843c749SSergey Zigachev {
504b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
505b843c749SSergey Zigachev 	const struct polaris10_pt_defaults *defaults = smu_data->power_tune_defaults;
506b843c749SSergey Zigachev 	uint32_t temp;
507b843c749SSergey Zigachev 
508b843c749SSergey Zigachev 	if (smu7_read_smc_sram_dword(hwmgr,
509b843c749SSergey Zigachev 			fuse_table_offset +
510b843c749SSergey Zigachev 			offsetof(SMU74_Discrete_PmFuses, TdcWaterfallCtl),
511b843c749SSergey Zigachev 			(uint32_t *)&temp, SMC_RAM_END))
512b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
513b843c749SSergey Zigachev 				"Attempt to read PmFuses.DW6 (SviLoadLineEn) from SMC Failed!",
514b843c749SSergey Zigachev 				return -EINVAL);
515b843c749SSergey Zigachev 	else {
516b843c749SSergey Zigachev 		smu_data->power_tune_table.TdcWaterfallCtl = defaults->TdcWaterfallCtl;
517b843c749SSergey Zigachev 		smu_data->power_tune_table.LPMLTemperatureMin =
518b843c749SSergey Zigachev 				(uint8_t)((temp >> 16) & 0xff);
519b843c749SSergey Zigachev 		smu_data->power_tune_table.LPMLTemperatureMax =
520b843c749SSergey Zigachev 				(uint8_t)((temp >> 8) & 0xff);
521b843c749SSergey Zigachev 		smu_data->power_tune_table.Reserved = (uint8_t)(temp & 0xff);
522b843c749SSergey Zigachev 	}
523b843c749SSergey Zigachev 	return 0;
524b843c749SSergey Zigachev }
525b843c749SSergey Zigachev 
polaris10_populate_temperature_scaler(struct pp_hwmgr * hwmgr)526b843c749SSergey Zigachev static int polaris10_populate_temperature_scaler(struct pp_hwmgr *hwmgr)
527b843c749SSergey Zigachev {
528b843c749SSergey Zigachev 	int i;
529b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
530b843c749SSergey Zigachev 
531b843c749SSergey Zigachev 	/* Currently not used. Set all to zero. */
532b843c749SSergey Zigachev 	for (i = 0; i < 16; i++)
533b843c749SSergey Zigachev 		smu_data->power_tune_table.LPMLTemperatureScaler[i] = 0;
534b843c749SSergey Zigachev 
535b843c749SSergey Zigachev 	return 0;
536b843c749SSergey Zigachev }
537b843c749SSergey Zigachev 
polaris10_populate_fuzzy_fan(struct pp_hwmgr * hwmgr)538b843c749SSergey Zigachev static int polaris10_populate_fuzzy_fan(struct pp_hwmgr *hwmgr)
539b843c749SSergey Zigachev {
540b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
541b843c749SSergey Zigachev 
542b843c749SSergey Zigachev /* TO DO move to hwmgr */
543b843c749SSergey Zigachev 	if ((hwmgr->thermal_controller.advanceFanControlParameters.usFanOutputSensitivity & (1 << 15))
544b843c749SSergey Zigachev 		|| 0 == hwmgr->thermal_controller.advanceFanControlParameters.usFanOutputSensitivity)
545b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usFanOutputSensitivity =
546b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usDefaultFanOutputSensitivity;
547b843c749SSergey Zigachev 
548b843c749SSergey Zigachev 	smu_data->power_tune_table.FuzzyFan_PwmSetDelta = PP_HOST_TO_SMC_US(
549b843c749SSergey Zigachev 				hwmgr->thermal_controller.advanceFanControlParameters.usFanOutputSensitivity);
550b843c749SSergey Zigachev 	return 0;
551b843c749SSergey Zigachev }
552b843c749SSergey Zigachev 
polaris10_populate_gnb_lpml(struct pp_hwmgr * hwmgr)553b843c749SSergey Zigachev static int polaris10_populate_gnb_lpml(struct pp_hwmgr *hwmgr)
554b843c749SSergey Zigachev {
555b843c749SSergey Zigachev 	int i;
556b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
557b843c749SSergey Zigachev 
558b843c749SSergey Zigachev 	/* Currently not used. Set all to zero. */
559b843c749SSergey Zigachev 	for (i = 0; i < 16; i++)
560b843c749SSergey Zigachev 		smu_data->power_tune_table.GnbLPML[i] = 0;
561b843c749SSergey Zigachev 
562b843c749SSergey Zigachev 	return 0;
563b843c749SSergey Zigachev }
564b843c749SSergey Zigachev 
polaris10_populate_bapm_vddc_base_leakage_sidd(struct pp_hwmgr * hwmgr)565b843c749SSergey Zigachev static int polaris10_populate_bapm_vddc_base_leakage_sidd(struct pp_hwmgr *hwmgr)
566b843c749SSergey Zigachev {
567b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
568b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
569b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
570b843c749SSergey Zigachev 	uint16_t hi_sidd = smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd;
571b843c749SSergey Zigachev 	uint16_t lo_sidd = smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd;
572b843c749SSergey Zigachev 	struct phm_cac_tdp_table *cac_table = table_info->cac_dtp_table;
573b843c749SSergey Zigachev 
574b843c749SSergey Zigachev 	hi_sidd = (uint16_t)(cac_table->usHighCACLeakage / 100 * 256);
575b843c749SSergey Zigachev 	lo_sidd = (uint16_t)(cac_table->usLowCACLeakage / 100 * 256);
576b843c749SSergey Zigachev 
577b843c749SSergey Zigachev 	smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd =
578b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(hi_sidd);
579b843c749SSergey Zigachev 	smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd =
580b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(lo_sidd);
581b843c749SSergey Zigachev 
582b843c749SSergey Zigachev 	return 0;
583b843c749SSergey Zigachev }
584b843c749SSergey Zigachev 
polaris10_populate_pm_fuses(struct pp_hwmgr * hwmgr)585b843c749SSergey Zigachev static int polaris10_populate_pm_fuses(struct pp_hwmgr *hwmgr)
586b843c749SSergey Zigachev {
587b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
588b843c749SSergey Zigachev 	uint32_t pm_fuse_table_offset;
589b843c749SSergey Zigachev 
590b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
591b843c749SSergey Zigachev 			PHM_PlatformCaps_PowerContainment)) {
592b843c749SSergey Zigachev 		if (smu7_read_smc_sram_dword(hwmgr,
593b843c749SSergey Zigachev 				SMU7_FIRMWARE_HEADER_LOCATION +
594b843c749SSergey Zigachev 				offsetof(SMU74_Firmware_Header, PmFuseTable),
595b843c749SSergey Zigachev 				&pm_fuse_table_offset, SMC_RAM_END))
596b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
597b843c749SSergey Zigachev 					"Attempt to get pm_fuse_table_offset Failed!",
598b843c749SSergey Zigachev 					return -EINVAL);
599b843c749SSergey Zigachev 
600b843c749SSergey Zigachev 		if (polaris10_populate_svi_load_line(hwmgr))
601b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
602b843c749SSergey Zigachev 					"Attempt to populate SviLoadLine Failed!",
603b843c749SSergey Zigachev 					return -EINVAL);
604b843c749SSergey Zigachev 
605b843c749SSergey Zigachev 		if (polaris10_populate_tdc_limit(hwmgr))
606b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
607b843c749SSergey Zigachev 					"Attempt to populate TDCLimit Failed!", return -EINVAL);
608b843c749SSergey Zigachev 
609b843c749SSergey Zigachev 		if (polaris10_populate_dw8(hwmgr, pm_fuse_table_offset))
610b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
611b843c749SSergey Zigachev 					"Attempt to populate TdcWaterfallCtl, "
612b843c749SSergey Zigachev 					"LPMLTemperature Min and Max Failed!",
613b843c749SSergey Zigachev 					return -EINVAL);
614b843c749SSergey Zigachev 
615b843c749SSergey Zigachev 		if (0 != polaris10_populate_temperature_scaler(hwmgr))
616b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
617b843c749SSergey Zigachev 					"Attempt to populate LPMLTemperatureScaler Failed!",
618b843c749SSergey Zigachev 					return -EINVAL);
619b843c749SSergey Zigachev 
620b843c749SSergey Zigachev 		if (polaris10_populate_fuzzy_fan(hwmgr))
621b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
622b843c749SSergey Zigachev 					"Attempt to populate Fuzzy Fan Control parameters Failed!",
623b843c749SSergey Zigachev 					return -EINVAL);
624b843c749SSergey Zigachev 
625b843c749SSergey Zigachev 		if (polaris10_populate_gnb_lpml(hwmgr))
626b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
627b843c749SSergey Zigachev 					"Attempt to populate GnbLPML Failed!",
628b843c749SSergey Zigachev 					return -EINVAL);
629b843c749SSergey Zigachev 
630b843c749SSergey Zigachev 		if (polaris10_populate_bapm_vddc_base_leakage_sidd(hwmgr))
631b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
632b843c749SSergey Zigachev 					"Attempt to populate BapmVddCBaseLeakage Hi and Lo "
633b843c749SSergey Zigachev 					"Sidd Failed!", return -EINVAL);
634b843c749SSergey Zigachev 
635b843c749SSergey Zigachev 		if (smu7_copy_bytes_to_smc(hwmgr, pm_fuse_table_offset,
636b843c749SSergey Zigachev 				(uint8_t *)&smu_data->power_tune_table,
637b843c749SSergey Zigachev 				(sizeof(struct SMU74_Discrete_PmFuses) - 92), SMC_RAM_END))
638b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
639b843c749SSergey Zigachev 					"Attempt to download PmFuseTable Failed!",
640b843c749SSergey Zigachev 					return -EINVAL);
641b843c749SSergey Zigachev 	}
642b843c749SSergey Zigachev 	return 0;
643b843c749SSergey Zigachev }
644b843c749SSergey Zigachev 
polaris10_populate_smc_mvdd_table(struct pp_hwmgr * hwmgr,SMU74_Discrete_DpmTable * table)645b843c749SSergey Zigachev static int polaris10_populate_smc_mvdd_table(struct pp_hwmgr *hwmgr,
646b843c749SSergey Zigachev 			SMU74_Discrete_DpmTable *table)
647b843c749SSergey Zigachev {
648b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
649b843c749SSergey Zigachev 	uint32_t count, level;
650b843c749SSergey Zigachev 
651b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control) {
652b843c749SSergey Zigachev 		count = data->mvdd_voltage_table.count;
653b843c749SSergey Zigachev 		if (count > SMU_MAX_SMIO_LEVELS)
654b843c749SSergey Zigachev 			count = SMU_MAX_SMIO_LEVELS;
655b843c749SSergey Zigachev 		for (level = 0; level < count; level++) {
656b843c749SSergey Zigachev 			table->SmioTable2.Pattern[level].Voltage =
657b843c749SSergey Zigachev 				PP_HOST_TO_SMC_US(data->mvdd_voltage_table.entries[count].value * VOLTAGE_SCALE);
658b843c749SSergey Zigachev 			/* Index into DpmTable.Smio. Drive bits from Smio entry to get this voltage level.*/
659b843c749SSergey Zigachev 			table->SmioTable2.Pattern[level].Smio =
660b843c749SSergey Zigachev 				(uint8_t) level;
661b843c749SSergey Zigachev 			table->Smio[level] |=
662b843c749SSergey Zigachev 				data->mvdd_voltage_table.entries[level].smio_low;
663b843c749SSergey Zigachev 		}
664b843c749SSergey Zigachev 		table->SmioMask2 = data->mvdd_voltage_table.mask_low;
665b843c749SSergey Zigachev 
666b843c749SSergey Zigachev 		table->MvddLevelCount = (uint32_t) PP_HOST_TO_SMC_UL(count);
667b843c749SSergey Zigachev 	}
668b843c749SSergey Zigachev 
669b843c749SSergey Zigachev 	return 0;
670b843c749SSergey Zigachev }
671b843c749SSergey Zigachev 
polaris10_populate_smc_vddci_table(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)672b843c749SSergey Zigachev static int polaris10_populate_smc_vddci_table(struct pp_hwmgr *hwmgr,
673b843c749SSergey Zigachev 					struct SMU74_Discrete_DpmTable *table)
674b843c749SSergey Zigachev {
675b843c749SSergey Zigachev 	uint32_t count, level;
676b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
677b843c749SSergey Zigachev 
678b843c749SSergey Zigachev 	count = data->vddci_voltage_table.count;
679b843c749SSergey Zigachev 
680b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
681b843c749SSergey Zigachev 		if (count > SMU_MAX_SMIO_LEVELS)
682b843c749SSergey Zigachev 			count = SMU_MAX_SMIO_LEVELS;
683b843c749SSergey Zigachev 		for (level = 0; level < count; ++level) {
684b843c749SSergey Zigachev 			table->SmioTable1.Pattern[level].Voltage =
685b843c749SSergey Zigachev 				PP_HOST_TO_SMC_US(data->vddci_voltage_table.entries[level].value * VOLTAGE_SCALE);
686b843c749SSergey Zigachev 			table->SmioTable1.Pattern[level].Smio = (uint8_t) level;
687b843c749SSergey Zigachev 
688b843c749SSergey Zigachev 			table->Smio[level] |= data->vddci_voltage_table.entries[level].smio_low;
689b843c749SSergey Zigachev 		}
690b843c749SSergey Zigachev 	}
691b843c749SSergey Zigachev 
692b843c749SSergey Zigachev 	table->SmioMask1 = data->vddci_voltage_table.mask_low;
693b843c749SSergey Zigachev 
694b843c749SSergey Zigachev 	return 0;
695b843c749SSergey Zigachev }
696b843c749SSergey Zigachev 
polaris10_populate_cac_table(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)697b843c749SSergey Zigachev static int polaris10_populate_cac_table(struct pp_hwmgr *hwmgr,
698b843c749SSergey Zigachev 		struct SMU74_Discrete_DpmTable *table)
699b843c749SSergey Zigachev {
700b843c749SSergey Zigachev 	uint32_t count;
701b843c749SSergey Zigachev 	uint8_t index;
702b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
703b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
704b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
705b843c749SSergey Zigachev 	struct phm_ppt_v1_voltage_lookup_table *lookup_table =
706b843c749SSergey Zigachev 			table_info->vddc_lookup_table;
707b843c749SSergey Zigachev 	/* tables is already swapped, so in order to use the value from it,
708b843c749SSergey Zigachev 	 * we need to swap it back.
709b843c749SSergey Zigachev 	 * We are populating vddc CAC data to BapmVddc table
710b843c749SSergey Zigachev 	 * in split and merged mode
711b843c749SSergey Zigachev 	 */
712b843c749SSergey Zigachev 	for (count = 0; count < lookup_table->count; count++) {
713b843c749SSergey Zigachev 		index = phm_get_voltage_index(lookup_table,
714b843c749SSergey Zigachev 				data->vddc_voltage_table.entries[count].value);
715b843c749SSergey Zigachev 		table->BapmVddcVidLoSidd[count] = convert_to_vid(lookup_table->entries[index].us_cac_low);
716b843c749SSergey Zigachev 		table->BapmVddcVidHiSidd[count] = convert_to_vid(lookup_table->entries[index].us_cac_mid);
717b843c749SSergey Zigachev 		table->BapmVddcVidHiSidd2[count] = convert_to_vid(lookup_table->entries[index].us_cac_high);
718b843c749SSergey Zigachev 	}
719b843c749SSergey Zigachev 
720b843c749SSergey Zigachev 	return 0;
721b843c749SSergey Zigachev }
722b843c749SSergey Zigachev 
polaris10_populate_smc_voltage_tables(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)723b843c749SSergey Zigachev static int polaris10_populate_smc_voltage_tables(struct pp_hwmgr *hwmgr,
724b843c749SSergey Zigachev 		struct SMU74_Discrete_DpmTable *table)
725b843c749SSergey Zigachev {
726b843c749SSergey Zigachev 	polaris10_populate_smc_vddci_table(hwmgr, table);
727b843c749SSergey Zigachev 	polaris10_populate_smc_mvdd_table(hwmgr, table);
728b843c749SSergey Zigachev 	polaris10_populate_cac_table(hwmgr, table);
729b843c749SSergey Zigachev 
730b843c749SSergey Zigachev 	return 0;
731b843c749SSergey Zigachev }
732b843c749SSergey Zigachev 
polaris10_populate_ulv_level(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_Ulv * state)733b843c749SSergey Zigachev static int polaris10_populate_ulv_level(struct pp_hwmgr *hwmgr,
734b843c749SSergey Zigachev 		struct SMU74_Discrete_Ulv *state)
735b843c749SSergey Zigachev {
736b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
737b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
738b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
739b843c749SSergey Zigachev 
740b843c749SSergey Zigachev 	state->CcPwrDynRm = 0;
741b843c749SSergey Zigachev 	state->CcPwrDynRm1 = 0;
742b843c749SSergey Zigachev 
743b843c749SSergey Zigachev 	state->VddcOffset = (uint16_t) table_info->us_ulv_voltage_offset;
744b843c749SSergey Zigachev 	state->VddcOffsetVid = (uint8_t)(table_info->us_ulv_voltage_offset *
745b843c749SSergey Zigachev 			VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1);
746b843c749SSergey Zigachev 
747b843c749SSergey Zigachev 	if (hwmgr->chip_id == CHIP_POLARIS12 || hwmgr->is_kicker)
748b843c749SSergey Zigachev 		state->VddcPhase = data->vddc_phase_shed_control ^ 0x3;
749b843c749SSergey Zigachev 	else
750b843c749SSergey Zigachev 		state->VddcPhase = (data->vddc_phase_shed_control) ? 0 : 1;
751b843c749SSergey Zigachev 
752b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm);
753b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm1);
754b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(state->VddcOffset);
755b843c749SSergey Zigachev 
756b843c749SSergey Zigachev 	return 0;
757b843c749SSergey Zigachev }
758b843c749SSergey Zigachev 
polaris10_populate_ulv_state(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)759b843c749SSergey Zigachev static int polaris10_populate_ulv_state(struct pp_hwmgr *hwmgr,
760b843c749SSergey Zigachev 		struct SMU74_Discrete_DpmTable *table)
761b843c749SSergey Zigachev {
762b843c749SSergey Zigachev 	return polaris10_populate_ulv_level(hwmgr, &table->Ulv);
763b843c749SSergey Zigachev }
764b843c749SSergey Zigachev 
polaris10_populate_smc_link_level(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)765b843c749SSergey Zigachev static int polaris10_populate_smc_link_level(struct pp_hwmgr *hwmgr,
766b843c749SSergey Zigachev 		struct SMU74_Discrete_DpmTable *table)
767b843c749SSergey Zigachev {
768b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
769b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
770b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
771b843c749SSergey Zigachev 	int i;
772b843c749SSergey Zigachev 
773b843c749SSergey Zigachev 	/* Index (dpm_table->pcie_speed_table.count)
774b843c749SSergey Zigachev 	 * is reserved for PCIE boot level. */
775b843c749SSergey Zigachev 	for (i = 0; i <= dpm_table->pcie_speed_table.count; i++) {
776b843c749SSergey Zigachev 		table->LinkLevel[i].PcieGenSpeed  =
777b843c749SSergey Zigachev 				(uint8_t)dpm_table->pcie_speed_table.dpm_levels[i].value;
778b843c749SSergey Zigachev 		table->LinkLevel[i].PcieLaneCount = (uint8_t)encode_pcie_lane_width(
779b843c749SSergey Zigachev 				dpm_table->pcie_speed_table.dpm_levels[i].param1);
780b843c749SSergey Zigachev 		table->LinkLevel[i].EnabledForActivity = 1;
781b843c749SSergey Zigachev 		table->LinkLevel[i].SPC = (uint8_t)(data->pcie_spc_cap & 0xff);
782b843c749SSergey Zigachev 		table->LinkLevel[i].DownThreshold = PP_HOST_TO_SMC_UL(5);
783b843c749SSergey Zigachev 		table->LinkLevel[i].UpThreshold = PP_HOST_TO_SMC_UL(30);
784b843c749SSergey Zigachev 	}
785b843c749SSergey Zigachev 
786b843c749SSergey Zigachev 	smu_data->smc_state_table.LinkLevelCount =
787b843c749SSergey Zigachev 			(uint8_t)dpm_table->pcie_speed_table.count;
788b843c749SSergey Zigachev 
789b843c749SSergey Zigachev /* To Do move to hwmgr */
790b843c749SSergey Zigachev 	data->dpm_level_enable_mask.pcie_dpm_enable_mask =
791b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
792b843c749SSergey Zigachev 
793b843c749SSergey Zigachev 	return 0;
794b843c749SSergey Zigachev }
795b843c749SSergey Zigachev 
796b843c749SSergey Zigachev 
polaris10_get_sclk_range_table(struct pp_hwmgr * hwmgr,SMU74_Discrete_DpmTable * table)797b843c749SSergey Zigachev static void polaris10_get_sclk_range_table(struct pp_hwmgr *hwmgr,
798b843c749SSergey Zigachev 				   SMU74_Discrete_DpmTable  *table)
799b843c749SSergey Zigachev {
800b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
801b843c749SSergey Zigachev 	uint32_t i, ref_clk;
802b843c749SSergey Zigachev 
803b843c749SSergey Zigachev 	struct pp_atom_ctrl_sclk_range_table range_table_from_vbios = { { {0} } };
804b843c749SSergey Zigachev 
805b843c749SSergey Zigachev 	ref_clk = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
806b843c749SSergey Zigachev 
807b843c749SSergey Zigachev 	if (0 == atomctrl_get_smc_sclk_range_table(hwmgr, &range_table_from_vbios)) {
808b843c749SSergey Zigachev 		for (i = 0; i < NUM_SCLK_RANGE; i++) {
809b843c749SSergey Zigachev 			table->SclkFcwRangeTable[i].vco_setting = range_table_from_vbios.entry[i].ucVco_setting;
810b843c749SSergey Zigachev 			table->SclkFcwRangeTable[i].postdiv = range_table_from_vbios.entry[i].ucPostdiv;
811b843c749SSergey Zigachev 			table->SclkFcwRangeTable[i].fcw_pcc = range_table_from_vbios.entry[i].usFcw_pcc;
812b843c749SSergey Zigachev 
813b843c749SSergey Zigachev 			table->SclkFcwRangeTable[i].fcw_trans_upper = range_table_from_vbios.entry[i].usFcw_trans_upper;
814b843c749SSergey Zigachev 			table->SclkFcwRangeTable[i].fcw_trans_lower = range_table_from_vbios.entry[i].usRcw_trans_lower;
815b843c749SSergey Zigachev 
816b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_pcc);
817b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_upper);
818b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_lower);
819b843c749SSergey Zigachev 		}
820b843c749SSergey Zigachev 		return;
821b843c749SSergey Zigachev 	}
822b843c749SSergey Zigachev 
823b843c749SSergey Zigachev 	for (i = 0; i < NUM_SCLK_RANGE; i++) {
824b843c749SSergey Zigachev 		smu_data->range_table[i].trans_lower_frequency = (ref_clk * Range_Table[i].fcw_trans_lower) >> Range_Table[i].postdiv;
825b843c749SSergey Zigachev 		smu_data->range_table[i].trans_upper_frequency = (ref_clk * Range_Table[i].fcw_trans_upper) >> Range_Table[i].postdiv;
826b843c749SSergey Zigachev 
827b843c749SSergey Zigachev 		table->SclkFcwRangeTable[i].vco_setting = Range_Table[i].vco_setting;
828b843c749SSergey Zigachev 		table->SclkFcwRangeTable[i].postdiv = Range_Table[i].postdiv;
829b843c749SSergey Zigachev 		table->SclkFcwRangeTable[i].fcw_pcc = Range_Table[i].fcw_pcc;
830b843c749SSergey Zigachev 
831b843c749SSergey Zigachev 		table->SclkFcwRangeTable[i].fcw_trans_upper = Range_Table[i].fcw_trans_upper;
832b843c749SSergey Zigachev 		table->SclkFcwRangeTable[i].fcw_trans_lower = Range_Table[i].fcw_trans_lower;
833b843c749SSergey Zigachev 
834b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_pcc);
835b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_upper);
836b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_lower);
837b843c749SSergey Zigachev 	}
838b843c749SSergey Zigachev }
839b843c749SSergey Zigachev 
polaris10_calculate_sclk_params(struct pp_hwmgr * hwmgr,uint32_t clock,SMU_SclkSetting * sclk_setting)840b843c749SSergey Zigachev static int polaris10_calculate_sclk_params(struct pp_hwmgr *hwmgr,
841b843c749SSergey Zigachev 		uint32_t clock, SMU_SclkSetting *sclk_setting)
842b843c749SSergey Zigachev {
843b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
844b843c749SSergey Zigachev 	const SMU74_Discrete_DpmTable *table = &(smu_data->smc_state_table);
845b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_ai dividers;
846b843c749SSergey Zigachev 	uint32_t ref_clock;
847b843c749SSergey Zigachev 	uint32_t pcc_target_percent, pcc_target_freq, ss_target_percent, ss_target_freq;
848b843c749SSergey Zigachev 	uint8_t i;
849b843c749SSergey Zigachev 	int result;
850b843c749SSergey Zigachev 	uint64_t temp;
851b843c749SSergey Zigachev 
852b843c749SSergey Zigachev 	sclk_setting->SclkFrequency = clock;
853b843c749SSergey Zigachev 	/* get the engine clock dividers for this clock value */
854b843c749SSergey Zigachev 	result = atomctrl_get_engine_pll_dividers_ai(hwmgr, clock,  &dividers);
855b843c749SSergey Zigachev 	if (result == 0) {
856b843c749SSergey Zigachev 		sclk_setting->Fcw_int = dividers.usSclk_fcw_int;
857b843c749SSergey Zigachev 		sclk_setting->Fcw_frac = dividers.usSclk_fcw_frac;
858b843c749SSergey Zigachev 		sclk_setting->Pcc_fcw_int = dividers.usPcc_fcw_int;
859b843c749SSergey Zigachev 		sclk_setting->PllRange = dividers.ucSclkPllRange;
860b843c749SSergey Zigachev 		sclk_setting->Sclk_slew_rate = 0x400;
861b843c749SSergey Zigachev 		sclk_setting->Pcc_up_slew_rate = dividers.usPcc_fcw_slew_frac;
862b843c749SSergey Zigachev 		sclk_setting->Pcc_down_slew_rate = 0xffff;
863b843c749SSergey Zigachev 		sclk_setting->SSc_En = dividers.ucSscEnable;
864b843c749SSergey Zigachev 		sclk_setting->Fcw1_int = dividers.usSsc_fcw1_int;
865b843c749SSergey Zigachev 		sclk_setting->Fcw1_frac = dividers.usSsc_fcw1_frac;
866b843c749SSergey Zigachev 		sclk_setting->Sclk_ss_slew_rate = dividers.usSsc_fcw_slew_frac;
867b843c749SSergey Zigachev 		return result;
868b843c749SSergey Zigachev 	}
869b843c749SSergey Zigachev 
870b843c749SSergey Zigachev 	ref_clock = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
871b843c749SSergey Zigachev 
872b843c749SSergey Zigachev 	for (i = 0; i < NUM_SCLK_RANGE; i++) {
873b843c749SSergey Zigachev 		if (clock > smu_data->range_table[i].trans_lower_frequency
874b843c749SSergey Zigachev 		&& clock <= smu_data->range_table[i].trans_upper_frequency) {
875b843c749SSergey Zigachev 			sclk_setting->PllRange = i;
876b843c749SSergey Zigachev 			break;
877b843c749SSergey Zigachev 		}
878b843c749SSergey Zigachev 	}
879b843c749SSergey Zigachev 
880b843c749SSergey Zigachev 	sclk_setting->Fcw_int = (uint16_t)((clock << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv) / ref_clock);
881b843c749SSergey Zigachev 	temp = clock << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv;
882b843c749SSergey Zigachev 	temp <<= 0x10;
883b843c749SSergey Zigachev 	do_div(temp, ref_clock);
884b843c749SSergey Zigachev 	sclk_setting->Fcw_frac = temp & 0xffff;
885b843c749SSergey Zigachev 
886b843c749SSergey Zigachev 	pcc_target_percent = 10; /*  Hardcode 10% for now. */
887b843c749SSergey Zigachev 	pcc_target_freq = clock - (clock * pcc_target_percent / 100);
888b843c749SSergey Zigachev 	sclk_setting->Pcc_fcw_int = (uint16_t)((pcc_target_freq << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv) / ref_clock);
889b843c749SSergey Zigachev 
890b843c749SSergey Zigachev 	ss_target_percent = 2; /*  Hardcode 2% for now. */
891b843c749SSergey Zigachev 	sclk_setting->SSc_En = 0;
892b843c749SSergey Zigachev 	if (ss_target_percent) {
893b843c749SSergey Zigachev 		sclk_setting->SSc_En = 1;
894b843c749SSergey Zigachev 		ss_target_freq = clock - (clock * ss_target_percent / 100);
895b843c749SSergey Zigachev 		sclk_setting->Fcw1_int = (uint16_t)((ss_target_freq << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv) / ref_clock);
896b843c749SSergey Zigachev 		temp = ss_target_freq << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv;
897b843c749SSergey Zigachev 		temp <<= 0x10;
898b843c749SSergey Zigachev 		do_div(temp, ref_clock);
899b843c749SSergey Zigachev 		sclk_setting->Fcw1_frac = temp & 0xffff;
900b843c749SSergey Zigachev 	}
901b843c749SSergey Zigachev 
902b843c749SSergey Zigachev 	return 0;
903b843c749SSergey Zigachev }
904b843c749SSergey Zigachev 
polaris10_populate_single_graphic_level(struct pp_hwmgr * hwmgr,uint32_t clock,struct SMU74_Discrete_GraphicsLevel * level)905b843c749SSergey Zigachev static int polaris10_populate_single_graphic_level(struct pp_hwmgr *hwmgr,
906b843c749SSergey Zigachev 		uint32_t clock, struct SMU74_Discrete_GraphicsLevel *level)
907b843c749SSergey Zigachev {
908b843c749SSergey Zigachev 	int result;
909b843c749SSergey Zigachev 	/* PP_Clocks minClocks; */
910b843c749SSergey Zigachev 	uint32_t mvdd;
911b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
912b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
913b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
914b843c749SSergey Zigachev 	SMU_SclkSetting curr_sclk_setting = { 0 };
915b843c749SSergey Zigachev 	phm_ppt_v1_clock_voltage_dependency_table *vdd_dep_table = NULL;
916b843c749SSergey Zigachev 
917b843c749SSergey Zigachev 	result = polaris10_calculate_sclk_params(hwmgr, clock, &curr_sclk_setting);
918b843c749SSergey Zigachev 
919b843c749SSergey Zigachev 	if (hwmgr->od_enabled)
920b843c749SSergey Zigachev 		vdd_dep_table = (phm_ppt_v1_clock_voltage_dependency_table *)&data->odn_dpm_table.vdd_dependency_on_sclk;
921b843c749SSergey Zigachev 	else
922b843c749SSergey Zigachev 		vdd_dep_table = table_info->vdd_dep_on_sclk;
923b843c749SSergey Zigachev 
924b843c749SSergey Zigachev 	/* populate graphics levels */
925b843c749SSergey Zigachev 	result = polaris10_get_dependency_volt_by_clk(hwmgr,
926b843c749SSergey Zigachev 			vdd_dep_table, clock,
927b843c749SSergey Zigachev 			&level->MinVoltage, &mvdd);
928b843c749SSergey Zigachev 
929b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result),
930b843c749SSergey Zigachev 			"can not find VDDC voltage value for "
931b843c749SSergey Zigachev 			"VDDC engine clock dependency table",
932b843c749SSergey Zigachev 			return result);
933b843c749SSergey Zigachev 	level->ActivityLevel = data->current_profile_setting.sclk_activity;
934b843c749SSergey Zigachev 
935b843c749SSergey Zigachev 	level->CcPwrDynRm = 0;
936b843c749SSergey Zigachev 	level->CcPwrDynRm1 = 0;
937b843c749SSergey Zigachev 	level->EnabledForActivity = 0;
938b843c749SSergey Zigachev 	level->EnabledForThrottle = 1;
939b843c749SSergey Zigachev 	level->UpHyst = data->current_profile_setting.sclk_up_hyst;
940b843c749SSergey Zigachev 	level->DownHyst = data->current_profile_setting.sclk_down_hyst;
941b843c749SSergey Zigachev 	level->VoltageDownHyst = 0;
942b843c749SSergey Zigachev 	level->PowerThrottle = 0;
943b843c749SSergey Zigachev 	data->display_timing.min_clock_in_sr = hwmgr->display_config->min_core_set_clock_in_sr;
944b843c749SSergey Zigachev 
945b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep))
946b843c749SSergey Zigachev 		level->DeepSleepDivId = smu7_get_sleep_divider_id_from_clock(clock,
947b843c749SSergey Zigachev 								hwmgr->display_config->min_core_set_clock_in_sr);
948b843c749SSergey Zigachev 
949b843c749SSergey Zigachev 	/* Default to slow, highest DPM level will be
950b843c749SSergey Zigachev 	 * set to PPSMC_DISPLAY_WATERMARK_LOW later.
951b843c749SSergey Zigachev 	 */
952b843c749SSergey Zigachev 	if (data->update_up_hyst)
953b843c749SSergey Zigachev 		level->UpHyst = (uint8_t)data->up_hyst;
954b843c749SSergey Zigachev 	if (data->update_down_hyst)
955b843c749SSergey Zigachev 		level->DownHyst = (uint8_t)data->down_hyst;
956b843c749SSergey Zigachev 
957b843c749SSergey Zigachev 	level->SclkSetting = curr_sclk_setting;
958b843c749SSergey Zigachev 
959b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->MinVoltage);
960b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm);
961b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm1);
962b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->ActivityLevel);
963b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->SclkSetting.SclkFrequency);
964b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw_int);
965b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw_frac);
966b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Pcc_fcw_int);
967b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Sclk_slew_rate);
968b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Pcc_up_slew_rate);
969b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Pcc_down_slew_rate);
970b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw1_int);
971b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw1_frac);
972b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Sclk_ss_slew_rate);
973b843c749SSergey Zigachev 	return 0;
974b843c749SSergey Zigachev }
975b843c749SSergey Zigachev 
polaris10_populate_all_graphic_levels(struct pp_hwmgr * hwmgr)976b843c749SSergey Zigachev static int polaris10_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
977b843c749SSergey Zigachev {
978b843c749SSergey Zigachev 	struct smu7_hwmgr *hw_data = (struct smu7_hwmgr *)(hwmgr->backend);
979b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
980b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &hw_data->dpm_table;
981b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
982b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
983b843c749SSergey Zigachev 	struct phm_ppt_v1_pcie_table *pcie_table = table_info->pcie_table;
984b843c749SSergey Zigachev 	uint8_t pcie_entry_cnt = (uint8_t) hw_data->dpm_table.pcie_speed_table.count;
985b843c749SSergey Zigachev 	int result = 0;
986b843c749SSergey Zigachev 	uint32_t array = smu_data->smu7_data.dpm_table_start +
987b843c749SSergey Zigachev 			offsetof(SMU74_Discrete_DpmTable, GraphicsLevel);
988b843c749SSergey Zigachev 	uint32_t array_size = sizeof(struct SMU74_Discrete_GraphicsLevel) *
989b843c749SSergey Zigachev 			SMU74_MAX_LEVELS_GRAPHICS;
990b843c749SSergey Zigachev 	struct SMU74_Discrete_GraphicsLevel *levels =
991b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsLevel;
992b843c749SSergey Zigachev 	uint32_t i, max_entry;
993b843c749SSergey Zigachev 	uint8_t hightest_pcie_level_enabled = 0,
994b843c749SSergey Zigachev 		lowest_pcie_level_enabled = 0,
995b843c749SSergey Zigachev 		mid_pcie_level_enabled = 0,
996b843c749SSergey Zigachev 		count = 0;
997b843c749SSergey Zigachev 
998b843c749SSergey Zigachev 	polaris10_get_sclk_range_table(hwmgr, &(smu_data->smc_state_table));
999b843c749SSergey Zigachev 
1000b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->sclk_table.count; i++) {
1001b843c749SSergey Zigachev 
1002b843c749SSergey Zigachev 		result = polaris10_populate_single_graphic_level(hwmgr,
1003b843c749SSergey Zigachev 				dpm_table->sclk_table.dpm_levels[i].value,
1004b843c749SSergey Zigachev 				&(smu_data->smc_state_table.GraphicsLevel[i]));
1005b843c749SSergey Zigachev 		if (result)
1006b843c749SSergey Zigachev 			return result;
1007b843c749SSergey Zigachev 
1008b843c749SSergey Zigachev 		/* Making sure only DPM level 0-1 have Deep Sleep Div ID populated. */
1009b843c749SSergey Zigachev 		if (i > 1)
1010b843c749SSergey Zigachev 			levels[i].DeepSleepDivId = 0;
1011b843c749SSergey Zigachev 	}
1012b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1013b843c749SSergey Zigachev 					PHM_PlatformCaps_SPLLShutdownSupport))
1014b843c749SSergey Zigachev 		smu_data->smc_state_table.GraphicsLevel[0].SclkSetting.SSc_En = 0;
1015b843c749SSergey Zigachev 
1016b843c749SSergey Zigachev 	smu_data->smc_state_table.GraphicsLevel[0].EnabledForActivity = 1;
1017b843c749SSergey Zigachev 	smu_data->smc_state_table.GraphicsDpmLevelCount =
1018b843c749SSergey Zigachev 			(uint8_t)dpm_table->sclk_table.count;
1019b843c749SSergey Zigachev 	hw_data->dpm_level_enable_mask.sclk_dpm_enable_mask =
1020b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
1021b843c749SSergey Zigachev 
1022b843c749SSergey Zigachev 
1023b843c749SSergey Zigachev 	if (pcie_table != NULL) {
1024b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((1 <= pcie_entry_cnt),
1025b843c749SSergey Zigachev 				"There must be 1 or more PCIE levels defined in PPTable.",
1026b843c749SSergey Zigachev 				return -EINVAL);
1027b843c749SSergey Zigachev 		max_entry = pcie_entry_cnt - 1;
1028b843c749SSergey Zigachev 		for (i = 0; i < dpm_table->sclk_table.count; i++)
1029b843c749SSergey Zigachev 			levels[i].pcieDpmLevel =
1030b843c749SSergey Zigachev 					(uint8_t) ((i < max_entry) ? i : max_entry);
1031b843c749SSergey Zigachev 	} else {
1032b843c749SSergey Zigachev 		while (hw_data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
1033b843c749SSergey Zigachev 				((hw_data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1034b843c749SSergey Zigachev 						(1 << (hightest_pcie_level_enabled + 1))) != 0))
1035b843c749SSergey Zigachev 			hightest_pcie_level_enabled++;
1036b843c749SSergey Zigachev 
1037b843c749SSergey Zigachev 		while (hw_data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
1038b843c749SSergey Zigachev 				((hw_data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1039b843c749SSergey Zigachev 						(1 << lowest_pcie_level_enabled)) == 0))
1040b843c749SSergey Zigachev 			lowest_pcie_level_enabled++;
1041b843c749SSergey Zigachev 
1042b843c749SSergey Zigachev 		while ((count < hightest_pcie_level_enabled) &&
1043b843c749SSergey Zigachev 				((hw_data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1044b843c749SSergey Zigachev 						(1 << (lowest_pcie_level_enabled + 1 + count))) == 0))
1045b843c749SSergey Zigachev 			count++;
1046b843c749SSergey Zigachev 
1047b843c749SSergey Zigachev 		mid_pcie_level_enabled = (lowest_pcie_level_enabled + 1 + count) <
1048b843c749SSergey Zigachev 				hightest_pcie_level_enabled ?
1049b843c749SSergey Zigachev 						(lowest_pcie_level_enabled + 1 + count) :
1050b843c749SSergey Zigachev 						hightest_pcie_level_enabled;
1051b843c749SSergey Zigachev 
1052b843c749SSergey Zigachev 		/* set pcieDpmLevel to hightest_pcie_level_enabled */
1053b843c749SSergey Zigachev 		for (i = 2; i < dpm_table->sclk_table.count; i++)
1054b843c749SSergey Zigachev 			levels[i].pcieDpmLevel = hightest_pcie_level_enabled;
1055b843c749SSergey Zigachev 
1056b843c749SSergey Zigachev 		/* set pcieDpmLevel to lowest_pcie_level_enabled */
1057b843c749SSergey Zigachev 		levels[0].pcieDpmLevel = lowest_pcie_level_enabled;
1058b843c749SSergey Zigachev 
1059b843c749SSergey Zigachev 		/* set pcieDpmLevel to mid_pcie_level_enabled */
1060b843c749SSergey Zigachev 		levels[1].pcieDpmLevel = mid_pcie_level_enabled;
1061b843c749SSergey Zigachev 	}
1062b843c749SSergey Zigachev 	/* level count will send to smc once at init smc table and never change */
1063b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr, array, (uint8_t *)levels,
1064b843c749SSergey Zigachev 			(uint32_t)array_size, SMC_RAM_END);
1065b843c749SSergey Zigachev 
1066b843c749SSergey Zigachev 	return result;
1067b843c749SSergey Zigachev }
1068b843c749SSergey Zigachev 
1069b843c749SSergey Zigachev 
polaris10_populate_single_memory_level(struct pp_hwmgr * hwmgr,uint32_t clock,struct SMU74_Discrete_MemoryLevel * mem_level)1070b843c749SSergey Zigachev static int polaris10_populate_single_memory_level(struct pp_hwmgr *hwmgr,
1071b843c749SSergey Zigachev 		uint32_t clock, struct SMU74_Discrete_MemoryLevel *mem_level)
1072b843c749SSergey Zigachev {
1073b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1074b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1075b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1076b843c749SSergey Zigachev 	int result = 0;
1077b843c749SSergey Zigachev 	uint32_t mclk_stutter_mode_threshold = 40000;
1078b843c749SSergey Zigachev 	phm_ppt_v1_clock_voltage_dependency_table *vdd_dep_table = NULL;
1079b843c749SSergey Zigachev 
1080b843c749SSergey Zigachev 
1081b843c749SSergey Zigachev 	if (hwmgr->od_enabled)
1082b843c749SSergey Zigachev 		vdd_dep_table = (phm_ppt_v1_clock_voltage_dependency_table *)&data->odn_dpm_table.vdd_dependency_on_mclk;
1083b843c749SSergey Zigachev 	else
1084b843c749SSergey Zigachev 		vdd_dep_table = table_info->vdd_dep_on_mclk;
1085b843c749SSergey Zigachev 
1086b843c749SSergey Zigachev 	if (vdd_dep_table) {
1087b843c749SSergey Zigachev 		result = polaris10_get_dependency_volt_by_clk(hwmgr,
1088b843c749SSergey Zigachev 				vdd_dep_table, clock,
1089b843c749SSergey Zigachev 				&mem_level->MinVoltage, &mem_level->MinMvdd);
1090b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1091b843c749SSergey Zigachev 				"can not find MinVddc voltage value from memory "
1092b843c749SSergey Zigachev 				"VDDC voltage dependency table", return result);
1093b843c749SSergey Zigachev 	}
1094b843c749SSergey Zigachev 
1095b843c749SSergey Zigachev 	mem_level->MclkFrequency = clock;
1096b843c749SSergey Zigachev 	mem_level->EnabledForThrottle = 1;
1097b843c749SSergey Zigachev 	mem_level->EnabledForActivity = 0;
1098b843c749SSergey Zigachev 	mem_level->UpHyst = data->current_profile_setting.mclk_up_hyst;
1099b843c749SSergey Zigachev 	mem_level->DownHyst = data->current_profile_setting.mclk_down_hyst;
1100b843c749SSergey Zigachev 	mem_level->VoltageDownHyst = 0;
1101b843c749SSergey Zigachev 	mem_level->ActivityLevel = data->current_profile_setting.mclk_activity;
1102b843c749SSergey Zigachev 	mem_level->StutterEnable = false;
1103b843c749SSergey Zigachev 	mem_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
1104b843c749SSergey Zigachev 
1105b843c749SSergey Zigachev 	data->display_timing.num_existing_displays = hwmgr->display_config->num_display;
1106b843c749SSergey Zigachev 
1107b843c749SSergey Zigachev 	if (mclk_stutter_mode_threshold &&
1108b843c749SSergey Zigachev 		(clock <= mclk_stutter_mode_threshold) &&
1109b843c749SSergey Zigachev 		(PHM_READ_FIELD(hwmgr->device, DPG_PIPE_STUTTER_CONTROL,
1110b843c749SSergey Zigachev 				STUTTER_ENABLE) & 0x1))
1111b843c749SSergey Zigachev 		mem_level->StutterEnable = true;
1112b843c749SSergey Zigachev 
1113b843c749SSergey Zigachev 	if (!result) {
1114b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinMvdd);
1115b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MclkFrequency);
1116b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(mem_level->ActivityLevel);
1117b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinVoltage);
1118b843c749SSergey Zigachev 	}
1119b843c749SSergey Zigachev 	return result;
1120b843c749SSergey Zigachev }
1121b843c749SSergey Zigachev 
polaris10_populate_all_memory_levels(struct pp_hwmgr * hwmgr)1122b843c749SSergey Zigachev static int polaris10_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
1123b843c749SSergey Zigachev {
1124b843c749SSergey Zigachev 	struct smu7_hwmgr *hw_data = (struct smu7_hwmgr *)(hwmgr->backend);
1125b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1126b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &hw_data->dpm_table;
1127b843c749SSergey Zigachev 	int result;
1128b843c749SSergey Zigachev 	/* populate MCLK dpm table to SMU7 */
1129b843c749SSergey Zigachev 	uint32_t array = smu_data->smu7_data.dpm_table_start +
1130b843c749SSergey Zigachev 			offsetof(SMU74_Discrete_DpmTable, MemoryLevel);
1131b843c749SSergey Zigachev 	uint32_t array_size = sizeof(SMU74_Discrete_MemoryLevel) *
1132b843c749SSergey Zigachev 			SMU74_MAX_LEVELS_MEMORY;
1133b843c749SSergey Zigachev 	struct SMU74_Discrete_MemoryLevel *levels =
1134b843c749SSergey Zigachev 			smu_data->smc_state_table.MemoryLevel;
1135b843c749SSergey Zigachev 	uint32_t i;
1136b843c749SSergey Zigachev 
1137b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->mclk_table.count; i++) {
1138b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 != dpm_table->mclk_table.dpm_levels[i].value),
1139b843c749SSergey Zigachev 				"can not populate memory level as memory clock is zero",
1140b843c749SSergey Zigachev 				return -EINVAL);
1141b843c749SSergey Zigachev 		result = polaris10_populate_single_memory_level(hwmgr,
1142b843c749SSergey Zigachev 				dpm_table->mclk_table.dpm_levels[i].value,
1143b843c749SSergey Zigachev 				&levels[i]);
1144b843c749SSergey Zigachev 		if (i == dpm_table->mclk_table.count - 1) {
1145b843c749SSergey Zigachev 			levels[i].DisplayWatermark = PPSMC_DISPLAY_WATERMARK_HIGH;
1146b843c749SSergey Zigachev 			levels[i].EnabledForActivity = 1;
1147b843c749SSergey Zigachev 		}
1148b843c749SSergey Zigachev 		if (result)
1149b843c749SSergey Zigachev 			return result;
1150b843c749SSergey Zigachev 	}
1151b843c749SSergey Zigachev 
1152b843c749SSergey Zigachev 	/* In order to prevent MC activity from stutter mode to push DPM up,
1153b843c749SSergey Zigachev 	 * the UVD change complements this by putting the MCLK in
1154b843c749SSergey Zigachev 	 * a higher state by default such that we are not affected by
1155b843c749SSergey Zigachev 	 * up threshold or and MCLK DPM latency.
1156b843c749SSergey Zigachev 	 */
1157b843c749SSergey Zigachev 	levels[0].ActivityLevel = 0x1f;
1158b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(levels[0].ActivityLevel);
1159b843c749SSergey Zigachev 
1160b843c749SSergey Zigachev 	smu_data->smc_state_table.MemoryDpmLevelCount =
1161b843c749SSergey Zigachev 			(uint8_t)dpm_table->mclk_table.count;
1162b843c749SSergey Zigachev 	hw_data->dpm_level_enable_mask.mclk_dpm_enable_mask =
1163b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
1164b843c749SSergey Zigachev 
1165b843c749SSergey Zigachev 	/* level count will send to smc once at init smc table and never change */
1166b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr, array, (uint8_t *)levels,
1167b843c749SSergey Zigachev 			(uint32_t)array_size, SMC_RAM_END);
1168b843c749SSergey Zigachev 
1169b843c749SSergey Zigachev 	return result;
1170b843c749SSergey Zigachev }
1171b843c749SSergey Zigachev 
polaris10_populate_mvdd_value(struct pp_hwmgr * hwmgr,uint32_t mclk,SMIO_Pattern * smio_pat)1172b843c749SSergey Zigachev static int polaris10_populate_mvdd_value(struct pp_hwmgr *hwmgr,
1173b843c749SSergey Zigachev 		uint32_t mclk, SMIO_Pattern *smio_pat)
1174b843c749SSergey Zigachev {
1175b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1176b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1177b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1178b843c749SSergey Zigachev 	uint32_t i = 0;
1179b843c749SSergey Zigachev 
1180b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE != data->mvdd_control) {
1181b843c749SSergey Zigachev 		/* find mvdd value which clock is more than request */
1182b843c749SSergey Zigachev 		for (i = 0; i < table_info->vdd_dep_on_mclk->count; i++) {
1183b843c749SSergey Zigachev 			if (mclk <= table_info->vdd_dep_on_mclk->entries[i].clk) {
1184b843c749SSergey Zigachev 				smio_pat->Voltage = data->mvdd_voltage_table.entries[i].value;
1185b843c749SSergey Zigachev 				break;
1186b843c749SSergey Zigachev 			}
1187b843c749SSergey Zigachev 		}
1188b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(i < table_info->vdd_dep_on_mclk->count,
1189b843c749SSergey Zigachev 				"MVDD Voltage is outside the supported range.",
1190b843c749SSergey Zigachev 				return -EINVAL);
1191b843c749SSergey Zigachev 	} else
1192b843c749SSergey Zigachev 		return -EINVAL;
1193b843c749SSergey Zigachev 
1194b843c749SSergey Zigachev 	return 0;
1195b843c749SSergey Zigachev }
1196b843c749SSergey Zigachev 
polaris10_populate_smc_acpi_level(struct pp_hwmgr * hwmgr,SMU74_Discrete_DpmTable * table)1197b843c749SSergey Zigachev static int polaris10_populate_smc_acpi_level(struct pp_hwmgr *hwmgr,
1198b843c749SSergey Zigachev 		SMU74_Discrete_DpmTable *table)
1199b843c749SSergey Zigachev {
1200b843c749SSergey Zigachev 	int result = 0;
1201b843c749SSergey Zigachev 	uint32_t sclk_frequency;
1202b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1203b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1204b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1205b843c749SSergey Zigachev 	SMIO_Pattern vol_level;
1206b843c749SSergey Zigachev 	uint32_t mvdd;
1207b843c749SSergey Zigachev 
1208b843c749SSergey Zigachev 	table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
1209b843c749SSergey Zigachev 
1210b843c749SSergey Zigachev 	/* Get MinVoltage and Frequency from DPM0,
1211b843c749SSergey Zigachev 	 * already converted to SMC_UL */
1212b843c749SSergey Zigachev 	sclk_frequency = data->vbios_boot_state.sclk_bootup_value;
1213b843c749SSergey Zigachev 	result = polaris10_get_dependency_volt_by_clk(hwmgr,
1214b843c749SSergey Zigachev 			table_info->vdd_dep_on_sclk,
1215b843c749SSergey Zigachev 			sclk_frequency,
1216b843c749SSergey Zigachev 			&table->ACPILevel.MinVoltage, &mvdd);
1217b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result),
1218b843c749SSergey Zigachev 			"Cannot find ACPI VDDC voltage value "
1219b843c749SSergey Zigachev 			"in Clock Dependency Table",
1220b843c749SSergey Zigachev 			);
1221b843c749SSergey Zigachev 
1222b843c749SSergey Zigachev 	result = polaris10_calculate_sclk_params(hwmgr, sclk_frequency,  &(table->ACPILevel.SclkSetting));
1223b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0, "Error retrieving Engine Clock dividers from VBIOS.", return result);
1224b843c749SSergey Zigachev 
1225b843c749SSergey Zigachev 	table->ACPILevel.DeepSleepDivId = 0;
1226b843c749SSergey Zigachev 	table->ACPILevel.CcPwrDynRm = 0;
1227b843c749SSergey Zigachev 	table->ACPILevel.CcPwrDynRm1 = 0;
1228b843c749SSergey Zigachev 
1229b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.Flags);
1230b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.MinVoltage);
1231b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm);
1232b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm1);
1233b843c749SSergey Zigachev 
1234b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SclkSetting.SclkFrequency);
1235b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw_int);
1236b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw_frac);
1237b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Pcc_fcw_int);
1238b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Sclk_slew_rate);
1239b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Pcc_up_slew_rate);
1240b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Pcc_down_slew_rate);
1241b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw1_int);
1242b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw1_frac);
1243b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Sclk_ss_slew_rate);
1244b843c749SSergey Zigachev 
1245b843c749SSergey Zigachev 
1246b843c749SSergey Zigachev 	/* Get MinVoltage and Frequency from DPM0, already converted to SMC_UL */
1247b843c749SSergey Zigachev 	table->MemoryACPILevel.MclkFrequency = data->vbios_boot_state.mclk_bootup_value;
1248b843c749SSergey Zigachev 	result = polaris10_get_dependency_volt_by_clk(hwmgr,
1249b843c749SSergey Zigachev 			table_info->vdd_dep_on_mclk,
1250b843c749SSergey Zigachev 			table->MemoryACPILevel.MclkFrequency,
1251b843c749SSergey Zigachev 			&table->MemoryACPILevel.MinVoltage, &mvdd);
1252b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result),
1253b843c749SSergey Zigachev 			"Cannot find ACPI VDDCI voltage value "
1254b843c749SSergey Zigachev 			"in Clock Dependency Table",
1255b843c749SSergey Zigachev 			);
1256b843c749SSergey Zigachev 
1257b843c749SSergey Zigachev 	if (!((SMU7_VOLTAGE_CONTROL_NONE == data->mvdd_control) ||
1258b843c749SSergey Zigachev 			(data->mclk_dpm_key_disabled)))
1259b843c749SSergey Zigachev 		polaris10_populate_mvdd_value(hwmgr,
1260b843c749SSergey Zigachev 				data->dpm_table.mclk_table.dpm_levels[0].value,
1261b843c749SSergey Zigachev 				&vol_level);
1262b843c749SSergey Zigachev 
1263b843c749SSergey Zigachev 	if (0 == polaris10_populate_mvdd_value(hwmgr, 0, &vol_level))
1264b843c749SSergey Zigachev 		table->MemoryACPILevel.MinMvdd = PP_HOST_TO_SMC_UL(vol_level.Voltage);
1265b843c749SSergey Zigachev 	else
1266b843c749SSergey Zigachev 		table->MemoryACPILevel.MinMvdd = 0;
1267b843c749SSergey Zigachev 
1268b843c749SSergey Zigachev 	table->MemoryACPILevel.StutterEnable = false;
1269b843c749SSergey Zigachev 
1270b843c749SSergey Zigachev 	table->MemoryACPILevel.EnabledForThrottle = 0;
1271b843c749SSergey Zigachev 	table->MemoryACPILevel.EnabledForActivity = 0;
1272b843c749SSergey Zigachev 	table->MemoryACPILevel.UpHyst = 0;
1273b843c749SSergey Zigachev 	table->MemoryACPILevel.DownHyst = 100;
1274b843c749SSergey Zigachev 	table->MemoryACPILevel.VoltageDownHyst = 0;
1275b843c749SSergey Zigachev 	table->MemoryACPILevel.ActivityLevel =
1276b843c749SSergey Zigachev 			PP_HOST_TO_SMC_US(data->current_profile_setting.mclk_activity);
1277b843c749SSergey Zigachev 
1278b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MclkFrequency);
1279b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MinVoltage);
1280b843c749SSergey Zigachev 
1281b843c749SSergey Zigachev 	return result;
1282b843c749SSergey Zigachev }
1283b843c749SSergey Zigachev 
polaris10_populate_smc_vce_level(struct pp_hwmgr * hwmgr,SMU74_Discrete_DpmTable * table)1284b843c749SSergey Zigachev static int polaris10_populate_smc_vce_level(struct pp_hwmgr *hwmgr,
1285b843c749SSergey Zigachev 		SMU74_Discrete_DpmTable *table)
1286b843c749SSergey Zigachev {
1287b843c749SSergey Zigachev 	int result = -EINVAL;
1288b843c749SSergey Zigachev 	uint8_t count;
1289b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
1290b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1291b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1292b843c749SSergey Zigachev 	struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1293b843c749SSergey Zigachev 			table_info->mm_dep_table;
1294b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1295b843c749SSergey Zigachev 	uint32_t vddci;
1296b843c749SSergey Zigachev 
1297b843c749SSergey Zigachev 	table->VceLevelCount = (uint8_t)(mm_table->count);
1298b843c749SSergey Zigachev 	table->VceBootLevel = 0;
1299b843c749SSergey Zigachev 
1300b843c749SSergey Zigachev 	for (count = 0; count < table->VceLevelCount; count++) {
1301b843c749SSergey Zigachev 		table->VceLevel[count].Frequency = mm_table->entries[count].eclk;
1302b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage = 0;
1303b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage |=
1304b843c749SSergey Zigachev 				(mm_table->entries[count].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
1305b843c749SSergey Zigachev 
1306b843c749SSergey Zigachev 		if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control)
1307b843c749SSergey Zigachev 			vddci = (uint32_t)phm_find_closest_vddci(&(data->vddci_voltage_table),
1308b843c749SSergey Zigachev 						mm_table->entries[count].vddc - VDDC_VDDCI_DELTA);
1309b843c749SSergey Zigachev 		else if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control)
1310b843c749SSergey Zigachev 			vddci = mm_table->entries[count].vddc - VDDC_VDDCI_DELTA;
1311b843c749SSergey Zigachev 		else
1312b843c749SSergey Zigachev 			vddci = (data->vbios_boot_state.vddci_bootup_value * VOLTAGE_SCALE) << VDDCI_SHIFT;
1313b843c749SSergey Zigachev 
1314b843c749SSergey Zigachev 
1315b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage |=
1316b843c749SSergey Zigachev 				(vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
1317b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1318b843c749SSergey Zigachev 
1319b843c749SSergey Zigachev 		/*retrieve divider value for VBIOS */
1320b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1321b843c749SSergey Zigachev 				table->VceLevel[count].Frequency, &dividers);
1322b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1323b843c749SSergey Zigachev 				"can not find divide id for VCE engine clock",
1324b843c749SSergey Zigachev 				return result);
1325b843c749SSergey Zigachev 
1326b843c749SSergey Zigachev 		table->VceLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
1327b843c749SSergey Zigachev 
1328b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].Frequency);
1329b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].MinVoltage);
1330b843c749SSergey Zigachev 	}
1331b843c749SSergey Zigachev 	return result;
1332b843c749SSergey Zigachev }
1333b843c749SSergey Zigachev 
polaris10_populate_memory_timing_parameters(struct pp_hwmgr * hwmgr,int32_t eng_clock,int32_t mem_clock,SMU74_Discrete_MCArbDramTimingTableEntry * arb_regs)1334b843c749SSergey Zigachev static int polaris10_populate_memory_timing_parameters(struct pp_hwmgr *hwmgr,
1335b843c749SSergey Zigachev 		int32_t eng_clock, int32_t mem_clock,
1336b843c749SSergey Zigachev 		SMU74_Discrete_MCArbDramTimingTableEntry *arb_regs)
1337b843c749SSergey Zigachev {
1338b843c749SSergey Zigachev 	uint32_t dram_timing;
1339b843c749SSergey Zigachev 	uint32_t dram_timing2;
1340b843c749SSergey Zigachev 	uint32_t burst_time;
1341b843c749SSergey Zigachev 	int result;
1342b843c749SSergey Zigachev 
1343b843c749SSergey Zigachev 	result = atomctrl_set_engine_dram_timings_rv770(hwmgr,
1344b843c749SSergey Zigachev 			eng_clock, mem_clock);
1345b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0,
1346b843c749SSergey Zigachev 			"Error calling VBIOS to set DRAM_TIMING.", return result);
1347b843c749SSergey Zigachev 
1348b843c749SSergey Zigachev 	dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
1349b843c749SSergey Zigachev 	dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
1350b843c749SSergey Zigachev 	burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0);
1351b843c749SSergey Zigachev 
1352b843c749SSergey Zigachev 
1353b843c749SSergey Zigachev 	arb_regs->McArbDramTiming  = PP_HOST_TO_SMC_UL(dram_timing);
1354b843c749SSergey Zigachev 	arb_regs->McArbDramTiming2 = PP_HOST_TO_SMC_UL(dram_timing2);
1355b843c749SSergey Zigachev 	arb_regs->McArbBurstTime   = (uint8_t)burst_time;
1356b843c749SSergey Zigachev 
1357b843c749SSergey Zigachev 	return 0;
1358b843c749SSergey Zigachev }
1359b843c749SSergey Zigachev 
polaris10_program_memory_timing_parameters(struct pp_hwmgr * hwmgr)1360b843c749SSergey Zigachev static int polaris10_program_memory_timing_parameters(struct pp_hwmgr *hwmgr)
1361b843c749SSergey Zigachev {
1362b843c749SSergey Zigachev 	struct smu7_hwmgr *hw_data = (struct smu7_hwmgr *)(hwmgr->backend);
1363b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1364b843c749SSergey Zigachev 	struct SMU74_Discrete_MCArbDramTimingTable arb_regs;
1365b843c749SSergey Zigachev 	uint32_t i, j;
1366b843c749SSergey Zigachev 	int result = 0;
1367b843c749SSergey Zigachev 
1368b843c749SSergey Zigachev 	for (i = 0; i < hw_data->dpm_table.sclk_table.count; i++) {
1369b843c749SSergey Zigachev 		for (j = 0; j < hw_data->dpm_table.mclk_table.count; j++) {
1370b843c749SSergey Zigachev 			result = polaris10_populate_memory_timing_parameters(hwmgr,
1371b843c749SSergey Zigachev 					hw_data->dpm_table.sclk_table.dpm_levels[i].value,
1372b843c749SSergey Zigachev 					hw_data->dpm_table.mclk_table.dpm_levels[j].value,
1373b843c749SSergey Zigachev 					&arb_regs.entries[i][j]);
1374b843c749SSergey Zigachev 			if (result == 0)
1375b843c749SSergey Zigachev 				result = atomctrl_set_ac_timing_ai(hwmgr, hw_data->dpm_table.mclk_table.dpm_levels[j].value, j);
1376b843c749SSergey Zigachev 			if (result != 0)
1377b843c749SSergey Zigachev 				return result;
1378b843c749SSergey Zigachev 		}
1379b843c749SSergey Zigachev 	}
1380b843c749SSergey Zigachev 
1381b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(
1382b843c749SSergey Zigachev 			hwmgr,
1383b843c749SSergey Zigachev 			smu_data->smu7_data.arb_table_start,
1384b843c749SSergey Zigachev 			(uint8_t *)&arb_regs,
1385b843c749SSergey Zigachev 			sizeof(SMU74_Discrete_MCArbDramTimingTable),
1386b843c749SSergey Zigachev 			SMC_RAM_END);
1387b843c749SSergey Zigachev 	return result;
1388b843c749SSergey Zigachev }
1389b843c749SSergey Zigachev 
polaris10_populate_smc_uvd_level(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)1390b843c749SSergey Zigachev static int polaris10_populate_smc_uvd_level(struct pp_hwmgr *hwmgr,
1391b843c749SSergey Zigachev 		struct SMU74_Discrete_DpmTable *table)
1392b843c749SSergey Zigachev {
1393b843c749SSergey Zigachev 	int result = -EINVAL;
1394b843c749SSergey Zigachev 	uint8_t count;
1395b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
1396b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1397b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1398b843c749SSergey Zigachev 	struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1399b843c749SSergey Zigachev 			table_info->mm_dep_table;
1400b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1401b843c749SSergey Zigachev 	uint32_t vddci;
1402b843c749SSergey Zigachev 
1403b843c749SSergey Zigachev 	table->UvdLevelCount = (uint8_t)(mm_table->count);
1404b843c749SSergey Zigachev 	table->UvdBootLevel = 0;
1405b843c749SSergey Zigachev 
1406b843c749SSergey Zigachev 	for (count = 0; count < table->UvdLevelCount; count++) {
1407b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage = 0;
1408b843c749SSergey Zigachev 		table->UvdLevel[count].VclkFrequency = mm_table->entries[count].vclk;
1409b843c749SSergey Zigachev 		table->UvdLevel[count].DclkFrequency = mm_table->entries[count].dclk;
1410b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
1411b843c749SSergey Zigachev 				VOLTAGE_SCALE) << VDDC_SHIFT;
1412b843c749SSergey Zigachev 
1413b843c749SSergey Zigachev 		if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control)
1414b843c749SSergey Zigachev 			vddci = (uint32_t)phm_find_closest_vddci(&(data->vddci_voltage_table),
1415b843c749SSergey Zigachev 						mm_table->entries[count].vddc - VDDC_VDDCI_DELTA);
1416b843c749SSergey Zigachev 		else if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control)
1417b843c749SSergey Zigachev 			vddci = mm_table->entries[count].vddc - VDDC_VDDCI_DELTA;
1418b843c749SSergey Zigachev 		else
1419b843c749SSergey Zigachev 			vddci = (data->vbios_boot_state.vddci_bootup_value * VOLTAGE_SCALE) << VDDCI_SHIFT;
1420b843c749SSergey Zigachev 
1421b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
1422b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1423b843c749SSergey Zigachev 
1424b843c749SSergey Zigachev 		/* retrieve divider value for VBIOS */
1425b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1426b843c749SSergey Zigachev 				table->UvdLevel[count].VclkFrequency, &dividers);
1427b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1428b843c749SSergey Zigachev 				"can not find divide id for Vclk clock", return result);
1429b843c749SSergey Zigachev 
1430b843c749SSergey Zigachev 		table->UvdLevel[count].VclkDivider = (uint8_t)dividers.pll_post_divider;
1431b843c749SSergey Zigachev 
1432b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1433b843c749SSergey Zigachev 				table->UvdLevel[count].DclkFrequency, &dividers);
1434b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1435b843c749SSergey Zigachev 				"can not find divide id for Dclk clock", return result);
1436b843c749SSergey Zigachev 
1437b843c749SSergey Zigachev 		table->UvdLevel[count].DclkDivider = (uint8_t)dividers.pll_post_divider;
1438b843c749SSergey Zigachev 
1439b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].VclkFrequency);
1440b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].DclkFrequency);
1441b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].MinVoltage);
1442b843c749SSergey Zigachev 	}
1443b843c749SSergey Zigachev 
1444b843c749SSergey Zigachev 	return result;
1445b843c749SSergey Zigachev }
1446b843c749SSergey Zigachev 
polaris10_populate_smc_boot_level(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)1447b843c749SSergey Zigachev static int polaris10_populate_smc_boot_level(struct pp_hwmgr *hwmgr,
1448b843c749SSergey Zigachev 		struct SMU74_Discrete_DpmTable *table)
1449b843c749SSergey Zigachev {
1450b843c749SSergey Zigachev 	int result = 0;
1451b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1452b843c749SSergey Zigachev 
1453b843c749SSergey Zigachev 	table->GraphicsBootLevel = 0;
1454b843c749SSergey Zigachev 	table->MemoryBootLevel = 0;
1455b843c749SSergey Zigachev 
1456b843c749SSergey Zigachev 	/* find boot level from dpm table */
1457b843c749SSergey Zigachev 	result = phm_find_boot_level(&(data->dpm_table.sclk_table),
1458b843c749SSergey Zigachev 			data->vbios_boot_state.sclk_bootup_value,
1459b843c749SSergey Zigachev 			(uint32_t *)&(table->GraphicsBootLevel));
1460b843c749SSergey Zigachev 
1461b843c749SSergey Zigachev 	result = phm_find_boot_level(&(data->dpm_table.mclk_table),
1462b843c749SSergey Zigachev 			data->vbios_boot_state.mclk_bootup_value,
1463b843c749SSergey Zigachev 			(uint32_t *)&(table->MemoryBootLevel));
1464b843c749SSergey Zigachev 
1465b843c749SSergey Zigachev 	table->BootVddc  = data->vbios_boot_state.vddc_bootup_value *
1466b843c749SSergey Zigachev 			VOLTAGE_SCALE;
1467b843c749SSergey Zigachev 	table->BootVddci = data->vbios_boot_state.vddci_bootup_value *
1468b843c749SSergey Zigachev 			VOLTAGE_SCALE;
1469b843c749SSergey Zigachev 	table->BootMVdd  = data->vbios_boot_state.mvdd_bootup_value *
1470b843c749SSergey Zigachev 			VOLTAGE_SCALE;
1471b843c749SSergey Zigachev 
1472b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->BootVddc);
1473b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->BootVddci);
1474b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->BootMVdd);
1475b843c749SSergey Zigachev 
1476b843c749SSergey Zigachev 	return 0;
1477b843c749SSergey Zigachev }
1478b843c749SSergey Zigachev 
polaris10_populate_smc_initailial_state(struct pp_hwmgr * hwmgr)1479b843c749SSergey Zigachev static int polaris10_populate_smc_initailial_state(struct pp_hwmgr *hwmgr)
1480b843c749SSergey Zigachev {
1481b843c749SSergey Zigachev 	struct smu7_hwmgr *hw_data = (struct smu7_hwmgr *)(hwmgr->backend);
1482b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1483b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1484b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1485b843c749SSergey Zigachev 	uint8_t count, level;
1486b843c749SSergey Zigachev 
1487b843c749SSergey Zigachev 	count = (uint8_t)(table_info->vdd_dep_on_sclk->count);
1488b843c749SSergey Zigachev 
1489b843c749SSergey Zigachev 	for (level = 0; level < count; level++) {
1490b843c749SSergey Zigachev 		if (table_info->vdd_dep_on_sclk->entries[level].clk >=
1491b843c749SSergey Zigachev 				hw_data->vbios_boot_state.sclk_bootup_value) {
1492b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsBootLevel = level;
1493b843c749SSergey Zigachev 			break;
1494b843c749SSergey Zigachev 		}
1495b843c749SSergey Zigachev 	}
1496b843c749SSergey Zigachev 
1497b843c749SSergey Zigachev 	count = (uint8_t)(table_info->vdd_dep_on_mclk->count);
1498b843c749SSergey Zigachev 	for (level = 0; level < count; level++) {
1499b843c749SSergey Zigachev 		if (table_info->vdd_dep_on_mclk->entries[level].clk >=
1500b843c749SSergey Zigachev 				hw_data->vbios_boot_state.mclk_bootup_value) {
1501b843c749SSergey Zigachev 			smu_data->smc_state_table.MemoryBootLevel = level;
1502b843c749SSergey Zigachev 			break;
1503b843c749SSergey Zigachev 		}
1504b843c749SSergey Zigachev 	}
1505b843c749SSergey Zigachev 
1506b843c749SSergey Zigachev 	return 0;
1507b843c749SSergey Zigachev }
1508b843c749SSergey Zigachev 
polaris10_populate_clock_stretcher_data_table(struct pp_hwmgr * hwmgr)1509b843c749SSergey Zigachev static int polaris10_populate_clock_stretcher_data_table(struct pp_hwmgr *hwmgr)
1510b843c749SSergey Zigachev {
1511b843c749SSergey Zigachev 	uint32_t ro, efuse, volt_without_cks, volt_with_cks, value, max, min;
1512b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1513b843c749SSergey Zigachev 
1514b843c749SSergey Zigachev 	uint8_t i, stretch_amount, volt_offset = 0;
1515b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1516b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1517b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
1518b843c749SSergey Zigachev 			table_info->vdd_dep_on_sclk;
1519b843c749SSergey Zigachev 
1520b843c749SSergey Zigachev 	stretch_amount = (uint8_t)table_info->cac_dtp_table->usClockStretchAmount;
1521b843c749SSergey Zigachev 
1522b843c749SSergey Zigachev 	/* Read SMU_Eefuse to read and calculate RO and determine
1523b843c749SSergey Zigachev 	 * if the part is SS or FF. if RO >= 1660MHz, part is FF.
1524b843c749SSergey Zigachev 	 */
1525b843c749SSergey Zigachev 	efuse = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1526b843c749SSergey Zigachev 			ixSMU_EFUSE_0 + (67 * 4));
1527b843c749SSergey Zigachev 	efuse &= 0xFF000000;
1528b843c749SSergey Zigachev 	efuse = efuse >> 24;
1529b843c749SSergey Zigachev 
1530b843c749SSergey Zigachev 	if (hwmgr->chip_id == CHIP_POLARIS10) {
1531b843c749SSergey Zigachev 		if (hwmgr->is_kicker) {
1532b843c749SSergey Zigachev 			min = 1200;
1533b843c749SSergey Zigachev 			max = 2500;
1534b843c749SSergey Zigachev 		} else {
1535b843c749SSergey Zigachev 			min = 1000;
1536b843c749SSergey Zigachev 			max = 2300;
1537b843c749SSergey Zigachev 		}
1538b843c749SSergey Zigachev 	} else if (hwmgr->chip_id == CHIP_POLARIS11) {
1539b843c749SSergey Zigachev 		if (hwmgr->is_kicker) {
1540b843c749SSergey Zigachev 			min = 900;
1541b843c749SSergey Zigachev 			max = 2100;
1542b843c749SSergey Zigachev 		} else {
1543b843c749SSergey Zigachev 			min = 1100;
1544b843c749SSergey Zigachev 			max = 2100;
1545b843c749SSergey Zigachev 		}
1546b843c749SSergey Zigachev 	} else {
1547b843c749SSergey Zigachev 		min = 1100;
1548b843c749SSergey Zigachev 		max = 2100;
1549b843c749SSergey Zigachev 	}
1550b843c749SSergey Zigachev 
1551b843c749SSergey Zigachev 	ro = efuse * (max - min) / 255 + min;
1552b843c749SSergey Zigachev 
1553b843c749SSergey Zigachev 	/* Populate Sclk_CKS_masterEn0_7 and Sclk_voltageOffset */
1554b843c749SSergey Zigachev 	for (i = 0; i < sclk_table->count; i++) {
1555b843c749SSergey Zigachev 		smu_data->smc_state_table.Sclk_CKS_masterEn0_7 |=
1556b843c749SSergey Zigachev 				sclk_table->entries[i].cks_enable << i;
1557b843c749SSergey Zigachev 		if (hwmgr->chip_id == CHIP_POLARIS10) {
1558b843c749SSergey Zigachev 			volt_without_cks = (uint32_t)((2753594000U + (sclk_table->entries[i].clk/100) * 136418 - (ro - 70) * 1000000) / \
1559b843c749SSergey Zigachev 						(2424180 - (sclk_table->entries[i].clk/100) * 1132925/1000));
1560b843c749SSergey Zigachev 			volt_with_cks = (uint32_t)((2797202000U + sclk_table->entries[i].clk/100 * 3232 - (ro - 65) * 1000000) / \
1561b843c749SSergey Zigachev 					(2522480 - sclk_table->entries[i].clk/100 * 115764/100));
1562b843c749SSergey Zigachev 		} else {
1563b843c749SSergey Zigachev 			volt_without_cks = (uint32_t)((2416794800U + (sclk_table->entries[i].clk/100) * 1476925/10 - (ro - 50) * 1000000) / \
1564b843c749SSergey Zigachev 						(2625416 - (sclk_table->entries[i].clk/100) * (12586807/10000)));
1565b843c749SSergey Zigachev 			volt_with_cks = (uint32_t)((2999656000U - sclk_table->entries[i].clk/100 * 392803 - (ro - 44) * 1000000) / \
1566b843c749SSergey Zigachev 					(3422454 - sclk_table->entries[i].clk/100 * (18886376/10000)));
1567b843c749SSergey Zigachev 		}
1568b843c749SSergey Zigachev 
1569b843c749SSergey Zigachev 		if (volt_without_cks >= volt_with_cks)
1570b843c749SSergey Zigachev 			volt_offset = (uint8_t)(((volt_without_cks - volt_with_cks +
1571b843c749SSergey Zigachev 					sclk_table->entries[i].cks_voffset) * 100 + 624) / 625);
1572b843c749SSergey Zigachev 
1573b843c749SSergey Zigachev 		smu_data->smc_state_table.Sclk_voltageOffset[i] = volt_offset;
1574b843c749SSergey Zigachev 	}
1575b843c749SSergey Zigachev 
1576b843c749SSergey Zigachev 	smu_data->smc_state_table.LdoRefSel = (table_info->cac_dtp_table->ucCKS_LDO_REFSEL != 0) ? table_info->cac_dtp_table->ucCKS_LDO_REFSEL : 6;
1577b843c749SSergey Zigachev 	/* Populate CKS Lookup Table */
1578b843c749SSergey Zigachev 	if (stretch_amount == 0 || stretch_amount > 5) {
1579b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1580b843c749SSergey Zigachev 				PHM_PlatformCaps_ClockStretcher);
1581b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
1582b843c749SSergey Zigachev 				"Stretch Amount in PPTable not supported",
1583b843c749SSergey Zigachev 				return -EINVAL);
1584b843c749SSergey Zigachev 	}
1585b843c749SSergey Zigachev 
1586b843c749SSergey Zigachev 	value = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL);
1587b843c749SSergey Zigachev 	value &= 0xFFFFFFFE;
1588b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL, value);
1589b843c749SSergey Zigachev 
1590b843c749SSergey Zigachev 	return 0;
1591b843c749SSergey Zigachev }
1592b843c749SSergey Zigachev 
polaris10_populate_vr_config(struct pp_hwmgr * hwmgr,struct SMU74_Discrete_DpmTable * table)1593b843c749SSergey Zigachev static int polaris10_populate_vr_config(struct pp_hwmgr *hwmgr,
1594b843c749SSergey Zigachev 		struct SMU74_Discrete_DpmTable *table)
1595b843c749SSergey Zigachev {
1596b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1597b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1598b843c749SSergey Zigachev 	uint16_t config;
1599b843c749SSergey Zigachev 
1600b843c749SSergey Zigachev 	config = VR_MERGED_WITH_VDDC;
1601b843c749SSergey Zigachev 	table->VRConfig |= (config << VRCONF_VDDGFX_SHIFT);
1602b843c749SSergey Zigachev 
1603b843c749SSergey Zigachev 	/* Set Vddc Voltage Controller */
1604b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control) {
1605b843c749SSergey Zigachev 		config = VR_SVI2_PLANE_1;
1606b843c749SSergey Zigachev 		table->VRConfig |= config;
1607b843c749SSergey Zigachev 	} else {
1608b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
1609b843c749SSergey Zigachev 				"VDDC should be on SVI2 control in merged mode!",
1610b843c749SSergey Zigachev 				);
1611b843c749SSergey Zigachev 	}
1612b843c749SSergey Zigachev 	/* Set Vddci Voltage Controller */
1613b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control) {
1614b843c749SSergey Zigachev 		config = VR_SVI2_PLANE_2;  /* only in merged mode */
1615b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1616b843c749SSergey Zigachev 	} else if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
1617b843c749SSergey Zigachev 		config = VR_SMIO_PATTERN_1;
1618b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1619b843c749SSergey Zigachev 	} else {
1620b843c749SSergey Zigachev 		config = VR_STATIC_VOLTAGE;
1621b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1622b843c749SSergey Zigachev 	}
1623b843c749SSergey Zigachev 	/* Set Mvdd Voltage Controller */
1624b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control) {
1625b843c749SSergey Zigachev 		config = VR_SVI2_PLANE_2;
1626b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
1627b843c749SSergey Zigachev 		cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, smu_data->smu7_data.soft_regs_start +
1628b843c749SSergey Zigachev 			offsetof(SMU74_SoftRegisters, AllowMvddSwitch), 0x1);
1629b843c749SSergey Zigachev 	} else {
1630b843c749SSergey Zigachev 		config = VR_STATIC_VOLTAGE;
1631b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
1632b843c749SSergey Zigachev 	}
1633b843c749SSergey Zigachev 
1634b843c749SSergey Zigachev 	return 0;
1635b843c749SSergey Zigachev }
1636b843c749SSergey Zigachev 
1637b843c749SSergey Zigachev 
polaris10_populate_avfs_parameters(struct pp_hwmgr * hwmgr)1638b843c749SSergey Zigachev static int polaris10_populate_avfs_parameters(struct pp_hwmgr *hwmgr)
1639b843c749SSergey Zigachev {
1640b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1641b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1642b843c749SSergey Zigachev 
1643b843c749SSergey Zigachev 	SMU74_Discrete_DpmTable  *table = &(smu_data->smc_state_table);
1644b843c749SSergey Zigachev 	int result = 0;
1645b843c749SSergey Zigachev 	struct pp_atom_ctrl__avfs_parameters avfs_params = {0};
1646b843c749SSergey Zigachev 	AVFS_meanNsigma_t AVFS_meanNsigma = { {0} };
1647b843c749SSergey Zigachev 	AVFS_Sclk_Offset_t AVFS_SclkOffset = { {0} };
1648b843c749SSergey Zigachev 	uint32_t tmp, i;
1649b843c749SSergey Zigachev 
1650b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1651b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)hwmgr->pptable;
1652b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
1653b843c749SSergey Zigachev 			table_info->vdd_dep_on_sclk;
1654b843c749SSergey Zigachev 
1655b843c749SSergey Zigachev 
1656b843c749SSergey Zigachev 	if (!hwmgr->avfs_supported)
1657b843c749SSergey Zigachev 		return 0;
1658b843c749SSergey Zigachev 
1659b843c749SSergey Zigachev 	result = atomctrl_get_avfs_information(hwmgr, &avfs_params);
1660b843c749SSergey Zigachev 
1661b843c749SSergey Zigachev 	if (0 == result) {
1662b843c749SSergey Zigachev 		table->BTCGB_VDROOP_TABLE[0].a0  = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSON_a0);
1663b843c749SSergey Zigachev 		table->BTCGB_VDROOP_TABLE[0].a1  = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSON_a1);
1664b843c749SSergey Zigachev 		table->BTCGB_VDROOP_TABLE[0].a2  = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSON_a2);
1665b843c749SSergey Zigachev 		table->BTCGB_VDROOP_TABLE[1].a0  = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSOFF_a0);
1666b843c749SSergey Zigachev 		table->BTCGB_VDROOP_TABLE[1].a1  = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSOFF_a1);
1667b843c749SSergey Zigachev 		table->BTCGB_VDROOP_TABLE[1].a2  = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSOFF_a2);
1668b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[0].m1 = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSON_m1);
1669b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[0].m2 = PP_HOST_TO_SMC_US(avfs_params.usAVFSGB_FUSE_TABLE_CKSON_m2);
1670b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[0].b  = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSON_b);
1671b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[0].m1_shift = 24;
1672b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[0].m2_shift  = 12;
1673b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[1].m1 = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSOFF_m1);
1674b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[1].m2 = PP_HOST_TO_SMC_US(avfs_params.usAVFSGB_FUSE_TABLE_CKSOFF_m2);
1675b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[1].b  = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSOFF_b);
1676b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[1].m1_shift = 24;
1677b843c749SSergey Zigachev 		table->AVFSGB_VDROOP_TABLE[1].m2_shift  = 12;
1678b843c749SSergey Zigachev 		table->MaxVoltage                = PP_HOST_TO_SMC_US(avfs_params.usMaxVoltage_0_25mv);
1679b843c749SSergey Zigachev 		AVFS_meanNsigma.Aconstant[0]      = PP_HOST_TO_SMC_UL(avfs_params.ulAVFS_meanNsigma_Acontant0);
1680b843c749SSergey Zigachev 		AVFS_meanNsigma.Aconstant[1]      = PP_HOST_TO_SMC_UL(avfs_params.ulAVFS_meanNsigma_Acontant1);
1681b843c749SSergey Zigachev 		AVFS_meanNsigma.Aconstant[2]      = PP_HOST_TO_SMC_UL(avfs_params.ulAVFS_meanNsigma_Acontant2);
1682b843c749SSergey Zigachev 		AVFS_meanNsigma.DC_tol_sigma      = PP_HOST_TO_SMC_US(avfs_params.usAVFS_meanNsigma_DC_tol_sigma);
1683b843c749SSergey Zigachev 		AVFS_meanNsigma.Platform_mean     = PP_HOST_TO_SMC_US(avfs_params.usAVFS_meanNsigma_Platform_mean);
1684b843c749SSergey Zigachev 		AVFS_meanNsigma.PSM_Age_CompFactor = PP_HOST_TO_SMC_US(avfs_params.usPSM_Age_ComFactor);
1685b843c749SSergey Zigachev 		AVFS_meanNsigma.Platform_sigma     = PP_HOST_TO_SMC_US(avfs_params.usAVFS_meanNsigma_Platform_sigma);
1686b843c749SSergey Zigachev 
1687b843c749SSergey Zigachev 		for (i = 0; i < NUM_VFT_COLUMNS; i++) {
1688b843c749SSergey Zigachev 			AVFS_meanNsigma.Static_Voltage_Offset[i] = (uint8_t)(sclk_table->entries[i].cks_voffset * 100 / 625);
1689b843c749SSergey Zigachev 			AVFS_SclkOffset.Sclk_Offset[i] = PP_HOST_TO_SMC_US((uint16_t)(sclk_table->entries[i].sclk_offset) / 100);
1690b843c749SSergey Zigachev 		}
1691b843c749SSergey Zigachev 
1692b843c749SSergey Zigachev 		result = smu7_read_smc_sram_dword(hwmgr,
1693b843c749SSergey Zigachev 				SMU7_FIRMWARE_HEADER_LOCATION + offsetof(SMU74_Firmware_Header, AvfsMeanNSigma),
1694b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
1695b843c749SSergey Zigachev 
1696b843c749SSergey Zigachev 		smu7_copy_bytes_to_smc(hwmgr,
1697b843c749SSergey Zigachev 					tmp,
1698b843c749SSergey Zigachev 					(uint8_t *)&AVFS_meanNsigma,
1699b843c749SSergey Zigachev 					sizeof(AVFS_meanNsigma_t),
1700b843c749SSergey Zigachev 					SMC_RAM_END);
1701b843c749SSergey Zigachev 
1702b843c749SSergey Zigachev 		result = smu7_read_smc_sram_dword(hwmgr,
1703b843c749SSergey Zigachev 				SMU7_FIRMWARE_HEADER_LOCATION + offsetof(SMU74_Firmware_Header, AvfsSclkOffsetTable),
1704b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
1705b843c749SSergey Zigachev 		smu7_copy_bytes_to_smc(hwmgr,
1706b843c749SSergey Zigachev 					tmp,
1707b843c749SSergey Zigachev 					(uint8_t *)&AVFS_SclkOffset,
1708b843c749SSergey Zigachev 					sizeof(AVFS_Sclk_Offset_t),
1709b843c749SSergey Zigachev 					SMC_RAM_END);
1710b843c749SSergey Zigachev 
1711b843c749SSergey Zigachev 		data->avfs_vdroop_override_setting = (avfs_params.ucEnableGB_VDROOP_TABLE_CKSON << BTCGB0_Vdroop_Enable_SHIFT) |
1712b843c749SSergey Zigachev 						(avfs_params.ucEnableGB_VDROOP_TABLE_CKSOFF << BTCGB1_Vdroop_Enable_SHIFT) |
1713b843c749SSergey Zigachev 						(avfs_params.ucEnableGB_FUSE_TABLE_CKSON << AVFSGB0_Vdroop_Enable_SHIFT) |
1714b843c749SSergey Zigachev 						(avfs_params.ucEnableGB_FUSE_TABLE_CKSOFF << AVFSGB1_Vdroop_Enable_SHIFT);
1715b843c749SSergey Zigachev 		data->apply_avfs_cks_off_voltage = (avfs_params.ucEnableApplyAVFS_CKS_OFF_Voltage == 1) ? true : false;
1716b843c749SSergey Zigachev 	}
1717b843c749SSergey Zigachev 	return result;
1718b843c749SSergey Zigachev }
1719b843c749SSergey Zigachev 
polaris10_init_arb_table_index(struct pp_hwmgr * hwmgr)1720b843c749SSergey Zigachev static int polaris10_init_arb_table_index(struct pp_hwmgr *hwmgr)
1721b843c749SSergey Zigachev {
1722b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1723b843c749SSergey Zigachev 	uint32_t tmp;
1724b843c749SSergey Zigachev 	int result;
1725b843c749SSergey Zigachev 
1726b843c749SSergey Zigachev 	/* This is a read-modify-write on the first byte of the ARB table.
1727b843c749SSergey Zigachev 	 * The first byte in the SMU73_Discrete_MCArbDramTimingTable structure
1728b843c749SSergey Zigachev 	 * is the field 'current'.
1729b843c749SSergey Zigachev 	 * This solution is ugly, but we never write the whole table only
1730b843c749SSergey Zigachev 	 * individual fields in it.
1731b843c749SSergey Zigachev 	 * In reality this field should not be in that structure
1732b843c749SSergey Zigachev 	 * but in a soft register.
1733b843c749SSergey Zigachev 	 */
1734b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
1735b843c749SSergey Zigachev 			smu_data->smu7_data.arb_table_start, &tmp, SMC_RAM_END);
1736b843c749SSergey Zigachev 
1737b843c749SSergey Zigachev 	if (result)
1738b843c749SSergey Zigachev 		return result;
1739b843c749SSergey Zigachev 
1740b843c749SSergey Zigachev 	tmp &= 0x00FFFFFF;
1741b843c749SSergey Zigachev 	tmp |= ((uint32_t)MC_CG_ARB_FREQ_F1) << 24;
1742b843c749SSergey Zigachev 
1743b843c749SSergey Zigachev 	return smu7_write_smc_sram_dword(hwmgr,
1744b843c749SSergey Zigachev 			smu_data->smu7_data.arb_table_start, tmp, SMC_RAM_END);
1745b843c749SSergey Zigachev }
1746b843c749SSergey Zigachev 
polaris10_initialize_power_tune_defaults(struct pp_hwmgr * hwmgr)1747b843c749SSergey Zigachev static void polaris10_initialize_power_tune_defaults(struct pp_hwmgr *hwmgr)
1748b843c749SSergey Zigachev {
1749b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1750b843c749SSergey Zigachev 	struct  phm_ppt_v1_information *table_info =
1751b843c749SSergey Zigachev 			(struct  phm_ppt_v1_information *)(hwmgr->pptable);
1752b843c749SSergey Zigachev 
1753b843c749SSergey Zigachev 	if (table_info &&
1754b843c749SSergey Zigachev 			table_info->cac_dtp_table->usPowerTuneDataSetID <= POWERTUNE_DEFAULT_SET_MAX &&
1755b843c749SSergey Zigachev 			table_info->cac_dtp_table->usPowerTuneDataSetID)
1756b843c749SSergey Zigachev 		smu_data->power_tune_defaults =
1757b843c749SSergey Zigachev 				&polaris10_power_tune_data_set_array
1758b843c749SSergey Zigachev 				[table_info->cac_dtp_table->usPowerTuneDataSetID - 1];
1759b843c749SSergey Zigachev 	else
1760b843c749SSergey Zigachev 		smu_data->power_tune_defaults = &polaris10_power_tune_data_set_array[0];
1761b843c749SSergey Zigachev 
1762b843c749SSergey Zigachev }
1763b843c749SSergey Zigachev 
polaris10_init_smc_table(struct pp_hwmgr * hwmgr)1764b843c749SSergey Zigachev static int polaris10_init_smc_table(struct pp_hwmgr *hwmgr)
1765b843c749SSergey Zigachev {
1766b843c749SSergey Zigachev 	int result;
1767b843c749SSergey Zigachev 	struct smu7_hwmgr *hw_data = (struct smu7_hwmgr *)(hwmgr->backend);
1768b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
1769b843c749SSergey Zigachev 
1770b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1771b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1772b843c749SSergey Zigachev 	struct SMU74_Discrete_DpmTable *table = &(smu_data->smc_state_table);
1773b843c749SSergey Zigachev 	uint8_t i;
1774b843c749SSergey Zigachev 	struct pp_atomctrl_gpio_pin_assignment gpio_pin;
1775b843c749SSergey Zigachev 	pp_atomctrl_clock_dividers_vi dividers;
1776b843c749SSergey Zigachev 
1777b843c749SSergey Zigachev 	polaris10_initialize_power_tune_defaults(hwmgr);
1778b843c749SSergey Zigachev 
1779b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE != hw_data->voltage_control)
1780b843c749SSergey Zigachev 		polaris10_populate_smc_voltage_tables(hwmgr, table);
1781b843c749SSergey Zigachev 
1782b843c749SSergey Zigachev 	table->SystemFlags = 0;
1783b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1784b843c749SSergey Zigachev 			PHM_PlatformCaps_AutomaticDCTransition))
1785b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
1786b843c749SSergey Zigachev 
1787b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1788b843c749SSergey Zigachev 			PHM_PlatformCaps_StepVddc))
1789b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
1790b843c749SSergey Zigachev 
1791b843c749SSergey Zigachev 	if (hw_data->is_memory_gddr5)
1792b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
1793b843c749SSergey Zigachev 
1794b843c749SSergey Zigachev 	if (hw_data->ulv_supported && table_info->us_ulv_voltage_offset) {
1795b843c749SSergey Zigachev 		result = polaris10_populate_ulv_state(hwmgr, table);
1796b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == result,
1797b843c749SSergey Zigachev 				"Failed to initialize ULV state!", return result);
1798b843c749SSergey Zigachev 		cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1799b843c749SSergey Zigachev 				ixCG_ULV_PARAMETER, SMU7_CGULVPARAMETER_DFLT);
1800b843c749SSergey Zigachev 	}
1801b843c749SSergey Zigachev 
1802b843c749SSergey Zigachev 	result = polaris10_populate_smc_link_level(hwmgr, table);
1803b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1804b843c749SSergey Zigachev 			"Failed to initialize Link Level!", return result);
1805b843c749SSergey Zigachev 
1806b843c749SSergey Zigachev 	result = polaris10_populate_all_graphic_levels(hwmgr);
1807b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1808b843c749SSergey Zigachev 			"Failed to initialize Graphics Level!", return result);
1809b843c749SSergey Zigachev 
1810b843c749SSergey Zigachev 	result = polaris10_populate_all_memory_levels(hwmgr);
1811b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1812b843c749SSergey Zigachev 			"Failed to initialize Memory Level!", return result);
1813b843c749SSergey Zigachev 
1814b843c749SSergey Zigachev 	result = polaris10_populate_smc_acpi_level(hwmgr, table);
1815b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1816b843c749SSergey Zigachev 			"Failed to initialize ACPI Level!", return result);
1817b843c749SSergey Zigachev 
1818b843c749SSergey Zigachev 	result = polaris10_populate_smc_vce_level(hwmgr, table);
1819b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1820b843c749SSergey Zigachev 			"Failed to initialize VCE Level!", return result);
1821b843c749SSergey Zigachev 
1822b843c749SSergey Zigachev 	/* Since only the initial state is completely set up at this point
1823b843c749SSergey Zigachev 	 * (the other states are just copies of the boot state) we only
1824b843c749SSergey Zigachev 	 * need to populate the  ARB settings for the initial state.
1825b843c749SSergey Zigachev 	 */
1826b843c749SSergey Zigachev 	result = polaris10_program_memory_timing_parameters(hwmgr);
1827b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1828b843c749SSergey Zigachev 			"Failed to Write ARB settings for the initial state.", return result);
1829b843c749SSergey Zigachev 
1830b843c749SSergey Zigachev 	result = polaris10_populate_smc_uvd_level(hwmgr, table);
1831b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1832b843c749SSergey Zigachev 			"Failed to initialize UVD Level!", return result);
1833b843c749SSergey Zigachev 
1834b843c749SSergey Zigachev 	result = polaris10_populate_smc_boot_level(hwmgr, table);
1835b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1836b843c749SSergey Zigachev 			"Failed to initialize Boot Level!", return result);
1837b843c749SSergey Zigachev 
1838b843c749SSergey Zigachev 	result = polaris10_populate_smc_initailial_state(hwmgr);
1839b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1840b843c749SSergey Zigachev 			"Failed to initialize Boot State!", return result);
1841b843c749SSergey Zigachev 
1842b843c749SSergey Zigachev 	result = polaris10_populate_bapm_parameters_in_dpm_table(hwmgr);
1843b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1844b843c749SSergey Zigachev 			"Failed to populate BAPM Parameters!", return result);
1845b843c749SSergey Zigachev 
1846b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1847b843c749SSergey Zigachev 			PHM_PlatformCaps_ClockStretcher)) {
1848b843c749SSergey Zigachev 		result = polaris10_populate_clock_stretcher_data_table(hwmgr);
1849b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == result,
1850b843c749SSergey Zigachev 				"Failed to populate Clock Stretcher Data Table!",
1851b843c749SSergey Zigachev 				return result);
1852b843c749SSergey Zigachev 	}
1853b843c749SSergey Zigachev 
1854b843c749SSergey Zigachev 	result = polaris10_populate_avfs_parameters(hwmgr);
1855b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result, "Failed to populate AVFS Parameters!", return result;);
1856b843c749SSergey Zigachev 
1857b843c749SSergey Zigachev 	table->CurrSclkPllRange = 0xff;
1858b843c749SSergey Zigachev 	table->GraphicsVoltageChangeEnable  = 1;
1859b843c749SSergey Zigachev 	table->GraphicsThermThrottleEnable  = 1;
1860b843c749SSergey Zigachev 	table->GraphicsInterval = 1;
1861b843c749SSergey Zigachev 	table->VoltageInterval  = 1;
1862b843c749SSergey Zigachev 	table->ThermalInterval  = 1;
1863b843c749SSergey Zigachev 	table->TemperatureLimitHigh =
1864b843c749SSergey Zigachev 			table_info->cac_dtp_table->usTargetOperatingTemp *
1865b843c749SSergey Zigachev 			SMU7_Q88_FORMAT_CONVERSION_UNIT;
1866b843c749SSergey Zigachev 	table->TemperatureLimitLow  =
1867b843c749SSergey Zigachev 			(table_info->cac_dtp_table->usTargetOperatingTemp - 1) *
1868b843c749SSergey Zigachev 			SMU7_Q88_FORMAT_CONVERSION_UNIT;
1869b843c749SSergey Zigachev 	table->MemoryVoltageChangeEnable = 1;
1870b843c749SSergey Zigachev 	table->MemoryInterval = 1;
1871b843c749SSergey Zigachev 	table->VoltageResponseTime = 0;
1872b843c749SSergey Zigachev 	table->PhaseResponseTime = 0;
1873b843c749SSergey Zigachev 	table->MemoryThermThrottleEnable = 1;
1874b843c749SSergey Zigachev 	table->PCIeBootLinkLevel = 0;
1875b843c749SSergey Zigachev 	table->PCIeGenInterval = 1;
1876b843c749SSergey Zigachev 	table->VRConfig = 0;
1877b843c749SSergey Zigachev 
1878b843c749SSergey Zigachev 	result = polaris10_populate_vr_config(hwmgr, table);
1879b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1880b843c749SSergey Zigachev 			"Failed to populate VRConfig setting!", return result);
1881b843c749SSergey Zigachev 	hw_data->vr_config = table->VRConfig;
1882b843c749SSergey Zigachev 	table->ThermGpio = 17;
1883b843c749SSergey Zigachev 	table->SclkStepSize = 0x4000;
1884b843c749SSergey Zigachev 
1885b843c749SSergey Zigachev 	if (atomctrl_get_pp_assign_pin(hwmgr, VDDC_VRHOT_GPIO_PINID, &gpio_pin)) {
1886b843c749SSergey Zigachev 		table->VRHotGpio = gpio_pin.uc_gpio_pin_bit_shift;
1887b843c749SSergey Zigachev 	} else {
1888b843c749SSergey Zigachev 		table->VRHotGpio = SMU7_UNUSED_GPIO_PIN;
1889b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1890b843c749SSergey Zigachev 				PHM_PlatformCaps_RegulatorHot);
1891b843c749SSergey Zigachev 	}
1892b843c749SSergey Zigachev 
1893b843c749SSergey Zigachev 	if (atomctrl_get_pp_assign_pin(hwmgr, PP_AC_DC_SWITCH_GPIO_PINID,
1894b843c749SSergey Zigachev 			&gpio_pin)) {
1895b843c749SSergey Zigachev 		table->AcDcGpio = gpio_pin.uc_gpio_pin_bit_shift;
1896b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
1897b843c749SSergey Zigachev 				PHM_PlatformCaps_AutomaticDCTransition);
1898b843c749SSergey Zigachev 	} else {
1899b843c749SSergey Zigachev 		table->AcDcGpio = SMU7_UNUSED_GPIO_PIN;
1900b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1901b843c749SSergey Zigachev 				PHM_PlatformCaps_AutomaticDCTransition);
1902b843c749SSergey Zigachev 	}
1903b843c749SSergey Zigachev 
1904b843c749SSergey Zigachev 	/* Thermal Output GPIO */
1905b843c749SSergey Zigachev 	if (atomctrl_get_pp_assign_pin(hwmgr, THERMAL_INT_OUTPUT_GPIO_PINID,
1906b843c749SSergey Zigachev 			&gpio_pin)) {
1907b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
1908b843c749SSergey Zigachev 				PHM_PlatformCaps_ThermalOutGPIO);
1909b843c749SSergey Zigachev 
1910b843c749SSergey Zigachev 		table->ThermOutGpio = gpio_pin.uc_gpio_pin_bit_shift;
1911b843c749SSergey Zigachev 
1912b843c749SSergey Zigachev 		/* For porlarity read GPIOPAD_A with assigned Gpio pin
1913b843c749SSergey Zigachev 		 * since VBIOS will program this register to set 'inactive state',
1914b843c749SSergey Zigachev 		 * driver can then determine 'active state' from this and
1915b843c749SSergey Zigachev 		 * program SMU with correct polarity
1916b843c749SSergey Zigachev 		 */
1917b843c749SSergey Zigachev 		table->ThermOutPolarity = (0 == (cgs_read_register(hwmgr->device, mmGPIOPAD_A)
1918b843c749SSergey Zigachev 					& (1 << gpio_pin.uc_gpio_pin_bit_shift))) ? 1:0;
1919b843c749SSergey Zigachev 		table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_ONLY;
1920b843c749SSergey Zigachev 
1921b843c749SSergey Zigachev 		/* if required, combine VRHot/PCC with thermal out GPIO */
1922b843c749SSergey Zigachev 		if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_RegulatorHot)
1923b843c749SSergey Zigachev 		&& phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_CombinePCCWithThermalSignal))
1924b843c749SSergey Zigachev 			table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_VRHOT;
1925b843c749SSergey Zigachev 	} else {
1926b843c749SSergey Zigachev 		table->ThermOutGpio = 17;
1927b843c749SSergey Zigachev 		table->ThermOutPolarity = 1;
1928b843c749SSergey Zigachev 		table->ThermOutMode = SMU7_THERM_OUT_MODE_DISABLE;
1929b843c749SSergey Zigachev 	}
1930b843c749SSergey Zigachev 
1931b843c749SSergey Zigachev 	/* Populate BIF_SCLK levels into SMC DPM table */
1932b843c749SSergey Zigachev 	for (i = 0; i <= hw_data->dpm_table.pcie_speed_table.count; i++) {
1933b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr, smu_data->bif_sclk_table[i], &dividers);
1934b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((result == 0), "Can not find DFS divide id for Sclk", return result);
1935b843c749SSergey Zigachev 
1936b843c749SSergey Zigachev 		if (i == 0)
1937b843c749SSergey Zigachev 			table->Ulv.BifSclkDfs = PP_HOST_TO_SMC_US((USHORT)(dividers.pll_post_divider));
1938b843c749SSergey Zigachev 		else
1939b843c749SSergey Zigachev 			table->LinkLevel[i-1].BifSclkDfs = PP_HOST_TO_SMC_US((USHORT)(dividers.pll_post_divider));
1940b843c749SSergey Zigachev 	}
1941b843c749SSergey Zigachev 
1942b843c749SSergey Zigachev 	for (i = 0; i < SMU74_MAX_ENTRIES_SMIO; i++)
1943b843c749SSergey Zigachev 		table->Smio[i] = PP_HOST_TO_SMC_UL(table->Smio[i]);
1944b843c749SSergey Zigachev 
1945b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SystemFlags);
1946b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->VRConfig);
1947b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask1);
1948b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask2);
1949b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SclkStepSize);
1950b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->CurrSclkPllRange);
1951b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitHigh);
1952b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitLow);
1953b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->VoltageResponseTime);
1954b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->PhaseResponseTime);
1955b843c749SSergey Zigachev 
1956b843c749SSergey Zigachev 	/* Upload all dpm data to SMC memory.(dpm level, dpm level count etc) */
1957b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr,
1958b843c749SSergey Zigachev 			smu_data->smu7_data.dpm_table_start +
1959b843c749SSergey Zigachev 			offsetof(SMU74_Discrete_DpmTable, SystemFlags),
1960b843c749SSergey Zigachev 			(uint8_t *)&(table->SystemFlags),
1961b843c749SSergey Zigachev 			sizeof(SMU74_Discrete_DpmTable) - 3 * sizeof(SMU74_PIDController),
1962b843c749SSergey Zigachev 			SMC_RAM_END);
1963b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1964b843c749SSergey Zigachev 			"Failed to upload dpm data to SMC memory!", return result);
1965b843c749SSergey Zigachev 
1966b843c749SSergey Zigachev 	result = polaris10_init_arb_table_index(hwmgr);
1967b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1968b843c749SSergey Zigachev 			"Failed to upload arb data to SMC memory!", return result);
1969b843c749SSergey Zigachev 
1970b843c749SSergey Zigachev 	result = polaris10_populate_pm_fuses(hwmgr);
1971b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1972b843c749SSergey Zigachev 			"Failed to  populate PM fuses to SMC memory!", return result);
1973b843c749SSergey Zigachev 
1974b843c749SSergey Zigachev 	return 0;
1975b843c749SSergey Zigachev }
1976b843c749SSergey Zigachev 
polaris10_program_mem_timing_parameters(struct pp_hwmgr * hwmgr)1977b843c749SSergey Zigachev static int polaris10_program_mem_timing_parameters(struct pp_hwmgr *hwmgr)
1978b843c749SSergey Zigachev {
1979b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1980b843c749SSergey Zigachev 
1981b843c749SSergey Zigachev 	if (data->need_update_smu7_dpm_table &
1982b843c749SSergey Zigachev 		(DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_OD_UPDATE_MCLK))
1983b843c749SSergey Zigachev 		return polaris10_program_memory_timing_parameters(hwmgr);
1984b843c749SSergey Zigachev 
1985b843c749SSergey Zigachev 	return 0;
1986b843c749SSergey Zigachev }
1987b843c749SSergey Zigachev 
1988*78973132SSergey Zigachev int polaris10_thermal_avfs_enable(struct pp_hwmgr *hwmgr);
polaris10_thermal_avfs_enable(struct pp_hwmgr * hwmgr)1989b843c749SSergey Zigachev int polaris10_thermal_avfs_enable(struct pp_hwmgr *hwmgr)
1990b843c749SSergey Zigachev {
1991b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1992b843c749SSergey Zigachev 
1993b843c749SSergey Zigachev 	if (!hwmgr->avfs_supported)
1994b843c749SSergey Zigachev 		return 0;
1995b843c749SSergey Zigachev 
1996b843c749SSergey Zigachev 	smum_send_msg_to_smc_with_parameter(hwmgr,
1997b843c749SSergey Zigachev 			PPSMC_MSG_SetGBDroopSettings, data->avfs_vdroop_override_setting);
1998b843c749SSergey Zigachev 
1999b843c749SSergey Zigachev 	smum_send_msg_to_smc(hwmgr, PPSMC_MSG_EnableAvfs);
2000b843c749SSergey Zigachev 
2001b843c749SSergey Zigachev 	/* Apply avfs cks-off voltages to avoid the overshoot
2002b843c749SSergey Zigachev 	 * when switching to the highest sclk frequency
2003b843c749SSergey Zigachev 	 */
2004b843c749SSergey Zigachev 	if (data->apply_avfs_cks_off_voltage)
2005b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_ApplyAvfsCksOffVoltage);
2006b843c749SSergey Zigachev 
2007b843c749SSergey Zigachev 	return 0;
2008b843c749SSergey Zigachev }
2009b843c749SSergey Zigachev 
polaris10_thermal_setup_fan_table(struct pp_hwmgr * hwmgr)2010b843c749SSergey Zigachev static int polaris10_thermal_setup_fan_table(struct pp_hwmgr *hwmgr)
2011b843c749SSergey Zigachev {
2012b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
2013b843c749SSergey Zigachev 	SMU74_Discrete_FanTable fan_table = { FDO_MODE_HARDWARE };
2014b843c749SSergey Zigachev 	uint32_t duty100;
2015b843c749SSergey Zigachev 	uint32_t t_diff1, t_diff2, pwm_diff1, pwm_diff2;
2016b843c749SSergey Zigachev 	uint16_t fdo_min, slope1, slope2;
2017b843c749SSergey Zigachev 	uint32_t reference_clock;
2018b843c749SSergey Zigachev 	int res;
2019b843c749SSergey Zigachev 	uint64_t tmp64;
2020b843c749SSergey Zigachev 
2021b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan) {
2022b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2023b843c749SSergey Zigachev 			PHM_PlatformCaps_MicrocodeFanControl);
2024b843c749SSergey Zigachev 		return 0;
2025b843c749SSergey Zigachev 	}
2026b843c749SSergey Zigachev 
2027b843c749SSergey Zigachev 	if (smu_data->smu7_data.fan_table_start == 0) {
2028b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2029b843c749SSergey Zigachev 				PHM_PlatformCaps_MicrocodeFanControl);
2030b843c749SSergey Zigachev 		return 0;
2031b843c749SSergey Zigachev 	}
2032b843c749SSergey Zigachev 
2033b843c749SSergey Zigachev 	duty100 = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
2034b843c749SSergey Zigachev 			CG_FDO_CTRL1, FMAX_DUTY100);
2035b843c749SSergey Zigachev 
2036b843c749SSergey Zigachev 	if (duty100 == 0) {
2037b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2038b843c749SSergey Zigachev 				PHM_PlatformCaps_MicrocodeFanControl);
2039b843c749SSergey Zigachev 		return 0;
2040b843c749SSergey Zigachev 	}
2041b843c749SSergey Zigachev 
2042b843c749SSergey Zigachev 	/* use hardware fan control */
2043b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.use_hw_fan_control)
2044b843c749SSergey Zigachev 		return 0;
2045b843c749SSergey Zigachev 
2046b843c749SSergey Zigachev 	tmp64 = hwmgr->thermal_controller.advanceFanControlParameters.
2047b843c749SSergey Zigachev 			usPWMMin * duty100;
2048b843c749SSergey Zigachev 	do_div(tmp64, 10000);
2049b843c749SSergey Zigachev 	fdo_min = (uint16_t)tmp64;
2050b843c749SSergey Zigachev 
2051b843c749SSergey Zigachev 	t_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usTMed -
2052b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usTMin;
2053b843c749SSergey Zigachev 	t_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usTHigh -
2054b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usTMed;
2055b843c749SSergey Zigachev 
2056b843c749SSergey Zigachev 	pwm_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed -
2057b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usPWMMin;
2058b843c749SSergey Zigachev 	pwm_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMHigh -
2059b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed;
2060b843c749SSergey Zigachev 
2061b843c749SSergey Zigachev 	slope1 = (uint16_t)((50 + ((16 * duty100 * pwm_diff1) / t_diff1)) / 100);
2062b843c749SSergey Zigachev 	slope2 = (uint16_t)((50 + ((16 * duty100 * pwm_diff2) / t_diff2)) / 100);
2063b843c749SSergey Zigachev 
2064b843c749SSergey Zigachev 	fan_table.TempMin = cpu_to_be16((50 + hwmgr->
2065b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.usTMin) / 100);
2066b843c749SSergey Zigachev 	fan_table.TempMed = cpu_to_be16((50 + hwmgr->
2067b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.usTMed) / 100);
2068b843c749SSergey Zigachev 	fan_table.TempMax = cpu_to_be16((50 + hwmgr->
2069b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.usTMax) / 100);
2070b843c749SSergey Zigachev 
2071b843c749SSergey Zigachev 	fan_table.Slope1 = cpu_to_be16(slope1);
2072b843c749SSergey Zigachev 	fan_table.Slope2 = cpu_to_be16(slope2);
2073b843c749SSergey Zigachev 
2074b843c749SSergey Zigachev 	fan_table.FdoMin = cpu_to_be16(fdo_min);
2075b843c749SSergey Zigachev 
2076b843c749SSergey Zigachev 	fan_table.HystDown = cpu_to_be16(hwmgr->
2077b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.ucTHyst);
2078b843c749SSergey Zigachev 
2079b843c749SSergey Zigachev 	fan_table.HystUp = cpu_to_be16(1);
2080b843c749SSergey Zigachev 
2081b843c749SSergey Zigachev 	fan_table.HystSlope = cpu_to_be16(1);
2082b843c749SSergey Zigachev 
2083b843c749SSergey Zigachev 	fan_table.TempRespLim = cpu_to_be16(5);
2084b843c749SSergey Zigachev 
2085b843c749SSergey Zigachev 	reference_clock = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
2086b843c749SSergey Zigachev 
2087b843c749SSergey Zigachev 	fan_table.RefreshPeriod = cpu_to_be32((hwmgr->
2088b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.ulCycleDelay *
2089b843c749SSergey Zigachev 			reference_clock) / 1600);
2090b843c749SSergey Zigachev 
2091b843c749SSergey Zigachev 	fan_table.FdoMax = cpu_to_be16((uint16_t)duty100);
2092b843c749SSergey Zigachev 
2093b843c749SSergey Zigachev 	fan_table.TempSrc = (uint8_t)PHM_READ_VFPF_INDIRECT_FIELD(
2094b843c749SSergey Zigachev 			hwmgr->device, CGS_IND_REG__SMC,
2095b843c749SSergey Zigachev 			CG_MULT_THERMAL_CTRL, TEMP_SEL);
2096b843c749SSergey Zigachev 
2097b843c749SSergey Zigachev 	res = smu7_copy_bytes_to_smc(hwmgr, smu_data->smu7_data.fan_table_start,
2098b843c749SSergey Zigachev 			(uint8_t *)&fan_table, (uint32_t)sizeof(fan_table),
2099b843c749SSergey Zigachev 			SMC_RAM_END);
2100b843c749SSergey Zigachev 
2101b843c749SSergey Zigachev 	if (!res && hwmgr->thermal_controller.
2102b843c749SSergey Zigachev 			advanceFanControlParameters.ucMinimumPWMLimit)
2103b843c749SSergey Zigachev 		res = smum_send_msg_to_smc_with_parameter(hwmgr,
2104b843c749SSergey Zigachev 				PPSMC_MSG_SetFanMinPwm,
2105b843c749SSergey Zigachev 				hwmgr->thermal_controller.
2106b843c749SSergey Zigachev 				advanceFanControlParameters.ucMinimumPWMLimit);
2107b843c749SSergey Zigachev 
2108b843c749SSergey Zigachev 	if (!res && hwmgr->thermal_controller.
2109b843c749SSergey Zigachev 			advanceFanControlParameters.ulMinFanSCLKAcousticLimit)
2110b843c749SSergey Zigachev 		res = smum_send_msg_to_smc_with_parameter(hwmgr,
2111b843c749SSergey Zigachev 				PPSMC_MSG_SetFanSclkTarget,
2112b843c749SSergey Zigachev 				hwmgr->thermal_controller.
2113b843c749SSergey Zigachev 				advanceFanControlParameters.ulMinFanSCLKAcousticLimit);
2114b843c749SSergey Zigachev 
2115b843c749SSergey Zigachev 	if (res)
2116b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2117b843c749SSergey Zigachev 				PHM_PlatformCaps_MicrocodeFanControl);
2118b843c749SSergey Zigachev 
2119b843c749SSergey Zigachev 	return 0;
2120b843c749SSergey Zigachev }
2121b843c749SSergey Zigachev 
polaris10_update_uvd_smc_table(struct pp_hwmgr * hwmgr)2122b843c749SSergey Zigachev static int polaris10_update_uvd_smc_table(struct pp_hwmgr *hwmgr)
2123b843c749SSergey Zigachev {
2124b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
2125b843c749SSergey Zigachev 	uint32_t mm_boot_level_offset, mm_boot_level_value;
2126b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2127b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2128b843c749SSergey Zigachev 
2129b843c749SSergey Zigachev 	smu_data->smc_state_table.UvdBootLevel = 0;
2130b843c749SSergey Zigachev 	if (table_info->mm_dep_table->count > 0)
2131b843c749SSergey Zigachev 		smu_data->smc_state_table.UvdBootLevel =
2132b843c749SSergey Zigachev 				(uint8_t) (table_info->mm_dep_table->count - 1);
2133b843c749SSergey Zigachev 	mm_boot_level_offset = smu_data->smu7_data.dpm_table_start + offsetof(SMU74_Discrete_DpmTable,
2134b843c749SSergey Zigachev 						UvdBootLevel);
2135b843c749SSergey Zigachev 	mm_boot_level_offset /= 4;
2136b843c749SSergey Zigachev 	mm_boot_level_offset *= 4;
2137b843c749SSergey Zigachev 	mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
2138b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset);
2139b843c749SSergey Zigachev 	mm_boot_level_value &= 0x00FFFFFF;
2140b843c749SSergey Zigachev 	mm_boot_level_value |= smu_data->smc_state_table.UvdBootLevel << 24;
2141b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device,
2142b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
2143b843c749SSergey Zigachev 
2144b843c749SSergey Zigachev 	if (!phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2145b843c749SSergey Zigachev 			PHM_PlatformCaps_UVDDPM) ||
2146b843c749SSergey Zigachev 		phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2147b843c749SSergey Zigachev 			PHM_PlatformCaps_StablePState))
2148b843c749SSergey Zigachev 		smum_send_msg_to_smc_with_parameter(hwmgr,
2149b843c749SSergey Zigachev 				PPSMC_MSG_UVDDPM_SetEnabledMask,
2150b843c749SSergey Zigachev 				(uint32_t)(1 << smu_data->smc_state_table.UvdBootLevel));
2151b843c749SSergey Zigachev 	return 0;
2152b843c749SSergey Zigachev }
2153b843c749SSergey Zigachev 
polaris10_update_vce_smc_table(struct pp_hwmgr * hwmgr)2154b843c749SSergey Zigachev static int polaris10_update_vce_smc_table(struct pp_hwmgr *hwmgr)
2155b843c749SSergey Zigachev {
2156b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
2157b843c749SSergey Zigachev 	uint32_t mm_boot_level_offset, mm_boot_level_value;
2158b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2159b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2160b843c749SSergey Zigachev 
2161b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2162b843c749SSergey Zigachev 					PHM_PlatformCaps_StablePState))
2163b843c749SSergey Zigachev 		smu_data->smc_state_table.VceBootLevel =
2164b843c749SSergey Zigachev 			(uint8_t) (table_info->mm_dep_table->count - 1);
2165b843c749SSergey Zigachev 	else
2166b843c749SSergey Zigachev 		smu_data->smc_state_table.VceBootLevel = 0;
2167b843c749SSergey Zigachev 
2168b843c749SSergey Zigachev 	mm_boot_level_offset = smu_data->smu7_data.dpm_table_start +
2169b843c749SSergey Zigachev 					offsetof(SMU74_Discrete_DpmTable, VceBootLevel);
2170b843c749SSergey Zigachev 	mm_boot_level_offset /= 4;
2171b843c749SSergey Zigachev 	mm_boot_level_offset *= 4;
2172b843c749SSergey Zigachev 	mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
2173b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset);
2174b843c749SSergey Zigachev 	mm_boot_level_value &= 0xFF00FFFF;
2175b843c749SSergey Zigachev 	mm_boot_level_value |= smu_data->smc_state_table.VceBootLevel << 16;
2176b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device,
2177b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
2178b843c749SSergey Zigachev 
2179b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_StablePState))
2180b843c749SSergey Zigachev 		smum_send_msg_to_smc_with_parameter(hwmgr,
2181b843c749SSergey Zigachev 				PPSMC_MSG_VCEDPM_SetEnabledMask,
2182b843c749SSergey Zigachev 				(uint32_t)1 << smu_data->smc_state_table.VceBootLevel);
2183b843c749SSergey Zigachev 	return 0;
2184b843c749SSergey Zigachev }
2185b843c749SSergey Zigachev 
polaris10_update_bif_smc_table(struct pp_hwmgr * hwmgr)2186b843c749SSergey Zigachev static int polaris10_update_bif_smc_table(struct pp_hwmgr *hwmgr)
2187b843c749SSergey Zigachev {
2188b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
2189b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2190b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2191b843c749SSergey Zigachev 	struct phm_ppt_v1_pcie_table *pcie_table = table_info->pcie_table;
2192b843c749SSergey Zigachev 	int max_entry, i;
2193b843c749SSergey Zigachev 
2194b843c749SSergey Zigachev 	max_entry = (SMU74_MAX_LEVELS_LINK < pcie_table->count) ?
2195b843c749SSergey Zigachev 						SMU74_MAX_LEVELS_LINK :
2196b843c749SSergey Zigachev 						pcie_table->count;
2197b843c749SSergey Zigachev 	/* Setup BIF_SCLK levels */
2198b843c749SSergey Zigachev 	for (i = 0; i < max_entry; i++)
2199b843c749SSergey Zigachev 		smu_data->bif_sclk_table[i] = pcie_table->entries[i].pcie_sclk;
2200b843c749SSergey Zigachev 	return 0;
2201b843c749SSergey Zigachev }
2202b843c749SSergey Zigachev 
polaris10_update_smc_table(struct pp_hwmgr * hwmgr,uint32_t type)2203b843c749SSergey Zigachev static int polaris10_update_smc_table(struct pp_hwmgr *hwmgr, uint32_t type)
2204b843c749SSergey Zigachev {
2205b843c749SSergey Zigachev 	switch (type) {
2206b843c749SSergey Zigachev 	case SMU_UVD_TABLE:
2207b843c749SSergey Zigachev 		polaris10_update_uvd_smc_table(hwmgr);
2208b843c749SSergey Zigachev 		break;
2209b843c749SSergey Zigachev 	case SMU_VCE_TABLE:
2210b843c749SSergey Zigachev 		polaris10_update_vce_smc_table(hwmgr);
2211b843c749SSergey Zigachev 		break;
2212b843c749SSergey Zigachev 	case SMU_BIF_TABLE:
2213b843c749SSergey Zigachev 		polaris10_update_bif_smc_table(hwmgr);
2214b843c749SSergey Zigachev 	default:
2215b843c749SSergey Zigachev 		break;
2216b843c749SSergey Zigachev 	}
2217b843c749SSergey Zigachev 	return 0;
2218b843c749SSergey Zigachev }
2219b843c749SSergey Zigachev 
polaris10_update_sclk_threshold(struct pp_hwmgr * hwmgr)2220b843c749SSergey Zigachev static int polaris10_update_sclk_threshold(struct pp_hwmgr *hwmgr)
2221b843c749SSergey Zigachev {
2222b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2223b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
2224b843c749SSergey Zigachev 
2225b843c749SSergey Zigachev 	int result = 0;
2226b843c749SSergey Zigachev 	uint32_t low_sclk_interrupt_threshold = 0;
2227b843c749SSergey Zigachev 
2228b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2229b843c749SSergey Zigachev 			PHM_PlatformCaps_SclkThrottleLowNotification)
2230b843c749SSergey Zigachev 		&& (data->low_sclk_interrupt_threshold != 0)) {
2231b843c749SSergey Zigachev 		low_sclk_interrupt_threshold =
2232b843c749SSergey Zigachev 				data->low_sclk_interrupt_threshold;
2233b843c749SSergey Zigachev 
2234b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(low_sclk_interrupt_threshold);
2235b843c749SSergey Zigachev 
2236b843c749SSergey Zigachev 		result = smu7_copy_bytes_to_smc(
2237b843c749SSergey Zigachev 				hwmgr,
2238b843c749SSergey Zigachev 				smu_data->smu7_data.dpm_table_start +
2239b843c749SSergey Zigachev 				offsetof(SMU74_Discrete_DpmTable,
2240b843c749SSergey Zigachev 					LowSclkInterruptThreshold),
2241b843c749SSergey Zigachev 				(uint8_t *)&low_sclk_interrupt_threshold,
2242b843c749SSergey Zigachev 				sizeof(uint32_t),
2243b843c749SSergey Zigachev 				SMC_RAM_END);
2244b843c749SSergey Zigachev 	}
2245b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((result == 0),
2246b843c749SSergey Zigachev 			"Failed to update SCLK threshold!", return result);
2247b843c749SSergey Zigachev 
2248b843c749SSergey Zigachev 	result = polaris10_program_mem_timing_parameters(hwmgr);
2249b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((result == 0),
2250b843c749SSergey Zigachev 			"Failed to program memory timing parameters!",
2251b843c749SSergey Zigachev 			);
2252b843c749SSergey Zigachev 
2253b843c749SSergey Zigachev 	return result;
2254b843c749SSergey Zigachev }
2255b843c749SSergey Zigachev 
polaris10_get_offsetof(uint32_t type,uint32_t member)2256b843c749SSergey Zigachev static uint32_t polaris10_get_offsetof(uint32_t type, uint32_t member)
2257b843c749SSergey Zigachev {
2258b843c749SSergey Zigachev 	switch (type) {
2259b843c749SSergey Zigachev 	case SMU_SoftRegisters:
2260b843c749SSergey Zigachev 		switch (member) {
2261b843c749SSergey Zigachev 		case HandshakeDisables:
2262b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, HandshakeDisables);
2263b843c749SSergey Zigachev 		case VoltageChangeTimeout:
2264b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, VoltageChangeTimeout);
2265b843c749SSergey Zigachev 		case AverageGraphicsActivity:
2266b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, AverageGraphicsActivity);
2267b843c749SSergey Zigachev 		case PreVBlankGap:
2268b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, PreVBlankGap);
2269b843c749SSergey Zigachev 		case VBlankTimeout:
2270b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, VBlankTimeout);
2271b843c749SSergey Zigachev 		case UcodeLoadStatus:
2272b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, UcodeLoadStatus);
2273b843c749SSergey Zigachev 		case DRAM_LOG_ADDR_H:
2274b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, DRAM_LOG_ADDR_H);
2275b843c749SSergey Zigachev 		case DRAM_LOG_ADDR_L:
2276b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, DRAM_LOG_ADDR_L);
2277b843c749SSergey Zigachev 		case DRAM_LOG_PHY_ADDR_H:
2278b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, DRAM_LOG_PHY_ADDR_H);
2279b843c749SSergey Zigachev 		case DRAM_LOG_PHY_ADDR_L:
2280b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, DRAM_LOG_PHY_ADDR_L);
2281b843c749SSergey Zigachev 		case DRAM_LOG_BUFF_SIZE:
2282b843c749SSergey Zigachev 			return offsetof(SMU74_SoftRegisters, DRAM_LOG_BUFF_SIZE);
2283b843c749SSergey Zigachev 		}
2284b843c749SSergey Zigachev 	case SMU_Discrete_DpmTable:
2285b843c749SSergey Zigachev 		switch (member) {
2286b843c749SSergey Zigachev 		case UvdBootLevel:
2287b843c749SSergey Zigachev 			return offsetof(SMU74_Discrete_DpmTable, UvdBootLevel);
2288b843c749SSergey Zigachev 		case VceBootLevel:
2289b843c749SSergey Zigachev 			return offsetof(SMU74_Discrete_DpmTable, VceBootLevel);
2290b843c749SSergey Zigachev 		case LowSclkInterruptThreshold:
2291b843c749SSergey Zigachev 			return offsetof(SMU74_Discrete_DpmTable, LowSclkInterruptThreshold);
2292b843c749SSergey Zigachev 		}
2293b843c749SSergey Zigachev 	}
2294b843c749SSergey Zigachev 	pr_warn("can't get the offset of type %x member %x\n", type, member);
2295b843c749SSergey Zigachev 	return 0;
2296b843c749SSergey Zigachev }
2297b843c749SSergey Zigachev 
polaris10_get_mac_definition(uint32_t value)2298b843c749SSergey Zigachev static uint32_t polaris10_get_mac_definition(uint32_t value)
2299b843c749SSergey Zigachev {
2300b843c749SSergey Zigachev 	switch (value) {
2301b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_GRAPHICS:
2302b843c749SSergey Zigachev 		return SMU74_MAX_LEVELS_GRAPHICS;
2303b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_MEMORY:
2304b843c749SSergey Zigachev 		return SMU74_MAX_LEVELS_MEMORY;
2305b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_LINK:
2306b843c749SSergey Zigachev 		return SMU74_MAX_LEVELS_LINK;
2307b843c749SSergey Zigachev 	case SMU_MAX_ENTRIES_SMIO:
2308b843c749SSergey Zigachev 		return SMU74_MAX_ENTRIES_SMIO;
2309b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDC:
2310b843c749SSergey Zigachev 		return SMU74_MAX_LEVELS_VDDC;
2311b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDGFX:
2312b843c749SSergey Zigachev 		return SMU74_MAX_LEVELS_VDDGFX;
2313b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDCI:
2314b843c749SSergey Zigachev 		return SMU74_MAX_LEVELS_VDDCI;
2315b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_MVDD:
2316b843c749SSergey Zigachev 		return SMU74_MAX_LEVELS_MVDD;
2317b843c749SSergey Zigachev 	case SMU_UVD_MCLK_HANDSHAKE_DISABLE:
2318b843c749SSergey Zigachev 		return SMU7_UVD_MCLK_HANDSHAKE_DISABLE;
2319b843c749SSergey Zigachev 	}
2320b843c749SSergey Zigachev 
2321b843c749SSergey Zigachev 	pr_warn("can't get the mac of %x\n", value);
2322b843c749SSergey Zigachev 	return 0;
2323b843c749SSergey Zigachev }
2324b843c749SSergey Zigachev 
polaris10_process_firmware_header(struct pp_hwmgr * hwmgr)2325b843c749SSergey Zigachev static int polaris10_process_firmware_header(struct pp_hwmgr *hwmgr)
2326b843c749SSergey Zigachev {
2327b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smu_backend);
2328b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2329b843c749SSergey Zigachev 	uint32_t tmp;
2330b843c749SSergey Zigachev 	int result;
2331b843c749SSergey Zigachev 	bool error = false;
2332b843c749SSergey Zigachev 
2333b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2334b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2335b843c749SSergey Zigachev 			offsetof(SMU74_Firmware_Header, DpmTable),
2336b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2337b843c749SSergey Zigachev 
2338b843c749SSergey Zigachev 	if (0 == result)
2339b843c749SSergey Zigachev 		smu_data->smu7_data.dpm_table_start = tmp;
2340b843c749SSergey Zigachev 
2341b843c749SSergey Zigachev 	error |= (0 != result);
2342b843c749SSergey Zigachev 
2343b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2344b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2345b843c749SSergey Zigachev 			offsetof(SMU74_Firmware_Header, SoftRegisters),
2346b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2347b843c749SSergey Zigachev 
2348b843c749SSergey Zigachev 	if (!result) {
2349b843c749SSergey Zigachev 		data->soft_regs_start = tmp;
2350b843c749SSergey Zigachev 		smu_data->smu7_data.soft_regs_start = tmp;
2351b843c749SSergey Zigachev 	}
2352b843c749SSergey Zigachev 
2353b843c749SSergey Zigachev 	error |= (0 != result);
2354b843c749SSergey Zigachev 
2355b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2356b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2357b843c749SSergey Zigachev 			offsetof(SMU74_Firmware_Header, mcRegisterTable),
2358b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2359b843c749SSergey Zigachev 
2360b843c749SSergey Zigachev 	if (!result)
2361b843c749SSergey Zigachev 		smu_data->smu7_data.mc_reg_table_start = tmp;
2362b843c749SSergey Zigachev 
2363b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2364b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2365b843c749SSergey Zigachev 			offsetof(SMU74_Firmware_Header, FanTable),
2366b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2367b843c749SSergey Zigachev 
2368b843c749SSergey Zigachev 	if (!result)
2369b843c749SSergey Zigachev 		smu_data->smu7_data.fan_table_start = tmp;
2370b843c749SSergey Zigachev 
2371b843c749SSergey Zigachev 	error |= (0 != result);
2372b843c749SSergey Zigachev 
2373b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2374b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2375b843c749SSergey Zigachev 			offsetof(SMU74_Firmware_Header, mcArbDramTimingTable),
2376b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2377b843c749SSergey Zigachev 
2378b843c749SSergey Zigachev 	if (!result)
2379b843c749SSergey Zigachev 		smu_data->smu7_data.arb_table_start = tmp;
2380b843c749SSergey Zigachev 
2381b843c749SSergey Zigachev 	error |= (0 != result);
2382b843c749SSergey Zigachev 
2383b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2384b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2385b843c749SSergey Zigachev 			offsetof(SMU74_Firmware_Header, Version),
2386b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2387b843c749SSergey Zigachev 
2388b843c749SSergey Zigachev 	if (!result)
2389b843c749SSergey Zigachev 		hwmgr->microcode_version_info.SMC = tmp;
2390b843c749SSergey Zigachev 
2391b843c749SSergey Zigachev 	error |= (0 != result);
2392b843c749SSergey Zigachev 
2393b843c749SSergey Zigachev 	return error ? -1 : 0;
2394b843c749SSergey Zigachev }
2395b843c749SSergey Zigachev 
polaris10_is_dpm_running(struct pp_hwmgr * hwmgr)2396b843c749SSergey Zigachev static bool polaris10_is_dpm_running(struct pp_hwmgr *hwmgr)
2397b843c749SSergey Zigachev {
2398b843c749SSergey Zigachev 	return (1 == PHM_READ_INDIRECT_FIELD(hwmgr->device,
2399b843c749SSergey Zigachev 			CGS_IND_REG__SMC, FEATURE_STATUS, VOLTAGE_CONTROLLER_ON))
2400b843c749SSergey Zigachev 			? true : false;
2401b843c749SSergey Zigachev }
2402b843c749SSergey Zigachev 
polaris10_update_dpm_settings(struct pp_hwmgr * hwmgr,void * profile_setting)2403b843c749SSergey Zigachev static int polaris10_update_dpm_settings(struct pp_hwmgr *hwmgr,
2404b843c749SSergey Zigachev 				void *profile_setting)
2405b843c749SSergey Zigachev {
2406b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2407b843c749SSergey Zigachev 	struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)
2408b843c749SSergey Zigachev 			(hwmgr->smu_backend);
2409b843c749SSergey Zigachev 	struct profile_mode_setting *setting;
2410b843c749SSergey Zigachev 	struct SMU74_Discrete_GraphicsLevel *levels =
2411b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsLevel;
2412b843c749SSergey Zigachev 	uint32_t array = smu_data->smu7_data.dpm_table_start +
2413b843c749SSergey Zigachev 			offsetof(SMU74_Discrete_DpmTable, GraphicsLevel);
2414b843c749SSergey Zigachev 
2415b843c749SSergey Zigachev 	uint32_t mclk_array = smu_data->smu7_data.dpm_table_start +
2416b843c749SSergey Zigachev 			offsetof(SMU74_Discrete_DpmTable, MemoryLevel);
2417b843c749SSergey Zigachev 	struct SMU74_Discrete_MemoryLevel *mclk_levels =
2418b843c749SSergey Zigachev 			smu_data->smc_state_table.MemoryLevel;
2419b843c749SSergey Zigachev 	uint32_t i;
2420b843c749SSergey Zigachev 	uint32_t offset, up_hyst_offset, down_hyst_offset, clk_activity_offset, tmp;
2421b843c749SSergey Zigachev 
2422b843c749SSergey Zigachev 	if (profile_setting == NULL)
2423b843c749SSergey Zigachev 		return -EINVAL;
2424b843c749SSergey Zigachev 
2425b843c749SSergey Zigachev 	setting = (struct profile_mode_setting *)profile_setting;
2426b843c749SSergey Zigachev 
2427b843c749SSergey Zigachev 	if (setting->bupdate_sclk) {
2428b843c749SSergey Zigachev 		if (!data->sclk_dpm_key_disabled)
2429b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_SCLKDPM_FreezeLevel);
2430b843c749SSergey Zigachev 		for (i = 0; i < smu_data->smc_state_table.GraphicsDpmLevelCount; i++) {
2431b843c749SSergey Zigachev 			if (levels[i].ActivityLevel !=
2432b843c749SSergey Zigachev 				cpu_to_be16(setting->sclk_activity)) {
2433b843c749SSergey Zigachev 				levels[i].ActivityLevel = cpu_to_be16(setting->sclk_activity);
2434b843c749SSergey Zigachev 
2435b843c749SSergey Zigachev 				clk_activity_offset = array + (sizeof(SMU74_Discrete_GraphicsLevel) * i)
2436b843c749SSergey Zigachev 						+ offsetof(SMU74_Discrete_GraphicsLevel, ActivityLevel);
2437b843c749SSergey Zigachev 				offset = clk_activity_offset & ~0x3;
2438b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2439b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(clk_activity_offset, tmp, levels[i].ActivityLevel, sizeof(uint16_t));
2440b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2441b843c749SSergey Zigachev 
2442b843c749SSergey Zigachev 			}
2443b843c749SSergey Zigachev 			if (levels[i].UpHyst != setting->sclk_up_hyst ||
2444b843c749SSergey Zigachev 				levels[i].DownHyst != setting->sclk_down_hyst) {
2445b843c749SSergey Zigachev 				levels[i].UpHyst = setting->sclk_up_hyst;
2446b843c749SSergey Zigachev 				levels[i].DownHyst = setting->sclk_down_hyst;
2447b843c749SSergey Zigachev 				up_hyst_offset = array + (sizeof(SMU74_Discrete_GraphicsLevel) * i)
2448b843c749SSergey Zigachev 						+ offsetof(SMU74_Discrete_GraphicsLevel, UpHyst);
2449b843c749SSergey Zigachev 				down_hyst_offset = array + (sizeof(SMU74_Discrete_GraphicsLevel) * i)
2450b843c749SSergey Zigachev 						+ offsetof(SMU74_Discrete_GraphicsLevel, DownHyst);
2451b843c749SSergey Zigachev 				offset = up_hyst_offset & ~0x3;
2452b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2453b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(up_hyst_offset, tmp, levels[i].UpHyst, sizeof(uint8_t));
2454b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(down_hyst_offset, tmp, levels[i].DownHyst, sizeof(uint8_t));
2455b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2456b843c749SSergey Zigachev 			}
2457b843c749SSergey Zigachev 		}
2458b843c749SSergey Zigachev 		if (!data->sclk_dpm_key_disabled)
2459b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_SCLKDPM_UnfreezeLevel);
2460b843c749SSergey Zigachev 	}
2461b843c749SSergey Zigachev 
2462b843c749SSergey Zigachev 	if (setting->bupdate_mclk) {
2463b843c749SSergey Zigachev 		if (!data->mclk_dpm_key_disabled)
2464b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_MCLKDPM_FreezeLevel);
2465b843c749SSergey Zigachev 		for (i = 0; i < smu_data->smc_state_table.MemoryDpmLevelCount; i++) {
2466b843c749SSergey Zigachev 			if (mclk_levels[i].ActivityLevel !=
2467b843c749SSergey Zigachev 				cpu_to_be16(setting->mclk_activity)) {
2468b843c749SSergey Zigachev 				mclk_levels[i].ActivityLevel = cpu_to_be16(setting->mclk_activity);
2469b843c749SSergey Zigachev 
2470b843c749SSergey Zigachev 				clk_activity_offset = mclk_array + (sizeof(SMU74_Discrete_MemoryLevel) * i)
2471b843c749SSergey Zigachev 						+ offsetof(SMU74_Discrete_MemoryLevel, ActivityLevel);
2472b843c749SSergey Zigachev 				offset = clk_activity_offset & ~0x3;
2473b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2474b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(clk_activity_offset, tmp, mclk_levels[i].ActivityLevel, sizeof(uint16_t));
2475b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2476b843c749SSergey Zigachev 
2477b843c749SSergey Zigachev 			}
2478b843c749SSergey Zigachev 			if (mclk_levels[i].UpHyst != setting->mclk_up_hyst ||
2479b843c749SSergey Zigachev 				mclk_levels[i].DownHyst != setting->mclk_down_hyst) {
2480b843c749SSergey Zigachev 				mclk_levels[i].UpHyst = setting->mclk_up_hyst;
2481b843c749SSergey Zigachev 				mclk_levels[i].DownHyst = setting->mclk_down_hyst;
2482b843c749SSergey Zigachev 				up_hyst_offset = mclk_array + (sizeof(SMU74_Discrete_MemoryLevel) * i)
2483b843c749SSergey Zigachev 						+ offsetof(SMU74_Discrete_MemoryLevel, UpHyst);
2484b843c749SSergey Zigachev 				down_hyst_offset = mclk_array + (sizeof(SMU74_Discrete_MemoryLevel) * i)
2485b843c749SSergey Zigachev 						+ offsetof(SMU74_Discrete_MemoryLevel, DownHyst);
2486b843c749SSergey Zigachev 				offset = up_hyst_offset & ~0x3;
2487b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2488b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(up_hyst_offset, tmp, mclk_levels[i].UpHyst, sizeof(uint8_t));
2489b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(down_hyst_offset, tmp, mclk_levels[i].DownHyst, sizeof(uint8_t));
2490b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2491b843c749SSergey Zigachev 			}
2492b843c749SSergey Zigachev 		}
2493b843c749SSergey Zigachev 		if (!data->mclk_dpm_key_disabled)
2494b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_MCLKDPM_UnfreezeLevel);
2495b843c749SSergey Zigachev 	}
2496b843c749SSergey Zigachev 	return 0;
2497b843c749SSergey Zigachev }
2498b843c749SSergey Zigachev 
2499b843c749SSergey Zigachev const struct pp_smumgr_func polaris10_smu_funcs = {
2500b843c749SSergey Zigachev 	.smu_init = polaris10_smu_init,
2501b843c749SSergey Zigachev 	.smu_fini = smu7_smu_fini,
2502b843c749SSergey Zigachev 	.start_smu = polaris10_start_smu,
2503b843c749SSergey Zigachev 	.check_fw_load_finish = smu7_check_fw_load_finish,
2504b843c749SSergey Zigachev 	.request_smu_load_fw = smu7_reload_firmware,
2505b843c749SSergey Zigachev 	.request_smu_load_specific_fw = NULL,
2506b843c749SSergey Zigachev 	.send_msg_to_smc = smu7_send_msg_to_smc,
2507b843c749SSergey Zigachev 	.send_msg_to_smc_with_parameter = smu7_send_msg_to_smc_with_parameter,
2508b843c749SSergey Zigachev 	.download_pptable_settings = NULL,
2509b843c749SSergey Zigachev 	.upload_pptable_settings = NULL,
2510b843c749SSergey Zigachev 	.update_smc_table = polaris10_update_smc_table,
2511b843c749SSergey Zigachev 	.get_offsetof = polaris10_get_offsetof,
2512b843c749SSergey Zigachev 	.process_firmware_header = polaris10_process_firmware_header,
2513b843c749SSergey Zigachev 	.init_smc_table = polaris10_init_smc_table,
2514b843c749SSergey Zigachev 	.update_sclk_threshold = polaris10_update_sclk_threshold,
2515b843c749SSergey Zigachev 	.thermal_avfs_enable = polaris10_thermal_avfs_enable,
2516b843c749SSergey Zigachev 	.thermal_setup_fan_table = polaris10_thermal_setup_fan_table,
2517b843c749SSergey Zigachev 	.populate_all_graphic_levels = polaris10_populate_all_graphic_levels,
2518b843c749SSergey Zigachev 	.populate_all_memory_levels = polaris10_populate_all_memory_levels,
2519b843c749SSergey Zigachev 	.get_mac_definition = polaris10_get_mac_definition,
2520b843c749SSergey Zigachev 	.is_dpm_running = polaris10_is_dpm_running,
2521b843c749SSergey Zigachev 	.is_hw_avfs_present = polaris10_is_hw_avfs_present,
2522b843c749SSergey Zigachev 	.update_dpm_settings = polaris10_update_dpm_settings,
2523b843c749SSergey Zigachev };
2524