Loading...
Note: File does not exist in v3.1.
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Architecture-specific ACPI-based support for suspend-to-idle.
4 *
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>
8 *
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
15 * mode accordingly.
16 */
17
18#include <linux/acpi.h>
19#include <linux/device.h>
20#include <linux/dmi.h>
21#include <linux/suspend.h>
22
23#include "../sleep.h"
24
25#ifdef CONFIG_SUSPEND
26
27static bool sleep_no_lps0 __read_mostly;
28module_param(sleep_no_lps0, bool, 0644);
29MODULE_PARM_DESC(sleep_no_lps0, "Do not use the special LPS0 device interface");
30
31static const struct acpi_device_id lps0_device_ids[] = {
32 {"PNP0D80", },
33 {"", },
34};
35
36/* Microsoft platform agnostic UUID */
37#define ACPI_LPS0_DSM_UUID_MICROSOFT "11e00d56-ce64-47ce-837b-1f898f9aa461"
38
39#define ACPI_LPS0_DSM_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
40
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
48
49/* AMD */
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
55
56static acpi_handle lps0_device_handle;
57static guid_t lps0_dsm_guid;
58static int lps0_dsm_func_mask;
59
60static guid_t lps0_dsm_guid_microsoft;
61static int lps0_dsm_func_mask_microsoft;
62
63/* Device constraint entry structure */
64struct lpi_device_info {
65 char *name;
66 int enabled;
67 union acpi_object *package;
68};
69
70/* Constraint package structure */
71struct lpi_device_constraint {
72 int uid;
73 int min_dstate;
74 int function_states;
75};
76
77struct lpi_constraints {
78 acpi_handle handle;
79 int min_dstate;
80};
81
82/* AMD Constraint package structure */
83struct lpi_device_constraint_amd {
84 char *name;
85 int enabled;
86 int function_states;
87 int min_dstate;
88};
89
90static LIST_HEAD(lps0_s2idle_devops_head);
91
92static struct lpi_constraints *lpi_constraints_table;
93static int lpi_constraints_table_size;
94static int rev_id;
95
96static void lpi_device_get_constraints_amd(void)
97{
98 union acpi_object *out_obj;
99 int i, j, k;
100
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);
104
105 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
106 out_obj ? "successful" : "failed");
107
108 if (!out_obj)
109 return;
110
111 for (i = 0; i < out_obj->package.count; i++) {
112 union acpi_object *package = &out_obj->package.elements[i];
113
114 if (package->type == ACPI_TYPE_PACKAGE) {
115 lpi_constraints_table = kcalloc(package->package.count,
116 sizeof(*lpi_constraints_table),
117 GFP_KERNEL);
118
119 if (!lpi_constraints_table)
120 goto free_acpi_buffer;
121
122 acpi_handle_debug(lps0_device_handle,
123 "LPI: constraints list begin:\n");
124
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;
129 acpi_status status;
130
131 for (k = 0; k < info_obj->package.count; ++k) {
132 union acpi_object *obj = &info_obj->package.elements[k];
133
134 list = &lpi_constraints_table[lpi_constraints_table_size];
135 list->min_dstate = -1;
136
137 switch (k) {
138 case 0:
139 dev_info.enabled = obj->integer.value;
140 break;
141 case 1:
142 dev_info.name = obj->string.pointer;
143 break;
144 case 2:
145 dev_info.function_states = obj->integer.value;
146 break;
147 case 3:
148 dev_info.min_dstate = obj->integer.value;
149 break;
150 }
151
152 if (!dev_info.enabled || !dev_info.name ||
153 !dev_info.min_dstate)
154 continue;
155
156 status = acpi_get_handle(NULL, dev_info.name,
157 &list->handle);
158 if (ACPI_FAILURE(status))
159 continue;
160
161 acpi_handle_debug(lps0_device_handle,
162 "Name:%s\n", dev_info.name);
163
164 list->min_dstate = dev_info.min_dstate;
165
166 if (list->min_dstate < 0) {
167 acpi_handle_debug(lps0_device_handle,
168 "Incomplete constraint defined\n");
169 continue;
170 }
171 }
172 lpi_constraints_table_size++;
173 }
174 }
175 }
176
177 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
178
179free_acpi_buffer:
180 ACPI_FREE(out_obj);
181}
182
183static void lpi_device_get_constraints(void)
184{
185 union acpi_object *out_obj;
186 int i;
187
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);
191
192 acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
193 out_obj ? "successful" : "failed");
194
195 if (!out_obj)
196 return;
197
198 lpi_constraints_table = kcalloc(out_obj->package.count,
199 sizeof(*lpi_constraints_table),
200 GFP_KERNEL);
201 if (!lpi_constraints_table)
202 goto free_acpi_buffer;
203
204 acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
205
206 for (i = 0; i < out_obj->package.count; i++) {
207 struct lpi_constraints *constraint;
208 acpi_status status;
209 union acpi_object *package = &out_obj->package.elements[i];
210 struct lpi_device_info info = { };
211 int package_count = 0, j;
212
213 if (!package)
214 continue;
215
216 for (j = 0; j < package->package.count; ++j) {
217 union acpi_object *element =
218 &(package->package.elements[j]);
219
220 switch (element->type) {
221 case ACPI_TYPE_INTEGER:
222 info.enabled = element->integer.value;
223 break;
224 case ACPI_TYPE_STRING:
225 info.name = element->string.pointer;
226 break;
227 case ACPI_TYPE_PACKAGE:
228 package_count = element->package.count;
229 info.package = element->package.elements;
230 break;
231 }
232 }
233
234 if (!info.enabled || !info.package || !info.name)
235 continue;
236
237 constraint = &lpi_constraints_table[lpi_constraints_table_size];
238
239 status = acpi_get_handle(NULL, info.name, &constraint->handle);
240 if (ACPI_FAILURE(status))
241 continue;
242
243 acpi_handle_debug(lps0_device_handle,
244 "index:%d Name:%s\n", i, info.name);
245
246 constraint->min_dstate = -1;
247
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;
253
254 switch (info_obj->type) {
255 case ACPI_TYPE_INTEGER:
256 /* version */
257 break;
258 case ACPI_TYPE_PACKAGE:
259 if (info_obj->package.count < 2)
260 break;
261
262 cnstr_pkg = info_obj->package.elements;
263 obj = &cnstr_pkg[0];
264 dev_info.uid = obj->integer.value;
265 obj = &cnstr_pkg[1];
266 dev_info.min_dstate = obj->integer.value;
267
268 acpi_handle_debug(lps0_device_handle,
269 "uid:%d min_dstate:%s\n",
270 dev_info.uid,
271 acpi_power_state_string(dev_info.min_dstate));
272
273 constraint->min_dstate = dev_info.min_dstate;
274 break;
275 }
276 }
277
278 if (constraint->min_dstate < 0) {
279 acpi_handle_debug(lps0_device_handle,
280 "Incomplete constraint defined\n");
281 continue;
282 }
283
284 lpi_constraints_table_size++;
285 }
286
287 acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
288
289free_acpi_buffer:
290 ACPI_FREE(out_obj);
291}
292
293static void lpi_check_constraints(void)
294{
295 int i;
296
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);
300
301 if (!adev)
302 continue;
303
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));
308
309 if (!adev->flags.power_manageable) {
310 acpi_handle_info(handle, "LPI: Device not power manageable\n");
311 lpi_constraints_table[i].handle = NULL;
312 continue;
313 }
314
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));
320 }
321}
322
323static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
324{
325 union acpi_object *out_obj;
326
327 if (!(func_mask & (1 << func)))
328 return;
329
330 out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
331 rev_id, func, NULL);
332 ACPI_FREE(out_obj);
333
334 acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
335 func, out_obj ? "successful" : "failed");
336}
337
338static bool acpi_s2idle_vendor_amd(void)
339{
340 return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
341}
342
343static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
344{
345 union acpi_object *obj;
346 int ret = -EINVAL;
347
348 guid_parse(uuid, dsm_guid);
349 obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
350
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);
356 goto out;
357 }
358
359 ret = *(int *)obj->buffer.pointer;
360 acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
361
362out:
363 ACPI_FREE(obj);
364 return ret;
365}
366
367struct amd_lps0_hid_device_data {
368 const bool check_off_by_one;
369};
370
371static const struct amd_lps0_hid_device_data amd_picasso = {
372 .check_off_by_one = true,
373};
374
375static const struct amd_lps0_hid_device_data amd_cezanne = {
376 .check_off_by_one = false,
377};
378
379static 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, },
384 {}
385};
386
387static int lps0_prefer_amd(const struct dmi_system_id *id)
388{
389 pr_debug("Using AMD GUID w/ _REV 2.\n");
390 rev_id = 2;
391 return 0;
392}
393static const struct dmi_system_id s2idle_dmi_table[] __initconst = {
394 {
395 /*
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.
400 */
401 .callback = lps0_prefer_amd,
402 .matches = {
403 DMI_MATCH(DMI_BOARD_VENDOR, "HP"),
404 DMI_MATCH(DMI_BOARD_NAME, "8990"),
405 },
406 },
407 {}
408};
409
410static int lps0_device_attach(struct acpi_device *adev,
411 const struct acpi_device_id *not_used)
412{
413 if (lps0_device_handle)
414 return 0;
415
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;
422
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))
425 break;
426 if (dev_id->id[0])
427 data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
428 else
429 data = &amd_cezanne;
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");
439 }
440 } else {
441 rev_id = 1;
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;
445 }
446
447 if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
448 return 0; //function evaluation failed
449
450 lps0_device_handle = adev->handle;
451
452 if (acpi_s2idle_vendor_amd())
453 lpi_device_get_constraints_amd();
454 else
455 lpi_device_get_constraints();
456
457 /*
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
460 * line.
461 */
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");
466 }
467
468 /*
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.
472 */
473 acpi_ec_mark_gpe_for_wake();
474
475 return 0;
476}
477
478static struct acpi_scan_handler lps0_handler = {
479 .ids = lps0_device_ids,
480 .attach = lps0_device_attach,
481};
482
483int acpi_s2idle_prepare_late(void)
484{
485 struct acpi_s2idle_dev_ops *handler;
486
487 if (!lps0_device_handle || sleep_no_lps0)
488 return 0;
489
490 if (pm_debug_messages_on)
491 lpi_check_constraints();
492
493 /* Screen off */
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);
499
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);
503
504 /* LPS0 entry */
505 if (lps0_dsm_func_mask > 0)
506 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
507 ACPI_LPS0_ENTRY_AMD :
508 ACPI_LPS0_ENTRY,
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);
516 }
517
518 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
519 if (handler->prepare)
520 handler->prepare();
521 }
522
523 return 0;
524}
525
526void acpi_s2idle_check(void)
527{
528 struct acpi_s2idle_dev_ops *handler;
529
530 if (!lps0_device_handle || sleep_no_lps0)
531 return;
532
533 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
534 if (handler->check)
535 handler->check();
536 }
537}
538
539void acpi_s2idle_restore_early(void)
540{
541 struct acpi_s2idle_dev_ops *handler;
542
543 if (!lps0_device_handle || sleep_no_lps0)
544 return;
545
546 list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
547 if (handler->restore)
548 handler->restore();
549
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);
554
555 /* LPS0 exit */
556 if (lps0_dsm_func_mask > 0)
557 acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
558 ACPI_LPS0_EXIT_AMD :
559 ACPI_LPS0_EXIT,
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);
564
565 /* Screen on */
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 :
572 ACPI_LPS0_SCREEN_ON,
573 lps0_dsm_func_mask, lps0_dsm_guid);
574}
575
576static 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,
585};
586
587void __init acpi_s2idle_setup(void)
588{
589 dmi_check_system(s2idle_dmi_table);
590 acpi_scan_add_handler(&lps0_handler);
591 s2idle_set_ops(&acpi_s2idle_ops_lps0);
592}
593
594int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
595{
596 unsigned int sleep_flags;
597
598 if (!lps0_device_handle || sleep_no_lps0)
599 return -ENODEV;
600
601 sleep_flags = lock_system_sleep();
602 list_add(&arg->list_node, &lps0_s2idle_devops_head);
603 unlock_system_sleep(sleep_flags);
604
605 return 0;
606}
607EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
608
609void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
610{
611 unsigned int sleep_flags;
612
613 if (!lps0_device_handle || sleep_no_lps0)
614 return;
615
616 sleep_flags = lock_system_sleep();
617 list_del(&arg->list_node);
618 unlock_system_sleep(sleep_flags);
619}
620EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
621
622#endif /* CONFIG_SUSPEND */