summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/SaInit/Dxe/VTd.c
blob: 1dea621030f131241e05e3c6c0ca312a352919bb (plain)
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
/** @file
  This code provides a initialization of intel VT-d (Virtualization Technology for Directed I/O).

@copyright
  Copyright (c) 1999 - 2013 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement

@todo assumption is ANDD table will use device path to be loaded dynamically, need to use pchplatformpolicy if assumption is incorrect.
**/
#include "SaInit.h"
#include "VTd.h"

UINT32                           mPchRootComplexBar;
DXE_PLATFORM_SA_POLICY_PROTOCOL  *mDxePlatformSaPolicy;
DXE_PCH_PLATFORM_POLICY_PROTOCOL *mDxePlatformPchPolicy;
PCH_SERIES                       mPchSeries;

/**
For device that specified by Device Num and Function Num,
mDevEnMap is used to check device presence.
  0x80 means use Device ID to detemine presence

 The structure is used to check if device scope is valid when update DMAR table
**/
UINT16  mDevEnMap[][2] = {
  {
    0x0200,
    0x80
  },  ///< D2F0
  {
    0x1D00,
    0x80
  },  ///< D29F0
  {
    0x1A00,
    0x80
  },  ///< D26F0
  {
    0x1400,
    0x80
  }   ///< D20F0
};

BOOLEAN mInterruptRemappingSupport;

UINT16
GetFunDisableBit (
  UINT8 DevNum,
  UINT8 FunNum
  )
/**
    Get the corresponding device Enable/Disable bit according DevNum and FunNum

    @param[in] DevNum  - Device Number
    @param[in] FunNum  - Function Number

    @retval If the device is found, return disable/Enable bit in FD/Deven reigster
    @retval If not found return 0xFF
**/
{
  UINTN Index;

  for (Index = 0; Index < sizeof (mDevEnMap) / 4; Index++) {
    if (mDevEnMap[Index][0] == ((DevNum << 0x08) | FunNum)) {
      return mDevEnMap[Index][1];
    }
  }

  return 0xFF;
}

VOID
UpdateDRHD (
  IN OUT VOID *DrhdEnginePtr
  )
/**
    Update the DRHD structure

    @param[in, out] DrhdEnginePtr       - A pointer to DRHD structure
**/
{
  UINT16                        Length;
  UINT16                        DisableBit;
  UINTN                         DeviceScopeNum;
  BOOLEAN                       NeedRemove;
  EFI_ACPI_DRHD_ENGINE1_STRUCT  *DrhdEngine;

  //
  // Convert DrhdEnginePtr to EFI_ACPI_DRHD_ENGINE1_STRUCT Pointer
  //
  DrhdEngine      = (EFI_ACPI_DRHD_ENGINE1_STRUCT *) DrhdEnginePtr;

  Length          = DrhdEngine->Length;
  DeviceScopeNum  = (DrhdEngine->Length - EFI_ACPI_DRHD_ENGINE_HEADER_LENGTH) / sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE);
  DisableBit = GetFunDisableBit (
                DrhdEngine->DeviceScope[0].PciPath[0],
                DrhdEngine->DeviceScope[0].PciPath[1]
                );
  NeedRemove = FALSE;
  if ((DisableBit == 0xFF) ||
      (DrhdEngine->RegisterBaseAddress == 0) ||
      ((DisableBit == 0x80) &&
      (MmPci32 (0, 0, DrhdEngine->DeviceScope[0].PciPath[0], DrhdEngine->DeviceScope[0].PciPath[1], 0x00) == 0xFFFFFFFF))
      ){
        NeedRemove = TRUE;
  }
  if (NeedRemove) {
    Length -= sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE);
  }
  ///
  /// If no devicescope is left, we set the structure length as 0x00
  ///
  if ((Length > EFI_ACPI_DRHD_ENGINE_HEADER_LENGTH) || (DrhdEngine->Flags == 0x01)) {
    DrhdEngine->Length = Length;
  } else {
    DrhdEngine->Length = 0;
  }
}

UINT8
GetIoApicID (
  VOID
  )
