hwxface.c 17 KB

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