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