1 // SPDX-License-Identifier: GPL-2.0
3 * Architecture-specific ACPI-based support for suspend-to-idle.
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>
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
18 #include <linux/acpi.h>
19 #include <linux/device.h>
20 #include <linux/dmi.h>
21 #include <linux/suspend.h>
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");
31 static bool prefer_microsoft_dsm_guid __read_mostly;
32 module_param(prefer_microsoft_dsm_guid, bool, 0644);
33 MODULE_PARM_DESC(prefer_microsoft_dsm_guid, "Prefer using Microsoft GUID in LPS0 device _DSM evaluation");
35 static const struct acpi_device_id lps0_device_ids[] = {
40 /* Microsoft platform agnostic UUID */
41 #define ACPI_LPS0_DSM_UUID_MICROSOFT "11e00d56-ce64-47ce-837b-1f898f9aa461"
43 #define ACPI_LPS0_DSM_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
45 #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS 1
46 #define ACPI_LPS0_SCREEN_OFF 3
47 #define ACPI_LPS0_SCREEN_ON 4
48 #define ACPI_LPS0_ENTRY 5
49 #define ACPI_LPS0_EXIT 6
50 #define ACPI_LPS0_MS_ENTRY 7
51 #define ACPI_LPS0_MS_EXIT 8
54 #define ACPI_LPS0_DSM_UUID_AMD "e3f32452-febc-43ce-9039-932122d37721"
55 #define ACPI_LPS0_ENTRY_AMD 2
56 #define ACPI_LPS0_EXIT_AMD 3
57 #define ACPI_LPS0_SCREEN_OFF_AMD 4
58 #define ACPI_LPS0_SCREEN_ON_AMD 5
60 static acpi_handle lps0_device_handle;
61 static guid_t lps0_dsm_guid;
62 static int lps0_dsm_func_mask;
64 static guid_t lps0_dsm_guid_microsoft;
65 static int lps0_dsm_func_mask_microsoft;
67 /* Device constraint entry structure */
68 struct lpi_device_info {
71 union acpi_object *package;
74 /* Constraint package structure */
75 struct lpi_device_constraint {
81 struct lpi_constraints {
86 /* AMD Constraint package structure */
87 struct lpi_device_constraint_amd {
94 static LIST_HEAD(lps0_s2idle_devops_head);
96 static struct lpi_constraints *lpi_constraints_table;
97 static int lpi_constraints_table_size;
100 static void lpi_device_get_constraints_amd(void)
102 union acpi_object *out_obj;
105 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
106 rev_id, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
107 NULL, ACPI_TYPE_PACKAGE);
109 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
110 out_obj ? "successful" : "failed");
115 for (i = 0; i < out_obj->package.count; i++) {
116 union acpi_object *package = &out_obj->package.elements[i];
118 if (package->type == ACPI_TYPE_PACKAGE) {
119 lpi_constraints_table = kcalloc(package->package.count,
120 sizeof(*lpi_constraints_table),
123 if (!lpi_constraints_table)
124 goto free_acpi_buffer;
126 acpi_handle_debug(lps0_device_handle,
127 "LPI: constraints list begin:\n");
129 for (j = 0; j < package->package.count; ++j) {
130 union acpi_object *info_obj = &package->package.elements[j];
131 struct lpi_device_constraint_amd dev_info = {};
132 struct lpi_constraints *list;
135 for (k = 0; k < info_obj->package.count; ++k) {
136 union acpi_object *obj = &info_obj->package.elements[k];
138 list = &lpi_constraints_table[lpi_constraints_table_size];
139 list->min_dstate = -1;
143 dev_info.enabled = obj->integer.value;
146 dev_info.name = obj->string.pointer;
149 dev_info.function_states = obj->integer.value;
152 dev_info.min_dstate = obj->integer.value;
156 if (!dev_info.enabled || !dev_info.name ||
157 !dev_info.min_dstate)
160 status = acpi_get_handle(NULL, dev_info.name,
162 if (ACPI_FAILURE(status))
165 acpi_handle_debug(lps0_device_handle,
166 "Name:%s\n", dev_info.name);
168 list->min_dstate = dev_info.min_dstate;
170 if (list->min_dstate < 0) {
171 acpi_handle_debug(lps0_device_handle,
172 "Incomplete constraint defined\n");
176 lpi_constraints_table_size++;
181 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
187 static void lpi_device_get_constraints(void)
189 union acpi_object *out_obj;
192 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
193 1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
194 NULL, ACPI_TYPE_PACKAGE);
196 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
197 out_obj ? "successful" : "failed");
202 lpi_constraints_table = kcalloc(out_obj->package.count,
203 sizeof(*lpi_constraints_table),
205 if (!lpi_constraints_table)
206 goto free_acpi_buffer;
208 acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
210 for (i = 0; i < out_obj->package.count; i++) {
211 struct lpi_constraints *constraint;
213 union acpi_object *package = &out_obj->package.elements[i];
214 struct lpi_device_info info = { };
215 int package_count = 0, j;
220 for (j = 0; j < package->package.count; ++j) {
221 union acpi_object *element =
222 &(package->package.elements[j]);
224 switch (element->type) {
225 case ACPI_TYPE_INTEGER:
226 info.enabled = element->integer.value;
228 case ACPI_TYPE_STRING:
229 info.name = element->string.pointer;
231 case ACPI_TYPE_PACKAGE:
232 package_count = element->package.count;
233 info.package = element->package.elements;
238 if (!info.enabled || !info.package || !info.name)
241 constraint = &lpi_constraints_table[lpi_constraints_table_size];
243 status = acpi_get_handle(NULL, info.name, &constraint->handle);
244 if (ACPI_FAILURE(status))
247 acpi_handle_debug(lps0_device_handle,
248 "index:%d Name:%s\n", i, info.name);
250 constraint->min_dstate = -1;
252 for (j = 0; j < package_count; ++j) {
253 union acpi_object *info_obj = &info.package[j];
254 union acpi_object *cnstr_pkg;
255 union acpi_object *obj;
256 struct lpi_device_constraint dev_info;
258 switch (info_obj->type) {
259 case ACPI_TYPE_INTEGER:
262 case ACPI_TYPE_PACKAGE:
263 if (info_obj->package.count < 2)
266 cnstr_pkg = info_obj->package.elements;
268 dev_info.uid = obj->integer.value;
270 dev_info.min_dstate = obj->integer.value;
272 acpi_handle_debug(lps0_device_handle,
273 "uid:%d min_dstate:%s\n",
275 acpi_power_state_string(dev_info.min_dstate));
277 constraint->min_dstate = dev_info.min_dstate;
282 if (constraint->min_dstate < 0) {
283 acpi_handle_debug(lps0_device_handle,
284 "Incomplete constraint defined\n");
288 lpi_constraints_table_size++;
291 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
297 static void lpi_check_constraints(void)
301 for (i = 0; i < lpi_constraints_table_size; ++i) {
302 acpi_handle handle = lpi_constraints_table[i].handle;
303 struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
308 acpi_handle_debug(handle,
309 "LPI: required min power state:%s current power state:%s\n",
310 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
311 acpi_power_state_string(adev->power.state));
313 if (!adev->flags.power_manageable) {
314 acpi_handle_info(handle, "LPI: Device not power manageable\n");
315 lpi_constraints_table[i].handle = NULL;
319 if (adev->power.state < lpi_constraints_table[i].min_dstate)
320 acpi_handle_info(handle,
321 "LPI: Constraint not met; min power state:%s current power state:%s\n",
322 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
323 acpi_power_state_string(adev->power.state));
327 static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
329 union acpi_object *out_obj;
331 if (!(func_mask & (1 << func)))
334 out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
338 acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
339 func, out_obj ? "successful" : "failed");
342 static bool acpi_s2idle_vendor_amd(void)
344 return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
347 static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
349 union acpi_object *obj;
352 guid_parse(uuid, dsm_guid);
353 obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
355 /* Check if the _DSM is present and as expected. */
356 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length == 0 ||
357 obj->buffer.length > sizeof(u32)) {
358 acpi_handle_debug(handle,
359 "_DSM UUID %s rev %d function 0 evaluation failed\n", uuid, rev);
363 ret = *(int *)obj->buffer.pointer;
364 acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
371 struct amd_lps0_hid_device_data {
372 const unsigned int rev_id;
373 const bool check_off_by_one;
374 const bool prefer_amd_guid;
377 static const struct amd_lps0_hid_device_data amd_picasso = {
379 .check_off_by_one = true,
380 .prefer_amd_guid = false,
383 static const struct amd_lps0_hid_device_data amd_cezanne = {
385 .check_off_by_one = false,
386 .prefer_amd_guid = false,
389 static const struct amd_lps0_hid_device_data amd_rembrandt = {
391 .check_off_by_one = false,
392 .prefer_amd_guid = true,
395 static const struct acpi_device_id amd_hid_ids[] = {
396 {"AMD0004", (kernel_ulong_t)&amd_picasso, },
397 {"AMD0005", (kernel_ulong_t)&amd_picasso, },
398 {"AMDI0005", (kernel_ulong_t)&amd_picasso, },
399 {"AMDI0006", (kernel_ulong_t)&amd_cezanne, },
400 {"AMDI0007", (kernel_ulong_t)&amd_rembrandt, },
404 static int lps0_prefer_microsoft(const struct dmi_system_id *id)
406 pr_debug("Preferring Microsoft GUID.\n");
407 prefer_microsoft_dsm_guid = true;
411 static const struct dmi_system_id s2idle_dmi_table[] __initconst = {
414 * ASUS TUF Gaming A17 FA707RE
415 * https://bugzilla.kernel.org/show_bug.cgi?id=216101
417 .callback = lps0_prefer_microsoft,
419 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
420 DMI_MATCH(DMI_PRODUCT_NAME, "ASUS TUF Gaming A17"),
424 /* ASUS ROG Zephyrus G14 (2022) */
425 .callback = lps0_prefer_microsoft,
427 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
428 DMI_MATCH(DMI_PRODUCT_NAME, "ROG Zephyrus G14 GA402"),
433 * Lenovo Yoga Slim 7 Pro X 14ARH7
434 * https://bugzilla.kernel.org/show_bug.cgi?id=216473 : 82V2
435 * https://bugzilla.kernel.org/show_bug.cgi?id=216438 : 82TL
437 .callback = lps0_prefer_microsoft,
439 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
440 DMI_MATCH(DMI_PRODUCT_NAME, "82"),
445 * ASUSTeK COMPUTER INC. ROG Flow X13 GV301RE_GV301RE
446 * https://gitlab.freedesktop.org/drm/amd/-/issues/2148
448 .callback = lps0_prefer_microsoft,
450 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
451 DMI_MATCH(DMI_PRODUCT_NAME, "ROG Flow X13 GV301"),
456 * ASUSTeK COMPUTER INC. ROG Flow X16 GV601RW_GV601RW
457 * https://gitlab.freedesktop.org/drm/amd/-/issues/2148
459 .callback = lps0_prefer_microsoft,
461 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
462 DMI_MATCH(DMI_PRODUCT_NAME, "ROG Flow X16 GV601"),
468 static int lps0_device_attach(struct acpi_device *adev,
469 const struct acpi_device_id *not_used)
471 if (lps0_device_handle)
474 lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
475 ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
476 &lps0_dsm_guid_microsoft);
477 if (acpi_s2idle_vendor_amd()) {
478 static const struct acpi_device_id *dev_id;
479 const struct amd_lps0_hid_device_data *data;
481 for (dev_id = &amd_hid_ids[0]; dev_id->id[0]; dev_id++)
482 if (acpi_dev_hid_uid_match(adev, dev_id->id, NULL))
485 data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
487 data = &amd_rembrandt;
488 rev_id = data->rev_id;
489 lps0_dsm_func_mask = validate_dsm(adev->handle,
490 ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
491 if (lps0_dsm_func_mask > 0x3 && data->check_off_by_one) {
492 lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
493 acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
494 ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
495 } else if (lps0_dsm_func_mask_microsoft > 0 && data->prefer_amd_guid &&
496 !prefer_microsoft_dsm_guid) {
497 lps0_dsm_func_mask_microsoft = -EINVAL;
498 acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
502 lps0_dsm_func_mask = validate_dsm(adev->handle,
503 ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
504 if (!prefer_microsoft_dsm_guid)
505 lps0_dsm_func_mask_microsoft = -EINVAL;
508 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
509 return 0; //function evaluation failed
511 lps0_device_handle = adev->handle;
513 if (acpi_s2idle_vendor_amd())
514 lpi_device_get_constraints_amd();
516 lpi_device_get_constraints();
519 * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in
520 * the FADT and the default suspend mode was not set from the command
523 if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) &&
524 mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) {
525 mem_sleep_current = PM_SUSPEND_TO_IDLE;
526 pr_info("Low-power S0 idle used by default for system suspend\n");
530 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
531 * EC GPE to be enabled while suspended for certain wakeup devices to
532 * work, so mark it as wakeup-capable.
534 acpi_ec_mark_gpe_for_wake();
539 static struct acpi_scan_handler lps0_handler = {
540 .ids = lps0_device_ids,
541 .attach = lps0_device_attach,
544 int acpi_s2idle_prepare_late(void)
546 struct acpi_s2idle_dev_ops *handler;
548 if (!lps0_device_handle || sleep_no_lps0)
551 if (pm_debug_messages_on)
552 lpi_check_constraints();
555 if (lps0_dsm_func_mask > 0)
556 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
557 ACPI_LPS0_SCREEN_OFF_AMD :
558 ACPI_LPS0_SCREEN_OFF,
559 lps0_dsm_func_mask, lps0_dsm_guid);
561 if (lps0_dsm_func_mask_microsoft > 0)
562 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
563 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
566 if (lps0_dsm_func_mask > 0)
567 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
568 ACPI_LPS0_ENTRY_AMD :
570 lps0_dsm_func_mask, lps0_dsm_guid);
571 if (lps0_dsm_func_mask_microsoft > 0) {
572 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
573 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
574 /* modern standby entry */
575 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
576 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
579 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
580 if (handler->prepare)
587 void acpi_s2idle_check(void)
589 struct acpi_s2idle_dev_ops *handler;
591 if (!lps0_device_handle || sleep_no_lps0)
594 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
600 void acpi_s2idle_restore_early(void)
602 struct acpi_s2idle_dev_ops *handler;
604 if (!lps0_device_handle || sleep_no_lps0)
607 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
608 if (handler->restore)
611 /* Modern standby exit */
612 if (lps0_dsm_func_mask_microsoft > 0)
613 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
614 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
617 if (lps0_dsm_func_mask > 0)
618 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
621 lps0_dsm_func_mask, lps0_dsm_guid);
622 if (lps0_dsm_func_mask_microsoft > 0)
623 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
624 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
627 if (lps0_dsm_func_mask_microsoft > 0)
628 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
629 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
630 if (lps0_dsm_func_mask > 0)
631 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
632 ACPI_LPS0_SCREEN_ON_AMD :
634 lps0_dsm_func_mask, lps0_dsm_guid);
637 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
638 .begin = acpi_s2idle_begin,
639 .prepare = acpi_s2idle_prepare,
640 .prepare_late = acpi_s2idle_prepare_late,
641 .check = acpi_s2idle_check,
642 .wake = acpi_s2idle_wake,
643 .restore_early = acpi_s2idle_restore_early,
644 .restore = acpi_s2idle_restore,
645 .end = acpi_s2idle_end,
648 void __init acpi_s2idle_setup(void)
650 dmi_check_system(s2idle_dmi_table);
651 acpi_scan_add_handler(&lps0_handler);
652 s2idle_set_ops(&acpi_s2idle_ops_lps0);
655 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
657 unsigned int sleep_flags;
659 if (!lps0_device_handle || sleep_no_lps0)
662 sleep_flags = lock_system_sleep();
663 list_add(&arg->list_node, &lps0_s2idle_devops_head);
664 unlock_system_sleep(sleep_flags);
668 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
670 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
672 unsigned int sleep_flags;
674 if (!lps0_device_handle || sleep_no_lps0)
677 sleep_flags = lock_system_sleep();
678 list_del(&arg->list_node);
679 unlock_system_sleep(sleep_flags);
681 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
683 #endif /* CONFIG_SUSPEND */