]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/PciBusDxe/PciBus.c
1. updated "the Bus Driver that creates all of its child handles on the first call...
[mirror_edk2.git] / IntelFrameworkModulePkg / 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 - 2009, Intel Corporation
12 All rights reserved. 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 *gEfiIncompatiblePciDeviceSupport = 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
45 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PCI_HOTPLUG_REQUEST_PROTOCOL mPciHotPlugRequest = {
46 PciHotPlugRequestNotify
47 };
48
49 /**
50 The Entry Point for PCI Bus module. The user code starts with this function.
51
52 Installs driver module protocols and. Creates virtual device handles for ConIn,
53 ConOut, and StdErr. Installs Simple Text In protocol, Simple Text In Ex protocol,
54 Simple Pointer protocol, Absolute Pointer protocol on those virtual handlers.
55 Installs Graphics Output protocol and/or UGA Draw protocol if needed.
56
57 @param[in] ImageHandle The firmware allocated handle for the EFI image.
58 @param[in] SystemTable A pointer to the EFI System Table.
59
60 @retval EFI_SUCCESS The entry point is executed successfully.
61 @retval other Some error occurred when executing this entry point.
62
63 **/
64 EFI_STATUS
65 EFIAPI
66 PciBusEntryPoint (
67 IN EFI_HANDLE ImageHandle,
68 IN EFI_SYSTEM_TABLE *SystemTable
69 )
70 {
71 EFI_STATUS Status;
72 EFI_HANDLE Handle;
73
74 //
75 // Initializes PCI devices pool
76 //
77 InitializePciDevicePool ();
78
79 //
80 // Install driver model protocol(s).
81 //
82 Status = EfiLibInstallDriverBindingComponentName2 (
83 ImageHandle,
84 SystemTable,
85 &gPciBusDriverBinding,
86 ImageHandle,
87 &gPciBusComponentName,
88 &gPciBusComponentName2
89 );
90 ASSERT_EFI_ERROR (Status);
91
92 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport)) {
93 //
94 // If Hot Plug is supported, install EFI PCI Hot Plug Request protocol.
95 //
96 Handle = NULL;
97 Status = gBS->InstallProtocolInterface (
98 &Handle,
99 &gEfiPciHotPlugRequestProtocolGuid,
100 EFI_NATIVE_INTERFACE,
101 &mPciHotPlugRequest
102 );
103 }
104
105 return Status;
106 }
107
108 /**
109 Test to see if this driver supports ControllerHandle. Any ControllerHandle
110 than contains a gEfiPciRootBridgeIoProtocolGuid protocol can be supported.
111
112 @param This Protocol instance pointer.
113 @param Controller Handle of device to test.
114 @param RemainingDevicePath Optional parameter use to pick a specific child.
115 device to start.
116
117 @retval EFI_SUCCESS This driver supports this device.
118 @retval EFI_ALREADY_STARTED This driver is already running on this device.
119 @retval other This driver does not support this device.
120
121 **/
122 EFI_STATUS
123 EFIAPI
124 PciBusDriverBindingSupported (
125 IN EFI_DRIVER_BINDING_PROTOCOL *This,
126 IN EFI_HANDLE Controller,
127 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
128 )
129 {
130 EFI_STATUS Status;
131 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
132 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
133 EFI_DEV_PATH_PTR Node;
134
135 //
136 // Check RemainingDevicePath validation
137 //
138 if (RemainingDevicePath != NULL) {
139 //
140 // Check if RemainingDevicePath is the End of Device Path Node,
141 // if yes, go on checking other conditions
142 //
143 if (!IsDevicePathEnd (RemainingDevicePath)) {
144 //
145 // If RemainingDevicePath isn't the End of Device Path Node,
146 // check its validation
147 //
148 Node.DevPath = RemainingDevicePath;
149 if (Node.DevPath->Type != HARDWARE_DEVICE_PATH ||
150 Node.DevPath->SubType != HW_PCI_DP ||
151 DevicePathNodeLength(Node.DevPath) != sizeof(PCI_DEVICE_PATH)) {
152 return EFI_UNSUPPORTED;
153 }
154 }
155 }
156
157 //
158 // Check if Pci Root Bridge IO protocol is installed by platform
159 //
160 Status = gBS->OpenProtocol (
161 Controller,
162 &gEfiPciRootBridgeIoProtocolGuid,
163 (VOID **) &PciRootBridgeIo,
164 This->DriverBindingHandle,
165 Controller,
166 EFI_OPEN_PROTOCOL_BY_DRIVER
167 );
168 if (Status == EFI_ALREADY_STARTED) {
169 return EFI_SUCCESS;
170 }
171
172 if (EFI_ERROR (Status)) {
173 return Status;
174 }
175
176 //
177 // Close the I/O Abstraction(s) used to perform the supported test
178 //
179 gBS->CloseProtocol (
180 Controller,
181 &gEfiPciRootBridgeIoProtocolGuid,
182 This->DriverBindingHandle,
183 Controller
184 );
185
186 //
187 // Open the EFI Device Path protocol needed to perform the supported test
188 //
189 Status = gBS->OpenProtocol (
190 Controller,
191 &gEfiDevicePathProtocolGuid,
192 (VOID **) &ParentDevicePath,
193 This->DriverBindingHandle,
194 Controller,
195 EFI_OPEN_PROTOCOL_BY_DRIVER
196 );
197 if (Status == EFI_ALREADY_STARTED) {
198 return EFI_SUCCESS;
199 }
200
201 if (EFI_ERROR (Status)) {
202 return Status;
203 }
204
205 //
206 // Close protocol, don't use device path protocol in the Support() function
207 //
208 gBS->CloseProtocol (
209 Controller,
210 &gEfiDevicePathProtocolGuid,
211 This->DriverBindingHandle,
212 Controller
213 );
214
215 return EFI_SUCCESS;
216 }
217
218 /**
219 Start this driver on ControllerHandle and enumerate Pci bus and start
220 all device under PCI bus.
221
222 @param This Protocol instance pointer.
223 @param Controller Handle of device to bind driver to.
224 @param RemainingDevicePath Optional parameter use to pick a specific child.
225 device to start.
226
227 @retval EFI_SUCCESS This driver is added to ControllerHandle.
228 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
229 @retval other This driver does not support this device.
230
231 **/
232 EFI_STATUS
233 EFIAPI
234 PciBusDriverBindingStart (
235 IN EFI_DRIVER_BINDING_PROTOCOL *This,
236 IN EFI_HANDLE Controller,
237 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
238 )
239 {
240 EFI_STATUS Status;
241
242 //
243 // Check RemainingDevicePath validation
244 //
245 if (RemainingDevicePath != NULL) {
246 //
247 // Check if RemainingDevicePath is the End of Device Path Node,
248 // if yes, return EFI_SUCCESS
249 //
250 if (IsDevicePathEnd (RemainingDevicePath)) {
251 return EFI_SUCCESS;
252 }
253 }
254
255 Status = gBS->LocateProtocol (
256 &gEfiIncompatiblePciDeviceSupportProtocolGuid,
257 NULL,
258 (VOID **) &gEfiIncompatiblePciDeviceSupport
259 );
260
261 //
262 // If PCI Platform protocol is available, get it now.
263 // If the platform implements this, it must be installed before BDS phase
264 //
265 gPciPlatformProtocol = NULL;
266 gBS->LocateProtocol (
267 &gEfiPciPlatformProtocolGuid,
268 NULL,
269 (VOID **) &gPciPlatformProtocol
270 );
271
272 gFullEnumeration = (BOOLEAN) ((SearchHostBridgeHandle (Controller) ? FALSE : TRUE));
273
274 //
275 // Enumerate the entire host bridge
276 // After enumeration, a database that records all the device information will be created
277 //
278 //
279 Status = PciEnumerator (Controller);
280
281 if (EFI_ERROR (Status)) {
282 return Status;
283 }
284
285 //
286 // Start all the devices under the entire host bridge.
287 //
288 StartPciDevices (Controller);
289
290 return EFI_SUCCESS;
291 }
292
293 /**
294 Stop this driver on ControllerHandle. Support stoping any child handles
295 created by this driver.
296
297 @param This Protocol instance pointer.
298 @param Controller Handle of device to stop driver on.
299 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
300 children is zero stop the entire bus driver.
301 @param ChildHandleBuffer List of Child Handles to Stop.
302
303 @retval EFI_SUCCESS This driver is removed ControllerHandle.
304 @retval other This driver was not removed from this device.
305
306 **/
307 EFI_STATUS
308 EFIAPI
309 PciBusDriverBindingStop (
310 IN EFI_DRIVER_BINDING_PROTOCOL *This,
311 IN EFI_HANDLE Controller,
312 IN UINTN NumberOfChildren,
313 IN EFI_HANDLE *ChildHandleBuffer
314 )
315 {
316 EFI_STATUS Status;
317 UINTN Index;
318 BOOLEAN AllChildrenStopped;
319
320 if (NumberOfChildren == 0) {
321 //
322 // Close the bus driver
323 //
324 gBS->CloseProtocol (
325 Controller,
326 &gEfiDevicePathProtocolGuid,
327 This->DriverBindingHandle,
328 Controller
329 );
330 gBS->CloseProtocol (
331 Controller,
332 &gEfiPciRootBridgeIoProtocolGuid,
333 This->DriverBindingHandle,
334 Controller
335 );
336
337 DestroyRootBridgeByHandle (
338 Controller
339 );
340
341 return EFI_SUCCESS;
342 }
343
344 //
345 // Stop all the children
346 //
347
348 AllChildrenStopped = TRUE;
349
350 for (Index = 0; Index < NumberOfChildren; Index++) {
351
352 //
353 // De register all the pci device
354 //
355 Status = DeRegisterPciDevice (Controller, ChildHandleBuffer[Index]);
356
357 if (EFI_ERROR (Status)) {
358 AllChildrenStopped = FALSE;
359 }
360 }
361
362 if (!AllChildrenStopped) {
363 return EFI_DEVICE_ERROR;
364 }
365
366 return EFI_SUCCESS;
367 }
368