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