]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/TimestampDxe/TimestampDxe.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / TimestampDxe / TimestampDxe.c
1 /** @file
2 Implementation of Timestamp Protocol using UEFI APIs.
3
4 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
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>
16
17 //
18 // The StartValue in TimerLib
19 //
20 UINT64 mTimerLibStartValue = 0;
21
22 //
23 // The EndValue in TimerLib
24 //
25 UINT64 mTimerLibEndValue = 0;
26
27 //
28 // The properties of timestamp
29 //
30 EFI_TIMESTAMP_PROPERTIES mTimestampProperties = {
31 0,
32 0
33 };
34
35 /**
36 Retrieves the current value of a 64-bit free running timestamp counter.
37
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.
45
46 @retval The current value of the free running timestamp counter.
47
48 **/
49 UINT64
50 EFIAPI
51 TimestampDriverGetTimestamp (
52 VOID
53 )
54 {
55 //
56 // The timestamp of Timestamp Protocol
57 //
58 UINT64 TimestampValue;
59
60 TimestampValue = 0;
61
62 //
63 // Get the timestamp
64 //
65 if (mTimerLibStartValue > mTimerLibEndValue) {
66 TimestampValue = mTimerLibStartValue - GetPerformanceCounter ();
67 } else {
68 TimestampValue = GetPerformanceCounter () - mTimerLibStartValue;
69 }
70
71 return TimestampValue;
72 }
73
74 /**
75 Obtains timestamp counter properties including frequency and value limits.
76
77 @param[out] Properties The properties of the timestamp counter.
78
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.
83
84 **/
85 EFI_STATUS
86 EFIAPI
87 TimestampDriverGetProperties (
88 OUT EFI_TIMESTAMP_PROPERTIES *Properties
89 )
90 {
91 if (Properties == NULL) {
92 return EFI_INVALID_PARAMETER;
93 }
94
95 //
96 // Get timestamp properties
97 //
98 CopyMem ((VOID *)Properties, (VOID *)&mTimestampProperties, sizeof (mTimestampProperties));
99
100 return EFI_SUCCESS;
101 }
102
103 //
104 // The Timestamp Protocol instance produced by this driver
105 //
106 EFI_TIMESTAMP_PROTOCOL mTimestamp = {
107 TimestampDriverGetTimestamp,
108 TimestampDriverGetProperties
109 };
110
111 /**
112 Entry point of the Timestamp Protocol driver.
113
114 @param ImageHandle The image handle of this driver.
115 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
116
117 @retval EFI_SUCCESS Watchdog Timer Architectural Protocol successfully installed.
118
119 **/
120 EFI_STATUS
121 EFIAPI
122 TimestampDriverInitialize (
123 IN EFI_HANDLE ImageHandle,
124 IN EFI_SYSTEM_TABLE *SystemTable
125 )
126 {
127 EFI_STATUS Status;
128
129 EFI_HANDLE TimestampHandle;
130
131 TimestampHandle = NULL;
132
133 //
134 // Get the start value, end value and frequency in Timerlib
135 //
136 mTimestampProperties.Frequency = GetPerformanceCounterProperties (&mTimerLibStartValue, &mTimerLibEndValue);
137
138 //
139 // Set the EndValue
140 //
141 if (mTimerLibEndValue > mTimerLibStartValue) {
142 mTimestampProperties.EndValue = mTimerLibEndValue - mTimerLibStartValue;
143 } else {
144 mTimestampProperties.EndValue = mTimerLibStartValue - mTimerLibEndValue;
145 }
146
147 DEBUG ((DEBUG_INFO, "TimerFrequency:0x%lx, TimerLibStartTime:0x%lx, TimerLibEndtime:0x%lx\n", mTimestampProperties.Frequency, mTimerLibStartValue, mTimerLibEndValue));
148
149 //
150 // Install the Timestamp Protocol onto a new handle
151 //
152 Status = gBS->InstallMultipleProtocolInterfaces (
153 &TimestampHandle,
154 &gEfiTimestampProtocolGuid,
155 &mTimestamp,
156 NULL
157 );
158
159 ASSERT_EFI_ERROR (Status);
160
161 return EFI_SUCCESS;
162 }