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