]> git.proxmox.com Git - mirror_edk2.git/blame - EdkUnixPkg/Dxe/UnixThunk/Chipset/Timer/Timer.c
Fix component name bugs when input Controller Name is invalid
[mirror_edk2.git] / EdkUnixPkg / Dxe / UnixThunk / Chipset / Timer / Timer.c
CommitLineData
c9093a06 1/*++\r
2\r
3Copyright (c) 2004, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 Timer.c\r
15\r
16Abstract:\r
17\r
377fc2ae 18 UNIX Emulation Timer Architectural Protocol Driver as defined in DXE CIS\r
c9093a06 19\r
377fc2ae 20 This Timer module uses an UNIX Thread to simulate the timer-tick driven\r
c9093a06 21 timer service. In the future, the Thread creation should possibly be \r
22 abstracted by the CPU architectural protocol\r
23\r
24--*/\r
25\r
26#include "Timer.h"\r
27\r
28//\r
29// Pointer to the CPU Architectural Protocol instance\r
30//\r
31EFI_CPU_ARCH_PROTOCOL *mCpu;\r
32\r
33//\r
34// The Timer Architectural Protocol that this driver produces\r
35//\r
36EFI_TIMER_ARCH_PROTOCOL mTimer = {\r
37 UnixTimerDriverRegisterHandler,\r
38 UnixTimerDriverSetTimerPeriod,\r
39 UnixTimerDriverGetTimerPeriod,\r
40 UnixTimerDriverGenerateSoftInterrupt\r
41};\r
42\r
43//\r
44// The notification function to call on every timer interrupt\r
45//\r
46EFI_TIMER_NOTIFY mTimerNotifyFunction = NULL;\r
47\r
48//\r
49// The current period of the timer interrupt\r
50//\r
51UINT64 mTimerPeriod;\r
52\r
53//\r
54// The timer value from the last timer interrupt\r
55//\r
56UINT32 mNtLastTick;\r
57\r
58VOID\r
59TimerCallback (UINT64 DeltaMs)
60/*++\r
61\r
62Routine Description:\r
63\r
64 TODO: Add function description\r
65\r
66Arguments:\r
67\r
68 wTimerID - TODO: add argument description\r
69 msg - TODO: add argument description\r
70 dwUser - TODO: add argument description\r
71 dw1 - TODO: add argument description\r
72 dw2 - TODO: add argument description\r
73\r
74Returns:\r
75\r
76 TODO: add return values\r
77\r
78--*/\r
79{\r
80 EFI_TPL OriginalTPL;\r
81 EFI_TIMER_NOTIFY CallbackFunction;
82
83\r
84 OriginalTPL = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);\r
85\r
86 CallbackFunction = mTimerNotifyFunction;\r
87\r
88 //\r
89 // Only invoke the callback function if a Non-NULL handler has been\r
90 // registered. Assume all other handlers are legal.\r
91 //\r
92 if (CallbackFunction != NULL) {\r
93 CallbackFunction ((UINT64) (DeltaMs * 10000));\r
94 }\r
95\r
96 gBS->RestoreTPL (OriginalTPL);\r
97\r
98}\r
99\r
100EFI_STATUS\r
101EFIAPI\r
102UnixTimerDriverRegisterHandler (\r
103 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
104 IN EFI_TIMER_NOTIFY NotifyFunction\r
105 )\r
106/*++\r
107\r
108Routine Description:\r
109\r
110 This function registers the handler NotifyFunction so it is called every time \r
111 the timer interrupt fires. It also passes the amount of time since the last \r
112 handler call to the NotifyFunction. If NotifyFunction is NULL, then the \r
113 handler is unregistered. If the handler is registered, then EFI_SUCCESS is \r
114 returned. If the CPU does not support registering a timer interrupt handler, \r
115 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler \r
116 when a handler is already registered, then EFI_ALREADY_STARTED is returned. \r
117 If an attempt is made to unregister a handler when a handler is not registered, \r
118 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to \r
119 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR \r
120 is returned.\r
121\r
122Arguments:\r
123\r
124 This - The EFI_TIMER_ARCH_PROTOCOL instance.\r
125\r
126 NotifyFunction - The function to call when a timer interrupt fires. This \r
127 function executes at TPL_HIGH_LEVEL. The DXE Core will \r
128 register a handler for the timer interrupt, so it can know \r
129 how much time has passed. This information is used to \r
130 signal timer based events. NULL will unregister the handler.\r
131\r
132Returns: \r
133\r
134 EFI_SUCCESS - The timer handler was registered.\r
135\r
136 EFI_UNSUPPORTED - The platform does not support timer interrupts.\r
137\r
138 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already \r
139 registered.\r
140\r
141 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not \r
142 previously registered.\r
143\r
144 EFI_DEVICE_ERROR - The timer handler could not be registered.\r
145\r
146--*/\r
147{\r
148 //\r
149 // Check for invalid parameters\r
150 //\r
151 if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {\r
152 return EFI_INVALID_PARAMETER;\r
153 }\r
154\r
155 if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {\r
156 return EFI_ALREADY_STARTED;\r
157 }\r
158\r
159 if (NotifyFunction == NULL) {
160 /* Disable timer. */
161 gUnix->SetTimer (0, TimerCallback);
162 } else if (mTimerNotifyFunction == NULL) {
163 /* Enable Timer. */
164 gUnix->SetTimer (mTimerPeriod * 10, TimerCallback);
165 }
166 mTimerNotifyFunction = NotifyFunction;\r
167\r
168 return EFI_SUCCESS;\r
169}\r
170\r
171EFI_STATUS\r
172EFIAPI\r
173UnixTimerDriverSetTimerPeriod (\r
174 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
175 IN UINT64 TimerPeriod\r
176 )\r
177/*++\r
178\r
179Routine Description:\r
180\r
181 This function adjusts the period of timer interrupts to the value specified \r
182 by TimerPeriod. If the timer period is updated, then the selected timer \r
183 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If \r
184 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned. \r
185 If an error occurs while attempting to update the timer period, then the \r
186 timer hardware will be put back in its state prior to this call, and \r
187 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt \r
188 is disabled. This is not the same as disabling the CPU's interrupts. \r
189 Instead, it must either turn off the timer hardware, or it must adjust the \r
190 interrupt controller so that a CPU interrupt is not generated when the timer \r
191 interrupt fires. \r
192\r
193Arguments:\r
194\r
195 This - The EFI_TIMER_ARCH_PROTOCOL instance.\r
196\r
197 TimerPeriod - The rate to program the timer interrupt in 100 nS units. If \r
198 the timer hardware is not programmable, then EFI_UNSUPPORTED is \r
199 returned. If the timer is programmable, then the timer period \r
200 will be rounded up to the nearest timer period that is supported \r
201 by the timer hardware. If TimerPeriod is set to 0, then the \r
202 timer interrupts will be disabled.\r
203\r
204Returns: \r
205\r
206 EFI_SUCCESS - The timer period was changed.\r
207\r
208 EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.\r
209\r
210 EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.\r
211\r
212--*/\r
213{\r
214\r
215 //\r
216 // If TimerPeriod is 0, then the timer thread should be canceled\r
217 // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread\r
218 //\r
219 if (TimerPeriod == 0
220 || ((TimerPeriod > TIMER_MINIMUM_VALUE)
221 && (TimerPeriod < TIMER_MAXIMUM_VALUE))) {\r
222 mTimerPeriod = TimerPeriod;\r
223
224 gUnix->SetTimer (TimerPeriod * 10, TimerCallback);
225 }
226\r
227 return EFI_SUCCESS;\r
228}\r
229\r
230EFI_STATUS\r
231EFIAPI\r
232UnixTimerDriverGetTimerPeriod (\r
233 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
234 OUT UINT64 *TimerPeriod\r
235 )\r
236/*++\r
237\r
238Routine Description:\r
239\r
240 This function retrieves the period of timer interrupts in 100 ns units, \r
241 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod \r
242 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is \r
243 returned, then the timer is currently disabled.\r
244\r
245Arguments:\r
246\r
247 This - The EFI_TIMER_ARCH_PROTOCOL instance.\r
248\r
249 TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If \r
250 0 is returned, then the timer is currently disabled.\r
251\r
252Returns: \r
253\r
254 EFI_SUCCESS - The timer period was returned in TimerPeriod.\r
255\r
256 EFI_INVALID_PARAMETER - TimerPeriod is NULL.\r
257\r
258--*/\r
259{\r
260 if (TimerPeriod == NULL) {\r
261 return EFI_INVALID_PARAMETER;\r
262 }\r
263\r
264 *TimerPeriod = mTimerPeriod;\r
265\r
266 return EFI_SUCCESS;\r
267}\r
268\r
269EFI_STATUS\r
270EFIAPI\r
271UnixTimerDriverGenerateSoftInterrupt (\r
272 IN EFI_TIMER_ARCH_PROTOCOL *This\r
273 )\r
274/*++\r
275\r
276Routine Description:\r
277\r
278 This function generates a soft timer interrupt. If the platform does not support soft \r
279 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned. \r
280 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler() \r
281 service, then a soft timer interrupt will be generated. If the timer interrupt is \r
282 enabled when this service is called, then the registered handler will be invoked. The \r
283 registered handler should not be able to distinguish a hardware-generated timer \r
284 interrupt from a software-generated timer interrupt.\r
285\r
286Arguments:\r
287\r
288 This - The EFI_TIMER_ARCH_PROTOCOL instance.\r
289\r
290Returns: \r
291\r
292 EFI_SUCCESS - The soft timer interrupt was generated.\r
293\r
294 EFI_UNSUPPORTEDT - The platform does not support the generation of soft timer interrupts.\r
295\r
296--*/\r
297{\r
298 return EFI_UNSUPPORTED;\r
299}\r
300\r
301EFI_STATUS\r
302EFIAPI\r
303UnixTimerDriverInitialize (\r
304 IN EFI_HANDLE ImageHandle,\r
305 IN EFI_SYSTEM_TABLE *SystemTable\r
306 )\r
307/*++\r
308\r
309Routine Description:\r
310\r
311 Initialize the Timer Architectural Protocol driver\r
312\r
313Arguments:\r
314\r
315 ImageHandle - ImageHandle of the loaded driver\r
316\r
317 SystemTable - Pointer to the System Table\r
318\r
319Returns:\r
320\r
321 EFI_SUCCESS - Timer Architectural Protocol created\r
322\r
323 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.\r
324 \r
325 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.\r
326\r
327--*/\r
328{\r
329 EFI_STATUS Status;\r
330 EFI_HANDLE Handle;\r
331\r
332 //\r
333 // Make sure the Timer Architectural Protocol is not already installed in the system\r
334 //\r
335 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);\r
336\r
337 //\r
338 // Get the CPU Architectural Protocol instance\r
339 //\r
340 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (void *)&mCpu);\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 Handle = NULL;\r
347 Status = gBS->InstallProtocolInterface (\r
348 &Handle,\r
349 &gEfiTimerArchProtocolGuid,\r
350 EFI_NATIVE_INTERFACE,\r
351 &mTimer\r
352 );\r
353 if (EFI_ERROR (Status)) {\r
354 return Status;\r
355 }\r
356\r
357 return EFI_SUCCESS;\r
358}\r