/**
    Get IOAPIC ID from LPC

    @retval APIC ID
**/
{
  UINT8 VOLATILE  *IoapicIndex;
  UINT32 VOLATILE *IoapicData;
  UINT32          Data32;

  ///
  /// Get IOAPIC base
  ///
  IoapicIndex = (UINT8 *) (UINTN) (R_PCH_IO_APIC_INDEX + ((PchMmRcrb16 (R_PCH_RCRB_OIC) & 0x0ff) << 12));
  IoapicData  = (UINT32 *) (UINTN) (R_PCH_IO_APIC_DATA + ((PchMmRcrb16 (R_PCH_RCRB_OIC) & 0x0ff) << 12));

  ///
  /// Get APIC ID from Identification Register (Index 0)
  ///
  *IoapicIndex  = 0;
  Data32        = (*IoapicData & 0x0F000000) >> 24;

  return (UINT8) Data32;
}

VOID
UpdateDRHD2 (
  IN OUT VOID *DrhdEnginePtr
  )
/**
    Update the second DRHD structure

    @param[in, out] DrhdEnginePtr       - A pointer to DRHD structure
**/
{
  UINT16                        Length;
  UINTN                         DeviceScopeNum;
  UINTN                         ValidDeviceScopeNum;
  UINT16                        Data16;
  UINT16                        HpetReg;
  UINT16                        Index;
  UINT8                         Bus;
  UINT8                         Path[2] = { 0, 0 };
  BOOLEAN                       NeedRemove;
  EFI_ACPI_DRHD_ENGINE2_STRUCT  *DrhdEngine;

  ///
  /// Convert DrhdEnginePtr to EFI_ACPI_DRHD_ENGINE2_STRUCT Pointer
  ///
  DrhdEngine      = (EFI_ACPI_DRHD_ENGINE2_STRUCT *) DrhdEnginePtr;

  Length          = DrhdEngine->Length;
  DeviceScopeNum  = (DrhdEngine->Length - EFI_ACPI_DRHD_ENGINE_HEADER_LENGTH) / sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE);
  Data16          = 0;
  Bus             = 0;
  HpetReg         = R_PCH_LPC_HPET0;
  ValidDeviceScopeNum = 0;

  for (Index = 0; Index < DeviceScopeNum; Index++) {
    NeedRemove = FALSE;
    /**
      For HPET and APIC, update device scope if Interrupt remapping is supported. remove device scope
      if interrupt remapping is not supported.
      - Index = 0 - IOAPIC
      - Index = 1 - HPET
      For Serial IO devices, they do not appear in PCI space, use platform policy to determine existence, also remove if PCH not LP
      - Index = 2 - I2C0
      - Index = 3 - I2C1
      - Index = 4 - SPI0
      - Index = 5 - SPI1
      - Index = 6 - UART0
      - Index = 7 - UART1
      - Index = 8 - SDIO
    **/
    if (mInterruptRemappingSupport) {
      if (Index == 0) {
        ///
        /// Update source id for IoApic's device scope entry
        ///
        Data16  = PchLpcPciCfg16 (R_PCH_LPC_IOXAPIC);
        Bus     = (UINT8) (Data16 >> 8);
        if (Bus != 0x00) {
          Path[0] = (UINT8) ((Data16 & 0xff) >> 3);
          Path[1] = (UINT8) (Data16 & 0x7);
        } else {
          //
          // BUGBUG: Here we just hardcode, because in this version, R_PCH_LPC_IOXAPIC is initialized AFTER Vtd run.  We can NOT get proper setting from PCH
          // We can NOT get proper setting from PCH
          /// @todo check if code still needed
          //
          DEBUG ((EFI_D_WARN, "BUGBUG: UpdateApicHpet use hardcode value - To be fixed!\n"));
          Bus     = 0xF0;
          Path[0] = 0x1F;
          Path[1] = 0x0;
        }
        DrhdEngine->DeviceScope[Index].StartBusNumber = Bus;
        //
        // Update APIC ID
        //
        DrhdEngine->DeviceScope[Index].EnumId = GetIoApicID ();
      }
      if (Index == 1) {
        ///
        /// Update source id for HPET's device scope entry
        ///
        Data16  = PchLpcPciCfg16 (HpetReg);
        Bus     = (UINT8) (Data16 >> 8);
        Path[0] = (UINT8) ((Data16 & 0xFF) >> 3);
        Path[1] = (UINT8) (Data16 & 0x7);
        DrhdEngine->DeviceScope[Index].StartBusNumber = Bus;
      }
    } else {
      if ((Index == 0) || (Index == 1)) {
        NeedRemove = TRUE;
      }
    }
    /*
    Pch removed device from PCI space and it is visible by ACPI only, we use platform policy to check
    if device is present. If Pch is 2 chip, remove all serialio devices.
    */
#ifdef SERIAL_IO_FLAG
    if (mPchSeries == PchLp){
      if (Index == 2){
        if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoI2c0 == 0){
          NeedRemove = TRUE;
        }
      }
      if (Index == 3){
        if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoI2c1 == 0){
          NeedRemove = TRUE;
        }
      }
      if (Index == 4){
        if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoSpi0 == 0){
          NeedRemove = TRUE;
        }
      }
      if (Index == 5){
        if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoSpi1 == 0){
          NeedRemove = TRUE;
        }
      }
      if (Index == 6){
        if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoUart0 == 0){
          NeedRemove = TRUE;
        }
      }
      if (Index == 7){
        if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoUart1 == 0){
          NeedRemove = TRUE;
        }
      }
      if (Index == 8){
        if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoSdio == 0){
          NeedRemove = TRUE;
        }
      }
    } else {
#endif
      if ((Index >= 2) && (Index <= 8)){
        NeedRemove = TRUE;
      }
#ifdef SERIAL_IO_FLAG
    }
