TimerDxe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /** @file
  2. Timer Architecture Protocol driver of the ARM flavor
  3. Copyright (c) 2011-2013 ARM Ltd. All rights reserved.<BR>
  4. This program and the accompanying materials
  5. are licensed and made available under the terms and conditions of the BSD License
  6. which accompanies this distribution. The full text of the license may be found at
  7. http://opensource.org/licenses/bsd-license.php
  8. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  10. **/
  11. #include <PiDxe.h>
  12. #include <Library/ArmLib.h>
  13. #include <Library/BaseLib.h>
  14. #include <Library/DebugLib.h>
  15. #include <Library/BaseMemoryLib.h>
  16. #include <Library/UefiBootServicesTableLib.h>
  17. #include <Library/UefiLib.h>
  18. #include <Library/PcdLib.h>
  19. #include <Library/IoLib.h>
  20. #include <Library/ArmArchTimerLib.h>
  21. #include <Protocol/Timer.h>
  22. #include <Protocol/HardwareInterrupt.h>
  23. // The notification function to call on every timer interrupt.
  24. EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL;
  25. EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)NULL;
  26. // The current period of the timer interrupt
  27. UINT64 mTimerPeriod = 0;
  28. // Cached copy of the Hardware Interrupt protocol instance
  29. EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;
  30. /**
  31. This function registers the handler NotifyFunction so it is called every time
  32. the timer interrupt fires. It also passes the amount of time since the last
  33. handler call to the NotifyFunction. If NotifyFunction is NULL, then the
  34. handler is unregistered. If the handler is registered, then EFI_SUCCESS is
  35. returned. If the CPU does not support registering a timer interrupt handler,
  36. then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
  37. when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  38. If an attempt is made to unregister a handler when a handler is not registered,
  39. then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
  40. register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
  41. is returned.
  42. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  43. @param NotifyFunction The function to call when a timer interrupt fires. This
  44. function executes at TPL_HIGH_LEVEL. The DXE Core will
  45. register a handler for the timer interrupt, so it can know
  46. how much time has passed. This information is used to
  47. signal timer based events. NULL will unregister the handler.
  48. @retval EFI_SUCCESS The timer handler was registered.
  49. @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
  50. @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
  51. registered.
  52. @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
  53. previously registered.
  54. @retval EFI_DEVICE_ERROR The timer handler could not be registered.
  55. **/
  56. EFI_STATUS
  57. EFIAPI
  58. TimerDriverRegisterHandler (
  59. IN EFI_TIMER_ARCH_PROTOCOL *This,
  60. IN EFI_TIMER_NOTIFY NotifyFunction
  61. )
  62. {
  63. if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
  64. return EFI_INVALID_PARAMETER;
  65. }
  66. if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
  67. return EFI_ALREADY_STARTED;
  68. }
  69. mTimerNotifyFunction = NotifyFunction;
  70. return EFI_SUCCESS;
  71. }
  72. /**
  73. Disable the timer
  74. **/
  75. VOID
  76. EFIAPI
  77. ExitBootServicesEvent (
  78. IN EFI_EVENT Event,
  79. IN VOID *Context
  80. )
  81. {
  82. ArmArchTimerDisableTimer ();
  83. }
  84. /**
  85. This function adjusts the period of timer interrupts to the value specified
  86. by TimerPeriod. If the timer period is updated, then the selected timer
  87. period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
  88. the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
  89. If an error occurs while attempting to update the timer period, then the
  90. timer hardware will be put back in its state prior to this call, and
  91. EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
  92. is disabled. This is not the same as disabling the CPU's interrupts.
  93. Instead, it must either turn off the timer hardware, or it must adjust the
  94. interrupt controller so that a CPU interrupt is not generated when the timer
  95. interrupt fires.
  96. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  97. @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
  98. the timer hardware is not programmable, then EFI_UNSUPPORTED is
  99. returned. If the timer is programmable, then the timer period
  100. will be rounded up to the nearest timer period that is supported
  101. by the timer hardware. If TimerPeriod is set to 0, then the
  102. timer interrupts will be disabled.
  103. @retval EFI_SUCCESS The timer period was changed.
  104. @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
  105. @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
  106. **/
  107. EFI_STATUS
  108. EFIAPI
  109. TimerDriverSetTimerPeriod (
  110. IN EFI_TIMER_ARCH_PROTOCOL *This,
  111. IN UINT64 TimerPeriod
  112. )
  113. {
  114. UINT64 TimerTicks;
  115. // Always disable the timer
  116. ArmArchTimerDisableTimer ();
  117. if (TimerPeriod != 0) {
  118. // Convert TimerPeriod to micro sec units
  119. TimerTicks = DivU64x32 (TimerPeriod, 10);
  120. TimerTicks = MultU64x32 (TimerTicks, (PcdGet32(PcdArmArchTimerFreqInHz)/1000000));
  121. ArmArchTimerSetTimerVal((UINTN)TimerTicks);
  122. // Enable the timer
  123. ArmArchTimerEnableTimer ();
  124. }
  125. // Save the new timer period
  126. mTimerPeriod = TimerPeriod;
  127. return EFI_SUCCESS;
  128. }
  129. /**
  130. This function retrieves the period of timer interrupts in 100 ns units,
  131. returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
  132. is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
  133. returned, then the timer is currently disabled.
  134. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  135. @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
  136. 0 is returned, then the timer is currently disabled.
  137. @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
  138. @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
  139. **/
  140. EFI_STATUS
  141. EFIAPI
  142. TimerDriverGetTimerPeriod (
  143. IN EFI_TIMER_ARCH_PROTOCOL *This,
  144. OUT UINT64 *TimerPeriod
  145. )
  146. {
  147. if (TimerPeriod == NULL) {
  148. return EFI_INVALID_PARAMETER;
  149. }
  150. *TimerPeriod = mTimerPeriod;
  151. return EFI_SUCCESS;
  152. }
  153. /**
  154. This function generates a soft timer interrupt. If the platform does not support soft
  155. timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
  156. If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
  157. service, then a soft timer interrupt will be generated. If the timer interrupt is
  158. enabled when this service is called, then the registered handler will be invoked. The
  159. registered handler should not be able to distinguish a hardware-generated timer
  160. interrupt from a software-generated timer interrupt.
  161. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  162. @retval EFI_SUCCESS The soft timer interrupt was generated.
  163. @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
  164. **/
  165. EFI_STATUS
  166. EFIAPI
  167. TimerDriverGenerateSoftInterrupt (
  168. IN EFI_TIMER_ARCH_PROTOCOL *This
  169. )
  170. {
  171. return EFI_UNSUPPORTED;
  172. }
  173. /**
  174. Interface structure for the Timer Architectural Protocol.
  175. @par Protocol Description:
  176. This protocol provides the services to initialize a periodic timer
  177. interrupt, and to register a handler that is called each time the timer
  178. interrupt fires. It may also provide a service to adjust the rate of the
  179. periodic timer interrupt. When a timer interrupt occurs, the handler is
  180. passed the amount of time that has passed since the previous timer
  181. interrupt.
  182. @param RegisterHandler
  183. Registers a handler that will be called each time the
  184. timer interrupt fires. TimerPeriod defines the minimum
  185. time between timer interrupts, so TimerPeriod will also
  186. be the minimum time between calls to the registered
  187. handler.
  188. @param SetTimerPeriod
  189. Sets the period of the timer interrupt in 100 nS units.
  190. This function is optional, and may return EFI_UNSUPPORTED.
  191. If this function is supported, then the timer period will
  192. be rounded up to the nearest supported timer period.
  193. @param GetTimerPeriod
  194. Retrieves the period of the timer interrupt in 100 nS units.
  195. @param GenerateSoftInterrupt
  196. Generates a soft timer interrupt that simulates the firing of
  197. the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
  198. a period of time.
  199. **/
  200. EFI_TIMER_ARCH_PROTOCOL gTimer = {
  201. TimerDriverRegisterHandler,
  202. TimerDriverSetTimerPeriod,
  203. TimerDriverGetTimerPeriod,
  204. TimerDriverGenerateSoftInterrupt
  205. };
  206. /**
  207. C Interrupt Handler called in the interrupt context when Source interrupt is active.
  208. @param Source Source of the interrupt. Hardware routing off a specific platform defines
  209. what source means.
  210. @param SystemContext Pointer to system register context. Mostly used by debuggers and will
  211. update the system context after the return from the interrupt if
  212. modified. Don't change these values unless you know what you are doing
  213. **/
  214. VOID
  215. EFIAPI
  216. TimerInterruptHandler (
  217. IN HARDWARE_INTERRUPT_SOURCE Source,
  218. IN EFI_SYSTEM_CONTEXT SystemContext
  219. )
  220. {
  221. EFI_TPL OriginalTPL;
  222. //
  223. // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
  224. // that raise to TPL_HIGH and then restore back to current level. Thus we need
  225. // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
  226. //
  227. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  228. // Check if the timer interrupt is active
  229. if ((ArmArchTimerGetTimerCtrlReg () ) & ARM_ARCH_TIMER_ISTATUS) {
  230. // Signal end of interrupt early to help avoid losing subsequent ticks from long duration handlers
  231. gInterrupt->EndOfInterrupt (gInterrupt, Source);
  232. if (mTimerNotifyFunction) {
  233. mTimerNotifyFunction (mTimerPeriod);
  234. }
  235. // Reload the Timer
  236. TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod));
  237. }
  238. // Enable timer interrupts
  239. gInterrupt->EnableInterruptSource (gInterrupt, Source);
  240. gBS->RestoreTPL (OriginalTPL);
  241. }
  242. /**
  243. Initialize the state information for the Timer Architectural Protocol and
  244. the Timer Debug support protocol that allows the debugger to break into a
  245. running program.
  246. @param ImageHandle of the loaded driver
  247. @param SystemTable Pointer to the System Table
  248. @retval EFI_SUCCESS Protocol registered
  249. @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
  250. @retval EFI_DEVICE_ERROR Hardware problems
  251. **/
  252. EFI_STATUS
  253. EFIAPI
  254. TimerInitialize (
  255. IN EFI_HANDLE ImageHandle,
  256. IN EFI_SYSTEM_TABLE *SystemTable
  257. )
  258. {
  259. EFI_HANDLE Handle = NULL;
  260. EFI_STATUS Status;
  261. UINTN TimerCtrlReg;
  262. if (ArmIsArchTimerImplemented () == 0) {
  263. DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, hence cann't use this Driver \n"));
  264. ASSERT (0);
  265. }
  266. // Find the interrupt controller protocol. ASSERT if not found.
  267. Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt);
  268. ASSERT_EFI_ERROR (Status);
  269. // Disable the timer
  270. TimerCtrlReg = ArmArchTimerGetTimerCtrlReg ();
  271. TimerCtrlReg |= ARM_ARCH_TIMER_IMASK;
  272. TimerCtrlReg &= ~ARM_ARCH_TIMER_ENABLE;
  273. ArmArchTimerSetTimerCtrlReg (TimerCtrlReg);
  274. Status = TimerDriverSetTimerPeriod (&gTimer, 0);
  275. ASSERT_EFI_ERROR (Status);
  276. // Install secure and Non-secure interrupt handlers
  277. // Note: Because it is not possible to determine the security state of the
  278. // CPU dynamically, we just install interrupt handler for both sec and non-sec
  279. // timer PPI
  280. Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerSecIntrNum), TimerInterruptHandler);
  281. ASSERT_EFI_ERROR (Status);
  282. Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerIntrNum), TimerInterruptHandler);
  283. ASSERT_EFI_ERROR (Status);
  284. // Set up default timer
  285. Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod)); // TIMER_DEFAULT_PERIOD
  286. ASSERT_EFI_ERROR (Status);
  287. // Install the Timer Architectural Protocol onto a new handle
  288. Status = gBS->InstallMultipleProtocolInterfaces(
  289. &Handle,
  290. &gEfiTimerArchProtocolGuid, &gTimer,
  291. NULL
  292. );
  293. ASSERT_EFI_ERROR(Status);
  294. // Everything is ready, unmask and enable timer interrupts
  295. TimerCtrlReg = ARM_ARCH_TIMER_ENABLE;
  296. ArmArchTimerSetTimerCtrlReg (TimerCtrlReg);
  297. // Register for an ExitBootServicesEvent
  298. Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, ExitBootServicesEvent, NULL, &EfiExitBootServicesEvent);
  299. ASSERT_EFI_ERROR (Status);
  300. return Status;
  301. }