Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sleep.c - ACPI sleep support. |
| 3 | * |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 4 | * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/suspend.h> |
Zhao Yakui | e49f711 | 2008-08-12 10:20:22 +0800 | [diff] [blame] | 18 | #include <linux/reboot.h> |
H. Peter Anvin | d1ee433 | 2011-02-14 15:42:46 -0800 | [diff] [blame] | 19 | #include <linux/acpi.h> |
Lin Ming | a2ef5c4 | 2012-03-21 10:58:46 +0800 | [diff] [blame] | 20 | #include <linux/module.h> |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 21 | #include <linux/pm_runtime.h> |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 22 | |
| 23 | #include <asm/io.h> |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <acpi/acpi_bus.h> |
| 26 | #include <acpi/acpi_drivers.h> |
Bjorn Helgaas | e60cc7a | 2009-03-13 12:08:26 -0600 | [diff] [blame] | 27 | |
| 28 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "sleep.h" |
| 30 | |
Stephen Hemminger | 01eac60 | 2010-10-18 18:47:25 -0700 | [diff] [blame] | 31 | static u8 sleep_states[ACPI_S_STATE_COUNT]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Zhao Yakui | e49f711 | 2008-08-12 10:20:22 +0800 | [diff] [blame] | 33 | static void acpi_sleep_tts_switch(u32 acpi_state) |
| 34 | { |
| 35 | union acpi_object in_arg = { ACPI_TYPE_INTEGER }; |
| 36 | struct acpi_object_list arg_list = { 1, &in_arg }; |
| 37 | acpi_status status = AE_OK; |
| 38 | |
| 39 | in_arg.integer.value = acpi_state; |
| 40 | status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL); |
| 41 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
| 42 | /* |
| 43 | * OS can't evaluate the _TTS object correctly. Some warning |
| 44 | * message will be printed. But it won't break anything. |
| 45 | */ |
| 46 | printk(KERN_NOTICE "Failure in evaluating _TTS object\n"); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static int tts_notify_reboot(struct notifier_block *this, |
| 51 | unsigned long code, void *x) |
| 52 | { |
| 53 | acpi_sleep_tts_switch(ACPI_STATE_S5); |
| 54 | return NOTIFY_DONE; |
| 55 | } |
| 56 | |
| 57 | static struct notifier_block tts_notifier = { |
| 58 | .notifier_call = tts_notify_reboot, |
| 59 | .next = NULL, |
| 60 | .priority = 0, |
| 61 | }; |
| 62 | |
Rafael J. Wysocki | c9b6c8f | 2008-01-08 00:10:57 +0100 | [diff] [blame] | 63 | static int acpi_sleep_prepare(u32 acpi_state) |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 64 | { |
| 65 | #ifdef CONFIG_ACPI_SLEEP |
| 66 | /* do we have a wakeup address for S2 and S3? */ |
| 67 | if (acpi_state == ACPI_STATE_S3) { |
H. Peter Anvin | 319b6ff | 2012-05-30 12:33:41 +0300 | [diff] [blame] | 68 | if (!acpi_wakeup_address) |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 69 | return -EFAULT; |
H. Peter Anvin | 319b6ff | 2012-05-30 12:33:41 +0300 | [diff] [blame] | 70 | acpi_set_firmware_waking_vector(acpi_wakeup_address); |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 71 | |
| 72 | } |
| 73 | ACPI_FLUSH_CPU_CACHE(); |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 74 | #endif |
Rafael J. Wysocki | c9b6c8f | 2008-01-08 00:10:57 +0100 | [diff] [blame] | 75 | printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n", |
| 76 | acpi_state); |
Rafael J. Wysocki | 78f5f02 | 2010-07-06 22:09:38 -0400 | [diff] [blame] | 77 | acpi_enable_wakeup_devices(acpi_state); |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 78 | acpi_enter_sleep_state_prep(acpi_state); |
| 79 | return 0; |
| 80 | } |
| 81 | |
Rafael J. Wysocki | 5d1e072 | 2008-10-22 14:58:43 -0400 | [diff] [blame] | 82 | #ifdef CONFIG_ACPI_SLEEP |
Jan Beulich | 091aad6 | 2010-12-07 14:52:25 +0000 | [diff] [blame] | 83 | static u32 acpi_target_sleep_state = ACPI_STATE_S0; |
Rafael J. Wysocki | a5ca734 | 2012-07-23 21:01:02 +0200 | [diff] [blame] | 84 | static bool pwr_btn_event_pending; |
Jan Beulich | 091aad6 | 2010-12-07 14:52:25 +0000 | [diff] [blame] | 85 | |
Zhang Rui | d7f0eea | 2009-12-30 15:36:42 +0800 | [diff] [blame] | 86 | /* |
Rafael J. Wysocki | 72ad5d7 | 2010-07-23 22:59:09 +0200 | [diff] [blame] | 87 | * The ACPI specification wants us to save NVS memory regions during hibernation |
| 88 | * and to restore them during the subsequent resume. Windows does that also for |
| 89 | * suspend to RAM. However, it is known that this mechanism does not work on |
| 90 | * all machines, so we allow the user to disable it with the help of the |
| 91 | * 'acpi_sleep=nonvs' kernel command line option. |
| 92 | */ |
| 93 | static bool nvs_nosave; |
| 94 | |
| 95 | void __init acpi_nvs_nosave(void) |
| 96 | { |
| 97 | nvs_nosave = true; |
| 98 | } |
| 99 | |
| 100 | /* |
Kristen Carlson Accardi | 1bad2f1 | 2012-10-26 13:39:15 +0200 | [diff] [blame] | 101 | * The ACPI specification wants us to save NVS memory regions during hibernation |
| 102 | * but says nothing about saving NVS during S3. Not all versions of Windows |
| 103 | * save NVS on S3 suspend either, and it is clear that not all systems need |
| 104 | * NVS to be saved at S3 time. To improve suspend/resume time, allow the |
| 105 | * user to disable saving NVS on S3 if their system does not require it, but |
| 106 | * continue to save/restore NVS for S4 as specified. |
| 107 | */ |
| 108 | static bool nvs_nosave_s3; |
| 109 | |
| 110 | void __init acpi_nvs_nosave_s3(void) |
| 111 | { |
| 112 | nvs_nosave_s3 = true; |
| 113 | } |
| 114 | |
| 115 | /* |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 116 | * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the |
| 117 | * user to request that behavior by using the 'acpi_old_suspend_ordering' |
| 118 | * kernel command line option that causes the following variable to be set. |
| 119 | */ |
| 120 | static bool old_suspend_ordering; |
| 121 | |
| 122 | void __init acpi_old_suspend_ordering(void) |
| 123 | { |
| 124 | old_suspend_ordering = true; |
| 125 | } |
| 126 | |
Carlos Corbacho | e41fb7c | 2008-07-23 21:28:43 -0700 | [diff] [blame] | 127 | static int __init init_old_suspend_ordering(const struct dmi_system_id *d) |
| 128 | { |
Zhang Rui | 0ac1b1d | 2012-11-30 12:57:03 +0100 | [diff] [blame^] | 129 | acpi_old_suspend_ordering(); |
Carlos Corbacho | e41fb7c | 2008-07-23 21:28:43 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Rafael J. Wysocki | 5399864 | 2010-09-24 16:46:14 -0400 | [diff] [blame] | 133 | static int __init init_nvs_nosave(const struct dmi_system_id *d) |
| 134 | { |
| 135 | acpi_nvs_nosave(); |
| 136 | return 0; |
| 137 | } |
| 138 | |
Carlos Corbacho | e41fb7c | 2008-07-23 21:28:43 -0700 | [diff] [blame] | 139 | static struct dmi_system_id __initdata acpisleep_dmi_table[] = { |
| 140 | { |
| 141 | .callback = init_old_suspend_ordering, |
| 142 | .ident = "Abit KN9 (nForce4 variant)", |
| 143 | .matches = { |
| 144 | DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"), |
| 145 | DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"), |
| 146 | }, |
| 147 | }, |
Rafael J. Wysocki | 4fb507b | 2008-10-14 22:54:06 +0200 | [diff] [blame] | 148 | { |
| 149 | .callback = init_old_suspend_ordering, |
| 150 | .ident = "HP xw4600 Workstation", |
| 151 | .matches = { |
| 152 | DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), |
| 153 | DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"), |
| 154 | }, |
| 155 | }, |
Rafael J. Wysocki | 65df784 | 2008-11-26 17:53:13 -0500 | [diff] [blame] | 156 | { |
Andy Whitcroft | a140449 | 2009-02-11 18:11:22 +0000 | [diff] [blame] | 157 | .callback = init_old_suspend_ordering, |
| 158 | .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)", |
| 159 | .matches = { |
| 160 | DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."), |
| 161 | DMI_MATCH(DMI_BOARD_NAME, "M2N8L"), |
| 162 | }, |
| 163 | }, |
Zhang Rui | 45e7798 | 2009-03-15 22:13:44 -0400 | [diff] [blame] | 164 | { |
Zhao Yakui | 2a9ef8e | 2009-03-18 16:36:25 +0800 | [diff] [blame] | 165 | .callback = init_old_suspend_ordering, |
| 166 | .ident = "Panasonic CF51-2L", |
| 167 | .matches = { |
| 168 | DMI_MATCH(DMI_BOARD_VENDOR, |
| 169 | "Matsushita Electric Industrial Co.,Ltd."), |
| 170 | DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"), |
| 171 | }, |
| 172 | }, |
Rafael J. Wysocki | 5399864 | 2010-09-24 16:46:14 -0400 | [diff] [blame] | 173 | { |
| 174 | .callback = init_nvs_nosave, |
Dave Jones | d11c78e | 2011-10-19 23:15:11 +0200 | [diff] [blame] | 175 | .ident = "Sony Vaio VGN-FW21E", |
| 176 | .matches = { |
| 177 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 178 | DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"), |
| 179 | }, |
| 180 | }, |
| 181 | { |
| 182 | .callback = init_nvs_nosave, |
Dave Jones | ddf6ce4 | 2011-11-03 00:58:59 +0100 | [diff] [blame] | 183 | .ident = "Sony Vaio VPCEB17FX", |
| 184 | .matches = { |
| 185 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 186 | DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"), |
| 187 | }, |
| 188 | }, |
| 189 | { |
| 190 | .callback = init_nvs_nosave, |
Rafael J. Wysocki | 5399864 | 2010-09-24 16:46:14 -0400 | [diff] [blame] | 191 | .ident = "Sony Vaio VGN-SR11M", |
| 192 | .matches = { |
| 193 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 194 | DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"), |
| 195 | }, |
| 196 | }, |
| 197 | { |
| 198 | .callback = init_nvs_nosave, |
| 199 | .ident = "Everex StepNote Series", |
| 200 | .matches = { |
| 201 | DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."), |
| 202 | DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"), |
| 203 | }, |
| 204 | }, |
Rafael J. Wysocki | af48931 | 2010-10-17 21:01:21 +0200 | [diff] [blame] | 205 | { |
| 206 | .callback = init_nvs_nosave, |
| 207 | .ident = "Sony Vaio VPCEB1Z1E", |
| 208 | .matches = { |
| 209 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 210 | DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"), |
| 211 | }, |
| 212 | }, |
Rafael J. Wysocki | 291a73c | 2010-12-12 21:10:42 +0100 | [diff] [blame] | 213 | { |
| 214 | .callback = init_nvs_nosave, |
| 215 | .ident = "Sony Vaio VGN-NW130D", |
| 216 | .matches = { |
| 217 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 218 | DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"), |
| 219 | }, |
| 220 | }, |
Rafael J. Wysocki | 7b33070 | 2011-01-06 23:37:01 +0100 | [diff] [blame] | 221 | { |
| 222 | .callback = init_nvs_nosave, |
Lan Tianyu | 93f7708 | 2012-01-21 09:23:56 +0800 | [diff] [blame] | 223 | .ident = "Sony Vaio VPCCW29FX", |
| 224 | .matches = { |
| 225 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 226 | DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"), |
| 227 | }, |
| 228 | }, |
| 229 | { |
| 230 | .callback = init_nvs_nosave, |
Rafael J. Wysocki | 7b33070 | 2011-01-06 23:37:01 +0100 | [diff] [blame] | 231 | .ident = "Averatec AV1020-ED2", |
| 232 | .matches = { |
| 233 | DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"), |
| 234 | DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"), |
| 235 | }, |
| 236 | }, |
Zhang Rui | bb0c5ed | 2011-07-30 01:40:48 -0400 | [diff] [blame] | 237 | { |
| 238 | .callback = init_old_suspend_ordering, |
| 239 | .ident = "Asus A8N-SLI DELUXE", |
| 240 | .matches = { |
| 241 | DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."), |
| 242 | DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"), |
| 243 | }, |
| 244 | }, |
| 245 | { |
| 246 | .callback = init_old_suspend_ordering, |
| 247 | .ident = "Asus A8N-SLI Premium", |
| 248 | .matches = { |
| 249 | DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."), |
| 250 | DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"), |
| 251 | }, |
| 252 | }, |
Rafael J. Wysocki | 89e8ea1 | 2011-10-06 20:35:03 +0200 | [diff] [blame] | 253 | { |
| 254 | .callback = init_nvs_nosave, |
| 255 | .ident = "Sony Vaio VGN-SR26GN_P", |
| 256 | .matches = { |
| 257 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 258 | DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"), |
| 259 | }, |
| 260 | }, |
Bogdan Radulescu | 731b25a | 2011-10-06 20:35:12 +0200 | [diff] [blame] | 261 | { |
| 262 | .callback = init_nvs_nosave, |
Lan Tianyu | 876ab79 | 2012-11-21 23:12:12 +0100 | [diff] [blame] | 263 | .ident = "Sony Vaio VPCEB1S1E", |
| 264 | .matches = { |
| 265 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 266 | DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"), |
| 267 | }, |
| 268 | }, |
| 269 | { |
| 270 | .callback = init_nvs_nosave, |
Bogdan Radulescu | 731b25a | 2011-10-06 20:35:12 +0200 | [diff] [blame] | 271 | .ident = "Sony Vaio VGN-FW520F", |
| 272 | .matches = { |
| 273 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 274 | DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"), |
| 275 | }, |
| 276 | }, |
Keng-Yu Lin | 5a50a7c | 2011-12-02 00:04:23 +0100 | [diff] [blame] | 277 | { |
| 278 | .callback = init_nvs_nosave, |
| 279 | .ident = "Asus K54C", |
| 280 | .matches = { |
| 281 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."), |
| 282 | DMI_MATCH(DMI_PRODUCT_NAME, "K54C"), |
| 283 | }, |
| 284 | }, |
| 285 | { |
| 286 | .callback = init_nvs_nosave, |
| 287 | .ident = "Asus K54HR", |
| 288 | .matches = { |
| 289 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."), |
| 290 | DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"), |
| 291 | }, |
| 292 | }, |
Carlos Corbacho | e41fb7c | 2008-07-23 21:28:43 -0700 | [diff] [blame] | 293 | {}, |
| 294 | }; |
Zhang Rui | 0ac1b1d | 2012-11-30 12:57:03 +0100 | [diff] [blame^] | 295 | |
| 296 | static void acpi_sleep_dmi_check(void) |
| 297 | { |
| 298 | dmi_check_system(acpisleep_dmi_table); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * acpi_pm_freeze - Disable the GPEs and suspend EC transactions. |
| 303 | */ |
| 304 | static int acpi_pm_freeze(void) |
| 305 | { |
| 306 | acpi_disable_all_gpes(); |
| 307 | acpi_os_wait_events_complete(); |
| 308 | acpi_ec_block_transactions(); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS. |
| 314 | */ |
| 315 | static int acpi_pm_pre_suspend(void) |
| 316 | { |
| 317 | acpi_pm_freeze(); |
| 318 | return suspend_nvs_save(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * __acpi_pm_prepare - Prepare the platform to enter the target state. |
| 323 | * |
| 324 | * If necessary, set the firmware waking vector and do arch-specific |
| 325 | * nastiness to get the wakeup code to the waking vector. |
| 326 | */ |
| 327 | static int __acpi_pm_prepare(void) |
| 328 | { |
| 329 | int error = acpi_sleep_prepare(acpi_target_sleep_state); |
| 330 | if (error) |
| 331 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 332 | |
| 333 | return error; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * acpi_pm_prepare - Prepare the platform to enter the target sleep |
| 338 | * state and disable the GPEs. |
| 339 | */ |
| 340 | static int acpi_pm_prepare(void) |
| 341 | { |
| 342 | int error = __acpi_pm_prepare(); |
| 343 | if (!error) |
| 344 | error = acpi_pm_pre_suspend(); |
| 345 | |
| 346 | return error; |
| 347 | } |
| 348 | |
| 349 | static int find_powerf_dev(struct device *dev, void *data) |
| 350 | { |
| 351 | struct acpi_device *device = to_acpi_device(dev); |
| 352 | const char *hid = acpi_device_hid(device); |
| 353 | |
| 354 | return !strcmp(hid, ACPI_BUTTON_HID_POWERF); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * acpi_pm_finish - Instruct the platform to leave a sleep state. |
| 359 | * |
| 360 | * This is called after we wake back up (or if entering the sleep state |
| 361 | * failed). |
| 362 | */ |
| 363 | static void acpi_pm_finish(void) |
| 364 | { |
| 365 | struct device *pwr_btn_dev; |
| 366 | u32 acpi_state = acpi_target_sleep_state; |
| 367 | |
| 368 | acpi_ec_unblock_transactions(); |
| 369 | suspend_nvs_free(); |
| 370 | |
| 371 | if (acpi_state == ACPI_STATE_S0) |
| 372 | return; |
| 373 | |
| 374 | printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n", |
| 375 | acpi_state); |
| 376 | acpi_disable_wakeup_devices(acpi_state); |
| 377 | acpi_leave_sleep_state(acpi_state); |
| 378 | |
| 379 | /* reset firmware waking vector */ |
| 380 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
| 381 | |
| 382 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 383 | |
| 384 | /* If we were woken with the fixed power button, provide a small |
| 385 | * hint to userspace in the form of a wakeup event on the fixed power |
| 386 | * button device (if it can be found). |
| 387 | * |
| 388 | * We delay the event generation til now, as the PM layer requires |
| 389 | * timekeeping to be running before we generate events. */ |
| 390 | if (!pwr_btn_event_pending) |
| 391 | return; |
| 392 | |
| 393 | pwr_btn_event_pending = false; |
| 394 | pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL, |
| 395 | find_powerf_dev); |
| 396 | if (pwr_btn_dev) { |
| 397 | pm_wakeup_event(pwr_btn_dev, 0); |
| 398 | put_device(pwr_btn_dev); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * acpi_pm_end - Finish up suspend sequence. |
| 404 | */ |
| 405 | static void acpi_pm_end(void) |
| 406 | { |
| 407 | /* |
| 408 | * This is necessary in case acpi_pm_finish() is not called during a |
| 409 | * failing transition to a sleep state. |
| 410 | */ |
| 411 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 412 | acpi_sleep_tts_switch(acpi_target_sleep_state); |
| 413 | } |
| 414 | #else /* !CONFIG_ACPI_SLEEP */ |
| 415 | #define acpi_target_sleep_state ACPI_STATE_S0 |
| 416 | static inline void acpi_sleep_dmi_check(void) {} |
| 417 | #endif /* CONFIG_ACPI_SLEEP */ |
| 418 | |
| 419 | #ifdef CONFIG_SUSPEND |
| 420 | static u32 acpi_suspend_states[] = { |
| 421 | [PM_SUSPEND_ON] = ACPI_STATE_S0, |
| 422 | [PM_SUSPEND_STANDBY] = ACPI_STATE_S1, |
| 423 | [PM_SUSPEND_MEM] = ACPI_STATE_S3, |
| 424 | [PM_SUSPEND_MAX] = ACPI_STATE_S5 |
| 425 | }; |
| 426 | |
| 427 | /** |
| 428 | * acpi_suspend_begin - Set the target system sleep state to the state |
| 429 | * associated with given @pm_state, if supported. |
| 430 | */ |
| 431 | static int acpi_suspend_begin(suspend_state_t pm_state) |
| 432 | { |
| 433 | u32 acpi_state = acpi_suspend_states[pm_state]; |
| 434 | int error = 0; |
| 435 | |
| 436 | error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc(); |
| 437 | if (error) |
| 438 | return error; |
| 439 | |
| 440 | if (sleep_states[acpi_state]) { |
| 441 | acpi_target_sleep_state = acpi_state; |
| 442 | acpi_sleep_tts_switch(acpi_target_sleep_state); |
| 443 | } else { |
| 444 | printk(KERN_ERR "ACPI does not support this state: %d\n", |
| 445 | pm_state); |
| 446 | error = -ENOSYS; |
| 447 | } |
| 448 | return error; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * acpi_suspend_enter - Actually enter a sleep state. |
| 453 | * @pm_state: ignored |
| 454 | * |
| 455 | * Flush caches and go to sleep. For STR we have to call arch-specific |
| 456 | * assembly, which in turn call acpi_enter_sleep_state(). |
| 457 | * It's unfortunate, but it works. Please fix if you're feeling frisky. |
| 458 | */ |
| 459 | static int acpi_suspend_enter(suspend_state_t pm_state) |
| 460 | { |
| 461 | acpi_status status = AE_OK; |
| 462 | u32 acpi_state = acpi_target_sleep_state; |
| 463 | int error; |
| 464 | |
| 465 | ACPI_FLUSH_CPU_CACHE(); |
| 466 | |
| 467 | switch (acpi_state) { |
| 468 | case ACPI_STATE_S1: |
| 469 | barrier(); |
| 470 | status = acpi_enter_sleep_state(acpi_state); |
| 471 | break; |
| 472 | |
| 473 | case ACPI_STATE_S3: |
| 474 | error = acpi_suspend_lowlevel(); |
| 475 | if (error) |
| 476 | return error; |
| 477 | pr_info(PREFIX "Low-level resume complete\n"); |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | /* This violates the spec but is required for bug compatibility. */ |
| 482 | acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1); |
| 483 | |
| 484 | /* Reprogram control registers */ |
| 485 | acpi_leave_sleep_state_prep(acpi_state); |
| 486 | |
| 487 | /* ACPI 3.0 specs (P62) says that it's the responsibility |
| 488 | * of the OSPM to clear the status bit [ implying that the |
| 489 | * POWER_BUTTON event should not reach userspace ] |
| 490 | * |
| 491 | * However, we do generate a small hint for userspace in the form of |
| 492 | * a wakeup event. We flag this condition for now and generate the |
| 493 | * event later, as we're currently too early in resume to be able to |
| 494 | * generate wakeup events. |
| 495 | */ |
| 496 | if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) { |
| 497 | acpi_event_status pwr_btn_status; |
| 498 | |
| 499 | acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status); |
| 500 | |
| 501 | if (pwr_btn_status & ACPI_EVENT_FLAG_SET) { |
| 502 | acpi_clear_event(ACPI_EVENT_POWER_BUTTON); |
| 503 | /* Flag for later */ |
| 504 | pwr_btn_event_pending = true; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Disable and clear GPE status before interrupt is enabled. Some GPEs |
| 510 | * (like wakeup GPE) haven't handler, this can avoid such GPE misfire. |
| 511 | * acpi_leave_sleep_state will reenable specific GPEs later |
| 512 | */ |
| 513 | acpi_disable_all_gpes(); |
| 514 | /* Allow EC transactions to happen. */ |
| 515 | acpi_ec_unblock_transactions_early(); |
| 516 | |
| 517 | suspend_nvs_restore(); |
| 518 | |
| 519 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 520 | } |
| 521 | |
| 522 | static int acpi_suspend_state_valid(suspend_state_t pm_state) |
| 523 | { |
| 524 | u32 acpi_state; |
| 525 | |
| 526 | switch (pm_state) { |
| 527 | case PM_SUSPEND_ON: |
| 528 | case PM_SUSPEND_STANDBY: |
| 529 | case PM_SUSPEND_MEM: |
| 530 | acpi_state = acpi_suspend_states[pm_state]; |
| 531 | |
| 532 | return sleep_states[acpi_state]; |
| 533 | default: |
| 534 | return 0; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | static const struct platform_suspend_ops acpi_suspend_ops = { |
| 539 | .valid = acpi_suspend_state_valid, |
| 540 | .begin = acpi_suspend_begin, |
| 541 | .prepare_late = acpi_pm_prepare, |
| 542 | .enter = acpi_suspend_enter, |
| 543 | .wake = acpi_pm_finish, |
| 544 | .end = acpi_pm_end, |
| 545 | }; |
| 546 | |
| 547 | /** |
| 548 | * acpi_suspend_begin_old - Set the target system sleep state to the |
| 549 | * state associated with given @pm_state, if supported, and |
| 550 | * execute the _PTS control method. This function is used if the |
| 551 | * pre-ACPI 2.0 suspend ordering has been requested. |
| 552 | */ |
| 553 | static int acpi_suspend_begin_old(suspend_state_t pm_state) |
| 554 | { |
| 555 | int error = acpi_suspend_begin(pm_state); |
| 556 | if (!error) |
| 557 | error = __acpi_pm_prepare(); |
| 558 | |
| 559 | return error; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has |
| 564 | * been requested. |
| 565 | */ |
| 566 | static const struct platform_suspend_ops acpi_suspend_ops_old = { |
| 567 | .valid = acpi_suspend_state_valid, |
| 568 | .begin = acpi_suspend_begin_old, |
| 569 | .prepare_late = acpi_pm_pre_suspend, |
| 570 | .enter = acpi_suspend_enter, |
| 571 | .wake = acpi_pm_finish, |
| 572 | .end = acpi_pm_end, |
| 573 | .recover = acpi_pm_finish, |
| 574 | }; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 575 | #endif /* CONFIG_SUSPEND */ |
| 576 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 577 | #ifdef CONFIG_HIBERNATION |
Shaohua Li | bdfe6b7 | 2008-07-23 21:28:41 -0700 | [diff] [blame] | 578 | static unsigned long s4_hardware_signature; |
| 579 | static struct acpi_table_facs *facs; |
| 580 | static bool nosigcheck; |
| 581 | |
| 582 | void __init acpi_no_s4_hw_signature(void) |
| 583 | { |
| 584 | nosigcheck = true; |
| 585 | } |
| 586 | |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 587 | static int acpi_hibernation_begin(void) |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 588 | { |
Rafael J. Wysocki | 3f4b0ef | 2008-10-26 20:52:15 +0100 | [diff] [blame] | 589 | int error; |
| 590 | |
Rafael J. Wysocki | 72ad5d7 | 2010-07-23 22:59:09 +0200 | [diff] [blame] | 591 | error = nvs_nosave ? 0 : suspend_nvs_alloc(); |
Rafael J. Wysocki | 3f4b0ef | 2008-10-26 20:52:15 +0100 | [diff] [blame] | 592 | if (!error) { |
| 593 | acpi_target_sleep_state = ACPI_STATE_S4; |
| 594 | acpi_sleep_tts_switch(acpi_target_sleep_state); |
| 595 | } |
| 596 | |
| 597 | return error; |
| 598 | } |
| 599 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 600 | static int acpi_hibernation_enter(void) |
| 601 | { |
| 602 | acpi_status status = AE_OK; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 603 | |
| 604 | ACPI_FLUSH_CPU_CACHE(); |
| 605 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 606 | /* This shouldn't return. If it returns, we have a problem */ |
Len Brown | 3f6f49c | 2012-07-26 20:08:54 -0400 | [diff] [blame] | 607 | status = acpi_enter_sleep_state(ACPI_STATE_S4); |
| 608 | /* Reprogram control registers */ |
| 609 | acpi_leave_sleep_state_prep(ACPI_STATE_S4); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 610 | |
| 611 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 612 | } |
| 613 | |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 614 | static void acpi_hibernation_leave(void) |
| 615 | { |
| 616 | /* |
| 617 | * If ACPI is not enabled by the BIOS and the boot kernel, we need to |
| 618 | * enable it here. |
| 619 | */ |
| 620 | acpi_enable(); |
Len Brown | 3f6f49c | 2012-07-26 20:08:54 -0400 | [diff] [blame] | 621 | /* Reprogram control registers */ |
| 622 | acpi_leave_sleep_state_prep(ACPI_STATE_S4); |
Shaohua Li | bdfe6b7 | 2008-07-23 21:28:41 -0700 | [diff] [blame] | 623 | /* Check the hardware signature */ |
| 624 | if (facs && s4_hardware_signature != facs->hardware_signature) { |
| 625 | printk(KERN_EMERG "ACPI: Hardware changed while hibernated, " |
| 626 | "cannot resume!\n"); |
| 627 | panic("ACPI S4 hardware signature mismatch"); |
| 628 | } |
Rafael J. Wysocki | 3f4b0ef | 2008-10-26 20:52:15 +0100 | [diff] [blame] | 629 | /* Restore the NVS memory area */ |
Matthew Garrett | dd4c4f1 | 2010-05-28 16:32:14 -0400 | [diff] [blame] | 630 | suspend_nvs_restore(); |
Rafael J. Wysocki | d5a6451 | 2010-04-09 01:39:40 +0200 | [diff] [blame] | 631 | /* Allow EC transactions to happen. */ |
Rafael J. Wysocki | fe95568 | 2010-04-09 01:40:38 +0200 | [diff] [blame] | 632 | acpi_ec_unblock_transactions_early(); |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Rafael J. Wysocki | d5a6451 | 2010-04-09 01:39:40 +0200 | [diff] [blame] | 635 | static void acpi_pm_thaw(void) |
Rafael J. Wysocki | f6bb13a | 2010-03-04 01:52:58 +0100 | [diff] [blame] | 636 | { |
Rafael J. Wysocki | fe95568 | 2010-04-09 01:40:38 +0200 | [diff] [blame] | 637 | acpi_ec_unblock_transactions(); |
Lin Ming | 3d97e42 | 2008-12-16 16:57:46 +0800 | [diff] [blame] | 638 | acpi_enable_all_runtime_gpes(); |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Lionel Debroux | 073ef1f | 2010-11-09 21:48:49 +0100 | [diff] [blame] | 641 | static const struct platform_hibernation_ops acpi_hibernation_ops = { |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 642 | .begin = acpi_hibernation_begin, |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 643 | .end = acpi_pm_end, |
Rafael J. Wysocki | c5f7a1b | 2010-07-02 00:14:09 +0200 | [diff] [blame] | 644 | .pre_snapshot = acpi_pm_prepare, |
Matthew Garrett | 2a6b697 | 2010-05-28 16:32:15 -0400 | [diff] [blame] | 645 | .finish = acpi_pm_finish, |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 646 | .prepare = acpi_pm_prepare, |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 647 | .enter = acpi_hibernation_enter, |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 648 | .leave = acpi_hibernation_leave, |
Rafael J. Wysocki | d5a6451 | 2010-04-09 01:39:40 +0200 | [diff] [blame] | 649 | .pre_restore = acpi_pm_freeze, |
| 650 | .restore_cleanup = acpi_pm_thaw, |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 651 | }; |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 652 | |
| 653 | /** |
| 654 | * acpi_hibernation_begin_old - Set the target system sleep state to |
| 655 | * ACPI_STATE_S4 and execute the _PTS control method. This |
| 656 | * function is used if the pre-ACPI 2.0 suspend ordering has been |
| 657 | * requested. |
| 658 | */ |
| 659 | static int acpi_hibernation_begin_old(void) |
| 660 | { |
Zhao Yakui | e49f711 | 2008-08-12 10:20:22 +0800 | [diff] [blame] | 661 | int error; |
| 662 | /* |
| 663 | * The _TTS object should always be evaluated before the _PTS object. |
| 664 | * When the old_suspended_ordering is true, the _PTS object is |
| 665 | * evaluated in the acpi_sleep_prepare. |
| 666 | */ |
| 667 | acpi_sleep_tts_switch(ACPI_STATE_S4); |
| 668 | |
| 669 | error = acpi_sleep_prepare(ACPI_STATE_S4); |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 670 | |
Rafael J. Wysocki | 3f4b0ef | 2008-10-26 20:52:15 +0100 | [diff] [blame] | 671 | if (!error) { |
Rafael J. Wysocki | 72ad5d7 | 2010-07-23 22:59:09 +0200 | [diff] [blame] | 672 | if (!nvs_nosave) |
Matthew Garrett | dd4c4f1 | 2010-05-28 16:32:14 -0400 | [diff] [blame] | 673 | error = suspend_nvs_alloc(); |
Rafael J. Wysocki | 3f4b0ef | 2008-10-26 20:52:15 +0100 | [diff] [blame] | 674 | if (!error) |
| 675 | acpi_target_sleep_state = ACPI_STATE_S4; |
| 676 | } |
| 677 | return error; |
| 678 | } |
| 679 | |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 680 | /* |
| 681 | * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has |
| 682 | * been requested. |
| 683 | */ |
Lionel Debroux | 073ef1f | 2010-11-09 21:48:49 +0100 | [diff] [blame] | 684 | static const struct platform_hibernation_ops acpi_hibernation_ops_old = { |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 685 | .begin = acpi_hibernation_begin_old, |
| 686 | .end = acpi_pm_end, |
Rafael J. Wysocki | c5f7a1b | 2010-07-02 00:14:09 +0200 | [diff] [blame] | 687 | .pre_snapshot = acpi_pm_pre_suspend, |
Rafael J. Wysocki | d5a6451 | 2010-04-09 01:39:40 +0200 | [diff] [blame] | 688 | .prepare = acpi_pm_freeze, |
Matthew Garrett | 2a6b697 | 2010-05-28 16:32:15 -0400 | [diff] [blame] | 689 | .finish = acpi_pm_finish, |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 690 | .enter = acpi_hibernation_enter, |
| 691 | .leave = acpi_hibernation_leave, |
Rafael J. Wysocki | d5a6451 | 2010-04-09 01:39:40 +0200 | [diff] [blame] | 692 | .pre_restore = acpi_pm_freeze, |
| 693 | .restore_cleanup = acpi_pm_thaw, |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 694 | .recover = acpi_pm_finish, |
| 695 | }; |
| 696 | #endif /* CONFIG_HIBERNATION */ |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 697 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 698 | int acpi_suspend(u32 acpi_state) |
| 699 | { |
| 700 | suspend_state_t states[] = { |
| 701 | [1] = PM_SUSPEND_STANDBY, |
| 702 | [3] = PM_SUSPEND_MEM, |
| 703 | [5] = PM_SUSPEND_MAX |
| 704 | }; |
| 705 | |
| 706 | if (acpi_state < 6 && states[acpi_state]) |
| 707 | return pm_suspend(states[acpi_state]); |
| 708 | if (acpi_state == 4) |
| 709 | return hibernate(); |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | |
Rafael J. Wysocki | aa33860 | 2011-02-11 00:06:54 +0100 | [diff] [blame] | 713 | #ifdef CONFIG_PM |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 714 | /** |
| 715 | * acpi_pm_device_sleep_state - return preferred power state of ACPI device |
| 716 | * in the system sleep state given by %acpi_target_sleep_state |
David Brownell | 2fe2de5 | 2008-06-05 01:15:40 +0200 | [diff] [blame] | 717 | * @dev: device to examine; its driver model wakeup flags control |
| 718 | * whether it should be able to wake up the system |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 719 | * @d_min_p: used to store the upper limit of allowed states range |
Huang Ying | ee85f54 | 2012-06-23 10:23:48 +0800 | [diff] [blame] | 720 | * @d_max_in: specify the lowest allowed states |
| 721 | * Return value: preferred power state of the device on success, -ENODEV |
| 722 | * (ie. if there's no 'struct acpi_device' for @dev) or -EINVAL on failure |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 723 | * |
| 724 | * Find the lowest power (highest number) ACPI device power state that |
| 725 | * device @dev can be in while the system is in the sleep state represented |
| 726 | * by %acpi_target_sleep_state. If @wake is nonzero, the device should be |
| 727 | * able to wake up the system from this sleep state. If @d_min_p is set, |
| 728 | * the highest power (lowest number) device power state of @dev allowed |
| 729 | * in this system sleep state is stored at the location pointed to by it. |
| 730 | * |
| 731 | * The caller must ensure that @dev is valid before using this function. |
| 732 | * The caller is also responsible for figuring out if the device is |
| 733 | * supposed to be able to wake up the system and passing this information |
| 734 | * via @wake. |
| 735 | */ |
| 736 | |
Huang Ying | ee85f54 | 2012-06-23 10:23:48 +0800 | [diff] [blame] | 737 | int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 738 | { |
| 739 | acpi_handle handle = DEVICE_ACPI_HANDLE(dev); |
| 740 | struct acpi_device *adev; |
| 741 | char acpi_method[] = "_SxD"; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 742 | unsigned long long d_min, d_max; |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 743 | |
Huang Ying | ee85f54 | 2012-06-23 10:23:48 +0800 | [diff] [blame] | 744 | if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3) |
| 745 | return -EINVAL; |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 746 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { |
Shaohua Li | ead7759 | 2007-08-23 15:01:13 +0800 | [diff] [blame] | 747 | printk(KERN_DEBUG "ACPI handle has no context!\n"); |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 748 | return -ENODEV; |
| 749 | } |
| 750 | |
| 751 | acpi_method[2] = '0' + acpi_target_sleep_state; |
| 752 | /* |
Huang Ying | ee85f54 | 2012-06-23 10:23:48 +0800 | [diff] [blame] | 753 | * If the sleep state is S0, the lowest limit from ACPI is D3, |
| 754 | * but if the device has _S0W, we will use the value from _S0W |
| 755 | * as the lowest limit from ACPI. Finally, we will constrain |
| 756 | * the lowest limit with the specified one. |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 757 | */ |
| 758 | d_min = ACPI_STATE_D0; |
| 759 | d_max = ACPI_STATE_D3; |
| 760 | |
| 761 | /* |
| 762 | * If present, _SxD methods return the minimum D-state (highest power |
| 763 | * state) we can use for the corresponding S-states. Otherwise, the |
| 764 | * minimum D-state is D0 (ACPI 3.x). |
| 765 | * |
| 766 | * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer |
| 767 | * provided -- that's our fault recovery, we ignore retval. |
| 768 | */ |
| 769 | if (acpi_target_sleep_state > ACPI_STATE_S0) |
| 770 | acpi_evaluate_integer(handle, acpi_method, NULL, &d_min); |
| 771 | |
| 772 | /* |
| 773 | * If _PRW says we can wake up the system from the target sleep state, |
| 774 | * the D-state returned by _SxD is sufficient for that (we assume a |
| 775 | * wakeup-aware driver if wake is set). Still, if _SxW exists |
| 776 | * (ACPI 3.x), it should return the maximum (lowest power) D-state that |
| 777 | * can wake the system. _S0W may be valid, too. |
| 778 | */ |
| 779 | if (acpi_target_sleep_state == ACPI_STATE_S0 || |
Rafael J. Wysocki | dbe9a2e | 2012-05-29 21:21:07 +0200 | [diff] [blame] | 780 | (device_may_wakeup(dev) && adev->wakeup.flags.valid && |
| 781 | adev->wakeup.sleep_state >= acpi_target_sleep_state)) { |
Rafael J. Wysocki | ad3399c | 2008-01-11 00:10:38 +0100 | [diff] [blame] | 782 | acpi_status status; |
| 783 | |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 784 | acpi_method[3] = 'W'; |
Rafael J. Wysocki | ad3399c | 2008-01-11 00:10:38 +0100 | [diff] [blame] | 785 | status = acpi_evaluate_integer(handle, acpi_method, NULL, |
| 786 | &d_max); |
| 787 | if (ACPI_FAILURE(status)) { |
Rafael J. Wysocki | 761afb8 | 2010-10-14 23:24:13 +0200 | [diff] [blame] | 788 | if (acpi_target_sleep_state != ACPI_STATE_S0 || |
| 789 | status != AE_NOT_FOUND) |
| 790 | d_max = d_min; |
Rafael J. Wysocki | ad3399c | 2008-01-11 00:10:38 +0100 | [diff] [blame] | 791 | } else if (d_max < d_min) { |
| 792 | /* Warn the user of the broken DSDT */ |
| 793 | printk(KERN_WARNING "ACPI: Wrong value from %s\n", |
| 794 | acpi_method); |
| 795 | /* Sanitize it */ |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 796 | d_min = d_max; |
Rafael J. Wysocki | ad3399c | 2008-01-11 00:10:38 +0100 | [diff] [blame] | 797 | } |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Huang Ying | ee85f54 | 2012-06-23 10:23:48 +0800 | [diff] [blame] | 800 | if (d_max_in < d_min) |
| 801 | return -EINVAL; |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 802 | if (d_min_p) |
| 803 | *d_min_p = d_min; |
Huang Ying | ee85f54 | 2012-06-23 10:23:48 +0800 | [diff] [blame] | 804 | /* constrain d_max with specified lowest limit (max number) */ |
| 805 | if (d_max > d_max_in) { |
| 806 | for (d_max = d_max_in; d_max > d_min; d_max--) { |
| 807 | if (adev->power.states[d_max].flags.valid) |
| 808 | break; |
| 809 | } |
| 810 | } |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 811 | return d_max; |
| 812 | } |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 813 | EXPORT_SYMBOL(acpi_pm_device_sleep_state); |
Rafael J. Wysocki | aa33860 | 2011-02-11 00:06:54 +0100 | [diff] [blame] | 814 | #endif /* CONFIG_PM */ |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 815 | |
Rafael J. Wysocki | 761afb8 | 2010-10-14 23:24:13 +0200 | [diff] [blame] | 816 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 817 | /** |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 818 | * acpi_pm_device_run_wake - Enable/disable wake-up for given device. |
| 819 | * @phys_dev: Device to enable/disable the platform to wake-up the system for. |
| 820 | * @enable: Whether enable or disable the wake-up functionality. |
| 821 | * |
| 822 | * Find the ACPI device object corresponding to @pci_dev and try to |
| 823 | * enable/disable the GPE associated with it. |
| 824 | */ |
| 825 | int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) |
| 826 | { |
| 827 | struct acpi_device *dev; |
| 828 | acpi_handle handle; |
| 829 | |
| 830 | if (!device_run_wake(phys_dev)) |
| 831 | return -EINVAL; |
| 832 | |
| 833 | handle = DEVICE_ACPI_HANDLE(phys_dev); |
| 834 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) { |
| 835 | dev_dbg(phys_dev, "ACPI handle has no context in %s!\n", |
| 836 | __func__); |
| 837 | return -ENODEV; |
| 838 | } |
| 839 | |
| 840 | if (enable) { |
| 841 | acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0); |
| 842 | acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number); |
| 843 | } else { |
| 844 | acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number); |
| 845 | acpi_disable_wakeup_device_power(dev); |
| 846 | } |
| 847 | |
| 848 | return 0; |
| 849 | } |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 850 | EXPORT_SYMBOL(acpi_pm_device_run_wake); |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 851 | |
| 852 | /** |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 853 | * acpi_pm_device_sleep_wake - enable or disable the system wake-up |
| 854 | * capability of given device |
| 855 | * @dev: device to handle |
| 856 | * @enable: 'true' - enable, 'false' - disable the wake-up capability |
| 857 | */ |
| 858 | int acpi_pm_device_sleep_wake(struct device *dev, bool enable) |
| 859 | { |
| 860 | acpi_handle handle; |
| 861 | struct acpi_device *adev; |
Rafael J. Wysocki | df8db91 | 2009-09-08 23:13:49 +0200 | [diff] [blame] | 862 | int error; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 863 | |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 864 | if (!device_can_wakeup(dev)) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 865 | return -EINVAL; |
| 866 | |
| 867 | handle = DEVICE_ACPI_HANDLE(dev); |
| 868 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { |
Rafael J. Wysocki | df8db91 | 2009-09-08 23:13:49 +0200 | [diff] [blame] | 869 | dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__); |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 870 | return -ENODEV; |
| 871 | } |
| 872 | |
Rafael J. Wysocki | e8b6f97 | 2010-06-25 01:18:39 +0200 | [diff] [blame] | 873 | error = enable ? |
| 874 | acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) : |
| 875 | acpi_disable_wakeup_device_power(adev); |
Rafael J. Wysocki | df8db91 | 2009-09-08 23:13:49 +0200 | [diff] [blame] | 876 | if (!error) |
| 877 | dev_info(dev, "wake-up capability %s by ACPI\n", |
| 878 | enable ? "enabled" : "disabled"); |
| 879 | |
| 880 | return error; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 881 | } |
Rafael J. Wysocki | 761afb8 | 2010-10-14 23:24:13 +0200 | [diff] [blame] | 882 | #endif /* CONFIG_PM_SLEEP */ |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 883 | |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 884 | static void acpi_power_off_prepare(void) |
| 885 | { |
| 886 | /* Prepare to power off the system */ |
| 887 | acpi_sleep_prepare(ACPI_STATE_S5); |
Lin Ming | 3d97e42 | 2008-12-16 16:57:46 +0800 | [diff] [blame] | 888 | acpi_disable_all_gpes(); |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static void acpi_power_off(void) |
| 892 | { |
| 893 | /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */ |
Frank Seidel | 4d93915 | 2009-02-04 17:03:07 +0100 | [diff] [blame] | 894 | printk(KERN_DEBUG "%s called\n", __func__); |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 895 | local_irq_disable(); |
Len Brown | 3f6f49c | 2012-07-26 20:08:54 -0400 | [diff] [blame] | 896 | acpi_enter_sleep_state(ACPI_STATE_S5); |
Len Brown | 96f15ef | 2009-04-17 23:32:20 -0400 | [diff] [blame] | 897 | } |
| 898 | |
Alexey Starikovskiy | aafbcd1 | 2007-02-10 01:32:16 -0500 | [diff] [blame] | 899 | int __init acpi_sleep_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 901 | acpi_status status; |
| 902 | u8 type_a, type_b; |
| 903 | #ifdef CONFIG_SUSPEND |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 904 | int i = 0; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 905 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | if (acpi_disabled) |
| 908 | return 0; |
| 909 | |
Zhang Rui | 0ac1b1d | 2012-11-30 12:57:03 +0100 | [diff] [blame^] | 910 | acpi_sleep_dmi_check(); |
| 911 | |
Frans Pop | 5a50fe7 | 2007-09-20 22:27:44 +0200 | [diff] [blame] | 912 | sleep_states[ACPI_STATE_S0] = 1; |
| 913 | printk(KERN_INFO PREFIX "(supports S0"); |
| 914 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 915 | #ifdef CONFIG_SUSPEND |
Frans Pop | 5a50fe7 | 2007-09-20 22:27:44 +0200 | [diff] [blame] | 916 | for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | status = acpi_get_sleep_type_data(i, &type_a, &type_b); |
| 918 | if (ACPI_SUCCESS(status)) { |
| 919 | sleep_states[i] = 1; |
Kay Sievers | be96447 | 2012-05-08 17:24:20 +0200 | [diff] [blame] | 920 | printk(KERN_CONT " S%d", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 924 | suspend_set_ops(old_suspend_ordering ? |
| 925 | &acpi_suspend_ops_old : &acpi_suspend_ops); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 926 | #endif |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 927 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 928 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 929 | status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b); |
| 930 | if (ACPI_SUCCESS(status)) { |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 931 | hibernation_set_ops(old_suspend_ordering ? |
| 932 | &acpi_hibernation_ops_old : &acpi_hibernation_ops); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 933 | sleep_states[ACPI_STATE_S4] = 1; |
Kay Sievers | be96447 | 2012-05-08 17:24:20 +0200 | [diff] [blame] | 934 | printk(KERN_CONT " S4"); |
Shaohua Li | bdfe6b7 | 2008-07-23 21:28:41 -0700 | [diff] [blame] | 935 | if (!nosigcheck) { |
Lin Ming | 3d97e42 | 2008-12-16 16:57:46 +0800 | [diff] [blame] | 936 | acpi_get_table(ACPI_SIG_FACS, 1, |
Shaohua Li | bdfe6b7 | 2008-07-23 21:28:41 -0700 | [diff] [blame] | 937 | (struct acpi_table_header **)&facs); |
| 938 | if (facs) |
| 939 | s4_hardware_signature = |
| 940 | facs->hardware_signature; |
| 941 | } |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 942 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 943 | #endif |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 944 | status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b); |
| 945 | if (ACPI_SUCCESS(status)) { |
| 946 | sleep_states[ACPI_STATE_S5] = 1; |
Kay Sievers | be96447 | 2012-05-08 17:24:20 +0200 | [diff] [blame] | 947 | printk(KERN_CONT " S5"); |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 948 | pm_power_off_prepare = acpi_power_off_prepare; |
| 949 | pm_power_off = acpi_power_off; |
| 950 | } |
Kay Sievers | be96447 | 2012-05-08 17:24:20 +0200 | [diff] [blame] | 951 | printk(KERN_CONT ")\n"); |
Zhao Yakui | e49f711 | 2008-08-12 10:20:22 +0800 | [diff] [blame] | 952 | /* |
| 953 | * Register the tts_notifier to reboot notifier list so that the _TTS |
| 954 | * object can also be evaluated when the system enters S5. |
| 955 | */ |
| 956 | register_reboot_notifier(&tts_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | return 0; |
| 958 | } |