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