xref: /linux/drivers/acpi/sleep.c (revision 68e22011856f036bd9b0328b9b62d953e668a7ae)
1d08ca2caSLen Brown /*
2d08ca2caSLen Brown  * sleep.c - ACPI sleep support.
3d08ca2caSLen Brown  *
4d08ca2caSLen Brown  * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5d08ca2caSLen Brown  * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
6d08ca2caSLen Brown  * Copyright (c) 2000-2003 Patrick Mochel
7d08ca2caSLen Brown  * Copyright (c) 2003 Open Source Development Lab
8d08ca2caSLen Brown  *
9d08ca2caSLen Brown  * This file is released under the GPLv2.
10d08ca2caSLen Brown  *
11d08ca2caSLen Brown  */
12d08ca2caSLen Brown 
13d08ca2caSLen Brown #include <linux/delay.h>
14d08ca2caSLen Brown #include <linux/irq.h>
15d08ca2caSLen Brown #include <linux/dmi.h>
16d08ca2caSLen Brown #include <linux/device.h>
17a8d46b9eSRafael J. Wysocki #include <linux/interrupt.h>
18d08ca2caSLen Brown #include <linux/suspend.h>
19d08ca2caSLen Brown #include <linux/reboot.h>
20d1ee4335SH. Peter Anvin #include <linux/acpi.h>
21a2ef5c4fSLin Ming #include <linux/module.h>
22504997cfSSudeep Holla #include <linux/syscore_ops.h>
23d08ca2caSLen Brown #include <asm/io.h>
24bb3632c6STodd E Brandt #include <trace/events/power.h>
25d08ca2caSLen Brown 
26e60cc7a6SBjorn Helgaas #include "internal.h"
27d08ca2caSLen Brown #include "sleep.h"
28d08ca2caSLen Brown 
2913737181SChen Yu /*
3013737181SChen Yu  * Some HW-full platforms do not have _S5, so they may need
3113737181SChen Yu  * to leverage efi power off for a shutdown.
3213737181SChen Yu  */
3313737181SChen Yu bool acpi_no_s5;
3401eac60bSStephen Hemminger static u8 sleep_states[ACPI_S_STATE_COUNT];
35d08ca2caSLen Brown 
36d08ca2caSLen Brown static void acpi_sleep_tts_switch(u32 acpi_state)
37d08ca2caSLen Brown {
380db98202SJiang Liu 	acpi_status status;
39d08ca2caSLen Brown 
400db98202SJiang Liu 	status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
41d08ca2caSLen Brown 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
42d08ca2caSLen Brown 		/*
43d08ca2caSLen Brown 		 * OS can't evaluate the _TTS object correctly. Some warning
44d08ca2caSLen Brown 		 * message will be printed. But it won't break anything.
45d08ca2caSLen Brown 		 */
46d08ca2caSLen Brown 		printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
47d08ca2caSLen Brown 	}
48d08ca2caSLen Brown }
49d08ca2caSLen Brown 
509713adc2SRafael J. Wysocki static int tts_notify_reboot(struct notifier_block *this,
51d08ca2caSLen Brown 			unsigned long code, void *x)
52d08ca2caSLen Brown {
53d08ca2caSLen Brown 	acpi_sleep_tts_switch(ACPI_STATE_S5);
54d08ca2caSLen Brown 	return NOTIFY_DONE;
55d08ca2caSLen Brown }
56d08ca2caSLen Brown 
579713adc2SRafael J. Wysocki static struct notifier_block tts_notifier = {
589713adc2SRafael J. Wysocki 	.notifier_call	= tts_notify_reboot,
59d08ca2caSLen Brown 	.next		= NULL,
60d08ca2caSLen Brown 	.priority	= 0,
61d08ca2caSLen Brown };
62d08ca2caSLen Brown 
63d08ca2caSLen Brown static int acpi_sleep_prepare(u32 acpi_state)
64d08ca2caSLen Brown {
65d08ca2caSLen Brown #ifdef CONFIG_ACPI_SLEEP
66d08ca2caSLen Brown 	/* do we have a wakeup address for S2 and S3? */
67d08ca2caSLen Brown 	if (acpi_state == ACPI_STATE_S3) {
68319b6ffcSH. Peter Anvin 		if (!acpi_wakeup_address)
69d08ca2caSLen Brown 			return -EFAULT;
70e3e9b577SRafael J. Wysocki 		acpi_set_waking_vector(acpi_wakeup_address);
71d08ca2caSLen Brown 
72d08ca2caSLen Brown 	}
73d08ca2caSLen Brown 	ACPI_FLUSH_CPU_CACHE();
74d08ca2caSLen Brown #endif
75d08ca2caSLen Brown 	printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
76d08ca2caSLen Brown 		acpi_state);
7778f5f023SRafael J. Wysocki 	acpi_enable_wakeup_devices(acpi_state);
78d08ca2caSLen Brown 	acpi_enter_sleep_state_prep(acpi_state);
79d08ca2caSLen Brown 	return 0;
80d08ca2caSLen Brown }
81d08ca2caSLen Brown 
82a4e90bedSRafael J. Wysocki static bool acpi_sleep_state_supported(u8 sleep_state)
83a4e90bedSRafael J. Wysocki {
84a4e90bedSRafael J. Wysocki 	acpi_status status;
85a4e90bedSRafael J. Wysocki 	u8 type_a, type_b;
86a4e90bedSRafael J. Wysocki 
87a4e90bedSRafael J. Wysocki 	status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
88a4e90bedSRafael J. Wysocki 	return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
89a4e90bedSRafael J. Wysocki 		|| (acpi_gbl_FADT.sleep_control.address
90a4e90bedSRafael J. Wysocki 			&& acpi_gbl_FADT.sleep_status.address));
91a4e90bedSRafael J. Wysocki }
92a4e90bedSRafael J. Wysocki 
93d08ca2caSLen Brown #ifdef CONFIG_ACPI_SLEEP
94091aad6aSJan Beulich static u32 acpi_target_sleep_state = ACPI_STATE_S0;
95a6ae7594SRafael J. Wysocki 
96a6ae7594SRafael J. Wysocki u32 acpi_target_system_state(void)
97a6ae7594SRafael J. Wysocki {
98a6ae7594SRafael J. Wysocki 	return acpi_target_sleep_state;
99a6ae7594SRafael J. Wysocki }
100fad16dd9SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_target_system_state);
101a6ae7594SRafael J. Wysocki 
102a5ca7345SRafael J. Wysocki static bool pwr_btn_event_pending;
103091aad6aSJan Beulich 
104d7f0eea9SZhang Rui /*
10572ad5d77SRafael J. Wysocki  * The ACPI specification wants us to save NVS memory regions during hibernation
10672ad5d77SRafael J. Wysocki  * and to restore them during the subsequent resume.  Windows does that also for
10772ad5d77SRafael J. Wysocki  * suspend to RAM.  However, it is known that this mechanism does not work on
10872ad5d77SRafael J. Wysocki  * all machines, so we allow the user to disable it with the help of the
10972ad5d77SRafael J. Wysocki  * 'acpi_sleep=nonvs' kernel command line option.
11072ad5d77SRafael J. Wysocki  */
11172ad5d77SRafael J. Wysocki static bool nvs_nosave;
11272ad5d77SRafael J. Wysocki 
11372ad5d77SRafael J. Wysocki void __init acpi_nvs_nosave(void)
11472ad5d77SRafael J. Wysocki {
11572ad5d77SRafael J. Wysocki 	nvs_nosave = true;
11672ad5d77SRafael J. Wysocki }
11772ad5d77SRafael J. Wysocki 
11872ad5d77SRafael J. Wysocki /*
1191bad2f19SKristen Carlson Accardi  * The ACPI specification wants us to save NVS memory regions during hibernation
1201bad2f19SKristen Carlson Accardi  * but says nothing about saving NVS during S3.  Not all versions of Windows
1211bad2f19SKristen Carlson Accardi  * save NVS on S3 suspend either, and it is clear that not all systems need
1221bad2f19SKristen Carlson Accardi  * NVS to be saved at S3 time.  To improve suspend/resume time, allow the
1231bad2f19SKristen Carlson Accardi  * user to disable saving NVS on S3 if their system does not require it, but
1241bad2f19SKristen Carlson Accardi  * continue to save/restore NVS for S4 as specified.
1251bad2f19SKristen Carlson Accardi  */
1261bad2f19SKristen Carlson Accardi static bool nvs_nosave_s3;
1271bad2f19SKristen Carlson Accardi 
1281bad2f19SKristen Carlson Accardi void __init acpi_nvs_nosave_s3(void)
1291bad2f19SKristen Carlson Accardi {
1301bad2f19SKristen Carlson Accardi 	nvs_nosave_s3 = true;
1311bad2f19SKristen Carlson Accardi }
1321bad2f19SKristen Carlson Accardi 
133cbc00c13SZhang Rui static int __init init_nvs_save_s3(const struct dmi_system_id *d)
134cbc00c13SZhang Rui {
135cbc00c13SZhang Rui 	nvs_nosave_s3 = false;
136cbc00c13SZhang Rui 	return 0;
137cbc00c13SZhang Rui }
138cbc00c13SZhang Rui 
1391bad2f19SKristen Carlson Accardi /*
140d08ca2caSLen Brown  * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
141d08ca2caSLen Brown  * user to request that behavior by using the 'acpi_old_suspend_ordering'
142d08ca2caSLen Brown  * kernel command line option that causes the following variable to be set.
143d08ca2caSLen Brown  */
144d08ca2caSLen Brown static bool old_suspend_ordering;
145d08ca2caSLen Brown 
146d08ca2caSLen Brown void __init acpi_old_suspend_ordering(void)
147d08ca2caSLen Brown {
148d08ca2caSLen Brown 	old_suspend_ordering = true;
149d08ca2caSLen Brown }
150d08ca2caSLen Brown 
151d08ca2caSLen Brown static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
152d08ca2caSLen Brown {
1530ac1b1d7SZhang Rui 	acpi_old_suspend_ordering();
154d08ca2caSLen Brown 	return 0;
155d08ca2caSLen Brown }
156d08ca2caSLen Brown 
15753998648SRafael J. Wysocki static int __init init_nvs_nosave(const struct dmi_system_id *d)
15853998648SRafael J. Wysocki {
15953998648SRafael J. Wysocki 	acpi_nvs_nosave();
16053998648SRafael J. Wysocki 	return 0;
16153998648SRafael J. Wysocki }
16253998648SRafael J. Wysocki 
16371630b7aSRafael J. Wysocki static bool acpi_sleep_no_lps0;
16471630b7aSRafael J. Wysocki 
16571630b7aSRafael J. Wysocki static int __init init_no_lps0(const struct dmi_system_id *d)
16671630b7aSRafael J. Wysocki {
16771630b7aSRafael J. Wysocki 	acpi_sleep_no_lps0 = true;
16871630b7aSRafael J. Wysocki 	return 0;
16971630b7aSRafael J. Wysocki }
17071630b7aSRafael J. Wysocki 
1716faadbbbSChristoph Hellwig static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
172d08ca2caSLen Brown 	{
173d08ca2caSLen Brown 	.callback = init_old_suspend_ordering,
174d08ca2caSLen Brown 	.ident = "Abit KN9 (nForce4 variant)",
175d08ca2caSLen Brown 	.matches = {
176d08ca2caSLen Brown 		DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
177d08ca2caSLen Brown 		DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
178d08ca2caSLen Brown 		},
179d08ca2caSLen Brown 	},
180d08ca2caSLen Brown 	{
181d08ca2caSLen Brown 	.callback = init_old_suspend_ordering,
182d08ca2caSLen Brown 	.ident = "HP xw4600 Workstation",
183d08ca2caSLen Brown 	.matches = {
184d08ca2caSLen Brown 		DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
185d08ca2caSLen Brown 		DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
186d08ca2caSLen Brown 		},
187d08ca2caSLen Brown 	},
188d08ca2caSLen Brown 	{
189a1404495SAndy Whitcroft 	.callback = init_old_suspend_ordering,
190a1404495SAndy Whitcroft 	.ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
191a1404495SAndy Whitcroft 	.matches = {
192a1404495SAndy Whitcroft 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
193a1404495SAndy Whitcroft 		DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
194a1404495SAndy Whitcroft 		},
195a1404495SAndy Whitcroft 	},
19645e77988SZhang Rui 	{
1972a9ef8e1SZhao Yakui 	.callback = init_old_suspend_ordering,
1982a9ef8e1SZhao Yakui 	.ident = "Panasonic CF51-2L",
1992a9ef8e1SZhao Yakui 	.matches = {
2002a9ef8e1SZhao Yakui 		DMI_MATCH(DMI_BOARD_VENDOR,
2012a9ef8e1SZhao Yakui 				"Matsushita Electric Industrial Co.,Ltd."),
2022a9ef8e1SZhao Yakui 		DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
2032a9ef8e1SZhao Yakui 		},
2042a9ef8e1SZhao Yakui 	},
20553998648SRafael J. Wysocki 	{
20653998648SRafael J. Wysocki 	.callback = init_nvs_nosave,
20766f2fda9SJoseph Salisbury 	.ident = "Sony Vaio VGN-FW41E_H",
20866f2fda9SJoseph Salisbury 	.matches = {
20966f2fda9SJoseph Salisbury 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
21066f2fda9SJoseph Salisbury 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
21166f2fda9SJoseph Salisbury 		},
21266f2fda9SJoseph Salisbury 	},
21366f2fda9SJoseph Salisbury 	{
21466f2fda9SJoseph Salisbury 	.callback = init_nvs_nosave,
215d11c78e9SDave Jones 	.ident = "Sony Vaio VGN-FW21E",
216d11c78e9SDave Jones 	.matches = {
217d11c78e9SDave Jones 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
218d11c78e9SDave Jones 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
219d11c78e9SDave Jones 		},
220d11c78e9SDave Jones 	},
221d11c78e9SDave Jones 	{
222d11c78e9SDave Jones 	.callback = init_nvs_nosave,
223469dd1c4SFabio Valentini 	.ident = "Sony Vaio VGN-FW21M",
224469dd1c4SFabio Valentini 	.matches = {
225469dd1c4SFabio Valentini 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
226469dd1c4SFabio Valentini 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
227469dd1c4SFabio Valentini 		},
228469dd1c4SFabio Valentini 	},
229469dd1c4SFabio Valentini 	{
230469dd1c4SFabio Valentini 	.callback = init_nvs_nosave,
231ddf6ce45SDave Jones 	.ident = "Sony Vaio VPCEB17FX",
232ddf6ce45SDave Jones 	.matches = {
233ddf6ce45SDave Jones 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
234ddf6ce45SDave Jones 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
235ddf6ce45SDave Jones 		},
236ddf6ce45SDave Jones 	},
237ddf6ce45SDave Jones 	{
238ddf6ce45SDave Jones 	.callback = init_nvs_nosave,
23953998648SRafael J. Wysocki 	.ident = "Sony Vaio VGN-SR11M",
24053998648SRafael J. Wysocki 	.matches = {
24153998648SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
24253998648SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
24353998648SRafael J. Wysocki 		},
24453998648SRafael J. Wysocki 	},
24553998648SRafael J. Wysocki 	{
24653998648SRafael J. Wysocki 	.callback = init_nvs_nosave,
24753998648SRafael J. Wysocki 	.ident = "Everex StepNote Series",
24853998648SRafael J. Wysocki 	.matches = {
24953998648SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
25053998648SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
25153998648SRafael J. Wysocki 		},
25253998648SRafael J. Wysocki 	},
253af48931cSRafael J. Wysocki 	{
254af48931cSRafael J. Wysocki 	.callback = init_nvs_nosave,
255af48931cSRafael J. Wysocki 	.ident = "Sony Vaio VPCEB1Z1E",
256af48931cSRafael J. Wysocki 	.matches = {
257af48931cSRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
258af48931cSRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
259af48931cSRafael J. Wysocki 		},
260af48931cSRafael J. Wysocki 	},
261291a73c9SRafael J. Wysocki 	{
262291a73c9SRafael J. Wysocki 	.callback = init_nvs_nosave,
263291a73c9SRafael J. Wysocki 	.ident = "Sony Vaio VGN-NW130D",
264291a73c9SRafael J. Wysocki 	.matches = {
265291a73c9SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
266291a73c9SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
267291a73c9SRafael J. Wysocki 		},
268291a73c9SRafael J. Wysocki 	},
2697b330707SRafael J. Wysocki 	{
2707b330707SRafael J. Wysocki 	.callback = init_nvs_nosave,
27193f77084SLan Tianyu 	.ident = "Sony Vaio VPCCW29FX",
27293f77084SLan Tianyu 	.matches = {
27393f77084SLan Tianyu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
27493f77084SLan Tianyu 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
27593f77084SLan Tianyu 		},
27693f77084SLan Tianyu 	},
27793f77084SLan Tianyu 	{
27893f77084SLan Tianyu 	.callback = init_nvs_nosave,
2797b330707SRafael J. Wysocki 	.ident = "Averatec AV1020-ED2",
2807b330707SRafael J. Wysocki 	.matches = {
2817b330707SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
2827b330707SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
2837b330707SRafael J. Wysocki 		},
2847b330707SRafael J. Wysocki 	},
285bb0c5ed6SZhang Rui 	{
286bb0c5ed6SZhang Rui 	.callback = init_old_suspend_ordering,
287bb0c5ed6SZhang Rui 	.ident = "Asus A8N-SLI DELUXE",
288bb0c5ed6SZhang Rui 	.matches = {
289bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
290bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
291bb0c5ed6SZhang Rui 		},
292bb0c5ed6SZhang Rui 	},
293bb0c5ed6SZhang Rui 	{
294bb0c5ed6SZhang Rui 	.callback = init_old_suspend_ordering,
295bb0c5ed6SZhang Rui 	.ident = "Asus A8N-SLI Premium",
296bb0c5ed6SZhang Rui 	.matches = {
297bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
298bb0c5ed6SZhang Rui 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
299bb0c5ed6SZhang Rui 		},
300bb0c5ed6SZhang Rui 	},
30189e8ea12SRafael J. Wysocki 	{
30289e8ea12SRafael J. Wysocki 	.callback = init_nvs_nosave,
30389e8ea12SRafael J. Wysocki 	.ident = "Sony Vaio VGN-SR26GN_P",
30489e8ea12SRafael J. Wysocki 	.matches = {
30589e8ea12SRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
30689e8ea12SRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
30789e8ea12SRafael J. Wysocki 		},
30889e8ea12SRafael J. Wysocki 	},
309731b25a4SBogdan Radulescu 	{
310731b25a4SBogdan Radulescu 	.callback = init_nvs_nosave,
311876ab790SLan Tianyu 	.ident = "Sony Vaio VPCEB1S1E",
312876ab790SLan Tianyu 	.matches = {
313876ab790SLan Tianyu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
314876ab790SLan Tianyu 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
315876ab790SLan Tianyu 		},
316876ab790SLan Tianyu 	},
317876ab790SLan Tianyu 	{
318876ab790SLan Tianyu 	.callback = init_nvs_nosave,
319731b25a4SBogdan Radulescu 	.ident = "Sony Vaio VGN-FW520F",
320731b25a4SBogdan Radulescu 	.matches = {
321731b25a4SBogdan Radulescu 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
322731b25a4SBogdan Radulescu 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
323731b25a4SBogdan Radulescu 		},
324731b25a4SBogdan Radulescu 	},
3255a50a7c3SKeng-Yu Lin 	{
3265a50a7c3SKeng-Yu Lin 	.callback = init_nvs_nosave,
3275a50a7c3SKeng-Yu Lin 	.ident = "Asus K54C",
3285a50a7c3SKeng-Yu Lin 	.matches = {
3295a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
3305a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
3315a50a7c3SKeng-Yu Lin 		},
3325a50a7c3SKeng-Yu Lin 	},
3335a50a7c3SKeng-Yu Lin 	{
3345a50a7c3SKeng-Yu Lin 	.callback = init_nvs_nosave,
3355a50a7c3SKeng-Yu Lin 	.ident = "Asus K54HR",
3365a50a7c3SKeng-Yu Lin 	.matches = {
3375a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
3385a50a7c3SKeng-Yu Lin 		DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
3395a50a7c3SKeng-Yu Lin 		},
3405a50a7c3SKeng-Yu Lin 	},
341cbc00c13SZhang Rui 	/*
342cbc00c13SZhang Rui 	 * https://bugzilla.kernel.org/show_bug.cgi?id=189431
343cbc00c13SZhang Rui 	 * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
344cbc00c13SZhang Rui 	 * saving during S3.
345cbc00c13SZhang Rui 	 */
346cbc00c13SZhang Rui 	{
347cbc00c13SZhang Rui 	.callback = init_nvs_save_s3,
348cbc00c13SZhang Rui 	.ident = "Lenovo G50-45",
349cbc00c13SZhang Rui 	.matches = {
350cbc00c13SZhang Rui 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
351cbc00c13SZhang Rui 		DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
352cbc00c13SZhang Rui 		},
353cbc00c13SZhang Rui 	},
35471630b7aSRafael J. Wysocki 	/*
35571630b7aSRafael J. Wysocki 	 * https://bugzilla.kernel.org/show_bug.cgi?id=196907
35671630b7aSRafael J. Wysocki 	 * Some Dell XPS13 9360 cannot do suspend-to-idle using the Low Power
35771630b7aSRafael J. Wysocki 	 * S0 Idle firmware interface.
35871630b7aSRafael J. Wysocki 	 */
35971630b7aSRafael J. Wysocki 	{
36071630b7aSRafael J. Wysocki 	.callback = init_no_lps0,
36171630b7aSRafael J. Wysocki 	.ident = "Dell XPS13 9360",
36271630b7aSRafael J. Wysocki 	.matches = {
36371630b7aSRafael J. Wysocki 		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
36471630b7aSRafael J. Wysocki 		DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
36571630b7aSRafael J. Wysocki 		},
36671630b7aSRafael J. Wysocki 	},
367855c1c2fSChen Yu 	/*
368855c1c2fSChen Yu 	 * ThinkPad X1 Tablet(2016) cannot do suspend-to-idle using
369855c1c2fSChen Yu 	 * the Low Power S0 Idle firmware interface (see
370855c1c2fSChen Yu 	 * https://bugzilla.kernel.org/show_bug.cgi?id=199057).
371855c1c2fSChen Yu 	 */
372855c1c2fSChen Yu 	{
373855c1c2fSChen Yu 	.callback = init_no_lps0,
374855c1c2fSChen Yu 	.ident = "ThinkPad X1 Tablet(2016)",
375855c1c2fSChen Yu 	.matches = {
376855c1c2fSChen Yu 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
377855c1c2fSChen Yu 		DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
378855c1c2fSChen Yu 		},
379855c1c2fSChen Yu 	},
380d08ca2caSLen Brown 	{},
381d08ca2caSLen Brown };
3820ac1b1d7SZhang Rui 
38357044031SRafael J. Wysocki static bool ignore_blacklist;
38457044031SRafael J. Wysocki 
38557044031SRafael J. Wysocki void __init acpi_sleep_no_blacklist(void)
38657044031SRafael J. Wysocki {
38757044031SRafael J. Wysocki 	ignore_blacklist = true;
38857044031SRafael J. Wysocki }
38957044031SRafael J. Wysocki 
3908b691c9cSJan Beulich static void __init acpi_sleep_dmi_check(void)
3910ac1b1d7SZhang Rui {
39257044031SRafael J. Wysocki 	if (ignore_blacklist)
39357044031SRafael J. Wysocki 		return;
39457044031SRafael J. Wysocki 
3959c0a30b6SAndy Shevchenko 	if (dmi_get_bios_year() >= 2012)
396821d6f03SLan Tianyu 		acpi_nvs_nosave_s3();
397821d6f03SLan Tianyu 
3980ac1b1d7SZhang Rui 	dmi_check_system(acpisleep_dmi_table);
3990ac1b1d7SZhang Rui }
4000ac1b1d7SZhang Rui 
4010ac1b1d7SZhang Rui /**
4020ac1b1d7SZhang Rui  * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
4030ac1b1d7SZhang Rui  */
4040ac1b1d7SZhang Rui static int acpi_pm_freeze(void)
4050ac1b1d7SZhang Rui {
4060ac1b1d7SZhang Rui 	acpi_disable_all_gpes();
4070ac1b1d7SZhang Rui 	acpi_os_wait_events_complete();
4080ac1b1d7SZhang Rui 	acpi_ec_block_transactions();
4090ac1b1d7SZhang Rui 	return 0;
4100ac1b1d7SZhang Rui }
4110ac1b1d7SZhang Rui 
4120ac1b1d7SZhang Rui /**
4130ac1b1d7SZhang Rui  * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
4140ac1b1d7SZhang Rui  */
4150ac1b1d7SZhang Rui static int acpi_pm_pre_suspend(void)
4160ac1b1d7SZhang Rui {
4170ac1b1d7SZhang Rui 	acpi_pm_freeze();
4180ac1b1d7SZhang Rui 	return suspend_nvs_save();
4190ac1b1d7SZhang Rui }
4200ac1b1d7SZhang Rui 
4210ac1b1d7SZhang Rui /**
4220ac1b1d7SZhang Rui  *	__acpi_pm_prepare - Prepare the platform to enter the target state.
4230ac1b1d7SZhang Rui  *
4240ac1b1d7SZhang Rui  *	If necessary, set the firmware waking vector and do arch-specific
4250ac1b1d7SZhang Rui  *	nastiness to get the wakeup code to the waking vector.
4260ac1b1d7SZhang Rui  */
4270ac1b1d7SZhang Rui static int __acpi_pm_prepare(void)
4280ac1b1d7SZhang Rui {
4290ac1b1d7SZhang Rui 	int error = acpi_sleep_prepare(acpi_target_sleep_state);
4300ac1b1d7SZhang Rui 	if (error)
4310ac1b1d7SZhang Rui 		acpi_target_sleep_state = ACPI_STATE_S0;
4320ac1b1d7SZhang Rui 
4330ac1b1d7SZhang Rui 	return error;
4340ac1b1d7SZhang Rui }
4350ac1b1d7SZhang Rui 
4360ac1b1d7SZhang Rui /**
4370ac1b1d7SZhang Rui  *	acpi_pm_prepare - Prepare the platform to enter the target sleep
4380ac1b1d7SZhang Rui  *		state and disable the GPEs.
4390ac1b1d7SZhang Rui  */
4400ac1b1d7SZhang Rui static int acpi_pm_prepare(void)
4410ac1b1d7SZhang Rui {
4420ac1b1d7SZhang Rui 	int error = __acpi_pm_prepare();
4430ac1b1d7SZhang Rui 	if (!error)
4440ac1b1d7SZhang Rui 		error = acpi_pm_pre_suspend();
4450ac1b1d7SZhang Rui 
4460ac1b1d7SZhang Rui 	return error;
4470ac1b1d7SZhang Rui }
4480ac1b1d7SZhang Rui 
4490ac1b1d7SZhang Rui static int find_powerf_dev(struct device *dev, void *data)
4500ac1b1d7SZhang Rui {
4510ac1b1d7SZhang Rui 	struct acpi_device *device = to_acpi_device(dev);
4520ac1b1d7SZhang Rui 	const char *hid = acpi_device_hid(device);
4530ac1b1d7SZhang Rui 
4540ac1b1d7SZhang Rui 	return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
4550ac1b1d7SZhang Rui }
4560ac1b1d7SZhang Rui 
4570ac1b1d7SZhang Rui /**
4580ac1b1d7SZhang Rui  *	acpi_pm_finish - Instruct the platform to leave a sleep state.
4590ac1b1d7SZhang Rui  *
4600ac1b1d7SZhang Rui  *	This is called after we wake back up (or if entering the sleep state
4610ac1b1d7SZhang Rui  *	failed).
4620ac1b1d7SZhang Rui  */
4630ac1b1d7SZhang Rui static void acpi_pm_finish(void)
4640ac1b1d7SZhang Rui {
4650ac1b1d7SZhang Rui 	struct device *pwr_btn_dev;
4660ac1b1d7SZhang Rui 	u32 acpi_state = acpi_target_sleep_state;
4670ac1b1d7SZhang Rui 
4680ac1b1d7SZhang Rui 	acpi_ec_unblock_transactions();
4690ac1b1d7SZhang Rui 	suspend_nvs_free();
4700ac1b1d7SZhang Rui 
4710ac1b1d7SZhang Rui 	if (acpi_state == ACPI_STATE_S0)
4720ac1b1d7SZhang Rui 		return;
4730ac1b1d7SZhang Rui 
4740ac1b1d7SZhang Rui 	printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
4750ac1b1d7SZhang Rui 		acpi_state);
4760ac1b1d7SZhang Rui 	acpi_disable_wakeup_devices(acpi_state);
4770ac1b1d7SZhang Rui 	acpi_leave_sleep_state(acpi_state);
4780ac1b1d7SZhang Rui 
4790ac1b1d7SZhang Rui 	/* reset firmware waking vector */
480e3e9b577SRafael J. Wysocki 	acpi_set_waking_vector(0);
4810ac1b1d7SZhang Rui 
4820ac1b1d7SZhang Rui 	acpi_target_sleep_state = ACPI_STATE_S0;
4830ac1b1d7SZhang Rui 
484781d737cSRafael J. Wysocki 	acpi_resume_power_resources();
485781d737cSRafael J. Wysocki 
4860ac1b1d7SZhang Rui 	/* If we were woken with the fixed power button, provide a small
4870ac1b1d7SZhang Rui 	 * hint to userspace in the form of a wakeup event on the fixed power
4880ac1b1d7SZhang Rui 	 * button device (if it can be found).
4890ac1b1d7SZhang Rui 	 *
4900ac1b1d7SZhang Rui 	 * We delay the event generation til now, as the PM layer requires
4910ac1b1d7SZhang Rui 	 * timekeeping to be running before we generate events. */
4920ac1b1d7SZhang Rui 	if (!pwr_btn_event_pending)
4930ac1b1d7SZhang Rui 		return;
4940ac1b1d7SZhang Rui 
4950ac1b1d7SZhang Rui 	pwr_btn_event_pending = false;
4960ac1b1d7SZhang Rui 	pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
4970ac1b1d7SZhang Rui 				      find_powerf_dev);
4980ac1b1d7SZhang Rui 	if (pwr_btn_dev) {
4990ac1b1d7SZhang Rui 		pm_wakeup_event(pwr_btn_dev, 0);
5000ac1b1d7SZhang Rui 		put_device(pwr_btn_dev);
5010ac1b1d7SZhang Rui 	}
5020ac1b1d7SZhang Rui }
5030ac1b1d7SZhang Rui 
5040ac1b1d7SZhang Rui /**
505ad07277eSRafael J. Wysocki  * acpi_pm_start - Start system PM transition.
506ad07277eSRafael J. Wysocki  */
507ad07277eSRafael J. Wysocki static void acpi_pm_start(u32 acpi_state)
508ad07277eSRafael J. Wysocki {
509ad07277eSRafael J. Wysocki 	acpi_target_sleep_state = acpi_state;
510ad07277eSRafael J. Wysocki 	acpi_sleep_tts_switch(acpi_target_sleep_state);
511ad07277eSRafael J. Wysocki 	acpi_scan_lock_acquire();
512ad07277eSRafael J. Wysocki }
513ad07277eSRafael J. Wysocki 
514ad07277eSRafael J. Wysocki /**
515ad07277eSRafael J. Wysocki  * acpi_pm_end - Finish up system PM transition.
5160ac1b1d7SZhang Rui  */
5170ac1b1d7SZhang Rui static void acpi_pm_end(void)
5180ac1b1d7SZhang Rui {
5198ece1d83SHans de Goede 	acpi_turn_off_unused_power_resources();
520ad07277eSRafael J. Wysocki 	acpi_scan_lock_release();
5210ac1b1d7SZhang Rui 	/*
5220ac1b1d7SZhang Rui 	 * This is necessary in case acpi_pm_finish() is not called during a
5230ac1b1d7SZhang Rui 	 * failing transition to a sleep state.
5240ac1b1d7SZhang Rui 	 */
5250ac1b1d7SZhang Rui 	acpi_target_sleep_state = ACPI_STATE_S0;
5260ac1b1d7SZhang Rui 	acpi_sleep_tts_switch(acpi_target_sleep_state);
5270ac1b1d7SZhang Rui }
5280ac1b1d7SZhang Rui #else /* !CONFIG_ACPI_SLEEP */
5290ac1b1d7SZhang Rui #define acpi_target_sleep_state	ACPI_STATE_S0
53071630b7aSRafael J. Wysocki #define acpi_sleep_no_lps0	(false)
5310ac1b1d7SZhang Rui static inline void acpi_sleep_dmi_check(void) {}
5320ac1b1d7SZhang Rui #endif /* CONFIG_ACPI_SLEEP */
5330ac1b1d7SZhang Rui 
5340ac1b1d7SZhang Rui #ifdef CONFIG_SUSPEND
5350ac1b1d7SZhang Rui static u32 acpi_suspend_states[] = {
5360ac1b1d7SZhang Rui 	[PM_SUSPEND_ON] = ACPI_STATE_S0,
5370ac1b1d7SZhang Rui 	[PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
5380ac1b1d7SZhang Rui 	[PM_SUSPEND_MEM] = ACPI_STATE_S3,
5390ac1b1d7SZhang Rui 	[PM_SUSPEND_MAX] = ACPI_STATE_S5
5400ac1b1d7SZhang Rui };
5410ac1b1d7SZhang Rui 
5420ac1b1d7SZhang Rui /**
5430ac1b1d7SZhang Rui  *	acpi_suspend_begin - Set the target system sleep state to the state
5440ac1b1d7SZhang Rui  *		associated with given @pm_state, if supported.
5450ac1b1d7SZhang Rui  */
5460ac1b1d7SZhang Rui static int acpi_suspend_begin(suspend_state_t pm_state)
5470ac1b1d7SZhang Rui {
5480ac1b1d7SZhang Rui 	u32 acpi_state = acpi_suspend_states[pm_state];
549ad07277eSRafael J. Wysocki 	int error;
5500ac1b1d7SZhang Rui 
5510ac1b1d7SZhang Rui 	error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
5520ac1b1d7SZhang Rui 	if (error)
5530ac1b1d7SZhang Rui 		return error;
5540ac1b1d7SZhang Rui 
555ad07277eSRafael J. Wysocki 	if (!sleep_states[acpi_state]) {
556ad07277eSRafael J. Wysocki 		pr_err("ACPI does not support sleep state S%u\n", acpi_state);
557ad07277eSRafael J. Wysocki 		return -ENOSYS;
5580ac1b1d7SZhang Rui 	}
559ef25ba04SRafael J. Wysocki 	if (acpi_state > ACPI_STATE_S1)
560ef25ba04SRafael J. Wysocki 		pm_set_suspend_via_firmware();
561ad07277eSRafael J. Wysocki 
562ad07277eSRafael J. Wysocki 	acpi_pm_start(acpi_state);
563ad07277eSRafael J. Wysocki 	return 0;
5640ac1b1d7SZhang Rui }
5650ac1b1d7SZhang Rui 
5660ac1b1d7SZhang Rui /**
5670ac1b1d7SZhang Rui  *	acpi_suspend_enter - Actually enter a sleep state.
5680ac1b1d7SZhang Rui  *	@pm_state: ignored
5690ac1b1d7SZhang Rui  *
5700ac1b1d7SZhang Rui  *	Flush caches and go to sleep. For STR we have to call arch-specific
5710ac1b1d7SZhang Rui  *	assembly, which in turn call acpi_enter_sleep_state().
5720ac1b1d7SZhang Rui  *	It's unfortunate, but it works. Please fix if you're feeling frisky.
5730ac1b1d7SZhang Rui  */
5740ac1b1d7SZhang Rui static int acpi_suspend_enter(suspend_state_t pm_state)
5750ac1b1d7SZhang Rui {
5760ac1b1d7SZhang Rui 	acpi_status status = AE_OK;
5770ac1b1d7SZhang Rui 	u32 acpi_state = acpi_target_sleep_state;
5780ac1b1d7SZhang Rui 	int error;
5790ac1b1d7SZhang Rui 
5800ac1b1d7SZhang Rui 	ACPI_FLUSH_CPU_CACHE();
5810ac1b1d7SZhang Rui 
582bb3632c6STodd E Brandt 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
5830ac1b1d7SZhang Rui 	switch (acpi_state) {
5840ac1b1d7SZhang Rui 	case ACPI_STATE_S1:
5850ac1b1d7SZhang Rui 		barrier();
5860ac1b1d7SZhang Rui 		status = acpi_enter_sleep_state(acpi_state);
5870ac1b1d7SZhang Rui 		break;
5880ac1b1d7SZhang Rui 
5890ac1b1d7SZhang Rui 	case ACPI_STATE_S3:
590d6a77eadSKonrad Rzeszutek Wilk 		if (!acpi_suspend_lowlevel)
591d6a77eadSKonrad Rzeszutek Wilk 			return -ENOSYS;
5920ac1b1d7SZhang Rui 		error = acpi_suspend_lowlevel();
5930ac1b1d7SZhang Rui 		if (error)
5940ac1b1d7SZhang Rui 			return error;
5950ac1b1d7SZhang Rui 		pr_info(PREFIX "Low-level resume complete\n");
596ef25ba04SRafael J. Wysocki 		pm_set_resume_via_firmware();
5970ac1b1d7SZhang Rui 		break;
5980ac1b1d7SZhang Rui 	}
599bb3632c6STodd E Brandt 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
6000ac1b1d7SZhang Rui 
6010ac1b1d7SZhang Rui 	/* This violates the spec but is required for bug compatibility. */
6020ac1b1d7SZhang Rui 	acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
6030ac1b1d7SZhang Rui 
6040ac1b1d7SZhang Rui 	/* Reprogram control registers */
6050ac1b1d7SZhang Rui 	acpi_leave_sleep_state_prep(acpi_state);
6060ac1b1d7SZhang Rui 
6070ac1b1d7SZhang Rui 	/* ACPI 3.0 specs (P62) says that it's the responsibility
6080ac1b1d7SZhang Rui 	 * of the OSPM to clear the status bit [ implying that the
6090ac1b1d7SZhang Rui 	 * POWER_BUTTON event should not reach userspace ]
6100ac1b1d7SZhang Rui 	 *
6110ac1b1d7SZhang Rui 	 * However, we do generate a small hint for userspace in the form of
6120ac1b1d7SZhang Rui 	 * a wakeup event. We flag this condition for now and generate the
6130ac1b1d7SZhang Rui 	 * event later, as we're currently too early in resume to be able to
6140ac1b1d7SZhang Rui 	 * generate wakeup events.
6150ac1b1d7SZhang Rui 	 */
6160ac1b1d7SZhang Rui 	if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
61751468a9dSAl Stone 		acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
6180ac1b1d7SZhang Rui 
6190ac1b1d7SZhang Rui 		acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
6200ac1b1d7SZhang Rui 
62118864cc4SLv Zheng 		if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
6220ac1b1d7SZhang Rui 			acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
6230ac1b1d7SZhang Rui 			/* Flag for later */
6240ac1b1d7SZhang Rui 			pwr_btn_event_pending = true;
6250ac1b1d7SZhang Rui 		}
6260ac1b1d7SZhang Rui 	}
6270ac1b1d7SZhang Rui 
6280ac1b1d7SZhang Rui 	/*
6290ac1b1d7SZhang Rui 	 * Disable and clear GPE status before interrupt is enabled. Some GPEs
6300ac1b1d7SZhang Rui 	 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
6310ac1b1d7SZhang Rui 	 * acpi_leave_sleep_state will reenable specific GPEs later
6320ac1b1d7SZhang Rui 	 */
6330ac1b1d7SZhang Rui 	acpi_disable_all_gpes();
6340ac1b1d7SZhang Rui 	/* Allow EC transactions to happen. */
635c2b46d67SLv Zheng 	acpi_ec_unblock_transactions();
6360ac1b1d7SZhang Rui 
6370ac1b1d7SZhang Rui 	suspend_nvs_restore();
6380ac1b1d7SZhang Rui 
6390ac1b1d7SZhang Rui 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
6400ac1b1d7SZhang Rui }
6410ac1b1d7SZhang Rui 
6420ac1b1d7SZhang Rui static int acpi_suspend_state_valid(suspend_state_t pm_state)
6430ac1b1d7SZhang Rui {
6440ac1b1d7SZhang Rui 	u32 acpi_state;
6450ac1b1d7SZhang Rui 
6460ac1b1d7SZhang Rui 	switch (pm_state) {
6470ac1b1d7SZhang Rui 	case PM_SUSPEND_ON:
6480ac1b1d7SZhang Rui 	case PM_SUSPEND_STANDBY:
6490ac1b1d7SZhang Rui 	case PM_SUSPEND_MEM:
6500ac1b1d7SZhang Rui 		acpi_state = acpi_suspend_states[pm_state];
6510ac1b1d7SZhang Rui 
6520ac1b1d7SZhang Rui 		return sleep_states[acpi_state];
6530ac1b1d7SZhang Rui 	default:
6540ac1b1d7SZhang Rui 		return 0;
6550ac1b1d7SZhang Rui 	}
6560ac1b1d7SZhang Rui }
6570ac1b1d7SZhang Rui 
6580ac1b1d7SZhang Rui static const struct platform_suspend_ops acpi_suspend_ops = {
6590ac1b1d7SZhang Rui 	.valid = acpi_suspend_state_valid,
6600ac1b1d7SZhang Rui 	.begin = acpi_suspend_begin,
6610ac1b1d7SZhang Rui 	.prepare_late = acpi_pm_prepare,
6620ac1b1d7SZhang Rui 	.enter = acpi_suspend_enter,
6630ac1b1d7SZhang Rui 	.wake = acpi_pm_finish,
6640ac1b1d7SZhang Rui 	.end = acpi_pm_end,
6650ac1b1d7SZhang Rui };
6660ac1b1d7SZhang Rui 
6670ac1b1d7SZhang Rui /**
6680ac1b1d7SZhang Rui  *	acpi_suspend_begin_old - Set the target system sleep state to the
6690ac1b1d7SZhang Rui  *		state associated with given @pm_state, if supported, and
6700ac1b1d7SZhang Rui  *		execute the _PTS control method.  This function is used if the
6710ac1b1d7SZhang Rui  *		pre-ACPI 2.0 suspend ordering has been requested.
6720ac1b1d7SZhang Rui  */
6730ac1b1d7SZhang Rui static int acpi_suspend_begin_old(suspend_state_t pm_state)
6740ac1b1d7SZhang Rui {
6750ac1b1d7SZhang Rui 	int error = acpi_suspend_begin(pm_state);
6760ac1b1d7SZhang Rui 	if (!error)
6770ac1b1d7SZhang Rui 		error = __acpi_pm_prepare();
6780ac1b1d7SZhang Rui 
6790ac1b1d7SZhang Rui 	return error;
6800ac1b1d7SZhang Rui }
6810ac1b1d7SZhang Rui 
6820ac1b1d7SZhang Rui /*
6830ac1b1d7SZhang Rui  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
6840ac1b1d7SZhang Rui  * been requested.
6850ac1b1d7SZhang Rui  */
6860ac1b1d7SZhang Rui static const struct platform_suspend_ops acpi_suspend_ops_old = {
6870ac1b1d7SZhang Rui 	.valid = acpi_suspend_state_valid,
6880ac1b1d7SZhang Rui 	.begin = acpi_suspend_begin_old,
6890ac1b1d7SZhang Rui 	.prepare_late = acpi_pm_pre_suspend,
6900ac1b1d7SZhang Rui 	.enter = acpi_suspend_enter,
6910ac1b1d7SZhang Rui 	.wake = acpi_pm_finish,
6920ac1b1d7SZhang Rui 	.end = acpi_pm_end,
6930ac1b1d7SZhang Rui 	.recover = acpi_pm_finish,
6940ac1b1d7SZhang Rui };
69502040f0bSRafael J. Wysocki 
6968110dd28SRafael J. Wysocki static bool s2idle_in_progress;
69733e4f80eSRafael J. Wysocki static bool s2idle_wakeup;
69833e4f80eSRafael J. Wysocki 
6998110dd28SRafael J. Wysocki /*
7008110dd28SRafael J. Wysocki  * On platforms supporting the Low Power S0 Idle interface there is an ACPI
7018110dd28SRafael J. Wysocki  * device object with the PNP0D80 compatible device ID (System Power Management
7028110dd28SRafael J. Wysocki  * Controller) and a specific _DSM method under it.  That method, if present,
7038110dd28SRafael J. Wysocki  * can be used to indicate to the platform that the OS is transitioning into a
7048110dd28SRafael J. Wysocki  * low-power state in which certain types of activity are not desirable or that
7058110dd28SRafael J. Wysocki  * it is leaving such a state, which allows the platform to adjust its operation
7068110dd28SRafael J. Wysocki  * mode accordingly.
7078110dd28SRafael J. Wysocki  */
7088110dd28SRafael J. Wysocki static const struct acpi_device_id lps0_device_ids[] = {
7098110dd28SRafael J. Wysocki 	{"PNP0D80", },
7108110dd28SRafael J. Wysocki 	{"", },
7118110dd28SRafael J. Wysocki };
7128110dd28SRafael J. Wysocki 
7138110dd28SRafael J. Wysocki #define ACPI_LPS0_DSM_UUID	"c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
7148110dd28SRafael J. Wysocki 
715726fb6b4SSrinivas Pandruvada #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS	1
7168110dd28SRafael J. Wysocki #define ACPI_LPS0_SCREEN_OFF	3
7178110dd28SRafael J. Wysocki #define ACPI_LPS0_SCREEN_ON	4
7188110dd28SRafael J. Wysocki #define ACPI_LPS0_ENTRY		5
7198110dd28SRafael J. Wysocki #define ACPI_LPS0_EXIT		6
7208110dd28SRafael J. Wysocki 
72129a5a6d7SRafael J. Wysocki #define ACPI_LPS0_SCREEN_MASK	((1 << ACPI_LPS0_SCREEN_OFF) | (1 << ACPI_LPS0_SCREEN_ON))
72229a5a6d7SRafael J. Wysocki #define ACPI_LPS0_PLATFORM_MASK	((1 << ACPI_LPS0_ENTRY) | (1 << ACPI_LPS0_EXIT))
7238110dd28SRafael J. Wysocki 
7248110dd28SRafael J. Wysocki static acpi_handle lps0_device_handle;
7258110dd28SRafael J. Wysocki static guid_t lps0_dsm_guid;
7268110dd28SRafael J. Wysocki static char lps0_dsm_func_mask;
7278110dd28SRafael J. Wysocki 
728726fb6b4SSrinivas Pandruvada /* Device constraint entry structure */
729726fb6b4SSrinivas Pandruvada struct lpi_device_info {
730726fb6b4SSrinivas Pandruvada 	char *name;
731726fb6b4SSrinivas Pandruvada 	int enabled;
732726fb6b4SSrinivas Pandruvada 	union acpi_object *package;
733726fb6b4SSrinivas Pandruvada };
734726fb6b4SSrinivas Pandruvada 
735726fb6b4SSrinivas Pandruvada /* Constraint package structure */
736726fb6b4SSrinivas Pandruvada struct lpi_device_constraint {
737726fb6b4SSrinivas Pandruvada 	int uid;
738726fb6b4SSrinivas Pandruvada 	int min_dstate;
739726fb6b4SSrinivas Pandruvada 	int function_states;
740726fb6b4SSrinivas Pandruvada };
741726fb6b4SSrinivas Pandruvada 
742726fb6b4SSrinivas Pandruvada struct lpi_constraints {
743726fb6b4SSrinivas Pandruvada 	acpi_handle handle;
744726fb6b4SSrinivas Pandruvada 	int min_dstate;
745726fb6b4SSrinivas Pandruvada };
746726fb6b4SSrinivas Pandruvada 
747726fb6b4SSrinivas Pandruvada static struct lpi_constraints *lpi_constraints_table;
748726fb6b4SSrinivas Pandruvada static int lpi_constraints_table_size;
749726fb6b4SSrinivas Pandruvada 
750726fb6b4SSrinivas Pandruvada static void lpi_device_get_constraints(void)
751726fb6b4SSrinivas Pandruvada {
752726fb6b4SSrinivas Pandruvada 	union acpi_object *out_obj;
753726fb6b4SSrinivas Pandruvada 	int i;
754726fb6b4SSrinivas Pandruvada 
755726fb6b4SSrinivas Pandruvada 	out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
756726fb6b4SSrinivas Pandruvada 					  1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
757726fb6b4SSrinivas Pandruvada 					  NULL, ACPI_TYPE_PACKAGE);
758726fb6b4SSrinivas Pandruvada 
759726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
760726fb6b4SSrinivas Pandruvada 			  out_obj ? "successful" : "failed");
761726fb6b4SSrinivas Pandruvada 
762726fb6b4SSrinivas Pandruvada 	if (!out_obj)
763726fb6b4SSrinivas Pandruvada 		return;
764726fb6b4SSrinivas Pandruvada 
765726fb6b4SSrinivas Pandruvada 	lpi_constraints_table = kcalloc(out_obj->package.count,
766726fb6b4SSrinivas Pandruvada 					sizeof(*lpi_constraints_table),
767726fb6b4SSrinivas Pandruvada 					GFP_KERNEL);
768726fb6b4SSrinivas Pandruvada 	if (!lpi_constraints_table)
769726fb6b4SSrinivas Pandruvada 		goto free_acpi_buffer;
770726fb6b4SSrinivas Pandruvada 
771726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
772726fb6b4SSrinivas Pandruvada 
773726fb6b4SSrinivas Pandruvada 	for (i = 0; i < out_obj->package.count; i++) {
774726fb6b4SSrinivas Pandruvada 		struct lpi_constraints *constraint;
775726fb6b4SSrinivas Pandruvada 		acpi_status status;
776726fb6b4SSrinivas Pandruvada 		union acpi_object *package = &out_obj->package.elements[i];
777726fb6b4SSrinivas Pandruvada 		struct lpi_device_info info = { };
778726fb6b4SSrinivas Pandruvada 		int package_count = 0, j;
779726fb6b4SSrinivas Pandruvada 
780726fb6b4SSrinivas Pandruvada 		if (!package)
781726fb6b4SSrinivas Pandruvada 			continue;
782726fb6b4SSrinivas Pandruvada 
783726fb6b4SSrinivas Pandruvada 		for (j = 0; j < package->package.count; ++j) {
784726fb6b4SSrinivas Pandruvada 			union acpi_object *element =
785726fb6b4SSrinivas Pandruvada 					&(package->package.elements[j]);
786726fb6b4SSrinivas Pandruvada 
787726fb6b4SSrinivas Pandruvada 			switch (element->type) {
788726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_INTEGER:
789726fb6b4SSrinivas Pandruvada 				info.enabled = element->integer.value;
790726fb6b4SSrinivas Pandruvada 				break;
791726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_STRING:
792726fb6b4SSrinivas Pandruvada 				info.name = element->string.pointer;
793726fb6b4SSrinivas Pandruvada 				break;
794726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_PACKAGE:
795726fb6b4SSrinivas Pandruvada 				package_count = element->package.count;
796726fb6b4SSrinivas Pandruvada 				info.package = element->package.elements;
797726fb6b4SSrinivas Pandruvada 				break;
798726fb6b4SSrinivas Pandruvada 			}
799726fb6b4SSrinivas Pandruvada 		}
800726fb6b4SSrinivas Pandruvada 
801726fb6b4SSrinivas Pandruvada 		if (!info.enabled || !info.package || !info.name)
802726fb6b4SSrinivas Pandruvada 			continue;
803726fb6b4SSrinivas Pandruvada 
804726fb6b4SSrinivas Pandruvada 		constraint = &lpi_constraints_table[lpi_constraints_table_size];
805726fb6b4SSrinivas Pandruvada 
806726fb6b4SSrinivas Pandruvada 		status = acpi_get_handle(NULL, info.name, &constraint->handle);
807726fb6b4SSrinivas Pandruvada 		if (ACPI_FAILURE(status))
808726fb6b4SSrinivas Pandruvada 			continue;
809726fb6b4SSrinivas Pandruvada 
810726fb6b4SSrinivas Pandruvada 		acpi_handle_debug(lps0_device_handle,
811726fb6b4SSrinivas Pandruvada 				  "index:%d Name:%s\n", i, info.name);
812726fb6b4SSrinivas Pandruvada 
813726fb6b4SSrinivas Pandruvada 		constraint->min_dstate = -1;
814726fb6b4SSrinivas Pandruvada 
815726fb6b4SSrinivas Pandruvada 		for (j = 0; j < package_count; ++j) {
816726fb6b4SSrinivas Pandruvada 			union acpi_object *info_obj = &info.package[j];
817726fb6b4SSrinivas Pandruvada 			union acpi_object *cnstr_pkg;
818726fb6b4SSrinivas Pandruvada 			union acpi_object *obj;
819726fb6b4SSrinivas Pandruvada 			struct lpi_device_constraint dev_info;
820726fb6b4SSrinivas Pandruvada 
821726fb6b4SSrinivas Pandruvada 			switch (info_obj->type) {
822726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_INTEGER:
823726fb6b4SSrinivas Pandruvada 				/* version */
824726fb6b4SSrinivas Pandruvada 				break;
825726fb6b4SSrinivas Pandruvada 			case ACPI_TYPE_PACKAGE:
826726fb6b4SSrinivas Pandruvada 				if (info_obj->package.count < 2)
827726fb6b4SSrinivas Pandruvada 					break;
828726fb6b4SSrinivas Pandruvada 
829726fb6b4SSrinivas Pandruvada 				cnstr_pkg = info_obj->package.elements;
830726fb6b4SSrinivas Pandruvada 				obj = &cnstr_pkg[0];
831726fb6b4SSrinivas Pandruvada 				dev_info.uid = obj->integer.value;
832726fb6b4SSrinivas Pandruvada 				obj = &cnstr_pkg[1];
833726fb6b4SSrinivas Pandruvada 				dev_info.min_dstate = obj->integer.value;
834726fb6b4SSrinivas Pandruvada 
835726fb6b4SSrinivas Pandruvada 				acpi_handle_debug(lps0_device_handle,
836726fb6b4SSrinivas Pandruvada 					"uid:%d min_dstate:%s\n",
837726fb6b4SSrinivas Pandruvada 					dev_info.uid,
838726fb6b4SSrinivas Pandruvada 					acpi_power_state_string(dev_info.min_dstate));
839726fb6b4SSrinivas Pandruvada 
840726fb6b4SSrinivas Pandruvada 				constraint->min_dstate = dev_info.min_dstate;
841726fb6b4SSrinivas Pandruvada 				break;
842726fb6b4SSrinivas Pandruvada 			}
843726fb6b4SSrinivas Pandruvada 		}
844726fb6b4SSrinivas Pandruvada 
845726fb6b4SSrinivas Pandruvada 		if (constraint->min_dstate < 0) {
846726fb6b4SSrinivas Pandruvada 			acpi_handle_debug(lps0_device_handle,
847726fb6b4SSrinivas Pandruvada 					  "Incomplete constraint defined\n");
848726fb6b4SSrinivas Pandruvada 			continue;
849726fb6b4SSrinivas Pandruvada 		}
850726fb6b4SSrinivas Pandruvada 
851726fb6b4SSrinivas Pandruvada 		lpi_constraints_table_size++;
852726fb6b4SSrinivas Pandruvada 	}
853726fb6b4SSrinivas Pandruvada 
854726fb6b4SSrinivas Pandruvada 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
855726fb6b4SSrinivas Pandruvada 
856726fb6b4SSrinivas Pandruvada free_acpi_buffer:
857726fb6b4SSrinivas Pandruvada 	ACPI_FREE(out_obj);
858726fb6b4SSrinivas Pandruvada }
859726fb6b4SSrinivas Pandruvada 
860726fb6b4SSrinivas Pandruvada static void lpi_check_constraints(void)
861726fb6b4SSrinivas Pandruvada {
862726fb6b4SSrinivas Pandruvada 	int i;
863726fb6b4SSrinivas Pandruvada 
864726fb6b4SSrinivas Pandruvada 	for (i = 0; i < lpi_constraints_table_size; ++i) {
865fa54150aSRafael J. Wysocki 		acpi_handle handle = lpi_constraints_table[i].handle;
866726fb6b4SSrinivas Pandruvada 		struct acpi_device *adev;
867726fb6b4SSrinivas Pandruvada 
868fa54150aSRafael J. Wysocki 		if (!handle || acpi_bus_get_device(handle, &adev))
869726fb6b4SSrinivas Pandruvada 			continue;
870726fb6b4SSrinivas Pandruvada 
871fa54150aSRafael J. Wysocki 		acpi_handle_debug(handle,
872726fb6b4SSrinivas Pandruvada 			"LPI: required min power state:%s current power state:%s\n",
873726fb6b4SSrinivas Pandruvada 			acpi_power_state_string(lpi_constraints_table[i].min_dstate),
874726fb6b4SSrinivas Pandruvada 			acpi_power_state_string(adev->power.state));
875726fb6b4SSrinivas Pandruvada 
876726fb6b4SSrinivas Pandruvada 		if (!adev->flags.power_manageable) {
877fa54150aSRafael J. Wysocki 			acpi_handle_info(handle, "LPI: Device not power manageable\n");
878fa54150aSRafael J. Wysocki 			lpi_constraints_table[i].handle = NULL;
879726fb6b4SSrinivas Pandruvada 			continue;
880726fb6b4SSrinivas Pandruvada 		}
881726fb6b4SSrinivas Pandruvada 
882726fb6b4SSrinivas Pandruvada 		if (adev->power.state < lpi_constraints_table[i].min_dstate)
883fa54150aSRafael J. Wysocki 			acpi_handle_info(handle,
884726fb6b4SSrinivas Pandruvada 				"LPI: Constraint not met; min power state:%s current power state:%s\n",
885726fb6b4SSrinivas Pandruvada 				acpi_power_state_string(lpi_constraints_table[i].min_dstate),
886726fb6b4SSrinivas Pandruvada 				acpi_power_state_string(adev->power.state));
887726fb6b4SSrinivas Pandruvada 	}
888726fb6b4SSrinivas Pandruvada }
889726fb6b4SSrinivas Pandruvada 
8908110dd28SRafael J. Wysocki static void acpi_sleep_run_lps0_dsm(unsigned int func)
8918110dd28SRafael J. Wysocki {
8928110dd28SRafael J. Wysocki 	union acpi_object *out_obj;
8938110dd28SRafael J. Wysocki 
8948110dd28SRafael J. Wysocki 	if (!(lps0_dsm_func_mask & (1 << func)))
8958110dd28SRafael J. Wysocki 		return;
8968110dd28SRafael J. Wysocki 
8978110dd28SRafael J. Wysocki 	out_obj = acpi_evaluate_dsm(lps0_device_handle, &lps0_dsm_guid, 1, func, NULL);
8988110dd28SRafael J. Wysocki 	ACPI_FREE(out_obj);
8998110dd28SRafael J. Wysocki 
9008110dd28SRafael J. Wysocki 	acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
9018110dd28SRafael J. Wysocki 			  func, out_obj ? "successful" : "failed");
9028110dd28SRafael J. Wysocki }
9038110dd28SRafael J. Wysocki 
9048110dd28SRafael J. Wysocki static int lps0_device_attach(struct acpi_device *adev,
9058110dd28SRafael J. Wysocki 			      const struct acpi_device_id *not_used)
9068110dd28SRafael J. Wysocki {
9078110dd28SRafael J. Wysocki 	union acpi_object *out_obj;
9088110dd28SRafael J. Wysocki 
9098110dd28SRafael J. Wysocki 	if (lps0_device_handle)
9108110dd28SRafael J. Wysocki 		return 0;
9118110dd28SRafael J. Wysocki 
91271630b7aSRafael J. Wysocki 	if (acpi_sleep_no_lps0) {
91371630b7aSRafael J. Wysocki 		acpi_handle_info(adev->handle,
91471630b7aSRafael J. Wysocki 				 "Low Power S0 Idle interface disabled\n");
91571630b7aSRafael J. Wysocki 		return 0;
91671630b7aSRafael J. Wysocki 	}
91771630b7aSRafael J. Wysocki 
9188110dd28SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
9198110dd28SRafael J. Wysocki 		return 0;
9208110dd28SRafael J. Wysocki 
9218110dd28SRafael J. Wysocki 	guid_parse(ACPI_LPS0_DSM_UUID, &lps0_dsm_guid);
9228110dd28SRafael J. Wysocki 	/* Check if the _DSM is present and as expected. */
9238110dd28SRafael J. Wysocki 	out_obj = acpi_evaluate_dsm(adev->handle, &lps0_dsm_guid, 1, 0, NULL);
9248110dd28SRafael J. Wysocki 	if (out_obj && out_obj->type == ACPI_TYPE_BUFFER) {
9258110dd28SRafael J. Wysocki 		char bitmask = *(char *)out_obj->buffer.pointer;
9268110dd28SRafael J. Wysocki 
92729a5a6d7SRafael J. Wysocki 		if ((bitmask & ACPI_LPS0_PLATFORM_MASK) == ACPI_LPS0_PLATFORM_MASK ||
92829a5a6d7SRafael J. Wysocki 		    (bitmask & ACPI_LPS0_SCREEN_MASK) == ACPI_LPS0_SCREEN_MASK) {
9298110dd28SRafael J. Wysocki 			lps0_dsm_func_mask = bitmask;
9308110dd28SRafael J. Wysocki 			lps0_device_handle = adev->handle;
931e870c6c8SRafael J. Wysocki 			/*
932e870c6c8SRafael J. Wysocki 			 * Use suspend-to-idle by default if the default
933e870c6c8SRafael J. Wysocki 			 * suspend mode was not set from the command line.
934e870c6c8SRafael J. Wysocki 			 */
935e870c6c8SRafael J. Wysocki 			if (mem_sleep_default > PM_SUSPEND_MEM)
936690cbb90SRafael J. Wysocki 				mem_sleep_current = PM_SUSPEND_TO_IDLE;
9378110dd28SRafael J. Wysocki 		}
9388110dd28SRafael J. Wysocki 
9398110dd28SRafael J. Wysocki 		acpi_handle_debug(adev->handle, "_DSM function mask: 0x%x\n",
9408110dd28SRafael J. Wysocki 				  bitmask);
9418110dd28SRafael J. Wysocki 	} else {
9428110dd28SRafael J. Wysocki 		acpi_handle_debug(adev->handle,
9438110dd28SRafael J. Wysocki 				  "_DSM function 0 evaluation failed\n");
9448110dd28SRafael J. Wysocki 	}
9458110dd28SRafael J. Wysocki 	ACPI_FREE(out_obj);
946726fb6b4SSrinivas Pandruvada 
947726fb6b4SSrinivas Pandruvada 	lpi_device_get_constraints();
948726fb6b4SSrinivas Pandruvada 
9498110dd28SRafael J. Wysocki 	return 0;
9508110dd28SRafael J. Wysocki }
9518110dd28SRafael J. Wysocki 
9528110dd28SRafael J. Wysocki static struct acpi_scan_handler lps0_handler = {
9538110dd28SRafael J. Wysocki 	.ids = lps0_device_ids,
9548110dd28SRafael J. Wysocki 	.attach = lps0_device_attach,
9558110dd28SRafael J. Wysocki };
9568110dd28SRafael J. Wysocki 
95723d5855fSRafael J. Wysocki static int acpi_s2idle_begin(void)
9581f0b6386SRafael J. Wysocki {
9591f0b6386SRafael J. Wysocki 	acpi_scan_lock_acquire();
9608110dd28SRafael J. Wysocki 	s2idle_in_progress = true;
9611f0b6386SRafael J. Wysocki 	return 0;
9621f0b6386SRafael J. Wysocki }
9631f0b6386SRafael J. Wysocki 
96423d5855fSRafael J. Wysocki static int acpi_s2idle_prepare(void)
965a8d46b9eSRafael J. Wysocki {
9668110dd28SRafael J. Wysocki 	if (lps0_device_handle) {
9678110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF);
9688110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY);
9698110dd28SRafael J. Wysocki 	}
970147a7d9dSRafael J. Wysocki 
9718c01275eSChen Yu 	if (acpi_sci_irq_valid())
9728c01275eSChen Yu 		enable_irq_wake(acpi_sci_irq);
973235d81a6SRafael J. Wysocki 
974a8d46b9eSRafael J. Wysocki 	return 0;
975a8d46b9eSRafael J. Wysocki }
976a8d46b9eSRafael J. Wysocki 
97723d5855fSRafael J. Wysocki static void acpi_s2idle_wake(void)
97833e4f80eSRafael J. Wysocki {
979726fb6b4SSrinivas Pandruvada 
980726fb6b4SSrinivas Pandruvada 	if (pm_debug_messages_on)
981726fb6b4SSrinivas Pandruvada 		lpi_check_constraints();
982726fb6b4SSrinivas Pandruvada 
98333e4f80eSRafael J. Wysocki 	/*
98433e4f80eSRafael J. Wysocki 	 * If IRQD_WAKEUP_ARMED is not set for the SCI at this point, it means
98533e4f80eSRafael J. Wysocki 	 * that the SCI has triggered while suspended, so cancel the wakeup in
98633e4f80eSRafael J. Wysocki 	 * case it has not been a wakeup event (the GPEs will be checked later).
98733e4f80eSRafael J. Wysocki 	 */
98833e4f80eSRafael J. Wysocki 	if (acpi_sci_irq_valid() &&
98933e4f80eSRafael J. Wysocki 	    !irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq))) {
99033e4f80eSRafael J. Wysocki 		pm_system_cancel_wakeup();
99133e4f80eSRafael J. Wysocki 		s2idle_wakeup = true;
992*68e22011SRafael J. Wysocki 		/*
993*68e22011SRafael J. Wysocki 		 * On some platforms with the LPS0 _DSM device noirq resume
994*68e22011SRafael J. Wysocki 		 * takes too much time for EC wakeup events to survive, so look
995*68e22011SRafael J. Wysocki 		 * for them now.
996*68e22011SRafael J. Wysocki 		 */
997*68e22011SRafael J. Wysocki 		if (lps0_device_handle)
998*68e22011SRafael J. Wysocki 			acpi_ec_dispatch_gpe();
99933e4f80eSRafael J. Wysocki 	}
100033e4f80eSRafael J. Wysocki }
100133e4f80eSRafael J. Wysocki 
100223d5855fSRafael J. Wysocki static void acpi_s2idle_sync(void)
100333e4f80eSRafael J. Wysocki {
100433e4f80eSRafael J. Wysocki 	/*
100533e4f80eSRafael J. Wysocki 	 * Process all pending events in case there are any wakeup ones.
100633e4f80eSRafael J. Wysocki 	 *
1007880a6627SRafael J. Wysocki 	 * The EC driver uses the system workqueue and an additional special
1008880a6627SRafael J. Wysocki 	 * one, so those need to be flushed too.
100933e4f80eSRafael J. Wysocki 	 */
10106f1d7c45SChris Chiu 	acpi_os_wait_events_complete();	/* synchronize SCI IRQ handling */
1011880a6627SRafael J. Wysocki 	acpi_ec_flush_work();
10126f1d7c45SChris Chiu 	acpi_os_wait_events_complete();	/* synchronize Notify handling */
101333e4f80eSRafael J. Wysocki 	s2idle_wakeup = false;
101433e4f80eSRafael J. Wysocki }
101533e4f80eSRafael J. Wysocki 
101623d5855fSRafael J. Wysocki static void acpi_s2idle_restore(void)
1017a8d46b9eSRafael J. Wysocki {
10188c01275eSChen Yu 	if (acpi_sci_irq_valid())
10198c01275eSChen Yu 		disable_irq_wake(acpi_sci_irq);
1020235d81a6SRafael J. Wysocki 
10218110dd28SRafael J. Wysocki 	if (lps0_device_handle) {
10228110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT);
10238110dd28SRafael J. Wysocki 		acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON);
1024a8d46b9eSRafael J. Wysocki 	}
10258110dd28SRafael J. Wysocki }
1026a8d46b9eSRafael J. Wysocki 
102723d5855fSRafael J. Wysocki static void acpi_s2idle_end(void)
10281f0b6386SRafael J. Wysocki {
10298110dd28SRafael J. Wysocki 	s2idle_in_progress = false;
10301f0b6386SRafael J. Wysocki 	acpi_scan_lock_release();
10311f0b6386SRafael J. Wysocki }
10321f0b6386SRafael J. Wysocki 
103323d5855fSRafael J. Wysocki static const struct platform_s2idle_ops acpi_s2idle_ops = {
103423d5855fSRafael J. Wysocki 	.begin = acpi_s2idle_begin,
103523d5855fSRafael J. Wysocki 	.prepare = acpi_s2idle_prepare,
103623d5855fSRafael J. Wysocki 	.wake = acpi_s2idle_wake,
103723d5855fSRafael J. Wysocki 	.sync = acpi_s2idle_sync,
103823d5855fSRafael J. Wysocki 	.restore = acpi_s2idle_restore,
103923d5855fSRafael J. Wysocki 	.end = acpi_s2idle_end,
10401f0b6386SRafael J. Wysocki };
10411f0b6386SRafael J. Wysocki 
104202040f0bSRafael J. Wysocki static void acpi_sleep_suspend_setup(void)
104302040f0bSRafael J. Wysocki {
104402040f0bSRafael J. Wysocki 	int i;
104502040f0bSRafael J. Wysocki 
1046a4e90bedSRafael J. Wysocki 	for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
1047a4e90bedSRafael J. Wysocki 		if (acpi_sleep_state_supported(i))
104802040f0bSRafael J. Wysocki 			sleep_states[i] = 1;
104902040f0bSRafael J. Wysocki 
105002040f0bSRafael J. Wysocki 	suspend_set_ops(old_suspend_ordering ?
105102040f0bSRafael J. Wysocki 		&acpi_suspend_ops_old : &acpi_suspend_ops);
10528110dd28SRafael J. Wysocki 
10538110dd28SRafael J. Wysocki 	acpi_scan_add_handler(&lps0_handler);
105423d5855fSRafael J. Wysocki 	s2idle_set_ops(&acpi_s2idle_ops);
105502040f0bSRafael J. Wysocki }
10561f0b6386SRafael J. Wysocki 
105702040f0bSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
10588110dd28SRafael J. Wysocki #define s2idle_in_progress	(false)
105933e4f80eSRafael J. Wysocki #define s2idle_wakeup		(false)
10608110dd28SRafael J. Wysocki #define lps0_device_handle	(NULL)
106102040f0bSRafael J. Wysocki static inline void acpi_sleep_suspend_setup(void) {}
106202040f0bSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
1063d08ca2caSLen Brown 
106433e4f80eSRafael J. Wysocki bool acpi_s2idle_wakeup(void)
106533e4f80eSRafael J. Wysocki {
106633e4f80eSRafael J. Wysocki 	return s2idle_wakeup;
106733e4f80eSRafael J. Wysocki }
106833e4f80eSRafael J. Wysocki 
10698110dd28SRafael J. Wysocki bool acpi_sleep_no_ec_events(void)
10708110dd28SRafael J. Wysocki {
10718110dd28SRafael J. Wysocki 	return !s2idle_in_progress || !lps0_device_handle;
10728110dd28SRafael J. Wysocki }
10738110dd28SRafael J. Wysocki 
1074504997cfSSudeep Holla #ifdef CONFIG_PM_SLEEP
1075504997cfSSudeep Holla static u32 saved_bm_rld;
1076504997cfSSudeep Holla 
1077504997cfSSudeep Holla static int  acpi_save_bm_rld(void)
1078504997cfSSudeep Holla {
1079504997cfSSudeep Holla 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
1080504997cfSSudeep Holla 	return 0;
1081504997cfSSudeep Holla }
1082504997cfSSudeep Holla 
1083504997cfSSudeep Holla static void  acpi_restore_bm_rld(void)
1084504997cfSSudeep Holla {
1085504997cfSSudeep Holla 	u32 resumed_bm_rld = 0;
1086504997cfSSudeep Holla 
1087504997cfSSudeep Holla 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
1088504997cfSSudeep Holla 	if (resumed_bm_rld == saved_bm_rld)
1089504997cfSSudeep Holla 		return;
1090504997cfSSudeep Holla 
1091504997cfSSudeep Holla 	acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
1092504997cfSSudeep Holla }
1093504997cfSSudeep Holla 
1094504997cfSSudeep Holla static struct syscore_ops acpi_sleep_syscore_ops = {
1095504997cfSSudeep Holla 	.suspend = acpi_save_bm_rld,
1096504997cfSSudeep Holla 	.resume = acpi_restore_bm_rld,
1097504997cfSSudeep Holla };
1098504997cfSSudeep Holla 
1099a042e0c6SJean Delvare static void acpi_sleep_syscore_init(void)
1100504997cfSSudeep Holla {
1101504997cfSSudeep Holla 	register_syscore_ops(&acpi_sleep_syscore_ops);
1102504997cfSSudeep Holla }
1103504997cfSSudeep Holla #else
1104504997cfSSudeep Holla static inline void acpi_sleep_syscore_init(void) {}
1105504997cfSSudeep Holla #endif /* CONFIG_PM_SLEEP */
1106504997cfSSudeep Holla 
1107d08ca2caSLen Brown #ifdef CONFIG_HIBERNATION
1108d08ca2caSLen Brown static unsigned long s4_hardware_signature;
1109d08ca2caSLen Brown static struct acpi_table_facs *facs;
1110d08ca2caSLen Brown static bool nosigcheck;
1111d08ca2caSLen Brown 
1112d08ca2caSLen Brown void __init acpi_no_s4_hw_signature(void)
1113d08ca2caSLen Brown {
1114d08ca2caSLen Brown 	nosigcheck = true;
1115d08ca2caSLen Brown }
1116d08ca2caSLen Brown 
1117d08ca2caSLen Brown static int acpi_hibernation_begin(void)
1118d08ca2caSLen Brown {
1119d08ca2caSLen Brown 	int error;
1120d08ca2caSLen Brown 
112172ad5d77SRafael J. Wysocki 	error = nvs_nosave ? 0 : suspend_nvs_alloc();
1122ad07277eSRafael J. Wysocki 	if (!error)
1123ad07277eSRafael J. Wysocki 		acpi_pm_start(ACPI_STATE_S4);
1124d08ca2caSLen Brown 
1125d08ca2caSLen Brown 	return error;
1126d08ca2caSLen Brown }
1127d08ca2caSLen Brown 
1128d08ca2caSLen Brown static int acpi_hibernation_enter(void)
1129d08ca2caSLen Brown {
1130d08ca2caSLen Brown 	acpi_status status = AE_OK;
1131d08ca2caSLen Brown 
1132d08ca2caSLen Brown 	ACPI_FLUSH_CPU_CACHE();
1133d08ca2caSLen Brown 
1134d08ca2caSLen Brown 	/* This shouldn't return.  If it returns, we have a problem */
11353f6f49c7SLen Brown 	status = acpi_enter_sleep_state(ACPI_STATE_S4);
11363f6f49c7SLen Brown 	/* Reprogram control registers */
11373f6f49c7SLen Brown 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1138d08ca2caSLen Brown 
1139d08ca2caSLen Brown 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
1140d08ca2caSLen Brown }
1141d08ca2caSLen Brown 
1142d08ca2caSLen Brown static void acpi_hibernation_leave(void)
1143d08ca2caSLen Brown {
1144fbda4b38SLukas Wunner 	pm_set_resume_via_firmware();
1145d08ca2caSLen Brown 	/*
1146d08ca2caSLen Brown 	 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
1147d08ca2caSLen Brown 	 * enable it here.
1148d08ca2caSLen Brown 	 */
1149d08ca2caSLen Brown 	acpi_enable();
11503f6f49c7SLen Brown 	/* Reprogram control registers */
11513f6f49c7SLen Brown 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1152d08ca2caSLen Brown 	/* Check the hardware signature */
11535c551e62SOliver Neukum 	if (facs && s4_hardware_signature != facs->hardware_signature)
11545c551e62SOliver Neukum 		pr_crit("ACPI: Hardware changed while hibernated, success doubtful!\n");
1155d08ca2caSLen Brown 	/* Restore the NVS memory area */
1156dd4c4f17SMatthew Garrett 	suspend_nvs_restore();
1157d5a64513SRafael J. Wysocki 	/* Allow EC transactions to happen. */
1158c2b46d67SLv Zheng 	acpi_ec_unblock_transactions();
1159d08ca2caSLen Brown }
1160d08ca2caSLen Brown 
1161d5a64513SRafael J. Wysocki static void acpi_pm_thaw(void)
1162f6bb13aaSRafael J. Wysocki {
1163fe955682SRafael J. Wysocki 	acpi_ec_unblock_transactions();
1164d08ca2caSLen Brown 	acpi_enable_all_runtime_gpes();
1165d08ca2caSLen Brown }
1166d08ca2caSLen Brown 
1167073ef1f6SLionel Debroux static const struct platform_hibernation_ops acpi_hibernation_ops = {
1168d08ca2caSLen Brown 	.begin = acpi_hibernation_begin,
1169d08ca2caSLen Brown 	.end = acpi_pm_end,
1170c5f7a1bbSRafael J. Wysocki 	.pre_snapshot = acpi_pm_prepare,
11712a6b6976SMatthew Garrett 	.finish = acpi_pm_finish,
1172d08ca2caSLen Brown 	.prepare = acpi_pm_prepare,
1173d08ca2caSLen Brown 	.enter = acpi_hibernation_enter,
1174d08ca2caSLen Brown 	.leave = acpi_hibernation_leave,
1175d5a64513SRafael J. Wysocki 	.pre_restore = acpi_pm_freeze,
1176d5a64513SRafael J. Wysocki 	.restore_cleanup = acpi_pm_thaw,
1177d08ca2caSLen Brown };
1178d08ca2caSLen Brown 
1179d08ca2caSLen Brown /**
1180d08ca2caSLen Brown  *	acpi_hibernation_begin_old - Set the target system sleep state to
1181d08ca2caSLen Brown  *		ACPI_STATE_S4 and execute the _PTS control method.  This
1182d08ca2caSLen Brown  *		function is used if the pre-ACPI 2.0 suspend ordering has been
1183d08ca2caSLen Brown  *		requested.
1184d08ca2caSLen Brown  */
1185d08ca2caSLen Brown static int acpi_hibernation_begin_old(void)
1186d08ca2caSLen Brown {
1187d08ca2caSLen Brown 	int error;
1188d08ca2caSLen Brown 	/*
1189d08ca2caSLen Brown 	 * The _TTS object should always be evaluated before the _PTS object.
1190d08ca2caSLen Brown 	 * When the old_suspended_ordering is true, the _PTS object is
1191d08ca2caSLen Brown 	 * evaluated in the acpi_sleep_prepare.
1192d08ca2caSLen Brown 	 */
1193d08ca2caSLen Brown 	acpi_sleep_tts_switch(ACPI_STATE_S4);
1194d08ca2caSLen Brown 
1195d08ca2caSLen Brown 	error = acpi_sleep_prepare(ACPI_STATE_S4);
1196d08ca2caSLen Brown 
1197d08ca2caSLen Brown 	if (!error) {
119872ad5d77SRafael J. Wysocki 		if (!nvs_nosave)
1199dd4c4f17SMatthew Garrett 			error = suspend_nvs_alloc();
1200ad07277eSRafael J. Wysocki 		if (!error) {
1201d08ca2caSLen Brown 			acpi_target_sleep_state = ACPI_STATE_S4;
1202ad07277eSRafael J. Wysocki 			acpi_scan_lock_acquire();
1203ad07277eSRafael J. Wysocki 		}
1204d08ca2caSLen Brown 	}
1205d08ca2caSLen Brown 	return error;
1206d08ca2caSLen Brown }
1207d08ca2caSLen Brown 
1208d08ca2caSLen Brown /*
1209d08ca2caSLen Brown  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
1210d08ca2caSLen Brown  * been requested.
1211d08ca2caSLen Brown  */
1212073ef1f6SLionel Debroux static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
1213d08ca2caSLen Brown 	.begin = acpi_hibernation_begin_old,
1214d08ca2caSLen Brown 	.end = acpi_pm_end,
1215c5f7a1bbSRafael J. Wysocki 	.pre_snapshot = acpi_pm_pre_suspend,
1216d5a64513SRafael J. Wysocki 	.prepare = acpi_pm_freeze,
12172a6b6976SMatthew Garrett 	.finish = acpi_pm_finish,
1218d08ca2caSLen Brown 	.enter = acpi_hibernation_enter,
1219d08ca2caSLen Brown 	.leave = acpi_hibernation_leave,
1220d5a64513SRafael J. Wysocki 	.pre_restore = acpi_pm_freeze,
1221d5a64513SRafael J. Wysocki 	.restore_cleanup = acpi_pm_thaw,
1222d08ca2caSLen Brown 	.recover = acpi_pm_finish,
1223d08ca2caSLen Brown };
122402040f0bSRafael J. Wysocki 
122502040f0bSRafael J. Wysocki static void acpi_sleep_hibernate_setup(void)
122602040f0bSRafael J. Wysocki {
1227a4e90bedSRafael J. Wysocki 	if (!acpi_sleep_state_supported(ACPI_STATE_S4))
122802040f0bSRafael J. Wysocki 		return;
122902040f0bSRafael J. Wysocki 
123002040f0bSRafael J. Wysocki 	hibernation_set_ops(old_suspend_ordering ?
123102040f0bSRafael J. Wysocki 			&acpi_hibernation_ops_old : &acpi_hibernation_ops);
123202040f0bSRafael J. Wysocki 	sleep_states[ACPI_STATE_S4] = 1;
123302040f0bSRafael J. Wysocki 	if (nosigcheck)
123402040f0bSRafael J. Wysocki 		return;
123502040f0bSRafael J. Wysocki 
123602040f0bSRafael J. Wysocki 	acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
123702040f0bSRafael J. Wysocki 	if (facs)
123802040f0bSRafael J. Wysocki 		s4_hardware_signature = facs->hardware_signature;
123902040f0bSRafael J. Wysocki }
124002040f0bSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */
124102040f0bSRafael J. Wysocki static inline void acpi_sleep_hibernate_setup(void) {}
124202040f0bSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */
1243d08ca2caSLen Brown 
1244d08ca2caSLen Brown static void acpi_power_off_prepare(void)
1245d08ca2caSLen Brown {
1246d08ca2caSLen Brown 	/* Prepare to power off the system */
1247d08ca2caSLen Brown 	acpi_sleep_prepare(ACPI_STATE_S5);
1248d08ca2caSLen Brown 	acpi_disable_all_gpes();
1249c52fa70cSRafael J. Wysocki 	acpi_os_wait_events_complete();
1250d08ca2caSLen Brown }
1251d08ca2caSLen Brown 
1252d08ca2caSLen Brown static void acpi_power_off(void)
1253d08ca2caSLen Brown {
1254d08ca2caSLen Brown 	/* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
12554d939155SFrank Seidel 	printk(KERN_DEBUG "%s called\n", __func__);
1256d08ca2caSLen Brown 	local_irq_disable();
12573f6f49c7SLen Brown 	acpi_enter_sleep_state(ACPI_STATE_S5);
125896f15efcSLen Brown }
125996f15efcSLen Brown 
1260d08ca2caSLen Brown int __init acpi_sleep_init(void)
1261d08ca2caSLen Brown {
1262ed4cf5b2SJoe Perches 	char supported[ACPI_S_STATE_COUNT * 3 + 1];
1263ed4cf5b2SJoe Perches 	char *pos = supported;
1264ed4cf5b2SJoe Perches 	int i;
1265d08ca2caSLen Brown 
12660ac1b1d7SZhang Rui 	acpi_sleep_dmi_check();
12670ac1b1d7SZhang Rui 
1268d08ca2caSLen Brown 	sleep_states[ACPI_STATE_S0] = 1;
1269d08ca2caSLen Brown 
1270504997cfSSudeep Holla 	acpi_sleep_syscore_init();
127102040f0bSRafael J. Wysocki 	acpi_sleep_suspend_setup();
127202040f0bSRafael J. Wysocki 	acpi_sleep_hibernate_setup();
1273d08ca2caSLen Brown 
1274a4e90bedSRafael J. Wysocki 	if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
1275d08ca2caSLen Brown 		sleep_states[ACPI_STATE_S5] = 1;
1276d08ca2caSLen Brown 		pm_power_off_prepare = acpi_power_off_prepare;
1277d08ca2caSLen Brown 		pm_power_off = acpi_power_off;
127813737181SChen Yu 	} else {
127913737181SChen Yu 		acpi_no_s5 = true;
1280d08ca2caSLen Brown 	}
1281ed4cf5b2SJoe Perches 
1282ed4cf5b2SJoe Perches 	supported[0] = 0;
1283ed4cf5b2SJoe Perches 	for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
1284ed4cf5b2SJoe Perches 		if (sleep_states[i])
1285ed4cf5b2SJoe Perches 			pos += sprintf(pos, " S%d", i);
1286ed4cf5b2SJoe Perches 	}
1287ed4cf5b2SJoe Perches 	pr_info(PREFIX "(supports%s)\n", supported);
1288ed4cf5b2SJoe Perches 
1289d08ca2caSLen Brown 	/*
12909713adc2SRafael J. Wysocki 	 * Register the tts_notifier to reboot notifier list so that the _TTS
12919713adc2SRafael J. Wysocki 	 * object can also be evaluated when the system enters S5.
1292d08ca2caSLen Brown 	 */
12939713adc2SRafael J. Wysocki 	register_reboot_notifier(&tts_notifier);
1294d08ca2caSLen Brown 	return 0;
1295d08ca2caSLen Brown }
1296