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