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.
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <IndustryStandard/Acpi10.h>
18 #include <IndustryStandard/Pci22.h>
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
25 #include <Protocol/IncompatiblePciDeviceSupport.h>
26 #include <Protocol/LegacyBios.h>
29 // The Legacy BIOS protocol has been located.
31 STATIC BOOLEAN mLegacyBiosInstalled
;
34 // The protocol interface this driver produces.
36 STATIC EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
37 mIncompatiblePciDeviceSupport
;
40 // Configuration template for the CheckDevice() protocol member function.
42 // Refer to Table 20 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
43 // the Platform Init 1.4a Spec, Volume 5.
45 // This structure is interpreted by the UpdatePciInfo() function in the edk2
46 // PCI Bus UEFI_DRIVER.
50 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR AddressSpaceDesc
;
51 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc
;
55 STATIC CONST MMIO64_PREFERENCE mConfiguration
= {
60 ACPI_ADDRESS_SPACE_DESCRIPTOR
, // Desc
62 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) -
64 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
,
68 ACPI_ADDRESS_SPACE_TYPE_MEM
, // ResType
69 PCI_ACPI_UNUSED
, // GenFlag
70 PCI_ACPI_UNUSED
, // SpecificFlag
71 64, // AddrSpaceGranularity:
72 // aperture selection hint
74 PCI_ACPI_UNUSED
, // AddrRangeMin
75 PCI_BAR_OLD_ALIGN
, // AddrRangeMax:
76 // no special alignment
78 PCI_BAR_ALL
, // AddrTranslationOffset:
81 PCI_BAR_NOCHANGE
// AddrLen:
82 // use probed BAR size
88 ACPI_END_TAG_DESCRIPTOR
, // Desc
89 0 // Checksum: to be ignored
94 // The CheckDevice() member function has been called.
96 STATIC BOOLEAN mCheckDeviceCalled
;
100 Notification callback for Legacy BIOS protocol installation.
102 @param[in] Event Event whose notification function is being invoked.
104 @param[in] Context The pointer to the notification function's context, which
105 is implementation-dependent.
110 LegacyBiosInstalled (
116 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
118 ASSERT (!mCheckDeviceCalled
);
120 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
,
121 NULL
/* Registration */, (VOID
**)&LegacyBios
);
122 if (EFI_ERROR (Status
)) {
126 mLegacyBiosInstalled
= TRUE
;
129 // Close the event and deregister this callback.
131 Status
= gBS
->CloseEvent (Event
);
132 ASSERT_EFI_ERROR (Status
);
137 Returns a list of ACPI resource descriptors that detail the special resource
138 configuration requirements for an incompatible PCI device.
140 Prior to bus enumeration, the PCI bus driver will look for the presence of
141 the EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL. Only one instance of this
142 protocol can be present in the system. For each PCI device that the PCI bus
143 driver discovers, the PCI bus driver calls this function with the device's
144 vendor ID, device ID, revision ID, subsystem vendor ID, and subsystem device
145 ID. If the VendorId, DeviceId, RevisionId, SubsystemVendorId, or
146 SubsystemDeviceId value is set to (UINTN)-1, that field will be ignored. The
147 ID values that are not (UINTN)-1 will be used to identify the current device.
149 This function will only return EFI_SUCCESS. However, if the device is an
150 incompatible PCI device, a list of ACPI resource descriptors will be returned
151 in Configuration. Otherwise, NULL will be returned in Configuration instead.
152 The PCI bus driver does not need to allocate memory for Configuration.
153 However, it is the PCI bus driver's responsibility to free it. The PCI bus
154 driver then can configure this device with the information that is derived
155 from this list of resource nodes, rather than the result of BAR probing.
157 Only the following two resource descriptor types from the ACPI Specification
158 may be used to describe the incompatible PCI device resource requirements:
159 - QWORD Address Space Descriptor (ACPI 2.0, section 6.4.3.5.1; also ACPI 3.0)
160 - End Tag (ACPI 2.0, section 6.4.2.8; also ACPI 3.0)
162 The QWORD Address Space Descriptor can describe memory, I/O, and bus number
163 ranges for dynamic or fixed resources. The configuration of a PCI root bridge
164 is described with one or more QWORD Address Space Descriptors, followed by an
165 End Tag. See the ACPI Specification for details on the field values.
167 @param[in] This Pointer to the
168 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
171 @param[in] VendorId A unique ID to identify the manufacturer of
172 the PCI device. See the Conventional PCI
173 Specification 3.0 for details.
175 @param[in] DeviceId A unique ID to identify the particular PCI
176 device. See the Conventional PCI
177 Specification 3.0 for details.
179 @param[in] RevisionId A PCI device-specific revision identifier.
180 See the Conventional PCI Specification 3.0
183 @param[in] SubsystemVendorId Specifies the subsystem vendor ID. See the
184 Conventional PCI Specification 3.0 for
187 @param[in] SubsystemDeviceId Specifies the subsystem device ID. See the
188 Conventional PCI Specification 3.0 for
191 @param[out] Configuration A list of ACPI resource descriptors that
192 detail the configuration requirement.
194 @retval EFI_SUCCESS The function always returns EFI_SUCCESS.
200 IN EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
*This
,
204 IN UINTN SubsystemVendorId
,
205 IN UINTN SubsystemDeviceId
,
206 OUT VOID
**Configuration
209 mCheckDeviceCalled
= TRUE
;
212 // Unlike the general description of this protocol member suggests, there is
213 // nothing incompatible about the PCI devices that we'll match here. We'll
214 // match all PCI devices, and generate exactly one QWORD Address Space
215 // Descriptor for each. That descriptor will instruct the PCI Bus UEFI_DRIVER
216 // not to degrade 64-bit MMIO BARs for the device, even if a PCI option ROM
217 // BAR is present on the device.
219 // The concern captured in the PCI Bus UEFI_DRIVER is that a legacy BIOS boot
220 // (via a CSM) could dispatch a legacy option ROM on the device, which might
221 // have trouble with MMIO BARs that have been allocated outside of the 32-bit
222 // address space. But, if we don't support legacy option ROMs at all, then
223 // this problem cannot arise.
225 if (mLegacyBiosInstalled
) {
227 // Don't interfere with resource degradation.
229 *Configuration
= NULL
;
234 // This member function is mis-specified actually: it is supposed to allocate
235 // memory, but as specified, it could not return an error status. Thankfully,
236 // the edk2 PCI Bus UEFI_DRIVER actually handles error codes; see the
237 // UpdatePciInfo() function.
239 *Configuration
= AllocateCopyPool (sizeof mConfiguration
, &mConfiguration
);
240 if (*Configuration
== NULL
) {
242 "%a: 64-bit MMIO BARs may be degraded for PCI 0x%04x:0x%04x (rev %d)\n",
243 __FUNCTION__
, (UINT32
)VendorId
, (UINT32
)DeviceId
, (UINT8
)RevisionId
));
244 return EFI_OUT_OF_RESOURCES
;
251 Entry point for this driver.
253 @param[in] ImageHandle Image handle of this driver.
254 @param[in] SystemTable Pointer to SystemTable.
256 @retval EFI_SUCESS Driver has loaded successfully.
257 @retval EFI_UNSUPPORTED PCI resource allocation has been disabled.
258 @retval EFI_UNSUPPORTED There is no 64-bit PCI MMIO aperture.
259 @return Error codes from lower level functions.
265 IN EFI_HANDLE ImageHandle
,
266 IN EFI_SYSTEM_TABLE
*SystemTable
274 // If the PCI Bus driver is not supposed to allocate resources, then it makes
275 // no sense to install a protocol that influences the resource allocation.
277 // Similarly, if there is no 64-bit PCI MMIO aperture, then 64-bit MMIO BARs
278 // have to be allocated under 4 GB unconditionally.
280 if (PcdGetBool (PcdPciDisableBusEnumeration
) ||
281 PcdGet64 (PcdPciMmio64Size
) == 0) {
282 return EFI_UNSUPPORTED
;
286 // Otherwise, create a protocol notify to see if a CSM is present. (With the
287 // CSM absent, the PCI Bus driver won't have to worry about allocating 64-bit
288 // MMIO BARs in the 32-bit MMIO aperture, for the sake of a legacy BIOS.)
290 // If the Legacy BIOS Protocol is present at the time of this driver starting
291 // up, we can mark immediately that the PCI Bus driver should perform the
292 // usual 64-bit MMIO BAR degradation.
294 // Otherwise, if the Legacy BIOS Protocol is absent at startup, it may be
295 // installed later. However, if it doesn't show up until the first
296 // EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL.CheckDevice() call from the
297 // PCI Bus driver, then it never will:
299 // 1. The following drivers are dispatched in some unspecified order:
300 // - PCI Host Bridge DXE_DRIVER,
301 // - PCI Bus UEFI_DRIVER,
302 // - this DXE_DRIVER,
303 // - Legacy BIOS DXE_DRIVER.
305 // 2. The DXE_CORE enters BDS.
307 // 3. The platform BDS connects the PCI Root Bridge IO instances (produced by
308 // the PCI Host Bridge DXE_DRIVER).
310 // 4. The PCI Bus UEFI_DRIVER enumerates resources and calls into this
311 // DXE_DRIVER (CheckDevice()).
313 // 5. This driver remembers if EFI_LEGACY_BIOS_PROTOCOL has been installed
314 // sometime during step 1 (produced by the Legacy BIOS DXE_DRIVER).
316 // For breaking this order, the Legacy BIOS DXE_DRIVER would have to install
317 // its protocol after the firmware enters BDS, which cannot happen.
319 Status
= gBS
->CreateEvent (EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
,
320 LegacyBiosInstalled
, NULL
/* Context */, &Event
);
321 if (EFI_ERROR (Status
)) {
325 Status
= gBS
->RegisterProtocolNotify (&gEfiLegacyBiosProtocolGuid
, Event
,
327 if (EFI_ERROR (Status
)) {
331 Status
= gBS
->SignalEvent (Event
);
332 ASSERT_EFI_ERROR (Status
);
334 mIncompatiblePciDeviceSupport
.CheckDevice
= CheckDevice
;
335 Status
= gBS
->InstallMultipleProtocolInterfaces (&ImageHandle
,
336 &gEfiIncompatiblePciDeviceSupportProtocolGuid
,
337 &mIncompatiblePciDeviceSupport
, NULL
);
338 if (EFI_ERROR (Status
)) {
345 if (!mLegacyBiosInstalled
) {
346 EFI_STATUS CloseStatus
;
348 CloseStatus
= gBS
->CloseEvent (Event
);
349 ASSERT_EFI_ERROR (CloseStatus
);