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