Home | History | Annotate | Line # | Download | only in hardware
hwtimer.c revision 1.1.1.4.2.2
      1          1.1    jruoho /******************************************************************************
      2          1.1    jruoho  *
      3          1.1    jruoho  * Name: hwtimer.c - ACPI Power Management Timer Interface
      4          1.1    jruoho  *
      5          1.1    jruoho  *****************************************************************************/
      6          1.1    jruoho 
      7      1.1.1.2    jruoho /*
      8  1.1.1.4.2.2     skrll  * Copyright (C) 2000 - 2016, Intel Corp.
      9          1.1    jruoho  * All rights reserved.
     10          1.1    jruoho  *
     11      1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12      1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13      1.1.1.2    jruoho  * are met:
     14      1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15      1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16      1.1.1.2    jruoho  *    without modification.
     17      1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21      1.1.1.2    jruoho  *    binary redistribution.
     22      1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24      1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25      1.1.1.2    jruoho  *
     26      1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27      1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.1.1.2    jruoho  * Software Foundation.
     29      1.1.1.2    jruoho  *
     30      1.1.1.2    jruoho  * NO WARRANTY
     31      1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42      1.1.1.2    jruoho  */
     43          1.1    jruoho 
     44      1.1.1.3  christos #define EXPORT_ACPI_INTERFACES
     45      1.1.1.3  christos 
     46          1.1    jruoho #include "acpi.h"
     47          1.1    jruoho #include "accommon.h"
     48          1.1    jruoho 
     49          1.1    jruoho #define _COMPONENT          ACPI_HARDWARE
     50          1.1    jruoho         ACPI_MODULE_NAME    ("hwtimer")
     51          1.1    jruoho 
     52          1.1    jruoho 
     53      1.1.1.3  christos #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
     54          1.1    jruoho /******************************************************************************
     55          1.1    jruoho  *
     56          1.1    jruoho  * FUNCTION:    AcpiGetTimerResolution
     57          1.1    jruoho  *
     58          1.1    jruoho  * PARAMETERS:  Resolution          - Where the resolution is returned
     59          1.1    jruoho  *
     60          1.1    jruoho  * RETURN:      Status and timer resolution
     61          1.1    jruoho  *
     62          1.1    jruoho  * DESCRIPTION: Obtains resolution of the ACPI PM Timer (24 or 32 bits).
     63          1.1    jruoho  *
     64          1.1    jruoho  ******************************************************************************/
     65          1.1    jruoho 
     66          1.1    jruoho ACPI_STATUS
     67          1.1    jruoho AcpiGetTimerResolution (
     68          1.1    jruoho     UINT32                  *Resolution)
     69          1.1    jruoho {
     70          1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiGetTimerResolution);
     71          1.1    jruoho 
     72          1.1    jruoho 
     73          1.1    jruoho     if (!Resolution)
     74          1.1    jruoho     {
     75          1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
     76          1.1    jruoho     }
     77          1.1    jruoho 
     78          1.1    jruoho     if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
     79          1.1    jruoho     {
     80          1.1    jruoho         *Resolution = 24;
     81          1.1    jruoho     }
     82          1.1    jruoho     else
     83          1.1    jruoho     {
     84          1.1    jruoho         *Resolution = 32;
     85          1.1    jruoho     }
     86          1.1    jruoho 
     87          1.1    jruoho     return_ACPI_STATUS (AE_OK);
     88          1.1    jruoho }
     89          1.1    jruoho 
     90          1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiGetTimerResolution)
     91          1.1    jruoho 
     92          1.1    jruoho 
     93          1.1    jruoho /******************************************************************************
     94          1.1    jruoho  *
     95          1.1    jruoho  * FUNCTION:    AcpiGetTimer
     96          1.1    jruoho  *
     97          1.1    jruoho  * PARAMETERS:  Ticks               - Where the timer value is returned
     98          1.1    jruoho  *
     99          1.1    jruoho  * RETURN:      Status and current timer value (ticks)
    100          1.1    jruoho  *
    101          1.1    jruoho  * DESCRIPTION: Obtains current value of ACPI PM Timer (in ticks).
    102          1.1    jruoho  *
    103          1.1    jruoho  ******************************************************************************/
    104          1.1    jruoho 
    105          1.1    jruoho ACPI_STATUS
    106          1.1    jruoho AcpiGetTimer (
    107          1.1    jruoho     UINT32                  *Ticks)
    108          1.1    jruoho {
    109          1.1    jruoho     ACPI_STATUS             Status;
    110          1.1    jruoho 
    111          1.1    jruoho 
    112          1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiGetTimer);
    113          1.1    jruoho 
    114          1.1    jruoho 
    115          1.1    jruoho     if (!Ticks)
    116          1.1    jruoho     {
    117          1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    118          1.1    jruoho     }
    119          1.1    jruoho 
    120      1.1.1.3  christos     /* ACPI 5.0A: PM Timer is optional */
    121      1.1.1.3  christos 
    122      1.1.1.3  christos     if (!AcpiGbl_FADT.XPmTimerBlock.Address)
    123      1.1.1.3  christos     {
    124      1.1.1.3  christos         return_ACPI_STATUS (AE_SUPPORT);
    125      1.1.1.3  christos     }
    126          1.1    jruoho 
    127      1.1.1.3  christos     Status = AcpiHwRead (Ticks, &AcpiGbl_FADT.XPmTimerBlock);
    128          1.1    jruoho     return_ACPI_STATUS (Status);
    129          1.1    jruoho }
    130          1.1    jruoho 
    131          1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiGetTimer)
    132          1.1    jruoho 
    133          1.1    jruoho 
    134          1.1    jruoho /******************************************************************************
    135          1.1    jruoho  *
    136          1.1    jruoho  * FUNCTION:    AcpiGetTimerDuration
    137          1.1    jruoho  *
    138          1.1    jruoho  * PARAMETERS:  StartTicks          - Starting timestamp
    139          1.1    jruoho  *              EndTicks            - End timestamp
    140          1.1    jruoho  *              TimeElapsed         - Where the elapsed time is returned
    141          1.1    jruoho  *
    142          1.1    jruoho  * RETURN:      Status and TimeElapsed
    143          1.1    jruoho  *
    144          1.1    jruoho  * DESCRIPTION: Computes the time elapsed (in microseconds) between two
    145          1.1    jruoho  *              PM Timer time stamps, taking into account the possibility of
    146          1.1    jruoho  *              rollovers, the timer resolution, and timer frequency.
    147          1.1    jruoho  *
    148          1.1    jruoho  *              The PM Timer's clock ticks at roughly 3.6 times per
    149          1.1    jruoho  *              _microsecond_, and its clock continues through Cx state
    150          1.1    jruoho  *              transitions (unlike many CPU timestamp counters) -- making it
    151          1.1    jruoho  *              a versatile and accurate timer.
    152          1.1    jruoho  *
    153          1.1    jruoho  *              Note that this function accommodates only a single timer
    154      1.1.1.3  christos  *              rollover. Thus for 24-bit timers, this function should only
    155          1.1    jruoho  *              be used for calculating durations less than ~4.6 seconds
    156          1.1    jruoho  *              (~20 minutes for 32-bit timers) -- calculations below:
    157          1.1    jruoho  *
    158          1.1    jruoho  *              2**24 Ticks / 3,600,000 Ticks/Sec = 4.66 sec
    159          1.1    jruoho  *              2**32 Ticks / 3,600,000 Ticks/Sec = 1193 sec or 19.88 minutes
    160          1.1    jruoho  *
    161          1.1    jruoho  ******************************************************************************/
    162          1.1    jruoho 
    163          1.1    jruoho ACPI_STATUS
    164          1.1    jruoho AcpiGetTimerDuration (
    165          1.1    jruoho     UINT32                  StartTicks,
    166          1.1    jruoho     UINT32                  EndTicks,
    167          1.1    jruoho     UINT32                  *TimeElapsed)
    168          1.1    jruoho {
    169          1.1    jruoho     ACPI_STATUS             Status;
    170          1.1    jruoho     UINT32                  DeltaTicks;
    171          1.1    jruoho     UINT64                  Quotient;
    172          1.1    jruoho 
    173          1.1    jruoho 
    174          1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiGetTimerDuration);
    175          1.1    jruoho 
    176          1.1    jruoho 
    177          1.1    jruoho     if (!TimeElapsed)
    178          1.1    jruoho     {
    179          1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    180          1.1    jruoho     }
    181          1.1    jruoho 
    182      1.1.1.3  christos     /* ACPI 5.0A: PM Timer is optional */
    183      1.1.1.3  christos 
    184      1.1.1.3  christos     if (!AcpiGbl_FADT.XPmTimerBlock.Address)
    185      1.1.1.3  christos     {
    186      1.1.1.3  christos         return_ACPI_STATUS (AE_SUPPORT);
    187      1.1.1.3  christos     }
    188      1.1.1.3  christos 
    189          1.1    jruoho     /*
    190          1.1    jruoho      * Compute Tick Delta:
    191          1.1    jruoho      * Handle (max one) timer rollovers on 24-bit versus 32-bit timers.
    192          1.1    jruoho      */
    193          1.1    jruoho     if (StartTicks < EndTicks)
    194          1.1    jruoho     {
    195          1.1    jruoho         DeltaTicks = EndTicks - StartTicks;
    196          1.1    jruoho     }
    197          1.1    jruoho     else if (StartTicks > EndTicks)
    198          1.1    jruoho     {
    199          1.1    jruoho         if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
    200          1.1    jruoho         {
    201          1.1    jruoho             /* 24-bit Timer */
    202          1.1    jruoho 
    203          1.1    jruoho             DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF);
    204          1.1    jruoho         }
    205          1.1    jruoho         else
    206          1.1    jruoho         {
    207          1.1    jruoho             /* 32-bit Timer */
    208          1.1    jruoho 
    209          1.1    jruoho             DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks;
    210          1.1    jruoho         }
    211          1.1    jruoho     }
    212          1.1    jruoho     else /* StartTicks == EndTicks */
    213          1.1    jruoho     {
    214          1.1    jruoho         *TimeElapsed = 0;
    215          1.1    jruoho         return_ACPI_STATUS (AE_OK);
    216          1.1    jruoho     }
    217          1.1    jruoho 
    218          1.1    jruoho     /*
    219          1.1    jruoho      * Compute Duration (Requires a 64-bit multiply and divide):
    220          1.1    jruoho      *
    221      1.1.1.3  christos      * TimeElapsed (microseconds) =
    222      1.1.1.3  christos      *  (DeltaTicks * ACPI_USEC_PER_SEC) / ACPI_PM_TIMER_FREQUENCY;
    223          1.1    jruoho      */
    224      1.1.1.3  christos     Status = AcpiUtShortDivide (((UINT64) DeltaTicks) * ACPI_USEC_PER_SEC,
    225      1.1.1.3  christos                 ACPI_PM_TIMER_FREQUENCY, &Quotient, NULL);
    226          1.1    jruoho 
    227          1.1    jruoho     *TimeElapsed = (UINT32) Quotient;
    228          1.1    jruoho     return_ACPI_STATUS (Status);
    229          1.1    jruoho }
    230          1.1    jruoho 
    231          1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiGetTimerDuration)
    232          1.1    jruoho 
    233      1.1.1.3  christos #endif /* !ACPI_REDUCED_HARDWARE */
    234