hwxface.c 17 KB

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