Metronome.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 not exceed 200 uS. The value in this field
  36. is a constant that must not be modified after the Metronome architectural
  37. protocol is installed. All consumers must treat this as a read-only field.
  38. **/
  39. EFI_METRONOME_ARCH_PROTOCOL gMetronome = {
  40. WaitForTick,
  41. FixedPcdGet32 (PcdMetronomeTickPeriod)
  42. };
  43. /**
  44. The WaitForTick() function waits for the number of ticks specified by
  45. TickNumber from a known time source in the platform. If TickNumber of
  46. ticks are detected, then EFI_SUCCESS is returned. The actual time passed
  47. between entry of this function and the first tick is between 0 and
  48. TickPeriod 100 nS units. If you want to guarantee that at least TickPeriod
  49. time has elapsed, wait for two ticks. This function waits for a hardware
  50. event to determine when a tick occurs. It is possible for interrupt
  51. processing, or exception processing to interrupt the execution of the
  52. WaitForTick() function. Depending on the hardware source for the ticks, it
  53. is possible for a tick to be missed. This function cannot guarantee that
  54. ticks will not be missed. If a timeout occurs waiting for the specified
  55. number of ticks, then EFI_TIMEOUT is returned.
  56. @param This The EFI_METRONOME_ARCH_PROTOCOL instance.
  57. @param TickNumber Number of ticks to wait.
  58. @retval EFI_SUCCESS The wait for the number of ticks specified by TickNumber
  59. succeeded.
  60. @retval EFI_TIMEOUT A timeout occurred waiting for the specified number of ticks.
  61. **/
  62. EFI_STATUS
  63. EFIAPI
  64. WaitForTick (
  65. IN EFI_METRONOME_ARCH_PROTOCOL *This,
  66. IN UINT32 TickNumber
  67. )
  68. {
  69. //
  70. // Compute how long to stall the CPU.
  71. // gMetronome.TickPeriod is in 100 ns units so it needs to be divided by 10
  72. // to get it in microseconds units.
  73. //
  74. MicroSecondDelay (TickNumber * gMetronome.TickPeriod / 10);
  75. return EFI_SUCCESS;
  76. }
  77. EFI_HANDLE gMetronomeHandle = NULL;
  78. /**
  79. Initialize the state information for the CPU Architectural Protocol
  80. @param ImageHandle of the loaded driver
  81. @param SystemTable Pointer to the System Table
  82. @retval EFI_SUCCESS Protocol registered
  83. @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
  84. @retval EFI_DEVICE_ERROR Hardware problems
  85. **/
  86. EFI_STATUS
  87. EFIAPI
  88. MetronomeInitialize (
  89. IN EFI_HANDLE ImageHandle,
  90. IN EFI_SYSTEM_TABLE *SystemTable
  91. )
  92. {
  93. EFI_STATUS Status;
  94. //
  95. // Do any hardware init required to make WaitForTick () to work here.
  96. //
  97. Status = gBS->InstallMultipleProtocolInterfaces (
  98. &gMetronomeHandle,
  99. &gEfiMetronomeArchProtocolGuid,
  100. &gMetronome,
  101. NULL
  102. );
  103. ASSERT_EFI_ERROR (Status);
  104. return Status;
  105. }