summaryrefslogtreecommitdiff
path: root/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Reset/RuntimeDxe/ScReset.c
blob: 36ac0e3d0371f492ef4b933c74cf22dd8133be35 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/** @file
  Reset Architectural Protocol implementation.

  Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>

  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.

**/

#include "ScReset.h"
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/ConfigBlockLib.h>
#include "CmosMap.h"
#include "ScAccess.h"
#include "HeciRegs.h"

SC_RESET_INSTANCE  *mResetInstance;


/**
  Install and initialize reset protocols.

  @param[in] ImageHandle                   Image handle of the loaded driver
  @param[in] SystemTable                   Pointer to the System Table

  @retval    EFI_SUCCESS                   Thread can be successfully created
  @retval    EFI_OUT_OF_RESOURCES          Cannot allocate protocol data structure
  @retval    EFI_DEVICE_ERROR              Cannot create the timer service

**/
EFI_STATUS
EFIAPI
InitializeScReset (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS                        Status;
  EFI_HANDLE                        ResetHandle;
#if ((TIANO_RELEASE_VERSION != 0) && (EFI_SPECIFICATION_VERSION < 0x00020000))
  DXE_SC_PLATFORM_POLICY_PROTOCOL   *ScPlatformPolicy;
#endif
  EFI_EVENT                         Event;
  EFI_PEI_HOB_POINTERS              HobPtr;
  SC_POLICY_HOB                     *ScPolicy;
  SC_GENERAL_CONFIG                 *GeneralConfig;

  DEBUG ((DEBUG_INFO, "InitializeScReset() Start\n"));
  ResetHandle = NULL;
  ScPolicy    = NULL;

  //
  // Get SC Policy HOB.
  //
  Status = EFI_NOT_FOUND;
  ZeroMem (&HobPtr, sizeof (EFI_PEI_HOB_POINTERS));
  HobPtr.Guid = GetFirstGuidHob (&gScPolicyHobGuid);
  ASSERT (HobPtr.Guid != NULL);

  ScPolicy = GET_GUID_HOB_DATA (HobPtr.Guid);
  Status = GetConfigBlock ((VOID *) ScPolicy, &gScGeneralConfigGuid, (VOID *) &GeneralConfig);
  ASSERT_EFI_ERROR (Status);

  //
  // Allocate Runtime memory for the ScExtendedReset protocol instance.
  //
  mResetInstance = AllocateRuntimePool (sizeof (SC_RESET_INSTANCE));
  if (mResetInstance == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

#if ((TIANO_RELEASE_VERSION != 0) && (EFI_SPECIFICATION_VERSION < 0x00020000))
  //
  // Get the desired platform setup policy.
  //
  Status = gBS->LocateProtocol (&gDxePlatformPolicyProtocolGuid, NULL, (VOID **) &ScPlatformPolicy);
  ASSERT_EFI_ERROR (Status);

  //
  // Check whether the CapsuleVariableName is filled.
  //
  if (ScPlatformPolicy->CapsuleVariableName != NULL) {
    //
    // Allocate a runtime space and copy string from CapsuleVariableName
    //
    mResetInstance->CapsuleVariableName = AllocateRuntimePool (StrSize (ScPlatformPolicy->CapsuleVariableName));
    CopyMem (
      mResetInstance->CapsuleVariableName,
      ScPlatformPolicy->CapsuleVariableName,
      StrSize (ScPlatformPolicy->CapsuleVariableName)
      );
  }
#endif
  //
  // Install protocol interface
  //
  mResetInstance->Signature                     = RESET_SIGNATURE;
  mResetInstance->Handle                        = NULL;
  mResetInstance->ScExtendedResetProtocol.Reset = ScExtendedReset;
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &mResetInstance->Handle,
                  &gEfiExtendedResetProtocolGuid,
                  &mResetInstance->ScExtendedResetProtocol,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);
  mResetInstance->PmcBase = PMC_BASE_ADDRESS;

  mResetInstance->AcpiBar = (UINT16) PcdGet16 (PcdScAcpiIoPortBaseAddress);

  mResetInstance->ResetSelect = (UINT8) GeneralConfig->ResetSelect;

  //
  // Make sure the Reset Architectural Protocol is not already installed in the system
  //
  ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiResetArchProtocolGuid);

  //
  // Hook the runtime service table
  //
  SystemTable->RuntimeServices->ResetSystem = (EFI_RESET_SYSTEM) IntelScResetSystem;

  //
  // Now install the Reset RT AP on a new handle
  //
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &ResetHandle,
                  &gEfiResetArchProtocolGuid,
                  NULL,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  //
  // Install a notification to convert the SCReset driver to Virtual Addr
  //
  Status = gBS->CreateEventEx (
                  EVT_NOTIFY_SIGNAL,
                  TPL_NOTIFY,
                  ScResetVirtualddressChangeEvent,
                  NULL,
                  &gEfiEventVirtualAddressChangeGuid,
                  &Event
                  );
  ASSERT_EFI_ERROR (Status);

  //
  // The Lib Deconstruct will automatically be called when entrypoint return error.
  //
  DEBUG ((DEBUG_INFO, "InitializeScReset() End\n"));

  return Status;
}


