blob: 964f814bb22fbba30e009742c6229d19c6a7ceed [file] [log] [blame]
mdkinney863be5d2011-06-10 18:58:08 +00001/** @file
2 This library implements the Timer Library using the Extended SAL Stall Services Class.
3
4 Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13**/
14
15#include <PiDxe.h>
16
17#include <Protocol/ExtendedSalServiceClasses.h>
18
19#include <Library/TimerLib.h>
20#include <Library/BaseLib.h>
21#include <Library/ExtendedSalLib.h>
22#include <Library/DebugLib.h>
23#include <Library/PalLib.h>
24
25/**
26 Stalls the CPU for at least the given number of microseconds.
27
28 This function wraps EsalStall function of Extended SAL Stall Services Class.
29 It stalls the CPU for the number of microseconds specified by MicroSeconds.
30
31 @param MicroSeconds The minimum number of microseconds to delay.
32
33 @return MicroSeconds
34
35**/
36UINTN
37EFIAPI
38MicroSecondDelay (
39 IN UINTN MicroSeconds
40 )
41{
42 EsalCall (
43 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO,
44 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI,
45 StallFunctionId,
46 MicroSeconds,
47 0,
48 0,
49 0,
50 0,
51 0,
52 0
53 );
54 return MicroSeconds;
55}
56
57/**
58 Stalls the CPU for at least the given number of nanoseconds.
59
60 This function wraps EsalStall function of Extended SAL Stall Services Class.
61 It stalls the CPU for the number of nanoseconds specified by NanoSeconds.
62
63 @param NanoSeconds The minimum number of nanoseconds to delay.
64
65 @return NanoSeconds
66
67**/
68UINTN
69EFIAPI
70NanoSecondDelay (
71 IN UINTN NanoSeconds
72 )
73{
74 UINT64 MicroSeconds;
75
76 //
77 // The unit of ESAL Stall service is microsecond, so we turn the time interval
78 // from nanosecond to microsecond, using the ceiling value to ensure stalling
79 // at least the given number of nanoseconds.
80 //
81 MicroSeconds = DivU64x32 (NanoSeconds + 999, 1000);
82 EsalCall (
83 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO,
84 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI,
85 StallFunctionId,
86 MicroSeconds,
87 0,
88 0,
89 0,
90 0,
91 0,
92 0
93 );
94 return NanoSeconds;
95}
96
97/**
98 Retrieves the current value of a 64-bit free running performance counter.
99
100 Retrieves the current value of a 64-bit free running performance counter. The
101 counter can either count up by 1 or count down by 1. If the physical
102 performance counter counts by a larger increment, then the counter values
103 must be translated. The properties of the counter can be retrieved from
104 GetPerformanceCounterProperties().
105
106 @return The current value of the free running performance counter.
107
108**/
109UINT64
110EFIAPI
111GetPerformanceCounter (
112 VOID
113 )
114{
115 return AsmReadItc ();
116}
117
118/**
119 Retrieves the 64-bit frequency in Hz and the range of performance counter
120 values.
121
122 If StartValue is not NULL, then the value that the performance counter starts
123 with immediately after is it rolls over is returned in StartValue. If
124 EndValue is not NULL, then the value that the performance counter end with
125 immediately before it rolls over is returned in EndValue. The 64-bit
126 frequency of the performance counter in Hz is always returned. If StartValue
127 is less than EndValue, then the performance counter counts up. If StartValue
128 is greater than EndValue, then the performance counter counts down. For
129 example, a 64-bit free running counter that counts up would have a StartValue
130 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
131 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
132
133 @param StartValue The value the performance counter starts with when it
134 rolls over.
135 @param EndValue The value that the performance counter ends with before
136 it rolls over.
137
138 @return The frequency in Hz.
139
140**/
141UINT64
142EFIAPI
143GetPerformanceCounterProperties (
144 OUT UINT64 *StartValue, OPTIONAL
145 OUT UINT64 *EndValue OPTIONAL
146 )
147{
148 PAL_CALL_RETURN PalRet;
149 UINT64 BaseFrequence;
150
151 //
152 // Get processor base frequency
153 //
154 PalRet = PalCall (PAL_FREQ_BASE, 0, 0, 0);
155 ASSERT (PalRet.Status == 0);
156 BaseFrequence = PalRet.r9;
157
158 //
159 // Get processor frequency ratio
160 //
161 PalRet = PalCall (PAL_FREQ_RATIOS, 0, 0, 0);
162 ASSERT (PalRet.Status == 0);
163
164 //
165 // Start value of counter is 0
166 //
167 if (StartValue != NULL) {
168 *StartValue = 0;
169 }
170
171 //
172 // End value of counter is 0xFFFFFFFFFFFFFFFF
173 //
174 if (EndValue != NULL) {
175 *EndValue = (UINT64)(-1);
176 }
177
178 return BaseFrequence * (PalRet.r11 >> 32) / (UINT32)PalRet.r11;
179}