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