Timer.c 12 KB

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