Timer.c 10 KB

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