hwxface.c 18 KB

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