#endif
    CopyMem (
      &DrhdEngine->DeviceScope[ValidDeviceScopeNum],
      &DrhdEngine->DeviceScope[Index],
      sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE)
      );
    if (NeedRemove) {
      Length -= sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE);
    } else {
      ValidDeviceScopeNum++;
    }
  }
  ///
  /// If no devicescope is left, we set the structure length as 0x00
  ///
  if ((Length > EFI_ACPI_DRHD_ENGINE_HEADER_LENGTH) || (DrhdEngine->Flags == 0x01)) {
    DrhdEngine->Length = Length;
  } else {
    DrhdEngine->Length = 0;
  }
}

VOID
UpdateRMRR (
  IN OUT VOID *RmrrPtr
  )
/**
    Update the RMRR structure

    @param[in, out] RmrrPtr             - A pointer to RMRR structure
**/
{
  UINT16                  Length;
  UINT16                  DisableBit;
  UINTN                   DeviceScopeNum;
  UINTN                   ValidDeviceScopeNum;
  UINTN                   Index;
  BOOLEAN                 NeedRemove;
  EFI_ACPI_RMRR_USB_STRUC *Rmrr;

  ///
  /// To make sure all devicescope can be checked,
  /// we convert the RmrrPtr to EFI_ACPI_RMRR_USB_STRUC pointer
  ///
  Rmrr                = (EFI_ACPI_RMRR_USB_STRUC *) RmrrPtr;
  Length              = Rmrr->Length;
  ValidDeviceScopeNum = 0;
  DeviceScopeNum      = (Rmrr->Length - EFI_ACPI_RMRR_HEADER_LENGTH) / sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE);
  for (Index = 0; Index < DeviceScopeNum; Index++) {
    ///
    /// here we assume Device will exist on PCH if Device Number is greater than 0x0F
    ///
    DisableBit = GetFunDisableBit (
                  Rmrr->DeviceScope[Index].PciPath[0],
                  Rmrr->DeviceScope[Index].PciPath[1]
                  );
    NeedRemove = FALSE;
    if ((DisableBit == 0xFF) ||
        ((DisableBit == 0x80) &&
        (MmPci32 (0, 0, Rmrr->DeviceScope[Index].PciPath[0], Rmrr->DeviceScope[Index].PciPath[1], 0x00) == 0xFFFFFFFF))
        ){
      NeedRemove = TRUE;
    }
    CopyMem (
      &Rmrr->DeviceScope[ValidDeviceScopeNum],
      &Rmrr->DeviceScope[Index],
      sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE)
      );
    if (NeedRemove) {
      Length -= sizeof (EFI_ACPI_DEV_SCOPE_STRUCTURE);
    } else {
      ValidDeviceScopeNum++;
    }
  }
  ///
  /// If No deviceScope is left, set length as 0x00
  ///
  if (Length > EFI_ACPI_RMRR_HEADER_LENGTH) {
    Rmrr->Length = Length;
  } else {
    Rmrr->Length = 0;
  }
}

