]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/sleep/main.c
ACPI: Introduce new device wakeup flag 'prepared'
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / sleep / main.c
CommitLineData
1da177e4
LT
1/*
2 * sleep.c - ACPI sleep support.
3 *
e2a5b420 4 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
1da177e4
LT
5 * 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>
f216cc37
AS
18
19#include <asm/io.h>
20
1da177e4
LT
21#include <acpi/acpi_bus.h>
22#include <acpi/acpi_drivers.h>
23#include "sleep.h"
24
25u8 sleep_states[ACPI_S_STATE_COUNT];
26
c9b6c8f6 27static int acpi_sleep_prepare(u32 acpi_state)
2f3f2226
AS
28{
29#ifdef CONFIG_ACPI_SLEEP
30 /* do we have a wakeup address for S2 and S3? */
31 if (acpi_state == ACPI_STATE_S3) {
32 if (!acpi_wakeup_address) {
33 return -EFAULT;
34 }
35 acpi_set_firmware_waking_vector((acpi_physical_address)
36 virt_to_phys((void *)
37 acpi_wakeup_address));
38
39 }
40 ACPI_FLUSH_CPU_CACHE();
41 acpi_enable_wakeup_device_prep(acpi_state);
42#endif
c9b6c8f6
RW
43 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
44 acpi_state);
2f3f2226
AS
45 acpi_enter_sleep_state_prep(acpi_state);
46 return 0;
47}
48
d8f3de0d
RW
49#ifdef CONFIG_PM_SLEEP
50static u32 acpi_target_sleep_state = ACPI_STATE_S0;
51
52/*
53 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
54 * user to request that behavior by using the 'acpi_old_suspend_ordering'
55 * kernel command line option that causes the following variable to be set.
56 */
57static bool old_suspend_ordering;
58
59void __init acpi_old_suspend_ordering(void)
60{
61 old_suspend_ordering = true;
62}
63
64/**
65 * acpi_pm_disable_gpes - Disable the GPEs.
66 */
67static int acpi_pm_disable_gpes(void)
68{
69 acpi_hw_disable_all_gpes();
70 return 0;
71}
72
73/**
74 * __acpi_pm_prepare - Prepare the platform to enter the target state.
75 *
76 * If necessary, set the firmware waking vector and do arch-specific
77 * nastiness to get the wakeup code to the waking vector.
78 */
79static int __acpi_pm_prepare(void)
80{
81 int error = acpi_sleep_prepare(acpi_target_sleep_state);
82
83 if (error)
84 acpi_target_sleep_state = ACPI_STATE_S0;
85 return error;
86}
87
88/**
89 * acpi_pm_prepare - Prepare the platform to enter the target sleep
90 * state and disable the GPEs.
91 */
92static int acpi_pm_prepare(void)
93{
94 int error = __acpi_pm_prepare();
95
96 if (!error)
97 acpi_hw_disable_all_gpes();
98 return error;
99}
100
101/**
102 * acpi_pm_finish - Instruct the platform to leave a sleep state.
103 *
104 * This is called after we wake back up (or if entering the sleep state
105 * failed).
106 */
107static void acpi_pm_finish(void)
108{
109 u32 acpi_state = acpi_target_sleep_state;
110
111 if (acpi_state == ACPI_STATE_S0)
112 return;
1da177e4 113
d8f3de0d
RW
114 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
115 acpi_state);
116 acpi_disable_wakeup_device(acpi_state);
117 acpi_leave_sleep_state(acpi_state);
118
119 /* reset firmware waking vector */
120 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
121
122 acpi_target_sleep_state = ACPI_STATE_S0;
123}
124
125/**
126 * acpi_pm_end - Finish up suspend sequence.
127 */
128static void acpi_pm_end(void)
129{
130 /*
131 * This is necessary in case acpi_pm_finish() is not called during a
132 * failing transition to a sleep state.
133 */
134 acpi_target_sleep_state = ACPI_STATE_S0;
135}
136#endif /* CONFIG_PM_SLEEP */
137
138#ifdef CONFIG_SUSPEND
1da177e4
LT
139extern void do_suspend_lowlevel(void);
140
141static u32 acpi_suspend_states[] = {
e2a5b420
AS
142 [PM_SUSPEND_ON] = ACPI_STATE_S0,
143 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
144 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
e2a5b420 145 [PM_SUSPEND_MAX] = ACPI_STATE_S5
1da177e4
LT
146};
147
e9b3aba8 148/**
2c6e33c3 149 * acpi_suspend_begin - Set the target system sleep state to the state
e9b3aba8
RW
150 * associated with given @pm_state, if supported.
151 */
2c6e33c3 152static int acpi_suspend_begin(suspend_state_t pm_state)
e9b3aba8
RW
153{
154 u32 acpi_state = acpi_suspend_states[pm_state];
155 int error = 0;
156
157 if (sleep_states[acpi_state]) {
158 acpi_target_sleep_state = acpi_state;
159 } else {
160 printk(KERN_ERR "ACPI does not support this state: %d\n",
161 pm_state);
162 error = -ENOSYS;
163 }
164 return error;
165}
166
1da177e4 167/**
2c6e33c3 168 * acpi_suspend_enter - Actually enter a sleep state.
e9b3aba8 169 * @pm_state: ignored
1da177e4 170 *
50ad147a
RW
171 * Flush caches and go to sleep. For STR we have to call arch-specific
172 * assembly, which in turn call acpi_enter_sleep_state().
1da177e4
LT
173 * It's unfortunate, but it works. Please fix if you're feeling frisky.
174 */
2c6e33c3 175static int acpi_suspend_enter(suspend_state_t pm_state)
1da177e4
LT
176{
177 acpi_status status = AE_OK;
178 unsigned long flags = 0;
e9b3aba8 179 u32 acpi_state = acpi_target_sleep_state;
1da177e4
LT
180
181 ACPI_FLUSH_CPU_CACHE();
182
183 /* Do arch specific saving of state. */
50ad147a 184 if (acpi_state == ACPI_STATE_S3) {
1da177e4 185 int error = acpi_save_state_mem();
e9b3aba8 186
60417f59 187 if (error)
1da177e4
LT
188 return error;
189 }
190
1da177e4
LT
191 local_irq_save(flags);
192 acpi_enable_wakeup_device(acpi_state);
e9b3aba8
RW
193 switch (acpi_state) {
194 case ACPI_STATE_S1:
1da177e4
LT
195 barrier();
196 status = acpi_enter_sleep_state(acpi_state);
197 break;
198
e9b3aba8 199 case ACPI_STATE_S3:
1da177e4
LT
200 do_suspend_lowlevel();
201 break;
1da177e4 202 }
872d83d0 203
c95d47a8
RW
204 /* Reprogram control registers and execute _BFS */
205 acpi_leave_sleep_state_prep(acpi_state);
206
23b168d4 207 /* ACPI 3.0 specs (P62) says that it's the responsibility
872d83d0
AP
208 * of the OSPM to clear the status bit [ implying that the
209 * POWER_BUTTON event should not reach userspace ]
210 */
211 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
212 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
213
a3627f67
SL
214 /*
215 * Disable and clear GPE status before interrupt is enabled. Some GPEs
216 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
217 * acpi_leave_sleep_state will reenable specific GPEs later
218 */
219 acpi_hw_disable_all_gpes();
220
1da177e4
LT
221 local_irq_restore(flags);
222 printk(KERN_DEBUG "Back to C!\n");
223
e9b3aba8 224 /* restore processor state */
50ad147a 225 if (acpi_state == ACPI_STATE_S3)
1da177e4
LT
226 acpi_restore_state_mem();
227
1da177e4
LT
228 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
229}
230
2c6e33c3 231static int acpi_suspend_state_valid(suspend_state_t pm_state)
eb9289eb 232{
e8c9c502 233 u32 acpi_state;
eb9289eb 234
e8c9c502
JB
235 switch (pm_state) {
236 case PM_SUSPEND_ON:
237 case PM_SUSPEND_STANDBY:
238 case PM_SUSPEND_MEM:
239 acpi_state = acpi_suspend_states[pm_state];
240
241 return sleep_states[acpi_state];
242 default:
243 return 0;
244 }
eb9289eb
SL
245}
246
2c6e33c3
LB
247static struct platform_suspend_ops acpi_suspend_ops = {
248 .valid = acpi_suspend_state_valid,
249 .begin = acpi_suspend_begin,
d8f3de0d
RW
250 .prepare = acpi_pm_prepare,
251 .enter = acpi_suspend_enter,
252 .finish = acpi_pm_finish,
253 .end = acpi_pm_end,
254};
255
256/**
257 * acpi_suspend_begin_old - Set the target system sleep state to the
258 * state associated with given @pm_state, if supported, and
259 * execute the _PTS control method. This function is used if the
260 * pre-ACPI 2.0 suspend ordering has been requested.
261 */
262static int acpi_suspend_begin_old(suspend_state_t pm_state)
263{
264 int error = acpi_suspend_begin(pm_state);
265
266 if (!error)
267 error = __acpi_pm_prepare();
268 return error;
269}
270
271/*
272 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
273 * been requested.
274 */
275static struct platform_suspend_ops acpi_suspend_ops_old = {
276 .valid = acpi_suspend_state_valid,
277 .begin = acpi_suspend_begin_old,
278 .prepare = acpi_pm_disable_gpes,
2c6e33c3 279 .enter = acpi_suspend_enter,
d8f3de0d
RW
280 .finish = acpi_pm_finish,
281 .end = acpi_pm_end,
282 .recover = acpi_pm_finish,
1da177e4 283};
296699de
RW
284#endif /* CONFIG_SUSPEND */
285
b0cb1a19 286#ifdef CONFIG_HIBERNATION
caea99ef 287static int acpi_hibernation_begin(void)
74f270af
RW
288{
289 acpi_target_sleep_state = ACPI_STATE_S4;
7731ce63 290 return 0;
74f270af
RW
291}
292
a3d25c27
RW
293static int acpi_hibernation_enter(void)
294{
295 acpi_status status = AE_OK;
296 unsigned long flags = 0;
297
298 ACPI_FLUSH_CPU_CACHE();
299
300 local_irq_save(flags);
301 acpi_enable_wakeup_device(ACPI_STATE_S4);
302 /* This shouldn't return. If it returns, we have a problem */
303 status = acpi_enter_sleep_state(ACPI_STATE_S4);
c95d47a8
RW
304 /* Reprogram control registers and execute _BFS */
305 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
a3d25c27
RW
306 local_irq_restore(flags);
307
308 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
309}
310
c7e0831d
RW
311static void acpi_hibernation_leave(void)
312{
313 /*
314 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
315 * enable it here.
316 */
317 acpi_enable();
c95d47a8
RW
318 /* Reprogram control registers and execute _BFS */
319 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
c7e0831d
RW
320}
321
d8f3de0d 322static void acpi_pm_enable_gpes(void)
a3d25c27 323{
d8f3de0d 324 acpi_hw_enable_all_runtime_gpes();
a3d25c27
RW
325}
326
d8f3de0d
RW
327static struct platform_hibernation_ops acpi_hibernation_ops = {
328 .begin = acpi_hibernation_begin,
329 .end = acpi_pm_end,
330 .pre_snapshot = acpi_pm_prepare,
331 .finish = acpi_pm_finish,
332 .prepare = acpi_pm_prepare,
333 .enter = acpi_hibernation_enter,
334 .leave = acpi_hibernation_leave,
335 .pre_restore = acpi_pm_disable_gpes,
336 .restore_cleanup = acpi_pm_enable_gpes,
337};
caea99ef 338
d8f3de0d
RW
339/**
340 * acpi_hibernation_begin_old - Set the target system sleep state to
341 * ACPI_STATE_S4 and execute the _PTS control method. This
342 * function is used if the pre-ACPI 2.0 suspend ordering has been
343 * requested.
344 */
345static int acpi_hibernation_begin_old(void)
a634cc10 346{
d8f3de0d 347 int error = acpi_sleep_prepare(ACPI_STATE_S4);
a634cc10 348
d8f3de0d
RW
349 if (!error)
350 acpi_target_sleep_state = ACPI_STATE_S4;
351 return error;
a634cc10
RW
352}
353
d8f3de0d
RW
354/*
355 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
356 * been requested.
357 */
358static struct platform_hibernation_ops acpi_hibernation_ops_old = {
359 .begin = acpi_hibernation_begin_old,
360 .end = acpi_pm_end,
361 .pre_snapshot = acpi_pm_disable_gpes,
362 .finish = acpi_pm_finish,
363 .prepare = acpi_pm_disable_gpes,
a3d25c27 364 .enter = acpi_hibernation_enter,
c7e0831d 365 .leave = acpi_hibernation_leave,
d8f3de0d
RW
366 .pre_restore = acpi_pm_disable_gpes,
367 .restore_cleanup = acpi_pm_enable_gpes,
368 .recover = acpi_pm_finish,
a3d25c27 369};
d8f3de0d 370#endif /* CONFIG_HIBERNATION */
a3d25c27 371
296699de
RW
372int acpi_suspend(u32 acpi_state)
373{
374 suspend_state_t states[] = {
375 [1] = PM_SUSPEND_STANDBY,
376 [3] = PM_SUSPEND_MEM,
377 [5] = PM_SUSPEND_MAX
378 };
379
380 if (acpi_state < 6 && states[acpi_state])
381 return pm_suspend(states[acpi_state]);
382 if (acpi_state == 4)
383 return hibernate();
384 return -EINVAL;
385}
386
853298bc 387#ifdef CONFIG_PM_SLEEP
fd4aff1a
SL
388/**
389 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
390 * in the system sleep state given by %acpi_target_sleep_state
06166780
DB
391 * @dev: device to examine; its driver model wakeup flags control
392 * whether it should be able to wake up the system
fd4aff1a
SL
393 * @d_min_p: used to store the upper limit of allowed states range
394 * Return value: preferred power state of the device on success, -ENODEV on
395 * failure (ie. if there's no 'struct acpi_device' for @dev)
396 *
397 * Find the lowest power (highest number) ACPI device power state that
398 * device @dev can be in while the system is in the sleep state represented
399 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
400 * able to wake up the system from this sleep state. If @d_min_p is set,
401 * the highest power (lowest number) device power state of @dev allowed
402 * in this system sleep state is stored at the location pointed to by it.
403 *
404 * The caller must ensure that @dev is valid before using this function.
405 * The caller is also responsible for figuring out if the device is
406 * supposed to be able to wake up the system and passing this information
407 * via @wake.
408 */
409
06166780 410int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
fd4aff1a
SL
411{
412 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
413 struct acpi_device *adev;
414 char acpi_method[] = "_SxD";
415 unsigned long d_min, d_max;
416
417 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
ead77594 418 printk(KERN_DEBUG "ACPI handle has no context!\n");
fd4aff1a
SL
419 return -ENODEV;
420 }
421
422 acpi_method[2] = '0' + acpi_target_sleep_state;
423 /*
424 * If the sleep state is S0, we will return D3, but if the device has
425 * _S0W, we will use the value from _S0W
426 */
427 d_min = ACPI_STATE_D0;
428 d_max = ACPI_STATE_D3;
429
430 /*
431 * If present, _SxD methods return the minimum D-state (highest power
432 * state) we can use for the corresponding S-states. Otherwise, the
433 * minimum D-state is D0 (ACPI 3.x).
434 *
435 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
436 * provided -- that's our fault recovery, we ignore retval.
437 */
438 if (acpi_target_sleep_state > ACPI_STATE_S0)
439 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
440
441 /*
442 * If _PRW says we can wake up the system from the target sleep state,
443 * the D-state returned by _SxD is sufficient for that (we assume a
444 * wakeup-aware driver if wake is set). Still, if _SxW exists
445 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
446 * can wake the system. _S0W may be valid, too.
447 */
448 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
06166780 449 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
fd4aff1a 450 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
ad3399c3
RW
451 acpi_status status;
452
fd4aff1a 453 acpi_method[3] = 'W';
ad3399c3
RW
454 status = acpi_evaluate_integer(handle, acpi_method, NULL,
455 &d_max);
456 if (ACPI_FAILURE(status)) {
457 d_max = d_min;
458 } else if (d_max < d_min) {
459 /* Warn the user of the broken DSDT */
460 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
461 acpi_method);
462 /* Sanitize it */
fd4aff1a 463 d_min = d_max;
ad3399c3 464 }
fd4aff1a
SL
465 }
466
467 if (d_min_p)
468 *d_min_p = d_min;
469 return d_max;
470}
853298bc 471#endif
fd4aff1a 472
f216cc37
AS
473static void acpi_power_off_prepare(void)
474{
475 /* Prepare to power off the system */
476 acpi_sleep_prepare(ACPI_STATE_S5);
3c1d2b60 477 acpi_hw_disable_all_gpes();
f216cc37
AS
478}
479
480static void acpi_power_off(void)
481{
482 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
96b2dd1f 483 printk("%s called\n", __func__);
f216cc37 484 local_irq_disable();
9c1c6a1b 485 acpi_enable_wakeup_device(ACPI_STATE_S5);
f216cc37
AS
486 acpi_enter_sleep_state(ACPI_STATE_S5);
487}
488
aafbcd16 489int __init acpi_sleep_init(void)
1da177e4 490{
296699de
RW
491 acpi_status status;
492 u8 type_a, type_b;
493#ifdef CONFIG_SUSPEND
e2a5b420 494 int i = 0;
296699de 495#endif
1da177e4
LT
496
497 if (acpi_disabled)
498 return 0;
499
5a50fe70
FP
500 sleep_states[ACPI_STATE_S0] = 1;
501 printk(KERN_INFO PREFIX "(supports S0");
502
296699de 503#ifdef CONFIG_SUSPEND
5a50fe70 504 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
1da177e4
LT
505 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
506 if (ACPI_SUCCESS(status)) {
507 sleep_states[i] = 1;
508 printk(" S%d", i);
509 }
1da177e4 510 }
1da177e4 511
d8f3de0d
RW
512 suspend_set_ops(old_suspend_ordering ?
513 &acpi_suspend_ops_old : &acpi_suspend_ops);
296699de 514#endif
a3d25c27 515
b0cb1a19 516#ifdef CONFIG_HIBERNATION
296699de
RW
517 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
518 if (ACPI_SUCCESS(status)) {
d8f3de0d
RW
519 hibernation_set_ops(old_suspend_ordering ?
520 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
296699de 521 sleep_states[ACPI_STATE_S4] = 1;
f216cc37 522 printk(" S4");
296699de 523 }
a3d25c27 524#endif
f216cc37
AS
525 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
526 if (ACPI_SUCCESS(status)) {
527 sleep_states[ACPI_STATE_S5] = 1;
528 printk(" S5");
529 pm_power_off_prepare = acpi_power_off_prepare;
530 pm_power_off = acpi_power_off;
531 }
532 printk(")\n");
1da177e4
LT
533 return 0;
534}