]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.c
MdePkg/BaseMemoryLib: widen aligned accesses to 32 or 64 bits
[mirror_edk2.git] / OvmfPkg / IncompatiblePciDeviceSupportDxe / IncompatiblePciDeviceSupport.c
1 /** @file
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.
5
6 Copyright (C) 2016, Red Hat, Inc.
7
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
12
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.
15 **/
16
17 #include <IndustryStandard/Acpi10.h>
18 #include <IndustryStandard/Pci22.h>
19
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24
25 #include <Protocol/IncompatiblePciDeviceSupport.h>
26 #include <Protocol/LegacyBios.h>
27
28 //
29 // The Legacy BIOS protocol has been located.
30 //
31 STATIC BOOLEAN mLegacyBiosInstalled;
32
33 //
34 // The protocol interface this driver produces.
35 //
36 STATIC EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
37 mIncompatiblePciDeviceSupport;
38
39 //
40 // Configuration template for the CheckDevice() protocol member function.
41 //
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.
44 //
45 // This structure is interpreted by the UpdatePciInfo() function in the edk2
46 // PCI Bus UEFI_DRIVER.
47 //
48 #pragma pack (1)
49 typedef struct {
50 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR AddressSpaceDesc;
51 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc;
52 } MMIO64_PREFERENCE;
53 #pragma pack ()
54
55 STATIC CONST MMIO64_PREFERENCE mConfiguration = {
56 //
57 // AddressSpaceDesc
58 //
59 {
60 ACPI_ADDRESS_SPACE_DESCRIPTOR, // Desc
61 (UINT16)( // Len
62 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -
63 OFFSET_OF (
64 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,
65 ResType
66 )
67 ),
68 ACPI_ADDRESS_SPACE_TYPE_MEM, // ResType
69 PCI_ACPI_UNUSED, // GenFlag
70 PCI_ACPI_UNUSED, // SpecificFlag
71 64, // AddrSpaceGranularity:
72 // aperture selection hint
73 // for BAR allocation
74 PCI_ACPI_UNUSED, // AddrRangeMin
75 PCI_BAR_OLD_ALIGN, // AddrRangeMax:
76 // no special alignment
77 // for affected BARs
78 PCI_BAR_ALL, // AddrTranslationOffset:
79 // hint covers all
80 // eligible BARs
81 PCI_BAR_NOCHANGE // AddrLen:
82 // use probed BAR size
83 },
84 //
85 // EndDesc
86 //
87 {
88 ACPI_END_TAG_DESCRIPTOR, // Desc
89 0 // Checksum: to be ignored
90 }
91 };
92
93 //
94 // The CheckDevice() member function has been called.
95 //
96 STATIC BOOLEAN mCheckDeviceCalled;
97
98
99 /**
100 Notification callback for Legacy BIOS protocol installation.
101
102 @param[in] Event Event whose notification function is being invoked.
103
104 @param[in] Context The pointer to the notification function's context, which
105 is implementation-dependent.
106 **/
107 STATIC
108 VOID
109 EFIAPI
110 LegacyBiosInstalled (
111 IN EFI_EVENT Event,
112 IN VOID *Context
113 )
114 {
115 EFI_STATUS Status;
116 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
117
118 ASSERT (!mCheckDeviceCalled);
119
120 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid,
121 NULL /* Registration */, (VOID **)&LegacyBios);
122 if (EFI_ERROR (Status)) {
123 return;
124 }
125
126 mLegacyBiosInstalled = TRUE;
127
128 //
129 // Close the event and deregister this callback.
130 //
131 Status = gBS->CloseEvent (Event);
132 ASSERT_EFI_ERROR (Status);
133 }
134
135
136 /**
137 Returns a list of ACPI resource descriptors that detail the special resource
138 configuration requirements for an incompatible PCI device.
139
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.
148
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.
156
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)
161
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.
166
167 @param[in] This Pointer to the
168 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
169 instance.
170
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.
174
175 @param[in] DeviceId A unique ID to identify the particular PCI
176 device. See the Conventional PCI
177 Specification 3.0 for details.
178
179 @param[in] RevisionId A PCI device-specific revision identifier.
180 See the Conventional PCI Specification 3.0
181 for details.
182
183 @param[in] SubsystemVendorId Specifies the subsystem vendor ID. See the
184 Conventional PCI Specification 3.0 for
185 details.
186
187 @param[in] SubsystemDeviceId Specifies the subsystem device ID. See the
188 Conventional PCI Specification 3.0 for
189 details.
190
191 @param[out] Configuration A list of ACPI resource descriptors that
192 detail the configuration requirement.
193
194 @retval EFI_SUCCESS The function always returns EFI_SUCCESS.
195 **/
196 STATIC
197 EFI_STATUS
198 EFIAPI
199 CheckDevice (
200 IN EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL *This,
201 IN UINTN VendorId,
202 IN UINTN DeviceId,
203 IN UINTN RevisionId,
204 IN UINTN SubsystemVendorId,
205 IN UINTN SubsystemDeviceId,
206 OUT VOID **Configuration
207 )
208 {
209 mCheckDeviceCalled = TRUE;
210
211 //
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.
218 //
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.
224 //
225 if (mLegacyBiosInstalled) {
226 //
227 // Don't interfere with resource degradation.
228 //
229 *Configuration = NULL;
230 return EFI_SUCCESS;
231 }
232
233 //
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.
238 //
239 *Configuration = AllocateCopyPool (sizeof mConfiguration, &mConfiguration);
240 if (*Configuration == NULL) {
241 DEBUG ((EFI_D_WARN,
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;
245 }
246 return EFI_SUCCESS;
247 }
248
249
250 /**
251 Entry point for this driver.
252
253 @param[in] ImageHandle Image handle of this driver.
254 @param[in] SystemTable Pointer to SystemTable.
255
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.
260
261 **/
262 EFI_STATUS
263 EFIAPI
264 DriverInitialize (
265 IN EFI_HANDLE ImageHandle,
266 IN EFI_SYSTEM_TABLE *SystemTable
267 )
268 {
269 EFI_STATUS Status;
270 EFI_EVENT Event;
271 VOID *Registration;
272
273 //
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.
276 //
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.
279 //
280 if (PcdGetBool (PcdPciDisableBusEnumeration) ||
281 PcdGet64 (PcdPciMmio64Size) == 0) {
282 return EFI_UNSUPPORTED;
283 }
284
285 //
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.)
289 //
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.
293 //
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:
298 //
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.
304 //
305 // 2. The DXE_CORE enters BDS.
306 //
307 // 3. The platform BDS connects the PCI Root Bridge IO instances (produced by
308 // the PCI Host Bridge DXE_DRIVER).
309 //
310 // 4. The PCI Bus UEFI_DRIVER enumerates resources and calls into this
311 // DXE_DRIVER (CheckDevice()).
312 //
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).
315 //
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.
318 //
319 Status = gBS->CreateEvent (EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
320 LegacyBiosInstalled, NULL /* Context */, &Event);
321 if (EFI_ERROR (Status)) {
322 return Status;
323 }
324
325 Status = gBS->RegisterProtocolNotify (&gEfiLegacyBiosProtocolGuid, Event,
326 &Registration);
327 if (EFI_ERROR (Status)) {
328 goto CloseEvent;
329 }
330
331 Status = gBS->SignalEvent (Event);
332 ASSERT_EFI_ERROR (Status);
333
334 mIncompatiblePciDeviceSupport.CheckDevice = CheckDevice;
335 Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,
336 &gEfiIncompatiblePciDeviceSupportProtocolGuid,
337 &mIncompatiblePciDeviceSupport, NULL);
338 if (EFI_ERROR (Status)) {
339 goto CloseEvent;
340 }
341
342 return EFI_SUCCESS;
343
344 CloseEvent:
345 if (!mLegacyBiosInstalled) {
346 EFI_STATUS CloseStatus;
347
348 CloseStatus = gBS->CloseEvent (Event);
349 ASSERT_EFI_ERROR (CloseStatus);
350 }
351
352 return Status;
353 }