blob: 10db8991bd15f78cad9a1486afc63cd284a9bcd4 [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
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020046int acpi_sleep_prepare(u32 acpi_state)
47{
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
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020062 acpi_enter_sleep_state_prep(acpi_state);
63 return 0;
64}
65
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020066#ifdef CONFIG_SUSPEND
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070067static struct platform_suspend_ops acpi_pm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069extern void do_suspend_lowlevel(void);
70
71static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050072 [PM_SUSPEND_ON] = ACPI_STATE_S0,
73 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
74 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050075 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
78static int init_8259A_after_S1;
79
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020080/**
Rafael J. Wysockic697eec2008-01-08 00:04:17 +010081 * acpi_pm_begin - Set the target system sleep state to the state
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020082 * associated with given @pm_state, if supported.
83 */
84
Rafael J. Wysockic697eec2008-01-08 00:04:17 +010085static int acpi_pm_begin(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020088 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020090 if (sleep_states[acpi_state]) {
91 acpi_target_sleep_state = acpi_state;
Rafael J. Wysocki60417f52008-01-08 00:07:39 +010092 if (new_pts_ordering)
93 return 0;
94
95 error = acpi_sleep_prepare(acpi_state);
96 if (error)
97 acpi_target_sleep_state = ACPI_STATE_S0;
98 else
99 acpi_sleep_finish_wake_up = true;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200100 } else {
101 printk(KERN_ERR "ACPI does not support this state: %d\n",
102 pm_state);
103 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200105 return error;
106}
107
108/**
109 * acpi_pm_prepare - Do preliminary suspend work.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200110 *
111 * If necessary, set the firmware waking vector and do arch-specific
112 * nastiness to get the wakeup code to the waking vector.
113 */
114
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700115static int acpi_pm_prepare(void)
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200116{
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100117 if (new_pts_ordering) {
118 int error = acpi_sleep_prepare(acpi_target_sleep_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200119
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100120 if (error) {
121 acpi_target_sleep_state = ACPI_STATE_S0;
122 return error;
123 }
124 acpi_sleep_finish_wake_up = true;
125 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200126
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100127 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/**
131 * acpi_pm_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200132 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200134 * Flush caches and go to sleep. For STR we have to call arch-specific
135 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * It's unfortunate, but it works. Please fix if you're feeling frisky.
137 */
138
139static int acpi_pm_enter(suspend_state_t pm_state)
140{
141 acpi_status status = AE_OK;
142 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200143 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 ACPI_FLUSH_CPU_CACHE();
146
147 /* Do arch specific saving of state. */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200148 if (acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200150
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100151 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return error;
153 }
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 local_irq_save(flags);
156 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200157 switch (acpi_state) {
158 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 barrier();
160 status = acpi_enter_sleep_state(acpi_state);
161 break;
162
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200163 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 do_suspend_lowlevel();
165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400167
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100168 /* Reprogram control registers and execute _BFS */
169 acpi_leave_sleep_state_prep(acpi_state);
170
Arnaud Patard872d83d2006-04-27 05:25:00 -0400171 /* ACPI 3.0 specs (P62) says that it's the responsabilty
172 * of the OSPM to clear the status bit [ implying that the
173 * POWER_BUTTON event should not reach userspace ]
174 */
175 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
176 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
177
Shaohua Lia3627f62007-06-20 09:17:58 +0800178 /*
179 * Disable and clear GPE status before interrupt is enabled. Some GPEs
180 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
181 * acpi_leave_sleep_state will reenable specific GPEs later
182 */
183 acpi_hw_disable_all_gpes();
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 local_irq_restore(flags);
186 printk(KERN_DEBUG "Back to C!\n");
187
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200188 /* restore processor state */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200189 if (acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 acpi_restore_state_mem();
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/**
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100196 * acpi_pm_finish - Instruct the platform to leave a sleep state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
198 * This is called after we wake back up (or if entering the sleep state
199 * failed).
200 */
201
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700202static void acpi_pm_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200204 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 acpi_disable_wakeup_device(acpi_state);
Alexey Starikovskiy1dbc1fd2007-10-22 14:18:12 +0400207 acpi_leave_sleep_state(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* reset firmware waking vector */
210 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
211
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200212 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100213 acpi_sleep_finish_wake_up = false;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200214
Len Browne8b2fd02007-07-24 22:26:33 -0400215#ifdef CONFIG_X86
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (init_8259A_after_S1) {
217 printk("Broken toshiba laptop -> kicking interrupts\n");
218 init_8259A(0);
219 }
Len Browne8b2fd02007-07-24 22:26:33 -0400220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100223/**
224 * acpi_pm_end - Finish up suspend sequence.
225 */
226
227static void acpi_pm_end(void)
228{
229 /*
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100230 * This is necessary in case acpi_pm_finish() is not called directly
231 * during a failing transition to a sleep state.
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100232 */
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100233 if (acpi_sleep_finish_wake_up)
234 acpi_pm_finish();
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100235}
236
Shaohua Lieb9289e2005-10-30 15:00:01 -0800237static int acpi_pm_state_valid(suspend_state_t pm_state)
238{
Johannes Berge8c9c502007-04-30 15:09:54 -0700239 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800240
Johannes Berge8c9c502007-04-30 15:09:54 -0700241 switch (pm_state) {
242 case PM_SUSPEND_ON:
243 case PM_SUSPEND_STANDBY:
244 case PM_SUSPEND_MEM:
245 acpi_state = acpi_suspend_states[pm_state];
246
247 return sleep_states[acpi_state];
248 default:
249 return 0;
250 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800251}
252
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700253static struct platform_suspend_ops acpi_pm_ops = {
Shaohua Lieb9289e2005-10-30 15:00:01 -0800254 .valid = acpi_pm_state_valid,
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100255 .begin = acpi_pm_begin,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500256 .prepare = acpi_pm_prepare,
257 .enter = acpi_pm_enter,
258 .finish = acpi_pm_finish,
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100259 .end = acpi_pm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200262/*
263 * Toshiba fails to preserve interrupts over S1, reinitialization
264 * of 8259 is needed after S1 resume.
265 */
Jeff Garzik18552562007-10-03 15:15:40 -0400266static int __init init_ints_after_s1(const struct dmi_system_id *d)
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200267{
268 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
269 init_8259A_after_S1 = 1;
270 return 0;
271}
272
273static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
274 {
275 .callback = init_ints_after_s1,
276 .ident = "Toshiba Satellite 4030cdt",
277 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
278 },
279 {},
280};
281#endif /* CONFIG_SUSPEND */
282
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200283#ifdef CONFIG_HIBERNATION
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100284static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700285{
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100286 int error;
287
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700288 acpi_target_sleep_state = ACPI_STATE_S4;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100289 if (new_pts_ordering)
290 return 0;
291
292 error = acpi_sleep_prepare(ACPI_STATE_S4);
293 if (error)
294 acpi_target_sleep_state = ACPI_STATE_S0;
295 else
296 acpi_sleep_finish_wake_up = true;
297
298 return error;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700299}
300
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700301static int acpi_hibernation_prepare(void)
302{
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100303 if (new_pts_ordering) {
304 int error = acpi_sleep_prepare(ACPI_STATE_S4);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100305
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100306 if (error) {
307 acpi_target_sleep_state = ACPI_STATE_S0;
308 return error;
309 }
310 acpi_sleep_finish_wake_up = true;
311 }
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100312
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100313 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700314}
315
316static int acpi_hibernation_enter(void)
317{
318 acpi_status status = AE_OK;
319 unsigned long flags = 0;
320
321 ACPI_FLUSH_CPU_CACHE();
322
323 local_irq_save(flags);
324 acpi_enable_wakeup_device(ACPI_STATE_S4);
325 /* This shouldn't return. If it returns, we have a problem */
326 status = acpi_enter_sleep_state(ACPI_STATE_S4);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100327 /* Reprogram control registers and execute _BFS */
328 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700329 local_irq_restore(flags);
330
331 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
332}
333
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700334static void acpi_hibernation_leave(void)
335{
336 /*
337 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
338 * enable it here.
339 */
340 acpi_enable();
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100341 /* Reprogram control registers and execute _BFS */
342 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700343}
344
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700345static void acpi_hibernation_finish(void)
346{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700347 acpi_disable_wakeup_device(ACPI_STATE_S4);
Alexey Starikovskiy1dbc1fd2007-10-22 14:18:12 +0400348 acpi_leave_sleep_state(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700349
350 /* reset firmware waking vector */
351 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700352
353 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100354 acpi_sleep_finish_wake_up = false;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700355}
356
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100357static void acpi_hibernation_end(void)
358{
359 /*
360 * This is necessary in case acpi_hibernation_finish() is not called
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100361 * directly during a failing transition to the sleep state.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100362 */
Rafael J. Wysocki7258ec52008-01-08 00:09:58 +0100363 if (acpi_sleep_finish_wake_up)
364 acpi_hibernation_finish();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100365}
366
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700367static int acpi_hibernation_pre_restore(void)
368{
369 acpi_status status;
370
371 status = acpi_hw_disable_all_gpes();
372
373 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
374}
375
376static void acpi_hibernation_restore_cleanup(void)
377{
378 acpi_hw_enable_all_runtime_gpes();
379}
380
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -0700381static struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100382 .begin = acpi_hibernation_begin,
383 .end = acpi_hibernation_end,
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700384 .pre_snapshot = acpi_hibernation_prepare,
385 .finish = acpi_hibernation_finish,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700386 .prepare = acpi_hibernation_prepare,
387 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700388 .leave = acpi_hibernation_leave,
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700389 .pre_restore = acpi_hibernation_pre_restore,
390 .restore_cleanup = acpi_hibernation_restore_cleanup,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700391};
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200392#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700393
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200394int acpi_suspend(u32 acpi_state)
395{
396 suspend_state_t states[] = {
397 [1] = PM_SUSPEND_STANDBY,
398 [3] = PM_SUSPEND_MEM,
399 [5] = PM_SUSPEND_MAX
400 };
401
402 if (acpi_state < 6 && states[acpi_state])
403 return pm_suspend(states[acpi_state]);
404 if (acpi_state == 4)
405 return hibernate();
406 return -EINVAL;
407}
408
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400409#ifdef CONFIG_PM_SLEEP
Shaohua Lifd4aff12007-07-17 22:40:25 +0200410/**
411 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
412 * in the system sleep state given by %acpi_target_sleep_state
413 * @dev: device to examine
414 * @wake: if set, the device should be able to wake up the system
415 * @d_min_p: used to store the upper limit of allowed states range
416 * Return value: preferred power state of the device on success, -ENODEV on
417 * failure (ie. if there's no 'struct acpi_device' for @dev)
418 *
419 * Find the lowest power (highest number) ACPI device power state that
420 * device @dev can be in while the system is in the sleep state represented
421 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
422 * able to wake up the system from this sleep state. If @d_min_p is set,
423 * the highest power (lowest number) device power state of @dev allowed
424 * in this system sleep state is stored at the location pointed to by it.
425 *
426 * The caller must ensure that @dev is valid before using this function.
427 * The caller is also responsible for figuring out if the device is
428 * supposed to be able to wake up the system and passing this information
429 * via @wake.
430 */
431
432int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
433{
434 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
435 struct acpi_device *adev;
436 char acpi_method[] = "_SxD";
437 unsigned long d_min, d_max;
438
439 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800440 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200441 return -ENODEV;
442 }
443
444 acpi_method[2] = '0' + acpi_target_sleep_state;
445 /*
446 * If the sleep state is S0, we will return D3, but if the device has
447 * _S0W, we will use the value from _S0W
448 */
449 d_min = ACPI_STATE_D0;
450 d_max = ACPI_STATE_D3;
451
452 /*
453 * If present, _SxD methods return the minimum D-state (highest power
454 * state) we can use for the corresponding S-states. Otherwise, the
455 * minimum D-state is D0 (ACPI 3.x).
456 *
457 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
458 * provided -- that's our fault recovery, we ignore retval.
459 */
460 if (acpi_target_sleep_state > ACPI_STATE_S0)
461 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
462
463 /*
464 * If _PRW says we can wake up the system from the target sleep state,
465 * the D-state returned by _SxD is sufficient for that (we assume a
466 * wakeup-aware driver if wake is set). Still, if _SxW exists
467 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
468 * can wake the system. _S0W may be valid, too.
469 */
470 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
471 (wake && adev->wakeup.state.enabled &&
472 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
473 acpi_method[3] = 'W';
474 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
475 /* Sanity check */
476 if (d_max < d_min)
477 d_min = d_max;
478 }
479
480 if (d_min_p)
481 *d_min_p = d_min;
482 return d_max;
483}
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400484#endif
Shaohua Lifd4aff12007-07-17 22:40:25 +0200485
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400486static void acpi_power_off_prepare(void)
487{
488 /* Prepare to power off the system */
489 acpi_sleep_prepare(ACPI_STATE_S5);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100490 acpi_hw_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400491}
492
493static void acpi_power_off(void)
494{
495 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
496 printk("%s called\n", __FUNCTION__);
497 local_irq_disable();
Alexey Starikovskiy9c1c6a12007-10-22 14:18:06 +0400498 acpi_enable_wakeup_device(ACPI_STATE_S5);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400499 acpi_enter_sleep_state(ACPI_STATE_S5);
500}
501
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500502int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200504 acpi_status status;
505 u8 type_a, type_b;
506#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500507 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 dmi_check_system(acpisleep_dmi_table);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if (acpi_disabled)
513 return 0;
514
Frans Pop5a50fe72007-09-20 22:27:44 +0200515 sleep_states[ACPI_STATE_S0] = 1;
516 printk(KERN_INFO PREFIX "(supports S0");
517
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200518#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200519 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
521 if (ACPI_SUCCESS(status)) {
522 sleep_states[i] = 1;
523 printk(" S%d", i);
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700527 suspend_set_ops(&acpi_pm_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200528#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700529
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200530#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200531 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
532 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700533 hibernation_set_ops(&acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200534 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400535 printk(" S4");
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200536 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700537#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400538 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
539 if (ACPI_SUCCESS(status)) {
540 sleep_states[ACPI_STATE_S5] = 1;
541 printk(" S5");
542 pm_power_off_prepare = acpi_power_off_prepare;
543 pm_power_off = acpi_power_off;
544 }
545 printk(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
547}