hwtimer.c 6.0 KB

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