blob: aa0a904e56311feda7f1581a474221fb754ed9d1 [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>
Zhao Yakuie49f7112008-08-12 10:20:22 +080018#include <linux/reboot.h>
H. Peter Anvind1ee4332011-02-14 15:42:46 -080019#include <linux/acpi.h>
Lin Minga2ef5c42012-03-21 10:58:46 +080020#include <linux/module.h>
Lin Mingb24e5092012-03-27 15:43:25 +080021#include <linux/pm_runtime.h>
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +040022
23#include <asm/io.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <acpi/acpi_bus.h>
26#include <acpi/acpi_drivers.h>
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060027
28#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "sleep.h"
30
Konrad Rzeszutek Wilk2a14e542012-04-22 23:03:17 -040031u8 wake_sleep_flags = ACPI_NO_OPTIONAL_METHODS;
Lin Minga2ef5c42012-03-21 10:58:46 +080032static unsigned int gts, bfs;
Konrad Rzeszutek Wilk2a14e542012-04-22 23:03:17 -040033static int set_param_wake_flag(const char *val, struct kernel_param *kp)
34{
35 int ret = param_set_int(val, kp);
36
37 if (ret)
38 return ret;
39
40 if (kp->arg == (const char *)&gts) {
41 if (gts)
42 wake_sleep_flags |= ACPI_EXECUTE_GTS;
43 else
44 wake_sleep_flags &= ~ACPI_EXECUTE_GTS;
45 }
46 if (kp->arg == (const char *)&bfs) {
47 if (bfs)
48 wake_sleep_flags |= ACPI_EXECUTE_BFS;
49 else
50 wake_sleep_flags &= ~ACPI_EXECUTE_BFS;
51 }
52 return ret;
53}
54module_param_call(gts, set_param_wake_flag, param_get_int, &gts, 0644);
55module_param_call(bfs, set_param_wake_flag, param_get_int, &bfs, 0644);
Lin Minga2ef5c42012-03-21 10:58:46 +080056MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend.");
57MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".);
58
Stephen Hemminger01eac602010-10-18 18:47:25 -070059static u8 sleep_states[ACPI_S_STATE_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Zhao Yakuie49f7112008-08-12 10:20:22 +080061static void acpi_sleep_tts_switch(u32 acpi_state)
62{
63 union acpi_object in_arg = { ACPI_TYPE_INTEGER };
64 struct acpi_object_list arg_list = { 1, &in_arg };
65 acpi_status status = AE_OK;
66
67 in_arg.integer.value = acpi_state;
68 status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
69 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
70 /*
71 * OS can't evaluate the _TTS object correctly. Some warning
72 * message will be printed. But it won't break anything.
73 */
74 printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
75 }
76}
77
78static int tts_notify_reboot(struct notifier_block *this,
79 unsigned long code, void *x)
80{
81 acpi_sleep_tts_switch(ACPI_STATE_S5);
82 return NOTIFY_DONE;
83}
84
85static struct notifier_block tts_notifier = {
86 .notifier_call = tts_notify_reboot,
87 .next = NULL,
88 .priority = 0,
89};
90
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010091static int acpi_sleep_prepare(u32 acpi_state)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020092{
93#ifdef CONFIG_ACPI_SLEEP
94 /* do we have a wakeup address for S2 and S3? */
95 if (acpi_state == ACPI_STATE_S3) {
96 if (!acpi_wakeup_address) {
97 return -EFAULT;
98 }
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020099 acpi_set_firmware_waking_vector(
100 (acpi_physical_address)acpi_wakeup_address);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +0200101
102 }
103 ACPI_FLUSH_CPU_CACHE();
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +0200104#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +0100105 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
106 acpi_state);
Rafael J. Wysocki78f5f022010-07-06 22:09:38 -0400107 acpi_enable_wakeup_devices(acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +0200108 acpi_enter_sleep_state_prep(acpi_state);
109 return 0;
110}
111
Rafael J. Wysocki5d1e0722008-10-22 14:58:43 -0400112#ifdef CONFIG_ACPI_SLEEP
Jan Beulich091aad62010-12-07 14:52:25 +0000113static u32 acpi_target_sleep_state = ACPI_STATE_S0;
114
Zhang Ruid7f0eea2009-12-30 15:36:42 +0800115/*
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200116 * The ACPI specification wants us to save NVS memory regions during hibernation
117 * and to restore them during the subsequent resume. Windows does that also for
118 * suspend to RAM. However, it is known that this mechanism does not work on
119 * all machines, so we allow the user to disable it with the help of the
120 * 'acpi_sleep=nonvs' kernel command line option.
121 */
122static bool nvs_nosave;
123
124void __init acpi_nvs_nosave(void)
125{
126 nvs_nosave = true;
127}
128
129/*
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200130 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
131 * user to request that behavior by using the 'acpi_old_suspend_ordering'
132 * kernel command line option that causes the following variable to be set.
133 */
134static bool old_suspend_ordering;
135
136void __init acpi_old_suspend_ordering(void)
137{
138 old_suspend_ordering = true;
139}
140
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700141static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
142{
Zhang Ruidc230682012-11-30 12:57:03 +0100143 acpi_old_suspend_ordering();
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700144 return 0;
145}
146
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400147static int __init init_nvs_nosave(const struct dmi_system_id *d)
148{
149 acpi_nvs_nosave();
150 return 0;
151}
152
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700153static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
154 {
155 .callback = init_old_suspend_ordering,
156 .ident = "Abit KN9 (nForce4 variant)",
157 .matches = {
158 DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
159 DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
160 },
161 },
Rafael J. Wysocki4fb507b2008-10-14 22:54:06 +0200162 {
163 .callback = init_old_suspend_ordering,
164 .ident = "HP xw4600 Workstation",
165 .matches = {
166 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
167 DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
168 },
169 },
Rafael J. Wysocki65df7842008-11-26 17:53:13 -0500170 {
Andy Whitcrofta1404492009-02-11 18:11:22 +0000171 .callback = init_old_suspend_ordering,
172 .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
173 .matches = {
174 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
175 DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
176 },
177 },
Zhang Rui45e77982009-03-15 22:13:44 -0400178 {
Zhao Yakui2a9ef8e2009-03-18 16:36:25 +0800179 .callback = init_old_suspend_ordering,
180 .ident = "Panasonic CF51-2L",
181 .matches = {
182 DMI_MATCH(DMI_BOARD_VENDOR,
183 "Matsushita Electric Industrial Co.,Ltd."),
184 DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
185 },
186 },
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400187 {
188 .callback = init_nvs_nosave,
Dave Jonesd11c78e2011-10-19 23:15:11 +0200189 .ident = "Sony Vaio VGN-FW21E",
190 .matches = {
191 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
192 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
193 },
194 },
195 {
196 .callback = init_nvs_nosave,
Dave Jonesddf6ce42011-11-03 00:58:59 +0100197 .ident = "Sony Vaio VPCEB17FX",
198 .matches = {
199 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
200 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
201 },
202 },
203 {
204 .callback = init_nvs_nosave,
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400205 .ident = "Sony Vaio VGN-SR11M",
206 .matches = {
207 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
208 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
209 },
210 },
211 {
212 .callback = init_nvs_nosave,
213 .ident = "Everex StepNote Series",
214 .matches = {
215 DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
216 DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
217 },
218 },
Rafael J. Wysockiaf489312010-10-17 21:01:21 +0200219 {
220 .callback = init_nvs_nosave,
221 .ident = "Sony Vaio VPCEB1Z1E",
222 .matches = {
223 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
224 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
225 },
226 },
Rafael J. Wysocki291a73c2010-12-12 21:10:42 +0100227 {
228 .callback = init_nvs_nosave,
229 .ident = "Sony Vaio VGN-NW130D",
230 .matches = {
231 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
232 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
233 },
234 },
Rafael J. Wysocki7b330702011-01-06 23:37:01 +0100235 {
236 .callback = init_nvs_nosave,
Lan Tianyu93f77082012-01-21 09:23:56 +0800237 .ident = "Sony Vaio VPCCW29FX",
238 .matches = {
239 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
240 DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
241 },
242 },
243 {
244 .callback = init_nvs_nosave,
Rafael J. Wysocki7b330702011-01-06 23:37:01 +0100245 .ident = "Averatec AV1020-ED2",
246 .matches = {
247 DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
248 DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
249 },
250 },
Zhang Ruibb0c5ed2011-07-30 01:40:48 -0400251 {
252 .callback = init_old_suspend_ordering,
253 .ident = "Asus A8N-SLI DELUXE",
254 .matches = {
255 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
256 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
257 },
258 },
259 {
260 .callback = init_old_suspend_ordering,
261 .ident = "Asus A8N-SLI Premium",
262 .matches = {
263 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
264 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
265 },
266 },
Rafael J. Wysocki89e8ea12011-10-06 20:35:03 +0200267 {
268 .callback = init_nvs_nosave,
269 .ident = "Sony Vaio VGN-SR26GN_P",
270 .matches = {
271 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
272 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
273 },
274 },
Bogdan Radulescu731b25a2011-10-06 20:35:12 +0200275 {
276 .callback = init_nvs_nosave,
Lan Tianyub54b1f32012-11-21 23:12:12 +0100277 .ident = "Sony Vaio VPCEB1S1E",
278 .matches = {
279 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
280 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
281 },
282 },
283 {
284 .callback = init_nvs_nosave,
Bogdan Radulescu731b25a2011-10-06 20:35:12 +0200285 .ident = "Sony Vaio VGN-FW520F",
286 .matches = {
287 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
288 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
289 },
290 },
Keng-Yu Lin5a50a7c2011-12-02 00:04:23 +0100291 {
292 .callback = init_nvs_nosave,
293 .ident = "Asus K54C",
294 .matches = {
295 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
296 DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
297 },
298 },
299 {
300 .callback = init_nvs_nosave,
301 .ident = "Asus K54HR",
302 .matches = {
303 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
304 DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
305 },
306 },
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700307 {},
308};
Zhang Ruidc230682012-11-30 12:57:03 +0100309
310static void acpi_sleep_dmi_check(void)
311{
312 dmi_check_system(acpisleep_dmi_table);
313}
314
315/**
316 * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
317 */
318static int acpi_pm_freeze(void)
319{
320 acpi_disable_all_gpes();
321 acpi_os_wait_events_complete(NULL);
322 acpi_ec_block_transactions();
323 return 0;
324}
325
326/**
327 * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
328 */
329static int acpi_pm_pre_suspend(void)
330{
331 acpi_pm_freeze();
332 return suspend_nvs_save();
333}
334
335/**
336 * __acpi_pm_prepare - Prepare the platform to enter the target state.
337 *
338 * If necessary, set the firmware waking vector and do arch-specific
339 * nastiness to get the wakeup code to the waking vector.
340 */
341static int __acpi_pm_prepare(void)
342{
343 int error = acpi_sleep_prepare(acpi_target_sleep_state);
344 if (error)
345 acpi_target_sleep_state = ACPI_STATE_S0;
346
347 return error;
348}
349
350/**
351 * acpi_pm_prepare - Prepare the platform to enter the target sleep
352 * state and disable the GPEs.
353 */
354static int acpi_pm_prepare(void)
355{
356 int error = __acpi_pm_prepare();
357 if (!error)
358 error = acpi_pm_pre_suspend();
359
360 return error;
361}
362
363/**
364 * acpi_pm_finish - Instruct the platform to leave a sleep state.
365 *
366 * This is called after we wake back up (or if entering the sleep state
367 * failed).
368 */
369static void acpi_pm_finish(void)
370{
371 u32 acpi_state = acpi_target_sleep_state;
372
373 acpi_ec_unblock_transactions();
374 suspend_nvs_free();
375
376 if (acpi_state == ACPI_STATE_S0)
377 return;
378
379 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
380 acpi_state);
381 acpi_disable_wakeup_devices(acpi_state);
382 acpi_leave_sleep_state(acpi_state);
383
384 /* reset firmware waking vector */
385 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
386
387 acpi_target_sleep_state = ACPI_STATE_S0;
388}
389
390/**
391 * acpi_pm_end - Finish up suspend sequence.
392 */
393static void acpi_pm_end(void)
394{
395 /*
396 * This is necessary in case acpi_pm_finish() is not called during a
397 * failing transition to a sleep state.
398 */
399 acpi_target_sleep_state = ACPI_STATE_S0;
400 acpi_sleep_tts_switch(acpi_target_sleep_state);
401}
402#else /* !CONFIG_ACPI_SLEEP */
403#define acpi_target_sleep_state ACPI_STATE_S0
404static inline void acpi_sleep_dmi_check(void) {}
405#endif /* CONFIG_ACPI_SLEEP */
406
407#ifdef CONFIG_SUSPEND
408static u32 acpi_suspend_states[] = {
409 [PM_SUSPEND_ON] = ACPI_STATE_S0,
410 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
411 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
412 [PM_SUSPEND_MAX] = ACPI_STATE_S5
413};
414
415/**
416 * acpi_suspend_begin - Set the target system sleep state to the state
417 * associated with given @pm_state, if supported.
418 */
419static int acpi_suspend_begin(suspend_state_t pm_state)
420{
421 u32 acpi_state = acpi_suspend_states[pm_state];
422 int error = 0;
423
424 error = nvs_nosave ? 0 : suspend_nvs_alloc();
425 if (error)
426 return error;
427
428 if (sleep_states[acpi_state]) {
429 acpi_target_sleep_state = acpi_state;
430 acpi_sleep_tts_switch(acpi_target_sleep_state);
431 } else {
432 printk(KERN_ERR "ACPI does not support this state: %d\n",
433 pm_state);
434 error = -ENOSYS;
435 }
436 return error;
437}
438
439/**
440 * acpi_suspend_enter - Actually enter a sleep state.
441 * @pm_state: ignored
442 *
443 * Flush caches and go to sleep. For STR we have to call arch-specific
444 * assembly, which in turn call acpi_enter_sleep_state().
445 * It's unfortunate, but it works. Please fix if you're feeling frisky.
446 */
447static int acpi_suspend_enter(suspend_state_t pm_state)
448{
449 acpi_status status = AE_OK;
450 u32 acpi_state = acpi_target_sleep_state;
451 int error;
452
453 ACPI_FLUSH_CPU_CACHE();
454
455 switch (acpi_state) {
456 case ACPI_STATE_S1:
457 barrier();
458 status = acpi_enter_sleep_state(acpi_state, wake_sleep_flags);
459 break;
460
461 case ACPI_STATE_S3:
462 error = acpi_suspend_lowlevel();
463 if (error)
464 return error;
465 pr_info(PREFIX "Low-level resume complete\n");
466 break;
467 }
468
469 /* This violates the spec but is required for bug compatibility. */
470 acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
471
472 /* Reprogram control registers and execute _BFS */
473 acpi_leave_sleep_state_prep(acpi_state, wake_sleep_flags);
474
475 /* ACPI 3.0 specs (P62) says that it's the responsibility
476 * of the OSPM to clear the status bit [ implying that the
477 * POWER_BUTTON event should not reach userspace ]
478 */
479 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
480 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
481
482 /*
483 * Disable and clear GPE status before interrupt is enabled. Some GPEs
484 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
485 * acpi_leave_sleep_state will reenable specific GPEs later
486 */
487 acpi_disable_all_gpes();
488 /* Allow EC transactions to happen. */
489 acpi_ec_unblock_transactions_early();
490
491 suspend_nvs_restore();
492
493 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
494}
495
496static int acpi_suspend_state_valid(suspend_state_t pm_state)
497{
498 u32 acpi_state;
499
500 switch (pm_state) {
501 case PM_SUSPEND_ON:
502 case PM_SUSPEND_STANDBY:
503 case PM_SUSPEND_MEM:
504 acpi_state = acpi_suspend_states[pm_state];
505
506 return sleep_states[acpi_state];
507 default:
508 return 0;
509 }
510}
511
512static const struct platform_suspend_ops acpi_suspend_ops = {
513 .valid = acpi_suspend_state_valid,
514 .begin = acpi_suspend_begin,
515 .prepare_late = acpi_pm_prepare,
516 .enter = acpi_suspend_enter,
517 .wake = acpi_pm_finish,
518 .end = acpi_pm_end,
519};
520
521/**
522 * acpi_suspend_begin_old - Set the target system sleep state to the
523 * state associated with given @pm_state, if supported, and
524 * execute the _PTS control method. This function is used if the
525 * pre-ACPI 2.0 suspend ordering has been requested.
526 */
527static int acpi_suspend_begin_old(suspend_state_t pm_state)
528{
529 int error = acpi_suspend_begin(pm_state);
530 if (!error)
531 error = __acpi_pm_prepare();
532
533 return error;
534}
535
536/*
537 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
538 * been requested.
539 */
540static const struct platform_suspend_ops acpi_suspend_ops_old = {
541 .valid = acpi_suspend_state_valid,
542 .begin = acpi_suspend_begin_old,
543 .prepare_late = acpi_pm_pre_suspend,
544 .enter = acpi_suspend_enter,
545 .wake = acpi_pm_finish,
546 .end = acpi_pm_end,
547 .recover = acpi_pm_finish,
548};
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200549#endif /* CONFIG_SUSPEND */
550
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200551#ifdef CONFIG_HIBERNATION
Shaohua Libdfe6b72008-07-23 21:28:41 -0700552static unsigned long s4_hardware_signature;
553static struct acpi_table_facs *facs;
554static bool nosigcheck;
555
556void __init acpi_no_s4_hw_signature(void)
557{
558 nosigcheck = true;
559}
560
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100561static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700562{
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100563 int error;
564
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200565 error = nvs_nosave ? 0 : suspend_nvs_alloc();
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100566 if (!error) {
567 acpi_target_sleep_state = ACPI_STATE_S4;
568 acpi_sleep_tts_switch(acpi_target_sleep_state);
569 }
570
571 return error;
572}
573
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700574static int acpi_hibernation_enter(void)
575{
576 acpi_status status = AE_OK;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700577
578 ACPI_FLUSH_CPU_CACHE();
579
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700580 /* This shouldn't return. If it returns, we have a problem */
Konrad Rzeszutek Wilk2a14e542012-04-22 23:03:17 -0400581 status = acpi_enter_sleep_state(ACPI_STATE_S4, wake_sleep_flags);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100582 /* Reprogram control registers and execute _BFS */
Konrad Rzeszutek Wilk2a14e542012-04-22 23:03:17 -0400583 acpi_leave_sleep_state_prep(ACPI_STATE_S4, wake_sleep_flags);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700584
585 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
586}
587
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700588static void acpi_hibernation_leave(void)
589{
590 /*
591 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
592 * enable it here.
593 */
594 acpi_enable();
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100595 /* Reprogram control registers and execute _BFS */
Konrad Rzeszutek Wilk2a14e542012-04-22 23:03:17 -0400596 acpi_leave_sleep_state_prep(ACPI_STATE_S4, wake_sleep_flags);
Shaohua Libdfe6b72008-07-23 21:28:41 -0700597 /* Check the hardware signature */
598 if (facs && s4_hardware_signature != facs->hardware_signature) {
599 printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
600 "cannot resume!\n");
601 panic("ACPI S4 hardware signature mismatch");
602 }
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100603 /* Restore the NVS memory area */
Matthew Garrettdd4c4f12010-05-28 16:32:14 -0400604 suspend_nvs_restore();
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200605 /* Allow EC transactions to happen. */
Rafael J. Wysockife955682010-04-09 01:40:38 +0200606 acpi_ec_unblock_transactions_early();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700607}
608
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200609static void acpi_pm_thaw(void)
Rafael J. Wysockif6bb13a2010-03-04 01:52:58 +0100610{
Rafael J. Wysockife955682010-04-09 01:40:38 +0200611 acpi_ec_unblock_transactions();
Lin Ming3d97e422008-12-16 16:57:46 +0800612 acpi_enable_all_runtime_gpes();
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700613}
614
Lionel Debroux073ef1f2010-11-09 21:48:49 +0100615static const struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100616 .begin = acpi_hibernation_begin,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200617 .end = acpi_pm_end,
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200618 .pre_snapshot = acpi_pm_prepare,
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400619 .finish = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200620 .prepare = acpi_pm_prepare,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700621 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700622 .leave = acpi_hibernation_leave,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200623 .pre_restore = acpi_pm_freeze,
624 .restore_cleanup = acpi_pm_thaw,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700625};
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200626
627/**
628 * acpi_hibernation_begin_old - Set the target system sleep state to
629 * ACPI_STATE_S4 and execute the _PTS control method. This
630 * function is used if the pre-ACPI 2.0 suspend ordering has been
631 * requested.
632 */
633static int acpi_hibernation_begin_old(void)
634{
Zhao Yakuie49f7112008-08-12 10:20:22 +0800635 int error;
636 /*
637 * The _TTS object should always be evaluated before the _PTS object.
638 * When the old_suspended_ordering is true, the _PTS object is
639 * evaluated in the acpi_sleep_prepare.
640 */
641 acpi_sleep_tts_switch(ACPI_STATE_S4);
642
643 error = acpi_sleep_prepare(ACPI_STATE_S4);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200644
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100645 if (!error) {
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200646 if (!nvs_nosave)
Matthew Garrettdd4c4f12010-05-28 16:32:14 -0400647 error = suspend_nvs_alloc();
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100648 if (!error)
649 acpi_target_sleep_state = ACPI_STATE_S4;
650 }
651 return error;
652}
653
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200654/*
655 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
656 * been requested.
657 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +0100658static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200659 .begin = acpi_hibernation_begin_old,
660 .end = acpi_pm_end,
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200661 .pre_snapshot = acpi_pm_pre_suspend,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200662 .prepare = acpi_pm_freeze,
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400663 .finish = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200664 .enter = acpi_hibernation_enter,
665 .leave = acpi_hibernation_leave,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200666 .pre_restore = acpi_pm_freeze,
667 .restore_cleanup = acpi_pm_thaw,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200668 .recover = acpi_pm_finish,
669};
670#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700671
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200672int acpi_suspend(u32 acpi_state)
673{
674 suspend_state_t states[] = {
675 [1] = PM_SUSPEND_STANDBY,
676 [3] = PM_SUSPEND_MEM,
677 [5] = PM_SUSPEND_MAX
678 };
679
680 if (acpi_state < 6 && states[acpi_state])
681 return pm_suspend(states[acpi_state]);
682 if (acpi_state == 4)
683 return hibernate();
684 return -EINVAL;
685}
686
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100687#ifdef CONFIG_PM
Shaohua Lifd4aff12007-07-17 22:40:25 +0200688/**
689 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
690 * in the system sleep state given by %acpi_target_sleep_state
David Brownell2fe2de52008-06-05 01:15:40 +0200691 * @dev: device to examine; its driver model wakeup flags control
692 * whether it should be able to wake up the system
Shaohua Lifd4aff12007-07-17 22:40:25 +0200693 * @d_min_p: used to store the upper limit of allowed states range
694 * Return value: preferred power state of the device on success, -ENODEV on
695 * failure (ie. if there's no 'struct acpi_device' for @dev)
696 *
697 * Find the lowest power (highest number) ACPI device power state that
698 * device @dev can be in while the system is in the sleep state represented
699 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
700 * able to wake up the system from this sleep state. If @d_min_p is set,
701 * the highest power (lowest number) device power state of @dev allowed
702 * in this system sleep state is stored at the location pointed to by it.
703 *
704 * The caller must ensure that @dev is valid before using this function.
705 * The caller is also responsible for figuring out if the device is
706 * supposed to be able to wake up the system and passing this information
707 * via @wake.
708 */
709
David Brownell2fe2de52008-06-05 01:15:40 +0200710int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
Shaohua Lifd4aff12007-07-17 22:40:25 +0200711{
712 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
713 struct acpi_device *adev;
714 char acpi_method[] = "_SxD";
Matthew Wilcox27663c52008-10-10 02:22:59 -0400715 unsigned long long d_min, d_max;
Shaohua Lifd4aff12007-07-17 22:40:25 +0200716
717 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800718 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200719 return -ENODEV;
720 }
721
722 acpi_method[2] = '0' + acpi_target_sleep_state;
723 /*
724 * If the sleep state is S0, we will return D3, but if the device has
725 * _S0W, we will use the value from _S0W
726 */
727 d_min = ACPI_STATE_D0;
728 d_max = ACPI_STATE_D3;
729
730 /*
731 * If present, _SxD methods return the minimum D-state (highest power
732 * state) we can use for the corresponding S-states. Otherwise, the
733 * minimum D-state is D0 (ACPI 3.x).
734 *
735 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
736 * provided -- that's our fault recovery, we ignore retval.
737 */
738 if (acpi_target_sleep_state > ACPI_STATE_S0)
739 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
740
741 /*
742 * If _PRW says we can wake up the system from the target sleep state,
743 * the D-state returned by _SxD is sufficient for that (we assume a
744 * wakeup-aware driver if wake is set). Still, if _SxW exists
745 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
746 * can wake the system. _S0W may be valid, too.
747 */
748 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
Rafael J. Wysocki7c2c0262012-05-29 21:21:07 +0200749 (device_may_wakeup(dev) && adev->wakeup.flags.valid &&
750 adev->wakeup.sleep_state >= acpi_target_sleep_state)) {
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100751 acpi_status status;
752
Shaohua Lifd4aff12007-07-17 22:40:25 +0200753 acpi_method[3] = 'W';
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100754 status = acpi_evaluate_integer(handle, acpi_method, NULL,
755 &d_max);
756 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200757 if (acpi_target_sleep_state != ACPI_STATE_S0 ||
758 status != AE_NOT_FOUND)
759 d_max = d_min;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100760 } else if (d_max < d_min) {
761 /* Warn the user of the broken DSDT */
762 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
763 acpi_method);
764 /* Sanitize it */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200765 d_min = d_max;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100766 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200767 }
768
769 if (d_min_p)
770 *d_min_p = d_min;
771 return d_max;
772}
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100773#endif /* CONFIG_PM */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200774
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200775#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200776/**
Lin Mingb24e5092012-03-27 15:43:25 +0800777 * acpi_pm_device_run_wake - Enable/disable wake-up for given device.
778 * @phys_dev: Device to enable/disable the platform to wake-up the system for.
779 * @enable: Whether enable or disable the wake-up functionality.
780 *
781 * Find the ACPI device object corresponding to @pci_dev and try to
782 * enable/disable the GPE associated with it.
783 */
784int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
785{
786 struct acpi_device *dev;
787 acpi_handle handle;
788
789 if (!device_run_wake(phys_dev))
790 return -EINVAL;
791
792 handle = DEVICE_ACPI_HANDLE(phys_dev);
793 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
794 dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
795 __func__);
796 return -ENODEV;
797 }
798
799 if (enable) {
800 acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
801 acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
802 } else {
803 acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
804 acpi_disable_wakeup_device_power(dev);
805 }
806
807 return 0;
808}
809
810/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200811 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
812 * capability of given device
813 * @dev: device to handle
814 * @enable: 'true' - enable, 'false' - disable the wake-up capability
815 */
816int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
817{
818 acpi_handle handle;
819 struct acpi_device *adev;
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200820 int error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200821
Rafael J. Wysocki0baed8d2009-09-08 23:16:24 +0200822 if (!device_can_wakeup(dev))
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200823 return -EINVAL;
824
825 handle = DEVICE_ACPI_HANDLE(dev);
826 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200827 dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200828 return -ENODEV;
829 }
830
Rafael J. Wysockie8b6f972010-06-25 01:18:39 +0200831 error = enable ?
832 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
833 acpi_disable_wakeup_device_power(adev);
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200834 if (!error)
835 dev_info(dev, "wake-up capability %s by ACPI\n",
836 enable ? "enabled" : "disabled");
837
838 return error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200839}
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200840#endif /* CONFIG_PM_SLEEP */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200841
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400842static void acpi_power_off_prepare(void)
843{
844 /* Prepare to power off the system */
845 acpi_sleep_prepare(ACPI_STATE_S5);
Lin Ming3d97e422008-12-16 16:57:46 +0800846 acpi_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400847}
848
849static void acpi_power_off(void)
850{
851 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
Frank Seidel4d939152009-02-04 17:03:07 +0100852 printk(KERN_DEBUG "%s called\n", __func__);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400853 local_irq_disable();
Konrad Rzeszutek Wilk2a14e542012-04-22 23:03:17 -0400854 acpi_enter_sleep_state(ACPI_STATE_S5, wake_sleep_flags);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400855}
856
Len Brown96f15ef2009-04-17 23:32:20 -0400857/*
858 * ACPI 2.0 created the optional _GTS and _BFS,
859 * but industry adoption has been neither rapid nor broad.
860 *
861 * Linux gets into trouble when it executes poorly validated
862 * paths through the BIOS, so disable _GTS and _BFS by default,
863 * but do speak up and offer the option to enable them.
864 */
Stephen Hemminger01eac602010-10-18 18:47:25 -0700865static void __init acpi_gts_bfs_check(void)
Len Brown96f15ef2009-04-17 23:32:20 -0400866{
867 acpi_handle dummy;
868
Bob Moore4efeeec2012-03-21 09:42:45 +0800869 if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__GTS, &dummy)))
Len Brown96f15ef2009-04-17 23:32:20 -0400870 {
871 printk(KERN_NOTICE PREFIX "BIOS offers _GTS\n");
872 printk(KERN_NOTICE PREFIX "If \"acpi.gts=1\" improves suspend, "
873 "please notify linux-acpi@vger.kernel.org\n");
874 }
Bob Moore4efeeec2012-03-21 09:42:45 +0800875 if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__BFS, &dummy)))
Len Brown96f15ef2009-04-17 23:32:20 -0400876 {
877 printk(KERN_NOTICE PREFIX "BIOS offers _BFS\n");
878 printk(KERN_NOTICE PREFIX "If \"acpi.bfs=1\" improves resume, "
879 "please notify linux-acpi@vger.kernel.org\n");
880 }
881}
882
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500883int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200885 acpi_status status;
886 u8 type_a, type_b;
887#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500888 int i = 0;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200889#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (acpi_disabled)
892 return 0;
893
Zhang Ruidc230682012-11-30 12:57:03 +0100894 acpi_sleep_dmi_check();
895
Frans Pop5a50fe72007-09-20 22:27:44 +0200896 sleep_states[ACPI_STATE_S0] = 1;
897 printk(KERN_INFO PREFIX "(supports S0");
898
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200899#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200900 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
902 if (ACPI_SUCCESS(status)) {
903 sleep_states[i] = 1;
904 printk(" S%d", i);
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200908 suspend_set_ops(old_suspend_ordering ?
909 &acpi_suspend_ops_old : &acpi_suspend_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200910#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700911
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200912#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200913 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
914 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200915 hibernation_set_ops(old_suspend_ordering ?
916 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200917 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400918 printk(" S4");
Shaohua Libdfe6b72008-07-23 21:28:41 -0700919 if (!nosigcheck) {
Lin Ming3d97e422008-12-16 16:57:46 +0800920 acpi_get_table(ACPI_SIG_FACS, 1,
Shaohua Libdfe6b72008-07-23 21:28:41 -0700921 (struct acpi_table_header **)&facs);
922 if (facs)
923 s4_hardware_signature =
924 facs->hardware_signature;
925 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200926 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700927#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400928 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
929 if (ACPI_SUCCESS(status)) {
930 sleep_states[ACPI_STATE_S5] = 1;
931 printk(" S5");
932 pm_power_off_prepare = acpi_power_off_prepare;
933 pm_power_off = acpi_power_off;
934 }
935 printk(")\n");
Zhao Yakuie49f7112008-08-12 10:20:22 +0800936 /*
937 * Register the tts_notifier to reboot notifier list so that the _TTS
938 * object can also be evaluated when the system enters S5.
939 */
940 register_reboot_notifier(&tts_notifier);
Len Brown96f15ef2009-04-17 23:32:20 -0400941 acpi_gts_bfs_check();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
943}