blob: 495c63a3e0afb1fce2e4fcb6801020ad2f8413ad [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 }
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020039 acpi_set_firmware_waking_vector(
40 (acpi_physical_address)acpi_wakeup_address);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020041
42 }
43 ACPI_FLUSH_CPU_CACHE();
44 acpi_enable_wakeup_device_prep(acpi_state);
45#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010046 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
47 acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020048 acpi_enter_sleep_state_prep(acpi_state);
49 return 0;
50}
51
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020052#ifdef CONFIG_SUSPEND
Len Brown2c6e33c2008-04-23 18:02:52 -040053static struct platform_suspend_ops acpi_suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055extern void do_suspend_lowlevel(void);
56
57static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050058 [PM_SUSPEND_ON] = ACPI_STATE_S0,
59 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
60 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050061 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64static int init_8259A_after_S1;
65
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020066/**
Len Brown2c6e33c2008-04-23 18:02:52 -040067 * acpi_suspend_begin - Set the target system sleep state to the state
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020068 * associated with given @pm_state, if supported.
69 */
70
Len Brown2c6e33c2008-04-23 18:02:52 -040071static int acpi_suspend_begin(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020074 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020076 if (sleep_states[acpi_state]) {
77 acpi_target_sleep_state = acpi_state;
78 } else {
79 printk(KERN_ERR "ACPI does not support this state: %d\n",
80 pm_state);
81 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020083 return error;
84}
85
86/**
Len Brown2c6e33c2008-04-23 18:02:52 -040087 * acpi_suspend_prepare - Do preliminary suspend work.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020088 *
89 * If necessary, set the firmware waking vector and do arch-specific
90 * nastiness to get the wakeup code to the waking vector.
91 */
92
Len Brown2c6e33c2008-04-23 18:02:52 -040093static int acpi_suspend_prepare(void)
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020094{
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +010095 int error = acpi_sleep_prepare(acpi_target_sleep_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020096
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +010097 if (error) {
98 acpi_target_sleep_state = ACPI_STATE_S0;
99 return error;
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100100 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200101
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100102 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400106 * acpi_suspend_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200107 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200109 * Flush caches and go to sleep. For STR we have to call arch-specific
110 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * It's unfortunate, but it works. Please fix if you're feeling frisky.
112 */
113
Len Brown2c6e33c2008-04-23 18:02:52 -0400114static int acpi_suspend_enter(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 acpi_status status = AE_OK;
117 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200118 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 ACPI_FLUSH_CPU_CACHE();
121
122 /* Do arch specific saving of state. */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200123 if (acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200125
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100126 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return error;
128 }
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 local_irq_save(flags);
131 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200132 switch (acpi_state) {
133 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 barrier();
135 status = acpi_enter_sleep_state(acpi_state);
136 break;
137
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200138 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 do_suspend_lowlevel();
140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400142
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100143 /* Reprogram control registers and execute _BFS */
144 acpi_leave_sleep_state_prep(acpi_state);
145
Pavel Machek23b168d2008-02-05 19:27:12 +0100146 /* ACPI 3.0 specs (P62) says that it's the responsibility
Arnaud Patard872d83d2006-04-27 05:25:00 -0400147 * of the OSPM to clear the status bit [ implying that the
148 * POWER_BUTTON event should not reach userspace ]
149 */
150 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
151 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
152
Shaohua Lia3627f62007-06-20 09:17:58 +0800153 /*
154 * Disable and clear GPE status before interrupt is enabled. Some GPEs
155 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
156 * acpi_leave_sleep_state will reenable specific GPEs later
157 */
158 acpi_hw_disable_all_gpes();
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 local_irq_restore(flags);
161 printk(KERN_DEBUG "Back to C!\n");
162
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200163 /* restore processor state */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200164 if (acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 acpi_restore_state_mem();
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400171 * acpi_suspend_finish - Instruct the platform to leave a sleep state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * This is called after we wake back up (or if entering the sleep state
174 * failed).
175 */
176
Len Brown2c6e33c2008-04-23 18:02:52 -0400177static void acpi_suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200179 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 acpi_disable_wakeup_device(acpi_state);
Alexey Starikovskiy1dbc1fd2007-10-22 14:18:12 +0400182 acpi_leave_sleep_state(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* reset firmware waking vector */
185 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
186
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200187 acpi_target_sleep_state = ACPI_STATE_S0;
188
Len Browne8b2fd02007-07-24 22:26:33 -0400189#ifdef CONFIG_X86
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (init_8259A_after_S1) {
191 printk("Broken toshiba laptop -> kicking interrupts\n");
192 init_8259A(0);
193 }
Len Browne8b2fd02007-07-24 22:26:33 -0400194#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100197/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400198 * acpi_suspend_end - Finish up suspend sequence.
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100199 */
200
Len Brown2c6e33c2008-04-23 18:02:52 -0400201static void acpi_suspend_end(void)
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100202{
203 /*
Len Brown2c6e33c2008-04-23 18:02:52 -0400204 * This is necessary in case acpi_suspend_finish() is not called during a
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100205 * failing transition to a sleep state.
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100206 */
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100207 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100208}
209
Len Brown2c6e33c2008-04-23 18:02:52 -0400210static int acpi_suspend_state_valid(suspend_state_t pm_state)
Shaohua Lieb9289e2005-10-30 15:00:01 -0800211{
Johannes Berge8c9c502007-04-30 15:09:54 -0700212 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800213
Johannes Berge8c9c502007-04-30 15:09:54 -0700214 switch (pm_state) {
215 case PM_SUSPEND_ON:
216 case PM_SUSPEND_STANDBY:
217 case PM_SUSPEND_MEM:
218 acpi_state = acpi_suspend_states[pm_state];
219
220 return sleep_states[acpi_state];
221 default:
222 return 0;
223 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800224}
225
Len Brown2c6e33c2008-04-23 18:02:52 -0400226static struct platform_suspend_ops acpi_suspend_ops = {
227 .valid = acpi_suspend_state_valid,
228 .begin = acpi_suspend_begin,
229 .prepare = acpi_suspend_prepare,
230 .enter = acpi_suspend_enter,
231 .finish = acpi_suspend_finish,
232 .end = acpi_suspend_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200235/*
236 * Toshiba fails to preserve interrupts over S1, reinitialization
237 * of 8259 is needed after S1 resume.
238 */
Jeff Garzik18552562007-10-03 15:15:40 -0400239static int __init init_ints_after_s1(const struct dmi_system_id *d)
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200240{
241 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
242 init_8259A_after_S1 = 1;
243 return 0;
244}
245
246static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
247 {
248 .callback = init_ints_after_s1,
249 .ident = "Toshiba Satellite 4030cdt",
250 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
251 },
252 {},
253};
254#endif /* CONFIG_SUSPEND */
255
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200256#ifdef CONFIG_HIBERNATION
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100257static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700258{
259 acpi_target_sleep_state = ACPI_STATE_S4;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100260
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100261 return 0;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700262}
263
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700264static int acpi_hibernation_prepare(void)
265{
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100266 int error = acpi_sleep_prepare(ACPI_STATE_S4);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100267
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100268 if (error) {
269 acpi_target_sleep_state = ACPI_STATE_S0;
270 return error;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100271 }
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100272
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100273 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700274}
275
276static int acpi_hibernation_enter(void)
277{
278 acpi_status status = AE_OK;
279 unsigned long flags = 0;
280
281 ACPI_FLUSH_CPU_CACHE();
282
283 local_irq_save(flags);
284 acpi_enable_wakeup_device(ACPI_STATE_S4);
285 /* This shouldn't return. If it returns, we have a problem */
286 status = acpi_enter_sleep_state(ACPI_STATE_S4);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100287 /* Reprogram control registers and execute _BFS */
288 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700289 local_irq_restore(flags);
290
291 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
292}
293
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700294static void acpi_hibernation_leave(void)
295{
296 /*
297 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
298 * enable it here.
299 */
300 acpi_enable();
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100301 /* Reprogram control registers and execute _BFS */
302 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700303}
304
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700305static void acpi_hibernation_finish(void)
306{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700307 acpi_disable_wakeup_device(ACPI_STATE_S4);
Alexey Starikovskiy1dbc1fd2007-10-22 14:18:12 +0400308 acpi_leave_sleep_state(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700309
310 /* reset firmware waking vector */
311 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700312
313 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700314}
315
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100316static void acpi_hibernation_end(void)
317{
318 /*
319 * This is necessary in case acpi_hibernation_finish() is not called
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100320 * during a failing transition to the sleep state.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100321 */
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100322 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100323}
324
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700325static int acpi_hibernation_pre_restore(void)
326{
327 acpi_status status;
328
329 status = acpi_hw_disable_all_gpes();
330
331 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
332}
333
334static void acpi_hibernation_restore_cleanup(void)
335{
336 acpi_hw_enable_all_runtime_gpes();
337}
338
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -0700339static struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100340 .begin = acpi_hibernation_begin,
341 .end = acpi_hibernation_end,
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700342 .pre_snapshot = acpi_hibernation_prepare,
343 .finish = acpi_hibernation_finish,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700344 .prepare = acpi_hibernation_prepare,
345 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700346 .leave = acpi_hibernation_leave,
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700347 .pre_restore = acpi_hibernation_pre_restore,
348 .restore_cleanup = acpi_hibernation_restore_cleanup,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700349};
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200350#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700351
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200352int acpi_suspend(u32 acpi_state)
353{
354 suspend_state_t states[] = {
355 [1] = PM_SUSPEND_STANDBY,
356 [3] = PM_SUSPEND_MEM,
357 [5] = PM_SUSPEND_MAX
358 };
359
360 if (acpi_state < 6 && states[acpi_state])
361 return pm_suspend(states[acpi_state]);
362 if (acpi_state == 4)
363 return hibernate();
364 return -EINVAL;
365}
366
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400367#ifdef CONFIG_PM_SLEEP
Shaohua Lifd4aff12007-07-17 22:40:25 +0200368/**
369 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
370 * in the system sleep state given by %acpi_target_sleep_state
371 * @dev: device to examine
372 * @wake: if set, the device should be able to wake up the system
373 * @d_min_p: used to store the upper limit of allowed states range
374 * Return value: preferred power state of the device on success, -ENODEV on
375 * failure (ie. if there's no 'struct acpi_device' for @dev)
376 *
377 * Find the lowest power (highest number) ACPI device power state that
378 * device @dev can be in while the system is in the sleep state represented
379 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
380 * able to wake up the system from this sleep state. If @d_min_p is set,
381 * the highest power (lowest number) device power state of @dev allowed
382 * in this system sleep state is stored at the location pointed to by it.
383 *
384 * The caller must ensure that @dev is valid before using this function.
385 * The caller is also responsible for figuring out if the device is
386 * supposed to be able to wake up the system and passing this information
387 * via @wake.
388 */
389
390int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
391{
392 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
393 struct acpi_device *adev;
394 char acpi_method[] = "_SxD";
395 unsigned long d_min, d_max;
396
397 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800398 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200399 return -ENODEV;
400 }
401
402 acpi_method[2] = '0' + acpi_target_sleep_state;
403 /*
404 * If the sleep state is S0, we will return D3, but if the device has
405 * _S0W, we will use the value from _S0W
406 */
407 d_min = ACPI_STATE_D0;
408 d_max = ACPI_STATE_D3;
409
410 /*
411 * If present, _SxD methods return the minimum D-state (highest power
412 * state) we can use for the corresponding S-states. Otherwise, the
413 * minimum D-state is D0 (ACPI 3.x).
414 *
415 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
416 * provided -- that's our fault recovery, we ignore retval.
417 */
418 if (acpi_target_sleep_state > ACPI_STATE_S0)
419 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
420
421 /*
422 * If _PRW says we can wake up the system from the target sleep state,
423 * the D-state returned by _SxD is sufficient for that (we assume a
424 * wakeup-aware driver if wake is set). Still, if _SxW exists
425 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
426 * can wake the system. _S0W may be valid, too.
427 */
428 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
429 (wake && adev->wakeup.state.enabled &&
430 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100431 acpi_status status;
432
Shaohua Lifd4aff12007-07-17 22:40:25 +0200433 acpi_method[3] = 'W';
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100434 status = acpi_evaluate_integer(handle, acpi_method, NULL,
435 &d_max);
436 if (ACPI_FAILURE(status)) {
437 d_max = d_min;
438 } else if (d_max < d_min) {
439 /* Warn the user of the broken DSDT */
440 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
441 acpi_method);
442 /* Sanitize it */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200443 d_min = d_max;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100444 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200445 }
446
447 if (d_min_p)
448 *d_min_p = d_min;
449 return d_max;
450}
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400451#endif
Shaohua Lifd4aff12007-07-17 22:40:25 +0200452
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400453static void acpi_power_off_prepare(void)
454{
455 /* Prepare to power off the system */
456 acpi_sleep_prepare(ACPI_STATE_S5);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100457 acpi_hw_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400458}
459
460static void acpi_power_off(void)
461{
462 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
Harvey Harrison96b2dd12008-03-05 18:24:51 -0800463 printk("%s called\n", __func__);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400464 local_irq_disable();
Alexey Starikovskiy9c1c6a12007-10-22 14:18:06 +0400465 acpi_enable_wakeup_device(ACPI_STATE_S5);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400466 acpi_enter_sleep_state(ACPI_STATE_S5);
467}
468
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500469int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200471 acpi_status status;
472 u8 type_a, type_b;
473#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500474 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 dmi_check_system(acpisleep_dmi_table);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (acpi_disabled)
480 return 0;
481
Frans Pop5a50fe72007-09-20 22:27:44 +0200482 sleep_states[ACPI_STATE_S0] = 1;
483 printk(KERN_INFO PREFIX "(supports S0");
484
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200485#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200486 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
488 if (ACPI_SUCCESS(status)) {
489 sleep_states[i] = 1;
490 printk(" S%d", i);
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Len Brown2c6e33c2008-04-23 18:02:52 -0400494 suspend_set_ops(&acpi_suspend_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200495#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700496
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200497#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200498 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
499 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700500 hibernation_set_ops(&acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200501 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400502 printk(" S4");
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200503 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700504#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400505 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
506 if (ACPI_SUCCESS(status)) {
507 sleep_states[ACPI_STATE_S5] = 1;
508 printk(" S5");
509 pm_power_off_prepare = acpi_power_off_prepare;
510 pm_power_off = acpi_power_off;
511 }
512 printk(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
514}