Timer.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*++ @file
  2. Emu Emulation Timer Architectural Protocol Driver as defined in DXE CIS
  3. This Timer module uses an Emu Thread to simulate the timer-tick driven
  4. timer service. In the future, the Thread creation should possibly be
  5. abstracted by the CPU architectural protocol
  6. Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
  7. Portions copyright (c) 2010 - 2011, Apple Inc. All rights reserved.
  8. SPDX-License-Identifier: BSD-2-Clause-Patent
  9. **/
  10. #include "PiDxe.h"
  11. #include <Protocol/Timer.h>
  12. #include <Protocol/Cpu.h>
  13. #include "Timer.h"
  14. #include <Library/BaseLib.h>
  15. #include <Library/DebugLib.h>
  16. #include <Library/UefiLib.h>
  17. #include <Library/UefiDriverEntryPoint.h>
  18. #include <Library/MemoryAllocationLib.h>
  19. #include <Library/UefiBootServicesTableLib.h>
  20. #include <Library/EmuThunkLib.h>
  21. //
  22. // Pointer to the CPU Architectural Protocol instance
  23. //
  24. EFI_CPU_ARCH_PROTOCOL *mCpu;
  25. //
  26. // The Timer Architectural Protocol that this driver produces
  27. //
  28. EFI_TIMER_ARCH_PROTOCOL mTimer = {
  29. EmuTimerDriverRegisterHandler,
  30. EmuTimerDriverSetTimerPeriod,
  31. EmuTimerDriverGetTimerPeriod,
  32. EmuTimerDriverGenerateSoftInterrupt
  33. };
  34. //
  35. // The notification function to call on every timer interrupt
  36. //
  37. EFI_TIMER_NOTIFY mTimerNotifyFunction = NULL;
  38. //
  39. // The current period of the timer interrupt
  40. //
  41. UINT64 mTimerPeriodMs;
  42. VOID
  43. EFIAPI
  44. TimerCallback (
  45. UINT64 DeltaMs
  46. )
  47. {
  48. EFI_TPL OriginalTPL;
  49. EFI_TIMER_NOTIFY CallbackFunction;
  50. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  51. if (OriginalTPL < TPL_HIGH_LEVEL) {
  52. CallbackFunction = mTimerNotifyFunction;
  53. //
  54. // Only invoke the callback function if a Non-NULL handler has been
  55. // registered. Assume all other handlers are legal.
  56. //
  57. if (CallbackFunction != NULL) {
  58. CallbackFunction (MultU64x32 (DeltaMs, 10000));
  59. }
  60. }
  61. gBS->RestoreTPL (OriginalTPL);
  62. }
  63. EFI_STATUS
  64. EFIAPI
  65. EmuTimerDriverRegisterHandler (
  66. IN EFI_TIMER_ARCH_PROTOCOL *This,
  67. IN EFI_TIMER_NOTIFY NotifyFunction
  68. )
  69. /*++
  70. Routine Description:
  71. This function registers the handler NotifyFunction so it is called every time
  72. the timer interrupt fires. It also passes the amount of time since the last
  73. handler call to the NotifyFunction. If NotifyFunction is NULL, then the
  74. handler is unregistered. If the handler is registered, then EFI_SUCCESS is
  75. returned. If the CPU does not support registering a timer interrupt handler,
  76. then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
  77. when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  78. If an attempt is made to unregister a handler when a handler is not registered,
  79. then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
  80. register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
  81. is returned.
  82. Arguments:
  83. This - The EFI_TIMER_ARCH_PROTOCOL instance.
  84. NotifyFunction - The function to call when a timer interrupt fires. This
  85. function executes at TPL_HIGH_LEVEL. The DXE Core will
  86. register a handler for the timer interrupt, so it can know
  87. how much time has passed. This information is used to
  88. signal timer based events. NULL will unregister the handler.
  89. Returns:
  90. EFI_SUCCESS - The timer handler was registered.
  91. EFI_UNSUPPORTED - The platform does not support timer interrupts.
  92. EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
  93. registered.
  94. EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
  95. previously registered.
  96. EFI_DEVICE_ERROR - The timer handler could not be registered.
  97. **/
  98. {
  99. //
  100. // Check for invalid parameters
  101. //
  102. if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
  103. return EFI_INVALID_PARAMETER;
  104. }
  105. if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
  106. return EFI_ALREADY_STARTED;
  107. }
  108. if (NotifyFunction == NULL) {
  109. /* Disable timer. */
  110. gEmuThunk->SetTimer (0, TimerCallback);
  111. } else if (mTimerNotifyFunction == NULL) {
  112. /* Enable Timer. */
  113. gEmuThunk->SetTimer (mTimerPeriodMs, TimerCallback);
  114. }
  115. mTimerNotifyFunction = NotifyFunction;
  116. return EFI_SUCCESS;
  117. }
  118. EFI_STATUS
  119. EFIAPI
  120. EmuTimerDriverSetTimerPeriod (
  121. IN EFI_TIMER_ARCH_PROTOCOL *This,
  122. IN UINT64 TimerPeriod
  123. )
  124. /*++
  125. Routine Description:
  126. This function adjusts the period of timer interrupts to the value specified
  127. by TimerPeriod. If the timer period is updated, then the selected timer
  128. period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
  129. the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
  130. If an error occurs while attempting to update the timer period, then the
  131. timer hardware will be put back in its state prior to this call, and
  132. EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
  133. is disabled. This is not the same as disabling the CPU's interrupts.
  134. Instead, it must either turn off the timer hardware, or it must adjust the
  135. interrupt controller so that a CPU interrupt is not generated when the timer
  136. interrupt fires.
  137. Arguments:
  138. This - The EFI_TIMER_ARCH_PROTOCOL instance.
  139. TimerPeriod - The rate to program the timer interrupt in 100 nS units. If
  140. the timer hardware is not programmable, then EFI_UNSUPPORTED is
  141. returned. If the timer is programmable, then the timer period
  142. will be rounded up to the nearest timer period that is supported
  143. by the timer hardware. If TimerPeriod is set to 0, then the
  144. timer interrupts will be disabled.
  145. Returns:
  146. EFI_SUCCESS - The timer period was changed.
  147. EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.
  148. EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.
  149. **/
  150. {
  151. //
  152. // If TimerPeriod is 0, then the timer thread should be canceled
  153. // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread
  154. //
  155. if ( (TimerPeriod == 0)
  156. || ( (TimerPeriod > TIMER_MINIMUM_VALUE)
  157. && (TimerPeriod < TIMER_MAXIMUM_VALUE)))
  158. {
  159. mTimerPeriodMs = DivU64x32 (TimerPeriod + 5000, 10000);
  160. gEmuThunk->SetTimer (mTimerPeriodMs, TimerCallback);
  161. }
  162. return EFI_SUCCESS;
  163. }
  164. EFI_STATUS
  165. EFIAPI
  166. EmuTimerDriverGetTimerPeriod (
  167. IN EFI_TIMER_ARCH_PROTOCOL *This,
  168. OUT UINT64 *TimerPeriod
  169. )
  170. /*++
  171. Routine Description:
  172. This function retrieves the period of timer interrupts in 100 ns units,
  173. returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
  174. is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
  175. returned, then the timer is currently disabled.
  176. Arguments:
  177. This - The EFI_TIMER_ARCH_PROTOCOL instance.
  178. TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If
  179. 0 is returned, then the timer is currently disabled.
  180. Returns:
  181. EFI_SUCCESS - The timer period was returned in TimerPeriod.
  182. EFI_INVALID_PARAMETER - TimerPeriod is NULL.
  183. **/
  184. {
  185. if (TimerPeriod == NULL) {
  186. return EFI_INVALID_PARAMETER;
  187. }
  188. *TimerPeriod = MultU64x32 (mTimerPeriodMs, 10000);
  189. return EFI_SUCCESS;
  190. }
  191. EFI_STATUS
  192. EFIAPI
  193. EmuTimerDriverGenerateSoftInterrupt (
  194. IN EFI_TIMER_ARCH_PROTOCOL *This
  195. )
  196. /*++
  197. Routine Description:
  198. This function generates a soft timer interrupt. If the platform does not support soft
  199. timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
  200. If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
  201. service, then a soft timer interrupt will be generated. If the timer interrupt is
  202. enabled when this service is called, then the registered handler will be invoked. The
  203. registered handler should not be able to distinguish a hardware-generated timer
  204. interrupt from a software-generated timer interrupt.
  205. Arguments:
  206. This - The EFI_TIMER_ARCH_PROTOCOL instance.
  207. Returns:
  208. EFI_SUCCESS - The soft timer interrupt was generated.
  209. EFI_UNSUPPORTED - The platform does not support the generation of soft timer interrupts.
  210. **/
  211. {
  212. return EFI_UNSUPPORTED;
  213. }
  214. EFI_STATUS
  215. EFIAPI
  216. EmuTimerDriverInitialize (
  217. IN EFI_HANDLE ImageHandle,
  218. IN EFI_SYSTEM_TABLE *SystemTable
  219. )
  220. /*++
  221. Routine Description:
  222. Initialize the Timer Architectural Protocol driver
  223. Arguments:
  224. ImageHandle - ImageHandle of the loaded driver
  225. SystemTable - Pointer to the System Table
  226. Returns:
  227. EFI_SUCCESS - Timer Architectural Protocol created
  228. EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.
  229. EFI_DEVICE_ERROR - A device error occurred attempting to initialize the driver.
  230. **/
  231. {
  232. EFI_STATUS Status;
  233. EFI_HANDLE Handle;
  234. //
  235. // Make sure the Timer Architectural Protocol is not already installed in the system
  236. //
  237. ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);
  238. //
  239. // Get the CPU Architectural Protocol instance
  240. //
  241. Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (void *)&mCpu);
  242. ASSERT_EFI_ERROR (Status);
  243. //
  244. // Start the timer thread at the default timer period
  245. //
  246. Status = mTimer.SetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);
  247. if (EFI_ERROR (Status)) {
  248. return Status;
  249. }
  250. //
  251. // Install the Timer Architectural Protocol onto a new handle
  252. //
  253. Handle = NULL;
  254. Status = gBS->InstallProtocolInterface (
  255. &Handle,
  256. &gEfiTimerArchProtocolGuid,
  257. EFI_NATIVE_INTERFACE,
  258. &mTimer
  259. );
  260. if (EFI_ERROR (Status)) {
  261. return Status;
  262. }
  263. return EFI_SUCCESS;
  264. }