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