]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/sleep/main.c
ACPI suspend: Call _PTS before suspending devices
[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
853298bc 27#ifdef CONFIG_PM_SLEEP
296699de 28static u32 acpi_target_sleep_state = ACPI_STATE_S0;
60417f59
RW
29static 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);
853298bc 44#endif
296699de 45
2f3f2226
AS
46int 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
2f3f2226
AS
62 acpi_enter_sleep_state_prep(acpi_state);
63 return 0;
64}
65
296699de 66#ifdef CONFIG_SUSPEND
26398a70 67static struct platform_suspend_ops acpi_pm_ops;
1da177e4 68
1da177e4
LT
69extern void do_suspend_lowlevel(void);
70
71static u32 acpi_suspend_states[] = {
e2a5b420
AS
72 [PM_SUSPEND_ON] = ACPI_STATE_S0,
73 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
74 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
e2a5b420 75 [PM_SUSPEND_MAX] = ACPI_STATE_S5
1da177e4
LT
76};
77
78static int init_8259A_after_S1;
79
e9b3aba8 80/**
c697eece 81 * acpi_pm_begin - Set the target system sleep state to the state
e9b3aba8
RW
82 * associated with given @pm_state, if supported.
83 */
84
c697eece 85static int acpi_pm_begin(suspend_state_t pm_state)
e9b3aba8
RW
86{
87 u32 acpi_state = acpi_suspend_states[pm_state];
88 int error = 0;
89
90 if (sleep_states[acpi_state]) {
91 acpi_target_sleep_state = acpi_state;
60417f59
RW
92 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;
e9b3aba8
RW
100 } else {
101 printk(KERN_ERR "ACPI does not support this state: %d\n",
102 pm_state);
103 error = -ENOSYS;
104 }
105 return error;
106}
107
1da177e4
LT
108/**
109 * acpi_pm_prepare - Do preliminary suspend work.
1da177e4 110 *
e9b3aba8
RW
111 * If necessary, set the firmware waking vector and do arch-specific
112 * nastiness to get the wakeup code to the waking vector.
1da177e4
LT
113 */
114
e6c5eb95 115static int acpi_pm_prepare(void)
1da177e4 116{
60417f59
RW
117 if (new_pts_ordering) {
118 int error = acpi_sleep_prepare(acpi_target_sleep_state);
1da177e4 119
60417f59
RW
120 if (error) {
121 acpi_target_sleep_state = ACPI_STATE_S0;
122 return error;
123 }
124 acpi_sleep_finish_wake_up = true;
125 }
e9b3aba8 126
60417f59 127 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
1da177e4
LT
128}
129
1da177e4
LT
130/**
131 * acpi_pm_enter - Actually enter a sleep state.
e9b3aba8 132 * @pm_state: ignored
1da177e4 133 *
50ad147a
RW
134 * Flush caches and go to sleep. For STR we have to call arch-specific
135 * assembly, which in turn call acpi_enter_sleep_state().
1da177e4
LT
136 * 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;
e9b3aba8 143 u32 acpi_state = acpi_target_sleep_state;
1da177e4
LT
144
145 ACPI_FLUSH_CPU_CACHE();
146
147 /* Do arch specific saving of state. */
50ad147a 148 if (acpi_state == ACPI_STATE_S3) {
1da177e4 149 int error = acpi_save_state_mem();
e9b3aba8 150
60417f59 151 if (error)
1da177e4
LT
152 return error;
153 }
154
1da177e4
LT
155 local_irq_save(flags);
156 acpi_enable_wakeup_device(acpi_state);
e9b3aba8
RW
157 switch (acpi_state) {
158 case ACPI_STATE_S1:
1da177e4
LT
159 barrier();
160 status = acpi_enter_sleep_state(acpi_state);
161 break;
162
e9b3aba8 163 case ACPI_STATE_S3:
1da177e4
LT
164 do_suspend_lowlevel();
165 break;
1da177e4 166 }
872d83d0 167
c95d47a8
RW
168 /* Reprogram control registers and execute _BFS */
169 acpi_leave_sleep_state_prep(acpi_state);
170
872d83d0
AP
171 /* 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
a3627f67
SL
178 /*
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
1da177e4
LT
185 local_irq_restore(flags);
186 printk(KERN_DEBUG "Back to C!\n");
187
e9b3aba8 188 /* restore processor state */
50ad147a 189 if (acpi_state == ACPI_STATE_S3)
1da177e4
LT
190 acpi_restore_state_mem();
191
1da177e4
LT
192 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
193}
194
1da177e4 195/**
c697eece 196 * acpi_pm_finish - Instruct the platform to leave a sleep state.
1da177e4
LT
197 *
198 * This is called after we wake back up (or if entering the sleep state
199 * failed).
200 */
201
e6c5eb95 202static void acpi_pm_finish(void)
1da177e4 203{
e9b3aba8 204 u32 acpi_state = acpi_target_sleep_state;
1da177e4 205
1da177e4 206 acpi_disable_wakeup_device(acpi_state);
1dbc1fda 207 acpi_leave_sleep_state(acpi_state);
1da177e4
LT
208
209 /* reset firmware waking vector */
210 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
211
e9b3aba8 212 acpi_target_sleep_state = ACPI_STATE_S0;
60417f59 213 acpi_sleep_finish_wake_up = false;
e9b3aba8 214
e8b2fd01 215#ifdef CONFIG_X86
1da177e4
LT
216 if (init_8259A_after_S1) {
217 printk("Broken toshiba laptop -> kicking interrupts\n");
218 init_8259A(0);
219 }
e8b2fd01 220#endif
1da177e4
LT
221}
222
c697eece
RW
223/**
224 * acpi_pm_end - Finish up suspend sequence.
225 */
226
227static void acpi_pm_end(void)
228{
229 /*
60417f59
RW
230 * This is necessary in case acpi_pm_finish() is not called directly
231 * during a failing transition to a sleep state.
c697eece 232 */
60417f59
RW
233 if (acpi_sleep_finish_wake_up)
234 acpi_pm_finish();
c697eece
RW
235}
236
eb9289eb
SL
237static int acpi_pm_state_valid(suspend_state_t pm_state)
238{
e8c9c502 239 u32 acpi_state;
eb9289eb 240
e8c9c502
JB
241 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 }
eb9289eb
SL
251}
252
26398a70 253static struct platform_suspend_ops acpi_pm_ops = {
eb9289eb 254 .valid = acpi_pm_state_valid,
c697eece 255 .begin = acpi_pm_begin,
e2a5b420
AS
256 .prepare = acpi_pm_prepare,
257 .enter = acpi_pm_enter,
258 .finish = acpi_pm_finish,
c697eece 259 .end = acpi_pm_end,
1da177e4
LT
260};
261
296699de
RW
262/*
263 * Toshiba fails to preserve interrupts over S1, reinitialization
264 * of 8259 is needed after S1 resume.
265 */
1855256c 266static int __init init_ints_after_s1(const struct dmi_system_id *d)
296699de
RW
267{
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
b0cb1a19 283#ifdef CONFIG_HIBERNATION
74f270af
RW
284static int acpi_hibernation_start(void)
285{
286 acpi_target_sleep_state = ACPI_STATE_S4;
287 return 0;
288}
289
a3d25c27
RW
290static int acpi_hibernation_prepare(void)
291{
3c1d2b60
RW
292 int error;
293
294 error = acpi_sleep_prepare(ACPI_STATE_S4);
295 if (error)
296 return error;
297
298 if (!ACPI_SUCCESS(acpi_hw_disable_all_gpes()))
299 error = -EFAULT;
300
301 return error;
a3d25c27
RW
302}
303
304static int acpi_hibernation_enter(void)
305{
306 acpi_status status = AE_OK;
307 unsigned long flags = 0;
308
309 ACPI_FLUSH_CPU_CACHE();
310
311 local_irq_save(flags);
312 acpi_enable_wakeup_device(ACPI_STATE_S4);
313 /* This shouldn't return. If it returns, we have a problem */
314 status = acpi_enter_sleep_state(ACPI_STATE_S4);
c95d47a8
RW
315 /* Reprogram control registers and execute _BFS */
316 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
a3d25c27
RW
317 local_irq_restore(flags);
318
319 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
320}
321
c7e0831d
RW
322static void acpi_hibernation_leave(void)
323{
324 /*
325 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
326 * enable it here.
327 */
328 acpi_enable();
c95d47a8
RW
329 /* Reprogram control registers and execute _BFS */
330 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
c7e0831d
RW
331}
332
a3d25c27
RW
333static void acpi_hibernation_finish(void)
334{
a3d25c27 335 acpi_disable_wakeup_device(ACPI_STATE_S4);
1dbc1fda 336 acpi_leave_sleep_state(ACPI_STATE_S4);
a3d25c27
RW
337
338 /* reset firmware waking vector */
339 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
74f270af
RW
340
341 acpi_target_sleep_state = ACPI_STATE_S0;
a3d25c27
RW
342}
343
a634cc10
RW
344static int acpi_hibernation_pre_restore(void)
345{
346 acpi_status status;
347
348 status = acpi_hw_disable_all_gpes();
349
350 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
351}
352
353static void acpi_hibernation_restore_cleanup(void)
354{
355 acpi_hw_enable_all_runtime_gpes();
356}
357
b3dac3b3 358static struct platform_hibernation_ops acpi_hibernation_ops = {
74f270af
RW
359 .start = acpi_hibernation_start,
360 .pre_snapshot = acpi_hibernation_prepare,
361 .finish = acpi_hibernation_finish,
a3d25c27
RW
362 .prepare = acpi_hibernation_prepare,
363 .enter = acpi_hibernation_enter,
c7e0831d 364 .leave = acpi_hibernation_leave,
a634cc10
RW
365 .pre_restore = acpi_hibernation_pre_restore,
366 .restore_cleanup = acpi_hibernation_restore_cleanup,
a3d25c27 367};
b0cb1a19 368#endif /* CONFIG_HIBERNATION */
a3d25c27 369
296699de
RW
370int acpi_suspend(u32 acpi_state)
371{
372 suspend_state_t states[] = {
373 [1] = PM_SUSPEND_STANDBY,
374 [3] = PM_SUSPEND_MEM,
375 [5] = PM_SUSPEND_MAX
376 };
377
378 if (acpi_state < 6 && states[acpi_state])
379 return pm_suspend(states[acpi_state]);
380 if (acpi_state == 4)
381 return hibernate();
382 return -EINVAL;
383}
384
853298bc 385#ifdef CONFIG_PM_SLEEP
fd4aff1a
SL
386/**
387 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
388 * in the system sleep state given by %acpi_target_sleep_state
389 * @dev: device to examine
390 * @wake: if set, the device should be able to wake up the system
391 * @d_min_p: used to store the upper limit of allowed states range
392 * Return value: preferred power state of the device on success, -ENODEV on
393 * failure (ie. if there's no 'struct acpi_device' for @dev)
394 *
395 * Find the lowest power (highest number) ACPI device power state that
396 * device @dev can be in while the system is in the sleep state represented
397 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
398 * able to wake up the system from this sleep state. If @d_min_p is set,
399 * the highest power (lowest number) device power state of @dev allowed
400 * in this system sleep state is stored at the location pointed to by it.
401 *
402 * The caller must ensure that @dev is valid before using this function.
403 * The caller is also responsible for figuring out if the device is
404 * supposed to be able to wake up the system and passing this information
405 * via @wake.
406 */
407
408int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
409{
410 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
411 struct acpi_device *adev;
412 char acpi_method[] = "_SxD";
413 unsigned long d_min, d_max;
414
415 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
ead77594 416 printk(KERN_DEBUG "ACPI handle has no context!\n");
fd4aff1a
SL
417 return -ENODEV;
418 }
419
420 acpi_method[2] = '0' + acpi_target_sleep_state;
421 /*
422 * If the sleep state is S0, we will return D3, but if the device has
423 * _S0W, we will use the value from _S0W
424 */
425 d_min = ACPI_STATE_D0;
426 d_max = ACPI_STATE_D3;
427
428 /*
429 * If present, _SxD methods return the minimum D-state (highest power
430 * state) we can use for the corresponding S-states. Otherwise, the
431 * minimum D-state is D0 (ACPI 3.x).
432 *
433 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
434 * provided -- that's our fault recovery, we ignore retval.
435 */
436 if (acpi_target_sleep_state > ACPI_STATE_S0)
437 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
438
439 /*
440 * If _PRW says we can wake up the system from the target sleep state,
441 * the D-state returned by _SxD is sufficient for that (we assume a
442 * wakeup-aware driver if wake is set). Still, if _SxW exists
443 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
444 * can wake the system. _S0W may be valid, too.
445 */
446 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
447 (wake && adev->wakeup.state.enabled &&
448 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
449 acpi_method[3] = 'W';
450 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
451 /* Sanity check */
452 if (d_max < d_min)
453 d_min = d_max;
454 }
455
456 if (d_min_p)
457 *d_min_p = d_min;
458 return d_max;
459}
853298bc 460#endif
fd4aff1a 461
f216cc37
AS
462static void acpi_power_off_prepare(void)
463{
464 /* Prepare to power off the system */
465 acpi_sleep_prepare(ACPI_STATE_S5);
3c1d2b60 466 acpi_hw_disable_all_gpes();
f216cc37
AS
467}
468
469static void acpi_power_off(void)
470{
471 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
472 printk("%s called\n", __FUNCTION__);
473 local_irq_disable();
9c1c6a1b 474 acpi_enable_wakeup_device(ACPI_STATE_S5);
f216cc37
AS
475 acpi_enter_sleep_state(ACPI_STATE_S5);
476}
477
aafbcd16 478int __init acpi_sleep_init(void)
1da177e4 479{
296699de
RW
480 acpi_status status;
481 u8 type_a, type_b;
482#ifdef CONFIG_SUSPEND
e2a5b420 483 int i = 0;
1da177e4
LT
484
485 dmi_check_system(acpisleep_dmi_table);
296699de 486#endif
1da177e4
LT
487
488 if (acpi_disabled)
489 return 0;
490
5a50fe70
FP
491 sleep_states[ACPI_STATE_S0] = 1;
492 printk(KERN_INFO PREFIX "(supports S0");
493
296699de 494#ifdef CONFIG_SUSPEND
5a50fe70 495 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
1da177e4
LT
496 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
497 if (ACPI_SUCCESS(status)) {
498 sleep_states[i] = 1;
499 printk(" S%d", i);
500 }
1da177e4 501 }
1da177e4 502
26398a70 503 suspend_set_ops(&acpi_pm_ops);
296699de 504#endif
a3d25c27 505
b0cb1a19 506#ifdef CONFIG_HIBERNATION
296699de
RW
507 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
508 if (ACPI_SUCCESS(status)) {
a3d25c27 509 hibernation_set_ops(&acpi_hibernation_ops);
296699de 510 sleep_states[ACPI_STATE_S4] = 1;
f216cc37 511 printk(" S4");
296699de 512 }
a3d25c27 513#endif
f216cc37
AS
514 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
515 if (ACPI_SUCCESS(status)) {
516 sleep_states[ACPI_STATE_S5] = 1;
517 printk(" S5");
518 pm_power_off_prepare = acpi_power_off_prepare;
519 pm_power_off = acpi_power_off;
520 }
521 printk(")\n");
1da177e4
LT
522 return 0;
523}