hwxface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /******************************************************************************
  2. *
  3. * Module Name: hwxface - Public ACPICA hardware interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, 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 "acnamesp.h"
  45. #define _COMPONENT ACPI_HARDWARE
  46. ACPI_MODULE_NAME("hwxface")
  47. /******************************************************************************
  48. *
  49. * FUNCTION: acpi_reset
  50. *
  51. * PARAMETERS: None
  52. *
  53. * RETURN: Status
  54. *
  55. * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
  56. * support reset register in PCI config space, this must be
  57. * handled separately.
  58. *
  59. ******************************************************************************/
  60. acpi_status acpi_reset(void)
  61. {
  62. struct acpi_generic_address *reset_reg;
  63. acpi_status status;
  64. ACPI_FUNCTION_TRACE(acpi_reset);
  65. reset_reg = &acpi_gbl_FADT.reset_register;
  66. /* Check if the reset register is supported */
  67. if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) ||
  68. !reset_reg->address) {
  69. return_ACPI_STATUS(AE_NOT_EXIST);
  70. }
  71. /* Write the reset value to the reset register */
  72. status = acpi_write(acpi_gbl_FADT.reset_value, reset_reg);
  73. return_ACPI_STATUS(status);
  74. }
  75. ACPI_EXPORT_SYMBOL(acpi_reset)
  76. /******************************************************************************
  77. *
  78. * FUNCTION: acpi_read
  79. *
  80. * PARAMETERS: Value - Where the value is returned
  81. * Reg - GAS register structure
  82. *
  83. * RETURN: Status
  84. *
  85. * DESCRIPTION: Read from either memory or IO space.
  86. *
  87. ******************************************************************************/
  88. acpi_status acpi_read(u32 *value, struct acpi_generic_address *reg)
  89. {
  90. u32 width;
  91. u64 address;
  92. acpi_status status;
  93. ACPI_FUNCTION_NAME(acpi_read);
  94. /*
  95. * Must have a valid pointer to a GAS structure, and a non-zero address
  96. * within.
  97. */
  98. if (!reg) {
  99. return (AE_BAD_PARAMETER);
  100. }
  101. /* Get a local copy of the address. Handles possible alignment issues */
  102. ACPI_MOVE_64_TO_64(&address, &reg->address);
  103. if (!address) {
  104. return (AE_BAD_ADDRESS);
  105. }
  106. /* Supported widths are 8/16/32 */
  107. width = reg->bit_width;
  108. if ((width != 8) && (width != 16) && (width != 32)) {
  109. return (AE_SUPPORT);
  110. }
  111. /* Initialize entire 32-bit return value to zero */
  112. *value = 0;
  113. /*
  114. * Two address spaces supported: Memory or IO. PCI_Config is
  115. * not supported here because the GAS structure is insufficient
  116. */
  117. switch (reg->space_id) {
  118. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  119. status = acpi_os_read_memory((acpi_physical_address) address,
  120. value, width);
  121. break;
  122. case ACPI_ADR_SPACE_SYSTEM_IO:
  123. status =
  124. acpi_hw_read_port((acpi_io_address) address, value, width);
  125. break;
  126. default:
  127. ACPI_ERROR((AE_INFO,
  128. "Unsupported address space: %X", reg->space_id));
  129. return (AE_BAD_PARAMETER);
  130. }
  131. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  132. "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
  133. *value, width, ACPI_FORMAT_UINT64(address),
  134. acpi_ut_get_region_name(reg->space_id)));
  135. return (status);
  136. }
  137. ACPI_EXPORT_SYMBOL(acpi_read)
  138. /******************************************************************************
  139. *
  140. * FUNCTION: acpi_write
  141. *
  142. * PARAMETERS: Value - To be written
  143. * Reg - GAS register structure
  144. *
  145. * RETURN: Status
  146. *
  147. * DESCRIPTION: Write to either memory or IO space.
  148. *
  149. ******************************************************************************/
  150. acpi_status acpi_write(u32 value, struct acpi_generic_address *reg)
  151. {
  152. u32 width;
  153. u64 address;
  154. acpi_status status;
  155. ACPI_FUNCTION_NAME(acpi_write);
  156. /*
  157. * Must have a valid pointer to a GAS structure, and a non-zero address
  158. * within.
  159. */
  160. if (!reg) {
  161. return (AE_BAD_PARAMETER);
  162. }
  163. /* Get a local copy of the address. Handles possible alignment issues */
  164. ACPI_MOVE_64_TO_64(&address, &reg->address);
  165. if (!address) {
  166. return (AE_BAD_ADDRESS);
  167. }
  168. /* Supported widths are 8/16/32 */
  169. width = reg->bit_width;
  170. if ((width != 8) && (width != 16) && (width != 32)) {
  171. return (AE_SUPPORT);
  172. }
  173. /*
  174. * Two address spaces supported: Memory or IO.
  175. * PCI_Config is not supported here because the GAS struct is insufficient
  176. */
  177. switch (reg->space_id) {
  178. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  179. status = acpi_os_write_memory((acpi_physical_address) address,
  180. value, width);
  181. break;
  182. case ACPI_ADR_SPACE_SYSTEM_IO:
  183. status = acpi_hw_write_port((acpi_io_address) address, value,
  184. width);
  185. break;
  186. default:
  187. ACPI_ERROR((AE_INFO,
  188. "Unsupported address space: %X", reg->space_id));
  189. return (AE_BAD_PARAMETER);
  190. }
  191. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  192. "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
  193. value, width, ACPI_FORMAT_UINT64(address),
  194. acpi_ut_get_region_name(reg->space_id)));
  195. return (status);
  196. }
  197. ACPI_EXPORT_SYMBOL(acpi_write)
  198. /*******************************************************************************
  199. *
  200. * FUNCTION: acpi_read_bit_register
  201. *
  202. * PARAMETERS: register_id - ID of ACPI Bit Register to access
  203. * return_value - Value that was read from the register,
  204. * normalized to bit position zero.
  205. *
  206. * RETURN: Status and the value read from the specified Register. Value
  207. * returned is normalized to bit0 (is shifted all the way right)
  208. *
  209. * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
  210. *
  211. * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
  212. * PM2 Control.
  213. *
  214. * Note: The hardware lock is not required when reading the ACPI bit registers
  215. * since almost all of them are single bit and it does not matter that
  216. * the parent hardware register can be split across two physical
  217. * registers. The only multi-bit field is SLP_TYP in the PM1 control
  218. * register, but this field does not cross an 8-bit boundary (nor does
  219. * it make much sense to actually read this field.)
  220. *
  221. ******************************************************************************/
  222. acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
  223. {
  224. struct acpi_bit_register_info *bit_reg_info;
  225. u32 register_value;
  226. u32 value;
  227. acpi_status status;
  228. ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
  229. /* Get the info structure corresponding to the requested ACPI Register */
  230. bit_reg_info = acpi_hw_get_bit_register_info(register_id);
  231. if (!bit_reg_info) {
  232. return_ACPI_STATUS(AE_BAD_PARAMETER);
  233. }
  234. /* Read the entire parent register */
  235. status = acpi_hw_register_read(bit_reg_info->parent_register,
  236. &register_value);
  237. if (ACPI_FAILURE(status)) {
  238. return_ACPI_STATUS(status);
  239. }
  240. /* Normalize the value that was read, mask off other bits */
  241. value = ((register_value & bit_reg_info->access_bit_mask)
  242. >> bit_reg_info->bit_position);
  243. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  244. "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
  245. register_id, bit_reg_info->parent_register,
  246. register_value, value));
  247. *return_value = value;
  248. return_ACPI_STATUS(AE_OK);
  249. }
  250. ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
  251. /*******************************************************************************
  252. *
  253. * FUNCTION: acpi_write_bit_register
  254. *
  255. * PARAMETERS: register_id - ID of ACPI Bit Register to access
  256. * Value - Value to write to the register, in bit
  257. * position zero. The bit is automaticallly
  258. * shifted to the correct position.
  259. *
  260. * RETURN: Status
  261. *
  262. * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
  263. * since most operations require a read/modify/write sequence.
  264. *
  265. * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
  266. * PM2 Control.
  267. *
  268. * Note that at this level, the fact that there may be actually two
  269. * hardware registers (A and B - and B may not exist) is abstracted.
  270. *
  271. ******************************************************************************/
  272. acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  273. {
  274. struct acpi_bit_register_info *bit_reg_info;
  275. acpi_cpu_flags lock_flags;
  276. u32 register_value;
  277. acpi_status status = AE_OK;
  278. ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
  279. /* Get the info structure corresponding to the requested ACPI Register */
  280. bit_reg_info = acpi_hw_get_bit_register_info(register_id);
  281. if (!bit_reg_info) {
  282. return_ACPI_STATUS(AE_BAD_PARAMETER);
  283. }
  284. lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  285. /*
  286. * At this point, we know that the parent register is one of the
  287. * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
  288. */
  289. if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
  290. /*
  291. * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
  292. *
  293. * Perform a register read to preserve the bits that we are not
  294. * interested in
  295. */
  296. status = acpi_hw_register_read(bit_reg_info->parent_register,
  297. &register_value);
  298. if (ACPI_FAILURE(status)) {
  299. goto unlock_and_exit;
  300. }
  301. /*
  302. * Insert the input bit into the value that was just read
  303. * and write the register
  304. */
  305. ACPI_REGISTER_INSERT_VALUE(register_value,
  306. bit_reg_info->bit_position,
  307. bit_reg_info->access_bit_mask,
  308. value);
  309. status = acpi_hw_register_write(bit_reg_info->parent_register,
  310. register_value);
  311. } else {
  312. /*
  313. * 2) Case for PM1 Status
  314. *
  315. * The Status register is different from the rest. Clear an event
  316. * by writing 1, writing 0 has no effect. So, the only relevant
  317. * information is the single bit we're interested in, all others
  318. * should be written as 0 so they will be left unchanged.
  319. */
  320. register_value = ACPI_REGISTER_PREPARE_BITS(value,
  321. bit_reg_info->
  322. bit_position,
  323. bit_reg_info->
  324. access_bit_mask);
  325. /* No need to write the register if value is all zeros */
  326. if (register_value) {
  327. status =
  328. acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  329. register_value);
  330. }
  331. }
  332. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  333. "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
  334. register_id, bit_reg_info->parent_register, value,
  335. register_value));
  336. unlock_and_exit:
  337. acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  338. return_ACPI_STATUS(status);
  339. }
  340. ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
  341. /*******************************************************************************
  342. *
  343. * FUNCTION: acpi_get_sleep_type_data
  344. *
  345. * PARAMETERS: sleep_state - Numeric sleep state
  346. * *sleep_type_a - Where SLP_TYPa is returned
  347. * *sleep_type_b - Where SLP_TYPb is returned
  348. *
  349. * RETURN: Status - ACPI status
  350. *
  351. * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested sleep
  352. * state.
  353. *
  354. ******************************************************************************/
  355. acpi_status
  356. acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
  357. {
  358. acpi_status status = AE_OK;
  359. struct acpi_evaluate_info *info;
  360. ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
  361. /* Validate parameters */
  362. if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
  363. return_ACPI_STATUS(AE_BAD_PARAMETER);
  364. }
  365. /* Allocate the evaluation information block */
  366. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  367. if (!info) {
  368. return_ACPI_STATUS(AE_NO_MEMORY);
  369. }
  370. info->pathname =
  371. ACPI_CAST_PTR(char, acpi_gbl_sleep_state_names[sleep_state]);
  372. /* Evaluate the namespace object containing the values for this state */
  373. status = acpi_ns_evaluate(info);
  374. if (ACPI_FAILURE(status)) {
  375. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  376. "%s while evaluating SleepState [%s]\n",
  377. acpi_format_exception(status),
  378. info->pathname));
  379. goto cleanup;
  380. }
  381. /* Must have a return object */
  382. if (!info->return_object) {
  383. ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
  384. info->pathname));
  385. status = AE_NOT_EXIST;
  386. }
  387. /* It must be of type Package */
  388. else if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
  389. ACPI_ERROR((AE_INFO,
  390. "Sleep State return object is not a Package"));
  391. status = AE_AML_OPERAND_TYPE;
  392. }
  393. /*
  394. * The package must have at least two elements. NOTE (March 2005): This
  395. * goes against the current ACPI spec which defines this object as a
  396. * package with one encoded DWORD element. However, existing practice
  397. * by BIOS vendors seems to be to have 2 or more elements, at least
  398. * one per sleep type (A/B).
  399. */
  400. else if (info->return_object->package.count < 2) {
  401. ACPI_ERROR((AE_INFO,
  402. "Sleep State return package does not have at least two elements"));
  403. status = AE_AML_NO_OPERAND;
  404. }
  405. /* The first two elements must both be of type Integer */
  406. else if (((info->return_object->package.elements[0])->common.type
  407. != ACPI_TYPE_INTEGER) ||
  408. ((info->return_object->package.elements[1])->common.type
  409. != ACPI_TYPE_INTEGER)) {
  410. ACPI_ERROR((AE_INFO,
  411. "Sleep State return package elements are not both Integers "
  412. "(%s, %s)",
  413. acpi_ut_get_object_type_name(info->return_object->
  414. package.elements[0]),
  415. acpi_ut_get_object_type_name(info->return_object->
  416. package.elements[1])));
  417. status = AE_AML_OPERAND_TYPE;
  418. } else {
  419. /* Valid _Sx_ package size, type, and value */
  420. *sleep_type_a = (u8)
  421. (info->return_object->package.elements[0])->integer.value;
  422. *sleep_type_b = (u8)
  423. (info->return_object->package.elements[1])->integer.value;
  424. }
  425. if (ACPI_FAILURE(status)) {
  426. ACPI_EXCEPTION((AE_INFO, status,
  427. "While evaluating SleepState [%s], bad Sleep object %p type %s",
  428. info->pathname, info->return_object,
  429. acpi_ut_get_object_type_name(info->
  430. return_object)));
  431. }
  432. acpi_ut_remove_reference(info->return_object);
  433. cleanup:
  434. ACPI_FREE(info);
  435. return_ACPI_STATUS(status);
  436. }
  437. ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)