1 | /** @file
|
---|
2 |
|
---|
3 | Functions and types shared by the SMM accessor PEI and DXE modules.
|
---|
4 |
|
---|
5 | Copyright (C) 2015, Red Hat, Inc.
|
---|
6 | Copyright (c) 2024 Intel Corporation.
|
---|
7 |
|
---|
8 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
9 |
|
---|
10 | **/
|
---|
11 | #include <IndustryStandard/Q35MchIch9.h>
|
---|
12 | #include <Library/DebugLib.h>
|
---|
13 | #include <Library/PcdLib.h>
|
---|
14 | #include <Library/PciLib.h>
|
---|
15 |
|
---|
16 | #include "SmramInternal.h"
|
---|
17 |
|
---|
18 | //
|
---|
19 | // The value of PcdQ35TsegMbytes is saved into this variable at module startup.
|
---|
20 | //
|
---|
21 | UINT16 mQ35TsegMbytes;
|
---|
22 |
|
---|
23 | //
|
---|
24 | // The value of PcdQ35SmramAtDefaultSmbase is saved into this variable at
|
---|
25 | // module startup.
|
---|
26 | //
|
---|
27 | STATIC BOOLEAN mQ35SmramAtDefaultSmbase;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | Save PcdQ35TsegMbytes into mQ35TsegMbytes.
|
---|
31 | **/
|
---|
32 | VOID
|
---|
33 | InitQ35TsegMbytes (
|
---|
34 | VOID
|
---|
35 | )
|
---|
36 | {
|
---|
37 | mQ35TsegMbytes = PcdGet16 (PcdQ35TsegMbytes);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | Save PcdQ35SmramAtDefaultSmbase into mQ35SmramAtDefaultSmbase.
|
---|
42 | **/
|
---|
43 | VOID
|
---|
44 | InitQ35SmramAtDefaultSmbase (
|
---|
45 | VOID
|
---|
46 | )
|
---|
47 | {
|
---|
48 | mQ35SmramAtDefaultSmbase = PcdGetBool (PcdQ35SmramAtDefaultSmbase);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | Read the MCH_SMRAM and ESMRAMC registers, and update the LockState and
|
---|
53 | OpenState fields in the PEI_SMM_ACCESS_PPI / EFI_SMM_ACCESS2_PROTOCOL object,
|
---|
54 | from the D_LCK and T_EN bits.
|
---|
55 |
|
---|
56 | PEI_SMM_ACCESS_PPI and EFI_SMM_ACCESS2_PROTOCOL member functions can rely on
|
---|
57 | the LockState and OpenState fields being up-to-date on entry, and they need
|
---|
58 | to restore the same invariant on exit, if they touch the bits in question.
|
---|
59 |
|
---|
60 | @param[out] LockState Reflects the D_LCK bit on output; TRUE iff SMRAM is
|
---|
61 | locked.
|
---|
62 | @param[out] OpenState Reflects the inverse of the T_EN bit on output; TRUE
|
---|
63 | iff SMRAM is open.
|
---|
64 | **/
|
---|
65 | VOID
|
---|
66 | GetStates (
|
---|
67 | OUT BOOLEAN *LockState,
|
---|
68 | OUT BOOLEAN *OpenState
|
---|
69 | )
|
---|
70 | {
|
---|
71 | UINT8 SmramVal, EsmramcVal;
|
---|
72 |
|
---|
73 | SmramVal = PciRead8 (DRAMC_REGISTER_Q35 (MCH_SMRAM));
|
---|
74 | EsmramcVal = PciRead8 (DRAMC_REGISTER_Q35 (MCH_ESMRAMC));
|
---|
75 |
|
---|
76 | *LockState = !!(SmramVal & MCH_SMRAM_D_LCK);
|
---|
77 | *OpenState = !(EsmramcVal & MCH_ESMRAMC_T_EN);
|
---|
78 | }
|
---|
79 |
|
---|
80 | //
|
---|
81 | // The functions below follow the PEI_SMM_ACCESS_PPI and
|
---|
82 | // EFI_SMM_ACCESS2_PROTOCOL member declarations. The PeiServices and This
|
---|
83 | // pointers are removed (TSEG doesn't depend on them), and so is the
|
---|
84 | // DescriptorIndex parameter (TSEG doesn't support range-wise locking).
|
---|
85 | //
|
---|
86 | // The LockState and OpenState members that are common to both
|
---|
87 | // PEI_SMM_ACCESS_PPI and EFI_SMM_ACCESS2_PROTOCOL are taken and updated in
|
---|
88 | // isolation from the rest of the (non-shared) members.
|
---|
89 | //
|
---|
90 |
|
---|
91 | EFI_STATUS
|
---|
92 | SmramAccessOpen (
|
---|
93 | OUT BOOLEAN *LockState,
|
---|
94 | OUT BOOLEAN *OpenState
|
---|
95 | )
|
---|
96 | {
|
---|
97 | //
|
---|
98 | // Open TSEG by clearing T_EN.
|
---|
99 | //
|
---|
100 | PciAnd8 (
|
---|
101 | DRAMC_REGISTER_Q35 (MCH_ESMRAMC),
|
---|
102 | (UINT8)((~(UINT32)MCH_ESMRAMC_T_EN) & 0xff)
|
---|
103 | );
|
---|
104 |
|
---|
105 | GetStates (LockState, OpenState);
|
---|
106 | if (!*OpenState) {
|
---|
107 | return EFI_DEVICE_ERROR;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return EFI_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 | EFI_STATUS
|
---|
114 | SmramAccessClose (
|
---|
115 | OUT BOOLEAN *LockState,
|
---|
116 | OUT BOOLEAN *OpenState
|
---|
117 | )
|
---|
118 | {
|
---|
119 | //
|
---|
120 | // Close TSEG by setting T_EN.
|
---|
121 | //
|
---|
122 | PciOr8 (DRAMC_REGISTER_Q35 (MCH_ESMRAMC), MCH_ESMRAMC_T_EN);
|
---|
123 |
|
---|
124 | GetStates (LockState, OpenState);
|
---|
125 | if (*OpenState) {
|
---|
126 | return EFI_DEVICE_ERROR;
|
---|
127 | }
|
---|
128 |
|
---|
129 | return EFI_SUCCESS;
|
---|
130 | }
|
---|
131 |
|
---|
132 | EFI_STATUS
|
---|
133 | SmramAccessLock (
|
---|
134 | OUT BOOLEAN *LockState,
|
---|
135 | IN OUT BOOLEAN *OpenState
|
---|
136 | )
|
---|
137 | {
|
---|
138 | if (*OpenState) {
|
---|
139 | return EFI_DEVICE_ERROR;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //
|
---|
143 | // Close & lock TSEG by setting T_EN and D_LCK.
|
---|
144 | //
|
---|
145 | PciOr8 (DRAMC_REGISTER_Q35 (MCH_ESMRAMC), MCH_ESMRAMC_T_EN);
|
---|
146 | PciOr8 (DRAMC_REGISTER_Q35 (MCH_SMRAM), MCH_SMRAM_D_LCK);
|
---|
147 |
|
---|
148 | //
|
---|
149 | // Close & lock the SMRAM at the default SMBASE, if it exists.
|
---|
150 | //
|
---|
151 | if (mQ35SmramAtDefaultSmbase) {
|
---|
152 | PciWrite8 (
|
---|
153 | DRAMC_REGISTER_Q35 (MCH_DEFAULT_SMBASE_CTL),
|
---|
154 | MCH_DEFAULT_SMBASE_LCK
|
---|
155 | );
|
---|
156 | }
|
---|
157 |
|
---|
158 | GetStates (LockState, OpenState);
|
---|
159 | if (*OpenState || !*LockState) {
|
---|
160 | return EFI_DEVICE_ERROR;
|
---|
161 | }
|
---|
162 |
|
---|
163 | return EFI_SUCCESS;
|
---|
164 | }
|
---|
165 |
|
---|
166 | EFI_STATUS
|
---|
167 | SmramAccessGetCapabilities (
|
---|
168 | IN OUT UINTN *SmramMapSize,
|
---|
169 | IN OUT EFI_SMRAM_DESCRIPTOR *SmramMap
|
---|
170 | )
|
---|
171 | {
|
---|
172 | UINTN BufferSize;
|
---|
173 | EFI_HOB_GUID_TYPE *GuidHob;
|
---|
174 | EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *DescriptorBlock;
|
---|
175 | UINTN Index;
|
---|
176 |
|
---|
177 | //
|
---|
178 | // Get Hob list
|
---|
179 | //
|
---|
180 | GuidHob = GetFirstGuidHob (&gEfiSmmSmramMemoryGuid);
|
---|
181 | DescriptorBlock = GET_GUID_HOB_DATA (GuidHob);
|
---|
182 | ASSERT (DescriptorBlock);
|
---|
183 |
|
---|
184 | BufferSize = DescriptorBlock->NumberOfSmmReservedRegions * sizeof (EFI_SMRAM_DESCRIPTOR);
|
---|
185 |
|
---|
186 | if (*SmramMapSize < BufferSize) {
|
---|
187 | *SmramMapSize = BufferSize;
|
---|
188 | return EFI_BUFFER_TOO_SMALL;
|
---|
189 | }
|
---|
190 |
|
---|
191 | //
|
---|
192 | // Update SmramMapSize to real return SMRAM map size
|
---|
193 | //
|
---|
194 | *SmramMapSize = BufferSize;
|
---|
195 |
|
---|
196 | //
|
---|
197 | // Use the hob to publish SMRAM capabilities
|
---|
198 | //
|
---|
199 | for (Index = 0; Index < DescriptorBlock->NumberOfSmmReservedRegions; Index++) {
|
---|
200 | SmramMap[Index].PhysicalStart = DescriptorBlock->Descriptor[Index].PhysicalStart;
|
---|
201 | SmramMap[Index].CpuStart = DescriptorBlock->Descriptor[Index].CpuStart;
|
---|
202 | SmramMap[Index].PhysicalSize = DescriptorBlock->Descriptor[Index].PhysicalSize;
|
---|
203 | SmramMap[Index].RegionState = DescriptorBlock->Descriptor[Index].RegionState;
|
---|
204 | }
|
---|
205 |
|
---|
206 | return EFI_SUCCESS;
|
---|
207 | }
|
---|