1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2015 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  */
23*b843c749SSergey Zigachev 
24*b843c749SSergey Zigachev #include "pp_debug.h"
25*b843c749SSergey Zigachev #include "smumgr.h"
26*b843c749SSergey Zigachev #include "smu7_dyn_defaults.h"
27*b843c749SSergey Zigachev #include "smu73.h"
28*b843c749SSergey Zigachev #include "smu_ucode_xfer_vi.h"
29*b843c749SSergey Zigachev #include "fiji_smumgr.h"
30*b843c749SSergey Zigachev #include "fiji_ppsmc.h"
31*b843c749SSergey Zigachev #include "smu73_discrete.h"
32*b843c749SSergey Zigachev #include "ppatomctrl.h"
33*b843c749SSergey Zigachev #include "smu/smu_7_1_3_d.h"
34*b843c749SSergey Zigachev #include "smu/smu_7_1_3_sh_mask.h"
35*b843c749SSergey Zigachev #include "gmc/gmc_8_1_d.h"
36*b843c749SSergey Zigachev #include "gmc/gmc_8_1_sh_mask.h"
37*b843c749SSergey Zigachev #include "oss/oss_3_0_d.h"
38*b843c749SSergey Zigachev #include "gca/gfx_8_0_d.h"
39*b843c749SSergey Zigachev #include "bif/bif_5_0_d.h"
40*b843c749SSergey Zigachev #include "bif/bif_5_0_sh_mask.h"
41*b843c749SSergey Zigachev #include "dce/dce_10_0_d.h"
42*b843c749SSergey Zigachev #include "dce/dce_10_0_sh_mask.h"
43*b843c749SSergey Zigachev #include "hardwaremanager.h"
44*b843c749SSergey Zigachev #include "cgs_common.h"
45*b843c749SSergey Zigachev #include "atombios.h"
46*b843c749SSergey Zigachev #include "pppcielanes.h"
47*b843c749SSergey Zigachev #include "hwmgr.h"
48*b843c749SSergey Zigachev #include "smu7_hwmgr.h"
49*b843c749SSergey Zigachev 
50*b843c749SSergey Zigachev 
51*b843c749SSergey Zigachev #define AVFS_EN_MSB                                        1568
52*b843c749SSergey Zigachev #define AVFS_EN_LSB                                        1568
53*b843c749SSergey Zigachev 
54*b843c749SSergey Zigachev #define FIJI_SMC_SIZE 0x20000
55*b843c749SSergey Zigachev 
56*b843c749SSergey Zigachev #define POWERTUNE_DEFAULT_SET_MAX    1
57*b843c749SSergey Zigachev #define VDDC_VDDCI_DELTA            300
58*b843c749SSergey Zigachev #define MC_CG_ARB_FREQ_F1           0x0b
59*b843c749SSergey Zigachev 
60*b843c749SSergey Zigachev /* [2.5%,~2.5%] Clock stretched is multiple of 2.5% vs
61*b843c749SSergey Zigachev  * not and [Fmin, Fmax, LDO_REFSEL, USE_FOR_LOW_FREQ]
62*b843c749SSergey Zigachev  */
63*b843c749SSergey Zigachev static const uint16_t fiji_clock_stretcher_lookup_table[2][4] = {
64*b843c749SSergey Zigachev 				{600, 1050, 3, 0}, {600, 1050, 6, 1} };
65*b843c749SSergey Zigachev 
66*b843c749SSergey Zigachev /* [FF, SS] type, [] 4 voltage ranges, and
67*b843c749SSergey Zigachev  * [Floor Freq, Boundary Freq, VID min , VID max]
68*b843c749SSergey Zigachev  */
69*b843c749SSergey Zigachev static const uint32_t fiji_clock_stretcher_ddt_table[2][4][4] = {
70*b843c749SSergey Zigachev 	{ {265, 529, 120, 128}, {325, 650, 96, 119}, {430, 860, 32, 95}, {0, 0, 0, 31} },
71*b843c749SSergey Zigachev 	{ {275, 550, 104, 112}, {319, 638, 96, 103}, {360, 720, 64, 95}, {384, 768, 32, 63} } };
72*b843c749SSergey Zigachev 
73*b843c749SSergey Zigachev /* [Use_For_Low_freq] value, [0%, 5%, 10%, 7.14%, 14.28%, 20%]
74*b843c749SSergey Zigachev  * (coming from PWR_CKS_CNTL.stretch_amount reg spec)
75*b843c749SSergey Zigachev  */
76*b843c749SSergey Zigachev static const uint8_t fiji_clock_stretch_amount_conversion[2][6] = {
77*b843c749SSergey Zigachev 				{0, 1, 3, 2, 4, 5}, {0, 2, 4, 5, 6, 5} };
78*b843c749SSergey Zigachev 
79*b843c749SSergey Zigachev static const struct fiji_pt_defaults fiji_power_tune_data_set_array[POWERTUNE_DEFAULT_SET_MAX] = {
80*b843c749SSergey Zigachev 		/*sviLoadLIneEn,  SviLoadLineVddC, TDC_VDDC_ThrottleReleaseLimitPerc */
81*b843c749SSergey Zigachev 		{1,               0xF,             0xFD,
82*b843c749SSergey Zigachev 		/* TDC_MAWt, TdcWaterfallCtl, DTEAmbientTempBase */
83*b843c749SSergey Zigachev 		0x19,        5,               45}
84*b843c749SSergey Zigachev };
85*b843c749SSergey Zigachev 
86*b843c749SSergey Zigachev static const struct SMU73_Discrete_GraphicsLevel avfs_graphics_level[8] = {
87*b843c749SSergey Zigachev 		/*  Min        Sclk       pcie     DeepSleep Activity  CgSpll      CgSpll    spllSpread  SpllSpread   CcPwr  CcPwr  Sclk   Display     Enabled     Enabled                       Voltage    Power */
88*b843c749SSergey Zigachev 		/* Voltage,  Frequency,  DpmLevel,  DivId,    Level,  FuncCntl3,  FuncCntl4,  Spectrum,   Spectrum2,  DynRm, DynRm1  Did, Watermark, ForActivity, ForThrottle, UpHyst, DownHyst, DownHyst, Throttle */
89*b843c749SSergey Zigachev 		{ 0x3c0fd047, 0x30750000,   0x00,     0x03,   0x1e00, 0x00200410, 0x87020000, 0x21680000, 0x0c000000,   0,      0,   0x16,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
90*b843c749SSergey Zigachev 		{ 0xa00fd047, 0x409c0000,   0x01,     0x04,   0x1e00, 0x00800510, 0x87020000, 0x21680000, 0x11000000,   0,      0,   0x16,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
91*b843c749SSergey Zigachev 		{ 0x0410d047, 0x50c30000,   0x01,     0x00,   0x1e00, 0x00600410, 0x87020000, 0x21680000, 0x0d000000,   0,      0,   0x0e,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
92*b843c749SSergey Zigachev 		{ 0x6810d047, 0x60ea0000,   0x01,     0x00,   0x1e00, 0x00800410, 0x87020000, 0x21680000, 0x0e000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
93*b843c749SSergey Zigachev 		{ 0xcc10d047, 0xe8fd0000,   0x01,     0x00,   0x1e00, 0x00e00410, 0x87020000, 0x21680000, 0x0f000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
94*b843c749SSergey Zigachev 		{ 0x3011d047, 0x70110100,   0x01,     0x00,   0x1e00, 0x00400510, 0x87020000, 0x21680000, 0x10000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
95*b843c749SSergey Zigachev 		{ 0x9411d047, 0xf8240100,   0x01,     0x00,   0x1e00, 0x00a00510, 0x87020000, 0x21680000, 0x11000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
96*b843c749SSergey Zigachev 		{ 0xf811d047, 0x80380100,   0x01,     0x00,   0x1e00, 0x00000610, 0x87020000, 0x21680000, 0x12000000,   0,      0,   0x0c,   0x01,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 }
97*b843c749SSergey Zigachev };
98*b843c749SSergey Zigachev 
fiji_start_smu_in_protection_mode(struct pp_hwmgr * hwmgr)99*b843c749SSergey Zigachev static int fiji_start_smu_in_protection_mode(struct pp_hwmgr *hwmgr)
100*b843c749SSergey Zigachev {
101*b843c749SSergey Zigachev 	int result = 0;
102*b843c749SSergey Zigachev 
103*b843c749SSergey Zigachev 	/* Wait for smc boot up */
104*b843c749SSergey Zigachev 	/* PHM_WAIT_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
105*b843c749SSergey Zigachev 		RCU_UC_EVENTS, boot_seq_done, 0); */
106*b843c749SSergey Zigachev 
107*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
108*b843c749SSergey Zigachev 			SMC_SYSCON_RESET_CNTL, rst_reg, 1);
109*b843c749SSergey Zigachev 
110*b843c749SSergey Zigachev 	result = smu7_upload_smu_firmware_image(hwmgr);
111*b843c749SSergey Zigachev 	if (result)
112*b843c749SSergey Zigachev 		return result;
113*b843c749SSergey Zigachev 
114*b843c749SSergey Zigachev 	/* Clear status */
115*b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
116*b843c749SSergey Zigachev 			ixSMU_STATUS, 0);
117*b843c749SSergey Zigachev 
118*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
119*b843c749SSergey Zigachev 			SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
120*b843c749SSergey Zigachev 
121*b843c749SSergey Zigachev 	/* De-assert reset */
122*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
123*b843c749SSergey Zigachev 			SMC_SYSCON_RESET_CNTL, rst_reg, 0);
124*b843c749SSergey Zigachev 
125*b843c749SSergey Zigachev 	/* Wait for ROM firmware to initialize interrupt hendler */
126*b843c749SSergey Zigachev 	/*SMUM_WAIT_VFPF_INDIRECT_REGISTER(hwmgr, SMC_IND,
127*b843c749SSergey Zigachev 			SMC_INTR_CNTL_MASK_0, 0x10040, 0xFFFFFFFF); */
128*b843c749SSergey Zigachev 
129*b843c749SSergey Zigachev 	/* Set SMU Auto Start */
130*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
131*b843c749SSergey Zigachev 			SMU_INPUT_DATA, AUTO_START, 1);
132*b843c749SSergey Zigachev 
133*b843c749SSergey Zigachev 	/* Clear firmware interrupt enable flag */
134*b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
135*b843c749SSergey Zigachev 			ixFIRMWARE_FLAGS, 0);
136*b843c749SSergey Zigachev 
137*b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND, RCU_UC_EVENTS,
138*b843c749SSergey Zigachev 			INTERRUPTS_ENABLED, 1);
139*b843c749SSergey Zigachev 
140*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmSMC_MSG_ARG_0, 0x20000);
141*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmSMC_MESSAGE_0, PPSMC_MSG_Test);
142*b843c749SSergey Zigachev 	PHM_WAIT_FIELD_UNEQUAL(hwmgr, SMC_RESP_0, SMC_RESP, 0);
143*b843c749SSergey Zigachev 
144*b843c749SSergey Zigachev 	/* Wait for done bit to be set */
145*b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
146*b843c749SSergey Zigachev 			SMU_STATUS, SMU_DONE, 0);
147*b843c749SSergey Zigachev 
148*b843c749SSergey Zigachev 	/* Check pass/failed indicator */
149*b843c749SSergey Zigachev 	if (PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
150*b843c749SSergey Zigachev 			SMU_STATUS, SMU_PASS) != 1) {
151*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
152*b843c749SSergey Zigachev 				"SMU Firmware start failed!", return -1);
153*b843c749SSergey Zigachev 	}
154*b843c749SSergey Zigachev 
155*b843c749SSergey Zigachev 	/* Wait for firmware to initialize */
156*b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND,
157*b843c749SSergey Zigachev 			FIRMWARE_FLAGS, INTERRUPTS_ENABLED, 1);
158*b843c749SSergey Zigachev 
159*b843c749SSergey Zigachev 	return result;
160*b843c749SSergey Zigachev }
161*b843c749SSergey Zigachev 
fiji_start_smu_in_non_protection_mode(struct pp_hwmgr * hwmgr)162*b843c749SSergey Zigachev static int fiji_start_smu_in_non_protection_mode(struct pp_hwmgr *hwmgr)
163*b843c749SSergey Zigachev {
164*b843c749SSergey Zigachev 	int result = 0;
165*b843c749SSergey Zigachev 
166*b843c749SSergey Zigachev 	/* wait for smc boot up */
167*b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
168*b843c749SSergey Zigachev 			RCU_UC_EVENTS, boot_seq_done, 0);
169*b843c749SSergey Zigachev 
170*b843c749SSergey Zigachev 	/* Clear firmware interrupt enable flag */
171*b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
172*b843c749SSergey Zigachev 			ixFIRMWARE_FLAGS, 0);
173*b843c749SSergey Zigachev 
174*b843c749SSergey Zigachev 	/* Assert reset */
175*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
176*b843c749SSergey Zigachev 			SMC_SYSCON_RESET_CNTL, rst_reg, 1);
177*b843c749SSergey Zigachev 
178*b843c749SSergey Zigachev 	result = smu7_upload_smu_firmware_image(hwmgr);
179*b843c749SSergey Zigachev 	if (result)
180*b843c749SSergey Zigachev 		return result;
181*b843c749SSergey Zigachev 
182*b843c749SSergey Zigachev 	/* Set smc instruct start point at 0x0 */
183*b843c749SSergey Zigachev 	smu7_program_jump_on_start(hwmgr);
184*b843c749SSergey Zigachev 
185*b843c749SSergey Zigachev 	/* Enable clock */
186*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
187*b843c749SSergey Zigachev 			SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
188*b843c749SSergey Zigachev 
189*b843c749SSergey Zigachev 	/* De-assert reset */
190*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
191*b843c749SSergey Zigachev 			SMC_SYSCON_RESET_CNTL, rst_reg, 0);
192*b843c749SSergey Zigachev 
193*b843c749SSergey Zigachev 	/* Wait for firmware to initialize */
194*b843c749SSergey Zigachev 	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND,
195*b843c749SSergey Zigachev 			FIRMWARE_FLAGS, INTERRUPTS_ENABLED, 1);
196*b843c749SSergey Zigachev 
197*b843c749SSergey Zigachev 	return result;
198*b843c749SSergey Zigachev }
199*b843c749SSergey Zigachev 
fiji_start_avfs_btc(struct pp_hwmgr * hwmgr)200*b843c749SSergey Zigachev static int fiji_start_avfs_btc(struct pp_hwmgr *hwmgr)
201*b843c749SSergey Zigachev {
202*b843c749SSergey Zigachev 	int result = 0;
203*b843c749SSergey Zigachev 	struct smu7_smumgr *smu_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
204*b843c749SSergey Zigachev 
205*b843c749SSergey Zigachev 	if (0 != smu_data->avfs_btc_param) {
206*b843c749SSergey Zigachev 		if (0 != smu7_send_msg_to_smc_with_parameter(hwmgr,
207*b843c749SSergey Zigachev 				PPSMC_MSG_PerformBtc, smu_data->avfs_btc_param)) {
208*b843c749SSergey Zigachev 			pr_info("[AVFS][Fiji_PerformBtc] PerformBTC SMU msg failed");
209*b843c749SSergey Zigachev 			result = -EINVAL;
210*b843c749SSergey Zigachev 		}
211*b843c749SSergey Zigachev 	}
212*b843c749SSergey Zigachev 	/* Soft-Reset to reset the engine before loading uCode */
213*b843c749SSergey Zigachev 	 /* halt */
214*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmCP_MEC_CNTL, 0x50000000);
215*b843c749SSergey Zigachev 	/* reset everything */
216*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmGRBM_SOFT_RESET, 0xffffffff);
217*b843c749SSergey Zigachev 	/* clear reset */
218*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmGRBM_SOFT_RESET, 0);
219*b843c749SSergey Zigachev 
220*b843c749SSergey Zigachev 	return result;
221*b843c749SSergey Zigachev }
222*b843c749SSergey Zigachev 
fiji_setup_graphics_level_structure(struct pp_hwmgr * hwmgr)223*b843c749SSergey Zigachev static int fiji_setup_graphics_level_structure(struct pp_hwmgr *hwmgr)
224*b843c749SSergey Zigachev {
225*b843c749SSergey Zigachev 	int32_t vr_config;
226*b843c749SSergey Zigachev 	uint32_t table_start;
227*b843c749SSergey Zigachev 	uint32_t level_addr, vr_config_addr;
228*b843c749SSergey Zigachev 	uint32_t level_size = sizeof(avfs_graphics_level);
229*b843c749SSergey Zigachev 
230*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_read_smc_sram_dword(hwmgr,
231*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
232*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, DpmTable),
233*b843c749SSergey Zigachev 			&table_start, 0x40000),
234*b843c749SSergey Zigachev 			"[AVFS][Fiji_SetupGfxLvlStruct] SMU could not "
235*b843c749SSergey Zigachev 			"communicate starting address of DPM table",
236*b843c749SSergey Zigachev 			return -1;);
237*b843c749SSergey Zigachev 
238*b843c749SSergey Zigachev 	/* Default value for vr_config =
239*b843c749SSergey Zigachev 	 * VR_MERGED_WITH_VDDC + VR_STATIC_VOLTAGE(VDDCI) */
240*b843c749SSergey Zigachev 	vr_config = 0x01000500;   /* Real value:0x50001 */
241*b843c749SSergey Zigachev 
242*b843c749SSergey Zigachev 	vr_config_addr = table_start +
243*b843c749SSergey Zigachev 			offsetof(SMU73_Discrete_DpmTable, VRConfig);
244*b843c749SSergey Zigachev 
245*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, vr_config_addr,
246*b843c749SSergey Zigachev 			(uint8_t *)&vr_config, sizeof(int32_t), 0x40000),
247*b843c749SSergey Zigachev 			"[AVFS][Fiji_SetupGfxLvlStruct] Problems copying "
248*b843c749SSergey Zigachev 			"vr_config value over to SMC",
249*b843c749SSergey Zigachev 			return -1;);
250*b843c749SSergey Zigachev 
251*b843c749SSergey Zigachev 	level_addr = table_start + offsetof(SMU73_Discrete_DpmTable, GraphicsLevel);
252*b843c749SSergey Zigachev 
253*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, level_addr,
254*b843c749SSergey Zigachev 			(uint8_t *)(&avfs_graphics_level), level_size, 0x40000),
255*b843c749SSergey Zigachev 			"[AVFS][Fiji_SetupGfxLvlStruct] Copying of DPM table failed!",
256*b843c749SSergey Zigachev 			return -1;);
257*b843c749SSergey Zigachev 
258*b843c749SSergey Zigachev 	return 0;
259*b843c749SSergey Zigachev }
260*b843c749SSergey Zigachev 
fiji_avfs_event_mgr(struct pp_hwmgr * hwmgr)261*b843c749SSergey Zigachev static int fiji_avfs_event_mgr(struct pp_hwmgr *hwmgr)
262*b843c749SSergey Zigachev {
263*b843c749SSergey Zigachev 	if (!hwmgr->avfs_supported)
264*b843c749SSergey Zigachev 		return 0;
265*b843c749SSergey Zigachev 
266*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == fiji_setup_graphics_level_structure(hwmgr),
267*b843c749SSergey Zigachev 			"[AVFS][fiji_avfs_event_mgr] Could not Copy Graphics Level"
268*b843c749SSergey Zigachev 			" table over to SMU",
269*b843c749SSergey Zigachev 			return -EINVAL);
270*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == smu7_setup_pwr_virus(hwmgr),
271*b843c749SSergey Zigachev 			"[AVFS][fiji_avfs_event_mgr] Could not setup "
272*b843c749SSergey Zigachev 			"Pwr Virus for AVFS ",
273*b843c749SSergey Zigachev 			return -EINVAL);
274*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == fiji_start_avfs_btc(hwmgr),
275*b843c749SSergey Zigachev 			"[AVFS][fiji_avfs_event_mgr] Failure at "
276*b843c749SSergey Zigachev 			"fiji_start_avfs_btc. AVFS Disabled",
277*b843c749SSergey Zigachev 			return -EINVAL);
278*b843c749SSergey Zigachev 
279*b843c749SSergey Zigachev 	return 0;
280*b843c749SSergey Zigachev }
281*b843c749SSergey Zigachev 
fiji_start_smu(struct pp_hwmgr * hwmgr)282*b843c749SSergey Zigachev static int fiji_start_smu(struct pp_hwmgr *hwmgr)
283*b843c749SSergey Zigachev {
284*b843c749SSergey Zigachev 	int result = 0;
285*b843c749SSergey Zigachev 	struct fiji_smumgr *priv = (struct fiji_smumgr *)(hwmgr->smu_backend);
286*b843c749SSergey Zigachev 
287*b843c749SSergey Zigachev 	/* Only start SMC if SMC RAM is not running */
288*b843c749SSergey Zigachev 	if (!smu7_is_smc_ram_running(hwmgr) && hwmgr->not_vf) {
289*b843c749SSergey Zigachev 		/* Check if SMU is running in protected mode */
290*b843c749SSergey Zigachev 		if (0 == PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device,
291*b843c749SSergey Zigachev 				CGS_IND_REG__SMC,
292*b843c749SSergey Zigachev 				SMU_FIRMWARE, SMU_MODE)) {
293*b843c749SSergey Zigachev 			result = fiji_start_smu_in_non_protection_mode(hwmgr);
294*b843c749SSergey Zigachev 			if (result)
295*b843c749SSergey Zigachev 				return result;
296*b843c749SSergey Zigachev 		} else {
297*b843c749SSergey Zigachev 			result = fiji_start_smu_in_protection_mode(hwmgr);
298*b843c749SSergey Zigachev 			if (result)
299*b843c749SSergey Zigachev 				return result;
300*b843c749SSergey Zigachev 		}
301*b843c749SSergey Zigachev 		if (fiji_avfs_event_mgr(hwmgr))
302*b843c749SSergey Zigachev 			hwmgr->avfs_supported = false;
303*b843c749SSergey Zigachev 	}
304*b843c749SSergey Zigachev 
305*b843c749SSergey Zigachev 	/* To initialize all clock gating before RLC loaded and running.*/
306*b843c749SSergey Zigachev 	amdgpu_device_ip_set_clockgating_state(hwmgr->adev,
307*b843c749SSergey Zigachev 			AMD_IP_BLOCK_TYPE_GFX, AMD_CG_STATE_GATE);
308*b843c749SSergey Zigachev 	amdgpu_device_ip_set_clockgating_state(hwmgr->adev,
309*b843c749SSergey Zigachev 			AMD_IP_BLOCK_TYPE_GMC, AMD_CG_STATE_GATE);
310*b843c749SSergey Zigachev 	amdgpu_device_ip_set_clockgating_state(hwmgr->adev,
311*b843c749SSergey Zigachev 			AMD_IP_BLOCK_TYPE_SDMA, AMD_CG_STATE_GATE);
312*b843c749SSergey Zigachev 	amdgpu_device_ip_set_clockgating_state(hwmgr->adev,
313*b843c749SSergey Zigachev 			AMD_IP_BLOCK_TYPE_COMMON, AMD_CG_STATE_GATE);
314*b843c749SSergey Zigachev 
315*b843c749SSergey Zigachev 	/* Setup SoftRegsStart here for register lookup in case
316*b843c749SSergey Zigachev 	 * DummyBackEnd is used and ProcessFirmwareHeader is not executed
317*b843c749SSergey Zigachev 	 */
318*b843c749SSergey Zigachev 	smu7_read_smc_sram_dword(hwmgr,
319*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
320*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, SoftRegisters),
321*b843c749SSergey Zigachev 			&(priv->smu7_data.soft_regs_start), 0x40000);
322*b843c749SSergey Zigachev 
323*b843c749SSergey Zigachev 	result = smu7_request_smu_load_fw(hwmgr);
324*b843c749SSergey Zigachev 
325*b843c749SSergey Zigachev 	return result;
326*b843c749SSergey Zigachev }
327*b843c749SSergey Zigachev 
fiji_is_hw_avfs_present(struct pp_hwmgr * hwmgr)328*b843c749SSergey Zigachev static bool fiji_is_hw_avfs_present(struct pp_hwmgr *hwmgr)
329*b843c749SSergey Zigachev {
330*b843c749SSergey Zigachev 
331*b843c749SSergey Zigachev 	uint32_t efuse = 0;
332*b843c749SSergey Zigachev 	uint32_t mask = (1 << ((AVFS_EN_MSB - AVFS_EN_LSB) + 1)) - 1;
333*b843c749SSergey Zigachev 
334*b843c749SSergey Zigachev 	if (!hwmgr->not_vf)
335*b843c749SSergey Zigachev 		return false;
336*b843c749SSergey Zigachev 
337*b843c749SSergey Zigachev 	if (!atomctrl_read_efuse(hwmgr, AVFS_EN_LSB, AVFS_EN_MSB,
338*b843c749SSergey Zigachev 			mask, &efuse)) {
339*b843c749SSergey Zigachev 		if (efuse)
340*b843c749SSergey Zigachev 			return true;
341*b843c749SSergey Zigachev 	}
342*b843c749SSergey Zigachev 	return false;
343*b843c749SSergey Zigachev }
344*b843c749SSergey Zigachev 
fiji_smu_init(struct pp_hwmgr * hwmgr)345*b843c749SSergey Zigachev static int fiji_smu_init(struct pp_hwmgr *hwmgr)
346*b843c749SSergey Zigachev {
347*b843c749SSergey Zigachev 	struct fiji_smumgr *fiji_priv = NULL;
348*b843c749SSergey Zigachev 
349*b843c749SSergey Zigachev 	fiji_priv = kzalloc(sizeof(struct fiji_smumgr), GFP_KERNEL);
350*b843c749SSergey Zigachev 
351*b843c749SSergey Zigachev 	if (fiji_priv == NULL)
352*b843c749SSergey Zigachev 		return -ENOMEM;
353*b843c749SSergey Zigachev 
354*b843c749SSergey Zigachev 	hwmgr->smu_backend = fiji_priv;
355*b843c749SSergey Zigachev 
356*b843c749SSergey Zigachev 	if (smu7_init(hwmgr)) {
357*b843c749SSergey Zigachev 		kfree(fiji_priv);
358*b843c749SSergey Zigachev 		return -EINVAL;
359*b843c749SSergey Zigachev 	}
360*b843c749SSergey Zigachev 
361*b843c749SSergey Zigachev 	return 0;
362*b843c749SSergey Zigachev }
363*b843c749SSergey Zigachev 
fiji_get_dependency_volt_by_clk(struct pp_hwmgr * hwmgr,struct phm_ppt_v1_clock_voltage_dependency_table * dep_table,uint32_t clock,uint32_t * voltage,uint32_t * mvdd)364*b843c749SSergey Zigachev static int fiji_get_dependency_volt_by_clk(struct pp_hwmgr *hwmgr,
365*b843c749SSergey Zigachev 		struct phm_ppt_v1_clock_voltage_dependency_table *dep_table,
366*b843c749SSergey Zigachev 		uint32_t clock, uint32_t *voltage, uint32_t *mvdd)
367*b843c749SSergey Zigachev {
368*b843c749SSergey Zigachev 	uint32_t i;
369*b843c749SSergey Zigachev 	uint16_t vddci;
370*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
371*b843c749SSergey Zigachev 	*voltage = *mvdd = 0;
372*b843c749SSergey Zigachev 
373*b843c749SSergey Zigachev 
374*b843c749SSergey Zigachev 	/* clock - voltage dependency table is empty table */
375*b843c749SSergey Zigachev 	if (dep_table->count == 0)
376*b843c749SSergey Zigachev 		return -EINVAL;
377*b843c749SSergey Zigachev 
378*b843c749SSergey Zigachev 	for (i = 0; i < dep_table->count; i++) {
379*b843c749SSergey Zigachev 		/* find first sclk bigger than request */
380*b843c749SSergey Zigachev 		if (dep_table->entries[i].clk >= clock) {
381*b843c749SSergey Zigachev 			*voltage |= (dep_table->entries[i].vddc *
382*b843c749SSergey Zigachev 					VOLTAGE_SCALE) << VDDC_SHIFT;
383*b843c749SSergey Zigachev 			if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
384*b843c749SSergey Zigachev 				*voltage |= (data->vbios_boot_state.vddci_bootup_value *
385*b843c749SSergey Zigachev 						VOLTAGE_SCALE) << VDDCI_SHIFT;
386*b843c749SSergey Zigachev 			else if (dep_table->entries[i].vddci)
387*b843c749SSergey Zigachev 				*voltage |= (dep_table->entries[i].vddci *
388*b843c749SSergey Zigachev 						VOLTAGE_SCALE) << VDDCI_SHIFT;
389*b843c749SSergey Zigachev 			else {
390*b843c749SSergey Zigachev 				vddci = phm_find_closest_vddci(&(data->vddci_voltage_table),
391*b843c749SSergey Zigachev 						(dep_table->entries[i].vddc -
392*b843c749SSergey Zigachev 								VDDC_VDDCI_DELTA));
393*b843c749SSergey Zigachev 				*voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
394*b843c749SSergey Zigachev 			}
395*b843c749SSergey Zigachev 
396*b843c749SSergey Zigachev 			if (SMU7_VOLTAGE_CONTROL_NONE == data->mvdd_control)
397*b843c749SSergey Zigachev 				*mvdd = data->vbios_boot_state.mvdd_bootup_value *
398*b843c749SSergey Zigachev 					VOLTAGE_SCALE;
399*b843c749SSergey Zigachev 			else if (dep_table->entries[i].mvdd)
400*b843c749SSergey Zigachev 				*mvdd = (uint32_t) dep_table->entries[i].mvdd *
401*b843c749SSergey Zigachev 					VOLTAGE_SCALE;
402*b843c749SSergey Zigachev 
403*b843c749SSergey Zigachev 			*voltage |= 1 << PHASES_SHIFT;
404*b843c749SSergey Zigachev 			return 0;
405*b843c749SSergey Zigachev 		}
406*b843c749SSergey Zigachev 	}
407*b843c749SSergey Zigachev 
408*b843c749SSergey Zigachev 	/* sclk is bigger than max sclk in the dependence table */
409*b843c749SSergey Zigachev 	*voltage |= (dep_table->entries[i - 1].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
410*b843c749SSergey Zigachev 
411*b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
412*b843c749SSergey Zigachev 		*voltage |= (data->vbios_boot_state.vddci_bootup_value *
413*b843c749SSergey Zigachev 				VOLTAGE_SCALE) << VDDCI_SHIFT;
414*b843c749SSergey Zigachev 	else if (dep_table->entries[i-1].vddci) {
415*b843c749SSergey Zigachev 		vddci = phm_find_closest_vddci(&(data->vddci_voltage_table),
416*b843c749SSergey Zigachev 				(dep_table->entries[i].vddc -
417*b843c749SSergey Zigachev 						VDDC_VDDCI_DELTA));
418*b843c749SSergey Zigachev 		*voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
419*b843c749SSergey Zigachev 	}
420*b843c749SSergey Zigachev 
421*b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE == data->mvdd_control)
422*b843c749SSergey Zigachev 		*mvdd = data->vbios_boot_state.mvdd_bootup_value * VOLTAGE_SCALE;
423*b843c749SSergey Zigachev 	else if (dep_table->entries[i].mvdd)
424*b843c749SSergey Zigachev 		*mvdd = (uint32_t) dep_table->entries[i - 1].mvdd * VOLTAGE_SCALE;
425*b843c749SSergey Zigachev 
426*b843c749SSergey Zigachev 	return 0;
427*b843c749SSergey Zigachev }
428*b843c749SSergey Zigachev 
429*b843c749SSergey Zigachev 
scale_fan_gain_settings(uint16_t raw_setting)430*b843c749SSergey Zigachev static uint16_t scale_fan_gain_settings(uint16_t raw_setting)
431*b843c749SSergey Zigachev {
432*b843c749SSergey Zigachev 	uint32_t tmp;
433*b843c749SSergey Zigachev 	tmp = raw_setting * 4096 / 100;
434*b843c749SSergey Zigachev 	return (uint16_t)tmp;
435*b843c749SSergey Zigachev }
436*b843c749SSergey Zigachev 
get_scl_sda_value(uint8_t line,uint8_t * scl,uint8_t * sda)437*b843c749SSergey Zigachev static void get_scl_sda_value(uint8_t line, uint8_t *scl, uint8_t *sda)
438*b843c749SSergey Zigachev {
439*b843c749SSergey Zigachev 	switch (line) {
440*b843c749SSergey Zigachev 	case SMU7_I2CLineID_DDC1:
441*b843c749SSergey Zigachev 		*scl = SMU7_I2C_DDC1CLK;
442*b843c749SSergey Zigachev 		*sda = SMU7_I2C_DDC1DATA;
443*b843c749SSergey Zigachev 		break;
444*b843c749SSergey Zigachev 	case SMU7_I2CLineID_DDC2:
445*b843c749SSergey Zigachev 		*scl = SMU7_I2C_DDC2CLK;
446*b843c749SSergey Zigachev 		*sda = SMU7_I2C_DDC2DATA;
447*b843c749SSergey Zigachev 		break;
448*b843c749SSergey Zigachev 	case SMU7_I2CLineID_DDC3:
449*b843c749SSergey Zigachev 		*scl = SMU7_I2C_DDC3CLK;
450*b843c749SSergey Zigachev 		*sda = SMU7_I2C_DDC3DATA;
451*b843c749SSergey Zigachev 		break;
452*b843c749SSergey Zigachev 	case SMU7_I2CLineID_DDC4:
453*b843c749SSergey Zigachev 		*scl = SMU7_I2C_DDC4CLK;
454*b843c749SSergey Zigachev 		*sda = SMU7_I2C_DDC4DATA;
455*b843c749SSergey Zigachev 		break;
456*b843c749SSergey Zigachev 	case SMU7_I2CLineID_DDC5:
457*b843c749SSergey Zigachev 		*scl = SMU7_I2C_DDC5CLK;
458*b843c749SSergey Zigachev 		*sda = SMU7_I2C_DDC5DATA;
459*b843c749SSergey Zigachev 		break;
460*b843c749SSergey Zigachev 	case SMU7_I2CLineID_DDC6:
461*b843c749SSergey Zigachev 		*scl = SMU7_I2C_DDC6CLK;
462*b843c749SSergey Zigachev 		*sda = SMU7_I2C_DDC6DATA;
463*b843c749SSergey Zigachev 		break;
464*b843c749SSergey Zigachev 	case SMU7_I2CLineID_SCLSDA:
465*b843c749SSergey Zigachev 		*scl = SMU7_I2C_SCL;
466*b843c749SSergey Zigachev 		*sda = SMU7_I2C_SDA;
467*b843c749SSergey Zigachev 		break;
468*b843c749SSergey Zigachev 	case SMU7_I2CLineID_DDCVGA:
469*b843c749SSergey Zigachev 		*scl = SMU7_I2C_DDCVGACLK;
470*b843c749SSergey Zigachev 		*sda = SMU7_I2C_DDCVGADATA;
471*b843c749SSergey Zigachev 		break;
472*b843c749SSergey Zigachev 	default:
473*b843c749SSergey Zigachev 		*scl = 0;
474*b843c749SSergey Zigachev 		*sda = 0;
475*b843c749SSergey Zigachev 		break;
476*b843c749SSergey Zigachev 	}
477*b843c749SSergey Zigachev }
478*b843c749SSergey Zigachev 
fiji_initialize_power_tune_defaults(struct pp_hwmgr * hwmgr)479*b843c749SSergey Zigachev static void fiji_initialize_power_tune_defaults(struct pp_hwmgr *hwmgr)
480*b843c749SSergey Zigachev {
481*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
482*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
483*b843c749SSergey Zigachev 			(struct  phm_ppt_v1_information *)(hwmgr->pptable);
484*b843c749SSergey Zigachev 
485*b843c749SSergey Zigachev 	if (table_info &&
486*b843c749SSergey Zigachev 			table_info->cac_dtp_table->usPowerTuneDataSetID <= POWERTUNE_DEFAULT_SET_MAX &&
487*b843c749SSergey Zigachev 			table_info->cac_dtp_table->usPowerTuneDataSetID)
488*b843c749SSergey Zigachev 		smu_data->power_tune_defaults =
489*b843c749SSergey Zigachev 				&fiji_power_tune_data_set_array
490*b843c749SSergey Zigachev 				[table_info->cac_dtp_table->usPowerTuneDataSetID - 1];
491*b843c749SSergey Zigachev 	else
492*b843c749SSergey Zigachev 		smu_data->power_tune_defaults = &fiji_power_tune_data_set_array[0];
493*b843c749SSergey Zigachev 
494*b843c749SSergey Zigachev }
495*b843c749SSergey Zigachev 
fiji_populate_bapm_parameters_in_dpm_table(struct pp_hwmgr * hwmgr)496*b843c749SSergey Zigachev static int fiji_populate_bapm_parameters_in_dpm_table(struct pp_hwmgr *hwmgr)
497*b843c749SSergey Zigachev {
498*b843c749SSergey Zigachev 
499*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
500*b843c749SSergey Zigachev 	const struct fiji_pt_defaults *defaults = smu_data->power_tune_defaults;
501*b843c749SSergey Zigachev 
502*b843c749SSergey Zigachev 	SMU73_Discrete_DpmTable  *dpm_table = &(smu_data->smc_state_table);
503*b843c749SSergey Zigachev 
504*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
505*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
506*b843c749SSergey Zigachev 	struct phm_cac_tdp_table *cac_dtp_table = table_info->cac_dtp_table;
507*b843c749SSergey Zigachev 	struct pp_advance_fan_control_parameters *fan_table =
508*b843c749SSergey Zigachev 			&hwmgr->thermal_controller.advanceFanControlParameters;
509*b843c749SSergey Zigachev 	uint8_t uc_scl, uc_sda;
510*b843c749SSergey Zigachev 
511*b843c749SSergey Zigachev 	/* TDP number of fraction bits are changed from 8 to 7 for Fiji
512*b843c749SSergey Zigachev 	 * as requested by SMC team
513*b843c749SSergey Zigachev 	 */
514*b843c749SSergey Zigachev 	dpm_table->DefaultTdp = PP_HOST_TO_SMC_US(
515*b843c749SSergey Zigachev 			(uint16_t)(cac_dtp_table->usTDP * 128));
516*b843c749SSergey Zigachev 	dpm_table->TargetTdp = PP_HOST_TO_SMC_US(
517*b843c749SSergey Zigachev 			(uint16_t)(cac_dtp_table->usTDP * 128));
518*b843c749SSergey Zigachev 
519*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(cac_dtp_table->usTargetOperatingTemp <= 255,
520*b843c749SSergey Zigachev 			"Target Operating Temp is out of Range!",
521*b843c749SSergey Zigachev 			);
522*b843c749SSergey Zigachev 
523*b843c749SSergey Zigachev 	dpm_table->GpuTjMax = (uint8_t)(cac_dtp_table->usTargetOperatingTemp);
524*b843c749SSergey Zigachev 	dpm_table->GpuTjHyst = 8;
525*b843c749SSergey Zigachev 
526*b843c749SSergey Zigachev 	dpm_table->DTEAmbientTempBase = defaults->DTEAmbientTempBase;
527*b843c749SSergey Zigachev 
528*b843c749SSergey Zigachev 	/* The following are for new Fiji Multi-input fan/thermal control */
529*b843c749SSergey Zigachev 	dpm_table->TemperatureLimitEdge = PP_HOST_TO_SMC_US(
530*b843c749SSergey Zigachev 			cac_dtp_table->usTargetOperatingTemp * 256);
531*b843c749SSergey Zigachev 	dpm_table->TemperatureLimitHotspot = PP_HOST_TO_SMC_US(
532*b843c749SSergey Zigachev 			cac_dtp_table->usTemperatureLimitHotspot * 256);
533*b843c749SSergey Zigachev 	dpm_table->TemperatureLimitLiquid1 = PP_HOST_TO_SMC_US(
534*b843c749SSergey Zigachev 			cac_dtp_table->usTemperatureLimitLiquid1 * 256);
535*b843c749SSergey Zigachev 	dpm_table->TemperatureLimitLiquid2 = PP_HOST_TO_SMC_US(
536*b843c749SSergey Zigachev 			cac_dtp_table->usTemperatureLimitLiquid2 * 256);
537*b843c749SSergey Zigachev 	dpm_table->TemperatureLimitVrVddc = PP_HOST_TO_SMC_US(
538*b843c749SSergey Zigachev 			cac_dtp_table->usTemperatureLimitVrVddc * 256);
539*b843c749SSergey Zigachev 	dpm_table->TemperatureLimitVrMvdd = PP_HOST_TO_SMC_US(
540*b843c749SSergey Zigachev 			cac_dtp_table->usTemperatureLimitVrMvdd * 256);
541*b843c749SSergey Zigachev 	dpm_table->TemperatureLimitPlx = PP_HOST_TO_SMC_US(
542*b843c749SSergey Zigachev 			cac_dtp_table->usTemperatureLimitPlx * 256);
543*b843c749SSergey Zigachev 
544*b843c749SSergey Zigachev 	dpm_table->FanGainEdge = PP_HOST_TO_SMC_US(
545*b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainEdge));
546*b843c749SSergey Zigachev 	dpm_table->FanGainHotspot = PP_HOST_TO_SMC_US(
547*b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainHotspot));
548*b843c749SSergey Zigachev 	dpm_table->FanGainLiquid = PP_HOST_TO_SMC_US(
549*b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainLiquid));
550*b843c749SSergey Zigachev 	dpm_table->FanGainVrVddc = PP_HOST_TO_SMC_US(
551*b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainVrVddc));
552*b843c749SSergey Zigachev 	dpm_table->FanGainVrMvdd = PP_HOST_TO_SMC_US(
553*b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainVrMvdd));
554*b843c749SSergey Zigachev 	dpm_table->FanGainPlx = PP_HOST_TO_SMC_US(
555*b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainPlx));
556*b843c749SSergey Zigachev 	dpm_table->FanGainHbm = PP_HOST_TO_SMC_US(
557*b843c749SSergey Zigachev 			scale_fan_gain_settings(fan_table->usFanGainHbm));
558*b843c749SSergey Zigachev 
559*b843c749SSergey Zigachev 	dpm_table->Liquid1_I2C_address = cac_dtp_table->ucLiquid1_I2C_address;
560*b843c749SSergey Zigachev 	dpm_table->Liquid2_I2C_address = cac_dtp_table->ucLiquid2_I2C_address;
561*b843c749SSergey Zigachev 	dpm_table->Vr_I2C_address = cac_dtp_table->ucVr_I2C_address;
562*b843c749SSergey Zigachev 	dpm_table->Plx_I2C_address = cac_dtp_table->ucPlx_I2C_address;
563*b843c749SSergey Zigachev 
564*b843c749SSergey Zigachev 	get_scl_sda_value(cac_dtp_table->ucLiquid_I2C_Line, &uc_scl, &uc_sda);
565*b843c749SSergey Zigachev 	dpm_table->Liquid_I2C_LineSCL = uc_scl;
566*b843c749SSergey Zigachev 	dpm_table->Liquid_I2C_LineSDA = uc_sda;
567*b843c749SSergey Zigachev 
568*b843c749SSergey Zigachev 	get_scl_sda_value(cac_dtp_table->ucVr_I2C_Line, &uc_scl, &uc_sda);
569*b843c749SSergey Zigachev 	dpm_table->Vr_I2C_LineSCL = uc_scl;
570*b843c749SSergey Zigachev 	dpm_table->Vr_I2C_LineSDA = uc_sda;
571*b843c749SSergey Zigachev 
572*b843c749SSergey Zigachev 	get_scl_sda_value(cac_dtp_table->ucPlx_I2C_Line, &uc_scl, &uc_sda);
573*b843c749SSergey Zigachev 	dpm_table->Plx_I2C_LineSCL = uc_scl;
574*b843c749SSergey Zigachev 	dpm_table->Plx_I2C_LineSDA = uc_sda;
575*b843c749SSergey Zigachev 
576*b843c749SSergey Zigachev 	return 0;
577*b843c749SSergey Zigachev }
578*b843c749SSergey Zigachev 
579*b843c749SSergey Zigachev 
fiji_populate_svi_load_line(struct pp_hwmgr * hwmgr)580*b843c749SSergey Zigachev static int fiji_populate_svi_load_line(struct pp_hwmgr *hwmgr)
581*b843c749SSergey Zigachev {
582*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
583*b843c749SSergey Zigachev 	const struct fiji_pt_defaults *defaults = smu_data->power_tune_defaults;
584*b843c749SSergey Zigachev 
585*b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineEn = defaults->SviLoadLineEn;
586*b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineVddC = defaults->SviLoadLineVddC;
587*b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineTrimVddC = 3;
588*b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineOffsetVddC = 0;
589*b843c749SSergey Zigachev 
590*b843c749SSergey Zigachev 	return 0;
591*b843c749SSergey Zigachev }
592*b843c749SSergey Zigachev 
593*b843c749SSergey Zigachev 
fiji_populate_tdc_limit(struct pp_hwmgr * hwmgr)594*b843c749SSergey Zigachev static int fiji_populate_tdc_limit(struct pp_hwmgr *hwmgr)
595*b843c749SSergey Zigachev {
596*b843c749SSergey Zigachev 	uint16_t tdc_limit;
597*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
598*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
599*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
600*b843c749SSergey Zigachev 	const struct fiji_pt_defaults *defaults = smu_data->power_tune_defaults;
601*b843c749SSergey Zigachev 
602*b843c749SSergey Zigachev 	/* TDC number of fraction bits are changed from 8 to 7
603*b843c749SSergey Zigachev 	 * for Fiji as requested by SMC team
604*b843c749SSergey Zigachev 	 */
605*b843c749SSergey Zigachev 	tdc_limit = (uint16_t)(table_info->cac_dtp_table->usTDC * 128);
606*b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_VDDC_PkgLimit =
607*b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(tdc_limit);
608*b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
609*b843c749SSergey Zigachev 			defaults->TDC_VDDC_ThrottleReleaseLimitPerc;
610*b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_MAWt = defaults->TDC_MAWt;
611*b843c749SSergey Zigachev 
612*b843c749SSergey Zigachev 	return 0;
613*b843c749SSergey Zigachev }
614*b843c749SSergey Zigachev 
fiji_populate_dw8(struct pp_hwmgr * hwmgr,uint32_t fuse_table_offset)615*b843c749SSergey Zigachev static int fiji_populate_dw8(struct pp_hwmgr *hwmgr, uint32_t fuse_table_offset)
616*b843c749SSergey Zigachev {
617*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
618*b843c749SSergey Zigachev 	const struct fiji_pt_defaults *defaults = smu_data->power_tune_defaults;
619*b843c749SSergey Zigachev 	uint32_t temp;
620*b843c749SSergey Zigachev 
621*b843c749SSergey Zigachev 	if (smu7_read_smc_sram_dword(hwmgr,
622*b843c749SSergey Zigachev 			fuse_table_offset +
623*b843c749SSergey Zigachev 			offsetof(SMU73_Discrete_PmFuses, TdcWaterfallCtl),
624*b843c749SSergey Zigachev 			(uint32_t *)&temp, SMC_RAM_END))
625*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
626*b843c749SSergey Zigachev 				"Attempt to read PmFuses.DW6 (SviLoadLineEn) from SMC Failed!",
627*b843c749SSergey Zigachev 				return -EINVAL);
628*b843c749SSergey Zigachev 	else {
629*b843c749SSergey Zigachev 		smu_data->power_tune_table.TdcWaterfallCtl = defaults->TdcWaterfallCtl;
630*b843c749SSergey Zigachev 		smu_data->power_tune_table.LPMLTemperatureMin =
631*b843c749SSergey Zigachev 				(uint8_t)((temp >> 16) & 0xff);
632*b843c749SSergey Zigachev 		smu_data->power_tune_table.LPMLTemperatureMax =
633*b843c749SSergey Zigachev 				(uint8_t)((temp >> 8) & 0xff);
634*b843c749SSergey Zigachev 		smu_data->power_tune_table.Reserved = (uint8_t)(temp & 0xff);
635*b843c749SSergey Zigachev 	}
636*b843c749SSergey Zigachev 	return 0;
637*b843c749SSergey Zigachev }
638*b843c749SSergey Zigachev 
fiji_populate_temperature_scaler(struct pp_hwmgr * hwmgr)639*b843c749SSergey Zigachev static int fiji_populate_temperature_scaler(struct pp_hwmgr *hwmgr)
640*b843c749SSergey Zigachev {
641*b843c749SSergey Zigachev 	int i;
642*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
643*b843c749SSergey Zigachev 
644*b843c749SSergey Zigachev 	/* Currently not used. Set all to zero. */
645*b843c749SSergey Zigachev 	for (i = 0; i < 16; i++)
646*b843c749SSergey Zigachev 		smu_data->power_tune_table.LPMLTemperatureScaler[i] = 0;
647*b843c749SSergey Zigachev 
648*b843c749SSergey Zigachev 	return 0;
649*b843c749SSergey Zigachev }
650*b843c749SSergey Zigachev 
fiji_populate_fuzzy_fan(struct pp_hwmgr * hwmgr)651*b843c749SSergey Zigachev static int fiji_populate_fuzzy_fan(struct pp_hwmgr *hwmgr)
652*b843c749SSergey Zigachev {
653*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
654*b843c749SSergey Zigachev 
655*b843c749SSergey Zigachev 	if ((hwmgr->thermal_controller.advanceFanControlParameters.
656*b843c749SSergey Zigachev 			usFanOutputSensitivity & (1 << 15)) ||
657*b843c749SSergey Zigachev 			0 == hwmgr->thermal_controller.advanceFanControlParameters.
658*b843c749SSergey Zigachev 			usFanOutputSensitivity)
659*b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.
660*b843c749SSergey Zigachev 		usFanOutputSensitivity = hwmgr->thermal_controller.
661*b843c749SSergey Zigachev 			advanceFanControlParameters.usDefaultFanOutputSensitivity;
662*b843c749SSergey Zigachev 
663*b843c749SSergey Zigachev 	smu_data->power_tune_table.FuzzyFan_PwmSetDelta =
664*b843c749SSergey Zigachev 			PP_HOST_TO_SMC_US(hwmgr->thermal_controller.
665*b843c749SSergey Zigachev 					advanceFanControlParameters.usFanOutputSensitivity);
666*b843c749SSergey Zigachev 	return 0;
667*b843c749SSergey Zigachev }
668*b843c749SSergey Zigachev 
fiji_populate_gnb_lpml(struct pp_hwmgr * hwmgr)669*b843c749SSergey Zigachev static int fiji_populate_gnb_lpml(struct pp_hwmgr *hwmgr)
670*b843c749SSergey Zigachev {
671*b843c749SSergey Zigachev 	int i;
672*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
673*b843c749SSergey Zigachev 
674*b843c749SSergey Zigachev 	/* Currently not used. Set all to zero. */
675*b843c749SSergey Zigachev 	for (i = 0; i < 16; i++)
676*b843c749SSergey Zigachev 		smu_data->power_tune_table.GnbLPML[i] = 0;
677*b843c749SSergey Zigachev 
678*b843c749SSergey Zigachev 	return 0;
679*b843c749SSergey Zigachev }
680*b843c749SSergey Zigachev 
fiji_populate_bapm_vddc_base_leakage_sidd(struct pp_hwmgr * hwmgr)681*b843c749SSergey Zigachev static int fiji_populate_bapm_vddc_base_leakage_sidd(struct pp_hwmgr *hwmgr)
682*b843c749SSergey Zigachev {
683*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
684*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
685*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
686*b843c749SSergey Zigachev 	uint16_t HiSidd = smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd;
687*b843c749SSergey Zigachev 	uint16_t LoSidd = smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd;
688*b843c749SSergey Zigachev 	struct phm_cac_tdp_table *cac_table = table_info->cac_dtp_table;
689*b843c749SSergey Zigachev 
690*b843c749SSergey Zigachev 	HiSidd = (uint16_t)(cac_table->usHighCACLeakage / 100 * 256);
691*b843c749SSergey Zigachev 	LoSidd = (uint16_t)(cac_table->usLowCACLeakage / 100 * 256);
692*b843c749SSergey Zigachev 
693*b843c749SSergey Zigachev 	smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd =
694*b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(HiSidd);
695*b843c749SSergey Zigachev 	smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd =
696*b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(LoSidd);
697*b843c749SSergey Zigachev 
698*b843c749SSergey Zigachev 	return 0;
699*b843c749SSergey Zigachev }
700*b843c749SSergey Zigachev 
fiji_populate_pm_fuses(struct pp_hwmgr * hwmgr)701*b843c749SSergey Zigachev static int fiji_populate_pm_fuses(struct pp_hwmgr *hwmgr)
702*b843c749SSergey Zigachev {
703*b843c749SSergey Zigachev 	uint32_t pm_fuse_table_offset;
704*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
705*b843c749SSergey Zigachev 
706*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
707*b843c749SSergey Zigachev 			PHM_PlatformCaps_PowerContainment)) {
708*b843c749SSergey Zigachev 		if (smu7_read_smc_sram_dword(hwmgr,
709*b843c749SSergey Zigachev 				SMU7_FIRMWARE_HEADER_LOCATION +
710*b843c749SSergey Zigachev 				offsetof(SMU73_Firmware_Header, PmFuseTable),
711*b843c749SSergey Zigachev 				&pm_fuse_table_offset, SMC_RAM_END))
712*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
713*b843c749SSergey Zigachev 					"Attempt to get pm_fuse_table_offset Failed!",
714*b843c749SSergey Zigachev 					return -EINVAL);
715*b843c749SSergey Zigachev 
716*b843c749SSergey Zigachev 		/* DW6 */
717*b843c749SSergey Zigachev 		if (fiji_populate_svi_load_line(hwmgr))
718*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
719*b843c749SSergey Zigachev 					"Attempt to populate SviLoadLine Failed!",
720*b843c749SSergey Zigachev 					return -EINVAL);
721*b843c749SSergey Zigachev 		/* DW7 */
722*b843c749SSergey Zigachev 		if (fiji_populate_tdc_limit(hwmgr))
723*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
724*b843c749SSergey Zigachev 					"Attempt to populate TDCLimit Failed!", return -EINVAL);
725*b843c749SSergey Zigachev 		/* DW8 */
726*b843c749SSergey Zigachev 		if (fiji_populate_dw8(hwmgr, pm_fuse_table_offset))
727*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
728*b843c749SSergey Zigachev 					"Attempt to populate TdcWaterfallCtl, "
729*b843c749SSergey Zigachev 					"LPMLTemperature Min and Max Failed!",
730*b843c749SSergey Zigachev 					return -EINVAL);
731*b843c749SSergey Zigachev 
732*b843c749SSergey Zigachev 		/* DW9-DW12 */
733*b843c749SSergey Zigachev 		if (0 != fiji_populate_temperature_scaler(hwmgr))
734*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
735*b843c749SSergey Zigachev 					"Attempt to populate LPMLTemperatureScaler Failed!",
736*b843c749SSergey Zigachev 					return -EINVAL);
737*b843c749SSergey Zigachev 
738*b843c749SSergey Zigachev 		/* DW13-DW14 */
739*b843c749SSergey Zigachev 		if (fiji_populate_fuzzy_fan(hwmgr))
740*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
741*b843c749SSergey Zigachev 					"Attempt to populate Fuzzy Fan Control parameters Failed!",
742*b843c749SSergey Zigachev 					return -EINVAL);
743*b843c749SSergey Zigachev 
744*b843c749SSergey Zigachev 		/* DW15-DW18 */
745*b843c749SSergey Zigachev 		if (fiji_populate_gnb_lpml(hwmgr))
746*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
747*b843c749SSergey Zigachev 					"Attempt to populate GnbLPML Failed!",
748*b843c749SSergey Zigachev 					return -EINVAL);
749*b843c749SSergey Zigachev 
750*b843c749SSergey Zigachev 		/* DW20 */
751*b843c749SSergey Zigachev 		if (fiji_populate_bapm_vddc_base_leakage_sidd(hwmgr))
752*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
753*b843c749SSergey Zigachev 					"Attempt to populate BapmVddCBaseLeakage Hi and Lo "
754*b843c749SSergey Zigachev 					"Sidd Failed!", return -EINVAL);
755*b843c749SSergey Zigachev 
756*b843c749SSergey Zigachev 		if (smu7_copy_bytes_to_smc(hwmgr, pm_fuse_table_offset,
757*b843c749SSergey Zigachev 				(uint8_t *)&smu_data->power_tune_table,
758*b843c749SSergey Zigachev 				sizeof(struct SMU73_Discrete_PmFuses), SMC_RAM_END))
759*b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
760*b843c749SSergey Zigachev 					"Attempt to download PmFuseTable Failed!",
761*b843c749SSergey Zigachev 					return -EINVAL);
762*b843c749SSergey Zigachev 	}
763*b843c749SSergey Zigachev 	return 0;
764*b843c749SSergey Zigachev }
765*b843c749SSergey Zigachev 
fiji_populate_cac_table(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_DpmTable * table)766*b843c749SSergey Zigachev static int fiji_populate_cac_table(struct pp_hwmgr *hwmgr,
767*b843c749SSergey Zigachev 		struct SMU73_Discrete_DpmTable *table)
768*b843c749SSergey Zigachev {
769*b843c749SSergey Zigachev 	uint32_t count;
770*b843c749SSergey Zigachev 	uint8_t index;
771*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
772*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
773*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
774*b843c749SSergey Zigachev 	struct phm_ppt_v1_voltage_lookup_table *lookup_table =
775*b843c749SSergey Zigachev 			table_info->vddc_lookup_table;
776*b843c749SSergey Zigachev 	/* tables is already swapped, so in order to use the value from it,
777*b843c749SSergey Zigachev 	 * we need to swap it back.
778*b843c749SSergey Zigachev 	 * We are populating vddc CAC data to BapmVddc table
779*b843c749SSergey Zigachev 	 * in split and merged mode
780*b843c749SSergey Zigachev 	 */
781*b843c749SSergey Zigachev 
782*b843c749SSergey Zigachev 	for (count = 0; count < lookup_table->count; count++) {
783*b843c749SSergey Zigachev 		index = phm_get_voltage_index(lookup_table,
784*b843c749SSergey Zigachev 				data->vddc_voltage_table.entries[count].value);
785*b843c749SSergey Zigachev 		table->BapmVddcVidLoSidd[count] =
786*b843c749SSergey Zigachev 			convert_to_vid(lookup_table->entries[index].us_cac_low);
787*b843c749SSergey Zigachev 		table->BapmVddcVidHiSidd[count] =
788*b843c749SSergey Zigachev 			convert_to_vid(lookup_table->entries[index].us_cac_high);
789*b843c749SSergey Zigachev 	}
790*b843c749SSergey Zigachev 
791*b843c749SSergey Zigachev 	return 0;
792*b843c749SSergey Zigachev }
793*b843c749SSergey Zigachev 
fiji_populate_smc_voltage_tables(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_DpmTable * table)794*b843c749SSergey Zigachev static int fiji_populate_smc_voltage_tables(struct pp_hwmgr *hwmgr,
795*b843c749SSergey Zigachev 		struct SMU73_Discrete_DpmTable *table)
796*b843c749SSergey Zigachev {
797*b843c749SSergey Zigachev 	int result;
798*b843c749SSergey Zigachev 
799*b843c749SSergey Zigachev 	result = fiji_populate_cac_table(hwmgr, table);
800*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
801*b843c749SSergey Zigachev 			"can not populate CAC voltage tables to SMC",
802*b843c749SSergey Zigachev 			return -EINVAL);
803*b843c749SSergey Zigachev 
804*b843c749SSergey Zigachev 	return 0;
805*b843c749SSergey Zigachev }
806*b843c749SSergey Zigachev 
fiji_populate_ulv_level(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_Ulv * state)807*b843c749SSergey Zigachev static int fiji_populate_ulv_level(struct pp_hwmgr *hwmgr,
808*b843c749SSergey Zigachev 		struct SMU73_Discrete_Ulv *state)
809*b843c749SSergey Zigachev {
810*b843c749SSergey Zigachev 	int result = 0;
811*b843c749SSergey Zigachev 
812*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
813*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
814*b843c749SSergey Zigachev 
815*b843c749SSergey Zigachev 	state->CcPwrDynRm = 0;
816*b843c749SSergey Zigachev 	state->CcPwrDynRm1 = 0;
817*b843c749SSergey Zigachev 
818*b843c749SSergey Zigachev 	state->VddcOffset = (uint16_t) table_info->us_ulv_voltage_offset;
819*b843c749SSergey Zigachev 	state->VddcOffsetVid = (uint8_t)(table_info->us_ulv_voltage_offset *
820*b843c749SSergey Zigachev 			VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1);
821*b843c749SSergey Zigachev 
822*b843c749SSergey Zigachev 	state->VddcPhase = 1;
823*b843c749SSergey Zigachev 
824*b843c749SSergey Zigachev 	if (!result) {
825*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm);
826*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm1);
827*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(state->VddcOffset);
828*b843c749SSergey Zigachev 	}
829*b843c749SSergey Zigachev 	return result;
830*b843c749SSergey Zigachev }
831*b843c749SSergey Zigachev 
fiji_populate_ulv_state(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_DpmTable * table)832*b843c749SSergey Zigachev static int fiji_populate_ulv_state(struct pp_hwmgr *hwmgr,
833*b843c749SSergey Zigachev 		struct SMU73_Discrete_DpmTable *table)
834*b843c749SSergey Zigachev {
835*b843c749SSergey Zigachev 	return fiji_populate_ulv_level(hwmgr, &table->Ulv);
836*b843c749SSergey Zigachev }
837*b843c749SSergey Zigachev 
fiji_populate_smc_link_level(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_DpmTable * table)838*b843c749SSergey Zigachev static int fiji_populate_smc_link_level(struct pp_hwmgr *hwmgr,
839*b843c749SSergey Zigachev 		struct SMU73_Discrete_DpmTable *table)
840*b843c749SSergey Zigachev {
841*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
842*b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
843*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
844*b843c749SSergey Zigachev 	int i;
845*b843c749SSergey Zigachev 
846*b843c749SSergey Zigachev 	/* Index (dpm_table->pcie_speed_table.count)
847*b843c749SSergey Zigachev 	 * is reserved for PCIE boot level. */
848*b843c749SSergey Zigachev 	for (i = 0; i <= dpm_table->pcie_speed_table.count; i++) {
849*b843c749SSergey Zigachev 		table->LinkLevel[i].PcieGenSpeed  =
850*b843c749SSergey Zigachev 				(uint8_t)dpm_table->pcie_speed_table.dpm_levels[i].value;
851*b843c749SSergey Zigachev 		table->LinkLevel[i].PcieLaneCount = (uint8_t)encode_pcie_lane_width(
852*b843c749SSergey Zigachev 				dpm_table->pcie_speed_table.dpm_levels[i].param1);
853*b843c749SSergey Zigachev 		table->LinkLevel[i].EnabledForActivity = 1;
854*b843c749SSergey Zigachev 		table->LinkLevel[i].SPC = (uint8_t)(data->pcie_spc_cap & 0xff);
855*b843c749SSergey Zigachev 		table->LinkLevel[i].DownThreshold = PP_HOST_TO_SMC_UL(5);
856*b843c749SSergey Zigachev 		table->LinkLevel[i].UpThreshold = PP_HOST_TO_SMC_UL(30);
857*b843c749SSergey Zigachev 	}
858*b843c749SSergey Zigachev 
859*b843c749SSergey Zigachev 	smu_data->smc_state_table.LinkLevelCount =
860*b843c749SSergey Zigachev 			(uint8_t)dpm_table->pcie_speed_table.count;
861*b843c749SSergey Zigachev 	data->dpm_level_enable_mask.pcie_dpm_enable_mask =
862*b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
863*b843c749SSergey Zigachev 
864*b843c749SSergey Zigachev 	return 0;
865*b843c749SSergey Zigachev }
866*b843c749SSergey Zigachev 
fiji_calculate_sclk_params(struct pp_hwmgr * hwmgr,uint32_t clock,struct SMU73_Discrete_GraphicsLevel * sclk)867*b843c749SSergey Zigachev static int fiji_calculate_sclk_params(struct pp_hwmgr *hwmgr,
868*b843c749SSergey Zigachev 		uint32_t clock, struct SMU73_Discrete_GraphicsLevel *sclk)
869*b843c749SSergey Zigachev {
870*b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
871*b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
872*b843c749SSergey Zigachev 	uint32_t spll_func_cntl            = data->clock_registers.vCG_SPLL_FUNC_CNTL;
873*b843c749SSergey Zigachev 	uint32_t spll_func_cntl_3          = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
874*b843c749SSergey Zigachev 	uint32_t spll_func_cntl_4          = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
875*b843c749SSergey Zigachev 	uint32_t cg_spll_spread_spectrum   = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
876*b843c749SSergey Zigachev 	uint32_t cg_spll_spread_spectrum_2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
877*b843c749SSergey Zigachev 	uint32_t ref_clock;
878*b843c749SSergey Zigachev 	uint32_t ref_divider;
879*b843c749SSergey Zigachev 	uint32_t fbdiv;
880*b843c749SSergey Zigachev 	int result;
881*b843c749SSergey Zigachev 
882*b843c749SSergey Zigachev 	/* get the engine clock dividers for this clock value */
883*b843c749SSergey Zigachev 	result = atomctrl_get_engine_pll_dividers_vi(hwmgr, clock,  &dividers);
884*b843c749SSergey Zigachev 
885*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0,
886*b843c749SSergey Zigachev 			"Error retrieving Engine Clock dividers from VBIOS.",
887*b843c749SSergey Zigachev 			return result);
888*b843c749SSergey Zigachev 
889*b843c749SSergey Zigachev 	/* To get FBDIV we need to multiply this by 16384 and divide it by Fref. */
890*b843c749SSergey Zigachev 	ref_clock = atomctrl_get_reference_clock(hwmgr);
891*b843c749SSergey Zigachev 	ref_divider = 1 + dividers.uc_pll_ref_div;
892*b843c749SSergey Zigachev 
893*b843c749SSergey Zigachev 	/* low 14 bits is fraction and high 12 bits is divider */
894*b843c749SSergey Zigachev 	fbdiv = dividers.ul_fb_div.ul_fb_divider & 0x3FFFFFF;
895*b843c749SSergey Zigachev 
896*b843c749SSergey Zigachev 	/* SPLL_FUNC_CNTL setup */
897*b843c749SSergey Zigachev 	spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
898*b843c749SSergey Zigachev 			SPLL_REF_DIV, dividers.uc_pll_ref_div);
899*b843c749SSergey Zigachev 	spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
900*b843c749SSergey Zigachev 			SPLL_PDIV_A,  dividers.uc_pll_post_div);
901*b843c749SSergey Zigachev 
902*b843c749SSergey Zigachev 	/* SPLL_FUNC_CNTL_3 setup*/
903*b843c749SSergey Zigachev 	spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3, CG_SPLL_FUNC_CNTL_3,
904*b843c749SSergey Zigachev 			SPLL_FB_DIV, fbdiv);
905*b843c749SSergey Zigachev 
906*b843c749SSergey Zigachev 	/* set to use fractional accumulation*/
907*b843c749SSergey Zigachev 	spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3, CG_SPLL_FUNC_CNTL_3,
908*b843c749SSergey Zigachev 			SPLL_DITHEN, 1);
909*b843c749SSergey Zigachev 
910*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
911*b843c749SSergey Zigachev 				PHM_PlatformCaps_EngineSpreadSpectrumSupport)) {
912*b843c749SSergey Zigachev 		struct pp_atomctrl_internal_ss_info ssInfo;
913*b843c749SSergey Zigachev 
914*b843c749SSergey Zigachev 		uint32_t vco_freq = clock * dividers.uc_pll_post_div;
915*b843c749SSergey Zigachev 		if (!atomctrl_get_engine_clock_spread_spectrum(hwmgr,
916*b843c749SSergey Zigachev 				vco_freq, &ssInfo)) {
917*b843c749SSergey Zigachev 			/*
918*b843c749SSergey Zigachev 			 * ss_info.speed_spectrum_percentage -- in unit of 0.01%
919*b843c749SSergey Zigachev 			 * ss_info.speed_spectrum_rate -- in unit of khz
920*b843c749SSergey Zigachev 			 *
921*b843c749SSergey Zigachev 			 * clks = reference_clock * 10 / (REFDIV + 1) / speed_spectrum_rate / 2
922*b843c749SSergey Zigachev 			 */
923*b843c749SSergey Zigachev 			uint32_t clk_s = ref_clock * 5 /
924*b843c749SSergey Zigachev 					(ref_divider * ssInfo.speed_spectrum_rate);
925*b843c749SSergey Zigachev 			/* clkv = 2 * D * fbdiv / NS */
926*b843c749SSergey Zigachev 			uint32_t clk_v = 4 * ssInfo.speed_spectrum_percentage *
927*b843c749SSergey Zigachev 					fbdiv / (clk_s * 10000);
928*b843c749SSergey Zigachev 
929*b843c749SSergey Zigachev 			cg_spll_spread_spectrum = PHM_SET_FIELD(cg_spll_spread_spectrum,
930*b843c749SSergey Zigachev 					CG_SPLL_SPREAD_SPECTRUM, CLKS, clk_s);
931*b843c749SSergey Zigachev 			cg_spll_spread_spectrum = PHM_SET_FIELD(cg_spll_spread_spectrum,
932*b843c749SSergey Zigachev 					CG_SPLL_SPREAD_SPECTRUM, SSEN, 1);
933*b843c749SSergey Zigachev 			cg_spll_spread_spectrum_2 = PHM_SET_FIELD(cg_spll_spread_spectrum_2,
934*b843c749SSergey Zigachev 					CG_SPLL_SPREAD_SPECTRUM_2, CLKV, clk_v);
935*b843c749SSergey Zigachev 		}
936*b843c749SSergey Zigachev 	}
937*b843c749SSergey Zigachev 
938*b843c749SSergey Zigachev 	sclk->SclkFrequency        = clock;
939*b843c749SSergey Zigachev 	sclk->CgSpllFuncCntl3      = spll_func_cntl_3;
940*b843c749SSergey Zigachev 	sclk->CgSpllFuncCntl4      = spll_func_cntl_4;
941*b843c749SSergey Zigachev 	sclk->SpllSpreadSpectrum   = cg_spll_spread_spectrum;
942*b843c749SSergey Zigachev 	sclk->SpllSpreadSpectrum2  = cg_spll_spread_spectrum_2;
943*b843c749SSergey Zigachev 	sclk->SclkDid              = (uint8_t)dividers.pll_post_divider;
944*b843c749SSergey Zigachev 
945*b843c749SSergey Zigachev 	return 0;
946*b843c749SSergey Zigachev }
947*b843c749SSergey Zigachev 
fiji_populate_single_graphic_level(struct pp_hwmgr * hwmgr,uint32_t clock,struct SMU73_Discrete_GraphicsLevel * level)948*b843c749SSergey Zigachev static int fiji_populate_single_graphic_level(struct pp_hwmgr *hwmgr,
949*b843c749SSergey Zigachev 		uint32_t clock, struct SMU73_Discrete_GraphicsLevel *level)
950*b843c749SSergey Zigachev {
951*b843c749SSergey Zigachev 	int result;
952*b843c749SSergey Zigachev 	/* PP_Clocks minClocks; */
953*b843c749SSergey Zigachev 	uint32_t threshold, mvdd;
954*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
955*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
956*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
957*b843c749SSergey Zigachev 	phm_ppt_v1_clock_voltage_dependency_table *vdd_dep_table = NULL;
958*b843c749SSergey Zigachev 
959*b843c749SSergey Zigachev 	result = fiji_calculate_sclk_params(hwmgr, clock, level);
960*b843c749SSergey Zigachev 
961*b843c749SSergey Zigachev 	if (hwmgr->od_enabled)
962*b843c749SSergey Zigachev 		vdd_dep_table = (phm_ppt_v1_clock_voltage_dependency_table *)&data->odn_dpm_table.vdd_dependency_on_sclk;
963*b843c749SSergey Zigachev 	else
964*b843c749SSergey Zigachev 		vdd_dep_table = table_info->vdd_dep_on_sclk;
965*b843c749SSergey Zigachev 
966*b843c749SSergey Zigachev 	/* populate graphics levels */
967*b843c749SSergey Zigachev 	result = fiji_get_dependency_volt_by_clk(hwmgr,
968*b843c749SSergey Zigachev 			vdd_dep_table, clock,
969*b843c749SSergey Zigachev 			(uint32_t *)(&level->MinVoltage), &mvdd);
970*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result),
971*b843c749SSergey Zigachev 			"can not find VDDC voltage value for "
972*b843c749SSergey Zigachev 			"VDDC engine clock dependency table",
973*b843c749SSergey Zigachev 			return result);
974*b843c749SSergey Zigachev 
975*b843c749SSergey Zigachev 	level->SclkFrequency = clock;
976*b843c749SSergey Zigachev 	level->ActivityLevel = data->current_profile_setting.sclk_activity;
977*b843c749SSergey Zigachev 	level->CcPwrDynRm = 0;
978*b843c749SSergey Zigachev 	level->CcPwrDynRm1 = 0;
979*b843c749SSergey Zigachev 	level->EnabledForActivity = 0;
980*b843c749SSergey Zigachev 	level->EnabledForThrottle = 1;
981*b843c749SSergey Zigachev 	level->UpHyst = data->current_profile_setting.sclk_up_hyst;
982*b843c749SSergey Zigachev 	level->DownHyst = data->current_profile_setting.sclk_down_hyst;
983*b843c749SSergey Zigachev 	level->VoltageDownHyst = 0;
984*b843c749SSergey Zigachev 	level->PowerThrottle = 0;
985*b843c749SSergey Zigachev 
986*b843c749SSergey Zigachev 	threshold = clock * data->fast_watermark_threshold / 100;
987*b843c749SSergey Zigachev 
988*b843c749SSergey Zigachev 	data->display_timing.min_clock_in_sr = hwmgr->display_config->min_core_set_clock_in_sr;
989*b843c749SSergey Zigachev 
990*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep))
991*b843c749SSergey Zigachev 		level->DeepSleepDivId = smu7_get_sleep_divider_id_from_clock(clock,
992*b843c749SSergey Zigachev 								hwmgr->display_config->min_core_set_clock_in_sr);
993*b843c749SSergey Zigachev 
994*b843c749SSergey Zigachev 
995*b843c749SSergey Zigachev 	/* Default to slow, highest DPM level will be
996*b843c749SSergey Zigachev 	 * set to PPSMC_DISPLAY_WATERMARK_LOW later.
997*b843c749SSergey Zigachev 	 */
998*b843c749SSergey Zigachev 	level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
999*b843c749SSergey Zigachev 
1000*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->MinVoltage);
1001*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->SclkFrequency);
1002*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(level->ActivityLevel);
1003*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->CgSpllFuncCntl3);
1004*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->CgSpllFuncCntl4);
1005*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->SpllSpreadSpectrum);
1006*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->SpllSpreadSpectrum2);
1007*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm);
1008*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm1);
1009*b843c749SSergey Zigachev 
1010*b843c749SSergey Zigachev 	return 0;
1011*b843c749SSergey Zigachev }
1012*b843c749SSergey Zigachev 
fiji_populate_all_graphic_levels(struct pp_hwmgr * hwmgr)1013*b843c749SSergey Zigachev static int fiji_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
1014*b843c749SSergey Zigachev {
1015*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1016*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
1017*b843c749SSergey Zigachev 
1018*b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
1019*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1020*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1021*b843c749SSergey Zigachev 	struct phm_ppt_v1_pcie_table *pcie_table = table_info->pcie_table;
1022*b843c749SSergey Zigachev 	uint8_t pcie_entry_cnt = (uint8_t) data->dpm_table.pcie_speed_table.count;
1023*b843c749SSergey Zigachev 	int result = 0;
1024*b843c749SSergey Zigachev 	uint32_t array = smu_data->smu7_data.dpm_table_start +
1025*b843c749SSergey Zigachev 			offsetof(SMU73_Discrete_DpmTable, GraphicsLevel);
1026*b843c749SSergey Zigachev 	uint32_t array_size = sizeof(struct SMU73_Discrete_GraphicsLevel) *
1027*b843c749SSergey Zigachev 			SMU73_MAX_LEVELS_GRAPHICS;
1028*b843c749SSergey Zigachev 	struct SMU73_Discrete_GraphicsLevel *levels =
1029*b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsLevel;
1030*b843c749SSergey Zigachev 	uint32_t i, max_entry;
1031*b843c749SSergey Zigachev 	uint8_t hightest_pcie_level_enabled = 0,
1032*b843c749SSergey Zigachev 			lowest_pcie_level_enabled = 0,
1033*b843c749SSergey Zigachev 			mid_pcie_level_enabled = 0,
1034*b843c749SSergey Zigachev 			count = 0;
1035*b843c749SSergey Zigachev 
1036*b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->sclk_table.count; i++) {
1037*b843c749SSergey Zigachev 		result = fiji_populate_single_graphic_level(hwmgr,
1038*b843c749SSergey Zigachev 				dpm_table->sclk_table.dpm_levels[i].value,
1039*b843c749SSergey Zigachev 				&levels[i]);
1040*b843c749SSergey Zigachev 		if (result)
1041*b843c749SSergey Zigachev 			return result;
1042*b843c749SSergey Zigachev 
1043*b843c749SSergey Zigachev 		/* Making sure only DPM level 0-1 have Deep Sleep Div ID populated. */
1044*b843c749SSergey Zigachev 		if (i > 1)
1045*b843c749SSergey Zigachev 			levels[i].DeepSleepDivId = 0;
1046*b843c749SSergey Zigachev 	}
1047*b843c749SSergey Zigachev 
1048*b843c749SSergey Zigachev 	/* Only enable level 0 for now.*/
1049*b843c749SSergey Zigachev 	levels[0].EnabledForActivity = 1;
1050*b843c749SSergey Zigachev 
1051*b843c749SSergey Zigachev 	/* set highest level watermark to high */
1052*b843c749SSergey Zigachev 	levels[dpm_table->sclk_table.count - 1].DisplayWatermark =
1053*b843c749SSergey Zigachev 			PPSMC_DISPLAY_WATERMARK_HIGH;
1054*b843c749SSergey Zigachev 
1055*b843c749SSergey Zigachev 	smu_data->smc_state_table.GraphicsDpmLevelCount =
1056*b843c749SSergey Zigachev 			(uint8_t)dpm_table->sclk_table.count;
1057*b843c749SSergey Zigachev 	data->dpm_level_enable_mask.sclk_dpm_enable_mask =
1058*b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
1059*b843c749SSergey Zigachev 
1060*b843c749SSergey Zigachev 	if (pcie_table != NULL) {
1061*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((1 <= pcie_entry_cnt),
1062*b843c749SSergey Zigachev 				"There must be 1 or more PCIE levels defined in PPTable.",
1063*b843c749SSergey Zigachev 				return -EINVAL);
1064*b843c749SSergey Zigachev 		max_entry = pcie_entry_cnt - 1;
1065*b843c749SSergey Zigachev 		for (i = 0; i < dpm_table->sclk_table.count; i++)
1066*b843c749SSergey Zigachev 			levels[i].pcieDpmLevel =
1067*b843c749SSergey Zigachev 					(uint8_t) ((i < max_entry) ? i : max_entry);
1068*b843c749SSergey Zigachev 	} else {
1069*b843c749SSergey Zigachev 		while (data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
1070*b843c749SSergey Zigachev 				((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1071*b843c749SSergey Zigachev 						(1 << (hightest_pcie_level_enabled + 1))) != 0))
1072*b843c749SSergey Zigachev 			hightest_pcie_level_enabled++;
1073*b843c749SSergey Zigachev 
1074*b843c749SSergey Zigachev 		while (data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
1075*b843c749SSergey Zigachev 				((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1076*b843c749SSergey Zigachev 						(1 << lowest_pcie_level_enabled)) == 0))
1077*b843c749SSergey Zigachev 			lowest_pcie_level_enabled++;
1078*b843c749SSergey Zigachev 
1079*b843c749SSergey Zigachev 		while ((count < hightest_pcie_level_enabled) &&
1080*b843c749SSergey Zigachev 				((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1081*b843c749SSergey Zigachev 						(1 << (lowest_pcie_level_enabled + 1 + count))) == 0))
1082*b843c749SSergey Zigachev 			count++;
1083*b843c749SSergey Zigachev 
1084*b843c749SSergey Zigachev 		mid_pcie_level_enabled = (lowest_pcie_level_enabled + 1 + count) <
1085*b843c749SSergey Zigachev 				hightest_pcie_level_enabled ?
1086*b843c749SSergey Zigachev 						(lowest_pcie_level_enabled + 1 + count) :
1087*b843c749SSergey Zigachev 						hightest_pcie_level_enabled;
1088*b843c749SSergey Zigachev 
1089*b843c749SSergey Zigachev 		/* set pcieDpmLevel to hightest_pcie_level_enabled */
1090*b843c749SSergey Zigachev 		for (i = 2; i < dpm_table->sclk_table.count; i++)
1091*b843c749SSergey Zigachev 			levels[i].pcieDpmLevel = hightest_pcie_level_enabled;
1092*b843c749SSergey Zigachev 
1093*b843c749SSergey Zigachev 		/* set pcieDpmLevel to lowest_pcie_level_enabled */
1094*b843c749SSergey Zigachev 		levels[0].pcieDpmLevel = lowest_pcie_level_enabled;
1095*b843c749SSergey Zigachev 
1096*b843c749SSergey Zigachev 		/* set pcieDpmLevel to mid_pcie_level_enabled */
1097*b843c749SSergey Zigachev 		levels[1].pcieDpmLevel = mid_pcie_level_enabled;
1098*b843c749SSergey Zigachev 	}
1099*b843c749SSergey Zigachev 	/* level count will send to smc once at init smc table and never change */
1100*b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr, array, (uint8_t *)levels,
1101*b843c749SSergey Zigachev 			(uint32_t)array_size, SMC_RAM_END);
1102*b843c749SSergey Zigachev 
1103*b843c749SSergey Zigachev 	return result;
1104*b843c749SSergey Zigachev }
1105*b843c749SSergey Zigachev 
1106*b843c749SSergey Zigachev 
1107*b843c749SSergey Zigachev /**
1108*b843c749SSergey Zigachev  * MCLK Frequency Ratio
1109*b843c749SSergey Zigachev  * SEQ_CG_RESP  Bit[31:24] - 0x0
1110*b843c749SSergey Zigachev  * Bit[27:24] \96 DDR3 Frequency ratio
1111*b843c749SSergey Zigachev  * 0x0 <= 100MHz,       450 < 0x8 <= 500MHz
1112*b843c749SSergey Zigachev  * 100 < 0x1 <= 150MHz,       500 < 0x9 <= 550MHz
1113*b843c749SSergey Zigachev  * 150 < 0x2 <= 200MHz,       550 < 0xA <= 600MHz
1114*b843c749SSergey Zigachev  * 200 < 0x3 <= 250MHz,       600 < 0xB <= 650MHz
1115*b843c749SSergey Zigachev  * 250 < 0x4 <= 300MHz,       650 < 0xC <= 700MHz
1116*b843c749SSergey Zigachev  * 300 < 0x5 <= 350MHz,       700 < 0xD <= 750MHz
1117*b843c749SSergey Zigachev  * 350 < 0x6 <= 400MHz,       750 < 0xE <= 800MHz
1118*b843c749SSergey Zigachev  * 400 < 0x7 <= 450MHz,       800 < 0xF
1119*b843c749SSergey Zigachev  */
fiji_get_mclk_frequency_ratio(uint32_t mem_clock)1120*b843c749SSergey Zigachev static uint8_t fiji_get_mclk_frequency_ratio(uint32_t mem_clock)
1121*b843c749SSergey Zigachev {
1122*b843c749SSergey Zigachev 	if (mem_clock <= 10000)
1123*b843c749SSergey Zigachev 		return 0x0;
1124*b843c749SSergey Zigachev 	if (mem_clock <= 15000)
1125*b843c749SSergey Zigachev 		return 0x1;
1126*b843c749SSergey Zigachev 	if (mem_clock <= 20000)
1127*b843c749SSergey Zigachev 		return 0x2;
1128*b843c749SSergey Zigachev 	if (mem_clock <= 25000)
1129*b843c749SSergey Zigachev 		return 0x3;
1130*b843c749SSergey Zigachev 	if (mem_clock <= 30000)
1131*b843c749SSergey Zigachev 		return 0x4;
1132*b843c749SSergey Zigachev 	if (mem_clock <= 35000)
1133*b843c749SSergey Zigachev 		return 0x5;
1134*b843c749SSergey Zigachev 	if (mem_clock <= 40000)
1135*b843c749SSergey Zigachev 		return 0x6;
1136*b843c749SSergey Zigachev 	if (mem_clock <= 45000)
1137*b843c749SSergey Zigachev 		return 0x7;
1138*b843c749SSergey Zigachev 	if (mem_clock <= 50000)
1139*b843c749SSergey Zigachev 		return 0x8;
1140*b843c749SSergey Zigachev 	if (mem_clock <= 55000)
1141*b843c749SSergey Zigachev 		return 0x9;
1142*b843c749SSergey Zigachev 	if (mem_clock <= 60000)
1143*b843c749SSergey Zigachev 		return 0xa;
1144*b843c749SSergey Zigachev 	if (mem_clock <= 65000)
1145*b843c749SSergey Zigachev 		return 0xb;
1146*b843c749SSergey Zigachev 	if (mem_clock <= 70000)
1147*b843c749SSergey Zigachev 		return 0xc;
1148*b843c749SSergey Zigachev 	if (mem_clock <= 75000)
1149*b843c749SSergey Zigachev 		return 0xd;
1150*b843c749SSergey Zigachev 	if (mem_clock <= 80000)
1151*b843c749SSergey Zigachev 		return 0xe;
1152*b843c749SSergey Zigachev 	/* mem_clock > 800MHz */
1153*b843c749SSergey Zigachev 	return 0xf;
1154*b843c749SSergey Zigachev }
1155*b843c749SSergey Zigachev 
fiji_calculate_mclk_params(struct pp_hwmgr * hwmgr,uint32_t clock,struct SMU73_Discrete_MemoryLevel * mclk)1156*b843c749SSergey Zigachev static int fiji_calculate_mclk_params(struct pp_hwmgr *hwmgr,
1157*b843c749SSergey Zigachev     uint32_t clock, struct SMU73_Discrete_MemoryLevel *mclk)
1158*b843c749SSergey Zigachev {
1159*b843c749SSergey Zigachev 	struct pp_atomctrl_memory_clock_param mem_param;
1160*b843c749SSergey Zigachev 	int result;
1161*b843c749SSergey Zigachev 
1162*b843c749SSergey Zigachev 	result = atomctrl_get_memory_pll_dividers_vi(hwmgr, clock, &mem_param);
1163*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result),
1164*b843c749SSergey Zigachev 			"Failed to get Memory PLL Dividers.",
1165*b843c749SSergey Zigachev 			);
1166*b843c749SSergey Zigachev 
1167*b843c749SSergey Zigachev 	/* Save the result data to outpupt memory level structure */
1168*b843c749SSergey Zigachev 	mclk->MclkFrequency   = clock;
1169*b843c749SSergey Zigachev 	mclk->MclkDivider     = (uint8_t)mem_param.mpll_post_divider;
1170*b843c749SSergey Zigachev 	mclk->FreqRange       = fiji_get_mclk_frequency_ratio(clock);
1171*b843c749SSergey Zigachev 
1172*b843c749SSergey Zigachev 	return result;
1173*b843c749SSergey Zigachev }
1174*b843c749SSergey Zigachev 
fiji_populate_single_memory_level(struct pp_hwmgr * hwmgr,uint32_t clock,struct SMU73_Discrete_MemoryLevel * mem_level)1175*b843c749SSergey Zigachev static int fiji_populate_single_memory_level(struct pp_hwmgr *hwmgr,
1176*b843c749SSergey Zigachev 		uint32_t clock, struct SMU73_Discrete_MemoryLevel *mem_level)
1177*b843c749SSergey Zigachev {
1178*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1179*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1180*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1181*b843c749SSergey Zigachev 	int result = 0;
1182*b843c749SSergey Zigachev 	uint32_t mclk_stutter_mode_threshold = 60000;
1183*b843c749SSergey Zigachev 	phm_ppt_v1_clock_voltage_dependency_table *vdd_dep_table = NULL;
1184*b843c749SSergey Zigachev 
1185*b843c749SSergey Zigachev 	if (hwmgr->od_enabled)
1186*b843c749SSergey Zigachev 		vdd_dep_table = (phm_ppt_v1_clock_voltage_dependency_table *)&data->odn_dpm_table.vdd_dependency_on_mclk;
1187*b843c749SSergey Zigachev 	else
1188*b843c749SSergey Zigachev 		vdd_dep_table = table_info->vdd_dep_on_mclk;
1189*b843c749SSergey Zigachev 
1190*b843c749SSergey Zigachev 	if (vdd_dep_table) {
1191*b843c749SSergey Zigachev 		result = fiji_get_dependency_volt_by_clk(hwmgr,
1192*b843c749SSergey Zigachev 				vdd_dep_table, clock,
1193*b843c749SSergey Zigachev 				(uint32_t *)(&mem_level->MinVoltage), &mem_level->MinMvdd);
1194*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1195*b843c749SSergey Zigachev 				"can not find MinVddc voltage value from memory "
1196*b843c749SSergey Zigachev 				"VDDC voltage dependency table", return result);
1197*b843c749SSergey Zigachev 	}
1198*b843c749SSergey Zigachev 
1199*b843c749SSergey Zigachev 	mem_level->EnabledForThrottle = 1;
1200*b843c749SSergey Zigachev 	mem_level->EnabledForActivity = 0;
1201*b843c749SSergey Zigachev 	mem_level->UpHyst = data->current_profile_setting.mclk_up_hyst;
1202*b843c749SSergey Zigachev 	mem_level->DownHyst = data->current_profile_setting.mclk_down_hyst;
1203*b843c749SSergey Zigachev 	mem_level->VoltageDownHyst = 0;
1204*b843c749SSergey Zigachev 	mem_level->ActivityLevel = data->current_profile_setting.mclk_activity;
1205*b843c749SSergey Zigachev 	mem_level->StutterEnable = false;
1206*b843c749SSergey Zigachev 
1207*b843c749SSergey Zigachev 	mem_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
1208*b843c749SSergey Zigachev 
1209*b843c749SSergey Zigachev 	/* enable stutter mode if all the follow condition applied
1210*b843c749SSergey Zigachev 	 * PECI_GetNumberOfActiveDisplays(hwmgr->pPECI,
1211*b843c749SSergey Zigachev 	 * &(data->DisplayTiming.numExistingDisplays));
1212*b843c749SSergey Zigachev 	 */
1213*b843c749SSergey Zigachev 	data->display_timing.num_existing_displays = 1;
1214*b843c749SSergey Zigachev 
1215*b843c749SSergey Zigachev 	if (mclk_stutter_mode_threshold &&
1216*b843c749SSergey Zigachev 		(clock <= mclk_stutter_mode_threshold) &&
1217*b843c749SSergey Zigachev 		(!data->is_uvd_enabled) &&
1218*b843c749SSergey Zigachev 		(PHM_READ_FIELD(hwmgr->device, DPG_PIPE_STUTTER_CONTROL,
1219*b843c749SSergey Zigachev 				STUTTER_ENABLE) & 0x1))
1220*b843c749SSergey Zigachev 		mem_level->StutterEnable = true;
1221*b843c749SSergey Zigachev 
1222*b843c749SSergey Zigachev 	result = fiji_calculate_mclk_params(hwmgr, clock, mem_level);
1223*b843c749SSergey Zigachev 	if (!result) {
1224*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinMvdd);
1225*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MclkFrequency);
1226*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(mem_level->ActivityLevel);
1227*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinVoltage);
1228*b843c749SSergey Zigachev 	}
1229*b843c749SSergey Zigachev 	return result;
1230*b843c749SSergey Zigachev }
1231*b843c749SSergey Zigachev 
fiji_populate_all_memory_levels(struct pp_hwmgr * hwmgr)1232*b843c749SSergey Zigachev static int fiji_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
1233*b843c749SSergey Zigachev {
1234*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1235*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
1236*b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
1237*b843c749SSergey Zigachev 	int result;
1238*b843c749SSergey Zigachev 	/* populate MCLK dpm table to SMU7 */
1239*b843c749SSergey Zigachev 	uint32_t array = smu_data->smu7_data.dpm_table_start +
1240*b843c749SSergey Zigachev 			offsetof(SMU73_Discrete_DpmTable, MemoryLevel);
1241*b843c749SSergey Zigachev 	uint32_t array_size = sizeof(SMU73_Discrete_MemoryLevel) *
1242*b843c749SSergey Zigachev 			SMU73_MAX_LEVELS_MEMORY;
1243*b843c749SSergey Zigachev 	struct SMU73_Discrete_MemoryLevel *levels =
1244*b843c749SSergey Zigachev 			smu_data->smc_state_table.MemoryLevel;
1245*b843c749SSergey Zigachev 	uint32_t i;
1246*b843c749SSergey Zigachev 
1247*b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->mclk_table.count; i++) {
1248*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 != dpm_table->mclk_table.dpm_levels[i].value),
1249*b843c749SSergey Zigachev 				"can not populate memory level as memory clock is zero",
1250*b843c749SSergey Zigachev 				return -EINVAL);
1251*b843c749SSergey Zigachev 		result = fiji_populate_single_memory_level(hwmgr,
1252*b843c749SSergey Zigachev 				dpm_table->mclk_table.dpm_levels[i].value,
1253*b843c749SSergey Zigachev 				&levels[i]);
1254*b843c749SSergey Zigachev 		if (result)
1255*b843c749SSergey Zigachev 			return result;
1256*b843c749SSergey Zigachev 	}
1257*b843c749SSergey Zigachev 
1258*b843c749SSergey Zigachev 	/* Only enable level 0 for now. */
1259*b843c749SSergey Zigachev 	levels[0].EnabledForActivity = 1;
1260*b843c749SSergey Zigachev 
1261*b843c749SSergey Zigachev 	/* in order to prevent MC activity from stutter mode to push DPM up.
1262*b843c749SSergey Zigachev 	 * the UVD change complements this by putting the MCLK in
1263*b843c749SSergey Zigachev 	 * a higher state by default such that we are not effected by
1264*b843c749SSergey Zigachev 	 * up threshold or and MCLK DPM latency.
1265*b843c749SSergey Zigachev 	 */
1266*b843c749SSergey Zigachev 	levels[0].ActivityLevel = (uint16_t)data->mclk_dpm0_activity_target;
1267*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(levels[0].ActivityLevel);
1268*b843c749SSergey Zigachev 
1269*b843c749SSergey Zigachev 	smu_data->smc_state_table.MemoryDpmLevelCount =
1270*b843c749SSergey Zigachev 			(uint8_t)dpm_table->mclk_table.count;
1271*b843c749SSergey Zigachev 	data->dpm_level_enable_mask.mclk_dpm_enable_mask =
1272*b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
1273*b843c749SSergey Zigachev 	/* set highest level watermark to high */
1274*b843c749SSergey Zigachev 	levels[dpm_table->mclk_table.count - 1].DisplayWatermark =
1275*b843c749SSergey Zigachev 			PPSMC_DISPLAY_WATERMARK_HIGH;
1276*b843c749SSergey Zigachev 
1277*b843c749SSergey Zigachev 	/* level count will send to smc once at init smc table and never change */
1278*b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr, array, (uint8_t *)levels,
1279*b843c749SSergey Zigachev 			(uint32_t)array_size, SMC_RAM_END);
1280*b843c749SSergey Zigachev 
1281*b843c749SSergey Zigachev 	return result;
1282*b843c749SSergey Zigachev }
1283*b843c749SSergey Zigachev 
fiji_populate_mvdd_value(struct pp_hwmgr * hwmgr,uint32_t mclk,SMIO_Pattern * smio_pat)1284*b843c749SSergey Zigachev static int fiji_populate_mvdd_value(struct pp_hwmgr *hwmgr,
1285*b843c749SSergey Zigachev 		uint32_t mclk, SMIO_Pattern *smio_pat)
1286*b843c749SSergey Zigachev {
1287*b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1288*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1289*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1290*b843c749SSergey Zigachev 	uint32_t i = 0;
1291*b843c749SSergey Zigachev 
1292*b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE != data->mvdd_control) {
1293*b843c749SSergey Zigachev 		/* find mvdd value which clock is more than request */
1294*b843c749SSergey Zigachev 		for (i = 0; i < table_info->vdd_dep_on_mclk->count; i++) {
1295*b843c749SSergey Zigachev 			if (mclk <= table_info->vdd_dep_on_mclk->entries[i].clk) {
1296*b843c749SSergey Zigachev 				smio_pat->Voltage = data->mvdd_voltage_table.entries[i].value;
1297*b843c749SSergey Zigachev 				break;
1298*b843c749SSergey Zigachev 			}
1299*b843c749SSergey Zigachev 		}
1300*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(i < table_info->vdd_dep_on_mclk->count,
1301*b843c749SSergey Zigachev 				"MVDD Voltage is outside the supported range.",
1302*b843c749SSergey Zigachev 				return -EINVAL);
1303*b843c749SSergey Zigachev 	} else
1304*b843c749SSergey Zigachev 		return -EINVAL;
1305*b843c749SSergey Zigachev 
1306*b843c749SSergey Zigachev 	return 0;
1307*b843c749SSergey Zigachev }
1308*b843c749SSergey Zigachev 
fiji_populate_smc_acpi_level(struct pp_hwmgr * hwmgr,SMU73_Discrete_DpmTable * table)1309*b843c749SSergey Zigachev static int fiji_populate_smc_acpi_level(struct pp_hwmgr *hwmgr,
1310*b843c749SSergey Zigachev 		SMU73_Discrete_DpmTable *table)
1311*b843c749SSergey Zigachev {
1312*b843c749SSergey Zigachev 	int result = 0;
1313*b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1314*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1315*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1316*b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
1317*b843c749SSergey Zigachev 	SMIO_Pattern vol_level;
1318*b843c749SSergey Zigachev 	uint32_t mvdd;
1319*b843c749SSergey Zigachev 	uint16_t us_mvdd;
1320*b843c749SSergey Zigachev 	uint32_t spll_func_cntl    = data->clock_registers.vCG_SPLL_FUNC_CNTL;
1321*b843c749SSergey Zigachev 	uint32_t spll_func_cntl_2  = data->clock_registers.vCG_SPLL_FUNC_CNTL_2;
1322*b843c749SSergey Zigachev 
1323*b843c749SSergey Zigachev 	table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
1324*b843c749SSergey Zigachev 
1325*b843c749SSergey Zigachev 	if (!data->sclk_dpm_key_disabled) {
1326*b843c749SSergey Zigachev 		/* Get MinVoltage and Frequency from DPM0,
1327*b843c749SSergey Zigachev 		 * already converted to SMC_UL */
1328*b843c749SSergey Zigachev 		table->ACPILevel.SclkFrequency =
1329*b843c749SSergey Zigachev 				data->dpm_table.sclk_table.dpm_levels[0].value;
1330*b843c749SSergey Zigachev 		result = fiji_get_dependency_volt_by_clk(hwmgr,
1331*b843c749SSergey Zigachev 				table_info->vdd_dep_on_sclk,
1332*b843c749SSergey Zigachev 				table->ACPILevel.SclkFrequency,
1333*b843c749SSergey Zigachev 				(uint32_t *)(&table->ACPILevel.MinVoltage), &mvdd);
1334*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1335*b843c749SSergey Zigachev 				"Cannot find ACPI VDDC voltage value " \
1336*b843c749SSergey Zigachev 				"in Clock Dependency Table",
1337*b843c749SSergey Zigachev 				);
1338*b843c749SSergey Zigachev 	} else {
1339*b843c749SSergey Zigachev 		table->ACPILevel.SclkFrequency =
1340*b843c749SSergey Zigachev 				data->vbios_boot_state.sclk_bootup_value;
1341*b843c749SSergey Zigachev 		table->ACPILevel.MinVoltage =
1342*b843c749SSergey Zigachev 				data->vbios_boot_state.vddc_bootup_value * VOLTAGE_SCALE;
1343*b843c749SSergey Zigachev 	}
1344*b843c749SSergey Zigachev 
1345*b843c749SSergey Zigachev 	/* get the engine clock dividers for this clock value */
1346*b843c749SSergey Zigachev 	result = atomctrl_get_engine_pll_dividers_vi(hwmgr,
1347*b843c749SSergey Zigachev 			table->ACPILevel.SclkFrequency,  &dividers);
1348*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0,
1349*b843c749SSergey Zigachev 			"Error retrieving Engine Clock dividers from VBIOS.",
1350*b843c749SSergey Zigachev 			return result);
1351*b843c749SSergey Zigachev 
1352*b843c749SSergey Zigachev 	table->ACPILevel.SclkDid = (uint8_t)dividers.pll_post_divider;
1353*b843c749SSergey Zigachev 	table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
1354*b843c749SSergey Zigachev 	table->ACPILevel.DeepSleepDivId = 0;
1355*b843c749SSergey Zigachev 
1356*b843c749SSergey Zigachev 	spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
1357*b843c749SSergey Zigachev 			SPLL_PWRON, 0);
1358*b843c749SSergey Zigachev 	spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
1359*b843c749SSergey Zigachev 			SPLL_RESET, 1);
1360*b843c749SSergey Zigachev 	spll_func_cntl_2 = PHM_SET_FIELD(spll_func_cntl_2, CG_SPLL_FUNC_CNTL_2,
1361*b843c749SSergey Zigachev 			SCLK_MUX_SEL, 4);
1362*b843c749SSergey Zigachev 
1363*b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
1364*b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
1365*b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl3 = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
1366*b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl4 = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
1367*b843c749SSergey Zigachev 	table->ACPILevel.SpllSpreadSpectrum = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
1368*b843c749SSergey Zigachev 	table->ACPILevel.SpllSpreadSpectrum2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
1369*b843c749SSergey Zigachev 	table->ACPILevel.CcPwrDynRm = 0;
1370*b843c749SSergey Zigachev 	table->ACPILevel.CcPwrDynRm1 = 0;
1371*b843c749SSergey Zigachev 
1372*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.Flags);
1373*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SclkFrequency);
1374*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.MinVoltage);
1375*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl);
1376*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl2);
1377*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl3);
1378*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl4);
1379*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum);
1380*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum2);
1381*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm);
1382*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm1);
1383*b843c749SSergey Zigachev 
1384*b843c749SSergey Zigachev 	if (!data->mclk_dpm_key_disabled) {
1385*b843c749SSergey Zigachev 		/* Get MinVoltage and Frequency from DPM0, already converted to SMC_UL */
1386*b843c749SSergey Zigachev 		table->MemoryACPILevel.MclkFrequency =
1387*b843c749SSergey Zigachev 				data->dpm_table.mclk_table.dpm_levels[0].value;
1388*b843c749SSergey Zigachev 		result = fiji_get_dependency_volt_by_clk(hwmgr,
1389*b843c749SSergey Zigachev 				table_info->vdd_dep_on_mclk,
1390*b843c749SSergey Zigachev 				table->MemoryACPILevel.MclkFrequency,
1391*b843c749SSergey Zigachev 			(uint32_t *)(&table->MemoryACPILevel.MinVoltage), &mvdd);
1392*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1393*b843c749SSergey Zigachev 				"Cannot find ACPI VDDCI voltage value in Clock Dependency Table",
1394*b843c749SSergey Zigachev 				);
1395*b843c749SSergey Zigachev 	} else {
1396*b843c749SSergey Zigachev 		table->MemoryACPILevel.MclkFrequency =
1397*b843c749SSergey Zigachev 				data->vbios_boot_state.mclk_bootup_value;
1398*b843c749SSergey Zigachev 		table->MemoryACPILevel.MinVoltage =
1399*b843c749SSergey Zigachev 				data->vbios_boot_state.vddci_bootup_value * VOLTAGE_SCALE;
1400*b843c749SSergey Zigachev 	}
1401*b843c749SSergey Zigachev 
1402*b843c749SSergey Zigachev 	us_mvdd = 0;
1403*b843c749SSergey Zigachev 	if ((SMU7_VOLTAGE_CONTROL_NONE == data->mvdd_control) ||
1404*b843c749SSergey Zigachev 			(data->mclk_dpm_key_disabled))
1405*b843c749SSergey Zigachev 		us_mvdd = data->vbios_boot_state.mvdd_bootup_value;
1406*b843c749SSergey Zigachev 	else {
1407*b843c749SSergey Zigachev 		if (!fiji_populate_mvdd_value(hwmgr,
1408*b843c749SSergey Zigachev 				data->dpm_table.mclk_table.dpm_levels[0].value,
1409*b843c749SSergey Zigachev 				&vol_level))
1410*b843c749SSergey Zigachev 			us_mvdd = vol_level.Voltage;
1411*b843c749SSergey Zigachev 	}
1412*b843c749SSergey Zigachev 
1413*b843c749SSergey Zigachev 	table->MemoryACPILevel.MinMvdd =
1414*b843c749SSergey Zigachev 			PP_HOST_TO_SMC_UL(us_mvdd * VOLTAGE_SCALE);
1415*b843c749SSergey Zigachev 
1416*b843c749SSergey Zigachev 	table->MemoryACPILevel.EnabledForThrottle = 0;
1417*b843c749SSergey Zigachev 	table->MemoryACPILevel.EnabledForActivity = 0;
1418*b843c749SSergey Zigachev 	table->MemoryACPILevel.UpHyst = 0;
1419*b843c749SSergey Zigachev 	table->MemoryACPILevel.DownHyst = 100;
1420*b843c749SSergey Zigachev 	table->MemoryACPILevel.VoltageDownHyst = 0;
1421*b843c749SSergey Zigachev 	table->MemoryACPILevel.ActivityLevel =
1422*b843c749SSergey Zigachev 			PP_HOST_TO_SMC_US(data->current_profile_setting.mclk_activity);
1423*b843c749SSergey Zigachev 
1424*b843c749SSergey Zigachev 	table->MemoryACPILevel.StutterEnable = false;
1425*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MclkFrequency);
1426*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MinVoltage);
1427*b843c749SSergey Zigachev 
1428*b843c749SSergey Zigachev 	return result;
1429*b843c749SSergey Zigachev }
1430*b843c749SSergey Zigachev 
fiji_populate_smc_vce_level(struct pp_hwmgr * hwmgr,SMU73_Discrete_DpmTable * table)1431*b843c749SSergey Zigachev static int fiji_populate_smc_vce_level(struct pp_hwmgr *hwmgr,
1432*b843c749SSergey Zigachev 		SMU73_Discrete_DpmTable *table)
1433*b843c749SSergey Zigachev {
1434*b843c749SSergey Zigachev 	int result = -EINVAL;
1435*b843c749SSergey Zigachev 	uint8_t count;
1436*b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
1437*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1438*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1439*b843c749SSergey Zigachev 	struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1440*b843c749SSergey Zigachev 			table_info->mm_dep_table;
1441*b843c749SSergey Zigachev 
1442*b843c749SSergey Zigachev 	table->VceLevelCount = (uint8_t)(mm_table->count);
1443*b843c749SSergey Zigachev 	table->VceBootLevel = 0;
1444*b843c749SSergey Zigachev 
1445*b843c749SSergey Zigachev 	for (count = 0; count < table->VceLevelCount; count++) {
1446*b843c749SSergey Zigachev 		table->VceLevel[count].Frequency = mm_table->entries[count].eclk;
1447*b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage = 0;
1448*b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage |=
1449*b843c749SSergey Zigachev 				(mm_table->entries[count].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
1450*b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage |=
1451*b843c749SSergey Zigachev 				((mm_table->entries[count].vddc - VDDC_VDDCI_DELTA) *
1452*b843c749SSergey Zigachev 						VOLTAGE_SCALE) << VDDCI_SHIFT;
1453*b843c749SSergey Zigachev 		table->VceLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1454*b843c749SSergey Zigachev 
1455*b843c749SSergey Zigachev 		/*retrieve divider value for VBIOS */
1456*b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1457*b843c749SSergey Zigachev 				table->VceLevel[count].Frequency, &dividers);
1458*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1459*b843c749SSergey Zigachev 				"can not find divide id for VCE engine clock",
1460*b843c749SSergey Zigachev 				return result);
1461*b843c749SSergey Zigachev 
1462*b843c749SSergey Zigachev 		table->VceLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
1463*b843c749SSergey Zigachev 
1464*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].Frequency);
1465*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].MinVoltage);
1466*b843c749SSergey Zigachev 	}
1467*b843c749SSergey Zigachev 	return result;
1468*b843c749SSergey Zigachev }
1469*b843c749SSergey Zigachev 
fiji_populate_smc_acp_level(struct pp_hwmgr * hwmgr,SMU73_Discrete_DpmTable * table)1470*b843c749SSergey Zigachev static int fiji_populate_smc_acp_level(struct pp_hwmgr *hwmgr,
1471*b843c749SSergey Zigachev 		SMU73_Discrete_DpmTable *table)
1472*b843c749SSergey Zigachev {
1473*b843c749SSergey Zigachev 	int result = -EINVAL;
1474*b843c749SSergey Zigachev 	uint8_t count;
1475*b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
1476*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1477*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1478*b843c749SSergey Zigachev 	struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1479*b843c749SSergey Zigachev 			table_info->mm_dep_table;
1480*b843c749SSergey Zigachev 
1481*b843c749SSergey Zigachev 	table->AcpLevelCount = (uint8_t)(mm_table->count);
1482*b843c749SSergey Zigachev 	table->AcpBootLevel = 0;
1483*b843c749SSergey Zigachev 
1484*b843c749SSergey Zigachev 	for (count = 0; count < table->AcpLevelCount; count++) {
1485*b843c749SSergey Zigachev 		table->AcpLevel[count].Frequency = mm_table->entries[count].aclk;
1486*b843c749SSergey Zigachev 		table->AcpLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
1487*b843c749SSergey Zigachev 				VOLTAGE_SCALE) << VDDC_SHIFT;
1488*b843c749SSergey Zigachev 		table->AcpLevel[count].MinVoltage |= ((mm_table->entries[count].vddc -
1489*b843c749SSergey Zigachev 				VDDC_VDDCI_DELTA) * VOLTAGE_SCALE) << VDDCI_SHIFT;
1490*b843c749SSergey Zigachev 		table->AcpLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1491*b843c749SSergey Zigachev 
1492*b843c749SSergey Zigachev 		/* retrieve divider value for VBIOS */
1493*b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1494*b843c749SSergey Zigachev 				table->AcpLevel[count].Frequency, &dividers);
1495*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1496*b843c749SSergey Zigachev 				"can not find divide id for engine clock", return result);
1497*b843c749SSergey Zigachev 
1498*b843c749SSergey Zigachev 		table->AcpLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
1499*b843c749SSergey Zigachev 
1500*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->AcpLevel[count].Frequency);
1501*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->AcpLevel[count].MinVoltage);
1502*b843c749SSergey Zigachev 	}
1503*b843c749SSergey Zigachev 	return result;
1504*b843c749SSergey Zigachev }
1505*b843c749SSergey Zigachev 
fiji_populate_memory_timing_parameters(struct pp_hwmgr * hwmgr,int32_t eng_clock,int32_t mem_clock,struct SMU73_Discrete_MCArbDramTimingTableEntry * arb_regs)1506*b843c749SSergey Zigachev static int fiji_populate_memory_timing_parameters(struct pp_hwmgr *hwmgr,
1507*b843c749SSergey Zigachev 		int32_t eng_clock, int32_t mem_clock,
1508*b843c749SSergey Zigachev 		struct SMU73_Discrete_MCArbDramTimingTableEntry *arb_regs)
1509*b843c749SSergey Zigachev {
1510*b843c749SSergey Zigachev 	uint32_t dram_timing;
1511*b843c749SSergey Zigachev 	uint32_t dram_timing2;
1512*b843c749SSergey Zigachev 	uint32_t burstTime;
1513*b843c749SSergey Zigachev 	ULONG state, trrds, trrdl;
1514*b843c749SSergey Zigachev 	int result;
1515*b843c749SSergey Zigachev 
1516*b843c749SSergey Zigachev 	result = atomctrl_set_engine_dram_timings_rv770(hwmgr,
1517*b843c749SSergey Zigachev 			eng_clock, mem_clock);
1518*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0,
1519*b843c749SSergey Zigachev 			"Error calling VBIOS to set DRAM_TIMING.", return result);
1520*b843c749SSergey Zigachev 
1521*b843c749SSergey Zigachev 	dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
1522*b843c749SSergey Zigachev 	dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
1523*b843c749SSergey Zigachev 	burstTime = cgs_read_register(hwmgr->device, mmMC_ARB_BURST_TIME);
1524*b843c749SSergey Zigachev 
1525*b843c749SSergey Zigachev 	state = PHM_GET_FIELD(burstTime, MC_ARB_BURST_TIME, STATE0);
1526*b843c749SSergey Zigachev 	trrds = PHM_GET_FIELD(burstTime, MC_ARB_BURST_TIME, TRRDS0);
1527*b843c749SSergey Zigachev 	trrdl = PHM_GET_FIELD(burstTime, MC_ARB_BURST_TIME, TRRDL0);
1528*b843c749SSergey Zigachev 
1529*b843c749SSergey Zigachev 	arb_regs->McArbDramTiming  = PP_HOST_TO_SMC_UL(dram_timing);
1530*b843c749SSergey Zigachev 	arb_regs->McArbDramTiming2 = PP_HOST_TO_SMC_UL(dram_timing2);
1531*b843c749SSergey Zigachev 	arb_regs->McArbBurstTime   = (uint8_t)burstTime;
1532*b843c749SSergey Zigachev 	arb_regs->TRRDS            = (uint8_t)trrds;
1533*b843c749SSergey Zigachev 	arb_regs->TRRDL            = (uint8_t)trrdl;
1534*b843c749SSergey Zigachev 
1535*b843c749SSergey Zigachev 	return 0;
1536*b843c749SSergey Zigachev }
1537*b843c749SSergey Zigachev 
fiji_program_memory_timing_parameters(struct pp_hwmgr * hwmgr)1538*b843c749SSergey Zigachev static int fiji_program_memory_timing_parameters(struct pp_hwmgr *hwmgr)
1539*b843c749SSergey Zigachev {
1540*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1541*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
1542*b843c749SSergey Zigachev 	struct SMU73_Discrete_MCArbDramTimingTable arb_regs;
1543*b843c749SSergey Zigachev 	uint32_t i, j;
1544*b843c749SSergey Zigachev 	int result = 0;
1545*b843c749SSergey Zigachev 
1546*b843c749SSergey Zigachev 	for (i = 0; i < data->dpm_table.sclk_table.count; i++) {
1547*b843c749SSergey Zigachev 		for (j = 0; j < data->dpm_table.mclk_table.count; j++) {
1548*b843c749SSergey Zigachev 			result = fiji_populate_memory_timing_parameters(hwmgr,
1549*b843c749SSergey Zigachev 					data->dpm_table.sclk_table.dpm_levels[i].value,
1550*b843c749SSergey Zigachev 					data->dpm_table.mclk_table.dpm_levels[j].value,
1551*b843c749SSergey Zigachev 					&arb_regs.entries[i][j]);
1552*b843c749SSergey Zigachev 			if (result)
1553*b843c749SSergey Zigachev 				break;
1554*b843c749SSergey Zigachev 		}
1555*b843c749SSergey Zigachev 	}
1556*b843c749SSergey Zigachev 
1557*b843c749SSergey Zigachev 	if (!result)
1558*b843c749SSergey Zigachev 		result = smu7_copy_bytes_to_smc(
1559*b843c749SSergey Zigachev 				hwmgr,
1560*b843c749SSergey Zigachev 				smu_data->smu7_data.arb_table_start,
1561*b843c749SSergey Zigachev 				(uint8_t *)&arb_regs,
1562*b843c749SSergey Zigachev 				sizeof(SMU73_Discrete_MCArbDramTimingTable),
1563*b843c749SSergey Zigachev 				SMC_RAM_END);
1564*b843c749SSergey Zigachev 	return result;
1565*b843c749SSergey Zigachev }
1566*b843c749SSergey Zigachev 
fiji_populate_smc_uvd_level(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_DpmTable * table)1567*b843c749SSergey Zigachev static int fiji_populate_smc_uvd_level(struct pp_hwmgr *hwmgr,
1568*b843c749SSergey Zigachev 		struct SMU73_Discrete_DpmTable *table)
1569*b843c749SSergey Zigachev {
1570*b843c749SSergey Zigachev 	int result = -EINVAL;
1571*b843c749SSergey Zigachev 	uint8_t count;
1572*b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
1573*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1574*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1575*b843c749SSergey Zigachev 	struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1576*b843c749SSergey Zigachev 			table_info->mm_dep_table;
1577*b843c749SSergey Zigachev 
1578*b843c749SSergey Zigachev 	table->UvdLevelCount = (uint8_t)(mm_table->count);
1579*b843c749SSergey Zigachev 	table->UvdBootLevel = 0;
1580*b843c749SSergey Zigachev 
1581*b843c749SSergey Zigachev 	for (count = 0; count < table->UvdLevelCount; count++) {
1582*b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage = 0;
1583*b843c749SSergey Zigachev 		table->UvdLevel[count].VclkFrequency = mm_table->entries[count].vclk;
1584*b843c749SSergey Zigachev 		table->UvdLevel[count].DclkFrequency = mm_table->entries[count].dclk;
1585*b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
1586*b843c749SSergey Zigachev 				VOLTAGE_SCALE) << VDDC_SHIFT;
1587*b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage |= ((mm_table->entries[count].vddc -
1588*b843c749SSergey Zigachev 				VDDC_VDDCI_DELTA) * VOLTAGE_SCALE) << VDDCI_SHIFT;
1589*b843c749SSergey Zigachev 		table->UvdLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1590*b843c749SSergey Zigachev 
1591*b843c749SSergey Zigachev 		/* retrieve divider value for VBIOS */
1592*b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1593*b843c749SSergey Zigachev 				table->UvdLevel[count].VclkFrequency, &dividers);
1594*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1595*b843c749SSergey Zigachev 				"can not find divide id for Vclk clock", return result);
1596*b843c749SSergey Zigachev 
1597*b843c749SSergey Zigachev 		table->UvdLevel[count].VclkDivider = (uint8_t)dividers.pll_post_divider;
1598*b843c749SSergey Zigachev 
1599*b843c749SSergey Zigachev 		result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1600*b843c749SSergey Zigachev 				table->UvdLevel[count].DclkFrequency, &dividers);
1601*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1602*b843c749SSergey Zigachev 				"can not find divide id for Dclk clock", return result);
1603*b843c749SSergey Zigachev 
1604*b843c749SSergey Zigachev 		table->UvdLevel[count].DclkDivider = (uint8_t)dividers.pll_post_divider;
1605*b843c749SSergey Zigachev 
1606*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].VclkFrequency);
1607*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].DclkFrequency);
1608*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].MinVoltage);
1609*b843c749SSergey Zigachev 
1610*b843c749SSergey Zigachev 	}
1611*b843c749SSergey Zigachev 	return result;
1612*b843c749SSergey Zigachev }
1613*b843c749SSergey Zigachev 
fiji_populate_smc_boot_level(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_DpmTable * table)1614*b843c749SSergey Zigachev static int fiji_populate_smc_boot_level(struct pp_hwmgr *hwmgr,
1615*b843c749SSergey Zigachev 		struct SMU73_Discrete_DpmTable *table)
1616*b843c749SSergey Zigachev {
1617*b843c749SSergey Zigachev 	int result = 0;
1618*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1619*b843c749SSergey Zigachev 
1620*b843c749SSergey Zigachev 	table->GraphicsBootLevel = 0;
1621*b843c749SSergey Zigachev 	table->MemoryBootLevel = 0;
1622*b843c749SSergey Zigachev 
1623*b843c749SSergey Zigachev 	/* find boot level from dpm table */
1624*b843c749SSergey Zigachev 	result = phm_find_boot_level(&(data->dpm_table.sclk_table),
1625*b843c749SSergey Zigachev 			data->vbios_boot_state.sclk_bootup_value,
1626*b843c749SSergey Zigachev 			(uint32_t *)&(table->GraphicsBootLevel));
1627*b843c749SSergey Zigachev 
1628*b843c749SSergey Zigachev 	result = phm_find_boot_level(&(data->dpm_table.mclk_table),
1629*b843c749SSergey Zigachev 			data->vbios_boot_state.mclk_bootup_value,
1630*b843c749SSergey Zigachev 			(uint32_t *)&(table->MemoryBootLevel));
1631*b843c749SSergey Zigachev 
1632*b843c749SSergey Zigachev 	table->BootVddc  = data->vbios_boot_state.vddc_bootup_value *
1633*b843c749SSergey Zigachev 			VOLTAGE_SCALE;
1634*b843c749SSergey Zigachev 	table->BootVddci = data->vbios_boot_state.vddci_bootup_value *
1635*b843c749SSergey Zigachev 			VOLTAGE_SCALE;
1636*b843c749SSergey Zigachev 	table->BootMVdd  = data->vbios_boot_state.mvdd_bootup_value *
1637*b843c749SSergey Zigachev 			VOLTAGE_SCALE;
1638*b843c749SSergey Zigachev 
1639*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->BootVddc);
1640*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->BootVddci);
1641*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->BootMVdd);
1642*b843c749SSergey Zigachev 
1643*b843c749SSergey Zigachev 	return 0;
1644*b843c749SSergey Zigachev }
1645*b843c749SSergey Zigachev 
fiji_populate_smc_initailial_state(struct pp_hwmgr * hwmgr)1646*b843c749SSergey Zigachev static int fiji_populate_smc_initailial_state(struct pp_hwmgr *hwmgr)
1647*b843c749SSergey Zigachev {
1648*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1649*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
1650*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1651*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1652*b843c749SSergey Zigachev 	uint8_t count, level;
1653*b843c749SSergey Zigachev 
1654*b843c749SSergey Zigachev 	count = (uint8_t)(table_info->vdd_dep_on_sclk->count);
1655*b843c749SSergey Zigachev 	for (level = 0; level < count; level++) {
1656*b843c749SSergey Zigachev 		if (table_info->vdd_dep_on_sclk->entries[level].clk >=
1657*b843c749SSergey Zigachev 				data->vbios_boot_state.sclk_bootup_value) {
1658*b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsBootLevel = level;
1659*b843c749SSergey Zigachev 			break;
1660*b843c749SSergey Zigachev 		}
1661*b843c749SSergey Zigachev 	}
1662*b843c749SSergey Zigachev 
1663*b843c749SSergey Zigachev 	count = (uint8_t)(table_info->vdd_dep_on_mclk->count);
1664*b843c749SSergey Zigachev 	for (level = 0; level < count; level++) {
1665*b843c749SSergey Zigachev 		if (table_info->vdd_dep_on_mclk->entries[level].clk >=
1666*b843c749SSergey Zigachev 				data->vbios_boot_state.mclk_bootup_value) {
1667*b843c749SSergey Zigachev 			smu_data->smc_state_table.MemoryBootLevel = level;
1668*b843c749SSergey Zigachev 			break;
1669*b843c749SSergey Zigachev 		}
1670*b843c749SSergey Zigachev 	}
1671*b843c749SSergey Zigachev 
1672*b843c749SSergey Zigachev 	return 0;
1673*b843c749SSergey Zigachev }
1674*b843c749SSergey Zigachev 
fiji_populate_clock_stretcher_data_table(struct pp_hwmgr * hwmgr)1675*b843c749SSergey Zigachev static int fiji_populate_clock_stretcher_data_table(struct pp_hwmgr *hwmgr)
1676*b843c749SSergey Zigachev {
1677*b843c749SSergey Zigachev 	uint32_t ro, efuse, efuse2, clock_freq, volt_without_cks,
1678*b843c749SSergey Zigachev 			volt_with_cks, value;
1679*b843c749SSergey Zigachev 	uint16_t clock_freq_u16;
1680*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
1681*b843c749SSergey Zigachev 	uint8_t type, i, j, cks_setting, stretch_amount, stretch_amount2,
1682*b843c749SSergey Zigachev 			volt_offset = 0;
1683*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1684*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1685*b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
1686*b843c749SSergey Zigachev 			table_info->vdd_dep_on_sclk;
1687*b843c749SSergey Zigachev 
1688*b843c749SSergey Zigachev 	stretch_amount = (uint8_t)table_info->cac_dtp_table->usClockStretchAmount;
1689*b843c749SSergey Zigachev 
1690*b843c749SSergey Zigachev 	/* Read SMU_Eefuse to read and calculate RO and determine
1691*b843c749SSergey Zigachev 	 * if the part is SS or FF. if RO >= 1660MHz, part is FF.
1692*b843c749SSergey Zigachev 	 */
1693*b843c749SSergey Zigachev 	efuse = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1694*b843c749SSergey Zigachev 			ixSMU_EFUSE_0 + (146 * 4));
1695*b843c749SSergey Zigachev 	efuse2 = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1696*b843c749SSergey Zigachev 			ixSMU_EFUSE_0 + (148 * 4));
1697*b843c749SSergey Zigachev 	efuse &= 0xFF000000;
1698*b843c749SSergey Zigachev 	efuse = efuse >> 24;
1699*b843c749SSergey Zigachev 	efuse2 &= 0xF;
1700*b843c749SSergey Zigachev 
1701*b843c749SSergey Zigachev 	if (efuse2 == 1)
1702*b843c749SSergey Zigachev 		ro = (2300 - 1350) * efuse / 255 + 1350;
1703*b843c749SSergey Zigachev 	else
1704*b843c749SSergey Zigachev 		ro = (2500 - 1000) * efuse / 255 + 1000;
1705*b843c749SSergey Zigachev 
1706*b843c749SSergey Zigachev 	if (ro >= 1660)
1707*b843c749SSergey Zigachev 		type = 0;
1708*b843c749SSergey Zigachev 	else
1709*b843c749SSergey Zigachev 		type = 1;
1710*b843c749SSergey Zigachev 
1711*b843c749SSergey Zigachev 	/* Populate Stretch amount */
1712*b843c749SSergey Zigachev 	smu_data->smc_state_table.ClockStretcherAmount = stretch_amount;
1713*b843c749SSergey Zigachev 
1714*b843c749SSergey Zigachev 	/* Populate Sclk_CKS_masterEn0_7 and Sclk_voltageOffset */
1715*b843c749SSergey Zigachev 	for (i = 0; i < sclk_table->count; i++) {
1716*b843c749SSergey Zigachev 		smu_data->smc_state_table.Sclk_CKS_masterEn0_7 |=
1717*b843c749SSergey Zigachev 				sclk_table->entries[i].cks_enable << i;
1718*b843c749SSergey Zigachev 		volt_without_cks = (uint32_t)((14041 *
1719*b843c749SSergey Zigachev 			(sclk_table->entries[i].clk/100) / 10000 + 3571 + 75 - ro) * 1000 /
1720*b843c749SSergey Zigachev 			(4026 - (13924 * (sclk_table->entries[i].clk/100) / 10000)));
1721*b843c749SSergey Zigachev 		volt_with_cks = (uint32_t)((13946 *
1722*b843c749SSergey Zigachev 			(sclk_table->entries[i].clk/100) / 10000 + 3320 + 45 - ro) * 1000 /
1723*b843c749SSergey Zigachev 			(3664 - (11454 * (sclk_table->entries[i].clk/100) / 10000)));
1724*b843c749SSergey Zigachev 		if (volt_without_cks >= volt_with_cks)
1725*b843c749SSergey Zigachev 			volt_offset = (uint8_t)(((volt_without_cks - volt_with_cks +
1726*b843c749SSergey Zigachev 					sclk_table->entries[i].cks_voffset) * 100 / 625) + 1);
1727*b843c749SSergey Zigachev 		smu_data->smc_state_table.Sclk_voltageOffset[i] = volt_offset;
1728*b843c749SSergey Zigachev 	}
1729*b843c749SSergey Zigachev 
1730*b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
1731*b843c749SSergey Zigachev 			STRETCH_ENABLE, 0x0);
1732*b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
1733*b843c749SSergey Zigachev 			masterReset, 0x1);
1734*b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
1735*b843c749SSergey Zigachev 			staticEnable, 0x1);
1736*b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
1737*b843c749SSergey Zigachev 			masterReset, 0x0);
1738*b843c749SSergey Zigachev 
1739*b843c749SSergey Zigachev 	/* Populate CKS Lookup Table */
1740*b843c749SSergey Zigachev 	if (stretch_amount == 1 || stretch_amount == 2 || stretch_amount == 5)
1741*b843c749SSergey Zigachev 		stretch_amount2 = 0;
1742*b843c749SSergey Zigachev 	else if (stretch_amount == 3 || stretch_amount == 4)
1743*b843c749SSergey Zigachev 		stretch_amount2 = 1;
1744*b843c749SSergey Zigachev 	else {
1745*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1746*b843c749SSergey Zigachev 				PHM_PlatformCaps_ClockStretcher);
1747*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
1748*b843c749SSergey Zigachev 				"Stretch Amount in PPTable not supported",
1749*b843c749SSergey Zigachev 				return -EINVAL);
1750*b843c749SSergey Zigachev 	}
1751*b843c749SSergey Zigachev 
1752*b843c749SSergey Zigachev 	value = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1753*b843c749SSergey Zigachev 			ixPWR_CKS_CNTL);
1754*b843c749SSergey Zigachev 	value &= 0xFFC2FF87;
1755*b843c749SSergey Zigachev 	smu_data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].minFreq =
1756*b843c749SSergey Zigachev 			fiji_clock_stretcher_lookup_table[stretch_amount2][0];
1757*b843c749SSergey Zigachev 	smu_data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].maxFreq =
1758*b843c749SSergey Zigachev 			fiji_clock_stretcher_lookup_table[stretch_amount2][1];
1759*b843c749SSergey Zigachev 	clock_freq_u16 = (uint16_t)(PP_SMC_TO_HOST_UL(smu_data->smc_state_table.
1760*b843c749SSergey Zigachev 			GraphicsLevel[smu_data->smc_state_table.GraphicsDpmLevelCount - 1].
1761*b843c749SSergey Zigachev 			SclkFrequency) / 100);
1762*b843c749SSergey Zigachev 	if (fiji_clock_stretcher_lookup_table[stretch_amount2][0] <
1763*b843c749SSergey Zigachev 			clock_freq_u16 &&
1764*b843c749SSergey Zigachev 	    fiji_clock_stretcher_lookup_table[stretch_amount2][1] >
1765*b843c749SSergey Zigachev 			clock_freq_u16) {
1766*b843c749SSergey Zigachev 		/* Program PWR_CKS_CNTL. CKS_USE_FOR_LOW_FREQ */
1767*b843c749SSergey Zigachev 		value |= (fiji_clock_stretcher_lookup_table[stretch_amount2][3]) << 16;
1768*b843c749SSergey Zigachev 		/* Program PWR_CKS_CNTL. CKS_LDO_REFSEL */
1769*b843c749SSergey Zigachev 		value |= (fiji_clock_stretcher_lookup_table[stretch_amount2][2]) << 18;
1770*b843c749SSergey Zigachev 		/* Program PWR_CKS_CNTL. CKS_STRETCH_AMOUNT */
1771*b843c749SSergey Zigachev 		value |= (fiji_clock_stretch_amount_conversion
1772*b843c749SSergey Zigachev 				[fiji_clock_stretcher_lookup_table[stretch_amount2][3]]
1773*b843c749SSergey Zigachev 				 [stretch_amount]) << 3;
1774*b843c749SSergey Zigachev 	}
1775*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(smu_data->smc_state_table.CKS_LOOKUPTable.
1776*b843c749SSergey Zigachev 			CKS_LOOKUPTableEntry[0].minFreq);
1777*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(smu_data->smc_state_table.CKS_LOOKUPTable.
1778*b843c749SSergey Zigachev 			CKS_LOOKUPTableEntry[0].maxFreq);
1779*b843c749SSergey Zigachev 	smu_data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].setting =
1780*b843c749SSergey Zigachev 			fiji_clock_stretcher_lookup_table[stretch_amount2][2] & 0x7F;
1781*b843c749SSergey Zigachev 	smu_data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].setting |=
1782*b843c749SSergey Zigachev 			(fiji_clock_stretcher_lookup_table[stretch_amount2][3]) << 7;
1783*b843c749SSergey Zigachev 
1784*b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1785*b843c749SSergey Zigachev 			ixPWR_CKS_CNTL, value);
1786*b843c749SSergey Zigachev 
1787*b843c749SSergey Zigachev 	/* Populate DDT Lookup Table */
1788*b843c749SSergey Zigachev 	for (i = 0; i < 4; i++) {
1789*b843c749SSergey Zigachev 		/* Assign the minimum and maximum VID stored
1790*b843c749SSergey Zigachev 		 * in the last row of Clock Stretcher Voltage Table.
1791*b843c749SSergey Zigachev 		 */
1792*b843c749SSergey Zigachev 		smu_data->smc_state_table.ClockStretcherDataTable.
1793*b843c749SSergey Zigachev 		ClockStretcherDataTableEntry[i].minVID =
1794*b843c749SSergey Zigachev 				(uint8_t) fiji_clock_stretcher_ddt_table[type][i][2];
1795*b843c749SSergey Zigachev 		smu_data->smc_state_table.ClockStretcherDataTable.
1796*b843c749SSergey Zigachev 		ClockStretcherDataTableEntry[i].maxVID =
1797*b843c749SSergey Zigachev 				(uint8_t) fiji_clock_stretcher_ddt_table[type][i][3];
1798*b843c749SSergey Zigachev 		/* Loop through each SCLK and check the frequency
1799*b843c749SSergey Zigachev 		 * to see if it lies within the frequency for clock stretcher.
1800*b843c749SSergey Zigachev 		 */
1801*b843c749SSergey Zigachev 		for (j = 0; j < smu_data->smc_state_table.GraphicsDpmLevelCount; j++) {
1802*b843c749SSergey Zigachev 			cks_setting = 0;
1803*b843c749SSergey Zigachev 			clock_freq = PP_SMC_TO_HOST_UL(
1804*b843c749SSergey Zigachev 					smu_data->smc_state_table.GraphicsLevel[j].SclkFrequency);
1805*b843c749SSergey Zigachev 			/* Check the allowed frequency against the sclk level[j].
1806*b843c749SSergey Zigachev 			 *  Sclk's endianness has already been converted,
1807*b843c749SSergey Zigachev 			 *  and it's in 10Khz unit,
1808*b843c749SSergey Zigachev 			 *  as opposed to Data table, which is in Mhz unit.
1809*b843c749SSergey Zigachev 			 */
1810*b843c749SSergey Zigachev 			if (clock_freq >=
1811*b843c749SSergey Zigachev 					(fiji_clock_stretcher_ddt_table[type][i][0]) * 100) {
1812*b843c749SSergey Zigachev 				cks_setting |= 0x2;
1813*b843c749SSergey Zigachev 				if (clock_freq <
1814*b843c749SSergey Zigachev 						(fiji_clock_stretcher_ddt_table[type][i][1]) * 100)
1815*b843c749SSergey Zigachev 					cks_setting |= 0x1;
1816*b843c749SSergey Zigachev 			}
1817*b843c749SSergey Zigachev 			smu_data->smc_state_table.ClockStretcherDataTable.
1818*b843c749SSergey Zigachev 			ClockStretcherDataTableEntry[i].setting |= cks_setting << (j * 2);
1819*b843c749SSergey Zigachev 		}
1820*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(smu_data->smc_state_table.
1821*b843c749SSergey Zigachev 				ClockStretcherDataTable.
1822*b843c749SSergey Zigachev 				ClockStretcherDataTableEntry[i].setting);
1823*b843c749SSergey Zigachev 	}
1824*b843c749SSergey Zigachev 
1825*b843c749SSergey Zigachev 	value = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL);
1826*b843c749SSergey Zigachev 	value &= 0xFFFFFFFE;
1827*b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL, value);
1828*b843c749SSergey Zigachev 
1829*b843c749SSergey Zigachev 	return 0;
1830*b843c749SSergey Zigachev }
1831*b843c749SSergey Zigachev 
fiji_populate_vr_config(struct pp_hwmgr * hwmgr,struct SMU73_Discrete_DpmTable * table)1832*b843c749SSergey Zigachev static int fiji_populate_vr_config(struct pp_hwmgr *hwmgr,
1833*b843c749SSergey Zigachev 		struct SMU73_Discrete_DpmTable *table)
1834*b843c749SSergey Zigachev {
1835*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1836*b843c749SSergey Zigachev 	uint16_t config;
1837*b843c749SSergey Zigachev 
1838*b843c749SSergey Zigachev 	config = VR_MERGED_WITH_VDDC;
1839*b843c749SSergey Zigachev 	table->VRConfig |= (config << VRCONF_VDDGFX_SHIFT);
1840*b843c749SSergey Zigachev 
1841*b843c749SSergey Zigachev 	/* Set Vddc Voltage Controller */
1842*b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control) {
1843*b843c749SSergey Zigachev 		config = VR_SVI2_PLANE_1;
1844*b843c749SSergey Zigachev 		table->VRConfig |= config;
1845*b843c749SSergey Zigachev 	} else {
1846*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
1847*b843c749SSergey Zigachev 				"VDDC should be on SVI2 control in merged mode!",
1848*b843c749SSergey Zigachev 				);
1849*b843c749SSergey Zigachev 	}
1850*b843c749SSergey Zigachev 	/* Set Vddci Voltage Controller */
1851*b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control) {
1852*b843c749SSergey Zigachev 		config = VR_SVI2_PLANE_2;  /* only in merged mode */
1853*b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1854*b843c749SSergey Zigachev 	} else if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
1855*b843c749SSergey Zigachev 		config = VR_SMIO_PATTERN_1;
1856*b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1857*b843c749SSergey Zigachev 	} else {
1858*b843c749SSergey Zigachev 		config = VR_STATIC_VOLTAGE;
1859*b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1860*b843c749SSergey Zigachev 	}
1861*b843c749SSergey Zigachev 	/* Set Mvdd Voltage Controller */
1862*b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control) {
1863*b843c749SSergey Zigachev 		config = VR_SVI2_PLANE_2;
1864*b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
1865*b843c749SSergey Zigachev 	} else if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control) {
1866*b843c749SSergey Zigachev 		config = VR_SMIO_PATTERN_2;
1867*b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
1868*b843c749SSergey Zigachev 	} else {
1869*b843c749SSergey Zigachev 		config = VR_STATIC_VOLTAGE;
1870*b843c749SSergey Zigachev 		table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
1871*b843c749SSergey Zigachev 	}
1872*b843c749SSergey Zigachev 
1873*b843c749SSergey Zigachev 	return 0;
1874*b843c749SSergey Zigachev }
1875*b843c749SSergey Zigachev 
fiji_init_arb_table_index(struct pp_hwmgr * hwmgr)1876*b843c749SSergey Zigachev static int fiji_init_arb_table_index(struct pp_hwmgr *hwmgr)
1877*b843c749SSergey Zigachev {
1878*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
1879*b843c749SSergey Zigachev 	uint32_t tmp;
1880*b843c749SSergey Zigachev 	int result;
1881*b843c749SSergey Zigachev 
1882*b843c749SSergey Zigachev 	/* This is a read-modify-write on the first byte of the ARB table.
1883*b843c749SSergey Zigachev 	 * The first byte in the SMU73_Discrete_MCArbDramTimingTable structure
1884*b843c749SSergey Zigachev 	 * is the field 'current'.
1885*b843c749SSergey Zigachev 	 * This solution is ugly, but we never write the whole table only
1886*b843c749SSergey Zigachev 	 * individual fields in it.
1887*b843c749SSergey Zigachev 	 * In reality this field should not be in that structure
1888*b843c749SSergey Zigachev 	 * but in a soft register.
1889*b843c749SSergey Zigachev 	 */
1890*b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
1891*b843c749SSergey Zigachev 			smu_data->smu7_data.arb_table_start, &tmp, SMC_RAM_END);
1892*b843c749SSergey Zigachev 
1893*b843c749SSergey Zigachev 	if (result)
1894*b843c749SSergey Zigachev 		return result;
1895*b843c749SSergey Zigachev 
1896*b843c749SSergey Zigachev 	tmp &= 0x00FFFFFF;
1897*b843c749SSergey Zigachev 	tmp |= ((uint32_t)MC_CG_ARB_FREQ_F1) << 24;
1898*b843c749SSergey Zigachev 
1899*b843c749SSergey Zigachev 	return smu7_write_smc_sram_dword(hwmgr,
1900*b843c749SSergey Zigachev 			smu_data->smu7_data.arb_table_start,  tmp, SMC_RAM_END);
1901*b843c749SSergey Zigachev }
1902*b843c749SSergey Zigachev 
fiji_setup_dpm_led_config(struct pp_hwmgr * hwmgr)1903*b843c749SSergey Zigachev static int fiji_setup_dpm_led_config(struct pp_hwmgr *hwmgr)
1904*b843c749SSergey Zigachev {
1905*b843c749SSergey Zigachev 	pp_atomctrl_voltage_table param_led_dpm;
1906*b843c749SSergey Zigachev 	int result = 0;
1907*b843c749SSergey Zigachev 	u32 mask = 0;
1908*b843c749SSergey Zigachev 
1909*b843c749SSergey Zigachev 	result = atomctrl_get_voltage_table_v3(hwmgr,
1910*b843c749SSergey Zigachev 					       VOLTAGE_TYPE_LEDDPM, VOLTAGE_OBJ_GPIO_LUT,
1911*b843c749SSergey Zigachev 					       &param_led_dpm);
1912*b843c749SSergey Zigachev 	if (result == 0) {
1913*b843c749SSergey Zigachev 		int i, j;
1914*b843c749SSergey Zigachev 		u32 tmp = param_led_dpm.mask_low;
1915*b843c749SSergey Zigachev 
1916*b843c749SSergey Zigachev 		for (i = 0, j = 0; i < 32; i++) {
1917*b843c749SSergey Zigachev 			if (tmp & 1) {
1918*b843c749SSergey Zigachev 				mask |= (i << (8 * j));
1919*b843c749SSergey Zigachev 				if (++j >= 3)
1920*b843c749SSergey Zigachev 					break;
1921*b843c749SSergey Zigachev 			}
1922*b843c749SSergey Zigachev 			tmp >>= 1;
1923*b843c749SSergey Zigachev 		}
1924*b843c749SSergey Zigachev 	}
1925*b843c749SSergey Zigachev 	if (mask)
1926*b843c749SSergey Zigachev 		smum_send_msg_to_smc_with_parameter(hwmgr,
1927*b843c749SSergey Zigachev 						    PPSMC_MSG_LedConfig,
1928*b843c749SSergey Zigachev 						    mask);
1929*b843c749SSergey Zigachev 	return 0;
1930*b843c749SSergey Zigachev }
1931*b843c749SSergey Zigachev 
fiji_init_smc_table(struct pp_hwmgr * hwmgr)1932*b843c749SSergey Zigachev static int fiji_init_smc_table(struct pp_hwmgr *hwmgr)
1933*b843c749SSergey Zigachev {
1934*b843c749SSergey Zigachev 	int result;
1935*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1936*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
1937*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1938*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1939*b843c749SSergey Zigachev 	struct SMU73_Discrete_DpmTable *table = &(smu_data->smc_state_table);
1940*b843c749SSergey Zigachev 	uint8_t i;
1941*b843c749SSergey Zigachev 	struct pp_atomctrl_gpio_pin_assignment gpio_pin;
1942*b843c749SSergey Zigachev 
1943*b843c749SSergey Zigachev 	fiji_initialize_power_tune_defaults(hwmgr);
1944*b843c749SSergey Zigachev 
1945*b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE != data->voltage_control)
1946*b843c749SSergey Zigachev 		fiji_populate_smc_voltage_tables(hwmgr, table);
1947*b843c749SSergey Zigachev 
1948*b843c749SSergey Zigachev 	table->SystemFlags = 0;
1949*b843c749SSergey Zigachev 
1950*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1951*b843c749SSergey Zigachev 			PHM_PlatformCaps_AutomaticDCTransition))
1952*b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
1953*b843c749SSergey Zigachev 
1954*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1955*b843c749SSergey Zigachev 			PHM_PlatformCaps_StepVddc))
1956*b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
1957*b843c749SSergey Zigachev 
1958*b843c749SSergey Zigachev 	if (data->is_memory_gddr5)
1959*b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
1960*b843c749SSergey Zigachev 
1961*b843c749SSergey Zigachev 	if (data->ulv_supported && table_info->us_ulv_voltage_offset) {
1962*b843c749SSergey Zigachev 		result = fiji_populate_ulv_state(hwmgr, table);
1963*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == result,
1964*b843c749SSergey Zigachev 				"Failed to initialize ULV state!", return result);
1965*b843c749SSergey Zigachev 		cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1966*b843c749SSergey Zigachev 				ixCG_ULV_PARAMETER, 0x40035);
1967*b843c749SSergey Zigachev 	}
1968*b843c749SSergey Zigachev 
1969*b843c749SSergey Zigachev 	result = fiji_populate_smc_link_level(hwmgr, table);
1970*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1971*b843c749SSergey Zigachev 			"Failed to initialize Link Level!", return result);
1972*b843c749SSergey Zigachev 
1973*b843c749SSergey Zigachev 	result = fiji_populate_all_graphic_levels(hwmgr);
1974*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1975*b843c749SSergey Zigachev 			"Failed to initialize Graphics Level!", return result);
1976*b843c749SSergey Zigachev 
1977*b843c749SSergey Zigachev 	result = fiji_populate_all_memory_levels(hwmgr);
1978*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1979*b843c749SSergey Zigachev 			"Failed to initialize Memory Level!", return result);
1980*b843c749SSergey Zigachev 
1981*b843c749SSergey Zigachev 	result = fiji_populate_smc_acpi_level(hwmgr, table);
1982*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1983*b843c749SSergey Zigachev 			"Failed to initialize ACPI Level!", return result);
1984*b843c749SSergey Zigachev 
1985*b843c749SSergey Zigachev 	result = fiji_populate_smc_vce_level(hwmgr, table);
1986*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1987*b843c749SSergey Zigachev 			"Failed to initialize VCE Level!", return result);
1988*b843c749SSergey Zigachev 
1989*b843c749SSergey Zigachev 	result = fiji_populate_smc_acp_level(hwmgr, table);
1990*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1991*b843c749SSergey Zigachev 			"Failed to initialize ACP Level!", return result);
1992*b843c749SSergey Zigachev 
1993*b843c749SSergey Zigachev 	/* Since only the initial state is completely set up at this point
1994*b843c749SSergey Zigachev 	 * (the other states are just copies of the boot state) we only
1995*b843c749SSergey Zigachev 	 * need to populate the  ARB settings for the initial state.
1996*b843c749SSergey Zigachev 	 */
1997*b843c749SSergey Zigachev 	result = fiji_program_memory_timing_parameters(hwmgr);
1998*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1999*b843c749SSergey Zigachev 			"Failed to Write ARB settings for the initial state.", return result);
2000*b843c749SSergey Zigachev 
2001*b843c749SSergey Zigachev 	result = fiji_populate_smc_uvd_level(hwmgr, table);
2002*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2003*b843c749SSergey Zigachev 			"Failed to initialize UVD Level!", return result);
2004*b843c749SSergey Zigachev 
2005*b843c749SSergey Zigachev 	result = fiji_populate_smc_boot_level(hwmgr, table);
2006*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2007*b843c749SSergey Zigachev 			"Failed to initialize Boot Level!", return result);
2008*b843c749SSergey Zigachev 
2009*b843c749SSergey Zigachev 	result = fiji_populate_smc_initailial_state(hwmgr);
2010*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2011*b843c749SSergey Zigachev 			"Failed to initialize Boot State!", return result);
2012*b843c749SSergey Zigachev 
2013*b843c749SSergey Zigachev 	result = fiji_populate_bapm_parameters_in_dpm_table(hwmgr);
2014*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2015*b843c749SSergey Zigachev 			"Failed to populate BAPM Parameters!", return result);
2016*b843c749SSergey Zigachev 
2017*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2018*b843c749SSergey Zigachev 			PHM_PlatformCaps_ClockStretcher)) {
2019*b843c749SSergey Zigachev 		result = fiji_populate_clock_stretcher_data_table(hwmgr);
2020*b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == result,
2021*b843c749SSergey Zigachev 				"Failed to populate Clock Stretcher Data Table!",
2022*b843c749SSergey Zigachev 				return result);
2023*b843c749SSergey Zigachev 	}
2024*b843c749SSergey Zigachev 
2025*b843c749SSergey Zigachev 	table->GraphicsVoltageChangeEnable  = 1;
2026*b843c749SSergey Zigachev 	table->GraphicsThermThrottleEnable  = 1;
2027*b843c749SSergey Zigachev 	table->GraphicsInterval = 1;
2028*b843c749SSergey Zigachev 	table->VoltageInterval  = 1;
2029*b843c749SSergey Zigachev 	table->ThermalInterval  = 1;
2030*b843c749SSergey Zigachev 	table->TemperatureLimitHigh =
2031*b843c749SSergey Zigachev 			table_info->cac_dtp_table->usTargetOperatingTemp *
2032*b843c749SSergey Zigachev 			SMU7_Q88_FORMAT_CONVERSION_UNIT;
2033*b843c749SSergey Zigachev 	table->TemperatureLimitLow  =
2034*b843c749SSergey Zigachev 			(table_info->cac_dtp_table->usTargetOperatingTemp - 1) *
2035*b843c749SSergey Zigachev 			SMU7_Q88_FORMAT_CONVERSION_UNIT;
2036*b843c749SSergey Zigachev 	table->MemoryVoltageChangeEnable = 1;
2037*b843c749SSergey Zigachev 	table->MemoryInterval = 1;
2038*b843c749SSergey Zigachev 	table->VoltageResponseTime = 0;
2039*b843c749SSergey Zigachev 	table->PhaseResponseTime = 0;
2040*b843c749SSergey Zigachev 	table->MemoryThermThrottleEnable = 1;
2041*b843c749SSergey Zigachev 	table->PCIeBootLinkLevel = 0;      /* 0:Gen1 1:Gen2 2:Gen3*/
2042*b843c749SSergey Zigachev 	table->PCIeGenInterval = 1;
2043*b843c749SSergey Zigachev 	table->VRConfig = 0;
2044*b843c749SSergey Zigachev 
2045*b843c749SSergey Zigachev 	result = fiji_populate_vr_config(hwmgr, table);
2046*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2047*b843c749SSergey Zigachev 			"Failed to populate VRConfig setting!", return result);
2048*b843c749SSergey Zigachev 	data->vr_config = table->VRConfig;
2049*b843c749SSergey Zigachev 	table->ThermGpio = 17;
2050*b843c749SSergey Zigachev 	table->SclkStepSize = 0x4000;
2051*b843c749SSergey Zigachev 
2052*b843c749SSergey Zigachev 	if (atomctrl_get_pp_assign_pin(hwmgr, VDDC_VRHOT_GPIO_PINID, &gpio_pin)) {
2053*b843c749SSergey Zigachev 		table->VRHotGpio = gpio_pin.uc_gpio_pin_bit_shift;
2054*b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2055*b843c749SSergey Zigachev 				PHM_PlatformCaps_RegulatorHot);
2056*b843c749SSergey Zigachev 	} else {
2057*b843c749SSergey Zigachev 		table->VRHotGpio = SMU7_UNUSED_GPIO_PIN;
2058*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2059*b843c749SSergey Zigachev 				PHM_PlatformCaps_RegulatorHot);
2060*b843c749SSergey Zigachev 	}
2061*b843c749SSergey Zigachev 
2062*b843c749SSergey Zigachev 	if (atomctrl_get_pp_assign_pin(hwmgr, PP_AC_DC_SWITCH_GPIO_PINID,
2063*b843c749SSergey Zigachev 			&gpio_pin)) {
2064*b843c749SSergey Zigachev 		table->AcDcGpio = gpio_pin.uc_gpio_pin_bit_shift;
2065*b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2066*b843c749SSergey Zigachev 				PHM_PlatformCaps_AutomaticDCTransition);
2067*b843c749SSergey Zigachev 	} else {
2068*b843c749SSergey Zigachev 		table->AcDcGpio = SMU7_UNUSED_GPIO_PIN;
2069*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2070*b843c749SSergey Zigachev 				PHM_PlatformCaps_AutomaticDCTransition);
2071*b843c749SSergey Zigachev 	}
2072*b843c749SSergey Zigachev 
2073*b843c749SSergey Zigachev 	/* Thermal Output GPIO */
2074*b843c749SSergey Zigachev 	if (atomctrl_get_pp_assign_pin(hwmgr, THERMAL_INT_OUTPUT_GPIO_PINID,
2075*b843c749SSergey Zigachev 			&gpio_pin)) {
2076*b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2077*b843c749SSergey Zigachev 				PHM_PlatformCaps_ThermalOutGPIO);
2078*b843c749SSergey Zigachev 
2079*b843c749SSergey Zigachev 		table->ThermOutGpio = gpio_pin.uc_gpio_pin_bit_shift;
2080*b843c749SSergey Zigachev 
2081*b843c749SSergey Zigachev 		/* For porlarity read GPIOPAD_A with assigned Gpio pin
2082*b843c749SSergey Zigachev 		 * since VBIOS will program this register to set 'inactive state',
2083*b843c749SSergey Zigachev 		 * driver can then determine 'active state' from this and
2084*b843c749SSergey Zigachev 		 * program SMU with correct polarity
2085*b843c749SSergey Zigachev 		 */
2086*b843c749SSergey Zigachev 		table->ThermOutPolarity = (0 == (cgs_read_register(hwmgr->device, mmGPIOPAD_A) &
2087*b843c749SSergey Zigachev 				(1 << gpio_pin.uc_gpio_pin_bit_shift))) ? 1:0;
2088*b843c749SSergey Zigachev 		table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_ONLY;
2089*b843c749SSergey Zigachev 
2090*b843c749SSergey Zigachev 		/* if required, combine VRHot/PCC with thermal out GPIO */
2091*b843c749SSergey Zigachev 		if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2092*b843c749SSergey Zigachev 				PHM_PlatformCaps_RegulatorHot) &&
2093*b843c749SSergey Zigachev 			phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2094*b843c749SSergey Zigachev 					PHM_PlatformCaps_CombinePCCWithThermalSignal))
2095*b843c749SSergey Zigachev 			table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_VRHOT;
2096*b843c749SSergey Zigachev 	} else {
2097*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2098*b843c749SSergey Zigachev 				PHM_PlatformCaps_ThermalOutGPIO);
2099*b843c749SSergey Zigachev 		table->ThermOutGpio = 17;
2100*b843c749SSergey Zigachev 		table->ThermOutPolarity = 1;
2101*b843c749SSergey Zigachev 		table->ThermOutMode = SMU7_THERM_OUT_MODE_DISABLE;
2102*b843c749SSergey Zigachev 	}
2103*b843c749SSergey Zigachev 
2104*b843c749SSergey Zigachev 	for (i = 0; i < SMU73_MAX_ENTRIES_SMIO; i++)
2105*b843c749SSergey Zigachev 		table->Smio[i] = PP_HOST_TO_SMC_UL(table->Smio[i]);
2106*b843c749SSergey Zigachev 
2107*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SystemFlags);
2108*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->VRConfig);
2109*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask1);
2110*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask2);
2111*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SclkStepSize);
2112*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitHigh);
2113*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitLow);
2114*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->VoltageResponseTime);
2115*b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->PhaseResponseTime);
2116*b843c749SSergey Zigachev 
2117*b843c749SSergey Zigachev 	/* Upload all dpm data to SMC memory.(dpm level, dpm level count etc) */
2118*b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr,
2119*b843c749SSergey Zigachev 			smu_data->smu7_data.dpm_table_start +
2120*b843c749SSergey Zigachev 			offsetof(SMU73_Discrete_DpmTable, SystemFlags),
2121*b843c749SSergey Zigachev 			(uint8_t *)&(table->SystemFlags),
2122*b843c749SSergey Zigachev 			sizeof(SMU73_Discrete_DpmTable) - 3 * sizeof(SMU73_PIDController),
2123*b843c749SSergey Zigachev 			SMC_RAM_END);
2124*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2125*b843c749SSergey Zigachev 			"Failed to upload dpm data to SMC memory!", return result);
2126*b843c749SSergey Zigachev 
2127*b843c749SSergey Zigachev 	result = fiji_init_arb_table_index(hwmgr);
2128*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2129*b843c749SSergey Zigachev 			"Failed to upload arb data to SMC memory!", return result);
2130*b843c749SSergey Zigachev 
2131*b843c749SSergey Zigachev 	result = fiji_populate_pm_fuses(hwmgr);
2132*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2133*b843c749SSergey Zigachev 			"Failed to  populate PM fuses to SMC memory!", return result);
2134*b843c749SSergey Zigachev 
2135*b843c749SSergey Zigachev 	result = fiji_setup_dpm_led_config(hwmgr);
2136*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2137*b843c749SSergey Zigachev 			    "Failed to setup dpm led config", return result);
2138*b843c749SSergey Zigachev 
2139*b843c749SSergey Zigachev 	return 0;
2140*b843c749SSergey Zigachev }
2141*b843c749SSergey Zigachev 
fiji_thermal_setup_fan_table(struct pp_hwmgr * hwmgr)2142*b843c749SSergey Zigachev static int fiji_thermal_setup_fan_table(struct pp_hwmgr *hwmgr)
2143*b843c749SSergey Zigachev {
2144*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
2145*b843c749SSergey Zigachev 
2146*b843c749SSergey Zigachev 	SMU73_Discrete_FanTable fan_table = { FDO_MODE_HARDWARE };
2147*b843c749SSergey Zigachev 	uint32_t duty100;
2148*b843c749SSergey Zigachev 	uint32_t t_diff1, t_diff2, pwm_diff1, pwm_diff2;
2149*b843c749SSergey Zigachev 	uint16_t fdo_min, slope1, slope2;
2150*b843c749SSergey Zigachev 	uint32_t reference_clock;
2151*b843c749SSergey Zigachev 	int res;
2152*b843c749SSergey Zigachev 	uint64_t tmp64;
2153*b843c749SSergey Zigachev 
2154*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan) {
2155*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2156*b843c749SSergey Zigachev 			PHM_PlatformCaps_MicrocodeFanControl);
2157*b843c749SSergey Zigachev 		return 0;
2158*b843c749SSergey Zigachev 	}
2159*b843c749SSergey Zigachev 
2160*b843c749SSergey Zigachev 	if (smu_data->smu7_data.fan_table_start == 0) {
2161*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2162*b843c749SSergey Zigachev 				PHM_PlatformCaps_MicrocodeFanControl);
2163*b843c749SSergey Zigachev 		return 0;
2164*b843c749SSergey Zigachev 	}
2165*b843c749SSergey Zigachev 
2166*b843c749SSergey Zigachev 	duty100 = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
2167*b843c749SSergey Zigachev 			CG_FDO_CTRL1, FMAX_DUTY100);
2168*b843c749SSergey Zigachev 
2169*b843c749SSergey Zigachev 	if (duty100 == 0) {
2170*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2171*b843c749SSergey Zigachev 				PHM_PlatformCaps_MicrocodeFanControl);
2172*b843c749SSergey Zigachev 		return 0;
2173*b843c749SSergey Zigachev 	}
2174*b843c749SSergey Zigachev 
2175*b843c749SSergey Zigachev 	tmp64 = hwmgr->thermal_controller.advanceFanControlParameters.
2176*b843c749SSergey Zigachev 			usPWMMin * duty100;
2177*b843c749SSergey Zigachev 	do_div(tmp64, 10000);
2178*b843c749SSergey Zigachev 	fdo_min = (uint16_t)tmp64;
2179*b843c749SSergey Zigachev 
2180*b843c749SSergey Zigachev 	t_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usTMed -
2181*b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usTMin;
2182*b843c749SSergey Zigachev 	t_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usTHigh -
2183*b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usTMed;
2184*b843c749SSergey Zigachev 
2185*b843c749SSergey Zigachev 	pwm_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed -
2186*b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usPWMMin;
2187*b843c749SSergey Zigachev 	pwm_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMHigh -
2188*b843c749SSergey Zigachev 			hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed;
2189*b843c749SSergey Zigachev 
2190*b843c749SSergey Zigachev 	slope1 = (uint16_t)((50 + ((16 * duty100 * pwm_diff1) / t_diff1)) / 100);
2191*b843c749SSergey Zigachev 	slope2 = (uint16_t)((50 + ((16 * duty100 * pwm_diff2) / t_diff2)) / 100);
2192*b843c749SSergey Zigachev 
2193*b843c749SSergey Zigachev 	fan_table.TempMin = cpu_to_be16((50 + hwmgr->
2194*b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.usTMin) / 100);
2195*b843c749SSergey Zigachev 	fan_table.TempMed = cpu_to_be16((50 + hwmgr->
2196*b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.usTMed) / 100);
2197*b843c749SSergey Zigachev 	fan_table.TempMax = cpu_to_be16((50 + hwmgr->
2198*b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.usTMax) / 100);
2199*b843c749SSergey Zigachev 
2200*b843c749SSergey Zigachev 	fan_table.Slope1 = cpu_to_be16(slope1);
2201*b843c749SSergey Zigachev 	fan_table.Slope2 = cpu_to_be16(slope2);
2202*b843c749SSergey Zigachev 
2203*b843c749SSergey Zigachev 	fan_table.FdoMin = cpu_to_be16(fdo_min);
2204*b843c749SSergey Zigachev 
2205*b843c749SSergey Zigachev 	fan_table.HystDown = cpu_to_be16(hwmgr->
2206*b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.ucTHyst);
2207*b843c749SSergey Zigachev 
2208*b843c749SSergey Zigachev 	fan_table.HystUp = cpu_to_be16(1);
2209*b843c749SSergey Zigachev 
2210*b843c749SSergey Zigachev 	fan_table.HystSlope = cpu_to_be16(1);
2211*b843c749SSergey Zigachev 
2212*b843c749SSergey Zigachev 	fan_table.TempRespLim = cpu_to_be16(5);
2213*b843c749SSergey Zigachev 
2214*b843c749SSergey Zigachev 	reference_clock = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
2215*b843c749SSergey Zigachev 
2216*b843c749SSergey Zigachev 	fan_table.RefreshPeriod = cpu_to_be32((hwmgr->
2217*b843c749SSergey Zigachev 			thermal_controller.advanceFanControlParameters.ulCycleDelay *
2218*b843c749SSergey Zigachev 			reference_clock) / 1600);
2219*b843c749SSergey Zigachev 
2220*b843c749SSergey Zigachev 	fan_table.FdoMax = cpu_to_be16((uint16_t)duty100);
2221*b843c749SSergey Zigachev 
2222*b843c749SSergey Zigachev 	fan_table.TempSrc = (uint8_t)PHM_READ_VFPF_INDIRECT_FIELD(
2223*b843c749SSergey Zigachev 			hwmgr->device, CGS_IND_REG__SMC,
2224*b843c749SSergey Zigachev 			CG_MULT_THERMAL_CTRL, TEMP_SEL);
2225*b843c749SSergey Zigachev 
2226*b843c749SSergey Zigachev 	res = smu7_copy_bytes_to_smc(hwmgr, smu_data->smu7_data.fan_table_start,
2227*b843c749SSergey Zigachev 			(uint8_t *)&fan_table, (uint32_t)sizeof(fan_table),
2228*b843c749SSergey Zigachev 			SMC_RAM_END);
2229*b843c749SSergey Zigachev 
2230*b843c749SSergey Zigachev 	if (!res && hwmgr->thermal_controller.
2231*b843c749SSergey Zigachev 			advanceFanControlParameters.ucMinimumPWMLimit)
2232*b843c749SSergey Zigachev 		res = smum_send_msg_to_smc_with_parameter(hwmgr,
2233*b843c749SSergey Zigachev 				PPSMC_MSG_SetFanMinPwm,
2234*b843c749SSergey Zigachev 				hwmgr->thermal_controller.
2235*b843c749SSergey Zigachev 				advanceFanControlParameters.ucMinimumPWMLimit);
2236*b843c749SSergey Zigachev 
2237*b843c749SSergey Zigachev 	if (!res && hwmgr->thermal_controller.
2238*b843c749SSergey Zigachev 			advanceFanControlParameters.ulMinFanSCLKAcousticLimit)
2239*b843c749SSergey Zigachev 		res = smum_send_msg_to_smc_with_parameter(hwmgr,
2240*b843c749SSergey Zigachev 				PPSMC_MSG_SetFanSclkTarget,
2241*b843c749SSergey Zigachev 				hwmgr->thermal_controller.
2242*b843c749SSergey Zigachev 				advanceFanControlParameters.ulMinFanSCLKAcousticLimit);
2243*b843c749SSergey Zigachev 
2244*b843c749SSergey Zigachev 	if (res)
2245*b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2246*b843c749SSergey Zigachev 				PHM_PlatformCaps_MicrocodeFanControl);
2247*b843c749SSergey Zigachev 
2248*b843c749SSergey Zigachev 	return 0;
2249*b843c749SSergey Zigachev }
2250*b843c749SSergey Zigachev 
2251*b843c749SSergey Zigachev 
fiji_thermal_avfs_enable(struct pp_hwmgr * hwmgr)2252*b843c749SSergey Zigachev static int fiji_thermal_avfs_enable(struct pp_hwmgr *hwmgr)
2253*b843c749SSergey Zigachev {
2254*b843c749SSergey Zigachev 	if (!hwmgr->avfs_supported)
2255*b843c749SSergey Zigachev 		return 0;
2256*b843c749SSergey Zigachev 
2257*b843c749SSergey Zigachev 	smum_send_msg_to_smc(hwmgr, PPSMC_MSG_EnableAvfs);
2258*b843c749SSergey Zigachev 
2259*b843c749SSergey Zigachev 	return 0;
2260*b843c749SSergey Zigachev }
2261*b843c749SSergey Zigachev 
fiji_program_mem_timing_parameters(struct pp_hwmgr * hwmgr)2262*b843c749SSergey Zigachev static int fiji_program_mem_timing_parameters(struct pp_hwmgr *hwmgr)
2263*b843c749SSergey Zigachev {
2264*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2265*b843c749SSergey Zigachev 
2266*b843c749SSergey Zigachev 	if (data->need_update_smu7_dpm_table &
2267*b843c749SSergey Zigachev 		(DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_OD_UPDATE_MCLK))
2268*b843c749SSergey Zigachev 		return fiji_program_memory_timing_parameters(hwmgr);
2269*b843c749SSergey Zigachev 
2270*b843c749SSergey Zigachev 	return 0;
2271*b843c749SSergey Zigachev }
2272*b843c749SSergey Zigachev 
fiji_update_sclk_threshold(struct pp_hwmgr * hwmgr)2273*b843c749SSergey Zigachev static int fiji_update_sclk_threshold(struct pp_hwmgr *hwmgr)
2274*b843c749SSergey Zigachev {
2275*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2276*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
2277*b843c749SSergey Zigachev 
2278*b843c749SSergey Zigachev 	int result = 0;
2279*b843c749SSergey Zigachev 	uint32_t low_sclk_interrupt_threshold = 0;
2280*b843c749SSergey Zigachev 
2281*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2282*b843c749SSergey Zigachev 			PHM_PlatformCaps_SclkThrottleLowNotification)
2283*b843c749SSergey Zigachev 		&& (data->low_sclk_interrupt_threshold != 0)) {
2284*b843c749SSergey Zigachev 		low_sclk_interrupt_threshold =
2285*b843c749SSergey Zigachev 				data->low_sclk_interrupt_threshold;
2286*b843c749SSergey Zigachev 
2287*b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(low_sclk_interrupt_threshold);
2288*b843c749SSergey Zigachev 
2289*b843c749SSergey Zigachev 		result = smu7_copy_bytes_to_smc(
2290*b843c749SSergey Zigachev 				hwmgr,
2291*b843c749SSergey Zigachev 				smu_data->smu7_data.dpm_table_start +
2292*b843c749SSergey Zigachev 				offsetof(SMU73_Discrete_DpmTable,
2293*b843c749SSergey Zigachev 					LowSclkInterruptThreshold),
2294*b843c749SSergey Zigachev 				(uint8_t *)&low_sclk_interrupt_threshold,
2295*b843c749SSergey Zigachev 				sizeof(uint32_t),
2296*b843c749SSergey Zigachev 				SMC_RAM_END);
2297*b843c749SSergey Zigachev 	}
2298*b843c749SSergey Zigachev 	result = fiji_program_mem_timing_parameters(hwmgr);
2299*b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((result == 0),
2300*b843c749SSergey Zigachev 			"Failed to program memory timing parameters!",
2301*b843c749SSergey Zigachev 			);
2302*b843c749SSergey Zigachev 	return result;
2303*b843c749SSergey Zigachev }
2304*b843c749SSergey Zigachev 
fiji_get_offsetof(uint32_t type,uint32_t member)2305*b843c749SSergey Zigachev static uint32_t fiji_get_offsetof(uint32_t type, uint32_t member)
2306*b843c749SSergey Zigachev {
2307*b843c749SSergey Zigachev 	switch (type) {
2308*b843c749SSergey Zigachev 	case SMU_SoftRegisters:
2309*b843c749SSergey Zigachev 		switch (member) {
2310*b843c749SSergey Zigachev 		case HandshakeDisables:
2311*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, HandshakeDisables);
2312*b843c749SSergey Zigachev 		case VoltageChangeTimeout:
2313*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, VoltageChangeTimeout);
2314*b843c749SSergey Zigachev 		case AverageGraphicsActivity:
2315*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, AverageGraphicsActivity);
2316*b843c749SSergey Zigachev 		case PreVBlankGap:
2317*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, PreVBlankGap);
2318*b843c749SSergey Zigachev 		case VBlankTimeout:
2319*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, VBlankTimeout);
2320*b843c749SSergey Zigachev 		case UcodeLoadStatus:
2321*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, UcodeLoadStatus);
2322*b843c749SSergey Zigachev 		case DRAM_LOG_ADDR_H:
2323*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, DRAM_LOG_ADDR_H);
2324*b843c749SSergey Zigachev 		case DRAM_LOG_ADDR_L:
2325*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, DRAM_LOG_ADDR_L);
2326*b843c749SSergey Zigachev 		case DRAM_LOG_PHY_ADDR_H:
2327*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, DRAM_LOG_PHY_ADDR_H);
2328*b843c749SSergey Zigachev 		case DRAM_LOG_PHY_ADDR_L:
2329*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, DRAM_LOG_PHY_ADDR_L);
2330*b843c749SSergey Zigachev 		case DRAM_LOG_BUFF_SIZE:
2331*b843c749SSergey Zigachev 			return offsetof(SMU73_SoftRegisters, DRAM_LOG_BUFF_SIZE);
2332*b843c749SSergey Zigachev 		}
2333*b843c749SSergey Zigachev 		break;
2334*b843c749SSergey Zigachev 	case SMU_Discrete_DpmTable:
2335*b843c749SSergey Zigachev 		switch (member) {
2336*b843c749SSergey Zigachev 		case UvdBootLevel:
2337*b843c749SSergey Zigachev 			return offsetof(SMU73_Discrete_DpmTable, UvdBootLevel);
2338*b843c749SSergey Zigachev 		case VceBootLevel:
2339*b843c749SSergey Zigachev 			return offsetof(SMU73_Discrete_DpmTable, VceBootLevel);
2340*b843c749SSergey Zigachev 		case LowSclkInterruptThreshold:
2341*b843c749SSergey Zigachev 			return offsetof(SMU73_Discrete_DpmTable, LowSclkInterruptThreshold);
2342*b843c749SSergey Zigachev 		}
2343*b843c749SSergey Zigachev 		break;
2344*b843c749SSergey Zigachev 	}
2345*b843c749SSergey Zigachev 	pr_warn("can't get the offset of type %x member %x\n", type, member);
2346*b843c749SSergey Zigachev 	return 0;
2347*b843c749SSergey Zigachev }
2348*b843c749SSergey Zigachev 
fiji_get_mac_definition(uint32_t value)2349*b843c749SSergey Zigachev static uint32_t fiji_get_mac_definition(uint32_t value)
2350*b843c749SSergey Zigachev {
2351*b843c749SSergey Zigachev 	switch (value) {
2352*b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_GRAPHICS:
2353*b843c749SSergey Zigachev 		return SMU73_MAX_LEVELS_GRAPHICS;
2354*b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_MEMORY:
2355*b843c749SSergey Zigachev 		return SMU73_MAX_LEVELS_MEMORY;
2356*b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_LINK:
2357*b843c749SSergey Zigachev 		return SMU73_MAX_LEVELS_LINK;
2358*b843c749SSergey Zigachev 	case SMU_MAX_ENTRIES_SMIO:
2359*b843c749SSergey Zigachev 		return SMU73_MAX_ENTRIES_SMIO;
2360*b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDC:
2361*b843c749SSergey Zigachev 		return SMU73_MAX_LEVELS_VDDC;
2362*b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDGFX:
2363*b843c749SSergey Zigachev 		return SMU73_MAX_LEVELS_VDDGFX;
2364*b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDCI:
2365*b843c749SSergey Zigachev 		return SMU73_MAX_LEVELS_VDDCI;
2366*b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_MVDD:
2367*b843c749SSergey Zigachev 		return SMU73_MAX_LEVELS_MVDD;
2368*b843c749SSergey Zigachev 	}
2369*b843c749SSergey Zigachev 
2370*b843c749SSergey Zigachev 	pr_warn("can't get the mac of %x\n", value);
2371*b843c749SSergey Zigachev 	return 0;
2372*b843c749SSergey Zigachev }
2373*b843c749SSergey Zigachev 
2374*b843c749SSergey Zigachev 
fiji_update_uvd_smc_table(struct pp_hwmgr * hwmgr)2375*b843c749SSergey Zigachev static int fiji_update_uvd_smc_table(struct pp_hwmgr *hwmgr)
2376*b843c749SSergey Zigachev {
2377*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
2378*b843c749SSergey Zigachev 	uint32_t mm_boot_level_offset, mm_boot_level_value;
2379*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2380*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2381*b843c749SSergey Zigachev 
2382*b843c749SSergey Zigachev 	smu_data->smc_state_table.UvdBootLevel = 0;
2383*b843c749SSergey Zigachev 	if (table_info->mm_dep_table->count > 0)
2384*b843c749SSergey Zigachev 		smu_data->smc_state_table.UvdBootLevel =
2385*b843c749SSergey Zigachev 				(uint8_t) (table_info->mm_dep_table->count - 1);
2386*b843c749SSergey Zigachev 	mm_boot_level_offset = smu_data->smu7_data.dpm_table_start + offsetof(SMU73_Discrete_DpmTable,
2387*b843c749SSergey Zigachev 						UvdBootLevel);
2388*b843c749SSergey Zigachev 	mm_boot_level_offset /= 4;
2389*b843c749SSergey Zigachev 	mm_boot_level_offset *= 4;
2390*b843c749SSergey Zigachev 	mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
2391*b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset);
2392*b843c749SSergey Zigachev 	mm_boot_level_value &= 0x00FFFFFF;
2393*b843c749SSergey Zigachev 	mm_boot_level_value |= smu_data->smc_state_table.UvdBootLevel << 24;
2394*b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device,
2395*b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
2396*b843c749SSergey Zigachev 
2397*b843c749SSergey Zigachev 	if (!phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2398*b843c749SSergey Zigachev 			PHM_PlatformCaps_UVDDPM) ||
2399*b843c749SSergey Zigachev 		phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2400*b843c749SSergey Zigachev 			PHM_PlatformCaps_StablePState))
2401*b843c749SSergey Zigachev 		smum_send_msg_to_smc_with_parameter(hwmgr,
2402*b843c749SSergey Zigachev 				PPSMC_MSG_UVDDPM_SetEnabledMask,
2403*b843c749SSergey Zigachev 				(uint32_t)(1 << smu_data->smc_state_table.UvdBootLevel));
2404*b843c749SSergey Zigachev 	return 0;
2405*b843c749SSergey Zigachev }
2406*b843c749SSergey Zigachev 
fiji_update_vce_smc_table(struct pp_hwmgr * hwmgr)2407*b843c749SSergey Zigachev static int fiji_update_vce_smc_table(struct pp_hwmgr *hwmgr)
2408*b843c749SSergey Zigachev {
2409*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
2410*b843c749SSergey Zigachev 	uint32_t mm_boot_level_offset, mm_boot_level_value;
2411*b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2412*b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2413*b843c749SSergey Zigachev 
2414*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2415*b843c749SSergey Zigachev 					PHM_PlatformCaps_StablePState))
2416*b843c749SSergey Zigachev 		smu_data->smc_state_table.VceBootLevel =
2417*b843c749SSergey Zigachev 			(uint8_t) (table_info->mm_dep_table->count - 1);
2418*b843c749SSergey Zigachev 	else
2419*b843c749SSergey Zigachev 		smu_data->smc_state_table.VceBootLevel = 0;
2420*b843c749SSergey Zigachev 
2421*b843c749SSergey Zigachev 	mm_boot_level_offset = smu_data->smu7_data.dpm_table_start +
2422*b843c749SSergey Zigachev 					offsetof(SMU73_Discrete_DpmTable, VceBootLevel);
2423*b843c749SSergey Zigachev 	mm_boot_level_offset /= 4;
2424*b843c749SSergey Zigachev 	mm_boot_level_offset *= 4;
2425*b843c749SSergey Zigachev 	mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
2426*b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset);
2427*b843c749SSergey Zigachev 	mm_boot_level_value &= 0xFF00FFFF;
2428*b843c749SSergey Zigachev 	mm_boot_level_value |= smu_data->smc_state_table.VceBootLevel << 16;
2429*b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device,
2430*b843c749SSergey Zigachev 			CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
2431*b843c749SSergey Zigachev 
2432*b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_StablePState))
2433*b843c749SSergey Zigachev 		smum_send_msg_to_smc_with_parameter(hwmgr,
2434*b843c749SSergey Zigachev 				PPSMC_MSG_VCEDPM_SetEnabledMask,
2435*b843c749SSergey Zigachev 				(uint32_t)1 << smu_data->smc_state_table.VceBootLevel);
2436*b843c749SSergey Zigachev 	return 0;
2437*b843c749SSergey Zigachev }
2438*b843c749SSergey Zigachev 
fiji_update_smc_table(struct pp_hwmgr * hwmgr,uint32_t type)2439*b843c749SSergey Zigachev static int fiji_update_smc_table(struct pp_hwmgr *hwmgr, uint32_t type)
2440*b843c749SSergey Zigachev {
2441*b843c749SSergey Zigachev 	switch (type) {
2442*b843c749SSergey Zigachev 	case SMU_UVD_TABLE:
2443*b843c749SSergey Zigachev 		fiji_update_uvd_smc_table(hwmgr);
2444*b843c749SSergey Zigachev 		break;
2445*b843c749SSergey Zigachev 	case SMU_VCE_TABLE:
2446*b843c749SSergey Zigachev 		fiji_update_vce_smc_table(hwmgr);
2447*b843c749SSergey Zigachev 		break;
2448*b843c749SSergey Zigachev 	default:
2449*b843c749SSergey Zigachev 		break;
2450*b843c749SSergey Zigachev 	}
2451*b843c749SSergey Zigachev 	return 0;
2452*b843c749SSergey Zigachev }
2453*b843c749SSergey Zigachev 
fiji_process_firmware_header(struct pp_hwmgr * hwmgr)2454*b843c749SSergey Zigachev static int fiji_process_firmware_header(struct pp_hwmgr *hwmgr)
2455*b843c749SSergey Zigachev {
2456*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2457*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smu_backend);
2458*b843c749SSergey Zigachev 	uint32_t tmp;
2459*b843c749SSergey Zigachev 	int result;
2460*b843c749SSergey Zigachev 	bool error = false;
2461*b843c749SSergey Zigachev 
2462*b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2463*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2464*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, DpmTable),
2465*b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2466*b843c749SSergey Zigachev 
2467*b843c749SSergey Zigachev 	if (0 == result)
2468*b843c749SSergey Zigachev 		smu_data->smu7_data.dpm_table_start = tmp;
2469*b843c749SSergey Zigachev 
2470*b843c749SSergey Zigachev 	error |= (0 != result);
2471*b843c749SSergey Zigachev 
2472*b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2473*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2474*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, SoftRegisters),
2475*b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2476*b843c749SSergey Zigachev 
2477*b843c749SSergey Zigachev 	if (!result) {
2478*b843c749SSergey Zigachev 		data->soft_regs_start = tmp;
2479*b843c749SSergey Zigachev 		smu_data->smu7_data.soft_regs_start = tmp;
2480*b843c749SSergey Zigachev 	}
2481*b843c749SSergey Zigachev 
2482*b843c749SSergey Zigachev 	error |= (0 != result);
2483*b843c749SSergey Zigachev 
2484*b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2485*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2486*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, mcRegisterTable),
2487*b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2488*b843c749SSergey Zigachev 
2489*b843c749SSergey Zigachev 	if (!result)
2490*b843c749SSergey Zigachev 		smu_data->smu7_data.mc_reg_table_start = tmp;
2491*b843c749SSergey Zigachev 
2492*b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2493*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2494*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, FanTable),
2495*b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2496*b843c749SSergey Zigachev 
2497*b843c749SSergey Zigachev 	if (!result)
2498*b843c749SSergey Zigachev 		smu_data->smu7_data.fan_table_start = tmp;
2499*b843c749SSergey Zigachev 
2500*b843c749SSergey Zigachev 	error |= (0 != result);
2501*b843c749SSergey Zigachev 
2502*b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2503*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2504*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, mcArbDramTimingTable),
2505*b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2506*b843c749SSergey Zigachev 
2507*b843c749SSergey Zigachev 	if (!result)
2508*b843c749SSergey Zigachev 		smu_data->smu7_data.arb_table_start = tmp;
2509*b843c749SSergey Zigachev 
2510*b843c749SSergey Zigachev 	error |= (0 != result);
2511*b843c749SSergey Zigachev 
2512*b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2513*b843c749SSergey Zigachev 			SMU7_FIRMWARE_HEADER_LOCATION +
2514*b843c749SSergey Zigachev 			offsetof(SMU73_Firmware_Header, Version),
2515*b843c749SSergey Zigachev 			&tmp, SMC_RAM_END);
2516*b843c749SSergey Zigachev 
2517*b843c749SSergey Zigachev 	if (!result)
2518*b843c749SSergey Zigachev 		hwmgr->microcode_version_info.SMC = tmp;
2519*b843c749SSergey Zigachev 
2520*b843c749SSergey Zigachev 	error |= (0 != result);
2521*b843c749SSergey Zigachev 
2522*b843c749SSergey Zigachev 	return error ? -1 : 0;
2523*b843c749SSergey Zigachev }
2524*b843c749SSergey Zigachev 
fiji_initialize_mc_reg_table(struct pp_hwmgr * hwmgr)2525*b843c749SSergey Zigachev static int fiji_initialize_mc_reg_table(struct pp_hwmgr *hwmgr)
2526*b843c749SSergey Zigachev {
2527*b843c749SSergey Zigachev 
2528*b843c749SSergey Zigachev 	/* Program additional LP registers
2529*b843c749SSergey Zigachev 	 * that are no longer programmed by VBIOS
2530*b843c749SSergey Zigachev 	 */
2531*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_RAS_TIMING_LP,
2532*b843c749SSergey Zigachev 			cgs_read_register(hwmgr->device, mmMC_SEQ_RAS_TIMING));
2533*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_CAS_TIMING_LP,
2534*b843c749SSergey Zigachev 			cgs_read_register(hwmgr->device, mmMC_SEQ_CAS_TIMING));
2535*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2_LP,
2536*b843c749SSergey Zigachev 			cgs_read_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2));
2537*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1_LP,
2538*b843c749SSergey Zigachev 			cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1));
2539*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0_LP,
2540*b843c749SSergey Zigachev 			cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0));
2541*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1_LP,
2542*b843c749SSergey Zigachev 			cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1));
2543*b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_TIMING_LP,
2544*b843c749SSergey Zigachev 			cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_TIMING));
2545*b843c749SSergey Zigachev 
2546*b843c749SSergey Zigachev 	return 0;
2547*b843c749SSergey Zigachev }
2548*b843c749SSergey Zigachev 
fiji_is_dpm_running(struct pp_hwmgr * hwmgr)2549*b843c749SSergey Zigachev static bool fiji_is_dpm_running(struct pp_hwmgr *hwmgr)
2550*b843c749SSergey Zigachev {
2551*b843c749SSergey Zigachev 	return (1 == PHM_READ_INDIRECT_FIELD(hwmgr->device,
2552*b843c749SSergey Zigachev 			CGS_IND_REG__SMC, FEATURE_STATUS, VOLTAGE_CONTROLLER_ON))
2553*b843c749SSergey Zigachev 			? true : false;
2554*b843c749SSergey Zigachev }
2555*b843c749SSergey Zigachev 
fiji_update_dpm_settings(struct pp_hwmgr * hwmgr,void * profile_setting)2556*b843c749SSergey Zigachev static int fiji_update_dpm_settings(struct pp_hwmgr *hwmgr,
2557*b843c749SSergey Zigachev 				void *profile_setting)
2558*b843c749SSergey Zigachev {
2559*b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2560*b843c749SSergey Zigachev 	struct fiji_smumgr *smu_data = (struct fiji_smumgr *)
2561*b843c749SSergey Zigachev 			(hwmgr->smu_backend);
2562*b843c749SSergey Zigachev 	struct profile_mode_setting *setting;
2563*b843c749SSergey Zigachev 	struct SMU73_Discrete_GraphicsLevel *levels =
2564*b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsLevel;
2565*b843c749SSergey Zigachev 	uint32_t array = smu_data->smu7_data.dpm_table_start +
2566*b843c749SSergey Zigachev 			offsetof(SMU73_Discrete_DpmTable, GraphicsLevel);
2567*b843c749SSergey Zigachev 
2568*b843c749SSergey Zigachev 	uint32_t mclk_array = smu_data->smu7_data.dpm_table_start +
2569*b843c749SSergey Zigachev 			offsetof(SMU73_Discrete_DpmTable, MemoryLevel);
2570*b843c749SSergey Zigachev 	struct SMU73_Discrete_MemoryLevel *mclk_levels =
2571*b843c749SSergey Zigachev 			smu_data->smc_state_table.MemoryLevel;
2572*b843c749SSergey Zigachev 	uint32_t i;
2573*b843c749SSergey Zigachev 	uint32_t offset, up_hyst_offset, down_hyst_offset, clk_activity_offset, tmp;
2574*b843c749SSergey Zigachev 
2575*b843c749SSergey Zigachev 	if (profile_setting == NULL)
2576*b843c749SSergey Zigachev 		return -EINVAL;
2577*b843c749SSergey Zigachev 
2578*b843c749SSergey Zigachev 	setting = (struct profile_mode_setting *)profile_setting;
2579*b843c749SSergey Zigachev 
2580*b843c749SSergey Zigachev 	if (setting->bupdate_sclk) {
2581*b843c749SSergey Zigachev 		if (!data->sclk_dpm_key_disabled)
2582*b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_SCLKDPM_FreezeLevel);
2583*b843c749SSergey Zigachev 		for (i = 0; i < smu_data->smc_state_table.GraphicsDpmLevelCount; i++) {
2584*b843c749SSergey Zigachev 			if (levels[i].ActivityLevel !=
2585*b843c749SSergey Zigachev 				cpu_to_be16(setting->sclk_activity)) {
2586*b843c749SSergey Zigachev 				levels[i].ActivityLevel = cpu_to_be16(setting->sclk_activity);
2587*b843c749SSergey Zigachev 
2588*b843c749SSergey Zigachev 				clk_activity_offset = array + (sizeof(SMU73_Discrete_GraphicsLevel) * i)
2589*b843c749SSergey Zigachev 						+ offsetof(SMU73_Discrete_GraphicsLevel, ActivityLevel);
2590*b843c749SSergey Zigachev 				offset = clk_activity_offset & ~0x3;
2591*b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2592*b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(clk_activity_offset, tmp, levels[i].ActivityLevel, sizeof(uint16_t));
2593*b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2594*b843c749SSergey Zigachev 
2595*b843c749SSergey Zigachev 			}
2596*b843c749SSergey Zigachev 			if (levels[i].UpHyst != setting->sclk_up_hyst ||
2597*b843c749SSergey Zigachev 				levels[i].DownHyst != setting->sclk_down_hyst) {
2598*b843c749SSergey Zigachev 				levels[i].UpHyst = setting->sclk_up_hyst;
2599*b843c749SSergey Zigachev 				levels[i].DownHyst = setting->sclk_down_hyst;
2600*b843c749SSergey Zigachev 				up_hyst_offset = array + (sizeof(SMU73_Discrete_GraphicsLevel) * i)
2601*b843c749SSergey Zigachev 						+ offsetof(SMU73_Discrete_GraphicsLevel, UpHyst);
2602*b843c749SSergey Zigachev 				down_hyst_offset = array + (sizeof(SMU73_Discrete_GraphicsLevel) * i)
2603*b843c749SSergey Zigachev 						+ offsetof(SMU73_Discrete_GraphicsLevel, DownHyst);
2604*b843c749SSergey Zigachev 				offset = up_hyst_offset & ~0x3;
2605*b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2606*b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(up_hyst_offset, tmp, levels[i].UpHyst, sizeof(uint8_t));
2607*b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(down_hyst_offset, tmp, levels[i].DownHyst, sizeof(uint8_t));
2608*b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2609*b843c749SSergey Zigachev 			}
2610*b843c749SSergey Zigachev 		}
2611*b843c749SSergey Zigachev 		if (!data->sclk_dpm_key_disabled)
2612*b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_SCLKDPM_UnfreezeLevel);
2613*b843c749SSergey Zigachev 	}
2614*b843c749SSergey Zigachev 
2615*b843c749SSergey Zigachev 	if (setting->bupdate_mclk) {
2616*b843c749SSergey Zigachev 		if (!data->mclk_dpm_key_disabled)
2617*b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_MCLKDPM_FreezeLevel);
2618*b843c749SSergey Zigachev 		for (i = 0; i < smu_data->smc_state_table.MemoryDpmLevelCount; i++) {
2619*b843c749SSergey Zigachev 			if (mclk_levels[i].ActivityLevel !=
2620*b843c749SSergey Zigachev 				cpu_to_be16(setting->mclk_activity)) {
2621*b843c749SSergey Zigachev 				mclk_levels[i].ActivityLevel = cpu_to_be16(setting->mclk_activity);
2622*b843c749SSergey Zigachev 
2623*b843c749SSergey Zigachev 				clk_activity_offset = mclk_array + (sizeof(SMU73_Discrete_MemoryLevel) * i)
2624*b843c749SSergey Zigachev 						+ offsetof(SMU73_Discrete_MemoryLevel, ActivityLevel);
2625*b843c749SSergey Zigachev 				offset = clk_activity_offset & ~0x3;
2626*b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2627*b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(clk_activity_offset, tmp, mclk_levels[i].ActivityLevel, sizeof(uint16_t));
2628*b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2629*b843c749SSergey Zigachev 
2630*b843c749SSergey Zigachev 			}
2631*b843c749SSergey Zigachev 			if (mclk_levels[i].UpHyst != setting->mclk_up_hyst ||
2632*b843c749SSergey Zigachev 				mclk_levels[i].DownHyst != setting->mclk_down_hyst) {
2633*b843c749SSergey Zigachev 				mclk_levels[i].UpHyst = setting->mclk_up_hyst;
2634*b843c749SSergey Zigachev 				mclk_levels[i].DownHyst = setting->mclk_down_hyst;
2635*b843c749SSergey Zigachev 				up_hyst_offset = mclk_array + (sizeof(SMU73_Discrete_MemoryLevel) * i)
2636*b843c749SSergey Zigachev 						+ offsetof(SMU73_Discrete_MemoryLevel, UpHyst);
2637*b843c749SSergey Zigachev 				down_hyst_offset = mclk_array + (sizeof(SMU73_Discrete_MemoryLevel) * i)
2638*b843c749SSergey Zigachev 						+ offsetof(SMU73_Discrete_MemoryLevel, DownHyst);
2639*b843c749SSergey Zigachev 				offset = up_hyst_offset & ~0x3;
2640*b843c749SSergey Zigachev 				tmp = PP_HOST_TO_SMC_UL(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset));
2641*b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(up_hyst_offset, tmp, mclk_levels[i].UpHyst, sizeof(uint8_t));
2642*b843c749SSergey Zigachev 				tmp = phm_set_field_to_u32(down_hyst_offset, tmp, mclk_levels[i].DownHyst, sizeof(uint8_t));
2643*b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset, PP_HOST_TO_SMC_UL(tmp));
2644*b843c749SSergey Zigachev 			}
2645*b843c749SSergey Zigachev 		}
2646*b843c749SSergey Zigachev 		if (!data->mclk_dpm_key_disabled)
2647*b843c749SSergey Zigachev 			smum_send_msg_to_smc(hwmgr, PPSMC_MSG_MCLKDPM_UnfreezeLevel);
2648*b843c749SSergey Zigachev 	}
2649*b843c749SSergey Zigachev 	return 0;
2650*b843c749SSergey Zigachev }
2651*b843c749SSergey Zigachev 
2652*b843c749SSergey Zigachev const struct pp_smumgr_func fiji_smu_funcs = {
2653*b843c749SSergey Zigachev 	.smu_init = &fiji_smu_init,
2654*b843c749SSergey Zigachev 	.smu_fini = &smu7_smu_fini,
2655*b843c749SSergey Zigachev 	.start_smu = &fiji_start_smu,
2656*b843c749SSergey Zigachev 	.check_fw_load_finish = &smu7_check_fw_load_finish,
2657*b843c749SSergey Zigachev 	.request_smu_load_fw = &smu7_reload_firmware,
2658*b843c749SSergey Zigachev 	.request_smu_load_specific_fw = NULL,
2659*b843c749SSergey Zigachev 	.send_msg_to_smc = &smu7_send_msg_to_smc,
2660*b843c749SSergey Zigachev 	.send_msg_to_smc_with_parameter = &smu7_send_msg_to_smc_with_parameter,
2661*b843c749SSergey Zigachev 	.download_pptable_settings = NULL,
2662*b843c749SSergey Zigachev 	.upload_pptable_settings = NULL,
2663*b843c749SSergey Zigachev 	.update_smc_table = fiji_update_smc_table,
2664*b843c749SSergey Zigachev 	.get_offsetof = fiji_get_offsetof,
2665*b843c749SSergey Zigachev 	.process_firmware_header = fiji_process_firmware_header,
2666*b843c749SSergey Zigachev 	.init_smc_table = fiji_init_smc_table,
2667*b843c749SSergey Zigachev 	.update_sclk_threshold = fiji_update_sclk_threshold,
2668*b843c749SSergey Zigachev 	.thermal_setup_fan_table = fiji_thermal_setup_fan_table,
2669*b843c749SSergey Zigachev 	.thermal_avfs_enable = fiji_thermal_avfs_enable,
2670*b843c749SSergey Zigachev 	.populate_all_graphic_levels = fiji_populate_all_graphic_levels,
2671*b843c749SSergey Zigachev 	.populate_all_memory_levels = fiji_populate_all_memory_levels,
2672*b843c749SSergey Zigachev 	.get_mac_definition = fiji_get_mac_definition,
2673*b843c749SSergey Zigachev 	.initialize_mc_reg_table = fiji_initialize_mc_reg_table,
2674*b843c749SSergey Zigachev 	.is_dpm_running = fiji_is_dpm_running,
2675*b843c749SSergey Zigachev 	.is_hw_avfs_present = fiji_is_hw_avfs_present,
2676*b843c749SSergey Zigachev 	.update_dpm_settings = fiji_update_dpm_settings,
2677*b843c749SSergey Zigachev };
2678