Pull d-states into release branch
[linux-2.6.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 #include <acpi/acpi_bus.h>
19 #include <acpi/acpi_drivers.h>
20 #include "sleep.h"
21
22 u8 sleep_states[ACPI_S_STATE_COUNT];
23
24 static struct pm_ops acpi_pm_ops;
25
26 extern void do_suspend_lowlevel(void);
27
28 static u32 acpi_suspend_states[] = {
29         [PM_SUSPEND_ON] = ACPI_STATE_S0,
30         [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
31         [PM_SUSPEND_MEM] = ACPI_STATE_S3,
32         [PM_SUSPEND_MAX] = ACPI_STATE_S5
33 };
34
35 static int init_8259A_after_S1;
36
37 extern int acpi_sleep_prepare(u32 acpi_state);
38 extern void acpi_power_off(void);
39
40 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
41
42 /**
43  *      acpi_pm_set_target - Set the target system sleep state to the state
44  *              associated with given @pm_state, if supported.
45  */
46
47 static int acpi_pm_set_target(suspend_state_t pm_state)
48 {
49         u32 acpi_state = acpi_suspend_states[pm_state];
50         int error = 0;
51
52         if (sleep_states[acpi_state]) {
53                 acpi_target_sleep_state = acpi_state;
54         } else {
55                 printk(KERN_ERR "ACPI does not support this state: %d\n",
56                         pm_state);
57                 error = -ENOSYS;
58         }
59         return error;
60 }
61
62 /**
63  *      acpi_pm_prepare - Do preliminary suspend work.
64  *      @pm_state: ignored
65  *
66  *      If necessary, set the firmware waking vector and do arch-specific
67  *      nastiness to get the wakeup code to the waking vector.
68  */
69
70 static int acpi_pm_prepare(suspend_state_t pm_state)
71 {
72         int error = acpi_sleep_prepare(acpi_target_sleep_state);
73
74         if (error)
75                 acpi_target_sleep_state = ACPI_STATE_S0;
76
77         return error;
78 }
79
80 /**
81  *      acpi_pm_enter - Actually enter a sleep state.
82  *      @pm_state: ignored
83  *
84  *      Flush caches and go to sleep. For STR we have to call arch-specific
85  *      assembly, which in turn call acpi_enter_sleep_state().
86  *      It's unfortunate, but it works. Please fix if you're feeling frisky.
87  */
88
89 static int acpi_pm_enter(suspend_state_t pm_state)
90 {
91         acpi_status status = AE_OK;
92         unsigned long flags = 0;
93         u32 acpi_state = acpi_target_sleep_state;
94
95         ACPI_FLUSH_CPU_CACHE();
96
97         /* Do arch specific saving of state. */
98         if (acpi_state == ACPI_STATE_S3) {
99                 int error = acpi_save_state_mem();
100
101                 if (error) {
102                         acpi_target_sleep_state = ACPI_STATE_S0;
103                         return error;
104                 }
105         }
106
107         local_irq_save(flags);
108         acpi_enable_wakeup_device(acpi_state);
109         switch (acpi_state) {
110         case ACPI_STATE_S1:
111                 barrier();
112                 status = acpi_enter_sleep_state(acpi_state);
113                 break;
114
115         case ACPI_STATE_S3:
116                 do_suspend_lowlevel();
117                 break;
118         }
119
120         /* ACPI 3.0 specs (P62) says that it's the responsabilty
121          * of the OSPM to clear the status bit [ implying that the
122          * POWER_BUTTON event should not reach userspace ]
123          */
124         if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
125                 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
126
127         local_irq_restore(flags);
128         printk(KERN_DEBUG "Back to C!\n");
129
130         /* restore processor state */
131         if (acpi_state == ACPI_STATE_S3)
132                 acpi_restore_state_mem();
133
134         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
135 }
136
137 /**
138  *      acpi_pm_finish - Finish up suspend sequence.
139  *      @pm_state: ignored
140  *
141  *      This is called after we wake back up (or if entering the sleep state
142  *      failed). 
143  */
144
145 static int acpi_pm_finish(suspend_state_t pm_state)
146 {
147         u32 acpi_state = acpi_target_sleep_state;
148
149         acpi_leave_sleep_state(acpi_state);
150         acpi_disable_wakeup_device(acpi_state);
151
152         /* reset firmware waking vector */
153         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
154
155         acpi_target_sleep_state = ACPI_STATE_S0;
156
157 #ifdef CONFIG_X86
158         if (init_8259A_after_S1) {
159                 printk("Broken toshiba laptop -> kicking interrupts\n");
160                 init_8259A(0);
161         }
162 #endif
163         return 0;
164 }
165
166 int acpi_suspend(u32 acpi_state)
167 {
168         suspend_state_t states[] = {
169                 [1] = PM_SUSPEND_STANDBY,
170                 [3] = PM_SUSPEND_MEM,
171                 [5] = PM_SUSPEND_MAX
172         };
173
174         if (acpi_state < 6 && states[acpi_state])
175                 return pm_suspend(states[acpi_state]);
176         if (acpi_state == 4)
177                 return hibernate();
178         return -EINVAL;
179 }
180
181 static int acpi_pm_state_valid(suspend_state_t pm_state)
182 {
183         u32 acpi_state;
184
185         switch (pm_state) {
186         case PM_SUSPEND_ON:
187         case PM_SUSPEND_STANDBY:
188         case PM_SUSPEND_MEM:
189                 acpi_state = acpi_suspend_states[pm_state];
190
191                 return sleep_states[acpi_state];
192         default:
193                 return 0;
194         }
195 }
196
197 static struct pm_ops acpi_pm_ops = {
198         .valid = acpi_pm_state_valid,
199         .set_target = acpi_pm_set_target,
200         .prepare = acpi_pm_prepare,
201         .enter = acpi_pm_enter,
202         .finish = acpi_pm_finish,
203 };
204
205 #ifdef CONFIG_SOFTWARE_SUSPEND
206 static int acpi_hibernation_prepare(void)
207 {
208         return acpi_sleep_prepare(ACPI_STATE_S4);
209 }
210
211 static int acpi_hibernation_enter(void)
212 {
213         acpi_status status = AE_OK;
214         unsigned long flags = 0;
215
216         ACPI_FLUSH_CPU_CACHE();
217
218         local_irq_save(flags);
219         acpi_enable_wakeup_device(ACPI_STATE_S4);
220         /* This shouldn't return.  If it returns, we have a problem */
221         status = acpi_enter_sleep_state(ACPI_STATE_S4);
222         local_irq_restore(flags);
223
224         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
225 }
226
227 static void acpi_hibernation_finish(void)
228 {
229         acpi_leave_sleep_state(ACPI_STATE_S4);
230         acpi_disable_wakeup_device(ACPI_STATE_S4);
231
232         /* reset firmware waking vector */
233         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
234 }
235
236 static int acpi_hibernation_pre_restore(void)
237 {
238         acpi_status status;
239
240         status = acpi_hw_disable_all_gpes();
241
242         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
243 }
244
245 static void acpi_hibernation_restore_cleanup(void)
246 {
247         acpi_hw_enable_all_runtime_gpes();
248 }
249
250 static struct hibernation_ops acpi_hibernation_ops = {
251         .prepare = acpi_hibernation_prepare,
252         .enter = acpi_hibernation_enter,
253         .finish = acpi_hibernation_finish,
254         .pre_restore = acpi_hibernation_pre_restore,
255         .restore_cleanup = acpi_hibernation_restore_cleanup,
256 };
257 #endif                          /* CONFIG_SOFTWARE_SUSPEND */
258
259 /**
260  *      acpi_pm_device_sleep_state - return preferred power state of ACPI device
261  *              in the system sleep state given by %acpi_target_sleep_state
262  *      @dev: device to examine
263  *      @wake: if set, the device should be able to wake up the system
264  *      @d_min_p: used to store the upper limit of allowed states range
265  *      Return value: preferred power state of the device on success, -ENODEV on
266  *              failure (ie. if there's no 'struct acpi_device' for @dev)
267  *
268  *      Find the lowest power (highest number) ACPI device power state that
269  *      device @dev can be in while the system is in the sleep state represented
270  *      by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
271  *      able to wake up the system from this sleep state.  If @d_min_p is set,
272  *      the highest power (lowest number) device power state of @dev allowed
273  *      in this system sleep state is stored at the location pointed to by it.
274  *
275  *      The caller must ensure that @dev is valid before using this function.
276  *      The caller is also responsible for figuring out if the device is
277  *      supposed to be able to wake up the system and passing this information
278  *      via @wake.
279  */
280
281 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
282 {
283         acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
284         struct acpi_device *adev;
285         char acpi_method[] = "_SxD";
286         unsigned long d_min, d_max;
287
288         if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
289                 printk(KERN_ERR "ACPI handle has no context!\n");
290                 return -ENODEV;
291         }
292
293         acpi_method[2] = '0' + acpi_target_sleep_state;
294         /*
295          * If the sleep state is S0, we will return D3, but if the device has
296          * _S0W, we will use the value from _S0W
297          */
298         d_min = ACPI_STATE_D0;
299         d_max = ACPI_STATE_D3;
300
301         /*
302          * If present, _SxD methods return the minimum D-state (highest power
303          * state) we can use for the corresponding S-states.  Otherwise, the
304          * minimum D-state is D0 (ACPI 3.x).
305          *
306          * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
307          * provided -- that's our fault recovery, we ignore retval.
308          */
309         if (acpi_target_sleep_state > ACPI_STATE_S0)
310                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
311
312         /*
313          * If _PRW says we can wake up the system from the target sleep state,
314          * the D-state returned by _SxD is sufficient for that (we assume a
315          * wakeup-aware driver if wake is set).  Still, if _SxW exists
316          * (ACPI 3.x), it should return the maximum (lowest power) D-state that
317          * can wake the system.  _S0W may be valid, too.
318          */
319         if (acpi_target_sleep_state == ACPI_STATE_S0 ||
320             (wake && adev->wakeup.state.enabled &&
321              adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
322                 acpi_method[3] = 'W';
323                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
324                 /* Sanity check */
325                 if (d_max < d_min)
326                         d_min = d_max;
327         }
328
329         if (d_min_p)
330                 *d_min_p = d_min;
331         return d_max;
332 }
333
334 /*
335  * Toshiba fails to preserve interrupts over S1, reinitialization
336  * of 8259 is needed after S1 resume.
337  */
338 static int __init init_ints_after_s1(struct dmi_system_id *d)
339 {
340         printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
341         init_8259A_after_S1 = 1;
342         return 0;
343 }
344
345 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
346         {
347          .callback = init_ints_after_s1,
348          .ident = "Toshiba Satellite 4030cdt",
349          .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
350          },
351         {},
352 };
353
354 int __init acpi_sleep_init(void)
355 {
356         int i = 0;
357
358         dmi_check_system(acpisleep_dmi_table);
359
360         if (acpi_disabled)
361                 return 0;
362
363         printk(KERN_INFO PREFIX "(supports");
364         for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
365                 acpi_status status;
366                 u8 type_a, type_b;
367                 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
368                 if (ACPI_SUCCESS(status)) {
369                         sleep_states[i] = 1;
370                         printk(" S%d", i);
371                 }
372         }
373         printk(")\n");
374
375         pm_set_ops(&acpi_pm_ops);
376
377 #ifdef CONFIG_SOFTWARE_SUSPEND
378         if (sleep_states[ACPI_STATE_S4])
379                 hibernation_set_ops(&acpi_hibernation_ops);
380 #else
381         sleep_states[ACPI_STATE_S4] = 0;
382 #endif
383
384         return 0;
385 }