hwxface.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /******************************************************************************
  2. *
  3. * Module Name: hwxface - Public ACPICA hardware interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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. #define EXPORT_ACPI_INTERFACES
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #define _COMPONENT ACPI_HARDWARE
  47. ACPI_MODULE_NAME("hwxface")
  48. /******************************************************************************
  49. *
  50. * FUNCTION: acpi_reset
  51. *
  52. * PARAMETERS: None
  53. *
  54. * RETURN: Status
  55. *
  56. * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
  57. * support reset register in PCI config space, this must be
  58. * handled separately.
  59. *
  60. ******************************************************************************/
  61. acpi_status acpi_reset(void)
  62. {
  63. struct acpi_generic_address *reset_reg;
  64. acpi_status status;
  65. ACPI_FUNCTION_TRACE(acpi_reset);
  66. reset_reg = &acpi_gbl_FADT.reset_register;
  67. /* Check if the reset register is supported */
  68. if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) ||
  69. !reset_reg->address) {
  70. return_ACPI_STATUS(AE_NOT_EXIST);
  71. }
  72. if (reset_reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  73. /*
  74. * For I/O space, write directly to the OSL. This bypasses the port
  75. * validation mechanism, which may block a valid write to the reset
  76. * register.
  77. *
  78. * NOTE:
  79. * The ACPI spec requires the reset register width to be 8, so we
  80. * hardcode it here and ignore the FADT value. This maintains
  81. * compatibility with other ACPI implementations that have allowed
  82. * BIOS code with bad register width values to go unnoticed.
  83. */
  84. status =
  85. acpi_os_write_port((acpi_io_address) reset_reg->address,
  86. acpi_gbl_FADT.reset_value,
  87. ACPI_RESET_REGISTER_WIDTH);
  88. } else {
  89. /* Write the reset value to the reset register */
  90. status = acpi_hw_write(acpi_gbl_FADT.reset_value, reset_reg);
  91. }
  92. return_ACPI_STATUS(status);
  93. }
  94. ACPI_EXPORT_SYMBOL(acpi_reset)
  95. /******************************************************************************
  96. *
  97. * FUNCTION: acpi_read
  98. *
  99. * PARAMETERS: value - Where the value is returned
  100. * reg - GAS register structure
  101. *
  102. * RETURN: Status
  103. *
  104. * DESCRIPTION: Read from either memory or IO space.
  105. *
  106. * LIMITATIONS: <These limitations also apply to acpi_write>
  107. * bit_width must be exactly 8, 16, 32, or 64.
  108. * space_ID must be system_memory or system_IO.
  109. * bit_offset and access_width are currently ignored, as there has
  110. * not been a need to implement these.
  111. *
  112. ******************************************************************************/
  113. acpi_status acpi_read(u64 *return_value, struct acpi_generic_address *reg)
  114. {
  115. u32 value_lo;
  116. u32 value_hi;
  117. u32 width;
  118. u64 address;
  119. acpi_status status;
  120. ACPI_FUNCTION_NAME(acpi_read);
  121. if (!return_value) {
  122. return (AE_BAD_PARAMETER);
  123. }
  124. /* Validate contents of the GAS register. Allow 64-bit transfers */
  125. status = acpi_hw_validate_register(reg, 64, &address);
  126. if (ACPI_FAILURE(status)) {
  127. return (status);
  128. }
  129. /*
  130. * Two address spaces supported: Memory or I/O. PCI_Config is
  131. * not supported here because the GAS structure is insufficient
  132. */
  133. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  134. status = acpi_os_read_memory((acpi_physical_address)
  135. address, return_value,
  136. reg->bit_width);
  137. if (ACPI_FAILURE(status)) {
  138. return (status);
  139. }
  140. } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
  141. value_lo = 0;
  142. value_hi = 0;
  143. width = reg->bit_width;
  144. if (width == 64) {
  145. width = 32; /* Break into two 32-bit transfers */
  146. }
  147. status = acpi_hw_read_port((acpi_io_address)
  148. address, &value_lo, width);
  149. if (ACPI_FAILURE(status)) {
  150. return (status);
  151. }
  152. if (reg->bit_width == 64) {
  153. /* Read the top 32 bits */
  154. status = acpi_hw_read_port((acpi_io_address)
  155. (address + 4), &value_hi,
  156. 32);
  157. if (ACPI_FAILURE(status)) {
  158. return (status);
  159. }
  160. }
  161. /* Set the return value only if status is AE_OK */
  162. *return_value = (value_lo | ((u64)value_hi << 32));
  163. }
  164. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  165. "Read: %8.8X%8.8X width %2d from %8.8X%8.8X (%s)\n",
  166. ACPI_FORMAT_UINT64(*return_value), reg->bit_width,
  167. ACPI_FORMAT_UINT64(address),
  168. acpi_ut_get_region_name(reg->space_id)));
  169. return (AE_OK);
  170. }
  171. ACPI_EXPORT_SYMBOL(acpi_read)
  172. /******************************************************************************
  173. *
  174. * FUNCTION: acpi_write
  175. *
  176. * PARAMETERS: value - Value to be written
  177. * reg - GAS register structure
  178. *
  179. * RETURN: Status
  180. *
  181. * DESCRIPTION: Write to either memory or IO space.
  182. *
  183. ******************************************************************************/
  184. acpi_status acpi_write(u64 value, struct acpi_generic_address *reg)
  185. {
  186. u32 width;
  187. u64 address;
  188. acpi_status status;
  189. ACPI_FUNCTION_NAME(acpi_write);
  190. /* Validate contents of the GAS register. Allow 64-bit transfers */
  191. status = acpi_hw_validate_register(reg, 64, &address);
  192. if (ACPI_FAILURE(status)) {
  193. return (status);
  194. }
  195. /*
  196. * Two address spaces supported: Memory or IO. PCI_Config is
  197. * not supported here because the GAS structure is insufficient
  198. */
  199. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  200. status = acpi_os_write_memory((acpi_physical_address)
  201. address, value, reg->bit_width);
  202. if (ACPI_FAILURE(status)) {
  203. return (status);
  204. }
  205. } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
  206. width = reg->bit_width;
  207. if (width == 64) {
  208. width = 32; /* Break into two 32-bit transfers */
  209. }
  210. status = acpi_hw_write_port((acpi_io_address)
  211. address, ACPI_LODWORD(value),
  212. width);
  213. if (ACPI_FAILURE(status)) {
  214. return (status);
  215. }
  216. if (reg->bit_width == 64) {
  217. status = acpi_hw_write_port((acpi_io_address)
  218. (address + 4),
  219. ACPI_HIDWORD(value), 32);
  220. if (ACPI_FAILURE(status)) {
  221. return (status);
  222. }
  223. }
  224. }
  225. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  226. "Wrote: %8.8X%8.8X width %2d to %8.8X%8.8X (%s)\n",
  227. ACPI_FORMAT_UINT64(value), reg->bit_width,
  228. ACPI_FORMAT_UINT64(address),
  229. acpi_ut_get_region_name(reg->space_id)));
  230. return (status);
  231. }
  232. ACPI_EXPORT_SYMBOL(acpi_write)
  233. #if (!ACPI_REDUCED_HARDWARE)
  234. /*******************************************************************************
  235. *
  236. * FUNCTION: acpi_read_bit_register
  237. *
  238. * PARAMETERS: register_id - ID of ACPI Bit Register to access
  239. * return_value - Value that was read from the register,
  240. * normalized to bit position zero.
  241. *
  242. * RETURN: Status and the value read from the specified Register. Value
  243. * returned is normalized to bit0 (is shifted all the way right)
  244. *
  245. * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
  246. *
  247. * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
  248. * PM2 Control.
  249. *
  250. * Note: The hardware lock is not required when reading the ACPI bit registers
  251. * since almost all of them are single bit and it does not matter that
  252. * the parent hardware register can be split across two physical
  253. * registers. The only multi-bit field is SLP_TYP in the PM1 control
  254. * register, but this field does not cross an 8-bit boundary (nor does
  255. * it make much sense to actually read this field.)
  256. *
  257. ******************************************************************************/
  258. acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
  259. {
  260. struct acpi_bit_register_info *bit_reg_info;
  261. u32 register_value;
  262. u32 value;
  263. acpi_status status;
  264. ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
  265. /* Get the info structure corresponding to the requested ACPI Register */
  266. bit_reg_info = acpi_hw_get_bit_register_info(register_id);
  267. if (!bit_reg_info) {
  268. return_ACPI_STATUS(AE_BAD_PARAMETER);
  269. }
  270. /* Read the entire parent register */
  271. status = acpi_hw_register_read(bit_reg_info->parent_register,
  272. &register_value);
  273. if (ACPI_FAILURE(status)) {
  274. return_ACPI_STATUS(status);
  275. }
  276. /* Normalize the value that was read, mask off other bits */
  277. value = ((register_value & bit_reg_info->access_bit_mask)
  278. >> bit_reg_info->bit_position);
  279. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  280. "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
  281. register_id, bit_reg_info->parent_register,
  282. register_value, value));
  283. *return_value = value;
  284. return_ACPI_STATUS(AE_OK);
  285. }
  286. ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
  287. /*******************************************************************************
  288. *
  289. * FUNCTION: acpi_write_bit_register
  290. *
  291. * PARAMETERS: register_id - ID of ACPI Bit Register to access
  292. * value - Value to write to the register, in bit
  293. * position zero. The bit is automatically
  294. * shifted to the correct position.
  295. *
  296. * RETURN: Status
  297. *
  298. * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
  299. * since most operations require a read/modify/write sequence.
  300. *
  301. * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
  302. * PM2 Control.
  303. *
  304. * Note that at this level, the fact that there may be actually two
  305. * hardware registers (A and B - and B may not exist) is abstracted.
  306. *
  307. ******************************************************************************/
  308. acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  309. {
  310. struct acpi_bit_register_info *bit_reg_info;
  311. acpi_cpu_flags lock_flags;
  312. u32 register_value;
  313. acpi_status status = AE_OK;
  314. ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
  315. /* Get the info structure corresponding to the requested ACPI Register */
  316. bit_reg_info = acpi_hw_get_bit_register_info(register_id);
  317. if (!bit_reg_info) {
  318. return_ACPI_STATUS(AE_BAD_PARAMETER);
  319. }
  320. lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  321. /*
  322. * At this point, we know that the parent register is one of the
  323. * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
  324. */
  325. if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
  326. /*
  327. * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
  328. *
  329. * Perform a register read to preserve the bits that we are not
  330. * interested in
  331. */
  332. status = acpi_hw_register_read(bit_reg_info->parent_register,
  333. &register_value);
  334. if (ACPI_FAILURE(status)) {
  335. goto unlock_and_exit;
  336. }
  337. /*
  338. * Insert the input bit into the value that was just read
  339. * and write the register
  340. */
  341. ACPI_REGISTER_INSERT_VALUE(register_value,
  342. bit_reg_info->bit_position,
  343. bit_reg_info->access_bit_mask,
  344. value);
  345. status = acpi_hw_register_write(bit_reg_info->parent_register,
  346. register_value);
  347. } else {
  348. /*
  349. * 2) Case for PM1 Status
  350. *
  351. * The Status register is different from the rest. Clear an event
  352. * by writing 1, writing 0 has no effect. So, the only relevant
  353. * information is the single bit we're interested in, all others
  354. * should be written as 0 so they will be left unchanged.
  355. */
  356. register_value = ACPI_REGISTER_PREPARE_BITS(value,
  357. bit_reg_info->
  358. bit_position,
  359. bit_reg_info->
  360. access_bit_mask);
  361. /* No need to write the register if value is all zeros */
  362. if (register_value) {
  363. status =
  364. acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  365. register_value);
  366. }
  367. }
  368. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  369. "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
  370. register_id, bit_reg_info->parent_register, value,
  371. register_value));
  372. unlock_and_exit:
  373. acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  374. return_ACPI_STATUS(status);
  375. }
  376. ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
  377. #endif /* !ACPI_REDUCED_HARDWARE */
  378. /*******************************************************************************
  379. *
  380. * FUNCTION: acpi_get_sleep_type_data
  381. *
  382. * PARAMETERS: sleep_state - Numeric sleep state
  383. * *sleep_type_a - Where SLP_TYPa is returned
  384. * *sleep_type_b - Where SLP_TYPb is returned
  385. *
  386. * RETURN: Status
  387. *
  388. * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested
  389. * sleep state via the appropriate \_Sx object.
  390. *
  391. * The sleep state package returned from the corresponding \_Sx_ object
  392. * must contain at least one integer.
  393. *
  394. * March 2005:
  395. * Added support for a package that contains two integers. This
  396. * goes against the ACPI specification which defines this object as a
  397. * package with one encoded DWORD integer. However, existing practice
  398. * by many BIOS vendors is to return a package with 2 or more integer
  399. * elements, at least one per sleep type (A/B).
  400. *
  401. * January 2013:
  402. * Therefore, we must be prepared to accept a package with either a
  403. * single integer or multiple integers.
  404. *
  405. * The single integer DWORD format is as follows:
  406. * BYTE 0 - Value for the PM1A SLP_TYP register
  407. * BYTE 1 - Value for the PM1B SLP_TYP register
  408. * BYTE 2-3 - Reserved
  409. *
  410. * The dual integer format is as follows:
  411. * Integer 0 - Value for the PM1A SLP_TYP register
  412. * Integer 1 - Value for the PM1A SLP_TYP register
  413. *
  414. ******************************************************************************/
  415. acpi_status
  416. acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
  417. {
  418. acpi_status status;
  419. struct acpi_evaluate_info *info;
  420. union acpi_operand_object **elements;
  421. ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
  422. /* Validate parameters */
  423. if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
  424. return_ACPI_STATUS(AE_BAD_PARAMETER);
  425. }
  426. /* Allocate the evaluation information block */
  427. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  428. if (!info) {
  429. return_ACPI_STATUS(AE_NO_MEMORY);
  430. }
  431. /*
  432. * Evaluate the \_Sx namespace object containing the register values
  433. * for this state
  434. */
  435. info->relative_pathname = ACPI_CAST_PTR(char,
  436. acpi_gbl_sleep_state_names
  437. [sleep_state]);
  438. status = acpi_ns_evaluate(info);
  439. if (ACPI_FAILURE(status)) {
  440. if (status == AE_NOT_FOUND) {
  441. /* The _Sx states are optional, ignore NOT_FOUND */
  442. goto final_cleanup;
  443. }
  444. goto warning_cleanup;
  445. }
  446. /* Must have a return object */
  447. if (!info->return_object) {
  448. ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
  449. info->relative_pathname));
  450. status = AE_AML_NO_RETURN_VALUE;
  451. goto warning_cleanup;
  452. }
  453. /* Return object must be of type Package */
  454. if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
  455. ACPI_ERROR((AE_INFO,
  456. "Sleep State return object is not a Package"));
  457. status = AE_AML_OPERAND_TYPE;
  458. goto return_value_cleanup;
  459. }
  460. /*
  461. * Any warnings about the package length or the object types have
  462. * already been issued by the predefined name module -- there is no
  463. * need to repeat them here.
  464. */
  465. elements = info->return_object->package.elements;
  466. switch (info->return_object->package.count) {
  467. case 0:
  468. status = AE_AML_PACKAGE_LIMIT;
  469. break;
  470. case 1:
  471. if (elements[0]->common.type != ACPI_TYPE_INTEGER) {
  472. status = AE_AML_OPERAND_TYPE;
  473. break;
  474. }
  475. /* A valid _Sx_ package with one integer */
  476. *sleep_type_a = (u8)elements[0]->integer.value;
  477. *sleep_type_b = (u8)(elements[0]->integer.value >> 8);
  478. break;
  479. case 2:
  480. default:
  481. if ((elements[0]->common.type != ACPI_TYPE_INTEGER) ||
  482. (elements[1]->common.type != ACPI_TYPE_INTEGER)) {
  483. status = AE_AML_OPERAND_TYPE;
  484. break;
  485. }
  486. /* A valid _Sx_ package with two integers */
  487. *sleep_type_a = (u8)elements[0]->integer.value;
  488. *sleep_type_b = (u8)elements[1]->integer.value;
  489. break;
  490. }
  491. return_value_cleanup:
  492. acpi_ut_remove_reference(info->return_object);
  493. warning_cleanup:
  494. if (ACPI_FAILURE(status)) {
  495. ACPI_EXCEPTION((AE_INFO, status,
  496. "While evaluating Sleep State [%s]",
  497. info->relative_pathname));
  498. }
  499. final_cleanup:
  500. ACPI_FREE(info);
  501. return_ACPI_STATUS(status);
  502. }
  503. ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)