blob: ac09c31cc70e4e8461f443fa94f1d7f1a94d1022 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: exsystem - Interface to OS services
4 *
5 *****************************************************************************/
6
7/*
Bob Moorec8100dc2016-01-15 08:17:03 +08008 * Copyright (C) 2000 - 2016, 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();
Len Brown4be44fc2005-08-05 00:44:28 -040081 status = acpi_os_wait_semaphore(semaphore, 1, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Len Brown4be44fc2005-08-05 00:44:28 -040083 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
84 "*** Thread awake after blocking, %s\n",
85 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /* Reacquire the interpreter */
88
Lv Zhenge2b8ddc2014-03-24 14:48:45 +080089 acpi_ex_enter_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
Len Brown4be44fc2005-08-05 00:44:28 -040092 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*******************************************************************************
96 *
Bob Moore967440e32006-06-23 17:04:00 -040097 * FUNCTION: acpi_ex_system_wait_mutex
98 *
Bob Mooreba494be2012-07-12 09:40:10 +080099 * PARAMETERS: mutex - Mutex to wait on
100 * timeout - Max time to wait
Bob Moore967440e32006-06-23 17:04:00 -0400101 *
102 * RETURN: Status
103 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400104 * DESCRIPTION: Implements a mutex wait with a check to see if the
Bob Moore73a30902012-10-31 02:26:55 +0000105 * mutex is available immediately. If it is not, the
Bob Mooref6dd9222006-07-07 20:44:38 -0400106 * interpreter is released before waiting.
Bob Moore967440e32006-06-23 17:04:00 -0400107 *
108 ******************************************************************************/
109
110acpi_status acpi_ex_system_wait_mutex(acpi_mutex mutex, u16 timeout)
111{
112 acpi_status status;
Bob Moore967440e32006-06-23 17:04:00 -0400113
114 ACPI_FUNCTION_TRACE(ex_system_wait_mutex);
115
116 status = acpi_os_acquire_mutex(mutex, ACPI_DO_NOT_WAIT);
117 if (ACPI_SUCCESS(status)) {
118 return_ACPI_STATUS(status);
119 }
120
121 if (status == AE_TIME) {
122
123 /* We must wait, so unlock the interpreter */
124
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800125 acpi_ex_exit_interpreter();
Bob Moore967440e32006-06-23 17:04:00 -0400126 status = acpi_os_acquire_mutex(mutex, timeout);
127
128 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
129 "*** Thread awake after blocking, %s\n",
130 acpi_format_exception(status)));
131
132 /* Reacquire the interpreter */
133
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800134 acpi_ex_enter_interpreter();
Bob Moore967440e32006-06-23 17:04:00 -0400135 }
136
137 return_ACPI_STATUS(status);
138}
139
140/*******************************************************************************
141 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * FUNCTION: acpi_ex_system_do_stall
143 *
Robert Moore44f6c012005-04-18 22:49:35 -0400144 * PARAMETERS: how_long - The amount of time to stall,
145 * in microseconds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *
147 * RETURN: Status
148 *
149 * DESCRIPTION: Suspend running thread for specified amount of time.
150 * Note: ACPI specification requires that Stall() does not
151 * relinquish the processor, and delays longer than 100 usec
Bob Moore73a30902012-10-31 02:26:55 +0000152 * should use Sleep() instead. We allow stalls up to 255 usec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * for compatibility with other interpreters and existing BIOSs.
154 *
155 ******************************************************************************/
156
Len Brown4be44fc2005-08-05 00:44:28 -0400157acpi_status acpi_ex_system_do_stall(u32 how_long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Len Brown4be44fc2005-08-05 00:44:28 -0400159 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Len Brown4be44fc2005-08-05 00:44:28 -0400161 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Len Brown4be44fc2005-08-05 00:44:28 -0400163 if (how_long > 255) { /* 255 microseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * Longer than 255 usec, this is an error
166 *
167 * (ACPI specifies 100 usec as max, but this gives some slack in
168 * order to support existing BIOSs)
169 */
Bob Moore1fad8732015-12-29 13:54:36 +0800170 ACPI_ERROR((AE_INFO,
171 "Time parameter is too large (%u)", how_long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 status = AE_AML_OPERAND_VALUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400173 } else {
174 acpi_os_stall(how_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
177 return (status);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*******************************************************************************
181 *
Bob Mooreada241d2010-04-27 11:48:02 +0800182 * FUNCTION: acpi_ex_system_do_sleep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
Bob Mooreada241d2010-04-27 11:48:02 +0800184 * PARAMETERS: how_long - The amount of time to sleep,
Robert Moore44f6c012005-04-18 22:49:35 -0400185 * in milliseconds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
187 * RETURN: None
188 *
Bob Mooreada241d2010-04-27 11:48:02 +0800189 * DESCRIPTION: Sleep the running thread for specified amount of time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
191 ******************************************************************************/
192
Bob Mooreada241d2010-04-27 11:48:02 +0800193acpi_status acpi_ex_system_do_sleep(u64 how_long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Len Brown4be44fc2005-08-05 00:44:28 -0400195 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* Since this thread will sleep, we must release the interpreter */
198
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800199 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Bob Moore9cbfa182010-05-26 11:22:41 +0800201 /*
202 * For compatibility with other ACPI implementations and to prevent
203 * accidental deep sleeps, limit the sleep time to something reasonable.
204 */
205 if (how_long > ACPI_MAX_SLEEP) {
206 how_long = ACPI_MAX_SLEEP;
207 }
208
Len Brown4be44fc2005-08-05 00:44:28 -0400209 acpi_os_sleep(how_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* And now we must get the interpreter again */
212
Lv Zhenge2b8ddc2014-03-24 14:48:45 +0800213 acpi_ex_enter_interpreter();
Len Brown4d2acd92007-05-09 22:56:38 -0400214 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*******************************************************************************
218 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * FUNCTION: acpi_ex_system_signal_event
220 *
Robert Moore44f6c012005-04-18 22:49:35 -0400221 * PARAMETERS: obj_desc - The object descriptor for this op
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
Robert Moore44f6c012005-04-18 22:49:35 -0400223 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
225 * DESCRIPTION: Provides an access point to perform synchronization operations
226 * within the AML.
227 *
228 ******************************************************************************/
229
Bob Moorec81da662007-02-02 19:48:18 +0300230acpi_status acpi_ex_system_signal_event(union acpi_operand_object * obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Len Brown4be44fc2005-08-05 00:44:28 -0400232 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Bob Mooreb229cf92006-04-21 17:15:00 -0400234 ACPI_FUNCTION_TRACE(ex_system_signal_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (obj_desc) {
Bob Moore967440e32006-06-23 17:04:00 -0400237 status =
238 acpi_os_signal_semaphore(obj_desc->event.os_semaphore, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*******************************************************************************
245 *
246 * FUNCTION: acpi_ex_system_wait_event
247 *
Robert Moore44f6c012005-04-18 22:49:35 -0400248 * PARAMETERS: time_desc - The 'time to delay' object descriptor
249 * obj_desc - The object descriptor for this op
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
251 * RETURN: Status
252 *
253 * DESCRIPTION: Provides an access point to perform synchronization operations
Bob Moore73a30902012-10-31 02:26:55 +0000254 * within the AML. This operation is a request to wait for an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * event.
256 *
257 ******************************************************************************/
258
259acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400260acpi_ex_system_wait_event(union acpi_operand_object *time_desc,
261 union acpi_operand_object *obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Len Brown4be44fc2005-08-05 00:44:28 -0400263 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Bob Mooreb229cf92006-04-21 17:15:00 -0400265 ACPI_FUNCTION_TRACE(ex_system_wait_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400268 status =
Bob Moore967440e32006-06-23 17:04:00 -0400269 acpi_ex_system_wait_semaphore(obj_desc->event.os_semaphore,
Len Brown4be44fc2005-08-05 00:44:28 -0400270 (u16) time_desc->integer.
271 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Len Brown4be44fc2005-08-05 00:44:28 -0400274 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*******************************************************************************
278 *
279 * FUNCTION: acpi_ex_system_reset_event
280 *
Robert Moore44f6c012005-04-18 22:49:35 -0400281 * PARAMETERS: obj_desc - The object descriptor for this op
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
283 * RETURN: Status
284 *
285 * DESCRIPTION: Reset an event to a known state.
286 *
287 ******************************************************************************/
288
Len Brown4be44fc2005-08-05 00:44:28 -0400289acpi_status acpi_ex_system_reset_event(union acpi_operand_object *obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Len Brown4be44fc2005-08-05 00:44:28 -0400291 acpi_status status = AE_OK;
Bob Moore967440e32006-06-23 17:04:00 -0400292 acpi_semaphore temp_semaphore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Len Brown4be44fc2005-08-05 00:44:28 -0400294 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
297 * We are going to simply delete the existing semaphore and
298 * create a new one!
299 */
Len Brown4be44fc2005-08-05 00:44:28 -0400300 status =
301 acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0, &temp_semaphore);
302 if (ACPI_SUCCESS(status)) {
Bob Moore967440e32006-06-23 17:04:00 -0400303 (void)acpi_os_delete_semaphore(obj_desc->event.os_semaphore);
304 obj_desc->event.os_semaphore = temp_semaphore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307 return (status);
308}