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 <linux/suspend.h> 30 #include <acpi/video.h> 31 #include <acpi/actbl.h> 32 33 #include <drm/drm_crtc_helper.h> 34 #include "amdgpu.h" 35 #include "amdgpu_pm.h" 36 #include "amdgpu_display.h" 37 #include "amd_acpi.h" 38 #include "atom.h" 39 40 struct amdgpu_atif_notification_cfg { 41 bool enabled; 42 int command_code; 43 }; 44 45 struct amdgpu_atif_notifications { 46 bool thermal_state; 47 bool forced_power_state; 48 bool system_power_state; 49 bool brightness_change; 50 bool dgpu_display_event; 51 bool gpu_package_power_limit; 52 }; 53 54 struct amdgpu_atif_functions { 55 bool system_params; 56 bool sbios_requests; 57 bool temperature_change; 58 bool query_backlight_transfer_characteristics; 59 bool ready_to_undock; 60 bool external_gpu_information; 61 }; 62 63 struct amdgpu_atif { 64 acpi_handle handle; 65 66 struct amdgpu_atif_notifications notifications; 67 struct amdgpu_atif_functions functions; 68 struct amdgpu_atif_notification_cfg notification_cfg; 69 struct backlight_device *bd; 70 struct amdgpu_dm_backlight_caps backlight_caps; 71 }; 72 73 struct amdgpu_atcs_functions { 74 bool get_ext_state; 75 bool pcie_perf_req; 76 bool pcie_dev_rdy; 77 bool pcie_bus_width; 78 bool power_shift_control; 79 }; 80 81 struct amdgpu_atcs { 82 acpi_handle handle; 83 84 struct amdgpu_atcs_functions functions; 85 }; 86 87 static struct amdgpu_acpi_priv { 88 struct amdgpu_atif atif; 89 struct amdgpu_atcs atcs; 90 } amdgpu_acpi_priv; 91 92 /* Call the ATIF method 93 */ 94 /** 95 * amdgpu_atif_call - call an ATIF method 96 * 97 * @atif: atif structure 98 * @function: the ATIF function to execute 99 * @params: ATIF function params 100 * 101 * Executes the requested ATIF function (all asics). 102 * Returns a pointer to the acpi output buffer. 103 */ 104 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif, 105 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 /* We need a second fake parameter */ 125 atif_arg_elements[1].type = ACPI_TYPE_INTEGER; 126 atif_arg_elements[1].integer.value = 0; 127 } 128 129 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg, 130 &buffer); 131 132 /* Fail only if calling the method fails and ATIF is supported */ 133 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 134 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n", 135 acpi_format_exception(status)); 136 kfree(buffer.pointer); 137 return NULL; 138 } 139 140 return buffer.pointer; 141 } 142 143 /** 144 * amdgpu_atif_parse_notification - parse supported notifications 145 * 146 * @n: supported notifications struct 147 * @mask: supported notifications mask from ATIF 148 * 149 * Use the supported notifications mask from ATIF function 150 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications 151 * are supported (all asics). 152 */ 153 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask) 154 { 155 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED; 156 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED; 157 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED; 158 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED; 159 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED; 160 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED; 161 } 162 163 /** 164 * amdgpu_atif_parse_functions - parse supported functions 165 * 166 * @f: supported functions struct 167 * @mask: supported functions mask from ATIF 168 * 169 * Use the supported functions mask from ATIF function 170 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions 171 * are supported (all asics). 172 */ 173 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask) 174 { 175 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED; 176 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED; 177 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED; 178 f->query_backlight_transfer_characteristics = 179 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED; 180 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED; 181 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED; 182 } 183 184 /** 185 * amdgpu_atif_verify_interface - verify ATIF 186 * 187 * @atif: amdgpu atif struct 188 * 189 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function 190 * to initialize ATIF and determine what features are supported 191 * (all asics). 192 * returns 0 on success, error on failure. 193 */ 194 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif) 195 { 196 union acpi_object *info; 197 struct atif_verify_interface output; 198 size_t size; 199 int err = 0; 200 201 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL); 202 if (!info) 203 return -EIO; 204 205 memset(&output, 0, sizeof(output)); 206 207 size = *(u16 *) info->buffer.pointer; 208 if (size < 12) { 209 DRM_INFO("ATIF buffer is too small: %zu\n", size); 210 err = -EINVAL; 211 goto out; 212 } 213 size = min(sizeof(output), size); 214 215 memcpy(&output, info->buffer.pointer, size); 216 217 /* TODO: check version? */ 218 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version); 219 220 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask); 221 amdgpu_atif_parse_functions(&atif->functions, output.function_bits); 222 223 out: 224 kfree(info); 225 return err; 226 } 227 228 /** 229 * amdgpu_atif_get_notification_params - determine notify configuration 230 * 231 * @atif: acpi handle 232 * 233 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function 234 * to determine if a notifier is used and if so which one 235 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n) 236 * where n is specified in the result if a notifier is used. 237 * Returns 0 on success, error on failure. 238 */ 239 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif) 240 { 241 union acpi_object *info; 242 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg; 243 struct atif_system_params params; 244 size_t size; 245 int err = 0; 246 247 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, 248 NULL); 249 if (!info) { 250 err = -EIO; 251 goto out; 252 } 253 254 size = *(u16 *) info->buffer.pointer; 255 if (size < 10) { 256 err = -EINVAL; 257 goto out; 258 } 259 260 memset(¶ms, 0, sizeof(params)); 261 size = min(sizeof(params), size); 262 memcpy(¶ms, info->buffer.pointer, size); 263 264 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n", 265 params.flags, params.valid_mask); 266 params.flags = params.flags & params.valid_mask; 267 268 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) { 269 n->enabled = false; 270 n->command_code = 0; 271 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) { 272 n->enabled = true; 273 n->command_code = 0x81; 274 } else { 275 if (size < 11) { 276 err = -EINVAL; 277 goto out; 278 } 279 n->enabled = true; 280 n->command_code = params.command_code; 281 } 282 283 out: 284 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n", 285 (n->enabled ? "enabled" : "disabled"), 286 n->command_code); 287 kfree(info); 288 return err; 289 } 290 291 /** 292 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal 293 * 294 * @atif: acpi handle 295 * 296 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function 297 * to determine the acceptable range of backlight values 298 * 299 * Backlight_caps.caps_valid will be set to true if the query is successful 300 * 301 * The input signals are in range 0-255 302 * 303 * This function assumes the display with backlight is the first LCD 304 * 305 * Returns 0 on success, error on failure. 306 */ 307 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif) 308 { 309 union acpi_object *info; 310 struct atif_qbtc_output characteristics; 311 struct atif_qbtc_arguments arguments; 312 struct acpi_buffer params; 313 size_t size; 314 int err = 0; 315 316 arguments.size = sizeof(arguments); 317 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1; 318 319 params.length = sizeof(arguments); 320 params.pointer = (void *)&arguments; 321 322 info = amdgpu_atif_call(atif, 323 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS, 324 ¶ms); 325 if (!info) { 326 err = -EIO; 327 goto out; 328 } 329 330 size = *(u16 *) info->buffer.pointer; 331 if (size < 10) { 332 err = -EINVAL; 333 goto out; 334 } 335 336 memset(&characteristics, 0, sizeof(characteristics)); 337 size = min(sizeof(characteristics), size); 338 memcpy(&characteristics, info->buffer.pointer, size); 339 340 atif->backlight_caps.caps_valid = true; 341 atif->backlight_caps.min_input_signal = 342 characteristics.min_input_signal; 343 atif->backlight_caps.max_input_signal = 344 characteristics.max_input_signal; 345 out: 346 kfree(info); 347 return err; 348 } 349 350 /** 351 * amdgpu_atif_get_sbios_requests - get requested sbios event 352 * 353 * @atif: acpi handle 354 * @req: atif sbios request struct 355 * 356 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function 357 * to determine what requests the sbios is making to the driver 358 * (all asics). 359 * Returns 0 on success, error on failure. 360 */ 361 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif, 362 struct atif_sbios_requests *req) 363 { 364 union acpi_object *info; 365 size_t size; 366 int count = 0; 367 368 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, 369 NULL); 370 if (!info) 371 return -EIO; 372 373 size = *(u16 *)info->buffer.pointer; 374 if (size < 0xd) { 375 count = -EINVAL; 376 goto out; 377 } 378 memset(req, 0, sizeof(*req)); 379 380 size = min(sizeof(*req), size); 381 memcpy(req, info->buffer.pointer, size); 382 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending); 383 384 count = hweight32(req->pending); 385 386 out: 387 kfree(info); 388 return count; 389 } 390 391 /** 392 * amdgpu_atif_handler - handle ATIF notify requests 393 * 394 * @adev: amdgpu_device pointer 395 * @event: atif sbios request struct 396 * 397 * Checks the acpi event and if it matches an atif event, 398 * handles it. 399 * 400 * Returns: 401 * NOTIFY_BAD or NOTIFY_DONE, depending on the event. 402 */ 403 static int amdgpu_atif_handler(struct amdgpu_device *adev, 404 struct acpi_bus_event *event) 405 { 406 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 407 int count; 408 409 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n", 410 event->device_class, event->type); 411 412 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0) 413 return NOTIFY_DONE; 414 415 /* Is this actually our event? */ 416 if (!atif->notification_cfg.enabled || 417 event->type != atif->notification_cfg.command_code) { 418 /* These events will generate keypresses otherwise */ 419 if (event->type == ACPI_VIDEO_NOTIFY_PROBE) 420 return NOTIFY_BAD; 421 else 422 return NOTIFY_DONE; 423 } 424 425 if (atif->functions.sbios_requests) { 426 struct atif_sbios_requests req; 427 428 /* Check pending SBIOS requests */ 429 count = amdgpu_atif_get_sbios_requests(atif, &req); 430 431 if (count <= 0) 432 return NOTIFY_BAD; 433 434 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count); 435 436 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) { 437 if (atif->bd) { 438 DRM_DEBUG_DRIVER("Changing brightness to %d\n", 439 req.backlight_level); 440 /* 441 * XXX backlight_device_set_brightness() is 442 * hardwired to post BACKLIGHT_UPDATE_SYSFS. 443 * It probably should accept 'reason' parameter. 444 */ 445 backlight_device_set_brightness(atif->bd, req.backlight_level); 446 } 447 } 448 449 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) { 450 if (adev->flags & AMD_IS_PX) { 451 pm_runtime_get_sync(adev_to_drm(adev)->dev); 452 /* Just fire off a uevent and let userspace tell us what to do */ 453 drm_helper_hpd_irq_event(adev_to_drm(adev)); 454 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 455 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 456 } 457 } 458 /* TODO: check other events */ 459 } 460 461 /* We've handled the event, stop the notifier chain. The ACPI interface 462 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to 463 * userspace if the event was generated only to signal a SBIOS 464 * request. 465 */ 466 return NOTIFY_BAD; 467 } 468 469 /* Call the ATCS method 470 */ 471 /** 472 * amdgpu_atcs_call - call an ATCS method 473 * 474 * @atcs: atcs structure 475 * @function: the ATCS function to execute 476 * @params: ATCS function params 477 * 478 * Executes the requested ATCS function (all asics). 479 * Returns a pointer to the acpi output buffer. 480 */ 481 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs, 482 int function, 483 struct acpi_buffer *params) 484 { 485 acpi_status status; 486 union acpi_object atcs_arg_elements[2]; 487 struct acpi_object_list atcs_arg; 488 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 489 490 atcs_arg.count = 2; 491 atcs_arg.pointer = &atcs_arg_elements[0]; 492 493 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER; 494 atcs_arg_elements[0].integer.value = function; 495 496 if (params) { 497 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER; 498 atcs_arg_elements[1].buffer.length = params->length; 499 atcs_arg_elements[1].buffer.pointer = params->pointer; 500 } else { 501 /* We need a second fake parameter */ 502 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER; 503 atcs_arg_elements[1].integer.value = 0; 504 } 505 506 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer); 507 508 /* Fail only if calling the method fails and ATIF is supported */ 509 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 510 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n", 511 acpi_format_exception(status)); 512 kfree(buffer.pointer); 513 return NULL; 514 } 515 516 return buffer.pointer; 517 } 518 519 /** 520 * amdgpu_atcs_parse_functions - parse supported functions 521 * 522 * @f: supported functions struct 523 * @mask: supported functions mask from ATCS 524 * 525 * Use the supported functions mask from ATCS function 526 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions 527 * are supported (all asics). 528 */ 529 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask) 530 { 531 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED; 532 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED; 533 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED; 534 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED; 535 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED; 536 } 537 538 /** 539 * amdgpu_atcs_verify_interface - verify ATCS 540 * 541 * @atcs: amdgpu atcs struct 542 * 543 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function 544 * to initialize ATCS and determine what features are supported 545 * (all asics). 546 * returns 0 on success, error on failure. 547 */ 548 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs) 549 { 550 union acpi_object *info; 551 struct atcs_verify_interface output; 552 size_t size; 553 int err = 0; 554 555 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL); 556 if (!info) 557 return -EIO; 558 559 memset(&output, 0, sizeof(output)); 560 561 size = *(u16 *) info->buffer.pointer; 562 if (size < 8) { 563 DRM_INFO("ATCS buffer is too small: %zu\n", size); 564 err = -EINVAL; 565 goto out; 566 } 567 size = min(sizeof(output), size); 568 569 memcpy(&output, info->buffer.pointer, size); 570 571 /* TODO: check version? */ 572 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version); 573 574 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits); 575 576 out: 577 kfree(info); 578 return err; 579 } 580 581 /** 582 * amdgpu_acpi_is_pcie_performance_request_supported 583 * 584 * @adev: amdgpu_device pointer 585 * 586 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods 587 * are supported (all asics). 588 * returns true if supported, false if not. 589 */ 590 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev) 591 { 592 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 593 594 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy) 595 return true; 596 597 return false; 598 } 599 600 /** 601 * amdgpu_acpi_is_power_shift_control_supported 602 * 603 * Check if the ATCS power shift control method 604 * is supported. 605 * returns true if supported, false if not. 606 */ 607 bool amdgpu_acpi_is_power_shift_control_supported(void) 608 { 609 return amdgpu_acpi_priv.atcs.functions.power_shift_control; 610 } 611 612 /** 613 * amdgpu_acpi_pcie_notify_device_ready 614 * 615 * @adev: amdgpu_device pointer 616 * 617 * Executes the PCIE_DEVICE_READY_NOTIFICATION method 618 * (all asics). 619 * returns 0 on success, error on failure. 620 */ 621 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev) 622 { 623 union acpi_object *info; 624 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 625 626 if (!atcs->functions.pcie_dev_rdy) 627 return -EINVAL; 628 629 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL); 630 if (!info) 631 return -EIO; 632 633 kfree(info); 634 635 return 0; 636 } 637 638 /** 639 * amdgpu_acpi_pcie_performance_request 640 * 641 * @adev: amdgpu_device pointer 642 * @perf_req: requested perf level (pcie gen speed) 643 * @advertise: set advertise caps flag if set 644 * 645 * Executes the PCIE_PERFORMANCE_REQUEST method to 646 * change the pcie gen speed (all asics). 647 * returns 0 on success, error on failure. 648 */ 649 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev, 650 u8 perf_req, bool advertise) 651 { 652 union acpi_object *info; 653 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 654 struct atcs_pref_req_input atcs_input; 655 struct atcs_pref_req_output atcs_output; 656 struct acpi_buffer params; 657 size_t size; 658 u32 retry = 3; 659 660 if (amdgpu_acpi_pcie_notify_device_ready(adev)) 661 return -EINVAL; 662 663 if (!atcs->functions.pcie_perf_req) 664 return -EINVAL; 665 666 atcs_input.size = sizeof(struct atcs_pref_req_input); 667 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */ 668 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8); 669 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK; 670 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION; 671 if (advertise) 672 atcs_input.flags |= ATCS_ADVERTISE_CAPS; 673 atcs_input.req_type = ATCS_PCIE_LINK_SPEED; 674 atcs_input.perf_req = perf_req; 675 676 params.length = sizeof(struct atcs_pref_req_input); 677 params.pointer = &atcs_input; 678 679 while (retry--) { 680 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms); 681 if (!info) 682 return -EIO; 683 684 memset(&atcs_output, 0, sizeof(atcs_output)); 685 686 size = *(u16 *) info->buffer.pointer; 687 if (size < 3) { 688 DRM_INFO("ATCS buffer is too small: %zu\n", size); 689 kfree(info); 690 return -EINVAL; 691 } 692 size = min(sizeof(atcs_output), size); 693 694 memcpy(&atcs_output, info->buffer.pointer, size); 695 696 kfree(info); 697 698 switch (atcs_output.ret_val) { 699 case ATCS_REQUEST_REFUSED: 700 default: 701 return -EINVAL; 702 case ATCS_REQUEST_COMPLETE: 703 return 0; 704 case ATCS_REQUEST_IN_PROGRESS: 705 udelay(10); 706 break; 707 } 708 } 709 710 return 0; 711 } 712 713 /** 714 * amdgpu_acpi_power_shift_control 715 * 716 * @adev: amdgpu_device pointer 717 * @dev_state: device acpi state 718 * @drv_state: driver state 719 * 720 * Executes the POWER_SHIFT_CONTROL method to 721 * communicate current dGPU device state and 722 * driver state to APU/SBIOS. 723 * returns 0 on success, error on failure. 724 */ 725 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev, 726 u8 dev_state, bool drv_state) 727 { 728 union acpi_object *info; 729 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 730 struct atcs_pwr_shift_input atcs_input; 731 struct acpi_buffer params; 732 733 if (!amdgpu_acpi_is_power_shift_control_supported()) 734 return -EINVAL; 735 736 atcs_input.size = sizeof(struct atcs_pwr_shift_input); 737 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */ 738 atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8); 739 atcs_input.dev_acpi_state = dev_state; 740 atcs_input.drv_state = drv_state; 741 742 params.length = sizeof(struct atcs_pwr_shift_input); 743 params.pointer = &atcs_input; 744 745 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms); 746 if (!info) { 747 DRM_ERROR("ATCS PSC update failed\n"); 748 return -EIO; 749 } 750 751 return 0; 752 } 753 754 /** 755 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS 756 * 757 * @dev: drm_device pointer 758 * @ss_state: current smart shift event 759 * 760 * returns 0 on success, 761 * otherwise return error number. 762 */ 763 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state) 764 { 765 struct amdgpu_device *adev = drm_to_adev(dev); 766 int r; 767 768 if (!amdgpu_device_supports_smart_shift(dev)) 769 return 0; 770 771 switch (ss_state) { 772 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational. 773 * SBIOS trigger “stop” at D3, Driver Not Operational. 774 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational. 775 */ 776 case AMDGPU_SS_DRV_LOAD: 777 r = amdgpu_acpi_power_shift_control(adev, 778 AMDGPU_ATCS_PSC_DEV_STATE_D0, 779 AMDGPU_ATCS_PSC_DRV_STATE_OPR); 780 break; 781 case AMDGPU_SS_DEV_D0: 782 r = amdgpu_acpi_power_shift_control(adev, 783 AMDGPU_ATCS_PSC_DEV_STATE_D0, 784 AMDGPU_ATCS_PSC_DRV_STATE_OPR); 785 break; 786 case AMDGPU_SS_DEV_D3: 787 r = amdgpu_acpi_power_shift_control(adev, 788 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT, 789 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR); 790 break; 791 case AMDGPU_SS_DRV_UNLOAD: 792 r = amdgpu_acpi_power_shift_control(adev, 793 AMDGPU_ATCS_PSC_DEV_STATE_D0, 794 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR); 795 break; 796 default: 797 return -EINVAL; 798 } 799 800 return r; 801 } 802 803 /** 804 * amdgpu_acpi_event - handle notify events 805 * 806 * @nb: notifier block 807 * @val: val 808 * @data: acpi event 809 * 810 * Calls relevant amdgpu functions in response to various 811 * acpi events. 812 * Returns NOTIFY code 813 */ 814 static int amdgpu_acpi_event(struct notifier_block *nb, 815 unsigned long val, 816 void *data) 817 { 818 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb); 819 struct acpi_bus_event *entry = (struct acpi_bus_event *)data; 820 821 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) { 822 if (power_supply_is_system_supplied() > 0) 823 DRM_DEBUG_DRIVER("pm: AC\n"); 824 else 825 DRM_DEBUG_DRIVER("pm: DC\n"); 826 827 amdgpu_pm_acpi_event_handler(adev); 828 } 829 830 /* Check for pending SBIOS requests */ 831 return amdgpu_atif_handler(adev, entry); 832 } 833 834 /* Call all ACPI methods here */ 835 /** 836 * amdgpu_acpi_init - init driver acpi support 837 * 838 * @adev: amdgpu_device pointer 839 * 840 * Verifies the AMD ACPI interfaces and registers with the acpi 841 * notifier chain (all asics). 842 * Returns 0 on success, error on failure. 843 */ 844 int amdgpu_acpi_init(struct amdgpu_device *adev) 845 { 846 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 847 848 if (atif->notifications.brightness_change) { 849 if (amdgpu_device_has_dc_support(adev)) { 850 #if defined(CONFIG_DRM_AMD_DC) 851 struct amdgpu_display_manager *dm = &adev->dm; 852 if (dm->backlight_dev[0]) 853 atif->bd = dm->backlight_dev[0]; 854 #endif 855 } else { 856 struct drm_encoder *tmp; 857 858 /* Find the encoder controlling the brightness */ 859 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list, 860 head) { 861 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp); 862 863 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) && 864 enc->enc_priv) { 865 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv; 866 if (dig->bl_dev) { 867 atif->bd = dig->bl_dev; 868 break; 869 } 870 } 871 } 872 } 873 } 874 adev->acpi_nb.notifier_call = amdgpu_acpi_event; 875 register_acpi_notifier(&adev->acpi_nb); 876 877 return 0; 878 } 879 880 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps) 881 { 882 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 883 884 caps->caps_valid = atif->backlight_caps.caps_valid; 885 caps->min_input_signal = atif->backlight_caps.min_input_signal; 886 caps->max_input_signal = atif->backlight_caps.max_input_signal; 887 } 888 889 /** 890 * amdgpu_acpi_fini - tear down driver acpi support 891 * 892 * @adev: amdgpu_device pointer 893 * 894 * Unregisters with the acpi notifier chain (all asics). 895 */ 896 void amdgpu_acpi_fini(struct amdgpu_device *adev) 897 { 898 unregister_acpi_notifier(&adev->acpi_nb); 899 } 900 901 /** 902 * amdgpu_atif_pci_probe_handle - look up the ATIF handle 903 * 904 * @pdev: pci device 905 * 906 * Look up the ATIF handles (all asics). 907 * Returns true if the handle is found, false if not. 908 */ 909 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev) 910 { 911 char acpi_method_name[255] = { 0 }; 912 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; 913 acpi_handle dhandle, atif_handle; 914 acpi_status status; 915 int ret; 916 917 dhandle = ACPI_HANDLE(&pdev->dev); 918 if (!dhandle) 919 return false; 920 921 status = acpi_get_handle(dhandle, "ATIF", &atif_handle); 922 if (ACPI_FAILURE(status)) { 923 return false; 924 } 925 amdgpu_acpi_priv.atif.handle = atif_handle; 926 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer); 927 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name); 928 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif); 929 if (ret) { 930 amdgpu_acpi_priv.atif.handle = 0; 931 return false; 932 } 933 return true; 934 } 935 936 /** 937 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle 938 * 939 * @pdev: pci device 940 * 941 * Look up the ATCS handles (all asics). 942 * Returns true if the handle is found, false if not. 943 */ 944 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev) 945 { 946 char acpi_method_name[255] = { 0 }; 947 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name }; 948 acpi_handle dhandle, atcs_handle; 949 acpi_status status; 950 int ret; 951 952 dhandle = ACPI_HANDLE(&pdev->dev); 953 if (!dhandle) 954 return false; 955 956 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle); 957 if (ACPI_FAILURE(status)) { 958 return false; 959 } 960 amdgpu_acpi_priv.atcs.handle = atcs_handle; 961 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer); 962 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name); 963 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs); 964 if (ret) { 965 amdgpu_acpi_priv.atcs.handle = 0; 966 return false; 967 } 968 return true; 969 } 970 971 /* 972 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods 973 * 974 * Check if we have the ATIF/ATCS methods and populate 975 * the structures in the driver. 976 */ 977 void amdgpu_acpi_detect(void) 978 { 979 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 980 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 981 struct pci_dev *pdev = NULL; 982 int ret; 983 984 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { 985 if (!atif->handle) 986 amdgpu_atif_pci_probe_handle(pdev); 987 if (!atcs->handle) 988 amdgpu_atcs_pci_probe_handle(pdev); 989 } 990 991 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) { 992 if (!atif->handle) 993 amdgpu_atif_pci_probe_handle(pdev); 994 if (!atcs->handle) 995 amdgpu_atcs_pci_probe_handle(pdev); 996 } 997 998 if (atif->functions.sbios_requests && !atif->functions.system_params) { 999 /* XXX check this workraround, if sbios request function is 1000 * present we have to see how it's configured in the system 1001 * params 1002 */ 1003 atif->functions.system_params = true; 1004 } 1005 1006 if (atif->functions.system_params) { 1007 ret = amdgpu_atif_get_notification_params(atif); 1008 if (ret) { 1009 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n", 1010 ret); 1011 /* Disable notification */ 1012 atif->notification_cfg.enabled = false; 1013 } 1014 } 1015 1016 if (atif->functions.query_backlight_transfer_characteristics) { 1017 ret = amdgpu_atif_query_backlight_caps(atif); 1018 if (ret) { 1019 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n", 1020 ret); 1021 atif->backlight_caps.caps_valid = false; 1022 } 1023 } else { 1024 atif->backlight_caps.caps_valid = false; 1025 } 1026 } 1027 1028 #if IS_ENABLED(CONFIG_SUSPEND) 1029 /** 1030 * amdgpu_acpi_is_s3_active 1031 * 1032 * @adev: amdgpu_device_pointer 1033 * 1034 * returns true if supported, false if not. 1035 */ 1036 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev) 1037 { 1038 return !(adev->flags & AMD_IS_APU) || 1039 (pm_suspend_target_state == PM_SUSPEND_MEM); 1040 } 1041 1042 /** 1043 * amdgpu_acpi_should_gpu_reset 1044 * 1045 * @adev: amdgpu_device_pointer 1046 * 1047 * returns true if should reset GPU, false if not 1048 */ 1049 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev) 1050 { 1051 if (adev->flags & AMD_IS_APU) 1052 return false; 1053 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE; 1054 } 1055 1056 /** 1057 * amdgpu_acpi_is_s0ix_active 1058 * 1059 * @adev: amdgpu_device_pointer 1060 * 1061 * returns true if supported, false if not. 1062 */ 1063 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev) 1064 { 1065 if (!(adev->flags & AMD_IS_APU) || 1066 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE)) 1067 return false; 1068 1069 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) { 1070 dev_warn_once(adev->dev, 1071 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n" 1072 "To use suspend-to-idle change the sleep mode in BIOS setup.\n"); 1073 return false; 1074 } 1075 1076 #if !IS_ENABLED(CONFIG_AMD_PMC) 1077 dev_warn_once(adev->dev, 1078 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n"); 1079 return false; 1080 #else 1081 return true; 1082 #endif /* CONFIG_AMD_PMC */ 1083 } 1084 1085 #endif /* CONFIG_SUSPEND */ 1086