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