xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/events/evgpeinit.c (revision f2df0cfe93a902842f6f4629ff614f5b3f9bf687)
1 /******************************************************************************
2  *
3  * Module Name: evgpeinit - System GPE initialization and update
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2016, 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    ("evgpeinit")
123 
124 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
125 
126 /*
127  * Note: History of _PRW support in ACPICA
128  *
129  * Originally (2000 - 2010), the GPE initialization code performed a walk of
130  * the entire namespace to execute the _PRW methods and detect all GPEs
131  * capable of waking the system.
132  *
133  * As of 10/2010, the _PRW method execution has been removed since it is
134  * actually unnecessary. The host OS must in fact execute all _PRW methods
135  * in order to identify the device/power-resource dependencies. We now put
136  * the onus on the host OS to identify the wake GPEs as part of this process
137  * and to inform ACPICA of these GPEs via the AcpiSetupGpeForWake interface. This
138  * not only reduces the complexity of the ACPICA initialization code, but in
139  * some cases (on systems with very large namespaces) it should reduce the
140  * kernel boot time as well.
141  */
142 
143 /*******************************************************************************
144  *
145  * FUNCTION:    AcpiEvGpeInitialize
146  *
147  * PARAMETERS:  None
148  *
149  * RETURN:      Status
150  *
151  * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
152  *
153  ******************************************************************************/
154 
155 ACPI_STATUS
156 AcpiEvGpeInitialize (
157     void)
158 {
159     UINT32                  RegisterCount0 = 0;
160     UINT32                  RegisterCount1 = 0;
161     UINT32                  GpeNumberMax = 0;
162     ACPI_STATUS             Status;
163 
164 
165     ACPI_FUNCTION_TRACE (EvGpeInitialize);
166 
167 
168     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT,
169         "Initializing General Purpose Events (GPEs):\n"));
170 
171     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
172     if (ACPI_FAILURE (Status))
173     {
174         return_ACPI_STATUS (Status);
175     }
176 
177     /*
178      * Initialize the GPE Block(s) defined in the FADT
179      *
180      * Why the GPE register block lengths are divided by 2:  From the ACPI
181      * Spec, section "General-Purpose Event Registers", we have:
182      *
183      * "Each register block contains two registers of equal length
184      *  GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
185      *  GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
186      *  The length of the GPE1_STS and GPE1_EN registers is equal to
187      *  half the GPE1_LEN. If a generic register block is not supported
188      *  then its respective block pointer and block length values in the
189      *  FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
190      *  to be the same size."
191      */
192 
193     /*
194      * Determine the maximum GPE number for this machine.
195      *
196      * Note: both GPE0 and GPE1 are optional, and either can exist without
197      * the other.
198      *
199      * If EITHER the register length OR the block address are zero, then that
200      * particular block is not supported.
201      */
202     if (AcpiGbl_FADT.Gpe0BlockLength &&
203         AcpiGbl_FADT.XGpe0Block.Address)
204     {
205         /* GPE block 0 exists (has both length and address > 0) */
206 
207         RegisterCount0 = (UINT16) (AcpiGbl_FADT.Gpe0BlockLength / 2);
208         GpeNumberMax = (RegisterCount0 * ACPI_GPE_REGISTER_WIDTH) - 1;
209 
210         /* Install GPE Block 0 */
211 
212         Status = AcpiEvCreateGpeBlock (AcpiGbl_FadtGpeDevice,
213             AcpiGbl_FADT.XGpe0Block.Address,
214             AcpiGbl_FADT.XGpe0Block.SpaceId,
215             RegisterCount0, 0,
216             AcpiGbl_FADT.SciInterrupt, &AcpiGbl_GpeFadtBlocks[0]);
217 
218         if (ACPI_FAILURE (Status))
219         {
220             ACPI_EXCEPTION ((AE_INFO, Status,
221                 "Could not create GPE Block 0"));
222         }
223     }
224 
225     if (AcpiGbl_FADT.Gpe1BlockLength &&
226         AcpiGbl_FADT.XGpe1Block.Address)
227     {
228         /* GPE block 1 exists (has both length and address > 0) */
229 
230         RegisterCount1 = (UINT16) (AcpiGbl_FADT.Gpe1BlockLength / 2);
231 
232         /* Check for GPE0/GPE1 overlap (if both banks exist) */
233 
234         if ((RegisterCount0) &&
235             (GpeNumberMax >= AcpiGbl_FADT.Gpe1Base))
236         {
237             ACPI_ERROR ((AE_INFO,
238                 "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
239                 "(GPE %u to %u) - Ignoring GPE1",
240                 GpeNumberMax, AcpiGbl_FADT.Gpe1Base,
241                 AcpiGbl_FADT.Gpe1Base +
242                 ((RegisterCount1 * ACPI_GPE_REGISTER_WIDTH) - 1)));
243 
244             /* Ignore GPE1 block by setting the register count to zero */
245 
246             RegisterCount1 = 0;
247         }
248         else
249         {
250             /* Install GPE Block 1 */
251 
252             Status = AcpiEvCreateGpeBlock (AcpiGbl_FadtGpeDevice,
253                 AcpiGbl_FADT.XGpe1Block.Address,
254                 AcpiGbl_FADT.XGpe1Block.SpaceId,
255                 RegisterCount1,
256                 AcpiGbl_FADT.Gpe1Base,
257                 AcpiGbl_FADT.SciInterrupt, &AcpiGbl_GpeFadtBlocks[1]);
258 
259             if (ACPI_FAILURE (Status))
260             {
261                 ACPI_EXCEPTION ((AE_INFO, Status,
262                     "Could not create GPE Block 1"));
263             }
264 
265             /*
266              * GPE0 and GPE1 do not have to be contiguous in the GPE number
267              * space. However, GPE0 always starts at GPE number zero.
268              */
269             GpeNumberMax = AcpiGbl_FADT.Gpe1Base +
270                 ((RegisterCount1 * ACPI_GPE_REGISTER_WIDTH) - 1);
271         }
272     }
273 
274     /* Exit if there are no GPE registers */
275 
276     if ((RegisterCount0 + RegisterCount1) == 0)
277     {
278         /* GPEs are not required by ACPI, this is OK */
279 
280         ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
281             "There are no GPE blocks defined in the FADT\n"));
282         Status = AE_OK;
283         goto Cleanup;
284     }
285 
286 
287 Cleanup:
288     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
289     return_ACPI_STATUS (AE_OK);
290 }
291 
292 
293 /*******************************************************************************
294  *
295  * FUNCTION:    AcpiEvUpdateGpes
296  *
297  * PARAMETERS:  TableOwnerId        - ID of the newly-loaded ACPI table
298  *
299  * RETURN:      None
300  *
301  * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
302  *              result of a Load() or LoadTable() operation. If new GPE
303  *              methods have been installed, register the new methods.
304  *
305  ******************************************************************************/
306 
307 void
308 AcpiEvUpdateGpes (
309     ACPI_OWNER_ID           TableOwnerId)
310 {
311     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo;
312     ACPI_GPE_BLOCK_INFO     *GpeBlock;
313     ACPI_GPE_WALK_INFO      WalkInfo;
314     ACPI_STATUS             Status = AE_OK;
315 
316 
317     /*
318      * Find any _Lxx/_Exx GPE methods that have just been loaded.
319      *
320      * Any GPEs that correspond to new _Lxx/_Exx methods are immediately
321      * enabled.
322      *
323      * Examine the namespace underneath each GpeDevice within the
324      * GpeBlock lists.
325      */
326     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
327     if (ACPI_FAILURE (Status))
328     {
329         return;
330     }
331 
332     WalkInfo.Count = 0;
333     WalkInfo.OwnerId = TableOwnerId;
334     WalkInfo.ExecuteByOwnerId = TRUE;
335 
336     /* Walk the interrupt level descriptor list */
337 
338     GpeXruptInfo = AcpiGbl_GpeXruptListHead;
339     while (GpeXruptInfo)
340     {
341         /* Walk all Gpe Blocks attached to this interrupt level */
342 
343         GpeBlock = GpeXruptInfo->GpeBlockListHead;
344         while (GpeBlock)
345         {
346             WalkInfo.GpeBlock = GpeBlock;
347             WalkInfo.GpeDevice = GpeBlock->Node;
348 
349             Status = AcpiNsWalkNamespace (ACPI_TYPE_METHOD,
350                 WalkInfo.GpeDevice, ACPI_UINT32_MAX,
351                 ACPI_NS_WALK_NO_UNLOCK, AcpiEvMatchGpeMethod,
352                 NULL, &WalkInfo, NULL);
353             if (ACPI_FAILURE (Status))
354             {
355                 ACPI_EXCEPTION ((AE_INFO, Status,
356                     "While decoding _Lxx/_Exx methods"));
357             }
358 
359             GpeBlock = GpeBlock->Next;
360         }
361 
362         GpeXruptInfo = GpeXruptInfo->Next;
363     }
364 
365     if (WalkInfo.Count)
366     {
367         ACPI_INFO ((AE_INFO, "Enabled %u new GPEs", WalkInfo.Count));
368     }
369 
370     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
371     return;
372 }
373 
374 
375 /*******************************************************************************
376  *
377  * FUNCTION:    AcpiEvMatchGpeMethod
378  *
379  * PARAMETERS:  Callback from WalkNamespace
380  *
381  * RETURN:      Status
382  *
383  * DESCRIPTION: Called from AcpiWalkNamespace. Expects each object to be a
384  *              control method under the _GPE portion of the namespace.
385  *              Extract the name and GPE type from the object, saving this
386  *              information for quick lookup during GPE dispatch. Allows a
387  *              per-OwnerId evaluation if ExecuteByOwnerId is TRUE in the
388  *              WalkInfo parameter block.
389  *
390  *              The name of each GPE control method is of the form:
391  *              "_Lxx" or "_Exx", where:
392  *                  L      - means that the GPE is level triggered
393  *                  E      - means that the GPE is edge triggered
394  *                  xx     - is the GPE number [in HEX]
395  *
396  * If WalkInfo->ExecuteByOwnerId is TRUE, we only execute examine GPE methods
397  * with that owner.
398  *
399  ******************************************************************************/
400 
401 ACPI_STATUS
402 AcpiEvMatchGpeMethod (
403     ACPI_HANDLE             ObjHandle,
404     UINT32                  Level,
405     void                    *Context,
406     void                    **ReturnValue)
407 {
408     ACPI_NAMESPACE_NODE     *MethodNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, ObjHandle);
409     ACPI_GPE_WALK_INFO      *WalkInfo = ACPI_CAST_PTR (ACPI_GPE_WALK_INFO, Context);
410     ACPI_GPE_EVENT_INFO     *GpeEventInfo;
411     UINT32                  GpeNumber;
412     char                    Name[ACPI_NAME_SIZE + 1];
413     UINT8                   Type;
414 
415 
416     ACPI_FUNCTION_TRACE (EvMatchGpeMethod);
417 
418 
419     /* Check if requested OwnerId matches this OwnerId */
420 
421     if ((WalkInfo->ExecuteByOwnerId) &&
422         (MethodNode->OwnerId != WalkInfo->OwnerId))
423     {
424         return_ACPI_STATUS (AE_OK);
425     }
426 
427     /*
428      * Match and decode the _Lxx and _Exx GPE method names
429      *
430      * 1) Extract the method name and null terminate it
431      */
432     ACPI_MOVE_32_TO_32 (Name, &MethodNode->Name.Integer);
433     Name[ACPI_NAME_SIZE] = 0;
434 
435     /* 2) Name must begin with an underscore */
436 
437     if (Name[0] != '_')
438     {
439         return_ACPI_STATUS (AE_OK); /* Ignore this method */
440     }
441 
442     /*
443      * 3) Edge/Level determination is based on the 2nd character
444      *    of the method name
445      */
446     switch (Name[1])
447     {
448     case 'L':
449 
450         Type = ACPI_GPE_LEVEL_TRIGGERED;
451         break;
452 
453     case 'E':
454 
455         Type = ACPI_GPE_EDGE_TRIGGERED;
456         break;
457 
458     default:
459 
460         /* Unknown method type, just ignore it */
461 
462         ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
463             "Ignoring unknown GPE method type: %s "
464             "(name not of form _Lxx or _Exx)", Name));
465         return_ACPI_STATUS (AE_OK);
466     }
467 
468     /* 4) The last two characters of the name are the hex GPE Number */
469 
470     GpeNumber = strtoul (&Name[2], NULL, 16);
471     if (GpeNumber == ACPI_UINT32_MAX)
472     {
473         /* Conversion failed; invalid method, just ignore it */
474 
475         ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
476             "Could not extract GPE number from name: %s "
477             "(name is not of form _Lxx or _Exx)", Name));
478         return_ACPI_STATUS (AE_OK);
479     }
480 
481     /* Ensure that we have a valid GPE number for this GPE block */
482 
483     GpeEventInfo = AcpiEvLowGetGpeInfo (GpeNumber, WalkInfo->GpeBlock);
484     if (!GpeEventInfo)
485     {
486         /*
487          * This GpeNumber is not valid for this GPE block, just ignore it.
488          * However, it may be valid for a different GPE block, since GPE0
489          * and GPE1 methods both appear under \_GPE.
490          */
491         return_ACPI_STATUS (AE_OK);
492     }
493 
494     if ((ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==
495             ACPI_GPE_DISPATCH_HANDLER) ||
496         (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==
497             ACPI_GPE_DISPATCH_RAW_HANDLER))
498     {
499         /* If there is already a handler, ignore this GPE method */
500 
501         return_ACPI_STATUS (AE_OK);
502     }
503 
504     if (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==
505         ACPI_GPE_DISPATCH_METHOD)
506     {
507         /*
508          * If there is already a method, ignore this method. But check
509          * for a type mismatch (if both the _Lxx AND _Exx exist)
510          */
511         if (Type != (GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK))
512         {
513             ACPI_ERROR ((AE_INFO,
514                 "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
515                 GpeNumber, GpeNumber, GpeNumber));
516         }
517         return_ACPI_STATUS (AE_OK);
518     }
519 
520     /* Disable the GPE in case it's been enabled already. */
521 
522     (void) AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
523 
524     /*
525      * Add the GPE information from above to the GpeEventInfo block for
526      * use during dispatch of this GPE.
527      */
528     GpeEventInfo->Flags &= ~(ACPI_GPE_DISPATCH_MASK);
529     GpeEventInfo->Flags |= (UINT8) (Type | ACPI_GPE_DISPATCH_METHOD);
530     GpeEventInfo->Dispatch.MethodNode = MethodNode;
531 
532     ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
533         "Registered GPE method %s as GPE number 0x%.2X\n",
534         Name, GpeNumber));
535     return_ACPI_STATUS (AE_OK);
536 }
537 
538 #endif /* !ACPI_REDUCED_HARDWARE */
539