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.
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/gfp.h>
26 #include <linux/slab.h>
27 #include "amd_shared.h"
28 #include "amd_powerplay.h"
29 #include "pp_instance.h"
30 #include "power_state.h"
31 #include "eventmanager.h"
34 #define PP_CHECK(handle) \
36 if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
40 static int pp_early_init(void *handle)
45 static int pp_sw_init(void *handle)
47 struct pp_instance *pp_handle;
48 struct pp_hwmgr *hwmgr;
54 pp_handle = (struct pp_instance *)handle;
55 hwmgr = pp_handle->hwmgr;
57 if (hwmgr == NULL || hwmgr->pptable_func == NULL ||
58 hwmgr->hwmgr_func == NULL ||
59 hwmgr->pptable_func->pptable_init == NULL ||
60 hwmgr->hwmgr_func->backend_init == NULL)
63 ret = hwmgr->pptable_func->pptable_init(hwmgr);
66 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
69 printk("amdgpu: powerplay initialization failed\n");
71 printk("amdgpu: powerplay initialized\n");
76 static int pp_sw_fini(void *handle)
78 struct pp_instance *pp_handle;
79 struct pp_hwmgr *hwmgr;
85 pp_handle = (struct pp_instance *)handle;
86 hwmgr = pp_handle->hwmgr;
88 if (hwmgr != NULL || hwmgr->hwmgr_func != NULL ||
89 hwmgr->hwmgr_func->backend_fini != NULL)
90 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
95 static int pp_hw_init(void *handle)
97 struct pp_instance *pp_handle;
98 struct pp_smumgr *smumgr;
99 struct pp_eventmgr *eventmgr;
105 pp_handle = (struct pp_instance *)handle;
106 smumgr = pp_handle->smu_mgr;
108 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
109 smumgr->smumgr_funcs->smu_init == NULL ||
110 smumgr->smumgr_funcs->start_smu == NULL)
113 ret = smumgr->smumgr_funcs->smu_init(smumgr);
115 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
119 ret = smumgr->smumgr_funcs->start_smu(smumgr);
121 printk(KERN_ERR "[ powerplay ] smc start failed\n");
122 smumgr->smumgr_funcs->smu_fini(smumgr);
126 hw_init_power_state_table(pp_handle->hwmgr);
127 eventmgr = pp_handle->eventmgr;
129 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
132 ret = eventmgr->pp_eventmgr_init(eventmgr);
136 static int pp_hw_fini(void *handle)
138 struct pp_instance *pp_handle;
139 struct pp_smumgr *smumgr;
140 struct pp_eventmgr *eventmgr;
145 pp_handle = (struct pp_instance *)handle;
146 eventmgr = pp_handle->eventmgr;
148 if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
149 eventmgr->pp_eventmgr_fini(eventmgr);
151 smumgr = pp_handle->smu_mgr;
153 if (smumgr != NULL || smumgr->smumgr_funcs != NULL ||
154 smumgr->smumgr_funcs->smu_fini != NULL)
155 smumgr->smumgr_funcs->smu_fini(smumgr);
160 static bool pp_is_idle(void *handle)
165 static int pp_wait_for_idle(void *handle)
170 static int pp_sw_reset(void *handle)
175 static void pp_print_status(void *handle)
180 static int pp_set_clockgating_state(void *handle,
181 enum amd_clockgating_state state)
186 static int pp_set_powergating_state(void *handle,
187 enum amd_powergating_state state)
192 static int pp_suspend(void *handle)
194 struct pp_instance *pp_handle;
195 struct pp_eventmgr *eventmgr;
196 struct pem_event_data event_data = { {0} };
201 pp_handle = (struct pp_instance *)handle;
202 eventmgr = pp_handle->eventmgr;
203 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
207 static int pp_resume(void *handle)
209 struct pp_instance *pp_handle;
210 struct pp_eventmgr *eventmgr;
211 struct pem_event_data event_data = { {0} };
212 struct pp_smumgr *smumgr;
218 pp_handle = (struct pp_instance *)handle;
219 smumgr = pp_handle->smu_mgr;
221 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
222 smumgr->smumgr_funcs->start_smu == NULL)
225 ret = smumgr->smumgr_funcs->start_smu(smumgr);
227 printk(KERN_ERR "[ powerplay ] smc start failed\n");
228 smumgr->smumgr_funcs->smu_fini(smumgr);
232 eventmgr = pp_handle->eventmgr;
233 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
238 const struct amd_ip_funcs pp_ip_funcs = {
239 .early_init = pp_early_init,
241 .sw_init = pp_sw_init,
242 .sw_fini = pp_sw_fini,
243 .hw_init = pp_hw_init,
244 .hw_fini = pp_hw_fini,
245 .suspend = pp_suspend,
247 .is_idle = pp_is_idle,
248 .wait_for_idle = pp_wait_for_idle,
249 .soft_reset = pp_sw_reset,
250 .print_status = pp_print_status,
251 .set_clockgating_state = pp_set_clockgating_state,
252 .set_powergating_state = pp_set_powergating_state,
255 static int pp_dpm_load_fw(void *handle)
260 static int pp_dpm_fw_loading_complete(void *handle)
265 static int pp_dpm_force_performance_level(void *handle,
266 enum amd_dpm_forced_level level)
268 struct pp_instance *pp_handle;
269 struct pp_hwmgr *hwmgr;
274 pp_handle = (struct pp_instance *)handle;
276 hwmgr = pp_handle->hwmgr;
278 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
279 hwmgr->hwmgr_func->force_dpm_level == NULL)
282 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
287 static enum amd_dpm_forced_level pp_dpm_get_performance_level(
290 struct pp_hwmgr *hwmgr;
295 hwmgr = ((struct pp_instance *)handle)->hwmgr;
300 return (((struct pp_instance *)handle)->hwmgr->dpm_level);
303 static int pp_dpm_get_sclk(void *handle, bool low)
305 struct pp_hwmgr *hwmgr;
310 hwmgr = ((struct pp_instance *)handle)->hwmgr;
312 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
313 hwmgr->hwmgr_func->get_sclk == NULL)
316 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
319 static int pp_dpm_get_mclk(void *handle, bool low)
321 struct pp_hwmgr *hwmgr;
326 hwmgr = ((struct pp_instance *)handle)->hwmgr;
328 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
329 hwmgr->hwmgr_func->get_mclk == NULL)
332 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
335 static int pp_dpm_powergate_vce(void *handle, bool gate)
337 struct pp_hwmgr *hwmgr;
342 hwmgr = ((struct pp_instance *)handle)->hwmgr;
344 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
345 hwmgr->hwmgr_func->powergate_vce == NULL)
348 return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
351 static int pp_dpm_powergate_uvd(void *handle, bool gate)
353 struct pp_hwmgr *hwmgr;
358 hwmgr = ((struct pp_instance *)handle)->hwmgr;
360 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
361 hwmgr->hwmgr_func->powergate_uvd == NULL)
364 return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
367 static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
370 case POWER_STATE_TYPE_BATTERY:
371 return PP_StateUILabel_Battery;
372 case POWER_STATE_TYPE_BALANCED:
373 return PP_StateUILabel_Balanced;
374 case POWER_STATE_TYPE_PERFORMANCE:
375 return PP_StateUILabel_Performance;
377 return PP_StateUILabel_None;
381 int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
384 struct pp_instance *pp_handle;
385 struct pem_event_data data = { {0} };
387 pp_handle = (struct pp_instance *)handle;
389 if (pp_handle == NULL)
393 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
394 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
396 case AMD_PP_EVENT_ENABLE_USER_STATE:
398 enum amd_pm_state_type ps;
402 ps = *(unsigned long *)input;
404 data.requested_ui_label = power_state_convert(ps);
405 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
408 case AMD_PP_EVENT_COMPLETE_INIT:
409 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
417 enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
419 struct pp_hwmgr *hwmgr;
420 struct pp_power_state *state;
425 hwmgr = ((struct pp_instance *)handle)->hwmgr;
427 if (hwmgr == NULL || hwmgr->current_ps == NULL)
430 state = hwmgr->current_ps;
432 switch (state->classification.ui_label) {
433 case PP_StateUILabel_Battery:
434 return POWER_STATE_TYPE_BATTERY;
435 case PP_StateUILabel_Balanced:
436 return POWER_STATE_TYPE_BALANCED;
437 case PP_StateUILabel_Performance:
438 return POWER_STATE_TYPE_PERFORMANCE;
440 if (state->classification.flags & PP_StateClassificationFlag_Boot)
441 return POWER_STATE_TYPE_INTERNAL_BOOT;
443 return POWER_STATE_TYPE_DEFAULT;
448 pp_debugfs_print_current_performance_level(void *handle,
451 struct pp_hwmgr *hwmgr;
456 hwmgr = ((struct pp_instance *)handle)->hwmgr;
458 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
459 hwmgr->hwmgr_func->print_current_perforce_level == NULL)
462 hwmgr->hwmgr_func->print_current_perforce_level(hwmgr, m);
465 static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
467 struct pp_hwmgr *hwmgr;
472 hwmgr = ((struct pp_instance *)handle)->hwmgr;
474 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
475 hwmgr->hwmgr_func->set_fan_control_mode == NULL)
478 return hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
481 static int pp_dpm_get_fan_control_mode(void *handle)
483 struct pp_hwmgr *hwmgr;
488 hwmgr = ((struct pp_instance *)handle)->hwmgr;
490 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
491 hwmgr->hwmgr_func->get_fan_control_mode == NULL)
494 return hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
497 static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
499 struct pp_hwmgr *hwmgr;
504 hwmgr = ((struct pp_instance *)handle)->hwmgr;
506 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
507 hwmgr->hwmgr_func->set_fan_speed_percent == NULL)
510 return hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
513 static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
515 struct pp_hwmgr *hwmgr;
520 hwmgr = ((struct pp_instance *)handle)->hwmgr;
522 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
523 hwmgr->hwmgr_func->get_fan_speed_percent == NULL)
526 return hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
529 static int pp_dpm_get_temperature(void *handle)
531 struct pp_hwmgr *hwmgr;
536 hwmgr = ((struct pp_instance *)handle)->hwmgr;
538 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
539 hwmgr->hwmgr_func->get_temperature == NULL)
542 return hwmgr->hwmgr_func->get_temperature(hwmgr);
545 static int pp_dpm_get_pp_num_states(void *handle,
546 struct pp_states_info *data)
548 struct pp_hwmgr *hwmgr;
554 hwmgr = ((struct pp_instance *)handle)->hwmgr;
556 if (hwmgr == NULL || hwmgr->ps == NULL)
559 data->nums = hwmgr->num_ps;
561 for (i = 0; i < hwmgr->num_ps; i++) {
562 struct pp_power_state *state = (struct pp_power_state *)
563 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
564 switch (state->classification.ui_label) {
565 case PP_StateUILabel_Battery:
566 data->states[i] = POWER_STATE_TYPE_BATTERY;
568 case PP_StateUILabel_Balanced:
569 data->states[i] = POWER_STATE_TYPE_BALANCED;
571 case PP_StateUILabel_Performance:
572 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
575 if (state->classification.flags & PP_StateClassificationFlag_Boot)
576 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
578 data->states[i] = POWER_STATE_TYPE_DEFAULT;
585 static int pp_dpm_get_pp_table(void *handle, char **table)
587 struct pp_hwmgr *hwmgr;
592 hwmgr = ((struct pp_instance *)handle)->hwmgr;
594 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
595 hwmgr->hwmgr_func->get_pp_table == NULL)
598 return hwmgr->hwmgr_func->get_pp_table(hwmgr, table);
601 static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
603 struct pp_hwmgr *hwmgr;
608 hwmgr = ((struct pp_instance *)handle)->hwmgr;
610 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
611 hwmgr->hwmgr_func->set_pp_table == NULL)
614 return hwmgr->hwmgr_func->set_pp_table(hwmgr, buf, size);
617 static int pp_dpm_force_clock_level(void *handle,
618 enum pp_clock_type type, int level)
620 struct pp_hwmgr *hwmgr;
625 hwmgr = ((struct pp_instance *)handle)->hwmgr;
627 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
628 hwmgr->hwmgr_func->force_clock_level == NULL)
631 return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, level);
634 static int pp_dpm_print_clock_levels(void *handle,
635 enum pp_clock_type type, char *buf)
637 struct pp_hwmgr *hwmgr;
642 hwmgr = ((struct pp_instance *)handle)->hwmgr;
644 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
645 hwmgr->hwmgr_func->print_clock_levels == NULL)
648 return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
651 const struct amd_powerplay_funcs pp_dpm_funcs = {
652 .get_temperature = pp_dpm_get_temperature,
653 .load_firmware = pp_dpm_load_fw,
654 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
655 .force_performance_level = pp_dpm_force_performance_level,
656 .get_performance_level = pp_dpm_get_performance_level,
657 .get_current_power_state = pp_dpm_get_current_power_state,
658 .get_sclk = pp_dpm_get_sclk,
659 .get_mclk = pp_dpm_get_mclk,
660 .powergate_vce = pp_dpm_powergate_vce,
661 .powergate_uvd = pp_dpm_powergate_uvd,
662 .dispatch_tasks = pp_dpm_dispatch_tasks,
663 .print_current_performance_level = pp_debugfs_print_current_performance_level,
664 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
665 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
666 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
667 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
668 .get_pp_num_states = pp_dpm_get_pp_num_states,
669 .get_pp_table = pp_dpm_get_pp_table,
670 .set_pp_table = pp_dpm_set_pp_table,
671 .force_clock_level = pp_dpm_force_clock_level,
672 .print_clock_levels = pp_dpm_print_clock_levels,
675 static int amd_pp_instance_init(struct amd_pp_init *pp_init,
676 struct amd_powerplay *amd_pp)
679 struct pp_instance *handle;
681 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
685 handle->pp_valid = PP_VALID;
687 ret = smum_init(pp_init, handle);
691 ret = hwmgr_init(pp_init, handle);
695 ret = eventmgr_init(handle);
699 amd_pp->pp_handle = handle;
703 hwmgr_fini(handle->hwmgr);
705 smum_fini(handle->smu_mgr);
711 static int amd_pp_instance_fini(void *handle)
713 struct pp_instance *instance = (struct pp_instance *)handle;
715 if (instance == NULL)
718 eventmgr_fini(instance->eventmgr);
720 hwmgr_fini(instance->hwmgr);
722 smum_fini(instance->smu_mgr);
728 int amd_powerplay_init(struct amd_pp_init *pp_init,
729 struct amd_powerplay *amd_pp)
733 if (pp_init == NULL || amd_pp == NULL)
736 ret = amd_pp_instance_init(pp_init, amd_pp);
741 amd_pp->ip_funcs = &pp_ip_funcs;
742 amd_pp->pp_funcs = &pp_dpm_funcs;
747 int amd_powerplay_fini(void *handle)
749 amd_pp_instance_fini(handle);
754 /* export this function to DAL */
756 int amd_powerplay_display_configuration_change(void *handle,
757 const struct amd_pp_display_configuration *display_config)
759 struct pp_hwmgr *hwmgr;
761 PP_CHECK((struct pp_instance *)handle);
763 hwmgr = ((struct pp_instance *)handle)->hwmgr;
765 phm_store_dal_configuration_data(hwmgr, display_config);
770 int amd_powerplay_get_display_power_level(void *handle,
771 struct amd_pp_simple_clock_info *output)
773 struct pp_hwmgr *hwmgr;
775 PP_CHECK((struct pp_instance *)handle);
780 hwmgr = ((struct pp_instance *)handle)->hwmgr;
782 return phm_get_dal_power_level(hwmgr, output);
785 int amd_powerplay_get_current_clocks(void *handle,
786 struct amd_pp_clock_info *clocks)
788 struct pp_hwmgr *hwmgr;
789 struct amd_pp_simple_clock_info simple_clocks;
790 struct pp_clock_info hw_clocks;
792 PP_CHECK((struct pp_instance *)handle);
797 hwmgr = ((struct pp_instance *)handle)->hwmgr;
799 phm_get_dal_power_level(hwmgr, &simple_clocks);
801 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_PowerContainment)) {
802 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment))
803 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetPowerContainmentClockInfo", return -1);
805 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_Activity))
806 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetClockInfo", return -1);
809 clocks->min_engine_clock = hw_clocks.min_eng_clk;
810 clocks->max_engine_clock = hw_clocks.max_eng_clk;
811 clocks->min_memory_clock = hw_clocks.min_mem_clk;
812 clocks->max_memory_clock = hw_clocks.max_mem_clk;
813 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
814 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
816 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
817 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
819 clocks->max_clocks_state = simple_clocks.level;
821 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
822 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
823 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
830 int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
834 struct pp_hwmgr *hwmgr;
836 PP_CHECK((struct pp_instance *)handle);
841 hwmgr = ((struct pp_instance *)handle)->hwmgr;
843 result = phm_get_clock_by_type(hwmgr, type, clocks);
848 int amd_powerplay_get_display_mode_validation_clocks(void *handle,
849 struct amd_pp_simple_clock_info *clocks)
852 struct pp_hwmgr *hwmgr;
854 PP_CHECK((struct pp_instance *)handle);
859 hwmgr = ((struct pp_instance *)handle)->hwmgr;
861 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
862 result = phm_get_max_high_clocks(hwmgr, clocks);