hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/** @file
 
  @copyright
  Copyright 2004 - 2021 Intel Corporation. <BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
 
#include <PchLimits.h>
#include <PiDxe.h>
#include <Protocol/IioUds.h>
#include <PciPlatform.h>
#include <PciPlatformHooks.h>
#include <PciSupportLib.h>
#include <IoApic.h>
#include <Library/S3BootScriptLib.h>
#include <Library/IoLib.h>
#include <Protocol/DynamicSiLibraryProtocol.h>
#include "../PciHostBridge/PciRootBridge.h"
#include "../PciHostBridge/PciHostBridge.h"
 
 
/******************************************************************************
 * Local definitions.
 ******************************************************************************/
/**
  Uncomment the PCIDEBUG macro to enable tracing the library activity in a test build.
 **/
#define PCIDEBUG(...) // { DEBUG((DEBUG_INFO, "[PCI] " __VA_ARGS__)); }
 
 
/******************************************************************************
 * Variables.
 ******************************************************************************/
SYSTEM_CONFIGURATION  mSystemConfiguration;
EFI_IIO_UDS_PROTOCOL *mIioUds = NULL;
 
 
/******************************************************************************
 * Functions.
 ******************************************************************************/
VOID
ChipsetCallback (
  IN  EFI_HANDLE                                    RootBridgeHandle,
  IN  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS   PciAddress,
  IN  EFI_PCI_ENUMERATION_PHASE                     Phase,
  IN  EFI_PCI_CALLBACK_CONTEXT                      *ContextPtr
  )
{
  EFI_LIST_ENTRY    *NodePtr;
  PCI_CALLBACK_DATA *CallbackDataPtr;
 
  //
  // Check if the node has been added
  //
  for (NodePtr = GetFirstNode (&mPciPrivateData.PciCallbackList);
       !IsNull (&mPciPrivateData.PciCallbackList, NodePtr);
       NodePtr = GetNextNode (&mPciPrivateData.PciCallbackList, NodePtr)) {
    CallbackDataPtr = PCI_CALLBACK_DATA_FROM_LINK (NodePtr);
    if (CallbackDataPtr->Phase & Phase) {
      (CallbackDataPtr->Function) (RootBridgeHandle, PciAddress, Phase, ContextPtr);
    }
  }
}
 
/**
 
    GC_TODO: add routine description
 
    @param StartBus - GC_TODO: add arg description
 
    @retval EFI_SUCCESS - GC_TODO: add retval description
 
**/
EFI_STATUS
PciTreeTraverse (
  IN  UINT8       Socket,
  IN  UINT8       Stack,
  IN  UINT8       StartBus
  )
{
  UINT64                          PciAddress;
  UINT8                           Device;
  UINT8                           Func;
  UINT8                           SecondaryBus;
  BOOLEAN                         MultiFunc;
  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
 
  if (Socket >= NELEMENTS (mPciPrivateData.PciRootBridgeIo) ||
      Stack >= NELEMENTS (mPciPrivateData.PciRootBridgeIo[Socket]) ||
      mPciPrivateData.PciRootBridgeIo[Socket][Stack] == NULL) {
    ASSERT (FALSE);
    return EFI_INVALID_PARAMETER;
  }
 
  PciRootBridgeIo = mPciPrivateData.PciRootBridgeIo[Socket][Stack];
 
  for (Device = 0; Device <= PCI_MAX_DEVICE; Device++) {
    MultiFunc = FALSE;
    for (Func = 0; Func <= PCI_MAX_FUNC; Func++) {
      if (IsPciDevicePresent (
            PciRootBridgeIo,
            &mPciPrivateData.Context.PciHeader,
            StartBus,
            Device,
            Func
            )) {
        if ((Func == 0) && IS_PCI_MULTI_FUNC(&mPciPrivateData.Context.PciHeader)) {
          MultiFunc = TRUE;
        }
        PciAddress = EFI_PCI_ADDRESS (StartBus, Device, Func, 0);
        mPciPrivateData.Context.PciRootBridgeIo = PciRootBridgeIo;
        ChipsetCallback (
          mPciPrivateData.RootBridgeHandle,
          *(EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *) &PciAddress,
          mPciPrivateData.PciEnumerationPhase,
          &(mPciPrivateData.Context)
          );
        if (IS_PCI_BRIDGE (&(mPciPrivateData.Context.PciHeader))) {
          PciAddress = EFI_PCI_ADDRESS (StartBus, Device, Func, PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET);
          PciRootBridgeIo->Pci.Read (
                                PciRootBridgeIo,
                                EfiPciWidthUint8,
                                *(UINT64 *) &PciAddress,
                                1,
                                &SecondaryBus
                                );
          if ((SecondaryBus > 0) && (SecondaryBus < 0xFF)) {
            //
            // Recursive call for next bus in this stack
            //
            PciTreeTraverse (Socket, Stack, SecondaryBus);
          }
        }
      }
 
      if (!MultiFunc) {
        //
        // Skip sub functions, this is not a multi function device
        //
        Func = PCI_MAX_FUNC;
      }
    }
  }
 
  return EFI_SUCCESS;
}
 
