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
];
26 static u32 acpi_target_sleep_state
= ACPI_STATE_S0
;
28 static int acpi_sleep_prepare(u32 acpi_state
)
30 #ifdef CONFIG_ACPI_SLEEP
31 /* do we have a wakeup address for S2 and S3? */
32 if (acpi_state
== ACPI_STATE_S3
) {
33 if (!acpi_wakeup_address
) {
36 acpi_set_firmware_waking_vector(
37 (acpi_physical_address
)acpi_wakeup_address
);
40 ACPI_FLUSH_CPU_CACHE();
41 acpi_enable_wakeup_device_prep(acpi_state
);
43 printk(KERN_INFO PREFIX
"Preparing to enter system sleep state S%d\n",
45 acpi_enter_sleep_state_prep(acpi_state
);
49 #ifdef CONFIG_ACPI_SLEEP
51 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
52 * user to request that behavior by using the 'acpi_old_suspend_ordering'
53 * kernel command line option that causes the following variable to be set.
55 static bool old_suspend_ordering
;
57 void __init
acpi_old_suspend_ordering(void)
59 old_suspend_ordering
= true;
63 * acpi_pm_disable_gpes - Disable the GPEs.
65 static int acpi_pm_disable_gpes(void)
67 acpi_hw_disable_all_gpes();
72 * __acpi_pm_prepare - Prepare the platform to enter the target state.
74 * If necessary, set the firmware waking vector and do arch-specific
75 * nastiness to get the wakeup code to the waking vector.
77 static int __acpi_pm_prepare(void)
79 int error
= acpi_sleep_prepare(acpi_target_sleep_state
);
82 acpi_target_sleep_state
= ACPI_STATE_S0
;
87 * acpi_pm_prepare - Prepare the platform to enter the target sleep
88 * state and disable the GPEs.
90 static int acpi_pm_prepare(void)
92 int error
= __acpi_pm_prepare();
95 acpi_hw_disable_all_gpes();
100 * acpi_pm_finish - Instruct the platform to leave a sleep state.
102 * This is called after we wake back up (or if entering the sleep state
105 static void acpi_pm_finish(void)
107 u32 acpi_state
= acpi_target_sleep_state
;
109 if (acpi_state
== ACPI_STATE_S0
)
112 printk(KERN_INFO PREFIX
"Waking up from system sleep state S%d\n",
114 acpi_disable_wakeup_device(acpi_state
);
115 acpi_leave_sleep_state(acpi_state
);
117 /* reset firmware waking vector */
118 acpi_set_firmware_waking_vector((acpi_physical_address
) 0);
120 acpi_target_sleep_state
= ACPI_STATE_S0
;
124 * acpi_pm_end - Finish up suspend sequence.
126 static void acpi_pm_end(void)
129 * This is necessary in case acpi_pm_finish() is not called during a
130 * failing transition to a sleep state.
132 acpi_target_sleep_state
= ACPI_STATE_S0
;
134 #endif /* CONFIG_ACPI_SLEEP */
136 #ifdef CONFIG_SUSPEND
137 extern void do_suspend_lowlevel(void);
139 static u32 acpi_suspend_states
[] = {
140 [PM_SUSPEND_ON
] = ACPI_STATE_S0
,
141 [PM_SUSPEND_STANDBY
] = ACPI_STATE_S1
,
142 [PM_SUSPEND_MEM
] = ACPI_STATE_S3
,
143 [PM_SUSPEND_MAX
] = ACPI_STATE_S5
147 * acpi_suspend_begin - Set the target system sleep state to the state
148 * associated with given @pm_state, if supported.
150 static int acpi_suspend_begin(suspend_state_t pm_state
)
152 u32 acpi_state
= acpi_suspend_states
[pm_state
];
155 if (sleep_states
[acpi_state
]) {
156 acpi_target_sleep_state
= acpi_state
;
158 printk(KERN_ERR
"ACPI does not support this state: %d\n",
166 * acpi_suspend_enter - Actually enter a sleep state.
169 * Flush caches and go to sleep. For STR we have to call arch-specific
170 * assembly, which in turn call acpi_enter_sleep_state().
171 * It's unfortunate, but it works. Please fix if you're feeling frisky.
173 static int acpi_suspend_enter(suspend_state_t pm_state
)
175 acpi_status status
= AE_OK
;
176 unsigned long flags
= 0;
177 u32 acpi_state
= acpi_target_sleep_state
;
179 ACPI_FLUSH_CPU_CACHE();
181 /* Do arch specific saving of state. */
182 if (acpi_state
== ACPI_STATE_S3
) {
183 int error
= acpi_save_state_mem();
189 local_irq_save(flags
);
190 acpi_enable_wakeup_device(acpi_state
);
191 switch (acpi_state
) {
194 status
= acpi_enter_sleep_state(acpi_state
);
198 do_suspend_lowlevel();
202 /* Reprogram control registers and execute _BFS */
203 acpi_leave_sleep_state_prep(acpi_state
);
205 /* ACPI 3.0 specs (P62) says that it's the responsibility
206 * of the OSPM to clear the status bit [ implying that the
207 * POWER_BUTTON event should not reach userspace ]
209 if (ACPI_SUCCESS(status
) && (acpi_state
== ACPI_STATE_S3
))
210 acpi_clear_event(ACPI_EVENT_POWER_BUTTON
);
213 * Disable and clear GPE status before interrupt is enabled. Some GPEs
214 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
215 * acpi_leave_sleep_state will reenable specific GPEs later
217 acpi_hw_disable_all_gpes();
219 local_irq_restore(flags
);
220 printk(KERN_DEBUG
"Back to C!\n");
222 /* restore processor state */
223 if (acpi_state
== ACPI_STATE_S3
)
224 acpi_restore_state_mem();
226 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
229 static int acpi_suspend_state_valid(suspend_state_t pm_state
)
235 case PM_SUSPEND_STANDBY
:
237 acpi_state
= acpi_suspend_states
[pm_state
];
239 return sleep_states
[acpi_state
];
245 static struct platform_suspend_ops acpi_suspend_ops
= {
246 .valid
= acpi_suspend_state_valid
,
247 .begin
= acpi_suspend_begin
,
248 .prepare
= acpi_pm_prepare
,
249 .enter
= acpi_suspend_enter
,
250 .finish
= acpi_pm_finish
,
255 * acpi_suspend_begin_old - Set the target system sleep state to the
256 * state associated with given @pm_state, if supported, and
257 * execute the _PTS control method. This function is used if the
258 * pre-ACPI 2.0 suspend ordering has been requested.
260 static int acpi_suspend_begin_old(suspend_state_t pm_state
)
262 int error
= acpi_suspend_begin(pm_state
);
265 error
= __acpi_pm_prepare();
270 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
273 static struct platform_suspend_ops acpi_suspend_ops_old
= {
274 .valid
= acpi_suspend_state_valid
,
275 .begin
= acpi_suspend_begin_old
,
276 .prepare
= acpi_pm_disable_gpes
,
277 .enter
= acpi_suspend_enter
,
278 .finish
= acpi_pm_finish
,
280 .recover
= acpi_pm_finish
,
283 static int __init
init_old_suspend_ordering(const struct dmi_system_id
*d
)
285 old_suspend_ordering
= true;
289 static struct dmi_system_id __initdata acpisleep_dmi_table
[] = {
291 .callback
= init_old_suspend_ordering
,
292 .ident
= "Abit KN9 (nForce4 variant)",
294 DMI_MATCH(DMI_BOARD_VENDOR
, "http://www.abit.com.tw/"),
295 DMI_MATCH(DMI_BOARD_NAME
, "KN9 Series(NF-CK804)"),
300 #endif /* CONFIG_SUSPEND */
302 #ifdef CONFIG_HIBERNATION
303 static unsigned long s4_hardware_signature
;
304 static struct acpi_table_facs
*facs
;
305 static bool nosigcheck
;
307 void __init
acpi_no_s4_hw_signature(void)
312 static int acpi_hibernation_begin(void)
314 acpi_target_sleep_state
= ACPI_STATE_S4
;
318 static int acpi_hibernation_enter(void)
320 acpi_status status
= AE_OK
;
321 unsigned long flags
= 0;
323 ACPI_FLUSH_CPU_CACHE();
325 local_irq_save(flags
);
326 acpi_enable_wakeup_device(ACPI_STATE_S4
);
327 /* This shouldn't return. If it returns, we have a problem */
328 status
= acpi_enter_sleep_state(ACPI_STATE_S4
);
329 /* Reprogram control registers and execute _BFS */
330 acpi_leave_sleep_state_prep(ACPI_STATE_S4
);
331 local_irq_restore(flags
);
333 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
336 static void acpi_hibernation_leave(void)
339 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
343 /* Reprogram control registers and execute _BFS */
344 acpi_leave_sleep_state_prep(ACPI_STATE_S4
);
345 /* Check the hardware signature */
346 if (facs
&& s4_hardware_signature
!= facs
->hardware_signature
) {
347 printk(KERN_EMERG
"ACPI: Hardware changed while hibernated, "
349 panic("ACPI S4 hardware signature mismatch");
353 static void acpi_pm_enable_gpes(void)
355 acpi_hw_enable_all_runtime_gpes();
358 static struct platform_hibernation_ops acpi_hibernation_ops
= {
359 .begin
= acpi_hibernation_begin
,
361 .pre_snapshot
= acpi_pm_prepare
,
362 .finish
= acpi_pm_finish
,
363 .prepare
= acpi_pm_prepare
,
364 .enter
= acpi_hibernation_enter
,
365 .leave
= acpi_hibernation_leave
,
366 .pre_restore
= acpi_pm_disable_gpes
,
367 .restore_cleanup
= acpi_pm_enable_gpes
,
371 * acpi_hibernation_begin_old - Set the target system sleep state to
372 * ACPI_STATE_S4 and execute the _PTS control method. This
373 * function is used if the pre-ACPI 2.0 suspend ordering has been
376 static int acpi_hibernation_begin_old(void)
378 int error
= acpi_sleep_prepare(ACPI_STATE_S4
);
381 acpi_target_sleep_state
= ACPI_STATE_S4
;
386 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
389 static struct platform_hibernation_ops acpi_hibernation_ops_old
= {
390 .begin
= acpi_hibernation_begin_old
,
392 .pre_snapshot
= acpi_pm_disable_gpes
,
393 .finish
= acpi_pm_finish
,
394 .prepare
= acpi_pm_disable_gpes
,
395 .enter
= acpi_hibernation_enter
,
396 .leave
= acpi_hibernation_leave
,
397 .pre_restore
= acpi_pm_disable_gpes
,
398 .restore_cleanup
= acpi_pm_enable_gpes
,
399 .recover
= acpi_pm_finish
,
401 #endif /* CONFIG_HIBERNATION */
403 int acpi_suspend(u32 acpi_state
)
405 suspend_state_t states
[] = {
406 [1] = PM_SUSPEND_STANDBY
,
407 [3] = PM_SUSPEND_MEM
,
411 if (acpi_state
< 6 && states
[acpi_state
])
412 return pm_suspend(states
[acpi_state
]);
418 #ifdef CONFIG_PM_SLEEP
420 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
421 * in the system sleep state given by %acpi_target_sleep_state
422 * @dev: device to examine; its driver model wakeup flags control
423 * whether it should be able to wake up the system
424 * @d_min_p: used to store the upper limit of allowed states range
425 * Return value: preferred power state of the device on success, -ENODEV on
426 * failure (ie. if there's no 'struct acpi_device' for @dev)
428 * Find the lowest power (highest number) ACPI device power state that
429 * device @dev can be in while the system is in the sleep state represented
430 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
431 * able to wake up the system from this sleep state. If @d_min_p is set,
432 * the highest power (lowest number) device power state of @dev allowed
433 * in this system sleep state is stored at the location pointed to by it.
435 * The caller must ensure that @dev is valid before using this function.
436 * The caller is also responsible for figuring out if the device is
437 * supposed to be able to wake up the system and passing this information
441 int acpi_pm_device_sleep_state(struct device
*dev
, int *d_min_p
)
443 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
444 struct acpi_device
*adev
;
445 char acpi_method
[] = "_SxD";
446 unsigned long d_min
, d_max
;
448 if (!handle
|| ACPI_FAILURE(acpi_bus_get_device(handle
, &adev
))) {
449 printk(KERN_DEBUG
"ACPI handle has no context!\n");
453 acpi_method
[2] = '0' + acpi_target_sleep_state
;
455 * If the sleep state is S0, we will return D3, but if the device has
456 * _S0W, we will use the value from _S0W
458 d_min
= ACPI_STATE_D0
;
459 d_max
= ACPI_STATE_D3
;
462 * If present, _SxD methods return the minimum D-state (highest power
463 * state) we can use for the corresponding S-states. Otherwise, the
464 * minimum D-state is D0 (ACPI 3.x).
466 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
467 * provided -- that's our fault recovery, we ignore retval.
469 if (acpi_target_sleep_state
> ACPI_STATE_S0
)
470 acpi_evaluate_integer(handle
, acpi_method
, NULL
, &d_min
);
473 * If _PRW says we can wake up the system from the target sleep state,
474 * the D-state returned by _SxD is sufficient for that (we assume a
475 * wakeup-aware driver if wake is set). Still, if _SxW exists
476 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
477 * can wake the system. _S0W may be valid, too.
479 if (acpi_target_sleep_state
== ACPI_STATE_S0
||
480 (device_may_wakeup(dev
) && adev
->wakeup
.state
.enabled
&&
481 adev
->wakeup
.sleep_state
<= acpi_target_sleep_state
)) {
484 acpi_method
[3] = 'W';
485 status
= acpi_evaluate_integer(handle
, acpi_method
, NULL
,
487 if (ACPI_FAILURE(status
)) {
489 } else if (d_max
< d_min
) {
490 /* Warn the user of the broken DSDT */
491 printk(KERN_WARNING
"ACPI: Wrong value from %s\n",
504 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
505 * capability of given device
506 * @dev: device to handle
507 * @enable: 'true' - enable, 'false' - disable the wake-up capability
509 int acpi_pm_device_sleep_wake(struct device
*dev
, bool enable
)
512 struct acpi_device
*adev
;
514 if (!device_may_wakeup(dev
))
517 handle
= DEVICE_ACPI_HANDLE(dev
);
518 if (!handle
|| ACPI_FAILURE(acpi_bus_get_device(handle
, &adev
))) {
519 printk(KERN_DEBUG
"ACPI handle has no context!\n");
524 acpi_enable_wakeup_device_power(adev
, acpi_target_sleep_state
) :
525 acpi_disable_wakeup_device_power(adev
);
529 static void acpi_power_off_prepare(void)
531 /* Prepare to power off the system */
532 acpi_sleep_prepare(ACPI_STATE_S5
);
533 acpi_hw_disable_all_gpes();
536 static void acpi_power_off(void)
538 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
539 printk("%s called\n", __func__
);
541 acpi_enable_wakeup_device(ACPI_STATE_S5
);
542 acpi_enter_sleep_state(ACPI_STATE_S5
);
545 int __init
acpi_sleep_init(void)
549 #ifdef CONFIG_SUSPEND
552 dmi_check_system(acpisleep_dmi_table
);
558 sleep_states
[ACPI_STATE_S0
] = 1;
559 printk(KERN_INFO PREFIX
"(supports S0");
561 #ifdef CONFIG_SUSPEND
562 for (i
= ACPI_STATE_S1
; i
< ACPI_STATE_S4
; i
++) {
563 status
= acpi_get_sleep_type_data(i
, &type_a
, &type_b
);
564 if (ACPI_SUCCESS(status
)) {
570 suspend_set_ops(old_suspend_ordering
?
571 &acpi_suspend_ops_old
: &acpi_suspend_ops
);
574 #ifdef CONFIG_HIBERNATION
575 status
= acpi_get_sleep_type_data(ACPI_STATE_S4
, &type_a
, &type_b
);
576 if (ACPI_SUCCESS(status
)) {
577 hibernation_set_ops(old_suspend_ordering
?
578 &acpi_hibernation_ops_old
: &acpi_hibernation_ops
);
579 sleep_states
[ACPI_STATE_S4
] = 1;
582 acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS
,
583 (struct acpi_table_header
**)&facs
);
585 s4_hardware_signature
=
586 facs
->hardware_signature
;
590 status
= acpi_get_sleep_type_data(ACPI_STATE_S5
, &type_a
, &type_b
);
591 if (ACPI_SUCCESS(status
)) {
592 sleep_states
[ACPI_STATE_S5
] = 1;
594 pm_power_off_prepare
= acpi_power_off_prepare
;
595 pm_power_off
= acpi_power_off
;