2 Driver Binding functions for PCI Bus module.
4 Single PCI bus driver instance will manager all PCI Root Bridges in one EFI based firmware,
5 since all PCI Root Bridges' resources need to be managed together.
6 Supported() function will try to get PCI Root Bridge IO Protocol.
7 Start() function will get PCI Host Bridge Resource Allocation Protocol to manage all
8 PCI Root Bridges. So it means platform needs install PCI Root Bridge IO protocol for each
9 PCI Root Bus and install PCI Host Bridge Resource Allocation Protocol.
11 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
12 SPDX-License-Identifier: BSD-2-Clause-Patent
19 // PCI Bus Driver Global Variables
21 EFI_DRIVER_BINDING_PROTOCOL gPciBusDriverBinding
= {
22 PciBusDriverBindingSupported
,
23 PciBusDriverBindingStart
,
24 PciBusDriverBindingStop
,
30 EFI_HANDLE gPciHostBrigeHandles
[PCI_MAX_HOST_BRIDGE_NUM
];
31 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
*gIncompatiblePciDeviceSupport
= NULL
;
32 UINTN gPciHostBridgeNumber
= 0;
33 BOOLEAN gFullEnumeration
= TRUE
;
34 UINT64 gAllOne
= 0xFFFFFFFFFFFFFFFFULL
;
37 EFI_PCI_PLATFORM_PROTOCOL
*gPciPlatformProtocol
;
38 EFI_PCI_OVERRIDE_PROTOCOL
*gPciOverrideProtocol
;
39 EDKII_IOMMU_PROTOCOL
*mIoMmuProtocol
;
42 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PCI_HOTPLUG_REQUEST_PROTOCOL mPciHotPlugRequest
= {
43 PciHotPlugRequestNotify
47 The Entry Point for PCI Bus module. The user code starts with this function.
49 Installs driver module protocols and. Creates virtual device handles for ConIn,
50 ConOut, and StdErr. Installs Simple Text In protocol, Simple Text In Ex protocol,
51 Simple Pointer protocol, Absolute Pointer protocol on those virtual handlers.
52 Installs Graphics Output protocol and/or UGA Draw protocol if needed.
54 @param[in] ImageHandle The firmware allocated handle for the EFI image.
55 @param[in] SystemTable A pointer to the EFI System Table.
57 @retval EFI_SUCCESS The entry point is executed successfully.
58 @retval other Some error occurred when executing this entry point.
64 IN EFI_HANDLE ImageHandle
,
65 IN EFI_SYSTEM_TABLE
*SystemTable
72 // Initializes PCI devices pool
74 InitializePciDevicePool ();
77 // Install driver model protocol(s).
79 Status
= EfiLibInstallDriverBindingComponentName2 (
82 &gPciBusDriverBinding
,
84 &gPciBusComponentName
,
85 &gPciBusComponentName2
87 ASSERT_EFI_ERROR (Status
);
89 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
91 // If Hot Plug is supported, install EFI PCI Hot Plug Request protocol.
94 Status
= gBS
->InstallProtocolInterface (
96 &gEfiPciHotPlugRequestProtocolGuid
,
106 Test to see if this driver supports ControllerHandle. Any ControllerHandle
107 than contains a gEfiPciRootBridgeIoProtocolGuid protocol can be supported.
109 @param This Protocol instance pointer.
110 @param Controller Handle of device to test.
111 @param RemainingDevicePath Optional parameter use to pick a specific child
114 @retval EFI_SUCCESS This driver supports this device.
115 @retval EFI_ALREADY_STARTED This driver is already running on this device.
116 @retval other This driver does not support this device.
121 PciBusDriverBindingSupported (
122 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
123 IN EFI_HANDLE Controller
,
124 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
128 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
129 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
130 EFI_DEV_PATH_PTR Node
;
133 // Check RemainingDevicePath validation
135 if (RemainingDevicePath
!= NULL
) {
137 // Check if RemainingDevicePath is the End of Device Path Node,
138 // if yes, go on checking other conditions
140 if (!IsDevicePathEnd (RemainingDevicePath
)) {
142 // If RemainingDevicePath isn't the End of Device Path Node,
143 // check its validation
145 Node
.DevPath
= RemainingDevicePath
;
146 if (Node
.DevPath
->Type
!= HARDWARE_DEVICE_PATH
||
147 Node
.DevPath
->SubType
!= HW_PCI_DP
||
148 DevicePathNodeLength(Node
.DevPath
) != sizeof(PCI_DEVICE_PATH
)) {
149 return EFI_UNSUPPORTED
;
155 // Check if Pci Root Bridge IO protocol is installed by platform
157 Status
= gBS
->OpenProtocol (
159 &gEfiPciRootBridgeIoProtocolGuid
,
160 (VOID
**) &PciRootBridgeIo
,
161 This
->DriverBindingHandle
,
163 EFI_OPEN_PROTOCOL_BY_DRIVER
165 if (Status
== EFI_ALREADY_STARTED
) {
169 if (EFI_ERROR (Status
)) {
174 // Close the I/O Abstraction(s) used to perform the supported test
178 &gEfiPciRootBridgeIoProtocolGuid
,
179 This
->DriverBindingHandle
,
184 // Open the EFI Device Path protocol needed to perform the supported test
186 Status
= gBS
->OpenProtocol (
188 &gEfiDevicePathProtocolGuid
,
189 (VOID
**) &ParentDevicePath
,
190 This
->DriverBindingHandle
,
192 EFI_OPEN_PROTOCOL_BY_DRIVER
194 if (Status
== EFI_ALREADY_STARTED
) {
198 if (EFI_ERROR (Status
)) {
203 // Close protocol, don't use device path protocol in the Support() function
207 &gEfiDevicePathProtocolGuid
,
208 This
->DriverBindingHandle
,
216 Start this driver on ControllerHandle and enumerate Pci bus and start
217 all device under PCI bus.
219 @param This Protocol instance pointer.
220 @param Controller Handle of device to bind driver to.
221 @param RemainingDevicePath Optional parameter use to pick a specific child
224 @retval EFI_SUCCESS This driver is added to ControllerHandle.
225 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
226 @retval other This driver does not support this device.
231 PciBusDriverBindingStart (
232 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
233 IN EFI_HANDLE Controller
,
234 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
238 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
239 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
242 // Initialize PciRootBridgeIo to suppress incorrect compiler warning.
244 PciRootBridgeIo
= NULL
;
247 // Check RemainingDevicePath validation
249 if (RemainingDevicePath
!= NULL
) {
251 // Check if RemainingDevicePath is the End of Device Path Node,
252 // if yes, return EFI_SUCCESS
254 if (IsDevicePathEnd (RemainingDevicePath
)) {
259 gBS
->LocateProtocol (
260 &gEfiIncompatiblePciDeviceSupportProtocolGuid
,
262 (VOID
**) &gIncompatiblePciDeviceSupport
266 // If PCI Platform protocol is available, get it now.
267 // If the platform implements this, it must be installed before BDS phase
269 gPciPlatformProtocol
= NULL
;
270 gBS
->LocateProtocol (
271 &gEfiPciPlatformProtocolGuid
,
273 (VOID
**) &gPciPlatformProtocol
277 // If PCI Platform protocol doesn't exist, try to Pci Override Protocol.
279 if (gPciPlatformProtocol
== NULL
) {
280 gPciOverrideProtocol
= NULL
;
281 gBS
->LocateProtocol (
282 &gEfiPciOverrideProtocolGuid
,
284 (VOID
**) &gPciOverrideProtocol
288 if (mIoMmuProtocol
== NULL
) {
289 gBS
->LocateProtocol (
290 &gEdkiiIoMmuProtocolGuid
,
292 (VOID
**) &mIoMmuProtocol
296 if (PcdGetBool (PcdPciDisableBusEnumeration
)) {
297 gFullEnumeration
= FALSE
;
299 gFullEnumeration
= (BOOLEAN
) ((SearchHostBridgeHandle (Controller
) ? FALSE
: TRUE
));
303 // Open Device Path Protocol for PCI root bridge
305 Status
= gBS
->OpenProtocol (
307 &gEfiDevicePathProtocolGuid
,
308 (VOID
**) &ParentDevicePath
,
309 This
->DriverBindingHandle
,
311 EFI_OPEN_PROTOCOL_GET_PROTOCOL
313 ASSERT_EFI_ERROR (Status
);
316 // Report Status Code to indicate PCI bus starts
318 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
320 (EFI_IO_BUS_PCI
| EFI_IOB_PC_INIT
),
324 Status
= EFI_SUCCESS
;
326 // Enumerate the entire host bridge
327 // After enumeration, a database that records all the device information will be created
330 if (gFullEnumeration
) {
332 // Get the rootbridge Io protocol to find the host bridge handle
334 Status
= gBS
->OpenProtocol (
336 &gEfiPciRootBridgeIoProtocolGuid
,
337 (VOID
**) &PciRootBridgeIo
,
338 gPciBusDriverBinding
.DriverBindingHandle
,
340 EFI_OPEN_PROTOCOL_GET_PROTOCOL
343 if (!EFI_ERROR (Status
)) {
344 Status
= PciEnumerator (Controller
, PciRootBridgeIo
->ParentHandle
);
348 // If PCI bus has already done the full enumeration, never do it again
350 Status
= PciEnumeratorLight (Controller
);
353 if (EFI_ERROR (Status
)) {
358 // Start all the devices under the entire host bridge.
360 StartPciDevices (Controller
);
362 if (gFullEnumeration
) {
363 gFullEnumeration
= FALSE
;
365 Status
= gBS
->InstallProtocolInterface (
366 &PciRootBridgeIo
->ParentHandle
,
367 &gEfiPciEnumerationCompleteProtocolGuid
,
368 EFI_NATIVE_INTERFACE
,
371 ASSERT_EFI_ERROR (Status
);
378 Stop this driver on ControllerHandle. Support stopping any child handles
379 created by this driver.
381 @param This Protocol instance pointer.
382 @param Controller Handle of device to stop driver on.
383 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
384 children is zero stop the entire bus driver.
385 @param ChildHandleBuffer List of Child Handles to Stop.
387 @retval EFI_SUCCESS This driver is removed ControllerHandle.
388 @retval other This driver was not removed from this device.
393 PciBusDriverBindingStop (
394 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
395 IN EFI_HANDLE Controller
,
396 IN UINTN NumberOfChildren
,
397 IN EFI_HANDLE
*ChildHandleBuffer
402 BOOLEAN AllChildrenStopped
;
404 if (NumberOfChildren
== 0) {
406 // Close the bus driver
410 &gEfiDevicePathProtocolGuid
,
411 This
->DriverBindingHandle
,
416 &gEfiPciRootBridgeIoProtocolGuid
,
417 This
->DriverBindingHandle
,
421 DestroyRootBridgeByHandle (
429 // Stop all the children
432 AllChildrenStopped
= TRUE
;
434 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
437 // De register all the pci device
439 Status
= DeRegisterPciDevice (Controller
, ChildHandleBuffer
[Index
]);
441 if (EFI_ERROR (Status
)) {
442 AllChildrenStopped
= FALSE
;
446 if (!AllChildrenStopped
) {
447 return EFI_DEVICE_ERROR
;