hwtimer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /******************************************************************************
  2. *
  3. * Name: hwtimer.c - ACPI Power Management Timer Interface
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  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. #define EXPORT_ACPI_INTERFACES
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #define _COMPONENT ACPI_HARDWARE
  46. ACPI_MODULE_NAME("hwtimer")
  47. #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
  48. /******************************************************************************
  49. *
  50. * FUNCTION: acpi_get_timer_resolution
  51. *
  52. * PARAMETERS: resolution - Where the resolution is returned
  53. *
  54. * RETURN: Status and timer resolution
  55. *
  56. * DESCRIPTION: Obtains resolution of the ACPI PM Timer (24 or 32 bits).
  57. *
  58. ******************************************************************************/
  59. acpi_status acpi_get_timer_resolution(u32 * resolution)
  60. {
  61. ACPI_FUNCTION_TRACE(acpi_get_timer_resolution);
  62. if (!resolution) {
  63. return_ACPI_STATUS(AE_BAD_PARAMETER);
  64. }
  65. if ((acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER) == 0) {
  66. *resolution = 24;
  67. } else {
  68. *resolution = 32;
  69. }
  70. return_ACPI_STATUS(AE_OK);
  71. }
  72. ACPI_EXPORT_SYMBOL(acpi_get_timer_resolution)
  73. /******************************************************************************
  74. *
  75. * FUNCTION: acpi_get_timer
  76. *
  77. * PARAMETERS: ticks - Where the timer value is returned
  78. *
  79. * RETURN: Status and current timer value (ticks)
  80. *
  81. * DESCRIPTION: Obtains current value of ACPI PM Timer (in ticks).
  82. *
  83. ******************************************************************************/
  84. acpi_status acpi_get_timer(u32 * ticks)
  85. {
  86. acpi_status status;
  87. ACPI_FUNCTION_TRACE(acpi_get_timer);
  88. if (!ticks) {
  89. return_ACPI_STATUS(AE_BAD_PARAMETER);
  90. }
  91. /* ACPI 5.0A: PM Timer is optional */
  92. if (!acpi_gbl_FADT.xpm_timer_block.address) {
  93. return_ACPI_STATUS(AE_SUPPORT);
  94. }
  95. status = acpi_hw_read(ticks, &acpi_gbl_FADT.xpm_timer_block);
  96. return_ACPI_STATUS(status);
  97. }
  98. ACPI_EXPORT_SYMBOL(acpi_get_timer)
  99. /******************************************************************************
  100. *
  101. * FUNCTION: acpi_get_timer_duration
  102. *
  103. * PARAMETERS: start_ticks - Starting timestamp
  104. * end_ticks - End timestamp
  105. * time_elapsed - Where the elapsed time is returned
  106. *
  107. * RETURN: Status and time_elapsed
  108. *
  109. * DESCRIPTION: Computes the time elapsed (in microseconds) between two
  110. * PM Timer time stamps, taking into account the possibility of
  111. * rollovers, the timer resolution, and timer frequency.
  112. *
  113. * The PM Timer's clock ticks at roughly 3.6 times per
  114. * _microsecond_, and its clock continues through Cx state
  115. * transitions (unlike many CPU timestamp counters) -- making it
  116. * a versatile and accurate timer.
  117. *
  118. * Note that this function accommodates only a single timer
  119. * rollover. Thus for 24-bit timers, this function should only
  120. * be used for calculating durations less than ~4.6 seconds
  121. * (~20 minutes for 32-bit timers) -- calculations below:
  122. *
  123. * 2**24 Ticks / 3,600,000 Ticks/Sec = 4.66 sec
  124. * 2**32 Ticks / 3,600,000 Ticks/Sec = 1193 sec or 19.88 minutes
  125. *
  126. ******************************************************************************/
  127. acpi_status
  128. acpi_get_timer_duration(u32 start_ticks, u32 end_ticks, u32 * time_elapsed)
  129. {
  130. acpi_status status;
  131. u32 delta_ticks;
  132. u64 quotient;
  133. ACPI_FUNCTION_TRACE(acpi_get_timer_duration);
  134. if (!time_elapsed) {
  135. return_ACPI_STATUS(AE_BAD_PARAMETER);
  136. }
  137. /* ACPI 5.0A: PM Timer is optional */
  138. if (!acpi_gbl_FADT.xpm_timer_block.address) {
  139. return_ACPI_STATUS(AE_SUPPORT);
  140. }
  141. /*
  142. * Compute Tick Delta:
  143. * Handle (max one) timer rollovers on 24-bit versus 32-bit timers.
  144. */
  145. if (start_ticks < end_ticks) {
  146. delta_ticks = end_ticks - start_ticks;
  147. } else if (start_ticks > end_ticks) {
  148. if ((acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER) == 0) {
  149. /* 24-bit Timer */
  150. delta_ticks =
  151. (((0x00FFFFFF - start_ticks) +
  152. end_ticks) & 0x00FFFFFF);
  153. } else {
  154. /* 32-bit Timer */
  155. delta_ticks = (0xFFFFFFFF - start_ticks) + end_ticks;
  156. }
  157. } else { /* start_ticks == end_ticks */
  158. *time_elapsed = 0;
  159. return_ACPI_STATUS(AE_OK);
  160. }
  161. /*
  162. * Compute Duration (Requires a 64-bit multiply and divide):
  163. *
  164. * time_elapsed (microseconds) =
  165. * (delta_ticks * ACPI_USEC_PER_SEC) / ACPI_PM_TIMER_FREQUENCY;
  166. */
  167. status = acpi_ut_short_divide(((u64)delta_ticks) * ACPI_USEC_PER_SEC,
  168. ACPI_PM_TIMER_FREQUENCY, &quotient, NULL);
  169. *time_elapsed = (u32) quotient;
  170. return_ACPI_STATUS(status);
  171. }
  172. ACPI_EXPORT_SYMBOL(acpi_get_timer_duration)
  173. #endif /* !ACPI_REDUCED_HARDWARE */