Home | History | Annotate | Line # | Download | only in acpiexec
aeregion.c revision 1.1.1.7.2.2
      1          1.1  christos /******************************************************************************
      2          1.1  christos  *
      3      1.1.1.6  christos  * Module Name: aeregion - Handler for operation regions
      4          1.1  christos  *
      5          1.1  christos  *****************************************************************************/
      6          1.1  christos 
      7          1.1  christos /*
      8  1.1.1.7.2.2    martin  * Copyright (C) 2000 - 2020, Intel Corp.
      9          1.1  christos  * All rights reserved.
     10          1.1  christos  *
     11          1.1  christos  * Redistribution and use in source and binary forms, with or without
     12          1.1  christos  * modification, are permitted provided that the following conditions
     13          1.1  christos  * are met:
     14          1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15          1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16          1.1  christos  *    without modification.
     17          1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18          1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19          1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20          1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21          1.1  christos  *    binary redistribution.
     22          1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23          1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24          1.1  christos  *    from this software without specific prior written permission.
     25          1.1  christos  *
     26          1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27          1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28          1.1  christos  * Software Foundation.
     29          1.1  christos  *
     30          1.1  christos  * NO WARRANTY
     31          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35          1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36          1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37          1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38          1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39          1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40          1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41          1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42          1.1  christos  */
     43          1.1  christos 
     44          1.1  christos #include "aecommon.h"
     45          1.1  christos 
     46          1.1  christos #define _COMPONENT          ACPI_TOOLS
     47          1.1  christos         ACPI_MODULE_NAME    ("aeregion")
     48          1.1  christos 
     49          1.1  christos 
     50          1.1  christos static AE_DEBUG_REGIONS     AeRegions;
     51          1.1  christos 
     52          1.1  christos 
     53          1.1  christos /******************************************************************************
     54          1.1  christos  *
     55          1.1  christos  * FUNCTION:    AeRegionHandler
     56          1.1  christos  *
     57          1.1  christos  * PARAMETERS:  Standard region handler parameters
     58          1.1  christos  *
     59          1.1  christos  * RETURN:      Status
     60          1.1  christos  *
     61          1.1  christos  * DESCRIPTION: Test handler - Handles some dummy regions via memory that can
     62          1.1  christos  *              be manipulated in Ring 3. Simulates actual reads and writes.
     63          1.1  christos  *
     64          1.1  christos  *****************************************************************************/
     65          1.1  christos 
     66          1.1  christos ACPI_STATUS
     67          1.1  christos AeRegionHandler (
     68          1.1  christos     UINT32                  Function,
     69          1.1  christos     ACPI_PHYSICAL_ADDRESS   Address,
     70          1.1  christos     UINT32                  BitWidth,
     71          1.1  christos     UINT64                  *Value,
     72          1.1  christos     void                    *HandlerContext,
     73          1.1  christos     void                    *RegionContext)
     74          1.1  christos {
     75          1.1  christos 
     76          1.1  christos     ACPI_OPERAND_OBJECT     *RegionObject = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, RegionContext);
     77          1.1  christos     UINT8                   *Buffer = ACPI_CAST_PTR (UINT8, Value);
     78          1.1  christos     UINT8                   *OldBuffer;
     79          1.1  christos     UINT8                   *NewBuffer;
     80          1.1  christos     ACPI_PHYSICAL_ADDRESS   BaseAddress;
     81          1.1  christos     ACPI_PHYSICAL_ADDRESS   BaseAddressEnd;
     82          1.1  christos     ACPI_PHYSICAL_ADDRESS   RegionAddress;
     83          1.1  christos     ACPI_PHYSICAL_ADDRESS   RegionAddressEnd;
     84  1.1.1.7.2.1  christos     UINT32                  Length;
     85  1.1.1.7.2.1  christos     UINT8                   DataLength;
     86  1.1.1.7.2.1  christos     UINT8                   *DataBuffer;
     87          1.1  christos     BOOLEAN                 BufferExists;
     88          1.1  christos     BOOLEAN                 BufferResize;
     89          1.1  christos     AE_REGION               *RegionElement;
     90          1.1  christos     void                    *BufferValue;
     91          1.1  christos     ACPI_STATUS             Status;
     92          1.1  christos     UINT32                  ByteWidth;
     93          1.1  christos     UINT32                  RegionLength;
     94          1.1  christos     UINT32                  i;
     95          1.1  christos     UINT8                   SpaceId;
     96          1.1  christos     ACPI_CONNECTION_INFO    *MyContext;
     97          1.1  christos     UINT32                  Value1;
     98          1.1  christos     UINT32                  Value2;
     99          1.1  christos     ACPI_RESOURCE           *Resource;
    100          1.1  christos 
    101          1.1  christos 
    102          1.1  christos     ACPI_FUNCTION_NAME (AeRegionHandler);
    103          1.1  christos 
    104  1.1.1.7.2.1  christos 
    105  1.1.1.7.2.1  christos     /* If the object is not a region, simply return */
    106  1.1.1.7.2.1  christos 
    107          1.1  christos     if (RegionObject->Region.Type != ACPI_TYPE_REGION)
    108          1.1  christos     {
    109          1.1  christos         return (AE_OK);
    110          1.1  christos     }
    111          1.1  christos 
    112          1.1  christos     /* Check that we actually got back our context parameter */
    113          1.1  christos 
    114          1.1  christos     if (HandlerContext != &AeMyContext)
    115          1.1  christos     {
    116  1.1.1.7.2.1  christos         AcpiOsPrintf (
    117  1.1.1.7.2.1  christos             "Region handler received incorrect context %p, should be %p\n",
    118          1.1  christos             HandlerContext, &AeMyContext);
    119          1.1  christos     }
    120          1.1  christos 
    121          1.1  christos     MyContext = ACPI_CAST_PTR (ACPI_CONNECTION_INFO, HandlerContext);
    122          1.1  christos 
    123          1.1  christos     /*
    124          1.1  christos      * Find the region's address space and length before searching
    125          1.1  christos      * the linked list.
    126          1.1  christos      */
    127          1.1  christos     BaseAddress = RegionObject->Region.Address;
    128  1.1.1.7.2.1  christos     Length = RegionObject->Region.Length;
    129          1.1  christos     SpaceId = RegionObject->Region.SpaceId;
    130          1.1  christos 
    131      1.1.1.4  christos     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
    132  1.1.1.7.2.1  christos         "Operation Region request on %s at 0x%X, BitWidth 0x%X, RegionLength 0x%X\n",
    133      1.1.1.4  christos         AcpiUtGetRegionName (RegionObject->Region.SpaceId),
    134  1.1.1.7.2.1  christos         (UINT32) Address, BitWidth, (UINT32) Length));
    135          1.1  christos 
    136          1.1  christos     /*
    137          1.1  christos      * Region support can be disabled with the -do option.
    138          1.1  christos      * We use this to support dynamically loaded tables where we pass a valid
    139          1.1  christos      * address to the AML.
    140          1.1  christos      */
    141          1.1  christos     if (AcpiGbl_DbOpt_NoRegionSupport)
    142          1.1  christos     {
    143          1.1  christos         BufferValue = ACPI_TO_POINTER (Address);
    144          1.1  christos         ByteWidth = (BitWidth / 8);
    145          1.1  christos 
    146          1.1  christos         if (BitWidth % 8)
    147          1.1  christos         {
    148          1.1  christos             ByteWidth += 1;
    149          1.1  christos         }
    150          1.1  christos         goto DoFunction;
    151          1.1  christos     }
    152          1.1  christos 
    153          1.1  christos     switch (SpaceId)
    154          1.1  christos     {
    155          1.1  christos     case ACPI_ADR_SPACE_SYSTEM_IO:
    156          1.1  christos         /*
    157          1.1  christos          * For I/O space, exercise the port validation
    158          1.1  christos          * Note: ReadPort currently always returns all ones, length=BitLength
    159          1.1  christos          */
    160          1.1  christos         switch (Function & ACPI_IO_MASK)
    161          1.1  christos         {
    162          1.1  christos         case ACPI_READ:
    163          1.1  christos 
    164          1.1  christos             if (BitWidth == 64)
    165          1.1  christos             {
    166          1.1  christos                 /* Split the 64-bit request into two 32-bit requests */
    167          1.1  christos 
    168          1.1  christos                 Status = AcpiHwReadPort (Address, &Value1, 32);
    169      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
    170          1.1  christos                 Status = AcpiHwReadPort (Address+4, &Value2, 32);
    171      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
    172          1.1  christos 
    173          1.1  christos                 *Value = Value1 | ((UINT64) Value2 << 32);
    174          1.1  christos             }
    175          1.1  christos             else
    176          1.1  christos             {
    177          1.1  christos                 Status = AcpiHwReadPort (Address, &Value1, BitWidth);
    178      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
    179          1.1  christos                 *Value = (UINT64) Value1;
    180          1.1  christos             }
    181          1.1  christos             break;
    182          1.1  christos 
    183          1.1  christos         case ACPI_WRITE:
    184          1.1  christos 
    185          1.1  christos             if (BitWidth == 64)
    186          1.1  christos             {
    187          1.1  christos                 /* Split the 64-bit request into two 32-bit requests */
    188          1.1  christos 
    189          1.1  christos                 Status = AcpiHwWritePort (Address, ACPI_LODWORD (*Value), 32);
    190      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
    191          1.1  christos                 Status = AcpiHwWritePort (Address+4, ACPI_HIDWORD (*Value), 32);
    192      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
    193          1.1  christos             }
    194          1.1  christos             else
    195          1.1  christos             {
    196          1.1  christos                 Status = AcpiHwWritePort (Address, (UINT32) *Value, BitWidth);
    197      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
    198          1.1  christos             }
    199          1.1  christos             break;
    200          1.1  christos 
    201          1.1  christos         default:
    202          1.1  christos 
    203          1.1  christos             Status = AE_BAD_PARAMETER;
    204          1.1  christos             break;
    205          1.1  christos         }
    206          1.1  christos 
    207          1.1  christos         if (ACPI_FAILURE (Status))
    208          1.1  christos         {
    209          1.1  christos             return (Status);
    210          1.1  christos         }
    211          1.1  christos 
    212          1.1  christos         /* Now go ahead and simulate the hardware */
    213          1.1  christos         break;
    214          1.1  christos 
    215          1.1  christos     /*
    216          1.1  christos      * SMBus and GenericSerialBus support the various bidirectional
    217          1.1  christos      * protocols.
    218          1.1  christos      */
    219          1.1  christos     case ACPI_ADR_SPACE_SMBUS:
    220          1.1  christos     case ACPI_ADR_SPACE_GSBUS:  /* ACPI 5.0 */
    221          1.1  christos 
    222  1.1.1.7.2.1  christos         Status = AcpiExGetProtocolBufferLength ((Function >> 16), &Length);
    223  1.1.1.7.2.1  christos         if (ACPI_FAILURE (Status))
    224          1.1  christos         {
    225  1.1.1.7.2.1  christos             AcpiOsPrintf ("AcpiExec: Invalid SMbus/GSbus protocol ID: 0x%X\n",
    226  1.1.1.7.2.1  christos                 (Function >> 16));
    227  1.1.1.7.2.1  christos             return (Status);
    228  1.1.1.7.2.1  christos         }
    229          1.1  christos 
    230  1.1.1.7.2.1  christos         /* Adjust for fixed SMBus buffer size */
    231          1.1  christos 
    232  1.1.1.7.2.1  christos         if ((SpaceId == ACPI_ADR_SPACE_SMBUS) &&
    233  1.1.1.7.2.1  christos             (Length > ACPI_SMBUS_DATA_SIZE))
    234  1.1.1.7.2.1  christos         {
    235  1.1.1.7.2.1  christos             Length = ACPI_SMBUS_DATA_SIZE; /* SMBus buffer is fixed-length */
    236          1.1  christos         }
    237          1.1  christos 
    238          1.1  christos         if (AcpiGbl_DisplayRegionAccess)
    239          1.1  christos         {
    240          1.1  christos             AcpiOsPrintf ("AcpiExec: %s "
    241  1.1.1.7.2.1  christos                 "%s: Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X\n",
    242          1.1  christos                 AcpiUtGetRegionName (SpaceId),
    243          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    244          1.1  christos                 (UINT32) (Function >> 16),
    245          1.1  christos                 (UINT32) Address, (UINT32) BaseAddress,
    246          1.1  christos                 Length, BitWidth, Buffer[1]);
    247          1.1  christos 
    248          1.1  christos             /* GenericSerialBus has a Connection() parameter */
    249          1.1  christos 
    250  1.1.1.7.2.1  christos             if ((SpaceId == ACPI_ADR_SPACE_GSBUS) && MyContext)
    251          1.1  christos             {
    252          1.1  christos                 Status = AcpiBufferToResource (MyContext->Connection,
    253          1.1  christos                     MyContext->Length, &Resource);
    254  1.1.1.7.2.1  christos                 if (ACPI_SUCCESS (Status))
    255  1.1.1.7.2.1  christos                 {
    256  1.1.1.7.2.1  christos                     ACPI_FREE (Resource);
    257  1.1.1.7.2.1  christos                 }
    258          1.1  christos 
    259  1.1.1.7.2.1  christos                 AcpiOsPrintf (" [AccessLength %.2X Connection %p]",
    260          1.1  christos                     MyContext->AccessLength, MyContext->Connection);
    261          1.1  christos             }
    262  1.1.1.7.2.1  christos 
    263          1.1  christos             AcpiOsPrintf ("\n");
    264          1.1  christos         }
    265          1.1  christos 
    266  1.1.1.7.2.1  christos         DataBuffer = &Buffer[2];
    267  1.1.1.7.2.1  christos         DataLength = (UINT8) Length;
    268  1.1.1.7.2.1  christos 
    269          1.1  christos         /* Setup the return buffer. Note: ASLTS depends on these fill values */
    270          1.1  christos 
    271  1.1.1.7.2.1  christos         if (Length == ACPI_MAX_GSBUS_DATA_SIZE)
    272  1.1.1.7.2.1  christos         {
    273  1.1.1.7.2.1  christos             DataLength = 0x20; /* For ASLTS only */
    274  1.1.1.7.2.1  christos         }
    275  1.1.1.7.2.1  christos 
    276          1.1  christos         for (i = 0; i < Length; i++)
    277          1.1  christos         {
    278  1.1.1.7.2.1  christos             DataBuffer[i] = (UINT8) (0xA0 + i);
    279          1.1  christos         }
    280          1.1  christos 
    281  1.1.1.7.2.1  christos         Buffer[0] = 0;                  /* Return Status, OK */
    282  1.1.1.7.2.1  christos         Buffer[1] = DataLength;         /* Length of valid data */
    283          1.1  christos         return (AE_OK);
    284          1.1  christos 
    285          1.1  christos     case ACPI_ADR_SPACE_IPMI: /* ACPI 4.0 */
    286          1.1  christos 
    287          1.1  christos         if (AcpiGbl_DisplayRegionAccess)
    288          1.1  christos         {
    289          1.1  christos             AcpiOsPrintf ("AcpiExec: IPMI "
    290          1.1  christos                 "%s: Attr %X Addr %.4X BaseAddr %.4X Len %.2X Width %X BufLen %X\n",
    291          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    292          1.1  christos                 (UINT32) (Function >> 16), (UINT32) Address, (UINT32) BaseAddress,
    293          1.1  christos                 Length, BitWidth, Buffer[1]);
    294          1.1  christos         }
    295          1.1  christos 
    296          1.1  christos         /*
    297          1.1  christos          * Regardless of a READ or WRITE, this handler is passed a 66-byte
    298          1.1  christos          * buffer in which to return the IPMI status/length/data.
    299          1.1  christos          *
    300          1.1  christos          * Return some example data to show use of the bidirectional buffer
    301          1.1  christos          */
    302  1.1.1.7.2.1  christos         Buffer[0] = 0;                      /* Status byte */
    303  1.1.1.7.2.1  christos         Buffer[1] = ACPI_IPMI_DATA_SIZE;    /* Return buffer data length */
    304  1.1.1.7.2.1  christos         Buffer[2] = 0;                      /* Completion code */
    305  1.1.1.7.2.1  christos         Buffer[3] = 0;                      /* Reserved */
    306          1.1  christos 
    307          1.1  christos         /*
    308          1.1  christos          * Fill the 66-byte buffer with the return data.
    309          1.1  christos          * Note: ASLTS depends on these fill values.
    310          1.1  christos          */
    311  1.1.1.7.2.1  christos         for (i = 4; i < ACPI_IPMI_BUFFER_SIZE; i++)
    312          1.1  christos         {
    313          1.1  christos             Buffer[i] = (UINT8) (i);
    314          1.1  christos         }
    315          1.1  christos         return (AE_OK);
    316          1.1  christos 
    317          1.1  christos     /*
    318          1.1  christos      * GPIO has some special semantics:
    319          1.1  christos      * 1) Address is the pin number index into the Connection() pin list
    320          1.1  christos      * 2) BitWidth is the actual number of bits (pins) defined by the field
    321          1.1  christos      */
    322          1.1  christos     case ACPI_ADR_SPACE_GPIO: /* ACPI 5.0 */
    323          1.1  christos 
    324          1.1  christos         if (AcpiGbl_DisplayRegionAccess)
    325          1.1  christos         {
    326          1.1  christos             AcpiOsPrintf ("AcpiExec: GPIO "
    327  1.1.1.7.2.1  christos                 "%s: Address %.4X Length %X BitWidth %X Conn %p\n",
    328          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    329  1.1.1.7.2.1  christos                 (UINT32) Address, Length, BitWidth, MyContext->Connection);
    330  1.1.1.7.2.1  christos         }
    331  1.1.1.7.2.1  christos 
    332  1.1.1.7.2.1  christos         /* Now perform the "normal" SystemMemory handling, for AcpiExec only */
    333  1.1.1.7.2.1  christos         break;
    334  1.1.1.7.2.1  christos 
    335  1.1.1.7.2.1  christos     /*
    336  1.1.1.7.2.1  christos      * PCC operation region will write the entire subspace's data and expect
    337  1.1.1.7.2.1  christos      * a response from the hardware. For acpiexec, we'll fill the buffer with
    338  1.1.1.7.2.1  christos      * default values. Note: ASLTS will depend on these values.
    339  1.1.1.7.2.1  christos      */
    340  1.1.1.7.2.1  christos     case ACPI_ADR_SPACE_PLATFORM_COMM: /* ACPI 6.3 */
    341  1.1.1.7.2.1  christos         if (AcpiGbl_DisplayRegionAccess)
    342  1.1.1.7.2.1  christos         {
    343  1.1.1.7.2.1  christos             AcpiOsPrintf ("AcpiExec: PCC Write : Addr %.4X Width %X\n",
    344  1.1.1.7.2.1  christos                 (UINT32) Address, BitWidth);
    345  1.1.1.7.2.1  christos         }
    346  1.1.1.7.2.1  christos         for (i = 0; i < Length; ++i)
    347  1.1.1.7.2.1  christos         {
    348  1.1.1.7.2.1  christos             Buffer[i] = (UINT8) i;
    349          1.1  christos         }
    350          1.1  christos         return (AE_OK);
    351          1.1  christos 
    352          1.1  christos     default:
    353          1.1  christos         break;
    354          1.1  christos     }
    355          1.1  christos 
    356          1.1  christos     /*
    357          1.1  christos      * Search through the linked list for this region's buffer
    358          1.1  christos      */
    359          1.1  christos     BufferExists = FALSE;
    360          1.1  christos     BufferResize = FALSE;
    361          1.1  christos     RegionElement = AeRegions.RegionList;
    362          1.1  christos 
    363          1.1  christos     if (AeRegions.NumberOfRegions)
    364          1.1  christos     {
    365          1.1  christos         BaseAddressEnd = BaseAddress + Length - 1;
    366          1.1  christos         while (!BufferExists && RegionElement)
    367          1.1  christos         {
    368          1.1  christos             RegionAddress = RegionElement->Address;
    369          1.1  christos             RegionAddressEnd = RegionElement->Address + RegionElement->Length - 1;
    370          1.1  christos             RegionLength = RegionElement->Length;
    371          1.1  christos 
    372          1.1  christos             /*
    373          1.1  christos              * Overlapping Region Support
    374          1.1  christos              *
    375          1.1  christos              * While searching through the region buffer list, determine if an
    376          1.1  christos              * overlap exists between the requested buffer space and the current
    377          1.1  christos              * RegionElement space. If there is an overlap then replace the old
    378          1.1  christos              * buffer with a new buffer of increased size before continuing to
    379          1.1  christos              * do the read or write
    380          1.1  christos              */
    381          1.1  christos             if (RegionElement->SpaceId != SpaceId ||
    382          1.1  christos                 BaseAddressEnd < RegionAddress ||
    383          1.1  christos                 BaseAddress > RegionAddressEnd)
    384          1.1  christos             {
    385          1.1  christos                 /*
    386          1.1  christos                  * Requested buffer is outside of the current RegionElement
    387          1.1  christos                  * bounds
    388          1.1  christos                  */
    389          1.1  christos                 RegionElement = RegionElement->NextRegion;
    390          1.1  christos             }
    391          1.1  christos             else
    392          1.1  christos             {
    393          1.1  christos                 /*
    394          1.1  christos                  * Some amount of buffer space sharing exists. There are 4 cases
    395          1.1  christos                  * to consider:
    396          1.1  christos                  *
    397          1.1  christos                  * 1. Right overlap
    398          1.1  christos                  * 2. Left overlap
    399          1.1  christos                  * 3. Left and right overlap
    400          1.1  christos                  * 4. Fully contained - no resizing required
    401          1.1  christos                  */
    402          1.1  christos                 BufferExists = TRUE;
    403          1.1  christos 
    404          1.1  christos                 if ((BaseAddress >= RegionAddress) &&
    405          1.1  christos                     (BaseAddress <= RegionAddressEnd) &&
    406          1.1  christos                     (BaseAddressEnd > RegionAddressEnd))
    407          1.1  christos                 {
    408          1.1  christos                     /* Right overlap */
    409          1.1  christos 
    410      1.1.1.2  christos                     RegionElement->Length = (UINT32) (BaseAddress -
    411      1.1.1.2  christos                         RegionAddress + Length);
    412          1.1  christos                     BufferResize = TRUE;
    413          1.1  christos                 }
    414          1.1  christos 
    415          1.1  christos                 else if ((BaseAddressEnd >= RegionAddress) &&
    416          1.1  christos                          (BaseAddressEnd <= RegionAddressEnd) &&
    417          1.1  christos                          (BaseAddress < RegionAddress))
    418          1.1  christos                 {
    419          1.1  christos                     /* Left overlap */
    420          1.1  christos 
    421          1.1  christos                     RegionElement->Address = BaseAddress;
    422      1.1.1.2  christos                     RegionElement->Length = (UINT32) (RegionAddress -
    423      1.1.1.2  christos                         BaseAddress + RegionElement->Length);
    424          1.1  christos                     BufferResize = TRUE;
    425          1.1  christos                 }
    426          1.1  christos 
    427          1.1  christos                 else if ((BaseAddress < RegionAddress) &&
    428          1.1  christos                          (BaseAddressEnd > RegionAddressEnd))
    429          1.1  christos                 {
    430          1.1  christos                     /* Left and right overlap */
    431          1.1  christos 
    432          1.1  christos                     RegionElement->Address = BaseAddress;
    433          1.1  christos                     RegionElement->Length = Length;
    434          1.1  christos                     BufferResize = TRUE;
    435          1.1  christos                 }
    436          1.1  christos 
    437          1.1  christos                 /*
    438          1.1  christos                  * only remaining case is fully contained for which we don't
    439          1.1  christos                  * need to do anything
    440          1.1  christos                  */
    441          1.1  christos                 if (BufferResize)
    442          1.1  christos                 {
    443          1.1  christos                     NewBuffer = AcpiOsAllocate (RegionElement->Length);
    444          1.1  christos                     if (!NewBuffer)
    445          1.1  christos                     {
    446          1.1  christos                         return (AE_NO_MEMORY);
    447          1.1  christos                     }
    448          1.1  christos 
    449          1.1  christos                     OldBuffer = RegionElement->Buffer;
    450          1.1  christos                     RegionElement->Buffer = NewBuffer;
    451          1.1  christos                     NewBuffer = NULL;
    452          1.1  christos 
    453          1.1  christos                     /* Initialize the region with the default fill value */
    454          1.1  christos 
    455      1.1.1.3  christos                     memset (RegionElement->Buffer,
    456          1.1  christos                         AcpiGbl_RegionFillValue, RegionElement->Length);
    457          1.1  christos 
    458          1.1  christos                     /*
    459          1.1  christos                      * Get BufferValue to point (within the new buffer) to the
    460          1.1  christos                      * base address of the old buffer
    461          1.1  christos                      */
    462          1.1  christos                     BufferValue = (UINT8 *) RegionElement->Buffer +
    463          1.1  christos                         (UINT64) RegionAddress -
    464          1.1  christos                         (UINT64) RegionElement->Address;
    465          1.1  christos 
    466          1.1  christos                     /*
    467          1.1  christos                      * Copy the old buffer to its same location within the new
    468          1.1  christos                      * buffer
    469          1.1  christos                      */
    470      1.1.1.3  christos                     memcpy (BufferValue, OldBuffer, RegionLength);
    471          1.1  christos                     AcpiOsFree (OldBuffer);
    472          1.1  christos                 }
    473          1.1  christos             }
    474          1.1  christos         }
    475          1.1  christos     }
    476          1.1  christos 
    477          1.1  christos     /*
    478          1.1  christos      * If the Region buffer does not exist, create it now
    479          1.1  christos      */
    480          1.1  christos     if (!BufferExists)
    481          1.1  christos     {
    482          1.1  christos         /* Do the memory allocations first */
    483          1.1  christos 
    484          1.1  christos         RegionElement = AcpiOsAllocate (sizeof (AE_REGION));
    485          1.1  christos         if (!RegionElement)
    486          1.1  christos         {
    487          1.1  christos             return (AE_NO_MEMORY);
    488          1.1  christos         }
    489          1.1  christos 
    490          1.1  christos         RegionElement->Buffer = AcpiOsAllocate (Length);
    491          1.1  christos         if (!RegionElement->Buffer)
    492          1.1  christos         {
    493          1.1  christos             AcpiOsFree (RegionElement);
    494          1.1  christos             return (AE_NO_MEMORY);
    495          1.1  christos         }
    496          1.1  christos 
    497          1.1  christos         /* Initialize the region with the default fill value */
    498          1.1  christos 
    499      1.1.1.3  christos         memset (RegionElement->Buffer, AcpiGbl_RegionFillValue, Length);
    500          1.1  christos 
    501          1.1  christos         RegionElement->Address      = BaseAddress;
    502          1.1  christos         RegionElement->Length       = Length;
    503          1.1  christos         RegionElement->SpaceId      = SpaceId;
    504          1.1  christos         RegionElement->NextRegion   = NULL;
    505          1.1  christos 
    506          1.1  christos         /*
    507          1.1  christos          * Increment the number of regions and put this one
    508          1.1  christos          * at the head of the list as it will probably get accessed
    509          1.1  christos          * more often anyway.
    510          1.1  christos          */
    511          1.1  christos         AeRegions.NumberOfRegions += 1;
    512          1.1  christos 
    513          1.1  christos         if (AeRegions.RegionList)
    514          1.1  christos         {
    515          1.1  christos             RegionElement->NextRegion = AeRegions.RegionList;
    516          1.1  christos         }
    517          1.1  christos 
    518          1.1  christos         AeRegions.RegionList = RegionElement;
    519          1.1  christos     }
    520          1.1  christos 
    521          1.1  christos     /* Calculate the size of the memory copy */
    522          1.1  christos 
    523          1.1  christos     ByteWidth = (BitWidth / 8);
    524          1.1  christos     if (BitWidth % 8)
    525          1.1  christos     {
    526          1.1  christos         ByteWidth += 1;
    527          1.1  christos     }
    528          1.1  christos 
    529          1.1  christos     /*
    530          1.1  christos      * The buffer exists and is pointed to by RegionElement.
    531          1.1  christos      * We now need to verify the request is valid and perform the operation.
    532          1.1  christos      *
    533          1.1  christos      * NOTE: RegionElement->Length is in bytes, therefore it we compare against
    534          1.1  christos      * ByteWidth (see above)
    535          1.1  christos      */
    536          1.1  christos     if ((RegionObject->Region.SpaceId != ACPI_ADR_SPACE_GPIO) &&
    537          1.1  christos         ((UINT64) Address + ByteWidth) >
    538          1.1  christos         ((UINT64)(RegionElement->Address) + RegionElement->Length))
    539          1.1  christos     {
    540          1.1  christos         ACPI_WARNING ((AE_INFO,
    541      1.1.1.4  christos             "Request on [%4.4s] is beyond region limit "
    542      1.1.1.4  christos             "Req-0x%X+0x%X, Base=0x%X, Len-0x%X",
    543          1.1  christos             (RegionObject->Region.Node)->Name.Ascii, (UINT32) Address,
    544          1.1  christos             ByteWidth, (UINT32)(RegionElement->Address),
    545          1.1  christos             RegionElement->Length));
    546          1.1  christos 
    547          1.1  christos         return (AE_AML_REGION_LIMIT);
    548          1.1  christos     }
    549          1.1  christos 
    550          1.1  christos     /*
    551          1.1  christos      * Get BufferValue to point to the "address" in the buffer
    552          1.1  christos      */
    553          1.1  christos     BufferValue = ((UINT8 *) RegionElement->Buffer +
    554      1.1.1.4  christos         ((UINT64) Address - (UINT64) RegionElement->Address));
    555          1.1  christos 
    556          1.1  christos DoFunction:
    557          1.1  christos     /*
    558          1.1  christos      * Perform a read or write to the buffer space
    559          1.1  christos      */
    560          1.1  christos     switch (Function)
    561          1.1  christos     {
    562          1.1  christos     case ACPI_READ:
    563          1.1  christos         /*
    564          1.1  christos          * Set the pointer Value to whatever is in the buffer
    565          1.1  christos          */
    566      1.1.1.3  christos         memcpy (Value, BufferValue, ByteWidth);
    567          1.1  christos         break;
    568          1.1  christos 
    569          1.1  christos     case ACPI_WRITE:
    570          1.1  christos         /*
    571          1.1  christos          * Write the contents of Value to the buffer
    572          1.1  christos          */
    573      1.1.1.3  christos         memcpy (BufferValue, Value, ByteWidth);
    574          1.1  christos         break;
    575          1.1  christos 
    576          1.1  christos     default:
    577          1.1  christos 
    578          1.1  christos         return (AE_BAD_PARAMETER);
    579          1.1  christos     }
    580          1.1  christos 
    581          1.1  christos     if (AcpiGbl_DisplayRegionAccess)
    582          1.1  christos     {
    583          1.1  christos         switch (SpaceId)
    584          1.1  christos         {
    585          1.1  christos         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
    586          1.1  christos 
    587          1.1  christos             AcpiOsPrintf ("AcpiExec: SystemMemory "
    588  1.1.1.7.2.1  christos                 "%s: Val %.8X Addr %.4X BitWidth %X [REGION: BaseAddr %.4X Len %.2X]\n",
    589          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    590          1.1  christos                 (UINT32) *Value, (UINT32) Address, BitWidth, (UINT32) BaseAddress, Length);
    591          1.1  christos             break;
    592          1.1  christos 
    593  1.1.1.7.2.1  christos         case ACPI_ADR_SPACE_GSBUS:
    594  1.1.1.7.2.1  christos 
    595  1.1.1.7.2.1  christos             AcpiOsPrintf ("AcpiExec: GenericSerialBus\n");
    596  1.1.1.7.2.1  christos             break;
    597  1.1.1.7.2.1  christos 
    598          1.1  christos         case ACPI_ADR_SPACE_GPIO:   /* ACPI 5.0 */
    599          1.1  christos 
    600          1.1  christos             /* This space is required to always be ByteAcc */
    601          1.1  christos 
    602          1.1  christos             Status = AcpiBufferToResource (MyContext->Connection,
    603          1.1  christos                 MyContext->Length, &Resource);
    604          1.1  christos 
    605          1.1  christos             AcpiOsPrintf ("AcpiExec: GeneralPurposeIo "
    606  1.1.1.7.2.1  christos                 "%s: %.8X Addr %.4X BaseAddr %.4X Length %.2X "
    607  1.1.1.7.2.1  christos                 "BitWidth %X AccLen %.2X Conn %p\n",
    608          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ", (UINT32) *Value,
    609          1.1  christos                 (UINT32) Address, (UINT32) BaseAddress, Length, BitWidth,
    610          1.1  christos                 MyContext->AccessLength, MyContext->Connection);
    611  1.1.1.7.2.1  christos             if (ACPI_SUCCESS (Status))
    612  1.1.1.7.2.1  christos             {
    613  1.1.1.7.2.1  christos                 ACPI_FREE (Resource);
    614  1.1.1.7.2.1  christos             }
    615          1.1  christos             break;
    616          1.1  christos 
    617          1.1  christos         default:
    618          1.1  christos 
    619  1.1.1.7.2.1  christos             AcpiOsPrintf ("AcpiExec: Region access on SpaceId %2.2X\n", SpaceId);
    620          1.1  christos             break;
    621          1.1  christos         }
    622          1.1  christos     }
    623          1.1  christos 
    624          1.1  christos     return (AE_OK);
    625          1.1  christos }
    626