]>
Commit | Line | Data |
---|---|---|
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 | |
995d8b85 | 11 | Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r |
9d510e61 | 12 | SPDX-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 | |
1436aea4 | 21 | EFI_DRIVER_BINDING_PROTOCOL gPciBusDriverBinding = {\r |
9060e3ec | 22 | PciBusDriverBindingSupported,\r |
23 | PciBusDriverBindingStart,\r | |
24 | PciBusDriverBindingStop,\r | |
25 | 0xa,\r | |
26 | NULL,\r | |
27 | NULL\r | |
28 | };\r | |
29 | \r | |
30 | EFI_HANDLE gPciHostBrigeHandles[PCI_MAX_HOST_BRIDGE_NUM];\r | |
ea669c1b | 31 | EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL *gIncompatiblePciDeviceSupport = NULL;\r |
1436aea4 MK |
32 | UINTN gPciHostBridgeNumber = 0;\r |
33 | BOOLEAN gFullEnumeration = TRUE;\r | |
34 | UINT64 gAllOne = 0xFFFFFFFFFFFFFFFFULL;\r | |
35 | UINT64 gAllZero = 0;\r | |
9060e3ec | 36 | \r |
1436aea4 MK |
37 | EFI_PCI_PLATFORM_PROTOCOL *gPciPlatformProtocol;\r |
38 | EFI_PCI_OVERRIDE_PROTOCOL *gPciOverrideProtocol;\r | |
39 | EDKII_IOMMU_PROTOCOL *mIoMmuProtocol;\r | |
40 | EDKII_DEVICE_SECURITY_PROTOCOL *mDeviceSecurityProtocol;\r | |
9060e3ec | 41 | \r |
1436aea4 | 42 | GLOBAL_REMOVE_IF_UNREFERENCED EFI_PCI_HOTPLUG_REQUEST_PROTOCOL mPciHotPlugRequest = {\r |
9060e3ec | 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 | |
61 | EFI_STATUS\r | |
62 | EFIAPI\r | |
63 | PciBusEntryPoint (\r | |
1436aea4 MK |
64 | IN EFI_HANDLE ImageHandle,\r |
65 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
9060e3ec | 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 | |
119 | EFI_STATUS\r | |
120 | EFIAPI\r | |
121 | PciBusDriverBindingSupported (\r | |
1436aea4 MK |
122 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r |
123 | IN EFI_HANDLE Controller,\r | |
124 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
9060e3ec | 125 | )\r |
126 | {\r | |
1436aea4 MK |
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 | |
9060e3ec | 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 | |
1436aea4 MK |
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 | {\r | |
9060e3ec | 150 | return EFI_UNSUPPORTED;\r |
151 | }\r | |
152 | }\r | |
153 | }\r | |
154 | \r | |
155 | //\r | |
156 | // Check if Pci Root Bridge IO protocol is installed by platform\r | |
157 | //\r | |
158 | Status = gBS->OpenProtocol (\r | |
159 | Controller,\r | |
160 | &gEfiPciRootBridgeIoProtocolGuid,\r | |
1436aea4 | 161 | (VOID **)&PciRootBridgeIo,\r |
9060e3ec | 162 | This->DriverBindingHandle,\r |
163 | Controller,\r | |
164 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
165 | );\r | |
166 | if (Status == EFI_ALREADY_STARTED) {\r | |
167 | return EFI_SUCCESS;\r | |
168 | }\r | |
169 | \r | |
170 | if (EFI_ERROR (Status)) {\r | |
171 | return Status;\r | |
172 | }\r | |
173 | \r | |
174 | //\r | |
175 | // Close the I/O Abstraction(s) used to perform the supported test\r | |
176 | //\r | |
177 | gBS->CloseProtocol (\r | |
1436aea4 MK |
178 | Controller,\r |
179 | &gEfiPciRootBridgeIoProtocolGuid,\r | |
180 | This->DriverBindingHandle,\r | |
181 | Controller\r | |
182 | );\r | |
9060e3ec | 183 | \r |
184 | //\r | |
185 | // Open the EFI Device Path protocol needed to perform the supported test\r | |
186 | //\r | |
187 | Status = gBS->OpenProtocol (\r | |
188 | Controller,\r | |
189 | &gEfiDevicePathProtocolGuid,\r | |
1436aea4 | 190 | (VOID **)&ParentDevicePath,\r |
9060e3ec | 191 | This->DriverBindingHandle,\r |
192 | Controller,\r | |
193 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
194 | );\r | |
195 | if (Status == EFI_ALREADY_STARTED) {\r | |
196 | return EFI_SUCCESS;\r | |
197 | }\r | |
198 | \r | |
199 | if (EFI_ERROR (Status)) {\r | |
200 | return Status;\r | |
201 | }\r | |
202 | \r | |
203 | //\r | |
204 | // Close protocol, don't use device path protocol in the Support() function\r | |
205 | //\r | |
206 | gBS->CloseProtocol (\r | |
1436aea4 MK |
207 | Controller,\r |
208 | &gEfiDevicePathProtocolGuid,\r | |
209 | This->DriverBindingHandle,\r | |
210 | Controller\r | |
211 | );\r | |
9060e3ec | 212 | \r |
213 | return EFI_SUCCESS;\r | |
214 | }\r | |
215 | \r | |
216 | /**\r | |
217 | Start this driver on ControllerHandle and enumerate Pci bus and start\r | |
218 | all device under PCI bus.\r | |
219 | \r | |
220 | @param This Protocol instance pointer.\r | |
221 | @param Controller Handle of device to bind driver to.\r | |
ed356b9e | 222 | @param RemainingDevicePath Optional parameter use to pick a specific child\r |
9060e3ec | 223 | device to start.\r |
224 | \r | |
225 | @retval EFI_SUCCESS This driver is added to ControllerHandle.\r | |
226 | @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.\r | |
227 | @retval other This driver does not support this device.\r | |
228 | \r | |
229 | **/\r | |
230 | EFI_STATUS\r | |
231 | EFIAPI\r | |
232 | PciBusDriverBindingStart (\r | |
233 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
234 | IN EFI_HANDLE Controller,\r | |
235 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
236 | )\r | |
237 | {\r | |
1436aea4 MK |
238 | EFI_STATUS Status;\r |
239 | EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r | |
240 | EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r | |
9060e3ec | 241 | \r |
a012bf6e DB |
242 | //\r |
243 | // Initialize PciRootBridgeIo to suppress incorrect compiler warning.\r | |
244 | //\r | |
245 | PciRootBridgeIo = NULL;\r | |
246 | \r | |
9060e3ec | 247 | //\r |
248 | // Check RemainingDevicePath validation\r | |
249 | //\r | |
250 | if (RemainingDevicePath != NULL) {\r | |
251 | //\r | |
d1102dba | 252 | // Check if RemainingDevicePath is the End of Device Path Node,\r |
9060e3ec | 253 | // if yes, return EFI_SUCCESS\r |
254 | //\r | |
255 | if (IsDevicePathEnd (RemainingDevicePath)) {\r | |
256 | return EFI_SUCCESS;\r | |
257 | }\r | |
258 | }\r | |
259 | \r | |
500e2ac2 OM |
260 | gBS->LocateProtocol (\r |
261 | &gEfiIncompatiblePciDeviceSupportProtocolGuid,\r | |
262 | NULL,\r | |
1436aea4 | 263 | (VOID **)&gIncompatiblePciDeviceSupport\r |
500e2ac2 | 264 | );\r |
9060e3ec | 265 | \r |
266 | //\r | |
267 | // If PCI Platform protocol is available, get it now.\r | |
268 | // If the platform implements this, it must be installed before BDS phase\r | |
269 | //\r | |
270 | gPciPlatformProtocol = NULL;\r | |
271 | gBS->LocateProtocol (\r | |
1436aea4 MK |
272 | &gEfiPciPlatformProtocolGuid,\r |
273 | NULL,\r | |
274 | (VOID **)&gPciPlatformProtocol\r | |
275 | );\r | |
9060e3ec | 276 | \r |
277 | //\r | |
278 | // If PCI Platform protocol doesn't exist, try to Pci Override Protocol.\r | |
279 | //\r | |
d1102dba | 280 | if (gPciPlatformProtocol == NULL) {\r |
9060e3ec | 281 | gPciOverrideProtocol = NULL;\r |
282 | gBS->LocateProtocol (\r | |
1436aea4 MK |
283 | &gEfiPciOverrideProtocolGuid,\r |
284 | NULL,\r | |
285 | (VOID **)&gPciOverrideProtocol\r | |
286 | );\r | |
d1102dba | 287 | }\r |
9060e3ec | 288 | \r |
11a6cc5b JY |
289 | if (mIoMmuProtocol == NULL) {\r |
290 | gBS->LocateProtocol (\r | |
1436aea4 MK |
291 | &gEdkiiIoMmuProtocolGuid,\r |
292 | NULL,\r | |
293 | (VOID **)&mIoMmuProtocol\r | |
294 | );\r | |
11a6cc5b JY |
295 | }\r |
296 | \r | |
995d8b85 JY |
297 | if (mDeviceSecurityProtocol == NULL) {\r |
298 | gBS->LocateProtocol (\r | |
1436aea4 MK |
299 | &gEdkiiDeviceSecurityProtocolGuid,\r |
300 | NULL,\r | |
301 | (VOID **)&mDeviceSecurityProtocol\r | |
302 | );\r | |
995d8b85 JY |
303 | }\r |
304 | \r | |
5624a27d WL |
305 | if (PcdGetBool (PcdPciDisableBusEnumeration)) {\r |
306 | gFullEnumeration = FALSE;\r | |
307 | } else {\r | |
1436aea4 | 308 | gFullEnumeration = (BOOLEAN)((SearchHostBridgeHandle (Controller) ? FALSE : TRUE));\r |
5624a27d | 309 | }\r |
9060e3ec | 310 | \r |
37623a5c | 311 | //\r |
312 | // Open Device Path Protocol for PCI root bridge\r | |
313 | //\r | |
314 | Status = gBS->OpenProtocol (\r | |
315 | Controller,\r | |
316 | &gEfiDevicePathProtocolGuid,\r | |
1436aea4 | 317 | (VOID **)&ParentDevicePath,\r |
37623a5c | 318 | This->DriverBindingHandle,\r |
319 | Controller,\r | |
320 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
d1102dba | 321 | );\r |
37623a5c | 322 | ASSERT_EFI_ERROR (Status);\r |
323 | \r | |
324 | //\r | |
325 | // Report Status Code to indicate PCI bus starts\r | |
326 | //\r | |
327 | REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r | |
328 | EFI_PROGRESS_CODE,\r | |
329 | (EFI_IO_BUS_PCI | EFI_IOB_PC_INIT),\r | |
330 | ParentDevicePath\r | |
331 | );\r | |
332 | \r | |
26329817 | 333 | Status = EFI_SUCCESS;\r |
9060e3ec | 334 | //\r |
335 | // Enumerate the entire host bridge\r | |
336 | // After enumeration, a database that records all the device information will be created\r | |
337 | //\r | |
338 | //\r | |
26329817 RN |
339 | if (gFullEnumeration) {\r |
340 | //\r | |
341 | // Get the rootbridge Io protocol to find the host bridge handle\r | |
342 | //\r | |
343 | Status = gBS->OpenProtocol (\r | |
344 | Controller,\r | |
345 | &gEfiPciRootBridgeIoProtocolGuid,\r | |
1436aea4 | 346 | (VOID **)&PciRootBridgeIo,\r |
26329817 RN |
347 | gPciBusDriverBinding.DriverBindingHandle,\r |
348 | Controller,\r | |
349 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
350 | );\r | |
351 | \r | |
352 | if (!EFI_ERROR (Status)) {\r | |
353 | Status = PciEnumerator (Controller, PciRootBridgeIo->ParentHandle);\r | |
354 | }\r | |
355 | } else {\r | |
356 | //\r | |
357 | // If PCI bus has already done the full enumeration, never do it again\r | |
358 | //\r | |
359 | Status = PciEnumeratorLight (Controller);\r | |
360 | }\r | |
9060e3ec | 361 | \r |
362 | if (EFI_ERROR (Status)) {\r | |
363 | return Status;\r | |
364 | }\r | |
365 | \r | |
366 | //\r | |
367 | // Start all the devices under the entire host bridge.\r | |
368 | //\r | |
369 | StartPciDevices (Controller);\r | |
370 | \r | |
42e8bc7d RN |
371 | if (gFullEnumeration) {\r |
372 | gFullEnumeration = FALSE;\r | |
373 | \r | |
374 | Status = gBS->InstallProtocolInterface (\r | |
375 | &PciRootBridgeIo->ParentHandle,\r | |
376 | &gEfiPciEnumerationCompleteProtocolGuid,\r | |
377 | EFI_NATIVE_INTERFACE,\r | |
378 | NULL\r | |
379 | );\r | |
380 | ASSERT_EFI_ERROR (Status);\r | |
381 | }\r | |
382 | \r | |
383 | return Status;\r | |
9060e3ec | 384 | }\r |
385 | \r | |
386 | /**\r | |
ed356b9e | 387 | Stop this driver on ControllerHandle. Support stopping any child handles\r |
9060e3ec | 388 | created by this driver.\r |
389 | \r | |
390 | @param This Protocol instance pointer.\r | |
391 | @param Controller Handle of device to stop driver on.\r | |
392 | @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r | |
393 | children is zero stop the entire bus driver.\r | |
394 | @param ChildHandleBuffer List of Child Handles to Stop.\r | |
395 | \r | |
396 | @retval EFI_SUCCESS This driver is removed ControllerHandle.\r | |
397 | @retval other This driver was not removed from this device.\r | |
398 | \r | |
399 | **/\r | |
400 | EFI_STATUS\r | |
401 | EFIAPI\r | |
402 | PciBusDriverBindingStop (\r | |
1436aea4 MK |
403 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r |
404 | IN EFI_HANDLE Controller,\r | |
405 | IN UINTN NumberOfChildren,\r | |
406 | IN EFI_HANDLE *ChildHandleBuffer\r | |
9060e3ec | 407 | )\r |
408 | {\r | |
409 | EFI_STATUS Status;\r | |
410 | UINTN Index;\r | |
411 | BOOLEAN AllChildrenStopped;\r | |
412 | \r | |
413 | if (NumberOfChildren == 0) {\r | |
414 | //\r | |
415 | // Close the bus driver\r | |
416 | //\r | |
417 | gBS->CloseProtocol (\r | |
1436aea4 MK |
418 | Controller,\r |
419 | &gEfiDevicePathProtocolGuid,\r | |
420 | This->DriverBindingHandle,\r | |
421 | Controller\r | |
422 | );\r | |
9060e3ec | 423 | gBS->CloseProtocol (\r |
1436aea4 MK |
424 | Controller,\r |
425 | &gEfiPciRootBridgeIoProtocolGuid,\r | |
426 | This->DriverBindingHandle,\r | |
427 | Controller\r | |
428 | );\r | |
9060e3ec | 429 | \r |
430 | DestroyRootBridgeByHandle (\r | |
431 | Controller\r | |
432 | );\r | |
433 | \r | |
434 | return EFI_SUCCESS;\r | |
435 | }\r | |
436 | \r | |
437 | //\r | |
438 | // Stop all the children\r | |
439 | //\r | |
440 | \r | |
441 | AllChildrenStopped = TRUE;\r | |
442 | \r | |
443 | for (Index = 0; Index < NumberOfChildren; Index++) {\r | |
9060e3ec | 444 | //\r |
445 | // De register all the pci device\r | |
446 | //\r | |
447 | Status = DeRegisterPciDevice (Controller, ChildHandleBuffer[Index]);\r | |
448 | \r | |
449 | if (EFI_ERROR (Status)) {\r | |
450 | AllChildrenStopped = FALSE;\r | |
451 | }\r | |
452 | }\r | |
453 | \r | |
454 | if (!AllChildrenStopped) {\r | |
455 | return EFI_DEVICE_ERROR;\r | |
456 | }\r | |
457 | \r | |
458 | return EFI_SUCCESS;\r | |
459 | }\r |