VOID
DmarTableUpdate (
  IN OUT   EFI_ACPI_DESCRIPTION_HEADER       *TableHeader,
  IN OUT   EFI_ACPI_TABLE_VERSION            *Version
  )
/**
  Update the DMAR table

  @param[in, out] TableHeader         - The table to be set
  @param[in, out] Version             - Version to publish
**/
{
  EFI_ACPI_DMAR_TABLE *DmarTable;
  EFI_ACPI_DMAR_TABLE TempDmarTable;
  UINTN               Offset;
  UINTN               StructureLen;
  UINT16              IgdMode;
  UINT16              GttMode;
  UINT32              IgdMemSize;
  UINT32              GttMemSize;

  IgdMemSize  = 0;
  GttMemSize  = 0;
  DmarTable = (EFI_ACPI_DMAR_TABLE *) TableHeader;

  if (mInterruptRemappingSupport) {
    DmarTable->Flags |= BIT0; ///< Set INTR_REMAP bit (BIT 0) if interrupt remapping is supported
  }
  ///
  /// Find IGD memsize
  ///
  IgdMode = (McD0PciCfg16 (R_SA_GGC) & B_SA_GGC_GMS_MASK) >> 3;
  if (IgdMode <= V_SA_GGC_GMS_512MB) {
    IgdMemSize = IgdMode * 32 * (1024) * (1024);
  } else {
    IgdMemSize = 0;
  }
  ///
  /// Find GTT mem size
  ///
  GttMode = (McD0PciCfg16 (R_SA_GGC) & B_SA_GGC_GGMS_MASK) >> 8;
  if (GttMode <= V_SA_GGC_GGMS_2MB) {
    GttMemSize = GttMode * (1024) * (1024);
  } else {
    GttMemSize = 0;
  }

  DmarTable->RmrrIgd.RmrBaseAddress   = (McD0PciCfg32 (R_SA_TOLUD) &~(0x01)) - IgdMemSize - GttMemSize;
  DmarTable->RmrrIgd.RmrLimitAddress  = DmarTable->RmrrIgd.RmrBaseAddress + IgdMemSize + GttMemSize - 1;
  DEBUG ((EFI_D_INFO, "RMRR Base  address IGD %016lX\n", DmarTable->RmrrIgd.RmrBaseAddress));
  DEBUG ((EFI_D_INFO, "RMRR Limit address IGD %016lX\n", DmarTable->RmrrIgd.RmrLimitAddress));

  DmarTable->RmrrUsb.RmrBaseAddress   = mDxePlatformSaPolicy->Vtd->RmrrUsbBaseAddress[0];
  DmarTable->RmrrUsb.RmrLimitAddress  = mDxePlatformSaPolicy->Vtd->RmrrUsbBaseAddress[1];

  ///
  /// Convert to 4KB alignment.
  ///
  DmarTable->RmrrUsb.RmrBaseAddress &= ~0xFFF;
  DmarTable->RmrrUsb.RmrLimitAddress &= ~0xFFF;
  DmarTable->RmrrUsb.RmrLimitAddress += 0x1000 - 1;

  DEBUG ((EFI_D_INFO, "RMRR Base  address USB %016lX\n", DmarTable->RmrrUsb.RmrBaseAddress));
  DEBUG ((EFI_D_INFO, "RMRR Limit address USB %016lX\n", DmarTable->RmrrUsb.RmrLimitAddress));

  ///
  /// @todo check if this check is still needed.
  ///
  if (DmarTable->RmrrUsb.RmrBaseAddress == 0) {
    DEBUG ((EFI_D_WARN, "BUGBUG:  RmrrUsb.RmrBaseAddress is 0 - To be fixed\n"));
  }
  ///
  /// Update DRHD structures of DmarTable
  ///
  DmarTable->DrhdEngine1.RegisterBaseAddress = (McMmio32 (R_SA_MCHBAR_VTD1_OFFSET) &~1);
  DmarTable->DrhdEngine2.RegisterBaseAddress = (McMmio32 (R_SA_MCHBAR_VTD2_OFFSET) &~1);

  DEBUG ((EFI_D_INFO, "VTD base address1 %x\n", DmarTable->DrhdEngine1.RegisterBaseAddress));
  DEBUG ((EFI_D_INFO, "VTD base address2 %x\n", DmarTable->DrhdEngine2.RegisterBaseAddress));
  ///
  /// copy DmarTable to TempDmarTable to be processed
  ///
  CopyMem (&TempDmarTable, DmarTable, sizeof (EFI_ACPI_DMAR_TABLE));

  ///
  /// Update DRHD structures of temp DMAR table
  ///
  UpdateDRHD (&TempDmarTable.DrhdEngine1);
  UpdateDRHD2 (&TempDmarTable.DrhdEngine2);

  ///
  /// Update RMRR structures of temp DMAR table
  ///
  UpdateRMRR ((VOID *) &TempDmarTable.RmrrUsb);
  UpdateRMRR ((VOID *) &TempDmarTable.RmrrIgd);

  ///
  /// Remove unused device scope or entire DRHD structures
  ///
  Offset = (UINTN) (&TempDmarTable.DrhdEngine1);
  if (TempDmarTable.DrhdEngine1.Length != 0) {
    Offset += TempDmarTable.DrhdEngine1.Length;
  }
  if (TempDmarTable.DrhdEngine2.Length != 0) {
    StructureLen = TempDmarTable.DrhdEngine2.Length;
    CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.DrhdEngine2, TempDmarTable.DrhdEngine2.Length);
    Offset += StructureLen;
  }
  ///
  /// Remove unused device scope or entire RMRR structures
  ///
  if (TempDmarTable.RmrrUsb.Length != 0) {
    StructureLen = TempDmarTable.RmrrUsb.Length;
    CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.RmrrUsb, TempDmarTable.RmrrUsb.Length);
    Offset += StructureLen;
  }
  if (TempDmarTable.RmrrIgd.Length != 0) {
    StructureLen = TempDmarTable.RmrrIgd.Length;
    CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.RmrrIgd, TempDmarTable.RmrrIgd.Length);
    Offset += StructureLen;
  }
