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