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