2 Implementation of Timestamp Protocol using UEFI APIs.
4 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/DebugLib.h>
11 #include <Library/UefiDriverEntryPoint.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/TimerLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Protocol/Timestamp.h>
18 // The StartValue in TimerLib
20 UINT64 mTimerLibStartValue
= 0;
23 // The EndValue in TimerLib
25 UINT64 mTimerLibEndValue
= 0;
28 // The properties of timestamp
30 EFI_TIMESTAMP_PROPERTIES mTimestampProperties
= {
36 Retrieves the current value of a 64-bit free running timestamp counter.
38 The counter shall count up in proportion to the amount of time that has passed. The counter value
39 will always roll over to zero. The properties of the counter can be retrieved from GetProperties().
40 The caller should be prepared for the function to return the same value twice across successive calls.
41 The counter value will not go backwards other than when wrapping, as defined by EndValue in GetProperties().
42 The frequency of the returned timestamp counter value must remain constant. Power management operations that
43 affect clocking must not change the returned counter frequency. The quantization of counter value updates may
44 vary as long as the value reflecting time passed remains consistent.
46 @retval The current value of the free running timestamp counter.
51 TimestampDriverGetTimestamp (
56 // The timestamp of Timestamp Protocol
58 UINT64 TimestampValue
;
65 if (mTimerLibStartValue
> mTimerLibEndValue
) {
66 TimestampValue
= mTimerLibStartValue
- GetPerformanceCounter ();
68 TimestampValue
= GetPerformanceCounter () - mTimerLibStartValue
;
71 return TimestampValue
;
75 Obtains timestamp counter properties including frequency and value limits.
77 @param[out] Properties The properties of the timestamp counter.
79 @retval EFI_SUCCESS The properties were successfully retrieved.
80 @retval EFI_DEVICE_ERROR An error occurred trying to retrieve the properties of the timestamp
81 counter subsystem. Properties is not pedated.
82 @retval EFI_INVALID_PARAMETER Properties is NULL.
87 TimestampDriverGetProperties (
88 OUT EFI_TIMESTAMP_PROPERTIES
*Properties
91 if (Properties
== NULL
) {
92 return EFI_INVALID_PARAMETER
;
96 // Get timestamp properties
98 CopyMem ((VOID
*)Properties
, (VOID
*)&mTimestampProperties
, sizeof (mTimestampProperties
));
104 // The Timestamp Protocol instance produced by this driver
106 EFI_TIMESTAMP_PROTOCOL mTimestamp
= {
107 TimestampDriverGetTimestamp
,
108 TimestampDriverGetProperties
112 Entry point of the Timestamp Protocol driver.
114 @param ImageHandle The image handle of this driver.
115 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
117 @retval EFI_SUCCESS Watchdog Timer Architectural Protocol successfully installed.
122 TimestampDriverInitialize (
123 IN EFI_HANDLE ImageHandle
,
124 IN EFI_SYSTEM_TABLE
*SystemTable
129 EFI_HANDLE TimestampHandle
;
131 TimestampHandle
= NULL
;
134 // Get the start value, end value and frequency in Timerlib
136 mTimestampProperties
.Frequency
= GetPerformanceCounterProperties (&mTimerLibStartValue
, &mTimerLibEndValue
);
141 if (mTimerLibEndValue
> mTimerLibStartValue
) {
142 mTimestampProperties
.EndValue
= mTimerLibEndValue
- mTimerLibStartValue
;
144 mTimestampProperties
.EndValue
= mTimerLibStartValue
- mTimerLibEndValue
;
147 DEBUG ((DEBUG_INFO
, "TimerFrequency:0x%lx, TimerLibStartTime:0x%lx, TimerLibEndtime:0x%lx\n", mTimestampProperties
.Frequency
, mTimerLibStartValue
, mTimerLibEndValue
));
150 // Install the Timestamp Protocol onto a new handle
152 Status
= gBS
->InstallMultipleProtocolInterfaces (
154 &gEfiTimestampProtocolGuid
,
159 ASSERT_EFI_ERROR (Status
);