LocalApicTimerDxe.c 11 KB

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