Metronome.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /** @file
  2. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  3. Copyright (c) 2013, ARM Ltd. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/UefiBootServicesTableLib.h>
  11. #include <Library/UefiLib.h>
  12. #include <Library/PcdLib.h>
  13. #include <Library/TimerLib.h>
  14. #include <Protocol/Metronome.h>
  15. EFI_STATUS
  16. EFIAPI
  17. WaitForTick (
  18. IN EFI_METRONOME_ARCH_PROTOCOL *This,
  19. IN UINT32 TickNumber
  20. );
  21. /**
  22. Interface structure for the Metronome Architectural Protocol.
  23. @par Protocol Description:
  24. This protocol provides access to a known time source in the platform to the
  25. core. The core uses this known time source to produce core services that
  26. require calibrated delays.
  27. @param WaitForTick
  28. Waits for a specified number of ticks from a known time source
  29. in the platform. The actual time passed between entry of this
  30. function and the first tick is between 0 and TickPeriod 100 nS
  31. units. If you want to guarantee that at least TickPeriod time
  32. has elapsed, wait for two ticks.
  33. @param TickPeriod
  34. The period of platform's known time source in 100 nS units.
  35. This value on any platform must be at least 10 uS, and must not
  36. exceed 200 uS. The value in this field is a constant that must
  37. not be modified after the Metronome architectural protocol is
  38. installed. All consumers must treat this as a read-only field.
  39. **/
  40. EFI_METRONOME_ARCH_PROTOCOL gMetronome = {
  41. WaitForTick,
  42. FixedPcdGet32 (PcdMetronomeTickPeriod)
  43. };
  44. /**
  45. The WaitForTick() function waits for the number of ticks specified by
  46. TickNumber from a known time source in the platform. If TickNumber of
  47. ticks are detected, then EFI_SUCCESS is returned. The actual time passed
  48. between entry of this function and the first tick is between 0 and
  49. TickPeriod 100 nS units. If you want to guarantee that at least TickPeriod
  50. time has elapsed, wait for two ticks. This function waits for a hardware
  51. event to determine when a tick occurs. It is possible for interrupt
  52. processing, or exception processing to interrupt the execution of the
  53. WaitForTick() function. Depending on the hardware source for the ticks, it
  54. is possible for a tick to be missed. This function cannot guarantee that
  55. ticks will not be missed. If a timeout occurs waiting for the specified
  56. number of ticks, then EFI_TIMEOUT is returned.
  57. @param This The EFI_METRONOME_ARCH_PROTOCOL instance.
  58. @param TickNumber Number of ticks to wait.
  59. @retval EFI_SUCCESS The wait for the number of ticks specified by TickNumber
  60. succeeded.
  61. @retval EFI_TIMEOUT A timeout occurred waiting for the specified number of ticks.
  62. **/
  63. EFI_STATUS
  64. EFIAPI
  65. WaitForTick (
  66. IN EFI_METRONOME_ARCH_PROTOCOL *This,
  67. IN UINT32 TickNumber
  68. )
  69. {
  70. //
  71. // Compute how long to stall the CPU.
  72. // gMetronome.TickPeriod is in 100 ns units so it needs to be divided by 10
  73. // to get it in microseconds units.
  74. //
  75. MicroSecondDelay (TickNumber * gMetronome.TickPeriod / 10);
  76. return EFI_SUCCESS;
  77. }
  78. EFI_HANDLE gMetronomeHandle = NULL;
  79. /**
  80. Initialize the state information for the CPU Architectural Protocol
  81. @param ImageHandle of the loaded driver
  82. @param SystemTable Pointer to the System Table
  83. @retval EFI_SUCCESS Protocol registered
  84. @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
  85. @retval EFI_DEVICE_ERROR Hardware problems
  86. **/
  87. EFI_STATUS
  88. EFIAPI
  89. MetronomeInitialize (
  90. IN EFI_HANDLE ImageHandle,
  91. IN EFI_SYSTEM_TABLE *SystemTable
  92. )
  93. {
  94. EFI_STATUS Status;
  95. //
  96. // Do any hardware init required to make WaitForTick () to work here.
  97. //
  98. Status = gBS->InstallMultipleProtocolInterfaces (
  99. &gMetronomeHandle,
  100. &gEfiMetronomeArchProtocolGuid,
  101. &gMetronome,
  102. NULL
  103. );
  104. ASSERT_EFI_ERROR (Status);
  105. return Status;
  106. }