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