osunixxf.c 33 KB

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