/******************************************************************************1*2* Module Name: evregion - ACPI address_space (op_region) handler 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"47#include "acinterp.h"4849#define _COMPONENT ACPI_EVENTS50ACPI_MODULE_NAME("evregion")5152/* Local prototypes */53static u854acpi_ev_has_default_handler(struct acpi_namespace_node *node,55acpi_adr_space_type space_id);5657static void acpi_ev_orphan_ec_reg_method(void);5859static acpi_status60acpi_ev_reg_run(acpi_handle obj_handle,61u32 level, void *context, void **return_value);6263static acpi_status64acpi_ev_install_handler(acpi_handle obj_handle,65u32 level, void *context, void **return_value);6667/* These are the address spaces that will get default handlers */6869#define ACPI_NUM_DEFAULT_SPACES 47071static u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {72ACPI_ADR_SPACE_SYSTEM_MEMORY,73ACPI_ADR_SPACE_SYSTEM_IO,74ACPI_ADR_SPACE_PCI_CONFIG,75ACPI_ADR_SPACE_DATA_TABLE76};7778/*******************************************************************************79*80* FUNCTION: acpi_ev_install_region_handlers81*82* PARAMETERS: None83*84* RETURN: Status85*86* DESCRIPTION: Installs the core subsystem default address space handlers.87*88******************************************************************************/8990acpi_status acpi_ev_install_region_handlers(void)91{92acpi_status status;93u32 i;9495ACPI_FUNCTION_TRACE(ev_install_region_handlers);9697status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);98if (ACPI_FAILURE(status)) {99return_ACPI_STATUS(status);100}101102/*103* All address spaces (PCI Config, EC, SMBus) are scope dependent and104* registration must occur for a specific device.105*106* In the case of the system memory and IO address spaces there is107* currently no device associated with the address space. For these we108* use the root.109*110* We install the default PCI config space handler at the root so that111* this space is immediately available even though the we have not112* enumerated all the PCI Root Buses yet. This is to conform to the ACPI113* specification which states that the PCI config space must be always114* available -- even though we are nowhere near ready to find the PCI root115* buses at this point.116*117* NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler118* has already been installed (via acpi_install_address_space_handler).119* Similar for AE_SAME_HANDLER.120*/121for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {122status = acpi_ev_install_space_handler(acpi_gbl_root_node,123acpi_gbl_default_address_spaces124[i],125ACPI_DEFAULT_HANDLER,126NULL, NULL);127switch (status) {128case AE_OK:129case AE_SAME_HANDLER:130case AE_ALREADY_EXISTS:131132/* These exceptions are all OK */133134status = AE_OK;135break;136137default:138139goto unlock_and_exit;140}141}142143unlock_and_exit:144(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);145return_ACPI_STATUS(status);146}147148/*******************************************************************************149*150* FUNCTION: acpi_ev_has_default_handler151*152* PARAMETERS: Node - Namespace node for the device153* space_id - The address space ID154*155* RETURN: TRUE if default handler is installed, FALSE otherwise156*157* DESCRIPTION: Check if the default handler is installed for the requested158* space ID.159*160******************************************************************************/161162static u8163acpi_ev_has_default_handler(struct acpi_namespace_node *node,164acpi_adr_space_type space_id)165{166union acpi_operand_object *obj_desc;167union acpi_operand_object *handler_obj;168169/* Must have an existing internal object */170171obj_desc = acpi_ns_get_attached_object(node);172if (obj_desc) {173handler_obj = obj_desc->device.handler;174175/* Walk the linked list of handlers for this object */176177while (handler_obj) {178if (handler_obj->address_space.space_id == space_id) {179if (handler_obj->address_space.handler_flags &180ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {181return (TRUE);182}183}184185handler_obj = handler_obj->address_space.next;186}187}188189return (FALSE);190}191192/*******************************************************************************193*194* FUNCTION: acpi_ev_initialize_op_regions195*196* PARAMETERS: None197*198* RETURN: Status199*200* DESCRIPTION: Execute _REG methods for all Operation Regions that have201* an installed default region handler.202*203******************************************************************************/204205acpi_status acpi_ev_initialize_op_regions(void)206{207acpi_status status;208u32 i;209210ACPI_FUNCTION_TRACE(ev_initialize_op_regions);211212status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);213if (ACPI_FAILURE(status)) {214return_ACPI_STATUS(status);215}216217/* Run the _REG methods for op_regions in each default address space */218219for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {220/*221* Make sure the installed handler is the DEFAULT handler. If not the222* default, the _REG methods will have already been run (when the223* handler was installed)224*/225if (acpi_ev_has_default_handler(acpi_gbl_root_node,226acpi_gbl_default_address_spaces227[i])) {228status =229acpi_ev_execute_reg_methods(acpi_gbl_root_node,230acpi_gbl_default_address_spaces231[i]);232}233}234235acpi_gbl_reg_methods_executed = TRUE;236237(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);238return_ACPI_STATUS(status);239}240241/*******************************************************************************242*243* FUNCTION: acpi_ev_execute_reg_method244*245* PARAMETERS: region_obj - Region object246* Function - Passed to _REG: On (1) or Off (0)247*248* RETURN: Status249*250* DESCRIPTION: Execute _REG method for a region251*252******************************************************************************/253254acpi_status255acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)256{257struct acpi_evaluate_info *info;258union acpi_operand_object *args[3];259union acpi_operand_object *region_obj2;260acpi_status status;261262ACPI_FUNCTION_TRACE(ev_execute_reg_method);263264region_obj2 = acpi_ns_get_secondary_object(region_obj);265if (!region_obj2) {266return_ACPI_STATUS(AE_NOT_EXIST);267}268269if (region_obj2->extra.method_REG == NULL) {270return_ACPI_STATUS(AE_OK);271}272273/* Allocate and initialize the evaluation information block */274275info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));276if (!info) {277return_ACPI_STATUS(AE_NO_MEMORY);278}279280info->prefix_node = region_obj2->extra.method_REG;281info->pathname = NULL;282info->parameters = args;283info->flags = ACPI_IGNORE_RETURN_VALUE;284285/*286* The _REG method has two arguments:287*288* Arg0 - Integer:289* Operation region space ID Same value as region_obj->Region.space_id290*291* Arg1 - Integer:292* connection status 1 for connecting the handler, 0 for disconnecting293* the handler (Passed as a parameter)294*/295args[0] =296acpi_ut_create_integer_object((u64) region_obj->region.space_id);297if (!args[0]) {298status = AE_NO_MEMORY;299goto cleanup1;300}301302args[1] = acpi_ut_create_integer_object((u64) function);303if (!args[1]) {304status = AE_NO_MEMORY;305goto cleanup2;306}307308args[2] = NULL; /* Terminate list */309310/* Execute the method, no return value */311312ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname313(ACPI_TYPE_METHOD, info->prefix_node, NULL));314315status = acpi_ns_evaluate(info);316acpi_ut_remove_reference(args[1]);317318cleanup2:319acpi_ut_remove_reference(args[0]);320321cleanup1:322ACPI_FREE(info);323return_ACPI_STATUS(status);324}325326/*******************************************************************************327*328* FUNCTION: acpi_ev_address_space_dispatch329*330* PARAMETERS: region_obj - Internal region object331* Function - Read or Write operation332* region_offset - Where in the region to read or write333* bit_width - Field width in bits (8, 16, 32, or 64)334* Value - Pointer to in or out value, must be335* a full 64-bit integer336*337* RETURN: Status338*339* DESCRIPTION: Dispatch an address space or operation region access to340* a previously installed handler.341*342******************************************************************************/343344acpi_status345acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,346u32 function,347u32 region_offset, u32 bit_width, u64 *value)348{349acpi_status status;350acpi_adr_space_handler handler;351acpi_adr_space_setup region_setup;352union acpi_operand_object *handler_desc;353union acpi_operand_object *region_obj2;354void *region_context = NULL;355356ACPI_FUNCTION_TRACE(ev_address_space_dispatch);357358region_obj2 = acpi_ns_get_secondary_object(region_obj);359if (!region_obj2) {360return_ACPI_STATUS(AE_NOT_EXIST);361}362363/* Ensure that there is a handler associated with this region */364365handler_desc = region_obj->region.handler;366if (!handler_desc) {367ACPI_ERROR((AE_INFO,368"No handler for Region [%4.4s] (%p) [%s]",369acpi_ut_get_node_name(region_obj->region.node),370region_obj,371acpi_ut_get_region_name(region_obj->region.372space_id)));373374return_ACPI_STATUS(AE_NOT_EXIST);375}376377/*378* It may be the case that the region has never been initialized.379* Some types of regions require special init code380*/381if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {382383/* This region has not been initialized yet, do it */384385region_setup = handler_desc->address_space.setup;386if (!region_setup) {387388/* No initialization routine, exit with error */389390ACPI_ERROR((AE_INFO,391"No init routine for region(%p) [%s]",392region_obj,393acpi_ut_get_region_name(region_obj->region.394space_id)));395return_ACPI_STATUS(AE_NOT_EXIST);396}397398/*399* We must exit the interpreter because the region setup will400* potentially execute control methods (for example, the _REG method401* for this region)402*/403acpi_ex_exit_interpreter();404405status = region_setup(region_obj, ACPI_REGION_ACTIVATE,406handler_desc->address_space.context,407®ion_context);408409/* Re-enter the interpreter */410411acpi_ex_enter_interpreter();412413/* Check for failure of the Region Setup */414415if (ACPI_FAILURE(status)) {416ACPI_EXCEPTION((AE_INFO, status,417"During region initialization: [%s]",418acpi_ut_get_region_name(region_obj->419region.420space_id)));421return_ACPI_STATUS(status);422}423424/* Region initialization may have been completed by region_setup */425426if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {427region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;428429if (region_obj2->extra.region_context) {430431/* The handler for this region was already installed */432433ACPI_FREE(region_context);434} else {435/*436* Save the returned context for use in all accesses to437* this particular region438*/439region_obj2->extra.region_context =440region_context;441}442}443}444445/* We have everything we need, we can invoke the address space handler */446447handler = handler_desc->address_space.handler;448449ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,450"Handler %p (@%p) Address %8.8X%8.8X [%s]\n",451®ion_obj->region.handler->address_space, handler,452ACPI_FORMAT_NATIVE_UINT(region_obj->region.address +453region_offset),454acpi_ut_get_region_name(region_obj->region.455space_id)));456457if (!(handler_desc->address_space.handler_flags &458ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {459/*460* For handlers other than the default (supplied) handlers, we must461* exit the interpreter because the handler *might* block -- we don't462* know what it will do, so we can't hold the lock on the intepreter.463*/464acpi_ex_exit_interpreter();465}466467/* Call the handler */468469status = handler(function,470(region_obj->region.address + region_offset),471bit_width, value, handler_desc->address_space.context,472region_obj2->extra.region_context);473474if (ACPI_FAILURE(status)) {475ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",476acpi_ut_get_region_name(region_obj->region.477space_id)));478}479480if (!(handler_desc->address_space.handler_flags &481ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {482/*483* We just returned from a non-default handler, we must re-enter the484* interpreter485*/486acpi_ex_enter_interpreter();487}488489return_ACPI_STATUS(status);490}491492/*******************************************************************************493*494* FUNCTION: acpi_ev_detach_region495*496* PARAMETERS: region_obj - Region Object497* acpi_ns_is_locked - Namespace Region Already Locked?498*499* RETURN: None500*501* DESCRIPTION: Break the association between the handler and the region502* this is a two way association.503*504******************************************************************************/505506void507acpi_ev_detach_region(union acpi_operand_object *region_obj,508u8 acpi_ns_is_locked)509{510union acpi_operand_object *handler_obj;511union acpi_operand_object *obj_desc;512union acpi_operand_object **last_obj_ptr;513acpi_adr_space_setup region_setup;514void **region_context;515union acpi_operand_object *region_obj2;516acpi_status status;517518ACPI_FUNCTION_TRACE(ev_detach_region);519520region_obj2 = acpi_ns_get_secondary_object(region_obj);521if (!region_obj2) {522return_VOID;523}524region_context = ®ion_obj2->extra.region_context;525526/* Get the address handler from the region object */527528handler_obj = region_obj->region.handler;529if (!handler_obj) {530531/* This region has no handler, all done */532533return_VOID;534}535536/* Find this region in the handler's list */537538obj_desc = handler_obj->address_space.region_list;539last_obj_ptr = &handler_obj->address_space.region_list;540541while (obj_desc) {542543/* Is this the correct Region? */544545if (obj_desc == region_obj) {546ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,547"Removing Region %p from address handler %p\n",548region_obj, handler_obj));549550/* This is it, remove it from the handler's list */551552*last_obj_ptr = obj_desc->region.next;553obj_desc->region.next = NULL; /* Must clear field */554555if (acpi_ns_is_locked) {556status =557acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);558if (ACPI_FAILURE(status)) {559return_VOID;560}561}562563/* Now stop region accesses by executing the _REG method */564565status =566acpi_ev_execute_reg_method(region_obj,567ACPI_REG_DISCONNECT);568if (ACPI_FAILURE(status)) {569ACPI_EXCEPTION((AE_INFO, status,570"from region _REG, [%s]",571acpi_ut_get_region_name572(region_obj->region.space_id)));573}574575if (acpi_ns_is_locked) {576status =577acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);578if (ACPI_FAILURE(status)) {579return_VOID;580}581}582583/*584* If the region has been activated, call the setup handler with585* the deactivate notification586*/587if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {588region_setup = handler_obj->address_space.setup;589status =590region_setup(region_obj,591ACPI_REGION_DEACTIVATE,592handler_obj->address_space.593context, region_context);594595/* Init routine may fail, Just ignore errors */596597if (ACPI_FAILURE(status)) {598ACPI_EXCEPTION((AE_INFO, status,599"from region handler - deactivate, [%s]",600acpi_ut_get_region_name601(region_obj->region.602space_id)));603}604605region_obj->region.flags &=606~(AOPOBJ_SETUP_COMPLETE);607}608609/*610* Remove handler reference in the region611*612* NOTE: this doesn't mean that the region goes away, the region613* is just inaccessible as indicated to the _REG method614*615* If the region is on the handler's list, this must be the616* region's handler617*/618region_obj->region.handler = NULL;619acpi_ut_remove_reference(handler_obj);620621return_VOID;622}623624/* Walk the linked list of handlers */625626last_obj_ptr = &obj_desc->region.next;627obj_desc = obj_desc->region.next;628}629630/* If we get here, the region was not in the handler's region list */631632ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,633"Cannot remove region %p from address handler %p\n",634region_obj, handler_obj));635636return_VOID;637}638639/*******************************************************************************640*641* FUNCTION: acpi_ev_attach_region642*643* PARAMETERS: handler_obj - Handler Object644* region_obj - Region Object645* acpi_ns_is_locked - Namespace Region Already Locked?646*647* RETURN: None648*649* DESCRIPTION: Create the association between the handler and the region650* this is a two way association.651*652******************************************************************************/653654acpi_status655acpi_ev_attach_region(union acpi_operand_object *handler_obj,656union acpi_operand_object *region_obj,657u8 acpi_ns_is_locked)658{659660ACPI_FUNCTION_TRACE(ev_attach_region);661662ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,663"Adding Region [%4.4s] %p to address handler %p [%s]\n",664acpi_ut_get_node_name(region_obj->region.node),665region_obj, handler_obj,666acpi_ut_get_region_name(region_obj->region.667space_id)));668669/* Link this region to the front of the handler's list */670671region_obj->region.next = handler_obj->address_space.region_list;672handler_obj->address_space.region_list = region_obj;673674/* Install the region's handler */675676if (region_obj->region.handler) {677return_ACPI_STATUS(AE_ALREADY_EXISTS);678}679680region_obj->region.handler = handler_obj;681acpi_ut_add_reference(handler_obj);682683return_ACPI_STATUS(AE_OK);684}685686/*******************************************************************************687*688* FUNCTION: acpi_ev_install_handler689*690* PARAMETERS: walk_namespace callback691*692* DESCRIPTION: This routine installs an address handler into objects that are693* of type Region or Device.694*695* If the Object is a Device, and the device has a handler of696* the same type then the search is terminated in that branch.697*698* This is because the existing handler is closer in proximity699* to any more regions than the one we are trying to install.700*701******************************************************************************/702703static acpi_status704acpi_ev_install_handler(acpi_handle obj_handle,705u32 level, void *context, void **return_value)706{707union acpi_operand_object *handler_obj;708union acpi_operand_object *next_handler_obj;709union acpi_operand_object *obj_desc;710struct acpi_namespace_node *node;711acpi_status status;712713ACPI_FUNCTION_NAME(ev_install_handler);714715handler_obj = (union acpi_operand_object *)context;716717/* Parameter validation */718719if (!handler_obj) {720return (AE_OK);721}722723/* Convert and validate the device handle */724725node = acpi_ns_validate_handle(obj_handle);726if (!node) {727return (AE_BAD_PARAMETER);728}729730/*731* We only care about regions and objects that are allowed to have732* address space handlers733*/734if ((node->type != ACPI_TYPE_DEVICE) &&735(node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {736return (AE_OK);737}738739/* Check for an existing internal object */740741obj_desc = acpi_ns_get_attached_object(node);742if (!obj_desc) {743744/* No object, just exit */745746return (AE_OK);747}748749/* Devices are handled different than regions */750751if (obj_desc->common.type == ACPI_TYPE_DEVICE) {752753/* Check if this Device already has a handler for this address space */754755next_handler_obj = obj_desc->device.handler;756while (next_handler_obj) {757758/* Found a handler, is it for the same address space? */759760if (next_handler_obj->address_space.space_id ==761handler_obj->address_space.space_id) {762ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,763"Found handler for region [%s] in device %p(%p) "764"handler %p\n",765acpi_ut_get_region_name766(handler_obj->address_space.767space_id), obj_desc,768next_handler_obj,769handler_obj));770771/*772* Since the object we found it on was a device, then it773* means that someone has already installed a handler for774* the branch of the namespace from this device on. Just775* bail out telling the walk routine to not traverse this776* branch. This preserves the scoping rule for handlers.777*/778return (AE_CTRL_DEPTH);779}780781/* Walk the linked list of handlers attached to this device */782783next_handler_obj = next_handler_obj->address_space.next;784}785786/*787* As long as the device didn't have a handler for this space we788* don't care about it. We just ignore it and proceed.789*/790return (AE_OK);791}792793/* Object is a Region */794795if (obj_desc->region.space_id != handler_obj->address_space.space_id) {796797/* This region is for a different address space, just ignore it */798799return (AE_OK);800}801802/*803* Now we have a region and it is for the handler's address space type.804*805* First disconnect region for any previous handler (if any)806*/807acpi_ev_detach_region(obj_desc, FALSE);808809/* Connect the region to the new handler */810811status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);812return (status);813}814815/*******************************************************************************816*817* FUNCTION: acpi_ev_install_space_handler818*819* PARAMETERS: Node - Namespace node for the device820* space_id - The address space ID821* Handler - Address of the handler822* Setup - Address of the setup function823* Context - Value passed to the handler on each access824*825* RETURN: Status826*827* DESCRIPTION: Install a handler for all op_regions of a given space_id.828* Assumes namespace is locked829*830******************************************************************************/831832acpi_status833acpi_ev_install_space_handler(struct acpi_namespace_node * node,834acpi_adr_space_type space_id,835acpi_adr_space_handler handler,836acpi_adr_space_setup setup, void *context)837{838union acpi_operand_object *obj_desc;839union acpi_operand_object *handler_obj;840acpi_status status;841acpi_object_type type;842u8 flags = 0;843844ACPI_FUNCTION_TRACE(ev_install_space_handler);845846/*847* This registration is valid for only the types below and the root. This848* is where the default handlers get placed.849*/850if ((node->type != ACPI_TYPE_DEVICE) &&851(node->type != ACPI_TYPE_PROCESSOR) &&852(node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {853status = AE_BAD_PARAMETER;854goto unlock_and_exit;855}856857if (handler == ACPI_DEFAULT_HANDLER) {858flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;859860switch (space_id) {861case ACPI_ADR_SPACE_SYSTEM_MEMORY:862handler = acpi_ex_system_memory_space_handler;863setup = acpi_ev_system_memory_region_setup;864break;865866case ACPI_ADR_SPACE_SYSTEM_IO:867handler = acpi_ex_system_io_space_handler;868setup = acpi_ev_io_space_region_setup;869break;870871case ACPI_ADR_SPACE_PCI_CONFIG:872handler = acpi_ex_pci_config_space_handler;873setup = acpi_ev_pci_config_region_setup;874break;875876case ACPI_ADR_SPACE_CMOS:877handler = acpi_ex_cmos_space_handler;878setup = acpi_ev_cmos_region_setup;879break;880881case ACPI_ADR_SPACE_PCI_BAR_TARGET:882handler = acpi_ex_pci_bar_space_handler;883setup = acpi_ev_pci_bar_region_setup;884break;885886case ACPI_ADR_SPACE_DATA_TABLE:887handler = acpi_ex_data_table_space_handler;888setup = NULL;889break;890891default:892status = AE_BAD_PARAMETER;893goto unlock_and_exit;894}895}896897/* If the caller hasn't specified a setup routine, use the default */898899if (!setup) {900setup = acpi_ev_default_region_setup;901}902903/* Check for an existing internal object */904905obj_desc = acpi_ns_get_attached_object(node);906if (obj_desc) {907/*908* The attached device object already exists. Make sure the handler909* is not already installed.910*/911handler_obj = obj_desc->device.handler;912913/* Walk the handler list for this device */914915while (handler_obj) {916917/* Same space_id indicates a handler already installed */918919if (handler_obj->address_space.space_id == space_id) {920if (handler_obj->address_space.handler ==921handler) {922/*923* It is (relatively) OK to attempt to install the SAME924* handler twice. This can easily happen with the925* PCI_Config space.926*/927status = AE_SAME_HANDLER;928goto unlock_and_exit;929} else {930/* A handler is already installed */931932status = AE_ALREADY_EXISTS;933}934goto unlock_and_exit;935}936937/* Walk the linked list of handlers */938939handler_obj = handler_obj->address_space.next;940}941} else {942ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,943"Creating object on Device %p while installing handler\n",944node));945946/* obj_desc does not exist, create one */947948if (node->type == ACPI_TYPE_ANY) {949type = ACPI_TYPE_DEVICE;950} else {951type = node->type;952}953954obj_desc = acpi_ut_create_internal_object(type);955if (!obj_desc) {956status = AE_NO_MEMORY;957goto unlock_and_exit;958}959960/* Init new descriptor */961962obj_desc->common.type = (u8) type;963964/* Attach the new object to the Node */965966status = acpi_ns_attach_object(node, obj_desc, type);967968/* Remove local reference to the object */969970acpi_ut_remove_reference(obj_desc);971972if (ACPI_FAILURE(status)) {973goto unlock_and_exit;974}975}976977ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,978"Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",979acpi_ut_get_region_name(space_id), space_id,980acpi_ut_get_node_name(node), node, obj_desc));981982/*983* Install the handler984*985* At this point there is no existing handler. Just allocate the object986* for the handler and link it into the list.987*/988handler_obj =989acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);990if (!handler_obj) {991status = AE_NO_MEMORY;992goto unlock_and_exit;993}994995/* Init handler obj */996997handler_obj->address_space.space_id = (u8) space_id;998handler_obj->address_space.handler_flags = flags;999handler_obj->address_space.region_list = NULL;1000handler_obj->address_space.node = node;1001handler_obj->address_space.handler = handler;1002handler_obj->address_space.context = context;1003handler_obj->address_space.setup = setup;10041005/* Install at head of Device.address_space list */10061007handler_obj->address_space.next = obj_desc->device.handler;10081009/*1010* The Device object is the first reference on the handler_obj.1011* Each region that uses the handler adds a reference.1012*/1013obj_desc->device.handler = handler_obj;10141015/*1016* Walk the namespace finding all of the regions this1017* handler will manage.1018*1019* Start at the device and search the branch toward1020* the leaf nodes until either the leaf is encountered or1021* a device is detected that has an address handler of the1022* same type.1023*1024* In either case, back up and search down the remainder1025* of the branch1026*/1027status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,1028ACPI_NS_WALK_UNLOCK,1029acpi_ev_install_handler, NULL,1030handler_obj, NULL);10311032unlock_and_exit:1033return_ACPI_STATUS(status);1034}10351036/*******************************************************************************1037*1038* FUNCTION: acpi_ev_execute_reg_methods1039*1040* PARAMETERS: Node - Namespace node for the device1041* space_id - The address space ID1042*1043* RETURN: Status1044*1045* DESCRIPTION: Run all _REG methods for the input Space ID;1046* Note: assumes namespace is locked, or system init time.1047*1048******************************************************************************/10491050acpi_status1051acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,1052acpi_adr_space_type space_id)1053{1054acpi_status status;10551056ACPI_FUNCTION_TRACE(ev_execute_reg_methods);10571058/*1059* Run all _REG methods for all Operation Regions for this space ID. This1060* is a separate walk in order to handle any interdependencies between1061* regions and _REG methods. (i.e. handlers must be installed for all1062* regions of this Space ID before we can run any _REG methods)1063*/1064status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,1065ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,1066NULL, &space_id, NULL);10671068/* Special case for EC: handle "orphan" _REG methods with no region */10691070if (space_id == ACPI_ADR_SPACE_EC) {1071acpi_ev_orphan_ec_reg_method();1072}10731074return_ACPI_STATUS(status);1075}10761077/*******************************************************************************1078*1079* FUNCTION: acpi_ev_reg_run1080*1081* PARAMETERS: walk_namespace callback1082*1083* DESCRIPTION: Run _REG method for region objects of the requested space_iD1084*1085******************************************************************************/10861087static acpi_status1088acpi_ev_reg_run(acpi_handle obj_handle,1089u32 level, void *context, void **return_value)1090{1091union acpi_operand_object *obj_desc;1092struct acpi_namespace_node *node;1093acpi_adr_space_type space_id;1094acpi_status status;10951096space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);10971098/* Convert and validate the device handle */10991100node = acpi_ns_validate_handle(obj_handle);1101if (!node) {1102return (AE_BAD_PARAMETER);1103}11041105/*1106* We only care about regions.and objects that are allowed to have address1107* space handlers1108*/1109if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {1110return (AE_OK);1111}11121113/* Check for an existing internal object */11141115obj_desc = acpi_ns_get_attached_object(node);1116if (!obj_desc) {11171118/* No object, just exit */11191120return (AE_OK);1121}11221123/* Object is a Region */11241125if (obj_desc->region.space_id != space_id) {11261127/* This region is for a different address space, just ignore it */11281129return (AE_OK);1130}11311132status = acpi_ev_execute_reg_method(obj_desc, ACPI_REG_CONNECT);1133return (status);1134}11351136/*******************************************************************************1137*1138* FUNCTION: acpi_ev_orphan_ec_reg_method1139*1140* PARAMETERS: None1141*1142* RETURN: None1143*1144* DESCRIPTION: Execute an "orphan" _REG method that appears under the EC1145* device. This is a _REG method that has no corresponding region1146* within the EC device scope. The orphan _REG method appears to1147* have been enabled by the description of the ECDT in the ACPI1148* specification: "The availability of the region space can be1149* detected by providing a _REG method object underneath the1150* Embedded Controller device."1151*1152* To quickly access the EC device, we use the EC_ID that appears1153* within the ECDT. Otherwise, we would need to perform a time-1154* consuming namespace walk, executing _HID methods to find the1155* EC device.1156*1157******************************************************************************/11581159static void acpi_ev_orphan_ec_reg_method(void)1160{1161struct acpi_table_ecdt *table;1162acpi_status status;1163struct acpi_object_list args;1164union acpi_object objects[2];1165struct acpi_namespace_node *ec_device_node;1166struct acpi_namespace_node *reg_method;1167struct acpi_namespace_node *next_node;11681169ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method);11701171/* Get the ECDT (if present in system) */11721173status = acpi_get_table(ACPI_SIG_ECDT, 0,1174ACPI_CAST_INDIRECT_PTR(struct acpi_table_header,1175&table));1176if (ACPI_FAILURE(status)) {1177return_VOID;1178}11791180/* We need a valid EC_ID string */11811182if (!(*table->id)) {1183return_VOID;1184}11851186/* Namespace is currently locked, must release */11871188(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);11891190/* Get a handle to the EC device referenced in the ECDT */11911192status = acpi_get_handle(NULL,1193ACPI_CAST_PTR(char, table->id),1194ACPI_CAST_PTR(acpi_handle, &ec_device_node));1195if (ACPI_FAILURE(status)) {1196goto exit;1197}11981199/* Get a handle to a _REG method immediately under the EC device */12001201status = acpi_get_handle(ec_device_node,1202METHOD_NAME__REG, ACPI_CAST_PTR(acpi_handle,1203®_method));1204if (ACPI_FAILURE(status)) {1205goto exit;1206}12071208/*1209* Execute the _REG method only if there is no Operation Region in1210* this scope with the Embedded Controller space ID. Otherwise, it1211* will already have been executed. Note, this allows for Regions1212* with other space IDs to be present; but the code below will then1213* execute the _REG method with the EC space ID argument.1214*/1215next_node = acpi_ns_get_next_node(ec_device_node, NULL);1216while (next_node) {1217if ((next_node->type == ACPI_TYPE_REGION) &&1218(next_node->object) &&1219(next_node->object->region.space_id == ACPI_ADR_SPACE_EC)) {1220goto exit; /* Do not execute _REG */1221}1222next_node = acpi_ns_get_next_node(ec_device_node, next_node);1223}12241225/* Evaluate the _REG(EC,Connect) method */12261227args.count = 2;1228args.pointer = objects;1229objects[0].type = ACPI_TYPE_INTEGER;1230objects[0].integer.value = ACPI_ADR_SPACE_EC;1231objects[1].type = ACPI_TYPE_INTEGER;1232objects[1].integer.value = ACPI_REG_CONNECT;12331234status = acpi_evaluate_object(reg_method, NULL, &args, NULL);12351236exit:1237/* We ignore all errors from above, don't care */12381239status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);1240return_VOID;1241}124212431244