ReactOS  0.4.14-dev-358-gbef841c
evglock.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Module Name: evglock - Global Lock support
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2019, Intel Corp.
9  * 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 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acinterp.h"
48 
49 #define _COMPONENT ACPI_EVENTS
50  ACPI_MODULE_NAME ("evglock")
51 
52 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
53 
54 /* Local prototypes */
55 
56 static UINT32
58  void *Context);
59 
60 
61 /*******************************************************************************
62  *
63  * FUNCTION: AcpiEvInitGlobalLockHandler
64  *
65  * PARAMETERS: None
66  *
67  * RETURN: Status
68  *
69  * DESCRIPTION: Install a handler for the global lock release event
70  *
71  ******************************************************************************/
72 
75  void)
76 {
78 
79 
80  ACPI_FUNCTION_TRACE (EvInitGlobalLockHandler);
81 
82 
83  /* If Hardware Reduced flag is set, there is no global lock */
84 
85  if (AcpiGbl_ReducedHardware)
86  {
88  }
89 
90  /* Attempt installation of the global lock handler */
91 
94 
95  /*
96  * If the global lock does not exist on this platform, the attempt to
97  * enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick).
98  * Map to AE_OK, but mark global lock as not present. Any attempt to
99  * actually use the global lock will be flagged with an error.
100  */
101  AcpiGbl_GlobalLockPresent = FALSE;
103  {
105  "No response from Global Lock hardware, disabling lock"));
106 
108  }
109 
110  Status = AcpiOsCreateLock (&AcpiGbl_GlobalLockPendingLock);
111  if (ACPI_FAILURE (Status))
112  {
114  }
115 
116  AcpiGbl_GlobalLockPending = FALSE;
117  AcpiGbl_GlobalLockPresent = TRUE;
119 }
120 
121 
122 /*******************************************************************************
123  *
124  * FUNCTION: AcpiEvRemoveGlobalLockHandler
125  *
126  * PARAMETERS: None
127  *
128  * RETURN: Status
129  *
130  * DESCRIPTION: Remove the handler for the Global Lock
131  *
132  ******************************************************************************/
133 
136  void)
137 {
139 
140 
141  ACPI_FUNCTION_TRACE (EvRemoveGlobalLockHandler);
142 
143 
144  AcpiGbl_GlobalLockPresent = FALSE;
147 
148  AcpiOsDeleteLock (AcpiGbl_GlobalLockPendingLock);
150 }
151 
152 
153 /*******************************************************************************
154  *
155  * FUNCTION: AcpiEvGlobalLockHandler
156  *
157  * PARAMETERS: Context - From thread interface, not used
158  *
159  * RETURN: ACPI_INTERRUPT_HANDLED
160  *
161  * DESCRIPTION: Invoked directly from the SCI handler when a global lock
162  * release interrupt occurs. If there is actually a pending
163  * request for the lock, signal the waiting thread.
164  *
165  ******************************************************************************/
166 
167 static UINT32
169  void *Context)
170 {
173 
174 
175  Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
176 
177  /*
178  * If a request for the global lock is not actually pending,
179  * we are done. This handles "spurious" global lock interrupts
180  * which are possible (and have been seen) with bad BIOSs.
181  */
182  if (!AcpiGbl_GlobalLockPending)
183  {
184  goto CleanupAndExit;
185  }
186 
187  /*
188  * Send a unit to the global lock semaphore. The actual acquisition
189  * of the global lock will be performed by the waiting thread.
190  */
191  Status = AcpiOsSignalSemaphore (AcpiGbl_GlobalLockSemaphore, 1);
192  if (ACPI_FAILURE (Status))
193  {
194  ACPI_ERROR ((AE_INFO, "Could not signal Global Lock semaphore"));
195  }
196 
197  AcpiGbl_GlobalLockPending = FALSE;
198 
199 
200 CleanupAndExit:
201 
202  AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
203  return (ACPI_INTERRUPT_HANDLED);
204 }
205 
206 
207 /******************************************************************************
208  *
209  * FUNCTION: AcpiEvAcquireGlobalLock
210  *
211  * PARAMETERS: Timeout - Max time to wait for the lock, in millisec.
212  *
213  * RETURN: Status
214  *
215  * DESCRIPTION: Attempt to gain ownership of the Global Lock.
216  *
217  * MUTEX: Interpreter must be locked
218  *
219  * Note: The original implementation allowed multiple threads to "acquire" the
220  * Global Lock, and the OS would hold the lock until the last thread had
221  * released it. However, this could potentially starve the BIOS out of the
222  * lock, especially in the case where there is a tight handshake between the
223  * Embedded Controller driver and the BIOS. Therefore, this implementation
224  * allows only one thread to acquire the HW Global Lock at a time, and makes
225  * the global lock appear as a standard mutex on the OS side.
226  *
227  *****************************************************************************/
228 
231  UINT16 Timeout)
232 {
235  BOOLEAN Acquired = FALSE;
236 
237 
238  ACPI_FUNCTION_TRACE (EvAcquireGlobalLock);
239 
240 
241  /*
242  * Only one thread can acquire the GL at a time, the GlobalLockMutex
243  * enforces this. This interface releases the interpreter if we must wait.
244  */
245  Status = AcpiExSystemWaitMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex,
246  Timeout);
247  if (ACPI_FAILURE (Status))
248  {
250  }
251 
252  /*
253  * Update the global lock handle and check for wraparound. The handle is
254  * only used for the external global lock interfaces, but it is updated
255  * here to properly handle the case where a single thread may acquire the
256  * lock via both the AML and the AcpiAcquireGlobalLock interfaces. The
257  * handle is therefore updated on the first acquire from a given thread
258  * regardless of where the acquisition request originated.
259  */
260  AcpiGbl_GlobalLockHandle++;
261  if (AcpiGbl_GlobalLockHandle == 0)
262  {
263  AcpiGbl_GlobalLockHandle = 1;
264  }
265 
266  /*
267  * Make sure that a global lock actually exists. If not, just
268  * treat the lock as a standard mutex.
269  */
270  if (!AcpiGbl_GlobalLockPresent)
271  {
272  AcpiGbl_GlobalLockAcquired = TRUE;
274  }
275 
276  Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
277 
278  do
279  {
280  /* Attempt to acquire the actual hardware lock */
281 
282  ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_FACS, Acquired);
283  if (Acquired)
284  {
285  AcpiGbl_GlobalLockAcquired = TRUE;
287  "Acquired hardware Global Lock\n"));
288  break;
289  }
290 
291  /*
292  * Did not get the lock. The pending bit was set above, and
293  * we must now wait until we receive the global lock
294  * released interrupt.
295  */
296  AcpiGbl_GlobalLockPending = TRUE;
297  AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
298 
300  "Waiting for hardware Global Lock\n"));
301 
302  /*
303  * Wait for handshake with the global lock interrupt handler.
304  * This interface releases the interpreter if we must wait.
305  */
307  AcpiGbl_GlobalLockSemaphore, ACPI_WAIT_FOREVER);
308 
309  Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
310 
311  } while (ACPI_SUCCESS (Status));
312 
313  AcpiGbl_GlobalLockPending = FALSE;
314  AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
315 
317 }
318 
319 
320 /*******************************************************************************
321  *
322  * FUNCTION: AcpiEvReleaseGlobalLock
323  *
324  * PARAMETERS: None
325  *
326  * RETURN: Status
327  *
328  * DESCRIPTION: Releases ownership of the Global Lock.
329  *
330  ******************************************************************************/
331 
334  void)
335 {
338 
339 
340  ACPI_FUNCTION_TRACE (EvReleaseGlobalLock);
341 
342 
343  /* Lock must be already acquired */
344 
345  if (!AcpiGbl_GlobalLockAcquired)
346  {
348  "Cannot release the ACPI Global Lock, it has not been acquired"));
350  }
351 
352  if (AcpiGbl_GlobalLockPresent)
353  {
354  /* Allow any thread to release the lock */
355 
356  ACPI_RELEASE_GLOBAL_LOCK (AcpiGbl_FACS, Pending);
357 
358  /*
359  * If the pending bit was set, we must write GBL_RLS to the control
360  * register
361  */
362  if (Pending)
363  {
366  }
367 
368  ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Released hardware Global Lock\n"));
369  }
370 
371  AcpiGbl_GlobalLockAcquired = FALSE;
372 
373  /* Release the local GL mutex */
374 
375  AcpiOsReleaseMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex);
377 }
378 
379 #endif /* !ACPI_REDUCED_HARDWARE */
ACPI_STATUS AcpiEvAcquireGlobalLock(UINT16 Timeout)
Definition: evglock.c:230
#define AE_NOT_ACQUIRED
Definition: acexcep.h:128
#define TRUE
Definition: types.h:120
ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
Definition: osl.c:498
#define ACPI_SUCCESS(a)
Definition: acexcep.h:94
#define ACPI_EVENT_GLOBAL
Definition: actypes.h:755
ACPI_STATUS AcpiWriteBitRegister(UINT32 RegisterId, UINT32 Value)
Definition: hwxface.c:282
#define ACPI_WARNING(plist)
Definition: acoutput.h:238
ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
Definition: osl.c:439
ACPI_STATUS AcpiInstallFixedEventHandler(UINT32 Event, ACPI_EVENT_HANDLER Handler, void *Context)
Definition: evxface.c:707
ACPI_STATUS AcpiEvRemoveGlobalLockHandler(void)
Definition: evglock.c:135
#define ACPI_CPU_FLAGS
Definition: actypes.h:252
#define ACPI_BITREG_GLOBAL_LOCK_RELEASE
Definition: actypes.h:914
UINT32 ACPI_STATUS
Definition: actypes.h:460
ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
Definition: osl.c:463
#define ACPI_ENABLE_EVENT
Definition: actypes.h:932
ACPI_STATUS AcpiEvInitGlobalLockHandler(void)
Definition: evglock.c:74
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define ACPI_FAILURE(a)
Definition: acexcep.h:95
#define ACPI_ACQUIRE_GLOBAL_LOCK(GLptr, Acq)
Definition: accygwin.h:82
unsigned int UINT32
ACPI_STATUS AcpiExSystemWaitMutex(ACPI_MUTEX Mutex, UINT16 Timeout)
Definition: exsystem.c:120
#define ACPI_MODULE_NAME(Name)
Definition: acoutput.h:216
ACPI_STATUS AcpiRemoveFixedEventHandler(UINT32 Event, ACPI_EVENT_HANDLER Handler)
Definition: evxface.c:786
unsigned char BOOLEAN
#define AE_INFO
Definition: acoutput.h:230
smooth NULL
Definition: ftsmooth.c:416
ACPI_STATUS AcpiExSystemWaitSemaphore(ACPI_SEMAPHORE Semaphore, UINT16 Timeout)
Definition: exsystem.c:68
#define ACPI_INTERRUPT_HANDLED
Definition: actypes.h:1239
void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
Definition: osl.c:485
#define ACPI_WAIT_FOREVER
Definition: actypes.h:501
#define AE_NO_HARDWARE_RESPONSE
Definition: acexcep.h:130
#define ACPI_DEBUG_PRINT(pl)
Definition: acoutput.h:475
static UINT32 AcpiEvGlobalLockHandler(void *Context)
Definition: evglock.c:168
Status
Definition: gdiplustypes.h:24
#define ACPI_DB_EXEC
Definition: acoutput.h:165
static ULONG Timeout
Definition: ping.c:61
#define return_ACPI_STATUS(s)
Definition: acoutput.h:496
#define ACPI_FUNCTION_TRACE(a)
Definition: acoutput.h:480
#define ACPI_RELEASE_GLOBAL_LOCK(GLptr, Pending)
Definition: accygwin.h:83
unsigned short UINT16
ACPI_STATUS AcpiEvReleaseGlobalLock(void)
Definition: evglock.c:333
#define ACPI_ERROR(plist)
Definition: acoutput.h:240
void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
Definition: osl.c:516
void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
Definition: osl.c:333
#define AE_OK
Definition: acexcep.h:97