1
2
3
4
5
6
7
8
9
10
11#include <linux/delay.h>
12#include <linux/irq.h>
13#include <linux/dmi.h>
14#include <linux/device.h>
15#include <linux/interrupt.h>
16#include <linux/suspend.h>
17#include <linux/reboot.h>
18#include <linux/acpi.h>
19#include <linux/module.h>
20#include <linux/syscore_ops.h>
21#include <asm/io.h>
22#include <trace/events/power.h>
23
24#include "internal.h"
25#include "sleep.h"
26
27
28
29
30
31bool acpi_no_s5;
32static u8 sleep_states[ACPI_S_STATE_COUNT];
33
34static void acpi_sleep_tts_switch(u32 acpi_state)
35{
36 acpi_status status;
37
38 status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
39 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
40
41
42
43
44 printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
45 }
46}
47
48static int tts_notify_reboot(struct notifier_block *this,
49 unsigned long code, void *x)
50{
51 acpi_sleep_tts_switch(ACPI_STATE_S5);
52 return NOTIFY_DONE;
53}
54
55static struct notifier_block tts_notifier = {
56 .notifier_call = tts_notify_reboot,
57 .next = NULL,
58 .priority = 0,
59};
60
61static int acpi_sleep_prepare(u32 acpi_state)
62{
63#ifdef CONFIG_ACPI_SLEEP
64 unsigned long acpi_wakeup_address;
65
66
67 if (acpi_state == ACPI_STATE_S3) {
68 acpi_wakeup_address = acpi_get_wakeup_address();
69 if (!acpi_wakeup_address)
70 return -EFAULT;
71 acpi_set_waking_vector(acpi_wakeup_address);
72
73 }
74 ACPI_FLUSH_CPU_CACHE();
75#endif
76 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
77 acpi_state);
78 acpi_enable_wakeup_devices(acpi_state);
79 acpi_enter_sleep_state_prep(acpi_state);
80 return 0;
81}
82
83bool acpi_sleep_state_supported(u8 sleep_state)
84{
85 acpi_status status;
86 u8 type_a, type_b;
87
88 status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
89 return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
90 || (acpi_gbl_FADT.sleep_control.address
91 && acpi_gbl_FADT.sleep_status.address));
92}
93
94#ifdef CONFIG_ACPI_SLEEP
95static u32 acpi_target_sleep_state = ACPI_STATE_S0;
96
97u32 acpi_target_system_state(void)
98{
99 return acpi_target_sleep_state;
100}
101EXPORT_SYMBOL_GPL(acpi_target_system_state);
102
103static bool pwr_btn_event_pending;
104
105
106
107
108
109
110
111
112static bool nvs_nosave;
113
114void __init acpi_nvs_nosave(void)
115{
116 nvs_nosave = true;
117}
118
119
120
121
122
123
124
125
126
127static bool nvs_nosave_s3;
128
129void __init acpi_nvs_nosave_s3(void)
130{
131 nvs_nosave_s3 = true;
132}
133
134static int __init init_nvs_save_s3(const struct dmi_system_id *d)
135{
136 nvs_nosave_s3 = false;
137 return 0;
138}
139
140
141
142
143
144
145static bool old_suspend_ordering;
146
147void __init acpi_old_suspend_ordering(void)
148{
149 old_suspend_ordering = true;
150}
151
152static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
153{
154 acpi_old_suspend_ordering();
155 return 0;
156}
157
158static int __init init_nvs_nosave(const struct dmi_system_id *d)
159{
160 acpi_nvs_nosave();
161 return 0;
162}
163
164bool acpi_sleep_default_s3;
165
166static int __init init_default_s3(const struct dmi_system_id *d)
167{
168 acpi_sleep_default_s3 = true;
169 return 0;
170}
171
172static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
173 {
174 .callback = init_old_suspend_ordering,
175 .ident = "Abit KN9 (nForce4 variant)",
176 .matches = {
177 DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
178 DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
179 },
180 },
181 {
182 .callback = init_old_suspend_ordering,
183 .ident = "HP xw4600 Workstation",
184 .matches = {
185 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
186 DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
187 },
188 },
189 {
190 .callback = init_old_suspend_ordering,
191 .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
192 .matches = {
193 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
194 DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
195 },
196 },
197 {
198 .callback = init_old_suspend_ordering,
199 .ident = "Panasonic CF51-2L",
200 .matches = {
201 DMI_MATCH(DMI_BOARD_VENDOR,
202 "Matsushita Electric Industrial Co.,Ltd."),
203 DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
204 },
205 },
206 {
207 .callback = init_nvs_nosave,
208 .ident = "Sony Vaio VGN-FW41E_H",
209 .matches = {
210 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
211 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
212 },
213 },
214 {
215 .callback = init_nvs_nosave,
216 .ident = "Sony Vaio VGN-FW21E",
217 .matches = {
218 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
219 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
220 },
221 },
222 {
223 .callback = init_nvs_nosave,
224 .ident = "Sony Vaio VGN-FW21M",
225 .matches = {
226 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
227 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
228 },
229 },
230 {
231 .callback = init_nvs_nosave,
232 .ident = "Sony Vaio VPCEB17FX",
233 .matches = {
234 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
235 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
236 },
237 },
238 {
239 .callback = init_nvs_nosave,
240 .ident = "Sony Vaio VGN-SR11M",
241 .matches = {
242 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
243 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
244 },
245 },
246 {
247 .callback = init_nvs_nosave,
248 .ident = "Everex StepNote Series",
249 .matches = {
250 DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
251 DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
252 },
253 },
254 {
255 .callback = init_nvs_nosave,
256 .ident = "Sony Vaio VPCEB1Z1E",
257 .matches = {
258 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
259 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
260 },
261 },
262 {
263 .callback = init_nvs_nosave,
264 .ident = "Sony Vaio VGN-NW130D",
265 .matches = {
266 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
267 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
268 },
269 },
270 {
271 .callback = init_nvs_nosave,
272 .ident = "Sony Vaio VPCCW29FX",
273 .matches = {
274 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
275 DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
276 },
277 },
278 {
279 .callback = init_nvs_nosave,
280 .ident = "Averatec AV1020-ED2",
281 .matches = {
282 DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
283 DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
284 },
285 },
286 {
287 .callback = init_old_suspend_ordering,
288 .ident = "Asus A8N-SLI DELUXE",
289 .matches = {
290 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
291 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
292 },
293 },
294 {
295 .callback = init_old_suspend_ordering,
296 .ident = "Asus A8N-SLI Premium",
297 .matches = {
298 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
299 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
300 },
301 },
302 {
303 .callback = init_nvs_nosave,
304 .ident = "Sony Vaio VGN-SR26GN_P",
305 .matches = {
306 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
307 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
308 },
309 },
310 {
311 .callback = init_nvs_nosave,
312 .ident = "Sony Vaio VPCEB1S1E",
313 .matches = {
314 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
315 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
316 },
317 },
318 {
319 .callback = init_nvs_nosave,
320 .ident = "Sony Vaio VGN-FW520F",
321 .matches = {
322 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
323 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
324 },
325 },
326 {
327 .callback = init_nvs_nosave,
328 .ident = "Asus K54C",
329 .matches = {
330 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
331 DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
332 },
333 },
334 {
335 .callback = init_nvs_nosave,
336 .ident = "Asus K54HR",
337 .matches = {
338 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
339 DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
340 },
341 },
342 {
343 .callback = init_nvs_save_s3,
344 .ident = "Asus 1025C",
345 .matches = {
346 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
347 DMI_MATCH(DMI_PRODUCT_NAME, "1025C"),
348 },
349 },
350
351
352
353
354
355 {
356 .callback = init_nvs_save_s3,
357 .ident = "Lenovo G50-45",
358 .matches = {
359 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
360 DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
361 },
362 },
363
364
365
366
367
368 {
369 .callback = init_default_s3,
370 .ident = "ThinkPad X1 Tablet(2016)",
371 .matches = {
372 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
373 DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
374 },
375 },
376 {},
377};
378
379static bool ignore_blacklist;
380
381void __init acpi_sleep_no_blacklist(void)
382{
383 ignore_blacklist = true;
384}
385
386static void __init acpi_sleep_dmi_check(void)
387{
388 if (ignore_blacklist)
389 return;
390
391 if (dmi_get_bios_year() >= 2012)
392 acpi_nvs_nosave_s3();
393
394 dmi_check_system(acpisleep_dmi_table);
395}
396
397
398
399
400static int acpi_pm_freeze(void)
401{
402 acpi_disable_all_gpes();
403 acpi_os_wait_events_complete();
404 acpi_ec_block_transactions();
405 return 0;
406}
407
408
409
410
411static int acpi_pm_pre_suspend(void)
412{
413 acpi_pm_freeze();
414 return suspend_nvs_save();
415}
416
417
418
419
420
421
422
423static int __acpi_pm_prepare(void)
424{
425 int error = acpi_sleep_prepare(acpi_target_sleep_state);
426 if (error)
427 acpi_target_sleep_state = ACPI_STATE_S0;
428
429 return error;
430}
431
432
433
434
435
436static int acpi_pm_prepare(void)
437{
438 int error = __acpi_pm_prepare();
439 if (!error)
440 error = acpi_pm_pre_suspend();
441
442 return error;
443}
444
445
446
447
448
449
450
451static void acpi_pm_finish(void)
452{
453 struct acpi_device *pwr_btn_adev;
454 u32 acpi_state = acpi_target_sleep_state;
455
456 acpi_ec_unblock_transactions();
457 suspend_nvs_free();
458
459 if (acpi_state == ACPI_STATE_S0)
460 return;
461
462 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
463 acpi_state);
464 acpi_disable_wakeup_devices(acpi_state);
465 acpi_leave_sleep_state(acpi_state);
466
467
468 acpi_set_waking_vector(0);
469
470 acpi_target_sleep_state = ACPI_STATE_S0;
471
472 acpi_resume_power_resources();
473
474
475
476
477
478
479
480 if (!pwr_btn_event_pending)
481 return;
482
483 pwr_btn_event_pending = false;
484 pwr_btn_adev = acpi_dev_get_first_match_dev(ACPI_BUTTON_HID_POWERF,
485 NULL, -1);
486 if (pwr_btn_adev) {
487 pm_wakeup_event(&pwr_btn_adev->dev, 0);
488 acpi_dev_put(pwr_btn_adev);
489 }
490}
491
492
493
494
495static void acpi_pm_start(u32 acpi_state)
496{
497 acpi_target_sleep_state = acpi_state;
498 acpi_sleep_tts_switch(acpi_target_sleep_state);
499 acpi_scan_lock_acquire();
500}
501
502
503
504
505static void acpi_pm_end(void)
506{
507 acpi_turn_off_unused_power_resources();
508 acpi_scan_lock_release();
509
510
511
512
513 acpi_target_sleep_state = ACPI_STATE_S0;
514 acpi_sleep_tts_switch(acpi_target_sleep_state);
515}
516#else
517#define sleep_no_lps0 (1)
518#define acpi_target_sleep_state ACPI_STATE_S0
519#define acpi_sleep_default_s3 (1)
520static inline void acpi_sleep_dmi_check(void) {}
521#endif
522
523#ifdef CONFIG_SUSPEND
524static u32 acpi_suspend_states[] = {
525 [PM_SUSPEND_ON] = ACPI_STATE_S0,
526 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
527 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
528 [PM_SUSPEND_MAX] = ACPI_STATE_S5
529};
530
531
532
533
534
535static int acpi_suspend_begin(suspend_state_t pm_state)
536{
537 u32 acpi_state = acpi_suspend_states[pm_state];
538 int error;
539
540 error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
541 if (error)
542 return error;
543
544 if (!sleep_states[acpi_state]) {
545 pr_err("ACPI does not support sleep state S%u\n", acpi_state);
546 return -ENOSYS;
547 }
548 if (acpi_state > ACPI_STATE_S1)
549 pm_set_suspend_via_firmware();
550
551 acpi_pm_start(acpi_state);
552 return 0;
553}
554
555
556
557
558
559
560
561
562
563static int acpi_suspend_enter(suspend_state_t pm_state)
564{
565 acpi_status status = AE_OK;
566 u32 acpi_state = acpi_target_sleep_state;
567 int error;
568
569 ACPI_FLUSH_CPU_CACHE();
570
571 trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
572 switch (acpi_state) {
573 case ACPI_STATE_S1:
574 barrier();
575 status = acpi_enter_sleep_state(acpi_state);
576 break;
577
578 case ACPI_STATE_S3:
579 if (!acpi_suspend_lowlevel)
580 return -ENOSYS;
581 error = acpi_suspend_lowlevel();
582 if (error)
583 return error;
584 pr_info(PREFIX "Low-level resume complete\n");
585 pm_set_resume_via_firmware();
586 break;
587 }
588 trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
589
590
591 acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
592
593
594 acpi_leave_sleep_state_prep(acpi_state);
595
596
597
598
599
600
601
602
603
604
605 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
606 acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
607
608 acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
609
610 if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
611 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
612
613 pwr_btn_event_pending = true;
614 }
615 }
616
617
618
619
620
621
622 acpi_disable_all_gpes();
623
624 acpi_ec_unblock_transactions();
625
626 suspend_nvs_restore();
627
628 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
629}
630
631static int acpi_suspend_state_valid(suspend_state_t pm_state)
632{
633 u32 acpi_state;
634
635 switch (pm_state) {
636 case PM_SUSPEND_ON:
637 case PM_SUSPEND_STANDBY:
638 case PM_SUSPEND_MEM:
639 acpi_state = acpi_suspend_states[pm_state];
640
641 return sleep_states[acpi_state];
642 default:
643 return 0;
644 }
645}
646
647static const struct platform_suspend_ops acpi_suspend_ops = {
648 .valid = acpi_suspend_state_valid,
649 .begin = acpi_suspend_begin,
650 .prepare_late = acpi_pm_prepare,
651 .enter = acpi_suspend_enter,
652 .wake = acpi_pm_finish,
653 .end = acpi_pm_end,
654};
655
656
657
658
659
660
661
662static int acpi_suspend_begin_old(suspend_state_t pm_state)
663{
664 int error = acpi_suspend_begin(pm_state);
665 if (!error)
666 error = __acpi_pm_prepare();
667
668 return error;
669}
670
671
672
673
674
675static const struct platform_suspend_ops acpi_suspend_ops_old = {
676 .valid = acpi_suspend_state_valid,
677 .begin = acpi_suspend_begin_old,
678 .prepare_late = acpi_pm_pre_suspend,
679 .enter = acpi_suspend_enter,
680 .wake = acpi_pm_finish,
681 .end = acpi_pm_end,
682 .recover = acpi_pm_finish,
683};
684
685static bool s2idle_wakeup;
686
687int acpi_s2idle_begin(void)
688{
689 acpi_scan_lock_acquire();
690 return 0;
691}
692
693int acpi_s2idle_prepare(void)
694{
695 if (acpi_sci_irq_valid()) {
696 enable_irq_wake(acpi_sci_irq);
697 acpi_ec_set_gpe_wake_mask(ACPI_GPE_ENABLE);
698 }
699
700 acpi_enable_wakeup_devices(ACPI_STATE_S0);
701
702
703 acpi_enable_all_wakeup_gpes();
704 acpi_os_wait_events_complete();
705
706 s2idle_wakeup = true;
707 return 0;
708}
709
710bool acpi_s2idle_wake(void)
711{
712 if (!acpi_sci_irq_valid())
713 return pm_wakeup_pending();
714
715 while (pm_wakeup_pending()) {
716
717
718
719
720
721
722 if (irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq))) {
723 pm_pr_dbg("Wakeup unrelated to ACPI SCI\n");
724 return true;
725 }
726
727
728
729
730
731 if (acpi_any_fixed_event_status_set()) {
732 pm_pr_dbg("ACPI fixed event wakeup\n");
733 return true;
734 }
735
736
737 if (acpi_check_wakeup_handlers()) {
738 pm_pr_dbg("ACPI custom handler wakeup\n");
739 return true;
740 }
741
742
743 if (acpi_ec_dispatch_gpe()) {
744 pm_pr_dbg("ACPI non-EC GPE wakeup\n");
745 return true;
746 }
747
748
749
750
751
752
753
754
755
756 pm_system_cancel_wakeup();
757 acpi_os_wait_events_complete();
758
759
760
761
762
763
764
765 if (pm_wakeup_pending()) {
766 pm_pr_dbg("Wakeup after ACPI Notify sync\n");
767 return true;
768 }
769
770 rearm_wake_irq(acpi_sci_irq);
771 }
772
773 return false;
774}
775
776void acpi_s2idle_restore(void)
777{
778
779
780
781
782 acpi_os_wait_events_complete();
783 acpi_ec_flush_work();
784 acpi_os_wait_events_complete();
785
786 s2idle_wakeup = false;
787
788 acpi_enable_all_runtime_gpes();
789
790 acpi_disable_wakeup_devices(ACPI_STATE_S0);
791
792 if (acpi_sci_irq_valid()) {
793 acpi_ec_set_gpe_wake_mask(ACPI_GPE_DISABLE);
794 disable_irq_wake(acpi_sci_irq);
795 }
796}
797
798void acpi_s2idle_end(void)
799{
800 acpi_scan_lock_release();
801}
802
803static const struct platform_s2idle_ops acpi_s2idle_ops = {
804 .begin = acpi_s2idle_begin,
805 .prepare = acpi_s2idle_prepare,
806 .wake = acpi_s2idle_wake,
807 .restore = acpi_s2idle_restore,
808 .end = acpi_s2idle_end,
809};
810
811void __weak acpi_s2idle_setup(void)
812{
813 s2idle_set_ops(&acpi_s2idle_ops);
814}
815
816static void acpi_sleep_suspend_setup(void)
817{
818 int i;
819
820 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
821 if (acpi_sleep_state_supported(i))
822 sleep_states[i] = 1;
823
824 suspend_set_ops(old_suspend_ordering ?
825 &acpi_suspend_ops_old : &acpi_suspend_ops);
826
827 acpi_s2idle_setup();
828}
829
830#else
831#define s2idle_wakeup (false)
832static inline void acpi_sleep_suspend_setup(void) {}
833#endif
834
835bool acpi_s2idle_wakeup(void)
836{
837 return s2idle_wakeup;
838}
839
840#ifdef CONFIG_PM_SLEEP
841static u32 saved_bm_rld;
842
843static int acpi_save_bm_rld(void)
844{
845 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
846 return 0;
847}
848
849static void acpi_restore_bm_rld(void)
850{
851 u32 resumed_bm_rld = 0;
852
853 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
854 if (resumed_bm_rld == saved_bm_rld)
855 return;
856
857 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
858}
859
860static struct syscore_ops acpi_sleep_syscore_ops = {
861 .suspend = acpi_save_bm_rld,
862 .resume = acpi_restore_bm_rld,
863};
864
865static void acpi_sleep_syscore_init(void)
866{
867 register_syscore_ops(&acpi_sleep_syscore_ops);
868}
869#else
870static inline void acpi_sleep_syscore_init(void) {}
871#endif
872
873#ifdef CONFIG_HIBERNATION
874static unsigned long s4_hardware_signature;
875static struct acpi_table_facs *facs;
876static bool nosigcheck;
877
878void __init acpi_no_s4_hw_signature(void)
879{
880 nosigcheck = true;
881}
882
883static int acpi_hibernation_begin(pm_message_t stage)
884{
885 if (!nvs_nosave) {
886 int error = suspend_nvs_alloc();
887 if (error)
888 return error;
889 }
890
891 if (stage.event == PM_EVENT_HIBERNATE)
892 pm_set_suspend_via_firmware();
893
894 acpi_pm_start(ACPI_STATE_S4);
895 return 0;
896}
897
898static int acpi_hibernation_enter(void)
899{
900 acpi_status status = AE_OK;
901
902 ACPI_FLUSH_CPU_CACHE();
903
904
905 status = acpi_enter_sleep_state(ACPI_STATE_S4);
906
907 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
908
909 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
910}
911
912static void acpi_hibernation_leave(void)
913{
914 pm_set_resume_via_firmware();
915
916
917
918
919 acpi_enable();
920
921 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
922
923 if (facs && s4_hardware_signature != facs->hardware_signature)
924 pr_crit("ACPI: Hardware changed while hibernated, success doubtful!\n");
925
926 suspend_nvs_restore();
927
928 acpi_ec_unblock_transactions();
929}
930
931static void acpi_pm_thaw(void)
932{
933 acpi_ec_unblock_transactions();
934 acpi_enable_all_runtime_gpes();
935}
936
937static const struct platform_hibernation_ops acpi_hibernation_ops = {
938 .begin = acpi_hibernation_begin,
939 .end = acpi_pm_end,
940 .pre_snapshot = acpi_pm_prepare,
941 .finish = acpi_pm_finish,
942 .prepare = acpi_pm_prepare,
943 .enter = acpi_hibernation_enter,
944 .leave = acpi_hibernation_leave,
945 .pre_restore = acpi_pm_freeze,
946 .restore_cleanup = acpi_pm_thaw,
947};
948
949
950
951
952
953
954
955static int acpi_hibernation_begin_old(pm_message_t stage)
956{
957 int error;
958
959
960
961
962
963 acpi_sleep_tts_switch(ACPI_STATE_S4);
964
965 error = acpi_sleep_prepare(ACPI_STATE_S4);
966 if (error)
967 return error;
968
969 if (!nvs_nosave) {
970 error = suspend_nvs_alloc();
971 if (error)
972 return error;
973 }
974
975 if (stage.event == PM_EVENT_HIBERNATE)
976 pm_set_suspend_via_firmware();
977
978 acpi_target_sleep_state = ACPI_STATE_S4;
979 acpi_scan_lock_acquire();
980 return 0;
981}
982
983
984
985
986
987static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
988 .begin = acpi_hibernation_begin_old,
989 .end = acpi_pm_end,
990 .pre_snapshot = acpi_pm_pre_suspend,
991 .prepare = acpi_pm_freeze,
992 .finish = acpi_pm_finish,
993 .enter = acpi_hibernation_enter,
994 .leave = acpi_hibernation_leave,
995 .pre_restore = acpi_pm_freeze,
996 .restore_cleanup = acpi_pm_thaw,
997 .recover = acpi_pm_finish,
998};
999
1000static void acpi_sleep_hibernate_setup(void)
1001{
1002 if (!acpi_sleep_state_supported(ACPI_STATE_S4))
1003 return;
1004
1005 hibernation_set_ops(old_suspend_ordering ?
1006 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
1007 sleep_states[ACPI_STATE_S4] = 1;
1008 if (nosigcheck)
1009 return;
1010
1011 acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
1012 if (facs) {
1013 s4_hardware_signature = facs->hardware_signature;
1014 acpi_put_table((struct acpi_table_header *)facs);
1015 }
1016}
1017#else
1018static inline void acpi_sleep_hibernate_setup(void) {}
1019#endif
1020
1021static void acpi_power_off_prepare(void)
1022{
1023
1024 acpi_sleep_prepare(ACPI_STATE_S5);
1025 acpi_disable_all_gpes();
1026 acpi_os_wait_events_complete();
1027}
1028
1029static void acpi_power_off(void)
1030{
1031
1032 printk(KERN_DEBUG "%s called\n", __func__);
1033 local_irq_disable();
1034 acpi_enter_sleep_state(ACPI_STATE_S5);
1035}
1036
1037int __init acpi_sleep_init(void)
1038{
1039 char supported[ACPI_S_STATE_COUNT * 3 + 1];
1040 char *pos = supported;
1041 int i;
1042
1043 acpi_sleep_dmi_check();
1044
1045 sleep_states[ACPI_STATE_S0] = 1;
1046
1047 acpi_sleep_syscore_init();
1048 acpi_sleep_suspend_setup();
1049 acpi_sleep_hibernate_setup();
1050
1051 if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
1052 sleep_states[ACPI_STATE_S5] = 1;
1053 pm_power_off_prepare = acpi_power_off_prepare;
1054 pm_power_off = acpi_power_off;
1055 } else {
1056 acpi_no_s5 = true;
1057 }
1058
1059 supported[0] = 0;
1060 for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
1061 if (sleep_states[i])
1062 pos += sprintf(pos, " S%d", i);
1063 }
1064 pr_info(PREFIX "(supports%s)\n", supported);
1065
1066
1067
1068
1069
1070 register_reboot_notifier(&tts_notifier);
1071 return 0;
1072}
1073