1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright 2012 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 */ 24 25 #include <linux/pci.h> 26 #include <linux/acpi.h> 27 #include <linux/backlight.h> 28 #include <linux/slab.h> 29 #include <linux/xarray.h> 30 #include <linux/power_supply.h> 31 #include <linux/numa.h> 32 #include <linux/pm_runtime.h> 33 #include <linux/suspend.h> 34 #include <acpi/video.h> 35 #include <acpi/actbl.h> 36 37 #include "amdgpu.h" 38 #include "amdgpu_pm.h" 39 #include "amdgpu_display.h" 40 #include "amd_acpi.h" 41 #include "atom.h" 42 43 /* Declare GUID for AMD _DSM method for XCCs */ 44 static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2, 45 0xb8, 0xb4, 0x45, 0x56, 0x2e, 46 0x8c, 0x5b, 0xec); 47 48 #define AMD_XCC_HID_START 3000 49 #define AMD_XCC_DSM_GET_NUM_FUNCS 0 50 #define AMD_XCC_DSM_GET_SUPP_MODE 1 51 #define AMD_XCC_DSM_GET_XCP_MODE 2 52 #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4 53 #define AMD_XCC_DSM_GET_TMR_INFO 5 54 #define AMD_XCC_DSM_NUM_FUNCS 5 55 56 #define AMD_XCC_MAX_HID 24 57 58 struct xarray numa_info_xa; 59 60 /* Encapsulates the XCD acpi object information */ 61 struct amdgpu_acpi_xcc_info { 62 struct list_head list; 63 struct amdgpu_numa_info *numa_info; 64 uint8_t xcp_node; 65 uint8_t phy_id; 66 acpi_handle handle; 67 }; 68 69 struct amdgpu_acpi_dev_info { 70 struct list_head list; 71 struct list_head xcc_list; 72 uint32_t sbdf; 73 uint16_t supp_xcp_mode; 74 uint16_t xcp_mode; 75 uint16_t mem_mode; 76 uint64_t tmr_base; 77 uint64_t tmr_size; 78 }; 79 80 struct list_head amdgpu_acpi_dev_list; 81 82 struct amdgpu_atif_notification_cfg { 83 bool enabled; 84 int command_code; 85 }; 86 87 struct amdgpu_atif_notifications { 88 bool thermal_state; 89 bool forced_power_state; 90 bool system_power_state; 91 bool brightness_change; 92 bool dgpu_display_event; 93 bool gpu_package_power_limit; 94 }; 95 96 struct amdgpu_atif_functions { 97 bool system_params; 98 bool sbios_requests; 99 bool temperature_change; 100 bool query_backlight_transfer_characteristics; 101 bool ready_to_undock; 102 bool external_gpu_information; 103 }; 104 105 struct amdgpu_atif { 106 acpi_handle handle; 107 108 struct amdgpu_atif_notifications notifications; 109 struct amdgpu_atif_functions functions; 110 struct amdgpu_atif_notification_cfg notification_cfg; 111 struct backlight_device *bd; 112 struct amdgpu_dm_backlight_caps backlight_caps; 113 }; 114 115 struct amdgpu_atcs_functions { 116 bool get_ext_state; 117 bool pcie_perf_req; 118 bool pcie_dev_rdy; 119 bool pcie_bus_width; 120 bool get_uma_size; 121 bool power_shift_control; 122 bool set_uma_allocation_size; 123 }; 124 125 struct amdgpu_atcs { 126 acpi_handle handle; 127 128 struct amdgpu_atcs_functions functions; 129 }; 130 131 static struct amdgpu_acpi_priv { 132 struct amdgpu_atif atif; 133 struct amdgpu_atcs atcs; 134 } amdgpu_acpi_priv; 135 136 /* Call the ATIF method 137 */ 138 /** 139 * amdgpu_atif_call - call an ATIF method 140 * 141 * @atif: atif structure 142 * @function: the ATIF function to execute 143 * @params: ATIF function params 144 * @min_size: minimum size of the expected output buffer in bytes 145 * 146 * Executes the requested ATIF function (all asics). 147 * Returns a pointer to the acpi output buffer. 148 */ 149 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif, 150 int function, 151 struct acpi_buffer *params, 152 size_t min_size) 153 { 154 acpi_status status; 155 union acpi_object *obj; 156 union acpi_object atif_arg_elements[2]; 157 struct acpi_object_list atif_arg; 158 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 159 160 atif_arg.count = 2; 161 atif_arg.pointer = &atif_arg_elements[0]; 162 163 atif_arg_elements[0].type = ACPI_TYPE_INTEGER; 164 atif_arg_elements[0].integer.value = function; 165 166 if (params) { 167 atif_arg_elements[1].type = ACPI_TYPE_BUFFER; 168 atif_arg_elements[1].buffer.length = params->length; 169 atif_arg_elements[1].buffer.pointer = params->pointer; 170 } else { 171 /* We need a second fake parameter */ 172 atif_arg_elements[1].type = ACPI_TYPE_INTEGER; 173 atif_arg_elements[1].integer.value = 0; 174 } 175 176 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg, 177 &buffer); 178 obj = (union acpi_object *)buffer.pointer; 179 180 /* Fail if calling the method fails */ 181 if (ACPI_FAILURE(status)) { 182 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n", 183 acpi_format_exception(status)); 184 kfree(obj); 185 return NULL; 186 } 187 188 if (obj->type != ACPI_TYPE_BUFFER) { 189 DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n", 190 obj->type); 191 kfree(obj); 192 return NULL; 193 } 194 195 if (obj->buffer.length < sizeof(u16)) { 196 DRM_DEBUG_DRIVER("ATIF buffer too small to hold size field: %u\n", 197 obj->buffer.length); 198 kfree(obj); 199 return NULL; 200 } 201 202 if (obj->buffer.length < *(u16 *)obj->buffer.pointer) { 203 DRM_DEBUG_DRIVER("ATIF buffer length mismatch: reported %u, actual %u\n", 204 *(u16 *)obj->buffer.pointer, 205 obj->buffer.length); 206 kfree(obj); 207 return NULL; 208 } 209 210 if (*(u16 *)obj->buffer.pointer < min_size) { 211 DRM_DEBUG_DRIVER("ATIF buffer too small: expected %zu, got %u\n", 212 min_size, *(u16 *)obj->buffer.pointer); 213 kfree(obj); 214 return NULL; 215 } 216 217 return obj; 218 } 219 220 /** 221 * amdgpu_atif_parse_notification - parse supported notifications 222 * 223 * @n: supported notifications struct 224 * @mask: supported notifications mask from ATIF 225 * 226 * Use the supported notifications mask from ATIF function 227 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications 228 * are supported (all asics). 229 */ 230 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask) 231 { 232 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED; 233 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED; 234 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED; 235 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED; 236 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED; 237 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED; 238 } 239 240 /** 241 * amdgpu_atif_parse_functions - parse supported functions 242 * 243 * @f: supported functions struct 244 * @mask: supported functions mask from ATIF 245 * 246 * Use the supported functions mask from ATIF function 247 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions 248 * are supported (all asics). 249 */ 250 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask) 251 { 252 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED; 253 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED; 254 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED; 255 f->query_backlight_transfer_characteristics = 256 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED; 257 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED; 258 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED; 259 } 260 261 /** 262 * amdgpu_atif_verify_interface - verify ATIF 263 * 264 * @atif: amdgpu atif struct 265 * 266 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function 267 * to initialize ATIF and determine what features are supported 268 * (all asics). 269 * returns 0 on success, error on failure. 270 */ 271 static noinline_for_stack 272 int amdgpu_atif_verify_interface(struct amdgpu_atif *atif) 273 { 274 union acpi_object *info; 275 struct atif_verify_interface output; 276 size_t size; 277 int err = 0; 278 279 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL, 280 sizeof(output)); 281 if (!info) 282 return -EIO; 283 284 memset(&output, 0, sizeof(output)); 285 286 size = min(sizeof(output), (size_t)*(u16 *)info->buffer.pointer); 287 288 memcpy(&output, info->buffer.pointer, size); 289 290 /* TODO: check version? */ 291 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version); 292 293 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask); 294 amdgpu_atif_parse_functions(&atif->functions, output.function_bits); 295 296 kfree(info); 297 return err; 298 } 299 300 /** 301 * amdgpu_atif_get_notification_params - determine notify configuration 302 * 303 * @atif: acpi handle 304 * 305 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function 306 * to determine if a notifier is used and if so which one 307 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n) 308 * where n is specified in the result if a notifier is used. 309 * Returns 0 on success, error on failure. 310 */ 311 static noinline_for_stack 312 int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif) 313 { 314 union acpi_object *info; 315 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg; 316 struct atif_system_params params; 317 size_t size; 318 int err = 0; 319 320 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, 321 NULL, offsetof(struct atif_system_params, command_code)); 322 if (!info) { 323 err = -EIO; 324 goto out; 325 } 326 327 memset(¶ms, 0, sizeof(params)); 328 size = min(sizeof(params), (size_t)*(u16 *)info->buffer.pointer); 329 memcpy(¶ms, info->buffer.pointer, size); 330 331 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n", 332 params.flags, params.valid_mask); 333 params.flags = params.flags & params.valid_mask; 334 335 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) { 336 n->enabled = false; 337 n->command_code = 0; 338 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) { 339 n->enabled = true; 340 n->command_code = 0x81; 341 } else { 342 if (size < 11) { 343 err = -EINVAL; 344 goto out; 345 } 346 n->enabled = true; 347 n->command_code = params.command_code; 348 } 349 350 out: 351 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n", 352 (n->enabled ? "enabled" : "disabled"), 353 n->command_code); 354 kfree(info); 355 return err; 356 } 357 358 /** 359 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal 360 * 361 * @atif: acpi handle 362 * 363 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function 364 * to determine the acceptable range of backlight values 365 * 366 * Backlight_caps.caps_valid will be set to true if the query is successful 367 * 368 * The input signals are in range 0-255 369 * 370 * This function assumes the display with backlight is the first LCD 371 * 372 * Returns 0 on success, error on failure. 373 */ 374 static noinline_for_stack 375 int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif) 376 { 377 union acpi_object *info; 378 struct atif_qbtc_output characteristics; 379 struct atif_qbtc_arguments arguments; 380 struct acpi_buffer params; 381 size_t size; 382 int err = 0; 383 384 arguments.size = sizeof(arguments); 385 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1; 386 387 params.length = sizeof(arguments); 388 params.pointer = (void *)&arguments; 389 390 info = amdgpu_atif_call(atif, 391 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS, 392 ¶ms, offsetof(struct atif_qbtc_output, data_points)); 393 if (!info) { 394 err = -EIO; 395 goto out; 396 } 397 398 memset(&characteristics, 0, sizeof(characteristics)); 399 size = min(sizeof(characteristics), (size_t)*(u16 *)info->buffer.pointer); 400 memcpy(&characteristics, info->buffer.pointer, size); 401 402 atif->backlight_caps.caps_valid = true; 403 atif->backlight_caps.min_input_signal = 404 characteristics.min_input_signal; 405 atif->backlight_caps.max_input_signal = 406 characteristics.max_input_signal; 407 atif->backlight_caps.ac_level = characteristics.ac_level; 408 atif->backlight_caps.dc_level = characteristics.dc_level; 409 atif->backlight_caps.data_points = characteristics.number_of_points; 410 memcpy(atif->backlight_caps.luminance_data, 411 characteristics.data_points, 412 sizeof(atif->backlight_caps.luminance_data)); 413 out: 414 kfree(info); 415 return err; 416 } 417 418 /** 419 * amdgpu_atif_get_sbios_requests - get requested sbios event 420 * 421 * @atif: acpi handle 422 * @req: atif sbios request struct 423 * 424 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function 425 * to determine what requests the sbios is making to the driver 426 * (all asics). 427 * Returns 0 on success, error on failure. 428 */ 429 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif, 430 struct atif_sbios_requests *req) 431 { 432 union acpi_object *info; 433 size_t size; 434 int count = 0; 435 436 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, 437 NULL, sizeof(*req)); 438 if (!info) 439 return -EIO; 440 441 memset(req, 0, sizeof(*req)); 442 443 size = min(sizeof(*req), (size_t)*(u16 *)info->buffer.pointer); 444 memcpy(req, info->buffer.pointer, size); 445 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending); 446 447 count = hweight32(req->pending); 448 449 kfree(info); 450 return count; 451 } 452 453 /** 454 * amdgpu_atif_handler - handle ATIF notify requests 455 * 456 * @adev: amdgpu_device pointer 457 * @event: atif sbios request struct 458 * 459 * Checks the acpi event and if it matches an atif event, 460 * handles it. 461 * 462 * Returns: 463 * NOTIFY_BAD or NOTIFY_DONE, depending on the event. 464 */ 465 static int amdgpu_atif_handler(struct amdgpu_device *adev, 466 struct acpi_bus_event *event) 467 { 468 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 469 int count; 470 471 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n", 472 event->device_class, event->type); 473 474 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0) 475 return NOTIFY_DONE; 476 477 /* Is this actually our event? */ 478 if (!atif->notification_cfg.enabled || 479 event->type != atif->notification_cfg.command_code) { 480 /* These events will generate keypresses otherwise */ 481 if (event->type == ACPI_VIDEO_NOTIFY_PROBE) 482 return NOTIFY_BAD; 483 else 484 return NOTIFY_DONE; 485 } 486 487 if (atif->functions.sbios_requests) { 488 struct atif_sbios_requests req; 489 490 /* Check pending SBIOS requests */ 491 count = amdgpu_atif_get_sbios_requests(atif, &req); 492 493 if (count <= 0) 494 return NOTIFY_BAD; 495 496 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count); 497 498 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) { 499 if (atif->bd) { 500 DRM_DEBUG_DRIVER("Changing brightness to %d\n", 501 req.backlight_level); 502 /* 503 * XXX backlight_device_set_brightness() is 504 * hardwired to post BACKLIGHT_UPDATE_SYSFS. 505 * It probably should accept 'reason' parameter. 506 */ 507 backlight_device_set_brightness(atif->bd, req.backlight_level); 508 } 509 } 510 511 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) { 512 if (adev->flags & AMD_IS_PX) { 513 pm_runtime_get_sync(adev_to_drm(adev)->dev); 514 /* Just fire off a uevent and let userspace tell us what to do */ 515 drm_helper_hpd_irq_event(adev_to_drm(adev)); 516 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 517 } 518 } 519 /* TODO: check other events */ 520 } 521 522 /* We've handled the event, stop the notifier chain. The ACPI interface 523 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to 524 * userspace if the event was generated only to signal a SBIOS 525 * request. 526 */ 527 return NOTIFY_BAD; 528 } 529 530 /* Call the ATCS method 531 */ 532 /** 533 * amdgpu_atcs_call - call an ATCS method 534 * 535 * @atcs: atcs structure 536 * @function: the ATCS function to execute 537 * @params: ATCS function params 538 * 539 * Executes the requested ATCS function (all asics). 540 * Returns a pointer to the acpi output buffer. 541 */ 542 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs, 543 int function, 544 struct acpi_buffer *params) 545 { 546 acpi_status status; 547 union acpi_object atcs_arg_elements[2]; 548 struct acpi_object_list atcs_arg; 549 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 550 551 atcs_arg.count = 2; 552 atcs_arg.pointer = &atcs_arg_elements[0]; 553 554 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER; 555 atcs_arg_elements[0].integer.value = function; 556 557 if (params) { 558 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER; 559 atcs_arg_elements[1].buffer.length = params->length; 560 atcs_arg_elements[1].buffer.pointer = params->pointer; 561 } else { 562 /* We need a second fake parameter */ 563 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER; 564 atcs_arg_elements[1].integer.value = 0; 565 } 566 567 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer); 568 569 /* Fail only if calling the method fails and ATIF is supported */ 570 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 571 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n", 572 acpi_format_exception(status)); 573 kfree(buffer.pointer); 574 return NULL; 575 } 576 577 return buffer.pointer; 578 } 579 580 /** 581 * amdgpu_atcs_parse_functions - parse supported functions 582 * 583 * @f: supported functions struct 584 * @mask: supported functions mask from ATCS 585 * 586 * Use the supported functions mask from ATCS function 587 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions 588 * are supported (all asics). 589 */ 590 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask) 591 { 592 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED; 593 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED; 594 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED; 595 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED; 596 f->get_uma_size = mask & ACPI_ATCS_GET_UMA_SIZE_SUPPORTED; 597 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED; 598 f->set_uma_allocation_size = mask & ACPI_ATCS_SET_UMA_ALLOCATION_SIZE_SUPPORTED; 599 } 600 601 /** 602 * amdgpu_atcs_verify_interface - verify ATCS 603 * 604 * @atcs: amdgpu atcs struct 605 * 606 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function 607 * to initialize ATCS and determine what features are supported 608 * (all asics). 609 * returns 0 on success, error on failure. 610 */ 611 static noinline_for_stack 612 int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs) 613 { 614 union acpi_object *info; 615 struct atcs_verify_interface output; 616 size_t size; 617 int err = 0; 618 619 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL); 620 if (!info) 621 return -EIO; 622 623 memset(&output, 0, sizeof(output)); 624 625 size = *(u16 *) info->buffer.pointer; 626 if (size < 8) { 627 DRM_INFO("ATCS buffer is too small: %zu\n", size); 628 err = -EINVAL; 629 goto out; 630 } 631 size = min(sizeof(output), size); 632 633 memcpy(&output, info->buffer.pointer, size); 634 635 /* TODO: check version? */ 636 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version); 637 638 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits); 639 640 out: 641 kfree(info); 642 return err; 643 } 644 645 /** 646 * amdgpu_acpi_is_pcie_performance_request_supported 647 * 648 * @adev: amdgpu_device pointer 649 * 650 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods 651 * are supported (all asics). 652 * returns true if supported, false if not. 653 */ 654 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev) 655 { 656 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 657 658 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy) 659 return true; 660 661 return false; 662 } 663 664 /** 665 * amdgpu_acpi_is_power_shift_control_supported 666 * 667 * Check if the ATCS power shift control method 668 * is supported. 669 * returns true if supported, false if not. 670 */ 671 bool amdgpu_acpi_is_power_shift_control_supported(void) 672 { 673 return amdgpu_acpi_priv.atcs.functions.power_shift_control; 674 } 675 676 bool amdgpu_acpi_is_set_uma_allocation_size_supported(void) 677 { 678 return amdgpu_acpi_priv.atcs.functions.set_uma_allocation_size; 679 } 680 681 /** 682 * amdgpu_acpi_pcie_notify_device_ready 683 * 684 * @adev: amdgpu_device pointer 685 * 686 * Executes the PCIE_DEVICE_READY_NOTIFICATION method 687 * (all asics). 688 * returns 0 on success, error on failure. 689 */ 690 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev) 691 { 692 union acpi_object *info; 693 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 694 695 if (!atcs->functions.pcie_dev_rdy) 696 return -EINVAL; 697 698 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL); 699 if (!info) 700 return -EIO; 701 702 kfree(info); 703 704 return 0; 705 } 706 707 /** 708 * amdgpu_acpi_pcie_performance_request 709 * 710 * @adev: amdgpu_device pointer 711 * @perf_req: requested perf level (pcie gen speed) 712 * @advertise: set advertise caps flag if set 713 * 714 * Executes the PCIE_PERFORMANCE_REQUEST method to 715 * change the pcie gen speed (all asics). 716 * returns 0 on success, error on failure. 717 */ 718 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev, 719 u8 perf_req, bool advertise) 720 { 721 union acpi_object *info; 722 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 723 struct atcs_pref_req_input atcs_input; 724 struct atcs_pref_req_output atcs_output; 725 struct acpi_buffer params; 726 size_t size; 727 u32 retry = 3; 728 729 if (amdgpu_acpi_pcie_notify_device_ready(adev)) 730 return -EINVAL; 731 732 if (!atcs->functions.pcie_perf_req) 733 return -EINVAL; 734 735 atcs_input.size = sizeof(struct atcs_pref_req_input); 736 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */ 737 atcs_input.client_id = pci_dev_id(adev->pdev); 738 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK; 739 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION; 740 if (advertise) 741 atcs_input.flags |= ATCS_ADVERTISE_CAPS; 742 atcs_input.req_type = ATCS_PCIE_LINK_SPEED; 743 atcs_input.perf_req = perf_req; 744 745 params.length = sizeof(struct atcs_pref_req_input); 746 params.pointer = &atcs_input; 747 748 while (retry--) { 749 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms); 750 if (!info) 751 return -EIO; 752 753 memset(&atcs_output, 0, sizeof(atcs_output)); 754 755 size = *(u16 *) info->buffer.pointer; 756 if (size < 3) { 757 drm_info(adev_to_drm(adev), 758 "ATCS buffer is too small: %zu\n", size); 759 kfree(info); 760 return -EINVAL; 761 } 762 size = min(sizeof(atcs_output), size); 763 764 memcpy(&atcs_output, info->buffer.pointer, size); 765 766 kfree(info); 767 768 switch (atcs_output.ret_val) { 769 case ATCS_REQUEST_REFUSED: 770 default: 771 return -EINVAL; 772 case ATCS_REQUEST_COMPLETE: 773 return 0; 774 case ATCS_REQUEST_IN_PROGRESS: 775 udelay(10); 776 break; 777 } 778 } 779 780 return 0; 781 } 782 783 /** 784 * amdgpu_acpi_power_shift_control 785 * 786 * @adev: amdgpu_device pointer 787 * @dev_state: device acpi state 788 * @drv_state: driver state 789 * 790 * Executes the POWER_SHIFT_CONTROL method to 791 * communicate current dGPU device state and 792 * driver state to APU/SBIOS. 793 * returns 0 on success, error on failure. 794 */ 795 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev, 796 u8 dev_state, bool drv_state) 797 { 798 union acpi_object *info; 799 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 800 struct atcs_pwr_shift_input atcs_input; 801 struct acpi_buffer params; 802 803 if (!amdgpu_acpi_is_power_shift_control_supported()) 804 return -EINVAL; 805 806 atcs_input.size = sizeof(struct atcs_pwr_shift_input); 807 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */ 808 atcs_input.dgpu_id = pci_dev_id(adev->pdev); 809 atcs_input.dev_acpi_state = dev_state; 810 atcs_input.drv_state = drv_state; 811 812 params.length = sizeof(struct atcs_pwr_shift_input); 813 params.pointer = &atcs_input; 814 815 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms); 816 if (!info) { 817 drm_err(adev_to_drm(adev), "ATCS PSC call failed\n"); 818 return -EIO; 819 } 820 821 kfree(info); 822 return 0; 823 } 824 825 /** 826 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS 827 * 828 * @adev: amdgpu device pointer 829 * @ss_state: current smart shift event 830 * 831 * returns 0 on success, 832 * otherwise return error number. 833 */ 834 int amdgpu_acpi_smart_shift_update(struct amdgpu_device *adev, 835 enum amdgpu_ss ss_state) 836 { 837 int r; 838 839 if (!amdgpu_device_supports_smart_shift(adev)) 840 return 0; 841 842 switch (ss_state) { 843 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational. 844 * SBIOS trigger “stop” at D3, Driver Not Operational. 845 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational. 846 */ 847 case AMDGPU_SS_DRV_LOAD: 848 r = amdgpu_acpi_power_shift_control(adev, 849 AMDGPU_ATCS_PSC_DEV_STATE_D0, 850 AMDGPU_ATCS_PSC_DRV_STATE_OPR); 851 break; 852 case AMDGPU_SS_DEV_D0: 853 r = amdgpu_acpi_power_shift_control(adev, 854 AMDGPU_ATCS_PSC_DEV_STATE_D0, 855 AMDGPU_ATCS_PSC_DRV_STATE_OPR); 856 break; 857 case AMDGPU_SS_DEV_D3: 858 r = amdgpu_acpi_power_shift_control(adev, 859 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT, 860 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR); 861 break; 862 case AMDGPU_SS_DRV_UNLOAD: 863 r = amdgpu_acpi_power_shift_control(adev, 864 AMDGPU_ATCS_PSC_DEV_STATE_D0, 865 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR); 866 break; 867 default: 868 return -EINVAL; 869 } 870 871 return r; 872 } 873 874 #ifdef CONFIG_ACPI_NUMA 875 static inline uint64_t amdgpu_acpi_get_numa_size(int nid) 876 { 877 /* This is directly using si_meminfo_node implementation as the 878 * function is not exported. 879 */ 880 int zone_type; 881 uint64_t managed_pages = 0; 882 883 pg_data_t *pgdat = NODE_DATA(nid); 884 885 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) 886 managed_pages += 887 zone_managed_pages(&pgdat->node_zones[zone_type]); 888 return managed_pages * PAGE_SIZE; 889 } 890 891 static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm) 892 { 893 struct amdgpu_numa_info *numa_info; 894 int nid; 895 896 numa_info = xa_load(&numa_info_xa, pxm); 897 898 if (!numa_info) { 899 struct sysinfo info; 900 901 numa_info = kzalloc_obj(*numa_info); 902 if (!numa_info) 903 return NULL; 904 905 nid = pxm_to_node(pxm); 906 numa_info->pxm = pxm; 907 numa_info->nid = nid; 908 909 if (numa_info->nid == NUMA_NO_NODE) { 910 si_meminfo(&info); 911 numa_info->size = info.totalram * info.mem_unit; 912 } else { 913 numa_info->size = amdgpu_acpi_get_numa_size(nid); 914 } 915 xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL); 916 } 917 918 return numa_info; 919 } 920 #endif 921 922 /** 923 * amdgpu_acpi_set_uma_allocation_size - Set Unified Memory Architecture allocation size via ACPI 924 * @adev: Pointer to the amdgpu_device structure 925 * @index: Index specifying the UMA allocation 926 * @type: Type of UMA allocation 927 * 928 * This function configures the UMA allocation size for the specified device 929 * using ACPI methods. The allocation is determined by the provided index and type. 930 * Returns 0 on success or a negative error code on failure. 931 */ 932 int amdgpu_acpi_set_uma_allocation_size(struct amdgpu_device *adev, u8 index, u8 type) 933 { 934 struct atcs_set_uma_allocation_size_input atcs_input; 935 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 936 struct acpi_buffer params; 937 union acpi_object *info; 938 939 if (!amdgpu_acpi_is_set_uma_allocation_size_supported()) 940 return -EINVAL; 941 942 atcs_input.size = sizeof(struct atcs_set_uma_allocation_size_input); 943 atcs_input.uma_size_index = index; 944 atcs_input.uma_size_type = type; 945 946 params.length = sizeof(struct atcs_set_uma_allocation_size_input); 947 params.pointer = &atcs_input; 948 949 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_SET_UMA_ALLOCATION_SIZE, ¶ms); 950 if (!info) { 951 drm_err(adev_to_drm(adev), "ATCS UMA allocation size update failed\n"); 952 return -EIO; 953 } 954 955 kfree(info); 956 957 return 0; 958 } 959 960 /** 961 * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu 962 * acpi device handle 963 * 964 * @handle: acpi handle 965 * @numa_info: amdgpu_numa_info structure holding numa information 966 * 967 * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a 968 * given amdgpu acpi device. 969 * 970 * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason 971 */ 972 static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle, 973 struct amdgpu_numa_info **numa_info) 974 { 975 #ifdef CONFIG_ACPI_NUMA 976 u64 pxm; 977 acpi_status status; 978 979 if (!numa_info) 980 return_ACPI_STATUS(AE_ERROR); 981 982 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm); 983 984 if (ACPI_FAILURE(status)) 985 return status; 986 987 *numa_info = amdgpu_acpi_get_numa_info(pxm); 988 989 if (!*numa_info) 990 return_ACPI_STATUS(AE_ERROR); 991 992 return_ACPI_STATUS(AE_OK); 993 #else 994 return_ACPI_STATUS(AE_NOT_EXIST); 995 #endif 996 } 997 998 static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf) 999 { 1000 struct amdgpu_acpi_dev_info *acpi_dev; 1001 1002 if (list_empty(&amdgpu_acpi_dev_list)) 1003 return NULL; 1004 1005 list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list) 1006 if (acpi_dev->sbdf == sbdf) 1007 return acpi_dev; 1008 1009 return NULL; 1010 } 1011 1012 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info, 1013 struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf) 1014 { 1015 struct amdgpu_acpi_dev_info *tmp; 1016 union acpi_object *obj; 1017 int ret = -ENOENT; 1018 1019 *dev_info = NULL; 1020 tmp = kzalloc_obj(struct amdgpu_acpi_dev_info); 1021 if (!tmp) 1022 return -ENOMEM; 1023 1024 INIT_LIST_HEAD(&tmp->xcc_list); 1025 INIT_LIST_HEAD(&tmp->list); 1026 tmp->sbdf = sbdf; 1027 1028 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0, 1029 AMD_XCC_DSM_GET_SUPP_MODE, NULL, 1030 ACPI_TYPE_INTEGER); 1031 1032 if (!obj) { 1033 acpi_handle_debug(xcc_info->handle, 1034 "_DSM function %d evaluation failed", 1035 AMD_XCC_DSM_GET_SUPP_MODE); 1036 ret = -ENOENT; 1037 goto out; 1038 } 1039 1040 tmp->supp_xcp_mode = obj->integer.value & 0xFFFF; 1041 ACPI_FREE(obj); 1042 1043 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0, 1044 AMD_XCC_DSM_GET_XCP_MODE, NULL, 1045 ACPI_TYPE_INTEGER); 1046 1047 if (!obj) { 1048 acpi_handle_debug(xcc_info->handle, 1049 "_DSM function %d evaluation failed", 1050 AMD_XCC_DSM_GET_XCP_MODE); 1051 ret = -ENOENT; 1052 goto out; 1053 } 1054 1055 tmp->xcp_mode = obj->integer.value & 0xFFFF; 1056 tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF; 1057 ACPI_FREE(obj); 1058 1059 /* Evaluate DSMs and fill XCC information */ 1060 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0, 1061 AMD_XCC_DSM_GET_TMR_INFO, NULL, 1062 ACPI_TYPE_PACKAGE); 1063 1064 if (!obj || obj->package.count < 2) { 1065 acpi_handle_debug(xcc_info->handle, 1066 "_DSM function %d evaluation failed", 1067 AMD_XCC_DSM_GET_TMR_INFO); 1068 ret = -ENOENT; 1069 goto out; 1070 } 1071 1072 tmp->tmr_base = obj->package.elements[0].integer.value; 1073 tmp->tmr_size = obj->package.elements[1].integer.value; 1074 ACPI_FREE(obj); 1075 1076 DRM_DEBUG_DRIVER( 1077 "New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx ", 1078 tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode, 1079 tmp->tmr_base, tmp->tmr_size); 1080 list_add_tail(&tmp->list, &amdgpu_acpi_dev_list); 1081 *dev_info = tmp; 1082 1083 return 0; 1084 1085 out: 1086 if (obj) 1087 ACPI_FREE(obj); 1088 kfree(tmp); 1089 1090 return ret; 1091 } 1092 1093 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info, 1094 u32 *sbdf) 1095 { 1096 union acpi_object *obj; 1097 acpi_status status; 1098 int ret = -ENOENT; 1099 1100 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0, 1101 AMD_XCC_DSM_GET_NUM_FUNCS, NULL, 1102 ACPI_TYPE_INTEGER); 1103 1104 if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS) 1105 goto out; 1106 ACPI_FREE(obj); 1107 1108 /* Evaluate DSMs and fill XCC information */ 1109 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0, 1110 AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL, 1111 ACPI_TYPE_INTEGER); 1112 1113 if (!obj) { 1114 acpi_handle_debug(xcc_info->handle, 1115 "_DSM function %d evaluation failed", 1116 AMD_XCC_DSM_GET_VF_XCC_MAPPING); 1117 ret = -EINVAL; 1118 goto out; 1119 } 1120 1121 /* PF xcc id [39:32] */ 1122 xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF; 1123 /* xcp node of this xcc [47:40] */ 1124 xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF; 1125 /* PF domain of this xcc [31:16] */ 1126 *sbdf = (obj->integer.value) & 0xFFFF0000; 1127 /* PF bus/dev/fn of this xcc [63:48] */ 1128 *sbdf |= (obj->integer.value >> 48) & 0xFFFF; 1129 ACPI_FREE(obj); 1130 obj = NULL; 1131 1132 status = 1133 amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info); 1134 1135 /* TODO: check if this check is required */ 1136 if (ACPI_SUCCESS(status)) 1137 ret = 0; 1138 out: 1139 if (obj) 1140 ACPI_FREE(obj); 1141 1142 return ret; 1143 } 1144 1145 static noinline_for_stack 1146 int amdgpu_acpi_enumerate_xcc(void) 1147 { 1148 struct amdgpu_acpi_dev_info *dev_info = NULL; 1149 struct amdgpu_acpi_xcc_info *xcc_info; 1150 struct acpi_device *acpi_dev; 1151 char hid[ACPI_ID_LEN]; 1152 int ret, id; 1153 u32 sbdf; 1154 1155 INIT_LIST_HEAD(&amdgpu_acpi_dev_list); 1156 xa_init(&numa_info_xa); 1157 1158 for (id = 0; id < AMD_XCC_MAX_HID; id++) { 1159 sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id); 1160 acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1); 1161 /* These ACPI objects are expected to be in sequential order. If 1162 * one is not found, no need to check the rest. 1163 */ 1164 if (!acpi_dev) { 1165 DRM_DEBUG_DRIVER("No matching acpi device found for %s\n", 1166 hid); 1167 break; 1168 } 1169 1170 xcc_info = kzalloc_obj(struct amdgpu_acpi_xcc_info); 1171 if (!xcc_info) { 1172 acpi_dev_put(acpi_dev); 1173 return -ENOMEM; 1174 } 1175 1176 INIT_LIST_HEAD(&xcc_info->list); 1177 xcc_info->handle = acpi_device_handle(acpi_dev); 1178 acpi_dev_put(acpi_dev); 1179 1180 ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf); 1181 if (ret) { 1182 kfree(xcc_info); 1183 continue; 1184 } 1185 1186 dev_info = amdgpu_acpi_get_dev(sbdf); 1187 1188 if (!dev_info) 1189 ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf); 1190 1191 if (ret == -ENOMEM) { 1192 kfree(xcc_info); 1193 return ret; 1194 } 1195 1196 if (!dev_info) { 1197 kfree(xcc_info); 1198 continue; 1199 } 1200 1201 list_add_tail(&xcc_info->list, &dev_info->xcc_list); 1202 } 1203 1204 return 0; 1205 } 1206 1207 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset, 1208 u64 *tmr_size) 1209 { 1210 struct amdgpu_acpi_dev_info *dev_info; 1211 u32 sbdf; 1212 1213 if (!tmr_offset || !tmr_size) 1214 return -EINVAL; 1215 1216 sbdf = (pci_domain_nr(adev->pdev->bus) << 16); 1217 sbdf |= pci_dev_id(adev->pdev); 1218 dev_info = amdgpu_acpi_get_dev(sbdf); 1219 if (!dev_info) 1220 return -ENOENT; 1221 1222 *tmr_offset = dev_info->tmr_base; 1223 *tmr_size = dev_info->tmr_size; 1224 1225 return 0; 1226 } 1227 1228 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id, 1229 struct amdgpu_numa_info *numa_info) 1230 { 1231 struct amdgpu_acpi_dev_info *dev_info; 1232 struct amdgpu_acpi_xcc_info *xcc_info; 1233 u32 sbdf; 1234 1235 if (!numa_info) 1236 return -EINVAL; 1237 1238 sbdf = (pci_domain_nr(adev->pdev->bus) << 16); 1239 sbdf |= pci_dev_id(adev->pdev); 1240 dev_info = amdgpu_acpi_get_dev(sbdf); 1241 if (!dev_info) 1242 return -ENOENT; 1243 1244 list_for_each_entry(xcc_info, &dev_info->xcc_list, list) { 1245 if (xcc_info->phy_id == xcc_id) { 1246 memcpy(numa_info, xcc_info->numa_info, 1247 sizeof(*numa_info)); 1248 return 0; 1249 } 1250 } 1251 1252 return -ENOENT; 1253 } 1254 1255 /** 1256 * amdgpu_acpi_event - handle notify events 1257 * 1258 * @nb: notifier block 1259 * @val: val 1260 * @data: acpi event 1261 * 1262 * Calls relevant amdgpu functions in response to various 1263 * acpi events. 1264 * Returns NOTIFY code 1265 */ 1266 static int amdgpu_acpi_event(struct notifier_block *nb, 1267 unsigned long val, 1268 void *data) 1269 { 1270 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb); 1271 struct acpi_bus_event *entry = (struct acpi_bus_event *)data; 1272 1273 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) { 1274 if (power_supply_is_system_supplied() > 0) 1275 DRM_DEBUG_DRIVER("pm: AC\n"); 1276 else 1277 DRM_DEBUG_DRIVER("pm: DC\n"); 1278 1279 amdgpu_pm_acpi_event_handler(adev); 1280 } 1281 1282 /* Check for pending SBIOS requests */ 1283 return amdgpu_atif_handler(adev, entry); 1284 } 1285 1286 /* Call all ACPI methods here */ 1287 /** 1288 * amdgpu_acpi_init - init driver acpi support 1289 * 1290 * @adev: amdgpu_device pointer 1291 * 1292 * Verifies the AMD ACPI interfaces and registers with the acpi 1293 * notifier chain (all asics). 1294 * Returns 0 on success, error on failure. 1295 */ 1296 int amdgpu_acpi_init(struct amdgpu_device *adev) 1297 { 1298 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 1299 1300 if (atif->notifications.brightness_change) { 1301 if (adev->dc_enabled) { 1302 #if defined(CONFIG_DRM_AMD_DC) 1303 struct amdgpu_display_manager *dm = &adev->dm; 1304 1305 if (dm->backlight_dev[0]) 1306 atif->bd = dm->backlight_dev[0]; 1307 #endif 1308 } else { 1309 struct drm_encoder *tmp; 1310 1311 /* Find the encoder controlling the brightness */ 1312 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list, 1313 head) { 1314 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp); 1315 1316 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) && 1317 enc->enc_priv) { 1318 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv; 1319 1320 if (dig->bl_dev) { 1321 atif->bd = dig->bl_dev; 1322 break; 1323 } 1324 } 1325 } 1326 } 1327 } 1328 adev->acpi_nb.notifier_call = amdgpu_acpi_event; 1329 register_acpi_notifier(&adev->acpi_nb); 1330 1331 return 0; 1332 } 1333 1334 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps) 1335 { 1336 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 1337 1338 memcpy(caps, &atif->backlight_caps, sizeof(*caps)); 1339 } 1340 1341 /** 1342 * amdgpu_acpi_fini - tear down driver acpi support 1343 * 1344 * @adev: amdgpu_device pointer 1345 * 1346 * Unregisters with the acpi notifier chain (all asics). 1347 */ 1348 void amdgpu_acpi_fini(struct amdgpu_device *adev) 1349 { 1350 unregister_acpi_notifier(&adev->acpi_nb); 1351 } 1352 1353 /** 1354 * amdgpu_atif_pci_probe_handle - look up the ATIF handle 1355 * 1356 * @pdev: pci device 1357 * 1358 * Look up the ATIF handles (all asics). 1359 * Returns true if the handle is found, false if not. 1360 */ 1361 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev) 1362 { 1363 char acpi_method_name[255] = { 0 }; 1364 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; 1365 acpi_handle dhandle, atif_handle; 1366 acpi_status status; 1367 int ret; 1368 1369 dhandle = ACPI_HANDLE(&pdev->dev); 1370 if (!dhandle) 1371 return false; 1372 1373 status = acpi_get_handle(dhandle, "ATIF", &atif_handle); 1374 if (ACPI_FAILURE(status)) 1375 return false; 1376 1377 amdgpu_acpi_priv.atif.handle = atif_handle; 1378 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer); 1379 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name); 1380 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif); 1381 if (ret) { 1382 amdgpu_acpi_priv.atif.handle = 0; 1383 return false; 1384 } 1385 return true; 1386 } 1387 1388 /** 1389 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle 1390 * 1391 * @pdev: pci device 1392 * 1393 * Look up the ATCS handles (all asics). 1394 * Returns true if the handle is found, false if not. 1395 */ 1396 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev) 1397 { 1398 char acpi_method_name[255] = { 0 }; 1399 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name }; 1400 acpi_handle dhandle, atcs_handle; 1401 acpi_status status; 1402 int ret; 1403 1404 dhandle = ACPI_HANDLE(&pdev->dev); 1405 if (!dhandle) 1406 return false; 1407 1408 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle); 1409 if (ACPI_FAILURE(status)) 1410 return false; 1411 1412 amdgpu_acpi_priv.atcs.handle = atcs_handle; 1413 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer); 1414 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name); 1415 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs); 1416 if (ret) { 1417 amdgpu_acpi_priv.atcs.handle = 0; 1418 return false; 1419 } 1420 return true; 1421 } 1422 1423 1424 /** 1425 * amdgpu_acpi_should_gpu_reset 1426 * 1427 * @adev: amdgpu_device_pointer 1428 * 1429 * returns true if should reset GPU, false if not 1430 */ 1431 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev) 1432 { 1433 if ((adev->flags & AMD_IS_APU) && 1434 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */ 1435 return false; 1436 1437 if ((adev->flags & AMD_IS_APU) && 1438 amdgpu_acpi_is_s3_active(adev)) 1439 return false; 1440 1441 if (amdgpu_sriov_vf(adev)) 1442 return false; 1443 1444 #if IS_ENABLED(CONFIG_SUSPEND) 1445 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE; 1446 #else 1447 return true; 1448 #endif 1449 } 1450 1451 #ifdef HAVE_ACPI_SRAT_GENERIC_AFFINITY 1452 static bool amdgpu_acpi_srat_pci_handle_match(struct pci_dev *pdev, 1453 struct acpi_srat_generic_affinity *gi) 1454 { 1455 u16 segment = *(u16 *)&gi->device_handle[0]; 1456 u8 bus = gi->device_handle[2]; 1457 u8 devfn = gi->device_handle[3]; 1458 1459 /* Only PCI device handles (ACPI 6.3: type 1; some firmware used 0) */ 1460 if (gi->device_handle_type != 0 && gi->device_handle_type != 1) 1461 return false; 1462 1463 if (!(gi->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED)) 1464 return false; 1465 1466 /* ACPI 6.3 Table 5-80: segment + bus + devfn bytes */ 1467 return segment == pci_domain_nr(pdev->bus) && 1468 bus == pdev->bus->number && 1469 devfn == pdev->devfn; 1470 } 1471 1472 int amdgpu_acpi_find_gpu_memory_in_srat(struct amdgpu_device *adev, 1473 uint64_t *base_addr, uint64_t *length) 1474 { 1475 struct acpi_srat_generic_affinity *gpu; 1476 struct acpi_subtable_header *sub_header; 1477 struct acpi_table_header *table_header; 1478 struct acpi_srat_mem_affinity *mem; 1479 struct pci_dev *pdev = adev->pdev; 1480 unsigned long subtable_len; 1481 unsigned long table_end; 1482 acpi_status status; 1483 bool pxm_found = false; 1484 bool mem_found = false; 1485 u64 mem_base = 0; 1486 u64 mem_len = 0; 1487 u32 gpu_pxm = 0; 1488 1489 if (!base_addr || !length) 1490 return -EINVAL; 1491 1492 status = acpi_get_table(ACPI_SIG_SRAT, 0, &table_header); 1493 if (ACPI_FAILURE(status)) 1494 return -ENOENT; 1495 1496 table_end = (unsigned long)table_header + table_header->length; 1497 1498 /* Pass 1: find GPU PXM from GENERIC_AFFINITY */ 1499 sub_header = (struct acpi_subtable_header *) 1500 ((unsigned long)table_header + sizeof(struct acpi_table_srat)); 1501 subtable_len = sub_header->length; 1502 1503 while (((unsigned long)sub_header) + subtable_len <= table_end) { 1504 if (subtable_len == 0) 1505 break; 1506 1507 if (sub_header->type == ACPI_SRAT_TYPE_GENERIC_AFFINITY) { 1508 gpu = (struct acpi_srat_generic_affinity *)sub_header; 1509 if (amdgpu_acpi_srat_pci_handle_match(pdev, gpu)) { 1510 gpu_pxm = gpu->proximity_domain; 1511 pxm_found = true; 1512 break; 1513 } 1514 } 1515 1516 sub_header = (struct acpi_subtable_header *) 1517 ((unsigned long)sub_header + subtable_len); 1518 subtable_len = sub_header->length; 1519 } 1520 1521 if (!pxm_found) { 1522 acpi_put_table(table_header); 1523 dev_err(adev->dev, "SRAT: no acpi generic initiator found\n"); 1524 return -ENOENT; 1525 } 1526 1527 /* Pass 2: find SPM memory for gpu_pxm */ 1528 sub_header = (struct acpi_subtable_header *) 1529 ((unsigned long)table_header + sizeof(struct acpi_table_srat)); 1530 subtable_len = sub_header->length; 1531 1532 while (((unsigned long)sub_header) + subtable_len <= table_end) { 1533 if (subtable_len == 0) 1534 break; 1535 1536 if (sub_header->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) { 1537 bool enabled, hotplug; 1538 1539 mem = (struct acpi_srat_mem_affinity *)sub_header; 1540 enabled = mem->flags & ACPI_SRAT_MEM_ENABLED; 1541 hotplug = mem->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE; 1542 1543 /* MUST filter ENABLED && !HOTPLUGGABLE */ 1544 if (enabled && !hotplug && 1545 mem->proximity_domain == gpu_pxm) { 1546 /* [mem->base_address, mem->length] is the SPM range */ 1547 mem_base = mem->base_address; 1548 mem_len = mem->length; 1549 mem_found = true; 1550 break; 1551 } 1552 } 1553 1554 sub_header = (struct acpi_subtable_header *) 1555 ((unsigned long)sub_header + subtable_len); 1556 subtable_len = sub_header->length; 1557 } 1558 1559 acpi_put_table(table_header); 1560 1561 if (!mem_found) { 1562 dev_err(adev->dev, "SRAT: no memory affinity for pxm %u\n", 1563 gpu_pxm); 1564 return -ENOENT; 1565 } 1566 1567 dev_info(adev->dev, "SRAT: gpu pxm=%u base=%llx length=%llx\n", 1568 gpu_pxm, mem_base, mem_len); 1569 1570 *base_addr = mem_base; 1571 *length = mem_len; 1572 1573 return 0; 1574 } 1575 #else 1576 int amdgpu_acpi_find_gpu_memory_in_srat(struct amdgpu_device *adev, 1577 uint64_t *base_addr, uint64_t *length) 1578 { 1579 if (!base_addr || !length) 1580 return -EINVAL; 1581 1582 return -ENOENT; 1583 } 1584 #endif 1585 1586 /* 1587 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods 1588 * 1589 * Check if we have the ATIF/ATCS methods and populate 1590 * the structures in the driver. 1591 */ 1592 void amdgpu_acpi_detect(void) 1593 { 1594 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif; 1595 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs; 1596 struct pci_dev *pdev = NULL; 1597 int ret; 1598 1599 while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) { 1600 if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) && 1601 (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8)) 1602 continue; 1603 1604 if (!atif->handle) 1605 amdgpu_atif_pci_probe_handle(pdev); 1606 if (!atcs->handle) 1607 amdgpu_atcs_pci_probe_handle(pdev); 1608 } 1609 1610 if (atif->functions.sbios_requests && !atif->functions.system_params) { 1611 /* XXX check this workraround, if sbios request function is 1612 * present we have to see how it's configured in the system 1613 * params 1614 */ 1615 atif->functions.system_params = true; 1616 } 1617 1618 if (atif->functions.system_params) { 1619 ret = amdgpu_atif_get_notification_params(atif); 1620 if (ret) { 1621 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n", 1622 ret); 1623 /* Disable notification */ 1624 atif->notification_cfg.enabled = false; 1625 } 1626 } 1627 1628 if (atif->functions.query_backlight_transfer_characteristics) { 1629 ret = amdgpu_atif_query_backlight_caps(atif); 1630 if (ret) { 1631 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n", 1632 ret); 1633 atif->backlight_caps.caps_valid = false; 1634 } 1635 } else { 1636 atif->backlight_caps.caps_valid = false; 1637 } 1638 1639 amdgpu_acpi_enumerate_xcc(); 1640 } 1641 1642 void amdgpu_acpi_release(void) 1643 { 1644 struct amdgpu_acpi_dev_info *dev_info, *dev_tmp; 1645 struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp; 1646 struct amdgpu_numa_info *numa_info; 1647 unsigned long index; 1648 1649 xa_for_each(&numa_info_xa, index, numa_info) { 1650 kfree(numa_info); 1651 xa_erase(&numa_info_xa, index); 1652 } 1653 1654 if (list_empty(&amdgpu_acpi_dev_list)) 1655 return; 1656 1657 list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list, 1658 list) { 1659 list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list, 1660 list) { 1661 list_del(&xcc_info->list); 1662 kfree(xcc_info); 1663 } 1664 1665 list_del(&dev_info->list); 1666 kfree(dev_info); 1667 } 1668 } 1669 1670 #if IS_ENABLED(CONFIG_SUSPEND) 1671 /** 1672 * amdgpu_acpi_is_s3_active 1673 * 1674 * @adev: amdgpu_device_pointer 1675 * 1676 * returns true if supported, false if not. 1677 */ 1678 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev) 1679 { 1680 return !(adev->flags & AMD_IS_APU) || 1681 (pm_suspend_target_state == PM_SUSPEND_MEM); 1682 } 1683 1684 /** 1685 * amdgpu_acpi_is_s0ix_active 1686 * 1687 * @adev: amdgpu_device_pointer 1688 * 1689 * returns true if supported, false if not. 1690 */ 1691 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev) 1692 { 1693 if (!(adev->flags & AMD_IS_APU) || 1694 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE)) 1695 return false; 1696 1697 if (adev->asic_type < CHIP_RAVEN) 1698 return false; 1699 1700 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK)) 1701 return false; 1702 1703 /* 1704 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally 1705 * risky to do any special firmware-related preparations for entering 1706 * S0ix even though the system is suspending to idle, so return false 1707 * in that case. 1708 */ 1709 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) { 1710 dev_err_once(adev->dev, 1711 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n" 1712 "To use suspend-to-idle change the sleep mode in BIOS setup.\n"); 1713 return false; 1714 } 1715 1716 #if !IS_ENABLED(CONFIG_AMD_PMC) 1717 dev_err_once(adev->dev, 1718 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n"); 1719 return false; 1720 #else 1721 return true; 1722 #endif /* CONFIG_AMD_PMC */ 1723 } 1724 #endif /* CONFIG_SUSPEND */ 1725 1726 #if IS_ENABLED(CONFIG_DRM_AMD_ISP) 1727 static const struct acpi_device_id isp_sensor_ids[] = { 1728 { "OMNI5C10" }, 1729 { } 1730 }; 1731 1732 static int isp_match_acpi_device_ids(struct device *dev, const void *data) 1733 { 1734 return acpi_match_device(data, dev) ? 1 : 0; 1735 } 1736 1737 int amdgpu_acpi_get_isp4_dev(struct acpi_device **dev) 1738 { 1739 struct device *pdev __free(put_device) = NULL; 1740 struct acpi_device *acpi_pdev; 1741 1742 pdev = bus_find_device(&platform_bus_type, NULL, isp_sensor_ids, 1743 isp_match_acpi_device_ids); 1744 if (!pdev) 1745 return -EINVAL; 1746 1747 acpi_pdev = ACPI_COMPANION(pdev); 1748 if (!acpi_pdev) 1749 return -ENODEV; 1750 1751 *dev = acpi_pdev; 1752 1753 return 0; 1754 } 1755 #endif /* CONFIG_DRM_AMD_ISP */ 1756