1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Module Name: exsystem - Interface to OS services
4433d6423SLionel Sambuc *
5433d6423SLionel Sambuc *****************************************************************************/
6433d6423SLionel Sambuc
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc * All rights reserved.
10433d6423SLionel Sambuc *
11*29492bb7SDavid van Moolenbroek * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek * are met:
14*29492bb7SDavid van Moolenbroek * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek * notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek * without modification.
17*29492bb7SDavid van Moolenbroek * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek * substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek * ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek * including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek * binary redistribution.
22*29492bb7SDavid van Moolenbroek * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek * of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek * from this software without specific prior written permission.
25433d6423SLionel Sambuc *
26*29492bb7SDavid van Moolenbroek * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek * Software Foundation.
29433d6423SLionel Sambuc *
30*29492bb7SDavid van Moolenbroek * NO WARRANTY
31*29492bb7SDavid van Moolenbroek * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek */
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "acinterp.h"
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc #define _COMPONENT ACPI_EXECUTER
49433d6423SLionel Sambuc ACPI_MODULE_NAME ("exsystem")
50433d6423SLionel Sambuc
51433d6423SLionel Sambuc
52433d6423SLionel Sambuc /*******************************************************************************
53433d6423SLionel Sambuc *
54433d6423SLionel Sambuc * FUNCTION: AcpiExSystemWaitSemaphore
55433d6423SLionel Sambuc *
56433d6423SLionel Sambuc * PARAMETERS: Semaphore - Semaphore to wait on
57433d6423SLionel Sambuc * Timeout - Max time to wait
58433d6423SLionel Sambuc *
59433d6423SLionel Sambuc * RETURN: Status
60433d6423SLionel Sambuc *
61433d6423SLionel Sambuc * DESCRIPTION: Implements a semaphore wait with a check to see if the
62433d6423SLionel Sambuc * semaphore is available immediately. If it is not, the
63433d6423SLionel Sambuc * interpreter is released before waiting.
64433d6423SLionel Sambuc *
65433d6423SLionel Sambuc ******************************************************************************/
66433d6423SLionel Sambuc
67433d6423SLionel Sambuc ACPI_STATUS
AcpiExSystemWaitSemaphore(ACPI_SEMAPHORE Semaphore,UINT16 Timeout)68433d6423SLionel Sambuc AcpiExSystemWaitSemaphore (
69433d6423SLionel Sambuc ACPI_SEMAPHORE Semaphore,
70433d6423SLionel Sambuc UINT16 Timeout)
71433d6423SLionel Sambuc {
72433d6423SLionel Sambuc ACPI_STATUS Status;
73433d6423SLionel Sambuc
74433d6423SLionel Sambuc
75433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (ExSystemWaitSemaphore);
76433d6423SLionel Sambuc
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc Status = AcpiOsWaitSemaphore (Semaphore, 1, ACPI_DO_NOT_WAIT);
79433d6423SLionel Sambuc if (ACPI_SUCCESS (Status))
80433d6423SLionel Sambuc {
81433d6423SLionel Sambuc return_ACPI_STATUS (Status);
82433d6423SLionel Sambuc }
83433d6423SLionel Sambuc
84433d6423SLionel Sambuc if (Status == AE_TIME)
85433d6423SLionel Sambuc {
86433d6423SLionel Sambuc /* We must wait, so unlock the interpreter */
87433d6423SLionel Sambuc
88*29492bb7SDavid van Moolenbroek AcpiExExitInterpreter ();
89433d6423SLionel Sambuc
90433d6423SLionel Sambuc Status = AcpiOsWaitSemaphore (Semaphore, 1, Timeout);
91433d6423SLionel Sambuc
92433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
93433d6423SLionel Sambuc "*** Thread awake after blocking, %s\n",
94433d6423SLionel Sambuc AcpiFormatException (Status)));
95433d6423SLionel Sambuc
96433d6423SLionel Sambuc /* Reacquire the interpreter */
97433d6423SLionel Sambuc
98*29492bb7SDavid van Moolenbroek AcpiExEnterInterpreter ();
99433d6423SLionel Sambuc }
100433d6423SLionel Sambuc
101433d6423SLionel Sambuc return_ACPI_STATUS (Status);
102433d6423SLionel Sambuc }
103433d6423SLionel Sambuc
104433d6423SLionel Sambuc
105433d6423SLionel Sambuc /*******************************************************************************
106433d6423SLionel Sambuc *
107433d6423SLionel Sambuc * FUNCTION: AcpiExSystemWaitMutex
108433d6423SLionel Sambuc *
109433d6423SLionel Sambuc * PARAMETERS: Mutex - Mutex to wait on
110433d6423SLionel Sambuc * Timeout - Max time to wait
111433d6423SLionel Sambuc *
112433d6423SLionel Sambuc * RETURN: Status
113433d6423SLionel Sambuc *
114433d6423SLionel Sambuc * DESCRIPTION: Implements a mutex wait with a check to see if the
115433d6423SLionel Sambuc * mutex is available immediately. If it is not, the
116433d6423SLionel Sambuc * interpreter is released before waiting.
117433d6423SLionel Sambuc *
118433d6423SLionel Sambuc ******************************************************************************/
119433d6423SLionel Sambuc
120433d6423SLionel Sambuc ACPI_STATUS
AcpiExSystemWaitMutex(ACPI_MUTEX Mutex,UINT16 Timeout)121433d6423SLionel Sambuc AcpiExSystemWaitMutex (
122433d6423SLionel Sambuc ACPI_MUTEX Mutex,
123433d6423SLionel Sambuc UINT16 Timeout)
124433d6423SLionel Sambuc {
125433d6423SLionel Sambuc ACPI_STATUS Status;
126433d6423SLionel Sambuc
127433d6423SLionel Sambuc
128433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (ExSystemWaitMutex);
129433d6423SLionel Sambuc
130433d6423SLionel Sambuc
131433d6423SLionel Sambuc Status = AcpiOsAcquireMutex (Mutex, ACPI_DO_NOT_WAIT);
132433d6423SLionel Sambuc if (ACPI_SUCCESS (Status))
133433d6423SLionel Sambuc {
134433d6423SLionel Sambuc return_ACPI_STATUS (Status);
135433d6423SLionel Sambuc }
136433d6423SLionel Sambuc
137433d6423SLionel Sambuc if (Status == AE_TIME)
138433d6423SLionel Sambuc {
139433d6423SLionel Sambuc /* We must wait, so unlock the interpreter */
140433d6423SLionel Sambuc
141*29492bb7SDavid van Moolenbroek AcpiExExitInterpreter ();
142433d6423SLionel Sambuc
143433d6423SLionel Sambuc Status = AcpiOsAcquireMutex (Mutex, Timeout);
144433d6423SLionel Sambuc
145433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
146433d6423SLionel Sambuc "*** Thread awake after blocking, %s\n",
147433d6423SLionel Sambuc AcpiFormatException (Status)));
148433d6423SLionel Sambuc
149433d6423SLionel Sambuc /* Reacquire the interpreter */
150433d6423SLionel Sambuc
151*29492bb7SDavid van Moolenbroek AcpiExEnterInterpreter ();
152433d6423SLionel Sambuc }
153433d6423SLionel Sambuc
154433d6423SLionel Sambuc return_ACPI_STATUS (Status);
155433d6423SLionel Sambuc }
156433d6423SLionel Sambuc
157433d6423SLionel Sambuc
158433d6423SLionel Sambuc /*******************************************************************************
159433d6423SLionel Sambuc *
160433d6423SLionel Sambuc * FUNCTION: AcpiExSystemDoStall
161433d6423SLionel Sambuc *
162433d6423SLionel Sambuc * PARAMETERS: HowLong - The amount of time to stall,
163433d6423SLionel Sambuc * in microseconds
164433d6423SLionel Sambuc *
165433d6423SLionel Sambuc * RETURN: Status
166433d6423SLionel Sambuc *
167433d6423SLionel Sambuc * DESCRIPTION: Suspend running thread for specified amount of time.
168433d6423SLionel Sambuc * Note: ACPI specification requires that Stall() does not
169433d6423SLionel Sambuc * relinquish the processor, and delays longer than 100 usec
170433d6423SLionel Sambuc * should use Sleep() instead. We allow stalls up to 255 usec
171433d6423SLionel Sambuc * for compatibility with other interpreters and existing BIOSs.
172433d6423SLionel Sambuc *
173433d6423SLionel Sambuc ******************************************************************************/
174433d6423SLionel Sambuc
175433d6423SLionel Sambuc ACPI_STATUS
AcpiExSystemDoStall(UINT32 HowLong)176433d6423SLionel Sambuc AcpiExSystemDoStall (
177433d6423SLionel Sambuc UINT32 HowLong)
178433d6423SLionel Sambuc {
179433d6423SLionel Sambuc ACPI_STATUS Status = AE_OK;
180433d6423SLionel Sambuc
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
183433d6423SLionel Sambuc
184433d6423SLionel Sambuc
185433d6423SLionel Sambuc if (HowLong > 255) /* 255 microseconds */
186433d6423SLionel Sambuc {
187433d6423SLionel Sambuc /*
188433d6423SLionel Sambuc * Longer than 255 usec, this is an error
189433d6423SLionel Sambuc *
190433d6423SLionel Sambuc * (ACPI specifies 100 usec as max, but this gives some slack in
191433d6423SLionel Sambuc * order to support existing BIOSs)
192433d6423SLionel Sambuc */
193433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO, "Time parameter is too large (%u)",
194433d6423SLionel Sambuc HowLong));
195433d6423SLionel Sambuc Status = AE_AML_OPERAND_VALUE;
196433d6423SLionel Sambuc }
197433d6423SLionel Sambuc else
198433d6423SLionel Sambuc {
199433d6423SLionel Sambuc AcpiOsStall (HowLong);
200433d6423SLionel Sambuc }
201433d6423SLionel Sambuc
202433d6423SLionel Sambuc return (Status);
203433d6423SLionel Sambuc }
204433d6423SLionel Sambuc
205433d6423SLionel Sambuc
206433d6423SLionel Sambuc /*******************************************************************************
207433d6423SLionel Sambuc *
208433d6423SLionel Sambuc * FUNCTION: AcpiExSystemDoSleep
209433d6423SLionel Sambuc *
210433d6423SLionel Sambuc * PARAMETERS: HowLong - The amount of time to sleep,
211433d6423SLionel Sambuc * in milliseconds
212433d6423SLionel Sambuc *
213433d6423SLionel Sambuc * RETURN: None
214433d6423SLionel Sambuc *
215433d6423SLionel Sambuc * DESCRIPTION: Sleep the running thread for specified amount of time.
216433d6423SLionel Sambuc *
217433d6423SLionel Sambuc ******************************************************************************/
218433d6423SLionel Sambuc
219433d6423SLionel Sambuc ACPI_STATUS
AcpiExSystemDoSleep(UINT64 HowLong)220433d6423SLionel Sambuc AcpiExSystemDoSleep (
221433d6423SLionel Sambuc UINT64 HowLong)
222433d6423SLionel Sambuc {
223433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
224433d6423SLionel Sambuc
225433d6423SLionel Sambuc
226433d6423SLionel Sambuc /* Since this thread will sleep, we must release the interpreter */
227433d6423SLionel Sambuc
228*29492bb7SDavid van Moolenbroek AcpiExExitInterpreter ();
229433d6423SLionel Sambuc
230433d6423SLionel Sambuc /*
231433d6423SLionel Sambuc * For compatibility with other ACPI implementations and to prevent
232433d6423SLionel Sambuc * accidental deep sleeps, limit the sleep time to something reasonable.
233433d6423SLionel Sambuc */
234433d6423SLionel Sambuc if (HowLong > ACPI_MAX_SLEEP)
235433d6423SLionel Sambuc {
236433d6423SLionel Sambuc HowLong = ACPI_MAX_SLEEP;
237433d6423SLionel Sambuc }
238433d6423SLionel Sambuc
239433d6423SLionel Sambuc AcpiOsSleep (HowLong);
240433d6423SLionel Sambuc
241433d6423SLionel Sambuc /* And now we must get the interpreter again */
242433d6423SLionel Sambuc
243*29492bb7SDavid van Moolenbroek AcpiExEnterInterpreter ();
244433d6423SLionel Sambuc return (AE_OK);
245433d6423SLionel Sambuc }
246433d6423SLionel Sambuc
247433d6423SLionel Sambuc
248433d6423SLionel Sambuc /*******************************************************************************
249433d6423SLionel Sambuc *
250433d6423SLionel Sambuc * FUNCTION: AcpiExSystemSignalEvent
251433d6423SLionel Sambuc *
252433d6423SLionel Sambuc * PARAMETERS: ObjDesc - The object descriptor for this op
253433d6423SLionel Sambuc *
254433d6423SLionel Sambuc * RETURN: Status
255433d6423SLionel Sambuc *
256433d6423SLionel Sambuc * DESCRIPTION: Provides an access point to perform synchronization operations
257433d6423SLionel Sambuc * within the AML.
258433d6423SLionel Sambuc *
259433d6423SLionel Sambuc ******************************************************************************/
260433d6423SLionel Sambuc
261433d6423SLionel Sambuc ACPI_STATUS
AcpiExSystemSignalEvent(ACPI_OPERAND_OBJECT * ObjDesc)262433d6423SLionel Sambuc AcpiExSystemSignalEvent (
263433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc)
264433d6423SLionel Sambuc {
265433d6423SLionel Sambuc ACPI_STATUS Status = AE_OK;
266433d6423SLionel Sambuc
267433d6423SLionel Sambuc
268433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (ExSystemSignalEvent);
269433d6423SLionel Sambuc
270433d6423SLionel Sambuc
271433d6423SLionel Sambuc if (ObjDesc)
272433d6423SLionel Sambuc {
273433d6423SLionel Sambuc Status = AcpiOsSignalSemaphore (ObjDesc->Event.OsSemaphore, 1);
274433d6423SLionel Sambuc }
275433d6423SLionel Sambuc
276433d6423SLionel Sambuc return_ACPI_STATUS (Status);
277433d6423SLionel Sambuc }
278433d6423SLionel Sambuc
279433d6423SLionel Sambuc
280433d6423SLionel Sambuc /*******************************************************************************
281433d6423SLionel Sambuc *
282433d6423SLionel Sambuc * FUNCTION: AcpiExSystemWaitEvent
283433d6423SLionel Sambuc *
284433d6423SLionel Sambuc * PARAMETERS: TimeDesc - The 'time to delay' object descriptor
285433d6423SLionel Sambuc * ObjDesc - The object descriptor for this op
286433d6423SLionel Sambuc *
287433d6423SLionel Sambuc * RETURN: Status
288433d6423SLionel Sambuc *
289433d6423SLionel Sambuc * DESCRIPTION: Provides an access point to perform synchronization operations
290433d6423SLionel Sambuc * within the AML. This operation is a request to wait for an
291433d6423SLionel Sambuc * event.
292433d6423SLionel Sambuc *
293433d6423SLionel Sambuc ******************************************************************************/
294433d6423SLionel Sambuc
295433d6423SLionel Sambuc ACPI_STATUS
AcpiExSystemWaitEvent(ACPI_OPERAND_OBJECT * TimeDesc,ACPI_OPERAND_OBJECT * ObjDesc)296433d6423SLionel Sambuc AcpiExSystemWaitEvent (
297433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *TimeDesc,
298433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc)
299433d6423SLionel Sambuc {
300433d6423SLionel Sambuc ACPI_STATUS Status = AE_OK;
301433d6423SLionel Sambuc
302433d6423SLionel Sambuc
303433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (ExSystemWaitEvent);
304433d6423SLionel Sambuc
305433d6423SLionel Sambuc
306433d6423SLionel Sambuc if (ObjDesc)
307433d6423SLionel Sambuc {
308433d6423SLionel Sambuc Status = AcpiExSystemWaitSemaphore (ObjDesc->Event.OsSemaphore,
309433d6423SLionel Sambuc (UINT16) TimeDesc->Integer.Value);
310433d6423SLionel Sambuc }
311433d6423SLionel Sambuc
312433d6423SLionel Sambuc return_ACPI_STATUS (Status);
313433d6423SLionel Sambuc }
314433d6423SLionel Sambuc
315433d6423SLionel Sambuc
316433d6423SLionel Sambuc /*******************************************************************************
317433d6423SLionel Sambuc *
318433d6423SLionel Sambuc * FUNCTION: AcpiExSystemResetEvent
319433d6423SLionel Sambuc *
320433d6423SLionel Sambuc * PARAMETERS: ObjDesc - The object descriptor for this op
321433d6423SLionel Sambuc *
322433d6423SLionel Sambuc * RETURN: Status
323433d6423SLionel Sambuc *
324433d6423SLionel Sambuc * DESCRIPTION: Reset an event to a known state.
325433d6423SLionel Sambuc *
326433d6423SLionel Sambuc ******************************************************************************/
327433d6423SLionel Sambuc
328433d6423SLionel Sambuc ACPI_STATUS
AcpiExSystemResetEvent(ACPI_OPERAND_OBJECT * ObjDesc)329433d6423SLionel Sambuc AcpiExSystemResetEvent (
330433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc)
331433d6423SLionel Sambuc {
332433d6423SLionel Sambuc ACPI_STATUS Status = AE_OK;
333433d6423SLionel Sambuc ACPI_SEMAPHORE TempSemaphore;
334433d6423SLionel Sambuc
335433d6423SLionel Sambuc
336433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
337433d6423SLionel Sambuc
338433d6423SLionel Sambuc
339433d6423SLionel Sambuc /*
340433d6423SLionel Sambuc * We are going to simply delete the existing semaphore and
341433d6423SLionel Sambuc * create a new one!
342433d6423SLionel Sambuc */
343433d6423SLionel Sambuc Status = AcpiOsCreateSemaphore (ACPI_NO_UNIT_LIMIT, 0, &TempSemaphore);
344433d6423SLionel Sambuc if (ACPI_SUCCESS (Status))
345433d6423SLionel Sambuc {
346433d6423SLionel Sambuc (void) AcpiOsDeleteSemaphore (ObjDesc->Event.OsSemaphore);
347433d6423SLionel Sambuc ObjDesc->Event.OsSemaphore = TempSemaphore;
348433d6423SLionel Sambuc }
349433d6423SLionel Sambuc
350433d6423SLionel Sambuc return (Status);
351433d6423SLionel Sambuc }
352