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