xref: /openbmc/linux/drivers/acpi/acpica/hwvalid.c (revision 612c2932)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
27f071903SBob Moore /******************************************************************************
37f071903SBob Moore  *
47f071903SBob Moore  * Module Name: hwvalid - I/O request validation
57f071903SBob Moore  *
6*612c2932SBob Moore  * Copyright (C) 2000 - 2023, Intel Corp.
77f071903SBob Moore  *
895857638SErik Schmauss  *****************************************************************************/
97f071903SBob Moore 
107f071903SBob Moore #include <acpi/acpi.h>
117f071903SBob Moore #include "accommon.h"
127f071903SBob Moore 
137f071903SBob Moore #define _COMPONENT          ACPI_HARDWARE
147f071903SBob Moore ACPI_MODULE_NAME("hwvalid")
157f071903SBob Moore 
167f071903SBob Moore /* Local prototypes */
177f071903SBob Moore static acpi_status
187f071903SBob Moore acpi_hw_validate_io_request(acpi_io_address address, u32 bit_width);
197f071903SBob Moore 
207f071903SBob Moore /*
217f071903SBob Moore  * Protected I/O ports. Some ports are always illegal, and some are
227f071903SBob Moore  * conditionally illegal. This table must remain ordered by port address.
237f071903SBob Moore  *
247f071903SBob Moore  * The table is used to implement the Microsoft port access rules that
257f071903SBob Moore  * first appeared in Windows XP. Some ports are always illegal, and some
26e1d91485SMario Limonciello  * ports are only illegal if the BIOS calls _OSI with nothing newer than
27e1d91485SMario Limonciello  * the specific _OSI strings.
287f071903SBob Moore  *
297f071903SBob Moore  * This provides ACPICA with the desired port protections and
307f071903SBob Moore  * Microsoft compatibility.
31ee6a0fbdSBob Moore  *
32ee6a0fbdSBob Moore  * Description of port entries:
33ee6a0fbdSBob Moore  *  DMA:   DMA controller
34ba494beeSBob Moore  *  PIC0:  Programmable Interrupt Controller (8259A)
35ee6a0fbdSBob Moore  *  PIT1:  System Timer 1
36ee6a0fbdSBob Moore  *  PIT2:  System Timer 2 failsafe
37ee6a0fbdSBob Moore  *  RTC:   Real-time clock
38ee6a0fbdSBob Moore  *  CMOS:  Extended CMOS
39ee6a0fbdSBob Moore  *  DMA1:  DMA 1 page registers
40ee6a0fbdSBob Moore  *  DMA1L: DMA 1 Ch 0 low page
41ee6a0fbdSBob Moore  *  DMA2:  DMA 2 page registers
42ee6a0fbdSBob Moore  *  DMA2L: DMA 2 low page refresh
43ee6a0fbdSBob Moore  *  ARBC:  Arbitration control
44ee6a0fbdSBob Moore  *  SETUP: Reserved system board setup
45ee6a0fbdSBob Moore  *  POS:   POS channel select
46ee6a0fbdSBob Moore  *  PIC1:  Cascaded PIC
47ee6a0fbdSBob Moore  *  IDMA:  ISA DMA
48ee6a0fbdSBob Moore  *  ELCR:  PIC edge/level registers
49ee6a0fbdSBob Moore  *  PCI:   PCI configuration space
507f071903SBob Moore  */
517f071903SBob Moore static const struct acpi_port_info acpi_protected_ports[] = {
52ee6a0fbdSBob Moore 	{"DMA", 0x0000, 0x000F, ACPI_OSI_WIN_XP},
537f071903SBob Moore 	{"PIC0", 0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL},
547f071903SBob Moore 	{"PIT1", 0x0040, 0x0043, ACPI_OSI_WIN_XP},
557f071903SBob Moore 	{"PIT2", 0x0048, 0x004B, ACPI_OSI_WIN_XP},
567f071903SBob Moore 	{"RTC", 0x0070, 0x0071, ACPI_OSI_WIN_XP},
577f071903SBob Moore 	{"CMOS", 0x0074, 0x0076, ACPI_OSI_WIN_XP},
58a38d75faSLen Brown 	{"DMA1", 0x0081, 0x0083, ACPI_OSI_WIN_XP},
59ee6a0fbdSBob Moore 	{"DMA1L", 0x0087, 0x0087, ACPI_OSI_WIN_XP},
60ee6a0fbdSBob Moore 	{"DMA2", 0x0089, 0x008B, ACPI_OSI_WIN_XP},
61ee6a0fbdSBob Moore 	{"DMA2L", 0x008F, 0x008F, ACPI_OSI_WIN_XP},
62ee6a0fbdSBob Moore 	{"ARBC", 0x0090, 0x0091, ACPI_OSI_WIN_XP},
63ee6a0fbdSBob Moore 	{"SETUP", 0x0093, 0x0094, ACPI_OSI_WIN_XP},
647f071903SBob Moore 	{"POS", 0x0096, 0x0097, ACPI_OSI_WIN_XP},
657f071903SBob Moore 	{"PIC1", 0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL},
66ee6a0fbdSBob Moore 	{"IDMA", 0x00C0, 0x00DF, ACPI_OSI_WIN_XP},
677f071903SBob Moore 	{"ELCR", 0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL},
68f28ad2c3SBob Moore 	{"PCI", 0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP}
697f071903SBob Moore };
707f071903SBob Moore 
717f071903SBob Moore #define ACPI_PORT_INFO_ENTRIES      ACPI_ARRAY_LENGTH (acpi_protected_ports)
727f071903SBob Moore 
737f071903SBob Moore /******************************************************************************
747f071903SBob Moore  *
757f071903SBob Moore  * FUNCTION:    acpi_hw_validate_io_request
767f071903SBob Moore  *
777f071903SBob Moore  * PARAMETERS:  Address             Address of I/O port/register
787f071903SBob Moore  *              bit_width           Number of bits (8,16,32)
797f071903SBob Moore  *
807f071903SBob Moore  * RETURN:      Status
817f071903SBob Moore  *
827f071903SBob Moore  * DESCRIPTION: Validates an I/O request (address/length). Certain ports are
837f071903SBob Moore  *              always illegal and some ports are only illegal depending on
847f071903SBob Moore  *              the requests the BIOS AML code makes to the predefined
857f071903SBob Moore  *              _OSI method.
867f071903SBob Moore  *
877f071903SBob Moore  ******************************************************************************/
887f071903SBob Moore 
897f071903SBob Moore static acpi_status
acpi_hw_validate_io_request(acpi_io_address address,u32 bit_width)907f071903SBob Moore acpi_hw_validate_io_request(acpi_io_address address, u32 bit_width)
917f071903SBob Moore {
927f071903SBob Moore 	u32 i;
937f071903SBob Moore 	u32 byte_width;
947f071903SBob Moore 	acpi_io_address last_address;
957f071903SBob Moore 	const struct acpi_port_info *port_info;
967f071903SBob Moore 
9756d03d7bSBob Moore 	ACPI_FUNCTION_TRACE(hw_validate_io_request);
987f071903SBob Moore 
997f071903SBob Moore 	/* Supported widths are 8/16/32 */
1007f071903SBob Moore 
1017f071903SBob Moore 	if ((bit_width != 8) && (bit_width != 16) && (bit_width != 32)) {
1028519bc9fSBob Moore 		ACPI_ERROR((AE_INFO,
1038519bc9fSBob Moore 			    "Bad BitWidth parameter: %8.8X", bit_width));
10456d03d7bSBob Moore 		return_ACPI_STATUS(AE_BAD_PARAMETER);
1057f071903SBob Moore 	}
1067f071903SBob Moore 
1077f071903SBob Moore 	port_info = acpi_protected_ports;
1087f071903SBob Moore 	byte_width = ACPI_DIV_8(bit_width);
1097f071903SBob Moore 	last_address = address + byte_width - 1;
1107f071903SBob Moore 
111cc2080b0SLv Zheng 	ACPI_DEBUG_PRINT((ACPI_DB_IO,
112cc2080b0SLv Zheng 			  "Address %8.8X%8.8X LastAddress %8.8X%8.8X Length %X",
113cc2080b0SLv Zheng 			  ACPI_FORMAT_UINT64(address),
114cc2080b0SLv Zheng 			  ACPI_FORMAT_UINT64(last_address), byte_width));
1157f071903SBob Moore 
1167f071903SBob Moore 	/* Maximum 16-bit address in I/O space */
1177f071903SBob Moore 
1187f071903SBob Moore 	if (last_address > ACPI_UINT16_MAX) {
1197f071903SBob Moore 		ACPI_ERROR((AE_INFO,
120cc2080b0SLv Zheng 			    "Illegal I/O port address/length above 64K: %8.8X%8.8X/0x%X",
121cc2080b0SLv Zheng 			    ACPI_FORMAT_UINT64(address), byte_width));
12256d03d7bSBob Moore 		return_ACPI_STATUS(AE_LIMIT);
1237f071903SBob Moore 	}
1247f071903SBob Moore 
1257f071903SBob Moore 	/* Exit if requested address is not within the protected port table */
1267f071903SBob Moore 
1277f071903SBob Moore 	if (address > acpi_protected_ports[ACPI_PORT_INFO_ENTRIES - 1].end) {
12856d03d7bSBob Moore 		return_ACPI_STATUS(AE_OK);
1297f071903SBob Moore 	}
1307f071903SBob Moore 
1317f071903SBob Moore 	/* Check request against the list of protected I/O ports */
1327f071903SBob Moore 
1337f071903SBob Moore 	for (i = 0; i < ACPI_PORT_INFO_ENTRIES; i++, port_info++) {
1347f071903SBob Moore 		/*
1357f071903SBob Moore 		 * Check if the requested address range will write to a reserved
1361b8f77aaSBob Moore 		 * port. There are four cases to consider:
1377f071903SBob Moore 		 *
1387f071903SBob Moore 		 * 1) Address range is contained completely in the port address range
1397f071903SBob Moore 		 * 2) Address range overlaps port range at the port range start
1407f071903SBob Moore 		 * 3) Address range overlaps port range at the port range end
1417f071903SBob Moore 		 * 4) Address range completely encompasses the port range
1427f071903SBob Moore 		 */
1437f071903SBob Moore 		if ((address <= port_info->end)
1447f071903SBob Moore 		    && (last_address >= port_info->start)) {
1457f071903SBob Moore 
1467f071903SBob Moore 			/* Port illegality may depend on the _OSI calls made by the BIOS */
1477f071903SBob Moore 
148e1d91485SMario Limonciello 			if (port_info->osi_dependency == ACPI_ALWAYS_ILLEGAL ||
149e1d91485SMario Limonciello 			    acpi_gbl_osi_data == port_info->osi_dependency) {
15056d03d7bSBob Moore 				ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
15156d03d7bSBob Moore 						  "Denied AML access to port 0x%8.8X%8.8X/%X (%s 0x%.4X-0x%.4X)\n",
152cc2080b0SLv Zheng 						  ACPI_FORMAT_UINT64(address),
1537f071903SBob Moore 						  byte_width, port_info->name,
154a65131e9SLin Ming 						  port_info->start,
155a65131e9SLin Ming 						  port_info->end));
1567f071903SBob Moore 
1577f071903SBob Moore 				return_ACPI_STATUS(AE_AML_ILLEGAL_ADDRESS);
1587f071903SBob Moore 			}
1597f071903SBob Moore 		}
1607f071903SBob Moore 
1617f071903SBob Moore 		/* Finished if address range ends before the end of this port */
1627f071903SBob Moore 
1637f071903SBob Moore 		if (last_address <= port_info->end) {
1647f071903SBob Moore 			break;
1657f071903SBob Moore 		}
1667f071903SBob Moore 	}
1677f071903SBob Moore 
16856d03d7bSBob Moore 	return_ACPI_STATUS(AE_OK);
1697f071903SBob Moore }
1707f071903SBob Moore 
1717f071903SBob Moore /******************************************************************************
1727f071903SBob Moore  *
1737f071903SBob Moore  * FUNCTION:    acpi_hw_read_port
1747f071903SBob Moore  *
1757f071903SBob Moore  * PARAMETERS:  Address             Address of I/O port/register to read
1761b8f77aaSBob Moore  *              Value               Where value (data) is returned
1777f071903SBob Moore  *              Width               Number of bits
1787f071903SBob Moore  *
179a65131e9SLin Ming  * RETURN:      Status and value read from port
1807f071903SBob Moore  *
1817f071903SBob Moore  * DESCRIPTION: Read data from an I/O port or register. This is a front-end
1827f071903SBob Moore  *              to acpi_os_read_port that performs validation on both the port
1837f071903SBob Moore  *              address and the length.
1847f071903SBob Moore  *
1857f071903SBob Moore  *****************************************************************************/
1867f071903SBob Moore 
acpi_hw_read_port(acpi_io_address address,u32 * value,u32 width)1877f071903SBob Moore acpi_status acpi_hw_read_port(acpi_io_address address, u32 *value, u32 width)
1887f071903SBob Moore {
1897f071903SBob Moore 	acpi_status status;
190a65131e9SLin Ming 	u32 one_byte;
191a65131e9SLin Ming 	u32 i;
192a65131e9SLin Ming 
193b681f7d9SMatthew Garrett 	/* Truncate address to 16 bits if requested */
194b681f7d9SMatthew Garrett 
195b681f7d9SMatthew Garrett 	if (acpi_gbl_truncate_io_addresses) {
196b681f7d9SMatthew Garrett 		address &= ACPI_UINT16_MAX;
197b681f7d9SMatthew Garrett 	}
198b681f7d9SMatthew Garrett 
199a65131e9SLin Ming 	/* Validate the entire request and perform the I/O */
2007f071903SBob Moore 
2017f071903SBob Moore 	status = acpi_hw_validate_io_request(address, width);
202a65131e9SLin Ming 	if (ACPI_SUCCESS(status)) {
203a65131e9SLin Ming 		status = acpi_os_read_port(address, value, width);
2049c0d7939SLv Zheng 		return (status);
205a65131e9SLin Ming 	}
206a65131e9SLin Ming 
207a65131e9SLin Ming 	if (status != AE_AML_ILLEGAL_ADDRESS) {
2089c0d7939SLv Zheng 		return (status);
209a65131e9SLin Ming 	}
210a65131e9SLin Ming 
211a65131e9SLin Ming 	/*
212a65131e9SLin Ming 	 * There has been a protection violation within the request. Fall
213a65131e9SLin Ming 	 * back to byte granularity port I/O and ignore the failing bytes.
2141b8f77aaSBob Moore 	 * This provides compatibility with other ACPI implementations.
215a65131e9SLin Ming 	 */
216a65131e9SLin Ming 	for (i = 0, *value = 0; i < width; i += 8) {
217a65131e9SLin Ming 
218a65131e9SLin Ming 		/* Validate and read one byte */
219a65131e9SLin Ming 
220a65131e9SLin Ming 		if (acpi_hw_validate_io_request(address, 8) == AE_OK) {
221a65131e9SLin Ming 			status = acpi_os_read_port(address, &one_byte, 8);
2227f071903SBob Moore 			if (ACPI_FAILURE(status)) {
2239c0d7939SLv Zheng 				return (status);
2247f071903SBob Moore 			}
2257f071903SBob Moore 
226a65131e9SLin Ming 			*value |= (one_byte << i);
227a65131e9SLin Ming 		}
228a65131e9SLin Ming 
229a65131e9SLin Ming 		address++;
230a65131e9SLin Ming 	}
231a65131e9SLin Ming 
2329c0d7939SLv Zheng 	return (AE_OK);
2337f071903SBob Moore }
2347f071903SBob Moore 
2357f071903SBob Moore /******************************************************************************
2367f071903SBob Moore  *
2377f071903SBob Moore  * FUNCTION:    acpi_hw_write_port
2387f071903SBob Moore  *
2397f071903SBob Moore  * PARAMETERS:  Address             Address of I/O port/register to write
2407f071903SBob Moore  *              Value               Value to write
2417f071903SBob Moore  *              Width               Number of bits
2427f071903SBob Moore  *
243a65131e9SLin Ming  * RETURN:      Status
2447f071903SBob Moore  *
2457f071903SBob Moore  * DESCRIPTION: Write data to an I/O port or register. This is a front-end
2467f071903SBob Moore  *              to acpi_os_write_port that performs validation on both the port
2477f071903SBob Moore  *              address and the length.
2487f071903SBob Moore  *
2497f071903SBob Moore  *****************************************************************************/
2507f071903SBob Moore 
acpi_hw_write_port(acpi_io_address address,u32 value,u32 width)2517f071903SBob Moore acpi_status acpi_hw_write_port(acpi_io_address address, u32 value, u32 width)
2527f071903SBob Moore {
2537f071903SBob Moore 	acpi_status status;
254a65131e9SLin Ming 	u32 i;
255a65131e9SLin Ming 
256b681f7d9SMatthew Garrett 	/* Truncate address to 16 bits if requested */
257b681f7d9SMatthew Garrett 
258b681f7d9SMatthew Garrett 	if (acpi_gbl_truncate_io_addresses) {
259b681f7d9SMatthew Garrett 		address &= ACPI_UINT16_MAX;
260b681f7d9SMatthew Garrett 	}
261b681f7d9SMatthew Garrett 
262a65131e9SLin Ming 	/* Validate the entire request and perform the I/O */
2637f071903SBob Moore 
2647f071903SBob Moore 	status = acpi_hw_validate_io_request(address, width);
265a65131e9SLin Ming 	if (ACPI_SUCCESS(status)) {
266a65131e9SLin Ming 		status = acpi_os_write_port(address, value, width);
2679c0d7939SLv Zheng 		return (status);
2687f071903SBob Moore 	}
2697f071903SBob Moore 
270a65131e9SLin Ming 	if (status != AE_AML_ILLEGAL_ADDRESS) {
2719c0d7939SLv Zheng 		return (status);
2727f071903SBob Moore 	}
273a65131e9SLin Ming 
274a65131e9SLin Ming 	/*
275a65131e9SLin Ming 	 * There has been a protection violation within the request. Fall
276a65131e9SLin Ming 	 * back to byte granularity port I/O and ignore the failing bytes.
2771b8f77aaSBob Moore 	 * This provides compatibility with other ACPI implementations.
278a65131e9SLin Ming 	 */
279a65131e9SLin Ming 	for (i = 0; i < width; i += 8) {
280a65131e9SLin Ming 
281a65131e9SLin Ming 		/* Validate and write one byte */
282a65131e9SLin Ming 
283a65131e9SLin Ming 		if (acpi_hw_validate_io_request(address, 8) == AE_OK) {
284a65131e9SLin Ming 			status =
285a65131e9SLin Ming 			    acpi_os_write_port(address, (value >> i) & 0xFF, 8);
286a65131e9SLin Ming 			if (ACPI_FAILURE(status)) {
2879c0d7939SLv Zheng 				return (status);
288a65131e9SLin Ming 			}
289a65131e9SLin Ming 		}
290a65131e9SLin Ming 
291a65131e9SLin Ming 		address++;
292a65131e9SLin Ming 	}
293a65131e9SLin Ming 
2949c0d7939SLv Zheng 	return (AE_OK);
295a65131e9SLin Ming }
29684b43284SRafael J. Wysocki 
29784b43284SRafael J. Wysocki /******************************************************************************
29884b43284SRafael J. Wysocki  *
29984b43284SRafael J. Wysocki  * FUNCTION:    acpi_hw_validate_io_block
30084b43284SRafael J. Wysocki  *
30184b43284SRafael J. Wysocki  * PARAMETERS:  Address             Address of I/O port/register blobk
30284b43284SRafael J. Wysocki  *              bit_width           Number of bits (8,16,32) in each register
30384b43284SRafael J. Wysocki  *              count               Number of registers in the block
30484b43284SRafael J. Wysocki  *
30584b43284SRafael J. Wysocki  * RETURN:      Status
30684b43284SRafael J. Wysocki  *
30784b43284SRafael J. Wysocki  * DESCRIPTION: Validates a block of I/O ports/registers.
30884b43284SRafael J. Wysocki  *
30984b43284SRafael J. Wysocki  ******************************************************************************/
31084b43284SRafael J. Wysocki 
acpi_hw_validate_io_block(u64 address,u32 bit_width,u32 count)31184b43284SRafael J. Wysocki acpi_status acpi_hw_validate_io_block(u64 address, u32 bit_width, u32 count)
31284b43284SRafael J. Wysocki {
31384b43284SRafael J. Wysocki 	acpi_status status;
31484b43284SRafael J. Wysocki 
31584b43284SRafael J. Wysocki 	while (count--) {
31684b43284SRafael J. Wysocki 		status = acpi_hw_validate_io_request((acpi_io_address)address,
31784b43284SRafael J. Wysocki 						     bit_width);
31884b43284SRafael J. Wysocki 		if (ACPI_FAILURE(status))
31984b43284SRafael J. Wysocki 			return_ACPI_STATUS(status);
32084b43284SRafael J. Wysocki 
32184b43284SRafael J. Wysocki 		address += ACPI_DIV_8(bit_width);
32284b43284SRafael J. Wysocki 	}
32384b43284SRafael J. Wysocki 
32484b43284SRafael J. Wysocki 	return_ACPI_STATUS(AE_OK);
32584b43284SRafael J. Wysocki }
326