Hibernation: Make sure that ACPI is enabled in acpi_hibernation_finish
[linux-2.6.git] / drivers / acpi / sleep / main.c
1 /*
2  * sleep.c - ACPI sleep support.
3  *
4  * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
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>
18
19 #include <asm/io.h>
20
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
27 #ifdef CONFIG_PM_SLEEP
28 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
29 #endif
30
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
52 #ifdef CONFIG_SUSPEND
53 static struct pm_ops acpi_pm_ops;
54
55 extern void do_suspend_lowlevel(void);
56
57 static u32 acpi_suspend_states[] = {
58         [PM_SUSPEND_ON] = ACPI_STATE_S0,
59         [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
60         [PM_SUSPEND_MEM] = ACPI_STATE_S3,
61         [PM_SUSPEND_MAX] = ACPI_STATE_S5
62 };
63
64 static int init_8259A_after_S1;
65
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
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
94 static 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;
102 }
103
104 /**
105  *      acpi_pm_enter - Actually enter a sleep state.
106  *      @pm_state: ignored
107  *
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().
110  *      It's unfortunate, but it works. Please fix if you're feeling frisky.
111  */
112
113 static int acpi_pm_enter(suspend_state_t pm_state)
114 {
115         acpi_status status = AE_OK;
116         unsigned long flags = 0;
117         u32 acpi_state = acpi_target_sleep_state;
118
119         ACPI_FLUSH_CPU_CACHE();
120
121         /* Do arch specific saving of state. */
122         if (acpi_state == ACPI_STATE_S3) {
123                 int error = acpi_save_state_mem();
124
125                 if (error) {
126                         acpi_target_sleep_state = ACPI_STATE_S0;
127                         return error;
128                 }
129         }
130
131         local_irq_save(flags);
132         acpi_enable_wakeup_device(acpi_state);
133         switch (acpi_state) {
134         case ACPI_STATE_S1:
135                 barrier();
136                 status = acpi_enter_sleep_state(acpi_state);
137                 break;
138
139         case ACPI_STATE_S3:
140                 do_suspend_lowlevel();
141                 break;
142         }
143
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
151         local_irq_restore(flags);
152         printk(KERN_DEBUG "Back to C!\n");
153
154         /* restore processor state */
155         if (acpi_state == ACPI_STATE_S3)
156                 acpi_restore_state_mem();
157
158         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
159 }
160
161 /**
162  *      acpi_pm_finish - Finish up suspend sequence.
163  *      @pm_state: ignored
164  *
165  *      This is called after we wake back up (or if entering the sleep state
166  *      failed). 
167  */
168
169 static int acpi_pm_finish(suspend_state_t pm_state)
170 {
171         u32 acpi_state = acpi_target_sleep_state;
172
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
179         acpi_target_sleep_state = ACPI_STATE_S0;
180
181 #ifdef CONFIG_X86
182         if (init_8259A_after_S1) {
183                 printk("Broken toshiba laptop -> kicking interrupts\n");
184                 init_8259A(0);
185         }
186 #endif
187         return 0;
188 }
189
190 static int acpi_pm_state_valid(suspend_state_t pm_state)
191 {
192         u32 acpi_state;
193
194         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         }
204 }
205
206 static struct pm_ops acpi_pm_ops = {
207         .valid = acpi_pm_state_valid,
208         .set_target = acpi_pm_set_target,
209         .prepare = acpi_pm_prepare,
210         .enter = acpi_pm_enter,
211         .finish = acpi_pm_finish,
212 };
213
214 /*
215  * Toshiba fails to preserve interrupts over S1, reinitialization
216  * of 8259 is needed after S1 resume.
217  */
218 static int __init init_ints_after_s1(struct dmi_system_id *d)
219 {
220         printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
221         init_8259A_after_S1 = 1;
222         return 0;
223 }
224
225 static 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
235 #ifdef CONFIG_HIBERNATION
236 static int acpi_hibernation_prepare(void)
237 {
238         return acpi_sleep_prepare(ACPI_STATE_S4);
239 }
240
241 static 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
257 static void acpi_hibernation_finish(void)
258 {
259         /*
260          * If ACPI is not enabled by the BIOS and the boot kernel, we need to
261          * enable it here.
262          */
263         acpi_enable();
264         acpi_leave_sleep_state(ACPI_STATE_S4);
265         acpi_disable_wakeup_device(ACPI_STATE_S4);
266
267         /* reset firmware waking vector */
268         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
269 }
270
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
285 static struct hibernation_ops acpi_hibernation_ops = {
286         .prepare = acpi_hibernation_prepare,
287         .enter = acpi_hibernation_enter,
288         .finish = acpi_hibernation_finish,
289         .pre_restore = acpi_hibernation_pre_restore,
290         .restore_cleanup = acpi_hibernation_restore_cleanup,
291 };
292 #endif                          /* CONFIG_HIBERNATION */
293
294 int acpi_suspend(u32 acpi_state)
295 {
296         suspend_state_t states[] = {
297                 [1] = PM_SUSPEND_STANDBY,
298                 [3] = PM_SUSPEND_MEM,
299                 [5] = PM_SUSPEND_MAX
300         };
301
302         if (acpi_state < 6 && states[acpi_state])
303                 return pm_suspend(states[acpi_state]);
304         if (acpi_state == 4)
305                 return hibernate();
306         return -EINVAL;
307 }
308
309 #ifdef CONFIG_PM_SLEEP
310 /**
311  *      acpi_pm_device_sleep_state - return preferred power state of ACPI device
312  *              in the system sleep state given by %acpi_target_sleep_state
313  *      @dev: device to examine
314  *      @wake: if set, the device should be able to wake up the system
315  *      @d_min_p: used to store the upper limit of allowed states range
316  *      Return value: preferred power state of the device on success, -ENODEV on
317  *              failure (ie. if there's no 'struct acpi_device' for @dev)
318  *
319  *      Find the lowest power (highest number) ACPI device power state that
320  *      device @dev can be in while the system is in the sleep state represented
321  *      by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
322  *      able to wake up the system from this sleep state.  If @d_min_p is set,
323  *      the highest power (lowest number) device power state of @dev allowed
324  *      in this system sleep state is stored at the location pointed to by it.
325  *
326  *      The caller must ensure that @dev is valid before using this function.
327  *      The caller is also responsible for figuring out if the device is
328  *      supposed to be able to wake up the system and passing this information
329  *      via @wake.
330  */
331
332 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
333 {
334         acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
335         struct acpi_device *adev;
336         char acpi_method[] = "_SxD";
337         unsigned long d_min, d_max;
338
339         if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
340                 printk(KERN_DEBUG "ACPI handle has no context!\n");
341                 return -ENODEV;
342         }
343
344         acpi_method[2] = '0' + acpi_target_sleep_state;
345         /*
346          * If the sleep state is S0, we will return D3, but if the device has
347          * _S0W, we will use the value from _S0W
348          */
349         d_min = ACPI_STATE_D0;
350         d_max = ACPI_STATE_D3;
351
352         /*
353          * If present, _SxD methods return the minimum D-state (highest power
354          * state) we can use for the corresponding S-states.  Otherwise, the
355          * minimum D-state is D0 (ACPI 3.x).
356          *
357          * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
358          * provided -- that's our fault recovery, we ignore retval.
359          */
360         if (acpi_target_sleep_state > ACPI_STATE_S0)
361                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
362
363         /*
364          * If _PRW says we can wake up the system from the target sleep state,
365          * the D-state returned by _SxD is sufficient for that (we assume a
366          * wakeup-aware driver if wake is set).  Still, if _SxW exists
367          * (ACPI 3.x), it should return the maximum (lowest power) D-state that
368          * can wake the system.  _S0W may be valid, too.
369          */
370         if (acpi_target_sleep_state == ACPI_STATE_S0 ||
371             (wake && adev->wakeup.state.enabled &&
372              adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
373                 acpi_method[3] = 'W';
374                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
375                 /* Sanity check */
376                 if (d_max < d_min)
377                         d_min = d_max;
378         }
379
380         if (d_min_p)
381                 *d_min_p = d_min;
382         return d_max;
383 }
384 #endif
385
386 static void acpi_power_off_prepare(void)
387 {
388         /* Prepare to power off the system */
389         acpi_sleep_prepare(ACPI_STATE_S5);
390 }
391
392 static void acpi_power_off(void)
393 {
394         /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
395         printk("%s called\n", __FUNCTION__);
396         local_irq_disable();
397         acpi_enter_sleep_state(ACPI_STATE_S5);
398 }
399
400 int __init acpi_sleep_init(void)
401 {
402         acpi_status status;
403         u8 type_a, type_b;
404 #ifdef CONFIG_SUSPEND
405         int i = 0;
406
407         dmi_check_system(acpisleep_dmi_table);
408 #endif
409
410         if (acpi_disabled)
411                 return 0;
412
413         sleep_states[ACPI_STATE_S0] = 1;
414         printk(KERN_INFO PREFIX "(supports S0");
415
416 #ifdef CONFIG_SUSPEND
417         for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
418                 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
419                 if (ACPI_SUCCESS(status)) {
420                         sleep_states[i] = 1;
421                         printk(" S%d", i);
422                 }
423         }
424
425         pm_set_ops(&acpi_pm_ops);
426 #endif
427
428 #ifdef CONFIG_HIBERNATION
429         status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
430         if (ACPI_SUCCESS(status)) {
431                 hibernation_set_ops(&acpi_hibernation_ops);
432                 sleep_states[ACPI_STATE_S4] = 1;
433                 printk(" S4");
434         }
435 #endif
436         status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
437         if (ACPI_SUCCESS(status)) {
438                 sleep_states[ACPI_STATE_S5] = 1;
439                 printk(" S5");
440                 pm_power_off_prepare = acpi_power_off_prepare;
441                 pm_power_off = acpi_power_off;
442         }
443         printk(")\n");
444         return 0;
445 }