exregion.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /******************************************************************************
  2. *
  3. * Module Name: exregion - ACPI default op_region (address space) handlers
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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 <acpi/acinterp.h>
  44. #define _COMPONENT ACPI_EXECUTER
  45. ACPI_MODULE_NAME("exregion")
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ex_system_memory_space_handler
  49. *
  50. * PARAMETERS: Function - Read or Write operation
  51. * Address - Where in the space to read or write
  52. * bit_width - Field width in bits (8, 16, or 32)
  53. * Value - Pointer to in or out value
  54. * handler_context - Pointer to Handler's context
  55. * region_context - Pointer to context specific to the
  56. * accessed region
  57. *
  58. * RETURN: Status
  59. *
  60. * DESCRIPTION: Handler for the System Memory address space (Op Region)
  61. *
  62. ******************************************************************************/
  63. acpi_status
  64. acpi_ex_system_memory_space_handler(u32 function,
  65. acpi_physical_address address,
  66. u32 bit_width,
  67. acpi_integer * value,
  68. void *handler_context, void *region_context)
  69. {
  70. acpi_status status = AE_OK;
  71. void *logical_addr_ptr = NULL;
  72. struct acpi_mem_space_context *mem_info = region_context;
  73. u32 length;
  74. acpi_size window_size;
  75. #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
  76. u32 remainder;
  77. #endif
  78. ACPI_FUNCTION_TRACE("ex_system_memory_space_handler");
  79. /* Validate and translate the bit width */
  80. switch (bit_width) {
  81. case 8:
  82. length = 1;
  83. break;
  84. case 16:
  85. length = 2;
  86. break;
  87. case 32:
  88. length = 4;
  89. break;
  90. case 64:
  91. length = 8;
  92. break;
  93. default:
  94. ACPI_REPORT_ERROR(("Invalid system_memory width %d\n",
  95. bit_width));
  96. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  97. }
  98. #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
  99. /*
  100. * Hardware does not support non-aligned data transfers, we must verify
  101. * the request.
  102. */
  103. (void)acpi_ut_short_divide((acpi_integer) address, length, NULL,
  104. &remainder);
  105. if (remainder != 0) {
  106. return_ACPI_STATUS(AE_AML_ALIGNMENT);
  107. }
  108. #endif
  109. /*
  110. * Does the request fit into the cached memory mapping?
  111. * Is 1) Address below the current mapping? OR
  112. * 2) Address beyond the current mapping?
  113. */
  114. if ((address < mem_info->mapped_physical_address) ||
  115. (((acpi_integer) address + length) > ((acpi_integer)
  116. mem_info->
  117. 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. * Don't attempt to map memory beyond the end of the region, and
  130. * constrain the maximum mapping size to something reasonable.
  131. */
  132. window_size = (acpi_size)
  133. ((mem_info->address + mem_info->length) - address);
  134. if (window_size > ACPI_SYSMEM_REGION_WINDOW_SIZE) {
  135. window_size = ACPI_SYSMEM_REGION_WINDOW_SIZE;
  136. }
  137. /* Create a new mapping starting at the address given */
  138. status = acpi_os_map_memory(address, window_size,
  139. (void **)&mem_info->
  140. mapped_logical_address);
  141. if (ACPI_FAILURE(status)) {
  142. ACPI_REPORT_ERROR(("Could not map memory at %8.8X%8.8X, size %X\n", ACPI_FORMAT_UINT64(address), (u32) window_size));
  143. mem_info->mapped_length = 0;
  144. return_ACPI_STATUS(status);
  145. }
  146. /* Save the physical address and mapping size */
  147. mem_info->mapped_physical_address = address;
  148. mem_info->mapped_length = window_size;
  149. }
  150. /*
  151. * Generate a logical pointer corresponding to the address we want to
  152. * access
  153. */
  154. logical_addr_ptr = mem_info->mapped_logical_address +
  155. ((acpi_integer) address -
  156. (acpi_integer) mem_info->mapped_physical_address);
  157. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  158. "system_memory %d (%d width) Address=%8.8X%8.8X\n",
  159. function, bit_width, ACPI_FORMAT_UINT64(address)));
  160. /*
  161. * Perform the memory read or write
  162. *
  163. * Note: For machines that do not support non-aligned transfers, the target
  164. * address was checked for alignment above. We do not attempt to break the
  165. * transfer up into smaller (byte-size) chunks because the AML specifically
  166. * asked for a transfer width that the hardware may require.
  167. */
  168. switch (function) {
  169. case ACPI_READ:
  170. *value = 0;
  171. switch (bit_width) {
  172. case 8:
  173. *value = (acpi_integer) ACPI_GET8(logical_addr_ptr);
  174. break;
  175. case 16:
  176. *value = (acpi_integer) ACPI_GET16(logical_addr_ptr);
  177. break;
  178. case 32:
  179. *value = (acpi_integer) ACPI_GET32(logical_addr_ptr);
  180. break;
  181. #if ACPI_MACHINE_WIDTH != 16
  182. case 64:
  183. *value = (acpi_integer) ACPI_GET64(logical_addr_ptr);
  184. break;
  185. #endif
  186. default:
  187. /* bit_width was already validated */
  188. break;
  189. }
  190. break;
  191. case ACPI_WRITE:
  192. switch (bit_width) {
  193. case 8:
  194. ACPI_SET8(logical_addr_ptr) = (u8) * value;
  195. break;
  196. case 16:
  197. ACPI_SET16(logical_addr_ptr) = (u16) * value;
  198. break;
  199. case 32:
  200. ACPI_SET32(logical_addr_ptr) = (u32) * value;
  201. break;
  202. #if ACPI_MACHINE_WIDTH != 16
  203. case 64:
  204. ACPI_SET64(logical_addr_ptr) = (u64) * value;
  205. break;
  206. #endif
  207. default:
  208. /* bit_width was already validated */
  209. break;
  210. }
  211. break;
  212. default:
  213. status = AE_BAD_PARAMETER;
  214. break;
  215. }
  216. return_ACPI_STATUS(status);
  217. }
  218. /*******************************************************************************
  219. *
  220. * FUNCTION: acpi_ex_system_io_space_handler
  221. *
  222. * PARAMETERS: Function - Read or Write operation
  223. * Address - Where in the space to read or write
  224. * bit_width - Field width in bits (8, 16, or 32)
  225. * Value - Pointer to in or out value
  226. * handler_context - Pointer to Handler's context
  227. * region_context - Pointer to context specific to the
  228. * accessed region
  229. *
  230. * RETURN: Status
  231. *
  232. * DESCRIPTION: Handler for the System IO address space (Op Region)
  233. *
  234. ******************************************************************************/
  235. acpi_status
  236. acpi_ex_system_io_space_handler(u32 function,
  237. acpi_physical_address address,
  238. u32 bit_width,
  239. acpi_integer * value,
  240. void *handler_context, void *region_context)
  241. {
  242. acpi_status status = AE_OK;
  243. u32 value32;
  244. ACPI_FUNCTION_TRACE("ex_system_io_space_handler");
  245. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  246. "system_iO %d (%d width) Address=%8.8X%8.8X\n",
  247. function, bit_width, ACPI_FORMAT_UINT64(address)));
  248. /* Decode the function parameter */
  249. switch (function) {
  250. case ACPI_READ:
  251. status = acpi_os_read_port((acpi_io_address) address,
  252. &value32, bit_width);
  253. *value = value32;
  254. break;
  255. case ACPI_WRITE:
  256. status = acpi_os_write_port((acpi_io_address) address,
  257. (u32) * value, bit_width);
  258. break;
  259. default:
  260. status = AE_BAD_PARAMETER;
  261. break;
  262. }
  263. return_ACPI_STATUS(status);
  264. }
  265. /*******************************************************************************
  266. *
  267. * FUNCTION: acpi_ex_pci_config_space_handler
  268. *
  269. * PARAMETERS: Function - Read or Write operation
  270. * Address - Where in the space to read or write
  271. * bit_width - Field width in bits (8, 16, or 32)
  272. * Value - Pointer to in or out value
  273. * handler_context - Pointer to Handler's context
  274. * region_context - Pointer to context specific to the
  275. * accessed region
  276. *
  277. * RETURN: Status
  278. *
  279. * DESCRIPTION: Handler for the PCI Config address space (Op Region)
  280. *
  281. ******************************************************************************/
  282. acpi_status
  283. acpi_ex_pci_config_space_handler(u32 function,
  284. acpi_physical_address address,
  285. u32 bit_width,
  286. acpi_integer * value,
  287. void *handler_context, void *region_context)
  288. {
  289. acpi_status status = AE_OK;
  290. struct acpi_pci_id *pci_id;
  291. u16 pci_register;
  292. ACPI_FUNCTION_TRACE("ex_pci_config_space_handler");
  293. /*
  294. * The arguments to acpi_os(Read|Write)pci_configuration are:
  295. *
  296. * pci_segment is the PCI bus segment range 0-31
  297. * pci_bus is the PCI bus number range 0-255
  298. * pci_device is the PCI device number range 0-31
  299. * pci_function is the PCI device function number
  300. * pci_register is the Config space register range 0-255 bytes
  301. *
  302. * Value - input value for write, output address for read
  303. *
  304. */
  305. pci_id = (struct acpi_pci_id *)region_context;
  306. pci_register = (u16) (u32) address;
  307. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  308. "pci_config %d (%d) Seg(%04x) Bus(%04x) Dev(%04x) Func(%04x) Reg(%04x)\n",
  309. function, bit_width, pci_id->segment, pci_id->bus,
  310. pci_id->device, pci_id->function, pci_register));
  311. switch (function) {
  312. case ACPI_READ:
  313. *value = 0;
  314. status = acpi_os_read_pci_configuration(pci_id, pci_register,
  315. value, bit_width);
  316. break;
  317. case ACPI_WRITE:
  318. status = acpi_os_write_pci_configuration(pci_id, pci_register,
  319. *value, bit_width);
  320. break;
  321. default:
  322. status = AE_BAD_PARAMETER;
  323. break;
  324. }
  325. return_ACPI_STATUS(status);
  326. }
  327. /*******************************************************************************
  328. *
  329. * FUNCTION: acpi_ex_cmos_space_handler
  330. *
  331. * PARAMETERS: Function - Read or Write operation
  332. * Address - Where in the space to read or write
  333. * bit_width - Field width in bits (8, 16, or 32)
  334. * Value - Pointer to in or out value
  335. * handler_context - Pointer to Handler's context
  336. * region_context - Pointer to context specific to the
  337. * accessed region
  338. *
  339. * RETURN: Status
  340. *
  341. * DESCRIPTION: Handler for the CMOS address space (Op Region)
  342. *
  343. ******************************************************************************/
  344. acpi_status
  345. acpi_ex_cmos_space_handler(u32 function,
  346. acpi_physical_address address,
  347. u32 bit_width,
  348. acpi_integer * value,
  349. void *handler_context, void *region_context)
  350. {
  351. acpi_status status = AE_OK;
  352. ACPI_FUNCTION_TRACE("ex_cmos_space_handler");
  353. return_ACPI_STATUS(status);
  354. }
  355. /*******************************************************************************
  356. *
  357. * FUNCTION: acpi_ex_pci_bar_space_handler
  358. *
  359. * PARAMETERS: Function - Read or Write operation
  360. * Address - Where in the space to read or write
  361. * bit_width - Field width in bits (8, 16, or 32)
  362. * Value - Pointer to in or out value
  363. * handler_context - Pointer to Handler's context
  364. * region_context - Pointer to context specific to the
  365. * accessed region
  366. *
  367. * RETURN: Status
  368. *
  369. * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
  370. *
  371. ******************************************************************************/
  372. acpi_status
  373. acpi_ex_pci_bar_space_handler(u32 function,
  374. acpi_physical_address address,
  375. u32 bit_width,
  376. acpi_integer * value,
  377. void *handler_context, void *region_context)
  378. {
  379. acpi_status status = AE_OK;
  380. ACPI_FUNCTION_TRACE("ex_pci_bar_space_handler");
  381. return_ACPI_STATUS(status);
  382. }
  383. /*******************************************************************************
  384. *
  385. * FUNCTION: acpi_ex_data_table_space_handler
  386. *
  387. * PARAMETERS: Function - Read or Write operation
  388. * Address - Where in the space to read or write
  389. * bit_width - Field width in bits (8, 16, or 32)
  390. * Value - Pointer to in or out value
  391. * handler_context - Pointer to Handler's context
  392. * region_context - Pointer to context specific to the
  393. * accessed region
  394. *
  395. * RETURN: Status
  396. *
  397. * DESCRIPTION: Handler for the Data Table address space (Op Region)
  398. *
  399. ******************************************************************************/
  400. acpi_status
  401. acpi_ex_data_table_space_handler(u32 function,
  402. acpi_physical_address address,
  403. u32 bit_width,
  404. acpi_integer * value,
  405. void *handler_context, void *region_context)
  406. {
  407. acpi_status status = AE_OK;
  408. u32 byte_width = ACPI_DIV_8(bit_width);
  409. u32 i;
  410. char *logical_addr_ptr;
  411. ACPI_FUNCTION_TRACE("ex_data_table_space_handler");
  412. logical_addr_ptr = ACPI_PHYSADDR_TO_PTR(address);
  413. /* Perform the memory read or write */
  414. switch (function) {
  415. case ACPI_READ:
  416. for (i = 0; i < byte_width; i++) {
  417. ((char *)value)[i] = logical_addr_ptr[i];
  418. }
  419. break;
  420. case ACPI_WRITE:
  421. default:
  422. return_ACPI_STATUS(AE_SUPPORT);
  423. }
  424. return_ACPI_STATUS(status);
  425. }