Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * sleep.c - ACPI sleep support.
0004  *
0005  * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
0006  * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
0007  * Copyright (c) 2000-2003 Patrick Mochel
0008  * Copyright (c) 2003 Open Source Development Lab
0009  */
0010 
0011 #define pr_fmt(fmt) "ACPI: PM: " fmt
0012 
0013 #include <linux/delay.h>
0014 #include <linux/irq.h>
0015 #include <linux/dmi.h>
0016 #include <linux/device.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/suspend.h>
0019 #include <linux/reboot.h>
0020 #include <linux/acpi.h>
0021 #include <linux/module.h>
0022 #include <linux/syscore_ops.h>
0023 #include <asm/io.h>
0024 #include <trace/events/power.h>
0025 
0026 #include "internal.h"
0027 #include "sleep.h"
0028 
0029 /*
0030  * Some HW-full platforms do not have _S5, so they may need
0031  * to leverage efi power off for a shutdown.
0032  */
0033 bool acpi_no_s5;
0034 static u8 sleep_states[ACPI_S_STATE_COUNT];
0035 
0036 static void acpi_sleep_tts_switch(u32 acpi_state)
0037 {
0038     acpi_status status;
0039 
0040     status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
0041     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
0042         /*
0043          * OS can't evaluate the _TTS object correctly. Some warning
0044          * message will be printed. But it won't break anything.
0045          */
0046         pr_notice("Failure in evaluating _TTS object\n");
0047     }
0048 }
0049 
0050 static int tts_notify_reboot(struct notifier_block *this,
0051             unsigned long code, void *x)
0052 {
0053     acpi_sleep_tts_switch(ACPI_STATE_S5);
0054     return NOTIFY_DONE;
0055 }
0056 
0057 static struct notifier_block tts_notifier = {
0058     .notifier_call  = tts_notify_reboot,
0059     .next       = NULL,
0060     .priority   = 0,
0061 };
0062 
0063 static int acpi_sleep_prepare(u32 acpi_state)
0064 {
0065 #ifdef CONFIG_ACPI_SLEEP
0066     unsigned long acpi_wakeup_address;
0067 
0068     /* do we have a wakeup address for S2 and S3? */
0069     if (acpi_state == ACPI_STATE_S3) {
0070         acpi_wakeup_address = acpi_get_wakeup_address();
0071         if (!acpi_wakeup_address)
0072             return -EFAULT;
0073         acpi_set_waking_vector(acpi_wakeup_address);
0074 
0075     }
0076 #endif
0077     pr_info("Preparing to enter system sleep state S%d\n", acpi_state);
0078     acpi_enable_wakeup_devices(acpi_state);
0079     acpi_enter_sleep_state_prep(acpi_state);
0080     return 0;
0081 }
0082 
0083 bool acpi_sleep_state_supported(u8 sleep_state)
0084 {
0085     acpi_status status;
0086     u8 type_a, type_b;
0087 
0088     status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
0089     return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
0090         || (acpi_gbl_FADT.sleep_control.address
0091             && acpi_gbl_FADT.sleep_status.address));
0092 }
0093 
0094 #ifdef CONFIG_ACPI_SLEEP
0095 static u32 acpi_target_sleep_state = ACPI_STATE_S0;
0096 
0097 u32 acpi_target_system_state(void)
0098 {
0099     return acpi_target_sleep_state;
0100 }
0101 EXPORT_SYMBOL_GPL(acpi_target_system_state);
0102 
0103 static bool pwr_btn_event_pending;
0104 
0105 /*
0106  * The ACPI specification wants us to save NVS memory regions during hibernation
0107  * and to restore them during the subsequent resume.  Windows does that also for
0108  * suspend to RAM.  However, it is known that this mechanism does not work on
0109  * all machines, so we allow the user to disable it with the help of the
0110  * 'acpi_sleep=nonvs' kernel command line option.
0111  */
0112 static bool nvs_nosave;
0113 
0114 void __init acpi_nvs_nosave(void)
0115 {
0116     nvs_nosave = true;
0117 }
0118 
0119 /*
0120  * The ACPI specification wants us to save NVS memory regions during hibernation
0121  * but says nothing about saving NVS during S3.  Not all versions of Windows
0122  * save NVS on S3 suspend either, and it is clear that not all systems need
0123  * NVS to be saved at S3 time.  To improve suspend/resume time, allow the
0124  * user to disable saving NVS on S3 if their system does not require it, but
0125  * continue to save/restore NVS for S4 as specified.
0126  */
0127 static bool nvs_nosave_s3;
0128 
0129 void __init acpi_nvs_nosave_s3(void)
0130 {
0131     nvs_nosave_s3 = true;
0132 }
0133 
0134 static int __init init_nvs_save_s3(const struct dmi_system_id *d)
0135 {
0136     nvs_nosave_s3 = false;
0137     return 0;
0138 }
0139 
0140 /*
0141  * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
0142  * user to request that behavior by using the 'acpi_old_suspend_ordering'
0143  * kernel command line option that causes the following variable to be set.
0144  */
0145 static bool old_suspend_ordering;
0146 
0147 void __init acpi_old_suspend_ordering(void)
0148 {
0149     old_suspend_ordering = true;
0150 }
0151 
0152 static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
0153 {
0154     acpi_old_suspend_ordering();
0155     return 0;
0156 }
0157 
0158 static int __init init_nvs_nosave(const struct dmi_system_id *d)
0159 {
0160     acpi_nvs_nosave();
0161     return 0;
0162 }
0163 
0164 bool acpi_sleep_default_s3;
0165 
0166 static int __init init_default_s3(const struct dmi_system_id *d)
0167 {
0168     acpi_sleep_default_s3 = true;
0169     return 0;
0170 }
0171 
0172 static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
0173     {
0174     .callback = init_old_suspend_ordering,
0175     .ident = "Abit KN9 (nForce4 variant)",
0176     .matches = {
0177         DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
0178         DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
0179         },
0180     },
0181     {
0182     .callback = init_old_suspend_ordering,
0183     .ident = "HP xw4600 Workstation",
0184     .matches = {
0185         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
0186         DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
0187         },
0188     },
0189     {
0190     .callback = init_old_suspend_ordering,
0191     .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
0192     .matches = {
0193         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
0194         DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
0195         },
0196     },
0197     {
0198     .callback = init_old_suspend_ordering,
0199     .ident = "Panasonic CF51-2L",
0200     .matches = {
0201         DMI_MATCH(DMI_BOARD_VENDOR,
0202                 "Matsushita Electric Industrial Co.,Ltd."),
0203         DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
0204         },
0205     },
0206     {
0207     .callback = init_nvs_nosave,
0208     .ident = "Sony Vaio VGN-FW41E_H",
0209     .matches = {
0210         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0211         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
0212         },
0213     },
0214     {
0215     .callback = init_nvs_nosave,
0216     .ident = "Sony Vaio VGN-FW21E",
0217     .matches = {
0218         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0219         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
0220         },
0221     },
0222     {
0223     .callback = init_nvs_nosave,
0224     .ident = "Sony Vaio VGN-FW21M",
0225     .matches = {
0226         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0227         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
0228         },
0229     },
0230     {
0231     .callback = init_nvs_nosave,
0232     .ident = "Sony Vaio VPCEB17FX",
0233     .matches = {
0234         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0235         DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
0236         },
0237     },
0238     {
0239     .callback = init_nvs_nosave,
0240     .ident = "Sony Vaio VGN-SR11M",
0241     .matches = {
0242         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0243         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
0244         },
0245     },
0246     {
0247     .callback = init_nvs_nosave,
0248     .ident = "Everex StepNote Series",
0249     .matches = {
0250         DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
0251         DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
0252         },
0253     },
0254     {
0255     .callback = init_nvs_nosave,
0256     .ident = "Sony Vaio VPCEB1Z1E",
0257     .matches = {
0258         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0259         DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
0260         },
0261     },
0262     {
0263     .callback = init_nvs_nosave,
0264     .ident = "Sony Vaio VGN-NW130D",
0265     .matches = {
0266         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0267         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
0268         },
0269     },
0270     {
0271     .callback = init_nvs_nosave,
0272     .ident = "Sony Vaio VPCCW29FX",
0273     .matches = {
0274         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0275         DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
0276         },
0277     },
0278     {
0279     .callback = init_nvs_nosave,
0280     .ident = "Averatec AV1020-ED2",
0281     .matches = {
0282         DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
0283         DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
0284         },
0285     },
0286     {
0287     .callback = init_old_suspend_ordering,
0288     .ident = "Asus A8N-SLI DELUXE",
0289     .matches = {
0290         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
0291         DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
0292         },
0293     },
0294     {
0295     .callback = init_old_suspend_ordering,
0296     .ident = "Asus A8N-SLI Premium",
0297     .matches = {
0298         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
0299         DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
0300         },
0301     },
0302     {
0303     .callback = init_nvs_nosave,
0304     .ident = "Sony Vaio VGN-SR26GN_P",
0305     .matches = {
0306         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0307         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
0308         },
0309     },
0310     {
0311     .callback = init_nvs_nosave,
0312     .ident = "Sony Vaio VPCEB1S1E",
0313     .matches = {
0314         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0315         DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
0316         },
0317     },
0318     {
0319     .callback = init_nvs_nosave,
0320     .ident = "Sony Vaio VGN-FW520F",
0321     .matches = {
0322         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
0323         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
0324         },
0325     },
0326     {
0327     .callback = init_nvs_nosave,
0328     .ident = "Asus K54C",
0329     .matches = {
0330         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
0331         DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
0332         },
0333     },
0334     {
0335     .callback = init_nvs_nosave,
0336     .ident = "Asus K54HR",
0337     .matches = {
0338         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
0339         DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
0340         },
0341     },
0342     {
0343     .callback = init_nvs_save_s3,
0344     .ident = "Asus 1025C",
0345     .matches = {
0346         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
0347         DMI_MATCH(DMI_PRODUCT_NAME, "1025C"),
0348         },
0349     },
0350     /*
0351      * https://bugzilla.kernel.org/show_bug.cgi?id=189431
0352      * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
0353      * saving during S3.
0354      */
0355     {
0356     .callback = init_nvs_save_s3,
0357     .ident = "Lenovo G50-45",
0358     .matches = {
0359         DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
0360         DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
0361         },
0362     },
0363     {
0364     .callback = init_nvs_save_s3,
0365     .ident = "Lenovo G40-45",
0366     .matches = {
0367         DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
0368         DMI_MATCH(DMI_PRODUCT_NAME, "80E1"),
0369         },
0370     },
0371     /*
0372      * ThinkPad X1 Tablet(2016) cannot do suspend-to-idle using
0373      * the Low Power S0 Idle firmware interface (see
0374      * https://bugzilla.kernel.org/show_bug.cgi?id=199057).
0375      */
0376     {
0377     .callback = init_default_s3,
0378     .ident = "ThinkPad X1 Tablet(2016)",
0379     .matches = {
0380         DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
0381         DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
0382         },
0383     },
0384     /*
0385      * ASUS B1400CEAE hangs on resume from suspend (see
0386      * https://bugzilla.kernel.org/show_bug.cgi?id=215742).
0387      */
0388     {
0389     .callback = init_default_s3,
0390     .ident = "ASUS B1400CEAE",
0391     .matches = {
0392         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
0393         DMI_MATCH(DMI_PRODUCT_NAME, "ASUS EXPERTBOOK B1400CEAE"),
0394         },
0395     },
0396     {},
0397 };
0398 
0399 static bool ignore_blacklist;
0400 
0401 void __init acpi_sleep_no_blacklist(void)
0402 {
0403     ignore_blacklist = true;
0404 }
0405 
0406 static void __init acpi_sleep_dmi_check(void)
0407 {
0408     if (ignore_blacklist)
0409         return;
0410 
0411     if (dmi_get_bios_year() >= 2012)
0412         acpi_nvs_nosave_s3();
0413 
0414     dmi_check_system(acpisleep_dmi_table);
0415 }
0416 
0417 /**
0418  * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
0419  */
0420 static int acpi_pm_freeze(void)
0421 {
0422     acpi_disable_all_gpes();
0423     acpi_os_wait_events_complete();
0424     acpi_ec_block_transactions();
0425     return 0;
0426 }
0427 
0428 /**
0429  * acpi_pm_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
0430  */
0431 static int acpi_pm_pre_suspend(void)
0432 {
0433     acpi_pm_freeze();
0434     return suspend_nvs_save();
0435 }
0436 
0437 /**
0438  *  __acpi_pm_prepare - Prepare the platform to enter the target state.
0439  *
0440  *  If necessary, set the firmware waking vector and do arch-specific
0441  *  nastiness to get the wakeup code to the waking vector.
0442  */
0443 static int __acpi_pm_prepare(void)
0444 {
0445     int error = acpi_sleep_prepare(acpi_target_sleep_state);
0446     if (error)
0447         acpi_target_sleep_state = ACPI_STATE_S0;
0448 
0449     return error;
0450 }
0451 
0452 /**
0453  *  acpi_pm_prepare - Prepare the platform to enter the target sleep
0454  *      state and disable the GPEs.
0455  */
0456 static int acpi_pm_prepare(void)
0457 {
0458     int error = __acpi_pm_prepare();
0459     if (!error)
0460         error = acpi_pm_pre_suspend();
0461 
0462     return error;
0463 }
0464 
0465 /**
0466  *  acpi_pm_finish - Instruct the platform to leave a sleep state.
0467  *
0468  *  This is called after we wake back up (or if entering the sleep state
0469  *  failed).
0470  */
0471 static void acpi_pm_finish(void)
0472 {
0473     struct acpi_device *pwr_btn_adev;
0474     u32 acpi_state = acpi_target_sleep_state;
0475 
0476     acpi_ec_unblock_transactions();
0477     suspend_nvs_free();
0478 
0479     if (acpi_state == ACPI_STATE_S0)
0480         return;
0481 
0482     pr_info("Waking up from system sleep state S%d\n", acpi_state);
0483     acpi_disable_wakeup_devices(acpi_state);
0484     acpi_leave_sleep_state(acpi_state);
0485 
0486     /* reset firmware waking vector */
0487     acpi_set_waking_vector(0);
0488 
0489     acpi_target_sleep_state = ACPI_STATE_S0;
0490 
0491     acpi_resume_power_resources();
0492 
0493     /* If we were woken with the fixed power button, provide a small
0494      * hint to userspace in the form of a wakeup event on the fixed power
0495      * button device (if it can be found).
0496      *
0497      * We delay the event generation til now, as the PM layer requires
0498      * timekeeping to be running before we generate events. */
0499     if (!pwr_btn_event_pending)
0500         return;
0501 
0502     pwr_btn_event_pending = false;
0503     pwr_btn_adev = acpi_dev_get_first_match_dev(ACPI_BUTTON_HID_POWERF,
0504                             NULL, -1);
0505     if (pwr_btn_adev) {
0506         pm_wakeup_event(&pwr_btn_adev->dev, 0);
0507         acpi_dev_put(pwr_btn_adev);
0508     }
0509 }
0510 
0511 /**
0512  * acpi_pm_start - Start system PM transition.
0513  */
0514 static void acpi_pm_start(u32 acpi_state)
0515 {
0516     acpi_target_sleep_state = acpi_state;
0517     acpi_sleep_tts_switch(acpi_target_sleep_state);
0518     acpi_scan_lock_acquire();
0519 }
0520 
0521 /**
0522  * acpi_pm_end - Finish up system PM transition.
0523  */
0524 static void acpi_pm_end(void)
0525 {
0526     acpi_turn_off_unused_power_resources();
0527     acpi_scan_lock_release();
0528     /*
0529      * This is necessary in case acpi_pm_finish() is not called during a
0530      * failing transition to a sleep state.
0531      */
0532     acpi_target_sleep_state = ACPI_STATE_S0;
0533     acpi_sleep_tts_switch(acpi_target_sleep_state);
0534 }
0535 #else /* !CONFIG_ACPI_SLEEP */
0536 #define sleep_no_lps0   (1)
0537 #define acpi_target_sleep_state ACPI_STATE_S0
0538 #define acpi_sleep_default_s3   (1)
0539 static inline void acpi_sleep_dmi_check(void) {}
0540 #endif /* CONFIG_ACPI_SLEEP */
0541 
0542 #ifdef CONFIG_SUSPEND
0543 static u32 acpi_suspend_states[] = {
0544     [PM_SUSPEND_ON] = ACPI_STATE_S0,
0545     [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
0546     [PM_SUSPEND_MEM] = ACPI_STATE_S3,
0547     [PM_SUSPEND_MAX] = ACPI_STATE_S5
0548 };
0549 
0550 /**
0551  *  acpi_suspend_begin - Set the target system sleep state to the state
0552  *      associated with given @pm_state, if supported.
0553  */
0554 static int acpi_suspend_begin(suspend_state_t pm_state)
0555 {
0556     u32 acpi_state = acpi_suspend_states[pm_state];
0557     int error;
0558 
0559     error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
0560     if (error)
0561         return error;
0562 
0563     if (!sleep_states[acpi_state]) {
0564         pr_err("ACPI does not support sleep state S%u\n", acpi_state);
0565         return -ENOSYS;
0566     }
0567     if (acpi_state > ACPI_STATE_S1)
0568         pm_set_suspend_via_firmware();
0569 
0570     acpi_pm_start(acpi_state);
0571     return 0;
0572 }
0573 
0574 /**
0575  *  acpi_suspend_enter - Actually enter a sleep state.
0576  *  @pm_state: ignored
0577  *
0578  *  Flush caches and go to sleep. For STR we have to call arch-specific
0579  *  assembly, which in turn call acpi_enter_sleep_state().
0580  *  It's unfortunate, but it works. Please fix if you're feeling frisky.
0581  */
0582 static int acpi_suspend_enter(suspend_state_t pm_state)
0583 {
0584     acpi_status status = AE_OK;
0585     u32 acpi_state = acpi_target_sleep_state;
0586     int error;
0587 
0588     trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
0589     switch (acpi_state) {
0590     case ACPI_STATE_S1:
0591         barrier();
0592         status = acpi_enter_sleep_state(acpi_state);
0593         break;
0594 
0595     case ACPI_STATE_S3:
0596         if (!acpi_suspend_lowlevel)
0597             return -ENOSYS;
0598         error = acpi_suspend_lowlevel();
0599         if (error)
0600             return error;
0601         pr_info("Low-level resume complete\n");
0602         pm_set_resume_via_firmware();
0603         break;
0604     }
0605     trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
0606 
0607     /* This violates the spec but is required for bug compatibility. */
0608     acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
0609 
0610     /* Reprogram control registers */
0611     acpi_leave_sleep_state_prep(acpi_state);
0612 
0613     /* ACPI 3.0 specs (P62) says that it's the responsibility
0614      * of the OSPM to clear the status bit [ implying that the
0615      * POWER_BUTTON event should not reach userspace ]
0616      *
0617      * However, we do generate a small hint for userspace in the form of
0618      * a wakeup event. We flag this condition for now and generate the
0619      * event later, as we're currently too early in resume to be able to
0620      * generate wakeup events.
0621      */
0622     if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
0623         acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
0624 
0625         acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
0626 
0627         if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
0628             acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
0629             /* Flag for later */
0630             pwr_btn_event_pending = true;
0631         }
0632     }
0633 
0634     /*
0635      * Disable and clear GPE status before interrupt is enabled. Some GPEs
0636      * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
0637      * acpi_leave_sleep_state will reenable specific GPEs later
0638      */
0639     acpi_disable_all_gpes();
0640     /* Allow EC transactions to happen. */
0641     acpi_ec_unblock_transactions();
0642 
0643     suspend_nvs_restore();
0644 
0645     return ACPI_SUCCESS(status) ? 0 : -EFAULT;
0646 }
0647 
0648 static int acpi_suspend_state_valid(suspend_state_t pm_state)
0649 {
0650     u32 acpi_state;
0651 
0652     switch (pm_state) {
0653     case PM_SUSPEND_ON:
0654     case PM_SUSPEND_STANDBY:
0655     case PM_SUSPEND_MEM:
0656         acpi_state = acpi_suspend_states[pm_state];
0657 
0658         return sleep_states[acpi_state];
0659     default:
0660         return 0;
0661     }
0662 }
0663 
0664 static const struct platform_suspend_ops acpi_suspend_ops = {
0665     .valid = acpi_suspend_state_valid,
0666     .begin = acpi_suspend_begin,
0667     .prepare_late = acpi_pm_prepare,
0668     .enter = acpi_suspend_enter,
0669     .wake = acpi_pm_finish,
0670     .end = acpi_pm_end,
0671 };
0672 
0673 /**
0674  *  acpi_suspend_begin_old - Set the target system sleep state to the
0675  *      state associated with given @pm_state, if supported, and
0676  *      execute the _PTS control method.  This function is used if the
0677  *      pre-ACPI 2.0 suspend ordering has been requested.
0678  */
0679 static int acpi_suspend_begin_old(suspend_state_t pm_state)
0680 {
0681     int error = acpi_suspend_begin(pm_state);
0682     if (!error)
0683         error = __acpi_pm_prepare();
0684 
0685     return error;
0686 }
0687 
0688 /*
0689  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
0690  * been requested.
0691  */
0692 static const struct platform_suspend_ops acpi_suspend_ops_old = {
0693     .valid = acpi_suspend_state_valid,
0694     .begin = acpi_suspend_begin_old,
0695     .prepare_late = acpi_pm_pre_suspend,
0696     .enter = acpi_suspend_enter,
0697     .wake = acpi_pm_finish,
0698     .end = acpi_pm_end,
0699     .recover = acpi_pm_finish,
0700 };
0701 
0702 static bool s2idle_wakeup;
0703 
0704 int acpi_s2idle_begin(void)
0705 {
0706     acpi_scan_lock_acquire();
0707     return 0;
0708 }
0709 
0710 int acpi_s2idle_prepare(void)
0711 {
0712     if (acpi_sci_irq_valid()) {
0713         enable_irq_wake(acpi_sci_irq);
0714         acpi_ec_set_gpe_wake_mask(ACPI_GPE_ENABLE);
0715     }
0716 
0717     acpi_enable_wakeup_devices(ACPI_STATE_S0);
0718 
0719     /* Change the configuration of GPEs to avoid spurious wakeup. */
0720     acpi_enable_all_wakeup_gpes();
0721     acpi_os_wait_events_complete();
0722 
0723     s2idle_wakeup = true;
0724     return 0;
0725 }
0726 
0727 bool acpi_s2idle_wake(void)
0728 {
0729     if (!acpi_sci_irq_valid())
0730         return pm_wakeup_pending();
0731 
0732     while (pm_wakeup_pending()) {
0733         /*
0734          * If IRQD_WAKEUP_ARMED is set for the SCI at this point, the
0735          * SCI has not triggered while suspended, so bail out (the
0736          * wakeup is pending anyway and the SCI is not the source of
0737          * it).
0738          */
0739         if (irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq))) {
0740             pm_pr_dbg("Wakeup unrelated to ACPI SCI\n");
0741             return true;
0742         }
0743 
0744         /*
0745          * If the status bit of any enabled fixed event is set, the
0746          * wakeup is regarded as valid.
0747          */
0748         if (acpi_any_fixed_event_status_set()) {
0749             pm_pr_dbg("ACPI fixed event wakeup\n");
0750             return true;
0751         }
0752 
0753         /* Check wakeups from drivers sharing the SCI. */
0754         if (acpi_check_wakeup_handlers()) {
0755             pm_pr_dbg("ACPI custom handler wakeup\n");
0756             return true;
0757         }
0758 
0759         /*
0760          * Check non-EC GPE wakeups and if there are none, cancel the
0761          * SCI-related wakeup and dispatch the EC GPE.
0762          */
0763         if (acpi_ec_dispatch_gpe()) {
0764             pm_pr_dbg("ACPI non-EC GPE wakeup\n");
0765             return true;
0766         }
0767 
0768         acpi_os_wait_events_complete();
0769 
0770         /*
0771          * The SCI is in the "suspended" state now and it cannot produce
0772          * new wakeup events till the rearming below, so if any of them
0773          * are pending here, they must be resulting from the processing
0774          * of EC events above or coming from somewhere else.
0775          */
0776         if (pm_wakeup_pending()) {
0777             pm_pr_dbg("Wakeup after ACPI Notify sync\n");
0778             return true;
0779         }
0780 
0781         pm_pr_dbg("Rearming ACPI SCI for wakeup\n");
0782 
0783         pm_wakeup_clear(acpi_sci_irq);
0784         rearm_wake_irq(acpi_sci_irq);
0785     }
0786 
0787     return false;
0788 }
0789 
0790 void acpi_s2idle_restore(void)
0791 {
0792     /*
0793      * Drain pending events before restoring the working-state configuration
0794      * of GPEs.
0795      */
0796     acpi_os_wait_events_complete(); /* synchronize GPE processing */
0797     acpi_ec_flush_work(); /* flush the EC driver's workqueues */
0798     acpi_os_wait_events_complete(); /* synchronize Notify handling */
0799 
0800     s2idle_wakeup = false;
0801 
0802     acpi_enable_all_runtime_gpes();
0803 
0804     acpi_disable_wakeup_devices(ACPI_STATE_S0);
0805 
0806     if (acpi_sci_irq_valid()) {
0807         acpi_ec_set_gpe_wake_mask(ACPI_GPE_DISABLE);
0808         disable_irq_wake(acpi_sci_irq);
0809     }
0810 }
0811 
0812 void acpi_s2idle_end(void)
0813 {
0814     acpi_scan_lock_release();
0815 }
0816 
0817 static const struct platform_s2idle_ops acpi_s2idle_ops = {
0818     .begin = acpi_s2idle_begin,
0819     .prepare = acpi_s2idle_prepare,
0820     .wake = acpi_s2idle_wake,
0821     .restore = acpi_s2idle_restore,
0822     .end = acpi_s2idle_end,
0823 };
0824 
0825 void __weak acpi_s2idle_setup(void)
0826 {
0827     if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
0828         pr_info("Efficient low-power S0 idle declared\n");
0829 
0830     s2idle_set_ops(&acpi_s2idle_ops);
0831 }
0832 
0833 static void acpi_sleep_suspend_setup(void)
0834 {
0835     bool suspend_ops_needed = false;
0836     int i;
0837 
0838     for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
0839         if (acpi_sleep_state_supported(i)) {
0840             sleep_states[i] = 1;
0841             suspend_ops_needed = true;
0842         }
0843 
0844     if (suspend_ops_needed)
0845         suspend_set_ops(old_suspend_ordering ?
0846                 &acpi_suspend_ops_old : &acpi_suspend_ops);
0847 
0848     acpi_s2idle_setup();
0849 }
0850 
0851 #else /* !CONFIG_SUSPEND */
0852 #define s2idle_wakeup       (false)
0853 static inline void acpi_sleep_suspend_setup(void) {}
0854 #endif /* !CONFIG_SUSPEND */
0855 
0856 bool acpi_s2idle_wakeup(void)
0857 {
0858     return s2idle_wakeup;
0859 }
0860 
0861 #ifdef CONFIG_PM_SLEEP
0862 static u32 saved_bm_rld;
0863 
0864 static int  acpi_save_bm_rld(void)
0865 {
0866     acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
0867     return 0;
0868 }
0869 
0870 static void  acpi_restore_bm_rld(void)
0871 {
0872     u32 resumed_bm_rld = 0;
0873 
0874     acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
0875     if (resumed_bm_rld == saved_bm_rld)
0876         return;
0877 
0878     acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
0879 }
0880 
0881 static struct syscore_ops acpi_sleep_syscore_ops = {
0882     .suspend = acpi_save_bm_rld,
0883     .resume = acpi_restore_bm_rld,
0884 };
0885 
0886 static void acpi_sleep_syscore_init(void)
0887 {
0888     register_syscore_ops(&acpi_sleep_syscore_ops);
0889 }
0890 #else
0891 static inline void acpi_sleep_syscore_init(void) {}
0892 #endif /* CONFIG_PM_SLEEP */
0893 
0894 #ifdef CONFIG_HIBERNATION
0895 static unsigned long s4_hardware_signature;
0896 static struct acpi_table_facs *facs;
0897 int acpi_check_s4_hw_signature = -1; /* Default behaviour is just to warn */
0898 
0899 static int acpi_hibernation_begin(pm_message_t stage)
0900 {
0901     if (!nvs_nosave) {
0902         int error = suspend_nvs_alloc();
0903         if (error)
0904             return error;
0905     }
0906 
0907     if (stage.event == PM_EVENT_HIBERNATE)
0908         pm_set_suspend_via_firmware();
0909 
0910     acpi_pm_start(ACPI_STATE_S4);
0911     return 0;
0912 }
0913 
0914 static int acpi_hibernation_enter(void)
0915 {
0916     acpi_status status = AE_OK;
0917 
0918     /* This shouldn't return.  If it returns, we have a problem */
0919     status = acpi_enter_sleep_state(ACPI_STATE_S4);
0920     /* Reprogram control registers */
0921     acpi_leave_sleep_state_prep(ACPI_STATE_S4);
0922 
0923     return ACPI_SUCCESS(status) ? 0 : -EFAULT;
0924 }
0925 
0926 static void acpi_hibernation_leave(void)
0927 {
0928     pm_set_resume_via_firmware();
0929     /*
0930      * If ACPI is not enabled by the BIOS and the boot kernel, we need to
0931      * enable it here.
0932      */
0933     acpi_enable();
0934     /* Reprogram control registers */
0935     acpi_leave_sleep_state_prep(ACPI_STATE_S4);
0936     /* Check the hardware signature */
0937     if (facs && s4_hardware_signature != facs->hardware_signature)
0938         pr_crit("Hardware changed while hibernated, success doubtful!\n");
0939     /* Restore the NVS memory area */
0940     suspend_nvs_restore();
0941     /* Allow EC transactions to happen. */
0942     acpi_ec_unblock_transactions();
0943 }
0944 
0945 static void acpi_pm_thaw(void)
0946 {
0947     acpi_ec_unblock_transactions();
0948     acpi_enable_all_runtime_gpes();
0949 }
0950 
0951 static const struct platform_hibernation_ops acpi_hibernation_ops = {
0952     .begin = acpi_hibernation_begin,
0953     .end = acpi_pm_end,
0954     .pre_snapshot = acpi_pm_prepare,
0955     .finish = acpi_pm_finish,
0956     .prepare = acpi_pm_prepare,
0957     .enter = acpi_hibernation_enter,
0958     .leave = acpi_hibernation_leave,
0959     .pre_restore = acpi_pm_freeze,
0960     .restore_cleanup = acpi_pm_thaw,
0961 };
0962 
0963 /**
0964  *  acpi_hibernation_begin_old - Set the target system sleep state to
0965  *      ACPI_STATE_S4 and execute the _PTS control method.  This
0966  *      function is used if the pre-ACPI 2.0 suspend ordering has been
0967  *      requested.
0968  */
0969 static int acpi_hibernation_begin_old(pm_message_t stage)
0970 {
0971     int error;
0972     /*
0973      * The _TTS object should always be evaluated before the _PTS object.
0974      * When the old_suspended_ordering is true, the _PTS object is
0975      * evaluated in the acpi_sleep_prepare.
0976      */
0977     acpi_sleep_tts_switch(ACPI_STATE_S4);
0978 
0979     error = acpi_sleep_prepare(ACPI_STATE_S4);
0980     if (error)
0981         return error;
0982 
0983     if (!nvs_nosave) {
0984         error = suspend_nvs_alloc();
0985         if (error)
0986             return error;
0987     }
0988 
0989     if (stage.event == PM_EVENT_HIBERNATE)
0990         pm_set_suspend_via_firmware();
0991 
0992     acpi_target_sleep_state = ACPI_STATE_S4;
0993     acpi_scan_lock_acquire();
0994     return 0;
0995 }
0996 
0997 /*
0998  * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
0999  * been requested.
1000  */
1001 static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
1002     .begin = acpi_hibernation_begin_old,
1003     .end = acpi_pm_end,
1004     .pre_snapshot = acpi_pm_pre_suspend,
1005     .prepare = acpi_pm_freeze,
1006     .finish = acpi_pm_finish,
1007     .enter = acpi_hibernation_enter,
1008     .leave = acpi_hibernation_leave,
1009     .pre_restore = acpi_pm_freeze,
1010     .restore_cleanup = acpi_pm_thaw,
1011     .recover = acpi_pm_finish,
1012 };
1013 
1014 static void acpi_sleep_hibernate_setup(void)
1015 {
1016     if (!acpi_sleep_state_supported(ACPI_STATE_S4))
1017         return;
1018 
1019     hibernation_set_ops(old_suspend_ordering ?
1020             &acpi_hibernation_ops_old : &acpi_hibernation_ops);
1021     sleep_states[ACPI_STATE_S4] = 1;
1022     if (!acpi_check_s4_hw_signature)
1023         return;
1024 
1025     acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
1026     if (facs) {
1027         /*
1028          * s4_hardware_signature is the local variable which is just
1029          * used to warn about mismatch after we're attempting to
1030          * resume (in violation of the ACPI specification.)
1031          */
1032         s4_hardware_signature = facs->hardware_signature;
1033 
1034         if (acpi_check_s4_hw_signature > 0) {
1035             /*
1036              * If we're actually obeying the ACPI specification
1037              * then the signature is written out as part of the
1038              * swsusp header, in order to allow the boot kernel
1039              * to gracefully decline to resume.
1040              */
1041             swsusp_hardware_signature = facs->hardware_signature;
1042         }
1043     }
1044 }
1045 #else /* !CONFIG_HIBERNATION */
1046 static inline void acpi_sleep_hibernate_setup(void) {}
1047 #endif /* !CONFIG_HIBERNATION */
1048 
1049 static int acpi_power_off_prepare(struct sys_off_data *data)
1050 {
1051     /* Prepare to power off the system */
1052     acpi_sleep_prepare(ACPI_STATE_S5);
1053     acpi_disable_all_gpes();
1054     acpi_os_wait_events_complete();
1055     return NOTIFY_DONE;
1056 }
1057 
1058 static int acpi_power_off(struct sys_off_data *data)
1059 {
1060     /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
1061     pr_debug("%s called\n", __func__);
1062     local_irq_disable();
1063     acpi_enter_sleep_state(ACPI_STATE_S5);
1064     return NOTIFY_DONE;
1065 }
1066 
1067 int __init acpi_sleep_init(void)
1068 {
1069     char supported[ACPI_S_STATE_COUNT * 3 + 1];
1070     char *pos = supported;
1071     int i;
1072 
1073     acpi_sleep_dmi_check();
1074 
1075     sleep_states[ACPI_STATE_S0] = 1;
1076 
1077     acpi_sleep_syscore_init();
1078     acpi_sleep_suspend_setup();
1079     acpi_sleep_hibernate_setup();
1080 
1081     if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
1082         sleep_states[ACPI_STATE_S5] = 1;
1083 
1084         register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE,
1085                      SYS_OFF_PRIO_FIRMWARE,
1086                      acpi_power_off_prepare, NULL);
1087 
1088         register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
1089                      SYS_OFF_PRIO_FIRMWARE,
1090                      acpi_power_off, NULL);
1091     } else {
1092         acpi_no_s5 = true;
1093     }
1094 
1095     supported[0] = 0;
1096     for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
1097         if (sleep_states[i])
1098             pos += sprintf(pos, " S%d", i);
1099     }
1100     pr_info("(supports%s)\n", supported);
1101 
1102     /*
1103      * Register the tts_notifier to reboot notifier list so that the _TTS
1104      * object can also be evaluated when the system enters S5.
1105      */
1106     register_reboot_notifier(&tts_notifier);
1107     return 0;
1108 }