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 This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25 // PCI Bus Driver Global Variables
27 EFI_DRIVER_BINDING_PROTOCOL gPciBusDriverBinding
= {
28 PciBusDriverBindingSupported
,
29 PciBusDriverBindingStart
,
30 PciBusDriverBindingStop
,
36 EFI_HANDLE gPciHostBrigeHandles
[PCI_MAX_HOST_BRIDGE_NUM
];
37 EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL
*gIncompatiblePciDeviceSupport
= NULL
;
38 UINTN gPciHostBridgeNumber
= 0;
39 BOOLEAN gFullEnumeration
= TRUE
;
40 UINT64 gAllOne
= 0xFFFFFFFFFFFFFFFFULL
;
43 EFI_PCI_PLATFORM_PROTOCOL
*gPciPlatformProtocol
;
44 EFI_PCI_OVERRIDE_PROTOCOL
*gPciOverrideProtocol
;
45 EDKII_IOMMU_PROTOCOL
*mIoMmuProtocol
;
48 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PCI_HOTPLUG_REQUEST_PROTOCOL mPciHotPlugRequest
= {
49 PciHotPlugRequestNotify
53 The Entry Point for PCI Bus module. The user code starts with this function.
55 Installs driver module protocols and. Creates virtual device handles for ConIn,
56 ConOut, and StdErr. Installs Simple Text In protocol, Simple Text In Ex protocol,
57 Simple Pointer protocol, Absolute Pointer protocol on those virtual handlers.
58 Installs Graphics Output protocol and/or UGA Draw protocol if needed.
60 @param[in] ImageHandle The firmware allocated handle for the EFI image.
61 @param[in] SystemTable A pointer to the EFI System Table.
63 @retval EFI_SUCCESS The entry point is executed successfully.
64 @retval other Some error occurred when executing this entry point.
70 IN EFI_HANDLE ImageHandle
,
71 IN EFI_SYSTEM_TABLE
*SystemTable
78 // Initializes PCI devices pool
80 InitializePciDevicePool ();
83 // Install driver model protocol(s).
85 Status
= EfiLibInstallDriverBindingComponentName2 (
88 &gPciBusDriverBinding
,
90 &gPciBusComponentName
,
91 &gPciBusComponentName2
93 ASSERT_EFI_ERROR (Status
);
95 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
97 // If Hot Plug is supported, install EFI PCI Hot Plug Request protocol.
100 Status
= gBS
->InstallProtocolInterface (
102 &gEfiPciHotPlugRequestProtocolGuid
,
103 EFI_NATIVE_INTERFACE
,
112 Test to see if this driver supports ControllerHandle. Any ControllerHandle
113 than contains a gEfiPciRootBridgeIoProtocolGuid protocol can be supported.
115 @param This Protocol instance pointer.
116 @param Controller Handle of device to test.
117 @param RemainingDevicePath Optional parameter use to pick a specific child
120 @retval EFI_SUCCESS This driver supports this device.
121 @retval EFI_ALREADY_STARTED This driver is already running on this device.
122 @retval other This driver does not support this device.
127 PciBusDriverBindingSupported (
128 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
129 IN EFI_HANDLE Controller
,
130 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
134 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
135 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
136 EFI_DEV_PATH_PTR Node
;
139 // Check RemainingDevicePath validation
141 if (RemainingDevicePath
!= NULL
) {
143 // Check if RemainingDevicePath is the End of Device Path Node,
144 // if yes, go on checking other conditions
146 if (!IsDevicePathEnd (RemainingDevicePath
)) {
148 // If RemainingDevicePath isn't the End of Device Path Node,
149 // check its validation
151 Node
.DevPath
= RemainingDevicePath
;
152 if (Node
.DevPath
->Type
!= HARDWARE_DEVICE_PATH
||
153 Node
.DevPath
->SubType
!= HW_PCI_DP
||
154 DevicePathNodeLength(Node
.DevPath
) != sizeof(PCI_DEVICE_PATH
)) {
155 return EFI_UNSUPPORTED
;
161 // Check if Pci Root Bridge IO protocol is installed by platform
163 Status
= gBS
->OpenProtocol (
165 &gEfiPciRootBridgeIoProtocolGuid
,
166 (VOID
**) &PciRootBridgeIo
,
167 This
->DriverBindingHandle
,
169 EFI_OPEN_PROTOCOL_BY_DRIVER
171 if (Status
== EFI_ALREADY_STARTED
) {
175 if (EFI_ERROR (Status
)) {
180 // Close the I/O Abstraction(s) used to perform the supported test
184 &gEfiPciRootBridgeIoProtocolGuid
,
185 This
->DriverBindingHandle
,
190 // Open the EFI Device Path protocol needed to perform the supported test
192 Status
= gBS
->OpenProtocol (
194 &gEfiDevicePathProtocolGuid
,
195 (VOID
**) &ParentDevicePath
,
196 This
->DriverBindingHandle
,
198 EFI_OPEN_PROTOCOL_BY_DRIVER
200 if (Status
== EFI_ALREADY_STARTED
) {
204 if (EFI_ERROR (Status
)) {
209 // Close protocol, don't use device path protocol in the Support() function
213 &gEfiDevicePathProtocolGuid
,
214 This
->DriverBindingHandle
,
222 Start this driver on ControllerHandle and enumerate Pci bus and start
223 all device under PCI bus.
225 @param This Protocol instance pointer.
226 @param Controller Handle of device to bind driver to.
227 @param RemainingDevicePath Optional parameter use to pick a specific child
230 @retval EFI_SUCCESS This driver is added to ControllerHandle.
231 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
232 @retval other This driver does not support this device.
237 PciBusDriverBindingStart (
238 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
239 IN EFI_HANDLE Controller
,
240 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
244 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
245 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
248 // Check RemainingDevicePath validation
250 if (RemainingDevicePath
!= NULL
) {
252 // Check if RemainingDevicePath is the End of Device Path Node,
253 // if yes, return EFI_SUCCESS
255 if (IsDevicePathEnd (RemainingDevicePath
)) {
260 gBS
->LocateProtocol (
261 &gEfiIncompatiblePciDeviceSupportProtocolGuid
,
263 (VOID
**) &gIncompatiblePciDeviceSupport
267 // If PCI Platform protocol is available, get it now.
268 // If the platform implements this, it must be installed before BDS phase
270 gPciPlatformProtocol
= NULL
;
271 gBS
->LocateProtocol (
272 &gEfiPciPlatformProtocolGuid
,
274 (VOID
**) &gPciPlatformProtocol
278 // If PCI Platform protocol doesn't exist, try to Pci Override Protocol.
280 if (gPciPlatformProtocol
== NULL
) {
281 gPciOverrideProtocol
= NULL
;
282 gBS
->LocateProtocol (
283 &gEfiPciOverrideProtocolGuid
,
285 (VOID
**) &gPciOverrideProtocol
289 if (mIoMmuProtocol
== NULL
) {
290 gBS
->LocateProtocol (
291 &gEdkiiIoMmuProtocolGuid
,
293 (VOID
**) &mIoMmuProtocol
297 if (PcdGetBool (PcdPciDisableBusEnumeration
)) {
298 gFullEnumeration
= FALSE
;
300 gFullEnumeration
= (BOOLEAN
) ((SearchHostBridgeHandle (Controller
) ? FALSE
: TRUE
));
304 // Open Device Path Protocol for PCI root bridge
306 Status
= gBS
->OpenProtocol (
308 &gEfiDevicePathProtocolGuid
,
309 (VOID
**) &ParentDevicePath
,
310 This
->DriverBindingHandle
,
312 EFI_OPEN_PROTOCOL_GET_PROTOCOL
314 ASSERT_EFI_ERROR (Status
);
317 // Report Status Code to indicate PCI bus starts
319 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
321 (EFI_IO_BUS_PCI
| EFI_IOB_PC_INIT
),
325 Status
= EFI_SUCCESS
;
327 // Enumerate the entire host bridge
328 // After enumeration, a database that records all the device information will be created
331 if (gFullEnumeration
) {
333 // Get the rootbridge Io protocol to find the host bridge handle
335 Status
= gBS
->OpenProtocol (
337 &gEfiPciRootBridgeIoProtocolGuid
,
338 (VOID
**) &PciRootBridgeIo
,
339 gPciBusDriverBinding
.DriverBindingHandle
,
341 EFI_OPEN_PROTOCOL_GET_PROTOCOL
344 if (!EFI_ERROR (Status
)) {
345 Status
= PciEnumerator (Controller
, PciRootBridgeIo
->ParentHandle
);
349 // If PCI bus has already done the full enumeration, never do it again
351 Status
= PciEnumeratorLight (Controller
);
354 if (EFI_ERROR (Status
)) {
359 // Start all the devices under the entire host bridge.
361 StartPciDevices (Controller
);
363 gFullEnumeration
= FALSE
;
368 Stop this driver on ControllerHandle. Support stopping any child handles
369 created by this driver.
371 @param This Protocol instance pointer.
372 @param Controller Handle of device to stop driver on.
373 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
374 children is zero stop the entire bus driver.
375 @param ChildHandleBuffer List of Child Handles to Stop.
377 @retval EFI_SUCCESS This driver is removed ControllerHandle.
378 @retval other This driver was not removed from this device.
383 PciBusDriverBindingStop (
384 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
385 IN EFI_HANDLE Controller
,
386 IN UINTN NumberOfChildren
,
387 IN EFI_HANDLE
*ChildHandleBuffer
392 BOOLEAN AllChildrenStopped
;
394 if (NumberOfChildren
== 0) {
396 // Close the bus driver
400 &gEfiDevicePathProtocolGuid
,
401 This
->DriverBindingHandle
,
406 &gEfiPciRootBridgeIoProtocolGuid
,
407 This
->DriverBindingHandle
,
411 DestroyRootBridgeByHandle (
419 // Stop all the children
422 AllChildrenStopped
= TRUE
;
424 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
427 // De register all the pci device
429 Status
= DeRegisterPciDevice (Controller
, ChildHandleBuffer
[Index
]);
431 if (EFI_ERROR (Status
)) {
432 AllChildrenStopped
= FALSE
;
436 if (!AllChildrenStopped
) {
437 return EFI_DEVICE_ERROR
;