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