exregion.c 15 KB

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