Timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /** @file
  2. Timer Architectural Protocol as defined in the DXE CIS
  3. Copyright (c) 2022 Loongson Technology Corporation Limited. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Protocol/Cpu.h>
  7. #include "Library/Cpu.h"
  8. #include <Library/DebugLib.h>
  9. #include <Library/StableTimer.h>
  10. #include "Timer.h"
  11. #include <Library/TimerLib.h>
  12. #include <Library/UefiBootServicesTableLib.h>
  13. //
  14. // The handle onto which the Timer Architectural Protocol will be installed
  15. //
  16. EFI_HANDLE mTimerHandle = NULL;
  17. EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)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. EFI_TIMER_NOTIFY mTimerNotifyFunction;
  36. //
  37. // The current period of the timer interrupt
  38. //
  39. volatile UINT64 mTimerPeriod = 0;
  40. volatile UINT64 mTimerTicks = 0;
  41. //
  42. // Const frequence in Hz
  43. //
  44. extern UINT32 StableTimerFreq;
  45. /**
  46. Sets the counter value for timer.
  47. @param Count The 16-bit counter value to program into stable timer.
  48. @retval VOID
  49. **/
  50. VOID
  51. SetPitCount (
  52. IN UINT64 Count
  53. )
  54. {
  55. if (Count <= 4) {
  56. return;
  57. }
  58. Count &= LOONGARCH_CSR_TMCFG_TIMEVAL;
  59. Count |= LOONGARCH_CSR_TMCFG_EN | LOONGARCH_CSR_TMCFG_PERIOD;
  60. LoongarchWriteqTmcfg (Count);
  61. }
  62. /**
  63. Timer Interrupt Handler.
  64. @param InterruptType The type of interrupt that occurred
  65. @param SystemContext A pointer to the system context when the interrupt occurred
  66. @retval VOID
  67. **/
  68. VOID
  69. EFIAPI
  70. TimerInterruptHandler (
  71. IN EFI_EXCEPTION_TYPE InterruptType,
  72. IN EFI_SYSTEM_CONTEXT SystemContext
  73. )
  74. {
  75. EFI_TPL OriginalTPL;
  76. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  77. //
  78. // Clear interrupt.
  79. //
  80. LoongarchWriteqTintclr (0x1);
  81. if (mTimerNotifyFunction != NULL) {
  82. //
  83. // @bug : This does not handle missed timer interrupts
  84. //
  85. mTimerNotifyFunction (mTimerPeriod);
  86. }
  87. gBS->RestoreTPL (OriginalTPL);
  88. }
  89. /**
  90. This function registers the handler NotifyFunction so it is called every time
  91. the timer interrupt fires. It also passes the amount of time since the last
  92. handler call to the NotifyFunction. If NotifyFunction is NULL, then the
  93. handler is unregistered. If the handler is registered, then EFI_SUCCESS is
  94. returned. If the CPU does not support registering a timer interrupt handler,
  95. then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
  96. when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  97. If an attempt is made to unregister a handler when a handler is not registered,
  98. then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
  99. register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
  100. is returned.
  101. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  102. @param NotifyFunction The function to call when a timer interrupt fires. This
  103. function executes at TPL_HIGH_LEVEL. The DXE Core will
  104. register a handler for the timer interrupt, so it can know
  105. how much time has passed. This information is used to
  106. signal timer based events. NULL will unregister the handler.
  107. @retval EFI_SUCCESS The timer handler was registered.
  108. @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
  109. @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
  110. registered.
  111. @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
  112. previously registered.
  113. @retval EFI_DEVICE_ERROR The timer handler could not be registered.
  114. **/
  115. EFI_STATUS
  116. EFIAPI
  117. TimerDriverRegisterHandler (
  118. IN EFI_TIMER_ARCH_PROTOCOL *This,
  119. IN EFI_TIMER_NOTIFY NotifyFunction
  120. )
  121. {
  122. //
  123. // Check for invalid parameters
  124. //
  125. if ((NotifyFunction == NULL)
  126. && (mTimerNotifyFunction == NULL))
  127. {
  128. return EFI_INVALID_PARAMETER;
  129. }
  130. if ((NotifyFunction != NULL)
  131. && mTimerNotifyFunction != NULL)
  132. {
  133. return EFI_ALREADY_STARTED;
  134. }
  135. mTimerNotifyFunction = NotifyFunction;
  136. return EFI_SUCCESS;
  137. }
  138. /**
  139. This function adjusts the period of timer interrupts to the value specified
  140. by TimerPeriod. If the timer period is updated, then the selected timer
  141. period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
  142. the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
  143. If an error occurs while attempting to update the timer period, then the
  144. timer hardware will be put back in its state prior to this call, and
  145. EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
  146. is disabled. This is not the same as disabling the CPU's interrupts.
  147. Instead, it must either turn off the timer hardware, or it must adjust the
  148. interrupt controller so that a CPU interrupt is not generated when the timer
  149. interrupt fires.
  150. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  151. @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
  152. the timer hardware is not programmable, then EFI_UNSUPPORTED is
  153. returned. If the timer is programmable, then the timer period
  154. will be rounded up to the nearest timer period that is supported
  155. by the timer hardware. If TimerPeriod is set to 0, then the
  156. timer interrupts will be disabled.
  157. @retval EFI_SUCCESS The timer period was changed.
  158. @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
  159. @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
  160. **/
  161. EFI_STATUS
  162. EFIAPI
  163. TimerDriverSetTimerPeriod (
  164. IN EFI_TIMER_ARCH_PROTOCOL *This,
  165. IN UINT64 TimerPeriod
  166. )
  167. {
  168. UINT64 TimerCount;
  169. if (TimerPeriod == 0) {
  170. //
  171. // Disable timer interrupt for a TimerPeriod of 0
  172. //
  173. mCpu->DisableInterrupt (mCpu);
  174. } else {
  175. TimerCount = TimerPeriod * StableTimerFreq / 10000000ULL;
  176. if (TimerCount >= BIT48) {
  177. TimerCount = 0;
  178. }
  179. //
  180. // Program the stable timer with the new count value
  181. //
  182. mTimerTicks = TimerCount;
  183. SetPitCount (TimerCount);
  184. //
  185. // Enable timer interrupt
  186. //
  187. mCpu->EnableInterrupt (mCpu);
  188. }
  189. //
  190. // Save the new timer period
  191. //
  192. mTimerPeriod = TimerPeriod;
  193. return EFI_SUCCESS;
  194. }
  195. /**
  196. This function retrieves the period of timer interrupts in 100 ns units,
  197. returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
  198. is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
  199. returned, then the timer is currently disabled.
  200. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  201. @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
  202. 0 is returned, then the timer is currently disabled.
  203. @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
  204. @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
  205. **/
  206. EFI_STATUS
  207. EFIAPI
  208. TimerDriverGetTimerPeriod (
  209. IN EFI_TIMER_ARCH_PROTOCOL *This,
  210. OUT UINT64 *TimerPeriod
  211. )
  212. {
  213. if (TimerPeriod == NULL) {
  214. return EFI_INVALID_PARAMETER;
  215. }
  216. *TimerPeriod = mTimerPeriod;
  217. return EFI_SUCCESS;
  218. }
  219. /**
  220. Disable the timer
  221. DXE Core will disable the timer after all the event handlers have run.
  222. @param[in] Event The Event that is being processed
  223. @param[in] Context Event Context
  224. **/
  225. VOID
  226. EFIAPI
  227. ExitBootServicesEvent (
  228. IN EFI_EVENT Event,
  229. IN VOID *Context
  230. )
  231. {
  232. /*
  233. * Disable timer interrupt when exiting boot service
  234. */
  235. LoongarchWriteqTmcfg (0);
  236. }
  237. /**
  238. This function generates a soft timer interrupt. If the platform does not support soft
  239. timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
  240. If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler ()
  241. service, then a soft timer interrupt will be generated. If the timer interrupt is
  242. enabled when this service is called, then the registered handler will be invoked. The
  243. registered handler should not be able to distinguish a hardware-generated timer
  244. interrupt from a software-generated timer interrupt.
  245. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  246. @retval EFI_SUCCESS The soft timer interrupt was generated.
  247. @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
  248. **/
  249. EFI_STATUS
  250. EFIAPI
  251. TimerDriverGenerateSoftInterrupt (
  252. IN EFI_TIMER_ARCH_PROTOCOL *This
  253. )
  254. {
  255. return EFI_UNSUPPORTED;
  256. }
  257. /**
  258. Initialize the Timer Architectural Protocol driver
  259. @param ImageHandle ImageHandle of the loaded driver
  260. @param SystemTable Pointer to the System Table
  261. @retval EFI_SUCCESS Timer Architectural Protocol created
  262. @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
  263. @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
  264. **/
  265. EFI_STATUS
  266. EFIAPI
  267. StableTimerDriverInitialize (
  268. IN EFI_HANDLE ImageHandle,
  269. IN EFI_SYSTEM_TABLE *SystemTable
  270. )
  271. {
  272. EFI_STATUS Status;
  273. UINT32 TimerVector;
  274. //
  275. // Initialize the pointer to our notify function.
  276. //
  277. mTimerNotifyFunction = NULL;
  278. //
  279. // Make sure the Timer Architectural Protocol is not already installed in the system
  280. //
  281. ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);
  282. //
  283. // Find the CPU architectural protocol.
  284. //
  285. Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &mCpu);
  286. ASSERT_EFI_ERROR (Status);
  287. //
  288. // Force the timer to be disabled
  289. //
  290. Status = TimerDriverSetTimerPeriod (&mTimer, 0);
  291. ASSERT_EFI_ERROR (Status);
  292. //
  293. // Calculate const frequence
  294. //
  295. StableTimerFreq = CalcConstFreq ();
  296. DEBUG ((DEBUG_INFO, "===========Stable timer freq %d Hz=============\n", StableTimerFreq));
  297. //
  298. // Install interrupt handler for Stable Timer #0 (ISA IRQ0)
  299. //
  300. TimerVector = EXCEPT_LOONGARCH_INT_TIMER;
  301. Status = mCpu->RegisterInterruptHandler (mCpu, TimerVector, TimerInterruptHandler);
  302. ASSERT_EFI_ERROR (Status);
  303. //
  304. // Enable TI local timer interrupt
  305. //
  306. CpuSetIP (1 << 11);
  307. //
  308. // Force the timer to be enabled at its default period
  309. //
  310. Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);
  311. ASSERT_EFI_ERROR (Status);
  312. //
  313. // Install the Timer Architectural Protocol onto a new handle
  314. //
  315. Status = gBS->InstallMultipleProtocolInterfaces (
  316. &mTimerHandle,
  317. &gEfiTimerArchProtocolGuid, &mTimer,
  318. NULL
  319. );
  320. ASSERT_EFI_ERROR (Status);
  321. // Register for an ExitBootServicesEvent
  322. Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, ExitBootServicesEvent, NULL,
  323. &EfiExitBootServicesEvent);
  324. ASSERT_EFI_ERROR (Status);
  325. return Status;
  326. }