]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 (\r
123 EfiResetCold,\r
124 EFI_TIMEOUT,\r
125 StrSize (ResetString),\r
126 (CHAR16 *)ResetString\r
127 );\r
128\r
129 // If we got here then the reset didn't work\r
130 ASSERT (FALSE);\r
131}\r
132\r
133/**\r
134 This function registers the handler NotifyFunction so it is called every time\r
135 the watchdog timer expires. It also passes the amount of time since the last\r
136 handler call to the NotifyFunction.\r
137 If NotifyFunction is not NULL and a handler is not already registered,\r
138 then the new handler is registered and EFI_SUCCESS is returned.\r
139 If NotifyFunction is NULL, and a handler is already registered,\r
140 then that handler is unregistered.\r
141 If an attempt is made to register a handler when a handler is already\r
142 registered, then EFI_ALREADY_STARTED is returned.\r
143 If an attempt is made to unregister a handler when a handler is not\r
144 registered, then EFI_INVALID_PARAMETER is returned.\r
145\r
146 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
147 @param NotifyFunction The function to call when a timer interrupt fires.\r
148 This function executes at TPL_HIGH_LEVEL. The DXE\r
149 Core will register a handler for the timer interrupt,\r
150 so it can know how much time has passed. This\r
151 information is used to signal timer based events.\r
152 NULL will unregister the handler.\r
153\r
154 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.\r
155\r
156**/\r
157STATIC\r
158EFI_STATUS\r
159EFIAPI\r
160WatchdogRegisterHandler (\r
161 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
162 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction\r
163 )\r
164{\r
165 if ((mWatchdogNotify == NULL) && (NotifyFunction == NULL)) {\r
166 return EFI_INVALID_PARAMETER;\r
167 }\r
168\r
169 if ((mWatchdogNotify != NULL) && (NotifyFunction != NULL)) {\r
170 return EFI_ALREADY_STARTED;\r
171 }\r
172\r
173 mWatchdogNotify = NotifyFunction;\r
174 return EFI_SUCCESS;\r
175}\r
176\r
177/**\r
178 This function sets the amount of time to wait before firing the watchdog\r
179 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog\r
180 timer is disabled.\r
181\r
182 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r
183 @param TimerPeriod The amount of time in 100ns units to wait before\r
184 the watchdog timer is fired. If TimerPeriod is zero,\r
185 then the watchdog timer is disabled.\r
186\r
187 @retval EFI_SUCCESS The watchdog timer has been programmed to fire\r
188 in TimerPeriod 100ns units.\r
189\r
190**/\r
191STATIC\r
192EFI_STATUS\r
193EFIAPI\r
194WatchdogSetTimerPeriod (\r
195 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
196 IN UINT64 TimerPeriod // In 100ns units\r
197 )\r
198{\r
199 UINTN SystemCount;\r
200\r
201 // if TimerPeriod is 0, this is a request to stop the watchdog.\r
202 if (TimerPeriod == 0) {\r
203 mNumTimerTicks = 0;\r
204 WatchdogDisable ();\r
205 return EFI_SUCCESS;\r
206 }\r
207\r
208 // Work out how many timer ticks will equate to TimerPeriod\r
209 mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;\r
210\r
211 /* If the number of required ticks is greater than the max the watchdog's\r
212 offset register (WOR) can hold, we need to manually compute and set\r
213 the compare register (WCV) */\r
214 if (mNumTimerTicks > MAX_UINT32) {\r
215 /* We need to enable the watchdog *before* writing to the compare register,\r
216 because enabling the watchdog causes an "explicit refresh", which\r
217 clobbers the compare register (WCV). In order to make sure this doesn't\r
218 trigger an interrupt, set the offset to max. */\r
219 WatchdogWriteOffsetRegister (MAX_UINT32);\r
220 WatchdogEnable ();\r
221 SystemCount = ArmGenericTimerGetSystemCount ();\r
222 WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);\r
223 } else {\r
224 WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);\r
225 WatchdogEnable ();\r
226 }\r
227\r
228 return EFI_SUCCESS;\r
229}\r
230\r
231/**\r
232 This function retrieves the period of timer interrupts in 100ns units,\r
233 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r
234 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r
235 returned, then the timer is currently disabled.\r
236\r
237 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
238 @param TimerPeriod A pointer to the timer period to retrieve in\r
239 100ns units. If 0 is returned, then the timer is\r
240 currently disabled.\r
241\r
242\r
243 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r
244 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r
245\r
246**/\r
247STATIC\r
248EFI_STATUS\r
249EFIAPI\r
250WatchdogGetTimerPeriod (\r
251 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
252 OUT UINT64 *TimerPeriod\r
253 )\r
254{\r
255 if (TimerPeriod == NULL) {\r
256 return EFI_INVALID_PARAMETER;\r
257 }\r
258\r
259 *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);\r
260\r
261 return EFI_SUCCESS;\r
262}\r
263\r
264/**\r
265 Interface structure for the Watchdog Architectural Protocol.\r
266\r
267 @par Protocol Description:\r
268 This protocol provides a service to set the amount of time to wait\r
269 before firing the watchdog timer, and it also provides a service to\r
270 register a handler that is invoked when the watchdog timer fires.\r
271\r
272 @par When the watchdog timer fires, control will be passed to a handler\r
273 if one has been registered. If no handler has been registered,\r
274 or the registered handler returns, then the system will be\r
275 reset by calling the Runtime Service ResetSystem().\r
276\r
277 @param RegisterHandler\r
278 Registers a handler that will be called each time the\r
279 watchdogtimer interrupt fires. TimerPeriod defines the minimum\r
280 time between timer interrupts, so TimerPeriod will also\r
281 be the minimum time between calls to the registered\r
282 handler.\r
283 NOTE: If the watchdog resets the system in hardware, then\r
284 this function will not have any chance of executing.\r
285\r
286 @param SetTimerPeriod\r
287 Sets the period of the timer interrupt in 100ns units.\r
288 This function is optional, and may return EFI_UNSUPPORTED.\r
289 If this function is supported, then the timer period will\r
290 be rounded up to the nearest supported timer period.\r
291\r
292 @param GetTimerPeriod\r
293 Retrieves the period of the timer interrupt in 100ns units.\r
294\r
295**/\r
296STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {\r
297 WatchdogRegisterHandler,\r
298 WatchdogSetTimerPeriod,\r
299 WatchdogGetTimerPeriod\r
300};\r
301\r
302STATIC EFI_EVENT mEfiExitBootServicesEvent;\r
303\r
304EFI_STATUS\r
305EFIAPI\r
306GenericWatchdogEntry (\r
307 IN EFI_HANDLE ImageHandle,\r
308 IN EFI_SYSTEM_TABLE *SystemTable\r
309 )\r
310{\r
311 EFI_STATUS Status;\r
312 EFI_HANDLE Handle;\r
313\r
314 Status = gBS->LocateProtocol (\r
315 &gHardwareInterrupt2ProtocolGuid,\r
316 NULL,\r
317 (VOID **)&mInterruptProtocol\r
318 );\r
319 ASSERT_EFI_ERROR (Status);\r
320\r
321 /* Make sure the Watchdog Timer Architectural Protocol has not been installed\r
322 in the system yet.\r
323 This will avoid conflicts with the universal watchdog */\r
324 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);\r
325\r
326 mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();\r
327 ASSERT (mTimerFrequencyHz != 0);\r
328\r
329 // Install interrupt handler\r
330 Status = mInterruptProtocol->RegisterInterruptSource (\r
331 mInterruptProtocol,\r
332 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
333 WatchdogInterruptHandler\r
334 );\r
335 if (EFI_ERROR (Status)) {\r
336 return Status;\r
337 }\r
338\r
339 Status = mInterruptProtocol->SetTriggerType (\r
340 mInterruptProtocol,\r
341 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
342 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING\r
343 );\r
344 if (EFI_ERROR (Status)) {\r
345 goto UnregisterHandler;\r
346 }\r
347\r
348 // Install the Timer Architectural Protocol onto a new handle\r
349 Handle = NULL;\r
350 Status = gBS->InstallMultipleProtocolInterfaces (\r
351 &Handle,\r
352 &gEfiWatchdogTimerArchProtocolGuid,\r
353 &mWatchdogTimer,\r
354 NULL\r
355 );\r
356 if (EFI_ERROR (Status)) {\r
357 goto UnregisterHandler;\r
358 }\r
359\r
360 // Register for an ExitBootServicesEvent\r
361 Status = gBS->CreateEvent (\r
362 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
363 TPL_NOTIFY,\r
364 WatchdogExitBootServicesEvent,\r
365 NULL,\r
366 &mEfiExitBootServicesEvent\r
367 );\r
368 ASSERT_EFI_ERROR (Status);\r
369\r
370 mNumTimerTicks = 0;\r
371 WatchdogDisable ();\r
372\r
373 return EFI_SUCCESS;\r
374\r
375UnregisterHandler:\r
376 // Unregister the handler\r
377 mInterruptProtocol->RegisterInterruptSource (\r
378 mInterruptProtocol,\r
379 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
380 NULL\r
381 );\r
382 return Status;\r
383}\r