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/suspend.h>
26 static bool sleep_no_lps0 __read_mostly;
27 module_param(sleep_no_lps0, bool, 0644);
28 MODULE_PARM_DESC(sleep_no_lps0, "Do not use the special LPS0 device interface");
30 static const struct acpi_device_id lps0_device_ids[] = {
35 /* Microsoft platform agnostic UUID */
36 #define ACPI_LPS0_DSM_UUID_MICROSOFT "11e00d56-ce64-47ce-837b-1f898f9aa461"
38 #define ACPI_LPS0_DSM_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
40 #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS 1
41 #define ACPI_LPS0_SCREEN_OFF 3
42 #define ACPI_LPS0_SCREEN_ON 4
43 #define ACPI_LPS0_ENTRY 5
44 #define ACPI_LPS0_EXIT 6
45 #define ACPI_LPS0_MS_ENTRY 7
46 #define ACPI_LPS0_MS_EXIT 8
49 #define ACPI_LPS0_DSM_UUID_AMD "e3f32452-febc-43ce-9039-932122d37721"
50 #define ACPI_LPS0_ENTRY_AMD 2
51 #define ACPI_LPS0_EXIT_AMD 3
52 #define ACPI_LPS0_SCREEN_OFF_AMD 4
53 #define ACPI_LPS0_SCREEN_ON_AMD 5
55 static acpi_handle lps0_device_handle;
56 static guid_t lps0_dsm_guid;
57 static int lps0_dsm_func_mask;
59 static guid_t lps0_dsm_guid_microsoft;
60 static int lps0_dsm_func_mask_microsoft;
62 /* Device constraint entry structure */
63 struct lpi_device_info {
66 union acpi_object *package;
69 /* Constraint package structure */
70 struct lpi_device_constraint {
76 struct lpi_constraints {
81 /* AMD Constraint package structure */
82 struct lpi_device_constraint_amd {
89 static LIST_HEAD(lps0_s2idle_devops_head);
91 static struct lpi_constraints *lpi_constraints_table;
92 static int lpi_constraints_table_size;
95 static void lpi_device_get_constraints_amd(void)
97 union acpi_object *out_obj;
100 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
101 rev_id, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
102 NULL, ACPI_TYPE_PACKAGE);
104 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
105 out_obj ? "successful" : "failed");
110 for (i = 0; i < out_obj->package.count; i++) {
111 union acpi_object *package = &out_obj->package.elements[i];
113 if (package->type == ACPI_TYPE_PACKAGE) {
114 lpi_constraints_table = kcalloc(package->package.count,
115 sizeof(*lpi_constraints_table),
118 if (!lpi_constraints_table)
119 goto free_acpi_buffer;
121 acpi_handle_debug(lps0_device_handle,
122 "LPI: constraints list begin:\n");
124 for (j = 0; j < package->package.count; ++j) {
125 union acpi_object *info_obj = &package->package.elements[j];
126 struct lpi_device_constraint_amd dev_info = {};
127 struct lpi_constraints *list;
130 for (k = 0; k < info_obj->package.count; ++k) {
131 union acpi_object *obj = &info_obj->package.elements[k];
133 list = &lpi_constraints_table[lpi_constraints_table_size];
134 list->min_dstate = -1;
138 dev_info.enabled = obj->integer.value;
141 dev_info.name = obj->string.pointer;
144 dev_info.function_states = obj->integer.value;
147 dev_info.min_dstate = obj->integer.value;
151 if (!dev_info.enabled || !dev_info.name ||
152 !dev_info.min_dstate)
155 status = acpi_get_handle(NULL, dev_info.name,
157 if (ACPI_FAILURE(status))
160 acpi_handle_debug(lps0_device_handle,
161 "Name:%s\n", dev_info.name);
163 list->min_dstate = dev_info.min_dstate;
165 if (list->min_dstate < 0) {
166 acpi_handle_debug(lps0_device_handle,
167 "Incomplete constraint defined\n");
171 lpi_constraints_table_size++;
176 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
182 static void lpi_device_get_constraints(void)
184 union acpi_object *out_obj;
187 out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
188 1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
189 NULL, ACPI_TYPE_PACKAGE);
191 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
192 out_obj ? "successful" : "failed");
197 lpi_constraints_table = kcalloc(out_obj->package.count,
198 sizeof(*lpi_constraints_table),
200 if (!lpi_constraints_table)
201 goto free_acpi_buffer;
203 acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
205 for (i = 0; i < out_obj->package.count; i++) {
206 struct lpi_constraints *constraint;
208 union acpi_object *package = &out_obj->package.elements[i];
209 struct lpi_device_info info = { };
210 int package_count = 0, j;
215 for (j = 0; j < package->package.count; ++j) {
216 union acpi_object *element =
217 &(package->package.elements[j]);
219 switch (element->type) {
220 case ACPI_TYPE_INTEGER:
221 info.enabled = element->integer.value;
223 case ACPI_TYPE_STRING:
224 info.name = element->string.pointer;
226 case ACPI_TYPE_PACKAGE:
227 package_count = element->package.count;
228 info.package = element->package.elements;
233 if (!info.enabled || !info.package || !info.name)
236 constraint = &lpi_constraints_table[lpi_constraints_table_size];
238 status = acpi_get_handle(NULL, info.name, &constraint->handle);
239 if (ACPI_FAILURE(status))
242 acpi_handle_debug(lps0_device_handle,
243 "index:%d Name:%s\n", i, info.name);
245 constraint->min_dstate = -1;
247 for (j = 0; j < package_count; ++j) {
248 union acpi_object *info_obj = &info.package[j];
249 union acpi_object *cnstr_pkg;
250 union acpi_object *obj;
251 struct lpi_device_constraint dev_info;
253 switch (info_obj->type) {
254 case ACPI_TYPE_INTEGER:
257 case ACPI_TYPE_PACKAGE:
258 if (info_obj->package.count < 2)
261 cnstr_pkg = info_obj->package.elements;
263 dev_info.uid = obj->integer.value;
265 dev_info.min_dstate = obj->integer.value;
267 acpi_handle_debug(lps0_device_handle,
268 "uid:%d min_dstate:%s\n",
270 acpi_power_state_string(dev_info.min_dstate));
272 constraint->min_dstate = dev_info.min_dstate;
277 if (constraint->min_dstate < 0) {
278 acpi_handle_debug(lps0_device_handle,
279 "Incomplete constraint defined\n");
283 lpi_constraints_table_size++;
286 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
292 static void lpi_check_constraints(void)
296 for (i = 0; i < lpi_constraints_table_size; ++i) {
297 acpi_handle handle = lpi_constraints_table[i].handle;
298 struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
303 acpi_handle_debug(handle,
304 "LPI: required min power state:%s current power state:%s\n",
305 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
306 acpi_power_state_string(adev->power.state));
308 if (!adev->flags.power_manageable) {
309 acpi_handle_info(handle, "LPI: Device not power manageable\n");
310 lpi_constraints_table[i].handle = NULL;
314 if (adev->power.state < lpi_constraints_table[i].min_dstate)
315 acpi_handle_info(handle,
316 "LPI: Constraint not met; min power state:%s current power state:%s\n",
317 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
318 acpi_power_state_string(adev->power.state));
322 static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
324 union acpi_object *out_obj;
326 if (!(func_mask & (1 << func)))
329 out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
333 acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
334 func, out_obj ? "successful" : "failed");
337 static bool acpi_s2idle_vendor_amd(void)
339 return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
342 static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
344 union acpi_object *obj;
347 guid_parse(uuid, dsm_guid);
348 obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
350 /* Check if the _DSM is present and as expected. */
351 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length == 0 ||
352 obj->buffer.length > sizeof(u32)) {
353 acpi_handle_debug(handle,
354 "_DSM UUID %s rev %d function 0 evaluation failed\n", uuid, rev);
358 ret = *(int *)obj->buffer.pointer;
359 acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
366 struct amd_lps0_hid_device_data {
367 const unsigned int rev_id;
368 const bool check_off_by_one;
369 const bool prefer_amd_guid;
372 static const struct amd_lps0_hid_device_data amd_picasso = {
374 .check_off_by_one = true,
375 .prefer_amd_guid = false,
378 static const struct amd_lps0_hid_device_data amd_cezanne = {
380 .check_off_by_one = false,
381 .prefer_amd_guid = false,
384 static const struct amd_lps0_hid_device_data amd_rembrandt = {
386 .check_off_by_one = false,
387 .prefer_amd_guid = true,
390 static const struct acpi_device_id amd_hid_ids[] = {
391 {"AMD0004", (kernel_ulong_t)&amd_picasso, },
392 {"AMD0005", (kernel_ulong_t)&amd_picasso, },
393 {"AMDI0005", (kernel_ulong_t)&amd_picasso, },
394 {"AMDI0006", (kernel_ulong_t)&amd_cezanne, },
395 {"AMDI0007", (kernel_ulong_t)&amd_rembrandt, },
399 static int lps0_device_attach(struct acpi_device *adev,
400 const struct acpi_device_id *not_used)
402 if (lps0_device_handle)
405 if (acpi_s2idle_vendor_amd()) {
406 static const struct acpi_device_id *dev_id;
407 const struct amd_lps0_hid_device_data *data;
409 for (dev_id = &amd_hid_ids[0]; dev_id->id[0]; dev_id++)
410 if (acpi_dev_hid_uid_match(adev, dev_id->id, NULL))
413 data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
416 rev_id = data->rev_id;
417 lps0_dsm_func_mask = validate_dsm(adev->handle,
418 ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
419 lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
420 ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
421 &lps0_dsm_guid_microsoft);
422 if (lps0_dsm_func_mask > 0x3 && data->check_off_by_one) {
423 lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
424 acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
425 ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
426 } else if (lps0_dsm_func_mask_microsoft > 0 && data->prefer_amd_guid &&
427 (!strcmp(hid, "AMDI0007") ||
428 !strcmp(hid, "AMDI0008"))) {
429 lps0_dsm_func_mask_microsoft = -EINVAL;
430 acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
434 lps0_dsm_func_mask = validate_dsm(adev->handle,
435 ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
436 lps0_dsm_func_mask_microsoft = -EINVAL;
439 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
440 return 0; //function evaluation failed
442 lps0_device_handle = adev->handle;
444 if (acpi_s2idle_vendor_amd())
445 lpi_device_get_constraints_amd();
447 lpi_device_get_constraints();
450 * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in
451 * the FADT and the default suspend mode was not set from the command
454 if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) &&
455 mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) {
456 mem_sleep_current = PM_SUSPEND_TO_IDLE;
457 pr_info("Low-power S0 idle used by default for system suspend\n");
461 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
462 * EC GPE to be enabled while suspended for certain wakeup devices to
463 * work, so mark it as wakeup-capable.
465 acpi_ec_mark_gpe_for_wake();
470 static struct acpi_scan_handler lps0_handler = {
471 .ids = lps0_device_ids,
472 .attach = lps0_device_attach,
475 int acpi_s2idle_prepare_late(void)
477 struct acpi_s2idle_dev_ops *handler;
479 if (!lps0_device_handle || sleep_no_lps0)
482 if (pm_debug_messages_on)
483 lpi_check_constraints();
486 if (lps0_dsm_func_mask > 0)
487 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
488 ACPI_LPS0_SCREEN_OFF_AMD :
489 ACPI_LPS0_SCREEN_OFF,
490 lps0_dsm_func_mask, lps0_dsm_guid);
492 if (lps0_dsm_func_mask_microsoft > 0)
493 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
494 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
497 if (lps0_dsm_func_mask > 0)
498 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
499 ACPI_LPS0_ENTRY_AMD :
501 lps0_dsm_func_mask, lps0_dsm_guid);
502 if (lps0_dsm_func_mask_microsoft > 0) {
503 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
504 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
505 /* modern standby entry */
506 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
507 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
510 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
511 if (handler->prepare)
518 void acpi_s2idle_restore_early(void)
520 struct acpi_s2idle_dev_ops *handler;
522 if (!lps0_device_handle || sleep_no_lps0)
525 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
526 if (handler->restore)
529 /* Modern standby exit */
530 if (lps0_dsm_func_mask_microsoft > 0)
531 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
532 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
535 if (lps0_dsm_func_mask > 0)
536 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
539 lps0_dsm_func_mask, lps0_dsm_guid);
540 if (lps0_dsm_func_mask_microsoft > 0)
541 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
542 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
545 if (lps0_dsm_func_mask_microsoft > 0)
546 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
547 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
548 if (lps0_dsm_func_mask > 0)
549 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
550 ACPI_LPS0_SCREEN_ON_AMD :
552 lps0_dsm_func_mask, lps0_dsm_guid);
555 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
556 .begin = acpi_s2idle_begin,
557 .prepare = acpi_s2idle_prepare,
558 .prepare_late = acpi_s2idle_prepare_late,
559 .wake = acpi_s2idle_wake,
560 .restore_early = acpi_s2idle_restore_early,
561 .restore = acpi_s2idle_restore,
562 .end = acpi_s2idle_end,
565 void acpi_s2idle_setup(void)
567 acpi_scan_add_handler(&lps0_handler);
568 s2idle_set_ops(&acpi_s2idle_ops_lps0);
571 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
573 if (!lps0_device_handle || sleep_no_lps0)
577 list_add(&arg->list_node, &lps0_s2idle_devops_head);
578 unlock_system_sleep();
582 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
584 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
586 if (!lps0_device_handle || sleep_no_lps0)
590 list_del(&arg->list_node);
591 unlock_system_sleep();
593 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
595 #endif /* CONFIG_SUSPEND */