ACPI suspend: Fix CONFIG_ACPI_SLEEP dependence and some compilation warnings
[linux-2.6/mini2440.git] / drivers / acpi / sleep / main.c
blob4c21480b58207c0132af414bb3c361204c3e0806
1 /*
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>
19 #include <asm/io.h>
21 #include <acpi/acpi_bus.h>
22 #include <acpi/acpi_drivers.h>
23 #include "sleep.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) {
34 return -EFAULT;
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);
42 #endif
43 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
44 acpi_state);
45 acpi_enter_sleep_state_prep(acpi_state);
46 return 0;
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;
62 /**
63 * acpi_pm_disable_gpes - Disable the GPEs.
65 static int acpi_pm_disable_gpes(void)
67 acpi_hw_disable_all_gpes();
68 return 0;
71 /**
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);
81 if (error)
82 acpi_target_sleep_state = ACPI_STATE_S0;
83 return error;
86 /**
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();
94 if (!error)
95 acpi_hw_disable_all_gpes();
96 return error;
99 /**
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
103 * failed).
105 static void acpi_pm_finish(void)
107 u32 acpi_state = acpi_target_sleep_state;
109 if (acpi_state == ACPI_STATE_S0)
110 return;
112 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
113 acpi_state);
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];
153 int error = 0;
155 if (sleep_states[acpi_state]) {
156 acpi_target_sleep_state = acpi_state;
157 } else {
158 printk(KERN_ERR "ACPI does not support this state: %d\n",
159 pm_state);
160 error = -ENOSYS;
162 return error;
166 * acpi_suspend_enter - Actually enter a sleep state.
167 * @pm_state: ignored
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();
185 if (error)
186 return error;
189 local_irq_save(flags);
190 acpi_enable_wakeup_device(acpi_state);
191 switch (acpi_state) {
192 case ACPI_STATE_S1:
193 barrier();
194 status = acpi_enter_sleep_state(acpi_state);
195 break;
197 case ACPI_STATE_S3:
198 do_suspend_lowlevel();
199 break;
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)
231 u32 acpi_state;
233 switch (pm_state) {
234 case PM_SUSPEND_ON:
235 case PM_SUSPEND_STANDBY:
236 case PM_SUSPEND_MEM:
237 acpi_state = acpi_suspend_states[pm_state];
239 return sleep_states[acpi_state];
240 default:
241 return 0;
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,
251 .end = acpi_pm_end,
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);
264 if (!error)
265 error = __acpi_pm_prepare();
266 return error;
270 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
271 * been requested.
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,
279 .end = acpi_pm_end,
280 .recover = acpi_pm_finish,
283 static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
285 old_suspend_ordering = true;
286 return 0;
289 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
291 .callback = init_old_suspend_ordering,
292 .ident = "Abit KN9 (nForce4 variant)",
293 .matches = {
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)
309 nosigcheck = true;
312 static int acpi_hibernation_begin(void)
314 acpi_target_sleep_state = ACPI_STATE_S4;
315 return 0;
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
340 * enable it here.
342 acpi_enable();
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, "
348 "cannot resume!\n");
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,
360 .end = acpi_pm_end,
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
374 * requested.
376 static int acpi_hibernation_begin_old(void)
378 int error = acpi_sleep_prepare(ACPI_STATE_S4);
380 if (!error)
381 acpi_target_sleep_state = ACPI_STATE_S4;
382 return error;
386 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
387 * been requested.
389 static struct platform_hibernation_ops acpi_hibernation_ops_old = {
390 .begin = acpi_hibernation_begin_old,
391 .end = acpi_pm_end,
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,
408 [5] = PM_SUSPEND_MAX
411 if (acpi_state < 6 && states[acpi_state])
412 return pm_suspend(states[acpi_state]);
413 if (acpi_state == 4)
414 return hibernate();
415 return -EINVAL;
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
438 * via @wake.
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");
450 return -ENODEV;
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)) {
482 acpi_status status;
484 acpi_method[3] = 'W';
485 status = acpi_evaluate_integer(handle, acpi_method, NULL,
486 &d_max);
487 if (ACPI_FAILURE(status)) {
488 d_max = d_min;
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",
492 acpi_method);
493 /* Sanitize it */
494 d_min = d_max;
498 if (d_min_p)
499 *d_min_p = d_min;
500 return d_max;
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)
511 acpi_handle handle;
512 struct acpi_device *adev;
514 if (!device_may_wakeup(dev))
515 return -EINVAL;
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");
520 return -ENODEV;
523 return enable ?
524 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
525 acpi_disable_wakeup_device_power(adev);
527 #endif
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__);
540 local_irq_disable();
541 acpi_enable_wakeup_device(ACPI_STATE_S5);
542 acpi_enter_sleep_state(ACPI_STATE_S5);
545 int __init acpi_sleep_init(void)
547 acpi_status status;
548 u8 type_a, type_b;
549 #ifdef CONFIG_SUSPEND
550 int i = 0;
552 dmi_check_system(acpisleep_dmi_table);
553 #endif
555 if (acpi_disabled)
556 return 0;
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)) {
565 sleep_states[i] = 1;
566 printk(" S%d", i);
570 suspend_set_ops(old_suspend_ordering ?
571 &acpi_suspend_ops_old : &acpi_suspend_ops);
572 #endif
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;
580 printk(" S4");
581 if (!nosigcheck) {
582 acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
583 (struct acpi_table_header **)&facs);
584 if (facs)
585 s4_hardware_signature =
586 facs->hardware_signature;
589 #endif
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;
593 printk(" S5");
594 pm_power_off_prepare = acpi_power_off_prepare;
595 pm_power_off = acpi_power_off;
597 printk(")\n");
598 return 0;