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