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