hwvalid.c revision 316303
1193267Sjkim/******************************************************************************
2193267Sjkim *
3193267Sjkim * Module Name: hwvalid - I/O request validation
4193267Sjkim *
5193267Sjkim *****************************************************************************/
6193267Sjkim
7316303Sjkim/******************************************************************************
8316303Sjkim *
9316303Sjkim * 1. Copyright Notice
10316303Sjkim *
11316303Sjkim * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12193267Sjkim * All rights reserved.
13193267Sjkim *
14316303Sjkim * 2. License
15316303Sjkim *
16316303Sjkim * 2.1. This is your license from Intel Corp. under its intellectual property
17316303Sjkim * rights. You may have additional license terms from the party that provided
18316303Sjkim * you this software, covering your right to use that party's intellectual
19316303Sjkim * property rights.
20316303Sjkim *
21316303Sjkim * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22316303Sjkim * copy of the source code appearing in this file ("Covered Code") an
23316303Sjkim * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24316303Sjkim * base code distributed originally by Intel ("Original Intel Code") to copy,
25316303Sjkim * make derivatives, distribute, use and display any portion of the Covered
26316303Sjkim * Code in any form, with the right to sublicense such rights; and
27316303Sjkim *
28316303Sjkim * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29316303Sjkim * license (with the right to sublicense), under only those claims of Intel
30316303Sjkim * patents that are infringed by the Original Intel Code, to make, use, sell,
31316303Sjkim * offer to sell, and import the Covered Code and derivative works thereof
32316303Sjkim * solely to the minimum extent necessary to exercise the above copyright
33316303Sjkim * license, and in no event shall the patent license extend to any additions
34316303Sjkim * to or modifications of the Original Intel Code. No other license or right
35316303Sjkim * is granted directly or by implication, estoppel or otherwise;
36316303Sjkim *
37316303Sjkim * The above copyright and patent license is granted only if the following
38316303Sjkim * conditions are met:
39316303Sjkim *
40316303Sjkim * 3. Conditions
41316303Sjkim *
42316303Sjkim * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43316303Sjkim * Redistribution of source code of any substantial portion of the Covered
44316303Sjkim * Code or modification with rights to further distribute source must include
45316303Sjkim * the above Copyright Notice, the above License, this list of Conditions,
46316303Sjkim * and the following Disclaimer and Export Compliance provision. In addition,
47316303Sjkim * Licensee must cause all Covered Code to which Licensee contributes to
48316303Sjkim * contain a file documenting the changes Licensee made to create that Covered
49316303Sjkim * Code and the date of any change. Licensee must include in that file the
50316303Sjkim * documentation of any changes made by any predecessor Licensee. Licensee
51316303Sjkim * must include a prominent statement that the modification is derived,
52316303Sjkim * directly or indirectly, from Original Intel Code.
53316303Sjkim *
54316303Sjkim * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55316303Sjkim * Redistribution of source code of any substantial portion of the Covered
56316303Sjkim * Code or modification without rights to further distribute source must
57316303Sjkim * include the following Disclaimer and Export Compliance provision in the
58316303Sjkim * documentation and/or other materials provided with distribution. In
59316303Sjkim * addition, Licensee may not authorize further sublicense of source of any
60316303Sjkim * portion of the Covered Code, and must include terms to the effect that the
61316303Sjkim * license from Licensee to its licensee is limited to the intellectual
62316303Sjkim * property embodied in the software Licensee provides to its licensee, and
63316303Sjkim * not to intellectual property embodied in modifications its licensee may
64316303Sjkim * make.
65316303Sjkim *
66316303Sjkim * 3.3. Redistribution of Executable. Redistribution in executable form of any
67316303Sjkim * substantial portion of the Covered Code or modification must reproduce the
68316303Sjkim * above Copyright Notice, and the following Disclaimer and Export Compliance
69316303Sjkim * provision in the documentation and/or other materials provided with the
70316303Sjkim * distribution.
71316303Sjkim *
72316303Sjkim * 3.4. Intel retains all right, title, and interest in and to the Original
73316303Sjkim * Intel Code.
74316303Sjkim *
75316303Sjkim * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76316303Sjkim * Intel shall be used in advertising or otherwise to promote the sale, use or
77316303Sjkim * other dealings in products derived from or relating to the Covered Code
78316303Sjkim * without prior written authorization from Intel.
79316303Sjkim *
80316303Sjkim * 4. Disclaimer and Export Compliance
81316303Sjkim *
82316303Sjkim * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83316303Sjkim * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84316303Sjkim * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85316303Sjkim * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86316303Sjkim * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88316303Sjkim * PARTICULAR PURPOSE.
89316303Sjkim *
90316303Sjkim * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91316303Sjkim * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92316303Sjkim * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93316303Sjkim * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94316303Sjkim * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95316303Sjkim * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96316303Sjkim * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97316303Sjkim * LIMITED REMEDY.
98316303Sjkim *
99316303Sjkim * 4.3. Licensee shall not export, either directly or indirectly, any of this
100316303Sjkim * software or system incorporating such software without first obtaining any
101316303Sjkim * required license or other approval from the U. S. Department of Commerce or
102316303Sjkim * any other agency or department of the United States Government. In the
103316303Sjkim * event Licensee exports any such software from the United States or
104316303Sjkim * re-exports any such software from a foreign destination, Licensee shall
105316303Sjkim * ensure that the distribution and export/re-export of the software is in
106316303Sjkim * compliance with all laws, regulations, orders, or other restrictions of the
107316303Sjkim * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108316303Sjkim * any of its subsidiaries will export/re-export any technical data, process,
109316303Sjkim * software, or service, directly or indirectly, to any country for which the
110316303Sjkim * United States government or any agency thereof requires an export license,
111316303Sjkim * other governmental approval, or letter of assurance, without first obtaining
112316303Sjkim * such license, approval or letter.
113316303Sjkim *
114316303Sjkim *****************************************************************************
115316303Sjkim *
116316303Sjkim * Alternatively, you may choose to be licensed under the terms of the
117316303Sjkim * following license:
118316303Sjkim *
119217365Sjkim * Redistribution and use in source and binary forms, with or without
120217365Sjkim * modification, are permitted provided that the following conditions
121217365Sjkim * are met:
122217365Sjkim * 1. Redistributions of source code must retain the above copyright
123217365Sjkim *    notice, this list of conditions, and the following disclaimer,
124217365Sjkim *    without modification.
125217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
127217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
128217365Sjkim *    including a substantially similar Disclaimer requirement for further
129217365Sjkim *    binary redistribution.
130217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
131217365Sjkim *    of any contributors may be used to endorse or promote products derived
132217365Sjkim *    from this software without specific prior written permission.
133193267Sjkim *
134316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135316303Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136316303Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137316303Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138316303Sjkim * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139316303Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140316303Sjkim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141316303Sjkim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142316303Sjkim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143316303Sjkim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144316303Sjkim * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145316303Sjkim *
146316303Sjkim * Alternatively, you may choose to be licensed under the terms of the
147217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
148217365Sjkim * Software Foundation.
149193267Sjkim *
150316303Sjkim *****************************************************************************/
151193267Sjkim
152193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
153193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
154193267Sjkim
155193267Sjkim#define _COMPONENT          ACPI_HARDWARE
156193267Sjkim        ACPI_MODULE_NAME    ("hwvalid")
157193267Sjkim
158193267Sjkim/* Local prototypes */
159193267Sjkim
160193267Sjkimstatic ACPI_STATUS
161193267SjkimAcpiHwValidateIoRequest (
162193267Sjkim    ACPI_IO_ADDRESS         Address,
163193267Sjkim    UINT32                  BitWidth);
164193267Sjkim
165193267Sjkim
166193267Sjkim/*
167193267Sjkim * Protected I/O ports. Some ports are always illegal, and some are
168193267Sjkim * conditionally illegal. This table must remain ordered by port address.
169193267Sjkim *
170193267Sjkim * The table is used to implement the Microsoft port access rules that
171193267Sjkim * first appeared in Windows XP. Some ports are always illegal, and some
172193267Sjkim * ports are only illegal if the BIOS calls _OSI with a WinXP string or
173193267Sjkim * later (meaning that the BIOS itelf is post-XP.)
174193267Sjkim *
175193267Sjkim * This provides ACPICA with the desired port protections and
176193267Sjkim * Microsoft compatibility.
177193267Sjkim *
178193267Sjkim * Description of port entries:
179193267Sjkim *  DMA:   DMA controller
180193267Sjkim *  PIC0:  Programmable Interrupt Controller (8259A)
181193267Sjkim *  PIT1:  System Timer 1
182193267Sjkim *  PIT2:  System Timer 2 failsafe
183193267Sjkim *  RTC:   Real-time clock
184193267Sjkim *  CMOS:  Extended CMOS
185193267Sjkim *  DMA1:  DMA 1 page registers
186193267Sjkim *  DMA1L: DMA 1 Ch 0 low page
187193267Sjkim *  DMA2:  DMA 2 page registers
188193267Sjkim *  DMA2L: DMA 2 low page refresh
189193267Sjkim *  ARBC:  Arbitration control
190193267Sjkim *  SETUP: Reserved system board setup
191193267Sjkim *  POS:   POS channel select
192193267Sjkim *  PIC1:  Cascaded PIC
193193267Sjkim *  IDMA:  ISA DMA
194193267Sjkim *  ELCR:  PIC edge/level registers
195193267Sjkim *  PCI:   PCI configuration space
196193267Sjkim */
197193267Sjkimstatic const ACPI_PORT_INFO     AcpiProtectedPorts[] =
198193267Sjkim{
199193267Sjkim    {"DMA",     0x0000, 0x000F, ACPI_OSI_WIN_XP},
200193267Sjkim    {"PIC0",    0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL},
201193267Sjkim    {"PIT1",    0x0040, 0x0043, ACPI_OSI_WIN_XP},
202193267Sjkim    {"PIT2",    0x0048, 0x004B, ACPI_OSI_WIN_XP},
203193267Sjkim    {"RTC",     0x0070, 0x0071, ACPI_OSI_WIN_XP},
204193267Sjkim    {"CMOS",    0x0074, 0x0076, ACPI_OSI_WIN_XP},
205193267Sjkim    {"DMA1",    0x0081, 0x0083, ACPI_OSI_WIN_XP},
206193267Sjkim    {"DMA1L",   0x0087, 0x0087, ACPI_OSI_WIN_XP},
207193267Sjkim    {"DMA2",    0x0089, 0x008B, ACPI_OSI_WIN_XP},
208193267Sjkim    {"DMA2L",   0x008F, 0x008F, ACPI_OSI_WIN_XP},
209193267Sjkim    {"ARBC",    0x0090, 0x0091, ACPI_OSI_WIN_XP},
210193267Sjkim    {"SETUP",   0x0093, 0x0094, ACPI_OSI_WIN_XP},
211193267Sjkim    {"POS",     0x0096, 0x0097, ACPI_OSI_WIN_XP},
212193267Sjkim    {"PIC1",    0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL},
213193267Sjkim    {"IDMA",    0x00C0, 0x00DF, ACPI_OSI_WIN_XP},
214193267Sjkim    {"ELCR",    0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL},
215193267Sjkim    {"PCI",     0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP}
216193267Sjkim};
217193267Sjkim
218316303Sjkim#define ACPI_PORT_INFO_ENTRIES      ACPI_ARRAY_LENGTH (AcpiProtectedPorts)
219193267Sjkim
220193267Sjkim
221193267Sjkim/******************************************************************************
222193267Sjkim *
223193267Sjkim * FUNCTION:    AcpiHwValidateIoRequest
224193267Sjkim *
225193267Sjkim * PARAMETERS:  Address             Address of I/O port/register
226193267Sjkim *              BitWidth            Number of bits (8,16,32)
227193267Sjkim *
228193267Sjkim * RETURN:      Status
229193267Sjkim *
230193267Sjkim * DESCRIPTION: Validates an I/O request (address/length). Certain ports are
231193267Sjkim *              always illegal and some ports are only illegal depending on
232193267Sjkim *              the requests the BIOS AML code makes to the predefined
233193267Sjkim *              _OSI method.
234193267Sjkim *
235193267Sjkim ******************************************************************************/
236193267Sjkim
237193267Sjkimstatic ACPI_STATUS
238193267SjkimAcpiHwValidateIoRequest (
239193267Sjkim    ACPI_IO_ADDRESS         Address,
240193267Sjkim    UINT32                  BitWidth)
241193267Sjkim{
242193267Sjkim    UINT32                  i;
243193267Sjkim    UINT32                  ByteWidth;
244193267Sjkim    ACPI_IO_ADDRESS         LastAddress;
245193267Sjkim    const ACPI_PORT_INFO    *PortInfo;
246193267Sjkim
247193267Sjkim
248316303Sjkim    ACPI_FUNCTION_NAME (HwValidateIoRequest);
249193267Sjkim
250193267Sjkim
251193267Sjkim    /* Supported widths are 8/16/32 */
252193267Sjkim
253193267Sjkim    if ((BitWidth != 8) &&
254193267Sjkim        (BitWidth != 16) &&
255193267Sjkim        (BitWidth != 32))
256193267Sjkim    {
257228110Sjkim        ACPI_ERROR ((AE_INFO,
258228110Sjkim            "Bad BitWidth parameter: %8.8X", BitWidth));
259193267Sjkim        return (AE_BAD_PARAMETER);
260193267Sjkim    }
261193267Sjkim
262193267Sjkim    PortInfo = AcpiProtectedPorts;
263193267Sjkim    ByteWidth = ACPI_DIV_8 (BitWidth);
264193267Sjkim    LastAddress = Address + ByteWidth - 1;
265193267Sjkim
266281396Sjkim    ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Address %8.8X%8.8X LastAddress %8.8X%8.8X Length %X",
267281396Sjkim        ACPI_FORMAT_UINT64 (Address), ACPI_FORMAT_UINT64 (LastAddress),
268193267Sjkim        ByteWidth));
269193267Sjkim
270193267Sjkim    /* Maximum 16-bit address in I/O space */
271193267Sjkim
272193267Sjkim    if (LastAddress > ACPI_UINT16_MAX)
273193267Sjkim    {
274193267Sjkim        ACPI_ERROR ((AE_INFO,
275281396Sjkim            "Illegal I/O port address/length above 64K: %8.8X%8.8X/0x%X",
276281396Sjkim            ACPI_FORMAT_UINT64 (Address), ByteWidth));
277316303Sjkim        return (AE_LIMIT);
278193267Sjkim    }
279193267Sjkim
280193267Sjkim    /* Exit if requested address is not within the protected port table */
281193267Sjkim
282193267Sjkim    if (Address > AcpiProtectedPorts[ACPI_PORT_INFO_ENTRIES - 1].End)
283193267Sjkim    {
284316303Sjkim        return (AE_OK);
285193267Sjkim    }
286193267Sjkim
287193267Sjkim    /* Check request against the list of protected I/O ports */
288193267Sjkim
289193267Sjkim    for (i = 0; i < ACPI_PORT_INFO_ENTRIES; i++, PortInfo++)
290193267Sjkim    {
291193267Sjkim        /*
292193267Sjkim         * Check if the requested address range will write to a reserved
293316303Sjkim         * port. There are four cases to consider:
294193267Sjkim         *
295193267Sjkim         * 1) Address range is contained completely in the port address range
296193267Sjkim         * 2) Address range overlaps port range at the port range start
297193267Sjkim         * 3) Address range overlaps port range at the port range end
298193267Sjkim         * 4) Address range completely encompasses the port range
299193267Sjkim         */
300193267Sjkim        if ((Address <= PortInfo->End) && (LastAddress >= PortInfo->Start))
301193267Sjkim        {
302193267Sjkim            /* Port illegality may depend on the _OSI calls made by the BIOS */
303193267Sjkim
304193267Sjkim            if (AcpiGbl_OsiData >= PortInfo->OsiDependency)
305193267Sjkim            {
306193267Sjkim                ACPI_DEBUG_PRINT ((ACPI_DB_IO,
307281396Sjkim                    "Denied AML access to port 0x%8.8X%8.8X/%X (%s 0x%.4X-0x%.4X)",
308281396Sjkim                    ACPI_FORMAT_UINT64 (Address), ByteWidth, PortInfo->Name,
309193267Sjkim                    PortInfo->Start, PortInfo->End));
310193267Sjkim
311193267Sjkim                return_ACPI_STATUS (AE_AML_ILLEGAL_ADDRESS);
312193267Sjkim            }
313193267Sjkim        }
314193267Sjkim
315193267Sjkim        /* Finished if address range ends before the end of this port */
316193267Sjkim
317193267Sjkim        if (LastAddress <= PortInfo->End)
318193267Sjkim        {
319193267Sjkim            break;
320193267Sjkim        }
321193267Sjkim    }
322193267Sjkim
323316303Sjkim    return (AE_OK);
324193267Sjkim}
325193267Sjkim
326193267Sjkim
327193267Sjkim/******************************************************************************
328193267Sjkim *
329193267Sjkim * FUNCTION:    AcpiHwReadPort
330193267Sjkim *
331193267Sjkim * PARAMETERS:  Address             Address of I/O port/register to read
332316303Sjkim *              Value               Where value (data) is returned
333193267Sjkim *              Width               Number of bits
334193267Sjkim *
335193267Sjkim * RETURN:      Status and value read from port
336193267Sjkim *
337193267Sjkim * DESCRIPTION: Read data from an I/O port or register. This is a front-end
338193267Sjkim *              to AcpiOsReadPort that performs validation on both the port
339193267Sjkim *              address and the length.
340193267Sjkim *
341193267Sjkim *****************************************************************************/
342193267Sjkim
343193267SjkimACPI_STATUS
344193267SjkimAcpiHwReadPort (
345193267Sjkim    ACPI_IO_ADDRESS         Address,
346193267Sjkim    UINT32                  *Value,
347193267Sjkim    UINT32                  Width)
348193267Sjkim{
349193267Sjkim    ACPI_STATUS             Status;
350193267Sjkim    UINT32                  OneByte;
351193267Sjkim    UINT32                  i;
352193267Sjkim
353193267Sjkim
354209746Sjkim    /* Truncate address to 16 bits if requested */
355209746Sjkim
356209746Sjkim    if (AcpiGbl_TruncateIoAddresses)
357209746Sjkim    {
358209746Sjkim        Address &= ACPI_UINT16_MAX;
359209746Sjkim    }
360209746Sjkim
361193267Sjkim    /* Validate the entire request and perform the I/O */
362193267Sjkim
363193267Sjkim    Status = AcpiHwValidateIoRequest (Address, Width);
364193267Sjkim    if (ACPI_SUCCESS (Status))
365193267Sjkim    {
366193267Sjkim        Status = AcpiOsReadPort (Address, Value, Width);
367193267Sjkim        return (Status);
368193267Sjkim    }
369193267Sjkim
370193267Sjkim    if (Status != AE_AML_ILLEGAL_ADDRESS)
371193267Sjkim    {
372193267Sjkim        return (Status);
373193267Sjkim    }
374193267Sjkim
375193267Sjkim    /*
376193267Sjkim     * There has been a protection violation within the request. Fall
377193267Sjkim     * back to byte granularity port I/O and ignore the failing bytes.
378316303Sjkim     * This provides compatibility with other ACPI implementations.
379193267Sjkim     */
380193267Sjkim    for (i = 0, *Value = 0; i < Width; i += 8)
381193267Sjkim    {
382193267Sjkim        /* Validate and read one byte */
383193267Sjkim
384193267Sjkim        if (AcpiHwValidateIoRequest (Address, 8) == AE_OK)
385193267Sjkim        {
386193267Sjkim            Status = AcpiOsReadPort (Address, &OneByte, 8);
387193267Sjkim            if (ACPI_FAILURE (Status))
388193267Sjkim            {
389193267Sjkim                return (Status);
390193267Sjkim            }
391193267Sjkim
392193267Sjkim            *Value |= (OneByte << i);
393193267Sjkim        }
394193267Sjkim
395193267Sjkim        Address++;
396193267Sjkim    }
397193267Sjkim
398193267Sjkim    return (AE_OK);
399193267Sjkim}
400193267Sjkim
401193267Sjkim
402193267Sjkim/******************************************************************************
403193267Sjkim *
404193267Sjkim * FUNCTION:    AcpiHwWritePort
405193267Sjkim *
406193267Sjkim * PARAMETERS:  Address             Address of I/O port/register to write
407193267Sjkim *              Value               Value to write
408193267Sjkim *              Width               Number of bits
409193267Sjkim *
410193267Sjkim * RETURN:      Status
411193267Sjkim *
412193267Sjkim * DESCRIPTION: Write data to an I/O port or register. This is a front-end
413193267Sjkim *              to AcpiOsWritePort that performs validation on both the port
414193267Sjkim *              address and the length.
415193267Sjkim *
416193267Sjkim *****************************************************************************/
417193267Sjkim
418193267SjkimACPI_STATUS
419193267SjkimAcpiHwWritePort (
420193267Sjkim    ACPI_IO_ADDRESS         Address,
421193267Sjkim    UINT32                  Value,
422193267Sjkim    UINT32                  Width)
423193267Sjkim{
424193267Sjkim    ACPI_STATUS             Status;
425193267Sjkim    UINT32                  i;
426193267Sjkim
427193267Sjkim
428209746Sjkim    /* Truncate address to 16 bits if requested */
429209746Sjkim
430209746Sjkim    if (AcpiGbl_TruncateIoAddresses)
431209746Sjkim    {
432209746Sjkim        Address &= ACPI_UINT16_MAX;
433209746Sjkim    }
434209746Sjkim
435193267Sjkim    /* Validate the entire request and perform the I/O */
436193267Sjkim
437193267Sjkim    Status = AcpiHwValidateIoRequest (Address, Width);
438193267Sjkim    if (ACPI_SUCCESS (Status))
439193267Sjkim    {
440193267Sjkim        Status = AcpiOsWritePort (Address, Value, Width);
441193267Sjkim        return (Status);
442193267Sjkim    }
443193267Sjkim
444193267Sjkim    if (Status != AE_AML_ILLEGAL_ADDRESS)
445193267Sjkim    {
446193267Sjkim        return (Status);
447193267Sjkim    }
448193267Sjkim
449193267Sjkim    /*
450193267Sjkim     * There has been a protection violation within the request. Fall
451193267Sjkim     * back to byte granularity port I/O and ignore the failing bytes.
452316303Sjkim     * This provides compatibility with other ACPI implementations.
453193267Sjkim     */
454193267Sjkim    for (i = 0; i < Width; i += 8)
455193267Sjkim    {
456193267Sjkim        /* Validate and write one byte */
457193267Sjkim
458193267Sjkim        if (AcpiHwValidateIoRequest (Address, 8) == AE_OK)
459193267Sjkim        {
460193267Sjkim            Status = AcpiOsWritePort (Address, (Value >> i) & 0xFF, 8);
461193267Sjkim            if (ACPI_FAILURE (Status))
462193267Sjkim            {
463193267Sjkim                return (Status);
464193267Sjkim            }
465193267Sjkim        }
466193267Sjkim
467193267Sjkim        Address++;
468193267Sjkim    }
469193267Sjkim
470193267Sjkim    return (AE_OK);
471193267Sjkim}
472