xref: /openbsd/sys/dev/pci/drm/amd/pm/powerplay/hwmgr/hwmgr.c (revision f005ef32)
1ad8b1aafSjsg /*
2ad8b1aafSjsg  * Copyright 2015 Advanced Micro Devices, Inc.
3ad8b1aafSjsg  *
4ad8b1aafSjsg  * Permission is hereby granted, free of charge, to any person obtaining a
5ad8b1aafSjsg  * copy of this software and associated documentation files (the "Software"),
6ad8b1aafSjsg  * to deal in the Software without restriction, including without limitation
7ad8b1aafSjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8ad8b1aafSjsg  * and/or sell copies of the Software, and to permit persons to whom the
9ad8b1aafSjsg  * Software is furnished to do so, subject to the following conditions:
10ad8b1aafSjsg  *
11ad8b1aafSjsg  * The above copyright notice and this permission notice shall be included in
12ad8b1aafSjsg  * all copies or substantial portions of the Software.
13ad8b1aafSjsg  *
14ad8b1aafSjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15ad8b1aafSjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16ad8b1aafSjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17ad8b1aafSjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18ad8b1aafSjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ad8b1aafSjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20ad8b1aafSjsg  * OTHER DEALINGS IN THE SOFTWARE.
21ad8b1aafSjsg  *
22ad8b1aafSjsg  */
23ad8b1aafSjsg 
24ad8b1aafSjsg #include "pp_debug.h"
25ad8b1aafSjsg #include <linux/delay.h>
26ad8b1aafSjsg #include <linux/kernel.h>
27ad8b1aafSjsg #include <linux/slab.h>
28ad8b1aafSjsg #include <linux/types.h>
29ad8b1aafSjsg #include <linux/pci.h>
30ad8b1aafSjsg #include <drm/amdgpu_drm.h>
31ad8b1aafSjsg #include "power_state.h"
32ad8b1aafSjsg #include "hwmgr.h"
33ad8b1aafSjsg #include "ppsmc.h"
34ad8b1aafSjsg #include "amd_acpi.h"
35ad8b1aafSjsg #include "pp_psm.h"
36*5ca02815Sjsg #include "vega10_hwmgr.h"
37ad8b1aafSjsg 
38ad8b1aafSjsg extern const struct pp_smumgr_func ci_smu_funcs;
39ad8b1aafSjsg extern const struct pp_smumgr_func smu8_smu_funcs;
40ad8b1aafSjsg extern const struct pp_smumgr_func iceland_smu_funcs;
41ad8b1aafSjsg extern const struct pp_smumgr_func tonga_smu_funcs;
42ad8b1aafSjsg extern const struct pp_smumgr_func fiji_smu_funcs;
43ad8b1aafSjsg extern const struct pp_smumgr_func polaris10_smu_funcs;
44ad8b1aafSjsg extern const struct pp_smumgr_func vegam_smu_funcs;
45ad8b1aafSjsg extern const struct pp_smumgr_func vega10_smu_funcs;
46ad8b1aafSjsg extern const struct pp_smumgr_func vega12_smu_funcs;
47ad8b1aafSjsg extern const struct pp_smumgr_func smu10_smu_funcs;
48ad8b1aafSjsg extern const struct pp_smumgr_func vega20_smu_funcs;
49ad8b1aafSjsg 
50ad8b1aafSjsg extern int smu10_init_function_pointers(struct pp_hwmgr *hwmgr);
51ad8b1aafSjsg 
52ad8b1aafSjsg static int polaris_set_asic_special_caps(struct pp_hwmgr *hwmgr);
53ad8b1aafSjsg static void hwmgr_init_default_caps(struct pp_hwmgr *hwmgr);
54ad8b1aafSjsg static int hwmgr_set_user_specify_caps(struct pp_hwmgr *hwmgr);
55ad8b1aafSjsg static int fiji_set_asic_special_caps(struct pp_hwmgr *hwmgr);
56ad8b1aafSjsg static int tonga_set_asic_special_caps(struct pp_hwmgr *hwmgr);
57ad8b1aafSjsg static int topaz_set_asic_special_caps(struct pp_hwmgr *hwmgr);
58ad8b1aafSjsg static int ci_set_asic_special_caps(struct pp_hwmgr *hwmgr);
59ad8b1aafSjsg 
60ad8b1aafSjsg 
hwmgr_init_workload_prority(struct pp_hwmgr * hwmgr)61ad8b1aafSjsg static void hwmgr_init_workload_prority(struct pp_hwmgr *hwmgr)
62ad8b1aafSjsg {
63ad8b1aafSjsg 	hwmgr->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
64ad8b1aafSjsg 	hwmgr->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
65ad8b1aafSjsg 	hwmgr->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
66ad8b1aafSjsg 	hwmgr->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
67ad8b1aafSjsg 	hwmgr->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
68ad8b1aafSjsg 	hwmgr->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
69ad8b1aafSjsg 
70ad8b1aafSjsg 	hwmgr->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
71ad8b1aafSjsg 	hwmgr->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
72ad8b1aafSjsg 	hwmgr->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
73ad8b1aafSjsg 	hwmgr->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
74ad8b1aafSjsg 	hwmgr->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
75ad8b1aafSjsg 	hwmgr->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
76ad8b1aafSjsg }
77ad8b1aafSjsg 
hwmgr_early_init(struct pp_hwmgr * hwmgr)78ad8b1aafSjsg int hwmgr_early_init(struct pp_hwmgr *hwmgr)
79ad8b1aafSjsg {
80ad8b1aafSjsg 	struct amdgpu_device *adev;
81ad8b1aafSjsg 
82ad8b1aafSjsg 	if (!hwmgr)
83ad8b1aafSjsg 		return -EINVAL;
84ad8b1aafSjsg 
85ad8b1aafSjsg 	hwmgr->usec_timeout = AMD_MAX_USEC_TIMEOUT;
86ad8b1aafSjsg 	hwmgr->pp_table_version = PP_TABLE_V1;
87ad8b1aafSjsg 	hwmgr->dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
88ad8b1aafSjsg 	hwmgr->request_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
89ad8b1aafSjsg 	hwmgr_init_default_caps(hwmgr);
90ad8b1aafSjsg 	hwmgr_set_user_specify_caps(hwmgr);
91ad8b1aafSjsg 	hwmgr->fan_ctrl_is_in_default_mode = true;
92ad8b1aafSjsg 	hwmgr_init_workload_prority(hwmgr);
93ad8b1aafSjsg 	hwmgr->gfxoff_state_changed_by_workload = false;
94ad8b1aafSjsg 
95ad8b1aafSjsg 	adev = hwmgr->adev;
96ad8b1aafSjsg 
97ad8b1aafSjsg 	switch (hwmgr->chip_family) {
98ad8b1aafSjsg 	case AMDGPU_FAMILY_CI:
99ad8b1aafSjsg 		adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
100ad8b1aafSjsg 		hwmgr->smumgr_funcs = &ci_smu_funcs;
101ad8b1aafSjsg 		ci_set_asic_special_caps(hwmgr);
102ad8b1aafSjsg 		hwmgr->feature_mask &= ~(PP_VBI_TIME_SUPPORT_MASK |
103ad8b1aafSjsg 					 PP_ENABLE_GFX_CG_THRU_SMU |
104ad8b1aafSjsg 					 PP_GFXOFF_MASK);
105ad8b1aafSjsg 		hwmgr->pp_table_version = PP_TABLE_V0;
106ad8b1aafSjsg 		hwmgr->od_enabled = false;
107ad8b1aafSjsg 		smu7_init_function_pointers(hwmgr);
108ad8b1aafSjsg 		break;
109ad8b1aafSjsg 	case AMDGPU_FAMILY_CZ:
110ad8b1aafSjsg 		adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
111ad8b1aafSjsg 		hwmgr->od_enabled = false;
112ad8b1aafSjsg 		hwmgr->smumgr_funcs = &smu8_smu_funcs;
113ad8b1aafSjsg 		hwmgr->feature_mask &= ~PP_GFXOFF_MASK;
114ad8b1aafSjsg 		smu8_init_function_pointers(hwmgr);
115ad8b1aafSjsg 		break;
116ad8b1aafSjsg 	case AMDGPU_FAMILY_VI:
117ad8b1aafSjsg 		adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
118ad8b1aafSjsg 		hwmgr->feature_mask &= ~PP_GFXOFF_MASK;
119ad8b1aafSjsg 		switch (hwmgr->chip_id) {
120ad8b1aafSjsg 		case CHIP_TOPAZ:
121ad8b1aafSjsg 			hwmgr->smumgr_funcs = &iceland_smu_funcs;
122ad8b1aafSjsg 			topaz_set_asic_special_caps(hwmgr);
123ad8b1aafSjsg 			hwmgr->feature_mask &= ~(PP_VBI_TIME_SUPPORT_MASK |
124ad8b1aafSjsg 						PP_ENABLE_GFX_CG_THRU_SMU);
125ad8b1aafSjsg 			hwmgr->pp_table_version = PP_TABLE_V0;
126ad8b1aafSjsg 			hwmgr->od_enabled = false;
127ad8b1aafSjsg 			break;
128ad8b1aafSjsg 		case CHIP_TONGA:
129ad8b1aafSjsg 			hwmgr->smumgr_funcs = &tonga_smu_funcs;
130ad8b1aafSjsg 			tonga_set_asic_special_caps(hwmgr);
131ad8b1aafSjsg 			hwmgr->feature_mask &= ~PP_VBI_TIME_SUPPORT_MASK;
132ad8b1aafSjsg 			break;
133ad8b1aafSjsg 		case CHIP_FIJI:
134ad8b1aafSjsg 			hwmgr->smumgr_funcs = &fiji_smu_funcs;
135ad8b1aafSjsg 			fiji_set_asic_special_caps(hwmgr);
136ad8b1aafSjsg 			hwmgr->feature_mask &= ~(PP_VBI_TIME_SUPPORT_MASK |
137ad8b1aafSjsg 						PP_ENABLE_GFX_CG_THRU_SMU);
138ad8b1aafSjsg 			break;
139ad8b1aafSjsg 		case CHIP_POLARIS11:
140ad8b1aafSjsg 		case CHIP_POLARIS10:
141ad8b1aafSjsg 		case CHIP_POLARIS12:
142ad8b1aafSjsg 			hwmgr->smumgr_funcs = &polaris10_smu_funcs;
143ad8b1aafSjsg 			polaris_set_asic_special_caps(hwmgr);
144ad8b1aafSjsg 			hwmgr->feature_mask &= ~(PP_UVD_HANDSHAKE_MASK);
145ad8b1aafSjsg 			break;
146ad8b1aafSjsg 		case CHIP_VEGAM:
147ad8b1aafSjsg 			hwmgr->smumgr_funcs = &vegam_smu_funcs;
148ad8b1aafSjsg 			polaris_set_asic_special_caps(hwmgr);
149ad8b1aafSjsg 			hwmgr->feature_mask &= ~(PP_UVD_HANDSHAKE_MASK);
150ad8b1aafSjsg 			break;
151ad8b1aafSjsg 		default:
152ad8b1aafSjsg 			return -EINVAL;
153ad8b1aafSjsg 		}
154ad8b1aafSjsg 		smu7_init_function_pointers(hwmgr);
155ad8b1aafSjsg 		break;
156ad8b1aafSjsg 	case AMDGPU_FAMILY_AI:
157ad8b1aafSjsg 		switch (hwmgr->chip_id) {
158ad8b1aafSjsg 		case CHIP_VEGA10:
159ad8b1aafSjsg 			adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
160ad8b1aafSjsg 			hwmgr->feature_mask &= ~PP_GFXOFF_MASK;
161ad8b1aafSjsg 			hwmgr->smumgr_funcs = &vega10_smu_funcs;
162ad8b1aafSjsg 			vega10_hwmgr_init(hwmgr);
163ad8b1aafSjsg 			break;
164ad8b1aafSjsg 		case CHIP_VEGA12:
165ad8b1aafSjsg 			hwmgr->smumgr_funcs = &vega12_smu_funcs;
166ad8b1aafSjsg 			vega12_hwmgr_init(hwmgr);
167ad8b1aafSjsg 			break;
168ad8b1aafSjsg 		case CHIP_VEGA20:
169ad8b1aafSjsg 			adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
170ad8b1aafSjsg 			hwmgr->feature_mask &= ~PP_GFXOFF_MASK;
171ad8b1aafSjsg 			hwmgr->smumgr_funcs = &vega20_smu_funcs;
172ad8b1aafSjsg 			vega20_hwmgr_init(hwmgr);
173ad8b1aafSjsg 			break;
174ad8b1aafSjsg 		default:
175ad8b1aafSjsg 			return -EINVAL;
176ad8b1aafSjsg 		}
177ad8b1aafSjsg 		break;
178ad8b1aafSjsg 	case AMDGPU_FAMILY_RV:
179ad8b1aafSjsg 		switch (hwmgr->chip_id) {
180ad8b1aafSjsg 		case CHIP_RAVEN:
181ad8b1aafSjsg 			hwmgr->od_enabled = false;
182ad8b1aafSjsg 			hwmgr->smumgr_funcs = &smu10_smu_funcs;
183ad8b1aafSjsg 			smu10_init_function_pointers(hwmgr);
184ad8b1aafSjsg 			break;
185ad8b1aafSjsg 		default:
186ad8b1aafSjsg 			return -EINVAL;
187ad8b1aafSjsg 		}
188ad8b1aafSjsg 		break;
189ad8b1aafSjsg 	default:
190ad8b1aafSjsg 		return -EINVAL;
191ad8b1aafSjsg 	}
192ad8b1aafSjsg 
193ad8b1aafSjsg 	return 0;
194ad8b1aafSjsg }
195ad8b1aafSjsg 
hwmgr_sw_init(struct pp_hwmgr * hwmgr)196ad8b1aafSjsg int hwmgr_sw_init(struct pp_hwmgr *hwmgr)
197ad8b1aafSjsg {
198ad8b1aafSjsg 	if (!hwmgr || !hwmgr->smumgr_funcs || !hwmgr->smumgr_funcs->smu_init)
199ad8b1aafSjsg 		return -EINVAL;
200ad8b1aafSjsg 
201ad8b1aafSjsg 	phm_register_irq_handlers(hwmgr);
202ad8b1aafSjsg 	pr_info("hwmgr_sw_init smu backed is %s\n", hwmgr->smumgr_funcs->name);
203ad8b1aafSjsg 
204ad8b1aafSjsg 	return hwmgr->smumgr_funcs->smu_init(hwmgr);
205ad8b1aafSjsg }
206ad8b1aafSjsg 
207ad8b1aafSjsg 
hwmgr_sw_fini(struct pp_hwmgr * hwmgr)208ad8b1aafSjsg int hwmgr_sw_fini(struct pp_hwmgr *hwmgr)
209ad8b1aafSjsg {
210ad8b1aafSjsg 	if (hwmgr && hwmgr->smumgr_funcs && hwmgr->smumgr_funcs->smu_fini)
211ad8b1aafSjsg 		hwmgr->smumgr_funcs->smu_fini(hwmgr);
212ad8b1aafSjsg 
213ad8b1aafSjsg 	return 0;
214ad8b1aafSjsg }
215ad8b1aafSjsg 
hwmgr_hw_init(struct pp_hwmgr * hwmgr)216ad8b1aafSjsg int hwmgr_hw_init(struct pp_hwmgr *hwmgr)
217ad8b1aafSjsg {
218ad8b1aafSjsg 	int ret = 0;
219ad8b1aafSjsg 
220ad8b1aafSjsg 	hwmgr->pp_one_vf = amdgpu_sriov_is_pp_one_vf((struct amdgpu_device *)hwmgr->adev);
221ad8b1aafSjsg 	hwmgr->pm_en = (amdgpu_dpm && (hwmgr->not_vf || hwmgr->pp_one_vf))
222ad8b1aafSjsg 			? true : false;
223ad8b1aafSjsg 	if (!hwmgr->pm_en)
224ad8b1aafSjsg 		return 0;
225ad8b1aafSjsg 
226ad8b1aafSjsg 	if (!hwmgr->pptable_func ||
227ad8b1aafSjsg 	    !hwmgr->pptable_func->pptable_init ||
228ad8b1aafSjsg 	    !hwmgr->hwmgr_func->backend_init) {
229ad8b1aafSjsg 		hwmgr->pm_en = false;
230ad8b1aafSjsg 		pr_info("dpm not supported \n");
231ad8b1aafSjsg 		return 0;
232ad8b1aafSjsg 	}
233ad8b1aafSjsg 
234ad8b1aafSjsg 	ret = hwmgr->pptable_func->pptable_init(hwmgr);
235ad8b1aafSjsg 	if (ret)
236ad8b1aafSjsg 		goto err;
237ad8b1aafSjsg 
238ad8b1aafSjsg 	((struct amdgpu_device *)hwmgr->adev)->pm.no_fan =
239ad8b1aafSjsg 				hwmgr->thermal_controller.fanInfo.bNoFan;
240ad8b1aafSjsg 
241ad8b1aafSjsg 	ret = hwmgr->hwmgr_func->backend_init(hwmgr);
242ad8b1aafSjsg 	if (ret)
243ad8b1aafSjsg 		goto err1;
244ad8b1aafSjsg  /* make sure dc limits are valid */
245ad8b1aafSjsg 	if ((hwmgr->dyn_state.max_clock_voltage_on_dc.sclk == 0) ||
246ad8b1aafSjsg 			(hwmgr->dyn_state.max_clock_voltage_on_dc.mclk == 0))
247ad8b1aafSjsg 			hwmgr->dyn_state.max_clock_voltage_on_dc =
248ad8b1aafSjsg 					hwmgr->dyn_state.max_clock_voltage_on_ac;
249ad8b1aafSjsg 
250ad8b1aafSjsg 	ret = psm_init_power_state_table(hwmgr);
251ad8b1aafSjsg 	if (ret)
252ad8b1aafSjsg 		goto err2;
253ad8b1aafSjsg 
254ad8b1aafSjsg 	ret = phm_setup_asic(hwmgr);
255ad8b1aafSjsg 	if (ret)
256ad8b1aafSjsg 		goto err2;
257ad8b1aafSjsg 
258ad8b1aafSjsg 	ret = phm_enable_dynamic_state_management(hwmgr);
259ad8b1aafSjsg 	if (ret)
260ad8b1aafSjsg 		goto err2;
261ad8b1aafSjsg 	ret = phm_start_thermal_controller(hwmgr);
262ad8b1aafSjsg 	ret |= psm_set_performance_states(hwmgr);
263ad8b1aafSjsg 	if (ret)
264ad8b1aafSjsg 		goto err2;
265ad8b1aafSjsg 
266ad8b1aafSjsg 	((struct amdgpu_device *)hwmgr->adev)->pm.dpm_enabled = true;
267ad8b1aafSjsg 
268ad8b1aafSjsg 	return 0;
269ad8b1aafSjsg err2:
270ad8b1aafSjsg 	if (hwmgr->hwmgr_func->backend_fini)
271ad8b1aafSjsg 		hwmgr->hwmgr_func->backend_fini(hwmgr);
272ad8b1aafSjsg err1:
273ad8b1aafSjsg 	if (hwmgr->pptable_func->pptable_fini)
274ad8b1aafSjsg 		hwmgr->pptable_func->pptable_fini(hwmgr);
275ad8b1aafSjsg err:
276ad8b1aafSjsg 	return ret;
277ad8b1aafSjsg }
278ad8b1aafSjsg 
hwmgr_hw_fini(struct pp_hwmgr * hwmgr)279ad8b1aafSjsg int hwmgr_hw_fini(struct pp_hwmgr *hwmgr)
280ad8b1aafSjsg {
281ad8b1aafSjsg 	if (!hwmgr || !hwmgr->pm_en || !hwmgr->not_vf)
282ad8b1aafSjsg 		return 0;
283ad8b1aafSjsg 
284ad8b1aafSjsg 	phm_stop_thermal_controller(hwmgr);
285ad8b1aafSjsg 	psm_set_boot_states(hwmgr);
286ad8b1aafSjsg 	psm_adjust_power_state_dynamic(hwmgr, true, NULL);
287ad8b1aafSjsg 	phm_disable_dynamic_state_management(hwmgr);
288ad8b1aafSjsg 	phm_disable_clock_power_gatings(hwmgr);
289ad8b1aafSjsg 
290ad8b1aafSjsg 	if (hwmgr->hwmgr_func->backend_fini)
291ad8b1aafSjsg 		hwmgr->hwmgr_func->backend_fini(hwmgr);
292ad8b1aafSjsg 	if (hwmgr->pptable_func->pptable_fini)
293ad8b1aafSjsg 		hwmgr->pptable_func->pptable_fini(hwmgr);
294ad8b1aafSjsg 	return psm_fini_power_state_table(hwmgr);
295ad8b1aafSjsg }
296ad8b1aafSjsg 
hwmgr_suspend(struct pp_hwmgr * hwmgr)297ad8b1aafSjsg int hwmgr_suspend(struct pp_hwmgr *hwmgr)
298ad8b1aafSjsg {
299ad8b1aafSjsg 	int ret = 0;
300ad8b1aafSjsg 
301ad8b1aafSjsg 	if (!hwmgr || !hwmgr->pm_en || !hwmgr->not_vf)
302ad8b1aafSjsg 		return 0;
303ad8b1aafSjsg 
304ad8b1aafSjsg 	phm_disable_smc_firmware_ctf(hwmgr);
305ad8b1aafSjsg 	ret = psm_set_boot_states(hwmgr);
306ad8b1aafSjsg 	if (ret)
307ad8b1aafSjsg 		return ret;
308ad8b1aafSjsg 	ret = psm_adjust_power_state_dynamic(hwmgr, true, NULL);
309ad8b1aafSjsg 	if (ret)
310ad8b1aafSjsg 		return ret;
311ad8b1aafSjsg 	ret = phm_power_down_asic(hwmgr);
312ad8b1aafSjsg 
313ad8b1aafSjsg 	return ret;
314ad8b1aafSjsg }
315ad8b1aafSjsg 
hwmgr_resume(struct pp_hwmgr * hwmgr)316ad8b1aafSjsg int hwmgr_resume(struct pp_hwmgr *hwmgr)
317ad8b1aafSjsg {
318ad8b1aafSjsg 	int ret = 0;
319ad8b1aafSjsg 
320ad8b1aafSjsg 	if (!hwmgr)
321ad8b1aafSjsg 		return -EINVAL;
322ad8b1aafSjsg 
323ad8b1aafSjsg 	if (!hwmgr->not_vf || !hwmgr->pm_en)
324ad8b1aafSjsg 		return 0;
325ad8b1aafSjsg 
326ad8b1aafSjsg 	ret = phm_setup_asic(hwmgr);
327ad8b1aafSjsg 	if (ret)
328ad8b1aafSjsg 		return ret;
329ad8b1aafSjsg 
330ad8b1aafSjsg 	ret = phm_enable_dynamic_state_management(hwmgr);
331ad8b1aafSjsg 	if (ret)
332ad8b1aafSjsg 		return ret;
333ad8b1aafSjsg 	ret = phm_start_thermal_controller(hwmgr);
334ad8b1aafSjsg 	ret |= psm_set_performance_states(hwmgr);
335ad8b1aafSjsg 	if (ret)
336ad8b1aafSjsg 		return ret;
337ad8b1aafSjsg 
338ad8b1aafSjsg 	ret = psm_adjust_power_state_dynamic(hwmgr, false, NULL);
339ad8b1aafSjsg 
340ad8b1aafSjsg 	return ret;
341ad8b1aafSjsg }
342ad8b1aafSjsg 
power_state_convert(enum amd_pm_state_type state)343ad8b1aafSjsg static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type  state)
344ad8b1aafSjsg {
345ad8b1aafSjsg 	switch (state) {
346ad8b1aafSjsg 	case POWER_STATE_TYPE_BATTERY:
347ad8b1aafSjsg 		return PP_StateUILabel_Battery;
348ad8b1aafSjsg 	case POWER_STATE_TYPE_BALANCED:
349ad8b1aafSjsg 		return PP_StateUILabel_Balanced;
350ad8b1aafSjsg 	case POWER_STATE_TYPE_PERFORMANCE:
351ad8b1aafSjsg 		return PP_StateUILabel_Performance;
352ad8b1aafSjsg 	default:
353ad8b1aafSjsg 		return PP_StateUILabel_None;
354ad8b1aafSjsg 	}
355ad8b1aafSjsg }
356ad8b1aafSjsg 
hwmgr_handle_task(struct pp_hwmgr * hwmgr,enum amd_pp_task task_id,enum amd_pm_state_type * user_state)357ad8b1aafSjsg int hwmgr_handle_task(struct pp_hwmgr *hwmgr, enum amd_pp_task task_id,
358ad8b1aafSjsg 		enum amd_pm_state_type *user_state)
359ad8b1aafSjsg {
360ad8b1aafSjsg 	int ret = 0;
361ad8b1aafSjsg 
362ad8b1aafSjsg 	if (hwmgr == NULL)
363ad8b1aafSjsg 		return -EINVAL;
364ad8b1aafSjsg 
365ad8b1aafSjsg 	switch (task_id) {
366ad8b1aafSjsg 	case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
367ad8b1aafSjsg 		if (!hwmgr->not_vf)
368ad8b1aafSjsg 			return ret;
369ad8b1aafSjsg 		ret = phm_pre_display_configuration_changed(hwmgr);
370ad8b1aafSjsg 		if (ret)
371ad8b1aafSjsg 			return ret;
372ad8b1aafSjsg 		ret = phm_set_cpu_power_state(hwmgr);
373ad8b1aafSjsg 		if (ret)
374ad8b1aafSjsg 			return ret;
375ad8b1aafSjsg 		ret = psm_set_performance_states(hwmgr);
376ad8b1aafSjsg 		if (ret)
377ad8b1aafSjsg 			return ret;
378ad8b1aafSjsg 		ret = psm_adjust_power_state_dynamic(hwmgr, false, NULL);
379ad8b1aafSjsg 		break;
380ad8b1aafSjsg 	case AMD_PP_TASK_ENABLE_USER_STATE:
381ad8b1aafSjsg 	{
382ad8b1aafSjsg 		enum PP_StateUILabel requested_ui_label;
383ad8b1aafSjsg 		struct pp_power_state *requested_ps = NULL;
384ad8b1aafSjsg 
385ad8b1aafSjsg 		if (!hwmgr->not_vf)
386ad8b1aafSjsg 			return ret;
387ad8b1aafSjsg 		if (user_state == NULL) {
388ad8b1aafSjsg 			ret = -EINVAL;
389ad8b1aafSjsg 			break;
390ad8b1aafSjsg 		}
391ad8b1aafSjsg 
392ad8b1aafSjsg 		requested_ui_label = power_state_convert(*user_state);
393ad8b1aafSjsg 		ret = psm_set_user_performance_state(hwmgr, requested_ui_label, &requested_ps);
394ad8b1aafSjsg 		if (ret)
395ad8b1aafSjsg 			return ret;
396ad8b1aafSjsg 		ret = psm_adjust_power_state_dynamic(hwmgr, true, requested_ps);
397ad8b1aafSjsg 		break;
398ad8b1aafSjsg 	}
399ad8b1aafSjsg 	case AMD_PP_TASK_COMPLETE_INIT:
400ad8b1aafSjsg 	case AMD_PP_TASK_READJUST_POWER_STATE:
401ad8b1aafSjsg 		ret = psm_adjust_power_state_dynamic(hwmgr, true, NULL);
402ad8b1aafSjsg 		break;
403ad8b1aafSjsg 	default:
404ad8b1aafSjsg 		break;
405ad8b1aafSjsg 	}
406ad8b1aafSjsg 	return ret;
407ad8b1aafSjsg }
408ad8b1aafSjsg 
hwmgr_init_default_caps(struct pp_hwmgr * hwmgr)409ad8b1aafSjsg void hwmgr_init_default_caps(struct pp_hwmgr *hwmgr)
410ad8b1aafSjsg {
411ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_PCIEPerformanceRequest);
412ad8b1aafSjsg 
413ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_UVDDPM);
414ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_VCEDPM);
415ad8b1aafSjsg 
416ad8b1aafSjsg #if defined(CONFIG_ACPI)
417ad8b1aafSjsg 	if (amdgpu_acpi_is_pcie_performance_request_supported(hwmgr->adev))
418ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_PCIEPerformanceRequest);
419ad8b1aafSjsg #endif
420ad8b1aafSjsg 
421ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
422ad8b1aafSjsg 		PHM_PlatformCaps_DynamicPatchPowerState);
423ad8b1aafSjsg 
424ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
425ad8b1aafSjsg 		PHM_PlatformCaps_EnableSMU7ThermalManagement);
426ad8b1aafSjsg 
427ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
428ad8b1aafSjsg 			PHM_PlatformCaps_DynamicPowerManagement);
429ad8b1aafSjsg 
430ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
431ad8b1aafSjsg 					PHM_PlatformCaps_SMC);
432ad8b1aafSjsg 
433ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
434ad8b1aafSjsg 					PHM_PlatformCaps_DynamicUVDState);
435ad8b1aafSjsg 
436ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
437ad8b1aafSjsg 						PHM_PlatformCaps_FanSpeedInTableIsRPM);
438ad8b1aafSjsg 	return;
439ad8b1aafSjsg }
440ad8b1aafSjsg 
hwmgr_set_user_specify_caps(struct pp_hwmgr * hwmgr)441ad8b1aafSjsg int hwmgr_set_user_specify_caps(struct pp_hwmgr *hwmgr)
442ad8b1aafSjsg {
443ad8b1aafSjsg 	if (hwmgr->feature_mask & PP_SCLK_DEEP_SLEEP_MASK)
444ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
445ad8b1aafSjsg 			PHM_PlatformCaps_SclkDeepSleep);
446ad8b1aafSjsg 	else
447ad8b1aafSjsg 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
448ad8b1aafSjsg 			PHM_PlatformCaps_SclkDeepSleep);
449ad8b1aafSjsg 
450ad8b1aafSjsg 	if (hwmgr->feature_mask & PP_POWER_CONTAINMENT_MASK) {
451ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
452ad8b1aafSjsg 			    PHM_PlatformCaps_PowerContainment);
453ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
454ad8b1aafSjsg 			PHM_PlatformCaps_CAC);
455ad8b1aafSjsg 	} else {
456ad8b1aafSjsg 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
457ad8b1aafSjsg 			    PHM_PlatformCaps_PowerContainment);
458ad8b1aafSjsg 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
459ad8b1aafSjsg 			PHM_PlatformCaps_CAC);
460ad8b1aafSjsg 	}
461ad8b1aafSjsg 
462ad8b1aafSjsg 	if (hwmgr->feature_mask & PP_OVERDRIVE_MASK)
463ad8b1aafSjsg 		hwmgr->od_enabled = true;
464ad8b1aafSjsg 
465ad8b1aafSjsg 	return 0;
466ad8b1aafSjsg }
467ad8b1aafSjsg 
polaris_set_asic_special_caps(struct pp_hwmgr * hwmgr)468ad8b1aafSjsg int polaris_set_asic_special_caps(struct pp_hwmgr *hwmgr)
469ad8b1aafSjsg {
470ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
471ad8b1aafSjsg 						PHM_PlatformCaps_EVV);
472ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
473ad8b1aafSjsg 						PHM_PlatformCaps_SQRamping);
474ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
475ad8b1aafSjsg 						PHM_PlatformCaps_RegulatorHot);
476ad8b1aafSjsg 
477ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
478*5ca02815Sjsg 			PHM_PlatformCaps_MemorySpreadSpectrumSupport);
479*5ca02815Sjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
480*5ca02815Sjsg 			PHM_PlatformCaps_EngineSpreadSpectrumSupport);
481*5ca02815Sjsg 
482*5ca02815Sjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
483ad8b1aafSjsg 					PHM_PlatformCaps_AutomaticDCTransition);
484ad8b1aafSjsg 
485*5ca02815Sjsg 	if (((hwmgr->chip_id == CHIP_POLARIS11) && !hwmgr->is_kicker) ||
486*5ca02815Sjsg 	    (hwmgr->chip_id == CHIP_POLARIS12))
487ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
488ad8b1aafSjsg 				PHM_PlatformCaps_SPLLShutdownSupport);
489ad8b1aafSjsg 
490ad8b1aafSjsg 	if (hwmgr->chip_id != CHIP_POLARIS11) {
491ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
492ad8b1aafSjsg 							PHM_PlatformCaps_DBRamping);
493ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
494ad8b1aafSjsg 							PHM_PlatformCaps_TDRamping);
495ad8b1aafSjsg 		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
496ad8b1aafSjsg 							PHM_PlatformCaps_TCPRamping);
497ad8b1aafSjsg 	}
498ad8b1aafSjsg 	return 0;
499ad8b1aafSjsg }
500ad8b1aafSjsg 
fiji_set_asic_special_caps(struct pp_hwmgr * hwmgr)501ad8b1aafSjsg int fiji_set_asic_special_caps(struct pp_hwmgr *hwmgr)
502ad8b1aafSjsg {
503ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
504ad8b1aafSjsg 						PHM_PlatformCaps_EVV);
505ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
506ad8b1aafSjsg 			PHM_PlatformCaps_SQRamping);
507ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
508ad8b1aafSjsg 			PHM_PlatformCaps_DBRamping);
509ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
510ad8b1aafSjsg 			PHM_PlatformCaps_TDRamping);
511ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
512ad8b1aafSjsg 			PHM_PlatformCaps_TCPRamping);
513ad8b1aafSjsg 	return 0;
514ad8b1aafSjsg }
515ad8b1aafSjsg 
tonga_set_asic_special_caps(struct pp_hwmgr * hwmgr)516ad8b1aafSjsg int tonga_set_asic_special_caps(struct pp_hwmgr *hwmgr)
517ad8b1aafSjsg {
518ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
519ad8b1aafSjsg 						PHM_PlatformCaps_EVV);
520ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
521ad8b1aafSjsg 			PHM_PlatformCaps_SQRamping);
522ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
523ad8b1aafSjsg 			PHM_PlatformCaps_DBRamping);
524ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
525ad8b1aafSjsg 			PHM_PlatformCaps_TDRamping);
526ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
527ad8b1aafSjsg 			PHM_PlatformCaps_TCPRamping);
528ad8b1aafSjsg 
529ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
530ad8b1aafSjsg 		      PHM_PlatformCaps_UVDPowerGating);
531ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
532ad8b1aafSjsg 		      PHM_PlatformCaps_VCEPowerGating);
533ad8b1aafSjsg 	return 0;
534ad8b1aafSjsg }
535ad8b1aafSjsg 
topaz_set_asic_special_caps(struct pp_hwmgr * hwmgr)536ad8b1aafSjsg int topaz_set_asic_special_caps(struct pp_hwmgr *hwmgr)
537ad8b1aafSjsg {
538ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
539ad8b1aafSjsg 						PHM_PlatformCaps_EVV);
540ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
541ad8b1aafSjsg 			PHM_PlatformCaps_SQRamping);
542ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
543ad8b1aafSjsg 			PHM_PlatformCaps_DBRamping);
544ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
545ad8b1aafSjsg 			PHM_PlatformCaps_TDRamping);
546ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
547ad8b1aafSjsg 			PHM_PlatformCaps_TCPRamping);
548ad8b1aafSjsg 	return 0;
549ad8b1aafSjsg }
550ad8b1aafSjsg 
ci_set_asic_special_caps(struct pp_hwmgr * hwmgr)551ad8b1aafSjsg int ci_set_asic_special_caps(struct pp_hwmgr *hwmgr)
552ad8b1aafSjsg {
553ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
554ad8b1aafSjsg 			PHM_PlatformCaps_SQRamping);
555ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
556ad8b1aafSjsg 			PHM_PlatformCaps_DBRamping);
557ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
558ad8b1aafSjsg 			PHM_PlatformCaps_TDRamping);
559ad8b1aafSjsg 	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
560ad8b1aafSjsg 			PHM_PlatformCaps_TCPRamping);
561ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
562ad8b1aafSjsg 			PHM_PlatformCaps_MemorySpreadSpectrumSupport);
563ad8b1aafSjsg 	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
564ad8b1aafSjsg 			PHM_PlatformCaps_EngineSpreadSpectrumSupport);
565ad8b1aafSjsg 	return 0;
566ad8b1aafSjsg }
567