1193267Sjkim/****************************************************************************** 2193267Sjkim * 3193267Sjkim * Module Name: hwvalid - I/O request validation 4193267Sjkim * 5193267Sjkim *****************************************************************************/ 6193267Sjkim 7217365Sjkim/* 8306536Sjkim * Copyright (C) 2000 - 2016, Intel Corp. 9193267Sjkim * All rights reserved. 10193267Sjkim * 11217365Sjkim * Redistribution and use in source and binary forms, with or without 12217365Sjkim * modification, are permitted provided that the following conditions 13217365Sjkim * are met: 14217365Sjkim * 1. Redistributions of source code must retain the above copyright 15217365Sjkim * notice, this list of conditions, and the following disclaimer, 16217365Sjkim * without modification. 17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18217365Sjkim * substantially similar to the "NO WARRANTY" disclaimer below 19217365Sjkim * ("Disclaimer") and any redistribution must be conditioned upon 20217365Sjkim * including a substantially similar Disclaimer requirement for further 21217365Sjkim * binary redistribution. 22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names 23217365Sjkim * of any contributors may be used to endorse or promote products derived 24217365Sjkim * from this software without specific prior written permission. 25193267Sjkim * 26217365Sjkim * Alternatively, this software may be distributed under the terms of the 27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free 28217365Sjkim * Software Foundation. 29193267Sjkim * 30217365Sjkim * NO WARRANTY 31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41217365Sjkim * POSSIBILITY OF SUCH DAMAGES. 42217365Sjkim */ 43193267Sjkim 44193341Sjkim#include <contrib/dev/acpica/include/acpi.h> 45193341Sjkim#include <contrib/dev/acpica/include/accommon.h> 46193267Sjkim 47193267Sjkim#define _COMPONENT ACPI_HARDWARE 48193267Sjkim ACPI_MODULE_NAME ("hwvalid") 49193267Sjkim 50193267Sjkim/* Local prototypes */ 51193267Sjkim 52193267Sjkimstatic ACPI_STATUS 53193267SjkimAcpiHwValidateIoRequest ( 54193267Sjkim ACPI_IO_ADDRESS Address, 55193267Sjkim UINT32 BitWidth); 56193267Sjkim 57193267Sjkim 58193267Sjkim/* 59193267Sjkim * Protected I/O ports. Some ports are always illegal, and some are 60193267Sjkim * conditionally illegal. This table must remain ordered by port address. 61193267Sjkim * 62193267Sjkim * The table is used to implement the Microsoft port access rules that 63193267Sjkim * first appeared in Windows XP. Some ports are always illegal, and some 64193267Sjkim * ports are only illegal if the BIOS calls _OSI with a WinXP string or 65193267Sjkim * later (meaning that the BIOS itelf is post-XP.) 66193267Sjkim * 67193267Sjkim * This provides ACPICA with the desired port protections and 68193267Sjkim * Microsoft compatibility. 69193267Sjkim * 70193267Sjkim * Description of port entries: 71193267Sjkim * DMA: DMA controller 72193267Sjkim * PIC0: Programmable Interrupt Controller (8259A) 73193267Sjkim * PIT1: System Timer 1 74193267Sjkim * PIT2: System Timer 2 failsafe 75193267Sjkim * RTC: Real-time clock 76193267Sjkim * CMOS: Extended CMOS 77193267Sjkim * DMA1: DMA 1 page registers 78193267Sjkim * DMA1L: DMA 1 Ch 0 low page 79193267Sjkim * DMA2: DMA 2 page registers 80193267Sjkim * DMA2L: DMA 2 low page refresh 81193267Sjkim * ARBC: Arbitration control 82193267Sjkim * SETUP: Reserved system board setup 83193267Sjkim * POS: POS channel select 84193267Sjkim * PIC1: Cascaded PIC 85193267Sjkim * IDMA: ISA DMA 86193267Sjkim * ELCR: PIC edge/level registers 87193267Sjkim * PCI: PCI configuration space 88193267Sjkim */ 89193267Sjkimstatic const ACPI_PORT_INFO AcpiProtectedPorts[] = 90193267Sjkim{ 91193267Sjkim {"DMA", 0x0000, 0x000F, ACPI_OSI_WIN_XP}, 92193267Sjkim {"PIC0", 0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL}, 93193267Sjkim {"PIT1", 0x0040, 0x0043, ACPI_OSI_WIN_XP}, 94193267Sjkim {"PIT2", 0x0048, 0x004B, ACPI_OSI_WIN_XP}, 95193267Sjkim {"RTC", 0x0070, 0x0071, ACPI_OSI_WIN_XP}, 96193267Sjkim {"CMOS", 0x0074, 0x0076, ACPI_OSI_WIN_XP}, 97193267Sjkim {"DMA1", 0x0081, 0x0083, ACPI_OSI_WIN_XP}, 98193267Sjkim {"DMA1L", 0x0087, 0x0087, ACPI_OSI_WIN_XP}, 99193267Sjkim {"DMA2", 0x0089, 0x008B, ACPI_OSI_WIN_XP}, 100193267Sjkim {"DMA2L", 0x008F, 0x008F, ACPI_OSI_WIN_XP}, 101193267Sjkim {"ARBC", 0x0090, 0x0091, ACPI_OSI_WIN_XP}, 102193267Sjkim {"SETUP", 0x0093, 0x0094, ACPI_OSI_WIN_XP}, 103193267Sjkim {"POS", 0x0096, 0x0097, ACPI_OSI_WIN_XP}, 104193267Sjkim {"PIC1", 0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL}, 105193267Sjkim {"IDMA", 0x00C0, 0x00DF, ACPI_OSI_WIN_XP}, 106193267Sjkim {"ELCR", 0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL}, 107193267Sjkim {"PCI", 0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP} 108193267Sjkim}; 109193267Sjkim 110193267Sjkim#define ACPI_PORT_INFO_ENTRIES ACPI_ARRAY_LENGTH (AcpiProtectedPorts) 111193267Sjkim 112193267Sjkim 113193267Sjkim/****************************************************************************** 114193267Sjkim * 115193267Sjkim * FUNCTION: AcpiHwValidateIoRequest 116193267Sjkim * 117193267Sjkim * PARAMETERS: Address Address of I/O port/register 118193267Sjkim * BitWidth Number of bits (8,16,32) 119193267Sjkim * 120193267Sjkim * RETURN: Status 121193267Sjkim * 122193267Sjkim * DESCRIPTION: Validates an I/O request (address/length). Certain ports are 123193267Sjkim * always illegal and some ports are only illegal depending on 124193267Sjkim * the requests the BIOS AML code makes to the predefined 125193267Sjkim * _OSI method. 126193267Sjkim * 127193267Sjkim ******************************************************************************/ 128193267Sjkim 129193267Sjkimstatic ACPI_STATUS 130193267SjkimAcpiHwValidateIoRequest ( 131193267Sjkim ACPI_IO_ADDRESS Address, 132193267Sjkim UINT32 BitWidth) 133193267Sjkim{ 134193267Sjkim UINT32 i; 135193267Sjkim UINT32 ByteWidth; 136193267Sjkim ACPI_IO_ADDRESS LastAddress; 137193267Sjkim const ACPI_PORT_INFO *PortInfo; 138193267Sjkim 139193267Sjkim 140193267Sjkim ACPI_FUNCTION_TRACE (HwValidateIoRequest); 141193267Sjkim 142193267Sjkim 143193267Sjkim /* Supported widths are 8/16/32 */ 144193267Sjkim 145193267Sjkim if ((BitWidth != 8) && 146193267Sjkim (BitWidth != 16) && 147193267Sjkim (BitWidth != 32)) 148193267Sjkim { 149228110Sjkim ACPI_ERROR ((AE_INFO, 150228110Sjkim "Bad BitWidth parameter: %8.8X", BitWidth)); 151193267Sjkim return (AE_BAD_PARAMETER); 152193267Sjkim } 153193267Sjkim 154193267Sjkim PortInfo = AcpiProtectedPorts; 155193267Sjkim ByteWidth = ACPI_DIV_8 (BitWidth); 156193267Sjkim LastAddress = Address + ByteWidth - 1; 157193267Sjkim 158281687Sjkim ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Address %8.8X%8.8X LastAddress %8.8X%8.8X Length %X", 159281687Sjkim ACPI_FORMAT_UINT64 (Address), ACPI_FORMAT_UINT64 (LastAddress), 160193267Sjkim ByteWidth)); 161193267Sjkim 162193267Sjkim /* Maximum 16-bit address in I/O space */ 163193267Sjkim 164193267Sjkim if (LastAddress > ACPI_UINT16_MAX) 165193267Sjkim { 166193267Sjkim ACPI_ERROR ((AE_INFO, 167281687Sjkim "Illegal I/O port address/length above 64K: %8.8X%8.8X/0x%X", 168281687Sjkim ACPI_FORMAT_UINT64 (Address), ByteWidth)); 169193267Sjkim return_ACPI_STATUS (AE_LIMIT); 170193267Sjkim } 171193267Sjkim 172193267Sjkim /* Exit if requested address is not within the protected port table */ 173193267Sjkim 174193267Sjkim if (Address > AcpiProtectedPorts[ACPI_PORT_INFO_ENTRIES - 1].End) 175193267Sjkim { 176193267Sjkim return_ACPI_STATUS (AE_OK); 177193267Sjkim } 178193267Sjkim 179193267Sjkim /* Check request against the list of protected I/O ports */ 180193267Sjkim 181193267Sjkim for (i = 0; i < ACPI_PORT_INFO_ENTRIES; i++, PortInfo++) 182193267Sjkim { 183193267Sjkim /* 184193267Sjkim * Check if the requested address range will write to a reserved 185193267Sjkim * port. Four cases to consider: 186193267Sjkim * 187193267Sjkim * 1) Address range is contained completely in the port address range 188193267Sjkim * 2) Address range overlaps port range at the port range start 189193267Sjkim * 3) Address range overlaps port range at the port range end 190193267Sjkim * 4) Address range completely encompasses the port range 191193267Sjkim */ 192193267Sjkim if ((Address <= PortInfo->End) && (LastAddress >= PortInfo->Start)) 193193267Sjkim { 194193267Sjkim /* Port illegality may depend on the _OSI calls made by the BIOS */ 195193267Sjkim 196193267Sjkim if (AcpiGbl_OsiData >= PortInfo->OsiDependency) 197193267Sjkim { 198193267Sjkim ACPI_DEBUG_PRINT ((ACPI_DB_IO, 199281687Sjkim "Denied AML access to port 0x%8.8X%8.8X/%X (%s 0x%.4X-0x%.4X)", 200281687Sjkim ACPI_FORMAT_UINT64 (Address), ByteWidth, PortInfo->Name, 201193267Sjkim PortInfo->Start, PortInfo->End)); 202193267Sjkim 203193267Sjkim return_ACPI_STATUS (AE_AML_ILLEGAL_ADDRESS); 204193267Sjkim } 205193267Sjkim } 206193267Sjkim 207193267Sjkim /* Finished if address range ends before the end of this port */ 208193267Sjkim 209193267Sjkim if (LastAddress <= PortInfo->End) 210193267Sjkim { 211193267Sjkim break; 212193267Sjkim } 213193267Sjkim } 214193267Sjkim 215193267Sjkim return_ACPI_STATUS (AE_OK); 216193267Sjkim} 217193267Sjkim 218193267Sjkim 219193267Sjkim/****************************************************************************** 220193267Sjkim * 221193267Sjkim * FUNCTION: AcpiHwReadPort 222193267Sjkim * 223193267Sjkim * PARAMETERS: Address Address of I/O port/register to read 224193267Sjkim * Value Where value is placed 225193267Sjkim * Width Number of bits 226193267Sjkim * 227193267Sjkim * RETURN: Status and value read from port 228193267Sjkim * 229193267Sjkim * DESCRIPTION: Read data from an I/O port or register. This is a front-end 230193267Sjkim * to AcpiOsReadPort that performs validation on both the port 231193267Sjkim * address and the length. 232193267Sjkim * 233193267Sjkim *****************************************************************************/ 234193267Sjkim 235193267SjkimACPI_STATUS 236193267SjkimAcpiHwReadPort ( 237193267Sjkim ACPI_IO_ADDRESS Address, 238193267Sjkim UINT32 *Value, 239193267Sjkim UINT32 Width) 240193267Sjkim{ 241193267Sjkim ACPI_STATUS Status; 242193267Sjkim UINT32 OneByte; 243193267Sjkim UINT32 i; 244193267Sjkim 245193267Sjkim 246209746Sjkim /* Truncate address to 16 bits if requested */ 247209746Sjkim 248209746Sjkim if (AcpiGbl_TruncateIoAddresses) 249209746Sjkim { 250209746Sjkim Address &= ACPI_UINT16_MAX; 251209746Sjkim } 252209746Sjkim 253193267Sjkim /* Validate the entire request and perform the I/O */ 254193267Sjkim 255193267Sjkim Status = AcpiHwValidateIoRequest (Address, Width); 256193267Sjkim if (ACPI_SUCCESS (Status)) 257193267Sjkim { 258193267Sjkim Status = AcpiOsReadPort (Address, Value, Width); 259193267Sjkim return (Status); 260193267Sjkim } 261193267Sjkim 262193267Sjkim if (Status != AE_AML_ILLEGAL_ADDRESS) 263193267Sjkim { 264193267Sjkim return (Status); 265193267Sjkim } 266193267Sjkim 267193267Sjkim /* 268193267Sjkim * There has been a protection violation within the request. Fall 269193267Sjkim * back to byte granularity port I/O and ignore the failing bytes. 270193267Sjkim * This provides Windows compatibility. 271193267Sjkim */ 272193267Sjkim for (i = 0, *Value = 0; i < Width; i += 8) 273193267Sjkim { 274193267Sjkim /* Validate and read one byte */ 275193267Sjkim 276193267Sjkim if (AcpiHwValidateIoRequest (Address, 8) == AE_OK) 277193267Sjkim { 278193267Sjkim Status = AcpiOsReadPort (Address, &OneByte, 8); 279193267Sjkim if (ACPI_FAILURE (Status)) 280193267Sjkim { 281193267Sjkim return (Status); 282193267Sjkim } 283193267Sjkim 284193267Sjkim *Value |= (OneByte << i); 285193267Sjkim } 286193267Sjkim 287193267Sjkim Address++; 288193267Sjkim } 289193267Sjkim 290193267Sjkim return (AE_OK); 291193267Sjkim} 292193267Sjkim 293193267Sjkim 294193267Sjkim/****************************************************************************** 295193267Sjkim * 296193267Sjkim * FUNCTION: AcpiHwWritePort 297193267Sjkim * 298193267Sjkim * PARAMETERS: Address Address of I/O port/register to write 299193267Sjkim * Value Value to write 300193267Sjkim * Width Number of bits 301193267Sjkim * 302193267Sjkim * RETURN: Status 303193267Sjkim * 304193267Sjkim * DESCRIPTION: Write data to an I/O port or register. This is a front-end 305193267Sjkim * to AcpiOsWritePort that performs validation on both the port 306193267Sjkim * address and the length. 307193267Sjkim * 308193267Sjkim *****************************************************************************/ 309193267Sjkim 310193267SjkimACPI_STATUS 311193267SjkimAcpiHwWritePort ( 312193267Sjkim ACPI_IO_ADDRESS Address, 313193267Sjkim UINT32 Value, 314193267Sjkim UINT32 Width) 315193267Sjkim{ 316193267Sjkim ACPI_STATUS Status; 317193267Sjkim UINT32 i; 318193267Sjkim 319193267Sjkim 320209746Sjkim /* Truncate address to 16 bits if requested */ 321209746Sjkim 322209746Sjkim if (AcpiGbl_TruncateIoAddresses) 323209746Sjkim { 324209746Sjkim Address &= ACPI_UINT16_MAX; 325209746Sjkim } 326209746Sjkim 327193267Sjkim /* Validate the entire request and perform the I/O */ 328193267Sjkim 329193267Sjkim Status = AcpiHwValidateIoRequest (Address, Width); 330193267Sjkim if (ACPI_SUCCESS (Status)) 331193267Sjkim { 332193267Sjkim Status = AcpiOsWritePort (Address, Value, Width); 333193267Sjkim return (Status); 334193267Sjkim } 335193267Sjkim 336193267Sjkim if (Status != AE_AML_ILLEGAL_ADDRESS) 337193267Sjkim { 338193267Sjkim return (Status); 339193267Sjkim } 340193267Sjkim 341193267Sjkim /* 342193267Sjkim * There has been a protection violation within the request. Fall 343193267Sjkim * back to byte granularity port I/O and ignore the failing bytes. 344193267Sjkim * This provides Windows compatibility. 345193267Sjkim */ 346193267Sjkim for (i = 0; i < Width; i += 8) 347193267Sjkim { 348193267Sjkim /* Validate and write one byte */ 349193267Sjkim 350193267Sjkim if (AcpiHwValidateIoRequest (Address, 8) == AE_OK) 351193267Sjkim { 352193267Sjkim Status = AcpiOsWritePort (Address, (Value >> i) & 0xFF, 8); 353193267Sjkim if (ACPI_FAILURE (Status)) 354193267Sjkim { 355193267Sjkim return (Status); 356193267Sjkim } 357193267Sjkim } 358193267Sjkim 359193267Sjkim Address++; 360193267Sjkim } 361193267Sjkim 362193267Sjkim return (AE_OK); 363193267Sjkim} 364