Home | History | Annotate | Line # | Download | only in hardware
hwtimer.c revision 1.1.1.12.4.1
      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.12.4.1   thorpej  * Copyright (C) 2000 - 2021, 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.12.4.1   thorpej  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 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.1.8  christos     UINT64                  TimerValue;
    111           1.1    jruoho 
    112           1.1    jruoho 
    113           1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiGetTimer);
    114           1.1    jruoho 
    115           1.1    jruoho 
    116           1.1    jruoho     if (!Ticks)
    117           1.1    jruoho     {
    118           1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    119           1.1    jruoho     }
    120           1.1    jruoho 
    121       1.1.1.3  christos     /* ACPI 5.0A: PM Timer is optional */
    122       1.1.1.3  christos 
    123       1.1.1.3  christos     if (!AcpiGbl_FADT.XPmTimerBlock.Address)
    124       1.1.1.3  christos     {
    125       1.1.1.3  christos         return_ACPI_STATUS (AE_SUPPORT);
    126       1.1.1.3  christos     }
    127           1.1    jruoho 
    128       1.1.1.8  christos     Status = AcpiHwRead (&TimerValue, &AcpiGbl_FADT.XPmTimerBlock);
    129       1.1.1.8  christos     if (ACPI_SUCCESS (Status))
    130       1.1.1.8  christos     {
    131       1.1.1.8  christos         /* ACPI PM Timer is defined to be 32 bits (PM_TMR_LEN) */
    132       1.1.1.8  christos 
    133       1.1.1.8  christos         *Ticks = (UINT32) TimerValue;
    134       1.1.1.8  christos     }
    135       1.1.1.8  christos 
    136           1.1    jruoho     return_ACPI_STATUS (Status);
    137           1.1    jruoho }
    138           1.1    jruoho 
    139           1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiGetTimer)
    140           1.1    jruoho 
    141           1.1    jruoho 
    142           1.1    jruoho /******************************************************************************
    143           1.1    jruoho  *
    144           1.1    jruoho  * FUNCTION:    AcpiGetTimerDuration
    145           1.1    jruoho  *
    146           1.1    jruoho  * PARAMETERS:  StartTicks          - Starting timestamp
    147           1.1    jruoho  *              EndTicks            - End timestamp
    148           1.1    jruoho  *              TimeElapsed         - Where the elapsed time is returned
    149           1.1    jruoho  *
    150           1.1    jruoho  * RETURN:      Status and TimeElapsed
    151           1.1    jruoho  *
    152           1.1    jruoho  * DESCRIPTION: Computes the time elapsed (in microseconds) between two
    153           1.1    jruoho  *              PM Timer time stamps, taking into account the possibility of
    154           1.1    jruoho  *              rollovers, the timer resolution, and timer frequency.
    155           1.1    jruoho  *
    156           1.1    jruoho  *              The PM Timer's clock ticks at roughly 3.6 times per
    157           1.1    jruoho  *              _microsecond_, and its clock continues through Cx state
    158           1.1    jruoho  *              transitions (unlike many CPU timestamp counters) -- making it
    159           1.1    jruoho  *              a versatile and accurate timer.
    160           1.1    jruoho  *
    161           1.1    jruoho  *              Note that this function accommodates only a single timer
    162       1.1.1.3  christos  *              rollover. Thus for 24-bit timers, this function should only
    163           1.1    jruoho  *              be used for calculating durations less than ~4.6 seconds
    164           1.1    jruoho  *              (~20 minutes for 32-bit timers) -- calculations below:
    165           1.1    jruoho  *
    166           1.1    jruoho  *              2**24 Ticks / 3,600,000 Ticks/Sec = 4.66 sec
    167           1.1    jruoho  *              2**32 Ticks / 3,600,000 Ticks/Sec = 1193 sec or 19.88 minutes
    168           1.1    jruoho  *
    169           1.1    jruoho  ******************************************************************************/
    170           1.1    jruoho 
    171           1.1    jruoho ACPI_STATUS
    172           1.1    jruoho AcpiGetTimerDuration (
    173           1.1    jruoho     UINT32                  StartTicks,
    174           1.1    jruoho     UINT32                  EndTicks,
    175           1.1    jruoho     UINT32                  *TimeElapsed)
    176           1.1    jruoho {
    177           1.1    jruoho     ACPI_STATUS             Status;
    178       1.1.1.9  christos     UINT64                  DeltaTicks;
    179           1.1    jruoho     UINT64                  Quotient;
    180           1.1    jruoho 
    181           1.1    jruoho 
    182           1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiGetTimerDuration);
    183           1.1    jruoho 
    184           1.1    jruoho 
    185           1.1    jruoho     if (!TimeElapsed)
    186           1.1    jruoho     {
    187           1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    188           1.1    jruoho     }
    189           1.1    jruoho 
    190       1.1.1.3  christos     /* ACPI 5.0A: PM Timer is optional */
    191       1.1.1.3  christos 
    192       1.1.1.3  christos     if (!AcpiGbl_FADT.XPmTimerBlock.Address)
    193       1.1.1.3  christos     {
    194       1.1.1.3  christos         return_ACPI_STATUS (AE_SUPPORT);
    195       1.1.1.3  christos     }
    196       1.1.1.3  christos 
    197       1.1.1.9  christos     if (StartTicks == EndTicks)
    198       1.1.1.9  christos     {
    199       1.1.1.9  christos         *TimeElapsed = 0;
    200       1.1.1.9  christos         return_ACPI_STATUS (AE_OK);
    201       1.1.1.9  christos     }
    202       1.1.1.9  christos 
    203           1.1    jruoho     /*
    204           1.1    jruoho      * Compute Tick Delta:
    205           1.1    jruoho      * Handle (max one) timer rollovers on 24-bit versus 32-bit timers.
    206           1.1    jruoho      */
    207       1.1.1.9  christos     DeltaTicks = EndTicks;
    208       1.1.1.9  christos     if (StartTicks > EndTicks)
    209           1.1    jruoho     {
    210           1.1    jruoho         if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
    211           1.1    jruoho         {
    212           1.1    jruoho             /* 24-bit Timer */
    213           1.1    jruoho 
    214       1.1.1.9  christos             DeltaTicks |= (UINT64) 1 << 24;
    215           1.1    jruoho         }
    216           1.1    jruoho         else
    217           1.1    jruoho         {
    218           1.1    jruoho             /* 32-bit Timer */
    219           1.1    jruoho 
    220       1.1.1.9  christos             DeltaTicks |= (UINT64) 1 << 32;
    221           1.1    jruoho         }
    222           1.1    jruoho     }
    223       1.1.1.9  christos     DeltaTicks -= StartTicks;
    224           1.1    jruoho 
    225           1.1    jruoho     /*
    226           1.1    jruoho      * Compute Duration (Requires a 64-bit multiply and divide):
    227           1.1    jruoho      *
    228       1.1.1.3  christos      * TimeElapsed (microseconds) =
    229       1.1.1.3  christos      *  (DeltaTicks * ACPI_USEC_PER_SEC) / ACPI_PM_TIMER_FREQUENCY;
    230           1.1    jruoho      */
    231       1.1.1.9  christos     Status = AcpiUtShortDivide (DeltaTicks * ACPI_USEC_PER_SEC,
    232       1.1.1.3  christos                 ACPI_PM_TIMER_FREQUENCY, &Quotient, NULL);
    233           1.1    jruoho 
    234           1.1    jruoho     *TimeElapsed = (UINT32) Quotient;
    235           1.1    jruoho     return_ACPI_STATUS (Status);
    236           1.1    jruoho }
    237           1.1    jruoho 
    238           1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiGetTimerDuration)
    239           1.1    jruoho 
    240       1.1.1.3  christos #endif /* !ACPI_REDUCED_HARDWARE */
    241