1/******************************************************************************
2 *
3 * Module Name: evgpeblk - GPE block creation and initialization.
4 *
5 *****************************************************************************/
6
7/******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2012, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116#include "acpi.h"
117#include "accommon.h"
118#include "acevents.h"
119#include "acnamesp.h"
120
121#define _COMPONENT          ACPI_EVENTS
122        ACPI_MODULE_NAME    ("evgpeblk")
123
124#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
125
126/* Local prototypes */
127
128static ACPI_STATUS
129AcpiEvInstallGpeBlock (
130    ACPI_GPE_BLOCK_INFO     *GpeBlock,
131    UINT32                  InterruptNumber);
132
133static ACPI_STATUS
134AcpiEvCreateGpeInfoBlocks (
135    ACPI_GPE_BLOCK_INFO     *GpeBlock);
136
137
138/*******************************************************************************
139 *
140 * FUNCTION:    AcpiEvInstallGpeBlock
141 *
142 * PARAMETERS:  GpeBlock                - New GPE block
143 *              InterruptNumber         - Xrupt to be associated with this
144 *                                        GPE block
145 *
146 * RETURN:      Status
147 *
148 * DESCRIPTION: Install new GPE block with mutex support
149 *
150 ******************************************************************************/
151
152static ACPI_STATUS
153AcpiEvInstallGpeBlock (
154    ACPI_GPE_BLOCK_INFO     *GpeBlock,
155    UINT32                  InterruptNumber)
156{
157    ACPI_GPE_BLOCK_INFO     *NextGpeBlock;
158    ACPI_GPE_XRUPT_INFO     *GpeXruptBlock;
159    ACPI_STATUS             Status;
160    ACPI_CPU_FLAGS          Flags;
161
162
163    ACPI_FUNCTION_TRACE (EvInstallGpeBlock);
164
165
166    Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
167    if (ACPI_FAILURE (Status))
168    {
169        return_ACPI_STATUS (Status);
170    }
171
172    GpeXruptBlock = AcpiEvGetGpeXruptBlock (InterruptNumber);
173    if (!GpeXruptBlock)
174    {
175        Status = AE_NO_MEMORY;
176        goto UnlockAndExit;
177    }
178
179    /* Install the new block at the end of the list with lock */
180
181    Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
182    if (GpeXruptBlock->GpeBlockListHead)
183    {
184        NextGpeBlock = GpeXruptBlock->GpeBlockListHead;
185        while (NextGpeBlock->Next)
186        {
187            NextGpeBlock = NextGpeBlock->Next;
188        }
189
190        NextGpeBlock->Next = GpeBlock;
191        GpeBlock->Previous = NextGpeBlock;
192    }
193    else
194    {
195        GpeXruptBlock->GpeBlockListHead = GpeBlock;
196    }
197
198    GpeBlock->XruptBlock = GpeXruptBlock;
199    AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
200
201
202UnlockAndExit:
203    Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
204    return_ACPI_STATUS (Status);
205}
206
207
208/*******************************************************************************
209 *
210 * FUNCTION:    AcpiEvDeleteGpeBlock
211 *
212 * PARAMETERS:  GpeBlock            - Existing GPE block
213 *
214 * RETURN:      Status
215 *
216 * DESCRIPTION: Remove a GPE block
217 *
218 ******************************************************************************/
219
220ACPI_STATUS
221AcpiEvDeleteGpeBlock (
222    ACPI_GPE_BLOCK_INFO     *GpeBlock)
223{
224    ACPI_STATUS             Status;
225    ACPI_CPU_FLAGS          Flags;
226
227
228    ACPI_FUNCTION_TRACE (EvInstallGpeBlock);
229
230
231    Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
232    if (ACPI_FAILURE (Status))
233    {
234        return_ACPI_STATUS (Status);
235    }
236
237    /* Disable all GPEs in this block */
238
239    Status = AcpiHwDisableGpeBlock (GpeBlock->XruptBlock, GpeBlock, NULL);
240
241    if (!GpeBlock->Previous && !GpeBlock->Next)
242    {
243        /* This is the last GpeBlock on this interrupt */
244
245        Status = AcpiEvDeleteGpeXrupt (GpeBlock->XruptBlock);
246        if (ACPI_FAILURE (Status))
247        {
248            goto UnlockAndExit;
249        }
250    }
251    else
252    {
253        /* Remove the block on this interrupt with lock */
254
255        Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
256        if (GpeBlock->Previous)
257        {
258            GpeBlock->Previous->Next = GpeBlock->Next;
259        }
260        else
261        {
262            GpeBlock->XruptBlock->GpeBlockListHead = GpeBlock->Next;
263        }
264
265        if (GpeBlock->Next)
266        {
267            GpeBlock->Next->Previous = GpeBlock->Previous;
268        }
269        AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
270    }
271
272    AcpiCurrentGpeCount -= GpeBlock->GpeCount;
273
274    /* Free the GpeBlock */
275
276    ACPI_FREE (GpeBlock->RegisterInfo);
277    ACPI_FREE (GpeBlock->EventInfo);
278    ACPI_FREE (GpeBlock);
279
280UnlockAndExit:
281    Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
282    return_ACPI_STATUS (Status);
283}
284
285
286/*******************************************************************************
287 *
288 * FUNCTION:    AcpiEvCreateGpeInfoBlocks
289 *
290 * PARAMETERS:  GpeBlock    - New GPE block
291 *
292 * RETURN:      Status
293 *
294 * DESCRIPTION: Create the RegisterInfo and EventInfo blocks for this GPE block
295 *
296 ******************************************************************************/
297
298static ACPI_STATUS
299AcpiEvCreateGpeInfoBlocks (
300    ACPI_GPE_BLOCK_INFO     *GpeBlock)
301{
302    ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo = NULL;
303    ACPI_GPE_EVENT_INFO     *GpeEventInfo = NULL;
304    ACPI_GPE_EVENT_INFO     *ThisEvent;
305    ACPI_GPE_REGISTER_INFO  *ThisRegister;
306    UINT32                  i;
307    UINT32                  j;
308    ACPI_STATUS             Status;
309
310
311    ACPI_FUNCTION_TRACE (EvCreateGpeInfoBlocks);
312
313
314    /* Allocate the GPE register information block */
315
316    GpeRegisterInfo = ACPI_ALLOCATE_ZEROED (
317                        (ACPI_SIZE) GpeBlock->RegisterCount *
318                        sizeof (ACPI_GPE_REGISTER_INFO));
319    if (!GpeRegisterInfo)
320    {
321        ACPI_ERROR ((AE_INFO,
322            "Could not allocate the GpeRegisterInfo table"));
323        return_ACPI_STATUS (AE_NO_MEMORY);
324    }
325
326    /*
327     * Allocate the GPE EventInfo block. There are eight distinct GPEs
328     * per register. Initialization to zeros is sufficient.
329     */
330    GpeEventInfo = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) GpeBlock->GpeCount *
331                    sizeof (ACPI_GPE_EVENT_INFO));
332    if (!GpeEventInfo)
333    {
334        ACPI_ERROR ((AE_INFO,
335            "Could not allocate the GpeEventInfo table"));
336        Status = AE_NO_MEMORY;
337        goto ErrorExit;
338    }
339
340    /* Save the new Info arrays in the GPE block */
341
342    GpeBlock->RegisterInfo = GpeRegisterInfo;
343    GpeBlock->EventInfo    = GpeEventInfo;
344
345    /*
346     * Initialize the GPE Register and Event structures. A goal of these
347     * tables is to hide the fact that there are two separate GPE register
348     * sets in a given GPE hardware block, the status registers occupy the
349     * first half, and the enable registers occupy the second half.
350     */
351    ThisRegister = GpeRegisterInfo;
352    ThisEvent    = GpeEventInfo;
353
354    for (i = 0; i < GpeBlock->RegisterCount; i++)
355    {
356        /* Init the RegisterInfo for this GPE register (8 GPEs) */
357
358        ThisRegister->BaseGpeNumber = (UINT8) (GpeBlock->BlockBaseNumber +
359                                             (i * ACPI_GPE_REGISTER_WIDTH));
360
361        ThisRegister->StatusAddress.Address =
362            GpeBlock->BlockAddress.Address + i;
363
364        ThisRegister->EnableAddress.Address =
365            GpeBlock->BlockAddress.Address + i + GpeBlock->RegisterCount;
366
367        ThisRegister->StatusAddress.SpaceId   = GpeBlock->BlockAddress.SpaceId;
368        ThisRegister->EnableAddress.SpaceId   = GpeBlock->BlockAddress.SpaceId;
369        ThisRegister->StatusAddress.BitWidth  = ACPI_GPE_REGISTER_WIDTH;
370        ThisRegister->EnableAddress.BitWidth  = ACPI_GPE_REGISTER_WIDTH;
371        ThisRegister->StatusAddress.BitOffset = 0;
372        ThisRegister->EnableAddress.BitOffset = 0;
373
374        /* Init the EventInfo for each GPE within this register */
375
376        for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
377        {
378            ThisEvent->GpeNumber = (UINT8) (ThisRegister->BaseGpeNumber + j);
379            ThisEvent->RegisterInfo = ThisRegister;
380            ThisEvent++;
381        }
382
383        /* Disable all GPEs within this register */
384
385        Status = AcpiHwWrite (0x00, &ThisRegister->EnableAddress);
386        if (ACPI_FAILURE (Status))
387        {
388            goto ErrorExit;
389        }
390
391        /* Clear any pending GPE events within this register */
392
393        Status = AcpiHwWrite (0xFF, &ThisRegister->StatusAddress);
394        if (ACPI_FAILURE (Status))
395        {
396            goto ErrorExit;
397        }
398
399        ThisRegister++;
400    }
401
402    return_ACPI_STATUS (AE_OK);
403
404
405ErrorExit:
406    if (GpeRegisterInfo)
407    {
408        ACPI_FREE (GpeRegisterInfo);
409    }
410    if (GpeEventInfo)
411    {
412        ACPI_FREE (GpeEventInfo);
413    }
414
415    return_ACPI_STATUS (Status);
416}
417
418
419/*******************************************************************************
420 *
421 * FUNCTION:    AcpiEvCreateGpeBlock
422 *
423 * PARAMETERS:  GpeDevice           - Handle to the parent GPE block
424 *              GpeBlockAddress     - Address and SpaceID
425 *              RegisterCount       - Number of GPE register pairs in the block
426 *              GpeBlockBaseNumber  - Starting GPE number for the block
427 *              InterruptNumber     - H/W interrupt for the block
428 *              ReturnGpeBlock      - Where the new block descriptor is returned
429 *
430 * RETURN:      Status
431 *
432 * DESCRIPTION: Create and Install a block of GPE registers. All GPEs within
433 *              the block are disabled at exit.
434 *              Note: Assumes namespace is locked.
435 *
436 ******************************************************************************/
437
438ACPI_STATUS
439AcpiEvCreateGpeBlock (
440    ACPI_NAMESPACE_NODE     *GpeDevice,
441    ACPI_GENERIC_ADDRESS    *GpeBlockAddress,
442    UINT32                  RegisterCount,
443    UINT8                   GpeBlockBaseNumber,
444    UINT32                  InterruptNumber,
445    ACPI_GPE_BLOCK_INFO     **ReturnGpeBlock)
446{
447    ACPI_STATUS             Status;
448    ACPI_GPE_BLOCK_INFO     *GpeBlock;
449    ACPI_GPE_WALK_INFO      WalkInfo;
450
451
452    ACPI_FUNCTION_TRACE (EvCreateGpeBlock);
453
454
455    if (!RegisterCount)
456    {
457        return_ACPI_STATUS (AE_OK);
458    }
459
460    /* Allocate a new GPE block */
461
462    GpeBlock = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_BLOCK_INFO));
463    if (!GpeBlock)
464    {
465        return_ACPI_STATUS (AE_NO_MEMORY);
466    }
467
468    /* Initialize the new GPE block */
469
470    GpeBlock->Node = GpeDevice;
471    GpeBlock->GpeCount = (UINT16) (RegisterCount * ACPI_GPE_REGISTER_WIDTH);
472    GpeBlock->Initialized = FALSE;
473    GpeBlock->RegisterCount = RegisterCount;
474    GpeBlock->BlockBaseNumber = GpeBlockBaseNumber;
475
476    ACPI_MEMCPY (&GpeBlock->BlockAddress, GpeBlockAddress,
477        sizeof (ACPI_GENERIC_ADDRESS));
478
479    /*
480     * Create the RegisterInfo and EventInfo sub-structures
481     * Note: disables and clears all GPEs in the block
482     */
483    Status = AcpiEvCreateGpeInfoBlocks (GpeBlock);
484    if (ACPI_FAILURE (Status))
485    {
486        ACPI_FREE (GpeBlock);
487        return_ACPI_STATUS (Status);
488    }
489
490    /* Install the new block in the global lists */
491
492    Status = AcpiEvInstallGpeBlock (GpeBlock, InterruptNumber);
493    if (ACPI_FAILURE (Status))
494    {
495        ACPI_FREE (GpeBlock);
496        return_ACPI_STATUS (Status);
497    }
498
499    AcpiGbl_AllGpesInitialized = FALSE;
500
501    /* Find all GPE methods (_Lxx or_Exx) for this block */
502
503    WalkInfo.GpeBlock = GpeBlock;
504    WalkInfo.GpeDevice = GpeDevice;
505    WalkInfo.ExecuteByOwnerId = FALSE;
506
507    Status = AcpiNsWalkNamespace (ACPI_TYPE_METHOD, GpeDevice,
508                ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK,
509                AcpiEvMatchGpeMethod, NULL, &WalkInfo, NULL);
510
511    /* Return the new block */
512
513    if (ReturnGpeBlock)
514    {
515        (*ReturnGpeBlock) = GpeBlock;
516    }
517
518    ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
519        "GPE %02X to %02X [%4.4s] %u regs on int 0x%X\n",
520        (UINT32) GpeBlock->BlockBaseNumber,
521        (UINT32) (GpeBlock->BlockBaseNumber + (GpeBlock->GpeCount - 1)),
522        GpeDevice->Name.Ascii, GpeBlock->RegisterCount,
523        InterruptNumber));
524
525    /* Update global count of currently available GPEs */
526
527    AcpiCurrentGpeCount += GpeBlock->GpeCount;
528    return_ACPI_STATUS (AE_OK);
529}
530
531
532/*******************************************************************************
533 *
534 * FUNCTION:    AcpiEvInitializeGpeBlock
535 *
536 * PARAMETERS:  ACPI_GPE_CALLBACK
537 *
538 * RETURN:      Status
539 *
540 * DESCRIPTION: Initialize and enable a GPE block. Enable GPEs that have
541 *              associated methods.
542 *              Note: Assumes namespace is locked.
543 *
544 ******************************************************************************/
545
546ACPI_STATUS
547AcpiEvInitializeGpeBlock (
548    ACPI_GPE_XRUPT_INFO     *GpeXruptInfo,
549    ACPI_GPE_BLOCK_INFO     *GpeBlock,
550    void                    *Ignored)
551{
552    ACPI_STATUS             Status;
553    ACPI_GPE_EVENT_INFO     *GpeEventInfo;
554    UINT32                  GpeEnabledCount;
555    UINT32                  GpeIndex;
556    UINT32                  i;
557    UINT32                  j;
558
559
560    ACPI_FUNCTION_TRACE (EvInitializeGpeBlock);
561
562
563    /*
564     * Ignore a null GPE block (e.g., if no GPE block 1 exists), and
565     * any GPE blocks that have been initialized already.
566     */
567    if (!GpeBlock || GpeBlock->Initialized)
568    {
569        return_ACPI_STATUS (AE_OK);
570    }
571
572    /*
573     * Enable all GPEs that have a corresponding method and have the
574     * ACPI_GPE_CAN_WAKE flag unset. Any other GPEs within this block
575     * must be enabled via the acpi_enable_gpe() interface.
576     */
577    GpeEnabledCount = 0;
578
579    for (i = 0; i < GpeBlock->RegisterCount; i++)
580    {
581        for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
582        {
583            /* Get the info block for this particular GPE */
584
585            GpeIndex = (i * ACPI_GPE_REGISTER_WIDTH) + j;
586            GpeEventInfo = &GpeBlock->EventInfo[GpeIndex];
587
588            /*
589             * Ignore GPEs that have no corresponding _Lxx/_Exx method
590             * and GPEs that are used to wake the system
591             */
592            if (((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_NONE) ||
593                ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_HANDLER) ||
594                (GpeEventInfo->Flags & ACPI_GPE_CAN_WAKE))
595            {
596                continue;
597            }
598
599            Status = AcpiEvAddGpeReference (GpeEventInfo);
600            if (ACPI_FAILURE (Status))
601            {
602                ACPI_EXCEPTION ((AE_INFO, Status,
603                    "Could not enable GPE 0x%02X",
604                    GpeIndex + GpeBlock->BlockBaseNumber));
605                continue;
606            }
607
608            GpeEnabledCount++;
609        }
610    }
611
612    if (GpeEnabledCount)
613    {
614        ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
615            "Enabled %u GPEs in this block\n", GpeEnabledCount));
616    }
617
618    GpeBlock->Initialized = TRUE;
619    return_ACPI_STATUS (AE_OK);
620}
621
622#endif /* !ACPI_REDUCED_HARDWARE */
623