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