LocalApicTimerDxe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /** @file
  2. Timer Architectural Protocol as defined in the DXE CIS
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  4. Copyright (c) 2019, Citrix Systems, Inc.
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Library/NestedInterruptTplLib.h>
  8. #include "LocalApicTimerDxe.h"
  9. //
  10. // The handle onto which the Timer Architectural Protocol will be installed
  11. //
  12. 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. EFI_TIMER_NOTIFY mTimerNotifyFunction;
  31. //
  32. // The current period of the timer interrupt
  33. //
  34. volatile UINT64 mTimerPeriod = 0;
  35. //
  36. // Worker Functions
  37. //
  38. /**
  39. Interrupt Handler.
  40. @param InterruptType The type of interrupt that occurred
  41. @param SystemContext A pointer to the system context when the interrupt occurred
  42. **/
  43. VOID
  44. EFIAPI
  45. TimerInterruptHandler (
  46. IN EFI_EXCEPTION_TYPE InterruptType,
  47. IN EFI_SYSTEM_CONTEXT SystemContext
  48. )
  49. {
  50. STATIC NESTED_INTERRUPT_STATE NestedInterruptState;
  51. EFI_TPL OriginalTPL;
  52. OriginalTPL = NestedInterruptRaiseTPL ();
  53. SendApicEoi ();
  54. if (mTimerNotifyFunction != NULL) {
  55. //
  56. // @bug : This does not handle missed timer interrupts
  57. //
  58. mTimerNotifyFunction (mTimerPeriod);
  59. }
  60. NestedInterruptRestoreTPL (OriginalTPL, SystemContext, &NestedInterruptState);
  61. }
  62. /**
  63. This function registers the handler NotifyFunction so it is called every time
  64. the timer interrupt fires. It also passes the amount of time since the last
  65. handler call to the NotifyFunction. If NotifyFunction is NULL, then the
  66. handler is unregistered. If the handler is registered, then EFI_SUCCESS is
  67. returned. If the CPU does not support registering a timer interrupt handler,
  68. then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
  69. when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  70. If an attempt is made to unregister a handler when a handler is not registered,
  71. then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
  72. register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
  73. is returned.
  74. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  75. @param NotifyFunction The function to call when a timer interrupt fires. This
  76. function executes at TPL_HIGH_LEVEL. The DXE Core will
  77. register a handler for the timer interrupt, so it can know
  78. how much time has passed. This information is used to
  79. signal timer based events. NULL will unregister the handler.
  80. @retval EFI_SUCCESS The timer handler was registered.
  81. @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
  82. @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
  83. registered.
  84. @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
  85. previously registered.
  86. @retval EFI_DEVICE_ERROR The timer handler could not be registered.
  87. **/
  88. EFI_STATUS
  89. EFIAPI
  90. TimerDriverRegisterHandler (
  91. IN EFI_TIMER_ARCH_PROTOCOL *This,
  92. IN EFI_TIMER_NOTIFY NotifyFunction
  93. )
  94. {
  95. //
  96. // Check for invalid parameters
  97. //
  98. if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
  99. return EFI_INVALID_PARAMETER;
  100. }
  101. if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
  102. return EFI_ALREADY_STARTED;
  103. }
  104. mTimerNotifyFunction = NotifyFunction;
  105. return EFI_SUCCESS;
  106. }
  107. /**
  108. This function adjusts the period of timer interrupts to the value specified
  109. by TimerPeriod. If the timer period is updated, then the selected timer
  110. period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
  111. the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
  112. If an error occurs while attempting to update the timer period, then the
  113. timer hardware will be put back in its state prior to this call, and
  114. EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
  115. is disabled. This is not the same as disabling the CPU's interrupts.
  116. Instead, it must either turn off the timer hardware, or it must adjust the
  117. interrupt controller so that a CPU interrupt is not generated when the timer
  118. interrupt fires.
  119. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  120. @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
  121. the timer hardware is not programmable, then EFI_UNSUPPORTED is
  122. returned. If the timer is programmable, then the timer period
  123. will be rounded up to the nearest timer period that is supported
  124. by the timer hardware. If TimerPeriod is set to 0, then the
  125. timer interrupts will be disabled.
  126. @retval EFI_SUCCESS The timer period was changed.
  127. @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
  128. @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
  129. **/
  130. EFI_STATUS
  131. EFIAPI
  132. TimerDriverSetTimerPeriod (
  133. IN EFI_TIMER_ARCH_PROTOCOL *This,
  134. IN UINT64 TimerPeriod
  135. )
  136. {
  137. UINT64 TimerCount;
  138. UINT32 TimerFrequency;
  139. UINT32 DivideValue = 1;
  140. if (TimerPeriod == 0) {
  141. //
  142. // Disable timer interrupt for a TimerPeriod of 0
  143. //
  144. DisableApicTimerInterrupt ();
  145. } else {
  146. TimerFrequency = PcdGet32 (PcdFSBClock) / (UINT32)DivideValue;
  147. //
  148. // Convert TimerPeriod into local APIC counts
  149. //
  150. // TimerPeriod is in 100ns
  151. // TimerPeriod/10000000 will be in seconds.
  152. TimerCount = DivU64x32 (
  153. MultU64x32 (TimerPeriod, TimerFrequency),
  154. 10000000
  155. );
  156. // Check for overflow
  157. if (TimerCount > MAX_UINT32) {
  158. TimerCount = MAX_UINT32;
  159. /* TimerPeriod = (MAX_UINT32 / TimerFrequency) * 10000000; */
  160. TimerPeriod = 429496730;
  161. }
  162. //
  163. // Program the timer with the new count value
  164. //
  165. InitializeApicTimer (DivideValue, (UINT32)TimerCount, TRUE, LOCAL_APIC_TIMER_VECTOR);
  166. //
  167. // Enable timer interrupt
  168. //
  169. EnableApicTimerInterrupt ();
  170. }
  171. //
  172. // Save the new timer period
  173. //
  174. mTimerPeriod = TimerPeriod;
  175. return EFI_SUCCESS;
  176. }
  177. /**
  178. This function retrieves the period of timer interrupts in 100 ns units,
  179. returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
  180. is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
  181. returned, then the timer is currently disabled.
  182. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  183. @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
  184. 0 is returned, then the timer is currently disabled.
  185. @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
  186. @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
  187. **/
  188. EFI_STATUS
  189. EFIAPI
  190. TimerDriverGetTimerPeriod (
  191. IN EFI_TIMER_ARCH_PROTOCOL *This,
  192. OUT UINT64 *TimerPeriod
  193. )
  194. {
  195. if (TimerPeriod == NULL) {
  196. return EFI_INVALID_PARAMETER;
  197. }
  198. *TimerPeriod = mTimerPeriod;
  199. return EFI_SUCCESS;
  200. }
  201. /**
  202. This function generates a soft timer interrupt. If the platform does not support soft
  203. timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
  204. If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
  205. service, then a soft timer interrupt will be generated. If the timer interrupt is
  206. enabled when this service is called, then the registered handler will be invoked. The
  207. registered handler should not be able to distinguish a hardware-generated timer
  208. interrupt from a software-generated timer interrupt.
  209. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  210. @retval EFI_SUCCESS The soft timer interrupt was generated.
  211. @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
  212. **/
  213. EFI_STATUS
  214. EFIAPI
  215. TimerDriverGenerateSoftInterrupt (
  216. IN EFI_TIMER_ARCH_PROTOCOL *This
  217. )
  218. {
  219. EFI_TPL OriginalTPL;
  220. if (GetApicTimerInterruptState ()) {
  221. //
  222. // Invoke the registered handler
  223. //
  224. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  225. if (mTimerNotifyFunction != NULL) {
  226. //
  227. // @bug : This does not handle missed timer interrupts
  228. //
  229. mTimerNotifyFunction (mTimerPeriod);
  230. }
  231. gBS->RestoreTPL (OriginalTPL);
  232. } else {
  233. return EFI_UNSUPPORTED;
  234. }
  235. return EFI_SUCCESS;
  236. }
  237. /**
  238. Initialize the Timer Architectural Protocol driver
  239. @param ImageHandle ImageHandle of the loaded driver
  240. @param SystemTable Pointer to the System Table
  241. @retval EFI_SUCCESS Timer Architectural Protocol created
  242. @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
  243. @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
  244. **/
  245. EFI_STATUS
  246. EFIAPI
  247. TimerDriverInitialize (
  248. IN EFI_HANDLE ImageHandle,
  249. IN EFI_SYSTEM_TABLE *SystemTable
  250. )
  251. {
  252. EFI_STATUS Status;
  253. //
  254. // Initialize the pointer to our notify function.
  255. //
  256. mTimerNotifyFunction = NULL;
  257. //
  258. // Make sure the Timer Architectural Protocol is not already installed in the system
  259. //
  260. ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);
  261. //
  262. // Find the CPU architectural protocol.
  263. //
  264. Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);
  265. ASSERT_EFI_ERROR (Status);
  266. //
  267. // Force the timer to be disabled
  268. //
  269. Status = TimerDriverSetTimerPeriod (&mTimer, 0);
  270. ASSERT_EFI_ERROR (Status);
  271. //
  272. // Install interrupt handler for Local APIC Timer
  273. //
  274. Status = mCpu->RegisterInterruptHandler (
  275. mCpu,
  276. LOCAL_APIC_TIMER_VECTOR,
  277. TimerInterruptHandler
  278. );
  279. ASSERT_EFI_ERROR (Status);
  280. //
  281. // Force the timer to be enabled at its default period
  282. //
  283. Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);
  284. ASSERT_EFI_ERROR (Status);
  285. //
  286. // Install the Timer Architectural Protocol onto a new handle
  287. //
  288. Status = gBS->InstallMultipleProtocolInterfaces (
  289. &mTimerHandle,
  290. &gEfiTimerArchProtocolGuid,
  291. &mTimer,
  292. NULL
  293. );
  294. ASSERT_EFI_ERROR (Status);
  295. return Status;
  296. }