Home | History | Annotate | Line # | Download | only in acpiexec
aeregion.c revision 1.1.1.6.4.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.6.4.1  pgoyette  * Copyright (C) 2000 - 2018, 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  christos     ACPI_SIZE               Length;
     85          1.1  christos     BOOLEAN                 BufferExists;
     86          1.1  christos     BOOLEAN                 BufferResize;
     87          1.1  christos     AE_REGION               *RegionElement;
     88          1.1  christos     void                    *BufferValue;
     89          1.1  christos     ACPI_STATUS             Status;
     90          1.1  christos     UINT32                  ByteWidth;
     91          1.1  christos     UINT32                  RegionLength;
     92          1.1  christos     UINT32                  i;
     93          1.1  christos     UINT8                   SpaceId;
     94          1.1  christos     ACPI_CONNECTION_INFO    *MyContext;
     95          1.1  christos     UINT32                  Value1;
     96          1.1  christos     UINT32                  Value2;
     97          1.1  christos     ACPI_RESOURCE           *Resource;
     98          1.1  christos 
     99          1.1  christos 
    100          1.1  christos     ACPI_FUNCTION_NAME (AeRegionHandler);
    101          1.1  christos 
    102          1.1  christos     /*
    103          1.1  christos      * If the object is not a region, simply return
    104          1.1  christos      */
    105          1.1  christos     if (RegionObject->Region.Type != ACPI_TYPE_REGION)
    106          1.1  christos     {
    107          1.1  christos         return (AE_OK);
    108          1.1  christos     }
    109          1.1  christos 
    110          1.1  christos     /* Check that we actually got back our context parameter */
    111          1.1  christos 
    112          1.1  christos     if (HandlerContext != &AeMyContext)
    113          1.1  christos     {
    114          1.1  christos         printf ("Region handler received incorrect context %p, should be %p\n",
    115          1.1  christos             HandlerContext, &AeMyContext);
    116          1.1  christos     }
    117          1.1  christos 
    118          1.1  christos     MyContext = ACPI_CAST_PTR (ACPI_CONNECTION_INFO, HandlerContext);
    119          1.1  christos 
    120          1.1  christos     /*
    121          1.1  christos      * Find the region's address space and length before searching
    122          1.1  christos      * the linked list.
    123          1.1  christos      */
    124          1.1  christos     BaseAddress = RegionObject->Region.Address;
    125          1.1  christos     Length = (ACPI_SIZE) RegionObject->Region.Length;
    126          1.1  christos     SpaceId = RegionObject->Region.SpaceId;
    127          1.1  christos 
    128      1.1.1.4  christos     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
    129  1.1.1.6.4.2  pgoyette         "Operation Region request on %s at 0x%X, BitWidth 0x%X, RegionLength 0x%X\n",
    130      1.1.1.4  christos         AcpiUtGetRegionName (RegionObject->Region.SpaceId),
    131  1.1.1.6.4.2  pgoyette         (UINT32) Address, BitWidth, (UINT32) Length));
    132          1.1  christos 
    133          1.1  christos     /*
    134          1.1  christos      * Region support can be disabled with the -do option.
    135          1.1  christos      * We use this to support dynamically loaded tables where we pass a valid
    136          1.1  christos      * address to the AML.
    137          1.1  christos      */
    138          1.1  christos     if (AcpiGbl_DbOpt_NoRegionSupport)
    139          1.1  christos     {
    140          1.1  christos         BufferValue = ACPI_TO_POINTER (Address);
    141          1.1  christos         ByteWidth = (BitWidth / 8);
    142          1.1  christos 
    143          1.1  christos         if (BitWidth % 8)
    144          1.1  christos         {
    145          1.1  christos             ByteWidth += 1;
    146          1.1  christos         }
    147          1.1  christos         goto DoFunction;
    148          1.1  christos     }
    149          1.1  christos 
    150          1.1  christos     switch (SpaceId)
    151          1.1  christos     {
    152          1.1  christos     case ACPI_ADR_SPACE_SYSTEM_IO:
    153          1.1  christos         /*
    154          1.1  christos          * For I/O space, exercise the port validation
    155          1.1  christos          * Note: ReadPort currently always returns all ones, length=BitLength
    156          1.1  christos          */
    157          1.1  christos         switch (Function & ACPI_IO_MASK)
    158          1.1  christos         {
    159          1.1  christos         case ACPI_READ:
    160          1.1  christos 
    161          1.1  christos             if (BitWidth == 64)
    162          1.1  christos             {
    163          1.1  christos                 /* Split the 64-bit request into two 32-bit requests */
    164          1.1  christos 
    165          1.1  christos                 Status = AcpiHwReadPort (Address, &Value1, 32);
    166      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
    167          1.1  christos                 Status = AcpiHwReadPort (Address+4, &Value2, 32);
    168      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
    169          1.1  christos 
    170          1.1  christos                 *Value = Value1 | ((UINT64) Value2 << 32);
    171          1.1  christos             }
    172          1.1  christos             else
    173          1.1  christos             {
    174          1.1  christos                 Status = AcpiHwReadPort (Address, &Value1, BitWidth);
    175      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
    176          1.1  christos                 *Value = (UINT64) Value1;
    177          1.1  christos             }
    178          1.1  christos             break;
    179          1.1  christos 
    180          1.1  christos         case ACPI_WRITE:
    181          1.1  christos 
    182          1.1  christos             if (BitWidth == 64)
    183          1.1  christos             {
    184          1.1  christos                 /* Split the 64-bit request into two 32-bit requests */
    185          1.1  christos 
    186          1.1  christos                 Status = AcpiHwWritePort (Address, ACPI_LODWORD (*Value), 32);
    187      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
    188          1.1  christos                 Status = AcpiHwWritePort (Address+4, ACPI_HIDWORD (*Value), 32);
    189      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
    190          1.1  christos             }
    191          1.1  christos             else
    192          1.1  christos             {
    193          1.1  christos                 Status = AcpiHwWritePort (Address, (UINT32) *Value, BitWidth);
    194      1.1.1.4  christos                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
    195          1.1  christos             }
    196          1.1  christos             break;
    197          1.1  christos 
    198          1.1  christos         default:
    199          1.1  christos 
    200          1.1  christos             Status = AE_BAD_PARAMETER;
    201          1.1  christos             break;
    202          1.1  christos         }
    203          1.1  christos 
    204          1.1  christos         if (ACPI_FAILURE (Status))
    205          1.1  christos         {
    206          1.1  christos             return (Status);
    207          1.1  christos         }
    208          1.1  christos 
    209          1.1  christos         /* Now go ahead and simulate the hardware */
    210          1.1  christos         break;
    211          1.1  christos 
    212          1.1  christos     /*
    213          1.1  christos      * SMBus and GenericSerialBus support the various bidirectional
    214          1.1  christos      * protocols.
    215          1.1  christos      */
    216          1.1  christos     case ACPI_ADR_SPACE_SMBUS:
    217          1.1  christos     case ACPI_ADR_SPACE_GSBUS:  /* ACPI 5.0 */
    218          1.1  christos 
    219          1.1  christos         Length = 0;
    220          1.1  christos 
    221          1.1  christos         switch (Function & ACPI_IO_MASK)
    222          1.1  christos         {
    223          1.1  christos         case ACPI_READ:
    224          1.1  christos 
    225          1.1  christos             switch (Function >> 16)
    226          1.1  christos             {
    227          1.1  christos             case AML_FIELD_ATTRIB_QUICK:
    228          1.1  christos 
    229          1.1  christos                 Length = 0;
    230          1.1  christos                 break;
    231          1.1  christos 
    232          1.1  christos             case AML_FIELD_ATTRIB_SEND_RCV:
    233          1.1  christos             case AML_FIELD_ATTRIB_BYTE:
    234          1.1  christos 
    235          1.1  christos                 Length = 1;
    236          1.1  christos                 break;
    237          1.1  christos 
    238          1.1  christos             case AML_FIELD_ATTRIB_WORD:
    239          1.1  christos             case AML_FIELD_ATTRIB_WORD_CALL:
    240          1.1  christos 
    241          1.1  christos                 Length = 2;
    242          1.1  christos                 break;
    243          1.1  christos 
    244          1.1  christos             case AML_FIELD_ATTRIB_BLOCK:
    245          1.1  christos             case AML_FIELD_ATTRIB_BLOCK_CALL:
    246          1.1  christos 
    247          1.1  christos                 Length = 32;
    248          1.1  christos                 break;
    249          1.1  christos 
    250          1.1  christos             case AML_FIELD_ATTRIB_MULTIBYTE:
    251          1.1  christos             case AML_FIELD_ATTRIB_RAW_BYTES:
    252          1.1  christos             case AML_FIELD_ATTRIB_RAW_PROCESS:
    253          1.1  christos 
    254          1.1  christos                 Length = MyContext->AccessLength;
    255          1.1  christos                 break;
    256          1.1  christos 
    257          1.1  christos             default:
    258          1.1  christos 
    259          1.1  christos                 break;
    260          1.1  christos             }
    261          1.1  christos             break;
    262          1.1  christos 
    263          1.1  christos         case ACPI_WRITE:
    264          1.1  christos 
    265          1.1  christos             switch (Function >> 16)
    266          1.1  christos             {
    267          1.1  christos             case AML_FIELD_ATTRIB_QUICK:
    268          1.1  christos             case AML_FIELD_ATTRIB_SEND_RCV:
    269          1.1  christos             case AML_FIELD_ATTRIB_BYTE:
    270          1.1  christos             case AML_FIELD_ATTRIB_WORD:
    271          1.1  christos             case AML_FIELD_ATTRIB_BLOCK:
    272          1.1  christos 
    273          1.1  christos                 Length = 0;
    274          1.1  christos                 break;
    275          1.1  christos 
    276          1.1  christos             case AML_FIELD_ATTRIB_WORD_CALL:
    277          1.1  christos                 Length = 2;
    278          1.1  christos                 break;
    279          1.1  christos 
    280          1.1  christos             case AML_FIELD_ATTRIB_BLOCK_CALL:
    281          1.1  christos                 Length = 32;
    282          1.1  christos                 break;
    283          1.1  christos 
    284          1.1  christos             case AML_FIELD_ATTRIB_MULTIBYTE:
    285          1.1  christos             case AML_FIELD_ATTRIB_RAW_BYTES:
    286          1.1  christos             case AML_FIELD_ATTRIB_RAW_PROCESS:
    287          1.1  christos 
    288          1.1  christos                 Length = MyContext->AccessLength;
    289          1.1  christos                 break;
    290          1.1  christos 
    291          1.1  christos             default:
    292          1.1  christos 
    293          1.1  christos                 break;
    294          1.1  christos             }
    295          1.1  christos             break;
    296          1.1  christos 
    297          1.1  christos         default:
    298          1.1  christos 
    299          1.1  christos             break;
    300          1.1  christos         }
    301          1.1  christos 
    302          1.1  christos         if (AcpiGbl_DisplayRegionAccess)
    303          1.1  christos         {
    304          1.1  christos             AcpiOsPrintf ("AcpiExec: %s "
    305  1.1.1.6.4.2  pgoyette                 "%s: Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X",
    306          1.1  christos                 AcpiUtGetRegionName (SpaceId),
    307          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    308          1.1  christos                 (UINT32) (Function >> 16),
    309          1.1  christos                 (UINT32) Address, (UINT32) BaseAddress,
    310          1.1  christos                 Length, BitWidth, Buffer[1]);
    311          1.1  christos 
    312          1.1  christos             /* GenericSerialBus has a Connection() parameter */
    313          1.1  christos 
    314          1.1  christos             if (SpaceId == ACPI_ADR_SPACE_GSBUS)
    315          1.1  christos             {
    316          1.1  christos                 Status = AcpiBufferToResource (MyContext->Connection,
    317          1.1  christos                     MyContext->Length, &Resource);
    318          1.1  christos 
    319  1.1.1.6.4.2  pgoyette                 AcpiOsPrintf (" [AccessLength %.2X Connnection %p]",
    320          1.1  christos                     MyContext->AccessLength, MyContext->Connection);
    321          1.1  christos             }
    322          1.1  christos             AcpiOsPrintf ("\n");
    323          1.1  christos         }
    324          1.1  christos 
    325          1.1  christos         /* Setup the return buffer. Note: ASLTS depends on these fill values */
    326          1.1  christos 
    327          1.1  christos         for (i = 0; i < Length; i++)
    328          1.1  christos         {
    329          1.1  christos             Buffer[i+2] = (UINT8) (0xA0 + i);
    330          1.1  christos         }
    331          1.1  christos 
    332          1.1  christos         Buffer[0] = 0x7A;
    333          1.1  christos         Buffer[1] = (UINT8) Length;
    334          1.1  christos         return (AE_OK);
    335          1.1  christos 
    336          1.1  christos 
    337          1.1  christos     case ACPI_ADR_SPACE_IPMI: /* ACPI 4.0 */
    338          1.1  christos 
    339          1.1  christos         if (AcpiGbl_DisplayRegionAccess)
    340          1.1  christos         {
    341          1.1  christos             AcpiOsPrintf ("AcpiExec: IPMI "
    342          1.1  christos                 "%s: Attr %X Addr %.4X BaseAddr %.4X Len %.2X Width %X BufLen %X\n",
    343          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    344          1.1  christos                 (UINT32) (Function >> 16), (UINT32) Address, (UINT32) BaseAddress,
    345          1.1  christos                 Length, BitWidth, Buffer[1]);
    346          1.1  christos         }
    347          1.1  christos 
    348          1.1  christos         /*
    349          1.1  christos          * Regardless of a READ or WRITE, this handler is passed a 66-byte
    350          1.1  christos          * buffer in which to return the IPMI status/length/data.
    351          1.1  christos          *
    352          1.1  christos          * Return some example data to show use of the bidirectional buffer
    353          1.1  christos          */
    354          1.1  christos         Buffer[0] = 0;       /* Status byte */
    355          1.1  christos         Buffer[1] = 64;      /* Return buffer data length */
    356          1.1  christos         Buffer[2] = 0;       /* Completion code */
    357          1.1  christos         Buffer[3] = 0;       /* Reserved */
    358          1.1  christos 
    359          1.1  christos         /*
    360          1.1  christos          * Fill the 66-byte buffer with the return data.
    361          1.1  christos          * Note: ASLTS depends on these fill values.
    362          1.1  christos          */
    363          1.1  christos         for (i = 4; i < 66; i++)
    364          1.1  christos         {
    365          1.1  christos             Buffer[i] = (UINT8) (i);
    366          1.1  christos         }
    367          1.1  christos         return (AE_OK);
    368          1.1  christos 
    369          1.1  christos     /*
    370          1.1  christos      * GPIO has some special semantics:
    371          1.1  christos      * 1) Address is the pin number index into the Connection() pin list
    372          1.1  christos      * 2) BitWidth is the actual number of bits (pins) defined by the field
    373          1.1  christos      */
    374          1.1  christos     case ACPI_ADR_SPACE_GPIO: /* ACPI 5.0 */
    375          1.1  christos 
    376          1.1  christos         if (AcpiGbl_DisplayRegionAccess)
    377          1.1  christos         {
    378          1.1  christos             AcpiOsPrintf ("AcpiExec: GPIO "
    379          1.1  christos                 "%s: Addr %.4X Width %X Conn %p\n",
    380          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    381          1.1  christos                 (UINT32) Address, BitWidth, MyContext->Connection);
    382          1.1  christos         }
    383          1.1  christos         return (AE_OK);
    384          1.1  christos 
    385          1.1  christos     default:
    386          1.1  christos         break;
    387          1.1  christos     }
    388          1.1  christos 
    389          1.1  christos     /*
    390          1.1  christos      * Search through the linked list for this region's buffer
    391          1.1  christos      */
    392          1.1  christos     BufferExists = FALSE;
    393          1.1  christos     BufferResize = FALSE;
    394          1.1  christos     RegionElement = AeRegions.RegionList;
    395          1.1  christos 
    396          1.1  christos     if (AeRegions.NumberOfRegions)
    397          1.1  christos     {
    398          1.1  christos         BaseAddressEnd = BaseAddress + Length - 1;
    399          1.1  christos         while (!BufferExists && RegionElement)
    400          1.1  christos         {
    401          1.1  christos             RegionAddress = RegionElement->Address;
    402          1.1  christos             RegionAddressEnd = RegionElement->Address + RegionElement->Length - 1;
    403          1.1  christos             RegionLength = RegionElement->Length;
    404          1.1  christos 
    405          1.1  christos             /*
    406          1.1  christos              * Overlapping Region Support
    407          1.1  christos              *
    408          1.1  christos              * While searching through the region buffer list, determine if an
    409          1.1  christos              * overlap exists between the requested buffer space and the current
    410          1.1  christos              * RegionElement space. If there is an overlap then replace the old
    411          1.1  christos              * buffer with a new buffer of increased size before continuing to
    412          1.1  christos              * do the read or write
    413          1.1  christos              */
    414          1.1  christos             if (RegionElement->SpaceId != SpaceId ||
    415          1.1  christos                 BaseAddressEnd < RegionAddress ||
    416          1.1  christos                 BaseAddress > RegionAddressEnd)
    417          1.1  christos             {
    418          1.1  christos                 /*
    419          1.1  christos                  * Requested buffer is outside of the current RegionElement
    420          1.1  christos                  * bounds
    421          1.1  christos                  */
    422          1.1  christos                 RegionElement = RegionElement->NextRegion;
    423          1.1  christos             }
    424          1.1  christos             else
    425          1.1  christos             {
    426          1.1  christos                 /*
    427          1.1  christos                  * Some amount of buffer space sharing exists. There are 4 cases
    428          1.1  christos                  * to consider:
    429          1.1  christos                  *
    430          1.1  christos                  * 1. Right overlap
    431          1.1  christos                  * 2. Left overlap
    432          1.1  christos                  * 3. Left and right overlap
    433          1.1  christos                  * 4. Fully contained - no resizing required
    434          1.1  christos                  */
    435          1.1  christos                 BufferExists = TRUE;
    436          1.1  christos 
    437          1.1  christos                 if ((BaseAddress >= RegionAddress) &&
    438          1.1  christos                     (BaseAddress <= RegionAddressEnd) &&
    439          1.1  christos                     (BaseAddressEnd > RegionAddressEnd))
    440          1.1  christos                 {
    441          1.1  christos                     /* Right overlap */
    442          1.1  christos 
    443      1.1.1.2  christos                     RegionElement->Length = (UINT32) (BaseAddress -
    444      1.1.1.2  christos                         RegionAddress + Length);
    445          1.1  christos                     BufferResize = TRUE;
    446          1.1  christos                 }
    447          1.1  christos 
    448          1.1  christos                 else if ((BaseAddressEnd >= RegionAddress) &&
    449          1.1  christos                          (BaseAddressEnd <= RegionAddressEnd) &&
    450          1.1  christos                          (BaseAddress < RegionAddress))
    451          1.1  christos                 {
    452          1.1  christos                     /* Left overlap */
    453          1.1  christos 
    454          1.1  christos                     RegionElement->Address = BaseAddress;
    455      1.1.1.2  christos                     RegionElement->Length = (UINT32) (RegionAddress -
    456      1.1.1.2  christos                         BaseAddress + RegionElement->Length);
    457          1.1  christos                     BufferResize = TRUE;
    458          1.1  christos                 }
    459          1.1  christos 
    460          1.1  christos                 else if ((BaseAddress < RegionAddress) &&
    461          1.1  christos                          (BaseAddressEnd > RegionAddressEnd))
    462          1.1  christos                 {
    463          1.1  christos                     /* Left and right overlap */
    464          1.1  christos 
    465          1.1  christos                     RegionElement->Address = BaseAddress;
    466          1.1  christos                     RegionElement->Length = Length;
    467          1.1  christos                     BufferResize = TRUE;
    468          1.1  christos                 }
    469          1.1  christos 
    470          1.1  christos                 /*
    471          1.1  christos                  * only remaining case is fully contained for which we don't
    472          1.1  christos                  * need to do anything
    473          1.1  christos                  */
    474          1.1  christos                 if (BufferResize)
    475          1.1  christos                 {
    476          1.1  christos                     NewBuffer = AcpiOsAllocate (RegionElement->Length);
    477          1.1  christos                     if (!NewBuffer)
    478          1.1  christos                     {
    479          1.1  christos                         return (AE_NO_MEMORY);
    480          1.1  christos                     }
    481          1.1  christos 
    482          1.1  christos                     OldBuffer = RegionElement->Buffer;
    483          1.1  christos                     RegionElement->Buffer = NewBuffer;
    484          1.1  christos                     NewBuffer = NULL;
    485          1.1  christos 
    486          1.1  christos                     /* Initialize the region with the default fill value */
    487          1.1  christos 
    488      1.1.1.3  christos                     memset (RegionElement->Buffer,
    489          1.1  christos                         AcpiGbl_RegionFillValue, RegionElement->Length);
    490          1.1  christos 
    491          1.1  christos                     /*
    492          1.1  christos                      * Get BufferValue to point (within the new buffer) to the
    493          1.1  christos                      * base address of the old buffer
    494          1.1  christos                      */
    495          1.1  christos                     BufferValue = (UINT8 *) RegionElement->Buffer +
    496          1.1  christos                         (UINT64) RegionAddress -
    497          1.1  christos                         (UINT64) RegionElement->Address;
    498          1.1  christos 
    499          1.1  christos                     /*
    500          1.1  christos                      * Copy the old buffer to its same location within the new
    501          1.1  christos                      * buffer
    502          1.1  christos                      */
    503      1.1.1.3  christos                     memcpy (BufferValue, OldBuffer, RegionLength);
    504          1.1  christos                     AcpiOsFree (OldBuffer);
    505          1.1  christos                 }
    506          1.1  christos             }
    507          1.1  christos         }
    508          1.1  christos     }
    509          1.1  christos 
    510          1.1  christos     /*
    511          1.1  christos      * If the Region buffer does not exist, create it now
    512          1.1  christos      */
    513          1.1  christos     if (!BufferExists)
    514          1.1  christos     {
    515          1.1  christos         /* Do the memory allocations first */
    516          1.1  christos 
    517          1.1  christos         RegionElement = AcpiOsAllocate (sizeof (AE_REGION));
    518          1.1  christos         if (!RegionElement)
    519          1.1  christos         {
    520          1.1  christos             return (AE_NO_MEMORY);
    521          1.1  christos         }
    522          1.1  christos 
    523          1.1  christos         RegionElement->Buffer = AcpiOsAllocate (Length);
    524          1.1  christos         if (!RegionElement->Buffer)
    525          1.1  christos         {
    526          1.1  christos             AcpiOsFree (RegionElement);
    527          1.1  christos             return (AE_NO_MEMORY);
    528          1.1  christos         }
    529          1.1  christos 
    530          1.1  christos         /* Initialize the region with the default fill value */
    531          1.1  christos 
    532      1.1.1.3  christos         memset (RegionElement->Buffer, AcpiGbl_RegionFillValue, Length);
    533          1.1  christos 
    534          1.1  christos         RegionElement->Address      = BaseAddress;
    535          1.1  christos         RegionElement->Length       = Length;
    536          1.1  christos         RegionElement->SpaceId      = SpaceId;
    537          1.1  christos         RegionElement->NextRegion   = NULL;
    538          1.1  christos 
    539          1.1  christos         /*
    540          1.1  christos          * Increment the number of regions and put this one
    541          1.1  christos          * at the head of the list as it will probably get accessed
    542          1.1  christos          * more often anyway.
    543          1.1  christos          */
    544          1.1  christos         AeRegions.NumberOfRegions += 1;
    545          1.1  christos 
    546          1.1  christos         if (AeRegions.RegionList)
    547          1.1  christos         {
    548          1.1  christos             RegionElement->NextRegion = AeRegions.RegionList;
    549          1.1  christos         }
    550          1.1  christos 
    551          1.1  christos         AeRegions.RegionList = RegionElement;
    552          1.1  christos     }
    553          1.1  christos 
    554          1.1  christos     /* Calculate the size of the memory copy */
    555          1.1  christos 
    556          1.1  christos     ByteWidth = (BitWidth / 8);
    557          1.1  christos 
    558          1.1  christos     if (BitWidth % 8)
    559          1.1  christos     {
    560          1.1  christos         ByteWidth += 1;
    561          1.1  christos     }
    562          1.1  christos 
    563          1.1  christos     /*
    564          1.1  christos      * The buffer exists and is pointed to by RegionElement.
    565          1.1  christos      * We now need to verify the request is valid and perform the operation.
    566          1.1  christos      *
    567          1.1  christos      * NOTE: RegionElement->Length is in bytes, therefore it we compare against
    568          1.1  christos      * ByteWidth (see above)
    569          1.1  christos      */
    570          1.1  christos     if ((RegionObject->Region.SpaceId != ACPI_ADR_SPACE_GPIO) &&
    571          1.1  christos         ((UINT64) Address + ByteWidth) >
    572          1.1  christos         ((UINT64)(RegionElement->Address) + RegionElement->Length))
    573          1.1  christos     {
    574          1.1  christos         ACPI_WARNING ((AE_INFO,
    575      1.1.1.4  christos             "Request on [%4.4s] is beyond region limit "
    576      1.1.1.4  christos             "Req-0x%X+0x%X, Base=0x%X, Len-0x%X",
    577          1.1  christos             (RegionObject->Region.Node)->Name.Ascii, (UINT32) Address,
    578          1.1  christos             ByteWidth, (UINT32)(RegionElement->Address),
    579          1.1  christos             RegionElement->Length));
    580          1.1  christos 
    581          1.1  christos         return (AE_AML_REGION_LIMIT);
    582          1.1  christos     }
    583          1.1  christos 
    584          1.1  christos     /*
    585          1.1  christos      * Get BufferValue to point to the "address" in the buffer
    586          1.1  christos      */
    587          1.1  christos     BufferValue = ((UINT8 *) RegionElement->Buffer +
    588      1.1.1.4  christos         ((UINT64) Address - (UINT64) RegionElement->Address));
    589          1.1  christos 
    590          1.1  christos DoFunction:
    591          1.1  christos     /*
    592          1.1  christos      * Perform a read or write to the buffer space
    593          1.1  christos      */
    594          1.1  christos     switch (Function)
    595          1.1  christos     {
    596          1.1  christos     case ACPI_READ:
    597          1.1  christos         /*
    598          1.1  christos          * Set the pointer Value to whatever is in the buffer
    599          1.1  christos          */
    600      1.1.1.3  christos         memcpy (Value, BufferValue, ByteWidth);
    601          1.1  christos         break;
    602          1.1  christos 
    603          1.1  christos     case ACPI_WRITE:
    604          1.1  christos         /*
    605          1.1  christos          * Write the contents of Value to the buffer
    606          1.1  christos          */
    607      1.1.1.3  christos         memcpy (BufferValue, Value, ByteWidth);
    608          1.1  christos         break;
    609          1.1  christos 
    610          1.1  christos     default:
    611          1.1  christos 
    612          1.1  christos         return (AE_BAD_PARAMETER);
    613          1.1  christos     }
    614          1.1  christos 
    615          1.1  christos     if (AcpiGbl_DisplayRegionAccess)
    616          1.1  christos     {
    617          1.1  christos         switch (SpaceId)
    618          1.1  christos         {
    619          1.1  christos         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
    620          1.1  christos 
    621          1.1  christos             AcpiOsPrintf ("AcpiExec: SystemMemory "
    622          1.1  christos                 "%s: Val %.8X Addr %.4X Width %X [REGION: BaseAddr %.4X Len %.2X]\n",
    623          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
    624          1.1  christos                 (UINT32) *Value, (UINT32) Address, BitWidth, (UINT32) BaseAddress, Length);
    625          1.1  christos             break;
    626          1.1  christos 
    627          1.1  christos         case ACPI_ADR_SPACE_GPIO:   /* ACPI 5.0 */
    628          1.1  christos 
    629          1.1  christos             /* This space is required to always be ByteAcc */
    630          1.1  christos 
    631          1.1  christos             Status = AcpiBufferToResource (MyContext->Connection,
    632          1.1  christos                 MyContext->Length, &Resource);
    633          1.1  christos 
    634          1.1  christos             AcpiOsPrintf ("AcpiExec: GeneralPurposeIo "
    635          1.1  christos                 "%s: Val %.8X Addr %.4X BaseAddr %.4X Len %.2X Width %X AccLen %.2X Conn %p\n",
    636          1.1  christos                 (Function & ACPI_IO_MASK) ? "Write" : "Read ", (UINT32) *Value,
    637          1.1  christos                 (UINT32) Address, (UINT32) BaseAddress, Length, BitWidth,
    638          1.1  christos                 MyContext->AccessLength, MyContext->Connection);
    639          1.1  christos             break;
    640          1.1  christos 
    641          1.1  christos         default:
    642          1.1  christos 
    643          1.1  christos             break;
    644          1.1  christos         }
    645          1.1  christos     }
    646          1.1  christos 
    647          1.1  christos     return (AE_OK);
    648          1.1  christos }
    649