summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/LynxPoint/Wdt/Dxe/WdtDxe.c
blob: ea5bd2f57f38b0872a825ff715861459f55b11ae (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
/** @file
  Implementation file for Watchdog Timer functionality

@copyright
  Copyright (c) 2010 - 2012 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement
**/
#if !defined(EDK_RELEASE_VERSION) || (EDK_RELEASE_VERSION < 0x00020000)
#include "EdkIIGlueDxe.h"
#endif

#include EFI_PROTOCOL_CONSUMER (PchReset)
#include "WdtCommon.h"
#include EFI_PROTOCOL_PRODUCER (Wdt)

VOID
EFIAPI
WdtRunBeforeOsBoot (
  IN EFI_EVENT Event,
  IN VOID      *Context
  );

EFI_STATUS
EFIAPI
WdtPchResetCallback (
  IN     PCH_RESET_TYPE           PchResetType
  );

#define TIMEOUT_AFTER_POST_MULTIPLIER 16
#define MINIMUM_TIMEOUT_AT_S4_EXIT    600 ///< 10 minutes
EFI_HANDLE                  mImageHandle;
WDT_PROTOCOL                mWdtProtocol = {
  WdtReloadAndStart,
  WdtCheckStatus,
  WdtDisable,
  WdtAllowKnownReset,
  IsWdtRequired,
  IsWdtEnabled
};

PCH_RESET_CALLBACK_PROTOCOL mPchResetCallbackProtocol = { WdtPchResetCallback };

/**
  Installs WDT protocol.
  Registers a function to be executed just before booting to OS.

  @param[in] ImageHandle          Image handle for this driver image
  @param[in] SystemTable          Pointer to the EFI System Table

  @retval EFI_SUCCESS             WDT DXE driver initialization completed successfully
**/
EFI_STATUS
WdtDxeEntryPoint (
  IN  EFI_HANDLE       ImageHandle,
  IN  EFI_SYSTEM_TABLE *SystemTable
  )
{
  EFI_STATUS  Status;
  EFI_EVENT   Event;

  DEBUG ((EFI_D_INFO, "(Wdt) Entry Point to WdtDxe\n"));

  mImageHandle = ImageHandle;

  Status = gBS->CreateEvent (
                  EVENT_SIGNAL_EXIT_BOOT_SERVICES,
                  EFI_TPL_CALLBACK,
                  WdtRunBeforeOsBoot,
                  NULL,
                  &Event
                  );
  ASSERT_EFI_ERROR (Status);

  Status = EfiCreateEventLegacyBootEx (
            EFI_TPL_CALLBACK,
            WdtRunBeforeOsBoot,
            NULL,
            &Event
            );
  ASSERT_EFI_ERROR (Status);

  DEBUG ((EFI_D_INFO, "(Wdt) WDT event registration; Status = %r\n", Status));

  Status = gBS->InstallProtocolInterface (
                  &ImageHandle,
                  &gWdtProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &mWdtProtocol
                  );
  ASSERT_EFI_ERROR (Status);

  Status = gBS->InstallProtocolInterface (
                  &ImageHandle,
                  &gPchResetCallbackProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &mPchResetCallbackProtocol
                  );
  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;
}

/**
  Turns on watchdog timer just before booting to OS, if an OS application requested that.
  Clears request status.
  Uninstalls Wdt protocol to prevent other modules from interfering with actions described above.

  @param[in] Event                useless here, but required in functions invoked by events
  @param[in] Context              useless here, but required in functions invoked by events

  @retval    None
**/
VOID
EFIAPI
WdtRunBeforeOsBoot (
  IN  EFI_EVENT Event,
  IN  VOID      *Context
  )
{
  UINT32                ReloadValue;
  UINT32                Readback;
  EFI_STATUS            Status;
  EFI_BOOT_MODE         BootMode;
  EFI_PEI_HOB_POINTERS  HobList;
  WDT_HOB               *WdtHob;

  gBS->CloseEvent (Event);

  DEBUG ((EFI_D_INFO, "(Wdt) RunWdtBeforeOsBoot\n"));
  Status = gBS->UninstallProtocolInterface (
                  mImageHandle,
                  &gWdtProtocolGuid,
                  &mWdtProtocol
                  );

  Status = gBS->UninstallProtocolInterface (
                  mImageHandle,
                  &gPchResetCallbackProtocolGuid,
                  &mPchResetCallbackProtocol
                  );
  ///
  /// check boot type, there are different flows for S4/S5
  ///
  EfiGetSystemConfigurationTable (&gEfiHobListGuid, (VOID **) &HobList.Raw);
  if (HobList.Header->HobType != EFI_HOB_TYPE_HANDOFF) {
    DEBUG ((EFI_D_ERROR, "(Wdt) Handoff Hob missing!\n"));
    return;
  }

  BootMode  = HobList.HandoffInformationTable->BootMode;

  WdtHob    = GetFirstGuidHob (&mWdtHobGuid);
  if (WdtHob == NULL) {
    return;
  }

  Readback    = IoRead32 (WdtGetAddress ());
  ReloadValue = TIMEOUT_AFTER_POST_MULTIPLIER * ((Readback & B_PCH_OC_WDT_CTL_AFTER_POST) >> 16);

  if (BootMode == BOOT_ON_S4_RESUME) {
    ///
    /// S4 resume: if WDT was enabled before S0->S4 transition,
    /// then WDT must be turned on even though TimeoutValueAfterPost == 0
    /// unlike in S5->S0 flow, ToVaP is not set to zero after being consumed
    ///
    if (WdtHob->Active == 1) {
      if (ReloadValue != 0) {
        WdtReloadAndStart (ReloadValue);
      } else {
        WdtReloadAndStart (MINIMUM_TIMEOUT_AT_S4_EXIT);
      }
    } else {
      WdtDisable ();
    }
  } else if (ReloadValue != 0) {
    ///
    /// start WDT with TimeoutValueAfterPost and clear that value from register
    ///
    Readback &= ~(B_PCH_OC_WDT_CTL_AFTER_POST);
    IoWrite32 (WdtGetAddress (), Readback);
    WdtReloadAndStart (ReloadValue);
  } else {
    WdtDisable ();
  }

  return;
}

/**
  WDT call back function for Pch Reset.

  @param[in] PchResetType         Pch Reset Types which includes PowerCycle, Globalreset.

  @retval EFI_SUCCESS             The function completed successfully
  @retval Others                  All other error conditions encountered result in an ASSERT.
**/
EFI_STATUS
EFIAPI
WdtPchResetCallback (
  IN     PCH_RESET_TYPE           PchResetType
  )
{
  WdtAllowKnownReset ();
  return EFI_SUCCESS;
}