/**
  Set flag to indicate the reset is controlled reset or uncontrolled reset.

**/
VOID
NotifyControlledReset(
  VOID
  )
{
  //
  // The following magic value in CMOS register is used by DXE module "NullMemoryTest"
  // to differentiate a reset as Controlled or Uncontrolled.
  //
  IoWrite8 (CmosIo_72, EFI_CMOS_CLEAN_RESET);
  IoWrite8 (CmosIo_73, B_MAGIC_CLEAN_RESET_VALUE); //Signal a controlled Reset
}


#if ((TIANO_RELEASE_VERSION != 0) && (EFI_SPECIFICATION_VERSION < 0x00020000))
/**
  If need be, do any special reset required for capsules. For this
  implementation where we're called from the ResetSystem() api,
  just set our capsule variable and return to let the caller
  do a soft reset.

  @param[in] CapsuleDataPtr            Pointer to the capsule block descriptors

**/
VOID
CapsuleReset (
  IN UINTN   CapsuleDataPtr
  )
{
  UINT32  Data32;
  UINT32  Eflags;
  UINT16  AcpiBaseAddr;

  //
  // Check the CapsuleVariableName, asset if it is not filled.
  //
  ASSERT (mResetInstance->CapsuleVariableName != NULL);

  //
  // This implementation assumes we're using a variable for the capsule
  // data pointer.
  //
  EfiSetVariable (
    mResetInstance->CapsuleVariableName,
    &gEfiCapsuleVendorGuid,
    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,  ///< attributes
    sizeof (UINTN),
    (VOID *) &CapsuleDataPtr
    );

  //
  // Read the ACPI Base address
  //
  AcpiBaseAddr = (UINT16) PcdGet16 (PcdScAcpiIoPortBaseAddress);
  ASSERT (AcpiBaseAddr != 0);

  //
  // Read the PM1_CNT register
  //
  Data32  = IoRead32 ((UINTN) (AcpiBaseAddr + R_ACPI_PM1_CNT));
  Data32  = (UINT32) ((Data32 & ~(B_ACPI_PM1_CNT_SLP_TYP + B_ACPI_PM1_CNT_SLP_EN)) | V_ACPI_PM1_CNT_S3);
  Eflags  = (UINT32) AsmReadEflags ();

  if ((Eflags & 0x200)) {
    DisableInterrupts ();
  }

  AsmWbinvd ();
  AsmWriteCr0 (AsmReadCr0 () | 0x060000000);

  IoWrite32 (
    (UINTN) (AcpiBaseAddr + R_ACPI_PM1_CNT),
    (UINT32) Data32
    );

  Data32 = Data32 | B_ACPI_PM1_CNT_SLP_EN;

  IoWrite32 (
    (UINTN) (AcpiBaseAddr + R_ACPI_PM1_CNT),
    (UINT32) Data32
    );

  if ((Eflags & 0x200)) {
    EnableInterrupts ();
  }
  //
  // Should not return
  //
  EFI_DEADLOOP ();
}
#endif