/**
 
  Program Io Apic Id
 
  @param IoApicAddress and IoApicId
 
  @retval None
 
**/
VOID
ProgramIoApicId (
  IN UINT32                            IoApicAddress,
  IN UINT8                             IoApicId
  )
{
 
  UINT32  Data;
 
  mPciPrivateData.CpuIo->Mem.Read (
                              mPciPrivateData.CpuIo,
                              EfiCpuIoWidthUint32,
                              IoApicAddress + EFI_IO_APIC_INDEX_OFFSET,
                              1,
                              &Data
                              );
 
  //
  // IOAPIC is not there
  //
  if (Data == (UINT32) -1) {
    return ;
  }
  //
  // Set up IO APIC ID and enable FSB delivery
  // Use CPU IO protocol since the IO APIC ranges
  // are not included in PCI apertures
  //
  Data = EFI_IO_APIC_ID_REGISTER;
  mPciPrivateData.CpuIo->Mem.Write (
                              mPciPrivateData.CpuIo,
                              EfiCpuIoWidthUint32,
                              IoApicAddress + EFI_IO_APIC_INDEX_OFFSET,
                              1,
                              &Data
                              );
 
  Data = IoApicId << EFI_IO_APIC_ID_BITSHIFT;
  mPciPrivateData.CpuIo->Mem.Write (
                              mPciPrivateData.CpuIo,
                              EfiCpuIoWidthUint32,
                              IoApicAddress + EFI_IO_APIC_DATA_OFFSET,
                              1,
                              &Data
                              );
 
  Data = EFI_IO_APIC_BOOT_CONFIG_REGISTER;
  mPciPrivateData.CpuIo->Mem.Write (
                              mPciPrivateData.CpuIo,
                              EfiCpuIoWidthUint32,
                              IoApicAddress + EFI_IO_APIC_INDEX_OFFSET,
                              1,
                              &Data
                              );
 
  Data = EFI_IO_APIC_FSB_INT_DELIVERY;
  mPciPrivateData.CpuIo->Mem.Write (
                              mPciPrivateData.CpuIo,
                              EfiCpuIoWidthUint32,
                              IoApicAddress + EFI_IO_APIC_DATA_OFFSET,
                              1,
                              &Data
                              );
}
 
