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