Timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /** @file
  2. Timer Architectural Protocol as defined in the DXE CIS
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/NestedInterruptTplLib.h>
  7. #include "Timer.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. // Pointer to the Legacy 8259 Protocol instance
  27. //
  28. EFI_LEGACY_8259_PROTOCOL *mLegacy8259;
  29. //
  30. // The notification function to call on every timer interrupt.
  31. // A bug in the compiler prevents us from initializing this here.
  32. //
  33. EFI_TIMER_NOTIFY mTimerNotifyFunction;
  34. //
  35. // The current period of the timer interrupt
  36. //
  37. volatile UINT64 mTimerPeriod = 0;
  38. //
  39. // Worker Functions
  40. //
  41. /**
  42. Sets the counter value for Timer #0 in a legacy 8254 timer.
  43. @param Count The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.
  44. **/
  45. VOID
  46. SetPitCount (
  47. IN UINT16 Count
  48. )
  49. {
  50. IoWrite8 (TIMER_CONTROL_PORT, 0x36);
  51. IoWrite8 (TIMER0_COUNT_PORT, (UINT8)(Count & 0xff));
  52. IoWrite8 (TIMER0_COUNT_PORT, (UINT8)((Count >> 8) & 0xff));
  53. }
  54. /**
  55. 8254 Timer #0 Interrupt Handler.
  56. @param InterruptType The type of interrupt that occurred
  57. @param SystemContext A pointer to the system context when the interrupt occurred
  58. **/
  59. VOID
  60. EFIAPI
  61. TimerInterruptHandler (
  62. IN EFI_EXCEPTION_TYPE InterruptType,
  63. IN EFI_SYSTEM_CONTEXT SystemContext
  64. )
  65. {
  66. STATIC NESTED_INTERRUPT_STATE NestedInterruptState;
  67. EFI_TPL OriginalTPL;
  68. OriginalTPL = NestedInterruptRaiseTPL ();
  69. mLegacy8259->EndOfInterrupt (mLegacy8259, Efi8259Irq0);
  70. if (mTimerNotifyFunction != NULL) {
  71. //
  72. // @bug : This does not handle missed timer interrupts
  73. //
  74. mTimerNotifyFunction (mTimerPeriod);
  75. }
  76. NestedInterruptRestoreTPL (OriginalTPL, SystemContext, &NestedInterruptState);
  77. }
  78. /**
  79. This function registers the handler NotifyFunction so it is called every time
  80. the timer interrupt fires. It also passes the amount of time since the last
  81. handler call to the NotifyFunction. If NotifyFunction is NULL, then the
  82. handler is unregistered. If the handler is registered, then EFI_SUCCESS is
  83. returned. If the CPU does not support registering a timer interrupt handler,
  84. then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
  85. when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  86. If an attempt is made to unregister a handler when a handler is not registered,
  87. then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
  88. register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
  89. is returned.
  90. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  91. @param NotifyFunction The function to call when a timer interrupt fires. This
  92. function executes at TPL_HIGH_LEVEL. The DXE Core will
  93. register a handler for the timer interrupt, so it can know
  94. how much time has passed. This information is used to
  95. signal timer based events. NULL will unregister the handler.
  96. @retval EFI_SUCCESS The timer handler was registered.
  97. @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
  98. @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
  99. registered.
  100. @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
  101. previously registered.
  102. @retval EFI_DEVICE_ERROR The timer handler could not be registered.
  103. **/
  104. EFI_STATUS
  105. EFIAPI
  106. TimerDriverRegisterHandler (
  107. IN EFI_TIMER_ARCH_PROTOCOL *This,
  108. IN EFI_TIMER_NOTIFY NotifyFunction
  109. )
  110. {
  111. //
  112. // Check for invalid parameters
  113. //
  114. if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
  115. return EFI_INVALID_PARAMETER;
  116. }
  117. if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
  118. return EFI_ALREADY_STARTED;
  119. }
  120. mTimerNotifyFunction = NotifyFunction;
  121. return EFI_SUCCESS;
  122. }
  123. /**
  124. This function adjusts the period of timer interrupts to the value specified
  125. by TimerPeriod. If the timer period is updated, then the selected timer
  126. period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
  127. the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
  128. If an error occurs while attempting to update the timer period, then the
  129. timer hardware will be put back in its state prior to this call, and
  130. EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
  131. is disabled. This is not the same as disabling the CPU's interrupts.
  132. Instead, it must either turn off the timer hardware, or it must adjust the
  133. interrupt controller so that a CPU interrupt is not generated when the timer
  134. interrupt fires.
  135. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  136. @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
  137. the timer hardware is not programmable, then EFI_UNSUPPORTED is
  138. returned. If the timer is programmable, then the timer period
  139. will be rounded up to the nearest timer period that is supported
  140. by the timer hardware. If TimerPeriod is set to 0, then the
  141. timer interrupts will be disabled.
  142. @retval EFI_SUCCESS The timer period was changed.
  143. @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
  144. @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
  145. **/
  146. EFI_STATUS
  147. EFIAPI
  148. TimerDriverSetTimerPeriod (
  149. IN EFI_TIMER_ARCH_PROTOCOL *This,
  150. IN UINT64 TimerPeriod
  151. )
  152. {
  153. UINT64 TimerCount;
  154. //
  155. // The basic clock is 1.19318 MHz or 0.119318 ticks per 100 ns.
  156. // TimerPeriod * 0.119318 = 8254 timer divisor. Using integer arithmetic
  157. // TimerCount = (TimerPeriod * 119318)/1000000.
  158. //
  159. // Round up to next highest integer. This guarantees that the timer is
  160. // equal to or slightly longer than the requested time.
  161. // TimerCount = ((TimerPeriod * 119318) + 500000)/1000000
  162. //
  163. // Note that a TimerCount of 0 is equivalent to a count of 65,536
  164. //
  165. // Since TimerCount is limited to 16 bits for IA32, TimerPeriod is limited
  166. // to 20 bits.
  167. //
  168. if (TimerPeriod == 0) {
  169. //
  170. // Disable timer interrupt for a TimerPeriod of 0
  171. //
  172. mLegacy8259->DisableIrq (mLegacy8259, Efi8259Irq0);
  173. } else {
  174. //
  175. // Convert TimerPeriod into 8254 counts
  176. //
  177. TimerCount = DivU64x32 (MultU64x32 (119318, (UINT32)TimerPeriod) + 500000, 1000000);
  178. //
  179. // Check for overflow
  180. //
  181. if (TimerCount >= 65536) {
  182. TimerCount = 0;
  183. TimerPeriod = MAX_TIMER_TICK_DURATION;
  184. }
  185. //
  186. // Program the 8254 timer with the new count value
  187. //
  188. SetPitCount ((UINT16)TimerCount);
  189. //
  190. // Enable timer interrupt
  191. //
  192. mLegacy8259->EnableIrq (mLegacy8259, Efi8259Irq0, FALSE);
  193. }
  194. //
  195. // Save the new timer period
  196. //
  197. mTimerPeriod = TimerPeriod;
  198. return EFI_SUCCESS;
  199. }
  200. /**
  201. This function retrieves the period of timer interrupts in 100 ns units,
  202. returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
  203. is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
  204. returned, then the timer is currently disabled.
  205. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  206. @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
  207. 0 is returned, then the timer is currently disabled.
  208. @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
  209. @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
  210. **/
  211. EFI_STATUS
  212. EFIAPI
  213. TimerDriverGetTimerPeriod (
  214. IN EFI_TIMER_ARCH_PROTOCOL *This,
  215. OUT UINT64 *TimerPeriod
  216. )
  217. {
  218. if (TimerPeriod == NULL) {
  219. return EFI_INVALID_PARAMETER;
  220. }
  221. *TimerPeriod = mTimerPeriod;
  222. return EFI_SUCCESS;
  223. }
  224. /**
  225. This function generates a soft timer interrupt. If the platform does not support soft
  226. timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
  227. If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
  228. service, then a soft timer interrupt will be generated. If the timer interrupt is
  229. enabled when this service is called, then the registered handler will be invoked. The
  230. registered handler should not be able to distinguish a hardware-generated timer
  231. interrupt from a software-generated timer interrupt.
  232. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  233. @retval EFI_SUCCESS The soft timer interrupt was generated.
  234. @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
  235. **/
  236. EFI_STATUS
  237. EFIAPI
  238. TimerDriverGenerateSoftInterrupt (
  239. IN EFI_TIMER_ARCH_PROTOCOL *This
  240. )
  241. {
  242. EFI_STATUS Status;
  243. UINT16 IRQMask;
  244. EFI_TPL OriginalTPL;
  245. //
  246. // If the timer interrupt is enabled, then the registered handler will be invoked.
  247. //
  248. Status = mLegacy8259->GetMask (mLegacy8259, NULL, NULL, &IRQMask, NULL);
  249. ASSERT_EFI_ERROR (Status);
  250. if ((IRQMask & 0x1) == 0) {
  251. //
  252. // Invoke the registered handler
  253. //
  254. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  255. if (mTimerNotifyFunction != NULL) {
  256. //
  257. // @bug : This does not handle missed timer interrupts
  258. //
  259. mTimerNotifyFunction (mTimerPeriod);
  260. }
  261. gBS->RestoreTPL (OriginalTPL);
  262. } else {
  263. return EFI_UNSUPPORTED;
  264. }
  265. return EFI_SUCCESS;
  266. }
  267. /**
  268. Initialize the Timer Architectural Protocol driver
  269. @param ImageHandle ImageHandle of the loaded driver
  270. @param SystemTable Pointer to the System Table
  271. @retval EFI_SUCCESS Timer Architectural Protocol created
  272. @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
  273. @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
  274. **/
  275. EFI_STATUS
  276. EFIAPI
  277. TimerDriverInitialize (
  278. IN EFI_HANDLE ImageHandle,
  279. IN EFI_SYSTEM_TABLE *SystemTable
  280. )
  281. {
  282. EFI_STATUS Status;
  283. UINT32 TimerVector;
  284. //
  285. // Initialize the pointer to our notify function.
  286. //
  287. mTimerNotifyFunction = NULL;
  288. //
  289. // Make sure the Timer Architectural Protocol is not already installed in the system
  290. //
  291. ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);
  292. //
  293. // Find the CPU architectural protocol.
  294. //
  295. Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);
  296. ASSERT_EFI_ERROR (Status);
  297. //
  298. // Find the Legacy8259 protocol.
  299. //
  300. Status = gBS->LocateProtocol (&gEfiLegacy8259ProtocolGuid, NULL, (VOID **)&mLegacy8259);
  301. ASSERT_EFI_ERROR (Status);
  302. //
  303. // Force the timer to be disabled
  304. //
  305. Status = TimerDriverSetTimerPeriod (&mTimer, 0);
  306. ASSERT_EFI_ERROR (Status);
  307. //
  308. // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver
  309. //
  310. TimerVector = 0;
  311. Status = mLegacy8259->GetVector (mLegacy8259, Efi8259Irq0, (UINT8 *)&TimerVector);
  312. ASSERT_EFI_ERROR (Status);
  313. //
  314. // Install interrupt handler for 8254 Timer #0 (ISA IRQ0)
  315. //
  316. Status = mCpu->RegisterInterruptHandler (mCpu, TimerVector, TimerInterruptHandler);
  317. ASSERT_EFI_ERROR (Status);
  318. //
  319. // Force the timer to be enabled at its default period
  320. //
  321. Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);
  322. ASSERT_EFI_ERROR (Status);
  323. //
  324. // Install the Timer Architectural Protocol onto a new handle
  325. //
  326. Status = gBS->InstallMultipleProtocolInterfaces (
  327. &mTimerHandle,
  328. &gEfiTimerArchProtocolGuid,
  329. &mTimer,
  330. NULL
  331. );
  332. ASSERT_EFI_ERROR (Status);
  333. return Status;
  334. }