#ifdef EFI_PCI_IOV_SUPPORT
/**
 
  Initialize the Pci Iov Platform Data.
 
  @param ImageHandle  -  Handle to the image.
  @param SystemTable  -  Handle to System Table.
 
  @retval EFI_STATUS  -  Status of the function calling.
 
**/
EFI_STATUS
EFIAPI
PciPlatformInitPciIovData (
  VOID
  )
{
  EFI_STATUS                            Status;
  EFI_PCI_IOV_PLATFORM_POLICY           PciIovPolicy;
  UINT32                                SystemPageSize;
  EFI_PCI_IOV_PLATFORM_PROTOCOL         *gPciIovPlatformProtocol;
 
  Status = gBS->LocateProtocol (
             &gEfiPciIovPlatformProtocolGuid,
             NULL,
             &gPciIovPlatformProtocol
             );
  if (!EFI_ERROR (Status)) {
    Status = gPciIovPlatformProtocol->GetSystemLowestPageSize (
                                           gPciIovPlatformProtocol,
                                           &SystemPageSize
                                           );
    if (!EFI_ERROR (Status)) {
      Status = PcdSet32S (PcdSrIovSystemPageSize, SystemPageSize);
      ASSERT_EFI_ERROR (Status);
      if (EFI_ERROR(Status)) {
        return Status;
      }
    } else {
      return Status;
    }
    Status = gPciIovPlatformProtocol->GetPlatformPolicy (
                                           gPciIovPlatformProtocol,
                                           &PciIovPolicy
                                           );
    if (!EFI_ERROR (Status)) {
      if (PciIovPolicy & EFI_PCI_IOV_POLICY_ARI) {
        Status = PcdSetBoolS (PcdAriSupport, TRUE);
        ASSERT_EFI_ERROR (Status);
        if (EFI_ERROR(Status)) {
          return Status;
        }
      } else {
        Status = PcdSetBoolS (PcdAriSupport, FALSE);
        ASSERT_EFI_ERROR (Status);
        if (EFI_ERROR(Status)) {
          return Status;
        }
      }
      if (PciIovPolicy & EFI_PCI_IOV_POLICY_SRIOV) {
        Status = PcdSetBoolS (PcdSrIovSupport, TRUE);
        ASSERT_EFI_ERROR (Status);
        if (EFI_ERROR(Status)) {
          return Status;
        }
      } else {
        Status = PcdSetBoolS (PcdSrIovSupport, FALSE);
        ASSERT_EFI_ERROR (Status);
        if (EFI_ERROR(Status)) {
          return Status;
        }
      }
      if (PciIovPolicy & EFI_PCI_IOV_POLICY_MRIOV) {
        Status = PcdSetBoolS (PcdMrIovSupport, TRUE);
        ASSERT_EFI_ERROR (Status);
        if (EFI_ERROR(Status)) {
          return Status;
        }
      } else {
        Status = PcdSetBoolS (PcdMrIovSupport, FALSE);
        ASSERT_EFI_ERROR (Status);
        if (EFI_ERROR(Status)) {
          return Status;
        }
      }
    } else {
      return Status;
    }
    DEBUG ((
          EFI_D_INFO,
          " Initialized SR-IOV Platform Data: PCIIovPolicy = 0x%x; SystemPageSize = 0x%x;\n",
          PciIovPolicy, SystemPageSize
          ));
  } else {
    DEBUG ((
          EFI_D_INFO,
          " Using default values for SystemPageSize;\n"
          ));
  }
  return Status;
}
#endif
 
/**
 
  Platform Pci Express init.
 
  @param HostBridgeInstance  -  Pointer to Host Bridge private data
                                does not support 64 bit memory addresses.
 
  @retval EFI_SUCCESS  -  Success.
 
**/
EFI_STATUS
PciPlatformEarlyInit (
  VOID
  )
{
  EFI_STATUS                        Status;
 
  Status = EFI_SUCCESS;
 
#ifdef EFI_PCI_IOV_SUPPORT
  Status = PciPlatformInitPciIovData();  // Update IOV PCD values
#endif
  return Status;
}
 
/**
 
  Attempts to set the XHCI controller's PCI CMD.MSE and CMD.BME bits to enable OS kernel debugging over XHCI.
 
**/
VOID
AttemptToSetXhciMse (
  )
 
