blob: 8f5d5edcf193b67a67f6990824f8ef8260e1afa5 [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
67 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050068}
69
70static int pp_sw_fini(void *handle)
71{
Jammy Zhou3bace352015-07-21 21:18:15 +080072 struct pp_instance *pp_handle;
73 struct pp_hwmgr *hwmgr;
74 int ret = 0;
75
76 if (handle == NULL)
77 return -EINVAL;
78
79 pp_handle = (struct pp_instance *)handle;
80 hwmgr = pp_handle->hwmgr;
81
82 if (hwmgr != NULL || hwmgr->hwmgr_func != NULL ||
83 hwmgr->hwmgr_func->backend_fini != NULL)
84 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
85
86 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050087}
88
89static int pp_hw_init(void *handle)
90{
Jammy Zhouac885b32015-07-21 17:43:02 +080091 struct pp_instance *pp_handle;
92 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +080093 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +080094 int ret = 0;
95
96 if (handle == NULL)
97 return -EINVAL;
98
99 pp_handle = (struct pp_instance *)handle;
100 smumgr = pp_handle->smu_mgr;
101
102 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
103 smumgr->smumgr_funcs->smu_init == NULL ||
104 smumgr->smumgr_funcs->start_smu == NULL)
105 return -EINVAL;
106
107 ret = smumgr->smumgr_funcs->smu_init(smumgr);
108 if (ret) {
109 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
110 return ret;
111 }
112
113 ret = smumgr->smumgr_funcs->start_smu(smumgr);
114 if (ret) {
115 printk(KERN_ERR "[ powerplay ] smc start failed\n");
116 smumgr->smumgr_funcs->smu_fini(smumgr);
117 return ret;
118 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800119
Rex Zhue92a0372015-09-23 15:14:54 +0800120 hw_init_power_state_table(pp_handle->hwmgr);
121 eventmgr = pp_handle->eventmgr;
122
123 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
124 return -EINVAL;
125
126 ret = eventmgr->pp_eventmgr_init(eventmgr);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500127 return 0;
128}
129
130static int pp_hw_fini(void *handle)
131{
Jammy Zhouac885b32015-07-21 17:43:02 +0800132 struct pp_instance *pp_handle;
133 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800134 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800135
136 if (handle == NULL)
137 return -EINVAL;
138
139 pp_handle = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800140 eventmgr = pp_handle->eventmgr;
141
142 if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
143 eventmgr->pp_eventmgr_fini(eventmgr);
144
Jammy Zhouac885b32015-07-21 17:43:02 +0800145 smumgr = pp_handle->smu_mgr;
146
147 if (smumgr != NULL || smumgr->smumgr_funcs != NULL ||
148 smumgr->smumgr_funcs->smu_fini != NULL)
149 smumgr->smumgr_funcs->smu_fini(smumgr);
150
Alex Deucher1f7371b2015-12-02 17:46:21 -0500151 return 0;
152}
153
154static bool pp_is_idle(void *handle)
155{
156 return 0;
157}
158
159static int pp_wait_for_idle(void *handle)
160{
161 return 0;
162}
163
164static int pp_sw_reset(void *handle)
165{
166 return 0;
167}
168
169static void pp_print_status(void *handle)
170{
171
172}
173
174static int pp_set_clockgating_state(void *handle,
175 enum amd_clockgating_state state)
176{
177 return 0;
178}
179
180static int pp_set_powergating_state(void *handle,
181 enum amd_powergating_state state)
182{
183 return 0;
184}
185
186static int pp_suspend(void *handle)
187{
Rex Zhu577bbe02015-08-28 12:56:43 +0800188 struct pp_instance *pp_handle;
189 struct pp_eventmgr *eventmgr;
190 struct pem_event_data event_data = { {0} };
191
192 if (handle == NULL)
193 return -EINVAL;
194
195 pp_handle = (struct pp_instance *)handle;
196 eventmgr = pp_handle->eventmgr;
197 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500198 return 0;
199}
200
201static int pp_resume(void *handle)
202{
Rex Zhu577bbe02015-08-28 12:56:43 +0800203 struct pp_instance *pp_handle;
204 struct pp_eventmgr *eventmgr;
205 struct pem_event_data event_data = { {0} };
Rex Zhue0b71a72015-12-29 10:25:19 +0800206 struct pp_smumgr *smumgr;
207 int ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800208
209 if (handle == NULL)
210 return -EINVAL;
211
212 pp_handle = (struct pp_instance *)handle;
Rex Zhue0b71a72015-12-29 10:25:19 +0800213 smumgr = pp_handle->smu_mgr;
214
215 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
216 smumgr->smumgr_funcs->start_smu == NULL)
217 return -EINVAL;
218
219 ret = smumgr->smumgr_funcs->start_smu(smumgr);
220 if (ret) {
221 printk(KERN_ERR "[ powerplay ] smc start failed\n");
222 smumgr->smumgr_funcs->smu_fini(smumgr);
223 return ret;
224 }
225
Rex Zhu577bbe02015-08-28 12:56:43 +0800226 eventmgr = pp_handle->eventmgr;
227 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
Rex Zhue0b71a72015-12-29 10:25:19 +0800228
Alex Deucher1f7371b2015-12-02 17:46:21 -0500229 return 0;
230}
231
232const struct amd_ip_funcs pp_ip_funcs = {
233 .early_init = pp_early_init,
234 .late_init = NULL,
235 .sw_init = pp_sw_init,
236 .sw_fini = pp_sw_fini,
237 .hw_init = pp_hw_init,
238 .hw_fini = pp_hw_fini,
239 .suspend = pp_suspend,
240 .resume = pp_resume,
241 .is_idle = pp_is_idle,
242 .wait_for_idle = pp_wait_for_idle,
243 .soft_reset = pp_sw_reset,
244 .print_status = pp_print_status,
245 .set_clockgating_state = pp_set_clockgating_state,
246 .set_powergating_state = pp_set_powergating_state,
247};
248
249static int pp_dpm_load_fw(void *handle)
250{
251 return 0;
252}
253
254static int pp_dpm_fw_loading_complete(void *handle)
255{
256 return 0;
257}
258
259static int pp_dpm_force_performance_level(void *handle,
260 enum amd_dpm_forced_level level)
261{
Rex Zhu577bbe02015-08-28 12:56:43 +0800262 struct pp_instance *pp_handle;
263 struct pp_hwmgr *hwmgr;
264
265 if (handle == NULL)
266 return -EINVAL;
267
268 pp_handle = (struct pp_instance *)handle;
269
270 hwmgr = pp_handle->hwmgr;
271
272 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
273 hwmgr->hwmgr_func->force_dpm_level == NULL)
274 return -EINVAL;
275
276 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
277
Alex Deucher1f7371b2015-12-02 17:46:21 -0500278 return 0;
279}
Rex Zhu577bbe02015-08-28 12:56:43 +0800280
Alex Deucher1f7371b2015-12-02 17:46:21 -0500281static enum amd_dpm_forced_level pp_dpm_get_performance_level(
282 void *handle)
283{
Rex Zhu577bbe02015-08-28 12:56:43 +0800284 struct pp_hwmgr *hwmgr;
285
286 if (handle == NULL)
287 return -EINVAL;
288
289 hwmgr = ((struct pp_instance *)handle)->hwmgr;
290
291 if (hwmgr == NULL)
292 return -EINVAL;
293
294 return (((struct pp_instance *)handle)->hwmgr->dpm_level);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500295}
Rex Zhu577bbe02015-08-28 12:56:43 +0800296
Alex Deucher1f7371b2015-12-02 17:46:21 -0500297static int pp_dpm_get_sclk(void *handle, bool low)
298{
Rex Zhu577bbe02015-08-28 12:56:43 +0800299 struct pp_hwmgr *hwmgr;
300
301 if (handle == NULL)
302 return -EINVAL;
303
304 hwmgr = ((struct pp_instance *)handle)->hwmgr;
305
306 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
307 hwmgr->hwmgr_func->get_sclk == NULL)
308 return -EINVAL;
309
310 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500311}
Rex Zhu577bbe02015-08-28 12:56:43 +0800312
Alex Deucher1f7371b2015-12-02 17:46:21 -0500313static int pp_dpm_get_mclk(void *handle, bool low)
314{
Rex Zhu577bbe02015-08-28 12:56:43 +0800315 struct pp_hwmgr *hwmgr;
316
317 if (handle == NULL)
318 return -EINVAL;
319
320 hwmgr = ((struct pp_instance *)handle)->hwmgr;
321
322 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
323 hwmgr->hwmgr_func->get_mclk == NULL)
324 return -EINVAL;
325
326 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500327}
Rex Zhu577bbe02015-08-28 12:56:43 +0800328
Alex Deucher1f7371b2015-12-02 17:46:21 -0500329static int pp_dpm_powergate_vce(void *handle, bool gate)
330{
Rex Zhu577bbe02015-08-28 12:56:43 +0800331 struct pp_hwmgr *hwmgr;
332
333 if (handle == NULL)
334 return -EINVAL;
335
336 hwmgr = ((struct pp_instance *)handle)->hwmgr;
337
338 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
339 hwmgr->hwmgr_func->powergate_vce == NULL)
340 return -EINVAL;
341
342 return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500343}
Rex Zhu577bbe02015-08-28 12:56:43 +0800344
Alex Deucher1f7371b2015-12-02 17:46:21 -0500345static int pp_dpm_powergate_uvd(void *handle, bool gate)
346{
Rex Zhu577bbe02015-08-28 12:56:43 +0800347 struct pp_hwmgr *hwmgr;
348
349 if (handle == NULL)
350 return -EINVAL;
351
352 hwmgr = ((struct pp_instance *)handle)->hwmgr;
353
354 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
355 hwmgr->hwmgr_func->powergate_uvd == NULL)
356 return -EINVAL;
357
358 return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
359}
360
361static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
362{
363 switch (state) {
364 case POWER_STATE_TYPE_BATTERY:
365 return PP_StateUILabel_Battery;
366 case POWER_STATE_TYPE_BALANCED:
367 return PP_StateUILabel_Balanced;
368 case POWER_STATE_TYPE_PERFORMANCE:
369 return PP_StateUILabel_Performance;
370 default:
371 return PP_StateUILabel_None;
372 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500373}
374
375int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
376{
Rex Zhu577bbe02015-08-28 12:56:43 +0800377 int ret = 0;
378 struct pp_instance *pp_handle;
379 struct pem_event_data data = { {0} };
380
381 pp_handle = (struct pp_instance *)handle;
382
383 if (pp_handle == NULL)
384 return -EINVAL;
385
386 switch (event_id) {
387 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
388 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
389 break;
390 case AMD_PP_EVENT_ENABLE_USER_STATE:
391 {
392 enum amd_pm_state_type ps;
393
394 if (input == NULL)
395 return -EINVAL;
396 ps = *(unsigned long *)input;
397
398 data.requested_ui_label = power_state_convert(ps);
399 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
400 }
401 break;
402 default:
403 break;
404 }
405 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500406}
Rex Zhu577bbe02015-08-28 12:56:43 +0800407
Alex Deucher1f7371b2015-12-02 17:46:21 -0500408enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
409{
Rex Zhu577bbe02015-08-28 12:56:43 +0800410 struct pp_hwmgr *hwmgr;
411 struct pp_power_state *state;
412
413 if (handle == NULL)
414 return -EINVAL;
415
416 hwmgr = ((struct pp_instance *)handle)->hwmgr;
417
418 if (hwmgr == NULL || hwmgr->current_ps == NULL)
419 return -EINVAL;
420
421 state = hwmgr->current_ps;
422
423 switch (state->classification.ui_label) {
424 case PP_StateUILabel_Battery:
425 return POWER_STATE_TYPE_BATTERY;
426 case PP_StateUILabel_Balanced:
427 return POWER_STATE_TYPE_BALANCED;
428 case PP_StateUILabel_Performance:
429 return POWER_STATE_TYPE_PERFORMANCE;
430 default:
431 return POWER_STATE_TYPE_DEFAULT;
432 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500433}
Rex Zhu577bbe02015-08-28 12:56:43 +0800434
Alex Deucher1f7371b2015-12-02 17:46:21 -0500435static void
436pp_debugfs_print_current_performance_level(void *handle,
437 struct seq_file *m)
438{
Rex Zhu577bbe02015-08-28 12:56:43 +0800439 struct pp_hwmgr *hwmgr;
440
441 if (handle == NULL)
442 return;
443
444 hwmgr = ((struct pp_instance *)handle)->hwmgr;
445
446 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
447 hwmgr->hwmgr_func->print_current_perforce_level == NULL)
448 return;
449
450 hwmgr->hwmgr_func->print_current_perforce_level(hwmgr, m);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500451}
Jammy Zhou3bace352015-07-21 21:18:15 +0800452
Rex Zhucac9a192015-10-16 11:48:21 +0800453static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
454{
455 struct pp_hwmgr *hwmgr;
456
457 if (handle == NULL)
458 return -EINVAL;
459
460 hwmgr = ((struct pp_instance *)handle)->hwmgr;
461
462 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
463 hwmgr->hwmgr_func->set_fan_control_mode == NULL)
464 return -EINVAL;
465
466 return hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
467}
468
469static int pp_dpm_get_fan_control_mode(void *handle)
470{
471 struct pp_hwmgr *hwmgr;
472
473 if (handle == NULL)
474 return -EINVAL;
475
476 hwmgr = ((struct pp_instance *)handle)->hwmgr;
477
478 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
479 hwmgr->hwmgr_func->get_fan_control_mode == NULL)
480 return -EINVAL;
481
482 return hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
483}
484
485static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
486{
487 struct pp_hwmgr *hwmgr;
488
489 if (handle == NULL)
490 return -EINVAL;
491
492 hwmgr = ((struct pp_instance *)handle)->hwmgr;
493
494 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
495 hwmgr->hwmgr_func->set_fan_speed_percent == NULL)
496 return -EINVAL;
497
498 return hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
499}
500
501static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
502{
503 struct pp_hwmgr *hwmgr;
504
505 if (handle == NULL)
506 return -EINVAL;
507
508 hwmgr = ((struct pp_instance *)handle)->hwmgr;
509
510 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
511 hwmgr->hwmgr_func->get_fan_speed_percent == NULL)
512 return -EINVAL;
513
514 return hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
515}
516
517static int pp_dpm_get_temperature(void *handle)
518{
519 struct pp_hwmgr *hwmgr;
520
521 if (handle == NULL)
522 return -EINVAL;
523
524 hwmgr = ((struct pp_instance *)handle)->hwmgr;
525
526 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
527 hwmgr->hwmgr_func->get_temperature == NULL)
528 return -EINVAL;
529
530 return hwmgr->hwmgr_func->get_temperature(hwmgr);
531}
Rex Zhu577bbe02015-08-28 12:56:43 +0800532
Alex Deucher1f7371b2015-12-02 17:46:21 -0500533const struct amd_powerplay_funcs pp_dpm_funcs = {
Rex Zhucac9a192015-10-16 11:48:21 +0800534 .get_temperature = pp_dpm_get_temperature,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500535 .load_firmware = pp_dpm_load_fw,
536 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
537 .force_performance_level = pp_dpm_force_performance_level,
538 .get_performance_level = pp_dpm_get_performance_level,
539 .get_current_power_state = pp_dpm_get_current_power_state,
540 .get_sclk = pp_dpm_get_sclk,
541 .get_mclk = pp_dpm_get_mclk,
542 .powergate_vce = pp_dpm_powergate_vce,
543 .powergate_uvd = pp_dpm_powergate_uvd,
544 .dispatch_tasks = pp_dpm_dispatch_tasks,
545 .print_current_performance_level = pp_debugfs_print_current_performance_level,
Rex Zhucac9a192015-10-16 11:48:21 +0800546 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
547 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
548 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
549 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500550};
551
Jammy Zhouac885b32015-07-21 17:43:02 +0800552static int amd_pp_instance_init(struct amd_pp_init *pp_init,
553 struct amd_powerplay *amd_pp)
554{
555 int ret;
556 struct pp_instance *handle;
557
558 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
559 if (handle == NULL)
560 return -ENOMEM;
561
Rex Zhua969e162015-12-29 13:56:03 +0800562 handle->pp_valid = PP_VALID;
563
Jammy Zhouac885b32015-07-21 17:43:02 +0800564 ret = smum_init(pp_init, handle);
565 if (ret)
Jammy Zhou3bace352015-07-21 21:18:15 +0800566 goto fail_smum;
567
568 ret = hwmgr_init(pp_init, handle);
569 if (ret)
570 goto fail_hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800571
Rex Zhue92a0372015-09-23 15:14:54 +0800572 ret = eventmgr_init(handle);
573 if (ret)
574 goto fail_eventmgr;
575
Jammy Zhouac885b32015-07-21 17:43:02 +0800576 amd_pp->pp_handle = handle;
577 return 0;
Jammy Zhou3bace352015-07-21 21:18:15 +0800578
Rex Zhue92a0372015-09-23 15:14:54 +0800579fail_eventmgr:
580 hwmgr_fini(handle->hwmgr);
Jammy Zhou3bace352015-07-21 21:18:15 +0800581fail_hwmgr:
582 smum_fini(handle->smu_mgr);
583fail_smum:
584 kfree(handle);
585 return ret;
Jammy Zhouac885b32015-07-21 17:43:02 +0800586}
587
588static int amd_pp_instance_fini(void *handle)
589{
590 struct pp_instance *instance = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800591
Jammy Zhouac885b32015-07-21 17:43:02 +0800592 if (instance == NULL)
593 return -EINVAL;
594
Rex Zhue92a0372015-09-23 15:14:54 +0800595 eventmgr_fini(instance->eventmgr);
596
Jammy Zhou3bace352015-07-21 21:18:15 +0800597 hwmgr_fini(instance->hwmgr);
598
Jammy Zhouac885b32015-07-21 17:43:02 +0800599 smum_fini(instance->smu_mgr);
600
601 kfree(handle);
602 return 0;
603}
604
Alex Deucher1f7371b2015-12-02 17:46:21 -0500605int amd_powerplay_init(struct amd_pp_init *pp_init,
606 struct amd_powerplay *amd_pp)
607{
Jammy Zhouac885b32015-07-21 17:43:02 +0800608 int ret;
609
Alex Deucher1f7371b2015-12-02 17:46:21 -0500610 if (pp_init == NULL || amd_pp == NULL)
611 return -EINVAL;
612
Jammy Zhouac885b32015-07-21 17:43:02 +0800613 ret = amd_pp_instance_init(pp_init, amd_pp);
614
615 if (ret)
616 return ret;
617
Alex Deucher1f7371b2015-12-02 17:46:21 -0500618 amd_pp->ip_funcs = &pp_ip_funcs;
619 amd_pp->pp_funcs = &pp_dpm_funcs;
620
621 return 0;
622}
623
624int amd_powerplay_fini(void *handle)
625{
Jammy Zhouac885b32015-07-21 17:43:02 +0800626 amd_pp_instance_fini(handle);
627
Alex Deucher1f7371b2015-12-02 17:46:21 -0500628 return 0;
629}
Rex Zhu7fb72a12015-11-19 13:35:30 +0800630
631/* export this function to DAL */
632
633int amd_powerplay_display_configuration_change(void *handle, const void *input)
634{
635 struct pp_hwmgr *hwmgr;
636 const struct amd_pp_display_configuration *display_config = input;
637
Rex Zhua969e162015-12-29 13:56:03 +0800638 PP_CHECK((struct pp_instance *)handle);
Rex Zhu7fb72a12015-11-19 13:35:30 +0800639
640 hwmgr = ((struct pp_instance *)handle)->hwmgr;
641
642 phm_store_dal_configuration_data(hwmgr, display_config);
Rex Zhue0b71a72015-12-29 10:25:19 +0800643
Rex Zhu7fb72a12015-11-19 13:35:30 +0800644 return 0;
645}
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500646
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -0500647int amd_powerplay_get_display_power_level(void *handle,
648 struct amd_pp_dal_clock_info *output)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500649{
650 struct pp_hwmgr *hwmgr;
651
Rex Zhua969e162015-12-29 13:56:03 +0800652 PP_CHECK((struct pp_instance *)handle);
653
654 if (output == NULL)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500655 return -EINVAL;
656
657 hwmgr = ((struct pp_instance *)handle)->hwmgr;
658
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -0500659 return phm_get_dal_power_level(hwmgr, output);
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500660}