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"
33 #define PP_CHECK(handle) \
35 if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
39 static int pp_early_init(void *handle
)
44 static int pp_sw_init(void *handle
)
46 struct pp_instance
*pp_handle
;
47 struct pp_hwmgr
*hwmgr
;
53 pp_handle
= (struct pp_instance
*)handle
;
54 hwmgr
= pp_handle
->hwmgr
;
56 if (hwmgr
== NULL
|| hwmgr
->pptable_func
== NULL
||
57 hwmgr
->hwmgr_func
== NULL
||
58 hwmgr
->pptable_func
->pptable_init
== NULL
||
59 hwmgr
->hwmgr_func
->backend_init
== NULL
)
62 ret
= hwmgr
->pptable_func
->pptable_init(hwmgr
);
65 ret
= hwmgr
->hwmgr_func
->backend_init(hwmgr
);
70 static int pp_sw_fini(void *handle
)
72 struct pp_instance
*pp_handle
;
73 struct pp_hwmgr
*hwmgr
;
79 pp_handle
= (struct pp_instance
*)handle
;
80 hwmgr
= pp_handle
->hwmgr
;
82 if (hwmgr
!= NULL
|| hwmgr
->hwmgr_func
!= NULL
||
83 hwmgr
->hwmgr_func
->backend_fini
!= NULL
)
84 ret
= hwmgr
->hwmgr_func
->backend_fini(hwmgr
);
89 static int pp_hw_init(void *handle
)
91 struct pp_instance
*pp_handle
;
92 struct pp_smumgr
*smumgr
;
93 struct pp_eventmgr
*eventmgr
;
99 pp_handle
= (struct pp_instance
*)handle
;
100 smumgr
= pp_handle
->smu_mgr
;
102 if (smumgr
== NULL
|| smumgr
->smumgr_funcs
== NULL
||
103 smumgr
->smumgr_funcs
->smu_init
== NULL
||
104 smumgr
->smumgr_funcs
->start_smu
== NULL
)
107 ret
= smumgr
->smumgr_funcs
->smu_init(smumgr
);
109 printk(KERN_ERR
"[ powerplay ] smc initialization failed\n");
113 ret
= smumgr
->smumgr_funcs
->start_smu(smumgr
);
115 printk(KERN_ERR
"[ powerplay ] smc start failed\n");
116 smumgr
->smumgr_funcs
->smu_fini(smumgr
);
120 hw_init_power_state_table(pp_handle
->hwmgr
);
121 eventmgr
= pp_handle
->eventmgr
;
123 if (eventmgr
== NULL
|| eventmgr
->pp_eventmgr_init
== NULL
)
126 ret
= eventmgr
->pp_eventmgr_init(eventmgr
);
130 static int pp_hw_fini(void *handle
)
132 struct pp_instance
*pp_handle
;
133 struct pp_smumgr
*smumgr
;
134 struct pp_eventmgr
*eventmgr
;
139 pp_handle
= (struct pp_instance
*)handle
;
140 eventmgr
= pp_handle
->eventmgr
;
142 if (eventmgr
!= NULL
|| eventmgr
->pp_eventmgr_fini
!= NULL
)
143 eventmgr
->pp_eventmgr_fini(eventmgr
);
145 smumgr
= pp_handle
->smu_mgr
;
147 if (smumgr
!= NULL
|| smumgr
->smumgr_funcs
!= NULL
||
148 smumgr
->smumgr_funcs
->smu_fini
!= NULL
)
149 smumgr
->smumgr_funcs
->smu_fini(smumgr
);
154 static bool pp_is_idle(void *handle
)
159 static int pp_wait_for_idle(void *handle
)
164 static int pp_sw_reset(void *handle
)
169 static void pp_print_status(void *handle
)
174 static int pp_set_clockgating_state(void *handle
,
175 enum amd_clockgating_state state
)
180 static int pp_set_powergating_state(void *handle
,
181 enum amd_powergating_state state
)
186 static int pp_suspend(void *handle
)
188 struct pp_instance
*pp_handle
;
189 struct pp_eventmgr
*eventmgr
;
190 struct pem_event_data event_data
= { {0} };
195 pp_handle
= (struct pp_instance
*)handle
;
196 eventmgr
= pp_handle
->eventmgr
;
197 pem_handle_event(eventmgr
, AMD_PP_EVENT_SUSPEND
, &event_data
);
201 static int pp_resume(void *handle
)
203 struct pp_instance
*pp_handle
;
204 struct pp_eventmgr
*eventmgr
;
205 struct pem_event_data event_data
= { {0} };
206 struct pp_smumgr
*smumgr
;
212 pp_handle
= (struct pp_instance
*)handle
;
213 smumgr
= pp_handle
->smu_mgr
;
215 if (smumgr
== NULL
|| smumgr
->smumgr_funcs
== NULL
||
216 smumgr
->smumgr_funcs
->start_smu
== NULL
)
219 ret
= smumgr
->smumgr_funcs
->start_smu(smumgr
);
221 printk(KERN_ERR
"[ powerplay ] smc start failed\n");
222 smumgr
->smumgr_funcs
->smu_fini(smumgr
);
226 eventmgr
= pp_handle
->eventmgr
;
227 pem_handle_event(eventmgr
, AMD_PP_EVENT_RESUME
, &event_data
);
232 const struct amd_ip_funcs pp_ip_funcs
= {
233 .early_init
= pp_early_init
,
235 .sw_init
= pp_sw_init
,
236 .sw_fini
= pp_sw_fini
,
237 .hw_init
= pp_hw_init
,
238 .hw_fini
= pp_hw_fini
,
239 .suspend
= pp_suspend
,
241 .is_idle
= pp_is_idle
,
242 .wait_for_idle
= pp_wait_for_idle
,
243 .soft_reset
= pp_sw_reset
,
244 .print_status
= pp_print_status
,
245 .set_clockgating_state
= pp_set_clockgating_state
,
246 .set_powergating_state
= pp_set_powergating_state
,
249 static int pp_dpm_load_fw(void *handle
)
254 static int pp_dpm_fw_loading_complete(void *handle
)
259 static int pp_dpm_force_performance_level(void *handle
,
260 enum amd_dpm_forced_level level
)
262 struct pp_instance
*pp_handle
;
263 struct pp_hwmgr
*hwmgr
;
268 pp_handle
= (struct pp_instance
*)handle
;
270 hwmgr
= pp_handle
->hwmgr
;
272 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
273 hwmgr
->hwmgr_func
->force_dpm_level
== NULL
)
276 hwmgr
->hwmgr_func
->force_dpm_level(hwmgr
, level
);
281 static enum amd_dpm_forced_level
pp_dpm_get_performance_level(
284 struct pp_hwmgr
*hwmgr
;
289 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
294 return (((struct pp_instance
*)handle
)->hwmgr
->dpm_level
);
297 static int pp_dpm_get_sclk(void *handle
, bool low
)
299 struct pp_hwmgr
*hwmgr
;
304 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
306 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
307 hwmgr
->hwmgr_func
->get_sclk
== NULL
)
310 return hwmgr
->hwmgr_func
->get_sclk(hwmgr
, low
);
313 static int pp_dpm_get_mclk(void *handle
, bool low
)
315 struct pp_hwmgr
*hwmgr
;
320 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
322 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
323 hwmgr
->hwmgr_func
->get_mclk
== NULL
)
326 return hwmgr
->hwmgr_func
->get_mclk(hwmgr
, low
);
329 static int pp_dpm_powergate_vce(void *handle
, bool gate
)
331 struct pp_hwmgr
*hwmgr
;
336 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
338 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
339 hwmgr
->hwmgr_func
->powergate_vce
== NULL
)
342 return hwmgr
->hwmgr_func
->powergate_vce(hwmgr
, gate
);
345 static int pp_dpm_powergate_uvd(void *handle
, bool gate
)
347 struct pp_hwmgr
*hwmgr
;
352 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
354 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
355 hwmgr
->hwmgr_func
->powergate_uvd
== NULL
)
358 return hwmgr
->hwmgr_func
->powergate_uvd(hwmgr
, gate
);
361 static enum PP_StateUILabel
power_state_convert(enum amd_pm_state_type state
)
364 case POWER_STATE_TYPE_BATTERY
:
365 return PP_StateUILabel_Battery
;
366 case POWER_STATE_TYPE_BALANCED
:
367 return PP_StateUILabel_Balanced
;
368 case POWER_STATE_TYPE_PERFORMANCE
:
369 return PP_StateUILabel_Performance
;
371 return PP_StateUILabel_None
;
375 int pp_dpm_dispatch_tasks(void *handle
, enum amd_pp_event event_id
, void *input
, void *output
)
378 struct pp_instance
*pp_handle
;
379 struct pem_event_data data
= { {0} };
381 pp_handle
= (struct pp_instance
*)handle
;
383 if (pp_handle
== NULL
)
387 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE
:
388 ret
= pem_handle_event(pp_handle
->eventmgr
, event_id
, &data
);
390 case AMD_PP_EVENT_ENABLE_USER_STATE
:
392 enum amd_pm_state_type ps
;
396 ps
= *(unsigned long *)input
;
398 data
.requested_ui_label
= power_state_convert(ps
);
399 ret
= pem_handle_event(pp_handle
->eventmgr
, event_id
, &data
);
408 enum amd_pm_state_type
pp_dpm_get_current_power_state(void *handle
)
410 struct pp_hwmgr
*hwmgr
;
411 struct pp_power_state
*state
;
416 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
418 if (hwmgr
== NULL
|| hwmgr
->current_ps
== NULL
)
421 state
= hwmgr
->current_ps
;
423 switch (state
->classification
.ui_label
) {
424 case PP_StateUILabel_Battery
:
425 return POWER_STATE_TYPE_BATTERY
;
426 case PP_StateUILabel_Balanced
:
427 return POWER_STATE_TYPE_BALANCED
;
428 case PP_StateUILabel_Performance
:
429 return POWER_STATE_TYPE_PERFORMANCE
;
431 return POWER_STATE_TYPE_DEFAULT
;
436 pp_debugfs_print_current_performance_level(void *handle
,
439 struct pp_hwmgr
*hwmgr
;
444 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
446 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
447 hwmgr
->hwmgr_func
->print_current_perforce_level
== NULL
)
450 hwmgr
->hwmgr_func
->print_current_perforce_level(hwmgr
, m
);
453 static int pp_dpm_set_fan_control_mode(void *handle
, uint32_t mode
)
455 struct pp_hwmgr
*hwmgr
;
460 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
462 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
463 hwmgr
->hwmgr_func
->set_fan_control_mode
== NULL
)
466 return hwmgr
->hwmgr_func
->set_fan_control_mode(hwmgr
, mode
);
469 static int pp_dpm_get_fan_control_mode(void *handle
)
471 struct pp_hwmgr
*hwmgr
;
476 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
478 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
479 hwmgr
->hwmgr_func
->get_fan_control_mode
== NULL
)
482 return hwmgr
->hwmgr_func
->get_fan_control_mode(hwmgr
);
485 static int pp_dpm_set_fan_speed_percent(void *handle
, uint32_t percent
)
487 struct pp_hwmgr
*hwmgr
;
492 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
494 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
495 hwmgr
->hwmgr_func
->set_fan_speed_percent
== NULL
)
498 return hwmgr
->hwmgr_func
->set_fan_speed_percent(hwmgr
, percent
);
501 static int pp_dpm_get_fan_speed_percent(void *handle
, uint32_t *speed
)
503 struct pp_hwmgr
*hwmgr
;
508 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
510 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
511 hwmgr
->hwmgr_func
->get_fan_speed_percent
== NULL
)
514 return hwmgr
->hwmgr_func
->get_fan_speed_percent(hwmgr
, speed
);
517 static int pp_dpm_get_temperature(void *handle
)
519 struct pp_hwmgr
*hwmgr
;
524 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
526 if (hwmgr
== NULL
|| hwmgr
->hwmgr_func
== NULL
||
527 hwmgr
->hwmgr_func
->get_temperature
== NULL
)
530 return hwmgr
->hwmgr_func
->get_temperature(hwmgr
);
533 const struct amd_powerplay_funcs pp_dpm_funcs
= {
534 .get_temperature
= pp_dpm_get_temperature
,
535 .load_firmware
= pp_dpm_load_fw
,
536 .wait_for_fw_loading_complete
= pp_dpm_fw_loading_complete
,
537 .force_performance_level
= pp_dpm_force_performance_level
,
538 .get_performance_level
= pp_dpm_get_performance_level
,
539 .get_current_power_state
= pp_dpm_get_current_power_state
,
540 .get_sclk
= pp_dpm_get_sclk
,
541 .get_mclk
= pp_dpm_get_mclk
,
542 .powergate_vce
= pp_dpm_powergate_vce
,
543 .powergate_uvd
= pp_dpm_powergate_uvd
,
544 .dispatch_tasks
= pp_dpm_dispatch_tasks
,
545 .print_current_performance_level
= pp_debugfs_print_current_performance_level
,
546 .set_fan_control_mode
= pp_dpm_set_fan_control_mode
,
547 .get_fan_control_mode
= pp_dpm_get_fan_control_mode
,
548 .set_fan_speed_percent
= pp_dpm_set_fan_speed_percent
,
549 .get_fan_speed_percent
= pp_dpm_get_fan_speed_percent
,
552 static int amd_pp_instance_init(struct amd_pp_init
*pp_init
,
553 struct amd_powerplay
*amd_pp
)
556 struct pp_instance
*handle
;
558 handle
= kzalloc(sizeof(struct pp_instance
), GFP_KERNEL
);
562 handle
->pp_valid
= PP_VALID
;
564 ret
= smum_init(pp_init
, handle
);
568 ret
= hwmgr_init(pp_init
, handle
);
572 ret
= eventmgr_init(handle
);
576 amd_pp
->pp_handle
= handle
;
580 hwmgr_fini(handle
->hwmgr
);
582 smum_fini(handle
->smu_mgr
);
588 static int amd_pp_instance_fini(void *handle
)
590 struct pp_instance
*instance
= (struct pp_instance
*)handle
;
592 if (instance
== NULL
)
595 eventmgr_fini(instance
->eventmgr
);
597 hwmgr_fini(instance
->hwmgr
);
599 smum_fini(instance
->smu_mgr
);
605 int amd_powerplay_init(struct amd_pp_init
*pp_init
,
606 struct amd_powerplay
*amd_pp
)
610 if (pp_init
== NULL
|| amd_pp
== NULL
)
613 ret
= amd_pp_instance_init(pp_init
, amd_pp
);
618 amd_pp
->ip_funcs
= &pp_ip_funcs
;
619 amd_pp
->pp_funcs
= &pp_dpm_funcs
;
624 int amd_powerplay_fini(void *handle
)
626 amd_pp_instance_fini(handle
);
631 /* export this function to DAL */
633 int amd_powerplay_display_configuration_change(void *handle
, const void *input
)
635 struct pp_hwmgr
*hwmgr
;
636 const struct amd_pp_display_configuration
*display_config
= input
;
638 PP_CHECK((struct pp_instance
*)handle
);
640 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
642 phm_store_dal_configuration_data(hwmgr
, display_config
);
647 int amd_powerplay_get_display_power_level(void *handle
,
648 struct amd_pp_dal_clock_info
*output
)
650 struct pp_hwmgr
*hwmgr
;
652 PP_CHECK((struct pp_instance
*)handle
);
657 hwmgr
= ((struct pp_instance
*)handle
)->hwmgr
;
659 return phm_get_dal_power_level(hwmgr
, output
);