]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 #include <Library/CcProbeLib.h>
22
23 #include <Protocol/IncompatiblePciDeviceSupport.h>
24 #include <Protocol/LegacyBios.h>
25
26 //
27 // The Legacy BIOS protocol has been located.
28 //
29 STATIC BOOLEAN mLegacyBiosInstalled;
30
31 //
32 // The protocol interface this driver produces.
33 //
34 STATIC EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
35 mIncompatiblePciDeviceSupport;
36
37 //
38 // Configuration template for the CheckDevice() protocol member function.
39 //
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.
42 //
43 // This structure is interpreted by the UpdatePciInfo() function in the edk2
44 // PCI Bus UEFI_DRIVER.
45 //
46 // This structure looks like:
47 // AddressDesc-1 + AddressDesc-2 + ... + AddressDesc-n + EndDesc
48 //
49 STATIC CONST EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR mMmio64Configuration = {
50 ACPI_ADDRESS_SPACE_DESCRIPTOR, // Desc
51 (UINT16)( // Len
52 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -
53 OFFSET_OF (
54 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,
55 ResType
56 )
57 ),
58 ACPI_ADDRESS_SPACE_TYPE_MEM, // ResType
59 0, // GenFlag
60 0, // SpecificFlag
61 64, // AddrSpaceGranularity:
62 // aperture selection hint
63 // for BAR allocation
64 0, // AddrRangeMin
65 0, // AddrRangeMax:
66 // no special alignment
67 // for affected BARs
68 MAX_UINT64, // AddrTranslationOffset:
69 // hint covers all
70 // eligible BARs
71 0 // AddrLen:
72 // use probed BAR size
73 };
74
75 //
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.
82 //
83 STATIC CONST EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR mOptionRomConfiguration = {
84 ACPI_ADDRESS_SPACE_DESCRIPTOR, // Desc
85 (UINT16)( // Len
86 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -
87 OFFSET_OF (
88 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,
89 ResType
90 )
91 ),
92 ACPI_ADDRESS_SPACE_TYPE_MEM, // ResType
93 0, // GenFlag
94 0, // Disable option roms SpecificFlag
95 64, // AddrSpaceGranularity:
96 // aperture selection hint
97 // for BAR allocation
98 MAX_UINT64, // AddrRangeMin
99 MAX_UINT64, // AddrRangeMax:
100 // no special alignment
101 // for affected BARs
102 6, // AddrTranslationOffset:
103 // hint covers all
104 // eligible BARs
105 0 // AddrLen:
106 // use probed BAR size
107 };
108
109 STATIC CONST EFI_ACPI_END_TAG_DESCRIPTOR mEndDesc = {
110 ACPI_END_TAG_DESCRIPTOR, // Desc
111 0 // Checksum: to be ignored
112 };
113
114 //
115 // The CheckDevice() member function has been called.
116 //
117 STATIC BOOLEAN mCheckDeviceCalled;
118
119 /**
120 Notification callback for Legacy BIOS protocol installation.
121
122 @param[in] Event Event whose notification function is being invoked.
123
124 @param[in] Context The pointer to the notification function's context, which
125 is implementation-dependent.
126 **/
127 STATIC
128 VOID
129 EFIAPI
130 LegacyBiosInstalled (
131 IN EFI_EVENT Event,
132 IN VOID *Context
133 )
134 {
135 EFI_STATUS Status;
136 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
137
138 ASSERT (!mCheckDeviceCalled);
139
140 Status = gBS->LocateProtocol (
141 &gEfiLegacyBiosProtocolGuid,
142 NULL /* Registration */,
143 (VOID **)&LegacyBios
144 );
145 if (EFI_ERROR (Status)) {
146 return;
147 }
148
149 mLegacyBiosInstalled = TRUE;
150
151 //
152 // Close the event and deregister this callback.
153 //
154 Status = gBS->CloseEvent (Event);
155 ASSERT_EFI_ERROR (Status);
156 }
157
158 /**
159 Returns a list of ACPI resource descriptors that detail the special resource
160 configuration requirements for an incompatible PCI device.
161
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.
170
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.
178
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)
183
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.
188
189 @param[in] This Pointer to the
190 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
191 instance.
192
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.
196
197 @param[in] DeviceId A unique ID to identify the particular PCI
198 device. See the Conventional PCI
199 Specification 3.0 for details.
200
201 @param[in] RevisionId A PCI device-specific revision identifier.
202 See the Conventional PCI Specification 3.0
203 for details.
204
205 @param[in] SubsystemVendorId Specifies the subsystem vendor ID. See the
206 Conventional PCI Specification 3.0 for
207 details.
208
209 @param[in] SubsystemDeviceId Specifies the subsystem device ID. See the
210 Conventional PCI Specification 3.0 for
211 details.
212
213 @param[out] Configuration A list of ACPI resource descriptors that
214 detail the configuration requirement.
215
216 @retval EFI_SUCCESS The function always returns EFI_SUCCESS.
217 **/
218 STATIC
219 EFI_STATUS
220 EFIAPI
221 CheckDevice (
222 IN EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL *This,
223 IN UINTN VendorId,
224 IN UINTN DeviceId,
225 IN UINTN RevisionId,
226 IN UINTN SubsystemVendorId,
227 IN UINTN SubsystemDeviceId,
228 OUT VOID **Configuration
229 )
230 {
231 mCheckDeviceCalled = TRUE;
232 UINTN Length;
233 UINT8 *Ptr;
234
235 //
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.
242 //
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.
248 //
249 if (mLegacyBiosInstalled) {
250 //
251 // Don't interfere with resource degradation.
252 //
253 *Configuration = NULL;
254 return EFI_SUCCESS;
255 }
256
257 //
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.
262 //
263 Length = sizeof mMmio64Configuration + sizeof mEndDesc;
264
265 //
266 // In Td guest OptionRom is not allowed.
267 //
268 if (CcProbe ()) {
269 Length += sizeof mOptionRomConfiguration;
270 }
271
272 *Configuration = AllocateZeroPool (Length);
273
274 if (*Configuration == NULL) {
275 DEBUG ((
276 DEBUG_WARN,
277 "%a: 64-bit MMIO BARs may be degraded for PCI 0x%04x:0x%04x (rev %d)\n",
278 __FUNCTION__,
279 (UINT32)VendorId,
280 (UINT32)DeviceId,
281 (UINT8)RevisionId
282 ));
283 return EFI_OUT_OF_RESOURCES;
284 }
285
286 Ptr = (UINT8 *)(UINTN)*Configuration;
287 CopyMem (Ptr, &mMmio64Configuration, sizeof mMmio64Configuration);
288 Length = sizeof mMmio64Configuration;
289
290 if (CcProbe ()) {
291 CopyMem (Ptr + Length, &mOptionRomConfiguration, sizeof mOptionRomConfiguration);
292 Length += sizeof mOptionRomConfiguration;
293 }
294
295 CopyMem (Ptr + Length, &mEndDesc, sizeof mEndDesc);
296
297 return EFI_SUCCESS;
298 }
299
300 /**
301 Entry point for this driver.
302
303 @param[in] ImageHandle Image handle of this driver.
304 @param[in] SystemTable Pointer to SystemTable.
305
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.
310
311 **/
312 EFI_STATUS
313 EFIAPI
314 DriverInitialize (
315 IN EFI_HANDLE ImageHandle,
316 IN EFI_SYSTEM_TABLE *SystemTable
317 )
318 {
319 EFI_STATUS Status;
320 EFI_EVENT Event;
321 VOID *Registration;
322
323 //
324 // If there is no 64-bit PCI MMIO aperture, then 64-bit MMIO BARs have to be
325 // allocated under 4 GB unconditionally.
326 //
327 if (PcdGet64 (PcdPciMmio64Size) == 0) {
328 return EFI_UNSUPPORTED;
329 }
330
331 //
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.)
335 //
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.
339 //
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:
344 //
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.
350 //
351 // 2. The DXE_CORE enters BDS.
352 //
353 // 3. The platform BDS connects the PCI Root Bridge IO instances (produced by
354 // the PCI Host Bridge DXE_DRIVER).
355 //
356 // 4. The PCI Bus UEFI_DRIVER enumerates resources and calls into this
357 // DXE_DRIVER (CheckDevice()).
358 //
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).
361 //
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.
364 //
365 Status = gBS->CreateEvent (
366 EVT_NOTIFY_SIGNAL,
367 TPL_CALLBACK,
368 LegacyBiosInstalled,
369 NULL /* Context */,
370 &Event
371 );
372 if (EFI_ERROR (Status)) {
373 return Status;
374 }
375
376 Status = gBS->RegisterProtocolNotify (
377 &gEfiLegacyBiosProtocolGuid,
378 Event,
379 &Registration
380 );
381 if (EFI_ERROR (Status)) {
382 goto CloseEvent;
383 }
384
385 Status = gBS->SignalEvent (Event);
386 ASSERT_EFI_ERROR (Status);
387
388 mIncompatiblePciDeviceSupport.CheckDevice = CheckDevice;
389 Status = gBS->InstallMultipleProtocolInterfaces (
390 &ImageHandle,
391 &gEfiIncompatiblePciDeviceSupportProtocolGuid,
392 &mIncompatiblePciDeviceSupport,
393 NULL
394 );
395 if (EFI_ERROR (Status)) {
396 goto CloseEvent;
397 }
398
399 return EFI_SUCCESS;
400
401 CloseEvent:
402 if (!mLegacyBiosInstalled) {
403 EFI_STATUS CloseStatus;
404
405 CloseStatus = gBS->CloseEvent (Event);
406 ASSERT_EFI_ERROR (CloseStatus);
407 }
408
409 return Status;
410 }