3 A PEIM with the following responsibilities:
5 - verify & configure the Q35 TSEG in the entry point,
6 - provide SMRAM access by producing PEI_SMM_ACCESS_PPI,
7 - set aside the SMM_S3_RESUME_STATE object at the bottom of TSEG, and expose
8 it via the gEfiAcpiVariableGuid GUID HOB.
10 This PEIM runs from RAM, so we can write to variables with static storage
13 Copyright (C) 2013, 2015, Red Hat, Inc.<BR>
14 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
16 SPDX-License-Identifier: BSD-2-Clause-Patent
20 #include <Guid/AcpiS3Context.h>
21 #include <Library/BaseLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/HobLib.h>
25 #include <Library/IoLib.h>
26 #include <Library/PcdLib.h>
27 #include <Library/PciLib.h>
28 #include <Library/PeiServicesLib.h>
29 #include <Ppi/SmmAccess.h>
31 #include <OvmfPlatforms.h>
33 #include "SmramInternal.h"
36 // PEI_SMM_ACCESS_PPI implementation.
40 Opens the SMRAM area to be accessible by a PEIM driver.
42 This function "opens" SMRAM so that it is visible while not inside of SMM.
43 The function should return EFI_UNSUPPORTED if the hardware does not support
44 hiding of SMRAM. The function should return EFI_DEVICE_ERROR if the SMRAM
45 configuration is locked.
47 @param PeiServices General purpose services available to every
49 @param This The pointer to the SMM Access Interface.
50 @param DescriptorIndex The region of SMRAM to Open.
52 @retval EFI_SUCCESS The region was successfully opened.
53 @retval EFI_DEVICE_ERROR The region could not be opened because locked
55 @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.
62 IN EFI_PEI_SERVICES
**PeiServices
,
63 IN PEI_SMM_ACCESS_PPI
*This
,
64 IN UINTN DescriptorIndex
67 if (DescriptorIndex
>= DescIdxCount
) {
68 return EFI_INVALID_PARAMETER
;
72 // According to current practice, DescriptorIndex is not considered at all,
73 // beyond validating it.
75 return SmramAccessOpen (&This
->LockState
, &This
->OpenState
);
79 Inhibits access to the SMRAM.
81 This function "closes" SMRAM so that it is not visible while outside of SMM.
82 The function should return EFI_UNSUPPORTED if the hardware does not support
85 @param PeiServices General purpose services available to every
87 @param This The pointer to the SMM Access Interface.
88 @param DescriptorIndex The region of SMRAM to Close.
90 @retval EFI_SUCCESS The region was successfully closed.
91 @retval EFI_DEVICE_ERROR The region could not be closed because
93 @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.
100 IN EFI_PEI_SERVICES
**PeiServices
,
101 IN PEI_SMM_ACCESS_PPI
*This
,
102 IN UINTN DescriptorIndex
105 if (DescriptorIndex
>= DescIdxCount
) {
106 return EFI_INVALID_PARAMETER
;
110 // According to current practice, DescriptorIndex is not considered at all,
111 // beyond validating it.
113 return SmramAccessClose (&This
->LockState
, &This
->OpenState
);
117 Inhibits access to the SMRAM.
119 This function prohibits access to the SMRAM region. This function is usually
120 implemented such that it is a write-once operation.
122 @param PeiServices General purpose services available to every
124 @param This The pointer to the SMM Access Interface.
125 @param DescriptorIndex The region of SMRAM to Close.
127 @retval EFI_SUCCESS The region was successfully locked.
128 @retval EFI_DEVICE_ERROR The region could not be locked because at
129 least one range is still open.
130 @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.
137 IN EFI_PEI_SERVICES
**PeiServices
,
138 IN PEI_SMM_ACCESS_PPI
*This
,
139 IN UINTN DescriptorIndex
142 if (DescriptorIndex
>= DescIdxCount
) {
143 return EFI_INVALID_PARAMETER
;
147 // According to current practice, DescriptorIndex is not considered at all,
148 // beyond validating it.
150 return SmramAccessLock (&This
->LockState
, &This
->OpenState
);
154 Queries the memory controller for the possible regions that will support
157 @param PeiServices General purpose services available to every
159 @param This The pointer to the SmmAccessPpi Interface.
160 @param SmramMapSize The pointer to the variable containing size of
161 the buffer to contain the description
163 @param SmramMap The buffer containing the data describing the
164 Smram region descriptors.
166 @retval EFI_BUFFER_TOO_SMALL The user did not provide a sufficient buffer.
167 @retval EFI_SUCCESS The user provided a sufficiently-sized buffer.
173 SmmAccessPeiGetCapabilities (
174 IN EFI_PEI_SERVICES
**PeiServices
,
175 IN PEI_SMM_ACCESS_PPI
*This
,
176 IN OUT UINTN
*SmramMapSize
,
177 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramMap
180 return SmramAccessGetCapabilities (
189 // LockState and OpenState will be filled in by the entry point.
191 STATIC PEI_SMM_ACCESS_PPI mAccess
= {
195 &SmmAccessPeiGetCapabilities
198 STATIC EFI_PEI_PPI_DESCRIPTOR mPpiList
[] = {
200 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
201 &gPeiSmmAccessPpiGuid
, &mAccess
206 // Utility functions.
214 IoWrite8 (0x70, Index
);
215 return IoRead8 (0x71);
220 GetSystemMemorySizeBelow4gb (
227 Cmos0x34
= CmosRead8 (0x34);
228 Cmos0x35
= CmosRead8 (0x35);
230 return ((Cmos0x35
<< 8 | Cmos0x34
) << 16) + SIZE_16MB
;
234 // Entry point of this driver.
238 SmmAccessPeiEntryPoint (
239 IN EFI_PEI_FILE_HANDLE FileHandle
,
240 IN CONST EFI_PEI_SERVICES
**PeiServices
243 UINT16 HostBridgeDevId
;
246 UINT32 TopOfLowRam
, TopOfLowRamMb
;
249 EFI_SMRAM_DESCRIPTOR SmramMap
[DescIdxCount
];
253 // This module should only be included if SMRAM support is required.
255 ASSERT (FeaturePcdGet (PcdSmmSmramRequire
));
258 // Verify if we're running on a Q35 machine type.
260 HostBridgeDevId
= PciRead16 (OVMF_HOSTBRIDGE_DID
);
261 if (HostBridgeDevId
!= INTEL_Q35_MCH_DEVICE_ID
) {
264 "%a: no SMRAM with host bridge DID=0x%04x; only "
265 "DID=0x%04x (Q35) is supported\n",
268 INTEL_Q35_MCH_DEVICE_ID
274 // Confirm if QEMU supports SMRAM.
276 // With no support for it, the ESMRAMC (Extended System Management RAM
277 // Control) register reads as zero. If there is support, the cache-enable
278 // bits are hard-coded as 1 by QEMU.
280 EsmramcVal
= PciRead8 (DRAMC_REGISTER_Q35 (MCH_ESMRAMC
));
281 RegMask8
= MCH_ESMRAMC_SM_CACHE
| MCH_ESMRAMC_SM_L1
| MCH_ESMRAMC_SM_L2
;
282 if ((EsmramcVal
& RegMask8
) != RegMask8
) {
285 "%a: this Q35 implementation lacks SMRAM\n",
291 TopOfLowRam
= GetSystemMemorySizeBelow4gb ();
292 ASSERT ((TopOfLowRam
& (SIZE_1MB
- 1)) == 0);
293 TopOfLowRamMb
= TopOfLowRam
>> 20;
296 // Some of the following registers are no-ops for QEMU at the moment, but it
297 // is recommended to set them correctly, since the ESMRAMC that we ultimately
298 // care about is in the same set of registers.
300 // First, we disable the integrated VGA, and set both the GTT Graphics Memory
301 // Size and the Graphics Mode Select memory pre-allocation fields to zero.
302 // This takes just one write to the Graphics Control Register.
304 PciWrite16 (DRAMC_REGISTER_Q35 (MCH_GGC
), MCH_GGC_IVD
);
307 // Set Top of Low Usable DRAM.
310 DRAMC_REGISTER_Q35 (MCH_TOLUD
),
311 (UINT16
)(TopOfLowRamMb
<< MCH_TOLUD_MB_SHIFT
)
315 // Given the zero graphics memory sizes configured above, set the
316 // graphics-related stolen memory bases to the same as TOLUD.
319 DRAMC_REGISTER_Q35 (MCH_GBSM
),
320 TopOfLowRamMb
<< MCH_GBSM_MB_SHIFT
323 DRAMC_REGISTER_Q35 (MCH_BGSM
),
324 TopOfLowRamMb
<< MCH_BGSM_MB_SHIFT
328 // Set TSEG Memory Base.
330 InitQ35TsegMbytes ();
332 DRAMC_REGISTER_Q35 (MCH_TSEGMB
),
333 (TopOfLowRamMb
- mQ35TsegMbytes
) << MCH_TSEGMB_MB_SHIFT
337 // Set TSEG size, and disable TSEG visibility outside of SMM. Note that the
338 // T_EN bit has inverse meaning; when T_EN is set, then TSEG visibility is
339 // *restricted* to SMM.
341 EsmramcVal
&= ~(UINT32
)MCH_ESMRAMC_TSEG_MASK
;
342 EsmramcVal
|= mQ35TsegMbytes
== 8 ? MCH_ESMRAMC_TSEG_8MB
:
343 mQ35TsegMbytes
== 2 ? MCH_ESMRAMC_TSEG_2MB
:
344 mQ35TsegMbytes
== 1 ? MCH_ESMRAMC_TSEG_1MB
:
345 MCH_ESMRAMC_TSEG_EXT
;
346 EsmramcVal
|= MCH_ESMRAMC_T_EN
;
347 PciWrite8 (DRAMC_REGISTER_Q35 (MCH_ESMRAMC
), EsmramcVal
);
350 // TSEG should be closed (see above), but unlocked, initially. Set G_SMRAME
351 // (Global SMRAM Enable) too, as both D_LCK and T_EN depend on it.
354 DRAMC_REGISTER_Q35 (MCH_SMRAM
),
355 (UINT8
)((~(UINT32
)MCH_SMRAM_D_LCK
) & 0xff),
360 // Create the GUID HOB and point it to the first SMRAM range.
362 GetStates (&mAccess
.LockState
, &mAccess
.OpenState
);
363 SmramMapSize
= sizeof SmramMap
;
364 Status
= SmramAccessGetCapabilities (
370 ASSERT_EFI_ERROR (Status
);
377 Count
= SmramMapSize
/ sizeof SmramMap
[0];
380 "%a: SMRAM map follows, %d entries\n",
386 "% 20a % 20a % 20a % 20a\n",
392 for (Idx
= 0; Idx
< Count
; ++Idx
) {
395 "% 20Lx % 20Lx % 20Lx % 20Lx\n",
396 SmramMap
[Idx
].PhysicalStart
,
397 SmramMap
[Idx
].PhysicalSize
,
398 SmramMap
[Idx
].CpuStart
,
399 SmramMap
[Idx
].RegionState
405 GuidHob
= BuildGuidHob (
406 &gEfiAcpiVariableGuid
,
407 sizeof SmramMap
[DescIdxSmmS3ResumeState
]
409 if (GuidHob
== NULL
) {
410 return EFI_OUT_OF_RESOURCES
;
415 &SmramMap
[DescIdxSmmS3ResumeState
],
416 sizeof SmramMap
[DescIdxSmmS3ResumeState
]
420 // SmramAccessLock() depends on "mQ35SmramAtDefaultSmbase"; init the latter
421 // just before exposing the former via PEI_SMM_ACCESS_PPI.Lock().
423 InitQ35SmramAtDefaultSmbase ();
426 // We're done. The next step should succeed, but even if it fails, we can't
427 // roll back the above BuildGuidHob() allocation, because PEI doesn't support
430 return PeiServicesInstallPpi (mPpiList
);
434 // We really don't want to continue in this case.
438 return EFI_UNSUPPORTED
;