hwtimer.c revision 99679
1218885Sdim
2218885Sdim/******************************************************************************
3218885Sdim *
4218885Sdim * Name: hwtimer.c - ACPI Power Management Timer Interface
5218885Sdim *              $Revision: 21 $
6218885Sdim *
7218885Sdim *****************************************************************************/
8218885Sdim
9218885Sdim/******************************************************************************
10218885Sdim *
11218885Sdim * 1. Copyright Notice
12218885Sdim *
13218885Sdim * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp.
14218885Sdim * All rights reserved.
15218885Sdim *
16218885Sdim * 2. License
17218885Sdim *
18218885Sdim * 2.1. This is your license from Intel Corp. under its intellectual property
19276479Sdim * rights.  You may have additional license terms from the party that provided
20218885Sdim * you this software, covering your right to use that party's intellectual
21218885Sdim * property rights.
22218885Sdim *
23218885Sdim * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24226633Sdim * copy of the source code appearing in this file ("Covered Code") an
25226633Sdim * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26226633Sdim * base code distributed originally by Intel ("Original Intel Code") to copy,
27226633Sdim * make derivatives, distribute, use and display any portion of the Covered
28226633Sdim * Code in any form, with the right to sublicense such rights; and
29226633Sdim *
30226633Sdim * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31226633Sdim * license (with the right to sublicense), under only those claims of Intel
32226633Sdim * patents that are infringed by the Original Intel Code, to make, use, sell,
33226633Sdim * offer to sell, and import the Covered Code and derivative works thereof
34226633Sdim * solely to the minimum extent necessary to exercise the above copyright
35226633Sdim * license, and in no event shall the patent license extend to any additions
36226633Sdim * to or modifications of the Original Intel Code.  No other license or right
37226633Sdim * is granted directly or by implication, estoppel or otherwise;
38226633Sdim *
39226633Sdim * The above copyright and patent license is granted only if the following
40226633Sdim * conditions are met:
41226633Sdim *
42226633Sdim * 3. Conditions
43226633Sdim *
44226633Sdim * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45226633Sdim * Redistribution of source code of any substantial portion of the Covered
46226633Sdim * Code or modification with rights to further distribute source must include
47226633Sdim * the above Copyright Notice, the above License, this list of Conditions,
48226633Sdim * and the following Disclaimer and Export Compliance provision.  In addition,
49226633Sdim * Licensee must cause all Covered Code to which Licensee contributes to
50226633Sdim * contain a file documenting the changes Licensee made to create that Covered
51261991Sdim * Code and the date of any change.  Licensee must include in that file the
52226633Sdim * documentation of any changes made by any predecessor Licensee.  Licensee
53226633Sdim * must include a prominent statement that the modification is derived,
54226633Sdim * directly or indirectly, from Original Intel Code.
55226633Sdim *
56226633Sdim * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57226633Sdim * Redistribution of source code of any substantial portion of the Covered
58226633Sdim * Code or modification without rights to further distribute source must
59226633Sdim * include the following Disclaimer and Export Compliance provision in the
60226633Sdim * documentation and/or other materials provided with distribution.  In
61226633Sdim * addition, Licensee may not authorize further sublicense of source of any
62226633Sdim * portion of the Covered Code, and must include terms to the effect that the
63226633Sdim * license from Licensee to its licensee is limited to the intellectual
64226633Sdim * property embodied in the software Licensee provides to its licensee, and
65226633Sdim * not to intellectual property embodied in modifications its licensee may
66226633Sdim * make.
67226633Sdim *
68239462Sdim * 3.3. Redistribution of Executable. Redistribution in executable form of any
69239462Sdim * substantial portion of the Covered Code or modification must reproduce the
70239462Sdim * above Copyright Notice, and the following Disclaimer and Export Compliance
71226633Sdim * provision in the documentation and/or other materials provided with the
72226633Sdim * distribution.
73226633Sdim *
74226633Sdim * 3.4. Intel retains all right, title, and interest in and to the Original
75226633Sdim * Intel Code.
76218885Sdim *
77226633Sdim * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78226633Sdim * Intel shall be used in advertising or otherwise to promote the sale, use or
79226633Sdim * other dealings in products derived from or relating to the Covered Code
80226633Sdim * without prior written authorization from Intel.
81226633Sdim *
82226633Sdim * 4. Disclaimer and Export Compliance
83226633Sdim *
84218885Sdim * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85218885Sdim * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86218885Sdim * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87280031Sdim * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88226633Sdim * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89280031Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90280031Sdim * PARTICULAR PURPOSE.
91218885Sdim *
92218885Sdim * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93218885Sdim * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94226633Sdim * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95226633Sdim * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96226633Sdim * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97226633Sdim * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98226633Sdim * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99218885Sdim * LIMITED REMEDY.
100218885Sdim *
101218885Sdim * 4.3. Licensee shall not export, either directly or indirectly, any of this
102218885Sdim * software or system incorporating such software without first obtaining any
103226633Sdim * required license or other approval from the U. S. Department of Commerce or
104226633Sdim * any other agency or department of the United States Government.  In the
105226633Sdim * event Licensee exports any such software from the United States or
106226633Sdim * re-exports any such software from a foreign destination, Licensee shall
107226633Sdim * ensure that the distribution and export/re-export of the software is in
108218885Sdim * compliance with all laws, regulations, orders, or other restrictions of the
109218885Sdim * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110218885Sdim * any of its subsidiaries will export/re-export any technical data, process,
111218885Sdim * software, or service, directly or indirectly, to any country for which the
112226633Sdim * United States government or any agency thereof requires an export license,
113226633Sdim * other governmental approval, or letter of assurance, without first obtaining
114226633Sdim * such license, approval or letter.
115226633Sdim *
116226633Sdim *****************************************************************************/
117218885Sdim
118218885Sdim#include "acpi.h"
119218885Sdim
120218885Sdim#define _COMPONENT          ACPI_HARDWARE
121226633Sdim        ACPI_MODULE_NAME    ("hwtimer")
122226633Sdim
123226633Sdim
124226633Sdim/******************************************************************************
125226633Sdim *
126218885Sdim * FUNCTION:    AcpiGetTimerResolution
127218885Sdim *
128218885Sdim * PARAMETERS:  none
129218885Sdim *
130218885Sdim * RETURN:      Number of bits of resolution in the PM Timer (24 or 32).
131 *
132 * DESCRIPTION: Obtains resolution of the ACPI PM Timer.
133 *
134 ******************************************************************************/
135
136ACPI_STATUS
137AcpiGetTimerResolution (
138    UINT32                  *Resolution)
139{
140    ACPI_FUNCTION_TRACE ("AcpiGetTimerResolution");
141
142
143    if (!Resolution)
144    {
145        return_ACPI_STATUS (AE_BAD_PARAMETER);
146    }
147
148    if (0 == AcpiGbl_FADT->TmrValExt)
149    {
150        *Resolution = 24;
151    }
152    else
153    {
154        *Resolution = 32;
155    }
156
157    return_ACPI_STATUS (AE_OK);
158}
159
160
161/******************************************************************************
162 *
163 * FUNCTION:    AcpiGetTimer
164 *
165 * PARAMETERS:  none
166 *
167 * RETURN:      Current value of the ACPI PM Timer (in ticks).
168 *
169 * DESCRIPTION: Obtains current value of ACPI PM Timer.
170 *
171 ******************************************************************************/
172
173ACPI_STATUS
174AcpiGetTimer (
175    UINT32                  *Ticks)
176{
177    ACPI_STATUS             Status;
178
179
180    ACPI_FUNCTION_TRACE ("AcpiGetTimer");
181
182
183    if (!Ticks)
184    {
185        return_ACPI_STATUS (AE_BAD_PARAMETER);
186    }
187
188    Status = AcpiHwLowLevelRead (32, Ticks, &AcpiGbl_FADT->XPmTmrBlk, 0);
189
190    return_ACPI_STATUS (Status);
191}
192
193
194/******************************************************************************
195 *
196 * FUNCTION:    AcpiGetTimerDuration
197 *
198 * PARAMETERS:  StartTicks
199 *              EndTicks
200 *              TimeElapsed
201 *
202 * RETURN:      TimeElapsed
203 *
204 * DESCRIPTION: Computes the time elapsed (in microseconds) between two
205 *              PM Timer time stamps, taking into account the possibility of
206 *              rollovers, the timer resolution, and timer frequency.
207 *
208 *              The PM Timer's clock ticks at roughly 3.6 times per
209 *              _microsecond_, and its clock continues through Cx state
210 *              transitions (unlike many CPU timestamp counters) -- making it
211 *              a versatile and accurate timer.
212 *
213 *              Note that this function accomodates only a single timer
214 *              rollover.  Thus for 24-bit timers, this function should only
215 *              be used for calculating durations less than ~4.6 seconds
216 *              (~20 hours for 32-bit timers).
217 *
218 ******************************************************************************/
219
220ACPI_STATUS
221AcpiGetTimerDuration (
222    UINT32                  StartTicks,
223    UINT32                  EndTicks,
224    UINT32                  *TimeElapsed)
225{
226    UINT32                  DeltaTicks = 0;
227    UINT64_OVERLAY          NormalizedTicks;
228    ACPI_STATUS             Status;
229    ACPI_INTEGER            OutQuotient;
230
231
232    ACPI_FUNCTION_TRACE ("AcpiGetTimerDuration");
233
234
235    if (!TimeElapsed)
236    {
237        return_ACPI_STATUS (AE_BAD_PARAMETER);
238    }
239
240    /*
241     * Compute Tick Delta:
242     * -------------------
243     * Handle (max one) timer rollovers on 24- versus 32-bit timers.
244     */
245    if (StartTicks < EndTicks)
246    {
247        DeltaTicks = EndTicks - StartTicks;
248    }
249    else if (StartTicks > EndTicks)
250    {
251        if (0 == AcpiGbl_FADT->TmrValExt)
252        {
253            /* 24-bit Timer */
254
255            DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF);
256        }
257        else
258        {
259            /* 32-bit Timer */
260
261            DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks;
262        }
263    }
264    else
265    {
266        *TimeElapsed = 0;
267        return_ACPI_STATUS (AE_OK);
268    }
269
270    /*
271     * Compute Duration:
272     * -----------------
273     *
274     * Requires a 64-bit divide:
275     *
276     * TimeElapsed = (DeltaTicks * 1000000) / PM_TIMER_FREQUENCY;
277     */
278    NormalizedTicks.Full = ((UINT64) DeltaTicks) * 1000000;
279
280    Status = AcpiUtShortDivide (&NormalizedTicks.Full, PM_TIMER_FREQUENCY,
281                                    &OutQuotient, NULL);
282
283    *TimeElapsed = (UINT32) OutQuotient;
284    return_ACPI_STATUS (Status);
285}
286
287
288