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