exregion.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exregion - ACPI default op_region (address space) handlers
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #define _COMPONENT ACPI_EXECUTER
  13. ACPI_MODULE_NAME("exregion")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: acpi_ex_system_memory_space_handler
  17. *
  18. * PARAMETERS: function - Read or Write operation
  19. * address - Where in the space to read or write
  20. * bit_width - Field width in bits (8, 16, or 32)
  21. * value - Pointer to in or out value
  22. * handler_context - Pointer to Handler's context
  23. * region_context - Pointer to context specific to the
  24. * accessed region
  25. *
  26. * RETURN: Status
  27. *
  28. * DESCRIPTION: Handler for the System Memory address space (Op Region)
  29. *
  30. ******************************************************************************/
  31. acpi_status
  32. acpi_ex_system_memory_space_handler(u32 function,
  33. acpi_physical_address address,
  34. u32 bit_width,
  35. u64 *value,
  36. void *handler_context, void *region_context)
  37. {
  38. acpi_status status = AE_OK;
  39. void *logical_addr_ptr = NULL;
  40. struct acpi_mem_space_context *mem_info = region_context;
  41. struct acpi_mem_mapping *mm = mem_info->cur_mm;
  42. u32 length;
  43. acpi_size map_length;
  44. acpi_size page_boundary_map_length;
  45. #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
  46. u32 remainder;
  47. #endif
  48. ACPI_FUNCTION_TRACE(ex_system_memory_space_handler);
  49. /* Validate and translate the bit width */
  50. switch (bit_width) {
  51. case 8:
  52. length = 1;
  53. break;
  54. case 16:
  55. length = 2;
  56. break;
  57. case 32:
  58. length = 4;
  59. break;
  60. case 64:
  61. length = 8;
  62. break;
  63. default:
  64. ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %u",
  65. bit_width));
  66. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  67. }
  68. #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
  69. /*
  70. * Hardware does not support non-aligned data transfers, we must verify
  71. * the request.
  72. */
  73. (void)acpi_ut_short_divide((u64) address, length, NULL, &remainder);
  74. if (remainder != 0) {
  75. return_ACPI_STATUS(AE_AML_ALIGNMENT);
  76. }
  77. #endif
  78. /*
  79. * Does the request fit into the cached memory mapping?
  80. * Is 1) Address below the current mapping? OR
  81. * 2) Address beyond the current mapping?
  82. */
  83. if (!mm || (address < mm->physical_address) ||
  84. ((u64) address + length > (u64) mm->physical_address + mm->length)) {
  85. /*
  86. * The request cannot be resolved by the current memory mapping.
  87. *
  88. * Look for an existing saved mapping covering the address range
  89. * at hand. If found, save it as the current one and carry out
  90. * the access.
  91. */
  92. for (mm = mem_info->first_mm; mm; mm = mm->next_mm) {
  93. if (mm == mem_info->cur_mm)
  94. continue;
  95. if (address < mm->physical_address)
  96. continue;
  97. if ((u64) address + length >
  98. (u64) mm->physical_address + mm->length)
  99. continue;
  100. mem_info->cur_mm = mm;
  101. goto access;
  102. }
  103. /* Create a new mappings list entry */
  104. mm = ACPI_ALLOCATE_ZEROED(sizeof(*mm));
  105. if (!mm) {
  106. ACPI_ERROR((AE_INFO,
  107. "Unable to save memory mapping at 0x%8.8X%8.8X, size %u",
  108. ACPI_FORMAT_UINT64(address), length));
  109. return_ACPI_STATUS(AE_NO_MEMORY);
  110. }
  111. /*
  112. * October 2009: Attempt to map from the requested address to the
  113. * end of the region. However, we will never map more than one
  114. * page, nor will we cross a page boundary.
  115. */
  116. map_length = (acpi_size)
  117. ((mem_info->address + mem_info->length) - address);
  118. /*
  119. * If mapping the entire remaining portion of the region will cross
  120. * a page boundary, just map up to the page boundary, do not cross.
  121. * On some systems, crossing a page boundary while mapping regions
  122. * can cause warnings if the pages have different attributes
  123. * due to resource management.
  124. *
  125. * This has the added benefit of constraining a single mapping to
  126. * one page, which is similar to the original code that used a 4k
  127. * maximum window.
  128. */
  129. page_boundary_map_length = (acpi_size)
  130. (ACPI_ROUND_UP(address, ACPI_DEFAULT_PAGE_SIZE) - address);
  131. if (page_boundary_map_length == 0) {
  132. page_boundary_map_length = ACPI_DEFAULT_PAGE_SIZE;
  133. }
  134. if (map_length > page_boundary_map_length) {
  135. map_length = page_boundary_map_length;
  136. }
  137. /* Create a new mapping starting at the address given */
  138. logical_addr_ptr = acpi_os_map_memory(address, map_length);
  139. if (!logical_addr_ptr) {
  140. ACPI_ERROR((AE_INFO,
  141. "Could not map memory at 0x%8.8X%8.8X, size %u",
  142. ACPI_FORMAT_UINT64(address),
  143. (u32)map_length));
  144. ACPI_FREE(mm);
  145. return_ACPI_STATUS(AE_NO_MEMORY);
  146. }
  147. /* Save the physical address and mapping size */
  148. mm->logical_address = logical_addr_ptr;
  149. mm->physical_address = address;
  150. mm->length = map_length;
  151. /*
  152. * Add the new entry to the mappigs list and save it as the
  153. * current mapping.
  154. */
  155. mm->next_mm = mem_info->first_mm;
  156. mem_info->first_mm = mm;
  157. mem_info->cur_mm = mm;
  158. }
  159. access:
  160. /*
  161. * Generate a logical pointer corresponding to the address we want to
  162. * access
  163. */
  164. logical_addr_ptr = mm->logical_address +
  165. ((u64) address - (u64) mm->physical_address);
  166. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  167. "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
  168. bit_width, function, ACPI_FORMAT_UINT64(address)));
  169. /*
  170. * Perform the memory read or write
  171. *
  172. * Note: For machines that do not support non-aligned transfers, the target
  173. * address was checked for alignment above. We do not attempt to break the
  174. * transfer up into smaller (byte-size) chunks because the AML specifically
  175. * asked for a transfer width that the hardware may require.
  176. */
  177. switch (function) {
  178. case ACPI_READ:
  179. *value = 0;
  180. switch (bit_width) {
  181. case 8:
  182. *value = (u64)ACPI_GET8(logical_addr_ptr);
  183. break;
  184. case 16:
  185. *value = (u64)ACPI_GET16(logical_addr_ptr);
  186. break;
  187. case 32:
  188. *value = (u64)ACPI_GET32(logical_addr_ptr);
  189. break;
  190. case 64:
  191. *value = (u64)ACPI_GET64(logical_addr_ptr);
  192. break;
  193. default:
  194. /* bit_width was already validated */
  195. break;
  196. }
  197. break;
  198. case ACPI_WRITE:
  199. switch (bit_width) {
  200. case 8:
  201. ACPI_SET8(logical_addr_ptr, *value);
  202. break;
  203. case 16:
  204. ACPI_SET16(logical_addr_ptr, *value);
  205. break;
  206. case 32:
  207. ACPI_SET32(logical_addr_ptr, *value);
  208. break;
  209. case 64:
  210. ACPI_SET64(logical_addr_ptr, *value);
  211. break;
  212. default:
  213. /* bit_width was already validated */
  214. break;
  215. }
  216. break;
  217. default:
  218. status = AE_BAD_PARAMETER;
  219. break;
  220. }
  221. return_ACPI_STATUS(status);
  222. }
  223. /*******************************************************************************
  224. *
  225. * FUNCTION: acpi_ex_system_io_space_handler
  226. *
  227. * PARAMETERS: function - Read or Write operation
  228. * address - Where in the space to read or write
  229. * bit_width - Field width in bits (8, 16, or 32)
  230. * value - Pointer to in or out value
  231. * handler_context - Pointer to Handler's context
  232. * region_context - Pointer to context specific to the
  233. * accessed region
  234. *
  235. * RETURN: Status
  236. *
  237. * DESCRIPTION: Handler for the System IO address space (Op Region)
  238. *
  239. ******************************************************************************/
  240. acpi_status
  241. acpi_ex_system_io_space_handler(u32 function,
  242. acpi_physical_address address,
  243. u32 bit_width,
  244. u64 *value,
  245. void *handler_context, void *region_context)
  246. {
  247. acpi_status status = AE_OK;
  248. u32 value32;
  249. ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
  250. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  251. "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
  252. bit_width, function, ACPI_FORMAT_UINT64(address)));
  253. /* Decode the function parameter */
  254. switch (function) {
  255. case ACPI_READ:
  256. status = acpi_hw_read_port((acpi_io_address)address,
  257. &value32, bit_width);
  258. *value = value32;
  259. break;
  260. case ACPI_WRITE:
  261. status = acpi_hw_write_port((acpi_io_address)address,
  262. (u32)*value, bit_width);
  263. break;
  264. default:
  265. status = AE_BAD_PARAMETER;
  266. break;
  267. }
  268. return_ACPI_STATUS(status);
  269. }
  270. #ifdef ACPI_PCI_CONFIGURED
  271. /*******************************************************************************
  272. *
  273. * FUNCTION: acpi_ex_pci_config_space_handler
  274. *
  275. * PARAMETERS: function - Read or Write operation
  276. * address - Where in the space to read or write
  277. * bit_width - Field width in bits (8, 16, or 32)
  278. * value - Pointer to in or out value
  279. * handler_context - Pointer to Handler's context
  280. * region_context - Pointer to context specific to the
  281. * accessed region
  282. *
  283. * RETURN: Status
  284. *
  285. * DESCRIPTION: Handler for the PCI Config address space (Op Region)
  286. *
  287. ******************************************************************************/
  288. acpi_status
  289. acpi_ex_pci_config_space_handler(u32 function,
  290. acpi_physical_address address,
  291. u32 bit_width,
  292. u64 *value,
  293. void *handler_context, void *region_context)
  294. {
  295. acpi_status status = AE_OK;
  296. struct acpi_pci_id *pci_id;
  297. u16 pci_register;
  298. ACPI_FUNCTION_TRACE(ex_pci_config_space_handler);
  299. /*
  300. * The arguments to acpi_os(Read|Write)pci_configuration are:
  301. *
  302. * pci_segment is the PCI bus segment range 0-31
  303. * pci_bus is the PCI bus number range 0-255
  304. * pci_device is the PCI device number range 0-31
  305. * pci_function is the PCI device function number
  306. * pci_register is the Config space register range 0-255 bytes
  307. *
  308. * value - input value for write, output address for read
  309. *
  310. */
  311. pci_id = (struct acpi_pci_id *)region_context;
  312. pci_register = (u16) (u32) address;
  313. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  314. "Pci-Config %u (%u) Seg(%04x) Bus(%04x) "
  315. "Dev(%04x) Func(%04x) Reg(%04x)\n",
  316. function, bit_width, pci_id->segment, pci_id->bus,
  317. pci_id->device, pci_id->function, pci_register));
  318. switch (function) {
  319. case ACPI_READ:
  320. *value = 0;
  321. status =
  322. acpi_os_read_pci_configuration(pci_id, pci_register, value,
  323. bit_width);
  324. break;
  325. case ACPI_WRITE:
  326. status =
  327. acpi_os_write_pci_configuration(pci_id, pci_register,
  328. *value, bit_width);
  329. break;
  330. default:
  331. status = AE_BAD_PARAMETER;
  332. break;
  333. }
  334. return_ACPI_STATUS(status);
  335. }
  336. #endif
  337. /*******************************************************************************
  338. *
  339. * FUNCTION: acpi_ex_cmos_space_handler
  340. *
  341. * PARAMETERS: function - Read or Write operation
  342. * address - Where in the space to read or write
  343. * bit_width - Field width in bits (8, 16, or 32)
  344. * value - Pointer to in or out value
  345. * handler_context - Pointer to Handler's context
  346. * region_context - Pointer to context specific to the
  347. * accessed region
  348. *
  349. * RETURN: Status
  350. *
  351. * DESCRIPTION: Handler for the CMOS address space (Op Region)
  352. *
  353. ******************************************************************************/
  354. acpi_status
  355. acpi_ex_cmos_space_handler(u32 function,
  356. acpi_physical_address address,
  357. u32 bit_width,
  358. u64 *value,
  359. void *handler_context, void *region_context)
  360. {
  361. acpi_status status = AE_OK;
  362. ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
  363. return_ACPI_STATUS(status);
  364. }
  365. #ifdef ACPI_PCI_CONFIGURED
  366. /*******************************************************************************
  367. *
  368. * FUNCTION: acpi_ex_pci_bar_space_handler
  369. *
  370. * PARAMETERS: function - Read or Write operation
  371. * address - Where in the space to read or write
  372. * bit_width - Field width in bits (8, 16, or 32)
  373. * value - Pointer to in or out value
  374. * handler_context - Pointer to Handler's context
  375. * region_context - Pointer to context specific to the
  376. * accessed region
  377. *
  378. * RETURN: Status
  379. *
  380. * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
  381. *
  382. ******************************************************************************/
  383. acpi_status
  384. acpi_ex_pci_bar_space_handler(u32 function,
  385. acpi_physical_address address,
  386. u32 bit_width,
  387. u64 *value,
  388. void *handler_context, void *region_context)
  389. {
  390. acpi_status status = AE_OK;
  391. ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
  392. return_ACPI_STATUS(status);
  393. }
  394. #endif
  395. /*******************************************************************************
  396. *
  397. * FUNCTION: acpi_ex_data_table_space_handler
  398. *
  399. * PARAMETERS: function - Read or Write operation
  400. * address - Where in the space to read or write
  401. * bit_width - Field width in bits (8, 16, or 32)
  402. * value - Pointer to in or out value
  403. * handler_context - Pointer to Handler's context
  404. * region_context - Pointer to context specific to the
  405. * accessed region
  406. *
  407. * RETURN: Status
  408. *
  409. * DESCRIPTION: Handler for the Data Table address space (Op Region)
  410. *
  411. ******************************************************************************/
  412. acpi_status
  413. acpi_ex_data_table_space_handler(u32 function,
  414. acpi_physical_address address,
  415. u32 bit_width,
  416. u64 *value,
  417. void *handler_context, void *region_context)
  418. {
  419. struct acpi_data_table_space_context *mapping;
  420. char *pointer;
  421. ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
  422. mapping = (struct acpi_data_table_space_context *) region_context;
  423. pointer = ACPI_CAST_PTR(char, mapping->pointer) +
  424. (address - ACPI_PTR_TO_PHYSADDR(mapping->pointer));
  425. /*
  426. * Perform the memory read or write. The bit_width was already
  427. * validated.
  428. */
  429. switch (function) {
  430. case ACPI_READ:
  431. memcpy(ACPI_CAST_PTR(char, value), pointer,
  432. ACPI_DIV_8(bit_width));
  433. break;
  434. case ACPI_WRITE:
  435. memcpy(pointer, ACPI_CAST_PTR(char, value),
  436. ACPI_DIV_8(bit_width));
  437. break;
  438. default:
  439. return_ACPI_STATUS(AE_BAD_PARAMETER);
  440. }
  441. return_ACPI_STATUS(AE_OK);
  442. }