2 Implementation of Timestamp Protocol using UEFI APIs.
4 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/DebugLib.h>
17 #include <Library/UefiDriverEntryPoint.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/TimerLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Protocol/Timestamp.h>
24 // The StartValue in TimerLib
26 UINT64 mTimerLibStartValue
= 0;
29 // The EndValue in TimerLib
31 UINT64 mTimerLibEndValue
= 0;
34 // The properties of timestamp
36 EFI_TIMESTAMP_PROPERTIES mTimestampProperties
= {
42 Retrieves the current value of a 64-bit free running timestamp counter.
44 The counter shall count up in proportion to the amount of time that has passed. The counter value
45 will always roll over to zero. The properties of the counter can be retrieved from GetProperties().
46 The caller should be prepared for the function to return the same value twice across successive calls.
47 The counter value will not go backwards other than when wrapping, as defined by EndValue in GetProperties().
48 The frequency of the returned timestamp counter value must remain constant. Power management operations that
49 affect clocking must not change the returned counter frequency. The quantization of counter value updates may
50 vary as long as the value reflecting time passed remains consistent.
52 @retval The current value of the free running timestamp counter.
57 TimestampDriverGetTimestamp (
62 // The timestamp of Timestamp Protocol
64 UINT64 TimestampValue
;
70 if (mTimerLibStartValue
> mTimerLibEndValue
) {
71 TimestampValue
= mTimerLibStartValue
- GetPerformanceCounter();
73 TimestampValue
= GetPerformanceCounter() - mTimerLibStartValue
;
76 return TimestampValue
;
80 Obtains timestamp counter properties including frequency and value limits.
82 @param[out] Properties The properties of the timestamp counter.
84 @retval EFI_SUCCESS The properties were successfully retrieved.
85 @retval EFI_DEVICE_ERROR An error occurred trying to retrieve the properties of the timestamp
86 counter subsystem. Properties is not pedated.
87 @retval EFI_INVALID_PARAMETER Properties is NULL.
92 TimestampDriverGetProperties(
93 OUT EFI_TIMESTAMP_PROPERTIES
*Properties
96 if (Properties
== NULL
) {
97 return EFI_INVALID_PARAMETER
;
101 // Get timestamp properties
103 CopyMem((VOID
*) Properties
, (VOID
*) &mTimestampProperties
, sizeof (mTimestampProperties
));
109 // The Timestamp Protocol instance produced by this driver
111 EFI_TIMESTAMP_PROTOCOL mTimestamp
= {
112 TimestampDriverGetTimestamp
,
113 TimestampDriverGetProperties
117 Entry point of the Timestamp Protocol driver.
119 @param ImageHandle The image handle of this driver.
120 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
122 @retval EFI_SUCCESS Watchdog Timer Architectural Protocol successfully installed.
127 TimestampDriverInitialize (
128 IN EFI_HANDLE ImageHandle
,
129 IN EFI_SYSTEM_TABLE
*SystemTable
134 EFI_HANDLE TimestampHandle
;
135 TimestampHandle
= NULL
;
138 // Get the start value, end value and frequency in Timerlib
140 mTimestampProperties
.Frequency
= GetPerformanceCounterProperties(&mTimerLibStartValue
, &mTimerLibEndValue
);
145 if (mTimerLibEndValue
> mTimerLibStartValue
) {
146 mTimestampProperties
.EndValue
= mTimerLibEndValue
- mTimerLibStartValue
;
148 mTimestampProperties
.EndValue
= mTimerLibStartValue
- mTimerLibEndValue
;
151 DEBUG ((EFI_D_INFO
, "TimerFrequency:0x%lx, TimerLibStartTime:0x%lx, TimerLibEndtime:0x%lx\n", mTimestampProperties
.Frequency
, mTimerLibStartValue
, mTimerLibEndValue
));
154 // Install the Timestamp Protocol onto a new handle
156 Status
= gBS
->InstallMultipleProtocolInterfaces (
158 &gEfiTimestampProtocolGuid
,
163 ASSERT_EFI_ERROR (Status
);