2 * sleep.c - ACPI sleep support.
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
9 * This file is released under the GPLv2.
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>
22 u8 sleep_states
[ACPI_S_STATE_COUNT
];
24 static struct pm_ops acpi_pm_ops
;
26 extern void do_suspend_lowlevel(void);
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
35 static int init_8259A_after_S1
;
37 extern int acpi_sleep_prepare(u32 acpi_state
);
38 extern void acpi_power_off(void);
40 static u32 acpi_target_sleep_state
= ACPI_STATE_S0
;
43 * acpi_pm_set_target - Set the target system sleep state to the state
44 * associated with given @pm_state, if supported.
47 static int acpi_pm_set_target(suspend_state_t pm_state
)
49 u32 acpi_state
= acpi_suspend_states
[pm_state
];
52 if (sleep_states
[acpi_state
]) {
53 acpi_target_sleep_state
= acpi_state
;
55 printk(KERN_ERR
"ACPI does not support this state: %d\n",
63 * acpi_pm_prepare - Do preliminary suspend work.
66 * If necessary, set the firmware waking vector and do arch-specific
67 * nastiness to get the wakeup code to the waking vector.
70 static int acpi_pm_prepare(suspend_state_t pm_state
)
72 int error
= acpi_sleep_prepare(acpi_target_sleep_state
);
75 acpi_target_sleep_state
= ACPI_STATE_S0
;
81 * acpi_pm_enter - Actually enter a sleep state.
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.
89 static int acpi_pm_enter(suspend_state_t pm_state
)
91 acpi_status status
= AE_OK
;
92 unsigned long flags
= 0;
93 u32 acpi_state
= acpi_target_sleep_state
;
95 ACPI_FLUSH_CPU_CACHE();
97 /* Do arch specific saving of state. */
98 if (acpi_state
== ACPI_STATE_S3
) {
99 int error
= acpi_save_state_mem();
102 acpi_target_sleep_state
= ACPI_STATE_S0
;
107 local_irq_save(flags
);
108 acpi_enable_wakeup_device(acpi_state
);
109 switch (acpi_state
) {
112 status
= acpi_enter_sleep_state(acpi_state
);
116 do_suspend_lowlevel();
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 ]
124 if (ACPI_SUCCESS(status
) && (acpi_state
== ACPI_STATE_S3
))
125 acpi_clear_event(ACPI_EVENT_POWER_BUTTON
);
127 local_irq_restore(flags
);
128 printk(KERN_DEBUG
"Back to C!\n");
130 /* restore processor state */
131 if (acpi_state
== ACPI_STATE_S3
)
132 acpi_restore_state_mem();
134 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
138 * acpi_pm_finish - Finish up suspend sequence.
141 * This is called after we wake back up (or if entering the sleep state
145 static int acpi_pm_finish(suspend_state_t pm_state
)
147 u32 acpi_state
= acpi_target_sleep_state
;
149 acpi_leave_sleep_state(acpi_state
);
150 acpi_disable_wakeup_device(acpi_state
);
152 /* reset firmware waking vector */
153 acpi_set_firmware_waking_vector((acpi_physical_address
) 0);
155 acpi_target_sleep_state
= ACPI_STATE_S0
;
158 if (init_8259A_after_S1
) {
159 printk("Broken toshiba laptop -> kicking interrupts\n");
166 int acpi_suspend(u32 acpi_state
)
168 suspend_state_t states
[] = {
169 [1] = PM_SUSPEND_STANDBY
,
170 [3] = PM_SUSPEND_MEM
,
174 if (acpi_state
< 6 && states
[acpi_state
])
175 return pm_suspend(states
[acpi_state
]);
181 static int acpi_pm_state_valid(suspend_state_t pm_state
)
187 case PM_SUSPEND_STANDBY
:
189 acpi_state
= acpi_suspend_states
[pm_state
];
191 return sleep_states
[acpi_state
];
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
,
205 #ifdef CONFIG_HIBERNATION
206 static int acpi_hibernation_prepare(void)
208 return acpi_sleep_prepare(ACPI_STATE_S4
);
211 static int acpi_hibernation_enter(void)
213 acpi_status status
= AE_OK
;
214 unsigned long flags
= 0;
216 ACPI_FLUSH_CPU_CACHE();
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
);
224 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
227 static void acpi_hibernation_finish(void)
229 acpi_leave_sleep_state(ACPI_STATE_S4
);
230 acpi_disable_wakeup_device(ACPI_STATE_S4
);
232 /* reset firmware waking vector */
233 acpi_set_firmware_waking_vector((acpi_physical_address
) 0);
236 static int acpi_hibernation_pre_restore(void)
240 status
= acpi_hw_disable_all_gpes();
242 return ACPI_SUCCESS(status
) ? 0 : -EFAULT
;
245 static void acpi_hibernation_restore_cleanup(void)
247 acpi_hw_enable_all_runtime_gpes();
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
,
257 #endif /* CONFIG_HIBERNATION */
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)
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.
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
281 int acpi_pm_device_sleep_state(struct device
*dev
, int wake
, int *d_min_p
)
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
;
288 if (!handle
|| ACPI_FAILURE(acpi_bus_get_device(handle
, &adev
))) {
289 printk(KERN_ERR
"ACPI handle has no context!\n");
293 acpi_method
[2] = '0' + acpi_target_sleep_state
;
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
298 d_min
= ACPI_STATE_D0
;
299 d_max
= ACPI_STATE_D3
;
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).
306 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
307 * provided -- that's our fault recovery, we ignore retval.
309 if (acpi_target_sleep_state
> ACPI_STATE_S0
)
310 acpi_evaluate_integer(handle
, acpi_method
, NULL
, &d_min
);
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.
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
);
335 * Toshiba fails to preserve interrupts over S1, reinitialization
336 * of 8259 is needed after S1 resume.
338 static int __init
init_ints_after_s1(struct dmi_system_id
*d
)
340 printk(KERN_WARNING
"%s with broken S1 detected.\n", d
->ident
);
341 init_8259A_after_S1
= 1;
345 static struct dmi_system_id __initdata acpisleep_dmi_table
[] = {
347 .callback
= init_ints_after_s1
,
348 .ident
= "Toshiba Satellite 4030cdt",
349 .matches
= {DMI_MATCH(DMI_PRODUCT_NAME
, "S4030CDT/4.3"),},
354 int __init
acpi_sleep_init(void)
358 dmi_check_system(acpisleep_dmi_table
);
363 printk(KERN_INFO PREFIX
"(supports");
364 for (i
= 0; i
< ACPI_S_STATE_COUNT
; i
++) {
367 status
= acpi_get_sleep_type_data(i
, &type_a
, &type_b
);
368 if (ACPI_SUCCESS(status
)) {
375 pm_set_ops(&acpi_pm_ops
);
377 #ifdef CONFIG_HIBERNATION
378 if (sleep_states
[ACPI_STATE_S4
])
379 hibernation_set_ops(&acpi_hibernation_ops
);
381 sleep_states
[ACPI_STATE_S4
] = 0;