]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.c
OvmfPkg/IncompatiblePciDeviceSupportDxe: Ignore OptionRom in Td guest
[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 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
8
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10 **/
11
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <IndustryStandard/Acpi10.h>
15 #include <IndustryStandard/Pci22.h>
16
17 #include <Library/DebugLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21
22 #include <Protocol/IncompatiblePciDeviceSupport.h>
23 #include <Protocol/LegacyBios.h>
24
25 //
26 // The Legacy BIOS protocol has been located.
27 //
28 STATIC BOOLEAN mLegacyBiosInstalled;
29
30 //
31 // The protocol interface this driver produces.
32 //
33 STATIC EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
34 mIncompatiblePciDeviceSupport;
35
36 //
37 // Configuration template for the CheckDevice() protocol member function.
38 //
39 // Refer to Table 20 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
40 // the Platform Init 1.4a Spec, Volume 5.
41 //
42 // This structure is interpreted by the UpdatePciInfo() function in the edk2
43 // PCI Bus UEFI_DRIVER.
44 //
45 // This structure looks like:
46 // AddressDesc-1 + AddressDesc-2 + ... + AddressDesc-n + EndDesc
47 //
48 STATIC CONST EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR mMmio64Configuration = {
49 ACPI_ADDRESS_SPACE_DESCRIPTOR, // Desc
50 (UINT16)( // Len
51 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -
52 OFFSET_OF (
53 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,
54 ResType
55 )
56 ),
57 ACPI_ADDRESS_SPACE_TYPE_MEM, // ResType
58 0, // GenFlag
59 0, // SpecificFlag
60 64, // AddrSpaceGranularity:
61 // aperture selection hint
62 // for BAR allocation
63 0, // AddrRangeMin
64 0, // AddrRangeMax:
65 // no special alignment
66 // for affected BARs
67 MAX_UINT64, // AddrTranslationOffset:
68 // hint covers all
69 // eligible BARs
70 0 // AddrLen:
71 // use probed BAR size
72 };
73
74 //
75 // mOptionRomConfiguration is present only in Td guest.
76 // Host VMM can inject option ROM which is untrusted in Td guest,
77 // so PCI option ROM needs to be ignored.
78 // According to "Table 20. ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage"
79 // PI spec 1.7, type-specific flags can be set to 0 when
80 // Address Translation Offset == 6 to skip device option ROM.
81 //
82 STATIC CONST EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR mOptionRomConfiguration = {
83 ACPI_ADDRESS_SPACE_DESCRIPTOR, // Desc
84 (UINT16)( // Len
85 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -
86 OFFSET_OF (
87 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,
88 ResType
89 )
90 ),
91 ACPI_ADDRESS_SPACE_TYPE_MEM, // ResType
92 0, // GenFlag
93 0, // Disable option roms SpecificFlag
94 64, // AddrSpaceGranularity:
95 // aperture selection hint
96 // for BAR allocation
97 MAX_UINT64, // AddrRangeMin
98 MAX_UINT64, // AddrRangeMax:
99 // no special alignment
100 // for affected BARs
101 6, // AddrTranslationOffset:
102 // hint covers all
103 // eligible BARs
104 0 // AddrLen:
105 // use probed BAR size
106 };
107
108 STATIC CONST EFI_ACPI_END_TAG_DESCRIPTOR mEndDesc = {
109 ACPI_END_TAG_DESCRIPTOR, // Desc
110 0 // Checksum: to be ignored
111 };
112
113 //
114 // The CheckDevice() member function has been called.
115 //
116 STATIC BOOLEAN mCheckDeviceCalled;
117
118 /**
119 Notification callback for Legacy BIOS protocol installation.
120
121 @param[in] Event Event whose notification function is being invoked.
122
123 @param[in] Context The pointer to the notification function's context, which
124 is implementation-dependent.
125 **/
126 STATIC
127 VOID
128 EFIAPI
129 LegacyBiosInstalled (
130 IN EFI_EVENT Event,
131 IN VOID *Context
132 )
133 {
134 EFI_STATUS Status;
135 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
136
137 ASSERT (!mCheckDeviceCalled);
138
139 Status = gBS->LocateProtocol (
140 &gEfiLegacyBiosProtocolGuid,
141 NULL /* Registration */,
142 (VOID **)&LegacyBios
143 );
144 if (EFI_ERROR (Status)) {
145 return;
146 }
147
148 mLegacyBiosInstalled = TRUE;
149
150 //
151 // Close the event and deregister this callback.
152 //
153 Status = gBS->CloseEvent (Event);
154 ASSERT_EFI_ERROR (Status);
155 }
156
157 /**
158 Returns a list of ACPI resource descriptors that detail the special resource
159 configuration requirements for an incompatible PCI device.
160
161 Prior to bus enumeration, the PCI bus driver will look for the presence of
162 the EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL. Only one instance of this
163 protocol can be present in the system. For each PCI device that the PCI bus
164 driver discovers, the PCI bus driver calls this function with the device's
165 vendor ID, device ID, revision ID, subsystem vendor ID, and subsystem device
166 ID. If the VendorId, DeviceId, RevisionId, SubsystemVendorId, or
167 SubsystemDeviceId value is set to (UINTN)-1, that field will be ignored. The
168 ID values that are not (UINTN)-1 will be used to identify the current device.
169
170 This function will only return EFI_SUCCESS. However, if the device is an
171 incompatible PCI device, a list of ACPI resource descriptors will be returned
172 in Configuration. Otherwise, NULL will be returned in Configuration instead.
173 The PCI bus driver does not need to allocate memory for Configuration.
174 However, it is the PCI bus driver's responsibility to free it. The PCI bus
175 driver then can configure this device with the information that is derived
176 from this list of resource nodes, rather than the result of BAR probing.
177
178 Only the following two resource descriptor types from the ACPI Specification
179 may be used to describe the incompatible PCI device resource requirements:
180 - QWORD Address Space Descriptor (ACPI 2.0, section 6.4.3.5.1; also ACPI 3.0)
181 - End Tag (ACPI 2.0, section 6.4.2.8; also ACPI 3.0)
182
183 The QWORD Address Space Descriptor can describe memory, I/O, and bus number
184 ranges for dynamic or fixed resources. The configuration of a PCI root bridge
185 is described with one or more QWORD Address Space Descriptors, followed by an
186 End Tag. See the ACPI Specification for details on the field values.
187
188 @param[in] This Pointer to the
189 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
190 instance.
191
192 @param[in] VendorId A unique ID to identify the manufacturer of
193 the PCI device. See the Conventional PCI
194 Specification 3.0 for details.
195
196 @param[in] DeviceId A unique ID to identify the particular PCI
197 device. See the Conventional PCI
198 Specification 3.0 for details.
199
200 @param[in] RevisionId A PCI device-specific revision identifier.
201 See the Conventional PCI Specification 3.0
202 for details.
203
204 @param[in] SubsystemVendorId Specifies the subsystem vendor ID. See the
205 Conventional PCI Specification 3.0 for
206 details.
207
208 @param[in] SubsystemDeviceId Specifies the subsystem device ID. See the
209 Conventional PCI Specification 3.0 for
210 details.
211
212 @param[out] Configuration A list of ACPI resource descriptors that
213 detail the configuration requirement.
214
215 @retval EFI_SUCCESS The function always returns EFI_SUCCESS.
216 **/
217 STATIC
218 EFI_STATUS
219 EFIAPI
220 CheckDevice (
221 IN EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL *This,
222 IN UINTN VendorId,
223 IN UINTN DeviceId,
224 IN UINTN RevisionId,
225 IN UINTN SubsystemVendorId,
226 IN UINTN SubsystemDeviceId,
227 OUT VOID **Configuration
228 )
229 {
230 mCheckDeviceCalled = TRUE;
231 UINTN Length;
232 UINT8 *Ptr;
233
234 //
235 // Unlike the general description of this protocol member suggests, there is
236 // nothing incompatible about the PCI devices that we'll match here. We'll
237 // match all PCI devices, and generate exactly one QWORD Address Space
238 // Descriptor for each. That descriptor will instruct the PCI Bus UEFI_DRIVER
239 // not to degrade 64-bit MMIO BARs for the device, even if a PCI option ROM
240 // BAR is present on the device.
241 //
242 // The concern captured in the PCI Bus UEFI_DRIVER is that a legacy BIOS boot
243 // (via a CSM) could dispatch a legacy option ROM on the device, which might
244 // have trouble with MMIO BARs that have been allocated outside of the 32-bit
245 // address space. But, if we don't support legacy option ROMs at all, then
246 // this problem cannot arise.
247 //
248 if (mLegacyBiosInstalled) {
249 //
250 // Don't interfere with resource degradation.
251 //
252 *Configuration = NULL;
253 return EFI_SUCCESS;
254 }
255
256 //
257 // This member function is mis-specified actually: it is supposed to allocate
258 // memory, but as specified, it could not return an error status. Thankfully,
259 // the edk2 PCI Bus UEFI_DRIVER actually handles error codes; see the
260 // UpdatePciInfo() function.
261 //
262 Length = sizeof mMmio64Configuration + sizeof mEndDesc;
263
264 //
265 // In Td guest OptionRom is not allowed.
266 //
267 if (TdIsEnabled ()) {
268 Length += sizeof mOptionRomConfiguration;
269 }
270
271 *Configuration = AllocateZeroPool (Length);
272
273 if (*Configuration == NULL) {
274 DEBUG ((
275 DEBUG_WARN,
276 "%a: 64-bit MMIO BARs may be degraded for PCI 0x%04x:0x%04x (rev %d)\n",
277 __FUNCTION__,
278 (UINT32)VendorId,
279 (UINT32)DeviceId,
280 (UINT8)RevisionId
281 ));
282 return EFI_OUT_OF_RESOURCES;
283 }
284
285 Ptr = (UINT8 *)(UINTN)*Configuration;
286 CopyMem (Ptr, &mMmio64Configuration, sizeof mMmio64Configuration);
287 Length = sizeof mMmio64Configuration;
288
289 if (TdIsEnabled ()) {
290 CopyMem (Ptr + Length, &mOptionRomConfiguration, sizeof mOptionRomConfiguration);
291 Length += sizeof mOptionRomConfiguration;
292 }
293
294 CopyMem (Ptr + Length, &mEndDesc, sizeof mEndDesc);
295
296 return EFI_SUCCESS;
297 }
298
299 /**
300 Entry point for this driver.
301
302 @param[in] ImageHandle Image handle of this driver.
303 @param[in] SystemTable Pointer to SystemTable.
304
305 @retval EFI_SUCESS Driver has loaded successfully.
306 @retval EFI_UNSUPPORTED PCI resource allocation has been disabled.
307 @retval EFI_UNSUPPORTED There is no 64-bit PCI MMIO aperture.
308 @return Error codes from lower level functions.
309
310 **/
311 EFI_STATUS
312 EFIAPI
313 DriverInitialize (
314 IN EFI_HANDLE ImageHandle,
315 IN EFI_SYSTEM_TABLE *SystemTable
316 )
317 {
318 EFI_STATUS Status;
319 EFI_EVENT Event;
320 VOID *Registration;
321
322 //
323 // If there is no 64-bit PCI MMIO aperture, then 64-bit MMIO BARs have to be
324 // allocated under 4 GB unconditionally.
325 //
326 if (PcdGet64 (PcdPciMmio64Size) == 0) {
327 return EFI_UNSUPPORTED;
328 }
329
330 //
331 // Otherwise, create a protocol notify to see if a CSM is present. (With the
332 // CSM absent, the PCI Bus driver won't have to worry about allocating 64-bit
333 // MMIO BARs in the 32-bit MMIO aperture, for the sake of a legacy BIOS.)
334 //
335 // If the Legacy BIOS Protocol is present at the time of this driver starting
336 // up, we can mark immediately that the PCI Bus driver should perform the
337 // usual 64-bit MMIO BAR degradation.
338 //
339 // Otherwise, if the Legacy BIOS Protocol is absent at startup, it may be
340 // installed later. However, if it doesn't show up until the first
341 // EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL.CheckDevice() call from the
342 // PCI Bus driver, then it never will:
343 //
344 // 1. The following drivers are dispatched in some unspecified order:
345 // - PCI Host Bridge DXE_DRIVER,
346 // - PCI Bus UEFI_DRIVER,
347 // - this DXE_DRIVER,
348 // - Legacy BIOS DXE_DRIVER.
349 //
350 // 2. The DXE_CORE enters BDS.
351 //
352 // 3. The platform BDS connects the PCI Root Bridge IO instances (produced by
353 // the PCI Host Bridge DXE_DRIVER).
354 //
355 // 4. The PCI Bus UEFI_DRIVER enumerates resources and calls into this
356 // DXE_DRIVER (CheckDevice()).
357 //
358 // 5. This driver remembers if EFI_LEGACY_BIOS_PROTOCOL has been installed
359 // sometime during step 1 (produced by the Legacy BIOS DXE_DRIVER).
360 //
361 // For breaking this order, the Legacy BIOS DXE_DRIVER would have to install
362 // its protocol after the firmware enters BDS, which cannot happen.
363 //
364 Status = gBS->CreateEvent (
365 EVT_NOTIFY_SIGNAL,
366 TPL_CALLBACK,
367 LegacyBiosInstalled,
368 NULL /* Context */,
369 &Event
370 );
371 if (EFI_ERROR (Status)) {
372 return Status;
373 }
374
375 Status = gBS->RegisterProtocolNotify (
376 &gEfiLegacyBiosProtocolGuid,
377 Event,
378 &Registration
379 );
380 if (EFI_ERROR (Status)) {
381 goto CloseEvent;
382 }
383
384 Status = gBS->SignalEvent (Event);
385 ASSERT_EFI_ERROR (Status);
386
387 mIncompatiblePciDeviceSupport.CheckDevice = CheckDevice;
388 Status = gBS->InstallMultipleProtocolInterfaces (
389 &ImageHandle,
390 &gEfiIncompatiblePciDeviceSupportProtocolGuid,
391 &mIncompatiblePciDeviceSupport,
392 NULL
393 );
394 if (EFI_ERROR (Status)) {
395 goto CloseEvent;
396 }
397
398 return EFI_SUCCESS;
399
400 CloseEvent:
401 if (!mLegacyBiosInstalled) {
402 EFI_STATUS CloseStatus;
403
404 CloseStatus = gBS->CloseEvent (Event);
405 ASSERT_EFI_ERROR (CloseStatus);
406 }
407
408 return Status;
409 }