blob: c3b0cd88d09f22972342af30e02abfafe18fadcc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sleep.c - ACPI sleep support.
3 *
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -05004 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
8 *
9 * This file is released under the GPLv2.
10 *
11 */
12
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>
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +040018
19#include <asm/io.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <acpi/acpi_bus.h>
22#include <acpi/acpi_drivers.h>
23#include "sleep.h"
24
25u8 sleep_states[ACPI_S_STATE_COUNT];
26
Alexey Starikovskiy853298b2007-09-25 18:45:15 +040027#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020028static u32 acpi_target_sleep_state = ACPI_STATE_S0;
Alexey Starikovskiy853298b2007-09-25 18:45:15 +040029#endif
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020030
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010031static int acpi_sleep_prepare(u32 acpi_state)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020032{
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;
38 }
39 acpi_set_firmware_waking_vector((acpi_physical_address)
40 virt_to_phys((void *)
41 acpi_wakeup_address));
42
43 }
44 ACPI_FLUSH_CPU_CACHE();
45 acpi_enable_wakeup_device_prep(acpi_state);
46#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010047 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
48 acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020049 acpi_enter_sleep_state_prep(acpi_state);
50 return 0;
51}
52
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020053#ifdef CONFIG_SUSPEND
Len Brown2c6e33c2008-04-23 18:02:52 -040054static struct platform_suspend_ops acpi_suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056extern void do_suspend_lowlevel(void);
57
58static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050059 [PM_SUSPEND_ON] = ACPI_STATE_S0,
60 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
61 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050062 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65static int init_8259A_after_S1;
66
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020067/**
Len Brown2c6e33c2008-04-23 18:02:52 -040068 * acpi_suspend_begin - Set the target system sleep state to the state
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020069 * associated with given @pm_state, if supported.
70 */
71
Len Brown2c6e33c2008-04-23 18:02:52 -040072static int acpi_suspend_begin(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020075 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020077 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020084 return error;
85}
86
87/**
Len Brown2c6e33c2008-04-23 18:02:52 -040088 * acpi_suspend_prepare - Do preliminary suspend work.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020089 *
90 * If necessary, set the firmware waking vector and do arch-specific
91 * nastiness to get the wakeup code to the waking vector.
92 */
93
Len Brown2c6e33c2008-04-23 18:02:52 -040094static int acpi_suspend_prepare(void)
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020095{
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +010096 int error = acpi_sleep_prepare(acpi_target_sleep_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020097
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +010098 if (error) {
99 acpi_target_sleep_state = ACPI_STATE_S0;
100 return error;
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100101 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200102
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100103 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400107 * acpi_suspend_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200108 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200110 * Flush caches and go to sleep. For STR we have to call arch-specific
111 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * It's unfortunate, but it works. Please fix if you're feeling frisky.
113 */
114
Len Brown2c6e33c2008-04-23 18:02:52 -0400115static int acpi_suspend_enter(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 acpi_status status = AE_OK;
118 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200119 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 ACPI_FLUSH_CPU_CACHE();
122
123 /* Do arch specific saving of state. */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200124 if (acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200126
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100127 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return error;
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 local_irq_save(flags);
132 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200133 switch (acpi_state) {
134 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 barrier();
136 status = acpi_enter_sleep_state(acpi_state);
137 break;
138
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200139 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 do_suspend_lowlevel();
141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400143
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100144 /* Reprogram control registers and execute _BFS */
145 acpi_leave_sleep_state_prep(acpi_state);
146
Pavel Machek23b168d2008-02-05 19:27:12 +0100147 /* ACPI 3.0 specs (P62) says that it's the responsibility
Arnaud Patard872d83d2006-04-27 05:25:00 -0400148 * of the OSPM to clear the status bit [ implying that the
149 * POWER_BUTTON event should not reach userspace ]
150 */
151 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
152 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
153
Shaohua Lia3627f62007-06-20 09:17:58 +0800154 /*
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
158 */
159 acpi_hw_disable_all_gpes();
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 local_irq_restore(flags);
162 printk(KERN_DEBUG "Back to C!\n");
163
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200164 /* restore processor state */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200165 if (acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 acpi_restore_state_mem();
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400172 * acpi_suspend_finish - Instruct the platform to leave a sleep state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
174 * This is called after we wake back up (or if entering the sleep state
175 * failed).
176 */
177
Len Brown2c6e33c2008-04-23 18:02:52 -0400178static void acpi_suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200180 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 acpi_disable_wakeup_device(acpi_state);
Alexey Starikovskiy1dbc1fda2007-10-22 14:18:12 +0400183 acpi_leave_sleep_state(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* reset firmware waking vector */
186 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
187
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200188 acpi_target_sleep_state = ACPI_STATE_S0;
189
Len Browne8b2fd02007-07-24 22:26:33 -0400190#ifdef CONFIG_X86
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (init_8259A_after_S1) {
192 printk("Broken toshiba laptop -> kicking interrupts\n");
193 init_8259A(0);
194 }
Len Browne8b2fd02007-07-24 22:26:33 -0400195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100198/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400199 * acpi_suspend_end - Finish up suspend sequence.
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100200 */
201
Len Brown2c6e33c2008-04-23 18:02:52 -0400202static void acpi_suspend_end(void)
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100203{
204 /*
Len Brown2c6e33c2008-04-23 18:02:52 -0400205 * This is necessary in case acpi_suspend_finish() is not called during a
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100206 * failing transition to a sleep state.
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100207 */
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100208 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100209}
210
Len Brown2c6e33c2008-04-23 18:02:52 -0400211static int acpi_suspend_state_valid(suspend_state_t pm_state)
Shaohua Lieb9289e2005-10-30 15:00:01 -0800212{
Johannes Berge8c9c502007-04-30 15:09:54 -0700213 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800214
Johannes Berge8c9c502007-04-30 15:09:54 -0700215 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];
220
221 return sleep_states[acpi_state];
222 default:
223 return 0;
224 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800225}
226
Len Brown2c6e33c2008-04-23 18:02:52 -0400227static struct platform_suspend_ops acpi_suspend_ops = {
228 .valid = acpi_suspend_state_valid,
229 .begin = acpi_suspend_begin,
230 .prepare = acpi_suspend_prepare,
231 .enter = acpi_suspend_enter,
232 .finish = acpi_suspend_finish,
233 .end = acpi_suspend_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200236/*
237 * Toshiba fails to preserve interrupts over S1, reinitialization
238 * of 8259 is needed after S1 resume.
239 */
Jeff Garzik18552562007-10-03 15:15:40 -0400240static int __init init_ints_after_s1(const struct dmi_system_id *d)
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200241{
242 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
243 init_8259A_after_S1 = 1;
244 return 0;
245}
246
247static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
248 {
249 .callback = init_ints_after_s1,
250 .ident = "Toshiba Satellite 4030cdt",
251 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
252 },
253 {},
254};
255#endif /* CONFIG_SUSPEND */
256
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200257#ifdef CONFIG_HIBERNATION
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100258static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700259{
260 acpi_target_sleep_state = ACPI_STATE_S4;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100261
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100262 return 0;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700263}
264
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700265static int acpi_hibernation_prepare(void)
266{
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100267 int error = acpi_sleep_prepare(ACPI_STATE_S4);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100268
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100269 if (error) {
270 acpi_target_sleep_state = ACPI_STATE_S0;
271 return error;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100272 }
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100273
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100274 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700275}
276
277static int acpi_hibernation_enter(void)
278{
279 acpi_status status = AE_OK;
280 unsigned long flags = 0;
281
282 ACPI_FLUSH_CPU_CACHE();
283
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);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100288 /* Reprogram control registers and execute _BFS */
289 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700290 local_irq_restore(flags);
291
292 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
293}
294
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700295static void acpi_hibernation_leave(void)
296{
297 /*
298 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
299 * enable it here.
300 */
301 acpi_enable();
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100302 /* Reprogram control registers and execute _BFS */
303 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700304}
305
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700306static void acpi_hibernation_finish(void)
307{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700308 acpi_disable_wakeup_device(ACPI_STATE_S4);
Alexey Starikovskiy1dbc1fda2007-10-22 14:18:12 +0400309 acpi_leave_sleep_state(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700310
311 /* reset firmware waking vector */
312 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700313
314 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700315}
316
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100317static void acpi_hibernation_end(void)
318{
319 /*
320 * This is necessary in case acpi_hibernation_finish() is not called
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100321 * during a failing transition to the sleep state.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100322 */
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100323 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100324}
325
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700326static int acpi_hibernation_pre_restore(void)
327{
328 acpi_status status;
329
330 status = acpi_hw_disable_all_gpes();
331
332 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
333}
334
335static void acpi_hibernation_restore_cleanup(void)
336{
337 acpi_hw_enable_all_runtime_gpes();
338}
339
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -0700340static struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100341 .begin = acpi_hibernation_begin,
342 .end = acpi_hibernation_end,
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700343 .pre_snapshot = acpi_hibernation_prepare,
344 .finish = acpi_hibernation_finish,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700345 .prepare = acpi_hibernation_prepare,
346 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700347 .leave = acpi_hibernation_leave,
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700348 .pre_restore = acpi_hibernation_pre_restore,
349 .restore_cleanup = acpi_hibernation_restore_cleanup,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700350};
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200351#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700352
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200353int acpi_suspend(u32 acpi_state)
354{
355 suspend_state_t states[] = {
356 [1] = PM_SUSPEND_STANDBY,
357 [3] = PM_SUSPEND_MEM,
358 [5] = PM_SUSPEND_MAX
359 };
360
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;
366}
367
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400368#ifdef CONFIG_PM_SLEEP
Shaohua Lifd4aff12007-07-17 22:40:25 +0200369/**
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)
377 *
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.
384 *
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.
389 */
390
391int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
392{
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;
397
398 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800399 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200400 return -ENODEV;
401 }
402
403 acpi_method[2] = '0' + acpi_target_sleep_state;
404 /*
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
407 */
408 d_min = ACPI_STATE_D0;
409 d_max = ACPI_STATE_D3;
410
411 /*
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).
415 *
416 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
417 * provided -- that's our fault recovery, we ignore retval.
418 */
419 if (acpi_target_sleep_state > ACPI_STATE_S0)
420 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
421
422 /*
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.
428 */
429 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
430 (wake && adev->wakeup.state.enabled &&
431 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100432 acpi_status status;
433
Shaohua Lifd4aff12007-07-17 22:40:25 +0200434 acpi_method[3] = 'W';
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100435 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 */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200444 d_min = d_max;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100445 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200446 }
447
448 if (d_min_p)
449 *d_min_p = d_min;
450 return d_max;
451}
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400452#endif
Shaohua Lifd4aff12007-07-17 22:40:25 +0200453
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400454static void acpi_power_off_prepare(void)
455{
456 /* Prepare to power off the system */
457 acpi_sleep_prepare(ACPI_STATE_S5);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100458 acpi_hw_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400459}
460
461static void acpi_power_off(void)
462{
463 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
Harvey Harrison96b2dd12008-03-05 18:24:51 -0800464 printk("%s called\n", __func__);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400465 local_irq_disable();
Alexey Starikovskiy9c1c6a12007-10-22 14:18:06 +0400466 acpi_enable_wakeup_device(ACPI_STATE_S5);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400467 acpi_enter_sleep_state(ACPI_STATE_S5);
468}
469
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500470int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200472 acpi_status status;
473 u8 type_a, type_b;
474#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500475 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 dmi_check_system(acpisleep_dmi_table);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200478#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (acpi_disabled)
481 return 0;
482
Frans Pop5a50fe72007-09-20 22:27:44 +0200483 sleep_states[ACPI_STATE_S0] = 1;
484 printk(KERN_INFO PREFIX "(supports S0");
485
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200486#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200487 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 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);
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Len Brown2c6e33c2008-04-23 18:02:52 -0400495 suspend_set_ops(&acpi_suspend_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200496#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700497
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200498#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200499 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
500 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700501 hibernation_set_ops(&acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200502 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400503 printk(" S4");
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200504 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700505#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400506 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;
512 }
513 printk(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515}