1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Architecture-specific ACPI-based support for suspend-to-idle. 4 * 5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 6 * Author: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> 7 * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com> 8 * 9 * On platforms supporting the Low Power S0 Idle interface there is an ACPI 10 * device object with the PNP0D80 compatible device ID (System Power Management 11 * Controller) and a specific _DSM method under it. That method, if present, 12 * can be used to indicate to the platform that the OS is transitioning into a 13 * low-power state in which certain types of activity are not desirable or that 14 * it is leaving such a state, which allows the platform to adjust its operation 15 * mode accordingly. 16 */ 17 18 #include <linux/acpi.h> 19 #include <linux/device.h> 20 #include <linux/dmi.h> 21 #include <linux/suspend.h> 22 23 #include "../sleep.h" 24 25 #ifdef CONFIG_SUSPEND 26 27 static bool sleep_no_lps0 __read_mostly; 28 module_param(sleep_no_lps0, bool, 0644); 29 MODULE_PARM_DESC(sleep_no_lps0, "Do not use the special LPS0 device interface"); 30 31 static const struct acpi_device_id lps0_device_ids[] = { 32 {"PNP0D80", }, 33 {"", }, 34 }; 35 36 /* Microsoft platform agnostic UUID */ 37 #define ACPI_LPS0_DSM_UUID_MICROSOFT "11e00d56-ce64-47ce-837b-1f898f9aa461" 38 39 #define ACPI_LPS0_DSM_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66" 40 41 #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS 1 42 #define ACPI_LPS0_SCREEN_OFF 3 43 #define ACPI_LPS0_SCREEN_ON 4 44 #define ACPI_LPS0_ENTRY 5 45 #define ACPI_LPS0_EXIT 6 46 #define ACPI_LPS0_MS_ENTRY 7 47 #define ACPI_LPS0_MS_EXIT 8 48 49 /* AMD */ 50 #define ACPI_LPS0_DSM_UUID_AMD "e3f32452-febc-43ce-9039-932122d37721" 51 #define ACPI_LPS0_ENTRY_AMD 2 52 #define ACPI_LPS0_EXIT_AMD 3 53 #define ACPI_LPS0_SCREEN_OFF_AMD 4 54 #define ACPI_LPS0_SCREEN_ON_AMD 5 55 56 static acpi_handle lps0_device_handle; 57 static guid_t lps0_dsm_guid; 58 static int lps0_dsm_func_mask; 59 60 static guid_t lps0_dsm_guid_microsoft; 61 static int lps0_dsm_func_mask_microsoft; 62 static int lps0_dsm_state; 63 64 /* Device constraint entry structure */ 65 struct lpi_device_info { 66 char *name; 67 int enabled; 68 union acpi_object *package; 69 }; 70 71 /* Constraint package structure */ 72 struct lpi_device_constraint { 73 int uid; 74 int min_dstate; 75 int function_states; 76 }; 77 78 struct lpi_constraints { 79 acpi_handle handle; 80 int min_dstate; 81 }; 82 83 /* AMD Constraint package structure */ 84 struct lpi_device_constraint_amd { 85 char *name; 86 int enabled; 87 int function_states; 88 int min_dstate; 89 }; 90 91 static LIST_HEAD(lps0_s2idle_devops_head); 92 93 static struct lpi_constraints *lpi_constraints_table; 94 static int lpi_constraints_table_size; 95 static int rev_id; 96 97 static void lpi_device_get_constraints_amd(void) 98 { 99 union acpi_object *out_obj; 100 int i, j, k; 101 102 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid, 103 rev_id, ACPI_LPS0_GET_DEVICE_CONSTRAINTS, 104 NULL, ACPI_TYPE_PACKAGE); 105 106 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n", 107 out_obj ? "successful" : "failed"); 108 109 if (!out_obj) 110 return; 111 112 for (i = 0; i < out_obj->package.count; i++) { 113 union acpi_object *package = &out_obj->package.elements[i]; 114 115 if (package->type == ACPI_TYPE_PACKAGE) { 116 lpi_constraints_table = kcalloc(package->package.count, 117 sizeof(*lpi_constraints_table), 118 GFP_KERNEL); 119 120 if (!lpi_constraints_table) 121 goto free_acpi_buffer; 122 123 acpi_handle_debug(lps0_device_handle, 124 "LPI: constraints list begin:\n"); 125 126 for (j = 0; j < package->package.count; ++j) { 127 union acpi_object *info_obj = &package->package.elements[j]; 128 struct lpi_device_constraint_amd dev_info = {}; 129 struct lpi_constraints *list; 130 acpi_status status; 131 132 for (k = 0; k < info_obj->package.count; ++k) { 133 union acpi_object *obj = &info_obj->package.elements[k]; 134 135 list = &lpi_constraints_table[lpi_constraints_table_size]; 136 list->min_dstate = -1; 137 138 switch (k) { 139 case 0: 140 dev_info.enabled = obj->integer.value; 141 break; 142 case 1: 143 dev_info.name = obj->string.pointer; 144 break; 145 case 2: 146 dev_info.function_states = obj->integer.value; 147 break; 148 case 3: 149 dev_info.min_dstate = obj->integer.value; 150 break; 151 } 152 153 if (!dev_info.enabled || !dev_info.name || 154 !dev_info.min_dstate) 155 continue; 156 157 status = acpi_get_handle(NULL, dev_info.name, 158 &list->handle); 159 if (ACPI_FAILURE(status)) 160 continue; 161 162 acpi_handle_debug(lps0_device_handle, 163 "Name:%s\n", dev_info.name); 164 165 list->min_dstate = dev_info.min_dstate; 166 167 if (list->min_dstate < 0) { 168 acpi_handle_debug(lps0_device_handle, 169 "Incomplete constraint defined\n"); 170 continue; 171 } 172 } 173 lpi_constraints_table_size++; 174 } 175 } 176 } 177 178 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n"); 179 180 free_acpi_buffer: 181 ACPI_FREE(out_obj); 182 } 183 184 static void lpi_device_get_constraints(void) 185 { 186 union acpi_object *out_obj; 187 int i; 188 189 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid, 190 1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS, 191 NULL, ACPI_TYPE_PACKAGE); 192 193 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n", 194 out_obj ? "successful" : "failed"); 195 196 if (!out_obj) 197 return; 198 199 lpi_constraints_table = kcalloc(out_obj->package.count, 200 sizeof(*lpi_constraints_table), 201 GFP_KERNEL); 202 if (!lpi_constraints_table) 203 goto free_acpi_buffer; 204 205 acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n"); 206 207 for (i = 0; i < out_obj->package.count; i++) { 208 struct lpi_constraints *constraint; 209 acpi_status status; 210 union acpi_object *package = &out_obj->package.elements[i]; 211 struct lpi_device_info info = { }; 212 int package_count = 0, j; 213 214 if (!package) 215 continue; 216 217 for (j = 0; j < package->package.count; ++j) { 218 union acpi_object *element = 219 &(package->package.elements[j]); 220 221 switch (element->type) { 222 case ACPI_TYPE_INTEGER: 223 info.enabled = element->integer.value; 224 break; 225 case ACPI_TYPE_STRING: 226 info.name = element->string.pointer; 227 break; 228 case ACPI_TYPE_PACKAGE: 229 package_count = element->package.count; 230 info.package = element->package.elements; 231 break; 232 } 233 } 234 235 if (!info.enabled || !info.package || !info.name) 236 continue; 237 238 constraint = &lpi_constraints_table[lpi_constraints_table_size]; 239 240 status = acpi_get_handle(NULL, info.name, &constraint->handle); 241 if (ACPI_FAILURE(status)) 242 continue; 243 244 acpi_handle_debug(lps0_device_handle, 245 "index:%d Name:%s\n", i, info.name); 246 247 constraint->min_dstate = -1; 248 249 for (j = 0; j < package_count; ++j) { 250 union acpi_object *info_obj = &info.package[j]; 251 union acpi_object *cnstr_pkg; 252 union acpi_object *obj; 253 struct lpi_device_constraint dev_info; 254 255 switch (info_obj->type) { 256 case ACPI_TYPE_INTEGER: 257 /* version */ 258 break; 259 case ACPI_TYPE_PACKAGE: 260 if (info_obj->package.count < 2) 261 break; 262 263 cnstr_pkg = info_obj->package.elements; 264 obj = &cnstr_pkg[0]; 265 dev_info.uid = obj->integer.value; 266 obj = &cnstr_pkg[1]; 267 dev_info.min_dstate = obj->integer.value; 268 269 acpi_handle_debug(lps0_device_handle, 270 "uid:%d min_dstate:%s\n", 271 dev_info.uid, 272 acpi_power_state_string(dev_info.min_dstate)); 273 274 constraint->min_dstate = dev_info.min_dstate; 275 break; 276 } 277 } 278 279 if (constraint->min_dstate < 0) { 280 acpi_handle_debug(lps0_device_handle, 281 "Incomplete constraint defined\n"); 282 continue; 283 } 284 285 lpi_constraints_table_size++; 286 } 287 288 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n"); 289 290 free_acpi_buffer: 291 ACPI_FREE(out_obj); 292 } 293 294 static void lpi_check_constraints(void) 295 { 296 int i; 297 298 for (i = 0; i < lpi_constraints_table_size; ++i) { 299 acpi_handle handle = lpi_constraints_table[i].handle; 300 struct acpi_device *adev = acpi_fetch_acpi_dev(handle); 301 302 if (!adev) 303 continue; 304 305 acpi_handle_debug(handle, 306 "LPI: required min power state:%s current power state:%s\n", 307 acpi_power_state_string(lpi_constraints_table[i].min_dstate), 308 acpi_power_state_string(adev->power.state)); 309 310 if (!adev->flags.power_manageable) { 311 acpi_handle_info(handle, "LPI: Device not power manageable\n"); 312 lpi_constraints_table[i].handle = NULL; 313 continue; 314 } 315 316 if (adev->power.state < lpi_constraints_table[i].min_dstate) 317 acpi_handle_info(handle, 318 "LPI: Constraint not met; min power state:%s current power state:%s\n", 319 acpi_power_state_string(lpi_constraints_table[i].min_dstate), 320 acpi_power_state_string(adev->power.state)); 321 } 322 } 323 324 static bool acpi_s2idle_vendor_amd(void) 325 { 326 return boot_cpu_data.x86_vendor == X86_VENDOR_AMD; 327 } 328 329 static const char *acpi_sleep_dsm_state_to_str(unsigned int state) 330 { 331 if (lps0_dsm_func_mask_microsoft || !acpi_s2idle_vendor_amd()) { 332 switch (state) { 333 case ACPI_LPS0_SCREEN_OFF: 334 return "screen off"; 335 case ACPI_LPS0_SCREEN_ON: 336 return "screen on"; 337 case ACPI_LPS0_ENTRY: 338 return "lps0 entry"; 339 case ACPI_LPS0_EXIT: 340 return "lps0 exit"; 341 case ACPI_LPS0_MS_ENTRY: 342 return "lps0 ms entry"; 343 case ACPI_LPS0_MS_EXIT: 344 return "lps0 ms exit"; 345 } 346 } else { 347 switch (state) { 348 case ACPI_LPS0_SCREEN_ON_AMD: 349 return "screen on"; 350 case ACPI_LPS0_SCREEN_OFF_AMD: 351 return "screen off"; 352 case ACPI_LPS0_ENTRY_AMD: 353 return "lps0 entry"; 354 case ACPI_LPS0_EXIT_AMD: 355 return "lps0 exit"; 356 } 357 } 358 359 return "unknown"; 360 } 361 362 static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid) 363 { 364 union acpi_object *out_obj; 365 366 if (!(func_mask & (1 << func))) 367 return; 368 369 out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid, 370 rev_id, func, NULL); 371 ACPI_FREE(out_obj); 372 373 lps0_dsm_state = func; 374 if (pm_debug_messages_on) { 375 acpi_handle_info(lps0_device_handle, 376 "%s transitioned to state %s\n", 377 out_obj ? "Successfully" : "Failed to", 378 acpi_sleep_dsm_state_to_str(lps0_dsm_state)); 379 } 380 } 381 382 383 static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid) 384 { 385 union acpi_object *obj; 386 int ret = -EINVAL; 387 388 guid_parse(uuid, dsm_guid); 389 obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL); 390 391 /* Check if the _DSM is present and as expected. */ 392 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length == 0 || 393 obj->buffer.length > sizeof(u32)) { 394 acpi_handle_debug(handle, 395 "_DSM UUID %s rev %d function 0 evaluation failed\n", uuid, rev); 396 goto out; 397 } 398 399 ret = *(int *)obj->buffer.pointer; 400 acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret); 401 402 out: 403 ACPI_FREE(obj); 404 return ret; 405 } 406 407 struct amd_lps0_hid_device_data { 408 const bool check_off_by_one; 409 }; 410 411 static const struct amd_lps0_hid_device_data amd_picasso = { 412 .check_off_by_one = true, 413 }; 414 415 static const struct amd_lps0_hid_device_data amd_cezanne = { 416 .check_off_by_one = false, 417 }; 418 419 static const struct acpi_device_id amd_hid_ids[] = { 420 {"AMD0004", (kernel_ulong_t)&amd_picasso, }, 421 {"AMD0005", (kernel_ulong_t)&amd_picasso, }, 422 {"AMDI0005", (kernel_ulong_t)&amd_picasso, }, 423 {"AMDI0006", (kernel_ulong_t)&amd_cezanne, }, 424 {} 425 }; 426 427 static int lps0_device_attach(struct acpi_device *adev, 428 const struct acpi_device_id *not_used) 429 { 430 if (lps0_device_handle) 431 return 0; 432 433 lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle, 434 ACPI_LPS0_DSM_UUID_MICROSOFT, 0, 435 &lps0_dsm_guid_microsoft); 436 if (acpi_s2idle_vendor_amd()) { 437 static const struct acpi_device_id *dev_id; 438 const struct amd_lps0_hid_device_data *data; 439 440 for (dev_id = &amd_hid_ids[0]; dev_id->id[0]; dev_id++) 441 if (acpi_dev_hid_uid_match(adev, dev_id->id, NULL)) 442 break; 443 if (dev_id->id[0]) 444 data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data; 445 else 446 data = &amd_cezanne; 447 lps0_dsm_func_mask = validate_dsm(adev->handle, 448 ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid); 449 if (lps0_dsm_func_mask > 0x3 && data->check_off_by_one) { 450 lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1; 451 acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n", 452 ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask); 453 } else if (lps0_dsm_func_mask_microsoft > 0 && rev_id) { 454 lps0_dsm_func_mask_microsoft = -EINVAL; 455 acpi_handle_debug(adev->handle, "_DSM Using AMD method\n"); 456 } 457 } else { 458 rev_id = 1; 459 lps0_dsm_func_mask = validate_dsm(adev->handle, 460 ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid); 461 lps0_dsm_func_mask_microsoft = -EINVAL; 462 } 463 464 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0) 465 return 0; //function evaluation failed 466 467 lps0_device_handle = adev->handle; 468 469 if (acpi_s2idle_vendor_amd()) 470 lpi_device_get_constraints_amd(); 471 else 472 lpi_device_get_constraints(); 473 474 /* 475 * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in 476 * the FADT and the default suspend mode was not set from the command 477 * line. 478 */ 479 if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) && 480 mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) { 481 mem_sleep_current = PM_SUSPEND_TO_IDLE; 482 pr_info("Low-power S0 idle used by default for system suspend\n"); 483 } 484 485 /* 486 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the 487 * EC GPE to be enabled while suspended for certain wakeup devices to 488 * work, so mark it as wakeup-capable. 489 */ 490 acpi_ec_mark_gpe_for_wake(); 491 492 return 0; 493 } 494 495 static struct acpi_scan_handler lps0_handler = { 496 .ids = lps0_device_ids, 497 .attach = lps0_device_attach, 498 }; 499 500 int acpi_s2idle_prepare_late(void) 501 { 502 struct acpi_s2idle_dev_ops *handler; 503 504 if (!lps0_device_handle || sleep_no_lps0) 505 return 0; 506 507 if (pm_debug_messages_on) 508 lpi_check_constraints(); 509 510 /* Screen off */ 511 if (lps0_dsm_func_mask > 0) 512 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ? 513 ACPI_LPS0_SCREEN_OFF_AMD : 514 ACPI_LPS0_SCREEN_OFF, 515 lps0_dsm_func_mask, lps0_dsm_guid); 516 517 if (lps0_dsm_func_mask_microsoft > 0) 518 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF, 519 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft); 520 521 /* LPS0 entry */ 522 if (lps0_dsm_func_mask > 0) 523 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ? 524 ACPI_LPS0_ENTRY_AMD : 525 ACPI_LPS0_ENTRY, 526 lps0_dsm_func_mask, lps0_dsm_guid); 527 if (lps0_dsm_func_mask_microsoft > 0) { 528 /* modern standby entry */ 529 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY, 530 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft); 531 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY, 532 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft); 533 } 534 535 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) { 536 if (handler->prepare) 537 handler->prepare(); 538 } 539 540 return 0; 541 } 542 543 void acpi_s2idle_check(void) 544 { 545 struct acpi_s2idle_dev_ops *handler; 546 547 if (!lps0_device_handle || sleep_no_lps0) 548 return; 549 550 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) { 551 if (handler->check) 552 handler->check(); 553 } 554 } 555 556 void acpi_s2idle_restore_early(void) 557 { 558 struct acpi_s2idle_dev_ops *handler; 559 560 if (!lps0_device_handle || sleep_no_lps0) 561 return; 562 563 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) 564 if (handler->restore) 565 handler->restore(); 566 567 /* LPS0 exit */ 568 if (lps0_dsm_func_mask > 0) 569 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ? 570 ACPI_LPS0_EXIT_AMD : 571 ACPI_LPS0_EXIT, 572 lps0_dsm_func_mask, lps0_dsm_guid); 573 if (lps0_dsm_func_mask_microsoft > 0) 574 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT, 575 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft); 576 577 /* Modern standby exit */ 578 if (lps0_dsm_func_mask_microsoft > 0) 579 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT, 580 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft); 581 582 /* Screen on */ 583 if (lps0_dsm_func_mask_microsoft > 0) 584 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON, 585 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft); 586 if (lps0_dsm_func_mask > 0) 587 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ? 588 ACPI_LPS0_SCREEN_ON_AMD : 589 ACPI_LPS0_SCREEN_ON, 590 lps0_dsm_func_mask, lps0_dsm_guid); 591 } 592 593 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = { 594 .begin = acpi_s2idle_begin, 595 .prepare = acpi_s2idle_prepare, 596 .prepare_late = acpi_s2idle_prepare_late, 597 .check = acpi_s2idle_check, 598 .wake = acpi_s2idle_wake, 599 .restore_early = acpi_s2idle_restore_early, 600 .restore = acpi_s2idle_restore, 601 .end = acpi_s2idle_end, 602 }; 603 604 void __init acpi_s2idle_setup(void) 605 { 606 acpi_scan_add_handler(&lps0_handler); 607 s2idle_set_ops(&acpi_s2idle_ops_lps0); 608 } 609 610 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg) 611 { 612 unsigned int sleep_flags; 613 614 if (!lps0_device_handle || sleep_no_lps0) 615 return -ENODEV; 616 617 sleep_flags = lock_system_sleep(); 618 list_add(&arg->list_node, &lps0_s2idle_devops_head); 619 unlock_system_sleep(sleep_flags); 620 621 return 0; 622 } 623 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev); 624 625 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg) 626 { 627 unsigned int sleep_flags; 628 629 if (!lps0_device_handle || sleep_no_lps0) 630 return; 631 632 sleep_flags = lock_system_sleep(); 633 list_del(&arg->list_node); 634 unlock_system_sleep(sleep_flags); 635 } 636 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev); 637 638 #endif /* CONFIG_SUSPEND */ 639