/drivers/acpi/utilities/utmisc.c

https://bitbucket.org/evzijst/gittest · C · 1516 lines · 665 code · 327 blank · 524 comment · 101 complexity · 66e5dc444c09bc602f30e3b6224b7e6f MD5 · raw file

  1. /*******************************************************************************
  2. *
  3. * Module Name: utmisc - common utility procedures
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <acpi/acnamesp.h>
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME ("utmisc")
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ut_print_string
  49. *
  50. * PARAMETERS: String - Null terminated ASCII string
  51. *
  52. * RETURN: None
  53. *
  54. * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
  55. * sequences.
  56. *
  57. ******************************************************************************/
  58. void
  59. acpi_ut_print_string (
  60. char *string,
  61. u8 max_length)
  62. {
  63. u32 i;
  64. if (!string) {
  65. acpi_os_printf ("<\"NULL STRING PTR\">");
  66. return;
  67. }
  68. acpi_os_printf ("\"");
  69. for (i = 0; string[i] && (i < max_length); i++) {
  70. /* Escape sequences */
  71. switch (string[i]) {
  72. case 0x07:
  73. acpi_os_printf ("\\a"); /* BELL */
  74. break;
  75. case 0x08:
  76. acpi_os_printf ("\\b"); /* BACKSPACE */
  77. break;
  78. case 0x0C:
  79. acpi_os_printf ("\\f"); /* FORMFEED */
  80. break;
  81. case 0x0A:
  82. acpi_os_printf ("\\n"); /* LINEFEED */
  83. break;
  84. case 0x0D:
  85. acpi_os_printf ("\\r"); /* CARRIAGE RETURN*/
  86. break;
  87. case 0x09:
  88. acpi_os_printf ("\\t"); /* HORIZONTAL TAB */
  89. break;
  90. case 0x0B:
  91. acpi_os_printf ("\\v"); /* VERTICAL TAB */
  92. break;
  93. case '\'': /* Single Quote */
  94. case '\"': /* Double Quote */
  95. case '\\': /* Backslash */
  96. acpi_os_printf ("\\%c", (int) string[i]);
  97. break;
  98. default:
  99. /* Check for printable character or hex escape */
  100. if (ACPI_IS_PRINT (string[i]))
  101. {
  102. /* This is a normal character */
  103. acpi_os_printf ("%c", (int) string[i]);
  104. }
  105. else
  106. {
  107. /* All others will be Hex escapes */
  108. acpi_os_printf ("\\x%2.2X", (s32) string[i]);
  109. }
  110. break;
  111. }
  112. }
  113. acpi_os_printf ("\"");
  114. if (i == max_length && string[i]) {
  115. acpi_os_printf ("...");
  116. }
  117. }
  118. /*******************************************************************************
  119. *
  120. * FUNCTION: acpi_ut_dword_byte_swap
  121. *
  122. * PARAMETERS: Value - Value to be converted
  123. *
  124. * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
  125. *
  126. ******************************************************************************/
  127. u32
  128. acpi_ut_dword_byte_swap (
  129. u32 value)
  130. {
  131. union {
  132. u32 value;
  133. u8 bytes[4];
  134. } out;
  135. union {
  136. u32 value;
  137. u8 bytes[4];
  138. } in;
  139. ACPI_FUNCTION_ENTRY ();
  140. in.value = value;
  141. out.bytes[0] = in.bytes[3];
  142. out.bytes[1] = in.bytes[2];
  143. out.bytes[2] = in.bytes[1];
  144. out.bytes[3] = in.bytes[0];
  145. return (out.value);
  146. }
  147. /*******************************************************************************
  148. *
  149. * FUNCTION: acpi_ut_set_integer_width
  150. *
  151. * PARAMETERS: Revision From DSDT header
  152. *
  153. * RETURN: None
  154. *
  155. * DESCRIPTION: Set the global integer bit width based upon the revision
  156. * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
  157. * For Revision 2 and above, Integers are 64 bits. Yes, this
  158. * makes a difference.
  159. *
  160. ******************************************************************************/
  161. void
  162. acpi_ut_set_integer_width (
  163. u8 revision)
  164. {
  165. if (revision <= 1) {
  166. acpi_gbl_integer_bit_width = 32;
  167. acpi_gbl_integer_nybble_width = 8;
  168. acpi_gbl_integer_byte_width = 4;
  169. }
  170. else {
  171. acpi_gbl_integer_bit_width = 64;
  172. acpi_gbl_integer_nybble_width = 16;
  173. acpi_gbl_integer_byte_width = 8;
  174. }
  175. }
  176. #ifdef ACPI_DEBUG_OUTPUT
  177. /*******************************************************************************
  178. *
  179. * FUNCTION: acpi_ut_display_init_pathname
  180. *
  181. * PARAMETERS: obj_handle - Handle whose pathname will be displayed
  182. * Path - Additional path string to be appended.
  183. * (NULL if no extra path)
  184. *
  185. * RETURN: acpi_status
  186. *
  187. * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
  188. *
  189. ******************************************************************************/
  190. void
  191. acpi_ut_display_init_pathname (
  192. u8 type,
  193. struct acpi_namespace_node *obj_handle,
  194. char *path)
  195. {
  196. acpi_status status;
  197. struct acpi_buffer buffer;
  198. ACPI_FUNCTION_ENTRY ();
  199. /* Only print the path if the appropriate debug level is enabled */
  200. if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
  201. return;
  202. }
  203. /* Get the full pathname to the node */
  204. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  205. status = acpi_ns_handle_to_pathname (obj_handle, &buffer);
  206. if (ACPI_FAILURE (status)) {
  207. return;
  208. }
  209. /* Print what we're doing */
  210. switch (type) {
  211. case ACPI_TYPE_METHOD:
  212. acpi_os_printf ("Executing ");
  213. break;
  214. default:
  215. acpi_os_printf ("Initializing ");
  216. break;
  217. }
  218. /* Print the object type and pathname */
  219. acpi_os_printf ("%-12s %s", acpi_ut_get_type_name (type), (char *) buffer.pointer);
  220. /* Extra path is used to append names like _STA, _INI, etc. */
  221. if (path) {
  222. acpi_os_printf (".%s", path);
  223. }
  224. acpi_os_printf ("\n");
  225. ACPI_MEM_FREE (buffer.pointer);
  226. }
  227. #endif
  228. /*******************************************************************************
  229. *
  230. * FUNCTION: acpi_ut_valid_acpi_name
  231. *
  232. * PARAMETERS: Character - The character to be examined
  233. *
  234. * RETURN: 1 if Character may appear in a name, else 0
  235. *
  236. * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
  237. * 1) Upper case alpha
  238. * 2) numeric
  239. * 3) underscore
  240. *
  241. ******************************************************************************/
  242. u8
  243. acpi_ut_valid_acpi_name (
  244. u32 name)
  245. {
  246. char *name_ptr = (char *) &name;
  247. char character;
  248. acpi_native_uint i;
  249. ACPI_FUNCTION_ENTRY ();
  250. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  251. character = *name_ptr;
  252. name_ptr++;
  253. if (!((character == '_') ||
  254. (character >= 'A' && character <= 'Z') ||
  255. (character >= '0' && character <= '9'))) {
  256. return (FALSE);
  257. }
  258. }
  259. return (TRUE);
  260. }
  261. /*******************************************************************************
  262. *
  263. * FUNCTION: acpi_ut_valid_acpi_character
  264. *
  265. * PARAMETERS: Character - The character to be examined
  266. *
  267. * RETURN: 1 if Character may appear in a name, else 0
  268. *
  269. * DESCRIPTION: Check for a printable character
  270. *
  271. ******************************************************************************/
  272. u8
  273. acpi_ut_valid_acpi_character (
  274. char character)
  275. {
  276. ACPI_FUNCTION_ENTRY ();
  277. return ((u8) ((character == '_') ||
  278. (character >= 'A' && character <= 'Z') ||
  279. (character >= '0' && character <= '9')));
  280. }
  281. /*******************************************************************************
  282. *
  283. * FUNCTION: acpi_ut_strtoul64
  284. *
  285. * PARAMETERS: String - Null terminated string
  286. * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
  287. * ret_integer - Where the converted integer is returned
  288. *
  289. * RETURN: Status and Converted value
  290. *
  291. * DESCRIPTION: Convert a string into an unsigned value.
  292. * NOTE: Does not support Octal strings, not needed.
  293. *
  294. ******************************************************************************/
  295. acpi_status
  296. acpi_ut_strtoul64 (
  297. char *string,
  298. u32 base,
  299. acpi_integer *ret_integer)
  300. {
  301. u32 this_digit = 0;
  302. acpi_integer return_value = 0;
  303. acpi_integer quotient;
  304. ACPI_FUNCTION_TRACE ("ut_stroul64");
  305. if ((!string) || !(*string)) {
  306. goto error_exit;
  307. }
  308. switch (base) {
  309. case ACPI_ANY_BASE:
  310. case 10:
  311. case 16:
  312. break;
  313. default:
  314. /* Invalid Base */
  315. return_ACPI_STATUS (AE_BAD_PARAMETER);
  316. }
  317. /* Skip over any white space in the buffer */
  318. while (ACPI_IS_SPACE (*string) || *string == '\t') {
  319. string++;
  320. }
  321. /*
  322. * If the input parameter Base is zero, then we need to
  323. * determine if it is decimal or hexadecimal:
  324. */
  325. if (base == 0) {
  326. if ((*string == '0') &&
  327. (ACPI_TOLOWER (*(string + 1)) == 'x')) {
  328. base = 16;
  329. string += 2;
  330. }
  331. else {
  332. base = 10;
  333. }
  334. }
  335. /*
  336. * For hexadecimal base, skip over the leading
  337. * 0 or 0x, if they are present.
  338. */
  339. if ((base == 16) &&
  340. (*string == '0') &&
  341. (ACPI_TOLOWER (*(string + 1)) == 'x')) {
  342. string += 2;
  343. }
  344. /* Any string left? */
  345. if (!(*string)) {
  346. goto error_exit;
  347. }
  348. /* Main loop: convert the string to a 64-bit integer */
  349. while (*string) {
  350. if (ACPI_IS_DIGIT (*string)) {
  351. /* Convert ASCII 0-9 to Decimal value */
  352. this_digit = ((u8) *string) - '0';
  353. }
  354. else {
  355. if (base == 10) {
  356. /* Digit is out of range */
  357. goto error_exit;
  358. }
  359. this_digit = (u8) ACPI_TOUPPER (*string);
  360. if (ACPI_IS_XDIGIT ((char) this_digit)) {
  361. /* Convert ASCII Hex char to value */
  362. this_digit = this_digit - 'A' + 10;
  363. }
  364. else {
  365. /*
  366. * We allow non-hex chars, just stop now, same as end-of-string.
  367. * See ACPI spec, string-to-integer conversion.
  368. */
  369. break;
  370. }
  371. }
  372. /* Divide the digit into the correct position */
  373. (void) acpi_ut_short_divide ((ACPI_INTEGER_MAX - (acpi_integer) this_digit),
  374. base, &quotient, NULL);
  375. if (return_value > quotient) {
  376. goto error_exit;
  377. }
  378. return_value *= base;
  379. return_value += this_digit;
  380. string++;
  381. }
  382. /* All done, normal exit */
  383. *ret_integer = return_value;
  384. return_ACPI_STATUS (AE_OK);
  385. error_exit:
  386. /* Base was set/validated above */
  387. if (base == 10) {
  388. return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
  389. }
  390. else {
  391. return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
  392. }
  393. }
  394. /*******************************************************************************
  395. *
  396. * FUNCTION: acpi_ut_strupr
  397. *
  398. * PARAMETERS: src_string - The source string to convert to
  399. *
  400. * RETURN: src_string
  401. *
  402. * DESCRIPTION: Convert string to uppercase
  403. *
  404. ******************************************************************************/
  405. #ifdef ACPI_FUTURE_USAGE
  406. char *
  407. acpi_ut_strupr (
  408. char *src_string)
  409. {
  410. char *string;
  411. ACPI_FUNCTION_ENTRY ();
  412. /* Walk entire string, uppercasing the letters */
  413. for (string = src_string; *string; ) {
  414. *string = (char) ACPI_TOUPPER (*string);
  415. string++;
  416. }
  417. return (src_string);
  418. }
  419. #endif /* ACPI_FUTURE_USAGE */
  420. /*******************************************************************************
  421. *
  422. * FUNCTION: acpi_ut_mutex_initialize
  423. *
  424. * PARAMETERS: None.
  425. *
  426. * RETURN: Status
  427. *
  428. * DESCRIPTION: Create the system mutex objects.
  429. *
  430. ******************************************************************************/
  431. acpi_status
  432. acpi_ut_mutex_initialize (
  433. void)
  434. {
  435. u32 i;
  436. acpi_status status;
  437. ACPI_FUNCTION_TRACE ("ut_mutex_initialize");
  438. /*
  439. * Create each of the predefined mutex objects
  440. */
  441. for (i = 0; i < NUM_MUTEX; i++) {
  442. status = acpi_ut_create_mutex (i);
  443. if (ACPI_FAILURE (status)) {
  444. return_ACPI_STATUS (status);
  445. }
  446. }
  447. status = acpi_os_create_lock (&acpi_gbl_gpe_lock);
  448. return_ACPI_STATUS (status);
  449. }
  450. /*******************************************************************************
  451. *
  452. * FUNCTION: acpi_ut_mutex_terminate
  453. *
  454. * PARAMETERS: None.
  455. *
  456. * RETURN: None.
  457. *
  458. * DESCRIPTION: Delete all of the system mutex objects.
  459. *
  460. ******************************************************************************/
  461. void
  462. acpi_ut_mutex_terminate (
  463. void)
  464. {
  465. u32 i;
  466. ACPI_FUNCTION_TRACE ("ut_mutex_terminate");
  467. /*
  468. * Delete each predefined mutex object
  469. */
  470. for (i = 0; i < NUM_MUTEX; i++) {
  471. (void) acpi_ut_delete_mutex (i);
  472. }
  473. acpi_os_delete_lock (acpi_gbl_gpe_lock);
  474. return_VOID;
  475. }
  476. /*******************************************************************************
  477. *
  478. * FUNCTION: acpi_ut_create_mutex
  479. *
  480. * PARAMETERS: mutex_iD - ID of the mutex to be created
  481. *
  482. * RETURN: Status
  483. *
  484. * DESCRIPTION: Create a mutex object.
  485. *
  486. ******************************************************************************/
  487. acpi_status
  488. acpi_ut_create_mutex (
  489. acpi_mutex_handle mutex_id)
  490. {
  491. acpi_status status = AE_OK;
  492. ACPI_FUNCTION_TRACE_U32 ("ut_create_mutex", mutex_id);
  493. if (mutex_id > MAX_MUTEX) {
  494. return_ACPI_STATUS (AE_BAD_PARAMETER);
  495. }
  496. if (!acpi_gbl_mutex_info[mutex_id].mutex) {
  497. status = acpi_os_create_semaphore (1, 1,
  498. &acpi_gbl_mutex_info[mutex_id].mutex);
  499. acpi_gbl_mutex_info[mutex_id].owner_id = ACPI_MUTEX_NOT_ACQUIRED;
  500. acpi_gbl_mutex_info[mutex_id].use_count = 0;
  501. }
  502. return_ACPI_STATUS (status);
  503. }
  504. /*******************************************************************************
  505. *
  506. * FUNCTION: acpi_ut_delete_mutex
  507. *
  508. * PARAMETERS: mutex_iD - ID of the mutex to be deleted
  509. *
  510. * RETURN: Status
  511. *
  512. * DESCRIPTION: Delete a mutex object.
  513. *
  514. ******************************************************************************/
  515. acpi_status
  516. acpi_ut_delete_mutex (
  517. acpi_mutex_handle mutex_id)
  518. {
  519. acpi_status status;
  520. ACPI_FUNCTION_TRACE_U32 ("ut_delete_mutex", mutex_id);
  521. if (mutex_id > MAX_MUTEX) {
  522. return_ACPI_STATUS (AE_BAD_PARAMETER);
  523. }
  524. status = acpi_os_delete_semaphore (acpi_gbl_mutex_info[mutex_id].mutex);
  525. acpi_gbl_mutex_info[mutex_id].mutex = NULL;
  526. acpi_gbl_mutex_info[mutex_id].owner_id = ACPI_MUTEX_NOT_ACQUIRED;
  527. return_ACPI_STATUS (status);
  528. }
  529. /*******************************************************************************
  530. *
  531. * FUNCTION: acpi_ut_acquire_mutex
  532. *
  533. * PARAMETERS: mutex_iD - ID of the mutex to be acquired
  534. *
  535. * RETURN: Status
  536. *
  537. * DESCRIPTION: Acquire a mutex object.
  538. *
  539. ******************************************************************************/
  540. acpi_status
  541. acpi_ut_acquire_mutex (
  542. acpi_mutex_handle mutex_id)
  543. {
  544. acpi_status status;
  545. u32 this_thread_id;
  546. ACPI_FUNCTION_NAME ("ut_acquire_mutex");
  547. if (mutex_id > MAX_MUTEX) {
  548. return (AE_BAD_PARAMETER);
  549. }
  550. this_thread_id = acpi_os_get_thread_id ();
  551. #ifdef ACPI_MUTEX_DEBUG
  552. {
  553. u32 i;
  554. /*
  555. * Mutex debug code, for internal debugging only.
  556. *
  557. * Deadlock prevention. Check if this thread owns any mutexes of value
  558. * greater than or equal to this one. If so, the thread has violated
  559. * the mutex ordering rule. This indicates a coding error somewhere in
  560. * the ACPI subsystem code.
  561. */
  562. for (i = mutex_id; i < MAX_MUTEX; i++) {
  563. if (acpi_gbl_mutex_info[i].owner_id == this_thread_id) {
  564. if (i == mutex_id) {
  565. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  566. "Mutex [%s] already acquired by this thread [%X]\n",
  567. acpi_ut_get_mutex_name (mutex_id), this_thread_id));
  568. return (AE_ALREADY_ACQUIRED);
  569. }
  570. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  571. "Invalid acquire order: Thread %X owns [%s], wants [%s]\n",
  572. this_thread_id, acpi_ut_get_mutex_name (i),
  573. acpi_ut_get_mutex_name (mutex_id)));
  574. return (AE_ACQUIRE_DEADLOCK);
  575. }
  576. }
  577. }
  578. #endif
  579. ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
  580. "Thread %X attempting to acquire Mutex [%s]\n",
  581. this_thread_id, acpi_ut_get_mutex_name (mutex_id)));
  582. status = acpi_os_wait_semaphore (acpi_gbl_mutex_info[mutex_id].mutex,
  583. 1, ACPI_WAIT_FOREVER);
  584. if (ACPI_SUCCESS (status)) {
  585. ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X acquired Mutex [%s]\n",
  586. this_thread_id, acpi_ut_get_mutex_name (mutex_id)));
  587. acpi_gbl_mutex_info[mutex_id].use_count++;
  588. acpi_gbl_mutex_info[mutex_id].owner_id = this_thread_id;
  589. }
  590. else {
  591. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Thread %X could not acquire Mutex [%s] %s\n",
  592. this_thread_id, acpi_ut_get_mutex_name (mutex_id),
  593. acpi_format_exception (status)));
  594. }
  595. return (status);
  596. }
  597. /*******************************************************************************
  598. *
  599. * FUNCTION: acpi_ut_release_mutex
  600. *
  601. * PARAMETERS: mutex_iD - ID of the mutex to be released
  602. *
  603. * RETURN: Status
  604. *
  605. * DESCRIPTION: Release a mutex object.
  606. *
  607. ******************************************************************************/
  608. acpi_status
  609. acpi_ut_release_mutex (
  610. acpi_mutex_handle mutex_id)
  611. {
  612. acpi_status status;
  613. u32 i;
  614. u32 this_thread_id;
  615. ACPI_FUNCTION_NAME ("ut_release_mutex");
  616. this_thread_id = acpi_os_get_thread_id ();
  617. ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
  618. "Thread %X releasing Mutex [%s]\n", this_thread_id,
  619. acpi_ut_get_mutex_name (mutex_id)));
  620. if (mutex_id > MAX_MUTEX) {
  621. return (AE_BAD_PARAMETER);
  622. }
  623. /*
  624. * Mutex must be acquired in order to release it!
  625. */
  626. if (acpi_gbl_mutex_info[mutex_id].owner_id == ACPI_MUTEX_NOT_ACQUIRED) {
  627. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  628. "Mutex [%s] is not acquired, cannot release\n",
  629. acpi_ut_get_mutex_name (mutex_id)));
  630. return (AE_NOT_ACQUIRED);
  631. }
  632. /*
  633. * Deadlock prevention. Check if this thread owns any mutexes of value
  634. * greater than this one. If so, the thread has violated the mutex
  635. * ordering rule. This indicates a coding error somewhere in
  636. * the ACPI subsystem code.
  637. */
  638. for (i = mutex_id; i < MAX_MUTEX; i++) {
  639. if (acpi_gbl_mutex_info[i].owner_id == this_thread_id) {
  640. if (i == mutex_id) {
  641. continue;
  642. }
  643. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  644. "Invalid release order: owns [%s], releasing [%s]\n",
  645. acpi_ut_get_mutex_name (i), acpi_ut_get_mutex_name (mutex_id)));
  646. return (AE_RELEASE_DEADLOCK);
  647. }
  648. }
  649. /* Mark unlocked FIRST */
  650. acpi_gbl_mutex_info[mutex_id].owner_id = ACPI_MUTEX_NOT_ACQUIRED;
  651. status = acpi_os_signal_semaphore (acpi_gbl_mutex_info[mutex_id].mutex, 1);
  652. if (ACPI_FAILURE (status)) {
  653. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Thread %X could not release Mutex [%s] %s\n",
  654. this_thread_id, acpi_ut_get_mutex_name (mutex_id),
  655. acpi_format_exception (status)));
  656. }
  657. else {
  658. ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X released Mutex [%s]\n",
  659. this_thread_id, acpi_ut_get_mutex_name (mutex_id)));
  660. }
  661. return (status);
  662. }
  663. /*******************************************************************************
  664. *
  665. * FUNCTION: acpi_ut_create_update_state_and_push
  666. *
  667. * PARAMETERS: *Object - Object to be added to the new state
  668. * Action - Increment/Decrement
  669. * state_list - List the state will be added to
  670. *
  671. * RETURN: None
  672. *
  673. * DESCRIPTION: Create a new state and push it
  674. *
  675. ******************************************************************************/
  676. acpi_status
  677. acpi_ut_create_update_state_and_push (
  678. union acpi_operand_object *object,
  679. u16 action,
  680. union acpi_generic_state **state_list)
  681. {
  682. union acpi_generic_state *state;
  683. ACPI_FUNCTION_ENTRY ();
  684. /* Ignore null objects; these are expected */
  685. if (!object) {
  686. return (AE_OK);
  687. }
  688. state = acpi_ut_create_update_state (object, action);
  689. if (!state) {
  690. return (AE_NO_MEMORY);
  691. }
  692. acpi_ut_push_generic_state (state_list, state);
  693. return (AE_OK);
  694. }
  695. /*******************************************************************************
  696. *
  697. * FUNCTION: acpi_ut_create_pkg_state_and_push
  698. *
  699. * PARAMETERS: *Object - Object to be added to the new state
  700. * Action - Increment/Decrement
  701. * state_list - List the state will be added to
  702. *
  703. * RETURN: None
  704. *
  705. * DESCRIPTION: Create a new state and push it
  706. *
  707. ******************************************************************************/
  708. #ifdef ACPI_FUTURE_USAGE
  709. acpi_status
  710. acpi_ut_create_pkg_state_and_push (
  711. void *internal_object,
  712. void *external_object,
  713. u16 index,
  714. union acpi_generic_state **state_list)
  715. {
  716. union acpi_generic_state *state;
  717. ACPI_FUNCTION_ENTRY ();
  718. state = acpi_ut_create_pkg_state (internal_object, external_object, index);
  719. if (!state) {
  720. return (AE_NO_MEMORY);
  721. }
  722. acpi_ut_push_generic_state (state_list, state);
  723. return (AE_OK);
  724. }
  725. #endif /* ACPI_FUTURE_USAGE */
  726. /*******************************************************************************
  727. *
  728. * FUNCTION: acpi_ut_push_generic_state
  729. *
  730. * PARAMETERS: list_head - Head of the state stack
  731. * State - State object to push
  732. *
  733. * RETURN: Status
  734. *
  735. * DESCRIPTION: Push a state object onto a state stack
  736. *
  737. ******************************************************************************/
  738. void
  739. acpi_ut_push_generic_state (
  740. union acpi_generic_state **list_head,
  741. union acpi_generic_state *state)
  742. {
  743. ACPI_FUNCTION_TRACE ("ut_push_generic_state");
  744. /* Push the state object onto the front of the list (stack) */
  745. state->common.next = *list_head;
  746. *list_head = state;
  747. return_VOID;
  748. }
  749. /*******************************************************************************
  750. *
  751. * FUNCTION: acpi_ut_pop_generic_state
  752. *
  753. * PARAMETERS: list_head - Head of the state stack
  754. *
  755. * RETURN: Status
  756. *
  757. * DESCRIPTION: Pop a state object from a state stack
  758. *
  759. ******************************************************************************/
  760. union acpi_generic_state *
  761. acpi_ut_pop_generic_state (
  762. union acpi_generic_state **list_head)
  763. {
  764. union acpi_generic_state *state;
  765. ACPI_FUNCTION_TRACE ("ut_pop_generic_state");
  766. /* Remove the state object at the head of the list (stack) */
  767. state = *list_head;
  768. if (state) {
  769. /* Update the list head */
  770. *list_head = state->common.next;
  771. }
  772. return_PTR (state);
  773. }
  774. /*******************************************************************************
  775. *
  776. * FUNCTION: acpi_ut_create_generic_state
  777. *
  778. * PARAMETERS: None
  779. *
  780. * RETURN: Status
  781. *
  782. * DESCRIPTION: Create a generic state object. Attempt to obtain one from
  783. * the global state cache; If none available, create a new one.
  784. *
  785. ******************************************************************************/
  786. union acpi_generic_state *
  787. acpi_ut_create_generic_state (void)
  788. {
  789. union acpi_generic_state *state;
  790. ACPI_FUNCTION_ENTRY ();
  791. state = acpi_ut_acquire_from_cache (ACPI_MEM_LIST_STATE);
  792. /* Initialize */
  793. if (state) {
  794. state->common.data_type = ACPI_DESC_TYPE_STATE;
  795. }
  796. return (state);
  797. }
  798. /*******************************************************************************
  799. *
  800. * FUNCTION: acpi_ut_create_thread_state
  801. *
  802. * PARAMETERS: None
  803. *
  804. * RETURN: Thread State
  805. *
  806. * DESCRIPTION: Create a "Thread State" - a flavor of the generic state used
  807. * to track per-thread info during method execution
  808. *
  809. ******************************************************************************/
  810. struct acpi_thread_state *
  811. acpi_ut_create_thread_state (
  812. void)
  813. {
  814. union acpi_generic_state *state;
  815. ACPI_FUNCTION_TRACE ("ut_create_thread_state");
  816. /* Create the generic state object */
  817. state = acpi_ut_create_generic_state ();
  818. if (!state) {
  819. return_PTR (NULL);
  820. }
  821. /* Init fields specific to the update struct */
  822. state->common.data_type = ACPI_DESC_TYPE_STATE_THREAD;
  823. state->thread.thread_id = acpi_os_get_thread_id ();
  824. return_PTR ((struct acpi_thread_state *) state);
  825. }
  826. /*******************************************************************************
  827. *
  828. * FUNCTION: acpi_ut_create_update_state
  829. *
  830. * PARAMETERS: Object - Initial Object to be installed in the
  831. * state
  832. * Action - Update action to be performed
  833. *
  834. * RETURN: Status
  835. *
  836. * DESCRIPTION: Create an "Update State" - a flavor of the generic state used
  837. * to update reference counts and delete complex objects such
  838. * as packages.
  839. *
  840. ******************************************************************************/
  841. union acpi_generic_state *
  842. acpi_ut_create_update_state (
  843. union acpi_operand_object *object,
  844. u16 action)
  845. {
  846. union acpi_generic_state *state;
  847. ACPI_FUNCTION_TRACE_PTR ("ut_create_update_state", object);
  848. /* Create the generic state object */
  849. state = acpi_ut_create_generic_state ();
  850. if (!state) {
  851. return_PTR (NULL);
  852. }
  853. /* Init fields specific to the update struct */
  854. state->common.data_type = ACPI_DESC_TYPE_STATE_UPDATE;
  855. state->update.object = object;
  856. state->update.value = action;
  857. return_PTR (state);
  858. }
  859. /*******************************************************************************
  860. *
  861. * FUNCTION: acpi_ut_create_pkg_state
  862. *
  863. * PARAMETERS: Object - Initial Object to be installed in the
  864. * state
  865. * Action - Update action to be performed
  866. *
  867. * RETURN: Status
  868. *
  869. * DESCRIPTION: Create a "Package State"
  870. *
  871. ******************************************************************************/
  872. union acpi_generic_state *
  873. acpi_ut_create_pkg_state (
  874. void *internal_object,
  875. void *external_object,
  876. u16 index)
  877. {
  878. union acpi_generic_state *state;
  879. ACPI_FUNCTION_TRACE_PTR ("ut_create_pkg_state", internal_object);
  880. /* Create the generic state object */
  881. state = acpi_ut_create_generic_state ();
  882. if (!state) {
  883. return_PTR (NULL);
  884. }
  885. /* Init fields specific to the update struct */
  886. state->common.data_type = ACPI_DESC_TYPE_STATE_PACKAGE;
  887. state->pkg.source_object = (union acpi_operand_object *) internal_object;
  888. state->pkg.dest_object = external_object;
  889. state->pkg.index = index;
  890. state->pkg.num_packages = 1;
  891. return_PTR (state);
  892. }
  893. /*******************************************************************************
  894. *
  895. * FUNCTION: acpi_ut_create_control_state
  896. *
  897. * PARAMETERS: None
  898. *
  899. * RETURN: Status
  900. *
  901. * DESCRIPTION: Create a "Control State" - a flavor of the generic state used
  902. * to support nested IF/WHILE constructs in the AML.
  903. *
  904. ******************************************************************************/
  905. union acpi_generic_state *
  906. acpi_ut_create_control_state (
  907. void)
  908. {
  909. union acpi_generic_state *state;
  910. ACPI_FUNCTION_TRACE ("ut_create_control_state");
  911. /* Create the generic state object */
  912. state = acpi_ut_create_generic_state ();
  913. if (!state) {
  914. return_PTR (NULL);
  915. }
  916. /* Init fields specific to the control struct */
  917. state->common.data_type = ACPI_DESC_TYPE_STATE_CONTROL;
  918. state->common.state = ACPI_CONTROL_CONDITIONAL_EXECUTING;
  919. return_PTR (state);
  920. }
  921. /*******************************************************************************
  922. *
  923. * FUNCTION: acpi_ut_delete_generic_state
  924. *
  925. * PARAMETERS: State - The state object to be deleted
  926. *
  927. * RETURN: Status
  928. *
  929. * DESCRIPTION: Put a state object back into the global state cache. The object
  930. * is not actually freed at this time.
  931. *
  932. ******************************************************************************/
  933. void
  934. acpi_ut_delete_generic_state (
  935. union acpi_generic_state *state)
  936. {
  937. ACPI_FUNCTION_TRACE ("ut_delete_generic_state");
  938. acpi_ut_release_to_cache (ACPI_MEM_LIST_STATE, state);
  939. return_VOID;
  940. }
  941. #ifdef ACPI_ENABLE_OBJECT_CACHE
  942. /*******************************************************************************
  943. *
  944. * FUNCTION: acpi_ut_delete_generic_state_cache
  945. *
  946. * PARAMETERS: None
  947. *
  948. * RETURN: Status
  949. *
  950. * DESCRIPTION: Purge the global state object cache. Used during subsystem
  951. * termination.
  952. *
  953. ******************************************************************************/
  954. void
  955. acpi_ut_delete_generic_state_cache (
  956. void)
  957. {
  958. ACPI_FUNCTION_TRACE ("ut_delete_generic_state_cache");
  959. acpi_ut_delete_generic_cache (ACPI_MEM_LIST_STATE);
  960. return_VOID;
  961. }
  962. #endif
  963. /*******************************************************************************
  964. *
  965. * FUNCTION: acpi_ut_walk_package_tree
  966. *
  967. * PARAMETERS: obj_desc - The Package object on which to resolve refs
  968. *
  969. * RETURN: Status
  970. *
  971. * DESCRIPTION: Walk through a package
  972. *
  973. ******************************************************************************/
  974. acpi_status
  975. acpi_ut_walk_package_tree (
  976. union acpi_operand_object *source_object,
  977. void *target_object,
  978. acpi_pkg_callback walk_callback,
  979. void *context)
  980. {
  981. acpi_status status = AE_OK;
  982. union acpi_generic_state *state_list = NULL;
  983. union acpi_generic_state *state;
  984. u32 this_index;
  985. union acpi_operand_object *this_source_obj;
  986. ACPI_FUNCTION_TRACE ("ut_walk_package_tree");
  987. state = acpi_ut_create_pkg_state (source_object, target_object, 0);
  988. if (!state) {
  989. return_ACPI_STATUS (AE_NO_MEMORY);
  990. }
  991. while (state) {
  992. /* Get one element of the package */
  993. this_index = state->pkg.index;
  994. this_source_obj = (union acpi_operand_object *)
  995. state->pkg.source_object->package.elements[this_index];
  996. /*
  997. * Check for:
  998. * 1) An uninitialized package element. It is completely
  999. * legal to declare a package and leave it uninitialized
  1000. * 2) Not an internal object - can be a namespace node instead
  1001. * 3) Any type other than a package. Packages are handled in else
  1002. * case below.
  1003. */
  1004. if ((!this_source_obj) ||
  1005. (ACPI_GET_DESCRIPTOR_TYPE (this_source_obj) != ACPI_DESC_TYPE_OPERAND) ||
  1006. (ACPI_GET_OBJECT_TYPE (this_source_obj) != ACPI_TYPE_PACKAGE)) {
  1007. status = walk_callback (ACPI_COPY_TYPE_SIMPLE, this_source_obj,
  1008. state, context);
  1009. if (ACPI_FAILURE (status)) {
  1010. return_ACPI_STATUS (status);
  1011. }
  1012. state->pkg.index++;
  1013. while (state->pkg.index >= state->pkg.source_object->package.count) {
  1014. /*
  1015. * We've handled all of the objects at this level, This means
  1016. * that we have just completed a package. That package may
  1017. * have contained one or more packages itself.
  1018. *
  1019. * Delete this state and pop the previous state (package).
  1020. */
  1021. acpi_ut_delete_generic_state (state);
  1022. state = acpi_ut_pop_generic_state (&state_list);
  1023. /* Finished when there are no more states */
  1024. if (!state) {
  1025. /*
  1026. * We have handled all of the objects in the top level
  1027. * package just add the length of the package objects
  1028. * and exit
  1029. */
  1030. return_ACPI_STATUS (AE_OK);
  1031. }
  1032. /*
  1033. * Go back up a level and move the index past the just
  1034. * completed package object.
  1035. */
  1036. state->pkg.index++;
  1037. }
  1038. }
  1039. else {
  1040. /* This is a subobject of type package */
  1041. status = walk_callback (ACPI_COPY_TYPE_PACKAGE, this_source_obj,
  1042. state, context);
  1043. if (ACPI_FAILURE (status)) {
  1044. return_ACPI_STATUS (status);
  1045. }
  1046. /*
  1047. * Push the current state and create a new one
  1048. * The callback above returned a new target package object.
  1049. */
  1050. acpi_ut_push_generic_state (&state_list, state);
  1051. state = acpi_ut_create_pkg_state (this_source_obj,
  1052. state->pkg.this_target_obj, 0);
  1053. if (!state) {
  1054. return_ACPI_STATUS (AE_NO_MEMORY);
  1055. }
  1056. }
  1057. }
  1058. /* We should never get here */
  1059. return_ACPI_STATUS (AE_AML_INTERNAL);
  1060. }
  1061. /*******************************************************************************
  1062. *
  1063. * FUNCTION: acpi_ut_generate_checksum
  1064. *
  1065. * PARAMETERS: Buffer - Buffer to be scanned
  1066. * Length - number of bytes to examine
  1067. *
  1068. * RETURN: checksum
  1069. *
  1070. * DESCRIPTION: Generate a checksum on a raw buffer
  1071. *
  1072. ******************************************************************************/
  1073. u8
  1074. acpi_ut_generate_checksum (
  1075. u8 *buffer,
  1076. u32 length)
  1077. {
  1078. u32 i;
  1079. signed char sum = 0;
  1080. for (i = 0; i < length; i++) {
  1081. sum = (signed char) (sum + buffer[i]);
  1082. }
  1083. return ((u8) (0 - sum));
  1084. }
  1085. /*******************************************************************************
  1086. *
  1087. * FUNCTION: acpi_ut_get_resource_end_tag
  1088. *
  1089. * PARAMETERS: obj_desc - The resource template buffer object
  1090. *
  1091. * RETURN: Pointer to the end tag
  1092. *
  1093. * DESCRIPTION: Find the END_TAG resource descriptor in a resource template
  1094. *
  1095. ******************************************************************************/
  1096. u8 *
  1097. acpi_ut_get_resource_end_tag (
  1098. union acpi_operand_object *obj_desc)
  1099. {
  1100. u8 buffer_byte;
  1101. u8 *buffer;
  1102. u8 *end_buffer;
  1103. buffer = obj_desc->buffer.pointer;
  1104. end_buffer = buffer + obj_desc->buffer.length;
  1105. while (buffer < end_buffer) {
  1106. buffer_byte = *buffer;
  1107. if (buffer_byte & ACPI_RDESC_TYPE_MASK) {
  1108. /* Large Descriptor - Length is next 2 bytes */
  1109. buffer += ((*(buffer+1) | (*(buffer+2) << 8)) + 3);
  1110. }
  1111. else {
  1112. /* Small Descriptor. End Tag will be found here */
  1113. if ((buffer_byte & ACPI_RDESC_SMALL_MASK) == ACPI_RDESC_TYPE_END_TAG) {
  1114. /* Found the end tag descriptor, all done. */
  1115. return (buffer);
  1116. }
  1117. /* Length is in the header */
  1118. buffer += ((buffer_byte & 0x07) + 1);
  1119. }
  1120. }
  1121. /* End tag not found */
  1122. return (NULL);
  1123. }
  1124. /*******************************************************************************
  1125. *
  1126. * FUNCTION: acpi_ut_report_error
  1127. *
  1128. * PARAMETERS: module_name - Caller's module name (for error output)
  1129. * line_number - Caller's line number (for error output)
  1130. * component_id - Caller's component ID (for error output)
  1131. * Message - Error message to use on failure
  1132. *
  1133. * RETURN: None
  1134. *
  1135. * DESCRIPTION: Print error message
  1136. *
  1137. ******************************************************************************/
  1138. void
  1139. acpi_ut_report_error (
  1140. char *module_name,
  1141. u32 line_number,
  1142. u32 component_id)
  1143. {
  1144. acpi_os_printf ("%8s-%04d: *** Error: ", module_name, line_number);
  1145. }
  1146. /*******************************************************************************
  1147. *
  1148. * FUNCTION: acpi_ut_report_warning
  1149. *
  1150. * PARAMETERS: module_name - Caller's module name (for error output)
  1151. * line_number - Caller's line number (for error output)
  1152. * component_id - Caller's component ID (for error output)
  1153. * Message - Error message to use on failure
  1154. *
  1155. * RETURN: None
  1156. *
  1157. * DESCRIPTION: Print warning message
  1158. *
  1159. ******************************************************************************/
  1160. void
  1161. acpi_ut_report_warning (
  1162. char *module_name,
  1163. u32 line_number,
  1164. u32 component_id)
  1165. {
  1166. acpi_os_printf ("%8s-%04d: *** Warning: ", module_name, line_number);
  1167. }
  1168. /*******************************************************************************
  1169. *
  1170. * FUNCTION: acpi_ut_report_info
  1171. *
  1172. * PARAMETERS: module_name - Caller's module name (for error output)
  1173. * line_number - Caller's line number (for error output)
  1174. * component_id - Caller's component ID (for error output)
  1175. * Message - Error message to use on failure
  1176. *
  1177. * RETURN: None
  1178. *
  1179. * DESCRIPTION: Print information message
  1180. *
  1181. ******************************************************************************/
  1182. void
  1183. acpi_ut_report_info (
  1184. char *module_name,
  1185. u32 line_number,
  1186. u32 component_id)
  1187. {
  1188. acpi_os_printf ("%8s-%04d: *** Info: ", module_name, line_number);
  1189. }