Home | History | Annotate | Line # | Download | only in hardware
hwvalid.c revision 1.1.1.4.2.4
      1          1.1    jruoho /******************************************************************************
      2          1.1    jruoho  *
      3          1.1    jruoho  * Module Name: hwvalid - I/O request validation
      4          1.1    jruoho  *
      5          1.1    jruoho  *****************************************************************************/
      6          1.1    jruoho 
      7      1.1.1.2    jruoho /*
      8  1.1.1.4.2.3     skrll  * Copyright (C) 2000 - 2017, Intel Corp.
      9          1.1    jruoho  * All rights reserved.
     10          1.1    jruoho  *
     11      1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12      1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13      1.1.1.2    jruoho  * are met:
     14      1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15      1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16      1.1.1.2    jruoho  *    without modification.
     17      1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21      1.1.1.2    jruoho  *    binary redistribution.
     22      1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24      1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25      1.1.1.2    jruoho  *
     26      1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27      1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.1.1.2    jruoho  * Software Foundation.
     29      1.1.1.2    jruoho  *
     30      1.1.1.2    jruoho  * NO WARRANTY
     31      1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42      1.1.1.2    jruoho  */
     43          1.1    jruoho 
     44          1.1    jruoho #include "acpi.h"
     45          1.1    jruoho #include "accommon.h"
     46          1.1    jruoho 
     47          1.1    jruoho #define _COMPONENT          ACPI_HARDWARE
     48          1.1    jruoho         ACPI_MODULE_NAME    ("hwvalid")
     49          1.1    jruoho 
     50          1.1    jruoho /* Local prototypes */
     51          1.1    jruoho 
     52          1.1    jruoho static ACPI_STATUS
     53          1.1    jruoho AcpiHwValidateIoRequest (
     54          1.1    jruoho     ACPI_IO_ADDRESS         Address,
     55          1.1    jruoho     UINT32                  BitWidth);
     56          1.1    jruoho 
     57          1.1    jruoho 
     58          1.1    jruoho /*
     59          1.1    jruoho  * Protected I/O ports. Some ports are always illegal, and some are
     60          1.1    jruoho  * conditionally illegal. This table must remain ordered by port address.
     61          1.1    jruoho  *
     62          1.1    jruoho  * The table is used to implement the Microsoft port access rules that
     63          1.1    jruoho  * first appeared in Windows XP. Some ports are always illegal, and some
     64          1.1    jruoho  * ports are only illegal if the BIOS calls _OSI with a WinXP string or
     65          1.1    jruoho  * later (meaning that the BIOS itelf is post-XP.)
     66          1.1    jruoho  *
     67          1.1    jruoho  * This provides ACPICA with the desired port protections and
     68          1.1    jruoho  * Microsoft compatibility.
     69          1.1    jruoho  *
     70          1.1    jruoho  * Description of port entries:
     71          1.1    jruoho  *  DMA:   DMA controller
     72          1.1    jruoho  *  PIC0:  Programmable Interrupt Controller (8259A)
     73          1.1    jruoho  *  PIT1:  System Timer 1
     74          1.1    jruoho  *  PIT2:  System Timer 2 failsafe
     75          1.1    jruoho  *  RTC:   Real-time clock
     76          1.1    jruoho  *  CMOS:  Extended CMOS
     77          1.1    jruoho  *  DMA1:  DMA 1 page registers
     78          1.1    jruoho  *  DMA1L: DMA 1 Ch 0 low page
     79          1.1    jruoho  *  DMA2:  DMA 2 page registers
     80          1.1    jruoho  *  DMA2L: DMA 2 low page refresh
     81          1.1    jruoho  *  ARBC:  Arbitration control
     82          1.1    jruoho  *  SETUP: Reserved system board setup
     83          1.1    jruoho  *  POS:   POS channel select
     84          1.1    jruoho  *  PIC1:  Cascaded PIC
     85          1.1    jruoho  *  IDMA:  ISA DMA
     86          1.1    jruoho  *  ELCR:  PIC edge/level registers
     87          1.1    jruoho  *  PCI:   PCI configuration space
     88          1.1    jruoho  */
     89          1.1    jruoho static const ACPI_PORT_INFO     AcpiProtectedPorts[] =
     90          1.1    jruoho {
     91          1.1    jruoho     {"DMA",     0x0000, 0x000F, ACPI_OSI_WIN_XP},
     92          1.1    jruoho     {"PIC0",    0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL},
     93          1.1    jruoho     {"PIT1",    0x0040, 0x0043, ACPI_OSI_WIN_XP},
     94          1.1    jruoho     {"PIT2",    0x0048, 0x004B, ACPI_OSI_WIN_XP},
     95          1.1    jruoho     {"RTC",     0x0070, 0x0071, ACPI_OSI_WIN_XP},
     96          1.1    jruoho     {"CMOS",    0x0074, 0x0076, ACPI_OSI_WIN_XP},
     97          1.1    jruoho     {"DMA1",    0x0081, 0x0083, ACPI_OSI_WIN_XP},
     98          1.1    jruoho     {"DMA1L",   0x0087, 0x0087, ACPI_OSI_WIN_XP},
     99          1.1    jruoho     {"DMA2",    0x0089, 0x008B, ACPI_OSI_WIN_XP},
    100          1.1    jruoho     {"DMA2L",   0x008F, 0x008F, ACPI_OSI_WIN_XP},
    101          1.1    jruoho     {"ARBC",    0x0090, 0x0091, ACPI_OSI_WIN_XP},
    102          1.1    jruoho     {"SETUP",   0x0093, 0x0094, ACPI_OSI_WIN_XP},
    103          1.1    jruoho     {"POS",     0x0096, 0x0097, ACPI_OSI_WIN_XP},
    104          1.1    jruoho     {"PIC1",    0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL},
    105          1.1    jruoho     {"IDMA",    0x00C0, 0x00DF, ACPI_OSI_WIN_XP},
    106          1.1    jruoho     {"ELCR",    0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL},
    107          1.1    jruoho     {"PCI",     0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP}
    108          1.1    jruoho };
    109          1.1    jruoho 
    110  1.1.1.4.2.4     skrll #define ACPI_PORT_INFO_ENTRIES      ACPI_ARRAY_LENGTH (AcpiProtectedPorts)
    111          1.1    jruoho 
    112          1.1    jruoho 
    113          1.1    jruoho /******************************************************************************
    114          1.1    jruoho  *
    115          1.1    jruoho  * FUNCTION:    AcpiHwValidateIoRequest
    116          1.1    jruoho  *
    117          1.1    jruoho  * PARAMETERS:  Address             Address of I/O port/register
    118          1.1    jruoho  *              BitWidth            Number of bits (8,16,32)
    119          1.1    jruoho  *
    120          1.1    jruoho  * RETURN:      Status
    121          1.1    jruoho  *
    122          1.1    jruoho  * DESCRIPTION: Validates an I/O request (address/length). Certain ports are
    123          1.1    jruoho  *              always illegal and some ports are only illegal depending on
    124          1.1    jruoho  *              the requests the BIOS AML code makes to the predefined
    125          1.1    jruoho  *              _OSI method.
    126          1.1    jruoho  *
    127          1.1    jruoho  ******************************************************************************/
    128          1.1    jruoho 
    129          1.1    jruoho static ACPI_STATUS
    130          1.1    jruoho AcpiHwValidateIoRequest (
    131          1.1    jruoho     ACPI_IO_ADDRESS         Address,
    132          1.1    jruoho     UINT32                  BitWidth)
    133          1.1    jruoho {
    134          1.1    jruoho     UINT32                  i;
    135          1.1    jruoho     UINT32                  ByteWidth;
    136          1.1    jruoho     ACPI_IO_ADDRESS         LastAddress;
    137          1.1    jruoho     const ACPI_PORT_INFO    *PortInfo;
    138          1.1    jruoho 
    139          1.1    jruoho 
    140  1.1.1.4.2.4     skrll     ACPI_FUNCTION_NAME (HwValidateIoRequest);
    141          1.1    jruoho 
    142          1.1    jruoho 
    143          1.1    jruoho     /* Supported widths are 8/16/32 */
    144          1.1    jruoho 
    145          1.1    jruoho     if ((BitWidth != 8) &&
    146          1.1    jruoho         (BitWidth != 16) &&
    147          1.1    jruoho         (BitWidth != 32))
    148          1.1    jruoho     {
    149      1.1.1.3  christos         ACPI_ERROR ((AE_INFO,
    150      1.1.1.3  christos             "Bad BitWidth parameter: %8.8X", BitWidth));
    151          1.1    jruoho         return (AE_BAD_PARAMETER);
    152          1.1    jruoho     }
    153          1.1    jruoho 
    154          1.1    jruoho     PortInfo = AcpiProtectedPorts;
    155          1.1    jruoho     ByteWidth = ACPI_DIV_8 (BitWidth);
    156          1.1    jruoho     LastAddress = Address + ByteWidth - 1;
    157          1.1    jruoho 
    158  1.1.1.4.2.1     skrll     ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Address %8.8X%8.8X LastAddress %8.8X%8.8X Length %X",
    159  1.1.1.4.2.1     skrll         ACPI_FORMAT_UINT64 (Address), ACPI_FORMAT_UINT64 (LastAddress),
    160          1.1    jruoho         ByteWidth));
    161          1.1    jruoho 
    162          1.1    jruoho     /* Maximum 16-bit address in I/O space */
    163          1.1    jruoho 
    164          1.1    jruoho     if (LastAddress > ACPI_UINT16_MAX)
    165          1.1    jruoho     {
    166          1.1    jruoho         ACPI_ERROR ((AE_INFO,
    167  1.1.1.4.2.1     skrll             "Illegal I/O port address/length above 64K: %8.8X%8.8X/0x%X",
    168  1.1.1.4.2.1     skrll             ACPI_FORMAT_UINT64 (Address), ByteWidth));
    169  1.1.1.4.2.4     skrll         return (AE_LIMIT);
    170          1.1    jruoho     }
    171          1.1    jruoho 
    172          1.1    jruoho     /* Exit if requested address is not within the protected port table */
    173          1.1    jruoho 
    174          1.1    jruoho     if (Address > AcpiProtectedPorts[ACPI_PORT_INFO_ENTRIES - 1].End)
    175          1.1    jruoho     {
    176  1.1.1.4.2.4     skrll         return (AE_OK);
    177          1.1    jruoho     }
    178          1.1    jruoho 
    179          1.1    jruoho     /* Check request against the list of protected I/O ports */
    180          1.1    jruoho 
    181          1.1    jruoho     for (i = 0; i < ACPI_PORT_INFO_ENTRIES; i++, PortInfo++)
    182          1.1    jruoho     {
    183          1.1    jruoho         /*
    184          1.1    jruoho          * Check if the requested address range will write to a reserved
    185  1.1.1.4.2.4     skrll          * port. There are four cases to consider:
    186          1.1    jruoho          *
    187          1.1    jruoho          * 1) Address range is contained completely in the port address range
    188          1.1    jruoho          * 2) Address range overlaps port range at the port range start
    189          1.1    jruoho          * 3) Address range overlaps port range at the port range end
    190          1.1    jruoho          * 4) Address range completely encompasses the port range
    191          1.1    jruoho          */
    192          1.1    jruoho         if ((Address <= PortInfo->End) && (LastAddress >= PortInfo->Start))
    193          1.1    jruoho         {
    194          1.1    jruoho             /* Port illegality may depend on the _OSI calls made by the BIOS */
    195          1.1    jruoho 
    196          1.1    jruoho             if (AcpiGbl_OsiData >= PortInfo->OsiDependency)
    197          1.1    jruoho             {
    198          1.1    jruoho                 ACPI_DEBUG_PRINT ((ACPI_DB_IO,
    199  1.1.1.4.2.1     skrll                     "Denied AML access to port 0x%8.8X%8.8X/%X (%s 0x%.4X-0x%.4X)",
    200  1.1.1.4.2.1     skrll                     ACPI_FORMAT_UINT64 (Address), ByteWidth, PortInfo->Name,
    201          1.1    jruoho                     PortInfo->Start, PortInfo->End));
    202          1.1    jruoho 
    203          1.1    jruoho                 return_ACPI_STATUS (AE_AML_ILLEGAL_ADDRESS);
    204          1.1    jruoho             }
    205          1.1    jruoho         }
    206          1.1    jruoho 
    207          1.1    jruoho         /* Finished if address range ends before the end of this port */
    208          1.1    jruoho 
    209          1.1    jruoho         if (LastAddress <= PortInfo->End)
    210          1.1    jruoho         {
    211          1.1    jruoho             break;
    212          1.1    jruoho         }
    213          1.1    jruoho     }
    214          1.1    jruoho 
    215  1.1.1.4.2.4     skrll     return (AE_OK);
    216          1.1    jruoho }
    217          1.1    jruoho 
    218          1.1    jruoho 
    219          1.1    jruoho /******************************************************************************
    220          1.1    jruoho  *
    221          1.1    jruoho  * FUNCTION:    AcpiHwReadPort
    222          1.1    jruoho  *
    223          1.1    jruoho  * PARAMETERS:  Address             Address of I/O port/register to read
    224  1.1.1.4.2.4     skrll  *              Value               Where value (data) is returned
    225          1.1    jruoho  *              Width               Number of bits
    226          1.1    jruoho  *
    227          1.1    jruoho  * RETURN:      Status and value read from port
    228          1.1    jruoho  *
    229          1.1    jruoho  * DESCRIPTION: Read data from an I/O port or register. This is a front-end
    230          1.1    jruoho  *              to AcpiOsReadPort that performs validation on both the port
    231          1.1    jruoho  *              address and the length.
    232          1.1    jruoho  *
    233          1.1    jruoho  *****************************************************************************/
    234          1.1    jruoho 
    235          1.1    jruoho ACPI_STATUS
    236          1.1    jruoho AcpiHwReadPort (
    237          1.1    jruoho     ACPI_IO_ADDRESS         Address,
    238          1.1    jruoho     UINT32                  *Value,
    239          1.1    jruoho     UINT32                  Width)
    240          1.1    jruoho {
    241          1.1    jruoho     ACPI_STATUS             Status;
    242          1.1    jruoho     UINT32                  OneByte;
    243          1.1    jruoho     UINT32                  i;
    244          1.1    jruoho 
    245          1.1    jruoho 
    246          1.1    jruoho     /* Truncate address to 16 bits if requested */
    247          1.1    jruoho 
    248          1.1    jruoho     if (AcpiGbl_TruncateIoAddresses)
    249          1.1    jruoho     {
    250          1.1    jruoho         Address &= ACPI_UINT16_MAX;
    251          1.1    jruoho     }
    252          1.1    jruoho 
    253          1.1    jruoho     /* Validate the entire request and perform the I/O */
    254          1.1    jruoho 
    255          1.1    jruoho     Status = AcpiHwValidateIoRequest (Address, Width);
    256          1.1    jruoho     if (ACPI_SUCCESS (Status))
    257          1.1    jruoho     {
    258          1.1    jruoho         Status = AcpiOsReadPort (Address, Value, Width);
    259          1.1    jruoho         return (Status);
    260          1.1    jruoho     }
    261          1.1    jruoho 
    262          1.1    jruoho     if (Status != AE_AML_ILLEGAL_ADDRESS)
    263          1.1    jruoho     {
    264          1.1    jruoho         return (Status);
    265          1.1    jruoho     }
    266          1.1    jruoho 
    267          1.1    jruoho     /*
    268          1.1    jruoho      * There has been a protection violation within the request. Fall
    269          1.1    jruoho      * back to byte granularity port I/O and ignore the failing bytes.
    270  1.1.1.4.2.4     skrll      * This provides compatibility with other ACPI implementations.
    271          1.1    jruoho      */
    272          1.1    jruoho     for (i = 0, *Value = 0; i < Width; i += 8)
    273          1.1    jruoho     {
    274          1.1    jruoho         /* Validate and read one byte */
    275          1.1    jruoho 
    276          1.1    jruoho         if (AcpiHwValidateIoRequest (Address, 8) == AE_OK)
    277          1.1    jruoho         {
    278          1.1    jruoho             Status = AcpiOsReadPort (Address, &OneByte, 8);
    279          1.1    jruoho             if (ACPI_FAILURE (Status))
    280          1.1    jruoho             {
    281          1.1    jruoho                 return (Status);
    282          1.1    jruoho             }
    283          1.1    jruoho 
    284          1.1    jruoho             *Value |= (OneByte << i);
    285          1.1    jruoho         }
    286          1.1    jruoho 
    287          1.1    jruoho         Address++;
    288          1.1    jruoho     }
    289          1.1    jruoho 
    290          1.1    jruoho     return (AE_OK);
    291          1.1    jruoho }
    292          1.1    jruoho 
    293          1.1    jruoho 
    294          1.1    jruoho /******************************************************************************
    295          1.1    jruoho  *
    296          1.1    jruoho  * FUNCTION:    AcpiHwWritePort
    297          1.1    jruoho  *
    298          1.1    jruoho  * PARAMETERS:  Address             Address of I/O port/register to write
    299          1.1    jruoho  *              Value               Value to write
    300          1.1    jruoho  *              Width               Number of bits
    301          1.1    jruoho  *
    302          1.1    jruoho  * RETURN:      Status
    303          1.1    jruoho  *
    304          1.1    jruoho  * DESCRIPTION: Write data to an I/O port or register. This is a front-end
    305          1.1    jruoho  *              to AcpiOsWritePort that performs validation on both the port
    306          1.1    jruoho  *              address and the length.
    307          1.1    jruoho  *
    308          1.1    jruoho  *****************************************************************************/
    309          1.1    jruoho 
    310          1.1    jruoho ACPI_STATUS
    311          1.1    jruoho AcpiHwWritePort (
    312          1.1    jruoho     ACPI_IO_ADDRESS         Address,
    313          1.1    jruoho     UINT32                  Value,
    314          1.1    jruoho     UINT32                  Width)
    315          1.1    jruoho {
    316          1.1    jruoho     ACPI_STATUS             Status;
    317          1.1    jruoho     UINT32                  i;
    318          1.1    jruoho 
    319          1.1    jruoho 
    320          1.1    jruoho     /* Truncate address to 16 bits if requested */
    321          1.1    jruoho 
    322          1.1    jruoho     if (AcpiGbl_TruncateIoAddresses)
    323          1.1    jruoho     {
    324          1.1    jruoho         Address &= ACPI_UINT16_MAX;
    325          1.1    jruoho     }
    326          1.1    jruoho 
    327          1.1    jruoho     /* Validate the entire request and perform the I/O */
    328          1.1    jruoho 
    329          1.1    jruoho     Status = AcpiHwValidateIoRequest (Address, Width);
    330          1.1    jruoho     if (ACPI_SUCCESS (Status))
    331          1.1    jruoho     {
    332          1.1    jruoho         Status = AcpiOsWritePort (Address, Value, Width);
    333          1.1    jruoho         return (Status);
    334          1.1    jruoho     }
    335          1.1    jruoho 
    336          1.1    jruoho     if (Status != AE_AML_ILLEGAL_ADDRESS)
    337          1.1    jruoho     {
    338          1.1    jruoho         return (Status);
    339          1.1    jruoho     }
    340          1.1    jruoho 
    341          1.1    jruoho     /*
    342          1.1    jruoho      * There has been a protection violation within the request. Fall
    343          1.1    jruoho      * back to byte granularity port I/O and ignore the failing bytes.
    344  1.1.1.4.2.4     skrll      * This provides compatibility with other ACPI implementations.
    345          1.1    jruoho      */
    346          1.1    jruoho     for (i = 0; i < Width; i += 8)
    347          1.1    jruoho     {
    348          1.1    jruoho         /* Validate and write one byte */
    349          1.1    jruoho 
    350          1.1    jruoho         if (AcpiHwValidateIoRequest (Address, 8) == AE_OK)
    351          1.1    jruoho         {
    352          1.1    jruoho             Status = AcpiOsWritePort (Address, (Value >> i) & 0xFF, 8);
    353          1.1    jruoho             if (ACPI_FAILURE (Status))
    354          1.1    jruoho             {
    355          1.1    jruoho                 return (Status);
    356          1.1    jruoho             }
    357          1.1    jruoho         }
    358          1.1    jruoho 
    359          1.1    jruoho         Address++;
    360          1.1    jruoho     }
    361          1.1    jruoho 
    362          1.1    jruoho     return (AE_OK);
    363          1.1    jruoho }
    364