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> |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 18 | |
| 19 | #include <asm/io.h> |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <acpi/acpi_bus.h> |
| 22 | #include <acpi/acpi_drivers.h> |
| 23 | #include "sleep.h" |
| 24 | |
| 25 | u8 sleep_states[ACPI_S_STATE_COUNT]; |
| 26 | |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 27 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 28 | static u32 acpi_target_sleep_state = ACPI_STATE_S0; |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 29 | static bool acpi_sleep_finish_wake_up; |
| 30 | |
| 31 | /* |
| 32 | * ACPI 2.0 and later want us to execute _PTS after suspending devices, so we |
| 33 | * allow the user to request that behavior by using the 'acpi_new_pts_ordering' |
| 34 | * kernel command line option that causes the following variable to be set. |
| 35 | */ |
| 36 | static bool new_pts_ordering; |
| 37 | |
| 38 | static int __init acpi_new_pts_ordering(char *str) |
| 39 | { |
| 40 | new_pts_ordering = true; |
| 41 | return 1; |
| 42 | } |
| 43 | __setup("acpi_new_pts_ordering", acpi_new_pts_ordering); |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 44 | #endif |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 45 | |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 46 | int acpi_sleep_prepare(u32 acpi_state) |
| 47 | { |
| 48 | #ifdef CONFIG_ACPI_SLEEP |
| 49 | /* do we have a wakeup address for S2 and S3? */ |
| 50 | if (acpi_state == ACPI_STATE_S3) { |
| 51 | if (!acpi_wakeup_address) { |
| 52 | return -EFAULT; |
| 53 | } |
| 54 | acpi_set_firmware_waking_vector((acpi_physical_address) |
| 55 | virt_to_phys((void *) |
| 56 | acpi_wakeup_address)); |
| 57 | |
| 58 | } |
| 59 | ACPI_FLUSH_CPU_CACHE(); |
| 60 | acpi_enable_wakeup_device_prep(acpi_state); |
| 61 | #endif |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 62 | acpi_enter_sleep_state_prep(acpi_state); |
| 63 | return 0; |
| 64 | } |
| 65 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 66 | #ifdef CONFIG_SUSPEND |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 67 | static struct platform_suspend_ops acpi_pm_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | extern void do_suspend_lowlevel(void); |
| 70 | |
| 71 | static u32 acpi_suspend_states[] = { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 72 | [PM_SUSPEND_ON] = ACPI_STATE_S0, |
| 73 | [PM_SUSPEND_STANDBY] = ACPI_STATE_S1, |
| 74 | [PM_SUSPEND_MEM] = ACPI_STATE_S3, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 75 | [PM_SUSPEND_MAX] = ACPI_STATE_S5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static int init_8259A_after_S1; |
| 79 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 80 | /** |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 81 | * acpi_pm_begin - Set the target system sleep state to the state |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 82 | * associated with given @pm_state, if supported. |
| 83 | */ |
| 84 | |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 85 | static int acpi_pm_begin(suspend_state_t pm_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | u32 acpi_state = acpi_suspend_states[pm_state]; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 88 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 90 | if (sleep_states[acpi_state]) { |
| 91 | acpi_target_sleep_state = acpi_state; |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 92 | if (new_pts_ordering) |
| 93 | return 0; |
| 94 | |
| 95 | error = acpi_sleep_prepare(acpi_state); |
| 96 | if (error) |
| 97 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 98 | else |
| 99 | acpi_sleep_finish_wake_up = true; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 100 | } else { |
| 101 | printk(KERN_ERR "ACPI does not support this state: %d\n", |
| 102 | pm_state); |
| 103 | error = -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 105 | return error; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * acpi_pm_prepare - Do preliminary suspend work. |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 110 | * |
| 111 | * If necessary, set the firmware waking vector and do arch-specific |
| 112 | * nastiness to get the wakeup code to the waking vector. |
| 113 | */ |
| 114 | |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 115 | static int acpi_pm_prepare(void) |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 116 | { |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 117 | if (new_pts_ordering) { |
| 118 | int error = acpi_sleep_prepare(acpi_target_sleep_state); |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 119 | |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 120 | if (error) { |
| 121 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 122 | return error; |
| 123 | } |
| 124 | acpi_sleep_finish_wake_up = true; |
| 125 | } |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 126 | |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 127 | return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /** |
| 131 | * acpi_pm_enter - Actually enter a sleep state. |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 132 | * @pm_state: ignored |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * |
Rafael J. Wysocki | 50ad147 | 2007-07-24 11:58:39 +0200 | [diff] [blame] | 134 | * Flush caches and go to sleep. For STR we have to call arch-specific |
| 135 | * assembly, which in turn call acpi_enter_sleep_state(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | * It's unfortunate, but it works. Please fix if you're feeling frisky. |
| 137 | */ |
| 138 | |
| 139 | static int acpi_pm_enter(suspend_state_t pm_state) |
| 140 | { |
| 141 | acpi_status status = AE_OK; |
| 142 | unsigned long flags = 0; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 143 | u32 acpi_state = acpi_target_sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | ACPI_FLUSH_CPU_CACHE(); |
| 146 | |
| 147 | /* Do arch specific saving of state. */ |
Rafael J. Wysocki | 50ad147 | 2007-07-24 11:58:39 +0200 | [diff] [blame] | 148 | if (acpi_state == ACPI_STATE_S3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | int error = acpi_save_state_mem(); |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 150 | |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 151 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return error; |
| 153 | } |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | local_irq_save(flags); |
| 156 | acpi_enable_wakeup_device(acpi_state); |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 157 | switch (acpi_state) { |
| 158 | case ACPI_STATE_S1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | barrier(); |
| 160 | status = acpi_enter_sleep_state(acpi_state); |
| 161 | break; |
| 162 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 163 | case ACPI_STATE_S3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | do_suspend_lowlevel(); |
| 165 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Arnaud Patard | 872d83d | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 167 | |
Rafael J. Wysocki | c95d47a | 2008-01-08 00:05:21 +0100 | [diff] [blame] | 168 | /* Reprogram control registers and execute _BFS */ |
| 169 | acpi_leave_sleep_state_prep(acpi_state); |
| 170 | |
Arnaud Patard | 872d83d | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 171 | /* ACPI 3.0 specs (P62) says that it's the responsabilty |
| 172 | * of the OSPM to clear the status bit [ implying that the |
| 173 | * POWER_BUTTON event should not reach userspace ] |
| 174 | */ |
| 175 | if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) |
| 176 | acpi_clear_event(ACPI_EVENT_POWER_BUTTON); |
| 177 | |
Shaohua Li | a3627f6 | 2007-06-20 09:17:58 +0800 | [diff] [blame] | 178 | /* |
| 179 | * Disable and clear GPE status before interrupt is enabled. Some GPEs |
| 180 | * (like wakeup GPE) haven't handler, this can avoid such GPE misfire. |
| 181 | * acpi_leave_sleep_state will reenable specific GPEs later |
| 182 | */ |
| 183 | acpi_hw_disable_all_gpes(); |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | local_irq_restore(flags); |
| 186 | printk(KERN_DEBUG "Back to C!\n"); |
| 187 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 188 | /* restore processor state */ |
Rafael J. Wysocki | 50ad147 | 2007-07-24 11:58:39 +0200 | [diff] [blame] | 189 | if (acpi_state == ACPI_STATE_S3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | acpi_restore_state_mem(); |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 193 | } |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /** |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 196 | * acpi_pm_finish - Instruct the platform to leave a sleep state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | * |
| 198 | * This is called after we wake back up (or if entering the sleep state |
| 199 | * failed). |
| 200 | */ |
| 201 | |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 202 | static void acpi_pm_finish(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 204 | u32 acpi_state = acpi_target_sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | acpi_disable_wakeup_device(acpi_state); |
Alexey Starikovskiy | 1dbc1fda | 2007-10-22 14:18:12 +0400 | [diff] [blame] | 207 | acpi_leave_sleep_state(acpi_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | /* reset firmware waking vector */ |
| 210 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
| 211 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 212 | acpi_target_sleep_state = ACPI_STATE_S0; |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 213 | acpi_sleep_finish_wake_up = false; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 214 | |
Len Brown | e8b2fd0 | 2007-07-24 22:26:33 -0400 | [diff] [blame] | 215 | #ifdef CONFIG_X86 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (init_8259A_after_S1) { |
| 217 | printk("Broken toshiba laptop -> kicking interrupts\n"); |
| 218 | init_8259A(0); |
| 219 | } |
Len Brown | e8b2fd0 | 2007-07-24 22:26:33 -0400 | [diff] [blame] | 220 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 223 | /** |
| 224 | * acpi_pm_end - Finish up suspend sequence. |
| 225 | */ |
| 226 | |
| 227 | static void acpi_pm_end(void) |
| 228 | { |
| 229 | /* |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 230 | * This is necessary in case acpi_pm_finish() is not called directly |
| 231 | * during a failing transition to a sleep state. |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 232 | */ |
Rafael J. Wysocki | 60417f5 | 2008-01-08 00:07:39 +0100 | [diff] [blame] | 233 | if (acpi_sleep_finish_wake_up) |
| 234 | acpi_pm_finish(); |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 237 | static int acpi_pm_state_valid(suspend_state_t pm_state) |
| 238 | { |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 239 | u32 acpi_state; |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 240 | |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 241 | switch (pm_state) { |
| 242 | case PM_SUSPEND_ON: |
| 243 | case PM_SUSPEND_STANDBY: |
| 244 | case PM_SUSPEND_MEM: |
| 245 | acpi_state = acpi_suspend_states[pm_state]; |
| 246 | |
| 247 | return sleep_states[acpi_state]; |
| 248 | default: |
| 249 | return 0; |
| 250 | } |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 253 | static struct platform_suspend_ops acpi_pm_ops = { |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 254 | .valid = acpi_pm_state_valid, |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 255 | .begin = acpi_pm_begin, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 256 | .prepare = acpi_pm_prepare, |
| 257 | .enter = acpi_pm_enter, |
| 258 | .finish = acpi_pm_finish, |
Rafael J. Wysocki | c697eec | 2008-01-08 00:04:17 +0100 | [diff] [blame] | 259 | .end = acpi_pm_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 262 | /* |
| 263 | * Toshiba fails to preserve interrupts over S1, reinitialization |
| 264 | * of 8259 is needed after S1 resume. |
| 265 | */ |
Jeff Garzik | 1855256 | 2007-10-03 15:15:40 -0400 | [diff] [blame] | 266 | static int __init init_ints_after_s1(const struct dmi_system_id *d) |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 267 | { |
| 268 | printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident); |
| 269 | init_8259A_after_S1 = 1; |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static struct dmi_system_id __initdata acpisleep_dmi_table[] = { |
| 274 | { |
| 275 | .callback = init_ints_after_s1, |
| 276 | .ident = "Toshiba Satellite 4030cdt", |
| 277 | .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),}, |
| 278 | }, |
| 279 | {}, |
| 280 | }; |
| 281 | #endif /* CONFIG_SUSPEND */ |
| 282 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 283 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 284 | static int acpi_hibernation_begin(void) |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 285 | { |
| 286 | acpi_target_sleep_state = ACPI_STATE_S4; |
| 287 | return 0; |
| 288 | } |
| 289 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 290 | static int acpi_hibernation_prepare(void) |
| 291 | { |
Rafael J. Wysocki | 3c1d2b6 | 2008-01-08 00:06:16 +0100 | [diff] [blame] | 292 | int error; |
| 293 | |
| 294 | error = acpi_sleep_prepare(ACPI_STATE_S4); |
| 295 | if (error) |
| 296 | return error; |
| 297 | |
| 298 | if (!ACPI_SUCCESS(acpi_hw_disable_all_gpes())) |
| 299 | error = -EFAULT; |
| 300 | |
| 301 | return error; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int acpi_hibernation_enter(void) |
| 305 | { |
| 306 | acpi_status status = AE_OK; |
| 307 | unsigned long flags = 0; |
| 308 | |
| 309 | ACPI_FLUSH_CPU_CACHE(); |
| 310 | |
| 311 | local_irq_save(flags); |
| 312 | acpi_enable_wakeup_device(ACPI_STATE_S4); |
| 313 | /* This shouldn't return. If it returns, we have a problem */ |
| 314 | status = acpi_enter_sleep_state(ACPI_STATE_S4); |
Rafael J. Wysocki | c95d47a | 2008-01-08 00:05:21 +0100 | [diff] [blame] | 315 | /* Reprogram control registers and execute _BFS */ |
| 316 | acpi_leave_sleep_state_prep(ACPI_STATE_S4); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 317 | local_irq_restore(flags); |
| 318 | |
| 319 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 320 | } |
| 321 | |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 322 | static void acpi_hibernation_leave(void) |
| 323 | { |
| 324 | /* |
| 325 | * If ACPI is not enabled by the BIOS and the boot kernel, we need to |
| 326 | * enable it here. |
| 327 | */ |
| 328 | acpi_enable(); |
Rafael J. Wysocki | c95d47a | 2008-01-08 00:05:21 +0100 | [diff] [blame] | 329 | /* Reprogram control registers and execute _BFS */ |
| 330 | acpi_leave_sleep_state_prep(ACPI_STATE_S4); |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 333 | static void acpi_hibernation_finish(void) |
| 334 | { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 335 | acpi_disable_wakeup_device(ACPI_STATE_S4); |
Alexey Starikovskiy | 1dbc1fda | 2007-10-22 14:18:12 +0400 | [diff] [blame] | 336 | acpi_leave_sleep_state(ACPI_STATE_S4); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 337 | |
| 338 | /* reset firmware waking vector */ |
| 339 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 340 | |
| 341 | acpi_target_sleep_state = ACPI_STATE_S0; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 344 | static void acpi_hibernation_end(void) |
| 345 | { |
| 346 | /* |
| 347 | * This is necessary in case acpi_hibernation_finish() is not called |
| 348 | * during a failing transition to the sleep state. |
| 349 | */ |
| 350 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 351 | } |
| 352 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 353 | static int acpi_hibernation_pre_restore(void) |
| 354 | { |
| 355 | acpi_status status; |
| 356 | |
| 357 | status = acpi_hw_disable_all_gpes(); |
| 358 | |
| 359 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 360 | } |
| 361 | |
| 362 | static void acpi_hibernation_restore_cleanup(void) |
| 363 | { |
| 364 | acpi_hw_enable_all_runtime_gpes(); |
| 365 | } |
| 366 | |
Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 367 | static struct platform_hibernation_ops acpi_hibernation_ops = { |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 368 | .begin = acpi_hibernation_begin, |
| 369 | .end = acpi_hibernation_end, |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 370 | .pre_snapshot = acpi_hibernation_prepare, |
| 371 | .finish = acpi_hibernation_finish, |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 372 | .prepare = acpi_hibernation_prepare, |
| 373 | .enter = acpi_hibernation_enter, |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 374 | .leave = acpi_hibernation_leave, |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 375 | .pre_restore = acpi_hibernation_pre_restore, |
| 376 | .restore_cleanup = acpi_hibernation_restore_cleanup, |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 377 | }; |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 378 | #endif /* CONFIG_HIBERNATION */ |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 379 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 380 | int acpi_suspend(u32 acpi_state) |
| 381 | { |
| 382 | suspend_state_t states[] = { |
| 383 | [1] = PM_SUSPEND_STANDBY, |
| 384 | [3] = PM_SUSPEND_MEM, |
| 385 | [5] = PM_SUSPEND_MAX |
| 386 | }; |
| 387 | |
| 388 | if (acpi_state < 6 && states[acpi_state]) |
| 389 | return pm_suspend(states[acpi_state]); |
| 390 | if (acpi_state == 4) |
| 391 | return hibernate(); |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 395 | #ifdef CONFIG_PM_SLEEP |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 396 | /** |
| 397 | * acpi_pm_device_sleep_state - return preferred power state of ACPI device |
| 398 | * in the system sleep state given by %acpi_target_sleep_state |
| 399 | * @dev: device to examine |
| 400 | * @wake: if set, the device should be able to wake up the system |
| 401 | * @d_min_p: used to store the upper limit of allowed states range |
| 402 | * Return value: preferred power state of the device on success, -ENODEV on |
| 403 | * failure (ie. if there's no 'struct acpi_device' for @dev) |
| 404 | * |
| 405 | * Find the lowest power (highest number) ACPI device power state that |
| 406 | * device @dev can be in while the system is in the sleep state represented |
| 407 | * by %acpi_target_sleep_state. If @wake is nonzero, the device should be |
| 408 | * able to wake up the system from this sleep state. If @d_min_p is set, |
| 409 | * the highest power (lowest number) device power state of @dev allowed |
| 410 | * in this system sleep state is stored at the location pointed to by it. |
| 411 | * |
| 412 | * The caller must ensure that @dev is valid before using this function. |
| 413 | * The caller is also responsible for figuring out if the device is |
| 414 | * supposed to be able to wake up the system and passing this information |
| 415 | * via @wake. |
| 416 | */ |
| 417 | |
| 418 | int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p) |
| 419 | { |
| 420 | acpi_handle handle = DEVICE_ACPI_HANDLE(dev); |
| 421 | struct acpi_device *adev; |
| 422 | char acpi_method[] = "_SxD"; |
| 423 | unsigned long d_min, d_max; |
| 424 | |
| 425 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { |
Shaohua Li | ead7759 | 2007-08-23 15:01:13 +0800 | [diff] [blame] | 426 | printk(KERN_DEBUG "ACPI handle has no context!\n"); |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 427 | return -ENODEV; |
| 428 | } |
| 429 | |
| 430 | acpi_method[2] = '0' + acpi_target_sleep_state; |
| 431 | /* |
| 432 | * If the sleep state is S0, we will return D3, but if the device has |
| 433 | * _S0W, we will use the value from _S0W |
| 434 | */ |
| 435 | d_min = ACPI_STATE_D0; |
| 436 | d_max = ACPI_STATE_D3; |
| 437 | |
| 438 | /* |
| 439 | * If present, _SxD methods return the minimum D-state (highest power |
| 440 | * state) we can use for the corresponding S-states. Otherwise, the |
| 441 | * minimum D-state is D0 (ACPI 3.x). |
| 442 | * |
| 443 | * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer |
| 444 | * provided -- that's our fault recovery, we ignore retval. |
| 445 | */ |
| 446 | if (acpi_target_sleep_state > ACPI_STATE_S0) |
| 447 | acpi_evaluate_integer(handle, acpi_method, NULL, &d_min); |
| 448 | |
| 449 | /* |
| 450 | * If _PRW says we can wake up the system from the target sleep state, |
| 451 | * the D-state returned by _SxD is sufficient for that (we assume a |
| 452 | * wakeup-aware driver if wake is set). Still, if _SxW exists |
| 453 | * (ACPI 3.x), it should return the maximum (lowest power) D-state that |
| 454 | * can wake the system. _S0W may be valid, too. |
| 455 | */ |
| 456 | if (acpi_target_sleep_state == ACPI_STATE_S0 || |
| 457 | (wake && adev->wakeup.state.enabled && |
| 458 | adev->wakeup.sleep_state <= acpi_target_sleep_state)) { |
| 459 | acpi_method[3] = 'W'; |
| 460 | acpi_evaluate_integer(handle, acpi_method, NULL, &d_max); |
| 461 | /* Sanity check */ |
| 462 | if (d_max < d_min) |
| 463 | d_min = d_max; |
| 464 | } |
| 465 | |
| 466 | if (d_min_p) |
| 467 | *d_min_p = d_min; |
| 468 | return d_max; |
| 469 | } |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 470 | #endif |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 471 | |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 472 | static void acpi_power_off_prepare(void) |
| 473 | { |
| 474 | /* Prepare to power off the system */ |
| 475 | acpi_sleep_prepare(ACPI_STATE_S5); |
Rafael J. Wysocki | 3c1d2b6 | 2008-01-08 00:06:16 +0100 | [diff] [blame] | 476 | acpi_hw_disable_all_gpes(); |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void acpi_power_off(void) |
| 480 | { |
| 481 | /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */ |
| 482 | printk("%s called\n", __FUNCTION__); |
| 483 | local_irq_disable(); |
Alexey Starikovskiy | 9c1c6a1 | 2007-10-22 14:18:06 +0400 | [diff] [blame] | 484 | acpi_enable_wakeup_device(ACPI_STATE_S5); |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 485 | acpi_enter_sleep_state(ACPI_STATE_S5); |
| 486 | } |
| 487 | |
Alexey Starikovskiy | aafbcd1 | 2007-02-10 01:32:16 -0500 | [diff] [blame] | 488 | int __init acpi_sleep_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 490 | acpi_status status; |
| 491 | u8 type_a, type_b; |
| 492 | #ifdef CONFIG_SUSPEND |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 493 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | dmi_check_system(acpisleep_dmi_table); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 496 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | if (acpi_disabled) |
| 499 | return 0; |
| 500 | |
Frans Pop | 5a50fe7 | 2007-09-20 22:27:44 +0200 | [diff] [blame] | 501 | sleep_states[ACPI_STATE_S0] = 1; |
| 502 | printk(KERN_INFO PREFIX "(supports S0"); |
| 503 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 504 | #ifdef CONFIG_SUSPEND |
Frans Pop | 5a50fe7 | 2007-09-20 22:27:44 +0200 | [diff] [blame] | 505 | for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | status = acpi_get_sleep_type_data(i, &type_a, &type_b); |
| 507 | if (ACPI_SUCCESS(status)) { |
| 508 | sleep_states[i] = 1; |
| 509 | printk(" S%d", i); |
| 510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 513 | suspend_set_ops(&acpi_pm_ops); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 514 | #endif |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 515 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 516 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 517 | status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b); |
| 518 | if (ACPI_SUCCESS(status)) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 519 | hibernation_set_ops(&acpi_hibernation_ops); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 520 | sleep_states[ACPI_STATE_S4] = 1; |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 521 | printk(" S4"); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 522 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 523 | #endif |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 524 | status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b); |
| 525 | if (ACPI_SUCCESS(status)) { |
| 526 | sleep_states[ACPI_STATE_S5] = 1; |
| 527 | printk(" S5"); |
| 528 | pm_power_off_prepare = acpi_power_off_prepare; |
| 529 | pm_power_off = acpi_power_off; |
| 530 | } |
| 531 | printk(")\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return 0; |
| 533 | } |