blob: 05450656fe3d1606f9b432308910ee1a303e3f51 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: exsystem - Interface to OS services
4 *
5 *****************************************************************************/
6
7/*
David E. Box82a80942015-02-05 15:20:45 +08008 * Copyright (C) 2000 - 2015, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acinterp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("exsystem")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*******************************************************************************
52 *
53 * FUNCTION: acpi_ex_system_wait_semaphore
54 *
Bob Mooreba494be2012-07-12 09:40:10 +080055 * PARAMETERS: semaphore - Semaphore to wait on
56 * timeout - Max time to wait
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * RETURN: Status
59 *
60 * DESCRIPTION: Implements a semaphore wait with a check to see if the
Bob Moore73a30902012-10-31 02:26:55 +000061 * semaphore is available immediately. If it is not, the
Bob Mooref6dd9222006-07-07 20:44:38 -040062 * interpreter is released before waiting.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 ******************************************************************************/
Bob Moore967440e32006-06-23 17:04:00 -040065acpi_status acpi_ex_system_wait_semaphore(acpi_semaphore semaphore, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Len Brown4be44fc2005-08-05 00:44:28 -040067 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bob Mooreb229cf92006-04-21 17:15:00 -040069 ACPI_FUNCTION_TRACE(ex_system_wait_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Bob Moore967440e32006-06-23 17:04:00 -040071 status = acpi_os_wait_semaphore(semaphore, 1, ACPI_DO_NOT_WAIT);
Len Brown4be44fc2005-08-05 00:44:28 -040072 if (ACPI_SUCCESS(status)) {
73 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
76 if (status == AE_TIME) {
Bob Moore52fc0b02006-10-02 00:00:00 -040077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* We must wait, so unlock the interpreter */
79
Lv Zhenge2b8ddc2014-03-24 14:48:45 +080080 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Len Brown4be44fc2005-08-05 00:44:28 -040082 status = acpi_os_wait_semaphore(semaphore, 1, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Len Brown4be44fc2005-08-05 00:44:28 -040084 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
85 "*** Thread awake after blocking, %s\n",
86 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /* Reacquire the interpreter */
89
Lv Zhenge2b8ddc2014-03-24 14:48:45 +080090 acpi_ex_enter_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Len Brown4be44fc2005-08-05 00:44:28 -040093 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*******************************************************************************
97 *
Bob Moore967440e32006-06-23 17:04:00 -040098 * FUNCTION: acpi_ex_system_wait_mutex
99 *
Bob Mooreba494be2012-07-12 09:40:10 +0800100 * PARAMETERS: mutex - Mutex to wait on
101 * timeout - Max time to wait
Bob Moore967440e32006-06-23 17:04:00 -0400102 *
103 * RETURN: Status
104 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400105 * DESCRIPTION: Implements a mutex wait with a check to see if the
Bob Moore73a30902012-10-31 02:26:55 +0000106 * mutex is available immediately. If it is not, the
Bob Mooref6dd9222006-07-07 20:44:38 -0400107 * interpreter is released before waiting.
Bob Moore967440e32006-06-23 17:04:00 -0400108 *
109 ******************************************************************************/
110
111acpi_status acpi_ex_system_wait_mutex(acpi_mutex mutex, u16 timeout)
112{
113 acpi_status status;
Bob Moore967440e32006-06-23 17:04:00 -0400114
115 ACPI_FUNCTION_TRACE(ex_system_wait_mutex);
116
117 status = acpi_os_acquire_mutex(mutex, ACPI_DO_NOT_WAIT);
118 if (ACPI_SUCCESS(status)) {
119 return_ACPI_STATUS(status);
120 }
121
122 if (status == AE_TIME) {
123
124 /* We must wait, so unlock the interpreter */
125
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800126 acpi_ex_exit_interpreter();
Bob Moore967440e32006-06-23 17:04:00 -0400127
128 status = acpi_os_acquire_mutex(mutex, timeout);
129
130 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
131 "*** Thread awake after blocking, %s\n",
132 acpi_format_exception(status)));
133
134 /* Reacquire the interpreter */
135
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800136 acpi_ex_enter_interpreter();
Bob Moore967440e32006-06-23 17:04:00 -0400137 }
138
139 return_ACPI_STATUS(status);
140}
141
142/*******************************************************************************
143 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * FUNCTION: acpi_ex_system_do_stall
145 *
Robert Moore44f6c012005-04-18 22:49:35 -0400146 * PARAMETERS: how_long - The amount of time to stall,
147 * in microseconds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
149 * RETURN: Status
150 *
151 * DESCRIPTION: Suspend running thread for specified amount of time.
152 * Note: ACPI specification requires that Stall() does not
153 * relinquish the processor, and delays longer than 100 usec
Bob Moore73a30902012-10-31 02:26:55 +0000154 * should use Sleep() instead. We allow stalls up to 255 usec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * for compatibility with other interpreters and existing BIOSs.
156 *
157 ******************************************************************************/
158
Len Brown4be44fc2005-08-05 00:44:28 -0400159acpi_status acpi_ex_system_do_stall(u32 how_long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Len Brown4be44fc2005-08-05 00:44:28 -0400161 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Len Brown4be44fc2005-08-05 00:44:28 -0400163 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Len Brown4be44fc2005-08-05 00:44:28 -0400165 if (how_long > 255) { /* 255 microseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * Longer than 255 usec, this is an error
168 *
169 * (ACPI specifies 100 usec as max, but this gives some slack in
170 * order to support existing BIOSs)
171 */
Bob Mooref6a22b02010-03-05 17:56:40 +0800172 ACPI_ERROR((AE_INFO, "Time parameter is too large (%u)",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500173 how_long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 status = AE_AML_OPERAND_VALUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 } else {
176 acpi_os_stall(how_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178
179 return (status);
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*******************************************************************************
183 *
Bob Mooreada241d2010-04-27 11:48:02 +0800184 * FUNCTION: acpi_ex_system_do_sleep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
Bob Mooreada241d2010-04-27 11:48:02 +0800186 * PARAMETERS: how_long - The amount of time to sleep,
Robert Moore44f6c012005-04-18 22:49:35 -0400187 * in milliseconds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
189 * RETURN: None
190 *
Bob Mooreada241d2010-04-27 11:48:02 +0800191 * DESCRIPTION: Sleep the running thread for specified amount of time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 ******************************************************************************/
194
Bob Mooreada241d2010-04-27 11:48:02 +0800195acpi_status acpi_ex_system_do_sleep(u64 how_long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Len Brown4be44fc2005-08-05 00:44:28 -0400197 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* Since this thread will sleep, we must release the interpreter */
200
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800201 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Bob Moore9cbfa182010-05-26 11:22:41 +0800203 /*
204 * For compatibility with other ACPI implementations and to prevent
205 * accidental deep sleeps, limit the sleep time to something reasonable.
206 */
207 if (how_long > ACPI_MAX_SLEEP) {
208 how_long = ACPI_MAX_SLEEP;
209 }
210
Len Brown4be44fc2005-08-05 00:44:28 -0400211 acpi_os_sleep(how_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* And now we must get the interpreter again */
214
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800215 acpi_ex_enter_interpreter();
Len Brown4d2acd92007-05-09 22:56:38 -0400216 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*******************************************************************************
220 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * FUNCTION: acpi_ex_system_signal_event
222 *
Robert Moore44f6c012005-04-18 22:49:35 -0400223 * PARAMETERS: obj_desc - The object descriptor for this op
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
Robert Moore44f6c012005-04-18 22:49:35 -0400225 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * DESCRIPTION: Provides an access point to perform synchronization operations
228 * within the AML.
229 *
230 ******************************************************************************/
231
Bob Moorec81da662007-02-02 19:48:18 +0300232acpi_status acpi_ex_system_signal_event(union acpi_operand_object * obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Len Brown4be44fc2005-08-05 00:44:28 -0400234 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Bob Mooreb229cf92006-04-21 17:15:00 -0400236 ACPI_FUNCTION_TRACE(ex_system_signal_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (obj_desc) {
Bob Moore967440e32006-06-23 17:04:00 -0400239 status =
240 acpi_os_signal_semaphore(obj_desc->event.os_semaphore, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*******************************************************************************
247 *
248 * FUNCTION: acpi_ex_system_wait_event
249 *
Robert Moore44f6c012005-04-18 22:49:35 -0400250 * PARAMETERS: time_desc - The 'time to delay' object descriptor
251 * obj_desc - The object descriptor for this op
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
253 * RETURN: Status
254 *
255 * DESCRIPTION: Provides an access point to perform synchronization operations
Bob Moore73a30902012-10-31 02:26:55 +0000256 * within the AML. This operation is a request to wait for an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * event.
258 *
259 ******************************************************************************/
260
261acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400262acpi_ex_system_wait_event(union acpi_operand_object *time_desc,
263 union acpi_operand_object *obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Len Brown4be44fc2005-08-05 00:44:28 -0400265 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Bob Mooreb229cf92006-04-21 17:15:00 -0400267 ACPI_FUNCTION_TRACE(ex_system_wait_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400270 status =
Bob Moore967440e32006-06-23 17:04:00 -0400271 acpi_ex_system_wait_semaphore(obj_desc->event.os_semaphore,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 (u16) time_desc->integer.
273 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
Len Brown4be44fc2005-08-05 00:44:28 -0400276 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*******************************************************************************
280 *
281 * FUNCTION: acpi_ex_system_reset_event
282 *
Robert Moore44f6c012005-04-18 22:49:35 -0400283 * PARAMETERS: obj_desc - The object descriptor for this op
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
285 * RETURN: Status
286 *
287 * DESCRIPTION: Reset an event to a known state.
288 *
289 ******************************************************************************/
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291acpi_status acpi_ex_system_reset_event(union acpi_operand_object *obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Len Brown4be44fc2005-08-05 00:44:28 -0400293 acpi_status status = AE_OK;
Bob Moore967440e32006-06-23 17:04:00 -0400294 acpi_semaphore temp_semaphore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Len Brown4be44fc2005-08-05 00:44:28 -0400296 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
299 * We are going to simply delete the existing semaphore and
300 * create a new one!
301 */
Len Brown4be44fc2005-08-05 00:44:28 -0400302 status =
303 acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0, &temp_semaphore);
304 if (ACPI_SUCCESS(status)) {
Bob Moore967440e32006-06-23 17:04:00 -0400305 (void)acpi_os_delete_semaphore(obj_desc->event.os_semaphore);
306 obj_desc->event.os_semaphore = temp_semaphore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309 return (status);
310}