GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / acpi / acpica / hwxfsleep.c
1 /******************************************************************************
2  *
3  * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2017, 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 #define EXPORT_ACPI_INTERFACES
45
46 #include <acpi/acpi.h>
47 #include "accommon.h"
48
49 #define _COMPONENT          ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwxfsleep")
51
52 /* Local prototypes */
53 #if (!ACPI_REDUCED_HARDWARE)
54 static acpi_status
55 acpi_hw_set_firmware_waking_vector(struct acpi_table_facs *facs,
56                                    acpi_physical_address physical_address,
57                                    acpi_physical_address physical_address64);
58 #endif
59
60 static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id);
61
62 /*
63  * Dispatch table used to efficiently branch to the various sleep
64  * functions.
65  */
66 #define ACPI_SLEEP_FUNCTION_ID         0
67 #define ACPI_WAKE_PREP_FUNCTION_ID     1
68 #define ACPI_WAKE_FUNCTION_ID          2
69
70 /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
71
72 static struct acpi_sleep_functions acpi_sleep_dispatch[] = {
73         {ACPI_STRUCT_INIT(legacy_function,
74                           ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_sleep)),
75          ACPI_STRUCT_INIT(extended_function,
76                           acpi_hw_extended_sleep)},
77         {ACPI_STRUCT_INIT(legacy_function,
78                           ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake_prep)),
79          ACPI_STRUCT_INIT(extended_function,
80                           acpi_hw_extended_wake_prep)},
81         {ACPI_STRUCT_INIT(legacy_function,
82                           ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake)),
83          ACPI_STRUCT_INIT(extended_function,
84                           acpi_hw_extended_wake)}
85 };
86
87 /*
88  * These functions are removed for the ACPI_REDUCED_HARDWARE case:
89  *      acpi_set_firmware_waking_vector
90  *      acpi_enter_sleep_state_s4bios
91  */
92
93 #if (!ACPI_REDUCED_HARDWARE)
94 /*******************************************************************************
95  *
96  * FUNCTION:    acpi_hw_set_firmware_waking_vector
97  *
98  * PARAMETERS:  facs                - Pointer to FACS table
99  *              physical_address    - 32-bit physical address of ACPI real mode
100  *                                    entry point
101  *              physical_address64  - 64-bit physical address of ACPI protected
102  *                                    mode entry point
103  *
104  * RETURN:      Status
105  *
106  * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
107  *
108  ******************************************************************************/
109
110 static acpi_status
111 acpi_hw_set_firmware_waking_vector(struct acpi_table_facs *facs,
112                                    acpi_physical_address physical_address,
113                                    acpi_physical_address physical_address64)
114 {
115         ACPI_FUNCTION_TRACE(acpi_hw_set_firmware_waking_vector);
116
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  *
148  * FUNCTION:    acpi_set_firmware_waking_vector
149  *
150  * PARAMETERS:  physical_address    - 32-bit physical address of ACPI real mode
151  *                                    entry point
152  *              physical_address64  - 64-bit physical address of ACPI protected
153  *                                    mode entry point
154  *
155  * RETURN:      Status
156  *
157  * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
158  *
159  ******************************************************************************/
160
161 acpi_status
162 acpi_set_firmware_waking_vector(acpi_physical_address physical_address,
163                                 acpi_physical_address physical_address64)
164 {
165
166         ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
167
168         if (acpi_gbl_FACS) {
169                 (void)acpi_hw_set_firmware_waking_vector(acpi_gbl_FACS,
170                                                          physical_address,
171                                                          physical_address64);
172         }
173
174         return_ACPI_STATUS(AE_OK);
175 }
176
177 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
178
179 /*******************************************************************************
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  ******************************************************************************/
191 acpi_status acpi_enter_sleep_state_s4bios(void)
192 {
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         status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
227                                     (u32)acpi_gbl_FADT.s4_bios_request, 8);
228
229         do {
230                 acpi_os_stall(ACPI_USEC_PER_MSEC);
231                 status =
232                     acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
233                 if (ACPI_FAILURE(status)) {
234                         return_ACPI_STATUS(status);
235                 }
236
237         } while (!in_value);
238
239         return_ACPI_STATUS(AE_OK);
240 }
241
242 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
243 #endif                          /* !ACPI_REDUCED_HARDWARE */
244 /*******************************************************************************
245  *
246  * FUNCTION:    acpi_hw_sleep_dispatch
247  *
248  * PARAMETERS:  sleep_state         - Which sleep state to enter/exit
249  *              function_id         - Sleep, wake_prep, or Wake
250  *
251  * RETURN:      Status from the invoked sleep handling function.
252  *
253  * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
254  *              function.
255  *
256  ******************************************************************************/
257 static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id)
258 {
259         acpi_status status;
260         struct acpi_sleep_functions *sleep_functions =
261             &acpi_sleep_dispatch[function_id];
262
263 #if (!ACPI_REDUCED_HARDWARE)
264         /*
265          * If the Hardware Reduced flag is set (from the FADT), we must
266          * use the extended sleep registers (FADT). Note: As per the ACPI
267          * specification, these extended registers are to be used for HW-reduced
268          * platforms only. They are not general-purpose replacements for the
269          * legacy PM register sleep support.
270          */
271         if (acpi_gbl_reduced_hardware) {
272                 status = sleep_functions->extended_function(sleep_state);
273         } else {
274                 /* Legacy sleep */
275
276                 status = sleep_functions->legacy_function(sleep_state);
277         }
278
279         return (status);
280
281 #else
282         /*
283          * For the case where reduced-hardware-only code is being generated,
284          * we know that only the extended sleep registers are available
285          */
286         status = sleep_functions->extended_function(sleep_state);
287         return (status);
288
289 #endif                          /* !ACPI_REDUCED_HARDWARE */
290 }
291
292 /*******************************************************************************
293  *
294  * FUNCTION:    acpi_enter_sleep_state_prep
295  *
296  * PARAMETERS:  sleep_state         - Which sleep state to enter
297  *
298  * RETURN:      Status
299  *
300  * DESCRIPTION: Prepare to enter a system sleep state.
301  *              This function must execute with interrupts enabled.
302  *              We break sleeping into 2 stages so that OSPM can handle
303  *              various OS-specific tasks between the two steps.
304  *
305  ******************************************************************************/
306
307 acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
308 {
309         acpi_status status;
310         struct acpi_object_list arg_list;
311         union acpi_object arg;
312         u32 sst_value;
313
314         ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
315
316         status = acpi_get_sleep_type_data(sleep_state,
317                                           &acpi_gbl_sleep_type_a,
318                                           &acpi_gbl_sleep_type_b);
319         if (ACPI_FAILURE(status)) {
320                 return_ACPI_STATUS(status);
321         }
322
323         status = acpi_get_sleep_type_data(ACPI_STATE_S0,
324                                           &acpi_gbl_sleep_type_a_s0,
325                                           &acpi_gbl_sleep_type_b_s0);
326         if (ACPI_FAILURE(status)) {
327                 acpi_gbl_sleep_type_a_s0 = ACPI_SLEEP_TYPE_INVALID;
328         }
329
330         /* Execute the _PTS method (Prepare To Sleep) */
331
332         arg_list.count = 1;
333         arg_list.pointer = &arg;
334         arg.type = ACPI_TYPE_INTEGER;
335         arg.integer.value = sleep_state;
336
337         status =
338             acpi_evaluate_object(NULL, METHOD_PATHNAME__PTS, &arg_list, NULL);
339         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
340                 return_ACPI_STATUS(status);
341         }
342
343         /* Setup the argument to the _SST method (System STatus) */
344
345         switch (sleep_state) {
346         case ACPI_STATE_S0:
347
348                 sst_value = ACPI_SST_WORKING;
349                 break;
350
351         case ACPI_STATE_S1:
352         case ACPI_STATE_S2:
353         case ACPI_STATE_S3:
354
355                 sst_value = ACPI_SST_SLEEPING;
356                 break;
357
358         case ACPI_STATE_S4:
359
360                 sst_value = ACPI_SST_SLEEP_CONTEXT;
361                 break;
362
363         default:
364
365                 sst_value = ACPI_SST_INDICATOR_OFF;     /* Default is off */
366                 break;
367         }
368
369         /*
370          * Set the system indicators to show the desired sleep state.
371          * _SST is an optional method (return no error if not found)
372          */
373         acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, sst_value);
374         return_ACPI_STATUS(AE_OK);
375 }
376
377 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
378
379 /*******************************************************************************
380  *
381  * FUNCTION:    acpi_enter_sleep_state
382  *
383  * PARAMETERS:  sleep_state         - Which sleep state to enter
384  *
385  * RETURN:      Status
386  *
387  * DESCRIPTION: Enter a system sleep state
388  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
389  *
390  ******************************************************************************/
391 acpi_status acpi_enter_sleep_state(u8 sleep_state)
392 {
393         acpi_status status;
394
395         ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
396
397         if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
398             (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
399                 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
400                             acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
401                 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
402         }
403
404         status = acpi_hw_sleep_dispatch(sleep_state, ACPI_SLEEP_FUNCTION_ID);
405         return_ACPI_STATUS(status);
406 }
407
408 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
409
410 /*******************************************************************************
411  *
412  * FUNCTION:    acpi_leave_sleep_state_prep
413  *
414  * PARAMETERS:  sleep_state         - Which sleep state we are exiting
415  *
416  * RETURN:      Status
417  *
418  * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
419  *              sleep. Called with interrupts DISABLED.
420  *              We break wake/resume into 2 stages so that OSPM can handle
421  *              various OS-specific tasks between the two steps.
422  *
423  ******************************************************************************/
424 acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
425 {
426         acpi_status status;
427
428         ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
429
430         status =
431             acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_PREP_FUNCTION_ID);
432         return_ACPI_STATUS(status);
433 }
434
435 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep)
436
437 /*******************************************************************************
438  *
439  * FUNCTION:    acpi_leave_sleep_state
440  *
441  * PARAMETERS:  sleep_state         - Which sleep state we are exiting
442  *
443  * RETURN:      Status
444  *
445  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
446  *              Called with interrupts ENABLED.
447  *
448  ******************************************************************************/
449 acpi_status acpi_leave_sleep_state(u8 sleep_state)
450 {
451         acpi_status status;
452
453         ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
454
455         status = acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_FUNCTION_ID);
456         return_ACPI_STATUS(status);
457 }
458
459 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)