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