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