blob: 589599f66fcce851e9ce5b94b2c5617544165022 [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"
Alex Deucher1f7371b2015-12-02 17:46:21 -050032
Rex Zhua969e162015-12-29 13:56:03 +080033#define PP_CHECK(handle) \
34 do { \
35 if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
36 return -EINVAL; \
37 } while (0)
38
Alex Deucher1f7371b2015-12-02 17:46:21 -050039static int pp_early_init(void *handle)
40{
41 return 0;
42}
43
44static int pp_sw_init(void *handle)
45{
Jammy Zhou3bace352015-07-21 21:18:15 +080046 struct pp_instance *pp_handle;
47 struct pp_hwmgr *hwmgr;
48 int ret = 0;
49
50 if (handle == NULL)
51 return -EINVAL;
52
53 pp_handle = (struct pp_instance *)handle;
54 hwmgr = pp_handle->hwmgr;
55
56 if (hwmgr == NULL || hwmgr->pptable_func == NULL ||
57 hwmgr->hwmgr_func == NULL ||
58 hwmgr->pptable_func->pptable_init == NULL ||
59 hwmgr->hwmgr_func->backend_init == NULL)
60 return -EINVAL;
61
62 ret = hwmgr->pptable_func->pptable_init(hwmgr);
Rex Zhue92a0372015-09-23 15:14:54 +080063
Jammy Zhou3bace352015-07-21 21:18:15 +080064 if (ret == 0)
65 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
66
Alex Deucher9441f962016-01-20 12:15:09 -050067 if (ret)
68 printk("amdgpu: powerplay initialization failed\n");
69 else
70 printk("amdgpu: powerplay initialized\n");
71
Jammy Zhou3bace352015-07-21 21:18:15 +080072 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050073}
74
75static int pp_sw_fini(void *handle)
76{
Jammy Zhou3bace352015-07-21 21:18:15 +080077 struct pp_instance *pp_handle;
78 struct pp_hwmgr *hwmgr;
79 int ret = 0;
80
81 if (handle == NULL)
82 return -EINVAL;
83
84 pp_handle = (struct pp_instance *)handle;
85 hwmgr = pp_handle->hwmgr;
86
87 if (hwmgr != NULL || hwmgr->hwmgr_func != NULL ||
88 hwmgr->hwmgr_func->backend_fini != NULL)
89 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
90
91 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050092}
93
94static int pp_hw_init(void *handle)
95{
Jammy Zhouac885b32015-07-21 17:43:02 +080096 struct pp_instance *pp_handle;
97 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +080098 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +080099 int ret = 0;
100
101 if (handle == NULL)
102 return -EINVAL;
103
104 pp_handle = (struct pp_instance *)handle;
105 smumgr = pp_handle->smu_mgr;
106
107 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
108 smumgr->smumgr_funcs->smu_init == NULL ||
109 smumgr->smumgr_funcs->start_smu == NULL)
110 return -EINVAL;
111
112 ret = smumgr->smumgr_funcs->smu_init(smumgr);
113 if (ret) {
114 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
115 return ret;
116 }
117
118 ret = smumgr->smumgr_funcs->start_smu(smumgr);
119 if (ret) {
120 printk(KERN_ERR "[ powerplay ] smc start failed\n");
121 smumgr->smumgr_funcs->smu_fini(smumgr);
122 return ret;
123 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800124
Rex Zhue92a0372015-09-23 15:14:54 +0800125 hw_init_power_state_table(pp_handle->hwmgr);
126 eventmgr = pp_handle->eventmgr;
127
128 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
129 return -EINVAL;
130
131 ret = eventmgr->pp_eventmgr_init(eventmgr);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500132 return 0;
133}
134
135static int pp_hw_fini(void *handle)
136{
Jammy Zhouac885b32015-07-21 17:43:02 +0800137 struct pp_instance *pp_handle;
138 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800139 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800140
141 if (handle == NULL)
142 return -EINVAL;
143
144 pp_handle = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800145 eventmgr = pp_handle->eventmgr;
146
147 if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
148 eventmgr->pp_eventmgr_fini(eventmgr);
149
Jammy Zhouac885b32015-07-21 17:43:02 +0800150 smumgr = pp_handle->smu_mgr;
151
152 if (smumgr != NULL || smumgr->smumgr_funcs != NULL ||
153 smumgr->smumgr_funcs->smu_fini != NULL)
154 smumgr->smumgr_funcs->smu_fini(smumgr);
155
Alex Deucher1f7371b2015-12-02 17:46:21 -0500156 return 0;
157}
158
159static bool pp_is_idle(void *handle)
160{
161 return 0;
162}
163
164static int pp_wait_for_idle(void *handle)
165{
166 return 0;
167}
168
169static int pp_sw_reset(void *handle)
170{
171 return 0;
172}
173
174static void pp_print_status(void *handle)
175{
176
177}
178
179static int pp_set_clockgating_state(void *handle,
180 enum amd_clockgating_state state)
181{
182 return 0;
183}
184
185static int pp_set_powergating_state(void *handle,
186 enum amd_powergating_state state)
187{
188 return 0;
189}
190
191static int pp_suspend(void *handle)
192{
Rex Zhu577bbe02015-08-28 12:56:43 +0800193 struct pp_instance *pp_handle;
194 struct pp_eventmgr *eventmgr;
195 struct pem_event_data event_data = { {0} };
196
197 if (handle == NULL)
198 return -EINVAL;
199
200 pp_handle = (struct pp_instance *)handle;
201 eventmgr = pp_handle->eventmgr;
202 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500203 return 0;
204}
205
206static int pp_resume(void *handle)
207{
Rex Zhu577bbe02015-08-28 12:56:43 +0800208 struct pp_instance *pp_handle;
209 struct pp_eventmgr *eventmgr;
210 struct pem_event_data event_data = { {0} };
Rex Zhue0b71a72015-12-29 10:25:19 +0800211 struct pp_smumgr *smumgr;
212 int ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800213
214 if (handle == NULL)
215 return -EINVAL;
216
217 pp_handle = (struct pp_instance *)handle;
Rex Zhue0b71a72015-12-29 10:25:19 +0800218 smumgr = pp_handle->smu_mgr;
219
220 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
221 smumgr->smumgr_funcs->start_smu == NULL)
222 return -EINVAL;
223
224 ret = smumgr->smumgr_funcs->start_smu(smumgr);
225 if (ret) {
226 printk(KERN_ERR "[ powerplay ] smc start failed\n");
227 smumgr->smumgr_funcs->smu_fini(smumgr);
228 return ret;
229 }
230
Rex Zhu577bbe02015-08-28 12:56:43 +0800231 eventmgr = pp_handle->eventmgr;
232 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
Rex Zhue0b71a72015-12-29 10:25:19 +0800233
Alex Deucher1f7371b2015-12-02 17:46:21 -0500234 return 0;
235}
236
237const struct amd_ip_funcs pp_ip_funcs = {
238 .early_init = pp_early_init,
239 .late_init = NULL,
240 .sw_init = pp_sw_init,
241 .sw_fini = pp_sw_fini,
242 .hw_init = pp_hw_init,
243 .hw_fini = pp_hw_fini,
244 .suspend = pp_suspend,
245 .resume = pp_resume,
246 .is_idle = pp_is_idle,
247 .wait_for_idle = pp_wait_for_idle,
248 .soft_reset = pp_sw_reset,
249 .print_status = pp_print_status,
250 .set_clockgating_state = pp_set_clockgating_state,
251 .set_powergating_state = pp_set_powergating_state,
252};
253
254static int pp_dpm_load_fw(void *handle)
255{
256 return 0;
257}
258
259static int pp_dpm_fw_loading_complete(void *handle)
260{
261 return 0;
262}
263
264static int pp_dpm_force_performance_level(void *handle,
265 enum amd_dpm_forced_level level)
266{
Rex Zhu577bbe02015-08-28 12:56:43 +0800267 struct pp_instance *pp_handle;
268 struct pp_hwmgr *hwmgr;
269
270 if (handle == NULL)
271 return -EINVAL;
272
273 pp_handle = (struct pp_instance *)handle;
274
275 hwmgr = pp_handle->hwmgr;
276
277 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
278 hwmgr->hwmgr_func->force_dpm_level == NULL)
279 return -EINVAL;
280
281 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
282
Alex Deucher1f7371b2015-12-02 17:46:21 -0500283 return 0;
284}
Rex Zhu577bbe02015-08-28 12:56:43 +0800285
Alex Deucher1f7371b2015-12-02 17:46:21 -0500286static enum amd_dpm_forced_level pp_dpm_get_performance_level(
287 void *handle)
288{
Rex Zhu577bbe02015-08-28 12:56:43 +0800289 struct pp_hwmgr *hwmgr;
290
291 if (handle == NULL)
292 return -EINVAL;
293
294 hwmgr = ((struct pp_instance *)handle)->hwmgr;
295
296 if (hwmgr == NULL)
297 return -EINVAL;
298
299 return (((struct pp_instance *)handle)->hwmgr->dpm_level);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500300}
Rex Zhu577bbe02015-08-28 12:56:43 +0800301
Alex Deucher1f7371b2015-12-02 17:46:21 -0500302static int pp_dpm_get_sclk(void *handle, bool low)
303{
Rex Zhu577bbe02015-08-28 12:56:43 +0800304 struct pp_hwmgr *hwmgr;
305
306 if (handle == NULL)
307 return -EINVAL;
308
309 hwmgr = ((struct pp_instance *)handle)->hwmgr;
310
311 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
312 hwmgr->hwmgr_func->get_sclk == NULL)
313 return -EINVAL;
314
315 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500316}
Rex Zhu577bbe02015-08-28 12:56:43 +0800317
Alex Deucher1f7371b2015-12-02 17:46:21 -0500318static int pp_dpm_get_mclk(void *handle, bool low)
319{
Rex Zhu577bbe02015-08-28 12:56:43 +0800320 struct pp_hwmgr *hwmgr;
321
322 if (handle == NULL)
323 return -EINVAL;
324
325 hwmgr = ((struct pp_instance *)handle)->hwmgr;
326
327 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
328 hwmgr->hwmgr_func->get_mclk == NULL)
329 return -EINVAL;
330
331 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500332}
Rex Zhu577bbe02015-08-28 12:56:43 +0800333
Alex Deucher1f7371b2015-12-02 17:46:21 -0500334static int pp_dpm_powergate_vce(void *handle, bool gate)
335{
Rex Zhu577bbe02015-08-28 12:56:43 +0800336 struct pp_hwmgr *hwmgr;
337
338 if (handle == NULL)
339 return -EINVAL;
340
341 hwmgr = ((struct pp_instance *)handle)->hwmgr;
342
343 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
344 hwmgr->hwmgr_func->powergate_vce == NULL)
345 return -EINVAL;
346
347 return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500348}
Rex Zhu577bbe02015-08-28 12:56:43 +0800349
Alex Deucher1f7371b2015-12-02 17:46:21 -0500350static int pp_dpm_powergate_uvd(void *handle, bool gate)
351{
Rex Zhu577bbe02015-08-28 12:56:43 +0800352 struct pp_hwmgr *hwmgr;
353
354 if (handle == NULL)
355 return -EINVAL;
356
357 hwmgr = ((struct pp_instance *)handle)->hwmgr;
358
359 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
360 hwmgr->hwmgr_func->powergate_uvd == NULL)
361 return -EINVAL;
362
363 return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
364}
365
366static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
367{
368 switch (state) {
369 case POWER_STATE_TYPE_BATTERY:
370 return PP_StateUILabel_Battery;
371 case POWER_STATE_TYPE_BALANCED:
372 return PP_StateUILabel_Balanced;
373 case POWER_STATE_TYPE_PERFORMANCE:
374 return PP_StateUILabel_Performance;
375 default:
376 return PP_StateUILabel_None;
377 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500378}
379
380int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
381{
Rex Zhu577bbe02015-08-28 12:56:43 +0800382 int ret = 0;
383 struct pp_instance *pp_handle;
384 struct pem_event_data data = { {0} };
385
386 pp_handle = (struct pp_instance *)handle;
387
388 if (pp_handle == NULL)
389 return -EINVAL;
390
391 switch (event_id) {
392 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
393 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
394 break;
395 case AMD_PP_EVENT_ENABLE_USER_STATE:
396 {
397 enum amd_pm_state_type ps;
398
399 if (input == NULL)
400 return -EINVAL;
401 ps = *(unsigned long *)input;
402
403 data.requested_ui_label = power_state_convert(ps);
404 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
Rex Zhudc26a2a2016-02-25 17:16:52 +0800405 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800406 }
Rex Zhudc26a2a2016-02-25 17:16:52 +0800407 case AMD_PP_EVENT_COMPLETE_INIT:
408 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
409 break;
Rex Zhu577bbe02015-08-28 12:56:43 +0800410 default:
411 break;
412 }
413 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500414}
Rex Zhu577bbe02015-08-28 12:56:43 +0800415
Alex Deucher1f7371b2015-12-02 17:46:21 -0500416enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
417{
Rex Zhu577bbe02015-08-28 12:56:43 +0800418 struct pp_hwmgr *hwmgr;
419 struct pp_power_state *state;
420
421 if (handle == NULL)
422 return -EINVAL;
423
424 hwmgr = ((struct pp_instance *)handle)->hwmgr;
425
426 if (hwmgr == NULL || hwmgr->current_ps == NULL)
427 return -EINVAL;
428
429 state = hwmgr->current_ps;
430
431 switch (state->classification.ui_label) {
432 case PP_StateUILabel_Battery:
433 return POWER_STATE_TYPE_BATTERY;
434 case PP_StateUILabel_Balanced:
435 return POWER_STATE_TYPE_BALANCED;
436 case PP_StateUILabel_Performance:
437 return POWER_STATE_TYPE_PERFORMANCE;
438 default:
439 return POWER_STATE_TYPE_DEFAULT;
440 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500441}
Rex Zhu577bbe02015-08-28 12:56:43 +0800442
Alex Deucher1f7371b2015-12-02 17:46:21 -0500443static void
444pp_debugfs_print_current_performance_level(void *handle,
445 struct seq_file *m)
446{
Rex Zhu577bbe02015-08-28 12:56:43 +0800447 struct pp_hwmgr *hwmgr;
448
449 if (handle == NULL)
450 return;
451
452 hwmgr = ((struct pp_instance *)handle)->hwmgr;
453
454 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
455 hwmgr->hwmgr_func->print_current_perforce_level == NULL)
456 return;
457
458 hwmgr->hwmgr_func->print_current_perforce_level(hwmgr, m);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500459}
Jammy Zhou3bace352015-07-21 21:18:15 +0800460
Rex Zhucac9a192015-10-16 11:48:21 +0800461static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
462{
463 struct pp_hwmgr *hwmgr;
464
465 if (handle == NULL)
466 return -EINVAL;
467
468 hwmgr = ((struct pp_instance *)handle)->hwmgr;
469
470 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
471 hwmgr->hwmgr_func->set_fan_control_mode == NULL)
472 return -EINVAL;
473
474 return hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
475}
476
477static int pp_dpm_get_fan_control_mode(void *handle)
478{
479 struct pp_hwmgr *hwmgr;
480
481 if (handle == NULL)
482 return -EINVAL;
483
484 hwmgr = ((struct pp_instance *)handle)->hwmgr;
485
486 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
487 hwmgr->hwmgr_func->get_fan_control_mode == NULL)
488 return -EINVAL;
489
490 return hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
491}
492
493static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
494{
495 struct pp_hwmgr *hwmgr;
496
497 if (handle == NULL)
498 return -EINVAL;
499
500 hwmgr = ((struct pp_instance *)handle)->hwmgr;
501
502 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
503 hwmgr->hwmgr_func->set_fan_speed_percent == NULL)
504 return -EINVAL;
505
506 return hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
507}
508
509static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
510{
511 struct pp_hwmgr *hwmgr;
512
513 if (handle == NULL)
514 return -EINVAL;
515
516 hwmgr = ((struct pp_instance *)handle)->hwmgr;
517
518 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
519 hwmgr->hwmgr_func->get_fan_speed_percent == NULL)
520 return -EINVAL;
521
522 return hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
523}
524
525static int pp_dpm_get_temperature(void *handle)
526{
527 struct pp_hwmgr *hwmgr;
528
529 if (handle == NULL)
530 return -EINVAL;
531
532 hwmgr = ((struct pp_instance *)handle)->hwmgr;
533
534 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
535 hwmgr->hwmgr_func->get_temperature == NULL)
536 return -EINVAL;
537
538 return hwmgr->hwmgr_func->get_temperature(hwmgr);
539}
Rex Zhu577bbe02015-08-28 12:56:43 +0800540
Alex Deucher1f7371b2015-12-02 17:46:21 -0500541const struct amd_powerplay_funcs pp_dpm_funcs = {
Rex Zhucac9a192015-10-16 11:48:21 +0800542 .get_temperature = pp_dpm_get_temperature,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500543 .load_firmware = pp_dpm_load_fw,
544 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
545 .force_performance_level = pp_dpm_force_performance_level,
546 .get_performance_level = pp_dpm_get_performance_level,
547 .get_current_power_state = pp_dpm_get_current_power_state,
548 .get_sclk = pp_dpm_get_sclk,
549 .get_mclk = pp_dpm_get_mclk,
550 .powergate_vce = pp_dpm_powergate_vce,
551 .powergate_uvd = pp_dpm_powergate_uvd,
552 .dispatch_tasks = pp_dpm_dispatch_tasks,
553 .print_current_performance_level = pp_debugfs_print_current_performance_level,
Rex Zhucac9a192015-10-16 11:48:21 +0800554 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
555 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
556 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
557 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500558};
559
Jammy Zhouac885b32015-07-21 17:43:02 +0800560static int amd_pp_instance_init(struct amd_pp_init *pp_init,
561 struct amd_powerplay *amd_pp)
562{
563 int ret;
564 struct pp_instance *handle;
565
566 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
567 if (handle == NULL)
568 return -ENOMEM;
569
Rex Zhua969e162015-12-29 13:56:03 +0800570 handle->pp_valid = PP_VALID;
571
Jammy Zhouac885b32015-07-21 17:43:02 +0800572 ret = smum_init(pp_init, handle);
573 if (ret)
Jammy Zhou3bace352015-07-21 21:18:15 +0800574 goto fail_smum;
575
576 ret = hwmgr_init(pp_init, handle);
577 if (ret)
578 goto fail_hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800579
Rex Zhue92a0372015-09-23 15:14:54 +0800580 ret = eventmgr_init(handle);
581 if (ret)
582 goto fail_eventmgr;
583
Jammy Zhouac885b32015-07-21 17:43:02 +0800584 amd_pp->pp_handle = handle;
585 return 0;
Jammy Zhou3bace352015-07-21 21:18:15 +0800586
Rex Zhue92a0372015-09-23 15:14:54 +0800587fail_eventmgr:
588 hwmgr_fini(handle->hwmgr);
Jammy Zhou3bace352015-07-21 21:18:15 +0800589fail_hwmgr:
590 smum_fini(handle->smu_mgr);
591fail_smum:
592 kfree(handle);
593 return ret;
Jammy Zhouac885b32015-07-21 17:43:02 +0800594}
595
596static int amd_pp_instance_fini(void *handle)
597{
598 struct pp_instance *instance = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800599
Jammy Zhouac885b32015-07-21 17:43:02 +0800600 if (instance == NULL)
601 return -EINVAL;
602
Rex Zhue92a0372015-09-23 15:14:54 +0800603 eventmgr_fini(instance->eventmgr);
604
Jammy Zhou3bace352015-07-21 21:18:15 +0800605 hwmgr_fini(instance->hwmgr);
606
Jammy Zhouac885b32015-07-21 17:43:02 +0800607 smum_fini(instance->smu_mgr);
608
609 kfree(handle);
610 return 0;
611}
612
Alex Deucher1f7371b2015-12-02 17:46:21 -0500613int amd_powerplay_init(struct amd_pp_init *pp_init,
614 struct amd_powerplay *amd_pp)
615{
Jammy Zhouac885b32015-07-21 17:43:02 +0800616 int ret;
617
Alex Deucher1f7371b2015-12-02 17:46:21 -0500618 if (pp_init == NULL || amd_pp == NULL)
619 return -EINVAL;
620
Jammy Zhouac885b32015-07-21 17:43:02 +0800621 ret = amd_pp_instance_init(pp_init, amd_pp);
622
623 if (ret)
624 return ret;
625
Alex Deucher1f7371b2015-12-02 17:46:21 -0500626 amd_pp->ip_funcs = &pp_ip_funcs;
627 amd_pp->pp_funcs = &pp_dpm_funcs;
628
629 return 0;
630}
631
632int amd_powerplay_fini(void *handle)
633{
Jammy Zhouac885b32015-07-21 17:43:02 +0800634 amd_pp_instance_fini(handle);
635
Alex Deucher1f7371b2015-12-02 17:46:21 -0500636 return 0;
637}
Rex Zhu7fb72a12015-11-19 13:35:30 +0800638
639/* export this function to DAL */
640
641int amd_powerplay_display_configuration_change(void *handle, const void *input)
642{
643 struct pp_hwmgr *hwmgr;
644 const struct amd_pp_display_configuration *display_config = input;
645
Rex Zhua969e162015-12-29 13:56:03 +0800646 PP_CHECK((struct pp_instance *)handle);
Rex Zhu7fb72a12015-11-19 13:35:30 +0800647
648 hwmgr = ((struct pp_instance *)handle)->hwmgr;
649
650 phm_store_dal_configuration_data(hwmgr, display_config);
Rex Zhue0b71a72015-12-29 10:25:19 +0800651
Rex Zhu7fb72a12015-11-19 13:35:30 +0800652 return 0;
653}
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500654
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -0500655int amd_powerplay_get_display_power_level(void *handle,
656 struct amd_pp_dal_clock_info *output)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500657{
658 struct pp_hwmgr *hwmgr;
659
Rex Zhua969e162015-12-29 13:56:03 +0800660 PP_CHECK((struct pp_instance *)handle);
661
662 if (output == NULL)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500663 return -EINVAL;
664
665 hwmgr = ((struct pp_instance *)handle)->hwmgr;
666
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -0500667 return phm_get_dal_power_level(hwmgr, output);
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500668}