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