1
0

timer.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. timer.c
  5. Abstract:
  6. This module implements platform timer services for BIOS machines.
  7. Author:
  8. Evan Green 3-Mar-2014
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/lib/types.h>
  16. #include <minoca/fw/acpitabs.h>
  17. #include <uefifw.h>
  18. #include "biosfw.h"
  19. //
  20. // --------------------------------------------------------------------- Macros
  21. //
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. //
  29. // ----------------------------------------------- Internal Function Prototypes
  30. //
  31. UINT64
  32. EfipPlatformReadTimer (
  33. VOID
  34. );
  35. //
  36. // -------------------------------------------------------------------- Globals
  37. //
  38. //
  39. // Hold on to the location of the PM timer.
  40. //
  41. UINT16 EfiPmTimerPort;
  42. UINT32 EfiPmTimerBitWidth;
  43. //
  44. // ------------------------------------------------------------------ Functions
  45. //
  46. EFIAPI
  47. EFI_STATUS
  48. EfiPlatformSetWatchdogTimer (
  49. UINTN Timeout,
  50. UINT64 WatchdogCode,
  51. UINTN DataSize,
  52. CHAR16 *WatchdogData
  53. )
  54. /*++
  55. Routine Description:
  56. This routine sets the system's watchdog timer.
  57. Arguments:
  58. Timeout - Supplies the number of seconds to set the timer for.
  59. WatchdogCode - Supplies a numeric code to log on a watchdog timeout event.
  60. DataSize - Supplies the size of the watchdog data.
  61. WatchdogData - Supplies an optional buffer that includes a null-terminated
  62. string, optionally followed by additional binary data.
  63. Return Value:
  64. EFI_SUCCESS on success.
  65. EFI_INVALID_PARAMETER if the supplied watchdog code is invalid.
  66. EFI_UNSUPPORTED if there is no watchdog timer.
  67. EFI_DEVICE_ERROR if an error occurred accessing the device hardware.
  68. --*/
  69. {
  70. return EFI_UNSUPPORTED;
  71. }
  72. EFI_STATUS
  73. EfiPlatformInitializeTimers (
  74. UINT32 *ClockTimerInterruptNumber,
  75. EFI_PLATFORM_SERVICE_TIMER_INTERRUPT *ClockTimerServiceRoutine,
  76. EFI_PLATFORM_READ_TIMER *ReadTimerRoutine,
  77. UINT64 *ReadTimerFrequency,
  78. UINT32 *ReadTimerWidth
  79. )
  80. /*++
  81. Routine Description:
  82. This routine initializes platform timer services. There are actually two
  83. different timer services returned in this routine. The periodic timer tick
  84. provides a periodic interrupt. The read timer provides a free running
  85. counter value. These are likely serviced by different timers. For the
  86. periodic timer tick, this routine should start the periodic interrupts
  87. coming in. The periodic rate of the timer can be anything reasonable, as
  88. the time counter will be used to count actual duration. The rate should be
  89. greater than twice the rollover rate of the time counter to ensure proper
  90. time accounting. Interrupts are disabled at the processor core for the
  91. duration of this routine.
  92. Arguments:
  93. ClockTimerInterruptNumber - Supplies a pointer where the interrupt line
  94. number of the periodic timer tick will be returned.
  95. ClockTimerServiceRoutine - Supplies a pointer where a pointer to a routine
  96. called when the periodic timer tick interrupt occurs will be returned.
  97. ReadTimerRoutine - Supplies a pointer where a pointer to a routine
  98. called to read the current timer value will be returned.
  99. ReadTimerFrequency - Supplies the frequency of the counter.
  100. ReadTimerWidth - Supplies a pointer where the read timer bit width will be
  101. returned.
  102. Return Value:
  103. EFI Status code.
  104. --*/
  105. {
  106. PFADT Fadt;
  107. PRSDP Rsdp;
  108. PRSDT Rsdt;
  109. ULONG RsdtTableCount;
  110. PULONG RsdtTableEntry;
  111. UINTN TableIndex;
  112. //
  113. // Find the FADT to figure out where the PM timer is.
  114. //
  115. Rsdp = EfiRsdpPointer;
  116. if (Rsdp == NULL) {
  117. return EFI_UNSUPPORTED;
  118. }
  119. Rsdt = (PRSDT)(UINTN)(Rsdp->RsdtAddress);
  120. RsdtTableCount = (Rsdt->Header.Length - sizeof(DESCRIPTION_HEADER)) /
  121. sizeof(ULONG);
  122. if (RsdtTableCount == 0) {
  123. return EFI_NOT_FOUND;
  124. }
  125. RsdtTableEntry = (PULONG)&(Rsdt->Entries);
  126. for (TableIndex = 0; TableIndex < RsdtTableCount; TableIndex += 1) {
  127. Fadt = (PFADT)RsdtTableEntry[TableIndex];
  128. if (Fadt->Header.Signature == FADT_SIGNATURE) {
  129. break;
  130. }
  131. }
  132. if (TableIndex == RsdtTableCount) {
  133. return EFI_NOT_FOUND;
  134. }
  135. //
  136. // Grab the PM timer port and width out of the FADT table.
  137. //
  138. EfiPmTimerPort = Fadt->PmTimerBlock;
  139. if (EfiPmTimerPort == 0) {
  140. return EFI_UNSUPPORTED;
  141. }
  142. EfiPmTimerBitWidth = 24;
  143. if ((Fadt->Flags & FADT_FLAG_PM_TIMER_32_BITS) != 0) {
  144. EfiPmTimerBitWidth = 32;
  145. }
  146. //
  147. // Clock interrupts are not supported as the BIOS may have 16-bit real mode
  148. // interrupts coming in.
  149. //
  150. *ClockTimerInterruptNumber = 0;
  151. *ClockTimerServiceRoutine = NULL;
  152. *ReadTimerRoutine = EfipPlatformReadTimer;
  153. *ReadTimerFrequency = PM_TIMER_FREQUENCY;
  154. *ReadTimerWidth = EfiPmTimerBitWidth;
  155. return EFI_SUCCESS;
  156. }
  157. VOID
  158. EfiPlatformTerminateTimers (
  159. VOID
  160. )
  161. /*++
  162. Routine Description:
  163. This routine terminates timer services in preparation for the termination
  164. of boot services.
  165. Arguments:
  166. None.
  167. Return Value:
  168. None.
  169. --*/
  170. {
  171. return;
  172. }
  173. //
  174. // --------------------------------------------------------- Internal Functions
  175. //
  176. UINT64
  177. EfipPlatformReadTimer (
  178. VOID
  179. )
  180. /*++
  181. Routine Description:
  182. This routine is called to read the current platform time value. The timer
  183. is assumed to be free running at a constant frequency, and should have a
  184. bit width as reported in the initialize function. The UEFI core will
  185. manage software bit extension out to 64 bits, this routine should just
  186. reporte the hardware timer value.
  187. Arguments:
  188. None.
  189. Return Value:
  190. Returns the hardware timer value.
  191. --*/
  192. {
  193. return EfiIoPortIn32(EfiPmTimerPort);
  194. }