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_enter_sleep_state_prep(acpi_state
);
52 static struct platform_suspend_ops acpi_pm_ops
;
54 extern void do_suspend_lowlevel(void);
56 static u32 acpi_suspend_states
[] = {
57 [PM_SUSPEND_ON
] = ACPI_STATE_S0
,
58 [PM_SUSPEND_STANDBY
] = ACPI_STATE_S1
,
59 [PM_SUSPEND_MEM
] = ACPI_STATE_S3
,
60 [PM_SUSPEND_MAX
] = ACPI_STATE_S5
63 static int init_8259A_after_S1
;
66 * acpi_pm_begin - Set the target system sleep state to the state
67 * associated with given @pm_state, if supported.
70 static int acpi_pm_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_pm_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_pm_prepare(void)
96 error
= acpi_sleep_prepare(acpi_target_sleep_state
);
98 acpi_target_sleep_state
= ACPI_STATE_S0
;
99 else if (!ACPI_SUCCESS(acpi_hw_disable_all_gpes()))
106 * acpi_pm_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_pm_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();
127 acpi_target_sleep_state
= ACPI_STATE_S0
;
132 local_irq_save(flags
);
133 acpi_enable_wakeup_device(acpi_state
);
134 switch (acpi_state
) {
137 status
= acpi_enter_sleep_state(acpi_state
);
141 do_suspend_lowlevel();
145 /* Reprogram control registers and execute _BFS */
146 acpi_leave_sleep_state_prep(acpi_state
);
148 /* ACPI 3.0 specs (P62) says that it's the responsabilty
149 * of the OSPM to clear the status bit [ implying that the
150 * POWER_BUTTON event should not reach userspace ]
152 if (ACPI_SUCCESS(status
) && (acpi_state
== ACPI_STATE_S3
))
153 acpi_clear_event(ACPI_EVENT_POWER_BUTTON
);
156 * Disable and clear GPE status before interrupt is enabled. Some GPEs
157 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
158 * acpi_leave_sleep_state will reenable specific GPEs later
160 acpi_hw_disable_all_gpes();
162 local_irq_restore(flags
);
163 printk(KERN_DEBUG
"Back to C!\n");
165 /* restore processor state */
166 if (acpi_state
== ACPI_STATE_S3
)
167 acpi_restore_state_mem();
169 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
173 * acpi_pm_finish - Instruct the platform to leave a sleep state.
175 * This is called after we wake back up (or if entering the sleep state
179 static void acpi_pm_finish(void)
181 u32 acpi_state
= acpi_target_sleep_state
;
183 acpi_disable_wakeup_device(acpi_state
);
184 acpi_leave_sleep_state(acpi_state
);
186 /* reset firmware waking vector */
187 acpi_set_firmware_waking_vector((acpi_physical_address
) 0);
189 acpi_target_sleep_state
= ACPI_STATE_S0
;
192 if (init_8259A_after_S1
) {
193 printk("Broken toshiba laptop -> kicking interrupts\n");
200 * acpi_pm_end - Finish up suspend sequence.
203 static void acpi_pm_end(void)
206 * This is necessary in case acpi_pm_finish() is not called during a
207 * failing transition to a sleep state.
209 acpi_target_sleep_state
= ACPI_STATE_S0
;
212 static int acpi_pm_state_valid(suspend_state_t pm_state
)
218 case PM_SUSPEND_STANDBY
:
220 acpi_state
= acpi_suspend_states
[pm_state
];
222 return sleep_states
[acpi_state
];
228 static struct platform_suspend_ops acpi_pm_ops
= {
229 .valid
= acpi_pm_state_valid
,
230 .begin
= acpi_pm_begin
,
231 .prepare
= acpi_pm_prepare
,
232 .enter
= acpi_pm_enter
,
233 .finish
= acpi_pm_finish
,
238 * Toshiba fails to preserve interrupts over S1, reinitialization
239 * of 8259 is needed after S1 resume.
241 static int __init
init_ints_after_s1(const struct dmi_system_id
*d
)
243 printk(KERN_WARNING
"%s with broken S1 detected.\n", d
->ident
);
244 init_8259A_after_S1
= 1;
248 static struct dmi_system_id __initdata acpisleep_dmi_table
[] = {
250 .callback
= init_ints_after_s1
,
251 .ident
= "Toshiba Satellite 4030cdt",
252 .matches
= {DMI_MATCH(DMI_PRODUCT_NAME
, "S4030CDT/4.3"),},
256 #endif /* CONFIG_SUSPEND */
258 #ifdef CONFIG_HIBERNATION
259 static int acpi_hibernation_start(void)
261 acpi_target_sleep_state
= ACPI_STATE_S4
;
265 static int acpi_hibernation_prepare(void)
269 error
= acpi_sleep_prepare(ACPI_STATE_S4
);
273 if (!ACPI_SUCCESS(acpi_hw_disable_all_gpes()))
279 static int acpi_hibernation_enter(void)
281 acpi_status status
= AE_OK
;
282 unsigned long flags
= 0;
284 ACPI_FLUSH_CPU_CACHE();
286 local_irq_save(flags
);
287 acpi_enable_wakeup_device(ACPI_STATE_S4
);
288 /* This shouldn't return. If it returns, we have a problem */
289 status
= acpi_enter_sleep_state(ACPI_STATE_S4
);
290 /* Reprogram control registers and execute _BFS */
291 acpi_leave_sleep_state_prep(ACPI_STATE_S4
);
292 local_irq_restore(flags
);
294 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
297 static void acpi_hibernation_leave(void)
300 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
304 /* Reprogram control registers and execute _BFS */
305 acpi_leave_sleep_state_prep(ACPI_STATE_S4
);
308 static void acpi_hibernation_finish(void)
310 acpi_disable_wakeup_device(ACPI_STATE_S4
);
311 acpi_leave_sleep_state(ACPI_STATE_S4
);
313 /* reset firmware waking vector */
314 acpi_set_firmware_waking_vector((acpi_physical_address
) 0);
316 acpi_target_sleep_state
= ACPI_STATE_S0
;
319 static int acpi_hibernation_pre_restore(void)
323 status
= acpi_hw_disable_all_gpes();
325 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
328 static void acpi_hibernation_restore_cleanup(void)
330 acpi_hw_enable_all_runtime_gpes();
333 static struct platform_hibernation_ops acpi_hibernation_ops
= {
334 .start
= acpi_hibernation_start
,
335 .pre_snapshot
= acpi_hibernation_prepare
,
336 .finish
= acpi_hibernation_finish
,
337 .prepare
= acpi_hibernation_prepare
,
338 .enter
= acpi_hibernation_enter
,
339 .leave
= acpi_hibernation_leave
,
340 .pre_restore
= acpi_hibernation_pre_restore
,
341 .restore_cleanup
= acpi_hibernation_restore_cleanup
,
343 #endif /* CONFIG_HIBERNATION */
345 int acpi_suspend(u32 acpi_state
)
347 suspend_state_t states
[] = {
348 [1] = PM_SUSPEND_STANDBY
,
349 [3] = PM_SUSPEND_MEM
,
353 if (acpi_state
< 6 && states
[acpi_state
])
354 return pm_suspend(states
[acpi_state
]);
360 #ifdef CONFIG_PM_SLEEP
362 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
363 * in the system sleep state given by %acpi_target_sleep_state
364 * @dev: device to examine
365 * @wake: if set, the device should be able to wake up the system
366 * @d_min_p: used to store the upper limit of allowed states range
367 * Return value: preferred power state of the device on success, -ENODEV on
368 * failure (ie. if there's no 'struct acpi_device' for @dev)
370 * Find the lowest power (highest number) ACPI device power state that
371 * device @dev can be in while the system is in the sleep state represented
372 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
373 * able to wake up the system from this sleep state. If @d_min_p is set,
374 * the highest power (lowest number) device power state of @dev allowed
375 * in this system sleep state is stored at the location pointed to by it.
377 * The caller must ensure that @dev is valid before using this function.
378 * The caller is also responsible for figuring out if the device is
379 * supposed to be able to wake up the system and passing this information
383 int acpi_pm_device_sleep_state(struct device
*dev
, int wake
, int *d_min_p
)
385 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
386 struct acpi_device
*adev
;
387 char acpi_method
[] = "_SxD";
388 unsigned long d_min
, d_max
;
390 if (!handle
|| ACPI_FAILURE(acpi_bus_get_device(handle
, &adev
))) {
391 printk(KERN_DEBUG
"ACPI handle has no context!\n");
395 acpi_method
[2] = '0' + acpi_target_sleep_state
;
397 * If the sleep state is S0, we will return D3, but if the device has
398 * _S0W, we will use the value from _S0W
400 d_min
= ACPI_STATE_D0
;
401 d_max
= ACPI_STATE_D3
;
404 * If present, _SxD methods return the minimum D-state (highest power
405 * state) we can use for the corresponding S-states. Otherwise, the
406 * minimum D-state is D0 (ACPI 3.x).
408 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
409 * provided -- that's our fault recovery, we ignore retval.
411 if (acpi_target_sleep_state
> ACPI_STATE_S0
)
412 acpi_evaluate_integer(handle
, acpi_method
, NULL
, &d_min
);
415 * If _PRW says we can wake up the system from the target sleep state,
416 * the D-state returned by _SxD is sufficient for that (we assume a
417 * wakeup-aware driver if wake is set). Still, if _SxW exists
418 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
419 * can wake the system. _S0W may be valid, too.
421 if (acpi_target_sleep_state
== ACPI_STATE_S0
||
422 (wake
&& adev
->wakeup
.state
.enabled
&&
423 adev
->wakeup
.sleep_state
<= acpi_target_sleep_state
)) {
424 acpi_method
[3] = 'W';
425 acpi_evaluate_integer(handle
, acpi_method
, NULL
, &d_max
);
437 static void acpi_power_off_prepare(void)
439 /* Prepare to power off the system */
440 acpi_sleep_prepare(ACPI_STATE_S5
);
441 acpi_hw_disable_all_gpes();
444 static void acpi_power_off(void)
446 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
447 printk("%s called\n", __FUNCTION__
);
449 acpi_enable_wakeup_device(ACPI_STATE_S5
);
450 acpi_enter_sleep_state(ACPI_STATE_S5
);
453 int __init
acpi_sleep_init(void)
457 #ifdef CONFIG_SUSPEND
460 dmi_check_system(acpisleep_dmi_table
);
466 sleep_states
[ACPI_STATE_S0
] = 1;
467 printk(KERN_INFO PREFIX
"(supports S0");
469 #ifdef CONFIG_SUSPEND
470 for (i
= ACPI_STATE_S1
; i
< ACPI_STATE_S4
; i
++) {
471 status
= acpi_get_sleep_type_data(i
, &type_a
, &type_b
);
472 if (ACPI_SUCCESS(status
)) {
478 suspend_set_ops(&acpi_pm_ops
);
481 #ifdef CONFIG_HIBERNATION
482 status
= acpi_get_sleep_type_data(ACPI_STATE_S4
, &type_a
, &type_b
);
483 if (ACPI_SUCCESS(status
)) {
484 hibernation_set_ops(&acpi_hibernation_ops
);
485 sleep_states
[ACPI_STATE_S4
] = 1;
489 status
= acpi_get_sleep_type_data(ACPI_STATE_S5
, &type_a
, &type_b
);
490 if (ACPI_SUCCESS(status
)) {
491 sleep_states
[ACPI_STATE_S5
] = 1;
493 pm_power_off_prepare
= acpi_power_off_prepare
;
494 pm_power_off
= acpi_power_off
;