]>
Commit | Line | Data |
---|---|---|
9f38945f OM |
1 | /** @file\r |
2 | *\r | |
8fe18cba | 3 | * Copyright (c) 2013-2018, ARM Limited. All rights reserved.\r |
9f38945f | 4 | *\r |
4059386c | 5 | * SPDX-License-Identifier: BSD-2-Clause-Patent\r |
9f38945f OM |
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 | |
baee8efb | 21 | #include <Protocol/HardwareInterrupt2.h>\r |
9f38945f | 22 | #include <Protocol/WatchdogTimer.h>\r |
9f38945f | 23 | \r |
0b4d97a0 | 24 | #include "GenericWatchdog.h"\r |
9f38945f | 25 | \r |
b0393756 EL |
26 | /* The number of 100ns periods (the unit of time passed to these functions)\r |
27 | in a second */\r | |
429309e0 | 28 | #define TIME_UNITS_PER_SECOND 10000000\r |
9f38945f | 29 | \r |
b0393756 | 30 | // Tick frequency of the generic timer basis of the generic watchdog.\r |
429309e0 | 31 | STATIC UINTN mTimerFrequencyHz = 0;\r |
9f38945f | 32 | \r |
b0393756 EL |
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 | |
429309e0 | 36 | STATIC UINT64 mNumTimerTicks = 0;\r |
9f38945f | 37 | \r |
429309e0 MK |
38 | STATIC EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol;\r |
39 | STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify;\r | |
9f38945f | 40 | \r |
d3b05936 | 41 | STATIC\r |
8fe18cba | 42 | VOID\r |
9f38945f OM |
43 | WatchdogWriteOffsetRegister (\r |
44 | UINT32 Value\r | |
45 | )\r | |
46 | {\r | |
8fe18cba | 47 | MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);\r |
9f38945f OM |
48 | }\r |
49 | \r | |
d3b05936 | 50 | STATIC\r |
8fe18cba | 51 | VOID\r |
9f38945f OM |
52 | WatchdogWriteCompareRegister (\r |
53 | UINT64 Value\r | |
54 | )\r | |
55 | {\r | |
dd4cae4d MW |
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 | |
9f38945f OM |
58 | }\r |
59 | \r | |
d3b05936 | 60 | STATIC\r |
8fe18cba | 61 | VOID\r |
9f38945f OM |
62 | WatchdogEnable (\r |
63 | VOID\r | |
64 | )\r | |
65 | {\r | |
8fe18cba | 66 | MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);\r |
9f38945f OM |
67 | }\r |
68 | \r | |
d3b05936 | 69 | STATIC\r |
8fe18cba | 70 | VOID\r |
9f38945f OM |
71 | WatchdogDisable (\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 | |
d3b05936 | 81 | STATIC\r |
9f38945f OM |
82 | VOID\r |
83 | EFIAPI\r | |
84 | WatchdogExitBootServicesEvent (\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 | |
b0393756 EL |
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 | |
9f38945f | 95 | */\r |
d3b05936 | 96 | STATIC\r |
9f38945f OM |
97 | VOID\r |
98 | EFIAPI\r | |
99 | WatchdogInterruptHandler (\r | |
429309e0 MK |
100 | IN HARDWARE_INTERRUPT_SOURCE Source,\r |
101 | IN EFI_SYSTEM_CONTEXT SystemContext\r | |
9f38945f OM |
102 | )\r |
103 | {\r | |
429309e0 MK |
104 | STATIC CONST CHAR16 ResetString[] = L"The generic watchdog timer ran out.";\r |
105 | UINT64 TimerPeriod;\r | |
9f38945f OM |
106 | \r |
107 | WatchdogDisable ();\r | |
108 | \r | |
109 | mInterruptProtocol->EndOfInterrupt (mInterruptProtocol, Source);\r | |
110 | \r | |
ba808d11 AB |
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 | |
429309e0 MK |
122 | gRT->ResetSystem (\r |
123 | EfiResetCold,\r | |
124 | EFI_TIMEOUT,\r | |
125 | StrSize (ResetString),\r | |
126 | (CHAR16 *)ResetString\r | |
127 | );\r | |
9f38945f OM |
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 | |
b0393756 EL |
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 | |
9f38945f OM |
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 | |
b0393756 | 154 | @retval EFI_UNSUPPORTED The code does not support NotifyFunction.\r |
9f38945f OM |
155 | \r |
156 | **/\r | |
d3b05936 | 157 | STATIC\r |
9f38945f OM |
158 | EFI_STATUS\r |
159 | EFIAPI\r | |
160 | WatchdogRegisterHandler (\r | |
429309e0 MK |
161 | IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r |
162 | IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction\r | |
9f38945f OM |
163 | )\r |
164 | {\r | |
429309e0 | 165 | if ((mWatchdogNotify == NULL) && (NotifyFunction == NULL)) {\r |
ba808d11 AB |
166 | return EFI_INVALID_PARAMETER;\r |
167 | }\r | |
168 | \r | |
429309e0 | 169 | if ((mWatchdogNotify != NULL) && (NotifyFunction != NULL)) {\r |
ba808d11 AB |
170 | return EFI_ALREADY_STARTED;\r |
171 | }\r | |
172 | \r | |
173 | mWatchdogNotify = NotifyFunction;\r | |
174 | return EFI_SUCCESS;\r | |
9f38945f OM |
175 | }\r |
176 | \r | |
177 | /**\r | |
178 | This function sets the amount of time to wait before firing the watchdog\r | |
b0393756 | 179 | timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog\r |
9f38945f OM |
180 | timer is disabled.\r |
181 | \r | |
182 | @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r | |
b0393756 EL |
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 | |
9f38945f | 186 | \r |
b0393756 | 187 | @retval EFI_SUCCESS The watchdog timer has been programmed to fire\r |
8fe18cba | 188 | in TimerPeriod 100ns units.\r |
9f38945f OM |
189 | \r |
190 | **/\r | |
d3b05936 | 191 | STATIC\r |
9f38945f OM |
192 | EFI_STATUS\r |
193 | EFIAPI\r | |
194 | WatchdogSetTimerPeriod (\r | |
429309e0 MK |
195 | IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r |
196 | IN UINT64 TimerPeriod // In 100ns units\r | |
9f38945f OM |
197 | )\r |
198 | {\r | |
429309e0 | 199 | UINTN SystemCount;\r |
9f38945f | 200 | \r |
b0393756 | 201 | // if TimerPeriod is 0, this is a request to stop the watchdog.\r |
9f38945f OM |
202 | if (TimerPeriod == 0) {\r |
203 | mNumTimerTicks = 0;\r | |
8fe18cba A |
204 | WatchdogDisable ();\r |
205 | return EFI_SUCCESS;\r | |
9f38945f OM |
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 | |
b0393756 EL |
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 | |
9f38945f | 214 | if (mNumTimerTicks > MAX_UINT32) {\r |
b0393756 EL |
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 | |
8fe18cba | 219 | WatchdogWriteOffsetRegister (MAX_UINT32);\r |
9f38945f | 220 | WatchdogEnable ();\r |
23666400 | 221 | SystemCount = ArmGenericTimerGetSystemCount ();\r |
8fe18cba | 222 | WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);\r |
9f38945f | 223 | } else {\r |
8fe18cba | 224 | WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);\r |
9f38945f OM |
225 | WatchdogEnable ();\r |
226 | }\r | |
227 | \r | |
8fe18cba | 228 | return EFI_SUCCESS;\r |
9f38945f OM |
229 | }\r |
230 | \r | |
231 | /**\r | |
b0393756 | 232 | This function retrieves the period of timer interrupts in 100ns units,\r |
9f38945f OM |
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 | |
b0393756 EL |
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 | |
9f38945f OM |
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 | |
d3b05936 | 247 | STATIC\r |
9f38945f OM |
248 | EFI_STATUS\r |
249 | EFIAPI\r | |
250 | WatchdogGetTimerPeriod (\r | |
429309e0 MK |
251 | IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r |
252 | OUT UINT64 *TimerPeriod\r | |
9f38945f OM |
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 | |
b0393756 | 287 | Sets the period of the timer interrupt in 100ns units.\r |
9f38945f OM |
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 | |
b0393756 | 293 | Retrieves the period of the timer interrupt in 100ns units.\r |
9f38945f OM |
294 | \r |
295 | **/\r | |
429309e0 | 296 | STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {\r |
d3b05936 AB |
297 | WatchdogRegisterHandler,\r |
298 | WatchdogSetTimerPeriod,\r | |
299 | WatchdogGetTimerPeriod\r | |
9f38945f OM |
300 | };\r |
301 | \r | |
429309e0 | 302 | STATIC EFI_EVENT mEfiExitBootServicesEvent;\r |
9f38945f OM |
303 | \r |
304 | EFI_STATUS\r | |
305 | EFIAPI\r | |
306 | GenericWatchdogEntry (\r | |
429309e0 MK |
307 | IN EFI_HANDLE ImageHandle,\r |
308 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
9f38945f OM |
309 | )\r |
310 | {\r | |
429309e0 MK |
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 | |
d3b05936 AB |
319 | ASSERT_EFI_ERROR (Status);\r |
320 | \r | |
b0393756 EL |
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 | |
9f38945f OM |
324 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);\r |
325 | \r | |
326 | mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();\r | |
327 | ASSERT (mTimerFrequencyHz != 0);\r | |
328 | \r | |
d3b05936 | 329 | // Install interrupt handler\r |
429309e0 MK |
330 | Status = mInterruptProtocol->RegisterInterruptSource (\r |
331 | mInterruptProtocol,\r | |
d3b05936 | 332 | FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r |
429309e0 MK |
333 | WatchdogInterruptHandler\r |
334 | );\r | |
d3b05936 AB |
335 | if (EFI_ERROR (Status)) {\r |
336 | return Status;\r | |
337 | }\r | |
338 | \r | |
429309e0 MK |
339 | Status = mInterruptProtocol->SetTriggerType (\r |
340 | mInterruptProtocol,\r | |
d3b05936 | 341 | FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r |
429309e0 MK |
342 | EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING\r |
343 | );\r | |
d3b05936 AB |
344 | if (EFI_ERROR (Status)) {\r |
345 | goto UnregisterHandler;\r | |
9f38945f OM |
346 | }\r |
347 | \r | |
d3b05936 AB |
348 | // Install the Timer Architectural Protocol onto a new handle\r |
349 | Handle = NULL;\r | |
429309e0 MK |
350 | Status = gBS->InstallMultipleProtocolInterfaces (\r |
351 | &Handle,\r | |
352 | &gEfiWatchdogTimerArchProtocolGuid,\r | |
353 | &mWatchdogTimer,\r | |
354 | NULL\r | |
355 | );\r | |
d3b05936 AB |
356 | if (EFI_ERROR (Status)) {\r |
357 | goto UnregisterHandler;\r | |
358 | }\r | |
359 | \r | |
360 | // Register for an ExitBootServicesEvent\r | |
429309e0 MK |
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 | |
b0393756 | 368 | ASSERT_EFI_ERROR (Status);\r |
9f38945f OM |
369 | \r |
370 | mNumTimerTicks = 0;\r | |
371 | WatchdogDisable ();\r | |
372 | \r | |
d3b05936 AB |
373 | return EFI_SUCCESS;\r |
374 | \r | |
375 | UnregisterHandler:\r | |
376 | // Unregister the handler\r | |
429309e0 MK |
377 | mInterruptProtocol->RegisterInterruptSource (\r |
378 | mInterruptProtocol,\r | |
d3b05936 | 379 | FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r |
429309e0 MK |
380 | NULL\r |
381 | );\r | |
9f38945f OM |
382 | return Status;\r |
383 | }\r |