hwxface.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: hwxface - Public ACPICA hardware interfaces
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #define EXPORT_ACPI_INTERFACES
  10. #include <acpi/acpi.h>
  11. #include "accommon.h"
  12. #include "acnamesp.h"
  13. #define _COMPONENT ACPI_HARDWARE
  14. ACPI_MODULE_NAME("hwxface")
  15. /******************************************************************************
  16. *
  17. * FUNCTION: acpi_reset
  18. *
  19. * PARAMETERS: None
  20. *
  21. * RETURN: Status
  22. *
  23. * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
  24. * support reset register in PCI config space, this must be
  25. * handled separately.
  26. *
  27. ******************************************************************************/
  28. acpi_status acpi_reset(void)
  29. {
  30. struct acpi_generic_address *reset_reg;
  31. acpi_status status;
  32. ACPI_FUNCTION_TRACE(acpi_reset);
  33. reset_reg = &acpi_gbl_FADT.reset_register;
  34. /* Check if the reset register is supported */
  35. if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) ||
  36. !reset_reg->address) {
  37. return_ACPI_STATUS(AE_NOT_EXIST);
  38. }
  39. if (reset_reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  40. /*
  41. * For I/O space, write directly to the OSL. This bypasses the port
  42. * validation mechanism, which may block a valid write to the reset
  43. * register.
  44. *
  45. * NOTE:
  46. * The ACPI spec requires the reset register width to be 8, so we
  47. * hardcode it here and ignore the FADT value. This maintains
  48. * compatibility with other ACPI implementations that have allowed
  49. * BIOS code with bad register width values to go unnoticed.
  50. */
  51. status = acpi_os_write_port((acpi_io_address)reset_reg->address,
  52. acpi_gbl_FADT.reset_value,
  53. ACPI_RESET_REGISTER_WIDTH);
  54. } else {
  55. /* Write the reset value to the reset register */
  56. status = acpi_hw_write(acpi_gbl_FADT.reset_value, reset_reg);
  57. }
  58. return_ACPI_STATUS(status);
  59. }
  60. ACPI_EXPORT_SYMBOL(acpi_reset)
  61. /******************************************************************************
  62. *
  63. * FUNCTION: acpi_read
  64. *
  65. * PARAMETERS: value - Where the value is returned
  66. * reg - GAS register structure
  67. *
  68. * RETURN: Status
  69. *
  70. * DESCRIPTION: Read from either memory or IO space.
  71. *
  72. * LIMITATIONS: <These limitations also apply to acpi_write>
  73. * bit_width must be exactly 8, 16, 32, or 64.
  74. * space_ID must be system_memory or system_IO.
  75. * bit_offset and access_width are currently ignored, as there has
  76. * not been a need to implement these.
  77. *
  78. ******************************************************************************/
  79. acpi_status acpi_read(u64 *return_value, struct acpi_generic_address *reg)
  80. {
  81. acpi_status status;
  82. ACPI_FUNCTION_NAME(acpi_read);
  83. status = acpi_hw_read(return_value, reg);
  84. return (status);
  85. }
  86. ACPI_EXPORT_SYMBOL(acpi_read)
  87. /******************************************************************************
  88. *
  89. * FUNCTION: acpi_write
  90. *
  91. * PARAMETERS: value - Value to be written
  92. * reg - GAS register structure
  93. *
  94. * RETURN: Status
  95. *
  96. * DESCRIPTION: Write to either memory or IO space.
  97. *
  98. ******************************************************************************/
  99. acpi_status acpi_write(u64 value, struct acpi_generic_address *reg)
  100. {
  101. acpi_status status;
  102. ACPI_FUNCTION_NAME(acpi_write);
  103. status = acpi_hw_write(value, reg);
  104. return (status);
  105. }
  106. ACPI_EXPORT_SYMBOL(acpi_write)
  107. #if (!ACPI_REDUCED_HARDWARE)
  108. /*******************************************************************************
  109. *
  110. * FUNCTION: acpi_read_bit_register
  111. *
  112. * PARAMETERS: register_id - ID of ACPI Bit Register to access
  113. * return_value - Value that was read from the register,
  114. * normalized to bit position zero.
  115. *
  116. * RETURN: Status and the value read from the specified Register. Value
  117. * returned is normalized to bit0 (is shifted all the way right)
  118. *
  119. * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
  120. *
  121. * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
  122. * PM2 Control.
  123. *
  124. * Note: The hardware lock is not required when reading the ACPI bit registers
  125. * since almost all of them are single bit and it does not matter that
  126. * the parent hardware register can be split across two physical
  127. * registers. The only multi-bit field is SLP_TYP in the PM1 control
  128. * register, but this field does not cross an 8-bit boundary (nor does
  129. * it make much sense to actually read this field.)
  130. *
  131. ******************************************************************************/
  132. acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
  133. {
  134. struct acpi_bit_register_info *bit_reg_info;
  135. u32 register_value;
  136. u32 value;
  137. acpi_status status;
  138. ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
  139. /* Get the info structure corresponding to the requested ACPI Register */
  140. bit_reg_info = acpi_hw_get_bit_register_info(register_id);
  141. if (!bit_reg_info) {
  142. return_ACPI_STATUS(AE_BAD_PARAMETER);
  143. }
  144. /* Read the entire parent register */
  145. status = acpi_hw_register_read(bit_reg_info->parent_register,
  146. &register_value);
  147. if (ACPI_FAILURE(status)) {
  148. return_ACPI_STATUS(status);
  149. }
  150. /* Normalize the value that was read, mask off other bits */
  151. value = ((register_value & bit_reg_info->access_bit_mask)
  152. >> bit_reg_info->bit_position);
  153. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  154. "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
  155. register_id, bit_reg_info->parent_register,
  156. register_value, value));
  157. *return_value = value;
  158. return_ACPI_STATUS(AE_OK);
  159. }
  160. ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
  161. /*******************************************************************************
  162. *
  163. * FUNCTION: acpi_write_bit_register
  164. *
  165. * PARAMETERS: register_id - ID of ACPI Bit Register to access
  166. * value - Value to write to the register, in bit
  167. * position zero. The bit is automatically
  168. * shifted to the correct position.
  169. *
  170. * RETURN: Status
  171. *
  172. * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
  173. * since most operations require a read/modify/write sequence.
  174. *
  175. * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
  176. * PM2 Control.
  177. *
  178. * Note that at this level, the fact that there may be actually two
  179. * hardware registers (A and B - and B may not exist) is abstracted.
  180. *
  181. ******************************************************************************/
  182. acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  183. {
  184. struct acpi_bit_register_info *bit_reg_info;
  185. acpi_cpu_flags lock_flags;
  186. u32 register_value;
  187. acpi_status status = AE_OK;
  188. ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
  189. /* Get the info structure corresponding to the requested ACPI Register */
  190. bit_reg_info = acpi_hw_get_bit_register_info(register_id);
  191. if (!bit_reg_info) {
  192. return_ACPI_STATUS(AE_BAD_PARAMETER);
  193. }
  194. lock_flags = acpi_os_acquire_raw_lock(acpi_gbl_hardware_lock);
  195. /*
  196. * At this point, we know that the parent register is one of the
  197. * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
  198. */
  199. if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
  200. /*
  201. * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
  202. *
  203. * Perform a register read to preserve the bits that we are not
  204. * interested in
  205. */
  206. status = acpi_hw_register_read(bit_reg_info->parent_register,
  207. &register_value);
  208. if (ACPI_FAILURE(status)) {
  209. goto unlock_and_exit;
  210. }
  211. /*
  212. * Insert the input bit into the value that was just read
  213. * and write the register
  214. */
  215. ACPI_REGISTER_INSERT_VALUE(register_value,
  216. bit_reg_info->bit_position,
  217. bit_reg_info->access_bit_mask,
  218. value);
  219. status = acpi_hw_register_write(bit_reg_info->parent_register,
  220. register_value);
  221. } else {
  222. /*
  223. * 2) Case for PM1 Status
  224. *
  225. * The Status register is different from the rest. Clear an event
  226. * by writing 1, writing 0 has no effect. So, the only relevant
  227. * information is the single bit we're interested in, all others
  228. * should be written as 0 so they will be left unchanged.
  229. */
  230. register_value = ACPI_REGISTER_PREPARE_BITS(value,
  231. bit_reg_info->
  232. bit_position,
  233. bit_reg_info->
  234. access_bit_mask);
  235. /* No need to write the register if value is all zeros */
  236. if (register_value) {
  237. status =
  238. acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  239. register_value);
  240. }
  241. }
  242. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  243. "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
  244. register_id, bit_reg_info->parent_register, value,
  245. register_value));
  246. unlock_and_exit:
  247. acpi_os_release_raw_lock(acpi_gbl_hardware_lock, lock_flags);
  248. return_ACPI_STATUS(status);
  249. }
  250. ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
  251. #endif /* !ACPI_REDUCED_HARDWARE */
  252. /*******************************************************************************
  253. *
  254. * FUNCTION: acpi_get_sleep_type_data
  255. *
  256. * PARAMETERS: sleep_state - Numeric sleep state
  257. * *sleep_type_a - Where SLP_TYPa is returned
  258. * *sleep_type_b - Where SLP_TYPb is returned
  259. *
  260. * RETURN: Status
  261. *
  262. * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested
  263. * sleep state via the appropriate \_Sx object.
  264. *
  265. * The sleep state package returned from the corresponding \_Sx_ object
  266. * must contain at least one integer.
  267. *
  268. * March 2005:
  269. * Added support for a package that contains two integers. This
  270. * goes against the ACPI specification which defines this object as a
  271. * package with one encoded DWORD integer. However, existing practice
  272. * by many BIOS vendors is to return a package with 2 or more integer
  273. * elements, at least one per sleep type (A/B).
  274. *
  275. * January 2013:
  276. * Therefore, we must be prepared to accept a package with either a
  277. * single integer or multiple integers.
  278. *
  279. * The single integer DWORD format is as follows:
  280. * BYTE 0 - Value for the PM1A SLP_TYP register
  281. * BYTE 1 - Value for the PM1B SLP_TYP register
  282. * BYTE 2-3 - Reserved
  283. *
  284. * The dual integer format is as follows:
  285. * Integer 0 - Value for the PM1A SLP_TYP register
  286. * Integer 1 - Value for the PM1A SLP_TYP register
  287. *
  288. ******************************************************************************/
  289. acpi_status
  290. acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
  291. {
  292. acpi_status status;
  293. struct acpi_evaluate_info *info;
  294. union acpi_operand_object **elements;
  295. ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
  296. /* Validate parameters */
  297. if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
  298. return_ACPI_STATUS(AE_BAD_PARAMETER);
  299. }
  300. /* Allocate the evaluation information block */
  301. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  302. if (!info) {
  303. return_ACPI_STATUS(AE_NO_MEMORY);
  304. }
  305. /*
  306. * Evaluate the \_Sx namespace object containing the register values
  307. * for this state
  308. */
  309. info->relative_pathname = acpi_gbl_sleep_state_names[sleep_state];
  310. status = acpi_ns_evaluate(info);
  311. if (ACPI_FAILURE(status)) {
  312. if (status == AE_NOT_FOUND) {
  313. /* The _Sx states are optional, ignore NOT_FOUND */
  314. goto final_cleanup;
  315. }
  316. goto warning_cleanup;
  317. }
  318. /* Must have a return object */
  319. if (!info->return_object) {
  320. ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
  321. info->relative_pathname));
  322. status = AE_AML_NO_RETURN_VALUE;
  323. goto warning_cleanup;
  324. }
  325. /* Return object must be of type Package */
  326. if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
  327. ACPI_ERROR((AE_INFO,
  328. "Sleep State return object is not a Package"));
  329. status = AE_AML_OPERAND_TYPE;
  330. goto return_value_cleanup;
  331. }
  332. /*
  333. * Any warnings about the package length or the object types have
  334. * already been issued by the predefined name module -- there is no
  335. * need to repeat them here.
  336. */
  337. elements = info->return_object->package.elements;
  338. switch (info->return_object->package.count) {
  339. case 0:
  340. status = AE_AML_PACKAGE_LIMIT;
  341. break;
  342. case 1:
  343. if (elements[0]->common.type != ACPI_TYPE_INTEGER) {
  344. status = AE_AML_OPERAND_TYPE;
  345. break;
  346. }
  347. /* A valid _Sx_ package with one integer */
  348. *sleep_type_a = (u8)elements[0]->integer.value;
  349. *sleep_type_b = (u8)(elements[0]->integer.value >> 8);
  350. break;
  351. case 2:
  352. default:
  353. if ((elements[0]->common.type != ACPI_TYPE_INTEGER) ||
  354. (elements[1]->common.type != ACPI_TYPE_INTEGER)) {
  355. status = AE_AML_OPERAND_TYPE;
  356. break;
  357. }
  358. /* A valid _Sx_ package with two integers */
  359. *sleep_type_a = (u8)elements[0]->integer.value;
  360. *sleep_type_b = (u8)elements[1]->integer.value;
  361. break;
  362. }
  363. return_value_cleanup:
  364. acpi_ut_remove_reference(info->return_object);
  365. warning_cleanup:
  366. if (ACPI_FAILURE(status)) {
  367. ACPI_EXCEPTION((AE_INFO, status,
  368. "While evaluating Sleep State [%s]",
  369. info->relative_pathname));
  370. }
  371. final_cleanup:
  372. ACPI_FREE(info);
  373. return_ACPI_STATUS(status);
  374. }
  375. ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)