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 - 2016, 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_redirect_output
 322 *
 323 * PARAMETERS:  destination         - An open file handle/pointer
 324 *
 325 * RETURN:      None
 326 *
 327 * DESCRIPTION: Causes redirect of acpi_os_printf and acpi_os_vprintf
 328 *
 329 *****************************************************************************/
 330
 331void acpi_os_redirect_output(void *destination)
 332{
 333
 334        acpi_gbl_output_file = destination;
 335}
 336
 337/******************************************************************************
 338 *
 339 * FUNCTION:    acpi_os_printf
 340 *
 341 * PARAMETERS:  fmt, ...            - Standard printf format
 342 *
 343 * RETURN:      None
 344 *
 345 * DESCRIPTION: Formatted output. Note: very similar to acpi_os_vprintf
 346 *              (performance), changes should be tracked in both functions.
 347 *
 348 *****************************************************************************/
 349
 350void ACPI_INTERNAL_VAR_XFACE acpi_os_printf(const char *fmt, ...)
 351{
 352        va_list args;
 353        u8 flags;
 354
 355        flags = acpi_gbl_db_output_flags;
 356        if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
 357
 358                /* Output is directable to either a file (if open) or the console */
 359
 360                if (acpi_gbl_debug_file) {
 361
 362                        /* Output file is open, send the output there */
 363
 364                        va_start(args, fmt);
 365                        vfprintf(acpi_gbl_debug_file, fmt, args);
 366                        va_end(args);
 367                } else {
 368                        /* No redirection, send output to console (once only!) */
 369
 370                        flags |= ACPI_DB_CONSOLE_OUTPUT;
 371                }
 372        }
 373
 374        if (flags & ACPI_DB_CONSOLE_OUTPUT) {
 375                va_start(args, fmt);
 376                vfprintf(acpi_gbl_output_file, fmt, args);
 377                va_end(args);
 378        }
 379}
 380
 381/******************************************************************************
 382 *
 383 * FUNCTION:    acpi_os_vprintf
 384 *
 385 * PARAMETERS:  fmt                 - Standard printf format
 386 *              args                - Argument list
 387 *
 388 * RETURN:      None
 389 *
 390 * DESCRIPTION: Formatted output with argument list pointer. Note: very
 391 *              similar to acpi_os_printf, changes should be tracked in both
 392 *              functions.
 393 *
 394 *****************************************************************************/
 395
 396void acpi_os_vprintf(const char *fmt, va_list args)
 397{
 398        u8 flags;
 399        char buffer[ACPI_VPRINTF_BUFFER_SIZE];
 400
 401        /*
 402         * We build the output string in a local buffer because we may be
 403         * outputting the buffer twice. Using vfprintf is problematic because
 404         * some implementations modify the args pointer/structure during
 405         * execution. Thus, we use the local buffer for portability.
 406         *
 407         * Note: Since this module is intended for use by the various ACPICA
 408         * utilities/applications, we can safely declare the buffer on the stack.
 409         * Also, This function is used for relatively small error messages only.
 410         */
 411        vsnprintf(buffer, ACPI_VPRINTF_BUFFER_SIZE, fmt, args);
 412
 413        flags = acpi_gbl_db_output_flags;
 414        if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
 415
 416                /* Output is directable to either a file (if open) or the console */
 417
 418                if (acpi_gbl_debug_file) {
 419
 420                        /* Output file is open, send the output there */
 421
 422                        fputs(buffer, acpi_gbl_debug_file);
 423                } else {
 424                        /* No redirection, send output to console (once only!) */
 425
 426                        flags |= ACPI_DB_CONSOLE_OUTPUT;
 427                }
 428        }
 429
 430        if (flags & ACPI_DB_CONSOLE_OUTPUT) {
 431                fputs(buffer, acpi_gbl_output_file);
 432        }
 433}
 434
 435#ifndef ACPI_EXEC_APP
 436/******************************************************************************
 437 *
 438 * FUNCTION:    acpi_os_get_line
 439 *
 440 * PARAMETERS:  buffer              - Where to return the command line
 441 *              buffer_length       - Maximum length of Buffer
 442 *              bytes_read          - Where the actual byte count is returned
 443 *
 444 * RETURN:      Status and actual bytes read
 445 *
 446 * DESCRIPTION: Get the next input line from the terminal. NOTE: For the
 447 *              acpi_exec utility, we use the acgetline module instead to
 448 *              provide line-editing and history support.
 449 *
 450 *****************************************************************************/
 451
 452acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
 453{
 454        int input_char;
 455        u32 end_of_line;
 456
 457        /* Standard acpi_os_get_line for all utilities except acpi_exec */
 458
 459        for (end_of_line = 0;; end_of_line++) {
 460                if (end_of_line >= buffer_length) {
 461                        return (AE_BUFFER_OVERFLOW);
 462                }
 463
 464                if ((input_char = getchar()) == EOF) {
 465                        return (AE_ERROR);
 466                }
 467
 468                if (!input_char || input_char == _ASCII_NEWLINE) {
 469                        break;
 470                }
 471
 472                buffer[end_of_line] = (char)input_char;
 473        }
 474
 475        /* Null terminate the buffer */
 476
 477        buffer[end_of_line] = 0;
 478
 479        /* Return the number of bytes in the string */
 480
 481        if (bytes_read) {
 482                *bytes_read = end_of_line;
 483        }
 484
 485        return (AE_OK);
 486}
 487#endif
 488
 489#ifndef ACPI_USE_NATIVE_MEMORY_MAPPING
 490/******************************************************************************
 491 *
 492 * FUNCTION:    acpi_os_map_memory
 493 *
 494 * PARAMETERS:  where               - Physical address of memory to be mapped
 495 *              length              - How much memory to map
 496 *
 497 * RETURN:      Pointer to mapped memory. Null on error.
 498 *
 499 * DESCRIPTION: Map physical memory into caller's address space
 500 *
 501 *****************************************************************************/
 502
 503void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)
 504{
 505
 506        return (ACPI_TO_POINTER((acpi_size)where));
 507}
 508
 509/******************************************************************************
 510 *
 511 * FUNCTION:    acpi_os_unmap_memory
 512 *
 513 * PARAMETERS:  where               - Logical address of memory to be unmapped
 514 *              length              - How much memory to unmap
 515 *
 516 * RETURN:      None.
 517 *
 518 * DESCRIPTION: Delete a previously created mapping. Where and Length must
 519 *              correspond to a previous mapping exactly.
 520 *
 521 *****************************************************************************/
 522
 523void acpi_os_unmap_memory(void *where, acpi_size length)
 524{
 525
 526        return;
 527}
 528#endif
 529
 530/******************************************************************************
 531 *
 532 * FUNCTION:    acpi_os_allocate
 533 *
 534 * PARAMETERS:  size                - Amount to allocate, in bytes
 535 *
 536 * RETURN:      Pointer to the new allocation. Null on error.
 537 *
 538 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
 539 *
 540 *****************************************************************************/
 541
 542void *acpi_os_allocate(acpi_size size)
 543{
 544        void *mem;
 545
 546        mem = (void *)malloc((size_t) size);
 547        return (mem);
 548}
 549
 550#ifdef USE_NATIVE_ALLOCATE_ZEROED
 551/******************************************************************************
 552 *
 553 * FUNCTION:    acpi_os_allocate_zeroed
 554 *
 555 * PARAMETERS:  size                - Amount to allocate, in bytes
 556 *
 557 * RETURN:      Pointer to the new allocation. Null on error.
 558 *
 559 * DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.
 560 *
 561 *****************************************************************************/
 562
 563void *acpi_os_allocate_zeroed(acpi_size size)
 564{
 565        void *mem;
 566
 567        mem = (void *)calloc(1, (size_t) size);
 568        return (mem);
 569}
 570#endif
 571
 572/******************************************************************************
 573 *
 574 * FUNCTION:    acpi_os_free
 575 *
 576 * PARAMETERS:  mem                 - Pointer to previously allocated memory
 577 *
 578 * RETURN:      None.
 579 *
 580 * DESCRIPTION: Free memory allocated via acpi_os_allocate
 581 *
 582 *****************************************************************************/
 583
 584void acpi_os_free(void *mem)
 585{
 586
 587        free(mem);
 588}
 589
 590#ifdef ACPI_SINGLE_THREADED
 591/******************************************************************************
 592 *
 593 * FUNCTION:    Semaphore stub functions
 594 *
 595 * DESCRIPTION: Stub functions used for single-thread applications that do
 596 *              not require semaphore synchronization. Full implementations
 597 *              of these functions appear after the stubs.
 598 *
 599 *****************************************************************************/
 600
 601acpi_status
 602acpi_os_create_semaphore(u32 max_units,
 603                         u32 initial_units, acpi_handle *out_handle)
 604{
 605        *out_handle = (acpi_handle)1;
 606        return (AE_OK);
 607}
 608
 609acpi_status acpi_os_delete_semaphore(acpi_handle handle)
 610{
 611        return (AE_OK);
 612}
 613
 614acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
 615{
 616        return (AE_OK);
 617}
 618
 619acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
 620{
 621        return (AE_OK);
 622}
 623
 624#else
 625/******************************************************************************
 626 *
 627 * FUNCTION:    acpi_os_create_semaphore
 628 *
 629 * PARAMETERS:  initial_units       - Units to be assigned to the new semaphore
 630 *              out_handle          - Where a handle will be returned
 631 *
 632 * RETURN:      Status
 633 *
 634 * DESCRIPTION: Create an OS semaphore
 635 *
 636 *****************************************************************************/
 637
 638acpi_status
 639acpi_os_create_semaphore(u32 max_units,
 640                         u32 initial_units, acpi_handle *out_handle)
 641{
 642        sem_t *sem;
 643
 644        if (!out_handle) {
 645                return (AE_BAD_PARAMETER);
 646        }
 647#ifdef __APPLE__
 648        {
 649                static int semaphore_count = 0;
 650                char semaphore_name[32];
 651
 652                snprintf(semaphore_name, sizeof(semaphore_name), "acpi_sem_%d",
 653                         semaphore_count++);
 654                printf("%s\n", semaphore_name);
 655                sem =
 656                    sem_open(semaphore_name, O_EXCL | O_CREAT, 0755,
 657                             initial_units);
 658                if (!sem) {
 659                        return (AE_NO_MEMORY);
 660                }
 661                sem_unlink(semaphore_name);     /* This just deletes the name */
 662        }
 663
 664#else
 665        sem = acpi_os_allocate(sizeof(sem_t));
 666        if (!sem) {
 667                return (AE_NO_MEMORY);
 668        }
 669
 670        if (sem_init(sem, 0, initial_units) == -1) {
 671                acpi_os_free(sem);
 672                return (AE_BAD_PARAMETER);
 673        }
 674#endif
 675
 676        *out_handle = (acpi_handle)sem;
 677        return (AE_OK);
 678}
 679
 680/******************************************************************************
 681 *
 682 * FUNCTION:    acpi_os_delete_semaphore
 683 *
 684 * PARAMETERS:  handle              - Handle returned by acpi_os_create_semaphore
 685 *
 686 * RETURN:      Status
 687 *
 688 * DESCRIPTION: Delete an OS semaphore
 689 *
 690 *****************************************************************************/
 691
 692acpi_status acpi_os_delete_semaphore(acpi_handle handle)
 693{
 694        sem_t *sem = (sem_t *) handle;
 695
 696        if (!sem) {
 697                return (AE_BAD_PARAMETER);
 698        }
 699#ifdef __APPLE__
 700        if (sem_close(sem) == -1) {
 701                return (AE_BAD_PARAMETER);
 702        }
 703#else
 704        if (sem_destroy(sem) == -1) {
 705                return (AE_BAD_PARAMETER);
 706        }
 707#endif
 708
 709        return (AE_OK);
 710}
 711
 712/******************************************************************************
 713 *
 714 * FUNCTION:    acpi_os_wait_semaphore
 715 *
 716 * PARAMETERS:  handle              - Handle returned by acpi_os_create_semaphore
 717 *              units               - How many units to wait for
 718 *              msec_timeout        - How long to wait (milliseconds)
 719 *
 720 * RETURN:      Status
 721 *
 722 * DESCRIPTION: Wait for units
 723 *
 724 *****************************************************************************/
 725
 726acpi_status
 727acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 msec_timeout)
 728{
 729        acpi_status status = AE_OK;
 730        sem_t *sem = (sem_t *) handle;
 731#ifndef ACPI_USE_ALTERNATE_TIMEOUT
 732        struct timespec time;
 733        int ret_val;
 734#endif
 735
 736        if (!sem) {
 737                return (AE_BAD_PARAMETER);
 738        }
 739
 740        switch (msec_timeout) {
 741                /*
 742                 * No Wait:
 743                 * --------
 744                 * A zero timeout value indicates that we shouldn't wait - just
 745                 * acquire the semaphore if available otherwise return AE_TIME
 746                 * (a.k.a. 'would block').
 747                 */
 748        case 0:
 749
 750                if (sem_trywait(sem) == -1) {
 751                        status = (AE_TIME);
 752                }
 753                break;
 754
 755                /* Wait Indefinitely */
 756
 757        case ACPI_WAIT_FOREVER:
 758
 759                if (sem_wait(sem)) {
 760                        status = (AE_TIME);
 761                }
 762                break;
 763
 764                /* Wait with msec_timeout */
 765
 766        default:
 767
 768#ifdef ACPI_USE_ALTERNATE_TIMEOUT
 769                /*
 770                 * Alternate timeout mechanism for environments where
 771                 * sem_timedwait is not available or does not work properly.
 772                 */
 773                while (msec_timeout) {
 774                        if (sem_trywait(sem) == 0) {
 775
 776                                /* Got the semaphore */
 777                                return (AE_OK);
 778                        }
 779
 780                        if (msec_timeout >= 10) {
 781                                msec_timeout -= 10;
 782                                usleep(10 * ACPI_USEC_PER_MSEC);        /* ten milliseconds */
 783                        } else {
 784                                msec_timeout--;
 785                                usleep(ACPI_USEC_PER_MSEC);     /* one millisecond */
 786                        }
 787                }
 788                status = (AE_TIME);
 789#else
 790                /*
 791                 * The interface to sem_timedwait is an absolute time, so we need to
 792                 * get the current time, then add in the millisecond Timeout value.
 793                 */
 794                if (clock_gettime(CLOCK_REALTIME, &time) == -1) {
 795                        perror("clock_gettime");
 796                        return (AE_TIME);
 797                }
 798
 799                time.tv_sec += (msec_timeout / ACPI_MSEC_PER_SEC);
 800                time.tv_nsec +=
 801                    ((msec_timeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);
 802
 803                /* Handle nanosecond overflow (field must be less than one second) */
 804
 805                if (time.tv_nsec >= ACPI_NSEC_PER_SEC) {
 806                        time.tv_sec += (time.tv_nsec / ACPI_NSEC_PER_SEC);
 807                        time.tv_nsec = (time.tv_nsec % ACPI_NSEC_PER_SEC);
 808                }
 809
 810                while (((ret_val = sem_timedwait(sem, &time)) == -1)
 811                       && (errno == EINTR)) {
 812                        continue;
 813                }
 814
 815                if (ret_val != 0) {
 816                        if (errno != ETIMEDOUT) {
 817                                perror("sem_timedwait");
 818                        }
 819                        status = (AE_TIME);
 820                }
 821#endif
 822                break;
 823        }
 824
 825        return (status);
 826}
 827
 828/******************************************************************************
 829 *
 830 * FUNCTION:    acpi_os_signal_semaphore
 831 *
 832 * PARAMETERS:  handle              - Handle returned by acpi_os_create_semaphore
 833 *              units               - Number of units to send
 834 *
 835 * RETURN:      Status
 836 *
 837 * DESCRIPTION: Send units
 838 *
 839 *****************************************************************************/
 840
 841acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
 842{
 843        sem_t *sem = (sem_t *) handle;
 844
 845        if (!sem) {
 846                return (AE_BAD_PARAMETER);
 847        }
 848
 849        if (sem_post(sem) == -1) {
 850                return (AE_LIMIT);
 851        }
 852
 853        return (AE_OK);
 854}
 855
 856#endif                          /* ACPI_SINGLE_THREADED */
 857
 858/******************************************************************************
 859 *
 860 * FUNCTION:    Spinlock interfaces
 861 *
 862 * DESCRIPTION: Map these interfaces to semaphore interfaces
 863 *
 864 *****************************************************************************/
 865
 866acpi_status acpi_os_create_lock(acpi_spinlock * out_handle)
 867{
 868
 869        return (acpi_os_create_semaphore(1, 1, out_handle));
 870}
 871
 872void acpi_os_delete_lock(acpi_spinlock handle)
 873{
 874        acpi_os_delete_semaphore(handle);
 875}
 876
 877acpi_cpu_flags acpi_os_acquire_lock(acpi_handle handle)
 878{
 879        acpi_os_wait_semaphore(handle, 1, 0xFFFF);
 880        return (0);
 881}
 882
 883void acpi_os_release_lock(acpi_spinlock handle, acpi_cpu_flags flags)
 884{
 885        acpi_os_signal_semaphore(handle, 1);
 886}
 887
 888/******************************************************************************
 889 *
 890 * FUNCTION:    acpi_os_install_interrupt_handler
 891 *
 892 * PARAMETERS:  interrupt_number    - Level handler should respond to.
 893 *              isr                 - Address of the ACPI interrupt handler
 894 *              except_ptr          - Where status is returned
 895 *
 896 * RETURN:      Handle to the newly installed handler.
 897 *
 898 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
 899 *              OS-independent handler.
 900 *
 901 *****************************************************************************/
 902
 903u32
 904acpi_os_install_interrupt_handler(u32 interrupt_number,
 905                                  acpi_osd_handler service_routine,
 906                                  void *context)
 907{
 908
 909        return (AE_OK);
 910}
 911
 912/******************************************************************************
 913 *
 914 * FUNCTION:    acpi_os_remove_interrupt_handler
 915 *
 916 * PARAMETERS:  handle              - Returned when handler was installed
 917 *
 918 * RETURN:      Status
 919 *
 920 * DESCRIPTION: Uninstalls an interrupt handler.
 921 *
 922 *****************************************************************************/
 923
 924acpi_status
 925acpi_os_remove_interrupt_handler(u32 interrupt_number,
 926                                 acpi_osd_handler service_routine)
 927{
 928
 929        return (AE_OK);
 930}
 931
 932/******************************************************************************
 933 *
 934 * FUNCTION:    acpi_os_stall
 935 *
 936 * PARAMETERS:  microseconds        - Time to sleep
 937 *
 938 * RETURN:      Blocks until sleep is completed.
 939 *
 940 * DESCRIPTION: Sleep at microsecond granularity
 941 *
 942 *****************************************************************************/
 943
 944void acpi_os_stall(u32 microseconds)
 945{
 946
 947        if (microseconds) {
 948                usleep(microseconds);
 949        }
 950}
 951
 952/******************************************************************************
 953 *
 954 * FUNCTION:    acpi_os_sleep
 955 *
 956 * PARAMETERS:  milliseconds        - Time to sleep
 957 *
 958 * RETURN:      Blocks until sleep is completed.
 959 *
 960 * DESCRIPTION: Sleep at millisecond granularity
 961 *
 962 *****************************************************************************/
 963
 964void acpi_os_sleep(u64 milliseconds)
 965{
 966
 967        /* Sleep for whole seconds */
 968
 969        sleep(milliseconds / ACPI_MSEC_PER_SEC);
 970
 971        /*
 972         * Sleep for remaining microseconds.
 973         * Arg to usleep() is in usecs and must be less than 1,000,000 (1 second).
 974         */
 975        usleep((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);
 976}
 977
 978/******************************************************************************
 979 *
 980 * FUNCTION:    acpi_os_get_timer
 981 *
 982 * PARAMETERS:  None
 983 *
 984 * RETURN:      Current time in 100 nanosecond units
 985 *
 986 * DESCRIPTION: Get the current system time
 987 *
 988 *****************************************************************************/
 989
 990u64 acpi_os_get_timer(void)
 991{
 992        struct timeval time;
 993
 994        /* This timer has sufficient resolution for user-space application code */
 995
 996        gettimeofday(&time, NULL);
 997
 998        /* (Seconds * 10^7 = 100ns(10^-7)) + (Microseconds(10^-6) * 10^1 = 100ns) */
 999
1000        return (((u64)time.tv_sec * ACPI_100NSEC_PER_SEC) +
1001                ((u64)time.tv_usec * ACPI_100NSEC_PER_USEC));
1002}
1003
1004/******************************************************************************
1005 *
1006 * FUNCTION:    acpi_os_read_pci_configuration
1007 *
1008 * PARAMETERS:  pci_id              - Seg/Bus/Dev
1009 *              pci_register        - Device Register
1010 *              value               - Buffer where value is placed
1011 *              width               - Number of bits
1012 *
1013 * RETURN:      Status
1014 *
1015 * DESCRIPTION: Read data from PCI configuration space
1016 *
1017 *****************************************************************************/
1018
1019acpi_status
1020acpi_os_read_pci_configuration(struct acpi_pci_id *pci_id,
1021                               u32 pci_register, u64 *value, u32 width)
1022{
1023
1024        *value = 0;
1025        return (AE_OK);
1026}
1027
1028/******************************************************************************
1029 *
1030 * FUNCTION:    acpi_os_write_pci_configuration
1031 *
1032 * PARAMETERS:  pci_id              - Seg/Bus/Dev
1033 *              pci_register        - Device Register
1034 *              value               - Value to be written
1035 *              width               - Number of bits
1036 *
1037 * RETURN:      Status.
1038 *
1039 * DESCRIPTION: Write data to PCI configuration space
1040 *
1041 *****************************************************************************/
1042
1043acpi_status
1044acpi_os_write_pci_configuration(struct acpi_pci_id *pci_id,
1045                                u32 pci_register, u64 value, u32 width)
1046{
1047
1048        return (AE_OK);
1049}
1050
1051/******************************************************************************
1052 *
1053 * FUNCTION:    acpi_os_read_port
1054 *
1055 * PARAMETERS:  address             - Address of I/O port/register to read
1056 *              value               - Where value is placed
1057 *              width               - Number of bits
1058 *
1059 * RETURN:      Value read from port
1060 *
1061 * DESCRIPTION: Read data from an I/O port or register
1062 *
1063 *****************************************************************************/
1064
1065acpi_status acpi_os_read_port(acpi_io_address address, u32 *value, u32 width)
1066{
1067
1068        switch (width) {
1069        case 8:
1070
1071                *value = 0xFF;
1072                break;
1073
1074        case 16:
1075
1076                *value = 0xFFFF;
1077                break;
1078
1079        case 32:
1080
1081                *value = 0xFFFFFFFF;
1082                break;
1083
1084        default:
1085
1086                return (AE_BAD_PARAMETER);
1087        }
1088
1089        return (AE_OK);
1090}
1091
1092/******************************************************************************
1093 *
1094 * FUNCTION:    acpi_os_write_port
1095 *
1096 * PARAMETERS:  address             - Address of I/O port/register to write
1097 *              value               - Value to write
1098 *              width               - Number of bits
1099 *
1100 * RETURN:      None
1101 *
1102 * DESCRIPTION: Write data to an I/O port or register
1103 *
1104 *****************************************************************************/
1105
1106acpi_status acpi_os_write_port(acpi_io_address address, u32 value, u32 width)
1107{
1108
1109        return (AE_OK);
1110}
1111
1112/******************************************************************************
1113 *
1114 * FUNCTION:    acpi_os_read_memory
1115 *
1116 * PARAMETERS:  address             - Physical Memory Address to read
1117 *              value               - Where value is placed
1118 *              width               - Number of bits (8,16,32, or 64)
1119 *
1120 * RETURN:      Value read from physical memory address. Always returned
1121 *              as a 64-bit integer, regardless of the read width.
1122 *
1123 * DESCRIPTION: Read data from a physical memory address
1124 *
1125 *****************************************************************************/
1126
1127acpi_status
1128acpi_os_read_memory(acpi_physical_address address, u64 *value, u32 width)
1129{
1130
1131        switch (width) {
1132        case 8:
1133        case 16:
1134        case 32:
1135        case 64:
1136
1137                *value = 0;
1138                break;
1139
1140        default:
1141
1142                return (AE_BAD_PARAMETER);
1143        }
1144        return (AE_OK);
1145}
1146
1147/******************************************************************************
1148 *
1149 * FUNCTION:    acpi_os_write_memory
1150 *
1151 * PARAMETERS:  address             - Physical Memory Address to write
1152 *              value               - Value to write
1153 *              width               - Number of bits (8,16,32, or 64)
1154 *
1155 * RETURN:      None
1156 *
1157 * DESCRIPTION: Write data to a physical memory address
1158 *
1159 *****************************************************************************/
1160
1161acpi_status
1162acpi_os_write_memory(acpi_physical_address address, u64 value, u32 width)
1163{
1164
1165        return (AE_OK);
1166}
1167
1168/******************************************************************************
1169 *
1170 * FUNCTION:    acpi_os_readable
1171 *
1172 * PARAMETERS:  pointer             - Area to be verified
1173 *              length              - Size of area
1174 *
1175 * RETURN:      TRUE if readable for entire length
1176 *
1177 * DESCRIPTION: Verify that a pointer is valid for reading
1178 *
1179 *****************************************************************************/
1180
1181u8 acpi_os_readable(void *pointer, acpi_size length)
1182{
1183
1184        return (TRUE);
1185}
1186
1187/******************************************************************************
1188 *
1189 * FUNCTION:    acpi_os_writable
1190 *
1191 * PARAMETERS:  pointer             - Area to be verified
1192 *              length              - Size of area
1193 *
1194 * RETURN:      TRUE if writable for entire length
1195 *
1196 * DESCRIPTION: Verify that a pointer is valid for writing
1197 *
1198 *****************************************************************************/
1199
1200u8 acpi_os_writable(void *pointer, acpi_size length)
1201{
1202
1203        return (TRUE);
1204}
1205
1206/******************************************************************************
1207 *
1208 * FUNCTION:    acpi_os_signal
1209 *
1210 * PARAMETERS:  function            - ACPI A signal function code
1211 *              info                - Pointer to function-dependent structure
1212 *
1213 * RETURN:      Status
1214 *
1215 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1216 *
1217 *****************************************************************************/
1218
1219acpi_status acpi_os_signal(u32 function, void *info)
1220{
1221
1222        switch (function) {
1223        case ACPI_SIGNAL_FATAL:
1224
1225                break;
1226
1227        case ACPI_SIGNAL_BREAKPOINT:
1228
1229                break;
1230
1231        default:
1232
1233                break;
1234        }
1235
1236        return (AE_OK);
1237}
1238
1239/* Optional multi-thread support */
1240
1241#ifndef ACPI_SINGLE_THREADED
1242/******************************************************************************
1243 *
1244 * FUNCTION:    acpi_os_get_thread_id
1245 *
1246 * PARAMETERS:  None
1247 *
1248 * RETURN:      Id of the running thread
1249 *
1250 * DESCRIPTION: Get the ID of the current (running) thread
1251 *
1252 *****************************************************************************/
1253
1254acpi_thread_id acpi_os_get_thread_id(void)
1255{
1256        pthread_t thread;
1257
1258        thread = pthread_self();
1259        return (ACPI_CAST_PTHREAD_T(thread));
1260}
1261
1262/******************************************************************************
1263 *
1264 * FUNCTION:    acpi_os_execute
1265 *
1266 * PARAMETERS:  type                - Type of execution
1267 *              function            - Address of the function to execute
1268 *              context             - Passed as a parameter to the function
1269 *
1270 * RETURN:      Status.
1271 *
1272 * DESCRIPTION: Execute a new thread
1273 *
1274 *****************************************************************************/
1275
1276acpi_status
1277acpi_os_execute(acpi_execute_type type,
1278                acpi_osd_exec_callback function, void *context)
1279{
1280        pthread_t thread;
1281        int ret;
1282
1283        ret =
1284            pthread_create(&thread, NULL, (PTHREAD_CALLBACK) function, context);
1285        if (ret) {
1286                acpi_os_printf("Create thread failed");
1287        }
1288        return (0);
1289}
1290
1291#else                           /* ACPI_SINGLE_THREADED */
1292acpi_thread_id acpi_os_get_thread_id(void)
1293{
1294        return (1);
1295}
1296
1297acpi_status
1298acpi_os_execute(acpi_execute_type type,
1299                acpi_osd_exec_callback function, void *context)
1300{
1301
1302        function(context);
1303
1304        return (AE_OK);
1305}
1306
1307#endif                          /* ACPI_SINGLE_THREADED */
1308
1309/******************************************************************************
1310 *
1311 * FUNCTION:    acpi_os_wait_events_complete
1312 *
1313 * PARAMETERS:  None
1314 *
1315 * RETURN:      None
1316 *
1317 * DESCRIPTION: Wait for all asynchronous events to complete. This
1318 *              implementation does nothing.
1319 *
1320 *****************************************************************************/
1321
1322void acpi_os_wait_events_complete(void)
1323{
1324        return;
1325}
1326