blob: 485de1347075c812c4c7dbd4347c67cca7bd67b3 [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;
Rafael J. Wysocki60417f52008-01-08 00:07:39 +010029static bool acpi_sleep_finish_wake_up;
30
31/*
32 * ACPI 2.0 and later want us to execute _PTS after suspending devices, so we
33 * allow the user to request that behavior by using the 'acpi_new_pts_ordering'
34 * kernel command line option that causes the following variable to be set.
35 */
36static bool new_pts_ordering;
37
38static int __init acpi_new_pts_ordering(char *str)
39{
40 new_pts_ordering = true;
41 return 1;
42}
43__setup("acpi_new_pts_ordering", acpi_new_pts_ordering);
Alexey Starikovskiy853298b2007-09-25 18:45:15 +040044#endif
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020045
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010046static int acpi_sleep_prepare(u32 acpi_state)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020047{
48#ifdef CONFIG_ACPI_SLEEP
49 /* do we have a wakeup address for S2 and S3? */
50 if (acpi_state == ACPI_STATE_S3) {
51 if (!acpi_wakeup_address) {
52 return -EFAULT;
53 }
54 acpi_set_firmware_waking_vector((acpi_physical_address)
55 virt_to_phys((void *)
56 acpi_wakeup_address));
57
58 }
59 ACPI_FLUSH_CPU_CACHE();
60 acpi_enable_wakeup_device_prep(acpi_state);
61#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010062 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
63 acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020064 acpi_enter_sleep_state_prep(acpi_state);
65 return 0;
66}
67
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020068#ifdef CONFIG_SUSPEND
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070069static struct platform_suspend_ops acpi_pm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071extern void do_suspend_lowlevel(void);
72
73static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050074 [PM_SUSPEND_ON] = ACPI_STATE_S0,
75 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
76 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050077 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80static int init_8259A_after_S1;
81
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020082/**
Rafael J. Wysockic697eec2008-01-08 00:04:17 +010083 * acpi_pm_begin - Set the target system sleep state to the state
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020084 * associated with given @pm_state, if supported.
85 */
86
Rafael J. Wysockic697eec2008-01-08 00:04:17 +010087static int acpi_pm_begin(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020090 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020092 if (sleep_states[acpi_state]) {
93 acpi_target_sleep_state = acpi_state;
Rafael J. Wysocki60417f52008-01-08 00:07:39 +010094 if (new_pts_ordering)
95 return 0;
96
97 error = acpi_sleep_prepare(acpi_state);
98 if (error)
99 acpi_target_sleep_state = ACPI_STATE_S0;
100 else
101 acpi_sleep_finish_wake_up = true;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200102 } else {
103 printk(KERN_ERR "ACPI does not support this state: %d\n",
104 pm_state);
105 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200107 return error;
108}
109
110/**
111 * acpi_pm_prepare - Do preliminary suspend work.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200112 *
113 * If necessary, set the firmware waking vector and do arch-specific
114 * nastiness to get the wakeup code to the waking vector.
115 */
116
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700117static int acpi_pm_prepare(void)
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200118{
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100119 if (new_pts_ordering) {
120 int error = acpi_sleep_prepare(acpi_target_sleep_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200121
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100122 if (error) {
123 acpi_target_sleep_state = ACPI_STATE_S0;
124 return error;
125 }
126 acpi_sleep_finish_wake_up = true;
127 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200128
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100129 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
133 * acpi_pm_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200134 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200136 * Flush caches and go to sleep. For STR we have to call arch-specific
137 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * It's unfortunate, but it works. Please fix if you're feeling frisky.
139 */
140
141static int acpi_pm_enter(suspend_state_t pm_state)
142{
143 acpi_status status = AE_OK;
144 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200145 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 ACPI_FLUSH_CPU_CACHE();
148
149 /* Do arch specific saving of state. */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200150 if (acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200152
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100153 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return error;
155 }
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 local_irq_save(flags);
158 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200159 switch (acpi_state) {
160 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 barrier();
162 status = acpi_enter_sleep_state(acpi_state);
163 break;
164
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200165 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 do_suspend_lowlevel();
167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400169
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100170 /* Reprogram control registers and execute _BFS */
171 acpi_leave_sleep_state_prep(acpi_state);
172
Arnaud Patard872d83d2006-04-27 05:25:00 -0400173 /* ACPI 3.0 specs (P62) says that it's the responsabilty
174 * of the OSPM to clear the status bit [ implying that the
175 * POWER_BUTTON event should not reach userspace ]
176 */
177 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
178 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
179
Shaohua Lia3627f62007-06-20 09:17:58 +0800180 /*
181 * Disable and clear GPE status before interrupt is enabled. Some GPEs
182 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
183 * acpi_leave_sleep_state will reenable specific GPEs later
184 */
185 acpi_hw_disable_all_gpes();
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 local_irq_restore(flags);
188 printk(KERN_DEBUG "Back to C!\n");
189
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200190 /* restore processor state */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200191 if (acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 acpi_restore_state_mem();
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/**
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100198 * acpi_pm_finish - Instruct the platform to leave a sleep state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
200 * This is called after we wake back up (or if entering the sleep state
201 * failed).
202 */
203
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700204static void acpi_pm_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200206 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 acpi_disable_wakeup_device(acpi_state);
Alexey Starikovskiy1dbc1fd2007-10-22 14:18:12 +0400209 acpi_leave_sleep_state(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* reset firmware waking vector */
212 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
213
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200214 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100215 acpi_sleep_finish_wake_up = false;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200216
Len Browne8b2fd02007-07-24 22:26:33 -0400217#ifdef CONFIG_X86
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (init_8259A_after_S1) {
219 printk("Broken toshiba laptop -> kicking interrupts\n");
220 init_8259A(0);
221 }
Len Browne8b2fd02007-07-24 22:26:33 -0400222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100225/**
226 * acpi_pm_end - Finish up suspend sequence.
227 */
228
229static void acpi_pm_end(void)
230{
231 /*
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100232 * This is necessary in case acpi_pm_finish() is not called directly
233 * during a failing transition to a sleep state.
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100234 */
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100235 if (acpi_sleep_finish_wake_up)
236 acpi_pm_finish();
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100237}
238
Shaohua Lieb9289e2005-10-30 15:00:01 -0800239static int acpi_pm_state_valid(suspend_state_t pm_state)
240{
Johannes Berge8c9c502007-04-30 15:09:54 -0700241 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800242
Johannes Berge8c9c502007-04-30 15:09:54 -0700243 switch (pm_state) {
244 case PM_SUSPEND_ON:
245 case PM_SUSPEND_STANDBY:
246 case PM_SUSPEND_MEM:
247 acpi_state = acpi_suspend_states[pm_state];
248
249 return sleep_states[acpi_state];
250 default:
251 return 0;
252 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800253}
254
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700255static struct platform_suspend_ops acpi_pm_ops = {
Shaohua Lieb9289e2005-10-30 15:00:01 -0800256 .valid = acpi_pm_state_valid,
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100257 .begin = acpi_pm_begin,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500258 .prepare = acpi_pm_prepare,
259 .enter = acpi_pm_enter,
260 .finish = acpi_pm_finish,
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100261 .end = acpi_pm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200264/*
265 * Toshiba fails to preserve interrupts over S1, reinitialization
266 * of 8259 is needed after S1 resume.
267 */
Jeff Garzik18552562007-10-03 15:15:40 -0400268static int __init init_ints_after_s1(const struct dmi_system_id *d)
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200269{
270 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
271 init_8259A_after_S1 = 1;
272 return 0;
273}
274
275static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
276 {
277 .callback = init_ints_after_s1,
278 .ident = "Toshiba Satellite 4030cdt",
279 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
280 },
281 {},
282};
283#endif /* CONFIG_SUSPEND */
284
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200285#ifdef CONFIG_HIBERNATION
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100286static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700287{
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100288 int error;
289
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700290 acpi_target_sleep_state = ACPI_STATE_S4;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100291 if (new_pts_ordering)
292 return 0;
293
294 error = acpi_sleep_prepare(ACPI_STATE_S4);
295 if (error)
296 acpi_target_sleep_state = ACPI_STATE_S0;
297 else
298 acpi_sleep_finish_wake_up = true;
299
300 return error;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700301}
302
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700303static int acpi_hibernation_prepare(void)
304{
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100305 if (new_pts_ordering) {
306 int error = acpi_sleep_prepare(ACPI_STATE_S4);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100307
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100308 if (error) {
309 acpi_target_sleep_state = ACPI_STATE_S0;
310 return error;
311 }
312 acpi_sleep_finish_wake_up = true;
313 }
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100314
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100315 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700316}
317
318static int acpi_hibernation_enter(void)
319{
320 acpi_status status = AE_OK;
321 unsigned long flags = 0;
322
323 ACPI_FLUSH_CPU_CACHE();
324
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);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100329 /* Reprogram control registers and execute _BFS */
330 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700331 local_irq_restore(flags);
332
333 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
334}
335
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700336static void acpi_hibernation_leave(void)
337{
338 /*
339 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
340 * enable it here.
341 */
342 acpi_enable();
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100343 /* Reprogram control registers and execute _BFS */
344 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700345}
346
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700347static void acpi_hibernation_finish(void)
348{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700349 acpi_disable_wakeup_device(ACPI_STATE_S4);
Alexey Starikovskiy1dbc1fd2007-10-22 14:18:12 +0400350 acpi_leave_sleep_state(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700351
352 /* reset firmware waking vector */
353 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700354
355 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100356 acpi_sleep_finish_wake_up = false;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700357}
358
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100359static void acpi_hibernation_end(void)
360{
361 /*
362 * This is necessary in case acpi_hibernation_finish() is not called
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100363 * directly during a failing transition to the sleep state.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100364 */
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100365 if (acpi_sleep_finish_wake_up)
366 acpi_hibernation_finish();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100367}
368
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700369static int acpi_hibernation_pre_restore(void)
370{
371 acpi_status status;
372
373 status = acpi_hw_disable_all_gpes();
374
375 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
376}
377
378static void acpi_hibernation_restore_cleanup(void)
379{
380 acpi_hw_enable_all_runtime_gpes();
381}
382
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -0700383static struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100384 .begin = acpi_hibernation_begin,
385 .end = acpi_hibernation_end,
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700386 .pre_snapshot = acpi_hibernation_prepare,
387 .finish = acpi_hibernation_finish,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700388 .prepare = acpi_hibernation_prepare,
389 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700390 .leave = acpi_hibernation_leave,
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700391 .pre_restore = acpi_hibernation_pre_restore,
392 .restore_cleanup = acpi_hibernation_restore_cleanup,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700393};
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200394#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700395
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200396int acpi_suspend(u32 acpi_state)
397{
398 suspend_state_t states[] = {
399 [1] = PM_SUSPEND_STANDBY,
400 [3] = PM_SUSPEND_MEM,
401 [5] = PM_SUSPEND_MAX
402 };
403
404 if (acpi_state < 6 && states[acpi_state])
405 return pm_suspend(states[acpi_state]);
406 if (acpi_state == 4)
407 return hibernate();
408 return -EINVAL;
409}
410
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400411#ifdef CONFIG_PM_SLEEP
Shaohua Lifd4aff12007-07-17 22:40:25 +0200412/**
413 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
414 * in the system sleep state given by %acpi_target_sleep_state
415 * @dev: device to examine
416 * @wake: if set, the device should be able to wake up the system
417 * @d_min_p: used to store the upper limit of allowed states range
418 * Return value: preferred power state of the device on success, -ENODEV on
419 * failure (ie. if there's no 'struct acpi_device' for @dev)
420 *
421 * Find the lowest power (highest number) ACPI device power state that
422 * device @dev can be in while the system is in the sleep state represented
423 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
424 * able to wake up the system from this sleep state. If @d_min_p is set,
425 * the highest power (lowest number) device power state of @dev allowed
426 * in this system sleep state is stored at the location pointed to by it.
427 *
428 * The caller must ensure that @dev is valid before using this function.
429 * The caller is also responsible for figuring out if the device is
430 * supposed to be able to wake up the system and passing this information
431 * via @wake.
432 */
433
434int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
435{
436 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
437 struct acpi_device *adev;
438 char acpi_method[] = "_SxD";
439 unsigned long d_min, d_max;
440
441 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800442 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200443 return -ENODEV;
444 }
445
446 acpi_method[2] = '0' + acpi_target_sleep_state;
447 /*
448 * If the sleep state is S0, we will return D3, but if the device has
449 * _S0W, we will use the value from _S0W
450 */
451 d_min = ACPI_STATE_D0;
452 d_max = ACPI_STATE_D3;
453
454 /*
455 * If present, _SxD methods return the minimum D-state (highest power
456 * state) we can use for the corresponding S-states. Otherwise, the
457 * minimum D-state is D0 (ACPI 3.x).
458 *
459 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
460 * provided -- that's our fault recovery, we ignore retval.
461 */
462 if (acpi_target_sleep_state > ACPI_STATE_S0)
463 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
464
465 /*
466 * If _PRW says we can wake up the system from the target sleep state,
467 * the D-state returned by _SxD is sufficient for that (we assume a
468 * wakeup-aware driver if wake is set). Still, if _SxW exists
469 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
470 * can wake the system. _S0W may be valid, too.
471 */
472 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
473 (wake && adev->wakeup.state.enabled &&
474 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
475 acpi_method[3] = 'W';
476 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
477 /* Sanity check */
478 if (d_max < d_min)
479 d_min = d_max;
480 }
481
482 if (d_min_p)
483 *d_min_p = d_min;
484 return d_max;
485}
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400486#endif
Shaohua Lifd4aff12007-07-17 22:40:25 +0200487
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400488static void acpi_power_off_prepare(void)
489{
490 /* Prepare to power off the system */
491 acpi_sleep_prepare(ACPI_STATE_S5);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100492 acpi_hw_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400493}
494
495static void acpi_power_off(void)
496{
497 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
498 printk("%s called\n", __FUNCTION__);
499 local_irq_disable();
Alexey Starikovskiy9c1c6a12007-10-22 14:18:06 +0400500 acpi_enable_wakeup_device(ACPI_STATE_S5);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400501 acpi_enter_sleep_state(ACPI_STATE_S5);
502}
503
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500504int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200506 acpi_status status;
507 u8 type_a, type_b;
508#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500509 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 dmi_check_system(acpisleep_dmi_table);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (acpi_disabled)
515 return 0;
516
Frans Pop5a50fe72007-09-20 22:27:44 +0200517 sleep_states[ACPI_STATE_S0] = 1;
518 printk(KERN_INFO PREFIX "(supports S0");
519
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200520#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200521 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
523 if (ACPI_SUCCESS(status)) {
524 sleep_states[i] = 1;
525 printk(" S%d", i);
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700529 suspend_set_ops(&acpi_pm_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200530#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700531
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200532#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200533 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
534 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700535 hibernation_set_ops(&acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200536 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400537 printk(" S4");
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200538 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700539#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400540 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
541 if (ACPI_SUCCESS(status)) {
542 sleep_states[ACPI_STATE_S5] = 1;
543 printk(" S5");
544 pm_power_off_prepare = acpi_power_off_prepare;
545 pm_power_off = acpi_power_off;
546 }
547 printk(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
549}