Timer.c 12 KB

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