hwtimer.c revision 197104
1145256Sjkoshy
2183266Sjkoshy/******************************************************************************
3174395Sjkoshy *
4145256Sjkoshy * Name: hwtimer.c - ACPI Power Management Timer Interface
5145256Sjkoshy *
6174395Sjkoshy *****************************************************************************/
7174395Sjkoshy
8174395Sjkoshy/******************************************************************************
9145256Sjkoshy *
10145256Sjkoshy * 1. Copyright Notice
11145256Sjkoshy *
12145256Sjkoshy * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13145256Sjkoshy * All rights reserved.
14145256Sjkoshy *
15145256Sjkoshy * 2. License
16145256Sjkoshy *
17145256Sjkoshy * 2.1. This is your license from Intel Corp. under its intellectual property
18145256Sjkoshy * rights.  You may have additional license terms from the party that provided
19145256Sjkoshy * you this software, covering your right to use that party's intellectual
20145256Sjkoshy * property rights.
21145256Sjkoshy *
22145256Sjkoshy * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23145256Sjkoshy * copy of the source code appearing in this file ("Covered Code") an
24145256Sjkoshy * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25145256Sjkoshy * base code distributed originally by Intel ("Original Intel Code") to copy,
26145256Sjkoshy * make derivatives, distribute, use and display any portion of the Covered
27145256Sjkoshy * Code in any form, with the right to sublicense such rights; and
28145256Sjkoshy *
29145256Sjkoshy * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30145256Sjkoshy * license (with the right to sublicense), under only those claims of Intel
31145256Sjkoshy * patents that are infringed by the Original Intel Code, to make, use, sell,
32145256Sjkoshy * offer to sell, and import the Covered Code and derivative works thereof
33145256Sjkoshy * solely to the minimum extent necessary to exercise the above copyright
34145256Sjkoshy * license, and in no event shall the patent license extend to any additions
35145256Sjkoshy * to or modifications of the Original Intel Code.  No other license or right
36145256Sjkoshy * is granted directly or by implication, estoppel or otherwise;
37145256Sjkoshy *
38145256Sjkoshy * The above copyright and patent license is granted only if the following
39145256Sjkoshy * conditions are met:
40145338Smarcel *
41183266Sjkoshy * 3. Conditions
42145256Sjkoshy *
43145256Sjkoshy * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44145256Sjkoshy * Redistribution of source code of any substantial portion of the Covered
45174395Sjkoshy * Code or modification with rights to further distribute source must include
46147191Sjkoshy * the above Copyright Notice, the above License, this list of Conditions,
47145256Sjkoshy * and the following Disclaimer and Export Compliance provision.  In addition,
48147191Sjkoshy * Licensee must cause all Covered Code to which Licensee contributes to
49145256Sjkoshy * contain a file documenting the changes Licensee made to create that Covered
50153110Sru * Code and the date of any change.  Licensee must include in that file the
51147191Sjkoshy * documentation of any changes made by any predecessor Licensee.  Licensee
52145256Sjkoshy * must include a prominent statement that the modification is derived,
53145256Sjkoshy * directly or indirectly, from Original Intel Code.
54145256Sjkoshy *
55145256Sjkoshy * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56145256Sjkoshy * Redistribution of source code of any substantial portion of the Covered
57145256Sjkoshy * Code or modification without rights to further distribute source must
58145256Sjkoshy * include the following Disclaimer and Export Compliance provision in the
59145256Sjkoshy * documentation and/or other materials provided with distribution.  In
60145256Sjkoshy * addition, Licensee may not authorize further sublicense of source of any
61147191Sjkoshy * portion of the Covered Code, and must include terms to the effect that the
62145256Sjkoshy * license from Licensee to its licensee is limited to the intellectual
63145256Sjkoshy * property embodied in the software Licensee provides to its licensee, and
64145256Sjkoshy * not to intellectual property embodied in modifications its licensee may
65145256Sjkoshy * make.
66147191Sjkoshy *
67147191Sjkoshy * 3.3. Redistribution of Executable. Redistribution in executable form of any
68145256Sjkoshy * substantial portion of the Covered Code or modification must reproduce the
69145256Sjkoshy * above Copyright Notice, and the following Disclaimer and Export Compliance
70145256Sjkoshy * provision in the documentation and/or other materials provided with the
71145256Sjkoshy * distribution.
72145256Sjkoshy *
73145256Sjkoshy * 3.4. Intel retains all right, title, and interest in and to the Original
74145256Sjkoshy * Intel Code.
75145256Sjkoshy *
76145256Sjkoshy * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77147191Sjkoshy * Intel shall be used in advertising or otherwise to promote the sale, use or
78147191Sjkoshy * other dealings in products derived from or relating to the Covered Code
79145256Sjkoshy * without prior written authorization from Intel.
80145256Sjkoshy *
81145256Sjkoshy * 4. Disclaimer and Export Compliance
82145256Sjkoshy *
83145256Sjkoshy * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84145256Sjkoshy * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85145256Sjkoshy * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86145256Sjkoshy * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87145256Sjkoshy * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88147191Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89147191Sjkoshy * PARTICULAR PURPOSE.
90145256Sjkoshy *
91145256Sjkoshy * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92145256Sjkoshy * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93145256Sjkoshy * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94145256Sjkoshy * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95145256Sjkoshy * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96145256Sjkoshy * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97145256Sjkoshy * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98145256Sjkoshy * LIMITED REMEDY.
99147191Sjkoshy *
100147191Sjkoshy * 4.3. Licensee shall not export, either directly or indirectly, any of this
101145256Sjkoshy * software or system incorporating such software without first obtaining any
102145256Sjkoshy * required license or other approval from the U. S. Department of Commerce or
103145256Sjkoshy * any other agency or department of the United States Government.  In the
104145256Sjkoshy * event Licensee exports any such software from the United States or
105145256Sjkoshy * re-exports any such software from a foreign destination, Licensee shall
106145256Sjkoshy * ensure that the distribution and export/re-export of the software is in
107145256Sjkoshy * compliance with all laws, regulations, orders, or other restrictions of the
108145256Sjkoshy * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109145256Sjkoshy * any of its subsidiaries will export/re-export any technical data, process,
110145256Sjkoshy * software, or service, directly or indirectly, to any country for which the
111145256Sjkoshy * United States government or any agency thereof requires an export license,
112145256Sjkoshy * other governmental approval, or letter of assurance, without first obtaining
113145256Sjkoshy * such license, approval or letter.
114145256Sjkoshy *
115145256Sjkoshy *****************************************************************************/
116147191Sjkoshy
117145256Sjkoshy#include <contrib/dev/acpica/include/acpi.h>
118145256Sjkoshy#include <contrib/dev/acpica/include/accommon.h>
119147191Sjkoshy
120147191Sjkoshy#define _COMPONENT          ACPI_HARDWARE
121147191Sjkoshy        ACPI_MODULE_NAME    ("hwtimer")
122145256Sjkoshy
123145256Sjkoshy
124145256Sjkoshy/******************************************************************************
125145256Sjkoshy *
126145256Sjkoshy * FUNCTION:    AcpiGetTimerResolution
127145256Sjkoshy *
128145256Sjkoshy * PARAMETERS:  Resolution          - Where the resolution is returned
129145256Sjkoshy *
130145256Sjkoshy * RETURN:      Status and timer resolution
131145256Sjkoshy *
132145256Sjkoshy * DESCRIPTION: Obtains resolution of the ACPI PM Timer (24 or 32 bits).
133145256Sjkoshy *
134145256Sjkoshy ******************************************************************************/
135145256Sjkoshy
136145256SjkoshyACPI_STATUS
137145256SjkoshyAcpiGetTimerResolution (
138145256Sjkoshy    UINT32                  *Resolution)
139145256Sjkoshy{
140145256Sjkoshy    ACPI_FUNCTION_TRACE (AcpiGetTimerResolution);
141145256Sjkoshy
142147191Sjkoshy
143145256Sjkoshy    if (!Resolution)
144145256Sjkoshy    {
145145256Sjkoshy        return_ACPI_STATUS (AE_BAD_PARAMETER);
146145256Sjkoshy    }
147145256Sjkoshy
148145256Sjkoshy    if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
149145256Sjkoshy    {
150145256Sjkoshy        *Resolution = 24;
151145256Sjkoshy    }
152145256Sjkoshy    else
153145256Sjkoshy    {
154145256Sjkoshy        *Resolution = 32;
155145256Sjkoshy    }
156145256Sjkoshy
157145256Sjkoshy    return_ACPI_STATUS (AE_OK);
158145256Sjkoshy}
159145256Sjkoshy
160145256SjkoshyACPI_EXPORT_SYMBOL (AcpiGetTimerResolution)
161145256Sjkoshy
162145256Sjkoshy
163145256Sjkoshy/******************************************************************************
164145256Sjkoshy *
165145256Sjkoshy * FUNCTION:    AcpiGetTimer
166145256Sjkoshy *
167145256Sjkoshy * PARAMETERS:  Ticks               - Where the timer value is returned
168145256Sjkoshy *
169145256Sjkoshy * RETURN:      Status and current timer value (ticks)
170145256Sjkoshy *
171145256Sjkoshy * DESCRIPTION: Obtains current value of ACPI PM Timer (in ticks).
172145256Sjkoshy *
173145256Sjkoshy ******************************************************************************/
174145256Sjkoshy
175145256SjkoshyACPI_STATUS
176145256SjkoshyAcpiGetTimer (
177145256Sjkoshy    UINT32                  *Ticks)
178145256Sjkoshy{
179145256Sjkoshy    ACPI_STATUS             Status;
180145256Sjkoshy
181145256Sjkoshy
182145256Sjkoshy    ACPI_FUNCTION_TRACE (AcpiGetTimer);
183145256Sjkoshy
184145256Sjkoshy
185145256Sjkoshy    if (!Ticks)
186145256Sjkoshy    {
187145256Sjkoshy        return_ACPI_STATUS (AE_BAD_PARAMETER);
188145256Sjkoshy    }
189145256Sjkoshy
190145256Sjkoshy    Status = AcpiHwRead (Ticks, &AcpiGbl_FADT.XPmTimerBlock);
191145256Sjkoshy
192145256Sjkoshy    return_ACPI_STATUS (Status);
193145256Sjkoshy}
194145256Sjkoshy
195145256SjkoshyACPI_EXPORT_SYMBOL (AcpiGetTimer)
196145256Sjkoshy
197145256Sjkoshy
198145256Sjkoshy/******************************************************************************
199145256Sjkoshy *
200145256Sjkoshy * FUNCTION:    AcpiGetTimerDuration
201145256Sjkoshy *
202145256Sjkoshy * PARAMETERS:  StartTicks          - Starting timestamp
203145256Sjkoshy *              EndTicks            - End timestamp
204145256Sjkoshy *              TimeElapsed         - Where the elapsed time is returned
205145256Sjkoshy *
206145256Sjkoshy * RETURN:      Status and TimeElapsed
207145256Sjkoshy *
208145256Sjkoshy * DESCRIPTION: Computes the time elapsed (in microseconds) between two
209145256Sjkoshy *              PM Timer time stamps, taking into account the possibility of
210145256Sjkoshy *              rollovers, the timer resolution, and timer frequency.
211145256Sjkoshy *
212145256Sjkoshy *              The PM Timer's clock ticks at roughly 3.6 times per
213145256Sjkoshy *              _microsecond_, and its clock continues through Cx state
214145256Sjkoshy *              transitions (unlike many CPU timestamp counters) -- making it
215145256Sjkoshy *              a versatile and accurate timer.
216145256Sjkoshy *
217145256Sjkoshy *              Note that this function accommodates only a single timer
218145256Sjkoshy *              rollover.  Thus for 24-bit timers, this function should only
219145256Sjkoshy *              be used for calculating durations less than ~4.6 seconds
220145256Sjkoshy *              (~20 minutes for 32-bit timers) -- calculations below:
221145256Sjkoshy *
222145256Sjkoshy *              2**24 Ticks / 3,600,000 Ticks/Sec = 4.66 sec
223145256Sjkoshy *              2**32 Ticks / 3,600,000 Ticks/Sec = 1193 sec or 19.88 minutes
224145256Sjkoshy *
225145256Sjkoshy ******************************************************************************/
226145256Sjkoshy
227145256SjkoshyACPI_STATUS
228145256SjkoshyAcpiGetTimerDuration (
229145256Sjkoshy    UINT32                  StartTicks,
230145256Sjkoshy    UINT32                  EndTicks,
231145256Sjkoshy    UINT32                  *TimeElapsed)
232145256Sjkoshy{
233145256Sjkoshy    ACPI_STATUS             Status;
234145256Sjkoshy    UINT32                  DeltaTicks;
235145256Sjkoshy    ACPI_INTEGER            Quotient;
236145256Sjkoshy
237145256Sjkoshy
238145256Sjkoshy    ACPI_FUNCTION_TRACE (AcpiGetTimerDuration);
239145256Sjkoshy
240145256Sjkoshy
241145256Sjkoshy    if (!TimeElapsed)
242145256Sjkoshy    {
243145256Sjkoshy        return_ACPI_STATUS (AE_BAD_PARAMETER);
244145256Sjkoshy    }
245145256Sjkoshy
246145256Sjkoshy    /*
247145256Sjkoshy     * Compute Tick Delta:
248184802Sjkoshy     * Handle (max one) timer rollovers on 24-bit versus 32-bit timers.
249184802Sjkoshy     */
250184802Sjkoshy    if (StartTicks < EndTicks)
251184802Sjkoshy    {
252184802Sjkoshy        DeltaTicks = EndTicks - StartTicks;
253184802Sjkoshy    }
254184802Sjkoshy    else if (StartTicks > EndTicks)
255184802Sjkoshy    {
256184802Sjkoshy        if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
257184802Sjkoshy        {
258145256Sjkoshy            /* 24-bit Timer */
259145256Sjkoshy
260145256Sjkoshy            DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF);
261145256Sjkoshy        }
262145256Sjkoshy        else
263145256Sjkoshy        {
264145256Sjkoshy            /* 32-bit Timer */
265145256Sjkoshy
266145256Sjkoshy            DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks;
267145256Sjkoshy        }
268145256Sjkoshy    }
269183266Sjkoshy    else /* StartTicks == EndTicks */
270145256Sjkoshy    {
271145256Sjkoshy        *TimeElapsed = 0;
272145256Sjkoshy        return_ACPI_STATUS (AE_OK);
273184802Sjkoshy    }
274184802Sjkoshy
275145256Sjkoshy    /*
276184802Sjkoshy     * Compute Duration (Requires a 64-bit multiply and divide):
277184802Sjkoshy     *
278145256Sjkoshy     * TimeElapsed = (DeltaTicks * 1000000) / PM_TIMER_FREQUENCY;
279145256Sjkoshy     */
280145256Sjkoshy    Status = AcpiUtShortDivide (((UINT64) DeltaTicks) * 1000000,
281145256Sjkoshy                PM_TIMER_FREQUENCY, &Quotient, NULL);
282145256Sjkoshy
283145774Sjkoshy    *TimeElapsed = (UINT32) Quotient;
284145256Sjkoshy    return_ACPI_STATUS (Status);
285145256Sjkoshy}
286145256Sjkoshy
287153110SruACPI_EXPORT_SYMBOL (AcpiGetTimerDuration)
288147191Sjkoshy
289145256Sjkoshy