2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/gfp.h>
27 #include <linux/slab.h>
28 #include <linux/firmware.h>
29 #include "amd_shared.h"
30 #include "amd_powerplay.h"
31 #include "power_state.h"
34 #include "amdgpu_dpm_internal.h"
35 #include "amdgpu_display.h"
37 static const struct amd_pm_funcs pp_dpm_funcs;
39 static int amd_powerplay_create(struct amdgpu_device *adev)
41 struct pp_hwmgr *hwmgr;
46 hwmgr = kzalloc(sizeof(struct pp_hwmgr), GFP_KERNEL);
51 hwmgr->not_vf = !amdgpu_sriov_vf(adev);
52 hwmgr->device = amdgpu_cgs_create_device(adev);
53 mutex_init(&hwmgr->msg_lock);
54 hwmgr->chip_family = adev->family;
55 hwmgr->chip_id = adev->asic_type;
56 hwmgr->feature_mask = adev->pm.pp_feature;
57 hwmgr->display_config = &adev->pm.pm_display_cfg;
58 adev->powerplay.pp_handle = hwmgr;
59 adev->powerplay.pp_funcs = &pp_dpm_funcs;
64 static void amd_powerplay_destroy(struct amdgpu_device *adev)
66 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
68 mutex_destroy(&hwmgr->msg_lock);
70 kfree(hwmgr->hardcode_pp_table);
71 hwmgr->hardcode_pp_table = NULL;
77 static int pp_early_init(void *handle)
80 struct amdgpu_device *adev = handle;
82 ret = amd_powerplay_create(adev);
87 ret = hwmgr_early_init(adev->powerplay.pp_handle);
94 static int pp_sw_init(void *handle)
96 struct amdgpu_device *adev = handle;
97 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
100 ret = hwmgr_sw_init(hwmgr);
102 pr_debug("powerplay sw init %s\n", ret ? "failed" : "successfully");
107 static int pp_sw_fini(void *handle)
109 struct amdgpu_device *adev = handle;
110 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
112 hwmgr_sw_fini(hwmgr);
114 amdgpu_ucode_release(&adev->pm.fw);
119 static int pp_hw_init(void *handle)
122 struct amdgpu_device *adev = handle;
123 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
125 ret = hwmgr_hw_init(hwmgr);
128 pr_err("powerplay hw init failed\n");
133 static int pp_hw_fini(void *handle)
135 struct amdgpu_device *adev = handle;
136 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
138 hwmgr_hw_fini(hwmgr);
143 static void pp_reserve_vram_for_smu(struct amdgpu_device *adev)
146 void *cpu_ptr = NULL;
148 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
150 if (amdgpu_bo_create_kernel(adev, adev->pm.smu_prv_buffer_size,
151 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
152 &adev->pm.smu_prv_buffer,
155 DRM_ERROR("amdgpu: failed to create smu prv buffer\n");
159 if (hwmgr->hwmgr_func->notify_cac_buffer_info)
160 r = hwmgr->hwmgr_func->notify_cac_buffer_info(hwmgr,
161 lower_32_bits((unsigned long)cpu_ptr),
162 upper_32_bits((unsigned long)cpu_ptr),
163 lower_32_bits(gpu_addr),
164 upper_32_bits(gpu_addr),
165 adev->pm.smu_prv_buffer_size);
168 amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
169 adev->pm.smu_prv_buffer = NULL;
170 DRM_ERROR("amdgpu: failed to notify SMU buffer address\n");
174 static int pp_late_init(void *handle)
176 struct amdgpu_device *adev = handle;
177 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
179 if (hwmgr && hwmgr->pm_en)
180 hwmgr_handle_task(hwmgr,
181 AMD_PP_TASK_COMPLETE_INIT, NULL);
182 if (adev->pm.smu_prv_buffer_size != 0)
183 pp_reserve_vram_for_smu(adev);
188 static void pp_late_fini(void *handle)
190 struct amdgpu_device *adev = handle;
192 if (adev->pm.smu_prv_buffer)
193 amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
194 amd_powerplay_destroy(adev);
198 static bool pp_is_idle(void *handle)
203 static int pp_wait_for_idle(void *handle)
208 static int pp_sw_reset(void *handle)
213 static int pp_set_powergating_state(void *handle,
214 enum amd_powergating_state state)
219 static int pp_suspend(void *handle)
221 struct amdgpu_device *adev = handle;
222 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
224 return hwmgr_suspend(hwmgr);
227 static int pp_resume(void *handle)
229 struct amdgpu_device *adev = handle;
230 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
232 return hwmgr_resume(hwmgr);
235 static int pp_set_clockgating_state(void *handle,
236 enum amd_clockgating_state state)
241 static const struct amd_ip_funcs pp_ip_funcs = {
243 .early_init = pp_early_init,
244 .late_init = pp_late_init,
245 .sw_init = pp_sw_init,
246 .sw_fini = pp_sw_fini,
247 .hw_init = pp_hw_init,
248 .hw_fini = pp_hw_fini,
249 .late_fini = pp_late_fini,
250 .suspend = pp_suspend,
252 .is_idle = pp_is_idle,
253 .wait_for_idle = pp_wait_for_idle,
254 .soft_reset = pp_sw_reset,
255 .set_clockgating_state = pp_set_clockgating_state,
256 .set_powergating_state = pp_set_powergating_state,
259 const struct amdgpu_ip_block_version pp_smu_ip_block =
261 .type = AMD_IP_BLOCK_TYPE_SMC,
265 .funcs = &pp_ip_funcs,
268 /* This interface only be supported On Vi,
269 * because only smu7/8 can help to load gfx/sdma fw,
270 * smu need to be enabled before load other ip's fw.
271 * so call start smu to load smu7 fw and other ip's fw
273 static int pp_dpm_load_fw(void *handle)
275 struct pp_hwmgr *hwmgr = handle;
277 if (!hwmgr || !hwmgr->smumgr_funcs || !hwmgr->smumgr_funcs->start_smu)
280 if (hwmgr->smumgr_funcs->start_smu(hwmgr)) {
281 pr_err("fw load failed\n");
288 static int pp_dpm_fw_loading_complete(void *handle)
293 static int pp_set_clockgating_by_smu(void *handle, uint32_t msg_id)
295 struct pp_hwmgr *hwmgr = handle;
297 if (!hwmgr || !hwmgr->pm_en)
300 if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
301 pr_info_ratelimited("%s was not implemented.\n", __func__);
305 return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
308 static void pp_dpm_en_umd_pstate(struct pp_hwmgr *hwmgr,
309 enum amd_dpm_forced_level *level)
311 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
312 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
313 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
314 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
316 if (!(hwmgr->dpm_level & profile_mode_mask)) {
317 /* enter umd pstate, save current level, disable gfx cg*/
318 if (*level & profile_mode_mask) {
319 hwmgr->saved_dpm_level = hwmgr->dpm_level;
320 hwmgr->en_umd_pstate = true;
323 /* exit umd pstate, restore level, enable gfx cg*/
324 if (!(*level & profile_mode_mask)) {
325 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
326 *level = hwmgr->saved_dpm_level;
327 hwmgr->en_umd_pstate = false;
332 static int pp_dpm_force_performance_level(void *handle,
333 enum amd_dpm_forced_level level)
335 struct pp_hwmgr *hwmgr = handle;
337 if (!hwmgr || !hwmgr->pm_en)
340 if (level == hwmgr->dpm_level)
343 pp_dpm_en_umd_pstate(hwmgr, &level);
344 hwmgr->request_dpm_level = level;
345 hwmgr_handle_task(hwmgr, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
350 static enum amd_dpm_forced_level pp_dpm_get_performance_level(
353 struct pp_hwmgr *hwmgr = handle;
355 if (!hwmgr || !hwmgr->pm_en)
358 return hwmgr->dpm_level;
361 static uint32_t pp_dpm_get_sclk(void *handle, bool low)
363 struct pp_hwmgr *hwmgr = handle;
365 if (!hwmgr || !hwmgr->pm_en)
368 if (hwmgr->hwmgr_func->get_sclk == NULL) {
369 pr_info_ratelimited("%s was not implemented.\n", __func__);
372 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
375 static uint32_t pp_dpm_get_mclk(void *handle, bool low)
377 struct pp_hwmgr *hwmgr = handle;
379 if (!hwmgr || !hwmgr->pm_en)
382 if (hwmgr->hwmgr_func->get_mclk == NULL) {
383 pr_info_ratelimited("%s was not implemented.\n", __func__);
386 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
389 static void pp_dpm_powergate_vce(void *handle, bool gate)
391 struct pp_hwmgr *hwmgr = handle;
393 if (!hwmgr || !hwmgr->pm_en)
396 if (hwmgr->hwmgr_func->powergate_vce == NULL) {
397 pr_info_ratelimited("%s was not implemented.\n", __func__);
400 hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
403 static void pp_dpm_powergate_uvd(void *handle, bool gate)
405 struct pp_hwmgr *hwmgr = handle;
407 if (!hwmgr || !hwmgr->pm_en)
410 if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
411 pr_info_ratelimited("%s was not implemented.\n", __func__);
414 hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
417 static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
418 enum amd_pm_state_type *user_state)
420 struct pp_hwmgr *hwmgr = handle;
422 if (!hwmgr || !hwmgr->pm_en)
425 return hwmgr_handle_task(hwmgr, task_id, user_state);
428 static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
430 struct pp_hwmgr *hwmgr = handle;
431 struct pp_power_state *state;
432 enum amd_pm_state_type pm_type;
434 if (!hwmgr || !hwmgr->pm_en || !hwmgr->current_ps)
437 state = hwmgr->current_ps;
439 switch (state->classification.ui_label) {
440 case PP_StateUILabel_Battery:
441 pm_type = POWER_STATE_TYPE_BATTERY;
443 case PP_StateUILabel_Balanced:
444 pm_type = POWER_STATE_TYPE_BALANCED;
446 case PP_StateUILabel_Performance:
447 pm_type = POWER_STATE_TYPE_PERFORMANCE;
450 if (state->classification.flags & PP_StateClassificationFlag_Boot)
451 pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
453 pm_type = POWER_STATE_TYPE_DEFAULT;
460 static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
462 struct pp_hwmgr *hwmgr = handle;
464 if (!hwmgr || !hwmgr->pm_en)
467 if (hwmgr->hwmgr_func->set_fan_control_mode == NULL)
473 hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
478 static int pp_dpm_get_fan_control_mode(void *handle, uint32_t *fan_mode)
480 struct pp_hwmgr *hwmgr = handle;
482 if (!hwmgr || !hwmgr->pm_en)
485 if (hwmgr->hwmgr_func->get_fan_control_mode == NULL)
491 *fan_mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
495 static int pp_dpm_set_fan_speed_pwm(void *handle, uint32_t speed)
497 struct pp_hwmgr *hwmgr = handle;
499 if (!hwmgr || !hwmgr->pm_en)
502 if (hwmgr->hwmgr_func->set_fan_speed_pwm == NULL)
505 if (speed == U32_MAX)
508 return hwmgr->hwmgr_func->set_fan_speed_pwm(hwmgr, speed);
511 static int pp_dpm_get_fan_speed_pwm(void *handle, uint32_t *speed)
513 struct pp_hwmgr *hwmgr = handle;
515 if (!hwmgr || !hwmgr->pm_en)
518 if (hwmgr->hwmgr_func->get_fan_speed_pwm == NULL)
524 return hwmgr->hwmgr_func->get_fan_speed_pwm(hwmgr, speed);
527 static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
529 struct pp_hwmgr *hwmgr = handle;
531 if (!hwmgr || !hwmgr->pm_en)
534 if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
540 return hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
543 static int pp_dpm_set_fan_speed_rpm(void *handle, uint32_t rpm)
545 struct pp_hwmgr *hwmgr = handle;
547 if (!hwmgr || !hwmgr->pm_en)
550 if (hwmgr->hwmgr_func->set_fan_speed_rpm == NULL)
556 return hwmgr->hwmgr_func->set_fan_speed_rpm(hwmgr, rpm);
559 static int pp_dpm_get_pp_num_states(void *handle,
560 struct pp_states_info *data)
562 struct pp_hwmgr *hwmgr = handle;
565 memset(data, 0, sizeof(*data));
567 if (!hwmgr || !hwmgr->pm_en ||!hwmgr->ps)
570 data->nums = hwmgr->num_ps;
572 for (i = 0; i < hwmgr->num_ps; i++) {
573 struct pp_power_state *state = (struct pp_power_state *)
574 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
575 switch (state->classification.ui_label) {
576 case PP_StateUILabel_Battery:
577 data->states[i] = POWER_STATE_TYPE_BATTERY;
579 case PP_StateUILabel_Balanced:
580 data->states[i] = POWER_STATE_TYPE_BALANCED;
582 case PP_StateUILabel_Performance:
583 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
586 if (state->classification.flags & PP_StateClassificationFlag_Boot)
587 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
589 data->states[i] = POWER_STATE_TYPE_DEFAULT;
595 static int pp_dpm_get_pp_table(void *handle, char **table)
597 struct pp_hwmgr *hwmgr = handle;
599 if (!hwmgr || !hwmgr->pm_en ||!hwmgr->soft_pp_table)
602 *table = (char *)hwmgr->soft_pp_table;
603 return hwmgr->soft_pp_table_size;
606 static int amd_powerplay_reset(void *handle)
608 struct pp_hwmgr *hwmgr = handle;
611 ret = hwmgr_hw_fini(hwmgr);
615 ret = hwmgr_hw_init(hwmgr);
619 return hwmgr_handle_task(hwmgr, AMD_PP_TASK_COMPLETE_INIT, NULL);
622 static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
624 struct pp_hwmgr *hwmgr = handle;
627 if (!hwmgr || !hwmgr->pm_en)
630 if (!hwmgr->hardcode_pp_table) {
631 hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
632 hwmgr->soft_pp_table_size,
634 if (!hwmgr->hardcode_pp_table)
638 memcpy(hwmgr->hardcode_pp_table, buf, size);
640 hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
642 ret = amd_powerplay_reset(handle);
646 if (hwmgr->hwmgr_func->avfs_control)
647 ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
652 static int pp_dpm_force_clock_level(void *handle,
653 enum pp_clock_type type, uint32_t mask)
655 struct pp_hwmgr *hwmgr = handle;
657 if (!hwmgr || !hwmgr->pm_en)
660 if (hwmgr->hwmgr_func->force_clock_level == NULL) {
661 pr_info_ratelimited("%s was not implemented.\n", __func__);
665 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
666 pr_debug("force clock level is for dpm manual mode only.\n");
670 return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
673 static int pp_dpm_emit_clock_levels(void *handle,
674 enum pp_clock_type type,
678 struct pp_hwmgr *hwmgr = handle;
680 if (!hwmgr || !hwmgr->pm_en)
683 if (!hwmgr->hwmgr_func->emit_clock_levels)
686 return hwmgr->hwmgr_func->emit_clock_levels(hwmgr, type, buf, offset);
689 static int pp_dpm_print_clock_levels(void *handle,
690 enum pp_clock_type type, char *buf)
692 struct pp_hwmgr *hwmgr = handle;
694 if (!hwmgr || !hwmgr->pm_en)
697 if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
698 pr_info_ratelimited("%s was not implemented.\n", __func__);
701 return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
704 static int pp_dpm_get_sclk_od(void *handle)
706 struct pp_hwmgr *hwmgr = handle;
708 if (!hwmgr || !hwmgr->pm_en)
711 if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
712 pr_info_ratelimited("%s was not implemented.\n", __func__);
715 return hwmgr->hwmgr_func->get_sclk_od(hwmgr);
718 static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
720 struct pp_hwmgr *hwmgr = handle;
722 if (!hwmgr || !hwmgr->pm_en)
725 if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
726 pr_info_ratelimited("%s was not implemented.\n", __func__);
730 return hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
733 static int pp_dpm_get_mclk_od(void *handle)
735 struct pp_hwmgr *hwmgr = handle;
737 if (!hwmgr || !hwmgr->pm_en)
740 if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
741 pr_info_ratelimited("%s was not implemented.\n", __func__);
744 return hwmgr->hwmgr_func->get_mclk_od(hwmgr);
747 static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
749 struct pp_hwmgr *hwmgr = handle;
751 if (!hwmgr || !hwmgr->pm_en)
754 if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
755 pr_info_ratelimited("%s was not implemented.\n", __func__);
758 return hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
761 static int pp_dpm_read_sensor(void *handle, int idx,
762 void *value, int *size)
764 struct pp_hwmgr *hwmgr = handle;
766 if (!hwmgr || !hwmgr->pm_en || !value)
770 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
771 *((uint32_t *)value) = hwmgr->pstate_sclk * 100;
773 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
774 *((uint32_t *)value) = hwmgr->pstate_mclk * 100;
776 case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:
777 *((uint32_t *)value) = hwmgr->pstate_sclk_peak * 100;
779 case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:
780 *((uint32_t *)value) = hwmgr->pstate_mclk_peak * 100;
782 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
783 *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMinRPM;
785 case AMDGPU_PP_SENSOR_MAX_FAN_RPM:
786 *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMaxRPM;
789 return hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
793 static struct amd_vce_state*
794 pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
796 struct pp_hwmgr *hwmgr = handle;
798 if (!hwmgr || !hwmgr->pm_en)
801 if (idx < hwmgr->num_vce_state_tables)
802 return &hwmgr->vce_states[idx];
806 static int pp_get_power_profile_mode(void *handle, char *buf)
808 struct pp_hwmgr *hwmgr = handle;
810 if (!hwmgr || !hwmgr->pm_en || !hwmgr->hwmgr_func->get_power_profile_mode)
815 return hwmgr->hwmgr_func->get_power_profile_mode(hwmgr, buf);
818 static int pp_set_power_profile_mode(void *handle, long *input, uint32_t size)
820 struct pp_hwmgr *hwmgr = handle;
822 if (!hwmgr || !hwmgr->pm_en || !hwmgr->hwmgr_func->set_power_profile_mode)
825 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
826 pr_debug("power profile setting is for manual dpm mode only.\n");
830 return hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, input, size);
833 static int pp_set_fine_grain_clk_vol(void *handle, uint32_t type, long *input, uint32_t size)
835 struct pp_hwmgr *hwmgr = handle;
837 if (!hwmgr || !hwmgr->pm_en)
840 if (hwmgr->hwmgr_func->set_fine_grain_clk_vol == NULL)
843 return hwmgr->hwmgr_func->set_fine_grain_clk_vol(hwmgr, type, input, size);
846 static int pp_odn_edit_dpm_table(void *handle, enum PP_OD_DPM_TABLE_COMMAND type,
847 long *input, uint32_t size)
849 struct pp_hwmgr *hwmgr = handle;
851 if (!hwmgr || !hwmgr->pm_en)
854 if (hwmgr->hwmgr_func->odn_edit_dpm_table == NULL) {
855 pr_info_ratelimited("%s was not implemented.\n", __func__);
859 return hwmgr->hwmgr_func->odn_edit_dpm_table(hwmgr, type, input, size);
862 static int pp_dpm_set_mp1_state(void *handle, enum pp_mp1_state mp1_state)
864 struct pp_hwmgr *hwmgr = handle;
872 if (hwmgr->hwmgr_func->set_mp1_state)
873 return hwmgr->hwmgr_func->set_mp1_state(hwmgr, mp1_state);
878 static int pp_dpm_switch_power_profile(void *handle,
879 enum PP_SMC_POWER_PROFILE type, bool en)
881 struct pp_hwmgr *hwmgr = handle;
885 if (!hwmgr || !hwmgr->pm_en)
888 if (hwmgr->hwmgr_func->set_power_profile_mode == NULL) {
889 pr_info_ratelimited("%s was not implemented.\n", __func__);
893 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
897 hwmgr->workload_mask &= ~(1 << hwmgr->workload_prority[type]);
898 index = fls(hwmgr->workload_mask);
899 index = index > 0 && index <= Workload_Policy_Max ? index - 1 : 0;
900 workload = hwmgr->workload_setting[index];
902 hwmgr->workload_mask |= (1 << hwmgr->workload_prority[type]);
903 index = fls(hwmgr->workload_mask);
904 index = index <= Workload_Policy_Max ? index - 1 : 0;
905 workload = hwmgr->workload_setting[index];
908 if (type == PP_SMC_POWER_PROFILE_COMPUTE &&
909 hwmgr->hwmgr_func->disable_power_features_for_compute_performance) {
910 if (hwmgr->hwmgr_func->disable_power_features_for_compute_performance(hwmgr, en))
914 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
915 hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, &workload, 0);
920 static int pp_set_power_limit(void *handle, uint32_t limit)
922 struct pp_hwmgr *hwmgr = handle;
923 uint32_t max_power_limit;
925 if (!hwmgr || !hwmgr->pm_en)
928 if (hwmgr->hwmgr_func->set_power_limit == NULL) {
929 pr_info_ratelimited("%s was not implemented.\n", __func__);
934 limit = hwmgr->default_power_limit;
936 max_power_limit = hwmgr->default_power_limit;
937 if (hwmgr->od_enabled) {
938 max_power_limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
939 max_power_limit /= 100;
942 if (limit > max_power_limit)
945 hwmgr->hwmgr_func->set_power_limit(hwmgr, limit);
946 hwmgr->power_limit = limit;
950 static int pp_get_power_limit(void *handle, uint32_t *limit,
951 enum pp_power_limit_level pp_limit_level,
952 enum pp_power_type power_type)
954 struct pp_hwmgr *hwmgr = handle;
957 if (!hwmgr || !hwmgr->pm_en ||!limit)
960 if (power_type != PP_PWR_TYPE_SUSTAINED)
963 switch (pp_limit_level) {
964 case PP_PWR_LIMIT_CURRENT:
965 *limit = hwmgr->power_limit;
967 case PP_PWR_LIMIT_DEFAULT:
968 *limit = hwmgr->default_power_limit;
970 case PP_PWR_LIMIT_MAX:
971 *limit = hwmgr->default_power_limit;
972 if (hwmgr->od_enabled) {
973 *limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
985 static int pp_display_configuration_change(void *handle,
986 const struct amd_pp_display_configuration *display_config)
988 struct pp_hwmgr *hwmgr = handle;
990 if (!hwmgr || !hwmgr->pm_en)
993 phm_store_dal_configuration_data(hwmgr, display_config);
997 static int pp_get_display_power_level(void *handle,
998 struct amd_pp_simple_clock_info *output)
1000 struct pp_hwmgr *hwmgr = handle;
1002 if (!hwmgr || !hwmgr->pm_en ||!output)
1005 return phm_get_dal_power_level(hwmgr, output);
1008 static int pp_get_current_clocks(void *handle,
1009 struct amd_pp_clock_info *clocks)
1011 struct amd_pp_simple_clock_info simple_clocks = { 0 };
1012 struct pp_clock_info hw_clocks;
1013 struct pp_hwmgr *hwmgr = handle;
1016 if (!hwmgr || !hwmgr->pm_en)
1019 phm_get_dal_power_level(hwmgr, &simple_clocks);
1021 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1022 PHM_PlatformCaps_PowerContainment))
1023 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1024 &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
1026 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1027 &hw_clocks, PHM_PerformanceLevelDesignation_Activity);
1030 pr_debug("Error in phm_get_clock_info \n");
1034 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1035 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1036 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1037 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1038 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1039 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1041 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1042 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1044 if (simple_clocks.level == 0)
1045 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1047 clocks->max_clocks_state = simple_clocks.level;
1049 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
1050 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1051 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1056 static int pp_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
1058 struct pp_hwmgr *hwmgr = handle;
1060 if (!hwmgr || !hwmgr->pm_en)
1066 return phm_get_clock_by_type(hwmgr, type, clocks);
1069 static int pp_get_clock_by_type_with_latency(void *handle,
1070 enum amd_pp_clock_type type,
1071 struct pp_clock_levels_with_latency *clocks)
1073 struct pp_hwmgr *hwmgr = handle;
1075 if (!hwmgr || !hwmgr->pm_en ||!clocks)
1078 return phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
1081 static int pp_get_clock_by_type_with_voltage(void *handle,
1082 enum amd_pp_clock_type type,
1083 struct pp_clock_levels_with_voltage *clocks)
1085 struct pp_hwmgr *hwmgr = handle;
1087 if (!hwmgr || !hwmgr->pm_en ||!clocks)
1090 return phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);
1093 static int pp_set_watermarks_for_clocks_ranges(void *handle,
1096 struct pp_hwmgr *hwmgr = handle;
1098 if (!hwmgr || !hwmgr->pm_en || !clock_ranges)
1101 return phm_set_watermarks_for_clocks_ranges(hwmgr,
1105 static int pp_display_clock_voltage_request(void *handle,
1106 struct pp_display_clock_request *clock)
1108 struct pp_hwmgr *hwmgr = handle;
1110 if (!hwmgr || !hwmgr->pm_en ||!clock)
1113 return phm_display_clock_voltage_request(hwmgr, clock);
1116 static int pp_get_display_mode_validation_clocks(void *handle,
1117 struct amd_pp_simple_clock_info *clocks)
1119 struct pp_hwmgr *hwmgr = handle;
1122 if (!hwmgr || !hwmgr->pm_en ||!clocks)
1125 clocks->level = PP_DAL_POWERLEVEL_7;
1127 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1128 ret = phm_get_max_high_clocks(hwmgr, clocks);
1133 static int pp_dpm_powergate_mmhub(void *handle)
1135 struct pp_hwmgr *hwmgr = handle;
1137 if (!hwmgr || !hwmgr->pm_en)
1140 if (hwmgr->hwmgr_func->powergate_mmhub == NULL) {
1141 pr_info_ratelimited("%s was not implemented.\n", __func__);
1145 return hwmgr->hwmgr_func->powergate_mmhub(hwmgr);
1148 static int pp_dpm_powergate_gfx(void *handle, bool gate)
1150 struct pp_hwmgr *hwmgr = handle;
1152 if (!hwmgr || !hwmgr->pm_en)
1155 if (hwmgr->hwmgr_func->powergate_gfx == NULL) {
1156 pr_info_ratelimited("%s was not implemented.\n", __func__);
1160 return hwmgr->hwmgr_func->powergate_gfx(hwmgr, gate);
1163 static void pp_dpm_powergate_acp(void *handle, bool gate)
1165 struct pp_hwmgr *hwmgr = handle;
1167 if (!hwmgr || !hwmgr->pm_en)
1170 if (hwmgr->hwmgr_func->powergate_acp == NULL) {
1171 pr_info_ratelimited("%s was not implemented.\n", __func__);
1175 hwmgr->hwmgr_func->powergate_acp(hwmgr, gate);
1178 static void pp_dpm_powergate_sdma(void *handle, bool gate)
1180 struct pp_hwmgr *hwmgr = handle;
1185 if (hwmgr->hwmgr_func->powergate_sdma == NULL) {
1186 pr_info_ratelimited("%s was not implemented.\n", __func__);
1190 hwmgr->hwmgr_func->powergate_sdma(hwmgr, gate);
1193 static int pp_set_powergating_by_smu(void *handle,
1194 uint32_t block_type, bool gate)
1198 switch (block_type) {
1199 case AMD_IP_BLOCK_TYPE_UVD:
1200 case AMD_IP_BLOCK_TYPE_VCN:
1201 pp_dpm_powergate_uvd(handle, gate);
1203 case AMD_IP_BLOCK_TYPE_VCE:
1204 pp_dpm_powergate_vce(handle, gate);
1206 case AMD_IP_BLOCK_TYPE_GMC:
1208 * For now, this is only used on PICASSO.
1209 * And only "gate" operation is supported.
1212 pp_dpm_powergate_mmhub(handle);
1214 case AMD_IP_BLOCK_TYPE_GFX:
1215 ret = pp_dpm_powergate_gfx(handle, gate);
1217 case AMD_IP_BLOCK_TYPE_ACP:
1218 pp_dpm_powergate_acp(handle, gate);
1220 case AMD_IP_BLOCK_TYPE_SDMA:
1221 pp_dpm_powergate_sdma(handle, gate);
1229 static int pp_notify_smu_enable_pwe(void *handle)
1231 struct pp_hwmgr *hwmgr = handle;
1233 if (!hwmgr || !hwmgr->pm_en)
1236 if (hwmgr->hwmgr_func->smus_notify_pwe == NULL) {
1237 pr_info_ratelimited("%s was not implemented.\n", __func__);
1241 hwmgr->hwmgr_func->smus_notify_pwe(hwmgr);
1246 static int pp_enable_mgpu_fan_boost(void *handle)
1248 struct pp_hwmgr *hwmgr = handle;
1253 if (!hwmgr->pm_en ||
1254 hwmgr->hwmgr_func->enable_mgpu_fan_boost == NULL)
1257 hwmgr->hwmgr_func->enable_mgpu_fan_boost(hwmgr);
1262 static int pp_set_min_deep_sleep_dcefclk(void *handle, uint32_t clock)
1264 struct pp_hwmgr *hwmgr = handle;
1266 if (!hwmgr || !hwmgr->pm_en)
1269 if (hwmgr->hwmgr_func->set_min_deep_sleep_dcefclk == NULL) {
1270 pr_debug("%s was not implemented.\n", __func__);
1274 hwmgr->hwmgr_func->set_min_deep_sleep_dcefclk(hwmgr, clock);
1279 static int pp_set_hard_min_dcefclk_by_freq(void *handle, uint32_t clock)
1281 struct pp_hwmgr *hwmgr = handle;
1283 if (!hwmgr || !hwmgr->pm_en)
1286 if (hwmgr->hwmgr_func->set_hard_min_dcefclk_by_freq == NULL) {
1287 pr_debug("%s was not implemented.\n", __func__);
1291 hwmgr->hwmgr_func->set_hard_min_dcefclk_by_freq(hwmgr, clock);
1296 static int pp_set_hard_min_fclk_by_freq(void *handle, uint32_t clock)
1298 struct pp_hwmgr *hwmgr = handle;
1300 if (!hwmgr || !hwmgr->pm_en)
1303 if (hwmgr->hwmgr_func->set_hard_min_fclk_by_freq == NULL) {
1304 pr_debug("%s was not implemented.\n", __func__);
1308 hwmgr->hwmgr_func->set_hard_min_fclk_by_freq(hwmgr, clock);
1313 static int pp_set_active_display_count(void *handle, uint32_t count)
1315 struct pp_hwmgr *hwmgr = handle;
1317 if (!hwmgr || !hwmgr->pm_en)
1320 return phm_set_active_display_count(hwmgr, count);
1323 static int pp_get_asic_baco_capability(void *handle, bool *cap)
1325 struct pp_hwmgr *hwmgr = handle;
1331 if (!(hwmgr->not_vf && amdgpu_dpm) ||
1332 !hwmgr->hwmgr_func->get_asic_baco_capability)
1335 hwmgr->hwmgr_func->get_asic_baco_capability(hwmgr, cap);
1340 static int pp_get_asic_baco_state(void *handle, int *state)
1342 struct pp_hwmgr *hwmgr = handle;
1347 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->get_asic_baco_state)
1350 hwmgr->hwmgr_func->get_asic_baco_state(hwmgr, (enum BACO_STATE *)state);
1355 static int pp_set_asic_baco_state(void *handle, int state)
1357 struct pp_hwmgr *hwmgr = handle;
1362 if (!(hwmgr->not_vf && amdgpu_dpm) ||
1363 !hwmgr->hwmgr_func->set_asic_baco_state)
1366 hwmgr->hwmgr_func->set_asic_baco_state(hwmgr, (enum BACO_STATE)state);
1371 static int pp_get_ppfeature_status(void *handle, char *buf)
1373 struct pp_hwmgr *hwmgr = handle;
1375 if (!hwmgr || !hwmgr->pm_en || !buf)
1378 if (hwmgr->hwmgr_func->get_ppfeature_status == NULL) {
1379 pr_info_ratelimited("%s was not implemented.\n", __func__);
1383 return hwmgr->hwmgr_func->get_ppfeature_status(hwmgr, buf);
1386 static int pp_set_ppfeature_status(void *handle, uint64_t ppfeature_masks)
1388 struct pp_hwmgr *hwmgr = handle;
1390 if (!hwmgr || !hwmgr->pm_en)
1393 if (hwmgr->hwmgr_func->set_ppfeature_status == NULL) {
1394 pr_info_ratelimited("%s was not implemented.\n", __func__);
1398 return hwmgr->hwmgr_func->set_ppfeature_status(hwmgr, ppfeature_masks);
1401 static int pp_asic_reset_mode_2(void *handle)
1403 struct pp_hwmgr *hwmgr = handle;
1405 if (!hwmgr || !hwmgr->pm_en)
1408 if (hwmgr->hwmgr_func->asic_reset == NULL) {
1409 pr_info_ratelimited("%s was not implemented.\n", __func__);
1413 return hwmgr->hwmgr_func->asic_reset(hwmgr, SMU_ASIC_RESET_MODE_2);
1416 static int pp_smu_i2c_bus_access(void *handle, bool acquire)
1418 struct pp_hwmgr *hwmgr = handle;
1420 if (!hwmgr || !hwmgr->pm_en)
1423 if (hwmgr->hwmgr_func->smu_i2c_bus_access == NULL) {
1424 pr_info_ratelimited("%s was not implemented.\n", __func__);
1428 return hwmgr->hwmgr_func->smu_i2c_bus_access(hwmgr, acquire);
1431 static int pp_set_df_cstate(void *handle, enum pp_df_cstate state)
1433 struct pp_hwmgr *hwmgr = handle;
1438 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->set_df_cstate)
1441 hwmgr->hwmgr_func->set_df_cstate(hwmgr, state);
1446 static int pp_set_xgmi_pstate(void *handle, uint32_t pstate)
1448 struct pp_hwmgr *hwmgr = handle;
1453 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->set_xgmi_pstate)
1456 hwmgr->hwmgr_func->set_xgmi_pstate(hwmgr, pstate);
1461 static ssize_t pp_get_gpu_metrics(void *handle, void **table)
1463 struct pp_hwmgr *hwmgr = handle;
1468 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->get_gpu_metrics)
1471 return hwmgr->hwmgr_func->get_gpu_metrics(hwmgr, table);
1474 static int pp_gfx_state_change_set(void *handle, uint32_t state)
1476 struct pp_hwmgr *hwmgr = handle;
1478 if (!hwmgr || !hwmgr->pm_en)
1481 if (hwmgr->hwmgr_func->gfx_state_change == NULL) {
1482 pr_info_ratelimited("%s was not implemented.\n", __func__);
1486 hwmgr->hwmgr_func->gfx_state_change(hwmgr, state);
1490 static int pp_get_prv_buffer_details(void *handle, void **addr, size_t *size)
1492 struct pp_hwmgr *hwmgr = handle;
1493 struct amdgpu_device *adev = hwmgr->adev;
1501 if (adev->pm.smu_prv_buffer) {
1502 err = amdgpu_bo_kmap(adev->pm.smu_prv_buffer, addr);
1505 *size = adev->pm.smu_prv_buffer_size;
1511 static void pp_pm_compute_clocks(void *handle)
1513 struct pp_hwmgr *hwmgr = handle;
1514 struct amdgpu_device *adev = hwmgr->adev;
1516 if (!adev->dc_enabled) {
1517 amdgpu_dpm_get_active_displays(adev);
1518 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
1519 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
1520 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
1521 /* we have issues with mclk switching with
1522 * refresh rates over 120 hz on the non-DC code.
1524 if (adev->pm.pm_display_cfg.vrefresh > 120)
1525 adev->pm.pm_display_cfg.min_vblank_time = 0;
1527 pp_display_configuration_change(handle,
1528 &adev->pm.pm_display_cfg);
1531 pp_dpm_dispatch_tasks(handle,
1532 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE,
1536 static const struct amd_pm_funcs pp_dpm_funcs = {
1537 .load_firmware = pp_dpm_load_fw,
1538 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
1539 .force_performance_level = pp_dpm_force_performance_level,
1540 .get_performance_level = pp_dpm_get_performance_level,
1541 .get_current_power_state = pp_dpm_get_current_power_state,
1542 .dispatch_tasks = pp_dpm_dispatch_tasks,
1543 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
1544 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
1545 .set_fan_speed_pwm = pp_dpm_set_fan_speed_pwm,
1546 .get_fan_speed_pwm = pp_dpm_get_fan_speed_pwm,
1547 .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
1548 .set_fan_speed_rpm = pp_dpm_set_fan_speed_rpm,
1549 .get_pp_num_states = pp_dpm_get_pp_num_states,
1550 .get_pp_table = pp_dpm_get_pp_table,
1551 .set_pp_table = pp_dpm_set_pp_table,
1552 .force_clock_level = pp_dpm_force_clock_level,
1553 .emit_clock_levels = pp_dpm_emit_clock_levels,
1554 .print_clock_levels = pp_dpm_print_clock_levels,
1555 .get_sclk_od = pp_dpm_get_sclk_od,
1556 .set_sclk_od = pp_dpm_set_sclk_od,
1557 .get_mclk_od = pp_dpm_get_mclk_od,
1558 .set_mclk_od = pp_dpm_set_mclk_od,
1559 .read_sensor = pp_dpm_read_sensor,
1560 .get_vce_clock_state = pp_dpm_get_vce_clock_state,
1561 .switch_power_profile = pp_dpm_switch_power_profile,
1562 .set_clockgating_by_smu = pp_set_clockgating_by_smu,
1563 .set_powergating_by_smu = pp_set_powergating_by_smu,
1564 .get_power_profile_mode = pp_get_power_profile_mode,
1565 .set_power_profile_mode = pp_set_power_profile_mode,
1566 .set_fine_grain_clk_vol = pp_set_fine_grain_clk_vol,
1567 .odn_edit_dpm_table = pp_odn_edit_dpm_table,
1568 .set_mp1_state = pp_dpm_set_mp1_state,
1569 .set_power_limit = pp_set_power_limit,
1570 .get_power_limit = pp_get_power_limit,
1572 .get_sclk = pp_dpm_get_sclk,
1573 .get_mclk = pp_dpm_get_mclk,
1574 .display_configuration_change = pp_display_configuration_change,
1575 .get_display_power_level = pp_get_display_power_level,
1576 .get_current_clocks = pp_get_current_clocks,
1577 .get_clock_by_type = pp_get_clock_by_type,
1578 .get_clock_by_type_with_latency = pp_get_clock_by_type_with_latency,
1579 .get_clock_by_type_with_voltage = pp_get_clock_by_type_with_voltage,
1580 .set_watermarks_for_clocks_ranges = pp_set_watermarks_for_clocks_ranges,
1581 .display_clock_voltage_request = pp_display_clock_voltage_request,
1582 .get_display_mode_validation_clocks = pp_get_display_mode_validation_clocks,
1583 .notify_smu_enable_pwe = pp_notify_smu_enable_pwe,
1584 .enable_mgpu_fan_boost = pp_enable_mgpu_fan_boost,
1585 .set_active_display_count = pp_set_active_display_count,
1586 .set_min_deep_sleep_dcefclk = pp_set_min_deep_sleep_dcefclk,
1587 .set_hard_min_dcefclk_by_freq = pp_set_hard_min_dcefclk_by_freq,
1588 .set_hard_min_fclk_by_freq = pp_set_hard_min_fclk_by_freq,
1589 .get_asic_baco_capability = pp_get_asic_baco_capability,
1590 .get_asic_baco_state = pp_get_asic_baco_state,
1591 .set_asic_baco_state = pp_set_asic_baco_state,
1592 .get_ppfeature_status = pp_get_ppfeature_status,
1593 .set_ppfeature_status = pp_set_ppfeature_status,
1594 .asic_reset_mode_2 = pp_asic_reset_mode_2,
1595 .smu_i2c_bus_access = pp_smu_i2c_bus_access,
1596 .set_df_cstate = pp_set_df_cstate,
1597 .set_xgmi_pstate = pp_set_xgmi_pstate,
1598 .get_gpu_metrics = pp_get_gpu_metrics,
1599 .gfx_state_change_set = pp_gfx_state_change_set,
1600 .get_smu_prv_buf_details = pp_get_prv_buffer_details,
1601 .pm_compute_clocks = pp_pm_compute_clocks,