osunixxf.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /******************************************************************************
  2. *
  3. * Module Name: osunixxf - UNIX OSL interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, 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. /*
  43. * These interfaces are required in order to compile the ASL compiler and the
  44. * various ACPICA tools under Linux or other Unix-like system.
  45. */
  46. #include <acpi/acpi.h>
  47. #include "accommon.h"
  48. #include "amlcode.h"
  49. #include "acparser.h"
  50. #include "acdebug.h"
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <stdarg.h>
  54. #include <unistd.h>
  55. #include <sys/time.h>
  56. #include <semaphore.h>
  57. #include <pthread.h>
  58. #include <errno.h>
  59. #define _COMPONENT ACPI_OS_SERVICES
  60. ACPI_MODULE_NAME("osunixxf")
  61. u8 acpi_gbl_debug_timeout = FALSE;
  62. /* Upcalls to acpi_exec */
  63. void
  64. ae_table_override(struct acpi_table_header *existing_table,
  65. struct acpi_table_header **new_table);
  66. typedef void *(*PTHREAD_CALLBACK) (void *);
  67. /* Buffer used by acpi_os_vprintf */
  68. #define ACPI_VPRINTF_BUFFER_SIZE 512
  69. #define _ASCII_NEWLINE '\n'
  70. /* Terminal support for acpi_exec only */
  71. #ifdef ACPI_EXEC_APP
  72. #include <termios.h>
  73. struct termios original_term_attributes;
  74. int term_attributes_were_set = 0;
  75. acpi_status acpi_ut_read_line(char *buffer, u32 buffer_length, u32 *bytes_read);
  76. static void os_enter_line_edit_mode(void);
  77. static void os_exit_line_edit_mode(void);
  78. /******************************************************************************
  79. *
  80. * FUNCTION: os_enter_line_edit_mode, os_exit_line_edit_mode
  81. *
  82. * PARAMETERS: None
  83. *
  84. * RETURN: None
  85. *
  86. * DESCRIPTION: Enter/Exit the raw character input mode for the terminal.
  87. *
  88. * Interactive line-editing support for the AML debugger. Used with the
  89. * common/acgetline module.
  90. *
  91. * readline() is not used because of non-portability. It is not available
  92. * on all systems, and if it is, often the package must be manually installed.
  93. *
  94. * Therefore, we use the POSIX tcgetattr/tcsetattr and do the minimal line
  95. * editing that we need in acpi_os_get_line.
  96. *
  97. * If the POSIX tcgetattr/tcsetattr interfaces are unavailable, these
  98. * calls will also work:
  99. * For os_enter_line_edit_mode: system ("stty cbreak -echo")
  100. * For os_exit_line_edit_mode: system ("stty cooked echo")
  101. *
  102. *****************************************************************************/
  103. static void os_enter_line_edit_mode(void)
  104. {
  105. struct termios local_term_attributes;
  106. /* Get and keep the original attributes */
  107. if (tcgetattr(STDIN_FILENO, &original_term_attributes)) {
  108. fprintf(stderr, "Could not get terminal attributes!\n");
  109. return;
  110. }
  111. /* Set the new attributes to enable raw character input */
  112. memcpy(&local_term_attributes, &original_term_attributes,
  113. sizeof(struct termios));
  114. local_term_attributes.c_lflag &= ~(ICANON | ECHO);
  115. local_term_attributes.c_cc[VMIN] = 1;
  116. local_term_attributes.c_cc[VTIME] = 0;
  117. if (tcsetattr(STDIN_FILENO, TCSANOW, &local_term_attributes)) {
  118. fprintf(stderr, "Could not set terminal attributes!\n");
  119. return;
  120. }
  121. term_attributes_were_set = 1;
  122. }
  123. static void os_exit_line_edit_mode(void)
  124. {
  125. if (!term_attributes_were_set) {
  126. return;
  127. }
  128. /* Set terminal attributes back to the original values */
  129. if (tcsetattr(STDIN_FILENO, TCSANOW, &original_term_attributes)) {
  130. fprintf(stderr, "Could not restore terminal attributes!\n");
  131. }
  132. }
  133. #else
  134. /* These functions are not needed for other ACPICA utilities */
  135. #define os_enter_line_edit_mode()
  136. #define os_exit_line_edit_mode()
  137. #endif
  138. /******************************************************************************
  139. *
  140. * FUNCTION: acpi_os_initialize, acpi_os_terminate
  141. *
  142. * PARAMETERS: None
  143. *
  144. * RETURN: Status
  145. *
  146. * DESCRIPTION: Initialize and terminate this module.
  147. *
  148. *****************************************************************************/
  149. acpi_status acpi_os_initialize(void)
  150. {
  151. acpi_status status;
  152. acpi_gbl_output_file = stdout;
  153. os_enter_line_edit_mode();
  154. status = acpi_os_create_lock(&acpi_gbl_print_lock);
  155. if (ACPI_FAILURE(status)) {
  156. return (status);
  157. }
  158. return (AE_OK);
  159. }
  160. acpi_status acpi_os_terminate(void)
  161. {
  162. os_exit_line_edit_mode();
  163. return (AE_OK);
  164. }
  165. #ifndef ACPI_USE_NATIVE_RSDP_POINTER
  166. /******************************************************************************
  167. *
  168. * FUNCTION: acpi_os_get_root_pointer
  169. *
  170. * PARAMETERS: None
  171. *
  172. * RETURN: RSDP physical address
  173. *
  174. * DESCRIPTION: Gets the ACPI root pointer (RSDP)
  175. *
  176. *****************************************************************************/
  177. acpi_physical_address acpi_os_get_root_pointer(void)
  178. {
  179. return (0);
  180. }
  181. #endif
  182. /******************************************************************************
  183. *
  184. * FUNCTION: acpi_os_predefined_override
  185. *
  186. * PARAMETERS: init_val - Initial value of the predefined object
  187. * new_val - The new value for the object
  188. *
  189. * RETURN: Status, pointer to value. Null pointer returned if not
  190. * overriding.
  191. *
  192. * DESCRIPTION: Allow the OS to override predefined names
  193. *
  194. *****************************************************************************/
  195. acpi_status
  196. acpi_os_predefined_override(const struct acpi_predefined_names * init_val,
  197. acpi_string * new_val)
  198. {
  199. if (!init_val || !new_val) {
  200. return (AE_BAD_PARAMETER);
  201. }
  202. *new_val = NULL;
  203. return (AE_OK);
  204. }
  205. /******************************************************************************
  206. *
  207. * FUNCTION: acpi_os_table_override
  208. *
  209. * PARAMETERS: existing_table - Header of current table (probably
  210. * firmware)
  211. * new_table - Where an entire new table is returned.
  212. *
  213. * RETURN: Status, pointer to new table. Null pointer returned if no
  214. * table is available to override
  215. *
  216. * DESCRIPTION: Return a different version of a table if one is available
  217. *
  218. *****************************************************************************/
  219. acpi_status
  220. acpi_os_table_override(struct acpi_table_header * existing_table,
  221. struct acpi_table_header ** new_table)
  222. {
  223. if (!existing_table || !new_table) {
  224. return (AE_BAD_PARAMETER);
  225. }
  226. *new_table = NULL;
  227. #ifdef ACPI_EXEC_APP
  228. ae_table_override(existing_table, new_table);
  229. return (AE_OK);
  230. #else
  231. return (AE_NO_ACPI_TABLES);
  232. #endif
  233. }
  234. /******************************************************************************
  235. *
  236. * FUNCTION: acpi_os_physical_table_override
  237. *
  238. * PARAMETERS: existing_table - Header of current table (probably firmware)
  239. * new_address - Where new table address is returned
  240. * (Physical address)
  241. * new_table_length - Where new table length is returned
  242. *
  243. * RETURN: Status, address/length of new table. Null pointer returned
  244. * if no table is available to override.
  245. *
  246. * DESCRIPTION: Returns AE_SUPPORT, function not used in user space.
  247. *
  248. *****************************************************************************/
  249. acpi_status
  250. acpi_os_physical_table_override(struct acpi_table_header * existing_table,
  251. acpi_physical_address * new_address,
  252. u32 *new_table_length)
  253. {
  254. return (AE_SUPPORT);
  255. }
  256. /******************************************************************************
  257. *
  258. * FUNCTION: acpi_os_redirect_output
  259. *
  260. * PARAMETERS: destination - An open file handle/pointer
  261. *
  262. * RETURN: None
  263. *
  264. * DESCRIPTION: Causes redirect of acpi_os_printf and acpi_os_vprintf
  265. *
  266. *****************************************************************************/
  267. void acpi_os_redirect_output(void *destination)
  268. {
  269. acpi_gbl_output_file = destination;
  270. }
  271. /******************************************************************************
  272. *
  273. * FUNCTION: acpi_os_printf
  274. *
  275. * PARAMETERS: fmt, ... - Standard printf format
  276. *
  277. * RETURN: None
  278. *
  279. * DESCRIPTION: Formatted output. Note: very similar to acpi_os_vprintf
  280. * (performance), changes should be tracked in both functions.
  281. *
  282. *****************************************************************************/
  283. void ACPI_INTERNAL_VAR_XFACE acpi_os_printf(const char *fmt, ...)
  284. {
  285. va_list args;
  286. u8 flags;
  287. flags = acpi_gbl_db_output_flags;
  288. if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
  289. /* Output is directable to either a file (if open) or the console */
  290. if (acpi_gbl_debug_file) {
  291. /* Output file is open, send the output there */
  292. va_start(args, fmt);
  293. vfprintf(acpi_gbl_debug_file, fmt, args);
  294. va_end(args);
  295. } else {
  296. /* No redirection, send output to console (once only!) */
  297. flags |= ACPI_DB_CONSOLE_OUTPUT;
  298. }
  299. }
  300. if (flags & ACPI_DB_CONSOLE_OUTPUT) {
  301. va_start(args, fmt);
  302. vfprintf(acpi_gbl_output_file, fmt, args);
  303. va_end(args);
  304. }
  305. }
  306. /******************************************************************************
  307. *
  308. * FUNCTION: acpi_os_vprintf
  309. *
  310. * PARAMETERS: fmt - Standard printf format
  311. * args - Argument list
  312. *
  313. * RETURN: None
  314. *
  315. * DESCRIPTION: Formatted output with argument list pointer. Note: very
  316. * similar to acpi_os_printf, changes should be tracked in both
  317. * functions.
  318. *
  319. *****************************************************************************/
  320. void acpi_os_vprintf(const char *fmt, va_list args)
  321. {
  322. u8 flags;
  323. char buffer[ACPI_VPRINTF_BUFFER_SIZE];
  324. /*
  325. * We build the output string in a local buffer because we may be
  326. * outputting the buffer twice. Using vfprintf is problematic because
  327. * some implementations modify the args pointer/structure during
  328. * execution. Thus, we use the local buffer for portability.
  329. *
  330. * Note: Since this module is intended for use by the various ACPICA
  331. * utilities/applications, we can safely declare the buffer on the stack.
  332. * Also, This function is used for relatively small error messages only.
  333. */
  334. vsnprintf(buffer, ACPI_VPRINTF_BUFFER_SIZE, fmt, args);
  335. flags = acpi_gbl_db_output_flags;
  336. if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
  337. /* Output is directable to either a file (if open) or the console */
  338. if (acpi_gbl_debug_file) {
  339. /* Output file is open, send the output there */
  340. fputs(buffer, acpi_gbl_debug_file);
  341. } else {
  342. /* No redirection, send output to console (once only!) */
  343. flags |= ACPI_DB_CONSOLE_OUTPUT;
  344. }
  345. }
  346. if (flags & ACPI_DB_CONSOLE_OUTPUT) {
  347. fputs(buffer, acpi_gbl_output_file);
  348. }
  349. }
  350. #ifndef ACPI_EXEC_APP
  351. /******************************************************************************
  352. *
  353. * FUNCTION: acpi_os_get_line
  354. *
  355. * PARAMETERS: buffer - Where to return the command line
  356. * buffer_length - Maximum length of Buffer
  357. * bytes_read - Where the actual byte count is returned
  358. *
  359. * RETURN: Status and actual bytes read
  360. *
  361. * DESCRIPTION: Get the next input line from the terminal. NOTE: For the
  362. * acpi_exec utility, we use the acgetline module instead to
  363. * provide line-editing and history support.
  364. *
  365. *****************************************************************************/
  366. acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
  367. {
  368. int input_char;
  369. u32 end_of_line;
  370. /* Standard acpi_os_get_line for all utilities except acpi_exec */
  371. for (end_of_line = 0;; end_of_line++) {
  372. if (end_of_line >= buffer_length) {
  373. return (AE_BUFFER_OVERFLOW);
  374. }
  375. if ((input_char = getchar()) == EOF) {
  376. return (AE_ERROR);
  377. }
  378. if (!input_char || input_char == _ASCII_NEWLINE) {
  379. break;
  380. }
  381. buffer[end_of_line] = (char)input_char;
  382. }
  383. /* Null terminate the buffer */
  384. buffer[end_of_line] = 0;
  385. /* Return the number of bytes in the string */
  386. if (bytes_read) {
  387. *bytes_read = end_of_line;
  388. }
  389. return (AE_OK);
  390. }
  391. #endif
  392. #ifndef ACPI_USE_NATIVE_MEMORY_MAPPING
  393. /******************************************************************************
  394. *
  395. * FUNCTION: acpi_os_map_memory
  396. *
  397. * PARAMETERS: where - Physical address of memory to be mapped
  398. * length - How much memory to map
  399. *
  400. * RETURN: Pointer to mapped memory. Null on error.
  401. *
  402. * DESCRIPTION: Map physical memory into caller's address space
  403. *
  404. *****************************************************************************/
  405. void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)
  406. {
  407. return (ACPI_TO_POINTER((acpi_size) where));
  408. }
  409. /******************************************************************************
  410. *
  411. * FUNCTION: acpi_os_unmap_memory
  412. *
  413. * PARAMETERS: where - Logical address of memory to be unmapped
  414. * length - How much memory to unmap
  415. *
  416. * RETURN: None.
  417. *
  418. * DESCRIPTION: Delete a previously created mapping. Where and Length must
  419. * correspond to a previous mapping exactly.
  420. *
  421. *****************************************************************************/
  422. void acpi_os_unmap_memory(void *where, acpi_size length)
  423. {
  424. return;
  425. }
  426. #endif
  427. /******************************************************************************
  428. *
  429. * FUNCTION: acpi_os_allocate
  430. *
  431. * PARAMETERS: size - Amount to allocate, in bytes
  432. *
  433. * RETURN: Pointer to the new allocation. Null on error.
  434. *
  435. * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
  436. *
  437. *****************************************************************************/
  438. void *acpi_os_allocate(acpi_size size)
  439. {
  440. void *mem;
  441. mem = (void *)malloc((size_t) size);
  442. return (mem);
  443. }
  444. #ifdef USE_NATIVE_ALLOCATE_ZEROED
  445. /******************************************************************************
  446. *
  447. * FUNCTION: acpi_os_allocate_zeroed
  448. *
  449. * PARAMETERS: size - Amount to allocate, in bytes
  450. *
  451. * RETURN: Pointer to the new allocation. Null on error.
  452. *
  453. * DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.
  454. *
  455. *****************************************************************************/
  456. void *acpi_os_allocate_zeroed(acpi_size size)
  457. {
  458. void *mem;
  459. mem = (void *)calloc(1, (size_t) size);
  460. return (mem);
  461. }
  462. #endif
  463. /******************************************************************************
  464. *
  465. * FUNCTION: acpi_os_free
  466. *
  467. * PARAMETERS: mem - Pointer to previously allocated memory
  468. *
  469. * RETURN: None.
  470. *
  471. * DESCRIPTION: Free memory allocated via acpi_os_allocate
  472. *
  473. *****************************************************************************/
  474. void acpi_os_free(void *mem)
  475. {
  476. free(mem);
  477. }
  478. #ifdef ACPI_SINGLE_THREADED
  479. /******************************************************************************
  480. *
  481. * FUNCTION: Semaphore stub functions
  482. *
  483. * DESCRIPTION: Stub functions used for single-thread applications that do
  484. * not require semaphore synchronization. Full implementations
  485. * of these functions appear after the stubs.
  486. *
  487. *****************************************************************************/
  488. acpi_status
  489. acpi_os_create_semaphore(u32 max_units,
  490. u32 initial_units, acpi_handle * out_handle)
  491. {
  492. *out_handle = (acpi_handle) 1;
  493. return (AE_OK);
  494. }
  495. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  496. {
  497. return (AE_OK);
  498. }
  499. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  500. {
  501. return (AE_OK);
  502. }
  503. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  504. {
  505. return (AE_OK);
  506. }
  507. #else
  508. /******************************************************************************
  509. *
  510. * FUNCTION: acpi_os_create_semaphore
  511. *
  512. * PARAMETERS: initial_units - Units to be assigned to the new semaphore
  513. * out_handle - Where a handle will be returned
  514. *
  515. * RETURN: Status
  516. *
  517. * DESCRIPTION: Create an OS semaphore
  518. *
  519. *****************************************************************************/
  520. acpi_status
  521. acpi_os_create_semaphore(u32 max_units,
  522. u32 initial_units, acpi_handle * out_handle)
  523. {
  524. sem_t *sem;
  525. if (!out_handle) {
  526. return (AE_BAD_PARAMETER);
  527. }
  528. #ifdef __APPLE__
  529. {
  530. char *semaphore_name = tmpnam(NULL);
  531. sem =
  532. sem_open(semaphore_name, O_EXCL | O_CREAT, 0755,
  533. initial_units);
  534. if (!sem) {
  535. return (AE_NO_MEMORY);
  536. }
  537. sem_unlink(semaphore_name); /* This just deletes the name */
  538. }
  539. #else
  540. sem = acpi_os_allocate(sizeof(sem_t));
  541. if (!sem) {
  542. return (AE_NO_MEMORY);
  543. }
  544. if (sem_init(sem, 0, initial_units) == -1) {
  545. acpi_os_free(sem);
  546. return (AE_BAD_PARAMETER);
  547. }
  548. #endif
  549. *out_handle = (acpi_handle) sem;
  550. return (AE_OK);
  551. }
  552. /******************************************************************************
  553. *
  554. * FUNCTION: acpi_os_delete_semaphore
  555. *
  556. * PARAMETERS: handle - Handle returned by acpi_os_create_semaphore
  557. *
  558. * RETURN: Status
  559. *
  560. * DESCRIPTION: Delete an OS semaphore
  561. *
  562. *****************************************************************************/
  563. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  564. {
  565. sem_t *sem = (sem_t *) handle;
  566. if (!sem) {
  567. return (AE_BAD_PARAMETER);
  568. }
  569. if (sem_destroy(sem) == -1) {
  570. return (AE_BAD_PARAMETER);
  571. }
  572. return (AE_OK);
  573. }
  574. /******************************************************************************
  575. *
  576. * FUNCTION: acpi_os_wait_semaphore
  577. *
  578. * PARAMETERS: handle - Handle returned by acpi_os_create_semaphore
  579. * units - How many units to wait for
  580. * msec_timeout - How long to wait (milliseconds)
  581. *
  582. * RETURN: Status
  583. *
  584. * DESCRIPTION: Wait for units
  585. *
  586. *****************************************************************************/
  587. acpi_status
  588. acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 msec_timeout)
  589. {
  590. acpi_status status = AE_OK;
  591. sem_t *sem = (sem_t *) handle;
  592. #ifndef ACPI_USE_ALTERNATE_TIMEOUT
  593. struct timespec time;
  594. int ret_val;
  595. #endif
  596. if (!sem) {
  597. return (AE_BAD_PARAMETER);
  598. }
  599. switch (msec_timeout) {
  600. /*
  601. * No Wait:
  602. * --------
  603. * A zero timeout value indicates that we shouldn't wait - just
  604. * acquire the semaphore if available otherwise return AE_TIME
  605. * (a.k.a. 'would block').
  606. */
  607. case 0:
  608. if (sem_trywait(sem) == -1) {
  609. status = (AE_TIME);
  610. }
  611. break;
  612. /* Wait Indefinitely */
  613. case ACPI_WAIT_FOREVER:
  614. if (sem_wait(sem)) {
  615. status = (AE_TIME);
  616. }
  617. break;
  618. /* Wait with msec_timeout */
  619. default:
  620. #ifdef ACPI_USE_ALTERNATE_TIMEOUT
  621. /*
  622. * Alternate timeout mechanism for environments where
  623. * sem_timedwait is not available or does not work properly.
  624. */
  625. while (msec_timeout) {
  626. if (sem_trywait(sem) == 0) {
  627. /* Got the semaphore */
  628. return (AE_OK);
  629. }
  630. if (msec_timeout >= 10) {
  631. msec_timeout -= 10;
  632. usleep(10 * ACPI_USEC_PER_MSEC); /* ten milliseconds */
  633. } else {
  634. msec_timeout--;
  635. usleep(ACPI_USEC_PER_MSEC); /* one millisecond */
  636. }
  637. }
  638. status = (AE_TIME);
  639. #else
  640. /*
  641. * The interface to sem_timedwait is an absolute time, so we need to
  642. * get the current time, then add in the millisecond Timeout value.
  643. */
  644. if (clock_gettime(CLOCK_REALTIME, &time) == -1) {
  645. perror("clock_gettime");
  646. return (AE_TIME);
  647. }
  648. time.tv_sec += (msec_timeout / ACPI_MSEC_PER_SEC);
  649. time.tv_nsec +=
  650. ((msec_timeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);
  651. /* Handle nanosecond overflow (field must be less than one second) */
  652. if (time.tv_nsec >= ACPI_NSEC_PER_SEC) {
  653. time.tv_sec += (time.tv_nsec / ACPI_NSEC_PER_SEC);
  654. time.tv_nsec = (time.tv_nsec % ACPI_NSEC_PER_SEC);
  655. }
  656. while (((ret_val = sem_timedwait(sem, &time)) == -1)
  657. && (errno == EINTR)) {
  658. continue;
  659. }
  660. if (ret_val != 0) {
  661. if (errno != ETIMEDOUT) {
  662. perror("sem_timedwait");
  663. }
  664. status = (AE_TIME);
  665. }
  666. #endif
  667. break;
  668. }
  669. return (status);
  670. }
  671. /******************************************************************************
  672. *
  673. * FUNCTION: acpi_os_signal_semaphore
  674. *
  675. * PARAMETERS: handle - Handle returned by acpi_os_create_semaphore
  676. * units - Number of units to send
  677. *
  678. * RETURN: Status
  679. *
  680. * DESCRIPTION: Send units
  681. *
  682. *****************************************************************************/
  683. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  684. {
  685. sem_t *sem = (sem_t *) handle;
  686. if (!sem) {
  687. return (AE_BAD_PARAMETER);
  688. }
  689. if (sem_post(sem) == -1) {
  690. return (AE_LIMIT);
  691. }
  692. return (AE_OK);
  693. }
  694. #endif /* ACPI_SINGLE_THREADED */
  695. /******************************************************************************
  696. *
  697. * FUNCTION: Spinlock interfaces
  698. *
  699. * DESCRIPTION: Map these interfaces to semaphore interfaces
  700. *
  701. *****************************************************************************/
  702. acpi_status acpi_os_create_lock(acpi_spinlock * out_handle)
  703. {
  704. return (acpi_os_create_semaphore(1, 1, out_handle));
  705. }
  706. void acpi_os_delete_lock(acpi_spinlock handle)
  707. {
  708. acpi_os_delete_semaphore(handle);
  709. }
  710. acpi_cpu_flags acpi_os_acquire_lock(acpi_handle handle)
  711. {
  712. acpi_os_wait_semaphore(handle, 1, 0xFFFF);
  713. return (0);
  714. }
  715. void acpi_os_release_lock(acpi_spinlock handle, acpi_cpu_flags flags)
  716. {
  717. acpi_os_signal_semaphore(handle, 1);
  718. }
  719. /******************************************************************************
  720. *
  721. * FUNCTION: acpi_os_install_interrupt_handler
  722. *
  723. * PARAMETERS: interrupt_number - Level handler should respond to.
  724. * isr - Address of the ACPI interrupt handler
  725. * except_ptr - Where status is returned
  726. *
  727. * RETURN: Handle to the newly installed handler.
  728. *
  729. * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
  730. * OS-independent handler.
  731. *
  732. *****************************************************************************/
  733. u32
  734. acpi_os_install_interrupt_handler(u32 interrupt_number,
  735. acpi_osd_handler service_routine,
  736. void *context)
  737. {
  738. return (AE_OK);
  739. }
  740. /******************************************************************************
  741. *
  742. * FUNCTION: acpi_os_remove_interrupt_handler
  743. *
  744. * PARAMETERS: handle - Returned when handler was installed
  745. *
  746. * RETURN: Status
  747. *
  748. * DESCRIPTION: Uninstalls an interrupt handler.
  749. *
  750. *****************************************************************************/
  751. acpi_status
  752. acpi_os_remove_interrupt_handler(u32 interrupt_number,
  753. acpi_osd_handler service_routine)
  754. {
  755. return (AE_OK);
  756. }
  757. /******************************************************************************
  758. *
  759. * FUNCTION: acpi_os_stall
  760. *
  761. * PARAMETERS: microseconds - Time to sleep
  762. *
  763. * RETURN: Blocks until sleep is completed.
  764. *
  765. * DESCRIPTION: Sleep at microsecond granularity
  766. *
  767. *****************************************************************************/
  768. void acpi_os_stall(u32 microseconds)
  769. {
  770. if (microseconds) {
  771. usleep(microseconds);
  772. }
  773. }
  774. /******************************************************************************
  775. *
  776. * FUNCTION: acpi_os_sleep
  777. *
  778. * PARAMETERS: milliseconds - Time to sleep
  779. *
  780. * RETURN: Blocks until sleep is completed.
  781. *
  782. * DESCRIPTION: Sleep at millisecond granularity
  783. *
  784. *****************************************************************************/
  785. void acpi_os_sleep(u64 milliseconds)
  786. {
  787. /* Sleep for whole seconds */
  788. sleep(milliseconds / ACPI_MSEC_PER_SEC);
  789. /*
  790. * Sleep for remaining microseconds.
  791. * Arg to usleep() is in usecs and must be less than 1,000,000 (1 second).
  792. */
  793. usleep((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);
  794. }
  795. /******************************************************************************
  796. *
  797. * FUNCTION: acpi_os_get_timer
  798. *
  799. * PARAMETERS: None
  800. *
  801. * RETURN: Current time in 100 nanosecond units
  802. *
  803. * DESCRIPTION: Get the current system time
  804. *
  805. *****************************************************************************/
  806. u64 acpi_os_get_timer(void)
  807. {
  808. struct timeval time;
  809. /* This timer has sufficient resolution for user-space application code */
  810. gettimeofday(&time, NULL);
  811. /* (Seconds * 10^7 = 100ns(10^-7)) + (Microseconds(10^-6) * 10^1 = 100ns) */
  812. return (((u64)time.tv_sec * ACPI_100NSEC_PER_SEC) +
  813. ((u64)time.tv_usec * ACPI_100NSEC_PER_USEC));
  814. }
  815. /******************************************************************************
  816. *
  817. * FUNCTION: acpi_os_read_pci_configuration
  818. *
  819. * PARAMETERS: pci_id - Seg/Bus/Dev
  820. * pci_register - Device Register
  821. * value - Buffer where value is placed
  822. * width - Number of bits
  823. *
  824. * RETURN: Status
  825. *
  826. * DESCRIPTION: Read data from PCI configuration space
  827. *
  828. *****************************************************************************/
  829. acpi_status
  830. acpi_os_read_pci_configuration(struct acpi_pci_id *pci_id,
  831. u32 pci_register, u64 *value, u32 width)
  832. {
  833. *value = 0;
  834. return (AE_OK);
  835. }
  836. /******************************************************************************
  837. *
  838. * FUNCTION: acpi_os_write_pci_configuration
  839. *
  840. * PARAMETERS: pci_id - Seg/Bus/Dev
  841. * pci_register - Device Register
  842. * value - Value to be written
  843. * width - Number of bits
  844. *
  845. * RETURN: Status.
  846. *
  847. * DESCRIPTION: Write data to PCI configuration space
  848. *
  849. *****************************************************************************/
  850. acpi_status
  851. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id,
  852. u32 pci_register, u64 value, u32 width)
  853. {
  854. return (AE_OK);
  855. }
  856. /******************************************************************************
  857. *
  858. * FUNCTION: acpi_os_read_port
  859. *
  860. * PARAMETERS: address - Address of I/O port/register to read
  861. * value - Where value is placed
  862. * width - Number of bits
  863. *
  864. * RETURN: Value read from port
  865. *
  866. * DESCRIPTION: Read data from an I/O port or register
  867. *
  868. *****************************************************************************/
  869. acpi_status acpi_os_read_port(acpi_io_address address, u32 *value, u32 width)
  870. {
  871. switch (width) {
  872. case 8:
  873. *value = 0xFF;
  874. break;
  875. case 16:
  876. *value = 0xFFFF;
  877. break;
  878. case 32:
  879. *value = 0xFFFFFFFF;
  880. break;
  881. default:
  882. return (AE_BAD_PARAMETER);
  883. }
  884. return (AE_OK);
  885. }
  886. /******************************************************************************
  887. *
  888. * FUNCTION: acpi_os_write_port
  889. *
  890. * PARAMETERS: address - Address of I/O port/register to write
  891. * value - Value to write
  892. * width - Number of bits
  893. *
  894. * RETURN: None
  895. *
  896. * DESCRIPTION: Write data to an I/O port or register
  897. *
  898. *****************************************************************************/
  899. acpi_status acpi_os_write_port(acpi_io_address address, u32 value, u32 width)
  900. {
  901. return (AE_OK);
  902. }
  903. /******************************************************************************
  904. *
  905. * FUNCTION: acpi_os_read_memory
  906. *
  907. * PARAMETERS: address - Physical Memory Address to read
  908. * value - Where value is placed
  909. * width - Number of bits (8,16,32, or 64)
  910. *
  911. * RETURN: Value read from physical memory address. Always returned
  912. * as a 64-bit integer, regardless of the read width.
  913. *
  914. * DESCRIPTION: Read data from a physical memory address
  915. *
  916. *****************************************************************************/
  917. acpi_status
  918. acpi_os_read_memory(acpi_physical_address address, u64 *value, u32 width)
  919. {
  920. switch (width) {
  921. case 8:
  922. case 16:
  923. case 32:
  924. case 64:
  925. *value = 0;
  926. break;
  927. default:
  928. return (AE_BAD_PARAMETER);
  929. }
  930. return (AE_OK);
  931. }
  932. /******************************************************************************
  933. *
  934. * FUNCTION: acpi_os_write_memory
  935. *
  936. * PARAMETERS: address - Physical Memory Address to write
  937. * value - Value to write
  938. * width - Number of bits (8,16,32, or 64)
  939. *
  940. * RETURN: None
  941. *
  942. * DESCRIPTION: Write data to a physical memory address
  943. *
  944. *****************************************************************************/
  945. acpi_status
  946. acpi_os_write_memory(acpi_physical_address address, u64 value, u32 width)
  947. {
  948. return (AE_OK);
  949. }
  950. /******************************************************************************
  951. *
  952. * FUNCTION: acpi_os_readable
  953. *
  954. * PARAMETERS: pointer - Area to be verified
  955. * length - Size of area
  956. *
  957. * RETURN: TRUE if readable for entire length
  958. *
  959. * DESCRIPTION: Verify that a pointer is valid for reading
  960. *
  961. *****************************************************************************/
  962. u8 acpi_os_readable(void *pointer, acpi_size length)
  963. {
  964. return (TRUE);
  965. }
  966. /******************************************************************************
  967. *
  968. * FUNCTION: acpi_os_writable
  969. *
  970. * PARAMETERS: pointer - Area to be verified
  971. * length - Size of area
  972. *
  973. * RETURN: TRUE if writable for entire length
  974. *
  975. * DESCRIPTION: Verify that a pointer is valid for writing
  976. *
  977. *****************************************************************************/
  978. u8 acpi_os_writable(void *pointer, acpi_size length)
  979. {
  980. return (TRUE);
  981. }
  982. /******************************************************************************
  983. *
  984. * FUNCTION: acpi_os_signal
  985. *
  986. * PARAMETERS: function - ACPI A signal function code
  987. * info - Pointer to function-dependent structure
  988. *
  989. * RETURN: Status
  990. *
  991. * DESCRIPTION: Miscellaneous functions. Example implementation only.
  992. *
  993. *****************************************************************************/
  994. acpi_status acpi_os_signal(u32 function, void *info)
  995. {
  996. switch (function) {
  997. case ACPI_SIGNAL_FATAL:
  998. break;
  999. case ACPI_SIGNAL_BREAKPOINT:
  1000. break;
  1001. default:
  1002. break;
  1003. }
  1004. return (AE_OK);
  1005. }
  1006. /* Optional multi-thread support */
  1007. #ifndef ACPI_SINGLE_THREADED
  1008. /******************************************************************************
  1009. *
  1010. * FUNCTION: acpi_os_get_thread_id
  1011. *
  1012. * PARAMETERS: None
  1013. *
  1014. * RETURN: Id of the running thread
  1015. *
  1016. * DESCRIPTION: Get the ID of the current (running) thread
  1017. *
  1018. *****************************************************************************/
  1019. acpi_thread_id acpi_os_get_thread_id(void)
  1020. {
  1021. pthread_t thread;
  1022. thread = pthread_self();
  1023. return (ACPI_CAST_PTHREAD_T(thread));
  1024. }
  1025. /******************************************************************************
  1026. *
  1027. * FUNCTION: acpi_os_execute
  1028. *
  1029. * PARAMETERS: type - Type of execution
  1030. * function - Address of the function to execute
  1031. * context - Passed as a parameter to the function
  1032. *
  1033. * RETURN: Status.
  1034. *
  1035. * DESCRIPTION: Execute a new thread
  1036. *
  1037. *****************************************************************************/
  1038. acpi_status
  1039. acpi_os_execute(acpi_execute_type type,
  1040. acpi_osd_exec_callback function, void *context)
  1041. {
  1042. pthread_t thread;
  1043. int ret;
  1044. ret =
  1045. pthread_create(&thread, NULL, (PTHREAD_CALLBACK) function, context);
  1046. if (ret) {
  1047. acpi_os_printf("Create thread failed");
  1048. }
  1049. return (0);
  1050. }
  1051. #else /* ACPI_SINGLE_THREADED */
  1052. acpi_thread_id acpi_os_get_thread_id(void)
  1053. {
  1054. return (1);
  1055. }
  1056. acpi_status
  1057. acpi_os_execute(acpi_execute_type type,
  1058. acpi_osd_exec_callback function, void *context)
  1059. {
  1060. function(context);
  1061. return (AE_OK);
  1062. }
  1063. #endif /* ACPI_SINGLE_THREADED */
  1064. /******************************************************************************
  1065. *
  1066. * FUNCTION: acpi_os_wait_events_complete
  1067. *
  1068. * PARAMETERS: None
  1069. *
  1070. * RETURN: None
  1071. *
  1072. * DESCRIPTION: Wait for all asynchronous events to complete. This
  1073. * implementation does nothing.
  1074. *
  1075. *****************************************************************************/
  1076. void acpi_os_wait_events_complete(void)
  1077. {
  1078. return;
  1079. }