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