1 /******************************************************************************
3 * Module Name: evgpeblk - GPE block creation and initialization.
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, 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 /* Local prototypes */
55 AcpiEvInstallGpeBlock (
56 ACPI_GPE_BLOCK_INFO *GpeBlock,
57 UINT32 InterruptNumber);
60 AcpiEvCreateGpeInfoBlocks (
61 ACPI_GPE_BLOCK_INFO *GpeBlock);
64 /*******************************************************************************
66 * FUNCTION: AcpiEvInstallGpeBlock
68 * PARAMETERS: GpeBlock - New GPE block
69 * InterruptNumber - Xrupt to be associated with this
74 * DESCRIPTION: Install new GPE block with mutex support
76 ******************************************************************************/
79 AcpiEvInstallGpeBlock (
80 ACPI_GPE_BLOCK_INFO *GpeBlock,
81 UINT32 InterruptNumber)
83 ACPI_GPE_BLOCK_INFO *NextGpeBlock;
84 ACPI_GPE_XRUPT_INFO *GpeXruptBlock;
89 ACPI_FUNCTION_TRACE (EvInstallGpeBlock);
92 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
93 if (ACPI_FAILURE (Status))
95 return_ACPI_STATUS (Status);
98 GpeXruptBlock = AcpiEvGetGpeXruptBlock (InterruptNumber);
101 Status = AE_NO_MEMORY;
105 /* Install the new block at the end of the list with lock */
107 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
108 if (GpeXruptBlock->GpeBlockListHead)
110 NextGpeBlock = GpeXruptBlock->GpeBlockListHead;
111 while (NextGpeBlock->Next)
113 NextGpeBlock = NextGpeBlock->Next;
116 NextGpeBlock->Next = GpeBlock;
117 GpeBlock->Previous = NextGpeBlock;
121 GpeXruptBlock->GpeBlockListHead = GpeBlock;
124 GpeBlock->XruptBlock = GpeXruptBlock;
125 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
129 Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
130 return_ACPI_STATUS (Status);
134 /*******************************************************************************
136 * FUNCTION: AcpiEvDeleteGpeBlock
138 * PARAMETERS: GpeBlock - Existing GPE block
142 * DESCRIPTION: Remove a GPE block
144 ******************************************************************************/
147 AcpiEvDeleteGpeBlock (
148 ACPI_GPE_BLOCK_INFO *GpeBlock)
151 ACPI_CPU_FLAGS Flags;
154 ACPI_FUNCTION_TRACE (EvInstallGpeBlock);
157 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
158 if (ACPI_FAILURE (Status))
160 return_ACPI_STATUS (Status);
163 /* Disable all GPEs in this block */
165 Status = AcpiHwDisableGpeBlock (GpeBlock->XruptBlock, GpeBlock, NULL);
167 if (!GpeBlock->Previous && !GpeBlock->Next)
169 /* This is the last GpeBlock on this interrupt */
171 Status = AcpiEvDeleteGpeXrupt (GpeBlock->XruptBlock);
172 if (ACPI_FAILURE (Status))
179 /* Remove the block on this interrupt with lock */
181 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
182 if (GpeBlock->Previous)
184 GpeBlock->Previous->Next = GpeBlock->Next;
188 GpeBlock->XruptBlock->GpeBlockListHead = GpeBlock->Next;
193 GpeBlock->Next->Previous = GpeBlock->Previous;
195 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
198 AcpiCurrentGpeCount -= GpeBlock->GpeCount;
200 /* Free the GpeBlock */
202 ACPI_FREE (GpeBlock->RegisterInfo);
203 ACPI_FREE (GpeBlock->EventInfo);
204 ACPI_FREE (GpeBlock);
207 Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
208 return_ACPI_STATUS (Status);
212 /*******************************************************************************
214 * FUNCTION: AcpiEvCreateGpeInfoBlocks
216 * PARAMETERS: GpeBlock - New GPE block
220 * DESCRIPTION: Create the RegisterInfo and EventInfo blocks for this GPE block
222 ******************************************************************************/
225 AcpiEvCreateGpeInfoBlocks (
226 ACPI_GPE_BLOCK_INFO *GpeBlock)
228 ACPI_GPE_REGISTER_INFO *GpeRegisterInfo = NULL;
229 ACPI_GPE_EVENT_INFO *GpeEventInfo = NULL;
230 ACPI_GPE_EVENT_INFO *ThisEvent;
231 ACPI_GPE_REGISTER_INFO *ThisRegister;
237 ACPI_FUNCTION_TRACE (EvCreateGpeInfoBlocks);
240 /* Allocate the GPE register information block */
242 GpeRegisterInfo = ACPI_ALLOCATE_ZEROED (
243 (ACPI_SIZE) GpeBlock->RegisterCount *
244 sizeof (ACPI_GPE_REGISTER_INFO));
245 if (!GpeRegisterInfo)
247 ACPI_ERROR ((AE_INFO,
248 "Could not allocate the GpeRegisterInfo table"));
249 return_ACPI_STATUS (AE_NO_MEMORY);
253 * Allocate the GPE EventInfo block. There are eight distinct GPEs
254 * per register. Initialization to zeros is sufficient.
256 GpeEventInfo = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) GpeBlock->GpeCount *
257 sizeof (ACPI_GPE_EVENT_INFO));
260 ACPI_ERROR ((AE_INFO,
261 "Could not allocate the GpeEventInfo table"));
262 Status = AE_NO_MEMORY;
266 /* Save the new Info arrays in the GPE block */
268 GpeBlock->RegisterInfo = GpeRegisterInfo;
269 GpeBlock->EventInfo = GpeEventInfo;
272 * Initialize the GPE Register and Event structures. A goal of these
273 * tables is to hide the fact that there are two separate GPE register
274 * sets in a given GPE hardware block, the status registers occupy the
275 * first half, and the enable registers occupy the second half.
277 ThisRegister = GpeRegisterInfo;
278 ThisEvent = GpeEventInfo;
280 for (i = 0; i < GpeBlock->RegisterCount; i++)
282 /* Init the RegisterInfo for this GPE register (8 GPEs) */
284 ThisRegister->BaseGpeNumber = (UINT8) (GpeBlock->BlockBaseNumber +
285 (i * ACPI_GPE_REGISTER_WIDTH));
287 ThisRegister->StatusAddress.Address =
288 GpeBlock->BlockAddress.Address + i;
290 ThisRegister->EnableAddress.Address =
291 GpeBlock->BlockAddress.Address + i + GpeBlock->RegisterCount;
293 ThisRegister->StatusAddress.SpaceId = GpeBlock->BlockAddress.SpaceId;
294 ThisRegister->EnableAddress.SpaceId = GpeBlock->BlockAddress.SpaceId;
295 ThisRegister->StatusAddress.BitWidth = ACPI_GPE_REGISTER_WIDTH;
296 ThisRegister->EnableAddress.BitWidth = ACPI_GPE_REGISTER_WIDTH;
297 ThisRegister->StatusAddress.BitOffset = 0;
298 ThisRegister->EnableAddress.BitOffset = 0;
300 /* Init the EventInfo for each GPE within this register */
302 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
304 ThisEvent->GpeNumber = (UINT8) (ThisRegister->BaseGpeNumber + j);
305 ThisEvent->RegisterInfo = ThisRegister;
309 /* Disable all GPEs within this register */
311 Status = AcpiHwWrite (0x00, &ThisRegister->EnableAddress);
312 if (ACPI_FAILURE (Status))
317 /* Clear any pending GPE events within this register */
319 Status = AcpiHwWrite (0xFF, &ThisRegister->StatusAddress);
320 if (ACPI_FAILURE (Status))
328 return_ACPI_STATUS (AE_OK);
334 ACPI_FREE (GpeRegisterInfo);
338 ACPI_FREE (GpeEventInfo);
341 return_ACPI_STATUS (Status);
345 /*******************************************************************************
347 * FUNCTION: AcpiEvCreateGpeBlock
349 * PARAMETERS: GpeDevice - Handle to the parent GPE block
350 * GpeBlockAddress - Address and SpaceID
351 * RegisterCount - Number of GPE register pairs in the block
352 * GpeBlockBaseNumber - Starting GPE number for the block
353 * InterruptNumber - H/W interrupt for the block
354 * ReturnGpeBlock - Where the new block descriptor is returned
358 * DESCRIPTION: Create and Install a block of GPE registers. All GPEs within
359 * the block are disabled at exit.
360 * Note: Assumes namespace is locked.
362 ******************************************************************************/
365 AcpiEvCreateGpeBlock (
366 ACPI_NAMESPACE_NODE *GpeDevice,
367 ACPI_GENERIC_ADDRESS *GpeBlockAddress,
368 UINT32 RegisterCount,
369 UINT8 GpeBlockBaseNumber,
370 UINT32 InterruptNumber,
371 ACPI_GPE_BLOCK_INFO **ReturnGpeBlock)
374 ACPI_GPE_BLOCK_INFO *GpeBlock;
375 ACPI_GPE_WALK_INFO WalkInfo;
378 ACPI_FUNCTION_TRACE (EvCreateGpeBlock);
383 return_ACPI_STATUS (AE_OK);
386 /* Allocate a new GPE block */
388 GpeBlock = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_BLOCK_INFO));
391 return_ACPI_STATUS (AE_NO_MEMORY);
394 /* Initialize the new GPE block */
396 GpeBlock->Node = GpeDevice;
397 GpeBlock->GpeCount = (UINT16) (RegisterCount * ACPI_GPE_REGISTER_WIDTH);
398 GpeBlock->Initialized = FALSE;
399 GpeBlock->RegisterCount = RegisterCount;
400 GpeBlock->BlockBaseNumber = GpeBlockBaseNumber;
402 ACPI_MEMCPY (&GpeBlock->BlockAddress, GpeBlockAddress,
403 sizeof (ACPI_GENERIC_ADDRESS));
406 * Create the RegisterInfo and EventInfo sub-structures
407 * Note: disables and clears all GPEs in the block
409 Status = AcpiEvCreateGpeInfoBlocks (GpeBlock);
410 if (ACPI_FAILURE (Status))
412 ACPI_FREE (GpeBlock);
413 return_ACPI_STATUS (Status);
416 /* Install the new block in the global lists */
418 Status = AcpiEvInstallGpeBlock (GpeBlock, InterruptNumber);
419 if (ACPI_FAILURE (Status))
421 ACPI_FREE (GpeBlock);
422 return_ACPI_STATUS (Status);
425 AcpiGbl_AllGpesInitialized = FALSE;
427 /* Find all GPE methods (_Lxx or_Exx) for this block */
429 WalkInfo.GpeBlock = GpeBlock;
430 WalkInfo.GpeDevice = GpeDevice;
431 WalkInfo.ExecuteByOwnerId = FALSE;
433 Status = AcpiNsWalkNamespace (ACPI_TYPE_METHOD, GpeDevice,
434 ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK,
435 AcpiEvMatchGpeMethod, NULL, &WalkInfo, NULL);
437 /* Return the new block */
441 (*ReturnGpeBlock) = GpeBlock;
444 ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
445 "GPE %02X to %02X [%4.4s] %u regs on int 0x%X\n",
446 (UINT32) GpeBlock->BlockBaseNumber,
447 (UINT32) (GpeBlock->BlockBaseNumber + (GpeBlock->GpeCount - 1)),
448 GpeDevice->Name.Ascii, GpeBlock->RegisterCount,
451 /* Update global count of currently available GPEs */
453 AcpiCurrentGpeCount += GpeBlock->GpeCount;
454 return_ACPI_STATUS (AE_OK);
458 /*******************************************************************************
460 * FUNCTION: AcpiEvInitializeGpeBlock
462 * PARAMETERS: ACPI_GPE_CALLBACK
466 * DESCRIPTION: Initialize and enable a GPE block. Enable GPEs that have
467 * associated methods.
468 * Note: Assumes namespace is locked.
470 ******************************************************************************/
473 AcpiEvInitializeGpeBlock (
474 ACPI_GPE_XRUPT_INFO *GpeXruptInfo,
475 ACPI_GPE_BLOCK_INFO *GpeBlock,
479 ACPI_GPE_EVENT_INFO *GpeEventInfo;
480 UINT32 GpeEnabledCount;
486 ACPI_FUNCTION_TRACE (EvInitializeGpeBlock);
490 * Ignore a null GPE block (e.g., if no GPE block 1 exists), and
491 * any GPE blocks that have been initialized already.
493 if (!GpeBlock || GpeBlock->Initialized)
495 return_ACPI_STATUS (AE_OK);
499 * Enable all GPEs that have a corresponding method and have the
500 * ACPI_GPE_CAN_WAKE flag unset. Any other GPEs within this block
501 * must be enabled via the acpi_enable_gpe() interface.
505 for (i = 0; i < GpeBlock->RegisterCount; i++)
507 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
509 /* Get the info block for this particular GPE */
511 GpeIndex = (i * ACPI_GPE_REGISTER_WIDTH) + j;
512 GpeEventInfo = &GpeBlock->EventInfo[GpeIndex];
515 * Ignore GPEs that have no corresponding _Lxx/_Exx method
516 * and GPEs that are used to wake the system
518 if (((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_NONE) ||
519 ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_HANDLER) ||
520 (GpeEventInfo->Flags & ACPI_GPE_CAN_WAKE))
525 Status = AcpiEvAddGpeReference (GpeEventInfo);
526 if (ACPI_FAILURE (Status))
528 ACPI_EXCEPTION ((AE_INFO, Status,
529 "Could not enable GPE 0x%02X",
530 GpeIndex + GpeBlock->BlockBaseNumber));
540 ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
541 "Enabled %u GPEs in this block\n", GpeEnabledCount));
544 GpeBlock->Initialized = TRUE;
545 return_ACPI_STATUS (AE_OK);