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;
29 static bool acpi_sleep_finish_wake_up;
32 * ACPI 2.0 and later want us to execute _PTS after suspending devices, so we
33 * allow the user to request that behavior by using the 'acpi_new_pts_ordering'
34 * kernel command line option that causes the following variable to be set.
36 static bool new_pts_ordering;
38 static int __init acpi_new_pts_ordering(char *str)
40 new_pts_ordering = true;
43 __setup("acpi_new_pts_ordering", acpi_new_pts_ordering);
46 int acpi_sleep_prepare(u32 acpi_state)
48 #ifdef CONFIG_ACPI_SLEEP
49 /* do we have a wakeup address for S2 and S3? */
50 if (acpi_state == ACPI_STATE_S3) {
51 if (!acpi_wakeup_address) {
54 acpi_set_firmware_waking_vector((acpi_physical_address)
56 acpi_wakeup_address));
59 ACPI_FLUSH_CPU_CACHE();
60 acpi_enable_wakeup_device_prep(acpi_state);
62 acpi_enter_sleep_state_prep(acpi_state);
67 static struct platform_suspend_ops acpi_pm_ops;
69 extern void do_suspend_lowlevel(void);
71 static u32 acpi_suspend_states[] = {
72 [PM_SUSPEND_ON] = ACPI_STATE_S0,
73 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
74 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
75 [PM_SUSPEND_MAX] = ACPI_STATE_S5
78 static int init_8259A_after_S1;
81 * acpi_pm_begin - Set the target system sleep state to the state
82 * associated with given @pm_state, if supported.
85 static int acpi_pm_begin(suspend_state_t pm_state)
87 u32 acpi_state = acpi_suspend_states[pm_state];
90 if (sleep_states[acpi_state]) {
91 acpi_target_sleep_state = acpi_state;
95 error = acpi_sleep_prepare(acpi_state);
97 acpi_target_sleep_state = ACPI_STATE_S0;
99 acpi_sleep_finish_wake_up = true;
101 printk(KERN_ERR "ACPI does not support this state: %d\n",
109 * acpi_pm_prepare - Do preliminary suspend work.
111 * If necessary, set the firmware waking vector and do arch-specific
112 * nastiness to get the wakeup code to the waking vector.
115 static int acpi_pm_prepare(void)
117 if (new_pts_ordering) {
118 int error = acpi_sleep_prepare(acpi_target_sleep_state);
121 acpi_target_sleep_state = ACPI_STATE_S0;
124 acpi_sleep_finish_wake_up = true;
127 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
131 * acpi_pm_enter - Actually enter a sleep state.
134 * Flush caches and go to sleep. For STR we have to call arch-specific
135 * assembly, which in turn call acpi_enter_sleep_state().
136 * It's unfortunate, but it works. Please fix if you're feeling frisky.
139 static int acpi_pm_enter(suspend_state_t pm_state)
141 acpi_status status = AE_OK;
142 unsigned long flags = 0;
143 u32 acpi_state = acpi_target_sleep_state;
145 ACPI_FLUSH_CPU_CACHE();
147 /* Do arch specific saving of state. */
148 if (acpi_state == ACPI_STATE_S3) {
149 int error = acpi_save_state_mem();
155 local_irq_save(flags);
156 acpi_enable_wakeup_device(acpi_state);
157 switch (acpi_state) {
160 status = acpi_enter_sleep_state(acpi_state);
164 do_suspend_lowlevel();
168 /* Reprogram control registers and execute _BFS */
169 acpi_leave_sleep_state_prep(acpi_state);
171 /* ACPI 3.0 specs (P62) says that it's the responsabilty
172 * of the OSPM to clear the status bit [ implying that the
173 * POWER_BUTTON event should not reach userspace ]
175 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
176 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
179 * Disable and clear GPE status before interrupt is enabled. Some GPEs
180 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
181 * acpi_leave_sleep_state will reenable specific GPEs later
183 acpi_hw_disable_all_gpes();
185 local_irq_restore(flags);
186 printk(KERN_DEBUG "Back to C!\n");
188 /* restore processor state */
189 if (acpi_state == ACPI_STATE_S3)
190 acpi_restore_state_mem();
192 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
196 * acpi_pm_finish - Instruct the platform to leave a sleep state.
198 * This is called after we wake back up (or if entering the sleep state
202 static void acpi_pm_finish(void)
204 u32 acpi_state = acpi_target_sleep_state;
206 acpi_disable_wakeup_device(acpi_state);
207 acpi_leave_sleep_state(acpi_state);
209 /* reset firmware waking vector */
210 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
212 acpi_target_sleep_state = ACPI_STATE_S0;
213 acpi_sleep_finish_wake_up = false;
216 if (init_8259A_after_S1) {
217 printk("Broken toshiba laptop -> kicking interrupts\n");
224 * acpi_pm_end - Finish up suspend sequence.
227 static void acpi_pm_end(void)
230 * This is necessary in case acpi_pm_finish() is not called directly
231 * during a failing transition to a sleep state.
233 if (acpi_sleep_finish_wake_up)
237 static int acpi_pm_state_valid(suspend_state_t pm_state)
243 case PM_SUSPEND_STANDBY:
245 acpi_state = acpi_suspend_states[pm_state];
247 return sleep_states[acpi_state];
253 static struct platform_suspend_ops acpi_pm_ops = {
254 .valid = acpi_pm_state_valid,
255 .begin = acpi_pm_begin,
256 .prepare = acpi_pm_prepare,
257 .enter = acpi_pm_enter,
258 .finish = acpi_pm_finish,
263 * Toshiba fails to preserve interrupts over S1, reinitialization
264 * of 8259 is needed after S1 resume.
266 static int __init init_ints_after_s1(const struct dmi_system_id *d)
268 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
269 init_8259A_after_S1 = 1;
273 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
275 .callback = init_ints_after_s1,
276 .ident = "Toshiba Satellite 4030cdt",
277 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
281 #endif /* CONFIG_SUSPEND */
283 #ifdef CONFIG_HIBERNATION
284 static int acpi_hibernation_begin(void)
288 acpi_target_sleep_state = ACPI_STATE_S4;
289 if (new_pts_ordering)
292 error = acpi_sleep_prepare(ACPI_STATE_S4);
294 acpi_target_sleep_state = ACPI_STATE_S0;
296 acpi_sleep_finish_wake_up = true;
301 static int acpi_hibernation_prepare(void)
303 if (new_pts_ordering) {
304 int error = acpi_sleep_prepare(ACPI_STATE_S4);
307 acpi_target_sleep_state = ACPI_STATE_S0;
310 acpi_sleep_finish_wake_up = true;
313 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
316 static int acpi_hibernation_enter(void)
318 acpi_status status = AE_OK;
319 unsigned long flags = 0;
321 ACPI_FLUSH_CPU_CACHE();
323 local_irq_save(flags);
324 acpi_enable_wakeup_device(ACPI_STATE_S4);
325 /* This shouldn't return. If it returns, we have a problem */
326 status = acpi_enter_sleep_state(ACPI_STATE_S4);
327 /* Reprogram control registers and execute _BFS */
328 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
329 local_irq_restore(flags);
331 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
334 static void acpi_hibernation_leave(void)
337 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
341 /* Reprogram control registers and execute _BFS */
342 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
345 static void acpi_hibernation_finish(void)
347 acpi_disable_wakeup_device(ACPI_STATE_S4);
348 acpi_leave_sleep_state(ACPI_STATE_S4);
350 /* reset firmware waking vector */
351 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
353 acpi_target_sleep_state = ACPI_STATE_S0;
354 acpi_sleep_finish_wake_up = false;
357 static void acpi_hibernation_end(void)
360 * This is necessary in case acpi_hibernation_finish() is not called
361 * directly during a failing transition to the sleep state.
363 if (acpi_sleep_finish_wake_up)
364 acpi_hibernation_finish();
367 static int acpi_hibernation_pre_restore(void)
371 status = acpi_hw_disable_all_gpes();
373 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
376 static void acpi_hibernation_restore_cleanup(void)
378 acpi_hw_enable_all_runtime_gpes();
381 static struct platform_hibernation_ops acpi_hibernation_ops = {
382 .begin = acpi_hibernation_begin,
383 .end = acpi_hibernation_end,
384 .pre_snapshot = acpi_hibernation_prepare,
385 .finish = acpi_hibernation_finish,
386 .prepare = acpi_hibernation_prepare,
387 .enter = acpi_hibernation_enter,
388 .leave = acpi_hibernation_leave,
389 .pre_restore = acpi_hibernation_pre_restore,
390 .restore_cleanup = acpi_hibernation_restore_cleanup,
392 #endif /* CONFIG_HIBERNATION */
394 int acpi_suspend(u32 acpi_state)
396 suspend_state_t states[] = {
397 [1] = PM_SUSPEND_STANDBY,
398 [3] = PM_SUSPEND_MEM,
402 if (acpi_state < 6 && states[acpi_state])
403 return pm_suspend(states[acpi_state]);
409 #ifdef CONFIG_PM_SLEEP
411 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
412 * in the system sleep state given by %acpi_target_sleep_state
413 * @dev: device to examine
414 * @wake: if set, the device should be able to wake up the system
415 * @d_min_p: used to store the upper limit of allowed states range
416 * Return value: preferred power state of the device on success, -ENODEV on
417 * failure (ie. if there's no 'struct acpi_device' for @dev)
419 * Find the lowest power (highest number) ACPI device power state that
420 * device @dev can be in while the system is in the sleep state represented
421 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
422 * able to wake up the system from this sleep state. If @d_min_p is set,
423 * the highest power (lowest number) device power state of @dev allowed
424 * in this system sleep state is stored at the location pointed to by it.
426 * The caller must ensure that @dev is valid before using this function.
427 * The caller is also responsible for figuring out if the device is
428 * supposed to be able to wake up the system and passing this information
432 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
434 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
435 struct acpi_device *adev;
436 char acpi_method[] = "_SxD";
437 unsigned long d_min, d_max;
439 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
440 printk(KERN_DEBUG "ACPI handle has no context!\n");
444 acpi_method[2] = '0' + acpi_target_sleep_state;
446 * If the sleep state is S0, we will return D3, but if the device has
447 * _S0W, we will use the value from _S0W
449 d_min = ACPI_STATE_D0;
450 d_max = ACPI_STATE_D3;
453 * If present, _SxD methods return the minimum D-state (highest power
454 * state) we can use for the corresponding S-states. Otherwise, the
455 * minimum D-state is D0 (ACPI 3.x).
457 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
458 * provided -- that's our fault recovery, we ignore retval.
460 if (acpi_target_sleep_state > ACPI_STATE_S0)
461 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
464 * If _PRW says we can wake up the system from the target sleep state,
465 * the D-state returned by _SxD is sufficient for that (we assume a
466 * wakeup-aware driver if wake is set). Still, if _SxW exists
467 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
468 * can wake the system. _S0W may be valid, too.
470 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
471 (wake && adev->wakeup.state.enabled &&
472 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
473 acpi_method[3] = 'W';
474 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
486 static void acpi_power_off_prepare(void)
488 /* Prepare to power off the system */
489 acpi_sleep_prepare(ACPI_STATE_S5);
490 acpi_hw_disable_all_gpes();
493 static void acpi_power_off(void)
495 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
496 printk("%s called\n", __FUNCTION__);
498 acpi_enable_wakeup_device(ACPI_STATE_S5);
499 acpi_enter_sleep_state(ACPI_STATE_S5);
502 int __init acpi_sleep_init(void)
506 #ifdef CONFIG_SUSPEND
509 dmi_check_system(acpisleep_dmi_table);
515 sleep_states[ACPI_STATE_S0] = 1;
516 printk(KERN_INFO PREFIX "(supports S0");
518 #ifdef CONFIG_SUSPEND
519 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
520 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
521 if (ACPI_SUCCESS(status)) {
527 suspend_set_ops(&acpi_pm_ops);
530 #ifdef CONFIG_HIBERNATION
531 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
532 if (ACPI_SUCCESS(status)) {
533 hibernation_set_ops(&acpi_hibernation_ops);
534 sleep_states[ACPI_STATE_S4] = 1;
538 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
539 if (ACPI_SUCCESS(status)) {
540 sleep_states[ACPI_STATE_S5] = 1;
542 pm_power_off_prepare = acpi_power_off_prepare;
543 pm_power_off = acpi_power_off;