]>
Commit | Line | Data |
---|---|---|
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 | |
13 | Module Name: IpfTimerLib.c\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | \r | |
18 | \r | |
19 | \r | |
20 | //\r | |
21 | // Include common header file for this module.\r | |
22 | //\r | |
23 | #include "CommonHeader.h"\r | |
24 | \r | |
25 | /**\r | |
26 | Performs a delay measured as number of ticks.\r | |
27 | \r | |
28 | An internal function to perform a delay measured as number of ticks. It's\r | |
29 | invoked by MicroSecondDelay() and NanoSecondDelay().\r | |
30 | \r | |
31 | @param Delay Number of ticks to delay.\r | |
32 | \r | |
33 | **/\r | |
34 | STATIC\r | |
35 | VOID\r | |
36 | InternalIpfDelay (\r | |
37 | IN INT64 Delay\r | |
38 | )\r | |
39 | {\r | |
40 | INT64 Ticks;\r | |
41 | \r | |
42 | //\r | |
43 | // The target timer count is calculated here\r | |
44 | //\r | |
45 | Ticks = (INT64)AsmReadItc () + Delay;\r | |
46 | \r | |
47 | //\r | |
48 | // Wait until time out\r | |
49 | // Delay > 2^63 could not be handled by this function\r | |
50 | // Timer wrap-arounds are handled correctly by this function\r | |
51 | //\r | |
52 | while (Ticks - (INT64)AsmReadItc() >= 0);\r | |
53 | }\r | |
54 | \r | |
55 | /**\r | |
56 | Stalls the CPU for at least the given number of microseconds.\r | |
57 | \r | |
58 | Stalls the CPU for the number of microseconds specified by MicroSeconds.\r | |
59 | \r | |
60 | @param MicroSeconds The minimum number of microseconds to delay.\r | |
61 | \r | |
62 | @return MicroSeconds\r | |
63 | \r | |
64 | **/\r | |
65 | UINTN\r | |
66 | EFIAPI\r | |
67 | MicroSecondDelay (\r | |
68 | IN UINTN MicroSeconds\r | |
69 | )\r | |
70 | {\r | |
71 | InternalIpfDelay (\r | |
72 | GetPerformanceCounterProperties (NULL, NULL) *\r | |
73 | MicroSeconds /\r | |
74 | 1000000\r | |
75 | );\r | |
76 | return MicroSeconds;\r | |
77 | }\r | |
78 | \r | |
79 | /**\r | |
80 | Stalls the CPU for at least the given number of nanoseconds.\r | |
81 | \r | |
82 | Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r | |
83 | \r | |
84 | @param NanoSeconds The minimum number of nanoseconds to delay.\r | |
85 | \r | |
86 | @return NanoSeconds\r | |
87 | \r | |
88 | **/\r | |
89 | UINTN\r | |
90 | EFIAPI\r | |
91 | NanoSecondDelay (\r | |
92 | IN UINTN NanoSeconds\r | |
93 | )\r | |
94 | {\r | |
95 | InternalIpfDelay (\r | |
96 | GetPerformanceCounterProperties (NULL, NULL) *\r | |
97 | NanoSeconds /\r | |
98 | 1000000000\r | |
99 | );\r | |
100 | return NanoSeconds;\r | |
101 | }\r | |
102 | \r | |
103 | /**\r | |
104 | Retrieves the current value of a 64-bit free running performance counter.\r | |
105 | \r | |
106 | Retrieves the current value of a 64-bit free running performance counter. The\r | |
107 | counter can either count up by 1 or count down by 1. If the physical\r | |
108 | performance counter counts by a larger increment, then the counter values\r | |
109 | must be translated. The properties of the counter can be retrieved from\r | |
110 | GetPerformanceCounterProperties().\r | |
111 | \r | |
112 | @return The current value of the free running performance counter.\r | |
113 | \r | |
114 | **/\r | |
115 | UINT64\r | |
116 | EFIAPI\r | |
117 | GetPerformanceCounter (\r | |
118 | VOID\r | |
119 | )\r | |
120 | {\r | |
121 | return AsmReadItc ();\r | |
122 | }\r | |
123 | \r | |
124 | /**\r | |
125 | Retrieves the 64-bit frequency in Hz and the range of performance counter\r | |
126 | values.\r | |
127 | \r | |
128 | If StartValue is not NULL, then the value that the performance counter starts\r | |
129 | with immediately after is it rolls over is returned in StartValue. If\r | |
130 | EndValue is not NULL, then the value that the performance counter end with\r | |
131 | immediately before it rolls over is returned in EndValue. The 64-bit\r | |
132 | frequency of the performance counter in Hz is always returned. If StartValue\r | |
133 | is less than EndValue, then the performance counter counts up. If StartValue\r | |
134 | is greater than EndValue, then the performance counter counts down. For\r | |
135 | example, a 64-bit free running counter that counts up would have a StartValue\r | |
136 | of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r | |
137 | that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r | |
138 | \r | |
139 | @param StartValue The value the performance counter starts with when it\r | |
140 | rolls over.\r | |
141 | @param EndValue The value that the performance counter ends with before\r | |
142 | it rolls over.\r | |
143 | \r | |
144 | @return The frequency in Hz.\r | |
145 | \r | |
146 | **/\r | |
147 | UINT64\r | |
148 | EFIAPI\r | |
149 | GetPerformanceCounterProperties (\r | |
150 | OUT UINT64 *StartValue, OPTIONAL\r | |
151 | OUT UINT64 *EndValue OPTIONAL\r | |
152 | )\r | |
153 | {\r | |
154 | PAL_CALL_RETURN PalRet;\r | |
155 | UINT64 BaseFrequence;\r | |
156 | \r | |
157 | PalRet = PalCallStatic (NULL, 13, 0, 0, 0);\r | |
158 | ASSERT (PalRet.Status == 0);\r | |
159 | BaseFrequence = PalRet.r9;\r | |
160 | \r | |
161 | PalRet = PalCallStatic (NULL, 14, 0, 0, 0);\r | |
162 | ASSERT (PalRet.Status == 0);\r | |
163 | \r | |
164 | if (StartValue != NULL) {\r | |
165 | *StartValue = 0;\r | |
166 | }\r | |
167 | \r | |
168 | if (EndValue != NULL) {\r | |
169 | *EndValue = (UINT64)(-1);\r | |
170 | }\r | |
171 | \r | |
172 | return BaseFrequence * (PalRet.r11 >> 32) / (UINT32)PalRet.r11;\r | |
173 | }\r |