xref: /freebsd/sys/contrib/dev/acpica/components/events/evgpeinit.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1 /******************************************************************************
2  *
3  * Module Name: evgpeinit - System GPE initialization and update
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 
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/acevents.h>
48 #include <contrib/dev/acpica/include/acnamesp.h>
49 
50 #define _COMPONENT          ACPI_EVENTS
51         ACPI_MODULE_NAME    ("evgpeinit")
52 
53 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
54 
55 /*
56  * Note: History of _PRW support in ACPICA
57  *
58  * Originally (2000 - 2010), the GPE initialization code performed a walk of
59  * the entire namespace to execute the _PRW methods and detect all GPEs
60  * capable of waking the system.
61  *
62  * As of 10/2010, the _PRW method execution has been removed since it is
63  * actually unnecessary. The host OS must in fact execute all _PRW methods
64  * in order to identify the device/power-resource dependencies. We now put
65  * the onus on the host OS to identify the wake GPEs as part of this process
66  * and to inform ACPICA of these GPEs via the AcpiSetupGpeForWake interface. This
67  * not only reduces the complexity of the ACPICA initialization code, but in
68  * some cases (on systems with very large namespaces) it should reduce the
69  * kernel boot time as well.
70  */
71 
72 /*******************************************************************************
73  *
74  * FUNCTION:    AcpiEvGpeInitialize
75  *
76  * PARAMETERS:  None
77  *
78  * RETURN:      Status
79  *
80  * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
81  *
82  ******************************************************************************/
83 
84 ACPI_STATUS
85 AcpiEvGpeInitialize (
86     void)
87 {
88     UINT32                  RegisterCount0 = 0;
89     UINT32                  RegisterCount1 = 0;
90     UINT32                  GpeNumberMax = 0;
91     ACPI_STATUS             Status;
92 
93 
94     ACPI_FUNCTION_TRACE (EvGpeInitialize);
95 
96 
97     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
98     if (ACPI_FAILURE (Status))
99     {
100         return_ACPI_STATUS (Status);
101     }
102 
103     /*
104      * Initialize the GPE Block(s) defined in the FADT
105      *
106      * Why the GPE register block lengths are divided by 2:  From the ACPI
107      * Spec, section "General-Purpose Event Registers", we have:
108      *
109      * "Each register block contains two registers of equal length
110      *  GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
111      *  GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
112      *  The length of the GPE1_STS and GPE1_EN registers is equal to
113      *  half the GPE1_LEN. If a generic register block is not supported
114      *  then its respective block pointer and block length values in the
115      *  FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
116      *  to be the same size."
117      */
118 
119     /*
120      * Determine the maximum GPE number for this machine.
121      *
122      * Note: both GPE0 and GPE1 are optional, and either can exist without
123      * the other.
124      *
125      * If EITHER the register length OR the block address are zero, then that
126      * particular block is not supported.
127      */
128     if (AcpiGbl_FADT.Gpe0BlockLength &&
129         AcpiGbl_FADT.XGpe0Block.Address)
130     {
131         /* GPE block 0 exists (has both length and address > 0) */
132 
133         RegisterCount0 = (UINT16) (AcpiGbl_FADT.Gpe0BlockLength / 2);
134 
135         GpeNumberMax = (RegisterCount0 * ACPI_GPE_REGISTER_WIDTH) - 1;
136 
137         /* Install GPE Block 0 */
138 
139         Status = AcpiEvCreateGpeBlock (AcpiGbl_FadtGpeDevice,
140                     &AcpiGbl_FADT.XGpe0Block, RegisterCount0, 0,
141                     AcpiGbl_FADT.SciInterrupt, &AcpiGbl_GpeFadtBlocks[0]);
142 
143         if (ACPI_FAILURE (Status))
144         {
145             ACPI_EXCEPTION ((AE_INFO, Status,
146                 "Could not create GPE Block 0"));
147         }
148     }
149 
150     if (AcpiGbl_FADT.Gpe1BlockLength &&
151         AcpiGbl_FADT.XGpe1Block.Address)
152     {
153         /* GPE block 1 exists (has both length and address > 0) */
154 
155         RegisterCount1 = (UINT16) (AcpiGbl_FADT.Gpe1BlockLength / 2);
156 
157         /* Check for GPE0/GPE1 overlap (if both banks exist) */
158 
159         if ((RegisterCount0) &&
160             (GpeNumberMax >= AcpiGbl_FADT.Gpe1Base))
161         {
162             ACPI_ERROR ((AE_INFO,
163                 "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
164                 "(GPE %u to %u) - Ignoring GPE1",
165                 GpeNumberMax, AcpiGbl_FADT.Gpe1Base,
166                 AcpiGbl_FADT.Gpe1Base +
167                 ((RegisterCount1 * ACPI_GPE_REGISTER_WIDTH) - 1)));
168 
169             /* Ignore GPE1 block by setting the register count to zero */
170 
171             RegisterCount1 = 0;
172         }
173         else
174         {
175             /* Install GPE Block 1 */
176 
177             Status = AcpiEvCreateGpeBlock (AcpiGbl_FadtGpeDevice,
178                         &AcpiGbl_FADT.XGpe1Block, RegisterCount1,
179                         AcpiGbl_FADT.Gpe1Base,
180                         AcpiGbl_FADT.SciInterrupt, &AcpiGbl_GpeFadtBlocks[1]);
181 
182             if (ACPI_FAILURE (Status))
183             {
184                 ACPI_EXCEPTION ((AE_INFO, Status,
185                     "Could not create GPE Block 1"));
186             }
187 
188             /*
189              * GPE0 and GPE1 do not have to be contiguous in the GPE number
190              * space. However, GPE0 always starts at GPE number zero.
191              */
192             GpeNumberMax = AcpiGbl_FADT.Gpe1Base +
193                             ((RegisterCount1 * ACPI_GPE_REGISTER_WIDTH) - 1);
194         }
195     }
196 
197     /* Exit if there are no GPE registers */
198 
199     if ((RegisterCount0 + RegisterCount1) == 0)
200     {
201         /* GPEs are not required by ACPI, this is OK */
202 
203         ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
204             "There are no GPE blocks defined in the FADT\n"));
205         Status = AE_OK;
206         goto Cleanup;
207     }
208 
209     /* Check for Max GPE number out-of-range */
210 
211     if (GpeNumberMax > ACPI_GPE_MAX)
212     {
213         ACPI_ERROR ((AE_INFO,
214             "Maximum GPE number from FADT is too large: 0x%X",
215             GpeNumberMax));
216         Status = AE_BAD_VALUE;
217         goto Cleanup;
218     }
219 
220 Cleanup:
221     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
222     return_ACPI_STATUS (AE_OK);
223 }
224 
225 
226 /*******************************************************************************
227  *
228  * FUNCTION:    AcpiEvUpdateGpes
229  *
230  * PARAMETERS:  TableOwnerId        - ID of the newly-loaded ACPI table
231  *
232  * RETURN:      None
233  *
234  * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
235  *              result of a Load() or LoadTable() operation. If new GPE
236  *              methods have been installed, register the new methods.
237  *
238  ******************************************************************************/
239 
240 void
241 AcpiEvUpdateGpes (
242     ACPI_OWNER_ID           TableOwnerId)
243 {
244     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo;
245     ACPI_GPE_BLOCK_INFO     *GpeBlock;
246     ACPI_GPE_WALK_INFO      WalkInfo;
247     ACPI_STATUS             Status = AE_OK;
248 
249 
250     /*
251      * Find any _Lxx/_Exx GPE methods that have just been loaded.
252      *
253      * Any GPEs that correspond to new _Lxx/_Exx methods are immediately
254      * enabled.
255      *
256      * Examine the namespace underneath each GpeDevice within the
257      * GpeBlock lists.
258      */
259     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
260     if (ACPI_FAILURE (Status))
261     {
262         return;
263     }
264 
265     WalkInfo.Count = 0;
266     WalkInfo.OwnerId = TableOwnerId;
267     WalkInfo.ExecuteByOwnerId = TRUE;
268 
269     /* Walk the interrupt level descriptor list */
270 
271     GpeXruptInfo = AcpiGbl_GpeXruptListHead;
272     while (GpeXruptInfo)
273     {
274         /* Walk all Gpe Blocks attached to this interrupt level */
275 
276         GpeBlock = GpeXruptInfo->GpeBlockListHead;
277         while (GpeBlock)
278         {
279             WalkInfo.GpeBlock = GpeBlock;
280             WalkInfo.GpeDevice = GpeBlock->Node;
281 
282             Status = AcpiNsWalkNamespace (ACPI_TYPE_METHOD,
283                         WalkInfo.GpeDevice, ACPI_UINT32_MAX,
284                         ACPI_NS_WALK_NO_UNLOCK, AcpiEvMatchGpeMethod,
285                         NULL, &WalkInfo, NULL);
286             if (ACPI_FAILURE (Status))
287             {
288                 ACPI_EXCEPTION ((AE_INFO, Status,
289                     "While decoding _Lxx/_Exx methods"));
290             }
291 
292             GpeBlock = GpeBlock->Next;
293         }
294 
295         GpeXruptInfo = GpeXruptInfo->Next;
296     }
297 
298     if (WalkInfo.Count)
299     {
300         ACPI_INFO ((AE_INFO, "Enabled %u new GPEs", WalkInfo.Count));
301     }
302 
303     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
304     return;
305 }
306 
307 
308 /*******************************************************************************
309  *
310  * FUNCTION:    AcpiEvMatchGpeMethod
311  *
312  * PARAMETERS:  Callback from WalkNamespace
313  *
314  * RETURN:      Status
315  *
316  * DESCRIPTION: Called from AcpiWalkNamespace. Expects each object to be a
317  *              control method under the _GPE portion of the namespace.
318  *              Extract the name and GPE type from the object, saving this
319  *              information for quick lookup during GPE dispatch. Allows a
320  *              per-OwnerId evaluation if ExecuteByOwnerId is TRUE in the
321  *              WalkInfo parameter block.
322  *
323  *              The name of each GPE control method is of the form:
324  *              "_Lxx" or "_Exx", where:
325  *                  L      - means that the GPE is level triggered
326  *                  E      - means that the GPE is edge triggered
327  *                  xx     - is the GPE number [in HEX]
328  *
329  * If WalkInfo->ExecuteByOwnerId is TRUE, we only execute examine GPE methods
330  * with that owner.
331  *
332  ******************************************************************************/
333 
334 ACPI_STATUS
335 AcpiEvMatchGpeMethod (
336     ACPI_HANDLE             ObjHandle,
337     UINT32                  Level,
338     void                    *Context,
339     void                    **ReturnValue)
340 {
341     ACPI_NAMESPACE_NODE     *MethodNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, ObjHandle);
342     ACPI_GPE_WALK_INFO      *WalkInfo = ACPI_CAST_PTR (ACPI_GPE_WALK_INFO, Context);
343     ACPI_GPE_EVENT_INFO     *GpeEventInfo;
344     UINT32                  GpeNumber;
345     char                    Name[ACPI_NAME_SIZE + 1];
346     UINT8                   Type;
347 
348 
349     ACPI_FUNCTION_TRACE (EvMatchGpeMethod);
350 
351 
352     /* Check if requested OwnerId matches this OwnerId */
353 
354     if ((WalkInfo->ExecuteByOwnerId) &&
355         (MethodNode->OwnerId != WalkInfo->OwnerId))
356     {
357         return_ACPI_STATUS (AE_OK);
358     }
359 
360     /*
361      * Match and decode the _Lxx and _Exx GPE method names
362      *
363      * 1) Extract the method name and null terminate it
364      */
365     ACPI_MOVE_32_TO_32 (Name, &MethodNode->Name.Integer);
366     Name[ACPI_NAME_SIZE] = 0;
367 
368     /* 2) Name must begin with an underscore */
369 
370     if (Name[0] != '_')
371     {
372         return_ACPI_STATUS (AE_OK); /* Ignore this method */
373     }
374 
375     /*
376      * 3) Edge/Level determination is based on the 2nd character
377      *    of the method name
378      */
379     switch (Name[1])
380     {
381     case 'L':
382         Type = ACPI_GPE_LEVEL_TRIGGERED;
383         break;
384 
385     case 'E':
386         Type = ACPI_GPE_EDGE_TRIGGERED;
387         break;
388 
389     default:
390         /* Unknown method type, just ignore it */
391 
392         ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
393             "Ignoring unknown GPE method type: %s "
394             "(name not of form _Lxx or _Exx)", Name));
395         return_ACPI_STATUS (AE_OK);
396     }
397 
398     /* 4) The last two characters of the name are the hex GPE Number */
399 
400     GpeNumber = ACPI_STRTOUL (&Name[2], NULL, 16);
401     if (GpeNumber == ACPI_UINT32_MAX)
402     {
403         /* Conversion failed; invalid method, just ignore it */
404 
405         ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
406             "Could not extract GPE number from name: %s "
407             "(name is not of form _Lxx or _Exx)", Name));
408         return_ACPI_STATUS (AE_OK);
409     }
410 
411     /* Ensure that we have a valid GPE number for this GPE block */
412 
413     GpeEventInfo = AcpiEvLowGetGpeInfo (GpeNumber, WalkInfo->GpeBlock);
414     if (!GpeEventInfo)
415     {
416         /*
417          * This GpeNumber is not valid for this GPE block, just ignore it.
418          * However, it may be valid for a different GPE block, since GPE0
419          * and GPE1 methods both appear under \_GPE.
420          */
421         return_ACPI_STATUS (AE_OK);
422     }
423 
424     if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) ==
425             ACPI_GPE_DISPATCH_HANDLER)
426     {
427         /* If there is already a handler, ignore this GPE method */
428 
429         return_ACPI_STATUS (AE_OK);
430     }
431 
432     if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) ==
433             ACPI_GPE_DISPATCH_METHOD)
434     {
435         /*
436          * If there is already a method, ignore this method. But check
437          * for a type mismatch (if both the _Lxx AND _Exx exist)
438          */
439         if (Type != (GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK))
440         {
441             ACPI_ERROR ((AE_INFO,
442                 "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
443                 GpeNumber, GpeNumber, GpeNumber));
444         }
445         return_ACPI_STATUS (AE_OK);
446     }
447 
448     /*
449      * Add the GPE information from above to the GpeEventInfo block for
450      * use during dispatch of this GPE.
451      */
452     GpeEventInfo->Flags &= ~(ACPI_GPE_DISPATCH_MASK);
453     GpeEventInfo->Flags |= (UINT8) (Type | ACPI_GPE_DISPATCH_METHOD);
454     GpeEventInfo->Dispatch.MethodNode = MethodNode;
455 
456     ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
457         "Registered GPE method %s as GPE number 0x%.2X\n",
458         Name, GpeNumber));
459     return_ACPI_STATUS (AE_OK);
460 }
461 
462 #endif /* !ACPI_REDUCED_HARDWARE */
463