summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Universal/WatchdogTimerDxe/WatchdogTimer.c
blob: 7762933e597271f38df23ada1cd574fdc6e6ef44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*++

Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

Module Name:

  WatchdogTimer.c

Abstract:

 Generic watchdog timer implemenetation using EFI APIs

Revision History

--*/

#include "WatchdogTimer.h"

//
// Handle for the Watchdog Timer Architectural Protocol instance produced by this driver
//
EFI_HANDLE                        mWatchdogTimerHandle = NULL;

//
// The Watchdog Timer Architectural Protocol instance produced by this driver
//
EFI_WATCHDOG_TIMER_ARCH_PROTOCOL  mWatchdogTimer = {
  WatchdogTimerDriverRegisterHandler,
  WatchdogTimerDriverSetTimerPeriod,
  WatchdogTimerDriverGetTimerPeriod
};

//
// The watchdog timer period in 100 nS units
//
UINT64                            mWatchdogTimerPeriod = 0;

//
// The notification function to call if the watchdig timer fires
//
EFI_WATCHDOG_TIMER_NOTIFY         mWatchdogTimerNotifyFunction = NULL;

//
// The one-shot timer event that is armed when the watchdog timer is enabled
//
EFI_EVENT                         mWatchdogTimerEvent;

//
// Worker Functions
//
STATIC
VOID
EFIAPI
WatchdogTimerDriverExpires (
  IN EFI_EVENT    Timer,
  IN VOID         *Context
  )
/*++

  Routine Description:

    Notification function that is called if the watchdog timer is fired.  If a
    handler has been registered with the Watchdog Timer Architectural Protocol,
    then that handler is called passing in the time period that has passed that
    cause the watchdog timer to fire.  Then, a call to the Runtime Service
    ResetSystem() is made to reset the platform.

  Arguments:

    Timer   - The one-shot timer event that was signaled when the watchdog timer
              expired.

    Context - The context that was registered when the event Timer was created.

  Returns:

    None.

--*/
{
  REPORT_STATUS_CODE (EFI_ERROR_CODE | EFI_ERROR_MINOR, PcdGet32 (PcdStatusCodeValueEfiWatchDogTimerExpired));

  //
  // If a notification function has been registered, then call it
  //
  if (mWatchdogTimerNotifyFunction != NULL) {
    mWatchdogTimerNotifyFunction (mWatchdogTimerPeriod);
  }
  //
  // Reset the platform
  //
  gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL);
}


EFI_STATUS
EFIAPI
WatchdogTimerDriverRegisterHandler (
  IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL  *This,
  IN EFI_WATCHDOG_TIMER_NOTIFY         NotifyFunction
  )
/*++

Routine Description:

  This function registers a handler that is to be invoked when the watchdog
  timer fires.  By default, the EFI_WATCHDOG_TIMER protocol will call the
  Runtime Service ResetSystem() when the watchdog timer fires.  If a
  NotifyFunction is registered, then the NotifyFunction will be called before
  the Runtime Service ResetSystem() is called.  If NotifyFunction is NULL, then
  the watchdog handler is unregistered.  If a watchdog handler is registered,
  then EFI_SUCCESS is returned.  If an attempt is made to register a handler
  when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  If an attempt is made to uninstall a handler when a handler is not installed,
  then return EFI_INVALID_PARAMETER.

Arguments:

  This           - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.

  NotifyFunction - The function to call when the watchdog timer fires.  If this
                   is NULL, then the handler will be unregistered.

Returns:

  EFI_SUCCESS           - The watchdog timer handler was registered or
                          unregistered.

  EFI_ALREADY_STARTED   - NotifyFunction is not NULL, and a handler is already
                          registered.

  EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
                          previously registered.

--*/
{
  if (NotifyFunction == NULL && mWatchdogTimerNotifyFunction == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  if (NotifyFunction != NULL && mWatchdogTimerNotifyFunction != NULL) {
    return EFI_ALREADY_STARTED;
  }

  mWatchdogTimerNotifyFunction = NotifyFunction;

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
WatchdogTimerDriverSetTimerPeriod (
  IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL  *This,
  IN UINT64                            TimerPeriod
  )
/*++

Routine Description:

  This function sets the amount of time to wait before firing the watchdog
  timer to TimerPeriod 100 nS units.  If TimerPeriod is 0, then the watchdog
  timer is disabled.

Arguments:

  This        - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.

  TimerPeriod - The amount of time in 100 nS units to wait before the watchdog
                timer is fired.  If TimerPeriod is zero, then the watchdog
                timer is disabled.

Returns:

  EFI_SUCCESS      - The watchdog timer has been programmed to fire in Time
                     100 nS units.

  EFI_DEVICE_ERROR - A watchdog timer could not be programmed due to a device
                     error.

--*/
{
  mWatchdogTimerPeriod = TimerPeriod;

  return gBS->SetTimer (
                mWatchdogTimerEvent,
                (mWatchdogTimerPeriod == 0) ? TimerCancel : TimerRelative,
                mWatchdogTimerPeriod
                );
}

EFI_STATUS
EFIAPI
WatchdogTimerDriverGetTimerPeriod (
  IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL  *This,
  IN UINT64                            *TimerPeriod
  )
/*++

Routine Description:

  This function retrieves the amount of time the system will wait before firing
  the watchdog timer.  This period is returned in TimerPeriod, and EFI_SUCCESS
  is returned.  If TimerPeriod is NULL, then EFI_INVALID_PARAMETER is returned.

Arguments:

  This        - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.

  TimerPeriod - A pointer to the amount of time in 100 nS units that the system
                will wait before the watchdog timer is fired.  If TimerPeriod of
                zero is returned, then the watchdog timer is disabled.

Returns:

  EFI_SUCCESS           - The amount of time that the system will wait before
                          firing the watchdog timer was returned in TimerPeriod.

  EFI_INVALID_PARAMETER - TimerPeriod is NULL.

--*/
{
  if (TimerPeriod == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  *TimerPeriod = mWatchdogTimerPeriod;

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
WatchdogTimerDriverInitialize (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
/*++

Routine Description:

  Initialize the Watchdog Timer Architectural Protocol driver

Arguments:

  ImageHandle - ImageHandle of the loaded driver

  SystemTable - Pointer to the System Table

Returns:

  EFI_SUCCESS           - Timer Architectural Protocol created

  EFI_OUT_OF_RESOURCES  - Not enough resources available to initialize driver.

  EFI_DEVICE_ERROR      - A device error occured attempting to initialize the driver.

--*/
{
  EFI_STATUS  Status;

  //
  // Make sure the Watchdog Timer Architectural Protocol is not already installed in the system
  //
  ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);

  //
  // Create the timer event used to implement a simple watchdog timer
  //
  Status = gBS->CreateEvent (
                  EVT_TIMER | EVT_NOTIFY_SIGNAL,
                  TPL_NOTIFY,
                  WatchdogTimerDriverExpires,
                  NULL,
                  &mWatchdogTimerEvent
                  );
  ASSERT_EFI_ERROR (Status);

  //
  // Install the Watchdog Timer Arch Protocol onto a new handle
  //
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &mWatchdogTimerHandle,
                  &gEfiWatchdogTimerArchProtocolGuid,
                  &mWatchdogTimer,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  return Status;
}