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