]>
Commit | Line | Data |
---|---|---|
9f38945f OM |
1 | /** @file\r |
2 | *\r | |
b0393756 | 3 | * Copyright (c) 2013-2017, 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 |
37 | UINTN 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 |
42 | UINT64 mNumTimerTicks = 0;\r |
43 | \r | |
baee8efb | 44 | EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol;\r |
9f38945f OM |
45 | \r |
46 | EFI_STATUS\r | |
47 | WatchdogWriteOffsetRegister (\r | |
48 | UINT32 Value\r | |
49 | )\r | |
50 | {\r | |
51 | return MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);\r | |
52 | }\r | |
53 | \r | |
54 | EFI_STATUS\r | |
55 | WatchdogWriteCompareRegister (\r | |
56 | UINT64 Value\r | |
57 | )\r | |
58 | {\r | |
59 | return MmioWrite64 (GENERIC_WDOG_COMPARE_VALUE_REG, Value);\r | |
60 | }\r | |
61 | \r | |
62 | EFI_STATUS\r | |
63 | WatchdogEnable (\r | |
64 | VOID\r | |
65 | )\r | |
66 | {\r | |
67 | return MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);\r | |
68 | }\r | |
69 | \r | |
70 | EFI_STATUS\r | |
71 | WatchdogDisable (\r | |
72 | VOID\r | |
73 | )\r | |
74 | {\r | |
75 | return MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);\r | |
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 | |
81 | VOID\r | |
82 | EFIAPI\r | |
83 | WatchdogExitBootServicesEvent (\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 |
95 | VOID\r | |
96 | EFIAPI\r | |
97 | WatchdogInterruptHandler (\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 | |
143 | EFI_STATUS\r | |
144 | EFIAPI\r | |
145 | WatchdogRegisterHandler (\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 EL |
165 | @retval EFI_SUCCESS The watchdog timer has been programmed to fire\r |
166 | in Time 100ns units.\r | |
167 | @retval EFI_DEVICE_ERROR A watchdog timer could not be programmed due\r | |
168 | to a device error.\r | |
9f38945f OM |
169 | \r |
170 | **/\r | |
171 | EFI_STATUS\r | |
172 | EFIAPI\r | |
173 | WatchdogSetTimerPeriod (\r | |
174 | IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r | |
175 | IN UINT64 TimerPeriod // In 100ns units\r | |
176 | )\r | |
177 | {\r | |
23666400 | 178 | UINTN SystemCount;\r |
9f38945f OM |
179 | EFI_STATUS Status;\r |
180 | \r | |
b0393756 | 181 | // if TimerPeriod is 0, this is a request to stop the watchdog.\r |
9f38945f OM |
182 | if (TimerPeriod == 0) {\r |
183 | mNumTimerTicks = 0;\r | |
184 | return WatchdogDisable ();\r | |
185 | }\r | |
186 | \r | |
187 | // Work out how many timer ticks will equate to TimerPeriod\r | |
188 | mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;\r | |
189 | \r | |
b0393756 EL |
190 | /* If the number of required ticks is greater than the max the watchdog's\r |
191 | offset register (WOR) can hold, we need to manually compute and set\r | |
192 | the compare register (WCV) */\r | |
9f38945f | 193 | if (mNumTimerTicks > MAX_UINT32) {\r |
b0393756 EL |
194 | /* We need to enable the watchdog *before* writing to the compare register,\r |
195 | because enabling the watchdog causes an "explicit refresh", which\r | |
196 | clobbers the compare register (WCV). In order to make sure this doesn't\r | |
197 | trigger an interrupt, set the offset to max. */\r | |
9f38945f OM |
198 | Status = WatchdogWriteOffsetRegister (MAX_UINT32);\r |
199 | if (EFI_ERROR (Status)) {\r | |
200 | return Status;\r | |
201 | }\r | |
202 | WatchdogEnable ();\r | |
23666400 RC |
203 | SystemCount = ArmGenericTimerGetSystemCount ();\r |
204 | Status = WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);\r | |
9f38945f OM |
205 | } else {\r |
206 | Status = WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);\r | |
207 | WatchdogEnable ();\r | |
208 | }\r | |
209 | \r | |
210 | return Status;\r | |
211 | }\r | |
212 | \r | |
213 | /**\r | |
b0393756 | 214 | This function retrieves the period of timer interrupts in 100ns units,\r |
9f38945f OM |
215 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r |
216 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r | |
217 | returned, then the timer is currently disabled.\r | |
218 | \r | |
219 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
b0393756 EL |
220 | @param TimerPeriod A pointer to the timer period to retrieve in\r |
221 | 100ns units. If 0 is returned, then the timer is\r | |
9f38945f OM |
222 | currently disabled.\r |
223 | \r | |
224 | \r | |
225 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r | |
226 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r | |
227 | \r | |
228 | **/\r | |
229 | EFI_STATUS\r | |
230 | EFIAPI\r | |
231 | WatchdogGetTimerPeriod (\r | |
232 | IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r | |
233 | OUT UINT64 *TimerPeriod\r | |
234 | )\r | |
235 | {\r | |
236 | if (TimerPeriod == NULL) {\r | |
237 | return EFI_INVALID_PARAMETER;\r | |
238 | }\r | |
239 | \r | |
240 | *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);\r | |
241 | \r | |
242 | return EFI_SUCCESS;\r | |
243 | }\r | |
244 | \r | |
245 | /**\r | |
246 | Interface structure for the Watchdog Architectural Protocol.\r | |
247 | \r | |
248 | @par Protocol Description:\r | |
249 | This protocol provides a service to set the amount of time to wait\r | |
250 | before firing the watchdog timer, and it also provides a service to\r | |
251 | register a handler that is invoked when the watchdog timer fires.\r | |
252 | \r | |
253 | @par When the watchdog timer fires, control will be passed to a handler\r | |
254 | if one has been registered. If no handler has been registered,\r | |
255 | or the registered handler returns, then the system will be\r | |
256 | reset by calling the Runtime Service ResetSystem().\r | |
257 | \r | |
258 | @param RegisterHandler\r | |
259 | Registers a handler that will be called each time the\r | |
260 | watchdogtimer interrupt fires. TimerPeriod defines the minimum\r | |
261 | time between timer interrupts, so TimerPeriod will also\r | |
262 | be the minimum time between calls to the registered\r | |
263 | handler.\r | |
264 | NOTE: If the watchdog resets the system in hardware, then\r | |
265 | this function will not have any chance of executing.\r | |
266 | \r | |
267 | @param SetTimerPeriod\r | |
b0393756 | 268 | Sets the period of the timer interrupt in 100ns units.\r |
9f38945f OM |
269 | This function is optional, and may return EFI_UNSUPPORTED.\r |
270 | If this function is supported, then the timer period will\r | |
271 | be rounded up to the nearest supported timer period.\r | |
272 | \r | |
273 | @param GetTimerPeriod\r | |
b0393756 | 274 | Retrieves the period of the timer interrupt in 100ns units.\r |
9f38945f OM |
275 | \r |
276 | **/\r | |
277 | EFI_WATCHDOG_TIMER_ARCH_PROTOCOL gWatchdogTimer = {\r | |
b0393756 EL |
278 | (EFI_WATCHDOG_TIMER_REGISTER_HANDLER)WatchdogRegisterHandler,\r |
279 | (EFI_WATCHDOG_TIMER_SET_TIMER_PERIOD)WatchdogSetTimerPeriod,\r | |
280 | (EFI_WATCHDOG_TIMER_GET_TIMER_PERIOD)WatchdogGetTimerPeriod\r | |
9f38945f OM |
281 | };\r |
282 | \r | |
283 | EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)NULL;\r | |
284 | \r | |
285 | EFI_STATUS\r | |
286 | EFIAPI\r | |
287 | GenericWatchdogEntry (\r | |
288 | IN EFI_HANDLE ImageHandle,\r | |
289 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
290 | )\r | |
291 | {\r | |
292 | EFI_STATUS Status;\r | |
293 | EFI_HANDLE Handle;\r | |
294 | \r | |
b0393756 EL |
295 | /* Make sure the Watchdog Timer Architectural Protocol has not been installed\r |
296 | in the system yet.\r | |
297 | This will avoid conflicts with the universal watchdog */\r | |
9f38945f OM |
298 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);\r |
299 | \r | |
300 | mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();\r | |
301 | ASSERT (mTimerFrequencyHz != 0);\r | |
302 | \r | |
303 | // Register for an ExitBootServicesEvent\r | |
304 | Status = gBS->CreateEvent (\r | |
b0393756 EL |
305 | EVT_SIGNAL_EXIT_BOOT_SERVICES,\r |
306 | TPL_NOTIFY,\r | |
307 | WatchdogExitBootServicesEvent,\r | |
308 | NULL,\r | |
309 | &EfiExitBootServicesEvent\r | |
9f38945f OM |
310 | );\r |
311 | if (!EFI_ERROR (Status)) {\r | |
312 | // Install interrupt handler\r | |
313 | Status = gBS->LocateProtocol (\r | |
baee8efb | 314 | &gHardwareInterrupt2ProtocolGuid,\r |
9f38945f OM |
315 | NULL,\r |
316 | (VOID **)&mInterruptProtocol\r | |
317 | );\r | |
318 | if (!EFI_ERROR (Status)) {\r | |
319 | Status = mInterruptProtocol->RegisterInterruptSource (\r | |
b0393756 EL |
320 | mInterruptProtocol,\r |
321 | FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r | |
322 | WatchdogInterruptHandler\r | |
323 | );\r | |
9f38945f | 324 | if (!EFI_ERROR (Status)) {\r |
baee8efb AB |
325 | Status = mInterruptProtocol->SetTriggerType (\r |
326 | mInterruptProtocol,\r | |
327 | FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r | |
328 | EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING\r | |
329 | );\r | |
330 | if (!EFI_ERROR (Status)) {\r | |
331 | // Install the Timer Architectural Protocol onto a new handle\r | |
332 | Handle = NULL;\r | |
333 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
334 | &Handle,\r | |
335 | &gEfiWatchdogTimerArchProtocolGuid,\r | |
336 | &gWatchdogTimer,\r | |
337 | NULL\r | |
338 | );\r | |
339 | }\r | |
9f38945f OM |
340 | }\r |
341 | }\r | |
342 | }\r | |
343 | \r | |
b0393756 | 344 | ASSERT_EFI_ERROR (Status);\r |
9f38945f OM |
345 | \r |
346 | mNumTimerTicks = 0;\r | |
347 | WatchdogDisable ();\r | |
348 | \r | |
349 | return Status;\r | |
350 | }\r |