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