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, |
| 32 | [PM_SUSPEND_DISK] = ACPI_STATE_S4, |
| 33 | [PM_SUSPEND_MAX] = ACPI_STATE_S5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static int init_8259A_after_S1; |
| 37 | |
| 38 | /** |
| 39 | * acpi_pm_prepare - Do preliminary suspend work. |
| 40 | * @pm_state: suspend state we're entering. |
| 41 | * |
| 42 | * Make sure we support the state. If we do, and we need it, set the |
| 43 | * firmware waking vector and do arch-specific nastiness to get the |
| 44 | * wakeup code to the waking vector. |
| 45 | */ |
| 46 | |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 47 | extern int acpi_sleep_prepare(u32 acpi_state); |
| 48 | extern void acpi_power_off(void); |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static int acpi_pm_prepare(suspend_state_t pm_state) |
| 51 | { |
| 52 | u32 acpi_state = acpi_suspend_states[pm_state]; |
| 53 | |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 54 | if (!sleep_states[acpi_state]) { |
| 55 | printk("acpi_pm_prepare does not support %d \n", pm_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 58 | return acpi_sleep_prepare(acpi_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /** |
| 62 | * acpi_pm_enter - Actually enter a sleep state. |
| 63 | * @pm_state: State we're entering. |
| 64 | * |
| 65 | * Flush caches and go to sleep. For STR or STD, we have to call |
| 66 | * arch-specific assembly, which in turn call acpi_enter_sleep_state(). |
| 67 | * It's unfortunate, but it works. Please fix if you're feeling frisky. |
| 68 | */ |
| 69 | |
| 70 | static int acpi_pm_enter(suspend_state_t pm_state) |
| 71 | { |
| 72 | acpi_status status = AE_OK; |
| 73 | unsigned long flags = 0; |
| 74 | u32 acpi_state = acpi_suspend_states[pm_state]; |
| 75 | |
| 76 | ACPI_FLUSH_CPU_CACHE(); |
| 77 | |
| 78 | /* Do arch specific saving of state. */ |
| 79 | if (pm_state > PM_SUSPEND_STANDBY) { |
| 80 | int error = acpi_save_state_mem(); |
| 81 | if (error) |
| 82 | return error; |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | local_irq_save(flags); |
| 86 | acpi_enable_wakeup_device(acpi_state); |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 87 | switch (pm_state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | case PM_SUSPEND_STANDBY: |
| 89 | barrier(); |
| 90 | status = acpi_enter_sleep_state(acpi_state); |
| 91 | break; |
| 92 | |
| 93 | case PM_SUSPEND_MEM: |
| 94 | do_suspend_lowlevel(); |
| 95 | break; |
| 96 | |
| 97 | case PM_SUSPEND_DISK: |
| 98 | if (acpi_pm_ops.pm_disk_mode == PM_DISK_PLATFORM) |
| 99 | status = acpi_enter_sleep_state(acpi_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | break; |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 101 | case PM_SUSPEND_MAX: |
| 102 | acpi_power_off(); |
| 103 | break; |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | default: |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | local_irq_restore(flags); |
| 109 | printk(KERN_DEBUG "Back to C!\n"); |
| 110 | |
| 111 | /* restore processor state |
| 112 | * We should only be here if we're coming back from STR or STD. |
| 113 | * And, in the case of the latter, the memory image should have already |
| 114 | * been loaded from disk. |
| 115 | */ |
| 116 | if (pm_state > PM_SUSPEND_STANDBY) |
| 117 | acpi_restore_state_mem(); |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /** |
| 123 | * acpi_pm_finish - Finish up suspend sequence. |
| 124 | * @pm_state: State we're coming out of. |
| 125 | * |
| 126 | * This is called after we wake back up (or if entering the sleep state |
| 127 | * failed). |
| 128 | */ |
| 129 | |
| 130 | static int acpi_pm_finish(suspend_state_t pm_state) |
| 131 | { |
| 132 | u32 acpi_state = acpi_suspend_states[pm_state]; |
| 133 | |
| 134 | acpi_leave_sleep_state(acpi_state); |
| 135 | acpi_disable_wakeup_device(acpi_state); |
| 136 | |
| 137 | /* reset firmware waking vector */ |
| 138 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
| 139 | |
| 140 | if (init_8259A_after_S1) { |
| 141 | printk("Broken toshiba laptop -> kicking interrupts\n"); |
| 142 | init_8259A(0); |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | int acpi_suspend(u32 acpi_state) |
| 148 | { |
| 149 | suspend_state_t states[] = { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 150 | [1] = PM_SUSPEND_STANDBY, |
| 151 | [3] = PM_SUSPEND_MEM, |
| 152 | [4] = PM_SUSPEND_DISK, |
| 153 | [5] = PM_SUSPEND_MAX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 156 | if (acpi_state < 6 && states[acpi_state]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return pm_suspend(states[acpi_state]); |
| 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 | { |
| 163 | u32 acpi_state = acpi_suspend_states[pm_state]; |
| 164 | |
| 165 | return sleep_states[acpi_state]; |
| 166 | } |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | static struct pm_ops acpi_pm_ops = { |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 169 | .valid = acpi_pm_state_valid, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 170 | .prepare = acpi_pm_prepare, |
| 171 | .enter = acpi_pm_enter, |
| 172 | .finish = acpi_pm_finish, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* |
| 176 | * Toshiba fails to preserve interrupts over S1, reinitialization |
| 177 | * of 8259 is needed after S1 resume. |
| 178 | */ |
| 179 | static int __init init_ints_after_s1(struct dmi_system_id *d) |
| 180 | { |
| 181 | printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident); |
| 182 | init_8259A_after_S1 = 1; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static struct dmi_system_id __initdata acpisleep_dmi_table[] = { |
| 187 | { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 188 | .callback = init_ints_after_s1, |
| 189 | .ident = "Toshiba Satellite 4030cdt", |
| 190 | .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),}, |
| 191 | }, |
| 192 | {}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static int __init acpi_sleep_init(void) |
| 196 | { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 197 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | dmi_check_system(acpisleep_dmi_table); |
| 200 | |
| 201 | if (acpi_disabled) |
| 202 | return 0; |
| 203 | |
| 204 | printk(KERN_INFO PREFIX "(supports"); |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 205 | for (i = 0; i < ACPI_S_STATE_COUNT; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | acpi_status status; |
| 207 | u8 type_a, type_b; |
| 208 | status = acpi_get_sleep_type_data(i, &type_a, &type_b); |
| 209 | if (ACPI_SUCCESS(status)) { |
| 210 | sleep_states[i] = 1; |
| 211 | printk(" S%d", i); |
| 212 | } |
| 213 | if (i == ACPI_STATE_S4) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (sleep_states[i]) |
| 215 | acpi_pm_ops.pm_disk_mode = PM_DISK_PLATFORM; |
| 216 | } |
| 217 | } |
| 218 | printk(")\n"); |
| 219 | |
| 220 | pm_set_ops(&acpi_pm_ops); |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | late_initcall(acpi_sleep_init); |