drm/bridge: Make (pre/post) enable/disable callbacks optional
[cascardo/linux.git] / drivers / gpu / drm / amd / powerplay / amd_powerplay.c
1 /*
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>
26 #include <linux/slab.h>
27 #include "amd_shared.h"
28 #include "amd_powerplay.h"
29 #include "pp_instance.h"
30 #include "power_state.h"
31 #include "eventmanager.h"
32
33 #define PP_CHECK(handle)                                                \
34         do {                                                            \
35                 if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
36                         return -EINVAL;                                 \
37         } while (0)
38
39 static int pp_early_init(void *handle)
40 {
41         return 0;
42 }
43
44 static int pp_sw_init(void *handle)
45 {
46         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);
63
64         if (ret == 0)
65                 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
66
67         return ret;
68 }
69
70 static int pp_sw_fini(void *handle)
71 {
72         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;
87 }
88
89 static int pp_hw_init(void *handle)
90 {
91         struct pp_instance *pp_handle;
92         struct pp_smumgr *smumgr;
93         struct pp_eventmgr *eventmgr;
94         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         }
119
120         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);
127         return 0;
128 }
129
130 static int pp_hw_fini(void *handle)
131 {
132         struct pp_instance *pp_handle;
133         struct pp_smumgr *smumgr;
134         struct pp_eventmgr *eventmgr;
135
136         if (handle == NULL)
137                 return -EINVAL;
138
139         pp_handle = (struct pp_instance *)handle;
140         eventmgr = pp_handle->eventmgr;
141
142         if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
143                 eventmgr->pp_eventmgr_fini(eventmgr);
144
145         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
151         return 0;
152 }
153
154 static bool pp_is_idle(void *handle)
155 {
156         return 0;
157 }
158
159 static int pp_wait_for_idle(void *handle)
160 {
161         return 0;
162 }
163
164 static int pp_sw_reset(void *handle)
165 {
166         return 0;
167 }
168
169 static void pp_print_status(void *handle)
170 {
171
172 }
173
174 static int pp_set_clockgating_state(void *handle,
175                                     enum amd_clockgating_state state)
176 {
177         return 0;
178 }
179
180 static int pp_set_powergating_state(void *handle,
181                                     enum amd_powergating_state state)
182 {
183         return 0;
184 }
185
186 static int pp_suspend(void *handle)
187 {
188         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);
198         return 0;
199 }
200
201 static int pp_resume(void *handle)
202 {
203         struct pp_instance *pp_handle;
204         struct pp_eventmgr *eventmgr;
205         struct pem_event_data event_data = { {0} };
206         struct pp_smumgr *smumgr;
207         int ret;
208
209         if (handle == NULL)
210                 return -EINVAL;
211
212         pp_handle = (struct pp_instance *)handle;
213         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
226         eventmgr = pp_handle->eventmgr;
227         pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
228
229         return 0;
230 }
231
232 const 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
249 static int pp_dpm_load_fw(void *handle)
250 {
251         return 0;
252 }
253
254 static int pp_dpm_fw_loading_complete(void *handle)
255 {
256         return 0;
257 }
258
259 static int pp_dpm_force_performance_level(void *handle,
260                                         enum amd_dpm_forced_level level)
261 {
262         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
278         return 0;
279 }
280
281 static enum amd_dpm_forced_level pp_dpm_get_performance_level(
282                                                                 void *handle)
283 {
284         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);
295 }
296
297 static int pp_dpm_get_sclk(void *handle, bool low)
298 {
299         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);
311 }
312
313 static int pp_dpm_get_mclk(void *handle, bool low)
314 {
315         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);
327 }
328
329 static int pp_dpm_powergate_vce(void *handle, bool gate)
330 {
331         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);
343 }
344
345 static int pp_dpm_powergate_uvd(void *handle, bool gate)
346 {
347         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
361 static 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         }
373 }
374
375 int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
376 {
377         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;
406 }
407
408 enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
409 {
410         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         }
433 }
434
435 static void
436 pp_debugfs_print_current_performance_level(void *handle,
437                                                struct seq_file *m)
438 {
439         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);
451 }
452
453 static 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
469 static 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
485 static 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
501 static 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
517 static 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 }
532
533 const struct amd_powerplay_funcs pp_dpm_funcs = {
534         .get_temperature = pp_dpm_get_temperature,
535         .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,
546         .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,
550 };
551
552 static 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
562         handle->pp_valid = PP_VALID;
563
564         ret = smum_init(pp_init, handle);
565         if (ret)
566                 goto fail_smum;
567
568         ret = hwmgr_init(pp_init, handle);
569         if (ret)
570                 goto fail_hwmgr;
571
572         ret = eventmgr_init(handle);
573         if (ret)
574                 goto fail_eventmgr;
575
576         amd_pp->pp_handle = handle;
577         return 0;
578
579 fail_eventmgr:
580         hwmgr_fini(handle->hwmgr);
581 fail_hwmgr:
582         smum_fini(handle->smu_mgr);
583 fail_smum:
584         kfree(handle);
585         return ret;
586 }
587
588 static int amd_pp_instance_fini(void *handle)
589 {
590         struct pp_instance *instance = (struct pp_instance *)handle;
591
592         if (instance == NULL)
593                 return -EINVAL;
594
595         eventmgr_fini(instance->eventmgr);
596
597         hwmgr_fini(instance->hwmgr);
598
599         smum_fini(instance->smu_mgr);
600
601         kfree(handle);
602         return 0;
603 }
604
605 int amd_powerplay_init(struct amd_pp_init *pp_init,
606                        struct amd_powerplay *amd_pp)
607 {
608         int ret;
609
610         if (pp_init == NULL || amd_pp == NULL)
611                 return -EINVAL;
612
613         ret = amd_pp_instance_init(pp_init, amd_pp);
614
615         if (ret)
616                 return ret;
617
618         amd_pp->ip_funcs = &pp_ip_funcs;
619         amd_pp->pp_funcs = &pp_dpm_funcs;
620
621         return 0;
622 }
623
624 int amd_powerplay_fini(void *handle)
625 {
626         amd_pp_instance_fini(handle);
627
628         return 0;
629 }
630
631 /* export this function to DAL */
632
633 int 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
638         PP_CHECK((struct pp_instance *)handle);
639
640         hwmgr = ((struct pp_instance *)handle)->hwmgr;
641
642         phm_store_dal_configuration_data(hwmgr, display_config);
643
644         return 0;
645 }
646
647 int amd_powerplay_get_display_power_level(void *handle,
648                 struct amd_pp_dal_clock_info *output)
649 {
650         struct pp_hwmgr  *hwmgr;
651
652         PP_CHECK((struct pp_instance *)handle);
653
654         if (output == NULL)
655                 return -EINVAL;
656
657         hwmgr = ((struct pp_instance *)handle)->hwmgr;
658
659         return phm_get_dal_power_level(hwmgr, output);
660 }