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((acpi_physical_address)
41 acpi_wakeup_address));
44 ACPI_FLUSH_CPU_CACHE();
45 acpi_enable_wakeup_device_prep(acpi_state);
47 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
49 acpi_enter_sleep_state_prep(acpi_state);
54 static struct platform_suspend_ops acpi_suspend_ops;
56 extern void do_suspend_lowlevel(void);
58 static u32 acpi_suspend_states[] = {
59 [PM_SUSPEND_ON] = ACPI_STATE_S0,
60 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
61 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
62 [PM_SUSPEND_MAX] = ACPI_STATE_S5
66 * acpi_suspend_begin - Set the target system sleep state to the state
67 * associated with given @pm_state, if supported.
70 static int acpi_suspend_begin(suspend_state_t pm_state)
72 u32 acpi_state = acpi_suspend_states[pm_state];
75 if (sleep_states[acpi_state]) {
76 acpi_target_sleep_state = acpi_state;
78 printk(KERN_ERR "ACPI does not support this state: %d\n",
86 * acpi_suspend_prepare - Do preliminary suspend work.
88 * If necessary, set the firmware waking vector and do arch-specific
89 * nastiness to get the wakeup code to the waking vector.
92 static int acpi_suspend_prepare(void)
94 int error = acpi_sleep_prepare(acpi_target_sleep_state);
97 acpi_target_sleep_state = ACPI_STATE_S0;
101 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
105 * acpi_suspend_enter - Actually enter a sleep state.
108 * Flush caches and go to sleep. For STR we have to call arch-specific
109 * assembly, which in turn call acpi_enter_sleep_state().
110 * It's unfortunate, but it works. Please fix if you're feeling frisky.
113 static int acpi_suspend_enter(suspend_state_t pm_state)
115 acpi_status status = AE_OK;
116 unsigned long flags = 0;
117 u32 acpi_state = acpi_target_sleep_state;
119 ACPI_FLUSH_CPU_CACHE();
121 /* Do arch specific saving of state. */
122 if (acpi_state == ACPI_STATE_S3) {
123 int error = acpi_save_state_mem();
129 local_irq_save(flags);
130 acpi_enable_wakeup_device(acpi_state);
131 switch (acpi_state) {
134 status = acpi_enter_sleep_state(acpi_state);
138 do_suspend_lowlevel();
142 /* Reprogram control registers and execute _BFS */
143 acpi_leave_sleep_state_prep(acpi_state);
145 /* ACPI 3.0 specs (P62) says that it's the responsibility
146 * of the OSPM to clear the status bit [ implying that the
147 * POWER_BUTTON event should not reach userspace ]
149 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
150 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
153 * Disable and clear GPE status before interrupt is enabled. Some GPEs
154 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
155 * acpi_leave_sleep_state will reenable specific GPEs later
157 acpi_hw_disable_all_gpes();
159 local_irq_restore(flags);
160 printk(KERN_DEBUG "Back to C!\n");
162 /* restore processor state */
163 if (acpi_state == ACPI_STATE_S3)
164 acpi_restore_state_mem();
166 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
170 * acpi_suspend_finish - Instruct the platform to leave a sleep state.
172 * This is called after we wake back up (or if entering the sleep state
176 static void acpi_suspend_finish(void)
178 u32 acpi_state = acpi_target_sleep_state;
180 acpi_disable_wakeup_device(acpi_state);
181 acpi_leave_sleep_state(acpi_state);
183 /* reset firmware waking vector */
184 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
186 acpi_target_sleep_state = ACPI_STATE_S0;
190 * acpi_suspend_end - Finish up suspend sequence.
193 static void acpi_suspend_end(void)
196 * This is necessary in case acpi_suspend_finish() is not called during a
197 * failing transition to a sleep state.
199 acpi_target_sleep_state = ACPI_STATE_S0;
202 static int acpi_suspend_state_valid(suspend_state_t pm_state)
208 case PM_SUSPEND_STANDBY:
210 acpi_state = acpi_suspend_states[pm_state];
212 return sleep_states[acpi_state];
218 static struct platform_suspend_ops acpi_suspend_ops = {
219 .valid = acpi_suspend_state_valid,
220 .begin = acpi_suspend_begin,
221 .prepare = acpi_suspend_prepare,
222 .enter = acpi_suspend_enter,
223 .finish = acpi_suspend_finish,
224 .end = acpi_suspend_end,
226 #endif /* CONFIG_SUSPEND */
228 #ifdef CONFIG_HIBERNATION
229 static int acpi_hibernation_begin(void)
231 acpi_target_sleep_state = ACPI_STATE_S4;
236 static int acpi_hibernation_prepare(void)
238 int error = acpi_sleep_prepare(ACPI_STATE_S4);
241 acpi_target_sleep_state = ACPI_STATE_S0;
245 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
248 static int acpi_hibernation_enter(void)
250 acpi_status status = AE_OK;
251 unsigned long flags = 0;
253 ACPI_FLUSH_CPU_CACHE();
255 local_irq_save(flags);
256 acpi_enable_wakeup_device(ACPI_STATE_S4);
257 /* This shouldn't return. If it returns, we have a problem */
258 status = acpi_enter_sleep_state(ACPI_STATE_S4);
259 /* Reprogram control registers and execute _BFS */
260 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
261 local_irq_restore(flags);
263 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
266 static void acpi_hibernation_leave(void)
269 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
273 /* Reprogram control registers and execute _BFS */
274 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
277 static void acpi_hibernation_finish(void)
279 acpi_disable_wakeup_device(ACPI_STATE_S4);
280 acpi_leave_sleep_state(ACPI_STATE_S4);
282 /* reset firmware waking vector */
283 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
285 acpi_target_sleep_state = ACPI_STATE_S0;
288 static void acpi_hibernation_end(void)
291 * This is necessary in case acpi_hibernation_finish() is not called
292 * during a failing transition to the sleep state.
294 acpi_target_sleep_state = ACPI_STATE_S0;
297 static int acpi_hibernation_pre_restore(void)
301 status = acpi_hw_disable_all_gpes();
303 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
306 static void acpi_hibernation_restore_cleanup(void)
308 acpi_hw_enable_all_runtime_gpes();
311 static struct platform_hibernation_ops acpi_hibernation_ops = {
312 .begin = acpi_hibernation_begin,
313 .end = acpi_hibernation_end,
314 .pre_snapshot = acpi_hibernation_prepare,
315 .finish = acpi_hibernation_finish,
316 .prepare = acpi_hibernation_prepare,
317 .enter = acpi_hibernation_enter,
318 .leave = acpi_hibernation_leave,
319 .pre_restore = acpi_hibernation_pre_restore,
320 .restore_cleanup = acpi_hibernation_restore_cleanup,
322 #endif /* CONFIG_HIBERNATION */
324 int acpi_suspend(u32 acpi_state)
326 suspend_state_t states[] = {
327 [1] = PM_SUSPEND_STANDBY,
328 [3] = PM_SUSPEND_MEM,
332 if (acpi_state < 6 && states[acpi_state])
333 return pm_suspend(states[acpi_state]);
339 #ifdef CONFIG_PM_SLEEP
341 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
342 * in the system sleep state given by %acpi_target_sleep_state
343 * @dev: device to examine; its driver model wakeup flags control
344 * whether it should be able to wake up the system
345 * @d_min_p: used to store the upper limit of allowed states range
346 * Return value: preferred power state of the device on success, -ENODEV on
347 * failure (ie. if there's no 'struct acpi_device' for @dev)
349 * Find the lowest power (highest number) ACPI device power state that
350 * device @dev can be in while the system is in the sleep state represented
351 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
352 * able to wake up the system from this sleep state. If @d_min_p is set,
353 * the highest power (lowest number) device power state of @dev allowed
354 * in this system sleep state is stored at the location pointed to by it.
356 * The caller must ensure that @dev is valid before using this function.
357 * The caller is also responsible for figuring out if the device is
358 * supposed to be able to wake up the system and passing this information
362 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
364 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
365 struct acpi_device *adev;
366 char acpi_method[] = "_SxD";
367 unsigned long d_min, d_max;
369 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
370 printk(KERN_DEBUG "ACPI handle has no context!\n");
374 acpi_method[2] = '0' + acpi_target_sleep_state;
376 * If the sleep state is S0, we will return D3, but if the device has
377 * _S0W, we will use the value from _S0W
379 d_min = ACPI_STATE_D0;
380 d_max = ACPI_STATE_D3;
383 * If present, _SxD methods return the minimum D-state (highest power
384 * state) we can use for the corresponding S-states. Otherwise, the
385 * minimum D-state is D0 (ACPI 3.x).
387 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
388 * provided -- that's our fault recovery, we ignore retval.
390 if (acpi_target_sleep_state > ACPI_STATE_S0)
391 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
394 * If _PRW says we can wake up the system from the target sleep state,
395 * the D-state returned by _SxD is sufficient for that (we assume a
396 * wakeup-aware driver if wake is set). Still, if _SxW exists
397 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
398 * can wake the system. _S0W may be valid, too.
400 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
401 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
402 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
405 acpi_method[3] = 'W';
406 status = acpi_evaluate_integer(handle, acpi_method, NULL,
408 if (ACPI_FAILURE(status)) {
410 } else if (d_max < d_min) {
411 /* Warn the user of the broken DSDT */
412 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
425 static void acpi_power_off_prepare(void)
427 /* Prepare to power off the system */
428 acpi_sleep_prepare(ACPI_STATE_S5);
429 acpi_hw_disable_all_gpes();
432 static void acpi_power_off(void)
434 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
435 printk("%s called\n", __func__);
437 acpi_enable_wakeup_device(ACPI_STATE_S5);
438 acpi_enter_sleep_state(ACPI_STATE_S5);
441 int __init acpi_sleep_init(void)
445 #ifdef CONFIG_SUSPEND
452 sleep_states[ACPI_STATE_S0] = 1;
453 printk(KERN_INFO PREFIX "(supports S0");
455 #ifdef CONFIG_SUSPEND
456 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
457 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
458 if (ACPI_SUCCESS(status)) {
464 suspend_set_ops(&acpi_suspend_ops);
467 #ifdef CONFIG_HIBERNATION
468 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
469 if (ACPI_SUCCESS(status)) {
470 hibernation_set_ops(&acpi_hibernation_ops);
471 sleep_states[ACPI_STATE_S4] = 1;
475 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
476 if (ACPI_SUCCESS(status)) {
477 sleep_states[ACPI_STATE_S5] = 1;
479 pm_power_off_prepare = acpi_power_off_prepare;
480 pm_power_off = acpi_power_off;