0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #include <linux/acpi.h>
0025 #include <linux/pci.h>
0026 #include <linux/pm_runtime.h>
0027 #include <linux/power_supply.h>
0028 #include <linux/slab.h>
0029
0030 #include <acpi/acpi_bus.h>
0031 #include <acpi/video.h>
0032
0033 #include <drm/drm_crtc_helper.h>
0034 #include <drm/drm_probe_helper.h>
0035
0036 #include "atom.h"
0037 #include "radeon.h"
0038 #include "radeon_acpi.h"
0039 #include "radeon_pm.h"
0040
0041 #if defined(CONFIG_VGA_SWITCHEROO)
0042 bool radeon_atpx_dgpu_req_power_for_displays(void);
0043 #else
0044 static inline bool radeon_atpx_dgpu_req_power_for_displays(void) { return false; }
0045 #endif
0046
0047 #define ACPI_AC_CLASS "ac_adapter"
0048
0049 struct atif_verify_interface {
0050 u16 size;
0051 u16 version;
0052 u32 notification_mask;
0053 u32 function_bits;
0054 } __packed;
0055
0056 struct atif_system_params {
0057 u16 size;
0058 u32 valid_mask;
0059 u32 flags;
0060 u8 command_code;
0061 } __packed;
0062
0063 struct atif_sbios_requests {
0064 u16 size;
0065 u32 pending;
0066 u8 panel_exp_mode;
0067 u8 thermal_gfx;
0068 u8 thermal_state;
0069 u8 forced_power_gfx;
0070 u8 forced_power_state;
0071 u8 system_power_src;
0072 u8 backlight_level;
0073 } __packed;
0074
0075 #define ATIF_NOTIFY_MASK 0x3
0076 #define ATIF_NOTIFY_NONE 0
0077 #define ATIF_NOTIFY_81 1
0078 #define ATIF_NOTIFY_N 2
0079
0080 struct atcs_verify_interface {
0081 u16 size;
0082 u16 version;
0083 u32 function_bits;
0084 } __packed;
0085
0086 #define ATCS_VALID_FLAGS_MASK 0x3
0087
0088 struct atcs_pref_req_input {
0089 u16 size;
0090 u16 client_id;
0091 u16 valid_flags_mask;
0092 u16 flags;
0093 u8 req_type;
0094 u8 perf_req;
0095 } __packed;
0096
0097 struct atcs_pref_req_output {
0098 u16 size;
0099 u8 ret_val;
0100 } __packed;
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
0115 struct acpi_buffer *params)
0116 {
0117 acpi_status status;
0118 union acpi_object atif_arg_elements[2];
0119 struct acpi_object_list atif_arg;
0120 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0121
0122 atif_arg.count = 2;
0123 atif_arg.pointer = &atif_arg_elements[0];
0124
0125 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
0126 atif_arg_elements[0].integer.value = function;
0127
0128 if (params) {
0129 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
0130 atif_arg_elements[1].buffer.length = params->length;
0131 atif_arg_elements[1].buffer.pointer = params->pointer;
0132 } else {
0133
0134 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
0135 atif_arg_elements[1].integer.value = 0;
0136 }
0137
0138 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
0139
0140
0141 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
0142 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
0143 acpi_format_exception(status));
0144 kfree(buffer.pointer);
0145 return NULL;
0146 }
0147
0148 return buffer.pointer;
0149 }
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
0162 {
0163 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
0164 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
0165 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
0166 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
0167 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
0168 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
0169 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
0170 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
0171 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
0172 }
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
0185 {
0186 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
0187 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
0188 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
0189 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
0190 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
0191 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
0192 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
0193 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
0194 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
0195 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
0196 }
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209 static int radeon_atif_verify_interface(acpi_handle handle,
0210 struct radeon_atif *atif)
0211 {
0212 union acpi_object *info;
0213 struct atif_verify_interface output;
0214 size_t size;
0215 int err = 0;
0216
0217 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
0218 if (!info)
0219 return -EIO;
0220
0221 memset(&output, 0, sizeof(output));
0222
0223 size = *(u16 *) info->buffer.pointer;
0224 if (size < 12) {
0225 DRM_INFO("ATIF buffer is too small: %zu\n", size);
0226 err = -EINVAL;
0227 goto out;
0228 }
0229 size = min(sizeof(output), size);
0230
0231 memcpy(&output, info->buffer.pointer, size);
0232
0233
0234 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
0235
0236 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
0237 radeon_atif_parse_functions(&atif->functions, output.function_bits);
0238
0239 out:
0240 kfree(info);
0241 return err;
0242 }
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256 static int radeon_atif_get_notification_params(acpi_handle handle,
0257 struct radeon_atif_notification_cfg *n)
0258 {
0259 union acpi_object *info;
0260 struct atif_system_params params;
0261 size_t size;
0262 int err = 0;
0263
0264 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
0265 if (!info) {
0266 err = -EIO;
0267 goto out;
0268 }
0269
0270 size = *(u16 *) info->buffer.pointer;
0271 if (size < 10) {
0272 err = -EINVAL;
0273 goto out;
0274 }
0275
0276 memset(¶ms, 0, sizeof(params));
0277 size = min(sizeof(params), size);
0278 memcpy(¶ms, info->buffer.pointer, size);
0279
0280 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
0281 params.flags, params.valid_mask);
0282 params.flags = params.flags & params.valid_mask;
0283
0284 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
0285 n->enabled = false;
0286 n->command_code = 0;
0287 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
0288 n->enabled = true;
0289 n->command_code = 0x81;
0290 } else {
0291 if (size < 11) {
0292 err = -EINVAL;
0293 goto out;
0294 }
0295 n->enabled = true;
0296 n->command_code = params.command_code;
0297 }
0298
0299 out:
0300 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
0301 (n->enabled ? "enabled" : "disabled"),
0302 n->command_code);
0303 kfree(info);
0304 return err;
0305 }
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318 static int radeon_atif_get_sbios_requests(acpi_handle handle,
0319 struct atif_sbios_requests *req)
0320 {
0321 union acpi_object *info;
0322 size_t size;
0323 int count = 0;
0324
0325 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
0326 if (!info)
0327 return -EIO;
0328
0329 size = *(u16 *)info->buffer.pointer;
0330 if (size < 0xd) {
0331 count = -EINVAL;
0332 goto out;
0333 }
0334 memset(req, 0, sizeof(*req));
0335
0336 size = min(sizeof(*req), size);
0337 memcpy(req, info->buffer.pointer, size);
0338 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
0339
0340 count = hweight32(req->pending);
0341
0342 out:
0343 kfree(info);
0344 return count;
0345 }
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357 static int radeon_atif_handler(struct radeon_device *rdev,
0358 struct acpi_bus_event *event)
0359 {
0360 struct radeon_atif *atif = &rdev->atif;
0361 struct atif_sbios_requests req;
0362 acpi_handle handle;
0363 int count;
0364
0365 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
0366 event->device_class, event->type);
0367
0368 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
0369 return NOTIFY_DONE;
0370
0371 if (!atif->notification_cfg.enabled ||
0372 event->type != atif->notification_cfg.command_code)
0373
0374 return NOTIFY_DONE;
0375
0376
0377 handle = ACPI_HANDLE(&rdev->pdev->dev);
0378 count = radeon_atif_get_sbios_requests(handle, &req);
0379
0380 if (count <= 0)
0381 return NOTIFY_DONE;
0382
0383 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
0384
0385 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
0386 struct radeon_encoder *enc = atif->encoder_for_bl;
0387
0388 if (enc) {
0389 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
0390 req.backlight_level);
0391
0392 radeon_set_backlight_level(rdev, enc, req.backlight_level);
0393
0394 if (rdev->is_atom_bios) {
0395 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
0396 backlight_force_update(dig->bl_dev,
0397 BACKLIGHT_UPDATE_HOTKEY);
0398 } else {
0399 struct radeon_encoder_lvds *dig = enc->enc_priv;
0400 backlight_force_update(dig->bl_dev,
0401 BACKLIGHT_UPDATE_HOTKEY);
0402 }
0403 }
0404 }
0405 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
0406 if ((rdev->flags & RADEON_IS_PX) &&
0407 radeon_atpx_dgpu_req_power_for_displays()) {
0408 pm_runtime_get_sync(rdev->ddev->dev);
0409
0410 drm_helper_hpd_irq_event(rdev->ddev);
0411 pm_runtime_mark_last_busy(rdev->ddev->dev);
0412 pm_runtime_put_autosuspend(rdev->ddev->dev);
0413 }
0414 }
0415
0416
0417
0418
0419
0420
0421
0422 return NOTIFY_BAD;
0423 }
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437 static union acpi_object *radeon_atcs_call(acpi_handle handle, int function,
0438 struct acpi_buffer *params)
0439 {
0440 acpi_status status;
0441 union acpi_object atcs_arg_elements[2];
0442 struct acpi_object_list atcs_arg;
0443 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0444
0445 atcs_arg.count = 2;
0446 atcs_arg.pointer = &atcs_arg_elements[0];
0447
0448 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
0449 atcs_arg_elements[0].integer.value = function;
0450
0451 if (params) {
0452 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
0453 atcs_arg_elements[1].buffer.length = params->length;
0454 atcs_arg_elements[1].buffer.pointer = params->pointer;
0455 } else {
0456
0457 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
0458 atcs_arg_elements[1].integer.value = 0;
0459 }
0460
0461 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
0462
0463
0464 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
0465 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
0466 acpi_format_exception(status));
0467 kfree(buffer.pointer);
0468 return NULL;
0469 }
0470
0471 return buffer.pointer;
0472 }
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
0485 {
0486 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
0487 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
0488 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
0489 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
0490 }
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 static int radeon_atcs_verify_interface(acpi_handle handle,
0504 struct radeon_atcs *atcs)
0505 {
0506 union acpi_object *info;
0507 struct atcs_verify_interface output;
0508 size_t size;
0509 int err = 0;
0510
0511 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
0512 if (!info)
0513 return -EIO;
0514
0515 memset(&output, 0, sizeof(output));
0516
0517 size = *(u16 *) info->buffer.pointer;
0518 if (size < 8) {
0519 DRM_INFO("ATCS buffer is too small: %zu\n", size);
0520 err = -EINVAL;
0521 goto out;
0522 }
0523 size = min(sizeof(output), size);
0524
0525 memcpy(&output, info->buffer.pointer, size);
0526
0527
0528 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
0529
0530 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
0531
0532 out:
0533 kfree(info);
0534 return err;
0535 }
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546 bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
0547 {
0548 struct radeon_atcs *atcs = &rdev->atcs;
0549
0550 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
0551 return true;
0552
0553 return false;
0554 }
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565 int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
0566 {
0567 acpi_handle handle;
0568 union acpi_object *info;
0569 struct radeon_atcs *atcs = &rdev->atcs;
0570
0571
0572 handle = ACPI_HANDLE(&rdev->pdev->dev);
0573 if (!handle)
0574 return -EINVAL;
0575
0576 if (!atcs->functions.pcie_dev_rdy)
0577 return -EINVAL;
0578
0579 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
0580 if (!info)
0581 return -EIO;
0582
0583 kfree(info);
0584
0585 return 0;
0586 }
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599 int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
0600 u8 perf_req, bool advertise)
0601 {
0602 acpi_handle handle;
0603 union acpi_object *info;
0604 struct radeon_atcs *atcs = &rdev->atcs;
0605 struct atcs_pref_req_input atcs_input;
0606 struct atcs_pref_req_output atcs_output;
0607 struct acpi_buffer params;
0608 size_t size;
0609 u32 retry = 3;
0610
0611
0612 handle = ACPI_HANDLE(&rdev->pdev->dev);
0613 if (!handle)
0614 return -EINVAL;
0615
0616 if (!atcs->functions.pcie_perf_req)
0617 return -EINVAL;
0618
0619 atcs_input.size = sizeof(struct atcs_pref_req_input);
0620
0621 atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
0622 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
0623 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
0624 if (advertise)
0625 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
0626 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
0627 atcs_input.perf_req = perf_req;
0628
0629 params.length = sizeof(struct atcs_pref_req_input);
0630 params.pointer = &atcs_input;
0631
0632 while (retry--) {
0633 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
0634 if (!info)
0635 return -EIO;
0636
0637 memset(&atcs_output, 0, sizeof(atcs_output));
0638
0639 size = *(u16 *) info->buffer.pointer;
0640 if (size < 3) {
0641 DRM_INFO("ATCS buffer is too small: %zu\n", size);
0642 kfree(info);
0643 return -EINVAL;
0644 }
0645 size = min(sizeof(atcs_output), size);
0646
0647 memcpy(&atcs_output, info->buffer.pointer, size);
0648
0649 kfree(info);
0650
0651 switch (atcs_output.ret_val) {
0652 case ATCS_REQUEST_REFUSED:
0653 default:
0654 return -EINVAL;
0655 case ATCS_REQUEST_COMPLETE:
0656 return 0;
0657 case ATCS_REQUEST_IN_PROGRESS:
0658 udelay(10);
0659 break;
0660 }
0661 }
0662
0663 return 0;
0664 }
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677 static int radeon_acpi_event(struct notifier_block *nb,
0678 unsigned long val,
0679 void *data)
0680 {
0681 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
0682 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
0683
0684 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
0685 if (power_supply_is_system_supplied() > 0)
0686 DRM_DEBUG_DRIVER("pm: AC\n");
0687 else
0688 DRM_DEBUG_DRIVER("pm: DC\n");
0689
0690 radeon_pm_acpi_event_handler(rdev);
0691 }
0692
0693
0694 return radeon_atif_handler(rdev, entry);
0695 }
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707 int radeon_acpi_init(struct radeon_device *rdev)
0708 {
0709 acpi_handle handle;
0710 struct radeon_atif *atif = &rdev->atif;
0711 struct radeon_atcs *atcs = &rdev->atcs;
0712 int ret;
0713
0714
0715 handle = ACPI_HANDLE(&rdev->pdev->dev);
0716
0717
0718 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
0719 return 0;
0720
0721
0722 ret = radeon_atcs_verify_interface(handle, atcs);
0723 if (ret) {
0724 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
0725 }
0726
0727
0728 ret = radeon_atif_verify_interface(handle, atif);
0729 if (ret) {
0730 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
0731 goto out;
0732 }
0733
0734 if (atif->notifications.brightness_change) {
0735 struct drm_encoder *tmp;
0736 struct radeon_encoder *target = NULL;
0737
0738
0739 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
0740 head) {
0741 struct radeon_encoder *enc = to_radeon_encoder(tmp);
0742
0743 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
0744 enc->enc_priv) {
0745 if (rdev->is_atom_bios) {
0746 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
0747 if (dig->bl_dev) {
0748 target = enc;
0749 break;
0750 }
0751 } else {
0752 struct radeon_encoder_lvds *dig = enc->enc_priv;
0753 if (dig->bl_dev) {
0754 target = enc;
0755 break;
0756 }
0757 }
0758 }
0759 }
0760
0761 atif->encoder_for_bl = target;
0762 }
0763
0764 if (atif->functions.sbios_requests && !atif->functions.system_params) {
0765
0766
0767
0768
0769 atif->functions.system_params = true;
0770 }
0771
0772 if (atif->functions.system_params) {
0773 ret = radeon_atif_get_notification_params(handle,
0774 &atif->notification_cfg);
0775 if (ret) {
0776 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
0777 ret);
0778
0779 atif->notification_cfg.enabled = false;
0780 }
0781 }
0782
0783 out:
0784 rdev->acpi_nb.notifier_call = radeon_acpi_event;
0785 register_acpi_notifier(&rdev->acpi_nb);
0786
0787 return ret;
0788 }
0789
0790
0791
0792
0793
0794
0795
0796
0797 void radeon_acpi_fini(struct radeon_device *rdev)
0798 {
0799 unregister_acpi_notifier(&rdev->acpi_nb);
0800 }