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