]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/sleep.c
ACPI / hotplug / PCI: Fix NULL pointer dereference in cleanup_bridge()
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / sleep.c
CommitLineData
1da177e4
LT
1/*
2 * sleep.c - ACPI sleep support.
3 *
e2a5b420 4 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
1da177e4
LT
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>
e49f711c 18#include <linux/reboot.h>
d1ee4335 19#include <linux/acpi.h>
a2ef5c4f 20#include <linux/module.h>
f216cc37
AS
21
22#include <asm/io.h>
23
1da177e4
LT
24#include <acpi/acpi_bus.h>
25#include <acpi/acpi_drivers.h>
e60cc7a6
BH
26
27#include "internal.h"
1da177e4
LT
28#include "sleep.h"
29
01eac60b 30static u8 sleep_states[ACPI_S_STATE_COUNT];
1da177e4 31
e49f711c
ZY
32static void acpi_sleep_tts_switch(u32 acpi_state)
33{
0db98202 34 acpi_status status;
e49f711c 35
0db98202 36 status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
e49f711c
ZY
37 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
38 /*
39 * OS can't evaluate the _TTS object correctly. Some warning
40 * message will be printed. But it won't break anything.
41 */
42 printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
43 }
44}
45
46static int tts_notify_reboot(struct notifier_block *this,
47 unsigned long code, void *x)
48{
49 acpi_sleep_tts_switch(ACPI_STATE_S5);
50 return NOTIFY_DONE;
51}
52
53static struct notifier_block tts_notifier = {
54 .notifier_call = tts_notify_reboot,
55 .next = NULL,
56 .priority = 0,
57};
58
c9b6c8f6 59static int acpi_sleep_prepare(u32 acpi_state)
2f3f2226
AS
60{
61#ifdef CONFIG_ACPI_SLEEP
62 /* do we have a wakeup address for S2 and S3? */
63 if (acpi_state == ACPI_STATE_S3) {
319b6ffc 64 if (!acpi_wakeup_address)
2f3f2226 65 return -EFAULT;
319b6ffc 66 acpi_set_firmware_waking_vector(acpi_wakeup_address);
2f3f2226
AS
67
68 }
69 ACPI_FLUSH_CPU_CACHE();
2f3f2226 70#endif
c9b6c8f6
RW
71 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
72 acpi_state);
78f5f023 73 acpi_enable_wakeup_devices(acpi_state);
2f3f2226
AS
74 acpi_enter_sleep_state_prep(acpi_state);
75 return 0;
76}
77
5d1e072b 78#ifdef CONFIG_ACPI_SLEEP
091aad6a 79static u32 acpi_target_sleep_state = ACPI_STATE_S0;
a6ae7594
RW
80
81u32 acpi_target_system_state(void)
82{
83 return acpi_target_sleep_state;
84}
85
a5ca7345 86static bool pwr_btn_event_pending;
091aad6a 87
72ad5d77
RW
88/*
89 * The ACPI specification wants us to save NVS memory regions during hibernation
90 * and to restore them during the subsequent resume. Windows does that also for
91 * suspend to RAM. However, it is known that this mechanism does not work on
92 * all machines, so we allow the user to disable it with the help of the
93 * 'acpi_sleep=nonvs' kernel command line option.
94 */
95static bool nvs_nosave;
96
97void __init acpi_nvs_nosave(void)
98{
99 nvs_nosave = true;
100}
101
1bad2f19
KCA
102/*
103 * The ACPI specification wants us to save NVS memory regions during hibernation
104 * but says nothing about saving NVS during S3. Not all versions of Windows
105 * save NVS on S3 suspend either, and it is clear that not all systems need
106 * NVS to be saved at S3 time. To improve suspend/resume time, allow the
107 * user to disable saving NVS on S3 if their system does not require it, but
108 * continue to save/restore NVS for S4 as specified.
109 */
110static bool nvs_nosave_s3;
111
112void __init acpi_nvs_nosave_s3(void)
113{
114 nvs_nosave_s3 = true;
115}
116
d8f3de0d
RW
117/*
118 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
119 * user to request that behavior by using the 'acpi_old_suspend_ordering'
120 * kernel command line option that causes the following variable to be set.
121 */
122static bool old_suspend_ordering;
123
124void __init acpi_old_suspend_ordering(void)
125{
126 old_suspend_ordering = true;
127}
128
0ac1b1d7
ZR
129static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
130{
131 acpi_old_suspend_ordering();
132 return 0;
133}
134
135static int __init init_nvs_nosave(const struct dmi_system_id *d)
136{
137 acpi_nvs_nosave();
138 return 0;
139}
140
141static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
142 {
143 .callback = init_old_suspend_ordering,
144 .ident = "Abit KN9 (nForce4 variant)",
145 .matches = {
146 DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
147 DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
148 },
149 },
150 {
151 .callback = init_old_suspend_ordering,
152 .ident = "HP xw4600 Workstation",
153 .matches = {
154 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
155 DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
156 },
157 },
158 {
159 .callback = init_old_suspend_ordering,
160 .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
161 .matches = {
162 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
163 DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
164 },
165 },
166 {
167 .callback = init_old_suspend_ordering,
168 .ident = "Panasonic CF51-2L",
169 .matches = {
170 DMI_MATCH(DMI_BOARD_VENDOR,
171 "Matsushita Electric Industrial Co.,Ltd."),
172 DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
173 },
174 },
175 {
176 .callback = init_nvs_nosave,
66f2fda9
JS
177 .ident = "Sony Vaio VGN-FW41E_H",
178 .matches = {
179 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
180 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
181 },
182 },
183 {
184 .callback = init_nvs_nosave,
0ac1b1d7
ZR
185 .ident = "Sony Vaio VGN-FW21E",
186 .matches = {
187 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
188 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
189 },
190 },
191 {
192 .callback = init_nvs_nosave,
469dd1c4
FV
193 .ident = "Sony Vaio VGN-FW21M",
194 .matches = {
195 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
196 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
197 },
198 },
199 {
200 .callback = init_nvs_nosave,
0ac1b1d7
ZR
201 .ident = "Sony Vaio VPCEB17FX",
202 .matches = {
203 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
204 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
205 },
206 },
207 {
208 .callback = init_nvs_nosave,
209 .ident = "Sony Vaio VGN-SR11M",
210 .matches = {
211 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
212 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
213 },
214 },
215 {
216 .callback = init_nvs_nosave,
217 .ident = "Everex StepNote Series",
218 .matches = {
219 DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
220 DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
221 },
222 },
223 {
224 .callback = init_nvs_nosave,
225 .ident = "Sony Vaio VPCEB1Z1E",
226 .matches = {
227 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
228 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
229 },
230 },
231 {
232 .callback = init_nvs_nosave,
233 .ident = "Sony Vaio VGN-NW130D",
234 .matches = {
235 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
236 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
237 },
238 },
239 {
240 .callback = init_nvs_nosave,
241 .ident = "Sony Vaio VPCCW29FX",
242 .matches = {
243 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
244 DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
245 },
246 },
247 {
248 .callback = init_nvs_nosave,
249 .ident = "Averatec AV1020-ED2",
250 .matches = {
251 DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
252 DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
253 },
254 },
255 {
256 .callback = init_old_suspend_ordering,
257 .ident = "Asus A8N-SLI DELUXE",
258 .matches = {
259 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
260 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
261 },
262 },
263 {
264 .callback = init_old_suspend_ordering,
265 .ident = "Asus A8N-SLI Premium",
266 .matches = {
267 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
268 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
269 },
270 },
271 {
272 .callback = init_nvs_nosave,
273 .ident = "Sony Vaio VGN-SR26GN_P",
274 .matches = {
275 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
276 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
277 },
278 },
279 {
280 .callback = init_nvs_nosave,
281 .ident = "Sony Vaio VPCEB1S1E",
282 .matches = {
283 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
284 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
285 },
286 },
287 {
288 .callback = init_nvs_nosave,
289 .ident = "Sony Vaio VGN-FW520F",
290 .matches = {
291 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
292 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
293 },
294 },
295 {
296 .callback = init_nvs_nosave,
297 .ident = "Asus K54C",
298 .matches = {
299 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
300 DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
301 },
302 },
303 {
304 .callback = init_nvs_nosave,
305 .ident = "Asus K54HR",
306 .matches = {
307 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
308 DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
309 },
310 },
311 {},
312};
313
314static void acpi_sleep_dmi_check(void)
315{
316 dmi_check_system(acpisleep_dmi_table);
317}
318
d8f3de0d 319/**
d5a64513 320 * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
d8f3de0d 321 */
d5a64513 322static int acpi_pm_freeze(void)
d8f3de0d 323{
3d97e426 324 acpi_disable_all_gpes();
bd6f10a5 325 acpi_os_wait_events_complete();
fe955682 326 acpi_ec_block_transactions();
d8f3de0d
RW
327 return 0;
328}
329
c5f7a1bb
RW
330/**
331 * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
332 */
333static int acpi_pm_pre_suspend(void)
334{
335 acpi_pm_freeze();
26fcaf60 336 return suspend_nvs_save();
c5f7a1bb
RW
337}
338
d8f3de0d
RW
339/**
340 * __acpi_pm_prepare - Prepare the platform to enter the target state.
341 *
342 * If necessary, set the firmware waking vector and do arch-specific
343 * nastiness to get the wakeup code to the waking vector.
344 */
345static int __acpi_pm_prepare(void)
346{
347 int error = acpi_sleep_prepare(acpi_target_sleep_state);
d8f3de0d
RW
348 if (error)
349 acpi_target_sleep_state = ACPI_STATE_S0;
c5f7a1bb 350
d8f3de0d
RW
351 return error;
352}
353
354/**
355 * acpi_pm_prepare - Prepare the platform to enter the target sleep
356 * state and disable the GPEs.
357 */
358static int acpi_pm_prepare(void)
359{
360 int error = __acpi_pm_prepare();
d8f3de0d 361 if (!error)
26fcaf60 362 error = acpi_pm_pre_suspend();
d5a64513 363
d8f3de0d
RW
364 return error;
365}
366
c10d7a13
DD
367static int find_powerf_dev(struct device *dev, void *data)
368{
369 struct acpi_device *device = to_acpi_device(dev);
370 const char *hid = acpi_device_hid(device);
371
372 return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
373}
374
d8f3de0d
RW
375/**
376 * acpi_pm_finish - Instruct the platform to leave a sleep state.
377 *
378 * This is called after we wake back up (or if entering the sleep state
379 * failed).
380 */
381static void acpi_pm_finish(void)
382{
c10d7a13 383 struct device *pwr_btn_dev;
d8f3de0d
RW
384 u32 acpi_state = acpi_target_sleep_state;
385
42de5532 386 acpi_ec_unblock_transactions();
d551d81d 387 suspend_nvs_free();
2a6b6976 388
d8f3de0d
RW
389 if (acpi_state == ACPI_STATE_S0)
390 return;
1da177e4 391
d8f3de0d
RW
392 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
393 acpi_state);
78f5f023 394 acpi_disable_wakeup_devices(acpi_state);
d8f3de0d
RW
395 acpi_leave_sleep_state(acpi_state);
396
397 /* reset firmware waking vector */
398 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
399
400 acpi_target_sleep_state = ACPI_STATE_S0;
c10d7a13 401
781d737c
RW
402 acpi_resume_power_resources();
403
c10d7a13
DD
404 /* If we were woken with the fixed power button, provide a small
405 * hint to userspace in the form of a wakeup event on the fixed power
406 * button device (if it can be found).
407 *
408 * We delay the event generation til now, as the PM layer requires
409 * timekeeping to be running before we generate events. */
410 if (!pwr_btn_event_pending)
411 return;
412
413 pwr_btn_event_pending = false;
414 pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
415 find_powerf_dev);
416 if (pwr_btn_dev) {
417 pm_wakeup_event(pwr_btn_dev, 0);
418 put_device(pwr_btn_dev);
419 }
d8f3de0d
RW
420}
421
422/**
423 * acpi_pm_end - Finish up suspend sequence.
424 */
425static void acpi_pm_end(void)
426{
427 /*
428 * This is necessary in case acpi_pm_finish() is not called during a
429 * failing transition to a sleep state.
430 */
431 acpi_target_sleep_state = ACPI_STATE_S0;
e49f711c 432 acpi_sleep_tts_switch(acpi_target_sleep_state);
d8f3de0d 433}
92daa7b5
RW
434#else /* !CONFIG_ACPI_SLEEP */
435#define acpi_target_sleep_state ACPI_STATE_S0
0ac1b1d7 436static inline void acpi_sleep_dmi_check(void) {}
5d1e072b 437#endif /* CONFIG_ACPI_SLEEP */
1da177e4 438
d8f3de0d 439#ifdef CONFIG_SUSPEND
1da177e4 440static u32 acpi_suspend_states[] = {
e2a5b420
AS
441 [PM_SUSPEND_ON] = ACPI_STATE_S0,
442 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
443 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
e2a5b420 444 [PM_SUSPEND_MAX] = ACPI_STATE_S5
1da177e4
LT
445};
446
e9b3aba8 447/**
2c6e33c3 448 * acpi_suspend_begin - Set the target system sleep state to the state
e9b3aba8
RW
449 * associated with given @pm_state, if supported.
450 */
2c6e33c3 451static int acpi_suspend_begin(suspend_state_t pm_state)
e9b3aba8
RW
452{
453 u32 acpi_state = acpi_suspend_states[pm_state];
454 int error = 0;
455
1bad2f19 456 error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
2a6b6976
MG
457 if (error)
458 return error;
459
e9b3aba8
RW
460 if (sleep_states[acpi_state]) {
461 acpi_target_sleep_state = acpi_state;
e49f711c 462 acpi_sleep_tts_switch(acpi_target_sleep_state);
e9b3aba8
RW
463 } else {
464 printk(KERN_ERR "ACPI does not support this state: %d\n",
465 pm_state);
466 error = -ENOSYS;
467 }
468 return error;
469}
470
1da177e4 471/**
2c6e33c3 472 * acpi_suspend_enter - Actually enter a sleep state.
e9b3aba8 473 * @pm_state: ignored
1da177e4 474 *
50ad147a
RW
475 * Flush caches and go to sleep. For STR we have to call arch-specific
476 * assembly, which in turn call acpi_enter_sleep_state().
1da177e4
LT
477 * It's unfortunate, but it works. Please fix if you're feeling frisky.
478 */
2c6e33c3 479static int acpi_suspend_enter(suspend_state_t pm_state)
1da177e4
LT
480{
481 acpi_status status = AE_OK;
e9b3aba8 482 u32 acpi_state = acpi_target_sleep_state;
979f11b0 483 int error;
1da177e4
LT
484
485 ACPI_FLUSH_CPU_CACHE();
486
e9b3aba8
RW
487 switch (acpi_state) {
488 case ACPI_STATE_S1:
1da177e4 489 barrier();
3f6f49c7 490 status = acpi_enter_sleep_state(acpi_state);
1da177e4
LT
491 break;
492
e9b3aba8 493 case ACPI_STATE_S3:
d6a77ead
KRW
494 if (!acpi_suspend_lowlevel)
495 return -ENOSYS;
f1a2003e 496 error = acpi_suspend_lowlevel();
979f11b0
RW
497 if (error)
498 return error;
7a63f08b 499 pr_info(PREFIX "Low-level resume complete\n");
1da177e4 500 break;
1da177e4 501 }
872d83d0 502
b6dacf63
MG
503 /* This violates the spec but is required for bug compatibility. */
504 acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
65df7847 505
3f6f49c7
LB
506 /* Reprogram control registers */
507 acpi_leave_sleep_state_prep(acpi_state);
c95d47a8 508
23b168d4 509 /* ACPI 3.0 specs (P62) says that it's the responsibility
872d83d0
AP
510 * of the OSPM to clear the status bit [ implying that the
511 * POWER_BUTTON event should not reach userspace ]
c10d7a13
DD
512 *
513 * However, we do generate a small hint for userspace in the form of
514 * a wakeup event. We flag this condition for now and generate the
515 * event later, as we're currently too early in resume to be able to
516 * generate wakeup events.
872d83d0 517 */
c10d7a13
DD
518 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
519 acpi_event_status pwr_btn_status;
520
521 acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
522
523 if (pwr_btn_status & ACPI_EVENT_FLAG_SET) {
524 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
525 /* Flag for later */
526 pwr_btn_event_pending = true;
527 }
528 }
872d83d0 529
a3627f67
SL
530 /*
531 * Disable and clear GPE status before interrupt is enabled. Some GPEs
532 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
533 * acpi_leave_sleep_state will reenable specific GPEs later
534 */
3d97e426 535 acpi_disable_all_gpes();
d5a64513 536 /* Allow EC transactions to happen. */
fe955682 537 acpi_ec_unblock_transactions_early();
a3627f67 538
2a6b6976
MG
539 suspend_nvs_restore();
540
1da177e4
LT
541 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
542}
543
2c6e33c3 544static int acpi_suspend_state_valid(suspend_state_t pm_state)
eb9289eb 545{
e8c9c502 546 u32 acpi_state;
eb9289eb 547
e8c9c502
JB
548 switch (pm_state) {
549 case PM_SUSPEND_ON:
550 case PM_SUSPEND_STANDBY:
551 case PM_SUSPEND_MEM:
552 acpi_state = acpi_suspend_states[pm_state];
553
554 return sleep_states[acpi_state];
555 default:
556 return 0;
557 }
eb9289eb
SL
558}
559
2f55ac07 560static const struct platform_suspend_ops acpi_suspend_ops = {
2c6e33c3
LB
561 .valid = acpi_suspend_state_valid,
562 .begin = acpi_suspend_begin,
6a7c7eaf 563 .prepare_late = acpi_pm_prepare,
2c6e33c3 564 .enter = acpi_suspend_enter,
618d7fd0 565 .wake = acpi_pm_finish,
d8f3de0d
RW
566 .end = acpi_pm_end,
567};
568
569/**
570 * acpi_suspend_begin_old - Set the target system sleep state to the
571 * state associated with given @pm_state, if supported, and
572 * execute the _PTS control method. This function is used if the
573 * pre-ACPI 2.0 suspend ordering has been requested.
574 */
575static int acpi_suspend_begin_old(suspend_state_t pm_state)
576{
577 int error = acpi_suspend_begin(pm_state);
d8f3de0d
RW
578 if (!error)
579 error = __acpi_pm_prepare();
c5f7a1bb 580
d8f3de0d
RW
581 return error;
582}
583
584/*
585 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
586 * been requested.
587 */
2f55ac07 588static const struct platform_suspend_ops acpi_suspend_ops_old = {
d8f3de0d
RW
589 .valid = acpi_suspend_state_valid,
590 .begin = acpi_suspend_begin_old,
c5f7a1bb 591 .prepare_late = acpi_pm_pre_suspend,
2c6e33c3 592 .enter = acpi_suspend_enter,
618d7fd0 593 .wake = acpi_pm_finish,
d8f3de0d
RW
594 .end = acpi_pm_end,
595 .recover = acpi_pm_finish,
1da177e4 596};
02040f0b
RW
597
598static void acpi_sleep_suspend_setup(void)
599{
600 int i;
601
602 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
603 acpi_status status;
604 u8 type_a, type_b;
605
606 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
607 if (ACPI_SUCCESS(status)) {
608 sleep_states[i] = 1;
02040f0b
RW
609 }
610 }
611
612 suspend_set_ops(old_suspend_ordering ?
613 &acpi_suspend_ops_old : &acpi_suspend_ops);
614}
615#else /* !CONFIG_SUSPEND */
616static inline void acpi_sleep_suspend_setup(void) {}
617#endif /* !CONFIG_SUSPEND */
296699de 618
b0cb1a19 619#ifdef CONFIG_HIBERNATION
bdfe6b7c
SL
620static unsigned long s4_hardware_signature;
621static struct acpi_table_facs *facs;
622static bool nosigcheck;
623
624void __init acpi_no_s4_hw_signature(void)
625{
626 nosigcheck = true;
627}
628
caea99ef 629static int acpi_hibernation_begin(void)
74f270af 630{
3f4b0ef7
RW
631 int error;
632
72ad5d77 633 error = nvs_nosave ? 0 : suspend_nvs_alloc();
3f4b0ef7
RW
634 if (!error) {
635 acpi_target_sleep_state = ACPI_STATE_S4;
636 acpi_sleep_tts_switch(acpi_target_sleep_state);
637 }
638
639 return error;
640}
641
a3d25c27
RW
642static int acpi_hibernation_enter(void)
643{
644 acpi_status status = AE_OK;
a3d25c27
RW
645
646 ACPI_FLUSH_CPU_CACHE();
647
a3d25c27 648 /* This shouldn't return. If it returns, we have a problem */
3f6f49c7
LB
649 status = acpi_enter_sleep_state(ACPI_STATE_S4);
650 /* Reprogram control registers */
651 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
a3d25c27
RW
652
653 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
654}
655
c7e0831d
RW
656static void acpi_hibernation_leave(void)
657{
658 /*
659 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
660 * enable it here.
661 */
662 acpi_enable();
3f6f49c7
LB
663 /* Reprogram control registers */
664 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
bdfe6b7c
SL
665 /* Check the hardware signature */
666 if (facs && s4_hardware_signature != facs->hardware_signature) {
667 printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
668 "cannot resume!\n");
669 panic("ACPI S4 hardware signature mismatch");
670 }
3f4b0ef7 671 /* Restore the NVS memory area */
dd4c4f17 672 suspend_nvs_restore();
d5a64513 673 /* Allow EC transactions to happen. */
fe955682 674 acpi_ec_unblock_transactions_early();
c7e0831d
RW
675}
676
d5a64513 677static void acpi_pm_thaw(void)
f6bb13aa 678{
fe955682 679 acpi_ec_unblock_transactions();
3d97e426 680 acpi_enable_all_runtime_gpes();
a3d25c27
RW
681}
682
073ef1f6 683static const struct platform_hibernation_ops acpi_hibernation_ops = {
d8f3de0d
RW
684 .begin = acpi_hibernation_begin,
685 .end = acpi_pm_end,
c5f7a1bb 686 .pre_snapshot = acpi_pm_prepare,
2a6b6976 687 .finish = acpi_pm_finish,
d8f3de0d
RW
688 .prepare = acpi_pm_prepare,
689 .enter = acpi_hibernation_enter,
690 .leave = acpi_hibernation_leave,
d5a64513
RW
691 .pre_restore = acpi_pm_freeze,
692 .restore_cleanup = acpi_pm_thaw,
d8f3de0d 693};
caea99ef 694
d8f3de0d
RW
695/**
696 * acpi_hibernation_begin_old - Set the target system sleep state to
697 * ACPI_STATE_S4 and execute the _PTS control method. This
698 * function is used if the pre-ACPI 2.0 suspend ordering has been
699 * requested.
700 */
701static int acpi_hibernation_begin_old(void)
a634cc10 702{
e49f711c
ZY
703 int error;
704 /*
705 * The _TTS object should always be evaluated before the _PTS object.
706 * When the old_suspended_ordering is true, the _PTS object is
707 * evaluated in the acpi_sleep_prepare.
708 */
709 acpi_sleep_tts_switch(ACPI_STATE_S4);
710
711 error = acpi_sleep_prepare(ACPI_STATE_S4);
a634cc10 712
3f4b0ef7 713 if (!error) {
72ad5d77 714 if (!nvs_nosave)
dd4c4f17 715 error = suspend_nvs_alloc();
3f4b0ef7
RW
716 if (!error)
717 acpi_target_sleep_state = ACPI_STATE_S4;
718 }
719 return error;
720}
721
d8f3de0d
RW
722/*
723 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
724 * been requested.
725 */
073ef1f6 726static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
d8f3de0d
RW
727 .begin = acpi_hibernation_begin_old,
728 .end = acpi_pm_end,
c5f7a1bb 729 .pre_snapshot = acpi_pm_pre_suspend,
d5a64513 730 .prepare = acpi_pm_freeze,
2a6b6976 731 .finish = acpi_pm_finish,
a3d25c27 732 .enter = acpi_hibernation_enter,
c7e0831d 733 .leave = acpi_hibernation_leave,
d5a64513
RW
734 .pre_restore = acpi_pm_freeze,
735 .restore_cleanup = acpi_pm_thaw,
d8f3de0d 736 .recover = acpi_pm_finish,
a3d25c27 737};
02040f0b
RW
738
739static void acpi_sleep_hibernate_setup(void)
740{
741 acpi_status status;
742 u8 type_a, type_b;
743
744 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
745 if (ACPI_FAILURE(status))
746 return;
747
748 hibernation_set_ops(old_suspend_ordering ?
749 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
750 sleep_states[ACPI_STATE_S4] = 1;
02040f0b
RW
751 if (nosigcheck)
752 return;
753
754 acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
755 if (facs)
756 s4_hardware_signature = facs->hardware_signature;
757}
758#else /* !CONFIG_HIBERNATION */
759static inline void acpi_sleep_hibernate_setup(void) {}
760#endif /* !CONFIG_HIBERNATION */
a3d25c27 761
296699de
RW
762int acpi_suspend(u32 acpi_state)
763{
764 suspend_state_t states[] = {
765 [1] = PM_SUSPEND_STANDBY,
766 [3] = PM_SUSPEND_MEM,
767 [5] = PM_SUSPEND_MAX
768 };
769
770 if (acpi_state < 6 && states[acpi_state])
771 return pm_suspend(states[acpi_state]);
772 if (acpi_state == 4)
773 return hibernate();
774 return -EINVAL;
775}
776
f216cc37
AS
777static void acpi_power_off_prepare(void)
778{
779 /* Prepare to power off the system */
780 acpi_sleep_prepare(ACPI_STATE_S5);
3d97e426 781 acpi_disable_all_gpes();
f216cc37
AS
782}
783
784static void acpi_power_off(void)
785{
786 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
4d939155 787 printk(KERN_DEBUG "%s called\n", __func__);
f216cc37 788 local_irq_disable();
3f6f49c7 789 acpi_enter_sleep_state(ACPI_STATE_S5);
96f15efc
LB
790}
791
aafbcd16 792int __init acpi_sleep_init(void)
1da177e4 793{
296699de
RW
794 acpi_status status;
795 u8 type_a, type_b;
ed4cf5b2
JP
796 char supported[ACPI_S_STATE_COUNT * 3 + 1];
797 char *pos = supported;
798 int i;
1da177e4
LT
799
800 if (acpi_disabled)
801 return 0;
802
0ac1b1d7
ZR
803 acpi_sleep_dmi_check();
804
5a50fe70 805 sleep_states[ACPI_STATE_S0] = 1;
5a50fe70 806
02040f0b
RW
807 acpi_sleep_suspend_setup();
808 acpi_sleep_hibernate_setup();
1da177e4 809
f216cc37
AS
810 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
811 if (ACPI_SUCCESS(status)) {
812 sleep_states[ACPI_STATE_S5] = 1;
f216cc37
AS
813 pm_power_off_prepare = acpi_power_off_prepare;
814 pm_power_off = acpi_power_off;
815 }
ed4cf5b2
JP
816
817 supported[0] = 0;
818 for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
819 if (sleep_states[i])
820 pos += sprintf(pos, " S%d", i);
821 }
822 pr_info(PREFIX "(supports%s)\n", supported);
823
e49f711c
ZY
824 /*
825 * Register the tts_notifier to reboot notifier list so that the _TTS
826 * object can also be evaluated when the system enters S5.
827 */
828 register_reboot_notifier(&tts_notifier);
1da177e4
LT
829 return 0;
830}