Debian Codegraph @

source/components/events/evglock.c

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Module Name: evglock - Global Lock support
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2026, 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 MERCHANTABILITY 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
57 AcpiEvGlobalLockHandler (
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
73 ACPI_STATUS
74 AcpiEvInitGlobalLockHandler (
75 void)
76 {
77 ACPI_STATUS Status;
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 {
87 return_ACPI_STATUS (AE_OK);
88 }
89
90 if (!AcpiGbl_UseGlobalLock)
91 {
92 return_ACPI_STATUS (AE_OK);
93 }
94
95 /* Attempt installation of the global lock handler */
96
97 Status = AcpiInstallFixedEventHandler (ACPI_EVENT_GLOBAL,
98 AcpiEvGlobalLockHandler, NULL);
99
100 /*
101 * If the global lock does not exist on this platform, the attempt to
102 * enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick).
103 * Map to AE_OK, but mark global lock as not present. Any attempt to
104 * actually use the global lock will be flagged with an error.
105 */
106 AcpiGbl_GlobalLockPresent = FALSE;
107 if (Status == AE_NO_HARDWARE_RESPONSE)
108 {
109 ACPI_ERROR ((AE_INFO,
110 "No response from Global Lock hardware, disabling lock"));
111
112 return_ACPI_STATUS (AE_OK);
113 }
114
115 Status = AcpiOsCreateLock (&AcpiGbl_GlobalLockPendingLock);
116 if (ACPI_FAILURE (Status))
117 {
118 return_ACPI_STATUS (Status);
119 }
120
121 AcpiGbl_GlobalLockPending = FALSE;
122 AcpiGbl_GlobalLockPresent = TRUE;
123 return_ACPI_STATUS (Status);
124 }
125
126
127 /*******************************************************************************
128 *
129 * FUNCTION: AcpiEvRemoveGlobalLockHandler
130 *
131 * PARAMETERS: None
132 *
133 * RETURN: Status
134 *
135 * DESCRIPTION: Remove the handler for the Global Lock
136 *
137 ******************************************************************************/
138
139 ACPI_STATUS
140 AcpiEvRemoveGlobalLockHandler (
141 void)
142 {
143 ACPI_STATUS Status;
144
145
146 ACPI_FUNCTION_TRACE (EvRemoveGlobalLockHandler);
147
148
149 AcpiGbl_GlobalLockPresent = FALSE;
150 Status = AcpiRemoveFixedEventHandler (ACPI_EVENT_GLOBAL,
151 AcpiEvGlobalLockHandler);
152
153 AcpiOsDeleteLock (AcpiGbl_GlobalLockPendingLock);
154 return_ACPI_STATUS (Status);
155 }
156
157
158 /*******************************************************************************
159 *
160 * FUNCTION: AcpiEvGlobalLockHandler
161 *
162 * PARAMETERS: Context - From thread interface, not used
163 *
164 * RETURN: ACPI_INTERRUPT_HANDLED
165 *
166 * DESCRIPTION: Invoked directly from the SCI handler when a global lock
167 * release interrupt occurs. If there is actually a pending
168 * request for the lock, signal the waiting thread.
169 *
170 ******************************************************************************/
171
172 static UINT32
173 AcpiEvGlobalLockHandler (
174 void *Context)
175 {
176 ACPI_STATUS Status;
177 ACPI_CPU_FLAGS Flags;
178
179
180 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
181
182 /*
183 * If a request for the global lock is not actually pending,
184 * we are done. This handles "spurious" global lock interrupts
185 * which are possible (and have been seen) with bad BIOSs.
186 */
187 if (!AcpiGbl_GlobalLockPending)
188 {
189 goto CleanupAndExit;
190 }
191
192 /*
193 * Send a unit to the global lock semaphore. The actual acquisition
194 * of the global lock will be performed by the waiting thread.
195 */
196 Status = AcpiOsSignalSemaphore (AcpiGbl_GlobalLockSemaphore, 1);
197 if (ACPI_FAILURE (Status))
198 {
199 ACPI_ERROR ((AE_INFO, "Could not signal Global Lock semaphore"));
200 }
201
202 AcpiGbl_GlobalLockPending = FALSE;
203
204
205 CleanupAndExit:
206
207 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
208 return (ACPI_INTERRUPT_HANDLED);
209 }
210
211
212 /******************************************************************************
213 *
214 * FUNCTION: AcpiEvAcquireGlobalLock
215 *
216 * PARAMETERS: Timeout - Max time to wait for the lock, in millisec.
217 *
218 * RETURN: Status
219 *
220 * DESCRIPTION: Attempt to gain ownership of the Global Lock.
221 *
222 * MUTEX: Interpreter must be locked
223 *
224 * Note: The original implementation allowed multiple threads to "acquire" the
225 * Global Lock, and the OS would hold the lock until the last thread had
226 * released it. However, this could potentially starve the BIOS out of the
227 * lock, especially in the case where there is a tight handshake between the
228 * Embedded Controller driver and the BIOS. Therefore, this implementation
229 * allows only one thread to acquire the HW Global Lock at a time, and makes
230 * the global lock appear as a standard mutex on the OS side.
231 *
232 *****************************************************************************/
233
234 ACPI_STATUS
235 AcpiEvAcquireGlobalLock (
236 UINT16 Timeout)
237 {
238 ACPI_CPU_FLAGS Flags;
239 ACPI_STATUS Status;
240 BOOLEAN Acquired = FALSE;
241
242
243 ACPI_FUNCTION_TRACE (EvAcquireGlobalLock);
244
245
246 /*
247 * Only one thread can acquire the GL at a time, the GlobalLockMutex
248 * enforces this. This interface releases the interpreter if we must wait.
249 */
250 Status = AcpiExSystemWaitMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex,
251 Timeout);
252 if (ACPI_FAILURE (Status))
253 {
254 return_ACPI_STATUS (Status);
255 }
256
257 /*
258 * Update the global lock handle and check for wraparound. The handle is
259 * only used for the external global lock interfaces, but it is updated
260 * here to properly handle the case where a single thread may acquire the
261 * lock via both the AML and the AcpiAcquireGlobalLock interfaces. The
262 * handle is therefore updated on the first acquire from a given thread
263 * regardless of where the acquisition request originated.
264 */
265 AcpiGbl_GlobalLockHandle++;
266 if (AcpiGbl_GlobalLockHandle == 0)
267 {
268 AcpiGbl_GlobalLockHandle = 1;
269 }
270
271 /*
272 * Make sure that a global lock actually exists. If not, just
273 * treat the lock as a standard mutex.
274 */
275 if (!AcpiGbl_GlobalLockPresent)
276 {
277 AcpiGbl_GlobalLockAcquired = TRUE;
278 return_ACPI_STATUS (AE_OK);
279 }
280
281 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
282
283 do
284 {
285 /* Attempt to acquire the actual hardware lock */
286
287 ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_FACS, Acquired);
288 if (Acquired)
289 {
290 AcpiGbl_GlobalLockAcquired = TRUE;
291 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
292 "Acquired hardware Global Lock\n"));
293 break;
294 }
295
296 /*
297 * Did not get the lock. The pending bit was set above, and
298 * we must now wait until we receive the global lock
299 * released interrupt.
300 */
301 AcpiGbl_GlobalLockPending = TRUE;
302 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
303
304 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
305 "Waiting for hardware Global Lock\n"));
306
307 /*
308 * Wait for handshake with the global lock interrupt handler.
309 * This interface releases the interpreter if we must wait.
310 */
311 Status = AcpiExSystemWaitSemaphore (
312 AcpiGbl_GlobalLockSemaphore, ACPI_WAIT_FOREVER);
313
314 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
315
316 } while (ACPI_SUCCESS (Status));
317
318 AcpiGbl_GlobalLockPending = FALSE;
319 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
320
321 return_ACPI_STATUS (Status);
322 }
323
324
325 /*******************************************************************************
326 *
327 * FUNCTION: AcpiEvReleaseGlobalLock
328 *
329 * PARAMETERS: None
330 *
331 * RETURN: Status
332 *
333 * DESCRIPTION: Releases ownership of the Global Lock.
334 *
335 ******************************************************************************/
336
337 ACPI_STATUS
338 AcpiEvReleaseGlobalLock (
339 void)
340 {
341 BOOLEAN Pending = FALSE;
342 ACPI_STATUS Status = AE_OK;
343
344
345 ACPI_FUNCTION_TRACE (EvReleaseGlobalLock);
346
347
348 /* Lock must be already acquired */
349
350 if (!AcpiGbl_GlobalLockAcquired)
351 {
352 ACPI_WARNING ((AE_INFO,
353 "Cannot release the ACPI Global Lock, it has not been acquired"));
354 return_ACPI_STATUS (AE_NOT_ACQUIRED);
355 }
356
357 if (AcpiGbl_GlobalLockPresent)
358 {
359 /* Allow any thread to release the lock */
360
361 ACPI_RELEASE_GLOBAL_LOCK (AcpiGbl_FACS, Pending);
362
363 /*
364 * If the pending bit was set, we must write GBL_RLS to the control
365 * register
366 */
367 if (Pending)
368 {
369 Status = AcpiWriteBitRegister (
370 ACPI_BITREG_GLOBAL_LOCK_RELEASE, ACPI_ENABLE_EVENT);
371 }
372
373 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Released hardware Global Lock\n"));
374 }
375
376 AcpiGbl_GlobalLockAcquired = FALSE;
377
378 /* Release the local GL mutex */
379
380 AcpiOsReleaseMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex);
381 return_ACPI_STATUS (Status);
382 }
383
384 #endif /* !ACPI_REDUCED_HARDWARE */