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