1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: hwvalid - I/O request validation
6 * Copyright (C) 2000 - 2023, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
13 #define _COMPONENT ACPI_HARDWARE
14 ACPI_MODULE_NAME("hwvalid")
16 /* Local prototypes */
18 acpi_hw_validate_io_request(acpi_io_address address
, u32 bit_width
);
21 * Protected I/O ports. Some ports are always illegal, and some are
22 * conditionally illegal. This table must remain ordered by port address.
24 * The table is used to implement the Microsoft port access rules that
25 * first appeared in Windows XP. Some ports are always illegal, and some
26 * ports are only illegal if the BIOS calls _OSI with nothing newer than
27 * the specific _OSI strings.
29 * This provides ACPICA with the desired port protections and
30 * Microsoft compatibility.
32 * Description of port entries:
34 * PIC0: Programmable Interrupt Controller (8259A)
35 * PIT1: System Timer 1
36 * PIT2: System Timer 2 failsafe
37 * RTC: Real-time clock
39 * DMA1: DMA 1 page registers
40 * DMA1L: DMA 1 Ch 0 low page
41 * DMA2: DMA 2 page registers
42 * DMA2L: DMA 2 low page refresh
43 * ARBC: Arbitration control
44 * SETUP: Reserved system board setup
45 * POS: POS channel select
48 * ELCR: PIC edge/level registers
49 * PCI: PCI configuration space
51 static const struct acpi_port_info acpi_protected_ports
[] = {
52 {"DMA", 0x0000, 0x000F, ACPI_OSI_WIN_XP
},
53 {"PIC0", 0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL
},
54 {"PIT1", 0x0040, 0x0043, ACPI_OSI_WIN_XP
},
55 {"PIT2", 0x0048, 0x004B, ACPI_OSI_WIN_XP
},
56 {"RTC", 0x0070, 0x0071, ACPI_OSI_WIN_XP
},
57 {"CMOS", 0x0074, 0x0076, ACPI_OSI_WIN_XP
},
58 {"DMA1", 0x0081, 0x0083, ACPI_OSI_WIN_XP
},
59 {"DMA1L", 0x0087, 0x0087, ACPI_OSI_WIN_XP
},
60 {"DMA2", 0x0089, 0x008B, ACPI_OSI_WIN_XP
},
61 {"DMA2L", 0x008F, 0x008F, ACPI_OSI_WIN_XP
},
62 {"ARBC", 0x0090, 0x0091, ACPI_OSI_WIN_XP
},
63 {"SETUP", 0x0093, 0x0094, ACPI_OSI_WIN_XP
},
64 {"POS", 0x0096, 0x0097, ACPI_OSI_WIN_XP
},
65 {"PIC1", 0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL
},
66 {"IDMA", 0x00C0, 0x00DF, ACPI_OSI_WIN_XP
},
67 {"ELCR", 0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL
},
68 {"PCI", 0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP
}
71 #define ACPI_PORT_INFO_ENTRIES ACPI_ARRAY_LENGTH (acpi_protected_ports)
73 /******************************************************************************
75 * FUNCTION: acpi_hw_validate_io_request
77 * PARAMETERS: Address Address of I/O port/register
78 * bit_width Number of bits (8,16,32)
82 * DESCRIPTION: Validates an I/O request (address/length). Certain ports are
83 * always illegal and some ports are only illegal depending on
84 * the requests the BIOS AML code makes to the predefined
87 ******************************************************************************/
90 acpi_hw_validate_io_request(acpi_io_address address
, u32 bit_width
)
94 acpi_io_address last_address
;
95 const struct acpi_port_info
*port_info
;
97 ACPI_FUNCTION_TRACE(hw_validate_io_request
);
99 /* Supported widths are 8/16/32 */
101 if ((bit_width
!= 8) && (bit_width
!= 16) && (bit_width
!= 32)) {
103 "Bad BitWidth parameter: %8.8X", bit_width
));
104 return_ACPI_STATUS(AE_BAD_PARAMETER
);
107 port_info
= acpi_protected_ports
;
108 byte_width
= ACPI_DIV_8(bit_width
);
109 last_address
= address
+ byte_width
- 1;
111 ACPI_DEBUG_PRINT((ACPI_DB_IO
,
112 "Address %8.8X%8.8X LastAddress %8.8X%8.8X Length %X",
113 ACPI_FORMAT_UINT64(address
),
114 ACPI_FORMAT_UINT64(last_address
), byte_width
));
116 /* Maximum 16-bit address in I/O space */
118 if (last_address
> ACPI_UINT16_MAX
) {
120 "Illegal I/O port address/length above 64K: %8.8X%8.8X/0x%X",
121 ACPI_FORMAT_UINT64(address
), byte_width
));
122 return_ACPI_STATUS(AE_LIMIT
);
125 /* Exit if requested address is not within the protected port table */
127 if (address
> acpi_protected_ports
[ACPI_PORT_INFO_ENTRIES
- 1].end
) {
128 return_ACPI_STATUS(AE_OK
);
131 /* Check request against the list of protected I/O ports */
133 for (i
= 0; i
< ACPI_PORT_INFO_ENTRIES
; i
++, port_info
++) {
135 * Check if the requested address range will write to a reserved
136 * port. There are four cases to consider:
138 * 1) Address range is contained completely in the port address range
139 * 2) Address range overlaps port range at the port range start
140 * 3) Address range overlaps port range at the port range end
141 * 4) Address range completely encompasses the port range
143 if ((address
<= port_info
->end
)
144 && (last_address
>= port_info
->start
)) {
146 /* Port illegality may depend on the _OSI calls made by the BIOS */
148 if (port_info
->osi_dependency
== ACPI_ALWAYS_ILLEGAL
||
149 acpi_gbl_osi_data
== port_info
->osi_dependency
) {
150 ACPI_DEBUG_PRINT((ACPI_DB_VALUES
,
151 "Denied AML access to port 0x%8.8X%8.8X/%X (%s 0x%.4X-0x%.4X)\n",
152 ACPI_FORMAT_UINT64(address
),
153 byte_width
, port_info
->name
,
157 return_ACPI_STATUS(AE_AML_ILLEGAL_ADDRESS
);
161 /* Finished if address range ends before the end of this port */
163 if (last_address
<= port_info
->end
) {
168 return_ACPI_STATUS(AE_OK
);
171 /******************************************************************************
173 * FUNCTION: acpi_hw_read_port
175 * PARAMETERS: Address Address of I/O port/register to read
176 * Value Where value (data) is returned
177 * Width Number of bits
179 * RETURN: Status and value read from port
181 * DESCRIPTION: Read data from an I/O port or register. This is a front-end
182 * to acpi_os_read_port that performs validation on both the port
183 * address and the length.
185 *****************************************************************************/
187 acpi_status
acpi_hw_read_port(acpi_io_address address
, u32
*value
, u32 width
)
193 /* Truncate address to 16 bits if requested */
195 if (acpi_gbl_truncate_io_addresses
) {
196 address
&= ACPI_UINT16_MAX
;
199 /* Validate the entire request and perform the I/O */
201 status
= acpi_hw_validate_io_request(address
, width
);
202 if (ACPI_SUCCESS(status
)) {
203 status
= acpi_os_read_port(address
, value
, width
);
207 if (status
!= AE_AML_ILLEGAL_ADDRESS
) {
212 * There has been a protection violation within the request. Fall
213 * back to byte granularity port I/O and ignore the failing bytes.
214 * This provides compatibility with other ACPI implementations.
216 for (i
= 0, *value
= 0; i
< width
; i
+= 8) {
218 /* Validate and read one byte */
220 if (acpi_hw_validate_io_request(address
, 8) == AE_OK
) {
221 status
= acpi_os_read_port(address
, &one_byte
, 8);
222 if (ACPI_FAILURE(status
)) {
226 *value
|= (one_byte
<< i
);
235 /******************************************************************************
237 * FUNCTION: acpi_hw_write_port
239 * PARAMETERS: Address Address of I/O port/register to write
240 * Value Value to write
241 * Width Number of bits
245 * DESCRIPTION: Write data to an I/O port or register. This is a front-end
246 * to acpi_os_write_port that performs validation on both the port
247 * address and the length.
249 *****************************************************************************/
251 acpi_status
acpi_hw_write_port(acpi_io_address address
, u32 value
, u32 width
)
256 /* Truncate address to 16 bits if requested */
258 if (acpi_gbl_truncate_io_addresses
) {
259 address
&= ACPI_UINT16_MAX
;
262 /* Validate the entire request and perform the I/O */
264 status
= acpi_hw_validate_io_request(address
, width
);
265 if (ACPI_SUCCESS(status
)) {
266 status
= acpi_os_write_port(address
, value
, width
);
270 if (status
!= AE_AML_ILLEGAL_ADDRESS
) {
275 * There has been a protection violation within the request. Fall
276 * back to byte granularity port I/O and ignore the failing bytes.
277 * This provides compatibility with other ACPI implementations.
279 for (i
= 0; i
< width
; i
+= 8) {
281 /* Validate and write one byte */
283 if (acpi_hw_validate_io_request(address
, 8) == AE_OK
) {
285 acpi_os_write_port(address
, (value
>> i
) & 0xFF, 8);
286 if (ACPI_FAILURE(status
)) {
297 /******************************************************************************
299 * FUNCTION: acpi_hw_validate_io_block
301 * PARAMETERS: Address Address of I/O port/register blobk
302 * bit_width Number of bits (8,16,32) in each register
303 * count Number of registers in the block
307 * DESCRIPTION: Validates a block of I/O ports/registers.
309 ******************************************************************************/
311 acpi_status
acpi_hw_validate_io_block(u64 address
, u32 bit_width
, u32 count
)
316 status
= acpi_hw_validate_io_request((acpi_io_address
)address
,
318 if (ACPI_FAILURE(status
))
319 return_ACPI_STATUS(status
);
321 address
+= ACPI_DIV_8(bit_width
);
324 return_ACPI_STATUS(AE_OK
);