1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2015 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev #include "pp_debug.h"
24b843c749SSergey Zigachev #include <linux/delay.h>
25b843c749SSergey Zigachev #include <linux/fb.h>
26b843c749SSergey Zigachev #include <linux/module.h>
27b843c749SSergey Zigachev #include <linux/slab.h>
28b843c749SSergey Zigachev #include <asm/div64.h>
29b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
30b843c749SSergey Zigachev #include "ppatomctrl.h"
31b843c749SSergey Zigachev #include "atombios.h"
32b843c749SSergey Zigachev #include "pptable_v1_0.h"
33b843c749SSergey Zigachev #include "pppcielanes.h"
34b843c749SSergey Zigachev #include "amd_pcie_helpers.h"
35b843c749SSergey Zigachev #include "hardwaremanager.h"
36b843c749SSergey Zigachev #include "process_pptables_v1_0.h"
37b843c749SSergey Zigachev #include "cgs_common.h"
38b843c749SSergey Zigachev 
39b843c749SSergey Zigachev #include "smu7_common.h"
40b843c749SSergey Zigachev 
41b843c749SSergey Zigachev #include "hwmgr.h"
42b843c749SSergey Zigachev #include "smu7_hwmgr.h"
43b843c749SSergey Zigachev #include "smu_ucode_xfer_vi.h"
44b843c749SSergey Zigachev #include "smu7_powertune.h"
45b843c749SSergey Zigachev #include "smu7_dyn_defaults.h"
46b843c749SSergey Zigachev #include "smu7_thermal.h"
47b843c749SSergey Zigachev #include "smu7_clockpowergating.h"
48b843c749SSergey Zigachev #include "processpptables.h"
49b843c749SSergey Zigachev #include "pp_thermal.h"
50b843c749SSergey Zigachev 
51b843c749SSergey Zigachev #include "ivsrcid/ivsrcid_vislands30.h"
52b843c749SSergey Zigachev 
53b843c749SSergey Zigachev #define MC_CG_ARB_FREQ_F0           0x0a
54b843c749SSergey Zigachev #define MC_CG_ARB_FREQ_F1           0x0b
55b843c749SSergey Zigachev #define MC_CG_ARB_FREQ_F2           0x0c
56b843c749SSergey Zigachev #define MC_CG_ARB_FREQ_F3           0x0d
57b843c749SSergey Zigachev 
58b843c749SSergey Zigachev #define MC_CG_SEQ_DRAMCONF_S0       0x05
59b843c749SSergey Zigachev #define MC_CG_SEQ_DRAMCONF_S1       0x06
60b843c749SSergey Zigachev #define MC_CG_SEQ_YCLK_SUSPEND      0x04
61b843c749SSergey Zigachev #define MC_CG_SEQ_YCLK_RESUME       0x0a
62b843c749SSergey Zigachev 
63b843c749SSergey Zigachev #define SMC_CG_IND_START            0xc0030000
64b843c749SSergey Zigachev #define SMC_CG_IND_END              0xc0040000
65b843c749SSergey Zigachev 
66b843c749SSergey Zigachev #define MEM_FREQ_LOW_LATENCY        25000
67b843c749SSergey Zigachev #define MEM_FREQ_HIGH_LATENCY       80000
68b843c749SSergey Zigachev 
69b843c749SSergey Zigachev #define MEM_LATENCY_HIGH            45
70b843c749SSergey Zigachev #define MEM_LATENCY_LOW             35
71b843c749SSergey Zigachev #define MEM_LATENCY_ERR             0xFFFF
72b843c749SSergey Zigachev 
73b843c749SSergey Zigachev #define MC_SEQ_MISC0_GDDR5_SHIFT 28
74b843c749SSergey Zigachev #define MC_SEQ_MISC0_GDDR5_MASK  0xf0000000
75b843c749SSergey Zigachev #define MC_SEQ_MISC0_GDDR5_VALUE 5
76b843c749SSergey Zigachev 
77b843c749SSergey Zigachev #define PCIE_BUS_CLK                10000
78b843c749SSergey Zigachev #define TCLK                        (PCIE_BUS_CLK / 10)
79b843c749SSergey Zigachev 
80b843c749SSergey Zigachev static const struct profile_mode_setting smu7_profiling[6] =
81b843c749SSergey Zigachev 					{{1, 0, 100, 30, 1, 0, 100, 10},
82b843c749SSergey Zigachev 					 {1, 10, 0, 30, 0, 0, 0, 0},
83b843c749SSergey Zigachev 					 {0, 0, 0, 0, 1, 10, 16, 31},
84b843c749SSergey Zigachev 					 {1, 0, 11, 50, 1, 0, 100, 10},
85b843c749SSergey Zigachev 					 {1, 0, 5, 30, 0, 0, 0, 0},
86b843c749SSergey Zigachev 					 {0, 0, 0, 0, 0, 0, 0, 0},
87b843c749SSergey Zigachev 					};
88b843c749SSergey Zigachev 
89b843c749SSergey Zigachev #define PPSMC_MSG_SetVBITimeout_VEGAM    ((uint16_t) 0x310)
90b843c749SSergey Zigachev 
91b843c749SSergey Zigachev #define ixPWR_SVI2_PLANE1_LOAD                     0xC0200280
92b843c749SSergey Zigachev #define PWR_SVI2_PLANE1_LOAD__PSI1_MASK                    0x00000020L
93b843c749SSergey Zigachev #define PWR_SVI2_PLANE1_LOAD__PSI0_EN_MASK                 0x00000040L
94b843c749SSergey Zigachev #define PWR_SVI2_PLANE1_LOAD__PSI1__SHIFT                  0x00000005
95b843c749SSergey Zigachev #define PWR_SVI2_PLANE1_LOAD__PSI0_EN__SHIFT               0x00000006
96b843c749SSergey Zigachev 
97b843c749SSergey Zigachev /** Values for the CG_THERMAL_CTRL::DPM_EVENT_SRC field. */
98b843c749SSergey Zigachev enum DPM_EVENT_SRC {
99b843c749SSergey Zigachev 	DPM_EVENT_SRC_ANALOG = 0,
100b843c749SSergey Zigachev 	DPM_EVENT_SRC_EXTERNAL = 1,
101b843c749SSergey Zigachev 	DPM_EVENT_SRC_DIGITAL = 2,
102b843c749SSergey Zigachev 	DPM_EVENT_SRC_ANALOG_OR_EXTERNAL = 3,
103b843c749SSergey Zigachev 	DPM_EVENT_SRC_DIGITAL_OR_EXTERNAL = 4
104b843c749SSergey Zigachev };
105b843c749SSergey Zigachev 
106b843c749SSergey Zigachev static const unsigned long PhwVIslands_Magic = (unsigned long)(PHM_VIslands_Magic);
107b843c749SSergey Zigachev static int smu7_force_clock_level(struct pp_hwmgr *hwmgr,
108b843c749SSergey Zigachev 		enum pp_clock_type type, uint32_t mask);
109b843c749SSergey Zigachev 
cast_phw_smu7_power_state(struct pp_hw_power_state * hw_ps)110b843c749SSergey Zigachev static struct smu7_power_state *cast_phw_smu7_power_state(
111b843c749SSergey Zigachev 				  struct pp_hw_power_state *hw_ps)
112b843c749SSergey Zigachev {
113b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((PhwVIslands_Magic == hw_ps->magic),
114b843c749SSergey Zigachev 				"Invalid Powerstate Type!",
115b843c749SSergey Zigachev 				 return NULL);
116b843c749SSergey Zigachev 
117b843c749SSergey Zigachev 	return (struct smu7_power_state *)hw_ps;
118b843c749SSergey Zigachev }
119b843c749SSergey Zigachev 
cast_const_phw_smu7_power_state(const struct pp_hw_power_state * hw_ps)120b843c749SSergey Zigachev static const struct smu7_power_state *cast_const_phw_smu7_power_state(
121b843c749SSergey Zigachev 				 const struct pp_hw_power_state *hw_ps)
122b843c749SSergey Zigachev {
123b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((PhwVIslands_Magic == hw_ps->magic),
124b843c749SSergey Zigachev 				"Invalid Powerstate Type!",
125b843c749SSergey Zigachev 				 return NULL);
126b843c749SSergey Zigachev 
127b843c749SSergey Zigachev 	return (const struct smu7_power_state *)hw_ps;
128b843c749SSergey Zigachev }
129b843c749SSergey Zigachev 
130b843c749SSergey Zigachev /**
131b843c749SSergey Zigachev  * Find the MC microcode version and store it in the HwMgr struct
132b843c749SSergey Zigachev  *
133b843c749SSergey Zigachev  * @param    hwmgr  the address of the powerplay hardware manager.
134b843c749SSergey Zigachev  * @return   always 0
135b843c749SSergey Zigachev  */
smu7_get_mc_microcode_version(struct pp_hwmgr * hwmgr)136b843c749SSergey Zigachev static int smu7_get_mc_microcode_version(struct pp_hwmgr *hwmgr)
137b843c749SSergey Zigachev {
138b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_IO_DEBUG_INDEX, 0x9F);
139b843c749SSergey Zigachev 
140b843c749SSergey Zigachev 	hwmgr->microcode_version_info.MC = cgs_read_register(hwmgr->device, mmMC_SEQ_IO_DEBUG_DATA);
141b843c749SSergey Zigachev 
142b843c749SSergey Zigachev 	return 0;
143b843c749SSergey Zigachev }
144b843c749SSergey Zigachev 
smu7_get_current_pcie_speed(struct pp_hwmgr * hwmgr)145b843c749SSergey Zigachev static uint16_t smu7_get_current_pcie_speed(struct pp_hwmgr *hwmgr)
146b843c749SSergey Zigachev {
147b843c749SSergey Zigachev 	uint32_t speedCntl = 0;
148b843c749SSergey Zigachev 
149b843c749SSergey Zigachev 	/* mmPCIE_PORT_INDEX rename as mmPCIE_INDEX */
150b843c749SSergey Zigachev 	speedCntl = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__PCIE,
151b843c749SSergey Zigachev 			ixPCIE_LC_SPEED_CNTL);
152b843c749SSergey Zigachev 	return((uint16_t)PHM_GET_FIELD(speedCntl,
153b843c749SSergey Zigachev 			PCIE_LC_SPEED_CNTL, LC_CURRENT_DATA_RATE));
154b843c749SSergey Zigachev }
155b843c749SSergey Zigachev 
smu7_get_current_pcie_lane_number(struct pp_hwmgr * hwmgr)156b843c749SSergey Zigachev static int smu7_get_current_pcie_lane_number(struct pp_hwmgr *hwmgr)
157b843c749SSergey Zigachev {
158b843c749SSergey Zigachev 	uint32_t link_width;
159b843c749SSergey Zigachev 
160b843c749SSergey Zigachev 	/* mmPCIE_PORT_INDEX rename as mmPCIE_INDEX */
161b843c749SSergey Zigachev 	link_width = PHM_READ_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__PCIE,
162b843c749SSergey Zigachev 			PCIE_LC_LINK_WIDTH_CNTL, LC_LINK_WIDTH_RD);
163b843c749SSergey Zigachev 
164b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((7 >= link_width),
165b843c749SSergey Zigachev 			"Invalid PCIe lane width!", return 0);
166b843c749SSergey Zigachev 
167b843c749SSergey Zigachev 	return decode_pcie_lane_width(link_width);
168b843c749SSergey Zigachev }
169b843c749SSergey Zigachev 
170b843c749SSergey Zigachev /**
171b843c749SSergey Zigachev * Enable voltage control
172b843c749SSergey Zigachev *
173b843c749SSergey Zigachev * @param    pHwMgr  the address of the powerplay hardware manager.
174b843c749SSergey Zigachev * @return   always PP_Result_OK
175b843c749SSergey Zigachev */
smu7_enable_smc_voltage_controller(struct pp_hwmgr * hwmgr)176b843c749SSergey Zigachev static int smu7_enable_smc_voltage_controller(struct pp_hwmgr *hwmgr)
177b843c749SSergey Zigachev {
178b843c749SSergey Zigachev 	if (hwmgr->chip_id == CHIP_VEGAM) {
179b843c749SSergey Zigachev 		PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device,
180b843c749SSergey Zigachev 				CGS_IND_REG__SMC, PWR_SVI2_PLANE1_LOAD, PSI1, 0);
181b843c749SSergey Zigachev 		PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device,
182b843c749SSergey Zigachev 				CGS_IND_REG__SMC, PWR_SVI2_PLANE1_LOAD, PSI0_EN, 0);
183b843c749SSergey Zigachev 	}
184b843c749SSergey Zigachev 
185b843c749SSergey Zigachev 	if (hwmgr->feature_mask & PP_SMC_VOLTAGE_CONTROL_MASK)
186b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_Voltage_Cntl_Enable);
187b843c749SSergey Zigachev 
188b843c749SSergey Zigachev 	return 0;
189b843c749SSergey Zigachev }
190b843c749SSergey Zigachev 
191b843c749SSergey Zigachev /**
192b843c749SSergey Zigachev * Checks if we want to support voltage control
193b843c749SSergey Zigachev *
194b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
195b843c749SSergey Zigachev */
smu7_voltage_control(const struct pp_hwmgr * hwmgr)196b843c749SSergey Zigachev static bool smu7_voltage_control(const struct pp_hwmgr *hwmgr)
197b843c749SSergey Zigachev {
198b843c749SSergey Zigachev 	const struct smu7_hwmgr *data =
199b843c749SSergey Zigachev 			(const struct smu7_hwmgr *)(hwmgr->backend);
200b843c749SSergey Zigachev 
201b843c749SSergey Zigachev 	return (SMU7_VOLTAGE_CONTROL_NONE != data->voltage_control);
202b843c749SSergey Zigachev }
203b843c749SSergey Zigachev 
204b843c749SSergey Zigachev /**
205b843c749SSergey Zigachev * Enable voltage control
206b843c749SSergey Zigachev *
207b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
208b843c749SSergey Zigachev * @return   always 0
209b843c749SSergey Zigachev */
smu7_enable_voltage_control(struct pp_hwmgr * hwmgr)210b843c749SSergey Zigachev static int smu7_enable_voltage_control(struct pp_hwmgr *hwmgr)
211b843c749SSergey Zigachev {
212b843c749SSergey Zigachev 	/* enable voltage control */
213b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
214b843c749SSergey Zigachev 			GENERAL_PWRMGT, VOLT_PWRMGT_EN, 1);
215b843c749SSergey Zigachev 
216b843c749SSergey Zigachev 	return 0;
217b843c749SSergey Zigachev }
218b843c749SSergey Zigachev 
phm_get_svi2_voltage_table_v0(pp_atomctrl_voltage_table * voltage_table,struct phm_clock_voltage_dependency_table * voltage_dependency_table)219b843c749SSergey Zigachev static int phm_get_svi2_voltage_table_v0(pp_atomctrl_voltage_table *voltage_table,
220b843c749SSergey Zigachev 		struct phm_clock_voltage_dependency_table *voltage_dependency_table
221b843c749SSergey Zigachev 		)
222b843c749SSergey Zigachev {
223b843c749SSergey Zigachev 	uint32_t i;
224b843c749SSergey Zigachev 
225b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((NULL != voltage_table),
226b843c749SSergey Zigachev 			"Voltage Dependency Table empty.", return -EINVAL;);
227b843c749SSergey Zigachev 
228b843c749SSergey Zigachev 	voltage_table->mask_low = 0;
229b843c749SSergey Zigachev 	voltage_table->phase_delay = 0;
230b843c749SSergey Zigachev 	voltage_table->count = voltage_dependency_table->count;
231b843c749SSergey Zigachev 
232b843c749SSergey Zigachev 	for (i = 0; i < voltage_dependency_table->count; i++) {
233b843c749SSergey Zigachev 		voltage_table->entries[i].value =
234b843c749SSergey Zigachev 			voltage_dependency_table->entries[i].v;
235b843c749SSergey Zigachev 		voltage_table->entries[i].smio_low = 0;
236b843c749SSergey Zigachev 	}
237b843c749SSergey Zigachev 
238b843c749SSergey Zigachev 	return 0;
239b843c749SSergey Zigachev }
240b843c749SSergey Zigachev 
241b843c749SSergey Zigachev 
242b843c749SSergey Zigachev /**
243b843c749SSergey Zigachev * Create Voltage Tables.
244b843c749SSergey Zigachev *
245b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
246b843c749SSergey Zigachev * @return   always 0
247b843c749SSergey Zigachev */
smu7_construct_voltage_tables(struct pp_hwmgr * hwmgr)248b843c749SSergey Zigachev static int smu7_construct_voltage_tables(struct pp_hwmgr *hwmgr)
249b843c749SSergey Zigachev {
250b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
251b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
252b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)hwmgr->pptable;
253b843c749SSergey Zigachev 	int result = 0;
254b843c749SSergey Zigachev 	uint32_t tmp;
255b843c749SSergey Zigachev 
256b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control) {
257b843c749SSergey Zigachev 		result = atomctrl_get_voltage_table_v3(hwmgr,
258b843c749SSergey Zigachev 				VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT,
259b843c749SSergey Zigachev 				&(data->mvdd_voltage_table));
260b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
261b843c749SSergey Zigachev 				"Failed to retrieve MVDD table.",
262b843c749SSergey Zigachev 				return result);
263b843c749SSergey Zigachev 	} else if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control) {
264b843c749SSergey Zigachev 		if (hwmgr->pp_table_version == PP_TABLE_V1)
265b843c749SSergey Zigachev 			result = phm_get_svi2_mvdd_voltage_table(&(data->mvdd_voltage_table),
266b843c749SSergey Zigachev 					table_info->vdd_dep_on_mclk);
267b843c749SSergey Zigachev 		else if (hwmgr->pp_table_version == PP_TABLE_V0)
268b843c749SSergey Zigachev 			result = phm_get_svi2_voltage_table_v0(&(data->mvdd_voltage_table),
269b843c749SSergey Zigachev 					hwmgr->dyn_state.mvdd_dependency_on_mclk);
270b843c749SSergey Zigachev 
271b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
272b843c749SSergey Zigachev 				"Failed to retrieve SVI2 MVDD table from dependancy table.",
273b843c749SSergey Zigachev 				return result;);
274b843c749SSergey Zigachev 	}
275b843c749SSergey Zigachev 
276b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
277b843c749SSergey Zigachev 		result = atomctrl_get_voltage_table_v3(hwmgr,
278b843c749SSergey Zigachev 				VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT,
279b843c749SSergey Zigachev 				&(data->vddci_voltage_table));
280b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
281b843c749SSergey Zigachev 				"Failed to retrieve VDDCI table.",
282b843c749SSergey Zigachev 				return result);
283b843c749SSergey Zigachev 	} else if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control) {
284b843c749SSergey Zigachev 		if (hwmgr->pp_table_version == PP_TABLE_V1)
285b843c749SSergey Zigachev 			result = phm_get_svi2_vddci_voltage_table(&(data->vddci_voltage_table),
286b843c749SSergey Zigachev 					table_info->vdd_dep_on_mclk);
287b843c749SSergey Zigachev 		else if (hwmgr->pp_table_version == PP_TABLE_V0)
288b843c749SSergey Zigachev 			result = phm_get_svi2_voltage_table_v0(&(data->vddci_voltage_table),
289b843c749SSergey Zigachev 					hwmgr->dyn_state.vddci_dependency_on_mclk);
290b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
291b843c749SSergey Zigachev 				"Failed to retrieve SVI2 VDDCI table from dependancy table.",
292b843c749SSergey Zigachev 				return result);
293b843c749SSergey Zigachev 	}
294b843c749SSergey Zigachev 
295b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vdd_gfx_control) {
296b843c749SSergey Zigachev 		/* VDDGFX has only SVI2 voltage control */
297b843c749SSergey Zigachev 		result = phm_get_svi2_vdd_voltage_table(&(data->vddgfx_voltage_table),
298b843c749SSergey Zigachev 					table_info->vddgfx_lookup_table);
299b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
300b843c749SSergey Zigachev 			"Failed to retrieve SVI2 VDDGFX table from lookup table.", return result;);
301b843c749SSergey Zigachev 	}
302b843c749SSergey Zigachev 
303b843c749SSergey Zigachev 
304b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->voltage_control) {
305b843c749SSergey Zigachev 		result = atomctrl_get_voltage_table_v3(hwmgr,
306b843c749SSergey Zigachev 					VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_GPIO_LUT,
307b843c749SSergey Zigachev 					&data->vddc_voltage_table);
308b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
309b843c749SSergey Zigachev 			"Failed to retrieve VDDC table.", return result;);
310b843c749SSergey Zigachev 	} else if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control) {
311b843c749SSergey Zigachev 
312b843c749SSergey Zigachev 		if (hwmgr->pp_table_version == PP_TABLE_V0)
313b843c749SSergey Zigachev 			result = phm_get_svi2_voltage_table_v0(&data->vddc_voltage_table,
314b843c749SSergey Zigachev 					hwmgr->dyn_state.vddc_dependency_on_mclk);
315b843c749SSergey Zigachev 		else if (hwmgr->pp_table_version == PP_TABLE_V1)
316b843c749SSergey Zigachev 			result = phm_get_svi2_vdd_voltage_table(&(data->vddc_voltage_table),
317b843c749SSergey Zigachev 				table_info->vddc_lookup_table);
318b843c749SSergey Zigachev 
319b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
320b843c749SSergey Zigachev 			"Failed to retrieve SVI2 VDDC table from dependancy table.", return result;);
321b843c749SSergey Zigachev 	}
322b843c749SSergey Zigachev 
323b843c749SSergey Zigachev 	tmp = smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_VDDC);
324b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
325b843c749SSergey Zigachev 			(data->vddc_voltage_table.count <= tmp),
326b843c749SSergey Zigachev 		"Too many voltage values for VDDC. Trimming to fit state table.",
327b843c749SSergey Zigachev 			phm_trim_voltage_table_to_fit_state_table(tmp,
328b843c749SSergey Zigachev 						&(data->vddc_voltage_table)));
329b843c749SSergey Zigachev 
330b843c749SSergey Zigachev 	tmp = smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_VDDGFX);
331b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
332b843c749SSergey Zigachev 			(data->vddgfx_voltage_table.count <= tmp),
333b843c749SSergey Zigachev 		"Too many voltage values for VDDC. Trimming to fit state table.",
334b843c749SSergey Zigachev 			phm_trim_voltage_table_to_fit_state_table(tmp,
335b843c749SSergey Zigachev 						&(data->vddgfx_voltage_table)));
336b843c749SSergey Zigachev 
337b843c749SSergey Zigachev 	tmp = smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_VDDCI);
338b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
339b843c749SSergey Zigachev 			(data->vddci_voltage_table.count <= tmp),
340b843c749SSergey Zigachev 		"Too many voltage values for VDDCI. Trimming to fit state table.",
341b843c749SSergey Zigachev 			phm_trim_voltage_table_to_fit_state_table(tmp,
342b843c749SSergey Zigachev 					&(data->vddci_voltage_table)));
343b843c749SSergey Zigachev 
344b843c749SSergey Zigachev 	tmp = smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_MVDD);
345b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
346b843c749SSergey Zigachev 			(data->mvdd_voltage_table.count <= tmp),
347b843c749SSergey Zigachev 		"Too many voltage values for MVDD. Trimming to fit state table.",
348b843c749SSergey Zigachev 			phm_trim_voltage_table_to_fit_state_table(tmp,
349b843c749SSergey Zigachev 						&(data->mvdd_voltage_table)));
350b843c749SSergey Zigachev 
351b843c749SSergey Zigachev 	return 0;
352b843c749SSergey Zigachev }
353b843c749SSergey Zigachev 
354b843c749SSergey Zigachev /**
355b843c749SSergey Zigachev * Programs static screed detection parameters
356b843c749SSergey Zigachev *
357b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
358b843c749SSergey Zigachev * @return   always 0
359b843c749SSergey Zigachev */
smu7_program_static_screen_threshold_parameters(struct pp_hwmgr * hwmgr)360b843c749SSergey Zigachev static int smu7_program_static_screen_threshold_parameters(
361b843c749SSergey Zigachev 							struct pp_hwmgr *hwmgr)
362b843c749SSergey Zigachev {
363b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
364b843c749SSergey Zigachev 
365b843c749SSergey Zigachev 	/* Set static screen threshold unit */
366b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
367b843c749SSergey Zigachev 			CG_STATIC_SCREEN_PARAMETER, STATIC_SCREEN_THRESHOLD_UNIT,
368b843c749SSergey Zigachev 			data->static_screen_threshold_unit);
369b843c749SSergey Zigachev 	/* Set static screen threshold */
370b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
371b843c749SSergey Zigachev 			CG_STATIC_SCREEN_PARAMETER, STATIC_SCREEN_THRESHOLD,
372b843c749SSergey Zigachev 			data->static_screen_threshold);
373b843c749SSergey Zigachev 
374b843c749SSergey Zigachev 	return 0;
375b843c749SSergey Zigachev }
376b843c749SSergey Zigachev 
377b843c749SSergey Zigachev /**
378b843c749SSergey Zigachev * Setup display gap for glitch free memory clock switching.
379b843c749SSergey Zigachev *
380b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
381b843c749SSergey Zigachev * @return   always  0
382b843c749SSergey Zigachev */
smu7_enable_display_gap(struct pp_hwmgr * hwmgr)383b843c749SSergey Zigachev static int smu7_enable_display_gap(struct pp_hwmgr *hwmgr)
384b843c749SSergey Zigachev {
385b843c749SSergey Zigachev 	uint32_t display_gap =
386b843c749SSergey Zigachev 			cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
387b843c749SSergey Zigachev 					ixCG_DISPLAY_GAP_CNTL);
388b843c749SSergey Zigachev 
389b843c749SSergey Zigachev 	display_gap = PHM_SET_FIELD(display_gap, CG_DISPLAY_GAP_CNTL,
390b843c749SSergey Zigachev 			DISP_GAP, DISPLAY_GAP_IGNORE);
391b843c749SSergey Zigachev 
392b843c749SSergey Zigachev 	display_gap = PHM_SET_FIELD(display_gap, CG_DISPLAY_GAP_CNTL,
393b843c749SSergey Zigachev 			DISP_GAP_MCHG, DISPLAY_GAP_VBLANK);
394b843c749SSergey Zigachev 
395b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
396b843c749SSergey Zigachev 			ixCG_DISPLAY_GAP_CNTL, display_gap);
397b843c749SSergey Zigachev 
398b843c749SSergey Zigachev 	return 0;
399b843c749SSergey Zigachev }
400b843c749SSergey Zigachev 
401b843c749SSergey Zigachev /**
402b843c749SSergey Zigachev * Programs activity state transition voting clients
403b843c749SSergey Zigachev *
404b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
405b843c749SSergey Zigachev * @return   always  0
406b843c749SSergey Zigachev */
smu7_program_voting_clients(struct pp_hwmgr * hwmgr)407b843c749SSergey Zigachev static int smu7_program_voting_clients(struct pp_hwmgr *hwmgr)
408b843c749SSergey Zigachev {
409b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
410b843c749SSergey Zigachev 	int i;
411b843c749SSergey Zigachev 
412b843c749SSergey Zigachev 	/* Clear reset for voting clients before enabling DPM */
413b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
414b843c749SSergey Zigachev 			SCLK_PWRMGT_CNTL, RESET_SCLK_CNT, 0);
415b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
416b843c749SSergey Zigachev 			SCLK_PWRMGT_CNTL, RESET_BUSY_CNT, 0);
417b843c749SSergey Zigachev 
418b843c749SSergey Zigachev 	for (i = 0; i < 8; i++)
419b843c749SSergey Zigachev 		cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
420b843c749SSergey Zigachev 					ixCG_FREQ_TRAN_VOTING_0 + i * 4,
421b843c749SSergey Zigachev 					data->voting_rights_clients[i]);
422b843c749SSergey Zigachev 	return 0;
423b843c749SSergey Zigachev }
424b843c749SSergey Zigachev 
smu7_clear_voting_clients(struct pp_hwmgr * hwmgr)425b843c749SSergey Zigachev static int smu7_clear_voting_clients(struct pp_hwmgr *hwmgr)
426b843c749SSergey Zigachev {
427b843c749SSergey Zigachev 	int i;
428b843c749SSergey Zigachev 
429b843c749SSergey Zigachev 	/* Reset voting clients before disabling DPM */
430b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
431b843c749SSergey Zigachev 			SCLK_PWRMGT_CNTL, RESET_SCLK_CNT, 1);
432b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
433b843c749SSergey Zigachev 			SCLK_PWRMGT_CNTL, RESET_BUSY_CNT, 1);
434b843c749SSergey Zigachev 
435b843c749SSergey Zigachev 	for (i = 0; i < 8; i++)
436b843c749SSergey Zigachev 		cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
437b843c749SSergey Zigachev 				ixCG_FREQ_TRAN_VOTING_0 + i * 4, 0);
438b843c749SSergey Zigachev 
439b843c749SSergey Zigachev 	return 0;
440b843c749SSergey Zigachev }
441b843c749SSergey Zigachev 
442b843c749SSergey Zigachev /* Copy one arb setting to another and then switch the active set.
443b843c749SSergey Zigachev  * arb_src and arb_dest is one of the MC_CG_ARB_FREQ_Fx constants.
444b843c749SSergey Zigachev  */
smu7_copy_and_switch_arb_sets(struct pp_hwmgr * hwmgr,uint32_t arb_src,uint32_t arb_dest)445b843c749SSergey Zigachev static int smu7_copy_and_switch_arb_sets(struct pp_hwmgr *hwmgr,
446b843c749SSergey Zigachev 		uint32_t arb_src, uint32_t arb_dest)
447b843c749SSergey Zigachev {
448b843c749SSergey Zigachev 	uint32_t mc_arb_dram_timing;
449b843c749SSergey Zigachev 	uint32_t mc_arb_dram_timing2;
450b843c749SSergey Zigachev 	uint32_t burst_time;
451b843c749SSergey Zigachev 	uint32_t mc_cg_config;
452b843c749SSergey Zigachev 
453b843c749SSergey Zigachev 	switch (arb_src) {
454b843c749SSergey Zigachev 	case MC_CG_ARB_FREQ_F0:
455b843c749SSergey Zigachev 		mc_arb_dram_timing  = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
456b843c749SSergey Zigachev 		mc_arb_dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
457b843c749SSergey Zigachev 		burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0);
458b843c749SSergey Zigachev 		break;
459b843c749SSergey Zigachev 	case MC_CG_ARB_FREQ_F1:
460b843c749SSergey Zigachev 		mc_arb_dram_timing  = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING_1);
461b843c749SSergey Zigachev 		mc_arb_dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2_1);
462b843c749SSergey Zigachev 		burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE1);
463b843c749SSergey Zigachev 		break;
464b843c749SSergey Zigachev 	default:
465b843c749SSergey Zigachev 		return -EINVAL;
466b843c749SSergey Zigachev 	}
467b843c749SSergey Zigachev 
468b843c749SSergey Zigachev 	switch (arb_dest) {
469b843c749SSergey Zigachev 	case MC_CG_ARB_FREQ_F0:
470b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING, mc_arb_dram_timing);
471b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2, mc_arb_dram_timing2);
472b843c749SSergey Zigachev 		PHM_WRITE_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0, burst_time);
473b843c749SSergey Zigachev 		break;
474b843c749SSergey Zigachev 	case MC_CG_ARB_FREQ_F1:
475b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING_1, mc_arb_dram_timing);
476b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2_1, mc_arb_dram_timing2);
477b843c749SSergey Zigachev 		PHM_WRITE_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE1, burst_time);
478b843c749SSergey Zigachev 		break;
479b843c749SSergey Zigachev 	default:
480b843c749SSergey Zigachev 		return -EINVAL;
481b843c749SSergey Zigachev 	}
482b843c749SSergey Zigachev 
483b843c749SSergey Zigachev 	mc_cg_config = cgs_read_register(hwmgr->device, mmMC_CG_CONFIG);
484b843c749SSergey Zigachev 	mc_cg_config |= 0x0000000F;
485b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_CG_CONFIG, mc_cg_config);
486b843c749SSergey Zigachev 	PHM_WRITE_FIELD(hwmgr->device, MC_ARB_CG, CG_ARB_REQ, arb_dest);
487b843c749SSergey Zigachev 
488b843c749SSergey Zigachev 	return 0;
489b843c749SSergey Zigachev }
490b843c749SSergey Zigachev 
smu7_reset_to_default(struct pp_hwmgr * hwmgr)491b843c749SSergey Zigachev static int smu7_reset_to_default(struct pp_hwmgr *hwmgr)
492b843c749SSergey Zigachev {
493b843c749SSergey Zigachev 	return smum_send_msg_to_smc(hwmgr, PPSMC_MSG_ResetToDefaults);
494b843c749SSergey Zigachev }
495b843c749SSergey Zigachev 
496b843c749SSergey Zigachev /**
497b843c749SSergey Zigachev * Initial switch from ARB F0->F1
498b843c749SSergey Zigachev *
499b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
500b843c749SSergey Zigachev * @return   always 0
501b843c749SSergey Zigachev * This function is to be called from the SetPowerState table.
502b843c749SSergey Zigachev */
smu7_initial_switch_from_arbf0_to_f1(struct pp_hwmgr * hwmgr)503b843c749SSergey Zigachev static int smu7_initial_switch_from_arbf0_to_f1(struct pp_hwmgr *hwmgr)
504b843c749SSergey Zigachev {
505b843c749SSergey Zigachev 	return smu7_copy_and_switch_arb_sets(hwmgr,
506b843c749SSergey Zigachev 			MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
507b843c749SSergey Zigachev }
508b843c749SSergey Zigachev 
smu7_force_switch_to_arbf0(struct pp_hwmgr * hwmgr)509b843c749SSergey Zigachev static int smu7_force_switch_to_arbf0(struct pp_hwmgr *hwmgr)
510b843c749SSergey Zigachev {
511b843c749SSergey Zigachev 	uint32_t tmp;
512b843c749SSergey Zigachev 
513b843c749SSergey Zigachev 	tmp = (cgs_read_ind_register(hwmgr->device,
514b843c749SSergey Zigachev 			CGS_IND_REG__SMC, ixSMC_SCRATCH9) &
515b843c749SSergey Zigachev 			0x0000ff00) >> 8;
516b843c749SSergey Zigachev 
517b843c749SSergey Zigachev 	if (tmp == MC_CG_ARB_FREQ_F0)
518b843c749SSergey Zigachev 		return 0;
519b843c749SSergey Zigachev 
520b843c749SSergey Zigachev 	return smu7_copy_and_switch_arb_sets(hwmgr,
521b843c749SSergey Zigachev 			tmp, MC_CG_ARB_FREQ_F0);
522b843c749SSergey Zigachev }
523b843c749SSergey Zigachev 
smu7_setup_default_pcie_table(struct pp_hwmgr * hwmgr)524b843c749SSergey Zigachev static int smu7_setup_default_pcie_table(struct pp_hwmgr *hwmgr)
525b843c749SSergey Zigachev {
526b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
527b843c749SSergey Zigachev 
528b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
529b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
530b843c749SSergey Zigachev 	struct phm_ppt_v1_pcie_table *pcie_table = NULL;
531b843c749SSergey Zigachev 
532b843c749SSergey Zigachev 	uint32_t i, max_entry;
533b843c749SSergey Zigachev 	uint32_t tmp;
534b843c749SSergey Zigachev 
535b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((data->use_pcie_performance_levels ||
536b843c749SSergey Zigachev 			data->use_pcie_power_saving_levels), "No pcie performance levels!",
537b843c749SSergey Zigachev 			return -EINVAL);
538b843c749SSergey Zigachev 
539b843c749SSergey Zigachev 	if (table_info != NULL)
540b843c749SSergey Zigachev 		pcie_table = table_info->pcie_table;
541b843c749SSergey Zigachev 
542b843c749SSergey Zigachev 	if (data->use_pcie_performance_levels &&
543b843c749SSergey Zigachev 			!data->use_pcie_power_saving_levels) {
544b843c749SSergey Zigachev 		data->pcie_gen_power_saving = data->pcie_gen_performance;
545b843c749SSergey Zigachev 		data->pcie_lane_power_saving = data->pcie_lane_performance;
546b843c749SSergey Zigachev 	} else if (!data->use_pcie_performance_levels &&
547b843c749SSergey Zigachev 			data->use_pcie_power_saving_levels) {
548b843c749SSergey Zigachev 		data->pcie_gen_performance = data->pcie_gen_power_saving;
549b843c749SSergey Zigachev 		data->pcie_lane_performance = data->pcie_lane_power_saving;
550b843c749SSergey Zigachev 	}
551b843c749SSergey Zigachev 	tmp = smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_LINK);
552b843c749SSergey Zigachev 	phm_reset_single_dpm_table(&data->dpm_table.pcie_speed_table,
553b843c749SSergey Zigachev 					tmp,
554b843c749SSergey Zigachev 					MAX_REGULAR_DPM_NUMBER);
555b843c749SSergey Zigachev 
556b843c749SSergey Zigachev 	if (pcie_table != NULL) {
557b843c749SSergey Zigachev 		/* max_entry is used to make sure we reserve one PCIE level
558b843c749SSergey Zigachev 		 * for boot level (fix for A+A PSPP issue).
559b843c749SSergey Zigachev 		 * If PCIE table from PPTable have ULV entry + 8 entries,
560b843c749SSergey Zigachev 		 * then ignore the last entry.*/
561b843c749SSergey Zigachev 		max_entry = (tmp < pcie_table->count) ? tmp : pcie_table->count;
562b843c749SSergey Zigachev 		for (i = 1; i < max_entry; i++) {
563b843c749SSergey Zigachev 			phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, i - 1,
564b843c749SSergey Zigachev 					get_pcie_gen_support(data->pcie_gen_cap,
565b843c749SSergey Zigachev 							pcie_table->entries[i].gen_speed),
566b843c749SSergey Zigachev 					get_pcie_lane_support(data->pcie_lane_cap,
567b843c749SSergey Zigachev 							pcie_table->entries[i].lane_width));
568b843c749SSergey Zigachev 		}
569b843c749SSergey Zigachev 		data->dpm_table.pcie_speed_table.count = max_entry - 1;
570b843c749SSergey Zigachev 		smum_update_smc_table(hwmgr, SMU_BIF_TABLE);
571b843c749SSergey Zigachev 	} else {
572b843c749SSergey Zigachev 		/* Hardcode Pcie Table */
573b843c749SSergey Zigachev 		phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 0,
574b843c749SSergey Zigachev 				get_pcie_gen_support(data->pcie_gen_cap,
575b843c749SSergey Zigachev 						PP_Min_PCIEGen),
576b843c749SSergey Zigachev 				get_pcie_lane_support(data->pcie_lane_cap,
577b843c749SSergey Zigachev 						PP_Max_PCIELane));
578b843c749SSergey Zigachev 		phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 1,
579b843c749SSergey Zigachev 				get_pcie_gen_support(data->pcie_gen_cap,
580b843c749SSergey Zigachev 						PP_Min_PCIEGen),
581b843c749SSergey Zigachev 				get_pcie_lane_support(data->pcie_lane_cap,
582b843c749SSergey Zigachev 						PP_Max_PCIELane));
583b843c749SSergey Zigachev 		phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 2,
584b843c749SSergey Zigachev 				get_pcie_gen_support(data->pcie_gen_cap,
585b843c749SSergey Zigachev 						PP_Max_PCIEGen),
586b843c749SSergey Zigachev 				get_pcie_lane_support(data->pcie_lane_cap,
587b843c749SSergey Zigachev 						PP_Max_PCIELane));
588b843c749SSergey Zigachev 		phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 3,
589b843c749SSergey Zigachev 				get_pcie_gen_support(data->pcie_gen_cap,
590b843c749SSergey Zigachev 						PP_Max_PCIEGen),
591b843c749SSergey Zigachev 				get_pcie_lane_support(data->pcie_lane_cap,
592b843c749SSergey Zigachev 						PP_Max_PCIELane));
593b843c749SSergey Zigachev 		phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 4,
594b843c749SSergey Zigachev 				get_pcie_gen_support(data->pcie_gen_cap,
595b843c749SSergey Zigachev 						PP_Max_PCIEGen),
596b843c749SSergey Zigachev 				get_pcie_lane_support(data->pcie_lane_cap,
597b843c749SSergey Zigachev 						PP_Max_PCIELane));
598b843c749SSergey Zigachev 		phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 5,
599b843c749SSergey Zigachev 				get_pcie_gen_support(data->pcie_gen_cap,
600b843c749SSergey Zigachev 						PP_Max_PCIEGen),
601b843c749SSergey Zigachev 				get_pcie_lane_support(data->pcie_lane_cap,
602b843c749SSergey Zigachev 						PP_Max_PCIELane));
603b843c749SSergey Zigachev 
604b843c749SSergey Zigachev 		data->dpm_table.pcie_speed_table.count = 6;
605b843c749SSergey Zigachev 	}
606b843c749SSergey Zigachev 	/* Populate last level for boot PCIE level, but do not increment count. */
607b843c749SSergey Zigachev 	if (hwmgr->chip_family == AMDGPU_FAMILY_CI) {
608b843c749SSergey Zigachev 		for (i = 0; i <= data->dpm_table.pcie_speed_table.count; i++)
609b843c749SSergey Zigachev 			phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, i,
610b843c749SSergey Zigachev 				get_pcie_gen_support(data->pcie_gen_cap,
611b843c749SSergey Zigachev 						PP_Max_PCIEGen),
612b843c749SSergey Zigachev 				data->vbios_boot_state.pcie_lane_bootup_value);
613b843c749SSergey Zigachev 	} else {
614b843c749SSergey Zigachev 		phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table,
615b843c749SSergey Zigachev 			data->dpm_table.pcie_speed_table.count,
616b843c749SSergey Zigachev 			get_pcie_gen_support(data->pcie_gen_cap,
617b843c749SSergey Zigachev 					PP_Min_PCIEGen),
618b843c749SSergey Zigachev 			get_pcie_lane_support(data->pcie_lane_cap,
619b843c749SSergey Zigachev 					PP_Max_PCIELane));
620b843c749SSergey Zigachev 	}
621b843c749SSergey Zigachev 	return 0;
622b843c749SSergey Zigachev }
623b843c749SSergey Zigachev 
smu7_reset_dpm_tables(struct pp_hwmgr * hwmgr)624b843c749SSergey Zigachev static int smu7_reset_dpm_tables(struct pp_hwmgr *hwmgr)
625b843c749SSergey Zigachev {
626b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
627b843c749SSergey Zigachev 
628b843c749SSergey Zigachev 	memset(&(data->dpm_table), 0x00, sizeof(data->dpm_table));
629b843c749SSergey Zigachev 
630b843c749SSergey Zigachev 	phm_reset_single_dpm_table(
631b843c749SSergey Zigachev 			&data->dpm_table.sclk_table,
632b843c749SSergey Zigachev 				smum_get_mac_definition(hwmgr,
633b843c749SSergey Zigachev 					SMU_MAX_LEVELS_GRAPHICS),
634b843c749SSergey Zigachev 					MAX_REGULAR_DPM_NUMBER);
635b843c749SSergey Zigachev 	phm_reset_single_dpm_table(
636b843c749SSergey Zigachev 			&data->dpm_table.mclk_table,
637b843c749SSergey Zigachev 			smum_get_mac_definition(hwmgr,
638b843c749SSergey Zigachev 				SMU_MAX_LEVELS_MEMORY), MAX_REGULAR_DPM_NUMBER);
639b843c749SSergey Zigachev 
640b843c749SSergey Zigachev 	phm_reset_single_dpm_table(
641b843c749SSergey Zigachev 			&data->dpm_table.vddc_table,
642b843c749SSergey Zigachev 				smum_get_mac_definition(hwmgr,
643b843c749SSergey Zigachev 					SMU_MAX_LEVELS_VDDC),
644b843c749SSergey Zigachev 					MAX_REGULAR_DPM_NUMBER);
645b843c749SSergey Zigachev 	phm_reset_single_dpm_table(
646b843c749SSergey Zigachev 			&data->dpm_table.vddci_table,
647b843c749SSergey Zigachev 			smum_get_mac_definition(hwmgr,
648b843c749SSergey Zigachev 				SMU_MAX_LEVELS_VDDCI), MAX_REGULAR_DPM_NUMBER);
649b843c749SSergey Zigachev 
650b843c749SSergey Zigachev 	phm_reset_single_dpm_table(
651b843c749SSergey Zigachev 			&data->dpm_table.mvdd_table,
652b843c749SSergey Zigachev 				smum_get_mac_definition(hwmgr,
653b843c749SSergey Zigachev 					SMU_MAX_LEVELS_MVDD),
654b843c749SSergey Zigachev 					MAX_REGULAR_DPM_NUMBER);
655b843c749SSergey Zigachev 	return 0;
656b843c749SSergey Zigachev }
657b843c749SSergey Zigachev /*
658b843c749SSergey Zigachev  * This function is to initialize all DPM state tables
659b843c749SSergey Zigachev  * for SMU7 based on the dependency table.
660b843c749SSergey Zigachev  * Dynamic state patching function will then trim these
661b843c749SSergey Zigachev  * state tables to the allowed range based
662b843c749SSergey Zigachev  * on the power policy or external client requests,
663b843c749SSergey Zigachev  * such as UVD request, etc.
664b843c749SSergey Zigachev  */
665b843c749SSergey Zigachev 
smu7_setup_dpm_tables_v0(struct pp_hwmgr * hwmgr)666b843c749SSergey Zigachev static int smu7_setup_dpm_tables_v0(struct pp_hwmgr *hwmgr)
667b843c749SSergey Zigachev {
668b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
669b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *allowed_vdd_sclk_table =
670b843c749SSergey Zigachev 		hwmgr->dyn_state.vddc_dependency_on_sclk;
671b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *allowed_vdd_mclk_table =
672b843c749SSergey Zigachev 		hwmgr->dyn_state.vddc_dependency_on_mclk;
673b843c749SSergey Zigachev 	struct phm_cac_leakage_table *std_voltage_table =
674b843c749SSergey Zigachev 		hwmgr->dyn_state.cac_leakage_table;
675b843c749SSergey Zigachev 	uint32_t i;
676b843c749SSergey Zigachev 
677b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_vdd_sclk_table != NULL,
678b843c749SSergey Zigachev 		"SCLK dependency table is missing. This table is mandatory", return -EINVAL);
679b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_vdd_sclk_table->count >= 1,
680b843c749SSergey Zigachev 		"SCLK dependency table has to have is missing. This table is mandatory", return -EINVAL);
681b843c749SSergey Zigachev 
682b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_vdd_mclk_table != NULL,
683b843c749SSergey Zigachev 		"MCLK dependency table is missing. This table is mandatory", return -EINVAL);
684b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_vdd_mclk_table->count >= 1,
685b843c749SSergey Zigachev 		"VMCLK dependency table has to have is missing. This table is mandatory", return -EINVAL);
686b843c749SSergey Zigachev 
687b843c749SSergey Zigachev 
688b843c749SSergey Zigachev 	/* Initialize Sclk DPM table based on allow Sclk values*/
689b843c749SSergey Zigachev 	data->dpm_table.sclk_table.count = 0;
690b843c749SSergey Zigachev 
691b843c749SSergey Zigachev 	for (i = 0; i < allowed_vdd_sclk_table->count; i++) {
692b843c749SSergey Zigachev 		if (i == 0 || data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count-1].value !=
693b843c749SSergey Zigachev 				allowed_vdd_sclk_table->entries[i].clk) {
694b843c749SSergey Zigachev 			data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count].value =
695b843c749SSergey Zigachev 				allowed_vdd_sclk_table->entries[i].clk;
696b843c749SSergey Zigachev 			data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count].enabled = (i == 0) ? 1 : 0;
697b843c749SSergey Zigachev 			data->dpm_table.sclk_table.count++;
698b843c749SSergey Zigachev 		}
699b843c749SSergey Zigachev 	}
700b843c749SSergey Zigachev 
701b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_vdd_mclk_table != NULL,
702b843c749SSergey Zigachev 		"MCLK dependency table is missing. This table is mandatory", return -EINVAL);
703b843c749SSergey Zigachev 	/* Initialize Mclk DPM table based on allow Mclk values */
704b843c749SSergey Zigachev 	data->dpm_table.mclk_table.count = 0;
705b843c749SSergey Zigachev 	for (i = 0; i < allowed_vdd_mclk_table->count; i++) {
706b843c749SSergey Zigachev 		if (i == 0 || data->dpm_table.mclk_table.dpm_levels[data->dpm_table.mclk_table.count-1].value !=
707b843c749SSergey Zigachev 			allowed_vdd_mclk_table->entries[i].clk) {
708b843c749SSergey Zigachev 			data->dpm_table.mclk_table.dpm_levels[data->dpm_table.mclk_table.count].value =
709b843c749SSergey Zigachev 				allowed_vdd_mclk_table->entries[i].clk;
710b843c749SSergey Zigachev 			data->dpm_table.mclk_table.dpm_levels[data->dpm_table.mclk_table.count].enabled = (i == 0) ? 1 : 0;
711b843c749SSergey Zigachev 			data->dpm_table.mclk_table.count++;
712b843c749SSergey Zigachev 		}
713b843c749SSergey Zigachev 	}
714b843c749SSergey Zigachev 
715b843c749SSergey Zigachev 	/* Initialize Vddc DPM table based on allow Vddc values.  And populate corresponding std values. */
716b843c749SSergey Zigachev 	for (i = 0; i < allowed_vdd_sclk_table->count; i++) {
717b843c749SSergey Zigachev 		data->dpm_table.vddc_table.dpm_levels[i].value = allowed_vdd_mclk_table->entries[i].v;
718b843c749SSergey Zigachev 		data->dpm_table.vddc_table.dpm_levels[i].param1 = std_voltage_table->entries[i].Leakage;
719b843c749SSergey Zigachev 		/* param1 is for corresponding std voltage */
720b843c749SSergey Zigachev 		data->dpm_table.vddc_table.dpm_levels[i].enabled = 1;
721b843c749SSergey Zigachev 	}
722b843c749SSergey Zigachev 
723b843c749SSergey Zigachev 	data->dpm_table.vddc_table.count = allowed_vdd_sclk_table->count;
724b843c749SSergey Zigachev 	allowed_vdd_mclk_table = hwmgr->dyn_state.vddci_dependency_on_mclk;
725b843c749SSergey Zigachev 
726b843c749SSergey Zigachev 	if (NULL != allowed_vdd_mclk_table) {
727b843c749SSergey Zigachev 		/* Initialize Vddci DPM table based on allow Mclk values */
728b843c749SSergey Zigachev 		for (i = 0; i < allowed_vdd_mclk_table->count; i++) {
729b843c749SSergey Zigachev 			data->dpm_table.vddci_table.dpm_levels[i].value = allowed_vdd_mclk_table->entries[i].v;
730b843c749SSergey Zigachev 			data->dpm_table.vddci_table.dpm_levels[i].enabled = 1;
731b843c749SSergey Zigachev 		}
732b843c749SSergey Zigachev 		data->dpm_table.vddci_table.count = allowed_vdd_mclk_table->count;
733b843c749SSergey Zigachev 	}
734b843c749SSergey Zigachev 
735b843c749SSergey Zigachev 	allowed_vdd_mclk_table = hwmgr->dyn_state.mvdd_dependency_on_mclk;
736b843c749SSergey Zigachev 
737b843c749SSergey Zigachev 	if (NULL != allowed_vdd_mclk_table) {
738b843c749SSergey Zigachev 		/*
739b843c749SSergey Zigachev 		 * Initialize MVDD DPM table based on allow Mclk
740b843c749SSergey Zigachev 		 * values
741b843c749SSergey Zigachev 		 */
742b843c749SSergey Zigachev 		for (i = 0; i < allowed_vdd_mclk_table->count; i++) {
743b843c749SSergey Zigachev 			data->dpm_table.mvdd_table.dpm_levels[i].value = allowed_vdd_mclk_table->entries[i].v;
744b843c749SSergey Zigachev 			data->dpm_table.mvdd_table.dpm_levels[i].enabled = 1;
745b843c749SSergey Zigachev 		}
746b843c749SSergey Zigachev 		data->dpm_table.mvdd_table.count = allowed_vdd_mclk_table->count;
747b843c749SSergey Zigachev 	}
748b843c749SSergey Zigachev 
749b843c749SSergey Zigachev 	return 0;
750b843c749SSergey Zigachev }
751b843c749SSergey Zigachev 
smu7_setup_dpm_tables_v1(struct pp_hwmgr * hwmgr)752b843c749SSergey Zigachev static int smu7_setup_dpm_tables_v1(struct pp_hwmgr *hwmgr)
753b843c749SSergey Zigachev {
754b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
755b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
756b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
757b843c749SSergey Zigachev 	uint32_t i;
758b843c749SSergey Zigachev 
759b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_sclk_table;
760b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table;
761b843c749SSergey Zigachev 
762b843c749SSergey Zigachev 	if (table_info == NULL)
763b843c749SSergey Zigachev 		return -EINVAL;
764b843c749SSergey Zigachev 
765b843c749SSergey Zigachev 	dep_sclk_table = table_info->vdd_dep_on_sclk;
766b843c749SSergey Zigachev 	dep_mclk_table = table_info->vdd_dep_on_mclk;
767b843c749SSergey Zigachev 
768b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(dep_sclk_table != NULL,
769b843c749SSergey Zigachev 			"SCLK dependency table is missing.",
770b843c749SSergey Zigachev 			return -EINVAL);
771b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(dep_sclk_table->count >= 1,
772b843c749SSergey Zigachev 			"SCLK dependency table count is 0.",
773b843c749SSergey Zigachev 			return -EINVAL);
774b843c749SSergey Zigachev 
775b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(dep_mclk_table != NULL,
776b843c749SSergey Zigachev 			"MCLK dependency table is missing.",
777b843c749SSergey Zigachev 			return -EINVAL);
778b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(dep_mclk_table->count >= 1,
779b843c749SSergey Zigachev 			"MCLK dependency table count is 0",
780b843c749SSergey Zigachev 			return -EINVAL);
781b843c749SSergey Zigachev 
782b843c749SSergey Zigachev 	/* Initialize Sclk DPM table based on allow Sclk values */
783b843c749SSergey Zigachev 	data->dpm_table.sclk_table.count = 0;
784b843c749SSergey Zigachev 	for (i = 0; i < dep_sclk_table->count; i++) {
785b843c749SSergey Zigachev 		if (i == 0 || data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count - 1].value !=
786b843c749SSergey Zigachev 						dep_sclk_table->entries[i].clk) {
787b843c749SSergey Zigachev 
788b843c749SSergey Zigachev 			data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count].value =
789b843c749SSergey Zigachev 					dep_sclk_table->entries[i].clk;
790b843c749SSergey Zigachev 
791b843c749SSergey Zigachev 			data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count].enabled =
792b843c749SSergey Zigachev 					(i == 0) ? true : false;
793b843c749SSergey Zigachev 			data->dpm_table.sclk_table.count++;
794b843c749SSergey Zigachev 		}
795b843c749SSergey Zigachev 	}
796b843c749SSergey Zigachev 	if (hwmgr->platform_descriptor.overdriveLimit.engineClock == 0)
797b843c749SSergey Zigachev 		hwmgr->platform_descriptor.overdriveLimit.engineClock = dep_sclk_table->entries[i-1].clk;
798b843c749SSergey Zigachev 	/* Initialize Mclk DPM table based on allow Mclk values */
799b843c749SSergey Zigachev 	data->dpm_table.mclk_table.count = 0;
800b843c749SSergey Zigachev 	for (i = 0; i < dep_mclk_table->count; i++) {
801b843c749SSergey Zigachev 		if (i == 0 || data->dpm_table.mclk_table.dpm_levels
802b843c749SSergey Zigachev 				[data->dpm_table.mclk_table.count - 1].value !=
803b843c749SSergey Zigachev 						dep_mclk_table->entries[i].clk) {
804b843c749SSergey Zigachev 			data->dpm_table.mclk_table.dpm_levels[data->dpm_table.mclk_table.count].value =
805b843c749SSergey Zigachev 							dep_mclk_table->entries[i].clk;
806b843c749SSergey Zigachev 			data->dpm_table.mclk_table.dpm_levels[data->dpm_table.mclk_table.count].enabled =
807b843c749SSergey Zigachev 							(i == 0) ? true : false;
808b843c749SSergey Zigachev 			data->dpm_table.mclk_table.count++;
809b843c749SSergey Zigachev 		}
810b843c749SSergey Zigachev 	}
811b843c749SSergey Zigachev 
812b843c749SSergey Zigachev 	if (hwmgr->platform_descriptor.overdriveLimit.memoryClock == 0)
813b843c749SSergey Zigachev 		hwmgr->platform_descriptor.overdriveLimit.memoryClock = dep_mclk_table->entries[i-1].clk;
814b843c749SSergey Zigachev 	return 0;
815b843c749SSergey Zigachev }
816b843c749SSergey Zigachev 
smu7_odn_initial_default_setting(struct pp_hwmgr * hwmgr)817b843c749SSergey Zigachev static int smu7_odn_initial_default_setting(struct pp_hwmgr *hwmgr)
818b843c749SSergey Zigachev {
819b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
820b843c749SSergey Zigachev 	struct smu7_odn_dpm_table *odn_table = &(data->odn_dpm_table);
821b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
822b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
823b843c749SSergey Zigachev 	uint32_t i;
824b843c749SSergey Zigachev 
825b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_sclk_table;
826b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table;
827b843c749SSergey Zigachev 	struct phm_odn_performance_level *entries;
828b843c749SSergey Zigachev 
829b843c749SSergey Zigachev 	if (table_info == NULL)
830b843c749SSergey Zigachev 		return -EINVAL;
831b843c749SSergey Zigachev 
832b843c749SSergey Zigachev 	dep_sclk_table = table_info->vdd_dep_on_sclk;
833b843c749SSergey Zigachev 	dep_mclk_table = table_info->vdd_dep_on_mclk;
834b843c749SSergey Zigachev 
835b843c749SSergey Zigachev 	odn_table->odn_core_clock_dpm_levels.num_of_pl =
836b843c749SSergey Zigachev 						data->golden_dpm_table.sclk_table.count;
837b843c749SSergey Zigachev 	entries = odn_table->odn_core_clock_dpm_levels.entries;
838b843c749SSergey Zigachev 	for (i=0; i<data->golden_dpm_table.sclk_table.count; i++) {
839b843c749SSergey Zigachev 		entries[i].clock = data->golden_dpm_table.sclk_table.dpm_levels[i].value;
840b843c749SSergey Zigachev 		entries[i].enabled = true;
841b843c749SSergey Zigachev 		entries[i].vddc = dep_sclk_table->entries[i].vddc;
842b843c749SSergey Zigachev 	}
843b843c749SSergey Zigachev 
844b843c749SSergey Zigachev 	smu_get_voltage_dependency_table_ppt_v1(dep_sclk_table,
845b843c749SSergey Zigachev 		(struct phm_ppt_v1_clock_voltage_dependency_table *)&(odn_table->vdd_dependency_on_sclk));
846b843c749SSergey Zigachev 
847b843c749SSergey Zigachev 	odn_table->odn_memory_clock_dpm_levels.num_of_pl =
848b843c749SSergey Zigachev 						data->golden_dpm_table.mclk_table.count;
849b843c749SSergey Zigachev 	entries = odn_table->odn_memory_clock_dpm_levels.entries;
850b843c749SSergey Zigachev 	for (i=0; i<data->golden_dpm_table.mclk_table.count; i++) {
851b843c749SSergey Zigachev 		entries[i].clock = data->golden_dpm_table.mclk_table.dpm_levels[i].value;
852b843c749SSergey Zigachev 		entries[i].enabled = true;
853b843c749SSergey Zigachev 		entries[i].vddc = dep_mclk_table->entries[i].vddc;
854b843c749SSergey Zigachev 	}
855b843c749SSergey Zigachev 
856b843c749SSergey Zigachev 	smu_get_voltage_dependency_table_ppt_v1(dep_mclk_table,
857b843c749SSergey Zigachev 		(struct phm_ppt_v1_clock_voltage_dependency_table *)&(odn_table->vdd_dependency_on_mclk));
858b843c749SSergey Zigachev 
859b843c749SSergey Zigachev 	return 0;
860b843c749SSergey Zigachev }
861b843c749SSergey Zigachev 
smu7_setup_voltage_range_from_vbios(struct pp_hwmgr * hwmgr)862b843c749SSergey Zigachev static void smu7_setup_voltage_range_from_vbios(struct pp_hwmgr *hwmgr)
863b843c749SSergey Zigachev {
864b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
865b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_sclk_table;
866b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
867b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
868b843c749SSergey Zigachev 	uint32_t min_vddc = 0;
869b843c749SSergey Zigachev 	uint32_t max_vddc = 0;
870b843c749SSergey Zigachev 
871b843c749SSergey Zigachev 	if (!table_info)
872b843c749SSergey Zigachev 		return;
873b843c749SSergey Zigachev 
874b843c749SSergey Zigachev 	dep_sclk_table = table_info->vdd_dep_on_sclk;
875b843c749SSergey Zigachev 
876b843c749SSergey Zigachev 	atomctrl_get_voltage_range(hwmgr, &max_vddc, &min_vddc);
877b843c749SSergey Zigachev 
878b843c749SSergey Zigachev 	if (min_vddc == 0 || min_vddc > 2000
879b843c749SSergey Zigachev 		|| min_vddc > dep_sclk_table->entries[0].vddc)
880b843c749SSergey Zigachev 		min_vddc = dep_sclk_table->entries[0].vddc;
881b843c749SSergey Zigachev 
882b843c749SSergey Zigachev 	if (max_vddc == 0 || max_vddc > 2000
883b843c749SSergey Zigachev 		|| max_vddc < dep_sclk_table->entries[dep_sclk_table->count-1].vddc)
884b843c749SSergey Zigachev 		max_vddc = dep_sclk_table->entries[dep_sclk_table->count-1].vddc;
885b843c749SSergey Zigachev 
886b843c749SSergey Zigachev 	data->odn_dpm_table.min_vddc = min_vddc;
887b843c749SSergey Zigachev 	data->odn_dpm_table.max_vddc = max_vddc;
888b843c749SSergey Zigachev }
889b843c749SSergey Zigachev 
smu7_check_dpm_table_updated(struct pp_hwmgr * hwmgr)890b843c749SSergey Zigachev static void smu7_check_dpm_table_updated(struct pp_hwmgr *hwmgr)
891b843c749SSergey Zigachev {
892b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
893b843c749SSergey Zigachev 	struct smu7_odn_dpm_table *odn_table = &(data->odn_dpm_table);
894b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
895b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
896b843c749SSergey Zigachev 	uint32_t i;
897b843c749SSergey Zigachev 
898b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_table;
899b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *odn_dep_table;
900b843c749SSergey Zigachev 
901b843c749SSergey Zigachev 	if (table_info == NULL)
902b843c749SSergey Zigachev 		return;
903b843c749SSergey Zigachev 
904b843c749SSergey Zigachev 	for (i = 0; i < data->dpm_table.sclk_table.count; i++) {
905b843c749SSergey Zigachev 		if (odn_table->odn_core_clock_dpm_levels.entries[i].clock !=
906b843c749SSergey Zigachev 					data->dpm_table.sclk_table.dpm_levels[i].value) {
907b843c749SSergey Zigachev 			data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK;
908b843c749SSergey Zigachev 			break;
909b843c749SSergey Zigachev 		}
910b843c749SSergey Zigachev 	}
911b843c749SSergey Zigachev 
912b843c749SSergey Zigachev 	for (i = 0; i < data->dpm_table.mclk_table.count; i++) {
913b843c749SSergey Zigachev 		if (odn_table->odn_memory_clock_dpm_levels.entries[i].clock !=
914b843c749SSergey Zigachev 					data->dpm_table.mclk_table.dpm_levels[i].value) {
915b843c749SSergey Zigachev 			data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_MCLK;
916b843c749SSergey Zigachev 			break;
917b843c749SSergey Zigachev 		}
918b843c749SSergey Zigachev 	}
919b843c749SSergey Zigachev 
920b843c749SSergey Zigachev 	dep_table = table_info->vdd_dep_on_mclk;
921b843c749SSergey Zigachev 	odn_dep_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)&(odn_table->vdd_dependency_on_mclk);
922b843c749SSergey Zigachev 
923b843c749SSergey Zigachev 	for (i = 0; i < dep_table->count; i++) {
924b843c749SSergey Zigachev 		if (dep_table->entries[i].vddc != odn_dep_table->entries[i].vddc) {
925b843c749SSergey Zigachev 			data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_VDDC | DPMTABLE_OD_UPDATE_MCLK;
926b843c749SSergey Zigachev 			return;
927b843c749SSergey Zigachev 		}
928b843c749SSergey Zigachev 	}
929b843c749SSergey Zigachev 
930b843c749SSergey Zigachev 	dep_table = table_info->vdd_dep_on_sclk;
931b843c749SSergey Zigachev 	odn_dep_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)&(odn_table->vdd_dependency_on_sclk);
932b843c749SSergey Zigachev 	for (i = 0; i < dep_table->count; i++) {
933b843c749SSergey Zigachev 		if (dep_table->entries[i].vddc != odn_dep_table->entries[i].vddc) {
934b843c749SSergey Zigachev 			data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_VDDC | DPMTABLE_OD_UPDATE_SCLK;
935b843c749SSergey Zigachev 			return;
936b843c749SSergey Zigachev 		}
937b843c749SSergey Zigachev 	}
938b843c749SSergey Zigachev 	if (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_VDDC) {
939b843c749SSergey Zigachev 		data->need_update_smu7_dpm_table &= ~DPMTABLE_OD_UPDATE_VDDC;
940b843c749SSergey Zigachev 		data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_OD_UPDATE_MCLK;
941b843c749SSergey Zigachev 	}
942b843c749SSergey Zigachev }
943b843c749SSergey Zigachev 
smu7_setup_default_dpm_tables(struct pp_hwmgr * hwmgr)944b843c749SSergey Zigachev static int smu7_setup_default_dpm_tables(struct pp_hwmgr *hwmgr)
945b843c749SSergey Zigachev {
946b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
947b843c749SSergey Zigachev 
948b843c749SSergey Zigachev 	smu7_reset_dpm_tables(hwmgr);
949b843c749SSergey Zigachev 
950b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V1)
951b843c749SSergey Zigachev 		smu7_setup_dpm_tables_v1(hwmgr);
952b843c749SSergey Zigachev 	else if (hwmgr->pp_table_version == PP_TABLE_V0)
953b843c749SSergey Zigachev 		smu7_setup_dpm_tables_v0(hwmgr);
954b843c749SSergey Zigachev 
955b843c749SSergey Zigachev 	smu7_setup_default_pcie_table(hwmgr);
956b843c749SSergey Zigachev 
957b843c749SSergey Zigachev 	/* save a copy of the default DPM table */
958b843c749SSergey Zigachev 	memcpy(&(data->golden_dpm_table), &(data->dpm_table),
959b843c749SSergey Zigachev 			sizeof(struct smu7_dpm_table));
960b843c749SSergey Zigachev 
961b843c749SSergey Zigachev 	/* initialize ODN table */
962b843c749SSergey Zigachev 	if (hwmgr->od_enabled) {
963b843c749SSergey Zigachev 		if (data->odn_dpm_table.max_vddc) {
964b843c749SSergey Zigachev 			smu7_check_dpm_table_updated(hwmgr);
965b843c749SSergey Zigachev 		} else {
966b843c749SSergey Zigachev 			smu7_setup_voltage_range_from_vbios(hwmgr);
967b843c749SSergey Zigachev 			smu7_odn_initial_default_setting(hwmgr);
968b843c749SSergey Zigachev 		}
969b843c749SSergey Zigachev 	}
970b843c749SSergey Zigachev 	return 0;
971b843c749SSergey Zigachev }
972b843c749SSergey Zigachev 
smu7_enable_vrhot_gpio_interrupt(struct pp_hwmgr * hwmgr)973b843c749SSergey Zigachev static int smu7_enable_vrhot_gpio_interrupt(struct pp_hwmgr *hwmgr)
974b843c749SSergey Zigachev {
975b843c749SSergey Zigachev 
976b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
977b843c749SSergey Zigachev 			PHM_PlatformCaps_RegulatorHot))
978b843c749SSergey Zigachev 		return smum_send_msg_to_smc(hwmgr,
979b843c749SSergey Zigachev 				PPSMC_MSG_EnableVRHotGPIOInterrupt);
980b843c749SSergey Zigachev 
981b843c749SSergey Zigachev 	return 0;
982b843c749SSergey Zigachev }
983b843c749SSergey Zigachev 
smu7_enable_sclk_control(struct pp_hwmgr * hwmgr)984b843c749SSergey Zigachev static int smu7_enable_sclk_control(struct pp_hwmgr *hwmgr)
985b843c749SSergey Zigachev {
986b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
987b843c749SSergey Zigachev 			SCLK_PWRMGT_OFF, 0);
988b843c749SSergey Zigachev 	return 0;
989b843c749SSergey Zigachev }
990b843c749SSergey Zigachev 
smu7_enable_ulv(struct pp_hwmgr * hwmgr)991b843c749SSergey Zigachev static int smu7_enable_ulv(struct pp_hwmgr *hwmgr)
992b843c749SSergey Zigachev {
993b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
994b843c749SSergey Zigachev 
995b843c749SSergey Zigachev 	if (data->ulv_supported)
996b843c749SSergey Zigachev 		return smum_send_msg_to_smc(hwmgr, PPSMC_MSG_EnableULV);
997b843c749SSergey Zigachev 
998b843c749SSergey Zigachev 	return 0;
999b843c749SSergey Zigachev }
1000b843c749SSergey Zigachev 
smu7_disable_ulv(struct pp_hwmgr * hwmgr)1001b843c749SSergey Zigachev static int smu7_disable_ulv(struct pp_hwmgr *hwmgr)
1002b843c749SSergey Zigachev {
1003b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1004b843c749SSergey Zigachev 
1005b843c749SSergey Zigachev 	if (data->ulv_supported)
1006b843c749SSergey Zigachev 		return smum_send_msg_to_smc(hwmgr, PPSMC_MSG_DisableULV);
1007b843c749SSergey Zigachev 
1008b843c749SSergey Zigachev 	return 0;
1009b843c749SSergey Zigachev }
1010b843c749SSergey Zigachev 
smu7_enable_deep_sleep_master_switch(struct pp_hwmgr * hwmgr)1011b843c749SSergey Zigachev static int smu7_enable_deep_sleep_master_switch(struct pp_hwmgr *hwmgr)
1012b843c749SSergey Zigachev {
1013b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1014b843c749SSergey Zigachev 			PHM_PlatformCaps_SclkDeepSleep)) {
1015b843c749SSergey Zigachev 		if (smum_send_msg_to_smc(hwmgr, PPSMC_MSG_MASTER_DeepSleep_ON))
1016b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
1017b843c749SSergey Zigachev 					"Attempt to enable Master Deep Sleep switch failed!",
1018b843c749SSergey Zigachev 					return -EINVAL);
1019b843c749SSergey Zigachev 	} else {
1020b843c749SSergey Zigachev 		if (smum_send_msg_to_smc(hwmgr,
1021b843c749SSergey Zigachev 				PPSMC_MSG_MASTER_DeepSleep_OFF)) {
1022b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
1023b843c749SSergey Zigachev 					"Attempt to disable Master Deep Sleep switch failed!",
1024b843c749SSergey Zigachev 					return -EINVAL);
1025b843c749SSergey Zigachev 		}
1026b843c749SSergey Zigachev 	}
1027b843c749SSergey Zigachev 
1028b843c749SSergey Zigachev 	return 0;
1029b843c749SSergey Zigachev }
1030b843c749SSergey Zigachev 
smu7_disable_deep_sleep_master_switch(struct pp_hwmgr * hwmgr)1031b843c749SSergey Zigachev static int smu7_disable_deep_sleep_master_switch(struct pp_hwmgr *hwmgr)
1032b843c749SSergey Zigachev {
1033b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1034b843c749SSergey Zigachev 			PHM_PlatformCaps_SclkDeepSleep)) {
1035b843c749SSergey Zigachev 		if (smum_send_msg_to_smc(hwmgr,
1036b843c749SSergey Zigachev 				PPSMC_MSG_MASTER_DeepSleep_OFF)) {
1037b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
1038b843c749SSergey Zigachev 					"Attempt to disable Master Deep Sleep switch failed!",
1039b843c749SSergey Zigachev 					return -EINVAL);
1040b843c749SSergey Zigachev 		}
1041b843c749SSergey Zigachev 	}
1042b843c749SSergey Zigachev 
1043b843c749SSergey Zigachev 	return 0;
1044b843c749SSergey Zigachev }
1045b843c749SSergey Zigachev 
smu7_disable_sclk_vce_handshake(struct pp_hwmgr * hwmgr)1046b843c749SSergey Zigachev static int smu7_disable_sclk_vce_handshake(struct pp_hwmgr *hwmgr)
1047b843c749SSergey Zigachev {
1048b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1049b843c749SSergey Zigachev 	uint32_t soft_register_value = 0;
1050b843c749SSergey Zigachev 	uint32_t handshake_disables_offset = data->soft_regs_start
1051b843c749SSergey Zigachev 				+ smum_get_offsetof(hwmgr,
1052b843c749SSergey Zigachev 					SMU_SoftRegisters, HandshakeDisables);
1053b843c749SSergey Zigachev 
1054b843c749SSergey Zigachev 	soft_register_value = cgs_read_ind_register(hwmgr->device,
1055b843c749SSergey Zigachev 				CGS_IND_REG__SMC, handshake_disables_offset);
1056b843c749SSergey Zigachev 	soft_register_value |= SMU7_VCE_SCLK_HANDSHAKE_DISABLE;
1057b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1058b843c749SSergey Zigachev 			handshake_disables_offset, soft_register_value);
1059b843c749SSergey Zigachev 	return 0;
1060b843c749SSergey Zigachev }
1061b843c749SSergey Zigachev 
smu7_disable_handshake_uvd(struct pp_hwmgr * hwmgr)1062b843c749SSergey Zigachev static int smu7_disable_handshake_uvd(struct pp_hwmgr *hwmgr)
1063b843c749SSergey Zigachev {
1064b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1065b843c749SSergey Zigachev 	uint32_t soft_register_value = 0;
1066b843c749SSergey Zigachev 	uint32_t handshake_disables_offset = data->soft_regs_start
1067b843c749SSergey Zigachev 				+ smum_get_offsetof(hwmgr,
1068b843c749SSergey Zigachev 					SMU_SoftRegisters, HandshakeDisables);
1069b843c749SSergey Zigachev 
1070b843c749SSergey Zigachev 	soft_register_value = cgs_read_ind_register(hwmgr->device,
1071b843c749SSergey Zigachev 				CGS_IND_REG__SMC, handshake_disables_offset);
1072b843c749SSergey Zigachev 	soft_register_value |= smum_get_mac_definition(hwmgr,
1073b843c749SSergey Zigachev 					SMU_UVD_MCLK_HANDSHAKE_DISABLE);
1074b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1075b843c749SSergey Zigachev 			handshake_disables_offset, soft_register_value);
1076b843c749SSergey Zigachev 	return 0;
1077b843c749SSergey Zigachev }
1078b843c749SSergey Zigachev 
smu7_enable_sclk_mclk_dpm(struct pp_hwmgr * hwmgr)1079b843c749SSergey Zigachev static int smu7_enable_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
1080b843c749SSergey Zigachev {
1081b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1082b843c749SSergey Zigachev 
1083b843c749SSergey Zigachev 	/* enable SCLK dpm */
1084b843c749SSergey Zigachev 	if (!data->sclk_dpm_key_disabled) {
1085b843c749SSergey Zigachev 		if (hwmgr->chip_id == CHIP_VEGAM)
1086b843c749SSergey Zigachev 			smu7_disable_sclk_vce_handshake(hwmgr);
1087b843c749SSergey Zigachev 
1088b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(
1089b843c749SSergey Zigachev 		(0 == smum_send_msg_to_smc(hwmgr, PPSMC_MSG_DPM_Enable)),
1090b843c749SSergey Zigachev 		"Failed to enable SCLK DPM during DPM Start Function!",
1091b843c749SSergey Zigachev 		return -EINVAL);
1092b843c749SSergey Zigachev 	}
1093b843c749SSergey Zigachev 
1094b843c749SSergey Zigachev 	/* enable MCLK dpm */
1095b843c749SSergey Zigachev 	if (0 == data->mclk_dpm_key_disabled) {
1096b843c749SSergey Zigachev 		if (!(hwmgr->feature_mask & PP_UVD_HANDSHAKE_MASK))
1097b843c749SSergey Zigachev 			smu7_disable_handshake_uvd(hwmgr);
1098b843c749SSergey Zigachev 
1099b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(
1100b843c749SSergey Zigachev 				(0 == smum_send_msg_to_smc(hwmgr,
1101b843c749SSergey Zigachev 						PPSMC_MSG_MCLKDPM_Enable)),
1102b843c749SSergey Zigachev 				"Failed to enable MCLK DPM during DPM Start Function!",
1103b843c749SSergey Zigachev 				return -EINVAL);
1104b843c749SSergey Zigachev 
1105b843c749SSergey Zigachev 		if (hwmgr->chip_family != CHIP_VEGAM)
1106b843c749SSergey Zigachev 			PHM_WRITE_FIELD(hwmgr->device, MC_SEQ_CNTL_3, CAC_EN, 0x1);
1107b843c749SSergey Zigachev 
1108b843c749SSergey Zigachev 
1109b843c749SSergey Zigachev 		if (hwmgr->chip_family == AMDGPU_FAMILY_CI) {
1110b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, 0xc0400d30, 0x5);
1111b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, 0xc0400d3c, 0x5);
1112b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, 0xc0400d80, 0x100005);
1113b843c749SSergey Zigachev 			udelay(10);
1114b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, 0xc0400d30, 0x400005);
1115b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, 0xc0400d3c, 0x400005);
1116b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, 0xc0400d80, 0x500005);
1117b843c749SSergey Zigachev 		} else {
1118b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC0_CNTL, 0x5);
1119b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC1_CNTL, 0x5);
1120b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_CPL_CNTL, 0x100005);
1121b843c749SSergey Zigachev 			udelay(10);
1122b843c749SSergey Zigachev 			if (hwmgr->chip_id == CHIP_VEGAM) {
1123b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC0_CNTL, 0x400009);
1124b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC1_CNTL, 0x400009);
1125b843c749SSergey Zigachev 			} else {
1126b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC0_CNTL, 0x400005);
1127b843c749SSergey Zigachev 				cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC1_CNTL, 0x400005);
1128b843c749SSergey Zigachev 			}
1129b843c749SSergey Zigachev 			cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_CPL_CNTL, 0x500005);
1130b843c749SSergey Zigachev 		}
1131b843c749SSergey Zigachev 	}
1132b843c749SSergey Zigachev 
1133b843c749SSergey Zigachev 	return 0;
1134b843c749SSergey Zigachev }
1135b843c749SSergey Zigachev 
smu7_start_dpm(struct pp_hwmgr * hwmgr)1136b843c749SSergey Zigachev static int smu7_start_dpm(struct pp_hwmgr *hwmgr)
1137b843c749SSergey Zigachev {
1138b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1139b843c749SSergey Zigachev 
1140b843c749SSergey Zigachev 	/*enable general power management */
1141b843c749SSergey Zigachev 
1142b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
1143b843c749SSergey Zigachev 			GLOBAL_PWRMGT_EN, 1);
1144b843c749SSergey Zigachev 
1145b843c749SSergey Zigachev 	/* enable sclk deep sleep */
1146b843c749SSergey Zigachev 
1147b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
1148b843c749SSergey Zigachev 			DYNAMIC_PM_EN, 1);
1149b843c749SSergey Zigachev 
1150b843c749SSergey Zigachev 	/* prepare for PCIE DPM */
1151b843c749SSergey Zigachev 
1152b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1153b843c749SSergey Zigachev 			data->soft_regs_start +
1154b843c749SSergey Zigachev 			smum_get_offsetof(hwmgr, SMU_SoftRegisters,
1155b843c749SSergey Zigachev 						VoltageChangeTimeout), 0x1000);
1156b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__PCIE,
1157b843c749SSergey Zigachev 			SWRST_COMMAND_1, RESETLC, 0x0);
1158b843c749SSergey Zigachev 
1159b843c749SSergey Zigachev 	if (hwmgr->chip_family == AMDGPU_FAMILY_CI)
1160b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, 0x1488,
1161b843c749SSergey Zigachev 			(cgs_read_register(hwmgr->device, 0x1488) & ~0x1));
1162b843c749SSergey Zigachev 
1163b843c749SSergey Zigachev 	if (smu7_enable_sclk_mclk_dpm(hwmgr)) {
1164b843c749SSergey Zigachev 		pr_err("Failed to enable Sclk DPM and Mclk DPM!");
1165b843c749SSergey Zigachev 		return -EINVAL;
1166b843c749SSergey Zigachev 	}
1167b843c749SSergey Zigachev 
1168b843c749SSergey Zigachev 	/* enable PCIE dpm */
1169b843c749SSergey Zigachev 	if (0 == data->pcie_dpm_key_disabled) {
1170b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(
1171b843c749SSergey Zigachev 				(0 == smum_send_msg_to_smc(hwmgr,
1172b843c749SSergey Zigachev 						PPSMC_MSG_PCIeDPM_Enable)),
1173b843c749SSergey Zigachev 				"Failed to enable pcie DPM during DPM Start Function!",
1174b843c749SSergey Zigachev 				return -EINVAL);
1175b843c749SSergey Zigachev 	}
1176b843c749SSergey Zigachev 
1177b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1178b843c749SSergey Zigachev 				PHM_PlatformCaps_Falcon_QuickTransition)) {
1179b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == smum_send_msg_to_smc(hwmgr,
1180b843c749SSergey Zigachev 				PPSMC_MSG_EnableACDCGPIOInterrupt)),
1181b843c749SSergey Zigachev 				"Failed to enable AC DC GPIO Interrupt!",
1182b843c749SSergey Zigachev 				);
1183b843c749SSergey Zigachev 	}
1184b843c749SSergey Zigachev 
1185b843c749SSergey Zigachev 	return 0;
1186b843c749SSergey Zigachev }
1187b843c749SSergey Zigachev 
smu7_disable_sclk_mclk_dpm(struct pp_hwmgr * hwmgr)1188b843c749SSergey Zigachev static int smu7_disable_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
1189b843c749SSergey Zigachev {
1190b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1191b843c749SSergey Zigachev 
1192b843c749SSergey Zigachev 	/* disable SCLK dpm */
1193b843c749SSergey Zigachev 	if (!data->sclk_dpm_key_disabled) {
1194b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(true == smum_is_dpm_running(hwmgr),
1195b843c749SSergey Zigachev 				"Trying to disable SCLK DPM when DPM is disabled",
1196b843c749SSergey Zigachev 				return 0);
1197b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_DPM_Disable);
1198b843c749SSergey Zigachev 	}
1199b843c749SSergey Zigachev 
1200b843c749SSergey Zigachev 	/* disable MCLK dpm */
1201b843c749SSergey Zigachev 	if (!data->mclk_dpm_key_disabled) {
1202b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(true == smum_is_dpm_running(hwmgr),
1203b843c749SSergey Zigachev 				"Trying to disable MCLK DPM when DPM is disabled",
1204b843c749SSergey Zigachev 				return 0);
1205b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_MCLKDPM_Disable);
1206b843c749SSergey Zigachev 	}
1207b843c749SSergey Zigachev 
1208b843c749SSergey Zigachev 	return 0;
1209b843c749SSergey Zigachev }
1210b843c749SSergey Zigachev 
smu7_stop_dpm(struct pp_hwmgr * hwmgr)1211b843c749SSergey Zigachev static int smu7_stop_dpm(struct pp_hwmgr *hwmgr)
1212b843c749SSergey Zigachev {
1213b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1214b843c749SSergey Zigachev 
1215b843c749SSergey Zigachev 	/* disable general power management */
1216b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
1217b843c749SSergey Zigachev 			GLOBAL_PWRMGT_EN, 0);
1218b843c749SSergey Zigachev 	/* disable sclk deep sleep */
1219b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
1220b843c749SSergey Zigachev 			DYNAMIC_PM_EN, 0);
1221b843c749SSergey Zigachev 
1222b843c749SSergey Zigachev 	/* disable PCIE dpm */
1223b843c749SSergey Zigachev 	if (!data->pcie_dpm_key_disabled) {
1224b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(
1225b843c749SSergey Zigachev 				(smum_send_msg_to_smc(hwmgr,
1226b843c749SSergey Zigachev 						PPSMC_MSG_PCIeDPM_Disable) == 0),
1227b843c749SSergey Zigachev 				"Failed to disable pcie DPM during DPM Stop Function!",
1228b843c749SSergey Zigachev 				return -EINVAL);
1229b843c749SSergey Zigachev 	}
1230b843c749SSergey Zigachev 
1231b843c749SSergey Zigachev 	smu7_disable_sclk_mclk_dpm(hwmgr);
1232b843c749SSergey Zigachev 
1233b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(true == smum_is_dpm_running(hwmgr),
1234b843c749SSergey Zigachev 			"Trying to disable voltage DPM when DPM is disabled",
1235b843c749SSergey Zigachev 			return 0);
1236b843c749SSergey Zigachev 
1237b843c749SSergey Zigachev 	smum_send_msg_to_smc(hwmgr, PPSMC_MSG_Voltage_Cntl_Disable);
1238b843c749SSergey Zigachev 
1239b843c749SSergey Zigachev 	return 0;
1240b843c749SSergey Zigachev }
1241b843c749SSergey Zigachev 
smu7_set_dpm_event_sources(struct pp_hwmgr * hwmgr,uint32_t sources)1242b843c749SSergey Zigachev static void smu7_set_dpm_event_sources(struct pp_hwmgr *hwmgr, uint32_t sources)
1243b843c749SSergey Zigachev {
1244b843c749SSergey Zigachev 	bool protection;
1245b843c749SSergey Zigachev 	enum DPM_EVENT_SRC src;
1246b843c749SSergey Zigachev 
1247b843c749SSergey Zigachev 	switch (sources) {
1248b843c749SSergey Zigachev 	default:
1249b843c749SSergey Zigachev 		pr_err("Unknown throttling event sources.");
1250b843c749SSergey Zigachev 		/* fall through */
1251b843c749SSergey Zigachev 	case 0:
1252b843c749SSergey Zigachev 		protection = false;
1253b843c749SSergey Zigachev 		/* src is unused */
1254b843c749SSergey Zigachev 		break;
1255b843c749SSergey Zigachev 	case (1 << PHM_AutoThrottleSource_Thermal):
1256b843c749SSergey Zigachev 		protection = true;
1257b843c749SSergey Zigachev 		src = DPM_EVENT_SRC_DIGITAL;
1258b843c749SSergey Zigachev 		break;
1259b843c749SSergey Zigachev 	case (1 << PHM_AutoThrottleSource_External):
1260b843c749SSergey Zigachev 		protection = true;
1261b843c749SSergey Zigachev 		src = DPM_EVENT_SRC_EXTERNAL;
1262b843c749SSergey Zigachev 		break;
1263b843c749SSergey Zigachev 	case (1 << PHM_AutoThrottleSource_External) |
1264b843c749SSergey Zigachev 			(1 << PHM_AutoThrottleSource_Thermal):
1265b843c749SSergey Zigachev 		protection = true;
1266b843c749SSergey Zigachev 		src = DPM_EVENT_SRC_DIGITAL_OR_EXTERNAL;
1267b843c749SSergey Zigachev 		break;
1268b843c749SSergey Zigachev 	}
1269b843c749SSergey Zigachev 	/* Order matters - don't enable thermal protection for the wrong source. */
1270b843c749SSergey Zigachev 	if (protection) {
1271b843c749SSergey Zigachev 		PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_THERMAL_CTRL,
1272b843c749SSergey Zigachev 				DPM_EVENT_SRC, src);
1273b843c749SSergey Zigachev 		PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
1274b843c749SSergey Zigachev 				THERMAL_PROTECTION_DIS,
1275b843c749SSergey Zigachev 				!phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1276b843c749SSergey Zigachev 						PHM_PlatformCaps_ThermalController));
1277b843c749SSergey Zigachev 	} else
1278b843c749SSergey Zigachev 		PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
1279b843c749SSergey Zigachev 				THERMAL_PROTECTION_DIS, 1);
1280b843c749SSergey Zigachev }
1281b843c749SSergey Zigachev 
smu7_enable_auto_throttle_source(struct pp_hwmgr * hwmgr,PHM_AutoThrottleSource source)1282b843c749SSergey Zigachev static int smu7_enable_auto_throttle_source(struct pp_hwmgr *hwmgr,
1283b843c749SSergey Zigachev 		PHM_AutoThrottleSource source)
1284b843c749SSergey Zigachev {
1285b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1286b843c749SSergey Zigachev 
1287b843c749SSergey Zigachev 	if (!(data->active_auto_throttle_sources & (1 << source))) {
1288b843c749SSergey Zigachev 		data->active_auto_throttle_sources |= 1 << source;
1289b843c749SSergey Zigachev 		smu7_set_dpm_event_sources(hwmgr, data->active_auto_throttle_sources);
1290b843c749SSergey Zigachev 	}
1291b843c749SSergey Zigachev 	return 0;
1292b843c749SSergey Zigachev }
1293b843c749SSergey Zigachev 
smu7_enable_thermal_auto_throttle(struct pp_hwmgr * hwmgr)1294b843c749SSergey Zigachev static int smu7_enable_thermal_auto_throttle(struct pp_hwmgr *hwmgr)
1295b843c749SSergey Zigachev {
1296b843c749SSergey Zigachev 	return smu7_enable_auto_throttle_source(hwmgr, PHM_AutoThrottleSource_Thermal);
1297b843c749SSergey Zigachev }
1298b843c749SSergey Zigachev 
smu7_disable_auto_throttle_source(struct pp_hwmgr * hwmgr,PHM_AutoThrottleSource source)1299b843c749SSergey Zigachev static int smu7_disable_auto_throttle_source(struct pp_hwmgr *hwmgr,
1300b843c749SSergey Zigachev 		PHM_AutoThrottleSource source)
1301b843c749SSergey Zigachev {
1302b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1303b843c749SSergey Zigachev 
1304b843c749SSergey Zigachev 	if (data->active_auto_throttle_sources & (1 << source)) {
1305b843c749SSergey Zigachev 		data->active_auto_throttle_sources &= ~(1 << source);
1306b843c749SSergey Zigachev 		smu7_set_dpm_event_sources(hwmgr, data->active_auto_throttle_sources);
1307b843c749SSergey Zigachev 	}
1308b843c749SSergey Zigachev 	return 0;
1309b843c749SSergey Zigachev }
1310b843c749SSergey Zigachev 
smu7_disable_thermal_auto_throttle(struct pp_hwmgr * hwmgr)1311b843c749SSergey Zigachev static int smu7_disable_thermal_auto_throttle(struct pp_hwmgr *hwmgr)
1312b843c749SSergey Zigachev {
1313b843c749SSergey Zigachev 	return smu7_disable_auto_throttle_source(hwmgr, PHM_AutoThrottleSource_Thermal);
1314b843c749SSergey Zigachev }
1315b843c749SSergey Zigachev 
smu7_pcie_performance_request(struct pp_hwmgr * hwmgr)1316b843c749SSergey Zigachev static int smu7_pcie_performance_request(struct pp_hwmgr *hwmgr)
1317b843c749SSergey Zigachev {
1318b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1319b843c749SSergey Zigachev 	data->pcie_performance_request = true;
1320b843c749SSergey Zigachev 
1321b843c749SSergey Zigachev 	return 0;
1322b843c749SSergey Zigachev }
1323b843c749SSergey Zigachev 
smu7_enable_dpm_tasks(struct pp_hwmgr * hwmgr)1324b843c749SSergey Zigachev static int smu7_enable_dpm_tasks(struct pp_hwmgr *hwmgr)
1325b843c749SSergey Zigachev {
1326b843c749SSergey Zigachev 	int tmp_result = 0;
1327b843c749SSergey Zigachev 	int result = 0;
1328b843c749SSergey Zigachev 
1329b843c749SSergey Zigachev 	if (smu7_voltage_control(hwmgr)) {
1330b843c749SSergey Zigachev 		tmp_result = smu7_enable_voltage_control(hwmgr);
1331b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(tmp_result == 0,
1332b843c749SSergey Zigachev 				"Failed to enable voltage control!",
1333b843c749SSergey Zigachev 				result = tmp_result);
1334b843c749SSergey Zigachev 
1335b843c749SSergey Zigachev 		tmp_result = smu7_construct_voltage_tables(hwmgr);
1336b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == tmp_result),
1337b843c749SSergey Zigachev 				"Failed to construct voltage tables!",
1338b843c749SSergey Zigachev 				result = tmp_result);
1339b843c749SSergey Zigachev 	}
1340b843c749SSergey Zigachev 	smum_initialize_mc_reg_table(hwmgr);
1341b843c749SSergey Zigachev 
1342b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1343b843c749SSergey Zigachev 			PHM_PlatformCaps_EngineSpreadSpectrumSupport))
1344b843c749SSergey Zigachev 		PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1345b843c749SSergey Zigachev 				GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, 1);
1346b843c749SSergey Zigachev 
1347b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1348b843c749SSergey Zigachev 			PHM_PlatformCaps_ThermalController))
1349b843c749SSergey Zigachev 		PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1350b843c749SSergey Zigachev 				GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, 0);
1351b843c749SSergey Zigachev 
1352b843c749SSergey Zigachev 	tmp_result = smu7_program_static_screen_threshold_parameters(hwmgr);
1353b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1354b843c749SSergey Zigachev 			"Failed to program static screen threshold parameters!",
1355b843c749SSergey Zigachev 			result = tmp_result);
1356b843c749SSergey Zigachev 
1357b843c749SSergey Zigachev 	tmp_result = smu7_enable_display_gap(hwmgr);
1358b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1359b843c749SSergey Zigachev 			"Failed to enable display gap!", result = tmp_result);
1360b843c749SSergey Zigachev 
1361b843c749SSergey Zigachev 	tmp_result = smu7_program_voting_clients(hwmgr);
1362b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1363b843c749SSergey Zigachev 			"Failed to program voting clients!", result = tmp_result);
1364b843c749SSergey Zigachev 
1365b843c749SSergey Zigachev 	tmp_result = smum_process_firmware_header(hwmgr);
1366b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1367b843c749SSergey Zigachev 			"Failed to process firmware header!", result = tmp_result);
1368b843c749SSergey Zigachev 
1369b843c749SSergey Zigachev 	if (hwmgr->chip_id != CHIP_VEGAM) {
1370b843c749SSergey Zigachev 		tmp_result = smu7_initial_switch_from_arbf0_to_f1(hwmgr);
1371b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == tmp_result),
1372b843c749SSergey Zigachev 				"Failed to initialize switch from ArbF0 to F1!",
1373b843c749SSergey Zigachev 				result = tmp_result);
1374b843c749SSergey Zigachev 	}
1375b843c749SSergey Zigachev 
1376b843c749SSergey Zigachev 	result = smu7_setup_default_dpm_tables(hwmgr);
1377b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1378b843c749SSergey Zigachev 			"Failed to setup default DPM tables!", return result);
1379b843c749SSergey Zigachev 
1380b843c749SSergey Zigachev 	tmp_result = smum_init_smc_table(hwmgr);
1381b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1382b843c749SSergey Zigachev 			"Failed to initialize SMC table!", result = tmp_result);
1383b843c749SSergey Zigachev 
1384b843c749SSergey Zigachev 	tmp_result = smu7_enable_vrhot_gpio_interrupt(hwmgr);
1385b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1386b843c749SSergey Zigachev 			"Failed to enable VR hot GPIO interrupt!", result = tmp_result);
1387b843c749SSergey Zigachev 
1388b843c749SSergey Zigachev 	smum_send_msg_to_smc(hwmgr, (PPSMC_Msg)PPSMC_NoDisplay);
1389b843c749SSergey Zigachev 
1390b843c749SSergey Zigachev 	tmp_result = smu7_enable_sclk_control(hwmgr);
1391b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1392b843c749SSergey Zigachev 			"Failed to enable SCLK control!", result = tmp_result);
1393b843c749SSergey Zigachev 
1394b843c749SSergey Zigachev 	tmp_result = smu7_enable_smc_voltage_controller(hwmgr);
1395b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1396b843c749SSergey Zigachev 			"Failed to enable voltage control!", result = tmp_result);
1397b843c749SSergey Zigachev 
1398b843c749SSergey Zigachev 	tmp_result = smu7_enable_ulv(hwmgr);
1399b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1400b843c749SSergey Zigachev 			"Failed to enable ULV!", result = tmp_result);
1401b843c749SSergey Zigachev 
1402b843c749SSergey Zigachev 	tmp_result = smu7_enable_deep_sleep_master_switch(hwmgr);
1403b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1404b843c749SSergey Zigachev 			"Failed to enable deep sleep master switch!", result = tmp_result);
1405b843c749SSergey Zigachev 
1406b843c749SSergey Zigachev 	tmp_result = smu7_enable_didt_config(hwmgr);
1407b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1408b843c749SSergey Zigachev 			"Failed to enable deep sleep master switch!", result = tmp_result);
1409b843c749SSergey Zigachev 
1410b843c749SSergey Zigachev 	tmp_result = smu7_start_dpm(hwmgr);
1411b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1412b843c749SSergey Zigachev 			"Failed to start DPM!", result = tmp_result);
1413b843c749SSergey Zigachev 
1414b843c749SSergey Zigachev 	tmp_result = smu7_enable_smc_cac(hwmgr);
1415b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1416b843c749SSergey Zigachev 			"Failed to enable SMC CAC!", result = tmp_result);
1417b843c749SSergey Zigachev 
1418b843c749SSergey Zigachev 	tmp_result = smu7_enable_power_containment(hwmgr);
1419b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1420b843c749SSergey Zigachev 			"Failed to enable power containment!", result = tmp_result);
1421b843c749SSergey Zigachev 
1422b843c749SSergey Zigachev 	tmp_result = smu7_power_control_set_level(hwmgr);
1423b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1424b843c749SSergey Zigachev 			"Failed to power control set level!", result = tmp_result);
1425b843c749SSergey Zigachev 
1426b843c749SSergey Zigachev 	tmp_result = smu7_enable_thermal_auto_throttle(hwmgr);
1427b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1428b843c749SSergey Zigachev 			"Failed to enable thermal auto throttle!", result = tmp_result);
1429b843c749SSergey Zigachev 
1430b843c749SSergey Zigachev 	tmp_result = smu7_pcie_performance_request(hwmgr);
1431b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
1432b843c749SSergey Zigachev 			"pcie performance request failed!", result = tmp_result);
1433b843c749SSergey Zigachev 
1434b843c749SSergey Zigachev 	return 0;
1435b843c749SSergey Zigachev }
1436b843c749SSergey Zigachev 
smu7_avfs_control(struct pp_hwmgr * hwmgr,bool enable)1437b843c749SSergey Zigachev static int smu7_avfs_control(struct pp_hwmgr *hwmgr, bool enable)
1438b843c749SSergey Zigachev {
1439b843c749SSergey Zigachev 	if (!hwmgr->avfs_supported)
1440b843c749SSergey Zigachev 		return 0;
1441b843c749SSergey Zigachev 
1442b843c749SSergey Zigachev 	if (enable) {
1443b843c749SSergey Zigachev 		if (!PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device,
1444b843c749SSergey Zigachev 				CGS_IND_REG__SMC, FEATURE_STATUS, AVS_ON)) {
1445b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(!smum_send_msg_to_smc(
1446b843c749SSergey Zigachev 					hwmgr, PPSMC_MSG_EnableAvfs),
1447b843c749SSergey Zigachev 					"Failed to enable AVFS!",
1448b843c749SSergey Zigachev 					return -EINVAL);
1449b843c749SSergey Zigachev 		}
1450b843c749SSergey Zigachev 	} else if (PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device,
1451b843c749SSergey Zigachev 			CGS_IND_REG__SMC, FEATURE_STATUS, AVS_ON)) {
1452b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(!smum_send_msg_to_smc(
1453b843c749SSergey Zigachev 				hwmgr, PPSMC_MSG_DisableAvfs),
1454b843c749SSergey Zigachev 				"Failed to disable AVFS!",
1455b843c749SSergey Zigachev 				return -EINVAL);
1456b843c749SSergey Zigachev 	}
1457b843c749SSergey Zigachev 
1458b843c749SSergey Zigachev 	return 0;
1459b843c749SSergey Zigachev }
1460b843c749SSergey Zigachev 
smu7_update_avfs(struct pp_hwmgr * hwmgr)1461b843c749SSergey Zigachev static int smu7_update_avfs(struct pp_hwmgr *hwmgr)
1462b843c749SSergey Zigachev {
1463b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1464b843c749SSergey Zigachev 
1465b843c749SSergey Zigachev 	if (!hwmgr->avfs_supported)
1466b843c749SSergey Zigachev 		return 0;
1467b843c749SSergey Zigachev 
1468b843c749SSergey Zigachev 	if (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_VDDC) {
1469b843c749SSergey Zigachev 		smu7_avfs_control(hwmgr, false);
1470b843c749SSergey Zigachev 	} else if (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_SCLK) {
1471b843c749SSergey Zigachev 		smu7_avfs_control(hwmgr, false);
1472b843c749SSergey Zigachev 		smu7_avfs_control(hwmgr, true);
1473b843c749SSergey Zigachev 	} else {
1474b843c749SSergey Zigachev 		smu7_avfs_control(hwmgr, true);
1475b843c749SSergey Zigachev 	}
1476b843c749SSergey Zigachev 
1477b843c749SSergey Zigachev 	return 0;
1478b843c749SSergey Zigachev }
1479b843c749SSergey Zigachev 
1480*78973132SSergey Zigachev int smu7_disable_dpm_tasks(struct pp_hwmgr *hwmgr);
smu7_disable_dpm_tasks(struct pp_hwmgr * hwmgr)1481b843c749SSergey Zigachev int smu7_disable_dpm_tasks(struct pp_hwmgr *hwmgr)
1482b843c749SSergey Zigachev {
1483b843c749SSergey Zigachev 	int tmp_result, result = 0;
1484b843c749SSergey Zigachev 
1485b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1486b843c749SSergey Zigachev 			PHM_PlatformCaps_ThermalController))
1487b843c749SSergey Zigachev 		PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1488b843c749SSergey Zigachev 				GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, 1);
1489b843c749SSergey Zigachev 
1490b843c749SSergey Zigachev 	tmp_result = smu7_disable_power_containment(hwmgr);
1491b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1492b843c749SSergey Zigachev 			"Failed to disable power containment!", result = tmp_result);
1493b843c749SSergey Zigachev 
1494b843c749SSergey Zigachev 	tmp_result = smu7_disable_smc_cac(hwmgr);
1495b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1496b843c749SSergey Zigachev 			"Failed to disable SMC CAC!", result = tmp_result);
1497b843c749SSergey Zigachev 
1498b843c749SSergey Zigachev 	tmp_result = smu7_disable_didt_config(hwmgr);
1499b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1500b843c749SSergey Zigachev 			"Failed to disable DIDT!", result = tmp_result);
1501b843c749SSergey Zigachev 
1502b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1503b843c749SSergey Zigachev 			CG_SPLL_SPREAD_SPECTRUM, SSEN, 0);
1504b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1505b843c749SSergey Zigachev 			GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, 0);
1506b843c749SSergey Zigachev 
1507b843c749SSergey Zigachev 	tmp_result = smu7_disable_thermal_auto_throttle(hwmgr);
1508b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1509b843c749SSergey Zigachev 			"Failed to disable thermal auto throttle!", result = tmp_result);
1510b843c749SSergey Zigachev 
1511b843c749SSergey Zigachev 	tmp_result = smu7_avfs_control(hwmgr, false);
1512b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1513b843c749SSergey Zigachev 			"Failed to disable AVFS!", result = tmp_result);
1514b843c749SSergey Zigachev 
1515b843c749SSergey Zigachev 	tmp_result = smu7_stop_dpm(hwmgr);
1516b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1517b843c749SSergey Zigachev 			"Failed to stop DPM!", result = tmp_result);
1518b843c749SSergey Zigachev 
1519b843c749SSergey Zigachev 	tmp_result = smu7_disable_deep_sleep_master_switch(hwmgr);
1520b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1521b843c749SSergey Zigachev 			"Failed to disable deep sleep master switch!", result = tmp_result);
1522b843c749SSergey Zigachev 
1523b843c749SSergey Zigachev 	tmp_result = smu7_disable_ulv(hwmgr);
1524b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1525b843c749SSergey Zigachev 			"Failed to disable ULV!", result = tmp_result);
1526b843c749SSergey Zigachev 
1527b843c749SSergey Zigachev 	tmp_result = smu7_clear_voting_clients(hwmgr);
1528b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1529b843c749SSergey Zigachev 			"Failed to clear voting clients!", result = tmp_result);
1530b843c749SSergey Zigachev 
1531b843c749SSergey Zigachev 	tmp_result = smu7_reset_to_default(hwmgr);
1532b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1533b843c749SSergey Zigachev 			"Failed to reset to default!", result = tmp_result);
1534b843c749SSergey Zigachev 
1535b843c749SSergey Zigachev 	tmp_result = smum_stop_smc(hwmgr);
1536b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1537b843c749SSergey Zigachev 			"Failed to stop smc!", result = tmp_result);
1538b843c749SSergey Zigachev 
1539b843c749SSergey Zigachev 	tmp_result = smu7_force_switch_to_arbf0(hwmgr);
1540b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((tmp_result == 0),
1541b843c749SSergey Zigachev 			"Failed to force to switch arbf0!", result = tmp_result);
1542b843c749SSergey Zigachev 
1543b843c749SSergey Zigachev 	return result;
1544b843c749SSergey Zigachev }
1545b843c749SSergey Zigachev 
1546*78973132SSergey Zigachev int smu7_reset_asic_tasks(struct pp_hwmgr *hwmgr);
smu7_reset_asic_tasks(struct pp_hwmgr * hwmgr)1547b843c749SSergey Zigachev int smu7_reset_asic_tasks(struct pp_hwmgr *hwmgr)
1548b843c749SSergey Zigachev {
1549b843c749SSergey Zigachev 
1550b843c749SSergey Zigachev 	return 0;
1551b843c749SSergey Zigachev }
1552b843c749SSergey Zigachev 
smu7_init_dpm_defaults(struct pp_hwmgr * hwmgr)1553b843c749SSergey Zigachev static void smu7_init_dpm_defaults(struct pp_hwmgr *hwmgr)
1554b843c749SSergey Zigachev {
1555b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1556b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1557b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1558b843c749SSergey Zigachev 	struct amdgpu_device *adev = hwmgr->adev;
1559b843c749SSergey Zigachev 
1560b843c749SSergey Zigachev 	data->dll_default_on = false;
1561b843c749SSergey Zigachev 	data->mclk_dpm0_activity_target = 0xa;
1562b843c749SSergey Zigachev 	data->vddc_vddgfx_delta = 300;
1563b843c749SSergey Zigachev 	data->static_screen_threshold = SMU7_STATICSCREENTHRESHOLD_DFLT;
1564b843c749SSergey Zigachev 	data->static_screen_threshold_unit = SMU7_STATICSCREENTHRESHOLDUNIT_DFLT;
1565b843c749SSergey Zigachev 	data->voting_rights_clients[0] = SMU7_VOTINGRIGHTSCLIENTS_DFLT0;
1566b843c749SSergey Zigachev 	data->voting_rights_clients[1]= SMU7_VOTINGRIGHTSCLIENTS_DFLT1;
1567b843c749SSergey Zigachev 	data->voting_rights_clients[2] = SMU7_VOTINGRIGHTSCLIENTS_DFLT2;
1568b843c749SSergey Zigachev 	data->voting_rights_clients[3]= SMU7_VOTINGRIGHTSCLIENTS_DFLT3;
1569b843c749SSergey Zigachev 	data->voting_rights_clients[4]= SMU7_VOTINGRIGHTSCLIENTS_DFLT4;
1570b843c749SSergey Zigachev 	data->voting_rights_clients[5]= SMU7_VOTINGRIGHTSCLIENTS_DFLT5;
1571b843c749SSergey Zigachev 	data->voting_rights_clients[6]= SMU7_VOTINGRIGHTSCLIENTS_DFLT6;
1572b843c749SSergey Zigachev 	data->voting_rights_clients[7]= SMU7_VOTINGRIGHTSCLIENTS_DFLT7;
1573b843c749SSergey Zigachev 
1574b843c749SSergey Zigachev 	data->mclk_dpm_key_disabled = hwmgr->feature_mask & PP_MCLK_DPM_MASK ? false : true;
1575b843c749SSergey Zigachev 	data->sclk_dpm_key_disabled = hwmgr->feature_mask & PP_SCLK_DPM_MASK ? false : true;
1576b843c749SSergey Zigachev 	data->pcie_dpm_key_disabled = hwmgr->feature_mask & PP_PCIE_DPM_MASK ? false : true;
1577b843c749SSergey Zigachev 	/* need to set voltage control types before EVV patching */
1578b843c749SSergey Zigachev 	data->voltage_control = SMU7_VOLTAGE_CONTROL_NONE;
1579b843c749SSergey Zigachev 	data->vddci_control = SMU7_VOLTAGE_CONTROL_NONE;
1580b843c749SSergey Zigachev 	data->mvdd_control = SMU7_VOLTAGE_CONTROL_NONE;
1581b843c749SSergey Zigachev 	data->enable_tdc_limit_feature = true;
1582b843c749SSergey Zigachev 	data->enable_pkg_pwr_tracking_feature = true;
1583b843c749SSergey Zigachev 	data->force_pcie_gen = PP_PCIEGenInvalid;
1584b843c749SSergey Zigachev 	data->ulv_supported = hwmgr->feature_mask & PP_ULV_MASK ? true : false;
1585b843c749SSergey Zigachev 	data->current_profile_setting.bupdate_sclk = 1;
1586b843c749SSergey Zigachev 	data->current_profile_setting.sclk_up_hyst = 0;
1587b843c749SSergey Zigachev 	data->current_profile_setting.sclk_down_hyst = 100;
1588b843c749SSergey Zigachev 	data->current_profile_setting.sclk_activity = SMU7_SCLK_TARGETACTIVITY_DFLT;
1589b843c749SSergey Zigachev 	data->current_profile_setting.bupdate_mclk = 1;
1590b843c749SSergey Zigachev 	data->current_profile_setting.mclk_up_hyst = 0;
1591b843c749SSergey Zigachev 	data->current_profile_setting.mclk_down_hyst = 100;
1592b843c749SSergey Zigachev 	data->current_profile_setting.mclk_activity = SMU7_MCLK_TARGETACTIVITY_DFLT;
1593b843c749SSergey Zigachev 	hwmgr->workload_mask = 1 << hwmgr->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D];
1594b843c749SSergey Zigachev 	hwmgr->power_profile_mode = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1595b843c749SSergey Zigachev 	hwmgr->default_power_profile_mode = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1596b843c749SSergey Zigachev 
1597b843c749SSergey Zigachev 	if (hwmgr->chip_id == CHIP_POLARIS12 || hwmgr->is_kicker) {
1598b843c749SSergey Zigachev 		uint8_t tmp1, tmp2;
1599b843c749SSergey Zigachev 		uint16_t tmp3 = 0;
1600b843c749SSergey Zigachev 		atomctrl_get_svi2_info(hwmgr, VOLTAGE_TYPE_VDDC, &tmp1, &tmp2,
1601b843c749SSergey Zigachev 						&tmp3);
1602b843c749SSergey Zigachev 		tmp3 = (tmp3 >> 5) & 0x3;
1603b843c749SSergey Zigachev 		data->vddc_phase_shed_control = ((tmp3 << 1) | (tmp3 >> 1)) & 0x3;
1604b843c749SSergey Zigachev 	} else if (hwmgr->chip_family == AMDGPU_FAMILY_CI) {
1605b843c749SSergey Zigachev 		data->vddc_phase_shed_control = 1;
1606b843c749SSergey Zigachev 	} else {
1607b843c749SSergey Zigachev 		data->vddc_phase_shed_control = 0;
1608b843c749SSergey Zigachev 	}
1609b843c749SSergey Zigachev 
1610b843c749SSergey Zigachev 	if (hwmgr->chip_id  == CHIP_HAWAII) {
1611b843c749SSergey Zigachev 		data->thermal_temp_setting.temperature_low = 94500;
1612b843c749SSergey Zigachev 		data->thermal_temp_setting.temperature_high = 95000;
1613b843c749SSergey Zigachev 		data->thermal_temp_setting.temperature_shutdown = 104000;
1614b843c749SSergey Zigachev 	} else {
1615b843c749SSergey Zigachev 		data->thermal_temp_setting.temperature_low = 99500;
1616b843c749SSergey Zigachev 		data->thermal_temp_setting.temperature_high = 100000;
1617b843c749SSergey Zigachev 		data->thermal_temp_setting.temperature_shutdown = 104000;
1618b843c749SSergey Zigachev 	}
1619b843c749SSergey Zigachev 
1620b843c749SSergey Zigachev 	data->fast_watermark_threshold = 100;
1621b843c749SSergey Zigachev 	if (atomctrl_is_voltage_controlled_by_gpio_v3(hwmgr,
1622b843c749SSergey Zigachev 			VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_SVID2))
1623b843c749SSergey Zigachev 		data->voltage_control = SMU7_VOLTAGE_CONTROL_BY_SVID2;
1624b843c749SSergey Zigachev 	else if (atomctrl_is_voltage_controlled_by_gpio_v3(hwmgr,
1625b843c749SSergey Zigachev 			VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_GPIO_LUT))
1626b843c749SSergey Zigachev 		data->voltage_control = SMU7_VOLTAGE_CONTROL_BY_GPIO;
1627b843c749SSergey Zigachev 
1628b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1629b843c749SSergey Zigachev 			PHM_PlatformCaps_ControlVDDGFX)) {
1630b843c749SSergey Zigachev 		if (atomctrl_is_voltage_controlled_by_gpio_v3(hwmgr,
1631b843c749SSergey Zigachev 			VOLTAGE_TYPE_VDDGFX, VOLTAGE_OBJ_SVID2)) {
1632b843c749SSergey Zigachev 			data->vdd_gfx_control = SMU7_VOLTAGE_CONTROL_BY_SVID2;
1633b843c749SSergey Zigachev 		}
1634b843c749SSergey Zigachev 	}
1635b843c749SSergey Zigachev 
1636b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1637b843c749SSergey Zigachev 			PHM_PlatformCaps_EnableMVDDControl)) {
1638b843c749SSergey Zigachev 		if (atomctrl_is_voltage_controlled_by_gpio_v3(hwmgr,
1639b843c749SSergey Zigachev 				VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT))
1640b843c749SSergey Zigachev 			data->mvdd_control = SMU7_VOLTAGE_CONTROL_BY_GPIO;
1641b843c749SSergey Zigachev 		else if (atomctrl_is_voltage_controlled_by_gpio_v3(hwmgr,
1642b843c749SSergey Zigachev 				VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_SVID2))
1643b843c749SSergey Zigachev 			data->mvdd_control = SMU7_VOLTAGE_CONTROL_BY_SVID2;
1644b843c749SSergey Zigachev 	}
1645b843c749SSergey Zigachev 
1646b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE == data->vdd_gfx_control)
1647b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1648b843c749SSergey Zigachev 			PHM_PlatformCaps_ControlVDDGFX);
1649b843c749SSergey Zigachev 
1650b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1651b843c749SSergey Zigachev 			PHM_PlatformCaps_ControlVDDCI)) {
1652b843c749SSergey Zigachev 		if (atomctrl_is_voltage_controlled_by_gpio_v3(hwmgr,
1653b843c749SSergey Zigachev 				VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT))
1654b843c749SSergey Zigachev 			data->vddci_control = SMU7_VOLTAGE_CONTROL_BY_GPIO;
1655b843c749SSergey Zigachev 		else if (atomctrl_is_voltage_controlled_by_gpio_v3(hwmgr,
1656b843c749SSergey Zigachev 				VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_SVID2))
1657b843c749SSergey Zigachev 			data->vddci_control = SMU7_VOLTAGE_CONTROL_BY_SVID2;
1658b843c749SSergey Zigachev 	}
1659b843c749SSergey Zigachev 
1660b843c749SSergey Zigachev 	if (data->mvdd_control == SMU7_VOLTAGE_CONTROL_NONE)
1661b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1662b843c749SSergey Zigachev 				PHM_PlatformCaps_EnableMVDDControl);
1663b843c749SSergey Zigachev 
1664b843c749SSergey Zigachev 	if (data->vddci_control == SMU7_VOLTAGE_CONTROL_NONE)
1665b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1666b843c749SSergey Zigachev 				PHM_PlatformCaps_ControlVDDCI);
1667b843c749SSergey Zigachev 
1668b843c749SSergey Zigachev 	if ((hwmgr->pp_table_version != PP_TABLE_V0) && (hwmgr->feature_mask & PP_CLOCK_STRETCH_MASK)
1669b843c749SSergey Zigachev 		&& (table_info->cac_dtp_table->usClockStretchAmount != 0))
1670b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
1671b843c749SSergey Zigachev 					PHM_PlatformCaps_ClockStretcher);
1672b843c749SSergey Zigachev 
1673b843c749SSergey Zigachev 	data->pcie_gen_performance.max = PP_PCIEGen1;
1674b843c749SSergey Zigachev 	data->pcie_gen_performance.min = PP_PCIEGen3;
1675b843c749SSergey Zigachev 	data->pcie_gen_power_saving.max = PP_PCIEGen1;
1676b843c749SSergey Zigachev 	data->pcie_gen_power_saving.min = PP_PCIEGen3;
1677b843c749SSergey Zigachev 	data->pcie_lane_performance.max = 0;
1678b843c749SSergey Zigachev 	data->pcie_lane_performance.min = 16;
1679b843c749SSergey Zigachev 	data->pcie_lane_power_saving.max = 0;
1680b843c749SSergey Zigachev 	data->pcie_lane_power_saving.min = 16;
1681b843c749SSergey Zigachev 
1682b843c749SSergey Zigachev 
1683b843c749SSergey Zigachev 	if (adev->pg_flags & AMD_PG_SUPPORT_UVD)
1684b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
1685b843c749SSergey Zigachev 			      PHM_PlatformCaps_UVDPowerGating);
1686b843c749SSergey Zigachev 	if (adev->pg_flags & AMD_PG_SUPPORT_VCE)
1687b843c749SSergey Zigachev 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
1688b843c749SSergey Zigachev 			      PHM_PlatformCaps_VCEPowerGating);
1689b843c749SSergey Zigachev }
1690b843c749SSergey Zigachev 
1691b843c749SSergey Zigachev /**
1692b843c749SSergey Zigachev * Get Leakage VDDC based on leakage ID.
1693b843c749SSergey Zigachev *
1694b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
1695b843c749SSergey Zigachev * @return   always 0
1696b843c749SSergey Zigachev */
smu7_get_evv_voltages(struct pp_hwmgr * hwmgr)1697b843c749SSergey Zigachev static int smu7_get_evv_voltages(struct pp_hwmgr *hwmgr)
1698b843c749SSergey Zigachev {
1699b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1700b843c749SSergey Zigachev 	uint16_t vv_id;
1701b843c749SSergey Zigachev 	uint16_t vddc = 0;
1702b843c749SSergey Zigachev 	uint16_t vddgfx = 0;
1703b843c749SSergey Zigachev 	uint16_t i, j;
1704b843c749SSergey Zigachev 	uint32_t sclk = 0;
1705b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1706b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)hwmgr->pptable;
1707b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table = NULL;
1708b843c749SSergey Zigachev 
1709b843c749SSergey Zigachev 
1710b843c749SSergey Zigachev 	for (i = 0; i < SMU7_MAX_LEAKAGE_COUNT; i++) {
1711b843c749SSergey Zigachev 		vv_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
1712b843c749SSergey Zigachev 
1713b843c749SSergey Zigachev 		if (data->vdd_gfx_control == SMU7_VOLTAGE_CONTROL_BY_SVID2) {
1714b843c749SSergey Zigachev 			if ((hwmgr->pp_table_version == PP_TABLE_V1)
1715b843c749SSergey Zigachev 			    && !phm_get_sclk_for_voltage_evv(hwmgr,
1716b843c749SSergey Zigachev 						table_info->vddgfx_lookup_table, vv_id, &sclk)) {
1717b843c749SSergey Zigachev 				if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1718b843c749SSergey Zigachev 							PHM_PlatformCaps_ClockStretcher)) {
1719b843c749SSergey Zigachev 					sclk_table = table_info->vdd_dep_on_sclk;
1720b843c749SSergey Zigachev 
1721b843c749SSergey Zigachev 					for (j = 1; j < sclk_table->count; j++) {
1722b843c749SSergey Zigachev 						if (sclk_table->entries[j].clk == sclk &&
1723b843c749SSergey Zigachev 								sclk_table->entries[j].cks_enable == 0) {
1724b843c749SSergey Zigachev 							sclk += 5000;
1725b843c749SSergey Zigachev 							break;
1726b843c749SSergey Zigachev 						}
1727b843c749SSergey Zigachev 					}
1728b843c749SSergey Zigachev 				}
1729b843c749SSergey Zigachev 				if (0 == atomctrl_get_voltage_evv_on_sclk
1730b843c749SSergey Zigachev 				    (hwmgr, VOLTAGE_TYPE_VDDGFX, sclk,
1731b843c749SSergey Zigachev 				     vv_id, &vddgfx)) {
1732b843c749SSergey Zigachev 					/* need to make sure vddgfx is less than 2v or else, it could burn the ASIC. */
1733b843c749SSergey Zigachev 					PP_ASSERT_WITH_CODE((vddgfx < 2000 && vddgfx != 0), "Invalid VDDGFX value!", return -EINVAL);
1734b843c749SSergey Zigachev 
1735b843c749SSergey Zigachev 					/* the voltage should not be zero nor equal to leakage ID */
1736b843c749SSergey Zigachev 					if (vddgfx != 0 && vddgfx != vv_id) {
1737b843c749SSergey Zigachev 						data->vddcgfx_leakage.actual_voltage[data->vddcgfx_leakage.count] = vddgfx;
1738b843c749SSergey Zigachev 						data->vddcgfx_leakage.leakage_id[data->vddcgfx_leakage.count] = vv_id;
1739b843c749SSergey Zigachev 						data->vddcgfx_leakage.count++;
1740b843c749SSergey Zigachev 					}
1741b843c749SSergey Zigachev 				} else {
1742b843c749SSergey Zigachev 					pr_info("Error retrieving EVV voltage value!\n");
1743b843c749SSergey Zigachev 				}
1744b843c749SSergey Zigachev 			}
1745b843c749SSergey Zigachev 		} else {
1746b843c749SSergey Zigachev 			if ((hwmgr->pp_table_version == PP_TABLE_V0)
1747b843c749SSergey Zigachev 				|| !phm_get_sclk_for_voltage_evv(hwmgr,
1748b843c749SSergey Zigachev 					table_info->vddc_lookup_table, vv_id, &sclk)) {
1749b843c749SSergey Zigachev 				if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1750b843c749SSergey Zigachev 						PHM_PlatformCaps_ClockStretcher)) {
1751b843c749SSergey Zigachev 					if (table_info == NULL)
1752b843c749SSergey Zigachev 						return -EINVAL;
1753b843c749SSergey Zigachev 					sclk_table = table_info->vdd_dep_on_sclk;
1754b843c749SSergey Zigachev 
1755b843c749SSergey Zigachev 					for (j = 1; j < sclk_table->count; j++) {
1756b843c749SSergey Zigachev 						if (sclk_table->entries[j].clk == sclk &&
1757b843c749SSergey Zigachev 								sclk_table->entries[j].cks_enable == 0) {
1758b843c749SSergey Zigachev 							sclk += 5000;
1759b843c749SSergey Zigachev 							break;
1760b843c749SSergey Zigachev 						}
1761b843c749SSergey Zigachev 					}
1762b843c749SSergey Zigachev 				}
1763b843c749SSergey Zigachev 
1764b843c749SSergey Zigachev 				if (phm_get_voltage_evv_on_sclk(hwmgr,
1765b843c749SSergey Zigachev 							VOLTAGE_TYPE_VDDC,
1766b843c749SSergey Zigachev 							sclk, vv_id, &vddc) == 0) {
1767b843c749SSergey Zigachev 					if (vddc >= 2000 || vddc == 0)
1768b843c749SSergey Zigachev 						return -EINVAL;
1769b843c749SSergey Zigachev 				} else {
1770b843c749SSergey Zigachev 					pr_debug("failed to retrieving EVV voltage!\n");
1771b843c749SSergey Zigachev 					continue;
1772b843c749SSergey Zigachev 				}
1773b843c749SSergey Zigachev 
1774b843c749SSergey Zigachev 				/* the voltage should not be zero nor equal to leakage ID */
1775b843c749SSergey Zigachev 				if (vddc != 0 && vddc != vv_id) {
1776b843c749SSergey Zigachev 					data->vddc_leakage.actual_voltage[data->vddc_leakage.count] = (uint16_t)(vddc);
1777b843c749SSergey Zigachev 					data->vddc_leakage.leakage_id[data->vddc_leakage.count] = vv_id;
1778b843c749SSergey Zigachev 					data->vddc_leakage.count++;
1779b843c749SSergey Zigachev 				}
1780b843c749SSergey Zigachev 			}
1781b843c749SSergey Zigachev 		}
1782b843c749SSergey Zigachev 	}
1783b843c749SSergey Zigachev 
1784b843c749SSergey Zigachev 	return 0;
1785b843c749SSergey Zigachev }
1786b843c749SSergey Zigachev 
1787b843c749SSergey Zigachev /**
1788b843c749SSergey Zigachev  * Change virtual leakage voltage to actual value.
1789b843c749SSergey Zigachev  *
1790b843c749SSergey Zigachev  * @param     hwmgr  the address of the powerplay hardware manager.
1791b843c749SSergey Zigachev  * @param     pointer to changing voltage
1792b843c749SSergey Zigachev  * @param     pointer to leakage table
1793b843c749SSergey Zigachev  */
smu7_patch_ppt_v1_with_vdd_leakage(struct pp_hwmgr * hwmgr,uint16_t * voltage,struct smu7_leakage_voltage * leakage_table)1794b843c749SSergey Zigachev static void smu7_patch_ppt_v1_with_vdd_leakage(struct pp_hwmgr *hwmgr,
1795b843c749SSergey Zigachev 		uint16_t *voltage, struct smu7_leakage_voltage *leakage_table)
1796b843c749SSergey Zigachev {
1797b843c749SSergey Zigachev 	uint32_t index;
1798b843c749SSergey Zigachev 
1799b843c749SSergey Zigachev 	/* search for leakage voltage ID 0xff01 ~ 0xff08 */
1800b843c749SSergey Zigachev 	for (index = 0; index < leakage_table->count; index++) {
1801b843c749SSergey Zigachev 		/* if this voltage matches a leakage voltage ID */
1802b843c749SSergey Zigachev 		/* patch with actual leakage voltage */
1803b843c749SSergey Zigachev 		if (leakage_table->leakage_id[index] == *voltage) {
1804b843c749SSergey Zigachev 			*voltage = leakage_table->actual_voltage[index];
1805b843c749SSergey Zigachev 			break;
1806b843c749SSergey Zigachev 		}
1807b843c749SSergey Zigachev 	}
1808b843c749SSergey Zigachev 
1809b843c749SSergey Zigachev 	if (*voltage > ATOM_VIRTUAL_VOLTAGE_ID0)
1810b843c749SSergey Zigachev 		pr_err("Voltage value looks like a Leakage ID but it's not patched \n");
1811b843c749SSergey Zigachev }
1812b843c749SSergey Zigachev 
1813b843c749SSergey Zigachev /**
1814b843c749SSergey Zigachev * Patch voltage lookup table by EVV leakages.
1815b843c749SSergey Zigachev *
1816b843c749SSergey Zigachev * @param     hwmgr  the address of the powerplay hardware manager.
1817b843c749SSergey Zigachev * @param     pointer to voltage lookup table
1818b843c749SSergey Zigachev * @param     pointer to leakage table
1819b843c749SSergey Zigachev * @return     always 0
1820b843c749SSergey Zigachev */
smu7_patch_lookup_table_with_leakage(struct pp_hwmgr * hwmgr,phm_ppt_v1_voltage_lookup_table * lookup_table,struct smu7_leakage_voltage * leakage_table)1821b843c749SSergey Zigachev static int smu7_patch_lookup_table_with_leakage(struct pp_hwmgr *hwmgr,
1822b843c749SSergey Zigachev 		phm_ppt_v1_voltage_lookup_table *lookup_table,
1823b843c749SSergey Zigachev 		struct smu7_leakage_voltage *leakage_table)
1824b843c749SSergey Zigachev {
1825b843c749SSergey Zigachev 	uint32_t i;
1826b843c749SSergey Zigachev 
1827b843c749SSergey Zigachev 	for (i = 0; i < lookup_table->count; i++)
1828b843c749SSergey Zigachev 		smu7_patch_ppt_v1_with_vdd_leakage(hwmgr,
1829b843c749SSergey Zigachev 				&lookup_table->entries[i].us_vdd, leakage_table);
1830b843c749SSergey Zigachev 
1831b843c749SSergey Zigachev 	return 0;
1832b843c749SSergey Zigachev }
1833b843c749SSergey Zigachev 
smu7_patch_clock_voltage_limits_with_vddc_leakage(struct pp_hwmgr * hwmgr,struct smu7_leakage_voltage * leakage_table,uint16_t * vddc)1834b843c749SSergey Zigachev static int smu7_patch_clock_voltage_limits_with_vddc_leakage(
1835b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr, struct smu7_leakage_voltage *leakage_table,
1836b843c749SSergey Zigachev 		uint16_t *vddc)
1837b843c749SSergey Zigachev {
1838b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1839b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1840b843c749SSergey Zigachev 	smu7_patch_ppt_v1_with_vdd_leakage(hwmgr, (uint16_t *)vddc, leakage_table);
1841b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_dc.vddc =
1842b843c749SSergey Zigachev 			table_info->max_clock_voltage_on_dc.vddc;
1843b843c749SSergey Zigachev 	return 0;
1844b843c749SSergey Zigachev }
1845b843c749SSergey Zigachev 
smu7_patch_voltage_dependency_tables_with_lookup_table(struct pp_hwmgr * hwmgr)1846b843c749SSergey Zigachev static int smu7_patch_voltage_dependency_tables_with_lookup_table(
1847b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr)
1848b843c749SSergey Zigachev {
1849b843c749SSergey Zigachev 	uint8_t entry_id;
1850b843c749SSergey Zigachev 	uint8_t voltage_id;
1851b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1852b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
1853b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
1854b843c749SSergey Zigachev 
1855b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
1856b843c749SSergey Zigachev 			table_info->vdd_dep_on_sclk;
1857b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *mclk_table =
1858b843c749SSergey Zigachev 			table_info->vdd_dep_on_mclk;
1859b843c749SSergey Zigachev 	struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1860b843c749SSergey Zigachev 			table_info->mm_dep_table;
1861b843c749SSergey Zigachev 
1862b843c749SSergey Zigachev 	if (data->vdd_gfx_control == SMU7_VOLTAGE_CONTROL_BY_SVID2) {
1863b843c749SSergey Zigachev 		for (entry_id = 0; entry_id < sclk_table->count; ++entry_id) {
1864b843c749SSergey Zigachev 			voltage_id = sclk_table->entries[entry_id].vddInd;
1865b843c749SSergey Zigachev 			sclk_table->entries[entry_id].vddgfx =
1866b843c749SSergey Zigachev 				table_info->vddgfx_lookup_table->entries[voltage_id].us_vdd;
1867b843c749SSergey Zigachev 		}
1868b843c749SSergey Zigachev 	} else {
1869b843c749SSergey Zigachev 		for (entry_id = 0; entry_id < sclk_table->count; ++entry_id) {
1870b843c749SSergey Zigachev 			voltage_id = sclk_table->entries[entry_id].vddInd;
1871b843c749SSergey Zigachev 			sclk_table->entries[entry_id].vddc =
1872b843c749SSergey Zigachev 				table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
1873b843c749SSergey Zigachev 		}
1874b843c749SSergey Zigachev 	}
1875b843c749SSergey Zigachev 
1876b843c749SSergey Zigachev 	for (entry_id = 0; entry_id < mclk_table->count; ++entry_id) {
1877b843c749SSergey Zigachev 		voltage_id = mclk_table->entries[entry_id].vddInd;
1878b843c749SSergey Zigachev 		mclk_table->entries[entry_id].vddc =
1879b843c749SSergey Zigachev 			table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
1880b843c749SSergey Zigachev 	}
1881b843c749SSergey Zigachev 
1882b843c749SSergey Zigachev 	for (entry_id = 0; entry_id < mm_table->count; ++entry_id) {
1883b843c749SSergey Zigachev 		voltage_id = mm_table->entries[entry_id].vddcInd;
1884b843c749SSergey Zigachev 		mm_table->entries[entry_id].vddc =
1885b843c749SSergey Zigachev 			table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
1886b843c749SSergey Zigachev 	}
1887b843c749SSergey Zigachev 
1888b843c749SSergey Zigachev 	return 0;
1889b843c749SSergey Zigachev 
1890b843c749SSergey Zigachev }
1891b843c749SSergey Zigachev 
phm_add_voltage(struct pp_hwmgr * hwmgr,phm_ppt_v1_voltage_lookup_table * look_up_table,phm_ppt_v1_voltage_lookup_record * record)1892b843c749SSergey Zigachev static int phm_add_voltage(struct pp_hwmgr *hwmgr,
1893b843c749SSergey Zigachev 			phm_ppt_v1_voltage_lookup_table *look_up_table,
1894b843c749SSergey Zigachev 			phm_ppt_v1_voltage_lookup_record *record)
1895b843c749SSergey Zigachev {
1896b843c749SSergey Zigachev 	uint32_t i;
1897b843c749SSergey Zigachev 
1898b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((NULL != look_up_table),
1899b843c749SSergey Zigachev 		"Lookup Table empty.", return -EINVAL);
1900b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 != look_up_table->count),
1901b843c749SSergey Zigachev 		"Lookup Table empty.", return -EINVAL);
1902b843c749SSergey Zigachev 
1903b843c749SSergey Zigachev 	i = smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_VDDGFX);
1904b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((i >= look_up_table->count),
1905b843c749SSergey Zigachev 		"Lookup Table is full.", return -EINVAL);
1906b843c749SSergey Zigachev 
1907b843c749SSergey Zigachev 	/* This is to avoid entering duplicate calculated records. */
1908b843c749SSergey Zigachev 	for (i = 0; i < look_up_table->count; i++) {
1909b843c749SSergey Zigachev 		if (look_up_table->entries[i].us_vdd == record->us_vdd) {
1910b843c749SSergey Zigachev 			if (look_up_table->entries[i].us_calculated == 1)
1911b843c749SSergey Zigachev 				return 0;
1912b843c749SSergey Zigachev 			break;
1913b843c749SSergey Zigachev 		}
1914b843c749SSergey Zigachev 	}
1915b843c749SSergey Zigachev 
1916b843c749SSergey Zigachev 	look_up_table->entries[i].us_calculated = 1;
1917b843c749SSergey Zigachev 	look_up_table->entries[i].us_vdd = record->us_vdd;
1918b843c749SSergey Zigachev 	look_up_table->entries[i].us_cac_low = record->us_cac_low;
1919b843c749SSergey Zigachev 	look_up_table->entries[i].us_cac_mid = record->us_cac_mid;
1920b843c749SSergey Zigachev 	look_up_table->entries[i].us_cac_high = record->us_cac_high;
1921b843c749SSergey Zigachev 	/* Only increment the count when we're appending, not replacing duplicate entry. */
1922b843c749SSergey Zigachev 	if (i == look_up_table->count)
1923b843c749SSergey Zigachev 		look_up_table->count++;
1924b843c749SSergey Zigachev 
1925b843c749SSergey Zigachev 	return 0;
1926b843c749SSergey Zigachev }
1927b843c749SSergey Zigachev 
1928b843c749SSergey Zigachev 
smu7_calc_voltage_dependency_tables(struct pp_hwmgr * hwmgr)1929b843c749SSergey Zigachev static int smu7_calc_voltage_dependency_tables(struct pp_hwmgr *hwmgr)
1930b843c749SSergey Zigachev {
1931b843c749SSergey Zigachev 	uint8_t entry_id;
1932b843c749SSergey Zigachev 	struct phm_ppt_v1_voltage_lookup_record v_record;
1933b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1934b843c749SSergey Zigachev 	struct phm_ppt_v1_information *pptable_info = (struct phm_ppt_v1_information *)(hwmgr->pptable);
1935b843c749SSergey Zigachev 
1936b843c749SSergey Zigachev 	phm_ppt_v1_clock_voltage_dependency_table *sclk_table = pptable_info->vdd_dep_on_sclk;
1937b843c749SSergey Zigachev 	phm_ppt_v1_clock_voltage_dependency_table *mclk_table = pptable_info->vdd_dep_on_mclk;
1938b843c749SSergey Zigachev 
1939b843c749SSergey Zigachev 	if (data->vdd_gfx_control == SMU7_VOLTAGE_CONTROL_BY_SVID2) {
1940b843c749SSergey Zigachev 		for (entry_id = 0; entry_id < sclk_table->count; ++entry_id) {
1941b843c749SSergey Zigachev 			if (sclk_table->entries[entry_id].vdd_offset & (1 << 15))
1942b843c749SSergey Zigachev 				v_record.us_vdd = sclk_table->entries[entry_id].vddgfx +
1943b843c749SSergey Zigachev 					sclk_table->entries[entry_id].vdd_offset - 0xFFFF;
1944b843c749SSergey Zigachev 			else
1945b843c749SSergey Zigachev 				v_record.us_vdd = sclk_table->entries[entry_id].vddgfx +
1946b843c749SSergey Zigachev 					sclk_table->entries[entry_id].vdd_offset;
1947b843c749SSergey Zigachev 
1948b843c749SSergey Zigachev 			sclk_table->entries[entry_id].vddc =
1949b843c749SSergey Zigachev 				v_record.us_cac_low = v_record.us_cac_mid =
1950b843c749SSergey Zigachev 				v_record.us_cac_high = v_record.us_vdd;
1951b843c749SSergey Zigachev 
1952b843c749SSergey Zigachev 			phm_add_voltage(hwmgr, pptable_info->vddc_lookup_table, &v_record);
1953b843c749SSergey Zigachev 		}
1954b843c749SSergey Zigachev 
1955b843c749SSergey Zigachev 		for (entry_id = 0; entry_id < mclk_table->count; ++entry_id) {
1956b843c749SSergey Zigachev 			if (mclk_table->entries[entry_id].vdd_offset & (1 << 15))
1957b843c749SSergey Zigachev 				v_record.us_vdd = mclk_table->entries[entry_id].vddc +
1958b843c749SSergey Zigachev 					mclk_table->entries[entry_id].vdd_offset - 0xFFFF;
1959b843c749SSergey Zigachev 			else
1960b843c749SSergey Zigachev 				v_record.us_vdd = mclk_table->entries[entry_id].vddc +
1961b843c749SSergey Zigachev 					mclk_table->entries[entry_id].vdd_offset;
1962b843c749SSergey Zigachev 
1963b843c749SSergey Zigachev 			mclk_table->entries[entry_id].vddgfx = v_record.us_cac_low =
1964b843c749SSergey Zigachev 				v_record.us_cac_mid = v_record.us_cac_high = v_record.us_vdd;
1965b843c749SSergey Zigachev 			phm_add_voltage(hwmgr, pptable_info->vddgfx_lookup_table, &v_record);
1966b843c749SSergey Zigachev 		}
1967b843c749SSergey Zigachev 	}
1968b843c749SSergey Zigachev 	return 0;
1969b843c749SSergey Zigachev }
1970b843c749SSergey Zigachev 
smu7_calc_mm_voltage_dependency_table(struct pp_hwmgr * hwmgr)1971b843c749SSergey Zigachev static int smu7_calc_mm_voltage_dependency_table(struct pp_hwmgr *hwmgr)
1972b843c749SSergey Zigachev {
1973b843c749SSergey Zigachev 	uint8_t entry_id;
1974b843c749SSergey Zigachev 	struct phm_ppt_v1_voltage_lookup_record v_record;
1975b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1976b843c749SSergey Zigachev 	struct phm_ppt_v1_information *pptable_info = (struct phm_ppt_v1_information *)(hwmgr->pptable);
1977b843c749SSergey Zigachev 	phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table = pptable_info->mm_dep_table;
1978b843c749SSergey Zigachev 
1979b843c749SSergey Zigachev 	if (data->vdd_gfx_control == SMU7_VOLTAGE_CONTROL_BY_SVID2) {
1980b843c749SSergey Zigachev 		for (entry_id = 0; entry_id < mm_table->count; entry_id++) {
1981b843c749SSergey Zigachev 			if (mm_table->entries[entry_id].vddgfx_offset & (1 << 15))
1982b843c749SSergey Zigachev 				v_record.us_vdd = mm_table->entries[entry_id].vddc +
1983b843c749SSergey Zigachev 					mm_table->entries[entry_id].vddgfx_offset - 0xFFFF;
1984b843c749SSergey Zigachev 			else
1985b843c749SSergey Zigachev 				v_record.us_vdd = mm_table->entries[entry_id].vddc +
1986b843c749SSergey Zigachev 					mm_table->entries[entry_id].vddgfx_offset;
1987b843c749SSergey Zigachev 
1988b843c749SSergey Zigachev 			/* Add the calculated VDDGFX to the VDDGFX lookup table */
1989b843c749SSergey Zigachev 			mm_table->entries[entry_id].vddgfx = v_record.us_cac_low =
1990b843c749SSergey Zigachev 				v_record.us_cac_mid = v_record.us_cac_high = v_record.us_vdd;
1991b843c749SSergey Zigachev 			phm_add_voltage(hwmgr, pptable_info->vddgfx_lookup_table, &v_record);
1992b843c749SSergey Zigachev 		}
1993b843c749SSergey Zigachev 	}
1994b843c749SSergey Zigachev 	return 0;
1995b843c749SSergey Zigachev }
1996b843c749SSergey Zigachev 
smu7_sort_lookup_table(struct pp_hwmgr * hwmgr,struct phm_ppt_v1_voltage_lookup_table * lookup_table)1997b843c749SSergey Zigachev static int smu7_sort_lookup_table(struct pp_hwmgr *hwmgr,
1998b843c749SSergey Zigachev 		struct phm_ppt_v1_voltage_lookup_table *lookup_table)
1999b843c749SSergey Zigachev {
2000b843c749SSergey Zigachev 	uint32_t table_size, i, j;
2001b843c749SSergey Zigachev 	struct phm_ppt_v1_voltage_lookup_record tmp_voltage_lookup_record;
2002b843c749SSergey Zigachev 	table_size = lookup_table->count;
2003b843c749SSergey Zigachev 
2004b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 != lookup_table->count,
2005b843c749SSergey Zigachev 		"Lookup table is empty", return -EINVAL);
2006b843c749SSergey Zigachev 
2007b843c749SSergey Zigachev 	/* Sorting voltages */
2008b843c749SSergey Zigachev 	for (i = 0; i < table_size - 1; i++) {
2009b843c749SSergey Zigachev 		for (j = i + 1; j > 0; j--) {
2010b843c749SSergey Zigachev 			if (lookup_table->entries[j].us_vdd <
2011b843c749SSergey Zigachev 					lookup_table->entries[j - 1].us_vdd) {
2012b843c749SSergey Zigachev 				tmp_voltage_lookup_record = lookup_table->entries[j - 1];
2013b843c749SSergey Zigachev 				lookup_table->entries[j - 1] = lookup_table->entries[j];
2014b843c749SSergey Zigachev 				lookup_table->entries[j] = tmp_voltage_lookup_record;
2015b843c749SSergey Zigachev 			}
2016b843c749SSergey Zigachev 		}
2017b843c749SSergey Zigachev 	}
2018b843c749SSergey Zigachev 
2019b843c749SSergey Zigachev 	return 0;
2020b843c749SSergey Zigachev }
2021b843c749SSergey Zigachev 
smu7_complete_dependency_tables(struct pp_hwmgr * hwmgr)2022b843c749SSergey Zigachev static int smu7_complete_dependency_tables(struct pp_hwmgr *hwmgr)
2023b843c749SSergey Zigachev {
2024b843c749SSergey Zigachev 	int result = 0;
2025b843c749SSergey Zigachev 	int tmp_result;
2026b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2027b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2028b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2029b843c749SSergey Zigachev 
2030b843c749SSergey Zigachev 	if (data->vdd_gfx_control == SMU7_VOLTAGE_CONTROL_BY_SVID2) {
2031b843c749SSergey Zigachev 		tmp_result = smu7_patch_lookup_table_with_leakage(hwmgr,
2032b843c749SSergey Zigachev 			table_info->vddgfx_lookup_table, &(data->vddcgfx_leakage));
2033b843c749SSergey Zigachev 		if (tmp_result != 0)
2034b843c749SSergey Zigachev 			result = tmp_result;
2035b843c749SSergey Zigachev 
2036b843c749SSergey Zigachev 		smu7_patch_ppt_v1_with_vdd_leakage(hwmgr,
2037b843c749SSergey Zigachev 			&table_info->max_clock_voltage_on_dc.vddgfx, &(data->vddcgfx_leakage));
2038b843c749SSergey Zigachev 	} else {
2039b843c749SSergey Zigachev 
2040b843c749SSergey Zigachev 		tmp_result = smu7_patch_lookup_table_with_leakage(hwmgr,
2041b843c749SSergey Zigachev 				table_info->vddc_lookup_table, &(data->vddc_leakage));
2042b843c749SSergey Zigachev 		if (tmp_result)
2043b843c749SSergey Zigachev 			result = tmp_result;
2044b843c749SSergey Zigachev 
2045b843c749SSergey Zigachev 		tmp_result = smu7_patch_clock_voltage_limits_with_vddc_leakage(hwmgr,
2046b843c749SSergey Zigachev 				&(data->vddc_leakage), &table_info->max_clock_voltage_on_dc.vddc);
2047b843c749SSergey Zigachev 		if (tmp_result)
2048b843c749SSergey Zigachev 			result = tmp_result;
2049b843c749SSergey Zigachev 	}
2050b843c749SSergey Zigachev 
2051b843c749SSergey Zigachev 	tmp_result = smu7_patch_voltage_dependency_tables_with_lookup_table(hwmgr);
2052b843c749SSergey Zigachev 	if (tmp_result)
2053b843c749SSergey Zigachev 		result = tmp_result;
2054b843c749SSergey Zigachev 
2055b843c749SSergey Zigachev 	tmp_result = smu7_calc_voltage_dependency_tables(hwmgr);
2056b843c749SSergey Zigachev 	if (tmp_result)
2057b843c749SSergey Zigachev 		result = tmp_result;
2058b843c749SSergey Zigachev 
2059b843c749SSergey Zigachev 	tmp_result = smu7_calc_mm_voltage_dependency_table(hwmgr);
2060b843c749SSergey Zigachev 	if (tmp_result)
2061b843c749SSergey Zigachev 		result = tmp_result;
2062b843c749SSergey Zigachev 
2063b843c749SSergey Zigachev 	tmp_result = smu7_sort_lookup_table(hwmgr, table_info->vddgfx_lookup_table);
2064b843c749SSergey Zigachev 	if (tmp_result)
2065b843c749SSergey Zigachev 		result = tmp_result;
2066b843c749SSergey Zigachev 
2067b843c749SSergey Zigachev 	tmp_result = smu7_sort_lookup_table(hwmgr, table_info->vddc_lookup_table);
2068b843c749SSergey Zigachev 	if (tmp_result)
2069b843c749SSergey Zigachev 		result = tmp_result;
2070b843c749SSergey Zigachev 
2071b843c749SSergey Zigachev 	return result;
2072b843c749SSergey Zigachev }
2073b843c749SSergey Zigachev 
smu7_set_private_data_based_on_pptable_v1(struct pp_hwmgr * hwmgr)2074b843c749SSergey Zigachev static int smu7_set_private_data_based_on_pptable_v1(struct pp_hwmgr *hwmgr)
2075b843c749SSergey Zigachev {
2076b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2077b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2078b843c749SSergey Zigachev 
2079b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *allowed_sclk_vdd_table =
2080b843c749SSergey Zigachev 						table_info->vdd_dep_on_sclk;
2081b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *allowed_mclk_vdd_table =
2082b843c749SSergey Zigachev 						table_info->vdd_dep_on_mclk;
2083b843c749SSergey Zigachev 
2084b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_sclk_vdd_table != NULL,
2085b843c749SSergey Zigachev 		"VDD dependency on SCLK table is missing.",
2086b843c749SSergey Zigachev 		return -EINVAL);
2087b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_sclk_vdd_table->count >= 1,
2088b843c749SSergey Zigachev 		"VDD dependency on SCLK table has to have is missing.",
2089b843c749SSergey Zigachev 		return -EINVAL);
2090b843c749SSergey Zigachev 
2091b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_mclk_vdd_table != NULL,
2092b843c749SSergey Zigachev 		"VDD dependency on MCLK table is missing",
2093b843c749SSergey Zigachev 		return -EINVAL);
2094b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_mclk_vdd_table->count >= 1,
2095b843c749SSergey Zigachev 		"VDD dependency on MCLK table has to have is missing.",
2096b843c749SSergey Zigachev 		return -EINVAL);
2097b843c749SSergey Zigachev 
2098b843c749SSergey Zigachev 	table_info->max_clock_voltage_on_ac.sclk =
2099b843c749SSergey Zigachev 		allowed_sclk_vdd_table->entries[allowed_sclk_vdd_table->count - 1].clk;
2100b843c749SSergey Zigachev 	table_info->max_clock_voltage_on_ac.mclk =
2101b843c749SSergey Zigachev 		allowed_mclk_vdd_table->entries[allowed_mclk_vdd_table->count - 1].clk;
2102b843c749SSergey Zigachev 	table_info->max_clock_voltage_on_ac.vddc =
2103b843c749SSergey Zigachev 		allowed_sclk_vdd_table->entries[allowed_sclk_vdd_table->count - 1].vddc;
2104b843c749SSergey Zigachev 	table_info->max_clock_voltage_on_ac.vddci =
2105b843c749SSergey Zigachev 		allowed_mclk_vdd_table->entries[allowed_mclk_vdd_table->count - 1].vddci;
2106b843c749SSergey Zigachev 
2107b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_ac.sclk = table_info->max_clock_voltage_on_ac.sclk;
2108b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_ac.mclk = table_info->max_clock_voltage_on_ac.mclk;
2109b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_ac.vddc = table_info->max_clock_voltage_on_ac.vddc;
2110b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_ac.vddci = table_info->max_clock_voltage_on_ac.vddci;
2111b843c749SSergey Zigachev 
2112b843c749SSergey Zigachev 	return 0;
2113b843c749SSergey Zigachev }
2114b843c749SSergey Zigachev 
smu7_patch_voltage_workaround(struct pp_hwmgr * hwmgr)2115b843c749SSergey Zigachev static int smu7_patch_voltage_workaround(struct pp_hwmgr *hwmgr)
2116b843c749SSergey Zigachev {
2117b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2118b843c749SSergey Zigachev 		       (struct phm_ppt_v1_information *)(hwmgr->pptable);
2119b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table;
2120b843c749SSergey Zigachev 	struct phm_ppt_v1_voltage_lookup_table *lookup_table;
2121b843c749SSergey Zigachev 	uint32_t i;
2122b843c749SSergey Zigachev 	uint32_t hw_revision, sub_vendor_id, sub_sys_id;
2123b843c749SSergey Zigachev 	struct amdgpu_device *adev = hwmgr->adev;
2124b843c749SSergey Zigachev 
2125b843c749SSergey Zigachev 	if (table_info != NULL) {
2126b843c749SSergey Zigachev 		dep_mclk_table = table_info->vdd_dep_on_mclk;
2127b843c749SSergey Zigachev 		lookup_table = table_info->vddc_lookup_table;
2128b843c749SSergey Zigachev 	} else
2129b843c749SSergey Zigachev 		return 0;
2130b843c749SSergey Zigachev 
2131b843c749SSergey Zigachev 	hw_revision = adev->pdev->revision;
2132b843c749SSergey Zigachev 	sub_sys_id = adev->pdev->subsystem_device;
2133b843c749SSergey Zigachev 	sub_vendor_id = adev->pdev->subsystem_vendor;
2134b843c749SSergey Zigachev 
2135b843c749SSergey Zigachev 	if (hwmgr->chip_id == CHIP_POLARIS10 && hw_revision == 0xC7 &&
2136b843c749SSergey Zigachev 			((sub_sys_id == 0xb37 && sub_vendor_id == 0x1002) ||
2137b843c749SSergey Zigachev 		    (sub_sys_id == 0x4a8 && sub_vendor_id == 0x1043) ||
2138b843c749SSergey Zigachev 		    (sub_sys_id == 0x9480 && sub_vendor_id == 0x1682))) {
2139b843c749SSergey Zigachev 		if (lookup_table->entries[dep_mclk_table->entries[dep_mclk_table->count-1].vddInd].us_vdd >= 1000)
2140b843c749SSergey Zigachev 			return 0;
2141b843c749SSergey Zigachev 
2142b843c749SSergey Zigachev 		for (i = 0; i < lookup_table->count; i++) {
2143b843c749SSergey Zigachev 			if (lookup_table->entries[i].us_vdd < 0xff01 && lookup_table->entries[i].us_vdd >= 1000) {
2144b843c749SSergey Zigachev 				dep_mclk_table->entries[dep_mclk_table->count-1].vddInd = (uint8_t) i;
2145b843c749SSergey Zigachev 				return 0;
2146b843c749SSergey Zigachev 			}
2147b843c749SSergey Zigachev 		}
2148b843c749SSergey Zigachev 	}
2149b843c749SSergey Zigachev 	return 0;
2150b843c749SSergey Zigachev }
2151b843c749SSergey Zigachev 
smu7_thermal_parameter_init(struct pp_hwmgr * hwmgr)2152b843c749SSergey Zigachev static int smu7_thermal_parameter_init(struct pp_hwmgr *hwmgr)
2153b843c749SSergey Zigachev {
2154b843c749SSergey Zigachev 	struct pp_atomctrl_gpio_pin_assignment gpio_pin_assignment;
2155b843c749SSergey Zigachev 	uint32_t temp_reg;
2156b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2157b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2158b843c749SSergey Zigachev 
2159b843c749SSergey Zigachev 
2160b843c749SSergey Zigachev 	if (atomctrl_get_pp_assign_pin(hwmgr, VDDC_PCC_GPIO_PINID, &gpio_pin_assignment)) {
2161b843c749SSergey Zigachev 		temp_reg = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCNB_PWRMGT_CNTL);
2162b843c749SSergey Zigachev 		switch (gpio_pin_assignment.uc_gpio_pin_bit_shift) {
2163b843c749SSergey Zigachev 		case 0:
2164b843c749SSergey Zigachev 			temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, GNB_SLOW_MODE, 0x1);
2165b843c749SSergey Zigachev 			break;
2166b843c749SSergey Zigachev 		case 1:
2167b843c749SSergey Zigachev 			temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, GNB_SLOW_MODE, 0x2);
2168b843c749SSergey Zigachev 			break;
2169b843c749SSergey Zigachev 		case 2:
2170b843c749SSergey Zigachev 			temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, GNB_SLOW, 0x1);
2171b843c749SSergey Zigachev 			break;
2172b843c749SSergey Zigachev 		case 3:
2173b843c749SSergey Zigachev 			temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, FORCE_NB_PS1, 0x1);
2174b843c749SSergey Zigachev 			break;
2175b843c749SSergey Zigachev 		case 4:
2176b843c749SSergey Zigachev 			temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, DPM_ENABLED, 0x1);
2177b843c749SSergey Zigachev 			break;
2178b843c749SSergey Zigachev 		default:
2179b843c749SSergey Zigachev 			break;
2180b843c749SSergey Zigachev 		}
2181b843c749SSergey Zigachev 		cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCNB_PWRMGT_CNTL, temp_reg);
2182b843c749SSergey Zigachev 	}
2183b843c749SSergey Zigachev 
2184b843c749SSergey Zigachev 	if (table_info == NULL)
2185b843c749SSergey Zigachev 		return 0;
2186b843c749SSergey Zigachev 
2187b843c749SSergey Zigachev 	if (table_info->cac_dtp_table->usDefaultTargetOperatingTemp != 0 &&
2188b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.ucFanControlMode) {
2189b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usFanPWMMinLimit =
2190b843c749SSergey Zigachev 			(uint16_t)hwmgr->thermal_controller.advanceFanControlParameters.ucMinimumPWMLimit;
2191b843c749SSergey Zigachev 
2192b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usFanPWMMaxLimit =
2193b843c749SSergey Zigachev 			(uint16_t)hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanPWM;
2194b843c749SSergey Zigachev 
2195b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usFanPWMStep = 1;
2196b843c749SSergey Zigachev 
2197b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usFanRPMMaxLimit = 100;
2198b843c749SSergey Zigachev 
2199b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usFanRPMMinLimit =
2200b843c749SSergey Zigachev 			(uint16_t)hwmgr->thermal_controller.advanceFanControlParameters.ucMinimumPWMLimit;
2201b843c749SSergey Zigachev 
2202b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usFanRPMStep = 1;
2203b843c749SSergey Zigachev 
2204b843c749SSergey Zigachev 		table_info->cac_dtp_table->usDefaultTargetOperatingTemp = (table_info->cac_dtp_table->usDefaultTargetOperatingTemp >= 50) ?
2205b843c749SSergey Zigachev 								(table_info->cac_dtp_table->usDefaultTargetOperatingTemp - 50) : 0;
2206b843c749SSergey Zigachev 
2207b843c749SSergey Zigachev 		table_info->cac_dtp_table->usOperatingTempMaxLimit = table_info->cac_dtp_table->usDefaultTargetOperatingTemp;
2208b843c749SSergey Zigachev 		table_info->cac_dtp_table->usOperatingTempStep = 1;
2209b843c749SSergey Zigachev 		table_info->cac_dtp_table->usOperatingTempHyst = 1;
2210b843c749SSergey Zigachev 
2211b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usMaxFanPWM =
2212b843c749SSergey Zigachev 			       hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanPWM;
2213b843c749SSergey Zigachev 
2214b843c749SSergey Zigachev 		hwmgr->thermal_controller.advanceFanControlParameters.usMaxFanRPM =
2215b843c749SSergey Zigachev 			       hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanRPM;
2216b843c749SSergey Zigachev 
2217b843c749SSergey Zigachev 		hwmgr->dyn_state.cac_dtp_table->usOperatingTempMinLimit =
2218b843c749SSergey Zigachev 			       table_info->cac_dtp_table->usOperatingTempMinLimit;
2219b843c749SSergey Zigachev 
2220b843c749SSergey Zigachev 		hwmgr->dyn_state.cac_dtp_table->usOperatingTempMaxLimit =
2221b843c749SSergey Zigachev 			       table_info->cac_dtp_table->usOperatingTempMaxLimit;
2222b843c749SSergey Zigachev 
2223b843c749SSergey Zigachev 		hwmgr->dyn_state.cac_dtp_table->usDefaultTargetOperatingTemp =
2224b843c749SSergey Zigachev 			       table_info->cac_dtp_table->usDefaultTargetOperatingTemp;
2225b843c749SSergey Zigachev 
2226b843c749SSergey Zigachev 		hwmgr->dyn_state.cac_dtp_table->usOperatingTempStep =
2227b843c749SSergey Zigachev 			       table_info->cac_dtp_table->usOperatingTempStep;
2228b843c749SSergey Zigachev 
2229b843c749SSergey Zigachev 		hwmgr->dyn_state.cac_dtp_table->usTargetOperatingTemp =
2230b843c749SSergey Zigachev 			       table_info->cac_dtp_table->usTargetOperatingTemp;
2231b843c749SSergey Zigachev 		if (hwmgr->feature_mask & PP_OD_FUZZY_FAN_CONTROL_MASK)
2232b843c749SSergey Zigachev 			phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2233b843c749SSergey Zigachev 					PHM_PlatformCaps_ODFuzzyFanControlSupport);
2234b843c749SSergey Zigachev 	}
2235b843c749SSergey Zigachev 
2236b843c749SSergey Zigachev 	return 0;
2237b843c749SSergey Zigachev }
2238b843c749SSergey Zigachev 
2239b843c749SSergey Zigachev /**
2240b843c749SSergey Zigachev  * Change virtual leakage voltage to actual value.
2241b843c749SSergey Zigachev  *
2242b843c749SSergey Zigachev  * @param     hwmgr  the address of the powerplay hardware manager.
2243b843c749SSergey Zigachev  * @param     pointer to changing voltage
2244b843c749SSergey Zigachev  * @param     pointer to leakage table
2245b843c749SSergey Zigachev  */
smu7_patch_ppt_v0_with_vdd_leakage(struct pp_hwmgr * hwmgr,uint32_t * voltage,struct smu7_leakage_voltage * leakage_table)2246b843c749SSergey Zigachev static void smu7_patch_ppt_v0_with_vdd_leakage(struct pp_hwmgr *hwmgr,
2247b843c749SSergey Zigachev 		uint32_t *voltage, struct smu7_leakage_voltage *leakage_table)
2248b843c749SSergey Zigachev {
2249b843c749SSergey Zigachev 	uint32_t index;
2250b843c749SSergey Zigachev 
2251b843c749SSergey Zigachev 	/* search for leakage voltage ID 0xff01 ~ 0xff08 */
2252b843c749SSergey Zigachev 	for (index = 0; index < leakage_table->count; index++) {
2253b843c749SSergey Zigachev 		/* if this voltage matches a leakage voltage ID */
2254b843c749SSergey Zigachev 		/* patch with actual leakage voltage */
2255b843c749SSergey Zigachev 		if (leakage_table->leakage_id[index] == *voltage) {
2256b843c749SSergey Zigachev 			*voltage = leakage_table->actual_voltage[index];
2257b843c749SSergey Zigachev 			break;
2258b843c749SSergey Zigachev 		}
2259b843c749SSergey Zigachev 	}
2260b843c749SSergey Zigachev 
2261b843c749SSergey Zigachev 	if (*voltage > ATOM_VIRTUAL_VOLTAGE_ID0)
2262b843c749SSergey Zigachev 		pr_err("Voltage value looks like a Leakage ID but it's not patched \n");
2263b843c749SSergey Zigachev }
2264b843c749SSergey Zigachev 
2265b843c749SSergey Zigachev 
smu7_patch_vddc(struct pp_hwmgr * hwmgr,struct phm_clock_voltage_dependency_table * tab)2266b843c749SSergey Zigachev static int smu7_patch_vddc(struct pp_hwmgr *hwmgr,
2267b843c749SSergey Zigachev 			      struct phm_clock_voltage_dependency_table *tab)
2268b843c749SSergey Zigachev {
2269b843c749SSergey Zigachev 	uint16_t i;
2270b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2271b843c749SSergey Zigachev 
2272b843c749SSergey Zigachev 	if (tab)
2273b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++)
2274b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &tab->entries[i].v,
2275b843c749SSergey Zigachev 						&data->vddc_leakage);
2276b843c749SSergey Zigachev 
2277b843c749SSergey Zigachev 	return 0;
2278b843c749SSergey Zigachev }
2279b843c749SSergey Zigachev 
smu7_patch_vddci(struct pp_hwmgr * hwmgr,struct phm_clock_voltage_dependency_table * tab)2280b843c749SSergey Zigachev static int smu7_patch_vddci(struct pp_hwmgr *hwmgr,
2281b843c749SSergey Zigachev 			       struct phm_clock_voltage_dependency_table *tab)
2282b843c749SSergey Zigachev {
2283b843c749SSergey Zigachev 	uint16_t i;
2284b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2285b843c749SSergey Zigachev 
2286b843c749SSergey Zigachev 	if (tab)
2287b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++)
2288b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &tab->entries[i].v,
2289b843c749SSergey Zigachev 							&data->vddci_leakage);
2290b843c749SSergey Zigachev 
2291b843c749SSergey Zigachev 	return 0;
2292b843c749SSergey Zigachev }
2293b843c749SSergey Zigachev 
smu7_patch_vce_vddc(struct pp_hwmgr * hwmgr,struct phm_vce_clock_voltage_dependency_table * tab)2294b843c749SSergey Zigachev static int smu7_patch_vce_vddc(struct pp_hwmgr *hwmgr,
2295b843c749SSergey Zigachev 				  struct phm_vce_clock_voltage_dependency_table *tab)
2296b843c749SSergey Zigachev {
2297b843c749SSergey Zigachev 	uint16_t i;
2298b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2299b843c749SSergey Zigachev 
2300b843c749SSergey Zigachev 	if (tab)
2301b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++)
2302b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &tab->entries[i].v,
2303b843c749SSergey Zigachev 							&data->vddc_leakage);
2304b843c749SSergey Zigachev 
2305b843c749SSergey Zigachev 	return 0;
2306b843c749SSergey Zigachev }
2307b843c749SSergey Zigachev 
2308b843c749SSergey Zigachev 
smu7_patch_uvd_vddc(struct pp_hwmgr * hwmgr,struct phm_uvd_clock_voltage_dependency_table * tab)2309b843c749SSergey Zigachev static int smu7_patch_uvd_vddc(struct pp_hwmgr *hwmgr,
2310b843c749SSergey Zigachev 				  struct phm_uvd_clock_voltage_dependency_table *tab)
2311b843c749SSergey Zigachev {
2312b843c749SSergey Zigachev 	uint16_t i;
2313b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2314b843c749SSergey Zigachev 
2315b843c749SSergey Zigachev 	if (tab)
2316b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++)
2317b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &tab->entries[i].v,
2318b843c749SSergey Zigachev 							&data->vddc_leakage);
2319b843c749SSergey Zigachev 
2320b843c749SSergey Zigachev 	return 0;
2321b843c749SSergey Zigachev }
2322b843c749SSergey Zigachev 
smu7_patch_vddc_shed_limit(struct pp_hwmgr * hwmgr,struct phm_phase_shedding_limits_table * tab)2323b843c749SSergey Zigachev static int smu7_patch_vddc_shed_limit(struct pp_hwmgr *hwmgr,
2324b843c749SSergey Zigachev 					 struct phm_phase_shedding_limits_table *tab)
2325b843c749SSergey Zigachev {
2326b843c749SSergey Zigachev 	uint16_t i;
2327b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2328b843c749SSergey Zigachev 
2329b843c749SSergey Zigachev 	if (tab)
2330b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++)
2331b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &tab->entries[i].Voltage,
2332b843c749SSergey Zigachev 							&data->vddc_leakage);
2333b843c749SSergey Zigachev 
2334b843c749SSergey Zigachev 	return 0;
2335b843c749SSergey Zigachev }
2336b843c749SSergey Zigachev 
smu7_patch_samu_vddc(struct pp_hwmgr * hwmgr,struct phm_samu_clock_voltage_dependency_table * tab)2337b843c749SSergey Zigachev static int smu7_patch_samu_vddc(struct pp_hwmgr *hwmgr,
2338b843c749SSergey Zigachev 				   struct phm_samu_clock_voltage_dependency_table *tab)
2339b843c749SSergey Zigachev {
2340b843c749SSergey Zigachev 	uint16_t i;
2341b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2342b843c749SSergey Zigachev 
2343b843c749SSergey Zigachev 	if (tab)
2344b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++)
2345b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &tab->entries[i].v,
2346b843c749SSergey Zigachev 							&data->vddc_leakage);
2347b843c749SSergey Zigachev 
2348b843c749SSergey Zigachev 	return 0;
2349b843c749SSergey Zigachev }
2350b843c749SSergey Zigachev 
smu7_patch_acp_vddc(struct pp_hwmgr * hwmgr,struct phm_acp_clock_voltage_dependency_table * tab)2351b843c749SSergey Zigachev static int smu7_patch_acp_vddc(struct pp_hwmgr *hwmgr,
2352b843c749SSergey Zigachev 				  struct phm_acp_clock_voltage_dependency_table *tab)
2353b843c749SSergey Zigachev {
2354b843c749SSergey Zigachev 	uint16_t i;
2355b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2356b843c749SSergey Zigachev 
2357b843c749SSergey Zigachev 	if (tab)
2358b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++)
2359b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &tab->entries[i].v,
2360b843c749SSergey Zigachev 					&data->vddc_leakage);
2361b843c749SSergey Zigachev 
2362b843c749SSergey Zigachev 	return 0;
2363b843c749SSergey Zigachev }
2364b843c749SSergey Zigachev 
smu7_patch_limits_vddc(struct pp_hwmgr * hwmgr,struct phm_clock_and_voltage_limits * tab)2365b843c749SSergey Zigachev static int smu7_patch_limits_vddc(struct pp_hwmgr *hwmgr,
2366b843c749SSergey Zigachev 				  struct phm_clock_and_voltage_limits *tab)
2367b843c749SSergey Zigachev {
2368b843c749SSergey Zigachev 	uint32_t vddc, vddci;
2369b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2370b843c749SSergey Zigachev 
2371b843c749SSergey Zigachev 	if (tab) {
2372b843c749SSergey Zigachev 		vddc = tab->vddc;
2373b843c749SSergey Zigachev 		smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &vddc,
2374b843c749SSergey Zigachev 						   &data->vddc_leakage);
2375b843c749SSergey Zigachev 		tab->vddc = vddc;
2376b843c749SSergey Zigachev 		vddci = tab->vddci;
2377b843c749SSergey Zigachev 		smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &vddci,
2378b843c749SSergey Zigachev 						   &data->vddci_leakage);
2379b843c749SSergey Zigachev 		tab->vddci = vddci;
2380b843c749SSergey Zigachev 	}
2381b843c749SSergey Zigachev 
2382b843c749SSergey Zigachev 	return 0;
2383b843c749SSergey Zigachev }
2384b843c749SSergey Zigachev 
smu7_patch_cac_vddc(struct pp_hwmgr * hwmgr,struct phm_cac_leakage_table * tab)2385b843c749SSergey Zigachev static int smu7_patch_cac_vddc(struct pp_hwmgr *hwmgr, struct phm_cac_leakage_table *tab)
2386b843c749SSergey Zigachev {
2387b843c749SSergey Zigachev 	uint32_t i;
2388b843c749SSergey Zigachev 	uint32_t vddc;
2389b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2390b843c749SSergey Zigachev 
2391b843c749SSergey Zigachev 	if (tab) {
2392b843c749SSergey Zigachev 		for (i = 0; i < tab->count; i++) {
2393b843c749SSergey Zigachev 			vddc = (uint32_t)(tab->entries[i].Vddc);
2394b843c749SSergey Zigachev 			smu7_patch_ppt_v0_with_vdd_leakage(hwmgr, &vddc, &data->vddc_leakage);
2395b843c749SSergey Zigachev 			tab->entries[i].Vddc = (uint16_t)vddc;
2396b843c749SSergey Zigachev 		}
2397b843c749SSergey Zigachev 	}
2398b843c749SSergey Zigachev 
2399b843c749SSergey Zigachev 	return 0;
2400b843c749SSergey Zigachev }
2401b843c749SSergey Zigachev 
smu7_patch_dependency_tables_with_leakage(struct pp_hwmgr * hwmgr)2402b843c749SSergey Zigachev static int smu7_patch_dependency_tables_with_leakage(struct pp_hwmgr *hwmgr)
2403b843c749SSergey Zigachev {
2404b843c749SSergey Zigachev 	int tmp;
2405b843c749SSergey Zigachev 
2406b843c749SSergey Zigachev 	tmp = smu7_patch_vddc(hwmgr, hwmgr->dyn_state.vddc_dependency_on_sclk);
2407b843c749SSergey Zigachev 	if (tmp)
2408b843c749SSergey Zigachev 		return -EINVAL;
2409b843c749SSergey Zigachev 
2410b843c749SSergey Zigachev 	tmp = smu7_patch_vddc(hwmgr, hwmgr->dyn_state.vddc_dependency_on_mclk);
2411b843c749SSergey Zigachev 	if (tmp)
2412b843c749SSergey Zigachev 		return -EINVAL;
2413b843c749SSergey Zigachev 
2414b843c749SSergey Zigachev 	tmp = smu7_patch_vddc(hwmgr, hwmgr->dyn_state.vddc_dep_on_dal_pwrl);
2415b843c749SSergey Zigachev 	if (tmp)
2416b843c749SSergey Zigachev 		return -EINVAL;
2417b843c749SSergey Zigachev 
2418b843c749SSergey Zigachev 	tmp = smu7_patch_vddci(hwmgr, hwmgr->dyn_state.vddci_dependency_on_mclk);
2419b843c749SSergey Zigachev 	if (tmp)
2420b843c749SSergey Zigachev 		return -EINVAL;
2421b843c749SSergey Zigachev 
2422b843c749SSergey Zigachev 	tmp = smu7_patch_vce_vddc(hwmgr, hwmgr->dyn_state.vce_clock_voltage_dependency_table);
2423b843c749SSergey Zigachev 	if (tmp)
2424b843c749SSergey Zigachev 		return -EINVAL;
2425b843c749SSergey Zigachev 
2426b843c749SSergey Zigachev 	tmp = smu7_patch_uvd_vddc(hwmgr, hwmgr->dyn_state.uvd_clock_voltage_dependency_table);
2427b843c749SSergey Zigachev 	if (tmp)
2428b843c749SSergey Zigachev 		return -EINVAL;
2429b843c749SSergey Zigachev 
2430b843c749SSergey Zigachev 	tmp = smu7_patch_samu_vddc(hwmgr, hwmgr->dyn_state.samu_clock_voltage_dependency_table);
2431b843c749SSergey Zigachev 	if (tmp)
2432b843c749SSergey Zigachev 		return -EINVAL;
2433b843c749SSergey Zigachev 
2434b843c749SSergey Zigachev 	tmp = smu7_patch_acp_vddc(hwmgr, hwmgr->dyn_state.acp_clock_voltage_dependency_table);
2435b843c749SSergey Zigachev 	if (tmp)
2436b843c749SSergey Zigachev 		return -EINVAL;
2437b843c749SSergey Zigachev 
2438b843c749SSergey Zigachev 	tmp = smu7_patch_vddc_shed_limit(hwmgr, hwmgr->dyn_state.vddc_phase_shed_limits_table);
2439b843c749SSergey Zigachev 	if (tmp)
2440b843c749SSergey Zigachev 		return -EINVAL;
2441b843c749SSergey Zigachev 
2442b843c749SSergey Zigachev 	tmp = smu7_patch_limits_vddc(hwmgr, &hwmgr->dyn_state.max_clock_voltage_on_ac);
2443b843c749SSergey Zigachev 	if (tmp)
2444b843c749SSergey Zigachev 		return -EINVAL;
2445b843c749SSergey Zigachev 
2446b843c749SSergey Zigachev 	tmp = smu7_patch_limits_vddc(hwmgr, &hwmgr->dyn_state.max_clock_voltage_on_dc);
2447b843c749SSergey Zigachev 	if (tmp)
2448b843c749SSergey Zigachev 		return -EINVAL;
2449b843c749SSergey Zigachev 
2450b843c749SSergey Zigachev 	tmp = smu7_patch_cac_vddc(hwmgr, hwmgr->dyn_state.cac_leakage_table);
2451b843c749SSergey Zigachev 	if (tmp)
2452b843c749SSergey Zigachev 		return -EINVAL;
2453b843c749SSergey Zigachev 
2454b843c749SSergey Zigachev 	return 0;
2455b843c749SSergey Zigachev }
2456b843c749SSergey Zigachev 
2457b843c749SSergey Zigachev 
smu7_set_private_data_based_on_pptable_v0(struct pp_hwmgr * hwmgr)2458b843c749SSergey Zigachev static int smu7_set_private_data_based_on_pptable_v0(struct pp_hwmgr *hwmgr)
2459b843c749SSergey Zigachev {
2460b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2461b843c749SSergey Zigachev 
2462b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *allowed_sclk_vddc_table = hwmgr->dyn_state.vddc_dependency_on_sclk;
2463b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *allowed_mclk_vddc_table = hwmgr->dyn_state.vddc_dependency_on_mclk;
2464b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *allowed_mclk_vddci_table = hwmgr->dyn_state.vddci_dependency_on_mclk;
2465b843c749SSergey Zigachev 
2466b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_sclk_vddc_table != NULL,
2467b843c749SSergey Zigachev 		"VDDC dependency on SCLK table is missing. This table is mandatory",
2468b843c749SSergey Zigachev 		return -EINVAL);
2469b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_sclk_vddc_table->count >= 1,
2470b843c749SSergey Zigachev 		"VDDC dependency on SCLK table has to have is missing. This table is mandatory",
2471b843c749SSergey Zigachev 		return -EINVAL);
2472b843c749SSergey Zigachev 
2473b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_mclk_vddc_table != NULL,
2474b843c749SSergey Zigachev 		"VDDC dependency on MCLK table is missing. This table is mandatory",
2475b843c749SSergey Zigachev 		return -EINVAL);
2476b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(allowed_mclk_vddc_table->count >= 1,
2477b843c749SSergey Zigachev 		"VDD dependency on MCLK table has to have is missing. This table is mandatory",
2478b843c749SSergey Zigachev 		return -EINVAL);
2479b843c749SSergey Zigachev 
2480b843c749SSergey Zigachev 	data->min_vddc_in_pptable = (uint16_t)allowed_sclk_vddc_table->entries[0].v;
2481b843c749SSergey Zigachev 	data->max_vddc_in_pptable = (uint16_t)allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
2482b843c749SSergey Zigachev 
2483b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_ac.sclk =
2484b843c749SSergey Zigachev 		allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].clk;
2485b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_ac.mclk =
2486b843c749SSergey Zigachev 		allowed_mclk_vddc_table->entries[allowed_mclk_vddc_table->count - 1].clk;
2487b843c749SSergey Zigachev 	hwmgr->dyn_state.max_clock_voltage_on_ac.vddc =
2488b843c749SSergey Zigachev 		allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
2489b843c749SSergey Zigachev 
2490b843c749SSergey Zigachev 	if (allowed_mclk_vddci_table != NULL && allowed_mclk_vddci_table->count >= 1) {
2491b843c749SSergey Zigachev 		data->min_vddci_in_pptable = (uint16_t)allowed_mclk_vddci_table->entries[0].v;
2492b843c749SSergey Zigachev 		data->max_vddci_in_pptable = (uint16_t)allowed_mclk_vddci_table->entries[allowed_mclk_vddci_table->count - 1].v;
2493b843c749SSergey Zigachev 	}
2494b843c749SSergey Zigachev 
2495b843c749SSergey Zigachev 	if (hwmgr->dyn_state.vddci_dependency_on_mclk != NULL && hwmgr->dyn_state.vddci_dependency_on_mclk->count >= 1)
2496b843c749SSergey Zigachev 		hwmgr->dyn_state.max_clock_voltage_on_ac.vddci = hwmgr->dyn_state.vddci_dependency_on_mclk->entries[hwmgr->dyn_state.vddci_dependency_on_mclk->count - 1].v;
2497b843c749SSergey Zigachev 
2498b843c749SSergey Zigachev 	return 0;
2499b843c749SSergey Zigachev }
2500b843c749SSergey Zigachev 
smu7_hwmgr_backend_fini(struct pp_hwmgr * hwmgr)2501b843c749SSergey Zigachev static int smu7_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
2502b843c749SSergey Zigachev {
2503b843c749SSergey Zigachev 	kfree(hwmgr->dyn_state.vddc_dep_on_dal_pwrl);
2504b843c749SSergey Zigachev 	hwmgr->dyn_state.vddc_dep_on_dal_pwrl = NULL;
2505b843c749SSergey Zigachev 	kfree(hwmgr->backend);
2506b843c749SSergey Zigachev 	hwmgr->backend = NULL;
2507b843c749SSergey Zigachev 
2508b843c749SSergey Zigachev 	return 0;
2509b843c749SSergey Zigachev }
2510b843c749SSergey Zigachev 
smu7_get_elb_voltages(struct pp_hwmgr * hwmgr)2511b843c749SSergey Zigachev static int smu7_get_elb_voltages(struct pp_hwmgr *hwmgr)
2512b843c749SSergey Zigachev {
2513b843c749SSergey Zigachev 	uint16_t virtual_voltage_id, vddc, vddci, efuse_voltage_id;
2514b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2515b843c749SSergey Zigachev 	int i;
2516b843c749SSergey Zigachev 
2517b843c749SSergey Zigachev 	if (atomctrl_get_leakage_id_from_efuse(hwmgr, &efuse_voltage_id) == 0) {
2518b843c749SSergey Zigachev 		for (i = 0; i < SMU7_MAX_LEAKAGE_COUNT; i++) {
2519b843c749SSergey Zigachev 			virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
2520b843c749SSergey Zigachev 			if (atomctrl_get_leakage_vddc_base_on_leakage(hwmgr, &vddc, &vddci,
2521b843c749SSergey Zigachev 								virtual_voltage_id,
2522b843c749SSergey Zigachev 								efuse_voltage_id) == 0) {
2523b843c749SSergey Zigachev 				if (vddc != 0 && vddc != virtual_voltage_id) {
2524b843c749SSergey Zigachev 					data->vddc_leakage.actual_voltage[data->vddc_leakage.count] = vddc;
2525b843c749SSergey Zigachev 					data->vddc_leakage.leakage_id[data->vddc_leakage.count] = virtual_voltage_id;
2526b843c749SSergey Zigachev 					data->vddc_leakage.count++;
2527b843c749SSergey Zigachev 				}
2528b843c749SSergey Zigachev 				if (vddci != 0 && vddci != virtual_voltage_id) {
2529b843c749SSergey Zigachev 					data->vddci_leakage.actual_voltage[data->vddci_leakage.count] = vddci;
2530b843c749SSergey Zigachev 					data->vddci_leakage.leakage_id[data->vddci_leakage.count] = virtual_voltage_id;
2531b843c749SSergey Zigachev 					data->vddci_leakage.count++;
2532b843c749SSergey Zigachev 				}
2533b843c749SSergey Zigachev 			}
2534b843c749SSergey Zigachev 		}
2535b843c749SSergey Zigachev 	}
2536b843c749SSergey Zigachev 	return 0;
2537b843c749SSergey Zigachev }
2538b843c749SSergey Zigachev 
smu7_hwmgr_backend_init(struct pp_hwmgr * hwmgr)2539b843c749SSergey Zigachev static int smu7_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
2540b843c749SSergey Zigachev {
2541b843c749SSergey Zigachev 	struct smu7_hwmgr *data;
2542b843c749SSergey Zigachev 	int result = 0;
2543b843c749SSergey Zigachev 
2544b843c749SSergey Zigachev 	data = kzalloc(sizeof(struct smu7_hwmgr), GFP_KERNEL);
2545b843c749SSergey Zigachev 	if (data == NULL)
2546b843c749SSergey Zigachev 		return -ENOMEM;
2547b843c749SSergey Zigachev 
2548b843c749SSergey Zigachev 	hwmgr->backend = data;
2549b843c749SSergey Zigachev 	smu7_patch_voltage_workaround(hwmgr);
2550b843c749SSergey Zigachev 	smu7_init_dpm_defaults(hwmgr);
2551b843c749SSergey Zigachev 
2552b843c749SSergey Zigachev 	/* Get leakage voltage based on leakage ID. */
2553b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2554b843c749SSergey Zigachev 			PHM_PlatformCaps_EVV)) {
2555b843c749SSergey Zigachev 		result = smu7_get_evv_voltages(hwmgr);
2556b843c749SSergey Zigachev 		if (result) {
2557b843c749SSergey Zigachev 			pr_info("Get EVV Voltage Failed.  Abort Driver loading!\n");
2558b843c749SSergey Zigachev 			return -EINVAL;
2559b843c749SSergey Zigachev 		}
2560b843c749SSergey Zigachev 	} else {
2561b843c749SSergey Zigachev 		smu7_get_elb_voltages(hwmgr);
2562b843c749SSergey Zigachev 	}
2563b843c749SSergey Zigachev 
2564b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V1) {
2565b843c749SSergey Zigachev 		smu7_complete_dependency_tables(hwmgr);
2566b843c749SSergey Zigachev 		smu7_set_private_data_based_on_pptable_v1(hwmgr);
2567b843c749SSergey Zigachev 	} else if (hwmgr->pp_table_version == PP_TABLE_V0) {
2568b843c749SSergey Zigachev 		smu7_patch_dependency_tables_with_leakage(hwmgr);
2569b843c749SSergey Zigachev 		smu7_set_private_data_based_on_pptable_v0(hwmgr);
2570b843c749SSergey Zigachev 	}
2571b843c749SSergey Zigachev 
2572b843c749SSergey Zigachev 	/* Initalize Dynamic State Adjustment Rule Settings */
2573b843c749SSergey Zigachev 	result = phm_initializa_dynamic_state_adjustment_rule_settings(hwmgr);
2574b843c749SSergey Zigachev 
2575b843c749SSergey Zigachev 	if (0 == result) {
2576b843c749SSergey Zigachev 		struct amdgpu_device *adev = hwmgr->adev;
2577b843c749SSergey Zigachev 
2578b843c749SSergey Zigachev 		data->is_tlu_enabled = false;
2579b843c749SSergey Zigachev 
2580b843c749SSergey Zigachev 		hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =
2581b843c749SSergey Zigachev 							SMU7_MAX_HARDWARE_POWERLEVELS;
2582b843c749SSergey Zigachev 		hwmgr->platform_descriptor.hardwarePerformanceLevels = 2;
2583b843c749SSergey Zigachev 		hwmgr->platform_descriptor.minimumClocksReductionPercentage = 50;
2584b843c749SSergey Zigachev 
2585b843c749SSergey Zigachev 		data->pcie_gen_cap = adev->pm.pcie_gen_mask;
2586b843c749SSergey Zigachev 		if (data->pcie_gen_cap & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
2587b843c749SSergey Zigachev 			data->pcie_spc_cap = 20;
2588b843c749SSergey Zigachev 		data->pcie_lane_cap = adev->pm.pcie_mlw_mask;
2589b843c749SSergey Zigachev 
2590b843c749SSergey Zigachev 		hwmgr->platform_descriptor.vbiosInterruptId = 0x20000400; /* IRQ_SOURCE1_SW_INT */
2591b843c749SSergey Zigachev /* The true clock step depends on the frequency, typically 4.5 or 9 MHz. Here we use 5. */
2592b843c749SSergey Zigachev 		hwmgr->platform_descriptor.clockStep.engineClock = 500;
2593b843c749SSergey Zigachev 		hwmgr->platform_descriptor.clockStep.memoryClock = 500;
2594b843c749SSergey Zigachev 		smu7_thermal_parameter_init(hwmgr);
2595b843c749SSergey Zigachev 	} else {
2596b843c749SSergey Zigachev 		/* Ignore return value in here, we are cleaning up a mess. */
2597b843c749SSergey Zigachev 		smu7_hwmgr_backend_fini(hwmgr);
2598b843c749SSergey Zigachev 	}
2599b843c749SSergey Zigachev 
2600b843c749SSergey Zigachev 	return 0;
2601b843c749SSergey Zigachev }
2602b843c749SSergey Zigachev 
smu7_force_dpm_highest(struct pp_hwmgr * hwmgr)2603b843c749SSergey Zigachev static int smu7_force_dpm_highest(struct pp_hwmgr *hwmgr)
2604b843c749SSergey Zigachev {
2605b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2606b843c749SSergey Zigachev 	uint32_t level, tmp;
2607b843c749SSergey Zigachev 
2608b843c749SSergey Zigachev 	if (!data->pcie_dpm_key_disabled) {
2609b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.pcie_dpm_enable_mask) {
2610b843c749SSergey Zigachev 			level = 0;
2611b843c749SSergey Zigachev 			tmp = data->dpm_level_enable_mask.pcie_dpm_enable_mask;
2612b843c749SSergey Zigachev 			while (tmp >>= 1)
2613b843c749SSergey Zigachev 				level++;
2614b843c749SSergey Zigachev 
2615b843c749SSergey Zigachev 			if (level)
2616b843c749SSergey Zigachev 				smum_send_msg_to_smc_with_parameter(hwmgr,
2617b843c749SSergey Zigachev 						PPSMC_MSG_PCIeDPM_ForceLevel, level);
2618b843c749SSergey Zigachev 		}
2619b843c749SSergey Zigachev 	}
2620b843c749SSergey Zigachev 
2621b843c749SSergey Zigachev 	if (!data->sclk_dpm_key_disabled) {
2622b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
2623b843c749SSergey Zigachev 			level = 0;
2624b843c749SSergey Zigachev 			tmp = data->dpm_level_enable_mask.sclk_dpm_enable_mask;
2625b843c749SSergey Zigachev 			while (tmp >>= 1)
2626b843c749SSergey Zigachev 				level++;
2627b843c749SSergey Zigachev 
2628b843c749SSergey Zigachev 			if (level)
2629b843c749SSergey Zigachev 				smum_send_msg_to_smc_with_parameter(hwmgr,
2630b843c749SSergey Zigachev 						PPSMC_MSG_SCLKDPM_SetEnabledMask,
2631b843c749SSergey Zigachev 						(1 << level));
2632b843c749SSergey Zigachev 		}
2633b843c749SSergey Zigachev 	}
2634b843c749SSergey Zigachev 
2635b843c749SSergey Zigachev 	if (!data->mclk_dpm_key_disabled) {
2636b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.mclk_dpm_enable_mask) {
2637b843c749SSergey Zigachev 			level = 0;
2638b843c749SSergey Zigachev 			tmp = data->dpm_level_enable_mask.mclk_dpm_enable_mask;
2639b843c749SSergey Zigachev 			while (tmp >>= 1)
2640b843c749SSergey Zigachev 				level++;
2641b843c749SSergey Zigachev 
2642b843c749SSergey Zigachev 			if (level)
2643b843c749SSergey Zigachev 				smum_send_msg_to_smc_with_parameter(hwmgr,
2644b843c749SSergey Zigachev 						PPSMC_MSG_MCLKDPM_SetEnabledMask,
2645b843c749SSergey Zigachev 						(1 << level));
2646b843c749SSergey Zigachev 		}
2647b843c749SSergey Zigachev 	}
2648b843c749SSergey Zigachev 
2649b843c749SSergey Zigachev 	return 0;
2650b843c749SSergey Zigachev }
2651b843c749SSergey Zigachev 
smu7_upload_dpm_level_enable_mask(struct pp_hwmgr * hwmgr)2652b843c749SSergey Zigachev static int smu7_upload_dpm_level_enable_mask(struct pp_hwmgr *hwmgr)
2653b843c749SSergey Zigachev {
2654b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2655b843c749SSergey Zigachev 
2656b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V1)
2657b843c749SSergey Zigachev 		phm_apply_dal_min_voltage_request(hwmgr);
2658b843c749SSergey Zigachev /* TO DO  for v0 iceland and Ci*/
2659b843c749SSergey Zigachev 
2660b843c749SSergey Zigachev 	if (!data->sclk_dpm_key_disabled) {
2661b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.sclk_dpm_enable_mask)
2662b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
2663b843c749SSergey Zigachev 					PPSMC_MSG_SCLKDPM_SetEnabledMask,
2664b843c749SSergey Zigachev 					data->dpm_level_enable_mask.sclk_dpm_enable_mask);
2665b843c749SSergey Zigachev 	}
2666b843c749SSergey Zigachev 
2667b843c749SSergey Zigachev 	if (!data->mclk_dpm_key_disabled) {
2668b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.mclk_dpm_enable_mask)
2669b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
2670b843c749SSergey Zigachev 					PPSMC_MSG_MCLKDPM_SetEnabledMask,
2671b843c749SSergey Zigachev 					data->dpm_level_enable_mask.mclk_dpm_enable_mask);
2672b843c749SSergey Zigachev 	}
2673b843c749SSergey Zigachev 
2674b843c749SSergey Zigachev 	return 0;
2675b843c749SSergey Zigachev }
2676b843c749SSergey Zigachev 
smu7_unforce_dpm_levels(struct pp_hwmgr * hwmgr)2677b843c749SSergey Zigachev static int smu7_unforce_dpm_levels(struct pp_hwmgr *hwmgr)
2678b843c749SSergey Zigachev {
2679b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2680b843c749SSergey Zigachev 
2681b843c749SSergey Zigachev 	if (!smum_is_dpm_running(hwmgr))
2682b843c749SSergey Zigachev 		return -EINVAL;
2683b843c749SSergey Zigachev 
2684b843c749SSergey Zigachev 	if (!data->pcie_dpm_key_disabled) {
2685b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr,
2686b843c749SSergey Zigachev 				PPSMC_MSG_PCIeDPM_UnForceLevel);
2687b843c749SSergey Zigachev 	}
2688b843c749SSergey Zigachev 
2689b843c749SSergey Zigachev 	return smu7_upload_dpm_level_enable_mask(hwmgr);
2690b843c749SSergey Zigachev }
2691b843c749SSergey Zigachev 
smu7_force_dpm_lowest(struct pp_hwmgr * hwmgr)2692b843c749SSergey Zigachev static int smu7_force_dpm_lowest(struct pp_hwmgr *hwmgr)
2693b843c749SSergey Zigachev {
2694b843c749SSergey Zigachev 	struct smu7_hwmgr *data =
2695b843c749SSergey Zigachev 			(struct smu7_hwmgr *)(hwmgr->backend);
2696b843c749SSergey Zigachev 	uint32_t level;
2697b843c749SSergey Zigachev 
2698b843c749SSergey Zigachev 	if (!data->sclk_dpm_key_disabled)
2699b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
2700b843c749SSergey Zigachev 			level = phm_get_lowest_enabled_level(hwmgr,
2701b843c749SSergey Zigachev 							      data->dpm_level_enable_mask.sclk_dpm_enable_mask);
2702b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
2703b843c749SSergey Zigachev 							    PPSMC_MSG_SCLKDPM_SetEnabledMask,
2704b843c749SSergey Zigachev 							    (1 << level));
2705b843c749SSergey Zigachev 
2706b843c749SSergey Zigachev 	}
2707b843c749SSergey Zigachev 
2708b843c749SSergey Zigachev 	if (!data->mclk_dpm_key_disabled) {
2709b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.mclk_dpm_enable_mask) {
2710b843c749SSergey Zigachev 			level = phm_get_lowest_enabled_level(hwmgr,
2711b843c749SSergey Zigachev 							      data->dpm_level_enable_mask.mclk_dpm_enable_mask);
2712b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
2713b843c749SSergey Zigachev 							    PPSMC_MSG_MCLKDPM_SetEnabledMask,
2714b843c749SSergey Zigachev 							    (1 << level));
2715b843c749SSergey Zigachev 		}
2716b843c749SSergey Zigachev 	}
2717b843c749SSergey Zigachev 
2718b843c749SSergey Zigachev 	if (!data->pcie_dpm_key_disabled) {
2719b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.pcie_dpm_enable_mask) {
2720b843c749SSergey Zigachev 			level = phm_get_lowest_enabled_level(hwmgr,
2721b843c749SSergey Zigachev 							      data->dpm_level_enable_mask.pcie_dpm_enable_mask);
2722b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
2723b843c749SSergey Zigachev 							    PPSMC_MSG_PCIeDPM_ForceLevel,
2724b843c749SSergey Zigachev 							    (level));
2725b843c749SSergey Zigachev 		}
2726b843c749SSergey Zigachev 	}
2727b843c749SSergey Zigachev 
2728b843c749SSergey Zigachev 	return 0;
2729b843c749SSergey Zigachev }
2730b843c749SSergey Zigachev 
smu7_get_profiling_clk(struct pp_hwmgr * hwmgr,enum amd_dpm_forced_level level,uint32_t * sclk_mask,uint32_t * mclk_mask,uint32_t * pcie_mask)2731b843c749SSergey Zigachev static int smu7_get_profiling_clk(struct pp_hwmgr *hwmgr, enum amd_dpm_forced_level level,
2732b843c749SSergey Zigachev 				uint32_t *sclk_mask, uint32_t *mclk_mask, uint32_t *pcie_mask)
2733b843c749SSergey Zigachev {
2734b843c749SSergey Zigachev 	uint32_t percentage;
2735b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2736b843c749SSergey Zigachev 	struct smu7_dpm_table *golden_dpm_table = &data->golden_dpm_table;
2737b843c749SSergey Zigachev 	int32_t tmp_mclk;
2738b843c749SSergey Zigachev 	int32_t tmp_sclk;
2739b843c749SSergey Zigachev 	int32_t count;
2740b843c749SSergey Zigachev 
2741b843c749SSergey Zigachev 	if (golden_dpm_table->mclk_table.count < 1)
2742b843c749SSergey Zigachev 		return -EINVAL;
2743b843c749SSergey Zigachev 
2744b843c749SSergey Zigachev 	percentage = 100 * golden_dpm_table->sclk_table.dpm_levels[golden_dpm_table->sclk_table.count - 1].value /
2745b843c749SSergey Zigachev 			golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count - 1].value;
2746b843c749SSergey Zigachev 
2747b843c749SSergey Zigachev 	if (golden_dpm_table->mclk_table.count == 1) {
2748b843c749SSergey Zigachev 		percentage = 70;
2749b843c749SSergey Zigachev 		tmp_mclk = golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count - 1].value;
2750b843c749SSergey Zigachev 		*mclk_mask = golden_dpm_table->mclk_table.count - 1;
2751b843c749SSergey Zigachev 	} else {
2752b843c749SSergey Zigachev 		tmp_mclk = golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count - 2].value;
2753b843c749SSergey Zigachev 		*mclk_mask = golden_dpm_table->mclk_table.count - 2;
2754b843c749SSergey Zigachev 	}
2755b843c749SSergey Zigachev 
2756b843c749SSergey Zigachev 	tmp_sclk = tmp_mclk * percentage / 100;
2757b843c749SSergey Zigachev 
2758b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V0) {
2759b843c749SSergey Zigachev 		for (count = hwmgr->dyn_state.vddc_dependency_on_sclk->count-1;
2760b843c749SSergey Zigachev 			count >= 0; count--) {
2761b843c749SSergey Zigachev 			if (tmp_sclk >= hwmgr->dyn_state.vddc_dependency_on_sclk->entries[count].clk) {
2762b843c749SSergey Zigachev 				tmp_sclk = hwmgr->dyn_state.vddc_dependency_on_sclk->entries[count].clk;
2763b843c749SSergey Zigachev 				*sclk_mask = count;
2764b843c749SSergey Zigachev 				break;
2765b843c749SSergey Zigachev 			}
2766b843c749SSergey Zigachev 		}
2767b843c749SSergey Zigachev 		if (count < 0 || level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) {
2768b843c749SSergey Zigachev 			*sclk_mask = 0;
2769b843c749SSergey Zigachev 			tmp_sclk = hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].clk;
2770b843c749SSergey Zigachev 		}
2771b843c749SSergey Zigachev 
2772b843c749SSergey Zigachev 		if (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)
2773b843c749SSergey Zigachev 			*sclk_mask = hwmgr->dyn_state.vddc_dependency_on_sclk->count-1;
2774b843c749SSergey Zigachev 	} else if (hwmgr->pp_table_version == PP_TABLE_V1) {
2775b843c749SSergey Zigachev 		struct phm_ppt_v1_information *table_info =
2776b843c749SSergey Zigachev 				(struct phm_ppt_v1_information *)(hwmgr->pptable);
2777b843c749SSergey Zigachev 
2778b843c749SSergey Zigachev 		for (count = table_info->vdd_dep_on_sclk->count-1; count >= 0; count--) {
2779b843c749SSergey Zigachev 			if (tmp_sclk >= table_info->vdd_dep_on_sclk->entries[count].clk) {
2780b843c749SSergey Zigachev 				tmp_sclk = table_info->vdd_dep_on_sclk->entries[count].clk;
2781b843c749SSergey Zigachev 				*sclk_mask = count;
2782b843c749SSergey Zigachev 				break;
2783b843c749SSergey Zigachev 			}
2784b843c749SSergey Zigachev 		}
2785b843c749SSergey Zigachev 		if (count < 0 || level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) {
2786b843c749SSergey Zigachev 			*sclk_mask = 0;
2787b843c749SSergey Zigachev 			tmp_sclk =  table_info->vdd_dep_on_sclk->entries[0].clk;
2788b843c749SSergey Zigachev 		}
2789b843c749SSergey Zigachev 
2790b843c749SSergey Zigachev 		if (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)
2791b843c749SSergey Zigachev 			*sclk_mask = table_info->vdd_dep_on_sclk->count - 1;
2792b843c749SSergey Zigachev 	}
2793b843c749SSergey Zigachev 
2794b843c749SSergey Zigachev 	if (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK)
2795b843c749SSergey Zigachev 		*mclk_mask = 0;
2796b843c749SSergey Zigachev 	else if (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)
2797b843c749SSergey Zigachev 		*mclk_mask = golden_dpm_table->mclk_table.count - 1;
2798b843c749SSergey Zigachev 
2799b843c749SSergey Zigachev 	*pcie_mask = data->dpm_table.pcie_speed_table.count - 1;
2800b843c749SSergey Zigachev 	hwmgr->pstate_sclk = tmp_sclk;
2801b843c749SSergey Zigachev 	hwmgr->pstate_mclk = tmp_mclk;
2802b843c749SSergey Zigachev 
2803b843c749SSergey Zigachev 	return 0;
2804b843c749SSergey Zigachev }
2805b843c749SSergey Zigachev 
smu7_force_dpm_level(struct pp_hwmgr * hwmgr,enum amd_dpm_forced_level level)2806b843c749SSergey Zigachev static int smu7_force_dpm_level(struct pp_hwmgr *hwmgr,
2807b843c749SSergey Zigachev 				enum amd_dpm_forced_level level)
2808b843c749SSergey Zigachev {
2809b843c749SSergey Zigachev 	int ret = 0;
2810b843c749SSergey Zigachev 	uint32_t sclk_mask = 0;
2811b843c749SSergey Zigachev 	uint32_t mclk_mask = 0;
2812b843c749SSergey Zigachev 	uint32_t pcie_mask = 0;
2813b843c749SSergey Zigachev 
2814b843c749SSergey Zigachev 	if (hwmgr->pstate_sclk == 0)
2815b843c749SSergey Zigachev 		smu7_get_profiling_clk(hwmgr, level, &sclk_mask, &mclk_mask, &pcie_mask);
2816b843c749SSergey Zigachev 
2817b843c749SSergey Zigachev 	switch (level) {
2818b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_HIGH:
2819b843c749SSergey Zigachev 		ret = smu7_force_dpm_highest(hwmgr);
2820b843c749SSergey Zigachev 		break;
2821b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_LOW:
2822b843c749SSergey Zigachev 		ret = smu7_force_dpm_lowest(hwmgr);
2823b843c749SSergey Zigachev 		break;
2824b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_AUTO:
2825b843c749SSergey Zigachev 		ret = smu7_unforce_dpm_levels(hwmgr);
2826b843c749SSergey Zigachev 		break;
2827b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
2828b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
2829b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
2830b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
2831b843c749SSergey Zigachev 		ret = smu7_get_profiling_clk(hwmgr, level, &sclk_mask, &mclk_mask, &pcie_mask);
2832b843c749SSergey Zigachev 		if (ret)
2833b843c749SSergey Zigachev 			return ret;
2834b843c749SSergey Zigachev 		smu7_force_clock_level(hwmgr, PP_SCLK, 1<<sclk_mask);
2835b843c749SSergey Zigachev 		smu7_force_clock_level(hwmgr, PP_MCLK, 1<<mclk_mask);
2836b843c749SSergey Zigachev 		smu7_force_clock_level(hwmgr, PP_PCIE, 1<<pcie_mask);
2837b843c749SSergey Zigachev 		break;
2838b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_MANUAL:
2839b843c749SSergey Zigachev 	case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
2840b843c749SSergey Zigachev 	default:
2841b843c749SSergey Zigachev 		break;
2842b843c749SSergey Zigachev 	}
2843b843c749SSergey Zigachev 
2844b843c749SSergey Zigachev 	if (!ret) {
2845b843c749SSergey Zigachev 		if (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK && hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)
2846b843c749SSergey Zigachev 			smu7_fan_ctrl_set_fan_speed_percent(hwmgr, 100);
2847b843c749SSergey Zigachev 		else if (level != AMD_DPM_FORCED_LEVEL_PROFILE_PEAK && hwmgr->dpm_level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)
2848b843c749SSergey Zigachev 			smu7_fan_ctrl_reset_fan_speed_to_default(hwmgr);
2849b843c749SSergey Zigachev 	}
2850b843c749SSergey Zigachev 	return ret;
2851b843c749SSergey Zigachev }
2852b843c749SSergey Zigachev 
smu7_get_power_state_size(struct pp_hwmgr * hwmgr)2853b843c749SSergey Zigachev static int smu7_get_power_state_size(struct pp_hwmgr *hwmgr)
2854b843c749SSergey Zigachev {
2855b843c749SSergey Zigachev 	return sizeof(struct smu7_power_state);
2856b843c749SSergey Zigachev }
2857b843c749SSergey Zigachev 
smu7_vblank_too_short(struct pp_hwmgr * hwmgr,uint32_t vblank_time_us)2858b843c749SSergey Zigachev static int smu7_vblank_too_short(struct pp_hwmgr *hwmgr,
2859b843c749SSergey Zigachev 				 uint32_t vblank_time_us)
2860b843c749SSergey Zigachev {
2861b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2862b843c749SSergey Zigachev 	uint32_t switch_limit_us;
2863b843c749SSergey Zigachev 
2864b843c749SSergey Zigachev 	switch (hwmgr->chip_id) {
2865b843c749SSergey Zigachev 	case CHIP_POLARIS10:
2866b843c749SSergey Zigachev 	case CHIP_POLARIS11:
2867b843c749SSergey Zigachev 	case CHIP_POLARIS12:
2868b843c749SSergey Zigachev 		switch_limit_us = data->is_memory_gddr5 ? 190 : 150;
2869b843c749SSergey Zigachev 		break;
2870b843c749SSergey Zigachev 	case CHIP_VEGAM:
2871b843c749SSergey Zigachev 		switch_limit_us = 30;
2872b843c749SSergey Zigachev 		break;
2873b843c749SSergey Zigachev 	default:
2874b843c749SSergey Zigachev 		switch_limit_us = data->is_memory_gddr5 ? 450 : 150;
2875b843c749SSergey Zigachev 		break;
2876b843c749SSergey Zigachev 	}
2877b843c749SSergey Zigachev 
2878b843c749SSergey Zigachev 	if (vblank_time_us < switch_limit_us)
2879b843c749SSergey Zigachev 		return true;
2880b843c749SSergey Zigachev 	else
2881b843c749SSergey Zigachev 		return false;
2882b843c749SSergey Zigachev }
2883b843c749SSergey Zigachev 
smu7_apply_state_adjust_rules(struct pp_hwmgr * hwmgr,struct pp_power_state * request_ps,const struct pp_power_state * current_ps)2884b843c749SSergey Zigachev static int smu7_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
2885b843c749SSergey Zigachev 				struct pp_power_state *request_ps,
2886b843c749SSergey Zigachev 			const struct pp_power_state *current_ps)
2887b843c749SSergey Zigachev {
2888b843c749SSergey Zigachev 	struct amdgpu_device *adev = hwmgr->adev;
2889b843c749SSergey Zigachev 	struct smu7_power_state *smu7_ps =
2890b843c749SSergey Zigachev 				cast_phw_smu7_power_state(&request_ps->hardware);
2891b843c749SSergey Zigachev 	uint32_t sclk;
2892b843c749SSergey Zigachev 	uint32_t mclk;
2893b843c749SSergey Zigachev 	struct PP_Clocks minimum_clocks = {0};
2894b843c749SSergey Zigachev 	bool disable_mclk_switching;
2895b843c749SSergey Zigachev 	bool disable_mclk_switching_for_frame_lock;
2896b843c749SSergey Zigachev 	const struct phm_clock_and_voltage_limits *max_limits;
2897b843c749SSergey Zigachev 	uint32_t i;
2898b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2899b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
2900b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
2901b843c749SSergey Zigachev 	int32_t count;
2902b843c749SSergey Zigachev 	int32_t stable_pstate_sclk = 0, stable_pstate_mclk = 0;
2903b843c749SSergey Zigachev 
2904b843c749SSergey Zigachev 	data->battery_state = (PP_StateUILabel_Battery ==
2905b843c749SSergey Zigachev 			request_ps->classification.ui_label);
2906b843c749SSergey Zigachev 
2907b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(smu7_ps->performance_level_count == 2,
2908b843c749SSergey Zigachev 				 "VI should always have 2 performance levels",
2909b843c749SSergey Zigachev 				);
2910b843c749SSergey Zigachev 
2911b843c749SSergey Zigachev 	max_limits = adev->pm.ac_power ?
2912b843c749SSergey Zigachev 			&(hwmgr->dyn_state.max_clock_voltage_on_ac) :
2913b843c749SSergey Zigachev 			&(hwmgr->dyn_state.max_clock_voltage_on_dc);
2914b843c749SSergey Zigachev 
2915b843c749SSergey Zigachev 	/* Cap clock DPM tables at DC MAX if it is in DC. */
2916b843c749SSergey Zigachev 	if (!adev->pm.ac_power) {
2917b843c749SSergey Zigachev 		for (i = 0; i < smu7_ps->performance_level_count; i++) {
2918b843c749SSergey Zigachev 			if (smu7_ps->performance_levels[i].memory_clock > max_limits->mclk)
2919b843c749SSergey Zigachev 				smu7_ps->performance_levels[i].memory_clock = max_limits->mclk;
2920b843c749SSergey Zigachev 			if (smu7_ps->performance_levels[i].engine_clock > max_limits->sclk)
2921b843c749SSergey Zigachev 				smu7_ps->performance_levels[i].engine_clock = max_limits->sclk;
2922b843c749SSergey Zigachev 		}
2923b843c749SSergey Zigachev 	}
2924b843c749SSergey Zigachev 
2925b843c749SSergey Zigachev 	minimum_clocks.engineClock = hwmgr->display_config->min_core_set_clock;
2926b843c749SSergey Zigachev 	minimum_clocks.memoryClock = hwmgr->display_config->min_mem_set_clock;
2927b843c749SSergey Zigachev 
2928b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2929b843c749SSergey Zigachev 			PHM_PlatformCaps_StablePState)) {
2930b843c749SSergey Zigachev 		max_limits = &(hwmgr->dyn_state.max_clock_voltage_on_ac);
2931b843c749SSergey Zigachev 		stable_pstate_sclk = (max_limits->sclk * 75) / 100;
2932b843c749SSergey Zigachev 
2933b843c749SSergey Zigachev 		for (count = table_info->vdd_dep_on_sclk->count - 1;
2934b843c749SSergey Zigachev 				count >= 0; count--) {
2935b843c749SSergey Zigachev 			if (stable_pstate_sclk >=
2936b843c749SSergey Zigachev 					table_info->vdd_dep_on_sclk->entries[count].clk) {
2937b843c749SSergey Zigachev 				stable_pstate_sclk =
2938b843c749SSergey Zigachev 						table_info->vdd_dep_on_sclk->entries[count].clk;
2939b843c749SSergey Zigachev 				break;
2940b843c749SSergey Zigachev 			}
2941b843c749SSergey Zigachev 		}
2942b843c749SSergey Zigachev 
2943b843c749SSergey Zigachev 		if (count < 0)
2944b843c749SSergey Zigachev 			stable_pstate_sclk = table_info->vdd_dep_on_sclk->entries[0].clk;
2945b843c749SSergey Zigachev 
2946b843c749SSergey Zigachev 		stable_pstate_mclk = max_limits->mclk;
2947b843c749SSergey Zigachev 
2948b843c749SSergey Zigachev 		minimum_clocks.engineClock = stable_pstate_sclk;
2949b843c749SSergey Zigachev 		minimum_clocks.memoryClock = stable_pstate_mclk;
2950b843c749SSergey Zigachev 	}
2951b843c749SSergey Zigachev 
2952b843c749SSergey Zigachev 	disable_mclk_switching_for_frame_lock = phm_cap_enabled(
2953b843c749SSergey Zigachev 				    hwmgr->platform_descriptor.platformCaps,
2954b843c749SSergey Zigachev 				    PHM_PlatformCaps_DisableMclkSwitchingForFrameLock);
2955b843c749SSergey Zigachev 
2956b843c749SSergey Zigachev 
2957b843c749SSergey Zigachev 	if (hwmgr->display_config->num_display == 0)
2958b843c749SSergey Zigachev 		disable_mclk_switching = false;
2959b843c749SSergey Zigachev 	else
2960b843c749SSergey Zigachev 		disable_mclk_switching = ((1 < hwmgr->display_config->num_display) ||
2961b843c749SSergey Zigachev 					  disable_mclk_switching_for_frame_lock ||
2962b843c749SSergey Zigachev 					  smu7_vblank_too_short(hwmgr, hwmgr->display_config->min_vblank_time));
2963b843c749SSergey Zigachev 
2964b843c749SSergey Zigachev 	sclk = smu7_ps->performance_levels[0].engine_clock;
2965b843c749SSergey Zigachev 	mclk = smu7_ps->performance_levels[0].memory_clock;
2966b843c749SSergey Zigachev 
2967b843c749SSergey Zigachev 	if (disable_mclk_switching)
2968b843c749SSergey Zigachev 		mclk = smu7_ps->performance_levels
2969b843c749SSergey Zigachev 		[smu7_ps->performance_level_count - 1].memory_clock;
2970b843c749SSergey Zigachev 
2971b843c749SSergey Zigachev 	if (sclk < minimum_clocks.engineClock)
2972b843c749SSergey Zigachev 		sclk = (minimum_clocks.engineClock > max_limits->sclk) ?
2973b843c749SSergey Zigachev 				max_limits->sclk : minimum_clocks.engineClock;
2974b843c749SSergey Zigachev 
2975b843c749SSergey Zigachev 	if (mclk < minimum_clocks.memoryClock)
2976b843c749SSergey Zigachev 		mclk = (minimum_clocks.memoryClock > max_limits->mclk) ?
2977b843c749SSergey Zigachev 				max_limits->mclk : minimum_clocks.memoryClock;
2978b843c749SSergey Zigachev 
2979b843c749SSergey Zigachev 	smu7_ps->performance_levels[0].engine_clock = sclk;
2980b843c749SSergey Zigachev 	smu7_ps->performance_levels[0].memory_clock = mclk;
2981b843c749SSergey Zigachev 
2982b843c749SSergey Zigachev 	smu7_ps->performance_levels[1].engine_clock =
2983b843c749SSergey Zigachev 		(smu7_ps->performance_levels[1].engine_clock >=
2984b843c749SSergey Zigachev 				smu7_ps->performance_levels[0].engine_clock) ?
2985b843c749SSergey Zigachev 						smu7_ps->performance_levels[1].engine_clock :
2986b843c749SSergey Zigachev 						smu7_ps->performance_levels[0].engine_clock;
2987b843c749SSergey Zigachev 
2988b843c749SSergey Zigachev 	if (disable_mclk_switching) {
2989b843c749SSergey Zigachev 		if (mclk < smu7_ps->performance_levels[1].memory_clock)
2990b843c749SSergey Zigachev 			mclk = smu7_ps->performance_levels[1].memory_clock;
2991b843c749SSergey Zigachev 
2992b843c749SSergey Zigachev 		smu7_ps->performance_levels[0].memory_clock = mclk;
2993b843c749SSergey Zigachev 		smu7_ps->performance_levels[1].memory_clock = mclk;
2994b843c749SSergey Zigachev 	} else {
2995b843c749SSergey Zigachev 		if (smu7_ps->performance_levels[1].memory_clock <
2996b843c749SSergey Zigachev 				smu7_ps->performance_levels[0].memory_clock)
2997b843c749SSergey Zigachev 			smu7_ps->performance_levels[1].memory_clock =
2998b843c749SSergey Zigachev 					smu7_ps->performance_levels[0].memory_clock;
2999b843c749SSergey Zigachev 	}
3000b843c749SSergey Zigachev 
3001b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3002b843c749SSergey Zigachev 			PHM_PlatformCaps_StablePState)) {
3003b843c749SSergey Zigachev 		for (i = 0; i < smu7_ps->performance_level_count; i++) {
3004b843c749SSergey Zigachev 			smu7_ps->performance_levels[i].engine_clock = stable_pstate_sclk;
3005b843c749SSergey Zigachev 			smu7_ps->performance_levels[i].memory_clock = stable_pstate_mclk;
3006b843c749SSergey Zigachev 			smu7_ps->performance_levels[i].pcie_gen = data->pcie_gen_performance.max;
3007b843c749SSergey Zigachev 			smu7_ps->performance_levels[i].pcie_lane = data->pcie_gen_performance.max;
3008b843c749SSergey Zigachev 		}
3009b843c749SSergey Zigachev 	}
3010b843c749SSergey Zigachev 	return 0;
3011b843c749SSergey Zigachev }
3012b843c749SSergey Zigachev 
3013b843c749SSergey Zigachev 
smu7_dpm_get_mclk(struct pp_hwmgr * hwmgr,bool low)3014b843c749SSergey Zigachev static uint32_t smu7_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
3015b843c749SSergey Zigachev {
3016b843c749SSergey Zigachev 	struct pp_power_state  *ps;
3017b843c749SSergey Zigachev 	struct smu7_power_state  *smu7_ps;
3018b843c749SSergey Zigachev 
3019b843c749SSergey Zigachev 	if (hwmgr == NULL)
3020b843c749SSergey Zigachev 		return -EINVAL;
3021b843c749SSergey Zigachev 
3022b843c749SSergey Zigachev 	ps = hwmgr->request_ps;
3023b843c749SSergey Zigachev 
3024b843c749SSergey Zigachev 	if (ps == NULL)
3025b843c749SSergey Zigachev 		return -EINVAL;
3026b843c749SSergey Zigachev 
3027b843c749SSergey Zigachev 	smu7_ps = cast_phw_smu7_power_state(&ps->hardware);
3028b843c749SSergey Zigachev 
3029b843c749SSergey Zigachev 	if (low)
3030b843c749SSergey Zigachev 		return smu7_ps->performance_levels[0].memory_clock;
3031b843c749SSergey Zigachev 	else
3032b843c749SSergey Zigachev 		return smu7_ps->performance_levels
3033b843c749SSergey Zigachev 				[smu7_ps->performance_level_count-1].memory_clock;
3034b843c749SSergey Zigachev }
3035b843c749SSergey Zigachev 
smu7_dpm_get_sclk(struct pp_hwmgr * hwmgr,bool low)3036b843c749SSergey Zigachev static uint32_t smu7_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
3037b843c749SSergey Zigachev {
3038b843c749SSergey Zigachev 	struct pp_power_state  *ps;
3039b843c749SSergey Zigachev 	struct smu7_power_state  *smu7_ps;
3040b843c749SSergey Zigachev 
3041b843c749SSergey Zigachev 	if (hwmgr == NULL)
3042b843c749SSergey Zigachev 		return -EINVAL;
3043b843c749SSergey Zigachev 
3044b843c749SSergey Zigachev 	ps = hwmgr->request_ps;
3045b843c749SSergey Zigachev 
3046b843c749SSergey Zigachev 	if (ps == NULL)
3047b843c749SSergey Zigachev 		return -EINVAL;
3048b843c749SSergey Zigachev 
3049b843c749SSergey Zigachev 	smu7_ps = cast_phw_smu7_power_state(&ps->hardware);
3050b843c749SSergey Zigachev 
3051b843c749SSergey Zigachev 	if (low)
3052b843c749SSergey Zigachev 		return smu7_ps->performance_levels[0].engine_clock;
3053b843c749SSergey Zigachev 	else
3054b843c749SSergey Zigachev 		return smu7_ps->performance_levels
3055b843c749SSergey Zigachev 				[smu7_ps->performance_level_count-1].engine_clock;
3056b843c749SSergey Zigachev }
3057b843c749SSergey Zigachev 
smu7_dpm_patch_boot_state(struct pp_hwmgr * hwmgr,struct pp_hw_power_state * hw_ps)3058b843c749SSergey Zigachev static int smu7_dpm_patch_boot_state(struct pp_hwmgr *hwmgr,
3059b843c749SSergey Zigachev 					struct pp_hw_power_state *hw_ps)
3060b843c749SSergey Zigachev {
3061b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3062b843c749SSergey Zigachev 	struct smu7_power_state *ps = (struct smu7_power_state *)hw_ps;
3063b843c749SSergey Zigachev 	ATOM_FIRMWARE_INFO_V2_2 *fw_info;
3064b843c749SSergey Zigachev 	uint16_t size;
3065b843c749SSergey Zigachev 	uint8_t frev, crev;
3066b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
3067b843c749SSergey Zigachev 
3068b843c749SSergey Zigachev 	/* First retrieve the Boot clocks and VDDC from the firmware info table.
3069b843c749SSergey Zigachev 	 * We assume here that fw_info is unchanged if this call fails.
3070b843c749SSergey Zigachev 	 */
3071b843c749SSergey Zigachev 	fw_info = (ATOM_FIRMWARE_INFO_V2_2 *)smu_atom_get_data_table(hwmgr->adev, index,
3072b843c749SSergey Zigachev 			&size, &frev, &crev);
3073b843c749SSergey Zigachev 	if (!fw_info)
3074b843c749SSergey Zigachev 		/* During a test, there is no firmware info table. */
3075b843c749SSergey Zigachev 		return 0;
3076b843c749SSergey Zigachev 
3077b843c749SSergey Zigachev 	/* Patch the state. */
3078b843c749SSergey Zigachev 	data->vbios_boot_state.sclk_bootup_value =
3079b843c749SSergey Zigachev 			le32_to_cpu(fw_info->ulDefaultEngineClock);
3080b843c749SSergey Zigachev 	data->vbios_boot_state.mclk_bootup_value =
3081b843c749SSergey Zigachev 			le32_to_cpu(fw_info->ulDefaultMemoryClock);
3082b843c749SSergey Zigachev 	data->vbios_boot_state.mvdd_bootup_value =
3083b843c749SSergey Zigachev 			le16_to_cpu(fw_info->usBootUpMVDDCVoltage);
3084b843c749SSergey Zigachev 	data->vbios_boot_state.vddc_bootup_value =
3085b843c749SSergey Zigachev 			le16_to_cpu(fw_info->usBootUpVDDCVoltage);
3086b843c749SSergey Zigachev 	data->vbios_boot_state.vddci_bootup_value =
3087b843c749SSergey Zigachev 			le16_to_cpu(fw_info->usBootUpVDDCIVoltage);
3088b843c749SSergey Zigachev 	data->vbios_boot_state.pcie_gen_bootup_value =
3089b843c749SSergey Zigachev 			smu7_get_current_pcie_speed(hwmgr);
3090b843c749SSergey Zigachev 
3091b843c749SSergey Zigachev 	data->vbios_boot_state.pcie_lane_bootup_value =
3092b843c749SSergey Zigachev 			(uint16_t)smu7_get_current_pcie_lane_number(hwmgr);
3093b843c749SSergey Zigachev 
3094b843c749SSergey Zigachev 	/* set boot power state */
3095b843c749SSergey Zigachev 	ps->performance_levels[0].memory_clock = data->vbios_boot_state.mclk_bootup_value;
3096b843c749SSergey Zigachev 	ps->performance_levels[0].engine_clock = data->vbios_boot_state.sclk_bootup_value;
3097b843c749SSergey Zigachev 	ps->performance_levels[0].pcie_gen = data->vbios_boot_state.pcie_gen_bootup_value;
3098b843c749SSergey Zigachev 	ps->performance_levels[0].pcie_lane = data->vbios_boot_state.pcie_lane_bootup_value;
3099b843c749SSergey Zigachev 
3100b843c749SSergey Zigachev 	return 0;
3101b843c749SSergey Zigachev }
3102b843c749SSergey Zigachev 
smu7_get_number_of_powerplay_table_entries(struct pp_hwmgr * hwmgr)3103b843c749SSergey Zigachev static int smu7_get_number_of_powerplay_table_entries(struct pp_hwmgr *hwmgr)
3104b843c749SSergey Zigachev {
3105b843c749SSergey Zigachev 	int result;
3106b843c749SSergey Zigachev 	unsigned long ret = 0;
3107b843c749SSergey Zigachev 
3108b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V0) {
3109b843c749SSergey Zigachev 		result = pp_tables_get_num_of_entries(hwmgr, &ret);
3110b843c749SSergey Zigachev 		return result ? 0 : ret;
3111b843c749SSergey Zigachev 	} else if (hwmgr->pp_table_version == PP_TABLE_V1) {
3112b843c749SSergey Zigachev 		result = get_number_of_powerplay_table_entries_v1_0(hwmgr);
3113b843c749SSergey Zigachev 		return result;
3114b843c749SSergey Zigachev 	}
3115b843c749SSergey Zigachev 	return 0;
3116b843c749SSergey Zigachev }
3117b843c749SSergey Zigachev 
smu7_get_pp_table_entry_callback_func_v1(struct pp_hwmgr * hwmgr,void * state,struct pp_power_state * power_state,void * pp_table,uint32_t classification_flag)3118b843c749SSergey Zigachev static int smu7_get_pp_table_entry_callback_func_v1(struct pp_hwmgr *hwmgr,
3119b843c749SSergey Zigachev 		void *state, struct pp_power_state *power_state,
3120b843c749SSergey Zigachev 		void *pp_table, uint32_t classification_flag)
3121b843c749SSergey Zigachev {
3122b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3123b843c749SSergey Zigachev 	struct smu7_power_state  *smu7_power_state =
3124b843c749SSergey Zigachev 			(struct smu7_power_state *)(&(power_state->hardware));
3125b843c749SSergey Zigachev 	struct smu7_performance_level *performance_level;
3126b843c749SSergey Zigachev 	ATOM_Tonga_State *state_entry = (ATOM_Tonga_State *)state;
3127b843c749SSergey Zigachev 	ATOM_Tonga_POWERPLAYTABLE *powerplay_table =
3128b843c749SSergey Zigachev 			(ATOM_Tonga_POWERPLAYTABLE *)pp_table;
3129b843c749SSergey Zigachev 	PPTable_Generic_SubTable_Header *sclk_dep_table =
3130b843c749SSergey Zigachev 			(PPTable_Generic_SubTable_Header *)
3131b843c749SSergey Zigachev 			(((unsigned long)powerplay_table) +
3132b843c749SSergey Zigachev 				le16_to_cpu(powerplay_table->usSclkDependencyTableOffset));
3133b843c749SSergey Zigachev 
3134b843c749SSergey Zigachev 	ATOM_Tonga_MCLK_Dependency_Table *mclk_dep_table =
3135b843c749SSergey Zigachev 			(ATOM_Tonga_MCLK_Dependency_Table *)
3136b843c749SSergey Zigachev 			(((unsigned long)powerplay_table) +
3137b843c749SSergey Zigachev 				le16_to_cpu(powerplay_table->usMclkDependencyTableOffset));
3138b843c749SSergey Zigachev 
3139b843c749SSergey Zigachev 	/* The following fields are not initialized here: id orderedList allStatesList */
3140b843c749SSergey Zigachev 	power_state->classification.ui_label =
3141b843c749SSergey Zigachev 			(le16_to_cpu(state_entry->usClassification) &
3142b843c749SSergey Zigachev 			ATOM_PPLIB_CLASSIFICATION_UI_MASK) >>
3143b843c749SSergey Zigachev 			ATOM_PPLIB_CLASSIFICATION_UI_SHIFT;
3144b843c749SSergey Zigachev 	power_state->classification.flags = classification_flag;
3145b843c749SSergey Zigachev 	/* NOTE: There is a classification2 flag in BIOS that is not being used right now */
3146b843c749SSergey Zigachev 
3147b843c749SSergey Zigachev 	power_state->classification.temporary_state = false;
3148b843c749SSergey Zigachev 	power_state->classification.to_be_deleted = false;
3149b843c749SSergey Zigachev 
3150b843c749SSergey Zigachev 	power_state->validation.disallowOnDC =
3151b843c749SSergey Zigachev 			(0 != (le32_to_cpu(state_entry->ulCapsAndSettings) &
3152b843c749SSergey Zigachev 					ATOM_Tonga_DISALLOW_ON_DC));
3153b843c749SSergey Zigachev 
3154b843c749SSergey Zigachev 	power_state->pcie.lanes = 0;
3155b843c749SSergey Zigachev 
3156b843c749SSergey Zigachev 	power_state->display.disableFrameModulation = false;
3157b843c749SSergey Zigachev 	power_state->display.limitRefreshrate = false;
3158b843c749SSergey Zigachev 	power_state->display.enableVariBright =
3159b843c749SSergey Zigachev 			(0 != (le32_to_cpu(state_entry->ulCapsAndSettings) &
3160b843c749SSergey Zigachev 					ATOM_Tonga_ENABLE_VARIBRIGHT));
3161b843c749SSergey Zigachev 
3162b843c749SSergey Zigachev 	power_state->validation.supportedPowerLevels = 0;
3163b843c749SSergey Zigachev 	power_state->uvd_clocks.VCLK = 0;
3164b843c749SSergey Zigachev 	power_state->uvd_clocks.DCLK = 0;
3165b843c749SSergey Zigachev 	power_state->temperatures.min = 0;
3166b843c749SSergey Zigachev 	power_state->temperatures.max = 0;
3167b843c749SSergey Zigachev 
3168b843c749SSergey Zigachev 	performance_level = &(smu7_power_state->performance_levels
3169b843c749SSergey Zigachev 			[smu7_power_state->performance_level_count++]);
3170b843c749SSergey Zigachev 
3171b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
3172b843c749SSergey Zigachev 			(smu7_power_state->performance_level_count < smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_GRAPHICS)),
3173b843c749SSergey Zigachev 			"Performance levels exceeds SMC limit!",
3174b843c749SSergey Zigachev 			return -EINVAL);
3175b843c749SSergey Zigachev 
3176b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
3177b843c749SSergey Zigachev 			(smu7_power_state->performance_level_count <=
3178b843c749SSergey Zigachev 					hwmgr->platform_descriptor.hardwareActivityPerformanceLevels),
3179b843c749SSergey Zigachev 			"Performance levels exceeds Driver limit!",
3180b843c749SSergey Zigachev 			return -EINVAL);
3181b843c749SSergey Zigachev 
3182b843c749SSergey Zigachev 	/* Performance levels are arranged from low to high. */
3183b843c749SSergey Zigachev 	performance_level->memory_clock = mclk_dep_table->entries
3184b843c749SSergey Zigachev 			[state_entry->ucMemoryClockIndexLow].ulMclk;
3185b843c749SSergey Zigachev 	if (sclk_dep_table->ucRevId == 0)
3186b843c749SSergey Zigachev 		performance_level->engine_clock = ((ATOM_Tonga_SCLK_Dependency_Table *)sclk_dep_table)->entries
3187b843c749SSergey Zigachev 			[state_entry->ucEngineClockIndexLow].ulSclk;
3188b843c749SSergey Zigachev 	else if (sclk_dep_table->ucRevId == 1)
3189b843c749SSergey Zigachev 		performance_level->engine_clock = ((ATOM_Polaris_SCLK_Dependency_Table *)sclk_dep_table)->entries
3190b843c749SSergey Zigachev 			[state_entry->ucEngineClockIndexLow].ulSclk;
3191b843c749SSergey Zigachev 	performance_level->pcie_gen = get_pcie_gen_support(data->pcie_gen_cap,
3192b843c749SSergey Zigachev 			state_entry->ucPCIEGenLow);
3193b843c749SSergey Zigachev 	performance_level->pcie_lane = get_pcie_lane_support(data->pcie_lane_cap,
3194b843c749SSergey Zigachev 			state_entry->ucPCIELaneLow);
3195b843c749SSergey Zigachev 
3196b843c749SSergey Zigachev 	performance_level = &(smu7_power_state->performance_levels
3197b843c749SSergey Zigachev 			[smu7_power_state->performance_level_count++]);
3198b843c749SSergey Zigachev 	performance_level->memory_clock = mclk_dep_table->entries
3199b843c749SSergey Zigachev 			[state_entry->ucMemoryClockIndexHigh].ulMclk;
3200b843c749SSergey Zigachev 
3201b843c749SSergey Zigachev 	if (sclk_dep_table->ucRevId == 0)
3202b843c749SSergey Zigachev 		performance_level->engine_clock = ((ATOM_Tonga_SCLK_Dependency_Table *)sclk_dep_table)->entries
3203b843c749SSergey Zigachev 			[state_entry->ucEngineClockIndexHigh].ulSclk;
3204b843c749SSergey Zigachev 	else if (sclk_dep_table->ucRevId == 1)
3205b843c749SSergey Zigachev 		performance_level->engine_clock = ((ATOM_Polaris_SCLK_Dependency_Table *)sclk_dep_table)->entries
3206b843c749SSergey Zigachev 			[state_entry->ucEngineClockIndexHigh].ulSclk;
3207b843c749SSergey Zigachev 
3208b843c749SSergey Zigachev 	performance_level->pcie_gen = get_pcie_gen_support(data->pcie_gen_cap,
3209b843c749SSergey Zigachev 			state_entry->ucPCIEGenHigh);
3210b843c749SSergey Zigachev 	performance_level->pcie_lane = get_pcie_lane_support(data->pcie_lane_cap,
3211b843c749SSergey Zigachev 			state_entry->ucPCIELaneHigh);
3212b843c749SSergey Zigachev 
3213b843c749SSergey Zigachev 	return 0;
3214b843c749SSergey Zigachev }
3215b843c749SSergey Zigachev 
smu7_get_pp_table_entry_v1(struct pp_hwmgr * hwmgr,unsigned long entry_index,struct pp_power_state * state)3216b843c749SSergey Zigachev static int smu7_get_pp_table_entry_v1(struct pp_hwmgr *hwmgr,
3217b843c749SSergey Zigachev 		unsigned long entry_index, struct pp_power_state *state)
3218b843c749SSergey Zigachev {
3219b843c749SSergey Zigachev 	int result;
3220b843c749SSergey Zigachev 	struct smu7_power_state *ps;
3221b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3222b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
3223b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
3224b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table =
3225b843c749SSergey Zigachev 			table_info->vdd_dep_on_mclk;
3226b843c749SSergey Zigachev 
3227b843c749SSergey Zigachev 	state->hardware.magic = PHM_VIslands_Magic;
3228b843c749SSergey Zigachev 
3229b843c749SSergey Zigachev 	ps = (struct smu7_power_state *)(&state->hardware);
3230b843c749SSergey Zigachev 
3231b843c749SSergey Zigachev 	result = get_powerplay_table_entry_v1_0(hwmgr, entry_index, state,
3232b843c749SSergey Zigachev 			smu7_get_pp_table_entry_callback_func_v1);
3233b843c749SSergey Zigachev 
3234b843c749SSergey Zigachev 	/* This is the earliest time we have all the dependency table and the VBIOS boot state
3235b843c749SSergey Zigachev 	 * as PP_Tables_GetPowerPlayTableEntry retrieves the VBIOS boot state
3236b843c749SSergey Zigachev 	 * if there is only one VDDCI/MCLK level, check if it's the same as VBIOS boot state
3237b843c749SSergey Zigachev 	 */
3238b843c749SSergey Zigachev 	if (dep_mclk_table != NULL && dep_mclk_table->count == 1) {
3239b843c749SSergey Zigachev 		if (dep_mclk_table->entries[0].clk !=
3240b843c749SSergey Zigachev 				data->vbios_boot_state.mclk_bootup_value)
3241b843c749SSergey Zigachev 			pr_debug("Single MCLK entry VDDCI/MCLK dependency table "
3242b843c749SSergey Zigachev 					"does not match VBIOS boot MCLK level");
3243b843c749SSergey Zigachev 		if (dep_mclk_table->entries[0].vddci !=
3244b843c749SSergey Zigachev 				data->vbios_boot_state.vddci_bootup_value)
3245b843c749SSergey Zigachev 			pr_debug("Single VDDCI entry VDDCI/MCLK dependency table "
3246b843c749SSergey Zigachev 					"does not match VBIOS boot VDDCI level");
3247b843c749SSergey Zigachev 	}
3248b843c749SSergey Zigachev 
3249b843c749SSergey Zigachev 	/* set DC compatible flag if this state supports DC */
3250b843c749SSergey Zigachev 	if (!state->validation.disallowOnDC)
3251b843c749SSergey Zigachev 		ps->dc_compatible = true;
3252b843c749SSergey Zigachev 
3253b843c749SSergey Zigachev 	if (state->classification.flags & PP_StateClassificationFlag_ACPI)
3254b843c749SSergey Zigachev 		data->acpi_pcie_gen = ps->performance_levels[0].pcie_gen;
3255b843c749SSergey Zigachev 
3256b843c749SSergey Zigachev 	ps->uvd_clks.vclk = state->uvd_clocks.VCLK;
3257b843c749SSergey Zigachev 	ps->uvd_clks.dclk = state->uvd_clocks.DCLK;
3258b843c749SSergey Zigachev 
3259b843c749SSergey Zigachev 	if (!result) {
3260b843c749SSergey Zigachev 		uint32_t i;
3261b843c749SSergey Zigachev 
3262b843c749SSergey Zigachev 		switch (state->classification.ui_label) {
3263b843c749SSergey Zigachev 		case PP_StateUILabel_Performance:
3264b843c749SSergey Zigachev 			data->use_pcie_performance_levels = true;
3265b843c749SSergey Zigachev 			for (i = 0; i < ps->performance_level_count; i++) {
3266b843c749SSergey Zigachev 				if (data->pcie_gen_performance.max <
3267b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3268b843c749SSergey Zigachev 					data->pcie_gen_performance.max =
3269b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3270b843c749SSergey Zigachev 
3271b843c749SSergey Zigachev 				if (data->pcie_gen_performance.min >
3272b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3273b843c749SSergey Zigachev 					data->pcie_gen_performance.min =
3274b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3275b843c749SSergey Zigachev 
3276b843c749SSergey Zigachev 				if (data->pcie_lane_performance.max <
3277b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3278b843c749SSergey Zigachev 					data->pcie_lane_performance.max =
3279b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3280b843c749SSergey Zigachev 				if (data->pcie_lane_performance.min >
3281b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3282b843c749SSergey Zigachev 					data->pcie_lane_performance.min =
3283b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3284b843c749SSergey Zigachev 			}
3285b843c749SSergey Zigachev 			break;
3286b843c749SSergey Zigachev 		case PP_StateUILabel_Battery:
3287b843c749SSergey Zigachev 			data->use_pcie_power_saving_levels = true;
3288b843c749SSergey Zigachev 
3289b843c749SSergey Zigachev 			for (i = 0; i < ps->performance_level_count; i++) {
3290b843c749SSergey Zigachev 				if (data->pcie_gen_power_saving.max <
3291b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3292b843c749SSergey Zigachev 					data->pcie_gen_power_saving.max =
3293b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3294b843c749SSergey Zigachev 
3295b843c749SSergey Zigachev 				if (data->pcie_gen_power_saving.min >
3296b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3297b843c749SSergey Zigachev 					data->pcie_gen_power_saving.min =
3298b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3299b843c749SSergey Zigachev 
3300b843c749SSergey Zigachev 				if (data->pcie_lane_power_saving.max <
3301b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3302b843c749SSergey Zigachev 					data->pcie_lane_power_saving.max =
3303b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3304b843c749SSergey Zigachev 
3305b843c749SSergey Zigachev 				if (data->pcie_lane_power_saving.min >
3306b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3307b843c749SSergey Zigachev 					data->pcie_lane_power_saving.min =
3308b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3309b843c749SSergey Zigachev 			}
3310b843c749SSergey Zigachev 			break;
3311b843c749SSergey Zigachev 		default:
3312b843c749SSergey Zigachev 			break;
3313b843c749SSergey Zigachev 		}
3314b843c749SSergey Zigachev 	}
3315b843c749SSergey Zigachev 	return 0;
3316b843c749SSergey Zigachev }
3317b843c749SSergey Zigachev 
smu7_get_pp_table_entry_callback_func_v0(struct pp_hwmgr * hwmgr,struct pp_hw_power_state * power_state,unsigned int index,const void * clock_info)3318b843c749SSergey Zigachev static int smu7_get_pp_table_entry_callback_func_v0(struct pp_hwmgr *hwmgr,
3319b843c749SSergey Zigachev 					struct pp_hw_power_state *power_state,
3320b843c749SSergey Zigachev 					unsigned int index, const void *clock_info)
3321b843c749SSergey Zigachev {
3322b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3323b843c749SSergey Zigachev 	struct smu7_power_state  *ps = cast_phw_smu7_power_state(power_state);
3324b843c749SSergey Zigachev 	const ATOM_PPLIB_CI_CLOCK_INFO *visland_clk_info = clock_info;
3325b843c749SSergey Zigachev 	struct smu7_performance_level *performance_level;
3326b843c749SSergey Zigachev 	uint32_t engine_clock, memory_clock;
3327b843c749SSergey Zigachev 	uint16_t pcie_gen_from_bios;
3328b843c749SSergey Zigachev 
3329b843c749SSergey Zigachev 	engine_clock = visland_clk_info->ucEngineClockHigh << 16 | visland_clk_info->usEngineClockLow;
3330b843c749SSergey Zigachev 	memory_clock = visland_clk_info->ucMemoryClockHigh << 16 | visland_clk_info->usMemoryClockLow;
3331b843c749SSergey Zigachev 
3332b843c749SSergey Zigachev 	if (!(data->mc_micro_code_feature & DISABLE_MC_LOADMICROCODE) && memory_clock > data->highest_mclk)
3333b843c749SSergey Zigachev 		data->highest_mclk = memory_clock;
3334b843c749SSergey Zigachev 
3335b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
3336b843c749SSergey Zigachev 			(ps->performance_level_count < smum_get_mac_definition(hwmgr, SMU_MAX_LEVELS_GRAPHICS)),
3337b843c749SSergey Zigachev 			"Performance levels exceeds SMC limit!",
3338b843c749SSergey Zigachev 			return -EINVAL);
3339b843c749SSergey Zigachev 
3340b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(
3341b843c749SSergey Zigachev 			(ps->performance_level_count <
3342b843c749SSergey Zigachev 					hwmgr->platform_descriptor.hardwareActivityPerformanceLevels),
3343b843c749SSergey Zigachev 			"Performance levels exceeds Driver limit, Skip!",
3344b843c749SSergey Zigachev 			return 0);
3345b843c749SSergey Zigachev 
3346b843c749SSergey Zigachev 	performance_level = &(ps->performance_levels
3347b843c749SSergey Zigachev 			[ps->performance_level_count++]);
3348b843c749SSergey Zigachev 
3349b843c749SSergey Zigachev 	/* Performance levels are arranged from low to high. */
3350b843c749SSergey Zigachev 	performance_level->memory_clock = memory_clock;
3351b843c749SSergey Zigachev 	performance_level->engine_clock = engine_clock;
3352b843c749SSergey Zigachev 
3353b843c749SSergey Zigachev 	pcie_gen_from_bios = visland_clk_info->ucPCIEGen;
3354b843c749SSergey Zigachev 
3355b843c749SSergey Zigachev 	performance_level->pcie_gen = get_pcie_gen_support(data->pcie_gen_cap, pcie_gen_from_bios);
3356b843c749SSergey Zigachev 	performance_level->pcie_lane = get_pcie_lane_support(data->pcie_lane_cap, visland_clk_info->usPCIELane);
3357b843c749SSergey Zigachev 
3358b843c749SSergey Zigachev 	return 0;
3359b843c749SSergey Zigachev }
3360b843c749SSergey Zigachev 
smu7_get_pp_table_entry_v0(struct pp_hwmgr * hwmgr,unsigned long entry_index,struct pp_power_state * state)3361b843c749SSergey Zigachev static int smu7_get_pp_table_entry_v0(struct pp_hwmgr *hwmgr,
3362b843c749SSergey Zigachev 		unsigned long entry_index, struct pp_power_state *state)
3363b843c749SSergey Zigachev {
3364b843c749SSergey Zigachev 	int result;
3365b843c749SSergey Zigachev 	struct smu7_power_state *ps;
3366b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3367b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *dep_mclk_table =
3368b843c749SSergey Zigachev 			hwmgr->dyn_state.vddci_dependency_on_mclk;
3369b843c749SSergey Zigachev 
3370b843c749SSergey Zigachev 	memset(&state->hardware, 0x00, sizeof(struct pp_hw_power_state));
3371b843c749SSergey Zigachev 
3372b843c749SSergey Zigachev 	state->hardware.magic = PHM_VIslands_Magic;
3373b843c749SSergey Zigachev 
3374b843c749SSergey Zigachev 	ps = (struct smu7_power_state *)(&state->hardware);
3375b843c749SSergey Zigachev 
3376b843c749SSergey Zigachev 	result = pp_tables_get_entry(hwmgr, entry_index, state,
3377b843c749SSergey Zigachev 			smu7_get_pp_table_entry_callback_func_v0);
3378b843c749SSergey Zigachev 
3379b843c749SSergey Zigachev 	/*
3380b843c749SSergey Zigachev 	 * This is the earliest time we have all the dependency table
3381b843c749SSergey Zigachev 	 * and the VBIOS boot state as
3382b843c749SSergey Zigachev 	 * PP_Tables_GetPowerPlayTableEntry retrieves the VBIOS boot
3383b843c749SSergey Zigachev 	 * state if there is only one VDDCI/MCLK level, check if it's
3384b843c749SSergey Zigachev 	 * the same as VBIOS boot state
3385b843c749SSergey Zigachev 	 */
3386b843c749SSergey Zigachev 	if (dep_mclk_table != NULL && dep_mclk_table->count == 1) {
3387b843c749SSergey Zigachev 		if (dep_mclk_table->entries[0].clk !=
3388b843c749SSergey Zigachev 				data->vbios_boot_state.mclk_bootup_value)
3389b843c749SSergey Zigachev 			pr_debug("Single MCLK entry VDDCI/MCLK dependency table "
3390b843c749SSergey Zigachev 					"does not match VBIOS boot MCLK level");
3391b843c749SSergey Zigachev 		if (dep_mclk_table->entries[0].v !=
3392b843c749SSergey Zigachev 				data->vbios_boot_state.vddci_bootup_value)
3393b843c749SSergey Zigachev 			pr_debug("Single VDDCI entry VDDCI/MCLK dependency table "
3394b843c749SSergey Zigachev 					"does not match VBIOS boot VDDCI level");
3395b843c749SSergey Zigachev 	}
3396b843c749SSergey Zigachev 
3397b843c749SSergey Zigachev 	/* set DC compatible flag if this state supports DC */
3398b843c749SSergey Zigachev 	if (!state->validation.disallowOnDC)
3399b843c749SSergey Zigachev 		ps->dc_compatible = true;
3400b843c749SSergey Zigachev 
3401b843c749SSergey Zigachev 	if (state->classification.flags & PP_StateClassificationFlag_ACPI)
3402b843c749SSergey Zigachev 		data->acpi_pcie_gen = ps->performance_levels[0].pcie_gen;
3403b843c749SSergey Zigachev 
3404b843c749SSergey Zigachev 	ps->uvd_clks.vclk = state->uvd_clocks.VCLK;
3405b843c749SSergey Zigachev 	ps->uvd_clks.dclk = state->uvd_clocks.DCLK;
3406b843c749SSergey Zigachev 
3407b843c749SSergey Zigachev 	if (!result) {
3408b843c749SSergey Zigachev 		uint32_t i;
3409b843c749SSergey Zigachev 
3410b843c749SSergey Zigachev 		switch (state->classification.ui_label) {
3411b843c749SSergey Zigachev 		case PP_StateUILabel_Performance:
3412b843c749SSergey Zigachev 			data->use_pcie_performance_levels = true;
3413b843c749SSergey Zigachev 
3414b843c749SSergey Zigachev 			for (i = 0; i < ps->performance_level_count; i++) {
3415b843c749SSergey Zigachev 				if (data->pcie_gen_performance.max <
3416b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3417b843c749SSergey Zigachev 					data->pcie_gen_performance.max =
3418b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3419b843c749SSergey Zigachev 
3420b843c749SSergey Zigachev 				if (data->pcie_gen_performance.min >
3421b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3422b843c749SSergey Zigachev 					data->pcie_gen_performance.min =
3423b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3424b843c749SSergey Zigachev 
3425b843c749SSergey Zigachev 				if (data->pcie_lane_performance.max <
3426b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3427b843c749SSergey Zigachev 					data->pcie_lane_performance.max =
3428b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3429b843c749SSergey Zigachev 
3430b843c749SSergey Zigachev 				if (data->pcie_lane_performance.min >
3431b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3432b843c749SSergey Zigachev 					data->pcie_lane_performance.min =
3433b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3434b843c749SSergey Zigachev 			}
3435b843c749SSergey Zigachev 			break;
3436b843c749SSergey Zigachev 		case PP_StateUILabel_Battery:
3437b843c749SSergey Zigachev 			data->use_pcie_power_saving_levels = true;
3438b843c749SSergey Zigachev 
3439b843c749SSergey Zigachev 			for (i = 0; i < ps->performance_level_count; i++) {
3440b843c749SSergey Zigachev 				if (data->pcie_gen_power_saving.max <
3441b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3442b843c749SSergey Zigachev 					data->pcie_gen_power_saving.max =
3443b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3444b843c749SSergey Zigachev 
3445b843c749SSergey Zigachev 				if (data->pcie_gen_power_saving.min >
3446b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_gen)
3447b843c749SSergey Zigachev 					data->pcie_gen_power_saving.min =
3448b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_gen;
3449b843c749SSergey Zigachev 
3450b843c749SSergey Zigachev 				if (data->pcie_lane_power_saving.max <
3451b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3452b843c749SSergey Zigachev 					data->pcie_lane_power_saving.max =
3453b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3454b843c749SSergey Zigachev 
3455b843c749SSergey Zigachev 				if (data->pcie_lane_power_saving.min >
3456b843c749SSergey Zigachev 						ps->performance_levels[i].pcie_lane)
3457b843c749SSergey Zigachev 					data->pcie_lane_power_saving.min =
3458b843c749SSergey Zigachev 							ps->performance_levels[i].pcie_lane;
3459b843c749SSergey Zigachev 			}
3460b843c749SSergey Zigachev 			break;
3461b843c749SSergey Zigachev 		default:
3462b843c749SSergey Zigachev 			break;
3463b843c749SSergey Zigachev 		}
3464b843c749SSergey Zigachev 	}
3465b843c749SSergey Zigachev 	return 0;
3466b843c749SSergey Zigachev }
3467b843c749SSergey Zigachev 
smu7_get_pp_table_entry(struct pp_hwmgr * hwmgr,unsigned long entry_index,struct pp_power_state * state)3468b843c749SSergey Zigachev static int smu7_get_pp_table_entry(struct pp_hwmgr *hwmgr,
3469b843c749SSergey Zigachev 		unsigned long entry_index, struct pp_power_state *state)
3470b843c749SSergey Zigachev {
3471b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V0)
3472b843c749SSergey Zigachev 		return smu7_get_pp_table_entry_v0(hwmgr, entry_index, state);
3473b843c749SSergey Zigachev 	else if (hwmgr->pp_table_version == PP_TABLE_V1)
3474b843c749SSergey Zigachev 		return smu7_get_pp_table_entry_v1(hwmgr, entry_index, state);
3475b843c749SSergey Zigachev 
3476b843c749SSergey Zigachev 	return 0;
3477b843c749SSergey Zigachev }
3478b843c749SSergey Zigachev 
smu7_get_gpu_power(struct pp_hwmgr * hwmgr,u32 * query)3479b843c749SSergey Zigachev static int smu7_get_gpu_power(struct pp_hwmgr *hwmgr, u32 *query)
3480b843c749SSergey Zigachev {
3481b843c749SSergey Zigachev 	struct amdgpu_device *adev = hwmgr->adev;
3482b843c749SSergey Zigachev 	int i;
3483b843c749SSergey Zigachev 	u32 tmp = 0;
3484b843c749SSergey Zigachev 
3485b843c749SSergey Zigachev 	if (!query)
3486b843c749SSergey Zigachev 		return -EINVAL;
3487b843c749SSergey Zigachev 
3488b843c749SSergey Zigachev 	/*
3489b843c749SSergey Zigachev 	 * PPSMC_MSG_GetCurrPkgPwr is not supported on:
3490b843c749SSergey Zigachev 	 *  - Hawaii
3491b843c749SSergey Zigachev 	 *  - Bonaire
3492b843c749SSergey Zigachev 	 *  - Fiji
3493b843c749SSergey Zigachev 	 *  - Tonga
3494b843c749SSergey Zigachev 	 */
3495b843c749SSergey Zigachev 	if ((adev->asic_type != CHIP_HAWAII) &&
3496b843c749SSergey Zigachev 	    (adev->asic_type != CHIP_BONAIRE) &&
3497b843c749SSergey Zigachev 	    (adev->asic_type != CHIP_FIJI) &&
3498b843c749SSergey Zigachev 	    (adev->asic_type != CHIP_TONGA)) {
3499b843c749SSergey Zigachev 		smum_send_msg_to_smc_with_parameter(hwmgr, PPSMC_MSG_GetCurrPkgPwr, 0);
3500b843c749SSergey Zigachev 		tmp = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
3501b843c749SSergey Zigachev 		*query = tmp;
3502b843c749SSergey Zigachev 
3503b843c749SSergey Zigachev 		if (tmp != 0)
3504b843c749SSergey Zigachev 			return 0;
3505b843c749SSergey Zigachev 	}
3506b843c749SSergey Zigachev 
3507b843c749SSergey Zigachev 	smum_send_msg_to_smc(hwmgr, PPSMC_MSG_PmStatusLogStart);
3508b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3509b843c749SSergey Zigachev 							ixSMU_PM_STATUS_95, 0);
3510b843c749SSergey Zigachev 
3511b843c749SSergey Zigachev 	for (i = 0; i < 10; i++) {
3512b843c749SSergey Zigachev 		mdelay(500);
3513b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_PmStatusLogSample);
3514b843c749SSergey Zigachev 		tmp = cgs_read_ind_register(hwmgr->device,
3515b843c749SSergey Zigachev 						CGS_IND_REG__SMC,
3516b843c749SSergey Zigachev 						ixSMU_PM_STATUS_95);
3517b843c749SSergey Zigachev 		if (tmp != 0)
3518b843c749SSergey Zigachev 			break;
3519b843c749SSergey Zigachev 	}
3520b843c749SSergey Zigachev 	*query = tmp;
3521b843c749SSergey Zigachev 
3522b843c749SSergey Zigachev 	return 0;
3523b843c749SSergey Zigachev }
3524b843c749SSergey Zigachev 
smu7_read_sensor(struct pp_hwmgr * hwmgr,int idx,void * value,int * size)3525b843c749SSergey Zigachev static int smu7_read_sensor(struct pp_hwmgr *hwmgr, int idx,
3526b843c749SSergey Zigachev 			    void *value, int *size)
3527b843c749SSergey Zigachev {
3528b843c749SSergey Zigachev 	uint32_t sclk, mclk, activity_percent;
3529b843c749SSergey Zigachev 	uint32_t offset, val_vid;
3530b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3531b843c749SSergey Zigachev 
3532b843c749SSergey Zigachev 	/* size must be at least 4 bytes for all sensors */
3533b843c749SSergey Zigachev 	if (*size < 4)
3534b843c749SSergey Zigachev 		return -EINVAL;
3535b843c749SSergey Zigachev 
3536b843c749SSergey Zigachev 	switch (idx) {
3537b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_GFX_SCLK:
3538b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_API_GetSclkFrequency);
3539b843c749SSergey Zigachev 		sclk = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
3540b843c749SSergey Zigachev 		*((uint32_t *)value) = sclk;
3541b843c749SSergey Zigachev 		*size = 4;
3542b843c749SSergey Zigachev 		return 0;
3543b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_GFX_MCLK:
3544b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_API_GetMclkFrequency);
3545b843c749SSergey Zigachev 		mclk = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
3546b843c749SSergey Zigachev 		*((uint32_t *)value) = mclk;
3547b843c749SSergey Zigachev 		*size = 4;
3548b843c749SSergey Zigachev 		return 0;
3549b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_GPU_LOAD:
3550b843c749SSergey Zigachev 		offset = data->soft_regs_start + smum_get_offsetof(hwmgr,
3551b843c749SSergey Zigachev 								SMU_SoftRegisters,
3552b843c749SSergey Zigachev 								AverageGraphicsActivity);
3553b843c749SSergey Zigachev 
3554b843c749SSergey Zigachev 		activity_percent = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset);
3555b843c749SSergey Zigachev 		activity_percent += 0x80;
3556b843c749SSergey Zigachev 		activity_percent >>= 8;
3557b843c749SSergey Zigachev 		*((uint32_t *)value) = activity_percent > 100 ? 100 : activity_percent;
3558b843c749SSergey Zigachev 		*size = 4;
3559b843c749SSergey Zigachev 		return 0;
3560b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_GPU_TEMP:
3561b843c749SSergey Zigachev 		*((uint32_t *)value) = smu7_thermal_get_temperature(hwmgr);
3562b843c749SSergey Zigachev 		*size = 4;
3563b843c749SSergey Zigachev 		return 0;
3564b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_UVD_POWER:
3565b843c749SSergey Zigachev 		*((uint32_t *)value) = data->uvd_power_gated ? 0 : 1;
3566b843c749SSergey Zigachev 		*size = 4;
3567b843c749SSergey Zigachev 		return 0;
3568b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_VCE_POWER:
3569b843c749SSergey Zigachev 		*((uint32_t *)value) = data->vce_power_gated ? 0 : 1;
3570b843c749SSergey Zigachev 		*size = 4;
3571b843c749SSergey Zigachev 		return 0;
3572b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_GPU_POWER:
3573b843c749SSergey Zigachev 		return smu7_get_gpu_power(hwmgr, (uint32_t *)value);
3574b843c749SSergey Zigachev 	case AMDGPU_PP_SENSOR_VDDGFX:
3575b843c749SSergey Zigachev 		if ((data->vr_config & VRCONF_VDDGFX_MASK) ==
3576b843c749SSergey Zigachev 		    (VR_SVI2_PLANE_2 << VRCONF_VDDGFX_SHIFT))
3577b843c749SSergey Zigachev 			val_vid = PHM_READ_INDIRECT_FIELD(hwmgr->device,
3578b843c749SSergey Zigachev 					CGS_IND_REG__SMC, PWR_SVI2_STATUS, PLANE2_VID);
3579b843c749SSergey Zigachev 		else
3580b843c749SSergey Zigachev 			val_vid = PHM_READ_INDIRECT_FIELD(hwmgr->device,
3581b843c749SSergey Zigachev 					CGS_IND_REG__SMC, PWR_SVI2_STATUS, PLANE1_VID);
3582b843c749SSergey Zigachev 
3583b843c749SSergey Zigachev 		*((uint32_t *)value) = (uint32_t)convert_to_vddc(val_vid);
3584b843c749SSergey Zigachev 		return 0;
3585b843c749SSergey Zigachev 	default:
3586b843c749SSergey Zigachev 		return -EINVAL;
3587b843c749SSergey Zigachev 	}
3588b843c749SSergey Zigachev }
3589b843c749SSergey Zigachev 
smu7_find_dpm_states_clocks_in_dpm_table(struct pp_hwmgr * hwmgr,const void * input)3590b843c749SSergey Zigachev static int smu7_find_dpm_states_clocks_in_dpm_table(struct pp_hwmgr *hwmgr, const void *input)
3591b843c749SSergey Zigachev {
3592b843c749SSergey Zigachev 	const struct phm_set_power_state_input *states =
3593b843c749SSergey Zigachev 			(const struct phm_set_power_state_input *)input;
3594b843c749SSergey Zigachev 	const struct smu7_power_state *smu7_ps =
3595b843c749SSergey Zigachev 			cast_const_phw_smu7_power_state(states->pnew_state);
3596b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3597b843c749SSergey Zigachev 	struct smu7_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
3598b843c749SSergey Zigachev 	uint32_t sclk = smu7_ps->performance_levels
3599b843c749SSergey Zigachev 			[smu7_ps->performance_level_count - 1].engine_clock;
3600b843c749SSergey Zigachev 	struct smu7_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
3601b843c749SSergey Zigachev 	uint32_t mclk = smu7_ps->performance_levels
3602b843c749SSergey Zigachev 			[smu7_ps->performance_level_count - 1].memory_clock;
3603b843c749SSergey Zigachev 	struct PP_Clocks min_clocks = {0};
3604b843c749SSergey Zigachev 	uint32_t i;
3605b843c749SSergey Zigachev 
3606b843c749SSergey Zigachev 	for (i = 0; i < sclk_table->count; i++) {
3607b843c749SSergey Zigachev 		if (sclk == sclk_table->dpm_levels[i].value)
3608b843c749SSergey Zigachev 			break;
3609b843c749SSergey Zigachev 	}
3610b843c749SSergey Zigachev 
3611b843c749SSergey Zigachev 	if (i >= sclk_table->count)
3612b843c749SSergey Zigachev 		data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK;
3613b843c749SSergey Zigachev 	else {
3614b843c749SSergey Zigachev 	/* TODO: Check SCLK in DAL's minimum clocks
3615b843c749SSergey Zigachev 	 * in case DeepSleep divider update is required.
3616b843c749SSergey Zigachev 	 */
3617b843c749SSergey Zigachev 		if (data->display_timing.min_clock_in_sr != min_clocks.engineClockInSR &&
3618b843c749SSergey Zigachev 			(min_clocks.engineClockInSR >= SMU7_MINIMUM_ENGINE_CLOCK ||
3619b843c749SSergey Zigachev 				data->display_timing.min_clock_in_sr >= SMU7_MINIMUM_ENGINE_CLOCK))
3620b843c749SSergey Zigachev 			data->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_SCLK;
3621b843c749SSergey Zigachev 	}
3622b843c749SSergey Zigachev 
3623b843c749SSergey Zigachev 	for (i = 0; i < mclk_table->count; i++) {
3624b843c749SSergey Zigachev 		if (mclk == mclk_table->dpm_levels[i].value)
3625b843c749SSergey Zigachev 			break;
3626b843c749SSergey Zigachev 	}
3627b843c749SSergey Zigachev 
3628b843c749SSergey Zigachev 	if (i >= mclk_table->count)
3629b843c749SSergey Zigachev 		data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_MCLK;
3630b843c749SSergey Zigachev 
3631b843c749SSergey Zigachev 
3632b843c749SSergey Zigachev 	if (data->display_timing.num_existing_displays != hwmgr->display_config->num_display)
3633b843c749SSergey Zigachev 		data->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_MCLK;
3634b843c749SSergey Zigachev 
3635b843c749SSergey Zigachev 	return 0;
3636b843c749SSergey Zigachev }
3637b843c749SSergey Zigachev 
smu7_get_maximum_link_speed(struct pp_hwmgr * hwmgr,const struct smu7_power_state * smu7_ps)3638b843c749SSergey Zigachev static uint16_t smu7_get_maximum_link_speed(struct pp_hwmgr *hwmgr,
3639b843c749SSergey Zigachev 		const struct smu7_power_state *smu7_ps)
3640b843c749SSergey Zigachev {
3641b843c749SSergey Zigachev 	uint32_t i;
3642b843c749SSergey Zigachev 	uint32_t sclk, max_sclk = 0;
3643b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3644b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
3645b843c749SSergey Zigachev 
3646b843c749SSergey Zigachev 	for (i = 0; i < smu7_ps->performance_level_count; i++) {
3647b843c749SSergey Zigachev 		sclk = smu7_ps->performance_levels[i].engine_clock;
3648b843c749SSergey Zigachev 		if (max_sclk < sclk)
3649b843c749SSergey Zigachev 			max_sclk = sclk;
3650b843c749SSergey Zigachev 	}
3651b843c749SSergey Zigachev 
3652b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->sclk_table.count; i++) {
3653b843c749SSergey Zigachev 		if (dpm_table->sclk_table.dpm_levels[i].value == max_sclk)
3654b843c749SSergey Zigachev 			return (uint16_t) ((i >= dpm_table->pcie_speed_table.count) ?
3655b843c749SSergey Zigachev 					dpm_table->pcie_speed_table.dpm_levels
3656b843c749SSergey Zigachev 					[dpm_table->pcie_speed_table.count - 1].value :
3657b843c749SSergey Zigachev 					dpm_table->pcie_speed_table.dpm_levels[i].value);
3658b843c749SSergey Zigachev 	}
3659b843c749SSergey Zigachev 
3660b843c749SSergey Zigachev 	return 0;
3661b843c749SSergey Zigachev }
3662b843c749SSergey Zigachev 
smu7_request_link_speed_change_before_state_change(struct pp_hwmgr * hwmgr,const void * input)3663b843c749SSergey Zigachev static int smu7_request_link_speed_change_before_state_change(
3664b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr, const void *input)
3665b843c749SSergey Zigachev {
3666b843c749SSergey Zigachev 	const struct phm_set_power_state_input *states =
3667b843c749SSergey Zigachev 			(const struct phm_set_power_state_input *)input;
3668b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3669b843c749SSergey Zigachev 	const struct smu7_power_state *smu7_nps =
3670b843c749SSergey Zigachev 			cast_const_phw_smu7_power_state(states->pnew_state);
3671b843c749SSergey Zigachev 	const struct smu7_power_state *polaris10_cps =
3672b843c749SSergey Zigachev 			cast_const_phw_smu7_power_state(states->pcurrent_state);
3673b843c749SSergey Zigachev 
3674b843c749SSergey Zigachev 	uint16_t target_link_speed = smu7_get_maximum_link_speed(hwmgr, smu7_nps);
3675b843c749SSergey Zigachev 	uint16_t current_link_speed;
3676b843c749SSergey Zigachev 
3677b843c749SSergey Zigachev 	if (data->force_pcie_gen == PP_PCIEGenInvalid)
3678b843c749SSergey Zigachev 		current_link_speed = smu7_get_maximum_link_speed(hwmgr, polaris10_cps);
3679b843c749SSergey Zigachev 	else
3680b843c749SSergey Zigachev 		current_link_speed = data->force_pcie_gen;
3681b843c749SSergey Zigachev 
3682b843c749SSergey Zigachev 	data->force_pcie_gen = PP_PCIEGenInvalid;
3683b843c749SSergey Zigachev 	data->pspp_notify_required = false;
3684b843c749SSergey Zigachev 
3685b843c749SSergey Zigachev 	if (target_link_speed > current_link_speed) {
3686b843c749SSergey Zigachev 		switch (target_link_speed) {
3687b843c749SSergey Zigachev #ifdef CONFIG_ACPI
3688b843c749SSergey Zigachev 		case PP_PCIEGen3:
3689b843c749SSergey Zigachev 			if (0 == amdgpu_acpi_pcie_performance_request(hwmgr->adev, PCIE_PERF_REQ_GEN3, false))
3690b843c749SSergey Zigachev 				break;
3691b843c749SSergey Zigachev 			data->force_pcie_gen = PP_PCIEGen2;
3692b843c749SSergey Zigachev 			if (current_link_speed == PP_PCIEGen2)
3693b843c749SSergey Zigachev 				break;
3694b843c749SSergey Zigachev 		case PP_PCIEGen2:
3695b843c749SSergey Zigachev 			if (0 == amdgpu_acpi_pcie_performance_request(hwmgr->adev, PCIE_PERF_REQ_GEN2, false))
3696b843c749SSergey Zigachev 				break;
3697b843c749SSergey Zigachev #endif
3698b843c749SSergey Zigachev 		default:
3699b843c749SSergey Zigachev 			data->force_pcie_gen = smu7_get_current_pcie_speed(hwmgr);
3700b843c749SSergey Zigachev 			break;
3701b843c749SSergey Zigachev 		}
3702b843c749SSergey Zigachev 	} else {
3703b843c749SSergey Zigachev 		if (target_link_speed < current_link_speed)
3704b843c749SSergey Zigachev 			data->pspp_notify_required = true;
3705b843c749SSergey Zigachev 	}
3706b843c749SSergey Zigachev 
3707b843c749SSergey Zigachev 	return 0;
3708b843c749SSergey Zigachev }
3709b843c749SSergey Zigachev 
smu7_freeze_sclk_mclk_dpm(struct pp_hwmgr * hwmgr)3710b843c749SSergey Zigachev static int smu7_freeze_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
3711b843c749SSergey Zigachev {
3712b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3713b843c749SSergey Zigachev 
3714b843c749SSergey Zigachev 	if (0 == data->need_update_smu7_dpm_table)
3715b843c749SSergey Zigachev 		return 0;
3716b843c749SSergey Zigachev 
3717b843c749SSergey Zigachev 	if ((0 == data->sclk_dpm_key_disabled) &&
3718b843c749SSergey Zigachev 		(data->need_update_smu7_dpm_table &
3719b843c749SSergey Zigachev 			(DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK))) {
3720b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(true == smum_is_dpm_running(hwmgr),
3721b843c749SSergey Zigachev 				"Trying to freeze SCLK DPM when DPM is disabled",
3722b843c749SSergey Zigachev 				);
3723b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr,
3724b843c749SSergey Zigachev 				PPSMC_MSG_SCLKDPM_FreezeLevel),
3725b843c749SSergey Zigachev 				"Failed to freeze SCLK DPM during FreezeSclkMclkDPM Function!",
3726b843c749SSergey Zigachev 				return -EINVAL);
3727b843c749SSergey Zigachev 	}
3728b843c749SSergey Zigachev 
3729b843c749SSergey Zigachev 	if ((0 == data->mclk_dpm_key_disabled) &&
3730b843c749SSergey Zigachev 		(data->need_update_smu7_dpm_table &
3731b843c749SSergey Zigachev 		 DPMTABLE_OD_UPDATE_MCLK)) {
3732b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(true == smum_is_dpm_running(hwmgr),
3733b843c749SSergey Zigachev 				"Trying to freeze MCLK DPM when DPM is disabled",
3734b843c749SSergey Zigachev 				);
3735b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr,
3736b843c749SSergey Zigachev 				PPSMC_MSG_MCLKDPM_FreezeLevel),
3737b843c749SSergey Zigachev 				"Failed to freeze MCLK DPM during FreezeSclkMclkDPM Function!",
3738b843c749SSergey Zigachev 				return -EINVAL);
3739b843c749SSergey Zigachev 	}
3740b843c749SSergey Zigachev 
3741b843c749SSergey Zigachev 	return 0;
3742b843c749SSergey Zigachev }
3743b843c749SSergey Zigachev 
smu7_populate_and_upload_sclk_mclk_dpm_levels(struct pp_hwmgr * hwmgr,const void * input)3744b843c749SSergey Zigachev static int smu7_populate_and_upload_sclk_mclk_dpm_levels(
3745b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr, const void *input)
3746b843c749SSergey Zigachev {
3747b843c749SSergey Zigachev 	int result = 0;
3748b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3749b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
3750b843c749SSergey Zigachev 	uint32_t count;
3751b843c749SSergey Zigachev 	struct smu7_odn_dpm_table *odn_table = &(data->odn_dpm_table);
3752b843c749SSergey Zigachev 	struct phm_odn_clock_levels *odn_sclk_table = &(odn_table->odn_core_clock_dpm_levels);
3753b843c749SSergey Zigachev 	struct phm_odn_clock_levels *odn_mclk_table = &(odn_table->odn_memory_clock_dpm_levels);
3754b843c749SSergey Zigachev 
3755b843c749SSergey Zigachev 	if (0 == data->need_update_smu7_dpm_table)
3756b843c749SSergey Zigachev 		return 0;
3757b843c749SSergey Zigachev 
3758b843c749SSergey Zigachev 	if (hwmgr->od_enabled && data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_SCLK) {
3759b843c749SSergey Zigachev 		for (count = 0; count < dpm_table->sclk_table.count; count++) {
3760b843c749SSergey Zigachev 			dpm_table->sclk_table.dpm_levels[count].enabled = odn_sclk_table->entries[count].enabled;
3761b843c749SSergey Zigachev 			dpm_table->sclk_table.dpm_levels[count].value = odn_sclk_table->entries[count].clock;
3762b843c749SSergey Zigachev 		}
3763b843c749SSergey Zigachev 	}
3764b843c749SSergey Zigachev 
3765b843c749SSergey Zigachev 	if (hwmgr->od_enabled && data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK) {
3766b843c749SSergey Zigachev 		for (count = 0; count < dpm_table->mclk_table.count; count++) {
3767b843c749SSergey Zigachev 			dpm_table->mclk_table.dpm_levels[count].enabled = odn_mclk_table->entries[count].enabled;
3768b843c749SSergey Zigachev 			dpm_table->mclk_table.dpm_levels[count].value = odn_mclk_table->entries[count].clock;
3769b843c749SSergey Zigachev 		}
3770b843c749SSergey Zigachev 	}
3771b843c749SSergey Zigachev 
3772b843c749SSergey Zigachev 	if (data->need_update_smu7_dpm_table &
3773b843c749SSergey Zigachev 			(DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK)) {
3774b843c749SSergey Zigachev 		result = smum_populate_all_graphic_levels(hwmgr);
3775b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
3776b843c749SSergey Zigachev 				"Failed to populate SCLK during PopulateNewDPMClocksStates Function!",
3777b843c749SSergey Zigachev 				return result);
3778b843c749SSergey Zigachev 	}
3779b843c749SSergey Zigachev 
3780b843c749SSergey Zigachev 	if (data->need_update_smu7_dpm_table &
3781b843c749SSergey Zigachev 			(DPMTABLE_OD_UPDATE_MCLK + DPMTABLE_UPDATE_MCLK)) {
3782b843c749SSergey Zigachev 		/*populate MCLK dpm table to SMU7 */
3783b843c749SSergey Zigachev 		result = smum_populate_all_memory_levels(hwmgr);
3784b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
3785b843c749SSergey Zigachev 				"Failed to populate MCLK during PopulateNewDPMClocksStates Function!",
3786b843c749SSergey Zigachev 				return result);
3787b843c749SSergey Zigachev 	}
3788b843c749SSergey Zigachev 
3789b843c749SSergey Zigachev 	return result;
3790b843c749SSergey Zigachev }
3791b843c749SSergey Zigachev 
smu7_trim_single_dpm_states(struct pp_hwmgr * hwmgr,struct smu7_single_dpm_table * dpm_table,uint32_t low_limit,uint32_t high_limit)3792b843c749SSergey Zigachev static int smu7_trim_single_dpm_states(struct pp_hwmgr *hwmgr,
3793b843c749SSergey Zigachev 			  struct smu7_single_dpm_table *dpm_table,
3794b843c749SSergey Zigachev 			uint32_t low_limit, uint32_t high_limit)
3795b843c749SSergey Zigachev {
3796b843c749SSergey Zigachev 	uint32_t i;
3797b843c749SSergey Zigachev 
3798b843c749SSergey Zigachev 	/* force the trim if mclk_switching is disabled to prevent flicker */
3799b843c749SSergey Zigachev 	bool force_trim = (low_limit == high_limit);
3800b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->count; i++) {
3801b843c749SSergey Zigachev 	/*skip the trim if od is enabled*/
3802b843c749SSergey Zigachev 		if ((!hwmgr->od_enabled || force_trim)
3803b843c749SSergey Zigachev 			&& (dpm_table->dpm_levels[i].value < low_limit
3804b843c749SSergey Zigachev 			|| dpm_table->dpm_levels[i].value > high_limit))
3805b843c749SSergey Zigachev 			dpm_table->dpm_levels[i].enabled = false;
3806b843c749SSergey Zigachev 		else
3807b843c749SSergey Zigachev 			dpm_table->dpm_levels[i].enabled = true;
3808b843c749SSergey Zigachev 	}
3809b843c749SSergey Zigachev 
3810b843c749SSergey Zigachev 	return 0;
3811b843c749SSergey Zigachev }
3812b843c749SSergey Zigachev 
smu7_trim_dpm_states(struct pp_hwmgr * hwmgr,const struct smu7_power_state * smu7_ps)3813b843c749SSergey Zigachev static int smu7_trim_dpm_states(struct pp_hwmgr *hwmgr,
3814b843c749SSergey Zigachev 		const struct smu7_power_state *smu7_ps)
3815b843c749SSergey Zigachev {
3816b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3817b843c749SSergey Zigachev 	uint32_t high_limit_count;
3818b843c749SSergey Zigachev 
3819b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((smu7_ps->performance_level_count >= 1),
3820b843c749SSergey Zigachev 			"power state did not have any performance level",
3821b843c749SSergey Zigachev 			return -EINVAL);
3822b843c749SSergey Zigachev 
3823b843c749SSergey Zigachev 	high_limit_count = (1 == smu7_ps->performance_level_count) ? 0 : 1;
3824b843c749SSergey Zigachev 
3825b843c749SSergey Zigachev 	smu7_trim_single_dpm_states(hwmgr,
3826b843c749SSergey Zigachev 			&(data->dpm_table.sclk_table),
3827b843c749SSergey Zigachev 			smu7_ps->performance_levels[0].engine_clock,
3828b843c749SSergey Zigachev 			smu7_ps->performance_levels[high_limit_count].engine_clock);
3829b843c749SSergey Zigachev 
3830b843c749SSergey Zigachev 	smu7_trim_single_dpm_states(hwmgr,
3831b843c749SSergey Zigachev 			&(data->dpm_table.mclk_table),
3832b843c749SSergey Zigachev 			smu7_ps->performance_levels[0].memory_clock,
3833b843c749SSergey Zigachev 			smu7_ps->performance_levels[high_limit_count].memory_clock);
3834b843c749SSergey Zigachev 
3835b843c749SSergey Zigachev 	return 0;
3836b843c749SSergey Zigachev }
3837b843c749SSergey Zigachev 
smu7_generate_dpm_level_enable_mask(struct pp_hwmgr * hwmgr,const void * input)3838b843c749SSergey Zigachev static int smu7_generate_dpm_level_enable_mask(
3839b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr, const void *input)
3840b843c749SSergey Zigachev {
3841b843c749SSergey Zigachev 	int result = 0;
3842b843c749SSergey Zigachev 	const struct phm_set_power_state_input *states =
3843b843c749SSergey Zigachev 			(const struct phm_set_power_state_input *)input;
3844b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3845b843c749SSergey Zigachev 	const struct smu7_power_state *smu7_ps =
3846b843c749SSergey Zigachev 			cast_const_phw_smu7_power_state(states->pnew_state);
3847b843c749SSergey Zigachev 
3848b843c749SSergey Zigachev 
3849b843c749SSergey Zigachev 	result = smu7_trim_dpm_states(hwmgr, smu7_ps);
3850b843c749SSergey Zigachev 	if (result)
3851b843c749SSergey Zigachev 		return result;
3852b843c749SSergey Zigachev 
3853b843c749SSergey Zigachev 	data->dpm_level_enable_mask.sclk_dpm_enable_mask =
3854b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&data->dpm_table.sclk_table);
3855b843c749SSergey Zigachev 	data->dpm_level_enable_mask.mclk_dpm_enable_mask =
3856b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&data->dpm_table.mclk_table);
3857b843c749SSergey Zigachev 	data->dpm_level_enable_mask.pcie_dpm_enable_mask =
3858b843c749SSergey Zigachev 			phm_get_dpm_level_enable_mask_value(&data->dpm_table.pcie_speed_table);
3859b843c749SSergey Zigachev 
3860b843c749SSergey Zigachev 	return 0;
3861b843c749SSergey Zigachev }
3862b843c749SSergey Zigachev 
smu7_unfreeze_sclk_mclk_dpm(struct pp_hwmgr * hwmgr)3863b843c749SSergey Zigachev static int smu7_unfreeze_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
3864b843c749SSergey Zigachev {
3865b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3866b843c749SSergey Zigachev 
3867b843c749SSergey Zigachev 	if (0 == data->need_update_smu7_dpm_table)
3868b843c749SSergey Zigachev 		return 0;
3869b843c749SSergey Zigachev 
3870b843c749SSergey Zigachev 	if ((0 == data->sclk_dpm_key_disabled) &&
3871b843c749SSergey Zigachev 		(data->need_update_smu7_dpm_table &
3872b843c749SSergey Zigachev 		(DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK))) {
3873b843c749SSergey Zigachev 
3874b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(true == smum_is_dpm_running(hwmgr),
3875b843c749SSergey Zigachev 				"Trying to Unfreeze SCLK DPM when DPM is disabled",
3876b843c749SSergey Zigachev 				);
3877b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr,
3878b843c749SSergey Zigachev 				PPSMC_MSG_SCLKDPM_UnfreezeLevel),
3879b843c749SSergey Zigachev 			"Failed to unfreeze SCLK DPM during UnFreezeSclkMclkDPM Function!",
3880b843c749SSergey Zigachev 			return -EINVAL);
3881b843c749SSergey Zigachev 	}
3882b843c749SSergey Zigachev 
3883b843c749SSergey Zigachev 	if ((0 == data->mclk_dpm_key_disabled) &&
3884b843c749SSergey Zigachev 		(data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
3885b843c749SSergey Zigachev 
3886b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(true == smum_is_dpm_running(hwmgr),
3887b843c749SSergey Zigachev 				"Trying to Unfreeze MCLK DPM when DPM is disabled",
3888b843c749SSergey Zigachev 				);
3889b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr,
3890b843c749SSergey Zigachev 				PPSMC_MSG_MCLKDPM_UnfreezeLevel),
3891b843c749SSergey Zigachev 		    "Failed to unfreeze MCLK DPM during UnFreezeSclkMclkDPM Function!",
3892b843c749SSergey Zigachev 		    return -EINVAL);
3893b843c749SSergey Zigachev 	}
3894b843c749SSergey Zigachev 
3895b843c749SSergey Zigachev 	data->need_update_smu7_dpm_table &= DPMTABLE_OD_UPDATE_VDDC;
3896b843c749SSergey Zigachev 
3897b843c749SSergey Zigachev 	return 0;
3898b843c749SSergey Zigachev }
3899b843c749SSergey Zigachev 
smu7_notify_link_speed_change_after_state_change(struct pp_hwmgr * hwmgr,const void * input)3900b843c749SSergey Zigachev static int smu7_notify_link_speed_change_after_state_change(
3901b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr, const void *input)
3902b843c749SSergey Zigachev {
3903b843c749SSergey Zigachev 	const struct phm_set_power_state_input *states =
3904b843c749SSergey Zigachev 			(const struct phm_set_power_state_input *)input;
3905b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3906b843c749SSergey Zigachev 	const struct smu7_power_state *smu7_ps =
3907b843c749SSergey Zigachev 			cast_const_phw_smu7_power_state(states->pnew_state);
3908b843c749SSergey Zigachev 	uint16_t target_link_speed = smu7_get_maximum_link_speed(hwmgr, smu7_ps);
3909b843c749SSergey Zigachev 	uint8_t  request;
3910b843c749SSergey Zigachev 
3911b843c749SSergey Zigachev 	if (data->pspp_notify_required) {
3912b843c749SSergey Zigachev 		if (target_link_speed == PP_PCIEGen3)
3913b843c749SSergey Zigachev 			request = PCIE_PERF_REQ_GEN3;
3914b843c749SSergey Zigachev 		else if (target_link_speed == PP_PCIEGen2)
3915b843c749SSergey Zigachev 			request = PCIE_PERF_REQ_GEN2;
3916b843c749SSergey Zigachev 		else
3917b843c749SSergey Zigachev 			request = PCIE_PERF_REQ_GEN1;
3918b843c749SSergey Zigachev 
3919b843c749SSergey Zigachev 		if (request == PCIE_PERF_REQ_GEN1 &&
3920b843c749SSergey Zigachev 				smu7_get_current_pcie_speed(hwmgr) > 0)
3921b843c749SSergey Zigachev 			return 0;
3922b843c749SSergey Zigachev 
3923b843c749SSergey Zigachev #ifdef CONFIG_ACPI
3924b843c749SSergey Zigachev 		if (amdgpu_acpi_pcie_performance_request(hwmgr->adev, request, false)) {
3925b843c749SSergey Zigachev 			if (PP_PCIEGen2 == target_link_speed)
3926b843c749SSergey Zigachev 				pr_info("PSPP request to switch to Gen2 from Gen3 Failed!");
3927b843c749SSergey Zigachev 			else
3928b843c749SSergey Zigachev 				pr_info("PSPP request to switch to Gen1 from Gen2 Failed!");
3929b843c749SSergey Zigachev 		}
3930b843c749SSergey Zigachev #endif
3931b843c749SSergey Zigachev 	}
3932b843c749SSergey Zigachev 
3933b843c749SSergey Zigachev 	return 0;
3934b843c749SSergey Zigachev }
3935b843c749SSergey Zigachev 
smu7_notify_smc_display(struct pp_hwmgr * hwmgr)3936b843c749SSergey Zigachev static int smu7_notify_smc_display(struct pp_hwmgr *hwmgr)
3937b843c749SSergey Zigachev {
3938b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3939b843c749SSergey Zigachev 
3940b843c749SSergey Zigachev 	if (hwmgr->feature_mask & PP_VBI_TIME_SUPPORT_MASK) {
3941b843c749SSergey Zigachev 		if (hwmgr->chip_id == CHIP_VEGAM)
3942b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
3943b843c749SSergey Zigachev 					(PPSMC_Msg)PPSMC_MSG_SetVBITimeout_VEGAM, data->frame_time_x2);
3944b843c749SSergey Zigachev 		else
3945b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
3946b843c749SSergey Zigachev 					(PPSMC_Msg)PPSMC_MSG_SetVBITimeout, data->frame_time_x2);
3947b843c749SSergey Zigachev 	}
3948b843c749SSergey Zigachev 	return (smum_send_msg_to_smc(hwmgr, (PPSMC_Msg)PPSMC_HasDisplay) == 0) ?  0 : -EINVAL;
3949b843c749SSergey Zigachev }
3950b843c749SSergey Zigachev 
smu7_set_power_state_tasks(struct pp_hwmgr * hwmgr,const void * input)3951b843c749SSergey Zigachev static int smu7_set_power_state_tasks(struct pp_hwmgr *hwmgr, const void *input)
3952b843c749SSergey Zigachev {
3953b843c749SSergey Zigachev 	int tmp_result, result = 0;
3954b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
3955b843c749SSergey Zigachev 
3956b843c749SSergey Zigachev 	tmp_result = smu7_find_dpm_states_clocks_in_dpm_table(hwmgr, input);
3957b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
3958b843c749SSergey Zigachev 			"Failed to find DPM states clocks in DPM table!",
3959b843c749SSergey Zigachev 			result = tmp_result);
3960b843c749SSergey Zigachev 
3961b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3962b843c749SSergey Zigachev 			PHM_PlatformCaps_PCIEPerformanceRequest)) {
3963b843c749SSergey Zigachev 		tmp_result =
3964b843c749SSergey Zigachev 			smu7_request_link_speed_change_before_state_change(hwmgr, input);
3965b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == tmp_result),
3966b843c749SSergey Zigachev 				"Failed to request link speed change before state change!",
3967b843c749SSergey Zigachev 				result = tmp_result);
3968b843c749SSergey Zigachev 	}
3969b843c749SSergey Zigachev 
3970b843c749SSergey Zigachev 	tmp_result = smu7_freeze_sclk_mclk_dpm(hwmgr);
3971b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
3972b843c749SSergey Zigachev 			"Failed to freeze SCLK MCLK DPM!", result = tmp_result);
3973b843c749SSergey Zigachev 
3974b843c749SSergey Zigachev 	tmp_result = smu7_populate_and_upload_sclk_mclk_dpm_levels(hwmgr, input);
3975b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
3976b843c749SSergey Zigachev 			"Failed to populate and upload SCLK MCLK DPM levels!",
3977b843c749SSergey Zigachev 			result = tmp_result);
3978b843c749SSergey Zigachev 
3979b843c749SSergey Zigachev 	/*
3980b843c749SSergey Zigachev 	 * If a custom pp table is loaded, set DPMTABLE_OD_UPDATE_VDDC flag.
3981b843c749SSergey Zigachev 	 * That effectively disables AVFS feature.
3982b843c749SSergey Zigachev 	 */
3983b843c749SSergey Zigachev 	if (hwmgr->hardcode_pp_table != NULL)
3984b843c749SSergey Zigachev 		data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_VDDC;
3985b843c749SSergey Zigachev 
3986b843c749SSergey Zigachev 	tmp_result = smu7_update_avfs(hwmgr);
3987b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
3988b843c749SSergey Zigachev 			"Failed to update avfs voltages!",
3989b843c749SSergey Zigachev 			result = tmp_result);
3990b843c749SSergey Zigachev 
3991b843c749SSergey Zigachev 	tmp_result = smu7_generate_dpm_level_enable_mask(hwmgr, input);
3992b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
3993b843c749SSergey Zigachev 			"Failed to generate DPM level enabled mask!",
3994b843c749SSergey Zigachev 			result = tmp_result);
3995b843c749SSergey Zigachev 
3996b843c749SSergey Zigachev 	tmp_result = smum_update_sclk_threshold(hwmgr);
3997b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
3998b843c749SSergey Zigachev 			"Failed to update SCLK threshold!",
3999b843c749SSergey Zigachev 			result = tmp_result);
4000b843c749SSergey Zigachev 
4001b843c749SSergey Zigachev 	tmp_result = smu7_notify_smc_display(hwmgr);
4002b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4003b843c749SSergey Zigachev 			"Failed to notify smc display settings!",
4004b843c749SSergey Zigachev 			result = tmp_result);
4005b843c749SSergey Zigachev 
4006b843c749SSergey Zigachev 	tmp_result = smu7_unfreeze_sclk_mclk_dpm(hwmgr);
4007b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4008b843c749SSergey Zigachev 			"Failed to unfreeze SCLK MCLK DPM!",
4009b843c749SSergey Zigachev 			result = tmp_result);
4010b843c749SSergey Zigachev 
4011b843c749SSergey Zigachev 	tmp_result = smu7_upload_dpm_level_enable_mask(hwmgr);
4012b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4013b843c749SSergey Zigachev 			"Failed to upload DPM level enabled mask!",
4014b843c749SSergey Zigachev 			result = tmp_result);
4015b843c749SSergey Zigachev 
4016b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4017b843c749SSergey Zigachev 			PHM_PlatformCaps_PCIEPerformanceRequest)) {
4018b843c749SSergey Zigachev 		tmp_result =
4019b843c749SSergey Zigachev 			smu7_notify_link_speed_change_after_state_change(hwmgr, input);
4020b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == tmp_result),
4021b843c749SSergey Zigachev 				"Failed to notify link speed change after state change!",
4022b843c749SSergey Zigachev 				result = tmp_result);
4023b843c749SSergey Zigachev 	}
4024b843c749SSergey Zigachev 	data->apply_optimized_settings = false;
4025b843c749SSergey Zigachev 	return result;
4026b843c749SSergey Zigachev }
4027b843c749SSergey Zigachev 
smu7_set_max_fan_pwm_output(struct pp_hwmgr * hwmgr,uint16_t us_max_fan_pwm)4028b843c749SSergey Zigachev static int smu7_set_max_fan_pwm_output(struct pp_hwmgr *hwmgr, uint16_t us_max_fan_pwm)
4029b843c749SSergey Zigachev {
4030b843c749SSergey Zigachev 	hwmgr->thermal_controller.
4031b843c749SSergey Zigachev 	advanceFanControlParameters.usMaxFanPWM = us_max_fan_pwm;
4032b843c749SSergey Zigachev 
4033b843c749SSergey Zigachev 	return smum_send_msg_to_smc_with_parameter(hwmgr,
4034b843c749SSergey Zigachev 			PPSMC_MSG_SetFanPwmMax, us_max_fan_pwm);
4035b843c749SSergey Zigachev }
4036b843c749SSergey Zigachev 
4037b843c749SSergey Zigachev static int
smu7_notify_smc_display_change(struct pp_hwmgr * hwmgr,bool has_display)4038b843c749SSergey Zigachev smu7_notify_smc_display_change(struct pp_hwmgr *hwmgr, bool has_display)
4039b843c749SSergey Zigachev {
4040b843c749SSergey Zigachev 	PPSMC_Msg msg = has_display ? (PPSMC_Msg)PPSMC_HasDisplay : (PPSMC_Msg)PPSMC_NoDisplay;
4041b843c749SSergey Zigachev 
4042b843c749SSergey Zigachev 	return (smum_send_msg_to_smc(hwmgr, msg) == 0) ?  0 : -1;
4043b843c749SSergey Zigachev }
4044b843c749SSergey Zigachev 
4045b843c749SSergey Zigachev static int
smu7_notify_smc_display_config_after_ps_adjustment(struct pp_hwmgr * hwmgr)4046b843c749SSergey Zigachev smu7_notify_smc_display_config_after_ps_adjustment(struct pp_hwmgr *hwmgr)
4047b843c749SSergey Zigachev {
4048b843c749SSergey Zigachev 	if (hwmgr->display_config->num_display > 1 &&
4049b843c749SSergey Zigachev 			!hwmgr->display_config->multi_monitor_in_sync)
4050b843c749SSergey Zigachev 		smu7_notify_smc_display_change(hwmgr, false);
4051b843c749SSergey Zigachev 
4052b843c749SSergey Zigachev 	return 0;
4053b843c749SSergey Zigachev }
4054b843c749SSergey Zigachev 
4055b843c749SSergey Zigachev /**
4056b843c749SSergey Zigachev * Programs the display gap
4057b843c749SSergey Zigachev *
4058b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
4059b843c749SSergey Zigachev * @return   always OK
4060b843c749SSergey Zigachev */
smu7_program_display_gap(struct pp_hwmgr * hwmgr)4061b843c749SSergey Zigachev static int smu7_program_display_gap(struct pp_hwmgr *hwmgr)
4062b843c749SSergey Zigachev {
4063b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4064b843c749SSergey Zigachev 	uint32_t display_gap = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_DISPLAY_GAP_CNTL);
4065b843c749SSergey Zigachev 	uint32_t display_gap2;
4066b843c749SSergey Zigachev 	uint32_t pre_vbi_time_in_us;
4067b843c749SSergey Zigachev 	uint32_t frame_time_in_us;
4068b843c749SSergey Zigachev 	uint32_t ref_clock, refresh_rate;
4069b843c749SSergey Zigachev 
4070b843c749SSergey Zigachev 	display_gap = PHM_SET_FIELD(display_gap, CG_DISPLAY_GAP_CNTL, DISP_GAP, (hwmgr->display_config->num_display > 0) ? DISPLAY_GAP_VBLANK_OR_WM : DISPLAY_GAP_IGNORE);
4071b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_DISPLAY_GAP_CNTL, display_gap);
4072b843c749SSergey Zigachev 
4073b843c749SSergey Zigachev 	ref_clock =  amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
4074b843c749SSergey Zigachev 	refresh_rate = hwmgr->display_config->vrefresh;
4075b843c749SSergey Zigachev 
4076b843c749SSergey Zigachev 	if (0 == refresh_rate)
4077b843c749SSergey Zigachev 		refresh_rate = 60;
4078b843c749SSergey Zigachev 
4079b843c749SSergey Zigachev 	frame_time_in_us = 1000000 / refresh_rate;
4080b843c749SSergey Zigachev 
4081b843c749SSergey Zigachev 	pre_vbi_time_in_us = frame_time_in_us - 200 - hwmgr->display_config->min_vblank_time;
4082b843c749SSergey Zigachev 
4083b843c749SSergey Zigachev 	data->frame_time_x2 = frame_time_in_us * 2 / 100;
4084b843c749SSergey Zigachev 
4085b843c749SSergey Zigachev 	if (data->frame_time_x2 < 280) {
4086b843c749SSergey Zigachev 		pr_debug("%s: enforce minimal VBITimeout: %d -> 280\n", __func__, data->frame_time_x2);
4087b843c749SSergey Zigachev 		data->frame_time_x2 = 280;
4088b843c749SSergey Zigachev 	}
4089b843c749SSergey Zigachev 
4090b843c749SSergey Zigachev 	display_gap2 = pre_vbi_time_in_us * (ref_clock / 100);
4091b843c749SSergey Zigachev 
4092b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_DISPLAY_GAP_CNTL2, display_gap2);
4093b843c749SSergey Zigachev 
4094b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
4095b843c749SSergey Zigachev 			data->soft_regs_start + smum_get_offsetof(hwmgr,
4096b843c749SSergey Zigachev 							SMU_SoftRegisters,
4097b843c749SSergey Zigachev 							PreVBlankGap), 0x64);
4098b843c749SSergey Zigachev 
4099b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
4100b843c749SSergey Zigachev 			data->soft_regs_start + smum_get_offsetof(hwmgr,
4101b843c749SSergey Zigachev 							SMU_SoftRegisters,
4102b843c749SSergey Zigachev 							VBlankTimeout),
4103b843c749SSergey Zigachev 					(frame_time_in_us - pre_vbi_time_in_us));
4104b843c749SSergey Zigachev 
4105b843c749SSergey Zigachev 	return 0;
4106b843c749SSergey Zigachev }
4107b843c749SSergey Zigachev 
smu7_display_configuration_changed_task(struct pp_hwmgr * hwmgr)4108b843c749SSergey Zigachev static int smu7_display_configuration_changed_task(struct pp_hwmgr *hwmgr)
4109b843c749SSergey Zigachev {
4110b843c749SSergey Zigachev 	return smu7_program_display_gap(hwmgr);
4111b843c749SSergey Zigachev }
4112b843c749SSergey Zigachev 
4113b843c749SSergey Zigachev /**
4114b843c749SSergey Zigachev *  Set maximum target operating fan output RPM
4115b843c749SSergey Zigachev *
4116b843c749SSergey Zigachev * @param    hwmgr:  the address of the powerplay hardware manager.
4117b843c749SSergey Zigachev * @param    usMaxFanRpm:  max operating fan RPM value.
4118b843c749SSergey Zigachev * @return   The response that came from the SMC.
4119b843c749SSergey Zigachev */
smu7_set_max_fan_rpm_output(struct pp_hwmgr * hwmgr,uint16_t us_max_fan_rpm)4120b843c749SSergey Zigachev static int smu7_set_max_fan_rpm_output(struct pp_hwmgr *hwmgr, uint16_t us_max_fan_rpm)
4121b843c749SSergey Zigachev {
4122b843c749SSergey Zigachev 	hwmgr->thermal_controller.
4123b843c749SSergey Zigachev 	advanceFanControlParameters.usMaxFanRPM = us_max_fan_rpm;
4124b843c749SSergey Zigachev 
4125b843c749SSergey Zigachev 	return smum_send_msg_to_smc_with_parameter(hwmgr,
4126b843c749SSergey Zigachev 			PPSMC_MSG_SetFanRpmMax, us_max_fan_rpm);
4127b843c749SSergey Zigachev }
4128b843c749SSergey Zigachev 
4129b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs smu7_irq_funcs = {
4130b843c749SSergey Zigachev 	.process = phm_irq_process,
4131b843c749SSergey Zigachev };
4132b843c749SSergey Zigachev 
smu7_register_irq_handlers(struct pp_hwmgr * hwmgr)4133b843c749SSergey Zigachev static int smu7_register_irq_handlers(struct pp_hwmgr *hwmgr)
4134b843c749SSergey Zigachev {
4135b843c749SSergey Zigachev 	struct amdgpu_irq_src *source =
4136b843c749SSergey Zigachev 		kzalloc(sizeof(struct amdgpu_irq_src), GFP_KERNEL);
4137b843c749SSergey Zigachev 
4138b843c749SSergey Zigachev 	if (!source)
4139b843c749SSergey Zigachev 		return -ENOMEM;
4140b843c749SSergey Zigachev 
4141b843c749SSergey Zigachev 	source->funcs = &smu7_irq_funcs;
4142b843c749SSergey Zigachev 
4143b843c749SSergey Zigachev 	amdgpu_irq_add_id((struct amdgpu_device *)(hwmgr->adev),
4144b843c749SSergey Zigachev 			AMDGPU_IH_CLIENTID_LEGACY,
4145b843c749SSergey Zigachev 			VISLANDS30_IV_SRCID_CG_TSS_THERMAL_LOW_TO_HIGH,
4146b843c749SSergey Zigachev 			source);
4147b843c749SSergey Zigachev 	amdgpu_irq_add_id((struct amdgpu_device *)(hwmgr->adev),
4148b843c749SSergey Zigachev 			AMDGPU_IH_CLIENTID_LEGACY,
4149b843c749SSergey Zigachev 			VISLANDS30_IV_SRCID_CG_TSS_THERMAL_HIGH_TO_LOW,
4150b843c749SSergey Zigachev 			source);
4151b843c749SSergey Zigachev 
4152b843c749SSergey Zigachev 	/* Register CTF(GPIO_19) interrupt */
4153b843c749SSergey Zigachev 	amdgpu_irq_add_id((struct amdgpu_device *)(hwmgr->adev),
4154b843c749SSergey Zigachev 			AMDGPU_IH_CLIENTID_LEGACY,
4155b843c749SSergey Zigachev 			VISLANDS30_IV_SRCID_GPIO_19,
4156b843c749SSergey Zigachev 			source);
4157b843c749SSergey Zigachev 
4158b843c749SSergey Zigachev 	return 0;
4159b843c749SSergey Zigachev }
4160b843c749SSergey Zigachev 
4161b843c749SSergey Zigachev static bool
smu7_check_smc_update_required_for_display_configuration(struct pp_hwmgr * hwmgr)4162b843c749SSergey Zigachev smu7_check_smc_update_required_for_display_configuration(struct pp_hwmgr *hwmgr)
4163b843c749SSergey Zigachev {
4164b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4165b843c749SSergey Zigachev 	bool is_update_required = false;
4166b843c749SSergey Zigachev 
4167b843c749SSergey Zigachev 	if (data->display_timing.num_existing_displays != hwmgr->display_config->num_display)
4168b843c749SSergey Zigachev 		is_update_required = true;
4169b843c749SSergey Zigachev 
4170b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
4171b843c749SSergey Zigachev 		if (data->display_timing.min_clock_in_sr != hwmgr->display_config->min_core_set_clock_in_sr &&
4172b843c749SSergey Zigachev 			(data->display_timing.min_clock_in_sr >= SMU7_MINIMUM_ENGINE_CLOCK ||
4173b843c749SSergey Zigachev 			hwmgr->display_config->min_core_set_clock_in_sr >= SMU7_MINIMUM_ENGINE_CLOCK))
4174b843c749SSergey Zigachev 			is_update_required = true;
4175b843c749SSergey Zigachev 	}
4176b843c749SSergey Zigachev 	return is_update_required;
4177b843c749SSergey Zigachev }
4178b843c749SSergey Zigachev 
smu7_are_power_levels_equal(const struct smu7_performance_level * pl1,const struct smu7_performance_level * pl2)4179b843c749SSergey Zigachev static inline bool smu7_are_power_levels_equal(const struct smu7_performance_level *pl1,
4180b843c749SSergey Zigachev 							   const struct smu7_performance_level *pl2)
4181b843c749SSergey Zigachev {
4182b843c749SSergey Zigachev 	return ((pl1->memory_clock == pl2->memory_clock) &&
4183b843c749SSergey Zigachev 		  (pl1->engine_clock == pl2->engine_clock) &&
4184b843c749SSergey Zigachev 		  (pl1->pcie_gen == pl2->pcie_gen) &&
4185b843c749SSergey Zigachev 		  (pl1->pcie_lane == pl2->pcie_lane));
4186b843c749SSergey Zigachev }
4187b843c749SSergey Zigachev 
smu7_check_states_equal(struct pp_hwmgr * hwmgr,const struct pp_hw_power_state * pstate1,const struct pp_hw_power_state * pstate2,bool * equal)4188b843c749SSergey Zigachev static int smu7_check_states_equal(struct pp_hwmgr *hwmgr,
4189b843c749SSergey Zigachev 		const struct pp_hw_power_state *pstate1,
4190b843c749SSergey Zigachev 		const struct pp_hw_power_state *pstate2, bool *equal)
4191b843c749SSergey Zigachev {
4192b843c749SSergey Zigachev 	const struct smu7_power_state *psa;
4193b843c749SSergey Zigachev 	const struct smu7_power_state *psb;
4194b843c749SSergey Zigachev 	int i;
4195b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4196b843c749SSergey Zigachev 
4197b843c749SSergey Zigachev 	if (pstate1 == NULL || pstate2 == NULL || equal == NULL)
4198b843c749SSergey Zigachev 		return -EINVAL;
4199b843c749SSergey Zigachev 
4200b843c749SSergey Zigachev 	psa = cast_const_phw_smu7_power_state(pstate1);
4201b843c749SSergey Zigachev 	psb = cast_const_phw_smu7_power_state(pstate2);
4202b843c749SSergey Zigachev 	/* If the two states don't even have the same number of performance levels they cannot be the same state. */
4203b843c749SSergey Zigachev 	if (psa->performance_level_count != psb->performance_level_count) {
4204b843c749SSergey Zigachev 		*equal = false;
4205b843c749SSergey Zigachev 		return 0;
4206b843c749SSergey Zigachev 	}
4207b843c749SSergey Zigachev 
4208b843c749SSergey Zigachev 	for (i = 0; i < psa->performance_level_count; i++) {
4209b843c749SSergey Zigachev 		if (!smu7_are_power_levels_equal(&(psa->performance_levels[i]), &(psb->performance_levels[i]))) {
4210b843c749SSergey Zigachev 			/* If we have found even one performance level pair that is different the states are different. */
4211b843c749SSergey Zigachev 			*equal = false;
4212b843c749SSergey Zigachev 			return 0;
4213b843c749SSergey Zigachev 		}
4214b843c749SSergey Zigachev 	}
4215b843c749SSergey Zigachev 
4216b843c749SSergey Zigachev 	/* If all performance levels are the same try to use the UVD clocks to break the tie.*/
4217b843c749SSergey Zigachev 	*equal = ((psa->uvd_clks.vclk == psb->uvd_clks.vclk) && (psa->uvd_clks.dclk == psb->uvd_clks.dclk));
4218b843c749SSergey Zigachev 	*equal &= ((psa->vce_clks.evclk == psb->vce_clks.evclk) && (psa->vce_clks.ecclk == psb->vce_clks.ecclk));
4219b843c749SSergey Zigachev 	*equal &= (psa->sclk_threshold == psb->sclk_threshold);
4220b843c749SSergey Zigachev 	/* For OD call, set value based on flag */
4221b843c749SSergey Zigachev 	*equal &= !(data->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK |
4222b843c749SSergey Zigachev 							DPMTABLE_OD_UPDATE_MCLK |
4223b843c749SSergey Zigachev 							DPMTABLE_OD_UPDATE_VDDC));
4224b843c749SSergey Zigachev 
4225b843c749SSergey Zigachev 	return 0;
4226b843c749SSergey Zigachev }
4227b843c749SSergey Zigachev 
smu7_check_mc_firmware(struct pp_hwmgr * hwmgr)4228b843c749SSergey Zigachev static int smu7_check_mc_firmware(struct pp_hwmgr *hwmgr)
4229b843c749SSergey Zigachev {
4230b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4231b843c749SSergey Zigachev 
4232b843c749SSergey Zigachev 	uint32_t vbios_version;
4233b843c749SSergey Zigachev 	uint32_t tmp;
4234b843c749SSergey Zigachev 
4235b843c749SSergey Zigachev 	/* Read MC indirect register offset 0x9F bits [3:0] to see
4236b843c749SSergey Zigachev 	 * if VBIOS has already loaded a full version of MC ucode
4237b843c749SSergey Zigachev 	 * or not.
4238b843c749SSergey Zigachev 	 */
4239b843c749SSergey Zigachev 
4240b843c749SSergey Zigachev 	smu7_get_mc_microcode_version(hwmgr);
4241b843c749SSergey Zigachev 	vbios_version = hwmgr->microcode_version_info.MC & 0xf;
4242b843c749SSergey Zigachev 
4243b843c749SSergey Zigachev 	data->need_long_memory_training = false;
4244b843c749SSergey Zigachev 
4245b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_IO_DEBUG_INDEX,
4246b843c749SSergey Zigachev 							ixMC_IO_DEBUG_UP_13);
4247b843c749SSergey Zigachev 	tmp = cgs_read_register(hwmgr->device, mmMC_SEQ_IO_DEBUG_DATA);
4248b843c749SSergey Zigachev 
4249b843c749SSergey Zigachev 	if (tmp & (1 << 23)) {
4250b843c749SSergey Zigachev 		data->mem_latency_high = MEM_LATENCY_HIGH;
4251b843c749SSergey Zigachev 		data->mem_latency_low = MEM_LATENCY_LOW;
4252b843c749SSergey Zigachev 	} else {
4253b843c749SSergey Zigachev 		data->mem_latency_high = 330;
4254b843c749SSergey Zigachev 		data->mem_latency_low = 330;
4255b843c749SSergey Zigachev 	}
4256b843c749SSergey Zigachev 
4257b843c749SSergey Zigachev 	return 0;
4258b843c749SSergey Zigachev }
4259b843c749SSergey Zigachev 
smu7_read_clock_registers(struct pp_hwmgr * hwmgr)4260b843c749SSergey Zigachev static int smu7_read_clock_registers(struct pp_hwmgr *hwmgr)
4261b843c749SSergey Zigachev {
4262b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4263b843c749SSergey Zigachev 
4264b843c749SSergey Zigachev 	data->clock_registers.vCG_SPLL_FUNC_CNTL         =
4265b843c749SSergey Zigachev 		cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_SPLL_FUNC_CNTL);
4266b843c749SSergey Zigachev 	data->clock_registers.vCG_SPLL_FUNC_CNTL_2       =
4267b843c749SSergey Zigachev 		cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_SPLL_FUNC_CNTL_2);
4268b843c749SSergey Zigachev 	data->clock_registers.vCG_SPLL_FUNC_CNTL_3       =
4269b843c749SSergey Zigachev 		cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_SPLL_FUNC_CNTL_3);
4270b843c749SSergey Zigachev 	data->clock_registers.vCG_SPLL_FUNC_CNTL_4       =
4271b843c749SSergey Zigachev 		cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_SPLL_FUNC_CNTL_4);
4272b843c749SSergey Zigachev 	data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM   =
4273b843c749SSergey Zigachev 		cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_SPLL_SPREAD_SPECTRUM);
4274b843c749SSergey Zigachev 	data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2 =
4275b843c749SSergey Zigachev 		cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_SPLL_SPREAD_SPECTRUM_2);
4276b843c749SSergey Zigachev 	data->clock_registers.vDLL_CNTL                  =
4277b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmDLL_CNTL);
4278b843c749SSergey Zigachev 	data->clock_registers.vMCLK_PWRMGT_CNTL          =
4279b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMCLK_PWRMGT_CNTL);
4280b843c749SSergey Zigachev 	data->clock_registers.vMPLL_AD_FUNC_CNTL         =
4281b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMPLL_AD_FUNC_CNTL);
4282b843c749SSergey Zigachev 	data->clock_registers.vMPLL_DQ_FUNC_CNTL         =
4283b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMPLL_DQ_FUNC_CNTL);
4284b843c749SSergey Zigachev 	data->clock_registers.vMPLL_FUNC_CNTL            =
4285b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMPLL_FUNC_CNTL);
4286b843c749SSergey Zigachev 	data->clock_registers.vMPLL_FUNC_CNTL_1          =
4287b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMPLL_FUNC_CNTL_1);
4288b843c749SSergey Zigachev 	data->clock_registers.vMPLL_FUNC_CNTL_2          =
4289b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMPLL_FUNC_CNTL_2);
4290b843c749SSergey Zigachev 	data->clock_registers.vMPLL_SS1                  =
4291b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMPLL_SS1);
4292b843c749SSergey Zigachev 	data->clock_registers.vMPLL_SS2                  =
4293b843c749SSergey Zigachev 		cgs_read_register(hwmgr->device, mmMPLL_SS2);
4294b843c749SSergey Zigachev 	return 0;
4295b843c749SSergey Zigachev 
4296b843c749SSergey Zigachev }
4297b843c749SSergey Zigachev 
4298b843c749SSergey Zigachev /**
4299b843c749SSergey Zigachev  * Find out if memory is GDDR5.
4300b843c749SSergey Zigachev  *
4301b843c749SSergey Zigachev  * @param    hwmgr  the address of the powerplay hardware manager.
4302b843c749SSergey Zigachev  * @return   always 0
4303b843c749SSergey Zigachev  */
smu7_get_memory_type(struct pp_hwmgr * hwmgr)4304b843c749SSergey Zigachev static int smu7_get_memory_type(struct pp_hwmgr *hwmgr)
4305b843c749SSergey Zigachev {
4306b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4307b843c749SSergey Zigachev 	struct amdgpu_device *adev = hwmgr->adev;
4308b843c749SSergey Zigachev 
4309b843c749SSergey Zigachev 	data->is_memory_gddr5 = (adev->gmc.vram_type == AMDGPU_VRAM_TYPE_GDDR5);
4310b843c749SSergey Zigachev 
4311b843c749SSergey Zigachev 	return 0;
4312b843c749SSergey Zigachev }
4313b843c749SSergey Zigachev 
4314b843c749SSergey Zigachev /**
4315b843c749SSergey Zigachev  * Enables Dynamic Power Management by SMC
4316b843c749SSergey Zigachev  *
4317b843c749SSergey Zigachev  * @param    hwmgr  the address of the powerplay hardware manager.
4318b843c749SSergey Zigachev  * @return   always 0
4319b843c749SSergey Zigachev  */
smu7_enable_acpi_power_management(struct pp_hwmgr * hwmgr)4320b843c749SSergey Zigachev static int smu7_enable_acpi_power_management(struct pp_hwmgr *hwmgr)
4321b843c749SSergey Zigachev {
4322b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
4323b843c749SSergey Zigachev 			GENERAL_PWRMGT, STATIC_PM_EN, 1);
4324b843c749SSergey Zigachev 
4325b843c749SSergey Zigachev 	return 0;
4326b843c749SSergey Zigachev }
4327b843c749SSergey Zigachev 
4328b843c749SSergey Zigachev /**
4329b843c749SSergey Zigachev  * Initialize PowerGating States for different engines
4330b843c749SSergey Zigachev  *
4331b843c749SSergey Zigachev  * @param    hwmgr  the address of the powerplay hardware manager.
4332b843c749SSergey Zigachev  * @return   always 0
4333b843c749SSergey Zigachev  */
smu7_init_power_gate_state(struct pp_hwmgr * hwmgr)4334b843c749SSergey Zigachev static int smu7_init_power_gate_state(struct pp_hwmgr *hwmgr)
4335b843c749SSergey Zigachev {
4336b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4337b843c749SSergey Zigachev 
4338b843c749SSergey Zigachev 	data->uvd_power_gated = false;
4339b843c749SSergey Zigachev 	data->vce_power_gated = false;
4340b843c749SSergey Zigachev 
4341b843c749SSergey Zigachev 	return 0;
4342b843c749SSergey Zigachev }
4343b843c749SSergey Zigachev 
smu7_init_sclk_threshold(struct pp_hwmgr * hwmgr)4344b843c749SSergey Zigachev static int smu7_init_sclk_threshold(struct pp_hwmgr *hwmgr)
4345b843c749SSergey Zigachev {
4346b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4347b843c749SSergey Zigachev 
4348b843c749SSergey Zigachev 	data->low_sclk_interrupt_threshold = 0;
4349b843c749SSergey Zigachev 	return 0;
4350b843c749SSergey Zigachev }
4351b843c749SSergey Zigachev 
smu7_setup_asic_task(struct pp_hwmgr * hwmgr)4352b843c749SSergey Zigachev static int smu7_setup_asic_task(struct pp_hwmgr *hwmgr)
4353b843c749SSergey Zigachev {
4354b843c749SSergey Zigachev 	int tmp_result, result = 0;
4355b843c749SSergey Zigachev 
4356b843c749SSergey Zigachev 	smu7_check_mc_firmware(hwmgr);
4357b843c749SSergey Zigachev 
4358b843c749SSergey Zigachev 	tmp_result = smu7_read_clock_registers(hwmgr);
4359b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4360b843c749SSergey Zigachev 			"Failed to read clock registers!", result = tmp_result);
4361b843c749SSergey Zigachev 
4362b843c749SSergey Zigachev 	tmp_result = smu7_get_memory_type(hwmgr);
4363b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4364b843c749SSergey Zigachev 			"Failed to get memory type!", result = tmp_result);
4365b843c749SSergey Zigachev 
4366b843c749SSergey Zigachev 	tmp_result = smu7_enable_acpi_power_management(hwmgr);
4367b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4368b843c749SSergey Zigachev 			"Failed to enable ACPI power management!", result = tmp_result);
4369b843c749SSergey Zigachev 
4370b843c749SSergey Zigachev 	tmp_result = smu7_init_power_gate_state(hwmgr);
4371b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4372b843c749SSergey Zigachev 			"Failed to init power gate state!", result = tmp_result);
4373b843c749SSergey Zigachev 
4374b843c749SSergey Zigachev 	tmp_result = smu7_get_mc_microcode_version(hwmgr);
4375b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4376b843c749SSergey Zigachev 			"Failed to get MC microcode version!", result = tmp_result);
4377b843c749SSergey Zigachev 
4378b843c749SSergey Zigachev 	tmp_result = smu7_init_sclk_threshold(hwmgr);
4379b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == tmp_result),
4380b843c749SSergey Zigachev 			"Failed to init sclk threshold!", result = tmp_result);
4381b843c749SSergey Zigachev 
4382b843c749SSergey Zigachev 	return result;
4383b843c749SSergey Zigachev }
4384b843c749SSergey Zigachev 
smu7_force_clock_level(struct pp_hwmgr * hwmgr,enum pp_clock_type type,uint32_t mask)4385b843c749SSergey Zigachev static int smu7_force_clock_level(struct pp_hwmgr *hwmgr,
4386b843c749SSergey Zigachev 		enum pp_clock_type type, uint32_t mask)
4387b843c749SSergey Zigachev {
4388b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4389b843c749SSergey Zigachev 
4390b843c749SSergey Zigachev 	if (mask == 0)
4391b843c749SSergey Zigachev 		return -EINVAL;
4392b843c749SSergey Zigachev 
4393b843c749SSergey Zigachev 	switch (type) {
4394b843c749SSergey Zigachev 	case PP_SCLK:
4395b843c749SSergey Zigachev 		if (!data->sclk_dpm_key_disabled)
4396b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
4397b843c749SSergey Zigachev 					PPSMC_MSG_SCLKDPM_SetEnabledMask,
4398b843c749SSergey Zigachev 					data->dpm_level_enable_mask.sclk_dpm_enable_mask & mask);
4399b843c749SSergey Zigachev 		break;
4400b843c749SSergey Zigachev 	case PP_MCLK:
4401b843c749SSergey Zigachev 		if (!data->mclk_dpm_key_disabled)
4402b843c749SSergey Zigachev 			smum_send_msg_to_smc_with_parameter(hwmgr,
4403b843c749SSergey Zigachev 					PPSMC_MSG_MCLKDPM_SetEnabledMask,
4404b843c749SSergey Zigachev 					data->dpm_level_enable_mask.mclk_dpm_enable_mask & mask);
4405b843c749SSergey Zigachev 		break;
4406b843c749SSergey Zigachev 	case PP_PCIE:
4407b843c749SSergey Zigachev 	{
4408b843c749SSergey Zigachev 		uint32_t tmp = mask & data->dpm_level_enable_mask.pcie_dpm_enable_mask;
4409b843c749SSergey Zigachev 
4410b843c749SSergey Zigachev 		if (!data->pcie_dpm_key_disabled) {
4411b843c749SSergey Zigachev 			if (fls(tmp) != ffs(tmp))
4412b843c749SSergey Zigachev 				smum_send_msg_to_smc(hwmgr, PPSMC_MSG_PCIeDPM_UnForceLevel);
4413b843c749SSergey Zigachev 			else
4414b843c749SSergey Zigachev 				smum_send_msg_to_smc_with_parameter(hwmgr,
4415b843c749SSergey Zigachev 					PPSMC_MSG_PCIeDPM_ForceLevel,
4416b843c749SSergey Zigachev 					fls(tmp) - 1);
4417b843c749SSergey Zigachev 		}
4418b843c749SSergey Zigachev 		break;
4419b843c749SSergey Zigachev 	}
4420b843c749SSergey Zigachev 	default:
4421b843c749SSergey Zigachev 		break;
4422b843c749SSergey Zigachev 	}
4423b843c749SSergey Zigachev 
4424b843c749SSergey Zigachev 	return 0;
4425b843c749SSergey Zigachev }
4426b843c749SSergey Zigachev 
smu7_print_clock_levels(struct pp_hwmgr * hwmgr,enum pp_clock_type type,char * buf)4427b843c749SSergey Zigachev static int smu7_print_clock_levels(struct pp_hwmgr *hwmgr,
4428b843c749SSergey Zigachev 		enum pp_clock_type type, char *buf)
4429b843c749SSergey Zigachev {
4430b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4431b843c749SSergey Zigachev 	struct smu7_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
4432b843c749SSergey Zigachev 	struct smu7_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
4433b843c749SSergey Zigachev 	struct smu7_single_dpm_table *pcie_table = &(data->dpm_table.pcie_speed_table);
4434b843c749SSergey Zigachev 	struct smu7_odn_dpm_table *odn_table = &(data->odn_dpm_table);
4435b843c749SSergey Zigachev 	struct phm_odn_clock_levels *odn_sclk_table = &(odn_table->odn_core_clock_dpm_levels);
4436b843c749SSergey Zigachev 	struct phm_odn_clock_levels *odn_mclk_table = &(odn_table->odn_memory_clock_dpm_levels);
4437b843c749SSergey Zigachev 	int i, now, size = 0;
4438b843c749SSergey Zigachev 	uint32_t clock, pcie_speed;
4439b843c749SSergey Zigachev 
4440b843c749SSergey Zigachev 	switch (type) {
4441b843c749SSergey Zigachev 	case PP_SCLK:
4442b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_API_GetSclkFrequency);
4443b843c749SSergey Zigachev 		clock = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
4444b843c749SSergey Zigachev 
4445b843c749SSergey Zigachev 		for (i = 0; i < sclk_table->count; i++) {
4446b843c749SSergey Zigachev 			if (clock > sclk_table->dpm_levels[i].value)
4447b843c749SSergey Zigachev 				continue;
4448b843c749SSergey Zigachev 			break;
4449b843c749SSergey Zigachev 		}
4450b843c749SSergey Zigachev 		now = i;
4451b843c749SSergey Zigachev 
4452b843c749SSergey Zigachev 		for (i = 0; i < sclk_table->count; i++)
4453b843c749SSergey Zigachev 			size += sprintf(buf + size, "%d: %uMhz %s\n",
4454b843c749SSergey Zigachev 					i, sclk_table->dpm_levels[i].value / 100,
4455b843c749SSergey Zigachev 					(i == now) ? "*" : "");
4456b843c749SSergey Zigachev 		break;
4457b843c749SSergey Zigachev 	case PP_MCLK:
4458b843c749SSergey Zigachev 		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_API_GetMclkFrequency);
4459b843c749SSergey Zigachev 		clock = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
4460b843c749SSergey Zigachev 
4461b843c749SSergey Zigachev 		for (i = 0; i < mclk_table->count; i++) {
4462b843c749SSergey Zigachev 			if (clock > mclk_table->dpm_levels[i].value)
4463b843c749SSergey Zigachev 				continue;
4464b843c749SSergey Zigachev 			break;
4465b843c749SSergey Zigachev 		}
4466b843c749SSergey Zigachev 		now = i;
4467b843c749SSergey Zigachev 
4468b843c749SSergey Zigachev 		for (i = 0; i < mclk_table->count; i++)
4469b843c749SSergey Zigachev 			size += sprintf(buf + size, "%d: %uMhz %s\n",
4470b843c749SSergey Zigachev 					i, mclk_table->dpm_levels[i].value / 100,
4471b843c749SSergey Zigachev 					(i == now) ? "*" : "");
4472b843c749SSergey Zigachev 		break;
4473b843c749SSergey Zigachev 	case PP_PCIE:
4474b843c749SSergey Zigachev 		pcie_speed = smu7_get_current_pcie_speed(hwmgr);
4475b843c749SSergey Zigachev 		for (i = 0; i < pcie_table->count; i++) {
4476b843c749SSergey Zigachev 			if (pcie_speed != pcie_table->dpm_levels[i].value)
4477b843c749SSergey Zigachev 				continue;
4478b843c749SSergey Zigachev 			break;
4479b843c749SSergey Zigachev 		}
4480b843c749SSergey Zigachev 		now = i;
4481b843c749SSergey Zigachev 
4482b843c749SSergey Zigachev 		for (i = 0; i < pcie_table->count; i++)
4483b843c749SSergey Zigachev 			size += sprintf(buf + size, "%d: %s %s\n", i,
4484b843c749SSergey Zigachev 					(pcie_table->dpm_levels[i].value == 0) ? "2.5GT/s, x8" :
4485b843c749SSergey Zigachev 					(pcie_table->dpm_levels[i].value == 1) ? "5.0GT/s, x16" :
4486b843c749SSergey Zigachev 					(pcie_table->dpm_levels[i].value == 2) ? "8.0GT/s, x16" : "",
4487b843c749SSergey Zigachev 					(i == now) ? "*" : "");
4488b843c749SSergey Zigachev 		break;
4489b843c749SSergey Zigachev 	case OD_SCLK:
4490b843c749SSergey Zigachev 		if (hwmgr->od_enabled) {
4491b843c749SSergey Zigachev 			size = sprintf(buf, "%s:\n", "OD_SCLK");
4492b843c749SSergey Zigachev 			for (i = 0; i < odn_sclk_table->num_of_pl; i++)
4493b843c749SSergey Zigachev 				size += sprintf(buf + size, "%d: %10uMHz %10umV\n",
4494b843c749SSergey Zigachev 					i, odn_sclk_table->entries[i].clock/100,
4495b843c749SSergey Zigachev 					odn_sclk_table->entries[i].vddc);
4496b843c749SSergey Zigachev 		}
4497b843c749SSergey Zigachev 		break;
4498b843c749SSergey Zigachev 	case OD_MCLK:
4499b843c749SSergey Zigachev 		if (hwmgr->od_enabled) {
4500b843c749SSergey Zigachev 			size = sprintf(buf, "%s:\n", "OD_MCLK");
4501b843c749SSergey Zigachev 			for (i = 0; i < odn_mclk_table->num_of_pl; i++)
4502b843c749SSergey Zigachev 				size += sprintf(buf + size, "%d: %10uMHz %10umV\n",
4503b843c749SSergey Zigachev 					i, odn_mclk_table->entries[i].clock/100,
4504b843c749SSergey Zigachev 					odn_mclk_table->entries[i].vddc);
4505b843c749SSergey Zigachev 		}
4506b843c749SSergey Zigachev 		break;
4507b843c749SSergey Zigachev 	case OD_RANGE:
4508b843c749SSergey Zigachev 		if (hwmgr->od_enabled) {
4509b843c749SSergey Zigachev 			size = sprintf(buf, "%s:\n", "OD_RANGE");
4510b843c749SSergey Zigachev 			size += sprintf(buf + size, "SCLK: %7uMHz %10uMHz\n",
4511b843c749SSergey Zigachev 				data->golden_dpm_table.sclk_table.dpm_levels[0].value/100,
4512b843c749SSergey Zigachev 				hwmgr->platform_descriptor.overdriveLimit.engineClock/100);
4513b843c749SSergey Zigachev 			size += sprintf(buf + size, "MCLK: %7uMHz %10uMHz\n",
4514b843c749SSergey Zigachev 				data->golden_dpm_table.mclk_table.dpm_levels[0].value/100,
4515b843c749SSergey Zigachev 				hwmgr->platform_descriptor.overdriveLimit.memoryClock/100);
4516b843c749SSergey Zigachev 			size += sprintf(buf + size, "VDDC: %7umV %11umV\n",
4517b843c749SSergey Zigachev 				data->odn_dpm_table.min_vddc,
4518b843c749SSergey Zigachev 				data->odn_dpm_table.max_vddc);
4519b843c749SSergey Zigachev 		}
4520b843c749SSergey Zigachev 		break;
4521b843c749SSergey Zigachev 	default:
4522b843c749SSergey Zigachev 		break;
4523b843c749SSergey Zigachev 	}
4524b843c749SSergey Zigachev 	return size;
4525b843c749SSergey Zigachev }
4526b843c749SSergey Zigachev 
smu7_set_fan_control_mode(struct pp_hwmgr * hwmgr,uint32_t mode)4527b843c749SSergey Zigachev static void smu7_set_fan_control_mode(struct pp_hwmgr *hwmgr, uint32_t mode)
4528b843c749SSergey Zigachev {
4529b843c749SSergey Zigachev 	switch (mode) {
4530b843c749SSergey Zigachev 	case AMD_FAN_CTRL_NONE:
4531b843c749SSergey Zigachev 		smu7_fan_ctrl_set_fan_speed_percent(hwmgr, 100);
4532b843c749SSergey Zigachev 		break;
4533b843c749SSergey Zigachev 	case AMD_FAN_CTRL_MANUAL:
4534b843c749SSergey Zigachev 		if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4535b843c749SSergey Zigachev 			PHM_PlatformCaps_MicrocodeFanControl))
4536b843c749SSergey Zigachev 			smu7_fan_ctrl_stop_smc_fan_control(hwmgr);
4537b843c749SSergey Zigachev 		break;
4538b843c749SSergey Zigachev 	case AMD_FAN_CTRL_AUTO:
4539b843c749SSergey Zigachev 		if (!smu7_fan_ctrl_set_static_mode(hwmgr, mode))
4540b843c749SSergey Zigachev 			smu7_fan_ctrl_start_smc_fan_control(hwmgr);
4541b843c749SSergey Zigachev 		break;
4542b843c749SSergey Zigachev 	default:
4543b843c749SSergey Zigachev 		break;
4544b843c749SSergey Zigachev 	}
4545b843c749SSergey Zigachev }
4546b843c749SSergey Zigachev 
smu7_get_fan_control_mode(struct pp_hwmgr * hwmgr)4547b843c749SSergey Zigachev static uint32_t smu7_get_fan_control_mode(struct pp_hwmgr *hwmgr)
4548b843c749SSergey Zigachev {
4549b843c749SSergey Zigachev 	return hwmgr->fan_ctrl_enabled ? AMD_FAN_CTRL_AUTO : AMD_FAN_CTRL_MANUAL;
4550b843c749SSergey Zigachev }
4551b843c749SSergey Zigachev 
smu7_get_sclk_od(struct pp_hwmgr * hwmgr)4552b843c749SSergey Zigachev static int smu7_get_sclk_od(struct pp_hwmgr *hwmgr)
4553b843c749SSergey Zigachev {
4554b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4555b843c749SSergey Zigachev 	struct smu7_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
4556b843c749SSergey Zigachev 	struct smu7_single_dpm_table *golden_sclk_table =
4557b843c749SSergey Zigachev 			&(data->golden_dpm_table.sclk_table);
4558b843c749SSergey Zigachev 	int value;
4559b843c749SSergey Zigachev 
4560b843c749SSergey Zigachev 	value = (sclk_table->dpm_levels[sclk_table->count - 1].value -
4561b843c749SSergey Zigachev 			golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value) *
4562b843c749SSergey Zigachev 			100 /
4563b843c749SSergey Zigachev 			golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value;
4564b843c749SSergey Zigachev 
4565b843c749SSergey Zigachev 	return value;
4566b843c749SSergey Zigachev }
4567b843c749SSergey Zigachev 
smu7_set_sclk_od(struct pp_hwmgr * hwmgr,uint32_t value)4568b843c749SSergey Zigachev static int smu7_set_sclk_od(struct pp_hwmgr *hwmgr, uint32_t value)
4569b843c749SSergey Zigachev {
4570b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4571b843c749SSergey Zigachev 	struct smu7_single_dpm_table *golden_sclk_table =
4572b843c749SSergey Zigachev 			&(data->golden_dpm_table.sclk_table);
4573b843c749SSergey Zigachev 	struct pp_power_state  *ps;
4574b843c749SSergey Zigachev 	struct smu7_power_state  *smu7_ps;
4575b843c749SSergey Zigachev 
4576b843c749SSergey Zigachev 	if (value > 20)
4577b843c749SSergey Zigachev 		value = 20;
4578b843c749SSergey Zigachev 
4579b843c749SSergey Zigachev 	ps = hwmgr->request_ps;
4580b843c749SSergey Zigachev 
4581b843c749SSergey Zigachev 	if (ps == NULL)
4582b843c749SSergey Zigachev 		return -EINVAL;
4583b843c749SSergey Zigachev 
4584b843c749SSergey Zigachev 	smu7_ps = cast_phw_smu7_power_state(&ps->hardware);
4585b843c749SSergey Zigachev 
4586b843c749SSergey Zigachev 	smu7_ps->performance_levels[smu7_ps->performance_level_count - 1].engine_clock =
4587b843c749SSergey Zigachev 			golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value *
4588b843c749SSergey Zigachev 			value / 100 +
4589b843c749SSergey Zigachev 			golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value;
4590b843c749SSergey Zigachev 
4591b843c749SSergey Zigachev 	return 0;
4592b843c749SSergey Zigachev }
4593b843c749SSergey Zigachev 
smu7_get_mclk_od(struct pp_hwmgr * hwmgr)4594b843c749SSergey Zigachev static int smu7_get_mclk_od(struct pp_hwmgr *hwmgr)
4595b843c749SSergey Zigachev {
4596b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4597b843c749SSergey Zigachev 	struct smu7_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
4598b843c749SSergey Zigachev 	struct smu7_single_dpm_table *golden_mclk_table =
4599b843c749SSergey Zigachev 			&(data->golden_dpm_table.mclk_table);
4600b843c749SSergey Zigachev 	int value;
4601b843c749SSergey Zigachev 
4602b843c749SSergey Zigachev 	value = (mclk_table->dpm_levels[mclk_table->count - 1].value -
4603b843c749SSergey Zigachev 			golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value) *
4604b843c749SSergey Zigachev 			100 /
4605b843c749SSergey Zigachev 			golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value;
4606b843c749SSergey Zigachev 
4607b843c749SSergey Zigachev 	return value;
4608b843c749SSergey Zigachev }
4609b843c749SSergey Zigachev 
smu7_set_mclk_od(struct pp_hwmgr * hwmgr,uint32_t value)4610b843c749SSergey Zigachev static int smu7_set_mclk_od(struct pp_hwmgr *hwmgr, uint32_t value)
4611b843c749SSergey Zigachev {
4612b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4613b843c749SSergey Zigachev 	struct smu7_single_dpm_table *golden_mclk_table =
4614b843c749SSergey Zigachev 			&(data->golden_dpm_table.mclk_table);
4615b843c749SSergey Zigachev 	struct pp_power_state  *ps;
4616b843c749SSergey Zigachev 	struct smu7_power_state  *smu7_ps;
4617b843c749SSergey Zigachev 
4618b843c749SSergey Zigachev 	if (value > 20)
4619b843c749SSergey Zigachev 		value = 20;
4620b843c749SSergey Zigachev 
4621b843c749SSergey Zigachev 	ps = hwmgr->request_ps;
4622b843c749SSergey Zigachev 
4623b843c749SSergey Zigachev 	if (ps == NULL)
4624b843c749SSergey Zigachev 		return -EINVAL;
4625b843c749SSergey Zigachev 
4626b843c749SSergey Zigachev 	smu7_ps = cast_phw_smu7_power_state(&ps->hardware);
4627b843c749SSergey Zigachev 
4628b843c749SSergey Zigachev 	smu7_ps->performance_levels[smu7_ps->performance_level_count - 1].memory_clock =
4629b843c749SSergey Zigachev 			golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value *
4630b843c749SSergey Zigachev 			value / 100 +
4631b843c749SSergey Zigachev 			golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value;
4632b843c749SSergey Zigachev 
4633b843c749SSergey Zigachev 	return 0;
4634b843c749SSergey Zigachev }
4635b843c749SSergey Zigachev 
4636b843c749SSergey Zigachev 
smu7_get_sclks(struct pp_hwmgr * hwmgr,struct amd_pp_clocks * clocks)4637b843c749SSergey Zigachev static int smu7_get_sclks(struct pp_hwmgr *hwmgr, struct amd_pp_clocks *clocks)
4638b843c749SSergey Zigachev {
4639b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
4640b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)hwmgr->pptable;
4641b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_sclk_table = NULL;
4642b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *sclk_table;
4643b843c749SSergey Zigachev 	int i;
4644b843c749SSergey Zigachev 
4645b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V1) {
4646b843c749SSergey Zigachev 		if (table_info == NULL || table_info->vdd_dep_on_sclk == NULL)
4647b843c749SSergey Zigachev 			return -EINVAL;
4648b843c749SSergey Zigachev 		dep_sclk_table = table_info->vdd_dep_on_sclk;
4649b843c749SSergey Zigachev 		for (i = 0; i < dep_sclk_table->count; i++)
4650b843c749SSergey Zigachev 			clocks->clock[i] = dep_sclk_table->entries[i].clk * 10;
4651b843c749SSergey Zigachev 		clocks->count = dep_sclk_table->count;
4652b843c749SSergey Zigachev 	} else if (hwmgr->pp_table_version == PP_TABLE_V0) {
4653b843c749SSergey Zigachev 		sclk_table = hwmgr->dyn_state.vddc_dependency_on_sclk;
4654b843c749SSergey Zigachev 		for (i = 0; i < sclk_table->count; i++)
4655b843c749SSergey Zigachev 			clocks->clock[i] = sclk_table->entries[i].clk * 10;
4656b843c749SSergey Zigachev 		clocks->count = sclk_table->count;
4657b843c749SSergey Zigachev 	}
4658b843c749SSergey Zigachev 
4659b843c749SSergey Zigachev 	return 0;
4660b843c749SSergey Zigachev }
4661b843c749SSergey Zigachev 
smu7_get_mem_latency(struct pp_hwmgr * hwmgr,uint32_t clk)4662b843c749SSergey Zigachev static uint32_t smu7_get_mem_latency(struct pp_hwmgr *hwmgr, uint32_t clk)
4663b843c749SSergey Zigachev {
4664b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4665b843c749SSergey Zigachev 
4666b843c749SSergey Zigachev 	if (clk >= MEM_FREQ_LOW_LATENCY && clk < MEM_FREQ_HIGH_LATENCY)
4667b843c749SSergey Zigachev 		return data->mem_latency_high;
4668b843c749SSergey Zigachev 	else if (clk >= MEM_FREQ_HIGH_LATENCY)
4669b843c749SSergey Zigachev 		return data->mem_latency_low;
4670b843c749SSergey Zigachev 	else
4671b843c749SSergey Zigachev 		return MEM_LATENCY_ERR;
4672b843c749SSergey Zigachev }
4673b843c749SSergey Zigachev 
smu7_get_mclks(struct pp_hwmgr * hwmgr,struct amd_pp_clocks * clocks)4674b843c749SSergey Zigachev static int smu7_get_mclks(struct pp_hwmgr *hwmgr, struct amd_pp_clocks *clocks)
4675b843c749SSergey Zigachev {
4676b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
4677b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)hwmgr->pptable;
4678b843c749SSergey Zigachev 	struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table;
4679b843c749SSergey Zigachev 	int i;
4680b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *mclk_table;
4681b843c749SSergey Zigachev 
4682b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V1) {
4683b843c749SSergey Zigachev 		if (table_info == NULL)
4684b843c749SSergey Zigachev 			return -EINVAL;
4685b843c749SSergey Zigachev 		dep_mclk_table = table_info->vdd_dep_on_mclk;
4686b843c749SSergey Zigachev 		for (i = 0; i < dep_mclk_table->count; i++) {
4687b843c749SSergey Zigachev 			clocks->clock[i] = dep_mclk_table->entries[i].clk * 10;
4688b843c749SSergey Zigachev 			clocks->latency[i] = smu7_get_mem_latency(hwmgr,
4689b843c749SSergey Zigachev 						dep_mclk_table->entries[i].clk);
4690b843c749SSergey Zigachev 		}
4691b843c749SSergey Zigachev 		clocks->count = dep_mclk_table->count;
4692b843c749SSergey Zigachev 	} else if (hwmgr->pp_table_version == PP_TABLE_V0) {
4693b843c749SSergey Zigachev 		mclk_table = hwmgr->dyn_state.vddc_dependency_on_mclk;
4694b843c749SSergey Zigachev 		for (i = 0; i < mclk_table->count; i++)
4695b843c749SSergey Zigachev 			clocks->clock[i] = mclk_table->entries[i].clk * 10;
4696b843c749SSergey Zigachev 		clocks->count = mclk_table->count;
4697b843c749SSergey Zigachev 	}
4698b843c749SSergey Zigachev 	return 0;
4699b843c749SSergey Zigachev }
4700b843c749SSergey Zigachev 
smu7_get_clock_by_type(struct pp_hwmgr * hwmgr,enum amd_pp_clock_type type,struct amd_pp_clocks * clocks)4701b843c749SSergey Zigachev static int smu7_get_clock_by_type(struct pp_hwmgr *hwmgr, enum amd_pp_clock_type type,
4702b843c749SSergey Zigachev 						struct amd_pp_clocks *clocks)
4703b843c749SSergey Zigachev {
4704b843c749SSergey Zigachev 	switch (type) {
4705b843c749SSergey Zigachev 	case amd_pp_sys_clock:
4706b843c749SSergey Zigachev 		smu7_get_sclks(hwmgr, clocks);
4707b843c749SSergey Zigachev 		break;
4708b843c749SSergey Zigachev 	case amd_pp_mem_clock:
4709b843c749SSergey Zigachev 		smu7_get_mclks(hwmgr, clocks);
4710b843c749SSergey Zigachev 		break;
4711b843c749SSergey Zigachev 	default:
4712b843c749SSergey Zigachev 		return -EINVAL;
4713b843c749SSergey Zigachev 	}
4714b843c749SSergey Zigachev 
4715b843c749SSergey Zigachev 	return 0;
4716b843c749SSergey Zigachev }
4717b843c749SSergey Zigachev 
smu7_notify_cac_buffer_info(struct pp_hwmgr * hwmgr,uint32_t virtual_addr_low,uint32_t virtual_addr_hi,uint32_t mc_addr_low,uint32_t mc_addr_hi,uint32_t size)4718b843c749SSergey Zigachev static int smu7_notify_cac_buffer_info(struct pp_hwmgr *hwmgr,
4719b843c749SSergey Zigachev 					uint32_t virtual_addr_low,
4720b843c749SSergey Zigachev 					uint32_t virtual_addr_hi,
4721b843c749SSergey Zigachev 					uint32_t mc_addr_low,
4722b843c749SSergey Zigachev 					uint32_t mc_addr_hi,
4723b843c749SSergey Zigachev 					uint32_t size)
4724b843c749SSergey Zigachev {
4725b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4726b843c749SSergey Zigachev 
4727b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
4728b843c749SSergey Zigachev 					data->soft_regs_start +
4729b843c749SSergey Zigachev 					smum_get_offsetof(hwmgr,
4730b843c749SSergey Zigachev 					SMU_SoftRegisters, DRAM_LOG_ADDR_H),
4731b843c749SSergey Zigachev 					mc_addr_hi);
4732b843c749SSergey Zigachev 
4733b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
4734b843c749SSergey Zigachev 					data->soft_regs_start +
4735b843c749SSergey Zigachev 					smum_get_offsetof(hwmgr,
4736b843c749SSergey Zigachev 					SMU_SoftRegisters, DRAM_LOG_ADDR_L),
4737b843c749SSergey Zigachev 					mc_addr_low);
4738b843c749SSergey Zigachev 
4739b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
4740b843c749SSergey Zigachev 					data->soft_regs_start +
4741b843c749SSergey Zigachev 					smum_get_offsetof(hwmgr,
4742b843c749SSergey Zigachev 					SMU_SoftRegisters, DRAM_LOG_PHY_ADDR_H),
4743b843c749SSergey Zigachev 					virtual_addr_hi);
4744b843c749SSergey Zigachev 
4745b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
4746b843c749SSergey Zigachev 					data->soft_regs_start +
4747b843c749SSergey Zigachev 					smum_get_offsetof(hwmgr,
4748b843c749SSergey Zigachev 					SMU_SoftRegisters, DRAM_LOG_PHY_ADDR_L),
4749b843c749SSergey Zigachev 					virtual_addr_low);
4750b843c749SSergey Zigachev 
4751b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
4752b843c749SSergey Zigachev 					data->soft_regs_start +
4753b843c749SSergey Zigachev 					smum_get_offsetof(hwmgr,
4754b843c749SSergey Zigachev 					SMU_SoftRegisters, DRAM_LOG_BUFF_SIZE),
4755b843c749SSergey Zigachev 					size);
4756b843c749SSergey Zigachev 	return 0;
4757b843c749SSergey Zigachev }
4758b843c749SSergey Zigachev 
smu7_get_max_high_clocks(struct pp_hwmgr * hwmgr,struct amd_pp_simple_clock_info * clocks)4759b843c749SSergey Zigachev static int smu7_get_max_high_clocks(struct pp_hwmgr *hwmgr,
4760b843c749SSergey Zigachev 					struct amd_pp_simple_clock_info *clocks)
4761b843c749SSergey Zigachev {
4762b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4763b843c749SSergey Zigachev 	struct smu7_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
4764b843c749SSergey Zigachev 	struct smu7_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
4765b843c749SSergey Zigachev 
4766b843c749SSergey Zigachev 	if (clocks == NULL)
4767b843c749SSergey Zigachev 		return -EINVAL;
4768b843c749SSergey Zigachev 
4769b843c749SSergey Zigachev 	clocks->memory_max_clock = mclk_table->count > 1 ?
4770b843c749SSergey Zigachev 				mclk_table->dpm_levels[mclk_table->count-1].value :
4771b843c749SSergey Zigachev 				mclk_table->dpm_levels[0].value;
4772b843c749SSergey Zigachev 	clocks->engine_max_clock = sclk_table->count > 1 ?
4773b843c749SSergey Zigachev 				sclk_table->dpm_levels[sclk_table->count-1].value :
4774b843c749SSergey Zigachev 				sclk_table->dpm_levels[0].value;
4775b843c749SSergey Zigachev 	return 0;
4776b843c749SSergey Zigachev }
4777b843c749SSergey Zigachev 
smu7_get_thermal_temperature_range(struct pp_hwmgr * hwmgr,struct PP_TemperatureRange * thermal_data)4778b843c749SSergey Zigachev static int smu7_get_thermal_temperature_range(struct pp_hwmgr *hwmgr,
4779b843c749SSergey Zigachev 		struct PP_TemperatureRange *thermal_data)
4780b843c749SSergey Zigachev {
4781b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4782b843c749SSergey Zigachev 	struct phm_ppt_v1_information *table_info =
4783b843c749SSergey Zigachev 			(struct phm_ppt_v1_information *)hwmgr->pptable;
4784b843c749SSergey Zigachev 
4785b843c749SSergey Zigachev 	memcpy(thermal_data, &SMU7ThermalPolicy[0], sizeof(struct PP_TemperatureRange));
4786b843c749SSergey Zigachev 
4787b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V1)
4788b843c749SSergey Zigachev 		thermal_data->max = table_info->cac_dtp_table->usSoftwareShutdownTemp *
4789b843c749SSergey Zigachev 			PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
4790b843c749SSergey Zigachev 	else if (hwmgr->pp_table_version == PP_TABLE_V0)
4791b843c749SSergey Zigachev 		thermal_data->max = data->thermal_temp_setting.temperature_shutdown *
4792b843c749SSergey Zigachev 			PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
4793b843c749SSergey Zigachev 
4794b843c749SSergey Zigachev 	return 0;
4795b843c749SSergey Zigachev }
4796b843c749SSergey Zigachev 
smu7_check_clk_voltage_valid(struct pp_hwmgr * hwmgr,enum PP_OD_DPM_TABLE_COMMAND type,uint32_t clk,uint32_t voltage)4797b843c749SSergey Zigachev static bool smu7_check_clk_voltage_valid(struct pp_hwmgr *hwmgr,
4798b843c749SSergey Zigachev 					enum PP_OD_DPM_TABLE_COMMAND type,
4799b843c749SSergey Zigachev 					uint32_t clk,
4800b843c749SSergey Zigachev 					uint32_t voltage)
4801b843c749SSergey Zigachev {
4802b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4803b843c749SSergey Zigachev 
4804b843c749SSergey Zigachev 	if (voltage < data->odn_dpm_table.min_vddc || voltage > data->odn_dpm_table.max_vddc) {
4805b843c749SSergey Zigachev 		pr_info("OD voltage is out of range [%d - %d] mV\n",
4806b843c749SSergey Zigachev 						data->odn_dpm_table.min_vddc,
4807b843c749SSergey Zigachev 						data->odn_dpm_table.max_vddc);
4808b843c749SSergey Zigachev 		return false;
4809b843c749SSergey Zigachev 	}
4810b843c749SSergey Zigachev 
4811b843c749SSergey Zigachev 	if (type == PP_OD_EDIT_SCLK_VDDC_TABLE) {
4812b843c749SSergey Zigachev 		if (data->golden_dpm_table.sclk_table.dpm_levels[0].value > clk ||
4813b843c749SSergey Zigachev 			hwmgr->platform_descriptor.overdriveLimit.engineClock < clk) {
4814b843c749SSergey Zigachev 			pr_info("OD engine clock is out of range [%d - %d] MHz\n",
4815b843c749SSergey Zigachev 				data->golden_dpm_table.sclk_table.dpm_levels[0].value/100,
4816b843c749SSergey Zigachev 				hwmgr->platform_descriptor.overdriveLimit.engineClock/100);
4817b843c749SSergey Zigachev 			return false;
4818b843c749SSergey Zigachev 		}
4819b843c749SSergey Zigachev 	} else if (type == PP_OD_EDIT_MCLK_VDDC_TABLE) {
4820b843c749SSergey Zigachev 		if (data->golden_dpm_table.mclk_table.dpm_levels[0].value > clk ||
4821b843c749SSergey Zigachev 			hwmgr->platform_descriptor.overdriveLimit.memoryClock < clk) {
4822b843c749SSergey Zigachev 			pr_info("OD memory clock is out of range [%d - %d] MHz\n",
4823b843c749SSergey Zigachev 				data->golden_dpm_table.mclk_table.dpm_levels[0].value/100,
4824b843c749SSergey Zigachev 				hwmgr->platform_descriptor.overdriveLimit.memoryClock/100);
4825b843c749SSergey Zigachev 			return false;
4826b843c749SSergey Zigachev 		}
4827b843c749SSergey Zigachev 	} else {
4828b843c749SSergey Zigachev 		return false;
4829b843c749SSergey Zigachev 	}
4830b843c749SSergey Zigachev 
4831b843c749SSergey Zigachev 	return true;
4832b843c749SSergey Zigachev }
4833b843c749SSergey Zigachev 
smu7_odn_edit_dpm_table(struct pp_hwmgr * hwmgr,enum PP_OD_DPM_TABLE_COMMAND type,long * input,uint32_t size)4834b843c749SSergey Zigachev static int smu7_odn_edit_dpm_table(struct pp_hwmgr *hwmgr,
4835b843c749SSergey Zigachev 					enum PP_OD_DPM_TABLE_COMMAND type,
4836b843c749SSergey Zigachev 					long *input, uint32_t size)
4837b843c749SSergey Zigachev {
4838b843c749SSergey Zigachev 	uint32_t i;
4839b843c749SSergey Zigachev 	struct phm_odn_clock_levels *podn_dpm_table_in_backend = NULL;
4840b843c749SSergey Zigachev 	struct smu7_odn_clock_voltage_dependency_table *podn_vdd_dep_in_backend = NULL;
4841b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4842b843c749SSergey Zigachev 
4843b843c749SSergey Zigachev 	uint32_t input_clk;
4844b843c749SSergey Zigachev 	uint32_t input_vol;
4845b843c749SSergey Zigachev 	uint32_t input_level;
4846b843c749SSergey Zigachev 
4847b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(input, "NULL user input for clock and voltage",
4848b843c749SSergey Zigachev 				return -EINVAL);
4849b843c749SSergey Zigachev 
4850b843c749SSergey Zigachev 	if (!hwmgr->od_enabled) {
4851b843c749SSergey Zigachev 		pr_info("OverDrive feature not enabled\n");
4852b843c749SSergey Zigachev 		return -EINVAL;
4853b843c749SSergey Zigachev 	}
4854b843c749SSergey Zigachev 
4855b843c749SSergey Zigachev 	if (PP_OD_EDIT_SCLK_VDDC_TABLE == type) {
4856b843c749SSergey Zigachev 		podn_dpm_table_in_backend = &data->odn_dpm_table.odn_core_clock_dpm_levels;
4857b843c749SSergey Zigachev 		podn_vdd_dep_in_backend = &data->odn_dpm_table.vdd_dependency_on_sclk;
4858b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((podn_dpm_table_in_backend && podn_vdd_dep_in_backend),
4859b843c749SSergey Zigachev 				"Failed to get ODN SCLK and Voltage tables",
4860b843c749SSergey Zigachev 				return -EINVAL);
4861b843c749SSergey Zigachev 	} else if (PP_OD_EDIT_MCLK_VDDC_TABLE == type) {
4862b843c749SSergey Zigachev 		podn_dpm_table_in_backend = &data->odn_dpm_table.odn_memory_clock_dpm_levels;
4863b843c749SSergey Zigachev 		podn_vdd_dep_in_backend = &data->odn_dpm_table.vdd_dependency_on_mclk;
4864b843c749SSergey Zigachev 
4865b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((podn_dpm_table_in_backend && podn_vdd_dep_in_backend),
4866b843c749SSergey Zigachev 			"Failed to get ODN MCLK and Voltage tables",
4867b843c749SSergey Zigachev 			return -EINVAL);
4868b843c749SSergey Zigachev 	} else if (PP_OD_RESTORE_DEFAULT_TABLE == type) {
4869b843c749SSergey Zigachev 		smu7_odn_initial_default_setting(hwmgr);
4870b843c749SSergey Zigachev 		return 0;
4871b843c749SSergey Zigachev 	} else if (PP_OD_COMMIT_DPM_TABLE == type) {
4872b843c749SSergey Zigachev 		smu7_check_dpm_table_updated(hwmgr);
4873b843c749SSergey Zigachev 		return 0;
4874b843c749SSergey Zigachev 	} else {
4875b843c749SSergey Zigachev 		return -EINVAL;
4876b843c749SSergey Zigachev 	}
4877b843c749SSergey Zigachev 
4878b843c749SSergey Zigachev 	for (i = 0; i < size; i += 3) {
4879b843c749SSergey Zigachev 		if (i + 3 > size || input[i] >= podn_dpm_table_in_backend->num_of_pl) {
4880b843c749SSergey Zigachev 			pr_info("invalid clock voltage input \n");
4881b843c749SSergey Zigachev 			return 0;
4882b843c749SSergey Zigachev 		}
4883b843c749SSergey Zigachev 		input_level = input[i];
4884b843c749SSergey Zigachev 		input_clk = input[i+1] * 100;
4885b843c749SSergey Zigachev 		input_vol = input[i+2];
4886b843c749SSergey Zigachev 
4887b843c749SSergey Zigachev 		if (smu7_check_clk_voltage_valid(hwmgr, type, input_clk, input_vol)) {
4888b843c749SSergey Zigachev 			podn_dpm_table_in_backend->entries[input_level].clock = input_clk;
4889b843c749SSergey Zigachev 			podn_vdd_dep_in_backend->entries[input_level].clk = input_clk;
4890b843c749SSergey Zigachev 			podn_dpm_table_in_backend->entries[input_level].vddc = input_vol;
4891b843c749SSergey Zigachev 			podn_vdd_dep_in_backend->entries[input_level].vddc = input_vol;
4892b843c749SSergey Zigachev 		} else {
4893b843c749SSergey Zigachev 			return -EINVAL;
4894b843c749SSergey Zigachev 		}
4895b843c749SSergey Zigachev 	}
4896b843c749SSergey Zigachev 
4897b843c749SSergey Zigachev 	return 0;
4898b843c749SSergey Zigachev }
4899b843c749SSergey Zigachev 
smu7_get_power_profile_mode(struct pp_hwmgr * hwmgr,char * buf)4900b843c749SSergey Zigachev static int smu7_get_power_profile_mode(struct pp_hwmgr *hwmgr, char *buf)
4901b843c749SSergey Zigachev {
4902b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4903b843c749SSergey Zigachev 	uint32_t i, size = 0;
4904b843c749SSergey Zigachev 	uint32_t len;
4905b843c749SSergey Zigachev 
4906b843c749SSergey Zigachev 	static const char *profile_name[6] = {"3D_FULL_SCREEN",
4907b843c749SSergey Zigachev 					"POWER_SAVING",
4908b843c749SSergey Zigachev 					"VIDEO",
4909b843c749SSergey Zigachev 					"VR",
4910b843c749SSergey Zigachev 					"COMPUTE",
4911b843c749SSergey Zigachev 					"CUSTOM"};
4912b843c749SSergey Zigachev 
4913b843c749SSergey Zigachev 	static const char *title[8] = {"NUM",
4914b843c749SSergey Zigachev 			"MODE_NAME",
4915b843c749SSergey Zigachev 			"SCLK_UP_HYST",
4916b843c749SSergey Zigachev 			"SCLK_DOWN_HYST",
4917b843c749SSergey Zigachev 			"SCLK_ACTIVE_LEVEL",
4918b843c749SSergey Zigachev 			"MCLK_UP_HYST",
4919b843c749SSergey Zigachev 			"MCLK_DOWN_HYST",
4920b843c749SSergey Zigachev 			"MCLK_ACTIVE_LEVEL"};
4921b843c749SSergey Zigachev 
4922b843c749SSergey Zigachev 	if (!buf)
4923b843c749SSergey Zigachev 		return -EINVAL;
4924b843c749SSergey Zigachev 
4925b843c749SSergey Zigachev 	size += sprintf(buf + size, "%s %16s %16s %16s %16s %16s %16s %16s\n",
4926b843c749SSergey Zigachev 			title[0], title[1], title[2], title[3],
4927b843c749SSergey Zigachev 			title[4], title[5], title[6], title[7]);
4928b843c749SSergey Zigachev 
4929b843c749SSergey Zigachev 	len = sizeof(smu7_profiling) / sizeof(struct profile_mode_setting);
4930b843c749SSergey Zigachev 
4931b843c749SSergey Zigachev 	for (i = 0; i < len; i++) {
4932b843c749SSergey Zigachev 		if (i == hwmgr->power_profile_mode) {
4933b843c749SSergey Zigachev 			size += sprintf(buf + size, "%3d %14s %s: %8d %16d %16d %16d %16d %16d\n",
4934b843c749SSergey Zigachev 			i, profile_name[i], "*",
4935b843c749SSergey Zigachev 			data->current_profile_setting.sclk_up_hyst,
4936b843c749SSergey Zigachev 			data->current_profile_setting.sclk_down_hyst,
4937b843c749SSergey Zigachev 			data->current_profile_setting.sclk_activity,
4938b843c749SSergey Zigachev 			data->current_profile_setting.mclk_up_hyst,
4939b843c749SSergey Zigachev 			data->current_profile_setting.mclk_down_hyst,
4940b843c749SSergey Zigachev 			data->current_profile_setting.mclk_activity);
4941b843c749SSergey Zigachev 			continue;
4942b843c749SSergey Zigachev 		}
4943b843c749SSergey Zigachev 		if (smu7_profiling[i].bupdate_sclk)
4944b843c749SSergey Zigachev 			size += sprintf(buf + size, "%3d %16s: %8d %16d %16d ",
4945b843c749SSergey Zigachev 			i, profile_name[i], smu7_profiling[i].sclk_up_hyst,
4946b843c749SSergey Zigachev 			smu7_profiling[i].sclk_down_hyst,
4947b843c749SSergey Zigachev 			smu7_profiling[i].sclk_activity);
4948b843c749SSergey Zigachev 		else
4949b843c749SSergey Zigachev 			size += sprintf(buf + size, "%3d %16s: %8s %16s %16s ",
4950b843c749SSergey Zigachev 			i, profile_name[i], "-", "-", "-");
4951b843c749SSergey Zigachev 
4952b843c749SSergey Zigachev 		if (smu7_profiling[i].bupdate_mclk)
4953b843c749SSergey Zigachev 			size += sprintf(buf + size, "%16d %16d %16d\n",
4954b843c749SSergey Zigachev 			smu7_profiling[i].mclk_up_hyst,
4955b843c749SSergey Zigachev 			smu7_profiling[i].mclk_down_hyst,
4956b843c749SSergey Zigachev 			smu7_profiling[i].mclk_activity);
4957b843c749SSergey Zigachev 		else
4958b843c749SSergey Zigachev 			size += sprintf(buf + size, "%16s %16s %16s\n",
4959b843c749SSergey Zigachev 			"-", "-", "-");
4960b843c749SSergey Zigachev 	}
4961b843c749SSergey Zigachev 
4962b843c749SSergey Zigachev 	return size;
4963b843c749SSergey Zigachev }
4964b843c749SSergey Zigachev 
smu7_patch_compute_profile_mode(struct pp_hwmgr * hwmgr,enum PP_SMC_POWER_PROFILE requst)4965b843c749SSergey Zigachev static void smu7_patch_compute_profile_mode(struct pp_hwmgr *hwmgr,
4966b843c749SSergey Zigachev 					enum PP_SMC_POWER_PROFILE requst)
4967b843c749SSergey Zigachev {
4968b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4969b843c749SSergey Zigachev 	uint32_t tmp, level;
4970b843c749SSergey Zigachev 
4971b843c749SSergey Zigachev 	if (requst == PP_SMC_POWER_PROFILE_COMPUTE) {
4972b843c749SSergey Zigachev 		if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
4973b843c749SSergey Zigachev 			level = 0;
4974b843c749SSergey Zigachev 			tmp = data->dpm_level_enable_mask.sclk_dpm_enable_mask;
4975b843c749SSergey Zigachev 			while (tmp >>= 1)
4976b843c749SSergey Zigachev 				level++;
4977b843c749SSergey Zigachev 			if (level > 0)
4978b843c749SSergey Zigachev 				smu7_force_clock_level(hwmgr, PP_SCLK, 3 << (level-1));
4979b843c749SSergey Zigachev 		}
4980b843c749SSergey Zigachev 	} else if (hwmgr->power_profile_mode == PP_SMC_POWER_PROFILE_COMPUTE) {
4981b843c749SSergey Zigachev 		smu7_force_clock_level(hwmgr, PP_SCLK, data->dpm_level_enable_mask.sclk_dpm_enable_mask);
4982b843c749SSergey Zigachev 	}
4983b843c749SSergey Zigachev }
4984b843c749SSergey Zigachev 
smu7_set_power_profile_mode(struct pp_hwmgr * hwmgr,long * input,uint32_t size)4985b843c749SSergey Zigachev static int smu7_set_power_profile_mode(struct pp_hwmgr *hwmgr, long *input, uint32_t size)
4986b843c749SSergey Zigachev {
4987b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
4988b843c749SSergey Zigachev 	struct profile_mode_setting tmp;
4989b843c749SSergey Zigachev 	enum PP_SMC_POWER_PROFILE mode;
4990b843c749SSergey Zigachev 
4991b843c749SSergey Zigachev 	if (input == NULL)
4992b843c749SSergey Zigachev 		return -EINVAL;
4993b843c749SSergey Zigachev 
4994b843c749SSergey Zigachev 	mode = input[size];
4995b843c749SSergey Zigachev 	switch (mode) {
4996b843c749SSergey Zigachev 	case PP_SMC_POWER_PROFILE_CUSTOM:
4997b843c749SSergey Zigachev 		if (size < 8)
4998b843c749SSergey Zigachev 			return -EINVAL;
4999b843c749SSergey Zigachev 
5000b843c749SSergey Zigachev 		tmp.bupdate_sclk = input[0];
5001b843c749SSergey Zigachev 		tmp.sclk_up_hyst = input[1];
5002b843c749SSergey Zigachev 		tmp.sclk_down_hyst = input[2];
5003b843c749SSergey Zigachev 		tmp.sclk_activity = input[3];
5004b843c749SSergey Zigachev 		tmp.bupdate_mclk = input[4];
5005b843c749SSergey Zigachev 		tmp.mclk_up_hyst = input[5];
5006b843c749SSergey Zigachev 		tmp.mclk_down_hyst = input[6];
5007b843c749SSergey Zigachev 		tmp.mclk_activity = input[7];
5008b843c749SSergey Zigachev 		if (!smum_update_dpm_settings(hwmgr, &tmp)) {
5009b843c749SSergey Zigachev 			memcpy(&data->current_profile_setting, &tmp, sizeof(struct profile_mode_setting));
5010b843c749SSergey Zigachev 			hwmgr->power_profile_mode = mode;
5011b843c749SSergey Zigachev 		}
5012b843c749SSergey Zigachev 		break;
5013b843c749SSergey Zigachev 	case PP_SMC_POWER_PROFILE_FULLSCREEN3D:
5014b843c749SSergey Zigachev 	case PP_SMC_POWER_PROFILE_POWERSAVING:
5015b843c749SSergey Zigachev 	case PP_SMC_POWER_PROFILE_VIDEO:
5016b843c749SSergey Zigachev 	case PP_SMC_POWER_PROFILE_VR:
5017b843c749SSergey Zigachev 	case PP_SMC_POWER_PROFILE_COMPUTE:
5018b843c749SSergey Zigachev 		if (mode == hwmgr->power_profile_mode)
5019b843c749SSergey Zigachev 			return 0;
5020b843c749SSergey Zigachev 
5021b843c749SSergey Zigachev 		memcpy(&tmp, &smu7_profiling[mode], sizeof(struct profile_mode_setting));
5022b843c749SSergey Zigachev 		if (!smum_update_dpm_settings(hwmgr, &tmp)) {
5023b843c749SSergey Zigachev 			if (tmp.bupdate_sclk) {
5024b843c749SSergey Zigachev 				data->current_profile_setting.bupdate_sclk = tmp.bupdate_sclk;
5025b843c749SSergey Zigachev 				data->current_profile_setting.sclk_up_hyst = tmp.sclk_up_hyst;
5026b843c749SSergey Zigachev 				data->current_profile_setting.sclk_down_hyst = tmp.sclk_down_hyst;
5027b843c749SSergey Zigachev 				data->current_profile_setting.sclk_activity = tmp.sclk_activity;
5028b843c749SSergey Zigachev 			}
5029b843c749SSergey Zigachev 			if (tmp.bupdate_mclk) {
5030b843c749SSergey Zigachev 				data->current_profile_setting.bupdate_mclk = tmp.bupdate_mclk;
5031b843c749SSergey Zigachev 				data->current_profile_setting.mclk_up_hyst = tmp.mclk_up_hyst;
5032b843c749SSergey Zigachev 				data->current_profile_setting.mclk_down_hyst = tmp.mclk_down_hyst;
5033b843c749SSergey Zigachev 				data->current_profile_setting.mclk_activity = tmp.mclk_activity;
5034b843c749SSergey Zigachev 			}
5035b843c749SSergey Zigachev 			smu7_patch_compute_profile_mode(hwmgr, mode);
5036b843c749SSergey Zigachev 			hwmgr->power_profile_mode = mode;
5037b843c749SSergey Zigachev 		}
5038b843c749SSergey Zigachev 		break;
5039b843c749SSergey Zigachev 	default:
5040b843c749SSergey Zigachev 		return -EINVAL;
5041b843c749SSergey Zigachev 	}
5042b843c749SSergey Zigachev 
5043b843c749SSergey Zigachev 	return 0;
5044b843c749SSergey Zigachev }
5045b843c749SSergey Zigachev 
5046b843c749SSergey Zigachev static const struct pp_hwmgr_func smu7_hwmgr_funcs = {
5047b843c749SSergey Zigachev 	.backend_init = &smu7_hwmgr_backend_init,
5048b843c749SSergey Zigachev 	.backend_fini = &smu7_hwmgr_backend_fini,
5049b843c749SSergey Zigachev 	.asic_setup = &smu7_setup_asic_task,
5050b843c749SSergey Zigachev 	.dynamic_state_management_enable = &smu7_enable_dpm_tasks,
5051b843c749SSergey Zigachev 	.apply_state_adjust_rules = smu7_apply_state_adjust_rules,
5052b843c749SSergey Zigachev 	.force_dpm_level = &smu7_force_dpm_level,
5053b843c749SSergey Zigachev 	.power_state_set = smu7_set_power_state_tasks,
5054b843c749SSergey Zigachev 	.get_power_state_size = smu7_get_power_state_size,
5055b843c749SSergey Zigachev 	.get_mclk = smu7_dpm_get_mclk,
5056b843c749SSergey Zigachev 	.get_sclk = smu7_dpm_get_sclk,
5057b843c749SSergey Zigachev 	.patch_boot_state = smu7_dpm_patch_boot_state,
5058b843c749SSergey Zigachev 	.get_pp_table_entry = smu7_get_pp_table_entry,
5059b843c749SSergey Zigachev 	.get_num_of_pp_table_entries = smu7_get_number_of_powerplay_table_entries,
5060b843c749SSergey Zigachev 	.powerdown_uvd = smu7_powerdown_uvd,
5061b843c749SSergey Zigachev 	.powergate_uvd = smu7_powergate_uvd,
5062b843c749SSergey Zigachev 	.powergate_vce = smu7_powergate_vce,
5063b843c749SSergey Zigachev 	.disable_clock_power_gating = smu7_disable_clock_power_gating,
5064b843c749SSergey Zigachev 	.update_clock_gatings = smu7_update_clock_gatings,
5065b843c749SSergey Zigachev 	.notify_smc_display_config_after_ps_adjustment = smu7_notify_smc_display_config_after_ps_adjustment,
5066b843c749SSergey Zigachev 	.display_config_changed = smu7_display_configuration_changed_task,
5067b843c749SSergey Zigachev 	.set_max_fan_pwm_output = smu7_set_max_fan_pwm_output,
5068b843c749SSergey Zigachev 	.set_max_fan_rpm_output = smu7_set_max_fan_rpm_output,
5069b843c749SSergey Zigachev 	.stop_thermal_controller = smu7_thermal_stop_thermal_controller,
5070b843c749SSergey Zigachev 	.get_fan_speed_info = smu7_fan_ctrl_get_fan_speed_info,
5071b843c749SSergey Zigachev 	.get_fan_speed_percent = smu7_fan_ctrl_get_fan_speed_percent,
5072b843c749SSergey Zigachev 	.set_fan_speed_percent = smu7_fan_ctrl_set_fan_speed_percent,
5073b843c749SSergey Zigachev 	.reset_fan_speed_to_default = smu7_fan_ctrl_reset_fan_speed_to_default,
5074b843c749SSergey Zigachev 	.get_fan_speed_rpm = smu7_fan_ctrl_get_fan_speed_rpm,
5075b843c749SSergey Zigachev 	.set_fan_speed_rpm = smu7_fan_ctrl_set_fan_speed_rpm,
5076b843c749SSergey Zigachev 	.uninitialize_thermal_controller = smu7_thermal_ctrl_uninitialize_thermal_controller,
5077b843c749SSergey Zigachev 	.register_irq_handlers = smu7_register_irq_handlers,
5078b843c749SSergey Zigachev 	.check_smc_update_required_for_display_configuration = smu7_check_smc_update_required_for_display_configuration,
5079b843c749SSergey Zigachev 	.check_states_equal = smu7_check_states_equal,
5080b843c749SSergey Zigachev 	.set_fan_control_mode = smu7_set_fan_control_mode,
5081b843c749SSergey Zigachev 	.get_fan_control_mode = smu7_get_fan_control_mode,
5082b843c749SSergey Zigachev 	.force_clock_level = smu7_force_clock_level,
5083b843c749SSergey Zigachev 	.print_clock_levels = smu7_print_clock_levels,
5084b843c749SSergey Zigachev 	.powergate_gfx = smu7_powergate_gfx,
5085b843c749SSergey Zigachev 	.get_sclk_od = smu7_get_sclk_od,
5086b843c749SSergey Zigachev 	.set_sclk_od = smu7_set_sclk_od,
5087b843c749SSergey Zigachev 	.get_mclk_od = smu7_get_mclk_od,
5088b843c749SSergey Zigachev 	.set_mclk_od = smu7_set_mclk_od,
5089b843c749SSergey Zigachev 	.get_clock_by_type = smu7_get_clock_by_type,
5090b843c749SSergey Zigachev 	.read_sensor = smu7_read_sensor,
5091b843c749SSergey Zigachev 	.dynamic_state_management_disable = smu7_disable_dpm_tasks,
5092b843c749SSergey Zigachev 	.avfs_control = smu7_avfs_control,
5093b843c749SSergey Zigachev 	.disable_smc_firmware_ctf = smu7_thermal_disable_alert,
5094b843c749SSergey Zigachev 	.start_thermal_controller = smu7_start_thermal_controller,
5095b843c749SSergey Zigachev 	.notify_cac_buffer_info = smu7_notify_cac_buffer_info,
5096b843c749SSergey Zigachev 	.get_max_high_clocks = smu7_get_max_high_clocks,
5097b843c749SSergey Zigachev 	.get_thermal_temperature_range = smu7_get_thermal_temperature_range,
5098b843c749SSergey Zigachev 	.odn_edit_dpm_table = smu7_odn_edit_dpm_table,
5099b843c749SSergey Zigachev 	.set_power_limit = smu7_set_power_limit,
5100b843c749SSergey Zigachev 	.get_power_profile_mode = smu7_get_power_profile_mode,
5101b843c749SSergey Zigachev 	.set_power_profile_mode = smu7_set_power_profile_mode,
5102b843c749SSergey Zigachev };
5103b843c749SSergey Zigachev 
smu7_get_sleep_divider_id_from_clock(uint32_t clock,uint32_t clock_insr)5104b843c749SSergey Zigachev uint8_t smu7_get_sleep_divider_id_from_clock(uint32_t clock,
5105b843c749SSergey Zigachev 		uint32_t clock_insr)
5106b843c749SSergey Zigachev {
5107b843c749SSergey Zigachev 	uint8_t i;
5108b843c749SSergey Zigachev 	uint32_t temp;
5109b843c749SSergey Zigachev 	uint32_t min = max(clock_insr, (uint32_t)SMU7_MINIMUM_ENGINE_CLOCK);
5110b843c749SSergey Zigachev 
5111b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((clock >= min), "Engine clock can't satisfy stutter requirement!", return 0);
5112b843c749SSergey Zigachev 	for (i = SMU7_MAX_DEEPSLEEP_DIVIDER_ID;  ; i--) {
5113b843c749SSergey Zigachev 		temp = clock >> i;
5114b843c749SSergey Zigachev 
5115b843c749SSergey Zigachev 		if (temp >= min || i == 0)
5116b843c749SSergey Zigachev 			break;
5117b843c749SSergey Zigachev 	}
5118b843c749SSergey Zigachev 	return i;
5119b843c749SSergey Zigachev }
5120b843c749SSergey Zigachev 
5121*78973132SSergey Zigachev int smu7_init_function_pointers(struct pp_hwmgr *hwmgr);
smu7_init_function_pointers(struct pp_hwmgr * hwmgr)5122b843c749SSergey Zigachev int smu7_init_function_pointers(struct pp_hwmgr *hwmgr)
5123b843c749SSergey Zigachev {
5124b843c749SSergey Zigachev 	int ret = 0;
5125b843c749SSergey Zigachev 
5126b843c749SSergey Zigachev 	hwmgr->hwmgr_func = &smu7_hwmgr_funcs;
5127b843c749SSergey Zigachev 	if (hwmgr->pp_table_version == PP_TABLE_V0)
5128b843c749SSergey Zigachev 		hwmgr->pptable_func = &pptable_funcs;
5129b843c749SSergey Zigachev 	else if (hwmgr->pp_table_version == PP_TABLE_V1)
5130b843c749SSergey Zigachev 		hwmgr->pptable_func = &pptable_v1_0_funcs;
5131b843c749SSergey Zigachev 
5132b843c749SSergey Zigachev 	return ret;
5133b843c749SSergey Zigachev }
5134