xref: /linux/drivers/acpi/sleep.c (revision 2e2c2fdc53437beffd2cf26aaf6187e602d565bc)
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
92091aad6aSJan Beulich static u32 acpi_target_sleep_state = ACPI_STATE_S0;
93a6ae7594SRafael J. Wysocki 
94a6ae7594SRafael J. Wysocki u32 acpi_target_system_state(void)
95a6ae7594SRafael J. Wysocki {
96a6ae7594SRafael J. Wysocki 	return acpi_target_sleep_state;
97a6ae7594SRafael J. Wysocki }
98fad16dd9SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_target_system_state);
99a6ae7594SRafael J. Wysocki 
100a5ca7345SRafael J. Wysocki static bool pwr_btn_event_pending;
101091aad6aSJan Beulich 
102d7f0eea9SZhang Rui /*
10372ad5d77SRafael J. Wysocki  * The ACPI specification wants us to save NVS memory regions during hibernation
10472ad5d77SRafael J. Wysocki  * and to restore them during the subsequent resume.  Windows does that also for
10572ad5d77SRafael J. Wysocki  * suspend to RAM.  However, it is known that this mechanism does not work on
10672ad5d77SRafael J. Wysocki  * all machines, so we allow the user to disable it with the help of the
10772ad5d77SRafael J. Wysocki  * 'acpi_sleep=nonvs' kernel command line option.
10872ad5d77SRafael J. Wysocki  */
10972ad5d77SRafael J. Wysocki static bool nvs_nosave;
11072ad5d77SRafael J. Wysocki 
11172ad5d77SRafael J. Wysocki void __init acpi_nvs_nosave(void)
11272ad5d77SRafael J. Wysocki {
11372ad5d77SRafael J. Wysocki 	nvs_nosave = true;
11472ad5d77SRafael J. Wysocki }
11572ad5d77SRafael J. Wysocki 
11672ad5d77SRafael J. Wysocki /*
1171bad2f19SKristen Carlson Accardi  * The ACPI specification wants us to save NVS memory regions during hibernation
1181bad2f19SKristen Carlson Accardi  * but says nothing about saving NVS during S3.  Not all versions of Windows
1191bad2f19SKristen Carlson Accardi  * save NVS on S3 suspend either, and it is clear that not all systems need
1201bad2f19SKristen Carlson Accardi  * NVS to be saved at S3 time.  To improve suspend/resume time, allow the
1211bad2f19SKristen Carlson Accardi  * user to disable saving NVS on S3 if their system does not require it, but
1221bad2f19SKristen Carlson Accardi  * continue to save/restore NVS for S4 as specified.
1231bad2f19SKristen Carlson Accardi  */
1241bad2f19SKristen Carlson Accardi static bool nvs_nosave_s3;
1251bad2f19SKristen Carlson Accardi 
1261bad2f19SKristen Carlson Accardi void __init acpi_nvs_nosave_s3(void)
1271bad2f19SKristen Carlson Accardi {
1281bad2f19SKristen Carlson Accardi 	nvs_nosave_s3 = true;
1291bad2f19SKristen Carlson Accardi }
1301bad2f19SKristen Carlson Accardi 
131cbc00c13SZhang Rui static int __init init_nvs_save_s3(const struct dmi_system_id *d)
132cbc00c13SZhang Rui {
133cbc00c13SZhang Rui 	nvs_nosave_s3 = false;
134cbc00c13SZhang Rui 	return 0;
135cbc00c13SZhang Rui }
136cbc00c13SZhang Rui 
1371bad2f19SKristen Carlson Accardi /*
138d08ca2caSLen Brown  * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
139d08ca2caSLen Brown  * user to request that behavior by using the 'acpi_old_suspend_ordering'
140d08ca2caSLen Brown  * kernel command line option that causes the following variable to be set.
141d08ca2caSLen Brown  */
142d08ca2caSLen Brown static bool old_suspend_ordering;
143d08ca2caSLen Brown 
144d08ca2caSLen Brown void __init acpi_old_suspend_ordering(void)
145d08ca2caSLen Brown {
146d08ca2caSLen Brown 	old_suspend_ordering = true;
147d08ca2caSLen Brown }
148d08ca2caSLen Brown 
149d08ca2caSLen Brown static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
150d08ca2caSLen Brown {
1510ac1b1d7SZhang Rui 	acpi_old_suspend_ordering();
152d08ca2caSLen Brown 	return 0;
153d08ca2caSLen Brown }
154d08ca2caSLen Brown 
15553998648SRafael J. Wysocki static int __init init_nvs_nosave(const struct dmi_system_id *d)
15653998648SRafael J. Wysocki {
15753998648SRafael J. Wysocki 	acpi_nvs_nosave();
15853998648SRafael J. Wysocki 	return 0;
15953998648SRafael J. Wysocki }
16053998648SRafael J. Wysocki 
16171630b7aSRafael J. Wysocki static bool acpi_sleep_no_lps0;
16271630b7aSRafael J. Wysocki 
16371630b7aSRafael J. Wysocki static int __init init_no_lps0(const struct dmi_system_id *d)
16471630b7aSRafael J. Wysocki {
16571630b7aSRafael J. Wysocki 	acpi_sleep_no_lps0 = true;
16671630b7aSRafael J. Wysocki 	return 0;
16771630b7aSRafael J. Wysocki }
16871630b7aSRafael J. Wysocki 
1696faadbbbSChristoph Hellwig static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
170d08ca2caSLen Brown 	{
171d08ca2caSLen Brown 	.callback = init_old_suspend_ordering,
172d08ca2caSLen Brown 	.ident = "Abit KN9 (nForce4 variant)",
173d08ca2caSLen Brown 	.matches = {
174d08ca2caSLen Brown 		DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
175d08ca2caSLen Brown 		DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
176d08ca2caSLen Brown 		},
177d08ca2caSLen Brown 	},
178d08ca2caSLen Brown 	{
179d08ca2caSLen Brown 	.callback = init_old_suspend_ordering,
180d08ca2caSLen Brown 	.ident = "HP xw4600 Workstation",
181d08ca2caSLen Brown 	.matches = {
182d08ca2caSLen Brown 		DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
183d08ca2caSLen Brown 		DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
184d08ca2caSLen Brown 		},
185d08ca2caSLen Brown 	},
186d08ca2caSLen Brown 	{
187a1404495SAndy Whitcroft 	.callback = init_old_suspend_ordering,
188a1404495SAndy Whitcroft 	.ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
189a1404495SAndy Whitcroft 	.matches = {
190a1404495SAndy Whitcroft 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
191a1404495SAndy Whitcroft 		DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
192a1404495SAndy Whitcroft 		},
193a1404495SAndy Whitcroft 	},
19445e77988SZhang Rui 	{
1952a9ef8e1SZhao Yakui 	.callback = init_old_suspend_ordering,
1962a9ef8e1SZhao Yakui 	.ident = "Panasonic CF51-2L",
1972a9ef8e1SZhao Yakui 	.matches = {
1982a9ef8e1SZhao Yakui 		DMI_MATCH(DMI_BOARD_VENDOR,
1992a9ef8e1SZhao Yakui 				"Matsushita Electric Industrial Co.,Ltd."),
2002a9ef8e1SZhao Yakui 		DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
2012a9ef8e1SZhao Yakui 		},
2022a9ef8e1SZhao Yakui 	},
20353998648SRafael J. Wysocki 	{
20453998648SRafael J. Wysocki 	.callback = init_nvs_nosave,
20566f2fda9SJoseph Salisbury 	.ident = "Sony Vaio VGN-FW41E_H",
20666f2fda9SJoseph Salisbury 	.matches = {
20766f2fda9SJoseph Salisbury 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
20866f2fda9SJoseph Salisbury 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
20966f2fda9SJoseph Salisbury 		},
21066f2fda9SJoseph Salisbury 	},
21166f2fda9SJoseph Salisbury 	{
21266f2fda9SJoseph Salisbury 	.callback = init_nvs_nosave,
213d11c78e9SDave Jones 	.ident = "Sony Vaio VGN-FW21E",
214d11c78e9SDave Jones 	.matches = {
215d11c78e9SDave Jones 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
216d11c78e9SDave Jones 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
217d11c78e9SDave Jones 		},
218d11c78e9SDave Jones 	},
219d11c78e9SDave Jones 	{
220d11c78e9SDave Jones 	.callback = init_nvs_nosave,
221469dd1c4SFabio Valentini 	.ident = "Sony Vaio VGN-FW21M",
222469dd1c4SFabio Valentini 	.matches = {
223469dd1c4SFabio Valentini 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
224469dd1c4SFabio Valentini 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
225469dd1c4SFabio Valentini 		},
226469dd1c4SFabio Valentini 	},
227469dd1c4SFabio Valentini 	{
228469dd1c4SFabio Valentini 	.callback = init_nvs_nosave,
229ddf6ce45SDave Jones 	.ident = "Sony Vaio VPCEB17FX",
230ddf6ce45SDave Jones 	.matches = {
231ddf6ce45SDave Jones 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
232ddf6ce45SDave Jones 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
233ddf6ce45SDave Jones 		},
234ddf6ce45SDave Jones 	},
235ddf6ce45SDave Jones 	{
236ddf6ce45SDave Jones 	.callback = init_nvs_nosave,
23753998648SRafael J. Wysocki 	.ident = "Sony Vaio VGN-SR11M",
23853998648SRafael J. Wysocki 	.matches = {
23953998648SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
24053998648SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
24153998648SRafael J. Wysocki 		},
24253998648SRafael J. Wysocki 	},
24353998648SRafael J. Wysocki 	{
24453998648SRafael J. Wysocki 	.callback = init_nvs_nosave,
24553998648SRafael J. Wysocki 	.ident = "Everex StepNote Series",
24653998648SRafael J. Wysocki 	.matches = {
24753998648SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
24853998648SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
24953998648SRafael J. Wysocki 		},
25053998648SRafael J. Wysocki 	},
251af48931cSRafael J. Wysocki 	{
252af48931cSRafael J. Wysocki 	.callback = init_nvs_nosave,
253af48931cSRafael J. Wysocki 	.ident = "Sony Vaio VPCEB1Z1E",
254af48931cSRafael J. Wysocki 	.matches = {
255af48931cSRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
256af48931cSRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
257af48931cSRafael J. Wysocki 		},
258af48931cSRafael J. Wysocki 	},
259291a73c9SRafael J. Wysocki 	{
260291a73c9SRafael J. Wysocki 	.callback = init_nvs_nosave,
261291a73c9SRafael J. Wysocki 	.ident = "Sony Vaio VGN-NW130D",
262291a73c9SRafael J. Wysocki 	.matches = {
263291a73c9SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
264291a73c9SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
265291a73c9SRafael J. Wysocki 		},
266291a73c9SRafael J. Wysocki 	},
2677b330707SRafael J. Wysocki 	{
2687b330707SRafael J. Wysocki 	.callback = init_nvs_nosave,
26993f77084SLan Tianyu 	.ident = "Sony Vaio VPCCW29FX",
27093f77084SLan Tianyu 	.matches = {
27193f77084SLan Tianyu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
27293f77084SLan Tianyu 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
27393f77084SLan Tianyu 		},
27493f77084SLan Tianyu 	},
27593f77084SLan Tianyu 	{
27693f77084SLan Tianyu 	.callback = init_nvs_nosave,
2777b330707SRafael J. Wysocki 	.ident = "Averatec AV1020-ED2",
2787b330707SRafael J. Wysocki 	.matches = {
2797b330707SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
2807b330707SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
2817b330707SRafael J. Wysocki 		},
2827b330707SRafael J. Wysocki 	},
283bb0c5ed6SZhang Rui 	{
284bb0c5ed6SZhang Rui 	.callback = init_old_suspend_ordering,
285bb0c5ed6SZhang Rui 	.ident = "Asus A8N-SLI DELUXE",
286bb0c5ed6SZhang Rui 	.matches = {
287bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
288bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
289bb0c5ed6SZhang Rui 		},
290bb0c5ed6SZhang Rui 	},
291bb0c5ed6SZhang Rui 	{
292bb0c5ed6SZhang Rui 	.callback = init_old_suspend_ordering,
293bb0c5ed6SZhang Rui 	.ident = "Asus A8N-SLI Premium",
294bb0c5ed6SZhang Rui 	.matches = {
295bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
296bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
297bb0c5ed6SZhang Rui 		},
298bb0c5ed6SZhang Rui 	},
29989e8ea12SRafael J. Wysocki 	{
30089e8ea12SRafael J. Wysocki 	.callback = init_nvs_nosave,
30189e8ea12SRafael J. Wysocki 	.ident = "Sony Vaio VGN-SR26GN_P",
30289e8ea12SRafael J. Wysocki 	.matches = {
30389e8ea12SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
30489e8ea12SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
30589e8ea12SRafael J. Wysocki 		},
30689e8ea12SRafael J. Wysocki 	},
307731b25a4SBogdan Radulescu 	{
308731b25a4SBogdan Radulescu 	.callback = init_nvs_nosave,
309876ab790SLan Tianyu 	.ident = "Sony Vaio VPCEB1S1E",
310876ab790SLan Tianyu 	.matches = {
311876ab790SLan Tianyu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
312876ab790SLan Tianyu 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
313876ab790SLan Tianyu 		},
314876ab790SLan Tianyu 	},
315876ab790SLan Tianyu 	{
316876ab790SLan Tianyu 	.callback = init_nvs_nosave,
317731b25a4SBogdan Radulescu 	.ident = "Sony Vaio VGN-FW520F",
318731b25a4SBogdan Radulescu 	.matches = {
319731b25a4SBogdan Radulescu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
320731b25a4SBogdan Radulescu 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
321731b25a4SBogdan Radulescu 		},
322731b25a4SBogdan Radulescu 	},
3235a50a7c3SKeng-Yu Lin 	{
3245a50a7c3SKeng-Yu Lin 	.callback = init_nvs_nosave,
3255a50a7c3SKeng-Yu Lin 	.ident = "Asus K54C",
3265a50a7c3SKeng-Yu Lin 	.matches = {
3275a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
3285a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
3295a50a7c3SKeng-Yu Lin 		},
3305a50a7c3SKeng-Yu Lin 	},
3315a50a7c3SKeng-Yu Lin 	{
3325a50a7c3SKeng-Yu Lin 	.callback = init_nvs_nosave,
3335a50a7c3SKeng-Yu Lin 	.ident = "Asus K54HR",
3345a50a7c3SKeng-Yu Lin 	.matches = {
3355a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
3365a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
3375a50a7c3SKeng-Yu Lin 		},
3385a50a7c3SKeng-Yu Lin 	},
339231f9415SWilly Tarreau 	{
340231f9415SWilly Tarreau 	.callback = init_nvs_save_s3,
341231f9415SWilly Tarreau 	.ident = "Asus 1025C",
342231f9415SWilly Tarreau 	.matches = {
343231f9415SWilly Tarreau 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
344231f9415SWilly Tarreau 		DMI_MATCH(DMI_PRODUCT_NAME, "1025C"),
345231f9415SWilly Tarreau 		},
346231f9415SWilly Tarreau 	},
347cbc00c13SZhang Rui 	/*
348cbc00c13SZhang Rui 	 * https://bugzilla.kernel.org/show_bug.cgi?id=189431
349cbc00c13SZhang Rui 	 * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
350cbc00c13SZhang Rui 	 * saving during S3.
351cbc00c13SZhang Rui 	 */
352cbc00c13SZhang Rui 	{
353cbc00c13SZhang Rui 	.callback = init_nvs_save_s3,
354cbc00c13SZhang Rui 	.ident = "Lenovo G50-45",
355cbc00c13SZhang Rui 	.matches = {
356cbc00c13SZhang Rui 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
357cbc00c13SZhang Rui 		DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
358cbc00c13SZhang Rui 		},
359cbc00c13SZhang Rui 	},
36071630b7aSRafael J. Wysocki 	/*
36171630b7aSRafael J. Wysocki 	 * https://bugzilla.kernel.org/show_bug.cgi?id=196907
36271630b7aSRafael J. Wysocki 	 * Some Dell XPS13 9360 cannot do suspend-to-idle using the Low Power
36371630b7aSRafael J. Wysocki 	 * S0 Idle firmware interface.
36471630b7aSRafael J. Wysocki 	 */
36571630b7aSRafael J. Wysocki 	{
36671630b7aSRafael J. Wysocki 	.callback = init_no_lps0,
36771630b7aSRafael J. Wysocki 	.ident = "Dell XPS13 9360",
36871630b7aSRafael J. Wysocki 	.matches = {
36971630b7aSRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
37071630b7aSRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
37171630b7aSRafael J. Wysocki 		},
37271630b7aSRafael J. Wysocki 	},
373855c1c2fSChen Yu 	/*
374855c1c2fSChen Yu 	 * ThinkPad X1 Tablet(2016) cannot do suspend-to-idle using
375855c1c2fSChen Yu 	 * the Low Power S0 Idle firmware interface (see
376855c1c2fSChen Yu 	 * https://bugzilla.kernel.org/show_bug.cgi?id=199057).
377855c1c2fSChen Yu 	 */
378855c1c2fSChen Yu 	{
379855c1c2fSChen Yu 	.callback = init_no_lps0,
380855c1c2fSChen Yu 	.ident = "ThinkPad X1 Tablet(2016)",
381855c1c2fSChen Yu 	.matches = {
382855c1c2fSChen Yu 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
383855c1c2fSChen Yu 		DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
384855c1c2fSChen Yu 		},
385855c1c2fSChen Yu 	},
386d08ca2caSLen Brown 	{},
387d08ca2caSLen Brown };
3880ac1b1d7SZhang Rui 
38957044031SRafael J. Wysocki static bool ignore_blacklist;
39057044031SRafael J. Wysocki 
39157044031SRafael J. Wysocki void __init acpi_sleep_no_blacklist(void)
39257044031SRafael J. Wysocki {
39357044031SRafael J. Wysocki 	ignore_blacklist = true;
39457044031SRafael J. Wysocki }
39557044031SRafael J. Wysocki 
3968b691c9cSJan Beulich static void __init acpi_sleep_dmi_check(void)
3970ac1b1d7SZhang Rui {
39857044031SRafael J. Wysocki 	if (ignore_blacklist)
39957044031SRafael J. Wysocki 		return;
40057044031SRafael J. Wysocki 
4019c0a30b6SAndy Shevchenko 	if (dmi_get_bios_year() >= 2012)
402821d6f03SLan Tianyu 		acpi_nvs_nosave_s3();
403821d6f03SLan Tianyu 
4040ac1b1d7SZhang Rui 	dmi_check_system(acpisleep_dmi_table);
4050ac1b1d7SZhang Rui }
4060ac1b1d7SZhang Rui 
4070ac1b1d7SZhang Rui /**
4080ac1b1d7SZhang Rui  * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
4090ac1b1d7SZhang Rui  */
4100ac1b1d7SZhang Rui static int acpi_pm_freeze(void)
4110ac1b1d7SZhang Rui {
4120ac1b1d7SZhang Rui 	acpi_disable_all_gpes();
4130ac1b1d7SZhang Rui 	acpi_os_wait_events_complete();
4140ac1b1d7SZhang Rui 	acpi_ec_block_transactions();
4150ac1b1d7SZhang Rui 	return 0;
4160ac1b1d7SZhang Rui }
4170ac1b1d7SZhang Rui 
4180ac1b1d7SZhang Rui /**
4190ac1b1d7SZhang Rui  * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
4200ac1b1d7SZhang Rui  */
4210ac1b1d7SZhang Rui static int acpi_pm_pre_suspend(void)
4220ac1b1d7SZhang Rui {
4230ac1b1d7SZhang Rui 	acpi_pm_freeze();
4240ac1b1d7SZhang Rui 	return suspend_nvs_save();
4250ac1b1d7SZhang Rui }
4260ac1b1d7SZhang Rui 
4270ac1b1d7SZhang Rui /**
4280ac1b1d7SZhang Rui  *	__acpi_pm_prepare - Prepare the platform to enter the target state.
4290ac1b1d7SZhang Rui  *
4300ac1b1d7SZhang Rui  *	If necessary, set the firmware waking vector and do arch-specific
4310ac1b1d7SZhang Rui  *	nastiness to get the wakeup code to the waking vector.
4320ac1b1d7SZhang Rui  */
4330ac1b1d7SZhang Rui static int __acpi_pm_prepare(void)
4340ac1b1d7SZhang Rui {
4350ac1b1d7SZhang Rui 	int error = acpi_sleep_prepare(acpi_target_sleep_state);
4360ac1b1d7SZhang Rui 	if (error)
4370ac1b1d7SZhang Rui 		acpi_target_sleep_state = ACPI_STATE_S0;
4380ac1b1d7SZhang Rui 
4390ac1b1d7SZhang Rui 	return error;
4400ac1b1d7SZhang Rui }
4410ac1b1d7SZhang Rui 
4420ac1b1d7SZhang Rui /**
4430ac1b1d7SZhang Rui  *	acpi_pm_prepare - Prepare the platform to enter the target sleep
4440ac1b1d7SZhang Rui  *		state and disable the GPEs.
4450ac1b1d7SZhang Rui  */
4460ac1b1d7SZhang Rui static int acpi_pm_prepare(void)
4470ac1b1d7SZhang Rui {
4480ac1b1d7SZhang Rui 	int error = __acpi_pm_prepare();
4490ac1b1d7SZhang Rui 	if (!error)
4500ac1b1d7SZhang Rui 		error = acpi_pm_pre_suspend();
4510ac1b1d7SZhang Rui 
4520ac1b1d7SZhang Rui 	return error;
4530ac1b1d7SZhang Rui }
4540ac1b1d7SZhang Rui 
4550ac1b1d7SZhang Rui /**
4560ac1b1d7SZhang Rui  *	acpi_pm_finish - Instruct the platform to leave a sleep state.
4570ac1b1d7SZhang Rui  *
4580ac1b1d7SZhang Rui  *	This is called after we wake back up (or if entering the sleep state
4590ac1b1d7SZhang Rui  *	failed).
4600ac1b1d7SZhang Rui  */
4610ac1b1d7SZhang Rui static void acpi_pm_finish(void)
4620ac1b1d7SZhang Rui {
463a3487d8fSAndy Shevchenko 	struct acpi_device *pwr_btn_adev;
4640ac1b1d7SZhang Rui 	u32 acpi_state = acpi_target_sleep_state;
4650ac1b1d7SZhang Rui 
4660ac1b1d7SZhang Rui 	acpi_ec_unblock_transactions();
4670ac1b1d7SZhang Rui 	suspend_nvs_free();
4680ac1b1d7SZhang Rui 
4690ac1b1d7SZhang Rui 	if (acpi_state == ACPI_STATE_S0)
4700ac1b1d7SZhang Rui 		return;
4710ac1b1d7SZhang Rui 
4720ac1b1d7SZhang Rui 	printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
4730ac1b1d7SZhang Rui 		acpi_state);
4740ac1b1d7SZhang Rui 	acpi_disable_wakeup_devices(acpi_state);
4750ac1b1d7SZhang Rui 	acpi_leave_sleep_state(acpi_state);
4760ac1b1d7SZhang Rui 
4770ac1b1d7SZhang Rui 	/* reset firmware waking vector */
478e3e9b577SRafael J. Wysocki 	acpi_set_waking_vector(0);
4790ac1b1d7SZhang Rui 
4800ac1b1d7SZhang Rui 	acpi_target_sleep_state = ACPI_STATE_S0;
4810ac1b1d7SZhang Rui 
482781d737cSRafael J. Wysocki 	acpi_resume_power_resources();
483781d737cSRafael J. Wysocki 
4840ac1b1d7SZhang Rui 	/* If we were woken with the fixed power button, provide a small
4850ac1b1d7SZhang Rui 	 * hint to userspace in the form of a wakeup event on the fixed power
4860ac1b1d7SZhang Rui 	 * button device (if it can be found).
4870ac1b1d7SZhang Rui 	 *
4880ac1b1d7SZhang Rui 	 * We delay the event generation til now, as the PM layer requires
4890ac1b1d7SZhang Rui 	 * timekeeping to be running before we generate events. */
4900ac1b1d7SZhang Rui 	if (!pwr_btn_event_pending)
4910ac1b1d7SZhang Rui 		return;
4920ac1b1d7SZhang Rui 
4930ac1b1d7SZhang Rui 	pwr_btn_event_pending = false;
494a3487d8fSAndy Shevchenko 	pwr_btn_adev = acpi_dev_get_first_match_dev(ACPI_BUTTON_HID_POWERF,
495a3487d8fSAndy Shevchenko 						    NULL, -1);
496a3487d8fSAndy Shevchenko 	if (pwr_btn_adev) {
497a3487d8fSAndy Shevchenko 		pm_wakeup_event(&pwr_btn_adev->dev, 0);
498a3487d8fSAndy Shevchenko 		acpi_dev_put(pwr_btn_adev);
4990ac1b1d7SZhang Rui 	}
5000ac1b1d7SZhang Rui }
5010ac1b1d7SZhang Rui 
5020ac1b1d7SZhang Rui /**
503ad07277eSRafael J. Wysocki  * acpi_pm_start - Start system PM transition.
504ad07277eSRafael J. Wysocki  */
505ad07277eSRafael J. Wysocki static void acpi_pm_start(u32 acpi_state)
506ad07277eSRafael J. Wysocki {
507ad07277eSRafael J. Wysocki 	acpi_target_sleep_state = acpi_state;
508ad07277eSRafael J. Wysocki 	acpi_sleep_tts_switch(acpi_target_sleep_state);
509ad07277eSRafael J. Wysocki 	acpi_scan_lock_acquire();
510ad07277eSRafael J. Wysocki }
511ad07277eSRafael J. Wysocki 
512ad07277eSRafael J. Wysocki /**
513ad07277eSRafael J. Wysocki  * acpi_pm_end - Finish up system PM transition.
5140ac1b1d7SZhang Rui  */
5150ac1b1d7SZhang Rui static void acpi_pm_end(void)
5160ac1b1d7SZhang Rui {
5178ece1d83SHans de Goede 	acpi_turn_off_unused_power_resources();
518ad07277eSRafael J. Wysocki 	acpi_scan_lock_release();
5190ac1b1d7SZhang Rui 	/*
5200ac1b1d7SZhang Rui 	 * This is necessary in case acpi_pm_finish() is not called during a
5210ac1b1d7SZhang Rui 	 * failing transition to a sleep state.
5220ac1b1d7SZhang Rui 	 */
5230ac1b1d7SZhang Rui 	acpi_target_sleep_state = ACPI_STATE_S0;
5240ac1b1d7SZhang Rui 	acpi_sleep_tts_switch(acpi_target_sleep_state);
5250ac1b1d7SZhang Rui }
5260ac1b1d7SZhang Rui #else /* !CONFIG_ACPI_SLEEP */
5270ac1b1d7SZhang Rui #define acpi_target_sleep_state	ACPI_STATE_S0
52871630b7aSRafael J. Wysocki #define acpi_sleep_no_lps0	(false)
5290ac1b1d7SZhang Rui static inline void acpi_sleep_dmi_check(void) {}
5300ac1b1d7SZhang Rui #endif /* CONFIG_ACPI_SLEEP */
5310ac1b1d7SZhang Rui 
5320ac1b1d7SZhang Rui #ifdef CONFIG_SUSPEND
5330ac1b1d7SZhang Rui static u32 acpi_suspend_states[] = {
5340ac1b1d7SZhang Rui 	[PM_SUSPEND_ON] = ACPI_STATE_S0,
5350ac1b1d7SZhang Rui 	[PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
5360ac1b1d7SZhang Rui 	[PM_SUSPEND_MEM] = ACPI_STATE_S3,
5370ac1b1d7SZhang Rui 	[PM_SUSPEND_MAX] = ACPI_STATE_S5
5380ac1b1d7SZhang Rui };
5390ac1b1d7SZhang Rui 
5400ac1b1d7SZhang Rui /**
5410ac1b1d7SZhang Rui  *	acpi_suspend_begin - Set the target system sleep state to the state
5420ac1b1d7SZhang Rui  *		associated with given @pm_state, if supported.
5430ac1b1d7SZhang Rui  */
5440ac1b1d7SZhang Rui static int acpi_suspend_begin(suspend_state_t pm_state)
5450ac1b1d7SZhang Rui {
5460ac1b1d7SZhang Rui 	u32 acpi_state = acpi_suspend_states[pm_state];
547ad07277eSRafael J. Wysocki 	int error;
5480ac1b1d7SZhang Rui 
5490ac1b1d7SZhang Rui 	error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
5500ac1b1d7SZhang Rui 	if (error)
5510ac1b1d7SZhang Rui 		return error;
5520ac1b1d7SZhang Rui 
553ad07277eSRafael J. Wysocki 	if (!sleep_states[acpi_state]) {
554ad07277eSRafael J. Wysocki 		pr_err("ACPI does not support sleep state S%u\n", acpi_state);
555ad07277eSRafael J. Wysocki 		return -ENOSYS;
5560ac1b1d7SZhang Rui 	}
557ef25ba04SRafael J. Wysocki 	if (acpi_state > ACPI_STATE_S1)
558ef25ba04SRafael J. Wysocki 		pm_set_suspend_via_firmware();
559ad07277eSRafael J. Wysocki 
560ad07277eSRafael J. Wysocki 	acpi_pm_start(acpi_state);
561ad07277eSRafael J. Wysocki 	return 0;
5620ac1b1d7SZhang Rui }
5630ac1b1d7SZhang Rui 
5640ac1b1d7SZhang Rui /**
5650ac1b1d7SZhang Rui  *	acpi_suspend_enter - Actually enter a sleep state.
5660ac1b1d7SZhang Rui  *	@pm_state: ignored
5670ac1b1d7SZhang Rui  *
5680ac1b1d7SZhang Rui  *	Flush caches and go to sleep. For STR we have to call arch-specific
5690ac1b1d7SZhang Rui  *	assembly, which in turn call acpi_enter_sleep_state().
5700ac1b1d7SZhang Rui  *	It's unfortunate, but it works. Please fix if you're feeling frisky.
5710ac1b1d7SZhang Rui  */
5720ac1b1d7SZhang Rui static int acpi_suspend_enter(suspend_state_t pm_state)
5730ac1b1d7SZhang Rui {
5740ac1b1d7SZhang Rui 	acpi_status status = AE_OK;
5750ac1b1d7SZhang Rui 	u32 acpi_state = acpi_target_sleep_state;
5760ac1b1d7SZhang Rui 	int error;
5770ac1b1d7SZhang Rui 
5780ac1b1d7SZhang Rui 	ACPI_FLUSH_CPU_CACHE();
5790ac1b1d7SZhang Rui 
580bb3632c6STodd E Brandt 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
5810ac1b1d7SZhang Rui 	switch (acpi_state) {
5820ac1b1d7SZhang Rui 	case ACPI_STATE_S1:
5830ac1b1d7SZhang Rui 		barrier();
5840ac1b1d7SZhang Rui 		status = acpi_enter_sleep_state(acpi_state);
5850ac1b1d7SZhang Rui 		break;
5860ac1b1d7SZhang Rui 
5870ac1b1d7SZhang Rui 	case ACPI_STATE_S3:
588d6a77eadSKonrad Rzeszutek Wilk 		if (!acpi_suspend_lowlevel)
589d6a77eadSKonrad Rzeszutek Wilk 			return -ENOSYS;
5900ac1b1d7SZhang Rui 		error = acpi_suspend_lowlevel();
5910ac1b1d7SZhang Rui 		if (error)
5920ac1b1d7SZhang Rui 			return error;
5930ac1b1d7SZhang Rui 		pr_info(PREFIX "Low-level resume complete\n");
594ef25ba04SRafael J. Wysocki 		pm_set_resume_via_firmware();
5950ac1b1d7SZhang Rui 		break;
5960ac1b1d7SZhang Rui 	}
597bb3632c6STodd E Brandt 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
5980ac1b1d7SZhang Rui 
5990ac1b1d7SZhang Rui 	/* This violates the spec but is required for bug compatibility. */
6000ac1b1d7SZhang Rui 	acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
6010ac1b1d7SZhang Rui 
6020ac1b1d7SZhang Rui 	/* Reprogram control registers */
6030ac1b1d7SZhang Rui 	acpi_leave_sleep_state_prep(acpi_state);
6040ac1b1d7SZhang Rui 
6050ac1b1d7SZhang Rui 	/* ACPI 3.0 specs (P62) says that it's the responsibility
6060ac1b1d7SZhang Rui 	 * of the OSPM to clear the status bit [ implying that the
6070ac1b1d7SZhang Rui 	 * POWER_BUTTON event should not reach userspace ]
6080ac1b1d7SZhang Rui 	 *
6090ac1b1d7SZhang Rui 	 * However, we do generate a small hint for userspace in the form of
6100ac1b1d7SZhang Rui 	 * a wakeup event. We flag this condition for now and generate the
6110ac1b1d7SZhang Rui 	 * event later, as we're currently too early in resume to be able to
6120ac1b1d7SZhang Rui 	 * generate wakeup events.
6130ac1b1d7SZhang Rui 	 */
6140ac1b1d7SZhang Rui 	if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
61551468a9dSAl Stone 		acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
6160ac1b1d7SZhang Rui 
6170ac1b1d7SZhang Rui 		acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
6180ac1b1d7SZhang Rui 
61918864cc4SLv Zheng 		if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
6200ac1b1d7SZhang Rui 			acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
6210ac1b1d7SZhang Rui 			/* Flag for later */
6220ac1b1d7SZhang Rui 			pwr_btn_event_pending = true;
6230ac1b1d7SZhang Rui 		}
6240ac1b1d7SZhang Rui 	}
6250ac1b1d7SZhang Rui 
6260ac1b1d7SZhang Rui 	/*
6270ac1b1d7SZhang Rui 	 * Disable and clear GPE status before interrupt is enabled. Some GPEs
6280ac1b1d7SZhang Rui 	 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
6290ac1b1d7SZhang Rui 	 * acpi_leave_sleep_state will reenable specific GPEs later
6300ac1b1d7SZhang Rui 	 */
6310ac1b1d7SZhang Rui 	acpi_disable_all_gpes();
6320ac1b1d7SZhang Rui 	/* Allow EC transactions to happen. */
633c2b46d67SLv Zheng 	acpi_ec_unblock_transactions();
6340ac1b1d7SZhang Rui 
6350ac1b1d7SZhang Rui 	suspend_nvs_restore();
6360ac1b1d7SZhang Rui 
6370ac1b1d7SZhang Rui 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
6380ac1b1d7SZhang Rui }
6390ac1b1d7SZhang Rui 
6400ac1b1d7SZhang Rui static int acpi_suspend_state_valid(suspend_state_t pm_state)
6410ac1b1d7SZhang Rui {
6420ac1b1d7SZhang Rui 	u32 acpi_state;
6430ac1b1d7SZhang Rui 
6440ac1b1d7SZhang Rui 	switch (pm_state) {
6450ac1b1d7SZhang Rui 	case PM_SUSPEND_ON:
6460ac1b1d7SZhang Rui 	case PM_SUSPEND_STANDBY:
6470ac1b1d7SZhang Rui 	case PM_SUSPEND_MEM:
6480ac1b1d7SZhang Rui 		acpi_state = acpi_suspend_states[pm_state];
6490ac1b1d7SZhang Rui 
6500ac1b1d7SZhang Rui 		return sleep_states[acpi_state];
6510ac1b1d7SZhang Rui 	default:
6520ac1b1d7SZhang Rui 		return 0;
6530ac1b1d7SZhang Rui 	}
6540ac1b1d7SZhang Rui }
6550ac1b1d7SZhang Rui 
6560ac1b1d7SZhang Rui static const struct platform_suspend_ops acpi_suspend_ops = {
6570ac1b1d7SZhang Rui 	.valid = acpi_suspend_state_valid,
6580ac1b1d7SZhang Rui 	.begin = acpi_suspend_begin,
6590ac1b1d7SZhang Rui 	.prepare_late = acpi_pm_prepare,
6600ac1b1d7SZhang Rui 	.enter = acpi_suspend_enter,
6610ac1b1d7SZhang Rui 	.wake = acpi_pm_finish,
6620ac1b1d7SZhang Rui 	.end = acpi_pm_end,
6630ac1b1d7SZhang Rui };
6640ac1b1d7SZhang Rui 
6650ac1b1d7SZhang Rui /**
6660ac1b1d7SZhang Rui  *	acpi_suspend_begin_old - Set the target system sleep state to the
6670ac1b1d7SZhang Rui  *		state associated with given @pm_state, if supported, and
6680ac1b1d7SZhang Rui  *		execute the _PTS control method.  This function is used if the
6690ac1b1d7SZhang Rui  *		pre-ACPI 2.0 suspend ordering has been requested.
6700ac1b1d7SZhang Rui  */
6710ac1b1d7SZhang Rui static int acpi_suspend_begin_old(suspend_state_t pm_state)
6720ac1b1d7SZhang Rui {
6730ac1b1d7SZhang Rui 	int error = acpi_suspend_begin(pm_state);
6740ac1b1d7SZhang Rui 	if (!error)
6750ac1b1d7SZhang Rui 		error = __acpi_pm_prepare();
6760ac1b1d7SZhang Rui 
6770ac1b1d7SZhang Rui 	return error;
6780ac1b1d7SZhang Rui }
6790ac1b1d7SZhang Rui 
6800ac1b1d7SZhang Rui /*
6810ac1b1d7SZhang Rui  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
6820ac1b1d7SZhang Rui  * been requested.
6830ac1b1d7SZhang Rui  */
6840ac1b1d7SZhang Rui static const struct platform_suspend_ops acpi_suspend_ops_old = {
6850ac1b1d7SZhang Rui 	.valid = acpi_suspend_state_valid,
6860ac1b1d7SZhang Rui 	.begin = acpi_suspend_begin_old,
6870ac1b1d7SZhang Rui 	.prepare_late = acpi_pm_pre_suspend,
6880ac1b1d7SZhang Rui 	.enter = acpi_suspend_enter,
6890ac1b1d7SZhang Rui 	.wake = acpi_pm_finish,
6900ac1b1d7SZhang Rui 	.end = acpi_pm_end,
6910ac1b1d7SZhang Rui 	.recover = acpi_pm_finish,
6920ac1b1d7SZhang Rui };
69302040f0bSRafael J. Wysocki 
6948110dd28SRafael J. Wysocki static bool s2idle_in_progress;
69533e4f80eSRafael J. Wysocki static bool s2idle_wakeup;
69633e4f80eSRafael J. Wysocki 
6978110dd28SRafael J. Wysocki /*
6988110dd28SRafael J. Wysocki  * On platforms supporting the Low Power S0 Idle interface there is an ACPI
6998110dd28SRafael J. Wysocki  * device object with the PNP0D80 compatible device ID (System Power Management
7008110dd28SRafael J. Wysocki  * Controller) and a specific _DSM method under it.  That method, if present,
7018110dd28SRafael J. Wysocki  * can be used to indicate to the platform that the OS is transitioning into a
7028110dd28SRafael J. Wysocki  * low-power state in which certain types of activity are not desirable or that
7038110dd28SRafael J. Wysocki  * it is leaving such a state, which allows the platform to adjust its operation
7048110dd28SRafael J. Wysocki  * mode accordingly.
7058110dd28SRafael J. Wysocki  */
7068110dd28SRafael J. Wysocki static const struct acpi_device_id lps0_device_ids[] = {
7078110dd28SRafael J. Wysocki 	{"PNP0D80", },
7088110dd28SRafael J. Wysocki 	{"", },
7098110dd28SRafael J. Wysocki };
7108110dd28SRafael J. Wysocki 
7118110dd28SRafael J. Wysocki #define ACPI_LPS0_DSM_UUID	"c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
7128110dd28SRafael J. Wysocki 
713726fb6b4SSrinivas Pandruvada #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS	1
7148110dd28SRafael J. Wysocki #define ACPI_LPS0_SCREEN_OFF	3
7158110dd28SRafael J. Wysocki #define ACPI_LPS0_SCREEN_ON	4
7168110dd28SRafael J. Wysocki #define ACPI_LPS0_ENTRY		5
7178110dd28SRafael J. Wysocki #define ACPI_LPS0_EXIT		6
7188110dd28SRafael J. Wysocki 
7198110dd28SRafael J. Wysocki static acpi_handle lps0_device_handle;
7208110dd28SRafael J. Wysocki static guid_t lps0_dsm_guid;
7218110dd28SRafael J. Wysocki static char lps0_dsm_func_mask;
7228110dd28SRafael J. Wysocki 
723726fb6b4SSrinivas Pandruvada /* Device constraint entry structure */
724726fb6b4SSrinivas Pandruvada struct lpi_device_info {
725726fb6b4SSrinivas Pandruvada 	char *name;
726726fb6b4SSrinivas Pandruvada 	int enabled;
727726fb6b4SSrinivas Pandruvada 	union acpi_object *package;
728726fb6b4SSrinivas Pandruvada };
729726fb6b4SSrinivas Pandruvada 
730726fb6b4SSrinivas Pandruvada /* Constraint package structure */
731726fb6b4SSrinivas Pandruvada struct lpi_device_constraint {
732726fb6b4SSrinivas Pandruvada 	int uid;
733726fb6b4SSrinivas Pandruvada 	int min_dstate;
734726fb6b4SSrinivas Pandruvada 	int function_states;
735726fb6b4SSrinivas Pandruvada };
736726fb6b4SSrinivas Pandruvada 
737726fb6b4SSrinivas Pandruvada struct lpi_constraints {
738726fb6b4SSrinivas Pandruvada 	acpi_handle handle;
739726fb6b4SSrinivas Pandruvada 	int min_dstate;
740726fb6b4SSrinivas Pandruvada };
741726fb6b4SSrinivas Pandruvada 
742726fb6b4SSrinivas Pandruvada static struct lpi_constraints *lpi_constraints_table;
743726fb6b4SSrinivas Pandruvada static int lpi_constraints_table_size;
744726fb6b4SSrinivas Pandruvada 
745726fb6b4SSrinivas Pandruvada static void lpi_device_get_constraints(void)
746726fb6b4SSrinivas Pandruvada {
747726fb6b4SSrinivas Pandruvada 	union acpi_object *out_obj;
748726fb6b4SSrinivas Pandruvada 	int i;
749726fb6b4SSrinivas Pandruvada 
750726fb6b4SSrinivas Pandruvada 	out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
751726fb6b4SSrinivas Pandruvada 					  1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
752726fb6b4SSrinivas Pandruvada 					  NULL, ACPI_TYPE_PACKAGE);
753726fb6b4SSrinivas Pandruvada 
754726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
755726fb6b4SSrinivas Pandruvada 			  out_obj ? "successful" : "failed");
756726fb6b4SSrinivas Pandruvada 
757726fb6b4SSrinivas Pandruvada 	if (!out_obj)
758726fb6b4SSrinivas Pandruvada 		return;
759726fb6b4SSrinivas Pandruvada 
760726fb6b4SSrinivas Pandruvada 	lpi_constraints_table = kcalloc(out_obj->package.count,
761726fb6b4SSrinivas Pandruvada 					sizeof(*lpi_constraints_table),
762726fb6b4SSrinivas Pandruvada 					GFP_KERNEL);
763726fb6b4SSrinivas Pandruvada 	if (!lpi_constraints_table)
764726fb6b4SSrinivas Pandruvada 		goto free_acpi_buffer;
765726fb6b4SSrinivas Pandruvada 
766726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
767726fb6b4SSrinivas Pandruvada 
768726fb6b4SSrinivas Pandruvada 	for (i = 0; i < out_obj->package.count; i++) {
769726fb6b4SSrinivas Pandruvada 		struct lpi_constraints *constraint;
770726fb6b4SSrinivas Pandruvada 		acpi_status status;
771726fb6b4SSrinivas Pandruvada 		union acpi_object *package = &out_obj->package.elements[i];
772726fb6b4SSrinivas Pandruvada 		struct lpi_device_info info = { };
773726fb6b4SSrinivas Pandruvada 		int package_count = 0, j;
774726fb6b4SSrinivas Pandruvada 
775726fb6b4SSrinivas Pandruvada 		if (!package)
776726fb6b4SSrinivas Pandruvada 			continue;
777726fb6b4SSrinivas Pandruvada 
778726fb6b4SSrinivas Pandruvada 		for (j = 0; j < package->package.count; ++j) {
779726fb6b4SSrinivas Pandruvada 			union acpi_object *element =
780726fb6b4SSrinivas Pandruvada 					&(package->package.elements[j]);
781726fb6b4SSrinivas Pandruvada 
782726fb6b4SSrinivas Pandruvada 			switch (element->type) {
783726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_INTEGER:
784726fb6b4SSrinivas Pandruvada 				info.enabled = element->integer.value;
785726fb6b4SSrinivas Pandruvada 				break;
786726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_STRING:
787726fb6b4SSrinivas Pandruvada 				info.name = element->string.pointer;
788726fb6b4SSrinivas Pandruvada 				break;
789726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_PACKAGE:
790726fb6b4SSrinivas Pandruvada 				package_count = element->package.count;
791726fb6b4SSrinivas Pandruvada 				info.package = element->package.elements;
792726fb6b4SSrinivas Pandruvada 				break;
793726fb6b4SSrinivas Pandruvada 			}
794726fb6b4SSrinivas Pandruvada 		}
795726fb6b4SSrinivas Pandruvada 
796726fb6b4SSrinivas Pandruvada 		if (!info.enabled || !info.package || !info.name)
797726fb6b4SSrinivas Pandruvada 			continue;
798726fb6b4SSrinivas Pandruvada 
799726fb6b4SSrinivas Pandruvada 		constraint = &lpi_constraints_table[lpi_constraints_table_size];
800726fb6b4SSrinivas Pandruvada 
801726fb6b4SSrinivas Pandruvada 		status = acpi_get_handle(NULL, info.name, &constraint->handle);
802726fb6b4SSrinivas Pandruvada 		if (ACPI_FAILURE(status))
803726fb6b4SSrinivas Pandruvada 			continue;
804726fb6b4SSrinivas Pandruvada 
805726fb6b4SSrinivas Pandruvada 		acpi_handle_debug(lps0_device_handle,
806726fb6b4SSrinivas Pandruvada 				  "index:%d Name:%s\n", i, info.name);
807726fb6b4SSrinivas Pandruvada 
808726fb6b4SSrinivas Pandruvada 		constraint->min_dstate = -1;
809726fb6b4SSrinivas Pandruvada 
810726fb6b4SSrinivas Pandruvada 		for (j = 0; j < package_count; ++j) {
811726fb6b4SSrinivas Pandruvada 			union acpi_object *info_obj = &info.package[j];
812726fb6b4SSrinivas Pandruvada 			union acpi_object *cnstr_pkg;
813726fb6b4SSrinivas Pandruvada 			union acpi_object *obj;
814726fb6b4SSrinivas Pandruvada 			struct lpi_device_constraint dev_info;
815726fb6b4SSrinivas Pandruvada 
816726fb6b4SSrinivas Pandruvada 			switch (info_obj->type) {
817726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_INTEGER:
818726fb6b4SSrinivas Pandruvada 				/* version */
819726fb6b4SSrinivas Pandruvada 				break;
820726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_PACKAGE:
821726fb6b4SSrinivas Pandruvada 				if (info_obj->package.count < 2)
822726fb6b4SSrinivas Pandruvada 					break;
823726fb6b4SSrinivas Pandruvada 
824726fb6b4SSrinivas Pandruvada 				cnstr_pkg = info_obj->package.elements;
825726fb6b4SSrinivas Pandruvada 				obj = &cnstr_pkg[0];
826726fb6b4SSrinivas Pandruvada 				dev_info.uid = obj->integer.value;
827726fb6b4SSrinivas Pandruvada 				obj = &cnstr_pkg[1];
828726fb6b4SSrinivas Pandruvada 				dev_info.min_dstate = obj->integer.value;
829726fb6b4SSrinivas Pandruvada 
830726fb6b4SSrinivas Pandruvada 				acpi_handle_debug(lps0_device_handle,
831726fb6b4SSrinivas Pandruvada 					"uid:%d min_dstate:%s\n",
832726fb6b4SSrinivas Pandruvada 					dev_info.uid,
833726fb6b4SSrinivas Pandruvada 					acpi_power_state_string(dev_info.min_dstate));
834726fb6b4SSrinivas Pandruvada 
835726fb6b4SSrinivas Pandruvada 				constraint->min_dstate = dev_info.min_dstate;
836726fb6b4SSrinivas Pandruvada 				break;
837726fb6b4SSrinivas Pandruvada 			}
838726fb6b4SSrinivas Pandruvada 		}
839726fb6b4SSrinivas Pandruvada 
840726fb6b4SSrinivas Pandruvada 		if (constraint->min_dstate < 0) {
841726fb6b4SSrinivas Pandruvada 			acpi_handle_debug(lps0_device_handle,
842726fb6b4SSrinivas Pandruvada 					  "Incomplete constraint defined\n");
843726fb6b4SSrinivas Pandruvada 			continue;
844726fb6b4SSrinivas Pandruvada 		}
845726fb6b4SSrinivas Pandruvada 
846726fb6b4SSrinivas Pandruvada 		lpi_constraints_table_size++;
847726fb6b4SSrinivas Pandruvada 	}
848726fb6b4SSrinivas Pandruvada 
849726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
850726fb6b4SSrinivas Pandruvada 
851726fb6b4SSrinivas Pandruvada free_acpi_buffer:
852726fb6b4SSrinivas Pandruvada 	ACPI_FREE(out_obj);
853726fb6b4SSrinivas Pandruvada }
854726fb6b4SSrinivas Pandruvada 
855726fb6b4SSrinivas Pandruvada static void lpi_check_constraints(void)
856726fb6b4SSrinivas Pandruvada {
857726fb6b4SSrinivas Pandruvada 	int i;
858726fb6b4SSrinivas Pandruvada 
859726fb6b4SSrinivas Pandruvada 	for (i = 0; i < lpi_constraints_table_size; ++i) {
860fa54150aSRafael J. Wysocki 		acpi_handle handle = lpi_constraints_table[i].handle;
861726fb6b4SSrinivas Pandruvada 		struct acpi_device *adev;
862726fb6b4SSrinivas Pandruvada 
863fa54150aSRafael J. Wysocki 		if (!handle || acpi_bus_get_device(handle, &adev))
864726fb6b4SSrinivas Pandruvada 			continue;
865726fb6b4SSrinivas Pandruvada 
866fa54150aSRafael J. Wysocki 		acpi_handle_debug(handle,
867726fb6b4SSrinivas Pandruvada 			"LPI: required min power state:%s current power state:%s\n",
868726fb6b4SSrinivas Pandruvada 			acpi_power_state_string(lpi_constraints_table[i].min_dstate),
869726fb6b4SSrinivas Pandruvada 			acpi_power_state_string(adev->power.state));
870726fb6b4SSrinivas Pandruvada 
871726fb6b4SSrinivas Pandruvada 		if (!adev->flags.power_manageable) {
872fa54150aSRafael J. Wysocki 			acpi_handle_info(handle, "LPI: Device not power manageable\n");
873fa54150aSRafael J. Wysocki 			lpi_constraints_table[i].handle = NULL;
874726fb6b4SSrinivas Pandruvada 			continue;
875726fb6b4SSrinivas Pandruvada 		}
876726fb6b4SSrinivas Pandruvada 
877726fb6b4SSrinivas Pandruvada 		if (adev->power.state < lpi_constraints_table[i].min_dstate)
878fa54150aSRafael J. Wysocki 			acpi_handle_info(handle,
879726fb6b4SSrinivas Pandruvada 				"LPI: Constraint not met; min power state:%s current power state:%s\n",
880726fb6b4SSrinivas Pandruvada 				acpi_power_state_string(lpi_constraints_table[i].min_dstate),
881726fb6b4SSrinivas Pandruvada 				acpi_power_state_string(adev->power.state));
882726fb6b4SSrinivas Pandruvada 	}
883726fb6b4SSrinivas Pandruvada }
884726fb6b4SSrinivas Pandruvada 
8858110dd28SRafael J. Wysocki static void acpi_sleep_run_lps0_dsm(unsigned int func)
8868110dd28SRafael J. Wysocki {
8878110dd28SRafael J. Wysocki 	union acpi_object *out_obj;
8888110dd28SRafael J. Wysocki 
8898110dd28SRafael J. Wysocki 	if (!(lps0_dsm_func_mask & (1 << func)))
8908110dd28SRafael J. Wysocki 		return;
8918110dd28SRafael J. Wysocki 
8928110dd28SRafael J. Wysocki 	out_obj = acpi_evaluate_dsm(lps0_device_handle, &lps0_dsm_guid, 1, func, NULL);
8938110dd28SRafael J. Wysocki 	ACPI_FREE(out_obj);
8948110dd28SRafael J. Wysocki 
8958110dd28SRafael J. Wysocki 	acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
8968110dd28SRafael J. Wysocki 			  func, out_obj ? "successful" : "failed");
8978110dd28SRafael J. Wysocki }
8988110dd28SRafael J. Wysocki 
8998110dd28SRafael J. Wysocki static int lps0_device_attach(struct acpi_device *adev,
9008110dd28SRafael J. Wysocki 			      const struct acpi_device_id *not_used)
9018110dd28SRafael J. Wysocki {
9028110dd28SRafael J. Wysocki 	union acpi_object *out_obj;
9038110dd28SRafael J. Wysocki 
9048110dd28SRafael J. Wysocki 	if (lps0_device_handle)
9058110dd28SRafael J. Wysocki 		return 0;
9068110dd28SRafael J. Wysocki 
90771630b7aSRafael J. Wysocki 	if (acpi_sleep_no_lps0) {
90871630b7aSRafael J. Wysocki 		acpi_handle_info(adev->handle,
90971630b7aSRafael J. Wysocki 				 "Low Power S0 Idle interface disabled\n");
91071630b7aSRafael J. Wysocki 		return 0;
91171630b7aSRafael J. Wysocki 	}
91271630b7aSRafael J. Wysocki 
9138110dd28SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
9148110dd28SRafael J. Wysocki 		return 0;
9158110dd28SRafael J. Wysocki 
9168110dd28SRafael J. Wysocki 	guid_parse(ACPI_LPS0_DSM_UUID, &lps0_dsm_guid);
9178110dd28SRafael J. Wysocki 	/* Check if the _DSM is present and as expected. */
9188110dd28SRafael J. Wysocki 	out_obj = acpi_evaluate_dsm(adev->handle, &lps0_dsm_guid, 1, 0, NULL);
919*2e2c2fdcSRafael J. Wysocki 	if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER) {
920*2e2c2fdcSRafael J. Wysocki 		acpi_handle_debug(adev->handle,
921*2e2c2fdcSRafael J. Wysocki 				  "_DSM function 0 evaluation failed\n");
922*2e2c2fdcSRafael J. Wysocki 		return 0;
923*2e2c2fdcSRafael J. Wysocki 	}
9248110dd28SRafael J. Wysocki 
925*2e2c2fdcSRafael J. Wysocki 	lps0_dsm_func_mask = *(char *)out_obj->buffer.pointer;
926*2e2c2fdcSRafael J. Wysocki 
927*2e2c2fdcSRafael J. Wysocki 	ACPI_FREE(out_obj);
928*2e2c2fdcSRafael J. Wysocki 
929*2e2c2fdcSRafael J. Wysocki 	acpi_handle_debug(adev->handle, "_DSM function mask: 0x%x\n",
930*2e2c2fdcSRafael J. Wysocki 			  lps0_dsm_func_mask);
931*2e2c2fdcSRafael J. Wysocki 
9328110dd28SRafael J. Wysocki 	lps0_device_handle = adev->handle;
933*2e2c2fdcSRafael J. Wysocki 
934*2e2c2fdcSRafael J. Wysocki 	lpi_device_get_constraints();
935*2e2c2fdcSRafael J. Wysocki 
936e870c6c8SRafael J. Wysocki 	/*
937*2e2c2fdcSRafael J. Wysocki 	 * Use suspend-to-idle by default if the default suspend mode was not
938*2e2c2fdcSRafael J. Wysocki 	 * set from the command line.
939e870c6c8SRafael J. Wysocki 	 */
940e870c6c8SRafael J. Wysocki 	if (mem_sleep_default > PM_SUSPEND_MEM)
941690cbb90SRafael J. Wysocki 		mem_sleep_current = PM_SUSPEND_TO_IDLE;
9428110dd28SRafael J. Wysocki 
9438110dd28SRafael J. Wysocki 	return 0;
9448110dd28SRafael J. Wysocki }
9458110dd28SRafael J. Wysocki 
9468110dd28SRafael J. Wysocki static struct acpi_scan_handler lps0_handler = {
9478110dd28SRafael J. Wysocki 	.ids = lps0_device_ids,
9488110dd28SRafael J. Wysocki 	.attach = lps0_device_attach,
9498110dd28SRafael J. Wysocki };
9508110dd28SRafael J. Wysocki 
95123d5855fSRafael J. Wysocki static int acpi_s2idle_begin(void)
9521f0b6386SRafael J. Wysocki {
9531f0b6386SRafael J. Wysocki 	acpi_scan_lock_acquire();
9548110dd28SRafael J. Wysocki 	s2idle_in_progress = true;
9551f0b6386SRafael J. Wysocki 	return 0;
9561f0b6386SRafael J. Wysocki }
9571f0b6386SRafael J. Wysocki 
95823d5855fSRafael J. Wysocki static int acpi_s2idle_prepare(void)
959a8d46b9eSRafael J. Wysocki {
9608110dd28SRafael J. Wysocki 	if (lps0_device_handle) {
9618110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF);
9628110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY);
9638110dd28SRafael J. Wysocki 	}
964147a7d9dSRafael J. Wysocki 
9658c01275eSChen Yu 	if (acpi_sci_irq_valid())
9668c01275eSChen Yu 		enable_irq_wake(acpi_sci_irq);
967235d81a6SRafael J. Wysocki 
9682f844b61SRajat Jain 	acpi_enable_wakeup_devices(ACPI_STATE_S0);
9692f844b61SRajat Jain 
970f941d3e4SRafael J. Wysocki 	/* Change the configuration of GPEs to avoid spurious wakeup. */
971f941d3e4SRafael J. Wysocki 	acpi_enable_all_wakeup_gpes();
972f941d3e4SRafael J. Wysocki 	acpi_os_wait_events_complete();
97341275eb5SRafael J. Wysocki 
97441275eb5SRafael J. Wysocki 	s2idle_wakeup = true;
975a8d46b9eSRafael J. Wysocki 	return 0;
976a8d46b9eSRafael J. Wysocki }
977a8d46b9eSRafael J. Wysocki 
97823d5855fSRafael J. Wysocki static void acpi_s2idle_wake(void)
97933e4f80eSRafael J. Wysocki {
98010a08fd6SRafael J. Wysocki 	if (lps0_device_handle && pm_debug_messages_on)
981726fb6b4SSrinivas Pandruvada 		lpi_check_constraints();
982726fb6b4SSrinivas Pandruvada 
98333e4f80eSRafael J. Wysocki 	/*
98456b99184SRafael J. Wysocki 	 * If IRQD_WAKEUP_ARMED is set for the SCI at this point, the SCI has
98556b99184SRafael J. Wysocki 	 * not triggered while suspended, so bail out.
98633e4f80eSRafael J. Wysocki 	 */
98756b99184SRafael J. Wysocki 	if (!acpi_sci_irq_valid() ||
98856b99184SRafael J. Wysocki 	    irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq)))
98956b99184SRafael J. Wysocki 		return;
99056b99184SRafael J. Wysocki 
99156b99184SRafael J. Wysocki 	/*
99256b99184SRafael J. Wysocki 	 * If there are EC events to process, the wakeup may be a spurious one
99356b99184SRafael J. Wysocki 	 * coming from the EC.
99456b99184SRafael J. Wysocki 	 */
99556b99184SRafael J. Wysocki 	if (acpi_ec_dispatch_gpe()) {
99656b99184SRafael J. Wysocki 		/*
99756b99184SRafael J. Wysocki 		 * Cancel the wakeup and process all pending events in case
99856b99184SRafael J. Wysocki 		 * there are any wakeup ones in there.
99956b99184SRafael J. Wysocki 		 *
100056b99184SRafael J. Wysocki 		 * Note that if any non-EC GPEs are active at this point, the
100156b99184SRafael J. Wysocki 		 * SCI will retrigger after the rearming below, so no events
100256b99184SRafael J. Wysocki 		 * should be missed by canceling the wakeup here.
100356b99184SRafael J. Wysocki 		 */
100433e4f80eSRafael J. Wysocki 		pm_system_cancel_wakeup();
100568e22011SRafael J. Wysocki 		/*
100656b99184SRafael J. Wysocki 		 * The EC driver uses the system workqueue and an additional
100756b99184SRafael J. Wysocki 		 * special one, so those need to be flushed too.
100868e22011SRafael J. Wysocki 		 */
100956b99184SRafael J. Wysocki 		acpi_os_wait_events_complete(); /* synchronize EC GPE processing */
1010880a6627SRafael J. Wysocki 		acpi_ec_flush_work();
10116f1d7c45SChris Chiu 		acpi_os_wait_events_complete(); /* synchronize Notify handling */
101233e4f80eSRafael J. Wysocki 	}
101333e4f80eSRafael J. Wysocki 
101456b99184SRafael J. Wysocki 	rearm_wake_irq(acpi_sci_irq);
101556b99184SRafael J. Wysocki }
101656b99184SRafael J. Wysocki 
101723d5855fSRafael J. Wysocki static void acpi_s2idle_restore(void)
1018a8d46b9eSRafael J. Wysocki {
101941275eb5SRafael J. Wysocki 	s2idle_wakeup = false;
102041275eb5SRafael J. Wysocki 
1021f941d3e4SRafael J. Wysocki 	acpi_enable_all_runtime_gpes();
1022f941d3e4SRafael J. Wysocki 
10232f844b61SRajat Jain 	acpi_disable_wakeup_devices(ACPI_STATE_S0);
10242f844b61SRajat Jain 
10258c01275eSChen Yu 	if (acpi_sci_irq_valid())
10268c01275eSChen Yu 		disable_irq_wake(acpi_sci_irq);
1027235d81a6SRafael J. Wysocki 
10288110dd28SRafael J. Wysocki 	if (lps0_device_handle) {
10298110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT);
10308110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON);
1031a8d46b9eSRafael J. Wysocki 	}
10328110dd28SRafael J. Wysocki }
1033a8d46b9eSRafael J. Wysocki 
103423d5855fSRafael J. Wysocki static void acpi_s2idle_end(void)
10351f0b6386SRafael J. Wysocki {
10368110dd28SRafael J. Wysocki 	s2idle_in_progress = false;
10371f0b6386SRafael J. Wysocki 	acpi_scan_lock_release();
10381f0b6386SRafael J. Wysocki }
10391f0b6386SRafael J. Wysocki 
104023d5855fSRafael J. Wysocki static const struct platform_s2idle_ops acpi_s2idle_ops = {
104123d5855fSRafael J. Wysocki 	.begin = acpi_s2idle_begin,
104223d5855fSRafael J. Wysocki 	.prepare = acpi_s2idle_prepare,
104323d5855fSRafael J. Wysocki 	.wake = acpi_s2idle_wake,
104423d5855fSRafael J. Wysocki 	.restore = acpi_s2idle_restore,
104523d5855fSRafael J. Wysocki 	.end = acpi_s2idle_end,
10461f0b6386SRafael J. Wysocki };
10471f0b6386SRafael J. Wysocki 
104802040f0bSRafael J. Wysocki static void acpi_sleep_suspend_setup(void)
104902040f0bSRafael J. Wysocki {
105002040f0bSRafael J. Wysocki 	int i;
105102040f0bSRafael J. Wysocki 
1052a4e90bedSRafael J. Wysocki 	for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
1053a4e90bedSRafael J. Wysocki 		if (acpi_sleep_state_supported(i))
105402040f0bSRafael J. Wysocki 			sleep_states[i] = 1;
105502040f0bSRafael J. Wysocki 
105602040f0bSRafael J. Wysocki 	suspend_set_ops(old_suspend_ordering ?
105702040f0bSRafael J. Wysocki 		&acpi_suspend_ops_old : &acpi_suspend_ops);
10588110dd28SRafael J. Wysocki 
10598110dd28SRafael J. Wysocki 	acpi_scan_add_handler(&lps0_handler);
106023d5855fSRafael J. Wysocki 	s2idle_set_ops(&acpi_s2idle_ops);
106102040f0bSRafael J. Wysocki }
10621f0b6386SRafael J. Wysocki 
106302040f0bSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
10648110dd28SRafael J. Wysocki #define s2idle_in_progress	(false)
106533e4f80eSRafael J. Wysocki #define s2idle_wakeup		(false)
10668110dd28SRafael J. Wysocki #define lps0_device_handle	(NULL)
106702040f0bSRafael J. Wysocki static inline void acpi_sleep_suspend_setup(void) {}
106802040f0bSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
1069d08ca2caSLen Brown 
107033e4f80eSRafael J. Wysocki bool acpi_s2idle_wakeup(void)
107133e4f80eSRafael J. Wysocki {
107233e4f80eSRafael J. Wysocki 	return s2idle_wakeup;
107333e4f80eSRafael J. Wysocki }
107433e4f80eSRafael J. Wysocki 
10758110dd28SRafael J. Wysocki bool acpi_sleep_no_ec_events(void)
10768110dd28SRafael J. Wysocki {
107710a08fd6SRafael J. Wysocki 	return !s2idle_in_progress;
10788110dd28SRafael J. Wysocki }
10798110dd28SRafael J. Wysocki 
1080504997cfSSudeep Holla #ifdef CONFIG_PM_SLEEP
1081504997cfSSudeep Holla static u32 saved_bm_rld;
1082504997cfSSudeep Holla 
1083504997cfSSudeep Holla static int  acpi_save_bm_rld(void)
1084504997cfSSudeep Holla {
1085504997cfSSudeep Holla 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
1086504997cfSSudeep Holla 	return 0;
1087504997cfSSudeep Holla }
1088504997cfSSudeep Holla 
1089504997cfSSudeep Holla static void  acpi_restore_bm_rld(void)
1090504997cfSSudeep Holla {
1091504997cfSSudeep Holla 	u32 resumed_bm_rld = 0;
1092504997cfSSudeep Holla 
1093504997cfSSudeep Holla 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
1094504997cfSSudeep Holla 	if (resumed_bm_rld == saved_bm_rld)
1095504997cfSSudeep Holla 		return;
1096504997cfSSudeep Holla 
1097504997cfSSudeep Holla 	acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
1098504997cfSSudeep Holla }
1099504997cfSSudeep Holla 
1100504997cfSSudeep Holla static struct syscore_ops acpi_sleep_syscore_ops = {
1101504997cfSSudeep Holla 	.suspend = acpi_save_bm_rld,
1102504997cfSSudeep Holla 	.resume = acpi_restore_bm_rld,
1103504997cfSSudeep Holla };
1104504997cfSSudeep Holla 
1105a042e0c6SJean Delvare static void acpi_sleep_syscore_init(void)
1106504997cfSSudeep Holla {
1107504997cfSSudeep Holla 	register_syscore_ops(&acpi_sleep_syscore_ops);
1108504997cfSSudeep Holla }
1109504997cfSSudeep Holla #else
1110504997cfSSudeep Holla static inline void acpi_sleep_syscore_init(void) {}
1111504997cfSSudeep Holla #endif /* CONFIG_PM_SLEEP */
1112504997cfSSudeep Holla 
1113d08ca2caSLen Brown #ifdef CONFIG_HIBERNATION
1114d08ca2caSLen Brown static unsigned long s4_hardware_signature;
1115d08ca2caSLen Brown static struct acpi_table_facs *facs;
1116d08ca2caSLen Brown static bool nosigcheck;
1117d08ca2caSLen Brown 
1118d08ca2caSLen Brown void __init acpi_no_s4_hw_signature(void)
1119d08ca2caSLen Brown {
1120d08ca2caSLen Brown 	nosigcheck = true;
1121d08ca2caSLen Brown }
1122d08ca2caSLen Brown 
1123bb186901SRafael J. Wysocki static int acpi_hibernation_begin(pm_message_t stage)
1124d08ca2caSLen Brown {
1125bb186901SRafael J. Wysocki 	if (!nvs_nosave) {
1126bb186901SRafael J. Wysocki 		int error = suspend_nvs_alloc();
1127bb186901SRafael J. Wysocki 		if (error)
1128d08ca2caSLen Brown 			return error;
1129d08ca2caSLen Brown 	}
1130d08ca2caSLen Brown 
1131bb186901SRafael J. Wysocki 	if (stage.event == PM_EVENT_HIBERNATE)
1132bb186901SRafael J. Wysocki 		pm_set_suspend_via_firmware();
1133bb186901SRafael J. Wysocki 
1134bb186901SRafael J. Wysocki 	acpi_pm_start(ACPI_STATE_S4);
1135bb186901SRafael J. Wysocki 	return 0;
1136bb186901SRafael J. Wysocki }
1137bb186901SRafael J. Wysocki 
1138d08ca2caSLen Brown static int acpi_hibernation_enter(void)
1139d08ca2caSLen Brown {
1140d08ca2caSLen Brown 	acpi_status status = AE_OK;
1141d08ca2caSLen Brown 
1142d08ca2caSLen Brown 	ACPI_FLUSH_CPU_CACHE();
1143d08ca2caSLen Brown 
1144d08ca2caSLen Brown 	/* This shouldn't return.  If it returns, we have a problem */
11453f6f49c7SLen Brown 	status = acpi_enter_sleep_state(ACPI_STATE_S4);
11463f6f49c7SLen Brown 	/* Reprogram control registers */
11473f6f49c7SLen Brown 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1148d08ca2caSLen Brown 
1149d08ca2caSLen Brown 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
1150d08ca2caSLen Brown }
1151d08ca2caSLen Brown 
1152d08ca2caSLen Brown static void acpi_hibernation_leave(void)
1153d08ca2caSLen Brown {
1154fbda4b38SLukas Wunner 	pm_set_resume_via_firmware();
1155d08ca2caSLen Brown 	/*
1156d08ca2caSLen Brown 	 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
1157d08ca2caSLen Brown 	 * enable it here.
1158d08ca2caSLen Brown 	 */
1159d08ca2caSLen Brown 	acpi_enable();
11603f6f49c7SLen Brown 	/* Reprogram control registers */
11613f6f49c7SLen Brown 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1162d08ca2caSLen Brown 	/* Check the hardware signature */
11635c551e62SOliver Neukum 	if (facs && s4_hardware_signature != facs->hardware_signature)
11645c551e62SOliver Neukum 		pr_crit("ACPI: Hardware changed while hibernated, success doubtful!\n");
1165d08ca2caSLen Brown 	/* Restore the NVS memory area */
1166dd4c4f17SMatthew Garrett 	suspend_nvs_restore();
1167d5a64513SRafael J. Wysocki 	/* Allow EC transactions to happen. */
1168c2b46d67SLv Zheng 	acpi_ec_unblock_transactions();
1169d08ca2caSLen Brown }
1170d08ca2caSLen Brown 
1171d5a64513SRafael J. Wysocki static void acpi_pm_thaw(void)
1172f6bb13aaSRafael J. Wysocki {
1173fe955682SRafael J. Wysocki 	acpi_ec_unblock_transactions();
1174d08ca2caSLen Brown 	acpi_enable_all_runtime_gpes();
1175d08ca2caSLen Brown }
1176d08ca2caSLen Brown 
1177073ef1f6SLionel Debroux static const struct platform_hibernation_ops acpi_hibernation_ops = {
1178d08ca2caSLen Brown 	.begin = acpi_hibernation_begin,
1179d08ca2caSLen Brown 	.end = acpi_pm_end,
1180c5f7a1bbSRafael J. Wysocki 	.pre_snapshot = acpi_pm_prepare,
11812a6b6976SMatthew Garrett 	.finish = acpi_pm_finish,
1182d08ca2caSLen Brown 	.prepare = acpi_pm_prepare,
1183d08ca2caSLen Brown 	.enter = acpi_hibernation_enter,
1184d08ca2caSLen Brown 	.leave = acpi_hibernation_leave,
1185d5a64513SRafael J. Wysocki 	.pre_restore = acpi_pm_freeze,
1186d5a64513SRafael J. Wysocki 	.restore_cleanup = acpi_pm_thaw,
1187d08ca2caSLen Brown };
1188d08ca2caSLen Brown 
1189d08ca2caSLen Brown /**
1190d08ca2caSLen Brown  *	acpi_hibernation_begin_old - Set the target system sleep state to
1191d08ca2caSLen Brown  *		ACPI_STATE_S4 and execute the _PTS control method.  This
1192d08ca2caSLen Brown  *		function is used if the pre-ACPI 2.0 suspend ordering has been
1193d08ca2caSLen Brown  *		requested.
1194d08ca2caSLen Brown  */
1195bb186901SRafael J. Wysocki static int acpi_hibernation_begin_old(pm_message_t stage)
1196d08ca2caSLen Brown {
1197d08ca2caSLen Brown 	int error;
1198d08ca2caSLen Brown 	/*
1199d08ca2caSLen Brown 	 * The _TTS object should always be evaluated before the _PTS object.
1200d08ca2caSLen Brown 	 * When the old_suspended_ordering is true, the _PTS object is
1201d08ca2caSLen Brown 	 * evaluated in the acpi_sleep_prepare.
1202d08ca2caSLen Brown 	 */
1203d08ca2caSLen Brown 	acpi_sleep_tts_switch(ACPI_STATE_S4);
1204d08ca2caSLen Brown 
1205d08ca2caSLen Brown 	error = acpi_sleep_prepare(ACPI_STATE_S4);
1206bb186901SRafael J. Wysocki 	if (error)
1207bb186901SRafael J. Wysocki 		return error;
1208d08ca2caSLen Brown 
1209bb186901SRafael J. Wysocki 	if (!nvs_nosave) {
1210dd4c4f17SMatthew Garrett 		error = suspend_nvs_alloc();
1211bb186901SRafael J. Wysocki 		if (error)
1212bb186901SRafael J. Wysocki 			return error;
1213bb186901SRafael J. Wysocki 	}
1214bb186901SRafael J. Wysocki 
1215bb186901SRafael J. Wysocki 	if (stage.event == PM_EVENT_HIBERNATE)
1216bb186901SRafael J. Wysocki 		pm_set_suspend_via_firmware();
1217bb186901SRafael J. Wysocki 
1218d08ca2caSLen Brown 	acpi_target_sleep_state = ACPI_STATE_S4;
1219ad07277eSRafael J. Wysocki 	acpi_scan_lock_acquire();
1220bb186901SRafael J. Wysocki 	return 0;
1221d08ca2caSLen Brown }
1222d08ca2caSLen Brown 
1223d08ca2caSLen Brown /*
1224d08ca2caSLen Brown  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
1225d08ca2caSLen Brown  * been requested.
1226d08ca2caSLen Brown  */
1227073ef1f6SLionel Debroux static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
1228d08ca2caSLen Brown 	.begin = acpi_hibernation_begin_old,
1229d08ca2caSLen Brown 	.end = acpi_pm_end,
1230c5f7a1bbSRafael J. Wysocki 	.pre_snapshot = acpi_pm_pre_suspend,
1231d5a64513SRafael J. Wysocki 	.prepare = acpi_pm_freeze,
12322a6b6976SMatthew Garrett 	.finish = acpi_pm_finish,
1233d08ca2caSLen Brown 	.enter = acpi_hibernation_enter,
1234d08ca2caSLen Brown 	.leave = acpi_hibernation_leave,
1235d5a64513SRafael J. Wysocki 	.pre_restore = acpi_pm_freeze,
1236d5a64513SRafael J. Wysocki 	.restore_cleanup = acpi_pm_thaw,
1237d08ca2caSLen Brown 	.recover = acpi_pm_finish,
1238d08ca2caSLen Brown };
123902040f0bSRafael J. Wysocki 
124002040f0bSRafael J. Wysocki static void acpi_sleep_hibernate_setup(void)
124102040f0bSRafael J. Wysocki {
1242a4e90bedSRafael J. Wysocki 	if (!acpi_sleep_state_supported(ACPI_STATE_S4))
124302040f0bSRafael J. Wysocki 		return;
124402040f0bSRafael J. Wysocki 
124502040f0bSRafael J. Wysocki 	hibernation_set_ops(old_suspend_ordering ?
124602040f0bSRafael J. Wysocki 			&acpi_hibernation_ops_old : &acpi_hibernation_ops);
124702040f0bSRafael J. Wysocki 	sleep_states[ACPI_STATE_S4] = 1;
124802040f0bSRafael J. Wysocki 	if (nosigcheck)
124902040f0bSRafael J. Wysocki 		return;
125002040f0bSRafael J. Wysocki 
125102040f0bSRafael J. Wysocki 	acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
125202040f0bSRafael J. Wysocki 	if (facs)
125302040f0bSRafael J. Wysocki 		s4_hardware_signature = facs->hardware_signature;
125402040f0bSRafael J. Wysocki }
125502040f0bSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */
125602040f0bSRafael J. Wysocki static inline void acpi_sleep_hibernate_setup(void) {}
125702040f0bSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */
1258d08ca2caSLen Brown 
1259d08ca2caSLen Brown static void acpi_power_off_prepare(void)
1260d08ca2caSLen Brown {
1261d08ca2caSLen Brown 	/* Prepare to power off the system */
1262d08ca2caSLen Brown 	acpi_sleep_prepare(ACPI_STATE_S5);
1263d08ca2caSLen Brown 	acpi_disable_all_gpes();
1264c52fa70cSRafael J. Wysocki 	acpi_os_wait_events_complete();
1265d08ca2caSLen Brown }
1266d08ca2caSLen Brown 
1267d08ca2caSLen Brown static void acpi_power_off(void)
1268d08ca2caSLen Brown {
1269d08ca2caSLen Brown 	/* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
12704d939155SFrank Seidel 	printk(KERN_DEBUG "%s called\n", __func__);
1271d08ca2caSLen Brown 	local_irq_disable();
12723f6f49c7SLen Brown 	acpi_enter_sleep_state(ACPI_STATE_S5);
127396f15efcSLen Brown }
127496f15efcSLen Brown 
1275d08ca2caSLen Brown int __init acpi_sleep_init(void)
1276d08ca2caSLen Brown {
1277ed4cf5b2SJoe Perches 	char supported[ACPI_S_STATE_COUNT * 3 + 1];
1278ed4cf5b2SJoe Perches 	char *pos = supported;
1279ed4cf5b2SJoe Perches 	int i;
1280d08ca2caSLen Brown 
12810ac1b1d7SZhang Rui 	acpi_sleep_dmi_check();
12820ac1b1d7SZhang Rui 
1283d08ca2caSLen Brown 	sleep_states[ACPI_STATE_S0] = 1;
1284d08ca2caSLen Brown 
1285504997cfSSudeep Holla 	acpi_sleep_syscore_init();
128602040f0bSRafael J. Wysocki 	acpi_sleep_suspend_setup();
128702040f0bSRafael J. Wysocki 	acpi_sleep_hibernate_setup();
1288d08ca2caSLen Brown 
1289a4e90bedSRafael J. Wysocki 	if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
1290d08ca2caSLen Brown 		sleep_states[ACPI_STATE_S5] = 1;
1291d08ca2caSLen Brown 		pm_power_off_prepare = acpi_power_off_prepare;
1292d08ca2caSLen Brown 		pm_power_off = acpi_power_off;
129313737181SChen Yu 	} else {
129413737181SChen Yu 		acpi_no_s5 = true;
1295d08ca2caSLen Brown 	}
1296ed4cf5b2SJoe Perches 
1297ed4cf5b2SJoe Perches 	supported[0] = 0;
1298ed4cf5b2SJoe Perches 	for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
1299ed4cf5b2SJoe Perches 		if (sleep_states[i])
1300ed4cf5b2SJoe Perches 			pos += sprintf(pos, " S%d", i);
1301ed4cf5b2SJoe Perches 	}
1302ed4cf5b2SJoe Perches 	pr_info(PREFIX "(supports%s)\n", supported);
1303ed4cf5b2SJoe Perches 
1304d08ca2caSLen Brown 	/*
13059713adc2SRafael J. Wysocki 	 * Register the tts_notifier to reboot notifier list so that the _TTS
13069713adc2SRafael J. Wysocki 	 * object can also be evaluated when the system enters S5.
1307d08ca2caSLen Brown 	 */
13089713adc2SRafael J. Wysocki 	register_reboot_notifier(&tts_notifier);
1309d08ca2caSLen Brown 	return 0;
1310d08ca2caSLen Brown }
1311