]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.c
Fix the bug which incorrectly programs the 64bit base address register in the PCI...
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciBusDxe / PciResourceSupport.c
1 /** @file
2 PCI resouces support functions implemntation for PCI Bus module.
3
4 Copyright (c) 2006 - 2013, 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 // The default policy for the PCI bus driver is NOT to reserve I/O ranges for both ISA aliases and VGA aliases.
19 //
20 BOOLEAN mReserveIsaAliases = FALSE;
21 BOOLEAN mReserveVgaAliases = FALSE;
22 BOOLEAN mPolicyDetermined = FALSE;
23
24 /**
25 The function is used to skip VGA range.
26
27 @param Start Returned start address including VGA range.
28 @param Length The length of VGA range.
29
30 **/
31 VOID
32 SkipVGAAperture (
33 OUT UINT64 *Start,
34 IN UINT64 Length
35 )
36 {
37 UINT64 Original;
38 UINT64 Mask;
39 UINT64 StartOffset;
40 UINT64 LimitOffset;
41
42 ASSERT (Start != NULL);
43 //
44 // For legacy VGA, bit 10 to bit 15 is not decoded
45 //
46 Mask = 0x3FF;
47
48 Original = *Start;
49 StartOffset = Original & Mask;
50 LimitOffset = ((*Start) + Length - 1) & Mask;
51 if (LimitOffset >= VGABASE1) {
52 *Start = *Start - StartOffset + VGALIMIT2 + 1;
53 }
54 }
55
56 /**
57 This function is used to skip ISA aliasing aperture.
58
59 @param Start Returned start address including ISA aliasing aperture.
60 @param Length The length of ISA aliasing aperture.
61
62 **/
63 VOID
64 SkipIsaAliasAperture (
65 OUT UINT64 *Start,
66 IN UINT64 Length
67 )
68 {
69
70 UINT64 Original;
71 UINT64 Mask;
72 UINT64 StartOffset;
73 UINT64 LimitOffset;
74
75 ASSERT (Start != NULL);
76
77 //
78 // For legacy ISA, bit 10 to bit 15 is not decoded
79 //
80 Mask = 0x3FF;
81
82 Original = *Start;
83 StartOffset = Original & Mask;
84 LimitOffset = ((*Start) + Length - 1) & Mask;
85
86 if (LimitOffset >= ISABASE) {
87 *Start = *Start - StartOffset + ISALIMIT + 1;
88 }
89 }
90
91 /**
92 This function inserts a resource node into the resource list.
93 The resource list is sorted in descend order.
94
95 @param Bridge PCI resource node for bridge.
96 @param ResNode Resource node want to be inserted.
97
98 **/
99 VOID
100 InsertResourceNode (
101 IN OUT PCI_RESOURCE_NODE *Bridge,
102 IN PCI_RESOURCE_NODE *ResNode
103 )
104 {
105 LIST_ENTRY *CurrentLink;
106 PCI_RESOURCE_NODE *Temp;
107 UINT64 ResNodeAlignRest;
108 UINT64 TempAlignRest;
109
110 ASSERT (Bridge != NULL);
111 ASSERT (ResNode != NULL);
112
113 InsertHeadList (&Bridge->ChildList, &ResNode->Link);
114
115 CurrentLink = Bridge->ChildList.ForwardLink->ForwardLink;
116 while (CurrentLink != &Bridge->ChildList) {
117 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
118
119 if (ResNode->Alignment > Temp->Alignment) {
120 break;
121 } else if (ResNode->Alignment == Temp->Alignment) {
122 ResNodeAlignRest = ResNode->Length & ResNode->Alignment;
123 TempAlignRest = Temp->Length & Temp->Alignment;
124 if ((ResNodeAlignRest == 0) || (ResNodeAlignRest >= TempAlignRest)) {
125 break;
126 }
127 }
128
129 SwapListEntries (&ResNode->Link, CurrentLink);
130
131 CurrentLink = ResNode->Link.ForwardLink;
132 }
133 }
134
135 /**
136 This routine is used to merge two different resource trees in need of
137 resoure degradation.
138
139 For example, if an upstream PPB doesn't support,
140 prefetchable memory decoding, the PCI bus driver will choose to call this function
141 to merge prefectchable memory resource list into normal memory list.
142
143 If the TypeMerge is TRUE, Res resource type is changed to the type of destination resource
144 type.
145 If Dst is NULL or Res is NULL, ASSERT ().
146
147 @param Dst Point to destination resource tree.
148 @param Res Point to source resource tree.
149 @param TypeMerge If the TypeMerge is TRUE, Res resource type is changed to the type of
150 destination resource type.
151
152 **/
153 VOID
154 MergeResourceTree (
155 IN PCI_RESOURCE_NODE *Dst,
156 IN PCI_RESOURCE_NODE *Res,
157 IN BOOLEAN TypeMerge
158 )
159 {
160
161 LIST_ENTRY *CurrentLink;
162 PCI_RESOURCE_NODE *Temp;
163
164 ASSERT (Dst != NULL);
165 ASSERT (Res != NULL);
166
167 while (!IsListEmpty (&Res->ChildList)) {
168 CurrentLink = Res->ChildList.ForwardLink;
169
170 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
171
172 if (TypeMerge) {
173 Temp->ResType = Dst->ResType;
174 }
175
176 RemoveEntryList (CurrentLink);
177 InsertResourceNode (Dst, Temp);
178 }
179 }
180
181 /**
182 This function is used to calculate the IO16 aperture
183 for a bridge.
184
185 @param Bridge PCI resource node for bridge.
186
187 **/
188 VOID
189 CalculateApertureIo16 (
190 IN PCI_RESOURCE_NODE *Bridge
191 )
192 {
193 EFI_STATUS Status;
194 UINT64 Aperture;
195 LIST_ENTRY *CurrentLink;
196 PCI_RESOURCE_NODE *Node;
197 UINT64 Offset;
198 EFI_PCI_PLATFORM_POLICY PciPolicy;
199
200 if (!mPolicyDetermined) {
201 //
202 // Check PciPlatform policy
203 //
204 Status = EFI_NOT_FOUND;
205 PciPolicy = 0;
206 if (gPciPlatformProtocol != NULL) {
207 Status = gPciPlatformProtocol->GetPlatformPolicy (
208 gPciPlatformProtocol,
209 &PciPolicy
210 );
211 }
212
213 if (EFI_ERROR (Status) && gPciOverrideProtocol != NULL) {
214 Status = gPciOverrideProtocol->GetPlatformPolicy (
215 gPciOverrideProtocol,
216 &PciPolicy
217 );
218 }
219
220 if (!EFI_ERROR (Status)) {
221 if ((PciPolicy & EFI_RESERVE_ISA_IO_ALIAS) != 0) {
222 mReserveIsaAliases = TRUE;
223 }
224 if ((PciPolicy & EFI_RESERVE_VGA_IO_ALIAS) != 0) {
225 mReserveVgaAliases = TRUE;
226 }
227 }
228 mPolicyDetermined = TRUE;
229 }
230
231 Aperture = 0;
232
233 if (Bridge == NULL) {
234 return ;
235 }
236
237 CurrentLink = Bridge->ChildList.ForwardLink;
238
239 //
240 // Assume the bridge is aligned
241 //
242 while (CurrentLink != &Bridge->ChildList) {
243
244 Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
245
246 //
247 // Consider the aperture alignment
248 //
249 Offset = Aperture & (Node->Alignment);
250
251 if (Offset != 0) {
252
253 Aperture = Aperture + (Node->Alignment + 1) - Offset;
254
255 }
256
257 //
258 // IsaEnable and VGAEnable can not be implemented now.
259 // If both of them are enabled, then the IO resource would
260 // become too limited to meet the requirement of most of devices.
261 //
262 if (mReserveIsaAliases || mReserveVgaAliases) {
263 if (!IS_PCI_BRIDGE (&(Node->PciDev->Pci)) && !IS_CARDBUS_BRIDGE (&(Node->PciDev->Pci))) {
264 //
265 // Check if there is need to support ISA/VGA decoding
266 // If so, we need to avoid isa/vga aliasing range
267 //
268 if (mReserveIsaAliases) {
269 SkipIsaAliasAperture (
270 &Aperture,
271 Node->Length
272 );
273 Offset = Aperture & (Node->Alignment);
274 if (Offset != 0) {
275 Aperture = Aperture + (Node->Alignment + 1) - Offset;
276 }
277 } else if (mReserveVgaAliases) {
278 SkipVGAAperture (
279 &Aperture,
280 Node->Length
281 );
282 Offset = Aperture & (Node->Alignment);
283 if (Offset != 0) {
284 Aperture = Aperture + (Node->Alignment + 1) - Offset;
285 }
286 }
287 }
288 }
289
290 Node->Offset = Aperture;
291
292 //
293 // Increment aperture by the length of node
294 //
295 Aperture += Node->Length;
296
297 CurrentLink = CurrentLink->ForwardLink;
298 }
299
300 //
301 // At last, adjust the aperture with the bridge's
302 // alignment
303 //
304 Offset = Aperture & (Bridge->Alignment);
305
306 if (Offset != 0) {
307 Aperture = Aperture + (Bridge->Alignment + 1) - Offset;
308 }
309
310 Bridge->Length = Aperture;
311 //
312 // At last, adjust the bridge's alignment to the first child's alignment
313 // if the bridge has at least one child
314 //
315 CurrentLink = Bridge->ChildList.ForwardLink;
316 if (CurrentLink != &Bridge->ChildList) {
317 Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
318 if (Node->Alignment > Bridge->Alignment) {
319 Bridge->Alignment = Node->Alignment;
320 }
321 }
322 }
323
324 /**
325 This function is used to calculate the resource aperture
326 for a given bridge device.
327
328 @param Bridge PCI resouce node for given bridge device.
329
330 **/
331 VOID
332 CalculateResourceAperture (
333 IN PCI_RESOURCE_NODE *Bridge
334 )
335 {
336 UINT64 Aperture;
337 LIST_ENTRY *CurrentLink;
338 PCI_RESOURCE_NODE *Node;
339
340 UINT64 Offset;
341
342 Aperture = 0;
343
344 if (Bridge == NULL) {
345 return ;
346 }
347
348 if (Bridge->ResType == PciBarTypeIo16) {
349
350 CalculateApertureIo16 (Bridge);
351 return ;
352 }
353
354 CurrentLink = Bridge->ChildList.ForwardLink;
355
356 //
357 // Assume the bridge is aligned
358 //
359 while (CurrentLink != &Bridge->ChildList) {
360
361 Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
362
363 //
364 // Apply padding resource if available
365 //
366 Offset = Aperture & (Node->Alignment);
367
368 if (Offset != 0) {
369
370 Aperture = Aperture + (Node->Alignment + 1) - Offset;
371
372 }
373
374 //
375 // Recode current aperture as a offset
376 // this offset will be used in future real allocation
377 //
378 Node->Offset = Aperture;
379
380 //
381 // Increment aperture by the length of node
382 //
383 Aperture += Node->Length;
384
385 //
386 // Consider the aperture alignment
387 //
388 CurrentLink = CurrentLink->ForwardLink;
389 }
390
391 //
392 // At last, adjust the aperture with the bridge's
393 // alignment
394 //
395 Offset = Aperture & (Bridge->Alignment);
396 if (Offset != 0) {
397 Aperture = Aperture + (Bridge->Alignment + 1) - Offset;
398 }
399
400 //
401 // If the bridge has already padded the resource and the
402 // amount of padded resource is larger, then keep the
403 // padded resource
404 //
405 if (Bridge->Length < Aperture) {
406 Bridge->Length = Aperture;
407 }
408
409 //
410 // At last, adjust the bridge's alignment to the first child's alignment
411 // if the bridge has at least one child
412 //
413 CurrentLink = Bridge->ChildList.ForwardLink;
414 if (CurrentLink != &Bridge->ChildList) {
415 Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
416 if (Node->Alignment > Bridge->Alignment) {
417 Bridge->Alignment = Node->Alignment;
418 }
419 }
420 }
421
422 /**
423 Get IO/Memory resource infor for given PCI device.
424
425 @param PciDev Pci device instance.
426 @param IoNode Resource info node for IO .
427 @param Mem32Node Resource info node for 32-bit memory.
428 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
429 @param Mem64Node Resource info node for 64-bit memory.
430 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
431
432 **/
433 VOID
434 GetResourceFromDevice (
435 IN PCI_IO_DEVICE *PciDev,
436 IN OUT PCI_RESOURCE_NODE *IoNode,
437 IN OUT PCI_RESOURCE_NODE *Mem32Node,
438 IN OUT PCI_RESOURCE_NODE *PMem32Node,
439 IN OUT PCI_RESOURCE_NODE *Mem64Node,
440 IN OUT PCI_RESOURCE_NODE *PMem64Node
441 )
442 {
443
444 UINT8 Index;
445 PCI_RESOURCE_NODE *Node;
446 BOOLEAN ResourceRequested;
447
448 Node = NULL;
449 ResourceRequested = FALSE;
450
451 for (Index = 0; Index < PCI_MAX_BAR; Index++) {
452
453 switch ((PciDev->PciBar)[Index].BarType) {
454
455 case PciBarTypeMem32:
456
457 Node = CreateResourceNode (
458 PciDev,
459 (PciDev->PciBar)[Index].Length,
460 (PciDev->PciBar)[Index].Alignment,
461 Index,
462 PciBarTypeMem32,
463 PciResUsageTypical
464 );
465
466 InsertResourceNode (
467 Mem32Node,
468 Node
469 );
470
471 ResourceRequested = TRUE;
472 break;
473
474 case PciBarTypeMem64:
475
476 Node = CreateResourceNode (
477 PciDev,
478 (PciDev->PciBar)[Index].Length,
479 (PciDev->PciBar)[Index].Alignment,
480 Index,
481 PciBarTypeMem64,
482 PciResUsageTypical
483 );
484
485 InsertResourceNode (
486 Mem64Node,
487 Node
488 );
489
490 ResourceRequested = TRUE;
491 break;
492
493 case PciBarTypePMem64:
494
495 Node = CreateResourceNode (
496 PciDev,
497 (PciDev->PciBar)[Index].Length,
498 (PciDev->PciBar)[Index].Alignment,
499 Index,
500 PciBarTypePMem64,
501 PciResUsageTypical
502 );
503
504 InsertResourceNode (
505 PMem64Node,
506 Node
507 );
508
509 ResourceRequested = TRUE;
510 break;
511
512 case PciBarTypePMem32:
513
514 Node = CreateResourceNode (
515 PciDev,
516 (PciDev->PciBar)[Index].Length,
517 (PciDev->PciBar)[Index].Alignment,
518 Index,
519 PciBarTypePMem32,
520 PciResUsageTypical
521 );
522
523 InsertResourceNode (
524 PMem32Node,
525 Node
526 );
527 ResourceRequested = TRUE;
528 break;
529
530 case PciBarTypeIo16:
531 case PciBarTypeIo32:
532
533 Node = CreateResourceNode (
534 PciDev,
535 (PciDev->PciBar)[Index].Length,
536 (PciDev->PciBar)[Index].Alignment,
537 Index,
538 PciBarTypeIo16,
539 PciResUsageTypical
540 );
541
542 InsertResourceNode (
543 IoNode,
544 Node
545 );
546 ResourceRequested = TRUE;
547 break;
548
549 case PciBarTypeUnknown:
550 break;
551
552 default:
553 break;
554 }
555 }
556
557 //
558 // Add VF resource
559 //
560 for (Index = 0; Index < PCI_MAX_BAR; Index++) {
561
562 switch ((PciDev->VfPciBar)[Index].BarType) {
563
564 case PciBarTypeMem32:
565
566 Node = CreateVfResourceNode (
567 PciDev,
568 (PciDev->VfPciBar)[Index].Length,
569 (PciDev->VfPciBar)[Index].Alignment,
570 Index,
571 PciBarTypeMem32,
572 PciResUsageTypical
573 );
574
575 InsertResourceNode (
576 Mem32Node,
577 Node
578 );
579
580 break;
581
582 case PciBarTypeMem64:
583
584 Node = CreateVfResourceNode (
585 PciDev,
586 (PciDev->VfPciBar)[Index].Length,
587 (PciDev->VfPciBar)[Index].Alignment,
588 Index,
589 PciBarTypeMem64,
590 PciResUsageTypical
591 );
592
593 InsertResourceNode (
594 Mem64Node,
595 Node
596 );
597
598 break;
599
600 case PciBarTypePMem64:
601
602 Node = CreateVfResourceNode (
603 PciDev,
604 (PciDev->VfPciBar)[Index].Length,
605 (PciDev->VfPciBar)[Index].Alignment,
606 Index,
607 PciBarTypePMem64,
608 PciResUsageTypical
609 );
610
611 InsertResourceNode (
612 PMem64Node,
613 Node
614 );
615
616 break;
617
618 case PciBarTypePMem32:
619
620 Node = CreateVfResourceNode (
621 PciDev,
622 (PciDev->VfPciBar)[Index].Length,
623 (PciDev->VfPciBar)[Index].Alignment,
624 Index,
625 PciBarTypePMem32,
626 PciResUsageTypical
627 );
628
629 InsertResourceNode (
630 PMem32Node,
631 Node
632 );
633 break;
634
635 case PciBarTypeIo16:
636 case PciBarTypeIo32:
637 break;
638
639 case PciBarTypeUnknown:
640 break;
641
642 default:
643 break;
644 }
645 }
646 // If there is no resource requested from this device,
647 // then we indicate this device has been allocated naturally.
648 //
649 if (!ResourceRequested) {
650 PciDev->Allocated = TRUE;
651 }
652 }
653
654 /**
655 This function is used to create a resource node.
656
657 @param PciDev Pci device instance.
658 @param Length Length of Io/Memory resource.
659 @param Alignment Alignment of resource.
660 @param Bar Bar index.
661 @param ResType Type of resource: IO/Memory.
662 @param ResUsage Resource usage.
663
664 @return PCI resource node created for given PCI device.
665 NULL means PCI resource node is not created.
666
667 **/
668 PCI_RESOURCE_NODE *
669 CreateResourceNode (
670 IN PCI_IO_DEVICE *PciDev,
671 IN UINT64 Length,
672 IN UINT64 Alignment,
673 IN UINT8 Bar,
674 IN PCI_BAR_TYPE ResType,
675 IN PCI_RESOURCE_USAGE ResUsage
676 )
677 {
678 PCI_RESOURCE_NODE *Node;
679
680 Node = NULL;
681
682 Node = AllocateZeroPool (sizeof (PCI_RESOURCE_NODE));
683 ASSERT (Node != NULL);
684 if (Node == NULL) {
685 return NULL;
686 }
687
688 Node->Signature = PCI_RESOURCE_SIGNATURE;
689 Node->PciDev = PciDev;
690 Node->Length = Length;
691 Node->Alignment = Alignment;
692 Node->Bar = Bar;
693 Node->ResType = ResType;
694 Node->Reserved = FALSE;
695 Node->ResourceUsage = ResUsage;
696 InitializeListHead (&Node->ChildList);
697
698 return Node;
699 }
700
701 /**
702 This function is used to create a IOV VF resource node.
703
704 @param PciDev Pci device instance.
705 @param Length Length of Io/Memory resource.
706 @param Alignment Alignment of resource.
707 @param Bar Bar index.
708 @param ResType Type of resource: IO/Memory.
709 @param ResUsage Resource usage.
710
711 @return PCI resource node created for given VF PCI device.
712 NULL means PCI resource node is not created.
713
714 **/
715 PCI_RESOURCE_NODE *
716 CreateVfResourceNode (
717 IN PCI_IO_DEVICE *PciDev,
718 IN UINT64 Length,
719 IN UINT64 Alignment,
720 IN UINT8 Bar,
721 IN PCI_BAR_TYPE ResType,
722 IN PCI_RESOURCE_USAGE ResUsage
723 )
724 {
725 PCI_RESOURCE_NODE *Node;
726
727 Node = CreateResourceNode (PciDev, Length, Alignment, Bar, ResType, ResUsage);
728 if (Node == NULL) {
729 return Node;
730 }
731
732 Node->Virtual = TRUE;
733
734 return Node;
735 }
736
737 /**
738 This function is used to extract resource request from
739 device node list.
740
741 @param Bridge Pci device instance.
742 @param IoNode Resource info node for IO.
743 @param Mem32Node Resource info node for 32-bit memory.
744 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
745 @param Mem64Node Resource info node for 64-bit memory.
746 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
747
748 **/
749 VOID
750 CreateResourceMap (
751 IN PCI_IO_DEVICE *Bridge,
752 IN OUT PCI_RESOURCE_NODE *IoNode,
753 IN OUT PCI_RESOURCE_NODE *Mem32Node,
754 IN OUT PCI_RESOURCE_NODE *PMem32Node,
755 IN OUT PCI_RESOURCE_NODE *Mem64Node,
756 IN OUT PCI_RESOURCE_NODE *PMem64Node
757 )
758 {
759 PCI_IO_DEVICE *Temp;
760 PCI_RESOURCE_NODE *IoBridge;
761 PCI_RESOURCE_NODE *Mem32Bridge;
762 PCI_RESOURCE_NODE *PMem32Bridge;
763 PCI_RESOURCE_NODE *Mem64Bridge;
764 PCI_RESOURCE_NODE *PMem64Bridge;
765 LIST_ENTRY *CurrentLink;
766
767 CurrentLink = Bridge->ChildList.ForwardLink;
768
769 while (CurrentLink != NULL && CurrentLink != &Bridge->ChildList) {
770
771 Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);
772
773 //
774 // Create resource nodes for this device by scanning the
775 // Bar array in the device private data
776 // If the upstream bridge doesn't support this device,
777 // no any resource node will be created for this device
778 //
779 GetResourceFromDevice (
780 Temp,
781 IoNode,
782 Mem32Node,
783 PMem32Node,
784 Mem64Node,
785 PMem64Node
786 );
787
788 if (IS_PCI_BRIDGE (&Temp->Pci)) {
789
790 //
791 // If the device has children, create a bridge resource node for this PPB
792 // Note: For PPB, memory aperture is aligned with 1MB and IO aperture
793 // is aligned with 4KB (smaller alignments may be supported).
794 //
795 IoBridge = CreateResourceNode (
796 Temp,
797 0,
798 Temp->BridgeIoAlignment,
799 PPB_IO_RANGE,
800 PciBarTypeIo16,
801 PciResUsageTypical
802 );
803
804 Mem32Bridge = CreateResourceNode (
805 Temp,
806 0,
807 0xFFFFF,
808 PPB_MEM32_RANGE,
809 PciBarTypeMem32,
810 PciResUsageTypical
811 );
812
813 PMem32Bridge = CreateResourceNode (
814 Temp,
815 0,
816 0xFFFFF,
817 PPB_PMEM32_RANGE,
818 PciBarTypePMem32,
819 PciResUsageTypical
820 );
821
822 Mem64Bridge = CreateResourceNode (
823 Temp,
824 0,
825 0xFFFFF,
826 PPB_MEM64_RANGE,
827 PciBarTypeMem64,
828 PciResUsageTypical
829 );
830
831 PMem64Bridge = CreateResourceNode (
832 Temp,
833 0,
834 0xFFFFF,
835 PPB_PMEM64_RANGE,
836 PciBarTypePMem64,
837 PciResUsageTypical
838 );
839
840 //
841 // Recursively create resouce map on this bridge
842 //
843 CreateResourceMap (
844 Temp,
845 IoBridge,
846 Mem32Bridge,
847 PMem32Bridge,
848 Mem64Bridge,
849 PMem64Bridge
850 );
851
852 if (ResourceRequestExisted (IoBridge)) {
853 InsertResourceNode (
854 IoNode,
855 IoBridge
856 );
857 } else {
858 FreePool (IoBridge);
859 IoBridge = NULL;
860 }
861
862 //
863 // If there is node under this resource bridge,
864 // then calculate bridge's aperture of this type
865 // and insert it into the respective resource tree.
866 // If no, delete this resource bridge
867 //
868 if (ResourceRequestExisted (Mem32Bridge)) {
869 InsertResourceNode (
870 Mem32Node,
871 Mem32Bridge
872 );
873 } else {
874 FreePool (Mem32Bridge);
875 Mem32Bridge = NULL;
876 }
877
878 //
879 // If there is node under this resource bridge,
880 // then calculate bridge's aperture of this type
881 // and insert it into the respective resource tree.
882 // If no, delete this resource bridge
883 //
884 if (ResourceRequestExisted (PMem32Bridge)) {
885 InsertResourceNode (
886 PMem32Node,
887 PMem32Bridge
888 );
889 } else {
890 FreePool (PMem32Bridge);
891 PMem32Bridge = NULL;
892 }
893
894 //
895 // If there is node under this resource bridge,
896 // then calculate bridge's aperture of this type
897 // and insert it into the respective resource tree.
898 // If no, delete this resource bridge
899 //
900 if (ResourceRequestExisted (Mem64Bridge)) {
901 InsertResourceNode (
902 Mem64Node,
903 Mem64Bridge
904 );
905 } else {
906 FreePool (Mem64Bridge);
907 Mem64Bridge = NULL;
908 }
909
910 //
911 // If there is node under this resource bridge,
912 // then calculate bridge's aperture of this type
913 // and insert it into the respective resource tree.
914 // If no, delete this resource bridge
915 //
916 if (ResourceRequestExisted (PMem64Bridge)) {
917 InsertResourceNode (
918 PMem64Node,
919 PMem64Bridge
920 );
921 } else {
922 FreePool (PMem64Bridge);
923 PMem64Bridge = NULL;
924 }
925
926 }
927
928 //
929 // If it is P2C, apply hard coded resource padding
930 //
931 if (IS_CARDBUS_BRIDGE (&Temp->Pci)) {
932 ResourcePaddingForCardBusBridge (
933 Temp,
934 IoNode,
935 Mem32Node,
936 PMem32Node,
937 Mem64Node,
938 PMem64Node
939 );
940 }
941
942 CurrentLink = CurrentLink->ForwardLink;
943 }
944
945 //
946 // To do some platform specific resource padding ...
947 //
948 ResourcePaddingPolicy (
949 Bridge,
950 IoNode,
951 Mem32Node,
952 PMem32Node,
953 Mem64Node,
954 PMem64Node
955 );
956
957 //
958 // Degrade resource if necessary
959 //
960 DegradeResource (
961 Bridge,
962 Mem32Node,
963 PMem32Node,
964 Mem64Node,
965 PMem64Node
966 );
967
968 //
969 // Calculate resource aperture for this bridge device
970 //
971 CalculateResourceAperture (Mem32Node);
972 CalculateResourceAperture (PMem32Node);
973 CalculateResourceAperture (Mem64Node);
974 CalculateResourceAperture (PMem64Node);
975 CalculateResourceAperture (IoNode);
976 }
977
978 /**
979 This function is used to do the resource padding for a specific platform.
980
981 @param PciDev Pci device instance.
982 @param IoNode Resource info node for IO.
983 @param Mem32Node Resource info node for 32-bit memory.
984 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
985 @param Mem64Node Resource info node for 64-bit memory.
986 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
987
988 **/
989 VOID
990 ResourcePaddingPolicy (
991 IN PCI_IO_DEVICE *PciDev,
992 IN PCI_RESOURCE_NODE *IoNode,
993 IN PCI_RESOURCE_NODE *Mem32Node,
994 IN PCI_RESOURCE_NODE *PMem32Node,
995 IN PCI_RESOURCE_NODE *Mem64Node,
996 IN PCI_RESOURCE_NODE *PMem64Node
997 )
998 {
999 //
1000 // Create padding resource node
1001 //
1002 if (PciDev->ResourcePaddingDescriptors != NULL) {
1003 ApplyResourcePadding (
1004 PciDev,
1005 IoNode,
1006 Mem32Node,
1007 PMem32Node,
1008 Mem64Node,
1009 PMem64Node
1010 );
1011 }
1012 }
1013
1014 /**
1015 This function is used to degrade resource if the upstream bridge
1016 doesn't support certain resource. Degradation path is
1017 PMEM64 -> MEM64 -> MEM32
1018 PMEM64 -> PMEM32 -> MEM32
1019 IO32 -> IO16.
1020
1021 @param Bridge Pci device instance.
1022 @param Mem32Node Resource info node for 32-bit memory.
1023 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
1024 @param Mem64Node Resource info node for 64-bit memory.
1025 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
1026
1027 **/
1028 VOID
1029 DegradeResource (
1030 IN PCI_IO_DEVICE *Bridge,
1031 IN PCI_RESOURCE_NODE *Mem32Node,
1032 IN PCI_RESOURCE_NODE *PMem32Node,
1033 IN PCI_RESOURCE_NODE *Mem64Node,
1034 IN PCI_RESOURCE_NODE *PMem64Node
1035 )
1036 {
1037 PCI_IO_DEVICE *Temp;
1038 LIST_ENTRY *ChildDeviceLink;
1039 LIST_ENTRY *ChildNodeLink;
1040 LIST_ENTRY *NextChildNodeLink;
1041 PCI_RESOURCE_NODE *TempNode;
1042
1043 //
1044 // If any child device has both option ROM and 64-bit BAR, degrade its PMEM64/MEM64
1045 // requests in case that if a legacy option ROM image can not access 64-bit resources.
1046 //
1047 ChildDeviceLink = Bridge->ChildList.ForwardLink;
1048 while (ChildDeviceLink != NULL && ChildDeviceLink != &Bridge->ChildList) {
1049 Temp = PCI_IO_DEVICE_FROM_LINK (ChildDeviceLink);
1050 if (Temp->RomSize != 0) {
1051 if (!IsListEmpty (&Mem64Node->ChildList)) {
1052 ChildNodeLink = Mem64Node->ChildList.ForwardLink;
1053 while (ChildNodeLink != &Mem64Node->ChildList) {
1054 TempNode = RESOURCE_NODE_FROM_LINK (ChildNodeLink);
1055 NextChildNodeLink = ChildNodeLink->ForwardLink;
1056
1057 if (TempNode->PciDev == Temp) {
1058 RemoveEntryList (ChildNodeLink);
1059 InsertResourceNode (Mem32Node, TempNode);
1060 }
1061 ChildNodeLink = NextChildNodeLink;
1062 }
1063 }
1064
1065 if (!IsListEmpty (&PMem64Node->ChildList)) {
1066 ChildNodeLink = PMem64Node->ChildList.ForwardLink;
1067 while (ChildNodeLink != &PMem64Node->ChildList) {
1068 TempNode = RESOURCE_NODE_FROM_LINK (ChildNodeLink);
1069 NextChildNodeLink = ChildNodeLink->ForwardLink;
1070
1071 if (TempNode->PciDev == Temp) {
1072 RemoveEntryList (ChildNodeLink);
1073 InsertResourceNode (PMem32Node, TempNode);
1074 }
1075 ChildNodeLink = NextChildNodeLink;
1076 }
1077 }
1078
1079 }
1080 ChildDeviceLink = ChildDeviceLink->ForwardLink;
1081 }
1082
1083 //
1084 // If firmware is in 32-bit mode,
1085 // then degrade PMEM64/MEM64 requests
1086 //
1087 if (sizeof (UINTN) <= 4) {
1088 MergeResourceTree (
1089 Mem32Node,
1090 Mem64Node,
1091 TRUE
1092 );
1093
1094 MergeResourceTree (
1095 PMem32Node,
1096 PMem64Node,
1097 TRUE
1098 );
1099 } else {
1100 //
1101 // if the bridge does not support MEM64, degrade MEM64 to MEM32
1102 //
1103 if (!BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_MEM64_DECODE_SUPPORTED)) {
1104 MergeResourceTree (
1105 Mem32Node,
1106 Mem64Node,
1107 TRUE
1108 );
1109 }
1110
1111 //
1112 // if the bridge does not support PMEM64, degrade PMEM64 to PMEM32
1113 //
1114 if (!BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_PMEM64_DECODE_SUPPORTED)) {
1115 MergeResourceTree (
1116 PMem32Node,
1117 PMem64Node,
1118 TRUE
1119 );
1120 }
1121
1122 //
1123 // if both PMEM64 and PMEM32 requests from child devices, which can not be satisfied
1124 // by a P2P bridge simultaneously, keep PMEM64 and degrade PMEM32 to MEM32.
1125 //
1126 if (!IsListEmpty (&PMem64Node->ChildList) && Bridge->Parent != NULL) {
1127 MergeResourceTree (
1128 Mem32Node,
1129 PMem32Node,
1130 TRUE
1131 );
1132 }
1133 }
1134
1135 //
1136 // If bridge doesn't support Pmem32
1137 // degrade it to mem32
1138 //
1139 if (!BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_PMEM32_DECODE_SUPPORTED)) {
1140 MergeResourceTree (
1141 Mem32Node,
1142 PMem32Node,
1143 TRUE
1144 );
1145 }
1146
1147 //
1148 // if root bridge supports combined Pmem Mem decoding
1149 // merge these two type of resource
1150 //
1151 if (BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_PMEM_MEM_COMBINE_SUPPORTED)) {
1152 MergeResourceTree (
1153 Mem32Node,
1154 PMem32Node,
1155 FALSE
1156 );
1157
1158 //
1159 // No need to check if to degrade MEM64 after merge, because
1160 // if there are PMEM64 still here, 64-bit decode should be supported
1161 // by the root bride.
1162 //
1163 MergeResourceTree (
1164 Mem64Node,
1165 PMem64Node,
1166 FALSE
1167 );
1168 }
1169 }
1170
1171 /**
1172 Test whether bridge device support decode resource.
1173
1174 @param Bridge Bridge device instance.
1175 @param Decode Decode type according to resource type.
1176
1177 @return TRUE The bridge device support decode resource.
1178 @return FALSE The bridge device don't support decode resource.
1179
1180 **/
1181 BOOLEAN
1182 BridgeSupportResourceDecode (
1183 IN PCI_IO_DEVICE *Bridge,
1184 IN UINT32 Decode
1185 )
1186 {
1187 if (((Bridge->Decodes) & Decode) != 0) {
1188 return TRUE;
1189 }
1190
1191 return FALSE;
1192 }
1193
1194 /**
1195 This function is used to program the resource allocated
1196 for each resource node under specified bridge.
1197
1198 @param Base Base address of resource to be progammed.
1199 @param Bridge PCI resource node for the bridge device.
1200
1201 @retval EFI_SUCCESS Successfully to program all resouces
1202 on given PCI bridge device.
1203 @retval EFI_OUT_OF_RESOURCES Base is all one.
1204
1205 **/
1206 EFI_STATUS
1207 ProgramResource (
1208 IN UINT64 Base,
1209 IN PCI_RESOURCE_NODE *Bridge
1210 )
1211 {
1212 LIST_ENTRY *CurrentLink;
1213 PCI_RESOURCE_NODE *Node;
1214 EFI_STATUS Status;
1215
1216 if (Base == gAllOne) {
1217 return EFI_OUT_OF_RESOURCES;
1218 }
1219
1220 CurrentLink = Bridge->ChildList.ForwardLink;
1221
1222 while (CurrentLink != &Bridge->ChildList) {
1223
1224 Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
1225
1226 if (!IS_PCI_BRIDGE (&(Node->PciDev->Pci))) {
1227
1228 if (IS_CARDBUS_BRIDGE (&(Node->PciDev->Pci))) {
1229 //
1230 // Program the PCI Card Bus device
1231 //
1232 ProgramP2C (Base, Node);
1233 } else {
1234 //
1235 // Program the PCI device BAR
1236 //
1237 ProgramBar (Base, Node);
1238 }
1239 } else {
1240 //
1241 // Program the PCI devices under this bridge
1242 //
1243 Status = ProgramResource (Base + Node->Offset, Node);
1244 if (EFI_ERROR (Status)) {
1245 return Status;
1246 }
1247
1248 ProgramPpbApperture (Base, Node);
1249 }
1250
1251 CurrentLink = CurrentLink->ForwardLink;
1252 }
1253
1254 return EFI_SUCCESS;
1255 }
1256
1257 /**
1258 Program Bar register for PCI device.
1259
1260 @param Base Base address for PCI device resource to be progammed.
1261 @param Node Point to resoure node structure.
1262
1263 **/
1264 VOID
1265 ProgramBar (
1266 IN UINT64 Base,
1267 IN PCI_RESOURCE_NODE *Node
1268 )
1269 {
1270 EFI_PCI_IO_PROTOCOL *PciIo;
1271 UINT64 Address;
1272 UINT32 Address32;
1273
1274 ASSERT (Node->Bar < PCI_MAX_BAR);
1275
1276 //
1277 // Check VF BAR
1278 //
1279 if (Node->Virtual) {
1280 ProgramVfBar (Base, Node);
1281 return;
1282 }
1283
1284 Address = 0;
1285 PciIo = &(Node->PciDev->PciIo);
1286
1287 Address = Base + Node->Offset;
1288
1289 //
1290 // Indicate pci bus driver has allocated
1291 // resource for this device
1292 // It might be a temporary solution here since
1293 // pci device could have multiple bar
1294 //
1295 Node->PciDev->Allocated = TRUE;
1296
1297 switch ((Node->PciDev->PciBar[Node->Bar]).BarType) {
1298
1299 case PciBarTypeIo16:
1300 case PciBarTypeIo32:
1301 case PciBarTypeMem32:
1302 case PciBarTypePMem32:
1303
1304 PciIo->Pci.Write (
1305 PciIo,
1306 EfiPciIoWidthUint32,
1307 (Node->PciDev->PciBar[Node->Bar]).Offset,
1308 1,
1309 &Address
1310 );
1311
1312 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1313
1314 break;
1315
1316 case PciBarTypeMem64:
1317 case PciBarTypePMem64:
1318
1319 Address32 = (UINT32) (Address & 0x00000000FFFFFFFF);
1320
1321 PciIo->Pci.Write (
1322 PciIo,
1323 EfiPciIoWidthUint32,
1324 (Node->PciDev->PciBar[Node->Bar]).Offset,
1325 1,
1326 &Address32
1327 );
1328
1329 Address32 = (UINT32) RShiftU64 (Address, 32);
1330
1331 PciIo->Pci.Write (
1332 PciIo,
1333 EfiPciIoWidthUint32,
1334 (UINT8) ((Node->PciDev->PciBar[Node->Bar]).Offset + 4),
1335 1,
1336 &Address32
1337 );
1338
1339 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1340
1341 break;
1342
1343 default:
1344 break;
1345 }
1346 }
1347
1348 /**
1349 Program IOV VF Bar register for PCI device.
1350
1351 @param Base Base address for PCI device resource to be progammed.
1352 @param Node Point to resoure node structure.
1353
1354 **/
1355 EFI_STATUS
1356 ProgramVfBar (
1357 IN UINT64 Base,
1358 IN PCI_RESOURCE_NODE *Node
1359 )
1360 {
1361 EFI_PCI_IO_PROTOCOL *PciIo;
1362 UINT64 Address;
1363 UINT32 Address32;
1364
1365 ASSERT (Node->Bar < PCI_MAX_BAR);
1366 ASSERT (Node->Virtual);
1367
1368 Address = 0;
1369 PciIo = &(Node->PciDev->PciIo);
1370
1371 Address = Base + Node->Offset;
1372
1373 //
1374 // Indicate pci bus driver has allocated
1375 // resource for this device
1376 // It might be a temporary solution here since
1377 // pci device could have multiple bar
1378 //
1379 Node->PciDev->Allocated = TRUE;
1380
1381 switch ((Node->PciDev->VfPciBar[Node->Bar]).BarType) {
1382
1383 case PciBarTypeMem32:
1384 case PciBarTypePMem32:
1385
1386 PciIo->Pci.Write (
1387 PciIo,
1388 EfiPciIoWidthUint32,
1389 (Node->PciDev->VfPciBar[Node->Bar]).Offset,
1390 1,
1391 &Address
1392 );
1393
1394 Node->PciDev->VfPciBar[Node->Bar].BaseAddress = Address;
1395 break;
1396
1397 case PciBarTypeMem64:
1398 case PciBarTypePMem64:
1399
1400 Address32 = (UINT32) (Address & 0x00000000FFFFFFFF);
1401
1402 PciIo->Pci.Write (
1403 PciIo,
1404 EfiPciIoWidthUint32,
1405 (Node->PciDev->VfPciBar[Node->Bar]).Offset,
1406 1,
1407 &Address32
1408 );
1409
1410 Address32 = (UINT32) RShiftU64 (Address, 32);
1411
1412 PciIo->Pci.Write (
1413 PciIo,
1414 EfiPciIoWidthUint32,
1415 ((Node->PciDev->VfPciBar[Node->Bar]).Offset + 4),
1416 1,
1417 &Address32
1418 );
1419
1420 Node->PciDev->VfPciBar[Node->Bar].BaseAddress = Address;
1421 break;
1422
1423 case PciBarTypeIo16:
1424 case PciBarTypeIo32:
1425 break;
1426
1427 default:
1428 break;
1429 }
1430
1431 return EFI_SUCCESS;
1432 }
1433
1434 /**
1435 Program PCI-PCI bridge apperture.
1436
1437 @param Base Base address for resource.
1438 @param Node Point to resoure node structure.
1439
1440 **/
1441 VOID
1442 ProgramPpbApperture (
1443 IN UINT64 Base,
1444 IN PCI_RESOURCE_NODE *Node
1445 )
1446 {
1447 EFI_PCI_IO_PROTOCOL *PciIo;
1448 UINT64 Address;
1449 UINT32 Address32;
1450
1451 Address = 0;
1452 //
1453 // If no device resource of this PPB, return anyway
1454 // Apperture is set default in the initialization code
1455 //
1456 if (Node->Length == 0 || Node->ResourceUsage == PciResUsagePadding) {
1457 //
1458 // For padding resource node, just ignore when programming
1459 //
1460 return ;
1461 }
1462
1463 PciIo = &(Node->PciDev->PciIo);
1464 Address = Base + Node->Offset;
1465
1466 //
1467 // Indicate the PPB resource has been allocated
1468 //
1469 Node->PciDev->Allocated = TRUE;
1470
1471 switch (Node->Bar) {
1472
1473 case PPB_BAR_0:
1474 case PPB_BAR_1:
1475 switch ((Node->PciDev->PciBar[Node->Bar]).BarType) {
1476
1477 case PciBarTypeIo16:
1478 case PciBarTypeIo32:
1479 case PciBarTypeMem32:
1480 case PciBarTypePMem32:
1481
1482 PciIo->Pci.Write (
1483 PciIo,
1484 EfiPciIoWidthUint32,
1485 (Node->PciDev->PciBar[Node->Bar]).Offset,
1486 1,
1487 &Address
1488 );
1489
1490 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1491 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1492 break;
1493
1494 case PciBarTypeMem64:
1495 case PciBarTypePMem64:
1496
1497 Address32 = (UINT32) (Address & 0x00000000FFFFFFFF);
1498
1499 PciIo->Pci.Write (
1500 PciIo,
1501 EfiPciIoWidthUint32,
1502 (Node->PciDev->PciBar[Node->Bar]).Offset,
1503 1,
1504 &Address32
1505 );
1506
1507 Address32 = (UINT32) RShiftU64 (Address, 32);
1508
1509 PciIo->Pci.Write (
1510 PciIo,
1511 EfiPciIoWidthUint32,
1512 (UINT8) ((Node->PciDev->PciBar[Node->Bar]).Offset + 4),
1513 1,
1514 &Address32
1515 );
1516
1517 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1518 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1519 break;
1520
1521 default:
1522 break;
1523 }
1524 break;
1525
1526 case PPB_IO_RANGE:
1527
1528 Address32 = ((UINT32) (Address)) >> 8;
1529 PciIo->Pci.Write (
1530 PciIo,
1531 EfiPciIoWidthUint8,
1532 0x1C,
1533 1,
1534 &Address32
1535 );
1536
1537 Address32 >>= 8;
1538 PciIo->Pci.Write (
1539 PciIo,
1540 EfiPciIoWidthUint16,
1541 0x30,
1542 1,
1543 &Address32
1544 );
1545
1546 Address32 = (UINT32) (Address + Node->Length - 1);
1547 Address32 = ((UINT32) (Address32)) >> 8;
1548 PciIo->Pci.Write (
1549 PciIo,
1550 EfiPciIoWidthUint8,
1551 0x1D,
1552 1,
1553 &Address32
1554 );
1555
1556 Address32 >>= 8;
1557 PciIo->Pci.Write (
1558 PciIo,
1559 EfiPciIoWidthUint16,
1560 0x32,
1561 1,
1562 &Address32
1563 );
1564
1565 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1566 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1567 break;
1568
1569 case PPB_MEM32_RANGE:
1570
1571 Address32 = ((UINT32) (Address)) >> 16;
1572 PciIo->Pci.Write (
1573 PciIo,
1574 EfiPciIoWidthUint16,
1575 0x20,
1576 1,
1577 &Address32
1578 );
1579
1580 Address32 = (UINT32) (Address + Node->Length - 1);
1581 Address32 = ((UINT32) (Address32)) >> 16;
1582 PciIo->Pci.Write (
1583 PciIo,
1584 EfiPciIoWidthUint16,
1585 0x22,
1586 1,
1587 &Address32
1588 );
1589
1590 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1591 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1592 break;
1593
1594 case PPB_PMEM32_RANGE:
1595 case PPB_PMEM64_RANGE:
1596
1597 Address32 = ((UINT32) (Address)) >> 16;
1598 PciIo->Pci.Write (
1599 PciIo,
1600 EfiPciIoWidthUint16,
1601 0x24,
1602 1,
1603 &Address32
1604 );
1605
1606 Address32 = (UINT32) (Address + Node->Length - 1);
1607 Address32 = ((UINT32) (Address32)) >> 16;
1608 PciIo->Pci.Write (
1609 PciIo,
1610 EfiPciIoWidthUint16,
1611 0x26,
1612 1,
1613 &Address32
1614 );
1615
1616 Address32 = (UINT32) RShiftU64 (Address, 32);
1617 PciIo->Pci.Write (
1618 PciIo,
1619 EfiPciIoWidthUint32,
1620 0x28,
1621 1,
1622 &Address32
1623 );
1624
1625 Address32 = (UINT32) RShiftU64 ((Address + Node->Length - 1), 32);
1626 PciIo->Pci.Write (
1627 PciIo,
1628 EfiPciIoWidthUint32,
1629 0x2C,
1630 1,
1631 &Address32
1632 );
1633
1634 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1635 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1636 break;
1637
1638 default:
1639 break;
1640 }
1641 }
1642
1643 /**
1644 Program parent bridge for Option Rom.
1645
1646 @param PciDevice Pci deivce instance.
1647 @param OptionRomBase Base address for Optiona Rom.
1648 @param Enable Enable or disable PCI memory.
1649
1650 **/
1651 VOID
1652 ProgrameUpstreamBridgeForRom (
1653 IN PCI_IO_DEVICE *PciDevice,
1654 IN UINT32 OptionRomBase,
1655 IN BOOLEAN Enable
1656 )
1657 {
1658 PCI_IO_DEVICE *Parent;
1659 PCI_RESOURCE_NODE Node;
1660 //
1661 // For root bridge, just return.
1662 //
1663 Parent = PciDevice->Parent;
1664 ZeroMem (&Node, sizeof (Node));
1665 while (Parent != NULL) {
1666 if (!IS_PCI_BRIDGE (&Parent->Pci)) {
1667 break;
1668 }
1669
1670 Node.PciDev = Parent;
1671 Node.Length = PciDevice->RomSize;
1672 Node.Alignment = 0;
1673 Node.Bar = PPB_MEM32_RANGE;
1674 Node.ResType = PciBarTypeMem32;
1675 Node.Offset = 0;
1676
1677 //
1678 // Program PPB to only open a single <= 16MB apperture
1679 //
1680 if (Enable) {
1681 ProgramPpbApperture (OptionRomBase, &Node);
1682 PCI_ENABLE_COMMAND_REGISTER (Parent, EFI_PCI_COMMAND_MEMORY_SPACE);
1683 } else {
1684 InitializePpb (Parent);
1685 PCI_DISABLE_COMMAND_REGISTER (Parent, EFI_PCI_COMMAND_MEMORY_SPACE);
1686 }
1687
1688 Parent = Parent->Parent;
1689 }
1690 }
1691
1692 /**
1693 Test whether resource exists for a bridge.
1694
1695 @param Bridge Point to resource node for a bridge.
1696
1697 @retval TRUE There is resource on the given bridge.
1698 @retval FALSE There isn't resource on the given bridge.
1699
1700 **/
1701 BOOLEAN
1702 ResourceRequestExisted (
1703 IN PCI_RESOURCE_NODE *Bridge
1704 )
1705 {
1706 if (Bridge != NULL) {
1707 if (!IsListEmpty (&Bridge->ChildList) || Bridge->Length != 0) {
1708 return TRUE;
1709 }
1710 }
1711
1712 return FALSE;
1713 }
1714
1715 /**
1716 Initialize resource pool structure.
1717
1718 @param ResourcePool Point to resource pool structure. This pool
1719 is reset to all zero when returned.
1720 @param ResourceType Type of resource.
1721
1722 **/
1723 VOID
1724 InitializeResourcePool (
1725 IN OUT PCI_RESOURCE_NODE *ResourcePool,
1726 IN PCI_BAR_TYPE ResourceType
1727 )
1728 {
1729 ZeroMem (ResourcePool, sizeof (PCI_RESOURCE_NODE));
1730 ResourcePool->ResType = ResourceType;
1731 ResourcePool->Signature = PCI_RESOURCE_SIGNATURE;
1732 InitializeListHead (&ResourcePool->ChildList);
1733 }
1734
1735 /**
1736 Destory given resource tree.
1737
1738 @param Bridge PCI resource root node of resource tree.
1739
1740 **/
1741 VOID
1742 DestroyResourceTree (
1743 IN PCI_RESOURCE_NODE *Bridge
1744 )
1745 {
1746 PCI_RESOURCE_NODE *Temp;
1747 LIST_ENTRY *CurrentLink;
1748
1749 while (!IsListEmpty (&Bridge->ChildList)) {
1750
1751 CurrentLink = Bridge->ChildList.ForwardLink;
1752
1753 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
1754 ASSERT (Temp);
1755
1756 RemoveEntryList (CurrentLink);
1757
1758 if (IS_PCI_BRIDGE (&(Temp->PciDev->Pci))) {
1759 DestroyResourceTree (Temp);
1760 }
1761
1762 FreePool (Temp);
1763 }
1764 }
1765
1766 /**
1767 Insert resource padding for P2C.
1768
1769 @param PciDev Pci device instance.
1770 @param IoNode Resource info node for IO.
1771 @param Mem32Node Resource info node for 32-bit memory.
1772 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
1773 @param Mem64Node Resource info node for 64-bit memory.
1774 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
1775
1776 **/
1777 VOID
1778 ResourcePaddingForCardBusBridge (
1779 IN PCI_IO_DEVICE *PciDev,
1780 IN PCI_RESOURCE_NODE *IoNode,
1781 IN PCI_RESOURCE_NODE *Mem32Node,
1782 IN PCI_RESOURCE_NODE *PMem32Node,
1783 IN PCI_RESOURCE_NODE *Mem64Node,
1784 IN PCI_RESOURCE_NODE *PMem64Node
1785 )
1786 {
1787 PCI_RESOURCE_NODE *Node;
1788
1789 Node = NULL;
1790
1791 //
1792 // Memory Base/Limit Register 0
1793 // Bar 1 denodes memory range 0
1794 //
1795 Node = CreateResourceNode (
1796 PciDev,
1797 0x2000000,
1798 0x1ffffff,
1799 1,
1800 PciBarTypeMem32,
1801 PciResUsagePadding
1802 );
1803
1804 InsertResourceNode (
1805 Mem32Node,
1806 Node
1807 );
1808
1809 //
1810 // Memory Base/Limit Register 1
1811 // Bar 2 denodes memory range1
1812 //
1813 Node = CreateResourceNode (
1814 PciDev,
1815 0x2000000,
1816 0x1ffffff,
1817 2,
1818 PciBarTypePMem32,
1819 PciResUsagePadding
1820 );
1821
1822 InsertResourceNode (
1823 PMem32Node,
1824 Node
1825 );
1826
1827 //
1828 // Io Base/Limit
1829 // Bar 3 denodes io range 0
1830 //
1831 Node = CreateResourceNode (
1832 PciDev,
1833 0x100,
1834 0xff,
1835 3,
1836 PciBarTypeIo16,
1837 PciResUsagePadding
1838 );
1839
1840 InsertResourceNode (
1841 IoNode,
1842 Node
1843 );
1844
1845 //
1846 // Io Base/Limit
1847 // Bar 4 denodes io range 0
1848 //
1849 Node = CreateResourceNode (
1850 PciDev,
1851 0x100,
1852 0xff,
1853 4,
1854 PciBarTypeIo16,
1855 PciResUsagePadding
1856 );
1857
1858 InsertResourceNode (
1859 IoNode,
1860 Node
1861 );
1862 }
1863
1864 /**
1865 Program PCI Card device register for given resource node.
1866
1867 @param Base Base address of PCI Card device to be programmed.
1868 @param Node Given resource node.
1869
1870 **/
1871 VOID
1872 ProgramP2C (
1873 IN UINT64 Base,
1874 IN PCI_RESOURCE_NODE *Node
1875 )
1876 {
1877 EFI_PCI_IO_PROTOCOL *PciIo;
1878 UINT64 Address;
1879 UINT64 TempAddress;
1880 UINT16 BridgeControl;
1881
1882 Address = 0;
1883 PciIo = &(Node->PciDev->PciIo);
1884
1885 Address = Base + Node->Offset;
1886
1887 //
1888 // Indicate pci bus driver has allocated
1889 // resource for this device
1890 // It might be a temporary solution here since
1891 // pci device could have multiple bar
1892 //
1893 Node->PciDev->Allocated = TRUE;
1894
1895 switch (Node->Bar) {
1896
1897 case P2C_BAR_0:
1898 PciIo->Pci.Write (
1899 PciIo,
1900 EfiPciIoWidthUint32,
1901 (Node->PciDev->PciBar[Node->Bar]).Offset,
1902 1,
1903 &Address
1904 );
1905
1906 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1907 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1908 break;
1909
1910 case P2C_MEM_1:
1911 PciIo->Pci.Write (
1912 PciIo,
1913 EfiPciIoWidthUint32,
1914 PCI_CARD_MEMORY_BASE_0,
1915 1,
1916 &Address
1917 );
1918
1919 TempAddress = Address + Node->Length - 1;
1920 PciIo->Pci.Write (
1921 PciIo,
1922 EfiPciIoWidthUint32,
1923 PCI_CARD_MEMORY_LIMIT_0,
1924 1,
1925 &TempAddress
1926 );
1927
1928 if (Node->ResType == PciBarTypeMem32) {
1929 //
1930 // Set non-prefetchable bit
1931 //
1932 PciIo->Pci.Read (
1933 PciIo,
1934 EfiPciIoWidthUint16,
1935 PCI_CARD_BRIDGE_CONTROL,
1936 1,
1937 &BridgeControl
1938 );
1939
1940 BridgeControl &= (UINT16) ~PCI_CARD_PREFETCHABLE_MEMORY_0_ENABLE;
1941 PciIo->Pci.Write (
1942 PciIo,
1943 EfiPciIoWidthUint16,
1944 PCI_CARD_BRIDGE_CONTROL,
1945 1,
1946 &BridgeControl
1947 );
1948
1949 } else {
1950 //
1951 // Set pre-fetchable bit
1952 //
1953 PciIo->Pci.Read (
1954 PciIo,
1955 EfiPciIoWidthUint16,
1956 PCI_CARD_BRIDGE_CONTROL,
1957 1,
1958 &BridgeControl
1959 );
1960
1961 BridgeControl |= PCI_CARD_PREFETCHABLE_MEMORY_0_ENABLE;
1962 PciIo->Pci.Write (
1963 PciIo,
1964 EfiPciIoWidthUint16,
1965 PCI_CARD_BRIDGE_CONTROL,
1966 1,
1967 &BridgeControl
1968 );
1969 }
1970
1971 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1972 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1973 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
1974
1975 break;
1976
1977 case P2C_MEM_2:
1978 PciIo->Pci.Write (
1979 PciIo,
1980 EfiPciIoWidthUint32,
1981 PCI_CARD_MEMORY_BASE_1,
1982 1,
1983 &Address
1984 );
1985
1986 TempAddress = Address + Node->Length - 1;
1987
1988 PciIo->Pci.Write (
1989 PciIo,
1990 EfiPciIoWidthUint32,
1991 PCI_CARD_MEMORY_LIMIT_1,
1992 1,
1993 &TempAddress
1994 );
1995
1996 if (Node->ResType == PciBarTypeMem32) {
1997
1998 //
1999 // Set non-prefetchable bit
2000 //
2001 PciIo->Pci.Read (
2002 PciIo,
2003 EfiPciIoWidthUint16,
2004 PCI_CARD_BRIDGE_CONTROL,
2005 1,
2006 &BridgeControl
2007 );
2008
2009 BridgeControl &= (UINT16) ~(PCI_CARD_PREFETCHABLE_MEMORY_1_ENABLE);
2010 PciIo->Pci.Write (
2011 PciIo,
2012 EfiPciIoWidthUint16,
2013 PCI_CARD_BRIDGE_CONTROL,
2014 1,
2015 &BridgeControl
2016 );
2017
2018 } else {
2019
2020 //
2021 // Set pre-fetchable bit
2022 //
2023 PciIo->Pci.Read (
2024 PciIo,
2025 EfiPciIoWidthUint16,
2026 PCI_CARD_BRIDGE_CONTROL,
2027 1,
2028 &BridgeControl
2029 );
2030
2031 BridgeControl |= PCI_CARD_PREFETCHABLE_MEMORY_1_ENABLE;
2032 PciIo->Pci.Write (
2033 PciIo,
2034 EfiPciIoWidthUint16,
2035 PCI_CARD_BRIDGE_CONTROL,
2036 1,
2037 &BridgeControl
2038 );
2039 }
2040
2041 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2042 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2043 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
2044 break;
2045
2046 case P2C_IO_1:
2047 PciIo->Pci.Write (
2048 PciIo,
2049 EfiPciIoWidthUint32,
2050 PCI_CARD_IO_BASE_0_LOWER,
2051 1,
2052 &Address
2053 );
2054
2055 TempAddress = Address + Node->Length - 1;
2056 PciIo->Pci.Write (
2057 PciIo,
2058 EfiPciIoWidthUint32,
2059 PCI_CARD_IO_LIMIT_0_LOWER,
2060 1,
2061 &TempAddress
2062 );
2063
2064 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2065 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2066 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
2067
2068 break;
2069
2070 case P2C_IO_2:
2071 PciIo->Pci.Write (
2072 PciIo,
2073 EfiPciIoWidthUint32,
2074 PCI_CARD_IO_BASE_1_LOWER,
2075 1,
2076 &Address
2077 );
2078
2079 TempAddress = Address + Node->Length - 1;
2080 PciIo->Pci.Write (
2081 PciIo,
2082 EfiPciIoWidthUint32,
2083 PCI_CARD_IO_LIMIT_1_LOWER,
2084 1,
2085 &TempAddress
2086 );
2087
2088 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2089 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2090 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
2091 break;
2092
2093 default:
2094 break;
2095 }
2096 }
2097
2098 /**
2099 Create padding resource node.
2100
2101 @param PciDev Pci device instance.
2102 @param IoNode Resource info node for IO.
2103 @param Mem32Node Resource info node for 32-bit memory.
2104 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
2105 @param Mem64Node Resource info node for 64-bit memory.
2106 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
2107
2108 **/
2109 VOID
2110 ApplyResourcePadding (
2111 IN PCI_IO_DEVICE *PciDev,
2112 IN PCI_RESOURCE_NODE *IoNode,
2113 IN PCI_RESOURCE_NODE *Mem32Node,
2114 IN PCI_RESOURCE_NODE *PMem32Node,
2115 IN PCI_RESOURCE_NODE *Mem64Node,
2116 IN PCI_RESOURCE_NODE *PMem64Node
2117 )
2118 {
2119 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Ptr;
2120 PCI_RESOURCE_NODE *Node;
2121 UINT8 DummyBarIndex;
2122
2123 DummyBarIndex = 0;
2124 Ptr = PciDev->ResourcePaddingDescriptors;
2125
2126 while (((EFI_ACPI_END_TAG_DESCRIPTOR *) Ptr)->Desc != ACPI_END_TAG_DESCRIPTOR) {
2127
2128 if (Ptr->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR && Ptr->ResType == ACPI_ADDRESS_SPACE_TYPE_IO) {
2129 if (Ptr->AddrLen != 0) {
2130
2131 Node = CreateResourceNode (
2132 PciDev,
2133 Ptr->AddrLen,
2134 Ptr->AddrRangeMax,
2135 DummyBarIndex,
2136 PciBarTypeIo16,
2137 PciResUsagePadding
2138 );
2139 InsertResourceNode (
2140 IoNode,
2141 Node
2142 );
2143 }
2144
2145 Ptr++;
2146 continue;
2147 }
2148
2149 if (Ptr->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR && Ptr->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {
2150
2151 if (Ptr->AddrSpaceGranularity == 32) {
2152
2153 //
2154 // prefechable
2155 //
2156 if (Ptr->SpecificFlag == 0x6) {
2157 if (Ptr->AddrLen != 0) {
2158 Node = CreateResourceNode (
2159 PciDev,
2160 Ptr->AddrLen,
2161 Ptr->AddrRangeMax,
2162 DummyBarIndex,
2163 PciBarTypePMem32,
2164 PciResUsagePadding
2165 );
2166 InsertResourceNode (
2167 PMem32Node,
2168 Node
2169 );
2170 }
2171
2172 Ptr++;
2173 continue;
2174 }
2175
2176 //
2177 // Non-prefechable
2178 //
2179 if (Ptr->SpecificFlag == 0) {
2180 if (Ptr->AddrLen != 0) {
2181 Node = CreateResourceNode (
2182 PciDev,
2183 Ptr->AddrLen,
2184 Ptr->AddrRangeMax,
2185 DummyBarIndex,
2186 PciBarTypeMem32,
2187 PciResUsagePadding
2188 );
2189 InsertResourceNode (
2190 Mem32Node,
2191 Node
2192 );
2193 }
2194
2195 Ptr++;
2196 continue;
2197 }
2198 }
2199
2200 if (Ptr->AddrSpaceGranularity == 64) {
2201
2202 //
2203 // prefechable
2204 //
2205 if (Ptr->SpecificFlag == 0x6) {
2206 if (Ptr->AddrLen != 0) {
2207 Node = CreateResourceNode (
2208 PciDev,
2209 Ptr->AddrLen,
2210 Ptr->AddrRangeMax,
2211 DummyBarIndex,
2212 PciBarTypePMem64,
2213 PciResUsagePadding
2214 );
2215 InsertResourceNode (
2216 PMem64Node,
2217 Node
2218 );
2219 }
2220
2221 Ptr++;
2222 continue;
2223 }
2224
2225 //
2226 // Non-prefechable
2227 //
2228 if (Ptr->SpecificFlag == 0) {
2229 if (Ptr->AddrLen != 0) {
2230 Node = CreateResourceNode (
2231 PciDev,
2232 Ptr->AddrLen,
2233 Ptr->AddrRangeMax,
2234 DummyBarIndex,
2235 PciBarTypeMem64,
2236 PciResUsagePadding
2237 );
2238 InsertResourceNode (
2239 Mem64Node,
2240 Node
2241 );
2242 }
2243
2244 Ptr++;
2245 continue;
2246 }
2247 }
2248 }
2249
2250 Ptr++;
2251 }
2252 }
2253
2254 /**
2255 Get padding resource for PCI-PCI bridge.
2256
2257 @param PciIoDevice PCI-PCI bridge device instance.
2258
2259 @note Feature flag PcdPciBusHotplugDeviceSupport determines
2260 whether need to pad resource for them.
2261 **/
2262 VOID
2263 GetResourcePaddingPpb (
2264 IN PCI_IO_DEVICE *PciIoDevice
2265 )
2266 {
2267 if (gPciHotPlugInit != NULL && FeaturePcdGet (PcdPciBusHotplugDeviceSupport)) {
2268 if (PciIoDevice->ResourcePaddingDescriptors == NULL) {
2269 GetResourcePaddingForHpb (PciIoDevice);
2270 }
2271 }
2272 }
2273