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 static void lpi_device_get_constraints_amd(void)
99 union acpi_object *out_obj;
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);
106 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
107 out_obj ? "successful" : "failed");
112 for (i = 0; i < out_obj->package.count; i++) {
113 union acpi_object *package = &out_obj->package.elements[i];
115 if (package->type == ACPI_TYPE_PACKAGE) {
116 lpi_constraints_table = kcalloc(package->package.count,
117 sizeof(*lpi_constraints_table),
120 if (!lpi_constraints_table)
121 goto free_acpi_buffer;
123 acpi_handle_debug(lps0_device_handle,
124 "LPI: constraints list begin:\n");
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;
132 for (k = 0; k < info_obj->package.count; ++k) {
133 union acpi_object *obj = &info_obj->package.elements[k];
135 list = &lpi_constraints_table[lpi_constraints_table_size];
136 list->min_dstate = -1;
140 dev_info.enabled = obj->integer.value;
143 dev_info.name = obj->string.pointer;
146 dev_info.function_states = obj->integer.value;
149 dev_info.min_dstate = obj->integer.value;
153 if (!dev_info.enabled || !dev_info.name ||
154 !dev_info.min_dstate)
157 status = acpi_get_handle(NULL, dev_info.name,
159 if (ACPI_FAILURE(status))
162 acpi_handle_debug(lps0_device_handle,
163 "Name:%s\n", dev_info.name);
165 list->min_dstate = dev_info.min_dstate;
167 if (list->min_dstate < 0) {
168 acpi_handle_debug(lps0_device_handle,
169 "Incomplete constraint defined\n");
173 lpi_constraints_table_size++;
178 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
184 static void lpi_device_get_constraints(void)
186 union acpi_object *out_obj;
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);
193 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
194 out_obj ? "successful" : "failed");
199 lpi_constraints_table = kcalloc(out_obj->package.count,
200 sizeof(*lpi_constraints_table),
202 if (!lpi_constraints_table)
203 goto free_acpi_buffer;
205 acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
207 for (i = 0; i < out_obj->package.count; i++) {
208 struct lpi_constraints *constraint;
210 union acpi_object *package = &out_obj->package.elements[i];
211 struct lpi_device_info info = { };
212 int package_count = 0, j;
217 for (j = 0; j < package->package.count; ++j) {
218 union acpi_object *element =
219 &(package->package.elements[j]);
221 switch (element->type) {
222 case ACPI_TYPE_INTEGER:
223 info.enabled = element->integer.value;
225 case ACPI_TYPE_STRING:
226 info.name = element->string.pointer;
228 case ACPI_TYPE_PACKAGE:
229 package_count = element->package.count;
230 info.package = element->package.elements;
235 if (!info.enabled || !info.package || !info.name)
238 constraint = &lpi_constraints_table[lpi_constraints_table_size];
240 status = acpi_get_handle(NULL, info.name, &constraint->handle);
241 if (ACPI_FAILURE(status))
244 acpi_handle_debug(lps0_device_handle,
245 "index:%d Name:%s\n", i, info.name);
247 constraint->min_dstate = -1;
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;
255 switch (info_obj->type) {
256 case ACPI_TYPE_INTEGER:
259 case ACPI_TYPE_PACKAGE:
260 if (info_obj->package.count < 2)
263 cnstr_pkg = info_obj->package.elements;
265 dev_info.uid = obj->integer.value;
267 dev_info.min_dstate = obj->integer.value;
269 acpi_handle_debug(lps0_device_handle,
270 "uid:%d min_dstate:%s\n",
272 acpi_power_state_string(dev_info.min_dstate));
274 constraint->min_dstate = dev_info.min_dstate;
279 if (constraint->min_dstate < 0) {
280 acpi_handle_debug(lps0_device_handle,
281 "Incomplete constraint defined\n");
285 lpi_constraints_table_size++;
288 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
294 static void lpi_check_constraints(void)
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);
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));
310 if (!adev->flags.power_manageable) {
311 acpi_handle_info(handle, "LPI: Device not power manageable\n");
312 lpi_constraints_table[i].handle = NULL;
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));
324 static bool acpi_s2idle_vendor_amd(void)
326 return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
329 static const char *acpi_sleep_dsm_state_to_str(unsigned int state)
331 if (lps0_dsm_func_mask_microsoft || !acpi_s2idle_vendor_amd()) {
333 case ACPI_LPS0_SCREEN_OFF:
335 case ACPI_LPS0_SCREEN_ON:
337 case ACPI_LPS0_ENTRY:
341 case ACPI_LPS0_MS_ENTRY:
342 return "lps0 ms entry";
343 case ACPI_LPS0_MS_EXIT:
344 return "lps0 ms exit";
348 case ACPI_LPS0_SCREEN_ON_AMD:
350 case ACPI_LPS0_SCREEN_OFF_AMD:
352 case ACPI_LPS0_ENTRY_AMD:
354 case ACPI_LPS0_EXIT_AMD:
362 static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
364 union acpi_object *out_obj;
366 if (!(func_mask & (1 << func)))
369 out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
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));
383 static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
385 union acpi_object *obj;
388 guid_parse(uuid, dsm_guid);
389 obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
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);
399 ret = *(int *)obj->buffer.pointer;
400 acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
407 struct amd_lps0_hid_device_data {
408 const bool check_off_by_one;
411 static const struct amd_lps0_hid_device_data amd_picasso = {
412 .check_off_by_one = true,
415 static const struct amd_lps0_hid_device_data amd_cezanne = {
416 .check_off_by_one = false,
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, },
427 static int lps0_device_attach(struct acpi_device *adev,
428 const struct acpi_device_id *not_used)
430 if (lps0_device_handle)
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;
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))
444 data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
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");
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;
464 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
465 return 0; //function evaluation failed
467 lps0_device_handle = adev->handle;
469 if (acpi_s2idle_vendor_amd())
470 lpi_device_get_constraints_amd();
472 lpi_device_get_constraints();
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
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");
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.
490 acpi_ec_mark_gpe_for_wake();
495 static struct acpi_scan_handler lps0_handler = {
496 .ids = lps0_device_ids,
497 .attach = lps0_device_attach,
500 int acpi_s2idle_prepare_late(void)
502 struct acpi_s2idle_dev_ops *handler;
504 if (!lps0_device_handle || sleep_no_lps0)
507 if (pm_debug_messages_on)
508 lpi_check_constraints();
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);
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);
522 if (lps0_dsm_func_mask > 0)
523 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
524 ACPI_LPS0_ENTRY_AMD :
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);
535 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
536 if (handler->prepare)
543 void acpi_s2idle_check(void)
545 struct acpi_s2idle_dev_ops *handler;
547 if (!lps0_device_handle || sleep_no_lps0)
550 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
556 void acpi_s2idle_restore_early(void)
558 struct acpi_s2idle_dev_ops *handler;
560 if (!lps0_device_handle || sleep_no_lps0)
563 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
564 if (handler->restore)
568 if (lps0_dsm_func_mask > 0)
569 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
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);
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);
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 :
590 lps0_dsm_func_mask, lps0_dsm_guid);
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,
604 void __init acpi_s2idle_setup(void)
606 acpi_scan_add_handler(&lps0_handler);
607 s2idle_set_ops(&acpi_s2idle_ops_lps0);
610 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
612 unsigned int sleep_flags;
614 if (!lps0_device_handle || sleep_no_lps0)
617 sleep_flags = lock_system_sleep();
618 list_add(&arg->list_node, &lps0_s2idle_devops_head);
619 unlock_system_sleep(sleep_flags);
623 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
625 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
627 unsigned int sleep_flags;
629 if (!lps0_device_handle || sleep_no_lps0)
632 sleep_flags = lock_system_sleep();
633 list_del(&arg->list_node);
634 unlock_system_sleep(sleep_flags);
636 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
638 #endif /* CONFIG_SUSPEND */