blob: cc7219084fadc20ffa8366eba10d465226326f3b [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 */
23#include <linux/types.h>
24#include <linux/kernel.h>
25#include <linux/gfp.h>
Jammy Zhouac885b32015-07-21 17:43:02 +080026#include <linux/slab.h>
Alex Deucher1f7371b2015-12-02 17:46:21 -050027#include "amd_shared.h"
28#include "amd_powerplay.h"
Jammy Zhouac885b32015-07-21 17:43:02 +080029#include "pp_instance.h"
Rex Zhu577bbe02015-08-28 12:56:43 +080030#include "power_state.h"
31#include "eventmanager.h"
Rex Zhue273b042015-12-07 18:44:23 +080032#include "pp_debug.h"
Alex Deucher1f7371b2015-12-02 17:46:21 -050033
Rex Zhuaf223df2016-07-28 16:51:47 +080034
Rex Zhua969e162015-12-29 13:56:03 +080035#define PP_CHECK(handle) \
36 do { \
37 if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
38 return -EINVAL; \
39 } while (0)
40
Rex Zhu7383bcb2016-03-30 11:35:50 +080041#define PP_CHECK_HW(hwmgr) \
42 do { \
43 if ((hwmgr) == NULL || (hwmgr)->hwmgr_func == NULL) \
Rex Zhuba5f8842016-10-27 15:29:57 +080044 return 0; \
Rex Zhu7383bcb2016-03-30 11:35:50 +080045 } while (0)
46
Alex Deucher1f7371b2015-12-02 17:46:21 -050047static int pp_early_init(void *handle)
48{
49 return 0;
50}
51
52static int pp_sw_init(void *handle)
53{
Jammy Zhou3bace352015-07-21 21:18:15 +080054 struct pp_instance *pp_handle;
55 struct pp_hwmgr *hwmgr;
56 int ret = 0;
57
58 if (handle == NULL)
59 return -EINVAL;
60
61 pp_handle = (struct pp_instance *)handle;
62 hwmgr = pp_handle->hwmgr;
63
Rex Zhu7383bcb2016-03-30 11:35:50 +080064 PP_CHECK_HW(hwmgr);
65
66 if (hwmgr->pptable_func == NULL ||
Jammy Zhou3bace352015-07-21 21:18:15 +080067 hwmgr->pptable_func->pptable_init == NULL ||
68 hwmgr->hwmgr_func->backend_init == NULL)
69 return -EINVAL;
70
71 ret = hwmgr->pptable_func->pptable_init(hwmgr);
Alex Deucher9441f962016-01-20 12:15:09 -050072 if (ret)
Huang Ruib4eeed52016-05-09 17:29:41 +080073 goto err;
Alex Deucher9441f962016-01-20 12:15:09 -050074
Huang Ruib4eeed52016-05-09 17:29:41 +080075 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
76 if (ret)
Monk Liu9d8f0862016-05-30 13:43:45 +080077 goto err1;
Huang Ruib4eeed52016-05-09 17:29:41 +080078
Huang Rui167112b2016-12-14 16:26:54 +080079 if (hwmgr->hwmgr_func->request_firmware) {
80 ret = hwmgr->hwmgr_func->request_firmware(hwmgr);
81 if (ret)
82 goto err2;
83 }
84
Huang Ruib4eeed52016-05-09 17:29:41 +080085 pr_info("amdgpu: powerplay initialized\n");
86
87 return 0;
Huang Rui167112b2016-12-14 16:26:54 +080088err2:
89 if (hwmgr->hwmgr_func->backend_fini)
90 hwmgr->hwmgr_func->backend_fini(hwmgr);
Monk Liu9d8f0862016-05-30 13:43:45 +080091err1:
92 if (hwmgr->pptable_func->pptable_fini)
93 hwmgr->pptable_func->pptable_fini(hwmgr);
Huang Ruib4eeed52016-05-09 17:29:41 +080094err:
95 pr_err("amdgpu: powerplay initialization failed\n");
Jammy Zhou3bace352015-07-21 21:18:15 +080096 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050097}
98
99static int pp_sw_fini(void *handle)
100{
Jammy Zhou3bace352015-07-21 21:18:15 +0800101 struct pp_instance *pp_handle;
102 struct pp_hwmgr *hwmgr;
103 int ret = 0;
104
105 if (handle == NULL)
106 return -EINVAL;
107
108 pp_handle = (struct pp_instance *)handle;
109 hwmgr = pp_handle->hwmgr;
110
Rex Zhu7383bcb2016-03-30 11:35:50 +0800111 PP_CHECK_HW(hwmgr);
112
Huang Rui167112b2016-12-14 16:26:54 +0800113 if (hwmgr->hwmgr_func->release_firmware)
114 ret = hwmgr->hwmgr_func->release_firmware(hwmgr);
115
Rex Zhu7383bcb2016-03-30 11:35:50 +0800116 if (hwmgr->hwmgr_func->backend_fini != NULL)
Jammy Zhou3bace352015-07-21 21:18:15 +0800117 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
118
Monk Liu9d8f0862016-05-30 13:43:45 +0800119 if (hwmgr->pptable_func->pptable_fini)
120 hwmgr->pptable_func->pptable_fini(hwmgr);
121
Jammy Zhou3bace352015-07-21 21:18:15 +0800122 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500123}
124
125static int pp_hw_init(void *handle)
126{
Jammy Zhouac885b32015-07-21 17:43:02 +0800127 struct pp_instance *pp_handle;
128 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800129 struct pp_eventmgr *eventmgr;
Rex Zhuba5f8842016-10-27 15:29:57 +0800130 struct pp_hwmgr *hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800131 int ret = 0;
132
133 if (handle == NULL)
134 return -EINVAL;
135
136 pp_handle = (struct pp_instance *)handle;
137 smumgr = pp_handle->smu_mgr;
Rex Zhuba5f8842016-10-27 15:29:57 +0800138 hwmgr = pp_handle->hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800139
140 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
141 smumgr->smumgr_funcs->smu_init == NULL ||
142 smumgr->smumgr_funcs->start_smu == NULL)
143 return -EINVAL;
144
145 ret = smumgr->smumgr_funcs->smu_init(smumgr);
146 if (ret) {
147 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
148 return ret;
149 }
150
151 ret = smumgr->smumgr_funcs->start_smu(smumgr);
152 if (ret) {
153 printk(KERN_ERR "[ powerplay ] smc start failed\n");
154 smumgr->smumgr_funcs->smu_fini(smumgr);
155 return ret;
156 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800157
Rex Zhuba5f8842016-10-27 15:29:57 +0800158 PP_CHECK_HW(hwmgr);
Rex Zhue92a0372015-09-23 15:14:54 +0800159
Rex Zhuba5f8842016-10-27 15:29:57 +0800160 hw_init_power_state_table(hwmgr);
161
162 eventmgr = pp_handle->eventmgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800163 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
164 return -EINVAL;
165
166 ret = eventmgr->pp_eventmgr_init(eventmgr);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500167 return 0;
168}
169
170static int pp_hw_fini(void *handle)
171{
Jammy Zhouac885b32015-07-21 17:43:02 +0800172 struct pp_instance *pp_handle;
173 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800174 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800175
176 if (handle == NULL)
177 return -EINVAL;
178
179 pp_handle = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800180 eventmgr = pp_handle->eventmgr;
181
Heinrich Schuchardtd36f3e02016-08-21 20:21:27 +0200182 if (eventmgr != NULL && eventmgr->pp_eventmgr_fini != NULL)
Rex Zhue92a0372015-09-23 15:14:54 +0800183 eventmgr->pp_eventmgr_fini(eventmgr);
184
Jammy Zhouac885b32015-07-21 17:43:02 +0800185 smumgr = pp_handle->smu_mgr;
186
Heinrich Schuchardtd36f3e02016-08-21 20:21:27 +0200187 if (smumgr != NULL && smumgr->smumgr_funcs != NULL &&
Jammy Zhouac885b32015-07-21 17:43:02 +0800188 smumgr->smumgr_funcs->smu_fini != NULL)
189 smumgr->smumgr_funcs->smu_fini(smumgr);
190
Alex Deucher1f7371b2015-12-02 17:46:21 -0500191 return 0;
192}
193
194static bool pp_is_idle(void *handle)
195{
Edward O'Callaghaned5121a2016-07-12 10:17:52 +1000196 return false;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500197}
198
199static int pp_wait_for_idle(void *handle)
200{
201 return 0;
202}
203
204static int pp_sw_reset(void *handle)
205{
206 return 0;
207}
208
Alex Deucher1f7371b2015-12-02 17:46:21 -0500209
Rex Zhu465f96e2016-09-18 16:52:03 +0800210int amd_set_clockgating_by_smu(void *handle, uint32_t msg_id)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500211{
Eric Huang03e39052016-02-09 16:26:00 -0500212 struct pp_hwmgr *hwmgr;
Eric Huang03e39052016-02-09 16:26:00 -0500213
214 if (handle == NULL)
215 return -EINVAL;
216
217 hwmgr = ((struct pp_instance *)handle)->hwmgr;
218
Rex Zhu7383bcb2016-03-30 11:35:50 +0800219 PP_CHECK_HW(hwmgr);
Eric Huang03e39052016-02-09 16:26:00 -0500220
Rex Zhu7383bcb2016-03-30 11:35:50 +0800221 if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
222 printk(KERN_INFO "%s was not implemented.\n", __func__);
Flora Cui538333f2016-02-15 15:45:59 +0800223 return 0;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800224 }
Flora Cui538333f2016-02-15 15:45:59 +0800225
Rex Zhu465f96e2016-09-18 16:52:03 +0800226 return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500227}
228
229static int pp_set_powergating_state(void *handle,
230 enum amd_powergating_state state)
231{
Eric Huang65f85e72016-02-11 15:54:45 -0500232 struct pp_hwmgr *hwmgr;
233
234 if (handle == NULL)
235 return -EINVAL;
236
237 hwmgr = ((struct pp_instance *)handle)->hwmgr;
238
Rex Zhu7383bcb2016-03-30 11:35:50 +0800239 PP_CHECK_HW(hwmgr);
240
241 if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
242 printk(KERN_INFO "%s was not implemented.\n", __func__);
243 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,
248 state == AMD_PG_STATE_GATE ? true : false);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500249}
250
251static int pp_suspend(void *handle)
252{
Rex Zhu577bbe02015-08-28 12:56:43 +0800253 struct pp_instance *pp_handle;
254 struct pp_eventmgr *eventmgr;
255 struct pem_event_data event_data = { {0} };
256
257 if (handle == NULL)
258 return -EINVAL;
259
260 pp_handle = (struct pp_instance *)handle;
261 eventmgr = pp_handle->eventmgr;
Rex Zhuba5f8842016-10-27 15:29:57 +0800262
263 if (eventmgr != NULL)
264 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500265 return 0;
266}
267
268static int pp_resume(void *handle)
269{
Rex Zhu577bbe02015-08-28 12:56:43 +0800270 struct pp_instance *pp_handle;
271 struct pp_eventmgr *eventmgr;
272 struct pem_event_data event_data = { {0} };
Rex Zhue0b71a72015-12-29 10:25:19 +0800273 struct pp_smumgr *smumgr;
274 int ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800275
276 if (handle == NULL)
277 return -EINVAL;
278
279 pp_handle = (struct pp_instance *)handle;
Rex Zhue0b71a72015-12-29 10:25:19 +0800280 smumgr = pp_handle->smu_mgr;
281
282 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
283 smumgr->smumgr_funcs->start_smu == NULL)
284 return -EINVAL;
285
286 ret = smumgr->smumgr_funcs->start_smu(smumgr);
287 if (ret) {
288 printk(KERN_ERR "[ powerplay ] smc start failed\n");
289 smumgr->smumgr_funcs->smu_fini(smumgr);
290 return ret;
291 }
292
Rex Zhu577bbe02015-08-28 12:56:43 +0800293 eventmgr = pp_handle->eventmgr;
Rex Zhuba5f8842016-10-27 15:29:57 +0800294 if (eventmgr != NULL)
295 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
Rex Zhue0b71a72015-12-29 10:25:19 +0800296
Alex Deucher1f7371b2015-12-02 17:46:21 -0500297 return 0;
298}
299
300const struct amd_ip_funcs pp_ip_funcs = {
Tom St Denis88a907d2016-05-04 14:28:35 -0400301 .name = "powerplay",
Alex Deucher1f7371b2015-12-02 17:46:21 -0500302 .early_init = pp_early_init,
303 .late_init = NULL,
304 .sw_init = pp_sw_init,
305 .sw_fini = pp_sw_fini,
306 .hw_init = pp_hw_init,
307 .hw_fini = pp_hw_fini,
308 .suspend = pp_suspend,
309 .resume = pp_resume,
310 .is_idle = pp_is_idle,
311 .wait_for_idle = pp_wait_for_idle,
312 .soft_reset = pp_sw_reset,
Rex Zhu465f96e2016-09-18 16:52:03 +0800313 .set_clockgating_state = NULL,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500314 .set_powergating_state = pp_set_powergating_state,
315};
316
317static int pp_dpm_load_fw(void *handle)
318{
319 return 0;
320}
321
322static int pp_dpm_fw_loading_complete(void *handle)
323{
324 return 0;
325}
326
327static int pp_dpm_force_performance_level(void *handle,
328 enum amd_dpm_forced_level level)
329{
Rex Zhu577bbe02015-08-28 12:56:43 +0800330 struct pp_instance *pp_handle;
331 struct pp_hwmgr *hwmgr;
332
333 if (handle == NULL)
334 return -EINVAL;
335
336 pp_handle = (struct pp_instance *)handle;
337
338 hwmgr = pp_handle->hwmgr;
339
Rex Zhu7383bcb2016-03-30 11:35:50 +0800340 PP_CHECK_HW(hwmgr);
341
342 if (hwmgr->hwmgr_func->force_dpm_level == NULL) {
343 printk(KERN_INFO "%s was not implemented.\n", __func__);
344 return 0;
345 }
Rex Zhu577bbe02015-08-28 12:56:43 +0800346
347 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
348
Alex Deucher1f7371b2015-12-02 17:46:21 -0500349 return 0;
350}
Rex Zhu577bbe02015-08-28 12:56:43 +0800351
Alex Deucher1f7371b2015-12-02 17:46:21 -0500352static enum amd_dpm_forced_level pp_dpm_get_performance_level(
353 void *handle)
354{
Rex Zhu577bbe02015-08-28 12:56:43 +0800355 struct pp_hwmgr *hwmgr;
356
357 if (handle == NULL)
358 return -EINVAL;
359
360 hwmgr = ((struct pp_instance *)handle)->hwmgr;
361
Rex Zhuba5f8842016-10-27 15:29:57 +0800362 PP_CHECK_HW(hwmgr);
Rex Zhu577bbe02015-08-28 12:56:43 +0800363
364 return (((struct pp_instance *)handle)->hwmgr->dpm_level);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500365}
Rex Zhu577bbe02015-08-28 12:56:43 +0800366
Alex Deucher1f7371b2015-12-02 17:46:21 -0500367static int pp_dpm_get_sclk(void *handle, bool low)
368{
Rex Zhu577bbe02015-08-28 12:56:43 +0800369 struct pp_hwmgr *hwmgr;
370
371 if (handle == NULL)
372 return -EINVAL;
373
374 hwmgr = ((struct pp_instance *)handle)->hwmgr;
375
Rex Zhu7383bcb2016-03-30 11:35:50 +0800376 PP_CHECK_HW(hwmgr);
377
378 if (hwmgr->hwmgr_func->get_sclk == NULL) {
379 printk(KERN_INFO "%s was not implemented.\n", __func__);
380 return 0;
381 }
Rex Zhu577bbe02015-08-28 12:56:43 +0800382
383 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500384}
Rex Zhu577bbe02015-08-28 12:56:43 +0800385
Alex Deucher1f7371b2015-12-02 17:46:21 -0500386static int pp_dpm_get_mclk(void *handle, bool low)
387{
Rex Zhu577bbe02015-08-28 12:56:43 +0800388 struct pp_hwmgr *hwmgr;
389
390 if (handle == NULL)
391 return -EINVAL;
392
393 hwmgr = ((struct pp_instance *)handle)->hwmgr;
394
Rex Zhu7383bcb2016-03-30 11:35:50 +0800395 PP_CHECK_HW(hwmgr);
396
397 if (hwmgr->hwmgr_func->get_mclk == NULL) {
398 printk(KERN_INFO "%s was not implemented.\n", __func__);
399 return 0;
400 }
Rex Zhu577bbe02015-08-28 12:56:43 +0800401
402 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500403}
Rex Zhu577bbe02015-08-28 12:56:43 +0800404
Alex Deucher1f7371b2015-12-02 17:46:21 -0500405static int pp_dpm_powergate_vce(void *handle, bool gate)
406{
Rex Zhu577bbe02015-08-28 12:56:43 +0800407 struct pp_hwmgr *hwmgr;
408
409 if (handle == NULL)
410 return -EINVAL;
411
412 hwmgr = ((struct pp_instance *)handle)->hwmgr;
413
Rex Zhu7383bcb2016-03-30 11:35:50 +0800414 PP_CHECK_HW(hwmgr);
415
416 if (hwmgr->hwmgr_func->powergate_vce == NULL) {
417 printk(KERN_INFO "%s was not implemented.\n", __func__);
418 return 0;
419 }
Rex Zhu577bbe02015-08-28 12:56:43 +0800420
421 return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500422}
Rex Zhu577bbe02015-08-28 12:56:43 +0800423
Alex Deucher1f7371b2015-12-02 17:46:21 -0500424static int pp_dpm_powergate_uvd(void *handle, bool gate)
425{
Rex Zhu577bbe02015-08-28 12:56:43 +0800426 struct pp_hwmgr *hwmgr;
427
428 if (handle == NULL)
429 return -EINVAL;
430
431 hwmgr = ((struct pp_instance *)handle)->hwmgr;
432
Rex Zhu7383bcb2016-03-30 11:35:50 +0800433 PP_CHECK_HW(hwmgr);
434
435 if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
436 printk(KERN_INFO "%s was not implemented.\n", __func__);
437 return 0;
438 }
Rex Zhu577bbe02015-08-28 12:56:43 +0800439
440 return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
441}
442
443static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
444{
445 switch (state) {
446 case POWER_STATE_TYPE_BATTERY:
447 return PP_StateUILabel_Battery;
448 case POWER_STATE_TYPE_BALANCED:
449 return PP_StateUILabel_Balanced;
450 case POWER_STATE_TYPE_PERFORMANCE:
451 return PP_StateUILabel_Performance;
452 default:
453 return PP_StateUILabel_None;
454 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500455}
456
Baoyou Xief8a4c112016-09-30 17:58:42 +0800457static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id,
458 void *input, void *output)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500459{
Rex Zhu577bbe02015-08-28 12:56:43 +0800460 int ret = 0;
461 struct pp_instance *pp_handle;
462 struct pem_event_data data = { {0} };
463
464 pp_handle = (struct pp_instance *)handle;
465
466 if (pp_handle == NULL)
467 return -EINVAL;
468
Rex Zhuba5f8842016-10-27 15:29:57 +0800469 if (pp_handle->eventmgr == NULL)
470 return 0;
471
Rex Zhu577bbe02015-08-28 12:56:43 +0800472 switch (event_id) {
473 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
474 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
475 break;
476 case AMD_PP_EVENT_ENABLE_USER_STATE:
477 {
478 enum amd_pm_state_type ps;
479
480 if (input == NULL)
481 return -EINVAL;
482 ps = *(unsigned long *)input;
483
484 data.requested_ui_label = power_state_convert(ps);
485 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
Rex Zhudc26a2a2016-02-25 17:16:52 +0800486 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800487 }
Rex Zhudc26a2a2016-02-25 17:16:52 +0800488 case AMD_PP_EVENT_COMPLETE_INIT:
489 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
490 break;
Eric Huang428bafa2016-05-12 14:51:21 -0400491 case AMD_PP_EVENT_READJUST_POWER_STATE:
Eric Huang428bafa2016-05-12 14:51:21 -0400492 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
493 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800494 default:
495 break;
496 }
497 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500498}
Rex Zhu577bbe02015-08-28 12:56:43 +0800499
Baoyou Xief8a4c112016-09-30 17:58:42 +0800500static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500501{
Rex Zhu577bbe02015-08-28 12:56:43 +0800502 struct pp_hwmgr *hwmgr;
503 struct pp_power_state *state;
504
505 if (handle == NULL)
506 return -EINVAL;
507
508 hwmgr = ((struct pp_instance *)handle)->hwmgr;
509
510 if (hwmgr == NULL || hwmgr->current_ps == NULL)
511 return -EINVAL;
512
513 state = hwmgr->current_ps;
514
515 switch (state->classification.ui_label) {
516 case PP_StateUILabel_Battery:
517 return POWER_STATE_TYPE_BATTERY;
518 case PP_StateUILabel_Balanced:
519 return POWER_STATE_TYPE_BALANCED;
520 case PP_StateUILabel_Performance:
521 return POWER_STATE_TYPE_PERFORMANCE;
522 default:
Eric Huangf3898ea2015-12-11 16:24:34 -0500523 if (state->classification.flags & PP_StateClassificationFlag_Boot)
524 return POWER_STATE_TYPE_INTERNAL_BOOT;
525 else
526 return POWER_STATE_TYPE_DEFAULT;
Rex Zhu577bbe02015-08-28 12:56:43 +0800527 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500528}
Rex Zhu577bbe02015-08-28 12:56:43 +0800529
Rex Zhucac9a192015-10-16 11:48:21 +0800530static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
531{
532 struct pp_hwmgr *hwmgr;
533
534 if (handle == NULL)
535 return -EINVAL;
536
537 hwmgr = ((struct pp_instance *)handle)->hwmgr;
538
Rex Zhu7383bcb2016-03-30 11:35:50 +0800539 PP_CHECK_HW(hwmgr);
540
541 if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
542 printk(KERN_INFO "%s was not implemented.\n", __func__);
543 return 0;
544 }
Rex Zhucac9a192015-10-16 11:48:21 +0800545
546 return hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
547}
548
549static int pp_dpm_get_fan_control_mode(void *handle)
550{
551 struct pp_hwmgr *hwmgr;
552
553 if (handle == NULL)
554 return -EINVAL;
555
556 hwmgr = ((struct pp_instance *)handle)->hwmgr;
557
Rex Zhu7383bcb2016-03-30 11:35:50 +0800558 PP_CHECK_HW(hwmgr);
559
560 if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
561 printk(KERN_INFO "%s was not implemented.\n", __func__);
562 return 0;
563 }
Rex Zhucac9a192015-10-16 11:48:21 +0800564
565 return hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
566}
567
568static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
569{
570 struct pp_hwmgr *hwmgr;
571
572 if (handle == NULL)
573 return -EINVAL;
574
575 hwmgr = ((struct pp_instance *)handle)->hwmgr;
576
Rex Zhu7383bcb2016-03-30 11:35:50 +0800577 PP_CHECK_HW(hwmgr);
578
579 if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
580 printk(KERN_INFO "%s was not implemented.\n", __func__);
581 return 0;
582 }
Rex Zhucac9a192015-10-16 11:48:21 +0800583
584 return hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
585}
586
587static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
588{
589 struct pp_hwmgr *hwmgr;
590
591 if (handle == NULL)
592 return -EINVAL;
593
594 hwmgr = ((struct pp_instance *)handle)->hwmgr;
595
Rex Zhu7383bcb2016-03-30 11:35:50 +0800596 PP_CHECK_HW(hwmgr);
597
598 if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
599 printk(KERN_INFO "%s was not implemented.\n", __func__);
600 return 0;
601 }
Rex Zhucac9a192015-10-16 11:48:21 +0800602
603 return hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
604}
605
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300606static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
607{
608 struct pp_hwmgr *hwmgr;
609
610 if (handle == NULL)
611 return -EINVAL;
612
613 hwmgr = ((struct pp_instance *)handle)->hwmgr;
614
615 PP_CHECK_HW(hwmgr);
616
617 if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
618 return -EINVAL;
619
620 return hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
621}
622
Rex Zhucac9a192015-10-16 11:48:21 +0800623static int pp_dpm_get_temperature(void *handle)
624{
625 struct pp_hwmgr *hwmgr;
626
627 if (handle == NULL)
628 return -EINVAL;
629
630 hwmgr = ((struct pp_instance *)handle)->hwmgr;
631
Rex Zhu7383bcb2016-03-30 11:35:50 +0800632 PP_CHECK_HW(hwmgr);
633
634 if (hwmgr->hwmgr_func->get_temperature == NULL) {
635 printk(KERN_INFO "%s was not implemented.\n", __func__);
636 return 0;
637 }
Rex Zhucac9a192015-10-16 11:48:21 +0800638
639 return hwmgr->hwmgr_func->get_temperature(hwmgr);
640}
Rex Zhu577bbe02015-08-28 12:56:43 +0800641
Eric Huangf3898ea2015-12-11 16:24:34 -0500642static int pp_dpm_get_pp_num_states(void *handle,
643 struct pp_states_info *data)
644{
645 struct pp_hwmgr *hwmgr;
646 int i;
647
648 if (!handle)
649 return -EINVAL;
650
651 hwmgr = ((struct pp_instance *)handle)->hwmgr;
652
653 if (hwmgr == NULL || hwmgr->ps == NULL)
654 return -EINVAL;
655
656 data->nums = hwmgr->num_ps;
657
658 for (i = 0; i < hwmgr->num_ps; i++) {
659 struct pp_power_state *state = (struct pp_power_state *)
660 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
661 switch (state->classification.ui_label) {
662 case PP_StateUILabel_Battery:
663 data->states[i] = POWER_STATE_TYPE_BATTERY;
664 break;
665 case PP_StateUILabel_Balanced:
666 data->states[i] = POWER_STATE_TYPE_BALANCED;
667 break;
668 case PP_StateUILabel_Performance:
669 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
670 break;
671 default:
672 if (state->classification.flags & PP_StateClassificationFlag_Boot)
673 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
674 else
675 data->states[i] = POWER_STATE_TYPE_DEFAULT;
676 }
677 }
678
679 return 0;
680}
681
682static int pp_dpm_get_pp_table(void *handle, char **table)
683{
684 struct pp_hwmgr *hwmgr;
685
686 if (!handle)
687 return -EINVAL;
688
689 hwmgr = ((struct pp_instance *)handle)->hwmgr;
690
Rex Zhu7383bcb2016-03-30 11:35:50 +0800691 PP_CHECK_HW(hwmgr);
692
Eric Huang4dcf9e62016-06-01 17:08:07 -0400693 if (!hwmgr->soft_pp_table)
694 return -EINVAL;
Eric Huangf3898ea2015-12-11 16:24:34 -0500695
Eric Huang4dcf9e62016-06-01 17:08:07 -0400696 *table = (char *)hwmgr->soft_pp_table;
697
698 return hwmgr->soft_pp_table_size;
Eric Huangf3898ea2015-12-11 16:24:34 -0500699}
700
701static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
702{
703 struct pp_hwmgr *hwmgr;
704
705 if (!handle)
706 return -EINVAL;
707
708 hwmgr = ((struct pp_instance *)handle)->hwmgr;
709
Rex Zhu7383bcb2016-03-30 11:35:50 +0800710 PP_CHECK_HW(hwmgr);
711
Eric Huang4dcf9e62016-06-01 17:08:07 -0400712 if (!hwmgr->hardcode_pp_table) {
Edward O'Callaghanefdf7a932016-09-04 12:36:19 +1000713 hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
714 hwmgr->soft_pp_table_size,
715 GFP_KERNEL);
Eric Huang4dcf9e62016-06-01 17:08:07 -0400716
717 if (!hwmgr->hardcode_pp_table)
718 return -ENOMEM;
Rex Zhu7383bcb2016-03-30 11:35:50 +0800719 }
Eric Huangf3898ea2015-12-11 16:24:34 -0500720
Eric Huang4dcf9e62016-06-01 17:08:07 -0400721 memcpy(hwmgr->hardcode_pp_table, buf, size);
722
723 hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
724
725 return amd_powerplay_reset(handle);
Eric Huangf3898ea2015-12-11 16:24:34 -0500726}
727
728static int pp_dpm_force_clock_level(void *handle,
Eric Huang56327082016-04-12 14:57:23 -0400729 enum pp_clock_type type, uint32_t mask)
Eric Huangf3898ea2015-12-11 16:24:34 -0500730{
731 struct pp_hwmgr *hwmgr;
732
733 if (!handle)
734 return -EINVAL;
735
736 hwmgr = ((struct pp_instance *)handle)->hwmgr;
737
Rex Zhu7383bcb2016-03-30 11:35:50 +0800738 PP_CHECK_HW(hwmgr);
739
740 if (hwmgr->hwmgr_func->force_clock_level == NULL) {
741 printk(KERN_INFO "%s was not implemented.\n", __func__);
742 return 0;
743 }
Eric Huangf3898ea2015-12-11 16:24:34 -0500744
Eric Huang56327082016-04-12 14:57:23 -0400745 return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
Eric Huangf3898ea2015-12-11 16:24:34 -0500746}
747
748static int pp_dpm_print_clock_levels(void *handle,
749 enum pp_clock_type type, char *buf)
750{
751 struct pp_hwmgr *hwmgr;
752
753 if (!handle)
754 return -EINVAL;
755
756 hwmgr = ((struct pp_instance *)handle)->hwmgr;
757
Rex Zhu7383bcb2016-03-30 11:35:50 +0800758 PP_CHECK_HW(hwmgr);
Eric Huangf3898ea2015-12-11 16:24:34 -0500759
Rex Zhu7383bcb2016-03-30 11:35:50 +0800760 if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
761 printk(KERN_INFO "%s was not implemented.\n", __func__);
762 return 0;
763 }
Eric Huangf3898ea2015-12-11 16:24:34 -0500764 return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
765}
766
Eric Huang428bafa2016-05-12 14:51:21 -0400767static int pp_dpm_get_sclk_od(void *handle)
768{
769 struct pp_hwmgr *hwmgr;
770
771 if (!handle)
772 return -EINVAL;
773
774 hwmgr = ((struct pp_instance *)handle)->hwmgr;
775
776 PP_CHECK_HW(hwmgr);
777
778 if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
779 printk(KERN_INFO "%s was not implemented.\n", __func__);
780 return 0;
781 }
782
783 return hwmgr->hwmgr_func->get_sclk_od(hwmgr);
784}
785
786static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
787{
788 struct pp_hwmgr *hwmgr;
789
790 if (!handle)
791 return -EINVAL;
792
793 hwmgr = ((struct pp_instance *)handle)->hwmgr;
794
795 PP_CHECK_HW(hwmgr);
796
797 if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
798 printk(KERN_INFO "%s was not implemented.\n", __func__);
799 return 0;
800 }
801
802 return hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
803}
804
Eric Huangf2bdc052016-05-24 15:11:17 -0400805static int pp_dpm_get_mclk_od(void *handle)
806{
807 struct pp_hwmgr *hwmgr;
808
809 if (!handle)
810 return -EINVAL;
811
812 hwmgr = ((struct pp_instance *)handle)->hwmgr;
813
814 PP_CHECK_HW(hwmgr);
815
816 if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
817 printk(KERN_INFO "%s was not implemented.\n", __func__);
818 return 0;
819 }
820
821 return hwmgr->hwmgr_func->get_mclk_od(hwmgr);
822}
823
824static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
825{
826 struct pp_hwmgr *hwmgr;
827
828 if (!handle)
829 return -EINVAL;
830
831 hwmgr = ((struct pp_instance *)handle)->hwmgr;
832
833 PP_CHECK_HW(hwmgr);
834
835 if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
836 printk(KERN_INFO "%s was not implemented.\n", __func__);
837 return 0;
838 }
839
840 return hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
841}
842
Tom St Denisa6e36952016-09-15 10:07:34 -0400843static int pp_dpm_read_sensor(void *handle, int idx, int32_t *value)
844{
845 struct pp_hwmgr *hwmgr;
846
847 if (!handle)
848 return -EINVAL;
849
850 hwmgr = ((struct pp_instance *)handle)->hwmgr;
851
852 PP_CHECK_HW(hwmgr);
853
854 if (hwmgr->hwmgr_func->read_sensor == NULL) {
855 printk(KERN_INFO "%s was not implemented.\n", __func__);
856 return 0;
857 }
858
859 return hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value);
860}
861
Alex Deucher597be302016-10-07 13:52:43 -0400862static struct amd_vce_state*
863pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
864{
865 struct pp_hwmgr *hwmgr;
866
867 if (handle) {
868 hwmgr = ((struct pp_instance *)handle)->hwmgr;
869
870 if (hwmgr && idx < hwmgr->num_vce_state_tables)
871 return &hwmgr->vce_states[idx];
872 }
873
874 return NULL;
875}
876
Alex Deucher1f7371b2015-12-02 17:46:21 -0500877const struct amd_powerplay_funcs pp_dpm_funcs = {
Rex Zhucac9a192015-10-16 11:48:21 +0800878 .get_temperature = pp_dpm_get_temperature,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500879 .load_firmware = pp_dpm_load_fw,
880 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
881 .force_performance_level = pp_dpm_force_performance_level,
882 .get_performance_level = pp_dpm_get_performance_level,
883 .get_current_power_state = pp_dpm_get_current_power_state,
884 .get_sclk = pp_dpm_get_sclk,
885 .get_mclk = pp_dpm_get_mclk,
886 .powergate_vce = pp_dpm_powergate_vce,
887 .powergate_uvd = pp_dpm_powergate_uvd,
888 .dispatch_tasks = pp_dpm_dispatch_tasks,
Rex Zhucac9a192015-10-16 11:48:21 +0800889 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
890 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
891 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
892 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
Grazvydas Ignotas72a16a92016-10-29 23:28:58 +0300893 .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
Eric Huangf3898ea2015-12-11 16:24:34 -0500894 .get_pp_num_states = pp_dpm_get_pp_num_states,
895 .get_pp_table = pp_dpm_get_pp_table,
896 .set_pp_table = pp_dpm_set_pp_table,
897 .force_clock_level = pp_dpm_force_clock_level,
898 .print_clock_levels = pp_dpm_print_clock_levels,
Eric Huang428bafa2016-05-12 14:51:21 -0400899 .get_sclk_od = pp_dpm_get_sclk_od,
900 .set_sclk_od = pp_dpm_set_sclk_od,
Eric Huangf2bdc052016-05-24 15:11:17 -0400901 .get_mclk_od = pp_dpm_get_mclk_od,
902 .set_mclk_od = pp_dpm_set_mclk_od,
Tom St Denisa6e36952016-09-15 10:07:34 -0400903 .read_sensor = pp_dpm_read_sensor,
Alex Deucher597be302016-10-07 13:52:43 -0400904 .get_vce_clock_state = pp_dpm_get_vce_clock_state,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500905};
906
Jammy Zhouac885b32015-07-21 17:43:02 +0800907static int amd_pp_instance_init(struct amd_pp_init *pp_init,
908 struct amd_powerplay *amd_pp)
909{
910 int ret;
911 struct pp_instance *handle;
912
913 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
914 if (handle == NULL)
915 return -ENOMEM;
916
Rex Zhua969e162015-12-29 13:56:03 +0800917 handle->pp_valid = PP_VALID;
918
Jammy Zhouac885b32015-07-21 17:43:02 +0800919 ret = smum_init(pp_init, handle);
920 if (ret)
Jammy Zhou3bace352015-07-21 21:18:15 +0800921 goto fail_smum;
922
Rex Zhuba5f8842016-10-27 15:29:57 +0800923
924 amd_pp->pp_handle = handle;
925
Trigger Huang7b1e8ca2016-11-16 10:13:45 -0500926 if ((amdgpu_dpm == 0)
927 || cgs_is_virtualization_enabled(pp_init->device))
Rex Zhuba5f8842016-10-27 15:29:57 +0800928 return 0;
929
Jammy Zhou3bace352015-07-21 21:18:15 +0800930 ret = hwmgr_init(pp_init, handle);
931 if (ret)
932 goto fail_hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800933
Rex Zhue92a0372015-09-23 15:14:54 +0800934 ret = eventmgr_init(handle);
935 if (ret)
936 goto fail_eventmgr;
937
Jammy Zhouac885b32015-07-21 17:43:02 +0800938 return 0;
Jammy Zhou3bace352015-07-21 21:18:15 +0800939
Rex Zhue92a0372015-09-23 15:14:54 +0800940fail_eventmgr:
941 hwmgr_fini(handle->hwmgr);
Jammy Zhou3bace352015-07-21 21:18:15 +0800942fail_hwmgr:
943 smum_fini(handle->smu_mgr);
944fail_smum:
945 kfree(handle);
946 return ret;
Jammy Zhouac885b32015-07-21 17:43:02 +0800947}
948
949static int amd_pp_instance_fini(void *handle)
950{
951 struct pp_instance *instance = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800952
Jammy Zhouac885b32015-07-21 17:43:02 +0800953 if (instance == NULL)
954 return -EINVAL;
955
Trigger Huang7b1e8ca2016-11-16 10:13:45 -0500956 if ((amdgpu_dpm != 0)
957 && !cgs_is_virtualization_enabled(instance->smu_mgr->device)) {
Rex Zhuba5f8842016-10-27 15:29:57 +0800958 eventmgr_fini(instance->eventmgr);
959 hwmgr_fini(instance->hwmgr);
960 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800961
Jammy Zhouac885b32015-07-21 17:43:02 +0800962 smum_fini(instance->smu_mgr);
Jammy Zhouac885b32015-07-21 17:43:02 +0800963 kfree(handle);
964 return 0;
965}
966
Alex Deucher1f7371b2015-12-02 17:46:21 -0500967int amd_powerplay_init(struct amd_pp_init *pp_init,
968 struct amd_powerplay *amd_pp)
969{
Jammy Zhouac885b32015-07-21 17:43:02 +0800970 int ret;
971
Alex Deucher1f7371b2015-12-02 17:46:21 -0500972 if (pp_init == NULL || amd_pp == NULL)
973 return -EINVAL;
974
Jammy Zhouac885b32015-07-21 17:43:02 +0800975 ret = amd_pp_instance_init(pp_init, amd_pp);
976
977 if (ret)
978 return ret;
979
Alex Deucher1f7371b2015-12-02 17:46:21 -0500980 amd_pp->ip_funcs = &pp_ip_funcs;
981 amd_pp->pp_funcs = &pp_dpm_funcs;
982
983 return 0;
984}
985
986int amd_powerplay_fini(void *handle)
987{
Jammy Zhouac885b32015-07-21 17:43:02 +0800988 amd_pp_instance_fini(handle);
989
Alex Deucher1f7371b2015-12-02 17:46:21 -0500990 return 0;
991}
Rex Zhu7fb72a12015-11-19 13:35:30 +0800992
Eric Huang4dcf9e62016-06-01 17:08:07 -0400993int amd_powerplay_reset(void *handle)
994{
995 struct pp_instance *instance = (struct pp_instance *)handle;
996 struct pp_eventmgr *eventmgr;
997 struct pem_event_data event_data = { {0} };
998 int ret;
999
1000 if (instance == NULL)
1001 return -EINVAL;
1002
1003 eventmgr = instance->eventmgr;
1004 if (!eventmgr || !eventmgr->pp_eventmgr_fini)
1005 return -EINVAL;
1006
1007 eventmgr->pp_eventmgr_fini(eventmgr);
1008
1009 ret = pp_sw_fini(handle);
1010 if (ret)
1011 return ret;
1012
1013 kfree(instance->hwmgr->ps);
1014
1015 ret = pp_sw_init(handle);
1016 if (ret)
1017 return ret;
1018
Trigger Huang7b1e8ca2016-11-16 10:13:45 -05001019 if ((amdgpu_dpm == 0)
1020 || cgs_is_virtualization_enabled(instance->smu_mgr->device))
Rex Zhuba5f8842016-10-27 15:29:57 +08001021 return 0;
1022
Xiangliang Yue9efaaa2016-11-30 14:07:16 +08001023 hw_init_power_state_table(instance->hwmgr);
1024
Eric Huang4dcf9e62016-06-01 17:08:07 -04001025 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
1026 return -EINVAL;
1027
1028 ret = eventmgr->pp_eventmgr_init(eventmgr);
1029 if (ret)
1030 return ret;
1031
1032 return pem_handle_event(eventmgr, AMD_PP_EVENT_COMPLETE_INIT, &event_data);
1033}
1034
Rex Zhu7fb72a12015-11-19 13:35:30 +08001035/* export this function to DAL */
1036
David Rokhvarg155f1127c2015-12-14 10:51:39 -05001037int amd_powerplay_display_configuration_change(void *handle,
1038 const struct amd_pp_display_configuration *display_config)
Rex Zhu7fb72a12015-11-19 13:35:30 +08001039{
1040 struct pp_hwmgr *hwmgr;
Rex Zhu7fb72a12015-11-19 13:35:30 +08001041
Rex Zhua969e162015-12-29 13:56:03 +08001042 PP_CHECK((struct pp_instance *)handle);
Rex Zhu7fb72a12015-11-19 13:35:30 +08001043
1044 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1045
Rex Zhuba5f8842016-10-27 15:29:57 +08001046 PP_CHECK_HW(hwmgr);
1047
Rex Zhu7fb72a12015-11-19 13:35:30 +08001048 phm_store_dal_configuration_data(hwmgr, display_config);
Rex Zhue0b71a72015-12-29 10:25:19 +08001049
Rex Zhu7fb72a12015-11-19 13:35:30 +08001050 return 0;
1051}
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001052
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -05001053int amd_powerplay_get_display_power_level(void *handle,
Rex Zhu47329132015-12-10 16:49:50 +08001054 struct amd_pp_simple_clock_info *output)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001055{
1056 struct pp_hwmgr *hwmgr;
1057
Rex Zhua969e162015-12-29 13:56:03 +08001058 PP_CHECK((struct pp_instance *)handle);
1059
1060 if (output == NULL)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001061 return -EINVAL;
1062
1063 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1064
Rex Zhuba5f8842016-10-27 15:29:57 +08001065 PP_CHECK_HW(hwmgr);
1066
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -05001067 return phm_get_dal_power_level(hwmgr, output);
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -05001068}
Rex Zhue273b042015-12-07 18:44:23 +08001069
1070int amd_powerplay_get_current_clocks(void *handle,
David Rokhvarg155f1127c2015-12-14 10:51:39 -05001071 struct amd_pp_clock_info *clocks)
Rex Zhue273b042015-12-07 18:44:23 +08001072{
1073 struct pp_hwmgr *hwmgr;
1074 struct amd_pp_simple_clock_info simple_clocks;
1075 struct pp_clock_info hw_clocks;
Rex Zhue273b042015-12-07 18:44:23 +08001076
Rex Zhufa9e6992015-12-29 13:56:03 +08001077 PP_CHECK((struct pp_instance *)handle);
1078
1079 if (clocks == NULL)
Rex Zhue273b042015-12-07 18:44:23 +08001080 return -EINVAL;
1081
1082 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1083
Rex Zhuba5f8842016-10-27 15:29:57 +08001084 PP_CHECK_HW(hwmgr);
1085
Rex Zhue273b042015-12-07 18:44:23 +08001086 phm_get_dal_power_level(hwmgr, &simple_clocks);
1087
1088 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_PowerContainment)) {
1089 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment))
1090 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetPowerContainmentClockInfo", return -1);
1091 } else {
1092 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_Activity))
1093 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetClockInfo", return -1);
1094 }
1095
1096 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1097 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1098 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1099 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1100 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1101 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1102
1103 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1104 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1105
1106 clocks->max_clocks_state = simple_clocks.level;
1107
1108 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
1109 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1110 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1111 }
1112
1113 return 0;
1114
1115}
1116
1117int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
1118{
1119 int result = -1;
1120
1121 struct pp_hwmgr *hwmgr;
1122
Rex Zhufa9e6992015-12-29 13:56:03 +08001123 PP_CHECK((struct pp_instance *)handle);
1124
1125 if (clocks == NULL)
Rex Zhue273b042015-12-07 18:44:23 +08001126 return -EINVAL;
1127
1128 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1129
Rex Zhuba5f8842016-10-27 15:29:57 +08001130 PP_CHECK_HW(hwmgr);
1131
Rex Zhue273b042015-12-07 18:44:23 +08001132 result = phm_get_clock_by_type(hwmgr, type, clocks);
1133
1134 return result;
1135}
1136
David Rokhvarg155f1127c2015-12-14 10:51:39 -05001137int amd_powerplay_get_display_mode_validation_clocks(void *handle,
1138 struct amd_pp_simple_clock_info *clocks)
Rex Zhue273b042015-12-07 18:44:23 +08001139{
1140 int result = -1;
Rex Zhue273b042015-12-07 18:44:23 +08001141 struct pp_hwmgr *hwmgr;
1142
Rex Zhufa9e6992015-12-29 13:56:03 +08001143 PP_CHECK((struct pp_instance *)handle);
1144
1145 if (clocks == NULL)
Rex Zhue273b042015-12-07 18:44:23 +08001146 return -EINVAL;
1147
1148 hwmgr = ((struct pp_instance *)handle)->hwmgr;
1149
Rex Zhuba5f8842016-10-27 15:29:57 +08001150 PP_CHECK_HW(hwmgr);
1151
Rex Zhue273b042015-12-07 18:44:23 +08001152 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1153 result = phm_get_max_high_clocks(hwmgr, clocks);
1154
1155 return result;
1156}
1157