hwtimer.c revision 99146
1311116Sdim 2311116Sdim/****************************************************************************** 3353358Sdim * 4353358Sdim * Name: hwtimer.c - ACPI Power Management Timer Interface 5353358Sdim * $Revision: 20 $ 6311116Sdim * 7311116Sdim *****************************************************************************/ 8311116Sdim 9311116Sdim/****************************************************************************** 10311116Sdim * 11311116Sdim * 1. Copyright Notice 12321369Sdim * 13321369Sdim * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp. 14311116Sdim * All rights reserved. 15311116Sdim * 16311116Sdim * 2. License 17311116Sdim * 18311116Sdim * 2.1. This is your license from Intel Corp. under its intellectual property 19311116Sdim * rights. You may have additional license terms from the party that provided 20311116Sdim * you this software, covering your right to use that party's intellectual 21311116Sdim * property rights. 22311116Sdim * 23311116Sdim * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 24311116Sdim * copy of the source code appearing in this file ("Covered Code") an 25311116Sdim * irrevocable, perpetual, worldwide license under Intel's copyrights in the 26311116Sdim * base code distributed originally by Intel ("Original Intel Code") to copy, 27311116Sdim * make derivatives, distribute, use and display any portion of the Covered 28311116Sdim * Code in any form, with the right to sublicense such rights; and 29321369Sdim * 30321369Sdim * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 31321369Sdim * license (with the right to sublicense), under only those claims of Intel 32321369Sdim * patents that are infringed by the Original Intel Code, to make, use, sell, 33321369Sdim * offer to sell, and import the Covered Code and derivative works thereof 34321369Sdim * solely to the minimum extent necessary to exercise the above copyright 35321369Sdim * license, and in no event shall the patent license extend to any additions 36321369Sdim * to or modifications of the Original Intel Code. No other license or right 37353358Sdim * is granted directly or by implication, estoppel or otherwise; 38353358Sdim * 39353358Sdim * The above copyright and patent license is granted only if the following 40353358Sdim * conditions are met: 41353358Sdim * 42353358Sdim * 3. Conditions 43353358Sdim * 44353358Sdim * 3.1. Redistribution of Source with Rights to Further Distribute Source. 45353358Sdim * Redistribution of source code of any substantial portion of the Covered 46353358Sdim * Code or modification with rights to further distribute source must include 47353358Sdim * the above Copyright Notice, the above License, this list of Conditions, 48353358Sdim * and the following Disclaimer and Export Compliance provision. In addition, 49353358Sdim * Licensee must cause all Covered Code to which Licensee contributes to 50353358Sdim * contain a file documenting the changes Licensee made to create that Covered 51353358Sdim * Code and the date of any change. Licensee must include in that file the 52360784Sdim * documentation of any changes made by any predecessor Licensee. Licensee 53353358Sdim * must include a prominent statement that the modification is derived, 54311116Sdim * directly or indirectly, from Original Intel Code. 55311116Sdim * 56311116Sdim * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 57311116Sdim * Redistribution of source code of any substantial portion of the Covered 58353358Sdim * Code or modification without rights to further distribute source must 59353358Sdim * include the following Disclaimer and Export Compliance provision in the 60353358Sdim * documentation and/or other materials provided with distribution. In 61311116Sdim * addition, Licensee may not authorize further sublicense of source of any 62311116Sdim * portion of the Covered Code, and must include terms to the effect that the 63311116Sdim * license from Licensee to its licensee is limited to the intellectual 64311116Sdim * property embodied in the software Licensee provides to its licensee, and 65311116Sdim * not to intellectual property embodied in modifications its licensee may 66311116Sdim * make. 67311116Sdim * 68311116Sdim * 3.3. Redistribution of Executable. Redistribution in executable form of any 69311116Sdim * substantial portion of the Covered Code or modification must reproduce the 70311116Sdim * above Copyright Notice, and the following Disclaimer and Export Compliance 71311116Sdim * provision in the documentation and/or other materials provided with the 72311116Sdim * distribution. 73311116Sdim * 74311116Sdim * 3.4. Intel retains all right, title, and interest in and to the Original 75311116Sdim * Intel Code. 76311116Sdim * 77311116Sdim * 3.5. Neither the name Intel nor any other trademark owned or controlled by 78311116Sdim * Intel shall be used in advertising or otherwise to promote the sale, use or 79321369Sdim * other dealings in products derived from or relating to the Covered Code 80321369Sdim * without prior written authorization from Intel. 81321369Sdim * 82321369Sdim * 4. Disclaimer and Export Compliance 83321369Sdim * 84321369Sdim * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 85311116Sdim * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 86311116Sdim * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 87311116Sdim * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 88311116Sdim * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 89311116Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 90311116Sdim * PARTICULAR PURPOSE. 91311116Sdim * 92311116Sdim * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 93311116Sdim * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 94311116Sdim * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 95311116Sdim * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 96311116Sdim * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 97311116Sdim * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 98311116Sdim * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 99311116Sdim * LIMITED REMEDY. 100353358Sdim * 101353358Sdim * 4.3. Licensee shall not export, either directly or indirectly, any of this 102353358Sdim * software or system incorporating such software without first obtaining any 103353358Sdim * required license or other approval from the U. S. Department of Commerce or 104353358Sdim * any other agency or department of the United States Government. In the 105353358Sdim * event Licensee exports any such software from the United States or 106353358Sdim * re-exports any such software from a foreign destination, Licensee shall 107311116Sdim * ensure that the distribution and export/re-export of the software is in 108311116Sdim * compliance with all laws, regulations, orders, or other restrictions of the 109311116Sdim * U.S. Export Administration Regulations. Licensee agrees that neither it nor 110311116Sdim * any of its subsidiaries will export/re-export any technical data, process, 111311116Sdim * software, or service, directly or indirectly, to any country for which the 112311116Sdim * United States government or any agency thereof requires an export license, 113311116Sdim * other governmental approval, or letter of assurance, without first obtaining 114311116Sdim * such license, approval or letter. 115311116Sdim * 116353358Sdim *****************************************************************************/ 117353358Sdim 118311116Sdim#include "acpi.h" 119353358Sdim#include "achware.h" 120311116Sdim 121311116Sdim#define _COMPONENT ACPI_HARDWARE 122311116Sdim ACPI_MODULE_NAME ("hwtimer") 123311116Sdim 124311116Sdim 125311116Sdim/****************************************************************************** 126311116Sdim * 127353358Sdim * FUNCTION: AcpiGetTimerResolution 128353358Sdim * 129360784Sdim * PARAMETERS: none 130360784Sdim * 131360784Sdim * RETURN: Number of bits of resolution in the PM Timer (24 or 32). 132360784Sdim * 133360784Sdim * DESCRIPTION: Obtains resolution of the ACPI PM Timer. 134353358Sdim * 135353358Sdim ******************************************************************************/ 136353358Sdim 137311116SdimACPI_STATUS 138311116SdimAcpiGetTimerResolution ( 139353358Sdim UINT32 *Resolution) 140353358Sdim{ 141353358Sdim ACPI_FUNCTION_TRACE ("AcpiGetTimerResolution"); 142353358Sdim 143353358Sdim 144311116Sdim if (!Resolution) 145311116Sdim { 146311116Sdim return_ACPI_STATUS (AE_BAD_PARAMETER); 147311116Sdim } 148353358Sdim 149353358Sdim if (0 == AcpiGbl_FADT->TmrValExt) 150353358Sdim { 151353358Sdim *Resolution = 24; 152353358Sdim } 153353358Sdim else 154353358Sdim { 155353358Sdim *Resolution = 32; 156311116Sdim } 157311116Sdim 158311116Sdim return_ACPI_STATUS (AE_OK); 159311116Sdim} 160311116Sdim 161311116Sdim 162311116Sdim/****************************************************************************** 163311116Sdim * 164311116Sdim * FUNCTION: AcpiGetTimer 165311116Sdim * 166311116Sdim * PARAMETERS: none 167311116Sdim * 168311116Sdim * RETURN: Current value of the ACPI PM Timer (in ticks). 169311116Sdim * 170311116Sdim * DESCRIPTION: Obtains current value of ACPI PM Timer. 171311116Sdim * 172311116Sdim ******************************************************************************/ 173353358Sdim 174353358SdimACPI_STATUS 175353358SdimAcpiGetTimer ( 176353358Sdim UINT32 *Ticks) 177353358Sdim{ 178311116Sdim ACPI_FUNCTION_TRACE ("AcpiGetTimer"); 179311116Sdim 180311116Sdim 181311116Sdim if (!Ticks) 182311116Sdim { 183311116Sdim return_ACPI_STATUS (AE_BAD_PARAMETER); 184311116Sdim } 185311116Sdim 186311116Sdim *Ticks = AcpiHwLowLevelRead (32, &AcpiGbl_FADT->XPmTmrBlk, 0); 187311116Sdim 188311116Sdim return_ACPI_STATUS (AE_OK); 189353358Sdim} 190353358Sdim 191311116Sdim 192353358Sdim/****************************************************************************** 193353358Sdim * 194353358Sdim * FUNCTION: AcpiGetTimerDuration 195353358Sdim * 196353358Sdim * PARAMETERS: StartTicks 197311116Sdim * EndTicks 198311116Sdim * TimeElapsed 199353358Sdim * 200353358Sdim * RETURN: TimeElapsed 201353358Sdim * 202311116Sdim * DESCRIPTION: Computes the time elapsed (in microseconds) between two 203311116Sdim * PM Timer time stamps, taking into account the possibility of 204353358Sdim * rollovers, the timer resolution, and timer frequency. 205353358Sdim * 206353358Sdim * The PM Timer's clock ticks at roughly 3.6 times per 207353358Sdim * _microsecond_, and its clock continues through Cx state 208353358Sdim * transitions (unlike many CPU timestamp counters) -- making it 209353358Sdim * a versatile and accurate timer. 210353358Sdim * 211311116Sdim * Note that this function accomodates only a single timer 212311116Sdim * rollover. Thus for 24-bit timers, this function should only 213321369Sdim * be used for calculating durations less than ~4.6 seconds 214311116Sdim * (~20 hours for 32-bit timers). 215311116Sdim * 216321369Sdim ******************************************************************************/ 217311116Sdim 218311116SdimACPI_STATUS 219311116SdimAcpiGetTimerDuration ( 220311116Sdim UINT32 StartTicks, 221311116Sdim UINT32 EndTicks, 222353358Sdim UINT32 *TimeElapsed) 223311116Sdim{ 224353358Sdim UINT32 DeltaTicks = 0; 225353358Sdim UINT64_OVERLAY NormalizedTicks; 226353358Sdim ACPI_STATUS Status; 227353358Sdim ACPI_INTEGER OutQuotient; 228353358Sdim 229353358Sdim 230353358Sdim ACPI_FUNCTION_TRACE ("AcpiGetTimerDuration"); 231353358Sdim 232353358Sdim 233353358Sdim if (!TimeElapsed) 234311116Sdim { 235311116Sdim return_ACPI_STATUS (AE_BAD_PARAMETER); 236311116Sdim } 237311116Sdim 238321369Sdim /* 239311116Sdim * Compute Tick Delta: 240311116Sdim * ------------------- 241321369Sdim * Handle (max one) timer rollovers on 24- versus 32-bit timers. 242321369Sdim */ 243311116Sdim if (StartTicks < EndTicks) 244321369Sdim { 245311116Sdim DeltaTicks = EndTicks - StartTicks; 246311116Sdim } 247311116Sdim else if (StartTicks > EndTicks) 248311116Sdim { 249353358Sdim if (0 == AcpiGbl_FADT->TmrValExt) 250353358Sdim { 251353358Sdim /* 24-bit Timer */ 252353358Sdim 253353358Sdim DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF); 254311116Sdim } 255311116Sdim else 256311116Sdim { 257311116Sdim /* 32-bit Timer */ 258353358Sdim 259353358Sdim DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks; 260311116Sdim } 261311116Sdim } 262311116Sdim else 263311116Sdim { 264311116Sdim *TimeElapsed = 0; 265311116Sdim return_ACPI_STATUS (AE_OK); 266311116Sdim } 267311116Sdim 268311116Sdim /* 269311116Sdim * Compute Duration: 270311116Sdim * ----------------- 271311116Sdim * 272311116Sdim * Requires a 64-bit divide: 273311116Sdim * 274353358Sdim * TimeElapsed = (DeltaTicks * 1000000) / PM_TIMER_FREQUENCY; 275353358Sdim */ 276353358Sdim NormalizedTicks.Full = ((UINT64) DeltaTicks) * 1000000; 277353358Sdim 278353358Sdim Status = AcpiUtShortDivide (&NormalizedTicks.Full, PM_TIMER_FREQUENCY, 279353358Sdim &OutQuotient, NULL); 280353358Sdim 281353358Sdim *TimeElapsed = (UINT32) OutQuotient; 282353358Sdim return_ACPI_STATUS (Status); 283353358Sdim} 284353358Sdim 285353358Sdim 286353358Sdim