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 static int lps0_device_attach(struct acpi_device *adev,
367 const struct acpi_device_id *not_used)
369 if (lps0_device_handle)
372 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
375 if (acpi_s2idle_vendor_amd()) {
376 /* AMD0004, AMD0005, AMDI0005:
377 * - Should use rev_id 0x0
378 * - function mask > 0x3: Should use AMD method, but has off by one bug
379 * - function mask = 0x3: Should use Microsoft method
381 * - should use rev_id 0x0
382 * - function mask = 0x3: Should use Microsoft method
384 * - Should use rev_id 0x2
385 * - Should only use AMD method
387 const char *hid = acpi_device_hid(adev);
388 rev_id = strcmp(hid, "AMDI0007") ? 0 : 2;
389 lps0_dsm_func_mask = validate_dsm(adev->handle,
390 ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
391 lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
392 ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
393 &lps0_dsm_guid_microsoft);
394 if (lps0_dsm_func_mask > 0x3 && (!strcmp(hid, "AMD0004") ||
395 !strcmp(hid, "AMD0005") ||
396 !strcmp(hid, "AMDI0005"))) {
397 lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
398 acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
399 ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
400 } else if (lps0_dsm_func_mask_microsoft > 0 && !strcmp(hid, "AMDI0007")) {
401 lps0_dsm_func_mask_microsoft = -EINVAL;
402 acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
406 lps0_dsm_func_mask = validate_dsm(adev->handle,
407 ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
408 lps0_dsm_func_mask_microsoft = -EINVAL;
411 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
412 return 0; //function evaluation failed
414 lps0_device_handle = adev->handle;
416 if (acpi_s2idle_vendor_amd())
417 lpi_device_get_constraints_amd();
419 lpi_device_get_constraints();
422 * Use suspend-to-idle by default if the default suspend mode was not
423 * set from the command line.
425 if (mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3)
426 mem_sleep_current = PM_SUSPEND_TO_IDLE;
429 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
430 * EC GPE to be enabled while suspended for certain wakeup devices to
431 * work, so mark it as wakeup-capable.
433 acpi_ec_mark_gpe_for_wake();
438 static struct acpi_scan_handler lps0_handler = {
439 .ids = lps0_device_ids,
440 .attach = lps0_device_attach,
443 int acpi_s2idle_prepare_late(void)
445 struct acpi_s2idle_dev_ops *handler;
447 if (!lps0_device_handle || sleep_no_lps0)
450 if (pm_debug_messages_on)
451 lpi_check_constraints();
454 if (lps0_dsm_func_mask > 0)
455 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
456 ACPI_LPS0_SCREEN_OFF_AMD :
457 ACPI_LPS0_SCREEN_OFF,
458 lps0_dsm_func_mask, lps0_dsm_guid);
460 if (lps0_dsm_func_mask_microsoft > 0)
461 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
462 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
465 if (lps0_dsm_func_mask > 0)
466 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
467 ACPI_LPS0_ENTRY_AMD :
469 lps0_dsm_func_mask, lps0_dsm_guid);
470 if (lps0_dsm_func_mask_microsoft > 0) {
471 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
472 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
473 /* modern standby entry */
474 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
475 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
478 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
479 if (handler->prepare)
486 void acpi_s2idle_restore_early(void)
488 struct acpi_s2idle_dev_ops *handler;
490 if (!lps0_device_handle || sleep_no_lps0)
493 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
494 if (handler->restore)
497 /* Modern standby exit */
498 if (lps0_dsm_func_mask_microsoft > 0)
499 acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
500 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
503 if (lps0_dsm_func_mask > 0)
504 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
507 lps0_dsm_func_mask, lps0_dsm_guid);
508 if (lps0_dsm_func_mask_microsoft > 0)
509 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
510 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
513 if (lps0_dsm_func_mask_microsoft > 0)
514 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
515 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
516 if (lps0_dsm_func_mask > 0)
517 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
518 ACPI_LPS0_SCREEN_ON_AMD :
520 lps0_dsm_func_mask, lps0_dsm_guid);
523 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
524 .begin = acpi_s2idle_begin,
525 .prepare = acpi_s2idle_prepare,
526 .prepare_late = acpi_s2idle_prepare_late,
527 .wake = acpi_s2idle_wake,
528 .restore_early = acpi_s2idle_restore_early,
529 .restore = acpi_s2idle_restore,
530 .end = acpi_s2idle_end,
533 void acpi_s2idle_setup(void)
535 acpi_scan_add_handler(&lps0_handler);
536 s2idle_set_ops(&acpi_s2idle_ops_lps0);
539 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
541 if (!lps0_device_handle || sleep_no_lps0)
545 list_add(&arg->list_node, &lps0_s2idle_devops_head);
546 unlock_system_sleep();
550 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
552 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
554 if (!lps0_device_handle || sleep_no_lps0)
558 list_del(&arg->list_node);
559 unlock_system_sleep();
561 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
563 #endif /* CONFIG_SUSPEND */