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