#ifdef SERIAL_IO_FLAG
  ///
  /// Include necessary ANDD structures. If not PchLp, remove all ANDD.
  ///
  if (mPchSeries == PchLp){
    if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoI2c0 != 0) {
      StructureLen = TempDmarTable.AnddI2C0.Length;
      CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.AnddI2C0, TempDmarTable.AnddI2C0.Length);
      Offset += StructureLen;
    }
    if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoI2c1 != 0) {
      StructureLen = TempDmarTable.AnddI2C1.Length;
      CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.AnddI2C1, TempDmarTable.AnddI2C1.Length);
      Offset += StructureLen;
    }
    if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoSpi0 != 0) {
      StructureLen = TempDmarTable.AnddSpi0.Length;
      CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.AnddSpi0, TempDmarTable.AnddSpi0.Length);
      Offset += StructureLen;
    }
    if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoSpi1 != 0) {
      StructureLen = TempDmarTable.AnddSpi1.Length;
      CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.AnddSpi1, TempDmarTable.AnddSpi1.Length);
      Offset += StructureLen;
    }
    if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoUart0 != 0) {
      StructureLen = TempDmarTable.AnddUa00.Length;
      CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.AnddUa00, TempDmarTable.AnddUa00.Length);
      Offset += StructureLen;
    }
    if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoUart1 != 0) {
      StructureLen = TempDmarTable.AnddUa01.Length;
      CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.AnddUa01, TempDmarTable.AnddUa01.Length);
      Offset += StructureLen;
    }
    if (mDxePlatformPchPolicy->DeviceEnabling->SerialIoSdio != 0) {
      StructureLen = TempDmarTable.AnddSdhc.Length;
      CopyMem ((VOID *) Offset, (VOID *) &TempDmarTable.AnddSdhc, TempDmarTable.AnddSdhc.Length);
      Offset += StructureLen;
    }
  }
#endif
  Offset = Offset - (UINTN) &TempDmarTable;
  TempDmarTable.Header.Checksum = (UINT8) (TempDmarTable.Header.Checksum + TempDmarTable.Header.Length - Offset);
  TempDmarTable.Header.Length = (UINT32) Offset;
  ///
  /// Replace DMAR table with rebuilt table TempDmarTable
  ///
  CopyMem ((VOID *) DmarTable, (VOID *) &TempDmarTable, TempDmarTable.Header.Length);
}

