5824213d5b504574bacbf33c7178d3a6bbdde473
[sfrench/cifs-2.6.git] / drivers / gpu / drm / amd / powerplay / hwmgr / vega20_hwmgr.c
1 /*
2  * Copyright 2018 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
24 #include <linux/delay.h>
25 #include <linux/fb.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28
29 #include "hwmgr.h"
30 #include "amd_powerplay.h"
31 #include "vega20_smumgr.h"
32 #include "hardwaremanager.h"
33 #include "ppatomfwctrl.h"
34 #include "atomfirmware.h"
35 #include "cgs_common.h"
36 #include "vega20_powertune.h"
37 #include "vega20_inc.h"
38 #include "pppcielanes.h"
39 #include "vega20_hwmgr.h"
40 #include "vega20_processpptables.h"
41 #include "vega20_pptable.h"
42 #include "vega20_thermal.h"
43 #include "vega20_ppsmc.h"
44 #include "pp_debug.h"
45 #include "amd_pcie_helpers.h"
46 #include "ppinterrupt.h"
47 #include "pp_overdriver.h"
48 #include "pp_thermal.h"
49 #include "soc15_common.h"
50 #include "smuio/smuio_9_0_offset.h"
51 #include "smuio/smuio_9_0_sh_mask.h"
52 #include "nbio/nbio_7_4_sh_mask.h"
53
54 #define smnPCIE_LC_SPEED_CNTL                   0x11140290
55 #define smnPCIE_LC_LINK_WIDTH_CNTL              0x11140288
56
57 static void vega20_set_default_registry_data(struct pp_hwmgr *hwmgr)
58 {
59         struct vega20_hwmgr *data =
60                         (struct vega20_hwmgr *)(hwmgr->backend);
61
62         data->gfxclk_average_alpha = PPVEGA20_VEGA20GFXCLKAVERAGEALPHA_DFLT;
63         data->socclk_average_alpha = PPVEGA20_VEGA20SOCCLKAVERAGEALPHA_DFLT;
64         data->uclk_average_alpha = PPVEGA20_VEGA20UCLKCLKAVERAGEALPHA_DFLT;
65         data->gfx_activity_average_alpha = PPVEGA20_VEGA20GFXACTIVITYAVERAGEALPHA_DFLT;
66         data->lowest_uclk_reserved_for_ulv = PPVEGA20_VEGA20LOWESTUCLKRESERVEDFORULV_DFLT;
67
68         data->display_voltage_mode = PPVEGA20_VEGA20DISPLAYVOLTAGEMODE_DFLT;
69         data->dcef_clk_quad_eqn_a = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
70         data->dcef_clk_quad_eqn_b = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
71         data->dcef_clk_quad_eqn_c = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
72         data->disp_clk_quad_eqn_a = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
73         data->disp_clk_quad_eqn_b = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
74         data->disp_clk_quad_eqn_c = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
75         data->pixel_clk_quad_eqn_a = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
76         data->pixel_clk_quad_eqn_b = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
77         data->pixel_clk_quad_eqn_c = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
78         data->phy_clk_quad_eqn_a = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
79         data->phy_clk_quad_eqn_b = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
80         data->phy_clk_quad_eqn_c = PPREGKEY_VEGA20QUADRATICEQUATION_DFLT;
81
82         /*
83          * Disable the following features for now:
84          *   GFXCLK DS
85          *   SOCLK DS
86          *   LCLK DS
87          *   DCEFCLK DS
88          *   FCLK DS
89          *   MP1CLK DS
90          *   MP0CLK DS
91          */
92         data->registry_data.disallowed_features = 0xE0041C00;
93         data->registry_data.od_state_in_dc_support = 0;
94         data->registry_data.thermal_support = 1;
95         data->registry_data.skip_baco_hardware = 0;
96
97         data->registry_data.log_avfs_param = 0;
98         data->registry_data.sclk_throttle_low_notification = 1;
99         data->registry_data.force_dpm_high = 0;
100         data->registry_data.stable_pstate_sclk_dpm_percentage = 75;
101
102         data->registry_data.didt_support = 0;
103         if (data->registry_data.didt_support) {
104                 data->registry_data.didt_mode = 6;
105                 data->registry_data.sq_ramping_support = 1;
106                 data->registry_data.db_ramping_support = 0;
107                 data->registry_data.td_ramping_support = 0;
108                 data->registry_data.tcp_ramping_support = 0;
109                 data->registry_data.dbr_ramping_support = 0;
110                 data->registry_data.edc_didt_support = 1;
111                 data->registry_data.gc_didt_support = 0;
112                 data->registry_data.psm_didt_support = 0;
113         }
114
115         data->registry_data.pcie_lane_override = 0xff;
116         data->registry_data.pcie_speed_override = 0xff;
117         data->registry_data.pcie_clock_override = 0xffffffff;
118         data->registry_data.regulator_hot_gpio_support = 1;
119         data->registry_data.ac_dc_switch_gpio_support = 0;
120         data->registry_data.quick_transition_support = 0;
121         data->registry_data.zrpm_start_temp = 0xffff;
122         data->registry_data.zrpm_stop_temp = 0xffff;
123         data->registry_data.od8_feature_enable = 1;
124         data->registry_data.disable_water_mark = 0;
125         data->registry_data.disable_pp_tuning = 0;
126         data->registry_data.disable_xlpp_tuning = 0;
127         data->registry_data.disable_workload_policy = 0;
128         data->registry_data.perf_ui_tuning_profile_turbo = 0x19190F0F;
129         data->registry_data.perf_ui_tuning_profile_powerSave = 0x19191919;
130         data->registry_data.perf_ui_tuning_profile_xl = 0x00000F0A;
131         data->registry_data.force_workload_policy_mask = 0;
132         data->registry_data.disable_3d_fs_detection = 0;
133         data->registry_data.fps_support = 1;
134         data->registry_data.disable_auto_wattman = 1;
135         data->registry_data.auto_wattman_debug = 0;
136         data->registry_data.auto_wattman_sample_period = 100;
137         data->registry_data.fclk_gfxclk_ratio = 0x3F6CCCCD;
138         data->registry_data.auto_wattman_threshold = 50;
139         data->registry_data.gfxoff_controlled_by_driver = 1;
140         data->gfxoff_allowed = false;
141         data->counter_gfxoff = 0;
142 }
143
144 static int vega20_set_features_platform_caps(struct pp_hwmgr *hwmgr)
145 {
146         struct vega20_hwmgr *data =
147                         (struct vega20_hwmgr *)(hwmgr->backend);
148         struct amdgpu_device *adev = hwmgr->adev;
149
150         if (data->vddci_control == VEGA20_VOLTAGE_CONTROL_NONE)
151                 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
152                                 PHM_PlatformCaps_ControlVDDCI);
153
154         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
155                         PHM_PlatformCaps_TablelessHardwareInterface);
156
157         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
158                         PHM_PlatformCaps_EnableSMU7ThermalManagement);
159
160         if (adev->pg_flags & AMD_PG_SUPPORT_UVD)
161                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
162                                 PHM_PlatformCaps_UVDPowerGating);
163
164         if (adev->pg_flags & AMD_PG_SUPPORT_VCE)
165                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
166                                 PHM_PlatformCaps_VCEPowerGating);
167
168         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
169                         PHM_PlatformCaps_UnTabledHardwareInterface);
170
171         if (data->registry_data.od8_feature_enable)
172                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
173                                 PHM_PlatformCaps_OD8inACSupport);
174
175         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
176                         PHM_PlatformCaps_ActivityReporting);
177         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
178                         PHM_PlatformCaps_FanSpeedInTableIsRPM);
179
180         if (data->registry_data.od_state_in_dc_support) {
181                 if (data->registry_data.od8_feature_enable)
182                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
183                                         PHM_PlatformCaps_OD8inDCSupport);
184         }
185
186         if (data->registry_data.thermal_support &&
187             data->registry_data.fuzzy_fan_control_support &&
188             hwmgr->thermal_controller.advanceFanControlParameters.usTMax)
189                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
190                                 PHM_PlatformCaps_ODFuzzyFanControlSupport);
191
192         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
193                         PHM_PlatformCaps_DynamicPowerManagement);
194         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
195                         PHM_PlatformCaps_SMC);
196         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
197                         PHM_PlatformCaps_ThermalPolicyDelay);
198
199         if (data->registry_data.force_dpm_high)
200                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
201                                 PHM_PlatformCaps_ExclusiveModeAlwaysHigh);
202
203         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
204                         PHM_PlatformCaps_DynamicUVDState);
205
206         if (data->registry_data.sclk_throttle_low_notification)
207                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
208                                 PHM_PlatformCaps_SclkThrottleLowNotification);
209
210         /* power tune caps */
211         /* assume disabled */
212         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
213                         PHM_PlatformCaps_PowerContainment);
214         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
215                         PHM_PlatformCaps_DiDtSupport);
216         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
217                         PHM_PlatformCaps_SQRamping);
218         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
219                         PHM_PlatformCaps_DBRamping);
220         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
221                         PHM_PlatformCaps_TDRamping);
222         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
223                         PHM_PlatformCaps_TCPRamping);
224         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
225                         PHM_PlatformCaps_DBRRamping);
226         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
227                         PHM_PlatformCaps_DiDtEDCEnable);
228         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
229                         PHM_PlatformCaps_GCEDC);
230         phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
231                         PHM_PlatformCaps_PSM);
232
233         if (data->registry_data.didt_support) {
234                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
235                                 PHM_PlatformCaps_DiDtSupport);
236                 if (data->registry_data.sq_ramping_support)
237                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
238                                         PHM_PlatformCaps_SQRamping);
239                 if (data->registry_data.db_ramping_support)
240                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
241                                         PHM_PlatformCaps_DBRamping);
242                 if (data->registry_data.td_ramping_support)
243                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
244                                         PHM_PlatformCaps_TDRamping);
245                 if (data->registry_data.tcp_ramping_support)
246                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
247                                         PHM_PlatformCaps_TCPRamping);
248                 if (data->registry_data.dbr_ramping_support)
249                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
250                                         PHM_PlatformCaps_DBRRamping);
251                 if (data->registry_data.edc_didt_support)
252                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
253                                         PHM_PlatformCaps_DiDtEDCEnable);
254                 if (data->registry_data.gc_didt_support)
255                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
256                                         PHM_PlatformCaps_GCEDC);
257                 if (data->registry_data.psm_didt_support)
258                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
259                                         PHM_PlatformCaps_PSM);
260         }
261
262         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
263                         PHM_PlatformCaps_RegulatorHot);
264
265         if (data->registry_data.ac_dc_switch_gpio_support) {
266                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
267                                 PHM_PlatformCaps_AutomaticDCTransition);
268                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
269                                 PHM_PlatformCaps_SMCtoPPLIBAcdcGpioScheme);
270         }
271
272         if (data->registry_data.quick_transition_support) {
273                 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
274                                 PHM_PlatformCaps_AutomaticDCTransition);
275                 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
276                                 PHM_PlatformCaps_SMCtoPPLIBAcdcGpioScheme);
277                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
278                                 PHM_PlatformCaps_Falcon_QuickTransition);
279         }
280
281         if (data->lowest_uclk_reserved_for_ulv != PPVEGA20_VEGA20LOWESTUCLKRESERVEDFORULV_DFLT) {
282                 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
283                                 PHM_PlatformCaps_LowestUclkReservedForUlv);
284                 if (data->lowest_uclk_reserved_for_ulv == 1)
285                         phm_cap_set(hwmgr->platform_descriptor.platformCaps,
286                                         PHM_PlatformCaps_LowestUclkReservedForUlv);
287         }
288
289         if (data->registry_data.custom_fan_support)
290                 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
291                                 PHM_PlatformCaps_CustomFanControlSupport);
292
293         return 0;
294 }
295
296 static void vega20_init_dpm_defaults(struct pp_hwmgr *hwmgr)
297 {
298         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
299         int i;
300
301         data->smu_features[GNLD_DPM_PREFETCHER].smu_feature_id =
302                         FEATURE_DPM_PREFETCHER_BIT;
303         data->smu_features[GNLD_DPM_GFXCLK].smu_feature_id =
304                         FEATURE_DPM_GFXCLK_BIT;
305         data->smu_features[GNLD_DPM_UCLK].smu_feature_id =
306                         FEATURE_DPM_UCLK_BIT;
307         data->smu_features[GNLD_DPM_SOCCLK].smu_feature_id =
308                         FEATURE_DPM_SOCCLK_BIT;
309         data->smu_features[GNLD_DPM_UVD].smu_feature_id =
310                         FEATURE_DPM_UVD_BIT;
311         data->smu_features[GNLD_DPM_VCE].smu_feature_id =
312                         FEATURE_DPM_VCE_BIT;
313         data->smu_features[GNLD_ULV].smu_feature_id =
314                         FEATURE_ULV_BIT;
315         data->smu_features[GNLD_DPM_MP0CLK].smu_feature_id =
316                         FEATURE_DPM_MP0CLK_BIT;
317         data->smu_features[GNLD_DPM_LINK].smu_feature_id =
318                         FEATURE_DPM_LINK_BIT;
319         data->smu_features[GNLD_DPM_DCEFCLK].smu_feature_id =
320                         FEATURE_DPM_DCEFCLK_BIT;
321         data->smu_features[GNLD_DS_GFXCLK].smu_feature_id =
322                         FEATURE_DS_GFXCLK_BIT;
323         data->smu_features[GNLD_DS_SOCCLK].smu_feature_id =
324                         FEATURE_DS_SOCCLK_BIT;
325         data->smu_features[GNLD_DS_LCLK].smu_feature_id =
326                         FEATURE_DS_LCLK_BIT;
327         data->smu_features[GNLD_PPT].smu_feature_id =
328                         FEATURE_PPT_BIT;
329         data->smu_features[GNLD_TDC].smu_feature_id =
330                         FEATURE_TDC_BIT;
331         data->smu_features[GNLD_THERMAL].smu_feature_id =
332                         FEATURE_THERMAL_BIT;
333         data->smu_features[GNLD_GFX_PER_CU_CG].smu_feature_id =
334                         FEATURE_GFX_PER_CU_CG_BIT;
335         data->smu_features[GNLD_RM].smu_feature_id =
336                         FEATURE_RM_BIT;
337         data->smu_features[GNLD_DS_DCEFCLK].smu_feature_id =
338                         FEATURE_DS_DCEFCLK_BIT;
339         data->smu_features[GNLD_ACDC].smu_feature_id =
340                         FEATURE_ACDC_BIT;
341         data->smu_features[GNLD_VR0HOT].smu_feature_id =
342                         FEATURE_VR0HOT_BIT;
343         data->smu_features[GNLD_VR1HOT].smu_feature_id =
344                         FEATURE_VR1HOT_BIT;
345         data->smu_features[GNLD_FW_CTF].smu_feature_id =
346                         FEATURE_FW_CTF_BIT;
347         data->smu_features[GNLD_LED_DISPLAY].smu_feature_id =
348                         FEATURE_LED_DISPLAY_BIT;
349         data->smu_features[GNLD_FAN_CONTROL].smu_feature_id =
350                         FEATURE_FAN_CONTROL_BIT;
351         data->smu_features[GNLD_DIDT].smu_feature_id = FEATURE_GFX_EDC_BIT;
352         data->smu_features[GNLD_GFXOFF].smu_feature_id = FEATURE_GFXOFF_BIT;
353         data->smu_features[GNLD_CG].smu_feature_id = FEATURE_CG_BIT;
354         data->smu_features[GNLD_DPM_FCLK].smu_feature_id = FEATURE_DPM_FCLK_BIT;
355         data->smu_features[GNLD_DS_FCLK].smu_feature_id = FEATURE_DS_FCLK_BIT;
356         data->smu_features[GNLD_DS_MP1CLK].smu_feature_id = FEATURE_DS_MP1CLK_BIT;
357         data->smu_features[GNLD_DS_MP0CLK].smu_feature_id = FEATURE_DS_MP0CLK_BIT;
358         data->smu_features[GNLD_XGMI].smu_feature_id = FEATURE_XGMI_BIT;
359
360         for (i = 0; i < GNLD_FEATURES_MAX; i++) {
361                 data->smu_features[i].smu_feature_bitmap =
362                         (uint64_t)(1ULL << data->smu_features[i].smu_feature_id);
363                 data->smu_features[i].allowed =
364                         ((data->registry_data.disallowed_features >> i) & 1) ?
365                         false : true;
366         }
367 }
368
369 static int vega20_set_private_data_based_on_pptable(struct pp_hwmgr *hwmgr)
370 {
371         return 0;
372 }
373
374 static int vega20_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
375 {
376         kfree(hwmgr->backend);
377         hwmgr->backend = NULL;
378
379         return 0;
380 }
381
382 static int vega20_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
383 {
384         struct vega20_hwmgr *data;
385         struct amdgpu_device *adev = hwmgr->adev;
386
387         data = kzalloc(sizeof(struct vega20_hwmgr), GFP_KERNEL);
388         if (data == NULL)
389                 return -ENOMEM;
390
391         hwmgr->backend = data;
392
393         hwmgr->workload_mask = 1 << hwmgr->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
394         hwmgr->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
395         hwmgr->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
396
397         vega20_set_default_registry_data(hwmgr);
398
399         data->disable_dpm_mask = 0xff;
400
401         /* need to set voltage control types before EVV patching */
402         data->vddc_control = VEGA20_VOLTAGE_CONTROL_NONE;
403         data->mvdd_control = VEGA20_VOLTAGE_CONTROL_NONE;
404         data->vddci_control = VEGA20_VOLTAGE_CONTROL_NONE;
405
406         data->water_marks_bitmap = 0;
407         data->avfs_exist = false;
408
409         vega20_set_features_platform_caps(hwmgr);
410
411         vega20_init_dpm_defaults(hwmgr);
412
413         /* Parse pptable data read from VBIOS */
414         vega20_set_private_data_based_on_pptable(hwmgr);
415
416         data->is_tlu_enabled = false;
417
418         hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =
419                         VEGA20_MAX_HARDWARE_POWERLEVELS;
420         hwmgr->platform_descriptor.hardwarePerformanceLevels = 2;
421         hwmgr->platform_descriptor.minimumClocksReductionPercentage = 50;
422
423         hwmgr->platform_descriptor.vbiosInterruptId = 0x20000400; /* IRQ_SOURCE1_SW_INT */
424         /* The true clock step depends on the frequency, typically 4.5 or 9 MHz. Here we use 5. */
425         hwmgr->platform_descriptor.clockStep.engineClock = 500;
426         hwmgr->platform_descriptor.clockStep.memoryClock = 500;
427
428         data->total_active_cus = adev->gfx.cu_info.number;
429
430         return 0;
431 }
432
433 static int vega20_init_sclk_threshold(struct pp_hwmgr *hwmgr)
434 {
435         struct vega20_hwmgr *data =
436                         (struct vega20_hwmgr *)(hwmgr->backend);
437
438         data->low_sclk_interrupt_threshold = 0;
439
440         return 0;
441 }
442
443 static int vega20_setup_asic_task(struct pp_hwmgr *hwmgr)
444 {
445         int ret = 0;
446
447         ret = vega20_init_sclk_threshold(hwmgr);
448         PP_ASSERT_WITH_CODE(!ret,
449                         "Failed to init sclk threshold!",
450                         return ret);
451
452         return 0;
453 }
454
455 /*
456  * @fn vega20_init_dpm_state
457  * @brief Function to initialize all Soft Min/Max and Hard Min/Max to 0xff.
458  *
459  * @param    dpm_state - the address of the DPM Table to initiailize.
460  * @return   None.
461  */
462 static void vega20_init_dpm_state(struct vega20_dpm_state *dpm_state)
463 {
464         dpm_state->soft_min_level = 0x0;
465         dpm_state->soft_max_level = 0xffff;
466         dpm_state->hard_min_level = 0x0;
467         dpm_state->hard_max_level = 0xffff;
468 }
469
470 static int vega20_get_number_of_dpm_level(struct pp_hwmgr *hwmgr,
471                 PPCLK_e clk_id, uint32_t *num_of_levels)
472 {
473         int ret = 0;
474
475         ret = smum_send_msg_to_smc_with_parameter(hwmgr,
476                         PPSMC_MSG_GetDpmFreqByIndex,
477                         (clk_id << 16 | 0xFF));
478         PP_ASSERT_WITH_CODE(!ret,
479                         "[GetNumOfDpmLevel] failed to get dpm levels!",
480                         return ret);
481
482         *num_of_levels = smum_get_argument(hwmgr);
483         PP_ASSERT_WITH_CODE(*num_of_levels > 0,
484                         "[GetNumOfDpmLevel] number of clk levels is invalid!",
485                         return -EINVAL);
486
487         return ret;
488 }
489
490 static int vega20_get_dpm_frequency_by_index(struct pp_hwmgr *hwmgr,
491                 PPCLK_e clk_id, uint32_t index, uint32_t *clk)
492 {
493         int ret = 0;
494
495         ret = smum_send_msg_to_smc_with_parameter(hwmgr,
496                         PPSMC_MSG_GetDpmFreqByIndex,
497                         (clk_id << 16 | index));
498         PP_ASSERT_WITH_CODE(!ret,
499                         "[GetDpmFreqByIndex] failed to get dpm freq by index!",
500                         return ret);
501
502         *clk = smum_get_argument(hwmgr);
503         PP_ASSERT_WITH_CODE(*clk,
504                         "[GetDpmFreqByIndex] clk value is invalid!",
505                         return -EINVAL);
506
507         return ret;
508 }
509
510 static int vega20_setup_single_dpm_table(struct pp_hwmgr *hwmgr,
511                 struct vega20_single_dpm_table *dpm_table, PPCLK_e clk_id)
512 {
513         int ret = 0;
514         uint32_t i, num_of_levels, clk;
515
516         ret = vega20_get_number_of_dpm_level(hwmgr, clk_id, &num_of_levels);
517         PP_ASSERT_WITH_CODE(!ret,
518                         "[SetupSingleDpmTable] failed to get clk levels!",
519                         return ret);
520
521         dpm_table->count = num_of_levels;
522
523         for (i = 0; i < num_of_levels; i++) {
524                 ret = vega20_get_dpm_frequency_by_index(hwmgr, clk_id, i, &clk);
525                 PP_ASSERT_WITH_CODE(!ret,
526                         "[SetupSingleDpmTable] failed to get clk of specific level!",
527                         return ret);
528                 dpm_table->dpm_levels[i].value = clk;
529                 dpm_table->dpm_levels[i].enabled = true;
530         }
531
532         return ret;
533 }
534
535 static int vega20_setup_gfxclk_dpm_table(struct pp_hwmgr *hwmgr)
536 {
537         struct vega20_hwmgr *data =
538                         (struct vega20_hwmgr *)(hwmgr->backend);
539         struct vega20_single_dpm_table *dpm_table;
540         int ret = 0;
541
542         dpm_table = &(data->dpm_table.gfx_table);
543         if (data->smu_features[GNLD_DPM_GFXCLK].enabled) {
544                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_GFXCLK);
545                 PP_ASSERT_WITH_CODE(!ret,
546                                 "[SetupDefaultDpmTable] failed to get gfxclk dpm levels!",
547                                 return ret);
548         } else {
549                 dpm_table->count = 1;
550                 dpm_table->dpm_levels[0].value = data->vbios_boot_state.gfx_clock / 100;
551         }
552
553         return ret;
554 }
555
556 static int vega20_setup_memclk_dpm_table(struct pp_hwmgr *hwmgr)
557 {
558         struct vega20_hwmgr *data =
559                         (struct vega20_hwmgr *)(hwmgr->backend);
560         struct vega20_single_dpm_table *dpm_table;
561         int ret = 0;
562
563         dpm_table = &(data->dpm_table.mem_table);
564         if (data->smu_features[GNLD_DPM_UCLK].enabled) {
565                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_UCLK);
566                 PP_ASSERT_WITH_CODE(!ret,
567                                 "[SetupDefaultDpmTable] failed to get memclk dpm levels!",
568                                 return ret);
569         } else {
570                 dpm_table->count = 1;
571                 dpm_table->dpm_levels[0].value = data->vbios_boot_state.mem_clock / 100;
572         }
573
574         return ret;
575 }
576
577 /*
578  * This function is to initialize all DPM state tables
579  * for SMU based on the dependency table.
580  * Dynamic state patching function will then trim these
581  * state tables to the allowed range based
582  * on the power policy or external client requests,
583  * such as UVD request, etc.
584  */
585 static int vega20_setup_default_dpm_tables(struct pp_hwmgr *hwmgr)
586 {
587         struct vega20_hwmgr *data =
588                         (struct vega20_hwmgr *)(hwmgr->backend);
589         struct vega20_single_dpm_table *dpm_table;
590         int ret = 0;
591
592         memset(&data->dpm_table, 0, sizeof(data->dpm_table));
593
594         /* socclk */
595         dpm_table = &(data->dpm_table.soc_table);
596         if (data->smu_features[GNLD_DPM_SOCCLK].enabled) {
597                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_SOCCLK);
598                 PP_ASSERT_WITH_CODE(!ret,
599                                 "[SetupDefaultDpmTable] failed to get socclk dpm levels!",
600                                 return ret);
601         } else {
602                 dpm_table->count = 1;
603                 dpm_table->dpm_levels[0].value = data->vbios_boot_state.soc_clock / 100;
604         }
605         vega20_init_dpm_state(&(dpm_table->dpm_state));
606
607         /* gfxclk */
608         dpm_table = &(data->dpm_table.gfx_table);
609         ret = vega20_setup_gfxclk_dpm_table(hwmgr);
610         if (ret)
611                 return ret;
612         vega20_init_dpm_state(&(dpm_table->dpm_state));
613
614         /* memclk */
615         dpm_table = &(data->dpm_table.mem_table);
616         ret = vega20_setup_memclk_dpm_table(hwmgr);
617         if (ret)
618                 return ret;
619         vega20_init_dpm_state(&(dpm_table->dpm_state));
620
621         /* eclk */
622         dpm_table = &(data->dpm_table.eclk_table);
623         if (data->smu_features[GNLD_DPM_VCE].enabled) {
624                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_ECLK);
625                 PP_ASSERT_WITH_CODE(!ret,
626                                 "[SetupDefaultDpmTable] failed to get eclk dpm levels!",
627                                 return ret);
628         } else {
629                 dpm_table->count = 1;
630                 dpm_table->dpm_levels[0].value = data->vbios_boot_state.eclock / 100;
631         }
632         vega20_init_dpm_state(&(dpm_table->dpm_state));
633
634         /* vclk */
635         dpm_table = &(data->dpm_table.vclk_table);
636         if (data->smu_features[GNLD_DPM_UVD].enabled) {
637                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_VCLK);
638                 PP_ASSERT_WITH_CODE(!ret,
639                                 "[SetupDefaultDpmTable] failed to get vclk dpm levels!",
640                                 return ret);
641         } else {
642                 dpm_table->count = 1;
643                 dpm_table->dpm_levels[0].value = data->vbios_boot_state.vclock / 100;
644         }
645         vega20_init_dpm_state(&(dpm_table->dpm_state));
646
647         /* dclk */
648         dpm_table = &(data->dpm_table.dclk_table);
649         if (data->smu_features[GNLD_DPM_UVD].enabled) {
650                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_DCLK);
651                 PP_ASSERT_WITH_CODE(!ret,
652                                 "[SetupDefaultDpmTable] failed to get dclk dpm levels!",
653                                 return ret);
654         } else {
655                 dpm_table->count = 1;
656                 dpm_table->dpm_levels[0].value = data->vbios_boot_state.dclock / 100;
657         }
658         vega20_init_dpm_state(&(dpm_table->dpm_state));
659
660         /* dcefclk */
661         dpm_table = &(data->dpm_table.dcef_table);
662         if (data->smu_features[GNLD_DPM_DCEFCLK].enabled) {
663                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_DCEFCLK);
664                 PP_ASSERT_WITH_CODE(!ret,
665                                 "[SetupDefaultDpmTable] failed to get dcefclk dpm levels!",
666                                 return ret);
667         } else {
668                 dpm_table->count = 1;
669                 dpm_table->dpm_levels[0].value = data->vbios_boot_state.dcef_clock / 100;
670         }
671         vega20_init_dpm_state(&(dpm_table->dpm_state));
672
673         /* pixclk */
674         dpm_table = &(data->dpm_table.pixel_table);
675         if (data->smu_features[GNLD_DPM_DCEFCLK].enabled) {
676                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_PIXCLK);
677                 PP_ASSERT_WITH_CODE(!ret,
678                                 "[SetupDefaultDpmTable] failed to get pixclk dpm levels!",
679                                 return ret);
680         } else
681                 dpm_table->count = 0;
682         vega20_init_dpm_state(&(dpm_table->dpm_state));
683
684         /* dispclk */
685         dpm_table = &(data->dpm_table.display_table);
686         if (data->smu_features[GNLD_DPM_DCEFCLK].enabled) {
687                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_DISPCLK);
688                 PP_ASSERT_WITH_CODE(!ret,
689                                 "[SetupDefaultDpmTable] failed to get dispclk dpm levels!",
690                                 return ret);
691         } else
692                 dpm_table->count = 0;
693         vega20_init_dpm_state(&(dpm_table->dpm_state));
694
695         /* phyclk */
696         dpm_table = &(data->dpm_table.phy_table);
697         if (data->smu_features[GNLD_DPM_DCEFCLK].enabled) {
698                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_PHYCLK);
699                 PP_ASSERT_WITH_CODE(!ret,
700                                 "[SetupDefaultDpmTable] failed to get phyclk dpm levels!",
701                                 return ret);
702         } else
703                 dpm_table->count = 0;
704         vega20_init_dpm_state(&(dpm_table->dpm_state));
705
706         /* fclk */
707         dpm_table = &(data->dpm_table.fclk_table);
708         if (data->smu_features[GNLD_DPM_FCLK].enabled) {
709                 ret = vega20_setup_single_dpm_table(hwmgr, dpm_table, PPCLK_FCLK);
710                 PP_ASSERT_WITH_CODE(!ret,
711                                 "[SetupDefaultDpmTable] failed to get fclk dpm levels!",
712                                 return ret);
713         } else
714                 dpm_table->count = 0;
715         vega20_init_dpm_state(&(dpm_table->dpm_state));
716
717         /* save a copy of the default DPM table */
718         memcpy(&(data->golden_dpm_table), &(data->dpm_table),
719                         sizeof(struct vega20_dpm_table));
720
721         return 0;
722 }
723
724 /**
725 * Initializes the SMC table and uploads it
726 *
727 * @param    hwmgr  the address of the powerplay hardware manager.
728 * @param    pInput  the pointer to input data (PowerState)
729 * @return   always 0
730 */
731 static int vega20_init_smc_table(struct pp_hwmgr *hwmgr)
732 {
733         int result;
734         struct vega20_hwmgr *data =
735                         (struct vega20_hwmgr *)(hwmgr->backend);
736         PPTable_t *pp_table = &(data->smc_state_table.pp_table);
737         struct pp_atomfwctrl_bios_boot_up_values boot_up_values;
738         struct phm_ppt_v3_information *pptable_information =
739                 (struct phm_ppt_v3_information *)hwmgr->pptable;
740
741         result = pp_atomfwctrl_get_vbios_bootup_values(hwmgr, &boot_up_values);
742         PP_ASSERT_WITH_CODE(!result,
743                         "[InitSMCTable] Failed to get vbios bootup values!",
744                         return result);
745
746         data->vbios_boot_state.vddc     = boot_up_values.usVddc;
747         data->vbios_boot_state.vddci    = boot_up_values.usVddci;
748         data->vbios_boot_state.mvddc    = boot_up_values.usMvddc;
749         data->vbios_boot_state.gfx_clock = boot_up_values.ulGfxClk;
750         data->vbios_boot_state.mem_clock = boot_up_values.ulUClk;
751         data->vbios_boot_state.soc_clock = boot_up_values.ulSocClk;
752         data->vbios_boot_state.dcef_clock = boot_up_values.ulDCEFClk;
753         data->vbios_boot_state.eclock = boot_up_values.ulEClk;
754         data->vbios_boot_state.vclock = boot_up_values.ulVClk;
755         data->vbios_boot_state.dclock = boot_up_values.ulDClk;
756         data->vbios_boot_state.uc_cooling_id = boot_up_values.ucCoolingID;
757
758         smum_send_msg_to_smc_with_parameter(hwmgr,
759                         PPSMC_MSG_SetMinDeepSleepDcefclk,
760                 (uint32_t)(data->vbios_boot_state.dcef_clock / 100));
761
762         memcpy(pp_table, pptable_information->smc_pptable, sizeof(PPTable_t));
763
764         result = smum_smc_table_manager(hwmgr,
765                                         (uint8_t *)pp_table, TABLE_PPTABLE, false);
766         PP_ASSERT_WITH_CODE(!result,
767                         "[InitSMCTable] Failed to upload PPtable!",
768                         return result);
769
770         return 0;
771 }
772
773 static int vega20_set_allowed_featuresmask(struct pp_hwmgr *hwmgr)
774 {
775         struct vega20_hwmgr *data =
776                         (struct vega20_hwmgr *)(hwmgr->backend);
777         uint32_t allowed_features_low = 0, allowed_features_high = 0;
778         int i;
779         int ret = 0;
780
781         for (i = 0; i < GNLD_FEATURES_MAX; i++)
782                 if (data->smu_features[i].allowed)
783                         data->smu_features[i].smu_feature_id > 31 ?
784                                 (allowed_features_high |=
785                                  ((data->smu_features[i].smu_feature_bitmap >> SMU_FEATURES_HIGH_SHIFT)
786                                   & 0xFFFFFFFF)) :
787                                 (allowed_features_low |=
788                                  ((data->smu_features[i].smu_feature_bitmap >> SMU_FEATURES_LOW_SHIFT)
789                                   & 0xFFFFFFFF));
790
791         ret = smum_send_msg_to_smc_with_parameter(hwmgr,
792                 PPSMC_MSG_SetAllowedFeaturesMaskHigh, allowed_features_high);
793         PP_ASSERT_WITH_CODE(!ret,
794                 "[SetAllowedFeaturesMask] Attempt to set allowed features mask(high) failed!",
795                 return ret);
796
797         ret = smum_send_msg_to_smc_with_parameter(hwmgr,
798                 PPSMC_MSG_SetAllowedFeaturesMaskLow, allowed_features_low);
799         PP_ASSERT_WITH_CODE(!ret,
800                 "[SetAllowedFeaturesMask] Attempt to set allowed features mask (low) failed!",
801                 return ret);
802
803         return 0;
804 }
805
806 static int vega20_run_btc_afll(struct pp_hwmgr *hwmgr)
807 {
808         return smum_send_msg_to_smc(hwmgr, PPSMC_MSG_RunAfllBtc);
809 }
810
811 static int vega20_enable_all_smu_features(struct pp_hwmgr *hwmgr)
812 {
813         struct vega20_hwmgr *data =
814                         (struct vega20_hwmgr *)(hwmgr->backend);
815         uint64_t features_enabled;
816         int i;
817         bool enabled;
818         int ret = 0;
819
820         PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc(hwmgr,
821                         PPSMC_MSG_EnableAllSmuFeatures)) == 0,
822                         "[EnableAllSMUFeatures] Failed to enable all smu features!",
823                         return ret);
824
825         ret = vega20_get_enabled_smc_features(hwmgr, &features_enabled);
826         PP_ASSERT_WITH_CODE(!ret,
827                         "[EnableAllSmuFeatures] Failed to get enabled smc features!",
828                         return ret);
829
830         for (i = 0; i < GNLD_FEATURES_MAX; i++) {
831                 enabled = (features_enabled & data->smu_features[i].smu_feature_bitmap) ?
832                         true : false;
833                 data->smu_features[i].enabled = enabled;
834                 data->smu_features[i].supported = enabled;
835
836 #if 0
837                 if (data->smu_features[i].allowed && !enabled)
838                         pr_info("[EnableAllSMUFeatures] feature %d is expected enabled!", i);
839                 else if (!data->smu_features[i].allowed && enabled)
840                         pr_info("[EnableAllSMUFeatures] feature %d is expected disabled!", i);
841 #endif
842         }
843
844         return 0;
845 }
846
847 static int vega20_notify_smc_display_change(struct pp_hwmgr *hwmgr)
848 {
849         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
850
851         if (data->smu_features[GNLD_DPM_UCLK].enabled)
852                 return smum_send_msg_to_smc_with_parameter(hwmgr,
853                         PPSMC_MSG_SetUclkFastSwitch,
854                         1);
855
856         return 0;
857 }
858
859 static int vega20_send_clock_ratio(struct pp_hwmgr *hwmgr)
860 {
861         struct vega20_hwmgr *data =
862                         (struct vega20_hwmgr *)(hwmgr->backend);
863
864         return smum_send_msg_to_smc_with_parameter(hwmgr,
865                         PPSMC_MSG_SetFclkGfxClkRatio,
866                         data->registry_data.fclk_gfxclk_ratio);
867 }
868
869 static int vega20_disable_all_smu_features(struct pp_hwmgr *hwmgr)
870 {
871         struct vega20_hwmgr *data =
872                         (struct vega20_hwmgr *)(hwmgr->backend);
873         uint64_t features_enabled;
874         int i;
875         bool enabled;
876         int ret = 0;
877
878         PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc(hwmgr,
879                         PPSMC_MSG_DisableAllSmuFeatures)) == 0,
880                         "[DisableAllSMUFeatures] Failed to disable all smu features!",
881                         return ret);
882
883         ret = vega20_get_enabled_smc_features(hwmgr, &features_enabled);
884         PP_ASSERT_WITH_CODE(!ret,
885                         "[DisableAllSMUFeatures] Failed to get enabled smc features!",
886                         return ret);
887
888         for (i = 0; i < GNLD_FEATURES_MAX; i++) {
889                 enabled = (features_enabled & data->smu_features[i].smu_feature_bitmap) ?
890                         true : false;
891                 data->smu_features[i].enabled = enabled;
892                 data->smu_features[i].supported = enabled;
893         }
894
895         return 0;
896 }
897
898 static int vega20_od8_set_feature_capabilities(
899                 struct pp_hwmgr *hwmgr)
900 {
901         struct phm_ppt_v3_information *pptable_information =
902                 (struct phm_ppt_v3_information *)hwmgr->pptable;
903         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
904         PPTable_t *pp_table = &(data->smc_state_table.pp_table);
905         struct vega20_od8_settings *od_settings = &(data->od8_settings);
906
907         od_settings->overdrive8_capabilities = 0;
908
909         if (data->smu_features[GNLD_DPM_GFXCLK].enabled) {
910                 if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_GFXCLK_LIMITS] &&
911                     pptable_information->od_settings_max[OD8_SETTING_GFXCLK_FMAX] > 0 &&
912                     pptable_information->od_settings_min[OD8_SETTING_GFXCLK_FMIN] > 0 &&
913                     (pptable_information->od_settings_max[OD8_SETTING_GFXCLK_FMAX] >=
914                     pptable_information->od_settings_min[OD8_SETTING_GFXCLK_FMIN]))
915                         od_settings->overdrive8_capabilities |= OD8_GFXCLK_LIMITS;
916
917                 if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_GFXCLK_CURVE] &&
918                     (pptable_information->od_settings_min[OD8_SETTING_GFXCLK_VOLTAGE1] >=
919                      pp_table->MinVoltageGfx / VOLTAGE_SCALE) &&
920                     (pptable_information->od_settings_max[OD8_SETTING_GFXCLK_VOLTAGE3] <=
921                      pp_table->MaxVoltageGfx / VOLTAGE_SCALE) &&
922                     (pptable_information->od_settings_max[OD8_SETTING_GFXCLK_VOLTAGE3] >=
923                      pptable_information->od_settings_min[OD8_SETTING_GFXCLK_VOLTAGE1]))
924                         od_settings->overdrive8_capabilities |= OD8_GFXCLK_CURVE;
925         }
926
927         if (data->smu_features[GNLD_DPM_UCLK].enabled) {
928                 if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_UCLK_MAX] &&
929                     pptable_information->od_settings_min[OD8_SETTING_UCLK_FMAX] > 0 &&
930                     pptable_information->od_settings_max[OD8_SETTING_UCLK_FMAX] > 0 &&
931                     (pptable_information->od_settings_max[OD8_SETTING_UCLK_FMAX] >=
932                     pptable_information->od_settings_min[OD8_SETTING_UCLK_FMAX]))
933                         od_settings->overdrive8_capabilities |= OD8_UCLK_MAX;
934         }
935
936         if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_POWER_LIMIT] &&
937             pptable_information->od_settings_max[OD8_SETTING_POWER_PERCENTAGE] > 0 &&
938             pptable_information->od_settings_max[OD8_SETTING_POWER_PERCENTAGE] <= 100 &&
939             pptable_information->od_settings_min[OD8_SETTING_POWER_PERCENTAGE] > 0 &&
940             pptable_information->od_settings_min[OD8_SETTING_POWER_PERCENTAGE] <= 100)
941                 od_settings->overdrive8_capabilities |= OD8_POWER_LIMIT;
942
943         if (data->smu_features[GNLD_FAN_CONTROL].enabled) {
944                 if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_FAN_ACOUSTIC_LIMIT] &&
945                     pptable_information->od_settings_min[OD8_SETTING_FAN_ACOUSTIC_LIMIT] > 0 &&
946                     pptable_information->od_settings_max[OD8_SETTING_FAN_ACOUSTIC_LIMIT] > 0 &&
947                     (pptable_information->od_settings_max[OD8_SETTING_FAN_ACOUSTIC_LIMIT] >=
948                      pptable_information->od_settings_min[OD8_SETTING_FAN_ACOUSTIC_LIMIT]))
949                         od_settings->overdrive8_capabilities |= OD8_ACOUSTIC_LIMIT_SCLK;
950
951                 if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_FAN_SPEED_MIN] &&
952                     (pptable_information->od_settings_min[OD8_SETTING_FAN_MIN_SPEED] >=
953                     (pp_table->FanPwmMin * pp_table->FanMaximumRpm / 100)) &&
954                     pptable_information->od_settings_max[OD8_SETTING_FAN_MIN_SPEED] > 0 &&
955                     (pptable_information->od_settings_max[OD8_SETTING_FAN_MIN_SPEED] >=
956                      pptable_information->od_settings_min[OD8_SETTING_FAN_MIN_SPEED]))
957                         od_settings->overdrive8_capabilities |= OD8_FAN_SPEED_MIN;
958         }
959
960         if (data->smu_features[GNLD_THERMAL].enabled) {
961                 if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_TEMPERATURE_FAN] &&
962                     pptable_information->od_settings_max[OD8_SETTING_FAN_TARGET_TEMP] > 0 &&
963                     pptable_information->od_settings_min[OD8_SETTING_FAN_TARGET_TEMP] > 0 &&
964                     (pptable_information->od_settings_max[OD8_SETTING_FAN_TARGET_TEMP] >=
965                      pptable_information->od_settings_min[OD8_SETTING_FAN_TARGET_TEMP]))
966                         od_settings->overdrive8_capabilities |= OD8_TEMPERATURE_FAN;
967
968                 if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_TEMPERATURE_SYSTEM] &&
969                     pptable_information->od_settings_max[OD8_SETTING_OPERATING_TEMP_MAX] > 0 &&
970                     pptable_information->od_settings_min[OD8_SETTING_OPERATING_TEMP_MAX] > 0 &&
971                     (pptable_information->od_settings_max[OD8_SETTING_OPERATING_TEMP_MAX] >=
972                      pptable_information->od_settings_min[OD8_SETTING_OPERATING_TEMP_MAX]))
973                         od_settings->overdrive8_capabilities |= OD8_TEMPERATURE_SYSTEM;
974         }
975
976         if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_MEMORY_TIMING_TUNE])
977                 od_settings->overdrive8_capabilities |= OD8_MEMORY_TIMING_TUNE;
978
979         if (pptable_information->od_feature_capabilities[ATOM_VEGA20_ODFEATURE_FAN_ZERO_RPM_CONTROL] &&
980             pp_table->FanZeroRpmEnable)
981                 od_settings->overdrive8_capabilities |= OD8_FAN_ZERO_RPM_CONTROL;
982
983         if (!od_settings->overdrive8_capabilities)
984                 hwmgr->od_enabled = false;
985
986         return 0;
987 }
988
989 static int vega20_od8_set_feature_id(
990                 struct pp_hwmgr *hwmgr)
991 {
992         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
993         struct vega20_od8_settings *od_settings = &(data->od8_settings);
994
995         if (od_settings->overdrive8_capabilities & OD8_GFXCLK_LIMITS) {
996                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMIN].feature_id =
997                         OD8_GFXCLK_LIMITS;
998                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMAX].feature_id =
999                         OD8_GFXCLK_LIMITS;
1000         } else {
1001                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMIN].feature_id =
1002                         0;
1003                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMAX].feature_id =
1004                         0;
1005         }
1006
1007         if (od_settings->overdrive8_capabilities & OD8_GFXCLK_CURVE) {
1008                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ1].feature_id =
1009                         OD8_GFXCLK_CURVE;
1010                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE1].feature_id =
1011                         OD8_GFXCLK_CURVE;
1012                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ2].feature_id =
1013                         OD8_GFXCLK_CURVE;
1014                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE2].feature_id =
1015                         OD8_GFXCLK_CURVE;
1016                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ3].feature_id =
1017                         OD8_GFXCLK_CURVE;
1018                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE3].feature_id =
1019                         OD8_GFXCLK_CURVE;
1020         } else {
1021                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ1].feature_id =
1022                         0;
1023                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE1].feature_id =
1024                         0;
1025                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ2].feature_id =
1026                         0;
1027                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE2].feature_id =
1028                         0;
1029                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ3].feature_id =
1030                         0;
1031                 od_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE3].feature_id =
1032                         0;
1033         }
1034
1035         if (od_settings->overdrive8_capabilities & OD8_UCLK_MAX)
1036                 od_settings->od8_settings_array[OD8_SETTING_UCLK_FMAX].feature_id = OD8_UCLK_MAX;
1037         else
1038                 od_settings->od8_settings_array[OD8_SETTING_UCLK_FMAX].feature_id = 0;
1039
1040         if (od_settings->overdrive8_capabilities & OD8_POWER_LIMIT)
1041                 od_settings->od8_settings_array[OD8_SETTING_POWER_PERCENTAGE].feature_id = OD8_POWER_LIMIT;
1042         else
1043                 od_settings->od8_settings_array[OD8_SETTING_POWER_PERCENTAGE].feature_id = 0;
1044
1045         if (od_settings->overdrive8_capabilities & OD8_ACOUSTIC_LIMIT_SCLK)
1046                 od_settings->od8_settings_array[OD8_SETTING_FAN_ACOUSTIC_LIMIT].feature_id =
1047                         OD8_ACOUSTIC_LIMIT_SCLK;
1048         else
1049                 od_settings->od8_settings_array[OD8_SETTING_FAN_ACOUSTIC_LIMIT].feature_id =
1050                         0;
1051
1052         if (od_settings->overdrive8_capabilities & OD8_FAN_SPEED_MIN)
1053                 od_settings->od8_settings_array[OD8_SETTING_FAN_MIN_SPEED].feature_id =
1054                         OD8_FAN_SPEED_MIN;
1055         else
1056                 od_settings->od8_settings_array[OD8_SETTING_FAN_MIN_SPEED].feature_id =
1057                         0;
1058
1059         if (od_settings->overdrive8_capabilities & OD8_TEMPERATURE_FAN)
1060                 od_settings->od8_settings_array[OD8_SETTING_FAN_TARGET_TEMP].feature_id =
1061                         OD8_TEMPERATURE_FAN;
1062         else
1063                 od_settings->od8_settings_array[OD8_SETTING_FAN_TARGET_TEMP].feature_id =
1064                         0;
1065
1066         if (od_settings->overdrive8_capabilities & OD8_TEMPERATURE_SYSTEM)
1067                 od_settings->od8_settings_array[OD8_SETTING_OPERATING_TEMP_MAX].feature_id =
1068                         OD8_TEMPERATURE_SYSTEM;
1069         else
1070                 od_settings->od8_settings_array[OD8_SETTING_OPERATING_TEMP_MAX].feature_id =
1071                         0;
1072
1073         return 0;
1074 }
1075
1076 static int vega20_od8_get_gfx_clock_base_voltage(
1077                 struct pp_hwmgr *hwmgr,
1078                 uint32_t *voltage,
1079                 uint32_t freq)
1080 {
1081         int ret = 0;
1082
1083         ret = smum_send_msg_to_smc_with_parameter(hwmgr,
1084                         PPSMC_MSG_GetAVFSVoltageByDpm,
1085                         ((AVFS_CURVE << 24) | (OD8_HOTCURVE_TEMPERATURE << 16) | freq));
1086         PP_ASSERT_WITH_CODE(!ret,
1087                         "[GetBaseVoltage] failed to get GFXCLK AVFS voltage from SMU!",
1088                         return ret);
1089
1090         *voltage = smum_get_argument(hwmgr);
1091         *voltage = *voltage / VOLTAGE_SCALE;
1092
1093         return 0;
1094 }
1095
1096 static int vega20_od8_initialize_default_settings(
1097                 struct pp_hwmgr *hwmgr)
1098 {
1099         struct phm_ppt_v3_information *pptable_information =
1100                 (struct phm_ppt_v3_information *)hwmgr->pptable;
1101         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
1102         struct vega20_od8_settings *od8_settings = &(data->od8_settings);
1103         OverDriveTable_t *od_table = &(data->smc_state_table.overdrive_table);
1104         int i, ret = 0;
1105
1106         /* Set Feature Capabilities */
1107         vega20_od8_set_feature_capabilities(hwmgr);
1108
1109         /* Map FeatureID to individual settings */
1110         vega20_od8_set_feature_id(hwmgr);
1111
1112         /* Set default values */
1113         ret = smum_smc_table_manager(hwmgr, (uint8_t *)od_table, TABLE_OVERDRIVE, true);
1114         PP_ASSERT_WITH_CODE(!ret,
1115                         "Failed to export over drive table!",
1116                         return ret);
1117
1118         if (od8_settings->overdrive8_capabilities & OD8_GFXCLK_LIMITS) {
1119                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMIN].default_value =
1120                         od_table->GfxclkFmin;
1121                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMAX].default_value =
1122                         od_table->GfxclkFmax;
1123         } else {
1124                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMIN].default_value =
1125                         0;
1126                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FMAX].default_value =
1127                         0;
1128         }
1129
1130         if (od8_settings->overdrive8_capabilities & OD8_GFXCLK_CURVE) {
1131                 od_table->GfxclkFreq1 = od_table->GfxclkFmin;
1132                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ1].default_value =
1133                         od_table->GfxclkFreq1;
1134
1135                 od_table->GfxclkFreq3 = od_table->GfxclkFmax;
1136                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ3].default_value =
1137                         od_table->GfxclkFreq3;
1138
1139                 od_table->GfxclkFreq2 = (od_table->GfxclkFreq1 + od_table->GfxclkFreq3) / 2;
1140                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ2].default_value =
1141                         od_table->GfxclkFreq2;
1142
1143                 PP_ASSERT_WITH_CODE(!vega20_od8_get_gfx_clock_base_voltage(hwmgr,
1144                                    &(od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE1].default_value),
1145                                      od_table->GfxclkFreq1),
1146                                 "[PhwVega20_OD8_InitializeDefaultSettings] Failed to get Base clock voltage from SMU!",
1147                                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE1].default_value = 0);
1148                 od_table->GfxclkVolt1 = od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE1].default_value
1149                         * VOLTAGE_SCALE;
1150
1151                 PP_ASSERT_WITH_CODE(!vega20_od8_get_gfx_clock_base_voltage(hwmgr,
1152                                    &(od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE2].default_value),
1153                                      od_table->GfxclkFreq2),
1154                                 "[PhwVega20_OD8_InitializeDefaultSettings] Failed to get Base clock voltage from SMU!",
1155                                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE2].default_value = 0);
1156                 od_table->GfxclkVolt2 = od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE2].default_value
1157                         * VOLTAGE_SCALE;
1158
1159                 PP_ASSERT_WITH_CODE(!vega20_od8_get_gfx_clock_base_voltage(hwmgr,
1160                                    &(od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE3].default_value),
1161                                      od_table->GfxclkFreq3),
1162                                 "[PhwVega20_OD8_InitializeDefaultSettings] Failed to get Base clock voltage from SMU!",
1163                                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE3].default_value = 0);
1164                 od_table->GfxclkVolt3 = od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE3].default_value
1165                         * VOLTAGE_SCALE;
1166         } else {
1167                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ1].default_value =
1168                         0;
1169                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE1].default_value =
1170                         0;
1171                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ2].default_value =
1172                         0;
1173                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE2].default_value =
1174                         0;
1175                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_FREQ3].default_value =
1176                         0;
1177                 od8_settings->od8_settings_array[OD8_SETTING_GFXCLK_VOLTAGE3].default_value =
1178                         0;
1179         }
1180
1181         if (od8_settings->overdrive8_capabilities & OD8_UCLK_MAX)
1182                 od8_settings->od8_settings_array[OD8_SETTING_UCLK_FMAX].default_value =
1183                         od_table->UclkFmax;
1184         else
1185                 od8_settings->od8_settings_array[OD8_SETTING_UCLK_FMAX].default_value =
1186                         0;
1187
1188         if (od8_settings->overdrive8_capabilities & OD8_POWER_LIMIT)
1189                 od8_settings->od8_settings_array[OD8_SETTING_POWER_PERCENTAGE].default_value =
1190                         od_table->OverDrivePct;
1191         else
1192                 od8_settings->od8_settings_array[OD8_SETTING_POWER_PERCENTAGE].default_value =
1193                         0;
1194
1195         if (od8_settings->overdrive8_capabilities & OD8_ACOUSTIC_LIMIT_SCLK)
1196                 od8_settings->od8_settings_array[OD8_SETTING_FAN_ACOUSTIC_LIMIT].default_value =
1197                         od_table->FanMaximumRpm;
1198         else
1199                 od8_settings->od8_settings_array[OD8_SETTING_FAN_ACOUSTIC_LIMIT].default_value =
1200                         0;
1201
1202         if (od8_settings->overdrive8_capabilities & OD8_FAN_SPEED_MIN)
1203                 od8_settings->od8_settings_array[OD8_SETTING_FAN_MIN_SPEED].default_value =
1204                         od_table->FanMinimumPwm * data->smc_state_table.pp_table.FanMaximumRpm / 100;
1205         else
1206                 od8_settings->od8_settings_array[OD8_SETTING_FAN_MIN_SPEED].default_value =
1207                         0;
1208
1209         if (od8_settings->overdrive8_capabilities & OD8_TEMPERATURE_FAN)
1210                 od8_settings->od8_settings_array[OD8_SETTING_FAN_TARGET_TEMP].default_value =
1211                         od_table->FanTargetTemperature;
1212         else
1213                 od8_settings->od8_settings_array[OD8_SETTING_FAN_TARGET_TEMP].default_value =
1214                         0;
1215
1216         if (od8_settings->overdrive8_capabilities & OD8_TEMPERATURE_SYSTEM)
1217                 od8_settings->od8_settings_array[OD8_SETTING_OPERATING_TEMP_MAX].default_value =
1218                         od_table->MaxOpTemp;
1219         else
1220                 od8_settings->od8_settings_array[OD8_SETTING_OPERATING_TEMP_MAX].default_value =
1221                         0;
1222
1223         for (i = 0; i < OD8_SETTING_COUNT; i++) {
1224                 if (od8_settings->od8_settings_array[i].feature_id) {
1225                         od8_settings->od8_settings_array[i].min_value =
1226                                 pptable_information->od_settings_min[i];
1227                         od8_settings->od8_settings_array[i].max_value =
1228                                 pptable_information->od_settings_max[i];
1229                         od8_settings->od8_settings_array[i].current_value =
1230                                 od8_settings->od8_settings_array[i].default_value;
1231                 } else {
1232                         od8_settings->od8_settings_array[i].min_value =
1233                                 0;
1234                         od8_settings->od8_settings_array[i].max_value =
1235                                 0;
1236                         od8_settings->od8_settings_array[i].current_value =
1237                                 0;
1238                 }
1239         }
1240
1241         ret = smum_smc_table_manager(hwmgr, (uint8_t *)od_table, TABLE_OVERDRIVE, false);
1242         PP_ASSERT_WITH_CODE(!ret,
1243                         "Failed to import over drive table!",
1244                         return ret);
1245
1246         return 0;
1247 }
1248
1249 static int vega20_od8_set_settings(
1250                 struct pp_hwmgr *hwmgr,
1251                 uint32_t index,
1252                 uint32_t value)
1253 {
1254         OverDriveTable_t od_table;
1255         int ret = 0;
1256         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
1257         struct vega20_od8_single_setting *od8_settings =
1258                         data->od8_settings.od8_settings_array;
1259
1260         ret = smum_smc_table_manager(hwmgr, (uint8_t *)(&od_table), TABLE_OVERDRIVE, true);
1261         PP_ASSERT_WITH_CODE(!ret,
1262                         "Failed to export over drive table!",
1263                         return ret);
1264
1265         switch(index) {
1266         case OD8_SETTING_GFXCLK_FMIN:
1267                 od_table.GfxclkFmin = (uint16_t)value;
1268                 break;
1269         case OD8_SETTING_GFXCLK_FMAX:
1270                 if (value < od8_settings[OD8_SETTING_GFXCLK_FMAX].min_value ||
1271                     value > od8_settings[OD8_SETTING_GFXCLK_FMAX].max_value)
1272                         return -EINVAL;
1273
1274                 od_table.GfxclkFmax = (uint16_t)value;
1275                 break;
1276         case OD8_SETTING_GFXCLK_FREQ1:
1277                 od_table.GfxclkFreq1 = (uint16_t)value;
1278                 break;
1279         case OD8_SETTING_GFXCLK_VOLTAGE1:
1280                 od_table.GfxclkVolt1 = (uint16_t)value;
1281                 break;
1282         case OD8_SETTING_GFXCLK_FREQ2:
1283                 od_table.GfxclkFreq2 = (uint16_t)value;
1284                 break;
1285         case OD8_SETTING_GFXCLK_VOLTAGE2:
1286                 od_table.GfxclkVolt2 = (uint16_t)value;
1287                 break;
1288         case OD8_SETTING_GFXCLK_FREQ3:
1289                 od_table.GfxclkFreq3 = (uint16_t)value;
1290                 break;
1291         case OD8_SETTING_GFXCLK_VOLTAGE3:
1292                 od_table.GfxclkVolt3 = (uint16_t)value;
1293                 break;
1294         case OD8_SETTING_UCLK_FMAX:
1295                 if (value < od8_settings[OD8_SETTING_UCLK_FMAX].min_value ||
1296                     value > od8_settings[OD8_SETTING_UCLK_FMAX].max_value)
1297                         return -EINVAL;
1298                 od_table.UclkFmax = (uint16_t)value;
1299                 break;
1300         case OD8_SETTING_POWER_PERCENTAGE:
1301                 od_table.OverDrivePct = (int16_t)value;
1302                 break;
1303         case OD8_SETTING_FAN_ACOUSTIC_LIMIT:
1304                 od_table.FanMaximumRpm = (uint16_t)value;
1305                 break;
1306         case OD8_SETTING_FAN_MIN_SPEED:
1307                 od_table.FanMinimumPwm = (uint16_t)value;
1308                 break;
1309         case OD8_SETTING_FAN_TARGET_TEMP:
1310                 od_table.FanTargetTemperature = (uint16_t)value;
1311                 break;
1312         case OD8_SETTING_OPERATING_TEMP_MAX:
1313                 od_table.MaxOpTemp = (uint16_t)value;
1314                 break;
1315         }
1316
1317         ret = smum_smc_table_manager(hwmgr, (uint8_t *)(&od_table), TABLE_OVERDRIVE, false);
1318         PP_ASSERT_WITH_CODE(!ret,
1319                         "Failed to import over drive table!",
1320                         return ret);
1321
1322         return 0;
1323 }
1324
1325 static int vega20_get_sclk_od(
1326                 struct pp_hwmgr *hwmgr)
1327 {
1328         struct vega20_hwmgr *data = hwmgr->backend;
1329         struct vega20_single_dpm_table *sclk_table =
1330                         &(data->dpm_table.gfx_table);
1331         struct vega20_single_dpm_table *golden_sclk_table =
1332                         &(data->golden_dpm_table.gfx_table);
1333         int value = sclk_table->dpm_levels[sclk_table->count - 1].value;
1334         int golden_value = golden_sclk_table->dpm_levels
1335                         [golden_sclk_table->count - 1].value;
1336
1337         /* od percentage */
1338         value -= golden_value;
1339         value = DIV_ROUND_UP(value * 100, golden_value);
1340
1341         return value;
1342 }
1343
1344 static int vega20_set_sclk_od(
1345                 struct pp_hwmgr *hwmgr, uint32_t value)
1346 {
1347         struct vega20_hwmgr *data = hwmgr->backend;
1348         struct vega20_single_dpm_table *golden_sclk_table =
1349                         &(data->golden_dpm_table.gfx_table);
1350         uint32_t od_sclk;
1351         int ret = 0;
1352
1353         od_sclk = golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value * value;
1354         od_sclk /= 100;
1355         od_sclk += golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value;
1356
1357         ret = vega20_od8_set_settings(hwmgr, OD8_SETTING_GFXCLK_FMAX, od_sclk);
1358         PP_ASSERT_WITH_CODE(!ret,
1359                         "[SetSclkOD] failed to set od gfxclk!",
1360                         return ret);
1361
1362         /* retrieve updated gfxclk table */
1363         ret = vega20_setup_gfxclk_dpm_table(hwmgr);
1364         PP_ASSERT_WITH_CODE(!ret,
1365                         "[SetSclkOD] failed to refresh gfxclk table!",
1366                         return ret);
1367
1368         return 0;
1369 }
1370
1371 static int vega20_get_mclk_od(
1372                 struct pp_hwmgr *hwmgr)
1373 {
1374         struct vega20_hwmgr *data = hwmgr->backend;
1375         struct vega20_single_dpm_table *mclk_table =
1376                         &(data->dpm_table.mem_table);
1377         struct vega20_single_dpm_table *golden_mclk_table =
1378                         &(data->golden_dpm_table.mem_table);
1379         int value = mclk_table->dpm_levels[mclk_table->count - 1].value;
1380         int golden_value = golden_mclk_table->dpm_levels
1381                         [golden_mclk_table->count - 1].value;
1382
1383         /* od percentage */
1384         value -= golden_value;
1385         value = DIV_ROUND_UP(value * 100, golden_value);
1386
1387         return value;
1388 }
1389
1390 static int vega20_set_mclk_od(
1391                 struct pp_hwmgr *hwmgr, uint32_t value)
1392 {
1393         struct vega20_hwmgr *data = hwmgr->backend;
1394         struct vega20_single_dpm_table *golden_mclk_table =
1395                         &(data->golden_dpm_table.mem_table);
1396         uint32_t od_mclk;
1397         int ret = 0;
1398
1399         od_mclk = golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value * value;
1400         od_mclk /= 100;
1401         od_mclk += golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value;
1402
1403         ret = vega20_od8_set_settings(hwmgr, OD8_SETTING_UCLK_FMAX, od_mclk);
1404         PP_ASSERT_WITH_CODE(!ret,
1405                         "[SetMclkOD] failed to set od memclk!",
1406                         return ret);
1407
1408         /* retrieve updated memclk table */
1409         ret = vega20_setup_memclk_dpm_table(hwmgr);
1410         PP_ASSERT_WITH_CODE(!ret,
1411                         "[SetMclkOD] failed to refresh memclk table!",
1412                         return ret);
1413
1414         return 0;
1415 }
1416
1417 static int vega20_populate_umdpstate_clocks(
1418                 struct pp_hwmgr *hwmgr)
1419 {
1420         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
1421         struct vega20_single_dpm_table *gfx_table = &(data->dpm_table.gfx_table);
1422         struct vega20_single_dpm_table *mem_table = &(data->dpm_table.mem_table);
1423
1424         hwmgr->pstate_sclk = gfx_table->dpm_levels[0].value;
1425         hwmgr->pstate_mclk = mem_table->dpm_levels[0].value;
1426
1427         if (gfx_table->count > VEGA20_UMD_PSTATE_GFXCLK_LEVEL &&
1428             mem_table->count > VEGA20_UMD_PSTATE_MCLK_LEVEL) {
1429                 hwmgr->pstate_sclk = gfx_table->dpm_levels[VEGA20_UMD_PSTATE_GFXCLK_LEVEL].value;
1430                 hwmgr->pstate_mclk = mem_table->dpm_levels[VEGA20_UMD_PSTATE_MCLK_LEVEL].value;
1431         }
1432
1433         hwmgr->pstate_sclk = hwmgr->pstate_sclk * 100;
1434         hwmgr->pstate_mclk = hwmgr->pstate_mclk * 100;
1435
1436         return 0;
1437 }
1438
1439 static int vega20_get_max_sustainable_clock(struct pp_hwmgr *hwmgr,
1440                 PP_Clock *clock, PPCLK_e clock_select)
1441 {
1442         int ret = 0;
1443
1444         PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc_with_parameter(hwmgr,
1445                         PPSMC_MSG_GetDcModeMaxDpmFreq,
1446                         (clock_select << 16))) == 0,
1447                         "[GetMaxSustainableClock] Failed to get max DC clock from SMC!",
1448                         return ret);
1449         *clock = smum_get_argument(hwmgr);
1450
1451         /* if DC limit is zero, return AC limit */
1452         if (*clock == 0) {
1453                 PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc_with_parameter(hwmgr,
1454                         PPSMC_MSG_GetMaxDpmFreq,
1455                         (clock_select << 16))) == 0,
1456                         "[GetMaxSustainableClock] failed to get max AC clock from SMC!",
1457                         return ret);
1458                 *clock = smum_get_argument(hwmgr);
1459         }
1460
1461         return 0;
1462 }
1463
1464 static int vega20_init_max_sustainable_clocks(struct pp_hwmgr *hwmgr)
1465 {
1466         struct vega20_hwmgr *data =
1467                 (struct vega20_hwmgr *)(hwmgr->backend);
1468         struct vega20_max_sustainable_clocks *max_sustainable_clocks =
1469                 &(data->max_sustainable_clocks);
1470         int ret = 0;
1471
1472         max_sustainable_clocks->uclock = data->vbios_boot_state.mem_clock / 100;
1473         max_sustainable_clocks->soc_clock = data->vbios_boot_state.soc_clock / 100;
1474         max_sustainable_clocks->dcef_clock = data->vbios_boot_state.dcef_clock / 100;
1475         max_sustainable_clocks->display_clock = 0xFFFFFFFF;
1476         max_sustainable_clocks->phy_clock = 0xFFFFFFFF;
1477         max_sustainable_clocks->pixel_clock = 0xFFFFFFFF;
1478
1479         if (data->smu_features[GNLD_DPM_UCLK].enabled)
1480                 PP_ASSERT_WITH_CODE((ret = vega20_get_max_sustainable_clock(hwmgr,
1481                                 &(max_sustainable_clocks->uclock),
1482                                 PPCLK_UCLK)) == 0,
1483                                 "[InitMaxSustainableClocks] failed to get max UCLK from SMC!",
1484                                 return ret);
1485
1486         if (data->smu_features[GNLD_DPM_SOCCLK].enabled)
1487                 PP_ASSERT_WITH_CODE((ret = vega20_get_max_sustainable_clock(hwmgr,
1488                                 &(max_sustainable_clocks->soc_clock),
1489                                 PPCLK_SOCCLK)) == 0,
1490                                 "[InitMaxSustainableClocks] failed to get max SOCCLK from SMC!",
1491                                 return ret);
1492
1493         if (data->smu_features[GNLD_DPM_DCEFCLK].enabled) {
1494                 PP_ASSERT_WITH_CODE((ret = vega20_get_max_sustainable_clock(hwmgr,
1495                                 &(max_sustainable_clocks->dcef_clock),
1496                                 PPCLK_DCEFCLK)) == 0,
1497                                 "[InitMaxSustainableClocks] failed to get max DCEFCLK from SMC!",
1498                                 return ret);
1499                 PP_ASSERT_WITH_CODE((ret = vega20_get_max_sustainable_clock(hwmgr,
1500                                 &(max_sustainable_clocks->display_clock),
1501                                 PPCLK_DISPCLK)) == 0,
1502                                 "[InitMaxSustainableClocks] failed to get max DISPCLK from SMC!",
1503                                 return ret);
1504                 PP_ASSERT_WITH_CODE((ret = vega20_get_max_sustainable_clock(hwmgr,
1505                                 &(max_sustainable_clocks->phy_clock),
1506                                 PPCLK_PHYCLK)) == 0,
1507                                 "[InitMaxSustainableClocks] failed to get max PHYCLK from SMC!",
1508                                 return ret);
1509                 PP_ASSERT_WITH_CODE((ret = vega20_get_max_sustainable_clock(hwmgr,
1510                                 &(max_sustainable_clocks->pixel_clock),
1511                                 PPCLK_PIXCLK)) == 0,
1512                                 "[InitMaxSustainableClocks] failed to get max PIXCLK from SMC!",
1513                                 return ret);
1514         }
1515
1516         if (max_sustainable_clocks->soc_clock < max_sustainable_clocks->uclock)
1517                 max_sustainable_clocks->uclock = max_sustainable_clocks->soc_clock;
1518
1519         return 0;
1520 }
1521
1522 static int vega20_enable_mgpu_fan_boost(struct pp_hwmgr *hwmgr)
1523 {
1524         int result;
1525
1526         result = smum_send_msg_to_smc(hwmgr,
1527                 PPSMC_MSG_SetMGpuFanBoostLimitRpm);
1528         PP_ASSERT_WITH_CODE(!result,
1529                         "[EnableMgpuFan] Failed to enable mgpu fan boost!",
1530                         return result);
1531
1532         return 0;
1533 }
1534
1535 static void vega20_init_powergate_state(struct pp_hwmgr *hwmgr)
1536 {
1537         struct vega20_hwmgr *data =
1538                 (struct vega20_hwmgr *)(hwmgr->backend);
1539
1540         data->uvd_power_gated = true;
1541         data->vce_power_gated = true;
1542
1543         if (data->smu_features[GNLD_DPM_UVD].enabled)
1544                 data->uvd_power_gated = false;
1545
1546         if (data->smu_features[GNLD_DPM_VCE].enabled)
1547                 data->vce_power_gated = false;
1548 }
1549
1550 static int vega20_enable_dpm_tasks(struct pp_hwmgr *hwmgr)
1551 {
1552         int result = 0;
1553
1554         smum_send_msg_to_smc_with_parameter(hwmgr,
1555                         PPSMC_MSG_NumOfDisplays, 0);
1556
1557         result = vega20_set_allowed_featuresmask(hwmgr);
1558         PP_ASSERT_WITH_CODE(!result,
1559                         "[EnableDPMTasks] Failed to set allowed featuresmask!\n",
1560                         return result);
1561
1562         result = vega20_init_smc_table(hwmgr);
1563         PP_ASSERT_WITH_CODE(!result,
1564                         "[EnableDPMTasks] Failed to initialize SMC table!",
1565                         return result);
1566
1567         result = vega20_run_btc_afll(hwmgr);
1568         PP_ASSERT_WITH_CODE(!result,
1569                         "[EnableDPMTasks] Failed to run btc afll!",
1570                         return result);
1571
1572         result = vega20_enable_all_smu_features(hwmgr);
1573         PP_ASSERT_WITH_CODE(!result,
1574                         "[EnableDPMTasks] Failed to enable all smu features!",
1575                         return result);
1576
1577         result = vega20_notify_smc_display_change(hwmgr);
1578         PP_ASSERT_WITH_CODE(!result,
1579                         "[EnableDPMTasks] Failed to notify smc display change!",
1580                         return result);
1581
1582         result = vega20_send_clock_ratio(hwmgr);
1583         PP_ASSERT_WITH_CODE(!result,
1584                         "[EnableDPMTasks] Failed to send clock ratio!",
1585                         return result);
1586
1587         /* Initialize UVD/VCE powergating state */
1588         vega20_init_powergate_state(hwmgr);
1589
1590         result = vega20_setup_default_dpm_tables(hwmgr);
1591         PP_ASSERT_WITH_CODE(!result,
1592                         "[EnableDPMTasks] Failed to setup default DPM tables!",
1593                         return result);
1594
1595         result = vega20_init_max_sustainable_clocks(hwmgr);
1596         PP_ASSERT_WITH_CODE(!result,
1597                         "[EnableDPMTasks] Failed to get maximum sustainable clocks!",
1598                         return result);
1599
1600         result = vega20_power_control_set_level(hwmgr);
1601         PP_ASSERT_WITH_CODE(!result,
1602                         "[EnableDPMTasks] Failed to power control set level!",
1603                         return result);
1604
1605         result = vega20_od8_initialize_default_settings(hwmgr);
1606         PP_ASSERT_WITH_CODE(!result,
1607                         "[EnableDPMTasks] Failed to initialize odn settings!",
1608                         return result);
1609
1610         result = vega20_populate_umdpstate_clocks(hwmgr);
1611         PP_ASSERT_WITH_CODE(!result,
1612                         "[EnableDPMTasks] Failed to populate umdpstate clocks!",
1613                         return result);
1614
1615         result = smum_send_msg_to_smc_with_parameter(hwmgr, PPSMC_MSG_GetPptLimit,
1616                         POWER_SOURCE_AC << 16);
1617         PP_ASSERT_WITH_CODE(!result,
1618                         "[GetPptLimit] get default PPT limit failed!",
1619                         return result);
1620         hwmgr->power_limit =
1621                 hwmgr->default_power_limit = smum_get_argument(hwmgr);
1622
1623         return 0;
1624 }
1625
1626 static uint32_t vega20_find_lowest_dpm_level(
1627                 struct vega20_single_dpm_table *table)
1628 {
1629         uint32_t i;
1630
1631         for (i = 0; i < table->count; i++) {
1632                 if (table->dpm_levels[i].enabled)
1633                         break;
1634         }
1635         if (i >= table->count) {
1636                 i = 0;
1637                 table->dpm_levels[i].enabled = true;
1638         }
1639
1640         return i;
1641 }
1642
1643 static uint32_t vega20_find_highest_dpm_level(
1644                 struct vega20_single_dpm_table *table)
1645 {
1646         int i = 0;
1647
1648         PP_ASSERT_WITH_CODE(table != NULL,
1649                         "[FindHighestDPMLevel] DPM Table does not exist!",
1650                         return 0);
1651         PP_ASSERT_WITH_CODE(table->count > 0,
1652                         "[FindHighestDPMLevel] DPM Table has no entry!",
1653                         return 0);
1654         PP_ASSERT_WITH_CODE(table->count <= MAX_REGULAR_DPM_NUMBER,
1655                         "[FindHighestDPMLevel] DPM Table has too many entries!",
1656                         return MAX_REGULAR_DPM_NUMBER - 1);
1657
1658         for (i = table->count - 1; i >= 0; i--) {
1659                 if (table->dpm_levels[i].enabled)
1660                         break;
1661         }
1662         if (i < 0) {
1663                 i = 0;
1664                 table->dpm_levels[i].enabled = true;
1665         }
1666
1667         return i;
1668 }
1669
1670 static int vega20_upload_dpm_min_level(struct pp_hwmgr *hwmgr)
1671 {
1672         struct vega20_hwmgr *data =
1673                         (struct vega20_hwmgr *)(hwmgr->backend);
1674         uint32_t min_freq;
1675         int ret = 0;
1676
1677         if (data->smu_features[GNLD_DPM_GFXCLK].enabled) {
1678                 min_freq = data->dpm_table.gfx_table.dpm_state.soft_min_level;
1679                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1680                                         hwmgr, PPSMC_MSG_SetSoftMinByFreq,
1681                                         (PPCLK_GFXCLK << 16) | (min_freq & 0xffff))),
1682                                         "Failed to set soft min gfxclk !",
1683                                         return ret);
1684         }
1685
1686         if (data->smu_features[GNLD_DPM_UCLK].enabled) {
1687                 min_freq = data->dpm_table.mem_table.dpm_state.soft_min_level;
1688                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1689                                         hwmgr, PPSMC_MSG_SetSoftMinByFreq,
1690                                         (PPCLK_UCLK << 16) | (min_freq & 0xffff))),
1691                                         "Failed to set soft min memclk !",
1692                                         return ret);
1693         }
1694
1695         if (data->smu_features[GNLD_DPM_UVD].enabled) {
1696                 min_freq = data->dpm_table.vclk_table.dpm_state.soft_min_level;
1697
1698                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1699                                         hwmgr, PPSMC_MSG_SetSoftMinByFreq,
1700                                         (PPCLK_VCLK << 16) | (min_freq & 0xffff))),
1701                                         "Failed to set soft min vclk!",
1702                                         return ret);
1703
1704                 min_freq = data->dpm_table.dclk_table.dpm_state.soft_min_level;
1705
1706                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1707                                         hwmgr, PPSMC_MSG_SetSoftMinByFreq,
1708                                         (PPCLK_DCLK << 16) | (min_freq & 0xffff))),
1709                                         "Failed to set soft min dclk!",
1710                                         return ret);
1711         }
1712
1713         if (data->smu_features[GNLD_DPM_VCE].enabled) {
1714                 min_freq = data->dpm_table.eclk_table.dpm_state.soft_min_level;
1715
1716                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1717                                         hwmgr, PPSMC_MSG_SetSoftMinByFreq,
1718                                         (PPCLK_ECLK << 16) | (min_freq & 0xffff))),
1719                                         "Failed to set soft min eclk!",
1720                                         return ret);
1721         }
1722
1723         if (data->smu_features[GNLD_DPM_SOCCLK].enabled) {
1724                 min_freq = data->dpm_table.soc_table.dpm_state.soft_min_level;
1725
1726                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1727                                         hwmgr, PPSMC_MSG_SetSoftMinByFreq,
1728                                         (PPCLK_SOCCLK << 16) | (min_freq & 0xffff))),
1729                                         "Failed to set soft min socclk!",
1730                                         return ret);
1731         }
1732
1733         return ret;
1734 }
1735
1736 static int vega20_upload_dpm_max_level(struct pp_hwmgr *hwmgr)
1737 {
1738         struct vega20_hwmgr *data =
1739                         (struct vega20_hwmgr *)(hwmgr->backend);
1740         uint32_t max_freq;
1741         int ret = 0;
1742
1743         if (data->smu_features[GNLD_DPM_GFXCLK].enabled) {
1744                 max_freq = data->dpm_table.gfx_table.dpm_state.soft_max_level;
1745
1746                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1747                                         hwmgr, PPSMC_MSG_SetSoftMaxByFreq,
1748                                         (PPCLK_GFXCLK << 16) | (max_freq & 0xffff))),
1749                                         "Failed to set soft max gfxclk!",
1750                                         return ret);
1751         }
1752
1753         if (data->smu_features[GNLD_DPM_UCLK].enabled) {
1754                 max_freq = data->dpm_table.mem_table.dpm_state.soft_max_level;
1755
1756                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1757                                         hwmgr, PPSMC_MSG_SetSoftMaxByFreq,
1758                                         (PPCLK_UCLK << 16) | (max_freq & 0xffff))),
1759                                         "Failed to set soft max memclk!",
1760                                         return ret);
1761         }
1762
1763         if (data->smu_features[GNLD_DPM_UVD].enabled) {
1764                 max_freq = data->dpm_table.vclk_table.dpm_state.soft_max_level;
1765
1766                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1767                                         hwmgr, PPSMC_MSG_SetSoftMaxByFreq,
1768                                         (PPCLK_VCLK << 16) | (max_freq & 0xffff))),
1769                                         "Failed to set soft max vclk!",
1770                                         return ret);
1771
1772                 max_freq = data->dpm_table.dclk_table.dpm_state.soft_max_level;
1773                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1774                                         hwmgr, PPSMC_MSG_SetSoftMaxByFreq,
1775                                         (PPCLK_DCLK << 16) | (max_freq & 0xffff))),
1776                                         "Failed to set soft max dclk!",
1777                                         return ret);
1778         }
1779
1780         if (data->smu_features[GNLD_DPM_VCE].enabled) {
1781                 max_freq = data->dpm_table.eclk_table.dpm_state.soft_max_level;
1782
1783                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1784                                         hwmgr, PPSMC_MSG_SetSoftMaxByFreq,
1785                                         (PPCLK_ECLK << 16) | (max_freq & 0xffff))),
1786                                         "Failed to set soft max eclk!",
1787                                         return ret);
1788         }
1789
1790         if (data->smu_features[GNLD_DPM_SOCCLK].enabled) {
1791                 max_freq = data->dpm_table.soc_table.dpm_state.soft_max_level;
1792
1793                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(
1794                                         hwmgr, PPSMC_MSG_SetSoftMaxByFreq,
1795                                         (PPCLK_SOCCLK << 16) | (max_freq & 0xffff))),
1796                                         "Failed to set soft max socclk!",
1797                                         return ret);
1798         }
1799
1800         return ret;
1801 }
1802
1803 int vega20_enable_disable_vce_dpm(struct pp_hwmgr *hwmgr, bool enable)
1804 {
1805         struct vega20_hwmgr *data =
1806                         (struct vega20_hwmgr *)(hwmgr->backend);
1807         int ret = 0;
1808
1809         if (data->smu_features[GNLD_DPM_VCE].supported) {
1810                 if (data->smu_features[GNLD_DPM_VCE].enabled == enable) {
1811                         if (enable)
1812                                 PP_DBG_LOG("[EnableDisableVCEDPM] feature VCE DPM already enabled!\n");
1813                         else
1814                                 PP_DBG_LOG("[EnableDisableVCEDPM] feature VCE DPM already disabled!\n");
1815                 }
1816
1817                 ret = vega20_enable_smc_features(hwmgr,
1818                                 enable,
1819                                 data->smu_features[GNLD_DPM_VCE].smu_feature_bitmap);
1820                 PP_ASSERT_WITH_CODE(!ret,
1821                                 "Attempt to Enable/Disable DPM VCE Failed!",
1822                                 return ret);
1823                 data->smu_features[GNLD_DPM_VCE].enabled = enable;
1824         }
1825
1826         return 0;
1827 }
1828
1829 static int vega20_get_clock_ranges(struct pp_hwmgr *hwmgr,
1830                 uint32_t *clock,
1831                 PPCLK_e clock_select,
1832                 bool max)
1833 {
1834         int ret;
1835         *clock = 0;
1836
1837         if (max) {
1838                 PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc_with_parameter(hwmgr,
1839                                 PPSMC_MSG_GetMaxDpmFreq, (clock_select << 16))) == 0,
1840                                 "[GetClockRanges] Failed to get max clock from SMC!",
1841                                 return ret);
1842                 *clock = smum_get_argument(hwmgr);
1843         } else {
1844                 PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc_with_parameter(hwmgr,
1845                                 PPSMC_MSG_GetMinDpmFreq,
1846                                 (clock_select << 16))) == 0,
1847                                 "[GetClockRanges] Failed to get min clock from SMC!",
1848                                 return ret);
1849                 *clock = smum_get_argument(hwmgr);
1850         }
1851
1852         return 0;
1853 }
1854
1855 static uint32_t vega20_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
1856 {
1857         struct vega20_hwmgr *data =
1858                         (struct vega20_hwmgr *)(hwmgr->backend);
1859         uint32_t gfx_clk;
1860         int ret = 0;
1861
1862         PP_ASSERT_WITH_CODE(data->smu_features[GNLD_DPM_GFXCLK].enabled,
1863                         "[GetSclks]: gfxclk dpm not enabled!\n",
1864                         return -EPERM);
1865
1866         if (low) {
1867                 ret = vega20_get_clock_ranges(hwmgr, &gfx_clk, PPCLK_GFXCLK, false);
1868                 PP_ASSERT_WITH_CODE(!ret,
1869                         "[GetSclks]: fail to get min PPCLK_GFXCLK\n",
1870                         return ret);
1871         } else {
1872                 ret = vega20_get_clock_ranges(hwmgr, &gfx_clk, PPCLK_GFXCLK, true);
1873                 PP_ASSERT_WITH_CODE(!ret,
1874                         "[GetSclks]: fail to get max PPCLK_GFXCLK\n",
1875                         return ret);
1876         }
1877
1878         return (gfx_clk * 100);
1879 }
1880
1881 static uint32_t vega20_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
1882 {
1883         struct vega20_hwmgr *data =
1884                         (struct vega20_hwmgr *)(hwmgr->backend);
1885         uint32_t mem_clk;
1886         int ret = 0;
1887
1888         PP_ASSERT_WITH_CODE(data->smu_features[GNLD_DPM_UCLK].enabled,
1889                         "[MemMclks]: memclk dpm not enabled!\n",
1890                         return -EPERM);
1891
1892         if (low) {
1893                 ret = vega20_get_clock_ranges(hwmgr, &mem_clk, PPCLK_UCLK, false);
1894                 PP_ASSERT_WITH_CODE(!ret,
1895                         "[GetMclks]: fail to get min PPCLK_UCLK\n",
1896                         return ret);
1897         } else {
1898                 ret = vega20_get_clock_ranges(hwmgr, &mem_clk, PPCLK_UCLK, true);
1899                 PP_ASSERT_WITH_CODE(!ret,
1900                         "[GetMclks]: fail to get max PPCLK_UCLK\n",
1901                         return ret);
1902         }
1903
1904         return (mem_clk * 100);
1905 }
1906
1907 static int vega20_get_gpu_power(struct pp_hwmgr *hwmgr,
1908                 uint32_t *query)
1909 {
1910         int ret = 0;
1911         SmuMetrics_t metrics_table;
1912
1913         ret = smum_smc_table_manager(hwmgr, (uint8_t *)&metrics_table, TABLE_SMU_METRICS, true);
1914         PP_ASSERT_WITH_CODE(!ret,
1915                         "Failed to export SMU METRICS table!",
1916                         return ret);
1917
1918         *query = metrics_table.CurrSocketPower << 8;
1919
1920         return ret;
1921 }
1922
1923 static int vega20_get_current_clk_freq(struct pp_hwmgr *hwmgr,
1924                 PPCLK_e clk_id, uint32_t *clk_freq)
1925 {
1926         int ret = 0;
1927
1928         *clk_freq = 0;
1929
1930         PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc_with_parameter(hwmgr,
1931                         PPSMC_MSG_GetDpmClockFreq, (clk_id << 16))) == 0,
1932                         "[GetCurrentClkFreq] Attempt to get Current Frequency Failed!",
1933                         return ret);
1934         *clk_freq = smum_get_argument(hwmgr);
1935
1936         *clk_freq = *clk_freq * 100;
1937
1938         return 0;
1939 }
1940
1941 static int vega20_get_current_activity_percent(struct pp_hwmgr *hwmgr,
1942                 uint32_t *activity_percent)
1943 {
1944         int ret = 0;
1945         SmuMetrics_t metrics_table;
1946
1947         ret = smum_smc_table_manager(hwmgr, (uint8_t *)&metrics_table, TABLE_SMU_METRICS, true);
1948         PP_ASSERT_WITH_CODE(!ret,
1949                         "Failed to export SMU METRICS table!",
1950                         return ret);
1951
1952         *activity_percent = metrics_table.AverageGfxActivity;
1953
1954         return ret;
1955 }
1956
1957 static int vega20_read_sensor(struct pp_hwmgr *hwmgr, int idx,
1958                               void *value, int *size)
1959 {
1960         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
1961         struct amdgpu_device *adev = hwmgr->adev;
1962         uint32_t val_vid;
1963         int ret = 0;
1964
1965         switch (idx) {
1966         case AMDGPU_PP_SENSOR_GFX_SCLK:
1967                 ret = vega20_get_current_clk_freq(hwmgr,
1968                                 PPCLK_GFXCLK,
1969                                 (uint32_t *)value);
1970                 if (!ret)
1971                         *size = 4;
1972                 break;
1973         case AMDGPU_PP_SENSOR_GFX_MCLK:
1974                 ret = vega20_get_current_clk_freq(hwmgr,
1975                                 PPCLK_UCLK,
1976                                 (uint32_t *)value);
1977                 if (!ret)
1978                         *size = 4;
1979                 break;
1980         case AMDGPU_PP_SENSOR_GPU_LOAD:
1981                 ret = vega20_get_current_activity_percent(hwmgr, (uint32_t *)value);
1982                 if (!ret)
1983                         *size = 4;
1984                 break;
1985         case AMDGPU_PP_SENSOR_GPU_TEMP:
1986                 *((uint32_t *)value) = vega20_thermal_get_temperature(hwmgr);
1987                 *size = 4;
1988                 break;
1989         case AMDGPU_PP_SENSOR_UVD_POWER:
1990                 *((uint32_t *)value) = data->uvd_power_gated ? 0 : 1;
1991                 *size = 4;
1992                 break;
1993         case AMDGPU_PP_SENSOR_VCE_POWER:
1994                 *((uint32_t *)value) = data->vce_power_gated ? 0 : 1;
1995                 *size = 4;
1996                 break;
1997         case AMDGPU_PP_SENSOR_GPU_POWER:
1998                 *size = 16;
1999                 ret = vega20_get_gpu_power(hwmgr, (uint32_t *)value);
2000                 break;
2001         case AMDGPU_PP_SENSOR_VDDGFX:
2002                 val_vid = (RREG32_SOC15(SMUIO, 0, mmSMUSVI0_TEL_PLANE0) &
2003                         SMUSVI0_TEL_PLANE0__SVI0_PLANE0_VDDCOR_MASK) >>
2004                         SMUSVI0_TEL_PLANE0__SVI0_PLANE0_VDDCOR__SHIFT;
2005                 *((uint32_t *)value) =
2006                         (uint32_t)convert_to_vddc((uint8_t)val_vid);
2007                 break;
2008         case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
2009                 ret = vega20_get_enabled_smc_features(hwmgr, (uint64_t *)value);
2010                 if (!ret)
2011                         *size = 8;
2012                 break;
2013         default:
2014                 ret = -EINVAL;
2015                 break;
2016         }
2017         return ret;
2018 }
2019
2020 int vega20_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
2021                 struct pp_display_clock_request *clock_req)
2022 {
2023         int result = 0;
2024         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2025         enum amd_pp_clock_type clk_type = clock_req->clock_type;
2026         uint32_t clk_freq = clock_req->clock_freq_in_khz / 1000;
2027         PPCLK_e clk_select = 0;
2028         uint32_t clk_request = 0;
2029
2030         if (data->smu_features[GNLD_DPM_DCEFCLK].enabled) {
2031                 switch (clk_type) {
2032                 case amd_pp_dcef_clock:
2033                         clk_select = PPCLK_DCEFCLK;
2034                         break;
2035                 case amd_pp_disp_clock:
2036                         clk_select = PPCLK_DISPCLK;
2037                         break;
2038                 case amd_pp_pixel_clock:
2039                         clk_select = PPCLK_PIXCLK;
2040                         break;
2041                 case amd_pp_phy_clock:
2042                         clk_select = PPCLK_PHYCLK;
2043                         break;
2044                 default:
2045                         pr_info("[DisplayClockVoltageRequest]Invalid Clock Type!");
2046                         result = -EINVAL;
2047                         break;
2048                 }
2049
2050                 if (!result) {
2051                         clk_request = (clk_select << 16) | clk_freq;
2052                         result = smum_send_msg_to_smc_with_parameter(hwmgr,
2053                                         PPSMC_MSG_SetHardMinByFreq,
2054                                         clk_request);
2055                 }
2056         }
2057
2058         return result;
2059 }
2060
2061 static int vega20_get_performance_level(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state,
2062                                 PHM_PerformanceLevelDesignation designation, uint32_t index,
2063                                 PHM_PerformanceLevel *level)
2064 {
2065         return 0;
2066 }
2067
2068 static int vega20_notify_smc_display_config_after_ps_adjustment(
2069                 struct pp_hwmgr *hwmgr)
2070 {
2071         struct vega20_hwmgr *data =
2072                         (struct vega20_hwmgr *)(hwmgr->backend);
2073         struct vega20_single_dpm_table *dpm_table =
2074                         &data->dpm_table.mem_table;
2075         struct PP_Clocks min_clocks = {0};
2076         struct pp_display_clock_request clock_req;
2077         int ret = 0;
2078
2079         min_clocks.dcefClock = hwmgr->display_config->min_dcef_set_clk;
2080         min_clocks.dcefClockInSR = hwmgr->display_config->min_dcef_deep_sleep_set_clk;
2081         min_clocks.memoryClock = hwmgr->display_config->min_mem_set_clock;
2082
2083         if (data->smu_features[GNLD_DPM_DCEFCLK].supported) {
2084                 clock_req.clock_type = amd_pp_dcef_clock;
2085                 clock_req.clock_freq_in_khz = min_clocks.dcefClock * 10;
2086                 if (!vega20_display_clock_voltage_request(hwmgr, &clock_req)) {
2087                         if (data->smu_features[GNLD_DS_DCEFCLK].supported)
2088                                 PP_ASSERT_WITH_CODE((ret = smum_send_msg_to_smc_with_parameter(
2089                                         hwmgr, PPSMC_MSG_SetMinDeepSleepDcefclk,
2090                                         min_clocks.dcefClockInSR / 100)) == 0,
2091                                         "Attempt to set divider for DCEFCLK Failed!",
2092                                         return ret);
2093                 } else {
2094                         pr_info("Attempt to set Hard Min for DCEFCLK Failed!");
2095                 }
2096         }
2097
2098         if (data->smu_features[GNLD_DPM_UCLK].enabled) {
2099                 dpm_table->dpm_state.hard_min_level = min_clocks.memoryClock / 100;
2100                 PP_ASSERT_WITH_CODE(!(ret = smum_send_msg_to_smc_with_parameter(hwmgr,
2101                                 PPSMC_MSG_SetHardMinByFreq,
2102                                 (PPCLK_UCLK << 16 ) | dpm_table->dpm_state.hard_min_level)),
2103                                 "[SetHardMinFreq] Set hard min uclk failed!",
2104                                 return ret);
2105         }
2106
2107         return 0;
2108 }
2109
2110 static int vega20_force_dpm_highest(struct pp_hwmgr *hwmgr)
2111 {
2112         struct vega20_hwmgr *data =
2113                         (struct vega20_hwmgr *)(hwmgr->backend);
2114         uint32_t soft_level;
2115         int ret = 0;
2116
2117         soft_level = vega20_find_highest_dpm_level(&(data->dpm_table.gfx_table));
2118
2119         data->dpm_table.gfx_table.dpm_state.soft_min_level =
2120                 data->dpm_table.gfx_table.dpm_state.soft_max_level =
2121                 data->dpm_table.gfx_table.dpm_levels[soft_level].value;
2122
2123         soft_level = vega20_find_highest_dpm_level(&(data->dpm_table.mem_table));
2124
2125         data->dpm_table.mem_table.dpm_state.soft_min_level =
2126                 data->dpm_table.mem_table.dpm_state.soft_max_level =
2127                 data->dpm_table.mem_table.dpm_levels[soft_level].value;
2128
2129         ret = vega20_upload_dpm_min_level(hwmgr);
2130         PP_ASSERT_WITH_CODE(!ret,
2131                         "Failed to upload boot level to highest!",
2132                         return ret);
2133
2134         ret = vega20_upload_dpm_max_level(hwmgr);
2135         PP_ASSERT_WITH_CODE(!ret,
2136                         "Failed to upload dpm max level to highest!",
2137                         return ret);
2138
2139         return 0;
2140 }
2141
2142 static int vega20_force_dpm_lowest(struct pp_hwmgr *hwmgr)
2143 {
2144         struct vega20_hwmgr *data =
2145                         (struct vega20_hwmgr *)(hwmgr->backend);
2146         uint32_t soft_level;
2147         int ret = 0;
2148
2149         soft_level = vega20_find_lowest_dpm_level(&(data->dpm_table.gfx_table));
2150
2151         data->dpm_table.gfx_table.dpm_state.soft_min_level =
2152                 data->dpm_table.gfx_table.dpm_state.soft_max_level =
2153                 data->dpm_table.gfx_table.dpm_levels[soft_level].value;
2154
2155         soft_level = vega20_find_lowest_dpm_level(&(data->dpm_table.mem_table));
2156
2157         data->dpm_table.mem_table.dpm_state.soft_min_level =
2158                 data->dpm_table.mem_table.dpm_state.soft_max_level =
2159                 data->dpm_table.mem_table.dpm_levels[soft_level].value;
2160
2161         ret = vega20_upload_dpm_min_level(hwmgr);
2162         PP_ASSERT_WITH_CODE(!ret,
2163                         "Failed to upload boot level to highest!",
2164                         return ret);
2165
2166         ret = vega20_upload_dpm_max_level(hwmgr);
2167         PP_ASSERT_WITH_CODE(!ret,
2168                         "Failed to upload dpm max level to highest!",
2169                         return ret);
2170
2171         return 0;
2172
2173 }
2174
2175 static int vega20_unforce_dpm_levels(struct pp_hwmgr *hwmgr)
2176 {
2177         int ret = 0;
2178
2179         ret = vega20_upload_dpm_min_level(hwmgr);
2180         PP_ASSERT_WITH_CODE(!ret,
2181                         "Failed to upload DPM Bootup Levels!",
2182                         return ret);
2183
2184         ret = vega20_upload_dpm_max_level(hwmgr);
2185         PP_ASSERT_WITH_CODE(!ret,
2186                         "Failed to upload DPM Max Levels!",
2187                         return ret);
2188
2189         return 0;
2190 }
2191
2192 static int vega20_get_profiling_clk_mask(struct pp_hwmgr *hwmgr, enum amd_dpm_forced_level level,
2193                                 uint32_t *sclk_mask, uint32_t *mclk_mask, uint32_t *soc_mask)
2194 {
2195         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2196         struct vega20_single_dpm_table *gfx_dpm_table = &(data->dpm_table.gfx_table);
2197         struct vega20_single_dpm_table *mem_dpm_table = &(data->dpm_table.mem_table);
2198         struct vega20_single_dpm_table *soc_dpm_table = &(data->dpm_table.soc_table);
2199
2200         *sclk_mask = 0;
2201         *mclk_mask = 0;
2202         *soc_mask  = 0;
2203
2204         if (gfx_dpm_table->count > VEGA20_UMD_PSTATE_GFXCLK_LEVEL &&
2205             mem_dpm_table->count > VEGA20_UMD_PSTATE_MCLK_LEVEL &&
2206             soc_dpm_table->count > VEGA20_UMD_PSTATE_SOCCLK_LEVEL) {
2207                 *sclk_mask = VEGA20_UMD_PSTATE_GFXCLK_LEVEL;
2208                 *mclk_mask = VEGA20_UMD_PSTATE_MCLK_LEVEL;
2209                 *soc_mask  = VEGA20_UMD_PSTATE_SOCCLK_LEVEL;
2210         }
2211
2212         if (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) {
2213                 *sclk_mask = 0;
2214         } else if (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) {
2215                 *mclk_mask = 0;
2216         } else if (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) {
2217                 *sclk_mask = gfx_dpm_table->count - 1;
2218                 *mclk_mask = mem_dpm_table->count - 1;
2219                 *soc_mask  = soc_dpm_table->count - 1;
2220         }
2221
2222         return 0;
2223 }
2224
2225 static int vega20_force_clock_level(struct pp_hwmgr *hwmgr,
2226                 enum pp_clock_type type, uint32_t mask)
2227 {
2228         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2229         uint32_t soft_min_level, soft_max_level;
2230         int ret = 0;
2231
2232         switch (type) {
2233         case PP_SCLK:
2234                 soft_min_level = mask ? (ffs(mask) - 1) : 0;
2235                 soft_max_level = mask ? (fls(mask) - 1) : 0;
2236
2237                 if (soft_max_level >= data->dpm_table.gfx_table.count) {
2238                         pr_err("Clock level specified %d is over max allowed %d\n",
2239                                         soft_max_level,
2240                                         data->dpm_table.gfx_table.count - 1);
2241                         return -EINVAL;
2242                 }
2243
2244                 data->dpm_table.gfx_table.dpm_state.soft_min_level =
2245                         data->dpm_table.gfx_table.dpm_levels[soft_min_level].value;
2246                 data->dpm_table.gfx_table.dpm_state.soft_max_level =
2247                         data->dpm_table.gfx_table.dpm_levels[soft_max_level].value;
2248
2249                 ret = vega20_upload_dpm_min_level(hwmgr);
2250                 PP_ASSERT_WITH_CODE(!ret,
2251                         "Failed to upload boot level to lowest!",
2252                         return ret);
2253
2254                 ret = vega20_upload_dpm_max_level(hwmgr);
2255                 PP_ASSERT_WITH_CODE(!ret,
2256                         "Failed to upload dpm max level to highest!",
2257                         return ret);
2258                 break;
2259
2260         case PP_MCLK:
2261                 soft_min_level = mask ? (ffs(mask) - 1) : 0;
2262                 soft_max_level = mask ? (fls(mask) - 1) : 0;
2263
2264                 if (soft_max_level >= data->dpm_table.mem_table.count) {
2265                         pr_err("Clock level specified %d is over max allowed %d\n",
2266                                         soft_max_level,
2267                                         data->dpm_table.mem_table.count - 1);
2268                         return -EINVAL;
2269                 }
2270
2271                 data->dpm_table.mem_table.dpm_state.soft_min_level =
2272                         data->dpm_table.mem_table.dpm_levels[soft_min_level].value;
2273                 data->dpm_table.mem_table.dpm_state.soft_max_level =
2274                         data->dpm_table.mem_table.dpm_levels[soft_max_level].value;
2275
2276                 ret = vega20_upload_dpm_min_level(hwmgr);
2277                 PP_ASSERT_WITH_CODE(!ret,
2278                         "Failed to upload boot level to lowest!",
2279                         return ret);
2280
2281                 ret = vega20_upload_dpm_max_level(hwmgr);
2282                 PP_ASSERT_WITH_CODE(!ret,
2283                         "Failed to upload dpm max level to highest!",
2284                         return ret);
2285
2286                 break;
2287
2288         case PP_PCIE:
2289                 soft_min_level = mask ? (ffs(mask) - 1) : 0;
2290                 soft_max_level = mask ? (fls(mask) - 1) : 0;
2291                 if (soft_min_level >= NUM_LINK_LEVELS ||
2292                     soft_max_level >= NUM_LINK_LEVELS)
2293                         return -EINVAL;
2294
2295                 ret = smum_send_msg_to_smc_with_parameter(hwmgr,
2296                         PPSMC_MSG_SetMinLinkDpmByIndex, soft_min_level);
2297                 PP_ASSERT_WITH_CODE(!ret,
2298                         "Failed to set min link dpm level!",
2299                         return ret);
2300
2301                 break;
2302
2303         default:
2304                 break;
2305         }
2306
2307         return 0;
2308 }
2309
2310 static int vega20_dpm_force_dpm_level(struct pp_hwmgr *hwmgr,
2311                                 enum amd_dpm_forced_level level)
2312 {
2313         int ret = 0;
2314         uint32_t sclk_mask, mclk_mask, soc_mask;
2315
2316         switch (level) {
2317         case AMD_DPM_FORCED_LEVEL_HIGH:
2318                 ret = vega20_force_dpm_highest(hwmgr);
2319                 break;
2320
2321         case AMD_DPM_FORCED_LEVEL_LOW:
2322                 ret = vega20_force_dpm_lowest(hwmgr);
2323                 break;
2324
2325         case AMD_DPM_FORCED_LEVEL_AUTO:
2326                 ret = vega20_unforce_dpm_levels(hwmgr);
2327                 break;
2328
2329         case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
2330         case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
2331         case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
2332         case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
2333                 ret = vega20_get_profiling_clk_mask(hwmgr, level, &sclk_mask, &mclk_mask, &soc_mask);
2334                 if (ret)
2335                         return ret;
2336                 vega20_force_clock_level(hwmgr, PP_SCLK, 1 << sclk_mask);
2337                 vega20_force_clock_level(hwmgr, PP_MCLK, 1 << mclk_mask);
2338                 break;
2339
2340         case AMD_DPM_FORCED_LEVEL_MANUAL:
2341         case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
2342         default:
2343                 break;
2344         }
2345
2346         return ret;
2347 }
2348
2349 static uint32_t vega20_get_fan_control_mode(struct pp_hwmgr *hwmgr)
2350 {
2351         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2352
2353         if (data->smu_features[GNLD_FAN_CONTROL].enabled == false)
2354                 return AMD_FAN_CTRL_MANUAL;
2355         else
2356                 return AMD_FAN_CTRL_AUTO;
2357 }
2358
2359 static void vega20_set_fan_control_mode(struct pp_hwmgr *hwmgr, uint32_t mode)
2360 {
2361         switch (mode) {
2362         case AMD_FAN_CTRL_NONE:
2363                 vega20_fan_ctrl_set_fan_speed_percent(hwmgr, 100);
2364                 break;
2365         case AMD_FAN_CTRL_MANUAL:
2366                 if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl))
2367                         vega20_fan_ctrl_stop_smc_fan_control(hwmgr);
2368                 break;
2369         case AMD_FAN_CTRL_AUTO:
2370                 if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl))
2371                         vega20_fan_ctrl_start_smc_fan_control(hwmgr);
2372                 break;
2373         default:
2374                 break;
2375         }
2376 }
2377
2378 static int vega20_get_dal_power_level(struct pp_hwmgr *hwmgr,
2379                 struct amd_pp_simple_clock_info *info)
2380 {
2381 #if 0
2382         struct phm_ppt_v2_information *table_info =
2383                         (struct phm_ppt_v2_information *)hwmgr->pptable;
2384         struct phm_clock_and_voltage_limits *max_limits =
2385                         &table_info->max_clock_voltage_on_ac;
2386
2387         info->engine_max_clock = max_limits->sclk;
2388         info->memory_max_clock = max_limits->mclk;
2389 #endif
2390         return 0;
2391 }
2392
2393
2394 static int vega20_get_sclks(struct pp_hwmgr *hwmgr,
2395                 struct pp_clock_levels_with_latency *clocks)
2396 {
2397         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2398         struct vega20_single_dpm_table *dpm_table = &(data->dpm_table.gfx_table);
2399         int i, count;
2400
2401         PP_ASSERT_WITH_CODE(data->smu_features[GNLD_DPM_GFXCLK].enabled,
2402                 "[GetSclks]: gfxclk dpm not enabled!\n",
2403                 return -EPERM);
2404
2405         count = (dpm_table->count > MAX_NUM_CLOCKS) ? MAX_NUM_CLOCKS : dpm_table->count;
2406         clocks->num_levels = count;
2407
2408         for (i = 0; i < count; i++) {
2409                 clocks->data[i].clocks_in_khz =
2410                         dpm_table->dpm_levels[i].value * 1000;
2411                 clocks->data[i].latency_in_us = 0;
2412         }
2413
2414         return 0;
2415 }
2416
2417 static uint32_t vega20_get_mem_latency(struct pp_hwmgr *hwmgr,
2418                 uint32_t clock)
2419 {
2420         return 25;
2421 }
2422
2423 static int vega20_get_memclocks(struct pp_hwmgr *hwmgr,
2424                 struct pp_clock_levels_with_latency *clocks)
2425 {
2426         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2427         struct vega20_single_dpm_table *dpm_table = &(data->dpm_table.mem_table);
2428         int i, count;
2429
2430         PP_ASSERT_WITH_CODE(data->smu_features[GNLD_DPM_UCLK].enabled,
2431                 "[GetMclks]: uclk dpm not enabled!\n",
2432                 return -EPERM);
2433
2434         count = (dpm_table->count > MAX_NUM_CLOCKS) ? MAX_NUM_CLOCKS : dpm_table->count;
2435         clocks->num_levels = data->mclk_latency_table.count = count;
2436
2437         for (i = 0; i < count; i++) {
2438                 clocks->data[i].clocks_in_khz =
2439                         data->mclk_latency_table.entries[i].frequency =
2440                         dpm_table->dpm_levels[i].value * 1000;
2441                 clocks->data[i].latency_in_us =
2442                         data->mclk_latency_table.entries[i].latency =
2443                         vega20_get_mem_latency(hwmgr, dpm_table->dpm_levels[i].value);
2444         }
2445
2446         return 0;
2447 }
2448
2449 static int vega20_get_dcefclocks(struct pp_hwmgr *hwmgr,
2450                 struct pp_clock_levels_with_latency *clocks)
2451 {
2452         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2453         struct vega20_single_dpm_table *dpm_table = &(data->dpm_table.dcef_table);
2454         int i, count;
2455
2456         PP_ASSERT_WITH_CODE(data->smu_features[GNLD_DPM_DCEFCLK].enabled,
2457                 "[GetDcfclocks]: dcefclk dpm not enabled!\n",
2458                 return -EPERM);
2459
2460         count = (dpm_table->count > MAX_NUM_CLOCKS) ? MAX_NUM_CLOCKS : dpm_table->count;
2461         clocks->num_levels = count;
2462
2463         for (i = 0; i < count; i++) {
2464                 clocks->data[i].clocks_in_khz =
2465                         dpm_table->dpm_levels[i].value * 1000;
2466                 clocks->data[i].latency_in_us = 0;
2467         }
2468
2469         return 0;
2470 }
2471
2472 static int vega20_get_socclocks(struct pp_hwmgr *hwmgr,
2473                 struct pp_clock_levels_with_latency *clocks)
2474 {
2475         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2476         struct vega20_single_dpm_table *dpm_table = &(data->dpm_table.soc_table);
2477         int i, count;
2478
2479         PP_ASSERT_WITH_CODE(data->smu_features[GNLD_DPM_SOCCLK].enabled,
2480                 "[GetSocclks]: socclk dpm not enabled!\n",
2481                 return -EPERM);
2482
2483         count = (dpm_table->count > MAX_NUM_CLOCKS) ? MAX_NUM_CLOCKS : dpm_table->count;
2484         clocks->num_levels = count;
2485
2486         for (i = 0; i < count; i++) {
2487                 clocks->data[i].clocks_in_khz =
2488                         dpm_table->dpm_levels[i].value * 1000;
2489                 clocks->data[i].latency_in_us = 0;
2490         }
2491
2492         return 0;
2493
2494 }
2495
2496 static int vega20_get_clock_by_type_with_latency(struct pp_hwmgr *hwmgr,
2497                 enum amd_pp_clock_type type,
2498                 struct pp_clock_levels_with_latency *clocks)
2499 {
2500         int ret;
2501
2502         switch (type) {
2503         case amd_pp_sys_clock:
2504                 ret = vega20_get_sclks(hwmgr, clocks);
2505                 break;
2506         case amd_pp_mem_clock:
2507                 ret = vega20_get_memclocks(hwmgr, clocks);
2508                 break;
2509         case amd_pp_dcef_clock:
2510                 ret = vega20_get_dcefclocks(hwmgr, clocks);
2511                 break;
2512         case amd_pp_soc_clock:
2513                 ret = vega20_get_socclocks(hwmgr, clocks);
2514                 break;
2515         default:
2516                 return -EINVAL;
2517         }
2518
2519         return ret;
2520 }
2521
2522 static int vega20_get_clock_by_type_with_voltage(struct pp_hwmgr *hwmgr,
2523                 enum amd_pp_clock_type type,
2524                 struct pp_clock_levels_with_voltage *clocks)
2525 {
2526         clocks->num_levels = 0;
2527
2528         return 0;
2529 }
2530
2531 static int vega20_set_watermarks_for_clocks_ranges(struct pp_hwmgr *hwmgr,
2532                                                    void *clock_ranges)
2533 {
2534         struct vega20_hwmgr *data = (struct vega20_hwmgr *)(hwmgr->backend);
2535         Watermarks_t *table = &(data->smc_state_table.water_marks_table);
2536         struct dm_pp_wm_sets_with_clock_ranges_soc15 *wm_with_clock_ranges = clock_ranges;
2537
2538         if (!data->registry_data.disable_water_mark &&
2539             data->smu_features[GNLD_DPM_DCEFCLK].supported &&
2540             data->smu_features[GNLD_DPM_SOCCLK].supported) {
2541                 smu_set_watermarks_for_clocks_ranges(table, wm_with_clock_ranges);
2542                 data->water_marks_bitmap |= WaterMarksExist;
2543                 data->water_marks_bitmap &= ~WaterMarksLoaded;
2544         }
2545
2546         return 0;
2547 }
2548
2549 static int vega20_odn_edit_dpm_table(struct pp_hwmgr *hwmgr,
2550                                         enum PP_OD_DPM_TABLE_COMMAND type,
2551                                         long *input, uint32_t size)
2552 {
2553         struct vega20_hwmgr *data =
2554                         (struct vega20_hwmgr *)(hwmgr->backend);
2555         struct vega20_od8_single_setting *od8_settings =
2556                         data->od8_settings.od8_settings_array;
2557         OverDriveTable_t *od_table =
2558                         &(data->smc_state_table.overdrive_table);
2559         struct pp_clock_levels_with_latency clocks;
2560         int32_t input_index, input_clk, input_vol, i;
2561         int od8_id;
2562         int ret;
2563
2564         PP_ASSERT_WITH_CODE(input, "NULL user input for clock and voltage",
2565                                 return -EINVAL);
2566
2567         switch (type) {
2568         case PP_OD_EDIT_SCLK_VDDC_TABLE:
2569                 if (!(od8_settings[OD8_SETTING_GFXCLK_FMIN].feature_id &&
2570                       od8_settings[OD8_SETTING_GFXCLK_FMAX].feature_id)) {
2571                         pr_info("Sclk min/max frequency overdrive not supported\n");
2572                         return -EOPNOTSUPP;
2573                 }
2574
2575                 for (i = 0; i < size; i += 2) {
2576                         if (i + 2 > size) {
2577                                 pr_info("invalid number of input parameters %d\n",
2578                                         size);
2579                                 return -EINVAL;
2580                         }
2581
2582                         input_index = input[i];
2583                         input_clk = input[i + 1];
2584
2585                         if (input_index != 0 && input_index != 1) {
2586                                 pr_info("Invalid index %d\n", input_index);
2587                                 pr_info("Support min/max sclk frequency setting only which index by 0/1\n");
2588                                 return -EINVAL;
2589                         }
2590
2591                         if (input_clk < od8_settings[OD8_SETTING_GFXCLK_FMIN].min_value ||
2592                             input_clk > od8_settings[OD8_SETTING_GFXCLK_FMAX].max_value) {
2593                                 pr_info("clock freq %d is not within allowed range [%d - %d]\n",
2594                                         input_clk,
2595                                         od8_settings[OD8_SETTING_GFXCLK_FMIN].min_value,
2596                                         od8_settings[OD8_SETTING_GFXCLK_FMAX].max_value);
2597                                 return -EINVAL;
2598                         }
2599
2600                         if ((input_index == 0 && od_table->GfxclkFmin != input_clk) ||
2601                             (input_index == 1 && od_table->GfxclkFmax != input_clk))
2602                                 data->gfxclk_overdrive = true;
2603
2604                         if (input_index == 0)
2605                                 od_table->GfxclkFmin = input_clk;
2606                         else
2607                                 od_table->GfxclkFmax = input_clk;
2608                 }
2609
2610                 break;
2611
2612         case PP_OD_EDIT_MCLK_VDDC_TABLE:
2613                 if (!od8_settings[OD8_SETTING_UCLK_FMAX].feature_id) {
2614                         pr_info("Mclk max frequency overdrive not supported\n");
2615                         return -EOPNOTSUPP;
2616                 }
2617
2618                 ret = vega20_get_memclocks(hwmgr, &clocks);
2619                 PP_ASSERT_WITH_CODE(!ret,
2620                                 "Attempt to get memory clk levels failed!",
2621                                 return ret);
2622
2623                 for (i = 0; i < size; i += 2) {
2624                         if (i + 2 > size) {
2625                                 pr_info("invalid number of input parameters %d\n",
2626                                         size);
2627                                 return -EINVAL;
2628                         }
2629
2630                         input_index = input[i];
2631                         input_clk = input[i + 1];
2632
2633                         if (input_index != 1) {
2634                                 pr_info("Invalid index %d\n", input_index);
2635                                 pr_info("Support max Mclk frequency setting only which index by 1\n");
2636                                 return -EINVAL;
2637                         }
2638
2639                         if (input_clk < clocks.data[0].clocks_in_khz / 1000 ||
2640                             input_clk > od8_settings[OD8_SETTING_UCLK_FMAX].max_value) {
2641                                 pr_info("clock freq %d is not within allowed range [%d - %d]\n",
2642                                         input_clk,
2643                                         clocks.data[0].clocks_in_khz / 1000,
2644                                         od8_settings[OD8_SETTING_UCLK_FMAX].max_value);
2645                                 return -EINVAL;
2646                         }
2647
2648                         if (input_index == 1 && od_table->UclkFmax != input_clk)
2649                                 data->memclk_overdrive = true;
2650
2651                         od_table->UclkFmax = input_clk;
2652                 }
2653
2654                 break;
2655
2656         case PP_OD_EDIT_VDDC_CURVE:
2657                 if (!(od8_settings[OD8_SETTING_GFXCLK_FREQ1].feature_id &&
2658                     od8_settings[OD8_SETTING_GFXCLK_FREQ2].feature_id &&
2659                     od8_settings[OD8_SETTING_GFXCLK_FREQ3].feature_id &&
2660                     od8_settings[OD8_SETTING_GFXCLK_VOLTAGE1].feature_id &&
2661                     od8_settings[OD8_SETTING_GFXCLK_VOLTAGE2].feature_id &&
2662                     od8_settings[OD8_SETTING_GFXCLK_VOLTAGE3].feature_id)) {
2663                         pr_info("Voltage curve calibrate not supported\n");
2664                         return -EOPNOTSUPP;
2665                 }
2666
2667                 for (i = 0; i < size; i += 3) {
2668                         if (i + 3 > size) {
2669                                 pr_info("invalid number of input parameters %d\n",
2670                                         size);
2671                                 return -EINVAL;
2672                         }
2673
2674                         input_index = input[i];
2675                         input_clk = input[i + 1];
2676                         input_vol = input[i + 2];
2677
2678                         if (input_index > 2) {
2679                                 pr_info("Setting for point %d is not supported\n",
2680                                                 input_index + 1);
2681                                 pr_info("Three supported points index by 0, 1, 2\n");
2682                                 return -EINVAL;
2683                         }
2684
2685                         od8_id = OD8_SETTING_GFXCLK_FREQ1 + 2 * input_index;
2686                         if (input_clk < od8_settings[od8_id].min_value ||
2687                             input_clk > od8_settings[od8_id].max_value) {
2688                                 pr_info("clock freq %d is not within allowed range [%d - %d]\n",
2689                                         input_clk,
2690                                         od8_settings[od8_id].min_value,
2691                                         od8_settings[od8_id].max_value);
2692                                 return -EINVAL;
2693                         }
2694
2695                         od8_id = OD8_SETTING_GFXCLK_VOLTAGE1 + 2 * input_index;
2696                         if (input_vol < od8_settings[od8_id].min_value ||
2697                             input_vol > od8_settings[od8_id].max_value) {
2698                                 pr_info("clock voltage %d is not within allowed range [%d - %d]\n",
2699                                         input_vol,
2700                                         od8_settings[od8_id].min_value,
2701                                         od8_settings[od8_id].max_value);
2702                                 return -EINVAL;
2703                         }
2704
2705                         switch (input_index) {
2706                         case 0:
2707                                 od_table->GfxclkFreq1 = input_clk;
2708                                 od_table->GfxclkVolt1 = input_vol * VOLTAGE_SCALE;
2709                                 break;
2710                         case 1:
2711                                 od_table->GfxclkFreq2 = input_clk;
2712                                 od_table->GfxclkVolt2 = input_vol * VOLTAGE_SCALE;
2713                                 break;
2714                         case 2:
2715                                 od_table->GfxclkFreq3 = input_clk;
2716                                 od_table->GfxclkVolt3 = input_vol * VOLTAGE_SCALE;
2717                                 break;
2718                         }
2719                 }
2720                 break;
2721
2722         case PP_OD_RESTORE_DEFAULT_TABLE:
2723                 data->gfxclk_overdrive = false;
2724                 data->memclk_overdrive = false;
2725
2726                 ret = smum_smc_table_manager(hwmgr,
2727                                              (uint8_t *)od_table,
2728                                              TABLE_OVERDRIVE, true);
2729                 PP_ASSERT_WITH_CODE(!ret,
2730                                 "Failed to export overdrive table!",
2731                                 return ret);
2732                 break;
2733
2734         case PP_OD_COMMIT_DPM_TABLE:
2735                 ret = smum_smc_table_manager(hwmgr,
2736                                              (uint8_t *)od_table,
2737                                              TABLE_OVERDRIVE, false);
2738                 PP_ASSERT_WITH_CODE(!ret,
2739                                 "Failed to import overdrive table!",
2740                                 return ret);
2741
2742                 /* retrieve updated gfxclk table */
2743                 if (data->gfxclk_overdrive) {
2744                         data->gfxclk_overdrive = false;
2745
2746                         ret = vega20_setup_gfxclk_dpm_table(hwmgr);
2747                         if (ret)
2748                                 return ret;
2749                 }
2750
2751                 /* retrieve updated memclk table */
2752                 if (data->memclk_overdrive) {
2753                         data->memclk_overdrive = false;
2754
2755                         ret = vega20_setup_memclk_dpm_table(hwmgr);
2756                         if (ret)
2757                                 return ret;
2758                 }
2759                 break;
2760
2761         default:
2762                 return -EINVAL;
2763         }
2764
2765         return 0;
2766 }
2767
2768 static int vega20_print_clock_levels(struct pp_hwmgr *hwmgr,
2769                 enum pp_clock_type type, char *buf)
2770 {
2771         struct vega20_hwmgr *data =
2772                         (struct vega20_hwmgr *)(hwmgr->backend);
2773         struct vega20_od8_single_setting *od8_settings =
2774                         data->od8_settings.od8_settings_array;
2775         OverDriveTable_t *od_table =
2776                         &(data->smc_state_table.overdrive_table);
2777         struct phm_ppt_v3_information *pptable_information =
2778                 (struct phm_ppt_v3_information *)hwmgr->pptable;
2779         PPTable_t *pptable = (PPTable_t *)pptable_information->smc_pptable;
2780         struct amdgpu_device *adev = hwmgr->adev;
2781         struct pp_clock_levels_with_latency clocks;
2782         int i, now, size = 0;
2783         int ret = 0;
2784         uint32_t gen_speed, lane_width;
2785
2786         switch (type) {
2787         case PP_SCLK:
2788                 ret = vega20_get_current_clk_freq(hwmgr, PPCLK_GFXCLK, &now);
2789                 PP_ASSERT_WITH_CODE(!ret,
2790                                 "Attempt to get current gfx clk Failed!",
2791                                 return ret);
2792
2793                 ret = vega20_get_sclks(hwmgr, &clocks);
2794                 PP_ASSERT_WITH_CODE(!ret,
2795                                 "Attempt to get gfx clk levels Failed!",
2796                                 return ret);
2797
2798                 for (i = 0; i < clocks.num_levels; i++)
2799                         size += sprintf(buf + size, "%d: %uMhz %s\n",
2800                                 i, clocks.data[i].clocks_in_khz / 1000,
2801                                 (clocks.data[i].clocks_in_khz == now * 10) ? "*" : "");
2802                 break;
2803
2804         case PP_MCLK:
2805                 ret = vega20_get_current_clk_freq(hwmgr, PPCLK_UCLK, &now);
2806                 PP_ASSERT_WITH_CODE(!ret,
2807                                 "Attempt to get current mclk freq Failed!",
2808                                 return ret);
2809
2810                 ret = vega20_get_memclocks(hwmgr, &clocks);
2811                 PP_ASSERT_WITH_CODE(!ret,
2812                                 "Attempt to get memory clk levels Failed!",
2813                                 return ret);
2814
2815                 for (i = 0; i < clocks.num_levels; i++)
2816                         size += sprintf(buf + size, "%d: %uMhz %s\n",
2817                                 i, clocks.data[i].clocks_in_khz / 1000,
2818                                 (clocks.data[i].clocks_in_khz == now * 10) ? "*" : "");