summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/SaInit/Dxe/SaInit.c
blob: af4d45ae6469f8071f914c41078182d43d97538b (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
/** @file
  This is the driver that initializes the Intel System Agent.

@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

**/
#include "SaInit.h"
#include EFI_GUID_DEFINITION (SaDataHob)
#include <Protocol/PciEnumerationComplete.h>
#include EFI_PROTOCOL_DEPENDENCY (SaGlobalNvsArea)
#include EFI_GUID_DEFINITION (SaSsdtTableStorage)

///
/// Global Variables
///
SYSTEM_AGENT_GLOBAL_NVS_AREA_PROTOCOL  mSaGlobalNvsAreaProtocol;
DXE_PLATFORM_SA_POLICY_PROTOCOL        *mDxePlatformSaPolicy;
UINT16                                 mSaIotrapSmiAddress;
BOOLEAN                                mInitPcieAspmAfterOprom;
EFI_GUID                               gEfiPciEnumerationCompleteProtocolGuid = EFI_PCI_ENUMERATION_COMPLETE_GUID;

EFI_STATUS
InitializeSaSsdtAcpiTables (
  VOID
  )
/**
@brief
  Initialize System Agent SSDT ACPI tables

  @retval EFI_SUCCESS    ACPI tables are initialized successfully
  @retval EFI_NOT_FOUND  ACPI tables not found
**/
{
  EFI_STATUS                    Status;
  EFI_HANDLE                    *HandleBuffer;
  UINTN                         NumberOfHandles;
  EFI_FV_FILETYPE               FileType;
  UINT32                        FvStatus;
  EFI_FV_FILE_ATTRIBUTES        Attributes;
  UINTN                         Size;
  UINTN                         i;
  EFI_FIRMWARE_VOLUME_PROTOCOL  *FwVol;
  INTN                          Instance;
  EFI_ACPI_COMMON_HEADER        *CurrentTable;
  UINTN                         AcpiTableKey;
  UINT8                         *CurrPtr;
  UINT8                         *EndPtr;
  UINT32                        *Signature;
  EFI_ACPI_DESCRIPTION_HEADER   *SaAcpiTable;
  EFI_ACPI_TABLE_PROTOCOL       *AcpiTable;

  FwVol               = NULL;
  SaAcpiTable = NULL;

  ///
  /// Locate ACPI Table protocol
  ///
  DEBUG ((EFI_D_INFO, "Init SA SSDT table\n"));
  Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, &AcpiTable);
  if (Status != EFI_SUCCESS) {
    DEBUG ((EFI_D_ERROR, "Fail to locate EfiAcpiTableProtocol.\n"));
    return EFI_NOT_FOUND;
  }

  ///
  /// 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,
                    &FwVol
                    );
    ASSERT_EFI_ERROR (Status);

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

    ///
    /// If we found it, then we are done
    ///
    if (Status == EFI_SUCCESS) {
      break;
    }
  }
  ///
  /// Free any allocated buffers
  ///
  FreePool (HandleBuffer);

  ///
  /// Sanity check that we found our data file
  ///
  ASSERT (FwVol != NULL);
  if (FwVol == NULL) {
    DEBUG ((EFI_D_INFO, "SA Global NVS table not found\n"));
    return EFI_NOT_FOUND;
  }
  ///
  /// Our exit status is determined by the success of the previous operations
  /// If the protocol was found, Instance already points to it.
  /// Read tables from the storage file.
  ///
  Instance      = 0;
  CurrentTable  = NULL;
  while (Status == EFI_SUCCESS) {
    Status = FwVol->ReadSection (
                      FwVol,
                      &gSaSsdtAcpiTableStorageGuid,
                      EFI_SECTION_RAW,
                      Instance,
                      &CurrentTable,
                      &Size,
                      &FvStatus
                      );

    if (!EFI_ERROR (Status)) {
      ///
      /// Check the table ID to modify the table
      ///
      if (((EFI_ACPI_DESCRIPTION_HEADER *) CurrentTable)->OemTableId == EFI_SIGNATURE_64 ('S', 'a', 'S', 's', 'd', 't', ' ', 0)) {
        SaAcpiTable = (EFI_ACPI_DESCRIPTION_HEADER *) CurrentTable;
        ///
        /// Locate the SSDT package
        ///
        CurrPtr = (UINT8 *) SaAcpiTable;
        EndPtr  = CurrPtr + SaAcpiTable->Length;

        for (; CurrPtr <= EndPtr; CurrPtr++) {
          Signature = (UINT32 *) (CurrPtr + 3);
          if (*Signature == EFI_SIGNATURE_32 ('S', 'A', 'N', 'V')) {
            ASSERT_EFI_ERROR (*(UINT32 *) (CurrPtr + 3 + sizeof (*Signature) + 2) == 0xFFFF0000);
            ASSERT_EFI_ERROR (*(UINT16 *) (CurrPtr + 3 + sizeof (*Signature) + 2 + sizeof (UINT32) + 1) == 0xAA55);
            ///
            /// SA Global NVS Area address
            ///
            *(UINT32 *) (CurrPtr + 3 + sizeof (*Signature) + 2) = (UINT32) (UINTN) mSaGlobalNvsAreaProtocol.Area;
            ///
            /// SA Global NVS Area size
            ///
            *(UINT16 *) (CurrPtr + 3 + sizeof (*Signature) + 2 + sizeof (UINT32) + 1) =
            sizeof (SYSTEM_AGENT_GLOBAL_NVS_AREA);

            AcpiTableKey = 0;
            Status = AcpiTable->InstallAcpiTable (
                                  AcpiTable,
                                  SaAcpiTable,
                                  SaAcpiTable->Length,
                                  &AcpiTableKey
                                  );
            ASSERT_EFI_ERROR (Status);
            return EFI_SUCCESS;
          }
        }
      }
      ///
      /// Increment the instance
      ///
      Instance++;
      CurrentTable = NULL;
    }
  }

  return Status;

}

