1 /*
2  * sleep.c - ACPI sleep support.
3  *
4  * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5  * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
6  * Copyright (c) 2000-2003 Patrick Mochel
7  * Copyright (c) 2003 Open Source Development Lab
8  *
9  * This file is released under the GPLv2.
10  *
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/irq.h>
15 #include <linux/dmi.h>
16 #include <linux/device.h>
17 #include <linux/suspend.h>
18 #include <linux/reboot.h>
19 #include <linux/acpi.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 
23 #include <asm/io.h>
24 
25 #include <acpi/acpi_bus.h>
26 #include <acpi/acpi_drivers.h>
27 
28 #include "internal.h"
29 #include "sleep.h"
30 
31 u8 wake_sleep_flags = ACPI_NO_OPTIONAL_METHODS;
32 static unsigned int gts, bfs;
set_param_wake_flag(const char * val,struct kernel_param * kp)33 static int set_param_wake_flag(const char *val, struct kernel_param *kp)
34 {
35 	int ret = param_set_int(val, kp);
36 
37 	if (ret)
38 		return ret;
39 
40 	if (kp->arg == (const char *)&gts) {
41 		if (gts)
42 			wake_sleep_flags |= ACPI_EXECUTE_GTS;
43 		else
44 			wake_sleep_flags &= ~ACPI_EXECUTE_GTS;
45 	}
46 	if (kp->arg == (const char *)&bfs) {
47 		if (bfs)
48 			wake_sleep_flags |= ACPI_EXECUTE_BFS;
49 		else
50 			wake_sleep_flags &= ~ACPI_EXECUTE_BFS;
51 	}
52 	return ret;
53 }
54 module_param_call(gts, set_param_wake_flag, param_get_int, &gts, 0644);
55 module_param_call(bfs, set_param_wake_flag, param_get_int, &bfs, 0644);
56 MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend.");
57 MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".);
58 
59 static u8 sleep_states[ACPI_S_STATE_COUNT];
60 
acpi_sleep_tts_switch(u32 acpi_state)61 static void acpi_sleep_tts_switch(u32 acpi_state)
62 {
63 	union acpi_object in_arg = { ACPI_TYPE_INTEGER };
64 	struct acpi_object_list arg_list = { 1, &in_arg };
65 	acpi_status status = AE_OK;
66 
67 	in_arg.integer.value = acpi_state;
68 	status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
69 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
70 		/*
71 		 * OS can't evaluate the _TTS object correctly. Some warning
72 		 * message will be printed. But it won't break anything.
73 		 */
74 		printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
75 	}
76 }
77 
tts_notify_reboot(struct notifier_block * this,unsigned long code,void * x)78 static int tts_notify_reboot(struct notifier_block *this,
79 			unsigned long code, void *x)
80 {
81 	acpi_sleep_tts_switch(ACPI_STATE_S5);
82 	return NOTIFY_DONE;
83 }
84 
85 static struct notifier_block tts_notifier = {
86 	.notifier_call	= tts_notify_reboot,
87 	.next		= NULL,
88 	.priority	= 0,
89 };
90 
acpi_sleep_prepare(u32 acpi_state)91 static int acpi_sleep_prepare(u32 acpi_state)
92 {
93 #ifdef CONFIG_ACPI_SLEEP
94 	/* do we have a wakeup address for S2 and S3? */
95 	if (acpi_state == ACPI_STATE_S3) {
96 		if (!acpi_wakeup_address) {
97 			return -EFAULT;
98 		}
99 		acpi_set_firmware_waking_vector(
100 				(acpi_physical_address)acpi_wakeup_address);
101 
102 	}
103 	ACPI_FLUSH_CPU_CACHE();
104 #endif
105 	printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
106 		acpi_state);
107 	acpi_enable_wakeup_devices(acpi_state);
108 	acpi_enter_sleep_state_prep(acpi_state);
109 	return 0;
110 }
111 
112 #ifdef CONFIG_ACPI_SLEEP
113 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
114 
115 /*
116  * The ACPI specification wants us to save NVS memory regions during hibernation
117  * and to restore them during the subsequent resume.  Windows does that also for
118  * suspend to RAM.  However, it is known that this mechanism does not work on
119  * all machines, so we allow the user to disable it with the help of the
120  * 'acpi_sleep=nonvs' kernel command line option.
121  */
122 static bool nvs_nosave;
123 
acpi_nvs_nosave(void)124 void __init acpi_nvs_nosave(void)
125 {
126 	nvs_nosave = true;
127 }
128 
129 /*
130  * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
131  * user to request that behavior by using the 'acpi_old_suspend_ordering'
132  * kernel command line option that causes the following variable to be set.
133  */
134 static bool old_suspend_ordering;
135 
acpi_old_suspend_ordering(void)136 void __init acpi_old_suspend_ordering(void)
137 {
138 	old_suspend_ordering = true;
139 }
140 
init_old_suspend_ordering(const struct dmi_system_id * d)141 static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
142 {
143 	acpi_old_suspend_ordering();
144 	return 0;
145 }
146 
init_nvs_nosave(const struct dmi_system_id * d)147 static int __init init_nvs_nosave(const struct dmi_system_id *d)
148 {
149 	acpi_nvs_nosave();
150 	return 0;
151 }
152 
153 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
154 	{
155 	.callback = init_old_suspend_ordering,
156 	.ident = "Abit KN9 (nForce4 variant)",
157 	.matches = {
158 		DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
159 		DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
160 		},
161 	},
162 	{
163 	.callback = init_old_suspend_ordering,
164 	.ident = "HP xw4600 Workstation",
165 	.matches = {
166 		DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
167 		DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
168 		},
169 	},
170 	{
171 	.callback = init_old_suspend_ordering,
172 	.ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
173 	.matches = {
174 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
175 		DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
176 		},
177 	},
178 	{
179 	.callback = init_old_suspend_ordering,
180 	.ident = "Panasonic CF51-2L",
181 	.matches = {
182 		DMI_MATCH(DMI_BOARD_VENDOR,
183 				"Matsushita Electric Industrial Co.,Ltd."),
184 		DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
185 		},
186 	},
187 	{
188 	.callback = init_nvs_nosave,
189 	.ident = "Sony Vaio VGN-FW41E_H",
190 	.matches = {
191 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
192 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
193 		},
194 	},
195 	{
196 	.callback = init_nvs_nosave,
197 	.ident = "Sony Vaio VGN-FW21E",
198 	.matches = {
199 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
200 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
201 		},
202 	},
203 	{
204 	.callback = init_nvs_nosave,
205 	.ident = "Sony Vaio VPCEB17FX",
206 	.matches = {
207 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
208 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
209 		},
210 	},
211 	{
212 	.callback = init_nvs_nosave,
213 	.ident = "Sony Vaio VGN-SR11M",
214 	.matches = {
215 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
216 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
217 		},
218 	},
219 	{
220 	.callback = init_nvs_nosave,
221 	.ident = "Everex StepNote Series",
222 	.matches = {
223 		DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
224 		DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
225 		},
226 	},
227 	{
228 	.callback = init_nvs_nosave,
229 	.ident = "Sony Vaio VPCEB1Z1E",
230 	.matches = {
231 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
232 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
233 		},
234 	},
235 	{
236 	.callback = init_nvs_nosave,
237 	.ident = "Sony Vaio VGN-NW130D",
238 	.matches = {
239 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
240 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
241 		},
242 	},
243 	{
244 	.callback = init_nvs_nosave,
245 	.ident = "Sony Vaio VPCCW29FX",
246 	.matches = {
247 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
248 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
249 		},
250 	},
251 	{
252 	.callback = init_nvs_nosave,
253 	.ident = "Averatec AV1020-ED2",
254 	.matches = {
255 		DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
256 		DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
257 		},
258 	},
259 	{
260 	.callback = init_old_suspend_ordering,
261 	.ident = "Asus A8N-SLI DELUXE",
262 	.matches = {
263 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
264 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
265 		},
266 	},
267 	{
268 	.callback = init_old_suspend_ordering,
269 	.ident = "Asus A8N-SLI Premium",
270 	.matches = {
271 		DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
272 		DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
273 		},
274 	},
275 	{
276 	.callback = init_nvs_nosave,
277 	.ident = "Sony Vaio VGN-SR26GN_P",
278 	.matches = {
279 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
280 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
281 		},
282 	},
283 	{
284 	.callback = init_nvs_nosave,
285 	.ident = "Sony Vaio VPCEB1S1E",
286 	.matches = {
287 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
288 		DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
289 		},
290 	},
291 	{
292 	.callback = init_nvs_nosave,
293 	.ident = "Sony Vaio VGN-FW520F",
294 	.matches = {
295 		DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
296 		DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
297 		},
298 	},
299 	{
300 	.callback = init_nvs_nosave,
301 	.ident = "Asus K54C",
302 	.matches = {
303 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
304 		DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
305 		},
306 	},
307 	{
308 	.callback = init_nvs_nosave,
309 	.ident = "Asus K54HR",
310 	.matches = {
311 		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
312 		DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
313 		},
314 	},
315 	{},
316 };
317 
acpi_sleep_dmi_check(void)318 static void acpi_sleep_dmi_check(void)
319 {
320 	dmi_check_system(acpisleep_dmi_table);
321 }
322 
323 /**
324  * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
325  */
acpi_pm_freeze(void)326 static int acpi_pm_freeze(void)
327 {
328 	acpi_disable_all_gpes();
329 	acpi_os_wait_events_complete(NULL);
330 	acpi_ec_block_transactions();
331 	return 0;
332 }
333 
334 /**
335  * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
336  */
acpi_pm_pre_suspend(void)337 static int acpi_pm_pre_suspend(void)
338 {
339 	acpi_pm_freeze();
340 	return suspend_nvs_save();
341 }
342 
343 /**
344  *	__acpi_pm_prepare - Prepare the platform to enter the target state.
345  *
346  *	If necessary, set the firmware waking vector and do arch-specific
347  *	nastiness to get the wakeup code to the waking vector.
348  */
__acpi_pm_prepare(void)349 static int __acpi_pm_prepare(void)
350 {
351 	int error = acpi_sleep_prepare(acpi_target_sleep_state);
352 	if (error)
353 		acpi_target_sleep_state = ACPI_STATE_S0;
354 
355 	return error;
356 }
357 
358 /**
359  *	acpi_pm_prepare - Prepare the platform to enter the target sleep
360  *		state and disable the GPEs.
361  */
acpi_pm_prepare(void)362 static int acpi_pm_prepare(void)
363 {
364 	int error = __acpi_pm_prepare();
365 	if (!error)
366 		error = acpi_pm_pre_suspend();
367 
368 	return error;
369 }
370 
371 /**
372  *	acpi_pm_finish - Instruct the platform to leave a sleep state.
373  *
374  *	This is called after we wake back up (or if entering the sleep state
375  *	failed).
376  */
acpi_pm_finish(void)377 static void acpi_pm_finish(void)
378 {
379 	u32 acpi_state = acpi_target_sleep_state;
380 
381 	acpi_ec_unblock_transactions();
382 	suspend_nvs_free();
383 
384 	if (acpi_state == ACPI_STATE_S0)
385 		return;
386 
387 	printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
388 		acpi_state);
389 	acpi_disable_wakeup_devices(acpi_state);
390 	acpi_leave_sleep_state(acpi_state);
391 
392 	/* reset firmware waking vector */
393 	acpi_set_firmware_waking_vector((acpi_physical_address) 0);
394 
395 	acpi_target_sleep_state = ACPI_STATE_S0;
396 }
397 
398 /**
399  *	acpi_pm_end - Finish up suspend sequence.
400  */
acpi_pm_end(void)401 static void acpi_pm_end(void)
402 {
403 	/*
404 	 * This is necessary in case acpi_pm_finish() is not called during a
405 	 * failing transition to a sleep state.
406 	 */
407 	acpi_target_sleep_state = ACPI_STATE_S0;
408 	acpi_sleep_tts_switch(acpi_target_sleep_state);
409 }
410 #else /* !CONFIG_ACPI_SLEEP */
411 #define acpi_target_sleep_state	ACPI_STATE_S0
acpi_sleep_dmi_check(void)412 static inline void acpi_sleep_dmi_check(void) {}
413 #endif /* CONFIG_ACPI_SLEEP */
414 
415 #ifdef CONFIG_SUSPEND
416 static u32 acpi_suspend_states[] = {
417 	[PM_SUSPEND_ON] = ACPI_STATE_S0,
418 	[PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
419 	[PM_SUSPEND_MEM] = ACPI_STATE_S3,
420 	[PM_SUSPEND_MAX] = ACPI_STATE_S5
421 };
422 
423 /**
424  *	acpi_suspend_begin - Set the target system sleep state to the state
425  *		associated with given @pm_state, if supported.
426  */
acpi_suspend_begin(suspend_state_t pm_state)427 static int acpi_suspend_begin(suspend_state_t pm_state)
428 {
429 	u32 acpi_state = acpi_suspend_states[pm_state];
430 	int error = 0;
431 
432 	error = nvs_nosave ? 0 : suspend_nvs_alloc();
433 	if (error)
434 		return error;
435 
436 	if (sleep_states[acpi_state]) {
437 		acpi_target_sleep_state = acpi_state;
438 		acpi_sleep_tts_switch(acpi_target_sleep_state);
439 	} else {
440 		printk(KERN_ERR "ACPI does not support this state: %d\n",
441 			pm_state);
442 		error = -ENOSYS;
443 	}
444 	return error;
445 }
446 
447 /**
448  *	acpi_suspend_enter - Actually enter a sleep state.
449  *	@pm_state: ignored
450  *
451  *	Flush caches and go to sleep. For STR we have to call arch-specific
452  *	assembly, which in turn call acpi_enter_sleep_state().
453  *	It's unfortunate, but it works. Please fix if you're feeling frisky.
454  */
acpi_suspend_enter(suspend_state_t pm_state)455 static int acpi_suspend_enter(suspend_state_t pm_state)
456 {
457 	acpi_status status = AE_OK;
458 	u32 acpi_state = acpi_target_sleep_state;
459 	int error;
460 
461 	ACPI_FLUSH_CPU_CACHE();
462 
463 	switch (acpi_state) {
464 	case ACPI_STATE_S1:
465 		barrier();
466 		status = acpi_enter_sleep_state(acpi_state, wake_sleep_flags);
467 		break;
468 
469 	case ACPI_STATE_S3:
470 		error = acpi_suspend_lowlevel();
471 		if (error)
472 			return error;
473 		pr_info(PREFIX "Low-level resume complete\n");
474 		break;
475 	}
476 
477 	/* This violates the spec but is required for bug compatibility. */
478 	acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
479 
480 	/* Reprogram control registers and execute _BFS */
481 	acpi_leave_sleep_state_prep(acpi_state, wake_sleep_flags);
482 
483 	/* ACPI 3.0 specs (P62) says that it's the responsibility
484 	 * of the OSPM to clear the status bit [ implying that the
485 	 * POWER_BUTTON event should not reach userspace ]
486 	 */
487 	if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
488 		acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
489 
490 	/*
491 	 * Disable and clear GPE status before interrupt is enabled. Some GPEs
492 	 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
493 	 * acpi_leave_sleep_state will reenable specific GPEs later
494 	 */
495 	acpi_disable_all_gpes();
496 	/* Allow EC transactions to happen. */
497 	acpi_ec_unblock_transactions_early();
498 
499 	suspend_nvs_restore();
500 
501 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
502 }
503 
acpi_suspend_state_valid(suspend_state_t pm_state)504 static int acpi_suspend_state_valid(suspend_state_t pm_state)
505 {
506 	u32 acpi_state;
507 
508 	switch (pm_state) {
509 	case PM_SUSPEND_ON:
510 	case PM_SUSPEND_STANDBY:
511 	case PM_SUSPEND_MEM:
512 		acpi_state = acpi_suspend_states[pm_state];
513 
514 		return sleep_states[acpi_state];
515 	default:
516 		return 0;
517 	}
518 }
519 
520 static const struct platform_suspend_ops acpi_suspend_ops = {
521 	.valid = acpi_suspend_state_valid,
522 	.begin = acpi_suspend_begin,
523 	.prepare_late = acpi_pm_prepare,
524 	.enter = acpi_suspend_enter,
525 	.wake = acpi_pm_finish,
526 	.end = acpi_pm_end,
527 };
528 
529 /**
530  *	acpi_suspend_begin_old - Set the target system sleep state to the
531  *		state associated with given @pm_state, if supported, and
532  *		execute the _PTS control method.  This function is used if the
533  *		pre-ACPI 2.0 suspend ordering has been requested.
534  */
acpi_suspend_begin_old(suspend_state_t pm_state)535 static int acpi_suspend_begin_old(suspend_state_t pm_state)
536 {
537 	int error = acpi_suspend_begin(pm_state);
538 	if (!error)
539 		error = __acpi_pm_prepare();
540 
541 	return error;
542 }
543 
544 /*
545  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
546  * been requested.
547  */
548 static const struct platform_suspend_ops acpi_suspend_ops_old = {
549 	.valid = acpi_suspend_state_valid,
550 	.begin = acpi_suspend_begin_old,
551 	.prepare_late = acpi_pm_pre_suspend,
552 	.enter = acpi_suspend_enter,
553 	.wake = acpi_pm_finish,
554 	.end = acpi_pm_end,
555 	.recover = acpi_pm_finish,
556 };
557 #endif /* CONFIG_SUSPEND */
558 
559 #ifdef CONFIG_HIBERNATION
560 static unsigned long s4_hardware_signature;
561 static struct acpi_table_facs *facs;
562 static bool nosigcheck;
563 
acpi_no_s4_hw_signature(void)564 void __init acpi_no_s4_hw_signature(void)
565 {
566 	nosigcheck = true;
567 }
568 
acpi_hibernation_begin(void)569 static int acpi_hibernation_begin(void)
570 {
571 	int error;
572 
573 	error = nvs_nosave ? 0 : suspend_nvs_alloc();
574 	if (!error) {
575 		acpi_target_sleep_state = ACPI_STATE_S4;
576 		acpi_sleep_tts_switch(acpi_target_sleep_state);
577 	}
578 
579 	return error;
580 }
581 
acpi_hibernation_enter(void)582 static int acpi_hibernation_enter(void)
583 {
584 	acpi_status status = AE_OK;
585 
586 	ACPI_FLUSH_CPU_CACHE();
587 
588 	/* This shouldn't return.  If it returns, we have a problem */
589 	status = acpi_enter_sleep_state(ACPI_STATE_S4, wake_sleep_flags);
590 	/* Reprogram control registers and execute _BFS */
591 	acpi_leave_sleep_state_prep(ACPI_STATE_S4, wake_sleep_flags);
592 
593 	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
594 }
595 
acpi_hibernation_leave(void)596 static void acpi_hibernation_leave(void)
597 {
598 	/*
599 	 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
600 	 * enable it here.
601 	 */
602 	acpi_enable();
603 	/* Reprogram control registers and execute _BFS */
604 	acpi_leave_sleep_state_prep(ACPI_STATE_S4, wake_sleep_flags);
605 	/* Check the hardware signature */
606 	if (facs && s4_hardware_signature != facs->hardware_signature) {
607 		printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
608 			"cannot resume!\n");
609 		panic("ACPI S4 hardware signature mismatch");
610 	}
611 	/* Restore the NVS memory area */
612 	suspend_nvs_restore();
613 	/* Allow EC transactions to happen. */
614 	acpi_ec_unblock_transactions_early();
615 }
616 
acpi_pm_thaw(void)617 static void acpi_pm_thaw(void)
618 {
619 	acpi_ec_unblock_transactions();
620 	acpi_enable_all_runtime_gpes();
621 }
622 
623 static const struct platform_hibernation_ops acpi_hibernation_ops = {
624 	.begin = acpi_hibernation_begin,
625 	.end = acpi_pm_end,
626 	.pre_snapshot = acpi_pm_prepare,
627 	.finish = acpi_pm_finish,
628 	.prepare = acpi_pm_prepare,
629 	.enter = acpi_hibernation_enter,
630 	.leave = acpi_hibernation_leave,
631 	.pre_restore = acpi_pm_freeze,
632 	.restore_cleanup = acpi_pm_thaw,
633 };
634 
635 /**
636  *	acpi_hibernation_begin_old - Set the target system sleep state to
637  *		ACPI_STATE_S4 and execute the _PTS control method.  This
638  *		function is used if the pre-ACPI 2.0 suspend ordering has been
639  *		requested.
640  */
acpi_hibernation_begin_old(void)641 static int acpi_hibernation_begin_old(void)
642 {
643 	int error;
644 	/*
645 	 * The _TTS object should always be evaluated before the _PTS object.
646 	 * When the old_suspended_ordering is true, the _PTS object is
647 	 * evaluated in the acpi_sleep_prepare.
648 	 */
649 	acpi_sleep_tts_switch(ACPI_STATE_S4);
650 
651 	error = acpi_sleep_prepare(ACPI_STATE_S4);
652 
653 	if (!error) {
654 		if (!nvs_nosave)
655 			error = suspend_nvs_alloc();
656 		if (!error)
657 			acpi_target_sleep_state = ACPI_STATE_S4;
658 	}
659 	return error;
660 }
661 
662 /*
663  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
664  * been requested.
665  */
666 static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
667 	.begin = acpi_hibernation_begin_old,
668 	.end = acpi_pm_end,
669 	.pre_snapshot = acpi_pm_pre_suspend,
670 	.prepare = acpi_pm_freeze,
671 	.finish = acpi_pm_finish,
672 	.enter = acpi_hibernation_enter,
673 	.leave = acpi_hibernation_leave,
674 	.pre_restore = acpi_pm_freeze,
675 	.restore_cleanup = acpi_pm_thaw,
676 	.recover = acpi_pm_finish,
677 };
678 #endif /* CONFIG_HIBERNATION */
679 
acpi_suspend(u32 acpi_state)680 int acpi_suspend(u32 acpi_state)
681 {
682 	suspend_state_t states[] = {
683 		[1] = PM_SUSPEND_STANDBY,
684 		[3] = PM_SUSPEND_MEM,
685 		[5] = PM_SUSPEND_MAX
686 	};
687 
688 	if (acpi_state < 6 && states[acpi_state])
689 		return pm_suspend(states[acpi_state]);
690 	if (acpi_state == 4)
691 		return hibernate();
692 	return -EINVAL;
693 }
694 
695 #ifdef CONFIG_PM
696 /**
697  *	acpi_pm_device_sleep_state - return preferred power state of ACPI device
698  *		in the system sleep state given by %acpi_target_sleep_state
699  *	@dev: device to examine; its driver model wakeup flags control
700  *		whether it should be able to wake up the system
701  *	@d_min_p: used to store the upper limit of allowed states range
702  *	Return value: preferred power state of the device on success, -ENODEV on
703  *		failure (ie. if there's no 'struct acpi_device' for @dev)
704  *
705  *	Find the lowest power (highest number) ACPI device power state that
706  *	device @dev can be in while the system is in the sleep state represented
707  *	by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
708  *	able to wake up the system from this sleep state.  If @d_min_p is set,
709  *	the highest power (lowest number) device power state of @dev allowed
710  *	in this system sleep state is stored at the location pointed to by it.
711  *
712  *	The caller must ensure that @dev is valid before using this function.
713  *	The caller is also responsible for figuring out if the device is
714  *	supposed to be able to wake up the system and passing this information
715  *	via @wake.
716  */
717 
acpi_pm_device_sleep_state(struct device * dev,int * d_min_p)718 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
719 {
720 	acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
721 	struct acpi_device *adev;
722 	char acpi_method[] = "_SxD";
723 	unsigned long long d_min, d_max;
724 
725 	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
726 		printk(KERN_DEBUG "ACPI handle has no context!\n");
727 		return -ENODEV;
728 	}
729 
730 	acpi_method[2] = '0' + acpi_target_sleep_state;
731 	/*
732 	 * If the sleep state is S0, we will return D3, but if the device has
733 	 * _S0W, we will use the value from _S0W
734 	 */
735 	d_min = ACPI_STATE_D0;
736 	d_max = ACPI_STATE_D3;
737 
738 	/*
739 	 * If present, _SxD methods return the minimum D-state (highest power
740 	 * state) we can use for the corresponding S-states.  Otherwise, the
741 	 * minimum D-state is D0 (ACPI 3.x).
742 	 *
743 	 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
744 	 * provided -- that's our fault recovery, we ignore retval.
745 	 */
746 	if (acpi_target_sleep_state > ACPI_STATE_S0)
747 		acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
748 
749 	/*
750 	 * If _PRW says we can wake up the system from the target sleep state,
751 	 * the D-state returned by _SxD is sufficient for that (we assume a
752 	 * wakeup-aware driver if wake is set).  Still, if _SxW exists
753 	 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
754 	 * can wake the system.  _S0W may be valid, too.
755 	 */
756 	if (acpi_target_sleep_state == ACPI_STATE_S0 ||
757 	    (device_may_wakeup(dev) && adev->wakeup.flags.valid &&
758 	     adev->wakeup.sleep_state >= acpi_target_sleep_state)) {
759 		acpi_status status;
760 
761 		acpi_method[3] = 'W';
762 		status = acpi_evaluate_integer(handle, acpi_method, NULL,
763 						&d_max);
764 		if (ACPI_FAILURE(status)) {
765 			if (acpi_target_sleep_state != ACPI_STATE_S0 ||
766 			    status != AE_NOT_FOUND)
767 				d_max = d_min;
768 		} else if (d_max < d_min) {
769 			/* Warn the user of the broken DSDT */
770 			printk(KERN_WARNING "ACPI: Wrong value from %s\n",
771 				acpi_method);
772 			/* Sanitize it */
773 			d_min = d_max;
774 		}
775 	}
776 
777 	if (d_min_p)
778 		*d_min_p = d_min;
779 	return d_max;
780 }
781 #endif /* CONFIG_PM */
782 
783 #ifdef CONFIG_PM_SLEEP
784 /**
785  * acpi_pm_device_run_wake - Enable/disable wake-up for given device.
786  * @phys_dev: Device to enable/disable the platform to wake-up the system for.
787  * @enable: Whether enable or disable the wake-up functionality.
788  *
789  * Find the ACPI device object corresponding to @pci_dev and try to
790  * enable/disable the GPE associated with it.
791  */
acpi_pm_device_run_wake(struct device * phys_dev,bool enable)792 int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
793 {
794 	struct acpi_device *dev;
795 	acpi_handle handle;
796 
797 	if (!device_run_wake(phys_dev))
798 		return -EINVAL;
799 
800 	handle = DEVICE_ACPI_HANDLE(phys_dev);
801 	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
802 		dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
803 			__func__);
804 		return -ENODEV;
805 	}
806 
807 	if (enable) {
808 		acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
809 		acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
810 	} else {
811 		acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
812 		acpi_disable_wakeup_device_power(dev);
813 	}
814 
815 	return 0;
816 }
817 
818 /**
819  *	acpi_pm_device_sleep_wake - enable or disable the system wake-up
820  *                                  capability of given device
821  *	@dev: device to handle
822  *	@enable: 'true' - enable, 'false' - disable the wake-up capability
823  */
acpi_pm_device_sleep_wake(struct device * dev,bool enable)824 int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
825 {
826 	acpi_handle handle;
827 	struct acpi_device *adev;
828 	int error;
829 
830 	if (!device_can_wakeup(dev))
831 		return -EINVAL;
832 
833 	handle = DEVICE_ACPI_HANDLE(dev);
834 	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
835 		dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
836 		return -ENODEV;
837 	}
838 
839 	error = enable ?
840 		acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
841 		acpi_disable_wakeup_device_power(adev);
842 	if (!error)
843 		dev_info(dev, "wake-up capability %s by ACPI\n",
844 				enable ? "enabled" : "disabled");
845 
846 	return error;
847 }
848 #endif  /* CONFIG_PM_SLEEP */
849 
acpi_power_off_prepare(void)850 static void acpi_power_off_prepare(void)
851 {
852 	/* Prepare to power off the system */
853 	acpi_sleep_prepare(ACPI_STATE_S5);
854 	acpi_disable_all_gpes();
855 }
856 
acpi_power_off(void)857 static void acpi_power_off(void)
858 {
859 	/* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
860 	printk(KERN_DEBUG "%s called\n", __func__);
861 	local_irq_disable();
862 	acpi_enter_sleep_state(ACPI_STATE_S5, wake_sleep_flags);
863 }
864 
865 /*
866  * ACPI 2.0 created the optional _GTS and _BFS,
867  * but industry adoption has been neither rapid nor broad.
868  *
869  * Linux gets into trouble when it executes poorly validated
870  * paths through the BIOS, so disable _GTS and _BFS by default,
871  * but do speak up and offer the option to enable them.
872  */
acpi_gts_bfs_check(void)873 static void __init acpi_gts_bfs_check(void)
874 {
875 	acpi_handle dummy;
876 
877 	if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__GTS, &dummy)))
878 	{
879 		printk(KERN_NOTICE PREFIX "BIOS offers _GTS\n");
880 		printk(KERN_NOTICE PREFIX "If \"acpi.gts=1\" improves suspend, "
881 			"please notify linux-acpi@vger.kernel.org\n");
882 	}
883 	if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__BFS, &dummy)))
884 	{
885 		printk(KERN_NOTICE PREFIX "BIOS offers _BFS\n");
886 		printk(KERN_NOTICE PREFIX "If \"acpi.bfs=1\" improves resume, "
887 			"please notify linux-acpi@vger.kernel.org\n");
888 	}
889 }
890 
acpi_sleep_init(void)891 int __init acpi_sleep_init(void)
892 {
893 	acpi_status status;
894 	u8 type_a, type_b;
895 #ifdef CONFIG_SUSPEND
896 	int i = 0;
897 #endif
898 
899 	if (acpi_disabled)
900 		return 0;
901 
902 	acpi_sleep_dmi_check();
903 
904 	sleep_states[ACPI_STATE_S0] = 1;
905 	printk(KERN_INFO PREFIX "(supports S0");
906 
907 #ifdef CONFIG_SUSPEND
908 	for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
909 		status = acpi_get_sleep_type_data(i, &type_a, &type_b);
910 		if (ACPI_SUCCESS(status)) {
911 			sleep_states[i] = 1;
912 			printk(" S%d", i);
913 		}
914 	}
915 
916 	suspend_set_ops(old_suspend_ordering ?
917 		&acpi_suspend_ops_old : &acpi_suspend_ops);
918 #endif
919 
920 #ifdef CONFIG_HIBERNATION
921 	status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
922 	if (ACPI_SUCCESS(status)) {
923 		hibernation_set_ops(old_suspend_ordering ?
924 			&acpi_hibernation_ops_old : &acpi_hibernation_ops);
925 		sleep_states[ACPI_STATE_S4] = 1;
926 		printk(" S4");
927 		if (!nosigcheck) {
928 			acpi_get_table(ACPI_SIG_FACS, 1,
929 				(struct acpi_table_header **)&facs);
930 			if (facs)
931 				s4_hardware_signature =
932 					facs->hardware_signature;
933 		}
934 	}
935 #endif
936 	status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
937 	if (ACPI_SUCCESS(status)) {
938 		sleep_states[ACPI_STATE_S5] = 1;
939 		printk(" S5");
940 		pm_power_off_prepare = acpi_power_off_prepare;
941 		pm_power_off = acpi_power_off;
942 	}
943 	printk(")\n");
944 	/*
945 	 * Register the tts_notifier to reboot notifier list so that the _TTS
946 	 * object can also be evaluated when the system enters S5.
947 	 */
948 	register_reboot_notifier(&tts_notifier);
949 	acpi_gts_bfs_check();
950 	return 0;
951 }
952