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 This program and the accompanying materials are licensed and made available
10 under the terms and conditions of the BSD License which accompanies this
11 distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
15 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <IndustryStandard/Acpi10.h>
19 #include <IndustryStandard/Pci22.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
26 #include <Protocol/IncompatiblePciDeviceSupport.h>
27 #include <Protocol/LegacyBios.h>
30 // The Legacy BIOS protocol has been located.
32 STATIC BOOLEAN mLegacyBiosInstalled
;
35 // The protocol interface this driver produces.
37 STATIC EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
38 mIncompatiblePciDeviceSupport
;
41 // Configuration template for the CheckDevice() protocol member function.
43 // Refer to Table 20 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
44 // the Platform Init 1.4a Spec, Volume 5.
46 // This structure is interpreted by the UpdatePciInfo() function in the edk2
47 // PCI Bus UEFI_DRIVER.
51 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR AddressSpaceDesc
;
52 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc
;
56 STATIC CONST MMIO64_PREFERENCE mConfiguration
= {
61 ACPI_ADDRESS_SPACE_DESCRIPTOR
, // Desc
63 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) -
65 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
,
69 ACPI_ADDRESS_SPACE_TYPE_MEM
, // ResType
72 64, // AddrSpaceGranularity:
73 // aperture selection hint
77 // no special alignment
79 MAX_UINT64
, // AddrTranslationOffset:
83 // use probed BAR size
89 ACPI_END_TAG_DESCRIPTOR
, // Desc
90 0 // Checksum: to be ignored
95 // The CheckDevice() member function has been called.
97 STATIC BOOLEAN mCheckDeviceCalled
;
101 Notification callback for Legacy BIOS protocol installation.
103 @param[in] Event Event whose notification function is being invoked.
105 @param[in] Context The pointer to the notification function's context, which
106 is implementation-dependent.
111 LegacyBiosInstalled (
117 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
119 ASSERT (!mCheckDeviceCalled
);
121 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
,
122 NULL
/* Registration */, (VOID
**)&LegacyBios
);
123 if (EFI_ERROR (Status
)) {
127 mLegacyBiosInstalled
= TRUE
;
130 // Close the event and deregister this callback.
132 Status
= gBS
->CloseEvent (Event
);
133 ASSERT_EFI_ERROR (Status
);
138 Returns a list of ACPI resource descriptors that detail the special resource
139 configuration requirements for an incompatible PCI device.
141 Prior to bus enumeration, the PCI bus driver will look for the presence of
142 the EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL. Only one instance of this
143 protocol can be present in the system. For each PCI device that the PCI bus
144 driver discovers, the PCI bus driver calls this function with the device's
145 vendor ID, device ID, revision ID, subsystem vendor ID, and subsystem device
146 ID. If the VendorId, DeviceId, RevisionId, SubsystemVendorId, or
147 SubsystemDeviceId value is set to (UINTN)-1, that field will be ignored. The
148 ID values that are not (UINTN)-1 will be used to identify the current device.
150 This function will only return EFI_SUCCESS. However, if the device is an
151 incompatible PCI device, a list of ACPI resource descriptors will be returned
152 in Configuration. Otherwise, NULL will be returned in Configuration instead.
153 The PCI bus driver does not need to allocate memory for Configuration.
154 However, it is the PCI bus driver's responsibility to free it. The PCI bus
155 driver then can configure this device with the information that is derived
156 from this list of resource nodes, rather than the result of BAR probing.
158 Only the following two resource descriptor types from the ACPI Specification
159 may be used to describe the incompatible PCI device resource requirements:
160 - QWORD Address Space Descriptor (ACPI 2.0, section 6.4.3.5.1; also ACPI 3.0)
161 - End Tag (ACPI 2.0, section 6.4.2.8; also ACPI 3.0)
163 The QWORD Address Space Descriptor can describe memory, I/O, and bus number
164 ranges for dynamic or fixed resources. The configuration of a PCI root bridge
165 is described with one or more QWORD Address Space Descriptors, followed by an
166 End Tag. See the ACPI Specification for details on the field values.
168 @param[in] This Pointer to the
169 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
172 @param[in] VendorId A unique ID to identify the manufacturer of
173 the PCI device. See the Conventional PCI
174 Specification 3.0 for details.
176 @param[in] DeviceId A unique ID to identify the particular PCI
177 device. See the Conventional PCI
178 Specification 3.0 for details.
180 @param[in] RevisionId A PCI device-specific revision identifier.
181 See the Conventional PCI Specification 3.0
184 @param[in] SubsystemVendorId Specifies the subsystem vendor ID. See the
185 Conventional PCI Specification 3.0 for
188 @param[in] SubsystemDeviceId Specifies the subsystem device ID. See the
189 Conventional PCI Specification 3.0 for
192 @param[out] Configuration A list of ACPI resource descriptors that
193 detail the configuration requirement.
195 @retval EFI_SUCCESS The function always returns EFI_SUCCESS.
201 IN EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
*This
,
205 IN UINTN SubsystemVendorId
,
206 IN UINTN SubsystemDeviceId
,
207 OUT VOID
**Configuration
210 mCheckDeviceCalled
= TRUE
;
213 // Unlike the general description of this protocol member suggests, there is
214 // nothing incompatible about the PCI devices that we'll match here. We'll
215 // match all PCI devices, and generate exactly one QWORD Address Space
216 // Descriptor for each. That descriptor will instruct the PCI Bus UEFI_DRIVER
217 // not to degrade 64-bit MMIO BARs for the device, even if a PCI option ROM
218 // BAR is present on the device.
220 // The concern captured in the PCI Bus UEFI_DRIVER is that a legacy BIOS boot
221 // (via a CSM) could dispatch a legacy option ROM on the device, which might
222 // have trouble with MMIO BARs that have been allocated outside of the 32-bit
223 // address space. But, if we don't support legacy option ROMs at all, then
224 // this problem cannot arise.
226 if (mLegacyBiosInstalled
) {
228 // Don't interfere with resource degradation.
230 *Configuration
= NULL
;
235 // This member function is mis-specified actually: it is supposed to allocate
236 // memory, but as specified, it could not return an error status. Thankfully,
237 // the edk2 PCI Bus UEFI_DRIVER actually handles error codes; see the
238 // UpdatePciInfo() function.
240 *Configuration
= AllocateCopyPool (sizeof mConfiguration
, &mConfiguration
);
241 if (*Configuration
== NULL
) {
243 "%a: 64-bit MMIO BARs may be degraded for PCI 0x%04x:0x%04x (rev %d)\n",
244 __FUNCTION__
, (UINT32
)VendorId
, (UINT32
)DeviceId
, (UINT8
)RevisionId
));
245 return EFI_OUT_OF_RESOURCES
;
252 Entry point for this driver.
254 @param[in] ImageHandle Image handle of this driver.
255 @param[in] SystemTable Pointer to SystemTable.
257 @retval EFI_SUCESS Driver has loaded successfully.
258 @retval EFI_UNSUPPORTED PCI resource allocation has been disabled.
259 @retval EFI_UNSUPPORTED There is no 64-bit PCI MMIO aperture.
260 @return Error codes from lower level functions.
266 IN EFI_HANDLE ImageHandle
,
267 IN EFI_SYSTEM_TABLE
*SystemTable
275 // If the PCI Bus driver is not supposed to allocate resources, then it makes
276 // no sense to install a protocol that influences the resource allocation.
278 // Similarly, if there is no 64-bit PCI MMIO aperture, then 64-bit MMIO BARs
279 // have to be allocated under 4 GB unconditionally.
281 if (PcdGetBool (PcdPciDisableBusEnumeration
) ||
282 PcdGet64 (PcdPciMmio64Size
) == 0) {
283 return EFI_UNSUPPORTED
;
287 // Otherwise, create a protocol notify to see if a CSM is present. (With the
288 // CSM absent, the PCI Bus driver won't have to worry about allocating 64-bit
289 // MMIO BARs in the 32-bit MMIO aperture, for the sake of a legacy BIOS.)
291 // If the Legacy BIOS Protocol is present at the time of this driver starting
292 // up, we can mark immediately that the PCI Bus driver should perform the
293 // usual 64-bit MMIO BAR degradation.
295 // Otherwise, if the Legacy BIOS Protocol is absent at startup, it may be
296 // installed later. However, if it doesn't show up until the first
297 // EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL.CheckDevice() call from the
298 // PCI Bus driver, then it never will:
300 // 1. The following drivers are dispatched in some unspecified order:
301 // - PCI Host Bridge DXE_DRIVER,
302 // - PCI Bus UEFI_DRIVER,
303 // - this DXE_DRIVER,
304 // - Legacy BIOS DXE_DRIVER.
306 // 2. The DXE_CORE enters BDS.
308 // 3. The platform BDS connects the PCI Root Bridge IO instances (produced by
309 // the PCI Host Bridge DXE_DRIVER).
311 // 4. The PCI Bus UEFI_DRIVER enumerates resources and calls into this
312 // DXE_DRIVER (CheckDevice()).
314 // 5. This driver remembers if EFI_LEGACY_BIOS_PROTOCOL has been installed
315 // sometime during step 1 (produced by the Legacy BIOS DXE_DRIVER).
317 // For breaking this order, the Legacy BIOS DXE_DRIVER would have to install
318 // its protocol after the firmware enters BDS, which cannot happen.
320 Status
= gBS
->CreateEvent (EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
,
321 LegacyBiosInstalled
, NULL
/* Context */, &Event
);
322 if (EFI_ERROR (Status
)) {
326 Status
= gBS
->RegisterProtocolNotify (&gEfiLegacyBiosProtocolGuid
, Event
,
328 if (EFI_ERROR (Status
)) {
332 Status
= gBS
->SignalEvent (Event
);
333 ASSERT_EFI_ERROR (Status
);
335 mIncompatiblePciDeviceSupport
.CheckDevice
= CheckDevice
;
336 Status
= gBS
->InstallMultipleProtocolInterfaces (&ImageHandle
,
337 &gEfiIncompatiblePciDeviceSupportProtocolGuid
,
338 &mIncompatiblePciDeviceSupport
, NULL
);
339 if (EFI_ERROR (Status
)) {
346 if (!mLegacyBiosInstalled
) {
347 EFI_STATUS CloseStatus
;
349 CloseStatus
= gBS
->CloseEvent (Event
);
350 ASSERT_EFI_ERROR (CloseStatus
);