hwtimer.c revision 202771
171867Smsmith
271867Smsmith/******************************************************************************
371867Smsmith *
471867Smsmith * Name: hwtimer.c - ACPI Power Management Timer Interface
571867Smsmith *
671867Smsmith *****************************************************************************/
771867Smsmith
871867Smsmith/******************************************************************************
971867Smsmith *
1071867Smsmith * 1. Copyright Notice
1171867Smsmith *
1271867Smsmith * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
1371867Smsmith * All rights reserved.
1471867Smsmith *
1571867Smsmith * 2. License
1671867Smsmith *
1771867Smsmith * 2.1. This is your license from Intel Corp. under its intellectual property
1871867Smsmith * rights.  You may have additional license terms from the party that provided
1971867Smsmith * you this software, covering your right to use that party's intellectual
2071867Smsmith * property rights.
2171867Smsmith *
2271867Smsmith * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2371867Smsmith * copy of the source code appearing in this file ("Covered Code") an
2471867Smsmith * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2571867Smsmith * base code distributed originally by Intel ("Original Intel Code") to copy,
2671867Smsmith * make derivatives, distribute, use and display any portion of the Covered
2771867Smsmith * Code in any form, with the right to sublicense such rights; and
2871867Smsmith *
2971867Smsmith * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
3071867Smsmith * license (with the right to sublicense), under only those claims of Intel
3171867Smsmith * patents that are infringed by the Original Intel Code, to make, use, sell,
3271867Smsmith * offer to sell, and import the Covered Code and derivative works thereof
3371867Smsmith * solely to the minimum extent necessary to exercise the above copyright
3471867Smsmith * license, and in no event shall the patent license extend to any additions
3571867Smsmith * to or modifications of the Original Intel Code.  No other license or right
3671867Smsmith * is granted directly or by implication, estoppel or otherwise;
3771867Smsmith *
3871867Smsmith * The above copyright and patent license is granted only if the following
3971867Smsmith * conditions are met:
4071867Smsmith *
4171867Smsmith * 3. Conditions
4271867Smsmith *
4371867Smsmith * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4471867Smsmith * Redistribution of source code of any substantial portion of the Covered
4571867Smsmith * Code or modification with rights to further distribute source must include
4671867Smsmith * the above Copyright Notice, the above License, this list of Conditions,
4771867Smsmith * and the following Disclaimer and Export Compliance provision.  In addition,
4871867Smsmith * Licensee must cause all Covered Code to which Licensee contributes to
4971867Smsmith * contain a file documenting the changes Licensee made to create that Covered
5071867Smsmith * Code and the date of any change.  Licensee must include in that file the
5171867Smsmith * documentation of any changes made by any predecessor Licensee.  Licensee
5271867Smsmith * must include a prominent statement that the modification is derived,
5371867Smsmith * directly or indirectly, from Original Intel Code.
5471867Smsmith *
5571867Smsmith * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5671867Smsmith * Redistribution of source code of any substantial portion of the Covered
5771867Smsmith * Code or modification without rights to further distribute source must
5871867Smsmith * include the following Disclaimer and Export Compliance provision in the
5971867Smsmith * documentation and/or other materials provided with distribution.  In
6071867Smsmith * addition, Licensee may not authorize further sublicense of source of any
6171867Smsmith * portion of the Covered Code, and must include terms to the effect that the
6271867Smsmith * license from Licensee to its licensee is limited to the intellectual
6371867Smsmith * property embodied in the software Licensee provides to its licensee, and
6471867Smsmith * not to intellectual property embodied in modifications its licensee may
6571867Smsmith * make.
6671867Smsmith *
6771867Smsmith * 3.3. Redistribution of Executable. Redistribution in executable form of any
6871867Smsmith * substantial portion of the Covered Code or modification must reproduce the
6971867Smsmith * above Copyright Notice, and the following Disclaimer and Export Compliance
7071867Smsmith * provision in the documentation and/or other materials provided with the
7171867Smsmith * distribution.
7271867Smsmith *
7371867Smsmith * 3.4. Intel retains all right, title, and interest in and to the Original
7471867Smsmith * Intel Code.
7571867Smsmith *
7671867Smsmith * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7771867Smsmith * Intel shall be used in advertising or otherwise to promote the sale, use or
7871867Smsmith * other dealings in products derived from or relating to the Covered Code
7971867Smsmith * without prior written authorization from Intel.
8071867Smsmith *
8171867Smsmith * 4. Disclaimer and Export Compliance
8271867Smsmith *
8371867Smsmith * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8471867Smsmith * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8571867Smsmith * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8671867Smsmith * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8771867Smsmith * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8871867Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
8971867Smsmith * PARTICULAR PURPOSE.
9071867Smsmith *
9171867Smsmith * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9271867Smsmith * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9371867Smsmith * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9471867Smsmith * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9571867Smsmith * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9671867Smsmith * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9771867Smsmith * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9871867Smsmith * LIMITED REMEDY.
9971867Smsmith *
10071867Smsmith * 4.3. Licensee shall not export, either directly or indirectly, any of this
10171867Smsmith * software or system incorporating such software without first obtaining any
10271867Smsmith * required license or other approval from the U. S. Department of Commerce or
10371867Smsmith * any other agency or department of the United States Government.  In the
10471867Smsmith * event Licensee exports any such software from the United States or
10571867Smsmith * re-exports any such software from a foreign destination, Licensee shall
10671867Smsmith * ensure that the distribution and export/re-export of the software is in
10771867Smsmith * compliance with all laws, regulations, orders, or other restrictions of the
10871867Smsmith * U.S. Export Administration Regulations. Licensee agrees that neither it nor
10971867Smsmith * any of its subsidiaries will export/re-export any technical data, process,
11071867Smsmith * software, or service, directly or indirectly, to any country for which the
11171867Smsmith * United States government or any agency thereof requires an export license,
11271867Smsmith * other governmental approval, or letter of assurance, without first obtaining
11371867Smsmith * such license, approval or letter.
11471867Smsmith *
11571867Smsmith *****************************************************************************/
11671867Smsmith
11771867Smsmith#include <contrib/dev/acpica/include/acpi.h>
11871867Smsmith#include <contrib/dev/acpica/include/accommon.h>
11971867Smsmith
12071867Smsmith#define _COMPONENT          ACPI_HARDWARE
12171867Smsmith        ACPI_MODULE_NAME    ("hwtimer")
12271867Smsmith
12371867Smsmith
12471867Smsmith/******************************************************************************
12571867Smsmith *
12671867Smsmith * FUNCTION:    AcpiGetTimerResolution
12771867Smsmith *
12871867Smsmith * PARAMETERS:  Resolution          - Where the resolution is returned
12971867Smsmith *
13071867Smsmith * RETURN:      Status and timer resolution
13171867Smsmith *
13271867Smsmith * DESCRIPTION: Obtains resolution of the ACPI PM Timer (24 or 32 bits).
13371867Smsmith *
13471867Smsmith ******************************************************************************/
13571867Smsmith
13671867SmsmithACPI_STATUS
13771867SmsmithAcpiGetTimerResolution (
13871867Smsmith    UINT32                  *Resolution)
13971867Smsmith{
14071867Smsmith    ACPI_FUNCTION_TRACE (AcpiGetTimerResolution);
14171867Smsmith
14271867Smsmith
14371867Smsmith    if (!Resolution)
14471867Smsmith    {
14571867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
14671867Smsmith    }
14771867Smsmith
14871867Smsmith    if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
14971867Smsmith    {
15071867Smsmith        *Resolution = 24;
15171867Smsmith    }
15271867Smsmith    else
15371867Smsmith    {
15471867Smsmith        *Resolution = 32;
15571867Smsmith    }
15671867Smsmith
15771867Smsmith    return_ACPI_STATUS (AE_OK);
15871867Smsmith}
15971867Smsmith
16071867SmsmithACPI_EXPORT_SYMBOL (AcpiGetTimerResolution)
16171867Smsmith
16271867Smsmith
16371867Smsmith/******************************************************************************
16471867Smsmith *
16571867Smsmith * FUNCTION:    AcpiGetTimer
16671867Smsmith *
16771867Smsmith * PARAMETERS:  Ticks               - Where the timer value is returned
16871867Smsmith *
16971867Smsmith * RETURN:      Status and current timer value (ticks)
17071867Smsmith *
17171867Smsmith * DESCRIPTION: Obtains current value of ACPI PM Timer (in ticks).
17271867Smsmith *
17371867Smsmith ******************************************************************************/
17471867Smsmith
17571867SmsmithACPI_STATUS
17671867SmsmithAcpiGetTimer (
17771867Smsmith    UINT32                  *Ticks)
17871867Smsmith{
17971867Smsmith    ACPI_STATUS             Status;
18071867Smsmith
18171867Smsmith
18271867Smsmith    ACPI_FUNCTION_TRACE (AcpiGetTimer);
18371867Smsmith
18471867Smsmith
18571867Smsmith    if (!Ticks)
18671867Smsmith    {
18771867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
18871867Smsmith    }
18971867Smsmith
19071867Smsmith    Status = AcpiHwRead (Ticks, &AcpiGbl_FADT.XPmTimerBlock);
19171867Smsmith
19271867Smsmith    return_ACPI_STATUS (Status);
19371867Smsmith}
19471867Smsmith
19571867SmsmithACPI_EXPORT_SYMBOL (AcpiGetTimer)
19671867Smsmith
19771867Smsmith
19871867Smsmith/******************************************************************************
19971867Smsmith *
20071867Smsmith * FUNCTION:    AcpiGetTimerDuration
20171867Smsmith *
20271867Smsmith * PARAMETERS:  StartTicks          - Starting timestamp
20371867Smsmith *              EndTicks            - End timestamp
20471867Smsmith *              TimeElapsed         - Where the elapsed time is returned
20571867Smsmith *
20671867Smsmith * RETURN:      Status and TimeElapsed
20771867Smsmith *
20871867Smsmith * DESCRIPTION: Computes the time elapsed (in microseconds) between two
20971867Smsmith *              PM Timer time stamps, taking into account the possibility of
21071867Smsmith *              rollovers, the timer resolution, and timer frequency.
21171867Smsmith *
21271867Smsmith *              The PM Timer's clock ticks at roughly 3.6 times per
21371867Smsmith *              _microsecond_, and its clock continues through Cx state
21471867Smsmith *              transitions (unlike many CPU timestamp counters) -- making it
21571867Smsmith *              a versatile and accurate timer.
21671867Smsmith *
21771867Smsmith *              Note that this function accommodates only a single timer
21871867Smsmith *              rollover.  Thus for 24-bit timers, this function should only
21971867Smsmith *              be used for calculating durations less than ~4.6 seconds
22071867Smsmith *              (~20 minutes for 32-bit timers) -- calculations below:
22171867Smsmith *
22271867Smsmith *              2**24 Ticks / 3,600,000 Ticks/Sec = 4.66 sec
22371867Smsmith *              2**32 Ticks / 3,600,000 Ticks/Sec = 1193 sec or 19.88 minutes
22471867Smsmith *
22571867Smsmith ******************************************************************************/
22671867Smsmith
22771867SmsmithACPI_STATUS
22871867SmsmithAcpiGetTimerDuration (
22971867Smsmith    UINT32                  StartTicks,
23071867Smsmith    UINT32                  EndTicks,
23171867Smsmith    UINT32                  *TimeElapsed)
23271867Smsmith{
23371867Smsmith    ACPI_STATUS             Status;
23471867Smsmith    UINT32                  DeltaTicks;
23571867Smsmith    UINT64                  Quotient;
23671867Smsmith
23771867Smsmith
23871867Smsmith    ACPI_FUNCTION_TRACE (AcpiGetTimerDuration);
23971867Smsmith
24071867Smsmith
24171867Smsmith    if (!TimeElapsed)
24271867Smsmith    {
24371867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
24471867Smsmith    }
24571867Smsmith
24671867Smsmith    /*
24771867Smsmith     * Compute Tick Delta:
24871867Smsmith     * Handle (max one) timer rollovers on 24-bit versus 32-bit timers.
24971867Smsmith     */
25071867Smsmith    if (StartTicks < EndTicks)
25171867Smsmith    {
25271867Smsmith        DeltaTicks = EndTicks - StartTicks;
25371867Smsmith    }
25471867Smsmith    else if (StartTicks > EndTicks)
25571867Smsmith    {
25671867Smsmith        if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
25771867Smsmith        {
25871867Smsmith            /* 24-bit Timer */
25971867Smsmith
26071867Smsmith            DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF);
26171867Smsmith        }
26271867Smsmith        else
26371867Smsmith        {
26471867Smsmith            /* 32-bit Timer */
26571867Smsmith
26671867Smsmith            DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks;
26771867Smsmith        }
26871867Smsmith    }
26971867Smsmith    else /* StartTicks == EndTicks */
27071867Smsmith    {
27171867Smsmith        *TimeElapsed = 0;
27271867Smsmith        return_ACPI_STATUS (AE_OK);
27371867Smsmith    }
27471867Smsmith
27571867Smsmith    /*
27671867Smsmith     * Compute Duration (Requires a 64-bit multiply and divide):
27771867Smsmith     *
27871867Smsmith     * TimeElapsed = (DeltaTicks * 1000000) / PM_TIMER_FREQUENCY;
27971867Smsmith     */
28071867Smsmith    Status = AcpiUtShortDivide (((UINT64) DeltaTicks) * 1000000,
28171867Smsmith                PM_TIMER_FREQUENCY, &Quotient, NULL);
28271867Smsmith
28371867Smsmith    *TimeElapsed = (UINT32) Quotient;
28471867Smsmith    return_ACPI_STATUS (Status);
28571867Smsmith}
28671867Smsmith
28771867SmsmithACPI_EXPORT_SYMBOL (AcpiGetTimerDuration)
28871867Smsmith
28971867Smsmith