]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/acpi/sleep/main.c
PCI ACPI: Rework PCI handling of wake-up
[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 static int acpi_sleep_prepare(u32 acpi_state)
28 {
29 #ifdef CONFIG_ACPI_SLEEP
30         /* do we have a wakeup address for S2 and S3? */
31         if (acpi_state == ACPI_STATE_S3) {
32                 if (!acpi_wakeup_address) {
33                         return -EFAULT;
34                 }
35                 acpi_set_firmware_waking_vector((acpi_physical_address)
36                                                 virt_to_phys((void *)
37                                                              acpi_wakeup_address));
38
39         }
40         ACPI_FLUSH_CPU_CACHE();
41         acpi_enable_wakeup_device_prep(acpi_state);
42 #endif
43         printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
44                 acpi_state);
45         acpi_enter_sleep_state_prep(acpi_state);
46         return 0;
47 }
48
49 #ifdef CONFIG_PM_SLEEP
50 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
51
52 /*
53  * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
54  * user to request that behavior by using the 'acpi_old_suspend_ordering'
55  * kernel command line option that causes the following variable to be set.
56  */
57 static bool old_suspend_ordering;
58
59 void __init acpi_old_suspend_ordering(void)
60 {
61         old_suspend_ordering = true;
62 }
63
64 /**
65  *      acpi_pm_disable_gpes - Disable the GPEs.
66  */
67 static int acpi_pm_disable_gpes(void)
68 {
69         acpi_hw_disable_all_gpes();
70         return 0;
71 }
72
73 /**
74  *      __acpi_pm_prepare - Prepare the platform to enter the target state.
75  *
76  *      If necessary, set the firmware waking vector and do arch-specific
77  *      nastiness to get the wakeup code to the waking vector.
78  */
79 static int __acpi_pm_prepare(void)
80 {
81         int error = acpi_sleep_prepare(acpi_target_sleep_state);
82
83         if (error)
84                 acpi_target_sleep_state = ACPI_STATE_S0;
85         return error;
86 }
87
88 /**
89  *      acpi_pm_prepare - Prepare the platform to enter the target sleep
90  *              state and disable the GPEs.
91  */
92 static int acpi_pm_prepare(void)
93 {
94         int error = __acpi_pm_prepare();
95
96         if (!error)
97                 acpi_hw_disable_all_gpes();
98         return error;
99 }
100
101 /**
102  *      acpi_pm_finish - Instruct the platform to leave a sleep state.
103  *
104  *      This is called after we wake back up (or if entering the sleep state
105  *      failed).
106  */
107 static void acpi_pm_finish(void)
108 {
109         u32 acpi_state = acpi_target_sleep_state;
110
111         if (acpi_state == ACPI_STATE_S0)
112                 return;
113
114         printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
115                 acpi_state);
116         acpi_disable_wakeup_device(acpi_state);
117         acpi_leave_sleep_state(acpi_state);
118
119         /* reset firmware waking vector */
120         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
121
122         acpi_target_sleep_state = ACPI_STATE_S0;
123 }
124
125 /**
126  *      acpi_pm_end - Finish up suspend sequence.
127  */
128 static void acpi_pm_end(void)
129 {
130         /*
131          * This is necessary in case acpi_pm_finish() is not called during a
132          * failing transition to a sleep state.
133          */
134         acpi_target_sleep_state = ACPI_STATE_S0;
135 }
136 #endif /* CONFIG_PM_SLEEP */
137
138 #ifdef CONFIG_SUSPEND
139 extern void do_suspend_lowlevel(void);
140
141 static u32 acpi_suspend_states[] = {
142         [PM_SUSPEND_ON] = ACPI_STATE_S0,
143         [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
144         [PM_SUSPEND_MEM] = ACPI_STATE_S3,
145         [PM_SUSPEND_MAX] = ACPI_STATE_S5
146 };
147
148 /**
149  *      acpi_suspend_begin - Set the target system sleep state to the state
150  *              associated with given @pm_state, if supported.
151  */
152 static int acpi_suspend_begin(suspend_state_t pm_state)
153 {
154         u32 acpi_state = acpi_suspend_states[pm_state];
155         int error = 0;
156
157         if (sleep_states[acpi_state]) {
158                 acpi_target_sleep_state = acpi_state;
159         } else {
160                 printk(KERN_ERR "ACPI does not support this state: %d\n",
161                         pm_state);
162                 error = -ENOSYS;
163         }
164         return error;
165 }
166
167 /**
168  *      acpi_suspend_enter - Actually enter a sleep state.
169  *      @pm_state: ignored
170  *
171  *      Flush caches and go to sleep. For STR we have to call arch-specific
172  *      assembly, which in turn call acpi_enter_sleep_state().
173  *      It's unfortunate, but it works. Please fix if you're feeling frisky.
174  */
175 static int acpi_suspend_enter(suspend_state_t pm_state)
176 {
177         acpi_status status = AE_OK;
178         unsigned long flags = 0;
179         u32 acpi_state = acpi_target_sleep_state;
180
181         ACPI_FLUSH_CPU_CACHE();
182
183         /* Do arch specific saving of state. */
184         if (acpi_state == ACPI_STATE_S3) {
185                 int error = acpi_save_state_mem();
186
187                 if (error)
188                         return error;
189         }
190
191         local_irq_save(flags);
192         acpi_enable_wakeup_device(acpi_state);
193         switch (acpi_state) {
194         case ACPI_STATE_S1:
195                 barrier();
196                 status = acpi_enter_sleep_state(acpi_state);
197                 break;
198
199         case ACPI_STATE_S3:
200                 do_suspend_lowlevel();
201                 break;
202         }
203
204         /* Reprogram control registers and execute _BFS */
205         acpi_leave_sleep_state_prep(acpi_state);
206
207         /* ACPI 3.0 specs (P62) says that it's the responsibility
208          * of the OSPM to clear the status bit [ implying that the
209          * POWER_BUTTON event should not reach userspace ]
210          */
211         if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
212                 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
213
214         /*
215          * Disable and clear GPE status before interrupt is enabled. Some GPEs
216          * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
217          * acpi_leave_sleep_state will reenable specific GPEs later
218          */
219         acpi_hw_disable_all_gpes();
220
221         local_irq_restore(flags);
222         printk(KERN_DEBUG "Back to C!\n");
223
224         /* restore processor state */
225         if (acpi_state == ACPI_STATE_S3)
226                 acpi_restore_state_mem();
227
228         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
229 }
230
231 static int acpi_suspend_state_valid(suspend_state_t pm_state)
232 {
233         u32 acpi_state;
234
235         switch (pm_state) {
236         case PM_SUSPEND_ON:
237         case PM_SUSPEND_STANDBY:
238         case PM_SUSPEND_MEM:
239                 acpi_state = acpi_suspend_states[pm_state];
240
241                 return sleep_states[acpi_state];
242         default:
243                 return 0;
244         }
245 }
246
247 static struct platform_suspend_ops acpi_suspend_ops = {
248         .valid = acpi_suspend_state_valid,
249         .begin = acpi_suspend_begin,
250         .prepare = acpi_pm_prepare,
251         .enter = acpi_suspend_enter,
252         .finish = acpi_pm_finish,
253         .end = acpi_pm_end,
254 };
255
256 /**
257  *      acpi_suspend_begin_old - Set the target system sleep state to the
258  *              state associated with given @pm_state, if supported, and
259  *              execute the _PTS control method.  This function is used if the
260  *              pre-ACPI 2.0 suspend ordering has been requested.
261  */
262 static int acpi_suspend_begin_old(suspend_state_t pm_state)
263 {
264         int error = acpi_suspend_begin(pm_state);
265
266         if (!error)
267                 error = __acpi_pm_prepare();
268         return error;
269 }
270
271 /*
272  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
273  * been requested.
274  */
275 static struct platform_suspend_ops acpi_suspend_ops_old = {
276         .valid = acpi_suspend_state_valid,
277         .begin = acpi_suspend_begin_old,
278         .prepare = acpi_pm_disable_gpes,
279         .enter = acpi_suspend_enter,
280         .finish = acpi_pm_finish,
281         .end = acpi_pm_end,
282         .recover = acpi_pm_finish,
283 };
284 #endif /* CONFIG_SUSPEND */
285
286 #ifdef CONFIG_HIBERNATION
287 static int acpi_hibernation_begin(void)
288 {
289         acpi_target_sleep_state = ACPI_STATE_S4;
290         return 0;
291 }
292
293 static int acpi_hibernation_enter(void)
294 {
295         acpi_status status = AE_OK;
296         unsigned long flags = 0;
297
298         ACPI_FLUSH_CPU_CACHE();
299
300         local_irq_save(flags);
301         acpi_enable_wakeup_device(ACPI_STATE_S4);
302         /* This shouldn't return.  If it returns, we have a problem */
303         status = acpi_enter_sleep_state(ACPI_STATE_S4);
304         /* Reprogram control registers and execute _BFS */
305         acpi_leave_sleep_state_prep(ACPI_STATE_S4);
306         local_irq_restore(flags);
307
308         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
309 }
310
311 static void acpi_hibernation_leave(void)
312 {
313         /*
314          * If ACPI is not enabled by the BIOS and the boot kernel, we need to
315          * enable it here.
316          */
317         acpi_enable();
318         /* Reprogram control registers and execute _BFS */
319         acpi_leave_sleep_state_prep(ACPI_STATE_S4);
320 }
321
322 static void acpi_pm_enable_gpes(void)
323 {
324         acpi_hw_enable_all_runtime_gpes();
325 }
326
327 static struct platform_hibernation_ops acpi_hibernation_ops = {
328         .begin = acpi_hibernation_begin,
329         .end = acpi_pm_end,
330         .pre_snapshot = acpi_pm_prepare,
331         .finish = acpi_pm_finish,
332         .prepare = acpi_pm_prepare,
333         .enter = acpi_hibernation_enter,
334         .leave = acpi_hibernation_leave,
335         .pre_restore = acpi_pm_disable_gpes,
336         .restore_cleanup = acpi_pm_enable_gpes,
337 };
338
339 /**
340  *      acpi_hibernation_begin_old - Set the target system sleep state to
341  *              ACPI_STATE_S4 and execute the _PTS control method.  This
342  *              function is used if the pre-ACPI 2.0 suspend ordering has been
343  *              requested.
344  */
345 static int acpi_hibernation_begin_old(void)
346 {
347         int error = acpi_sleep_prepare(ACPI_STATE_S4);
348
349         if (!error)
350                 acpi_target_sleep_state = ACPI_STATE_S4;
351         return error;
352 }
353
354 /*
355  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
356  * been requested.
357  */
358 static struct platform_hibernation_ops acpi_hibernation_ops_old = {
359         .begin = acpi_hibernation_begin_old,
360         .end = acpi_pm_end,
361         .pre_snapshot = acpi_pm_disable_gpes,
362         .finish = acpi_pm_finish,
363         .prepare = acpi_pm_disable_gpes,
364         .enter = acpi_hibernation_enter,
365         .leave = acpi_hibernation_leave,
366         .pre_restore = acpi_pm_disable_gpes,
367         .restore_cleanup = acpi_pm_enable_gpes,
368         .recover = acpi_pm_finish,
369 };
370 #endif /* CONFIG_HIBERNATION */
371
372 int acpi_suspend(u32 acpi_state)
373 {
374         suspend_state_t states[] = {
375                 [1] = PM_SUSPEND_STANDBY,
376                 [3] = PM_SUSPEND_MEM,
377                 [5] = PM_SUSPEND_MAX
378         };
379
380         if (acpi_state < 6 && states[acpi_state])
381                 return pm_suspend(states[acpi_state]);
382         if (acpi_state == 4)
383                 return hibernate();
384         return -EINVAL;
385 }
386
387 #ifdef CONFIG_PM_SLEEP
388 /**
389  *      acpi_pm_device_sleep_state - return preferred power state of ACPI device
390  *              in the system sleep state given by %acpi_target_sleep_state
391  *      @dev: device to examine; its driver model wakeup flags control
392  *              whether it should be able to wake up the system
393  *      @d_min_p: used to store the upper limit of allowed states range
394  *      Return value: preferred power state of the device on success, -ENODEV on
395  *              failure (ie. if there's no 'struct acpi_device' for @dev)
396  *
397  *      Find the lowest power (highest number) ACPI device power state that
398  *      device @dev can be in while the system is in the sleep state represented
399  *      by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
400  *      able to wake up the system from this sleep state.  If @d_min_p is set,
401  *      the highest power (lowest number) device power state of @dev allowed
402  *      in this system sleep state is stored at the location pointed to by it.
403  *
404  *      The caller must ensure that @dev is valid before using this function.
405  *      The caller is also responsible for figuring out if the device is
406  *      supposed to be able to wake up the system and passing this information
407  *      via @wake.
408  */
409
410 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
411 {
412         acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
413         struct acpi_device *adev;
414         char acpi_method[] = "_SxD";
415         unsigned long d_min, d_max;
416
417         if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
418                 printk(KERN_DEBUG "ACPI handle has no context!\n");
419                 return -ENODEV;
420         }
421
422         acpi_method[2] = '0' + acpi_target_sleep_state;
423         /*
424          * If the sleep state is S0, we will return D3, but if the device has
425          * _S0W, we will use the value from _S0W
426          */
427         d_min = ACPI_STATE_D0;
428         d_max = ACPI_STATE_D3;
429
430         /*
431          * If present, _SxD methods return the minimum D-state (highest power
432          * state) we can use for the corresponding S-states.  Otherwise, the
433          * minimum D-state is D0 (ACPI 3.x).
434          *
435          * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
436          * provided -- that's our fault recovery, we ignore retval.
437          */
438         if (acpi_target_sleep_state > ACPI_STATE_S0)
439                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
440
441         /*
442          * If _PRW says we can wake up the system from the target sleep state,
443          * the D-state returned by _SxD is sufficient for that (we assume a
444          * wakeup-aware driver if wake is set).  Still, if _SxW exists
445          * (ACPI 3.x), it should return the maximum (lowest power) D-state that
446          * can wake the system.  _S0W may be valid, too.
447          */
448         if (acpi_target_sleep_state == ACPI_STATE_S0 ||
449             (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
450              adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
451                 acpi_status status;
452
453                 acpi_method[3] = 'W';
454                 status = acpi_evaluate_integer(handle, acpi_method, NULL,
455                                                 &d_max);
456                 if (ACPI_FAILURE(status)) {
457                         d_max = d_min;
458                 } else if (d_max < d_min) {
459                         /* Warn the user of the broken DSDT */
460                         printk(KERN_WARNING "ACPI: Wrong value from %s\n",
461                                 acpi_method);
462                         /* Sanitize it */
463                         d_min = d_max;
464                 }
465         }
466
467         if (d_min_p)
468                 *d_min_p = d_min;
469         return d_max;
470 }
471
472 /**
473  *      acpi_pm_device_sleep_wake - enable or disable the system wake-up
474  *                                  capability of given device
475  *      @dev: device to handle
476  *      @enable: 'true' - enable, 'false' - disable the wake-up capability
477  */
478 int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
479 {
480         acpi_handle handle;
481         struct acpi_device *adev;
482
483         if (!device_may_wakeup(dev))
484                 return -EINVAL;
485
486         handle = DEVICE_ACPI_HANDLE(dev);
487         if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
488                 printk(KERN_DEBUG "ACPI handle has no context!\n");
489                 return -ENODEV;
490         }
491
492         return enable ?
493                 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
494                 acpi_disable_wakeup_device_power(adev);
495 }
496 #endif
497
498 static void acpi_power_off_prepare(void)
499 {
500         /* Prepare to power off the system */
501         acpi_sleep_prepare(ACPI_STATE_S5);
502         acpi_hw_disable_all_gpes();
503 }
504
505 static void acpi_power_off(void)
506 {
507         /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
508         printk("%s called\n", __func__);
509         local_irq_disable();
510         acpi_enable_wakeup_device(ACPI_STATE_S5);
511         acpi_enter_sleep_state(ACPI_STATE_S5);
512 }
513
514 int __init acpi_sleep_init(void)
515 {
516         acpi_status status;
517         u8 type_a, type_b;
518 #ifdef CONFIG_SUSPEND
519         int i = 0;
520 #endif
521
522         if (acpi_disabled)
523                 return 0;
524
525         sleep_states[ACPI_STATE_S0] = 1;
526         printk(KERN_INFO PREFIX "(supports S0");
527
528 #ifdef CONFIG_SUSPEND
529         for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
530                 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
531                 if (ACPI_SUCCESS(status)) {
532                         sleep_states[i] = 1;
533                         printk(" S%d", i);
534                 }
535         }
536
537         suspend_set_ops(old_suspend_ordering ?
538                 &acpi_suspend_ops_old : &acpi_suspend_ops);
539 #endif
540
541 #ifdef CONFIG_HIBERNATION
542         status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
543         if (ACPI_SUCCESS(status)) {
544                 hibernation_set_ops(old_suspend_ordering ?
545                         &acpi_hibernation_ops_old : &acpi_hibernation_ops);
546                 sleep_states[ACPI_STATE_S4] = 1;
547                 printk(" S4");
548         }
549 #endif
550         status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
551         if (ACPI_SUCCESS(status)) {
552                 sleep_states[ACPI_STATE_S5] = 1;
553                 printk(" S5");
554                 pm_power_off_prepare = acpi_power_off_prepare;
555                 pm_power_off = acpi_power_off;
556         }
557         printk(")\n");
558         return 0;
559 }