]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/PciBusDxe/PciEnumerator.c
MdeModulePkg PciBusDxe: The PCI Bus Driver is updated to support multiple PCI bus...
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciBusDxe / PciEnumerator.c
1 /** @file
2 PCI eunmeration implementation on entire PCI bus system for PCI Bus module.
3
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PciBus.h"
16
17 /**
18 This routine is used to enumerate entire pci bus system
19 in a given platform.
20
21 @param Controller Parent controller handle.
22
23 @retval EFI_SUCCESS PCI enumeration finished successfully.
24 @retval other Some error occurred when enumerating the pci bus system.
25
26 **/
27 EFI_STATUS
28 PciEnumerator (
29 IN EFI_HANDLE Controller
30 )
31 {
32 EFI_HANDLE Handle;
33 EFI_HANDLE HostBridgeHandle;
34 EFI_STATUS Status;
35 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *PciResAlloc;
36 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
37
38 //
39 // If PCI bus has already done the full enumeration, never do it again
40 //
41 if (!gFullEnumeration) {
42 return PciEnumeratorLight (Controller);
43 }
44
45 //
46 // Get the rootbridge Io protocol to find the host bridge handle
47 //
48 Status = gBS->OpenProtocol (
49 Controller,
50 &gEfiPciRootBridgeIoProtocolGuid,
51 (VOID **) &PciRootBridgeIo,
52 gPciBusDriverBinding.DriverBindingHandle,
53 Controller,
54 EFI_OPEN_PROTOCOL_GET_PROTOCOL
55 );
56
57 if (EFI_ERROR (Status)) {
58 return Status;
59 }
60
61 //
62 // Get the host bridge handle
63 //
64 HostBridgeHandle = PciRootBridgeIo->ParentHandle;
65
66 //
67 // Get the pci host bridge resource allocation protocol
68 //
69 Status = gBS->OpenProtocol (
70 HostBridgeHandle,
71 &gEfiPciHostBridgeResourceAllocationProtocolGuid,
72 (VOID **) &PciResAlloc,
73 gPciBusDriverBinding.DriverBindingHandle,
74 Controller,
75 EFI_OPEN_PROTOCOL_GET_PROTOCOL
76 );
77
78 if (EFI_ERROR (Status)) {
79 return Status;
80 }
81
82 //
83 // Notify the pci bus enumeration is about to begin
84 //
85 NotifyPhase (PciResAlloc, EfiPciHostBridgeBeginEnumeration);
86
87 //
88 // Start the bus allocation phase
89 //
90 Status = PciHostBridgeEnumerator (PciResAlloc);
91
92 if (EFI_ERROR (Status)) {
93 return Status;
94 }
95
96 //
97 // Submit the resource request
98 //
99 Status = PciHostBridgeResourceAllocator (PciResAlloc);
100
101 if (EFI_ERROR (Status)) {
102 return Status;
103 }
104
105 //
106 // Notify the pci bus enumeration is about to complete
107 //
108 NotifyPhase (PciResAlloc, EfiPciHostBridgeEndEnumeration);
109
110 //
111 // Process P2C
112 //
113 Status = PciHostBridgeP2CProcess (PciResAlloc);
114
115 if (EFI_ERROR (Status)) {
116 return Status;
117 }
118
119 //
120 // Process attributes for devices on this host bridge
121 //
122 Status = PciHostBridgeDeviceAttribute (PciResAlloc);
123 if (EFI_ERROR (Status)) {
124 return Status;
125 }
126
127 gFullEnumeration = FALSE;
128
129 Handle = NULL;
130 Status = gBS->InstallProtocolInterface (
131 &Handle,
132 &gEfiPciEnumerationCompleteProtocolGuid,
133 EFI_NATIVE_INTERFACE,
134 NULL
135 );
136 if (EFI_ERROR (Status)) {
137 return Status;
138 }
139
140 return EFI_SUCCESS;
141 }
142
143 /**
144 Enumerate PCI root bridge.
145
146 @param PciResAlloc Pointer to protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
147 @param RootBridgeDev Instance of root bridge device.
148
149 @retval EFI_SUCCESS Successfully enumerated root bridge.
150 @retval other Failed to enumerate root bridge.
151
152 **/
153 EFI_STATUS
154 PciRootBridgeEnumerator (
155 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *PciResAlloc,
156 IN PCI_IO_DEVICE *RootBridgeDev
157 )
158 {
159 EFI_STATUS Status;
160 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Configuration;
161 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Configuration1;
162 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Configuration2;
163 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Configuration3;
164 UINT8 SubBusNumber;
165 UINT8 StartBusNumber;
166 UINT8 PaddedBusRange;
167 EFI_HANDLE RootBridgeHandle;
168 UINT8 Desc;
169 UINT64 AddrLen;
170 UINT64 AddrRangeMin;
171
172 SubBusNumber = 0;
173 StartBusNumber = 0;
174 PaddedBusRange = 0;
175
176 //
177 // Get the root bridge handle
178 //
179 RootBridgeHandle = RootBridgeDev->Handle;
180
181 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
182 EFI_PROGRESS_CODE,
183 EFI_IO_BUS_PCI | EFI_IOB_PCI_BUS_ENUM,
184 RootBridgeDev->DevicePath
185 );
186
187 //
188 // Get the Bus information
189 //
190 Status = PciResAlloc->StartBusEnumeration (
191 PciResAlloc,
192 RootBridgeHandle,
193 (VOID **) &Configuration
194 );
195
196 if (EFI_ERROR (Status)) {
197 return Status;
198 }
199
200 if (Configuration == NULL || Configuration->Desc == ACPI_END_TAG_DESCRIPTOR) {
201 return EFI_INVALID_PARAMETER;
202 }
203 RootBridgeDev->BusNumberRanges = Configuration;
204
205 //
206 // Sort the descriptors in ascending order
207 //
208 for (Configuration1 = Configuration; Configuration1->Desc != ACPI_END_TAG_DESCRIPTOR; Configuration1++) {
209 Configuration2 = Configuration1;
210 for (Configuration3 = Configuration1 + 1; Configuration3->Desc != ACPI_END_TAG_DESCRIPTOR; Configuration3++) {
211 if (Configuration2->AddrRangeMin > Configuration3->AddrRangeMin) {
212 Configuration2 = Configuration3;
213 }
214 }
215 //
216 // All other fields other than AddrRangeMin and AddrLen are ignored in a descriptor,
217 // so only need to swap these two fields.
218 //
219 if (Configuration2 != Configuration1) {
220 AddrRangeMin = Configuration1->AddrRangeMin;
221 Configuration1->AddrRangeMin = Configuration2->AddrRangeMin;
222 Configuration2->AddrRangeMin = AddrRangeMin;
223
224 AddrLen = Configuration1->AddrLen;
225 Configuration1->AddrLen = Configuration2->AddrLen;
226 Configuration2->AddrLen = AddrLen;
227 }
228 }
229
230 //
231 // Get the bus number to start with
232 //
233 StartBusNumber = (UINT8) (Configuration->AddrRangeMin);
234
235 //
236 // Initialize the subordinate bus number
237 //
238 SubBusNumber = StartBusNumber;
239
240 //
241 // Reset all assigned PCI bus number
242 //
243 ResetAllPpbBusNumber (
244 RootBridgeDev,
245 StartBusNumber
246 );
247
248 //
249 // Assign bus number
250 //
251 Status = PciScanBus (
252 RootBridgeDev,
253 StartBusNumber,
254 &SubBusNumber,
255 &PaddedBusRange
256 );
257
258 if (EFI_ERROR (Status)) {
259 return Status;
260 }
261
262
263 //
264 // Assign max bus number scanned
265 //
266
267 Status = PciAllocateBusNumber (RootBridgeDev, SubBusNumber, PaddedBusRange, &SubBusNumber);
268 if (EFI_ERROR (Status)) {
269 return Status;
270 }
271
272 //
273 // Find the bus range which contains the higest bus number, then returns the number of buses
274 // that should be decoded.
275 //
276 while (Configuration->AddrRangeMin + Configuration->AddrLen - 1 < SubBusNumber) {
277 Configuration++;
278 }
279 AddrLen = Configuration->AddrLen;
280 Configuration->AddrLen = SubBusNumber - Configuration->AddrRangeMin + 1;
281
282 //
283 // Save the Desc field of the next descriptor. Mark the next descriptor as an END descriptor.
284 //
285 Configuration++;
286 Desc = Configuration->Desc;
287 Configuration->Desc = ACPI_END_TAG_DESCRIPTOR;
288
289 //
290 // Set bus number
291 //
292 Status = PciResAlloc->SetBusNumbers (
293 PciResAlloc,
294 RootBridgeHandle,
295 RootBridgeDev->BusNumberRanges
296 );
297
298 //
299 // Restore changed fields
300 //
301 Configuration->Desc = Desc;
302 (Configuration - 1)->AddrLen = AddrLen;
303
304 return Status;
305 }
306
307 /**
308 This routine is used to process all PCI devices' Option Rom
309 on a certain root bridge.
310
311 @param Bridge Given parent's root bridge.
312 @param RomBase Base address of ROM driver loaded from.
313 @param MaxLength Maximum rom size.
314
315 **/
316 VOID
317 ProcessOptionRom (
318 IN PCI_IO_DEVICE *Bridge,
319 IN UINT64 RomBase,
320 IN UINT64 MaxLength
321 )
322 {
323 LIST_ENTRY *CurrentLink;
324 PCI_IO_DEVICE *Temp;
325
326 //
327 // Go through bridges to reach all devices
328 //
329 CurrentLink = Bridge->ChildList.ForwardLink;
330 while (CurrentLink != NULL && CurrentLink != &Bridge->ChildList) {
331 Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);
332 if (!IsListEmpty (&Temp->ChildList)) {
333
334 //
335 // Go further to process the option rom under this bridge
336 //
337 ProcessOptionRom (Temp, RomBase, MaxLength);
338 }
339
340 if (Temp->RomSize != 0 && Temp->RomSize <= MaxLength) {
341
342 //
343 // Load and process the option rom
344 //
345 LoadOpRomImage (Temp, RomBase);
346 }
347
348 CurrentLink = CurrentLink->ForwardLink;
349 }
350 }
351
352 /**
353 This routine is used to assign bus number to the given PCI bus system
354
355 @param Bridge Parent root bridge instance.
356 @param StartBusNumber Number of beginning.
357 @param SubBusNumber The number of sub bus.
358
359 @retval EFI_SUCCESS Successfully assigned bus number.
360 @retval EFI_DEVICE_ERROR Failed to assign bus number.
361
362 **/
363 EFI_STATUS
364 PciAssignBusNumber (
365 IN PCI_IO_DEVICE *Bridge,
366 IN UINT8 StartBusNumber,
367 OUT UINT8 *SubBusNumber
368 )
369 {
370 EFI_STATUS Status;
371 PCI_TYPE00 Pci;
372 UINT8 Device;
373 UINT8 Func;
374 UINT64 Address;
375 UINTN SecondBus;
376 UINT16 Register;
377 UINT8 Register8;
378 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
379
380 PciRootBridgeIo = Bridge->PciRootBridgeIo;
381
382 SecondBus = 0;
383 Register = 0;
384
385 *SubBusNumber = StartBusNumber;
386
387 //
388 // First check to see whether the parent is ppb
389 //
390 for (Device = 0; Device <= PCI_MAX_DEVICE; Device++) {
391 for (Func = 0; Func <= PCI_MAX_FUNC; Func++) {
392
393 //
394 // Check to see whether a pci device is present
395 //
396 Status = PciDevicePresent (
397 PciRootBridgeIo,
398 &Pci,
399 StartBusNumber,
400 Device,
401 Func
402 );
403
404 if (!EFI_ERROR (Status) &&
405 (IS_PCI_BRIDGE (&Pci) || IS_CARDBUS_BRIDGE (&Pci))) {
406
407 //
408 // Reserved one bus for cardbus bridge
409 //
410 Status = PciAllocateBusNumber (Bridge, *SubBusNumber, 1, SubBusNumber);
411 if (EFI_ERROR (Status)) {
412 return Status;
413 }
414 SecondBus = *SubBusNumber;
415
416 Register = (UINT16) ((SecondBus << 8) | (UINT16) StartBusNumber);
417
418 Address = EFI_PCI_ADDRESS (StartBusNumber, Device, Func, 0x18);
419
420 Status = PciRootBridgeIo->Pci.Write (
421 PciRootBridgeIo,
422 EfiPciWidthUint16,
423 Address,
424 1,
425 &Register
426 );
427
428 //
429 // Initialize SubBusNumber to SecondBus
430 //
431 Address = EFI_PCI_ADDRESS (StartBusNumber, Device, Func, 0x1A);
432 Status = PciRootBridgeIo->Pci.Write (
433 PciRootBridgeIo,
434 EfiPciWidthUint8,
435 Address,
436 1,
437 SubBusNumber
438 );
439 //
440 // If it is PPB, resursively search down this bridge
441 //
442 if (IS_PCI_BRIDGE (&Pci)) {
443
444 Register8 = 0xFF;
445 Status = PciRootBridgeIo->Pci.Write (
446 PciRootBridgeIo,
447 EfiPciWidthUint8,
448 Address,
449 1,
450 &Register8
451 );
452
453 Status = PciAssignBusNumber (
454 Bridge,
455 (UINT8) (SecondBus),
456 SubBusNumber
457 );
458
459 if (EFI_ERROR (Status)) {
460 return EFI_DEVICE_ERROR;
461 }
462 }
463
464 //
465 // Set the current maximum bus number under the PPB
466 //
467 Address = EFI_PCI_ADDRESS (StartBusNumber, Device, Func, 0x1A);
468
469 Status = PciRootBridgeIo->Pci.Write (
470 PciRootBridgeIo,
471 EfiPciWidthUint8,
472 Address,
473 1,
474 SubBusNumber
475 );
476
477 }
478
479 if (Func == 0 && !IS_PCI_MULTI_FUNC (&Pci)) {
480
481 //
482 // Skip sub functions, this is not a multi function device
483 //
484 Func = PCI_MAX_FUNC;
485 }
486 }
487 }
488
489 return EFI_SUCCESS;
490 }
491
492 /**
493 This routine is used to determine the root bridge attribute by interfacing
494 the host bridge resource allocation protocol.
495
496 @param PciResAlloc Protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
497 @param RootBridgeDev Root bridge instance
498
499 @retval EFI_SUCCESS Successfully got root bridge's attribute.
500 @retval other Failed to get attribute.
501
502 **/
503 EFI_STATUS
504 DetermineRootBridgeAttributes (
505 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *PciResAlloc,
506 IN PCI_IO_DEVICE *RootBridgeDev
507 )
508 {
509 UINT64 Attributes;
510 EFI_STATUS Status;
511 EFI_HANDLE RootBridgeHandle;
512
513 Attributes = 0;
514 RootBridgeHandle = RootBridgeDev->Handle;
515
516 //
517 // Get root bridge attribute by calling into pci host bridge resource allocation protocol
518 //
519 Status = PciResAlloc->GetAllocAttributes (
520 PciResAlloc,
521 RootBridgeHandle,
522 &Attributes
523 );
524
525 if (EFI_ERROR (Status)) {
526 return Status;
527 }
528
529 //
530 // Here is the point where PCI bus driver calls HOST bridge allocation protocol
531 // Currently we hardcoded for ea815
532 //
533 if ((Attributes & EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM) != 0) {
534 RootBridgeDev->Decodes |= EFI_BRIDGE_PMEM_MEM_COMBINE_SUPPORTED;
535 }
536
537 if ((Attributes & EFI_PCI_HOST_BRIDGE_MEM64_DECODE) != 0) {
538 RootBridgeDev->Decodes |= EFI_BRIDGE_MEM64_DECODE_SUPPORTED;
539 RootBridgeDev->Decodes |= EFI_BRIDGE_PMEM64_DECODE_SUPPORTED;
540 }
541
542 RootBridgeDev->Decodes |= EFI_BRIDGE_MEM32_DECODE_SUPPORTED;
543 RootBridgeDev->Decodes |= EFI_BRIDGE_PMEM32_DECODE_SUPPORTED;
544 RootBridgeDev->Decodes |= EFI_BRIDGE_IO16_DECODE_SUPPORTED;
545
546 return EFI_SUCCESS;
547 }
548
549 /**
550 Get Max Option Rom size on specified bridge.
551
552 @param Bridge Given bridge device instance.
553
554 @return Max size of option rom needed.
555
556 **/
557 UINT64
558 GetMaxOptionRomSize (
559 IN PCI_IO_DEVICE *Bridge
560 )
561 {
562 LIST_ENTRY *CurrentLink;
563 PCI_IO_DEVICE *Temp;
564 UINT64 MaxOptionRomSize;
565 UINT64 TempOptionRomSize;
566
567 MaxOptionRomSize = 0;
568
569 //
570 // Go through bridges to reach all devices
571 //
572 CurrentLink = Bridge->ChildList.ForwardLink;
573 while (CurrentLink != NULL && CurrentLink != &Bridge->ChildList) {
574 Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);
575 if (!IsListEmpty (&Temp->ChildList)) {
576
577 //
578 // Get max option rom size under this bridge
579 //
580 TempOptionRomSize = GetMaxOptionRomSize (Temp);
581
582 //
583 // Compare with the option rom size of the bridge
584 // Get the larger one
585 //
586 if (Temp->RomSize > TempOptionRomSize) {
587 TempOptionRomSize = Temp->RomSize;
588 }
589
590 } else {
591
592 //
593 // For devices get the rom size directly
594 //
595 TempOptionRomSize = Temp->RomSize;
596 }
597
598 //
599 // Get the largest rom size on this bridge
600 //
601 if (TempOptionRomSize > MaxOptionRomSize) {
602 MaxOptionRomSize = TempOptionRomSize;
603 }
604
605 CurrentLink = CurrentLink->ForwardLink;
606 }
607
608 return MaxOptionRomSize;
609 }
610
611 /**
612 Process attributes of devices on this host bridge
613
614 @param PciResAlloc Protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
615
616 @retval EFI_SUCCESS Successfully process attribute.
617 @retval EFI_NOT_FOUND Can not find the specific root bridge device.
618 @retval other Failed to determine the root bridge device's attribute.
619
620 **/
621 EFI_STATUS
622 PciHostBridgeDeviceAttribute (
623 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *PciResAlloc
624 )
625 {
626 EFI_HANDLE RootBridgeHandle;
627 PCI_IO_DEVICE *RootBridgeDev;
628 EFI_STATUS Status;
629
630 RootBridgeHandle = NULL;
631
632 while (PciResAlloc->GetNextRootBridge (PciResAlloc, &RootBridgeHandle) == EFI_SUCCESS) {
633
634 //
635 // Get RootBridg Device by handle
636 //
637 RootBridgeDev = GetRootBridgeByHandle (RootBridgeHandle);
638
639 if (RootBridgeDev == NULL) {
640 return EFI_NOT_FOUND;
641 }
642
643 //
644 // Set the attributes for devcies behind the Root Bridge
645 //
646 Status = DetermineDeviceAttribute (RootBridgeDev);
647 if (EFI_ERROR (Status)) {
648 return Status;
649 }
650
651 }
652
653 return EFI_SUCCESS;
654 }
655
656 /**
657 Get resource allocation status from the ACPI resource descriptor.
658
659 @param AcpiConfig Point to Acpi configuration table.
660 @param IoResStatus Return the status of I/O resource.
661 @param Mem32ResStatus Return the status of 32-bit Memory resource.
662 @param PMem32ResStatus Return the status of 32-bit Prefetchable Memory resource.
663 @param Mem64ResStatus Return the status of 64-bit Memory resource.
664 @param PMem64ResStatus Return the status of 64-bit Prefetchable Memory resource.
665
666 **/
667 VOID
668 GetResourceAllocationStatus (
669 VOID *AcpiConfig,
670 OUT UINT64 *IoResStatus,
671 OUT UINT64 *Mem32ResStatus,
672 OUT UINT64 *PMem32ResStatus,
673 OUT UINT64 *Mem64ResStatus,
674 OUT UINT64 *PMem64ResStatus
675 )
676 {
677 UINT8 *Temp;
678 UINT64 ResStatus;
679 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *ACPIAddressDesc;
680
681 Temp = (UINT8 *) AcpiConfig;
682
683 while (*Temp == ACPI_ADDRESS_SPACE_DESCRIPTOR) {
684
685 ACPIAddressDesc = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Temp;
686 ResStatus = ACPIAddressDesc->AddrTranslationOffset;
687
688 switch (ACPIAddressDesc->ResType) {
689 case 0:
690 if (ACPIAddressDesc->AddrSpaceGranularity == 32) {
691 if (ACPIAddressDesc->SpecificFlag == 0x06) {
692 //
693 // Pmem32
694 //
695 *PMem32ResStatus = ResStatus;
696 } else {
697 //
698 // Mem32
699 //
700 *Mem32ResStatus = ResStatus;
701 }
702 }
703
704 if (ACPIAddressDesc->AddrSpaceGranularity == 64) {
705 if (ACPIAddressDesc->SpecificFlag == 0x06) {
706 //
707 // PMem64
708 //
709 *PMem64ResStatus = ResStatus;
710 } else {
711 //
712 // Mem64
713 //
714 *Mem64ResStatus = ResStatus;
715 }
716 }
717
718 break;
719
720 case 1:
721 //
722 // Io
723 //
724 *IoResStatus = ResStatus;
725 break;
726
727 default:
728 break;
729 }
730
731 Temp += sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR);
732 }
733 }
734
735 /**
736 Remove a PCI device from device pool and mark its bar.
737
738 @param PciDevice Instance of Pci device.
739
740 @retval EFI_SUCCESS Successfully remove the PCI device.
741 @retval EFI_ABORTED Pci device is a root bridge or a PCI-PCI bridge.
742
743 **/
744 EFI_STATUS
745 RejectPciDevice (
746 IN PCI_IO_DEVICE *PciDevice
747 )
748 {
749 PCI_IO_DEVICE *Bridge;
750 PCI_IO_DEVICE *Temp;
751 LIST_ENTRY *CurrentLink;
752
753 //
754 // Remove the padding resource from a bridge
755 //
756 if ( IS_PCI_BRIDGE(&PciDevice->Pci) &&
757 PciDevice->ResourcePaddingDescriptors != NULL ) {
758 FreePool (PciDevice->ResourcePaddingDescriptors);
759 PciDevice->ResourcePaddingDescriptors = NULL;
760 return EFI_SUCCESS;
761 }
762
763 //
764 // Skip RB and PPB
765 //
766 if (IS_PCI_BRIDGE (&PciDevice->Pci) || (PciDevice->Parent == NULL)) {
767 return EFI_ABORTED;
768 }
769
770 if (IS_CARDBUS_BRIDGE (&PciDevice->Pci)) {
771 //
772 // Get the root bridge device
773 //
774 Bridge = PciDevice;
775 while (Bridge->Parent != NULL) {
776 Bridge = Bridge->Parent;
777 }
778
779 RemoveAllPciDeviceOnBridge (Bridge->Handle, PciDevice);
780
781 //
782 // Mark its bar
783 //
784 InitializeP2C (PciDevice);
785 }
786
787 //
788 // Remove the device
789 //
790 Bridge = PciDevice->Parent;
791 CurrentLink = Bridge->ChildList.ForwardLink;
792 while (CurrentLink != NULL && CurrentLink != &Bridge->ChildList) {
793 Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);
794 if (Temp == PciDevice) {
795 InitializePciDevice (Temp);
796 RemoveEntryList (CurrentLink);
797 FreePciDevice (Temp);
798 return EFI_SUCCESS;
799 }
800
801 CurrentLink = CurrentLink->ForwardLink;
802 }
803
804 return EFI_ABORTED;
805 }
806
807 /**
808 Determine whethter a PCI device can be rejected.
809
810 @param PciResNode Pointer to Pci resource node instance.
811
812 @retval TRUE The PCI device can be rejected.
813 @retval TRUE The PCI device cannot be rejected.
814
815 **/
816 BOOLEAN
817 IsRejectiveDevice (
818 IN PCI_RESOURCE_NODE *PciResNode
819 )
820 {
821 PCI_IO_DEVICE *Temp;
822
823 Temp = PciResNode->PciDev;
824
825 //
826 // Ensure the device is present
827 //
828 if (Temp == NULL) {
829 return FALSE;
830 }
831
832 //
833 // PPB and RB should go ahead
834 //
835 if (IS_PCI_BRIDGE (&Temp->Pci) || (Temp->Parent == NULL)) {
836 return TRUE;
837 }
838
839 //
840 // Skip device on Bus0
841 //
842 if ((Temp->Parent != NULL) && (Temp->BusNumber == 0)) {
843 return FALSE;
844 }
845
846 //
847 // Skip VGA
848 //
849 if (IS_PCI_VGA (&Temp->Pci)) {
850 return FALSE;
851 }
852
853 return TRUE;
854 }
855
856 /**
857 Compare two resource nodes and get the larger resource consumer.
858
859 @param PciResNode1 resource node 1 want to be compared
860 @param PciResNode2 resource node 2 want to be compared
861
862 @return Larger resource node.
863
864 **/
865 PCI_RESOURCE_NODE *
866 GetLargerConsumerDevice (
867 IN PCI_RESOURCE_NODE *PciResNode1,
868 IN PCI_RESOURCE_NODE *PciResNode2
869 )
870 {
871 if (PciResNode2 == NULL) {
872 return PciResNode1;
873 }
874
875 if ((IS_PCI_BRIDGE(&(PciResNode2->PciDev->Pci)) || (PciResNode2->PciDev->Parent == NULL)) \
876 && (PciResNode2->ResourceUsage != PciResUsagePadding) )
877 {
878 return PciResNode1;
879 }
880
881 if (PciResNode1 == NULL) {
882 return PciResNode2;
883 }
884
885 if ((PciResNode1->Length) > (PciResNode2->Length)) {
886 return PciResNode1;
887 }
888
889 return PciResNode2;
890 }
891
892
893 /**
894 Get the max resource consumer in the host resource pool.
895
896 @param ResPool Pointer to resource pool node.
897
898 @return The max resource consumer in the host resource pool.
899
900 **/
901 PCI_RESOURCE_NODE *
902 GetMaxResourceConsumerDevice (
903 IN PCI_RESOURCE_NODE *ResPool
904 )
905 {
906 PCI_RESOURCE_NODE *Temp;
907 LIST_ENTRY *CurrentLink;
908 PCI_RESOURCE_NODE *PciResNode;
909 PCI_RESOURCE_NODE *PPBResNode;
910
911 PciResNode = NULL;
912
913 CurrentLink = ResPool->ChildList.ForwardLink;
914 while (CurrentLink != NULL && CurrentLink != &ResPool->ChildList) {
915
916 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
917
918 if (!IsRejectiveDevice (Temp)) {
919 CurrentLink = CurrentLink->ForwardLink;
920 continue;
921 }
922
923 if ((IS_PCI_BRIDGE (&(Temp->PciDev->Pci)) || (Temp->PciDev->Parent == NULL)) \
924 && (Temp->ResourceUsage != PciResUsagePadding))
925 {
926 PPBResNode = GetMaxResourceConsumerDevice (Temp);
927 PciResNode = GetLargerConsumerDevice (PciResNode, PPBResNode);
928 } else {
929 PciResNode = GetLargerConsumerDevice (PciResNode, Temp);
930 }
931
932 CurrentLink = CurrentLink->ForwardLink;
933 }
934
935 return PciResNode;
936 }
937
938 /**
939 Adjust host bridge allocation so as to reduce resource requirement
940
941 @param IoPool Pointer to instance of I/O resource Node.
942 @param Mem32Pool Pointer to instance of 32-bit memory resource Node.
943 @param PMem32Pool Pointer to instance of 32-bit Prefetchable memory resource node.
944 @param Mem64Pool Pointer to instance of 64-bit memory resource node.
945 @param PMem64Pool Pointer to instance of 64-bit Prefetchable memory resource node.
946 @param IoResStatus Status of I/O resource Node.
947 @param Mem32ResStatus Status of 32-bit memory resource Node.
948 @param PMem32ResStatus Status of 32-bit Prefetchable memory resource node.
949 @param Mem64ResStatus Status of 64-bit memory resource node.
950 @param PMem64ResStatus Status of 64-bit Prefetchable memory resource node.
951
952 @retval EFI_SUCCESS Successfully adjusted resoruce on host bridge.
953 @retval EFI_ABORTED Host bridge hasn't this resource type or no resource be adjusted.
954
955 **/
956 EFI_STATUS
957 PciHostBridgeAdjustAllocation (
958 IN PCI_RESOURCE_NODE *IoPool,
959 IN PCI_RESOURCE_NODE *Mem32Pool,
960 IN PCI_RESOURCE_NODE *PMem32Pool,
961 IN PCI_RESOURCE_NODE *Mem64Pool,
962 IN PCI_RESOURCE_NODE *PMem64Pool,
963 IN UINT64 IoResStatus,
964 IN UINT64 Mem32ResStatus,
965 IN UINT64 PMem32ResStatus,
966 IN UINT64 Mem64ResStatus,
967 IN UINT64 PMem64ResStatus
968 )
969 {
970 BOOLEAN AllocationAjusted;
971 PCI_RESOURCE_NODE *PciResNode;
972 PCI_RESOURCE_NODE *ResPool[5];
973 PCI_IO_DEVICE *RemovedPciDev[5];
974 UINT64 ResStatus[5];
975 UINTN RemovedPciDevNum;
976 UINTN DevIndex;
977 UINTN ResType;
978 EFI_STATUS Status;
979 EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD AllocFailExtendedData;
980
981 PciResNode = NULL;
982 ZeroMem (RemovedPciDev, 5 * sizeof (PCI_IO_DEVICE *));
983 RemovedPciDevNum = 0;
984
985 ResPool[0] = IoPool;
986 ResPool[1] = Mem32Pool;
987 ResPool[2] = PMem32Pool;
988 ResPool[3] = Mem64Pool;
989 ResPool[4] = PMem64Pool;
990
991 ResStatus[0] = IoResStatus;
992 ResStatus[1] = Mem32ResStatus;
993 ResStatus[2] = PMem32ResStatus;
994 ResStatus[3] = Mem64ResStatus;
995 ResStatus[4] = PMem64ResStatus;
996
997 AllocationAjusted = FALSE;
998
999 for (ResType = 0; ResType < 5; ResType++) {
1000
1001 if (ResStatus[ResType] == EFI_RESOURCE_SATISFIED) {
1002 continue;
1003 }
1004
1005 if (ResStatus[ResType] == EFI_RESOURCE_NOT_SATISFIED) {
1006 //
1007 // Host bridge hasn't this resource type
1008 //
1009 return EFI_ABORTED;
1010 }
1011
1012 //
1013 // Hostbridge hasn't enough resource
1014 //
1015 PciResNode = GetMaxResourceConsumerDevice (ResPool[ResType]);
1016 if (PciResNode == NULL) {
1017 continue;
1018 }
1019
1020 //
1021 // Check if the device has been removed before
1022 //
1023 for (DevIndex = 0; DevIndex < RemovedPciDevNum; DevIndex++) {
1024 if (PciResNode->PciDev == RemovedPciDev[DevIndex]) {
1025 break;
1026 }
1027 }
1028
1029 if (DevIndex != RemovedPciDevNum) {
1030 continue;
1031 }
1032
1033 //
1034 // Remove the device if it isn't in the array
1035 //
1036 Status = RejectPciDevice (PciResNode->PciDev);
1037 if (Status == EFI_SUCCESS) {
1038
1039 //
1040 // Raise the EFI_IOB_EC_RESOURCE_CONFLICT status code
1041 //
1042 //
1043 // Have no way to get ReqRes, AllocRes & Bar here
1044 //
1045 ZeroMem (&AllocFailExtendedData, sizeof (AllocFailExtendedData));
1046 AllocFailExtendedData.DevicePathSize = (UINT16) sizeof (EFI_DEVICE_PATH_PROTOCOL);
1047 AllocFailExtendedData.DevicePath = (UINT8 *) PciResNode->PciDev->DevicePath;
1048 AllocFailExtendedData.Bar = PciResNode->Bar;
1049
1050 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
1051 EFI_PROGRESS_CODE,
1052 EFI_IO_BUS_PCI | EFI_IOB_EC_RESOURCE_CONFLICT,
1053 (VOID *) &AllocFailExtendedData,
1054 sizeof (AllocFailExtendedData)
1055 );
1056
1057 //
1058 // Add it to the array and indicate at least a device has been rejected
1059 //
1060 RemovedPciDev[RemovedPciDevNum++] = PciResNode->PciDev;
1061 AllocationAjusted = TRUE;
1062 }
1063 }
1064 //
1065 // End for
1066 //
1067
1068 if (AllocationAjusted) {
1069 return EFI_SUCCESS;
1070 } else {
1071 return EFI_ABORTED;
1072 }
1073 }
1074
1075 /**
1076 Summary requests for all resource type, and contruct ACPI resource
1077 requestor instance.
1078
1079 @param Bridge detecting bridge
1080 @param IoNode Pointer to instance of I/O resource Node
1081 @param Mem32Node Pointer to instance of 32-bit memory resource Node
1082 @param PMem32Node Pointer to instance of 32-bit Pmemory resource node
1083 @param Mem64Node Pointer to instance of 64-bit memory resource node
1084 @param PMem64Node Pointer to instance of 64-bit Pmemory resource node
1085 @param Config Output buffer holding new constructed APCI resource requestor
1086
1087 @retval EFI_SUCCESS Successfully constructed ACPI resource.
1088 @retval EFI_OUT_OF_RESOURCES No memory availabe.
1089
1090 **/
1091 EFI_STATUS
1092 ConstructAcpiResourceRequestor (
1093 IN PCI_IO_DEVICE *Bridge,
1094 IN PCI_RESOURCE_NODE *IoNode,
1095 IN PCI_RESOURCE_NODE *Mem32Node,
1096 IN PCI_RESOURCE_NODE *PMem32Node,
1097 IN PCI_RESOURCE_NODE *Mem64Node,
1098 IN PCI_RESOURCE_NODE *PMem64Node,
1099 OUT VOID **Config
1100 )
1101 {
1102 UINT8 NumConfig;
1103 UINT8 Aperture;
1104 UINT8 *Configuration;
1105 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Ptr;
1106 EFI_ACPI_END_TAG_DESCRIPTOR *PtrEnd;
1107
1108 NumConfig = 0;
1109 Aperture = 0;
1110
1111 *Config = NULL;
1112
1113 //
1114 // if there is io request, add to the io aperture
1115 //
1116 if (ResourceRequestExisted (IoNode)) {
1117 NumConfig++;
1118 Aperture |= 0x01;
1119 }
1120
1121 //
1122 // if there is mem32 request, add to the mem32 aperture
1123 //
1124 if (ResourceRequestExisted (Mem32Node)) {
1125 NumConfig++;
1126 Aperture |= 0x02;
1127 }
1128
1129 //
1130 // if there is pmem32 request, add to the pmem32 aperture
1131 //
1132 if (ResourceRequestExisted (PMem32Node)) {
1133 NumConfig++;
1134 Aperture |= 0x04;
1135 }
1136
1137 //
1138 // if there is mem64 request, add to the mem64 aperture
1139 //
1140 if (ResourceRequestExisted (Mem64Node)) {
1141 NumConfig++;
1142 Aperture |= 0x08;
1143 }
1144
1145 //
1146 // if there is pmem64 request, add to the pmem64 aperture
1147 //
1148 if (ResourceRequestExisted (PMem64Node)) {
1149 NumConfig++;
1150 Aperture |= 0x10;
1151 }
1152
1153 if (NumConfig != 0) {
1154
1155 //
1156 // If there is at least one type of resource request,
1157 // allocate a acpi resource node
1158 //
1159 Configuration = AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) * NumConfig + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR));
1160 if (Configuration == NULL) {
1161 return EFI_OUT_OF_RESOURCES;
1162 }
1163
1164 Ptr = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration;
1165
1166 //
1167 // Deal with io aperture
1168 //
1169 if ((Aperture & 0x01) != 0) {
1170 Ptr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
1171 Ptr->Len = (UINT16) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3);
1172 //
1173 // Io
1174 //
1175 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;
1176 //
1177 // non ISA range
1178 //
1179 Ptr->SpecificFlag = 1;
1180 Ptr->AddrLen = IoNode->Length;
1181 Ptr->AddrRangeMax = IoNode->Alignment;
1182
1183 Ptr++;
1184 }
1185 //
1186 // Deal with mem32 aperture
1187 //
1188 if ((Aperture & 0x02) != 0) {
1189 Ptr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
1190 Ptr->Len = (UINT16) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3);
1191 //
1192 // Mem
1193 //
1194 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
1195 //
1196 // Nonprefechable
1197 //
1198 Ptr->SpecificFlag = 0;
1199 //
1200 // 32 bit
1201 //
1202 Ptr->AddrSpaceGranularity = 32;
1203 Ptr->AddrLen = Mem32Node->Length;
1204 Ptr->AddrRangeMax = Mem32Node->Alignment;
1205
1206 Ptr++;
1207 }
1208
1209 //
1210 // Deal with Pmem32 aperture
1211 //
1212 if ((Aperture & 0x04) != 0) {
1213 Ptr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
1214 Ptr->Len = (UINT16) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3);
1215 //
1216 // Mem
1217 //
1218 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
1219 //
1220 // prefechable
1221 //
1222 Ptr->SpecificFlag = 0x6;
1223 //
1224 // 32 bit
1225 //
1226 Ptr->AddrSpaceGranularity = 32;
1227 Ptr->AddrLen = PMem32Node->Length;
1228 Ptr->AddrRangeMax = PMem32Node->Alignment;
1229
1230 Ptr++;
1231 }
1232 //
1233 // Deal with mem64 aperture
1234 //
1235 if ((Aperture & 0x08) != 0) {
1236 Ptr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
1237 Ptr->Len = (UINT16) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3);
1238 //
1239 // Mem
1240 //
1241 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
1242 //
1243 // nonprefechable
1244 //
1245 Ptr->SpecificFlag = 0;
1246 //
1247 // 64 bit
1248 //
1249 Ptr->AddrSpaceGranularity = 64;
1250 Ptr->AddrLen = Mem64Node->Length;
1251 Ptr->AddrRangeMax = Mem64Node->Alignment;
1252
1253 Ptr++;
1254 }
1255 //
1256 // Deal with Pmem64 aperture
1257 //
1258 if ((Aperture & 0x10) != 0) {
1259 Ptr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
1260 Ptr->Len = (UINT16) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3);
1261 //
1262 // Mem
1263 //
1264 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
1265 //
1266 // prefechable
1267 //
1268 Ptr->SpecificFlag = 0x06;
1269 //
1270 // 64 bit
1271 //
1272 Ptr->AddrSpaceGranularity = 64;
1273 Ptr->AddrLen = PMem64Node->Length;
1274 Ptr->AddrRangeMax = PMem64Node->Alignment;
1275
1276 Ptr++;
1277 }
1278
1279 //
1280 // put the checksum
1281 //
1282 PtrEnd = (EFI_ACPI_END_TAG_DESCRIPTOR *) Ptr;
1283
1284 PtrEnd->Desc = ACPI_END_TAG_DESCRIPTOR;
1285 PtrEnd->Checksum = 0;
1286
1287 } else {
1288
1289 //
1290 // If there is no resource request
1291 //
1292 Configuration = AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR));
1293 if (Configuration == NULL) {
1294 return EFI_OUT_OF_RESOURCES;
1295 }
1296
1297 Ptr = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) (Configuration);
1298 Ptr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
1299
1300 PtrEnd = (EFI_ACPI_END_TAG_DESCRIPTOR *) (Ptr + 1);
1301 PtrEnd->Desc = ACPI_END_TAG_DESCRIPTOR;
1302 PtrEnd->Checksum = 0;
1303 }
1304
1305 *Config = Configuration;
1306
1307 return EFI_SUCCESS;
1308 }
1309
1310 /**
1311 Get resource base from an acpi configuration descriptor.
1312
1313 @param Config An acpi configuration descriptor.
1314 @param IoBase Output of I/O resource base address.
1315 @param Mem32Base Output of 32-bit memory base address.
1316 @param PMem32Base Output of 32-bit prefetchable memory base address.
1317 @param Mem64Base Output of 64-bit memory base address.
1318 @param PMem64Base Output of 64-bit prefetchable memory base address.
1319
1320 **/
1321 VOID
1322 GetResourceBase (
1323 IN VOID *Config,
1324 OUT UINT64 *IoBase,
1325 OUT UINT64 *Mem32Base,
1326 OUT UINT64 *PMem32Base,
1327 OUT UINT64 *Mem64Base,
1328 OUT UINT64 *PMem64Base
1329 )
1330 {
1331 UINT8 *Temp;
1332 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Ptr;
1333 UINT64 ResStatus;
1334
1335 ASSERT (Config != NULL);
1336
1337 *IoBase = 0xFFFFFFFFFFFFFFFFULL;
1338 *Mem32Base = 0xFFFFFFFFFFFFFFFFULL;
1339 *PMem32Base = 0xFFFFFFFFFFFFFFFFULL;
1340 *Mem64Base = 0xFFFFFFFFFFFFFFFFULL;
1341 *PMem64Base = 0xFFFFFFFFFFFFFFFFULL;
1342
1343 Temp = (UINT8 *) Config;
1344
1345 while (*Temp == ACPI_ADDRESS_SPACE_DESCRIPTOR) {
1346
1347 Ptr = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Temp;
1348 ResStatus = Ptr->AddrTranslationOffset;
1349
1350 if (ResStatus == EFI_RESOURCE_SATISFIED) {
1351
1352 switch (Ptr->ResType) {
1353
1354 //
1355 // Memory type aperture
1356 //
1357 case 0:
1358
1359 //
1360 // Check to see the granularity
1361 //
1362 if (Ptr->AddrSpaceGranularity == 32) {
1363 if ((Ptr->SpecificFlag & 0x06) != 0) {
1364 *PMem32Base = Ptr->AddrRangeMin;
1365 } else {
1366 *Mem32Base = Ptr->AddrRangeMin;
1367 }
1368 }
1369
1370 if (Ptr->AddrSpaceGranularity == 64) {
1371 if ((Ptr->SpecificFlag & 0x06) != 0) {
1372 *PMem64Base = Ptr->AddrRangeMin;
1373 } else {
1374 *Mem64Base = Ptr->AddrRangeMin;
1375 }
1376 }
1377 break;
1378
1379 case 1:
1380
1381 //
1382 // Io type aperture
1383 //
1384 *IoBase = Ptr->AddrRangeMin;
1385 break;
1386
1387 default:
1388 break;
1389
1390 }
1391 //
1392 // End switch
1393 //
1394 }
1395 //
1396 // End for
1397 //
1398 Temp += sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR);
1399 }
1400 }
1401
1402 /**
1403 Enumerate pci bridge, allocate resource and determine attribute
1404 for devices on this bridge.
1405
1406 @param BridgeDev Pointer to instance of bridge device.
1407
1408 @retval EFI_SUCCESS Successfully enumerated PCI bridge.
1409 @retval other Failed to enumerate.
1410
1411 **/
1412 EFI_STATUS
1413 PciBridgeEnumerator (
1414 IN PCI_IO_DEVICE *BridgeDev
1415 )
1416 {
1417 UINT8 SubBusNumber;
1418 UINT8 StartBusNumber;
1419 EFI_PCI_IO_PROTOCOL *PciIo;
1420 EFI_STATUS Status;
1421
1422 SubBusNumber = 0;
1423 StartBusNumber = 0;
1424 PciIo = &(BridgeDev->PciIo);
1425 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x19, 1, &StartBusNumber);
1426
1427 if (EFI_ERROR (Status)) {
1428 return Status;
1429 }
1430
1431 Status = PciAssignBusNumber (
1432 BridgeDev,
1433 StartBusNumber,
1434 &SubBusNumber
1435 );
1436
1437 if (EFI_ERROR (Status)) {
1438 return Status;
1439 }
1440
1441 Status = PciPciDeviceInfoCollector (BridgeDev, StartBusNumber);
1442
1443 if (EFI_ERROR (Status)) {
1444 return Status;
1445 }
1446
1447 Status = PciBridgeResourceAllocator (BridgeDev);
1448
1449 if (EFI_ERROR (Status)) {
1450 return Status;
1451 }
1452
1453 Status = DetermineDeviceAttribute (BridgeDev);
1454
1455 if (EFI_ERROR (Status)) {
1456 return Status;
1457 }
1458
1459 return EFI_SUCCESS;
1460
1461 }
1462
1463 /**
1464 Allocate all kinds of resource for PCI bridge.
1465
1466 @param Bridge Pointer to bridge instance.
1467
1468 @retval EFI_SUCCESS Successfully allocated resource for PCI bridge.
1469 @retval other Failed to allocate resource for bridge.
1470
1471 **/
1472 EFI_STATUS
1473 PciBridgeResourceAllocator (
1474 IN PCI_IO_DEVICE *Bridge
1475 )
1476 {
1477 PCI_RESOURCE_NODE *IoBridge;
1478 PCI_RESOURCE_NODE *Mem32Bridge;
1479 PCI_RESOURCE_NODE *PMem32Bridge;
1480 PCI_RESOURCE_NODE *Mem64Bridge;
1481 PCI_RESOURCE_NODE *PMem64Bridge;
1482 UINT64 IoBase;
1483 UINT64 Mem32Base;
1484 UINT64 PMem32Base;
1485 UINT64 Mem64Base;
1486 UINT64 PMem64Base;
1487 EFI_STATUS Status;
1488
1489 IoBridge = CreateResourceNode (
1490 Bridge,
1491 0,
1492 Bridge->BridgeIoAlignment,
1493 0,
1494 PciBarTypeIo16,
1495 PciResUsageTypical
1496 );
1497
1498 Mem32Bridge = CreateResourceNode (
1499 Bridge,
1500 0,
1501 0xFFFFF,
1502 0,
1503 PciBarTypeMem32,
1504 PciResUsageTypical
1505 );
1506
1507 PMem32Bridge = CreateResourceNode (
1508 Bridge,
1509 0,
1510 0xFFFFF,
1511 0,
1512 PciBarTypePMem32,
1513 PciResUsageTypical
1514 );
1515
1516 Mem64Bridge = CreateResourceNode (
1517 Bridge,
1518 0,
1519 0xFFFFF,
1520 0,
1521 PciBarTypeMem64,
1522 PciResUsageTypical
1523 );
1524
1525 PMem64Bridge = CreateResourceNode (
1526 Bridge,
1527 0,
1528 0xFFFFF,
1529 0,
1530 PciBarTypePMem64,
1531 PciResUsageTypical
1532 );
1533
1534 //
1535 // Create resourcemap by going through all the devices subject to this root bridge
1536 //
1537 CreateResourceMap (
1538 Bridge,
1539 IoBridge,
1540 Mem32Bridge,
1541 PMem32Bridge,
1542 Mem64Bridge,
1543 PMem64Bridge
1544 );
1545
1546 Status = GetResourceBaseFromBridge (
1547 Bridge,
1548 &IoBase,
1549 &Mem32Base,
1550 &PMem32Base,
1551 &Mem64Base,
1552 &PMem64Base
1553 );
1554
1555 if (EFI_ERROR (Status)) {
1556 return Status;
1557 }
1558
1559 //
1560 // Program IO resources
1561 //
1562 ProgramResource (
1563 IoBase,
1564 IoBridge
1565 );
1566
1567 //
1568 // Program Mem32 resources
1569 //
1570 ProgramResource (
1571 Mem32Base,
1572 Mem32Bridge
1573 );
1574
1575 //
1576 // Program PMem32 resources
1577 //
1578 ProgramResource (
1579 PMem32Base,
1580 PMem32Bridge
1581 );
1582
1583 //
1584 // Program Mem64 resources
1585 //
1586 ProgramResource (
1587 Mem64Base,
1588 Mem64Bridge
1589 );
1590
1591 //
1592 // Program PMem64 resources
1593 //
1594 ProgramResource (
1595 PMem64Base,
1596 PMem64Bridge
1597 );
1598
1599 DestroyResourceTree (IoBridge);
1600 DestroyResourceTree (Mem32Bridge);
1601 DestroyResourceTree (PMem32Bridge);
1602 DestroyResourceTree (PMem64Bridge);
1603 DestroyResourceTree (Mem64Bridge);
1604
1605 gBS->FreePool (IoBridge);
1606 gBS->FreePool (Mem32Bridge);
1607 gBS->FreePool (PMem32Bridge);
1608 gBS->FreePool (PMem64Bridge);
1609 gBS->FreePool (Mem64Bridge);
1610
1611 return EFI_SUCCESS;
1612 }
1613
1614 /**
1615 Get resource base address for a pci bridge device.
1616
1617 @param Bridge Given Pci driver instance.
1618 @param IoBase Output for base address of I/O type resource.
1619 @param Mem32Base Output for base address of 32-bit memory type resource.
1620 @param PMem32Base Ooutput for base address of 32-bit Pmemory type resource.
1621 @param Mem64Base Output for base address of 64-bit memory type resource.
1622 @param PMem64Base Output for base address of 64-bit Pmemory type resource.
1623
1624 @retval EFI_SUCCESS Successfully got resource base address.
1625 @retval EFI_OUT_OF_RESOURCES PCI bridge is not available.
1626
1627 **/
1628 EFI_STATUS
1629 GetResourceBaseFromBridge (
1630 IN PCI_IO_DEVICE *Bridge,
1631 OUT UINT64 *IoBase,
1632 OUT UINT64 *Mem32Base,
1633 OUT UINT64 *PMem32Base,
1634 OUT UINT64 *Mem64Base,
1635 OUT UINT64 *PMem64Base
1636 )
1637 {
1638 if (!Bridge->Allocated) {
1639 return EFI_OUT_OF_RESOURCES;
1640 }
1641
1642 *IoBase = gAllOne;
1643 *Mem32Base = gAllOne;
1644 *PMem32Base = gAllOne;
1645 *Mem64Base = gAllOne;
1646 *PMem64Base = gAllOne;
1647
1648 if (IS_PCI_BRIDGE (&Bridge->Pci)) {
1649
1650 if (Bridge->PciBar[PPB_IO_RANGE].Length > 0) {
1651 *IoBase = Bridge->PciBar[PPB_IO_RANGE].BaseAddress;
1652 }
1653
1654 if (Bridge->PciBar[PPB_MEM32_RANGE].Length > 0) {
1655 *Mem32Base = Bridge->PciBar[PPB_MEM32_RANGE].BaseAddress;
1656 }
1657
1658 if (Bridge->PciBar[PPB_PMEM32_RANGE].Length > 0) {
1659 *PMem32Base = Bridge->PciBar[PPB_PMEM32_RANGE].BaseAddress;
1660 }
1661
1662 if (Bridge->PciBar[PPB_PMEM64_RANGE].Length > 0) {
1663 *PMem64Base = Bridge->PciBar[PPB_PMEM64_RANGE].BaseAddress;
1664 } else {
1665 *PMem64Base = gAllOne;
1666 }
1667
1668 }
1669
1670 if (IS_CARDBUS_BRIDGE (&Bridge->Pci)) {
1671 if (Bridge->PciBar[P2C_IO_1].Length > 0) {
1672 *IoBase = Bridge->PciBar[P2C_IO_1].BaseAddress;
1673 } else {
1674 if (Bridge->PciBar[P2C_IO_2].Length > 0) {
1675 *IoBase = Bridge->PciBar[P2C_IO_2].BaseAddress;
1676 }
1677 }
1678
1679 if (Bridge->PciBar[P2C_MEM_1].Length > 0) {
1680 if (Bridge->PciBar[P2C_MEM_1].BarType == PciBarTypePMem32) {
1681 *PMem32Base = Bridge->PciBar[P2C_MEM_1].BaseAddress;
1682 }
1683
1684 if (Bridge->PciBar[P2C_MEM_1].BarType == PciBarTypeMem32) {
1685 *Mem32Base = Bridge->PciBar[P2C_MEM_1].BaseAddress;
1686 }
1687 }
1688
1689 if (Bridge->PciBar[P2C_MEM_2].Length > 0) {
1690 if (Bridge->PciBar[P2C_MEM_2].BarType == PciBarTypePMem32) {
1691 *PMem32Base = Bridge->PciBar[P2C_MEM_2].BaseAddress;
1692 }
1693
1694 if (Bridge->PciBar[P2C_MEM_2].BarType == PciBarTypeMem32) {
1695 *Mem32Base = Bridge->PciBar[P2C_MEM_2].BaseAddress;
1696 }
1697 }
1698 }
1699
1700 return EFI_SUCCESS;
1701 }
1702
1703 /**
1704 These are the notifications from the PCI bus driver that it is about to enter a certain
1705 phase of the PCI enumeration process.
1706
1707 This member function can be used to notify the host bridge driver to perform specific actions,
1708 including any chipset-specific initialization, so that the chipset is ready to enter the next phase.
1709 Eight notification points are defined at this time. See belows:
1710 EfiPciHostBridgeBeginEnumeration Resets the host bridge PCI apertures and internal data
1711 structures. The PCI enumerator should issue this notification
1712 before starting a fresh enumeration process. Enumeration cannot
1713 be restarted after sending any other notification such as
1714 EfiPciHostBridgeBeginBusAllocation.
1715 EfiPciHostBridgeBeginBusAllocation The bus allocation phase is about to begin. No specific action is
1716 required here. This notification can be used to perform any
1717 chipset-specific programming.
1718 EfiPciHostBridgeEndBusAllocation The bus allocation and bus programming phase is complete. No
1719 specific action is required here. This notification can be used to
1720 perform any chipset-specific programming.
1721 EfiPciHostBridgeBeginResourceAllocation
1722 The resource allocation phase is about to begin. No specific
1723 action is required here. This notification can be used to perform
1724 any chipset-specific programming.
1725 EfiPciHostBridgeAllocateResources Allocates resources per previously submitted requests for all the PCI
1726 root bridges. These resource settings are returned on the next call to
1727 GetProposedResources(). Before calling NotifyPhase() with a Phase of
1728 EfiPciHostBridgeAllocateResource, the PCI bus enumerator is responsible
1729 for gathering I/O and memory requests for
1730 all the PCI root bridges and submitting these requests using
1731 SubmitResources(). This function pads the resource amount
1732 to suit the root bridge hardware, takes care of dependencies between
1733 the PCI root bridges, and calls the Global Coherency Domain (GCD)
1734 with the allocation request. In the case of padding, the allocated range
1735 could be bigger than what was requested.
1736 EfiPciHostBridgeSetResources Programs the host bridge hardware to decode previously allocated
1737 resources (proposed resources) for all the PCI root bridges. After the
1738 hardware is programmed, reassigning resources will not be supported.
1739 The bus settings are not affected.
1740 EfiPciHostBridgeFreeResources Deallocates resources that were previously allocated for all the PCI
1741 root bridges and resets the I/O and memory apertures to their initial
1742 state. The bus settings are not affected. If the request to allocate
1743 resources fails, the PCI enumerator can use this notification to
1744 deallocate previous resources, adjust the requests, and retry
1745 allocation.
1746 EfiPciHostBridgeEndResourceAllocation The resource allocation phase is completed. No specific action is
1747 required here. This notification can be used to perform any chipsetspecific
1748 programming.
1749
1750 @param[in] PciResAlloc The instance pointer of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
1751 @param[in] Phase The phase during enumeration
1752
1753 @retval EFI_NOT_READY This phase cannot be entered at this time. For example, this error
1754 is valid for a Phase of EfiPciHostBridgeAllocateResources if
1755 SubmitResources() has not been called for one or more
1756 PCI root bridges before this call
1757 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. This error is valid
1758 for a Phase of EfiPciHostBridgeSetResources.
1759 @retval EFI_INVALID_PARAMETER Invalid phase parameter
1760 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1761 This error is valid for a Phase of EfiPciHostBridgeAllocateResources if the
1762 previously submitted resource requests cannot be fulfilled or
1763 were only partially fulfilled.
1764 @retval EFI_SUCCESS The notification was accepted without any errors.
1765
1766 **/
1767 EFI_STATUS
1768 NotifyPhase (
1769 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *PciResAlloc,
1770 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase
1771 )
1772 {
1773 EFI_HANDLE HostBridgeHandle;
1774 EFI_HANDLE RootBridgeHandle;
1775 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
1776 EFI_STATUS Status;
1777
1778 HostBridgeHandle = NULL;
1779 RootBridgeHandle = NULL;
1780 if (gPciPlatformProtocol != NULL) {
1781 //
1782 // Get Host Bridge Handle.
1783 //
1784 PciResAlloc->GetNextRootBridge (PciResAlloc, &RootBridgeHandle);
1785
1786 //
1787 // Get the rootbridge Io protocol to find the host bridge handle
1788 //
1789 Status = gBS->HandleProtocol (
1790 RootBridgeHandle,
1791 &gEfiPciRootBridgeIoProtocolGuid,
1792 (VOID **) &PciRootBridgeIo
1793 );
1794
1795 if (EFI_ERROR (Status)) {
1796 return EFI_NOT_FOUND;
1797 }
1798
1799 HostBridgeHandle = PciRootBridgeIo->ParentHandle;
1800
1801 //
1802 // Call PlatformPci::PlatformNotify() if the protocol is present.
1803 //
1804 gPciPlatformProtocol->PlatformNotify (
1805 gPciPlatformProtocol,
1806 HostBridgeHandle,
1807 Phase,
1808 ChipsetEntry
1809 );
1810 } else if (gPciOverrideProtocol != NULL){
1811 //
1812 // Get Host Bridge Handle.
1813 //
1814 PciResAlloc->GetNextRootBridge (PciResAlloc, &RootBridgeHandle);
1815
1816 //
1817 // Get the rootbridge Io protocol to find the host bridge handle
1818 //
1819 Status = gBS->HandleProtocol (
1820 RootBridgeHandle,
1821 &gEfiPciRootBridgeIoProtocolGuid,
1822 (VOID **) &PciRootBridgeIo
1823 );
1824
1825 if (EFI_ERROR (Status)) {
1826 return EFI_NOT_FOUND;
1827 }
1828
1829 HostBridgeHandle = PciRootBridgeIo->ParentHandle;
1830
1831 //
1832 // Call PlatformPci::PhaseNotify() if the protocol is present.
1833 //
1834 gPciOverrideProtocol->PlatformNotify (
1835 gPciOverrideProtocol,
1836 HostBridgeHandle,
1837 Phase,
1838 ChipsetEntry
1839 );
1840 }
1841
1842 Status = PciResAlloc->NotifyPhase (
1843 PciResAlloc,
1844 Phase
1845 );
1846
1847 if (gPciPlatformProtocol != NULL) {
1848 //
1849 // Call PlatformPci::PlatformNotify() if the protocol is present.
1850 //
1851 gPciPlatformProtocol->PlatformNotify (
1852 gPciPlatformProtocol,
1853 HostBridgeHandle,
1854 Phase,
1855 ChipsetExit
1856 );
1857
1858 } else if (gPciOverrideProtocol != NULL) {
1859 //
1860 // Call PlatformPci::PhaseNotify() if the protocol is present.
1861 //
1862 gPciOverrideProtocol->PlatformNotify (
1863 gPciOverrideProtocol,
1864 HostBridgeHandle,
1865 Phase,
1866 ChipsetExit
1867 );
1868 }
1869
1870 return EFI_SUCCESS;
1871 }
1872
1873 /**
1874 Provides the hooks from the PCI bus driver to every PCI controller (device/function) at various
1875 stages of the PCI enumeration process that allow the host bridge driver to preinitialize individual
1876 PCI controllers before enumeration.
1877
1878 This function is called during the PCI enumeration process. No specific action is expected from this
1879 member function. It allows the host bridge driver to preinitialize individual PCI controllers before
1880 enumeration.
1881
1882 @param Bridge Pointer to the EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL instance.
1883 @param Bus The bus number of the pci device.
1884 @param Device The device number of the pci device.
1885 @param Func The function number of the pci device.
1886 @param Phase The phase of the PCI device enumeration.
1887
1888 @retval EFI_SUCCESS The requested parameters were returned.
1889 @retval EFI_INVALID_PARAMETER RootBridgeHandle is not a valid root bridge handle.
1890 @retval EFI_INVALID_PARAMETER Phase is not a valid phase that is defined in
1891 EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE.
1892 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. The PCI enumerator should
1893 not enumerate this device, including its child devices if it is a PCI-to-PCI
1894 bridge.
1895
1896 **/
1897 EFI_STATUS
1898 PreprocessController (
1899 IN PCI_IO_DEVICE *Bridge,
1900 IN UINT8 Bus,
1901 IN UINT8 Device,
1902 IN UINT8 Func,
1903 IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase
1904 )
1905 {
1906 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS RootBridgePciAddress;
1907 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *PciResAlloc;
1908 EFI_HANDLE RootBridgeHandle;
1909 EFI_HANDLE HostBridgeHandle;
1910 EFI_STATUS Status;
1911
1912 //
1913 // Get the host bridge handle
1914 //
1915 HostBridgeHandle = Bridge->PciRootBridgeIo->ParentHandle;
1916
1917 //
1918 // Get the pci host bridge resource allocation protocol
1919 //
1920 Status = gBS->OpenProtocol (
1921 HostBridgeHandle,
1922 &gEfiPciHostBridgeResourceAllocationProtocolGuid,
1923 (VOID **) &PciResAlloc,
1924 NULL,
1925 NULL,
1926 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1927 );
1928
1929 if (EFI_ERROR (Status)) {
1930 return EFI_UNSUPPORTED;
1931 }
1932
1933 //
1934 // Get Root Brige Handle
1935 //
1936 while (Bridge->Parent != NULL) {
1937 Bridge = Bridge->Parent;
1938 }
1939
1940 RootBridgeHandle = Bridge->Handle;
1941
1942 RootBridgePciAddress.Register = 0;
1943 RootBridgePciAddress.Function = Func;
1944 RootBridgePciAddress.Device = Device;
1945 RootBridgePciAddress.Bus = Bus;
1946 RootBridgePciAddress.ExtendedRegister = 0;
1947
1948 if (gPciPlatformProtocol != NULL) {
1949 //
1950 // Call PlatformPci::PrepController() if the protocol is present.
1951 //
1952 gPciPlatformProtocol->PlatformPrepController (
1953 gPciPlatformProtocol,
1954 HostBridgeHandle,
1955 RootBridgeHandle,
1956 RootBridgePciAddress,
1957 Phase,
1958 ChipsetEntry
1959 );
1960 } else if (gPciOverrideProtocol != NULL) {
1961 //
1962 // Call PlatformPci::PrepController() if the protocol is present.
1963 //
1964 gPciOverrideProtocol->PlatformPrepController (
1965 gPciOverrideProtocol,
1966 HostBridgeHandle,
1967 RootBridgeHandle,
1968 RootBridgePciAddress,
1969 Phase,
1970 ChipsetEntry
1971 );
1972 }
1973
1974 Status = PciResAlloc->PreprocessController (
1975 PciResAlloc,
1976 RootBridgeHandle,
1977 RootBridgePciAddress,
1978 Phase
1979 );
1980
1981 if (gPciPlatformProtocol != NULL) {
1982 //
1983 // Call PlatformPci::PrepController() if the protocol is present.
1984 //
1985 gPciPlatformProtocol->PlatformPrepController (
1986 gPciPlatformProtocol,
1987 HostBridgeHandle,
1988 RootBridgeHandle,
1989 RootBridgePciAddress,
1990 Phase,
1991 ChipsetExit
1992 );
1993 } else if (gPciOverrideProtocol != NULL) {
1994 //
1995 // Call PlatformPci::PrepController() if the protocol is present.
1996 //
1997 gPciOverrideProtocol->PlatformPrepController (
1998 gPciOverrideProtocol,
1999 HostBridgeHandle,
2000 RootBridgeHandle,
2001 RootBridgePciAddress,
2002 Phase,
2003 ChipsetExit
2004 );
2005 }
2006
2007 return EFI_SUCCESS;
2008 }
2009
2010 /**
2011 This function allows the PCI bus driver to be notified to act as requested when a hot-plug event has
2012 happened on the hot-plug controller. Currently, the operations include add operation and remove operation..
2013
2014 @param This A pointer to the hot plug request protocol.
2015 @param Operation The operation the PCI bus driver is requested to make.
2016 @param Controller The handle of the hot-plug controller.
2017 @param RemainingDevicePath The remaining device path for the PCI-like hot-plug device.
2018 @param NumberOfChildren The number of child handles.
2019 For a add operation, it is an output parameter.
2020 For a remove operation, it's an input parameter.
2021 @param ChildHandleBuffer The buffer which contains the child handles.
2022
2023 @retval EFI_INVALID_PARAMETER Operation is not a legal value.
2024 Controller is NULL or not a valid handle.
2025 NumberOfChildren is NULL.
2026 ChildHandleBuffer is NULL while Operation is add.
2027 @retval EFI_OUT_OF_RESOURCES There are no enough resources to start the devices.
2028 @retval EFI_NOT_FOUND Can not find bridge according to controller handle.
2029 @retval EFI_SUCCESS The handles for the specified device have been created or destroyed
2030 as requested, and for an add operation, the new handles are
2031 returned in ChildHandleBuffer.
2032 **/
2033 EFI_STATUS
2034 EFIAPI
2035 PciHotPlugRequestNotify (
2036 IN EFI_PCI_HOTPLUG_REQUEST_PROTOCOL * This,
2037 IN EFI_PCI_HOTPLUG_OPERATION Operation,
2038 IN EFI_HANDLE Controller,
2039 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL,
2040 IN OUT UINT8 *NumberOfChildren,
2041 IN OUT EFI_HANDLE * ChildHandleBuffer
2042 )
2043 {
2044 PCI_IO_DEVICE *Bridge;
2045 PCI_IO_DEVICE *Temp;
2046 EFI_PCI_IO_PROTOCOL *PciIo;
2047 UINTN Index;
2048 EFI_HANDLE RootBridgeHandle;
2049 EFI_STATUS Status;
2050
2051 //
2052 // Check input parameter validity
2053 //
2054 if ((Controller == NULL) || (NumberOfChildren == NULL)){
2055 return EFI_INVALID_PARAMETER;
2056 }
2057
2058 if ((Operation != EfiPciHotPlugRequestAdd) && (Operation != EfiPciHotplugRequestRemove)) {
2059 return EFI_INVALID_PARAMETER;
2060 }
2061
2062 if (Operation == EfiPciHotPlugRequestAdd){
2063 if (ChildHandleBuffer == NULL) {
2064 return EFI_INVALID_PARAMETER;
2065 }
2066 } else if ((Operation == EfiPciHotplugRequestRemove) && (*NumberOfChildren != 0)) {
2067 if (ChildHandleBuffer == NULL) {
2068 return EFI_INVALID_PARAMETER;
2069 }
2070 }
2071
2072 Status = gBS->OpenProtocol (
2073 Controller,
2074 &gEfiPciIoProtocolGuid,
2075 (VOID **) &PciIo,
2076 gPciBusDriverBinding.DriverBindingHandle,
2077 Controller,
2078 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2079 );
2080
2081 if (EFI_ERROR (Status)) {
2082 return EFI_NOT_FOUND;
2083 }
2084
2085 Bridge = PCI_IO_DEVICE_FROM_PCI_IO_THIS (PciIo);
2086
2087 //
2088 // Get root bridge handle
2089 //
2090 Temp = Bridge;
2091 while (Temp->Parent != NULL) {
2092 Temp = Temp->Parent;
2093 }
2094
2095 RootBridgeHandle = Temp->Handle;
2096
2097 if (Operation == EfiPciHotPlugRequestAdd) {
2098
2099 if (NumberOfChildren != NULL) {
2100 *NumberOfChildren = 0;
2101 }
2102
2103 if (IsListEmpty (&Bridge->ChildList)) {
2104
2105 Status = PciBridgeEnumerator (Bridge);
2106
2107 if (EFI_ERROR (Status)) {
2108 return Status;
2109 }
2110 }
2111
2112 Status = StartPciDevicesOnBridge (
2113 RootBridgeHandle,
2114 Bridge,
2115 RemainingDevicePath,
2116 NumberOfChildren,
2117 ChildHandleBuffer
2118 );
2119
2120 return Status;
2121 }
2122
2123 if (Operation == EfiPciHotplugRequestRemove) {
2124
2125 if (*NumberOfChildren == 0) {
2126 //
2127 // Remove all devices on the bridge
2128 //
2129 RemoveAllPciDeviceOnBridge (RootBridgeHandle, Bridge);
2130 return EFI_SUCCESS;
2131
2132 }
2133
2134 for (Index = 0; Index < *NumberOfChildren; Index++) {
2135 //
2136 // De register all the pci device
2137 //
2138 Status = DeRegisterPciDevice (RootBridgeHandle, ChildHandleBuffer[Index]);
2139
2140 if (EFI_ERROR (Status)) {
2141 return Status;
2142 }
2143
2144 }
2145 //
2146 // End for
2147 //
2148 return EFI_SUCCESS;
2149 }
2150
2151 return EFI_SUCCESS;
2152 }
2153
2154 /**
2155 Search hostbridge according to given handle
2156
2157 @param RootBridgeHandle Host bridge handle.
2158
2159 @retval TRUE Found host bridge handle.
2160 @retval FALSE Not found hot bridge handle.
2161
2162 **/
2163 BOOLEAN
2164 SearchHostBridgeHandle (
2165 IN EFI_HANDLE RootBridgeHandle
2166 )
2167 {
2168 EFI_HANDLE HostBridgeHandle;
2169 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
2170 UINTN Index;
2171 EFI_STATUS Status;
2172
2173 //
2174 // Get the rootbridge Io protocol to find the host bridge handle
2175 //
2176 Status = gBS->OpenProtocol (
2177 RootBridgeHandle,
2178 &gEfiPciRootBridgeIoProtocolGuid,
2179 (VOID **) &PciRootBridgeIo,
2180 gPciBusDriverBinding.DriverBindingHandle,
2181 RootBridgeHandle,
2182 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2183 );
2184
2185 if (EFI_ERROR (Status)) {
2186 return FALSE;
2187 }
2188
2189 HostBridgeHandle = PciRootBridgeIo->ParentHandle;
2190 for (Index = 0; Index < gPciHostBridgeNumber; Index++) {
2191 if (HostBridgeHandle == gPciHostBrigeHandles[Index]) {
2192 return TRUE;
2193 }
2194 }
2195
2196 return FALSE;
2197 }
2198
2199 /**
2200 Add host bridge handle to global variable for enumerating.
2201
2202 @param HostBridgeHandle Host bridge handle.
2203
2204 @retval EFI_SUCCESS Successfully added host bridge.
2205 @retval EFI_ABORTED Host bridge is NULL, or given host bridge
2206 has been in host bridge list.
2207
2208 **/
2209 EFI_STATUS
2210 AddHostBridgeEnumerator (
2211 IN EFI_HANDLE HostBridgeHandle
2212 )
2213 {
2214 UINTN Index;
2215
2216 if (HostBridgeHandle == NULL) {
2217 return EFI_ABORTED;
2218 }
2219
2220 for (Index = 0; Index < gPciHostBridgeNumber; Index++) {
2221 if (HostBridgeHandle == gPciHostBrigeHandles[Index]) {
2222 return EFI_ABORTED;
2223 }
2224 }
2225
2226 if (Index < PCI_MAX_HOST_BRIDGE_NUM) {
2227 gPciHostBrigeHandles[Index] = HostBridgeHandle;
2228 gPciHostBridgeNumber++;
2229 }
2230
2231 return EFI_SUCCESS;
2232 }
2233