osunixxf.c 32 KB

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