]>
Commit | Line | Data |
---|---|---|
d668c8bc AP |
1 | /** @file\r |
2 | Timer Architectural Protocol as defined in the DXE CIS\r | |
3 | \r | |
4 | Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
5 | Copyright (c) 2019, Citrix Systems, Inc.\r | |
6 | \r | |
7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
8 | \r | |
9 | **/\r | |
10 | \r | |
11 | #include "XenTimerDxe.h"\r | |
12 | \r | |
13 | //\r | |
14 | // The handle onto which the Timer Architectural Protocol will be installed\r | |
15 | //\r | |
16 | EFI_HANDLE mTimerHandle = NULL;\r | |
17 | \r | |
18 | //\r | |
19 | // The Timer Architectural Protocol that this driver produces\r | |
20 | //\r | |
21 | EFI_TIMER_ARCH_PROTOCOL mTimer = {\r | |
22 | TimerDriverRegisterHandler,\r | |
23 | TimerDriverSetTimerPeriod,\r | |
24 | TimerDriverGetTimerPeriod,\r | |
25 | TimerDriverGenerateSoftInterrupt\r | |
26 | };\r | |
27 | \r | |
28 | //\r | |
29 | // Pointer to the CPU Architectural Protocol instance\r | |
30 | //\r | |
31 | EFI_CPU_ARCH_PROTOCOL *mCpu;\r | |
32 | \r | |
33 | //\r | |
34 | // The notification function to call on every timer interrupt.\r | |
35 | // A bug in the compiler prevents us from initializing this here.\r | |
36 | //\r | |
37 | EFI_TIMER_NOTIFY mTimerNotifyFunction;\r | |
38 | \r | |
39 | //\r | |
40 | // The current period of the timer interrupt\r | |
41 | //\r | |
42 | volatile UINT64 mTimerPeriod = 0;\r | |
43 | \r | |
44 | //\r | |
45 | // Worker Functions\r | |
46 | //\r | |
47 | /**\r | |
48 | Interrupt Handler.\r | |
49 | \r | |
50 | @param InterruptType The type of interrupt that occurred\r | |
51 | @param SystemContext A pointer to the system context when the interrupt occurred\r | |
52 | **/\r | |
53 | VOID\r | |
54 | EFIAPI\r | |
55 | TimerInterruptHandler (\r | |
56 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
57 | IN EFI_SYSTEM_CONTEXT SystemContext\r | |
58 | )\r | |
59 | {\r | |
60 | EFI_TPL OriginalTPL;\r | |
61 | \r | |
62 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
63 | \r | |
64 | SendApicEoi();\r | |
65 | \r | |
66 | if (mTimerNotifyFunction != NULL) {\r | |
67 | //\r | |
68 | // @bug : This does not handle missed timer interrupts\r | |
69 | //\r | |
70 | mTimerNotifyFunction (mTimerPeriod);\r | |
71 | }\r | |
72 | \r | |
73 | gBS->RestoreTPL (OriginalTPL);\r | |
74 | }\r | |
75 | \r | |
76 | /**\r | |
77 | \r | |
78 | This function registers the handler NotifyFunction so it is called every time\r | |
79 | the timer interrupt fires. It also passes the amount of time since the last\r | |
80 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the\r | |
81 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is\r | |
82 | returned. If the CPU does not support registering a timer interrupt handler,\r | |
83 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler\r | |
84 | when a handler is already registered, then EFI_ALREADY_STARTED is returned.\r | |
85 | If an attempt is made to unregister a handler when a handler is not registered,\r | |
86 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to\r | |
87 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR\r | |
88 | is returned.\r | |
89 | \r | |
90 | \r | |
91 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
92 | @param NotifyFunction The function to call when a timer interrupt fires. This\r | |
93 | function executes at TPL_HIGH_LEVEL. The DXE Core will\r | |
94 | register a handler for the timer interrupt, so it can know\r | |
95 | how much time has passed. This information is used to\r | |
96 | signal timer based events. NULL will unregister the handler.\r | |
97 | \r | |
98 | @retval EFI_SUCCESS The timer handler was registered.\r | |
99 | @retval EFI_UNSUPPORTED The platform does not support timer interrupts.\r | |
100 | @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already\r | |
101 | registered.\r | |
102 | @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r | |
103 | previously registered.\r | |
104 | @retval EFI_DEVICE_ERROR The timer handler could not be registered.\r | |
105 | \r | |
106 | **/\r | |
107 | EFI_STATUS\r | |
108 | EFIAPI\r | |
109 | TimerDriverRegisterHandler (\r | |
110 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
111 | IN EFI_TIMER_NOTIFY NotifyFunction\r | |
112 | )\r | |
113 | {\r | |
114 | //\r | |
115 | // Check for invalid parameters\r | |
116 | //\r | |
117 | if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {\r | |
118 | return EFI_INVALID_PARAMETER;\r | |
119 | }\r | |
120 | \r | |
121 | if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {\r | |
122 | return EFI_ALREADY_STARTED;\r | |
123 | }\r | |
124 | \r | |
125 | mTimerNotifyFunction = NotifyFunction;\r | |
126 | \r | |
127 | return EFI_SUCCESS;\r | |
128 | }\r | |
129 | \r | |
130 | /**\r | |
131 | \r | |
132 | This function adjusts the period of timer interrupts to the value specified\r | |
133 | by TimerPeriod. If the timer period is updated, then the selected timer\r | |
134 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If\r | |
135 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.\r | |
136 | If an error occurs while attempting to update the timer period, then the\r | |
137 | timer hardware will be put back in its state prior to this call, and\r | |
138 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt\r | |
139 | is disabled. This is not the same as disabling the CPU's interrupts.\r | |
140 | Instead, it must either turn off the timer hardware, or it must adjust the\r | |
141 | interrupt controller so that a CPU interrupt is not generated when the timer\r | |
142 | interrupt fires.\r | |
143 | \r | |
144 | \r | |
145 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
146 | @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If\r | |
147 | the timer hardware is not programmable, then EFI_UNSUPPORTED is\r | |
148 | returned. If the timer is programmable, then the timer period\r | |
149 | will be rounded up to the nearest timer period that is supported\r | |
150 | by the timer hardware. If TimerPeriod is set to 0, then the\r | |
151 | timer interrupts will be disabled.\r | |
152 | \r | |
153 | @retval EFI_SUCCESS The timer period was changed.\r | |
154 | @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.\r | |
155 | @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.\r | |
156 | \r | |
157 | **/\r | |
158 | EFI_STATUS\r | |
159 | EFIAPI\r | |
160 | TimerDriverSetTimerPeriod (\r | |
161 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
162 | IN UINT64 TimerPeriod\r | |
163 | )\r | |
164 | {\r | |
165 | UINT64 TimerCount;\r | |
166 | UINT32 TimerFrequency;\r | |
167 | UINTN DivideValue = 1;\r | |
168 | \r | |
169 | if (TimerPeriod == 0) {\r | |
170 | //\r | |
171 | // Disable timer interrupt for a TimerPeriod of 0\r | |
172 | //\r | |
173 | DisableApicTimerInterrupt();\r | |
174 | } else {\r | |
175 | TimerFrequency = PcdGet32(PcdFSBClock) / DivideValue;\r | |
176 | \r | |
177 | //\r | |
178 | // Convert TimerPeriod into local APIC counts\r | |
179 | //\r | |
180 | // TimerPeriod is in 100ns\r | |
181 | // TimerPeriod/10000000 will be in seconds.\r | |
182 | TimerCount = DivU64x32 (MultU64x32 (TimerPeriod, TimerFrequency),\r | |
183 | 10000000);\r | |
184 | \r | |
185 | // Check for overflow\r | |
186 | if (TimerCount > MAX_UINT32) {\r | |
187 | TimerCount = MAX_UINT32;\r | |
188 | /* TimerPeriod = (MAX_UINT32 / TimerFrequency) * 10000000; */\r | |
189 | TimerPeriod = 429496730;\r | |
190 | }\r | |
191 | \r | |
192 | //\r | |
193 | // Program the timer with the new count value\r | |
194 | //\r | |
195 | InitializeApicTimer(DivideValue, TimerCount, TRUE, LOCAL_APIC_TIMER_VECTOR);\r | |
196 | \r | |
197 | //\r | |
198 | // Enable timer interrupt\r | |
199 | //\r | |
200 | EnableApicTimerInterrupt();\r | |
201 | }\r | |
202 | //\r | |
203 | // Save the new timer period\r | |
204 | //\r | |
205 | mTimerPeriod = TimerPeriod;\r | |
206 | \r | |
207 | return EFI_SUCCESS;\r | |
208 | }\r | |
209 | \r | |
210 | /**\r | |
211 | \r | |
212 | This function retrieves the period of timer interrupts in 100 ns units,\r | |
213 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r | |
214 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r | |
215 | returned, then the timer is currently disabled.\r | |
216 | \r | |
217 | \r | |
218 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
219 | @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If\r | |
220 | 0 is returned, then the timer is currently disabled.\r | |
221 | \r | |
222 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r | |
223 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r | |
224 | \r | |
225 | **/\r | |
226 | EFI_STATUS\r | |
227 | EFIAPI\r | |
228 | TimerDriverGetTimerPeriod (\r | |
229 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
230 | OUT UINT64 *TimerPeriod\r | |
231 | )\r | |
232 | {\r | |
233 | if (TimerPeriod == NULL) {\r | |
234 | return EFI_INVALID_PARAMETER;\r | |
235 | }\r | |
236 | \r | |
237 | *TimerPeriod = mTimerPeriod;\r | |
238 | \r | |
239 | return EFI_SUCCESS;\r | |
240 | }\r | |
241 | \r | |
242 | /**\r | |
243 | \r | |
244 | This function generates a soft timer interrupt. If the platform does not support soft\r | |
245 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.\r | |
246 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()\r | |
247 | service, then a soft timer interrupt will be generated. If the timer interrupt is\r | |
248 | enabled when this service is called, then the registered handler will be invoked. The\r | |
249 | registered handler should not be able to distinguish a hardware-generated timer\r | |
250 | interrupt from a software-generated timer interrupt.\r | |
251 | \r | |
252 | \r | |
253 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
254 | \r | |
255 | @retval EFI_SUCCESS The soft timer interrupt was generated.\r | |
256 | @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.\r | |
257 | \r | |
258 | **/\r | |
259 | EFI_STATUS\r | |
260 | EFIAPI\r | |
261 | TimerDriverGenerateSoftInterrupt (\r | |
262 | IN EFI_TIMER_ARCH_PROTOCOL *This\r | |
263 | )\r | |
264 | {\r | |
265 | EFI_TPL OriginalTPL;\r | |
266 | \r | |
267 | if (GetApicTimerInterruptState()) {\r | |
268 | //\r | |
269 | // Invoke the registered handler\r | |
270 | //\r | |
271 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
272 | \r | |
273 | if (mTimerNotifyFunction != NULL) {\r | |
274 | //\r | |
275 | // @bug : This does not handle missed timer interrupts\r | |
276 | //\r | |
277 | mTimerNotifyFunction (mTimerPeriod);\r | |
278 | }\r | |
279 | \r | |
280 | gBS->RestoreTPL (OriginalTPL);\r | |
281 | } else {\r | |
282 | return EFI_UNSUPPORTED;\r | |
283 | }\r | |
284 | \r | |
285 | return EFI_SUCCESS;\r | |
286 | }\r | |
287 | \r | |
288 | /**\r | |
289 | Initialize the Timer Architectural Protocol driver\r | |
290 | \r | |
291 | @param ImageHandle ImageHandle of the loaded driver\r | |
292 | @param SystemTable Pointer to the System Table\r | |
293 | \r | |
294 | @retval EFI_SUCCESS Timer Architectural Protocol created\r | |
295 | @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.\r | |
296 | @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.\r | |
297 | \r | |
298 | **/\r | |
299 | EFI_STATUS\r | |
300 | EFIAPI\r | |
301 | TimerDriverInitialize (\r | |
302 | IN EFI_HANDLE ImageHandle,\r | |
303 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
304 | )\r | |
305 | {\r | |
306 | EFI_STATUS Status;\r | |
307 | \r | |
308 | //\r | |
309 | // Initialize the pointer to our notify function.\r | |
310 | //\r | |
311 | mTimerNotifyFunction = NULL;\r | |
312 | \r | |
313 | //\r | |
314 | // Make sure the Timer Architectural Protocol is not already installed in the system\r | |
315 | //\r | |
316 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);\r | |
317 | \r | |
318 | //\r | |
319 | // Find the CPU architectural protocol.\r | |
320 | //\r | |
321 | Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &mCpu);\r | |
322 | ASSERT_EFI_ERROR (Status);\r | |
323 | \r | |
324 | //\r | |
325 | // Force the timer to be disabled\r | |
326 | //\r | |
327 | Status = TimerDriverSetTimerPeriod (&mTimer, 0);\r | |
328 | ASSERT_EFI_ERROR (Status);\r | |
329 | \r | |
330 | //\r | |
331 | // Install interrupt handler for Local APIC Timer\r | |
332 | //\r | |
333 | Status = mCpu->RegisterInterruptHandler (mCpu, LOCAL_APIC_TIMER_VECTOR,\r | |
334 | TimerInterruptHandler);\r | |
335 | ASSERT_EFI_ERROR (Status);\r | |
336 | \r | |
337 | //\r | |
338 | // Force the timer to be enabled at its default period\r | |
339 | //\r | |
340 | Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);\r | |
341 | ASSERT_EFI_ERROR (Status);\r | |
342 | \r | |
343 | //\r | |
344 | // Install the Timer Architectural Protocol onto a new handle\r | |
345 | //\r | |
346 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
347 | &mTimerHandle,\r | |
348 | &gEfiTimerArchProtocolGuid, &mTimer,\r | |
349 | NULL\r | |
350 | );\r | |
351 | ASSERT_EFI_ERROR (Status);\r | |
352 | \r | |
353 | return Status;\r | |
354 | }\r | |
355 | \r |