{
  UINT32                            XhciBar;
  UINT16                            Command;
  DYNAMIC_SI_LIBARY_PROTOCOL        *DynamicSiLibraryProtocol = NULL;
  EFI_STATUS                        Status;
 
  Status = gBS->LocateProtocol (&gDynamicSiLibraryProtocolGuid, NULL, &DynamicSiLibraryProtocol);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return;
  }
 
  //
  // Step 1. Make sure the XHCI BAR is initialized.
  //         Check if lower 32 bits of 64-bit BAR are configured.
  //
  XhciBar = MmioRead32 (DynamicSiLibraryProtocol->MmPciBase (DEFAULT_PCI_BUS_NUMBER_PCH, PCI_DEVICE_NUMBER_PCH_XHCI, PCI_FUNCTION_NUMBER_PCH_XHCI) + R_XHCI_CFG_BAR0) & ~(0xF);
  if (XhciBar == 0xFFFFFFF0) {
    return;
  }
  if ((XhciBar & 0xFFFF0000) == 0) {
    //
    // If lower 32 bits are not configured, check upper 32 bits.
    //
    XhciBar = MmioRead32 (DynamicSiLibraryProtocol->MmPciBase (DEFAULT_PCI_BUS_NUMBER_PCH, PCI_DEVICE_NUMBER_PCH_XHCI, PCI_FUNCTION_NUMBER_PCH_XHCI) + R_XHCI_CFG_BAR0 + 4);
    if (XhciBar == 0) {
      return;
    }
  }
 
  //
  // Step 2. If XHCI's MSE (Memory Space Enable) or BME (Bus Master Enable) bits are cleared, set them.
  //
  Command = MmioRead16 (DynamicSiLibraryProtocol->MmPciBase (DEFAULT_PCI_BUS_NUMBER_PCH, PCI_DEVICE_NUMBER_PCH_XHCI, PCI_FUNCTION_NUMBER_PCH_XHCI) + PCI_COMMAND_OFFSET);
  if ((Command & (EFI_PCI_COMMAND_MEMORY_SPACE | EFI_PCI_COMMAND_BUS_MASTER)) != (EFI_PCI_COMMAND_MEMORY_SPACE | EFI_PCI_COMMAND_BUS_MASTER)) {
    MmioOr16 (DynamicSiLibraryProtocol->MmPciBase (DEFAULT_PCI_BUS_NUMBER_PCH, PCI_DEVICE_NUMBER_PCH_XHCI, PCI_FUNCTION_NUMBER_PCH_XHCI) + PCI_COMMAND_OFFSET, (EFI_PCI_COMMAND_MEMORY_SPACE | EFI_PCI_COMMAND_BUS_MASTER));
  }
}
 
 
/**
 
  Init pci device registers after the device resources have been allocated, so
  that devices behind a bus could be accessed.
 
  @param HostBridgeInstance  -  PCI_HOST_BRIDGE_INSTANCE.
 
  @retval EFI_SUCCESS  -  Function has completed successfully.
 
**/
EFI_STATUS
PciPlatformPostInit (
  VOID
  )
{
  DYNAMIC_SI_LIBARY_PROTOCOL        *DynamicSiLibraryProtocol = NULL;
  EFI_STATUS                        Status;
 
  //
  // Program all the IOAPIC in system
  //
 
  Status = gBS->LocateProtocol (&gDynamicSiLibraryProtocolGuid, NULL, &DynamicSiLibraryProtocol);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
 
  UINT8 Socket, Stack, IoApicId, ApicIndex = 0;
  CPU_CSR_ACCESS_VAR   *CpuCsrAccessVarPtr = NULL;
  Stack = 0;
  IoApicId = 0;
  CpuCsrAccessVarPtr = DynamicSiLibraryProtocol->GetSysCpuCsrAccessVar ();
  DEBUG ((DEBUG_INFO, "PciPlatformPostInit: setting up IOAPIC for PCH\n"));
  ProgramIoApicId (mIioUds->IioUdsPtr->PlatformData.IIO_resource[0].StackRes[0].IoApicBase, PCH_IOAPIC_ID);
  for (Socket = 0; Socket < MAX_SOCKET; Socket++) {
    if (!(CpuCsrAccessVarPtr->socketPresentBitMap & (1 << Socket))) {
      continue;
    }
 
    for (Stack = 0; Stack < MAX_IIO_STACK; Stack++, ApicIndex++) {
      if (!(mIioUds->IioUdsPtr->PlatformData.CpuQpiInfo[Socket].stackPresentBitmap & (1 << Stack))) {
        continue;
      }
      switch (ApicIndex) {
      case 0:
        IoApicId = PC00_IOAPIC_ID;
        break;
      case 1:
        IoApicId = PC01_IOAPIC_ID;
        break;
      case 2:
        IoApicId = PC02_IOAPIC_ID;
        break;
      case 3:
        IoApicId = PC03_IOAPIC_ID;
        break;
      case 4:
        IoApicId = PC04_IOAPIC_ID;
        break;
      case 5:
        IoApicId = PC05_IOAPIC_ID;
        break;
      case 6:
        IoApicId = PC06_IOAPIC_ID;
        break;
      case 7:
        IoApicId = PC07_IOAPIC_ID;
        break;
      case 8:
        IoApicId = PC08_IOAPIC_ID;
        break;
      case 9:
        IoApicId = PC09_IOAPIC_ID;
        break;
      case 10:
        IoApicId = PC10_IOAPIC_ID;
        break;
      case 11:
        IoApicId = PC11_IOAPIC_ID;
        break;
      case 12:
        IoApicId = PC12_IOAPIC_ID;
        break;
      case 13:
        IoApicId = PC13_IOAPIC_ID;
        break;
      case 14:
        IoApicId = PC14_IOAPIC_ID;
        break;
      case 15:
        IoApicId = PC15_IOAPIC_ID;
        break;
      case 16:
        IoApicId = PC16_IOAPIC_ID;
        break;
      case 17:
        IoApicId = PC17_IOAPIC_ID;
        break;
      case 18:
        IoApicId = PC18_IOAPIC_ID;
        break;
      case 19:
        IoApicId = PC19_IOAPIC_ID;
        break;
      case 20:
        IoApicId = PC20_IOAPIC_ID;
        break;
      case 21:
        IoApicId = PC21_IOAPIC_ID;
        break;
      case 22:
        IoApicId = PC22_IOAPIC_ID;
        break;
      case 23:
        IoApicId = PC23_IOAPIC_ID;
        break;
      case 24:
        IoApicId = PC24_IOAPIC_ID;
        break;
      case 25:
        IoApicId = PC25_IOAPIC_ID;
        break;
      case 26:
        IoApicId = PC26_IOAPIC_ID;
        break;
      case 27:
        IoApicId = PC27_IOAPIC_ID;
        break;
      case 28:
        IoApicId = PC28_IOAPIC_ID;
        break;
      case 29:
        IoApicId = PC29_IOAPIC_ID;
        break;
      case 30:
        IoApicId = PC30_IOAPIC_ID;
        break;
      case 31:
        IoApicId = PC31_IOAPIC_ID;
        break;
      case 32:
        IoApicId = PC32_IOAPIC_ID;
        break;
      case 33:
        IoApicId = PC33_IOAPIC_ID;
        break;
      case 34:
        IoApicId = PC34_IOAPIC_ID;
        break;
      case 35:
        IoApicId = PC35_IOAPIC_ID;
        break;
      case 36:
        IoApicId = PC36_IOAPIC_ID;
        break;
      case 37:
        IoApicId = PC37_IOAPIC_ID;
        break;
      case 38:
        IoApicId = PC38_IOAPIC_ID;
        break;
      case 39:
        IoApicId = PC39_IOAPIC_ID;
        break;
      case 40:
        IoApicId = PC40_IOAPIC_ID;
        break;
      case 41:
        IoApicId = PC41_IOAPIC_ID;
        break;
      case 42:
        IoApicId = PC42_IOAPIC_ID;
        break;
      case 43:
        IoApicId = PC43_IOAPIC_ID;
        break;
      case 44:
        IoApicId = PC44_IOAPIC_ID;
        break;
      case 45:
        IoApicId = PC45_IOAPIC_ID;
        break;
      case 46:
        IoApicId = PC46_IOAPIC_ID;
        break;
      case 47:
        IoApicId = PC47_IOAPIC_ID;
        break;
      default:
        break;
      }
      if ((Socket == 0) && (Stack == 0)) {
        ProgramIoApicId ((mIioUds->IioUdsPtr->PlatformData.IIO_resource[Socket].StackRes[Stack].IoApicBase + 0x1000), IoApicId);
      } else {
        ProgramIoApicId (mIioUds->IioUdsPtr->PlatformData.IIO_resource[Socket].StackRes[Stack].IoApicBase, IoApicId);
      }
    }
  }
 
  AttemptToSetXhciMse ();
 
  return EFI_SUCCESS;
}
 
