summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/LynxPoint/Wdt/Pei/WdtPei.c
blob: c1d1e03f8aa52b53f49c8cbec3e27f3a8a36f7b7 (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
/** @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 "EdkIIGluePeim.h"
#endif

#include EFI_PPI_CONSUMER (PchReset)
#include "WdtCommon.h"
#include EFI_PPI_PRODUCER (Wdt)

EFI_STATUS
EFIAPI
WdtPchResetCallback (
  IN     PCH_RESET_TYPE           PchResetType
  );

static WDT_PPI                    mWdtPpi = {
  WdtReloadAndStart,
  WdtCheckStatus,
  WdtDisable,
  WdtAllowKnownReset,
  IsWdtRequired,
  IsWdtEnabled
};

static PCH_RESET_CALLBACK_PPI     mPchResetCallbackPpi = { WdtPchResetCallback };

EFI_STATUS
EndOfPeiCallback (
  IN EFI_PEI_SERVICES           **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Ppi
  );

static EFI_PEI_PPI_DESCRIPTOR     mInstallWdtPpi = {
  EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
  &gWdtPpiGuid,
  &mWdtPpi
};

static EFI_PEI_PPI_DESCRIPTOR     mInstallPchResetCallbackPpi = {
  EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
  &gPchResetCallbackPpiGuid,
  &mPchResetCallbackPpi
};

static EFI_PEI_NOTIFY_DESCRIPTOR  mNotifyList = {
  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  &gEndOfPeiSignalPpiGuid,
  EndOfPeiCallback
};

#define MINIMUM_TIMEOUT_AT_S3_EXIT  10  ///< seconds

/**
  Reads PCH registers to check if platform wakes from S3/S4

  @param[in] None

  @retval TRUE                    if platfrom wakes from S3/S4
  @retval FALSE                   otherwise
**/
UINT8
IsWakeFromS3_S4 (
  VOID
  )
{
  UINT32  Address;
  UINT16  SleepType;

  Address = MmioRead32 (
              MmPciAddress (0,
              DEFAULT_PCI_BUS_NUMBER_PCH,
              PCI_DEVICE_NUMBER_PCH_LPC,
              PCI_FUNCTION_NUMBER_PCH_LPC,
              R_PCH_LPC_ACPI_BASE)
              ) & B_PCH_LPC_ACPI_BASE_BAR;

  if (IoRead16 (Address + R_PCH_ACPI_PM1_STS) & B_PCH_ACPI_PM1_STS_WAK) {
    SleepType = IoRead16 (Address + R_PCH_ACPI_PM1_CNT) & B_PCH_ACPI_PM1_CNT_SLP_TYP;
    if ((SleepType == V_PCH_ACPI_PM1_CNT_S3) || (SleepType == V_PCH_ACPI_PM1_CNT_S4)) {
      return TRUE;
    }
  }

  return FALSE;
}

/**
  Initializes watchdog failure bits.
  If there was an unexpected reset, enforces WDT expiration.
  Stores initial WDT state in a HOB, it is useful in flows with S3/S4 resume.
  Stops watchdog.
  Installs watchdog PPI for other modules to use.

  @param[in] FfsHeader            Pointer to Firmware File System file header.
  @param[in] PeiServices          General purpose services available to every PEIM.

  @retval EFI_SUCCESS             When everything is OK
**/
EFI_STATUS
WdtPeiEntryPoint (
  IN  EFI_FFS_FILE_HEADER *FfsHeader,
  IN  EFI_PEI_SERVICES    **PeiServices
  )
{
  UINT32      Readback;
  EFI_STATUS  Status;
  UINT16      TimeoutValue;
  UINT8       Active;
  WDT_HOB     *WdtHobPtr;

#ifndef WDT_SUPPORT_ENABLED
  ///
  /// clear status bits and disable watchdog, then lock the register
  ///
  IoWrite32 (WdtGetAddress (), (B_PCH_OC_WDT_CTL_ICCSURV_STS | B_PCH_OC_WDT_CTL_NO_ICCSURV_STS));
  IoWrite32 (WdtGetAddress (), B_PCH_OC_WDT_CTL_LCK);
#endif

  Readback = IoRead32 (WdtGetAddress ());

  DEBUG ((EFI_D_INFO, "(WDT) Readback = 0x%08x\n", Readback));
  ///
  /// Write current Wdt settings to a HOB, they may be be needed in S3/S4 resume paths
  ///
  if (Readback & B_PCH_OC_WDT_CTL_EN) {
    Active        = 1;
    TimeoutValue  = (UINT16) ((Readback & B_PCH_OC_WDT_CTL_TOV_MASK) + 1);
  } else {
    Active        = 0;
    TimeoutValue  = 0;
  }

  Status = (*PeiServices)->CreateHob (PeiServices, EFI_HOB_TYPE_GUID_EXTENSION, sizeof (WDT_HOB), (VOID **) &WdtHobPtr);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  WdtHobPtr->Header.Name  = mWdtHobGuid;
  WdtHobPtr->Active       = Active;
  WdtHobPtr->TimeoutValue = TimeoutValue;
  ///
  /// If there was a WDT expiration, set Failure Status and clear timeout status bits
  /// Timeout status bits are cleared by writing '1'
  ///
  if (Readback & (B_PCH_OC_WDT_CTL_ICCSURV_STS | B_PCH_OC_WDT_CTL_NO_ICCSURV_STS)) {
    DEBUG ((EFI_D_ERROR, "(WDT) Expiration detected.\n", Readback));
    Readback |= B_PCH_OC_WDT_CTL_FAILURE_STS;
    Readback |= (B_PCH_OC_WDT_CTL_ICCSURV_STS | B_PCH_OC_WDT_CTL_NO_ICCSURV_STS);
    Readback &= ~(B_PCH_OC_WDT_CTL_UNXP_RESET_STS);
  } else {
    ///
    /// If there was unexpected reset but no WDT expiration and no resume from S3/S4,
    /// clear unexpected reset status and enforce expiration. This is to inform Firmware
    /// which has no access to unexpected reset status bit, that something went wrong.
    ///
    if ((Readback & B_PCH_OC_WDT_CTL_UNXP_RESET_STS) && !IsWakeFromS3_S4 ()) {
#if defined EFI_DEBUG && !defined USE_WDT_IN_DEBUG_BIOS
      DEBUG ((EFI_D_ERROR, "(WDT) Unexpected reset detected and ignored.\n"));
      Readback &= ~(B_PCH_OC_WDT_CTL_FAILURE_STS | B_PCH_OC_WDT_CTL_UNXP_RESET_STS);
      Readback |= (B_PCH_OC_WDT_CTL_ICCSURV_STS | B_PCH_OC_WDT_CTL_NO_ICCSURV_STS);
#else
      DEBUG ((EFI_D_ERROR, "(WDT) Unexpected reset detected. Enforcing Wdt expiration.\n"));
      WdtReloadAndStart (1);
      while (1) {
        ///
        /// wait for reboot caused by WDT expiration
        ///
      }
#endif
    } else {
      ///
      /// No WDT expiration and no unexpected reset - clear Failure status
      ///
      DEBUG ((EFI_D_INFO, "(WDT) Status OK.\n", Readback));
      Readback &= ~(B_PCH_OC_WDT_CTL_FAILURE_STS);
      Readback |= (B_PCH_OC_WDT_CTL_ICCSURV_STS | B_PCH_OC_WDT_CTL_NO_ICCSURV_STS);
    }
  }

  IoWrite32 (WdtGetAddress (), Readback);
  ///
  /// register an event for EndOfPei. It will support Wdt in resume from S3.
  ///
  Status  = (**PeiServices).NotifyPpi (PeiServices, &mNotifyList);

  Status  = (**PeiServices).InstallPpi (PeiServices, &mInstallWdtPpi);

  Status  = (**PeiServices).InstallPpi (PeiServices, &mInstallPchResetCallbackPpi);

  ASSERT_EFI_ERROR (Status);

  return Status;
}

/**
  Support for WDT in S3 resume.
  If WDT was enabled during S0->S3 transition, this function will turn on WDT
  just before waking OS. Timeout value will be overridden if it was too small.

  @param[in] PeiServices          General purpose services available to every PEIM.
  @param[in] NotifyDescriptor     The notification structure this PEIM registered on install.
  @param[in] Ppi                  The memory discovered PPI.  Not used.

  @retval EFI_SUCCESS             When everything is OK
  @retval EFI_NOT_FOUND           WdtHob is not found
**/
EFI_STATUS
EndOfPeiCallback (
  IN EFI_PEI_SERVICES           **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Ppi
  )
{
  WDT_HOB       *WdtHob;
  EFI_STATUS    Status;
  EFI_BOOT_MODE BootMode;

  DEBUG ((EFI_D_INFO, "(WDT) EndOfPeiCallback\n"));
  Status = (*PeiServices)->GetBootMode (PeiServices, &BootMode);
  ASSERT_EFI_ERROR (Status);
  WdtHob = GetFirstGuidHob (&mWdtHobGuid);
  if (WdtHob == NULL) {
    return EFI_NOT_FOUND;
  }

  DEBUG ((EFI_D_INFO, "(WDT) BootMode %d, Hob, active %d, ToV %d\n", BootMode, WdtHob->Active, WdtHob->TimeoutValue));

  if (BootMode == BOOT_ON_S3_RESUME) {
    if (WdtHob->Active == 1) {
      if (WdtHob->TimeoutValue < MINIMUM_TIMEOUT_AT_S3_EXIT) {
        WdtReloadAndStart (MINIMUM_TIMEOUT_AT_S3_EXIT);
      } else {
        WdtReloadAndStart (WdtHob->TimeoutValue);
      }
    } else {
      WdtDisable ();
    }
  }

  return EFI_SUCCESS;
}

/**
  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;
}