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