Path: blob/main/sys/contrib/dev/acpica/components/executer/exregion.c
48521 views
/******************************************************************************1*2* Module Name: exregion - ACPI default OpRegion (address space) handlers3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153#include <contrib/dev/acpica/include/acinterp.h>154155156#define _COMPONENT ACPI_EXECUTER157ACPI_MODULE_NAME ("exregion")158159160/*******************************************************************************161*162* FUNCTION: AcpiExSystemMemorySpaceHandler163*164* PARAMETERS: Function - Read or Write operation165* Address - Where in the space to read or write166* BitWidth - Field width in bits (8, 16, or 32)167* Value - Pointer to in or out value168* HandlerContext - Pointer to Handler's context169* RegionContext - Pointer to context specific to the170* accessed region171*172* RETURN: Status173*174* DESCRIPTION: Handler for the System Memory address space (Op Region)175*176******************************************************************************/177178ACPI_STATUS179AcpiExSystemMemorySpaceHandler (180UINT32 Function,181ACPI_PHYSICAL_ADDRESS Address,182UINT32 BitWidth,183UINT64 *Value,184void *HandlerContext,185void *RegionContext)186{187ACPI_STATUS Status = AE_OK;188void *LogicalAddrPtr = NULL;189ACPI_MEM_SPACE_CONTEXT *MemInfo = RegionContext;190ACPI_MEM_MAPPING *Mm = MemInfo->CurMm;191UINT32 Length;192ACPI_SIZE MapLength;193#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED194UINT32 Remainder;195#endif196197198ACPI_FUNCTION_TRACE (ExSystemMemorySpaceHandler);199200201/* Validate and translate the bit width */202203switch (BitWidth)204{205case 8:206207Length = 1;208break;209210case 16:211212Length = 2;213break;214215case 32:216217Length = 4;218break;219220case 64:221222Length = 8;223break;224225default:226227ACPI_ERROR ((AE_INFO, "Invalid SystemMemory width %u",228BitWidth));229return_ACPI_STATUS (AE_AML_OPERAND_VALUE);230}231232#ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED233/*234* Hardware does not support non-aligned data transfers, we must verify235* the request.236*/237(void) AcpiUtShortDivide ((UINT64) Address, Length, NULL, &Remainder);238if (Remainder != 0)239{240return_ACPI_STATUS (AE_AML_ALIGNMENT);241}242#endif243244/*245* Does the request fit into the cached memory mapping?246* Is 1) Address below the current mapping? OR247* 2) Address beyond the current mapping?248*/249if (!Mm || (Address < Mm->PhysicalAddress) ||250((UINT64) Address + Length > (UINT64) Mm->PhysicalAddress + Mm->Length))251{252/*253* The request cannot be resolved by the current memory mapping.254*255* Look for an existing saved mapping covering the address range256* at hand. If found, save it as the current one and carry out257* the access.258*/259for (Mm = MemInfo->FirstMm; Mm; Mm = Mm->NextMm)260{261if (Mm == MemInfo->CurMm)262{263continue;264}265266if (Address < Mm->PhysicalAddress)267{268continue;269}270271if ((UINT64) Address + Length > (UINT64) Mm->PhysicalAddress + Mm->Length)272{273continue;274}275276MemInfo->CurMm = Mm;277goto access;278}279280/* Create a new mappings list entry */281282Mm = ACPI_ALLOCATE_ZEROED(sizeof(*Mm));283if (!Mm)284{285ACPI_ERROR((AE_INFO,286"Unable to save memory mapping at 0x%8.8X%8.8X, size %u",287ACPI_FORMAT_UINT64(Address), Length));288return_ACPI_STATUS(AE_NO_MEMORY);289}290291/*292* October 2009: Attempt to map from the requested address to the293* end of the region. However, we will never map more than one294* page, nor will we cross a page boundary.295*/296MapLength = (ACPI_SIZE)297((MemInfo->Address + MemInfo->Length) - Address);298299if (MapLength > ACPI_DEFAULT_PAGE_SIZE)300{301MapLength = ACPI_DEFAULT_PAGE_SIZE;302}303304/* Create a new mapping starting at the address given */305306LogicalAddrPtr = AcpiOsMapMemory(Address, MapLength);307if (!LogicalAddrPtr)308{309ACPI_ERROR ((AE_INFO,310"Could not map memory at 0x%8.8X%8.8X, size %u",311ACPI_FORMAT_UINT64 (Address), (UINT32) MapLength));312ACPI_FREE(Mm);313return_ACPI_STATUS (AE_NO_MEMORY);314}315316/* Save the physical address and mapping size */317318Mm->LogicalAddress = LogicalAddrPtr;319Mm->PhysicalAddress = Address;320Mm->Length = MapLength;321322/*323* Add the new entry to the mappigs list and save it as the324* current mapping.325*/326Mm->NextMm = MemInfo->FirstMm;327MemInfo->FirstMm = Mm;328MemInfo->CurMm = Mm;329}330331access:332/*333* Generate a logical pointer corresponding to the address we want to334* access335*/336LogicalAddrPtr = Mm->LogicalAddress +337((UINT64) Address - (UINT64) Mm->PhysicalAddress);338339ACPI_DEBUG_PRINT ((ACPI_DB_INFO,340"System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",341BitWidth, Function, ACPI_FORMAT_UINT64 (Address)));342343/*344* Perform the memory read or write345*346* Note: For machines that do not support non-aligned transfers, the target347* address was checked for alignment above. We do not attempt to break the348* transfer up into smaller (byte-size) chunks because the AML specifically349* asked for a transfer width that the hardware may require.350*/351switch (Function)352{353case ACPI_READ:354355*Value = 0;356switch (BitWidth)357{358case 8:359360*Value = (UINT64) ACPI_GET8 (LogicalAddrPtr);361break;362363case 16:364365*Value = (UINT64) ACPI_GET16 (LogicalAddrPtr);366break;367368case 32:369370*Value = (UINT64) ACPI_GET32 (LogicalAddrPtr);371break;372373case 64:374375*Value = (UINT64) ACPI_GET64 (LogicalAddrPtr);376break;377378default:379380/* BitWidth was already validated */381382break;383}384break;385386case ACPI_WRITE:387388switch (BitWidth)389{390case 8:391392ACPI_SET8 (LogicalAddrPtr, *Value);393break;394395case 16:396397ACPI_SET16 (LogicalAddrPtr, *Value);398break;399400case 32:401402ACPI_SET32 (LogicalAddrPtr, *Value);403break;404405case 64:406407ACPI_SET64 (LogicalAddrPtr, *Value);408break;409410default:411412/* BitWidth was already validated */413414break;415}416break;417418default:419420Status = AE_BAD_PARAMETER;421break;422}423424return_ACPI_STATUS (Status);425}426427428/*******************************************************************************429*430* FUNCTION: AcpiExSystemIoSpaceHandler431*432* PARAMETERS: Function - Read or Write operation433* Address - Where in the space to read or write434* BitWidth - Field width in bits (8, 16, or 32)435* Value - Pointer to in or out value436* HandlerContext - Pointer to Handler's context437* RegionContext - Pointer to context specific to the438* accessed region439*440* RETURN: Status441*442* DESCRIPTION: Handler for the System IO address space (Op Region)443*444******************************************************************************/445446ACPI_STATUS447AcpiExSystemIoSpaceHandler (448UINT32 Function,449ACPI_PHYSICAL_ADDRESS Address,450UINT32 BitWidth,451UINT64 *Value,452void *HandlerContext,453void *RegionContext)454{455ACPI_STATUS Status = AE_OK;456UINT32 Value32;457458459ACPI_FUNCTION_TRACE (ExSystemIoSpaceHandler);460461462ACPI_DEBUG_PRINT ((ACPI_DB_INFO,463"System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",464BitWidth, Function, ACPI_FORMAT_UINT64 (Address)));465466/* Decode the function parameter */467468switch (Function)469{470case ACPI_READ:471472Status = AcpiHwReadPort ((ACPI_IO_ADDRESS) Address,473&Value32, BitWidth);474*Value = Value32;475break;476477case ACPI_WRITE:478479Status = AcpiHwWritePort ((ACPI_IO_ADDRESS) Address,480(UINT32) *Value, BitWidth);481break;482483default:484485Status = AE_BAD_PARAMETER;486break;487}488489return_ACPI_STATUS (Status);490}491492493/*******************************************************************************494*495* FUNCTION: AcpiExPciConfigSpaceHandler496*497* PARAMETERS: Function - Read or Write operation498* Address - Where in the space to read or write499* BitWidth - Field width in bits (8, 16, or 32)500* Value - Pointer to in or out value501* HandlerContext - Pointer to Handler's context502* RegionContext - Pointer to context specific to the503* accessed region504*505* RETURN: Status506*507* DESCRIPTION: Handler for the PCI Config address space (Op Region)508*509******************************************************************************/510511ACPI_STATUS512AcpiExPciConfigSpaceHandler (513UINT32 Function,514ACPI_PHYSICAL_ADDRESS Address,515UINT32 BitWidth,516UINT64 *Value,517void *HandlerContext,518void *RegionContext)519{520ACPI_STATUS Status = AE_OK;521ACPI_PCI_ID *PciId;522UINT16 PciRegister;523524525ACPI_FUNCTION_TRACE (ExPciConfigSpaceHandler);526527528/*529* The arguments to AcpiOs(Read|Write)PciConfiguration are:530*531* PciSegment is the PCI bus segment range 0-31532* PciBus is the PCI bus number range 0-255533* PciDevice is the PCI device number range 0-31534* PciFunction is the PCI device function number535* PciRegister is the Config space register range 0-255 bytes536*537* Value - input value for write, output address for read538*539*/540PciId = (ACPI_PCI_ID *) RegionContext;541PciRegister = (UINT16) (UINT32) Address;542543ACPI_DEBUG_PRINT ((ACPI_DB_INFO,544"Pci-Config %u (%u) Seg(%04x) Bus(%04x) "545"Dev(%04x) Func(%04x) Reg(%04x)\n",546Function, BitWidth, PciId->Segment, PciId->Bus, PciId->Device,547PciId->Function, PciRegister));548549switch (Function)550{551case ACPI_READ:552553*Value = 0;554Status = AcpiOsReadPciConfiguration (555PciId, PciRegister, Value, BitWidth);556break;557558case ACPI_WRITE:559560Status = AcpiOsWritePciConfiguration (561PciId, PciRegister, *Value, BitWidth);562break;563564default:565566Status = AE_BAD_PARAMETER;567break;568}569570return_ACPI_STATUS (Status);571}572573574/*******************************************************************************575*576* FUNCTION: AcpiExCmosSpaceHandler577*578* PARAMETERS: Function - Read or Write operation579* Address - Where in the space to read or write580* BitWidth - Field width in bits (8, 16, or 32)581* Value - Pointer to in or out value582* HandlerContext - Pointer to Handler's context583* RegionContext - Pointer to context specific to the584* accessed region585*586* RETURN: Status587*588* DESCRIPTION: Handler for the CMOS address space (Op Region)589*590******************************************************************************/591592ACPI_STATUS593AcpiExCmosSpaceHandler (594UINT32 Function,595ACPI_PHYSICAL_ADDRESS Address,596UINT32 BitWidth,597UINT64 *Value,598void *HandlerContext,599void *RegionContext)600{601ACPI_STATUS Status = AE_OK;602603604ACPI_FUNCTION_TRACE (ExCmosSpaceHandler);605606607return_ACPI_STATUS (Status);608}609610611/*******************************************************************************612*613* FUNCTION: AcpiExPciBarSpaceHandler614*615* PARAMETERS: Function - Read or Write operation616* Address - Where in the space to read or write617* BitWidth - Field width in bits (8, 16, or 32)618* Value - Pointer to in or out value619* HandlerContext - Pointer to Handler's context620* RegionContext - Pointer to context specific to the621* accessed region622*623* RETURN: Status624*625* DESCRIPTION: Handler for the PCI BarTarget address space (Op Region)626*627******************************************************************************/628629ACPI_STATUS630AcpiExPciBarSpaceHandler (631UINT32 Function,632ACPI_PHYSICAL_ADDRESS Address,633UINT32 BitWidth,634UINT64 *Value,635void *HandlerContext,636void *RegionContext)637{638ACPI_STATUS Status = AE_OK;639640641ACPI_FUNCTION_TRACE (ExPciBarSpaceHandler);642643644return_ACPI_STATUS (Status);645}646647648/*******************************************************************************649*650* FUNCTION: AcpiExDataTableSpaceHandler651*652* PARAMETERS: Function - Read or Write operation653* Address - Where in the space to read or write654* BitWidth - Field width in bits (8, 16, or 32)655* Value - Pointer to in or out value656* HandlerContext - Pointer to Handler's context657* RegionContext - Pointer to context specific to the658* accessed region659*660* RETURN: Status661*662* DESCRIPTION: Handler for the Data Table address space (Op Region)663*664******************************************************************************/665666ACPI_STATUS667AcpiExDataTableSpaceHandler (668UINT32 Function,669ACPI_PHYSICAL_ADDRESS Address,670UINT32 BitWidth,671UINT64 *Value,672void *HandlerContext,673void *RegionContext)674{675ACPI_DATA_TABLE_MAPPING *Mapping;676char *Pointer;677678679ACPI_FUNCTION_TRACE (ExDataTableSpaceHandler);680681682Mapping = (ACPI_DATA_TABLE_MAPPING *) RegionContext;683Pointer = ACPI_CAST_PTR (char, Mapping->Pointer) +684(Address - ACPI_PTR_TO_PHYSADDR (Mapping->Pointer));685686/*687* Perform the memory read or write. The BitWidth was already688* validated.689*/690switch (Function)691{692case ACPI_READ:693694memcpy (ACPI_CAST_PTR (char, Value), Pointer, ACPI_DIV_8 (BitWidth));695break;696697case ACPI_WRITE:698699memcpy (Pointer, ACPI_CAST_PTR (char, Value), ACPI_DIV_8 (BitWidth));700break;701702default:703704return_ACPI_STATUS (AE_BAD_PARAMETER);705}706707return_ACPI_STATUS (AE_OK);708}709710711