]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/acpi/sleep/main.c
ACPI PM: Remove obsolete Toshiba workaround
[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 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 /**
66  *      acpi_suspend_begin - Set the target system sleep state to the state
67  *              associated with given @pm_state, if supported.
68  */
69
70 static int acpi_suspend_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_suspend_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_suspend_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                 return error;
99         }
100
101         return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
102 }
103
104 /**
105  *      acpi_suspend_enter - Actually enter a sleep state.
106  *      @pm_state: ignored
107  *
108  *      Flush caches and go to sleep. For STR we have to call arch-specific
109  *      assembly, which in turn call acpi_enter_sleep_state().
110  *      It's unfortunate, but it works. Please fix if you're feeling frisky.
111  */
112
113 static int acpi_suspend_enter(suspend_state_t pm_state)
114 {
115         acpi_status status = AE_OK;
116         unsigned long flags = 0;
117         u32 acpi_state = acpi_target_sleep_state;
118
119         ACPI_FLUSH_CPU_CACHE();
120
121         /* Do arch specific saving of state. */
122         if (acpi_state == ACPI_STATE_S3) {
123                 int error = acpi_save_state_mem();
124
125                 if (error)
126                         return error;
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 responsibility
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_suspend_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_suspend_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
189 /**
190  *      acpi_suspend_end - Finish up suspend sequence.
191  */
192
193 static void acpi_suspend_end(void)
194 {
195         /*
196          * This is necessary in case acpi_suspend_finish() is not called during a
197          * failing transition to a sleep state.
198          */
199         acpi_target_sleep_state = ACPI_STATE_S0;
200 }
201
202 static int acpi_suspend_state_valid(suspend_state_t pm_state)
203 {
204         u32 acpi_state;
205
206         switch (pm_state) {
207         case PM_SUSPEND_ON:
208         case PM_SUSPEND_STANDBY:
209         case PM_SUSPEND_MEM:
210                 acpi_state = acpi_suspend_states[pm_state];
211
212                 return sleep_states[acpi_state];
213         default:
214                 return 0;
215         }
216 }
217
218 static struct platform_suspend_ops acpi_suspend_ops = {
219         .valid = acpi_suspend_state_valid,
220         .begin = acpi_suspend_begin,
221         .prepare = acpi_suspend_prepare,
222         .enter = acpi_suspend_enter,
223         .finish = acpi_suspend_finish,
224         .end = acpi_suspend_end,
225 };
226 #endif /* CONFIG_SUSPEND */
227
228 #ifdef CONFIG_HIBERNATION
229 static int acpi_hibernation_begin(void)
230 {
231         acpi_target_sleep_state = ACPI_STATE_S4;
232
233         return 0;
234 }
235
236 static int acpi_hibernation_prepare(void)
237 {
238         int error = acpi_sleep_prepare(ACPI_STATE_S4);
239
240         if (error) {
241                 acpi_target_sleep_state = ACPI_STATE_S0;
242                 return error;
243         }
244
245         return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
246 }
247
248 static int acpi_hibernation_enter(void)
249 {
250         acpi_status status = AE_OK;
251         unsigned long flags = 0;
252
253         ACPI_FLUSH_CPU_CACHE();
254
255         local_irq_save(flags);
256         acpi_enable_wakeup_device(ACPI_STATE_S4);
257         /* This shouldn't return.  If it returns, we have a problem */
258         status = acpi_enter_sleep_state(ACPI_STATE_S4);
259         /* Reprogram control registers and execute _BFS */
260         acpi_leave_sleep_state_prep(ACPI_STATE_S4);
261         local_irq_restore(flags);
262
263         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
264 }
265
266 static void acpi_hibernation_leave(void)
267 {
268         /*
269          * If ACPI is not enabled by the BIOS and the boot kernel, we need to
270          * enable it here.
271          */
272         acpi_enable();
273         /* Reprogram control registers and execute _BFS */
274         acpi_leave_sleep_state_prep(ACPI_STATE_S4);
275 }
276
277 static void acpi_hibernation_finish(void)
278 {
279         acpi_disable_wakeup_device(ACPI_STATE_S4);
280         acpi_leave_sleep_state(ACPI_STATE_S4);
281
282         /* reset firmware waking vector */
283         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
284
285         acpi_target_sleep_state = ACPI_STATE_S0;
286 }
287
288 static void acpi_hibernation_end(void)
289 {
290         /*
291          * This is necessary in case acpi_hibernation_finish() is not called
292          * during a failing transition to the sleep state.
293          */
294         acpi_target_sleep_state = ACPI_STATE_S0;
295 }
296
297 static int acpi_hibernation_pre_restore(void)
298 {
299         acpi_status status;
300
301         status = acpi_hw_disable_all_gpes();
302
303         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
304 }
305
306 static void acpi_hibernation_restore_cleanup(void)
307 {
308         acpi_hw_enable_all_runtime_gpes();
309 }
310
311 static struct platform_hibernation_ops acpi_hibernation_ops = {
312         .begin = acpi_hibernation_begin,
313         .end = acpi_hibernation_end,
314         .pre_snapshot = acpi_hibernation_prepare,
315         .finish = acpi_hibernation_finish,
316         .prepare = acpi_hibernation_prepare,
317         .enter = acpi_hibernation_enter,
318         .leave = acpi_hibernation_leave,
319         .pre_restore = acpi_hibernation_pre_restore,
320         .restore_cleanup = acpi_hibernation_restore_cleanup,
321 };
322 #endif                          /* CONFIG_HIBERNATION */
323
324 int acpi_suspend(u32 acpi_state)
325 {
326         suspend_state_t states[] = {
327                 [1] = PM_SUSPEND_STANDBY,
328                 [3] = PM_SUSPEND_MEM,
329                 [5] = PM_SUSPEND_MAX
330         };
331
332         if (acpi_state < 6 && states[acpi_state])
333                 return pm_suspend(states[acpi_state]);
334         if (acpi_state == 4)
335                 return hibernate();
336         return -EINVAL;
337 }
338
339 #ifdef CONFIG_PM_SLEEP
340 /**
341  *      acpi_pm_device_sleep_state - return preferred power state of ACPI device
342  *              in the system sleep state given by %acpi_target_sleep_state
343  *      @dev: device to examine; its driver model wakeup flags control
344  *              whether it should be able to wake up the system
345  *      @d_min_p: used to store the upper limit of allowed states range
346  *      Return value: preferred power state of the device on success, -ENODEV on
347  *              failure (ie. if there's no 'struct acpi_device' for @dev)
348  *
349  *      Find the lowest power (highest number) ACPI device power state that
350  *      device @dev can be in while the system is in the sleep state represented
351  *      by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
352  *      able to wake up the system from this sleep state.  If @d_min_p is set,
353  *      the highest power (lowest number) device power state of @dev allowed
354  *      in this system sleep state is stored at the location pointed to by it.
355  *
356  *      The caller must ensure that @dev is valid before using this function.
357  *      The caller is also responsible for figuring out if the device is
358  *      supposed to be able to wake up the system and passing this information
359  *      via @wake.
360  */
361
362 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
363 {
364         acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
365         struct acpi_device *adev;
366         char acpi_method[] = "_SxD";
367         unsigned long d_min, d_max;
368
369         if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
370                 printk(KERN_DEBUG "ACPI handle has no context!\n");
371                 return -ENODEV;
372         }
373
374         acpi_method[2] = '0' + acpi_target_sleep_state;
375         /*
376          * If the sleep state is S0, we will return D3, but if the device has
377          * _S0W, we will use the value from _S0W
378          */
379         d_min = ACPI_STATE_D0;
380         d_max = ACPI_STATE_D3;
381
382         /*
383          * If present, _SxD methods return the minimum D-state (highest power
384          * state) we can use for the corresponding S-states.  Otherwise, the
385          * minimum D-state is D0 (ACPI 3.x).
386          *
387          * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
388          * provided -- that's our fault recovery, we ignore retval.
389          */
390         if (acpi_target_sleep_state > ACPI_STATE_S0)
391                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
392
393         /*
394          * If _PRW says we can wake up the system from the target sleep state,
395          * the D-state returned by _SxD is sufficient for that (we assume a
396          * wakeup-aware driver if wake is set).  Still, if _SxW exists
397          * (ACPI 3.x), it should return the maximum (lowest power) D-state that
398          * can wake the system.  _S0W may be valid, too.
399          */
400         if (acpi_target_sleep_state == ACPI_STATE_S0 ||
401             (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
402              adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
403                 acpi_status status;
404
405                 acpi_method[3] = 'W';
406                 status = acpi_evaluate_integer(handle, acpi_method, NULL,
407                                                 &d_max);
408                 if (ACPI_FAILURE(status)) {
409                         d_max = d_min;
410                 } else if (d_max < d_min) {
411                         /* Warn the user of the broken DSDT */
412                         printk(KERN_WARNING "ACPI: Wrong value from %s\n",
413                                 acpi_method);
414                         /* Sanitize it */
415                         d_min = d_max;
416                 }
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         acpi_hw_disable_all_gpes();
430 }
431
432 static void acpi_power_off(void)
433 {
434         /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
435         printk("%s called\n", __func__);
436         local_irq_disable();
437         acpi_enable_wakeup_device(ACPI_STATE_S5);
438         acpi_enter_sleep_state(ACPI_STATE_S5);
439 }
440
441 int __init acpi_sleep_init(void)
442 {
443         acpi_status status;
444         u8 type_a, type_b;
445 #ifdef CONFIG_SUSPEND
446         int i = 0;
447 #endif
448
449         if (acpi_disabled)
450                 return 0;
451
452         sleep_states[ACPI_STATE_S0] = 1;
453         printk(KERN_INFO PREFIX "(supports S0");
454
455 #ifdef CONFIG_SUSPEND
456         for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
457                 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
458                 if (ACPI_SUCCESS(status)) {
459                         sleep_states[i] = 1;
460                         printk(" S%d", i);
461                 }
462         }
463
464         suspend_set_ops(&acpi_suspend_ops);
465 #endif
466
467 #ifdef CONFIG_HIBERNATION
468         status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
469         if (ACPI_SUCCESS(status)) {
470                 hibernation_set_ops(&acpi_hibernation_ops);
471                 sleep_states[ACPI_STATE_S4] = 1;
472                 printk(" S4");
473         }
474 #endif
475         status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
476         if (ACPI_SUCCESS(status)) {
477                 sleep_states[ACPI_STATE_S5] = 1;
478                 printk(" S5");
479                 pm_power_off_prepare = acpi_power_off_prepare;
480                 pm_power_off = acpi_power_off;
481         }
482         printk(")\n");
483         return 0;
484 }