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
;
64 if (mTimerLibStartValue
> mTimerLibEndValue
) {
65 TimestampValue
= mTimerLibStartValue
- GetPerformanceCounter();
67 TimestampValue
= GetPerformanceCounter() - mTimerLibStartValue
;
70 return TimestampValue
;
74 Obtains timestamp counter properties including frequency and value limits.
76 @param[out] Properties The properties of the timestamp counter.
78 @retval EFI_SUCCESS The properties were successfully retrieved.
79 @retval EFI_DEVICE_ERROR An error occurred trying to retrieve the properties of the timestamp
80 counter subsystem. Properties is not pedated.
81 @retval EFI_INVALID_PARAMETER Properties is NULL.
86 TimestampDriverGetProperties(
87 OUT EFI_TIMESTAMP_PROPERTIES
*Properties
90 if (Properties
== NULL
) {
91 return EFI_INVALID_PARAMETER
;
95 // Get timestamp properties
97 CopyMem((VOID
*) Properties
, (VOID
*) &mTimestampProperties
, sizeof (mTimestampProperties
));
103 // The Timestamp Protocol instance produced by this driver
105 EFI_TIMESTAMP_PROTOCOL mTimestamp
= {
106 TimestampDriverGetTimestamp
,
107 TimestampDriverGetProperties
111 Entry point of the Timestamp Protocol driver.
113 @param ImageHandle The image handle of this driver.
114 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
116 @retval EFI_SUCCESS Watchdog Timer Architectural Protocol successfully installed.
121 TimestampDriverInitialize (
122 IN EFI_HANDLE ImageHandle
,
123 IN EFI_SYSTEM_TABLE
*SystemTable
128 EFI_HANDLE TimestampHandle
;
129 TimestampHandle
= NULL
;
132 // Get the start value, end value and frequency in Timerlib
134 mTimestampProperties
.Frequency
= GetPerformanceCounterProperties(&mTimerLibStartValue
, &mTimerLibEndValue
);
139 if (mTimerLibEndValue
> mTimerLibStartValue
) {
140 mTimestampProperties
.EndValue
= mTimerLibEndValue
- mTimerLibStartValue
;
142 mTimestampProperties
.EndValue
= mTimerLibStartValue
- mTimerLibEndValue
;
145 DEBUG ((EFI_D_INFO
, "TimerFrequency:0x%lx, TimerLibStartTime:0x%lx, TimerLibEndtime:0x%lx\n", mTimestampProperties
.Frequency
, mTimerLibStartValue
, mTimerLibEndValue
));
148 // Install the Timestamp Protocol onto a new handle
150 Status
= gBS
->InstallMultipleProtocolInterfaces (
152 &gEfiTimestampProtocolGuid
,
157 ASSERT_EFI_ERROR (Status
);