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