blob: 4c21480b58207c0132af414bb3c361204c3e0806 [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];
Rafael J. Wysocki5d1e0722008-10-22 14:58:43 -040026static u32 acpi_target_sleep_state = ACPI_STATE_S0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010028static int acpi_sleep_prepare(u32 acpi_state)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020029{
30#ifdef CONFIG_ACPI_SLEEP
31 /* do we have a wakeup address for S2 and S3? */
32 if (acpi_state == ACPI_STATE_S3) {
33 if (!acpi_wakeup_address) {
34 return -EFAULT;
35 }
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020036 acpi_set_firmware_waking_vector(
37 (acpi_physical_address)acpi_wakeup_address);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020038
39 }
40 ACPI_FLUSH_CPU_CACHE();
41 acpi_enable_wakeup_device_prep(acpi_state);
42#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010043 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
44 acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020045 acpi_enter_sleep_state_prep(acpi_state);
46 return 0;
47}
48
Rafael J. Wysocki5d1e0722008-10-22 14:58:43 -040049#ifdef CONFIG_ACPI_SLEEP
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +020050/*
51 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
52 * user to request that behavior by using the 'acpi_old_suspend_ordering'
53 * kernel command line option that causes the following variable to be set.
54 */
55static bool old_suspend_ordering;
56
57void __init acpi_old_suspend_ordering(void)
58{
59 old_suspend_ordering = true;
60}
61
62/**
63 * acpi_pm_disable_gpes - Disable the GPEs.
64 */
65static int acpi_pm_disable_gpes(void)
66{
67 acpi_hw_disable_all_gpes();
68 return 0;
69}
70
71/**
72 * __acpi_pm_prepare - Prepare the platform to enter the target state.
73 *
74 * If necessary, set the firmware waking vector and do arch-specific
75 * nastiness to get the wakeup code to the waking vector.
76 */
77static int __acpi_pm_prepare(void)
78{
79 int error = acpi_sleep_prepare(acpi_target_sleep_state);
80
81 if (error)
82 acpi_target_sleep_state = ACPI_STATE_S0;
83 return error;
84}
85
86/**
87 * acpi_pm_prepare - Prepare the platform to enter the target sleep
88 * state and disable the GPEs.
89 */
90static int acpi_pm_prepare(void)
91{
92 int error = __acpi_pm_prepare();
93
94 if (!error)
95 acpi_hw_disable_all_gpes();
96 return error;
97}
98
99/**
100 * acpi_pm_finish - Instruct the platform to leave a sleep state.
101 *
102 * This is called after we wake back up (or if entering the sleep state
103 * failed).
104 */
105static void acpi_pm_finish(void)
106{
107 u32 acpi_state = acpi_target_sleep_state;
108
109 if (acpi_state == ACPI_STATE_S0)
110 return;
111
112 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
113 acpi_state);
114 acpi_disable_wakeup_device(acpi_state);
115 acpi_leave_sleep_state(acpi_state);
116
117 /* reset firmware waking vector */
118 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
119
120 acpi_target_sleep_state = ACPI_STATE_S0;
121}
122
123/**
124 * acpi_pm_end - Finish up suspend sequence.
125 */
126static void acpi_pm_end(void)
127{
128 /*
129 * This is necessary in case acpi_pm_finish() is not called during a
130 * failing transition to a sleep state.
131 */
132 acpi_target_sleep_state = ACPI_STATE_S0;
133}
Rafael J. Wysocki5d1e0722008-10-22 14:58:43 -0400134#endif /* CONFIG_ACPI_SLEEP */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200135
136#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137extern void do_suspend_lowlevel(void);
138
139static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500140 [PM_SUSPEND_ON] = ACPI_STATE_S0,
141 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
142 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500143 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200146/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400147 * acpi_suspend_begin - Set the target system sleep state to the state
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200148 * associated with given @pm_state, if supported.
149 */
Len Brown2c6e33c2008-04-23 18:02:52 -0400150static int acpi_suspend_begin(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200153 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200155 if (sleep_states[acpi_state]) {
156 acpi_target_sleep_state = acpi_state;
157 } else {
158 printk(KERN_ERR "ACPI does not support this state: %d\n",
159 pm_state);
160 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200162 return error;
163}
164
165/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400166 * acpi_suspend_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200167 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200169 * Flush caches and go to sleep. For STR we have to call arch-specific
170 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * It's unfortunate, but it works. Please fix if you're feeling frisky.
172 */
Len Brown2c6e33c2008-04-23 18:02:52 -0400173static int acpi_suspend_enter(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 acpi_status status = AE_OK;
176 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200177 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 ACPI_FLUSH_CPU_CACHE();
180
181 /* Do arch specific saving of state. */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200182 if (acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200184
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100185 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return error;
187 }
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 local_irq_save(flags);
190 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200191 switch (acpi_state) {
192 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 barrier();
194 status = acpi_enter_sleep_state(acpi_state);
195 break;
196
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200197 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 do_suspend_lowlevel();
199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400201
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100202 /* Reprogram control registers and execute _BFS */
203 acpi_leave_sleep_state_prep(acpi_state);
204
Pavel Machek23b168d2008-02-05 19:27:12 +0100205 /* ACPI 3.0 specs (P62) says that it's the responsibility
Arnaud Patard872d83d2006-04-27 05:25:00 -0400206 * of the OSPM to clear the status bit [ implying that the
207 * POWER_BUTTON event should not reach userspace ]
208 */
209 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
210 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
211
Shaohua Lia3627f62007-06-20 09:17:58 +0800212 /*
213 * Disable and clear GPE status before interrupt is enabled. Some GPEs
214 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
215 * acpi_leave_sleep_state will reenable specific GPEs later
216 */
217 acpi_hw_disable_all_gpes();
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 local_irq_restore(flags);
220 printk(KERN_DEBUG "Back to C!\n");
221
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200222 /* restore processor state */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200223 if (acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 acpi_restore_state_mem();
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
227}
228
Len Brown2c6e33c2008-04-23 18:02:52 -0400229static int acpi_suspend_state_valid(suspend_state_t pm_state)
Shaohua Lieb9289e2005-10-30 15:00:01 -0800230{
Johannes Berge8c9c502007-04-30 15:09:54 -0700231 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800232
Johannes Berge8c9c502007-04-30 15:09:54 -0700233 switch (pm_state) {
234 case PM_SUSPEND_ON:
235 case PM_SUSPEND_STANDBY:
236 case PM_SUSPEND_MEM:
237 acpi_state = acpi_suspend_states[pm_state];
238
239 return sleep_states[acpi_state];
240 default:
241 return 0;
242 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800243}
244
Len Brown2c6e33c2008-04-23 18:02:52 -0400245static struct platform_suspend_ops acpi_suspend_ops = {
246 .valid = acpi_suspend_state_valid,
247 .begin = acpi_suspend_begin,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200248 .prepare = acpi_pm_prepare,
Len Brown2c6e33c2008-04-23 18:02:52 -0400249 .enter = acpi_suspend_enter,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200250 .finish = acpi_pm_finish,
251 .end = acpi_pm_end,
252};
253
254/**
255 * acpi_suspend_begin_old - Set the target system sleep state to the
256 * state associated with given @pm_state, if supported, and
257 * execute the _PTS control method. This function is used if the
258 * pre-ACPI 2.0 suspend ordering has been requested.
259 */
260static int acpi_suspend_begin_old(suspend_state_t pm_state)
261{
262 int error = acpi_suspend_begin(pm_state);
263
264 if (!error)
265 error = __acpi_pm_prepare();
266 return error;
267}
268
269/*
270 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
271 * been requested.
272 */
273static struct platform_suspend_ops acpi_suspend_ops_old = {
274 .valid = acpi_suspend_state_valid,
275 .begin = acpi_suspend_begin_old,
276 .prepare = acpi_pm_disable_gpes,
277 .enter = acpi_suspend_enter,
278 .finish = acpi_pm_finish,
279 .end = acpi_pm_end,
280 .recover = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700282
283static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
284{
285 old_suspend_ordering = true;
286 return 0;
287}
288
289static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
290 {
291 .callback = init_old_suspend_ordering,
292 .ident = "Abit KN9 (nForce4 variant)",
293 .matches = {
294 DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
295 DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
296 },
297 },
298 {},
299};
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200300#endif /* CONFIG_SUSPEND */
301
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200302#ifdef CONFIG_HIBERNATION
Shaohua Libdfe6b72008-07-23 21:28:41 -0700303static unsigned long s4_hardware_signature;
304static struct acpi_table_facs *facs;
305static bool nosigcheck;
306
307void __init acpi_no_s4_hw_signature(void)
308{
309 nosigcheck = true;
310}
311
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100312static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700313{
314 acpi_target_sleep_state = ACPI_STATE_S4;
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100315 return 0;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700316}
317
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700318static 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);
Shaohua Libdfe6b72008-07-23 21:28:41 -0700345 /* Check the hardware signature */
346 if (facs && s4_hardware_signature != facs->hardware_signature) {
347 printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
348 "cannot resume!\n");
349 panic("ACPI S4 hardware signature mismatch");
350 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700351}
352
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200353static void acpi_pm_enable_gpes(void)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700354{
355 acpi_hw_enable_all_runtime_gpes();
356}
357
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -0700358static struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100359 .begin = acpi_hibernation_begin,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200360 .end = acpi_pm_end,
361 .pre_snapshot = acpi_pm_prepare,
362 .finish = acpi_pm_finish,
363 .prepare = acpi_pm_prepare,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700364 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700365 .leave = acpi_hibernation_leave,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200366 .pre_restore = acpi_pm_disable_gpes,
367 .restore_cleanup = acpi_pm_enable_gpes,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700368};
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200369
370/**
371 * acpi_hibernation_begin_old - Set the target system sleep state to
372 * ACPI_STATE_S4 and execute the _PTS control method. This
373 * function is used if the pre-ACPI 2.0 suspend ordering has been
374 * requested.
375 */
376static int acpi_hibernation_begin_old(void)
377{
378 int error = acpi_sleep_prepare(ACPI_STATE_S4);
379
380 if (!error)
381 acpi_target_sleep_state = ACPI_STATE_S4;
382 return error;
383}
384
385/*
386 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
387 * been requested.
388 */
389static struct platform_hibernation_ops acpi_hibernation_ops_old = {
390 .begin = acpi_hibernation_begin_old,
391 .end = acpi_pm_end,
392 .pre_snapshot = acpi_pm_disable_gpes,
393 .finish = acpi_pm_finish,
394 .prepare = acpi_pm_disable_gpes,
395 .enter = acpi_hibernation_enter,
396 .leave = acpi_hibernation_leave,
397 .pre_restore = acpi_pm_disable_gpes,
398 .restore_cleanup = acpi_pm_enable_gpes,
399 .recover = acpi_pm_finish,
400};
401#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700402
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200403int acpi_suspend(u32 acpi_state)
404{
405 suspend_state_t states[] = {
406 [1] = PM_SUSPEND_STANDBY,
407 [3] = PM_SUSPEND_MEM,
408 [5] = PM_SUSPEND_MAX
409 };
410
411 if (acpi_state < 6 && states[acpi_state])
412 return pm_suspend(states[acpi_state]);
413 if (acpi_state == 4)
414 return hibernate();
415 return -EINVAL;
416}
417
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400418#ifdef CONFIG_PM_SLEEP
Shaohua Lifd4aff12007-07-17 22:40:25 +0200419/**
420 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
421 * in the system sleep state given by %acpi_target_sleep_state
David Brownell2fe2de52008-06-05 01:15:40 +0200422 * @dev: device to examine; its driver model wakeup flags control
423 * whether it should be able to wake up the system
Shaohua Lifd4aff12007-07-17 22:40:25 +0200424 * @d_min_p: used to store the upper limit of allowed states range
425 * Return value: preferred power state of the device on success, -ENODEV on
426 * failure (ie. if there's no 'struct acpi_device' for @dev)
427 *
428 * Find the lowest power (highest number) ACPI device power state that
429 * device @dev can be in while the system is in the sleep state represented
430 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
431 * able to wake up the system from this sleep state. If @d_min_p is set,
432 * the highest power (lowest number) device power state of @dev allowed
433 * in this system sleep state is stored at the location pointed to by it.
434 *
435 * The caller must ensure that @dev is valid before using this function.
436 * The caller is also responsible for figuring out if the device is
437 * supposed to be able to wake up the system and passing this information
438 * via @wake.
439 */
440
David Brownell2fe2de52008-06-05 01:15:40 +0200441int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
Shaohua Lifd4aff12007-07-17 22:40:25 +0200442{
443 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
444 struct acpi_device *adev;
445 char acpi_method[] = "_SxD";
446 unsigned long d_min, d_max;
447
448 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800449 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200450 return -ENODEV;
451 }
452
453 acpi_method[2] = '0' + acpi_target_sleep_state;
454 /*
455 * If the sleep state is S0, we will return D3, but if the device has
456 * _S0W, we will use the value from _S0W
457 */
458 d_min = ACPI_STATE_D0;
459 d_max = ACPI_STATE_D3;
460
461 /*
462 * If present, _SxD methods return the minimum D-state (highest power
463 * state) we can use for the corresponding S-states. Otherwise, the
464 * minimum D-state is D0 (ACPI 3.x).
465 *
466 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
467 * provided -- that's our fault recovery, we ignore retval.
468 */
469 if (acpi_target_sleep_state > ACPI_STATE_S0)
470 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
471
472 /*
473 * If _PRW says we can wake up the system from the target sleep state,
474 * the D-state returned by _SxD is sufficient for that (we assume a
475 * wakeup-aware driver if wake is set). Still, if _SxW exists
476 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
477 * can wake the system. _S0W may be valid, too.
478 */
479 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
David Brownell2fe2de52008-06-05 01:15:40 +0200480 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
Shaohua Lifd4aff12007-07-17 22:40:25 +0200481 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100482 acpi_status status;
483
Shaohua Lifd4aff12007-07-17 22:40:25 +0200484 acpi_method[3] = 'W';
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100485 status = acpi_evaluate_integer(handle, acpi_method, NULL,
486 &d_max);
487 if (ACPI_FAILURE(status)) {
488 d_max = d_min;
489 } else if (d_max < d_min) {
490 /* Warn the user of the broken DSDT */
491 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
492 acpi_method);
493 /* Sanitize it */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200494 d_min = d_max;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100495 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200496 }
497
498 if (d_min_p)
499 *d_min_p = d_min;
500 return d_max;
501}
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200502
503/**
504 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
505 * capability of given device
506 * @dev: device to handle
507 * @enable: 'true' - enable, 'false' - disable the wake-up capability
508 */
509int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
510{
511 acpi_handle handle;
512 struct acpi_device *adev;
513
514 if (!device_may_wakeup(dev))
515 return -EINVAL;
516
517 handle = DEVICE_ACPI_HANDLE(dev);
518 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
519 printk(KERN_DEBUG "ACPI handle has no context!\n");
520 return -ENODEV;
521 }
522
523 return enable ?
524 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
525 acpi_disable_wakeup_device_power(adev);
526}
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400527#endif
Shaohua Lifd4aff12007-07-17 22:40:25 +0200528
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400529static void acpi_power_off_prepare(void)
530{
531 /* Prepare to power off the system */
532 acpi_sleep_prepare(ACPI_STATE_S5);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100533 acpi_hw_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400534}
535
536static void acpi_power_off(void)
537{
538 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
Harvey Harrison96b2dd12008-03-05 18:24:51 -0800539 printk("%s called\n", __func__);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400540 local_irq_disable();
Alexey Starikovskiy9c1c6a12007-10-22 14:18:06 +0400541 acpi_enable_wakeup_device(ACPI_STATE_S5);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400542 acpi_enter_sleep_state(ACPI_STATE_S5);
543}
544
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500545int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200547 acpi_status status;
548 u8 type_a, type_b;
549#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500550 int i = 0;
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700551
552 dmi_check_system(acpisleep_dmi_table);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (acpi_disabled)
556 return 0;
557
Frans Pop5a50fe72007-09-20 22:27:44 +0200558 sleep_states[ACPI_STATE_S0] = 1;
559 printk(KERN_INFO PREFIX "(supports S0");
560
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200561#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200562 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
564 if (ACPI_SUCCESS(status)) {
565 sleep_states[i] = 1;
566 printk(" S%d", i);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200570 suspend_set_ops(old_suspend_ordering ?
571 &acpi_suspend_ops_old : &acpi_suspend_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200572#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700573
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200574#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200575 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
576 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200577 hibernation_set_ops(old_suspend_ordering ?
578 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200579 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400580 printk(" S4");
Shaohua Libdfe6b72008-07-23 21:28:41 -0700581 if (!nosigcheck) {
582 acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
583 (struct acpi_table_header **)&facs);
584 if (facs)
585 s4_hardware_signature =
586 facs->hardware_signature;
587 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200588 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700589#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400590 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
591 if (ACPI_SUCCESS(status)) {
592 sleep_states[ACPI_STATE_S5] = 1;
593 printk(" S5");
594 pm_power_off_prepare = acpi_power_off_prepare;
595 pm_power_off = acpi_power_off;
596 }
597 printk(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599}