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