blob: 9b7c2862c16e5a3f4a0b7a632c8f9e610bafcdba [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 Zhua969e162015-12-29 13:56:03 +080034#define PP_CHECK(handle) \
35 do { \
36 if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
37 return -EINVAL; \
38 } while (0)
39
Alex Deucher1f7371b2015-12-02 17:46:21 -050040static int pp_early_init(void *handle)
41{
42 return 0;
43}
44
45static int pp_sw_init(void *handle)
46{
Jammy Zhou3bace352015-07-21 21:18:15 +080047 struct pp_instance *pp_handle;
48 struct pp_hwmgr *hwmgr;
49 int ret = 0;
50
51 if (handle == NULL)
52 return -EINVAL;
53
54 pp_handle = (struct pp_instance *)handle;
55 hwmgr = pp_handle->hwmgr;
56
57 if (hwmgr == NULL || hwmgr->pptable_func == NULL ||
58 hwmgr->hwmgr_func == NULL ||
59 hwmgr->pptable_func->pptable_init == NULL ||
60 hwmgr->hwmgr_func->backend_init == NULL)
61 return -EINVAL;
62
63 ret = hwmgr->pptable_func->pptable_init(hwmgr);
Rex Zhue92a0372015-09-23 15:14:54 +080064
Jammy Zhou3bace352015-07-21 21:18:15 +080065 if (ret == 0)
66 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
67
Alex Deucher9441f962016-01-20 12:15:09 -050068 if (ret)
69 printk("amdgpu: powerplay initialization failed\n");
70 else
71 printk("amdgpu: powerplay initialized\n");
72
Jammy Zhou3bace352015-07-21 21:18:15 +080073 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050074}
75
76static int pp_sw_fini(void *handle)
77{
Jammy Zhou3bace352015-07-21 21:18:15 +080078 struct pp_instance *pp_handle;
79 struct pp_hwmgr *hwmgr;
80 int ret = 0;
81
82 if (handle == NULL)
83 return -EINVAL;
84
85 pp_handle = (struct pp_instance *)handle;
86 hwmgr = pp_handle->hwmgr;
87
88 if (hwmgr != NULL || hwmgr->hwmgr_func != NULL ||
89 hwmgr->hwmgr_func->backend_fini != NULL)
90 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
91
92 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050093}
94
95static int pp_hw_init(void *handle)
96{
Jammy Zhouac885b32015-07-21 17:43:02 +080097 struct pp_instance *pp_handle;
98 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +080099 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800100 int ret = 0;
101
102 if (handle == NULL)
103 return -EINVAL;
104
105 pp_handle = (struct pp_instance *)handle;
106 smumgr = pp_handle->smu_mgr;
107
108 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
109 smumgr->smumgr_funcs->smu_init == NULL ||
110 smumgr->smumgr_funcs->start_smu == NULL)
111 return -EINVAL;
112
113 ret = smumgr->smumgr_funcs->smu_init(smumgr);
114 if (ret) {
115 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
116 return ret;
117 }
118
119 ret = smumgr->smumgr_funcs->start_smu(smumgr);
120 if (ret) {
121 printk(KERN_ERR "[ powerplay ] smc start failed\n");
122 smumgr->smumgr_funcs->smu_fini(smumgr);
123 return ret;
124 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800125
Rex Zhue92a0372015-09-23 15:14:54 +0800126 hw_init_power_state_table(pp_handle->hwmgr);
127 eventmgr = pp_handle->eventmgr;
128
129 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
130 return -EINVAL;
131
132 ret = eventmgr->pp_eventmgr_init(eventmgr);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500133 return 0;
134}
135
136static int pp_hw_fini(void *handle)
137{
Jammy Zhouac885b32015-07-21 17:43:02 +0800138 struct pp_instance *pp_handle;
139 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800140 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800141
142 if (handle == NULL)
143 return -EINVAL;
144
145 pp_handle = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800146 eventmgr = pp_handle->eventmgr;
147
148 if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
149 eventmgr->pp_eventmgr_fini(eventmgr);
150
Jammy Zhouac885b32015-07-21 17:43:02 +0800151 smumgr = pp_handle->smu_mgr;
152
153 if (smumgr != NULL || smumgr->smumgr_funcs != NULL ||
154 smumgr->smumgr_funcs->smu_fini != NULL)
155 smumgr->smumgr_funcs->smu_fini(smumgr);
156
Alex Deucher1f7371b2015-12-02 17:46:21 -0500157 return 0;
158}
159
160static bool pp_is_idle(void *handle)
161{
162 return 0;
163}
164
165static int pp_wait_for_idle(void *handle)
166{
167 return 0;
168}
169
170static int pp_sw_reset(void *handle)
171{
172 return 0;
173}
174
175static void pp_print_status(void *handle)
176{
177
178}
179
180static int pp_set_clockgating_state(void *handle,
181 enum amd_clockgating_state state)
182{
183 return 0;
184}
185
186static int pp_set_powergating_state(void *handle,
187 enum amd_powergating_state state)
188{
189 return 0;
190}
191
192static int pp_suspend(void *handle)
193{
Rex Zhu577bbe02015-08-28 12:56:43 +0800194 struct pp_instance *pp_handle;
195 struct pp_eventmgr *eventmgr;
196 struct pem_event_data event_data = { {0} };
197
198 if (handle == NULL)
199 return -EINVAL;
200
201 pp_handle = (struct pp_instance *)handle;
202 eventmgr = pp_handle->eventmgr;
203 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500204 return 0;
205}
206
207static int pp_resume(void *handle)
208{
Rex Zhu577bbe02015-08-28 12:56:43 +0800209 struct pp_instance *pp_handle;
210 struct pp_eventmgr *eventmgr;
211 struct pem_event_data event_data = { {0} };
Rex Zhue0b71a72015-12-29 10:25:19 +0800212 struct pp_smumgr *smumgr;
213 int ret;
Rex Zhu577bbe02015-08-28 12:56:43 +0800214
215 if (handle == NULL)
216 return -EINVAL;
217
218 pp_handle = (struct pp_instance *)handle;
Rex Zhue0b71a72015-12-29 10:25:19 +0800219 smumgr = pp_handle->smu_mgr;
220
221 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
222 smumgr->smumgr_funcs->start_smu == NULL)
223 return -EINVAL;
224
225 ret = smumgr->smumgr_funcs->start_smu(smumgr);
226 if (ret) {
227 printk(KERN_ERR "[ powerplay ] smc start failed\n");
228 smumgr->smumgr_funcs->smu_fini(smumgr);
229 return ret;
230 }
231
Rex Zhu577bbe02015-08-28 12:56:43 +0800232 eventmgr = pp_handle->eventmgr;
233 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
Rex Zhue0b71a72015-12-29 10:25:19 +0800234
Alex Deucher1f7371b2015-12-02 17:46:21 -0500235 return 0;
236}
237
238const struct amd_ip_funcs pp_ip_funcs = {
239 .early_init = pp_early_init,
240 .late_init = NULL,
241 .sw_init = pp_sw_init,
242 .sw_fini = pp_sw_fini,
243 .hw_init = pp_hw_init,
244 .hw_fini = pp_hw_fini,
245 .suspend = pp_suspend,
246 .resume = pp_resume,
247 .is_idle = pp_is_idle,
248 .wait_for_idle = pp_wait_for_idle,
249 .soft_reset = pp_sw_reset,
250 .print_status = pp_print_status,
251 .set_clockgating_state = pp_set_clockgating_state,
252 .set_powergating_state = pp_set_powergating_state,
253};
254
255static int pp_dpm_load_fw(void *handle)
256{
257 return 0;
258}
259
260static int pp_dpm_fw_loading_complete(void *handle)
261{
262 return 0;
263}
264
265static int pp_dpm_force_performance_level(void *handle,
266 enum amd_dpm_forced_level level)
267{
Rex Zhu577bbe02015-08-28 12:56:43 +0800268 struct pp_instance *pp_handle;
269 struct pp_hwmgr *hwmgr;
270
271 if (handle == NULL)
272 return -EINVAL;
273
274 pp_handle = (struct pp_instance *)handle;
275
276 hwmgr = pp_handle->hwmgr;
277
278 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
279 hwmgr->hwmgr_func->force_dpm_level == NULL)
280 return -EINVAL;
281
282 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
283
Alex Deucher1f7371b2015-12-02 17:46:21 -0500284 return 0;
285}
Rex Zhu577bbe02015-08-28 12:56:43 +0800286
Alex Deucher1f7371b2015-12-02 17:46:21 -0500287static enum amd_dpm_forced_level pp_dpm_get_performance_level(
288 void *handle)
289{
Rex Zhu577bbe02015-08-28 12:56:43 +0800290 struct pp_hwmgr *hwmgr;
291
292 if (handle == NULL)
293 return -EINVAL;
294
295 hwmgr = ((struct pp_instance *)handle)->hwmgr;
296
297 if (hwmgr == NULL)
298 return -EINVAL;
299
300 return (((struct pp_instance *)handle)->hwmgr->dpm_level);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500301}
Rex Zhu577bbe02015-08-28 12:56:43 +0800302
Alex Deucher1f7371b2015-12-02 17:46:21 -0500303static int pp_dpm_get_sclk(void *handle, bool low)
304{
Rex Zhu577bbe02015-08-28 12:56:43 +0800305 struct pp_hwmgr *hwmgr;
306
307 if (handle == NULL)
308 return -EINVAL;
309
310 hwmgr = ((struct pp_instance *)handle)->hwmgr;
311
312 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
313 hwmgr->hwmgr_func->get_sclk == NULL)
314 return -EINVAL;
315
316 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500317}
Rex Zhu577bbe02015-08-28 12:56:43 +0800318
Alex Deucher1f7371b2015-12-02 17:46:21 -0500319static int pp_dpm_get_mclk(void *handle, bool low)
320{
Rex Zhu577bbe02015-08-28 12:56:43 +0800321 struct pp_hwmgr *hwmgr;
322
323 if (handle == NULL)
324 return -EINVAL;
325
326 hwmgr = ((struct pp_instance *)handle)->hwmgr;
327
328 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
329 hwmgr->hwmgr_func->get_mclk == NULL)
330 return -EINVAL;
331
332 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500333}
Rex Zhu577bbe02015-08-28 12:56:43 +0800334
Alex Deucher1f7371b2015-12-02 17:46:21 -0500335static int pp_dpm_powergate_vce(void *handle, bool gate)
336{
Rex Zhu577bbe02015-08-28 12:56:43 +0800337 struct pp_hwmgr *hwmgr;
338
339 if (handle == NULL)
340 return -EINVAL;
341
342 hwmgr = ((struct pp_instance *)handle)->hwmgr;
343
344 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
345 hwmgr->hwmgr_func->powergate_vce == NULL)
346 return -EINVAL;
347
348 return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500349}
Rex Zhu577bbe02015-08-28 12:56:43 +0800350
Alex Deucher1f7371b2015-12-02 17:46:21 -0500351static int pp_dpm_powergate_uvd(void *handle, bool gate)
352{
Rex Zhu577bbe02015-08-28 12:56:43 +0800353 struct pp_hwmgr *hwmgr;
354
355 if (handle == NULL)
356 return -EINVAL;
357
358 hwmgr = ((struct pp_instance *)handle)->hwmgr;
359
360 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
361 hwmgr->hwmgr_func->powergate_uvd == NULL)
362 return -EINVAL;
363
364 return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
365}
366
367static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
368{
369 switch (state) {
370 case POWER_STATE_TYPE_BATTERY:
371 return PP_StateUILabel_Battery;
372 case POWER_STATE_TYPE_BALANCED:
373 return PP_StateUILabel_Balanced;
374 case POWER_STATE_TYPE_PERFORMANCE:
375 return PP_StateUILabel_Performance;
376 default:
377 return PP_StateUILabel_None;
378 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500379}
380
381int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
382{
Rex Zhu577bbe02015-08-28 12:56:43 +0800383 int ret = 0;
384 struct pp_instance *pp_handle;
385 struct pem_event_data data = { {0} };
386
387 pp_handle = (struct pp_instance *)handle;
388
389 if (pp_handle == NULL)
390 return -EINVAL;
391
392 switch (event_id) {
393 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
394 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
395 break;
396 case AMD_PP_EVENT_ENABLE_USER_STATE:
397 {
398 enum amd_pm_state_type ps;
399
400 if (input == NULL)
401 return -EINVAL;
402 ps = *(unsigned long *)input;
403
404 data.requested_ui_label = power_state_convert(ps);
405 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
406 }
407 break;
408 default:
409 break;
410 }
411 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500412}
Rex Zhu577bbe02015-08-28 12:56:43 +0800413
Alex Deucher1f7371b2015-12-02 17:46:21 -0500414enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
415{
Rex Zhu577bbe02015-08-28 12:56:43 +0800416 struct pp_hwmgr *hwmgr;
417 struct pp_power_state *state;
418
419 if (handle == NULL)
420 return -EINVAL;
421
422 hwmgr = ((struct pp_instance *)handle)->hwmgr;
423
424 if (hwmgr == NULL || hwmgr->current_ps == NULL)
425 return -EINVAL;
426
427 state = hwmgr->current_ps;
428
429 switch (state->classification.ui_label) {
430 case PP_StateUILabel_Battery:
431 return POWER_STATE_TYPE_BATTERY;
432 case PP_StateUILabel_Balanced:
433 return POWER_STATE_TYPE_BALANCED;
434 case PP_StateUILabel_Performance:
435 return POWER_STATE_TYPE_PERFORMANCE;
436 default:
Eric Huangf3898ea2015-12-11 16:24:34 -0500437 if (state->classification.flags & PP_StateClassificationFlag_Boot)
438 return POWER_STATE_TYPE_INTERNAL_BOOT;
439 else
440 return POWER_STATE_TYPE_DEFAULT;
Rex Zhu577bbe02015-08-28 12:56:43 +0800441 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500442}
Rex Zhu577bbe02015-08-28 12:56:43 +0800443
Alex Deucher1f7371b2015-12-02 17:46:21 -0500444static void
445pp_debugfs_print_current_performance_level(void *handle,
446 struct seq_file *m)
447{
Rex Zhu577bbe02015-08-28 12:56:43 +0800448 struct pp_hwmgr *hwmgr;
449
450 if (handle == NULL)
451 return;
452
453 hwmgr = ((struct pp_instance *)handle)->hwmgr;
454
455 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
456 hwmgr->hwmgr_func->print_current_perforce_level == NULL)
457 return;
458
459 hwmgr->hwmgr_func->print_current_perforce_level(hwmgr, m);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500460}
Jammy Zhou3bace352015-07-21 21:18:15 +0800461
Rex Zhucac9a192015-10-16 11:48:21 +0800462static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
463{
464 struct pp_hwmgr *hwmgr;
465
466 if (handle == NULL)
467 return -EINVAL;
468
469 hwmgr = ((struct pp_instance *)handle)->hwmgr;
470
471 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
472 hwmgr->hwmgr_func->set_fan_control_mode == NULL)
473 return -EINVAL;
474
475 return hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
476}
477
478static int pp_dpm_get_fan_control_mode(void *handle)
479{
480 struct pp_hwmgr *hwmgr;
481
482 if (handle == NULL)
483 return -EINVAL;
484
485 hwmgr = ((struct pp_instance *)handle)->hwmgr;
486
487 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
488 hwmgr->hwmgr_func->get_fan_control_mode == NULL)
489 return -EINVAL;
490
491 return hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
492}
493
494static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
495{
496 struct pp_hwmgr *hwmgr;
497
498 if (handle == NULL)
499 return -EINVAL;
500
501 hwmgr = ((struct pp_instance *)handle)->hwmgr;
502
503 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
504 hwmgr->hwmgr_func->set_fan_speed_percent == NULL)
505 return -EINVAL;
506
507 return hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
508}
509
510static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
511{
512 struct pp_hwmgr *hwmgr;
513
514 if (handle == NULL)
515 return -EINVAL;
516
517 hwmgr = ((struct pp_instance *)handle)->hwmgr;
518
519 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
520 hwmgr->hwmgr_func->get_fan_speed_percent == NULL)
521 return -EINVAL;
522
523 return hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
524}
525
526static int pp_dpm_get_temperature(void *handle)
527{
528 struct pp_hwmgr *hwmgr;
529
530 if (handle == NULL)
531 return -EINVAL;
532
533 hwmgr = ((struct pp_instance *)handle)->hwmgr;
534
535 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
536 hwmgr->hwmgr_func->get_temperature == NULL)
537 return -EINVAL;
538
539 return hwmgr->hwmgr_func->get_temperature(hwmgr);
540}
Rex Zhu577bbe02015-08-28 12:56:43 +0800541
Eric Huangf3898ea2015-12-11 16:24:34 -0500542static int pp_dpm_get_pp_num_states(void *handle,
543 struct pp_states_info *data)
544{
545 struct pp_hwmgr *hwmgr;
546 int i;
547
548 if (!handle)
549 return -EINVAL;
550
551 hwmgr = ((struct pp_instance *)handle)->hwmgr;
552
553 if (hwmgr == NULL || hwmgr->ps == NULL)
554 return -EINVAL;
555
556 data->nums = hwmgr->num_ps;
557
558 for (i = 0; i < hwmgr->num_ps; i++) {
559 struct pp_power_state *state = (struct pp_power_state *)
560 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
561 switch (state->classification.ui_label) {
562 case PP_StateUILabel_Battery:
563 data->states[i] = POWER_STATE_TYPE_BATTERY;
564 break;
565 case PP_StateUILabel_Balanced:
566 data->states[i] = POWER_STATE_TYPE_BALANCED;
567 break;
568 case PP_StateUILabel_Performance:
569 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
570 break;
571 default:
572 if (state->classification.flags & PP_StateClassificationFlag_Boot)
573 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
574 else
575 data->states[i] = POWER_STATE_TYPE_DEFAULT;
576 }
577 }
578
579 return 0;
580}
581
582static int pp_dpm_get_pp_table(void *handle, char **table)
583{
584 struct pp_hwmgr *hwmgr;
585
586 if (!handle)
587 return -EINVAL;
588
589 hwmgr = ((struct pp_instance *)handle)->hwmgr;
590
591 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
592 hwmgr->hwmgr_func->get_pp_table == NULL)
593 return -EINVAL;
594
595 return hwmgr->hwmgr_func->get_pp_table(hwmgr, table);
596}
597
598static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
599{
600 struct pp_hwmgr *hwmgr;
601
602 if (!handle)
603 return -EINVAL;
604
605 hwmgr = ((struct pp_instance *)handle)->hwmgr;
606
607 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
608 hwmgr->hwmgr_func->set_pp_table == NULL)
609 return -EINVAL;
610
611 return hwmgr->hwmgr_func->set_pp_table(hwmgr, buf, size);
612}
613
614static int pp_dpm_force_clock_level(void *handle,
615 enum pp_clock_type type, int level)
616{
617 struct pp_hwmgr *hwmgr;
618
619 if (!handle)
620 return -EINVAL;
621
622 hwmgr = ((struct pp_instance *)handle)->hwmgr;
623
624 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
625 hwmgr->hwmgr_func->force_clock_level == NULL)
626 return -EINVAL;
627
628 return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, level);
629}
630
631static int pp_dpm_print_clock_levels(void *handle,
632 enum pp_clock_type type, char *buf)
633{
634 struct pp_hwmgr *hwmgr;
635
636 if (!handle)
637 return -EINVAL;
638
639 hwmgr = ((struct pp_instance *)handle)->hwmgr;
640
641 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
642 hwmgr->hwmgr_func->print_clock_levels == NULL)
643 return -EINVAL;
644
645 return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
646}
647
Alex Deucher1f7371b2015-12-02 17:46:21 -0500648const struct amd_powerplay_funcs pp_dpm_funcs = {
Rex Zhucac9a192015-10-16 11:48:21 +0800649 .get_temperature = pp_dpm_get_temperature,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500650 .load_firmware = pp_dpm_load_fw,
651 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
652 .force_performance_level = pp_dpm_force_performance_level,
653 .get_performance_level = pp_dpm_get_performance_level,
654 .get_current_power_state = pp_dpm_get_current_power_state,
655 .get_sclk = pp_dpm_get_sclk,
656 .get_mclk = pp_dpm_get_mclk,
657 .powergate_vce = pp_dpm_powergate_vce,
658 .powergate_uvd = pp_dpm_powergate_uvd,
659 .dispatch_tasks = pp_dpm_dispatch_tasks,
660 .print_current_performance_level = pp_debugfs_print_current_performance_level,
Rex Zhucac9a192015-10-16 11:48:21 +0800661 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
662 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
663 .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
664 .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
Eric Huangf3898ea2015-12-11 16:24:34 -0500665 .get_pp_num_states = pp_dpm_get_pp_num_states,
666 .get_pp_table = pp_dpm_get_pp_table,
667 .set_pp_table = pp_dpm_set_pp_table,
668 .force_clock_level = pp_dpm_force_clock_level,
669 .print_clock_levels = pp_dpm_print_clock_levels,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500670};
671
Jammy Zhouac885b32015-07-21 17:43:02 +0800672static int amd_pp_instance_init(struct amd_pp_init *pp_init,
673 struct amd_powerplay *amd_pp)
674{
675 int ret;
676 struct pp_instance *handle;
677
678 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
679 if (handle == NULL)
680 return -ENOMEM;
681
Rex Zhua969e162015-12-29 13:56:03 +0800682 handle->pp_valid = PP_VALID;
683
Jammy Zhouac885b32015-07-21 17:43:02 +0800684 ret = smum_init(pp_init, handle);
685 if (ret)
Jammy Zhou3bace352015-07-21 21:18:15 +0800686 goto fail_smum;
687
688 ret = hwmgr_init(pp_init, handle);
689 if (ret)
690 goto fail_hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800691
Rex Zhue92a0372015-09-23 15:14:54 +0800692 ret = eventmgr_init(handle);
693 if (ret)
694 goto fail_eventmgr;
695
Jammy Zhouac885b32015-07-21 17:43:02 +0800696 amd_pp->pp_handle = handle;
697 return 0;
Jammy Zhou3bace352015-07-21 21:18:15 +0800698
Rex Zhue92a0372015-09-23 15:14:54 +0800699fail_eventmgr:
700 hwmgr_fini(handle->hwmgr);
Jammy Zhou3bace352015-07-21 21:18:15 +0800701fail_hwmgr:
702 smum_fini(handle->smu_mgr);
703fail_smum:
704 kfree(handle);
705 return ret;
Jammy Zhouac885b32015-07-21 17:43:02 +0800706}
707
708static int amd_pp_instance_fini(void *handle)
709{
710 struct pp_instance *instance = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800711
Jammy Zhouac885b32015-07-21 17:43:02 +0800712 if (instance == NULL)
713 return -EINVAL;
714
Rex Zhue92a0372015-09-23 15:14:54 +0800715 eventmgr_fini(instance->eventmgr);
716
Jammy Zhou3bace352015-07-21 21:18:15 +0800717 hwmgr_fini(instance->hwmgr);
718
Jammy Zhouac885b32015-07-21 17:43:02 +0800719 smum_fini(instance->smu_mgr);
720
721 kfree(handle);
722 return 0;
723}
724
Alex Deucher1f7371b2015-12-02 17:46:21 -0500725int amd_powerplay_init(struct amd_pp_init *pp_init,
726 struct amd_powerplay *amd_pp)
727{
Jammy Zhouac885b32015-07-21 17:43:02 +0800728 int ret;
729
Alex Deucher1f7371b2015-12-02 17:46:21 -0500730 if (pp_init == NULL || amd_pp == NULL)
731 return -EINVAL;
732
Jammy Zhouac885b32015-07-21 17:43:02 +0800733 ret = amd_pp_instance_init(pp_init, amd_pp);
734
735 if (ret)
736 return ret;
737
Alex Deucher1f7371b2015-12-02 17:46:21 -0500738 amd_pp->ip_funcs = &pp_ip_funcs;
739 amd_pp->pp_funcs = &pp_dpm_funcs;
740
741 return 0;
742}
743
744int amd_powerplay_fini(void *handle)
745{
Jammy Zhouac885b32015-07-21 17:43:02 +0800746 amd_pp_instance_fini(handle);
747
Alex Deucher1f7371b2015-12-02 17:46:21 -0500748 return 0;
749}
Rex Zhu7fb72a12015-11-19 13:35:30 +0800750
751/* export this function to DAL */
752
David Rokhvarg155f1127c2015-12-14 10:51:39 -0500753int amd_powerplay_display_configuration_change(void *handle,
754 const struct amd_pp_display_configuration *display_config)
Rex Zhu7fb72a12015-11-19 13:35:30 +0800755{
756 struct pp_hwmgr *hwmgr;
Rex Zhu7fb72a12015-11-19 13:35:30 +0800757
Rex Zhua969e162015-12-29 13:56:03 +0800758 PP_CHECK((struct pp_instance *)handle);
Rex Zhu7fb72a12015-11-19 13:35:30 +0800759
760 hwmgr = ((struct pp_instance *)handle)->hwmgr;
761
762 phm_store_dal_configuration_data(hwmgr, display_config);
Rex Zhue0b71a72015-12-29 10:25:19 +0800763
Rex Zhu7fb72a12015-11-19 13:35:30 +0800764 return 0;
765}
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500766
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -0500767int amd_powerplay_get_display_power_level(void *handle,
Rex Zhu47329132015-12-10 16:49:50 +0800768 struct amd_pp_simple_clock_info *output)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500769{
770 struct pp_hwmgr *hwmgr;
771
Rex Zhua969e162015-12-29 13:56:03 +0800772 PP_CHECK((struct pp_instance *)handle);
773
774 if (output == NULL)
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500775 return -EINVAL;
776
777 hwmgr = ((struct pp_instance *)handle)->hwmgr;
778
Vitaly Prosyak1c9a9082015-12-03 10:27:57 -0500779 return phm_get_dal_power_level(hwmgr, output);
Vitaly Prosyakc4dd2062015-11-30 16:39:53 -0500780}
Rex Zhue273b042015-12-07 18:44:23 +0800781
782int amd_powerplay_get_current_clocks(void *handle,
David Rokhvarg155f1127c2015-12-14 10:51:39 -0500783 struct amd_pp_clock_info *clocks)
Rex Zhue273b042015-12-07 18:44:23 +0800784{
785 struct pp_hwmgr *hwmgr;
786 struct amd_pp_simple_clock_info simple_clocks;
787 struct pp_clock_info hw_clocks;
Rex Zhue273b042015-12-07 18:44:23 +0800788
David Rokhvarg155f1127c2015-12-14 10:51:39 -0500789 if (handle == NULL || clocks == NULL)
Rex Zhue273b042015-12-07 18:44:23 +0800790 return -EINVAL;
791
792 hwmgr = ((struct pp_instance *)handle)->hwmgr;
793
794 phm_get_dal_power_level(hwmgr, &simple_clocks);
795
796 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_PowerContainment)) {
797 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment))
798 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetPowerContainmentClockInfo", return -1);
799 } else {
800 if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_Activity))
801 PP_ASSERT_WITH_CODE(0, "Error in PHM_GetClockInfo", return -1);
802 }
803
804 clocks->min_engine_clock = hw_clocks.min_eng_clk;
805 clocks->max_engine_clock = hw_clocks.max_eng_clk;
806 clocks->min_memory_clock = hw_clocks.min_mem_clk;
807 clocks->max_memory_clock = hw_clocks.max_mem_clk;
808 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
809 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
810
811 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
812 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
813
814 clocks->max_clocks_state = simple_clocks.level;
815
816 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
817 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
818 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
819 }
820
821 return 0;
822
823}
824
825int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
826{
827 int result = -1;
828
829 struct pp_hwmgr *hwmgr;
830
831 if (handle == NULL || clocks == NULL)
832 return -EINVAL;
833
834 hwmgr = ((struct pp_instance *)handle)->hwmgr;
835
836 result = phm_get_clock_by_type(hwmgr, type, clocks);
837
838 return result;
839}
840
David Rokhvarg155f1127c2015-12-14 10:51:39 -0500841int amd_powerplay_get_display_mode_validation_clocks(void *handle,
842 struct amd_pp_simple_clock_info *clocks)
Rex Zhue273b042015-12-07 18:44:23 +0800843{
844 int result = -1;
Rex Zhue273b042015-12-07 18:44:23 +0800845 struct pp_hwmgr *hwmgr;
846
847 if (handle == NULL || clocks == NULL)
848 return -EINVAL;
849
850 hwmgr = ((struct pp_instance *)handle)->hwmgr;
851
852 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
853 result = phm_get_max_high_clocks(hwmgr, clocks);
854
855 return result;
856}
857