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