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