1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/pci.h>
25#include <linux/acpi.h>
26#include <linux/slab.h>
27#include <linux/power_supply.h>
28#include <linux/pm_runtime.h>
29#include <acpi/video.h>
30#include <drm/drmP.h>
31#include <drm/drm_crtc_helper.h>
32#include <drm/drm_probe_helper.h>
33#include "radeon.h"
34#include "radeon_acpi.h"
35#include "atom.h"
36
37#if defined(CONFIG_VGA_SWITCHEROO)
38bool radeon_atpx_dgpu_req_power_for_displays(void);
39#else
40static inline bool radeon_atpx_dgpu_req_power_for_displays(void) { return false; }
41#endif
42
43#define ACPI_AC_CLASS "ac_adapter"
44
45extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
46
47struct atif_verify_interface {
48 u16 size;
49 u16 version;
50 u32 notification_mask;
51 u32 function_bits;
52} __packed;
53
54struct atif_system_params {
55 u16 size;
56 u32 valid_mask;
57 u32 flags;
58 u8 command_code;
59} __packed;
60
61struct atif_sbios_requests {
62 u16 size;
63 u32 pending;
64 u8 panel_exp_mode;
65 u8 thermal_gfx;
66 u8 thermal_state;
67 u8 forced_power_gfx;
68 u8 forced_power_state;
69 u8 system_power_src;
70 u8 backlight_level;
71} __packed;
72
73#define ATIF_NOTIFY_MASK 0x3
74#define ATIF_NOTIFY_NONE 0
75#define ATIF_NOTIFY_81 1
76#define ATIF_NOTIFY_N 2
77
78struct atcs_verify_interface {
79 u16 size;
80 u16 version;
81 u32 function_bits;
82} __packed;
83
84#define ATCS_VALID_FLAGS_MASK 0x3
85
86struct atcs_pref_req_input {
87 u16 size;
88 u16 client_id;
89 u16 valid_flags_mask;
90 u16 flags;
91 u8 req_type;
92 u8 perf_req;
93} __packed;
94
95struct atcs_pref_req_output {
96 u16 size;
97 u8 ret_val;
98} __packed;
99
100
101
102
103
104
105
106
107
108
109
110
111
112static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
113 struct acpi_buffer *params)
114{
115 acpi_status status;
116 union acpi_object atif_arg_elements[2];
117 struct acpi_object_list atif_arg;
118 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
119
120 atif_arg.count = 2;
121 atif_arg.pointer = &atif_arg_elements[0];
122
123 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
124 atif_arg_elements[0].integer.value = function;
125
126 if (params) {
127 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
128 atif_arg_elements[1].buffer.length = params->length;
129 atif_arg_elements[1].buffer.pointer = params->pointer;
130 } else {
131
132 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
133 atif_arg_elements[1].integer.value = 0;
134 }
135
136 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
137
138
139 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
140 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
141 acpi_format_exception(status));
142 kfree(buffer.pointer);
143 return NULL;
144 }
145
146 return buffer.pointer;
147}
148
149
150
151
152
153
154
155
156
157
158
159static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
160{
161 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
162 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
163 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
164 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
165 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
166 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
167 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
168 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
169 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
170}
171
172
173
174
175
176
177
178
179
180
181
182static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
183{
184 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
185 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
186 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
187 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
188 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
189 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
190 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
191 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
192 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
193 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
194}
195
196
197
198
199
200
201
202
203
204
205
206
207static int radeon_atif_verify_interface(acpi_handle handle,
208 struct radeon_atif *atif)
209{
210 union acpi_object *info;
211 struct atif_verify_interface output;
212 size_t size;
213 int err = 0;
214
215 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
216 if (!info)
217 return -EIO;
218
219 memset(&output, 0, sizeof(output));
220
221 size = *(u16 *) info->buffer.pointer;
222 if (size < 12) {
223 DRM_INFO("ATIF buffer is too small: %zu\n", size);
224 err = -EINVAL;
225 goto out;
226 }
227 size = min(sizeof(output), size);
228
229 memcpy(&output, info->buffer.pointer, size);
230
231
232 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
233
234 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
235 radeon_atif_parse_functions(&atif->functions, output.function_bits);
236
237out:
238 kfree(info);
239 return err;
240}
241
242
243
244
245
246
247
248
249
250
251
252
253
254static int radeon_atif_get_notification_params(acpi_handle handle,
255 struct radeon_atif_notification_cfg *n)
256{
257 union acpi_object *info;
258 struct atif_system_params params;
259 size_t size;
260 int err = 0;
261
262 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
263 if (!info) {
264 err = -EIO;
265 goto out;
266 }
267
268 size = *(u16 *) info->buffer.pointer;
269 if (size < 10) {
270 err = -EINVAL;
271 goto out;
272 }
273
274 memset(¶ms, 0, sizeof(params));
275 size = min(sizeof(params), size);
276 memcpy(¶ms, info->buffer.pointer, size);
277
278 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
279 params.flags, params.valid_mask);
280 params.flags = params.flags & params.valid_mask;
281
282 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
283 n->enabled = false;
284 n->command_code = 0;
285 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
286 n->enabled = true;
287 n->command_code = 0x81;
288 } else {
289 if (size < 11) {
290 err = -EINVAL;
291 goto out;
292 }
293 n->enabled = true;
294 n->command_code = params.command_code;
295 }
296
297out:
298 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
299 (n->enabled ? "enabled" : "disabled"),
300 n->command_code);
301 kfree(info);
302 return err;
303}
304
305
306
307
308
309
310
311
312
313
314
315
316static int radeon_atif_get_sbios_requests(acpi_handle handle,
317 struct atif_sbios_requests *req)
318{
319 union acpi_object *info;
320 size_t size;
321 int count = 0;
322
323 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
324 if (!info)
325 return -EIO;
326
327 size = *(u16 *)info->buffer.pointer;
328 if (size < 0xd) {
329 count = -EINVAL;
330 goto out;
331 }
332 memset(req, 0, sizeof(*req));
333
334 size = min(sizeof(*req), size);
335 memcpy(req, info->buffer.pointer, size);
336 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
337
338 count = hweight32(req->pending);
339
340out:
341 kfree(info);
342 return count;
343}
344
345
346
347
348
349
350
351
352
353
354
355static int radeon_atif_handler(struct radeon_device *rdev,
356 struct acpi_bus_event *event)
357{
358 struct radeon_atif *atif = &rdev->atif;
359 struct atif_sbios_requests req;
360 acpi_handle handle;
361 int count;
362
363 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
364 event->device_class, event->type);
365
366 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
367 return NOTIFY_DONE;
368
369 if (!atif->notification_cfg.enabled ||
370 event->type != atif->notification_cfg.command_code)
371
372 return NOTIFY_DONE;
373
374
375 handle = ACPI_HANDLE(&rdev->pdev->dev);
376 count = radeon_atif_get_sbios_requests(handle, &req);
377
378 if (count <= 0)
379 return NOTIFY_DONE;
380
381 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
382
383 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
384 struct radeon_encoder *enc = atif->encoder_for_bl;
385
386 if (enc) {
387 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
388 req.backlight_level);
389
390 radeon_set_backlight_level(rdev, enc, req.backlight_level);
391
392#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
393 if (rdev->is_atom_bios) {
394 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
395 backlight_force_update(dig->bl_dev,
396 BACKLIGHT_UPDATE_HOTKEY);
397 } else {
398 struct radeon_encoder_lvds *dig = enc->enc_priv;
399 backlight_force_update(dig->bl_dev,
400 BACKLIGHT_UPDATE_HOTKEY);
401 }
402#endif
403 }
404 }
405 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
406 if ((rdev->flags & RADEON_IS_PX) &&
407 radeon_atpx_dgpu_req_power_for_displays()) {
408 pm_runtime_get_sync(rdev->ddev->dev);
409
410 drm_helper_hpd_irq_event(rdev->ddev);
411 pm_runtime_mark_last_busy(rdev->ddev->dev);
412 pm_runtime_put_autosuspend(rdev->ddev->dev);
413 }
414 }
415
416
417
418
419
420
421
422 return NOTIFY_BAD;
423}
424
425
426
427
428
429
430
431
432
433
434
435
436
437static union acpi_object *radeon_atcs_call(acpi_handle handle, int function,
438 struct acpi_buffer *params)
439{
440 acpi_status status;
441 union acpi_object atcs_arg_elements[2];
442 struct acpi_object_list atcs_arg;
443 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
444
445 atcs_arg.count = 2;
446 atcs_arg.pointer = &atcs_arg_elements[0];
447
448 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
449 atcs_arg_elements[0].integer.value = function;
450
451 if (params) {
452 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
453 atcs_arg_elements[1].buffer.length = params->length;
454 atcs_arg_elements[1].buffer.pointer = params->pointer;
455 } else {
456
457 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
458 atcs_arg_elements[1].integer.value = 0;
459 }
460
461 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
462
463
464 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
465 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
466 acpi_format_exception(status));
467 kfree(buffer.pointer);
468 return NULL;
469 }
470
471 return buffer.pointer;
472}
473
474
475
476
477
478
479
480
481
482
483
484static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
485{
486 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
487 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
488 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
489 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
490}
491
492
493
494
495
496
497
498
499
500
501
502
503static int radeon_atcs_verify_interface(acpi_handle handle,
504 struct radeon_atcs *atcs)
505{
506 union acpi_object *info;
507 struct atcs_verify_interface output;
508 size_t size;
509 int err = 0;
510
511 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
512 if (!info)
513 return -EIO;
514
515 memset(&output, 0, sizeof(output));
516
517 size = *(u16 *) info->buffer.pointer;
518 if (size < 8) {
519 DRM_INFO("ATCS buffer is too small: %zu\n", size);
520 err = -EINVAL;
521 goto out;
522 }
523 size = min(sizeof(output), size);
524
525 memcpy(&output, info->buffer.pointer, size);
526
527
528 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
529
530 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
531
532out:
533 kfree(info);
534 return err;
535}
536
537
538
539
540
541
542
543
544
545
546bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
547{
548 struct radeon_atcs *atcs = &rdev->atcs;
549
550 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
551 return true;
552
553 return false;
554}
555
556
557
558
559
560
561
562
563
564
565int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
566{
567 acpi_handle handle;
568 union acpi_object *info;
569 struct radeon_atcs *atcs = &rdev->atcs;
570
571
572 handle = ACPI_HANDLE(&rdev->pdev->dev);
573 if (!handle)
574 return -EINVAL;
575
576 if (!atcs->functions.pcie_dev_rdy)
577 return -EINVAL;
578
579 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
580 if (!info)
581 return -EIO;
582
583 kfree(info);
584
585 return 0;
586}
587
588
589
590
591
592
593
594
595
596
597
598
599int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
600 u8 perf_req, bool advertise)
601{
602 acpi_handle handle;
603 union acpi_object *info;
604 struct radeon_atcs *atcs = &rdev->atcs;
605 struct atcs_pref_req_input atcs_input;
606 struct atcs_pref_req_output atcs_output;
607 struct acpi_buffer params;
608 size_t size;
609 u32 retry = 3;
610
611
612 handle = ACPI_HANDLE(&rdev->pdev->dev);
613 if (!handle)
614 return -EINVAL;
615
616 if (!atcs->functions.pcie_perf_req)
617 return -EINVAL;
618
619 atcs_input.size = sizeof(struct atcs_pref_req_input);
620
621 atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
622 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
623 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
624 if (advertise)
625 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
626 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
627 atcs_input.perf_req = perf_req;
628
629 params.length = sizeof(struct atcs_pref_req_input);
630 params.pointer = &atcs_input;
631
632 while (retry--) {
633 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
634 if (!info)
635 return -EIO;
636
637 memset(&atcs_output, 0, sizeof(atcs_output));
638
639 size = *(u16 *) info->buffer.pointer;
640 if (size < 3) {
641 DRM_INFO("ATCS buffer is too small: %zu\n", size);
642 kfree(info);
643 return -EINVAL;
644 }
645 size = min(sizeof(atcs_output), size);
646
647 memcpy(&atcs_output, info->buffer.pointer, size);
648
649 kfree(info);
650
651 switch (atcs_output.ret_val) {
652 case ATCS_REQUEST_REFUSED:
653 default:
654 return -EINVAL;
655 case ATCS_REQUEST_COMPLETE:
656 return 0;
657 case ATCS_REQUEST_IN_PROGRESS:
658 udelay(10);
659 break;
660 }
661 }
662
663 return 0;
664}
665
666
667
668
669
670
671
672
673
674
675
676
677static int radeon_acpi_event(struct notifier_block *nb,
678 unsigned long val,
679 void *data)
680{
681 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
682 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
683
684 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
685 if (power_supply_is_system_supplied() > 0)
686 DRM_DEBUG_DRIVER("pm: AC\n");
687 else
688 DRM_DEBUG_DRIVER("pm: DC\n");
689
690 radeon_pm_acpi_event_handler(rdev);
691 }
692
693
694 return radeon_atif_handler(rdev, entry);
695}
696
697
698
699
700
701
702
703
704
705
706
707int radeon_acpi_init(struct radeon_device *rdev)
708{
709 acpi_handle handle;
710 struct radeon_atif *atif = &rdev->atif;
711 struct radeon_atcs *atcs = &rdev->atcs;
712 int ret;
713
714
715 handle = ACPI_HANDLE(&rdev->pdev->dev);
716
717
718 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
719 return 0;
720
721
722 ret = radeon_atcs_verify_interface(handle, atcs);
723 if (ret) {
724 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
725 }
726
727
728 ret = radeon_atif_verify_interface(handle, atif);
729 if (ret) {
730 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
731 goto out;
732 }
733
734 if (atif->notifications.brightness_change) {
735 struct drm_encoder *tmp;
736 struct radeon_encoder *target = NULL;
737
738
739 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
740 head) {
741 struct radeon_encoder *enc = to_radeon_encoder(tmp);
742
743 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
744 enc->enc_priv) {
745 if (rdev->is_atom_bios) {
746 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
747 if (dig->bl_dev) {
748 target = enc;
749 break;
750 }
751 } else {
752 struct radeon_encoder_lvds *dig = enc->enc_priv;
753 if (dig->bl_dev) {
754 target = enc;
755 break;
756 }
757 }
758 }
759 }
760
761 atif->encoder_for_bl = target;
762 }
763
764 if (atif->functions.sbios_requests && !atif->functions.system_params) {
765
766
767
768
769 atif->functions.system_params = true;
770 }
771
772 if (atif->functions.system_params) {
773 ret = radeon_atif_get_notification_params(handle,
774 &atif->notification_cfg);
775 if (ret) {
776 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
777 ret);
778
779 atif->notification_cfg.enabled = false;
780 }
781 }
782
783out:
784 rdev->acpi_nb.notifier_call = radeon_acpi_event;
785 register_acpi_notifier(&rdev->acpi_nb);
786
787 return ret;
788}
789
790
791
792
793
794
795
796
797void radeon_acpi_fini(struct radeon_device *rdev)
798{
799 unregister_acpi_notifier(&rdev->acpi_nb);
800}
801