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 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 acpi_gpe_sleep_prepare(acpi_state);
48 acpi_enter_sleep_state_prep(acpi_state);
53 static struct platform_suspend_ops acpi_pm_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_pm_set_target - Set the target system sleep state to the state
68 * associated with given @pm_state, if supported.
71 static int acpi_pm_set_target(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_pm_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_pm_prepare(void)
95 int error = acpi_sleep_prepare(acpi_target_sleep_state);
98 acpi_target_sleep_state = ACPI_STATE_S0;
104 * acpi_pm_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_pm_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();
125 acpi_target_sleep_state = ACPI_STATE_S0;
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 /* ACPI 3.0 specs (P62) says that it's the responsabilty
144 * of the OSPM to clear the status bit [ implying that the
145 * POWER_BUTTON event should not reach userspace ]
147 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
148 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
150 local_irq_restore(flags);
151 printk(KERN_DEBUG "Back to C!\n");
153 /* restore processor state */
154 if (acpi_state == ACPI_STATE_S3)
155 acpi_restore_state_mem();
157 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
161 * acpi_pm_finish - Finish up suspend sequence.
163 * This is called after we wake back up (or if entering the sleep state
167 static void acpi_pm_finish(void)
169 u32 acpi_state = acpi_target_sleep_state;
171 acpi_leave_sleep_state(acpi_state);
172 acpi_disable_wakeup_device(acpi_state);
174 /* reset firmware waking vector */
175 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
177 acpi_target_sleep_state = ACPI_STATE_S0;
180 if (init_8259A_after_S1) {
181 printk("Broken toshiba laptop -> kicking interrupts\n");
187 static int acpi_pm_state_valid(suspend_state_t pm_state)
193 case PM_SUSPEND_STANDBY:
195 acpi_state = acpi_suspend_states[pm_state];
197 return sleep_states[acpi_state];
203 static struct platform_suspend_ops acpi_pm_ops = {
204 .valid = acpi_pm_state_valid,
205 .set_target = acpi_pm_set_target,
206 .prepare = acpi_pm_prepare,
207 .enter = acpi_pm_enter,
208 .finish = acpi_pm_finish,
212 * Toshiba fails to preserve interrupts over S1, reinitialization
213 * of 8259 is needed after S1 resume.
215 static int __init init_ints_after_s1(const struct dmi_system_id *d)
217 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
218 init_8259A_after_S1 = 1;
222 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
224 .callback = init_ints_after_s1,
225 .ident = "Toshiba Satellite 4030cdt",
226 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
230 #endif /* CONFIG_SUSPEND */
232 #ifdef CONFIG_HIBERNATION
233 static int acpi_hibernation_start(void)
235 acpi_target_sleep_state = ACPI_STATE_S4;
239 static int acpi_hibernation_prepare(void)
241 return acpi_sleep_prepare(ACPI_STATE_S4);
244 static int acpi_hibernation_enter(void)
246 acpi_status status = AE_OK;
247 unsigned long flags = 0;
249 ACPI_FLUSH_CPU_CACHE();
251 local_irq_save(flags);
252 acpi_enable_wakeup_device(ACPI_STATE_S4);
253 /* This shouldn't return. If it returns, we have a problem */
254 status = acpi_enter_sleep_state(ACPI_STATE_S4);
255 local_irq_restore(flags);
257 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
260 static void acpi_hibernation_leave(void)
263 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
269 static void acpi_hibernation_finish(void)
271 acpi_leave_sleep_state(ACPI_STATE_S4);
272 acpi_disable_wakeup_device(ACPI_STATE_S4);
274 /* reset firmware waking vector */
275 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
277 acpi_target_sleep_state = ACPI_STATE_S0;
280 static int acpi_hibernation_pre_restore(void)
284 status = acpi_hw_disable_all_gpes();
286 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
289 static void acpi_hibernation_restore_cleanup(void)
291 acpi_hw_enable_all_runtime_gpes();
294 static struct platform_hibernation_ops acpi_hibernation_ops = {
295 .start = acpi_hibernation_start,
296 .pre_snapshot = acpi_hibernation_prepare,
297 .finish = acpi_hibernation_finish,
298 .prepare = acpi_hibernation_prepare,
299 .enter = acpi_hibernation_enter,
300 .leave = acpi_hibernation_leave,
301 .pre_restore = acpi_hibernation_pre_restore,
302 .restore_cleanup = acpi_hibernation_restore_cleanup,
304 #endif /* CONFIG_HIBERNATION */
306 int acpi_suspend(u32 acpi_state)
308 suspend_state_t states[] = {
309 [1] = PM_SUSPEND_STANDBY,
310 [3] = PM_SUSPEND_MEM,
314 if (acpi_state < 6 && states[acpi_state])
315 return pm_suspend(states[acpi_state]);
321 #ifdef CONFIG_PM_SLEEP
323 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
324 * in the system sleep state given by %acpi_target_sleep_state
325 * @dev: device to examine
326 * @wake: if set, the device should be able to wake up the system
327 * @d_min_p: used to store the upper limit of allowed states range
328 * Return value: preferred power state of the device on success, -ENODEV on
329 * failure (ie. if there's no 'struct acpi_device' for @dev)
331 * Find the lowest power (highest number) ACPI device power state that
332 * device @dev can be in while the system is in the sleep state represented
333 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
334 * able to wake up the system from this sleep state. If @d_min_p is set,
335 * the highest power (lowest number) device power state of @dev allowed
336 * in this system sleep state is stored at the location pointed to by it.
338 * The caller must ensure that @dev is valid before using this function.
339 * The caller is also responsible for figuring out if the device is
340 * supposed to be able to wake up the system and passing this information
344 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
346 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
347 struct acpi_device *adev;
348 char acpi_method[] = "_SxD";
349 unsigned long d_min, d_max;
351 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
352 printk(KERN_DEBUG "ACPI handle has no context!\n");
356 acpi_method[2] = '0' + acpi_target_sleep_state;
358 * If the sleep state is S0, we will return D3, but if the device has
359 * _S0W, we will use the value from _S0W
361 d_min = ACPI_STATE_D0;
362 d_max = ACPI_STATE_D3;
365 * If present, _SxD methods return the minimum D-state (highest power
366 * state) we can use for the corresponding S-states. Otherwise, the
367 * minimum D-state is D0 (ACPI 3.x).
369 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
370 * provided -- that's our fault recovery, we ignore retval.
372 if (acpi_target_sleep_state > ACPI_STATE_S0)
373 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
376 * If _PRW says we can wake up the system from the target sleep state,
377 * the D-state returned by _SxD is sufficient for that (we assume a
378 * wakeup-aware driver if wake is set). Still, if _SxW exists
379 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
380 * can wake the system. _S0W may be valid, too.
382 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
383 (wake && adev->wakeup.state.enabled &&
384 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
385 acpi_method[3] = 'W';
386 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
398 static void acpi_power_off_prepare(void)
400 /* Prepare to power off the system */
401 acpi_sleep_prepare(ACPI_STATE_S5);
404 static void acpi_power_off(void)
406 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
407 printk("%s called\n", __FUNCTION__);
409 acpi_enter_sleep_state(ACPI_STATE_S5);
412 int __init acpi_sleep_init(void)
416 #ifdef CONFIG_SUSPEND
419 dmi_check_system(acpisleep_dmi_table);
425 sleep_states[ACPI_STATE_S0] = 1;
426 printk(KERN_INFO PREFIX "(supports S0");
428 #ifdef CONFIG_SUSPEND
429 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
430 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
431 if (ACPI_SUCCESS(status)) {
437 suspend_set_ops(&acpi_pm_ops);
440 #ifdef CONFIG_HIBERNATION
441 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
442 if (ACPI_SUCCESS(status)) {
443 hibernation_set_ops(&acpi_hibernation_ops);
444 sleep_states[ACPI_STATE_S4] = 1;
448 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
449 if (ACPI_SUCCESS(status)) {
450 sleep_states[ACPI_STATE_S5] = 1;
452 pm_power_off_prepare = acpi_power_off_prepare;
453 pm_power_off = acpi_power_off;