2 This driver publishes the SMM Access 2 Protocol.
4 Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "SmmAccessDxe.h"
11 SMM_ACCESS_PRIVATE_DATA mSmmAccess
;
14 Update region state from SMRAM description
16 @param[in] OrLogic Indicate to use OR if true or AND if false.
17 @param[in] Value The value to set to region state based on OrLogic.
21 SyncRegionState2SmramDesc (
28 for (Index
= 0; Index
< mSmmAccess
.NumberRegions
; Index
++) {
30 mSmmAccess
.SmramDesc
[Index
].RegionState
|= Value
;
32 mSmmAccess
.SmramDesc
[Index
].RegionState
&= Value
;
38 This routine accepts a request to "open" a region of SMRAM. The
39 region could be legacy ABSEG, HSEG, or TSEG near top of physical memory.
40 The use of "open" means that the memory is visible from all boot-service
43 @param This Pointer to the SMM Access Interface.
45 @retval EFI_SUCCESS The region was successfully opened.
46 @retval EFI_DEVICE_ERROR The region could not be opened because locked by chipset.
47 @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.
53 IN EFI_SMM_ACCESS2_PROTOCOL
*This
56 if ((mSmmAccess
.SmmRegionState
& EFI_SMRAM_LOCKED
) != 0) {
58 // Cannot open a "locked" region
60 DEBUG ((DEBUG_INFO
, "Cannot open the locked SMRAM Region\n"));
61 return EFI_DEVICE_ERROR
;
64 mSmmAccess
.SmmRegionState
&= ~(EFI_SMRAM_CLOSED
| EFI_ALLOCATED
);
65 SyncRegionState2SmramDesc (FALSE
, (UINT64
)(UINTN
)(~(EFI_SMRAM_CLOSED
| EFI_ALLOCATED
)));
67 mSmmAccess
.SmmRegionState
|= EFI_SMRAM_OPEN
;
68 SyncRegionState2SmramDesc (TRUE
, EFI_SMRAM_OPEN
);
69 mSmmAccess
.SmmAccess
.OpenState
= TRUE
;
75 This routine accepts a request to "close" a region of SMRAM. The region
76 could be legacy AB or TSEG near top of physical memory.
77 The use of "close" means that the memory is only visible from SMM agents,
78 not from BS or RT code.
80 @param This Pointer to the SMM Access Interface.
82 @retval EFI_SUCCESS The region was successfully closed.
83 @retval EFI_DEVICE_ERROR The region could not be closed because locked by
85 @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.
91 IN EFI_SMM_ACCESS2_PROTOCOL
*This
94 if ((mSmmAccess
.SmmRegionState
& EFI_SMRAM_LOCKED
) != 0) {
96 // Cannot close a "locked" region
98 DEBUG ((DEBUG_INFO
, "Cannot close the locked SMRAM Region\n"));
99 return EFI_DEVICE_ERROR
;
102 if ((mSmmAccess
.SmmRegionState
& EFI_SMRAM_CLOSED
) != 0) {
103 return EFI_DEVICE_ERROR
;
106 mSmmAccess
.SmmRegionState
&= ~EFI_SMRAM_OPEN
;
107 SyncRegionState2SmramDesc (FALSE
, (UINT64
)(UINTN
)(~EFI_SMRAM_OPEN
));
109 mSmmAccess
.SmmRegionState
|= (EFI_SMRAM_CLOSED
| EFI_ALLOCATED
);
110 SyncRegionState2SmramDesc (TRUE
, EFI_SMRAM_CLOSED
| EFI_ALLOCATED
);
112 mSmmAccess
.SmmAccess
.OpenState
= FALSE
;
118 This routine accepts a request to "lock" SMRAM. The
119 region could be legacy AB or TSEG near top of physical memory.
120 The use of "lock" means that the memory can no longer be opened
123 @param This Pointer to the SMM Access Interface.
125 @retval EFI_SUCCESS The region was successfully locked.
126 @retval EFI_DEVICE_ERROR The region could not be locked because at least
127 one range is still open.
128 @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.
134 IN EFI_SMM_ACCESS2_PROTOCOL
*This
137 if (mSmmAccess
.SmmAccess
.OpenState
) {
138 DEBUG ((DEBUG_INFO
, "Cannot lock SMRAM when it is still open\n"));
139 return EFI_DEVICE_ERROR
;
142 mSmmAccess
.SmmRegionState
|= EFI_SMRAM_LOCKED
;
143 SyncRegionState2SmramDesc (TRUE
, EFI_SMRAM_LOCKED
);
144 mSmmAccess
.SmmAccess
.LockState
= TRUE
;
149 This routine services a user request to discover the SMRAM
150 capabilities of this platform. This will report the possible
151 ranges that are possible for SMRAM access, based upon the
152 memory controller capabilities.
154 @param This Pointer to the SMRAM Access Interface.
155 @param SmramMapSize Pointer to the variable containing size of the
156 buffer to contain the description information.
157 @param SmramMap Buffer containing the data describing the Smram
160 @retval EFI_BUFFER_TOO_SMALL The user did not provide a sufficient buffer.
161 @retval EFI_SUCCESS The user provided a sufficiently-sized buffer.
167 IN CONST EFI_SMM_ACCESS2_PROTOCOL
*This
,
168 IN OUT UINTN
*SmramMapSize
,
169 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramMap
173 UINTN NecessaryBufferSize
;
175 NecessaryBufferSize
= mSmmAccess
.NumberRegions
* sizeof (EFI_SMRAM_DESCRIPTOR
);
176 if (*SmramMapSize
< NecessaryBufferSize
) {
177 Status
= EFI_BUFFER_TOO_SMALL
;
179 CopyMem (SmramMap
, mSmmAccess
.SmramDesc
, NecessaryBufferSize
);
180 Status
= EFI_SUCCESS
;
183 *SmramMapSize
= NecessaryBufferSize
;
188 This function installs EFI_SMM_ACCESS_PROTOCOL.
190 @param ImageHandle Handle for the image of this driver
191 @param SystemTable Pointer to the EFI System Table
193 @retval EFI_UNSUPPORTED There's no Intel ICH on this platform
194 @return The status returned from InstallProtocolInterface().
199 SmmAccessEntryPoint (
200 IN EFI_HANDLE ImageHandle
,
201 IN EFI_SYSTEM_TABLE
*SystemTable
205 EFI_HOB_GUID_TYPE
*GuidHob
;
207 EFI_SMRAM_HOB_DESCRIPTOR_BLOCK
*SmramHob
;
211 // Get SMRAM info HOB
213 GuidHob
= GetFirstGuidHob (&gEfiSmmSmramMemoryGuid
);
214 if (GuidHob
== NULL
) {
215 DEBUG ((DEBUG_INFO
, "SMRAM HOB NOT found\n"));
216 return EFI_NOT_FOUND
;
219 SmramHob
= (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK
*)GET_GUID_HOB_DATA (GuidHob
);
220 SmmRegionNum
= SmramHob
->NumberOfSmmReservedRegions
;
221 mSmmAccess
.SmramDesc
= AllocateZeroPool (sizeof (EFI_SMRAM_DESCRIPTOR
) * SmmRegionNum
);
222 if (mSmmAccess
.SmramDesc
== NULL
) {
223 return EFI_OUT_OF_RESOURCES
;
226 CopyMem (mSmmAccess
.SmramDesc
, &SmramHob
->Descriptor
, sizeof (EFI_SMRAM_DESCRIPTOR
) * SmmRegionNum
);
228 DEBUG ((DEBUG_INFO
, "NumberOfSmmReservedRegions = 0x%x\n", SmmRegionNum
));
229 for (Index
= 0; Index
< SmmRegionNum
; Index
++) {
232 "%d: base=0x%x, size = 0x%x, State=0x%x\n",
234 SmramHob
->Descriptor
[Index
].PhysicalStart
,
235 SmramHob
->Descriptor
[Index
].PhysicalSize
,
236 SmramHob
->Descriptor
[Index
].RegionState
238 mSmmAccess
.SmramDesc
[Index
].RegionState
&= EFI_ALLOCATED
;
239 mSmmAccess
.SmramDesc
[Index
].RegionState
|= EFI_SMRAM_CLOSED
| EFI_CACHEABLE
;
242 mSmmAccess
.Signature
= SMM_ACCESS_PRIVATE_DATA_SIGNATURE
;
243 mSmmAccess
.NumberRegions
= SmmRegionNum
;
244 mSmmAccess
.SmmAccess
.Open
= Open
;
245 mSmmAccess
.SmmAccess
.Close
= Close
;
246 mSmmAccess
.SmmAccess
.Lock
= Lock
;
247 mSmmAccess
.SmmAccess
.GetCapabilities
= GetCapabilities
;
248 mSmmAccess
.SmmAccess
.LockState
= FALSE
;
249 mSmmAccess
.SmmAccess
.OpenState
= FALSE
;
250 mSmmAccess
.SmmRegionState
= EFI_SMRAM_CLOSED
;
252 Status
= gBS
->InstallMultipleProtocolInterfaces (
254 &gEfiSmmAccess2ProtocolGuid
,
255 &mSmmAccess
.SmmAccess
,