exregion.c 15 KB

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