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
64 static int init_8259A_after_S1;
67 * acpi_suspend_begin - Set the target system sleep state to the state
68 * associated with given @pm_state, if supported.
71 static int acpi_suspend_begin(suspend_state_t pm_state)
73 u32 acpi_state = acpi_suspend_states[pm_state];
76 if (sleep_states[acpi_state]) {
77 acpi_target_sleep_state = acpi_state;
79 printk(KERN_ERR "ACPI does not support this state: %d\n",
87 * acpi_suspend_prepare - Do preliminary suspend work.
89 * If necessary, set the firmware waking vector and do arch-specific
90 * nastiness to get the wakeup code to the waking vector.
93 static int acpi_suspend_prepare(void)
95 int error = acpi_sleep_prepare(acpi_target_sleep_state);
98 acpi_target_sleep_state = ACPI_STATE_S0;
102 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
106 * acpi_suspend_enter - Actually enter a sleep state.
109 * Flush caches and go to sleep. For STR we have to call arch-specific
110 * assembly, which in turn call acpi_enter_sleep_state().
111 * It's unfortunate, but it works. Please fix if you're feeling frisky.
114 static int acpi_suspend_enter(suspend_state_t pm_state)
116 acpi_status status = AE_OK;
117 unsigned long flags = 0;
118 u32 acpi_state = acpi_target_sleep_state;
120 ACPI_FLUSH_CPU_CACHE();
122 /* Do arch specific saving of state. */
123 if (acpi_state == ACPI_STATE_S3) {
124 int error = acpi_save_state_mem();
130 local_irq_save(flags);
131 acpi_enable_wakeup_device(acpi_state);
132 switch (acpi_state) {
135 status = acpi_enter_sleep_state(acpi_state);
139 do_suspend_lowlevel();
143 /* Reprogram control registers and execute _BFS */
144 acpi_leave_sleep_state_prep(acpi_state);
146 /* ACPI 3.0 specs (P62) says that it's the responsibility
147 * of the OSPM to clear the status bit [ implying that the
148 * POWER_BUTTON event should not reach userspace ]
150 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
151 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
154 * Disable and clear GPE status before interrupt is enabled. Some GPEs
155 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
156 * acpi_leave_sleep_state will reenable specific GPEs later
158 acpi_hw_disable_all_gpes();
160 local_irq_restore(flags);
161 printk(KERN_DEBUG "Back to C!\n");
163 /* restore processor state */
164 if (acpi_state == ACPI_STATE_S3)
165 acpi_restore_state_mem();
167 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
171 * acpi_suspend_finish - Instruct the platform to leave a sleep state.
173 * This is called after we wake back up (or if entering the sleep state
177 static void acpi_suspend_finish(void)
179 u32 acpi_state = acpi_target_sleep_state;
181 acpi_disable_wakeup_device(acpi_state);
182 acpi_leave_sleep_state(acpi_state);
184 /* reset firmware waking vector */
185 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
187 acpi_target_sleep_state = ACPI_STATE_S0;
190 if (init_8259A_after_S1) {
191 printk("Broken toshiba laptop -> kicking interrupts\n");
198 * acpi_suspend_end - Finish up suspend sequence.
201 static void acpi_suspend_end(void)
204 * This is necessary in case acpi_suspend_finish() is not called during a
205 * failing transition to a sleep state.
207 acpi_target_sleep_state = ACPI_STATE_S0;
210 static int acpi_suspend_state_valid(suspend_state_t pm_state)
216 case PM_SUSPEND_STANDBY:
218 acpi_state = acpi_suspend_states[pm_state];
220 return sleep_states[acpi_state];
226 static struct platform_suspend_ops acpi_suspend_ops = {
227 .valid = acpi_suspend_state_valid,
228 .begin = acpi_suspend_begin,
229 .prepare = acpi_suspend_prepare,
230 .enter = acpi_suspend_enter,
231 .finish = acpi_suspend_finish,
232 .end = acpi_suspend_end,
236 * Toshiba fails to preserve interrupts over S1, reinitialization
237 * of 8259 is needed after S1 resume.
239 static int __init init_ints_after_s1(const struct dmi_system_id *d)
241 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
242 init_8259A_after_S1 = 1;
246 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
248 .callback = init_ints_after_s1,
249 .ident = "Toshiba Satellite 4030cdt",
250 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
254 #endif /* CONFIG_SUSPEND */
256 #ifdef CONFIG_HIBERNATION
257 static int acpi_hibernation_begin(void)
259 acpi_target_sleep_state = ACPI_STATE_S4;
264 static int acpi_hibernation_prepare(void)
266 int error = acpi_sleep_prepare(ACPI_STATE_S4);
269 acpi_target_sleep_state = ACPI_STATE_S0;
273 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
276 static int acpi_hibernation_enter(void)
278 acpi_status status = AE_OK;
279 unsigned long flags = 0;
281 ACPI_FLUSH_CPU_CACHE();
283 local_irq_save(flags);
284 acpi_enable_wakeup_device(ACPI_STATE_S4);
285 /* This shouldn't return. If it returns, we have a problem */
286 status = acpi_enter_sleep_state(ACPI_STATE_S4);
287 /* Reprogram control registers and execute _BFS */
288 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
289 local_irq_restore(flags);
291 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
294 static void acpi_hibernation_leave(void)
297 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
301 /* Reprogram control registers and execute _BFS */
302 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
305 static void acpi_hibernation_finish(void)
307 acpi_disable_wakeup_device(ACPI_STATE_S4);
308 acpi_leave_sleep_state(ACPI_STATE_S4);
310 /* reset firmware waking vector */
311 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
313 acpi_target_sleep_state = ACPI_STATE_S0;
316 static void acpi_hibernation_end(void)
319 * This is necessary in case acpi_hibernation_finish() is not called
320 * during a failing transition to the sleep state.
322 acpi_target_sleep_state = ACPI_STATE_S0;
325 static int acpi_hibernation_pre_restore(void)
329 status = acpi_hw_disable_all_gpes();
331 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
334 static void acpi_hibernation_restore_cleanup(void)
336 acpi_hw_enable_all_runtime_gpes();
339 static struct platform_hibernation_ops acpi_hibernation_ops = {
340 .begin = acpi_hibernation_begin,
341 .end = acpi_hibernation_end,
342 .pre_snapshot = acpi_hibernation_prepare,
343 .finish = acpi_hibernation_finish,
344 .prepare = acpi_hibernation_prepare,
345 .enter = acpi_hibernation_enter,
346 .leave = acpi_hibernation_leave,
347 .pre_restore = acpi_hibernation_pre_restore,
348 .restore_cleanup = acpi_hibernation_restore_cleanup,
350 #endif /* CONFIG_HIBERNATION */
352 int acpi_suspend(u32 acpi_state)
354 suspend_state_t states[] = {
355 [1] = PM_SUSPEND_STANDBY,
356 [3] = PM_SUSPEND_MEM,
360 if (acpi_state < 6 && states[acpi_state])
361 return pm_suspend(states[acpi_state]);
367 #ifdef CONFIG_PM_SLEEP
369 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
370 * in the system sleep state given by %acpi_target_sleep_state
371 * @dev: device to examine
372 * @wake: if set, the device should be able to wake up the system
373 * @d_min_p: used to store the upper limit of allowed states range
374 * Return value: preferred power state of the device on success, -ENODEV on
375 * failure (ie. if there's no 'struct acpi_device' for @dev)
377 * Find the lowest power (highest number) ACPI device power state that
378 * device @dev can be in while the system is in the sleep state represented
379 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
380 * able to wake up the system from this sleep state. If @d_min_p is set,
381 * the highest power (lowest number) device power state of @dev allowed
382 * in this system sleep state is stored at the location pointed to by it.
384 * The caller must ensure that @dev is valid before using this function.
385 * The caller is also responsible for figuring out if the device is
386 * supposed to be able to wake up the system and passing this information
390 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
392 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
393 struct acpi_device *adev;
394 char acpi_method[] = "_SxD";
395 unsigned long d_min, d_max;
397 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
398 printk(KERN_DEBUG "ACPI handle has no context!\n");
402 acpi_method[2] = '0' + acpi_target_sleep_state;
404 * If the sleep state is S0, we will return D3, but if the device has
405 * _S0W, we will use the value from _S0W
407 d_min = ACPI_STATE_D0;
408 d_max = ACPI_STATE_D3;
411 * If present, _SxD methods return the minimum D-state (highest power
412 * state) we can use for the corresponding S-states. Otherwise, the
413 * minimum D-state is D0 (ACPI 3.x).
415 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
416 * provided -- that's our fault recovery, we ignore retval.
418 if (acpi_target_sleep_state > ACPI_STATE_S0)
419 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
422 * If _PRW says we can wake up the system from the target sleep state,
423 * the D-state returned by _SxD is sufficient for that (we assume a
424 * wakeup-aware driver if wake is set). Still, if _SxW exists
425 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
426 * can wake the system. _S0W may be valid, too.
428 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
429 (wake && adev->wakeup.state.enabled &&
430 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
433 acpi_method[3] = 'W';
434 status = acpi_evaluate_integer(handle, acpi_method, NULL,
436 if (ACPI_FAILURE(status)) {
438 } else if (d_max < d_min) {
439 /* Warn the user of the broken DSDT */
440 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
453 static void acpi_power_off_prepare(void)
455 /* Prepare to power off the system */
456 acpi_sleep_prepare(ACPI_STATE_S5);
457 acpi_hw_disable_all_gpes();
460 static void acpi_power_off(void)
462 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
463 printk("%s called\n", __func__);
465 acpi_enable_wakeup_device(ACPI_STATE_S5);
466 acpi_enter_sleep_state(ACPI_STATE_S5);
469 int __init acpi_sleep_init(void)
473 #ifdef CONFIG_SUSPEND
476 dmi_check_system(acpisleep_dmi_table);
482 sleep_states[ACPI_STATE_S0] = 1;
483 printk(KERN_INFO PREFIX "(supports S0");
485 #ifdef CONFIG_SUSPEND
486 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
487 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
488 if (ACPI_SUCCESS(status)) {
494 suspend_set_ops(&acpi_suspend_ops);
497 #ifdef CONFIG_HIBERNATION
498 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
499 if (ACPI_SUCCESS(status)) {
500 hibernation_set_ops(&acpi_hibernation_ops);
501 sleep_states[ACPI_STATE_S4] = 1;
505 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
506 if (ACPI_SUCCESS(status)) {
507 sleep_states[ACPI_STATE_S5] = 1;
509 pm_power_off_prepare = acpi_power_off_prepare;
510 pm_power_off = acpi_power_off;