osunixxf.c 33 KB

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