blob: 66ccfc0fc6221568d4998db7db95dd2a7180da6f [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
33static int pp_early_init(void *handle)
34{
35 return 0;
36}
37
38static int pp_sw_init(void *handle)
39{
Jammy Zhou3bace352015-07-21 21:18:15 +080040 struct pp_instance *pp_handle;
41 struct pp_hwmgr *hwmgr;
42 int ret = 0;
43
44 if (handle == NULL)
45 return -EINVAL;
46
47 pp_handle = (struct pp_instance *)handle;
48 hwmgr = pp_handle->hwmgr;
49
50 if (hwmgr == NULL || hwmgr->pptable_func == NULL ||
51 hwmgr->hwmgr_func == NULL ||
52 hwmgr->pptable_func->pptable_init == NULL ||
53 hwmgr->hwmgr_func->backend_init == NULL)
54 return -EINVAL;
55
56 ret = hwmgr->pptable_func->pptable_init(hwmgr);
Rex Zhue92a0372015-09-23 15:14:54 +080057
Jammy Zhou3bace352015-07-21 21:18:15 +080058 if (ret == 0)
59 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
60
61 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050062}
63
64static int pp_sw_fini(void *handle)
65{
Jammy Zhou3bace352015-07-21 21:18:15 +080066 struct pp_instance *pp_handle;
67 struct pp_hwmgr *hwmgr;
68 int ret = 0;
69
70 if (handle == NULL)
71 return -EINVAL;
72
73 pp_handle = (struct pp_instance *)handle;
74 hwmgr = pp_handle->hwmgr;
75
76 if (hwmgr != NULL || hwmgr->hwmgr_func != NULL ||
77 hwmgr->hwmgr_func->backend_fini != NULL)
78 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
79
80 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050081}
82
83static int pp_hw_init(void *handle)
84{
Jammy Zhouac885b32015-07-21 17:43:02 +080085 struct pp_instance *pp_handle;
86 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +080087 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +080088 int ret = 0;
89
90 if (handle == NULL)
91 return -EINVAL;
92
93 pp_handle = (struct pp_instance *)handle;
94 smumgr = pp_handle->smu_mgr;
95
96 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
97 smumgr->smumgr_funcs->smu_init == NULL ||
98 smumgr->smumgr_funcs->start_smu == NULL)
99 return -EINVAL;
100
101 ret = smumgr->smumgr_funcs->smu_init(smumgr);
102 if (ret) {
103 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
104 return ret;
105 }
106
107 ret = smumgr->smumgr_funcs->start_smu(smumgr);
108 if (ret) {
109 printk(KERN_ERR "[ powerplay ] smc start failed\n");
110 smumgr->smumgr_funcs->smu_fini(smumgr);
111 return ret;
112 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800113
Rex Zhue92a0372015-09-23 15:14:54 +0800114 hw_init_power_state_table(pp_handle->hwmgr);
115 eventmgr = pp_handle->eventmgr;
116
117 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
118 return -EINVAL;
119
120 ret = eventmgr->pp_eventmgr_init(eventmgr);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500121 return 0;
122}
123
124static int pp_hw_fini(void *handle)
125{
Jammy Zhouac885b32015-07-21 17:43:02 +0800126 struct pp_instance *pp_handle;
127 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800128 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800129
130 if (handle == NULL)
131 return -EINVAL;
132
133 pp_handle = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800134 eventmgr = pp_handle->eventmgr;
135
136 if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
137 eventmgr->pp_eventmgr_fini(eventmgr);
138
Jammy Zhouac885b32015-07-21 17:43:02 +0800139 smumgr = pp_handle->smu_mgr;
140
141 if (smumgr != NULL || smumgr->smumgr_funcs != NULL ||
142 smumgr->smumgr_funcs->smu_fini != NULL)
143 smumgr->smumgr_funcs->smu_fini(smumgr);
144
Alex Deucher1f7371b2015-12-02 17:46:21 -0500145 return 0;
146}
147
148static bool pp_is_idle(void *handle)
149{
150 return 0;
151}
152
153static int pp_wait_for_idle(void *handle)
154{
155 return 0;
156}
157
158static int pp_sw_reset(void *handle)
159{
160 return 0;
161}
162
163static void pp_print_status(void *handle)
164{
165
166}
167
168static int pp_set_clockgating_state(void *handle,
169 enum amd_clockgating_state state)
170{
171 return 0;
172}
173
174static int pp_set_powergating_state(void *handle,
175 enum amd_powergating_state state)
176{
177 return 0;
178}
179
180static int pp_suspend(void *handle)
181{
Rex Zhu577bbe02015-08-28 12:56:43 +0800182 struct pp_instance *pp_handle;
183 struct pp_eventmgr *eventmgr;
184 struct pem_event_data event_data = { {0} };
185
186 if (handle == NULL)
187 return -EINVAL;
188
189 pp_handle = (struct pp_instance *)handle;
190 eventmgr = pp_handle->eventmgr;
191 pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500192 return 0;
193}
194
195static int pp_resume(void *handle)
196{
Rex Zhu577bbe02015-08-28 12:56:43 +0800197 struct pp_instance *pp_handle;
198 struct pp_eventmgr *eventmgr;
199 struct pem_event_data event_data = { {0} };
200
201 if (handle == NULL)
202 return -EINVAL;
203
204 pp_handle = (struct pp_instance *)handle;
205 eventmgr = pp_handle->eventmgr;
206 pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500207 return 0;
208}
209
210const struct amd_ip_funcs pp_ip_funcs = {
211 .early_init = pp_early_init,
212 .late_init = NULL,
213 .sw_init = pp_sw_init,
214 .sw_fini = pp_sw_fini,
215 .hw_init = pp_hw_init,
216 .hw_fini = pp_hw_fini,
217 .suspend = pp_suspend,
218 .resume = pp_resume,
219 .is_idle = pp_is_idle,
220 .wait_for_idle = pp_wait_for_idle,
221 .soft_reset = pp_sw_reset,
222 .print_status = pp_print_status,
223 .set_clockgating_state = pp_set_clockgating_state,
224 .set_powergating_state = pp_set_powergating_state,
225};
226
227static int pp_dpm_load_fw(void *handle)
228{
229 return 0;
230}
231
232static int pp_dpm_fw_loading_complete(void *handle)
233{
234 return 0;
235}
236
237static int pp_dpm_force_performance_level(void *handle,
238 enum amd_dpm_forced_level level)
239{
Rex Zhu577bbe02015-08-28 12:56:43 +0800240 struct pp_instance *pp_handle;
241 struct pp_hwmgr *hwmgr;
242
243 if (handle == NULL)
244 return -EINVAL;
245
246 pp_handle = (struct pp_instance *)handle;
247
248 hwmgr = pp_handle->hwmgr;
249
250 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
251 hwmgr->hwmgr_func->force_dpm_level == NULL)
252 return -EINVAL;
253
254 hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
255
Alex Deucher1f7371b2015-12-02 17:46:21 -0500256 return 0;
257}
Rex Zhu577bbe02015-08-28 12:56:43 +0800258
Alex Deucher1f7371b2015-12-02 17:46:21 -0500259static enum amd_dpm_forced_level pp_dpm_get_performance_level(
260 void *handle)
261{
Rex Zhu577bbe02015-08-28 12:56:43 +0800262 struct pp_hwmgr *hwmgr;
263
264 if (handle == NULL)
265 return -EINVAL;
266
267 hwmgr = ((struct pp_instance *)handle)->hwmgr;
268
269 if (hwmgr == NULL)
270 return -EINVAL;
271
272 return (((struct pp_instance *)handle)->hwmgr->dpm_level);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500273}
Rex Zhu577bbe02015-08-28 12:56:43 +0800274
Alex Deucher1f7371b2015-12-02 17:46:21 -0500275static int pp_dpm_get_sclk(void *handle, bool low)
276{
Rex Zhu577bbe02015-08-28 12:56:43 +0800277 struct pp_hwmgr *hwmgr;
278
279 if (handle == NULL)
280 return -EINVAL;
281
282 hwmgr = ((struct pp_instance *)handle)->hwmgr;
283
284 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
285 hwmgr->hwmgr_func->get_sclk == NULL)
286 return -EINVAL;
287
288 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500289}
Rex Zhu577bbe02015-08-28 12:56:43 +0800290
Alex Deucher1f7371b2015-12-02 17:46:21 -0500291static int pp_dpm_get_mclk(void *handle, bool low)
292{
Rex Zhu577bbe02015-08-28 12:56:43 +0800293 struct pp_hwmgr *hwmgr;
294
295 if (handle == NULL)
296 return -EINVAL;
297
298 hwmgr = ((struct pp_instance *)handle)->hwmgr;
299
300 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
301 hwmgr->hwmgr_func->get_mclk == NULL)
302 return -EINVAL;
303
304 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500305}
Rex Zhu577bbe02015-08-28 12:56:43 +0800306
Alex Deucher1f7371b2015-12-02 17:46:21 -0500307static int pp_dpm_powergate_vce(void *handle, bool gate)
308{
Rex Zhu577bbe02015-08-28 12:56:43 +0800309 struct pp_hwmgr *hwmgr;
310
311 if (handle == NULL)
312 return -EINVAL;
313
314 hwmgr = ((struct pp_instance *)handle)->hwmgr;
315
316 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
317 hwmgr->hwmgr_func->powergate_vce == NULL)
318 return -EINVAL;
319
320 return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500321}
Rex Zhu577bbe02015-08-28 12:56:43 +0800322
Alex Deucher1f7371b2015-12-02 17:46:21 -0500323static int pp_dpm_powergate_uvd(void *handle, bool gate)
324{
Rex Zhu577bbe02015-08-28 12:56:43 +0800325 struct pp_hwmgr *hwmgr;
326
327 if (handle == NULL)
328 return -EINVAL;
329
330 hwmgr = ((struct pp_instance *)handle)->hwmgr;
331
332 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
333 hwmgr->hwmgr_func->powergate_uvd == NULL)
334 return -EINVAL;
335
336 return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
337}
338
339static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
340{
341 switch (state) {
342 case POWER_STATE_TYPE_BATTERY:
343 return PP_StateUILabel_Battery;
344 case POWER_STATE_TYPE_BALANCED:
345 return PP_StateUILabel_Balanced;
346 case POWER_STATE_TYPE_PERFORMANCE:
347 return PP_StateUILabel_Performance;
348 default:
349 return PP_StateUILabel_None;
350 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500351}
352
353int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
354{
Rex Zhu577bbe02015-08-28 12:56:43 +0800355 int ret = 0;
356 struct pp_instance *pp_handle;
357 struct pem_event_data data = { {0} };
358
359 pp_handle = (struct pp_instance *)handle;
360
361 if (pp_handle == NULL)
362 return -EINVAL;
363
364 switch (event_id) {
365 case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
366 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
367 break;
368 case AMD_PP_EVENT_ENABLE_USER_STATE:
369 {
370 enum amd_pm_state_type ps;
371
372 if (input == NULL)
373 return -EINVAL;
374 ps = *(unsigned long *)input;
375
376 data.requested_ui_label = power_state_convert(ps);
377 ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
378 }
379 break;
380 default:
381 break;
382 }
383 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500384}
Rex Zhu577bbe02015-08-28 12:56:43 +0800385
Alex Deucher1f7371b2015-12-02 17:46:21 -0500386enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
387{
Rex Zhu577bbe02015-08-28 12:56:43 +0800388 struct pp_hwmgr *hwmgr;
389 struct pp_power_state *state;
390
391 if (handle == NULL)
392 return -EINVAL;
393
394 hwmgr = ((struct pp_instance *)handle)->hwmgr;
395
396 if (hwmgr == NULL || hwmgr->current_ps == NULL)
397 return -EINVAL;
398
399 state = hwmgr->current_ps;
400
401 switch (state->classification.ui_label) {
402 case PP_StateUILabel_Battery:
403 return POWER_STATE_TYPE_BATTERY;
404 case PP_StateUILabel_Balanced:
405 return POWER_STATE_TYPE_BALANCED;
406 case PP_StateUILabel_Performance:
407 return POWER_STATE_TYPE_PERFORMANCE;
408 default:
409 return POWER_STATE_TYPE_DEFAULT;
410 }
Alex Deucher1f7371b2015-12-02 17:46:21 -0500411}
Rex Zhu577bbe02015-08-28 12:56:43 +0800412
Alex Deucher1f7371b2015-12-02 17:46:21 -0500413static void
414pp_debugfs_print_current_performance_level(void *handle,
415 struct seq_file *m)
416{
Rex Zhu577bbe02015-08-28 12:56:43 +0800417 struct pp_hwmgr *hwmgr;
418
419 if (handle == NULL)
420 return;
421
422 hwmgr = ((struct pp_instance *)handle)->hwmgr;
423
424 if (hwmgr == NULL || hwmgr->hwmgr_func == NULL ||
425 hwmgr->hwmgr_func->print_current_perforce_level == NULL)
426 return;
427
428 hwmgr->hwmgr_func->print_current_perforce_level(hwmgr, m);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500429}
Jammy Zhou3bace352015-07-21 21:18:15 +0800430
Rex Zhu577bbe02015-08-28 12:56:43 +0800431
Alex Deucher1f7371b2015-12-02 17:46:21 -0500432const struct amd_powerplay_funcs pp_dpm_funcs = {
433 .get_temperature = NULL,
434 .load_firmware = pp_dpm_load_fw,
435 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
436 .force_performance_level = pp_dpm_force_performance_level,
437 .get_performance_level = pp_dpm_get_performance_level,
438 .get_current_power_state = pp_dpm_get_current_power_state,
439 .get_sclk = pp_dpm_get_sclk,
440 .get_mclk = pp_dpm_get_mclk,
441 .powergate_vce = pp_dpm_powergate_vce,
442 .powergate_uvd = pp_dpm_powergate_uvd,
443 .dispatch_tasks = pp_dpm_dispatch_tasks,
444 .print_current_performance_level = pp_debugfs_print_current_performance_level,
445};
446
Jammy Zhouac885b32015-07-21 17:43:02 +0800447static int amd_pp_instance_init(struct amd_pp_init *pp_init,
448 struct amd_powerplay *amd_pp)
449{
450 int ret;
451 struct pp_instance *handle;
452
453 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
454 if (handle == NULL)
455 return -ENOMEM;
456
457 ret = smum_init(pp_init, handle);
458 if (ret)
Jammy Zhou3bace352015-07-21 21:18:15 +0800459 goto fail_smum;
460
461 ret = hwmgr_init(pp_init, handle);
462 if (ret)
463 goto fail_hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800464
Rex Zhue92a0372015-09-23 15:14:54 +0800465 ret = eventmgr_init(handle);
466 if (ret)
467 goto fail_eventmgr;
468
Jammy Zhouac885b32015-07-21 17:43:02 +0800469 amd_pp->pp_handle = handle;
470 return 0;
Jammy Zhou3bace352015-07-21 21:18:15 +0800471
Rex Zhue92a0372015-09-23 15:14:54 +0800472fail_eventmgr:
473 hwmgr_fini(handle->hwmgr);
Jammy Zhou3bace352015-07-21 21:18:15 +0800474fail_hwmgr:
475 smum_fini(handle->smu_mgr);
476fail_smum:
477 kfree(handle);
478 return ret;
Jammy Zhouac885b32015-07-21 17:43:02 +0800479}
480
481static int amd_pp_instance_fini(void *handle)
482{
483 struct pp_instance *instance = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800484
Jammy Zhouac885b32015-07-21 17:43:02 +0800485 if (instance == NULL)
486 return -EINVAL;
487
Rex Zhue92a0372015-09-23 15:14:54 +0800488 eventmgr_fini(instance->eventmgr);
489
Jammy Zhou3bace352015-07-21 21:18:15 +0800490 hwmgr_fini(instance->hwmgr);
491
Jammy Zhouac885b32015-07-21 17:43:02 +0800492 smum_fini(instance->smu_mgr);
493
494 kfree(handle);
495 return 0;
496}
497
Alex Deucher1f7371b2015-12-02 17:46:21 -0500498int amd_powerplay_init(struct amd_pp_init *pp_init,
499 struct amd_powerplay *amd_pp)
500{
Jammy Zhouac885b32015-07-21 17:43:02 +0800501 int ret;
502
Alex Deucher1f7371b2015-12-02 17:46:21 -0500503 if (pp_init == NULL || amd_pp == NULL)
504 return -EINVAL;
505
Jammy Zhouac885b32015-07-21 17:43:02 +0800506 ret = amd_pp_instance_init(pp_init, amd_pp);
507
508 if (ret)
509 return ret;
510
Alex Deucher1f7371b2015-12-02 17:46:21 -0500511 amd_pp->ip_funcs = &pp_ip_funcs;
512 amd_pp->pp_funcs = &pp_dpm_funcs;
513
514 return 0;
515}
516
517int amd_powerplay_fini(void *handle)
518{
Jammy Zhouac885b32015-07-21 17:43:02 +0800519 amd_pp_instance_fini(handle);
520
Alex Deucher1f7371b2015-12-02 17:46:21 -0500521 return 0;
522}