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;
63 /* Device constraint entry structure */
64 struct lpi_device_info {
67 union acpi_object *package;
70 /* Constraint package structure */
71 struct lpi_device_constraint {
77 struct lpi_constraints {
82 /* AMD Constraint package structure */
83 struct lpi_device_constraint_amd {
90 static LIST_HEAD(lps0_s2idle_devops_head);
92 static struct lpi_constraints *lpi_constraints_table;
93 static int lpi_constraints_table_size;
96 static void lpi_device_get_constraints_amd(void)
98 union acpi_object *out_obj;
101 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
102 rev_id, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
103 NULL, ACPI_TYPE_PACKAGE);
105 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
106 out_obj ? "successful" : "failed");
111 for (i = 0; i < out_obj->package.count; i++) {
112 union acpi_object *package = &out_obj->package.elements[i];
114 if (package->type == ACPI_TYPE_PACKAGE) {
115 lpi_constraints_table = kcalloc(package->package.count,
116 sizeof(*lpi_constraints_table),
119 if (!lpi_constraints_table)
120 goto free_acpi_buffer;
122 acpi_handle_debug(lps0_device_handle,
123 "LPI: constraints list begin:\n");
125 for (j = 0; j < package->package.count; ++j) {
126 union acpi_object *info_obj = &package->package.elements[j];
127 struct lpi_device_constraint_amd dev_info = {};
128 struct lpi_constraints *list;
131 for (k = 0; k < info_obj->package.count; ++k) {
132 union acpi_object *obj = &info_obj->package.elements[k];
134 list = &lpi_constraints_table[lpi_constraints_table_size];
135 list->min_dstate = -1;
139 dev_info.enabled = obj->integer.value;
142 dev_info.name = obj->string.pointer;
145 dev_info.function_states = obj->integer.value;
148 dev_info.min_dstate = obj->integer.value;
152 if (!dev_info.enabled || !dev_info.name ||
153 !dev_info.min_dstate)
156 status = acpi_get_handle(NULL, dev_info.name,
158 if (ACPI_FAILURE(status))
161 acpi_handle_debug(lps0_device_handle,
162 "Name:%s\n", dev_info.name);
164 list->min_dstate = dev_info.min_dstate;
166 if (list->min_dstate < 0) {
167 acpi_handle_debug(lps0_device_handle,
168 "Incomplete constraint defined\n");
172 lpi_constraints_table_size++;
177 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
183 static void lpi_device_get_constraints(void)
185 union acpi_object *out_obj;
188 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
189 1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
190 NULL, ACPI_TYPE_PACKAGE);
192 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
193 out_obj ? "successful" : "failed");
198 lpi_constraints_table = kcalloc(out_obj->package.count,
199 sizeof(*lpi_constraints_table),
201 if (!lpi_constraints_table)
202 goto free_acpi_buffer;
204 acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
206 for (i = 0; i < out_obj->package.count; i++) {
207 struct lpi_constraints *constraint;
209 union acpi_object *package = &out_obj->package.elements[i];
210 struct lpi_device_info info = { };
211 int package_count = 0, j;
216 for (j = 0; j < package->package.count; ++j) {
217 union acpi_object *element =
218 &(package->package.elements[j]);
220 switch (element->type) {
221 case ACPI_TYPE_INTEGER:
222 info.enabled = element->integer.value;
224 case ACPI_TYPE_STRING:
225 info.name = element->string.pointer;
227 case ACPI_TYPE_PACKAGE:
228 package_count = element->package.count;
229 info.package = element->package.elements;
234 if (!info.enabled || !info.package || !info.name)
237 constraint = &lpi_constraints_table[lpi_constraints_table_size];
239 status = acpi_get_handle(NULL, info.name, &constraint->handle);
240 if (ACPI_FAILURE(status))
243 acpi_handle_debug(lps0_device_handle,
244 "index:%d Name:%s\n", i, info.name);
246 constraint->min_dstate = -1;
248 for (j = 0; j < package_count; ++j) {
249 union acpi_object *info_obj = &info.package[j];
250 union acpi_object *cnstr_pkg;
251 union acpi_object *obj;
252 struct lpi_device_constraint dev_info;
254 switch (info_obj->type) {
255 case ACPI_TYPE_INTEGER:
258 case ACPI_TYPE_PACKAGE:
259 if (info_obj->package.count < 2)
262 cnstr_pkg = info_obj->package.elements;
264 dev_info.uid = obj->integer.value;
266 dev_info.min_dstate = obj->integer.value;
268 acpi_handle_debug(lps0_device_handle,
269 "uid:%d min_dstate:%s\n",
271 acpi_power_state_string(dev_info.min_dstate));
273 constraint->min_dstate = dev_info.min_dstate;
278 if (constraint->min_dstate < 0) {
279 acpi_handle_debug(lps0_device_handle,
280 "Incomplete constraint defined\n");
284 lpi_constraints_table_size++;
287 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
293 static void lpi_check_constraints(void)
297 for (i = 0; i < lpi_constraints_table_size; ++i) {
298 acpi_handle handle = lpi_constraints_table[i].handle;
299 struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
304 acpi_handle_debug(handle,
305 "LPI: required min power state:%s current power state:%s\n",
306 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
307 acpi_power_state_string(adev->power.state));
309 if (!adev->flags.power_manageable) {
310 acpi_handle_info(handle, "LPI: Device not power manageable\n");
311 lpi_constraints_table[i].handle = NULL;
315 if (adev->power.state < lpi_constraints_table[i].min_dstate)
316 acpi_handle_info(handle,
317 "LPI: Constraint not met; min power state:%s current power state:%s\n",
318 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
319 acpi_power_state_string(adev->power.state));
323 static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
325 union acpi_object *out_obj;
327 if (!(func_mask & (1 << func)))
330 out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
334 acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
335 func, out_obj ? "successful" : "failed");
338 static bool acpi_s2idle_vendor_amd(void)
340 return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
343 static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
345 union acpi_object *obj;
348 guid_parse(uuid, dsm_guid);
349 obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
351 /* Check if the _DSM is present and as expected. */
352 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length == 0 ||
353 obj->buffer.length > sizeof(u32)) {
354 acpi_handle_debug(handle,
355 "_DSM UUID %s rev %d function 0 evaluation failed\n", uuid, rev);
359 ret = *(int *)obj->buffer.pointer;
360 acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
367 struct amd_lps0_hid_device_data {
368 const bool check_off_by_one;
371 static const struct amd_lps0_hid_device_data amd_picasso = {
372 .check_off_by_one = true,
375 static const struct amd_lps0_hid_device_data amd_cezanne = {
376 .check_off_by_one = false,
379 static const struct acpi_device_id amd_hid_ids[] = {
380 {"AMD0004", (kernel_ulong_t)&amd_picasso, },
381 {"AMD0005", (kernel_ulong_t)&amd_picasso, },
382 {"AMDI0005", (kernel_ulong_t)&amd_picasso, },
383 {"AMDI0006", (kernel_ulong_t)&amd_cezanne, },
387 static int lps0_prefer_amd(const struct dmi_system_id *id)
389 pr_debug("Using AMD GUID w/ _REV 2.\n");
393 static const struct dmi_system_id s2idle_dmi_table[] __initconst = {
396 * AMD Rembrandt based HP EliteBook 835/845/865 G9
397 * Contains specialized AML in AMD/_REV 2 path to avoid
398 * triggering a bug in Qualcomm WLAN firmware. This may be
399 * removed in the future if that firmware is fixed.
401 .callback = lps0_prefer_amd,
403 DMI_MATCH(DMI_BOARD_VENDOR, "HP"),
404 DMI_MATCH(DMI_BOARD_NAME, "8990"),
410 static int lps0_device_attach(struct acpi_device *adev,
411 const struct acpi_device_id *not_used)
413 if (lps0_device_handle)
416 lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
417 ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
418 &lps0_dsm_guid_microsoft);
419 if (acpi_s2idle_vendor_amd()) {
420 static const struct acpi_device_id *dev_id;
421 const struct amd_lps0_hid_device_data *data;
423 for (dev_id = &amd_hid_ids[0]; dev_id->id[0]; dev_id++)
424 if (acpi_dev_hid_uid_match(adev, dev_id->id, NULL))
427 data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
430 lps0_dsm_func_mask = validate_dsm(adev->handle,
431 ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
432 if (lps0_dsm_func_mask > 0x3 && data->check_off_by_one) {
433 lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
434 acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
435 ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
436 } else if (lps0_dsm_func_mask_microsoft > 0 && rev_id) {
437 lps0_dsm_func_mask_microsoft = -EINVAL;
438 acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
442 lps0_dsm_func_mask = validate_dsm(adev->handle,
443 ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
444 lps0_dsm_func_mask_microsoft = -EINVAL;
447 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
448 return 0; //function evaluation failed
450 lps0_device_handle = adev->handle;
452 if (acpi_s2idle_vendor_amd())
453 lpi_device_get_constraints_amd();
455 lpi_device_get_constraints();
458 * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in
459 * the FADT and the default suspend mode was not set from the command
462 if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) &&
463 mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) {
464 mem_sleep_current = PM_SUSPEND_TO_IDLE;
465 pr_info("Low-power S0 idle used by default for system suspend\n");
469 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
470 * EC GPE to be enabled while suspended for certain wakeup devices to
471 * work, so mark it as wakeup-capable.
473 acpi_ec_mark_gpe_for_wake();
478 static struct acpi_scan_handler lps0_handler = {
479 .ids = lps0_device_ids,
480 .attach = lps0_device_attach,
483 int acpi_s2idle_prepare_late(void)
485 struct acpi_s2idle_dev_ops *handler;
487 if (!lps0_device_handle || sleep_no_lps0)
490 if (pm_debug_messages_on)
491 lpi_check_constraints();
494 if (lps0_dsm_func_mask > 0)
495 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
496 ACPI_LPS0_SCREEN_OFF_AMD :
497 ACPI_LPS0_SCREEN_OFF,
498 lps0_dsm_func_mask, lps0_dsm_guid);
500 if (lps0_dsm_func_mask_microsoft > 0)
501 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
502 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
505 if (lps0_dsm_func_mask > 0)
506 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
507 ACPI_LPS0_ENTRY_AMD :
509 lps0_dsm_func_mask, lps0_dsm_guid);
510 if (lps0_dsm_func_mask_microsoft > 0) {
511 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
512 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
513 /* modern standby entry */
514 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
515 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
518 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
519 if (handler->prepare)
526 void acpi_s2idle_check(void)
528 struct acpi_s2idle_dev_ops *handler;
530 if (!lps0_device_handle || sleep_no_lps0)
533 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
539 void acpi_s2idle_restore_early(void)
541 struct acpi_s2idle_dev_ops *handler;
543 if (!lps0_device_handle || sleep_no_lps0)
546 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
547 if (handler->restore)
550 /* Modern standby exit */
551 if (lps0_dsm_func_mask_microsoft > 0)
552 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
553 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
556 if (lps0_dsm_func_mask > 0)
557 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
560 lps0_dsm_func_mask, lps0_dsm_guid);
561 if (lps0_dsm_func_mask_microsoft > 0)
562 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
563 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
566 if (lps0_dsm_func_mask_microsoft > 0)
567 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
568 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
569 if (lps0_dsm_func_mask > 0)
570 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
571 ACPI_LPS0_SCREEN_ON_AMD :
573 lps0_dsm_func_mask, lps0_dsm_guid);
576 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
577 .begin = acpi_s2idle_begin,
578 .prepare = acpi_s2idle_prepare,
579 .prepare_late = acpi_s2idle_prepare_late,
580 .check = acpi_s2idle_check,
581 .wake = acpi_s2idle_wake,
582 .restore_early = acpi_s2idle_restore_early,
583 .restore = acpi_s2idle_restore,
584 .end = acpi_s2idle_end,
587 void __init acpi_s2idle_setup(void)
589 dmi_check_system(s2idle_dmi_table);
590 acpi_scan_add_handler(&lps0_handler);
591 s2idle_set_ops(&acpi_s2idle_ops_lps0);
594 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
596 unsigned int sleep_flags;
598 if (!lps0_device_handle || sleep_no_lps0)
601 sleep_flags = lock_system_sleep();
602 list_add(&arg->list_node, &lps0_s2idle_devops_head);
603 unlock_system_sleep(sleep_flags);
607 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
609 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
611 unsigned int sleep_flags;
613 if (!lps0_device_handle || sleep_no_lps0)
616 sleep_flags = lock_system_sleep();
617 list_del(&arg->list_node);
618 unlock_system_sleep(sleep_flags);
620 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
622 #endif /* CONFIG_SUSPEND */