]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/SecPeiDxeTimerLibCpu/IpfTimerLib.c
Handle case when PAL calls 13 or 14 may not be available.
[mirror_edk2.git] / MdePkg / Library / SecPeiDxeTimerLibCpu / IpfTimerLib.c
CommitLineData
e386b444 1/** @file\r
2 Timer Library functions built upon ITC on IPF.\r
3\r
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e386b444 13**/\r
14\r
f734a10a
A
15#include <Base.h>\r
16#include <Library/TimerLib.h>\r
17#include <Library/BaseLib.h>\r
18#include <Library/IoLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/PcdLib.h>\r
e386b444 21\r
22\r
e386b444 23/**\r
24 Performs a delay measured as number of ticks.\r
25\r
26 An internal function to perform a delay measured as number of ticks. It's\r
27 invoked by MicroSecondDelay() and NanoSecondDelay().\r
28\r
29 @param Delay Number of ticks to delay.\r
30\r
31**/\r
32STATIC\r
33VOID\r
34InternalIpfDelay (\r
35 IN INT64 Delay\r
36 )\r
37{\r
38 INT64 Ticks;\r
39\r
40 //\r
41 // The target timer count is calculated here\r
42 //\r
43 Ticks = (INT64)AsmReadItc () + Delay;\r
44\r
45 //\r
46 // Wait until time out\r
47 // Delay > 2^63 could not be handled by this function\r
48 // Timer wrap-arounds are handled correctly by this function\r
49 //\r
50 while (Ticks - (INT64)AsmReadItc() >= 0);\r
51}\r
52\r
53/**\r
54 Stalls the CPU for at least the given number of microseconds.\r
55\r
56 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
57\r
58 @param MicroSeconds The minimum number of microseconds to delay.\r
59\r
60 @return MicroSeconds\r
61\r
62**/\r
63UINTN\r
64EFIAPI\r
65MicroSecondDelay (\r
66 IN UINTN MicroSeconds\r
67 )\r
68{\r
69 InternalIpfDelay (\r
70 GetPerformanceCounterProperties (NULL, NULL) *\r
71 MicroSeconds /\r
72 1000000\r
73 );\r
74 return MicroSeconds;\r
75}\r
76\r
77/**\r
78 Stalls the CPU for at least the given number of nanoseconds.\r
79\r
80 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
81\r
82 @param NanoSeconds The minimum number of nanoseconds to delay.\r
83\r
84 @return NanoSeconds\r
85\r
86**/\r
87UINTN\r
88EFIAPI\r
89NanoSecondDelay (\r
90 IN UINTN NanoSeconds\r
91 )\r
92{\r
93 InternalIpfDelay (\r
94 GetPerformanceCounterProperties (NULL, NULL) *\r
95 NanoSeconds /\r
96 1000000000\r
97 );\r
98 return NanoSeconds;\r
99}\r
100\r
101/**\r
102 Retrieves the current value of a 64-bit free running performance counter.\r
103\r
104 Retrieves the current value of a 64-bit free running performance counter. The\r
105 counter can either count up by 1 or count down by 1. If the physical\r
106 performance counter counts by a larger increment, then the counter values\r
107 must be translated. The properties of the counter can be retrieved from\r
108 GetPerformanceCounterProperties().\r
109\r
110 @return The current value of the free running performance counter.\r
111\r
112**/\r
113UINT64\r
114EFIAPI\r
115GetPerformanceCounter (\r
116 VOID\r
117 )\r
118{\r
119 return AsmReadItc ();\r
120}\r
121\r
122/**\r
123 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
124 values.\r
125\r
126 If StartValue is not NULL, then the value that the performance counter starts\r
127 with immediately after is it rolls over is returned in StartValue. If\r
128 EndValue is not NULL, then the value that the performance counter end with\r
129 immediately before it rolls over is returned in EndValue. The 64-bit\r
130 frequency of the performance counter in Hz is always returned. If StartValue\r
131 is less than EndValue, then the performance counter counts up. If StartValue\r
132 is greater than EndValue, then the performance counter counts down. For\r
133 example, a 64-bit free running counter that counts up would have a StartValue\r
134 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
135 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
136\r
137 @param StartValue The value the performance counter starts with when it\r
138 rolls over.\r
139 @param EndValue The value that the performance counter ends with before\r
140 it rolls over.\r
141\r
142 @return The frequency in Hz.\r
143\r
144**/\r
145UINT64\r
146EFIAPI\r
147GetPerformanceCounterProperties (\r
148 OUT UINT64 *StartValue, OPTIONAL\r
149 OUT UINT64 *EndValue OPTIONAL\r
150 )\r
151{\r
152 PAL_CALL_RETURN PalRet;\r
153 UINT64 BaseFrequence;\r
154\r
e386b444 155 if (StartValue != NULL) {\r
156 *StartValue = 0;\r
157 }\r
158\r
159 if (EndValue != NULL) {\r
160 *EndValue = (UINT64)(-1);\r
161 }\r
162\r
580ad4a0 163 PalRet = PalCallStatic (NULL, 13, 0, 0, 0);\r
164 if (PalRet.Status != 0) {\r
165 return 1000000;\r
166 }\r
167 BaseFrequence = PalRet.r9;\r
168\r
169 PalRet = PalCallStatic (NULL, 14, 0, 0, 0);\r
170 if (PalRet.Status != 0) {\r
171 return 1000000;\r
172 }\r
173\r
e386b444 174 return BaseFrequence * (PalRet.r11 >> 32) / (UINT32)PalRet.r11;\r
175}\r