/******************************************************************************1*2* Module Name: evgpe - General Purpose Event handling and dispatch3*4*****************************************************************************/56/*7* Copyright (C) 2000 - 2011, Intel Corp.8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions, and the following disclaimer,15* without modification.16* 2. Redistributions in binary form must reproduce at minimum a disclaimer17* substantially similar to the "NO WARRANTY" disclaimer below18* ("Disclaimer") and any redistribution must be conditioned upon19* including a substantially similar Disclaimer requirement for further20* binary redistribution.21* 3. Neither the names of the above-listed copyright holders nor the names22* of any contributors may be used to endorse or promote products derived23* from this software without specific prior written permission.24*25* Alternatively, this software may be distributed under the terms of the26* GNU General Public License ("GPL") version 2 as published by the Free27* Software Foundation.28*29* NO WARRANTY30* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS31* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT32* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR33* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT34* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL35* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS36* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)37* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,38* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING39* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE40* POSSIBILITY OF SUCH DAMAGES.41*/4243#include <acpi/acpi.h>44#include "accommon.h"45#include "acevents.h"46#include "acnamesp.h"4748#define _COMPONENT ACPI_EVENTS49ACPI_MODULE_NAME("evgpe")5051/* Local prototypes */52static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);5354static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);5556/*******************************************************************************57*58* FUNCTION: acpi_ev_update_gpe_enable_mask59*60* PARAMETERS: gpe_event_info - GPE to update61*62* RETURN: Status63*64* DESCRIPTION: Updates GPE register enable mask based upon whether there are65* runtime references to this GPE66*67******************************************************************************/6869acpi_status70acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)71{72struct acpi_gpe_register_info *gpe_register_info;73u32 register_bit;7475ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);7677gpe_register_info = gpe_event_info->register_info;78if (!gpe_register_info) {79return_ACPI_STATUS(AE_NOT_EXIST);80}8182register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info,83gpe_register_info);8485/* Clear the run bit up front */8687ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);8889/* Set the mask bit only if there are references to this GPE */9091if (gpe_event_info->runtime_count) {92ACPI_SET_BIT(gpe_register_info->enable_for_run, (u8)register_bit);93}9495return_ACPI_STATUS(AE_OK);96}9798/*******************************************************************************99*100* FUNCTION: acpi_ev_enable_gpe101*102* PARAMETERS: gpe_event_info - GPE to enable103*104* RETURN: Status105*106* DESCRIPTION: Clear a GPE of stale events and enable it.107*108******************************************************************************/109acpi_status110acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)111{112acpi_status status;113114ACPI_FUNCTION_TRACE(ev_enable_gpe);115116/*117* We will only allow a GPE to be enabled if it has either an associated118* method (_Lxx/_Exx) or a handler, or is using the implicit notify119* feature. Otherwise, the GPE will be immediately disabled by120* acpi_ev_gpe_dispatch the first time it fires.121*/122if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==123ACPI_GPE_DISPATCH_NONE) {124return_ACPI_STATUS(AE_NO_HANDLER);125}126127/* Clear the GPE (of stale events) */128status = acpi_hw_clear_gpe(gpe_event_info);129if (ACPI_FAILURE(status)) {130return_ACPI_STATUS(status);131}132133/* Enable the requested GPE */134status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);135136return_ACPI_STATUS(status);137}138139140/*******************************************************************************141*142* FUNCTION: acpi_ev_add_gpe_reference143*144* PARAMETERS: gpe_event_info - Add a reference to this GPE145*146* RETURN: Status147*148* DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is149* hardware-enabled.150*151******************************************************************************/152153acpi_status acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)154{155acpi_status status = AE_OK;156157ACPI_FUNCTION_TRACE(ev_add_gpe_reference);158159if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {160return_ACPI_STATUS(AE_LIMIT);161}162163gpe_event_info->runtime_count++;164if (gpe_event_info->runtime_count == 1) {165166/* Enable on first reference */167168status = acpi_ev_update_gpe_enable_mask(gpe_event_info);169if (ACPI_SUCCESS(status)) {170status = acpi_ev_enable_gpe(gpe_event_info);171}172173if (ACPI_FAILURE(status)) {174gpe_event_info->runtime_count--;175}176}177178return_ACPI_STATUS(status);179}180181/*******************************************************************************182*183* FUNCTION: acpi_ev_remove_gpe_reference184*185* PARAMETERS: gpe_event_info - Remove a reference to this GPE186*187* RETURN: Status188*189* DESCRIPTION: Remove a reference to a GPE. When the last reference is190* removed, the GPE is hardware-disabled.191*192******************************************************************************/193194acpi_status acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)195{196acpi_status status = AE_OK;197198ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);199200if (!gpe_event_info->runtime_count) {201return_ACPI_STATUS(AE_LIMIT);202}203204gpe_event_info->runtime_count--;205if (!gpe_event_info->runtime_count) {206207/* Disable on last reference */208209status = acpi_ev_update_gpe_enable_mask(gpe_event_info);210if (ACPI_SUCCESS(status)) {211status = acpi_hw_low_set_gpe(gpe_event_info,212ACPI_GPE_DISABLE);213}214215if (ACPI_FAILURE(status)) {216gpe_event_info->runtime_count++;217}218}219220return_ACPI_STATUS(status);221}222223/*******************************************************************************224*225* FUNCTION: acpi_ev_low_get_gpe_info226*227* PARAMETERS: gpe_number - Raw GPE number228* gpe_block - A GPE info block229*230* RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number231* is not within the specified GPE block)232*233* DESCRIPTION: Returns the event_info struct associated with this GPE. This is234* the low-level implementation of ev_get_gpe_event_info.235*236******************************************************************************/237238struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,239struct acpi_gpe_block_info240*gpe_block)241{242u32 gpe_index;243244/*245* Validate that the gpe_number is within the specified gpe_block.246* (Two steps)247*/248if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {249return (NULL);250}251252gpe_index = gpe_number - gpe_block->block_base_number;253if (gpe_index >= gpe_block->gpe_count) {254return (NULL);255}256257return (&gpe_block->event_info[gpe_index]);258}259260261/*******************************************************************************262*263* FUNCTION: acpi_ev_get_gpe_event_info264*265* PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1266* gpe_number - Raw GPE number267*268* RETURN: A GPE event_info struct. NULL if not a valid GPE269*270* DESCRIPTION: Returns the event_info struct associated with this GPE.271* Validates the gpe_block and the gpe_number272*273* Should be called only when the GPE lists are semaphore locked274* and not subject to change.275*276******************************************************************************/277278struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,279u32 gpe_number)280{281union acpi_operand_object *obj_desc;282struct acpi_gpe_event_info *gpe_info;283u32 i;284285ACPI_FUNCTION_ENTRY();286287/* A NULL gpe_device means use the FADT-defined GPE block(s) */288289if (!gpe_device) {290291/* Examine GPE Block 0 and 1 (These blocks are permanent) */292293for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {294gpe_info = acpi_ev_low_get_gpe_info(gpe_number,295acpi_gbl_gpe_fadt_blocks296[i]);297if (gpe_info) {298return (gpe_info);299}300}301302/* The gpe_number was not in the range of either FADT GPE block */303304return (NULL);305}306307/* A Non-NULL gpe_device means this is a GPE Block Device */308309obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)310gpe_device);311if (!obj_desc || !obj_desc->device.gpe_block) {312return (NULL);313}314315return (acpi_ev_low_get_gpe_info316(gpe_number, obj_desc->device.gpe_block));317}318319/*******************************************************************************320*321* FUNCTION: acpi_ev_gpe_detect322*323* PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.324* Can have multiple GPE blocks attached.325*326* RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED327*328* DESCRIPTION: Detect if any GP events have occurred. This function is329* executed at interrupt level.330*331******************************************************************************/332333u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)334{335acpi_status status;336struct acpi_gpe_block_info *gpe_block;337struct acpi_gpe_register_info *gpe_register_info;338u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;339u8 enabled_status_byte;340u32 status_reg;341u32 enable_reg;342acpi_cpu_flags flags;343u32 i;344u32 j;345346ACPI_FUNCTION_NAME(ev_gpe_detect);347348/* Check for the case where there are no GPEs */349350if (!gpe_xrupt_list) {351return (int_status);352}353354/*355* We need to obtain the GPE lock for both the data structs and registers356* Note: Not necessary to obtain the hardware lock, since the GPE357* registers are owned by the gpe_lock.358*/359flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);360361/* Examine all GPE blocks attached to this interrupt level */362363gpe_block = gpe_xrupt_list->gpe_block_list_head;364while (gpe_block) {365/*366* Read all of the 8-bit GPE status and enable registers in this GPE367* block, saving all of them. Find all currently active GP events.368*/369for (i = 0; i < gpe_block->register_count; i++) {370371/* Get the next status/enable pair */372373gpe_register_info = &gpe_block->register_info[i];374375/*376* Optimization: If there are no GPEs enabled within this377* register, we can safely ignore the entire register.378*/379if (!(gpe_register_info->enable_for_run |380gpe_register_info->enable_for_wake)) {381continue;382}383384/* Read the Status Register */385386status =387acpi_hw_read(&status_reg,388&gpe_register_info->status_address);389if (ACPI_FAILURE(status)) {390goto unlock_and_exit;391}392393/* Read the Enable Register */394395status =396acpi_hw_read(&enable_reg,397&gpe_register_info->enable_address);398if (ACPI_FAILURE(status)) {399goto unlock_and_exit;400}401402ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,403"Read GPE Register at GPE%02X: Status=%02X, Enable=%02X\n",404gpe_register_info->base_gpe_number,405status_reg, enable_reg));406407/* Check if there is anything active at all in this register */408409enabled_status_byte = (u8) (status_reg & enable_reg);410if (!enabled_status_byte) {411412/* No active GPEs in this register, move on */413414continue;415}416417/* Now look at the individual GPEs in this byte register */418419for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {420421/* Examine one GPE bit */422423if (enabled_status_byte & (1 << j)) {424/*425* Found an active GPE. Dispatch the event to a handler426* or method.427*/428int_status |=429acpi_ev_gpe_dispatch(gpe_block->430node,431&gpe_block->432event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);433}434}435}436437gpe_block = gpe_block->next;438}439440unlock_and_exit:441442acpi_os_release_lock(acpi_gbl_gpe_lock, flags);443return (int_status);444}445446/*******************************************************************************447*448* FUNCTION: acpi_ev_asynch_execute_gpe_method449*450* PARAMETERS: Context (gpe_event_info) - Info for this GPE451*452* RETURN: None453*454* DESCRIPTION: Perform the actual execution of a GPE control method. This455* function is called from an invocation of acpi_os_execute and456* therefore does NOT execute at interrupt level - so that457* the control method itself is not executed in the context of458* an interrupt handler.459*460******************************************************************************/461462static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)463{464struct acpi_gpe_event_info *gpe_event_info = context;465acpi_status status;466struct acpi_gpe_event_info *local_gpe_event_info;467struct acpi_evaluate_info *info;468struct acpi_gpe_notify_object *notify_object;469470ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);471472/* Allocate a local GPE block */473474local_gpe_event_info =475ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_event_info));476if (!local_gpe_event_info) {477ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "while handling a GPE"));478return_VOID;479}480481status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);482if (ACPI_FAILURE(status)) {483ACPI_FREE(local_gpe_event_info);484return_VOID;485}486487/* Must revalidate the gpe_number/gpe_block */488489if (!acpi_ev_valid_gpe_event(gpe_event_info)) {490status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);491ACPI_FREE(local_gpe_event_info);492return_VOID;493}494495/*496* Take a snapshot of the GPE info for this level - we copy the info to497* prevent a race condition with remove_handler/remove_block.498*/499ACPI_MEMCPY(local_gpe_event_info, gpe_event_info,500sizeof(struct acpi_gpe_event_info));501502status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);503if (ACPI_FAILURE(status)) {504return_VOID;505}506507/* Do the correct dispatch - normal method or implicit notify */508509switch (local_gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {510case ACPI_GPE_DISPATCH_NOTIFY:511512/*513* Implicit notify.514* Dispatch a DEVICE_WAKE notify to the appropriate handler.515* NOTE: the request is queued for execution after this method516* completes. The notify handlers are NOT invoked synchronously517* from this thread -- because handlers may in turn run other518* control methods.519*/520status = acpi_ev_queue_notify_request(521local_gpe_event_info->dispatch.device.node,522ACPI_NOTIFY_DEVICE_WAKE);523524notify_object = local_gpe_event_info->dispatch.device.next;525while (ACPI_SUCCESS(status) && notify_object) {526status = acpi_ev_queue_notify_request(527notify_object->node,528ACPI_NOTIFY_DEVICE_WAKE);529notify_object = notify_object->next;530}531532break;533534case ACPI_GPE_DISPATCH_METHOD:535536/* Allocate the evaluation information block */537538info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));539if (!info) {540status = AE_NO_MEMORY;541} else {542/*543* Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx544* control method that corresponds to this GPE545*/546info->prefix_node =547local_gpe_event_info->dispatch.method_node;548info->flags = ACPI_IGNORE_RETURN_VALUE;549550status = acpi_ns_evaluate(info);551ACPI_FREE(info);552}553554if (ACPI_FAILURE(status)) {555ACPI_EXCEPTION((AE_INFO, status,556"while evaluating GPE method [%4.4s]",557acpi_ut_get_node_name558(local_gpe_event_info->dispatch.559method_node)));560}561562break;563564default:565return_VOID; /* Should never happen */566}567568/* Defer enabling of GPE until all notify handlers are done */569570status = acpi_os_execute(OSL_NOTIFY_HANDLER,571acpi_ev_asynch_enable_gpe,572local_gpe_event_info);573if (ACPI_FAILURE(status)) {574ACPI_FREE(local_gpe_event_info);575}576return_VOID;577}578579580/*******************************************************************************581*582* FUNCTION: acpi_ev_asynch_enable_gpe583*584* PARAMETERS: Context (gpe_event_info) - Info for this GPE585* Callback from acpi_os_execute586*587* RETURN: None588*589* DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to590* complete (i.e., finish execution of Notify)591*592******************************************************************************/593594static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)595{596struct acpi_gpe_event_info *gpe_event_info = context;597598(void)acpi_ev_finish_gpe(gpe_event_info);599600ACPI_FREE(gpe_event_info);601return;602}603604605/*******************************************************************************606*607* FUNCTION: acpi_ev_finish_gpe608*609* PARAMETERS: gpe_event_info - Info for this GPE610*611* RETURN: Status612*613* DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution614* of a GPE method or a synchronous or asynchronous GPE handler.615*616******************************************************************************/617618acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)619{620acpi_status status;621622if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==623ACPI_GPE_LEVEL_TRIGGERED) {624/*625* GPE is level-triggered, we clear the GPE status bit after626* handling the event.627*/628status = acpi_hw_clear_gpe(gpe_event_info);629if (ACPI_FAILURE(status)) {630return (status);631}632}633634/*635* Enable this GPE, conditionally. This means that the GPE will636* only be physically enabled if the enable_for_run bit is set637* in the event_info.638*/639(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);640return (AE_OK);641}642643644/*******************************************************************************645*646* FUNCTION: acpi_ev_gpe_dispatch647*648* PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1649* gpe_event_info - Info for this GPE650* gpe_number - Number relative to the parent GPE block651*652* RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED653*654* DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)655* or method (e.g. _Lxx/_Exx) handler.656*657* This function executes at interrupt level.658*659******************************************************************************/660661u32662acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,663struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)664{665acpi_status status;666u32 return_value;667668ACPI_FUNCTION_TRACE(ev_gpe_dispatch);669670/* Invoke global event handler if present */671672acpi_gpe_count++;673if (acpi_gbl_global_event_handler) {674acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE, gpe_device,675gpe_number,676acpi_gbl_global_event_handler_context);677}678679/*680* If edge-triggered, clear the GPE status bit now. Note that681* level-triggered events are cleared after the GPE is serviced.682*/683if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==684ACPI_GPE_EDGE_TRIGGERED) {685status = acpi_hw_clear_gpe(gpe_event_info);686if (ACPI_FAILURE(status)) {687ACPI_EXCEPTION((AE_INFO, status,688"Unable to clear GPE%02X", gpe_number));689return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);690}691}692693/*694* Always disable the GPE so that it does not keep firing before695* any asynchronous activity completes (either from the execution696* of a GPE method or an asynchronous GPE handler.)697*698* If there is no handler or method to run, just disable the699* GPE and leave it disabled permanently to prevent further such700* pointless events from firing.701*/702status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);703if (ACPI_FAILURE(status)) {704ACPI_EXCEPTION((AE_INFO, status,705"Unable to disable GPE%02X", gpe_number));706return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);707}708709/*710* Dispatch the GPE to either an installed handler or the control711* method associated with this GPE (_Lxx or _Exx). If a handler712* exists, we invoke it and do not attempt to run the method.713* If there is neither a handler nor a method, leave the GPE714* disabled.715*/716switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {717case ACPI_GPE_DISPATCH_HANDLER:718719/* Invoke the installed handler (at interrupt level) */720721return_value =722gpe_event_info->dispatch.handler->address(gpe_device,723gpe_number,724gpe_event_info->725dispatch.handler->726context);727728/* If requested, clear (if level-triggered) and reenable the GPE */729730if (return_value & ACPI_REENABLE_GPE) {731(void)acpi_ev_finish_gpe(gpe_event_info);732}733break;734735case ACPI_GPE_DISPATCH_METHOD:736case ACPI_GPE_DISPATCH_NOTIFY:737738/*739* Execute the method associated with the GPE740* NOTE: Level-triggered GPEs are cleared after the method completes.741*/742status = acpi_os_execute(OSL_GPE_HANDLER,743acpi_ev_asynch_execute_gpe_method,744gpe_event_info);745if (ACPI_FAILURE(status)) {746ACPI_EXCEPTION((AE_INFO, status,747"Unable to queue handler for GPE%2X - event disabled",748gpe_number));749}750break;751752default:753754/*755* No handler or method to run!756* 03/2010: This case should no longer be possible. We will not allow757* a GPE to be enabled if it has no handler or method.758*/759ACPI_ERROR((AE_INFO,760"No handler or method for GPE%02X, disabling event",761gpe_number));762763break;764}765766return_UINT32(ACPI_INTERRUPT_HANDLED);767}768769770