blob: e5c095ca6083a9a91169b65e986f73a3efc7d8fa [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)),
Kees Cook99e597a2017-07-10 15:23:08 +080075 ACPI_STRUCT_INIT(extended_function,
76 acpi_hw_extended_sleep)},
Kees Cook3d867f62017-06-27 00:53:07 +020077 {ACPI_STRUCT_INIT(legacy_function,
78 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake_prep)),
Kees Cook99e597a2017-07-10 15:23:08 +080079 ACPI_STRUCT_INIT(extended_function,
80 acpi_hw_extended_wake_prep)},
Kees Cook3d867f62017-06-27 00:53:07 +020081 {ACPI_STRUCT_INIT(legacy_function,
82 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake)),
Kees Cook99e597a2017-07-10 15:23:08 +080083 ACPI_STRUCT_INIT(extended_function,
84 acpi_hw_extended_wake)}
Bob Moore72a88872012-02-14 18:57:13 +080085};
86
87/*
88 * These functions are removed for the ACPI_REDUCED_HARDWARE case:
89 * acpi_set_firmware_waking_vector
Bob Moore72a88872012-02-14 18:57:13 +080090 * acpi_enter_sleep_state_s4bios
91 */
92
Bob Moore33620c52012-02-14 18:14:27 +080093#if (!ACPI_REDUCED_HARDWARE)
Bob Moore2feec472012-02-14 15:00:53 +080094/*******************************************************************************
95 *
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +010096 * FUNCTION: acpi_hw_set_firmware_waking_vector
Lv Zhengf06147f2015-07-01 14:43:18 +080097 *
98 * PARAMETERS: facs - Pointer to FACS table
99 * physical_address - 32-bit physical address of ACPI real mode
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100100 * entry point
Lv Zhengf06147f2015-07-01 14:43:18 +0800101 * physical_address64 - 64-bit physical address of ACPI protected
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100102 * mode entry point
Lv Zhengf06147f2015-07-01 14:43:18 +0800103 *
104 * RETURN: Status
105 *
106 * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
107 *
108 ******************************************************************************/
109
110static acpi_status
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100111acpi_hw_set_firmware_waking_vector(struct acpi_table_facs *facs,
112 acpi_physical_address physical_address,
113 acpi_physical_address physical_address64)
Lv Zhengf06147f2015-07-01 14:43:18 +0800114{
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100115 ACPI_FUNCTION_TRACE(acpi_hw_set_firmware_waking_vector);
Lv Zhengf06147f2015-07-01 14:43:18 +0800116
117
118 /*
119 * According to the ACPI specification 2.0c and later, the 64-bit
120 * waking vector should be cleared and the 32-bit waking vector should
121 * be used, unless we want the wake-up code to be called by the BIOS in
122 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
123 * to fail to resume if the 64-bit vector is used.
124 */
125
126 /* Set the 32-bit vector */
127
128 facs->firmware_waking_vector = (u32)physical_address;
129
130 if (facs->length > 32) {
131 if (facs->version >= 1) {
132
133 /* Set the 64-bit vector */
134
135 facs->xfirmware_waking_vector = physical_address64;
136 } else {
137 /* Clear the 64-bit vector if it exists */
138
139 facs->xfirmware_waking_vector = 0;
140 }
141 }
142
143 return_ACPI_STATUS(AE_OK);
144}
145
146/*******************************************************************************
147 *
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100148 * FUNCTION: acpi_set_firmware_waking_vector
Bob Moore2feec472012-02-14 15:00:53 +0800149 *
150 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100151 * entry point
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800152 * physical_address64 - 64-bit physical address of ACPI protected
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100153 * mode entry point
Bob Moore2feec472012-02-14 15:00:53 +0800154 *
155 * RETURN: Status
156 *
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800157 * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
Bob Moore2feec472012-02-14 15:00:53 +0800158 *
159 ******************************************************************************/
Bob Moore72a88872012-02-14 18:57:13 +0800160
Lv Zhengaca2a5d2015-07-01 14:43:04 +0800161acpi_status
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100162acpi_set_firmware_waking_vector(acpi_physical_address physical_address,
163 acpi_physical_address physical_address64)
Bob Moore2feec472012-02-14 15:00:53 +0800164{
Lv Zhengf06147f2015-07-01 14:43:18 +0800165
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100166 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
Bob Moore2feec472012-02-14 15:00:53 +0800167
Lv Zheng74846192015-08-25 10:29:08 +0800168 if (acpi_gbl_FACS) {
Rafael J. Wysockie3e9b572016-01-04 22:05:20 +0100169 (void)acpi_hw_set_firmware_waking_vector(acpi_gbl_FACS,
170 physical_address,
171 physical_address64);
Bob Moore2feec472012-02-14 15:00:53 +0800172 }
173
174 return_ACPI_STATUS(AE_OK);
175}
176
177ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
178
Bob Moore2feec472012-02-14 15:00:53 +0800179/*******************************************************************************
180 *
181 * FUNCTION: acpi_enter_sleep_state_s4bios
182 *
183 * PARAMETERS: None
184 *
185 * RETURN: Status
186 *
187 * DESCRIPTION: Perform a S4 bios request.
188 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
189 *
190 ******************************************************************************/
Lv Zheng40bce102013-10-31 09:31:18 +0800191acpi_status acpi_enter_sleep_state_s4bios(void)
Bob Moore2feec472012-02-14 15:00:53 +0800192{
193 u32 in_value;
194 acpi_status status;
195
196 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
197
198 /* Clear the wake status bit (PM1) */
199
200 status =
201 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
202 if (ACPI_FAILURE(status)) {
203 return_ACPI_STATUS(status);
204 }
205
206 status = acpi_hw_clear_acpi_status();
207 if (ACPI_FAILURE(status)) {
208 return_ACPI_STATUS(status);
209 }
210
211 /*
212 * 1) Disable/Clear all GPEs
213 * 2) Enable all wakeup GPEs
214 */
215 status = acpi_hw_disable_all_gpes();
216 if (ACPI_FAILURE(status)) {
217 return_ACPI_STATUS(status);
218 }
219 acpi_gbl_system_awake_and_running = FALSE;
220
221 status = acpi_hw_enable_all_wakeup_gpes();
222 if (ACPI_FAILURE(status)) {
223 return_ACPI_STATUS(status);
224 }
225
226 ACPI_FLUSH_CPU_CACHE();
227
228 status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
Bob Mooreba494be2012-07-12 09:40:10 +0800229 (u32)acpi_gbl_FADT.s4_bios_request, 8);
Bob Moore2feec472012-02-14 15:00:53 +0800230
231 do {
Bob Moorec41679a2012-12-31 00:05:46 +0000232 acpi_os_stall(ACPI_USEC_PER_MSEC);
Bob Moore2feec472012-02-14 15:00:53 +0800233 status =
234 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
235 if (ACPI_FAILURE(status)) {
236 return_ACPI_STATUS(status);
237 }
Bob Moore1fad8732015-12-29 13:54:36 +0800238
Bob Moore2feec472012-02-14 15:00:53 +0800239 } while (!in_value);
240
241 return_ACPI_STATUS(AE_OK);
242}
243
244ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Bob Moore33620c52012-02-14 18:14:27 +0800245#endif /* !ACPI_REDUCED_HARDWARE */
Bob Moore2feec472012-02-14 15:00:53 +0800246/*******************************************************************************
247 *
Bob Moore72a88872012-02-14 18:57:13 +0800248 * FUNCTION: acpi_hw_sleep_dispatch
249 *
250 * PARAMETERS: sleep_state - Which sleep state to enter/exit
251 * function_id - Sleep, wake_prep, or Wake
252 *
253 * RETURN: Status from the invoked sleep handling function.
254 *
255 * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
256 * function.
257 *
258 ******************************************************************************/
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000259static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id)
Bob Moore72a88872012-02-14 18:57:13 +0800260{
261 acpi_status status;
262 struct acpi_sleep_functions *sleep_functions =
263 &acpi_sleep_dispatch[function_id];
264
265#if (!ACPI_REDUCED_HARDWARE)
Bob Moore72a88872012-02-14 18:57:13 +0800266 /*
267 * If the Hardware Reduced flag is set (from the FADT), we must
Lv Zheng7cec7042013-06-08 00:59:18 +0000268 * use the extended sleep registers (FADT). Note: As per the ACPI
269 * specification, these extended registers are to be used for HW-reduced
270 * platforms only. They are not general-purpose replacements for the
271 * legacy PM register sleep support.
Bob Moore72a88872012-02-14 18:57:13 +0800272 */
Lv Zheng7cec7042013-06-08 00:59:18 +0000273 if (acpi_gbl_reduced_hardware) {
Len Brown3f6f49c2012-07-26 20:08:54 -0400274 status = sleep_functions->extended_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800275 } else {
276 /* Legacy sleep */
277
Len Brown3f6f49c2012-07-26 20:08:54 -0400278 status = sleep_functions->legacy_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800279 }
280
281 return (status);
282
283#else
284 /*
285 * For the case where reduced-hardware-only code is being generated,
286 * we know that only the extended sleep registers are available
287 */
Len Brown3f6f49c2012-07-26 20:08:54 -0400288 status = sleep_functions->extended_function(sleep_state);
Bob Moore72a88872012-02-14 18:57:13 +0800289 return (status);
290
291#endif /* !ACPI_REDUCED_HARDWARE */
292}
293
294/*******************************************************************************
295 *
Bob Moore2feec472012-02-14 15:00:53 +0800296 * FUNCTION: acpi_enter_sleep_state_prep
297 *
298 * PARAMETERS: sleep_state - Which sleep state to enter
299 *
300 * RETURN: Status
301 *
Bob Moore72a88872012-02-14 18:57:13 +0800302 * DESCRIPTION: Prepare to enter a system sleep state.
Bob Moore2feec472012-02-14 15:00:53 +0800303 * This function must execute with interrupts enabled.
304 * We break sleeping into 2 stages so that OSPM can handle
305 * various OS-specific tasks between the two steps.
306 *
307 ******************************************************************************/
Bob Moore72a88872012-02-14 18:57:13 +0800308
Bob Moore2feec472012-02-14 15:00:53 +0800309acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
310{
311 acpi_status status;
312 struct acpi_object_list arg_list;
313 union acpi_object arg;
314 u32 sst_value;
315
316 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
317
Bob Moore2feec472012-02-14 15:00:53 +0800318 status = acpi_get_sleep_type_data(sleep_state,
319 &acpi_gbl_sleep_type_a,
320 &acpi_gbl_sleep_type_b);
321 if (ACPI_FAILURE(status)) {
322 return_ACPI_STATUS(status);
323 }
324
325 /* Execute the _PTS method (Prepare To Sleep) */
326
327 arg_list.count = 1;
328 arg_list.pointer = &arg;
329 arg.type = ACPI_TYPE_INTEGER;
330 arg.integer.value = sleep_state;
331
Bob Moore4efeeec2012-03-21 09:42:45 +0800332 status =
333 acpi_evaluate_object(NULL, METHOD_PATHNAME__PTS, &arg_list, NULL);
Bob Moore2feec472012-02-14 15:00:53 +0800334 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
335 return_ACPI_STATUS(status);
336 }
337
338 /* Setup the argument to the _SST method (System STatus) */
339
340 switch (sleep_state) {
341 case ACPI_STATE_S0:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000342
Bob Moore2feec472012-02-14 15:00:53 +0800343 sst_value = ACPI_SST_WORKING;
344 break;
345
346 case ACPI_STATE_S1:
347 case ACPI_STATE_S2:
348 case ACPI_STATE_S3:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000349
Bob Moore2feec472012-02-14 15:00:53 +0800350 sst_value = ACPI_SST_SLEEPING;
351 break;
352
353 case ACPI_STATE_S4:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000354
Bob Moore2feec472012-02-14 15:00:53 +0800355 sst_value = ACPI_SST_SLEEP_CONTEXT;
356 break;
357
358 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000359
Bob Moore2feec472012-02-14 15:00:53 +0800360 sst_value = ACPI_SST_INDICATOR_OFF; /* Default is off */
361 break;
362 }
363
364 /*
365 * Set the system indicators to show the desired sleep state.
366 * _SST is an optional method (return no error if not found)
367 */
Bob Moore4efeeec2012-03-21 09:42:45 +0800368 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, sst_value);
Bob Moore2feec472012-02-14 15:00:53 +0800369 return_ACPI_STATUS(AE_OK);
370}
371
372ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
373
374/*******************************************************************************
375 *
376 * FUNCTION: acpi_enter_sleep_state
377 *
378 * PARAMETERS: sleep_state - Which sleep state to enter
379 *
380 * RETURN: Status
381 *
Lv Zheng75c80442012-12-19 05:36:49 +0000382 * DESCRIPTION: Enter a system sleep state
Bob Moore2feec472012-02-14 15:00:53 +0800383 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
384 *
385 ******************************************************************************/
Lv Zheng40bce102013-10-31 09:31:18 +0800386acpi_status acpi_enter_sleep_state(u8 sleep_state)
Bob Moore2feec472012-02-14 15:00:53 +0800387{
388 acpi_status status;
389
390 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
391
392 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
393 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
394 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
395 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
396 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
397 }
398
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000399 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_SLEEP_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800400 return_ACPI_STATUS(status);
401}
402
403ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
404
405/*******************************************************************************
406 *
407 * FUNCTION: acpi_leave_sleep_state_prep
408 *
409 * PARAMETERS: sleep_state - Which sleep state we are exiting
410 *
411 * RETURN: Status
412 *
413 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
Lv Zheng75c80442012-12-19 05:36:49 +0000414 * sleep. Called with interrupts DISABLED.
415 * We break wake/resume into 2 stages so that OSPM can handle
416 * various OS-specific tasks between the two steps.
Bob Moore2feec472012-02-14 15:00:53 +0800417 *
418 ******************************************************************************/
Len Brown3f6f49c2012-07-26 20:08:54 -0400419acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Bob Moore2feec472012-02-14 15:00:53 +0800420{
421 acpi_status status;
422
Bob Moore72a88872012-02-14 18:57:13 +0800423 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Bob Moore2feec472012-02-14 15:00:53 +0800424
Lin Ming8a73b172012-03-21 10:01:49 +0800425 status =
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000426 acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_PREP_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800427 return_ACPI_STATUS(status);
428}
429
430ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep)
431
432/*******************************************************************************
433 *
434 * FUNCTION: acpi_leave_sleep_state
435 *
Bob Moore72a88872012-02-14 18:57:13 +0800436 * PARAMETERS: sleep_state - Which sleep state we are exiting
Bob Moore2feec472012-02-14 15:00:53 +0800437 *
438 * RETURN: Status
439 *
440 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
441 * Called with interrupts ENABLED.
442 *
443 ******************************************************************************/
444acpi_status acpi_leave_sleep_state(u8 sleep_state)
445{
446 acpi_status status;
447
448 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
449
Len Brown3f6f49c2012-07-26 20:08:54 -0400450 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_FUNCTION_ID);
Bob Moore2feec472012-02-14 15:00:53 +0800451 return_ACPI_STATUS(status);
452}
453
454ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)