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