blob: 54c2dfcf86511dec831693c86c0494f16e6a1028 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sleep.c - ACPI sleep support.
3 *
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -05004 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <acpi/acpi_bus.h>
19#include <acpi/acpi_drivers.h>
20#include "sleep.h"
21
22u8 sleep_states[ACPI_S_STATE_COUNT];
23
24static struct pm_ops acpi_pm_ops;
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026extern void do_suspend_lowlevel(void);
27
28static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050029 [PM_SUSPEND_ON] = ACPI_STATE_S0,
30 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
31 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050032 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35static int init_8259A_after_S1;
36
37/**
38 * acpi_pm_prepare - Do preliminary suspend work.
39 * @pm_state: suspend state we're entering.
40 *
41 * Make sure we support the state. If we do, and we need it, set the
42 * firmware waking vector and do arch-specific nastiness to get the
43 * wakeup code to the waking vector.
44 */
45
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050046extern int acpi_sleep_prepare(u32 acpi_state);
47extern void acpi_power_off(void);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int acpi_pm_prepare(suspend_state_t pm_state)
50{
51 u32 acpi_state = acpi_suspend_states[pm_state];
52
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050053 if (!sleep_states[acpi_state]) {
54 printk("acpi_pm_prepare does not support %d \n", pm_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050057 return acpi_sleep_prepare(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/**
61 * acpi_pm_enter - Actually enter a sleep state.
62 * @pm_state: State we're entering.
63 *
64 * Flush caches and go to sleep. For STR or STD, we have to call
65 * arch-specific assembly, which in turn call acpi_enter_sleep_state().
66 * It's unfortunate, but it works. Please fix if you're feeling frisky.
67 */
68
69static int acpi_pm_enter(suspend_state_t pm_state)
70{
71 acpi_status status = AE_OK;
72 unsigned long flags = 0;
73 u32 acpi_state = acpi_suspend_states[pm_state];
74
75 ACPI_FLUSH_CPU_CACHE();
76
77 /* Do arch specific saving of state. */
78 if (pm_state > PM_SUSPEND_STANDBY) {
79 int error = acpi_save_state_mem();
80 if (error)
81 return error;
82 }
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 local_irq_save(flags);
85 acpi_enable_wakeup_device(acpi_state);
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050086 switch (pm_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 case PM_SUSPEND_STANDBY:
88 barrier();
89 status = acpi_enter_sleep_state(acpi_state);
90 break;
91
92 case PM_SUSPEND_MEM:
93 do_suspend_lowlevel();
94 break;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 default:
97 return -EINVAL;
98 }
Arnaud Patard872d83d2006-04-27 05:25:00 -040099
100 /* ACPI 3.0 specs (P62) says that it's the responsabilty
101 * of the OSPM to clear the status bit [ implying that the
102 * POWER_BUTTON event should not reach userspace ]
103 */
104 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
105 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 local_irq_restore(flags);
108 printk(KERN_DEBUG "Back to C!\n");
109
110 /* restore processor state
111 * We should only be here if we're coming back from STR or STD.
112 * And, in the case of the latter, the memory image should have already
113 * been loaded from disk.
114 */
115 if (pm_state > PM_SUSPEND_STANDBY)
116 acpi_restore_state_mem();
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/**
122 * acpi_pm_finish - Finish up suspend sequence.
123 * @pm_state: State we're coming out of.
124 *
125 * This is called after we wake back up (or if entering the sleep state
126 * failed).
127 */
128
129static int acpi_pm_finish(suspend_state_t pm_state)
130{
131 u32 acpi_state = acpi_suspend_states[pm_state];
132
133 acpi_leave_sleep_state(acpi_state);
134 acpi_disable_wakeup_device(acpi_state);
135
136 /* reset firmware waking vector */
137 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
138
Len Browne8b2fd02007-07-24 22:26:33 -0400139#ifdef CONFIG_X86
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (init_8259A_after_S1) {
141 printk("Broken toshiba laptop -> kicking interrupts\n");
142 init_8259A(0);
143 }
Len Browne8b2fd02007-07-24 22:26:33 -0400144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148int acpi_suspend(u32 acpi_state)
149{
150 suspend_state_t states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500151 [1] = PM_SUSPEND_STANDBY,
152 [3] = PM_SUSPEND_MEM,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500153 [5] = PM_SUSPEND_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 };
155
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500156 if (acpi_state < 6 && states[acpi_state])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return pm_suspend(states[acpi_state]);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700158 if (acpi_state == 4)
159 return hibernate();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EINVAL;
161}
162
Shaohua Lieb9289e2005-10-30 15:00:01 -0800163static int acpi_pm_state_valid(suspend_state_t pm_state)
164{
Johannes Berge8c9c502007-04-30 15:09:54 -0700165 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800166
Johannes Berge8c9c502007-04-30 15:09:54 -0700167 switch (pm_state) {
168 case PM_SUSPEND_ON:
169 case PM_SUSPEND_STANDBY:
170 case PM_SUSPEND_MEM:
171 acpi_state = acpi_suspend_states[pm_state];
172
173 return sleep_states[acpi_state];
174 default:
175 return 0;
176 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static struct pm_ops acpi_pm_ops = {
Shaohua Lieb9289e2005-10-30 15:00:01 -0800180 .valid = acpi_pm_state_valid,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500181 .prepare = acpi_pm_prepare,
182 .enter = acpi_pm_enter,
183 .finish = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700186#ifdef CONFIG_SOFTWARE_SUSPEND
187static int acpi_hibernation_prepare(void)
188{
189 return acpi_sleep_prepare(ACPI_STATE_S4);
190}
191
192static int acpi_hibernation_enter(void)
193{
194 acpi_status status = AE_OK;
195 unsigned long flags = 0;
196
197 ACPI_FLUSH_CPU_CACHE();
198
199 local_irq_save(flags);
200 acpi_enable_wakeup_device(ACPI_STATE_S4);
201 /* This shouldn't return. If it returns, we have a problem */
202 status = acpi_enter_sleep_state(ACPI_STATE_S4);
203 local_irq_restore(flags);
204
205 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
206}
207
208static void acpi_hibernation_finish(void)
209{
210 acpi_leave_sleep_state(ACPI_STATE_S4);
211 acpi_disable_wakeup_device(ACPI_STATE_S4);
212
213 /* reset firmware waking vector */
214 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700215}
216
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700217static int acpi_hibernation_pre_restore(void)
218{
219 acpi_status status;
220
221 status = acpi_hw_disable_all_gpes();
222
223 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
224}
225
226static void acpi_hibernation_restore_cleanup(void)
227{
228 acpi_hw_enable_all_runtime_gpes();
229}
230
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700231static struct hibernation_ops acpi_hibernation_ops = {
232 .prepare = acpi_hibernation_prepare,
233 .enter = acpi_hibernation_enter,
234 .finish = acpi_hibernation_finish,
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700235 .pre_restore = acpi_hibernation_pre_restore,
236 .restore_cleanup = acpi_hibernation_restore_cleanup,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700237};
Len Brownfd350942007-05-09 23:34:35 -0400238#endif /* CONFIG_SOFTWARE_SUSPEND */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Toshiba fails to preserve interrupts over S1, reinitialization
242 * of 8259 is needed after S1 resume.
243 */
244static int __init init_ints_after_s1(struct dmi_system_id *d)
245{
246 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
247 init_8259A_after_S1 = 1;
248 return 0;
249}
250
251static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
252 {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500253 .callback = init_ints_after_s1,
254 .ident = "Toshiba Satellite 4030cdt",
255 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
256 },
257 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500260int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500262 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 dmi_check_system(acpisleep_dmi_table);
265
266 if (acpi_disabled)
267 return 0;
268
269 printk(KERN_INFO PREFIX "(supports");
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500270 for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 acpi_status status;
272 u8 type_a, type_b;
273 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
274 if (ACPI_SUCCESS(status)) {
275 sleep_states[i] = 1;
276 printk(" S%d", i);
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 printk(")\n");
280
281 pm_set_ops(&acpi_pm_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700282
283#ifdef CONFIG_SOFTWARE_SUSPEND
284 if (sleep_states[ACPI_STATE_S4])
285 hibernation_set_ops(&acpi_hibernation_ops);
286#else
287 sleep_states[ACPI_STATE_S4] = 0;
288#endif
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 0;
291}