1 
2 /******************************************************************************
3  *
4  * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2004, R. Byron Moore
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 
47 #define _COMPONENT          ACPI_HARDWARE
48 	 ACPI_MODULE_NAME    ("hwsleep")
49 
50 
51 #define METHOD_NAME__BFS        "\\_BFS"
52 #define METHOD_NAME__GTS        "\\_GTS"
53 #define METHOD_NAME__PTS        "\\_PTS"
54 #define METHOD_NAME__SST        "\\_SI._SST"
55 #define METHOD_NAME__WAK        "\\_WAK"
56 
57 #define ACPI_SST_INDICATOR_OFF  0
58 #define ACPI_SST_WORKING        1
59 #define ACPI_SST_WAKING         2
60 #define ACPI_SST_SLEEPING       3
61 #define ACPI_SST_SLEEP_CONTEXT  4
62 
63 
64 /******************************************************************************
65  *
66  * FUNCTION:    acpi_set_firmware_waking_vector
67  *
68  * PARAMETERS:  physical_address    - Physical address of ACPI real mode
69  *                                    entry point.
70  *
71  * RETURN:      Status
72  *
73  * DESCRIPTION: access function for d_firmware_waking_vector field in FACS
74  *
75  ******************************************************************************/
76 
77 acpi_status
acpi_set_firmware_waking_vector(acpi_physical_address physical_address)78 acpi_set_firmware_waking_vector (
79 	acpi_physical_address physical_address)
80 {
81 
82 	ACPI_FUNCTION_TRACE ("acpi_set_firmware_waking_vector");
83 
84 
85 	/* Set the vector */
86 
87 	if (acpi_gbl_common_fACS.vector_width == 32) {
88 		*(ACPI_CAST_PTR (u32, acpi_gbl_common_fACS.firmware_waking_vector))
89 				= (u32) physical_address;
90 	}
91 	else {
92 		*acpi_gbl_common_fACS.firmware_waking_vector
93 				= physical_address;
94 	}
95 
96 	return_ACPI_STATUS (AE_OK);
97 }
98 
99 
100 /******************************************************************************
101  *
102  * FUNCTION:    acpi_get_firmware_waking_vector
103  *
104  * PARAMETERS:  *physical_address   - Output buffer where contents of
105  *                                    the firmware_waking_vector field of
106  *                                    the FACS will be stored.
107  *
108  * RETURN:      Status
109  *
110  * DESCRIPTION: Access function for firmware_waking_vector field in FACS
111  *
112  ******************************************************************************/
113 
114 acpi_status
acpi_get_firmware_waking_vector(acpi_physical_address * physical_address)115 acpi_get_firmware_waking_vector (
116 	acpi_physical_address *physical_address)
117 {
118 
119 	ACPI_FUNCTION_TRACE ("acpi_get_firmware_waking_vector");
120 
121 
122 	if (!physical_address) {
123 		return_ACPI_STATUS (AE_BAD_PARAMETER);
124 	}
125 
126 	/* Get the vector */
127 
128 	if (acpi_gbl_common_fACS.vector_width == 32) {
129 		*physical_address = (acpi_physical_address)
130 			*(ACPI_CAST_PTR (u32, acpi_gbl_common_fACS.firmware_waking_vector));
131 	}
132 	else {
133 		*physical_address =
134 			*acpi_gbl_common_fACS.firmware_waking_vector;
135 	}
136 
137 	return_ACPI_STATUS (AE_OK);
138 }
139 
140 
141 /******************************************************************************
142  *
143  * FUNCTION:    acpi_enter_sleep_state_prep
144  *
145  * PARAMETERS:  sleep_state         - Which sleep state to enter
146  *
147  * RETURN:      Status
148  *
149  * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
150  *              This function must execute with interrupts enabled.
151  *              We break sleeping into 2 stages so that OSPM can handle
152  *              various OS-specific tasks between the two steps.
153  *
154  ******************************************************************************/
155 
156 acpi_status
acpi_enter_sleep_state_prep(u8 sleep_state)157 acpi_enter_sleep_state_prep (
158 	u8                          sleep_state)
159 {
160 	acpi_status                 status;
161 	struct acpi_object_list     arg_list;
162 	union acpi_object           arg;
163 
164 
165 	ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_prep");
166 
167 
168 	/*
169 	 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
170 	 */
171 	status = acpi_get_sleep_type_data (sleep_state,
172 			  &acpi_gbl_sleep_type_a, &acpi_gbl_sleep_type_b);
173 	if (ACPI_FAILURE (status)) {
174 		return_ACPI_STATUS (status);
175 	}
176 
177 	/* Setup parameter object */
178 
179 	arg_list.count = 1;
180 	arg_list.pointer = &arg;
181 
182 	arg.type = ACPI_TYPE_INTEGER;
183 	arg.integer.value = sleep_state;
184 
185 	/* Run the _PTS and _GTS methods */
186 
187 	status = acpi_evaluate_object (NULL, METHOD_NAME__PTS, &arg_list, NULL);
188 	if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
189 		return_ACPI_STATUS (status);
190 	}
191 
192 	status = acpi_evaluate_object (NULL, METHOD_NAME__GTS, &arg_list, NULL);
193 	if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
194 		return_ACPI_STATUS (status);
195 	}
196 
197 	/* Setup the argument to _SST */
198 
199 	switch (sleep_state) {
200 	case ACPI_STATE_S0:
201 		arg.integer.value = ACPI_SST_WORKING;
202 		break;
203 
204 	case ACPI_STATE_S1:
205 	case ACPI_STATE_S2:
206 	case ACPI_STATE_S3:
207 		arg.integer.value = ACPI_SST_SLEEPING;
208 		break;
209 
210 	case ACPI_STATE_S4:
211 		arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
212 		break;
213 
214 	default:
215 		arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is indicator off */
216 		break;
217 	}
218 
219 	/* Set the system indicators to show the desired sleep state. */
220 
221 	status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
222 	if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
223 		 ACPI_REPORT_ERROR (("Method _SST failed, %s\n", acpi_format_exception (status)));
224 	}
225 
226 	return_ACPI_STATUS (AE_OK);
227 }
228 
229 
230 /******************************************************************************
231  *
232  * FUNCTION:    acpi_enter_sleep_state
233  *
234  * PARAMETERS:  sleep_state         - Which sleep state to enter
235  *
236  * RETURN:      Status
237  *
238  * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
239  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
240  *
241  ******************************************************************************/
242 
243 acpi_status
acpi_enter_sleep_state(u8 sleep_state)244 acpi_enter_sleep_state (
245 	u8                              sleep_state)
246 {
247 	u32                             PM1Acontrol;
248 	u32                             PM1Bcontrol;
249 	struct acpi_bit_register_info   *sleep_type_reg_info;
250 	struct acpi_bit_register_info   *sleep_enable_reg_info;
251 	u32                             in_value;
252 	acpi_status                     status;
253 
254 
255 	ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state");
256 
257 
258 	if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
259 		(acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
260 		ACPI_REPORT_ERROR (("Sleep values out of range: A=%X B=%X\n",
261 			acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
262 		return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
263 	}
264 
265 	sleep_type_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A);
266 	sleep_enable_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE);
267 
268 	/* Clear wake status */
269 
270 	status = acpi_set_register (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
271 	if (ACPI_FAILURE (status)) {
272 		return_ACPI_STATUS (status);
273 	}
274 
275 	status = acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK);
276 	if (ACPI_FAILURE (status)) {
277 		return_ACPI_STATUS (status);
278 	}
279 
280 	if (sleep_state != ACPI_STATE_S5) {
281 		/* Disable BM arbitration */
282 
283 		status = acpi_set_register (ACPI_BITREG_ARB_DISABLE, 1, ACPI_MTX_DO_NOT_LOCK);
284 		if (ACPI_FAILURE (status)) {
285 			return_ACPI_STATUS (status);
286 		}
287 	}
288 
289 	/*
290 	 * 1) Disable all runtime GPEs
291 	 * 2) Enable all wakeup GPEs
292 	 */
293 	status = acpi_hw_prepare_gpes_for_sleep ();
294 	if (ACPI_FAILURE (status)) {
295 		return_ACPI_STATUS (status);
296 	}
297 
298 	/* Get current value of PM1A control */
299 
300 	status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
301 	if (ACPI_FAILURE (status)) {
302 		return_ACPI_STATUS (status);
303 	}
304 	ACPI_DEBUG_PRINT ((ACPI_DB_INIT, "Entering sleep state [S%d]\n", sleep_state));
305 
306 	/* Clear SLP_EN and SLP_TYP fields */
307 
308 	PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask | sleep_enable_reg_info->access_bit_mask);
309 	PM1Bcontrol = PM1Acontrol;
310 
311 	/* Insert SLP_TYP bits */
312 
313 	PM1Acontrol |= (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
314 	PM1Bcontrol |= (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
315 
316 	/*
317 	 * We split the writes of SLP_TYP and SLP_EN to workaround
318 	 * poorly implemented hardware.
319 	 */
320 
321 	/* Write #1: fill in SLP_TYP data */
322 
323 	status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
324 	if (ACPI_FAILURE (status)) {
325 		return_ACPI_STATUS (status);
326 	}
327 
328 	status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
329 	if (ACPI_FAILURE (status)) {
330 		return_ACPI_STATUS (status);
331 	}
332 
333 	/* Insert SLP_ENABLE bit */
334 
335 	PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
336 	PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
337 
338 	/* Write #2: SLP_TYP + SLP_EN */
339 
340 	ACPI_FLUSH_CPU_CACHE ();
341 
342 	status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
343 	if (ACPI_FAILURE (status)) {
344 		return_ACPI_STATUS (status);
345 	}
346 
347 	status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
348 	if (ACPI_FAILURE (status)) {
349 		return_ACPI_STATUS (status);
350 	}
351 
352 	if (sleep_state > ACPI_STATE_S3) {
353 		/*
354 		 * We wanted to sleep > S3, but it didn't happen (by virtue of the fact that
355 		 * we are still executing!)
356 		 *
357 		 * Wait ten seconds, then try again. This is to get S4/S5 to work on all machines.
358 		 *
359 		 * We wait so long to allow chipsets that poll this reg very slowly to
360 		 * still read the right value. Ideally, this block would go
361 		 * away entirely.
362 		 */
363 		acpi_os_stall (10000000);
364 
365 		status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, ACPI_REGISTER_PM1_CONTROL,
366 				 sleep_enable_reg_info->access_bit_mask);
367 		if (ACPI_FAILURE (status)) {
368 			return_ACPI_STATUS (status);
369 		}
370 	}
371 
372 	/* Wait until we enter sleep state */
373 
374 	do {
375 		status = acpi_get_register (ACPI_BITREG_WAKE_STATUS, &in_value, ACPI_MTX_DO_NOT_LOCK);
376 		if (ACPI_FAILURE (status)) {
377 			return_ACPI_STATUS (status);
378 		}
379 
380 		/* Spin until we wake */
381 
382 	} while (!in_value);
383 
384 	return_ACPI_STATUS (AE_OK);
385 }
386 
387 
388 /******************************************************************************
389  *
390  * FUNCTION:    acpi_enter_sleep_state_s4bios
391  *
392  * PARAMETERS:  None
393  *
394  * RETURN:      Status
395  *
396  * DESCRIPTION: Perform a S4 bios request.
397  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
398  *
399  ******************************************************************************/
400 
401 acpi_status
acpi_enter_sleep_state_s4bios(void)402 acpi_enter_sleep_state_s4bios (
403 	void)
404 {
405 	u32                             in_value;
406 	acpi_status                     status;
407 
408 
409 	ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_s4bios");
410 
411 
412 	status = acpi_set_register (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
413 	if (ACPI_FAILURE (status)) {
414 		return_ACPI_STATUS (status);
415 	}
416 
417 	status = acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK);
418 	if (ACPI_FAILURE (status)) {
419 		return_ACPI_STATUS (status);
420 	}
421 
422 	/*
423 	 * 1) Disable all runtime GPEs
424 	 * 2) Enable all wakeup GPEs
425 	 */
426 	status = acpi_hw_prepare_gpes_for_sleep ();
427 	if (ACPI_FAILURE (status)) {
428 		return_ACPI_STATUS (status);
429 	}
430 
431 	ACPI_FLUSH_CPU_CACHE ();
432 
433 	status = acpi_os_write_port (acpi_gbl_FADT->smi_cmd, (u32) acpi_gbl_FADT->S4bios_req, 8);
434 
435 	do {
436 		acpi_os_stall(1000);
437 		status = acpi_get_register (ACPI_BITREG_WAKE_STATUS, &in_value, ACPI_MTX_DO_NOT_LOCK);
438 		if (ACPI_FAILURE (status)) {
439 			return_ACPI_STATUS (status);
440 		}
441 	} while (!in_value);
442 
443 	return_ACPI_STATUS (AE_OK);
444 }
445 
446 
447 /******************************************************************************
448  *
449  * FUNCTION:    acpi_leave_sleep_state
450  *
451  * PARAMETERS:  sleep_state         - Which sleep state we just exited
452  *
453  * RETURN:      Status
454  *
455  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
456  *
457  ******************************************************************************/
458 
459 acpi_status
acpi_leave_sleep_state(u8 sleep_state)460 acpi_leave_sleep_state (
461 	u8                              sleep_state)
462 {
463 	struct acpi_object_list         arg_list;
464 	union acpi_object               arg;
465 	acpi_status                     status;
466 	struct acpi_bit_register_info   *sleep_type_reg_info;
467 	struct acpi_bit_register_info   *sleep_enable_reg_info;
468 	u32                             PM1Acontrol;
469 	u32                             PM1Bcontrol;
470 
471 
472 	ACPI_FUNCTION_TRACE ("acpi_leave_sleep_state");
473 
474 
475 	/*
476 	 * Set SLP_TYPE and SLP_EN to state S0.
477 	 * This is unclear from the ACPI Spec, but it is required
478 	 * by some machines.
479 	 */
480 	status = acpi_get_sleep_type_data (ACPI_STATE_S0,
481 			  &acpi_gbl_sleep_type_a, &acpi_gbl_sleep_type_b);
482 	if (ACPI_SUCCESS (status)) {
483 		sleep_type_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A);
484 		sleep_enable_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE);
485 
486 		/* Get current value of PM1A control */
487 
488 		status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
489 				 ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
490 		if (ACPI_SUCCESS (status)) {
491 			/* Clear SLP_EN and SLP_TYP fields */
492 
493 			PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
494 					   sleep_enable_reg_info->access_bit_mask);
495 			PM1Bcontrol = PM1Acontrol;
496 
497 			/* Insert SLP_TYP bits */
498 
499 			PM1Acontrol |= (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
500 			PM1Bcontrol |= (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
501 
502 			/* Just ignore any errors */
503 
504 			(void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
505 					  ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
506 			(void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
507 					  ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
508 		}
509 	}
510 
511 	/* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
512 
513 	acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
514 
515 	/* Setup parameter object */
516 
517 	arg_list.count = 1;
518 	arg_list.pointer = &arg;
519 	arg.type = ACPI_TYPE_INTEGER;
520 
521 	/* Ignore any errors from these methods */
522 
523 	arg.integer.value = ACPI_SST_WAKING;
524 	status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
525 	if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
526 		ACPI_REPORT_ERROR (("Method _SST failed, %s\n", acpi_format_exception (status)));
527 	}
528 
529 	arg.integer.value = sleep_state;
530 	status = acpi_evaluate_object (NULL, METHOD_NAME__BFS, &arg_list, NULL);
531 	if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
532 		ACPI_REPORT_ERROR (("Method _BFS failed, %s\n", acpi_format_exception (status)));
533 	}
534 
535 	status = acpi_evaluate_object (NULL, METHOD_NAME__WAK, &arg_list, NULL);
536 	if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
537 		ACPI_REPORT_ERROR (("Method _WAK failed, %s\n", acpi_format_exception (status)));
538 	}
539 	/* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
540 
541 	/*
542 	 * Restore the GPEs:
543 	 * 1) Disable all wakeup GPEs
544 	 * 2) Enable all runtime GPEs
545 	 */
546 	status = acpi_hw_restore_gpes_on_wake ();
547 	if (ACPI_FAILURE (status)) {
548 		return_ACPI_STATUS (status);
549 	}
550 
551 	/* Enable power button */
552 
553 	acpi_set_register(acpi_gbl_fixed_event_info[ACPI_EVENT_POWER_BUTTON].enable_register_id,
554 			1, ACPI_MTX_DO_NOT_LOCK);
555 	acpi_set_register(acpi_gbl_fixed_event_info[ACPI_EVENT_POWER_BUTTON].status_register_id,
556 			1, ACPI_MTX_DO_NOT_LOCK);
557 
558 	/* Enable BM arbitration */
559 
560 	status = acpi_set_register (ACPI_BITREG_ARB_DISABLE, 0, ACPI_MTX_LOCK);
561 	if (ACPI_FAILURE (status)) {
562 		return_ACPI_STATUS (status);
563 	}
564 
565 	arg.integer.value = ACPI_SST_WORKING;
566 	status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
567 	if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
568 		ACPI_REPORT_ERROR (("Method _SST failed, %s\n", acpi_format_exception (status)));
569 	}
570 
571 	return_ACPI_STATUS (status);
572 }
573