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