/**
 
  The PlatformPrepController() function can be used to notify the platform driver so that
  it can perform platform-specific actions. No specific actions are required.
  Several notification points are defined at this time. More synchronization points may be
  added as required in the future. The PCI bus driver calls the platform driver twice for
  every PCI controller-once before the PCI Host Bridge Resource Allocation Protocol driver
  is notified, and once after the PCI Host Bridge Resource Allocation Protocol driver has
  been notified.
  This member function may not perform any error checking on the input parameters. It also
  does not return any error codes. If this member function detects any error condition, it
  needs to handle those errors on its own because there is no way to surface any errors to
  the caller.
 
  @param This          -  Pointer to the EFI_PCI_PLATFORM_PROTOCOL instance.
  @param HostBridge    -  The associated PCI Host bridge handle.
  @param RootBridge    -  The associated PCI root bridge handle.
  @param PciAddress    -  The address of the PCI device on the PCI bus.
  @param Phase         -  The phase of the PCI controller enumeration.
  @param ChipsetPhase  -  Defines the execution phase of the PCI chipset driver.
 
  @retval EFI_SUCCESS     -  The function completed successfully.
  @retval EFI_UNSUPPORTED -  Not supported.
 
**/
EFI_STATUS
EFIAPI
PlatformPrepController (
  IN  EFI_PCI_PLATFORM_PROTOCOL                      *This,
  IN  EFI_HANDLE                                     HostBridge,
  IN  EFI_HANDLE                                     RootBridge,
  IN  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS    PciAddress,
  IN  EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE   Phase,
  IN  EFI_PCI_CHIPSET_EXECUTION_PHASE                ChipsetPhase
  )
{
  EFI_STATUS                       Status = EFI_SUCCESS;
  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL  *RootBridgeIo;
  PCI_TYPE00                       Pci0;
  UINT64                           Address;
  UINT8                            SecBus;
  UINT8                            Device;
  UINT8                            Func;
  UINT64                           DummyData = 0xFFFFFFFF;
  UINT32                           DidVid;
  DYNAMIC_SI_LIBARY_PROTOCOL       *DynamicSiLibraryProtocol = NULL;
 
  if (mPciPrivateData.RootBridgeHandle == NULL) {
    mPciPrivateData.RootBridgeHandle = RootBridge;
  }
 
  Status = gBS->HandleProtocol (
                    mPciPrivateData.RootBridgeHandle,
                    &gEfiPciRootBridgeIoProtocolGuid,
                    (VOID **) &RootBridgeIo
                    );
  ASSERT_EFI_ERROR (Status);
 
  Status = gBS->LocateProtocol (&gDynamicSiLibraryProtocolGuid, NULL, &DynamicSiLibraryProtocol);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
 
// Workaround for PCI devices under Pilot IV, this video controller can only be exposed if write 0xFFFFFFFF to it and read back
  if (Phase == EfiPciBeforeChildBusEnumeration && ChipsetPhase == ChipsetExit ) {
 
// Read the entire config header
    Address = EFI_PCI_ADDRESS (PciAddress.Bus, PciAddress.Device, PciAddress.Function, 0);
    Status = RootBridgeIo->Pci.Read (
                                RootBridgeIo,
                                EfiPciWidthUint32,
                                Address,
                                sizeof (PCI_TYPE00) / sizeof (UINT32),
                                &Pci0
                                );
 
    if (!EFI_ERROR (Status) && IS_PCI_BRIDGE(&Pci0)) {
 
      // Read the secondary bus number
      Address = EFI_PCI_ADDRESS (PciAddress.Bus, PciAddress.Device, PciAddress.Function, PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET);
      Status = RootBridgeIo->Pci.Read (
                                RootBridgeIo,
                                EfiPciWidthUint8,
                                Address,
                                1,
                                &SecBus
                                );
 
      if (!EFI_ERROR (Status)) {
 
        //
        // For this bridge with existing secondary bus apply PCI Intel WAs
        //
        DidVid = ((Pci0.Hdr).DeviceId << 16) | (Pci0.Hdr).VendorId;
        DynamicSiLibraryProtocol->IioPciHookBeforeEnumeration ((UINT8)RootBridgeIo->SegmentNumber, PciAddress.Bus, PciAddress.Device, PciAddress.Function, DidVid);
 
        for (Device = 0; Device <= PCI_MAX_DEVICE; Device++) {
          for (Func = 0; Func <= PCI_MAX_FUNC; Func++) {
            Address = EFI_PCI_ADDRESS (SecBus, Device, Func, 0);
            Status = RootBridgeIo->Pci.Read (
                                      RootBridgeIo,
                                      EfiPciWidthUint32,
                                      Address,
                                      1,
                                      &Pci0
                                      );
 
            if ( !EFI_ERROR (Status) && (Pci0.Hdr).VendorId == 0xffff) {
 
              Status = RootBridgeIo->Pci.Write(
                                        RootBridgeIo,
                                        EfiPciWidthUint32,
                                        Address,
                                        1,
                                        &DummyData
                                        );
              PCIDEBUG ("%a: For B(0x%x)-D(0x%x)-F(0x%x),Pci.Write() returns with %r\n",
                        __FUNCTION__, SecBus, Device, Func, Status);
 
              if (EFI_ERROR (Status)) {
                //
                // If error, go to next function
                //
                continue;
              } else {
                Func = PCI_MAX_FUNC;    // skip the remaining function
              }
            }
          }
        }
      }
    }
  }
 
  return EFI_SUCCESS;
}
 
