blob: 7ef13934968f3d56d2e76cba94ec5424cfdaaa3c [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/*
Bob Moore7735ca02017-02-08 11:00:08 +08008 * Copyright (C) 2000 - 2017, 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 Zhengf06147f2015-07-01 14:43:18 +080053#if (!ACPI_REDUCED_HARDWARE)
54static acpi_status
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +010055acpi_hw_set_firmware_waking_vector(struct acpi_table_facs *facs,
56 acpi_physical_address physical_address,
57 acpi_physical_address physical_address64);
Lv Zhengf06147f2015-07-01 14:43:18 +080058#endif
59
Lv Zheng1f86e8c2012-10-31 02:25:45 +000060static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id);
Bob Moore72a88872012-02-14 18:57:13 +080061
62/*
63 * Dispatch table used to efficiently branch to the various sleep
64 * functions.
65 */
Lin Ming8a73b172012-03-21 10:01:49 +080066#define ACPI_SLEEP_FUNCTION_ID 0
67#define ACPI_WAKE_PREP_FUNCTION_ID 1
68#define ACPI_WAKE_FUNCTION_ID 2
Bob Moore72a88872012-02-14 18:57:13 +080069
70/* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
71
72static struct acpi_sleep_functions acpi_sleep_dispatch[] = {
Kees Cook3d867f62017-06-27 00:53:07 +020073 {ACPI_STRUCT_INIT(legacy_function,
74 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_sleep)),
75 ACPI_STRUCT_INIT(extended_function, acpi_hw_extended_sleep) },
76 {ACPI_STRUCT_INIT(legacy_function,
77 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake_prep)),
78 ACPI_STRUCT_INIT(extended_function, acpi_hw_extended_wake_prep) },
79 {ACPI_STRUCT_INIT(legacy_function,
80 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake)),
81 ACPI_STRUCT_INIT(extended_function, acpi_hw_extended_wake) }
Bob Moore72a88872012-02-14 18:57:13 +080082};
83
84/*
85 * These functions are removed for the ACPI_REDUCED_HARDWARE case:
86 * acpi_set_firmware_waking_vector
Bob Moore72a88872012-02-14 18:57:13 +080087 * acpi_enter_sleep_state_s4bios
88 */
89
Bob Moore33620c52012-02-14 18:14:27 +080090#if (!ACPI_REDUCED_HARDWARE)
Bob Moore2feec472012-02-14 15:00:53 +080091/*******************************************************************************
92 *
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +010093 * FUNCTION: acpi_hw_set_firmware_waking_vector
Lv Zhengf06147f2015-07-01 14:43:18 +080094 *
95 * PARAMETERS: facs - Pointer to FACS table
96 * physical_address - 32-bit physical address of ACPI real mode
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +010097 * entry point
Lv Zhengf06147f2015-07-01 14:43:18 +080098 * physical_address64 - 64-bit physical address of ACPI protected
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +010099 * mode entry point
Lv Zhengf06147f2015-07-01 14:43:18 +0800100 *
101 * RETURN: Status
102 *
103 * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
104 *
105 ******************************************************************************/
106
107static acpi_status
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100108acpi_hw_set_firmware_waking_vector(struct acpi_table_facs *facs,
109 acpi_physical_address physical_address,
110 acpi_physical_address physical_address64)
Lv Zhengf06147f2015-07-01 14:43:18 +0800111{
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100112 ACPI_FUNCTION_TRACE(acpi_hw_set_firmware_waking_vector);
Lv Zhengf06147f2015-07-01 14:43:18 +0800113
114
115 /*
116 * According to the ACPI specification 2.0c and later, the 64-bit
117 * waking vector should be cleared and the 32-bit waking vector should
118 * be used, unless we want the wake-up code to be called by the BIOS in
119 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
120 * to fail to resume if the 64-bit vector is used.
121 */
122
123 /* Set the 32-bit vector */
124
125 facs->firmware_waking_vector = (u32)physical_address;
126
127 if (facs->length > 32) {
128 if (facs->version >= 1) {
129
130 /* Set the 64-bit vector */
131
132 facs->xfirmware_waking_vector = physical_address64;
133 } else {
134 /* Clear the 64-bit vector if it exists */
135
136 facs->xfirmware_waking_vector = 0;
137 }
138 }
139
140 return_ACPI_STATUS(AE_OK);
141}
142
143/*******************************************************************************
144 *
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100145 * FUNCTION: acpi_set_firmware_waking_vector
Bob Moore2feec472012-02-14 15:00:53 +0800146 *
147 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100148 * entry point
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800149 * physical_address64 - 64-bit physical address of ACPI protected
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100150 * mode entry point
Bob Moore2feec472012-02-14 15:00:53 +0800151 *
152 * RETURN: Status
153 *
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800154 * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
Bob Moore2feec472012-02-14 15:00:53 +0800155 *
156 ******************************************************************************/
Bob Moore72a88872012-02-14 18:57:13 +0800157
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800158acpi_status
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100159acpi_set_firmware_waking_vector(acpi_physical_address physical_address,
160 acpi_physical_address physical_address64)
Bob Moore2feec472012-02-14 15:00:53 +0800161{
Lv Zhengf06147f2015-07-01 14:43:18 +0800162
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100163 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
Bob Moore2feec472012-02-14 15:00:53 +0800164
Lv Zheng74846192015-08-25 10:29:08 +0800165 if (acpi_gbl_FACS) {
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100166 (void)acpi_hw_set_firmware_waking_vector(acpi_gbl_FACS,
167 physical_address,
168 physical_address64);
Bob Moore2feec472012-02-14 15:00:53 +0800169 }
170
171 return_ACPI_STATUS(AE_OK);
172}
173
174ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
175
Bob Moore2feec472012-02-14 15:00:53 +0800176/*******************************************************************************
177 *
178 * FUNCTION: acpi_enter_sleep_state_s4bios
179 *
180 * PARAMETERS: None
181 *
182 * RETURN: Status
183 *
184 * DESCRIPTION: Perform a S4 bios request.
185 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
186 *
187 ******************************************************************************/
Lv Zheng40bce102013-10-31 09:31:18 +0800188acpi_status acpi_enter_sleep_state_s4bios(void)
Bob Moore2feec472012-02-14 15:00:53 +0800189{
190 u32 in_value;
191 acpi_status status;
192
193 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
194
195 /* Clear the wake status bit (PM1) */
196
197 status =
198 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
199 if (ACPI_FAILURE(status)) {
200 return_ACPI_STATUS(status);
201 }
202
203 status = acpi_hw_clear_acpi_status();
204 if (ACPI_FAILURE(status)) {
205 return_ACPI_STATUS(status);
206 }
207
208 /*
209 * 1) Disable/Clear all GPEs
210 * 2) Enable all wakeup GPEs
211 */
212 status = acpi_hw_disable_all_gpes();
213 if (ACPI_FAILURE(status)) {
214 return_ACPI_STATUS(status);
215 }
216 acpi_gbl_system_awake_and_running = FALSE;
217
218 status = acpi_hw_enable_all_wakeup_gpes();
219 if (ACPI_FAILURE(status)) {
220 return_ACPI_STATUS(status);
221 }
222
223 ACPI_FLUSH_CPU_CACHE();
224
225 status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
Bob Mooreba494be2012-07-12 09:40:10 +0800226 (u32)acpi_gbl_FADT.s4_bios_request, 8);
Bob Moore2feec472012-02-14 15:00:53 +0800227
228 do {
Bob Moorec41679a2012-12-31 00:05:46 +0000229 acpi_os_stall(ACPI_USEC_PER_MSEC);
Bob Moore2feec472012-02-14 15:00:53 +0800230 status =
231 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
232 if (ACPI_FAILURE(status)) {
233 return_ACPI_STATUS(status);
234 }
Bob Moore1fad8732015-12-29 13:54:36 +0800235
Bob Moore2feec472012-02-14 15:00:53 +0800236 } while (!in_value);
237
238 return_ACPI_STATUS(AE_OK);
239}
240
241ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Bob Moore33620c52012-02-14 18:14:27 +0800242#endif /* !ACPI_REDUCED_HARDWARE */
Bob Moore2feec472012-02-14 15:00:53 +0800243/*******************************************************************************
244 *
Bob Moore72a88872012-02-14 18:57:13 +0800245 * FUNCTION: acpi_hw_sleep_dispatch
246 *
247 * PARAMETERS: sleep_state - Which sleep state to enter/exit
248 * function_id - Sleep, wake_prep, or Wake
249 *
250 * RETURN: Status from the invoked sleep handling function.
251 *
252 * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
253 * function.
254 *
255 ******************************************************************************/
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000256static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id)
Bob Moore72a88872012-02-14 18:57:13 +0800257{
258 acpi_status status;
259 struct acpi_sleep_functions *sleep_functions =
260 &acpi_sleep_dispatch[function_id];
261
262#if (!ACPI_REDUCED_HARDWARE)
Bob Moore72a88872012-02-14 18:57:13 +0800263 /*
264 * If the Hardware Reduced flag is set (from the FADT), we must
Lv Zheng7cec7042013-06-08 00:59:18 +0000265 * use the extended sleep registers (FADT). Note: As per the ACPI
266 * specification, these extended registers are to be used for HW-reduced
267 * platforms only. They are not general-purpose replacements for the
268 * legacy PM register sleep support.
Bob Moore72a88872012-02-14 18:57:13 +0800269 */
Lv Zheng7cec7042013-06-08 00:59:18 +0000270 if (acpi_gbl_reduced_hardware) {
Len Brown3f6f49c2012-07-26 20:08:54 -0400271 status = sleep_functions->extended_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800272 } else {
273 /* Legacy sleep */
274
Len Brown3f6f49c2012-07-26 20:08:54 -0400275 status = sleep_functions->legacy_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800276 }
277
278 return (status);
279
280#else
281 /*
282 * For the case where reduced-hardware-only code is being generated,
283 * we know that only the extended sleep registers are available
284 */
Len Brown3f6f49c2012-07-26 20:08:54 -0400285 status = sleep_functions->extended_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800286 return (status);
287
288#endif /* !ACPI_REDUCED_HARDWARE */
289}
290
291/*******************************************************************************
292 *
Bob Moore2feec472012-02-14 15:00:53 +0800293 * FUNCTION: acpi_enter_sleep_state_prep
294 *
295 * PARAMETERS: sleep_state - Which sleep state to enter
296 *
297 * RETURN: Status
298 *
Bob Moore72a88872012-02-14 18:57:13 +0800299 * DESCRIPTION: Prepare to enter a system sleep state.
Bob Moore2feec472012-02-14 15:00:53 +0800300 * This function must execute with interrupts enabled.
301 * We break sleeping into 2 stages so that OSPM can handle
302 * various OS-specific tasks between the two steps.
303 *
304 ******************************************************************************/
Bob Moore72a88872012-02-14 18:57:13 +0800305
Bob Moore2feec472012-02-14 15:00:53 +0800306acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
307{
308 acpi_status status;
309 struct acpi_object_list arg_list;
310 union acpi_object arg;
311 u32 sst_value;
312
313 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
314
Bob Moore2feec472012-02-14 15:00:53 +0800315 status = acpi_get_sleep_type_data(sleep_state,
316 &acpi_gbl_sleep_type_a,
317 &acpi_gbl_sleep_type_b);
318 if (ACPI_FAILURE(status)) {
319 return_ACPI_STATUS(status);
320 }
321
322 /* Execute the _PTS method (Prepare To Sleep) */
323
324 arg_list.count = 1;
325 arg_list.pointer = &arg;
326 arg.type = ACPI_TYPE_INTEGER;
327 arg.integer.value = sleep_state;
328
Bob Moore4efeeec2012-03-21 09:42:45 +0800329 status =
330 acpi_evaluate_object(NULL, METHOD_PATHNAME__PTS, &arg_list, NULL);
Bob Moore2feec472012-02-14 15:00:53 +0800331 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
332 return_ACPI_STATUS(status);
333 }
334
335 /* Setup the argument to the _SST method (System STatus) */
336
337 switch (sleep_state) {
338 case ACPI_STATE_S0:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000339
Bob Moore2feec472012-02-14 15:00:53 +0800340 sst_value = ACPI_SST_WORKING;
341 break;
342
343 case ACPI_STATE_S1:
344 case ACPI_STATE_S2:
345 case ACPI_STATE_S3:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000346
Bob Moore2feec472012-02-14 15:00:53 +0800347 sst_value = ACPI_SST_SLEEPING;
348 break;
349
350 case ACPI_STATE_S4:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000351
Bob Moore2feec472012-02-14 15:00:53 +0800352 sst_value = ACPI_SST_SLEEP_CONTEXT;
353 break;
354
355 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000356
Bob Moore2feec472012-02-14 15:00:53 +0800357 sst_value = ACPI_SST_INDICATOR_OFF; /* Default is off */
358 break;
359 }
360
361 /*
362 * Set the system indicators to show the desired sleep state.
363 * _SST is an optional method (return no error if not found)
364 */
Bob Moore4efeeec2012-03-21 09:42:45 +0800365 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, sst_value);
Bob Moore2feec472012-02-14 15:00:53 +0800366 return_ACPI_STATUS(AE_OK);
367}
368
369ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
370
371/*******************************************************************************
372 *
373 * FUNCTION: acpi_enter_sleep_state
374 *
375 * PARAMETERS: sleep_state - Which sleep state to enter
376 *
377 * RETURN: Status
378 *
Lv Zheng75c80442012-12-19 05:36:49 +0000379 * DESCRIPTION: Enter a system sleep state
Bob Moore2feec472012-02-14 15:00:53 +0800380 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
381 *
382 ******************************************************************************/
Lv Zheng40bce102013-10-31 09:31:18 +0800383acpi_status acpi_enter_sleep_state(u8 sleep_state)
Bob Moore2feec472012-02-14 15:00:53 +0800384{
385 acpi_status status;
386
387 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
388
389 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
390 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
391 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
392 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
393 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
394 }
395
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000396 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_SLEEP_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800397 return_ACPI_STATUS(status);
398}
399
400ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
401
402/*******************************************************************************
403 *
404 * FUNCTION: acpi_leave_sleep_state_prep
405 *
406 * PARAMETERS: sleep_state - Which sleep state we are exiting
407 *
408 * RETURN: Status
409 *
410 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
Lv Zheng75c80442012-12-19 05:36:49 +0000411 * sleep. Called with interrupts DISABLED.
412 * We break wake/resume into 2 stages so that OSPM can handle
413 * various OS-specific tasks between the two steps.
Bob Moore2feec472012-02-14 15:00:53 +0800414 *
415 ******************************************************************************/
Len Brown3f6f49c2012-07-26 20:08:54 -0400416acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Bob Moore2feec472012-02-14 15:00:53 +0800417{
418 acpi_status status;
419
Bob Moore72a88872012-02-14 18:57:13 +0800420 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Bob Moore2feec472012-02-14 15:00:53 +0800421
Lin Ming8a73b172012-03-21 10:01:49 +0800422 status =
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000423 acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_PREP_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800424 return_ACPI_STATUS(status);
425}
426
427ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep)
428
429/*******************************************************************************
430 *
431 * FUNCTION: acpi_leave_sleep_state
432 *
Bob Moore72a88872012-02-14 18:57:13 +0800433 * PARAMETERS: sleep_state - Which sleep state we are exiting
Bob Moore2feec472012-02-14 15:00:53 +0800434 *
435 * RETURN: Status
436 *
437 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
438 * Called with interrupts ENABLED.
439 *
440 ******************************************************************************/
441acpi_status acpi_leave_sleep_state(u8 sleep_state)
442{
443 acpi_status status;
444
445 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
446
Len Brown3f6f49c2012-07-26 20:08:54 -0400447 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800448 return_ACPI_STATUS(status);
449}
450
451ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)