ACPICA: Linuxize: Reduce divergences for 20150616 release
[firefly-linux-kernel-4.4.55.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 - 2015, 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 static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id);
54
55 /*
56  * Dispatch table used to efficiently branch to the various sleep
57  * functions.
58  */
59 #define ACPI_SLEEP_FUNCTION_ID         0
60 #define ACPI_WAKE_PREP_FUNCTION_ID     1
61 #define ACPI_WAKE_FUNCTION_ID          2
62
63 /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
64
65 static 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:
75  *      acpi_set_firmware_waking_vector
76  *      acpi_set_firmware_waking_vector64
77  *      acpi_enter_sleep_state_s4bios
78  */
79
80 #if (!ACPI_REDUCED_HARDWARE)
81 /*******************************************************************************
82  *
83  * FUNCTION:    acpi_set_firmware_waking_vector
84  *
85  * PARAMETERS:  physical_address    - 32-bit physical address of ACPI real mode
86  *                                    entry point.
87  *
88  * RETURN:      Status
89  *
90  * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
91  *
92  ******************************************************************************/
93
94 acpi_status acpi_set_firmware_waking_vector(u32 physical_address)
95 {
96         ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
97
98
99         /*
100          * According to the ACPI specification 2.0c and later, the 64-bit
101          * waking vector should be cleared and the 32-bit waking vector should
102          * be used, unless we want the wake-up code to be called by the BIOS in
103          * Protected Mode.  Some systems (for example HP dv5-1004nr) are known
104          * to fail to resume if the 64-bit vector is used.
105          */
106
107         /* Set the 32-bit vector */
108
109         acpi_gbl_FACS->firmware_waking_vector = physical_address;
110
111         /* Clear the 64-bit vector if it exists */
112
113         if ((acpi_gbl_FACS->length > 32) && (acpi_gbl_FACS->version >= 1)) {
114                 acpi_gbl_FACS->xfirmware_waking_vector = 0;
115         }
116
117         return_ACPI_STATUS(AE_OK);
118 }
119
120 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
121
122 #if ACPI_MACHINE_WIDTH == 64
123 /*******************************************************************************
124  *
125  * FUNCTION:    acpi_set_firmware_waking_vector64
126  *
127  * PARAMETERS:  physical_address    - 64-bit physical address of ACPI protected
128  *                                    mode entry point.
129  *
130  * RETURN:      Status
131  *
132  * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
133  *              it exists in the table. This function is intended for use with
134  *              64-bit host operating systems.
135  *
136  ******************************************************************************/
137 acpi_status acpi_set_firmware_waking_vector64(u64 physical_address)
138 {
139         ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64);
140
141         /* Determine if the 64-bit vector actually exists */
142
143         if ((acpi_gbl_FACS->length <= 32) || (acpi_gbl_FACS->version < 1)) {
144                 return_ACPI_STATUS(AE_NOT_EXIST);
145         }
146
147         /* Clear 32-bit vector, set the 64-bit X_ vector */
148
149         acpi_gbl_FACS->firmware_waking_vector = 0;
150         acpi_gbl_FACS->xfirmware_waking_vector = physical_address;
151         return_ACPI_STATUS(AE_OK);
152 }
153
154 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64)
155 #endif
156 /*******************************************************************************
157  *
158  * FUNCTION:    acpi_enter_sleep_state_s4bios
159  *
160  * PARAMETERS:  None
161  *
162  * RETURN:      Status
163  *
164  * DESCRIPTION: Perform a S4 bios request.
165  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
166  *
167  ******************************************************************************/
168 acpi_status acpi_enter_sleep_state_s4bios(void)
169 {
170         u32 in_value;
171         acpi_status status;
172
173         ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
174
175         /* Clear the wake status bit (PM1) */
176
177         status =
178             acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
179         if (ACPI_FAILURE(status)) {
180                 return_ACPI_STATUS(status);
181         }
182
183         status = acpi_hw_clear_acpi_status();
184         if (ACPI_FAILURE(status)) {
185                 return_ACPI_STATUS(status);
186         }
187
188         /*
189          * 1) Disable/Clear all GPEs
190          * 2) Enable all wakeup GPEs
191          */
192         status = acpi_hw_disable_all_gpes();
193         if (ACPI_FAILURE(status)) {
194                 return_ACPI_STATUS(status);
195         }
196         acpi_gbl_system_awake_and_running = FALSE;
197
198         status = acpi_hw_enable_all_wakeup_gpes();
199         if (ACPI_FAILURE(status)) {
200                 return_ACPI_STATUS(status);
201         }
202
203         ACPI_FLUSH_CPU_CACHE();
204
205         status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
206                                     (u32)acpi_gbl_FADT.s4_bios_request, 8);
207
208         do {
209                 acpi_os_stall(ACPI_USEC_PER_MSEC);
210                 status =
211                     acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
212                 if (ACPI_FAILURE(status)) {
213                         return_ACPI_STATUS(status);
214                 }
215         } while (!in_value);
216
217         return_ACPI_STATUS(AE_OK);
218 }
219
220 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
221 #endif                          /* !ACPI_REDUCED_HARDWARE */
222 /*******************************************************************************
223  *
224  * FUNCTION:    acpi_hw_sleep_dispatch
225  *
226  * PARAMETERS:  sleep_state         - Which sleep state to enter/exit
227  *              function_id         - Sleep, wake_prep, or Wake
228  *
229  * RETURN:      Status from the invoked sleep handling function.
230  *
231  * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
232  *              function.
233  *
234  ******************************************************************************/
235 static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id)
236 {
237         acpi_status status;
238         struct acpi_sleep_functions *sleep_functions =
239             &acpi_sleep_dispatch[function_id];
240
241 #if (!ACPI_REDUCED_HARDWARE)
242         /*
243          * If the Hardware Reduced flag is set (from the FADT), we must
244          * use the extended sleep registers (FADT). Note: As per the ACPI
245          * specification, these extended registers are to be used for HW-reduced
246          * platforms only. They are not general-purpose replacements for the
247          * legacy PM register sleep support.
248          */
249         if (acpi_gbl_reduced_hardware) {
250                 status = sleep_functions->extended_function(sleep_state);
251         } else {
252                 /* Legacy sleep */
253
254                 status = sleep_functions->legacy_function(sleep_state);
255         }
256
257         return (status);
258
259 #else
260         /*
261          * For the case where reduced-hardware-only code is being generated,
262          * we know that only the extended sleep registers are available
263          */
264         status = sleep_functions->extended_function(sleep_state);
265         return (status);
266
267 #endif                          /* !ACPI_REDUCED_HARDWARE */
268 }
269
270 /*******************************************************************************
271  *
272  * FUNCTION:    acpi_enter_sleep_state_prep
273  *
274  * PARAMETERS:  sleep_state         - Which sleep state to enter
275  *
276  * RETURN:      Status
277  *
278  * DESCRIPTION: Prepare to enter a system sleep state.
279  *              This function must execute with interrupts enabled.
280  *              We break sleeping into 2 stages so that OSPM can handle
281  *              various OS-specific tasks between the two steps.
282  *
283  ******************************************************************************/
284
285 acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
286 {
287         acpi_status status;
288         struct acpi_object_list arg_list;
289         union acpi_object arg;
290         u32 sst_value;
291
292         ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
293
294         status = acpi_get_sleep_type_data(sleep_state,
295                                           &acpi_gbl_sleep_type_a,
296                                           &acpi_gbl_sleep_type_b);
297         if (ACPI_FAILURE(status)) {
298                 return_ACPI_STATUS(status);
299         }
300
301         /* Execute the _PTS method (Prepare To Sleep) */
302
303         arg_list.count = 1;
304         arg_list.pointer = &arg;
305         arg.type = ACPI_TYPE_INTEGER;
306         arg.integer.value = sleep_state;
307
308         status =
309             acpi_evaluate_object(NULL, METHOD_PATHNAME__PTS, &arg_list, NULL);
310         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
311                 return_ACPI_STATUS(status);
312         }
313
314         /* Setup the argument to the _SST method (System STatus) */
315
316         switch (sleep_state) {
317         case ACPI_STATE_S0:
318
319                 sst_value = ACPI_SST_WORKING;
320                 break;
321
322         case ACPI_STATE_S1:
323         case ACPI_STATE_S2:
324         case ACPI_STATE_S3:
325
326                 sst_value = ACPI_SST_SLEEPING;
327                 break;
328
329         case ACPI_STATE_S4:
330
331                 sst_value = ACPI_SST_SLEEP_CONTEXT;
332                 break;
333
334         default:
335
336                 sst_value = ACPI_SST_INDICATOR_OFF;     /* Default is off */
337                 break;
338         }
339
340         /*
341          * Set the system indicators to show the desired sleep state.
342          * _SST is an optional method (return no error if not found)
343          */
344         acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, sst_value);
345         return_ACPI_STATUS(AE_OK);
346 }
347
348 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
349
350 /*******************************************************************************
351  *
352  * FUNCTION:    acpi_enter_sleep_state
353  *
354  * PARAMETERS:  sleep_state         - Which sleep state to enter
355  *
356  * RETURN:      Status
357  *
358  * DESCRIPTION: Enter a system sleep state
359  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
360  *
361  ******************************************************************************/
362 acpi_status acpi_enter_sleep_state(u8 sleep_state)
363 {
364         acpi_status status;
365
366         ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
367
368         if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
369             (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
370                 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
371                             acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
372                 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
373         }
374
375         status = acpi_hw_sleep_dispatch(sleep_state, ACPI_SLEEP_FUNCTION_ID);
376         return_ACPI_STATUS(status);
377 }
378
379 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
380
381 /*******************************************************************************
382  *
383  * FUNCTION:    acpi_leave_sleep_state_prep
384  *
385  * PARAMETERS:  sleep_state         - Which sleep state we are exiting
386  *
387  * RETURN:      Status
388  *
389  * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
390  *              sleep. Called with interrupts DISABLED.
391  *              We break wake/resume into 2 stages so that OSPM can handle
392  *              various OS-specific tasks between the two steps.
393  *
394  ******************************************************************************/
395 acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
396 {
397         acpi_status status;
398
399         ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
400
401         status =
402             acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_PREP_FUNCTION_ID);
403         return_ACPI_STATUS(status);
404 }
405
406 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep)
407
408 /*******************************************************************************
409  *
410  * FUNCTION:    acpi_leave_sleep_state
411  *
412  * PARAMETERS:  sleep_state         - Which sleep state we are exiting
413  *
414  * RETURN:      Status
415  *
416  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
417  *              Called with interrupts ENABLED.
418  *
419  ******************************************************************************/
420 acpi_status acpi_leave_sleep_state(u8 sleep_state)
421 {
422         acpi_status status;
423
424         ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
425
426         status = acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_FUNCTION_ID);
427         return_ACPI_STATUS(status);
428 }
429
430 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)