EFI_STATUS
EFIAPI
SaInitEntryPoint (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
/**
  SystemAgent DXE Initialization.

  @param[in] ImageHandle             Handle for the image of this driver
  @param[in] SystemTable             Pointer to the EFI System Table

  @retval EFI_SUCCESS             The function completed successfully
  @retval EFI_OUT_OF_RESOURCES    No enough buffer to allocate
**/
{
  EFI_STATUS                Status;
  VOID                      *Registration;
  SA_INSTANCE_PRIVATE_DATA  *SaInstance;
  UINTN                     MCHBAR_BASE;
  SA_DATA_HOB               *SaDataHob;

  DEBUG ((EFI_D_INFO, "SaInitDxe Start\n"));

  INITIALIZE_SCRIPT (ImageHandle, SystemTable);
  SaInstance  = NULL;

  MCHBAR_BASE = McD0PciCfg64 (R_SA_MCHBAR) &~BIT0;

  ///
  /// Get the platform setup policy.
  ///
  Status = gBS->LocateProtocol (&gDxePlatformSaPolicyGuid, NULL, (VOID **) &mDxePlatformSaPolicy);
  ASSERT_EFI_ERROR (Status);

  ///
  /// Dump SA Platform Policy
  ///
  SaDxePolicyDump (mDxePlatformSaPolicy);

  ///
  /// Get SaDataHob HOB and see if ASPM should be programmed before or after OpROM.
  ///
  mSaIotrapSmiAddress    = 0;
  mInitPcieAspmAfterOprom = 0;
  SaDataHob              = NULL;
  SaDataHob              = (SA_DATA_HOB *)GetFirstGuidHob (&gSaDataHobGuid);
  if (SaDataHob != NULL) {
    mSaIotrapSmiAddress    = SaDataHob->SaIotrapSmiAddress;
    mInitPcieAspmAfterOprom = SaDataHob->InitPcieAspmAfterOprom;
  }

  ///
  /// If there was no DXE ASPM code, always executes SMM code
  ///
#if SA_PCIE_ASPM_IN_DXE == 0
  mInitPcieAspmAfterOprom = 1;
#endif
  ///
  /// If there was no SMM mode supported, always enable DXE mode
  ///
#if SA_PCIE_ASPM_IN_SMM == 0
  mInitPcieAspmAfterOprom = 0;
#endif

  ///
  /// Install System Agent Global NVS protocol
  ///
  DEBUG ((EFI_D_INFO, "Install SA GNVS protocol\n"));
  Status = (gBS->AllocatePool) (EfiACPIMemoryNVS, sizeof (SYSTEM_AGENT_GLOBAL_NVS_AREA), &mSaGlobalNvsAreaProtocol.Area);
  ASSERT_EFI_ERROR (Status);
  ZeroMem ((VOID *) mSaGlobalNvsAreaProtocol.Area, sizeof (SYSTEM_AGENT_GLOBAL_NVS_AREA));
  mSaGlobalNvsAreaProtocol.Area->SaRcRevision = SA_RC_VERSION;
  mSaGlobalNvsAreaProtocol.Area->XPcieCfgBaseAddress  = (UINT32) (MmPciAddress (0, 0, 0, 0, 0x0));

  Status = gBS->InstallMultipleProtocolInterfaces (
                  &ImageHandle,
                  &gSaGlobalNvsAreaProtocolGuid,
                  &mSaGlobalNvsAreaProtocol,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  ///
  /// PciExpress Dxe Initialization
  ///
  DEBUG ((EFI_D_INFO, "Initializing PciExpress (Dxe)\n"));
  PciExpressInit (mDxePlatformSaPolicy);

  ///
  /// Internal devices and Misc configurations.
  ///
  DEBUG ((EFI_D_INFO, "Internal Device and Misc Configurations\n"));
  DeviceConfigure (mDxePlatformSaPolicy);
  ProgramSvidSid (mDxePlatformSaPolicy);

  ///
  /// LegacyRegion Driver
  ///
  DEBUG ((EFI_D_INFO, "Initializing Legacy Region\n"));
  LegacyRegionInstall (ImageHandle);

  ///
  /// GtPostInit Initialization
  ///
  DEBUG ((EFI_D_INFO, "Initializing GT PowerManagement and other GT POST related\n"));
  GraphicsInit (ImageHandle, mDxePlatformSaPolicy);

  ///
  /// Audio (dHDA) Initialization
  ///
  DEBUG ((EFI_D_INFO, "Initializing CPU Audio (dHDA) device\n"));
  AudioInit (ImageHandle, mDxePlatformSaPolicy);

  //
  // @todo remove this VP SKIP and prepare for PO settings to skip this code.
  //
  ///
  /// Vtd Initialization
  ///
  DEBUG ((EFI_D_INFO, "Initializing VT-d\n"));
  VtdInit (mDxePlatformSaPolicy);

  ///
  /// IgdOpRegion Install Initialization
  ///
  DEBUG ((EFI_D_INFO, "Initializing IGD OpRegion\n"));
  IgdOpRegionInit ();


  ///
  /// Create PCI Enumeration Completed callback for SA
  ///
  EfiCreateProtocolNotifyEvent (
    &gEfiPciEnumerationCompleteProtocolGuid,
    EFI_TPL_CALLBACK,
    SaPciEnumCompleteCallback,
    NULL,
    &Registration
    );

  ///
  /// Create ExitPmAuth callback for SA
  ///
  EfiCreateProtocolNotifyEvent (
    &gExitPmAuthProtocolGuid,
    EFI_TPL_CALLBACK,
    SaExitPmAuthCallback,
    NULL,
    &Registration
    );

  ///
  /// Install SA_INFO_PROTOCOL
  ///
  SaInstance = AllocateZeroPool (sizeof (SA_INSTANCE_PRIVATE_DATA));
  if (SaInstance == NULL) {
    ASSERT (FALSE);
    return EFI_OUT_OF_RESOURCES;
  }

  SaInstance->SaInfo.Revision = SA_INFO_PROTOCOL_REVISION_1;
  ///
  /// RCVersion[31:0] is the release number.
  /// For example:
  /// SaFramework 0.6.0.01 should be 00 06 00 01 (0x00060001)
  /// SaFramework 0.6.2    should be 00 06 02 00 (0x00060200)
  ///
  SaInstance->SaInfo.RCVersion = SA_RC_VERSION;

  Status = gBS->InstallMultipleProtocolInterfaces (
                  &ImageHandle,
                  &gEfiSaInfoProtocolGuid,
                  &(SaInstance->SaInfo),
                  NULL
                  );

  ///
  /// Install System Agent Global NVS ACPI table
  ///
#ifndef Remove_SaSsdt_Data_To_Dsdt 
  Status = InitializeSaSsdtAcpiTables ();
#endif //AMI_OVERRIDE

#ifdef SG_SUPPORT

  DEBUG ((EFI_D_INFO, "Initializing Switchable Graphics (Dxe)\n"));
  SwitchableGraphicsInit (ImageHandle, SystemTable, mDxePlatformSaPolicy);

#endif

  DEBUG ((EFI_D_INFO, "SaInitDxe End\n"));

  return EFI_SUCCESS;
}

VOID
DeviceConfigure (
  IN DXE_PLATFORM_SA_POLICY_PROTOCOL *DxePlatformSaPolicy
  )
/**
  Initialize GT Post Routines.

    @param[in] ImageHandle         -     Handle for the image of this driver
    @param[in] DxePlatformSaPolicy -     SA DxePlatformPolicy protocol
**/
{
  UINT64  MchBar;

  MchBar = McD0PciCfg64 (R_SA_MCHBAR) &~BIT0;

  ///
  /// Enable/Disable CHAP device (B0,D7,F0).
  ///
  if (DxePlatformSaPolicy->MiscConfig->ChapDeviceEnable) {
    McD0PciCfg32Or (R_SA_DEVEN, B_SA_DEVEN_D7EN_MASK);
  } else {
    McD0PciCfg32And (R_SA_DEVEN, ~(B_SA_DEVEN_D7EN_MASK));
  }
  ///
  /// Enable/Disable Thermal device (B0,D4,F0).
  ///
  if (DxePlatformSaPolicy->MiscConfig->Device4Enable) {
    McD0PciCfg32Or (R_SA_DEVEN, B_SA_DEVEN_D4EN_MASK);
  } else {
    McD0PciCfg32And (R_SA_DEVEN, ~(B_SA_DEVEN_D4EN_MASK));
  }
  ///
  /// Enable/Disable Audio device (B0,D3,F0).
  ///
  if ((DxePlatformSaPolicy->MiscConfig->AudioEnable) && (McD2PciCfg16 (R_SA_IGD_VID) != 0xFFFF)) {
    McD0PciCfg32Or (R_SA_DEVEN, B_SA_DEVEN_D3EN_MASK);
  } else {
    McD0PciCfg32And (R_SA_DEVEN, ~(B_SA_DEVEN_D3EN_MASK));
  }

  return ;
}

VOID
ProgramSvidSid (
  IN DXE_PLATFORM_SA_POLICY_PROTOCOL *DxePlatformSaPolicy
  )
/**
    Program SA devices Subsystem Vendor Identifier (SVID) and Subsystem Identifier (SID).

    @param[in] DxePlatformSaPolicy   The SAPlatform Policy protocol instance
**/
{
  UINT8                         Index;
  UINT8                         BusNumber;
  UINTN                         PciEAddressBase;
  UINT8                         DeviceNumber;
  UINT8                         FunctionNumber;
  UINT8                         SvidRegOffset;
  UINT16                        Data16;
  STATIC SA_SVID_SID_INIT_ENTRY SvidSidInitTable[] = {
    {
      0,
      0,
      PCI_SVID_OFFSET
    },
    {
      1,
      0,
      R_SA_PEG_SS_OFFSET
    },
    {
      1,
      1,
      R_SA_PEG_SS_OFFSET
    },
    {
      1,
      2,
      R_SA_PEG_SS_OFFSET
    },
    {
      2,
      0,
      PCI_SVID_OFFSET
    },
    {
      3,
      0,
      PCI_SVID_OFFSET
    },
    {
      4,
      0,
      PCI_SVID_OFFSET
    },
    {
      7,
      0,
      PCI_SVID_OFFSET
    }
  };

  if ((DxePlatformSaPolicy->MiscConfig->DefaultSvidSid->SubSystemVendorId != 0) ||
      (DxePlatformSaPolicy->MiscConfig->DefaultSvidSid->SubSystemId != 0)
      ) {
    for (Index = 0; Index < (sizeof (SvidSidInitTable) / sizeof (SA_SVID_SID_INIT_ENTRY)); Index++) {
      BusNumber       = 0;
      DeviceNumber    = SvidSidInitTable[Index].DeviceNumber;
      FunctionNumber  = SvidSidInitTable[Index].FunctionNumber;
      SvidRegOffset   = SvidSidInitTable[Index].SvidRegOffset;
      PciEAddressBase = (UINT32) MmPciAddress (0, BusNumber, DeviceNumber, FunctionNumber, 0);
      ///
      /// Skip if the device is disabled
      ///
      if (MmioRead16 (PciEAddressBase + PCI_VID) == 0xFFFF) {
        continue;
      }
      ///
      /// Program SA devices Subsystem Vendor Identifier (SVID)
      ///
      Data16 = DxePlatformSaPolicy->MiscConfig->DefaultSvidSid->SubSystemVendorId;
      MmioWrite16 (
        (UINTN) (PciEAddressBase + SvidRegOffset),
        Data16
        );
      SCRIPT_MEM_WRITE (
        EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
        EfiBootScriptWidthUint16,
        (UINTN) (PciEAddressBase + SvidRegOffset),
        1,
        (VOID *) (UINTN) (PciEAddressBase + SvidRegOffset)
        );

      ///
      /// Program SA devices Subsystem Identifier (SID)
      ///
      Data16 = DxePlatformSaPolicy->MiscConfig->DefaultSvidSid->SubSystemId;
      MmioWrite16 (
        (UINTN) (PciEAddressBase + SvidRegOffset + 2),
        Data16
        );
      SCRIPT_MEM_WRITE (
        EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
        EfiBootScriptWidthUint16,
        (UINTN) (PciEAddressBase + SvidRegOffset + 2),
        1,
        (VOID *) (UINTN) (PciEAddressBase + SvidRegOffset + 2)
        );
    }
  }

  return;
}

VOID
SaFviExitPmAuth(
  VOID
  )
/**
  This function populates the SA FVI version numbers
**/
{
  const UINT8               StrEnabled[sizeof (SA_CRID_ENABLED)]    = SA_CRID_ENABLED;
  const UINT8               StrDisabled[sizeof (SA_CRID_DISABLED)]  = SA_CRID_DISABLED;
  const CodeVersion         PcieRcVersion = {
#include "SaPcieVersion.h"
  };
  const CodeVersion         MemRcVersion = {
#include "MrcVersion.h"
  };
  EFI_STATUS                Status;
  EFI_IA32_REGISTER_SET     RegSet;
  UINT16                    VbiosBuildNum;
  EFI_LEGACY_BIOS_PROTOCOL  *LegacyBios;

  if (mDxePlatformSaPolicy->MiscConfig->FviReport) {
    InitFviDataHubCbContext (
      mDxePlatformSaPolicy->MiscConfig->FviSmbiosType,
      (UINT8) mSaFviElements,
      &mSaFviVersionData
      );
    mSaFviElementsData[MEM_RC_VER].Element.Version.MajorVersion = (UINT8) MemRcVersion.Major;
    mSaFviElementsData[MEM_RC_VER].Element.Version.MinorVersion = (UINT8) MemRcVersion.Minor;
    mSaFviElementsData[MEM_RC_VER].Element.Version.Revision     = (UINT8) MemRcVersion.Rev;
    mSaFviElementsData[MEM_RC_VER].Element.Version.BuildNum     = (UINT16) MemRcVersion.Build;
    mSaFviElementsData[PCIE_VER].Element.Version.MajorVersion   = (UINT8) PcieRcVersion.Major;
    mSaFviElementsData[PCIE_VER].Element.Version.MinorVersion   = (UINT8) PcieRcVersion.Minor;
    mSaFviElementsData[PCIE_VER].Element.Version.Revision       = (UINT8) PcieRcVersion.Rev;
    mSaFviElementsData[PCIE_VER].Element.Version.BuildNum       = (UINT16) PcieRcVersion.Build;
    mSaFviElementsData[CRID_ORIGINAL].Element.Version.BuildNum  = (UINT16) McD0PciCfg8 (PCI_REVISION_ID_OFFSET);

    if (mDxePlatformSaPolicy->MiscConfig->CridEnable == TRUE) {
      CopyMem (mSaFviElementsData[CRID_STATUS].Element.VerString, StrEnabled, sizeof (StrEnabled));
    } else {
      CopyMem (mSaFviElementsData[CRID_STATUS].Element.VerString, StrDisabled, sizeof (StrDisabled));
    }

    mSaFviElementsData[CRID_NEW].Element.Version.BuildNum = (UINT16) McD0PciCfg8 (PCI_REVISION_ID_OFFSET);
    //
    // Check IGFX device
    //
    VbiosBuildNum = 0xFFFF;
    Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (void **) &LegacyBios);
    if (Status == EFI_SUCCESS) {
      RegSet.X.AX   = 0x5f01;
      Status        = LegacyBios->Int86 (LegacyBios, 0x15, &RegSet);
      VbiosBuildNum = (((RegSet.X.DX & 0x0F00) << 4) | ((RegSet.X.DX & 0x000F) << 8) | ((RegSet.X.BX & 0x0F00) >> 4) | (RegSet.X.BX & 0x000F));
    }
    mSaFviElementsData[VBIOS_VER].Element.Version.BuildNum = VbiosBuildNum;

    CreateRcFviDatahub (&mSaFviVersionData);
  }
}

VOID
EFIAPI
SaPciEnumCompleteCallback (
  IN EFI_EVENT    Event,
  IN VOID         *Context
  )
/**
  This function gets registered as a callback to perform SA initialization before ExitPmAuth

  @param[in] Event     - A pointer to the Event that triggered the callback.
  @param[in] Context   - A pointer to private data registered with the callback function.
**/
{
  EFI_STATUS          Status;
  VOID                *ProtocolPointer;

  ///
  /// Check if this is first time called by EfiCreateProtocolNotifyEvent() or not,
  /// if it is, we will skip it until real event is triggered
  ///
  Status = gBS->LocateProtocol (&gEfiPciEnumerationCompleteProtocolGuid, NULL, &ProtocolPointer);
  if (EFI_SUCCESS != Status) {
     return;
  }

  gBS->CloseEvent (Event);

  Status = PegInitBeforeExitPmAuth ();
  if (EFI_SUCCESS != Status) {
    DEBUG ((EFI_D_ERROR, "[SA] Pcie initialization before ExitPmAuth Error, Status = %x \n", Status));
    ASSERT_EFI_ERROR (Status);
  }
}

VOID
EFIAPI
SaExitPmAuthCallback (
  IN EFI_EVENT    Event,
  IN VOID         *Context
  )
/**
  This function gets registered as a callback to perform SA configuration security lock

  @param[in] Event     - A pointer to the Event that triggered the callback.
  @param[in] Context   - A pointer to private data registered with the callback function.
**/
{
  EFI_STATUS          Status;
  VOID                *ProtocolPointer;

  ///
  /// Check if this is first time called by EfiCreateProtocolNotifyEvent() or not,
  /// if it is, we will skip it until real event is triggered
  ///
  Status = gBS->LocateProtocol (&gExitPmAuthProtocolGuid, NULL, &ProtocolPointer);
  if (EFI_SUCCESS != Status) {
     return;
  }

  gBS->CloseEvent (Event);

  if (McD2PciCfg16 (R_SA_IGD_VID) != 0xFFFF) {
    Status = PostPmInitExitPmAuth();
    if (EFI_SUCCESS != Status) {
      DEBUG ((EFI_D_ERROR, "[SA] ExitPmAuth GraphicsInit Error, Status = %x \n", Status));
      ASSERT_EFI_ERROR (Status);
    }
  }

  UpdateDmarExitPmAuth();

  if (McD2PciCfg16 (R_SA_IGD_VID) != 0xFFFF) {
    Status = GetVBiosVbtExitPmAuth();
    if (EFI_SUCCESS != Status) {
      DEBUG ((EFI_D_ERROR, "[SA] ExitPmAuth Op Region Error, Status = %x \n", Status));
    }

    Status = UpdateIgdOpRegionExitPmAuth();
    if (EFI_SUCCESS != Status) {
      DEBUG ((EFI_D_ERROR, "[SA] ExitPmAuth Update Op Region Error, Status = %x \n", Status));
    }
  }

  SaFviExitPmAuth();

  Status = SaSecurityInit ();
  if (EFI_SUCCESS != Status) {
    DEBUG ((EFI_D_ERROR, "[SA] Security lock Error, Status = %x \n", Status));
    ASSERT_EFI_ERROR (Status);
  }
  return;
}

VOID
SaDxePolicyDump (
  IN DXE_PLATFORM_SA_POLICY_PROTOCOL *mDxePlatformSaPolicy
  )
/**
  This function prints the DXE phase platform policy.

  @param[in] mDxePlatformSaPolicy - SA DxePlatformPolicy protocol
**/
{
#ifdef EFI_DEBUG
  INTN  i;

  DEBUG ((EFI_D_INFO, "\n------------------------ SA Platform Policy (DXE) dump BEGIN -----------------\n"));
  DEBUG ((EFI_D_INFO, "Revision : %x\n", mDxePlatformSaPolicy->Revision));
  DEBUG ((EFI_D_INFO, "------------------------ SA_VTD_CONFIGURATION -----------------\n"));
  DEBUG ((EFI_D_INFO, " VtdEnable : %x\n", mDxePlatformSaPolicy->Vtd->VtdEnable));
  DEBUG ((EFI_D_INFO, " RmrrUsbBaseAddress : %x\n", mDxePlatformSaPolicy->Vtd->RmrrUsbBaseAddress));

  DEBUG ((EFI_D_INFO, " VtdBaseAddress[%d] :", SA_VTD_ENGINE_NUMBER));
  for (i = 0; i < SA_VTD_ENGINE_NUMBER; i++) {
    DEBUG ((EFI_D_INFO, " %x", mDxePlatformSaPolicy->Vtd->BaseAddress[i]));
  }
  DEBUG ((EFI_D_INFO, "\n"));

  DEBUG ((EFI_D_INFO, "------------------------ SA_MEMORY_CONFIGURATION -----------------\n"));
  DEBUG ((EFI_D_INFO, " SpdAddressTable[%d] :", SA_MC_MAX_SOCKETS));
  for (i = 0; i < SA_MC_MAX_SOCKETS; i++) {
    DEBUG ((EFI_D_INFO, " %x", mDxePlatformSaPolicy->MemoryConfig->SpdAddressTable[i]));
  }
  DEBUG ((EFI_D_INFO, "\n"));

  DEBUG ((EFI_D_INFO, " ChannelASlotMap : %x\n", mDxePlatformSaPolicy->MemoryConfig->ChannelASlotMap));
  DEBUG ((EFI_D_INFO, " ChannelBSlotMap : %x\n", mDxePlatformSaPolicy->MemoryConfig->ChannelBSlotMap));
  DEBUG ((EFI_D_INFO, " RmtBdatEnable : %x\n", mDxePlatformSaPolicy->MemoryConfig->RmtBdatEnable));
  DEBUG ((EFI_D_INFO, " MrcTimeMeasure : %x\n", mDxePlatformSaPolicy->MemoryConfig->MrcTimeMeasure));
  DEBUG ((EFI_D_INFO, " MrcFastBoot : %x\n", mDxePlatformSaPolicy->MemoryConfig->MrcFastBoot));

  DEBUG ((EFI_D_INFO, "------------------------ SA_PCIE_CONFIGURATION -----------------\n"));
  DEBUG ((EFI_D_INFO, " DmiAspm : %x\n", mDxePlatformSaPolicy->PcieConfig->DmiAspm));

  DEBUG ((EFI_D_INFO, " PegAspm[%d] :", SA_PEG_MAX_FUN));
  for (i = 0; i < SA_PEG_MAX_FUN; i++) {
    DEBUG ((EFI_D_INFO, " %x", mDxePlatformSaPolicy->PcieConfig->PegAspm[i]));
  }
  DEBUG ((EFI_D_INFO, "\n"));

  DEBUG ((EFI_D_INFO, " PegAspmL0s[%d] :", SA_PEG_MAX_FUN));
  for (i = 0; i < SA_PEG_MAX_FUN; i++) {
    DEBUG ((EFI_D_INFO, " %x", mDxePlatformSaPolicy->PcieConfig->PegAspmL0s[i]));
  }
  DEBUG ((EFI_D_INFO, "\n"));

  DEBUG ((EFI_D_INFO, " PegDeEmphasis[%d] :", SA_PEG_MAX_FUN));
  for (i = 0; i < SA_PEG_MAX_FUN; i++) {
    DEBUG ((EFI_D_INFO, " %x", mDxePlatformSaPolicy->PcieConfig->PegDeEmphasis[i]));
  }
  DEBUG ((EFI_D_INFO, "\n"));

  DEBUG ((EFI_D_INFO, " DmiExtSync : %x\n", mDxePlatformSaPolicy->PcieConfig->DmiExtSync));
  DEBUG ((EFI_D_INFO, " DmiDeEmphasis : %x\n", mDxePlatformSaPolicy->PcieConfig->DmiDeEmphasis));
  DEBUG ((EFI_D_INFO, " DmiIot : %x\n", mDxePlatformSaPolicy->PcieConfig->DmiIot));

  if (mDxePlatformSaPolicy->Revision >= DXE_SA_PLATFORM_POLICY_PROTOCOL_REVISION_2) {
    if (mDxePlatformSaPolicy->PcieConfig->PcieAspmDevsOverride != NULL) {
      DEBUG ((EFI_D_INFO, "------------------------ PCIE_ASPM_DEV_INFO -----------------\n"));
      DEBUG ((EFI_D_INFO, " VendorId DeviceId RevId RootApmcMask EndpointApmcMask\n"));
      i = 0;
      while ((mDxePlatformSaPolicy->PcieConfig->PcieAspmDevsOverride[i].VendorId != SA_PCIE_DEV_END_OF_TABLE) &&
             (i < MAX_PCIE_ASPM_OVERRIDE)) {
        DEBUG ((EFI_D_INFO, " %04x     %04x     %02x    %01x            %01x\n",
          mDxePlatformSaPolicy->PcieConfig->PcieAspmDevsOverride[i].VendorId,
          mDxePlatformSaPolicy->PcieConfig->PcieAspmDevsOverride[i].DeviceId,
          mDxePlatformSaPolicy->PcieConfig->PcieAspmDevsOverride[i].RevId,
          mDxePlatformSaPolicy->PcieConfig->PcieAspmDevsOverride[i].RootApmcMask,
          mDxePlatformSaPolicy->PcieConfig->PcieAspmDevsOverride[i].EndpointApmcMask));
        i++;
      }
      DEBUG ((EFI_D_INFO, "------------------------ END_OF_TABLE -----------------------\n"));
    }
    if (mDxePlatformSaPolicy->PcieConfig->PcieLtrDevsOverride != NULL) {
      DEBUG ((EFI_D_INFO, "------------------------ PCIE_LTR_DEV_INFO -----------------\n"));
      DEBUG ((EFI_D_INFO, " VendorId DeviceId RevId SnoopLatency NonSnoopLatency\n"));
      i = 0;
      while ((mDxePlatformSaPolicy->PcieConfig->PcieLtrDevsOverride[i].VendorId != SA_PCIE_DEV_END_OF_TABLE) &&
             (i < MAX_PCIE_LTR_OVERRIDE)) {
        DEBUG ((EFI_D_INFO, " %04x     %04x     %02x    %01x            %01x\n",
          mDxePlatformSaPolicy->PcieConfig->PcieLtrDevsOverride[i].VendorId,
          mDxePlatformSaPolicy->PcieConfig->PcieLtrDevsOverride[i].DeviceId,
          mDxePlatformSaPolicy->PcieConfig->PcieLtrDevsOverride[i].RevId,
          mDxePlatformSaPolicy->PcieConfig->PcieLtrDevsOverride[i].SnoopLatency,
          mDxePlatformSaPolicy->PcieConfig->PcieLtrDevsOverride[i].NonSnoopLatency));
        i++;
      }
      DEBUG ((EFI_D_INFO, "------------------------ END_OF_TABLE ----------------------\n"));
    }
  }

  for (i = 0; i < SA_PEG_MAX_FUN; i++) {
    DEBUG ((EFI_D_INFO, " PegPwrOpt[%d].LtrEnable            : %x\n", i, mDxePlatformSaPolicy->PcieConfig->PegPwrOpt[i].LtrEnable));
    DEBUG ((EFI_D_INFO, " PegPwrOpt[%d].LtrMaxSnoopLatency   : %x\n", i, mDxePlatformSaPolicy->PcieConfig->PegPwrOpt[i].LtrMaxSnoopLatency));
    DEBUG ((EFI_D_INFO, " PegPwrOpt[%d].ObffEnable           : %x\n", i, mDxePlatformSaPolicy->PcieConfig->PegPwrOpt[i].ObffEnable));
    DEBUG ((EFI_D_INFO, " PegPwrOpt[%d].LtrMaxNoSnoopLatency : %x\n", i, mDxePlatformSaPolicy->PcieConfig->PegPwrOpt[i].LtrMaxNoSnoopLatency));
  }

  if (mDxePlatformSaPolicy->Revision >= DXE_SA_PLATFORM_POLICY_PROTOCOL_REVISION_3) {
    DEBUG ((EFI_D_INFO, "------------------------ SA_SG_VBIOS_CONFIGURATION -----------------\n"));
    DEBUG ((EFI_D_INFO, " LoadVbios : %x\n", mDxePlatformSaPolicy->VbiosConfig->LoadVbios));
    DEBUG ((EFI_D_INFO, " ExecuteVbios : %x\n", mDxePlatformSaPolicy->VbiosConfig->ExecuteVbios));
    DEBUG ((EFI_D_INFO, " VbiosSource : %x\n", mDxePlatformSaPolicy->VbiosConfig->VbiosSource));
  }


  DEBUG ((EFI_D_INFO, "------------------------ SA_IGD_CONFIGURATION -----------------\n"));
  DEBUG ((EFI_D_INFO, " RenderStandby : %x\n", mDxePlatformSaPolicy->IgdConfig->RenderStandby));
  DEBUG ((EFI_D_INFO, "------------------------ SA_MISC_CONFIGURATION -----------------\n"));
  DEBUG ((EFI_D_INFO, " ChapDeviceEnable : %x\n", mDxePlatformSaPolicy->MiscConfig->ChapDeviceEnable));
  DEBUG ((EFI_D_INFO, " Device4Enable : %x\n", mDxePlatformSaPolicy->MiscConfig->Device4Enable));
  DEBUG ((EFI_D_INFO, " SubSystemVendorId : %x\n", mDxePlatformSaPolicy->MiscConfig->DefaultSvidSid->SubSystemVendorId));
  DEBUG ((EFI_D_INFO, " SubSystemId : %x\n", mDxePlatformSaPolicy->MiscConfig->DefaultSvidSid->SubSystemId));
  DEBUG ((EFI_D_INFO, " CridEnable : %x\n", mDxePlatformSaPolicy->MiscConfig->CridEnable));
  DEBUG ((EFI_D_INFO, " AudioEnable : %x\n", mDxePlatformSaPolicy->MiscConfig->AudioEnable));
  DEBUG ((EFI_D_INFO, " FviReport : %x\n", mDxePlatformSaPolicy->MiscConfig->FviReport));
  DEBUG ((EFI_D_INFO, " FviSmbiosType : %x\n", mDxePlatformSaPolicy->MiscConfig->FviSmbiosType));
  DEBUG ((EFI_D_INFO, "\n------------------------ SA Platform Policy (DXE) dump END -----------------\n"));
#endif
  return;
}