blob: 55eca6eabcd8e2b38187ada6f123eecb36f250cb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <acpi/acpi_bus.h>
19#include <acpi/acpi_drivers.h>
20#include "sleep.h"
21
22u8 sleep_states[ACPI_S_STATE_COUNT];
23
24static struct pm_ops acpi_pm_ops;
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026extern void do_suspend_lowlevel(void);
27
28static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050029 [PM_SUSPEND_ON] = ACPI_STATE_S0,
30 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
31 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050032 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35static int init_8259A_after_S1;
36
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050037extern int acpi_sleep_prepare(u32 acpi_state);
38extern void acpi_power_off(void);
39
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020040static u32 acpi_target_sleep_state = ACPI_STATE_S0;
41
42/**
43 * acpi_pm_set_target - Set the target system sleep state to the state
44 * associated with given @pm_state, if supported.
45 */
46
47static int acpi_pm_set_target(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020050 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020052 if (sleep_states[acpi_state]) {
53 acpi_target_sleep_state = acpi_state;
54 } else {
55 printk(KERN_ERR "ACPI does not support this state: %d\n",
56 pm_state);
57 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020059 return error;
60}
61
62/**
63 * acpi_pm_prepare - Do preliminary suspend work.
64 * @pm_state: ignored
65 *
66 * If necessary, set the firmware waking vector and do arch-specific
67 * nastiness to get the wakeup code to the waking vector.
68 */
69
70static int acpi_pm_prepare(suspend_state_t pm_state)
71{
72 int error = acpi_sleep_prepare(acpi_target_sleep_state);
73
74 if (error)
75 acpi_target_sleep_state = ACPI_STATE_S0;
76
77 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/**
81 * acpi_pm_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020082 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020084 * Flush caches and go to sleep. For STR or S2, we have to call
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * arch-specific assembly, which in turn call acpi_enter_sleep_state().
86 * It's unfortunate, but it works. Please fix if you're feeling frisky.
87 */
88
89static int acpi_pm_enter(suspend_state_t pm_state)
90{
91 acpi_status status = AE_OK;
92 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020093 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 ACPI_FLUSH_CPU_CACHE();
96
97 /* Do arch specific saving of state. */
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +020098 if (acpi_state == ACPI_STATE_S2 || acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200100
101 if (error) {
102 acpi_target_sleep_state = ACPI_STATE_S0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return error;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 local_irq_save(flags);
108 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200109 switch (acpi_state) {
110 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 barrier();
112 status = acpi_enter_sleep_state(acpi_state);
113 break;
114
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200115 case ACPI_STATE_S2:
116 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 do_suspend_lowlevel();
118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400120
121 /* ACPI 3.0 specs (P62) says that it's the responsabilty
122 * of the OSPM to clear the status bit [ implying that the
123 * POWER_BUTTON event should not reach userspace ]
124 */
125 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
126 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 local_irq_restore(flags);
129 printk(KERN_DEBUG "Back to C!\n");
130
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200131 /* restore processor state */
132 if (acpi_state == ACPI_STATE_S2 || acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 acpi_restore_state_mem();
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/**
139 * acpi_pm_finish - Finish up suspend sequence.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200140 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 *
142 * This is called after we wake back up (or if entering the sleep state
143 * failed).
144 */
145
146static int acpi_pm_finish(suspend_state_t pm_state)
147{
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200148 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 acpi_leave_sleep_state(acpi_state);
151 acpi_disable_wakeup_device(acpi_state);
152
153 /* reset firmware waking vector */
154 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
155
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200156 acpi_target_sleep_state = ACPI_STATE_S0;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (init_8259A_after_S1) {
159 printk("Broken toshiba laptop -> kicking interrupts\n");
160 init_8259A(0);
161 }
162 return 0;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165int acpi_suspend(u32 acpi_state)
166{
167 suspend_state_t states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500168 [1] = PM_SUSPEND_STANDBY,
169 [3] = PM_SUSPEND_MEM,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500170 [5] = PM_SUSPEND_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 };
172
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500173 if (acpi_state < 6 && states[acpi_state])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return pm_suspend(states[acpi_state]);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700175 if (acpi_state == 4)
176 return hibernate();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -EINVAL;
178}
179
Shaohua Lieb9289e2005-10-30 15:00:01 -0800180static int acpi_pm_state_valid(suspend_state_t pm_state)
181{
Johannes Berge8c9c502007-04-30 15:09:54 -0700182 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800183
Johannes Berge8c9c502007-04-30 15:09:54 -0700184 switch (pm_state) {
185 case PM_SUSPEND_ON:
186 case PM_SUSPEND_STANDBY:
187 case PM_SUSPEND_MEM:
188 acpi_state = acpi_suspend_states[pm_state];
189
190 return sleep_states[acpi_state];
191 default:
192 return 0;
193 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static struct pm_ops acpi_pm_ops = {
Shaohua Lieb9289e2005-10-30 15:00:01 -0800197 .valid = acpi_pm_state_valid,
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200198 .set_target = acpi_pm_set_target,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500199 .prepare = acpi_pm_prepare,
200 .enter = acpi_pm_enter,
201 .finish = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700204#ifdef CONFIG_SOFTWARE_SUSPEND
205static int acpi_hibernation_prepare(void)
206{
207 return acpi_sleep_prepare(ACPI_STATE_S4);
208}
209
210static int acpi_hibernation_enter(void)
211{
212 acpi_status status = AE_OK;
213 unsigned long flags = 0;
214
215 ACPI_FLUSH_CPU_CACHE();
216
217 local_irq_save(flags);
218 acpi_enable_wakeup_device(ACPI_STATE_S4);
219 /* This shouldn't return. If it returns, we have a problem */
220 status = acpi_enter_sleep_state(ACPI_STATE_S4);
221 local_irq_restore(flags);
222
223 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
224}
225
226static void acpi_hibernation_finish(void)
227{
228 acpi_leave_sleep_state(ACPI_STATE_S4);
229 acpi_disable_wakeup_device(ACPI_STATE_S4);
230
231 /* reset firmware waking vector */
232 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
233
234 if (init_8259A_after_S1) {
235 printk("Broken toshiba laptop -> kicking interrupts\n");
236 init_8259A(0);
237 }
238}
239
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700240static int acpi_hibernation_pre_restore(void)
241{
242 acpi_status status;
243
244 status = acpi_hw_disable_all_gpes();
245
246 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
247}
248
249static void acpi_hibernation_restore_cleanup(void)
250{
251 acpi_hw_enable_all_runtime_gpes();
252}
253
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700254static struct hibernation_ops acpi_hibernation_ops = {
255 .prepare = acpi_hibernation_prepare,
256 .enter = acpi_hibernation_enter,
257 .finish = acpi_hibernation_finish,
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700258 .pre_restore = acpi_hibernation_pre_restore,
259 .restore_cleanup = acpi_hibernation_restore_cleanup,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700260};
Len Brownfd350942007-05-09 23:34:35 -0400261#endif /* CONFIG_SOFTWARE_SUSPEND */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700262
Shaohua Lifd4aff12007-07-17 22:40:25 +0200263/**
264 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
265 * in the system sleep state given by %acpi_target_sleep_state
266 * @dev: device to examine
267 * @wake: if set, the device should be able to wake up the system
268 * @d_min_p: used to store the upper limit of allowed states range
269 * Return value: preferred power state of the device on success, -ENODEV on
270 * failure (ie. if there's no 'struct acpi_device' for @dev)
271 *
272 * Find the lowest power (highest number) ACPI device power state that
273 * device @dev can be in while the system is in the sleep state represented
274 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
275 * able to wake up the system from this sleep state. If @d_min_p is set,
276 * the highest power (lowest number) device power state of @dev allowed
277 * in this system sleep state is stored at the location pointed to by it.
278 *
279 * The caller must ensure that @dev is valid before using this function.
280 * The caller is also responsible for figuring out if the device is
281 * supposed to be able to wake up the system and passing this information
282 * via @wake.
283 */
284
285int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
286{
287 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
288 struct acpi_device *adev;
289 char acpi_method[] = "_SxD";
290 unsigned long d_min, d_max;
291
292 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
293 printk(KERN_ERR "ACPI handle has no context!\n");
294 return -ENODEV;
295 }
296
297 acpi_method[2] = '0' + acpi_target_sleep_state;
298 /*
299 * If the sleep state is S0, we will return D3, but if the device has
300 * _S0W, we will use the value from _S0W
301 */
302 d_min = ACPI_STATE_D0;
303 d_max = ACPI_STATE_D3;
304
305 /*
306 * If present, _SxD methods return the minimum D-state (highest power
307 * state) we can use for the corresponding S-states. Otherwise, the
308 * minimum D-state is D0 (ACPI 3.x).
309 *
310 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
311 * provided -- that's our fault recovery, we ignore retval.
312 */
313 if (acpi_target_sleep_state > ACPI_STATE_S0)
314 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
315
316 /*
317 * If _PRW says we can wake up the system from the target sleep state,
318 * the D-state returned by _SxD is sufficient for that (we assume a
319 * wakeup-aware driver if wake is set). Still, if _SxW exists
320 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
321 * can wake the system. _S0W may be valid, too.
322 */
323 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
324 (wake && adev->wakeup.state.enabled &&
325 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
326 acpi_method[3] = 'W';
327 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
328 /* Sanity check */
329 if (d_max < d_min)
330 d_min = d_max;
331 }
332
333 if (d_min_p)
334 *d_min_p = d_min;
335 return d_max;
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*
339 * Toshiba fails to preserve interrupts over S1, reinitialization
340 * of 8259 is needed after S1 resume.
341 */
342static int __init init_ints_after_s1(struct dmi_system_id *d)
343{
344 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
345 init_8259A_after_S1 = 1;
346 return 0;
347}
348
349static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
350 {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500351 .callback = init_ints_after_s1,
352 .ident = "Toshiba Satellite 4030cdt",
353 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
354 },
355 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356};
357
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500358int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500360 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 dmi_check_system(acpisleep_dmi_table);
363
364 if (acpi_disabled)
365 return 0;
366
367 printk(KERN_INFO PREFIX "(supports");
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500368 for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 acpi_status status;
370 u8 type_a, type_b;
371 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
372 if (ACPI_SUCCESS(status)) {
373 sleep_states[i] = 1;
374 printk(" S%d", i);
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 printk(")\n");
378
379 pm_set_ops(&acpi_pm_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700380
381#ifdef CONFIG_SOFTWARE_SUSPEND
382 if (sleep_states[ACPI_STATE_S4])
383 hibernation_set_ops(&acpi_hibernation_ops);
384#else
385 sleep_states[ACPI_STATE_S4] = 0;
386#endif
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389}