]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.c
Update the copyright notice format
[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 - 2009, 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
244 //
245 // Check RemainingDevicePath validation
246 //
247 if (RemainingDevicePath != NULL) {
248 //
249 // Check if RemainingDevicePath is the End of Device Path Node,
250 // if yes, return EFI_SUCCESS
251 //
252 if (IsDevicePathEnd (RemainingDevicePath)) {
253 return EFI_SUCCESS;
254 }
255 }
256
257 Status = gBS->LocateProtocol (
258 &gEfiIncompatiblePciDeviceSupportProtocolGuid,
259 NULL,
260 (VOID **) &gEfiIncompatiblePciDeviceSupport
261 );
262
263 //
264 // If PCI Platform protocol is available, get it now.
265 // If the platform implements this, it must be installed before BDS phase
266 //
267 gPciPlatformProtocol = NULL;
268 gBS->LocateProtocol (
269 &gEfiPciPlatformProtocolGuid,
270 NULL,
271 (VOID **) &gPciPlatformProtocol
272 );
273
274 //
275 // If PCI Platform protocol doesn't exist, try to Pci Override Protocol.
276 //
277 if (gPciPlatformProtocol == NULL) {
278 gPciOverrideProtocol = NULL;
279 gBS->LocateProtocol (
280 &gEfiPciOverrideProtocolGuid,
281 NULL,
282 (VOID **) &gPciOverrideProtocol
283 );
284 }
285
286 gFullEnumeration = (BOOLEAN) ((SearchHostBridgeHandle (Controller) ? FALSE : TRUE));
287
288 //
289 // Enumerate the entire host bridge
290 // After enumeration, a database that records all the device information will be created
291 //
292 //
293 Status = PciEnumerator (Controller);
294
295 if (EFI_ERROR (Status)) {
296 return Status;
297 }
298
299 //
300 // Start all the devices under the entire host bridge.
301 //
302 StartPciDevices (Controller);
303
304 return EFI_SUCCESS;
305 }
306
307 /**
308 Stop this driver on ControllerHandle. Support stoping any child handles
309 created by this driver.
310
311 @param This Protocol instance pointer.
312 @param Controller Handle of device to stop driver on.
313 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
314 children is zero stop the entire bus driver.
315 @param ChildHandleBuffer List of Child Handles to Stop.
316
317 @retval EFI_SUCCESS This driver is removed ControllerHandle.
318 @retval other This driver was not removed from this device.
319
320 **/
321 EFI_STATUS
322 EFIAPI
323 PciBusDriverBindingStop (
324 IN EFI_DRIVER_BINDING_PROTOCOL *This,
325 IN EFI_HANDLE Controller,
326 IN UINTN NumberOfChildren,
327 IN EFI_HANDLE *ChildHandleBuffer
328 )
329 {
330 EFI_STATUS Status;
331 UINTN Index;
332 BOOLEAN AllChildrenStopped;
333
334 if (NumberOfChildren == 0) {
335 //
336 // Close the bus driver
337 //
338 gBS->CloseProtocol (
339 Controller,
340 &gEfiDevicePathProtocolGuid,
341 This->DriverBindingHandle,
342 Controller
343 );
344 gBS->CloseProtocol (
345 Controller,
346 &gEfiPciRootBridgeIoProtocolGuid,
347 This->DriverBindingHandle,
348 Controller
349 );
350
351 DestroyRootBridgeByHandle (
352 Controller
353 );
354
355 return EFI_SUCCESS;
356 }
357
358 //
359 // Stop all the children
360 //
361
362 AllChildrenStopped = TRUE;
363
364 for (Index = 0; Index < NumberOfChildren; Index++) {
365
366 //
367 // De register all the pci device
368 //
369 Status = DeRegisterPciDevice (Controller, ChildHandleBuffer[Index]);
370
371 if (EFI_ERROR (Status)) {
372 AllChildrenStopped = FALSE;
373 }
374 }
375
376 if (!AllChildrenStopped) {
377 return EFI_DEVICE_ERROR;
378 }
379
380 return EFI_SUCCESS;
381 }
382