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