vserver 1.9.3
[linux-2.6.git] / drivers / acpi / sleep / main.c
1 /*
2  * sleep.c - ACPI sleep support.
3  *
4  * Copyright (c) 2000-2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2.
8  *
9  */
10
11 #include <linux/delay.h>
12 #include <linux/irq.h>
13 #include <linux/dmi.h>
14 #include <linux/device.h>
15 #include <linux/suspend.h>
16 #include <acpi/acpi_bus.h>
17 #include <acpi/acpi_drivers.h>
18 #include "sleep.h"
19
20 u8 sleep_states[ACPI_S_STATE_COUNT];
21
22 static struct pm_ops acpi_pm_ops;
23
24 extern void do_suspend_lowlevel_s4bios(void);
25 extern void do_suspend_lowlevel(void);
26
27 static u32 acpi_suspend_states[] = {
28         [PM_SUSPEND_ON]         = ACPI_STATE_S0,
29         [PM_SUSPEND_STANDBY]    = ACPI_STATE_S1,
30         [PM_SUSPEND_MEM]        = ACPI_STATE_S3,
31         [PM_SUSPEND_DISK]       = ACPI_STATE_S4,
32 };
33
34 static int init_8259A_after_S1;
35
36 /**
37  *      acpi_pm_prepare - Do preliminary suspend work.
38  *      @pm_state:              suspend state we're entering.
39  *
40  *      Make sure we support the state. If we do, and we need it, set the
41  *      firmware waking vector and do arch-specific nastiness to get the 
42  *      wakeup code to the waking vector. 
43  */
44
45 static int acpi_pm_prepare(u32 pm_state)
46 {
47         u32 acpi_state = acpi_suspend_states[pm_state];
48
49         if (!sleep_states[acpi_state])
50                 return -EPERM;
51
52         /* do we have a wakeup address for S2 and S3? */
53         /* Here, we support only S4BIOS, those we set the wakeup address */
54         /* S4OS is only supported for now via swsusp.. */
55         if (pm_state == PM_SUSPEND_MEM || pm_state == PM_SUSPEND_DISK) {
56                 if (!acpi_wakeup_address)
57                         return -EFAULT;
58                 acpi_set_firmware_waking_vector(
59                         (acpi_physical_address) acpi_wakeup_address);
60         }
61         ACPI_FLUSH_CPU_CACHE();
62         acpi_enable_wakeup_device_prep(acpi_state);
63         acpi_enter_sleep_state_prep(acpi_state);
64         return 0;
65 }
66
67
68 /**
69  *      acpi_pm_enter - Actually enter a sleep state.
70  *      @pm_state:              State we're entering.
71  *
72  *      Flush caches and go to sleep. For STR or STD, we have to call 
73  *      arch-specific assembly, which in turn call acpi_enter_sleep_state().
74  *      It's unfortunate, but it works. Please fix if you're feeling frisky.
75  */
76
77 static int acpi_pm_enter(u32 pm_state)
78 {
79         acpi_status status = AE_OK;
80         unsigned long flags = 0;
81         u32 acpi_state = acpi_suspend_states[pm_state];
82
83         ACPI_FLUSH_CPU_CACHE();
84
85         /* Do arch specific saving of state. */
86         if (pm_state > PM_SUSPEND_STANDBY) {
87                 int error = acpi_save_state_mem();
88                 if (error)
89                         return error;
90         }
91
92
93         local_irq_save(flags);
94         acpi_enable_wakeup_device(acpi_state);
95         switch (pm_state)
96         {
97         case PM_SUSPEND_STANDBY:
98                 barrier();
99                 status = acpi_enter_sleep_state(acpi_state);
100                 break;
101
102         case PM_SUSPEND_MEM:
103                 do_suspend_lowlevel();
104                 break;
105
106         case PM_SUSPEND_DISK:
107                 if (acpi_pm_ops.pm_disk_mode == PM_DISK_PLATFORM)
108                         status = acpi_enter_sleep_state(acpi_state);
109                 else
110                         do_suspend_lowlevel_s4bios();
111                 break;
112         default:
113                 return -EINVAL;
114         }
115         local_irq_restore(flags);
116         printk(KERN_DEBUG "Back to C!\n");
117
118         /* restore processor state
119          * We should only be here if we're coming back from STR or STD.
120          * And, in the case of the latter, the memory image should have already
121          * been loaded from disk.
122          */
123         if (pm_state > PM_SUSPEND_STANDBY)
124                 acpi_restore_state_mem();
125
126
127         return ACPI_SUCCESS(status) ? 0 : -EFAULT;
128 }
129
130
131 /**
132  *      acpi_pm_finish - Finish up suspend sequence.
133  *      @pm_state:              State we're coming out of.
134  *
135  *      This is called after we wake back up (or if entering the sleep state
136  *      failed). 
137  */
138
139 static int acpi_pm_finish(u32 pm_state)
140 {
141         u32 acpi_state = acpi_suspend_states[pm_state];
142
143         acpi_leave_sleep_state(acpi_state);
144         acpi_disable_wakeup_device(acpi_state);
145
146         /* reset firmware waking vector */
147         acpi_set_firmware_waking_vector((acpi_physical_address) 0);
148
149         if (init_8259A_after_S1) {
150                 printk("Broken toshiba laptop -> kicking interrupts\n");
151                 init_8259A(0);
152         }
153         return 0;
154 }
155
156
157 int acpi_suspend(u32 acpi_state)
158 {
159         u32 states[] = {
160                 [1]     = PM_SUSPEND_STANDBY,
161                 [3]     = PM_SUSPEND_MEM,
162                 [4]     = PM_SUSPEND_DISK,
163         };
164
165         if (acpi_state <= 4 && states[acpi_state])
166                 return pm_suspend(states[acpi_state]);
167         return -EINVAL;
168 }
169
170 static struct pm_ops acpi_pm_ops = {
171         .prepare        = acpi_pm_prepare,
172         .enter          = acpi_pm_enter,
173         .finish         = acpi_pm_finish,
174 };
175
176
177 /*
178  * Toshiba fails to preserve interrupts over S1, reinitialization
179  * of 8259 is needed after S1 resume.
180  */
181 static int __init init_ints_after_s1(struct dmi_system_id *d)
182 {
183         printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
184         init_8259A_after_S1 = 1;
185         return 0;
186 }
187
188 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
189         {
190                 .callback = init_ints_after_s1,
191                 .ident = "Toshiba Satellite 4030cdt",
192                 .matches = { DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"), },
193         },
194         { },
195 };
196
197 static int __init acpi_sleep_init(void)
198 {
199         int                     i = 0;
200
201         dmi_check_system(acpisleep_dmi_table);
202
203         if (acpi_disabled)
204                 return 0;
205
206         printk(KERN_INFO PREFIX "(supports");
207         for (i=0; i < ACPI_S_STATE_COUNT; i++) {
208                 acpi_status status;
209                 u8 type_a, type_b;
210                 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
211                 if (ACPI_SUCCESS(status)) {
212                         sleep_states[i] = 1;
213                         printk(" S%d", i);
214                 }
215                 if (i == ACPI_STATE_S4) {
216                         if (acpi_gbl_FACS->S4bios_f) {
217                                 sleep_states[i] = 1;
218                                 printk(" S4bios");
219                                 acpi_pm_ops.pm_disk_mode = PM_DISK_FIRMWARE;
220                         }
221                         if (sleep_states[i])
222                                 acpi_pm_ops.pm_disk_mode = PM_DISK_PLATFORM;
223                 }
224         }
225         printk(")\n");
226
227         pm_set_ops(&acpi_pm_ops);
228         return 0;
229 }
230
231 late_initcall(acpi_sleep_init);