xref: /linux/drivers/acpi/sleep.c (revision ac9eafbe930abb589e9289842a99cc575cadb854)
155716d26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d08ca2caSLen Brown /*
3d08ca2caSLen Brown  * sleep.c - ACPI sleep support.
4d08ca2caSLen Brown  *
5d08ca2caSLen Brown  * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
6d08ca2caSLen Brown  * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
7d08ca2caSLen Brown  * Copyright (c) 2000-2003 Patrick Mochel
8d08ca2caSLen Brown  * Copyright (c) 2003 Open Source Development Lab
9d08ca2caSLen Brown  */
10d08ca2caSLen Brown 
11d08ca2caSLen Brown #include <linux/delay.h>
12d08ca2caSLen Brown #include <linux/irq.h>
13d08ca2caSLen Brown #include <linux/dmi.h>
14d08ca2caSLen Brown #include <linux/device.h>
15a8d46b9eSRafael J. Wysocki #include <linux/interrupt.h>
16d08ca2caSLen Brown #include <linux/suspend.h>
17d08ca2caSLen Brown #include <linux/reboot.h>
18d1ee4335SH. Peter Anvin #include <linux/acpi.h>
19a2ef5c4fSLin Ming #include <linux/module.h>
20504997cfSSudeep Holla #include <linux/syscore_ops.h>
21d08ca2caSLen Brown #include <asm/io.h>
22bb3632c6STodd E Brandt #include <trace/events/power.h>
23d08ca2caSLen Brown 
24e60cc7a6SBjorn Helgaas #include "internal.h"
25d08ca2caSLen Brown #include "sleep.h"
26d08ca2caSLen Brown 
2713737181SChen Yu /*
2813737181SChen Yu  * Some HW-full platforms do not have _S5, so they may need
2913737181SChen Yu  * to leverage efi power off for a shutdown.
3013737181SChen Yu  */
3113737181SChen Yu bool acpi_no_s5;
3201eac60bSStephen Hemminger static u8 sleep_states[ACPI_S_STATE_COUNT];
33d08ca2caSLen Brown 
34d08ca2caSLen Brown static void acpi_sleep_tts_switch(u32 acpi_state)
35d08ca2caSLen Brown {
360db98202SJiang Liu 	acpi_status status;
37d08ca2caSLen Brown 
380db98202SJiang Liu 	status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
39d08ca2caSLen Brown 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
40d08ca2caSLen Brown 		/*
41d08ca2caSLen Brown 		 * OS can't evaluate the _TTS object correctly. Some warning
42d08ca2caSLen Brown 		 * message will be printed. But it won't break anything.
43d08ca2caSLen Brown 		 */
44d08ca2caSLen Brown 		printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
45d08ca2caSLen Brown 	}
46d08ca2caSLen Brown }
47d08ca2caSLen Brown 
489713adc2SRafael J. Wysocki static int tts_notify_reboot(struct notifier_block *this,
49d08ca2caSLen Brown 			unsigned long code, void *x)
50d08ca2caSLen Brown {
51d08ca2caSLen Brown 	acpi_sleep_tts_switch(ACPI_STATE_S5);
52d08ca2caSLen Brown 	return NOTIFY_DONE;
53d08ca2caSLen Brown }
54d08ca2caSLen Brown 
559713adc2SRafael J. Wysocki static struct notifier_block tts_notifier = {
569713adc2SRafael J. Wysocki 	.notifier_call	= tts_notify_reboot,
57d08ca2caSLen Brown 	.next		= NULL,
58d08ca2caSLen Brown 	.priority	= 0,
59d08ca2caSLen Brown };
60d08ca2caSLen Brown 
61d08ca2caSLen Brown static int acpi_sleep_prepare(u32 acpi_state)
62d08ca2caSLen Brown {
63d08ca2caSLen Brown #ifdef CONFIG_ACPI_SLEEP
64d08ca2caSLen Brown 	/* do we have a wakeup address for S2 and S3? */
65d08ca2caSLen Brown 	if (acpi_state == ACPI_STATE_S3) {
66319b6ffcSH. Peter Anvin 		if (!acpi_wakeup_address)
67d08ca2caSLen Brown 			return -EFAULT;
68e3e9b577SRafael J. Wysocki 		acpi_set_waking_vector(acpi_wakeup_address);
69d08ca2caSLen Brown 
70d08ca2caSLen Brown 	}
71d08ca2caSLen Brown 	ACPI_FLUSH_CPU_CACHE();
72d08ca2caSLen Brown #endif
73d08ca2caSLen Brown 	printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
74d08ca2caSLen Brown 		acpi_state);
7578f5f023SRafael J. Wysocki 	acpi_enable_wakeup_devices(acpi_state);
76d08ca2caSLen Brown 	acpi_enter_sleep_state_prep(acpi_state);
77d08ca2caSLen Brown 	return 0;
78d08ca2caSLen Brown }
79d08ca2caSLen Brown 
80ad5a449bSDexuan Cui bool acpi_sleep_state_supported(u8 sleep_state)
81a4e90bedSRafael J. Wysocki {
82a4e90bedSRafael J. Wysocki 	acpi_status status;
83a4e90bedSRafael J. Wysocki 	u8 type_a, type_b;
84a4e90bedSRafael J. Wysocki 
85a4e90bedSRafael J. Wysocki 	status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
86a4e90bedSRafael J. Wysocki 	return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
87a4e90bedSRafael J. Wysocki 		|| (acpi_gbl_FADT.sleep_control.address
88a4e90bedSRafael J. Wysocki 			&& acpi_gbl_FADT.sleep_status.address));
89a4e90bedSRafael J. Wysocki }
90a4e90bedSRafael J. Wysocki 
91d08ca2caSLen Brown #ifdef CONFIG_ACPI_SLEEP
92068b47d0SRafael J. Wysocki static bool sleep_no_lps0 __read_mostly;
93068b47d0SRafael J. Wysocki module_param(sleep_no_lps0, bool, 0644);
94068b47d0SRafael J. Wysocki MODULE_PARM_DESC(sleep_no_lps0, "Do not use the special LPS0 device interface");
95068b47d0SRafael J. Wysocki 
96091aad6aSJan Beulich static u32 acpi_target_sleep_state = ACPI_STATE_S0;
97a6ae7594SRafael J. Wysocki 
98a6ae7594SRafael J. Wysocki u32 acpi_target_system_state(void)
99a6ae7594SRafael J. Wysocki {
100a6ae7594SRafael J. Wysocki 	return acpi_target_sleep_state;
101a6ae7594SRafael J. Wysocki }
102fad16dd9SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_target_system_state);
103a6ae7594SRafael J. Wysocki 
104a5ca7345SRafael J. Wysocki static bool pwr_btn_event_pending;
105091aad6aSJan Beulich 
106d7f0eea9SZhang Rui /*
10772ad5d77SRafael J. Wysocki  * The ACPI specification wants us to save NVS memory regions during hibernation
10872ad5d77SRafael J. Wysocki  * and to restore them during the subsequent resume.  Windows does that also for
10972ad5d77SRafael J. Wysocki  * suspend to RAM.  However, it is known that this mechanism does not work on
11072ad5d77SRafael J. Wysocki  * all machines, so we allow the user to disable it with the help of the
11172ad5d77SRafael J. Wysocki  * 'acpi_sleep=nonvs' kernel command line option.
11272ad5d77SRafael J. Wysocki  */
11372ad5d77SRafael J. Wysocki static bool nvs_nosave;
11472ad5d77SRafael J. Wysocki 
11572ad5d77SRafael J. Wysocki void __init acpi_nvs_nosave(void)
11672ad5d77SRafael J. Wysocki {
11772ad5d77SRafael J. Wysocki 	nvs_nosave = true;
11872ad5d77SRafael J. Wysocki }
11972ad5d77SRafael J. Wysocki 
12072ad5d77SRafael J. Wysocki /*
1211bad2f19SKristen Carlson Accardi  * The ACPI specification wants us to save NVS memory regions during hibernation
1221bad2f19SKristen Carlson Accardi  * but says nothing about saving NVS during S3.  Not all versions of Windows
1231bad2f19SKristen Carlson Accardi  * save NVS on S3 suspend either, and it is clear that not all systems need
1241bad2f19SKristen Carlson Accardi  * NVS to be saved at S3 time.  To improve suspend/resume time, allow the
1251bad2f19SKristen Carlson Accardi  * user to disable saving NVS on S3 if their system does not require it, but
1261bad2f19SKristen Carlson Accardi  * continue to save/restore NVS for S4 as specified.
1271bad2f19SKristen Carlson Accardi  */
1281bad2f19SKristen Carlson Accardi static bool nvs_nosave_s3;
1291bad2f19SKristen Carlson Accardi 
1301bad2f19SKristen Carlson Accardi void __init acpi_nvs_nosave_s3(void)
1311bad2f19SKristen Carlson Accardi {
1321bad2f19SKristen Carlson Accardi 	nvs_nosave_s3 = true;
1331bad2f19SKristen Carlson Accardi }
1341bad2f19SKristen Carlson Accardi 
135cbc00c13SZhang Rui static int __init init_nvs_save_s3(const struct dmi_system_id *d)
136cbc00c13SZhang Rui {
137cbc00c13SZhang Rui 	nvs_nosave_s3 = false;
138cbc00c13SZhang Rui 	return 0;
139cbc00c13SZhang Rui }
140cbc00c13SZhang Rui 
1411bad2f19SKristen Carlson Accardi /*
142d08ca2caSLen Brown  * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
143d08ca2caSLen Brown  * user to request that behavior by using the 'acpi_old_suspend_ordering'
144d08ca2caSLen Brown  * kernel command line option that causes the following variable to be set.
145d08ca2caSLen Brown  */
146d08ca2caSLen Brown static bool old_suspend_ordering;
147d08ca2caSLen Brown 
148d08ca2caSLen Brown void __init acpi_old_suspend_ordering(void)
149d08ca2caSLen Brown {
150d08ca2caSLen Brown 	old_suspend_ordering = true;
151d08ca2caSLen Brown }
152d08ca2caSLen Brown 
153d08ca2caSLen Brown static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
154d08ca2caSLen Brown {
1550ac1b1d7SZhang Rui 	acpi_old_suspend_ordering();
156d08ca2caSLen Brown 	return 0;
157d08ca2caSLen Brown }
158d08ca2caSLen Brown 
15953998648SRafael J. Wysocki static int __init init_nvs_nosave(const struct dmi_system_id *d)
16053998648SRafael J. Wysocki {
16153998648SRafael J. Wysocki 	acpi_nvs_nosave();
16253998648SRafael J. Wysocki 	return 0;
16353998648SRafael J. Wysocki }
16453998648SRafael J. Wysocki 
165068b47d0SRafael J. Wysocki static bool acpi_sleep_default_s3;
16671630b7aSRafael J. Wysocki 
167068b47d0SRafael J. Wysocki static int __init init_default_s3(const struct dmi_system_id *d)
16871630b7aSRafael J. Wysocki {
169068b47d0SRafael J. Wysocki 	acpi_sleep_default_s3 = true;
17071630b7aSRafael J. Wysocki 	return 0;
17171630b7aSRafael J. Wysocki }
17271630b7aSRafael J. Wysocki 
1736faadbbbSChristoph Hellwig static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
174d08ca2caSLen Brown 	{
175d08ca2caSLen Brown 	.callback = init_old_suspend_ordering,
176d08ca2caSLen Brown 	.ident = "Abit KN9 (nForce4 variant)",
177d08ca2caSLen Brown 	.matches = {
178d08ca2caSLen Brown 		DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
179d08ca2caSLen Brown 		DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
180d08ca2caSLen Brown 		},
181d08ca2caSLen Brown 	},
182d08ca2caSLen Brown 	{
183d08ca2caSLen Brown 	.callback = init_old_suspend_ordering,
184d08ca2caSLen Brown 	.ident = "HP xw4600 Workstation",
185d08ca2caSLen Brown 	.matches = {
186d08ca2caSLen Brown 		DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
187d08ca2caSLen Brown 		DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
188d08ca2caSLen Brown 		},
189d08ca2caSLen Brown 	},
190d08ca2caSLen Brown 	{
191a1404495SAndy Whitcroft 	.callback = init_old_suspend_ordering,
192a1404495SAndy Whitcroft 	.ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
193a1404495SAndy Whitcroft 	.matches = {
194a1404495SAndy Whitcroft 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
195a1404495SAndy Whitcroft 		DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
196a1404495SAndy Whitcroft 		},
197a1404495SAndy Whitcroft 	},
19845e77988SZhang Rui 	{
1992a9ef8e1SZhao Yakui 	.callback = init_old_suspend_ordering,
2002a9ef8e1SZhao Yakui 	.ident = "Panasonic CF51-2L",
2012a9ef8e1SZhao Yakui 	.matches = {
2022a9ef8e1SZhao Yakui 		DMI_MATCH(DMI_BOARD_VENDOR,
2032a9ef8e1SZhao Yakui 				"Matsushita Electric Industrial Co.,Ltd."),
2042a9ef8e1SZhao Yakui 		DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
2052a9ef8e1SZhao Yakui 		},
2062a9ef8e1SZhao Yakui 	},
20753998648SRafael J. Wysocki 	{
20853998648SRafael J. Wysocki 	.callback = init_nvs_nosave,
20966f2fda9SJoseph Salisbury 	.ident = "Sony Vaio VGN-FW41E_H",
21066f2fda9SJoseph Salisbury 	.matches = {
21166f2fda9SJoseph Salisbury 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
21266f2fda9SJoseph Salisbury 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
21366f2fda9SJoseph Salisbury 		},
21466f2fda9SJoseph Salisbury 	},
21566f2fda9SJoseph Salisbury 	{
21666f2fda9SJoseph Salisbury 	.callback = init_nvs_nosave,
217d11c78e9SDave Jones 	.ident = "Sony Vaio VGN-FW21E",
218d11c78e9SDave Jones 	.matches = {
219d11c78e9SDave Jones 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
220d11c78e9SDave Jones 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
221d11c78e9SDave Jones 		},
222d11c78e9SDave Jones 	},
223d11c78e9SDave Jones 	{
224d11c78e9SDave Jones 	.callback = init_nvs_nosave,
225469dd1c4SFabio Valentini 	.ident = "Sony Vaio VGN-FW21M",
226469dd1c4SFabio Valentini 	.matches = {
227469dd1c4SFabio Valentini 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
228469dd1c4SFabio Valentini 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
229469dd1c4SFabio Valentini 		},
230469dd1c4SFabio Valentini 	},
231469dd1c4SFabio Valentini 	{
232469dd1c4SFabio Valentini 	.callback = init_nvs_nosave,
233ddf6ce45SDave Jones 	.ident = "Sony Vaio VPCEB17FX",
234ddf6ce45SDave Jones 	.matches = {
235ddf6ce45SDave Jones 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
236ddf6ce45SDave Jones 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
237ddf6ce45SDave Jones 		},
238ddf6ce45SDave Jones 	},
239ddf6ce45SDave Jones 	{
240ddf6ce45SDave Jones 	.callback = init_nvs_nosave,
24153998648SRafael J. Wysocki 	.ident = "Sony Vaio VGN-SR11M",
24253998648SRafael J. Wysocki 	.matches = {
24353998648SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
24453998648SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
24553998648SRafael J. Wysocki 		},
24653998648SRafael J. Wysocki 	},
24753998648SRafael J. Wysocki 	{
24853998648SRafael J. Wysocki 	.callback = init_nvs_nosave,
24953998648SRafael J. Wysocki 	.ident = "Everex StepNote Series",
25053998648SRafael J. Wysocki 	.matches = {
25153998648SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
25253998648SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
25353998648SRafael J. Wysocki 		},
25453998648SRafael J. Wysocki 	},
255af48931cSRafael J. Wysocki 	{
256af48931cSRafael J. Wysocki 	.callback = init_nvs_nosave,
257af48931cSRafael J. Wysocki 	.ident = "Sony Vaio VPCEB1Z1E",
258af48931cSRafael J. Wysocki 	.matches = {
259af48931cSRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
260af48931cSRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
261af48931cSRafael J. Wysocki 		},
262af48931cSRafael J. Wysocki 	},
263291a73c9SRafael J. Wysocki 	{
264291a73c9SRafael J. Wysocki 	.callback = init_nvs_nosave,
265291a73c9SRafael J. Wysocki 	.ident = "Sony Vaio VGN-NW130D",
266291a73c9SRafael J. Wysocki 	.matches = {
267291a73c9SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
268291a73c9SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
269291a73c9SRafael J. Wysocki 		},
270291a73c9SRafael J. Wysocki 	},
2717b330707SRafael J. Wysocki 	{
2727b330707SRafael J. Wysocki 	.callback = init_nvs_nosave,
27393f77084SLan Tianyu 	.ident = "Sony Vaio VPCCW29FX",
27493f77084SLan Tianyu 	.matches = {
27593f77084SLan Tianyu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
27693f77084SLan Tianyu 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
27793f77084SLan Tianyu 		},
27893f77084SLan Tianyu 	},
27993f77084SLan Tianyu 	{
28093f77084SLan Tianyu 	.callback = init_nvs_nosave,
2817b330707SRafael J. Wysocki 	.ident = "Averatec AV1020-ED2",
2827b330707SRafael J. Wysocki 	.matches = {
2837b330707SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
2847b330707SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
2857b330707SRafael J. Wysocki 		},
2867b330707SRafael J. Wysocki 	},
287bb0c5ed6SZhang Rui 	{
288bb0c5ed6SZhang Rui 	.callback = init_old_suspend_ordering,
289bb0c5ed6SZhang Rui 	.ident = "Asus A8N-SLI DELUXE",
290bb0c5ed6SZhang Rui 	.matches = {
291bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
292bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
293bb0c5ed6SZhang Rui 		},
294bb0c5ed6SZhang Rui 	},
295bb0c5ed6SZhang Rui 	{
296bb0c5ed6SZhang Rui 	.callback = init_old_suspend_ordering,
297bb0c5ed6SZhang Rui 	.ident = "Asus A8N-SLI Premium",
298bb0c5ed6SZhang Rui 	.matches = {
299bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
300bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
301bb0c5ed6SZhang Rui 		},
302bb0c5ed6SZhang Rui 	},
30389e8ea12SRafael J. Wysocki 	{
30489e8ea12SRafael J. Wysocki 	.callback = init_nvs_nosave,
30589e8ea12SRafael J. Wysocki 	.ident = "Sony Vaio VGN-SR26GN_P",
30689e8ea12SRafael J. Wysocki 	.matches = {
30789e8ea12SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
30889e8ea12SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
30989e8ea12SRafael J. Wysocki 		},
31089e8ea12SRafael J. Wysocki 	},
311731b25a4SBogdan Radulescu 	{
312731b25a4SBogdan Radulescu 	.callback = init_nvs_nosave,
313876ab790SLan Tianyu 	.ident = "Sony Vaio VPCEB1S1E",
314876ab790SLan Tianyu 	.matches = {
315876ab790SLan Tianyu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
316876ab790SLan Tianyu 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
317876ab790SLan Tianyu 		},
318876ab790SLan Tianyu 	},
319876ab790SLan Tianyu 	{
320876ab790SLan Tianyu 	.callback = init_nvs_nosave,
321731b25a4SBogdan Radulescu 	.ident = "Sony Vaio VGN-FW520F",
322731b25a4SBogdan Radulescu 	.matches = {
323731b25a4SBogdan Radulescu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
324731b25a4SBogdan Radulescu 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
325731b25a4SBogdan Radulescu 		},
326731b25a4SBogdan Radulescu 	},
3275a50a7c3SKeng-Yu Lin 	{
3285a50a7c3SKeng-Yu Lin 	.callback = init_nvs_nosave,
3295a50a7c3SKeng-Yu Lin 	.ident = "Asus K54C",
3305a50a7c3SKeng-Yu Lin 	.matches = {
3315a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
3325a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
3335a50a7c3SKeng-Yu Lin 		},
3345a50a7c3SKeng-Yu Lin 	},
3355a50a7c3SKeng-Yu Lin 	{
3365a50a7c3SKeng-Yu Lin 	.callback = init_nvs_nosave,
3375a50a7c3SKeng-Yu Lin 	.ident = "Asus K54HR",
3385a50a7c3SKeng-Yu Lin 	.matches = {
3395a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
3405a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
3415a50a7c3SKeng-Yu Lin 		},
3425a50a7c3SKeng-Yu Lin 	},
343231f9415SWilly Tarreau 	{
344231f9415SWilly Tarreau 	.callback = init_nvs_save_s3,
345231f9415SWilly Tarreau 	.ident = "Asus 1025C",
346231f9415SWilly Tarreau 	.matches = {
347231f9415SWilly Tarreau 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
348231f9415SWilly Tarreau 		DMI_MATCH(DMI_PRODUCT_NAME, "1025C"),
349231f9415SWilly Tarreau 		},
350231f9415SWilly Tarreau 	},
351cbc00c13SZhang Rui 	/*
352cbc00c13SZhang Rui 	 * https://bugzilla.kernel.org/show_bug.cgi?id=189431
353cbc00c13SZhang Rui 	 * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
354cbc00c13SZhang Rui 	 * saving during S3.
355cbc00c13SZhang Rui 	 */
356cbc00c13SZhang Rui 	{
357cbc00c13SZhang Rui 	.callback = init_nvs_save_s3,
358cbc00c13SZhang Rui 	.ident = "Lenovo G50-45",
359cbc00c13SZhang Rui 	.matches = {
360cbc00c13SZhang Rui 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
361cbc00c13SZhang Rui 		DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
362cbc00c13SZhang Rui 		},
363cbc00c13SZhang Rui 	},
36471630b7aSRafael J. Wysocki 	/*
36571630b7aSRafael J. Wysocki 	 * https://bugzilla.kernel.org/show_bug.cgi?id=196907
36671630b7aSRafael J. Wysocki 	 * Some Dell XPS13 9360 cannot do suspend-to-idle using the Low Power
36771630b7aSRafael J. Wysocki 	 * S0 Idle firmware interface.
36871630b7aSRafael J. Wysocki 	 */
36971630b7aSRafael J. Wysocki 	{
370068b47d0SRafael J. Wysocki 	.callback = init_default_s3,
37171630b7aSRafael J. Wysocki 	.ident = "Dell XPS13 9360",
37271630b7aSRafael J. Wysocki 	.matches = {
37371630b7aSRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
37471630b7aSRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
37571630b7aSRafael J. Wysocki 		},
37671630b7aSRafael J. Wysocki 	},
377855c1c2fSChen Yu 	/*
378855c1c2fSChen Yu 	 * ThinkPad X1 Tablet(2016) cannot do suspend-to-idle using
379855c1c2fSChen Yu 	 * the Low Power S0 Idle firmware interface (see
380855c1c2fSChen Yu 	 * https://bugzilla.kernel.org/show_bug.cgi?id=199057).
381855c1c2fSChen Yu 	 */
382855c1c2fSChen Yu 	{
383068b47d0SRafael J. Wysocki 	.callback = init_default_s3,
384855c1c2fSChen Yu 	.ident = "ThinkPad X1 Tablet(2016)",
385855c1c2fSChen Yu 	.matches = {
386855c1c2fSChen Yu 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
387855c1c2fSChen Yu 		DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
388855c1c2fSChen Yu 		},
389855c1c2fSChen Yu 	},
390d08ca2caSLen Brown 	{},
391d08ca2caSLen Brown };
3920ac1b1d7SZhang Rui 
39357044031SRafael J. Wysocki static bool ignore_blacklist;
39457044031SRafael J. Wysocki 
39557044031SRafael J. Wysocki void __init acpi_sleep_no_blacklist(void)
39657044031SRafael J. Wysocki {
39757044031SRafael J. Wysocki 	ignore_blacklist = true;
39857044031SRafael J. Wysocki }
39957044031SRafael J. Wysocki 
4008b691c9cSJan Beulich static void __init acpi_sleep_dmi_check(void)
4010ac1b1d7SZhang Rui {
40257044031SRafael J. Wysocki 	if (ignore_blacklist)
40357044031SRafael J. Wysocki 		return;
40457044031SRafael J. Wysocki 
4059c0a30b6SAndy Shevchenko 	if (dmi_get_bios_year() >= 2012)
406821d6f03SLan Tianyu 		acpi_nvs_nosave_s3();
407821d6f03SLan Tianyu 
4080ac1b1d7SZhang Rui 	dmi_check_system(acpisleep_dmi_table);
4090ac1b1d7SZhang Rui }
4100ac1b1d7SZhang Rui 
4110ac1b1d7SZhang Rui /**
4120ac1b1d7SZhang Rui  * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
4130ac1b1d7SZhang Rui  */
4140ac1b1d7SZhang Rui static int acpi_pm_freeze(void)
4150ac1b1d7SZhang Rui {
4160ac1b1d7SZhang Rui 	acpi_disable_all_gpes();
4170ac1b1d7SZhang Rui 	acpi_os_wait_events_complete();
4180ac1b1d7SZhang Rui 	acpi_ec_block_transactions();
4190ac1b1d7SZhang Rui 	return 0;
4200ac1b1d7SZhang Rui }
4210ac1b1d7SZhang Rui 
4220ac1b1d7SZhang Rui /**
4230ac1b1d7SZhang Rui  * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
4240ac1b1d7SZhang Rui  */
4250ac1b1d7SZhang Rui static int acpi_pm_pre_suspend(void)
4260ac1b1d7SZhang Rui {
4270ac1b1d7SZhang Rui 	acpi_pm_freeze();
4280ac1b1d7SZhang Rui 	return suspend_nvs_save();
4290ac1b1d7SZhang Rui }
4300ac1b1d7SZhang Rui 
4310ac1b1d7SZhang Rui /**
4320ac1b1d7SZhang Rui  *	__acpi_pm_prepare - Prepare the platform to enter the target state.
4330ac1b1d7SZhang Rui  *
4340ac1b1d7SZhang Rui  *	If necessary, set the firmware waking vector and do arch-specific
4350ac1b1d7SZhang Rui  *	nastiness to get the wakeup code to the waking vector.
4360ac1b1d7SZhang Rui  */
4370ac1b1d7SZhang Rui static int __acpi_pm_prepare(void)
4380ac1b1d7SZhang Rui {
4390ac1b1d7SZhang Rui 	int error = acpi_sleep_prepare(acpi_target_sleep_state);
4400ac1b1d7SZhang Rui 	if (error)
4410ac1b1d7SZhang Rui 		acpi_target_sleep_state = ACPI_STATE_S0;
4420ac1b1d7SZhang Rui 
4430ac1b1d7SZhang Rui 	return error;
4440ac1b1d7SZhang Rui }
4450ac1b1d7SZhang Rui 
4460ac1b1d7SZhang Rui /**
4470ac1b1d7SZhang Rui  *	acpi_pm_prepare - Prepare the platform to enter the target sleep
4480ac1b1d7SZhang Rui  *		state and disable the GPEs.
4490ac1b1d7SZhang Rui  */
4500ac1b1d7SZhang Rui static int acpi_pm_prepare(void)
4510ac1b1d7SZhang Rui {
4520ac1b1d7SZhang Rui 	int error = __acpi_pm_prepare();
4530ac1b1d7SZhang Rui 	if (!error)
4540ac1b1d7SZhang Rui 		error = acpi_pm_pre_suspend();
4550ac1b1d7SZhang Rui 
4560ac1b1d7SZhang Rui 	return error;
4570ac1b1d7SZhang Rui }
4580ac1b1d7SZhang Rui 
4590ac1b1d7SZhang Rui /**
4600ac1b1d7SZhang Rui  *	acpi_pm_finish - Instruct the platform to leave a sleep state.
4610ac1b1d7SZhang Rui  *
4620ac1b1d7SZhang Rui  *	This is called after we wake back up (or if entering the sleep state
4630ac1b1d7SZhang Rui  *	failed).
4640ac1b1d7SZhang Rui  */
4650ac1b1d7SZhang Rui static void acpi_pm_finish(void)
4660ac1b1d7SZhang Rui {
467a3487d8fSAndy Shevchenko 	struct acpi_device *pwr_btn_adev;
4680ac1b1d7SZhang Rui 	u32 acpi_state = acpi_target_sleep_state;
4690ac1b1d7SZhang Rui 
4700ac1b1d7SZhang Rui 	acpi_ec_unblock_transactions();
4710ac1b1d7SZhang Rui 	suspend_nvs_free();
4720ac1b1d7SZhang Rui 
4730ac1b1d7SZhang Rui 	if (acpi_state == ACPI_STATE_S0)
4740ac1b1d7SZhang Rui 		return;
4750ac1b1d7SZhang Rui 
4760ac1b1d7SZhang Rui 	printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
4770ac1b1d7SZhang Rui 		acpi_state);
4780ac1b1d7SZhang Rui 	acpi_disable_wakeup_devices(acpi_state);
4790ac1b1d7SZhang Rui 	acpi_leave_sleep_state(acpi_state);
4800ac1b1d7SZhang Rui 
4810ac1b1d7SZhang Rui 	/* reset firmware waking vector */
482e3e9b577SRafael J. Wysocki 	acpi_set_waking_vector(0);
4830ac1b1d7SZhang Rui 
4840ac1b1d7SZhang Rui 	acpi_target_sleep_state = ACPI_STATE_S0;
4850ac1b1d7SZhang Rui 
486781d737cSRafael J. Wysocki 	acpi_resume_power_resources();
487781d737cSRafael J. Wysocki 
4880ac1b1d7SZhang Rui 	/* If we were woken with the fixed power button, provide a small
4890ac1b1d7SZhang Rui 	 * hint to userspace in the form of a wakeup event on the fixed power
4900ac1b1d7SZhang Rui 	 * button device (if it can be found).
4910ac1b1d7SZhang Rui 	 *
4920ac1b1d7SZhang Rui 	 * We delay the event generation til now, as the PM layer requires
4930ac1b1d7SZhang Rui 	 * timekeeping to be running before we generate events. */
4940ac1b1d7SZhang Rui 	if (!pwr_btn_event_pending)
4950ac1b1d7SZhang Rui 		return;
4960ac1b1d7SZhang Rui 
4970ac1b1d7SZhang Rui 	pwr_btn_event_pending = false;
498a3487d8fSAndy Shevchenko 	pwr_btn_adev = acpi_dev_get_first_match_dev(ACPI_BUTTON_HID_POWERF,
499a3487d8fSAndy Shevchenko 						    NULL, -1);
500a3487d8fSAndy Shevchenko 	if (pwr_btn_adev) {
501a3487d8fSAndy Shevchenko 		pm_wakeup_event(&pwr_btn_adev->dev, 0);
502a3487d8fSAndy Shevchenko 		acpi_dev_put(pwr_btn_adev);
5030ac1b1d7SZhang Rui 	}
5040ac1b1d7SZhang Rui }
5050ac1b1d7SZhang Rui 
5060ac1b1d7SZhang Rui /**
507ad07277eSRafael J. Wysocki  * acpi_pm_start - Start system PM transition.
508ad07277eSRafael J. Wysocki  */
509ad07277eSRafael J. Wysocki static void acpi_pm_start(u32 acpi_state)
510ad07277eSRafael J. Wysocki {
511ad07277eSRafael J. Wysocki 	acpi_target_sleep_state = acpi_state;
512ad07277eSRafael J. Wysocki 	acpi_sleep_tts_switch(acpi_target_sleep_state);
513ad07277eSRafael J. Wysocki 	acpi_scan_lock_acquire();
514ad07277eSRafael J. Wysocki }
515ad07277eSRafael J. Wysocki 
516ad07277eSRafael J. Wysocki /**
517ad07277eSRafael J. Wysocki  * acpi_pm_end - Finish up system PM transition.
5180ac1b1d7SZhang Rui  */
5190ac1b1d7SZhang Rui static void acpi_pm_end(void)
5200ac1b1d7SZhang Rui {
5218ece1d83SHans de Goede 	acpi_turn_off_unused_power_resources();
522ad07277eSRafael J. Wysocki 	acpi_scan_lock_release();
5230ac1b1d7SZhang Rui 	/*
5240ac1b1d7SZhang Rui 	 * This is necessary in case acpi_pm_finish() is not called during a
5250ac1b1d7SZhang Rui 	 * failing transition to a sleep state.
5260ac1b1d7SZhang Rui 	 */
5270ac1b1d7SZhang Rui 	acpi_target_sleep_state = ACPI_STATE_S0;
5280ac1b1d7SZhang Rui 	acpi_sleep_tts_switch(acpi_target_sleep_state);
5290ac1b1d7SZhang Rui }
5300ac1b1d7SZhang Rui #else /* !CONFIG_ACPI_SLEEP */
531068b47d0SRafael J. Wysocki #define sleep_no_lps0	(1)
5320ac1b1d7SZhang Rui #define acpi_target_sleep_state	ACPI_STATE_S0
533068b47d0SRafael J. Wysocki #define acpi_sleep_default_s3	(1)
5340ac1b1d7SZhang Rui static inline void acpi_sleep_dmi_check(void) {}
5350ac1b1d7SZhang Rui #endif /* CONFIG_ACPI_SLEEP */
5360ac1b1d7SZhang Rui 
5370ac1b1d7SZhang Rui #ifdef CONFIG_SUSPEND
5380ac1b1d7SZhang Rui static u32 acpi_suspend_states[] = {
5390ac1b1d7SZhang Rui 	[PM_SUSPEND_ON] = ACPI_STATE_S0,
5400ac1b1d7SZhang Rui 	[PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
5410ac1b1d7SZhang Rui 	[PM_SUSPEND_MEM] = ACPI_STATE_S3,
5420ac1b1d7SZhang Rui 	[PM_SUSPEND_MAX] = ACPI_STATE_S5
5430ac1b1d7SZhang Rui };
5440ac1b1d7SZhang Rui 
5450ac1b1d7SZhang Rui /**
5460ac1b1d7SZhang Rui  *	acpi_suspend_begin - Set the target system sleep state to the state
5470ac1b1d7SZhang Rui  *		associated with given @pm_state, if supported.
5480ac1b1d7SZhang Rui  */
5490ac1b1d7SZhang Rui static int acpi_suspend_begin(suspend_state_t pm_state)
5500ac1b1d7SZhang Rui {
5510ac1b1d7SZhang Rui 	u32 acpi_state = acpi_suspend_states[pm_state];
552ad07277eSRafael J. Wysocki 	int error;
5530ac1b1d7SZhang Rui 
5540ac1b1d7SZhang Rui 	error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
5550ac1b1d7SZhang Rui 	if (error)
5560ac1b1d7SZhang Rui 		return error;
5570ac1b1d7SZhang Rui 
558ad07277eSRafael J. Wysocki 	if (!sleep_states[acpi_state]) {
559ad07277eSRafael J. Wysocki 		pr_err("ACPI does not support sleep state S%u\n", acpi_state);
560ad07277eSRafael J. Wysocki 		return -ENOSYS;
5610ac1b1d7SZhang Rui 	}
562ef25ba04SRafael J. Wysocki 	if (acpi_state > ACPI_STATE_S1)
563ef25ba04SRafael J. Wysocki 		pm_set_suspend_via_firmware();
564ad07277eSRafael J. Wysocki 
565ad07277eSRafael J. Wysocki 	acpi_pm_start(acpi_state);
566ad07277eSRafael J. Wysocki 	return 0;
5670ac1b1d7SZhang Rui }
5680ac1b1d7SZhang Rui 
5690ac1b1d7SZhang Rui /**
5700ac1b1d7SZhang Rui  *	acpi_suspend_enter - Actually enter a sleep state.
5710ac1b1d7SZhang Rui  *	@pm_state: ignored
5720ac1b1d7SZhang Rui  *
5730ac1b1d7SZhang Rui  *	Flush caches and go to sleep. For STR we have to call arch-specific
5740ac1b1d7SZhang Rui  *	assembly, which in turn call acpi_enter_sleep_state().
5750ac1b1d7SZhang Rui  *	It's unfortunate, but it works. Please fix if you're feeling frisky.
5760ac1b1d7SZhang Rui  */
5770ac1b1d7SZhang Rui static int acpi_suspend_enter(suspend_state_t pm_state)
5780ac1b1d7SZhang Rui {
5790ac1b1d7SZhang Rui 	acpi_status status = AE_OK;
5800ac1b1d7SZhang Rui 	u32 acpi_state = acpi_target_sleep_state;
5810ac1b1d7SZhang Rui 	int error;
5820ac1b1d7SZhang Rui 
5830ac1b1d7SZhang Rui 	ACPI_FLUSH_CPU_CACHE();
5840ac1b1d7SZhang Rui 
585bb3632c6STodd E Brandt 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
5860ac1b1d7SZhang Rui 	switch (acpi_state) {
5870ac1b1d7SZhang Rui 	case ACPI_STATE_S1:
5880ac1b1d7SZhang Rui 		barrier();
5890ac1b1d7SZhang Rui 		status = acpi_enter_sleep_state(acpi_state);
5900ac1b1d7SZhang Rui 		break;
5910ac1b1d7SZhang Rui 
5920ac1b1d7SZhang Rui 	case ACPI_STATE_S3:
593d6a77eadSKonrad Rzeszutek Wilk 		if (!acpi_suspend_lowlevel)
594d6a77eadSKonrad Rzeszutek Wilk 			return -ENOSYS;
5950ac1b1d7SZhang Rui 		error = acpi_suspend_lowlevel();
5960ac1b1d7SZhang Rui 		if (error)
5970ac1b1d7SZhang Rui 			return error;
5980ac1b1d7SZhang Rui 		pr_info(PREFIX "Low-level resume complete\n");
599ef25ba04SRafael J. Wysocki 		pm_set_resume_via_firmware();
6000ac1b1d7SZhang Rui 		break;
6010ac1b1d7SZhang Rui 	}
602bb3632c6STodd E Brandt 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
6030ac1b1d7SZhang Rui 
6040ac1b1d7SZhang Rui 	/* This violates the spec but is required for bug compatibility. */
6050ac1b1d7SZhang Rui 	acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
6060ac1b1d7SZhang Rui 
6070ac1b1d7SZhang Rui 	/* Reprogram control registers */
6080ac1b1d7SZhang Rui 	acpi_leave_sleep_state_prep(acpi_state);
6090ac1b1d7SZhang Rui 
6100ac1b1d7SZhang Rui 	/* ACPI 3.0 specs (P62) says that it's the responsibility
6110ac1b1d7SZhang Rui 	 * of the OSPM to clear the status bit [ implying that the
6120ac1b1d7SZhang Rui 	 * POWER_BUTTON event should not reach userspace ]
6130ac1b1d7SZhang Rui 	 *
6140ac1b1d7SZhang Rui 	 * However, we do generate a small hint for userspace in the form of
6150ac1b1d7SZhang Rui 	 * a wakeup event. We flag this condition for now and generate the
6160ac1b1d7SZhang Rui 	 * event later, as we're currently too early in resume to be able to
6170ac1b1d7SZhang Rui 	 * generate wakeup events.
6180ac1b1d7SZhang Rui 	 */
6190ac1b1d7SZhang Rui 	if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
62051468a9dSAl Stone 		acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
6210ac1b1d7SZhang Rui 
6220ac1b1d7SZhang Rui 		acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
6230ac1b1d7SZhang Rui 
62418864cc4SLv Zheng 		if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
6250ac1b1d7SZhang Rui 			acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
6260ac1b1d7SZhang Rui 			/* Flag for later */
6270ac1b1d7SZhang Rui 			pwr_btn_event_pending = true;
6280ac1b1d7SZhang Rui 		}
6290ac1b1d7SZhang Rui 	}
6300ac1b1d7SZhang Rui 
6310ac1b1d7SZhang Rui 	/*
6320ac1b1d7SZhang Rui 	 * Disable and clear GPE status before interrupt is enabled. Some GPEs
6330ac1b1d7SZhang Rui 	 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
6340ac1b1d7SZhang Rui 	 * acpi_leave_sleep_state will reenable specific GPEs later
6350ac1b1d7SZhang Rui 	 */
6360ac1b1d7SZhang Rui 	acpi_disable_all_gpes();
6370ac1b1d7SZhang Rui 	/* Allow EC transactions to happen. */
638c2b46d67SLv Zheng 	acpi_ec_unblock_transactions();
6390ac1b1d7SZhang Rui 
6400ac1b1d7SZhang Rui 	suspend_nvs_restore();
6410ac1b1d7SZhang Rui 
6420ac1b1d7SZhang Rui 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
6430ac1b1d7SZhang Rui }
6440ac1b1d7SZhang Rui 
6450ac1b1d7SZhang Rui static int acpi_suspend_state_valid(suspend_state_t pm_state)
6460ac1b1d7SZhang Rui {
6470ac1b1d7SZhang Rui 	u32 acpi_state;
6480ac1b1d7SZhang Rui 
6490ac1b1d7SZhang Rui 	switch (pm_state) {
6500ac1b1d7SZhang Rui 	case PM_SUSPEND_ON:
6510ac1b1d7SZhang Rui 	case PM_SUSPEND_STANDBY:
6520ac1b1d7SZhang Rui 	case PM_SUSPEND_MEM:
6530ac1b1d7SZhang Rui 		acpi_state = acpi_suspend_states[pm_state];
6540ac1b1d7SZhang Rui 
6550ac1b1d7SZhang Rui 		return sleep_states[acpi_state];
6560ac1b1d7SZhang Rui 	default:
6570ac1b1d7SZhang Rui 		return 0;
6580ac1b1d7SZhang Rui 	}
6590ac1b1d7SZhang Rui }
6600ac1b1d7SZhang Rui 
6610ac1b1d7SZhang Rui static const struct platform_suspend_ops acpi_suspend_ops = {
6620ac1b1d7SZhang Rui 	.valid = acpi_suspend_state_valid,
6630ac1b1d7SZhang Rui 	.begin = acpi_suspend_begin,
6640ac1b1d7SZhang Rui 	.prepare_late = acpi_pm_prepare,
6650ac1b1d7SZhang Rui 	.enter = acpi_suspend_enter,
6660ac1b1d7SZhang Rui 	.wake = acpi_pm_finish,
6670ac1b1d7SZhang Rui 	.end = acpi_pm_end,
6680ac1b1d7SZhang Rui };
6690ac1b1d7SZhang Rui 
6700ac1b1d7SZhang Rui /**
6710ac1b1d7SZhang Rui  *	acpi_suspend_begin_old - Set the target system sleep state to the
6720ac1b1d7SZhang Rui  *		state associated with given @pm_state, if supported, and
6730ac1b1d7SZhang Rui  *		execute the _PTS control method.  This function is used if the
6740ac1b1d7SZhang Rui  *		pre-ACPI 2.0 suspend ordering has been requested.
6750ac1b1d7SZhang Rui  */
6760ac1b1d7SZhang Rui static int acpi_suspend_begin_old(suspend_state_t pm_state)
6770ac1b1d7SZhang Rui {
6780ac1b1d7SZhang Rui 	int error = acpi_suspend_begin(pm_state);
6790ac1b1d7SZhang Rui 	if (!error)
6800ac1b1d7SZhang Rui 		error = __acpi_pm_prepare();
6810ac1b1d7SZhang Rui 
6820ac1b1d7SZhang Rui 	return error;
6830ac1b1d7SZhang Rui }
6840ac1b1d7SZhang Rui 
6850ac1b1d7SZhang Rui /*
6860ac1b1d7SZhang Rui  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
6870ac1b1d7SZhang Rui  * been requested.
6880ac1b1d7SZhang Rui  */
6890ac1b1d7SZhang Rui static const struct platform_suspend_ops acpi_suspend_ops_old = {
6900ac1b1d7SZhang Rui 	.valid = acpi_suspend_state_valid,
6910ac1b1d7SZhang Rui 	.begin = acpi_suspend_begin_old,
6920ac1b1d7SZhang Rui 	.prepare_late = acpi_pm_pre_suspend,
6930ac1b1d7SZhang Rui 	.enter = acpi_suspend_enter,
6940ac1b1d7SZhang Rui 	.wake = acpi_pm_finish,
6950ac1b1d7SZhang Rui 	.end = acpi_pm_end,
6960ac1b1d7SZhang Rui 	.recover = acpi_pm_finish,
6970ac1b1d7SZhang Rui };
69802040f0bSRafael J. Wysocki 
69933e4f80eSRafael J. Wysocki static bool s2idle_wakeup;
70033e4f80eSRafael J. Wysocki 
7018110dd28SRafael J. Wysocki /*
7028110dd28SRafael J. Wysocki  * On platforms supporting the Low Power S0 Idle interface there is an ACPI
7038110dd28SRafael J. Wysocki  * device object with the PNP0D80 compatible device ID (System Power Management
7048110dd28SRafael J. Wysocki  * Controller) and a specific _DSM method under it.  That method, if present,
7058110dd28SRafael J. Wysocki  * can be used to indicate to the platform that the OS is transitioning into a
7068110dd28SRafael J. Wysocki  * low-power state in which certain types of activity are not desirable or that
7078110dd28SRafael J. Wysocki  * it is leaving such a state, which allows the platform to adjust its operation
7088110dd28SRafael J. Wysocki  * mode accordingly.
7098110dd28SRafael J. Wysocki  */
7108110dd28SRafael J. Wysocki static const struct acpi_device_id lps0_device_ids[] = {
7118110dd28SRafael J. Wysocki 	{"PNP0D80", },
7128110dd28SRafael J. Wysocki 	{"", },
7138110dd28SRafael J. Wysocki };
7148110dd28SRafael J. Wysocki 
7158110dd28SRafael J. Wysocki #define ACPI_LPS0_DSM_UUID	"c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
7168110dd28SRafael J. Wysocki 
717726fb6b4SSrinivas Pandruvada #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS	1
7188110dd28SRafael J. Wysocki #define ACPI_LPS0_SCREEN_OFF	3
7198110dd28SRafael J. Wysocki #define ACPI_LPS0_SCREEN_ON	4
7208110dd28SRafael J. Wysocki #define ACPI_LPS0_ENTRY		5
7218110dd28SRafael J. Wysocki #define ACPI_LPS0_EXIT		6
7228110dd28SRafael J. Wysocki 
7238110dd28SRafael J. Wysocki static acpi_handle lps0_device_handle;
7248110dd28SRafael J. Wysocki static guid_t lps0_dsm_guid;
7258110dd28SRafael J. Wysocki static char lps0_dsm_func_mask;
7268110dd28SRafael J. Wysocki 
727726fb6b4SSrinivas Pandruvada /* Device constraint entry structure */
728726fb6b4SSrinivas Pandruvada struct lpi_device_info {
729726fb6b4SSrinivas Pandruvada 	char *name;
730726fb6b4SSrinivas Pandruvada 	int enabled;
731726fb6b4SSrinivas Pandruvada 	union acpi_object *package;
732726fb6b4SSrinivas Pandruvada };
733726fb6b4SSrinivas Pandruvada 
734726fb6b4SSrinivas Pandruvada /* Constraint package structure */
735726fb6b4SSrinivas Pandruvada struct lpi_device_constraint {
736726fb6b4SSrinivas Pandruvada 	int uid;
737726fb6b4SSrinivas Pandruvada 	int min_dstate;
738726fb6b4SSrinivas Pandruvada 	int function_states;
739726fb6b4SSrinivas Pandruvada };
740726fb6b4SSrinivas Pandruvada 
741726fb6b4SSrinivas Pandruvada struct lpi_constraints {
742726fb6b4SSrinivas Pandruvada 	acpi_handle handle;
743726fb6b4SSrinivas Pandruvada 	int min_dstate;
744726fb6b4SSrinivas Pandruvada };
745726fb6b4SSrinivas Pandruvada 
746726fb6b4SSrinivas Pandruvada static struct lpi_constraints *lpi_constraints_table;
747726fb6b4SSrinivas Pandruvada static int lpi_constraints_table_size;
748726fb6b4SSrinivas Pandruvada 
749726fb6b4SSrinivas Pandruvada static void lpi_device_get_constraints(void)
750726fb6b4SSrinivas Pandruvada {
751726fb6b4SSrinivas Pandruvada 	union acpi_object *out_obj;
752726fb6b4SSrinivas Pandruvada 	int i;
753726fb6b4SSrinivas Pandruvada 
754726fb6b4SSrinivas Pandruvada 	out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
755726fb6b4SSrinivas Pandruvada 					  1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
756726fb6b4SSrinivas Pandruvada 					  NULL, ACPI_TYPE_PACKAGE);
757726fb6b4SSrinivas Pandruvada 
758726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
759726fb6b4SSrinivas Pandruvada 			  out_obj ? "successful" : "failed");
760726fb6b4SSrinivas Pandruvada 
761726fb6b4SSrinivas Pandruvada 	if (!out_obj)
762726fb6b4SSrinivas Pandruvada 		return;
763726fb6b4SSrinivas Pandruvada 
764726fb6b4SSrinivas Pandruvada 	lpi_constraints_table = kcalloc(out_obj->package.count,
765726fb6b4SSrinivas Pandruvada 					sizeof(*lpi_constraints_table),
766726fb6b4SSrinivas Pandruvada 					GFP_KERNEL);
767726fb6b4SSrinivas Pandruvada 	if (!lpi_constraints_table)
768726fb6b4SSrinivas Pandruvada 		goto free_acpi_buffer;
769726fb6b4SSrinivas Pandruvada 
770726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
771726fb6b4SSrinivas Pandruvada 
772726fb6b4SSrinivas Pandruvada 	for (i = 0; i < out_obj->package.count; i++) {
773726fb6b4SSrinivas Pandruvada 		struct lpi_constraints *constraint;
774726fb6b4SSrinivas Pandruvada 		acpi_status status;
775726fb6b4SSrinivas Pandruvada 		union acpi_object *package = &out_obj->package.elements[i];
776726fb6b4SSrinivas Pandruvada 		struct lpi_device_info info = { };
777726fb6b4SSrinivas Pandruvada 		int package_count = 0, j;
778726fb6b4SSrinivas Pandruvada 
779726fb6b4SSrinivas Pandruvada 		if (!package)
780726fb6b4SSrinivas Pandruvada 			continue;
781726fb6b4SSrinivas Pandruvada 
782726fb6b4SSrinivas Pandruvada 		for (j = 0; j < package->package.count; ++j) {
783726fb6b4SSrinivas Pandruvada 			union acpi_object *element =
784726fb6b4SSrinivas Pandruvada 					&(package->package.elements[j]);
785726fb6b4SSrinivas Pandruvada 
786726fb6b4SSrinivas Pandruvada 			switch (element->type) {
787726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_INTEGER:
788726fb6b4SSrinivas Pandruvada 				info.enabled = element->integer.value;
789726fb6b4SSrinivas Pandruvada 				break;
790726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_STRING:
791726fb6b4SSrinivas Pandruvada 				info.name = element->string.pointer;
792726fb6b4SSrinivas Pandruvada 				break;
793726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_PACKAGE:
794726fb6b4SSrinivas Pandruvada 				package_count = element->package.count;
795726fb6b4SSrinivas Pandruvada 				info.package = element->package.elements;
796726fb6b4SSrinivas Pandruvada 				break;
797726fb6b4SSrinivas Pandruvada 			}
798726fb6b4SSrinivas Pandruvada 		}
799726fb6b4SSrinivas Pandruvada 
800726fb6b4SSrinivas Pandruvada 		if (!info.enabled || !info.package || !info.name)
801726fb6b4SSrinivas Pandruvada 			continue;
802726fb6b4SSrinivas Pandruvada 
803726fb6b4SSrinivas Pandruvada 		constraint = &lpi_constraints_table[lpi_constraints_table_size];
804726fb6b4SSrinivas Pandruvada 
805726fb6b4SSrinivas Pandruvada 		status = acpi_get_handle(NULL, info.name, &constraint->handle);
806726fb6b4SSrinivas Pandruvada 		if (ACPI_FAILURE(status))
807726fb6b4SSrinivas Pandruvada 			continue;
808726fb6b4SSrinivas Pandruvada 
809726fb6b4SSrinivas Pandruvada 		acpi_handle_debug(lps0_device_handle,
810726fb6b4SSrinivas Pandruvada 				  "index:%d Name:%s\n", i, info.name);
811726fb6b4SSrinivas Pandruvada 
812726fb6b4SSrinivas Pandruvada 		constraint->min_dstate = -1;
813726fb6b4SSrinivas Pandruvada 
814726fb6b4SSrinivas Pandruvada 		for (j = 0; j < package_count; ++j) {
815726fb6b4SSrinivas Pandruvada 			union acpi_object *info_obj = &info.package[j];
816726fb6b4SSrinivas Pandruvada 			union acpi_object *cnstr_pkg;
817726fb6b4SSrinivas Pandruvada 			union acpi_object *obj;
818726fb6b4SSrinivas Pandruvada 			struct lpi_device_constraint dev_info;
819726fb6b4SSrinivas Pandruvada 
820726fb6b4SSrinivas Pandruvada 			switch (info_obj->type) {
821726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_INTEGER:
822726fb6b4SSrinivas Pandruvada 				/* version */
823726fb6b4SSrinivas Pandruvada 				break;
824726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_PACKAGE:
825726fb6b4SSrinivas Pandruvada 				if (info_obj->package.count < 2)
826726fb6b4SSrinivas Pandruvada 					break;
827726fb6b4SSrinivas Pandruvada 
828726fb6b4SSrinivas Pandruvada 				cnstr_pkg = info_obj->package.elements;
829726fb6b4SSrinivas Pandruvada 				obj = &cnstr_pkg[0];
830726fb6b4SSrinivas Pandruvada 				dev_info.uid = obj->integer.value;
831726fb6b4SSrinivas Pandruvada 				obj = &cnstr_pkg[1];
832726fb6b4SSrinivas Pandruvada 				dev_info.min_dstate = obj->integer.value;
833726fb6b4SSrinivas Pandruvada 
834726fb6b4SSrinivas Pandruvada 				acpi_handle_debug(lps0_device_handle,
835726fb6b4SSrinivas Pandruvada 					"uid:%d min_dstate:%s\n",
836726fb6b4SSrinivas Pandruvada 					dev_info.uid,
837726fb6b4SSrinivas Pandruvada 					acpi_power_state_string(dev_info.min_dstate));
838726fb6b4SSrinivas Pandruvada 
839726fb6b4SSrinivas Pandruvada 				constraint->min_dstate = dev_info.min_dstate;
840726fb6b4SSrinivas Pandruvada 				break;
841726fb6b4SSrinivas Pandruvada 			}
842726fb6b4SSrinivas Pandruvada 		}
843726fb6b4SSrinivas Pandruvada 
844726fb6b4SSrinivas Pandruvada 		if (constraint->min_dstate < 0) {
845726fb6b4SSrinivas Pandruvada 			acpi_handle_debug(lps0_device_handle,
846726fb6b4SSrinivas Pandruvada 					  "Incomplete constraint defined\n");
847726fb6b4SSrinivas Pandruvada 			continue;
848726fb6b4SSrinivas Pandruvada 		}
849726fb6b4SSrinivas Pandruvada 
850726fb6b4SSrinivas Pandruvada 		lpi_constraints_table_size++;
851726fb6b4SSrinivas Pandruvada 	}
852726fb6b4SSrinivas Pandruvada 
853726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
854726fb6b4SSrinivas Pandruvada 
855726fb6b4SSrinivas Pandruvada free_acpi_buffer:
856726fb6b4SSrinivas Pandruvada 	ACPI_FREE(out_obj);
857726fb6b4SSrinivas Pandruvada }
858726fb6b4SSrinivas Pandruvada 
859726fb6b4SSrinivas Pandruvada static void lpi_check_constraints(void)
860726fb6b4SSrinivas Pandruvada {
861726fb6b4SSrinivas Pandruvada 	int i;
862726fb6b4SSrinivas Pandruvada 
863726fb6b4SSrinivas Pandruvada 	for (i = 0; i < lpi_constraints_table_size; ++i) {
864fa54150aSRafael J. Wysocki 		acpi_handle handle = lpi_constraints_table[i].handle;
865726fb6b4SSrinivas Pandruvada 		struct acpi_device *adev;
866726fb6b4SSrinivas Pandruvada 
867fa54150aSRafael J. Wysocki 		if (!handle || acpi_bus_get_device(handle, &adev))
868726fb6b4SSrinivas Pandruvada 			continue;
869726fb6b4SSrinivas Pandruvada 
870fa54150aSRafael J. Wysocki 		acpi_handle_debug(handle,
871726fb6b4SSrinivas Pandruvada 			"LPI: required min power state:%s current power state:%s\n",
872726fb6b4SSrinivas Pandruvada 			acpi_power_state_string(lpi_constraints_table[i].min_dstate),
873726fb6b4SSrinivas Pandruvada 			acpi_power_state_string(adev->power.state));
874726fb6b4SSrinivas Pandruvada 
875726fb6b4SSrinivas Pandruvada 		if (!adev->flags.power_manageable) {
876fa54150aSRafael J. Wysocki 			acpi_handle_info(handle, "LPI: Device not power manageable\n");
877fa54150aSRafael J. Wysocki 			lpi_constraints_table[i].handle = NULL;
878726fb6b4SSrinivas Pandruvada 			continue;
879726fb6b4SSrinivas Pandruvada 		}
880726fb6b4SSrinivas Pandruvada 
881726fb6b4SSrinivas Pandruvada 		if (adev->power.state < lpi_constraints_table[i].min_dstate)
882fa54150aSRafael J. Wysocki 			acpi_handle_info(handle,
883726fb6b4SSrinivas Pandruvada 				"LPI: Constraint not met; min power state:%s current power state:%s\n",
884726fb6b4SSrinivas Pandruvada 				acpi_power_state_string(lpi_constraints_table[i].min_dstate),
885726fb6b4SSrinivas Pandruvada 				acpi_power_state_string(adev->power.state));
886726fb6b4SSrinivas Pandruvada 	}
887726fb6b4SSrinivas Pandruvada }
888726fb6b4SSrinivas Pandruvada 
8898110dd28SRafael J. Wysocki static void acpi_sleep_run_lps0_dsm(unsigned int func)
8908110dd28SRafael J. Wysocki {
8918110dd28SRafael J. Wysocki 	union acpi_object *out_obj;
8928110dd28SRafael J. Wysocki 
8938110dd28SRafael J. Wysocki 	if (!(lps0_dsm_func_mask & (1 << func)))
8948110dd28SRafael J. Wysocki 		return;
8958110dd28SRafael J. Wysocki 
8968110dd28SRafael J. Wysocki 	out_obj = acpi_evaluate_dsm(lps0_device_handle, &lps0_dsm_guid, 1, func, NULL);
8978110dd28SRafael J. Wysocki 	ACPI_FREE(out_obj);
8988110dd28SRafael J. Wysocki 
8998110dd28SRafael J. Wysocki 	acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
9008110dd28SRafael J. Wysocki 			  func, out_obj ? "successful" : "failed");
9018110dd28SRafael J. Wysocki }
9028110dd28SRafael J. Wysocki 
9038110dd28SRafael J. Wysocki static int lps0_device_attach(struct acpi_device *adev,
9048110dd28SRafael J. Wysocki 			      const struct acpi_device_id *not_used)
9058110dd28SRafael J. Wysocki {
9068110dd28SRafael J. Wysocki 	union acpi_object *out_obj;
9078110dd28SRafael J. Wysocki 
9088110dd28SRafael J. Wysocki 	if (lps0_device_handle)
9098110dd28SRafael J. Wysocki 		return 0;
9108110dd28SRafael J. Wysocki 
9118110dd28SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
9128110dd28SRafael J. Wysocki 		return 0;
9138110dd28SRafael J. Wysocki 
9148110dd28SRafael J. Wysocki 	guid_parse(ACPI_LPS0_DSM_UUID, &lps0_dsm_guid);
9158110dd28SRafael J. Wysocki 	/* Check if the _DSM is present and as expected. */
9168110dd28SRafael J. Wysocki 	out_obj = acpi_evaluate_dsm(adev->handle, &lps0_dsm_guid, 1, 0, NULL);
9172e2c2fdcSRafael J. Wysocki 	if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER) {
9182e2c2fdcSRafael J. Wysocki 		acpi_handle_debug(adev->handle,
9192e2c2fdcSRafael J. Wysocki 				  "_DSM function 0 evaluation failed\n");
9202e2c2fdcSRafael J. Wysocki 		return 0;
9212e2c2fdcSRafael J. Wysocki 	}
9228110dd28SRafael J. Wysocki 
9232e2c2fdcSRafael J. Wysocki 	lps0_dsm_func_mask = *(char *)out_obj->buffer.pointer;
9242e2c2fdcSRafael J. Wysocki 
9252e2c2fdcSRafael J. Wysocki 	ACPI_FREE(out_obj);
9262e2c2fdcSRafael J. Wysocki 
9272e2c2fdcSRafael J. Wysocki 	acpi_handle_debug(adev->handle, "_DSM function mask: 0x%x\n",
9282e2c2fdcSRafael J. Wysocki 			  lps0_dsm_func_mask);
9292e2c2fdcSRafael J. Wysocki 
9308110dd28SRafael J. Wysocki 	lps0_device_handle = adev->handle;
9312e2c2fdcSRafael J. Wysocki 
9322e2c2fdcSRafael J. Wysocki 	lpi_device_get_constraints();
9332e2c2fdcSRafael J. Wysocki 
934e870c6c8SRafael J. Wysocki 	/*
9352e2c2fdcSRafael J. Wysocki 	 * Use suspend-to-idle by default if the default suspend mode was not
9362e2c2fdcSRafael J. Wysocki 	 * set from the command line.
937e870c6c8SRafael J. Wysocki 	 */
938068b47d0SRafael J. Wysocki 	if (mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3)
939690cbb90SRafael J. Wysocki 		mem_sleep_current = PM_SUSPEND_TO_IDLE;
9408110dd28SRafael J. Wysocki 
9418110dd28SRafael J. Wysocki 	return 0;
9428110dd28SRafael J. Wysocki }
9438110dd28SRafael J. Wysocki 
9448110dd28SRafael J. Wysocki static struct acpi_scan_handler lps0_handler = {
9458110dd28SRafael J. Wysocki 	.ids = lps0_device_ids,
9468110dd28SRafael J. Wysocki 	.attach = lps0_device_attach,
9478110dd28SRafael J. Wysocki };
9488110dd28SRafael J. Wysocki 
94923d5855fSRafael J. Wysocki static int acpi_s2idle_begin(void)
9501f0b6386SRafael J. Wysocki {
9511f0b6386SRafael J. Wysocki 	acpi_scan_lock_acquire();
9521f0b6386SRafael J. Wysocki 	return 0;
9531f0b6386SRafael J. Wysocki }
9541f0b6386SRafael J. Wysocki 
95523d5855fSRafael J. Wysocki static int acpi_s2idle_prepare(void)
956a8d46b9eSRafael J. Wysocki {
9578c01275eSChen Yu 	if (acpi_sci_irq_valid())
9588c01275eSChen Yu 		enable_irq_wake(acpi_sci_irq);
959235d81a6SRafael J. Wysocki 
9602f844b61SRajat Jain 	acpi_enable_wakeup_devices(ACPI_STATE_S0);
9612f844b61SRajat Jain 
962f941d3e4SRafael J. Wysocki 	/* Change the configuration of GPEs to avoid spurious wakeup. */
963f941d3e4SRafael J. Wysocki 	acpi_enable_all_wakeup_gpes();
964f941d3e4SRafael J. Wysocki 	acpi_os_wait_events_complete();
96541275eb5SRafael J. Wysocki 
96641275eb5SRafael J. Wysocki 	s2idle_wakeup = true;
967a8d46b9eSRafael J. Wysocki 	return 0;
968a8d46b9eSRafael J. Wysocki }
969a8d46b9eSRafael J. Wysocki 
970*ac9eafbeSRafael J. Wysocki static int acpi_s2idle_prepare_late(void)
97133e4f80eSRafael J. Wysocki {
972*ac9eafbeSRafael J. Wysocki 	if (!lps0_device_handle || sleep_no_lps0)
973*ac9eafbeSRafael J. Wysocki 		return 0;
974*ac9eafbeSRafael J. Wysocki 
975*ac9eafbeSRafael J. Wysocki 	if (pm_debug_messages_on)
976726fb6b4SSrinivas Pandruvada 		lpi_check_constraints();
977726fb6b4SSrinivas Pandruvada 
978*ac9eafbeSRafael J. Wysocki 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF);
979*ac9eafbeSRafael J. Wysocki 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY);
980*ac9eafbeSRafael J. Wysocki 
981*ac9eafbeSRafael J. Wysocki 	return 0;
982*ac9eafbeSRafael J. Wysocki }
983*ac9eafbeSRafael J. Wysocki 
984*ac9eafbeSRafael J. Wysocki static void acpi_s2idle_wake(void)
985*ac9eafbeSRafael J. Wysocki {
98633e4f80eSRafael J. Wysocki 	/*
98756b99184SRafael J. Wysocki 	 * If IRQD_WAKEUP_ARMED is set for the SCI at this point, the SCI has
98856b99184SRafael J. Wysocki 	 * not triggered while suspended, so bail out.
98933e4f80eSRafael J. Wysocki 	 */
99056b99184SRafael J. Wysocki 	if (!acpi_sci_irq_valid() ||
99156b99184SRafael J. Wysocki 	    irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq)))
99256b99184SRafael J. Wysocki 		return;
99356b99184SRafael J. Wysocki 
99456b99184SRafael J. Wysocki 	/*
99556b99184SRafael J. Wysocki 	 * If there are EC events to process, the wakeup may be a spurious one
99656b99184SRafael J. Wysocki 	 * coming from the EC.
99756b99184SRafael J. Wysocki 	 */
99856b99184SRafael J. Wysocki 	if (acpi_ec_dispatch_gpe()) {
99956b99184SRafael J. Wysocki 		/*
100056b99184SRafael J. Wysocki 		 * Cancel the wakeup and process all pending events in case
100156b99184SRafael J. Wysocki 		 * there are any wakeup ones in there.
100256b99184SRafael J. Wysocki 		 *
100356b99184SRafael J. Wysocki 		 * Note that if any non-EC GPEs are active at this point, the
100456b99184SRafael J. Wysocki 		 * SCI will retrigger after the rearming below, so no events
100556b99184SRafael J. Wysocki 		 * should be missed by canceling the wakeup here.
100656b99184SRafael J. Wysocki 		 */
100733e4f80eSRafael J. Wysocki 		pm_system_cancel_wakeup();
100868e22011SRafael J. Wysocki 		/*
100956b99184SRafael J. Wysocki 		 * The EC driver uses the system workqueue and an additional
101056b99184SRafael J. Wysocki 		 * special one, so those need to be flushed too.
101168e22011SRafael J. Wysocki 		 */
101256b99184SRafael J. Wysocki 		acpi_os_wait_events_complete(); /* synchronize EC GPE processing */
1013880a6627SRafael J. Wysocki 		acpi_ec_flush_work();
10146f1d7c45SChris Chiu 		acpi_os_wait_events_complete(); /* synchronize Notify handling */
101533e4f80eSRafael J. Wysocki 	}
101633e4f80eSRafael J. Wysocki 
101756b99184SRafael J. Wysocki 	rearm_wake_irq(acpi_sci_irq);
101856b99184SRafael J. Wysocki }
101956b99184SRafael J. Wysocki 
1020*ac9eafbeSRafael J. Wysocki static void acpi_s2idle_restore_early(void)
1021*ac9eafbeSRafael J. Wysocki {
1022*ac9eafbeSRafael J. Wysocki 	if (!lps0_device_handle || sleep_no_lps0)
1023*ac9eafbeSRafael J. Wysocki 		return;
1024*ac9eafbeSRafael J. Wysocki 
1025*ac9eafbeSRafael J. Wysocki 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT);
1026*ac9eafbeSRafael J. Wysocki 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON);
1027*ac9eafbeSRafael J. Wysocki }
1028*ac9eafbeSRafael J. Wysocki 
102923d5855fSRafael J. Wysocki static void acpi_s2idle_restore(void)
1030a8d46b9eSRafael J. Wysocki {
103141275eb5SRafael J. Wysocki 	s2idle_wakeup = false;
103241275eb5SRafael J. Wysocki 
1033f941d3e4SRafael J. Wysocki 	acpi_enable_all_runtime_gpes();
1034f941d3e4SRafael J. Wysocki 
10352f844b61SRajat Jain 	acpi_disable_wakeup_devices(ACPI_STATE_S0);
10362f844b61SRajat Jain 
10378c01275eSChen Yu 	if (acpi_sci_irq_valid())
10388c01275eSChen Yu 		disable_irq_wake(acpi_sci_irq);
10398110dd28SRafael J. Wysocki }
1040a8d46b9eSRafael J. Wysocki 
104123d5855fSRafael J. Wysocki static void acpi_s2idle_end(void)
10421f0b6386SRafael J. Wysocki {
10431f0b6386SRafael J. Wysocki 	acpi_scan_lock_release();
10441f0b6386SRafael J. Wysocki }
10451f0b6386SRafael J. Wysocki 
104623d5855fSRafael J. Wysocki static const struct platform_s2idle_ops acpi_s2idle_ops = {
104723d5855fSRafael J. Wysocki 	.begin = acpi_s2idle_begin,
104823d5855fSRafael J. Wysocki 	.prepare = acpi_s2idle_prepare,
1049*ac9eafbeSRafael J. Wysocki 	.prepare_late = acpi_s2idle_prepare_late,
105023d5855fSRafael J. Wysocki 	.wake = acpi_s2idle_wake,
1051*ac9eafbeSRafael J. Wysocki 	.restore_early = acpi_s2idle_restore_early,
105223d5855fSRafael J. Wysocki 	.restore = acpi_s2idle_restore,
105323d5855fSRafael J. Wysocki 	.end = acpi_s2idle_end,
10541f0b6386SRafael J. Wysocki };
10551f0b6386SRafael J. Wysocki 
105602040f0bSRafael J. Wysocki static void acpi_sleep_suspend_setup(void)
105702040f0bSRafael J. Wysocki {
105802040f0bSRafael J. Wysocki 	int i;
105902040f0bSRafael J. Wysocki 
1060a4e90bedSRafael J. Wysocki 	for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
1061a4e90bedSRafael J. Wysocki 		if (acpi_sleep_state_supported(i))
106202040f0bSRafael J. Wysocki 			sleep_states[i] = 1;
106302040f0bSRafael J. Wysocki 
106402040f0bSRafael J. Wysocki 	suspend_set_ops(old_suspend_ordering ?
106502040f0bSRafael J. Wysocki 		&acpi_suspend_ops_old : &acpi_suspend_ops);
10668110dd28SRafael J. Wysocki 
10678110dd28SRafael J. Wysocki 	acpi_scan_add_handler(&lps0_handler);
106823d5855fSRafael J. Wysocki 	s2idle_set_ops(&acpi_s2idle_ops);
106902040f0bSRafael J. Wysocki }
10701f0b6386SRafael J. Wysocki 
107102040f0bSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
107233e4f80eSRafael J. Wysocki #define s2idle_wakeup		(false)
10738110dd28SRafael J. Wysocki #define lps0_device_handle	(NULL)
107402040f0bSRafael J. Wysocki static inline void acpi_sleep_suspend_setup(void) {}
107502040f0bSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
1076d08ca2caSLen Brown 
107733e4f80eSRafael J. Wysocki bool acpi_s2idle_wakeup(void)
107833e4f80eSRafael J. Wysocki {
107933e4f80eSRafael J. Wysocki 	return s2idle_wakeup;
108033e4f80eSRafael J. Wysocki }
108133e4f80eSRafael J. Wysocki 
1082504997cfSSudeep Holla #ifdef CONFIG_PM_SLEEP
1083504997cfSSudeep Holla static u32 saved_bm_rld;
1084504997cfSSudeep Holla 
1085504997cfSSudeep Holla static int  acpi_save_bm_rld(void)
1086504997cfSSudeep Holla {
1087504997cfSSudeep Holla 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
1088504997cfSSudeep Holla 	return 0;
1089504997cfSSudeep Holla }
1090504997cfSSudeep Holla 
1091504997cfSSudeep Holla static void  acpi_restore_bm_rld(void)
1092504997cfSSudeep Holla {
1093504997cfSSudeep Holla 	u32 resumed_bm_rld = 0;
1094504997cfSSudeep Holla 
1095504997cfSSudeep Holla 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
1096504997cfSSudeep Holla 	if (resumed_bm_rld == saved_bm_rld)
1097504997cfSSudeep Holla 		return;
1098504997cfSSudeep Holla 
1099504997cfSSudeep Holla 	acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
1100504997cfSSudeep Holla }
1101504997cfSSudeep Holla 
1102504997cfSSudeep Holla static struct syscore_ops acpi_sleep_syscore_ops = {
1103504997cfSSudeep Holla 	.suspend = acpi_save_bm_rld,
1104504997cfSSudeep Holla 	.resume = acpi_restore_bm_rld,
1105504997cfSSudeep Holla };
1106504997cfSSudeep Holla 
1107a042e0c6SJean Delvare static void acpi_sleep_syscore_init(void)
1108504997cfSSudeep Holla {
1109504997cfSSudeep Holla 	register_syscore_ops(&acpi_sleep_syscore_ops);
1110504997cfSSudeep Holla }
1111504997cfSSudeep Holla #else
1112504997cfSSudeep Holla static inline void acpi_sleep_syscore_init(void) {}
1113504997cfSSudeep Holla #endif /* CONFIG_PM_SLEEP */
1114504997cfSSudeep Holla 
1115d08ca2caSLen Brown #ifdef CONFIG_HIBERNATION
1116d08ca2caSLen Brown static unsigned long s4_hardware_signature;
1117d08ca2caSLen Brown static struct acpi_table_facs *facs;
1118d08ca2caSLen Brown static bool nosigcheck;
1119d08ca2caSLen Brown 
1120d08ca2caSLen Brown void __init acpi_no_s4_hw_signature(void)
1121d08ca2caSLen Brown {
1122d08ca2caSLen Brown 	nosigcheck = true;
1123d08ca2caSLen Brown }
1124d08ca2caSLen Brown 
1125bb186901SRafael J. Wysocki static int acpi_hibernation_begin(pm_message_t stage)
1126d08ca2caSLen Brown {
1127bb186901SRafael J. Wysocki 	if (!nvs_nosave) {
1128bb186901SRafael J. Wysocki 		int error = suspend_nvs_alloc();
1129bb186901SRafael J. Wysocki 		if (error)
1130d08ca2caSLen Brown 			return error;
1131d08ca2caSLen Brown 	}
1132d08ca2caSLen Brown 
1133bb186901SRafael J. Wysocki 	if (stage.event == PM_EVENT_HIBERNATE)
1134bb186901SRafael J. Wysocki 		pm_set_suspend_via_firmware();
1135bb186901SRafael J. Wysocki 
1136bb186901SRafael J. Wysocki 	acpi_pm_start(ACPI_STATE_S4);
1137bb186901SRafael J. Wysocki 	return 0;
1138bb186901SRafael J. Wysocki }
1139bb186901SRafael J. Wysocki 
1140d08ca2caSLen Brown static int acpi_hibernation_enter(void)
1141d08ca2caSLen Brown {
1142d08ca2caSLen Brown 	acpi_status status = AE_OK;
1143d08ca2caSLen Brown 
1144d08ca2caSLen Brown 	ACPI_FLUSH_CPU_CACHE();
1145d08ca2caSLen Brown 
1146d08ca2caSLen Brown 	/* This shouldn't return.  If it returns, we have a problem */
11473f6f49c7SLen Brown 	status = acpi_enter_sleep_state(ACPI_STATE_S4);
11483f6f49c7SLen Brown 	/* Reprogram control registers */
11493f6f49c7SLen Brown 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1150d08ca2caSLen Brown 
1151d08ca2caSLen Brown 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
1152d08ca2caSLen Brown }
1153d08ca2caSLen Brown 
1154d08ca2caSLen Brown static void acpi_hibernation_leave(void)
1155d08ca2caSLen Brown {
1156fbda4b38SLukas Wunner 	pm_set_resume_via_firmware();
1157d08ca2caSLen Brown 	/*
1158d08ca2caSLen Brown 	 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
1159d08ca2caSLen Brown 	 * enable it here.
1160d08ca2caSLen Brown 	 */
1161d08ca2caSLen Brown 	acpi_enable();
11623f6f49c7SLen Brown 	/* Reprogram control registers */
11633f6f49c7SLen Brown 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1164d08ca2caSLen Brown 	/* Check the hardware signature */
11655c551e62SOliver Neukum 	if (facs && s4_hardware_signature != facs->hardware_signature)
11665c551e62SOliver Neukum 		pr_crit("ACPI: Hardware changed while hibernated, success doubtful!\n");
1167d08ca2caSLen Brown 	/* Restore the NVS memory area */
1168dd4c4f17SMatthew Garrett 	suspend_nvs_restore();
1169d5a64513SRafael J. Wysocki 	/* Allow EC transactions to happen. */
1170c2b46d67SLv Zheng 	acpi_ec_unblock_transactions();
1171d08ca2caSLen Brown }
1172d08ca2caSLen Brown 
1173d5a64513SRafael J. Wysocki static void acpi_pm_thaw(void)
1174f6bb13aaSRafael J. Wysocki {
1175fe955682SRafael J. Wysocki 	acpi_ec_unblock_transactions();
1176d08ca2caSLen Brown 	acpi_enable_all_runtime_gpes();
1177d08ca2caSLen Brown }
1178d08ca2caSLen Brown 
1179073ef1f6SLionel Debroux static const struct platform_hibernation_ops acpi_hibernation_ops = {
1180d08ca2caSLen Brown 	.begin = acpi_hibernation_begin,
1181d08ca2caSLen Brown 	.end = acpi_pm_end,
1182c5f7a1bbSRafael J. Wysocki 	.pre_snapshot = acpi_pm_prepare,
11832a6b6976SMatthew Garrett 	.finish = acpi_pm_finish,
1184d08ca2caSLen Brown 	.prepare = acpi_pm_prepare,
1185d08ca2caSLen Brown 	.enter = acpi_hibernation_enter,
1186d08ca2caSLen Brown 	.leave = acpi_hibernation_leave,
1187d5a64513SRafael J. Wysocki 	.pre_restore = acpi_pm_freeze,
1188d5a64513SRafael J. Wysocki 	.restore_cleanup = acpi_pm_thaw,
1189d08ca2caSLen Brown };
1190d08ca2caSLen Brown 
1191d08ca2caSLen Brown /**
1192d08ca2caSLen Brown  *	acpi_hibernation_begin_old - Set the target system sleep state to
1193d08ca2caSLen Brown  *		ACPI_STATE_S4 and execute the _PTS control method.  This
1194d08ca2caSLen Brown  *		function is used if the pre-ACPI 2.0 suspend ordering has been
1195d08ca2caSLen Brown  *		requested.
1196d08ca2caSLen Brown  */
1197bb186901SRafael J. Wysocki static int acpi_hibernation_begin_old(pm_message_t stage)
1198d08ca2caSLen Brown {
1199d08ca2caSLen Brown 	int error;
1200d08ca2caSLen Brown 	/*
1201d08ca2caSLen Brown 	 * The _TTS object should always be evaluated before the _PTS object.
1202d08ca2caSLen Brown 	 * When the old_suspended_ordering is true, the _PTS object is
1203d08ca2caSLen Brown 	 * evaluated in the acpi_sleep_prepare.
1204d08ca2caSLen Brown 	 */
1205d08ca2caSLen Brown 	acpi_sleep_tts_switch(ACPI_STATE_S4);
1206d08ca2caSLen Brown 
1207d08ca2caSLen Brown 	error = acpi_sleep_prepare(ACPI_STATE_S4);
1208bb186901SRafael J. Wysocki 	if (error)
1209bb186901SRafael J. Wysocki 		return error;
1210d08ca2caSLen Brown 
1211bb186901SRafael J. Wysocki 	if (!nvs_nosave) {
1212dd4c4f17SMatthew Garrett 		error = suspend_nvs_alloc();
1213bb186901SRafael J. Wysocki 		if (error)
1214bb186901SRafael J. Wysocki 			return error;
1215bb186901SRafael J. Wysocki 	}
1216bb186901SRafael J. Wysocki 
1217bb186901SRafael J. Wysocki 	if (stage.event == PM_EVENT_HIBERNATE)
1218bb186901SRafael J. Wysocki 		pm_set_suspend_via_firmware();
1219bb186901SRafael J. Wysocki 
1220d08ca2caSLen Brown 	acpi_target_sleep_state = ACPI_STATE_S4;
1221ad07277eSRafael J. Wysocki 	acpi_scan_lock_acquire();
1222bb186901SRafael J. Wysocki 	return 0;
1223d08ca2caSLen Brown }
1224d08ca2caSLen Brown 
1225d08ca2caSLen Brown /*
1226d08ca2caSLen Brown  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
1227d08ca2caSLen Brown  * been requested.
1228d08ca2caSLen Brown  */
1229073ef1f6SLionel Debroux static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
1230d08ca2caSLen Brown 	.begin = acpi_hibernation_begin_old,
1231d08ca2caSLen Brown 	.end = acpi_pm_end,
1232c5f7a1bbSRafael J. Wysocki 	.pre_snapshot = acpi_pm_pre_suspend,
1233d5a64513SRafael J. Wysocki 	.prepare = acpi_pm_freeze,
12342a6b6976SMatthew Garrett 	.finish = acpi_pm_finish,
1235d08ca2caSLen Brown 	.enter = acpi_hibernation_enter,
1236d08ca2caSLen Brown 	.leave = acpi_hibernation_leave,
1237d5a64513SRafael J. Wysocki 	.pre_restore = acpi_pm_freeze,
1238d5a64513SRafael J. Wysocki 	.restore_cleanup = acpi_pm_thaw,
1239d08ca2caSLen Brown 	.recover = acpi_pm_finish,
1240d08ca2caSLen Brown };
124102040f0bSRafael J. Wysocki 
124202040f0bSRafael J. Wysocki static void acpi_sleep_hibernate_setup(void)
124302040f0bSRafael J. Wysocki {
1244a4e90bedSRafael J. Wysocki 	if (!acpi_sleep_state_supported(ACPI_STATE_S4))
124502040f0bSRafael J. Wysocki 		return;
124602040f0bSRafael J. Wysocki 
124702040f0bSRafael J. Wysocki 	hibernation_set_ops(old_suspend_ordering ?
124802040f0bSRafael J. Wysocki 			&acpi_hibernation_ops_old : &acpi_hibernation_ops);
124902040f0bSRafael J. Wysocki 	sleep_states[ACPI_STATE_S4] = 1;
125002040f0bSRafael J. Wysocki 	if (nosigcheck)
125102040f0bSRafael J. Wysocki 		return;
125202040f0bSRafael J. Wysocki 
125302040f0bSRafael J. Wysocki 	acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
125402040f0bSRafael J. Wysocki 	if (facs)
125502040f0bSRafael J. Wysocki 		s4_hardware_signature = facs->hardware_signature;
125602040f0bSRafael J. Wysocki }
125702040f0bSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */
125802040f0bSRafael J. Wysocki static inline void acpi_sleep_hibernate_setup(void) {}
125902040f0bSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */
1260d08ca2caSLen Brown 
1261d08ca2caSLen Brown static void acpi_power_off_prepare(void)
1262d08ca2caSLen Brown {
1263d08ca2caSLen Brown 	/* Prepare to power off the system */
1264d08ca2caSLen Brown 	acpi_sleep_prepare(ACPI_STATE_S5);
1265d08ca2caSLen Brown 	acpi_disable_all_gpes();
1266c52fa70cSRafael J. Wysocki 	acpi_os_wait_events_complete();
1267d08ca2caSLen Brown }
1268d08ca2caSLen Brown 
1269d08ca2caSLen Brown static void acpi_power_off(void)
1270d08ca2caSLen Brown {
1271d08ca2caSLen Brown 	/* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
12724d939155SFrank Seidel 	printk(KERN_DEBUG "%s called\n", __func__);
1273d08ca2caSLen Brown 	local_irq_disable();
12743f6f49c7SLen Brown 	acpi_enter_sleep_state(ACPI_STATE_S5);
127596f15efcSLen Brown }
127696f15efcSLen Brown 
1277d08ca2caSLen Brown int __init acpi_sleep_init(void)
1278d08ca2caSLen Brown {
1279ed4cf5b2SJoe Perches 	char supported[ACPI_S_STATE_COUNT * 3 + 1];
1280ed4cf5b2SJoe Perches 	char *pos = supported;
1281ed4cf5b2SJoe Perches 	int i;
1282d08ca2caSLen Brown 
12830ac1b1d7SZhang Rui 	acpi_sleep_dmi_check();
12840ac1b1d7SZhang Rui 
1285d08ca2caSLen Brown 	sleep_states[ACPI_STATE_S0] = 1;
1286d08ca2caSLen Brown 
1287504997cfSSudeep Holla 	acpi_sleep_syscore_init();
128802040f0bSRafael J. Wysocki 	acpi_sleep_suspend_setup();
128902040f0bSRafael J. Wysocki 	acpi_sleep_hibernate_setup();
1290d08ca2caSLen Brown 
1291a4e90bedSRafael J. Wysocki 	if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
1292d08ca2caSLen Brown 		sleep_states[ACPI_STATE_S5] = 1;
1293d08ca2caSLen Brown 		pm_power_off_prepare = acpi_power_off_prepare;
1294d08ca2caSLen Brown 		pm_power_off = acpi_power_off;
129513737181SChen Yu 	} else {
129613737181SChen Yu 		acpi_no_s5 = true;
1297d08ca2caSLen Brown 	}
1298ed4cf5b2SJoe Perches 
1299ed4cf5b2SJoe Perches 	supported[0] = 0;
1300ed4cf5b2SJoe Perches 	for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
1301ed4cf5b2SJoe Perches 		if (sleep_states[i])
1302ed4cf5b2SJoe Perches 			pos += sprintf(pos, " S%d", i);
1303ed4cf5b2SJoe Perches 	}
1304ed4cf5b2SJoe Perches 	pr_info(PREFIX "(supports%s)\n", supported);
1305ed4cf5b2SJoe Perches 
1306d08ca2caSLen Brown 	/*
13079713adc2SRafael J. Wysocki 	 * Register the tts_notifier to reboot notifier list so that the _TTS
13089713adc2SRafael J. Wysocki 	 * object can also be evaluated when the system enters S5.
1309d08ca2caSLen Brown 	 */
13109713adc2SRafael J. Wysocki 	register_reboot_notifier(&tts_notifier);
1311d08ca2caSLen Brown 	return 0;
1312d08ca2caSLen Brown }
1313