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