hwtimer.c revision 78986
171867Smsmith
271867Smsmith/******************************************************************************
371867Smsmith *
471867Smsmith * Name: hwtimer.c - ACPI Power Management Timer Interface
578986Smsmith *              $Revision: 10 $
671867Smsmith *
771867Smsmith *****************************************************************************/
871867Smsmith
971867Smsmith/******************************************************************************
1071867Smsmith *
1171867Smsmith * 1. Copyright Notice
1271867Smsmith *
1371867Smsmith * Some or all of this work - Copyright (c) 1999, 2000, 2001, Intel Corp.
1471867Smsmith * All rights reserved.
1571867Smsmith *
1671867Smsmith * 2. License
1771867Smsmith *
1871867Smsmith * 2.1. This is your license from Intel Corp. under its intellectual property
1971867Smsmith * rights.  You may have additional license terms from the party that provided
2071867Smsmith * you this software, covering your right to use that party's intellectual
2171867Smsmith * property rights.
2271867Smsmith *
2371867Smsmith * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2471867Smsmith * copy of the source code appearing in this file ("Covered Code") an
2571867Smsmith * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2671867Smsmith * base code distributed originally by Intel ("Original Intel Code") to copy,
2771867Smsmith * make derivatives, distribute, use and display any portion of the Covered
2871867Smsmith * Code in any form, with the right to sublicense such rights; and
2971867Smsmith *
3071867Smsmith * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
3171867Smsmith * license (with the right to sublicense), under only those claims of Intel
3271867Smsmith * patents that are infringed by the Original Intel Code, to make, use, sell,
3371867Smsmith * offer to sell, and import the Covered Code and derivative works thereof
3471867Smsmith * solely to the minimum extent necessary to exercise the above copyright
3571867Smsmith * license, and in no event shall the patent license extend to any additions
3671867Smsmith * to or modifications of the Original Intel Code.  No other license or right
3771867Smsmith * is granted directly or by implication, estoppel or otherwise;
3871867Smsmith *
3971867Smsmith * The above copyright and patent license is granted only if the following
4071867Smsmith * conditions are met:
4171867Smsmith *
4271867Smsmith * 3. Conditions
4371867Smsmith *
4471867Smsmith * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4571867Smsmith * Redistribution of source code of any substantial portion of the Covered
4671867Smsmith * Code or modification with rights to further distribute source must include
4771867Smsmith * the above Copyright Notice, the above License, this list of Conditions,
4871867Smsmith * and the following Disclaimer and Export Compliance provision.  In addition,
4971867Smsmith * Licensee must cause all Covered Code to which Licensee contributes to
5071867Smsmith * contain a file documenting the changes Licensee made to create that Covered
5171867Smsmith * Code and the date of any change.  Licensee must include in that file the
5271867Smsmith * documentation of any changes made by any predecessor Licensee.  Licensee
5371867Smsmith * must include a prominent statement that the modification is derived,
5471867Smsmith * directly or indirectly, from Original Intel Code.
5571867Smsmith *
5671867Smsmith * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5771867Smsmith * Redistribution of source code of any substantial portion of the Covered
5871867Smsmith * Code or modification without rights to further distribute source must
5971867Smsmith * include the following Disclaimer and Export Compliance provision in the
6071867Smsmith * documentation and/or other materials provided with distribution.  In
6171867Smsmith * addition, Licensee may not authorize further sublicense of source of any
6271867Smsmith * portion of the Covered Code, and must include terms to the effect that the
6371867Smsmith * license from Licensee to its licensee is limited to the intellectual
6471867Smsmith * property embodied in the software Licensee provides to its licensee, and
6571867Smsmith * not to intellectual property embodied in modifications its licensee may
6671867Smsmith * make.
6771867Smsmith *
6871867Smsmith * 3.3. Redistribution of Executable. Redistribution in executable form of any
6971867Smsmith * substantial portion of the Covered Code or modification must reproduce the
7071867Smsmith * above Copyright Notice, and the following Disclaimer and Export Compliance
7171867Smsmith * provision in the documentation and/or other materials provided with the
7271867Smsmith * distribution.
7371867Smsmith *
7471867Smsmith * 3.4. Intel retains all right, title, and interest in and to the Original
7571867Smsmith * Intel Code.
7671867Smsmith *
7771867Smsmith * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7871867Smsmith * Intel shall be used in advertising or otherwise to promote the sale, use or
7971867Smsmith * other dealings in products derived from or relating to the Covered Code
8071867Smsmith * without prior written authorization from Intel.
8171867Smsmith *
8271867Smsmith * 4. Disclaimer and Export Compliance
8371867Smsmith *
8471867Smsmith * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8571867Smsmith * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8671867Smsmith * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8771867Smsmith * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8871867Smsmith * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8971867Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
9071867Smsmith * PARTICULAR PURPOSE.
9171867Smsmith *
9271867Smsmith * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9371867Smsmith * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9471867Smsmith * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9571867Smsmith * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9671867Smsmith * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9771867Smsmith * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9871867Smsmith * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9971867Smsmith * LIMITED REMEDY.
10071867Smsmith *
10171867Smsmith * 4.3. Licensee shall not export, either directly or indirectly, any of this
10271867Smsmith * software or system incorporating such software without first obtaining any
10371867Smsmith * required license or other approval from the U. S. Department of Commerce or
10471867Smsmith * any other agency or department of the United States Government.  In the
10571867Smsmith * event Licensee exports any such software from the United States or
10671867Smsmith * re-exports any such software from a foreign destination, Licensee shall
10771867Smsmith * ensure that the distribution and export/re-export of the software is in
10871867Smsmith * compliance with all laws, regulations, orders, or other restrictions of the
10971867Smsmith * U.S. Export Administration Regulations. Licensee agrees that neither it nor
11071867Smsmith * any of its subsidiaries will export/re-export any technical data, process,
11171867Smsmith * software, or service, directly or indirectly, to any country for which the
11271867Smsmith * United States government or any agency thereof requires an export license,
11371867Smsmith * other governmental approval, or letter of assurance, without first obtaining
11471867Smsmith * such license, approval or letter.
11571867Smsmith *
11671867Smsmith *****************************************************************************/
11771867Smsmith
11871867Smsmith#include "acpi.h"
11971867Smsmith#include "achware.h"
12071867Smsmith
12177424Smsmith#define _COMPONENT          ACPI_HARDWARE
12271867Smsmith        MODULE_NAME         ("hwtimer")
12371867Smsmith
12471867Smsmith
12571867Smsmith/******************************************************************************
12671867Smsmith *
12771867Smsmith * FUNCTION:    AcpiGetTimerResolution
12871867Smsmith *
12971867Smsmith * PARAMETERS:  none
13071867Smsmith *
13171867Smsmith * RETURN:      Number of bits of resolution in the PM Timer (24 or 32).
13271867Smsmith *
13371867Smsmith * DESCRIPTION: Obtains resolution of the ACPI PM Timer.
13471867Smsmith *
13571867Smsmith ******************************************************************************/
13671867Smsmith
13771867SmsmithACPI_STATUS
13871867SmsmithAcpiGetTimerResolution (
13971867Smsmith    UINT32                  *Resolution)
14071867Smsmith{
14178986Smsmith    ACPI_STATUS             Status;
14277424Smsmith
14378986Smsmith
14471867Smsmith    FUNCTION_TRACE ("AcpiGetTimerResolution");
14571867Smsmith
14677424Smsmith
14777424Smsmith    /* Ensure that ACPI has been initialized */
14877424Smsmith
14977424Smsmith    ACPI_IS_INITIALIZATION_COMPLETE (Status);
15077424Smsmith    if (ACPI_FAILURE (Status))
15177424Smsmith    {
15277424Smsmith        return_ACPI_STATUS (Status);
15377424Smsmith    }
15477424Smsmith
15571867Smsmith    if (!Resolution)
15671867Smsmith    {
15771867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
15871867Smsmith    }
15971867Smsmith
16071867Smsmith    if (0 == AcpiGbl_FADT->TmrValExt)
16171867Smsmith    {
16271867Smsmith        *Resolution = 24;
16371867Smsmith    }
16471867Smsmith    else
16571867Smsmith    {
16671867Smsmith        *Resolution = 32;
16771867Smsmith    }
16871867Smsmith
16971867Smsmith    return_ACPI_STATUS (AE_OK);
17071867Smsmith}
17171867Smsmith
17271867Smsmith
17371867Smsmith/******************************************************************************
17471867Smsmith *
17571867Smsmith * FUNCTION:    AcpiGetTimer
17671867Smsmith *
17771867Smsmith * PARAMETERS:  none
17871867Smsmith *
17971867Smsmith * RETURN:      Current value of the ACPI PM Timer (in ticks).
18071867Smsmith *
18171867Smsmith * DESCRIPTION: Obtains current value of ACPI PM Timer.
18271867Smsmith *
18371867Smsmith ******************************************************************************/
18471867Smsmith
18571867SmsmithACPI_STATUS
18671867SmsmithAcpiGetTimer (
18771867Smsmith    UINT32                  *Ticks)
18871867Smsmith{
18978986Smsmith    ACPI_STATUS             Status;
19077424Smsmith
19178986Smsmith
19271867Smsmith    FUNCTION_TRACE ("AcpiGetTimer");
19371867Smsmith
19477424Smsmith
19577424Smsmith    /* Ensure that ACPI has been initialized */
19677424Smsmith
19777424Smsmith    ACPI_IS_INITIALIZATION_COMPLETE (Status);
19877424Smsmith    if (ACPI_FAILURE (Status))
19977424Smsmith    {
20077424Smsmith        return_ACPI_STATUS (Status);
20177424Smsmith    }
20277424Smsmith
20371867Smsmith    if (!Ticks)
20471867Smsmith    {
20571867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
20671867Smsmith    }
20771867Smsmith
20871867Smsmith    *Ticks = AcpiOsIn32 ((ACPI_IO_ADDRESS) ACPI_GET_ADDRESS (AcpiGbl_FADT->XPmTmrBlk.Address));
20971867Smsmith
21071867Smsmith    return_ACPI_STATUS (AE_OK);
21171867Smsmith}
21271867Smsmith
21371867Smsmith
21471867Smsmith/******************************************************************************
21571867Smsmith *
21671867Smsmith * FUNCTION:    AcpiGetTimerDuration
21771867Smsmith *
21871867Smsmith * PARAMETERS:  StartTicks
21971867Smsmith *              EndTicks
22071867Smsmith *              TimeElapsed
22171867Smsmith *
22277424Smsmith * RETURN:      TimeElapsed
22371867Smsmith *
22471867Smsmith * DESCRIPTION: Computes the time elapsed (in microseconds) between two
22571867Smsmith *              PM Timer time stamps, taking into account the possibility of
22677424Smsmith *              rollovers, the timer resolution, and timer frequency.
22777424Smsmith *
22877424Smsmith *              The PM Timer's clock ticks at roughly 3.6 times per
22971867Smsmith *              _microsecond_, and its clock continues through Cx state
23071867Smsmith *              transitions (unlike many CPU timestamp counters) -- making it
23171867Smsmith *              a versatile and accurate timer.
23271867Smsmith *
23377424Smsmith *              Note that this function accomodates only a single timer
23471867Smsmith *              rollover.  Thus for 24-bit timers, this function should only
23577424Smsmith *              be used for calculating durations less than ~4.6 seconds
23671867Smsmith *              (~20 hours for 32-bit timers).
23771867Smsmith *
23871867Smsmith ******************************************************************************/
23971867Smsmith
24071867SmsmithACPI_STATUS
24171867SmsmithAcpiGetTimerDuration (
24271867Smsmith    UINT32                  StartTicks,
24371867Smsmith    UINT32                  EndTicks,
24471867Smsmith    UINT32                  *TimeElapsed)
24571867Smsmith{
24671867Smsmith    UINT32                  DeltaTicks = 0;
24771867Smsmith    UINT32                  Seconds = 0;
24871867Smsmith    UINT32                  Milliseconds = 0;
24971867Smsmith    UINT32                  Microseconds = 0;
25071867Smsmith    UINT32                  Remainder = 0;
25171867Smsmith
25278986Smsmith
25371867Smsmith    FUNCTION_TRACE ("AcpiGetTimerDuration");
25471867Smsmith
25577424Smsmith    if (!TimeElapsed)
25671867Smsmith    {
25771867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
25871867Smsmith    }
25971867Smsmith
26077424Smsmith    /*
26171867Smsmith     * Compute Tick Delta:
26271867Smsmith     * -------------------
26373561Smsmith     * Handle (max one) timer rollovers on 24- versus 32-bit timers.
26471867Smsmith     */
26571867Smsmith    if (StartTicks < EndTicks)
26671867Smsmith    {
26771867Smsmith        DeltaTicks = EndTicks - StartTicks;
26871867Smsmith    }
26977424Smsmith    else if (StartTicks > EndTicks)
27071867Smsmith    {
27171867Smsmith        /* 24-bit Timer */
27271867Smsmith        if (0 == AcpiGbl_FADT->TmrValExt)
27371867Smsmith        {
27473561Smsmith            DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF);
27571867Smsmith        }
27671867Smsmith        /* 32-bit Timer */
27777424Smsmith        else
27871867Smsmith        {
27971867Smsmith            DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks;
28071867Smsmith        }
28171867Smsmith    }
28273561Smsmith    else
28373561Smsmith    {
28473561Smsmith        *TimeElapsed = 0;
28573561Smsmith        return_ACPI_STATUS (AE_OK);
28673561Smsmith    }
28771867Smsmith
28877424Smsmith    /*
28971867Smsmith     * Compute Duration:
29071867Smsmith     * -----------------
29177424Smsmith     * Since certain compilers (gcc/Linux, argh!) don't support 64-bit
29277424Smsmith     * divides in kernel-space we have to do some trickery to preserve
29371867Smsmith     * accuracy while using 32-bit math.
29471867Smsmith     *
29577424Smsmith     * TBD: Change to use 64-bit math when supported.
29677424Smsmith     *
29771867Smsmith     * The process is as follows:
29877424Smsmith     *  1. Compute the number of seconds by dividing Delta Ticks by
29971867Smsmith     *     the timer frequency.
30071867Smsmith     *  2. Compute the number of milliseconds in the remainder from step #1
30171867Smsmith     *     by multiplying by 1000 and then dividing by the timer frequency.
30271867Smsmith     *  3. Compute the number of microseconds in the remainder from step #2
30371867Smsmith     *     by multiplying by 1000 and then dividing by the timer frequency.
30471867Smsmith     *  4. Add the results from steps 1, 2, and 3 to get the total duration.
30571867Smsmith     *
30677424Smsmith     * Example: The time elapsed for DeltaTicks = 0xFFFFFFFF should be
30771867Smsmith     *          1199864031 microseconds.  This is computed as follows:
30871867Smsmith     *          Step #1: Seconds = 1199; Remainder = 3092840
30971867Smsmith     *          Step #2: Milliseconds = 864; Remainder = 113120
31071867Smsmith     *          Step #3: Microseconds = 31; Remainder = <don't care!>
31171867Smsmith     */
31271867Smsmith
31371867Smsmith    /* Step #1 */
31471867Smsmith    Seconds = DeltaTicks / PM_TIMER_FREQUENCY;
31571867Smsmith    Remainder = DeltaTicks % PM_TIMER_FREQUENCY;
31671867Smsmith
31771867Smsmith    /* Step #2 */
31871867Smsmith    Milliseconds = (Remainder * 1000) / PM_TIMER_FREQUENCY;
31971867Smsmith    Remainder = (Remainder * 1000) % PM_TIMER_FREQUENCY;
32071867Smsmith
32171867Smsmith    /* Step #3 */
32271867Smsmith    Microseconds = (Remainder * 1000) / PM_TIMER_FREQUENCY;
32371867Smsmith
32471867Smsmith    /* Step #4 */
32571867Smsmith    *TimeElapsed = Seconds * 1000000;
32671867Smsmith    *TimeElapsed += Milliseconds * 1000;
32771867Smsmith    *TimeElapsed += Microseconds;
32871867Smsmith
32971867Smsmith    return_ACPI_STATUS (AE_OK);
33071867Smsmith}
33171867Smsmith
33271867Smsmith
333