osunixxf.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. static int semaphore_count = 0;
  535. char semaphore_name[32];
  536. snprintf(semaphore_name, sizeof(semaphore_name), "acpi_sem_%d",
  537. semaphore_count++);
  538. printf("%s\n", semaphore_name);
  539. sem =
  540. sem_open(semaphore_name, O_EXCL | O_CREAT, 0755,
  541. initial_units);
  542. if (!sem) {
  543. return (AE_NO_MEMORY);
  544. }
  545. sem_unlink(semaphore_name); /* This just deletes the name */
  546. }
  547. #else
  548. sem = acpi_os_allocate(sizeof(sem_t));
  549. if (!sem) {
  550. return (AE_NO_MEMORY);
  551. }
  552. if (sem_init(sem, 0, initial_units) == -1) {
  553. acpi_os_free(sem);
  554. return (AE_BAD_PARAMETER);
  555. }
  556. #endif
  557. *out_handle = (acpi_handle)sem;
  558. return (AE_OK);
  559. }
  560. /******************************************************************************
  561. *
  562. * FUNCTION: acpi_os_delete_semaphore
  563. *
  564. * PARAMETERS: handle - Handle returned by acpi_os_create_semaphore
  565. *
  566. * RETURN: Status
  567. *
  568. * DESCRIPTION: Delete an OS semaphore
  569. *
  570. *****************************************************************************/
  571. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  572. {
  573. sem_t *sem = (sem_t *) handle;
  574. if (!sem) {
  575. return (AE_BAD_PARAMETER);
  576. }
  577. #ifdef __APPLE__
  578. if (sem_close(sem) == -1) {
  579. return (AE_BAD_PARAMETER);
  580. }
  581. #else
  582. if (sem_destroy(sem) == -1) {
  583. return (AE_BAD_PARAMETER);
  584. }
  585. #endif
  586. return (AE_OK);
  587. }
  588. /******************************************************************************
  589. *
  590. * FUNCTION: acpi_os_wait_semaphore
  591. *
  592. * PARAMETERS: handle - Handle returned by acpi_os_create_semaphore
  593. * units - How many units to wait for
  594. * msec_timeout - How long to wait (milliseconds)
  595. *
  596. * RETURN: Status
  597. *
  598. * DESCRIPTION: Wait for units
  599. *
  600. *****************************************************************************/
  601. acpi_status
  602. acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 msec_timeout)
  603. {
  604. acpi_status status = AE_OK;
  605. sem_t *sem = (sem_t *) handle;
  606. #ifndef ACPI_USE_ALTERNATE_TIMEOUT
  607. struct timespec time;
  608. int ret_val;
  609. #endif
  610. if (!sem) {
  611. return (AE_BAD_PARAMETER);
  612. }
  613. switch (msec_timeout) {
  614. /*
  615. * No Wait:
  616. * --------
  617. * A zero timeout value indicates that we shouldn't wait - just
  618. * acquire the semaphore if available otherwise return AE_TIME
  619. * (a.k.a. 'would block').
  620. */
  621. case 0:
  622. if (sem_trywait(sem) == -1) {
  623. status = (AE_TIME);
  624. }
  625. break;
  626. /* Wait Indefinitely */
  627. case ACPI_WAIT_FOREVER:
  628. if (sem_wait(sem)) {
  629. status = (AE_TIME);
  630. }
  631. break;
  632. /* Wait with msec_timeout */
  633. default:
  634. #ifdef ACPI_USE_ALTERNATE_TIMEOUT
  635. /*
  636. * Alternate timeout mechanism for environments where
  637. * sem_timedwait is not available or does not work properly.
  638. */
  639. while (msec_timeout) {
  640. if (sem_trywait(sem) == 0) {
  641. /* Got the semaphore */
  642. return (AE_OK);
  643. }
  644. if (msec_timeout >= 10) {
  645. msec_timeout -= 10;
  646. usleep(10 * ACPI_USEC_PER_MSEC); /* ten milliseconds */
  647. } else {
  648. msec_timeout--;
  649. usleep(ACPI_USEC_PER_MSEC); /* one millisecond */
  650. }
  651. }
  652. status = (AE_TIME);
  653. #else
  654. /*
  655. * The interface to sem_timedwait is an absolute time, so we need to
  656. * get the current time, then add in the millisecond Timeout value.
  657. */
  658. if (clock_gettime(CLOCK_REALTIME, &time) == -1) {
  659. perror("clock_gettime");
  660. return (AE_TIME);
  661. }
  662. time.tv_sec += (msec_timeout / ACPI_MSEC_PER_SEC);
  663. time.tv_nsec +=
  664. ((msec_timeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);
  665. /* Handle nanosecond overflow (field must be less than one second) */
  666. if (time.tv_nsec >= ACPI_NSEC_PER_SEC) {
  667. time.tv_sec += (time.tv_nsec / ACPI_NSEC_PER_SEC);
  668. time.tv_nsec = (time.tv_nsec % ACPI_NSEC_PER_SEC);
  669. }
  670. while (((ret_val = sem_timedwait(sem, &time)) == -1)
  671. && (errno == EINTR)) {
  672. continue;
  673. }
  674. if (ret_val != 0) {
  675. if (errno != ETIMEDOUT) {
  676. perror("sem_timedwait");
  677. }
  678. status = (AE_TIME);
  679. }
  680. #endif
  681. break;
  682. }
  683. return (status);
  684. }
  685. /******************************************************************************
  686. *
  687. * FUNCTION: acpi_os_signal_semaphore
  688. *
  689. * PARAMETERS: handle - Handle returned by acpi_os_create_semaphore
  690. * units - Number of units to send
  691. *
  692. * RETURN: Status
  693. *
  694. * DESCRIPTION: Send units
  695. *
  696. *****************************************************************************/
  697. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  698. {
  699. sem_t *sem = (sem_t *) handle;
  700. if (!sem) {
  701. return (AE_BAD_PARAMETER);
  702. }
  703. if (sem_post(sem) == -1) {
  704. return (AE_LIMIT);
  705. }
  706. return (AE_OK);
  707. }
  708. #endif /* ACPI_SINGLE_THREADED */
  709. /******************************************************************************
  710. *
  711. * FUNCTION: Spinlock interfaces
  712. *
  713. * DESCRIPTION: Map these interfaces to semaphore interfaces
  714. *
  715. *****************************************************************************/
  716. acpi_status acpi_os_create_lock(acpi_spinlock * out_handle)
  717. {
  718. return (acpi_os_create_semaphore(1, 1, out_handle));
  719. }
  720. void acpi_os_delete_lock(acpi_spinlock handle)
  721. {
  722. acpi_os_delete_semaphore(handle);
  723. }
  724. acpi_cpu_flags acpi_os_acquire_lock(acpi_handle handle)
  725. {
  726. acpi_os_wait_semaphore(handle, 1, 0xFFFF);
  727. return (0);
  728. }
  729. void acpi_os_release_lock(acpi_spinlock handle, acpi_cpu_flags flags)
  730. {
  731. acpi_os_signal_semaphore(handle, 1);
  732. }
  733. /******************************************************************************
  734. *
  735. * FUNCTION: acpi_os_install_interrupt_handler
  736. *
  737. * PARAMETERS: interrupt_number - Level handler should respond to.
  738. * isr - Address of the ACPI interrupt handler
  739. * except_ptr - Where status is returned
  740. *
  741. * RETURN: Handle to the newly installed handler.
  742. *
  743. * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
  744. * OS-independent handler.
  745. *
  746. *****************************************************************************/
  747. u32
  748. acpi_os_install_interrupt_handler(u32 interrupt_number,
  749. acpi_osd_handler service_routine,
  750. void *context)
  751. {
  752. return (AE_OK);
  753. }
  754. /******************************************************************************
  755. *
  756. * FUNCTION: acpi_os_remove_interrupt_handler
  757. *
  758. * PARAMETERS: handle - Returned when handler was installed
  759. *
  760. * RETURN: Status
  761. *
  762. * DESCRIPTION: Uninstalls an interrupt handler.
  763. *
  764. *****************************************************************************/
  765. acpi_status
  766. acpi_os_remove_interrupt_handler(u32 interrupt_number,
  767. acpi_osd_handler service_routine)
  768. {
  769. return (AE_OK);
  770. }
  771. /******************************************************************************
  772. *
  773. * FUNCTION: acpi_os_stall
  774. *
  775. * PARAMETERS: microseconds - Time to sleep
  776. *
  777. * RETURN: Blocks until sleep is completed.
  778. *
  779. * DESCRIPTION: Sleep at microsecond granularity
  780. *
  781. *****************************************************************************/
  782. void acpi_os_stall(u32 microseconds)
  783. {
  784. if (microseconds) {
  785. usleep(microseconds);
  786. }
  787. }
  788. /******************************************************************************
  789. *
  790. * FUNCTION: acpi_os_sleep
  791. *
  792. * PARAMETERS: milliseconds - Time to sleep
  793. *
  794. * RETURN: Blocks until sleep is completed.
  795. *
  796. * DESCRIPTION: Sleep at millisecond granularity
  797. *
  798. *****************************************************************************/
  799. void acpi_os_sleep(u64 milliseconds)
  800. {
  801. /* Sleep for whole seconds */
  802. sleep(milliseconds / ACPI_MSEC_PER_SEC);
  803. /*
  804. * Sleep for remaining microseconds.
  805. * Arg to usleep() is in usecs and must be less than 1,000,000 (1 second).
  806. */
  807. usleep((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);
  808. }
  809. /******************************************************************************
  810. *
  811. * FUNCTION: acpi_os_get_timer
  812. *
  813. * PARAMETERS: None
  814. *
  815. * RETURN: Current time in 100 nanosecond units
  816. *
  817. * DESCRIPTION: Get the current system time
  818. *
  819. *****************************************************************************/
  820. u64 acpi_os_get_timer(void)
  821. {
  822. struct timeval time;
  823. /* This timer has sufficient resolution for user-space application code */
  824. gettimeofday(&time, NULL);
  825. /* (Seconds * 10^7 = 100ns(10^-7)) + (Microseconds(10^-6) * 10^1 = 100ns) */
  826. return (((u64)time.tv_sec * ACPI_100NSEC_PER_SEC) +
  827. ((u64)time.tv_usec * ACPI_100NSEC_PER_USEC));
  828. }
  829. /******************************************************************************
  830. *
  831. * FUNCTION: acpi_os_read_pci_configuration
  832. *
  833. * PARAMETERS: pci_id - Seg/Bus/Dev
  834. * pci_register - Device Register
  835. * value - Buffer where value is placed
  836. * width - Number of bits
  837. *
  838. * RETURN: Status
  839. *
  840. * DESCRIPTION: Read data from PCI configuration space
  841. *
  842. *****************************************************************************/
  843. acpi_status
  844. acpi_os_read_pci_configuration(struct acpi_pci_id *pci_id,
  845. u32 pci_register, u64 *value, u32 width)
  846. {
  847. *value = 0;
  848. return (AE_OK);
  849. }
  850. /******************************************************************************
  851. *
  852. * FUNCTION: acpi_os_write_pci_configuration
  853. *
  854. * PARAMETERS: pci_id - Seg/Bus/Dev
  855. * pci_register - Device Register
  856. * value - Value to be written
  857. * width - Number of bits
  858. *
  859. * RETURN: Status.
  860. *
  861. * DESCRIPTION: Write data to PCI configuration space
  862. *
  863. *****************************************************************************/
  864. acpi_status
  865. acpi_os_write_pci_configuration(struct acpi_pci_id *pci_id,
  866. u32 pci_register, u64 value, u32 width)
  867. {
  868. return (AE_OK);
  869. }
  870. /******************************************************************************
  871. *
  872. * FUNCTION: acpi_os_read_port
  873. *
  874. * PARAMETERS: address - Address of I/O port/register to read
  875. * value - Where value is placed
  876. * width - Number of bits
  877. *
  878. * RETURN: Value read from port
  879. *
  880. * DESCRIPTION: Read data from an I/O port or register
  881. *
  882. *****************************************************************************/
  883. acpi_status acpi_os_read_port(acpi_io_address address, u32 *value, u32 width)
  884. {
  885. switch (width) {
  886. case 8:
  887. *value = 0xFF;
  888. break;
  889. case 16:
  890. *value = 0xFFFF;
  891. break;
  892. case 32:
  893. *value = 0xFFFFFFFF;
  894. break;
  895. default:
  896. return (AE_BAD_PARAMETER);
  897. }
  898. return (AE_OK);
  899. }
  900. /******************************************************************************
  901. *
  902. * FUNCTION: acpi_os_write_port
  903. *
  904. * PARAMETERS: address - Address of I/O port/register to write
  905. * value - Value to write
  906. * width - Number of bits
  907. *
  908. * RETURN: None
  909. *
  910. * DESCRIPTION: Write data to an I/O port or register
  911. *
  912. *****************************************************************************/
  913. acpi_status acpi_os_write_port(acpi_io_address address, u32 value, u32 width)
  914. {
  915. return (AE_OK);
  916. }
  917. /******************************************************************************
  918. *
  919. * FUNCTION: acpi_os_read_memory
  920. *
  921. * PARAMETERS: address - Physical Memory Address to read
  922. * value - Where value is placed
  923. * width - Number of bits (8,16,32, or 64)
  924. *
  925. * RETURN: Value read from physical memory address. Always returned
  926. * as a 64-bit integer, regardless of the read width.
  927. *
  928. * DESCRIPTION: Read data from a physical memory address
  929. *
  930. *****************************************************************************/
  931. acpi_status
  932. acpi_os_read_memory(acpi_physical_address address, u64 *value, u32 width)
  933. {
  934. switch (width) {
  935. case 8:
  936. case 16:
  937. case 32:
  938. case 64:
  939. *value = 0;
  940. break;
  941. default:
  942. return (AE_BAD_PARAMETER);
  943. }
  944. return (AE_OK);
  945. }
  946. /******************************************************************************
  947. *
  948. * FUNCTION: acpi_os_write_memory
  949. *
  950. * PARAMETERS: address - Physical Memory Address to write
  951. * value - Value to write
  952. * width - Number of bits (8,16,32, or 64)
  953. *
  954. * RETURN: None
  955. *
  956. * DESCRIPTION: Write data to a physical memory address
  957. *
  958. *****************************************************************************/
  959. acpi_status
  960. acpi_os_write_memory(acpi_physical_address address, u64 value, u32 width)
  961. {
  962. return (AE_OK);
  963. }
  964. /******************************************************************************
  965. *
  966. * FUNCTION: acpi_os_readable
  967. *
  968. * PARAMETERS: pointer - Area to be verified
  969. * length - Size of area
  970. *
  971. * RETURN: TRUE if readable for entire length
  972. *
  973. * DESCRIPTION: Verify that a pointer is valid for reading
  974. *
  975. *****************************************************************************/
  976. u8 acpi_os_readable(void *pointer, acpi_size length)
  977. {
  978. return (TRUE);
  979. }
  980. /******************************************************************************
  981. *
  982. * FUNCTION: acpi_os_writable
  983. *
  984. * PARAMETERS: pointer - Area to be verified
  985. * length - Size of area
  986. *
  987. * RETURN: TRUE if writable for entire length
  988. *
  989. * DESCRIPTION: Verify that a pointer is valid for writing
  990. *
  991. *****************************************************************************/
  992. u8 acpi_os_writable(void *pointer, acpi_size length)
  993. {
  994. return (TRUE);
  995. }
  996. /******************************************************************************
  997. *
  998. * FUNCTION: acpi_os_signal
  999. *
  1000. * PARAMETERS: function - ACPI A signal function code
  1001. * info - Pointer to function-dependent structure
  1002. *
  1003. * RETURN: Status
  1004. *
  1005. * DESCRIPTION: Miscellaneous functions. Example implementation only.
  1006. *
  1007. *****************************************************************************/
  1008. acpi_status acpi_os_signal(u32 function, void *info)
  1009. {
  1010. switch (function) {
  1011. case ACPI_SIGNAL_FATAL:
  1012. break;
  1013. case ACPI_SIGNAL_BREAKPOINT:
  1014. break;
  1015. default:
  1016. break;
  1017. }
  1018. return (AE_OK);
  1019. }
  1020. /* Optional multi-thread support */
  1021. #ifndef ACPI_SINGLE_THREADED
  1022. /******************************************************************************
  1023. *
  1024. * FUNCTION: acpi_os_get_thread_id
  1025. *
  1026. * PARAMETERS: None
  1027. *
  1028. * RETURN: Id of the running thread
  1029. *
  1030. * DESCRIPTION: Get the ID of the current (running) thread
  1031. *
  1032. *****************************************************************************/
  1033. acpi_thread_id acpi_os_get_thread_id(void)
  1034. {
  1035. pthread_t thread;
  1036. thread = pthread_self();
  1037. return (ACPI_CAST_PTHREAD_T(thread));
  1038. }
  1039. /******************************************************************************
  1040. *
  1041. * FUNCTION: acpi_os_execute
  1042. *
  1043. * PARAMETERS: type - Type of execution
  1044. * function - Address of the function to execute
  1045. * context - Passed as a parameter to the function
  1046. *
  1047. * RETURN: Status.
  1048. *
  1049. * DESCRIPTION: Execute a new thread
  1050. *
  1051. *****************************************************************************/
  1052. acpi_status
  1053. acpi_os_execute(acpi_execute_type type,
  1054. acpi_osd_exec_callback function, void *context)
  1055. {
  1056. pthread_t thread;
  1057. int ret;
  1058. ret =
  1059. pthread_create(&thread, NULL, (PTHREAD_CALLBACK) function, context);
  1060. if (ret) {
  1061. acpi_os_printf("Create thread failed");
  1062. }
  1063. return (0);
  1064. }
  1065. #else /* ACPI_SINGLE_THREADED */
  1066. acpi_thread_id acpi_os_get_thread_id(void)
  1067. {
  1068. return (1);
  1069. }
  1070. acpi_status
  1071. acpi_os_execute(acpi_execute_type type,
  1072. acpi_osd_exec_callback function, void *context)
  1073. {
  1074. function(context);
  1075. return (AE_OK);
  1076. }
  1077. #endif /* ACPI_SINGLE_THREADED */
  1078. /******************************************************************************
  1079. *
  1080. * FUNCTION: acpi_os_wait_events_complete
  1081. *
  1082. * PARAMETERS: None
  1083. *
  1084. * RETURN: None
  1085. *
  1086. * DESCRIPTION: Wait for all asynchronous events to complete. This
  1087. * implementation does nothing.
  1088. *
  1089. *****************************************************************************/
  1090. void acpi_os_wait_events_complete(void)
  1091. {
  1092. return;
  1093. }