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