hwtimer.c revision 1.1.1.2.2.2 1 1.1.1.2.2.2 bouyer
2 1.1.1.2.2.2 bouyer /******************************************************************************
3 1.1.1.2.2.2 bouyer *
4 1.1.1.2.2.2 bouyer * Name: hwtimer.c - ACPI Power Management Timer Interface
5 1.1.1.2.2.2 bouyer *
6 1.1.1.2.2.2 bouyer *****************************************************************************/
7 1.1.1.2.2.2 bouyer
8 1.1.1.2.2.2 bouyer /*
9 1.1.1.2.2.2 bouyer * Copyright (C) 2000 - 2011, Intel Corp.
10 1.1.1.2.2.2 bouyer * All rights reserved.
11 1.1.1.2.2.2 bouyer *
12 1.1.1.2.2.2 bouyer * Redistribution and use in source and binary forms, with or without
13 1.1.1.2.2.2 bouyer * modification, are permitted provided that the following conditions
14 1.1.1.2.2.2 bouyer * are met:
15 1.1.1.2.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
16 1.1.1.2.2.2 bouyer * notice, this list of conditions, and the following disclaimer,
17 1.1.1.2.2.2 bouyer * without modification.
18 1.1.1.2.2.2 bouyer * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 1.1.1.2.2.2 bouyer * substantially similar to the "NO WARRANTY" disclaimer below
20 1.1.1.2.2.2 bouyer * ("Disclaimer") and any redistribution must be conditioned upon
21 1.1.1.2.2.2 bouyer * including a substantially similar Disclaimer requirement for further
22 1.1.1.2.2.2 bouyer * binary redistribution.
23 1.1.1.2.2.2 bouyer * 3. Neither the names of the above-listed copyright holders nor the names
24 1.1.1.2.2.2 bouyer * of any contributors may be used to endorse or promote products derived
25 1.1.1.2.2.2 bouyer * from this software without specific prior written permission.
26 1.1.1.2.2.2 bouyer *
27 1.1.1.2.2.2 bouyer * Alternatively, this software may be distributed under the terms of the
28 1.1.1.2.2.2 bouyer * GNU General Public License ("GPL") version 2 as published by the Free
29 1.1.1.2.2.2 bouyer * Software Foundation.
30 1.1.1.2.2.2 bouyer *
31 1.1.1.2.2.2 bouyer * NO WARRANTY
32 1.1.1.2.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 1.1.1.2.2.2 bouyer * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 1.1.1.2.2.2 bouyer * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 1.1.1.2.2.2 bouyer * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 1.1.1.2.2.2 bouyer * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1.1.2.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1.1.2.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1.1.2.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 1.1.1.2.2.2 bouyer * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 1.1.1.2.2.2 bouyer * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 1.1.1.2.2.2 bouyer * POSSIBILITY OF SUCH DAMAGES.
43 1.1.1.2.2.2 bouyer */
44 1.1.1.2.2.2 bouyer
45 1.1.1.2.2.2 bouyer #include "acpi.h"
46 1.1.1.2.2.2 bouyer #include "accommon.h"
47 1.1.1.2.2.2 bouyer
48 1.1.1.2.2.2 bouyer #define _COMPONENT ACPI_HARDWARE
49 1.1.1.2.2.2 bouyer ACPI_MODULE_NAME ("hwtimer")
50 1.1.1.2.2.2 bouyer
51 1.1.1.2.2.2 bouyer
52 1.1.1.2.2.2 bouyer /******************************************************************************
53 1.1.1.2.2.2 bouyer *
54 1.1.1.2.2.2 bouyer * FUNCTION: AcpiGetTimerResolution
55 1.1.1.2.2.2 bouyer *
56 1.1.1.2.2.2 bouyer * PARAMETERS: Resolution - Where the resolution is returned
57 1.1.1.2.2.2 bouyer *
58 1.1.1.2.2.2 bouyer * RETURN: Status and timer resolution
59 1.1.1.2.2.2 bouyer *
60 1.1.1.2.2.2 bouyer * DESCRIPTION: Obtains resolution of the ACPI PM Timer (24 or 32 bits).
61 1.1.1.2.2.2 bouyer *
62 1.1.1.2.2.2 bouyer ******************************************************************************/
63 1.1.1.2.2.2 bouyer
64 1.1.1.2.2.2 bouyer ACPI_STATUS
65 1.1.1.2.2.2 bouyer AcpiGetTimerResolution (
66 1.1.1.2.2.2 bouyer UINT32 *Resolution)
67 1.1.1.2.2.2 bouyer {
68 1.1.1.2.2.2 bouyer ACPI_FUNCTION_TRACE (AcpiGetTimerResolution);
69 1.1.1.2.2.2 bouyer
70 1.1.1.2.2.2 bouyer
71 1.1.1.2.2.2 bouyer if (!Resolution)
72 1.1.1.2.2.2 bouyer {
73 1.1.1.2.2.2 bouyer return_ACPI_STATUS (AE_BAD_PARAMETER);
74 1.1.1.2.2.2 bouyer }
75 1.1.1.2.2.2 bouyer
76 1.1.1.2.2.2 bouyer if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
77 1.1.1.2.2.2 bouyer {
78 1.1.1.2.2.2 bouyer *Resolution = 24;
79 1.1.1.2.2.2 bouyer }
80 1.1.1.2.2.2 bouyer else
81 1.1.1.2.2.2 bouyer {
82 1.1.1.2.2.2 bouyer *Resolution = 32;
83 1.1.1.2.2.2 bouyer }
84 1.1.1.2.2.2 bouyer
85 1.1.1.2.2.2 bouyer return_ACPI_STATUS (AE_OK);
86 1.1.1.2.2.2 bouyer }
87 1.1.1.2.2.2 bouyer
88 1.1.1.2.2.2 bouyer ACPI_EXPORT_SYMBOL (AcpiGetTimerResolution)
89 1.1.1.2.2.2 bouyer
90 1.1.1.2.2.2 bouyer
91 1.1.1.2.2.2 bouyer /******************************************************************************
92 1.1.1.2.2.2 bouyer *
93 1.1.1.2.2.2 bouyer * FUNCTION: AcpiGetTimer
94 1.1.1.2.2.2 bouyer *
95 1.1.1.2.2.2 bouyer * PARAMETERS: Ticks - Where the timer value is returned
96 1.1.1.2.2.2 bouyer *
97 1.1.1.2.2.2 bouyer * RETURN: Status and current timer value (ticks)
98 1.1.1.2.2.2 bouyer *
99 1.1.1.2.2.2 bouyer * DESCRIPTION: Obtains current value of ACPI PM Timer (in ticks).
100 1.1.1.2.2.2 bouyer *
101 1.1.1.2.2.2 bouyer ******************************************************************************/
102 1.1.1.2.2.2 bouyer
103 1.1.1.2.2.2 bouyer ACPI_STATUS
104 1.1.1.2.2.2 bouyer AcpiGetTimer (
105 1.1.1.2.2.2 bouyer UINT32 *Ticks)
106 1.1.1.2.2.2 bouyer {
107 1.1.1.2.2.2 bouyer ACPI_STATUS Status;
108 1.1.1.2.2.2 bouyer
109 1.1.1.2.2.2 bouyer
110 1.1.1.2.2.2 bouyer ACPI_FUNCTION_TRACE (AcpiGetTimer);
111 1.1.1.2.2.2 bouyer
112 1.1.1.2.2.2 bouyer
113 1.1.1.2.2.2 bouyer if (!Ticks)
114 1.1.1.2.2.2 bouyer {
115 1.1.1.2.2.2 bouyer return_ACPI_STATUS (AE_BAD_PARAMETER);
116 1.1.1.2.2.2 bouyer }
117 1.1.1.2.2.2 bouyer
118 1.1.1.2.2.2 bouyer Status = AcpiHwRead (Ticks, &AcpiGbl_FADT.XPmTimerBlock);
119 1.1.1.2.2.2 bouyer
120 1.1.1.2.2.2 bouyer return_ACPI_STATUS (Status);
121 1.1.1.2.2.2 bouyer }
122 1.1.1.2.2.2 bouyer
123 1.1.1.2.2.2 bouyer ACPI_EXPORT_SYMBOL (AcpiGetTimer)
124 1.1.1.2.2.2 bouyer
125 1.1.1.2.2.2 bouyer
126 1.1.1.2.2.2 bouyer /******************************************************************************
127 1.1.1.2.2.2 bouyer *
128 1.1.1.2.2.2 bouyer * FUNCTION: AcpiGetTimerDuration
129 1.1.1.2.2.2 bouyer *
130 1.1.1.2.2.2 bouyer * PARAMETERS: StartTicks - Starting timestamp
131 1.1.1.2.2.2 bouyer * EndTicks - End timestamp
132 1.1.1.2.2.2 bouyer * TimeElapsed - Where the elapsed time is returned
133 1.1.1.2.2.2 bouyer *
134 1.1.1.2.2.2 bouyer * RETURN: Status and TimeElapsed
135 1.1.1.2.2.2 bouyer *
136 1.1.1.2.2.2 bouyer * DESCRIPTION: Computes the time elapsed (in microseconds) between two
137 1.1.1.2.2.2 bouyer * PM Timer time stamps, taking into account the possibility of
138 1.1.1.2.2.2 bouyer * rollovers, the timer resolution, and timer frequency.
139 1.1.1.2.2.2 bouyer *
140 1.1.1.2.2.2 bouyer * The PM Timer's clock ticks at roughly 3.6 times per
141 1.1.1.2.2.2 bouyer * _microsecond_, and its clock continues through Cx state
142 1.1.1.2.2.2 bouyer * transitions (unlike many CPU timestamp counters) -- making it
143 1.1.1.2.2.2 bouyer * a versatile and accurate timer.
144 1.1.1.2.2.2 bouyer *
145 1.1.1.2.2.2 bouyer * Note that this function accommodates only a single timer
146 1.1.1.2.2.2 bouyer * rollover. Thus for 24-bit timers, this function should only
147 1.1.1.2.2.2 bouyer * be used for calculating durations less than ~4.6 seconds
148 1.1.1.2.2.2 bouyer * (~20 minutes for 32-bit timers) -- calculations below:
149 1.1.1.2.2.2 bouyer *
150 1.1.1.2.2.2 bouyer * 2**24 Ticks / 3,600,000 Ticks/Sec = 4.66 sec
151 1.1.1.2.2.2 bouyer * 2**32 Ticks / 3,600,000 Ticks/Sec = 1193 sec or 19.88 minutes
152 1.1.1.2.2.2 bouyer *
153 1.1.1.2.2.2 bouyer ******************************************************************************/
154 1.1.1.2.2.2 bouyer
155 1.1.1.2.2.2 bouyer ACPI_STATUS
156 1.1.1.2.2.2 bouyer AcpiGetTimerDuration (
157 1.1.1.2.2.2 bouyer UINT32 StartTicks,
158 1.1.1.2.2.2 bouyer UINT32 EndTicks,
159 1.1.1.2.2.2 bouyer UINT32 *TimeElapsed)
160 1.1.1.2.2.2 bouyer {
161 1.1.1.2.2.2 bouyer ACPI_STATUS Status;
162 1.1.1.2.2.2 bouyer UINT32 DeltaTicks;
163 1.1.1.2.2.2 bouyer UINT64 Quotient;
164 1.1.1.2.2.2 bouyer
165 1.1.1.2.2.2 bouyer
166 1.1.1.2.2.2 bouyer ACPI_FUNCTION_TRACE (AcpiGetTimerDuration);
167 1.1.1.2.2.2 bouyer
168 1.1.1.2.2.2 bouyer
169 1.1.1.2.2.2 bouyer if (!TimeElapsed)
170 1.1.1.2.2.2 bouyer {
171 1.1.1.2.2.2 bouyer return_ACPI_STATUS (AE_BAD_PARAMETER);
172 1.1.1.2.2.2 bouyer }
173 1.1.1.2.2.2 bouyer
174 1.1.1.2.2.2 bouyer /*
175 1.1.1.2.2.2 bouyer * Compute Tick Delta:
176 1.1.1.2.2.2 bouyer * Handle (max one) timer rollovers on 24-bit versus 32-bit timers.
177 1.1.1.2.2.2 bouyer */
178 1.1.1.2.2.2 bouyer if (StartTicks < EndTicks)
179 1.1.1.2.2.2 bouyer {
180 1.1.1.2.2.2 bouyer DeltaTicks = EndTicks - StartTicks;
181 1.1.1.2.2.2 bouyer }
182 1.1.1.2.2.2 bouyer else if (StartTicks > EndTicks)
183 1.1.1.2.2.2 bouyer {
184 1.1.1.2.2.2 bouyer if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
185 1.1.1.2.2.2 bouyer {
186 1.1.1.2.2.2 bouyer /* 24-bit Timer */
187 1.1.1.2.2.2 bouyer
188 1.1.1.2.2.2 bouyer DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF);
189 1.1.1.2.2.2 bouyer }
190 1.1.1.2.2.2 bouyer else
191 1.1.1.2.2.2 bouyer {
192 1.1.1.2.2.2 bouyer /* 32-bit Timer */
193 1.1.1.2.2.2 bouyer
194 1.1.1.2.2.2 bouyer DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks;
195 1.1.1.2.2.2 bouyer }
196 1.1.1.2.2.2 bouyer }
197 1.1.1.2.2.2 bouyer else /* StartTicks == EndTicks */
198 1.1.1.2.2.2 bouyer {
199 1.1.1.2.2.2 bouyer *TimeElapsed = 0;
200 1.1.1.2.2.2 bouyer return_ACPI_STATUS (AE_OK);
201 1.1.1.2.2.2 bouyer }
202 1.1.1.2.2.2 bouyer
203 1.1.1.2.2.2 bouyer /*
204 1.1.1.2.2.2 bouyer * Compute Duration (Requires a 64-bit multiply and divide):
205 1.1.1.2.2.2 bouyer *
206 1.1.1.2.2.2 bouyer * TimeElapsed = (DeltaTicks * 1000000) / PM_TIMER_FREQUENCY;
207 1.1.1.2.2.2 bouyer */
208 1.1.1.2.2.2 bouyer Status = AcpiUtShortDivide (((UINT64) DeltaTicks) * 1000000,
209 1.1.1.2.2.2 bouyer PM_TIMER_FREQUENCY, &Quotient, NULL);
210 1.1.1.2.2.2 bouyer
211 1.1.1.2.2.2 bouyer *TimeElapsed = (UINT32) Quotient;
212 1.1.1.2.2.2 bouyer return_ACPI_STATUS (Status);
213 1.1.1.2.2.2 bouyer }
214 1.1.1.2.2.2 bouyer
215 1.1.1.2.2.2 bouyer ACPI_EXPORT_SYMBOL (AcpiGetTimerDuration)
216 1.1.1.2.2.2 bouyer
217