]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
UefiCpuPkg: Change complex DEBUG_CODE() to DEBUG_CODE_BEGIN/END()
[mirror_edk2.git] / ArmPkg / Drivers / GenericWatchdogDxe / GenericWatchdogDxe.c
... / ...
CommitLineData
1/** @file\r
2*\r
3* Copyright (c) 2013-2018, ARM Limited. All rights reserved.\r
4*\r
5* SPDX-License-Identifier: BSD-2-Clause-Patent\r
6*\r
7**/\r
8\r
9#include <PiDxe.h>\r
10\r
11#include <Library/BaseLib.h>\r
12#include <Library/BaseMemoryLib.h>\r
13#include <Library/DebugLib.h>\r
14#include <Library/IoLib.h>\r
15#include <Library/PcdLib.h>\r
16#include <Library/UefiBootServicesTableLib.h>\r
17#include <Library/UefiRuntimeServicesTableLib.h>\r
18#include <Library/UefiLib.h>\r
19#include <Library/ArmGenericTimerCounterLib.h>\r
20\r
21#include <Protocol/HardwareInterrupt2.h>\r
22#include <Protocol/WatchdogTimer.h>\r
23\r
24#include "GenericWatchdog.h"\r
25\r
26/* The number of 100ns periods (the unit of time passed to these functions)\r
27 in a second */\r
28#define TIME_UNITS_PER_SECOND 10000000\r
29\r
30// Tick frequency of the generic timer basis of the generic watchdog.\r
31STATIC UINTN mTimerFrequencyHz = 0;\r
32\r
33/* In cases where the compare register was set manually, information about\r
34 how long the watchdog was asked to wait cannot be retrieved from hardware.\r
35 It is therefore stored here. 0 means the timer is not running. */\r
36STATIC UINT64 mNumTimerTicks = 0;\r
37\r
38STATIC EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol;\r
39STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify;\r
40\r
41STATIC\r
42VOID\r
43WatchdogWriteOffsetRegister (\r
44 UINT32 Value\r
45 )\r
46{\r
47 MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);\r
48}\r
49\r
50STATIC\r
51VOID\r
52WatchdogWriteCompareRegister (\r
53 UINT64 Value\r
54 )\r
55{\r
56 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_LOW, Value & MAX_UINT32);\r
57 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_HIGH, (Value >> 32) & MAX_UINT32);\r
58}\r
59\r
60STATIC\r
61VOID\r
62WatchdogEnable (\r
63 VOID\r
64 )\r
65{\r
66 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);\r
67}\r
68\r
69STATIC\r
70VOID\r
71WatchdogDisable (\r
72 VOID\r
73 )\r
74{\r
75 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);\r
76}\r
77\r
78/** On exiting boot services we must make sure the Watchdog Timer\r
79 is stopped.\r
80**/\r
81STATIC\r
82VOID\r
83EFIAPI\r
84WatchdogExitBootServicesEvent (\r
85 IN EFI_EVENT Event,\r
86 IN VOID *Context\r
87 )\r
88{\r
89 WatchdogDisable ();\r
90 mNumTimerTicks = 0;\r
91}\r
92\r
93/* This function is called when the watchdog's first signal (WS0) goes high.\r
94 It uses the ResetSystem Runtime Service to reset the board.\r
95*/\r
96STATIC\r
97VOID\r
98EFIAPI\r
99WatchdogInterruptHandler (\r
100 IN HARDWARE_INTERRUPT_SOURCE Source,\r
101 IN EFI_SYSTEM_CONTEXT SystemContext\r
102 )\r
103{\r
104 STATIC CONST CHAR16 ResetString[]= L"The generic watchdog timer ran out.";\r
105 UINT64 TimerPeriod;\r
106\r
107 WatchdogDisable ();\r
108\r
109 mInterruptProtocol->EndOfInterrupt (mInterruptProtocol, Source);\r
110\r
111 //\r
112 // The notify function should be called with the elapsed number of ticks\r
113 // since the watchdog was armed, which should exceed the timer period.\r
114 // We don't actually know the elapsed number of ticks, so let's return\r
115 // the timer period plus 1.\r
116 //\r
117 if (mWatchdogNotify != NULL) {\r
118 TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);\r
119 mWatchdogNotify (TimerPeriod + 1);\r
120 }\r
121\r
122 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, StrSize (ResetString),\r
123 (CHAR16 *)ResetString);\r
124\r
125 // If we got here then the reset didn't work\r
126 ASSERT (FALSE);\r
127}\r
128\r
129/**\r
130 This function registers the handler NotifyFunction so it is called every time\r
131 the watchdog timer expires. It also passes the amount of time since the last\r
132 handler call to the NotifyFunction.\r
133 If NotifyFunction is not NULL and a handler is not already registered,\r
134 then the new handler is registered and EFI_SUCCESS is returned.\r
135 If NotifyFunction is NULL, and a handler is already registered,\r
136 then that handler is unregistered.\r
137 If an attempt is made to register a handler when a handler is already\r
138 registered, then EFI_ALREADY_STARTED is returned.\r
139 If an attempt is made to unregister a handler when a handler is not\r
140 registered, then EFI_INVALID_PARAMETER is returned.\r
141\r
142 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
143 @param NotifyFunction The function to call when a timer interrupt fires.\r
144 This function executes at TPL_HIGH_LEVEL. The DXE\r
145 Core will register a handler for the timer interrupt,\r
146 so it can know how much time has passed. This\r
147 information is used to signal timer based events.\r
148 NULL will unregister the handler.\r
149\r
150 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.\r
151\r
152**/\r
153STATIC\r
154EFI_STATUS\r
155EFIAPI\r
156WatchdogRegisterHandler (\r
157 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
158 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction\r
159 )\r
160{\r
161 if (mWatchdogNotify == NULL && NotifyFunction == NULL) {\r
162 return EFI_INVALID_PARAMETER;\r
163 }\r
164\r
165 if (mWatchdogNotify != NULL && NotifyFunction != NULL) {\r
166 return EFI_ALREADY_STARTED;\r
167 }\r
168\r
169 mWatchdogNotify = NotifyFunction;\r
170 return EFI_SUCCESS;\r
171}\r
172\r
173/**\r
174 This function sets the amount of time to wait before firing the watchdog\r
175 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog\r
176 timer is disabled.\r
177\r
178 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r
179 @param TimerPeriod The amount of time in 100ns units to wait before\r
180 the watchdog timer is fired. If TimerPeriod is zero,\r
181 then the watchdog timer is disabled.\r
182\r
183 @retval EFI_SUCCESS The watchdog timer has been programmed to fire\r
184 in TimerPeriod 100ns units.\r
185\r
186**/\r
187STATIC\r
188EFI_STATUS\r
189EFIAPI\r
190WatchdogSetTimerPeriod (\r
191 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
192 IN UINT64 TimerPeriod // In 100ns units\r
193 )\r
194{\r
195 UINTN SystemCount;\r
196\r
197 // if TimerPeriod is 0, this is a request to stop the watchdog.\r
198 if (TimerPeriod == 0) {\r
199 mNumTimerTicks = 0;\r
200 WatchdogDisable ();\r
201 return EFI_SUCCESS;\r
202 }\r
203\r
204 // Work out how many timer ticks will equate to TimerPeriod\r
205 mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;\r
206\r
207 /* If the number of required ticks is greater than the max the watchdog's\r
208 offset register (WOR) can hold, we need to manually compute and set\r
209 the compare register (WCV) */\r
210 if (mNumTimerTicks > MAX_UINT32) {\r
211 /* We need to enable the watchdog *before* writing to the compare register,\r
212 because enabling the watchdog causes an "explicit refresh", which\r
213 clobbers the compare register (WCV). In order to make sure this doesn't\r
214 trigger an interrupt, set the offset to max. */\r
215 WatchdogWriteOffsetRegister (MAX_UINT32);\r
216 WatchdogEnable ();\r
217 SystemCount = ArmGenericTimerGetSystemCount ();\r
218 WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);\r
219 } else {\r
220 WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);\r
221 WatchdogEnable ();\r
222 }\r
223\r
224 return EFI_SUCCESS;\r
225}\r
226\r
227/**\r
228 This function retrieves the period of timer interrupts in 100ns units,\r
229 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r
230 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r
231 returned, then the timer is currently disabled.\r
232\r
233 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
234 @param TimerPeriod A pointer to the timer period to retrieve in\r
235 100ns units. If 0 is returned, then the timer is\r
236 currently disabled.\r
237\r
238\r
239 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r
240 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r
241\r
242**/\r
243STATIC\r
244EFI_STATUS\r
245EFIAPI\r
246WatchdogGetTimerPeriod (\r
247 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
248 OUT UINT64 *TimerPeriod\r
249 )\r
250{\r
251 if (TimerPeriod == NULL) {\r
252 return EFI_INVALID_PARAMETER;\r
253 }\r
254\r
255 *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);\r
256\r
257 return EFI_SUCCESS;\r
258}\r
259\r
260/**\r
261 Interface structure for the Watchdog Architectural Protocol.\r
262\r
263 @par Protocol Description:\r
264 This protocol provides a service to set the amount of time to wait\r
265 before firing the watchdog timer, and it also provides a service to\r
266 register a handler that is invoked when the watchdog timer fires.\r
267\r
268 @par When the watchdog timer fires, control will be passed to a handler\r
269 if one has been registered. If no handler has been registered,\r
270 or the registered handler returns, then the system will be\r
271 reset by calling the Runtime Service ResetSystem().\r
272\r
273 @param RegisterHandler\r
274 Registers a handler that will be called each time the\r
275 watchdogtimer interrupt fires. TimerPeriod defines the minimum\r
276 time between timer interrupts, so TimerPeriod will also\r
277 be the minimum time between calls to the registered\r
278 handler.\r
279 NOTE: If the watchdog resets the system in hardware, then\r
280 this function will not have any chance of executing.\r
281\r
282 @param SetTimerPeriod\r
283 Sets the period of the timer interrupt in 100ns units.\r
284 This function is optional, and may return EFI_UNSUPPORTED.\r
285 If this function is supported, then the timer period will\r
286 be rounded up to the nearest supported timer period.\r
287\r
288 @param GetTimerPeriod\r
289 Retrieves the period of the timer interrupt in 100ns units.\r
290\r
291**/\r
292STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {\r
293 WatchdogRegisterHandler,\r
294 WatchdogSetTimerPeriod,\r
295 WatchdogGetTimerPeriod\r
296};\r
297\r
298STATIC EFI_EVENT mEfiExitBootServicesEvent;\r
299\r
300EFI_STATUS\r
301EFIAPI\r
302GenericWatchdogEntry (\r
303 IN EFI_HANDLE ImageHandle,\r
304 IN EFI_SYSTEM_TABLE *SystemTable\r
305 )\r
306{\r
307 EFI_STATUS Status;\r
308 EFI_HANDLE Handle;\r
309\r
310 Status = gBS->LocateProtocol (&gHardwareInterrupt2ProtocolGuid, NULL,\r
311 (VOID **)&mInterruptProtocol);\r
312 ASSERT_EFI_ERROR (Status);\r
313\r
314 /* Make sure the Watchdog Timer Architectural Protocol has not been installed\r
315 in the system yet.\r
316 This will avoid conflicts with the universal watchdog */\r
317 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);\r
318\r
319 mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();\r
320 ASSERT (mTimerFrequencyHz != 0);\r
321\r
322 // Install interrupt handler\r
323 Status = mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,\r
324 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
325 WatchdogInterruptHandler);\r
326 if (EFI_ERROR (Status)) {\r
327 return Status;\r
328 }\r
329\r
330 Status = mInterruptProtocol->SetTriggerType (mInterruptProtocol,\r
331 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
332 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING);\r
333 if (EFI_ERROR (Status)) {\r
334 goto UnregisterHandler;\r
335 }\r
336\r
337 // Install the Timer Architectural Protocol onto a new handle\r
338 Handle = NULL;\r
339 Status = gBS->InstallMultipleProtocolInterfaces (&Handle,\r
340 &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer,\r
341 NULL);\r
342 if (EFI_ERROR (Status)) {\r
343 goto UnregisterHandler;\r
344 }\r
345\r
346 // Register for an ExitBootServicesEvent\r
347 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,\r
348 WatchdogExitBootServicesEvent, NULL,\r
349 &mEfiExitBootServicesEvent);\r
350 ASSERT_EFI_ERROR (Status);\r
351\r
352 mNumTimerTicks = 0;\r
353 WatchdogDisable ();\r
354\r
355 return EFI_SUCCESS;\r
356\r
357UnregisterHandler:\r
358 // Unregister the handler\r
359 mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,\r
360 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
361 NULL);\r
362 return Status;\r
363}\r