1 // SPDX-License-Identifier: GPL-2.0
3 * Architecture-specific ACPI-based support for suspend-to-idle.
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 const struct acpi_device_id lps0_device_ids[] = {
36 /* Microsoft platform agnostic UUID */
37 #define ACPI_LPS0_DSM_UUID_MICROSOFT "11e00d56-ce64-47ce-837b-1f898f9aa461"
39 #define ACPI_LPS0_DSM_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
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
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
56 static acpi_handle lps0_device_handle;
57 static guid_t lps0_dsm_guid;
58 static int lps0_dsm_func_mask;
60 static guid_t lps0_dsm_guid_microsoft;
61 static int lps0_dsm_func_mask_microsoft;
62 static int lps0_dsm_state;
64 /* Device constraint entry structure */
65 struct lpi_device_info {
68 union acpi_object *package;
71 /* Constraint package structure */
72 struct lpi_device_constraint {
78 struct lpi_constraints {
83 /* AMD Constraint package structure */
84 struct lpi_device_constraint_amd {
91 static LIST_HEAD(lps0_s2idle_devops_head);
93 static struct lpi_constraints *lpi_constraints_table;
94 static int lpi_constraints_table_size;
97 #define for_each_lpi_constraint(entry) \
99 entry = &lpi_constraints_table[i], i < lpi_constraints_table_size; \
102 static void lpi_device_get_constraints_amd(void)
104 union acpi_object *out_obj;
107 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
108 rev_id, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
109 NULL, ACPI_TYPE_PACKAGE);
111 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
112 out_obj ? "successful" : "failed");
117 for (i = 0; i < out_obj->package.count; i++) {
118 union acpi_object *package = &out_obj->package.elements[i];
120 if (package->type == ACPI_TYPE_PACKAGE) {
121 if (lpi_constraints_table) {
122 acpi_handle_err(lps0_device_handle,
123 "Duplicate constraints list\n");
124 goto free_acpi_buffer;
127 lpi_constraints_table = kcalloc(package->package.count,
128 sizeof(*lpi_constraints_table),
131 if (!lpi_constraints_table)
132 goto free_acpi_buffer;
134 acpi_handle_debug(lps0_device_handle,
135 "LPI: constraints list begin:\n");
137 for (j = 0; j < package->package.count; j++) {
138 union acpi_object *info_obj = &package->package.elements[j];
139 struct lpi_device_constraint_amd dev_info = {};
140 struct lpi_constraints *list;
143 list = &lpi_constraints_table[lpi_constraints_table_size];
145 for (k = 0; k < info_obj->package.count; k++) {
146 union acpi_object *obj = &info_obj->package.elements[k];
150 dev_info.enabled = obj->integer.value;
153 dev_info.name = obj->string.pointer;
156 dev_info.function_states = obj->integer.value;
159 dev_info.min_dstate = obj->integer.value;
164 acpi_handle_debug(lps0_device_handle,
165 "Name:%s, Enabled: %d, States: %d, MinDstate: %d\n",
168 dev_info.function_states,
169 dev_info.min_dstate);
171 if (!dev_info.enabled || !dev_info.name ||
172 !dev_info.min_dstate)
175 status = acpi_get_handle(NULL, dev_info.name, &list->handle);
176 if (ACPI_FAILURE(status))
179 list->min_dstate = dev_info.min_dstate;
181 lpi_constraints_table_size++;
186 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
192 static void lpi_device_get_constraints(void)
194 union acpi_object *out_obj;
197 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
198 1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
199 NULL, ACPI_TYPE_PACKAGE);
201 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
202 out_obj ? "successful" : "failed");
207 lpi_constraints_table = kcalloc(out_obj->package.count,
208 sizeof(*lpi_constraints_table),
210 if (!lpi_constraints_table)
211 goto free_acpi_buffer;
213 acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
215 for (i = 0; i < out_obj->package.count; i++) {
216 struct lpi_constraints *constraint;
218 union acpi_object *package = &out_obj->package.elements[i];
219 struct lpi_device_info info = { };
220 int package_count = 0, j;
225 for (j = 0; j < package->package.count; j++) {
226 union acpi_object *element =
227 &(package->package.elements[j]);
229 switch (element->type) {
230 case ACPI_TYPE_INTEGER:
231 info.enabled = element->integer.value;
233 case ACPI_TYPE_STRING:
234 info.name = element->string.pointer;
236 case ACPI_TYPE_PACKAGE:
237 package_count = element->package.count;
238 info.package = element->package.elements;
243 if (!info.enabled || !info.package || !info.name)
246 constraint = &lpi_constraints_table[lpi_constraints_table_size];
248 status = acpi_get_handle(NULL, info.name, &constraint->handle);
249 if (ACPI_FAILURE(status))
252 acpi_handle_debug(lps0_device_handle,
253 "index:%d Name:%s\n", i, info.name);
255 constraint->min_dstate = -1;
257 for (j = 0; j < package_count; j++) {
258 union acpi_object *info_obj = &info.package[j];
259 union acpi_object *cnstr_pkg;
260 union acpi_object *obj;
261 struct lpi_device_constraint dev_info;
263 switch (info_obj->type) {
264 case ACPI_TYPE_INTEGER:
267 case ACPI_TYPE_PACKAGE:
268 if (info_obj->package.count < 2)
271 cnstr_pkg = info_obj->package.elements;
273 dev_info.uid = obj->integer.value;
275 dev_info.min_dstate = obj->integer.value;
277 acpi_handle_debug(lps0_device_handle,
278 "uid:%d min_dstate:%s\n",
280 acpi_power_state_string(dev_info.min_dstate));
282 constraint->min_dstate = dev_info.min_dstate;
287 if (constraint->min_dstate < 0) {
288 acpi_handle_debug(lps0_device_handle,
289 "Incomplete constraint defined\n");
293 lpi_constraints_table_size++;
296 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
303 * acpi_get_lps0_constraint - Get the LPS0 constraint for a device.
304 * @adev: Device to get the constraint for.
306 * The LPS0 constraint is the shallowest (minimum) power state in which the
307 * device can be so as to allow the platform as a whole to achieve additional
308 * energy conservation by utilizing a system-wide low-power state.
311 * - ACPI power state value of the constraint for @adev on success.
312 * - Otherwise, ACPI_STATE_UNKNOWN.
314 int acpi_get_lps0_constraint(struct acpi_device *adev)
316 struct lpi_constraints *entry;
318 for_each_lpi_constraint(entry) {
319 if (adev->handle == entry->handle)
320 return entry->min_dstate;
323 return ACPI_STATE_UNKNOWN;
326 static void lpi_check_constraints(void)
328 struct lpi_constraints *entry;
330 for_each_lpi_constraint(entry) {
331 struct acpi_device *adev = acpi_fetch_acpi_dev(entry->handle);
336 acpi_handle_debug(entry->handle,
337 "LPI: required min power state:%s current power state:%s\n",
338 acpi_power_state_string(entry->min_dstate),
339 acpi_power_state_string(adev->power.state));
341 if (!adev->flags.power_manageable) {
342 acpi_handle_info(entry->handle, "LPI: Device not power manageable\n");
343 entry->handle = NULL;
347 if (adev->power.state < entry->min_dstate)
348 acpi_handle_info(entry->handle,
349 "LPI: Constraint not met; min power state:%s current power state:%s\n",
350 acpi_power_state_string(entry->min_dstate),
351 acpi_power_state_string(adev->power.state));
355 static bool acpi_s2idle_vendor_amd(void)
357 return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
360 static const char *acpi_sleep_dsm_state_to_str(unsigned int state)
362 if (lps0_dsm_func_mask_microsoft || !acpi_s2idle_vendor_amd()) {
364 case ACPI_LPS0_SCREEN_OFF:
366 case ACPI_LPS0_SCREEN_ON:
368 case ACPI_LPS0_ENTRY:
372 case ACPI_LPS0_MS_ENTRY:
373 return "lps0 ms entry";
374 case ACPI_LPS0_MS_EXIT:
375 return "lps0 ms exit";
379 case ACPI_LPS0_SCREEN_ON_AMD:
381 case ACPI_LPS0_SCREEN_OFF_AMD:
383 case ACPI_LPS0_ENTRY_AMD:
385 case ACPI_LPS0_EXIT_AMD:
393 static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
395 union acpi_object *out_obj;
397 if (!(func_mask & (1 << func)))
400 out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
404 lps0_dsm_state = func;
405 if (pm_debug_messages_on) {
406 acpi_handle_info(lps0_device_handle,
407 "%s transitioned to state %s\n",
408 out_obj ? "Successfully" : "Failed to",
409 acpi_sleep_dsm_state_to_str(lps0_dsm_state));
414 static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
416 union acpi_object *obj;
419 guid_parse(uuid, dsm_guid);
420 obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
422 /* Check if the _DSM is present and as expected. */
423 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length == 0 ||
424 obj->buffer.length > sizeof(u32)) {
425 acpi_handle_debug(handle,
426 "_DSM UUID %s rev %d function 0 evaluation failed\n", uuid, rev);
430 ret = *(int *)obj->buffer.pointer;
431 acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
438 struct amd_lps0_hid_device_data {
439 const bool check_off_by_one;
442 static const struct amd_lps0_hid_device_data amd_picasso = {
443 .check_off_by_one = true,
446 static const struct amd_lps0_hid_device_data amd_cezanne = {
447 .check_off_by_one = false,
450 static const struct acpi_device_id amd_hid_ids[] = {
451 {"AMD0004", (kernel_ulong_t)&amd_picasso, },
452 {"AMD0005", (kernel_ulong_t)&amd_picasso, },
453 {"AMDI0005", (kernel_ulong_t)&amd_picasso, },
454 {"AMDI0006", (kernel_ulong_t)&amd_cezanne, },
458 static int lps0_device_attach(struct acpi_device *adev,
459 const struct acpi_device_id *not_used)
461 if (lps0_device_handle)
464 lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
465 ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
466 &lps0_dsm_guid_microsoft);
467 if (acpi_s2idle_vendor_amd()) {
468 static const struct acpi_device_id *dev_id;
469 const struct amd_lps0_hid_device_data *data;
471 for (dev_id = &amd_hid_ids[0]; dev_id->id[0]; dev_id++)
472 if (acpi_dev_hid_uid_match(adev, dev_id->id, NULL))
475 data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
478 lps0_dsm_func_mask = validate_dsm(adev->handle,
479 ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
480 if (lps0_dsm_func_mask > 0x3 && data->check_off_by_one) {
481 lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
482 acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
483 ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
484 } else if (lps0_dsm_func_mask_microsoft > 0 && rev_id) {
485 lps0_dsm_func_mask_microsoft = -EINVAL;
486 acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
490 lps0_dsm_func_mask = validate_dsm(adev->handle,
491 ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
492 lps0_dsm_func_mask_microsoft = -EINVAL;
495 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
496 return 0; //function evaluation failed
498 lps0_device_handle = adev->handle;
500 if (acpi_s2idle_vendor_amd())
501 lpi_device_get_constraints_amd();
503 lpi_device_get_constraints();
506 * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in
507 * the FADT and the default suspend mode was not set from the command
510 if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) &&
511 mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) {
512 mem_sleep_current = PM_SUSPEND_TO_IDLE;
513 pr_info("Low-power S0 idle used by default for system suspend\n");
517 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
518 * EC GPE to be enabled while suspended for certain wakeup devices to
519 * work, so mark it as wakeup-capable.
521 acpi_ec_mark_gpe_for_wake();
526 static struct acpi_scan_handler lps0_handler = {
527 .ids = lps0_device_ids,
528 .attach = lps0_device_attach,
531 int acpi_s2idle_prepare_late(void)
533 struct acpi_s2idle_dev_ops *handler;
535 if (!lps0_device_handle || sleep_no_lps0)
538 if (pm_debug_messages_on)
539 lpi_check_constraints();
542 if (lps0_dsm_func_mask > 0)
543 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
544 ACPI_LPS0_SCREEN_OFF_AMD :
545 ACPI_LPS0_SCREEN_OFF,
546 lps0_dsm_func_mask, lps0_dsm_guid);
548 if (lps0_dsm_func_mask_microsoft > 0)
549 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
550 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
553 if (lps0_dsm_func_mask > 0)
554 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
555 ACPI_LPS0_ENTRY_AMD :
557 lps0_dsm_func_mask, lps0_dsm_guid);
558 if (lps0_dsm_func_mask_microsoft > 0) {
559 /* modern standby entry */
560 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
561 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
562 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
563 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
566 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
567 if (handler->prepare)
574 void acpi_s2idle_check(void)
576 struct acpi_s2idle_dev_ops *handler;
578 if (!lps0_device_handle || sleep_no_lps0)
581 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
587 void acpi_s2idle_restore_early(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)
595 if (handler->restore)
599 if (lps0_dsm_func_mask > 0)
600 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
603 lps0_dsm_func_mask, lps0_dsm_guid);
604 if (lps0_dsm_func_mask_microsoft > 0)
605 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
606 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
608 /* Modern standby exit */
609 if (lps0_dsm_func_mask_microsoft > 0)
610 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
611 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
614 if (lps0_dsm_func_mask_microsoft > 0)
615 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
616 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() ?
619 ACPI_LPS0_SCREEN_ON_AMD :
621 lps0_dsm_func_mask, lps0_dsm_guid);
624 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
625 .begin = acpi_s2idle_begin,
626 .prepare = acpi_s2idle_prepare,
627 .prepare_late = acpi_s2idle_prepare_late,
628 .check = acpi_s2idle_check,
629 .wake = acpi_s2idle_wake,
630 .restore_early = acpi_s2idle_restore_early,
631 .restore = acpi_s2idle_restore,
632 .end = acpi_s2idle_end,
635 void __init acpi_s2idle_setup(void)
637 acpi_scan_add_handler(&lps0_handler);
638 s2idle_set_ops(&acpi_s2idle_ops_lps0);
641 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
643 unsigned int sleep_flags;
645 if (!lps0_device_handle || sleep_no_lps0)
648 sleep_flags = lock_system_sleep();
649 list_add(&arg->list_node, &lps0_s2idle_devops_head);
650 unlock_system_sleep(sleep_flags);
654 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
656 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
658 unsigned int sleep_flags;
660 if (!lps0_device_handle || sleep_no_lps0)
663 sleep_flags = lock_system_sleep();
664 list_del(&arg->list_node);
665 unlock_system_sleep(sleep_flags);
667 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
669 #endif /* CONFIG_SUSPEND */