2 * sleep.c - ACPI sleep support.
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
9 * This file is released under the GPLv2.
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>
18 #include <linux/reboot.h>
19 #include <linux/acpi.h>
20 #include <linux/module.h>
24 #include <acpi/acpi_bus.h>
25 #include <acpi/acpi_drivers.h>
30 static u8 sleep_states[ACPI_S_STATE_COUNT];
32 static void acpi_sleep_tts_switch(u32 acpi_state)
34 union acpi_object in_arg = { ACPI_TYPE_INTEGER };
35 struct acpi_object_list arg_list = { 1, &in_arg };
36 acpi_status status = AE_OK;
38 in_arg.integer.value = acpi_state;
39 status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
40 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
42 * OS can't evaluate the _TTS object correctly. Some warning
43 * message will be printed. But it won't break anything.
45 printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
49 static int tts_notify_reboot(struct notifier_block *this,
50 unsigned long code, void *x)
52 acpi_sleep_tts_switch(ACPI_STATE_S5);
56 static struct notifier_block tts_notifier = {
57 .notifier_call = tts_notify_reboot,
62 static int acpi_sleep_prepare(u32 acpi_state)
64 #ifdef CONFIG_ACPI_SLEEP
65 /* do we have a wakeup address for S2 and S3? */
66 if (acpi_state == ACPI_STATE_S3) {
67 if (!acpi_wakeup_address)
69 acpi_set_firmware_waking_vector(acpi_wakeup_address);
72 ACPI_FLUSH_CPU_CACHE();
74 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
76 acpi_enable_wakeup_devices(acpi_state);
77 acpi_enter_sleep_state_prep(acpi_state);
81 #ifdef CONFIG_ACPI_SLEEP
82 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
83 static bool pwr_btn_event_pending;
86 * The ACPI specification wants us to save NVS memory regions during hibernation
87 * and to restore them during the subsequent resume. Windows does that also for
88 * suspend to RAM. However, it is known that this mechanism does not work on
89 * all machines, so we allow the user to disable it with the help of the
90 * 'acpi_sleep=nonvs' kernel command line option.
92 static bool nvs_nosave;
94 void __init acpi_nvs_nosave(void)
100 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
101 * user to request that behavior by using the 'acpi_old_suspend_ordering'
102 * kernel command line option that causes the following variable to be set.
104 static bool old_suspend_ordering;
106 void __init acpi_old_suspend_ordering(void)
108 old_suspend_ordering = true;
112 * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
114 static int acpi_pm_freeze(void)
116 acpi_disable_all_gpes();
117 acpi_os_wait_events_complete();
118 acpi_ec_block_transactions();
123 * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
125 static int acpi_pm_pre_suspend(void)
128 return suspend_nvs_save();
132 * __acpi_pm_prepare - Prepare the platform to enter the target state.
134 * If necessary, set the firmware waking vector and do arch-specific
135 * nastiness to get the wakeup code to the waking vector.
137 static int __acpi_pm_prepare(void)
139 int error = acpi_sleep_prepare(acpi_target_sleep_state);
141 acpi_target_sleep_state = ACPI_STATE_S0;
147 * acpi_pm_prepare - Prepare the platform to enter the target sleep
148 * state and disable the GPEs.
150 static int acpi_pm_prepare(void)
152 int error = __acpi_pm_prepare();
154 error = acpi_pm_pre_suspend();
159 static int find_powerf_dev(struct device *dev, void *data)
161 struct acpi_device *device = to_acpi_device(dev);
162 const char *hid = acpi_device_hid(device);
164 return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
168 * acpi_pm_finish - Instruct the platform to leave a sleep state.
170 * This is called after we wake back up (or if entering the sleep state
173 static void acpi_pm_finish(void)
175 struct device *pwr_btn_dev;
176 u32 acpi_state = acpi_target_sleep_state;
178 acpi_ec_unblock_transactions();
181 if (acpi_state == ACPI_STATE_S0)
184 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
186 acpi_disable_wakeup_devices(acpi_state);
187 acpi_leave_sleep_state(acpi_state);
189 /* reset firmware waking vector */
190 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
192 acpi_target_sleep_state = ACPI_STATE_S0;
194 /* If we were woken with the fixed power button, provide a small
195 * hint to userspace in the form of a wakeup event on the fixed power
196 * button device (if it can be found).
198 * We delay the event generation til now, as the PM layer requires
199 * timekeeping to be running before we generate events. */
200 if (!pwr_btn_event_pending)
203 pwr_btn_event_pending = false;
204 pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
207 pm_wakeup_event(pwr_btn_dev, 0);
208 put_device(pwr_btn_dev);
213 * acpi_pm_end - Finish up suspend sequence.
215 static void acpi_pm_end(void)
218 * This is necessary in case acpi_pm_finish() is not called during a
219 * failing transition to a sleep state.
221 acpi_target_sleep_state = ACPI_STATE_S0;
222 acpi_sleep_tts_switch(acpi_target_sleep_state);
224 #else /* !CONFIG_ACPI_SLEEP */
225 #define acpi_target_sleep_state ACPI_STATE_S0
226 #endif /* CONFIG_ACPI_SLEEP */
228 #ifdef CONFIG_SUSPEND
229 static u32 acpi_suspend_states[] = {
230 [PM_SUSPEND_ON] = ACPI_STATE_S0,
231 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
232 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
233 [PM_SUSPEND_MAX] = ACPI_STATE_S5
237 * acpi_suspend_begin - Set the target system sleep state to the state
238 * associated with given @pm_state, if supported.
240 static int acpi_suspend_begin(suspend_state_t pm_state)
242 u32 acpi_state = acpi_suspend_states[pm_state];
245 error = nvs_nosave ? 0 : suspend_nvs_alloc();
249 if (sleep_states[acpi_state]) {
250 acpi_target_sleep_state = acpi_state;
251 acpi_sleep_tts_switch(acpi_target_sleep_state);
253 printk(KERN_ERR "ACPI does not support this state: %d\n",
261 * acpi_suspend_enter - Actually enter a sleep state.
264 * Flush caches and go to sleep. For STR we have to call arch-specific
265 * assembly, which in turn call acpi_enter_sleep_state().
266 * It's unfortunate, but it works. Please fix if you're feeling frisky.
268 static int acpi_suspend_enter(suspend_state_t pm_state)
270 acpi_status status = AE_OK;
271 u32 acpi_state = acpi_target_sleep_state;
274 ACPI_FLUSH_CPU_CACHE();
276 switch (acpi_state) {
279 status = acpi_enter_sleep_state(acpi_state);
283 error = acpi_suspend_lowlevel();
286 pr_info(PREFIX "Low-level resume complete\n");
290 /* This violates the spec but is required for bug compatibility. */
291 acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
293 /* Reprogram control registers */
294 acpi_leave_sleep_state_prep(acpi_state);
296 /* ACPI 3.0 specs (P62) says that it's the responsibility
297 * of the OSPM to clear the status bit [ implying that the
298 * POWER_BUTTON event should not reach userspace ]
300 * However, we do generate a small hint for userspace in the form of
301 * a wakeup event. We flag this condition for now and generate the
302 * event later, as we're currently too early in resume to be able to
303 * generate wakeup events.
305 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
306 acpi_event_status pwr_btn_status;
308 acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
310 if (pwr_btn_status & ACPI_EVENT_FLAG_SET) {
311 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
313 pwr_btn_event_pending = true;
318 * Disable and clear GPE status before interrupt is enabled. Some GPEs
319 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
320 * acpi_leave_sleep_state will reenable specific GPEs later
322 acpi_disable_all_gpes();
323 /* Allow EC transactions to happen. */
324 acpi_ec_unblock_transactions_early();
326 suspend_nvs_restore();
328 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
331 static int acpi_suspend_state_valid(suspend_state_t pm_state)
337 case PM_SUSPEND_STANDBY:
339 acpi_state = acpi_suspend_states[pm_state];
341 return sleep_states[acpi_state];
347 static const struct platform_suspend_ops acpi_suspend_ops = {
348 .valid = acpi_suspend_state_valid,
349 .begin = acpi_suspend_begin,
350 .prepare_late = acpi_pm_prepare,
351 .enter = acpi_suspend_enter,
352 .wake = acpi_pm_finish,
357 * acpi_suspend_begin_old - Set the target system sleep state to the
358 * state associated with given @pm_state, if supported, and
359 * execute the _PTS control method. This function is used if the
360 * pre-ACPI 2.0 suspend ordering has been requested.
362 static int acpi_suspend_begin_old(suspend_state_t pm_state)
364 int error = acpi_suspend_begin(pm_state);
366 error = __acpi_pm_prepare();
372 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
375 static const struct platform_suspend_ops acpi_suspend_ops_old = {
376 .valid = acpi_suspend_state_valid,
377 .begin = acpi_suspend_begin_old,
378 .prepare_late = acpi_pm_pre_suspend,
379 .enter = acpi_suspend_enter,
380 .wake = acpi_pm_finish,
382 .recover = acpi_pm_finish,
385 static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
387 old_suspend_ordering = true;
391 static int __init init_nvs_nosave(const struct dmi_system_id *d)
397 static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
399 .callback = init_old_suspend_ordering,
400 .ident = "Abit KN9 (nForce4 variant)",
402 DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
403 DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
407 .callback = init_old_suspend_ordering,
408 .ident = "HP xw4600 Workstation",
410 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
411 DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
415 .callback = init_old_suspend_ordering,
416 .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
418 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
419 DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
423 .callback = init_old_suspend_ordering,
424 .ident = "Panasonic CF51-2L",
426 DMI_MATCH(DMI_BOARD_VENDOR,
427 "Matsushita Electric Industrial Co.,Ltd."),
428 DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
432 .callback = init_nvs_nosave,
433 .ident = "Sony Vaio VGN-FW21E",
435 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
436 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
440 .callback = init_nvs_nosave,
441 .ident = "Sony Vaio VPCEB17FX",
443 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
444 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
448 .callback = init_nvs_nosave,
449 .ident = "Sony Vaio VGN-SR11M",
451 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
452 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
456 .callback = init_nvs_nosave,
457 .ident = "Everex StepNote Series",
459 DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
460 DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
464 .callback = init_nvs_nosave,
465 .ident = "Sony Vaio VPCEB1Z1E",
467 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
468 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
472 .callback = init_nvs_nosave,
473 .ident = "Sony Vaio VGN-NW130D",
475 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
476 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
480 .callback = init_nvs_nosave,
481 .ident = "Sony Vaio VPCCW29FX",
483 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
484 DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
488 .callback = init_nvs_nosave,
489 .ident = "Averatec AV1020-ED2",
491 DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
492 DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
496 .callback = init_old_suspend_ordering,
497 .ident = "Asus A8N-SLI DELUXE",
499 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
500 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
504 .callback = init_old_suspend_ordering,
505 .ident = "Asus A8N-SLI Premium",
507 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
508 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
512 .callback = init_nvs_nosave,
513 .ident = "Sony Vaio VGN-SR26GN_P",
515 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
516 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
520 .callback = init_nvs_nosave,
521 .ident = "Sony Vaio VGN-FW520F",
523 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
524 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
528 .callback = init_nvs_nosave,
529 .ident = "Asus K54C",
531 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
532 DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
536 .callback = init_nvs_nosave,
537 .ident = "Asus K54HR",
539 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
540 DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
545 #endif /* CONFIG_SUSPEND */
547 #ifdef CONFIG_HIBERNATION
548 static unsigned long s4_hardware_signature;
549 static struct acpi_table_facs *facs;
550 static bool nosigcheck;
552 void __init acpi_no_s4_hw_signature(void)
557 static int acpi_hibernation_begin(void)
561 error = nvs_nosave ? 0 : suspend_nvs_alloc();
563 acpi_target_sleep_state = ACPI_STATE_S4;
564 acpi_sleep_tts_switch(acpi_target_sleep_state);
570 static int acpi_hibernation_enter(void)
572 acpi_status status = AE_OK;
574 ACPI_FLUSH_CPU_CACHE();
576 /* This shouldn't return. If it returns, we have a problem */
577 status = acpi_enter_sleep_state(ACPI_STATE_S4);
578 /* Reprogram control registers */
579 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
581 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
584 static void acpi_hibernation_leave(void)
587 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
591 /* Reprogram control registers */
592 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
593 /* Check the hardware signature */
594 if (facs && s4_hardware_signature != facs->hardware_signature) {
595 printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
597 panic("ACPI S4 hardware signature mismatch");
599 /* Restore the NVS memory area */
600 suspend_nvs_restore();
601 /* Allow EC transactions to happen. */
602 acpi_ec_unblock_transactions_early();
605 static void acpi_pm_thaw(void)
607 acpi_ec_unblock_transactions();
608 acpi_enable_all_runtime_gpes();
611 static const struct platform_hibernation_ops acpi_hibernation_ops = {
612 .begin = acpi_hibernation_begin,
614 .pre_snapshot = acpi_pm_prepare,
615 .finish = acpi_pm_finish,
616 .prepare = acpi_pm_prepare,
617 .enter = acpi_hibernation_enter,
618 .leave = acpi_hibernation_leave,
619 .pre_restore = acpi_pm_freeze,
620 .restore_cleanup = acpi_pm_thaw,
624 * acpi_hibernation_begin_old - Set the target system sleep state to
625 * ACPI_STATE_S4 and execute the _PTS control method. This
626 * function is used if the pre-ACPI 2.0 suspend ordering has been
629 static int acpi_hibernation_begin_old(void)
633 * The _TTS object should always be evaluated before the _PTS object.
634 * When the old_suspended_ordering is true, the _PTS object is
635 * evaluated in the acpi_sleep_prepare.
637 acpi_sleep_tts_switch(ACPI_STATE_S4);
639 error = acpi_sleep_prepare(ACPI_STATE_S4);
643 error = suspend_nvs_alloc();
645 acpi_target_sleep_state = ACPI_STATE_S4;
651 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
654 static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
655 .begin = acpi_hibernation_begin_old,
657 .pre_snapshot = acpi_pm_pre_suspend,
658 .prepare = acpi_pm_freeze,
659 .finish = acpi_pm_finish,
660 .enter = acpi_hibernation_enter,
661 .leave = acpi_hibernation_leave,
662 .pre_restore = acpi_pm_freeze,
663 .restore_cleanup = acpi_pm_thaw,
664 .recover = acpi_pm_finish,
666 #endif /* CONFIG_HIBERNATION */
668 int acpi_suspend(u32 acpi_state)
670 suspend_state_t states[] = {
671 [1] = PM_SUSPEND_STANDBY,
672 [3] = PM_SUSPEND_MEM,
676 if (acpi_state < 6 && states[acpi_state])
677 return pm_suspend(states[acpi_state]);
685 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
686 * @dev: Device whose preferred target power state to return.
687 * @d_min_p: Location to store the upper limit of the allowed states range.
688 * @d_max_in: Deepest low-power state to take into consideration.
689 * Return value: Preferred power state of the device on success, -ENODEV
690 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
692 * The caller must ensure that @dev is valid before using this function.
694 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
696 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
697 struct acpi_device *adev;
699 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
700 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
704 return acpi_device_power_state(dev, adev, acpi_target_sleep_state,
707 EXPORT_SYMBOL(acpi_pm_device_sleep_state);
708 #endif /* CONFIG_PM */
710 #ifdef CONFIG_PM_SLEEP
712 * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
713 * @dev: Device to enable/desible to wake up the system from sleep states.
714 * @enable: Whether to enable or disable @dev to wake up the system.
716 int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
719 struct acpi_device *adev;
722 if (!device_can_wakeup(dev))
725 handle = DEVICE_ACPI_HANDLE(dev);
726 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
727 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
731 error = __acpi_device_sleep_wake(adev, acpi_target_sleep_state, enable);
733 dev_info(dev, "System wakeup %s by ACPI\n",
734 enable ? "enabled" : "disabled");
738 #endif /* CONFIG_PM_SLEEP */
740 static void acpi_power_off_prepare(void)
742 /* Prepare to power off the system */
743 acpi_sleep_prepare(ACPI_STATE_S5);
744 acpi_disable_all_gpes();
747 static void acpi_power_off(void)
749 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
750 printk(KERN_DEBUG "%s called\n", __func__);
752 acpi_enter_sleep_state(ACPI_STATE_S5);
755 int __init acpi_sleep_init(void)
759 #ifdef CONFIG_SUSPEND
762 dmi_check_system(acpisleep_dmi_table);
768 sleep_states[ACPI_STATE_S0] = 1;
769 printk(KERN_INFO PREFIX "(supports S0");
771 #ifdef CONFIG_SUSPEND
772 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
773 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
774 if (ACPI_SUCCESS(status)) {
776 printk(KERN_CONT " S%d", i);
780 suspend_set_ops(old_suspend_ordering ?
781 &acpi_suspend_ops_old : &acpi_suspend_ops);
784 #ifdef CONFIG_HIBERNATION
785 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
786 if (ACPI_SUCCESS(status)) {
787 hibernation_set_ops(old_suspend_ordering ?
788 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
789 sleep_states[ACPI_STATE_S4] = 1;
790 printk(KERN_CONT " S4");
792 acpi_get_table(ACPI_SIG_FACS, 1,
793 (struct acpi_table_header **)&facs);
795 s4_hardware_signature =
796 facs->hardware_signature;
800 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
801 if (ACPI_SUCCESS(status)) {
802 sleep_states[ACPI_STATE_S5] = 1;
803 printk(KERN_CONT " S5");
804 pm_power_off_prepare = acpi_power_off_prepare;
805 pm_power_off = acpi_power_off;
807 printk(KERN_CONT ")\n");
809 * Register the tts_notifier to reboot notifier list so that the _TTS
810 * object can also be evaluated when the system enters S5.
812 register_reboot_notifier(&tts_notifier);