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