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