blob: 6efef87b405ccbc0c4d74668a4d77aecde5ca8d4 [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
Stephen Hemminger01eac602010-10-18 18:47:25 -070031static u8 sleep_states[ACPI_S_STATE_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Zhao Yakuie49f7112008-08-12 10:20:22 +080033static void acpi_sleep_tts_switch(u32 acpi_state)
34{
35 union acpi_object in_arg = { ACPI_TYPE_INTEGER };
36 struct acpi_object_list arg_list = { 1, &in_arg };
37 acpi_status status = AE_OK;
38
39 in_arg.integer.value = acpi_state;
40 status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
41 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
42 /*
43 * OS can't evaluate the _TTS object correctly. Some warning
44 * message will be printed. But it won't break anything.
45 */
46 printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
47 }
48}
49
50static int tts_notify_reboot(struct notifier_block *this,
51 unsigned long code, void *x)
52{
53 acpi_sleep_tts_switch(ACPI_STATE_S5);
54 return NOTIFY_DONE;
55}
56
57static struct notifier_block tts_notifier = {
58 .notifier_call = tts_notify_reboot,
59 .next = NULL,
60 .priority = 0,
61};
62
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010063static int acpi_sleep_prepare(u32 acpi_state)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020064{
65#ifdef CONFIG_ACPI_SLEEP
66 /* do we have a wakeup address for S2 and S3? */
67 if (acpi_state == ACPI_STATE_S3) {
H. Peter Anvin319b6ff2012-05-30 12:33:41 +030068 if (!acpi_wakeup_address)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020069 return -EFAULT;
H. Peter Anvin319b6ff2012-05-30 12:33:41 +030070 acpi_set_firmware_waking_vector(acpi_wakeup_address);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020071
72 }
73 ACPI_FLUSH_CPU_CACHE();
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020074#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010075 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
76 acpi_state);
Rafael J. Wysocki78f5f022010-07-06 22:09:38 -040077 acpi_enable_wakeup_devices(acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020078 acpi_enter_sleep_state_prep(acpi_state);
79 return 0;
80}
81
Rafael J. Wysocki5d1e0722008-10-22 14:58:43 -040082#ifdef CONFIG_ACPI_SLEEP
Jan Beulich091aad62010-12-07 14:52:25 +000083static u32 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockia5ca7342012-07-23 21:01:02 +020084static bool pwr_btn_event_pending;
Jan Beulich091aad62010-12-07 14:52:25 +000085
Zhang Ruid7f0eea2009-12-30 15:36:42 +080086/*
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +020087 * The ACPI specification wants us to save NVS memory regions during hibernation
88 * and to restore them during the subsequent resume. Windows does that also for
89 * suspend to RAM. However, it is known that this mechanism does not work on
90 * all machines, so we allow the user to disable it with the help of the
91 * 'acpi_sleep=nonvs' kernel command line option.
92 */
93static bool nvs_nosave;
94
95void __init acpi_nvs_nosave(void)
96{
97 nvs_nosave = true;
98}
99
100/*
Kristen Carlson Accardi1bad2f12012-10-26 13:39:15 +0200101 * The ACPI specification wants us to save NVS memory regions during hibernation
102 * but says nothing about saving NVS during S3. Not all versions of Windows
103 * save NVS on S3 suspend either, and it is clear that not all systems need
104 * NVS to be saved at S3 time. To improve suspend/resume time, allow the
105 * user to disable saving NVS on S3 if their system does not require it, but
106 * continue to save/restore NVS for S4 as specified.
107 */
108static bool nvs_nosave_s3;
109
110void __init acpi_nvs_nosave_s3(void)
111{
112 nvs_nosave_s3 = true;
113}
114
115/*
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200116 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
117 * user to request that behavior by using the 'acpi_old_suspend_ordering'
118 * kernel command line option that causes the following variable to be set.
119 */
120static bool old_suspend_ordering;
121
122void __init acpi_old_suspend_ordering(void)
123{
124 old_suspend_ordering = true;
125}
126
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700127static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
128{
Zhang Rui0ac1b1d2012-11-30 12:57:03 +0100129 acpi_old_suspend_ordering();
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700130 return 0;
131}
132
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400133static int __init init_nvs_nosave(const struct dmi_system_id *d)
134{
135 acpi_nvs_nosave();
136 return 0;
137}
138
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700139static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
140 {
141 .callback = init_old_suspend_ordering,
142 .ident = "Abit KN9 (nForce4 variant)",
143 .matches = {
144 DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
145 DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
146 },
147 },
Rafael J. Wysocki4fb507b2008-10-14 22:54:06 +0200148 {
149 .callback = init_old_suspend_ordering,
150 .ident = "HP xw4600 Workstation",
151 .matches = {
152 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
153 DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
154 },
155 },
Rafael J. Wysocki65df7842008-11-26 17:53:13 -0500156 {
Andy Whitcrofta1404492009-02-11 18:11:22 +0000157 .callback = init_old_suspend_ordering,
158 .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
159 .matches = {
160 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
161 DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
162 },
163 },
Zhang Rui45e77982009-03-15 22:13:44 -0400164 {
Zhao Yakui2a9ef8e2009-03-18 16:36:25 +0800165 .callback = init_old_suspend_ordering,
166 .ident = "Panasonic CF51-2L",
167 .matches = {
168 DMI_MATCH(DMI_BOARD_VENDOR,
169 "Matsushita Electric Industrial Co.,Ltd."),
170 DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
171 },
172 },
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400173 {
174 .callback = init_nvs_nosave,
Dave Jonesd11c78e2011-10-19 23:15:11 +0200175 .ident = "Sony Vaio VGN-FW21E",
176 .matches = {
177 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
178 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
179 },
180 },
181 {
182 .callback = init_nvs_nosave,
Dave Jonesddf6ce42011-11-03 00:58:59 +0100183 .ident = "Sony Vaio VPCEB17FX",
184 .matches = {
185 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
186 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
187 },
188 },
189 {
190 .callback = init_nvs_nosave,
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400191 .ident = "Sony Vaio VGN-SR11M",
192 .matches = {
193 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
194 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
195 },
196 },
197 {
198 .callback = init_nvs_nosave,
199 .ident = "Everex StepNote Series",
200 .matches = {
201 DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
202 DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
203 },
204 },
Rafael J. Wysockiaf489312010-10-17 21:01:21 +0200205 {
206 .callback = init_nvs_nosave,
207 .ident = "Sony Vaio VPCEB1Z1E",
208 .matches = {
209 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
210 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
211 },
212 },
Rafael J. Wysocki291a73c2010-12-12 21:10:42 +0100213 {
214 .callback = init_nvs_nosave,
215 .ident = "Sony Vaio VGN-NW130D",
216 .matches = {
217 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
218 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
219 },
220 },
Rafael J. Wysocki7b330702011-01-06 23:37:01 +0100221 {
222 .callback = init_nvs_nosave,
Lan Tianyu93f77082012-01-21 09:23:56 +0800223 .ident = "Sony Vaio VPCCW29FX",
224 .matches = {
225 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
226 DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
227 },
228 },
229 {
230 .callback = init_nvs_nosave,
Rafael J. Wysocki7b330702011-01-06 23:37:01 +0100231 .ident = "Averatec AV1020-ED2",
232 .matches = {
233 DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
234 DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
235 },
236 },
Zhang Ruibb0c5ed2011-07-30 01:40:48 -0400237 {
238 .callback = init_old_suspend_ordering,
239 .ident = "Asus A8N-SLI DELUXE",
240 .matches = {
241 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
242 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
243 },
244 },
245 {
246 .callback = init_old_suspend_ordering,
247 .ident = "Asus A8N-SLI Premium",
248 .matches = {
249 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
250 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
251 },
252 },
Rafael J. Wysocki89e8ea12011-10-06 20:35:03 +0200253 {
254 .callback = init_nvs_nosave,
255 .ident = "Sony Vaio VGN-SR26GN_P",
256 .matches = {
257 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
258 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
259 },
260 },
Bogdan Radulescu731b25a2011-10-06 20:35:12 +0200261 {
262 .callback = init_nvs_nosave,
Lan Tianyu876ab792012-11-21 23:12:12 +0100263 .ident = "Sony Vaio VPCEB1S1E",
264 .matches = {
265 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
266 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
267 },
268 },
269 {
270 .callback = init_nvs_nosave,
Bogdan Radulescu731b25a2011-10-06 20:35:12 +0200271 .ident = "Sony Vaio VGN-FW520F",
272 .matches = {
273 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
274 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
275 },
276 },
Keng-Yu Lin5a50a7c2011-12-02 00:04:23 +0100277 {
278 .callback = init_nvs_nosave,
279 .ident = "Asus K54C",
280 .matches = {
281 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
282 DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
283 },
284 },
285 {
286 .callback = init_nvs_nosave,
287 .ident = "Asus K54HR",
288 .matches = {
289 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
290 DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
291 },
292 },
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700293 {},
294};
Zhang Rui0ac1b1d2012-11-30 12:57:03 +0100295
296static void acpi_sleep_dmi_check(void)
297{
298 dmi_check_system(acpisleep_dmi_table);
299}
300
301/**
302 * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
303 */
304static int acpi_pm_freeze(void)
305{
306 acpi_disable_all_gpes();
307 acpi_os_wait_events_complete();
308 acpi_ec_block_transactions();
309 return 0;
310}
311
312/**
313 * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
314 */
315static int acpi_pm_pre_suspend(void)
316{
317 acpi_pm_freeze();
318 return suspend_nvs_save();
319}
320
321/**
322 * __acpi_pm_prepare - Prepare the platform to enter the target state.
323 *
324 * If necessary, set the firmware waking vector and do arch-specific
325 * nastiness to get the wakeup code to the waking vector.
326 */
327static int __acpi_pm_prepare(void)
328{
329 int error = acpi_sleep_prepare(acpi_target_sleep_state);
330 if (error)
331 acpi_target_sleep_state = ACPI_STATE_S0;
332
333 return error;
334}
335
336/**
337 * acpi_pm_prepare - Prepare the platform to enter the target sleep
338 * state and disable the GPEs.
339 */
340static int acpi_pm_prepare(void)
341{
342 int error = __acpi_pm_prepare();
343 if (!error)
344 error = acpi_pm_pre_suspend();
345
346 return error;
347}
348
349static int find_powerf_dev(struct device *dev, void *data)
350{
351 struct acpi_device *device = to_acpi_device(dev);
352 const char *hid = acpi_device_hid(device);
353
354 return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
355}
356
357/**
358 * acpi_pm_finish - Instruct the platform to leave a sleep state.
359 *
360 * This is called after we wake back up (or if entering the sleep state
361 * failed).
362 */
363static void acpi_pm_finish(void)
364{
365 struct device *pwr_btn_dev;
366 u32 acpi_state = acpi_target_sleep_state;
367
368 acpi_ec_unblock_transactions();
369 suspend_nvs_free();
370
371 if (acpi_state == ACPI_STATE_S0)
372 return;
373
374 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
375 acpi_state);
376 acpi_disable_wakeup_devices(acpi_state);
377 acpi_leave_sleep_state(acpi_state);
378
379 /* reset firmware waking vector */
380 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
381
382 acpi_target_sleep_state = ACPI_STATE_S0;
383
384 /* If we were woken with the fixed power button, provide a small
385 * hint to userspace in the form of a wakeup event on the fixed power
386 * button device (if it can be found).
387 *
388 * We delay the event generation til now, as the PM layer requires
389 * timekeeping to be running before we generate events. */
390 if (!pwr_btn_event_pending)
391 return;
392
393 pwr_btn_event_pending = false;
394 pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
395 find_powerf_dev);
396 if (pwr_btn_dev) {
397 pm_wakeup_event(pwr_btn_dev, 0);
398 put_device(pwr_btn_dev);
399 }
400}
401
402/**
403 * acpi_pm_end - Finish up suspend sequence.
404 */
405static void acpi_pm_end(void)
406{
407 /*
408 * This is necessary in case acpi_pm_finish() is not called during a
409 * failing transition to a sleep state.
410 */
411 acpi_target_sleep_state = ACPI_STATE_S0;
412 acpi_sleep_tts_switch(acpi_target_sleep_state);
413}
414#else /* !CONFIG_ACPI_SLEEP */
415#define acpi_target_sleep_state ACPI_STATE_S0
416static inline void acpi_sleep_dmi_check(void) {}
417#endif /* CONFIG_ACPI_SLEEP */
418
419#ifdef CONFIG_SUSPEND
420static u32 acpi_suspend_states[] = {
421 [PM_SUSPEND_ON] = ACPI_STATE_S0,
422 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
423 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
424 [PM_SUSPEND_MAX] = ACPI_STATE_S5
425};
426
427/**
428 * acpi_suspend_begin - Set the target system sleep state to the state
429 * associated with given @pm_state, if supported.
430 */
431static int acpi_suspend_begin(suspend_state_t pm_state)
432{
433 u32 acpi_state = acpi_suspend_states[pm_state];
434 int error = 0;
435
436 error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
437 if (error)
438 return error;
439
440 if (sleep_states[acpi_state]) {
441 acpi_target_sleep_state = acpi_state;
442 acpi_sleep_tts_switch(acpi_target_sleep_state);
443 } else {
444 printk(KERN_ERR "ACPI does not support this state: %d\n",
445 pm_state);
446 error = -ENOSYS;
447 }
448 return error;
449}
450
451/**
452 * acpi_suspend_enter - Actually enter a sleep state.
453 * @pm_state: ignored
454 *
455 * Flush caches and go to sleep. For STR we have to call arch-specific
456 * assembly, which in turn call acpi_enter_sleep_state().
457 * It's unfortunate, but it works. Please fix if you're feeling frisky.
458 */
459static int acpi_suspend_enter(suspend_state_t pm_state)
460{
461 acpi_status status = AE_OK;
462 u32 acpi_state = acpi_target_sleep_state;
463 int error;
464
465 ACPI_FLUSH_CPU_CACHE();
466
467 switch (acpi_state) {
468 case ACPI_STATE_S1:
469 barrier();
470 status = acpi_enter_sleep_state(acpi_state);
471 break;
472
473 case ACPI_STATE_S3:
474 error = acpi_suspend_lowlevel();
475 if (error)
476 return error;
477 pr_info(PREFIX "Low-level resume complete\n");
478 break;
479 }
480
481 /* This violates the spec but is required for bug compatibility. */
482 acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
483
484 /* Reprogram control registers */
485 acpi_leave_sleep_state_prep(acpi_state);
486
487 /* ACPI 3.0 specs (P62) says that it's the responsibility
488 * of the OSPM to clear the status bit [ implying that the
489 * POWER_BUTTON event should not reach userspace ]
490 *
491 * However, we do generate a small hint for userspace in the form of
492 * a wakeup event. We flag this condition for now and generate the
493 * event later, as we're currently too early in resume to be able to
494 * generate wakeup events.
495 */
496 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
497 acpi_event_status pwr_btn_status;
498
499 acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
500
501 if (pwr_btn_status & ACPI_EVENT_FLAG_SET) {
502 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
503 /* Flag for later */
504 pwr_btn_event_pending = true;
505 }
506 }
507
508 /*
509 * Disable and clear GPE status before interrupt is enabled. Some GPEs
510 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
511 * acpi_leave_sleep_state will reenable specific GPEs later
512 */
513 acpi_disable_all_gpes();
514 /* Allow EC transactions to happen. */
515 acpi_ec_unblock_transactions_early();
516
517 suspend_nvs_restore();
518
519 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
520}
521
522static int acpi_suspend_state_valid(suspend_state_t pm_state)
523{
524 u32 acpi_state;
525
526 switch (pm_state) {
527 case PM_SUSPEND_ON:
528 case PM_SUSPEND_STANDBY:
529 case PM_SUSPEND_MEM:
530 acpi_state = acpi_suspend_states[pm_state];
531
532 return sleep_states[acpi_state];
533 default:
534 return 0;
535 }
536}
537
538static const struct platform_suspend_ops acpi_suspend_ops = {
539 .valid = acpi_suspend_state_valid,
540 .begin = acpi_suspend_begin,
541 .prepare_late = acpi_pm_prepare,
542 .enter = acpi_suspend_enter,
543 .wake = acpi_pm_finish,
544 .end = acpi_pm_end,
545};
546
547/**
548 * acpi_suspend_begin_old - Set the target system sleep state to the
549 * state associated with given @pm_state, if supported, and
550 * execute the _PTS control method. This function is used if the
551 * pre-ACPI 2.0 suspend ordering has been requested.
552 */
553static int acpi_suspend_begin_old(suspend_state_t pm_state)
554{
555 int error = acpi_suspend_begin(pm_state);
556 if (!error)
557 error = __acpi_pm_prepare();
558
559 return error;
560}
561
562/*
563 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
564 * been requested.
565 */
566static const struct platform_suspend_ops acpi_suspend_ops_old = {
567 .valid = acpi_suspend_state_valid,
568 .begin = acpi_suspend_begin_old,
569 .prepare_late = acpi_pm_pre_suspend,
570 .enter = acpi_suspend_enter,
571 .wake = acpi_pm_finish,
572 .end = acpi_pm_end,
573 .recover = acpi_pm_finish,
574};
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200575#endif /* CONFIG_SUSPEND */
576
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200577#ifdef CONFIG_HIBERNATION
Shaohua Libdfe6b72008-07-23 21:28:41 -0700578static unsigned long s4_hardware_signature;
579static struct acpi_table_facs *facs;
580static bool nosigcheck;
581
582void __init acpi_no_s4_hw_signature(void)
583{
584 nosigcheck = true;
585}
586
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100587static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700588{
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100589 int error;
590
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200591 error = nvs_nosave ? 0 : suspend_nvs_alloc();
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100592 if (!error) {
593 acpi_target_sleep_state = ACPI_STATE_S4;
594 acpi_sleep_tts_switch(acpi_target_sleep_state);
595 }
596
597 return error;
598}
599
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700600static int acpi_hibernation_enter(void)
601{
602 acpi_status status = AE_OK;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700603
604 ACPI_FLUSH_CPU_CACHE();
605
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700606 /* This shouldn't return. If it returns, we have a problem */
Len Brown3f6f49c2012-07-26 20:08:54 -0400607 status = acpi_enter_sleep_state(ACPI_STATE_S4);
608 /* Reprogram control registers */
609 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700610
611 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
612}
613
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700614static void acpi_hibernation_leave(void)
615{
616 /*
617 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
618 * enable it here.
619 */
620 acpi_enable();
Len Brown3f6f49c2012-07-26 20:08:54 -0400621 /* Reprogram control registers */
622 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Shaohua Libdfe6b72008-07-23 21:28:41 -0700623 /* Check the hardware signature */
624 if (facs && s4_hardware_signature != facs->hardware_signature) {
625 printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
626 "cannot resume!\n");
627 panic("ACPI S4 hardware signature mismatch");
628 }
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100629 /* Restore the NVS memory area */
Matthew Garrettdd4c4f12010-05-28 16:32:14 -0400630 suspend_nvs_restore();
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200631 /* Allow EC transactions to happen. */
Rafael J. Wysockife955682010-04-09 01:40:38 +0200632 acpi_ec_unblock_transactions_early();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700633}
634
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200635static void acpi_pm_thaw(void)
Rafael J. Wysockif6bb13a2010-03-04 01:52:58 +0100636{
Rafael J. Wysockife955682010-04-09 01:40:38 +0200637 acpi_ec_unblock_transactions();
Lin Ming3d97e422008-12-16 16:57:46 +0800638 acpi_enable_all_runtime_gpes();
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700639}
640
Lionel Debroux073ef1f2010-11-09 21:48:49 +0100641static const struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100642 .begin = acpi_hibernation_begin,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200643 .end = acpi_pm_end,
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200644 .pre_snapshot = acpi_pm_prepare,
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400645 .finish = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200646 .prepare = acpi_pm_prepare,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700647 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700648 .leave = acpi_hibernation_leave,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200649 .pre_restore = acpi_pm_freeze,
650 .restore_cleanup = acpi_pm_thaw,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700651};
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200652
653/**
654 * acpi_hibernation_begin_old - Set the target system sleep state to
655 * ACPI_STATE_S4 and execute the _PTS control method. This
656 * function is used if the pre-ACPI 2.0 suspend ordering has been
657 * requested.
658 */
659static int acpi_hibernation_begin_old(void)
660{
Zhao Yakuie49f7112008-08-12 10:20:22 +0800661 int error;
662 /*
663 * The _TTS object should always be evaluated before the _PTS object.
664 * When the old_suspended_ordering is true, the _PTS object is
665 * evaluated in the acpi_sleep_prepare.
666 */
667 acpi_sleep_tts_switch(ACPI_STATE_S4);
668
669 error = acpi_sleep_prepare(ACPI_STATE_S4);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200670
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100671 if (!error) {
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200672 if (!nvs_nosave)
Matthew Garrettdd4c4f12010-05-28 16:32:14 -0400673 error = suspend_nvs_alloc();
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100674 if (!error)
675 acpi_target_sleep_state = ACPI_STATE_S4;
676 }
677 return error;
678}
679
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200680/*
681 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
682 * been requested.
683 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +0100684static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200685 .begin = acpi_hibernation_begin_old,
686 .end = acpi_pm_end,
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200687 .pre_snapshot = acpi_pm_pre_suspend,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200688 .prepare = acpi_pm_freeze,
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400689 .finish = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200690 .enter = acpi_hibernation_enter,
691 .leave = acpi_hibernation_leave,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200692 .pre_restore = acpi_pm_freeze,
693 .restore_cleanup = acpi_pm_thaw,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200694 .recover = acpi_pm_finish,
695};
696#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700697
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200698int acpi_suspend(u32 acpi_state)
699{
700 suspend_state_t states[] = {
701 [1] = PM_SUSPEND_STANDBY,
702 [3] = PM_SUSPEND_MEM,
703 [5] = PM_SUSPEND_MAX
704 };
705
706 if (acpi_state < 6 && states[acpi_state])
707 return pm_suspend(states[acpi_state]);
708 if (acpi_state == 4)
709 return hibernate();
710 return -EINVAL;
711}
712
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100713#ifdef CONFIG_PM
Shaohua Lifd4aff12007-07-17 22:40:25 +0200714/**
715 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
716 * in the system sleep state given by %acpi_target_sleep_state
David Brownell2fe2de52008-06-05 01:15:40 +0200717 * @dev: device to examine; its driver model wakeup flags control
718 * whether it should be able to wake up the system
Shaohua Lifd4aff12007-07-17 22:40:25 +0200719 * @d_min_p: used to store the upper limit of allowed states range
Huang Yingee85f542012-06-23 10:23:48 +0800720 * @d_max_in: specify the lowest allowed states
721 * Return value: preferred power state of the device on success, -ENODEV
722 * (ie. if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
Shaohua Lifd4aff12007-07-17 22:40:25 +0200723 *
724 * Find the lowest power (highest number) ACPI device power state that
725 * device @dev can be in while the system is in the sleep state represented
726 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
727 * able to wake up the system from this sleep state. If @d_min_p is set,
728 * the highest power (lowest number) device power state of @dev allowed
729 * in this system sleep state is stored at the location pointed to by it.
730 *
731 * The caller must ensure that @dev is valid before using this function.
732 * The caller is also responsible for figuring out if the device is
733 * supposed to be able to wake up the system and passing this information
734 * via @wake.
735 */
736
Huang Yingee85f542012-06-23 10:23:48 +0800737int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
Shaohua Lifd4aff12007-07-17 22:40:25 +0200738{
739 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
740 struct acpi_device *adev;
741 char acpi_method[] = "_SxD";
Matthew Wilcox27663c52008-10-10 02:22:59 -0400742 unsigned long long d_min, d_max;
Shaohua Lifd4aff12007-07-17 22:40:25 +0200743
Huang Yingee85f542012-06-23 10:23:48 +0800744 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
745 return -EINVAL;
Shaohua Lifd4aff12007-07-17 22:40:25 +0200746 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800747 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200748 return -ENODEV;
749 }
750
751 acpi_method[2] = '0' + acpi_target_sleep_state;
752 /*
Huang Yingee85f542012-06-23 10:23:48 +0800753 * If the sleep state is S0, the lowest limit from ACPI is D3,
754 * but if the device has _S0W, we will use the value from _S0W
755 * as the lowest limit from ACPI. Finally, we will constrain
756 * the lowest limit with the specified one.
Shaohua Lifd4aff12007-07-17 22:40:25 +0200757 */
758 d_min = ACPI_STATE_D0;
759 d_max = ACPI_STATE_D3;
760
761 /*
762 * If present, _SxD methods return the minimum D-state (highest power
763 * state) we can use for the corresponding S-states. Otherwise, the
764 * minimum D-state is D0 (ACPI 3.x).
765 *
766 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
767 * provided -- that's our fault recovery, we ignore retval.
768 */
769 if (acpi_target_sleep_state > ACPI_STATE_S0)
770 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
771
772 /*
773 * If _PRW says we can wake up the system from the target sleep state,
774 * the D-state returned by _SxD is sufficient for that (we assume a
775 * wakeup-aware driver if wake is set). Still, if _SxW exists
776 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
777 * can wake the system. _S0W may be valid, too.
778 */
779 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
Rafael J. Wysockidbe9a2e2012-05-29 21:21:07 +0200780 (device_may_wakeup(dev) && adev->wakeup.flags.valid &&
781 adev->wakeup.sleep_state >= acpi_target_sleep_state)) {
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100782 acpi_status status;
783
Shaohua Lifd4aff12007-07-17 22:40:25 +0200784 acpi_method[3] = 'W';
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100785 status = acpi_evaluate_integer(handle, acpi_method, NULL,
786 &d_max);
787 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200788 if (acpi_target_sleep_state != ACPI_STATE_S0 ||
789 status != AE_NOT_FOUND)
790 d_max = d_min;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100791 } else if (d_max < d_min) {
792 /* Warn the user of the broken DSDT */
793 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
794 acpi_method);
795 /* Sanitize it */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200796 d_min = d_max;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100797 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200798 }
799
Huang Yingee85f542012-06-23 10:23:48 +0800800 if (d_max_in < d_min)
801 return -EINVAL;
Shaohua Lifd4aff12007-07-17 22:40:25 +0200802 if (d_min_p)
803 *d_min_p = d_min;
Huang Yingee85f542012-06-23 10:23:48 +0800804 /* constrain d_max with specified lowest limit (max number) */
805 if (d_max > d_max_in) {
806 for (d_max = d_max_in; d_max > d_min; d_max--) {
807 if (adev->power.states[d_max].flags.valid)
808 break;
809 }
810 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200811 return d_max;
812}
Lin Ming3bd46602012-06-25 16:13:06 +0800813EXPORT_SYMBOL(acpi_pm_device_sleep_state);
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100814#endif /* CONFIG_PM */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200815
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200816#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200817/**
Lin Mingb24e5092012-03-27 15:43:25 +0800818 * acpi_pm_device_run_wake - Enable/disable wake-up for given device.
819 * @phys_dev: Device to enable/disable the platform to wake-up the system for.
820 * @enable: Whether enable or disable the wake-up functionality.
821 *
822 * Find the ACPI device object corresponding to @pci_dev and try to
823 * enable/disable the GPE associated with it.
824 */
825int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
826{
827 struct acpi_device *dev;
828 acpi_handle handle;
829
830 if (!device_run_wake(phys_dev))
831 return -EINVAL;
832
833 handle = DEVICE_ACPI_HANDLE(phys_dev);
834 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
835 dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
836 __func__);
837 return -ENODEV;
838 }
839
840 if (enable) {
841 acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
842 acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
843 } else {
844 acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
845 acpi_disable_wakeup_device_power(dev);
846 }
847
848 return 0;
849}
Lin Ming3bd46602012-06-25 16:13:06 +0800850EXPORT_SYMBOL(acpi_pm_device_run_wake);
Lin Mingb24e5092012-03-27 15:43:25 +0800851
852/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200853 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
854 * capability of given device
855 * @dev: device to handle
856 * @enable: 'true' - enable, 'false' - disable the wake-up capability
857 */
858int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
859{
860 acpi_handle handle;
861 struct acpi_device *adev;
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200862 int error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200863
Rafael J. Wysocki0baed8d2009-09-08 23:16:24 +0200864 if (!device_can_wakeup(dev))
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200865 return -EINVAL;
866
867 handle = DEVICE_ACPI_HANDLE(dev);
868 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200869 dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200870 return -ENODEV;
871 }
872
Rafael J. Wysockie8b6f972010-06-25 01:18:39 +0200873 error = enable ?
874 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
875 acpi_disable_wakeup_device_power(adev);
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200876 if (!error)
877 dev_info(dev, "wake-up capability %s by ACPI\n",
878 enable ? "enabled" : "disabled");
879
880 return error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200881}
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200882#endif /* CONFIG_PM_SLEEP */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200883
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400884static void acpi_power_off_prepare(void)
885{
886 /* Prepare to power off the system */
887 acpi_sleep_prepare(ACPI_STATE_S5);
Lin Ming3d97e422008-12-16 16:57:46 +0800888 acpi_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400889}
890
891static void acpi_power_off(void)
892{
893 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
Frank Seidel4d939152009-02-04 17:03:07 +0100894 printk(KERN_DEBUG "%s called\n", __func__);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400895 local_irq_disable();
Len Brown3f6f49c2012-07-26 20:08:54 -0400896 acpi_enter_sleep_state(ACPI_STATE_S5);
Len Brown96f15ef2009-04-17 23:32:20 -0400897}
898
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500899int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200901 acpi_status status;
902 u8 type_a, type_b;
903#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500904 int i = 0;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200905#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (acpi_disabled)
908 return 0;
909
Zhang Rui0ac1b1d2012-11-30 12:57:03 +0100910 acpi_sleep_dmi_check();
911
Frans Pop5a50fe72007-09-20 22:27:44 +0200912 sleep_states[ACPI_STATE_S0] = 1;
913 printk(KERN_INFO PREFIX "(supports S0");
914
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200915#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200916 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
918 if (ACPI_SUCCESS(status)) {
919 sleep_states[i] = 1;
Kay Sieversbe964472012-05-08 17:24:20 +0200920 printk(KERN_CONT " S%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200924 suspend_set_ops(old_suspend_ordering ?
925 &acpi_suspend_ops_old : &acpi_suspend_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200926#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700927
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200928#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200929 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
930 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200931 hibernation_set_ops(old_suspend_ordering ?
932 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200933 sleep_states[ACPI_STATE_S4] = 1;
Kay Sieversbe964472012-05-08 17:24:20 +0200934 printk(KERN_CONT " S4");
Shaohua Libdfe6b72008-07-23 21:28:41 -0700935 if (!nosigcheck) {
Lin Ming3d97e422008-12-16 16:57:46 +0800936 acpi_get_table(ACPI_SIG_FACS, 1,
Shaohua Libdfe6b72008-07-23 21:28:41 -0700937 (struct acpi_table_header **)&facs);
938 if (facs)
939 s4_hardware_signature =
940 facs->hardware_signature;
941 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200942 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700943#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400944 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
945 if (ACPI_SUCCESS(status)) {
946 sleep_states[ACPI_STATE_S5] = 1;
Kay Sieversbe964472012-05-08 17:24:20 +0200947 printk(KERN_CONT " S5");
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400948 pm_power_off_prepare = acpi_power_off_prepare;
949 pm_power_off = acpi_power_off;
950 }
Kay Sieversbe964472012-05-08 17:24:20 +0200951 printk(KERN_CONT ")\n");
Zhao Yakuie49f7112008-08-12 10:20:22 +0800952 /*
953 * Register the tts_notifier to reboot notifier list so that the _TTS
954 * object can also be evaluated when the system enters S5.
955 */
956 register_reboot_notifier(&tts_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return 0;
958}