drm/exynos: Stop using drm_framebuffer_unregister_private
[linux/fpc-iii.git] / drivers / gpu / drm / amd / powerplay / amd_powerplay.c
blobc81cf14127289b7746e987f580cb132188d86e4e
1 /*
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"
32 #include "pp_debug.h"
35 #define PP_CHECK(handle) \
36 do { \
37 if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
38 return -EINVAL; \
39 } while (0)
41 #define PP_CHECK_HW(hwmgr) \
42 do { \
43 if ((hwmgr) == NULL || (hwmgr)->hwmgr_func == NULL) \
44 return 0; \
45 } while (0)
47 static int pp_early_init(void *handle)
49 return 0;
52 static int pp_sw_init(void *handle)
54 struct pp_instance *pp_handle;
55 struct pp_hwmgr *hwmgr;
56 int ret = 0;
58 if (handle == NULL)
59 return -EINVAL;
61 pp_handle = (struct pp_instance *)handle;
62 hwmgr = pp_handle->hwmgr;
64 PP_CHECK_HW(hwmgr);
66 if (hwmgr->pptable_func == NULL ||
67 hwmgr->pptable_func->pptable_init == NULL ||
68 hwmgr->hwmgr_func->backend_init == NULL)
69 return -EINVAL;
71 ret = hwmgr->pptable_func->pptable_init(hwmgr);
72 if (ret)
73 goto err;
75 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
76 if (ret)
77 goto err1;
79 pr_info("amdgpu: powerplay initialized\n");
81 return 0;
82 err1:
83 if (hwmgr->pptable_func->pptable_fini)
84 hwmgr->pptable_func->pptable_fini(hwmgr);
85 err:
86 pr_err("amdgpu: powerplay initialization failed\n");
87 return ret;
90 static int pp_sw_fini(void *handle)
92 struct pp_instance *pp_handle;
93 struct pp_hwmgr *hwmgr;
94 int ret = 0;
96 if (handle == NULL)
97 return -EINVAL;
99 pp_handle = (struct pp_instance *)handle;
100 hwmgr = pp_handle->hwmgr;
102 PP_CHECK_HW(hwmgr);
104 if (hwmgr->hwmgr_func->backend_fini != NULL)
105 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
107 if (hwmgr->pptable_func->pptable_fini)
108 hwmgr->pptable_func->pptable_fini(hwmgr);
110 return ret;
113 static int pp_hw_init(void *handle)
115 struct pp_instance *pp_handle;
116 struct pp_smumgr *smumgr;
117 struct pp_eventmgr *eventmgr;
118 struct pp_hwmgr *hwmgr;
119 int ret = 0;
121 if (handle == NULL)
122 return -EINVAL;
124 pp_handle = (struct pp_instance *)handle;
125 smumgr = pp_handle->smu_mgr;
126 hwmgr = pp_handle->hwmgr;
128 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
129 smumgr->smumgr_funcs->smu_init == NULL ||
130 smumgr->smumgr_funcs->start_smu == NULL)
131 return -EINVAL;
133 ret = smumgr->smumgr_funcs->smu_init(smumgr);
134 if (ret) {
135 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
136 return ret;
139 ret = smumgr->smumgr_funcs->start_smu(smumgr);
140 if (ret) {
141 printk(KERN_ERR "[ powerplay ] smc start failed\n");
142 smumgr->smumgr_funcs->smu_fini(smumgr);
143 return ret;
146 PP_CHECK_HW(hwmgr);
148 hw_init_power_state_table(hwmgr);
150 eventmgr = pp_handle->eventmgr;
151 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
152 return -EINVAL;
154 ret = eventmgr->pp_eventmgr_init(eventmgr);
155 return 0;
158 static int pp_hw_fini(void *handle)
160 struct pp_instance *pp_handle;
161 struct pp_smumgr *smumgr;
162 struct pp_eventmgr *eventmgr;
164 if (handle == NULL)
165 return -EINVAL;
167 pp_handle = (struct pp_instance *)handle;
168 eventmgr = pp_handle->eventmgr;
170 if (eventmgr != NULL && eventmgr->pp_eventmgr_fini != NULL)
171 eventmgr->pp_eventmgr_fini(eventmgr);
173 smumgr = pp_handle->smu_mgr;
175 if (smumgr != NULL && smumgr->smumgr_funcs != NULL &&
176 smumgr->smumgr_funcs->smu_fini != NULL)
177 smumgr->smumgr_funcs->smu_fini(smumgr);
179 return 0;
182 static bool pp_is_idle(void *handle)
184 return false;
187 static int pp_wait_for_idle(void *handle)
189 return 0;
192 static int pp_sw_reset(void *handle)
194 return 0;
198 int amd_set_clockgating_by_smu(void *handle, uint32_t msg_id)
200 struct pp_hwmgr *hwmgr;
202 if (handle == NULL)
203 return -EINVAL;
205 hwmgr = ((struct pp_instance *)handle)->hwmgr;
207 PP_CHECK_HW(hwmgr);
209 if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
210 printk(KERN_INFO "%s was not implemented.\n", __func__);
211 return 0;
214 return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
217 static int pp_set_powergating_state(void *handle,
218 enum amd_powergating_state state)
220 struct pp_hwmgr *hwmgr;
222 if (handle == NULL)
223 return -EINVAL;
225 hwmgr = ((struct pp_instance *)handle)->hwmgr;
227 PP_CHECK_HW(hwmgr);
229 if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
230 printk(KERN_INFO "%s was not implemented.\n", __func__);
231 return 0;
234 /* Enable/disable GFX per cu powergating through SMU */
235 return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
236 state == AMD_PG_STATE_GATE ? true : false);
239 static int pp_suspend(void *handle)
241 struct pp_instance *pp_handle;
242 struct pp_eventmgr *eventmgr;
243 struct pem_event_data event_data = { {0} };
245 if (handle == NULL)
246 return -EINVAL;
248 pp_handle = (struct pp_instance *)handle;
249 eventmgr = pp_handle->eventmgr;
251 if (eventmgr != NULL)
252 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
253 return 0;
256 static int pp_resume(void *handle)
258 struct pp_instance *pp_handle;
259 struct pp_eventmgr *eventmgr;
260 struct pem_event_data event_data = { {0} };
261 struct pp_smumgr *smumgr;
262 int ret;
264 if (handle == NULL)
265 return -EINVAL;
267 pp_handle = (struct pp_instance *)handle;
268 smumgr = pp_handle->smu_mgr;
270 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
271 smumgr->smumgr_funcs->start_smu == NULL)
272 return -EINVAL;
274 ret = smumgr->smumgr_funcs->start_smu(smumgr);
275 if (ret) {
276 printk(KERN_ERR "[ powerplay ] smc start failed\n");
277 smumgr->smumgr_funcs->smu_fini(smumgr);
278 return ret;
281 eventmgr = pp_handle->eventmgr;
282 if (eventmgr != NULL)
283 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
285 return 0;
288 const struct amd_ip_funcs pp_ip_funcs = {
289 .name = "powerplay",
290 .early_init = pp_early_init,
291 .late_init = NULL,
292 .sw_init = pp_sw_init,
293 .sw_fini = pp_sw_fini,
294 .hw_init = pp_hw_init,
295 .hw_fini = pp_hw_fini,
296 .suspend = pp_suspend,
297 .resume = pp_resume,
298 .is_idle = pp_is_idle,
299 .wait_for_idle = pp_wait_for_idle,
300 .soft_reset = pp_sw_reset,
301 .set_clockgating_state = NULL,
302 .set_powergating_state = pp_set_powergating_state,
305 static int pp_dpm_load_fw(void *handle)
307 return 0;
310 static int pp_dpm_fw_loading_complete(void *handle)
312 return 0;
315 static int pp_dpm_force_performance_level(void *handle,
316 enum amd_dpm_forced_level level)
318 struct pp_instance *pp_handle;
319 struct pp_hwmgr *hwmgr;
321 if (handle == NULL)
322 return -EINVAL;
324 pp_handle = (struct pp_instance *)handle;
326 hwmgr = pp_handle->hwmgr;
328 PP_CHECK_HW(hwmgr);
330 if (hwmgr->hwmgr_func->force_dpm_level == NULL) {
331 printk(KERN_INFO "%s was not implemented.\n", __func__);
332 return 0;
335 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
337 return 0;
340 static enum amd_dpm_forced_level pp_dpm_get_performance_level(
341 void *handle)
343 struct pp_hwmgr *hwmgr;
345 if (handle == NULL)
346 return -EINVAL;
348 hwmgr = ((struct pp_instance *)handle)->hwmgr;
350 PP_CHECK_HW(hwmgr);
352 return (((struct pp_instance *)handle)->hwmgr->dpm_level);
355 static int pp_dpm_get_sclk(void *handle, bool low)
357 struct pp_hwmgr *hwmgr;
359 if (handle == NULL)
360 return -EINVAL;
362 hwmgr = ((struct pp_instance *)handle)->hwmgr;
364 PP_CHECK_HW(hwmgr);
366 if (hwmgr->hwmgr_func->get_sclk == NULL) {
367 printk(KERN_INFO "%s was not implemented.\n", __func__);
368 return 0;
371 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
374 static int pp_dpm_get_mclk(void *handle, bool low)
376 struct pp_hwmgr *hwmgr;
378 if (handle == NULL)
379 return -EINVAL;
381 hwmgr = ((struct pp_instance *)handle)->hwmgr;
383 PP_CHECK_HW(hwmgr);
385 if (hwmgr->hwmgr_func->get_mclk == NULL) {
386 printk(KERN_INFO "%s was not implemented.\n", __func__);
387 return 0;
390 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
393 static int pp_dpm_powergate_vce(void *handle, bool gate)
395 struct pp_hwmgr *hwmgr;
397 if (handle == NULL)
398 return -EINVAL;
400 hwmgr = ((struct pp_instance *)handle)->hwmgr;
402 PP_CHECK_HW(hwmgr);
404 if (hwmgr->hwmgr_func->powergate_vce == NULL) {
405 printk(KERN_INFO "%s was not implemented.\n", __func__);
406 return 0;
409 return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
412 static int pp_dpm_powergate_uvd(void *handle, bool gate)
414 struct pp_hwmgr *hwmgr;
416 if (handle == NULL)
417 return -EINVAL;
419 hwmgr = ((struct pp_instance *)handle)->hwmgr;
421 PP_CHECK_HW(hwmgr);
423 if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
424 printk(KERN_INFO "%s was not implemented.\n", __func__);
425 return 0;
428 return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
431 static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
433 switch (state) {
434 case POWER_STATE_TYPE_BATTERY:
435 return PP_StateUILabel_Battery;
436 case POWER_STATE_TYPE_BALANCED:
437 return PP_StateUILabel_Balanced;
438 case POWER_STATE_TYPE_PERFORMANCE:
439 return PP_StateUILabel_Performance;
440 default:
441 return PP_StateUILabel_None;
445 static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id,
446 void *input, void *output)
448 int ret = 0;
449 struct pp_instance *pp_handle;
450 struct pem_event_data data = { {0} };
452 pp_handle = (struct pp_instance *)handle;
454 if (pp_handle == NULL)
455 return -EINVAL;
457 if (pp_handle->eventmgr == NULL)
458 return 0;
460 switch (event_id) {
461 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
462 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
463 break;
464 case AMD_PP_EVENT_ENABLE_USER_STATE:
466 enum amd_pm_state_type ps;
468 if (input == NULL)
469 return -EINVAL;
470 ps = *(unsigned long *)input;
472 data.requested_ui_label = power_state_convert(ps);
473 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
474 break;
476 case AMD_PP_EVENT_COMPLETE_INIT:
477 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
478 break;
479 case AMD_PP_EVENT_READJUST_POWER_STATE:
480 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
481 break;
482 default:
483 break;
485 return ret;
488 static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
490 struct pp_hwmgr *hwmgr;
491 struct pp_power_state *state;
493 if (handle == NULL)
494 return -EINVAL;
496 hwmgr = ((struct pp_instance *)handle)->hwmgr;
498 if (hwmgr == NULL || hwmgr->current_ps == NULL)
499 return -EINVAL;
501 state = hwmgr->current_ps;
503 switch (state->classification.ui_label) {
504 case PP_StateUILabel_Battery:
505 return POWER_STATE_TYPE_BATTERY;
506 case PP_StateUILabel_Balanced:
507 return POWER_STATE_TYPE_BALANCED;
508 case PP_StateUILabel_Performance:
509 return POWER_STATE_TYPE_PERFORMANCE;
510 default:
511 if (state->classification.flags & PP_StateClassificationFlag_Boot)
512 return POWER_STATE_TYPE_INTERNAL_BOOT;
513 else
514 return POWER_STATE_TYPE_DEFAULT;
518 static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
520 struct pp_hwmgr *hwmgr;
522 if (handle == NULL)
523 return -EINVAL;
525 hwmgr = ((struct pp_instance *)handle)->hwmgr;
527 PP_CHECK_HW(hwmgr);
529 if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
530 printk(KERN_INFO "%s was not implemented.\n", __func__);
531 return 0;
534 return hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
537 static int pp_dpm_get_fan_control_mode(void *handle)
539 struct pp_hwmgr *hwmgr;
541 if (handle == NULL)
542 return -EINVAL;
544 hwmgr = ((struct pp_instance *)handle)->hwmgr;
546 PP_CHECK_HW(hwmgr);
548 if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
549 printk(KERN_INFO "%s was not implemented.\n", __func__);
550 return 0;
553 return hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
556 static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
558 struct pp_hwmgr *hwmgr;
560 if (handle == NULL)
561 return -EINVAL;
563 hwmgr = ((struct pp_instance *)handle)->hwmgr;
565 PP_CHECK_HW(hwmgr);
567 if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
568 printk(KERN_INFO "%s was not implemented.\n", __func__);
569 return 0;
572 return hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
575 static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
577 struct pp_hwmgr *hwmgr;
579 if (handle == NULL)
580 return -EINVAL;
582 hwmgr = ((struct pp_instance *)handle)->hwmgr;
584 PP_CHECK_HW(hwmgr);
586 if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
587 printk(KERN_INFO "%s was not implemented.\n", __func__);
588 return 0;
591 return hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
594 static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
596 struct pp_hwmgr *hwmgr;
598 if (handle == NULL)
599 return -EINVAL;
601 hwmgr = ((struct pp_instance *)handle)->hwmgr;
603 PP_CHECK_HW(hwmgr);
605 if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
606 return -EINVAL;
608 return hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
611 static int pp_dpm_get_temperature(void *handle)
613 struct pp_hwmgr *hwmgr;
615 if (handle == NULL)
616 return -EINVAL;
618 hwmgr = ((struct pp_instance *)handle)->hwmgr;
620 PP_CHECK_HW(hwmgr);
622 if (hwmgr->hwmgr_func->get_temperature == NULL) {
623 printk(KERN_INFO "%s was not implemented.\n", __func__);
624 return 0;
627 return hwmgr->hwmgr_func->get_temperature(hwmgr);
630 static int pp_dpm_get_pp_num_states(void *handle,
631 struct pp_states_info *data)
633 struct pp_hwmgr *hwmgr;
634 int i;
636 if (!handle)
637 return -EINVAL;
639 hwmgr = ((struct pp_instance *)handle)->hwmgr;
641 if (hwmgr == NULL || hwmgr->ps == NULL)
642 return -EINVAL;
644 data->nums = hwmgr->num_ps;
646 for (i = 0; i < hwmgr->num_ps; i++) {
647 struct pp_power_state *state = (struct pp_power_state *)
648 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
649 switch (state->classification.ui_label) {
650 case PP_StateUILabel_Battery:
651 data->states[i] = POWER_STATE_TYPE_BATTERY;
652 break;
653 case PP_StateUILabel_Balanced:
654 data->states[i] = POWER_STATE_TYPE_BALANCED;
655 break;
656 case PP_StateUILabel_Performance:
657 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
658 break;
659 default:
660 if (state->classification.flags & PP_StateClassificationFlag_Boot)
661 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
662 else
663 data->states[i] = POWER_STATE_TYPE_DEFAULT;
667 return 0;
670 static int pp_dpm_get_pp_table(void *handle, char **table)
672 struct pp_hwmgr *hwmgr;
674 if (!handle)
675 return -EINVAL;
677 hwmgr = ((struct pp_instance *)handle)->hwmgr;
679 PP_CHECK_HW(hwmgr);
681 if (!hwmgr->soft_pp_table)
682 return -EINVAL;
684 *table = (char *)hwmgr->soft_pp_table;
686 return hwmgr->soft_pp_table_size;
689 static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
691 struct pp_hwmgr *hwmgr;
693 if (!handle)
694 return -EINVAL;
696 hwmgr = ((struct pp_instance *)handle)->hwmgr;
698 PP_CHECK_HW(hwmgr);
700 if (!hwmgr->hardcode_pp_table) {
701 hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
702 hwmgr->soft_pp_table_size,
703 GFP_KERNEL);
705 if (!hwmgr->hardcode_pp_table)
706 return -ENOMEM;
709 memcpy(hwmgr->hardcode_pp_table, buf, size);
711 hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
713 return amd_powerplay_reset(handle);
716 static int pp_dpm_force_clock_level(void *handle,
717 enum pp_clock_type type, uint32_t mask)
719 struct pp_hwmgr *hwmgr;
721 if (!handle)
722 return -EINVAL;
724 hwmgr = ((struct pp_instance *)handle)->hwmgr;
726 PP_CHECK_HW(hwmgr);
728 if (hwmgr->hwmgr_func->force_clock_level == NULL) {
729 printk(KERN_INFO "%s was not implemented.\n", __func__);
730 return 0;
733 return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
736 static int pp_dpm_print_clock_levels(void *handle,
737 enum pp_clock_type type, char *buf)
739 struct pp_hwmgr *hwmgr;
741 if (!handle)
742 return -EINVAL;
744 hwmgr = ((struct pp_instance *)handle)->hwmgr;
746 PP_CHECK_HW(hwmgr);
748 if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
749 printk(KERN_INFO "%s was not implemented.\n", __func__);
750 return 0;
752 return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
755 static int pp_dpm_get_sclk_od(void *handle)
757 struct pp_hwmgr *hwmgr;
759 if (!handle)
760 return -EINVAL;
762 hwmgr = ((struct pp_instance *)handle)->hwmgr;
764 PP_CHECK_HW(hwmgr);
766 if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
767 printk(KERN_INFO "%s was not implemented.\n", __func__);
768 return 0;
771 return hwmgr->hwmgr_func->get_sclk_od(hwmgr);
774 static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
776 struct pp_hwmgr *hwmgr;
778 if (!handle)
779 return -EINVAL;
781 hwmgr = ((struct pp_instance *)handle)->hwmgr;
783 PP_CHECK_HW(hwmgr);
785 if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
786 printk(KERN_INFO "%s was not implemented.\n", __func__);
787 return 0;
790 return hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
793 static int pp_dpm_get_mclk_od(void *handle)
795 struct pp_hwmgr *hwmgr;
797 if (!handle)
798 return -EINVAL;
800 hwmgr = ((struct pp_instance *)handle)->hwmgr;
802 PP_CHECK_HW(hwmgr);
804 if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
805 printk(KERN_INFO "%s was not implemented.\n", __func__);
806 return 0;
809 return hwmgr->hwmgr_func->get_mclk_od(hwmgr);
812 static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
814 struct pp_hwmgr *hwmgr;
816 if (!handle)
817 return -EINVAL;
819 hwmgr = ((struct pp_instance *)handle)->hwmgr;
821 PP_CHECK_HW(hwmgr);
823 if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
824 printk(KERN_INFO "%s was not implemented.\n", __func__);
825 return 0;
828 return hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
831 static int pp_dpm_read_sensor(void *handle, int idx, int32_t *value)
833 struct pp_hwmgr *hwmgr;
835 if (!handle)
836 return -EINVAL;
838 hwmgr = ((struct pp_instance *)handle)->hwmgr;
840 PP_CHECK_HW(hwmgr);
842 if (hwmgr->hwmgr_func->read_sensor == NULL) {
843 printk(KERN_INFO "%s was not implemented.\n", __func__);
844 return 0;
847 return hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value);
850 static struct amd_vce_state*
851 pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
853 struct pp_hwmgr *hwmgr;
855 if (handle) {
856 hwmgr = ((struct pp_instance *)handle)->hwmgr;
858 if (hwmgr && idx < hwmgr->num_vce_state_tables)
859 return &hwmgr->vce_states[idx];
862 return NULL;
865 const struct amd_powerplay_funcs pp_dpm_funcs = {
866 .get_temperature = pp_dpm_get_temperature,
867 .load_firmware = pp_dpm_load_fw,
868 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
869 .force_performance_level = pp_dpm_force_performance_level,
870 .get_performance_level = pp_dpm_get_performance_level,
871 .get_current_power_state = pp_dpm_get_current_power_state,
872 .get_sclk = pp_dpm_get_sclk,
873 .get_mclk = pp_dpm_get_mclk,
874 .powergate_vce = pp_dpm_powergate_vce,
875 .powergate_uvd = pp_dpm_powergate_uvd,
876 .dispatch_tasks = pp_dpm_dispatch_tasks,
877 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
878 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
879 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
880 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
881 .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
882 .get_pp_num_states = pp_dpm_get_pp_num_states,
883 .get_pp_table = pp_dpm_get_pp_table,
884 .set_pp_table = pp_dpm_set_pp_table,
885 .force_clock_level = pp_dpm_force_clock_level,
886 .print_clock_levels = pp_dpm_print_clock_levels,
887 .get_sclk_od = pp_dpm_get_sclk_od,
888 .set_sclk_od = pp_dpm_set_sclk_od,
889 .get_mclk_od = pp_dpm_get_mclk_od,
890 .set_mclk_od = pp_dpm_set_mclk_od,
891 .read_sensor = pp_dpm_read_sensor,
892 .get_vce_clock_state = pp_dpm_get_vce_clock_state,
895 static int amd_pp_instance_init(struct amd_pp_init *pp_init,
896 struct amd_powerplay *amd_pp)
898 int ret;
899 struct pp_instance *handle;
901 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
902 if (handle == NULL)
903 return -ENOMEM;
905 handle->pp_valid = PP_VALID;
907 ret = smum_init(pp_init, handle);
908 if (ret)
909 goto fail_smum;
912 amd_pp->pp_handle = handle;
914 if ((amdgpu_dpm == 0)
915 || cgs_is_virtualization_enabled(pp_init->device))
916 return 0;
918 ret = hwmgr_init(pp_init, handle);
919 if (ret)
920 goto fail_hwmgr;
922 ret = eventmgr_init(handle);
923 if (ret)
924 goto fail_eventmgr;
926 return 0;
928 fail_eventmgr:
929 hwmgr_fini(handle->hwmgr);
930 fail_hwmgr:
931 smum_fini(handle->smu_mgr);
932 fail_smum:
933 kfree(handle);
934 return ret;
937 static int amd_pp_instance_fini(void *handle)
939 struct pp_instance *instance = (struct pp_instance *)handle;
941 if (instance == NULL)
942 return -EINVAL;
944 if ((amdgpu_dpm != 0)
945 && !cgs_is_virtualization_enabled(instance->smu_mgr->device)) {
946 eventmgr_fini(instance->eventmgr);
947 hwmgr_fini(instance->hwmgr);
950 smum_fini(instance->smu_mgr);
951 kfree(handle);
952 return 0;
955 int amd_powerplay_init(struct amd_pp_init *pp_init,
956 struct amd_powerplay *amd_pp)
958 int ret;
960 if (pp_init == NULL || amd_pp == NULL)
961 return -EINVAL;
963 ret = amd_pp_instance_init(pp_init, amd_pp);
965 if (ret)
966 return ret;
968 amd_pp->ip_funcs = &pp_ip_funcs;
969 amd_pp->pp_funcs = &pp_dpm_funcs;
971 return 0;
974 int amd_powerplay_fini(void *handle)
976 amd_pp_instance_fini(handle);
978 return 0;
981 int amd_powerplay_reset(void *handle)
983 struct pp_instance *instance = (struct pp_instance *)handle;
984 struct pp_eventmgr *eventmgr;
985 struct pem_event_data event_data = { {0} };
986 int ret;
988 if (instance == NULL)
989 return -EINVAL;
991 eventmgr = instance->eventmgr;
992 if (!eventmgr || !eventmgr->pp_eventmgr_fini)
993 return -EINVAL;
995 eventmgr->pp_eventmgr_fini(eventmgr);
997 ret = pp_sw_fini(handle);
998 if (ret)
999 return ret;
1001 kfree(instance->hwmgr->ps);
1003 ret = pp_sw_init(handle);
1004 if (ret)
1005 return ret;
1007 if ((amdgpu_dpm == 0)
1008 || cgs_is_virtualization_enabled(instance->smu_mgr->device))
1009 return 0;
1011 hw_init_power_state_table(instance->hwmgr);
1013 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
1014 return -EINVAL;
1016 ret = eventmgr->pp_eventmgr_init(eventmgr);
1017 if (ret)
1018 return ret;
1020 return pem_handle_event(eventmgr, AMD_PP_EVENT_COMPLETE_INIT, &event_data);
1023 /* export this function to DAL */
1025 int amd_powerplay_display_configuration_change(void *handle,
1026 const struct amd_pp_display_configuration *display_config)
1028 struct pp_hwmgr *hwmgr;
1030 PP_CHECK((struct pp_instance *)handle);
1032 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1034 PP_CHECK_HW(hwmgr);
1036 phm_store_dal_configuration_data(hwmgr, display_config);
1038 return 0;
1041 int amd_powerplay_get_display_power_level(void *handle,
1042 struct amd_pp_simple_clock_info *output)
1044 struct pp_hwmgr *hwmgr;
1046 PP_CHECK((struct pp_instance *)handle);
1048 if (output == NULL)
1049 return -EINVAL;
1051 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1053 PP_CHECK_HW(hwmgr);
1055 return phm_get_dal_power_level(hwmgr, output);
1058 int amd_powerplay_get_current_clocks(void *handle,
1059 struct amd_pp_clock_info *clocks)
1061 struct pp_hwmgr *hwmgr;
1062 struct amd_pp_simple_clock_info simple_clocks;
1063 struct pp_clock_info hw_clocks;
1065 PP_CHECK((struct pp_instance *)handle);
1067 if (clocks == NULL)
1068 return -EINVAL;
1070 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1072 PP_CHECK_HW(hwmgr);
1074 phm_get_dal_power_level(hwmgr, &simple_clocks);
1076 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_PowerContainment)) {
1077 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment))
1078 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetPowerContainmentClockInfo", return -1);
1079 } else {
1080 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_Activity))
1081 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetClockInfo", return -1);
1084 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1085 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1086 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1087 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1088 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1089 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1091 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1092 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1094 clocks->max_clocks_state = simple_clocks.level;
1096 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
1097 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1098 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1101 return 0;
1105 int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
1107 int result = -1;
1109 struct pp_hwmgr *hwmgr;
1111 PP_CHECK((struct pp_instance *)handle);
1113 if (clocks == NULL)
1114 return -EINVAL;
1116 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1118 PP_CHECK_HW(hwmgr);
1120 result = phm_get_clock_by_type(hwmgr, type, clocks);
1122 return result;
1125 int amd_powerplay_get_display_mode_validation_clocks(void *handle,
1126 struct amd_pp_simple_clock_info *clocks)
1128 int result = -1;
1129 struct pp_hwmgr *hwmgr;
1131 PP_CHECK((struct pp_instance *)handle);
1133 if (clocks == NULL)
1134 return -EINVAL;
1136 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1138 PP_CHECK_HW(hwmgr);
1140 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1141 result = phm_get_max_high_clocks(hwmgr, clocks);
1143 return result;