Timer.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /** @file
  2. RISC-V Timer Architectural Protocol
  3. Copyright (c) 2019, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/BaseLib.h>
  7. #include <Library/BaseRiscVSbiLib.h>
  8. #include "Timer.h"
  9. //
  10. // The handle onto which the Timer Architectural Protocol will be installed
  11. //
  12. STATIC EFI_HANDLE mTimerHandle = NULL;
  13. //
  14. // The Timer Architectural Protocol that this driver produces
  15. //
  16. EFI_TIMER_ARCH_PROTOCOL mTimer = {
  17. TimerDriverRegisterHandler,
  18. TimerDriverSetTimerPeriod,
  19. TimerDriverGetTimerPeriod,
  20. TimerDriverGenerateSoftInterrupt
  21. };
  22. //
  23. // Pointer to the CPU Architectural Protocol instance
  24. //
  25. EFI_CPU_ARCH_PROTOCOL *mCpu;
  26. //
  27. // The notification function to call on every timer interrupt.
  28. // A bug in the compiler prevents us from initializing this here.
  29. //
  30. STATIC EFI_TIMER_NOTIFY mTimerNotifyFunction;
  31. //
  32. // The current period of the timer interrupt
  33. //
  34. STATIC UINT64 mTimerPeriod = 0;
  35. /**
  36. Timer Interrupt Handler.
  37. @param InterruptType The type of interrupt that occured
  38. @param SystemContext A pointer to the system context when the interrupt occured
  39. **/
  40. VOID
  41. EFIAPI
  42. TimerInterruptHandler (
  43. IN EFI_EXCEPTION_TYPE InterruptType,
  44. IN EFI_SYSTEM_CONTEXT SystemContext
  45. )
  46. {
  47. EFI_TPL OriginalTPL;
  48. UINT64 RiscvTimer;
  49. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  50. if (mTimerNotifyFunction != NULL) {
  51. mTimerNotifyFunction (mTimerPeriod);
  52. }
  53. RiscVDisableTimerInterrupt (); // Disable SMode timer int
  54. RiscVClearPendingTimerInterrupt ();
  55. if (mTimerPeriod == 0) {
  56. gBS->RestoreTPL (OriginalTPL);
  57. RiscVDisableTimerInterrupt (); // Disable SMode timer int
  58. return;
  59. }
  60. RiscvTimer = RiscVReadTimer ();
  61. SbiSetTimer (RiscvTimer += mTimerPeriod);
  62. gBS->RestoreTPL (OriginalTPL);
  63. RiscVEnableTimerInterrupt (); // enable SMode timer int
  64. }
  65. /**
  66. This function registers the handler NotifyFunction so it is called every time
  67. the timer interrupt fires. It also passes the amount of time since the last
  68. handler call to the NotifyFunction. If NotifyFunction is NULL, then the
  69. handler is unregistered. If the handler is registered, then EFI_SUCCESS is
  70. returned. If the CPU does not support registering a timer interrupt handler,
  71. then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
  72. when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  73. If an attempt is made to unregister a handler when a handler is not registered,
  74. then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
  75. register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
  76. is returned.
  77. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  78. @param NotifyFunction The function to call when a timer interrupt fires. This
  79. function executes at TPL_HIGH_LEVEL. The DXE Core will
  80. register a handler for the timer interrupt, so it can know
  81. how much time has passed. This information is used to
  82. signal timer based events. NULL will unregister the handler.
  83. @retval EFI_SUCCESS The timer handler was registered.
  84. @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
  85. @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
  86. registered.
  87. @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
  88. previously registered.
  89. @retval EFI_DEVICE_ERROR The timer handler could not be registered.
  90. **/
  91. EFI_STATUS
  92. EFIAPI
  93. TimerDriverRegisterHandler (
  94. IN EFI_TIMER_ARCH_PROTOCOL *This,
  95. IN EFI_TIMER_NOTIFY NotifyFunction
  96. )
  97. {
  98. DEBUG ((DEBUG_INFO, "TimerDriverRegisterHandler(0x%lx) called\n", NotifyFunction));
  99. mTimerNotifyFunction = NotifyFunction;
  100. return EFI_SUCCESS;
  101. }
  102. /**
  103. This function adjusts the period of timer interrupts to the value specified
  104. by TimerPeriod. If the timer period is updated, then the selected timer
  105. period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
  106. the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
  107. If an error occurs while attempting to update the timer period, then the
  108. timer hardware will be put back in its state prior to this call, and
  109. EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
  110. is disabled. This is not the same as disabling the CPU's interrupts.
  111. Instead, it must either turn off the timer hardware, or it must adjust the
  112. interrupt controller so that a CPU interrupt is not generated when the timer
  113. interrupt fires.
  114. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  115. @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
  116. the timer hardware is not programmable, then EFI_UNSUPPORTED is
  117. returned. If the timer is programmable, then the timer period
  118. will be rounded up to the nearest timer period that is supported
  119. by the timer hardware. If TimerPeriod is set to 0, then the
  120. timer interrupts will be disabled.
  121. @retval EFI_SUCCESS The timer period was changed.
  122. @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
  123. @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
  124. **/
  125. EFI_STATUS
  126. EFIAPI
  127. TimerDriverSetTimerPeriod (
  128. IN EFI_TIMER_ARCH_PROTOCOL *This,
  129. IN UINT64 TimerPeriod
  130. )
  131. {
  132. UINT64 RiscvTimer;
  133. DEBUG ((DEBUG_INFO, "TimerDriverSetTimerPeriod(0x%lx)\n", TimerPeriod));
  134. if (TimerPeriod == 0) {
  135. mTimerPeriod = 0;
  136. RiscVDisableTimerInterrupt (); // Disable SMode timer int
  137. return EFI_SUCCESS;
  138. }
  139. mTimerPeriod = TimerPeriod / 10; // convert unit from 100ns to 1us
  140. RiscvTimer = RiscVReadTimer ();
  141. SbiSetTimer (RiscvTimer + mTimerPeriod);
  142. mCpu->EnableInterrupt (mCpu);
  143. RiscVEnableTimerInterrupt (); // enable SMode timer int
  144. return EFI_SUCCESS;
  145. }
  146. /**
  147. This function retrieves the period of timer interrupts in 100 ns units,
  148. returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
  149. is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
  150. returned, then the timer is currently disabled.
  151. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  152. @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
  153. 0 is returned, then the timer is currently disabled.
  154. @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
  155. @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
  156. **/
  157. EFI_STATUS
  158. EFIAPI
  159. TimerDriverGetTimerPeriod (
  160. IN EFI_TIMER_ARCH_PROTOCOL *This,
  161. OUT UINT64 *TimerPeriod
  162. )
  163. {
  164. *TimerPeriod = mTimerPeriod;
  165. return EFI_SUCCESS;
  166. }
  167. /**
  168. This function generates a soft timer interrupt. If the platform does not support soft
  169. timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
  170. If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
  171. service, then a soft timer interrupt will be generated. If the timer interrupt is
  172. enabled when this service is called, then the registered handler will be invoked. The
  173. registered handler should not be able to distinguish a hardware-generated timer
  174. interrupt from a software-generated timer interrupt.
  175. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  176. @retval EFI_SUCCESS The soft timer interrupt was generated.
  177. @retval EFI_UNSUPPORTEDT The platform does not support the generation of soft timer interrupts.
  178. **/
  179. EFI_STATUS
  180. EFIAPI
  181. TimerDriverGenerateSoftInterrupt (
  182. IN EFI_TIMER_ARCH_PROTOCOL *This
  183. )
  184. {
  185. return EFI_SUCCESS;
  186. }
  187. /**
  188. Initialize the Timer Architectural Protocol driver
  189. @param ImageHandle ImageHandle of the loaded driver
  190. @param SystemTable Pointer to the System Table
  191. @retval EFI_SUCCESS Timer Architectural Protocol created
  192. @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
  193. @retval EFI_DEVICE_ERROR A device error occured attempting to initialize the driver.
  194. **/
  195. EFI_STATUS
  196. EFIAPI
  197. TimerDriverInitialize (
  198. IN EFI_HANDLE ImageHandle,
  199. IN EFI_SYSTEM_TABLE *SystemTable
  200. )
  201. {
  202. EFI_STATUS Status;
  203. //
  204. // Initialize the pointer to our notify function.
  205. //
  206. mTimerNotifyFunction = NULL;
  207. //
  208. // Make sure the Timer Architectural Protocol is not already installed in the system
  209. //
  210. ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);
  211. //
  212. // Find the CPU architectural protocol.
  213. //
  214. Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);
  215. ASSERT_EFI_ERROR (Status);
  216. //
  217. // Force the timer to be disabled
  218. //
  219. Status = TimerDriverSetTimerPeriod (&mTimer, 0);
  220. ASSERT_EFI_ERROR (Status);
  221. //
  222. // Install interrupt handler for RISC-V Timer.
  223. //
  224. Status = mCpu->RegisterInterruptHandler (mCpu, EXCEPT_RISCV_TIMER_INT, TimerInterruptHandler);
  225. ASSERT_EFI_ERROR (Status);
  226. //
  227. // Force the timer to be enabled at its default period
  228. //
  229. Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);
  230. ASSERT_EFI_ERROR (Status);
  231. //
  232. // Install the Timer Architectural Protocol onto a new handle
  233. //
  234. Status = gBS->InstallMultipleProtocolInterfaces (
  235. &mTimerHandle,
  236. &gEfiTimerArchProtocolGuid,
  237. &mTimer,
  238. NULL
  239. );
  240. ASSERT_EFI_ERROR (Status);
  241. return Status;
  242. }