2 A simple DXE_DRIVER that causes the PCI Bus UEFI_DRIVER to allocate 64-bit
3 MMIO BARs above 4 GB, regardless of option ROM availability (as long as a CSM
4 is not present), conserving 32-bit MMIO aperture for 32-bit BARs.
6 Copyright (C) 2016, Red Hat, Inc.
7 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <IndustryStandard/Acpi10.h>
15 #include <IndustryStandard/Pci22.h>
17 #include <Library/DebugLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/CcProbeLib.h>
23 #include <Protocol/IncompatiblePciDeviceSupport.h>
24 #include <Protocol/LegacyBios.h>
27 // The Legacy BIOS protocol has been located.
29 STATIC BOOLEAN mLegacyBiosInstalled
;
32 // The protocol interface this driver produces.
34 STATIC EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
35 mIncompatiblePciDeviceSupport
;
38 // Configuration template for the CheckDevice() protocol member function.
40 // Refer to Table 20 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
41 // the Platform Init 1.4a Spec, Volume 5.
43 // This structure is interpreted by the UpdatePciInfo() function in the edk2
44 // PCI Bus UEFI_DRIVER.
46 // This structure looks like:
47 // AddressDesc-1 + AddressDesc-2 + ... + AddressDesc-n + EndDesc
49 STATIC CONST EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR mMmio64Configuration
= {
50 ACPI_ADDRESS_SPACE_DESCRIPTOR
, // Desc
52 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) -
54 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
,
58 ACPI_ADDRESS_SPACE_TYPE_MEM
, // ResType
61 64, // AddrSpaceGranularity:
62 // aperture selection hint
66 // no special alignment
68 MAX_UINT64
, // AddrTranslationOffset:
72 // use probed BAR size
76 // mOptionRomConfiguration is present only in Td guest.
77 // Host VMM can inject option ROM which is untrusted in Td guest,
78 // so PCI option ROM needs to be ignored.
79 // According to "Table 20. ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage"
80 // PI spec 1.7, type-specific flags can be set to 0 when
81 // Address Translation Offset == 6 to skip device option ROM.
83 STATIC CONST EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR mOptionRomConfiguration
= {
84 ACPI_ADDRESS_SPACE_DESCRIPTOR
, // Desc
86 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) -
88 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
,
92 ACPI_ADDRESS_SPACE_TYPE_MEM
, // ResType
94 0, // Disable option roms SpecificFlag
95 64, // AddrSpaceGranularity:
96 // aperture selection hint
98 MAX_UINT64
, // AddrRangeMin
99 MAX_UINT64
, // AddrRangeMax:
100 // no special alignment
102 6, // AddrTranslationOffset:
106 // use probed BAR size
109 STATIC CONST EFI_ACPI_END_TAG_DESCRIPTOR mEndDesc
= {
110 ACPI_END_TAG_DESCRIPTOR
, // Desc
111 0 // Checksum: to be ignored
115 // The CheckDevice() member function has been called.
117 STATIC BOOLEAN mCheckDeviceCalled
;
120 Notification callback for Legacy BIOS protocol installation.
122 @param[in] Event Event whose notification function is being invoked.
124 @param[in] Context The pointer to the notification function's context, which
125 is implementation-dependent.
130 LegacyBiosInstalled (
136 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
138 ASSERT (!mCheckDeviceCalled
);
140 Status
= gBS
->LocateProtocol (
141 &gEfiLegacyBiosProtocolGuid
,
142 NULL
/* Registration */,
145 if (EFI_ERROR (Status
)) {
149 mLegacyBiosInstalled
= TRUE
;
152 // Close the event and deregister this callback.
154 Status
= gBS
->CloseEvent (Event
);
155 ASSERT_EFI_ERROR (Status
);
159 Returns a list of ACPI resource descriptors that detail the special resource
160 configuration requirements for an incompatible PCI device.
162 Prior to bus enumeration, the PCI bus driver will look for the presence of
163 the EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL. Only one instance of this
164 protocol can be present in the system. For each PCI device that the PCI bus
165 driver discovers, the PCI bus driver calls this function with the device's
166 vendor ID, device ID, revision ID, subsystem vendor ID, and subsystem device
167 ID. If the VendorId, DeviceId, RevisionId, SubsystemVendorId, or
168 SubsystemDeviceId value is set to (UINTN)-1, that field will be ignored. The
169 ID values that are not (UINTN)-1 will be used to identify the current device.
171 This function will only return EFI_SUCCESS. However, if the device is an
172 incompatible PCI device, a list of ACPI resource descriptors will be returned
173 in Configuration. Otherwise, NULL will be returned in Configuration instead.
174 The PCI bus driver does not need to allocate memory for Configuration.
175 However, it is the PCI bus driver's responsibility to free it. The PCI bus
176 driver then can configure this device with the information that is derived
177 from this list of resource nodes, rather than the result of BAR probing.
179 Only the following two resource descriptor types from the ACPI Specification
180 may be used to describe the incompatible PCI device resource requirements:
181 - QWORD Address Space Descriptor (ACPI 2.0, section 6.4.3.5.1; also ACPI 3.0)
182 - End Tag (ACPI 2.0, section 6.4.2.8; also ACPI 3.0)
184 The QWORD Address Space Descriptor can describe memory, I/O, and bus number
185 ranges for dynamic or fixed resources. The configuration of a PCI root bridge
186 is described with one or more QWORD Address Space Descriptors, followed by an
187 End Tag. See the ACPI Specification for details on the field values.
189 @param[in] This Pointer to the
190 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
193 @param[in] VendorId A unique ID to identify the manufacturer of
194 the PCI device. See the Conventional PCI
195 Specification 3.0 for details.
197 @param[in] DeviceId A unique ID to identify the particular PCI
198 device. See the Conventional PCI
199 Specification 3.0 for details.
201 @param[in] RevisionId A PCI device-specific revision identifier.
202 See the Conventional PCI Specification 3.0
205 @param[in] SubsystemVendorId Specifies the subsystem vendor ID. See the
206 Conventional PCI Specification 3.0 for
209 @param[in] SubsystemDeviceId Specifies the subsystem device ID. See the
210 Conventional PCI Specification 3.0 for
213 @param[out] Configuration A list of ACPI resource descriptors that
214 detail the configuration requirement.
216 @retval EFI_SUCCESS The function always returns EFI_SUCCESS.
222 IN EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
*This
,
226 IN UINTN SubsystemVendorId
,
227 IN UINTN SubsystemDeviceId
,
228 OUT VOID
**Configuration
231 mCheckDeviceCalled
= TRUE
;
236 // Unlike the general description of this protocol member suggests, there is
237 // nothing incompatible about the PCI devices that we'll match here. We'll
238 // match all PCI devices, and generate exactly one QWORD Address Space
239 // Descriptor for each. That descriptor will instruct the PCI Bus UEFI_DRIVER
240 // not to degrade 64-bit MMIO BARs for the device, even if a PCI option ROM
241 // BAR is present on the device.
243 // The concern captured in the PCI Bus UEFI_DRIVER is that a legacy BIOS boot
244 // (via a CSM) could dispatch a legacy option ROM on the device, which might
245 // have trouble with MMIO BARs that have been allocated outside of the 32-bit
246 // address space. But, if we don't support legacy option ROMs at all, then
247 // this problem cannot arise.
249 if (mLegacyBiosInstalled
) {
251 // Don't interfere with resource degradation.
253 *Configuration
= NULL
;
258 // This member function is mis-specified actually: it is supposed to allocate
259 // memory, but as specified, it could not return an error status. Thankfully,
260 // the edk2 PCI Bus UEFI_DRIVER actually handles error codes; see the
261 // UpdatePciInfo() function.
263 Length
= sizeof mMmio64Configuration
+ sizeof mEndDesc
;
266 // In Td guest OptionRom is not allowed.
269 Length
+= sizeof mOptionRomConfiguration
;
272 *Configuration
= AllocateZeroPool (Length
);
274 if (*Configuration
== NULL
) {
277 "%a: 64-bit MMIO BARs may be degraded for PCI 0x%04x:0x%04x (rev %d)\n",
283 return EFI_OUT_OF_RESOURCES
;
286 Ptr
= (UINT8
*)(UINTN
)*Configuration
;
287 CopyMem (Ptr
, &mMmio64Configuration
, sizeof mMmio64Configuration
);
288 Length
= sizeof mMmio64Configuration
;
291 CopyMem (Ptr
+ Length
, &mOptionRomConfiguration
, sizeof mOptionRomConfiguration
);
292 Length
+= sizeof mOptionRomConfiguration
;
295 CopyMem (Ptr
+ Length
, &mEndDesc
, sizeof mEndDesc
);
301 Entry point for this driver.
303 @param[in] ImageHandle Image handle of this driver.
304 @param[in] SystemTable Pointer to SystemTable.
306 @retval EFI_SUCESS Driver has loaded successfully.
307 @retval EFI_UNSUPPORTED PCI resource allocation has been disabled.
308 @retval EFI_UNSUPPORTED There is no 64-bit PCI MMIO aperture.
309 @return Error codes from lower level functions.
315 IN EFI_HANDLE ImageHandle
,
316 IN EFI_SYSTEM_TABLE
*SystemTable
324 // If there is no 64-bit PCI MMIO aperture, then 64-bit MMIO BARs have to be
325 // allocated under 4 GB unconditionally.
327 if (PcdGet64 (PcdPciMmio64Size
) == 0) {
328 return EFI_UNSUPPORTED
;
332 // Otherwise, create a protocol notify to see if a CSM is present. (With the
333 // CSM absent, the PCI Bus driver won't have to worry about allocating 64-bit
334 // MMIO BARs in the 32-bit MMIO aperture, for the sake of a legacy BIOS.)
336 // If the Legacy BIOS Protocol is present at the time of this driver starting
337 // up, we can mark immediately that the PCI Bus driver should perform the
338 // usual 64-bit MMIO BAR degradation.
340 // Otherwise, if the Legacy BIOS Protocol is absent at startup, it may be
341 // installed later. However, if it doesn't show up until the first
342 // EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL.CheckDevice() call from the
343 // PCI Bus driver, then it never will:
345 // 1. The following drivers are dispatched in some unspecified order:
346 // - PCI Host Bridge DXE_DRIVER,
347 // - PCI Bus UEFI_DRIVER,
348 // - this DXE_DRIVER,
349 // - Legacy BIOS DXE_DRIVER.
351 // 2. The DXE_CORE enters BDS.
353 // 3. The platform BDS connects the PCI Root Bridge IO instances (produced by
354 // the PCI Host Bridge DXE_DRIVER).
356 // 4. The PCI Bus UEFI_DRIVER enumerates resources and calls into this
357 // DXE_DRIVER (CheckDevice()).
359 // 5. This driver remembers if EFI_LEGACY_BIOS_PROTOCOL has been installed
360 // sometime during step 1 (produced by the Legacy BIOS DXE_DRIVER).
362 // For breaking this order, the Legacy BIOS DXE_DRIVER would have to install
363 // its protocol after the firmware enters BDS, which cannot happen.
365 Status
= gBS
->CreateEvent (
372 if (EFI_ERROR (Status
)) {
376 Status
= gBS
->RegisterProtocolNotify (
377 &gEfiLegacyBiosProtocolGuid
,
381 if (EFI_ERROR (Status
)) {
385 Status
= gBS
->SignalEvent (Event
);
386 ASSERT_EFI_ERROR (Status
);
388 mIncompatiblePciDeviceSupport
.CheckDevice
= CheckDevice
;
389 Status
= gBS
->InstallMultipleProtocolInterfaces (
391 &gEfiIncompatiblePciDeviceSupportProtocolGuid
,
392 &mIncompatiblePciDeviceSupport
,
395 if (EFI_ERROR (Status
)) {
402 if (!mLegacyBiosInstalled
) {
403 EFI_STATUS CloseStatus
;
405 CloseStatus
= gBS
->CloseEvent (Event
);
406 ASSERT_EFI_ERROR (CloseStatus
);