utmisc.c 38 KB

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