blob: 5055acf2163c6d4e55fec3f68871c90ec6c41c1d [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];
26
Alexey Starikovskiy853298b2007-09-25 18:45:15 +040027#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020028static u32 acpi_target_sleep_state = ACPI_STATE_S0;
Alexey Starikovskiy853298b2007-09-25 18:45:15 +040029#endif
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020030
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020031int acpi_sleep_prepare(u32 acpi_state)
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
47 acpi_gpe_sleep_prepare(acpi_state);
48 acpi_enter_sleep_state_prep(acpi_state);
49 return 0;
50}
51
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020052#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct pm_ops acpi_pm_ops;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055extern void do_suspend_lowlevel(void);
56
57static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050058 [PM_SUSPEND_ON] = ACPI_STATE_S0,
59 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
60 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050061 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64static int init_8259A_after_S1;
65
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020066/**
67 * acpi_pm_set_target - Set the target system sleep state to the state
68 * associated with given @pm_state, if supported.
69 */
70
71static int acpi_pm_set_target(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020074 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020076 if (sleep_states[acpi_state]) {
77 acpi_target_sleep_state = acpi_state;
78 } else {
79 printk(KERN_ERR "ACPI does not support this state: %d\n",
80 pm_state);
81 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020083 return error;
84}
85
86/**
87 * acpi_pm_prepare - Do preliminary suspend work.
88 * @pm_state: ignored
89 *
90 * If necessary, set the firmware waking vector and do arch-specific
91 * nastiness to get the wakeup code to the waking vector.
92 */
93
94static int acpi_pm_prepare(suspend_state_t pm_state)
95{
96 int error = acpi_sleep_prepare(acpi_target_sleep_state);
97
98 if (error)
99 acpi_target_sleep_state = ACPI_STATE_S0;
100
101 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/**
105 * acpi_pm_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200106 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200108 * Flush caches and go to sleep. For STR we have to call arch-specific
109 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * It's unfortunate, but it works. Please fix if you're feeling frisky.
111 */
112
113static int acpi_pm_enter(suspend_state_t pm_state)
114{
115 acpi_status status = AE_OK;
116 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200117 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 ACPI_FLUSH_CPU_CACHE();
120
121 /* Do arch specific saving of state. */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200122 if (acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200124
125 if (error) {
126 acpi_target_sleep_state = ACPI_STATE_S0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return error;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 local_irq_save(flags);
132 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200133 switch (acpi_state) {
134 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 barrier();
136 status = acpi_enter_sleep_state(acpi_state);
137 break;
138
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200139 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 do_suspend_lowlevel();
141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400143
144 /* ACPI 3.0 specs (P62) says that it's the responsabilty
145 * of the OSPM to clear the status bit [ implying that the
146 * POWER_BUTTON event should not reach userspace ]
147 */
148 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
149 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 local_irq_restore(flags);
152 printk(KERN_DEBUG "Back to C!\n");
153
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200154 /* restore processor state */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200155 if (acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 acpi_restore_state_mem();
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/**
162 * acpi_pm_finish - Finish up suspend sequence.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200163 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * This is called after we wake back up (or if entering the sleep state
166 * failed).
167 */
168
169static int acpi_pm_finish(suspend_state_t pm_state)
170{
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200171 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 acpi_leave_sleep_state(acpi_state);
174 acpi_disable_wakeup_device(acpi_state);
175
176 /* reset firmware waking vector */
177 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
178
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200179 acpi_target_sleep_state = ACPI_STATE_S0;
180
Len Browne8b2fd02007-07-24 22:26:33 -0400181#ifdef CONFIG_X86
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (init_8259A_after_S1) {
183 printk("Broken toshiba laptop -> kicking interrupts\n");
184 init_8259A(0);
185 }
Len Browne8b2fd02007-07-24 22:26:33 -0400186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return 0;
188}
189
Shaohua Lieb9289e2005-10-30 15:00:01 -0800190static int acpi_pm_state_valid(suspend_state_t pm_state)
191{
Johannes Berge8c9c502007-04-30 15:09:54 -0700192 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800193
Johannes Berge8c9c502007-04-30 15:09:54 -0700194 switch (pm_state) {
195 case PM_SUSPEND_ON:
196 case PM_SUSPEND_STANDBY:
197 case PM_SUSPEND_MEM:
198 acpi_state = acpi_suspend_states[pm_state];
199
200 return sleep_states[acpi_state];
201 default:
202 return 0;
203 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static struct pm_ops acpi_pm_ops = {
Shaohua Lieb9289e2005-10-30 15:00:01 -0800207 .valid = acpi_pm_state_valid,
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200208 .set_target = acpi_pm_set_target,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500209 .prepare = acpi_pm_prepare,
210 .enter = acpi_pm_enter,
211 .finish = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200214/*
215 * Toshiba fails to preserve interrupts over S1, reinitialization
216 * of 8259 is needed after S1 resume.
217 */
Jeff Garzik18552562007-10-03 15:15:40 -0400218static int __init init_ints_after_s1(const struct dmi_system_id *d)
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200219{
220 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
221 init_8259A_after_S1 = 1;
222 return 0;
223}
224
225static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
226 {
227 .callback = init_ints_after_s1,
228 .ident = "Toshiba Satellite 4030cdt",
229 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
230 },
231 {},
232};
233#endif /* CONFIG_SUSPEND */
234
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200235#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700236static int acpi_hibernation_prepare(void)
237{
238 return acpi_sleep_prepare(ACPI_STATE_S4);
239}
240
241static int acpi_hibernation_enter(void)
242{
243 acpi_status status = AE_OK;
244 unsigned long flags = 0;
245
246 ACPI_FLUSH_CPU_CACHE();
247
248 local_irq_save(flags);
249 acpi_enable_wakeup_device(ACPI_STATE_S4);
250 /* This shouldn't return. If it returns, we have a problem */
251 status = acpi_enter_sleep_state(ACPI_STATE_S4);
252 local_irq_restore(flags);
253
254 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
255}
256
257static void acpi_hibernation_finish(void)
258{
259 acpi_leave_sleep_state(ACPI_STATE_S4);
260 acpi_disable_wakeup_device(ACPI_STATE_S4);
261
262 /* reset firmware waking vector */
263 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700264}
265
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700266static int acpi_hibernation_pre_restore(void)
267{
268 acpi_status status;
269
270 status = acpi_hw_disable_all_gpes();
271
272 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
273}
274
275static void acpi_hibernation_restore_cleanup(void)
276{
277 acpi_hw_enable_all_runtime_gpes();
278}
279
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700280static struct hibernation_ops acpi_hibernation_ops = {
281 .prepare = acpi_hibernation_prepare,
282 .enter = acpi_hibernation_enter,
283 .finish = acpi_hibernation_finish,
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700284 .pre_restore = acpi_hibernation_pre_restore,
285 .restore_cleanup = acpi_hibernation_restore_cleanup,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700286};
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200287#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700288
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200289int acpi_suspend(u32 acpi_state)
290{
291 suspend_state_t states[] = {
292 [1] = PM_SUSPEND_STANDBY,
293 [3] = PM_SUSPEND_MEM,
294 [5] = PM_SUSPEND_MAX
295 };
296
297 if (acpi_state < 6 && states[acpi_state])
298 return pm_suspend(states[acpi_state]);
299 if (acpi_state == 4)
300 return hibernate();
301 return -EINVAL;
302}
303
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400304#ifdef CONFIG_PM_SLEEP
Shaohua Lifd4aff12007-07-17 22:40:25 +0200305/**
306 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
307 * in the system sleep state given by %acpi_target_sleep_state
308 * @dev: device to examine
309 * @wake: if set, the device should be able to wake up the system
310 * @d_min_p: used to store the upper limit of allowed states range
311 * Return value: preferred power state of the device on success, -ENODEV on
312 * failure (ie. if there's no 'struct acpi_device' for @dev)
313 *
314 * Find the lowest power (highest number) ACPI device power state that
315 * device @dev can be in while the system is in the sleep state represented
316 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
317 * able to wake up the system from this sleep state. If @d_min_p is set,
318 * the highest power (lowest number) device power state of @dev allowed
319 * in this system sleep state is stored at the location pointed to by it.
320 *
321 * The caller must ensure that @dev is valid before using this function.
322 * The caller is also responsible for figuring out if the device is
323 * supposed to be able to wake up the system and passing this information
324 * via @wake.
325 */
326
327int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
328{
329 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
330 struct acpi_device *adev;
331 char acpi_method[] = "_SxD";
332 unsigned long d_min, d_max;
333
334 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800335 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200336 return -ENODEV;
337 }
338
339 acpi_method[2] = '0' + acpi_target_sleep_state;
340 /*
341 * If the sleep state is S0, we will return D3, but if the device has
342 * _S0W, we will use the value from _S0W
343 */
344 d_min = ACPI_STATE_D0;
345 d_max = ACPI_STATE_D3;
346
347 /*
348 * If present, _SxD methods return the minimum D-state (highest power
349 * state) we can use for the corresponding S-states. Otherwise, the
350 * minimum D-state is D0 (ACPI 3.x).
351 *
352 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
353 * provided -- that's our fault recovery, we ignore retval.
354 */
355 if (acpi_target_sleep_state > ACPI_STATE_S0)
356 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
357
358 /*
359 * If _PRW says we can wake up the system from the target sleep state,
360 * the D-state returned by _SxD is sufficient for that (we assume a
361 * wakeup-aware driver if wake is set). Still, if _SxW exists
362 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
363 * can wake the system. _S0W may be valid, too.
364 */
365 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
366 (wake && adev->wakeup.state.enabled &&
367 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
368 acpi_method[3] = 'W';
369 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
370 /* Sanity check */
371 if (d_max < d_min)
372 d_min = d_max;
373 }
374
375 if (d_min_p)
376 *d_min_p = d_min;
377 return d_max;
378}
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400379#endif
Shaohua Lifd4aff12007-07-17 22:40:25 +0200380
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400381static void acpi_power_off_prepare(void)
382{
383 /* Prepare to power off the system */
384 acpi_sleep_prepare(ACPI_STATE_S5);
385}
386
387static void acpi_power_off(void)
388{
389 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
390 printk("%s called\n", __FUNCTION__);
391 local_irq_disable();
392 acpi_enter_sleep_state(ACPI_STATE_S5);
393}
394
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500395int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200397 acpi_status status;
398 u8 type_a, type_b;
399#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500400 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 dmi_check_system(acpisleep_dmi_table);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (acpi_disabled)
406 return 0;
407
Frans Pop5a50fe72007-09-20 22:27:44 +0200408 sleep_states[ACPI_STATE_S0] = 1;
409 printk(KERN_INFO PREFIX "(supports S0");
410
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200411#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200412 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
414 if (ACPI_SUCCESS(status)) {
415 sleep_states[i] = 1;
416 printk(" S%d", i);
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 pm_set_ops(&acpi_pm_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200421#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700422
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200423#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200424 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
425 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700426 hibernation_set_ops(&acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200427 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400428 printk(" S4");
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200429 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700430#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400431 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
432 if (ACPI_SUCCESS(status)) {
433 sleep_states[ACPI_STATE_S5] = 1;
434 printk(" S5");
435 pm_power_off_prepare = acpi_power_off_prepare;
436 pm_power_off = acpi_power_off;
437 }
438 printk(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440}