hwxface.c 17 KB

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