hwtimer.c revision 91116
11590Srgrimes 21590Srgrimes/****************************************************************************** 31590Srgrimes * 41590Srgrimes * Name: hwtimer.c - ACPI Power Management Timer Interface 51590Srgrimes * $Revision: 19 $ 61590Srgrimes * 71590Srgrimes *****************************************************************************/ 81590Srgrimes 91590Srgrimes/****************************************************************************** 101590Srgrimes * 111590Srgrimes * 1. Copyright Notice 121590Srgrimes * 131590Srgrimes * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp. 141590Srgrimes * All rights reserved. 151590Srgrimes * 161590Srgrimes * 2. License 171590Srgrimes * 181590Srgrimes * 2.1. This is your license from Intel Corp. under its intellectual property 191590Srgrimes * rights. You may have additional license terms from the party that provided 201590Srgrimes * you this software, covering your right to use that party's intellectual 211590Srgrimes * property rights. 221590Srgrimes * 231590Srgrimes * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 241590Srgrimes * copy of the source code appearing in this file ("Covered Code") an 251590Srgrimes * irrevocable, perpetual, worldwide license under Intel's copyrights in the 261590Srgrimes * base code distributed originally by Intel ("Original Intel Code") to copy, 271590Srgrimes * make derivatives, distribute, use and display any portion of the Covered 281590Srgrimes * Code in any form, with the right to sublicense such rights; and 291590Srgrimes * 301590Srgrimes * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 311590Srgrimes * license (with the right to sublicense), under only those claims of Intel 321590Srgrimes * patents that are infringed by the Original Intel Code, to make, use, sell, 331590Srgrimes * offer to sell, and import the Covered Code and derivative works thereof 341590Srgrimes * solely to the minimum extent necessary to exercise the above copyright 351590Srgrimes * license, and in no event shall the patent license extend to any additions 361590Srgrimes * to or modifications of the Original Intel Code. No other license or right 371590Srgrimes * is granted directly or by implication, estoppel or otherwise; 381590Srgrimes * 391590Srgrimes * The above copyright and patent license is granted only if the following 401590Srgrimes * conditions are met: 411590Srgrimes * 421590Srgrimes * 3. Conditions 431590Srgrimes * 441590Srgrimes * 3.1. Redistribution of Source with Rights to Further Distribute Source. 451590Srgrimes * Redistribution of source code of any substantial portion of the Covered 461590Srgrimes * Code or modification with rights to further distribute source must include 471590Srgrimes * the above Copyright Notice, the above License, this list of Conditions, 481590Srgrimes * and the following Disclaimer and Export Compliance provision. In addition, 491590Srgrimes * Licensee must cause all Covered Code to which Licensee contributes to 501590Srgrimes * contain a file documenting the changes Licensee made to create that Covered 511590Srgrimes * Code and the date of any change. Licensee must include in that file the 521590Srgrimes * documentation of any changes made by any predecessor Licensee. Licensee 531590Srgrimes * must include a prominent statement that the modification is derived, 5419190Salex * directly or indirectly, from Original Intel Code. 551590Srgrimes * 561590Srgrimes * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 571590Srgrimes * Redistribution of source code of any substantial portion of the Covered 581590Srgrimes * Code or modification without rights to further distribute source must 591590Srgrimes * include the following Disclaimer and Export Compliance provision in the 601590Srgrimes * documentation and/or other materials provided with distribution. In 611590Srgrimes * addition, Licensee may not authorize further sublicense of source of any 621590Srgrimes * portion of the Covered Code, and must include terms to the effect that the 631590Srgrimes * license from Licensee to its licensee is limited to the intellectual 641590Srgrimes * property embodied in the software Licensee provides to its licensee, and 651590Srgrimes * not to intellectual property embodied in modifications its licensee may 661590Srgrimes * make. 671590Srgrimes * 681590Srgrimes * 3.3. Redistribution of Executable. Redistribution in executable form of any 691590Srgrimes * substantial portion of the Covered Code or modification must reproduce the 701590Srgrimes * above Copyright Notice, and the following Disclaimer and Export Compliance 711590Srgrimes * provision in the documentation and/or other materials provided with the 721590Srgrimes * distribution. 731590Srgrimes * 741590Srgrimes * 3.4. Intel retains all right, title, and interest in and to the Original 751590Srgrimes * Intel Code. 761590Srgrimes * 771590Srgrimes * 3.5. Neither the name Intel nor any other trademark owned or controlled by 781590Srgrimes * Intel shall be used in advertising or otherwise to promote the sale, use or 791590Srgrimes * other dealings in products derived from or relating to the Covered Code 801590Srgrimes * without prior written authorization from Intel. 811590Srgrimes * 821590Srgrimes * 4. Disclaimer and Export Compliance 831590Srgrimes * 841590Srgrimes * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 851590Srgrimes * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 861590Srgrimes * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 871590Srgrimes * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 881590Srgrimes * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 891590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 901590Srgrimes * PARTICULAR PURPOSE. 911590Srgrimes * 921590Srgrimes * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 931590Srgrimes * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 941590Srgrimes * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 951590Srgrimes * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 961590Srgrimes * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 971590Srgrimes * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 981590Srgrimes * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 991590Srgrimes * LIMITED REMEDY. 1001590Srgrimes * 1011590Srgrimes * 4.3. Licensee shall not export, either directly or indirectly, any of this 1021590Srgrimes * software or system incorporating such software without first obtaining any 1031590Srgrimes * required license or other approval from the U. S. Department of Commerce or 1041590Srgrimes * any other agency or department of the United States Government. In the 1051590Srgrimes * event Licensee exports any such software from the United States or 10619190Salex * re-exports any such software from a foreign destination, Licensee shall 10719193Salex * ensure that the distribution and export/re-export of the software is in 1081590Srgrimes * compliance with all laws, regulations, orders, or other restrictions of the 1091590Srgrimes * U.S. Export Administration Regulations. Licensee agrees that neither it nor 1101590Srgrimes * any of its subsidiaries will export/re-export any technical data, process, 1111590Srgrimes * software, or service, directly or indirectly, to any country for which the 1121590Srgrimes * United States government or any agency thereof requires an export license, 1131590Srgrimes * other governmental approval, or letter of assurance, without first obtaining 1141590Srgrimes * such license, approval or letter. 1151590Srgrimes * 1161590Srgrimes *****************************************************************************/ 1171590Srgrimes 1181590Srgrimes#include "acpi.h" 1191590Srgrimes#include "achware.h" 1201590Srgrimes 1211590Srgrimes#define _COMPONENT ACPI_HARDWARE 1221590Srgrimes ACPI_MODULE_NAME ("hwtimer") 1231590Srgrimes 1241590Srgrimes 1251590Srgrimes/****************************************************************************** 1261590Srgrimes * 1271590Srgrimes * FUNCTION: AcpiGetTimerResolution 1281590Srgrimes * 1291590Srgrimes * PARAMETERS: none 1301590Srgrimes * 1311590Srgrimes * RETURN: Number of bits of resolution in the PM Timer (24 or 32). 1321590Srgrimes * 1331590Srgrimes * DESCRIPTION: Obtains resolution of the ACPI PM Timer. 1341590Srgrimes * 1351590Srgrimes ******************************************************************************/ 1361590Srgrimes 1371590SrgrimesACPI_STATUS 1381590SrgrimesAcpiGetTimerResolution ( 1391590Srgrimes UINT32 *Resolution) 1401590Srgrimes{ 1411590Srgrimes ACPI_FUNCTION_TRACE ("AcpiGetTimerResolution"); 1421590Srgrimes 1431590Srgrimes 1441590Srgrimes if (!Resolution) 1451590Srgrimes { 1461590Srgrimes return_ACPI_STATUS (AE_BAD_PARAMETER); 1471590Srgrimes } 1481590Srgrimes 1491590Srgrimes if (0 == AcpiGbl_FADT->TmrValExt) 1501590Srgrimes { 1511590Srgrimes *Resolution = 24; 1521590Srgrimes } 1531590Srgrimes else 1541590Srgrimes { 1551590Srgrimes *Resolution = 32; 1561590Srgrimes } 1571590Srgrimes 1581590Srgrimes return_ACPI_STATUS (AE_OK); 1591590Srgrimes} 1601590Srgrimes 1611590Srgrimes 1621590Srgrimes/****************************************************************************** 1631590Srgrimes * 1641590Srgrimes * FUNCTION: AcpiGetTimer 1651590Srgrimes * 1661590Srgrimes * PARAMETERS: none 1671590Srgrimes * 1681590Srgrimes * RETURN: Current value of the ACPI PM Timer (in ticks). 1691590Srgrimes * 1701590Srgrimes * DESCRIPTION: Obtains current value of ACPI PM Timer. 1711590Srgrimes * 1721590Srgrimes ******************************************************************************/ 1731590Srgrimes 1741590SrgrimesACPI_STATUS 1751590SrgrimesAcpiGetTimer ( 1761590Srgrimes UINT32 *Ticks) 1771590Srgrimes{ 1781590Srgrimes ACPI_FUNCTION_TRACE ("AcpiGetTimer"); 1791590Srgrimes 1801590Srgrimes 1811590Srgrimes if (!Ticks) 1821590Srgrimes { 1831590Srgrimes return_ACPI_STATUS (AE_BAD_PARAMETER); 1841590Srgrimes } 1851590Srgrimes 1861590Srgrimes AcpiOsReadPort ((ACPI_IO_ADDRESS) 1871590Srgrimes ACPI_GET_ADDRESS (AcpiGbl_FADT->XPmTmrBlk.Address), Ticks, 32); 1881590Srgrimes 1891590Srgrimes return_ACPI_STATUS (AE_OK); 1901590Srgrimes} 1911590Srgrimes 1921590Srgrimes 1931590Srgrimes/****************************************************************************** 1941590Srgrimes * 1951590Srgrimes * FUNCTION: AcpiGetTimerDuration 1961590Srgrimes * 1971590Srgrimes * PARAMETERS: StartTicks 1981590Srgrimes * EndTicks 1991590Srgrimes * TimeElapsed 2001590Srgrimes * 2011590Srgrimes * RETURN: TimeElapsed 2021590Srgrimes * 2031590Srgrimes * DESCRIPTION: Computes the time elapsed (in microseconds) between two 2041590Srgrimes * PM Timer time stamps, taking into account the possibility of 2051590Srgrimes * rollovers, the timer resolution, and timer frequency. 2061590Srgrimes * 2071590Srgrimes * The PM Timer's clock ticks at roughly 3.6 times per 2081590Srgrimes * _microsecond_, and its clock continues through Cx state 2091590Srgrimes * transitions (unlike many CPU timestamp counters) -- making it 2101590Srgrimes * a versatile and accurate timer. 2111590Srgrimes * 2121590Srgrimes * Note that this function accomodates only a single timer 2131590Srgrimes * rollover. Thus for 24-bit timers, this function should only 2141590Srgrimes * be used for calculating durations less than ~4.6 seconds 2151590Srgrimes * (~20 hours for 32-bit timers). 2161590Srgrimes * 2171590Srgrimes ******************************************************************************/ 2181590Srgrimes 2191590SrgrimesACPI_STATUS 2201590SrgrimesAcpiGetTimerDuration ( 2211590Srgrimes UINT32 StartTicks, 2221590Srgrimes UINT32 EndTicks, 2231590Srgrimes UINT32 *TimeElapsed) 2241590Srgrimes{ 2251590Srgrimes UINT32 DeltaTicks = 0; 2261590Srgrimes UINT64_OVERLAY NormalizedTicks; 2271590Srgrimes ACPI_STATUS Status; 2281590Srgrimes ACPI_INTEGER OutQuotient; 2291590Srgrimes 2301590Srgrimes 2311590Srgrimes ACPI_FUNCTION_TRACE ("AcpiGetTimerDuration"); 2321590Srgrimes 2331590Srgrimes 2341590Srgrimes if (!TimeElapsed) 2351590Srgrimes { 23619190Salex return_ACPI_STATUS (AE_BAD_PARAMETER); 2371590Srgrimes } 2381590Srgrimes 2391590Srgrimes /* 2401590Srgrimes * Compute Tick Delta: 2411590Srgrimes * ------------------- 2421590Srgrimes * Handle (max one) timer rollovers on 24- versus 32-bit timers. 2431590Srgrimes */ 2441590Srgrimes if (StartTicks < EndTicks) 2451590Srgrimes { 2461590Srgrimes DeltaTicks = EndTicks - StartTicks; 2471590Srgrimes } 2481590Srgrimes else if (StartTicks > EndTicks) 2491590Srgrimes { 2501590Srgrimes if (0 == AcpiGbl_FADT->TmrValExt) 2511590Srgrimes { 2521590Srgrimes /* 24-bit Timer */ 2531590Srgrimes 2541590Srgrimes DeltaTicks = (((0x00FFFFFF - StartTicks) + EndTicks) & 0x00FFFFFF); 2551590Srgrimes } 2561590Srgrimes else 2571590Srgrimes { 2581590Srgrimes /* 32-bit Timer */ 2591590Srgrimes 2601590Srgrimes DeltaTicks = (0xFFFFFFFF - StartTicks) + EndTicks; 2611590Srgrimes } 2621590Srgrimes } 2631590Srgrimes else 2641590Srgrimes { 2651590Srgrimes *TimeElapsed = 0; 2661590Srgrimes return_ACPI_STATUS (AE_OK); 2671590Srgrimes } 26819190Salex 2691590Srgrimes /* 2701590Srgrimes * Compute Duration: 2711590Srgrimes * ----------------- 2721590Srgrimes * 2731590Srgrimes * Requires a 64-bit divide: 2741590Srgrimes * 2751590Srgrimes * TimeElapsed = (DeltaTicks * 1000000) / PM_TIMER_FREQUENCY; 2761590Srgrimes */ 2771590Srgrimes NormalizedTicks.Full = ((UINT64) DeltaTicks) * 1000000; 2781590Srgrimes 2791590Srgrimes Status = AcpiUtShortDivide (&NormalizedTicks.Full, PM_TIMER_FREQUENCY, 2801590Srgrimes &OutQuotient, NULL); 2811590Srgrimes 2821590Srgrimes *TimeElapsed = (UINT32) OutQuotient; 2831590Srgrimes return_ACPI_STATUS (Status); 2841590Srgrimes} 2851590Srgrimes 2861590Srgrimes 2871590Srgrimes