]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/acpi/sleep/main.c
ACPI: suspend: consolidate handling of Sx states addendum
[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 u32 acpi_target_sleep_state = ACPI_STATE_S0;
28
29 #ifdef CONFIG_SUSPEND
30 static struct pm_ops acpi_pm_ops;
31
32 extern void do_suspend_lowlevel(void);
33
34 static u32 acpi_suspend_states[] = {
35         [PM_SUSPEND_ON] = ACPI_STATE_S0,
36         [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
37         [PM_SUSPEND_MEM] = ACPI_STATE_S3,
38         [PM_SUSPEND_MAX] = ACPI_STATE_S5
39 };
40
41 static int init_8259A_after_S1;
42
43 /**
44  *      acpi_pm_set_target - Set the target system sleep state to the state
45  *              associated with given @pm_state, if supported.
46  */
47
48 static int acpi_pm_set_target(suspend_state_t pm_state)
49 {
50         u32 acpi_state = acpi_suspend_states[pm_state];
51         int error = 0;
52
53         if (sleep_states[acpi_state]) {
54                 acpi_target_sleep_state = acpi_state;
55         } else {
56                 printk(KERN_ERR "ACPI does not support this state: %d\n",
57                         pm_state);
58                 error = -ENOSYS;
59         }
60         return error;
61 }
62
63 int acpi_sleep_prepare(u32 acpi_state)
64 {
65 #ifdef CONFIG_ACPI_SLEEP
66         /* do we have a wakeup address for S2 and S3? */
67         if (acpi_state == ACPI_STATE_S3) {
68                 if (!acpi_wakeup_address) {
69                         return -EFAULT;
70                 }
71                 acpi_set_firmware_waking_vector((acpi_physical_address)
72                                                 virt_to_phys((void *)
73                                                              acpi_wakeup_address));
74
75         }
76         ACPI_FLUSH_CPU_CACHE();
77         acpi_enable_wakeup_device_prep(acpi_state);
78 #endif
79         acpi_gpe_sleep_prepare(acpi_state);
80         acpi_enter_sleep_state_prep(acpi_state);
81         return 0;
82 }
83
84 /**
85  *      acpi_pm_prepare - Do preliminary suspend work.
86  *      @pm_state: ignored
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(suspend_state_t pm_state)
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  *      @pm_state: ignored
162  *
163  *      This is called after we wake back up (or if entering the sleep state
164  *      failed). 
165  */
166
167 static int acpi_pm_finish(suspend_state_t pm_state)
168 {
169         u32 acpi_state = acpi_target_sleep_state;
170
171         acpi_leave_sleep_state(acpi_state);
172         acpi_disable_wakeup_device(acpi_state);
173
174         /* reset firmware waking vector */
175         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
176
177         acpi_target_sleep_state = ACPI_STATE_S0;
178
179 #ifdef CONFIG_X86
180         if (init_8259A_after_S1) {
181                 printk("Broken toshiba laptop -> kicking interrupts\n");
182                 init_8259A(0);
183         }
184 #endif
185         return 0;
186 }
187
188 static int acpi_pm_state_valid(suspend_state_t pm_state)
189 {
190         u32 acpi_state;
191
192         switch (pm_state) {
193         case PM_SUSPEND_ON:
194         case PM_SUSPEND_STANDBY:
195         case PM_SUSPEND_MEM:
196                 acpi_state = acpi_suspend_states[pm_state];
197
198                 return sleep_states[acpi_state];
199         default:
200                 return 0;
201         }
202 }
203
204 static struct pm_ops acpi_pm_ops = {
205         .valid = acpi_pm_state_valid,
206         .set_target = acpi_pm_set_target,
207         .prepare = acpi_pm_prepare,
208         .enter = acpi_pm_enter,
209         .finish = acpi_pm_finish,
210 };
211
212 /*
213  * Toshiba fails to preserve interrupts over S1, reinitialization
214  * of 8259 is needed after S1 resume.
215  */
216 static int __init init_ints_after_s1(struct dmi_system_id *d)
217 {
218         printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
219         init_8259A_after_S1 = 1;
220         return 0;
221 }
222
223 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
224         {
225          .callback = init_ints_after_s1,
226          .ident = "Toshiba Satellite 4030cdt",
227          .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
228          },
229         {},
230 };
231 #endif /* CONFIG_SUSPEND */
232
233 #ifdef CONFIG_HIBERNATION
234 static int acpi_hibernation_prepare(void)
235 {
236         return acpi_sleep_prepare(ACPI_STATE_S4);
237 }
238
239 static int acpi_hibernation_enter(void)
240 {
241         acpi_status status = AE_OK;
242         unsigned long flags = 0;
243
244         ACPI_FLUSH_CPU_CACHE();
245
246         local_irq_save(flags);
247         acpi_enable_wakeup_device(ACPI_STATE_S4);
248         /* This shouldn't return.  If it returns, we have a problem */
249         status = acpi_enter_sleep_state(ACPI_STATE_S4);
250         local_irq_restore(flags);
251
252         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
253 }
254
255 static void acpi_hibernation_finish(void)
256 {
257         acpi_leave_sleep_state(ACPI_STATE_S4);
258         acpi_disable_wakeup_device(ACPI_STATE_S4);
259
260         /* reset firmware waking vector */
261         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
262 }
263
264 static int acpi_hibernation_pre_restore(void)
265 {
266         acpi_status status;
267
268         status = acpi_hw_disable_all_gpes();
269
270         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
271 }
272
273 static void acpi_hibernation_restore_cleanup(void)
274 {
275         acpi_hw_enable_all_runtime_gpes();
276 }
277
278 static struct hibernation_ops acpi_hibernation_ops = {
279         .prepare = acpi_hibernation_prepare,
280         .enter = acpi_hibernation_enter,
281         .finish = acpi_hibernation_finish,
282         .pre_restore = acpi_hibernation_pre_restore,
283         .restore_cleanup = acpi_hibernation_restore_cleanup,
284 };
285 #endif                          /* CONFIG_HIBERNATION */
286
287 int acpi_suspend(u32 acpi_state)
288 {
289         suspend_state_t states[] = {
290                 [1] = PM_SUSPEND_STANDBY,
291                 [3] = PM_SUSPEND_MEM,
292                 [5] = PM_SUSPEND_MAX
293         };
294
295         if (acpi_state < 6 && states[acpi_state])
296                 return pm_suspend(states[acpi_state]);
297         if (acpi_state == 4)
298                 return hibernate();
299         return -EINVAL;
300 }
301
302 /**
303  *      acpi_pm_device_sleep_state - return preferred power state of ACPI device
304  *              in the system sleep state given by %acpi_target_sleep_state
305  *      @dev: device to examine
306  *      @wake: if set, the device should be able to wake up the system
307  *      @d_min_p: used to store the upper limit of allowed states range
308  *      Return value: preferred power state of the device on success, -ENODEV on
309  *              failure (ie. if there's no 'struct acpi_device' for @dev)
310  *
311  *      Find the lowest power (highest number) ACPI device power state that
312  *      device @dev can be in while the system is in the sleep state represented
313  *      by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
314  *      able to wake up the system from this sleep state.  If @d_min_p is set,
315  *      the highest power (lowest number) device power state of @dev allowed
316  *      in this system sleep state is stored at the location pointed to by it.
317  *
318  *      The caller must ensure that @dev is valid before using this function.
319  *      The caller is also responsible for figuring out if the device is
320  *      supposed to be able to wake up the system and passing this information
321  *      via @wake.
322  */
323
324 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
325 {
326         acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
327         struct acpi_device *adev;
328         char acpi_method[] = "_SxD";
329         unsigned long d_min, d_max;
330
331         if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
332                 printk(KERN_DEBUG "ACPI handle has no context!\n");
333                 return -ENODEV;
334         }
335
336         acpi_method[2] = '0' + acpi_target_sleep_state;
337         /*
338          * If the sleep state is S0, we will return D3, but if the device has
339          * _S0W, we will use the value from _S0W
340          */
341         d_min = ACPI_STATE_D0;
342         d_max = ACPI_STATE_D3;
343
344         /*
345          * If present, _SxD methods return the minimum D-state (highest power
346          * state) we can use for the corresponding S-states.  Otherwise, the
347          * minimum D-state is D0 (ACPI 3.x).
348          *
349          * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
350          * provided -- that's our fault recovery, we ignore retval.
351          */
352         if (acpi_target_sleep_state > ACPI_STATE_S0)
353                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
354
355         /*
356          * If _PRW says we can wake up the system from the target sleep state,
357          * the D-state returned by _SxD is sufficient for that (we assume a
358          * wakeup-aware driver if wake is set).  Still, if _SxW exists
359          * (ACPI 3.x), it should return the maximum (lowest power) D-state that
360          * can wake the system.  _S0W may be valid, too.
361          */
362         if (acpi_target_sleep_state == ACPI_STATE_S0 ||
363             (wake && adev->wakeup.state.enabled &&
364              adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
365                 acpi_method[3] = 'W';
366                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
367                 /* Sanity check */
368                 if (d_max < d_min)
369                         d_min = d_max;
370         }
371
372         if (d_min_p)
373                 *d_min_p = d_min;
374         return d_max;
375 }
376
377 static void acpi_power_off_prepare(void)
378 {
379         /* Prepare to power off the system */
380         acpi_sleep_prepare(ACPI_STATE_S5);
381 }
382
383 static void acpi_power_off(void)
384 {
385         /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
386         printk("%s called\n", __FUNCTION__);
387         local_irq_disable();
388         acpi_enter_sleep_state(ACPI_STATE_S5);
389 }
390
391 int __init acpi_sleep_init(void)
392 {
393         acpi_status status;
394         u8 type_a, type_b;
395 #ifdef CONFIG_SUSPEND
396         int i = 0;
397
398         dmi_check_system(acpisleep_dmi_table);
399 #endif
400
401         if (acpi_disabled)
402                 return 0;
403
404         sleep_states[ACPI_STATE_S0] = 1;
405         printk(KERN_INFO PREFIX "(supports S0");
406
407 #ifdef CONFIG_SUSPEND
408         for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
409                 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
410                 if (ACPI_SUCCESS(status)) {
411                         sleep_states[i] = 1;
412                         printk(" S%d", i);
413                 }
414         }
415
416         pm_set_ops(&acpi_pm_ops);
417 #endif
418
419 #ifdef CONFIG_HIBERNATION
420         status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
421         if (ACPI_SUCCESS(status)) {
422                 hibernation_set_ops(&acpi_hibernation_ops);
423                 sleep_states[ACPI_STATE_S4] = 1;
424                 printk(" S4");
425         }
426 #endif
427         status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
428         if (ACPI_SUCCESS(status)) {
429                 sleep_states[ACPI_STATE_S5] = 1;
430                 printk(" S5");
431                 pm_power_off_prepare = acpi_power_off_prepare;
432                 pm_power_off = acpi_power_off;
433         }
434         printk(")\n");
435         return 0;
436 }