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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <acpi/acpi_bus.h> |
| 19 | #include <acpi/acpi_drivers.h> |
| 20 | #include "sleep.h" |
| 21 | |
| 22 | u8 sleep_states[ACPI_S_STATE_COUNT]; |
| 23 | |
| 24 | static struct pm_ops acpi_pm_ops; |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | extern void do_suspend_lowlevel(void); |
| 27 | |
| 28 | static u32 acpi_suspend_states[] = { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 29 | [PM_SUSPEND_ON] = ACPI_STATE_S0, |
| 30 | [PM_SUSPEND_STANDBY] = ACPI_STATE_S1, |
| 31 | [PM_SUSPEND_MEM] = ACPI_STATE_S3, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 32 | [PM_SUSPEND_MAX] = ACPI_STATE_S5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static int init_8259A_after_S1; |
| 36 | |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 37 | extern int acpi_sleep_prepare(u32 acpi_state); |
| 38 | extern void acpi_power_off(void); |
| 39 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 40 | static u32 acpi_target_sleep_state = ACPI_STATE_S0; |
| 41 | |
| 42 | /** |
| 43 | * acpi_pm_set_target - Set the target system sleep state to the state |
| 44 | * associated with given @pm_state, if supported. |
| 45 | */ |
| 46 | |
| 47 | static int acpi_pm_set_target(suspend_state_t pm_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | u32 acpi_state = acpi_suspend_states[pm_state]; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 50 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 52 | if (sleep_states[acpi_state]) { |
| 53 | acpi_target_sleep_state = acpi_state; |
| 54 | } else { |
| 55 | printk(KERN_ERR "ACPI does not support this state: %d\n", |
| 56 | pm_state); |
| 57 | error = -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 59 | return error; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * acpi_pm_prepare - Do preliminary suspend work. |
| 64 | * @pm_state: ignored |
| 65 | * |
| 66 | * If necessary, set the firmware waking vector and do arch-specific |
| 67 | * nastiness to get the wakeup code to the waking vector. |
| 68 | */ |
| 69 | |
| 70 | static int acpi_pm_prepare(suspend_state_t pm_state) |
| 71 | { |
| 72 | int error = acpi_sleep_prepare(acpi_target_sleep_state); |
| 73 | |
| 74 | if (error) |
| 75 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 76 | |
| 77 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /** |
| 81 | * acpi_pm_enter - Actually enter a sleep state. |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 82 | * @pm_state: ignored |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 84 | * Flush caches and go to sleep. For STR or S2, we have to call |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * arch-specific assembly, which in turn call acpi_enter_sleep_state(). |
| 86 | * It's unfortunate, but it works. Please fix if you're feeling frisky. |
| 87 | */ |
| 88 | |
| 89 | static int acpi_pm_enter(suspend_state_t pm_state) |
| 90 | { |
| 91 | acpi_status status = AE_OK; |
| 92 | unsigned long flags = 0; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 93 | u32 acpi_state = acpi_target_sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | ACPI_FLUSH_CPU_CACHE(); |
| 96 | |
| 97 | /* Do arch specific saving of state. */ |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 98 | if (acpi_state == ACPI_STATE_S2 || acpi_state == ACPI_STATE_S3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | int error = acpi_save_state_mem(); |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 100 | |
| 101 | if (error) { |
| 102 | acpi_target_sleep_state = ACPI_STATE_S0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return error; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | local_irq_save(flags); |
| 108 | acpi_enable_wakeup_device(acpi_state); |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 109 | switch (acpi_state) { |
| 110 | case ACPI_STATE_S1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | barrier(); |
| 112 | status = acpi_enter_sleep_state(acpi_state); |
| 113 | break; |
| 114 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 115 | case ACPI_STATE_S2: |
| 116 | case ACPI_STATE_S3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | do_suspend_lowlevel(); |
| 118 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Arnaud Patard | 872d83d | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 120 | |
| 121 | /* ACPI 3.0 specs (P62) says that it's the responsabilty |
| 122 | * of the OSPM to clear the status bit [ implying that the |
| 123 | * POWER_BUTTON event should not reach userspace ] |
| 124 | */ |
| 125 | if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) |
| 126 | acpi_clear_event(ACPI_EVENT_POWER_BUTTON); |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | local_irq_restore(flags); |
| 129 | printk(KERN_DEBUG "Back to C!\n"); |
| 130 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 131 | /* restore processor state */ |
| 132 | if (acpi_state == ACPI_STATE_S2 || acpi_state == ACPI_STATE_S3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | acpi_restore_state_mem(); |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /** |
| 139 | * acpi_pm_finish - Finish up suspend sequence. |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 140 | * @pm_state: ignored |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * |
| 142 | * This is called after we wake back up (or if entering the sleep state |
| 143 | * failed). |
| 144 | */ |
| 145 | |
| 146 | static int acpi_pm_finish(suspend_state_t pm_state) |
| 147 | { |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 148 | u32 acpi_state = acpi_target_sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | acpi_leave_sleep_state(acpi_state); |
| 151 | acpi_disable_wakeup_device(acpi_state); |
| 152 | |
| 153 | /* reset firmware waking vector */ |
| 154 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
| 155 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 156 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (init_8259A_after_S1) { |
| 159 | printk("Broken toshiba laptop -> kicking interrupts\n"); |
| 160 | init_8259A(0); |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | int acpi_suspend(u32 acpi_state) |
| 166 | { |
| 167 | suspend_state_t states[] = { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 168 | [1] = PM_SUSPEND_STANDBY, |
| 169 | [3] = PM_SUSPEND_MEM, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 170 | [5] = PM_SUSPEND_MAX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 173 | if (acpi_state < 6 && states[acpi_state]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return pm_suspend(states[acpi_state]); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 175 | if (acpi_state == 4) |
| 176 | return hibernate(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 180 | static int acpi_pm_state_valid(suspend_state_t pm_state) |
| 181 | { |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 182 | u32 acpi_state; |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 183 | |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 184 | switch (pm_state) { |
| 185 | case PM_SUSPEND_ON: |
| 186 | case PM_SUSPEND_STANDBY: |
| 187 | case PM_SUSPEND_MEM: |
| 188 | acpi_state = acpi_suspend_states[pm_state]; |
| 189 | |
| 190 | return sleep_states[acpi_state]; |
| 191 | default: |
| 192 | return 0; |
| 193 | } |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | static struct pm_ops acpi_pm_ops = { |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 197 | .valid = acpi_pm_state_valid, |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame^] | 198 | .set_target = acpi_pm_set_target, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 199 | .prepare = acpi_pm_prepare, |
| 200 | .enter = acpi_pm_enter, |
| 201 | .finish = acpi_pm_finish, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 204 | #ifdef CONFIG_SOFTWARE_SUSPEND |
| 205 | static int acpi_hibernation_prepare(void) |
| 206 | { |
| 207 | return acpi_sleep_prepare(ACPI_STATE_S4); |
| 208 | } |
| 209 | |
| 210 | static int acpi_hibernation_enter(void) |
| 211 | { |
| 212 | acpi_status status = AE_OK; |
| 213 | unsigned long flags = 0; |
| 214 | |
| 215 | ACPI_FLUSH_CPU_CACHE(); |
| 216 | |
| 217 | local_irq_save(flags); |
| 218 | acpi_enable_wakeup_device(ACPI_STATE_S4); |
| 219 | /* This shouldn't return. If it returns, we have a problem */ |
| 220 | status = acpi_enter_sleep_state(ACPI_STATE_S4); |
| 221 | local_irq_restore(flags); |
| 222 | |
| 223 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 224 | } |
| 225 | |
| 226 | static void acpi_hibernation_finish(void) |
| 227 | { |
| 228 | acpi_leave_sleep_state(ACPI_STATE_S4); |
| 229 | acpi_disable_wakeup_device(ACPI_STATE_S4); |
| 230 | |
| 231 | /* reset firmware waking vector */ |
| 232 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
| 233 | |
| 234 | if (init_8259A_after_S1) { |
| 235 | printk("Broken toshiba laptop -> kicking interrupts\n"); |
| 236 | init_8259A(0); |
| 237 | } |
| 238 | } |
| 239 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 240 | static int acpi_hibernation_pre_restore(void) |
| 241 | { |
| 242 | acpi_status status; |
| 243 | |
| 244 | status = acpi_hw_disable_all_gpes(); |
| 245 | |
| 246 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 247 | } |
| 248 | |
| 249 | static void acpi_hibernation_restore_cleanup(void) |
| 250 | { |
| 251 | acpi_hw_enable_all_runtime_gpes(); |
| 252 | } |
| 253 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 254 | static struct hibernation_ops acpi_hibernation_ops = { |
| 255 | .prepare = acpi_hibernation_prepare, |
| 256 | .enter = acpi_hibernation_enter, |
| 257 | .finish = acpi_hibernation_finish, |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 258 | .pre_restore = acpi_hibernation_pre_restore, |
| 259 | .restore_cleanup = acpi_hibernation_restore_cleanup, |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 260 | }; |
Len Brown | fd35094 | 2007-05-09 23:34:35 -0400 | [diff] [blame] | 261 | #endif /* CONFIG_SOFTWARE_SUSPEND */ |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* |
| 264 | * Toshiba fails to preserve interrupts over S1, reinitialization |
| 265 | * of 8259 is needed after S1 resume. |
| 266 | */ |
| 267 | static int __init init_ints_after_s1(struct dmi_system_id *d) |
| 268 | { |
| 269 | printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident); |
| 270 | init_8259A_after_S1 = 1; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static struct dmi_system_id __initdata acpisleep_dmi_table[] = { |
| 275 | { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 276 | .callback = init_ints_after_s1, |
| 277 | .ident = "Toshiba Satellite 4030cdt", |
| 278 | .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),}, |
| 279 | }, |
| 280 | {}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | }; |
| 282 | |
Alexey Starikovskiy | aafbcd1 | 2007-02-10 01:32:16 -0500 | [diff] [blame] | 283 | int __init acpi_sleep_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 285 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | dmi_check_system(acpisleep_dmi_table); |
| 288 | |
| 289 | if (acpi_disabled) |
| 290 | return 0; |
| 291 | |
| 292 | printk(KERN_INFO PREFIX "(supports"); |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 293 | for (i = 0; i < ACPI_S_STATE_COUNT; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | acpi_status status; |
| 295 | u8 type_a, type_b; |
| 296 | status = acpi_get_sleep_type_data(i, &type_a, &type_b); |
| 297 | if (ACPI_SUCCESS(status)) { |
| 298 | sleep_states[i] = 1; |
| 299 | printk(" S%d", i); |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | printk(")\n"); |
| 303 | |
| 304 | pm_set_ops(&acpi_pm_ops); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 305 | |
| 306 | #ifdef CONFIG_SOFTWARE_SUSPEND |
| 307 | if (sleep_states[ACPI_STATE_S4]) |
| 308 | hibernation_set_ops(&acpi_hibernation_ops); |
| 309 | #else |
| 310 | sleep_states[ACPI_STATE_S4] = 0; |
| 311 | #endif |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return 0; |
| 314 | } |