1193267Sjkim/******************************************************************************
2193267Sjkim *
3193267Sjkim * Module Name: hwvalid - I/O request validation
4193267Sjkim *
5193267Sjkim *****************************************************************************/
6193267Sjkim
7217365Sjkim/*
8245582Sjkim * Copyright (C) 2000 - 2013, 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
44193267Sjkim#define __HWVALID_C__
45193267Sjkim
46193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
47193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
48193267Sjkim
49193267Sjkim#define _COMPONENT          ACPI_HARDWARE
50193267Sjkim        ACPI_MODULE_NAME    ("hwvalid")
51193267Sjkim
52193267Sjkim/* Local prototypes */
53193267Sjkim
54193267Sjkimstatic ACPI_STATUS
55193267SjkimAcpiHwValidateIoRequest (
56193267Sjkim    ACPI_IO_ADDRESS         Address,
57193267Sjkim    UINT32                  BitWidth);
58193267Sjkim
59193267Sjkim
60193267Sjkim/*
61193267Sjkim * Protected I/O ports. Some ports are always illegal, and some are
62193267Sjkim * conditionally illegal. This table must remain ordered by port address.
63193267Sjkim *
64193267Sjkim * The table is used to implement the Microsoft port access rules that
65193267Sjkim * first appeared in Windows XP. Some ports are always illegal, and some
66193267Sjkim * ports are only illegal if the BIOS calls _OSI with a WinXP string or
67193267Sjkim * later (meaning that the BIOS itelf is post-XP.)
68193267Sjkim *
69193267Sjkim * This provides ACPICA with the desired port protections and
70193267Sjkim * Microsoft compatibility.
71193267Sjkim *
72193267Sjkim * Description of port entries:
73193267Sjkim *  DMA:   DMA controller
74193267Sjkim *  PIC0:  Programmable Interrupt Controller (8259A)
75193267Sjkim *  PIT1:  System Timer 1
76193267Sjkim *  PIT2:  System Timer 2 failsafe
77193267Sjkim *  RTC:   Real-time clock
78193267Sjkim *  CMOS:  Extended CMOS
79193267Sjkim *  DMA1:  DMA 1 page registers
80193267Sjkim *  DMA1L: DMA 1 Ch 0 low page
81193267Sjkim *  DMA2:  DMA 2 page registers
82193267Sjkim *  DMA2L: DMA 2 low page refresh
83193267Sjkim *  ARBC:  Arbitration control
84193267Sjkim *  SETUP: Reserved system board setup
85193267Sjkim *  POS:   POS channel select
86193267Sjkim *  PIC1:  Cascaded PIC
87193267Sjkim *  IDMA:  ISA DMA
88193267Sjkim *  ELCR:  PIC edge/level registers
89193267Sjkim *  PCI:   PCI configuration space
90193267Sjkim */
91193267Sjkimstatic const ACPI_PORT_INFO     AcpiProtectedPorts[] =
92193267Sjkim{
93193267Sjkim    {"DMA",     0x0000, 0x000F, ACPI_OSI_WIN_XP},
94193267Sjkim    {"PIC0",    0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL},
95193267Sjkim    {"PIT1",    0x0040, 0x0043, ACPI_OSI_WIN_XP},
96193267Sjkim    {"PIT2",    0x0048, 0x004B, ACPI_OSI_WIN_XP},
97193267Sjkim    {"RTC",     0x0070, 0x0071, ACPI_OSI_WIN_XP},
98193267Sjkim    {"CMOS",    0x0074, 0x0076, ACPI_OSI_WIN_XP},
99193267Sjkim    {"DMA1",    0x0081, 0x0083, ACPI_OSI_WIN_XP},
100193267Sjkim    {"DMA1L",   0x0087, 0x0087, ACPI_OSI_WIN_XP},
101193267Sjkim    {"DMA2",    0x0089, 0x008B, ACPI_OSI_WIN_XP},
102193267Sjkim    {"DMA2L",   0x008F, 0x008F, ACPI_OSI_WIN_XP},
103193267Sjkim    {"ARBC",    0x0090, 0x0091, ACPI_OSI_WIN_XP},
104193267Sjkim    {"SETUP",   0x0093, 0x0094, ACPI_OSI_WIN_XP},
105193267Sjkim    {"POS",     0x0096, 0x0097, ACPI_OSI_WIN_XP},
106193267Sjkim    {"PIC1",    0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL},
107193267Sjkim    {"IDMA",    0x00C0, 0x00DF, ACPI_OSI_WIN_XP},
108193267Sjkim    {"ELCR",    0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL},
109193267Sjkim    {"PCI",     0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP}
110193267Sjkim};
111193267Sjkim
112193267Sjkim#define ACPI_PORT_INFO_ENTRIES  ACPI_ARRAY_LENGTH (AcpiProtectedPorts)
113193267Sjkim
114193267Sjkim
115193267Sjkim/******************************************************************************
116193267Sjkim *
117193267Sjkim * FUNCTION:    AcpiHwValidateIoRequest
118193267Sjkim *
119193267Sjkim * PARAMETERS:  Address             Address of I/O port/register
120193267Sjkim *              BitWidth            Number of bits (8,16,32)
121193267Sjkim *
122193267Sjkim * RETURN:      Status
123193267Sjkim *
124193267Sjkim * DESCRIPTION: Validates an I/O request (address/length). Certain ports are
125193267Sjkim *              always illegal and some ports are only illegal depending on
126193267Sjkim *              the requests the BIOS AML code makes to the predefined
127193267Sjkim *              _OSI method.
128193267Sjkim *
129193267Sjkim ******************************************************************************/
130193267Sjkim
131193267Sjkimstatic ACPI_STATUS
132193267SjkimAcpiHwValidateIoRequest (
133193267Sjkim    ACPI_IO_ADDRESS         Address,
134193267Sjkim    UINT32                  BitWidth)
135193267Sjkim{
136193267Sjkim    UINT32                  i;
137193267Sjkim    UINT32                  ByteWidth;
138193267Sjkim    ACPI_IO_ADDRESS         LastAddress;
139193267Sjkim    const ACPI_PORT_INFO    *PortInfo;
140193267Sjkim
141193267Sjkim
142193267Sjkim    ACPI_FUNCTION_TRACE (HwValidateIoRequest);
143193267Sjkim
144193267Sjkim
145193267Sjkim    /* Supported widths are 8/16/32 */
146193267Sjkim
147193267Sjkim    if ((BitWidth != 8) &&
148193267Sjkim        (BitWidth != 16) &&
149193267Sjkim        (BitWidth != 32))
150193267Sjkim    {
151228110Sjkim        ACPI_ERROR ((AE_INFO,
152228110Sjkim            "Bad BitWidth parameter: %8.8X", BitWidth));
153193267Sjkim        return (AE_BAD_PARAMETER);
154193267Sjkim    }
155193267Sjkim
156193267Sjkim    PortInfo = AcpiProtectedPorts;
157193267Sjkim    ByteWidth = ACPI_DIV_8 (BitWidth);
158193267Sjkim    LastAddress = Address + ByteWidth - 1;
159193267Sjkim
160193267Sjkim    ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Address %p LastAddress %p Length %X",
161193267Sjkim        ACPI_CAST_PTR (void, Address), ACPI_CAST_PTR (void, LastAddress),
162193267Sjkim        ByteWidth));
163193267Sjkim
164193267Sjkim    /* Maximum 16-bit address in I/O space */
165193267Sjkim
166193267Sjkim    if (LastAddress > ACPI_UINT16_MAX)
167193267Sjkim    {
168193267Sjkim        ACPI_ERROR ((AE_INFO,
169204773Sjkim            "Illegal I/O port address/length above 64K: %p/0x%X",
170193267Sjkim            ACPI_CAST_PTR (void, Address), ByteWidth));
171193267Sjkim        return_ACPI_STATUS (AE_LIMIT);
172193267Sjkim    }
173193267Sjkim
174193267Sjkim    /* Exit if requested address is not within the protected port table */
175193267Sjkim
176193267Sjkim    if (Address > AcpiProtectedPorts[ACPI_PORT_INFO_ENTRIES - 1].End)
177193267Sjkim    {
178193267Sjkim        return_ACPI_STATUS (AE_OK);
179193267Sjkim    }
180193267Sjkim
181193267Sjkim    /* Check request against the list of protected I/O ports */
182193267Sjkim
183193267Sjkim    for (i = 0; i < ACPI_PORT_INFO_ENTRIES; i++, PortInfo++)
184193267Sjkim    {
185193267Sjkim        /*
186193267Sjkim         * Check if the requested address range will write to a reserved
187193267Sjkim         * port. Four cases to consider:
188193267Sjkim         *
189193267Sjkim         * 1) Address range is contained completely in the port address range
190193267Sjkim         * 2) Address range overlaps port range at the port range start
191193267Sjkim         * 3) Address range overlaps port range at the port range end
192193267Sjkim         * 4) Address range completely encompasses the port range
193193267Sjkim         */
194193267Sjkim        if ((Address <= PortInfo->End) && (LastAddress >= PortInfo->Start))
195193267Sjkim        {
196193267Sjkim            /* Port illegality may depend on the _OSI calls made by the BIOS */
197193267Sjkim
198193267Sjkim            if (AcpiGbl_OsiData >= PortInfo->OsiDependency)
199193267Sjkim            {
200193267Sjkim                ACPI_DEBUG_PRINT ((ACPI_DB_IO,
201193267Sjkim                    "Denied AML access to port 0x%p/%X (%s 0x%.4X-0x%.4X)",
202193267Sjkim                    ACPI_CAST_PTR (void, Address), ByteWidth, PortInfo->Name,
203193267Sjkim                    PortInfo->Start, PortInfo->End));
204193267Sjkim
205193267Sjkim                return_ACPI_STATUS (AE_AML_ILLEGAL_ADDRESS);
206193267Sjkim            }
207193267Sjkim        }
208193267Sjkim
209193267Sjkim        /* Finished if address range ends before the end of this port */
210193267Sjkim
211193267Sjkim        if (LastAddress <= PortInfo->End)
212193267Sjkim        {
213193267Sjkim            break;
214193267Sjkim        }
215193267Sjkim    }
216193267Sjkim
217193267Sjkim    return_ACPI_STATUS (AE_OK);
218193267Sjkim}
219193267Sjkim
220193267Sjkim
221193267Sjkim/******************************************************************************
222193267Sjkim *
223193267Sjkim * FUNCTION:    AcpiHwReadPort
224193267Sjkim *
225193267Sjkim * PARAMETERS:  Address             Address of I/O port/register to read
226193267Sjkim *              Value               Where value is placed
227193267Sjkim *              Width               Number of bits
228193267Sjkim *
229193267Sjkim * RETURN:      Status and value read from port
230193267Sjkim *
231193267Sjkim * DESCRIPTION: Read data from an I/O port or register. This is a front-end
232193267Sjkim *              to AcpiOsReadPort that performs validation on both the port
233193267Sjkim *              address and the length.
234193267Sjkim *
235193267Sjkim *****************************************************************************/
236193267Sjkim
237193267SjkimACPI_STATUS
238193267SjkimAcpiHwReadPort (
239193267Sjkim    ACPI_IO_ADDRESS         Address,
240193267Sjkim    UINT32                  *Value,
241193267Sjkim    UINT32                  Width)
242193267Sjkim{
243193267Sjkim    ACPI_STATUS             Status;
244193267Sjkim    UINT32                  OneByte;
245193267Sjkim    UINT32                  i;
246193267Sjkim
247193267Sjkim
248209746Sjkim    /* Truncate address to 16 bits if requested */
249209746Sjkim
250209746Sjkim    if (AcpiGbl_TruncateIoAddresses)
251209746Sjkim    {
252209746Sjkim        Address &= ACPI_UINT16_MAX;
253209746Sjkim    }
254209746Sjkim
255193267Sjkim    /* Validate the entire request and perform the I/O */
256193267Sjkim
257193267Sjkim    Status = AcpiHwValidateIoRequest (Address, Width);
258193267Sjkim    if (ACPI_SUCCESS (Status))
259193267Sjkim    {
260193267Sjkim        Status = AcpiOsReadPort (Address, Value, Width);
261193267Sjkim        return (Status);
262193267Sjkim    }
263193267Sjkim
264193267Sjkim    if (Status != AE_AML_ILLEGAL_ADDRESS)
265193267Sjkim    {
266193267Sjkim        return (Status);
267193267Sjkim    }
268193267Sjkim
269193267Sjkim    /*
270193267Sjkim     * There has been a protection violation within the request. Fall
271193267Sjkim     * back to byte granularity port I/O and ignore the failing bytes.
272193267Sjkim     * This provides Windows compatibility.
273193267Sjkim     */
274193267Sjkim    for (i = 0, *Value = 0; i < Width; i += 8)
275193267Sjkim    {
276193267Sjkim        /* Validate and read one byte */
277193267Sjkim
278193267Sjkim        if (AcpiHwValidateIoRequest (Address, 8) == AE_OK)
279193267Sjkim        {
280193267Sjkim            Status = AcpiOsReadPort (Address, &OneByte, 8);
281193267Sjkim            if (ACPI_FAILURE (Status))
282193267Sjkim            {
283193267Sjkim                return (Status);
284193267Sjkim            }
285193267Sjkim
286193267Sjkim            *Value |= (OneByte << i);
287193267Sjkim        }
288193267Sjkim
289193267Sjkim        Address++;
290193267Sjkim    }
291193267Sjkim
292193267Sjkim    return (AE_OK);
293193267Sjkim}
294193267Sjkim
295193267Sjkim
296193267Sjkim/******************************************************************************
297193267Sjkim *
298193267Sjkim * FUNCTION:    AcpiHwWritePort
299193267Sjkim *
300193267Sjkim * PARAMETERS:  Address             Address of I/O port/register to write
301193267Sjkim *              Value               Value to write
302193267Sjkim *              Width               Number of bits
303193267Sjkim *
304193267Sjkim * RETURN:      Status
305193267Sjkim *
306193267Sjkim * DESCRIPTION: Write data to an I/O port or register. This is a front-end
307193267Sjkim *              to AcpiOsWritePort that performs validation on both the port
308193267Sjkim *              address and the length.
309193267Sjkim *
310193267Sjkim *****************************************************************************/
311193267Sjkim
312193267SjkimACPI_STATUS
313193267SjkimAcpiHwWritePort (
314193267Sjkim    ACPI_IO_ADDRESS         Address,
315193267Sjkim    UINT32                  Value,
316193267Sjkim    UINT32                  Width)
317193267Sjkim{
318193267Sjkim    ACPI_STATUS             Status;
319193267Sjkim    UINT32                  i;
320193267Sjkim
321193267Sjkim
322209746Sjkim    /* Truncate address to 16 bits if requested */
323209746Sjkim
324209746Sjkim    if (AcpiGbl_TruncateIoAddresses)
325209746Sjkim    {
326209746Sjkim        Address &= ACPI_UINT16_MAX;
327209746Sjkim    }
328209746Sjkim
329193267Sjkim    /* Validate the entire request and perform the I/O */
330193267Sjkim
331193267Sjkim    Status = AcpiHwValidateIoRequest (Address, Width);
332193267Sjkim    if (ACPI_SUCCESS (Status))
333193267Sjkim    {
334193267Sjkim        Status = AcpiOsWritePort (Address, Value, Width);
335193267Sjkim        return (Status);
336193267Sjkim    }
337193267Sjkim
338193267Sjkim    if (Status != AE_AML_ILLEGAL_ADDRESS)
339193267Sjkim    {
340193267Sjkim        return (Status);
341193267Sjkim    }
342193267Sjkim
343193267Sjkim    /*
344193267Sjkim     * There has been a protection violation within the request. Fall
345193267Sjkim     * back to byte granularity port I/O and ignore the failing bytes.
346193267Sjkim     * This provides Windows compatibility.
347193267Sjkim     */
348193267Sjkim    for (i = 0; i < Width; i += 8)
349193267Sjkim    {
350193267Sjkim        /* Validate and write one byte */
351193267Sjkim
352193267Sjkim        if (AcpiHwValidateIoRequest (Address, 8) == AE_OK)
353193267Sjkim        {
354193267Sjkim            Status = AcpiOsWritePort (Address, (Value >> i) & 0xFF, 8);
355193267Sjkim            if (ACPI_FAILURE (Status))
356193267Sjkim            {
357193267Sjkim                return (Status);
358193267Sjkim            }
359193267Sjkim        }
360193267Sjkim
361193267Sjkim        Address++;
362193267Sjkim    }
363193267Sjkim
364193267Sjkim    return (AE_OK);
365193267Sjkim}
366