Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / acpi / sleep / main.c
blob71183eea7906fe05424a0500676d31107d119c4e
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];
27 #ifdef CONFIG_PM_SLEEP
28 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
29 #endif
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) {
37 return -EFAULT;
39 acpi_set_firmware_waking_vector((acpi_physical_address)
40 virt_to_phys((void *)
41 acpi_wakeup_address));
44 ACPI_FLUSH_CPU_CACHE();
45 acpi_enable_wakeup_device_prep(acpi_state);
46 #endif
47 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
48 acpi_state);
49 acpi_enter_sleep_state_prep(acpi_state);
50 return 0;
53 #ifdef CONFIG_SUSPEND
54 static struct platform_suspend_ops acpi_pm_ops;
56 extern void do_suspend_lowlevel(void);
58 static u32 acpi_suspend_states[] = {
59 [PM_SUSPEND_ON] = ACPI_STATE_S0,
60 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
61 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
62 [PM_SUSPEND_MAX] = ACPI_STATE_S5
65 static int init_8259A_after_S1;
67 /**
68 * acpi_pm_begin - Set the target system sleep state to the state
69 * associated with given @pm_state, if supported.
72 static int acpi_pm_begin(suspend_state_t pm_state)
74 u32 acpi_state = acpi_suspend_states[pm_state];
75 int error = 0;
77 if (sleep_states[acpi_state]) {
78 acpi_target_sleep_state = acpi_state;
79 } else {
80 printk(KERN_ERR "ACPI does not support this state: %d\n",
81 pm_state);
82 error = -ENOSYS;
84 return error;
87 /**
88 * acpi_pm_prepare - Do preliminary suspend work.
90 * If necessary, set the firmware waking vector and do arch-specific
91 * nastiness to get the wakeup code to the waking vector.
94 static int acpi_pm_prepare(void)
96 int error = acpi_sleep_prepare(acpi_target_sleep_state);
98 if (error) {
99 acpi_target_sleep_state = ACPI_STATE_S0;
100 return error;
103 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
107 * acpi_pm_enter - Actually enter a sleep state.
108 * @pm_state: ignored
110 * Flush caches and go to sleep. For STR we have to call arch-specific
111 * assembly, which in turn call acpi_enter_sleep_state().
112 * It's unfortunate, but it works. Please fix if you're feeling frisky.
115 static int acpi_pm_enter(suspend_state_t pm_state)
117 acpi_status status = AE_OK;
118 unsigned long flags = 0;
119 u32 acpi_state = acpi_target_sleep_state;
121 ACPI_FLUSH_CPU_CACHE();
123 /* Do arch specific saving of state. */
124 if (acpi_state == ACPI_STATE_S3) {
125 int error = acpi_save_state_mem();
127 if (error)
128 return error;
131 local_irq_save(flags);
132 acpi_enable_wakeup_device(acpi_state);
133 switch (acpi_state) {
134 case ACPI_STATE_S1:
135 barrier();
136 status = acpi_enter_sleep_state(acpi_state);
137 break;
139 case ACPI_STATE_S3:
140 do_suspend_lowlevel();
141 break;
144 /* Reprogram control registers and execute _BFS */
145 acpi_leave_sleep_state_prep(acpi_state);
147 /* ACPI 3.0 specs (P62) says that it's the responsibility
148 * of the OSPM to clear the status bit [ implying that the
149 * POWER_BUTTON event should not reach userspace ]
151 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
152 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
155 * Disable and clear GPE status before interrupt is enabled. Some GPEs
156 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
157 * acpi_leave_sleep_state will reenable specific GPEs later
159 acpi_hw_disable_all_gpes();
161 local_irq_restore(flags);
162 printk(KERN_DEBUG "Back to C!\n");
164 /* restore processor state */
165 if (acpi_state == ACPI_STATE_S3)
166 acpi_restore_state_mem();
168 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
172 * acpi_pm_finish - Instruct the platform to leave a sleep state.
174 * This is called after we wake back up (or if entering the sleep state
175 * failed).
178 static void acpi_pm_finish(void)
180 u32 acpi_state = acpi_target_sleep_state;
182 acpi_disable_wakeup_device(acpi_state);
183 acpi_leave_sleep_state(acpi_state);
185 /* reset firmware waking vector */
186 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
188 acpi_target_sleep_state = ACPI_STATE_S0;
190 #ifdef CONFIG_X86
191 if (init_8259A_after_S1) {
192 printk("Broken toshiba laptop -> kicking interrupts\n");
193 init_8259A(0);
195 #endif
199 * acpi_pm_end - Finish up suspend sequence.
202 static void acpi_pm_end(void)
205 * This is necessary in case acpi_pm_finish() is not called during a
206 * failing transition to a sleep state.
208 acpi_target_sleep_state = ACPI_STATE_S0;
211 static int acpi_pm_state_valid(suspend_state_t pm_state)
213 u32 acpi_state;
215 switch (pm_state) {
216 case PM_SUSPEND_ON:
217 case PM_SUSPEND_STANDBY:
218 case PM_SUSPEND_MEM:
219 acpi_state = acpi_suspend_states[pm_state];
221 return sleep_states[acpi_state];
222 default:
223 return 0;
227 static struct platform_suspend_ops acpi_pm_ops = {
228 .valid = acpi_pm_state_valid,
229 .begin = acpi_pm_begin,
230 .prepare = acpi_pm_prepare,
231 .enter = acpi_pm_enter,
232 .finish = acpi_pm_finish,
233 .end = acpi_pm_end,
237 * Toshiba fails to preserve interrupts over S1, reinitialization
238 * of 8259 is needed after S1 resume.
240 static int __init init_ints_after_s1(const struct dmi_system_id *d)
242 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
243 init_8259A_after_S1 = 1;
244 return 0;
247 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
249 .callback = init_ints_after_s1,
250 .ident = "Toshiba Satellite 4030cdt",
251 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
255 #endif /* CONFIG_SUSPEND */
257 #ifdef CONFIG_HIBERNATION
258 static int acpi_hibernation_begin(void)
260 acpi_target_sleep_state = ACPI_STATE_S4;
262 return 0;
265 static int acpi_hibernation_prepare(void)
267 int error = acpi_sleep_prepare(ACPI_STATE_S4);
269 if (error) {
270 acpi_target_sleep_state = ACPI_STATE_S0;
271 return error;
274 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
277 static int acpi_hibernation_enter(void)
279 acpi_status status = AE_OK;
280 unsigned long flags = 0;
282 ACPI_FLUSH_CPU_CACHE();
284 local_irq_save(flags);
285 acpi_enable_wakeup_device(ACPI_STATE_S4);
286 /* This shouldn't return. If it returns, we have a problem */
287 status = acpi_enter_sleep_state(ACPI_STATE_S4);
288 /* Reprogram control registers and execute _BFS */
289 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
290 local_irq_restore(flags);
292 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
295 static void acpi_hibernation_leave(void)
298 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
299 * enable it here.
301 acpi_enable();
302 /* Reprogram control registers and execute _BFS */
303 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
306 static void acpi_hibernation_finish(void)
308 acpi_disable_wakeup_device(ACPI_STATE_S4);
309 acpi_leave_sleep_state(ACPI_STATE_S4);
311 /* reset firmware waking vector */
312 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
314 acpi_target_sleep_state = ACPI_STATE_S0;
317 static void acpi_hibernation_end(void)
320 * This is necessary in case acpi_hibernation_finish() is not called
321 * during a failing transition to the sleep state.
323 acpi_target_sleep_state = ACPI_STATE_S0;
326 static int acpi_hibernation_pre_restore(void)
328 acpi_status status;
330 status = acpi_hw_disable_all_gpes();
332 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
335 static void acpi_hibernation_restore_cleanup(void)
337 acpi_hw_enable_all_runtime_gpes();
340 static struct platform_hibernation_ops acpi_hibernation_ops = {
341 .begin = acpi_hibernation_begin,
342 .end = acpi_hibernation_end,
343 .pre_snapshot = acpi_hibernation_prepare,
344 .finish = acpi_hibernation_finish,
345 .prepare = acpi_hibernation_prepare,
346 .enter = acpi_hibernation_enter,
347 .leave = acpi_hibernation_leave,
348 .pre_restore = acpi_hibernation_pre_restore,
349 .restore_cleanup = acpi_hibernation_restore_cleanup,
351 #endif /* CONFIG_HIBERNATION */
353 int acpi_suspend(u32 acpi_state)
355 suspend_state_t states[] = {
356 [1] = PM_SUSPEND_STANDBY,
357 [3] = PM_SUSPEND_MEM,
358 [5] = PM_SUSPEND_MAX
361 if (acpi_state < 6 && states[acpi_state])
362 return pm_suspend(states[acpi_state]);
363 if (acpi_state == 4)
364 return hibernate();
365 return -EINVAL;
368 #ifdef CONFIG_PM_SLEEP
370 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
371 * in the system sleep state given by %acpi_target_sleep_state
372 * @dev: device to examine
373 * @wake: if set, the device should be able to wake up the system
374 * @d_min_p: used to store the upper limit of allowed states range
375 * Return value: preferred power state of the device on success, -ENODEV on
376 * failure (ie. if there's no 'struct acpi_device' for @dev)
378 * Find the lowest power (highest number) ACPI device power state that
379 * device @dev can be in while the system is in the sleep state represented
380 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
381 * able to wake up the system from this sleep state. If @d_min_p is set,
382 * the highest power (lowest number) device power state of @dev allowed
383 * in this system sleep state is stored at the location pointed to by it.
385 * The caller must ensure that @dev is valid before using this function.
386 * The caller is also responsible for figuring out if the device is
387 * supposed to be able to wake up the system and passing this information
388 * via @wake.
391 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
393 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
394 struct acpi_device *adev;
395 char acpi_method[] = "_SxD";
396 unsigned long d_min, d_max;
398 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
399 printk(KERN_DEBUG "ACPI handle has no context!\n");
400 return -ENODEV;
403 acpi_method[2] = '0' + acpi_target_sleep_state;
405 * If the sleep state is S0, we will return D3, but if the device has
406 * _S0W, we will use the value from _S0W
408 d_min = ACPI_STATE_D0;
409 d_max = ACPI_STATE_D3;
412 * If present, _SxD methods return the minimum D-state (highest power
413 * state) we can use for the corresponding S-states. Otherwise, the
414 * minimum D-state is D0 (ACPI 3.x).
416 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
417 * provided -- that's our fault recovery, we ignore retval.
419 if (acpi_target_sleep_state > ACPI_STATE_S0)
420 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
423 * If _PRW says we can wake up the system from the target sleep state,
424 * the D-state returned by _SxD is sufficient for that (we assume a
425 * wakeup-aware driver if wake is set). Still, if _SxW exists
426 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
427 * can wake the system. _S0W may be valid, too.
429 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
430 (wake && adev->wakeup.state.enabled &&
431 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
432 acpi_status status;
434 acpi_method[3] = 'W';
435 status = acpi_evaluate_integer(handle, acpi_method, NULL,
436 &d_max);
437 if (ACPI_FAILURE(status)) {
438 d_max = d_min;
439 } else if (d_max < d_min) {
440 /* Warn the user of the broken DSDT */
441 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
442 acpi_method);
443 /* Sanitize it */
444 d_min = d_max;
448 if (d_min_p)
449 *d_min_p = d_min;
450 return d_max;
452 #endif
454 static void acpi_power_off_prepare(void)
456 /* Prepare to power off the system */
457 acpi_sleep_prepare(ACPI_STATE_S5);
458 acpi_hw_disable_all_gpes();
461 static void acpi_power_off(void)
463 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
464 printk("%s called\n", __func__);
465 local_irq_disable();
466 acpi_enable_wakeup_device(ACPI_STATE_S5);
467 acpi_enter_sleep_state(ACPI_STATE_S5);
470 int __init acpi_sleep_init(void)
472 acpi_status status;
473 u8 type_a, type_b;
474 #ifdef CONFIG_SUSPEND
475 int i = 0;
477 dmi_check_system(acpisleep_dmi_table);
478 #endif
480 if (acpi_disabled)
481 return 0;
483 sleep_states[ACPI_STATE_S0] = 1;
484 printk(KERN_INFO PREFIX "(supports S0");
486 #ifdef CONFIG_SUSPEND
487 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
488 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
489 if (ACPI_SUCCESS(status)) {
490 sleep_states[i] = 1;
491 printk(" S%d", i);
495 suspend_set_ops(&acpi_pm_ops);
496 #endif
498 #ifdef CONFIG_HIBERNATION
499 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
500 if (ACPI_SUCCESS(status)) {
501 hibernation_set_ops(&acpi_hibernation_ops);
502 sleep_states[ACPI_STATE_S4] = 1;
503 printk(" S4");
505 #endif
506 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
507 if (ACPI_SUCCESS(status)) {
508 sleep_states[ACPI_STATE_S5] = 1;
509 printk(" S5");
510 pm_power_off_prepare = acpi_power_off_prepare;
511 pm_power_off = acpi_power_off;
513 printk(")\n");
514 return 0;