hwvalid.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: hwvalid - I/O request validation
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #define _COMPONENT ACPI_HARDWARE
  12. ACPI_MODULE_NAME("hwvalid")
  13. /* Local prototypes */
  14. static acpi_status
  15. acpi_hw_validate_io_request(acpi_io_address address, u32 bit_width);
  16. /*
  17. * Protected I/O ports. Some ports are always illegal, and some are
  18. * conditionally illegal. This table must remain ordered by port address.
  19. *
  20. * The table is used to implement the Microsoft port access rules that
  21. * first appeared in Windows XP. Some ports are always illegal, and some
  22. * ports are only illegal if the BIOS calls _OSI with nothing newer than
  23. * the specific _OSI strings.
  24. *
  25. * This provides ACPICA with the desired port protections and
  26. * Microsoft compatibility.
  27. *
  28. * Description of port entries:
  29. * DMA: DMA controller
  30. * PIC0: Programmable Interrupt Controller (8259A)
  31. * PIT1: System Timer 1
  32. * PIT2: System Timer 2 failsafe
  33. * RTC: Real-time clock
  34. * CMOS: Extended CMOS
  35. * DMA1: DMA 1 page registers
  36. * DMA1L: DMA 1 Ch 0 low page
  37. * DMA2: DMA 2 page registers
  38. * DMA2L: DMA 2 low page refresh
  39. * ARBC: Arbitration control
  40. * SETUP: Reserved system board setup
  41. * POS: POS channel select
  42. * PIC1: Cascaded PIC
  43. * IDMA: ISA DMA
  44. * ELCR: PIC edge/level registers
  45. * PCI: PCI configuration space
  46. */
  47. static const struct acpi_port_info acpi_protected_ports[] = {
  48. {"DMA", 0x0000, 0x000F, ACPI_OSI_WIN_XP},
  49. {"PIC0", 0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL},
  50. {"PIT1", 0x0040, 0x0043, ACPI_OSI_WIN_XP},
  51. {"PIT2", 0x0048, 0x004B, ACPI_OSI_WIN_XP},
  52. {"RTC", 0x0070, 0x0071, ACPI_OSI_WIN_XP},
  53. {"CMOS", 0x0074, 0x0076, ACPI_OSI_WIN_XP},
  54. {"DMA1", 0x0081, 0x0083, ACPI_OSI_WIN_XP},
  55. {"DMA1L", 0x0087, 0x0087, ACPI_OSI_WIN_XP},
  56. {"DMA2", 0x0089, 0x008B, ACPI_OSI_WIN_XP},
  57. {"DMA2L", 0x008F, 0x008F, ACPI_OSI_WIN_XP},
  58. {"ARBC", 0x0090, 0x0091, ACPI_OSI_WIN_XP},
  59. {"SETUP", 0x0093, 0x0094, ACPI_OSI_WIN_XP},
  60. {"POS", 0x0096, 0x0097, ACPI_OSI_WIN_XP},
  61. {"PIC1", 0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL},
  62. {"IDMA", 0x00C0, 0x00DF, ACPI_OSI_WIN_XP},
  63. {"ELCR", 0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL},
  64. {"PCI", 0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP}
  65. };
  66. #define ACPI_PORT_INFO_ENTRIES ACPI_ARRAY_LENGTH (acpi_protected_ports)
  67. /******************************************************************************
  68. *
  69. * FUNCTION: acpi_hw_validate_io_request
  70. *
  71. * PARAMETERS: Address Address of I/O port/register
  72. * bit_width Number of bits (8,16,32)
  73. *
  74. * RETURN: Status
  75. *
  76. * DESCRIPTION: Validates an I/O request (address/length). Certain ports are
  77. * always illegal and some ports are only illegal depending on
  78. * the requests the BIOS AML code makes to the predefined
  79. * _OSI method.
  80. *
  81. ******************************************************************************/
  82. static acpi_status
  83. acpi_hw_validate_io_request(acpi_io_address address, u32 bit_width)
  84. {
  85. u32 i;
  86. u32 byte_width;
  87. acpi_io_address last_address;
  88. const struct acpi_port_info *port_info;
  89. ACPI_FUNCTION_TRACE(hw_validate_io_request);
  90. /* Supported widths are 8/16/32 */
  91. if ((bit_width != 8) && (bit_width != 16) && (bit_width != 32)) {
  92. ACPI_ERROR((AE_INFO,
  93. "Bad BitWidth parameter: %8.8X", bit_width));
  94. return_ACPI_STATUS(AE_BAD_PARAMETER);
  95. }
  96. port_info = acpi_protected_ports;
  97. byte_width = ACPI_DIV_8(bit_width);
  98. last_address = address + byte_width - 1;
  99. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  100. "Address %8.8X%8.8X LastAddress %8.8X%8.8X Length %X",
  101. ACPI_FORMAT_UINT64(address),
  102. ACPI_FORMAT_UINT64(last_address), byte_width));
  103. /* Maximum 16-bit address in I/O space */
  104. if (last_address > ACPI_UINT16_MAX) {
  105. ACPI_ERROR((AE_INFO,
  106. "Illegal I/O port address/length above 64K: %8.8X%8.8X/0x%X",
  107. ACPI_FORMAT_UINT64(address), byte_width));
  108. return_ACPI_STATUS(AE_LIMIT);
  109. }
  110. /* Exit if requested address is not within the protected port table */
  111. if (address > acpi_protected_ports[ACPI_PORT_INFO_ENTRIES - 1].end) {
  112. return_ACPI_STATUS(AE_OK);
  113. }
  114. /* Check request against the list of protected I/O ports */
  115. for (i = 0; i < ACPI_PORT_INFO_ENTRIES; i++, port_info++) {
  116. /*
  117. * Check if the requested address range will write to a reserved
  118. * port. There are four cases to consider:
  119. *
  120. * 1) Address range is contained completely in the port address range
  121. * 2) Address range overlaps port range at the port range start
  122. * 3) Address range overlaps port range at the port range end
  123. * 4) Address range completely encompasses the port range
  124. */
  125. if ((address <= port_info->end)
  126. && (last_address >= port_info->start)) {
  127. /* Port illegality may depend on the _OSI calls made by the BIOS */
  128. if (port_info->osi_dependency == ACPI_ALWAYS_ILLEGAL ||
  129. acpi_gbl_osi_data == port_info->osi_dependency) {
  130. ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
  131. "Denied AML access to port 0x%8.8X%8.8X/%X (%s 0x%.4X-0x%.4X)\n",
  132. ACPI_FORMAT_UINT64(address),
  133. byte_width, port_info->name,
  134. port_info->start,
  135. port_info->end));
  136. return_ACPI_STATUS(AE_AML_ILLEGAL_ADDRESS);
  137. }
  138. }
  139. /* Finished if address range ends before the end of this port */
  140. if (last_address <= port_info->end) {
  141. break;
  142. }
  143. }
  144. return_ACPI_STATUS(AE_OK);
  145. }
  146. /******************************************************************************
  147. *
  148. * FUNCTION: acpi_hw_read_port
  149. *
  150. * PARAMETERS: Address Address of I/O port/register to read
  151. * Value Where value (data) is returned
  152. * Width Number of bits
  153. *
  154. * RETURN: Status and value read from port
  155. *
  156. * DESCRIPTION: Read data from an I/O port or register. This is a front-end
  157. * to acpi_os_read_port that performs validation on both the port
  158. * address and the length.
  159. *
  160. *****************************************************************************/
  161. acpi_status acpi_hw_read_port(acpi_io_address address, u32 *value, u32 width)
  162. {
  163. acpi_status status;
  164. u32 one_byte;
  165. u32 i;
  166. /* Truncate address to 16 bits if requested */
  167. if (acpi_gbl_truncate_io_addresses) {
  168. address &= ACPI_UINT16_MAX;
  169. }
  170. /* Validate the entire request and perform the I/O */
  171. status = acpi_hw_validate_io_request(address, width);
  172. if (ACPI_SUCCESS(status)) {
  173. status = acpi_os_read_port(address, value, width);
  174. return (status);
  175. }
  176. if (status != AE_AML_ILLEGAL_ADDRESS) {
  177. return (status);
  178. }
  179. /*
  180. * There has been a protection violation within the request. Fall
  181. * back to byte granularity port I/O and ignore the failing bytes.
  182. * This provides compatibility with other ACPI implementations.
  183. */
  184. for (i = 0, *value = 0; i < width; i += 8) {
  185. /* Validate and read one byte */
  186. if (acpi_hw_validate_io_request(address, 8) == AE_OK) {
  187. status = acpi_os_read_port(address, &one_byte, 8);
  188. if (ACPI_FAILURE(status)) {
  189. return (status);
  190. }
  191. *value |= (one_byte << i);
  192. }
  193. address++;
  194. }
  195. return (AE_OK);
  196. }
  197. /******************************************************************************
  198. *
  199. * FUNCTION: acpi_hw_write_port
  200. *
  201. * PARAMETERS: Address Address of I/O port/register to write
  202. * Value Value to write
  203. * Width Number of bits
  204. *
  205. * RETURN: Status
  206. *
  207. * DESCRIPTION: Write data to an I/O port or register. This is a front-end
  208. * to acpi_os_write_port that performs validation on both the port
  209. * address and the length.
  210. *
  211. *****************************************************************************/
  212. acpi_status acpi_hw_write_port(acpi_io_address address, u32 value, u32 width)
  213. {
  214. acpi_status status;
  215. u32 i;
  216. /* Truncate address to 16 bits if requested */
  217. if (acpi_gbl_truncate_io_addresses) {
  218. address &= ACPI_UINT16_MAX;
  219. }
  220. /* Validate the entire request and perform the I/O */
  221. status = acpi_hw_validate_io_request(address, width);
  222. if (ACPI_SUCCESS(status)) {
  223. status = acpi_os_write_port(address, value, width);
  224. return (status);
  225. }
  226. if (status != AE_AML_ILLEGAL_ADDRESS) {
  227. return (status);
  228. }
  229. /*
  230. * There has been a protection violation within the request. Fall
  231. * back to byte granularity port I/O and ignore the failing bytes.
  232. * This provides compatibility with other ACPI implementations.
  233. */
  234. for (i = 0; i < width; i += 8) {
  235. /* Validate and write one byte */
  236. if (acpi_hw_validate_io_request(address, 8) == AE_OK) {
  237. status =
  238. acpi_os_write_port(address, (value >> i) & 0xFF, 8);
  239. if (ACPI_FAILURE(status)) {
  240. return (status);
  241. }
  242. }
  243. address++;
  244. }
  245. return (AE_OK);
  246. }
  247. /******************************************************************************
  248. *
  249. * FUNCTION: acpi_hw_validate_io_block
  250. *
  251. * PARAMETERS: Address Address of I/O port/register blobk
  252. * bit_width Number of bits (8,16,32) in each register
  253. * count Number of registers in the block
  254. *
  255. * RETURN: Status
  256. *
  257. * DESCRIPTION: Validates a block of I/O ports/registers.
  258. *
  259. ******************************************************************************/
  260. acpi_status acpi_hw_validate_io_block(u64 address, u32 bit_width, u32 count)
  261. {
  262. acpi_status status;
  263. while (count--) {
  264. status = acpi_hw_validate_io_request((acpi_io_address)address,
  265. bit_width);
  266. if (ACPI_FAILURE(status))
  267. return_ACPI_STATUS(status);
  268. address += ACPI_DIV_8(bit_width);
  269. }
  270. return_ACPI_STATUS(AE_OK);
  271. }