2 * sleep.c - ACPI sleep support.
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
9 * This file is released under the GPLv2.
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>
21 #include <acpi/acpi_bus.h>
22 #include <acpi/acpi_drivers.h>
25 u8 sleep_states[ACPI_S_STATE_COUNT];
27 static int acpi_sleep_prepare(u32 acpi_state)
29 #ifdef CONFIG_ACPI_SLEEP
30 /* do we have a wakeup address for S2 and S3? */
31 if (acpi_state == ACPI_STATE_S3) {
32 if (!acpi_wakeup_address) {
35 acpi_set_firmware_waking_vector((acpi_physical_address)
37 acpi_wakeup_address));
40 ACPI_FLUSH_CPU_CACHE();
41 acpi_enable_wakeup_device_prep(acpi_state);
43 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
45 acpi_enter_sleep_state_prep(acpi_state);
49 #ifdef CONFIG_PM_SLEEP
50 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
53 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
54 * user to request that behavior by using the 'acpi_old_suspend_ordering'
55 * kernel command line option that causes the following variable to be set.
57 static bool old_suspend_ordering;
59 void __init acpi_old_suspend_ordering(void)
61 old_suspend_ordering = true;
65 * acpi_pm_disable_gpes - Disable the GPEs.
67 static int acpi_pm_disable_gpes(void)
69 acpi_hw_disable_all_gpes();
74 * __acpi_pm_prepare - Prepare the platform to enter the target state.
76 * If necessary, set the firmware waking vector and do arch-specific
77 * nastiness to get the wakeup code to the waking vector.
79 static int __acpi_pm_prepare(void)
81 int error = acpi_sleep_prepare(acpi_target_sleep_state);
84 acpi_target_sleep_state = ACPI_STATE_S0;
89 * acpi_pm_prepare - Prepare the platform to enter the target sleep
90 * state and disable the GPEs.
92 static int acpi_pm_prepare(void)
94 int error = __acpi_pm_prepare();
97 acpi_hw_disable_all_gpes();
102 * acpi_pm_finish - Instruct the platform to leave a sleep state.
104 * This is called after we wake back up (or if entering the sleep state
107 static void acpi_pm_finish(void)
109 u32 acpi_state = acpi_target_sleep_state;
111 if (acpi_state == ACPI_STATE_S0)
114 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
116 acpi_disable_wakeup_device(acpi_state);
117 acpi_leave_sleep_state(acpi_state);
119 /* reset firmware waking vector */
120 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
122 acpi_target_sleep_state = ACPI_STATE_S0;
126 * acpi_pm_end - Finish up suspend sequence.
128 static void acpi_pm_end(void)
131 * This is necessary in case acpi_pm_finish() is not called during a
132 * failing transition to a sleep state.
134 acpi_target_sleep_state = ACPI_STATE_S0;
136 #endif /* CONFIG_PM_SLEEP */
138 #ifdef CONFIG_SUSPEND
139 extern void do_suspend_lowlevel(void);
141 static u32 acpi_suspend_states[] = {
142 [PM_SUSPEND_ON] = ACPI_STATE_S0,
143 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
144 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
145 [PM_SUSPEND_MAX] = ACPI_STATE_S5
149 * acpi_suspend_begin - Set the target system sleep state to the state
150 * associated with given @pm_state, if supported.
152 static int acpi_suspend_begin(suspend_state_t pm_state)
154 u32 acpi_state = acpi_suspend_states[pm_state];
157 if (sleep_states[acpi_state]) {
158 acpi_target_sleep_state = acpi_state;
160 printk(KERN_ERR "ACPI does not support this state: %d\n",
168 * acpi_suspend_enter - Actually enter a sleep state.
171 * Flush caches and go to sleep. For STR we have to call arch-specific
172 * assembly, which in turn call acpi_enter_sleep_state().
173 * It's unfortunate, but it works. Please fix if you're feeling frisky.
175 static int acpi_suspend_enter(suspend_state_t pm_state)
177 acpi_status status = AE_OK;
178 unsigned long flags = 0;
179 u32 acpi_state = acpi_target_sleep_state;
181 ACPI_FLUSH_CPU_CACHE();
183 /* Do arch specific saving of state. */
184 if (acpi_state == ACPI_STATE_S3) {
185 int error = acpi_save_state_mem();
191 local_irq_save(flags);
192 acpi_enable_wakeup_device(acpi_state);
193 switch (acpi_state) {
196 status = acpi_enter_sleep_state(acpi_state);
200 do_suspend_lowlevel();
204 /* Reprogram control registers and execute _BFS */
205 acpi_leave_sleep_state_prep(acpi_state);
207 /* ACPI 3.0 specs (P62) says that it's the responsibility
208 * of the OSPM to clear the status bit [ implying that the
209 * POWER_BUTTON event should not reach userspace ]
211 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
212 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
215 * Disable and clear GPE status before interrupt is enabled. Some GPEs
216 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
217 * acpi_leave_sleep_state will reenable specific GPEs later
219 acpi_hw_disable_all_gpes();
221 local_irq_restore(flags);
222 printk(KERN_DEBUG "Back to C!\n");
224 /* restore processor state */
225 if (acpi_state == ACPI_STATE_S3)
226 acpi_restore_state_mem();
228 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
231 static int acpi_suspend_state_valid(suspend_state_t pm_state)
237 case PM_SUSPEND_STANDBY:
239 acpi_state = acpi_suspend_states[pm_state];
241 return sleep_states[acpi_state];
247 static struct platform_suspend_ops acpi_suspend_ops = {
248 .valid = acpi_suspend_state_valid,
249 .begin = acpi_suspend_begin,
250 .prepare = acpi_pm_prepare,
251 .enter = acpi_suspend_enter,
252 .finish = acpi_pm_finish,
257 * acpi_suspend_begin_old - Set the target system sleep state to the
258 * state associated with given @pm_state, if supported, and
259 * execute the _PTS control method. This function is used if the
260 * pre-ACPI 2.0 suspend ordering has been requested.
262 static int acpi_suspend_begin_old(suspend_state_t pm_state)
264 int error = acpi_suspend_begin(pm_state);
267 error = __acpi_pm_prepare();
272 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
275 static struct platform_suspend_ops acpi_suspend_ops_old = {
276 .valid = acpi_suspend_state_valid,
277 .begin = acpi_suspend_begin_old,
278 .prepare = acpi_pm_disable_gpes,
279 .enter = acpi_suspend_enter,
280 .finish = acpi_pm_finish,
282 .recover = acpi_pm_finish,
284 #endif /* CONFIG_SUSPEND */
286 #ifdef CONFIG_HIBERNATION
287 static int acpi_hibernation_begin(void)
289 acpi_target_sleep_state = ACPI_STATE_S4;
293 static int acpi_hibernation_enter(void)
295 acpi_status status = AE_OK;
296 unsigned long flags = 0;
298 ACPI_FLUSH_CPU_CACHE();
300 local_irq_save(flags);
301 acpi_enable_wakeup_device(ACPI_STATE_S4);
302 /* This shouldn't return. If it returns, we have a problem */
303 status = acpi_enter_sleep_state(ACPI_STATE_S4);
304 /* Reprogram control registers and execute _BFS */
305 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
306 local_irq_restore(flags);
308 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
311 static void acpi_hibernation_leave(void)
314 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
318 /* Reprogram control registers and execute _BFS */
319 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
322 static void acpi_pm_enable_gpes(void)
324 acpi_hw_enable_all_runtime_gpes();
327 static struct platform_hibernation_ops acpi_hibernation_ops = {
328 .begin = acpi_hibernation_begin,
330 .pre_snapshot = acpi_pm_prepare,
331 .finish = acpi_pm_finish,
332 .prepare = acpi_pm_prepare,
333 .enter = acpi_hibernation_enter,
334 .leave = acpi_hibernation_leave,
335 .pre_restore = acpi_pm_disable_gpes,
336 .restore_cleanup = acpi_pm_enable_gpes,
340 * acpi_hibernation_begin_old - Set the target system sleep state to
341 * ACPI_STATE_S4 and execute the _PTS control method. This
342 * function is used if the pre-ACPI 2.0 suspend ordering has been
345 static int acpi_hibernation_begin_old(void)
347 int error = acpi_sleep_prepare(ACPI_STATE_S4);
350 acpi_target_sleep_state = ACPI_STATE_S4;
355 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
358 static struct platform_hibernation_ops acpi_hibernation_ops_old = {
359 .begin = acpi_hibernation_begin_old,
361 .pre_snapshot = acpi_pm_disable_gpes,
362 .finish = acpi_pm_finish,
363 .prepare = acpi_pm_disable_gpes,
364 .enter = acpi_hibernation_enter,
365 .leave = acpi_hibernation_leave,
366 .pre_restore = acpi_pm_disable_gpes,
367 .restore_cleanup = acpi_pm_enable_gpes,
368 .recover = acpi_pm_finish,
370 #endif /* CONFIG_HIBERNATION */
372 int acpi_suspend(u32 acpi_state)
374 suspend_state_t states[] = {
375 [1] = PM_SUSPEND_STANDBY,
376 [3] = PM_SUSPEND_MEM,
380 if (acpi_state < 6 && states[acpi_state])
381 return pm_suspend(states[acpi_state]);
387 #ifdef CONFIG_PM_SLEEP
389 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
390 * in the system sleep state given by %acpi_target_sleep_state
391 * @dev: device to examine; its driver model wakeup flags control
392 * whether it should be able to wake up the system
393 * @d_min_p: used to store the upper limit of allowed states range
394 * Return value: preferred power state of the device on success, -ENODEV on
395 * failure (ie. if there's no 'struct acpi_device' for @dev)
397 * Find the lowest power (highest number) ACPI device power state that
398 * device @dev can be in while the system is in the sleep state represented
399 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
400 * able to wake up the system from this sleep state. If @d_min_p is set,
401 * the highest power (lowest number) device power state of @dev allowed
402 * in this system sleep state is stored at the location pointed to by it.
404 * The caller must ensure that @dev is valid before using this function.
405 * The caller is also responsible for figuring out if the device is
406 * supposed to be able to wake up the system and passing this information
410 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
412 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
413 struct acpi_device *adev;
414 char acpi_method[] = "_SxD";
415 unsigned long d_min, d_max;
417 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
418 printk(KERN_DEBUG "ACPI handle has no context!\n");
422 acpi_method[2] = '0' + acpi_target_sleep_state;
424 * If the sleep state is S0, we will return D3, but if the device has
425 * _S0W, we will use the value from _S0W
427 d_min = ACPI_STATE_D0;
428 d_max = ACPI_STATE_D3;
431 * If present, _SxD methods return the minimum D-state (highest power
432 * state) we can use for the corresponding S-states. Otherwise, the
433 * minimum D-state is D0 (ACPI 3.x).
435 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
436 * provided -- that's our fault recovery, we ignore retval.
438 if (acpi_target_sleep_state > ACPI_STATE_S0)
439 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
442 * If _PRW says we can wake up the system from the target sleep state,
443 * the D-state returned by _SxD is sufficient for that (we assume a
444 * wakeup-aware driver if wake is set). Still, if _SxW exists
445 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
446 * can wake the system. _S0W may be valid, too.
448 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
449 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
450 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
453 acpi_method[3] = 'W';
454 status = acpi_evaluate_integer(handle, acpi_method, NULL,
456 if (ACPI_FAILURE(status)) {
458 } else if (d_max < d_min) {
459 /* Warn the user of the broken DSDT */
460 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
473 static void acpi_power_off_prepare(void)
475 /* Prepare to power off the system */
476 acpi_sleep_prepare(ACPI_STATE_S5);
477 acpi_hw_disable_all_gpes();
480 static void acpi_power_off(void)
482 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
483 printk("%s called\n", __func__);
485 acpi_enable_wakeup_device(ACPI_STATE_S5);
486 acpi_enter_sleep_state(ACPI_STATE_S5);
489 int __init acpi_sleep_init(void)
493 #ifdef CONFIG_SUSPEND
500 sleep_states[ACPI_STATE_S0] = 1;
501 printk(KERN_INFO PREFIX "(supports S0");
503 #ifdef CONFIG_SUSPEND
504 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
505 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
506 if (ACPI_SUCCESS(status)) {
512 suspend_set_ops(old_suspend_ordering ?
513 &acpi_suspend_ops_old : &acpi_suspend_ops);
516 #ifdef CONFIG_HIBERNATION
517 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
518 if (ACPI_SUCCESS(status)) {
519 hibernation_set_ops(old_suspend_ordering ?
520 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
521 sleep_states[ACPI_STATE_S4] = 1;
525 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
526 if (ACPI_SUCCESS(status)) {
527 sleep_states[ACPI_STATE_S5] = 1;
529 pm_power_off_prepare = acpi_power_off_prepare;
530 pm_power_off = acpi_power_off;