exregion.c 16 KB

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