hwxface.c 17 KB

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