Bob Moore | 2feec47 | 2012-02-14 15:00:53 +0800 | [diff] [blame^] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces |
| 4 | * |
| 5 | *****************************************************************************/ |
| 6 | |
| 7 | /* |
| 8 | * Copyright (C) 2000 - 2012, Intel Corp. |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions, and the following disclaimer, |
| 16 | * without modification. |
| 17 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer |
| 18 | * substantially similar to the "NO WARRANTY" disclaimer below |
| 19 | * ("Disclaimer") and any redistribution must be conditioned upon |
| 20 | * including a substantially similar Disclaimer requirement for further |
| 21 | * binary redistribution. |
| 22 | * 3. Neither the names of the above-listed copyright holders nor the names |
| 23 | * of any contributors may be used to endorse or promote products derived |
| 24 | * from this software without specific prior written permission. |
| 25 | * |
| 26 | * Alternatively, this software may be distributed under the terms of the |
| 27 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 28 | * Software Foundation. |
| 29 | * |
| 30 | * NO WARRANTY |
| 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR |
| 34 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 35 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 37 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 40 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 41 | * POSSIBILITY OF SUCH DAMAGES. |
| 42 | */ |
| 43 | |
| 44 | #include <acpi/acpi.h> |
| 45 | #include "accommon.h" |
| 46 | #include <linux/module.h> |
| 47 | |
| 48 | #define _COMPONENT ACPI_HARDWARE |
| 49 | ACPI_MODULE_NAME("hwxfsleep") |
| 50 | |
| 51 | /******************************************************************************* |
| 52 | * |
| 53 | * FUNCTION: acpi_set_firmware_waking_vector |
| 54 | * |
| 55 | * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode |
| 56 | * entry point. |
| 57 | * |
| 58 | * RETURN: Status |
| 59 | * |
| 60 | * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS |
| 61 | * |
| 62 | ******************************************************************************/ |
| 63 | acpi_status acpi_set_firmware_waking_vector(u32 physical_address) |
| 64 | { |
| 65 | ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector); |
| 66 | |
| 67 | |
| 68 | /* |
| 69 | * According to the ACPI specification 2.0c and later, the 64-bit |
| 70 | * waking vector should be cleared and the 32-bit waking vector should |
| 71 | * be used, unless we want the wake-up code to be called by the BIOS in |
| 72 | * Protected Mode. Some systems (for example HP dv5-1004nr) are known |
| 73 | * to fail to resume if the 64-bit vector is used. |
| 74 | */ |
| 75 | |
| 76 | /* Set the 32-bit vector */ |
| 77 | |
| 78 | acpi_gbl_FACS->firmware_waking_vector = physical_address; |
| 79 | |
| 80 | /* Clear the 64-bit vector if it exists */ |
| 81 | |
| 82 | if ((acpi_gbl_FACS->length > 32) && (acpi_gbl_FACS->version >= 1)) { |
| 83 | acpi_gbl_FACS->xfirmware_waking_vector = 0; |
| 84 | } |
| 85 | |
| 86 | return_ACPI_STATUS(AE_OK); |
| 87 | } |
| 88 | |
| 89 | ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector) |
| 90 | |
| 91 | #if ACPI_MACHINE_WIDTH == 64 |
| 92 | /******************************************************************************* |
| 93 | * |
| 94 | * FUNCTION: acpi_set_firmware_waking_vector64 |
| 95 | * |
| 96 | * PARAMETERS: physical_address - 64-bit physical address of ACPI protected |
| 97 | * mode entry point. |
| 98 | * |
| 99 | * RETURN: Status |
| 100 | * |
| 101 | * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if |
| 102 | * it exists in the table. This function is intended for use with |
| 103 | * 64-bit host operating systems. |
| 104 | * |
| 105 | ******************************************************************************/ |
| 106 | acpi_status acpi_set_firmware_waking_vector64(u64 physical_address) |
| 107 | { |
| 108 | ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64); |
| 109 | |
| 110 | |
| 111 | /* Determine if the 64-bit vector actually exists */ |
| 112 | |
| 113 | if ((acpi_gbl_FACS->length <= 32) || (acpi_gbl_FACS->version < 1)) { |
| 114 | return_ACPI_STATUS(AE_NOT_EXIST); |
| 115 | } |
| 116 | |
| 117 | /* Clear 32-bit vector, set the 64-bit X_ vector */ |
| 118 | |
| 119 | acpi_gbl_FACS->firmware_waking_vector = 0; |
| 120 | acpi_gbl_FACS->xfirmware_waking_vector = physical_address; |
| 121 | return_ACPI_STATUS(AE_OK); |
| 122 | } |
| 123 | |
| 124 | ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64) |
| 125 | #endif |
| 126 | |
| 127 | /******************************************************************************* |
| 128 | * |
| 129 | * FUNCTION: acpi_enter_sleep_state_s4bios |
| 130 | * |
| 131 | * PARAMETERS: None |
| 132 | * |
| 133 | * RETURN: Status |
| 134 | * |
| 135 | * DESCRIPTION: Perform a S4 bios request. |
| 136 | * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED |
| 137 | * |
| 138 | ******************************************************************************/ |
| 139 | acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void) |
| 140 | { |
| 141 | u32 in_value; |
| 142 | acpi_status status; |
| 143 | |
| 144 | ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios); |
| 145 | |
| 146 | /* Clear the wake status bit (PM1) */ |
| 147 | |
| 148 | status = |
| 149 | acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS); |
| 150 | if (ACPI_FAILURE(status)) { |
| 151 | return_ACPI_STATUS(status); |
| 152 | } |
| 153 | |
| 154 | status = acpi_hw_clear_acpi_status(); |
| 155 | if (ACPI_FAILURE(status)) { |
| 156 | return_ACPI_STATUS(status); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * 1) Disable/Clear all GPEs |
| 161 | * 2) Enable all wakeup GPEs |
| 162 | */ |
| 163 | status = acpi_hw_disable_all_gpes(); |
| 164 | if (ACPI_FAILURE(status)) { |
| 165 | return_ACPI_STATUS(status); |
| 166 | } |
| 167 | acpi_gbl_system_awake_and_running = FALSE; |
| 168 | |
| 169 | status = acpi_hw_enable_all_wakeup_gpes(); |
| 170 | if (ACPI_FAILURE(status)) { |
| 171 | return_ACPI_STATUS(status); |
| 172 | } |
| 173 | |
| 174 | ACPI_FLUSH_CPU_CACHE(); |
| 175 | |
| 176 | status = acpi_hw_write_port(acpi_gbl_FADT.smi_command, |
| 177 | (u32)acpi_gbl_FADT.S4bios_request, 8); |
| 178 | |
| 179 | do { |
| 180 | acpi_os_stall(1000); |
| 181 | status = |
| 182 | acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value); |
| 183 | if (ACPI_FAILURE(status)) { |
| 184 | return_ACPI_STATUS(status); |
| 185 | } |
| 186 | } while (!in_value); |
| 187 | |
| 188 | return_ACPI_STATUS(AE_OK); |
| 189 | } |
| 190 | |
| 191 | ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios) |
| 192 | |
| 193 | /******************************************************************************* |
| 194 | * |
| 195 | * FUNCTION: acpi_enter_sleep_state_prep |
| 196 | * |
| 197 | * PARAMETERS: sleep_state - Which sleep state to enter |
| 198 | * |
| 199 | * RETURN: Status |
| 200 | * |
| 201 | * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231) |
| 202 | * This function must execute with interrupts enabled. |
| 203 | * We break sleeping into 2 stages so that OSPM can handle |
| 204 | * various OS-specific tasks between the two steps. |
| 205 | * |
| 206 | ******************************************************************************/ |
| 207 | acpi_status acpi_enter_sleep_state_prep(u8 sleep_state) |
| 208 | { |
| 209 | acpi_status status; |
| 210 | struct acpi_object_list arg_list; |
| 211 | union acpi_object arg; |
| 212 | u32 sst_value; |
| 213 | |
| 214 | ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep); |
| 215 | |
| 216 | /* _PSW methods could be run here to enable wake-on keyboard, LAN, etc. */ |
| 217 | |
| 218 | status = acpi_get_sleep_type_data(sleep_state, |
| 219 | &acpi_gbl_sleep_type_a, |
| 220 | &acpi_gbl_sleep_type_b); |
| 221 | if (ACPI_FAILURE(status)) { |
| 222 | return_ACPI_STATUS(status); |
| 223 | } |
| 224 | |
| 225 | /* Execute the _PTS method (Prepare To Sleep) */ |
| 226 | |
| 227 | arg_list.count = 1; |
| 228 | arg_list.pointer = &arg; |
| 229 | arg.type = ACPI_TYPE_INTEGER; |
| 230 | arg.integer.value = sleep_state; |
| 231 | |
| 232 | status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL); |
| 233 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
| 234 | return_ACPI_STATUS(status); |
| 235 | } |
| 236 | |
| 237 | /* Setup the argument to the _SST method (System STatus) */ |
| 238 | |
| 239 | switch (sleep_state) { |
| 240 | case ACPI_STATE_S0: |
| 241 | sst_value = ACPI_SST_WORKING; |
| 242 | break; |
| 243 | |
| 244 | case ACPI_STATE_S1: |
| 245 | case ACPI_STATE_S2: |
| 246 | case ACPI_STATE_S3: |
| 247 | sst_value = ACPI_SST_SLEEPING; |
| 248 | break; |
| 249 | |
| 250 | case ACPI_STATE_S4: |
| 251 | sst_value = ACPI_SST_SLEEP_CONTEXT; |
| 252 | break; |
| 253 | |
| 254 | default: |
| 255 | sst_value = ACPI_SST_INDICATOR_OFF; /* Default is off */ |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Set the system indicators to show the desired sleep state. |
| 261 | * _SST is an optional method (return no error if not found) |
| 262 | */ |
| 263 | acpi_hw_execute_SST(sst_value); |
| 264 | return_ACPI_STATUS(AE_OK); |
| 265 | } |
| 266 | |
| 267 | ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep) |
| 268 | |
| 269 | /******************************************************************************* |
| 270 | * |
| 271 | * FUNCTION: acpi_enter_sleep_state |
| 272 | * |
| 273 | * PARAMETERS: sleep_state - Which sleep state to enter |
| 274 | * |
| 275 | * RETURN: Status |
| 276 | * |
| 277 | * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231) |
| 278 | * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED |
| 279 | * |
| 280 | ******************************************************************************/ |
| 281 | acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state) |
| 282 | { |
| 283 | acpi_status status; |
| 284 | |
| 285 | ACPI_FUNCTION_TRACE(acpi_enter_sleep_state); |
| 286 | |
| 287 | if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) || |
| 288 | (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) { |
| 289 | ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X", |
| 290 | acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b)); |
| 291 | return_ACPI_STATUS(AE_AML_OPERAND_VALUE); |
| 292 | } |
| 293 | |
| 294 | /* If Hardware Reduced flag is set, must use the extended sleep registers */ |
| 295 | |
| 296 | if (acpi_gbl_reduced_hardware || acpi_gbl_FADT.sleep_control.address) { |
| 297 | status = acpi_hw_extended_sleep(sleep_state); |
| 298 | } else { |
| 299 | /* Legacy sleep */ |
| 300 | |
| 301 | status = acpi_hw_legacy_sleep(sleep_state); |
| 302 | } |
| 303 | |
| 304 | return_ACPI_STATUS(status); |
| 305 | } |
| 306 | |
| 307 | ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state) |
| 308 | |
| 309 | /******************************************************************************* |
| 310 | * |
| 311 | * FUNCTION: acpi_leave_sleep_state_prep |
| 312 | * |
| 313 | * PARAMETERS: sleep_state - Which sleep state we are exiting |
| 314 | * |
| 315 | * RETURN: Status |
| 316 | * |
| 317 | * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a |
| 318 | * sleep. |
| 319 | * Called with interrupts DISABLED. |
| 320 | * |
| 321 | ******************************************************************************/ |
| 322 | acpi_status acpi_leave_sleep_state_prep(u8 sleep_state) |
| 323 | { |
| 324 | acpi_status status; |
| 325 | |
| 326 | ACPI_FUNCTION_TRACE(acpi_leave_sleep_state); |
| 327 | |
| 328 | |
| 329 | /* If Hardware Reduced flag is set, must use the extended sleep registers */ |
| 330 | |
| 331 | if (acpi_gbl_reduced_hardware || acpi_gbl_FADT.sleep_control.address) { |
| 332 | status = acpi_hw_extended_wake_prep(sleep_state); |
| 333 | } else { |
| 334 | /* Legacy sleep */ |
| 335 | |
| 336 | status = acpi_hw_legacy_wake_prep(sleep_state); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | return_ACPI_STATUS(status); |
| 341 | } |
| 342 | |
| 343 | ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep) |
| 344 | |
| 345 | /******************************************************************************* |
| 346 | * |
| 347 | * FUNCTION: acpi_leave_sleep_state |
| 348 | * |
| 349 | * PARAMETERS: sleep_state - Which sleep state we just exited |
| 350 | * |
| 351 | * RETURN: Status |
| 352 | * |
| 353 | * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep |
| 354 | * Called with interrupts ENABLED. |
| 355 | * |
| 356 | ******************************************************************************/ |
| 357 | acpi_status acpi_leave_sleep_state(u8 sleep_state) |
| 358 | { |
| 359 | acpi_status status; |
| 360 | |
| 361 | ACPI_FUNCTION_TRACE(acpi_leave_sleep_state); |
| 362 | |
| 363 | |
| 364 | /* If Hardware Reduced flag is set, must use the extended sleep registers */ |
| 365 | |
| 366 | if (acpi_gbl_reduced_hardware || acpi_gbl_FADT.sleep_control.address) { |
| 367 | status = acpi_hw_extended_wake(sleep_state); |
| 368 | } else { |
| 369 | /* Legacy sleep */ |
| 370 | |
| 371 | status = acpi_hw_legacy_wake(sleep_state); |
| 372 | } |
| 373 | |
| 374 | return_ACPI_STATUS(status); |
| 375 | } |
| 376 | |
| 377 | ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state) |