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