• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sleep.c - ACPI sleep support.
4  *
5  * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
6  * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
7  * Copyright (c) 2000-2003 Patrick Mochel
8  * Copyright (c) 2003 Open Source Development Lab
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/irq.h>
13 #include <linux/dmi.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/suspend.h>
17 #include <linux/reboot.h>
18 #include <linux/acpi.h>
19 #include <linux/module.h>
20 #include <linux/syscore_ops.h>
21 #include <asm/io.h>
22 #include <trace/events/power.h>
23 
24 #include "internal.h"
25 #include "sleep.h"
26 
27 /*
28  * Some HW-full platforms do not have _S5, so they may need
29  * to leverage efi power off for a shutdown.
30  */
31 bool acpi_no_s5;
32 static u8 sleep_states[ACPI_S_STATE_COUNT];
33 
acpi_sleep_tts_switch(u32 acpi_state)34 static void acpi_sleep_tts_switch(u32 acpi_state)
35 {
36 	acpi_status status;
37 
38 	status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
39 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
40 		/*
41 		 * OS can't evaluate the _TTS object correctly. Some warning
42 		 * message will be printed. But it won't break anything.
43 		 */
44 		printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
45 	}
46 }
47 
tts_notify_reboot(struct notifier_block * this,unsigned long code,void * x)48 static int tts_notify_reboot(struct notifier_block *this,
49 			unsigned long code, void *x)
50 {
51 	acpi_sleep_tts_switch(ACPI_STATE_S5);
52 	return NOTIFY_DONE;
53 }
54 
55 static struct notifier_block tts_notifier = {
56 	.notifier_call	= tts_notify_reboot,
57 	.next		= NULL,
58 	.priority	= 0,
59 };
60 
acpi_sleep_prepare(u32 acpi_state)61 static int acpi_sleep_prepare(u32 acpi_state)
62 {
63 #ifdef CONFIG_ACPI_SLEEP
64 	/* do we have a wakeup address for S2 and S3? */
65 	if (acpi_state == ACPI_STATE_S3) {
66 		if (!acpi_wakeup_address)
67 			return -EFAULT;
68 		acpi_set_waking_vector(acpi_wakeup_address);
69 
70 	}
71 	ACPI_FLUSH_CPU_CACHE();
72 #endif
73 	printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
74 		acpi_state);
75 	acpi_enable_wakeup_devices(acpi_state);
76 	acpi_enter_sleep_state_prep(acpi_state);
77 	return 0;
78 }
79 
acpi_sleep_state_supported(u8 sleep_state)80 bool acpi_sleep_state_supported(u8 sleep_state)
81 {
82 	acpi_status status;
83 	u8 type_a, type_b;
84 
85 	status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
86 	return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
87 		|| (acpi_gbl_FADT.sleep_control.address
88 			&& acpi_gbl_FADT.sleep_status.address));
89 }
90 
91 #ifdef CONFIG_ACPI_SLEEP
92 static bool sleep_no_lps0 __read_mostly;
93 module_param(sleep_no_lps0, bool, 0644);
94 MODULE_PARM_DESC(sleep_no_lps0, "Do not use the special LPS0 device interface");
95 
96 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
97 
acpi_target_system_state(void)98 u32 acpi_target_system_state(void)
99 {
100 	return acpi_target_sleep_state;
101 }
102 EXPORT_SYMBOL_GPL(acpi_target_system_state);
103 
104 static bool pwr_btn_event_pending;
105 
106 /*
107  * The ACPI specification wants us to save NVS memory regions during hibernation
108  * and to restore them during the subsequent resume.  Windows does that also for
109  * suspend to RAM.  However, it is known that this mechanism does not work on
110  * all machines, so we allow the user to disable it with the help of the
111  * 'acpi_sleep=nonvs' kernel command line option.
112  */
113 static bool nvs_nosave;
114 
acpi_nvs_nosave(void)115 void __init acpi_nvs_nosave(void)
116 {
117 	nvs_nosave = true;
118 }
119 
120 /*
121  * The ACPI specification wants us to save NVS memory regions during hibernation
122  * but says nothing about saving NVS during S3.  Not all versions of Windows
123  * save NVS on S3 suspend either, and it is clear that not all systems need
124  * NVS to be saved at S3 time.  To improve suspend/resume time, allow the
125  * user to disable saving NVS on S3 if their system does not require it, but
126  * continue to save/restore NVS for S4 as specified.
127  */
128 static bool nvs_nosave_s3;
129 
acpi_nvs_nosave_s3(void)130 void __init acpi_nvs_nosave_s3(void)
131 {
132 	nvs_nosave_s3 = true;
133 }
134 
init_nvs_save_s3(const struct dmi_system_id * d)135 static int __init init_nvs_save_s3(const struct dmi_system_id *d)
136 {
137 	nvs_nosave_s3 = false;
138 	return 0;
139 }
140 
141 /*
142  * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
143  * user to request that behavior by using the 'acpi_old_suspend_ordering'
144  * kernel command line option that causes the following variable to be set.
145  */
146 static bool old_suspend_ordering;
147 
acpi_old_suspend_ordering(void)148 void __init acpi_old_suspend_ordering(void)
149 {
150 	old_suspend_ordering = true;
151 }
152 
init_old_suspend_ordering(const struct dmi_system_id * d)153 static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
154 {
155 	acpi_old_suspend_ordering();
156 	return 0;
157 }
158 
init_nvs_nosave(const struct dmi_system_id * d)159 static int __init init_nvs_nosave(const struct dmi_system_id *d)
160 {
161 	acpi_nvs_nosave();
162 	return 0;
163 }
164 
165 static bool acpi_sleep_default_s3;
166 
init_default_s3(const struct dmi_system_id * d)167 static int __init init_default_s3(const struct dmi_system_id *d)
168 {
169 	acpi_sleep_default_s3 = true;
170 	return 0;
171 }
172 
173 static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
174 	{
175 	.callback = init_old_suspend_ordering,
176 	.ident = "Abit KN9 (nForce4 variant)",
177 	.matches = {
178 		DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
179 		DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
180 		},
181 	},
182 	{
183 	.callback = init_old_suspend_ordering,
184 	.ident = "HP xw4600 Workstation",
185 	.matches = {
186 		DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
187 		DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
188 		},
189 	},
190 	{
191 	.callback = init_old_suspend_ordering,
192 	.ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
193 	.matches = {
194 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
195 		DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
196 		},
197 	},
198 	{
199 	.callback = init_old_suspend_ordering,
200 	.ident = "Panasonic CF51-2L",
201 	.matches = {
202 		DMI_MATCH(DMI_BOARD_VENDOR,
203 				"Matsushita Electric Industrial Co.,Ltd."),
204 		DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
205 		},
206 	},
207 	{
208 	.callback = init_nvs_nosave,
209 	.ident = "Sony Vaio VGN-FW41E_H",
210 	.matches = {
211 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
212 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
213 		},
214 	},
215 	{
216 	.callback = init_nvs_nosave,
217 	.ident = "Sony Vaio VGN-FW21E",
218 	.matches = {
219 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
220 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
221 		},
222 	},
223 	{
224 	.callback = init_nvs_nosave,
225 	.ident = "Sony Vaio VGN-FW21M",
226 	.matches = {
227 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
228 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
229 		},
230 	},
231 	{
232 	.callback = init_nvs_nosave,
233 	.ident = "Sony Vaio VPCEB17FX",
234 	.matches = {
235 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
236 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
237 		},
238 	},
239 	{
240 	.callback = init_nvs_nosave,
241 	.ident = "Sony Vaio VGN-SR11M",
242 	.matches = {
243 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
244 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
245 		},
246 	},
247 	{
248 	.callback = init_nvs_nosave,
249 	.ident = "Everex StepNote Series",
250 	.matches = {
251 		DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
252 		DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
253 		},
254 	},
255 	{
256 	.callback = init_nvs_nosave,
257 	.ident = "Sony Vaio VPCEB1Z1E",
258 	.matches = {
259 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
260 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
261 		},
262 	},
263 	{
264 	.callback = init_nvs_nosave,
265 	.ident = "Sony Vaio VGN-NW130D",
266 	.matches = {
267 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
268 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
269 		},
270 	},
271 	{
272 	.callback = init_nvs_nosave,
273 	.ident = "Sony Vaio VPCCW29FX",
274 	.matches = {
275 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
276 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
277 		},
278 	},
279 	{
280 	.callback = init_nvs_nosave,
281 	.ident = "Averatec AV1020-ED2",
282 	.matches = {
283 		DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
284 		DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
285 		},
286 	},
287 	{
288 	.callback = init_old_suspend_ordering,
289 	.ident = "Asus A8N-SLI DELUXE",
290 	.matches = {
291 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
292 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
293 		},
294 	},
295 	{
296 	.callback = init_old_suspend_ordering,
297 	.ident = "Asus A8N-SLI Premium",
298 	.matches = {
299 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
300 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
301 		},
302 	},
303 	{
304 	.callback = init_nvs_nosave,
305 	.ident = "Sony Vaio VGN-SR26GN_P",
306 	.matches = {
307 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
308 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
309 		},
310 	},
311 	{
312 	.callback = init_nvs_nosave,
313 	.ident = "Sony Vaio VPCEB1S1E",
314 	.matches = {
315 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
316 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
317 		},
318 	},
319 	{
320 	.callback = init_nvs_nosave,
321 	.ident = "Sony Vaio VGN-FW520F",
322 	.matches = {
323 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
324 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
325 		},
326 	},
327 	{
328 	.callback = init_nvs_nosave,
329 	.ident = "Asus K54C",
330 	.matches = {
331 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
332 		DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
333 		},
334 	},
335 	{
336 	.callback = init_nvs_nosave,
337 	.ident = "Asus K54HR",
338 	.matches = {
339 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
340 		DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
341 		},
342 	},
343 	{
344 	.callback = init_nvs_save_s3,
345 	.ident = "Asus 1025C",
346 	.matches = {
347 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
348 		DMI_MATCH(DMI_PRODUCT_NAME, "1025C"),
349 		},
350 	},
351 	/*
352 	 * https://bugzilla.kernel.org/show_bug.cgi?id=189431
353 	 * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
354 	 * saving during S3.
355 	 */
356 	{
357 	.callback = init_nvs_save_s3,
358 	.ident = "Lenovo G50-45",
359 	.matches = {
360 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
361 		DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
362 		},
363 	},
364 	{
365 	.callback = init_nvs_save_s3,
366 	.ident = "Lenovo G40-45",
367 	.matches = {
368 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
369 		DMI_MATCH(DMI_PRODUCT_NAME, "80E1"),
370 		},
371 	},
372 	/*
373 	 * ThinkPad X1 Tablet(2016) cannot do suspend-to-idle using
374 	 * the Low Power S0 Idle firmware interface (see
375 	 * https://bugzilla.kernel.org/show_bug.cgi?id=199057).
376 	 */
377 	{
378 	.callback = init_default_s3,
379 	.ident = "ThinkPad X1 Tablet(2016)",
380 	.matches = {
381 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
382 		DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
383 		},
384 	},
385 	{},
386 };
387 
388 static bool ignore_blacklist;
389 
acpi_sleep_no_blacklist(void)390 void __init acpi_sleep_no_blacklist(void)
391 {
392 	ignore_blacklist = true;
393 }
394 
acpi_sleep_dmi_check(void)395 static void __init acpi_sleep_dmi_check(void)
396 {
397 	if (ignore_blacklist)
398 		return;
399 
400 	if (dmi_get_bios_year() >= 2012)
401 		acpi_nvs_nosave_s3();
402 
403 	dmi_check_system(acpisleep_dmi_table);
404 }
405 
406 /**
407  * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
408  */
acpi_pm_freeze(void)409 static int acpi_pm_freeze(void)
410 {
411 	acpi_disable_all_gpes();
412 	acpi_os_wait_events_complete();
413 	acpi_ec_block_transactions();
414 	return 0;
415 }
416 
417 /**
418  * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
419  */
acpi_pm_pre_suspend(void)420 static int acpi_pm_pre_suspend(void)
421 {
422 	acpi_pm_freeze();
423 	return suspend_nvs_save();
424 }
425 
426 /**
427  *	__acpi_pm_prepare - Prepare the platform to enter the target state.
428  *
429  *	If necessary, set the firmware waking vector and do arch-specific
430  *	nastiness to get the wakeup code to the waking vector.
431  */
__acpi_pm_prepare(void)432 static int __acpi_pm_prepare(void)
433 {
434 	int error = acpi_sleep_prepare(acpi_target_sleep_state);
435 	if (error)
436 		acpi_target_sleep_state = ACPI_STATE_S0;
437 
438 	return error;
439 }
440 
441 /**
442  *	acpi_pm_prepare - Prepare the platform to enter the target sleep
443  *		state and disable the GPEs.
444  */
acpi_pm_prepare(void)445 static int acpi_pm_prepare(void)
446 {
447 	int error = __acpi_pm_prepare();
448 	if (!error)
449 		error = acpi_pm_pre_suspend();
450 
451 	return error;
452 }
453 
454 /**
455  *	acpi_pm_finish - Instruct the platform to leave a sleep state.
456  *
457  *	This is called after we wake back up (or if entering the sleep state
458  *	failed).
459  */
acpi_pm_finish(void)460 static void acpi_pm_finish(void)
461 {
462 	struct acpi_device *pwr_btn_adev;
463 	u32 acpi_state = acpi_target_sleep_state;
464 
465 	acpi_ec_unblock_transactions();
466 	suspend_nvs_free();
467 
468 	if (acpi_state == ACPI_STATE_S0)
469 		return;
470 
471 	printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
472 		acpi_state);
473 	acpi_disable_wakeup_devices(acpi_state);
474 	acpi_leave_sleep_state(acpi_state);
475 
476 	/* reset firmware waking vector */
477 	acpi_set_waking_vector(0);
478 
479 	acpi_target_sleep_state = ACPI_STATE_S0;
480 
481 	acpi_resume_power_resources();
482 
483 	/* If we were woken with the fixed power button, provide a small
484 	 * hint to userspace in the form of a wakeup event on the fixed power
485 	 * button device (if it can be found).
486 	 *
487 	 * We delay the event generation til now, as the PM layer requires
488 	 * timekeeping to be running before we generate events. */
489 	if (!pwr_btn_event_pending)
490 		return;
491 
492 	pwr_btn_event_pending = false;
493 	pwr_btn_adev = acpi_dev_get_first_match_dev(ACPI_BUTTON_HID_POWERF,
494 						    NULL, -1);
495 	if (pwr_btn_adev) {
496 		pm_wakeup_event(&pwr_btn_adev->dev, 0);
497 		acpi_dev_put(pwr_btn_adev);
498 	}
499 }
500 
501 /**
502  * acpi_pm_start - Start system PM transition.
503  */
acpi_pm_start(u32 acpi_state)504 static void acpi_pm_start(u32 acpi_state)
505 {
506 	acpi_target_sleep_state = acpi_state;
507 	acpi_sleep_tts_switch(acpi_target_sleep_state);
508 	acpi_scan_lock_acquire();
509 }
510 
511 /**
512  * acpi_pm_end - Finish up system PM transition.
513  */
acpi_pm_end(void)514 static void acpi_pm_end(void)
515 {
516 	acpi_turn_off_unused_power_resources();
517 	acpi_scan_lock_release();
518 	/*
519 	 * This is necessary in case acpi_pm_finish() is not called during a
520 	 * failing transition to a sleep state.
521 	 */
522 	acpi_target_sleep_state = ACPI_STATE_S0;
523 	acpi_sleep_tts_switch(acpi_target_sleep_state);
524 }
525 #else /* !CONFIG_ACPI_SLEEP */
526 #define sleep_no_lps0	(1)
527 #define acpi_target_sleep_state	ACPI_STATE_S0
528 #define acpi_sleep_default_s3	(1)
acpi_sleep_dmi_check(void)529 static inline void acpi_sleep_dmi_check(void) {}
530 #endif /* CONFIG_ACPI_SLEEP */
531 
532 #ifdef CONFIG_SUSPEND
533 static u32 acpi_suspend_states[] = {
534 	[PM_SUSPEND_ON] = ACPI_STATE_S0,
535 	[PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
536 	[PM_SUSPEND_MEM] = ACPI_STATE_S3,
537 	[PM_SUSPEND_MAX] = ACPI_STATE_S5
538 };
539 
540 /**
541  *	acpi_suspend_begin - Set the target system sleep state to the state
542  *		associated with given @pm_state, if supported.
543  */
acpi_suspend_begin(suspend_state_t pm_state)544 static int acpi_suspend_begin(suspend_state_t pm_state)
545 {
546 	u32 acpi_state = acpi_suspend_states[pm_state];
547 	int error;
548 
549 	error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
550 	if (error)
551 		return error;
552 
553 	if (!sleep_states[acpi_state]) {
554 		pr_err("ACPI does not support sleep state S%u\n", acpi_state);
555 		return -ENOSYS;
556 	}
557 	if (acpi_state > ACPI_STATE_S1)
558 		pm_set_suspend_via_firmware();
559 
560 	acpi_pm_start(acpi_state);
561 	return 0;
562 }
563 
564 /**
565  *	acpi_suspend_enter - Actually enter a sleep state.
566  *	@pm_state: ignored
567  *
568  *	Flush caches and go to sleep. For STR we have to call arch-specific
569  *	assembly, which in turn call acpi_enter_sleep_state().
570  *	It's unfortunate, but it works. Please fix if you're feeling frisky.
571  */
acpi_suspend_enter(suspend_state_t pm_state)572 static int acpi_suspend_enter(suspend_state_t pm_state)
573 {
574 	acpi_status status = AE_OK;
575 	u32 acpi_state = acpi_target_sleep_state;
576 	int error;
577 
578 	ACPI_FLUSH_CPU_CACHE();
579 
580 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
581 	switch (acpi_state) {
582 	case ACPI_STATE_S1:
583 		barrier();
584 		status = acpi_enter_sleep_state(acpi_state);
585 		break;
586 
587 	case ACPI_STATE_S3:
588 		if (!acpi_suspend_lowlevel)
589 			return -ENOSYS;
590 		error = acpi_suspend_lowlevel();
591 		if (error)
592 			return error;
593 		pr_info(PREFIX "Low-level resume complete\n");
594 		pm_set_resume_via_firmware();
595 		break;
596 	}
597 	trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
598 
599 	/* This violates the spec but is required for bug compatibility. */
600 	acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
601 
602 	/* Reprogram control registers */
603 	acpi_leave_sleep_state_prep(acpi_state);
604 
605 	/* ACPI 3.0 specs (P62) says that it's the responsibility
606 	 * of the OSPM to clear the status bit [ implying that the
607 	 * POWER_BUTTON event should not reach userspace ]
608 	 *
609 	 * However, we do generate a small hint for userspace in the form of
610 	 * a wakeup event. We flag this condition for now and generate the
611 	 * event later, as we're currently too early in resume to be able to
612 	 * generate wakeup events.
613 	 */
614 	if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
615 		acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
616 
617 		acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
618 
619 		if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
620 			acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
621 			/* Flag for later */
622 			pwr_btn_event_pending = true;
623 		}
624 	}
625 
626 	/*
627 	 * Disable and clear GPE status before interrupt is enabled. Some GPEs
628 	 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
629 	 * acpi_leave_sleep_state will reenable specific GPEs later
630 	 */
631 	acpi_disable_all_gpes();
632 	/* Allow EC transactions to happen. */
633 	acpi_ec_unblock_transactions();
634 
635 	suspend_nvs_restore();
636 
637 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
638 }
639 
acpi_suspend_state_valid(suspend_state_t pm_state)640 static int acpi_suspend_state_valid(suspend_state_t pm_state)
641 {
642 	u32 acpi_state;
643 
644 	switch (pm_state) {
645 	case PM_SUSPEND_ON:
646 	case PM_SUSPEND_STANDBY:
647 	case PM_SUSPEND_MEM:
648 		acpi_state = acpi_suspend_states[pm_state];
649 
650 		return sleep_states[acpi_state];
651 	default:
652 		return 0;
653 	}
654 }
655 
656 static const struct platform_suspend_ops acpi_suspend_ops = {
657 	.valid = acpi_suspend_state_valid,
658 	.begin = acpi_suspend_begin,
659 	.prepare_late = acpi_pm_prepare,
660 	.enter = acpi_suspend_enter,
661 	.wake = acpi_pm_finish,
662 	.end = acpi_pm_end,
663 };
664 
665 /**
666  *	acpi_suspend_begin_old - Set the target system sleep state to the
667  *		state associated with given @pm_state, if supported, and
668  *		execute the _PTS control method.  This function is used if the
669  *		pre-ACPI 2.0 suspend ordering has been requested.
670  */
acpi_suspend_begin_old(suspend_state_t pm_state)671 static int acpi_suspend_begin_old(suspend_state_t pm_state)
672 {
673 	int error = acpi_suspend_begin(pm_state);
674 	if (!error)
675 		error = __acpi_pm_prepare();
676 
677 	return error;
678 }
679 
680 /*
681  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
682  * been requested.
683  */
684 static const struct platform_suspend_ops acpi_suspend_ops_old = {
685 	.valid = acpi_suspend_state_valid,
686 	.begin = acpi_suspend_begin_old,
687 	.prepare_late = acpi_pm_pre_suspend,
688 	.enter = acpi_suspend_enter,
689 	.wake = acpi_pm_finish,
690 	.end = acpi_pm_end,
691 	.recover = acpi_pm_finish,
692 };
693 
694 static bool s2idle_wakeup;
695 
696 /*
697  * On platforms supporting the Low Power S0 Idle interface there is an ACPI
698  * device object with the PNP0D80 compatible device ID (System Power Management
699  * Controller) and a specific _DSM method under it.  That method, if present,
700  * can be used to indicate to the platform that the OS is transitioning into a
701  * low-power state in which certain types of activity are not desirable or that
702  * it is leaving such a state, which allows the platform to adjust its operation
703  * mode accordingly.
704  */
705 static const struct acpi_device_id lps0_device_ids[] = {
706 	{"PNP0D80", },
707 	{"", },
708 };
709 
710 #define ACPI_LPS0_DSM_UUID	"c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
711 
712 #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS	1
713 #define ACPI_LPS0_SCREEN_OFF	3
714 #define ACPI_LPS0_SCREEN_ON	4
715 #define ACPI_LPS0_ENTRY		5
716 #define ACPI_LPS0_EXIT		6
717 
718 static acpi_handle lps0_device_handle;
719 static guid_t lps0_dsm_guid;
720 static char lps0_dsm_func_mask;
721 
722 /* Device constraint entry structure */
723 struct lpi_device_info {
724 	char *name;
725 	int enabled;
726 	union acpi_object *package;
727 };
728 
729 /* Constraint package structure */
730 struct lpi_device_constraint {
731 	int uid;
732 	int min_dstate;
733 	int function_states;
734 };
735 
736 struct lpi_constraints {
737 	acpi_handle handle;
738 	int min_dstate;
739 };
740 
741 static struct lpi_constraints *lpi_constraints_table;
742 static int lpi_constraints_table_size;
743 
lpi_device_get_constraints(void)744 static void lpi_device_get_constraints(void)
745 {
746 	union acpi_object *out_obj;
747 	int i;
748 
749 	out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
750 					  1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
751 					  NULL, ACPI_TYPE_PACKAGE);
752 
753 	acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
754 			  out_obj ? "successful" : "failed");
755 
756 	if (!out_obj)
757 		return;
758 
759 	lpi_constraints_table = kcalloc(out_obj->package.count,
760 					sizeof(*lpi_constraints_table),
761 					GFP_KERNEL);
762 	if (!lpi_constraints_table)
763 		goto free_acpi_buffer;
764 
765 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
766 
767 	for (i = 0; i < out_obj->package.count; i++) {
768 		struct lpi_constraints *constraint;
769 		acpi_status status;
770 		union acpi_object *package = &out_obj->package.elements[i];
771 		struct lpi_device_info info = { };
772 		int package_count = 0, j;
773 
774 		if (!package)
775 			continue;
776 
777 		for (j = 0; j < package->package.count; ++j) {
778 			union acpi_object *element =
779 					&(package->package.elements[j]);
780 
781 			switch (element->type) {
782 			case ACPI_TYPE_INTEGER:
783 				info.enabled = element->integer.value;
784 				break;
785 			case ACPI_TYPE_STRING:
786 				info.name = element->string.pointer;
787 				break;
788 			case ACPI_TYPE_PACKAGE:
789 				package_count = element->package.count;
790 				info.package = element->package.elements;
791 				break;
792 			}
793 		}
794 
795 		if (!info.enabled || !info.package || !info.name)
796 			continue;
797 
798 		constraint = &lpi_constraints_table[lpi_constraints_table_size];
799 
800 		status = acpi_get_handle(NULL, info.name, &constraint->handle);
801 		if (ACPI_FAILURE(status))
802 			continue;
803 
804 		acpi_handle_debug(lps0_device_handle,
805 				  "index:%d Name:%s\n", i, info.name);
806 
807 		constraint->min_dstate = -1;
808 
809 		for (j = 0; j < package_count; ++j) {
810 			union acpi_object *info_obj = &info.package[j];
811 			union acpi_object *cnstr_pkg;
812 			union acpi_object *obj;
813 			struct lpi_device_constraint dev_info;
814 
815 			switch (info_obj->type) {
816 			case ACPI_TYPE_INTEGER:
817 				/* version */
818 				break;
819 			case ACPI_TYPE_PACKAGE:
820 				if (info_obj->package.count < 2)
821 					break;
822 
823 				cnstr_pkg = info_obj->package.elements;
824 				obj = &cnstr_pkg[0];
825 				dev_info.uid = obj->integer.value;
826 				obj = &cnstr_pkg[1];
827 				dev_info.min_dstate = obj->integer.value;
828 
829 				acpi_handle_debug(lps0_device_handle,
830 					"uid:%d min_dstate:%s\n",
831 					dev_info.uid,
832 					acpi_power_state_string(dev_info.min_dstate));
833 
834 				constraint->min_dstate = dev_info.min_dstate;
835 				break;
836 			}
837 		}
838 
839 		if (constraint->min_dstate < 0) {
840 			acpi_handle_debug(lps0_device_handle,
841 					  "Incomplete constraint defined\n");
842 			continue;
843 		}
844 
845 		lpi_constraints_table_size++;
846 	}
847 
848 	acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
849 
850 free_acpi_buffer:
851 	ACPI_FREE(out_obj);
852 }
853 
lpi_check_constraints(void)854 static void lpi_check_constraints(void)
855 {
856 	int i;
857 
858 	for (i = 0; i < lpi_constraints_table_size; ++i) {
859 		acpi_handle handle = lpi_constraints_table[i].handle;
860 		struct acpi_device *adev;
861 
862 		if (!handle || acpi_bus_get_device(handle, &adev))
863 			continue;
864 
865 		acpi_handle_debug(handle,
866 			"LPI: required min power state:%s current power state:%s\n",
867 			acpi_power_state_string(lpi_constraints_table[i].min_dstate),
868 			acpi_power_state_string(adev->power.state));
869 
870 		if (!adev->flags.power_manageable) {
871 			acpi_handle_info(handle, "LPI: Device not power manageable\n");
872 			lpi_constraints_table[i].handle = NULL;
873 			continue;
874 		}
875 
876 		if (adev->power.state < lpi_constraints_table[i].min_dstate)
877 			acpi_handle_info(handle,
878 				"LPI: Constraint not met; min power state:%s current power state:%s\n",
879 				acpi_power_state_string(lpi_constraints_table[i].min_dstate),
880 				acpi_power_state_string(adev->power.state));
881 	}
882 }
883 
acpi_sleep_run_lps0_dsm(unsigned int func)884 static void acpi_sleep_run_lps0_dsm(unsigned int func)
885 {
886 	union acpi_object *out_obj;
887 
888 	if (!(lps0_dsm_func_mask & (1 << func)))
889 		return;
890 
891 	out_obj = acpi_evaluate_dsm(lps0_device_handle, &lps0_dsm_guid, 1, func, NULL);
892 	ACPI_FREE(out_obj);
893 
894 	acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
895 			  func, out_obj ? "successful" : "failed");
896 }
897 
lps0_device_attach(struct acpi_device * adev,const struct acpi_device_id * not_used)898 static int lps0_device_attach(struct acpi_device *adev,
899 			      const struct acpi_device_id *not_used)
900 {
901 	union acpi_object *out_obj;
902 
903 	if (lps0_device_handle)
904 		return 0;
905 
906 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
907 		return 0;
908 
909 	guid_parse(ACPI_LPS0_DSM_UUID, &lps0_dsm_guid);
910 	/* Check if the _DSM is present and as expected. */
911 	out_obj = acpi_evaluate_dsm(adev->handle, &lps0_dsm_guid, 1, 0, NULL);
912 	if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER) {
913 		acpi_handle_debug(adev->handle,
914 				  "_DSM function 0 evaluation failed\n");
915 		return 0;
916 	}
917 
918 	lps0_dsm_func_mask = *(char *)out_obj->buffer.pointer;
919 
920 	ACPI_FREE(out_obj);
921 
922 	acpi_handle_debug(adev->handle, "_DSM function mask: 0x%x\n",
923 			  lps0_dsm_func_mask);
924 
925 	lps0_device_handle = adev->handle;
926 
927 	lpi_device_get_constraints();
928 
929 	/*
930 	 * Use suspend-to-idle by default if the default suspend mode was not
931 	 * set from the command line.
932 	 */
933 	if (mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3)
934 		mem_sleep_current = PM_SUSPEND_TO_IDLE;
935 
936 	/*
937 	 * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
938 	 * EC GPE to be enabled while suspended for certain wakeup devices to
939 	 * work, so mark it as wakeup-capable.
940 	 */
941 	acpi_ec_mark_gpe_for_wake();
942 
943 	return 0;
944 }
945 
946 static struct acpi_scan_handler lps0_handler = {
947 	.ids = lps0_device_ids,
948 	.attach = lps0_device_attach,
949 };
950 
acpi_s2idle_begin(void)951 static int acpi_s2idle_begin(void)
952 {
953 	acpi_scan_lock_acquire();
954 	return 0;
955 }
956 
acpi_s2idle_prepare(void)957 static int acpi_s2idle_prepare(void)
958 {
959 	if (acpi_sci_irq_valid()) {
960 		enable_irq_wake(acpi_sci_irq);
961 		acpi_ec_set_gpe_wake_mask(ACPI_GPE_ENABLE);
962 	}
963 
964 	acpi_enable_wakeup_devices(ACPI_STATE_S0);
965 
966 	/* Change the configuration of GPEs to avoid spurious wakeup. */
967 	acpi_enable_all_wakeup_gpes();
968 	acpi_os_wait_events_complete();
969 
970 	s2idle_wakeup = true;
971 	return 0;
972 }
973 
acpi_s2idle_prepare_late(void)974 static int acpi_s2idle_prepare_late(void)
975 {
976 	if (!lps0_device_handle || sleep_no_lps0)
977 		return 0;
978 
979 	if (pm_debug_messages_on)
980 		lpi_check_constraints();
981 
982 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF);
983 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY);
984 
985 	return 0;
986 }
987 
acpi_s2idle_wake(void)988 static bool acpi_s2idle_wake(void)
989 {
990 	if (!acpi_sci_irq_valid())
991 		return pm_wakeup_pending();
992 
993 	while (pm_wakeup_pending()) {
994 		/*
995 		 * If IRQD_WAKEUP_ARMED is set for the SCI at this point, the
996 		 * SCI has not triggered while suspended, so bail out (the
997 		 * wakeup is pending anyway and the SCI is not the source of
998 		 * it).
999 		 */
1000 		if (irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq)))
1001 			return true;
1002 
1003 		/*
1004 		 * If the status bit of any enabled fixed event is set, the
1005 		 * wakeup is regarded as valid.
1006 		 */
1007 		if (acpi_any_fixed_event_status_set())
1008 			return true;
1009 
1010 		/* Check wakeups from drivers sharing the SCI. */
1011 		if (acpi_check_wakeup_handlers())
1012 			return true;
1013 
1014 		/* Check non-EC GPE wakeups and dispatch the EC GPE. */
1015 		if (acpi_ec_dispatch_gpe())
1016 			return true;
1017 
1018 		/*
1019 		 * Cancel the SCI wakeup and process all pending events in case
1020 		 * there are any wakeup ones in there.
1021 		 *
1022 		 * Note that if any non-EC GPEs are active at this point, the
1023 		 * SCI will retrigger after the rearming below, so no events
1024 		 * should be missed by canceling the wakeup here.
1025 		 */
1026 		pm_system_cancel_wakeup();
1027 		acpi_os_wait_events_complete();
1028 
1029 		/*
1030 		 * The SCI is in the "suspended" state now and it cannot produce
1031 		 * new wakeup events till the rearming below, so if any of them
1032 		 * are pending here, they must be resulting from the processing
1033 		 * of EC events above or coming from somewhere else.
1034 		 */
1035 		if (pm_wakeup_pending())
1036 			return true;
1037 
1038 		pm_wakeup_clear(acpi_sci_irq);
1039 		rearm_wake_irq(acpi_sci_irq);
1040 	}
1041 
1042 	return false;
1043 }
1044 
acpi_s2idle_restore_early(void)1045 static void acpi_s2idle_restore_early(void)
1046 {
1047 	if (!lps0_device_handle || sleep_no_lps0)
1048 		return;
1049 
1050 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT);
1051 	acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON);
1052 }
1053 
acpi_s2idle_restore(void)1054 static void acpi_s2idle_restore(void)
1055 {
1056 	/*
1057 	 * Drain pending events before restoring the working-state configuration
1058 	 * of GPEs.
1059 	 */
1060 	acpi_os_wait_events_complete(); /* synchronize GPE processing */
1061 	acpi_ec_flush_work(); /* flush the EC driver's workqueues */
1062 	acpi_os_wait_events_complete(); /* synchronize Notify handling */
1063 
1064 	s2idle_wakeup = false;
1065 
1066 	acpi_enable_all_runtime_gpes();
1067 
1068 	acpi_disable_wakeup_devices(ACPI_STATE_S0);
1069 
1070 	if (acpi_sci_irq_valid()) {
1071 		acpi_ec_set_gpe_wake_mask(ACPI_GPE_DISABLE);
1072 		disable_irq_wake(acpi_sci_irq);
1073 	}
1074 }
1075 
acpi_s2idle_end(void)1076 static void acpi_s2idle_end(void)
1077 {
1078 	acpi_scan_lock_release();
1079 }
1080 
1081 static const struct platform_s2idle_ops acpi_s2idle_ops = {
1082 	.begin = acpi_s2idle_begin,
1083 	.prepare = acpi_s2idle_prepare,
1084 	.prepare_late = acpi_s2idle_prepare_late,
1085 	.wake = acpi_s2idle_wake,
1086 	.restore_early = acpi_s2idle_restore_early,
1087 	.restore = acpi_s2idle_restore,
1088 	.end = acpi_s2idle_end,
1089 };
1090 
acpi_sleep_suspend_setup(void)1091 static void acpi_sleep_suspend_setup(void)
1092 {
1093 	int i;
1094 
1095 	for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
1096 		if (acpi_sleep_state_supported(i))
1097 			sleep_states[i] = 1;
1098 
1099 	suspend_set_ops(old_suspend_ordering ?
1100 		&acpi_suspend_ops_old : &acpi_suspend_ops);
1101 
1102 	acpi_scan_add_handler(&lps0_handler);
1103 	s2idle_set_ops(&acpi_s2idle_ops);
1104 }
1105 
1106 #else /* !CONFIG_SUSPEND */
1107 #define s2idle_wakeup		(false)
1108 #define lps0_device_handle	(NULL)
acpi_sleep_suspend_setup(void)1109 static inline void acpi_sleep_suspend_setup(void) {}
1110 #endif /* !CONFIG_SUSPEND */
1111 
acpi_s2idle_wakeup(void)1112 bool acpi_s2idle_wakeup(void)
1113 {
1114 	return s2idle_wakeup;
1115 }
1116 
1117 #ifdef CONFIG_PM_SLEEP
1118 static u32 saved_bm_rld;
1119 
acpi_save_bm_rld(void)1120 static int  acpi_save_bm_rld(void)
1121 {
1122 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
1123 	return 0;
1124 }
1125 
acpi_restore_bm_rld(void)1126 static void  acpi_restore_bm_rld(void)
1127 {
1128 	u32 resumed_bm_rld = 0;
1129 
1130 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
1131 	if (resumed_bm_rld == saved_bm_rld)
1132 		return;
1133 
1134 	acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
1135 }
1136 
1137 static struct syscore_ops acpi_sleep_syscore_ops = {
1138 	.suspend = acpi_save_bm_rld,
1139 	.resume = acpi_restore_bm_rld,
1140 };
1141 
acpi_sleep_syscore_init(void)1142 static void acpi_sleep_syscore_init(void)
1143 {
1144 	register_syscore_ops(&acpi_sleep_syscore_ops);
1145 }
1146 #else
acpi_sleep_syscore_init(void)1147 static inline void acpi_sleep_syscore_init(void) {}
1148 #endif /* CONFIG_PM_SLEEP */
1149 
1150 #ifdef CONFIG_HIBERNATION
1151 static unsigned long s4_hardware_signature;
1152 static struct acpi_table_facs *facs;
1153 static bool nosigcheck;
1154 
acpi_no_s4_hw_signature(void)1155 void __init acpi_no_s4_hw_signature(void)
1156 {
1157 	nosigcheck = true;
1158 }
1159 
acpi_hibernation_begin(pm_message_t stage)1160 static int acpi_hibernation_begin(pm_message_t stage)
1161 {
1162 	if (!nvs_nosave) {
1163 		int error = suspend_nvs_alloc();
1164 		if (error)
1165 			return error;
1166 	}
1167 
1168 	if (stage.event == PM_EVENT_HIBERNATE)
1169 		pm_set_suspend_via_firmware();
1170 
1171 	acpi_pm_start(ACPI_STATE_S4);
1172 	return 0;
1173 }
1174 
acpi_hibernation_enter(void)1175 static int acpi_hibernation_enter(void)
1176 {
1177 	acpi_status status = AE_OK;
1178 
1179 	ACPI_FLUSH_CPU_CACHE();
1180 
1181 	/* This shouldn't return.  If it returns, we have a problem */
1182 	status = acpi_enter_sleep_state(ACPI_STATE_S4);
1183 	/* Reprogram control registers */
1184 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1185 
1186 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
1187 }
1188 
acpi_hibernation_leave(void)1189 static void acpi_hibernation_leave(void)
1190 {
1191 	pm_set_resume_via_firmware();
1192 	/*
1193 	 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
1194 	 * enable it here.
1195 	 */
1196 	acpi_enable();
1197 	/* Reprogram control registers */
1198 	acpi_leave_sleep_state_prep(ACPI_STATE_S4);
1199 	/* Check the hardware signature */
1200 	if (facs && s4_hardware_signature != facs->hardware_signature)
1201 		pr_crit("ACPI: Hardware changed while hibernated, success doubtful!\n");
1202 	/* Restore the NVS memory area */
1203 	suspend_nvs_restore();
1204 	/* Allow EC transactions to happen. */
1205 	acpi_ec_unblock_transactions();
1206 }
1207 
acpi_pm_thaw(void)1208 static void acpi_pm_thaw(void)
1209 {
1210 	acpi_ec_unblock_transactions();
1211 	acpi_enable_all_runtime_gpes();
1212 }
1213 
1214 static const struct platform_hibernation_ops acpi_hibernation_ops = {
1215 	.begin = acpi_hibernation_begin,
1216 	.end = acpi_pm_end,
1217 	.pre_snapshot = acpi_pm_prepare,
1218 	.finish = acpi_pm_finish,
1219 	.prepare = acpi_pm_prepare,
1220 	.enter = acpi_hibernation_enter,
1221 	.leave = acpi_hibernation_leave,
1222 	.pre_restore = acpi_pm_freeze,
1223 	.restore_cleanup = acpi_pm_thaw,
1224 };
1225 
1226 /**
1227  *	acpi_hibernation_begin_old - Set the target system sleep state to
1228  *		ACPI_STATE_S4 and execute the _PTS control method.  This
1229  *		function is used if the pre-ACPI 2.0 suspend ordering has been
1230  *		requested.
1231  */
acpi_hibernation_begin_old(pm_message_t stage)1232 static int acpi_hibernation_begin_old(pm_message_t stage)
1233 {
1234 	int error;
1235 	/*
1236 	 * The _TTS object should always be evaluated before the _PTS object.
1237 	 * When the old_suspended_ordering is true, the _PTS object is
1238 	 * evaluated in the acpi_sleep_prepare.
1239 	 */
1240 	acpi_sleep_tts_switch(ACPI_STATE_S4);
1241 
1242 	error = acpi_sleep_prepare(ACPI_STATE_S4);
1243 	if (error)
1244 		return error;
1245 
1246 	if (!nvs_nosave) {
1247 		error = suspend_nvs_alloc();
1248 		if (error)
1249 			return error;
1250 	}
1251 
1252 	if (stage.event == PM_EVENT_HIBERNATE)
1253 		pm_set_suspend_via_firmware();
1254 
1255 	acpi_target_sleep_state = ACPI_STATE_S4;
1256 	acpi_scan_lock_acquire();
1257 	return 0;
1258 }
1259 
1260 /*
1261  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
1262  * been requested.
1263  */
1264 static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
1265 	.begin = acpi_hibernation_begin_old,
1266 	.end = acpi_pm_end,
1267 	.pre_snapshot = acpi_pm_pre_suspend,
1268 	.prepare = acpi_pm_freeze,
1269 	.finish = acpi_pm_finish,
1270 	.enter = acpi_hibernation_enter,
1271 	.leave = acpi_hibernation_leave,
1272 	.pre_restore = acpi_pm_freeze,
1273 	.restore_cleanup = acpi_pm_thaw,
1274 	.recover = acpi_pm_finish,
1275 };
1276 
acpi_sleep_hibernate_setup(void)1277 static void acpi_sleep_hibernate_setup(void)
1278 {
1279 	if (!acpi_sleep_state_supported(ACPI_STATE_S4))
1280 		return;
1281 
1282 	hibernation_set_ops(old_suspend_ordering ?
1283 			&acpi_hibernation_ops_old : &acpi_hibernation_ops);
1284 	sleep_states[ACPI_STATE_S4] = 1;
1285 	if (nosigcheck)
1286 		return;
1287 
1288 	acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
1289 	if (facs)
1290 		s4_hardware_signature = facs->hardware_signature;
1291 }
1292 #else /* !CONFIG_HIBERNATION */
acpi_sleep_hibernate_setup(void)1293 static inline void acpi_sleep_hibernate_setup(void) {}
1294 #endif /* !CONFIG_HIBERNATION */
1295 
acpi_power_off_prepare(void)1296 static void acpi_power_off_prepare(void)
1297 {
1298 	/* Prepare to power off the system */
1299 	acpi_sleep_prepare(ACPI_STATE_S5);
1300 	acpi_disable_all_gpes();
1301 	acpi_os_wait_events_complete();
1302 }
1303 
acpi_power_off(void)1304 static void acpi_power_off(void)
1305 {
1306 	/* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
1307 	printk(KERN_DEBUG "%s called\n", __func__);
1308 	local_irq_disable();
1309 	acpi_enter_sleep_state(ACPI_STATE_S5);
1310 }
1311 
acpi_sleep_init(void)1312 int __init acpi_sleep_init(void)
1313 {
1314 	char supported[ACPI_S_STATE_COUNT * 3 + 1];
1315 	char *pos = supported;
1316 	int i;
1317 
1318 	acpi_sleep_dmi_check();
1319 
1320 	sleep_states[ACPI_STATE_S0] = 1;
1321 
1322 	acpi_sleep_syscore_init();
1323 	acpi_sleep_suspend_setup();
1324 	acpi_sleep_hibernate_setup();
1325 
1326 	if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
1327 		sleep_states[ACPI_STATE_S5] = 1;
1328 		pm_power_off_prepare = acpi_power_off_prepare;
1329 		pm_power_off = acpi_power_off;
1330 	} else {
1331 		acpi_no_s5 = true;
1332 	}
1333 
1334 	supported[0] = 0;
1335 	for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
1336 		if (sleep_states[i])
1337 			pos += sprintf(pos, " S%d", i);
1338 	}
1339 	pr_info(PREFIX "(supports%s)\n", supported);
1340 
1341 	/*
1342 	 * Register the tts_notifier to reboot notifier list so that the _TTS
1343 	 * object can also be evaluated when the system enters S5.
1344 	 */
1345 	register_reboot_notifier(&tts_notifier);
1346 	return 0;
1347 }
1348