]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.c
Fix a bug in DegradeResource().
[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 - 2009, Intel Corporation
5 All rights reserved. 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 PCI_RESOURCE_NODE *TempNode;
1054
1055 //
1056 // If any child device has both option ROM and 64-bit BAR, degrade its PMEM64/MEM64
1057 // requests in case that if a legacy option ROM image can not access 64-bit resources.
1058 //
1059 ChildDeviceLink = Bridge->ChildList.ForwardLink;
1060 while (ChildDeviceLink != NULL && ChildDeviceLink != &Bridge->ChildList) {
1061 Temp = PCI_IO_DEVICE_FROM_LINK (ChildDeviceLink);
1062 if (Temp->RomSize != 0) {
1063 if (!IsListEmpty (&Mem64Node->ChildList)) {
1064 ChildNodeLink = Mem64Node->ChildList.ForwardLink;
1065 while (ChildNodeLink != &Mem64Node->ChildList) {
1066 TempNode = RESOURCE_NODE_FROM_LINK (ChildNodeLink);
1067
1068 if (TempNode->PciDev == Temp) {
1069 RemoveEntryList (ChildNodeLink);
1070 InsertResourceNode (Mem32Node, TempNode);
1071 }
1072 ChildNodeLink = TempNode->Link.ForwardLink;
1073 }
1074 }
1075
1076 if (!IsListEmpty (&PMem64Node->ChildList)) {
1077 ChildNodeLink = PMem64Node->ChildList.ForwardLink;
1078 while (ChildNodeLink != &PMem64Node->ChildList) {
1079 TempNode = RESOURCE_NODE_FROM_LINK (ChildNodeLink);
1080
1081 if (TempNode->PciDev == Temp) {
1082 RemoveEntryList (ChildNodeLink);
1083 InsertResourceNode (PMem32Node, TempNode);
1084 }
1085 ChildNodeLink = TempNode->Link.ForwardLink;
1086 }
1087 }
1088
1089 }
1090 ChildDeviceLink = ChildDeviceLink->ForwardLink;
1091 }
1092
1093 //
1094 // If firmware is in 32-bit mode,
1095 // then degrade PMEM64/MEM64 requests
1096 //
1097 if (sizeof (UINTN) <= 4) {
1098 MergeResourceTree (
1099 Mem32Node,
1100 Mem64Node,
1101 TRUE
1102 );
1103
1104 MergeResourceTree (
1105 PMem32Node,
1106 PMem64Node,
1107 TRUE
1108 );
1109 } else {
1110 //
1111 // if the bridge does not support MEM64, degrade MEM64 to MEM32
1112 //
1113 if (!BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_MEM64_DECODE_SUPPORTED)) {
1114 MergeResourceTree (
1115 Mem32Node,
1116 Mem64Node,
1117 TRUE
1118 );
1119 }
1120
1121 //
1122 // if the bridge does not support PMEM64, degrade PMEM64 to PMEM32
1123 //
1124 if (!BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_PMEM64_DECODE_SUPPORTED)) {
1125 MergeResourceTree (
1126 PMem32Node,
1127 PMem64Node,
1128 TRUE
1129 );
1130 }
1131
1132 //
1133 // if both PMEM64 and PMEM32 requests from child devices, which can not be satisfied
1134 // by a P2P bridge simultaneously, keep PMEM64 and degrade PMEM32 to MEM32.
1135 //
1136 if (!IsListEmpty (&PMem64Node->ChildList) && Bridge->Parent != NULL) {
1137 MergeResourceTree (
1138 Mem32Node,
1139 PMem32Node,
1140 TRUE
1141 );
1142 }
1143 }
1144
1145 //
1146 // If bridge doesn't support Pmem32
1147 // degrade it to mem32
1148 //
1149 if (!BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_PMEM32_DECODE_SUPPORTED)) {
1150 MergeResourceTree (
1151 Mem32Node,
1152 PMem32Node,
1153 TRUE
1154 );
1155 }
1156
1157 //
1158 // if root bridge supports combined Pmem Mem decoding
1159 // merge these two type of resource
1160 //
1161 if (BridgeSupportResourceDecode (Bridge, EFI_BRIDGE_PMEM_MEM_COMBINE_SUPPORTED)) {
1162 MergeResourceTree (
1163 Mem32Node,
1164 PMem32Node,
1165 FALSE
1166 );
1167
1168 //
1169 // No need to check if to degrade MEM64 after merge, because
1170 // if there are PMEM64 still here, 64-bit decode should be supported
1171 // by the root bride.
1172 //
1173 MergeResourceTree (
1174 Mem64Node,
1175 PMem64Node,
1176 FALSE
1177 );
1178 }
1179 }
1180
1181 /**
1182 Test whether bridge device support decode resource.
1183
1184 @param Bridge Bridge device instance.
1185 @param Decode Decode type according to resource type.
1186
1187 @return TRUE The bridge device support decode resource.
1188 @return FALSE The bridge device don't support decode resource.
1189
1190 **/
1191 BOOLEAN
1192 BridgeSupportResourceDecode (
1193 IN PCI_IO_DEVICE *Bridge,
1194 IN UINT32 Decode
1195 )
1196 {
1197 if (((Bridge->Decodes) & Decode) != 0) {
1198 return TRUE;
1199 }
1200
1201 return FALSE;
1202 }
1203
1204 /**
1205 This function is used to program the resource allocated
1206 for each resource node under specified bridge.
1207
1208 @param Base Base address of resource to be progammed.
1209 @param Bridge PCI resource node for the bridge device.
1210
1211 @retval EFI_SUCCESS Successfully to program all resouces
1212 on given PCI bridge device.
1213 @retval EFI_OUT_OF_RESOURCES Base is all one.
1214
1215 **/
1216 EFI_STATUS
1217 ProgramResource (
1218 IN UINT64 Base,
1219 IN PCI_RESOURCE_NODE *Bridge
1220 )
1221 {
1222 LIST_ENTRY *CurrentLink;
1223 PCI_RESOURCE_NODE *Node;
1224 EFI_STATUS Status;
1225
1226 if (Base == gAllOne) {
1227 return EFI_OUT_OF_RESOURCES;
1228 }
1229
1230 CurrentLink = Bridge->ChildList.ForwardLink;
1231
1232 while (CurrentLink != &Bridge->ChildList) {
1233
1234 Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
1235
1236 if (!IS_PCI_BRIDGE (&(Node->PciDev->Pci))) {
1237
1238 if (IS_CARDBUS_BRIDGE (&(Node->PciDev->Pci))) {
1239 //
1240 // Program the PCI Card Bus device
1241 //
1242 ProgramP2C (Base, Node);
1243 } else {
1244 //
1245 // Program the PCI device BAR
1246 //
1247 ProgramBar (Base, Node);
1248 }
1249 } else {
1250 //
1251 // Program the PCI devices under this bridge
1252 //
1253 Status = ProgramResource (Base + Node->Offset, Node);
1254 if (EFI_ERROR (Status)) {
1255 return Status;
1256 }
1257
1258 ProgramPpbApperture (Base, Node);
1259 }
1260
1261 CurrentLink = CurrentLink->ForwardLink;
1262 }
1263
1264 return EFI_SUCCESS;
1265 }
1266
1267 /**
1268 Program Bar register for PCI device.
1269
1270 @param Base Base address for PCI device resource to be progammed.
1271 @param Node Point to resoure node structure.
1272
1273 **/
1274 VOID
1275 ProgramBar (
1276 IN UINT64 Base,
1277 IN PCI_RESOURCE_NODE *Node
1278 )
1279 {
1280 EFI_PCI_IO_PROTOCOL *PciIo;
1281 UINT64 Address;
1282 UINT32 Address32;
1283
1284 ASSERT (Node->Bar < PCI_MAX_BAR);
1285
1286 //
1287 // Check VF BAR
1288 //
1289 if (Node->Virtual) {
1290 ProgramVfBar (Base, Node);
1291 }
1292
1293 Address = 0;
1294 PciIo = &(Node->PciDev->PciIo);
1295
1296 Address = Base + Node->Offset;
1297
1298 //
1299 // Indicate pci bus driver has allocated
1300 // resource for this device
1301 // It might be a temporary solution here since
1302 // pci device could have multiple bar
1303 //
1304 Node->PciDev->Allocated = TRUE;
1305
1306 switch ((Node->PciDev->PciBar[Node->Bar]).BarType) {
1307
1308 case PciBarTypeIo16:
1309 case PciBarTypeIo32:
1310 case PciBarTypeMem32:
1311 case PciBarTypePMem32:
1312
1313 PciIo->Pci.Write (
1314 PciIo,
1315 EfiPciIoWidthUint32,
1316 (Node->PciDev->PciBar[Node->Bar]).Offset,
1317 1,
1318 &Address
1319 );
1320
1321 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1322
1323 break;
1324
1325 case PciBarTypeMem64:
1326 case PciBarTypePMem64:
1327
1328 Address32 = (UINT32) (Address & 0x00000000FFFFFFFF);
1329
1330 PciIo->Pci.Write (
1331 PciIo,
1332 EfiPciIoWidthUint32,
1333 (Node->PciDev->PciBar[Node->Bar]).Offset,
1334 1,
1335 &Address32
1336 );
1337
1338 Address32 = (UINT32) RShiftU64 (Address, 32);
1339
1340 PciIo->Pci.Write (
1341 PciIo,
1342 EfiPciIoWidthUint32,
1343 (UINT8) ((Node->PciDev->PciBar[Node->Bar]).Offset + 4),
1344 1,
1345 &Address32
1346 );
1347
1348 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1349
1350 break;
1351
1352 default:
1353 break;
1354 }
1355 }
1356
1357 /**
1358 Program IOV VF Bar register for PCI device.
1359
1360 @param Base Base address for PCI device resource to be progammed.
1361 @param Node Point to resoure node structure.
1362
1363 **/
1364 EFI_STATUS
1365 ProgramVfBar (
1366 IN UINT64 Base,
1367 IN PCI_RESOURCE_NODE *Node
1368 )
1369 {
1370 EFI_PCI_IO_PROTOCOL *PciIo;
1371 UINT64 Address;
1372 UINT32 Address32;
1373
1374 ASSERT (Node->Bar < PCI_MAX_BAR);
1375 ASSERT (Node->Virtual);
1376
1377 Address = 0;
1378 PciIo = &(Node->PciDev->PciIo);
1379
1380 Address = Base + Node->Offset;
1381
1382 //
1383 // Indicate pci bus driver has allocated
1384 // resource for this device
1385 // It might be a temporary solution here since
1386 // pci device could have multiple bar
1387 //
1388 Node->PciDev->Allocated = TRUE;
1389
1390 switch ((Node->PciDev->VfPciBar[Node->Bar]).BarType) {
1391
1392 case PciBarTypeMem32:
1393 case PciBarTypePMem32:
1394
1395 PciIo->Pci.Write (
1396 PciIo,
1397 EfiPciIoWidthUint32,
1398 (Node->PciDev->VfPciBar[Node->Bar]).Offset,
1399 1,
1400 &Address
1401 );
1402
1403 Node->PciDev->VfPciBar[Node->Bar].BaseAddress = Address;
1404
1405 DEBUG ((
1406 EFI_D_INFO,
1407 "PCI-IOV B%x.D%x.F%x - VF Bar (Offset - 0x%x) 32Mem (Address - 0x%x)\n",
1408 (UINTN)Node->PciDev->BusNumber,
1409 (UINTN)Node->PciDev->DeviceNumber,
1410 (UINTN)Node->PciDev->FunctionNumber,
1411 (UINTN)(Node->PciDev->VfPciBar[Node->Bar]).Offset,
1412 (UINTN)Address
1413 ));
1414
1415 break;
1416
1417 case PciBarTypeMem64:
1418 case PciBarTypePMem64:
1419
1420 Address32 = (UINT32) (Address & 0x00000000FFFFFFFF);
1421
1422 PciIo->Pci.Write (
1423 PciIo,
1424 EfiPciIoWidthUint32,
1425 (Node->PciDev->VfPciBar[Node->Bar]).Offset,
1426 1,
1427 &Address32
1428 );
1429
1430 Address32 = (UINT32) RShiftU64 (Address, 32);
1431
1432 PciIo->Pci.Write (
1433 PciIo,
1434 EfiPciIoWidthUint32,
1435 ((Node->PciDev->VfPciBar[Node->Bar]).Offset + 4),
1436 1,
1437 &Address32
1438 );
1439
1440 Node->PciDev->VfPciBar[Node->Bar].BaseAddress = Address;
1441
1442 DEBUG ((
1443 EFI_D_INFO,
1444 "PCI-IOV B%x.D%x.F%x - VF Bar (Offset - 0x%x) 64Mem (Address - 0x%lx)\n",
1445 (UINTN)Node->PciDev->BusNumber,
1446 (UINTN)Node->PciDev->DeviceNumber,
1447 (UINTN)Node->PciDev->FunctionNumber,
1448 (UINTN)(Node->PciDev->VfPciBar[Node->Bar]).Offset,
1449 (UINT64)Address
1450 ));
1451
1452 break;
1453
1454 case PciBarTypeIo16:
1455 case PciBarTypeIo32:
1456 break;
1457
1458 default:
1459 break;
1460 }
1461
1462 return EFI_SUCCESS;
1463 }
1464
1465 /**
1466 Program PCI-PCI bridge apperture.
1467
1468 @param Base Base address for resource.
1469 @param Node Point to resoure node structure.
1470
1471 **/
1472 VOID
1473 ProgramPpbApperture (
1474 IN UINT64 Base,
1475 IN PCI_RESOURCE_NODE *Node
1476 )
1477 {
1478 EFI_PCI_IO_PROTOCOL *PciIo;
1479 UINT64 Address;
1480 UINT32 Address32;
1481
1482 Address = 0;
1483 //
1484 // If no device resource of this PPB, return anyway
1485 // Apperture is set default in the initialization code
1486 //
1487 if (Node->Length == 0 || Node->ResourceUsage == PciResUsagePadding) {
1488 //
1489 // For padding resource node, just ignore when programming
1490 //
1491 return ;
1492 }
1493
1494 PciIo = &(Node->PciDev->PciIo);
1495 Address = Base + Node->Offset;
1496
1497 //
1498 // Indicate the PPB resource has been allocated
1499 //
1500 Node->PciDev->Allocated = TRUE;
1501
1502 switch (Node->Bar) {
1503
1504 case PPB_BAR_0:
1505 case PPB_BAR_1:
1506 PciIo->Pci.Write (
1507 PciIo,
1508 EfiPciIoWidthUint32,
1509 (Node->PciDev->PciBar[Node->Bar]).Offset,
1510 1,
1511 &Address
1512 );
1513
1514 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1515 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1516
1517 break;
1518
1519 case PPB_IO_RANGE:
1520
1521 Address32 = ((UINT32) (Address)) >> 8;
1522 PciIo->Pci.Write (
1523 PciIo,
1524 EfiPciIoWidthUint8,
1525 0x1C,
1526 1,
1527 &Address32
1528 );
1529
1530 Address32 >>= 8;
1531 PciIo->Pci.Write (
1532 PciIo,
1533 EfiPciIoWidthUint16,
1534 0x30,
1535 1,
1536 &Address32
1537 );
1538
1539 Address32 = (UINT32) (Address + Node->Length - 1);
1540 Address32 = ((UINT32) (Address32)) >> 8;
1541 PciIo->Pci.Write (
1542 PciIo,
1543 EfiPciIoWidthUint8,
1544 0x1D,
1545 1,
1546 &Address32
1547 );
1548
1549 Address32 >>= 8;
1550 PciIo->Pci.Write (
1551 PciIo,
1552 EfiPciIoWidthUint16,
1553 0x32,
1554 1,
1555 &Address32
1556 );
1557
1558 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1559 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1560 break;
1561
1562 case PPB_MEM32_RANGE:
1563
1564 Address32 = ((UINT32) (Address)) >> 16;
1565 PciIo->Pci.Write (
1566 PciIo,
1567 EfiPciIoWidthUint16,
1568 0x20,
1569 1,
1570 &Address32
1571 );
1572
1573 Address32 = (UINT32) (Address + Node->Length - 1);
1574 Address32 = ((UINT32) (Address32)) >> 16;
1575 PciIo->Pci.Write (
1576 PciIo,
1577 EfiPciIoWidthUint16,
1578 0x22,
1579 1,
1580 &Address32
1581 );
1582
1583 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1584 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1585 break;
1586
1587 case PPB_PMEM32_RANGE:
1588 case PPB_PMEM64_RANGE:
1589
1590 Address32 = ((UINT32) (Address)) >> 16;
1591 PciIo->Pci.Write (
1592 PciIo,
1593 EfiPciIoWidthUint16,
1594 0x24,
1595 1,
1596 &Address32
1597 );
1598
1599 Address32 = (UINT32) (Address + Node->Length - 1);
1600 Address32 = ((UINT32) (Address32)) >> 16;
1601 PciIo->Pci.Write (
1602 PciIo,
1603 EfiPciIoWidthUint16,
1604 0x26,
1605 1,
1606 &Address32
1607 );
1608
1609 Address32 = (UINT32) RShiftU64 (Address, 32);
1610 PciIo->Pci.Write (
1611 PciIo,
1612 EfiPciIoWidthUint32,
1613 0x28,
1614 1,
1615 &Address32
1616 );
1617
1618 Address32 = (UINT32) RShiftU64 ((Address + Node->Length - 1), 32);
1619 PciIo->Pci.Write (
1620 PciIo,
1621 EfiPciIoWidthUint32,
1622 0x2C,
1623 1,
1624 &Address32
1625 );
1626
1627 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
1628 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
1629 break;
1630
1631 default:
1632 break;
1633 }
1634 }
1635
1636 /**
1637 Program parent bridge for Option Rom.
1638
1639 @param PciDevice Pci deivce instance.
1640 @param OptionRomBase Base address for Optiona Rom.
1641 @param Enable Enable or disable PCI memory.
1642
1643 **/
1644 VOID
1645 ProgrameUpstreamBridgeForRom (
1646 IN PCI_IO_DEVICE *PciDevice,
1647 IN UINT32 OptionRomBase,
1648 IN BOOLEAN Enable
1649 )
1650 {
1651 PCI_IO_DEVICE *Parent;
1652 PCI_RESOURCE_NODE Node;
1653 //
1654 // For root bridge, just return.
1655 //
1656 Parent = PciDevice->Parent;
1657 ZeroMem (&Node, sizeof (Node));
1658 while (Parent != NULL) {
1659 if (!IS_PCI_BRIDGE (&Parent->Pci)) {
1660 break;
1661 }
1662
1663 Node.PciDev = Parent;
1664 Node.Length = PciDevice->RomSize;
1665 Node.Alignment = 0;
1666 Node.Bar = PPB_MEM32_RANGE;
1667 Node.ResType = PciBarTypeMem32;
1668 Node.Offset = 0;
1669
1670 //
1671 // Program PPB to only open a single <= 16MB apperture
1672 //
1673 if (Enable) {
1674 ProgramPpbApperture (OptionRomBase, &Node);
1675 PCI_ENABLE_COMMAND_REGISTER (Parent, EFI_PCI_COMMAND_MEMORY_SPACE);
1676 } else {
1677 InitializePpb (Parent);
1678 PCI_DISABLE_COMMAND_REGISTER (Parent, EFI_PCI_COMMAND_MEMORY_SPACE);
1679 }
1680
1681 Parent = Parent->Parent;
1682 }
1683 }
1684
1685 /**
1686 Test whether resource exists for a bridge.
1687
1688 @param Bridge Point to resource node for a bridge.
1689
1690 @retval TRUE There is resource on the given bridge.
1691 @retval FALSE There isn't resource on the given bridge.
1692
1693 **/
1694 BOOLEAN
1695 ResourceRequestExisted (
1696 IN PCI_RESOURCE_NODE *Bridge
1697 )
1698 {
1699 if (Bridge != NULL) {
1700 if (!IsListEmpty (&Bridge->ChildList) || Bridge->Length != 0) {
1701 return TRUE;
1702 }
1703 }
1704
1705 return FALSE;
1706 }
1707
1708 /**
1709 Initialize resource pool structure.
1710
1711 @param ResourcePool Point to resource pool structure. This pool
1712 is reset to all zero when returned.
1713 @param ResourceType Type of resource.
1714
1715 **/
1716 VOID
1717 InitializeResourcePool (
1718 IN OUT PCI_RESOURCE_NODE *ResourcePool,
1719 IN PCI_BAR_TYPE ResourceType
1720 )
1721 {
1722 ZeroMem (ResourcePool, sizeof (PCI_RESOURCE_NODE));
1723 ResourcePool->ResType = ResourceType;
1724 ResourcePool->Signature = PCI_RESOURCE_SIGNATURE;
1725 InitializeListHead (&ResourcePool->ChildList);
1726 }
1727
1728
1729 /**
1730 Get all resource information for given Pci device.
1731
1732 @param PciDev Pci device instance.
1733 @param IoBridge Io resource node.
1734 @param Mem32Bridge 32-bit memory node.
1735 @param PMem32Bridge 32-bit Pmemory node.
1736 @param Mem64Bridge 64-bit memory node.
1737 @param PMem64Bridge 64-bit PMemory node.
1738 @param IoPool Link list header for Io resource.
1739 @param Mem32Pool Link list header for 32-bit memory.
1740 @param PMem32Pool Link list header for 32-bit Prefetchable memory.
1741 @param Mem64Pool Link list header for 64-bit memory.
1742 @param PMem64Pool Link list header for 64-bit Prefetchable memory.
1743
1744 **/
1745 VOID
1746 GetResourceMap (
1747 IN PCI_IO_DEVICE *PciDev,
1748 IN PCI_RESOURCE_NODE **IoBridge,
1749 IN PCI_RESOURCE_NODE **Mem32Bridge,
1750 IN PCI_RESOURCE_NODE **PMem32Bridge,
1751 IN PCI_RESOURCE_NODE **Mem64Bridge,
1752 IN PCI_RESOURCE_NODE **PMem64Bridge,
1753 IN PCI_RESOURCE_NODE *IoPool,
1754 IN PCI_RESOURCE_NODE *Mem32Pool,
1755 IN PCI_RESOURCE_NODE *PMem32Pool,
1756 IN PCI_RESOURCE_NODE *Mem64Pool,
1757 IN PCI_RESOURCE_NODE *PMem64Pool
1758 )
1759 {
1760
1761 PCI_RESOURCE_NODE *Temp;
1762 LIST_ENTRY *CurrentLink;
1763
1764 CurrentLink = IoPool->ChildList.ForwardLink;
1765
1766 //
1767 // Get Io resource map
1768 //
1769 while (CurrentLink != &IoPool->ChildList) {
1770
1771 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
1772
1773 if (Temp->PciDev == PciDev) {
1774 *IoBridge = Temp;
1775 }
1776
1777 CurrentLink = CurrentLink->ForwardLink;
1778 }
1779
1780 //
1781 // Get Mem32 resource map
1782 //
1783 CurrentLink = Mem32Pool->ChildList.ForwardLink;
1784
1785 while (CurrentLink != &Mem32Pool->ChildList) {
1786
1787 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
1788
1789 if (Temp->PciDev == PciDev) {
1790 *Mem32Bridge = Temp;
1791 }
1792
1793 CurrentLink = CurrentLink->ForwardLink;
1794 }
1795
1796 //
1797 // Get Pmem32 resource map
1798 //
1799 CurrentLink = PMem32Pool->ChildList.ForwardLink;
1800
1801 while (CurrentLink != &PMem32Pool->ChildList) {
1802
1803 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
1804
1805 if (Temp->PciDev == PciDev) {
1806 *PMem32Bridge = Temp;
1807 }
1808
1809 CurrentLink = CurrentLink->ForwardLink;
1810 }
1811
1812 //
1813 // Get Mem64 resource map
1814 //
1815 CurrentLink = Mem64Pool->ChildList.ForwardLink;
1816
1817 while (CurrentLink != &Mem64Pool->ChildList) {
1818
1819 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
1820
1821 if (Temp->PciDev == PciDev) {
1822 *Mem64Bridge = Temp;
1823 }
1824
1825 CurrentLink = CurrentLink->ForwardLink;
1826 }
1827
1828 //
1829 // Get Pmem64 resource map
1830 //
1831 CurrentLink = PMem64Pool->ChildList.ForwardLink;
1832
1833 while (CurrentLink != &PMem64Pool->ChildList) {
1834
1835 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
1836
1837 if (Temp->PciDev == PciDev) {
1838 *PMem64Bridge = Temp;
1839 }
1840
1841 CurrentLink = CurrentLink->ForwardLink;
1842 }
1843 }
1844
1845 /**
1846 Destory given resource tree.
1847
1848 @param Bridge PCI resource root node of resource tree.
1849
1850 **/
1851 VOID
1852 DestroyResourceTree (
1853 IN PCI_RESOURCE_NODE *Bridge
1854 )
1855 {
1856 PCI_RESOURCE_NODE *Temp;
1857 LIST_ENTRY *CurrentLink;
1858
1859 while (!IsListEmpty (&Bridge->ChildList)) {
1860
1861 CurrentLink = Bridge->ChildList.ForwardLink;
1862
1863 Temp = RESOURCE_NODE_FROM_LINK (CurrentLink);
1864 ASSERT (Temp);
1865
1866 RemoveEntryList (CurrentLink);
1867
1868 if (IS_PCI_BRIDGE (&(Temp->PciDev->Pci))) {
1869 DestroyResourceTree (Temp);
1870 }
1871
1872 FreePool (Temp);
1873 }
1874 }
1875
1876 /**
1877 Insert resource padding for P2C.
1878
1879 @param PciDev Pci device instance.
1880 @param IoNode Resource info node for IO.
1881 @param Mem32Node Resource info node for 32-bit memory.
1882 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
1883 @param Mem64Node Resource info node for 64-bit memory.
1884 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
1885
1886 **/
1887 VOID
1888 ResourcePaddingForCardBusBridge (
1889 IN PCI_IO_DEVICE *PciDev,
1890 IN PCI_RESOURCE_NODE *IoNode,
1891 IN PCI_RESOURCE_NODE *Mem32Node,
1892 IN PCI_RESOURCE_NODE *PMem32Node,
1893 IN PCI_RESOURCE_NODE *Mem64Node,
1894 IN PCI_RESOURCE_NODE *PMem64Node
1895 )
1896 {
1897 PCI_RESOURCE_NODE *Node;
1898
1899 Node = NULL;
1900
1901 //
1902 // Memory Base/Limit Register 0
1903 // Bar 1 denodes memory range 0
1904 //
1905 Node = CreateResourceNode (
1906 PciDev,
1907 0x2000000,
1908 0x1ffffff,
1909 1,
1910 PciBarTypeMem32,
1911 PciResUsagePadding
1912 );
1913
1914 InsertResourceNode (
1915 Mem32Node,
1916 Node
1917 );
1918
1919 //
1920 // Memory Base/Limit Register 1
1921 // Bar 2 denodes memory range1
1922 //
1923 Node = CreateResourceNode (
1924 PciDev,
1925 0x2000000,
1926 0x1ffffff,
1927 2,
1928 PciBarTypePMem32,
1929 PciResUsagePadding
1930 );
1931
1932 InsertResourceNode (
1933 PMem32Node,
1934 Node
1935 );
1936
1937 //
1938 // Io Base/Limit
1939 // Bar 3 denodes io range 0
1940 //
1941 Node = CreateResourceNode (
1942 PciDev,
1943 0x100,
1944 0xff,
1945 3,
1946 PciBarTypeIo16,
1947 PciResUsagePadding
1948 );
1949
1950 InsertResourceNode (
1951 IoNode,
1952 Node
1953 );
1954
1955 //
1956 // Io Base/Limit
1957 // Bar 4 denodes io range 0
1958 //
1959 Node = CreateResourceNode (
1960 PciDev,
1961 0x100,
1962 0xff,
1963 4,
1964 PciBarTypeIo16,
1965 PciResUsagePadding
1966 );
1967
1968 InsertResourceNode (
1969 IoNode,
1970 Node
1971 );
1972 }
1973
1974 /**
1975 Program PCI Card device register for given resource node.
1976
1977 @param Base Base address of PCI Card device to be programmed.
1978 @param Node Given resource node.
1979
1980 **/
1981 VOID
1982 ProgramP2C (
1983 IN UINT64 Base,
1984 IN PCI_RESOURCE_NODE *Node
1985 )
1986 {
1987 EFI_PCI_IO_PROTOCOL *PciIo;
1988 UINT64 Address;
1989 UINT64 TempAddress;
1990 UINT16 BridgeControl;
1991
1992 Address = 0;
1993 PciIo = &(Node->PciDev->PciIo);
1994
1995 Address = Base + Node->Offset;
1996
1997 //
1998 // Indicate pci bus driver has allocated
1999 // resource for this device
2000 // It might be a temporary solution here since
2001 // pci device could have multiple bar
2002 //
2003 Node->PciDev->Allocated = TRUE;
2004
2005 switch (Node->Bar) {
2006
2007 case P2C_BAR_0:
2008 PciIo->Pci.Write (
2009 PciIo,
2010 EfiPciIoWidthUint32,
2011 (Node->PciDev->PciBar[Node->Bar]).Offset,
2012 1,
2013 &Address
2014 );
2015
2016 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2017 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2018 break;
2019
2020 case P2C_MEM_1:
2021 PciIo->Pci.Write (
2022 PciIo,
2023 EfiPciIoWidthUint32,
2024 PCI_CARD_MEMORY_BASE_0,
2025 1,
2026 &Address
2027 );
2028
2029 TempAddress = Address + Node->Length - 1;
2030 PciIo->Pci.Write (
2031 PciIo,
2032 EfiPciIoWidthUint32,
2033 PCI_CARD_MEMORY_LIMIT_0,
2034 1,
2035 &TempAddress
2036 );
2037
2038 if (Node->ResType == PciBarTypeMem32) {
2039 //
2040 // Set non-prefetchable bit
2041 //
2042 PciIo->Pci.Read (
2043 PciIo,
2044 EfiPciIoWidthUint16,
2045 PCI_CARD_BRIDGE_CONTROL,
2046 1,
2047 &BridgeControl
2048 );
2049
2050 BridgeControl &= (UINT16) ~PCI_CARD_PREFETCHABLE_MEMORY_0_ENABLE;
2051 PciIo->Pci.Write (
2052 PciIo,
2053 EfiPciIoWidthUint16,
2054 PCI_CARD_BRIDGE_CONTROL,
2055 1,
2056 &BridgeControl
2057 );
2058
2059 } else {
2060 //
2061 // Set pre-fetchable bit
2062 //
2063 PciIo->Pci.Read (
2064 PciIo,
2065 EfiPciIoWidthUint16,
2066 PCI_CARD_BRIDGE_CONTROL,
2067 1,
2068 &BridgeControl
2069 );
2070
2071 BridgeControl |= PCI_CARD_PREFETCHABLE_MEMORY_0_ENABLE;
2072 PciIo->Pci.Write (
2073 PciIo,
2074 EfiPciIoWidthUint16,
2075 PCI_CARD_BRIDGE_CONTROL,
2076 1,
2077 &BridgeControl
2078 );
2079 }
2080
2081 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2082 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2083 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
2084
2085 break;
2086
2087 case P2C_MEM_2:
2088 PciIo->Pci.Write (
2089 PciIo,
2090 EfiPciIoWidthUint32,
2091 PCI_CARD_MEMORY_BASE_1,
2092 1,
2093 &Address
2094 );
2095
2096 TempAddress = Address + Node->Length - 1;
2097
2098 PciIo->Pci.Write (
2099 PciIo,
2100 EfiPciIoWidthUint32,
2101 PCI_CARD_MEMORY_LIMIT_1,
2102 1,
2103 &TempAddress
2104 );
2105
2106 if (Node->ResType == PciBarTypeMem32) {
2107
2108 //
2109 // Set non-prefetchable bit
2110 //
2111 PciIo->Pci.Read (
2112 PciIo,
2113 EfiPciIoWidthUint16,
2114 PCI_CARD_BRIDGE_CONTROL,
2115 1,
2116 &BridgeControl
2117 );
2118
2119 BridgeControl &= (UINT16) ~(PCI_CARD_PREFETCHABLE_MEMORY_1_ENABLE);
2120 PciIo->Pci.Write (
2121 PciIo,
2122 EfiPciIoWidthUint16,
2123 PCI_CARD_BRIDGE_CONTROL,
2124 1,
2125 &BridgeControl
2126 );
2127
2128 } else {
2129
2130 //
2131 // Set pre-fetchable bit
2132 //
2133 PciIo->Pci.Read (
2134 PciIo,
2135 EfiPciIoWidthUint16,
2136 PCI_CARD_BRIDGE_CONTROL,
2137 1,
2138 &BridgeControl
2139 );
2140
2141 BridgeControl |= PCI_CARD_PREFETCHABLE_MEMORY_1_ENABLE;
2142 PciIo->Pci.Write (
2143 PciIo,
2144 EfiPciIoWidthUint16,
2145 PCI_CARD_BRIDGE_CONTROL,
2146 1,
2147 &BridgeControl
2148 );
2149 }
2150
2151 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2152 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2153 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
2154 break;
2155
2156 case P2C_IO_1:
2157 PciIo->Pci.Write (
2158 PciIo,
2159 EfiPciIoWidthUint32,
2160 PCI_CARD_IO_BASE_0_LOWER,
2161 1,
2162 &Address
2163 );
2164
2165 TempAddress = Address + Node->Length - 1;
2166 PciIo->Pci.Write (
2167 PciIo,
2168 EfiPciIoWidthUint32,
2169 PCI_CARD_IO_LIMIT_0_LOWER,
2170 1,
2171 &TempAddress
2172 );
2173
2174 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2175 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2176 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
2177
2178 break;
2179
2180 case P2C_IO_2:
2181 PciIo->Pci.Write (
2182 PciIo,
2183 EfiPciIoWidthUint32,
2184 PCI_CARD_IO_BASE_1_LOWER,
2185 1,
2186 &Address
2187 );
2188
2189 TempAddress = Address + Node->Length - 1;
2190 PciIo->Pci.Write (
2191 PciIo,
2192 EfiPciIoWidthUint32,
2193 PCI_CARD_IO_LIMIT_1_LOWER,
2194 1,
2195 &TempAddress
2196 );
2197
2198 Node->PciDev->PciBar[Node->Bar].BaseAddress = Address;
2199 Node->PciDev->PciBar[Node->Bar].Length = Node->Length;
2200 Node->PciDev->PciBar[Node->Bar].BarType = Node->ResType;
2201 break;
2202
2203 default:
2204 break;
2205 }
2206 }
2207
2208 /**
2209 Create padding resource node.
2210
2211 @param PciDev Pci device instance.
2212 @param IoNode Resource info node for IO.
2213 @param Mem32Node Resource info node for 32-bit memory.
2214 @param PMem32Node Resource info node for 32-bit Prefetchable Memory.
2215 @param Mem64Node Resource info node for 64-bit memory.
2216 @param PMem64Node Resource info node for 64-bit Prefetchable Memory.
2217
2218 **/
2219 VOID
2220 ApplyResourcePadding (
2221 IN PCI_IO_DEVICE *PciDev,
2222 IN PCI_RESOURCE_NODE *IoNode,
2223 IN PCI_RESOURCE_NODE *Mem32Node,
2224 IN PCI_RESOURCE_NODE *PMem32Node,
2225 IN PCI_RESOURCE_NODE *Mem64Node,
2226 IN PCI_RESOURCE_NODE *PMem64Node
2227 )
2228 {
2229 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Ptr;
2230 PCI_RESOURCE_NODE *Node;
2231 UINT8 DummyBarIndex;
2232
2233 DummyBarIndex = 0;
2234 Ptr = PciDev->ResourcePaddingDescriptors;
2235
2236 while (((EFI_ACPI_END_TAG_DESCRIPTOR *) Ptr)->Desc != ACPI_END_TAG_DESCRIPTOR) {
2237
2238 if (Ptr->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR && Ptr->ResType == ACPI_ADDRESS_SPACE_TYPE_IO) {
2239 if (Ptr->AddrLen != 0) {
2240
2241 Node = CreateResourceNode (
2242 PciDev,
2243 Ptr->AddrLen,
2244 Ptr->AddrRangeMax,
2245 DummyBarIndex,
2246 PciBarTypeIo16,
2247 PciResUsagePadding
2248 );
2249 InsertResourceNode (
2250 IoNode,
2251 Node
2252 );
2253 }
2254
2255 Ptr++;
2256 continue;
2257 }
2258
2259 if (Ptr->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR && Ptr->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {
2260
2261 if (Ptr->AddrSpaceGranularity == 32) {
2262
2263 //
2264 // prefechable
2265 //
2266 if (Ptr->SpecificFlag == 0x6) {
2267 if (Ptr->AddrLen != 0) {
2268 Node = CreateResourceNode (
2269 PciDev,
2270 Ptr->AddrLen,
2271 Ptr->AddrRangeMax,
2272 DummyBarIndex,
2273 PciBarTypePMem32,
2274 PciResUsagePadding
2275 );
2276 InsertResourceNode (
2277 PMem32Node,
2278 Node
2279 );
2280 }
2281
2282 Ptr++;
2283 continue;
2284 }
2285
2286 //
2287 // Non-prefechable
2288 //
2289 if (Ptr->SpecificFlag == 0) {
2290 if (Ptr->AddrLen != 0) {
2291 Node = CreateResourceNode (
2292 PciDev,
2293 Ptr->AddrLen,
2294 Ptr->AddrRangeMax,
2295 DummyBarIndex,
2296 PciBarTypeMem32,
2297 PciResUsagePadding
2298 );
2299 InsertResourceNode (
2300 Mem32Node,
2301 Node
2302 );
2303 }
2304
2305 Ptr++;
2306 continue;
2307 }
2308 }
2309
2310 if (Ptr->AddrSpaceGranularity == 64) {
2311
2312 //
2313 // prefechable
2314 //
2315 if (Ptr->SpecificFlag == 0x6) {
2316 if (Ptr->AddrLen != 0) {
2317 Node = CreateResourceNode (
2318 PciDev,
2319 Ptr->AddrLen,
2320 Ptr->AddrRangeMax,
2321 DummyBarIndex,
2322 PciBarTypePMem64,
2323 PciResUsagePadding
2324 );
2325 InsertResourceNode (
2326 PMem64Node,
2327 Node
2328 );
2329 }
2330
2331 Ptr++;
2332 continue;
2333 }
2334
2335 //
2336 // Non-prefechable
2337 //
2338 if (Ptr->SpecificFlag == 0) {
2339 if (Ptr->AddrLen != 0) {
2340 Node = CreateResourceNode (
2341 PciDev,
2342 Ptr->AddrLen,
2343 Ptr->AddrRangeMax,
2344 DummyBarIndex,
2345 PciBarTypeMem64,
2346 PciResUsagePadding
2347 );
2348 InsertResourceNode (
2349 Mem64Node,
2350 Node
2351 );
2352 }
2353
2354 Ptr++;
2355 continue;
2356 }
2357 }
2358 }
2359
2360 Ptr++;
2361 }
2362 }
2363
2364 /**
2365 Get padding resource for PCI-PCI bridge.
2366
2367 @param PciIoDevice PCI-PCI bridge device instance.
2368
2369 @note Feature flag PcdPciBusHotplugDeviceSupport determines
2370 whether need to pad resource for them.
2371 **/
2372 VOID
2373 GetResourcePaddingPpb (
2374 IN PCI_IO_DEVICE *PciIoDevice
2375 )
2376 {
2377 if (gPciHotPlugInit != NULL && FeaturePcdGet (PcdPciBusHotplugDeviceSupport)) {
2378 if (PciIoDevice->ResourcePaddingDescriptors == NULL) {
2379 GetResourcePaddingForHpb (PciIoDevice);
2380 }
2381 }
2382 }
2383