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 #ifdef CONFIG_PM_SLEEP
28 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
31 static int acpi_sleep_prepare(u32 acpi_state)
33 #ifdef CONFIG_ACPI_SLEEP
34 /* do we have a wakeup address for S2 and S3? */
35 if (acpi_state == ACPI_STATE_S3) {
36 if (!acpi_wakeup_address) {
39 acpi_set_firmware_waking_vector(
40 (acpi_physical_address)acpi_wakeup_address);
43 ACPI_FLUSH_CPU_CACHE();
44 acpi_enable_wakeup_device_prep(acpi_state);
46 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
48 acpi_enter_sleep_state_prep(acpi_state);
53 static struct platform_suspend_ops acpi_suspend_ops;
55 extern void do_suspend_lowlevel(void);
57 static u32 acpi_suspend_states[] = {
58 [PM_SUSPEND_ON] = ACPI_STATE_S0,
59 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
60 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
61 [PM_SUSPEND_MAX] = ACPI_STATE_S5
65 * acpi_suspend_begin - Set the target system sleep state to the state
66 * associated with given @pm_state, if supported.
69 static int acpi_suspend_begin(suspend_state_t pm_state)
71 u32 acpi_state = acpi_suspend_states[pm_state];
74 if (sleep_states[acpi_state]) {
75 acpi_target_sleep_state = acpi_state;
77 printk(KERN_ERR "ACPI does not support this state: %d\n",
85 * acpi_suspend_prepare - Do preliminary suspend work.
87 * If necessary, set the firmware waking vector and do arch-specific
88 * nastiness to get the wakeup code to the waking vector.
91 static int acpi_suspend_prepare(void)
93 int error = acpi_sleep_prepare(acpi_target_sleep_state);
96 acpi_target_sleep_state = ACPI_STATE_S0;
100 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
104 * acpi_suspend_enter - Actually enter a sleep state.
107 * Flush caches and go to sleep. For STR we have to call arch-specific
108 * assembly, which in turn call acpi_enter_sleep_state().
109 * It's unfortunate, but it works. Please fix if you're feeling frisky.
112 static int acpi_suspend_enter(suspend_state_t pm_state)
114 acpi_status status = AE_OK;
115 unsigned long flags = 0;
116 u32 acpi_state = acpi_target_sleep_state;
118 ACPI_FLUSH_CPU_CACHE();
120 /* Do arch specific saving of state. */
121 if (acpi_state == ACPI_STATE_S3) {
122 int error = acpi_save_state_mem();
128 local_irq_save(flags);
129 acpi_enable_wakeup_device(acpi_state);
130 switch (acpi_state) {
133 status = acpi_enter_sleep_state(acpi_state);
137 do_suspend_lowlevel();
141 /* Reprogram control registers and execute _BFS */
142 acpi_leave_sleep_state_prep(acpi_state);
144 /* ACPI 3.0 specs (P62) says that it's the responsibility
145 * of the OSPM to clear the status bit [ implying that the
146 * POWER_BUTTON event should not reach userspace ]
148 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
149 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
152 * Disable and clear GPE status before interrupt is enabled. Some GPEs
153 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
154 * acpi_leave_sleep_state will reenable specific GPEs later
156 acpi_hw_disable_all_gpes();
158 local_irq_restore(flags);
159 printk(KERN_DEBUG "Back to C!\n");
161 /* restore processor state */
162 if (acpi_state == ACPI_STATE_S3)
163 acpi_restore_state_mem();
165 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
169 * acpi_suspend_finish - Instruct the platform to leave a sleep state.
171 * This is called after we wake back up (or if entering the sleep state
175 static void acpi_suspend_finish(void)
177 u32 acpi_state = acpi_target_sleep_state;
179 acpi_disable_wakeup_device(acpi_state);
180 acpi_leave_sleep_state(acpi_state);
182 /* reset firmware waking vector */
183 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
185 acpi_target_sleep_state = ACPI_STATE_S0;
189 * acpi_suspend_end - Finish up suspend sequence.
192 static void acpi_suspend_end(void)
195 * This is necessary in case acpi_suspend_finish() is not called during a
196 * failing transition to a sleep state.
198 acpi_target_sleep_state = ACPI_STATE_S0;
201 static int acpi_suspend_state_valid(suspend_state_t pm_state)
207 case PM_SUSPEND_STANDBY:
209 acpi_state = acpi_suspend_states[pm_state];
211 return sleep_states[acpi_state];
217 static struct platform_suspend_ops acpi_suspend_ops = {
218 .valid = acpi_suspend_state_valid,
219 .begin = acpi_suspend_begin,
220 .prepare = acpi_suspend_prepare,
221 .enter = acpi_suspend_enter,
222 .finish = acpi_suspend_finish,
223 .end = acpi_suspend_end,
225 #endif /* CONFIG_SUSPEND */
227 #ifdef CONFIG_HIBERNATION
228 static int acpi_hibernation_begin(void)
230 acpi_target_sleep_state = ACPI_STATE_S4;
235 static int acpi_hibernation_prepare(void)
237 int error = acpi_sleep_prepare(ACPI_STATE_S4);
240 acpi_target_sleep_state = ACPI_STATE_S0;
244 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
247 static int acpi_hibernation_enter(void)
249 acpi_status status = AE_OK;
250 unsigned long flags = 0;
252 ACPI_FLUSH_CPU_CACHE();
254 local_irq_save(flags);
255 acpi_enable_wakeup_device(ACPI_STATE_S4);
256 /* This shouldn't return. If it returns, we have a problem */
257 status = acpi_enter_sleep_state(ACPI_STATE_S4);
258 /* Reprogram control registers and execute _BFS */
259 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
260 local_irq_restore(flags);
262 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
265 static void acpi_hibernation_leave(void)
268 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
272 /* Reprogram control registers and execute _BFS */
273 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
276 static void acpi_hibernation_finish(void)
278 acpi_disable_wakeup_device(ACPI_STATE_S4);
279 acpi_leave_sleep_state(ACPI_STATE_S4);
281 /* reset firmware waking vector */
282 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
284 acpi_target_sleep_state = ACPI_STATE_S0;
287 static void acpi_hibernation_end(void)
290 * This is necessary in case acpi_hibernation_finish() is not called
291 * during a failing transition to the sleep state.
293 acpi_target_sleep_state = ACPI_STATE_S0;
296 static int acpi_hibernation_pre_restore(void)
300 status = acpi_hw_disable_all_gpes();
302 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
305 static void acpi_hibernation_restore_cleanup(void)
307 acpi_hw_enable_all_runtime_gpes();
310 static struct platform_hibernation_ops acpi_hibernation_ops = {
311 .begin = acpi_hibernation_begin,
312 .end = acpi_hibernation_end,
313 .pre_snapshot = acpi_hibernation_prepare,
314 .finish = acpi_hibernation_finish,
315 .prepare = acpi_hibernation_prepare,
316 .enter = acpi_hibernation_enter,
317 .leave = acpi_hibernation_leave,
318 .pre_restore = acpi_hibernation_pre_restore,
319 .restore_cleanup = acpi_hibernation_restore_cleanup,
321 #endif /* CONFIG_HIBERNATION */
323 int acpi_suspend(u32 acpi_state)
325 suspend_state_t states[] = {
326 [1] = PM_SUSPEND_STANDBY,
327 [3] = PM_SUSPEND_MEM,
331 if (acpi_state < 6 && states[acpi_state])
332 return pm_suspend(states[acpi_state]);
338 #ifdef CONFIG_PM_SLEEP
340 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
341 * in the system sleep state given by %acpi_target_sleep_state
342 * @dev: device to examine; its driver model wakeup flags control
343 * whether it should be able to wake up the system
344 * @d_min_p: used to store the upper limit of allowed states range
345 * Return value: preferred power state of the device on success, -ENODEV on
346 * failure (ie. if there's no 'struct acpi_device' for @dev)
348 * Find the lowest power (highest number) ACPI device power state that
349 * device @dev can be in while the system is in the sleep state represented
350 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
351 * able to wake up the system from this sleep state. If @d_min_p is set,
352 * the highest power (lowest number) device power state of @dev allowed
353 * in this system sleep state is stored at the location pointed to by it.
355 * The caller must ensure that @dev is valid before using this function.
356 * The caller is also responsible for figuring out if the device is
357 * supposed to be able to wake up the system and passing this information
361 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
363 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
364 struct acpi_device *adev;
365 char acpi_method[] = "_SxD";
366 unsigned long d_min, d_max;
368 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
369 printk(KERN_DEBUG "ACPI handle has no context!\n");
373 acpi_method[2] = '0' + acpi_target_sleep_state;
375 * If the sleep state is S0, we will return D3, but if the device has
376 * _S0W, we will use the value from _S0W
378 d_min = ACPI_STATE_D0;
379 d_max = ACPI_STATE_D3;
382 * If present, _SxD methods return the minimum D-state (highest power
383 * state) we can use for the corresponding S-states. Otherwise, the
384 * minimum D-state is D0 (ACPI 3.x).
386 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
387 * provided -- that's our fault recovery, we ignore retval.
389 if (acpi_target_sleep_state > ACPI_STATE_S0)
390 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
393 * If _PRW says we can wake up the system from the target sleep state,
394 * the D-state returned by _SxD is sufficient for that (we assume a
395 * wakeup-aware driver if wake is set). Still, if _SxW exists
396 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
397 * can wake the system. _S0W may be valid, too.
399 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
400 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
401 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
404 acpi_method[3] = 'W';
405 status = acpi_evaluate_integer(handle, acpi_method, NULL,
407 if (ACPI_FAILURE(status)) {
409 } else if (d_max < d_min) {
410 /* Warn the user of the broken DSDT */
411 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
424 static void acpi_power_off_prepare(void)
426 /* Prepare to power off the system */
427 acpi_sleep_prepare(ACPI_STATE_S5);
428 acpi_hw_disable_all_gpes();
431 static void acpi_power_off(void)
433 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
434 printk("%s called\n", __func__);
436 acpi_enable_wakeup_device(ACPI_STATE_S5);
437 acpi_enter_sleep_state(ACPI_STATE_S5);
440 int __init acpi_sleep_init(void)
444 #ifdef CONFIG_SUSPEND
451 sleep_states[ACPI_STATE_S0] = 1;
452 printk(KERN_INFO PREFIX "(supports S0");
454 #ifdef CONFIG_SUSPEND
455 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
456 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
457 if (ACPI_SUCCESS(status)) {
463 suspend_set_ops(&acpi_suspend_ops);
466 #ifdef CONFIG_HIBERNATION
467 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
468 if (ACPI_SUCCESS(status)) {
469 hibernation_set_ops(&acpi_hibernation_ops);
470 sleep_states[ACPI_STATE_S4] = 1;
474 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
475 if (ACPI_SUCCESS(status)) {
476 sleep_states[ACPI_STATE_S5] = 1;
478 pm_power_off_prepare = acpi_power_off_prepare;
479 pm_power_off = acpi_power_off;