]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/acpi/sleep/main.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[mirror_ubuntu-artful-kernel.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
19 #include <asm/io.h>
20
21 #include <acpi/acpi_bus.h>
22 #include <acpi/acpi_drivers.h>
23 #include "sleep.h"
24
25 u8 sleep_states[ACPI_S_STATE_COUNT];
26
27 #ifdef CONFIG_PM_SLEEP
28 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
29 #endif
30
31 static int acpi_sleep_prepare(u32 acpi_state)
32 {
33 #ifdef CONFIG_ACPI_SLEEP
34 /* do we have a wakeup address for S2 and S3? */
35 if (acpi_state == ACPI_STATE_S3) {
36 if (!acpi_wakeup_address) {
37 return -EFAULT;
38 }
39 acpi_set_firmware_waking_vector(
40 (acpi_physical_address)acpi_wakeup_address);
41
42 }
43 ACPI_FLUSH_CPU_CACHE();
44 acpi_enable_wakeup_device_prep(acpi_state);
45 #endif
46 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
47 acpi_state);
48 acpi_enter_sleep_state_prep(acpi_state);
49 return 0;
50 }
51
52 #ifdef CONFIG_SUSPEND
53 static struct platform_suspend_ops acpi_suspend_ops;
54
55 extern void do_suspend_lowlevel(void);
56
57 static u32 acpi_suspend_states[] = {
58 [PM_SUSPEND_ON] = ACPI_STATE_S0,
59 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
60 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
61 [PM_SUSPEND_MAX] = ACPI_STATE_S5
62 };
63
64 /**
65 * acpi_suspend_begin - Set the target system sleep state to the state
66 * associated with given @pm_state, if supported.
67 */
68
69 static int acpi_suspend_begin(suspend_state_t pm_state)
70 {
71 u32 acpi_state = acpi_suspend_states[pm_state];
72 int error = 0;
73
74 if (sleep_states[acpi_state]) {
75 acpi_target_sleep_state = acpi_state;
76 } else {
77 printk(KERN_ERR "ACPI does not support this state: %d\n",
78 pm_state);
79 error = -ENOSYS;
80 }
81 return error;
82 }
83
84 /**
85 * acpi_suspend_prepare - Do preliminary suspend work.
86 *
87 * If necessary, set the firmware waking vector and do arch-specific
88 * nastiness to get the wakeup code to the waking vector.
89 */
90
91 static int acpi_suspend_prepare(void)
92 {
93 int error = acpi_sleep_prepare(acpi_target_sleep_state);
94
95 if (error) {
96 acpi_target_sleep_state = ACPI_STATE_S0;
97 return error;
98 }
99
100 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
101 }
102
103 /**
104 * acpi_suspend_enter - Actually enter a sleep state.
105 * @pm_state: ignored
106 *
107 * Flush caches and go to sleep. For STR we have to call arch-specific
108 * assembly, which in turn call acpi_enter_sleep_state().
109 * It's unfortunate, but it works. Please fix if you're feeling frisky.
110 */
111
112 static int acpi_suspend_enter(suspend_state_t pm_state)
113 {
114 acpi_status status = AE_OK;
115 unsigned long flags = 0;
116 u32 acpi_state = acpi_target_sleep_state;
117
118 ACPI_FLUSH_CPU_CACHE();
119
120 /* Do arch specific saving of state. */
121 if (acpi_state == ACPI_STATE_S3) {
122 int error = acpi_save_state_mem();
123
124 if (error)
125 return error;
126 }
127
128 local_irq_save(flags);
129 acpi_enable_wakeup_device(acpi_state);
130 switch (acpi_state) {
131 case ACPI_STATE_S1:
132 barrier();
133 status = acpi_enter_sleep_state(acpi_state);
134 break;
135
136 case ACPI_STATE_S3:
137 do_suspend_lowlevel();
138 break;
139 }
140
141 /* Reprogram control registers and execute _BFS */
142 acpi_leave_sleep_state_prep(acpi_state);
143
144 /* ACPI 3.0 specs (P62) says that it's the responsibility
145 * of the OSPM to clear the status bit [ implying that the
146 * POWER_BUTTON event should not reach userspace ]
147 */
148 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
149 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
150
151 /*
152 * Disable and clear GPE status before interrupt is enabled. Some GPEs
153 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
154 * acpi_leave_sleep_state will reenable specific GPEs later
155 */
156 acpi_hw_disable_all_gpes();
157
158 local_irq_restore(flags);
159 printk(KERN_DEBUG "Back to C!\n");
160
161 /* restore processor state */
162 if (acpi_state == ACPI_STATE_S3)
163 acpi_restore_state_mem();
164
165 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
166 }
167
168 /**
169 * acpi_suspend_finish - Instruct the platform to leave a sleep state.
170 *
171 * This is called after we wake back up (or if entering the sleep state
172 * failed).
173 */
174
175 static void acpi_suspend_finish(void)
176 {
177 u32 acpi_state = acpi_target_sleep_state;
178
179 acpi_disable_wakeup_device(acpi_state);
180 acpi_leave_sleep_state(acpi_state);
181
182 /* reset firmware waking vector */
183 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
184
185 acpi_target_sleep_state = ACPI_STATE_S0;
186 }
187
188 /**
189 * acpi_suspend_end - Finish up suspend sequence.
190 */
191
192 static void acpi_suspend_end(void)
193 {
194 /*
195 * This is necessary in case acpi_suspend_finish() is not called during a
196 * failing transition to a sleep state.
197 */
198 acpi_target_sleep_state = ACPI_STATE_S0;
199 }
200
201 static int acpi_suspend_state_valid(suspend_state_t pm_state)
202 {
203 u32 acpi_state;
204
205 switch (pm_state) {
206 case PM_SUSPEND_ON:
207 case PM_SUSPEND_STANDBY:
208 case PM_SUSPEND_MEM:
209 acpi_state = acpi_suspend_states[pm_state];
210
211 return sleep_states[acpi_state];
212 default:
213 return 0;
214 }
215 }
216
217 static struct platform_suspend_ops acpi_suspend_ops = {
218 .valid = acpi_suspend_state_valid,
219 .begin = acpi_suspend_begin,
220 .prepare = acpi_suspend_prepare,
221 .enter = acpi_suspend_enter,
222 .finish = acpi_suspend_finish,
223 .end = acpi_suspend_end,
224 };
225 #endif /* CONFIG_SUSPEND */
226
227 #ifdef CONFIG_HIBERNATION
228 static int acpi_hibernation_begin(void)
229 {
230 acpi_target_sleep_state = ACPI_STATE_S4;
231
232 return 0;
233 }
234
235 static int acpi_hibernation_prepare(void)
236 {
237 int error = acpi_sleep_prepare(ACPI_STATE_S4);
238
239 if (error) {
240 acpi_target_sleep_state = ACPI_STATE_S0;
241 return error;
242 }
243
244 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
245 }
246
247 static int acpi_hibernation_enter(void)
248 {
249 acpi_status status = AE_OK;
250 unsigned long flags = 0;
251
252 ACPI_FLUSH_CPU_CACHE();
253
254 local_irq_save(flags);
255 acpi_enable_wakeup_device(ACPI_STATE_S4);
256 /* This shouldn't return. If it returns, we have a problem */
257 status = acpi_enter_sleep_state(ACPI_STATE_S4);
258 /* Reprogram control registers and execute _BFS */
259 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
260 local_irq_restore(flags);
261
262 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
263 }
264
265 static void acpi_hibernation_leave(void)
266 {
267 /*
268 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
269 * enable it here.
270 */
271 acpi_enable();
272 /* Reprogram control registers and execute _BFS */
273 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
274 }
275
276 static void acpi_hibernation_finish(void)
277 {
278 acpi_disable_wakeup_device(ACPI_STATE_S4);
279 acpi_leave_sleep_state(ACPI_STATE_S4);
280
281 /* reset firmware waking vector */
282 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
283
284 acpi_target_sleep_state = ACPI_STATE_S0;
285 }
286
287 static void acpi_hibernation_end(void)
288 {
289 /*
290 * This is necessary in case acpi_hibernation_finish() is not called
291 * during a failing transition to the sleep state.
292 */
293 acpi_target_sleep_state = ACPI_STATE_S0;
294 }
295
296 static int acpi_hibernation_pre_restore(void)
297 {
298 acpi_status status;
299
300 status = acpi_hw_disable_all_gpes();
301
302 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
303 }
304
305 static void acpi_hibernation_restore_cleanup(void)
306 {
307 acpi_hw_enable_all_runtime_gpes();
308 }
309
310 static struct platform_hibernation_ops acpi_hibernation_ops = {
311 .begin = acpi_hibernation_begin,
312 .end = acpi_hibernation_end,
313 .pre_snapshot = acpi_hibernation_prepare,
314 .finish = acpi_hibernation_finish,
315 .prepare = acpi_hibernation_prepare,
316 .enter = acpi_hibernation_enter,
317 .leave = acpi_hibernation_leave,
318 .pre_restore = acpi_hibernation_pre_restore,
319 .restore_cleanup = acpi_hibernation_restore_cleanup,
320 };
321 #endif /* CONFIG_HIBERNATION */
322
323 int acpi_suspend(u32 acpi_state)
324 {
325 suspend_state_t states[] = {
326 [1] = PM_SUSPEND_STANDBY,
327 [3] = PM_SUSPEND_MEM,
328 [5] = PM_SUSPEND_MAX
329 };
330
331 if (acpi_state < 6 && states[acpi_state])
332 return pm_suspend(states[acpi_state]);
333 if (acpi_state == 4)
334 return hibernate();
335 return -EINVAL;
336 }
337
338 #ifdef CONFIG_PM_SLEEP
339 /**
340 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
341 * in the system sleep state given by %acpi_target_sleep_state
342 * @dev: device to examine; its driver model wakeup flags control
343 * whether it should be able to wake up the system
344 * @d_min_p: used to store the upper limit of allowed states range
345 * Return value: preferred power state of the device on success, -ENODEV on
346 * failure (ie. if there's no 'struct acpi_device' for @dev)
347 *
348 * Find the lowest power (highest number) ACPI device power state that
349 * device @dev can be in while the system is in the sleep state represented
350 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
351 * able to wake up the system from this sleep state. If @d_min_p is set,
352 * the highest power (lowest number) device power state of @dev allowed
353 * in this system sleep state is stored at the location pointed to by it.
354 *
355 * The caller must ensure that @dev is valid before using this function.
356 * The caller is also responsible for figuring out if the device is
357 * supposed to be able to wake up the system and passing this information
358 * via @wake.
359 */
360
361 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
362 {
363 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
364 struct acpi_device *adev;
365 char acpi_method[] = "_SxD";
366 unsigned long d_min, d_max;
367
368 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
369 printk(KERN_DEBUG "ACPI handle has no context!\n");
370 return -ENODEV;
371 }
372
373 acpi_method[2] = '0' + acpi_target_sleep_state;
374 /*
375 * If the sleep state is S0, we will return D3, but if the device has
376 * _S0W, we will use the value from _S0W
377 */
378 d_min = ACPI_STATE_D0;
379 d_max = ACPI_STATE_D3;
380
381 /*
382 * If present, _SxD methods return the minimum D-state (highest power
383 * state) we can use for the corresponding S-states. Otherwise, the
384 * minimum D-state is D0 (ACPI 3.x).
385 *
386 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
387 * provided -- that's our fault recovery, we ignore retval.
388 */
389 if (acpi_target_sleep_state > ACPI_STATE_S0)
390 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
391
392 /*
393 * If _PRW says we can wake up the system from the target sleep state,
394 * the D-state returned by _SxD is sufficient for that (we assume a
395 * wakeup-aware driver if wake is set). Still, if _SxW exists
396 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
397 * can wake the system. _S0W may be valid, too.
398 */
399 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
400 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
401 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
402 acpi_status status;
403
404 acpi_method[3] = 'W';
405 status = acpi_evaluate_integer(handle, acpi_method, NULL,
406 &d_max);
407 if (ACPI_FAILURE(status)) {
408 d_max = d_min;
409 } else if (d_max < d_min) {
410 /* Warn the user of the broken DSDT */
411 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
412 acpi_method);
413 /* Sanitize it */
414 d_min = d_max;
415 }
416 }
417
418 if (d_min_p)
419 *d_min_p = d_min;
420 return d_max;
421 }
422 #endif
423
424 static void acpi_power_off_prepare(void)
425 {
426 /* Prepare to power off the system */
427 acpi_sleep_prepare(ACPI_STATE_S5);
428 acpi_hw_disable_all_gpes();
429 }
430
431 static void acpi_power_off(void)
432 {
433 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
434 printk("%s called\n", __func__);
435 local_irq_disable();
436 acpi_enable_wakeup_device(ACPI_STATE_S5);
437 acpi_enter_sleep_state(ACPI_STATE_S5);
438 }
439
440 int __init acpi_sleep_init(void)
441 {
442 acpi_status status;
443 u8 type_a, type_b;
444 #ifdef CONFIG_SUSPEND
445 int i = 0;
446 #endif
447
448 if (acpi_disabled)
449 return 0;
450
451 sleep_states[ACPI_STATE_S0] = 1;
452 printk(KERN_INFO PREFIX "(supports S0");
453
454 #ifdef CONFIG_SUSPEND
455 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
456 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
457 if (ACPI_SUCCESS(status)) {
458 sleep_states[i] = 1;
459 printk(" S%d", i);
460 }
461 }
462
463 suspend_set_ops(&acpi_suspend_ops);
464 #endif
465
466 #ifdef CONFIG_HIBERNATION
467 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
468 if (ACPI_SUCCESS(status)) {
469 hibernation_set_ops(&acpi_hibernation_ops);
470 sleep_states[ACPI_STATE_S4] = 1;
471 printk(" S4");
472 }
473 #endif
474 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
475 if (ACPI_SUCCESS(status)) {
476 sleep_states[ACPI_STATE_S5] = 1;
477 printk(" S5");
478 pm_power_off_prepare = acpi_power_off_prepare;
479 pm_power_off = acpi_power_off;
480 }
481 printk(")\n");
482 return 0;
483 }