evmisc.c revision 129684
1/******************************************************************************
2 *
3 * Module Name: evmisc - Miscellaneous event manager support functions
4 *              $Revision: 75 $
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2004, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights.  You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
21 *
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
28 *
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code.  No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision.  In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change.  Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee.  Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
54 *
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution.  In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
66 *
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government.  In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117#include "acpi.h"
118#include "acevents.h"
119#include "acnamesp.h"
120#include "acinterp.h"
121
122#define _COMPONENT          ACPI_EVENTS
123        ACPI_MODULE_NAME    ("evmisc")
124
125
126/*******************************************************************************
127 *
128 * FUNCTION:    AcpiEvIsNotifyObject
129 *
130 * PARAMETERS:  Node            - Node to check
131 *
132 * RETURN:      TRUE if notifies allowed on this object
133 *
134 * DESCRIPTION: Check type of node for a object that supports notifies.
135 *
136 *              TBD: This could be replaced by a flag bit in the node.
137 *
138 ******************************************************************************/
139
140BOOLEAN
141AcpiEvIsNotifyObject (
142    ACPI_NAMESPACE_NODE     *Node)
143{
144    switch (Node->Type)
145    {
146    case ACPI_TYPE_DEVICE:
147    case ACPI_TYPE_PROCESSOR:
148    case ACPI_TYPE_POWER:
149    case ACPI_TYPE_THERMAL:
150        /*
151         * These are the ONLY objects that can receive ACPI notifications
152         */
153        return (TRUE);
154
155    default:
156        return (FALSE);
157    }
158}
159
160
161/*******************************************************************************
162 *
163 * FUNCTION:    AcpiEvQueueNotifyRequest
164 *
165 * PARAMETERS:
166 *
167 * RETURN:      None.
168 *
169 * DESCRIPTION: Dispatch a device notification event to a previously
170 *              installed handler.
171 *
172 ******************************************************************************/
173
174#ifdef ACPI_DEBUG_OUTPUT
175static const char        *AcpiNotifyValueNames[] =
176{
177    "Bus Check",
178    "Device Check",
179    "Device Wake",
180    "Eject request",
181    "Device Check Light",
182    "Frequency Mismatch",
183    "Bus Mode Mismatch",
184    "Power Fault"
185};
186#endif
187
188ACPI_STATUS
189AcpiEvQueueNotifyRequest (
190    ACPI_NAMESPACE_NODE     *Node,
191    UINT32                  NotifyValue)
192{
193    ACPI_OPERAND_OBJECT     *ObjDesc;
194    ACPI_OPERAND_OBJECT     *HandlerObj = NULL;
195    ACPI_GENERIC_STATE      *NotifyInfo;
196    ACPI_STATUS             Status = AE_OK;
197
198
199    ACPI_FUNCTION_NAME ("EvQueueNotifyRequest");
200
201
202    /*
203     * For value 3 (Ejection Request), some device method may need to be run.
204     * For value 2 (Device Wake) if _PRW exists, the _PS0 method may need to be run.
205     * For value 0x80 (Status Change) on the power button or sleep button,
206     * initiate soft-off or sleep operation?
207     */
208    ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
209        "Dispatching Notify(%X) on node %p\n", NotifyValue, Node));
210
211    if (NotifyValue <= 7)
212    {
213        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Notify value: %s\n",
214                AcpiNotifyValueNames[NotifyValue]));
215    }
216    else
217    {
218        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Notify value: 0x%2.2X **Device Specific**\n",
219                NotifyValue));
220    }
221
222    /*
223     * Get the notify object attached to the NS Node
224     */
225    ObjDesc = AcpiNsGetAttachedObject (Node);
226    if (ObjDesc)
227    {
228        /* We have the notify object, Get the right handler */
229
230        switch (Node->Type)
231        {
232        case ACPI_TYPE_DEVICE:
233        case ACPI_TYPE_THERMAL:
234        case ACPI_TYPE_PROCESSOR:
235        case ACPI_TYPE_POWER:
236
237            if (NotifyValue <= ACPI_MAX_SYS_NOTIFY)
238            {
239                HandlerObj = ObjDesc->CommonNotify.SystemNotify;
240            }
241            else
242            {
243                HandlerObj = ObjDesc->CommonNotify.DeviceNotify;
244            }
245            break;
246
247        default:
248            /* All other types are not supported */
249            return (AE_TYPE);
250        }
251    }
252
253    /* If there is any handler to run, schedule the dispatcher */
254
255    if ((AcpiGbl_SystemNotify.Handler && (NotifyValue <= ACPI_MAX_SYS_NOTIFY)) ||
256        (AcpiGbl_DeviceNotify.Handler && (NotifyValue > ACPI_MAX_SYS_NOTIFY))  ||
257        HandlerObj)
258    {
259        NotifyInfo = AcpiUtCreateGenericState ();
260        if (!NotifyInfo)
261        {
262            return (AE_NO_MEMORY);
263        }
264
265        NotifyInfo->Common.DataType   = ACPI_DESC_TYPE_STATE_NOTIFY;
266        NotifyInfo->Notify.Node       = Node;
267        NotifyInfo->Notify.Value      = (UINT16) NotifyValue;
268        NotifyInfo->Notify.HandlerObj = HandlerObj;
269
270        Status = AcpiOsQueueForExecution (OSD_PRIORITY_HIGH,
271                        AcpiEvNotifyDispatch, NotifyInfo);
272        if (ACPI_FAILURE (Status))
273        {
274            AcpiUtDeleteGenericState (NotifyInfo);
275        }
276    }
277
278    if (!HandlerObj)
279    {
280        /* There is no per-device notify handler for this device */
281
282        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
283            "No notify handler for Notify(%4.4s, %X) node %p\n",
284            AcpiUtGetNodeName (Node), NotifyValue, Node));
285    }
286
287    return (Status);
288}
289
290
291/*******************************************************************************
292 *
293 * FUNCTION:    AcpiEvNotifyDispatch
294 *
295 * PARAMETERS:
296 *
297 * RETURN:      None.
298 *
299 * DESCRIPTION: Dispatch a device notification event to a previously
300 *              installed handler.
301 *
302 ******************************************************************************/
303
304void ACPI_SYSTEM_XFACE
305AcpiEvNotifyDispatch (
306    void                    *Context)
307{
308    ACPI_GENERIC_STATE      *NotifyInfo = (ACPI_GENERIC_STATE *) Context;
309    ACPI_NOTIFY_HANDLER     GlobalHandler = NULL;
310    void                    *GlobalContext = NULL;
311    ACPI_OPERAND_OBJECT     *HandlerObj;
312
313
314    ACPI_FUNCTION_ENTRY ();
315
316
317    /*
318     * We will invoke a global notify handler if installed.
319     * This is done _before_ we invoke the per-device handler attached to the device.
320     */
321    if (NotifyInfo->Notify.Value <= ACPI_MAX_SYS_NOTIFY)
322    {
323        /* Global system notification handler */
324
325        if (AcpiGbl_SystemNotify.Handler)
326        {
327            GlobalHandler = AcpiGbl_SystemNotify.Handler;
328            GlobalContext = AcpiGbl_SystemNotify.Context;
329        }
330    }
331    else
332    {
333        /* Global driver notification handler */
334
335        if (AcpiGbl_DeviceNotify.Handler)
336        {
337            GlobalHandler = AcpiGbl_DeviceNotify.Handler;
338            GlobalContext = AcpiGbl_DeviceNotify.Context;
339        }
340    }
341
342    /* Invoke the system handler first, if present */
343
344    if (GlobalHandler)
345    {
346        GlobalHandler (NotifyInfo->Notify.Node, NotifyInfo->Notify.Value, GlobalContext);
347    }
348
349    /* Now invoke the per-device handler, if present */
350
351    HandlerObj = NotifyInfo->Notify.HandlerObj;
352    if (HandlerObj)
353    {
354        HandlerObj->Notify.Handler (NotifyInfo->Notify.Node, NotifyInfo->Notify.Value,
355                        HandlerObj->Notify.Context);
356    }
357
358    /* All done with the info object */
359
360    AcpiUtDeleteGenericState (NotifyInfo);
361}
362
363
364/*******************************************************************************
365 *
366 * FUNCTION:    AcpiEvGlobalLockThread
367 *
368 * RETURN:      None
369 *
370 * DESCRIPTION: Invoked by SCI interrupt handler upon acquisition of the
371 *              Global Lock.  Simply signal all threads that are waiting
372 *              for the lock.
373 *
374 ******************************************************************************/
375
376static void ACPI_SYSTEM_XFACE
377AcpiEvGlobalLockThread (
378    void                    *Context)
379{
380    ACPI_STATUS             Status;
381
382
383    /* Signal threads that are waiting for the lock */
384
385    if (AcpiGbl_GlobalLockThreadCount)
386    {
387        /* Send sufficient units to the semaphore */
388
389        Status = AcpiOsSignalSemaphore (AcpiGbl_GlobalLockSemaphore,
390                                AcpiGbl_GlobalLockThreadCount);
391        if (ACPI_FAILURE (Status))
392        {
393            ACPI_REPORT_ERROR (("Could not signal Global Lock semaphore\n"));
394        }
395    }
396}
397
398
399/*******************************************************************************
400 *
401 * FUNCTION:    AcpiEvGlobalLockHandler
402 *
403 * RETURN:      Status
404 *
405 * DESCRIPTION: Invoked directly from the SCI handler when a global lock
406 *              release interrupt occurs.  Grab the global lock and queue
407 *              the global lock thread for execution
408 *
409 ******************************************************************************/
410
411static UINT32
412AcpiEvGlobalLockHandler (
413    void                    *Context)
414{
415    BOOLEAN                 Acquired = FALSE;
416    ACPI_STATUS             Status;
417
418
419    /*
420     * Attempt to get the lock
421     * If we don't get it now, it will be marked pending and we will
422     * take another interrupt when it becomes free.
423     */
424    ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_CommonFACS.GlobalLock, Acquired);
425    if (Acquired)
426    {
427        /* Got the lock, now wake all threads waiting for it */
428
429        AcpiGbl_GlobalLockAcquired = TRUE;
430
431        /* Run the Global Lock thread which will signal all waiting threads */
432
433        Status = AcpiOsQueueForExecution (OSD_PRIORITY_HIGH,
434                        AcpiEvGlobalLockThread, Context);
435        if (ACPI_FAILURE (Status))
436        {
437            ACPI_REPORT_ERROR (("Could not queue Global Lock thread, %s\n",
438                AcpiFormatException (Status)));
439
440            return (ACPI_INTERRUPT_NOT_HANDLED);
441        }
442    }
443
444    return (ACPI_INTERRUPT_HANDLED);
445}
446
447
448/*******************************************************************************
449 *
450 * FUNCTION:    AcpiEvInitGlobalLockHandler
451 *
452 * RETURN:      Status
453 *
454 * DESCRIPTION: Install a handler for the global lock release event
455 *
456 ******************************************************************************/
457
458ACPI_STATUS
459AcpiEvInitGlobalLockHandler (void)
460{
461    ACPI_STATUS             Status;
462
463
464    ACPI_FUNCTION_TRACE ("EvInitGlobalLockHandler");
465
466
467    AcpiGbl_GlobalLockPresent = TRUE;
468    Status = AcpiInstallFixedEventHandler (ACPI_EVENT_GLOBAL,
469                                            AcpiEvGlobalLockHandler, NULL);
470
471    /*
472     * If the global lock does not exist on this platform, the attempt
473     * to enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick)
474     * Map to AE_OK, but mark global lock as not present.
475     * Any attempt to actually use the global lock will be flagged
476     * with an error.
477     */
478    if (Status == AE_NO_HARDWARE_RESPONSE)
479    {
480        AcpiGbl_GlobalLockPresent = FALSE;
481        Status = AE_OK;
482    }
483
484    return_ACPI_STATUS (Status);
485}
486
487
488/******************************************************************************
489 *
490 * FUNCTION:    AcpiEvAcquireGlobalLock
491 *
492 * RETURN:      Status
493 *
494 * DESCRIPTION: Attempt to gain ownership of the Global Lock.
495 *
496 *****************************************************************************/
497
498ACPI_STATUS
499AcpiEvAcquireGlobalLock (
500    UINT16                  Timeout)
501{
502    ACPI_STATUS             Status = AE_OK;
503    BOOLEAN                 Acquired = FALSE;
504
505
506    ACPI_FUNCTION_TRACE ("EvAcquireGlobalLock");
507
508
509#ifndef ACPI_APPLICATION
510    /* Make sure that we actually have a global lock */
511
512    if (!AcpiGbl_GlobalLockPresent)
513    {
514        return_ACPI_STATUS (AE_NO_GLOBAL_LOCK);
515    }
516#endif
517
518    /* One more thread wants the global lock */
519
520    AcpiGbl_GlobalLockThreadCount++;
521
522    /* If we (OS side vs. BIOS side) have the hardware lock already, we are done */
523
524    if (AcpiGbl_GlobalLockAcquired)
525    {
526        return_ACPI_STATUS (AE_OK);
527    }
528
529    /* We must acquire the actual hardware lock */
530
531    ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_CommonFACS.GlobalLock, Acquired);
532    if (Acquired)
533    {
534       /* We got the lock */
535
536        ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Acquired the HW Global Lock\n"));
537
538        AcpiGbl_GlobalLockAcquired = TRUE;
539        return_ACPI_STATUS (AE_OK);
540    }
541
542    /*
543     * Did not get the lock.  The pending bit was set above, and we must now
544     * wait until we get the global lock released interrupt.
545     */
546    ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Waiting for the HW Global Lock\n"));
547
548    /*
549     * Acquire the global lock semaphore first.
550     * Since this wait will block, we must release the interpreter
551     */
552    Status = AcpiExSystemWaitSemaphore (AcpiGbl_GlobalLockSemaphore,
553                                            Timeout);
554    return_ACPI_STATUS (Status);
555}
556
557
558/*******************************************************************************
559 *
560 * FUNCTION:    AcpiEvReleaseGlobalLock
561 *
562 * DESCRIPTION: Releases ownership of the Global Lock.
563 *
564 ******************************************************************************/
565
566ACPI_STATUS
567AcpiEvReleaseGlobalLock (void)
568{
569    BOOLEAN                 Pending = FALSE;
570    ACPI_STATUS             Status = AE_OK;
571
572
573    ACPI_FUNCTION_TRACE ("EvReleaseGlobalLock");
574
575
576    if (!AcpiGbl_GlobalLockThreadCount)
577    {
578        ACPI_REPORT_WARNING(("Cannot release HW Global Lock, it has not been acquired\n"));
579        return_ACPI_STATUS (AE_NOT_ACQUIRED);
580    }
581
582    /* One fewer thread has the global lock */
583
584    AcpiGbl_GlobalLockThreadCount--;
585    if (AcpiGbl_GlobalLockThreadCount)
586    {
587        /* There are still some threads holding the lock, cannot release */
588
589        return_ACPI_STATUS (AE_OK);
590    }
591
592    /*
593     * No more threads holding lock, we can do the actual hardware
594     * release
595     */
596    ACPI_RELEASE_GLOBAL_LOCK (AcpiGbl_CommonFACS.GlobalLock, Pending);
597    AcpiGbl_GlobalLockAcquired = FALSE;
598
599    /*
600     * If the pending bit was set, we must write GBL_RLS to the control
601     * register
602     */
603    if (Pending)
604    {
605        Status = AcpiSetRegister (ACPI_BITREG_GLOBAL_LOCK_RELEASE, 1, ACPI_MTX_LOCK);
606    }
607
608    return_ACPI_STATUS (Status);
609}
610
611
612/******************************************************************************
613 *
614 * FUNCTION:    AcpiEvTerminate
615 *
616 * PARAMETERS:  none
617 *
618 * RETURN:      none
619 *
620 * DESCRIPTION: Disable events and free memory allocated for table storage.
621 *
622 ******************************************************************************/
623
624void
625AcpiEvTerminate (void)
626{
627    ACPI_NATIVE_UINT        i;
628    ACPI_STATUS             Status;
629
630
631    ACPI_FUNCTION_TRACE ("EvTerminate");
632
633
634    if (AcpiGbl_EventsInitialized)
635    {
636        /*
637         * Disable all event-related functionality.
638         * In all cases, on error, print a message but obviously we don't abort.
639         */
640
641        /* Disable all fixed events */
642
643        for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++)
644        {
645            Status = AcpiDisableEvent ((UINT32) i, 0);
646            if (ACPI_FAILURE (Status))
647            {
648                ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not disable fixed event %d\n", (UINT32) i));
649            }
650        }
651
652        /* Disable all GPEs in all GPE blocks */
653
654        Status = AcpiEvWalkGpeList (AcpiHwDisableGpeBlock);
655
656        /* Remove SCI handler */
657
658        Status = AcpiEvRemoveSciHandler ();
659        if (ACPI_FAILURE(Status))
660        {
661            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not remove SCI handler\n"));
662        }
663    }
664
665    /* Deallocate all handler objects installed within GPE info structs */
666
667    Status = AcpiEvWalkGpeList (AcpiEvDeleteGpeHandlers);
668
669    /* Return to original mode if necessary */
670
671    if (AcpiGbl_OriginalMode == ACPI_SYS_MODE_LEGACY)
672    {
673        Status = AcpiDisable ();
674        if (ACPI_FAILURE (Status))
675        {
676            ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "AcpiDisable failed\n"));
677        }
678    }
679    return_VOID;
680}
681
682