EFI_STATUS
WaForVc0RemappingEngine (
  UINT64                      MchBar
  )
/**
  Workaround for VC0 remapping engine

  @param[in] MchBar             - MCHBAR address

  @retval EFI_SUCCESS           - successed.
**/
{
  UINT16              DeviceId;
  UINT32              Vc0RemapEngineBase;
  UINT32              Data32Or;
  UINT32              Data32And;

  DeviceId  = PchLpcPciCfg16(R_PCH_LPC_DEVICE_ID);
  Vc0RemapEngineBase = Mmio32(MchBar, R_SA_MCHBAR_VTD2_OFFSET) & 0xFFFFFFFE;

  ///
  /// Disable VTD SuperPage policy when iGfx is enabled
  ///
  if (McD2PciCfg16(R_SA_IGD_VID) != 0xFFFF) {
    Data32And = (UINT32)~(BIT25);
    Data32Or  = 0;
    Mmio32And(Vc0RemapEngineBase, 0xFF0, Data32And);
    SCRIPT_MEM_READ_WRITE
      (
        EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
        EfiBootScriptWidthUint32,
        (UINTN) (Vc0RemapEngineBase + 0xFF0),
        &Data32Or,  ///< Data to be ORed
        &Data32And  ///< Data to be ANDed
      );
  }

  return EFI_SUCCESS;
}

VOID
UpdateDmarExitPmAuth (
  VOID
  )
/**
  ExitPmAuth routine for update DMAR
**/
{
  EFI_STATUS                      Status;
  EFI_HANDLE                      *HandleBuffer;
  UINTN                           NumberOfHandles;
  EFI_FV_FILETYPE                 FileType;
  UINT32                          FvStatus;
  EFI_FV_FILE_ATTRIBUTES          Attributes;
  UINTN                           Size;
  UINTN                           i;
  INTN                            Instance;
  EFI_ACPI_TABLE_VERSION          Version;
  EFI_ACPI_COMMON_HEADER          *CurrentTable;
  UINTN                           AcpiTableHandle;
  EFI_FIRMWARE_VOLUME_PROTOCOL    *FwVol;
  EFI_ACPI_TABLE_PROTOCOL         *AcpiTable;
  EFI_ACPI_DESCRIPTION_HEADER     *VtdAcpiTable;
  STATIC BOOLEAN                  Triggered = FALSE;

  if (Triggered) {
    return;
  }

  Triggered     = TRUE;

  FwVol         = NULL;
  AcpiTable     = NULL;
  VtdAcpiTable  = NULL;

  ///
  /// Locate PCH platform policy protocol and PCH series to support feature enabling/disabling
  ///
  Status = gBS->LocateProtocol (
                  &gDxePchPlatformPolicyProtocolGuid,
                  NULL,
                  (VOID**) &mDxePlatformPchPolicy
                  );
  ASSERT_EFI_ERROR (Status);
  mPchSeries  = GetPchSeries();

  if ((!mDxePlatformSaPolicy->Vtd->VtdEnable) || (McD0PciCfg32 (R_SA_MC_CAPID0_A_OFFSET) & BIT23)) {
      DEBUG ((EFI_D_INFO, "Vtd Disabled, skip DMAR Table install\n"));

      return;
  }

  ///
  /// Locate ACPI support protocol
  ///
  Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTable);

  ///
  /// Locate protocol.
  /// There is little chance we can't find an FV protocol
  ///
  Status = gBS->LocateHandleBuffer (
                  ByProtocol,
                  &gEfiFirmwareVolumeProtocolGuid,
                  NULL,
                  &NumberOfHandles,
                  &HandleBuffer
                  );
  ASSERT_EFI_ERROR (Status);

  ///
  /// Looking for FV with ACPI storage file
  ///
  for (i = 0; i < NumberOfHandles; i++) {
    ///
    /// Get the protocol on this handle
    /// This should not fail because of LocateHandleBuffer
    ///
    Status = gBS->HandleProtocol (
                    HandleBuffer[i],
                    &gEfiFirmwareVolumeProtocolGuid,
                    (VOID **) &FwVol
                    );
    ASSERT_EFI_ERROR (Status);

    ///
    /// See if it has the ACPI storage file
    ///
    Size      = 0;
    FvStatus  = 0;
    Status = FwVol->ReadFile (
                      FwVol,
                      &gSaAcpiTableStorageGuid,
                      NULL,
                      &Size,
                      &FileType,
                      &Attributes,
                      &FvStatus
                      );

    ///
    /// If we found it, then we are done
    ///
    if (Status == EFI_SUCCESS) {
      break;
    }
  }
  ///
  /// Our exit status is determined by the success of the previous operations
  /// If the protocol was found, Instance already points to it.
  ///
  ///
  /// Free any allocated buffers
  ///
  FreePool (HandleBuffer);

  ///
  /// Sanity check that we found our data file
  ///
  ASSERT (FwVol);
  if (FwVol == NULL) {
    return ;
  }
  ///
  /// By default, a table belongs in all ACPI table versions published.
  ///
  Version = EFI_ACPI_TABLE_VERSION_1_0B | EFI_ACPI_TABLE_VERSION_2_0 | EFI_ACPI_TABLE_VERSION_3_0;

  ///
  /// Read tables from the storage file.
  ///
  Instance      = 0;
  CurrentTable  = NULL;

  while (Status == EFI_SUCCESS) {
    Status = FwVol->ReadSection (
                      FwVol,
                      &gSaAcpiTableStorageGuid,
                      EFI_SECTION_RAW,
                      Instance,
                      (VOID **) &CurrentTable,
                      &Size,
                      &FvStatus
                      );

    if (!EFI_ERROR (Status)) {
      ///
      /// Check the Signature ID to modify the table
      ///
      switch (((EFI_ACPI_DESCRIPTION_HEADER *) CurrentTable)->Signature) {

      case EFI_ACPI_VTD_DMAR_TABLE_SIGNATURE:
        VtdAcpiTable = (EFI_ACPI_DESCRIPTION_HEADER *) CurrentTable;
        DmarTableUpdate (VtdAcpiTable, &Version);
        break;

      default:
        break;
      }
      ///
      /// Increment the instance
      ///
      Instance++;
      CurrentTable = NULL;
    }
  }
  ///
  /// Update the VTD table in the ACPI tables.
  ///
  AcpiTableHandle = 0;
  if (VtdAcpiTable != NULL) {
    Status = AcpiTable->InstallAcpiTable (
                          AcpiTable,
                          VtdAcpiTable,
                          VtdAcpiTable->Length,
                          &AcpiTableHandle
                          );
  }
}

