ACPI: Remove references to ACPI_STATE_S2 from acpi_pm_enter
[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         if (init_8259A_after_S1) {
158                 printk("Broken toshiba laptop -> kicking interrupts\n");
159                 init_8259A(0);
160         }
161         return 0;
162 }
163
164 int acpi_suspend(u32 acpi_state)
165 {
166         suspend_state_t states[] = {
167                 [1] = PM_SUSPEND_STANDBY,
168                 [3] = PM_SUSPEND_MEM,
169                 [5] = PM_SUSPEND_MAX
170         };
171
172         if (acpi_state < 6 && states[acpi_state])
173                 return pm_suspend(states[acpi_state]);
174         if (acpi_state == 4)
175                 return hibernate();
176         return -EINVAL;
177 }
178
179 static int acpi_pm_state_valid(suspend_state_t pm_state)
180 {
181         u32 acpi_state;
182
183         switch (pm_state) {
184         case PM_SUSPEND_ON:
185         case PM_SUSPEND_STANDBY:
186         case PM_SUSPEND_MEM:
187                 acpi_state = acpi_suspend_states[pm_state];
188
189                 return sleep_states[acpi_state];
190         default:
191                 return 0;
192         }
193 }
194
195 static struct pm_ops acpi_pm_ops = {
196         .valid = acpi_pm_state_valid,
197         .set_target = acpi_pm_set_target,
198         .prepare = acpi_pm_prepare,
199         .enter = acpi_pm_enter,
200         .finish = acpi_pm_finish,
201 };
202
203 #ifdef CONFIG_SOFTWARE_SUSPEND
204 static int acpi_hibernation_prepare(void)
205 {
206         return acpi_sleep_prepare(ACPI_STATE_S4);
207 }
208
209 static int acpi_hibernation_enter(void)
210 {
211         acpi_status status = AE_OK;
212         unsigned long flags = 0;
213
214         ACPI_FLUSH_CPU_CACHE();
215
216         local_irq_save(flags);
217         acpi_enable_wakeup_device(ACPI_STATE_S4);
218         /* This shouldn't return.  If it returns, we have a problem */
219         status = acpi_enter_sleep_state(ACPI_STATE_S4);
220         local_irq_restore(flags);
221
222         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
223 }
224
225 static void acpi_hibernation_finish(void)
226 {
227         acpi_leave_sleep_state(ACPI_STATE_S4);
228         acpi_disable_wakeup_device(ACPI_STATE_S4);
229
230         /* reset firmware waking vector */
231         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
232
233         if (init_8259A_after_S1) {
234                 printk("Broken toshiba laptop -> kicking interrupts\n");
235                 init_8259A(0);
236         }
237 }
238
239 static int acpi_hibernation_pre_restore(void)
240 {
241         acpi_status status;
242
243         status = acpi_hw_disable_all_gpes();
244
245         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
246 }
247
248 static void acpi_hibernation_restore_cleanup(void)
249 {
250         acpi_hw_enable_all_runtime_gpes();
251 }
252
253 static struct hibernation_ops acpi_hibernation_ops = {
254         .prepare = acpi_hibernation_prepare,
255         .enter = acpi_hibernation_enter,
256         .finish = acpi_hibernation_finish,
257         .pre_restore = acpi_hibernation_pre_restore,
258         .restore_cleanup = acpi_hibernation_restore_cleanup,
259 };
260 #endif                          /* CONFIG_SOFTWARE_SUSPEND */
261
262 /**
263  *      acpi_pm_device_sleep_state - return preferred power state of ACPI device
264  *              in the system sleep state given by %acpi_target_sleep_state
265  *      @dev: device to examine
266  *      @wake: if set, the device should be able to wake up the system
267  *      @d_min_p: used to store the upper limit of allowed states range
268  *      Return value: preferred power state of the device on success, -ENODEV on
269  *              failure (ie. if there's no 'struct acpi_device' for @dev)
270  *
271  *      Find the lowest power (highest number) ACPI device power state that
272  *      device @dev can be in while the system is in the sleep state represented
273  *      by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
274  *      able to wake up the system from this sleep state.  If @d_min_p is set,
275  *      the highest power (lowest number) device power state of @dev allowed
276  *      in this system sleep state is stored at the location pointed to by it.
277  *
278  *      The caller must ensure that @dev is valid before using this function.
279  *      The caller is also responsible for figuring out if the device is
280  *      supposed to be able to wake up the system and passing this information
281  *      via @wake.
282  */
283
284 int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
285 {
286         acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
287         struct acpi_device *adev;
288         char acpi_method[] = "_SxD";
289         unsigned long d_min, d_max;
290
291         if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
292                 printk(KERN_ERR "ACPI handle has no context!\n");
293                 return -ENODEV;
294         }
295
296         acpi_method[2] = '0' + acpi_target_sleep_state;
297         /*
298          * If the sleep state is S0, we will return D3, but if the device has
299          * _S0W, we will use the value from _S0W
300          */
301         d_min = ACPI_STATE_D0;
302         d_max = ACPI_STATE_D3;
303
304         /*
305          * If present, _SxD methods return the minimum D-state (highest power
306          * state) we can use for the corresponding S-states.  Otherwise, the
307          * minimum D-state is D0 (ACPI 3.x).
308          *
309          * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
310          * provided -- that's our fault recovery, we ignore retval.
311          */
312         if (acpi_target_sleep_state > ACPI_STATE_S0)
313                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
314
315         /*
316          * If _PRW says we can wake up the system from the target sleep state,
317          * the D-state returned by _SxD is sufficient for that (we assume a
318          * wakeup-aware driver if wake is set).  Still, if _SxW exists
319          * (ACPI 3.x), it should return the maximum (lowest power) D-state that
320          * can wake the system.  _S0W may be valid, too.
321          */
322         if (acpi_target_sleep_state == ACPI_STATE_S0 ||
323             (wake && adev->wakeup.state.enabled &&
324              adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
325                 acpi_method[3] = 'W';
326                 acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
327                 /* Sanity check */
328                 if (d_max < d_min)
329                         d_min = d_max;
330         }
331
332         if (d_min_p)
333                 *d_min_p = d_min;
334         return d_max;
335 }
336
337 /*
338  * Toshiba fails to preserve interrupts over S1, reinitialization
339  * of 8259 is needed after S1 resume.
340  */
341 static int __init init_ints_after_s1(struct dmi_system_id *d)
342 {
343         printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
344         init_8259A_after_S1 = 1;
345         return 0;
346 }
347
348 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
349         {
350          .callback = init_ints_after_s1,
351          .ident = "Toshiba Satellite 4030cdt",
352          .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
353          },
354         {},
355 };
356
357 int __init acpi_sleep_init(void)
358 {
359         int i = 0;
360
361         dmi_check_system(acpisleep_dmi_table);
362
363         if (acpi_disabled)
364                 return 0;
365
366         printk(KERN_INFO PREFIX "(supports");
367         for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
368                 acpi_status status;
369                 u8 type_a, type_b;
370                 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
371                 if (ACPI_SUCCESS(status)) {
372                         sleep_states[i] = 1;
373                         printk(" S%d", i);
374                 }
375         }
376         printk(")\n");
377
378         pm_set_ops(&acpi_pm_ops);
379
380 #ifdef CONFIG_SOFTWARE_SUSPEND
381         if (sleep_states[ACPI_STATE_S4])
382                 hibernation_set_ops(&acpi_hibernation_ops);
383 #else
384         sleep_states[ACPI_STATE_S4] = 0;
385 #endif
386
387         return 0;
388 }