blob: 25cd9783b640aa3dd3ba5f90f9d761946d9539dd [file] [log] [blame]
Bob Moore2feec472012-02-14 15:00:53 +08001/******************************************************************************
2 *
3 * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
4 *
5 *****************************************************************************/
6
7/*
David E. Box82a80942015-02-05 15:20:45 +08008 * Copyright (C) 2000 - 2015, Intel Corp.
Bob Moore2feec472012-02-14 15:00:53 +08009 * 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
Lv Zheng839e9282013-10-29 09:29:51 +080044#define EXPORT_ACPI_INTERFACES
45
Bob Moore2feec472012-02-14 15:00:53 +080046#include <acpi/acpi.h>
47#include "accommon.h"
Bob Moore2feec472012-02-14 15:00:53 +080048
49#define _COMPONENT ACPI_HARDWARE
50ACPI_MODULE_NAME("hwxfsleep")
51
Bob Moore72a88872012-02-14 18:57:13 +080052/* Local prototypes */
Lv Zheng1f86e8c2012-10-31 02:25:45 +000053static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id);
Bob Moore72a88872012-02-14 18:57:13 +080054
55/*
56 * Dispatch table used to efficiently branch to the various sleep
57 * functions.
58 */
Lin Ming8a73b172012-03-21 10:01:49 +080059#define ACPI_SLEEP_FUNCTION_ID 0
60#define ACPI_WAKE_PREP_FUNCTION_ID 1
61#define ACPI_WAKE_FUNCTION_ID 2
Bob Moore72a88872012-02-14 18:57:13 +080062
63/* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
64
65static struct acpi_sleep_functions acpi_sleep_dispatch[] = {
66 {ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_sleep),
67 acpi_hw_extended_sleep},
68 {ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake_prep),
69 acpi_hw_extended_wake_prep},
70 {ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake), acpi_hw_extended_wake}
71};
72
73/*
74 * These functions are removed for the ACPI_REDUCED_HARDWARE case:
Lv Zhengaca2a5d2015-07-01 14:43:04 +080075 * acpi_set_firmware_waking_vectors
Bob Moore72a88872012-02-14 18:57:13 +080076 * acpi_set_firmware_waking_vector
77 * acpi_set_firmware_waking_vector64
78 * acpi_enter_sleep_state_s4bios
79 */
80
Bob Moore33620c52012-02-14 18:14:27 +080081#if (!ACPI_REDUCED_HARDWARE)
Bob Moore2feec472012-02-14 15:00:53 +080082/*******************************************************************************
83 *
Lv Zhengaca2a5d2015-07-01 14:43:04 +080084 * FUNCTION: acpi_set_firmware_waking_vectors
Bob Moore2feec472012-02-14 15:00:53 +080085 *
86 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
87 * entry point.
Lv Zhengaca2a5d2015-07-01 14:43:04 +080088 * physical_address64 - 64-bit physical address of ACPI protected
89 * mode entry point.
Bob Moore2feec472012-02-14 15:00:53 +080090 *
91 * RETURN: Status
92 *
Lv Zhengaca2a5d2015-07-01 14:43:04 +080093 * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
Bob Moore2feec472012-02-14 15:00:53 +080094 *
95 ******************************************************************************/
Bob Moore72a88872012-02-14 18:57:13 +080096
Lv Zhengaca2a5d2015-07-01 14:43:04 +080097acpi_status
98acpi_set_firmware_waking_vectors(acpi_physical_address physical_address,
99 acpi_physical_address physical_address64)
Bob Moore2feec472012-02-14 15:00:53 +0800100{
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800101 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vectors);
Bob Moore2feec472012-02-14 15:00:53 +0800102
103
104 /*
105 * According to the ACPI specification 2.0c and later, the 64-bit
106 * waking vector should be cleared and the 32-bit waking vector should
107 * be used, unless we want the wake-up code to be called by the BIOS in
108 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
109 * to fail to resume if the 64-bit vector is used.
110 */
111
112 /* Set the 32-bit vector */
113
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800114 acpi_gbl_FACS->firmware_waking_vector = (u32)physical_address;
Bob Moore2feec472012-02-14 15:00:53 +0800115
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800116 if (acpi_gbl_FACS->length > 32) {
117 if (acpi_gbl_FACS->version >= 1) {
Bob Moore2feec472012-02-14 15:00:53 +0800118
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800119 /* Set the 64-bit vector */
120
121 acpi_gbl_FACS->xfirmware_waking_vector =
122 physical_address64;
123 } else {
124 /* Clear the 64-bit vector if it exists */
125
126 acpi_gbl_FACS->xfirmware_waking_vector = 0;
127 }
Bob Moore2feec472012-02-14 15:00:53 +0800128 }
129
130 return_ACPI_STATUS(AE_OK);
131}
132
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800133ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vectors)
134
135/*******************************************************************************
136 *
137 * FUNCTION: acpi_set_firmware_waking_vector
138 *
139 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
140 * entry point.
141 *
142 * RETURN: Status
143 *
144 * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
145 *
146 ******************************************************************************/
147acpi_status acpi_set_firmware_waking_vector(u32 physical_address)
148{
149 acpi_status status;
150
151 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
152
153 status = acpi_set_firmware_waking_vectors((acpi_physical_address)
154 physical_address, 0);
155
156 return_ACPI_STATUS(status);
157}
158
Bob Moore2feec472012-02-14 15:00:53 +0800159ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
160
161#if ACPI_MACHINE_WIDTH == 64
162/*******************************************************************************
163 *
164 * FUNCTION: acpi_set_firmware_waking_vector64
165 *
166 * PARAMETERS: physical_address - 64-bit physical address of ACPI protected
167 * mode entry point.
168 *
169 * RETURN: Status
170 *
171 * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
172 * it exists in the table. This function is intended for use with
173 * 64-bit host operating systems.
174 *
175 ******************************************************************************/
176acpi_status acpi_set_firmware_waking_vector64(u64 physical_address)
177{
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800178 acpi_status status;
179
Bob Moore2feec472012-02-14 15:00:53 +0800180 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64);
181
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800182 status = acpi_set_firmware_waking_vectors(0,
183 (acpi_physical_address)
184 physical_address);
Bob Moore2feec472012-02-14 15:00:53 +0800185
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800186 return_ACPI_STATUS(status);
Bob Moore2feec472012-02-14 15:00:53 +0800187}
188
189ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64)
190#endif
Bob Moore2feec472012-02-14 15:00:53 +0800191/*******************************************************************************
192 *
193 * FUNCTION: acpi_enter_sleep_state_s4bios
194 *
195 * PARAMETERS: None
196 *
197 * RETURN: Status
198 *
199 * DESCRIPTION: Perform a S4 bios request.
200 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
201 *
202 ******************************************************************************/
Lv Zheng40bce102013-10-31 09:31:18 +0800203acpi_status acpi_enter_sleep_state_s4bios(void)
Bob Moore2feec472012-02-14 15:00:53 +0800204{
205 u32 in_value;
206 acpi_status status;
207
208 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
209
210 /* Clear the wake status bit (PM1) */
211
212 status =
213 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
214 if (ACPI_FAILURE(status)) {
215 return_ACPI_STATUS(status);
216 }
217
218 status = acpi_hw_clear_acpi_status();
219 if (ACPI_FAILURE(status)) {
220 return_ACPI_STATUS(status);
221 }
222
223 /*
224 * 1) Disable/Clear all GPEs
225 * 2) Enable all wakeup GPEs
226 */
227 status = acpi_hw_disable_all_gpes();
228 if (ACPI_FAILURE(status)) {
229 return_ACPI_STATUS(status);
230 }
231 acpi_gbl_system_awake_and_running = FALSE;
232
233 status = acpi_hw_enable_all_wakeup_gpes();
234 if (ACPI_FAILURE(status)) {
235 return_ACPI_STATUS(status);
236 }
237
238 ACPI_FLUSH_CPU_CACHE();
239
240 status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
Bob Mooreba494be2012-07-12 09:40:10 +0800241 (u32)acpi_gbl_FADT.s4_bios_request, 8);
Bob Moore2feec472012-02-14 15:00:53 +0800242
243 do {
Bob Moorec41679a2012-12-31 00:05:46 +0000244 acpi_os_stall(ACPI_USEC_PER_MSEC);
Bob Moore2feec472012-02-14 15:00:53 +0800245 status =
246 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
247 if (ACPI_FAILURE(status)) {
248 return_ACPI_STATUS(status);
249 }
250 } while (!in_value);
251
252 return_ACPI_STATUS(AE_OK);
253}
254
255ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Bob Moore33620c52012-02-14 18:14:27 +0800256#endif /* !ACPI_REDUCED_HARDWARE */
Bob Moore2feec472012-02-14 15:00:53 +0800257/*******************************************************************************
258 *
Bob Moore72a88872012-02-14 18:57:13 +0800259 * FUNCTION: acpi_hw_sleep_dispatch
260 *
261 * PARAMETERS: sleep_state - Which sleep state to enter/exit
262 * function_id - Sleep, wake_prep, or Wake
263 *
264 * RETURN: Status from the invoked sleep handling function.
265 *
266 * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
267 * function.
268 *
269 ******************************************************************************/
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000270static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id)
Bob Moore72a88872012-02-14 18:57:13 +0800271{
272 acpi_status status;
273 struct acpi_sleep_functions *sleep_functions =
274 &acpi_sleep_dispatch[function_id];
275
276#if (!ACPI_REDUCED_HARDWARE)
Bob Moore72a88872012-02-14 18:57:13 +0800277 /*
278 * If the Hardware Reduced flag is set (from the FADT), we must
Lv Zheng7cec7042013-06-08 00:59:18 +0000279 * use the extended sleep registers (FADT). Note: As per the ACPI
280 * specification, these extended registers are to be used for HW-reduced
281 * platforms only. They are not general-purpose replacements for the
282 * legacy PM register sleep support.
Bob Moore72a88872012-02-14 18:57:13 +0800283 */
Lv Zheng7cec7042013-06-08 00:59:18 +0000284 if (acpi_gbl_reduced_hardware) {
Len Brown3f6f49c2012-07-26 20:08:54 -0400285 status = sleep_functions->extended_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800286 } else {
287 /* Legacy sleep */
288
Len Brown3f6f49c2012-07-26 20:08:54 -0400289 status = sleep_functions->legacy_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800290 }
291
292 return (status);
293
294#else
295 /*
296 * For the case where reduced-hardware-only code is being generated,
297 * we know that only the extended sleep registers are available
298 */
Len Brown3f6f49c2012-07-26 20:08:54 -0400299 status = sleep_functions->extended_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800300 return (status);
301
302#endif /* !ACPI_REDUCED_HARDWARE */
303}
304
305/*******************************************************************************
306 *
Bob Moore2feec472012-02-14 15:00:53 +0800307 * FUNCTION: acpi_enter_sleep_state_prep
308 *
309 * PARAMETERS: sleep_state - Which sleep state to enter
310 *
311 * RETURN: Status
312 *
Bob Moore72a88872012-02-14 18:57:13 +0800313 * DESCRIPTION: Prepare to enter a system sleep state.
Bob Moore2feec472012-02-14 15:00:53 +0800314 * This function must execute with interrupts enabled.
315 * We break sleeping into 2 stages so that OSPM can handle
316 * various OS-specific tasks between the two steps.
317 *
318 ******************************************************************************/
Bob Moore72a88872012-02-14 18:57:13 +0800319
Bob Moore2feec472012-02-14 15:00:53 +0800320acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
321{
322 acpi_status status;
323 struct acpi_object_list arg_list;
324 union acpi_object arg;
325 u32 sst_value;
326
327 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
328
Bob Moore2feec472012-02-14 15:00:53 +0800329 status = acpi_get_sleep_type_data(sleep_state,
330 &acpi_gbl_sleep_type_a,
331 &acpi_gbl_sleep_type_b);
332 if (ACPI_FAILURE(status)) {
333 return_ACPI_STATUS(status);
334 }
335
336 /* Execute the _PTS method (Prepare To Sleep) */
337
338 arg_list.count = 1;
339 arg_list.pointer = &arg;
340 arg.type = ACPI_TYPE_INTEGER;
341 arg.integer.value = sleep_state;
342
Bob Moore4efeeec2012-03-21 09:42:45 +0800343 status =
344 acpi_evaluate_object(NULL, METHOD_PATHNAME__PTS, &arg_list, NULL);
Bob Moore2feec472012-02-14 15:00:53 +0800345 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
346 return_ACPI_STATUS(status);
347 }
348
349 /* Setup the argument to the _SST method (System STatus) */
350
351 switch (sleep_state) {
352 case ACPI_STATE_S0:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000353
Bob Moore2feec472012-02-14 15:00:53 +0800354 sst_value = ACPI_SST_WORKING;
355 break;
356
357 case ACPI_STATE_S1:
358 case ACPI_STATE_S2:
359 case ACPI_STATE_S3:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000360
Bob Moore2feec472012-02-14 15:00:53 +0800361 sst_value = ACPI_SST_SLEEPING;
362 break;
363
364 case ACPI_STATE_S4:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000365
Bob Moore2feec472012-02-14 15:00:53 +0800366 sst_value = ACPI_SST_SLEEP_CONTEXT;
367 break;
368
369 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000370
Bob Moore2feec472012-02-14 15:00:53 +0800371 sst_value = ACPI_SST_INDICATOR_OFF; /* Default is off */
372 break;
373 }
374
375 /*
376 * Set the system indicators to show the desired sleep state.
377 * _SST is an optional method (return no error if not found)
378 */
Bob Moore4efeeec2012-03-21 09:42:45 +0800379 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, sst_value);
Bob Moore2feec472012-02-14 15:00:53 +0800380 return_ACPI_STATUS(AE_OK);
381}
382
383ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
384
385/*******************************************************************************
386 *
387 * FUNCTION: acpi_enter_sleep_state
388 *
389 * PARAMETERS: sleep_state - Which sleep state to enter
390 *
391 * RETURN: Status
392 *
Lv Zheng75c80442012-12-19 05:36:49 +0000393 * DESCRIPTION: Enter a system sleep state
Bob Moore2feec472012-02-14 15:00:53 +0800394 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
395 *
396 ******************************************************************************/
Lv Zheng40bce102013-10-31 09:31:18 +0800397acpi_status acpi_enter_sleep_state(u8 sleep_state)
Bob Moore2feec472012-02-14 15:00:53 +0800398{
399 acpi_status status;
400
401 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
402
403 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
404 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
405 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
406 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
407 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
408 }
409
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000410 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_SLEEP_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800411 return_ACPI_STATUS(status);
412}
413
414ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
415
416/*******************************************************************************
417 *
418 * FUNCTION: acpi_leave_sleep_state_prep
419 *
420 * PARAMETERS: sleep_state - Which sleep state we are exiting
421 *
422 * RETURN: Status
423 *
424 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
Lv Zheng75c80442012-12-19 05:36:49 +0000425 * sleep. Called with interrupts DISABLED.
426 * We break wake/resume into 2 stages so that OSPM can handle
427 * various OS-specific tasks between the two steps.
Bob Moore2feec472012-02-14 15:00:53 +0800428 *
429 ******************************************************************************/
Len Brown3f6f49c2012-07-26 20:08:54 -0400430acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Bob Moore2feec472012-02-14 15:00:53 +0800431{
432 acpi_status status;
433
Bob Moore72a88872012-02-14 18:57:13 +0800434 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Bob Moore2feec472012-02-14 15:00:53 +0800435
Lin Ming8a73b172012-03-21 10:01:49 +0800436 status =
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000437 acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_PREP_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800438 return_ACPI_STATUS(status);
439}
440
441ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep)
442
443/*******************************************************************************
444 *
445 * FUNCTION: acpi_leave_sleep_state
446 *
Bob Moore72a88872012-02-14 18:57:13 +0800447 * PARAMETERS: sleep_state - Which sleep state we are exiting
Bob Moore2feec472012-02-14 15:00:53 +0800448 *
449 * RETURN: Status
450 *
451 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
452 * Called with interrupts ENABLED.
453 *
454 ******************************************************************************/
455acpi_status acpi_leave_sleep_state(u8 sleep_state)
456{
457 acpi_status status;
458
459 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
460
Len Brown3f6f49c2012-07-26 20:08:54 -0400461 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800462 return_ACPI_STATUS(status);
463}
464
465ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)