hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
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
/** @file
  Sample to provide SecTemporaryRamDone function.
 
  @copyright
  Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.<BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
 
#include <PiPei.h>
 
#include <Ppi/TemporaryRamDone.h>
#include <Ppi/PlatformInitTempRamExitPpi.h>
 
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/FspWrapperPlatformLib.h>
#include <Library/FspWrapperApiLib.h>
#include <Library/PeiServicesTablePointerLib.h>
 
#include <Guid/FspHeaderFile.h>
 
#include <Register/ArchitecturalMsr.h>
 
#define MSR_NEM 0x000002E0
 
/**
This interface disables temporary memory in SEC Phase.
This is for dispatch mode use.  We should properly produce the FSP_TEMP_RAM_EXIT_PPI and then call
that instead, but the FSP does not produce that PPI
**/
VOID
EFIAPI
SecPlatformDisableTemporaryMemoryDispatchHack (
  VOID
  )
{
  UINT64  MsrValue;
  UINT64  MtrrDefaultType;
  MSR_IA32_MTRR_DEF_TYPE_REGISTER   DefType;
 
  //
  // Force and INVD.
  //
  AsmInvd ();
 
  //
  // Disable MTRRs.
  //
  DefType.Uint64 = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
  MtrrDefaultType = DefType.Uint64;
  DefType.Bits.E = 0;
  AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE, DefType.Uint64);
 
  //
  // Force and INVD to prevent MCA error.
  //
  AsmInvd ();
 
  //
  // Clear NEM Run and NEM Setup bits individually.
  //
  MsrValue = AsmReadMsr64 (MSR_NEM);
  MsrValue &= ~((UINT64) BIT1);
  AsmWriteMsr64 (MSR_NEM, MsrValue);
  MsrValue &= ~((UINT64) BIT0);
  AsmWriteMsr64 (MSR_NEM, MsrValue);
 
  //
  // Restore MTRR default setting
  //
  AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE, MtrrDefaultType);
}
 
/**
This interface disables temporary memory in SEC Phase.
**/
VOID
EFIAPI
SecPlatformDisableTemporaryMemory (
  VOID
  )
{
  EFI_STATUS                        Status;
  VOID                              *TempRamExitParam;
  CONST EFI_PEI_SERVICES            **PeiServices;
  PLATFORM_INIT_TEMP_RAM_EXIT_PPI   *PlatformInitTempRamExitPpi;
 
  DEBUG ((DEBUG_INFO, "SecPlatformDisableTemporaryMemory enter\n"));
  PeiServices = GetPeiServicesTablePointer ();
  ASSERT (PeiServices != NULL);
  if (PeiServices == NULL) {
    return ;
  }
  ASSERT ((*PeiServices) != NULL);
  if ((*PeiServices) == NULL) {
    return;
  }
  Status = (*PeiServices)->LocatePpi (
                            PeiServices,
                            &gPlatformInitTempRamExitPpiGuid,
                            0,
                            NULL,
                            (VOID **) &PlatformInitTempRamExitPpi
                            );
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR (Status)) {
    return;
  }
 
  Status = PlatformInitTempRamExitPpi->PlatformInitBeforeTempRamExit ();
  ASSERT_EFI_ERROR (Status);
 
  if (PcdGet8 (PcdFspModeSelection) == 1) {
    //
    // FSP API mode
    //
    TempRamExitParam = UpdateTempRamExitParam ();
    Status = CallTempRamExit (TempRamExitParam);
    DEBUG ((DEBUG_INFO, "TempRamExit status: 0x%x\n", Status));
    ASSERT_EFI_ERROR (Status);
  } else {
    SecPlatformDisableTemporaryMemoryDispatchHack ();
  }
 
  Status = PlatformInitTempRamExitPpi->PlatformInitAfterTempRamExit ();
  ASSERT_EFI_ERROR(Status);
 
  return ;
}