/**
 
  Perform initialization by the phase indicated.
 
  @param This          -  Pointer to the EFI_PCI_PLATFORM_PROTOCOL instance.
  @param HostBridge    -  The associated PCI Host bridge handle.
  @param Phase         -  The phase of the PCI controller enumeration.
  @param ChipsetPhase  -  Defines the execution phase of the PCI chipset driver.
 
  @retval EFI_SUCCESS  -  Must return with success.
 
**/
EFI_STATUS
EFIAPI
PhaseNotify (
  IN  EFI_PCI_PLATFORM_PROTOCOL                      *This,
  IN  EFI_HANDLE                                     HostBridge,
  IN  EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE  Phase,
  IN  EFI_PCI_CHIPSET_EXECUTION_PHASE                ChipsetPhase
  )
{
  EFI_STATUS                                        Status = EFI_SUCCESS;
  UINT16                                            StackBit;
  UINT8                                             Socket;
  UINT8                                             Stack;
  EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL  *HostResAllocPtr;
  PCI_HOST_BRIDGE_INSTANCE                          *HostBridgePtr;
  PCI_ROOT_BRIDGE_INSTANCE                          *RootBridgePtr;
  LIST_ENTRY                                        *NodePtr;
  CPU_CSR_ACCESS_VAR                                *CpuCsrAccessVarPtr;
  DYNAMIC_SI_LIBARY_PROTOCOL                        *DynamicSiLibraryProtocol = NULL;
 
  static CHAR8 *NotifyPhase2Name[] = {"EfiPciHostBridgeBeginEnumeration",
                                      "EfiPciHostBridgeBeginBusAllocation",
                                      "EfiPciHostBridgeEndBusAllocation",
                                      "EfiPciHostBridgeBeginResourceAllocation",
                                      "EfiPciHostBridgeAllocateResources",
                                      "EfiPciHostBridgeSetResources",
                                      "EfiPciHostBridgeFreeResources",
                                      "EfiPciHostBridgeEndResourceAllocation",
                                      "EfiPciHostBridgeEndEnumeration"};
 
  if (Phase < NELEMENTS (NotifyPhase2Name)) {
    DEBUG ((DEBUG_INFO, "[PCI] %a phase notified (execution %d)\n", NotifyPhase2Name[Phase], ChipsetPhase));
  } else {
    DEBUG ((DEBUG_ERROR, "[PCI] ERROR: Unknown phase %d notified (execution %d)\n", Phase, ChipsetPhase));
  }
 
  Status = gBS->LocateProtocol (&gDynamicSiLibraryProtocolGuid, NULL, &DynamicSiLibraryProtocol);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
 
  CpuCsrAccessVarPtr = DynamicSiLibraryProtocol->GetSysCpuCsrAccessVar ();
 
  if (ChipsetPhase == ChipsetEntry) {
    return EFI_SUCCESS;
  }
  //
  // If for multiple Host bridges, need special consideration
  //
  switch (Phase) {
 
  case EfiPciHostBridgeBeginEnumeration:
    //
    // Pre-initialization before PCI bus enumeration
    // No bus number and no PCI resource
    // Locate the IIO Protocol Interface
    //
    Status = gBS->LocateProtocol (
                    &gEfiIioUdsProtocolGuid,
                    NULL,
                    &mIioUds
                    );
    ASSERT_EFI_ERROR (Status);
 
    Status = gBS->LocateProtocol (
                    &gEfiCpuIo2ProtocolGuid,
                    NULL,
                    &mPciPrivateData.CpuIo
                    );
    ASSERT_EFI_ERROR (Status);
 
    mPciPrivateData.Context.CpuIo = mPciPrivateData.CpuIo;
    DEBUG ((DEBUG_INFO, "[PCI] Platform Pre-Initialization (Before bus scanning)\n"));
    //
    // Locate gEfiPciRootBridgeIoProtocolGuid instance created for each IIO stack.
    // They were created by host bridge driver and linked to the
    // gEfiPciHostBridgeResourceAllocationProtocolGuid protocol.
    //
    Status = gBS->LocateProtocol (
                    &gEfiPciHostBridgeResourceAllocationProtocolGuid,
                    NULL,
                    &HostResAllocPtr
                    );
    ASSERT_EFI_ERROR (Status);
 
    HostBridgePtr = CR (HostResAllocPtr, PCI_HOST_BRIDGE_INSTANCE, ResAlloc, PCI_HOST_BRIDGE_SIGNATURE);
    for (NodePtr = GetFirstNode (&HostBridgePtr->RootBridges);
         !IsNull (&HostBridgePtr->RootBridges, NodePtr);
         NodePtr = GetNextNode (&HostBridgePtr->RootBridges, NodePtr)) {
      RootBridgePtr = CR (NodePtr, PCI_ROOT_BRIDGE_INSTANCE, Link, PCI_ROOT_BRIDGE_SIGNATURE);
      for (Socket = 0; Socket < NELEMENTS (mPciPrivateData.PciRootBridgeIo); Socket++) {
        if (!mIioUds->IioUdsPtr->PlatformData.IIO_resource[Socket].Valid) {
          continue;
        }
        for (StackBit = 1, Stack = 0;
             Stack < NELEMENTS (mPciPrivateData.PciRootBridgeIo[Socket]);
             StackBit <<= 1, Stack++) {
          if ((CpuCsrAccessVarPtr->stackPresentBitmap[Socket] & StackBit) &&
              CpuCsrAccessVarPtr->StackBus[Socket][Stack] == RootBridgePtr->Aperture.BusBase) {
            //
            // This is the stack handled by this instance of root bridge IO protocol. Store it for future use.
            //
            mPciPrivateData.PciRootBridgeIo[Socket][Stack] = &RootBridgePtr->RootBridgeIo;
            Socket = NELEMENTS (mPciPrivateData.PciRootBridgeIo);
            break;
          }
        }
      }
    }
    PciPlatformEarlyInit ();
    break;
 
  case EfiPciHostBridgeEndBusAllocation:
    //
    // There are two rounds PCI bus scanning
    // First round will initilize the PCI hotplug device
    // Second round will be the final one
    //
    if (mPciPrivateData.BusAssignedTime == 0) {
      mPciPrivateData.PciEnumerationPhase = EfiPciEnumerationDeviceScanning;
      for (Socket = 0; Socket < NELEMENTS (mPciPrivateData.PciRootBridgeIo); Socket++) {
        if (!mIioUds->IioUdsPtr->PlatformData.IIO_resource[Socket].Valid) {
          continue;
        }
        for (Stack = 0; Stack < NELEMENTS (mPciPrivateData.PciRootBridgeIo[Socket]); Stack ++) {
          if (mPciPrivateData.PciRootBridgeIo[Socket][Stack] == NULL) {
            continue;
          }
          PciTreeTraverse (Socket, Stack, CpuCsrAccessVarPtr->StackBus[Socket][Stack]);
        }
      }
      mPciPrivateData.BusAssignedTime++;
      DEBUG ((DEBUG_INFO, "[PCI] Platform bus assigned\n"));
    }
    break;
 
  case EfiPciHostBridgeBeginResourceAllocation:
    //
    // PCI bus number has been assigned, but resource is still empty
    //
    DEBUG ((DEBUG_INFO, "[PCI] Platform Mid-Initialization (After bus number assignment)\n"));
    mPciPrivateData.PciEnumerationPhase = EfiPciEnumerationBusNumberAssigned;
    for (Socket = 0; Socket < NELEMENTS (mPciPrivateData.PciRootBridgeIo); Socket++) {
      if (!mIioUds->IioUdsPtr->PlatformData.IIO_resource[Socket].Valid) {
        continue;
      }
      for (Stack = 0; Stack < NELEMENTS (mPciPrivateData.PciRootBridgeIo[Socket]); Stack ++) {
        if (mPciPrivateData.PciRootBridgeIo[Socket][Stack] == NULL) {
          continue;
        }
        PciTreeTraverse (Socket, Stack, CpuCsrAccessVarPtr->StackBus[Socket][Stack]);
      }
    }
    //PciPlatformMidInit ();
    break;
 
  case EfiPciHostBridgeEndResourceAllocation:
    //
    // Resource enumeration is done.
    // Both bus number and resource have been assigned
    // Do any post initialization.
    //
    DEBUG ((DEBUG_INFO, "[PCI] Platform Post-Initialization (After resource alloction)\n"));
    mPciPrivateData.PciEnumerationPhase = EfiPciEnumerationResourceAssigned;
    for (Socket = 0; Socket < NELEMENTS (mPciPrivateData.PciRootBridgeIo); Socket++) {
      if (!mIioUds->IioUdsPtr->PlatformData.IIO_resource[Socket].Valid) {
        continue;
      }
      for (Stack = 0; Stack < NELEMENTS (mPciPrivateData.PciRootBridgeIo[Socket]); Stack ++) {
        if (mPciPrivateData.PciRootBridgeIo[Socket][Stack] == NULL) {
          continue;
        }
        PciTreeTraverse (Socket, Stack, CpuCsrAccessVarPtr->StackBus[Socket][Stack]);
      }
    }
    PciPlatformPostInit ();
    break;
 
  default:
    return EFI_UNSUPPORTED;
  }
 
  return EFI_SUCCESS;
}