/**
  Generate system reset by Reset Control Register (IO Cf9h)
  Note: This reset is hooked to RuntimeServices->ResetSystem and will be called
        in OS environment and if any debug messages added then need to handle it
        carefully, else debug build cause to BSOD.
        Its recommended not to have debug messages over here.

  @param[in] ResetType                 Warm or cold
  @param[in] ResetStatus               Possible cause of reset
  @param[in] DataSize                  Size of ResetData in bytes
  @param[in] ResetData                 Optional Unicode string

**/
VOID
EFIAPI
IntelScResetSystem (
  IN EFI_RESET_TYPE   ResetType,
  IN EFI_STATUS       ResetStatus,
  IN UINTN            DataSize,
  IN VOID             *ResetData OPTIONAL
  )
{
  UINT8          InitialData;
  UINT8          OutputData;
  UINT16         AcpiBase;
  UINT16         Data16;
  UINT32         Data32;

  NotifyControlledReset ();

  switch (ResetType) {
    case EfiResetWarm:
      InitialData = V_RST_CNT_HARDSTARTSTATE;
      OutputData  = mResetInstance->ResetSelect;
      if (mResetInstance->ResetSelect == V_RST_CNT_FULLRESET) {
      }
      break;

    case EfiResetCold:
      InitialData = V_RST_CNT_HARDSTARTSTATE;
      OutputData  = V_RST_CNT_FULLRESET;
      break;

    case EfiResetShutdown:
      AcpiBase = (UINT16) mResetInstance->AcpiBar;

      //
      // Then, GPE0_EN should be disabled to avoid any GPI waking up the system from S5
      //
      Data16 = 0;

      IoWrite16 ((UINTN) (AcpiBase + R_ACPI_GPE0a_EN), Data16);

      //
      // Clear Sleep SMI Status
      //
      IoWrite16 (AcpiBase + R_SMI_STS,
                 (UINT16) (IoRead16 (AcpiBase + R_SMI_STS) | B_SMI_STS_ON_SLP_EN));
      //
      // Clear Sleep Type Enable
      //
      IoWrite16 (AcpiBase + R_SMI_EN,
                 (UINT16) (IoRead16 (AcpiBase + R_SMI_EN) & (~B_SMI_EN_ON_SLP_EN)));
      //
      // Clear Power Button Status
      //
      IoWrite16(AcpiBase + R_ACPI_PM1_STS, B_ACPI_PM1_STS_PWRBTN);

      //
      // Secondly, Power Button Status bit must be cleared
      //
      // Write a "1" to bit[8] of power button status register at
      // (ABASE + PM1_STS) to clear this bit
      // Clear it through SMI Status register
      //
      Data16 = B_SMI_STS_PM1_STS_REG;
      IoWrite16 ((UINTN) (AcpiBase + R_SMI_STS), Data16);

      //
      // Finally, transform system into S5 sleep state
      //
      Data32  = IoRead32 ((UINTN) (AcpiBase + R_ACPI_PM1_CNT));
      Data32  = (UINT32) ((Data32 & ~(B_ACPI_PM1_CNT_SLP_TYP + B_ACPI_PM1_CNT_SLP_EN)) | V_ACPI_PM1_CNT_S5);

      IoWrite32 ((UINTN) (AcpiBase + R_ACPI_PM1_CNT), Data32);

      Data32 = Data32 | B_ACPI_PM1_CNT_SLP_EN;

      IoWrite32 ((UINTN) (AcpiBase + R_ACPI_PM1_CNT), Data32);
      //
      // Should not return
      //
      CpuDeadLoop();
      return ;

    default:
      return ;
  }

  IoWrite8 (
    (UINTN) R_RST_CNT,
    (UINT8) InitialData
    );

  IoWrite8 (
    (UINTN) R_RST_CNT,
    (UINT8) OutputData
    );

  //
  // Given we should have reset getting here would be bad
  //
  CpuDeadLoop();

  ASSERT (FALSE);
}


/**
  Execute SC Extended Reset from the host controller.

  @param[in] This                      Pointer to the EFI_SC_EXTENDED_RESET_PROTOCOL instance.
  @param[in] ScExtendedResetTypes      SC Extended Reset Types which includes PowerCycle, Globalreset.

  @retval    EFI_SUCCESS               Successfully completed.
  @retval    EFI_INVALID_PARAMETER     If ResetType is invalid.

**/
EFI_STATUS
EFIAPI
ScExtendedReset (
  IN EFI_SC_EXTENDED_RESET_PROTOCOL   *This,
  IN SC_EXTENDED_RESET_TYPES          ScExtendedResetTypes
  )
{
  //
  // Check if the parameters are valid.
  //
  if ((ScExtendedResetTypes.PowerCycle == 0) && (ScExtendedResetTypes.GlobalReset == 0)) {
    return EFI_INVALID_PARAMETER;
  }

  NotifyControlledReset ();

  if (ScExtendedResetTypes.GlobalReset == 1) {
    MmioOr32 (mResetInstance->PmcBase + R_PMC_PMIR, (UINT32) (B_PMC_PMIR_CF9GR));
  }

  IoWrite8 ((UINTN) R_RST_CNT, (UINT8) V_RST_CNT_FULLRESET);

  //
  // Waiting for system reset
  //
  while (TRUE);

  return EFI_SUCCESS;
}


/**
  Fixup internal data pointers so that the services can be called in virtual mode.

  @param[in]  Event                     The event registered.
  @param[in]  Context                   Event context. Not used in this event handler.

**/
VOID
EFIAPI
ScResetVirtualddressChangeEvent (
  IN EFI_EVENT        Event,
  IN VOID             *Context
  )
{
  EfiConvertPointer (0x0, (VOID **) &(mResetInstance->ScExtendedResetProtocol.Reset));
#if ((TIANO_RELEASE_VERSION != 0) && (EFI_SPECIFICATION_VERSION < 0x00020000))
  EfiConvertPointer (0x0, (VOID **) &(mResetInstance->CapsuleVariableName));
#endif
  EfiConvertPointer (0x0, (VOID **) &(mResetInstance->AcpiBar));
  EfiConvertPointer (0x0, (VOID **) &(mResetInstance->PmcBase));
  EfiConvertPointer (0x0, (VOID **) &(mResetInstance));
}