xref: /openbmc/linux/drivers/acpi/x86/s2idle.c (revision f4356947)
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 
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");
30 
31 static 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 
56 static acpi_handle lps0_device_handle;
57 static guid_t lps0_dsm_guid;
58 static int lps0_dsm_func_mask;
59 
60 static guid_t lps0_dsm_guid_microsoft;
61 static int lps0_dsm_func_mask_microsoft;
62 
63 /* Device constraint entry structure */
64 struct lpi_device_info {
65 	char *name;
66 	int enabled;
67 	union acpi_object *package;
68 };
69 
70 /* Constraint package structure */
71 struct lpi_device_constraint {
72 	int uid;
73 	int min_dstate;
74 	int function_states;
75 };
76 
77 struct lpi_constraints {
78 	acpi_handle handle;
79 	int min_dstate;
80 };
81 
82 /* AMD Constraint package structure */
83 struct lpi_device_constraint_amd {
84 	char *name;
85 	int enabled;
86 	int function_states;
87 	int min_dstate;
88 };
89 
90 static LIST_HEAD(lps0_s2idle_devops_head);
91 
92 static struct lpi_constraints *lpi_constraints_table;
93 static int lpi_constraints_table_size;
94 static int rev_id;
95 
96 static 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 
179 free_acpi_buffer:
180 	ACPI_FREE(out_obj);
181 }
182 
183 static 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 
289 free_acpi_buffer:
290 	ACPI_FREE(out_obj);
291 }
292 
293 static 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 
323 static 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 
338 static bool acpi_s2idle_vendor_amd(void)
339 {
340 	return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
341 }
342 
343 static 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 
362 out:
363 	ACPI_FREE(obj);
364 	return ret;
365 }
366 
367 struct amd_lps0_hid_device_data {
368 	const bool check_off_by_one;
369 };
370 
371 static const struct amd_lps0_hid_device_data amd_picasso = {
372 	.check_off_by_one = true,
373 };
374 
375 static const struct amd_lps0_hid_device_data amd_cezanne = {
376 	.check_off_by_one = false,
377 };
378 
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,	},
384 	{}
385 };
386 
387 static int lps0_device_attach(struct acpi_device *adev,
388 			      const struct acpi_device_id *not_used)
389 {
390 	if (lps0_device_handle)
391 		return 0;
392 
393 	lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
394 						    ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
395 						    &lps0_dsm_guid_microsoft);
396 	if (acpi_s2idle_vendor_amd()) {
397 		static const struct acpi_device_id *dev_id;
398 		const struct amd_lps0_hid_device_data *data;
399 
400 		for (dev_id = &amd_hid_ids[0]; dev_id->id[0]; dev_id++)
401 			if (acpi_dev_hid_uid_match(adev, dev_id->id, NULL))
402 				break;
403 		if (dev_id->id[0])
404 			data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
405 		else
406 			data = &amd_cezanne;
407 		lps0_dsm_func_mask = validate_dsm(adev->handle,
408 					ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
409 		if (lps0_dsm_func_mask > 0x3 && data->check_off_by_one) {
410 			lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
411 			acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
412 					  ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
413 		} else if (lps0_dsm_func_mask_microsoft > 0 && rev_id) {
414 			lps0_dsm_func_mask_microsoft = -EINVAL;
415 			acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
416 		}
417 	} else {
418 		rev_id = 1;
419 		lps0_dsm_func_mask = validate_dsm(adev->handle,
420 					ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
421 		lps0_dsm_func_mask_microsoft = -EINVAL;
422 	}
423 
424 	if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
425 		return 0; //function evaluation failed
426 
427 	lps0_device_handle = adev->handle;
428 
429 	if (acpi_s2idle_vendor_amd())
430 		lpi_device_get_constraints_amd();
431 	else
432 		lpi_device_get_constraints();
433 
434 	/*
435 	 * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in
436 	 * the FADT and the default suspend mode was not set from the command
437 	 * line.
438 	 */
439 	if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) &&
440 	    mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) {
441 		mem_sleep_current = PM_SUSPEND_TO_IDLE;
442 		pr_info("Low-power S0 idle used by default for system suspend\n");
443 	}
444 
445 	/*
446 	 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
447 	 * EC GPE to be enabled while suspended for certain wakeup devices to
448 	 * work, so mark it as wakeup-capable.
449 	 */
450 	acpi_ec_mark_gpe_for_wake();
451 
452 	return 0;
453 }
454 
455 static struct acpi_scan_handler lps0_handler = {
456 	.ids = lps0_device_ids,
457 	.attach = lps0_device_attach,
458 };
459 
460 int acpi_s2idle_prepare_late(void)
461 {
462 	struct acpi_s2idle_dev_ops *handler;
463 
464 	if (!lps0_device_handle || sleep_no_lps0)
465 		return 0;
466 
467 	if (pm_debug_messages_on)
468 		lpi_check_constraints();
469 
470 	/* Screen off */
471 	if (lps0_dsm_func_mask > 0)
472 		acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
473 					ACPI_LPS0_SCREEN_OFF_AMD :
474 					ACPI_LPS0_SCREEN_OFF,
475 					lps0_dsm_func_mask, lps0_dsm_guid);
476 
477 	if (lps0_dsm_func_mask_microsoft > 0)
478 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
479 				lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
480 
481 	/* LPS0 entry */
482 	if (lps0_dsm_func_mask > 0)
483 		acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
484 					ACPI_LPS0_ENTRY_AMD :
485 					ACPI_LPS0_ENTRY,
486 					lps0_dsm_func_mask, lps0_dsm_guid);
487 	if (lps0_dsm_func_mask_microsoft > 0) {
488 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
489 				lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
490 		/* modern standby entry */
491 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
492 				lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
493 	}
494 
495 	list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
496 		if (handler->prepare)
497 			handler->prepare();
498 	}
499 
500 	return 0;
501 }
502 
503 void acpi_s2idle_check(void)
504 {
505 	struct acpi_s2idle_dev_ops *handler;
506 
507 	if (!lps0_device_handle || sleep_no_lps0)
508 		return;
509 
510 	list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
511 		if (handler->check)
512 			handler->check();
513 	}
514 }
515 
516 void acpi_s2idle_restore_early(void)
517 {
518 	struct acpi_s2idle_dev_ops *handler;
519 
520 	if (!lps0_device_handle || sleep_no_lps0)
521 		return;
522 
523 	list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
524 		if (handler->restore)
525 			handler->restore();
526 
527 	/* Modern standby exit */
528 	if (lps0_dsm_func_mask_microsoft > 0)
529 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
530 				lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
531 
532 	/* LPS0 exit */
533 	if (lps0_dsm_func_mask > 0)
534 		acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
535 					ACPI_LPS0_EXIT_AMD :
536 					ACPI_LPS0_EXIT,
537 					lps0_dsm_func_mask, lps0_dsm_guid);
538 	if (lps0_dsm_func_mask_microsoft > 0)
539 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
540 				lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
541 
542 	/* Screen on */
543 	if (lps0_dsm_func_mask_microsoft > 0)
544 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
545 				lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
546 	if (lps0_dsm_func_mask > 0)
547 		acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
548 					ACPI_LPS0_SCREEN_ON_AMD :
549 					ACPI_LPS0_SCREEN_ON,
550 					lps0_dsm_func_mask, lps0_dsm_guid);
551 }
552 
553 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
554 	.begin = acpi_s2idle_begin,
555 	.prepare = acpi_s2idle_prepare,
556 	.prepare_late = acpi_s2idle_prepare_late,
557 	.check = acpi_s2idle_check,
558 	.wake = acpi_s2idle_wake,
559 	.restore_early = acpi_s2idle_restore_early,
560 	.restore = acpi_s2idle_restore,
561 	.end = acpi_s2idle_end,
562 };
563 
564 void __init acpi_s2idle_setup(void)
565 {
566 	acpi_scan_add_handler(&lps0_handler);
567 	s2idle_set_ops(&acpi_s2idle_ops_lps0);
568 }
569 
570 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
571 {
572 	unsigned int sleep_flags;
573 
574 	if (!lps0_device_handle || sleep_no_lps0)
575 		return -ENODEV;
576 
577 	sleep_flags = lock_system_sleep();
578 	list_add(&arg->list_node, &lps0_s2idle_devops_head);
579 	unlock_system_sleep(sleep_flags);
580 
581 	return 0;
582 }
583 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
584 
585 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
586 {
587 	unsigned int sleep_flags;
588 
589 	if (!lps0_device_handle || sleep_no_lps0)
590 		return;
591 
592 	sleep_flags = lock_system_sleep();
593 	list_del(&arg->list_node);
594 	unlock_system_sleep(sleep_flags);
595 }
596 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
597 
598 #endif /* CONFIG_SUSPEND */
599