1 /* 2 * Copyright 2012 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 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 <acpi/actbl.h> 31 32 #include <drm/drm_crtc_helper.h> 33 #include "amdgpu.h" 34 #include "amdgpu_pm.h" 35 #include "amdgpu_display.h" 36 #include "amd_acpi.h" 37 #include "atom.h" 38 39 struct amdgpu_atif_notification_cfg { 40 bool enabled; 41 int command_code; 42 }; 43 44 struct amdgpu_atif_notifications { 45 bool thermal_state; 46 bool forced_power_state; 47 bool system_power_state; 48 bool brightness_change; 49 bool dgpu_display_event; 50 bool gpu_package_power_limit; 51 }; 52 53 struct amdgpu_atif_functions { 54 bool system_params; 55 bool sbios_requests; 56 bool temperature_change; 57 bool query_backlight_transfer_characteristics; 58 bool ready_to_undock; 59 bool external_gpu_information; 60 }; 61 62 struct amdgpu_atif { 63 acpi_handle handle; 64 65 struct amdgpu_atif_notifications notifications; 66 struct amdgpu_atif_functions functions; 67 struct amdgpu_atif_notification_cfg notification_cfg; 68 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 69 struct backlight_device *bd; 70 #endif 71 struct amdgpu_dm_backlight_caps backlight_caps; 72 }; 73 74 /* Call the ATIF method 75 */ 76 /** 77 * amdgpu_atif_call - call an ATIF method 78 * 79 * @atif: acpi handle 80 * @function: the ATIF function to execute 81 * @params: ATIF function params 82 * 83 * Executes the requested ATIF function (all asics). 84 * Returns a pointer to the acpi output buffer. 85 */ 86 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif, 87 int function, 88 struct acpi_buffer *params) 89 { 90 acpi_status status; 91 union acpi_object atif_arg_elements[2]; 92 struct acpi_object_list atif_arg; 93 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 94 95 atif_arg.count = 2; 96 atif_arg.pointer = &atif_arg_elements[0]; 97 98 atif_arg_elements[0].type = ACPI_TYPE_INTEGER; 99 atif_arg_elements[0].integer.value = function; 100 101 if (params) { 102 atif_arg_elements[1].type = ACPI_TYPE_BUFFER; 103 atif_arg_elements[1].buffer.length = params->length; 104 atif_arg_elements[1].buffer.pointer = params->pointer; 105 } else { 106 /* We need a second fake parameter */ 107 atif_arg_elements[1].type = ACPI_TYPE_INTEGER; 108 atif_arg_elements[1].integer.value = 0; 109 } 110 111 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg, 112 &buffer); 113 114 /* Fail only if calling the method fails and ATIF is supported */ 115 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 116 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n", 117 acpi_format_exception(status)); 118 kfree(buffer.pointer); 119 return NULL; 120 } 121 122 return buffer.pointer; 123 } 124 125 /** 126 * amdgpu_atif_parse_notification - parse supported notifications 127 * 128 * @n: supported notifications struct 129 * @mask: supported notifications mask from ATIF 130 * 131 * Use the supported notifications mask from ATIF function 132 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications 133 * are supported (all asics). 134 */ 135 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask) 136 { 137 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED; 138 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED; 139 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED; 140 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED; 141 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED; 142 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED; 143 } 144 145 /** 146 * amdgpu_atif_parse_functions - parse supported functions 147 * 148 * @f: supported functions struct 149 * @mask: supported functions mask from ATIF 150 * 151 * Use the supported functions mask from ATIF function 152 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions 153 * are supported (all asics). 154 */ 155 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask) 156 { 157 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED; 158 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED; 159 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED; 160 f->query_backlight_transfer_characteristics = 161 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED; 162 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED; 163 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED; 164 } 165 166 /** 167 * amdgpu_atif_verify_interface - verify ATIF 168 * 169 * @atif: amdgpu atif struct 170 * 171 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function 172 * to initialize ATIF and determine what features are supported 173 * (all asics). 174 * returns 0 on success, error on failure. 175 */ 176 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif) 177 { 178 union acpi_object *info; 179 struct atif_verify_interface output; 180 size_t size; 181 int err = 0; 182 183 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL); 184 if (!info) 185 return -EIO; 186 187 memset(&output, 0, sizeof(output)); 188 189 size = *(u16 *) info->buffer.pointer; 190 if (size < 12) { 191 DRM_INFO("ATIF buffer is too small: %zu\n", size); 192 err = -EINVAL; 193 goto out; 194 } 195 size = min(sizeof(output), size); 196 197 memcpy(&output, info->buffer.pointer, size); 198 199 /* TODO: check version? */ 200 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version); 201 202 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask); 203 amdgpu_atif_parse_functions(&atif->functions, output.function_bits); 204 205 out: 206 kfree(info); 207 return err; 208 } 209 210 static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle) 211 { 212 acpi_handle handle = NULL; 213 char acpi_method_name[255] = { 0 }; 214 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name }; 215 acpi_status status; 216 217 /* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only 218 * systems, ATIF is in the dGPU's namespace. 219 */ 220 status = acpi_get_handle(dhandle, "ATIF", &handle); 221 if (ACPI_SUCCESS(status)) 222 goto out; 223 224 if (amdgpu_has_atpx()) { 225 status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF", 226 &handle); 227 if (ACPI_SUCCESS(status)) 228 goto out; 229 } 230 231 DRM_DEBUG_DRIVER("No ATIF handle found\n"); 232 return NULL; 233 out: 234 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 235 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name); 236 return handle; 237 } 238 239 /** 240 * amdgpu_atif_get_notification_params - determine notify configuration 241 * 242 * @atif: acpi handle 243 * 244 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function 245 * to determine if a notifier is used and if so which one 246 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n) 247 * where n is specified in the result if a notifier is used. 248 * Returns 0 on success, error on failure. 249 */ 250 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif) 251 { 252 union acpi_object *info; 253 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg; 254 struct atif_system_params params; 255 size_t size; 256 int err = 0; 257 258 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, 259 NULL); 260 if (!info) { 261 err = -EIO; 262 goto out; 263 } 264 265 size = *(u16 *) info->buffer.pointer; 266 if (size < 10) { 267 err = -EINVAL; 268 goto out; 269 } 270 271 memset(¶ms, 0, sizeof(params)); 272 size = min(sizeof(params), size); 273 memcpy(¶ms, info->buffer.pointer, size); 274 275 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n", 276 params.flags, params.valid_mask); 277 params.flags = params.flags & params.valid_mask; 278 279 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) { 280 n->enabled = false; 281 n->command_code = 0; 282 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) { 283 n->enabled = true; 284 n->command_code = 0x81; 285 } else { 286 if (size < 11) { 287 err = -EINVAL; 288 goto out; 289 } 290 n->enabled = true; 291 n->command_code = params.command_code; 292 } 293 294 out: 295 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n", 296 (n->enabled ? "enabled" : "disabled"), 297 n->command_code); 298 kfree(info); 299 return err; 300 } 301 302 /** 303 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal 304 * 305 * @atif: acpi handle 306 * 307 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function 308 * to determine the acceptable range of backlight values 309 * 310 * Backlight_caps.caps_valid will be set to true if the query is successful 311 * 312 * The input signals are in range 0-255 313 * 314 * This function assumes the display with backlight is the first LCD 315 * 316 * Returns 0 on success, error on failure. 317 */ 318 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif) 319 { 320 union acpi_object *info; 321 struct atif_qbtc_output characteristics; 322 struct atif_qbtc_arguments arguments; 323 struct acpi_buffer params; 324 size_t size; 325 int err = 0; 326 327 arguments.size = sizeof(arguments); 328 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1; 329 330 params.length = sizeof(arguments); 331 params.pointer = (void *)&arguments; 332 333 info = amdgpu_atif_call(atif, 334 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS, 335 ¶ms); 336 if (!info) { 337 err = -EIO; 338 goto out; 339 } 340 341 size = *(u16 *) info->buffer.pointer; 342 if (size < 10) { 343 err = -EINVAL; 344 goto out; 345 } 346 347 memset(&characteristics, 0, sizeof(characteristics)); 348 size = min(sizeof(characteristics), size); 349 memcpy(&characteristics, info->buffer.pointer, size); 350 351 atif->backlight_caps.caps_valid = true; 352 atif->backlight_caps.min_input_signal = 353 characteristics.min_input_signal; 354 atif->backlight_caps.max_input_signal = 355 characteristics.max_input_signal; 356 out: 357 kfree(info); 358 return err; 359 } 360 361 /** 362 * amdgpu_atif_get_sbios_requests - get requested sbios event 363 * 364 * @atif: acpi handle 365 * @req: atif sbios request struct 366 * 367 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function 368 * to determine what requests the sbios is making to the driver 369 * (all asics). 370 * Returns 0 on success, error on failure. 371 */ 372 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif, 373 struct atif_sbios_requests *req) 374 { 375 union acpi_object *info; 376 size_t size; 377 int count = 0; 378 379 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, 380 NULL); 381 if (!info) 382 return -EIO; 383 384 size = *(u16 *)info->buffer.pointer; 385 if (size < 0xd) { 386 count = -EINVAL; 387 goto out; 388 } 389 memset(req, 0, sizeof(*req)); 390 391 size = min(sizeof(*req), size); 392 memcpy(req, info->buffer.pointer, size); 393 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending); 394 395 count = hweight32(req->pending); 396 397 out: 398 kfree(info); 399 return count; 400 } 401 402 /** 403 * amdgpu_atif_handler - handle ATIF notify requests 404 * 405 * @adev: amdgpu_device pointer 406 * @event: atif sbios request struct 407 * 408 * Checks the acpi event and if it matches an atif event, 409 * handles it. 410 * 411 * Returns: 412 * NOTIFY_BAD or NOTIFY_DONE, depending on the event. 413 */ 414 static int amdgpu_atif_handler(struct amdgpu_device *adev, 415 struct acpi_bus_event *event) 416 { 417 struct amdgpu_atif *atif = adev->atif; 418 int count; 419 420 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n", 421 event->device_class, event->type); 422 423 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0) 424 return NOTIFY_DONE; 425 426 /* Is this actually our event? */ 427 if (!atif || 428 !atif->notification_cfg.enabled || 429 event->type != atif->notification_cfg.command_code) { 430 /* These events will generate keypresses otherwise */ 431 if (event->type == ACPI_VIDEO_NOTIFY_PROBE) 432 return NOTIFY_BAD; 433 else 434 return NOTIFY_DONE; 435 } 436 437 if (atif->functions.sbios_requests) { 438 struct atif_sbios_requests req; 439 440 /* Check pending SBIOS requests */ 441 count = amdgpu_atif_get_sbios_requests(atif, &req); 442 443 if (count <= 0) 444 return NOTIFY_BAD; 445 446 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count); 447 448 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) { 449 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 450 if (atif->bd) { 451 DRM_DEBUG_DRIVER("Changing brightness to %d\n", 452 req.backlight_level); 453 /* 454 * XXX backlight_device_set_brightness() is 455 * hardwired to post BACKLIGHT_UPDATE_SYSFS. 456 * It probably should accept 'reason' parameter. 457 */ 458 backlight_device_set_brightness(atif->bd, req.backlight_level); 459 } 460 #endif 461 } 462 463 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) { 464 if (adev->flags & AMD_IS_PX) { 465 pm_runtime_get_sync(adev_to_drm(adev)->dev); 466 /* Just fire off a uevent and let userspace tell us what to do */ 467 drm_helper_hpd_irq_event(adev_to_drm(adev)); 468 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 469 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 470 } 471 } 472 /* TODO: check other events */ 473 } 474 475 /* We've handled the event, stop the notifier chain. The ACPI interface 476 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to 477 * userspace if the event was generated only to signal a SBIOS 478 * request. 479 */ 480 return NOTIFY_BAD; 481 } 482 483 /* Call the ATCS method 484 */ 485 /** 486 * amdgpu_atcs_call - call an ATCS method 487 * 488 * @handle: acpi handle 489 * @function: the ATCS function to execute 490 * @params: ATCS function params 491 * 492 * Executes the requested ATCS function (all asics). 493 * Returns a pointer to the acpi output buffer. 494 */ 495 static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function, 496 struct acpi_buffer *params) 497 { 498 acpi_status status; 499 union acpi_object atcs_arg_elements[2]; 500 struct acpi_object_list atcs_arg; 501 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 502 503 atcs_arg.count = 2; 504 atcs_arg.pointer = &atcs_arg_elements[0]; 505 506 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER; 507 atcs_arg_elements[0].integer.value = function; 508 509 if (params) { 510 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER; 511 atcs_arg_elements[1].buffer.length = params->length; 512 atcs_arg_elements[1].buffer.pointer = params->pointer; 513 } else { 514 /* We need a second fake parameter */ 515 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER; 516 atcs_arg_elements[1].integer.value = 0; 517 } 518 519 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer); 520 521 /* Fail only if calling the method fails and ATIF is supported */ 522 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 523 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n", 524 acpi_format_exception(status)); 525 kfree(buffer.pointer); 526 return NULL; 527 } 528 529 return buffer.pointer; 530 } 531 532 /** 533 * amdgpu_atcs_parse_functions - parse supported functions 534 * 535 * @f: supported functions struct 536 * @mask: supported functions mask from ATCS 537 * 538 * Use the supported functions mask from ATCS function 539 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions 540 * are supported (all asics). 541 */ 542 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask) 543 { 544 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED; 545 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED; 546 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED; 547 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED; 548 } 549 550 /** 551 * amdgpu_atcs_verify_interface - verify ATCS 552 * 553 * @handle: acpi handle 554 * @atcs: amdgpu atcs struct 555 * 556 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function 557 * to initialize ATCS and determine what features are supported 558 * (all asics). 559 * returns 0 on success, error on failure. 560 */ 561 static int amdgpu_atcs_verify_interface(acpi_handle handle, 562 struct amdgpu_atcs *atcs) 563 { 564 union acpi_object *info; 565 struct atcs_verify_interface output; 566 size_t size; 567 int err = 0; 568 569 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL); 570 if (!info) 571 return -EIO; 572 573 memset(&output, 0, sizeof(output)); 574 575 size = *(u16 *) info->buffer.pointer; 576 if (size < 8) { 577 DRM_INFO("ATCS buffer is too small: %zu\n", size); 578 err = -EINVAL; 579 goto out; 580 } 581 size = min(sizeof(output), size); 582 583 memcpy(&output, info->buffer.pointer, size); 584 585 /* TODO: check version? */ 586 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version); 587 588 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits); 589 590 out: 591 kfree(info); 592 return err; 593 } 594 595 /** 596 * amdgpu_acpi_is_pcie_performance_request_supported 597 * 598 * @adev: amdgpu_device pointer 599 * 600 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods 601 * are supported (all asics). 602 * returns true if supported, false if not. 603 */ 604 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev) 605 { 606 struct amdgpu_atcs *atcs = &adev->atcs; 607 608 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy) 609 return true; 610 611 return false; 612 } 613 614 /** 615 * amdgpu_acpi_pcie_notify_device_ready 616 * 617 * @adev: amdgpu_device pointer 618 * 619 * Executes the PCIE_DEVICE_READY_NOTIFICATION method 620 * (all asics). 621 * returns 0 on success, error on failure. 622 */ 623 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev) 624 { 625 acpi_handle handle; 626 union acpi_object *info; 627 struct amdgpu_atcs *atcs = &adev->atcs; 628 629 /* Get the device handle */ 630 handle = ACPI_HANDLE(&adev->pdev->dev); 631 if (!handle) 632 return -EINVAL; 633 634 if (!atcs->functions.pcie_dev_rdy) 635 return -EINVAL; 636 637 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL); 638 if (!info) 639 return -EIO; 640 641 kfree(info); 642 643 return 0; 644 } 645 646 /** 647 * amdgpu_acpi_pcie_performance_request 648 * 649 * @adev: amdgpu_device pointer 650 * @perf_req: requested perf level (pcie gen speed) 651 * @advertise: set advertise caps flag if set 652 * 653 * Executes the PCIE_PERFORMANCE_REQUEST method to 654 * change the pcie gen speed (all asics). 655 * returns 0 on success, error on failure. 656 */ 657 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev, 658 u8 perf_req, bool advertise) 659 { 660 acpi_handle handle; 661 union acpi_object *info; 662 struct amdgpu_atcs *atcs = &adev->atcs; 663 struct atcs_pref_req_input atcs_input; 664 struct atcs_pref_req_output atcs_output; 665 struct acpi_buffer params; 666 size_t size; 667 u32 retry = 3; 668 669 if (amdgpu_acpi_pcie_notify_device_ready(adev)) 670 return -EINVAL; 671 672 /* Get the device handle */ 673 handle = ACPI_HANDLE(&adev->pdev->dev); 674 if (!handle) 675 return -EINVAL; 676 677 if (!atcs->functions.pcie_perf_req) 678 return -EINVAL; 679 680 atcs_input.size = sizeof(struct atcs_pref_req_input); 681 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */ 682 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8); 683 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK; 684 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION; 685 if (advertise) 686 atcs_input.flags |= ATCS_ADVERTISE_CAPS; 687 atcs_input.req_type = ATCS_PCIE_LINK_SPEED; 688 atcs_input.perf_req = perf_req; 689 690 params.length = sizeof(struct atcs_pref_req_input); 691 params.pointer = &atcs_input; 692 693 while (retry--) { 694 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms); 695 if (!info) 696 return -EIO; 697 698 memset(&atcs_output, 0, sizeof(atcs_output)); 699 700 size = *(u16 *) info->buffer.pointer; 701 if (size < 3) { 702 DRM_INFO("ATCS buffer is too small: %zu\n", size); 703 kfree(info); 704 return -EINVAL; 705 } 706 size = min(sizeof(atcs_output), size); 707 708 memcpy(&atcs_output, info->buffer.pointer, size); 709 710 kfree(info); 711 712 switch (atcs_output.ret_val) { 713 case ATCS_REQUEST_REFUSED: 714 default: 715 return -EINVAL; 716 case ATCS_REQUEST_COMPLETE: 717 return 0; 718 case ATCS_REQUEST_IN_PROGRESS: 719 udelay(10); 720 break; 721 } 722 } 723 724 return 0; 725 } 726 727 /** 728 * amdgpu_acpi_event - handle notify events 729 * 730 * @nb: notifier block 731 * @val: val 732 * @data: acpi event 733 * 734 * Calls relevant amdgpu functions in response to various 735 * acpi events. 736 * Returns NOTIFY code 737 */ 738 static int amdgpu_acpi_event(struct notifier_block *nb, 739 unsigned long val, 740 void *data) 741 { 742 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb); 743 struct acpi_bus_event *entry = (struct acpi_bus_event *)data; 744 745 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) { 746 if (power_supply_is_system_supplied() > 0) 747 DRM_DEBUG_DRIVER("pm: AC\n"); 748 else 749 DRM_DEBUG_DRIVER("pm: DC\n"); 750 751 amdgpu_pm_acpi_event_handler(adev); 752 } 753 754 /* Check for pending SBIOS requests */ 755 return amdgpu_atif_handler(adev, entry); 756 } 757 758 /* Call all ACPI methods here */ 759 /** 760 * amdgpu_acpi_init - init driver acpi support 761 * 762 * @adev: amdgpu_device pointer 763 * 764 * Verifies the AMD ACPI interfaces and registers with the acpi 765 * notifier chain (all asics). 766 * Returns 0 on success, error on failure. 767 */ 768 int amdgpu_acpi_init(struct amdgpu_device *adev) 769 { 770 acpi_handle handle, atif_handle; 771 struct amdgpu_atif *atif; 772 struct amdgpu_atcs *atcs = &adev->atcs; 773 int ret; 774 775 /* Get the device handle */ 776 handle = ACPI_HANDLE(&adev->pdev->dev); 777 778 if (!adev->bios || !handle) 779 return 0; 780 781 /* Call the ATCS method */ 782 ret = amdgpu_atcs_verify_interface(handle, atcs); 783 if (ret) { 784 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret); 785 } 786 787 /* Probe for ATIF, and initialize it if found */ 788 atif_handle = amdgpu_atif_probe_handle(handle); 789 if (!atif_handle) 790 goto out; 791 792 atif = kzalloc(sizeof(*atif), GFP_KERNEL); 793 if (!atif) { 794 DRM_WARN("Not enough memory to initialize ATIF\n"); 795 goto out; 796 } 797 atif->handle = atif_handle; 798 799 /* Call the ATIF method */ 800 ret = amdgpu_atif_verify_interface(atif); 801 if (ret) { 802 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret); 803 kfree(atif); 804 goto out; 805 } 806 adev->atif = atif; 807 808 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 809 if (atif->notifications.brightness_change) { 810 if (amdgpu_device_has_dc_support(adev)) { 811 #if defined(CONFIG_DRM_AMD_DC) 812 struct amdgpu_display_manager *dm = &adev->dm; 813 atif->bd = dm->backlight_dev; 814 #endif 815 } else { 816 struct drm_encoder *tmp; 817 818 /* Find the encoder controlling the brightness */ 819 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list, 820 head) { 821 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp); 822 823 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) && 824 enc->enc_priv) { 825 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv; 826 if (dig->bl_dev) { 827 atif->bd = dig->bl_dev; 828 break; 829 } 830 } 831 } 832 } 833 } 834 #endif 835 836 if (atif->functions.sbios_requests && !atif->functions.system_params) { 837 /* XXX check this workraround, if sbios request function is 838 * present we have to see how it's configured in the system 839 * params 840 */ 841 atif->functions.system_params = true; 842 } 843 844 if (atif->functions.system_params) { 845 ret = amdgpu_atif_get_notification_params(atif); 846 if (ret) { 847 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n", 848 ret); 849 /* Disable notification */ 850 atif->notification_cfg.enabled = false; 851 } 852 } 853 854 if (atif->functions.query_backlight_transfer_characteristics) { 855 ret = amdgpu_atif_query_backlight_caps(atif); 856 if (ret) { 857 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n", 858 ret); 859 atif->backlight_caps.caps_valid = false; 860 } 861 } else { 862 atif->backlight_caps.caps_valid = false; 863 } 864 865 out: 866 adev->acpi_nb.notifier_call = amdgpu_acpi_event; 867 register_acpi_notifier(&adev->acpi_nb); 868 869 return ret; 870 } 871 872 void amdgpu_acpi_get_backlight_caps(struct amdgpu_device *adev, 873 struct amdgpu_dm_backlight_caps *caps) 874 { 875 if (!adev->atif) { 876 caps->caps_valid = false; 877 return; 878 } 879 caps->caps_valid = adev->atif->backlight_caps.caps_valid; 880 caps->min_input_signal = adev->atif->backlight_caps.min_input_signal; 881 caps->max_input_signal = adev->atif->backlight_caps.max_input_signal; 882 } 883 884 /** 885 * amdgpu_acpi_fini - tear down driver acpi support 886 * 887 * @adev: amdgpu_device pointer 888 * 889 * Unregisters with the acpi notifier chain (all asics). 890 */ 891 void amdgpu_acpi_fini(struct amdgpu_device *adev) 892 { 893 unregister_acpi_notifier(&adev->acpi_nb); 894 kfree(adev->atif); 895 } 896 897 /** 898 * amdgpu_acpi_is_s0ix_supported 899 * 900 * @adev: amdgpu_device_pointer 901 * 902 * returns true if supported, false if not. 903 */ 904 bool amdgpu_acpi_is_s0ix_supported(struct amdgpu_device *adev) 905 { 906 #if defined(CONFIG_AMD_PMC) || defined(CONFIG_AMD_PMC_MODULE) 907 if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) { 908 if (adev->flags & AMD_IS_APU) 909 return true; 910 } 911 #endif 912 return false; 913 } 914