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