blob: adb6e7b9280ce48650b4bb287a1b122e4c06002b [file] [log] [blame]
Alex Deucher1f7371b2015-12-02 17:46:21 -05001/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 */
Huang Rui7bd55422016-12-26 14:05:30 +080023#include "pp_debug.h"
Alex Deucher1f7371b2015-12-02 17:46:21 -050024#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/gfp.h>
Jammy Zhouac885b32015-07-21 17:43:02 +080027#include <linux/slab.h>
Alex Deucher1f7371b2015-12-02 17:46:21 -050028#include "amd_shared.h"
29#include "amd_powerplay.h"
Jammy Zhouac885b32015-07-21 17:43:02 +080030#include "pp_instance.h"
Rex Zhu577bbe02015-08-28 12:56:43 +080031#include "power_state.h"
Alex Deucher1f7371b2015-12-02 17:46:21 -050032
Rex Zhue5f23732017-09-29 13:57:54 +080033#define PP_DPM_DISABLED 0xCCCC
34
Rex Zhu6d07fe72017-09-25 18:51:50 +080035static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
36 void *input, void *output);
37
Rex Zhu1c863802016-12-28 19:43:23 +080038static inline int pp_check(struct pp_instance *handle)
39{
Rex Zhue1827a32017-09-28 16:12:51 +080040 if (handle == NULL)
Rex Zhu1c863802016-12-28 19:43:23 +080041 return -EINVAL;
Rex Zhua969e162015-12-29 13:56:03 +080042
Rex Zhub3b03052017-09-26 13:28:27 -040043 if (handle->hwmgr == NULL || handle->hwmgr->smumgr_funcs == NULL)
Rex Zhu1c863802016-12-28 19:43:23 +080044 return -EINVAL;
45
46 if (handle->pm_en == 0)
47 return PP_DPM_DISABLED;
48
Rex Zhub3b03052017-09-26 13:28:27 -040049 if (handle->hwmgr->hwmgr_func == NULL)
Rex Zhu1c863802016-12-28 19:43:23 +080050 return PP_DPM_DISABLED;
51
52 return 0;
53}
Rex Zhu7383bcb2016-03-30 11:35:50 +080054
Rex Zhu139a2852017-09-25 20:46:37 +080055static int amd_powerplay_create(struct amd_pp_init *pp_init,
56 void **handle)
57{
58 struct pp_instance *instance;
59
60 if (pp_init == NULL || handle == NULL)
61 return -EINVAL;
62
63 instance = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
64 if (instance == NULL)
65 return -ENOMEM;
66
Rex Zhu139a2852017-09-25 20:46:37 +080067 instance->chip_family = pp_init->chip_family;
68 instance->chip_id = pp_init->chip_id;
69 instance->pm_en = pp_init->pm_en;
70 instance->feature_mask = pp_init->feature_mask;
71 instance->device = pp_init->device;
72 mutex_init(&instance->pp_lock);
73 *handle = instance;
74 return 0;
75}
76
77static int amd_powerplay_destroy(void *handle)
78{
79 struct pp_instance *instance = (struct pp_instance *)handle;
80
Eric Huang7b38a492017-10-31 17:35:28 -040081 kfree(instance->hwmgr->hardcode_pp_table);
82 instance->hwmgr->hardcode_pp_table = NULL;
83
Rex Zhu139a2852017-09-25 20:46:37 +080084 kfree(instance->hwmgr);
85 instance->hwmgr = NULL;
86
87 kfree(instance);
88 instance = NULL;
89 return 0;
90}
91
Alex Deucher1f7371b2015-12-02 17:46:21 -050092static int pp_early_init(void *handle)
93{
Rex Zhu1c863802016-12-28 19:43:23 +080094 int ret;
Rex Zhu139a2852017-09-25 20:46:37 +080095 struct pp_instance *pp_handle = NULL;
96
97 pp_handle = cgs_register_pp_handle(handle, amd_powerplay_create);
98
99 if (!pp_handle)
100 return -EINVAL;
Rex Zhu1c863802016-12-28 19:43:23 +0800101
Rex Zhub3b03052017-09-26 13:28:27 -0400102 ret = hwmgr_early_init(pp_handle);
Rex Zhu1c863802016-12-28 19:43:23 +0800103 if (ret)
Rex Zhub3b03052017-09-26 13:28:27 -0400104 return -EINVAL;
Rex Zhu1c863802016-12-28 19:43:23 +0800105
Alex Deucher1f7371b2015-12-02 17:46:21 -0500106 return 0;
107}
108
109static int pp_sw_init(void *handle)
110{
Rex Zhub3b03052017-09-26 13:28:27 -0400111 struct pp_hwmgr *hwmgr;
Jammy Zhou3bace352015-07-21 21:18:15 +0800112 int ret = 0;
Rex Zhu1c863802016-12-28 19:43:23 +0800113 struct pp_instance *pp_handle = (struct pp_instance *)handle;
Jammy Zhou3bace352015-07-21 21:18:15 +0800114
Rex Zhu1c863802016-12-28 19:43:23 +0800115 ret = pp_check(pp_handle);
Jammy Zhou3bace352015-07-21 21:18:15 +0800116
Rex Zhue5f23732017-09-29 13:57:54 +0800117 if (ret >= 0) {
Rex Zhub3b03052017-09-26 13:28:27 -0400118 hwmgr = pp_handle->hwmgr;
Jammy Zhou3bace352015-07-21 21:18:15 +0800119
Rex Zhub3b03052017-09-26 13:28:27 -0400120 if (hwmgr->smumgr_funcs->smu_init == NULL)
Rex Zhu1c863802016-12-28 19:43:23 +0800121 return -EINVAL;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800122
Rex Zhub3b03052017-09-26 13:28:27 -0400123 ret = hwmgr->smumgr_funcs->smu_init(hwmgr);
Jammy Zhou3bace352015-07-21 21:18:15 +0800124
pding9953b722017-10-26 09:30:38 +0800125 pr_debug("amdgpu: powerplay sw initialized\n");
Huang Rui167112b2016-12-14 16:26:54 +0800126 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800127 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500128}
129
130static int pp_sw_fini(void *handle)
131{
Rex Zhub3b03052017-09-26 13:28:27 -0400132 struct pp_hwmgr *hwmgr;
Jammy Zhou3bace352015-07-21 21:18:15 +0800133 int ret = 0;
Rex Zhu1c863802016-12-28 19:43:23 +0800134 struct pp_instance *pp_handle = (struct pp_instance *)handle;
Jammy Zhou3bace352015-07-21 21:18:15 +0800135
Rex Zhu1c863802016-12-28 19:43:23 +0800136 ret = pp_check(pp_handle);
Rex Zhue5f23732017-09-29 13:57:54 +0800137 if (ret >= 0) {
Rex Zhub3b03052017-09-26 13:28:27 -0400138 hwmgr = pp_handle->hwmgr;
Jammy Zhou3bace352015-07-21 21:18:15 +0800139
Rex Zhub3b03052017-09-26 13:28:27 -0400140 if (hwmgr->smumgr_funcs->smu_fini == NULL)
Rex Zhu1c863802016-12-28 19:43:23 +0800141 return -EINVAL;
Jammy Zhou3bace352015-07-21 21:18:15 +0800142
Rex Zhub3b03052017-09-26 13:28:27 -0400143 ret = hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
Rex Zhu1c863802016-12-28 19:43:23 +0800144 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800145 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500146}
147
148static int pp_hw_init(void *handle)
149{
Jammy Zhouac885b32015-07-21 17:43:02 +0800150 int ret = 0;
Rex Zhu1c863802016-12-28 19:43:23 +0800151 struct pp_instance *pp_handle = (struct pp_instance *)handle;
Rex Zhub3b03052017-09-26 13:28:27 -0400152 struct pp_hwmgr *hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800153
Rex Zhu1c863802016-12-28 19:43:23 +0800154 ret = pp_check(pp_handle);
Jammy Zhouac885b32015-07-21 17:43:02 +0800155
Rex Zhue5f23732017-09-29 13:57:54 +0800156 if (ret >= 0) {
Rex Zhub3b03052017-09-26 13:28:27 -0400157 hwmgr = pp_handle->hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800158
Rex Zhub3b03052017-09-26 13:28:27 -0400159 if (hwmgr->smumgr_funcs->start_smu == NULL)
Rex Zhu1c863802016-12-28 19:43:23 +0800160 return -EINVAL;
Jammy Zhouac885b32015-07-21 17:43:02 +0800161
Rex Zhub3b03052017-09-26 13:28:27 -0400162 if(hwmgr->smumgr_funcs->start_smu(pp_handle->hwmgr)) {
Rex Zhu1c863802016-12-28 19:43:23 +0800163 pr_err("smc start failed\n");
Rex Zhub3b03052017-09-26 13:28:27 -0400164 hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
Ingo Molnared7158b2018-02-22 10:54:55 +0100165 return -EINVAL;
Rex Zhu1c863802016-12-28 19:43:23 +0800166 }
167 if (ret == PP_DPM_DISABLED)
Rex Zhue5f23732017-09-29 13:57:54 +0800168 goto exit;
169 ret = hwmgr_hw_init(pp_handle);
170 if (ret)
171 goto exit;
Jammy Zhouac885b32015-07-21 17:43:02 +0800172 }
Rex Zhue5f23732017-09-29 13:57:54 +0800173 return ret;
174exit:
Rex Zhu1c863802016-12-28 19:43:23 +0800175 pp_handle->pm_en = 0;
Rex Zhue5f23732017-09-29 13:57:54 +0800176 cgs_notify_dpm_enabled(hwmgr->device, false);
177 return 0;
178
Alex Deucher1f7371b2015-12-02 17:46:21 -0500179}
180
181static int pp_hw_fini(void *handle)
182{
Rex Zhu1c863802016-12-28 19:43:23 +0800183 struct pp_instance *pp_handle = (struct pp_instance *)handle;
184 int ret = 0;
Jammy Zhouac885b32015-07-21 17:43:02 +0800185
Rex Zhu1c863802016-12-28 19:43:23 +0800186 ret = pp_check(pp_handle);
Rex Zhudf1e6392017-09-01 13:46:20 +0800187 if (ret == 0)
Rex Zhu1c863802016-12-28 19:43:23 +0800188 hwmgr_hw_fini(pp_handle);
Rex Zhudf1e6392017-09-01 13:46:20 +0800189
Alex Deucher1f7371b2015-12-02 17:46:21 -0500190 return 0;
191}
192
Rex Zhu6d07fe72017-09-25 18:51:50 +0800193static int pp_late_init(void *handle)
194{
195 struct pp_instance *pp_handle = (struct pp_instance *)handle;
196 int ret = 0;
197
198 ret = pp_check(pp_handle);
199 if (ret == 0)
200 pp_dpm_dispatch_tasks(pp_handle,
201 AMD_PP_TASK_COMPLETE_INIT, NULL, NULL);
202
203 return 0;
204}
205
Rex Zhu139a2852017-09-25 20:46:37 +0800206static void pp_late_fini(void *handle)
207{
208 amd_powerplay_destroy(handle);
209}
210
211
Alex Deucher1f7371b2015-12-02 17:46:21 -0500212static bool pp_is_idle(void *handle)
213{
Edward O'Callaghaned5121a2016-07-12 10:17:52 +1000214 return false;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500215}
216
217static int pp_wait_for_idle(void *handle)
218{
219 return 0;
220}
221
222static int pp_sw_reset(void *handle)
223{
224 return 0;
225}
226
Alex Deucher1f7371b2015-12-02 17:46:21 -0500227static int pp_set_powergating_state(void *handle,
228 enum amd_powergating_state state)
229{
Eric Huang65f85e72016-02-11 15:54:45 -0500230 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800231 struct pp_instance *pp_handle = (struct pp_instance *)handle;
232 int ret = 0;
Eric Huang65f85e72016-02-11 15:54:45 -0500233
Rex Zhu1c863802016-12-28 19:43:23 +0800234 ret = pp_check(pp_handle);
Eric Huang65f85e72016-02-11 15:54:45 -0500235
Rex Zhuae979882017-09-29 14:36:15 +0800236 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800237 return ret;
Eric Huang65f85e72016-02-11 15:54:45 -0500238
Rex Zhu1c863802016-12-28 19:43:23 +0800239 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800240
241 if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800242 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800243 return 0;
244 }
Eric Huang65f85e72016-02-11 15:54:45 -0500245
246 /* Enable/disable GFX per cu powergating through SMU */
247 return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
Andrew F. Davis93a4aec2017-03-15 11:20:24 -0500248 state == AMD_PG_STATE_GATE);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500249}
250
251static int pp_suspend(void *handle)
252{
Rex Zhu1c863802016-12-28 19:43:23 +0800253 struct pp_instance *pp_handle = (struct pp_instance *)handle;
254 int ret = 0;
Rex Zhu577bbe02015-08-28 12:56:43 +0800255
Rex Zhu1c863802016-12-28 19:43:23 +0800256 ret = pp_check(pp_handle);
Rex Zhue5f23732017-09-29 13:57:54 +0800257 if (ret == 0)
258 hwmgr_hw_suspend(pp_handle);
259 return 0;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500260}
261
262static int pp_resume(void *handle)
263{
Rex Zhub3b03052017-09-26 13:28:27 -0400264 struct pp_hwmgr *hwmgr;
Rex Zhue5f23732017-09-29 13:57:54 +0800265 int ret;
Rex Zhu1c863802016-12-28 19:43:23 +0800266 struct pp_instance *pp_handle = (struct pp_instance *)handle;
Rex Zhu577bbe02015-08-28 12:56:43 +0800267
Rex Zhue5f23732017-09-29 13:57:54 +0800268 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800269
Rex Zhue5f23732017-09-29 13:57:54 +0800270 if (ret < 0)
271 return ret;
Rex Zhu1c863802016-12-28 19:43:23 +0800272
Rex Zhub3b03052017-09-26 13:28:27 -0400273 hwmgr = pp_handle->hwmgr;
Rex Zhue0b71a72015-12-29 10:25:19 +0800274
Rex Zhub3b03052017-09-26 13:28:27 -0400275 if (hwmgr->smumgr_funcs->start_smu == NULL)
Rex Zhue0b71a72015-12-29 10:25:19 +0800276 return -EINVAL;
277
Rex Zhue5f23732017-09-29 13:57:54 +0800278 if (hwmgr->smumgr_funcs->start_smu(pp_handle->hwmgr)) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800279 pr_err("smc start failed\n");
Rex Zhub3b03052017-09-26 13:28:27 -0400280 hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
Rex Zhue5f23732017-09-29 13:57:54 +0800281 return -EINVAL;
Rex Zhue0b71a72015-12-29 10:25:19 +0800282 }
283
Rex Zhue5f23732017-09-29 13:57:54 +0800284 if (ret == PP_DPM_DISABLED)
Monk Liu8fdf2692017-01-25 15:55:30 +0800285 return 0;
Rex Zhu1c863802016-12-28 19:43:23 +0800286
Rex Zhudf1e6392017-09-01 13:46:20 +0800287 return hwmgr_hw_resume(pp_handle);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500288}
289
290const struct amd_ip_funcs pp_ip_funcs = {
Tom St Denis88a907d2016-05-04 14:28:35 -0400291 .name = "powerplay",
Alex Deucher1f7371b2015-12-02 17:46:21 -0500292 .early_init = pp_early_init,
Rex Zhu6d07fe72017-09-25 18:51:50 +0800293 .late_init = pp_late_init,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500294 .sw_init = pp_sw_init,
295 .sw_fini = pp_sw_fini,
296 .hw_init = pp_hw_init,
297 .hw_fini = pp_hw_fini,
Rex Zhu139a2852017-09-25 20:46:37 +0800298 .late_fini = pp_late_fini,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500299 .suspend = pp_suspend,
300 .resume = pp_resume,
301 .is_idle = pp_is_idle,
302 .wait_for_idle = pp_wait_for_idle,
303 .soft_reset = pp_sw_reset,
Rex Zhu465f96e2016-09-18 16:52:03 +0800304 .set_clockgating_state = NULL,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500305 .set_powergating_state = pp_set_powergating_state,
306};
307
308static int pp_dpm_load_fw(void *handle)
309{
310 return 0;
311}
312
313static int pp_dpm_fw_loading_complete(void *handle)
314{
315 return 0;
316}
317
Rex Zhu3811f8f2017-09-26 13:39:38 +0800318static int pp_set_clockgating_by_smu(void *handle, uint32_t msg_id)
319{
320 struct pp_hwmgr *hwmgr;
321 struct pp_instance *pp_handle = (struct pp_instance *)handle;
322 int ret = 0;
323
324 ret = pp_check(pp_handle);
325
326 if (ret)
327 return ret;
328
329 hwmgr = pp_handle->hwmgr;
330
331 if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
332 pr_info("%s was not implemented.\n", __func__);
333 return 0;
334 }
335
336 return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
337}
338
Rex Zhu9947f702017-08-29 16:08:56 +0800339static void pp_dpm_en_umd_pstate(struct pp_hwmgr *hwmgr,
340 enum amd_dpm_forced_level *level)
341{
342 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
343 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
344 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
345 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
346
347 if (!(hwmgr->dpm_level & profile_mode_mask)) {
348 /* enter umd pstate, save current level, disable gfx cg*/
349 if (*level & profile_mode_mask) {
350 hwmgr->saved_dpm_level = hwmgr->dpm_level;
351 hwmgr->en_umd_pstate = true;
352 cgs_set_clockgating_state(hwmgr->device,
353 AMD_IP_BLOCK_TYPE_GFX,
354 AMD_CG_STATE_UNGATE);
355 cgs_set_powergating_state(hwmgr->device,
356 AMD_IP_BLOCK_TYPE_GFX,
357 AMD_PG_STATE_UNGATE);
358 }
359 } else {
360 /* exit umd pstate, restore level, enable gfx cg*/
361 if (!(*level & profile_mode_mask)) {
362 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
363 *level = hwmgr->saved_dpm_level;
364 hwmgr->en_umd_pstate = false;
365 cgs_set_clockgating_state(hwmgr->device,
366 AMD_IP_BLOCK_TYPE_GFX,
367 AMD_CG_STATE_GATE);
368 cgs_set_powergating_state(hwmgr->device,
369 AMD_IP_BLOCK_TYPE_GFX,
370 AMD_PG_STATE_GATE);
371 }
372 }
373}
374
Alex Deucher1f7371b2015-12-02 17:46:21 -0500375static int pp_dpm_force_performance_level(void *handle,
376 enum amd_dpm_forced_level level)
377{
Rex Zhu577bbe02015-08-28 12:56:43 +0800378 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800379 struct pp_instance *pp_handle = (struct pp_instance *)handle;
380 int ret = 0;
Rex Zhu577bbe02015-08-28 12:56:43 +0800381
Rex Zhu1c863802016-12-28 19:43:23 +0800382 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800383
Rex Zhuae979882017-09-29 14:36:15 +0800384 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800385 return ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800386
387 hwmgr = pp_handle->hwmgr;
388
Rex Zhu9947f702017-08-29 16:08:56 +0800389 if (level == hwmgr->dpm_level)
390 return 0;
391
Rex Zhu2a507102017-02-20 17:07:36 +0800392 mutex_lock(&pp_handle->pp_lock);
Rex Zhu9947f702017-08-29 16:08:56 +0800393 pp_dpm_en_umd_pstate(hwmgr, &level);
394 hwmgr->request_dpm_level = level;
Rex Zhudf1e6392017-09-01 13:46:20 +0800395 hwmgr_handle_task(pp_handle, AMD_PP_TASK_READJUST_POWER_STATE, NULL, NULL);
Rex Zhu2a507102017-02-20 17:07:36 +0800396 mutex_unlock(&pp_handle->pp_lock);
Rex Zhu8621bbb2017-12-18 19:48:00 +0800397
Alex Deucher1f7371b2015-12-02 17:46:21 -0500398 return 0;
399}
Rex Zhu577bbe02015-08-28 12:56:43 +0800400
Alex Deucher1f7371b2015-12-02 17:46:21 -0500401static enum amd_dpm_forced_level pp_dpm_get_performance_level(
402 void *handle)
403{
Rex Zhu577bbe02015-08-28 12:56:43 +0800404 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800405 struct pp_instance *pp_handle = (struct pp_instance *)handle;
406 int ret = 0;
Rex Zhu2a507102017-02-20 17:07:36 +0800407 enum amd_dpm_forced_level level;
Rex Zhu577bbe02015-08-28 12:56:43 +0800408
Rex Zhu1c863802016-12-28 19:43:23 +0800409 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800410
Rex Zhuae979882017-09-29 14:36:15 +0800411 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800412 return ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800413
Rex Zhu1c863802016-12-28 19:43:23 +0800414 hwmgr = pp_handle->hwmgr;
Rex Zhu2a507102017-02-20 17:07:36 +0800415 mutex_lock(&pp_handle->pp_lock);
416 level = hwmgr->dpm_level;
417 mutex_unlock(&pp_handle->pp_lock);
418 return level;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500419}
Rex Zhu577bbe02015-08-28 12:56:43 +0800420
Rex Zhuf93f0c32017-09-06 16:08:03 +0800421static uint32_t pp_dpm_get_sclk(void *handle, bool low)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500422{
Rex Zhu577bbe02015-08-28 12:56:43 +0800423 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800424 struct pp_instance *pp_handle = (struct pp_instance *)handle;
425 int ret = 0;
Rex Zhuf93f0c32017-09-06 16:08:03 +0800426 uint32_t clk = 0;
Rex Zhu577bbe02015-08-28 12:56:43 +0800427
Rex Zhu1c863802016-12-28 19:43:23 +0800428 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800429
Rex Zhuae979882017-09-29 14:36:15 +0800430 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800431 return ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800432
Rex Zhu1c863802016-12-28 19:43:23 +0800433 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800434
435 if (hwmgr->hwmgr_func->get_sclk == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800436 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800437 return 0;
438 }
Rex Zhu2a507102017-02-20 17:07:36 +0800439 mutex_lock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800440 clk = hwmgr->hwmgr_func->get_sclk(hwmgr, low);
Rex Zhu2a507102017-02-20 17:07:36 +0800441 mutex_unlock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800442 return clk;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500443}
Rex Zhu577bbe02015-08-28 12:56:43 +0800444
Rex Zhuf93f0c32017-09-06 16:08:03 +0800445static uint32_t pp_dpm_get_mclk(void *handle, bool low)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500446{
Rex Zhu577bbe02015-08-28 12:56:43 +0800447 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800448 struct pp_instance *pp_handle = (struct pp_instance *)handle;
449 int ret = 0;
Rex Zhuf93f0c32017-09-06 16:08:03 +0800450 uint32_t clk = 0;
Rex Zhu577bbe02015-08-28 12:56:43 +0800451
Rex Zhu1c863802016-12-28 19:43:23 +0800452 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800453
Rex Zhuae979882017-09-29 14:36:15 +0800454 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800455 return ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800456
Rex Zhu1c863802016-12-28 19:43:23 +0800457 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800458
459 if (hwmgr->hwmgr_func->get_mclk == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800460 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800461 return 0;
462 }
Rex Zhu2a507102017-02-20 17:07:36 +0800463 mutex_lock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800464 clk = hwmgr->hwmgr_func->get_mclk(hwmgr, low);
Rex Zhu2a507102017-02-20 17:07:36 +0800465 mutex_unlock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800466 return clk;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500467}
Rex Zhu577bbe02015-08-28 12:56:43 +0800468
Rex Zhuf93f0c32017-09-06 16:08:03 +0800469static void pp_dpm_powergate_vce(void *handle, bool gate)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500470{
Rex Zhu577bbe02015-08-28 12:56:43 +0800471 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800472 struct pp_instance *pp_handle = (struct pp_instance *)handle;
473 int ret = 0;
Rex Zhu577bbe02015-08-28 12:56:43 +0800474
Rex Zhu1c863802016-12-28 19:43:23 +0800475 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800476
Rex Zhuae979882017-09-29 14:36:15 +0800477 if (ret)
Rex Zhuf93f0c32017-09-06 16:08:03 +0800478 return;
Rex Zhu577bbe02015-08-28 12:56:43 +0800479
Rex Zhu1c863802016-12-28 19:43:23 +0800480 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800481
482 if (hwmgr->hwmgr_func->powergate_vce == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800483 pr_info("%s was not implemented.\n", __func__);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800484 return;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800485 }
Rex Zhu2a507102017-02-20 17:07:36 +0800486 mutex_lock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800487 hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
Rex Zhu2a507102017-02-20 17:07:36 +0800488 mutex_unlock(&pp_handle->pp_lock);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500489}
Rex Zhu577bbe02015-08-28 12:56:43 +0800490
Rex Zhuf93f0c32017-09-06 16:08:03 +0800491static void pp_dpm_powergate_uvd(void *handle, bool gate)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500492{
Rex Zhu577bbe02015-08-28 12:56:43 +0800493 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800494 struct pp_instance *pp_handle = (struct pp_instance *)handle;
495 int ret = 0;
Rex Zhu577bbe02015-08-28 12:56:43 +0800496
Rex Zhu1c863802016-12-28 19:43:23 +0800497 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800498
Rex Zhuae979882017-09-29 14:36:15 +0800499 if (ret)
Rex Zhuf93f0c32017-09-06 16:08:03 +0800500 return;
Rex Zhu577bbe02015-08-28 12:56:43 +0800501
Rex Zhu1c863802016-12-28 19:43:23 +0800502 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800503
504 if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800505 pr_info("%s was not implemented.\n", __func__);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800506 return;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800507 }
Rex Zhu2a507102017-02-20 17:07:36 +0800508 mutex_lock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800509 hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
Rex Zhu2a507102017-02-20 17:07:36 +0800510 mutex_unlock(&pp_handle->pp_lock);
Rex Zhu577bbe02015-08-28 12:56:43 +0800511}
512
Rex Zhudf1e6392017-09-01 13:46:20 +0800513static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
Baoyou Xief8a4c112016-09-30 17:58:42 +0800514 void *input, void *output)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500515{
Rex Zhu577bbe02015-08-28 12:56:43 +0800516 int ret = 0;
Rex Zhu1c863802016-12-28 19:43:23 +0800517 struct pp_instance *pp_handle = (struct pp_instance *)handle;
Rex Zhu577bbe02015-08-28 12:56:43 +0800518
Rex Zhu1c863802016-12-28 19:43:23 +0800519 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800520
Rex Zhuae979882017-09-29 14:36:15 +0800521 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800522 return ret;
Rex Zhudf1e6392017-09-01 13:46:20 +0800523
Rex Zhu2a507102017-02-20 17:07:36 +0800524 mutex_lock(&pp_handle->pp_lock);
Rex Zhudf1e6392017-09-01 13:46:20 +0800525 ret = hwmgr_handle_task(pp_handle, task_id, input, output);
Rex Zhu2a507102017-02-20 17:07:36 +0800526 mutex_unlock(&pp_handle->pp_lock);
Rex Zhudf1e6392017-09-01 13:46:20 +0800527
Rex Zhu577bbe02015-08-28 12:56:43 +0800528 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500529}
Rex Zhu577bbe02015-08-28 12:56:43 +0800530
Baoyou Xief8a4c112016-09-30 17:58:42 +0800531static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500532{
Rex Zhu577bbe02015-08-28 12:56:43 +0800533 struct pp_hwmgr *hwmgr;
534 struct pp_power_state *state;
Rex Zhu1c863802016-12-28 19:43:23 +0800535 struct pp_instance *pp_handle = (struct pp_instance *)handle;
536 int ret = 0;
Rex Zhu2a507102017-02-20 17:07:36 +0800537 enum amd_pm_state_type pm_type;
Rex Zhu577bbe02015-08-28 12:56:43 +0800538
Rex Zhu1c863802016-12-28 19:43:23 +0800539 ret = pp_check(pp_handle);
Rex Zhu577bbe02015-08-28 12:56:43 +0800540
Rex Zhuae979882017-09-29 14:36:15 +0800541 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800542 return ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800543
Rex Zhu1c863802016-12-28 19:43:23 +0800544 hwmgr = pp_handle->hwmgr;
545
546 if (hwmgr->current_ps == NULL)
Rex Zhu577bbe02015-08-28 12:56:43 +0800547 return -EINVAL;
548
Rex Zhu2a507102017-02-20 17:07:36 +0800549 mutex_lock(&pp_handle->pp_lock);
550
Rex Zhu577bbe02015-08-28 12:56:43 +0800551 state = hwmgr->current_ps;
552
553 switch (state->classification.ui_label) {
554 case PP_StateUILabel_Battery:
Rex Zhu2a507102017-02-20 17:07:36 +0800555 pm_type = POWER_STATE_TYPE_BATTERY;
Dan Carpenter0f987cd2017-04-03 21:41:47 +0300556 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800557 case PP_StateUILabel_Balanced:
Rex Zhu2a507102017-02-20 17:07:36 +0800558 pm_type = POWER_STATE_TYPE_BALANCED;
Dan Carpenter0f987cd2017-04-03 21:41:47 +0300559 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800560 case PP_StateUILabel_Performance:
Rex Zhu2a507102017-02-20 17:07:36 +0800561 pm_type = POWER_STATE_TYPE_PERFORMANCE;
Dan Carpenter0f987cd2017-04-03 21:41:47 +0300562 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800563 default:
Eric Huangf3898ea2015-12-11 16:24:34 -0500564 if (state->classification.flags & PP_StateClassificationFlag_Boot)
Rex Zhu2a507102017-02-20 17:07:36 +0800565 pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
Eric Huangf3898ea2015-12-11 16:24:34 -0500566 else
Rex Zhu2a507102017-02-20 17:07:36 +0800567 pm_type = POWER_STATE_TYPE_DEFAULT;
Dan Carpenter0f987cd2017-04-03 21:41:47 +0300568 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800569 }
Rex Zhu2a507102017-02-20 17:07:36 +0800570 mutex_unlock(&pp_handle->pp_lock);
571
572 return pm_type;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500573}
Rex Zhu577bbe02015-08-28 12:56:43 +0800574
Rex Zhuf93f0c32017-09-06 16:08:03 +0800575static void pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
Rex Zhucac9a192015-10-16 11:48:21 +0800576{
577 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800578 struct pp_instance *pp_handle = (struct pp_instance *)handle;
579 int ret = 0;
Rex Zhucac9a192015-10-16 11:48:21 +0800580
Rex Zhu1c863802016-12-28 19:43:23 +0800581 ret = pp_check(pp_handle);
Rex Zhucac9a192015-10-16 11:48:21 +0800582
Rex Zhuae979882017-09-29 14:36:15 +0800583 if (ret)
Rex Zhuf93f0c32017-09-06 16:08:03 +0800584 return;
Rex Zhucac9a192015-10-16 11:48:21 +0800585
Rex Zhu1c863802016-12-28 19:43:23 +0800586 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800587
588 if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800589 pr_info("%s was not implemented.\n", __func__);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800590 return;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800591 }
Rex Zhu2a507102017-02-20 17:07:36 +0800592 mutex_lock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800593 hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
Rex Zhu2a507102017-02-20 17:07:36 +0800594 mutex_unlock(&pp_handle->pp_lock);
Rex Zhucac9a192015-10-16 11:48:21 +0800595}
596
Rex Zhuf93f0c32017-09-06 16:08:03 +0800597static uint32_t pp_dpm_get_fan_control_mode(void *handle)
Rex Zhucac9a192015-10-16 11:48:21 +0800598{
599 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800600 struct pp_instance *pp_handle = (struct pp_instance *)handle;
601 int ret = 0;
Rex Zhuf93f0c32017-09-06 16:08:03 +0800602 uint32_t mode = 0;
Rex Zhucac9a192015-10-16 11:48:21 +0800603
Rex Zhu1c863802016-12-28 19:43:23 +0800604 ret = pp_check(pp_handle);
Rex Zhucac9a192015-10-16 11:48:21 +0800605
Rex Zhuae979882017-09-29 14:36:15 +0800606 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800607 return ret;
Rex Zhucac9a192015-10-16 11:48:21 +0800608
Rex Zhu1c863802016-12-28 19:43:23 +0800609 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800610
611 if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800612 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800613 return 0;
614 }
Rex Zhu2a507102017-02-20 17:07:36 +0800615 mutex_lock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800616 mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
Rex Zhu2a507102017-02-20 17:07:36 +0800617 mutex_unlock(&pp_handle->pp_lock);
Rex Zhuf93f0c32017-09-06 16:08:03 +0800618 return mode;
Rex Zhucac9a192015-10-16 11:48:21 +0800619}
620
621static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
622{
623 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800624 struct pp_instance *pp_handle = (struct pp_instance *)handle;
625 int ret = 0;
Rex Zhucac9a192015-10-16 11:48:21 +0800626
Rex Zhu1c863802016-12-28 19:43:23 +0800627 ret = pp_check(pp_handle);
Rex Zhucac9a192015-10-16 11:48:21 +0800628
Rex Zhuae979882017-09-29 14:36:15 +0800629 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800630 return ret;
Rex Zhucac9a192015-10-16 11:48:21 +0800631
Rex Zhu1c863802016-12-28 19:43:23 +0800632 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800633
634 if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800635 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800636 return 0;
637 }
Rex Zhu2a507102017-02-20 17:07:36 +0800638 mutex_lock(&pp_handle->pp_lock);
639 ret = hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
640 mutex_unlock(&pp_handle->pp_lock);
641 return ret;
Rex Zhucac9a192015-10-16 11:48:21 +0800642}
643
644static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
645{
646 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800647 struct pp_instance *pp_handle = (struct pp_instance *)handle;
648 int ret = 0;
Rex Zhucac9a192015-10-16 11:48:21 +0800649
Rex Zhu1c863802016-12-28 19:43:23 +0800650 ret = pp_check(pp_handle);
Rex Zhucac9a192015-10-16 11:48:21 +0800651
Rex Zhuae979882017-09-29 14:36:15 +0800652 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800653 return ret;
Rex Zhucac9a192015-10-16 11:48:21 +0800654
Rex Zhu1c863802016-12-28 19:43:23 +0800655 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800656
657 if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800658 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800659 return 0;
660 }
Rex Zhucac9a192015-10-16 11:48:21 +0800661
Rex Zhu2a507102017-02-20 17:07:36 +0800662 mutex_lock(&pp_handle->pp_lock);
663 ret = hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
664 mutex_unlock(&pp_handle->pp_lock);
665 return ret;
Rex Zhucac9a192015-10-16 11:48:21 +0800666}
667
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300668static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
669{
670 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800671 struct pp_instance *pp_handle = (struct pp_instance *)handle;
672 int ret = 0;
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300673
Rex Zhu1c863802016-12-28 19:43:23 +0800674 ret = pp_check(pp_handle);
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300675
Rex Zhuae979882017-09-29 14:36:15 +0800676 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800677 return ret;
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300678
Rex Zhu1c863802016-12-28 19:43:23 +0800679 hwmgr = pp_handle->hwmgr;
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300680
681 if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
682 return -EINVAL;
683
Rex Zhu2a507102017-02-20 17:07:36 +0800684 mutex_lock(&pp_handle->pp_lock);
685 ret = hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
686 mutex_unlock(&pp_handle->pp_lock);
687 return ret;
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300688}
689
Rex Zhucac9a192015-10-16 11:48:21 +0800690static int pp_dpm_get_temperature(void *handle)
691{
692 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800693 struct pp_instance *pp_handle = (struct pp_instance *)handle;
694 int ret = 0;
Rex Zhucac9a192015-10-16 11:48:21 +0800695
Rex Zhu1c863802016-12-28 19:43:23 +0800696 ret = pp_check(pp_handle);
Rex Zhucac9a192015-10-16 11:48:21 +0800697
Rex Zhuae979882017-09-29 14:36:15 +0800698 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800699 return ret;
Rex Zhucac9a192015-10-16 11:48:21 +0800700
Rex Zhu1c863802016-12-28 19:43:23 +0800701 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800702
703 if (hwmgr->hwmgr_func->get_temperature == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800704 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800705 return 0;
706 }
Rex Zhu2a507102017-02-20 17:07:36 +0800707 mutex_lock(&pp_handle->pp_lock);
708 ret = hwmgr->hwmgr_func->get_temperature(hwmgr);
709 mutex_unlock(&pp_handle->pp_lock);
710 return ret;
Rex Zhucac9a192015-10-16 11:48:21 +0800711}
Rex Zhu577bbe02015-08-28 12:56:43 +0800712
Eric Huangf3898ea2015-12-11 16:24:34 -0500713static int pp_dpm_get_pp_num_states(void *handle,
714 struct pp_states_info *data)
715{
716 struct pp_hwmgr *hwmgr;
717 int i;
Rex Zhu1c863802016-12-28 19:43:23 +0800718 struct pp_instance *pp_handle = (struct pp_instance *)handle;
719 int ret = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500720
Evan Quan4dbda352017-12-28 14:37:58 +0800721 memset(data, 0, sizeof(*data));
722
Rex Zhu1c863802016-12-28 19:43:23 +0800723 ret = pp_check(pp_handle);
Eric Huangf3898ea2015-12-11 16:24:34 -0500724
Rex Zhuae979882017-09-29 14:36:15 +0800725 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800726 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500727
Rex Zhu1c863802016-12-28 19:43:23 +0800728 hwmgr = pp_handle->hwmgr;
729
730 if (hwmgr->ps == NULL)
Eric Huangf3898ea2015-12-11 16:24:34 -0500731 return -EINVAL;
732
Rex Zhu2a507102017-02-20 17:07:36 +0800733 mutex_lock(&pp_handle->pp_lock);
734
Eric Huangf3898ea2015-12-11 16:24:34 -0500735 data->nums = hwmgr->num_ps;
736
737 for (i = 0; i < hwmgr->num_ps; i++) {
738 struct pp_power_state *state = (struct pp_power_state *)
739 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
740 switch (state->classification.ui_label) {
741 case PP_StateUILabel_Battery:
742 data->states[i] = POWER_STATE_TYPE_BATTERY;
743 break;
744 case PP_StateUILabel_Balanced:
745 data->states[i] = POWER_STATE_TYPE_BALANCED;
746 break;
747 case PP_StateUILabel_Performance:
748 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
749 break;
750 default:
751 if (state->classification.flags & PP_StateClassificationFlag_Boot)
752 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
753 else
754 data->states[i] = POWER_STATE_TYPE_DEFAULT;
755 }
756 }
Rex Zhu2a507102017-02-20 17:07:36 +0800757 mutex_unlock(&pp_handle->pp_lock);
Eric Huangf3898ea2015-12-11 16:24:34 -0500758 return 0;
759}
760
761static int pp_dpm_get_pp_table(void *handle, char **table)
762{
763 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800764 struct pp_instance *pp_handle = (struct pp_instance *)handle;
765 int ret = 0;
Rex Zhu2a507102017-02-20 17:07:36 +0800766 int size = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500767
Rex Zhu1c863802016-12-28 19:43:23 +0800768 ret = pp_check(pp_handle);
Eric Huangf3898ea2015-12-11 16:24:34 -0500769
Rex Zhuae979882017-09-29 14:36:15 +0800770 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800771 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500772
Rex Zhu1c863802016-12-28 19:43:23 +0800773 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800774
Eric Huang4dcf9e62016-06-01 17:08:07 -0400775 if (!hwmgr->soft_pp_table)
776 return -EINVAL;
Eric Huangf3898ea2015-12-11 16:24:34 -0500777
Rex Zhu2a507102017-02-20 17:07:36 +0800778 mutex_lock(&pp_handle->pp_lock);
Eric Huang4dcf9e62016-06-01 17:08:07 -0400779 *table = (char *)hwmgr->soft_pp_table;
Rex Zhu2a507102017-02-20 17:07:36 +0800780 size = hwmgr->soft_pp_table_size;
781 mutex_unlock(&pp_handle->pp_lock);
782 return size;
Eric Huangf3898ea2015-12-11 16:24:34 -0500783}
784
Rex Zhuf685d712017-10-25 23:55:23 -0400785static int amd_powerplay_reset(void *handle)
786{
787 struct pp_instance *instance = (struct pp_instance *)handle;
788 int ret;
789
790 ret = pp_check(instance);
791 if (ret)
792 return ret;
793
794 ret = pp_hw_fini(instance);
795 if (ret)
796 return ret;
797
798 ret = hwmgr_hw_init(instance);
799 if (ret)
800 return ret;
801
802 return hwmgr_handle_task(instance, AMD_PP_TASK_COMPLETE_INIT, NULL, NULL);
803}
804
Eric Huangf3898ea2015-12-11 16:24:34 -0500805static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
806{
807 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800808 struct pp_instance *pp_handle = (struct pp_instance *)handle;
809 int ret = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500810
Rex Zhu1c863802016-12-28 19:43:23 +0800811 ret = pp_check(pp_handle);
Eric Huangf3898ea2015-12-11 16:24:34 -0500812
Rex Zhuae979882017-09-29 14:36:15 +0800813 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800814 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500815
Rex Zhu1c863802016-12-28 19:43:23 +0800816 hwmgr = pp_handle->hwmgr;
Rex Zhu2a507102017-02-20 17:07:36 +0800817 mutex_lock(&pp_handle->pp_lock);
Eric Huang4dcf9e62016-06-01 17:08:07 -0400818 if (!hwmgr->hardcode_pp_table) {
Edward O'Callaghanefdf7a932016-09-04 12:36:19 +1000819 hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
820 hwmgr->soft_pp_table_size,
821 GFP_KERNEL);
Rex Zhu2a507102017-02-20 17:07:36 +0800822 if (!hwmgr->hardcode_pp_table) {
823 mutex_unlock(&pp_handle->pp_lock);
Eric Huang4dcf9e62016-06-01 17:08:07 -0400824 return -ENOMEM;
Rex Zhu2a507102017-02-20 17:07:36 +0800825 }
Rex Zhu7383bcb2016-03-30 11:35:50 +0800826 }
Eric Huangf3898ea2015-12-11 16:24:34 -0500827
Eric Huang4dcf9e62016-06-01 17:08:07 -0400828 memcpy(hwmgr->hardcode_pp_table, buf, size);
829
830 hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
Rex Zhu2a507102017-02-20 17:07:36 +0800831 mutex_unlock(&pp_handle->pp_lock);
Eric Huang4dcf9e62016-06-01 17:08:07 -0400832
Eric Huangdd4bdf32017-03-01 15:49:31 -0500833 ret = amd_powerplay_reset(handle);
834 if (ret)
835 return ret;
836
837 if (hwmgr->hwmgr_func->avfs_control) {
838 ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
839 if (ret)
840 return ret;
841 }
842
843 return 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500844}
845
846static int pp_dpm_force_clock_level(void *handle,
Eric Huang56327082016-04-12 14:57:23 -0400847 enum pp_clock_type type, uint32_t mask)
Eric Huangf3898ea2015-12-11 16:24:34 -0500848{
849 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800850 struct pp_instance *pp_handle = (struct pp_instance *)handle;
851 int ret = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500852
Rex Zhu1c863802016-12-28 19:43:23 +0800853 ret = pp_check(pp_handle);
Eric Huangf3898ea2015-12-11 16:24:34 -0500854
Rex Zhuae979882017-09-29 14:36:15 +0800855 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800856 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500857
Rex Zhu1c863802016-12-28 19:43:23 +0800858 hwmgr = pp_handle->hwmgr;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800859
860 if (hwmgr->hwmgr_func->force_clock_level == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800861 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800862 return 0;
863 }
Rex Zhu2a507102017-02-20 17:07:36 +0800864 mutex_lock(&pp_handle->pp_lock);
865 hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
866 mutex_unlock(&pp_handle->pp_lock);
867 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500868}
869
870static int pp_dpm_print_clock_levels(void *handle,
871 enum pp_clock_type type, char *buf)
872{
873 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800874 struct pp_instance *pp_handle = (struct pp_instance *)handle;
875 int ret = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500876
Rex Zhu1c863802016-12-28 19:43:23 +0800877 ret = pp_check(pp_handle);
Eric Huangf3898ea2015-12-11 16:24:34 -0500878
Rex Zhuae979882017-09-29 14:36:15 +0800879 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800880 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500881
Rex Zhu1c863802016-12-28 19:43:23 +0800882 hwmgr = pp_handle->hwmgr;
Eric Huangf3898ea2015-12-11 16:24:34 -0500883
Rex Zhu7383bcb2016-03-30 11:35:50 +0800884 if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800885 pr_info("%s was not implemented.\n", __func__);
Rex Zhu7383bcb2016-03-30 11:35:50 +0800886 return 0;
887 }
Rex Zhu2a507102017-02-20 17:07:36 +0800888 mutex_lock(&pp_handle->pp_lock);
889 ret = hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
890 mutex_unlock(&pp_handle->pp_lock);
891 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500892}
893
Eric Huang428bafa2016-05-12 14:51:21 -0400894static int pp_dpm_get_sclk_od(void *handle)
895{
896 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800897 struct pp_instance *pp_handle = (struct pp_instance *)handle;
898 int ret = 0;
Eric Huang428bafa2016-05-12 14:51:21 -0400899
Rex Zhu1c863802016-12-28 19:43:23 +0800900 ret = pp_check(pp_handle);
Eric Huang428bafa2016-05-12 14:51:21 -0400901
Rex Zhuae979882017-09-29 14:36:15 +0800902 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800903 return ret;
Eric Huang428bafa2016-05-12 14:51:21 -0400904
Rex Zhu1c863802016-12-28 19:43:23 +0800905 hwmgr = pp_handle->hwmgr;
Eric Huang428bafa2016-05-12 14:51:21 -0400906
907 if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800908 pr_info("%s was not implemented.\n", __func__);
Eric Huang428bafa2016-05-12 14:51:21 -0400909 return 0;
910 }
Rex Zhu2a507102017-02-20 17:07:36 +0800911 mutex_lock(&pp_handle->pp_lock);
912 ret = hwmgr->hwmgr_func->get_sclk_od(hwmgr);
913 mutex_unlock(&pp_handle->pp_lock);
914 return ret;
Eric Huang428bafa2016-05-12 14:51:21 -0400915}
916
917static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
918{
919 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800920 struct pp_instance *pp_handle = (struct pp_instance *)handle;
921 int ret = 0;
Eric Huang428bafa2016-05-12 14:51:21 -0400922
Rex Zhu1c863802016-12-28 19:43:23 +0800923 ret = pp_check(pp_handle);
Eric Huang428bafa2016-05-12 14:51:21 -0400924
Rex Zhuae979882017-09-29 14:36:15 +0800925 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800926 return ret;
Eric Huang428bafa2016-05-12 14:51:21 -0400927
Rex Zhu1c863802016-12-28 19:43:23 +0800928 hwmgr = pp_handle->hwmgr;
Eric Huang428bafa2016-05-12 14:51:21 -0400929
930 if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800931 pr_info("%s was not implemented.\n", __func__);
Eric Huang428bafa2016-05-12 14:51:21 -0400932 return 0;
933 }
934
Rex Zhu2a507102017-02-20 17:07:36 +0800935 mutex_lock(&pp_handle->pp_lock);
936 ret = hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
Alex Deucherad4febd2017-03-31 10:51:29 -0400937 mutex_unlock(&pp_handle->pp_lock);
Rex Zhu2a507102017-02-20 17:07:36 +0800938 return ret;
Eric Huang428bafa2016-05-12 14:51:21 -0400939}
940
Eric Huangf2bdc052016-05-24 15:11:17 -0400941static int pp_dpm_get_mclk_od(void *handle)
942{
943 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800944 struct pp_instance *pp_handle = (struct pp_instance *)handle;
945 int ret = 0;
Eric Huangf2bdc052016-05-24 15:11:17 -0400946
Rex Zhu1c863802016-12-28 19:43:23 +0800947 ret = pp_check(pp_handle);
Eric Huangf2bdc052016-05-24 15:11:17 -0400948
Rex Zhuae979882017-09-29 14:36:15 +0800949 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800950 return ret;
Eric Huangf2bdc052016-05-24 15:11:17 -0400951
Rex Zhu1c863802016-12-28 19:43:23 +0800952 hwmgr = pp_handle->hwmgr;
Eric Huangf2bdc052016-05-24 15:11:17 -0400953
954 if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800955 pr_info("%s was not implemented.\n", __func__);
Eric Huangf2bdc052016-05-24 15:11:17 -0400956 return 0;
957 }
Rex Zhu2a507102017-02-20 17:07:36 +0800958 mutex_lock(&pp_handle->pp_lock);
959 ret = hwmgr->hwmgr_func->get_mclk_od(hwmgr);
960 mutex_unlock(&pp_handle->pp_lock);
961 return ret;
Eric Huangf2bdc052016-05-24 15:11:17 -0400962}
963
964static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
965{
966 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800967 struct pp_instance *pp_handle = (struct pp_instance *)handle;
968 int ret = 0;
Eric Huangf2bdc052016-05-24 15:11:17 -0400969
Rex Zhu1c863802016-12-28 19:43:23 +0800970 ret = pp_check(pp_handle);
Eric Huangf2bdc052016-05-24 15:11:17 -0400971
Rex Zhuae979882017-09-29 14:36:15 +0800972 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800973 return ret;
Eric Huangf2bdc052016-05-24 15:11:17 -0400974
Rex Zhu1c863802016-12-28 19:43:23 +0800975 hwmgr = pp_handle->hwmgr;
Eric Huangf2bdc052016-05-24 15:11:17 -0400976
977 if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +0800978 pr_info("%s was not implemented.\n", __func__);
Eric Huangf2bdc052016-05-24 15:11:17 -0400979 return 0;
980 }
Rex Zhu2a507102017-02-20 17:07:36 +0800981 mutex_lock(&pp_handle->pp_lock);
982 ret = hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
983 mutex_unlock(&pp_handle->pp_lock);
984 return ret;
Eric Huangf2bdc052016-05-24 15:11:17 -0400985}
986
Tom St Denis9f8df7d2017-02-09 14:29:01 -0500987static int pp_dpm_read_sensor(void *handle, int idx,
988 void *value, int *size)
Tom St Denisa6e36952016-09-15 10:07:34 -0400989{
990 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +0800991 struct pp_instance *pp_handle = (struct pp_instance *)handle;
992 int ret = 0;
Tom St Denisa6e36952016-09-15 10:07:34 -0400993
Rex Zhu1c863802016-12-28 19:43:23 +0800994 ret = pp_check(pp_handle);
Tom St Denisa6e36952016-09-15 10:07:34 -0400995
Rex Zhuae979882017-09-29 14:36:15 +0800996 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +0800997 return ret;
Tom St Denisa6e36952016-09-15 10:07:34 -0400998
Rex Zhu1c863802016-12-28 19:43:23 +0800999 hwmgr = pp_handle->hwmgr;
Tom St Denisa6e36952016-09-15 10:07:34 -04001000
1001 if (hwmgr->hwmgr_func->read_sensor == NULL) {
Huang Rui0fb829d2016-12-26 14:24:05 +08001002 pr_info("%s was not implemented.\n", __func__);
Tom St Denisa6e36952016-09-15 10:07:34 -04001003 return 0;
1004 }
1005
Rex Zhu2a507102017-02-20 17:07:36 +08001006 mutex_lock(&pp_handle->pp_lock);
1007 ret = hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
1008 mutex_unlock(&pp_handle->pp_lock);
1009
1010 return ret;
Tom St Denisa6e36952016-09-15 10:07:34 -04001011}
1012
Alex Deucher597be302016-10-07 13:52:43 -04001013static struct amd_vce_state*
1014pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
1015{
1016 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +08001017 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1018 int ret = 0;
Alex Deucher597be302016-10-07 13:52:43 -04001019
Rex Zhu1c863802016-12-28 19:43:23 +08001020 ret = pp_check(pp_handle);
Alex Deucher597be302016-10-07 13:52:43 -04001021
Rex Zhuae979882017-09-29 14:36:15 +08001022 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +08001023 return NULL;
1024
1025 hwmgr = pp_handle->hwmgr;
1026
1027 if (hwmgr && idx < hwmgr->num_vce_state_tables)
1028 return &hwmgr->vce_states[idx];
Alex Deucher597be302016-10-07 13:52:43 -04001029 return NULL;
1030}
1031
Eric Huang34bb2732016-09-12 16:17:44 -04001032static int pp_dpm_reset_power_profile_state(void *handle,
1033 struct amd_pp_profile *request)
1034{
1035 struct pp_hwmgr *hwmgr;
1036 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1037
1038 if (!request || pp_check(pp_handle))
1039 return -EINVAL;
1040
1041 hwmgr = pp_handle->hwmgr;
1042
1043 if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
1044 pr_info("%s was not implemented.\n", __func__);
1045 return 0;
1046 }
1047
1048 if (request->type == AMD_PP_GFX_PROFILE) {
1049 hwmgr->gfx_power_profile = hwmgr->default_gfx_power_profile;
1050 return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
1051 &hwmgr->gfx_power_profile);
1052 } else if (request->type == AMD_PP_COMPUTE_PROFILE) {
1053 hwmgr->compute_power_profile =
1054 hwmgr->default_compute_power_profile;
1055 return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
1056 &hwmgr->compute_power_profile);
1057 } else
1058 return -EINVAL;
1059}
1060
1061static int pp_dpm_get_power_profile_state(void *handle,
1062 struct amd_pp_profile *query)
1063{
1064 struct pp_hwmgr *hwmgr;
1065 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1066
1067 if (!query || pp_check(pp_handle))
1068 return -EINVAL;
1069
1070 hwmgr = pp_handle->hwmgr;
1071
1072 if (query->type == AMD_PP_GFX_PROFILE)
1073 memcpy(query, &hwmgr->gfx_power_profile,
1074 sizeof(struct amd_pp_profile));
1075 else if (query->type == AMD_PP_COMPUTE_PROFILE)
1076 memcpy(query, &hwmgr->compute_power_profile,
1077 sizeof(struct amd_pp_profile));
1078 else
1079 return -EINVAL;
1080
1081 return 0;
1082}
1083
1084static int pp_dpm_set_power_profile_state(void *handle,
1085 struct amd_pp_profile *request)
1086{
1087 struct pp_hwmgr *hwmgr;
1088 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1089 int ret = -1;
1090
1091 if (!request || pp_check(pp_handle))
1092 return -EINVAL;
1093
1094 hwmgr = pp_handle->hwmgr;
1095
1096 if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
1097 pr_info("%s was not implemented.\n", __func__);
1098 return 0;
1099 }
1100
1101 if (request->min_sclk ||
1102 request->min_mclk ||
1103 request->activity_threshold ||
1104 request->up_hyst ||
1105 request->down_hyst) {
1106 if (request->type == AMD_PP_GFX_PROFILE)
1107 memcpy(&hwmgr->gfx_power_profile, request,
1108 sizeof(struct amd_pp_profile));
1109 else if (request->type == AMD_PP_COMPUTE_PROFILE)
1110 memcpy(&hwmgr->compute_power_profile, request,
1111 sizeof(struct amd_pp_profile));
1112 else
1113 return -EINVAL;
1114
1115 if (request->type == hwmgr->current_power_profile)
1116 ret = hwmgr->hwmgr_func->set_power_profile_state(
1117 hwmgr,
1118 request);
1119 } else {
1120 /* set power profile if it exists */
1121 switch (request->type) {
1122 case AMD_PP_GFX_PROFILE:
1123 ret = hwmgr->hwmgr_func->set_power_profile_state(
1124 hwmgr,
1125 &hwmgr->gfx_power_profile);
1126 break;
1127 case AMD_PP_COMPUTE_PROFILE:
1128 ret = hwmgr->hwmgr_func->set_power_profile_state(
1129 hwmgr,
1130 &hwmgr->compute_power_profile);
1131 break;
1132 default:
1133 return -EINVAL;
1134 }
1135 }
1136
1137 if (!ret)
1138 hwmgr->current_power_profile = request->type;
1139
1140 return 0;
1141}
1142
1143static int pp_dpm_switch_power_profile(void *handle,
1144 enum amd_pp_profile_type type)
1145{
1146 struct pp_hwmgr *hwmgr;
1147 struct amd_pp_profile request = {0};
1148 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1149
1150 if (pp_check(pp_handle))
1151 return -EINVAL;
1152
1153 hwmgr = pp_handle->hwmgr;
1154
1155 if (hwmgr->current_power_profile != type) {
1156 request.type = type;
1157 pp_dpm_set_power_profile_state(handle, &request);
1158 }
1159
1160 return 0;
1161}
1162
Rex Zhu4c7c8bb2017-10-09 12:22:21 +08001163static int pp_dpm_notify_smu_memory_info(void *handle,
1164 uint32_t virtual_addr_low,
1165 uint32_t virtual_addr_hi,
1166 uint32_t mc_addr_low,
1167 uint32_t mc_addr_hi,
1168 uint32_t size)
1169{
1170 struct pp_hwmgr *hwmgr;
1171 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1172 int ret = 0;
1173
1174 ret = pp_check(pp_handle);
1175
1176 if (ret)
1177 return ret;
1178
1179 hwmgr = pp_handle->hwmgr;
1180
1181 if (hwmgr->hwmgr_func->notify_cac_buffer_info == NULL) {
1182 pr_info("%s was not implemented.\n", __func__);
1183 return -EINVAL;
1184 }
1185
1186 mutex_lock(&pp_handle->pp_lock);
1187
1188 ret = hwmgr->hwmgr_func->notify_cac_buffer_info(hwmgr, virtual_addr_low,
1189 virtual_addr_hi, mc_addr_low, mc_addr_hi,
1190 size);
1191
1192 mutex_unlock(&pp_handle->pp_lock);
1193
1194 return ret;
1195}
1196
Rex Zhuf685d712017-10-25 23:55:23 -04001197static int pp_display_configuration_change(void *handle,
David Rokhvarg155f1127c2015-12-14 10:51:39 -05001198 const struct amd_pp_display_configuration *display_config)
Rex Zhu7fb72a12015-11-19 13:35:30 +08001199{
1200 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +08001201 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1202 int ret = 0;
Rex Zhu7fb72a12015-11-19 13:35:30 +08001203
Rex Zhu1c863802016-12-28 19:43:23 +08001204 ret = pp_check(pp_handle);
Rex Zhu7fb72a12015-11-19 13:35:30 +08001205
Rex Zhuae979882017-09-29 14:36:15 +08001206 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +08001207 return ret;
Rex Zhu7fb72a12015-11-19 13:35:30 +08001208
Rex Zhu1c863802016-12-28 19:43:23 +08001209 hwmgr = pp_handle->hwmgr;
Rex Zhu2a507102017-02-20 17:07:36 +08001210 mutex_lock(&pp_handle->pp_lock);
Rex Zhu7fb72a12015-11-19 13:35:30 +08001211 phm_store_dal_configuration_data(hwmgr, display_config);
Rex Zhu2a507102017-02-20 17:07:36 +08001212 mutex_unlock(&pp_handle->pp_lock);
Rex Zhu7fb72a12015-11-19 13:35:30 +08001213 return 0;
1214}
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001215
Rex Zhuf685d712017-10-25 23:55:23 -04001216static int pp_get_display_power_level(void *handle,
Rex Zhu47329132015-12-10 16:49:50 +08001217 struct amd_pp_simple_clock_info *output)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001218{
1219 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +08001220 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1221 int ret = 0;
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001222
Rex Zhu1c863802016-12-28 19:43:23 +08001223 ret = pp_check(pp_handle);
1224
Rex Zhuae979882017-09-29 14:36:15 +08001225 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +08001226 return ret;
1227
1228 hwmgr = pp_handle->hwmgr;
Rex Zhua969e162015-12-29 13:56:03 +08001229
1230 if (output == NULL)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001231 return -EINVAL;
1232
Rex Zhu2a507102017-02-20 17:07:36 +08001233 mutex_lock(&pp_handle->pp_lock);
1234 ret = phm_get_dal_power_level(hwmgr, output);
1235 mutex_unlock(&pp_handle->pp_lock);
1236 return ret;
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001237}
Rex Zhue273b042015-12-07 18:44:23 +08001238
Rex Zhuf685d712017-10-25 23:55:23 -04001239static int pp_get_current_clocks(void *handle,
David Rokhvarg155f1127c2015-12-14 10:51:39 -05001240 struct amd_pp_clock_info *clocks)
Rex Zhue273b042015-12-07 18:44:23 +08001241{
Rex Zhue273b042015-12-07 18:44:23 +08001242 struct amd_pp_simple_clock_info simple_clocks;
1243 struct pp_clock_info hw_clocks;
Rex Zhu1c863802016-12-28 19:43:23 +08001244 struct pp_hwmgr *hwmgr;
1245 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1246 int ret = 0;
Rex Zhue273b042015-12-07 18:44:23 +08001247
Rex Zhu1c863802016-12-28 19:43:23 +08001248 ret = pp_check(pp_handle);
Rex Zhufa9e6992015-12-29 13:56:03 +08001249
Rex Zhuae979882017-09-29 14:36:15 +08001250 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +08001251 return ret;
Rex Zhue273b042015-12-07 18:44:23 +08001252
Rex Zhu1c863802016-12-28 19:43:23 +08001253 hwmgr = pp_handle->hwmgr;
Rex Zhuba5f8842016-10-27 15:29:57 +08001254
Rex Zhu2a507102017-02-20 17:07:36 +08001255 mutex_lock(&pp_handle->pp_lock);
1256
Rex Zhue273b042015-12-07 18:44:23 +08001257 phm_get_dal_power_level(hwmgr, &simple_clocks);
1258
Rex Zhu2a507102017-02-20 17:07:36 +08001259 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1260 PHM_PlatformCaps_PowerContainment))
1261 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1262 &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
1263 else
1264 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1265 &hw_clocks, PHM_PerformanceLevelDesignation_Activity);
1266
Rex Zhuae979882017-09-29 14:36:15 +08001267 if (ret) {
Rex Zhu2a507102017-02-20 17:07:36 +08001268 pr_info("Error in phm_get_clock_info \n");
1269 mutex_unlock(&pp_handle->pp_lock);
1270 return -EINVAL;
Rex Zhue273b042015-12-07 18:44:23 +08001271 }
1272
1273 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1274 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1275 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1276 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1277 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1278 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1279
1280 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1281 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1282
1283 clocks->max_clocks_state = simple_clocks.level;
1284
1285 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
1286 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1287 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1288 }
Rex Zhu2a507102017-02-20 17:07:36 +08001289 mutex_unlock(&pp_handle->pp_lock);
Rex Zhue273b042015-12-07 18:44:23 +08001290 return 0;
Rex Zhue273b042015-12-07 18:44:23 +08001291}
1292
Rex Zhuf685d712017-10-25 23:55:23 -04001293static int pp_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
Rex Zhue273b042015-12-07 18:44:23 +08001294{
Rex Zhu1c863802016-12-28 19:43:23 +08001295 struct pp_hwmgr *hwmgr;
1296 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1297 int ret = 0;
Rex Zhue273b042015-12-07 18:44:23 +08001298
Rex Zhu1c863802016-12-28 19:43:23 +08001299 ret = pp_check(pp_handle);
Rex Zhue273b042015-12-07 18:44:23 +08001300
Rex Zhuae979882017-09-29 14:36:15 +08001301 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +08001302 return ret;
1303
1304 hwmgr = pp_handle->hwmgr;
Rex Zhufa9e6992015-12-29 13:56:03 +08001305
1306 if (clocks == NULL)
Rex Zhue273b042015-12-07 18:44:23 +08001307 return -EINVAL;
1308
Rex Zhu2a507102017-02-20 17:07:36 +08001309 mutex_lock(&pp_handle->pp_lock);
1310 ret = phm_get_clock_by_type(hwmgr, type, clocks);
1311 mutex_unlock(&pp_handle->pp_lock);
1312 return ret;
Rex Zhue273b042015-12-07 18:44:23 +08001313}
1314
Rex Zhuf685d712017-10-25 23:55:23 -04001315static int pp_get_clock_by_type_with_latency(void *handle,
Eric Huangd0187722017-03-06 13:13:48 -05001316 enum amd_pp_clock_type type,
1317 struct pp_clock_levels_with_latency *clocks)
1318{
1319 struct pp_hwmgr *hwmgr;
1320 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1321 int ret = 0;
1322
1323 ret = pp_check(pp_handle);
Rex Zhuae979882017-09-29 14:36:15 +08001324 if (ret)
Eric Huangd0187722017-03-06 13:13:48 -05001325 return ret;
1326
1327 if (!clocks)
1328 return -EINVAL;
1329
1330 mutex_lock(&pp_handle->pp_lock);
1331 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1332 ret = phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
1333 mutex_unlock(&pp_handle->pp_lock);
1334 return ret;
1335}
1336
Rex Zhuf685d712017-10-25 23:55:23 -04001337static int pp_get_clock_by_type_with_voltage(void *handle,
Eric Huangd0187722017-03-06 13:13:48 -05001338 enum amd_pp_clock_type type,
1339 struct pp_clock_levels_with_voltage *clocks)
1340{
1341 struct pp_hwmgr *hwmgr;
1342 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1343 int ret = 0;
1344
1345 ret = pp_check(pp_handle);
Rex Zhuae979882017-09-29 14:36:15 +08001346 if (ret)
Eric Huangd0187722017-03-06 13:13:48 -05001347 return ret;
1348
1349 if (!clocks)
1350 return -EINVAL;
1351
1352 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1353
1354 mutex_lock(&pp_handle->pp_lock);
1355
1356 ret = phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);
1357
1358 mutex_unlock(&pp_handle->pp_lock);
1359 return ret;
1360}
1361
Rex Zhuf685d712017-10-25 23:55:23 -04001362static int pp_set_watermarks_for_clocks_ranges(void *handle,
Eric Huangd0187722017-03-06 13:13:48 -05001363 struct pp_wm_sets_with_clock_ranges_soc15 *wm_with_clock_ranges)
1364{
1365 struct pp_hwmgr *hwmgr;
1366 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1367 int ret = 0;
1368
1369 ret = pp_check(pp_handle);
Rex Zhuae979882017-09-29 14:36:15 +08001370 if (ret)
Eric Huangd0187722017-03-06 13:13:48 -05001371 return ret;
1372
1373 if (!wm_with_clock_ranges)
1374 return -EINVAL;
1375
1376 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1377
1378 mutex_lock(&pp_handle->pp_lock);
1379 ret = phm_set_watermarks_for_clocks_ranges(hwmgr,
1380 wm_with_clock_ranges);
1381 mutex_unlock(&pp_handle->pp_lock);
1382
1383 return ret;
1384}
1385
Rex Zhuf685d712017-10-25 23:55:23 -04001386static int pp_display_clock_voltage_request(void *handle,
Eric Huangd0187722017-03-06 13:13:48 -05001387 struct pp_display_clock_request *clock)
1388{
1389 struct pp_hwmgr *hwmgr;
1390 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1391 int ret = 0;
1392
1393 ret = pp_check(pp_handle);
Rex Zhuae979882017-09-29 14:36:15 +08001394 if (ret)
Eric Huangd0187722017-03-06 13:13:48 -05001395 return ret;
1396
1397 if (!clock)
1398 return -EINVAL;
1399
1400 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1401
1402 mutex_lock(&pp_handle->pp_lock);
1403 ret = phm_display_clock_voltage_request(hwmgr, clock);
1404 mutex_unlock(&pp_handle->pp_lock);
1405
1406 return ret;
1407}
1408
Rex Zhuf685d712017-10-25 23:55:23 -04001409static int pp_get_display_mode_validation_clocks(void *handle,
David Rokhvarg155f1127c2015-12-14 10:51:39 -05001410 struct amd_pp_simple_clock_info *clocks)
Rex Zhue273b042015-12-07 18:44:23 +08001411{
Rex Zhue273b042015-12-07 18:44:23 +08001412 struct pp_hwmgr *hwmgr;
Rex Zhu1c863802016-12-28 19:43:23 +08001413 struct pp_instance *pp_handle = (struct pp_instance *)handle;
1414 int ret = 0;
Rex Zhue273b042015-12-07 18:44:23 +08001415
Rex Zhu1c863802016-12-28 19:43:23 +08001416 ret = pp_check(pp_handle);
1417
Rex Zhuae979882017-09-29 14:36:15 +08001418 if (ret)
Rex Zhu1c863802016-12-28 19:43:23 +08001419 return ret;
1420
1421 hwmgr = pp_handle->hwmgr;
1422
Rex Zhufa9e6992015-12-29 13:56:03 +08001423 if (clocks == NULL)
Rex Zhue273b042015-12-07 18:44:23 +08001424 return -EINVAL;
1425
Rex Zhu2a507102017-02-20 17:07:36 +08001426 mutex_lock(&pp_handle->pp_lock);
1427
Rex Zhue273b042015-12-07 18:44:23 +08001428 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
Rex Zhu1c863802016-12-28 19:43:23 +08001429 ret = phm_get_max_high_clocks(hwmgr, clocks);
Rex Zhue273b042015-12-07 18:44:23 +08001430
Rex Zhu2a507102017-02-20 17:07:36 +08001431 mutex_unlock(&pp_handle->pp_lock);
Rex Zhu1c863802016-12-28 19:43:23 +08001432 return ret;
Rex Zhue273b042015-12-07 18:44:23 +08001433}
1434
Rex Zhuf685d712017-10-25 23:55:23 -04001435const struct amd_pm_funcs pp_dpm_funcs = {
1436 .get_temperature = pp_dpm_get_temperature,
1437 .load_firmware = pp_dpm_load_fw,
1438 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
1439 .force_performance_level = pp_dpm_force_performance_level,
1440 .get_performance_level = pp_dpm_get_performance_level,
1441 .get_current_power_state = pp_dpm_get_current_power_state,
1442 .powergate_vce = pp_dpm_powergate_vce,
1443 .powergate_uvd = pp_dpm_powergate_uvd,
1444 .dispatch_tasks = pp_dpm_dispatch_tasks,
1445 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
1446 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
1447 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
1448 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
1449 .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
1450 .get_pp_num_states = pp_dpm_get_pp_num_states,
1451 .get_pp_table = pp_dpm_get_pp_table,
1452 .set_pp_table = pp_dpm_set_pp_table,
1453 .force_clock_level = pp_dpm_force_clock_level,
1454 .print_clock_levels = pp_dpm_print_clock_levels,
1455 .get_sclk_od = pp_dpm_get_sclk_od,
1456 .set_sclk_od = pp_dpm_set_sclk_od,
1457 .get_mclk_od = pp_dpm_get_mclk_od,
1458 .set_mclk_od = pp_dpm_set_mclk_od,
1459 .read_sensor = pp_dpm_read_sensor,
1460 .get_vce_clock_state = pp_dpm_get_vce_clock_state,
1461 .reset_power_profile_state = pp_dpm_reset_power_profile_state,
1462 .get_power_profile_state = pp_dpm_get_power_profile_state,
1463 .set_power_profile_state = pp_dpm_set_power_profile_state,
1464 .switch_power_profile = pp_dpm_switch_power_profile,
1465 .set_clockgating_by_smu = pp_set_clockgating_by_smu,
Rex Zhu4c7c8bb2017-10-09 12:22:21 +08001466 .notify_smu_memory_info = pp_dpm_notify_smu_memory_info,
Rex Zhuf685d712017-10-25 23:55:23 -04001467/* export to DC */
1468 .get_sclk = pp_dpm_get_sclk,
1469 .get_mclk = pp_dpm_get_mclk,
1470 .display_configuration_change = pp_display_configuration_change,
1471 .get_display_power_level = pp_get_display_power_level,
1472 .get_current_clocks = pp_get_current_clocks,
1473 .get_clock_by_type = pp_get_clock_by_type,
1474 .get_clock_by_type_with_latency = pp_get_clock_by_type_with_latency,
1475 .get_clock_by_type_with_voltage = pp_get_clock_by_type_with_voltage,
1476 .set_watermarks_for_clocks_ranges = pp_set_watermarks_for_clocks_ranges,
1477 .display_clock_voltage_request = pp_display_clock_voltage_request,
1478 .get_display_mode_validation_clocks = pp_get_display_mode_validation_clocks,
1479};