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 "amd_shared.h"
29 #include "amd_powerplay.h"
30 #include "pp_instance.h"
31 #include "power_state.h"
32 #include "eventmanager.h"
35 static inline int pp_check(struct pp_instance *handle)
37 if (handle == NULL || handle->pp_valid != PP_VALID)
40 if (handle->smu_mgr == NULL || handle->smu_mgr->smumgr_funcs == NULL)
43 if (handle->pm_en == 0)
44 return PP_DPM_DISABLED;
46 if (handle->hwmgr == NULL || handle->hwmgr->hwmgr_func == NULL
47 || handle->eventmgr == NULL)
48 return PP_DPM_DISABLED;
53 static int pp_early_init(void *handle)
56 struct pp_instance *pp_handle = (struct pp_instance *)handle;
58 ret = smum_early_init(pp_handle);
62 if ((pp_handle->pm_en == 0)
63 || cgs_is_virtualization_enabled(pp_handle->device))
64 return PP_DPM_DISABLED;
66 ret = hwmgr_early_init(pp_handle);
69 return PP_DPM_DISABLED;
72 ret = eventmgr_early_init(pp_handle);
74 kfree(pp_handle->hwmgr);
75 pp_handle->hwmgr = NULL;
77 return PP_DPM_DISABLED;
83 static int pp_sw_init(void *handle)
85 struct pp_smumgr *smumgr;
87 struct pp_instance *pp_handle = (struct pp_instance *)handle;
89 ret = pp_check(pp_handle);
91 if (ret == 0 || ret == PP_DPM_DISABLED) {
92 smumgr = pp_handle->smu_mgr;
94 if (smumgr->smumgr_funcs->smu_init == NULL)
97 ret = smumgr->smumgr_funcs->smu_init(smumgr);
99 pr_info("amdgpu: powerplay sw initialized\n");
104 static int pp_sw_fini(void *handle)
106 struct pp_smumgr *smumgr;
108 struct pp_instance *pp_handle = (struct pp_instance *)handle;
110 ret = pp_check(pp_handle);
111 if (ret == 0 || ret == PP_DPM_DISABLED) {
112 smumgr = pp_handle->smu_mgr;
114 if (smumgr->smumgr_funcs->smu_fini == NULL)
117 ret = smumgr->smumgr_funcs->smu_fini(smumgr);
122 static int pp_hw_init(void *handle)
124 struct pp_smumgr *smumgr;
125 struct pp_eventmgr *eventmgr;
127 struct pp_instance *pp_handle = (struct pp_instance *)handle;
129 ret = pp_check(pp_handle);
131 if (ret == 0 || ret == PP_DPM_DISABLED) {
132 smumgr = pp_handle->smu_mgr;
134 if (smumgr->smumgr_funcs->start_smu == NULL)
137 if(smumgr->smumgr_funcs->start_smu(smumgr)) {
138 pr_err("smc start failed\n");
139 smumgr->smumgr_funcs->smu_fini(smumgr);
142 if (ret == PP_DPM_DISABLED)
143 return PP_DPM_DISABLED;
146 ret = hwmgr_hw_init(pp_handle);
150 eventmgr = pp_handle->eventmgr;
151 if (eventmgr->pp_eventmgr_init == NULL ||
152 eventmgr->pp_eventmgr_init(eventmgr))
157 pp_handle->pm_en = 0;
158 kfree(pp_handle->eventmgr);
159 kfree(pp_handle->hwmgr);
160 pp_handle->hwmgr = NULL;
161 pp_handle->eventmgr = NULL;
162 return PP_DPM_DISABLED;
165 static int pp_hw_fini(void *handle)
167 struct pp_eventmgr *eventmgr;
168 struct pp_instance *pp_handle = (struct pp_instance *)handle;
171 ret = pp_check(pp_handle);
174 eventmgr = pp_handle->eventmgr;
176 if (eventmgr->pp_eventmgr_fini != NULL)
177 eventmgr->pp_eventmgr_fini(eventmgr);
179 hwmgr_hw_fini(pp_handle);
184 static bool pp_is_idle(void *handle)
189 static int pp_wait_for_idle(void *handle)
194 static int pp_sw_reset(void *handle)
200 int amd_set_clockgating_by_smu(void *handle, uint32_t msg_id)
202 struct pp_hwmgr *hwmgr;
203 struct pp_instance *pp_handle = (struct pp_instance *)handle;
206 ret = pp_check(pp_handle);
211 hwmgr = pp_handle->hwmgr;
213 if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
214 pr_info("%s was not implemented.\n", __func__);
218 return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
221 static int pp_set_powergating_state(void *handle,
222 enum amd_powergating_state state)
224 struct pp_hwmgr *hwmgr;
225 struct pp_instance *pp_handle = (struct pp_instance *)handle;
228 ret = pp_check(pp_handle);
233 hwmgr = pp_handle->hwmgr;
235 if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
236 pr_info("%s was not implemented.\n", __func__);
240 /* Enable/disable GFX per cu powergating through SMU */
241 return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
242 state == AMD_PG_STATE_GATE);
245 static int pp_suspend(void *handle)
247 struct pp_eventmgr *eventmgr;
248 struct pem_event_data event_data = { {0} };
249 struct pp_instance *pp_handle = (struct pp_instance *)handle;
252 ret = pp_check(pp_handle);
257 eventmgr = pp_handle->eventmgr;
258 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
263 static int pp_resume(void *handle)
265 struct pp_eventmgr *eventmgr;
266 struct pem_event_data event_data = { {0} };
267 struct pp_smumgr *smumgr;
269 struct pp_instance *pp_handle = (struct pp_instance *)handle;
271 ret1 = pp_check(pp_handle);
273 if (ret1 != 0 && ret1 != PP_DPM_DISABLED)
276 smumgr = pp_handle->smu_mgr;
278 if (smumgr->smumgr_funcs->start_smu == NULL)
281 ret = smumgr->smumgr_funcs->start_smu(smumgr);
283 pr_err("smc start failed\n");
284 smumgr->smumgr_funcs->smu_fini(smumgr);
288 if (ret1 == PP_DPM_DISABLED)
291 eventmgr = pp_handle->eventmgr;
293 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
298 const struct amd_ip_funcs pp_ip_funcs = {
300 .early_init = pp_early_init,
302 .sw_init = pp_sw_init,
303 .sw_fini = pp_sw_fini,
304 .hw_init = pp_hw_init,
305 .hw_fini = pp_hw_fini,
306 .suspend = pp_suspend,
308 .is_idle = pp_is_idle,
309 .wait_for_idle = pp_wait_for_idle,
310 .soft_reset = pp_sw_reset,
311 .set_clockgating_state = NULL,
312 .set_powergating_state = pp_set_powergating_state,
315 static int pp_dpm_load_fw(void *handle)
320 static int pp_dpm_fw_loading_complete(void *handle)
325 static int pp_dpm_force_performance_level(void *handle,
326 enum amd_dpm_forced_level level)
328 struct pp_hwmgr *hwmgr;
329 struct pp_instance *pp_handle = (struct pp_instance *)handle;
332 ret = pp_check(pp_handle);
337 hwmgr = pp_handle->hwmgr;
339 if (hwmgr->hwmgr_func->force_dpm_level == NULL) {
340 pr_info("%s was not implemented.\n", __func__);
344 mutex_lock(&pp_handle->pp_lock);
345 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
346 mutex_unlock(&pp_handle->pp_lock);
350 static enum amd_dpm_forced_level pp_dpm_get_performance_level(
353 struct pp_hwmgr *hwmgr;
354 struct pp_instance *pp_handle = (struct pp_instance *)handle;
356 enum amd_dpm_forced_level level;
358 ret = pp_check(pp_handle);
363 hwmgr = pp_handle->hwmgr;
364 mutex_lock(&pp_handle->pp_lock);
365 level = hwmgr->dpm_level;
366 mutex_unlock(&pp_handle->pp_lock);
370 static int pp_dpm_get_sclk(void *handle, bool low)
372 struct pp_hwmgr *hwmgr;
373 struct pp_instance *pp_handle = (struct pp_instance *)handle;
376 ret = pp_check(pp_handle);
381 hwmgr = pp_handle->hwmgr;
383 if (hwmgr->hwmgr_func->get_sclk == NULL) {
384 pr_info("%s was not implemented.\n", __func__);
387 mutex_lock(&pp_handle->pp_lock);
388 ret = hwmgr->hwmgr_func->get_sclk(hwmgr, low);
389 mutex_unlock(&pp_handle->pp_lock);
393 static int pp_dpm_get_mclk(void *handle, bool low)
395 struct pp_hwmgr *hwmgr;
396 struct pp_instance *pp_handle = (struct pp_instance *)handle;
399 ret = pp_check(pp_handle);
404 hwmgr = pp_handle->hwmgr;
406 if (hwmgr->hwmgr_func->get_mclk == NULL) {
407 pr_info("%s was not implemented.\n", __func__);
410 mutex_lock(&pp_handle->pp_lock);
411 ret = hwmgr->hwmgr_func->get_mclk(hwmgr, low);
412 mutex_unlock(&pp_handle->pp_lock);
416 static int pp_dpm_powergate_vce(void *handle, bool gate)
418 struct pp_hwmgr *hwmgr;
419 struct pp_instance *pp_handle = (struct pp_instance *)handle;
422 ret = pp_check(pp_handle);
427 hwmgr = pp_handle->hwmgr;
429 if (hwmgr->hwmgr_func->powergate_vce == NULL) {
430 pr_info("%s was not implemented.\n", __func__);
433 mutex_lock(&pp_handle->pp_lock);
434 ret = hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
435 mutex_unlock(&pp_handle->pp_lock);
439 static int pp_dpm_powergate_uvd(void *handle, bool gate)
441 struct pp_hwmgr *hwmgr;
442 struct pp_instance *pp_handle = (struct pp_instance *)handle;
445 ret = pp_check(pp_handle);
450 hwmgr = pp_handle->hwmgr;
452 if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
453 pr_info("%s was not implemented.\n", __func__);
456 mutex_lock(&pp_handle->pp_lock);
457 ret = hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
458 mutex_unlock(&pp_handle->pp_lock);
462 static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
465 case POWER_STATE_TYPE_BATTERY:
466 return PP_StateUILabel_Battery;
467 case POWER_STATE_TYPE_BALANCED:
468 return PP_StateUILabel_Balanced;
469 case POWER_STATE_TYPE_PERFORMANCE:
470 return PP_StateUILabel_Performance;
472 return PP_StateUILabel_None;
476 static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id,
477 void *input, void *output)
480 struct pem_event_data data = { {0} };
481 struct pp_instance *pp_handle = (struct pp_instance *)handle;
483 ret = pp_check(pp_handle);
487 mutex_lock(&pp_handle->pp_lock);
489 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
490 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
492 case AMD_PP_EVENT_ENABLE_USER_STATE:
494 enum amd_pm_state_type ps;
500 ps = *(unsigned long *)input;
502 data.requested_ui_label = power_state_convert(ps);
503 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
506 case AMD_PP_EVENT_COMPLETE_INIT:
507 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
509 case AMD_PP_EVENT_READJUST_POWER_STATE:
510 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
515 mutex_unlock(&pp_handle->pp_lock);
519 static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
521 struct pp_hwmgr *hwmgr;
522 struct pp_power_state *state;
523 struct pp_instance *pp_handle = (struct pp_instance *)handle;
525 enum amd_pm_state_type pm_type;
527 ret = pp_check(pp_handle);
532 hwmgr = pp_handle->hwmgr;
534 if (hwmgr->current_ps == NULL)
537 mutex_lock(&pp_handle->pp_lock);
539 state = hwmgr->current_ps;
541 switch (state->classification.ui_label) {
542 case PP_StateUILabel_Battery:
543 pm_type = POWER_STATE_TYPE_BATTERY;
545 case PP_StateUILabel_Balanced:
546 pm_type = POWER_STATE_TYPE_BALANCED;
548 case PP_StateUILabel_Performance:
549 pm_type = POWER_STATE_TYPE_PERFORMANCE;
552 if (state->classification.flags & PP_StateClassificationFlag_Boot)
553 pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
555 pm_type = POWER_STATE_TYPE_DEFAULT;
558 mutex_unlock(&pp_handle->pp_lock);
563 static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
565 struct pp_hwmgr *hwmgr;
566 struct pp_instance *pp_handle = (struct pp_instance *)handle;
569 ret = pp_check(pp_handle);
574 hwmgr = pp_handle->hwmgr;
576 if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
577 pr_info("%s was not implemented.\n", __func__);
580 mutex_lock(&pp_handle->pp_lock);
581 ret = hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
582 mutex_unlock(&pp_handle->pp_lock);
586 static int pp_dpm_get_fan_control_mode(void *handle)
588 struct pp_hwmgr *hwmgr;
589 struct pp_instance *pp_handle = (struct pp_instance *)handle;
592 ret = pp_check(pp_handle);
597 hwmgr = pp_handle->hwmgr;
599 if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
600 pr_info("%s was not implemented.\n", __func__);
603 mutex_lock(&pp_handle->pp_lock);
604 ret = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
605 mutex_unlock(&pp_handle->pp_lock);
609 static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
611 struct pp_hwmgr *hwmgr;
612 struct pp_instance *pp_handle = (struct pp_instance *)handle;
615 ret = pp_check(pp_handle);
620 hwmgr = pp_handle->hwmgr;
622 if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
623 pr_info("%s was not implemented.\n", __func__);
626 mutex_lock(&pp_handle->pp_lock);
627 ret = hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
628 mutex_unlock(&pp_handle->pp_lock);
632 static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
634 struct pp_hwmgr *hwmgr;
635 struct pp_instance *pp_handle = (struct pp_instance *)handle;
638 ret = pp_check(pp_handle);
643 hwmgr = pp_handle->hwmgr;
645 if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
646 pr_info("%s was not implemented.\n", __func__);
650 mutex_lock(&pp_handle->pp_lock);
651 ret = hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
652 mutex_unlock(&pp_handle->pp_lock);
656 static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
658 struct pp_hwmgr *hwmgr;
659 struct pp_instance *pp_handle = (struct pp_instance *)handle;
662 ret = pp_check(pp_handle);
667 hwmgr = pp_handle->hwmgr;
669 if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
672 mutex_lock(&pp_handle->pp_lock);
673 ret = hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
674 mutex_unlock(&pp_handle->pp_lock);
678 static int pp_dpm_get_temperature(void *handle)
680 struct pp_hwmgr *hwmgr;
681 struct pp_instance *pp_handle = (struct pp_instance *)handle;
684 ret = pp_check(pp_handle);
689 hwmgr = pp_handle->hwmgr;
691 if (hwmgr->hwmgr_func->get_temperature == NULL) {
692 pr_info("%s was not implemented.\n", __func__);
695 mutex_lock(&pp_handle->pp_lock);
696 ret = hwmgr->hwmgr_func->get_temperature(hwmgr);
697 mutex_unlock(&pp_handle->pp_lock);
701 static int pp_dpm_get_pp_num_states(void *handle,
702 struct pp_states_info *data)
704 struct pp_hwmgr *hwmgr;
706 struct pp_instance *pp_handle = (struct pp_instance *)handle;
709 ret = pp_check(pp_handle);
714 hwmgr = pp_handle->hwmgr;
716 if (hwmgr->ps == NULL)
719 mutex_lock(&pp_handle->pp_lock);
721 data->nums = hwmgr->num_ps;
723 for (i = 0; i < hwmgr->num_ps; i++) {
724 struct pp_power_state *state = (struct pp_power_state *)
725 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
726 switch (state->classification.ui_label) {
727 case PP_StateUILabel_Battery:
728 data->states[i] = POWER_STATE_TYPE_BATTERY;
730 case PP_StateUILabel_Balanced:
731 data->states[i] = POWER_STATE_TYPE_BALANCED;
733 case PP_StateUILabel_Performance:
734 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
737 if (state->classification.flags & PP_StateClassificationFlag_Boot)
738 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
740 data->states[i] = POWER_STATE_TYPE_DEFAULT;
743 mutex_unlock(&pp_handle->pp_lock);
747 static int pp_dpm_get_pp_table(void *handle, char **table)
749 struct pp_hwmgr *hwmgr;
750 struct pp_instance *pp_handle = (struct pp_instance *)handle;
754 ret = pp_check(pp_handle);
759 hwmgr = pp_handle->hwmgr;
761 if (!hwmgr->soft_pp_table)
764 mutex_lock(&pp_handle->pp_lock);
765 *table = (char *)hwmgr->soft_pp_table;
766 size = hwmgr->soft_pp_table_size;
767 mutex_unlock(&pp_handle->pp_lock);
771 static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
773 struct pp_hwmgr *hwmgr;
774 struct pp_instance *pp_handle = (struct pp_instance *)handle;
777 ret = pp_check(pp_handle);
782 hwmgr = pp_handle->hwmgr;
783 mutex_lock(&pp_handle->pp_lock);
784 if (!hwmgr->hardcode_pp_table) {
785 hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
786 hwmgr->soft_pp_table_size,
788 if (!hwmgr->hardcode_pp_table) {
789 mutex_unlock(&pp_handle->pp_lock);
794 memcpy(hwmgr->hardcode_pp_table, buf, size);
796 hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
797 mutex_unlock(&pp_handle->pp_lock);
799 ret = amd_powerplay_reset(handle);
803 if (hwmgr->hwmgr_func->avfs_control) {
804 ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
812 static int pp_dpm_force_clock_level(void *handle,
813 enum pp_clock_type type, uint32_t mask)
815 struct pp_hwmgr *hwmgr;
816 struct pp_instance *pp_handle = (struct pp_instance *)handle;
819 ret = pp_check(pp_handle);
824 hwmgr = pp_handle->hwmgr;
826 if (hwmgr->hwmgr_func->force_clock_level == NULL) {
827 pr_info("%s was not implemented.\n", __func__);
830 mutex_lock(&pp_handle->pp_lock);
831 hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
832 mutex_unlock(&pp_handle->pp_lock);
836 static int pp_dpm_print_clock_levels(void *handle,
837 enum pp_clock_type type, char *buf)
839 struct pp_hwmgr *hwmgr;
840 struct pp_instance *pp_handle = (struct pp_instance *)handle;
843 ret = pp_check(pp_handle);
848 hwmgr = pp_handle->hwmgr;
850 if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
851 pr_info("%s was not implemented.\n", __func__);
854 mutex_lock(&pp_handle->pp_lock);
855 ret = hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
856 mutex_unlock(&pp_handle->pp_lock);
860 static int pp_dpm_get_sclk_od(void *handle)
862 struct pp_hwmgr *hwmgr;
863 struct pp_instance *pp_handle = (struct pp_instance *)handle;
866 ret = pp_check(pp_handle);
871 hwmgr = pp_handle->hwmgr;
873 if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
874 pr_info("%s was not implemented.\n", __func__);
877 mutex_lock(&pp_handle->pp_lock);
878 ret = hwmgr->hwmgr_func->get_sclk_od(hwmgr);
879 mutex_unlock(&pp_handle->pp_lock);
883 static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
885 struct pp_hwmgr *hwmgr;
886 struct pp_instance *pp_handle = (struct pp_instance *)handle;
889 ret = pp_check(pp_handle);
894 hwmgr = pp_handle->hwmgr;
896 if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
897 pr_info("%s was not implemented.\n", __func__);
901 mutex_lock(&pp_handle->pp_lock);
902 ret = hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
903 mutex_unlock(&pp_handle->pp_lock);
907 static int pp_dpm_get_mclk_od(void *handle)
909 struct pp_hwmgr *hwmgr;
910 struct pp_instance *pp_handle = (struct pp_instance *)handle;
913 ret = pp_check(pp_handle);
918 hwmgr = pp_handle->hwmgr;
920 if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
921 pr_info("%s was not implemented.\n", __func__);
924 mutex_lock(&pp_handle->pp_lock);
925 ret = hwmgr->hwmgr_func->get_mclk_od(hwmgr);
926 mutex_unlock(&pp_handle->pp_lock);
930 static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
932 struct pp_hwmgr *hwmgr;
933 struct pp_instance *pp_handle = (struct pp_instance *)handle;
936 ret = pp_check(pp_handle);
941 hwmgr = pp_handle->hwmgr;
943 if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
944 pr_info("%s was not implemented.\n", __func__);
947 mutex_lock(&pp_handle->pp_lock);
948 ret = hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
949 mutex_unlock(&pp_handle->pp_lock);
953 static int pp_dpm_read_sensor(void *handle, int idx,
954 void *value, int *size)
956 struct pp_hwmgr *hwmgr;
957 struct pp_instance *pp_handle = (struct pp_instance *)handle;
960 ret = pp_check(pp_handle);
965 hwmgr = pp_handle->hwmgr;
967 if (hwmgr->hwmgr_func->read_sensor == NULL) {
968 pr_info("%s was not implemented.\n", __func__);
972 mutex_lock(&pp_handle->pp_lock);
973 ret = hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
974 mutex_unlock(&pp_handle->pp_lock);
979 static struct amd_vce_state*
980 pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
982 struct pp_hwmgr *hwmgr;
983 struct pp_instance *pp_handle = (struct pp_instance *)handle;
986 ret = pp_check(pp_handle);
991 hwmgr = pp_handle->hwmgr;
993 if (hwmgr && idx < hwmgr->num_vce_state_tables)
994 return &hwmgr->vce_states[idx];
998 static int pp_dpm_reset_power_profile_state(void *handle,
999 struct amd_pp_profile *request)
1001 struct pp_hwmgr *hwmgr;
1002 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1004 if (!request || pp_check(pp_handle))
1007 hwmgr = pp_handle->hwmgr;
1009 if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
1010 pr_info("%s was not implemented.\n", __func__);
1014 if (request->type == AMD_PP_GFX_PROFILE) {
1015 hwmgr->gfx_power_profile = hwmgr->default_gfx_power_profile;
1016 return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
1017 &hwmgr->gfx_power_profile);
1018 } else if (request->type == AMD_PP_COMPUTE_PROFILE) {
1019 hwmgr->compute_power_profile =
1020 hwmgr->default_compute_power_profile;
1021 return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
1022 &hwmgr->compute_power_profile);
1027 static int pp_dpm_get_power_profile_state(void *handle,
1028 struct amd_pp_profile *query)
1030 struct pp_hwmgr *hwmgr;
1031 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1033 if (!query || pp_check(pp_handle))
1036 hwmgr = pp_handle->hwmgr;
1038 if (query->type == AMD_PP_GFX_PROFILE)
1039 memcpy(query, &hwmgr->gfx_power_profile,
1040 sizeof(struct amd_pp_profile));
1041 else if (query->type == AMD_PP_COMPUTE_PROFILE)
1042 memcpy(query, &hwmgr->compute_power_profile,
1043 sizeof(struct amd_pp_profile));
1050 static int pp_dpm_set_power_profile_state(void *handle,
1051 struct amd_pp_profile *request)
1053 struct pp_hwmgr *hwmgr;
1054 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1057 if (!request || pp_check(pp_handle))
1060 hwmgr = pp_handle->hwmgr;
1062 if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
1063 pr_info("%s was not implemented.\n", __func__);
1067 if (request->min_sclk ||
1068 request->min_mclk ||
1069 request->activity_threshold ||
1071 request->down_hyst) {
1072 if (request->type == AMD_PP_GFX_PROFILE)
1073 memcpy(&hwmgr->gfx_power_profile, request,
1074 sizeof(struct amd_pp_profile));
1075 else if (request->type == AMD_PP_COMPUTE_PROFILE)
1076 memcpy(&hwmgr->compute_power_profile, request,
1077 sizeof(struct amd_pp_profile));
1081 if (request->type == hwmgr->current_power_profile)
1082 ret = hwmgr->hwmgr_func->set_power_profile_state(
1086 /* set power profile if it exists */
1087 switch (request->type) {
1088 case AMD_PP_GFX_PROFILE:
1089 ret = hwmgr->hwmgr_func->set_power_profile_state(
1091 &hwmgr->gfx_power_profile);
1093 case AMD_PP_COMPUTE_PROFILE:
1094 ret = hwmgr->hwmgr_func->set_power_profile_state(
1096 &hwmgr->compute_power_profile);
1104 hwmgr->current_power_profile = request->type;
1109 static int pp_dpm_switch_power_profile(void *handle,
1110 enum amd_pp_profile_type type)
1112 struct pp_hwmgr *hwmgr;
1113 struct amd_pp_profile request = {0};
1114 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1116 if (pp_check(pp_handle))
1119 hwmgr = pp_handle->hwmgr;
1121 if (hwmgr->current_power_profile != type) {
1122 request.type = type;
1123 pp_dpm_set_power_profile_state(handle, &request);
1129 const struct amd_powerplay_funcs pp_dpm_funcs = {
1130 .get_temperature = pp_dpm_get_temperature,
1131 .load_firmware = pp_dpm_load_fw,
1132 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
1133 .force_performance_level = pp_dpm_force_performance_level,
1134 .get_performance_level = pp_dpm_get_performance_level,
1135 .get_current_power_state = pp_dpm_get_current_power_state,
1136 .get_sclk = pp_dpm_get_sclk,
1137 .get_mclk = pp_dpm_get_mclk,
1138 .powergate_vce = pp_dpm_powergate_vce,
1139 .powergate_uvd = pp_dpm_powergate_uvd,
1140 .dispatch_tasks = pp_dpm_dispatch_tasks,
1141 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
1142 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
1143 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
1144 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
1145 .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
1146 .get_pp_num_states = pp_dpm_get_pp_num_states,
1147 .get_pp_table = pp_dpm_get_pp_table,
1148 .set_pp_table = pp_dpm_set_pp_table,
1149 .force_clock_level = pp_dpm_force_clock_level,
1150 .print_clock_levels = pp_dpm_print_clock_levels,
1151 .get_sclk_od = pp_dpm_get_sclk_od,
1152 .set_sclk_od = pp_dpm_set_sclk_od,
1153 .get_mclk_od = pp_dpm_get_mclk_od,
1154 .set_mclk_od = pp_dpm_set_mclk_od,
1155 .read_sensor = pp_dpm_read_sensor,
1156 .get_vce_clock_state = pp_dpm_get_vce_clock_state,
1157 .reset_power_profile_state = pp_dpm_reset_power_profile_state,
1158 .get_power_profile_state = pp_dpm_get_power_profile_state,
1159 .set_power_profile_state = pp_dpm_set_power_profile_state,
1160 .switch_power_profile = pp_dpm_switch_power_profile,
1163 int amd_powerplay_create(struct amd_pp_init *pp_init,
1166 struct pp_instance *instance;
1168 if (pp_init == NULL || handle == NULL)
1171 instance = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
1172 if (instance == NULL)
1175 instance->pp_valid = PP_VALID;
1176 instance->chip_family = pp_init->chip_family;
1177 instance->chip_id = pp_init->chip_id;
1178 instance->pm_en = pp_init->pm_en;
1179 instance->feature_mask = pp_init->feature_mask;
1180 instance->device = pp_init->device;
1181 mutex_init(&instance->pp_lock);
1186 int amd_powerplay_destroy(void *handle)
1188 struct pp_instance *instance = (struct pp_instance *)handle;
1190 if (instance->pm_en) {
1191 kfree(instance->eventmgr);
1192 kfree(instance->hwmgr);
1193 instance->hwmgr = NULL;
1194 instance->eventmgr = NULL;
1197 kfree(instance->smu_mgr);
1198 instance->smu_mgr = NULL;
1204 int amd_powerplay_reset(void *handle)
1206 struct pp_instance *instance = (struct pp_instance *)handle;
1207 struct pp_eventmgr *eventmgr;
1208 struct pem_event_data event_data = { {0} };
1211 if (cgs_is_virtualization_enabled(instance->smu_mgr->device))
1212 return PP_DPM_DISABLED;
1214 ret = pp_check(instance);
1218 ret = pp_hw_fini(handle);
1222 ret = hwmgr_hw_init(instance);
1224 return PP_DPM_DISABLED;
1226 eventmgr = instance->eventmgr;
1228 if (eventmgr->pp_eventmgr_init == NULL)
1229 return PP_DPM_DISABLED;
1231 ret = eventmgr->pp_eventmgr_init(eventmgr);
1235 return pem_handle_event(eventmgr, AMD_PP_EVENT_COMPLETE_INIT, &event_data);
1238 /* export this function to DAL */
1240 int amd_powerplay_display_configuration_change(void *handle,
1241 const struct amd_pp_display_configuration *display_config)
1243 struct pp_hwmgr *hwmgr;
1244 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1247 ret = pp_check(pp_handle);
1252 hwmgr = pp_handle->hwmgr;
1253 mutex_lock(&pp_handle->pp_lock);
1254 phm_store_dal_configuration_data(hwmgr, display_config);
1255 mutex_unlock(&pp_handle->pp_lock);
1259 int amd_powerplay_get_display_power_level(void *handle,
1260 struct amd_pp_simple_clock_info *output)
1262 struct pp_hwmgr *hwmgr;
1263 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1266 ret = pp_check(pp_handle);
1271 hwmgr = pp_handle->hwmgr;
1276 mutex_lock(&pp_handle->pp_lock);
1277 ret = phm_get_dal_power_level(hwmgr, output);
1278 mutex_unlock(&pp_handle->pp_lock);
1282 int amd_powerplay_get_current_clocks(void *handle,
1283 struct amd_pp_clock_info *clocks)
1285 struct amd_pp_simple_clock_info simple_clocks;
1286 struct pp_clock_info hw_clocks;
1287 struct pp_hwmgr *hwmgr;
1288 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1291 ret = pp_check(pp_handle);
1296 hwmgr = pp_handle->hwmgr;
1298 mutex_lock(&pp_handle->pp_lock);
1300 phm_get_dal_power_level(hwmgr, &simple_clocks);
1302 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1303 PHM_PlatformCaps_PowerContainment))
1304 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1305 &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
1307 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1308 &hw_clocks, PHM_PerformanceLevelDesignation_Activity);
1311 pr_info("Error in phm_get_clock_info \n");
1312 mutex_unlock(&pp_handle->pp_lock);
1316 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1317 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1318 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1319 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1320 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1321 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1323 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1324 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1326 clocks->max_clocks_state = simple_clocks.level;
1328 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
1329 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1330 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1332 mutex_unlock(&pp_handle->pp_lock);
1336 int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
1338 struct pp_hwmgr *hwmgr;
1339 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1342 ret = pp_check(pp_handle);
1347 hwmgr = pp_handle->hwmgr;
1352 mutex_lock(&pp_handle->pp_lock);
1353 ret = phm_get_clock_by_type(hwmgr, type, clocks);
1354 mutex_unlock(&pp_handle->pp_lock);
1358 int amd_powerplay_get_clock_by_type_with_latency(void *handle,
1359 enum amd_pp_clock_type type,
1360 struct pp_clock_levels_with_latency *clocks)
1362 struct pp_hwmgr *hwmgr;
1363 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1366 ret = pp_check(pp_handle);
1373 mutex_lock(&pp_handle->pp_lock);
1374 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1375 ret = phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
1376 mutex_unlock(&pp_handle->pp_lock);
1380 int amd_powerplay_get_clock_by_type_with_voltage(void *handle,
1381 enum amd_pp_clock_type type,
1382 struct pp_clock_levels_with_voltage *clocks)
1384 struct pp_hwmgr *hwmgr;
1385 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1388 ret = pp_check(pp_handle);
1395 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1397 mutex_lock(&pp_handle->pp_lock);
1399 ret = phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);
1401 mutex_unlock(&pp_handle->pp_lock);
1405 int amd_powerplay_set_watermarks_for_clocks_ranges(void *handle,
1406 struct pp_wm_sets_with_clock_ranges_soc15 *wm_with_clock_ranges)
1408 struct pp_hwmgr *hwmgr;
1409 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1412 ret = pp_check(pp_handle);
1416 if (!wm_with_clock_ranges)
1419 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1421 mutex_lock(&pp_handle->pp_lock);
1422 ret = phm_set_watermarks_for_clocks_ranges(hwmgr,
1423 wm_with_clock_ranges);
1424 mutex_unlock(&pp_handle->pp_lock);
1429 int amd_powerplay_display_clock_voltage_request(void *handle,
1430 struct pp_display_clock_request *clock)
1432 struct pp_hwmgr *hwmgr;
1433 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1436 ret = pp_check(pp_handle);
1443 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1445 mutex_lock(&pp_handle->pp_lock);
1446 ret = phm_display_clock_voltage_request(hwmgr, clock);
1447 mutex_unlock(&pp_handle->pp_lock);
1452 int amd_powerplay_get_display_mode_validation_clocks(void *handle,
1453 struct amd_pp_simple_clock_info *clocks)
1455 struct pp_hwmgr *hwmgr;
1456 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1459 ret = pp_check(pp_handle);
1464 hwmgr = pp_handle->hwmgr;
1469 mutex_lock(&pp_handle->pp_lock);
1471 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1472 ret = phm_get_max_high_clocks(hwmgr, clocks);
1474 mutex_unlock(&pp_handle->pp_lock);