GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / acpi / acpica / hwsleep.c
1 /******************************************************************************
2  *
3  * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
4  *                   original/legacy sleep/PM registers.
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2017, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47
48 #define _COMPONENT          ACPI_HARDWARE
49 ACPI_MODULE_NAME("hwsleep")
50
51 #if (!ACPI_REDUCED_HARDWARE)    /* Entire module */
52 /*******************************************************************************
53  *
54  * FUNCTION:    acpi_hw_legacy_sleep
55  *
56  * PARAMETERS:  sleep_state         - Which sleep state to enter
57  *
58  * RETURN:      Status
59  *
60  * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
61  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
62  *
63  ******************************************************************************/
64 acpi_status acpi_hw_legacy_sleep(u8 sleep_state)
65 {
66         struct acpi_bit_register_info *sleep_type_reg_info;
67         struct acpi_bit_register_info *sleep_enable_reg_info;
68         u32 pm1a_control;
69         u32 pm1b_control;
70         u32 in_value;
71         acpi_status status;
72
73         ACPI_FUNCTION_TRACE(hw_legacy_sleep);
74
75         sleep_type_reg_info =
76             acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
77         sleep_enable_reg_info =
78             acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
79
80         /* Clear wake status */
81
82         status = acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
83                                          ACPI_CLEAR_STATUS);
84         if (ACPI_FAILURE(status)) {
85                 return_ACPI_STATUS(status);
86         }
87
88         /* Clear all fixed and general purpose status bits */
89
90         status = acpi_hw_clear_acpi_status();
91         if (ACPI_FAILURE(status)) {
92                 return_ACPI_STATUS(status);
93         }
94
95         /*
96          * 1) Disable/Clear all GPEs
97          * 2) Enable all wakeup GPEs
98          */
99         status = acpi_hw_disable_all_gpes();
100         if (ACPI_FAILURE(status)) {
101                 return_ACPI_STATUS(status);
102         }
103         acpi_gbl_system_awake_and_running = FALSE;
104
105         status = acpi_hw_enable_all_wakeup_gpes();
106         if (ACPI_FAILURE(status)) {
107                 return_ACPI_STATUS(status);
108         }
109
110         /* Get current value of PM1A control */
111
112         status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
113                                        &pm1a_control);
114         if (ACPI_FAILURE(status)) {
115                 return_ACPI_STATUS(status);
116         }
117         ACPI_DEBUG_PRINT((ACPI_DB_INIT,
118                           "Entering sleep state [S%u]\n", sleep_state));
119
120         /* Clear the SLP_EN and SLP_TYP fields */
121
122         pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
123                           sleep_enable_reg_info->access_bit_mask);
124         pm1b_control = pm1a_control;
125
126         /* Insert the SLP_TYP bits */
127
128         pm1a_control |=
129             (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
130         pm1b_control |=
131             (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
132
133         /*
134          * We split the writes of SLP_TYP and SLP_EN to workaround
135          * poorly implemented hardware.
136          */
137
138         /* Write #1: write the SLP_TYP data to the PM1 Control registers */
139
140         status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
141         if (ACPI_FAILURE(status)) {
142                 return_ACPI_STATUS(status);
143         }
144
145         /* Insert the sleep enable (SLP_EN) bit */
146
147         pm1a_control |= sleep_enable_reg_info->access_bit_mask;
148         pm1b_control |= sleep_enable_reg_info->access_bit_mask;
149
150         /* Flush caches, as per ACPI specification */
151
152         if (sleep_state < ACPI_STATE_S4) {
153                 ACPI_FLUSH_CPU_CACHE();
154         }
155
156         status = acpi_os_enter_sleep(sleep_state, pm1a_control, pm1b_control);
157         if (status == AE_CTRL_TERMINATE) {
158                 return_ACPI_STATUS(AE_OK);
159         }
160         if (ACPI_FAILURE(status)) {
161                 return_ACPI_STATUS(status);
162         }
163
164         /* Write #2: Write both SLP_TYP + SLP_EN */
165
166         status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
167         if (ACPI_FAILURE(status)) {
168                 return_ACPI_STATUS(status);
169         }
170
171         if (sleep_state > ACPI_STATE_S3) {
172                 /*
173                  * We wanted to sleep > S3, but it didn't happen (by virtue of the
174                  * fact that we are still executing!)
175                  *
176                  * Wait ten seconds, then try again. This is to get S4/S5 to work on
177                  * all machines.
178                  *
179                  * We wait so long to allow chipsets that poll this reg very slowly
180                  * to still read the right value. Ideally, this block would go
181                  * away entirely.
182                  */
183                 acpi_os_stall(10 * ACPI_USEC_PER_SEC);
184
185                 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
186                                                 sleep_enable_reg_info->
187                                                 access_bit_mask);
188                 if (ACPI_FAILURE(status)) {
189                         return_ACPI_STATUS(status);
190                 }
191         }
192
193         /* Wait for transition back to Working State */
194
195         do {
196                 status =
197                     acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
198                 if (ACPI_FAILURE(status)) {
199                         return_ACPI_STATUS(status);
200                 }
201
202         } while (!in_value);
203
204         return_ACPI_STATUS(AE_OK);
205 }
206
207 /*******************************************************************************
208  *
209  * FUNCTION:    acpi_hw_legacy_wake_prep
210  *
211  * PARAMETERS:  sleep_state         - Which sleep state we just exited
212  *
213  * RETURN:      Status
214  *
215  * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
216  *              sleep.
217  *              Called with interrupts ENABLED.
218  *
219  ******************************************************************************/
220
221 acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state)
222 {
223         acpi_status status = AE_OK;
224         struct acpi_bit_register_info *sleep_type_reg_info;
225         struct acpi_bit_register_info *sleep_enable_reg_info;
226         u32 pm1a_control;
227         u32 pm1b_control;
228
229         ACPI_FUNCTION_TRACE(hw_legacy_wake_prep);
230
231         /*
232          * Set SLP_TYPE and SLP_EN to state S0.
233          * This is unclear from the ACPI Spec, but it is required
234          * by some machines.
235          */
236         if (acpi_gbl_sleep_type_a_s0 != ACPI_SLEEP_TYPE_INVALID) {
237                 sleep_type_reg_info =
238                     acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
239                 sleep_enable_reg_info =
240                     acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
241
242                 /* Get current value of PM1A control */
243
244                 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
245                                                &pm1a_control);
246                 if (ACPI_SUCCESS(status)) {
247
248                         /* Clear the SLP_EN and SLP_TYP fields */
249
250                         pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
251                                           sleep_enable_reg_info->
252                                           access_bit_mask);
253                         pm1b_control = pm1a_control;
254
255                         /* Insert the SLP_TYP bits */
256
257                         pm1a_control |= (acpi_gbl_sleep_type_a_s0 <<
258                                          sleep_type_reg_info->bit_position);
259                         pm1b_control |= (acpi_gbl_sleep_type_b_s0 <<
260                                          sleep_type_reg_info->bit_position);
261
262                         /* Write the control registers and ignore any errors */
263
264                         (void)acpi_hw_write_pm1_control(pm1a_control,
265                                                         pm1b_control);
266                 }
267         }
268
269         return_ACPI_STATUS(status);
270 }
271
272 /*******************************************************************************
273  *
274  * FUNCTION:    acpi_hw_legacy_wake
275  *
276  * PARAMETERS:  sleep_state         - Which sleep state we just exited
277  *
278  * RETURN:      Status
279  *
280  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
281  *              Called with interrupts ENABLED.
282  *
283  ******************************************************************************/
284
285 acpi_status acpi_hw_legacy_wake(u8 sleep_state)
286 {
287         acpi_status status;
288
289         ACPI_FUNCTION_TRACE(hw_legacy_wake);
290
291         /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
292
293         acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
294         acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING);
295
296         /*
297          * GPEs must be enabled before _WAK is called as GPEs
298          * might get fired there
299          *
300          * Restore the GPEs:
301          * 1) Disable/Clear all GPEs
302          * 2) Enable all runtime GPEs
303          */
304         status = acpi_hw_disable_all_gpes();
305         if (ACPI_FAILURE(status)) {
306                 return_ACPI_STATUS(status);
307         }
308
309         status = acpi_hw_enable_all_runtime_gpes();
310         if (ACPI_FAILURE(status)) {
311                 return_ACPI_STATUS(status);
312         }
313
314         /*
315          * Now we can execute _WAK, etc. Some machines require that the GPEs
316          * are enabled before the wake methods are executed.
317          */
318         acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state);
319
320         /*
321          * Some BIOS code assumes that WAK_STS will be cleared on resume
322          * and use it to determine whether the system is rebooting or
323          * resuming. Clear WAK_STS for compatibility.
324          */
325         (void)acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
326                                       ACPI_CLEAR_STATUS);
327         acpi_gbl_system_awake_and_running = TRUE;
328
329         /* Enable power button */
330
331         (void)
332             acpi_write_bit_register(acpi_gbl_fixed_event_info
333                                     [ACPI_EVENT_POWER_BUTTON].
334                                     enable_register_id, ACPI_ENABLE_EVENT);
335
336         (void)
337             acpi_write_bit_register(acpi_gbl_fixed_event_info
338                                     [ACPI_EVENT_POWER_BUTTON].
339                                     status_register_id, ACPI_CLEAR_STATUS);
340
341         acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WORKING);
342         return_ACPI_STATUS(status);
343 }
344
345 #endif                          /* !ACPI_REDUCED_HARDWARE */