EFI_STATUS
VtdInit (
  IN  DXE_PLATFORM_SA_POLICY_PROTOCOL    *DxePlatformSaPolicy
  )
/**
@brief
  Locate the VT-d ACPI tables data file and read ACPI SSDT tables.
  Publish the appropriate SSDT based on current configuration and capabilities.

  @param[in] DxePlatformSaPolicy  -  SA DxePlatformPolicy protocol

  @retval EFI_SUCCESS     - Vtd initialization complete
  @exception EFI_UNSUPPORTED - Vtd is not enabled by policy
**/
{
  EFI_STATUS   Status;
  UINTN        i;
  UINT64       MchBar;
  UINT32       Data32Or;
  UINT32       Data32And;
  UINT32       VtdBase;
  UINT32       VtBarReg [SA_VTD_ENGINE_NUMBER];
  CPU_FAMILY   CpuFamilyId;
  CPU_STEPPING CpuStepping;

  mInterruptRemappingSupport  = FALSE;
  mPchRootComplexBar          = MmPci32 (0, 0, 31, 0, 0xF0) &~BIT0;
  MchBar                      = McD0PciCfg64 (R_SA_MCHBAR) &~BIT0;

  VtBarReg[0]                 = R_SA_MCHBAR_VTD1_OFFSET;
  VtBarReg[1]                 = R_SA_MCHBAR_VTD2_OFFSET;
  mDxePlatformSaPolicy        = DxePlatformSaPolicy;

  ///
  /// Check SA supports VTD and VTD is enabled in setup menu
  ///
  if ((!mDxePlatformSaPolicy->Vtd->VtdEnable) || (McD0PciCfg32 (R_SA_MC_CAPID0_A_OFFSET) & BIT23)) {
    DEBUG ((EFI_D_WARN, "VTd disabled or no capability!\n"));
    return EFI_UNSUPPORTED;
  }
  DEBUG ((EFI_D_INFO, "VTd enabled\n"));

  ///
  /// 14.1 Program Remap Engine Base Address
  /// Configure VTD1 BAR
  ///
  i = 0;

  ///
  /// Skip GFXVTBAR if IGD is disabled
  ///
  if (McD2PciCfg16 (R_SA_IGD_VID) != 0xFFFF) {
    Data32Or = mDxePlatformSaPolicy->Vtd->BaseAddress[i];
    Data32Or |= 0x1;
    Mmio32 (MchBar, R_SA_MCHBAR_VTD1_OFFSET) = Data32Or;
    SCRIPT_MEM_WRITE (
      EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
      EfiBootScriptWidthUint32,
      (UINTN) (MchBar + R_SA_MCHBAR_VTD1_OFFSET),
      1,
      &Data32Or
      );
    i++;
  }

  ///
  /// Configure VTD2 BAR
  ///
  Data32Or = mDxePlatformSaPolicy->Vtd->BaseAddress[i];
  Data32Or |= 0x1;
  Mmio32 (MchBar, R_SA_MCHBAR_VTD2_OFFSET) = Data32Or;
  SCRIPT_MEM_WRITE (
    EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
    EfiBootScriptWidthUint32,
    (UINTN) (MchBar + R_SA_MCHBAR_VTD2_OFFSET),
    1,
    &Data32Or
    );

  ///
  /// Workaround for VC0 remapping engine
  ///
  Status = WaForVc0RemappingEngine (MchBar);
  ASSERT_EFI_ERROR (Status);

  for (i = 0; i < SA_VTD_ENGINE_NUMBER; i++) {
    VtdBase = Mmio32 (MchBar, VtBarReg[i]) & 0xfffffffe;

    ///
    /// skip if the VT bar is 0
    ///
    if (VtdBase == 0) {
      continue;
    }

    CpuFamilyId = GetCpuFamily();
    CpuStepping = GetCpuStepping();

    if ((CpuFamilyId == EnumCpuHsw) || (CpuFamilyId == EnumCpuHswUlt) || (CpuFamilyId == EnumCpuCrw)
        ) {
      Data32And = (UINT32) ~(BIT15+BIT16+BIT17+BIT18+BIT19); ///< mask out 19:15
      Data32Or  = BIT15;
      if (i == 1) {
        Mmio32AndThenOr (VtdBase, 0xF04, Data32And, Data32Or);
        SCRIPT_MEM_READ_WRITE (
          EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
          EfiBootScriptWidthUint32,
          (UINTN) (VtdBase + 0xF04),
          &Data32Or,  ///< Data to be ORed
          &Data32And  ///< Data to be ANDed
        );
      }
    }
    ///
    /// 14.2 Set the remap engine policy bits
    ///
    Data32And = 0x0; ///< mask out all bits
    Data32Or  = 0;


    if ((CpuFamilyId == EnumCpuHsw) || (CpuFamilyId == EnumCpuHswUlt) || (CpuFamilyId == EnumCpuCrw)) {
      if (i == 0) {
        Data32Or |= 0x02100000;
      }
      if (i == 1) {
        if (McD2PciCfg16 (R_SA_IGD_VID) != 0xFFFF) {
          Data32Or |= 0x000A5003;
        } else {
          Data32Or |= 0x020A5003;
        }
      }
    }

    ///
    /// Set lock bit
    ///
    Data32Or |= BIT31;


    Mmio32AndThenOr (VtdBase, 0xFF0, Data32And, Data32Or);
    SCRIPT_MEM_READ_WRITE (
      EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
      EfiBootScriptWidthUint32,
      (UINTN) (VtdBase + 0xFF0),
      &Data32Or,  ///< Data to be ORed
      &Data32And  ///< Data to be ANDed
      );

    ///
    /// Check IR status
    ///
    if ((Mmio32 (VtdBase, VTD_ECAP_REG) & IR) && !(mInterruptRemappingSupport)) {
      mInterruptRemappingSupport = TRUE;
    }
  }

  return EFI_SUCCESS;
}