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