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