]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/TimestampDxe/TimestampDxe.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / TimestampDxe / TimestampDxe.c
CommitLineData
6a0d41c0
SQ
1/** @file\r
2 Implementation of Timestamp Protocol using UEFI APIs.\r
d1102dba
LG
3\r
4Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
6a0d41c0
SQ
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Uefi.h>\r
16#include <Library/DebugLib.h>\r
17#include <Library/UefiDriverEntryPoint.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/TimerLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Protocol/Timestamp.h>\r
22\r
23//\r
24// The StartValue in TimerLib\r
25//\r
26UINT64 mTimerLibStartValue = 0;\r
27\r
28//\r
29// The EndValue in TimerLib\r
30//\r
31UINT64 mTimerLibEndValue = 0;\r
32\r
33//\r
34// The properties of timestamp\r
35//\r
36EFI_TIMESTAMP_PROPERTIES mTimestampProperties = {\r
37 0,\r
38 0\r
39};\r
40\r
41/**\r
42 Retrieves the current value of a 64-bit free running timestamp counter.\r
43\r
44 The counter shall count up in proportion to the amount of time that has passed. The counter value\r
45 will always roll over to zero. The properties of the counter can be retrieved from GetProperties().\r
46 The caller should be prepared for the function to return the same value twice across successive calls.\r
47 The counter value will not go backwards other than when wrapping, as defined by EndValue in GetProperties().\r
d1102dba
LG
48 The frequency of the returned timestamp counter value must remain constant. Power management operations that\r
49 affect clocking must not change the returned counter frequency. The quantization of counter value updates may\r
50 vary as long as the value reflecting time passed remains consistent.\r
6a0d41c0
SQ
51\r
52 @retval The current value of the free running timestamp counter.\r
53\r
54**/\r
55UINT64\r
56EFIAPI\r
57TimestampDriverGetTimestamp (\r
58 VOID\r
59 )\r
60{\r
61 //\r
62 // The timestamp of Timestamp Protocol\r
63 //\r
64 UINT64 TimestampValue;\r
65 TimestampValue = 0;\r
d1102dba 66\r
6a0d41c0
SQ
67 //\r
68 // Get the timestamp\r
69 //\r
70 if (mTimerLibStartValue > mTimerLibEndValue) {\r
71 TimestampValue = mTimerLibStartValue - GetPerformanceCounter();\r
72 } else {\r
73 TimestampValue = GetPerformanceCounter() - mTimerLibStartValue;\r
74 }\r
d1102dba 75\r
6a0d41c0
SQ
76 return TimestampValue;\r
77}\r
78\r
79/**\r
80 Obtains timestamp counter properties including frequency and value limits.\r
81\r
82 @param[out] Properties The properties of the timestamp counter.\r
83\r
d1102dba
LG
84 @retval EFI_SUCCESS The properties were successfully retrieved.\r
85 @retval EFI_DEVICE_ERROR An error occurred trying to retrieve the properties of the timestamp\r
86 counter subsystem. Properties is not pedated.\r
6a0d41c0
SQ
87 @retval EFI_INVALID_PARAMETER Properties is NULL.\r
88\r
89**/\r
90EFI_STATUS\r
91EFIAPI\r
92TimestampDriverGetProperties(\r
93 OUT EFI_TIMESTAMP_PROPERTIES *Properties\r
94 )\r
95{\r
96 if (Properties == NULL) {\r
97 return EFI_INVALID_PARAMETER;\r
98 }\r
d1102dba 99\r
6a0d41c0
SQ
100 //\r
101 // Get timestamp properties\r
102 //\r
103 CopyMem((VOID *) Properties, (VOID *) &mTimestampProperties, sizeof (mTimestampProperties));\r
d1102dba 104\r
6a0d41c0
SQ
105 return EFI_SUCCESS;\r
106}\r
107\r
108//\r
109// The Timestamp Protocol instance produced by this driver\r
110//\r
111EFI_TIMESTAMP_PROTOCOL mTimestamp = {\r
112 TimestampDriverGetTimestamp,\r
113 TimestampDriverGetProperties\r
114};\r
115\r
116/**\r
117 Entry point of the Timestamp Protocol driver.\r
118\r
119 @param ImageHandle The image handle of this driver.\r
120 @param SystemTable The pointer of EFI_SYSTEM_TABLE.\r
121\r
122 @retval EFI_SUCCESS Watchdog Timer Architectural Protocol successfully installed.\r
123\r
124**/\r
125EFI_STATUS\r
126EFIAPI\r
127TimestampDriverInitialize (\r
128 IN EFI_HANDLE ImageHandle,\r
129 IN EFI_SYSTEM_TABLE *SystemTable\r
130 )\r
131{\r
132 EFI_STATUS Status;\r
d1102dba 133\r
6a0d41c0
SQ
134 EFI_HANDLE TimestampHandle;\r
135 TimestampHandle = NULL;\r
d1102dba 136\r
6a0d41c0
SQ
137 //\r
138 // Get the start value, end value and frequency in Timerlib\r
139 //\r
140 mTimestampProperties.Frequency = GetPerformanceCounterProperties(&mTimerLibStartValue, &mTimerLibEndValue);\r
d1102dba 141\r
6a0d41c0 142 //\r
d1102dba 143 // Set the EndValue\r
6a0d41c0
SQ
144 //\r
145 if (mTimerLibEndValue > mTimerLibStartValue) {\r
146 mTimestampProperties.EndValue = mTimerLibEndValue - mTimerLibStartValue;\r
147 } else {\r
148 mTimestampProperties.EndValue = mTimerLibStartValue - mTimerLibEndValue;\r
149 }\r
d1102dba 150\r
6a0d41c0 151 DEBUG ((EFI_D_INFO, "TimerFrequency:0x%lx, TimerLibStartTime:0x%lx, TimerLibEndtime:0x%lx\n", mTimestampProperties.Frequency, mTimerLibStartValue, mTimerLibEndValue));\r
d1102dba 152\r
6a0d41c0
SQ
153 //\r
154 // Install the Timestamp Protocol onto a new handle\r
155 //\r
156 Status = gBS->InstallMultipleProtocolInterfaces (\r
157 &TimestampHandle,\r
158 &gEfiTimestampProtocolGuid,\r
159 &mTimestamp,\r
160 NULL\r
161 );\r
d1102dba 162\r
6a0d41c0
SQ
163 ASSERT_EFI_ERROR (Status);\r
164\r
165 return EFI_SUCCESS;\r
166}\r