]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.c
MdeModulePkg/PciBusDxe: dispatch option ROMs for foreign architectures
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciBusDxe / PciBus.c
CommitLineData
9060e3ec 1/** @file\r
2 Driver Binding functions for PCI Bus module.\r
3\r
4 Single PCI bus driver instance will manager all PCI Root Bridges in one EFI based firmware,\r
5 since all PCI Root Bridges' resources need to be managed together.\r
6 Supported() function will try to get PCI Root Bridge IO Protocol.\r
7 Start() function will get PCI Host Bridge Resource Allocation Protocol to manage all\r
8 PCI Root Bridges. So it means platform needs install PCI Root Bridge IO protocol for each\r
9 PCI Root Bus and install PCI Host Bridge Resource Allocation Protocol.\r
10\r
26329817 11Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 12SPDX-License-Identifier: BSD-2-Clause-Patent\r
9060e3ec 13\r
14**/\r
15\r
16#include "PciBus.h"\r
17\r
18//\r
19// PCI Bus Driver Global Variables\r
20//\r
21EFI_DRIVER_BINDING_PROTOCOL gPciBusDriverBinding = {\r
22 PciBusDriverBindingSupported,\r
23 PciBusDriverBindingStart,\r
24 PciBusDriverBindingStop,\r
25 0xa,\r
26 NULL,\r
27 NULL\r
28};\r
29\r
30EFI_HANDLE gPciHostBrigeHandles[PCI_MAX_HOST_BRIDGE_NUM];\r
ea669c1b 31EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL *gIncompatiblePciDeviceSupport = NULL;\r
9060e3ec 32UINTN gPciHostBridgeNumber = 0;\r
33BOOLEAN gFullEnumeration = TRUE;\r
34UINT64 gAllOne = 0xFFFFFFFFFFFFFFFFULL;\r
35UINT64 gAllZero = 0;\r
36\r
37EFI_PCI_PLATFORM_PROTOCOL *gPciPlatformProtocol;\r
38EFI_PCI_OVERRIDE_PROTOCOL *gPciOverrideProtocol;\r
11a6cc5b 39EDKII_IOMMU_PROTOCOL *mIoMmuProtocol;\r
9060e3ec 40\r
41\r
42GLOBAL_REMOVE_IF_UNREFERENCED EFI_PCI_HOTPLUG_REQUEST_PROTOCOL mPciHotPlugRequest = {\r
43 PciHotPlugRequestNotify\r
44};\r
45\r
46/**\r
47 The Entry Point for PCI Bus module. The user code starts with this function.\r
48\r
49 Installs driver module protocols and. Creates virtual device handles for ConIn,\r
50 ConOut, and StdErr. Installs Simple Text In protocol, Simple Text In Ex protocol,\r
51 Simple Pointer protocol, Absolute Pointer protocol on those virtual handlers.\r
52 Installs Graphics Output protocol and/or UGA Draw protocol if needed.\r
53\r
54 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
55 @param[in] SystemTable A pointer to the EFI System Table.\r
56\r
57 @retval EFI_SUCCESS The entry point is executed successfully.\r
58 @retval other Some error occurred when executing this entry point.\r
59\r
60**/\r
61EFI_STATUS\r
62EFIAPI\r
63PciBusEntryPoint (\r
64 IN EFI_HANDLE ImageHandle,\r
65 IN EFI_SYSTEM_TABLE *SystemTable\r
66 )\r
67{\r
68 EFI_STATUS Status;\r
69 EFI_HANDLE Handle;\r
70\r
71 //\r
72 // Initializes PCI devices pool\r
73 //\r
74 InitializePciDevicePool ();\r
75\r
76 //\r
77 // Install driver model protocol(s).\r
78 //\r
79 Status = EfiLibInstallDriverBindingComponentName2 (\r
80 ImageHandle,\r
81 SystemTable,\r
82 &gPciBusDriverBinding,\r
83 ImageHandle,\r
84 &gPciBusComponentName,\r
85 &gPciBusComponentName2\r
86 );\r
87 ASSERT_EFI_ERROR (Status);\r
88\r
89 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport)) {\r
90 //\r
91 // If Hot Plug is supported, install EFI PCI Hot Plug Request protocol.\r
92 //\r
93 Handle = NULL;\r
94 Status = gBS->InstallProtocolInterface (\r
95 &Handle,\r
96 &gEfiPciHotPlugRequestProtocolGuid,\r
97 EFI_NATIVE_INTERFACE,\r
98 &mPciHotPlugRequest\r
99 );\r
100 }\r
101\r
102 return Status;\r
103}\r
104\r
105/**\r
106 Test to see if this driver supports ControllerHandle. Any ControllerHandle\r
107 than contains a gEfiPciRootBridgeIoProtocolGuid protocol can be supported.\r
108\r
109 @param This Protocol instance pointer.\r
110 @param Controller Handle of device to test.\r
ed356b9e 111 @param RemainingDevicePath Optional parameter use to pick a specific child\r
9060e3ec 112 device to start.\r
113\r
114 @retval EFI_SUCCESS This driver supports this device.\r
115 @retval EFI_ALREADY_STARTED This driver is already running on this device.\r
116 @retval other This driver does not support this device.\r
117\r
118**/\r
119EFI_STATUS\r
120EFIAPI\r
121PciBusDriverBindingSupported (\r
122 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
123 IN EFI_HANDLE Controller,\r
124 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
125 )\r
126{\r
127 EFI_STATUS Status;\r
128 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
129 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
130 EFI_DEV_PATH_PTR Node;\r
131\r
132 //\r
133 // Check RemainingDevicePath validation\r
134 //\r
135 if (RemainingDevicePath != NULL) {\r
136 //\r
d1102dba 137 // Check if RemainingDevicePath is the End of Device Path Node,\r
9060e3ec 138 // if yes, go on checking other conditions\r
139 //\r
140 if (!IsDevicePathEnd (RemainingDevicePath)) {\r
141 //\r
142 // If RemainingDevicePath isn't the End of Device Path Node,\r
143 // check its validation\r
144 //\r
145 Node.DevPath = RemainingDevicePath;\r
146 if (Node.DevPath->Type != HARDWARE_DEVICE_PATH ||\r
147 Node.DevPath->SubType != HW_PCI_DP ||\r
148 DevicePathNodeLength(Node.DevPath) != sizeof(PCI_DEVICE_PATH)) {\r
149 return EFI_UNSUPPORTED;\r
150 }\r
151 }\r
152 }\r
153\r
154 //\r
155 // Check if Pci Root Bridge IO protocol is installed by platform\r
156 //\r
157 Status = gBS->OpenProtocol (\r
158 Controller,\r
159 &gEfiPciRootBridgeIoProtocolGuid,\r
160 (VOID **) &PciRootBridgeIo,\r
161 This->DriverBindingHandle,\r
162 Controller,\r
163 EFI_OPEN_PROTOCOL_BY_DRIVER\r
164 );\r
165 if (Status == EFI_ALREADY_STARTED) {\r
166 return EFI_SUCCESS;\r
167 }\r
168\r
169 if (EFI_ERROR (Status)) {\r
170 return Status;\r
171 }\r
172\r
173 //\r
174 // Close the I/O Abstraction(s) used to perform the supported test\r
175 //\r
176 gBS->CloseProtocol (\r
177 Controller,\r
178 &gEfiPciRootBridgeIoProtocolGuid,\r
179 This->DriverBindingHandle,\r
180 Controller\r
181 );\r
182\r
183 //\r
184 // Open the EFI Device Path protocol needed to perform the supported test\r
185 //\r
186 Status = gBS->OpenProtocol (\r
187 Controller,\r
188 &gEfiDevicePathProtocolGuid,\r
189 (VOID **) &ParentDevicePath,\r
190 This->DriverBindingHandle,\r
191 Controller,\r
192 EFI_OPEN_PROTOCOL_BY_DRIVER\r
193 );\r
194 if (Status == EFI_ALREADY_STARTED) {\r
195 return EFI_SUCCESS;\r
196 }\r
197\r
198 if (EFI_ERROR (Status)) {\r
199 return Status;\r
200 }\r
201\r
202 //\r
203 // Close protocol, don't use device path protocol in the Support() function\r
204 //\r
205 gBS->CloseProtocol (\r
206 Controller,\r
207 &gEfiDevicePathProtocolGuid,\r
208 This->DriverBindingHandle,\r
209 Controller\r
210 );\r
211\r
212 return EFI_SUCCESS;\r
213}\r
214\r
215/**\r
216 Start this driver on ControllerHandle and enumerate Pci bus and start\r
217 all device under PCI bus.\r
218\r
219 @param This Protocol instance pointer.\r
220 @param Controller Handle of device to bind driver to.\r
ed356b9e 221 @param RemainingDevicePath Optional parameter use to pick a specific child\r
9060e3ec 222 device to start.\r
223\r
224 @retval EFI_SUCCESS This driver is added to ControllerHandle.\r
225 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.\r
226 @retval other This driver does not support this device.\r
227\r
228**/\r
229EFI_STATUS\r
230EFIAPI\r
231PciBusDriverBindingStart (\r
232 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
233 IN EFI_HANDLE Controller,\r
234 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
235 )\r
236{\r
26329817
RN
237 EFI_STATUS Status;\r
238 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
239 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
9060e3ec 240\r
a012bf6e
DB
241 //\r
242 // Initialize PciRootBridgeIo to suppress incorrect compiler warning.\r
243 //\r
244 PciRootBridgeIo = NULL;\r
245\r
9060e3ec 246 //\r
247 // Check RemainingDevicePath validation\r
248 //\r
249 if (RemainingDevicePath != NULL) {\r
250 //\r
d1102dba 251 // Check if RemainingDevicePath is the End of Device Path Node,\r
9060e3ec 252 // if yes, return EFI_SUCCESS\r
253 //\r
254 if (IsDevicePathEnd (RemainingDevicePath)) {\r
255 return EFI_SUCCESS;\r
256 }\r
257 }\r
258\r
500e2ac2
OM
259 gBS->LocateProtocol (\r
260 &gEfiIncompatiblePciDeviceSupportProtocolGuid,\r
261 NULL,\r
ea669c1b 262 (VOID **) &gIncompatiblePciDeviceSupport\r
500e2ac2 263 );\r
9060e3ec 264\r
265 //\r
266 // If PCI Platform protocol is available, get it now.\r
267 // If the platform implements this, it must be installed before BDS phase\r
268 //\r
269 gPciPlatformProtocol = NULL;\r
270 gBS->LocateProtocol (\r
271 &gEfiPciPlatformProtocolGuid,\r
272 NULL,\r
273 (VOID **) &gPciPlatformProtocol\r
274 );\r
275\r
276 //\r
277 // If PCI Platform protocol doesn't exist, try to Pci Override Protocol.\r
278 //\r
d1102dba 279 if (gPciPlatformProtocol == NULL) {\r
9060e3ec 280 gPciOverrideProtocol = NULL;\r
281 gBS->LocateProtocol (\r
282 &gEfiPciOverrideProtocolGuid,\r
283 NULL,\r
284 (VOID **) &gPciOverrideProtocol\r
285 );\r
d1102dba 286 }\r
9060e3ec 287\r
11a6cc5b
JY
288 if (mIoMmuProtocol == NULL) {\r
289 gBS->LocateProtocol (\r
290 &gEdkiiIoMmuProtocolGuid,\r
291 NULL,\r
292 (VOID **) &mIoMmuProtocol\r
293 );\r
294 }\r
295\r
5624a27d
WL
296 if (PcdGetBool (PcdPciDisableBusEnumeration)) {\r
297 gFullEnumeration = FALSE;\r
298 } else {\r
299 gFullEnumeration = (BOOLEAN) ((SearchHostBridgeHandle (Controller) ? FALSE : TRUE));\r
300 }\r
9060e3ec 301\r
37623a5c 302 //\r
303 // Open Device Path Protocol for PCI root bridge\r
304 //\r
305 Status = gBS->OpenProtocol (\r
306 Controller,\r
307 &gEfiDevicePathProtocolGuid,\r
308 (VOID **) &ParentDevicePath,\r
309 This->DriverBindingHandle,\r
310 Controller,\r
311 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
d1102dba 312 );\r
37623a5c 313 ASSERT_EFI_ERROR (Status);\r
314\r
315 //\r
316 // Report Status Code to indicate PCI bus starts\r
317 //\r
318 REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
319 EFI_PROGRESS_CODE,\r
320 (EFI_IO_BUS_PCI | EFI_IOB_PC_INIT),\r
321 ParentDevicePath\r
322 );\r
323\r
26329817 324 Status = EFI_SUCCESS;\r
9060e3ec 325 //\r
326 // Enumerate the entire host bridge\r
327 // After enumeration, a database that records all the device information will be created\r
328 //\r
329 //\r
26329817
RN
330 if (gFullEnumeration) {\r
331 //\r
332 // Get the rootbridge Io protocol to find the host bridge handle\r
333 //\r
334 Status = gBS->OpenProtocol (\r
335 Controller,\r
336 &gEfiPciRootBridgeIoProtocolGuid,\r
337 (VOID **) &PciRootBridgeIo,\r
338 gPciBusDriverBinding.DriverBindingHandle,\r
339 Controller,\r
340 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
341 );\r
342\r
343 if (!EFI_ERROR (Status)) {\r
344 Status = PciEnumerator (Controller, PciRootBridgeIo->ParentHandle);\r
345 }\r
346 } else {\r
347 //\r
348 // If PCI bus has already done the full enumeration, never do it again\r
349 //\r
350 Status = PciEnumeratorLight (Controller);\r
351 }\r
9060e3ec 352\r
353 if (EFI_ERROR (Status)) {\r
354 return Status;\r
355 }\r
356\r
357 //\r
358 // Start all the devices under the entire host bridge.\r
359 //\r
360 StartPciDevices (Controller);\r
361\r
42e8bc7d
RN
362 if (gFullEnumeration) {\r
363 gFullEnumeration = FALSE;\r
364\r
365 Status = gBS->InstallProtocolInterface (\r
366 &PciRootBridgeIo->ParentHandle,\r
367 &gEfiPciEnumerationCompleteProtocolGuid,\r
368 EFI_NATIVE_INTERFACE,\r
369 NULL\r
370 );\r
371 ASSERT_EFI_ERROR (Status);\r
372 }\r
373\r
374 return Status;\r
9060e3ec 375}\r
376\r
377/**\r
ed356b9e 378 Stop this driver on ControllerHandle. Support stopping any child handles\r
9060e3ec 379 created by this driver.\r
380\r
381 @param This Protocol instance pointer.\r
382 @param Controller Handle of device to stop driver on.\r
383 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
384 children is zero stop the entire bus driver.\r
385 @param ChildHandleBuffer List of Child Handles to Stop.\r
386\r
387 @retval EFI_SUCCESS This driver is removed ControllerHandle.\r
388 @retval other This driver was not removed from this device.\r
389\r
390**/\r
391EFI_STATUS\r
392EFIAPI\r
393PciBusDriverBindingStop (\r
394 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
395 IN EFI_HANDLE Controller,\r
396 IN UINTN NumberOfChildren,\r
397 IN EFI_HANDLE *ChildHandleBuffer\r
398 )\r
399{\r
400 EFI_STATUS Status;\r
401 UINTN Index;\r
402 BOOLEAN AllChildrenStopped;\r
403\r
404 if (NumberOfChildren == 0) {\r
405 //\r
406 // Close the bus driver\r
407 //\r
408 gBS->CloseProtocol (\r
409 Controller,\r
410 &gEfiDevicePathProtocolGuid,\r
411 This->DriverBindingHandle,\r
412 Controller\r
413 );\r
414 gBS->CloseProtocol (\r
415 Controller,\r
416 &gEfiPciRootBridgeIoProtocolGuid,\r
417 This->DriverBindingHandle,\r
418 Controller\r
419 );\r
420\r
421 DestroyRootBridgeByHandle (\r
422 Controller\r
423 );\r
424\r
425 return EFI_SUCCESS;\r
426 }\r
427\r
428 //\r
429 // Stop all the children\r
430 //\r
431\r
432 AllChildrenStopped = TRUE;\r
433\r
434 for (Index = 0; Index < NumberOfChildren; Index++) {\r
435\r
436 //\r
437 // De register all the pci device\r
438 //\r
439 Status = DeRegisterPciDevice (Controller, ChildHandleBuffer[Index]);\r
440\r
441 if (EFI_ERROR (Status)) {\r
442 AllChildrenStopped = FALSE;\r
443 }\r
444 }\r
445\r
446 if (!AllChildrenStopped) {\r
447 return EFI_DEVICE_ERROR;\r
448 }\r
449\r
450 return EFI_SUCCESS;\r
451}\r
452\r