timer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. timer.c
  9. Abstract:
  10. This module implements support for hardware timer services in the UEFI
  11. core, including the periodic tick and time counter.
  12. Author:
  13. Evan Green 3-Mar-3014
  14. Environment:
  15. Firmware
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include "ueficore.h"
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // ------------------------------------------------------ Data Type Definitions
  26. //
  27. //
  28. // ----------------------------------------------- Internal Function Prototypes
  29. //
  30. //
  31. // -------------------------------------------------------------------- Globals
  32. //
  33. //
  34. // Define timer services data.
  35. //
  36. UINT32 EfiClockTimerInterruptNumber;
  37. EFI_PLATFORM_SERVICE_TIMER_INTERRUPT EfiClockTimerServiceRoutine;
  38. EFI_PLATFORM_READ_TIMER EfiReadTimerRoutine;
  39. UINT64 EfiReadTimerFrequency;
  40. UINT32 EfiReadTimerWidth;
  41. UINT64 EfiTimeCounterValue;
  42. UINTN EfiClockInterruptCount;
  43. //
  44. // ------------------------------------------------------------------ Functions
  45. //
  46. EFIAPI
  47. EFI_STATUS
  48. EfiCoreGetNextMonotonicCount (
  49. UINT64 *Count
  50. )
  51. /*++
  52. Routine Description:
  53. This routine returns a monotonically increasing count for the platform.
  54. Arguments:
  55. Count - Supplies a pointer where the next count is returned.
  56. Return Value:
  57. EFI_SUCCESS on success.
  58. EFI_INVALID_PARAMETER if the count is NULL.
  59. EFI_DEVICE_ERROR if the device is not functioning properly.
  60. --*/
  61. {
  62. if (Count == NULL) {
  63. return EFI_INVALID_PARAMETER;
  64. }
  65. if (EfiReadTimerRoutine == NULL) {
  66. return EFI_UNSUPPORTED;
  67. }
  68. *Count = EfiCoreReadTimeCounter();
  69. return EFI_SUCCESS;
  70. }
  71. EFIAPI
  72. EFI_STATUS
  73. EfiCoreStall (
  74. UINTN Microseconds
  75. )
  76. /*++
  77. Routine Description:
  78. This routine induces a fine-grained delay.
  79. Arguments:
  80. Microseconds - Supplies the number of microseconds to stall execution for.
  81. Return Value:
  82. EFI_SUCCESS on success.
  83. --*/
  84. {
  85. UINT64 CurrentTime;
  86. UINT64 EndTime;
  87. UINT64 Frequency;
  88. if (EfiReadTimerRoutine == NULL) {
  89. return EFI_UNSUPPORTED;
  90. }
  91. CurrentTime = EfiCoreReadTimeCounter();
  92. Frequency = EfiCoreGetTimeCounterFrequency();
  93. if (Frequency == 0) {
  94. return EFI_DEVICE_ERROR;
  95. }
  96. EndTime = CurrentTime + ((Microseconds * Frequency) / 1000000ULL);
  97. while (EfiCoreReadTimeCounter() < EndTime) {
  98. NOTHING;
  99. }
  100. return EFI_SUCCESS;
  101. }
  102. EFIAPI
  103. EFI_STATUS
  104. EfiCoreSetWatchdogTimer (
  105. UINTN Timeout,
  106. UINT64 WatchdogCode,
  107. UINTN DataSize,
  108. CHAR16 *WatchdogData
  109. )
  110. /*++
  111. Routine Description:
  112. This routine sets the system's watchdog timer.
  113. Arguments:
  114. Timeout - Supplies the number of seconds to set the timer for.
  115. WatchdogCode - Supplies a numeric code to log on a watchdog timeout event.
  116. DataSize - Supplies the size of the watchdog data.
  117. WatchdogData - Supplies an optional buffer that includes a null-terminated
  118. string, optionally followed by additional binary data.
  119. Return Value:
  120. EFI_SUCCESS on success.
  121. EFI_INVALID_PARAMETER if the supplied watchdog code is invalid.
  122. EFI_UNSUPPORTED if there is no watchdog timer.
  123. EFI_DEVICE_ERROR if an error occurred accessing the device hardware.
  124. --*/
  125. {
  126. EFI_STATUS Status;
  127. Status = EfiPlatformSetWatchdogTimer(Timeout,
  128. WatchdogCode,
  129. DataSize,
  130. WatchdogData);
  131. return Status;
  132. }
  133. UINT64
  134. EfiCoreReadTimeCounter (
  135. VOID
  136. )
  137. /*++
  138. Routine Description:
  139. This routine reads the current time counter value.
  140. Arguments:
  141. None.
  142. Return Value:
  143. Returns a 64-bit non-decreasing value.
  144. 0 if the time counter is not implemented.
  145. --*/
  146. {
  147. BOOLEAN Enabled;
  148. UINT64 HardwareMask;
  149. UINT64 HardwareValue;
  150. UINT64 HighBit;
  151. UINT64 Value;
  152. if (EfiReadTimerRoutine == NULL) {
  153. return 0;
  154. }
  155. Enabled = EfiDisableInterrupts();
  156. HardwareValue = EfiReadTimerRoutine();
  157. HardwareMask = (1ULL << EfiReadTimerWidth) - 1;
  158. HighBit = 1ULL << (EfiReadTimerWidth - 1);
  159. //
  160. // If the high bit flipped from one to zero, add one to the software
  161. // part.
  162. //
  163. if (((EfiTimeCounterValue & HighBit) != 0) &&
  164. ((HardwareValue & HighBit) == 0)) {
  165. EfiTimeCounterValue += 1ULL << EfiReadTimerWidth;
  166. }
  167. Value = (EfiTimeCounterValue & (~HardwareMask)) | HardwareValue;
  168. EfiTimeCounterValue = Value;
  169. if (Enabled != FALSE) {
  170. EfiEnableInterrupts();
  171. }
  172. return Value;
  173. }
  174. UINT64
  175. EfiCoreReadRecentTimeCounter (
  176. VOID
  177. )
  178. /*++
  179. Routine Description:
  180. This routine reads a relatively recent but not entirely up to date version
  181. of the time counter.
  182. Arguments:
  183. None.
  184. Return Value:
  185. Returns a 64-bit non-decreasing value.
  186. 0 if the time counter is not implemented.
  187. --*/
  188. {
  189. BOOLEAN Enabled;
  190. UINT64 Value;
  191. Enabled = EfiDisableInterrupts();
  192. Value = EfiTimeCounterValue;
  193. if (Enabled != FALSE) {
  194. EfiEnableInterrupts();
  195. }
  196. return Value;
  197. }
  198. UINT64
  199. EfiCoreGetTimeCounterFrequency (
  200. VOID
  201. )
  202. /*++
  203. Routine Description:
  204. This routine returns the frequency of the time counter.
  205. Arguments:
  206. None.
  207. Return Value:
  208. Returns the frequency of the time counter.
  209. 0 if the time counter is not implemented.
  210. --*/
  211. {
  212. return EfiReadTimerFrequency;
  213. }
  214. VOID
  215. EfiCoreServiceClockInterrupt (
  216. UINT32 InterruptNumber
  217. )
  218. /*++
  219. Routine Description:
  220. This routine is called to service the clock interrupt.
  221. Arguments:
  222. InterruptNumber - Supplies the interrupt number that fired.
  223. Return Value:
  224. None.
  225. --*/
  226. {
  227. UINT64 NewTime;
  228. ASSERT(InterruptNumber == EfiClockTimerInterruptNumber);
  229. ASSERT(EfiClockTimerServiceRoutine != NULL);
  230. ASSERT(EfiAreInterruptsEnabled() == FALSE);
  231. EfiClockInterruptCount += 1;
  232. //
  233. // Read the time counter to keep it up to date.
  234. //
  235. NewTime = EfiCoreReadTimeCounter();
  236. EfiClockTimerServiceRoutine(InterruptNumber);
  237. EfipCoreTimerTick(NewTime);
  238. return;
  239. }
  240. EFI_STATUS
  241. EfiCoreInitializeTimerServices (
  242. VOID
  243. )
  244. /*++
  245. Routine Description:
  246. This routine initializes platform timer services, including the periodic
  247. tick and time counter.
  248. Arguments:
  249. None.
  250. Return Value:
  251. EFI status code.
  252. --*/
  253. {
  254. BOOLEAN Enabled;
  255. EFI_STATUS Status;
  256. Enabled = EfiDisableInterrupts();
  257. Status = EfiPlatformInitializeTimers(&EfiClockTimerInterruptNumber,
  258. &EfiClockTimerServiceRoutine,
  259. &EfiReadTimerRoutine,
  260. &EfiReadTimerFrequency,
  261. &EfiReadTimerWidth);
  262. if (EFI_ERROR(Status)) {
  263. goto CoreInitializeTimerServicesEnd;
  264. }
  265. ASSERT((EfiReadTimerRoutine != NULL) &&
  266. (EfiReadTimerFrequency != 0) &&
  267. (EfiReadTimerWidth > 1));
  268. //
  269. // Perform an initial read of the counter to get a baseline.
  270. //
  271. EfiCoreReadTimeCounter();
  272. Status = EFI_SUCCESS;
  273. CoreInitializeTimerServicesEnd:
  274. if (Enabled != FALSE) {
  275. EfiEnableInterrupts();
  276. }
  277. return Status;
  278. }
  279. VOID
  280. EfiCoreTerminateTimerServices (
  281. VOID
  282. )
  283. /*++
  284. Routine Description:
  285. This routine terminates timer services in preparation for the termination
  286. of boot services.
  287. Arguments:
  288. None.
  289. Return Value:
  290. None.
  291. --*/
  292. {
  293. EfiPlatformTerminateTimers();
  294. return;
  295. }
  296. //
  297. // --------------------------------------------------------- Internal Functions
  298. //