1 /******************************************************************************
3 * Module Name: evgpeblk - GPE block creation and initialization.
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acevents.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME ("evgpeblk")
52 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
54 /* Local prototypes */
57 AcpiEvInstallGpeBlock (
58 ACPI_GPE_BLOCK_INFO *GpeBlock,
59 UINT32 InterruptNumber);
62 AcpiEvCreateGpeInfoBlocks (
63 ACPI_GPE_BLOCK_INFO *GpeBlock);
66 /*******************************************************************************
68 * FUNCTION: AcpiEvInstallGpeBlock
70 * PARAMETERS: GpeBlock - New GPE block
71 * InterruptNumber - Xrupt to be associated with this
76 * DESCRIPTION: Install new GPE block with mutex support
78 ******************************************************************************/
81 AcpiEvInstallGpeBlock (
82 ACPI_GPE_BLOCK_INFO *GpeBlock,
83 UINT32 InterruptNumber)
85 ACPI_GPE_BLOCK_INFO *NextGpeBlock;
86 ACPI_GPE_XRUPT_INFO *GpeXruptBlock;
91 ACPI_FUNCTION_TRACE (EvInstallGpeBlock);
94 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
95 if (ACPI_FAILURE (Status))
97 return_ACPI_STATUS (Status);
100 Status = AcpiEvGetGpeXruptBlock (InterruptNumber, &GpeXruptBlock);
101 if (ACPI_FAILURE (Status))
106 /* Install the new block at the end of the list with lock */
108 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
109 if (GpeXruptBlock->GpeBlockListHead)
111 NextGpeBlock = GpeXruptBlock->GpeBlockListHead;
112 while (NextGpeBlock->Next)
114 NextGpeBlock = NextGpeBlock->Next;
117 NextGpeBlock->Next = GpeBlock;
118 GpeBlock->Previous = NextGpeBlock;
122 GpeXruptBlock->GpeBlockListHead = GpeBlock;
125 GpeBlock->XruptBlock = GpeXruptBlock;
126 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
130 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
131 return_ACPI_STATUS (Status);
135 /*******************************************************************************
137 * FUNCTION: AcpiEvDeleteGpeBlock
139 * PARAMETERS: GpeBlock - Existing GPE block
143 * DESCRIPTION: Remove a GPE block
145 ******************************************************************************/
148 AcpiEvDeleteGpeBlock (
149 ACPI_GPE_BLOCK_INFO *GpeBlock)
152 ACPI_CPU_FLAGS Flags;
155 ACPI_FUNCTION_TRACE (EvInstallGpeBlock);
158 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
159 if (ACPI_FAILURE (Status))
161 return_ACPI_STATUS (Status);
164 /* Disable all GPEs in this block */
166 Status = AcpiHwDisableGpeBlock (GpeBlock->XruptBlock, GpeBlock, NULL);
168 if (!GpeBlock->Previous && !GpeBlock->Next)
170 /* This is the last GpeBlock on this interrupt */
172 Status = AcpiEvDeleteGpeXrupt (GpeBlock->XruptBlock);
173 if (ACPI_FAILURE (Status))
180 /* Remove the block on this interrupt with lock */
182 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
183 if (GpeBlock->Previous)
185 GpeBlock->Previous->Next = GpeBlock->Next;
189 GpeBlock->XruptBlock->GpeBlockListHead = GpeBlock->Next;
194 GpeBlock->Next->Previous = GpeBlock->Previous;
196 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
199 AcpiCurrentGpeCount -= GpeBlock->GpeCount;
201 /* Free the GpeBlock */
203 ACPI_FREE (GpeBlock->RegisterInfo);
204 ACPI_FREE (GpeBlock->EventInfo);
205 ACPI_FREE (GpeBlock);
208 Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
209 return_ACPI_STATUS (Status);
213 /*******************************************************************************
215 * FUNCTION: AcpiEvCreateGpeInfoBlocks
217 * PARAMETERS: GpeBlock - New GPE block
221 * DESCRIPTION: Create the RegisterInfo and EventInfo blocks for this GPE block
223 ******************************************************************************/
226 AcpiEvCreateGpeInfoBlocks (
227 ACPI_GPE_BLOCK_INFO *GpeBlock)
229 ACPI_GPE_REGISTER_INFO *GpeRegisterInfo = NULL;
230 ACPI_GPE_EVENT_INFO *GpeEventInfo = NULL;
231 ACPI_GPE_EVENT_INFO *ThisEvent;
232 ACPI_GPE_REGISTER_INFO *ThisRegister;
238 ACPI_FUNCTION_TRACE (EvCreateGpeInfoBlocks);
241 /* Allocate the GPE register information block */
243 GpeRegisterInfo = ACPI_ALLOCATE_ZEROED (
244 (ACPI_SIZE) GpeBlock->RegisterCount *
245 sizeof (ACPI_GPE_REGISTER_INFO));
246 if (!GpeRegisterInfo)
248 ACPI_ERROR ((AE_INFO,
249 "Could not allocate the GpeRegisterInfo table"));
250 return_ACPI_STATUS (AE_NO_MEMORY);
254 * Allocate the GPE EventInfo block. There are eight distinct GPEs
255 * per register. Initialization to zeros is sufficient.
257 GpeEventInfo = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) GpeBlock->GpeCount *
258 sizeof (ACPI_GPE_EVENT_INFO));
261 ACPI_ERROR ((AE_INFO,
262 "Could not allocate the GpeEventInfo table"));
263 Status = AE_NO_MEMORY;
267 /* Save the new Info arrays in the GPE block */
269 GpeBlock->RegisterInfo = GpeRegisterInfo;
270 GpeBlock->EventInfo = GpeEventInfo;
273 * Initialize the GPE Register and Event structures. A goal of these
274 * tables is to hide the fact that there are two separate GPE register
275 * sets in a given GPE hardware block, the status registers occupy the
276 * first half, and the enable registers occupy the second half.
278 ThisRegister = GpeRegisterInfo;
279 ThisEvent = GpeEventInfo;
281 for (i = 0; i < GpeBlock->RegisterCount; i++)
283 /* Init the RegisterInfo for this GPE register (8 GPEs) */
285 ThisRegister->BaseGpeNumber = (UINT16)
286 (GpeBlock->BlockBaseNumber + (i * ACPI_GPE_REGISTER_WIDTH));
288 ThisRegister->StatusAddress.Address =
289 GpeBlock->Address + i;
291 ThisRegister->EnableAddress.Address =
292 GpeBlock->Address + i + GpeBlock->RegisterCount;
294 ThisRegister->StatusAddress.SpaceId = GpeBlock->SpaceId;
295 ThisRegister->EnableAddress.SpaceId = GpeBlock->SpaceId;
296 ThisRegister->StatusAddress.BitWidth = ACPI_GPE_REGISTER_WIDTH;
297 ThisRegister->EnableAddress.BitWidth = ACPI_GPE_REGISTER_WIDTH;
298 ThisRegister->StatusAddress.BitOffset = 0;
299 ThisRegister->EnableAddress.BitOffset = 0;
301 /* Init the EventInfo for each GPE within this register */
303 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
305 ThisEvent->GpeNumber = (UINT8) (ThisRegister->BaseGpeNumber + j);
306 ThisEvent->RegisterInfo = ThisRegister;
310 /* Disable all GPEs within this register */
312 Status = AcpiHwWrite (0x00, &ThisRegister->EnableAddress);
313 if (ACPI_FAILURE (Status))
318 /* Clear any pending GPE events within this register */
320 Status = AcpiHwWrite (0xFF, &ThisRegister->StatusAddress);
321 if (ACPI_FAILURE (Status))
329 return_ACPI_STATUS (AE_OK);
335 ACPI_FREE (GpeRegisterInfo);
339 ACPI_FREE (GpeEventInfo);
342 return_ACPI_STATUS (Status);
346 /*******************************************************************************
348 * FUNCTION: AcpiEvCreateGpeBlock
350 * PARAMETERS: GpeDevice - Handle to the parent GPE block
351 * GpeBlockAddress - Address and SpaceID
352 * RegisterCount - Number of GPE register pairs in the block
353 * GpeBlockBaseNumber - Starting GPE number for the block
354 * InterruptNumber - H/W interrupt for the block
355 * ReturnGpeBlock - Where the new block descriptor is returned
359 * DESCRIPTION: Create and Install a block of GPE registers. All GPEs within
360 * the block are disabled at exit.
361 * Note: Assumes namespace is locked.
363 ******************************************************************************/
366 AcpiEvCreateGpeBlock (
367 ACPI_NAMESPACE_NODE *GpeDevice,
370 UINT32 RegisterCount,
371 UINT16 GpeBlockBaseNumber,
372 UINT32 InterruptNumber,
373 ACPI_GPE_BLOCK_INFO **ReturnGpeBlock)
376 ACPI_GPE_BLOCK_INFO *GpeBlock;
377 ACPI_GPE_WALK_INFO WalkInfo;
380 ACPI_FUNCTION_TRACE (EvCreateGpeBlock);
385 return_ACPI_STATUS (AE_OK);
388 /* Allocate a new GPE block */
390 GpeBlock = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_BLOCK_INFO));
393 return_ACPI_STATUS (AE_NO_MEMORY);
396 /* Initialize the new GPE block */
398 GpeBlock->Address = Address;
399 GpeBlock->SpaceId = SpaceId;
400 GpeBlock->Node = GpeDevice;
401 GpeBlock->GpeCount = (UINT16) (RegisterCount * ACPI_GPE_REGISTER_WIDTH);
402 GpeBlock->Initialized = FALSE;
403 GpeBlock->RegisterCount = RegisterCount;
404 GpeBlock->BlockBaseNumber = GpeBlockBaseNumber;
407 * Create the RegisterInfo and EventInfo sub-structures
408 * Note: disables and clears all GPEs in the block
410 Status = AcpiEvCreateGpeInfoBlocks (GpeBlock);
411 if (ACPI_FAILURE (Status))
413 ACPI_FREE (GpeBlock);
414 return_ACPI_STATUS (Status);
417 /* Install the new block in the global lists */
419 Status = AcpiEvInstallGpeBlock (GpeBlock, InterruptNumber);
420 if (ACPI_FAILURE (Status))
422 ACPI_FREE (GpeBlock->RegisterInfo);
423 ACPI_FREE (GpeBlock->EventInfo);
424 ACPI_FREE (GpeBlock);
425 return_ACPI_STATUS (Status);
428 AcpiGbl_AllGpesInitialized = FALSE;
430 /* Find all GPE methods (_Lxx or_Exx) for this block */
432 WalkInfo.GpeBlock = GpeBlock;
433 WalkInfo.GpeDevice = GpeDevice;
434 WalkInfo.ExecuteByOwnerId = FALSE;
436 Status = AcpiNsWalkNamespace (ACPI_TYPE_METHOD, GpeDevice,
437 ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK,
438 AcpiEvMatchGpeMethod, NULL, &WalkInfo, NULL);
440 /* Return the new block */
444 (*ReturnGpeBlock) = GpeBlock;
447 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT,
448 " Initialized GPE %02X to %02X [%4.4s] %u regs on interrupt 0x%X%s\n",
449 (UINT32) GpeBlock->BlockBaseNumber,
450 (UINT32) (GpeBlock->BlockBaseNumber + (GpeBlock->GpeCount - 1)),
451 GpeDevice->Name.Ascii, GpeBlock->RegisterCount, InterruptNumber,
452 InterruptNumber == AcpiGbl_FADT.SciInterrupt ? " (SCI)" : ""));
454 /* Update global count of currently available GPEs */
456 AcpiCurrentGpeCount += GpeBlock->GpeCount;
457 return_ACPI_STATUS (AE_OK);
461 /*******************************************************************************
463 * FUNCTION: AcpiEvInitializeGpeBlock
465 * PARAMETERS: ACPI_GPE_CALLBACK
469 * DESCRIPTION: Initialize and enable a GPE block. Enable GPEs that have
470 * associated methods.
471 * Note: Assumes namespace is locked.
473 ******************************************************************************/
476 AcpiEvInitializeGpeBlock (
477 ACPI_GPE_XRUPT_INFO *GpeXruptInfo,
478 ACPI_GPE_BLOCK_INFO *GpeBlock,
482 ACPI_GPE_EVENT_INFO *GpeEventInfo;
483 UINT32 GpeEnabledCount;
489 ACPI_FUNCTION_TRACE (EvInitializeGpeBlock);
493 * Ignore a null GPE block (e.g., if no GPE block 1 exists), and
494 * any GPE blocks that have been initialized already.
496 if (!GpeBlock || GpeBlock->Initialized)
498 return_ACPI_STATUS (AE_OK);
502 * Enable all GPEs that have a corresponding method and have the
503 * ACPI_GPE_CAN_WAKE flag unset. Any other GPEs within this block
504 * must be enabled via the acpi_enable_gpe() interface.
508 for (i = 0; i < GpeBlock->RegisterCount; i++)
510 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
512 /* Get the info block for this particular GPE */
514 GpeIndex = (i * ACPI_GPE_REGISTER_WIDTH) + j;
515 GpeEventInfo = &GpeBlock->EventInfo[GpeIndex];
518 * Ignore GPEs that have no corresponding _Lxx/_Exx method
519 * and GPEs that are used to wake the system
521 if (((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_NONE) ||
522 ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_HANDLER) ||
523 (GpeEventInfo->Flags & ACPI_GPE_CAN_WAKE))
528 Status = AcpiEvAddGpeReference (GpeEventInfo);
529 if (ACPI_FAILURE (Status))
531 ACPI_EXCEPTION ((AE_INFO, Status,
532 "Could not enable GPE 0x%02X",
533 GpeIndex + GpeBlock->BlockBaseNumber));
544 "Enabled %u GPEs in block %02X to %02X", GpeEnabledCount,
545 (UINT32) GpeBlock->BlockBaseNumber,
546 (UINT32) (GpeBlock->BlockBaseNumber + (GpeBlock->GpeCount - 1))));
549 GpeBlock->Initialized = TRUE;
550 return_ACPI_STATUS (AE_OK);
553 #endif /* !ACPI_REDUCED_HARDWARE */