1db2bae30SDana Myers /******************************************************************************
2db2bae30SDana Myers *
3db2bae30SDana Myers * Module Name: exsystem - Interface to OS services
4db2bae30SDana Myers *
5db2bae30SDana Myers *****************************************************************************/
6db2bae30SDana Myers
726f3cdf0SGordon Ross /*
8*de5d74c2SJerry Jelinek * Copyright (C) 2000 - 2016, Intel Corp.
9db2bae30SDana Myers * All rights reserved.
10db2bae30SDana Myers *
1126f3cdf0SGordon Ross * Redistribution and use in source and binary forms, with or without
1226f3cdf0SGordon Ross * modification, are permitted provided that the following conditions
1326f3cdf0SGordon Ross * are met:
1426f3cdf0SGordon Ross * 1. Redistributions of source code must retain the above copyright
1526f3cdf0SGordon Ross * notice, this list of conditions, and the following disclaimer,
1626f3cdf0SGordon Ross * without modification.
1726f3cdf0SGordon Ross * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1826f3cdf0SGordon Ross * substantially similar to the "NO WARRANTY" disclaimer below
1926f3cdf0SGordon Ross * ("Disclaimer") and any redistribution must be conditioned upon
2026f3cdf0SGordon Ross * including a substantially similar Disclaimer requirement for further
2126f3cdf0SGordon Ross * binary redistribution.
2226f3cdf0SGordon Ross * 3. Neither the names of the above-listed copyright holders nor the names
2326f3cdf0SGordon Ross * of any contributors may be used to endorse or promote products derived
2426f3cdf0SGordon Ross * from this software without specific prior written permission.
25db2bae30SDana Myers *
2626f3cdf0SGordon Ross * Alternatively, this software may be distributed under the terms of the
2726f3cdf0SGordon Ross * GNU General Public License ("GPL") version 2 as published by the Free
2826f3cdf0SGordon Ross * Software Foundation.
29db2bae30SDana Myers *
3026f3cdf0SGordon Ross * NO WARRANTY
3126f3cdf0SGordon Ross * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3226f3cdf0SGordon Ross * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3326f3cdf0SGordon Ross * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3426f3cdf0SGordon Ross * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3526f3cdf0SGordon Ross * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3626f3cdf0SGordon Ross * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3726f3cdf0SGordon Ross * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3826f3cdf0SGordon Ross * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3926f3cdf0SGordon Ross * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4026f3cdf0SGordon Ross * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4126f3cdf0SGordon Ross * POSSIBILITY OF SUCH DAMAGES.
4226f3cdf0SGordon Ross */
43db2bae30SDana Myers
44db2bae30SDana Myers #include "acpi.h"
45aa2aa9a6SDana Myers #include "accommon.h"
46db2bae30SDana Myers #include "acinterp.h"
47db2bae30SDana Myers
48db2bae30SDana Myers #define _COMPONENT ACPI_EXECUTER
49db2bae30SDana Myers ACPI_MODULE_NAME ("exsystem")
50db2bae30SDana Myers
51db2bae30SDana Myers
52db2bae30SDana Myers /*******************************************************************************
53db2bae30SDana Myers *
54db2bae30SDana Myers * FUNCTION: AcpiExSystemWaitSemaphore
55db2bae30SDana Myers *
56db2bae30SDana Myers * PARAMETERS: Semaphore - Semaphore to wait on
57db2bae30SDana Myers * Timeout - Max time to wait
58db2bae30SDana Myers *
59db2bae30SDana Myers * RETURN: Status
60db2bae30SDana Myers *
61db2bae30SDana Myers * DESCRIPTION: Implements a semaphore wait with a check to see if the
62db2bae30SDana Myers * semaphore is available immediately. If it is not, the
63db2bae30SDana Myers * interpreter is released before waiting.
64db2bae30SDana Myers *
65db2bae30SDana Myers ******************************************************************************/
66db2bae30SDana Myers
67db2bae30SDana Myers ACPI_STATUS
AcpiExSystemWaitSemaphore(ACPI_SEMAPHORE Semaphore,UINT16 Timeout)68db2bae30SDana Myers AcpiExSystemWaitSemaphore (
69db2bae30SDana Myers ACPI_SEMAPHORE Semaphore,
70db2bae30SDana Myers UINT16 Timeout)
71db2bae30SDana Myers {
72db2bae30SDana Myers ACPI_STATUS Status;
73db2bae30SDana Myers
74db2bae30SDana Myers
75db2bae30SDana Myers ACPI_FUNCTION_TRACE (ExSystemWaitSemaphore);
76db2bae30SDana Myers
77db2bae30SDana Myers
78db2bae30SDana Myers Status = AcpiOsWaitSemaphore (Semaphore, 1, ACPI_DO_NOT_WAIT);
79db2bae30SDana Myers if (ACPI_SUCCESS (Status))
80db2bae30SDana Myers {
81db2bae30SDana Myers return_ACPI_STATUS (Status);
82db2bae30SDana Myers }
83db2bae30SDana Myers
84db2bae30SDana Myers if (Status == AE_TIME)
85db2bae30SDana Myers {
86db2bae30SDana Myers /* We must wait, so unlock the interpreter */
87db2bae30SDana Myers
88*de5d74c2SJerry Jelinek AcpiExExitInterpreter ();
89db2bae30SDana Myers Status = AcpiOsWaitSemaphore (Semaphore, 1, Timeout);
90db2bae30SDana Myers
91db2bae30SDana Myers ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
92db2bae30SDana Myers "*** Thread awake after blocking, %s\n",
93db2bae30SDana Myers AcpiFormatException (Status)));
94db2bae30SDana Myers
95db2bae30SDana Myers /* Reacquire the interpreter */
96db2bae30SDana Myers
97*de5d74c2SJerry Jelinek AcpiExEnterInterpreter ();
98db2bae30SDana Myers }
99db2bae30SDana Myers
100db2bae30SDana Myers return_ACPI_STATUS (Status);
101db2bae30SDana Myers }
102db2bae30SDana Myers
103db2bae30SDana Myers
104db2bae30SDana Myers /*******************************************************************************
105db2bae30SDana Myers *
106db2bae30SDana Myers * FUNCTION: AcpiExSystemWaitMutex
107db2bae30SDana Myers *
108db2bae30SDana Myers * PARAMETERS: Mutex - Mutex to wait on
109db2bae30SDana Myers * Timeout - Max time to wait
110db2bae30SDana Myers *
111db2bae30SDana Myers * RETURN: Status
112db2bae30SDana Myers *
113db2bae30SDana Myers * DESCRIPTION: Implements a mutex wait with a check to see if the
114db2bae30SDana Myers * mutex is available immediately. If it is not, the
115db2bae30SDana Myers * interpreter is released before waiting.
116db2bae30SDana Myers *
117db2bae30SDana Myers ******************************************************************************/
118db2bae30SDana Myers
119db2bae30SDana Myers ACPI_STATUS
AcpiExSystemWaitMutex(ACPI_MUTEX Mutex,UINT16 Timeout)120db2bae30SDana Myers AcpiExSystemWaitMutex (
121db2bae30SDana Myers ACPI_MUTEX Mutex,
122db2bae30SDana Myers UINT16 Timeout)
123db2bae30SDana Myers {
124db2bae30SDana Myers ACPI_STATUS Status;
125db2bae30SDana Myers
126db2bae30SDana Myers
127db2bae30SDana Myers ACPI_FUNCTION_TRACE (ExSystemWaitMutex);
128db2bae30SDana Myers
129db2bae30SDana Myers
130db2bae30SDana Myers Status = AcpiOsAcquireMutex (Mutex, ACPI_DO_NOT_WAIT);
131db2bae30SDana Myers if (ACPI_SUCCESS (Status))
132db2bae30SDana Myers {
133db2bae30SDana Myers return_ACPI_STATUS (Status);
134db2bae30SDana Myers }
135db2bae30SDana Myers
136db2bae30SDana Myers if (Status == AE_TIME)
137db2bae30SDana Myers {
138db2bae30SDana Myers /* We must wait, so unlock the interpreter */
139db2bae30SDana Myers
140*de5d74c2SJerry Jelinek AcpiExExitInterpreter ();
141db2bae30SDana Myers Status = AcpiOsAcquireMutex (Mutex, Timeout);
142db2bae30SDana Myers
143db2bae30SDana Myers ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
144db2bae30SDana Myers "*** Thread awake after blocking, %s\n",
145db2bae30SDana Myers AcpiFormatException (Status)));
146db2bae30SDana Myers
147db2bae30SDana Myers /* Reacquire the interpreter */
148db2bae30SDana Myers
149*de5d74c2SJerry Jelinek AcpiExEnterInterpreter ();
150db2bae30SDana Myers }
151db2bae30SDana Myers
152db2bae30SDana Myers return_ACPI_STATUS (Status);
153db2bae30SDana Myers }
154db2bae30SDana Myers
155db2bae30SDana Myers
156db2bae30SDana Myers /*******************************************************************************
157db2bae30SDana Myers *
158db2bae30SDana Myers * FUNCTION: AcpiExSystemDoStall
159db2bae30SDana Myers *
160db2bae30SDana Myers * PARAMETERS: HowLong - The amount of time to stall,
161db2bae30SDana Myers * in microseconds
162db2bae30SDana Myers *
163db2bae30SDana Myers * RETURN: Status
164db2bae30SDana Myers *
165db2bae30SDana Myers * DESCRIPTION: Suspend running thread for specified amount of time.
166db2bae30SDana Myers * Note: ACPI specification requires that Stall() does not
167db2bae30SDana Myers * relinquish the processor, and delays longer than 100 usec
168db2bae30SDana Myers * should use Sleep() instead. We allow stalls up to 255 usec
169db2bae30SDana Myers * for compatibility with other interpreters and existing BIOSs.
170db2bae30SDana Myers *
171db2bae30SDana Myers ******************************************************************************/
172db2bae30SDana Myers
173db2bae30SDana Myers ACPI_STATUS
AcpiExSystemDoStall(UINT32 HowLong)174db2bae30SDana Myers AcpiExSystemDoStall (
175db2bae30SDana Myers UINT32 HowLong)
176db2bae30SDana Myers {
177db2bae30SDana Myers ACPI_STATUS Status = AE_OK;
178db2bae30SDana Myers
179db2bae30SDana Myers
180db2bae30SDana Myers ACPI_FUNCTION_ENTRY ();
181db2bae30SDana Myers
182db2bae30SDana Myers
183db2bae30SDana Myers if (HowLong > 255) /* 255 microseconds */
184db2bae30SDana Myers {
185db2bae30SDana Myers /*
186db2bae30SDana Myers * Longer than 255 usec, this is an error
187db2bae30SDana Myers *
188db2bae30SDana Myers * (ACPI specifies 100 usec as max, but this gives some slack in
189db2bae30SDana Myers * order to support existing BIOSs)
190db2bae30SDana Myers */
191*de5d74c2SJerry Jelinek ACPI_ERROR ((AE_INFO,
192*de5d74c2SJerry Jelinek "Time parameter is too large (%u)", HowLong));
193db2bae30SDana Myers Status = AE_AML_OPERAND_VALUE;
194db2bae30SDana Myers }
195db2bae30SDana Myers else
196db2bae30SDana Myers {
197db2bae30SDana Myers AcpiOsStall (HowLong);
198db2bae30SDana Myers }
199db2bae30SDana Myers
200db2bae30SDana Myers return (Status);
201db2bae30SDana Myers }
202db2bae30SDana Myers
203db2bae30SDana Myers
204db2bae30SDana Myers /*******************************************************************************
205db2bae30SDana Myers *
20626f3cdf0SGordon Ross * FUNCTION: AcpiExSystemDoSleep
207db2bae30SDana Myers *
20826f3cdf0SGordon Ross * PARAMETERS: HowLong - The amount of time to sleep,
209db2bae30SDana Myers * in milliseconds
210db2bae30SDana Myers *
211db2bae30SDana Myers * RETURN: None
212db2bae30SDana Myers *
21326f3cdf0SGordon Ross * DESCRIPTION: Sleep the running thread for specified amount of time.
214db2bae30SDana Myers *
215db2bae30SDana Myers ******************************************************************************/
216db2bae30SDana Myers
217db2bae30SDana Myers ACPI_STATUS
AcpiExSystemDoSleep(UINT64 HowLong)21826f3cdf0SGordon Ross AcpiExSystemDoSleep (
21926f3cdf0SGordon Ross UINT64 HowLong)
220db2bae30SDana Myers {
221db2bae30SDana Myers ACPI_FUNCTION_ENTRY ();
222db2bae30SDana Myers
223db2bae30SDana Myers
224db2bae30SDana Myers /* Since this thread will sleep, we must release the interpreter */
225db2bae30SDana Myers
226*de5d74c2SJerry Jelinek AcpiExExitInterpreter ();
227db2bae30SDana Myers
22826f3cdf0SGordon Ross /*
22926f3cdf0SGordon Ross * For compatibility with other ACPI implementations and to prevent
23026f3cdf0SGordon Ross * accidental deep sleeps, limit the sleep time to something reasonable.
23126f3cdf0SGordon Ross */
23226f3cdf0SGordon Ross if (HowLong > ACPI_MAX_SLEEP)
23326f3cdf0SGordon Ross {
23426f3cdf0SGordon Ross HowLong = ACPI_MAX_SLEEP;
23526f3cdf0SGordon Ross }
23626f3cdf0SGordon Ross
237db2bae30SDana Myers AcpiOsSleep (HowLong);
238db2bae30SDana Myers
239db2bae30SDana Myers /* And now we must get the interpreter again */
240db2bae30SDana Myers
241*de5d74c2SJerry Jelinek AcpiExEnterInterpreter ();
242db2bae30SDana Myers return (AE_OK);
243db2bae30SDana Myers }
244db2bae30SDana Myers
245db2bae30SDana Myers
246db2bae30SDana Myers /*******************************************************************************
247db2bae30SDana Myers *
248db2bae30SDana Myers * FUNCTION: AcpiExSystemSignalEvent
249db2bae30SDana Myers *
250db2bae30SDana Myers * PARAMETERS: ObjDesc - The object descriptor for this op
251db2bae30SDana Myers *
252db2bae30SDana Myers * RETURN: Status
253db2bae30SDana Myers *
254db2bae30SDana Myers * DESCRIPTION: Provides an access point to perform synchronization operations
255db2bae30SDana Myers * within the AML.
256db2bae30SDana Myers *
257db2bae30SDana Myers ******************************************************************************/
258db2bae30SDana Myers
259db2bae30SDana Myers ACPI_STATUS
AcpiExSystemSignalEvent(ACPI_OPERAND_OBJECT * ObjDesc)260db2bae30SDana Myers AcpiExSystemSignalEvent (
261db2bae30SDana Myers ACPI_OPERAND_OBJECT *ObjDesc)
262db2bae30SDana Myers {
263db2bae30SDana Myers ACPI_STATUS Status = AE_OK;
264db2bae30SDana Myers
265db2bae30SDana Myers
266db2bae30SDana Myers ACPI_FUNCTION_TRACE (ExSystemSignalEvent);
267db2bae30SDana Myers
268db2bae30SDana Myers
269db2bae30SDana Myers if (ObjDesc)
270db2bae30SDana Myers {
271db2bae30SDana Myers Status = AcpiOsSignalSemaphore (ObjDesc->Event.OsSemaphore, 1);
272db2bae30SDana Myers }
273db2bae30SDana Myers
274db2bae30SDana Myers return_ACPI_STATUS (Status);
275db2bae30SDana Myers }
276db2bae30SDana Myers
277db2bae30SDana Myers
278db2bae30SDana Myers /*******************************************************************************
279db2bae30SDana Myers *
280db2bae30SDana Myers * FUNCTION: AcpiExSystemWaitEvent
281db2bae30SDana Myers *
282db2bae30SDana Myers * PARAMETERS: TimeDesc - The 'time to delay' object descriptor
283db2bae30SDana Myers * ObjDesc - The object descriptor for this op
284db2bae30SDana Myers *
285db2bae30SDana Myers * RETURN: Status
286db2bae30SDana Myers *
287db2bae30SDana Myers * DESCRIPTION: Provides an access point to perform synchronization operations
288db2bae30SDana Myers * within the AML. This operation is a request to wait for an
289db2bae30SDana Myers * event.
290db2bae30SDana Myers *
291db2bae30SDana Myers ******************************************************************************/
292db2bae30SDana Myers
293db2bae30SDana Myers ACPI_STATUS
AcpiExSystemWaitEvent(ACPI_OPERAND_OBJECT * TimeDesc,ACPI_OPERAND_OBJECT * ObjDesc)294db2bae30SDana Myers AcpiExSystemWaitEvent (
295db2bae30SDana Myers ACPI_OPERAND_OBJECT *TimeDesc,
296db2bae30SDana Myers ACPI_OPERAND_OBJECT *ObjDesc)
297db2bae30SDana Myers {
298db2bae30SDana Myers ACPI_STATUS Status = AE_OK;
299db2bae30SDana Myers
300db2bae30SDana Myers
301db2bae30SDana Myers ACPI_FUNCTION_TRACE (ExSystemWaitEvent);
302db2bae30SDana Myers
303db2bae30SDana Myers
304db2bae30SDana Myers if (ObjDesc)
305db2bae30SDana Myers {
306db2bae30SDana Myers Status = AcpiExSystemWaitSemaphore (ObjDesc->Event.OsSemaphore,
307db2bae30SDana Myers (UINT16) TimeDesc->Integer.Value);
308db2bae30SDana Myers }
309db2bae30SDana Myers
310db2bae30SDana Myers return_ACPI_STATUS (Status);
311db2bae30SDana Myers }
312db2bae30SDana Myers
313db2bae30SDana Myers
314db2bae30SDana Myers /*******************************************************************************
315db2bae30SDana Myers *
316db2bae30SDana Myers * FUNCTION: AcpiExSystemResetEvent
317db2bae30SDana Myers *
318db2bae30SDana Myers * PARAMETERS: ObjDesc - The object descriptor for this op
319db2bae30SDana Myers *
320db2bae30SDana Myers * RETURN: Status
321db2bae30SDana Myers *
322db2bae30SDana Myers * DESCRIPTION: Reset an event to a known state.
323db2bae30SDana Myers *
324db2bae30SDana Myers ******************************************************************************/
325db2bae30SDana Myers
326db2bae30SDana Myers ACPI_STATUS
AcpiExSystemResetEvent(ACPI_OPERAND_OBJECT * ObjDesc)327db2bae30SDana Myers AcpiExSystemResetEvent (
328db2bae30SDana Myers ACPI_OPERAND_OBJECT *ObjDesc)
329db2bae30SDana Myers {
330db2bae30SDana Myers ACPI_STATUS Status = AE_OK;
331db2bae30SDana Myers ACPI_SEMAPHORE TempSemaphore;
332db2bae30SDana Myers
333db2bae30SDana Myers
334db2bae30SDana Myers ACPI_FUNCTION_ENTRY ();
335db2bae30SDana Myers
336db2bae30SDana Myers
337db2bae30SDana Myers /*
338db2bae30SDana Myers * We are going to simply delete the existing semaphore and
339db2bae30SDana Myers * create a new one!
340db2bae30SDana Myers */
341db2bae30SDana Myers Status = AcpiOsCreateSemaphore (ACPI_NO_UNIT_LIMIT, 0, &TempSemaphore);
342db2bae30SDana Myers if (ACPI_SUCCESS (Status))
343db2bae30SDana Myers {
344db2bae30SDana Myers (void) AcpiOsDeleteSemaphore (ObjDesc->Event.OsSemaphore);
345db2bae30SDana Myers ObjDesc->Event.OsSemaphore = TempSemaphore;
346db2bae30SDana Myers }
347db2bae30SDana Myers
348db2bae30SDana Myers return (Status);
349db2bae30SDana Myers }
350