summaryrefslogtreecommitdiff
path: root/Platform/BroxtonPlatformPkg/Common/PlatformSettings/PlatformPreMemPei/PlatformInitPreMem.c
blob: cab38476499381672bb6309d32feffce0eedda52 (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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
/** @file
  Source code file for Platform Init Pre-Memory PEI module.

  Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Base.h>
#include <ScAccess.h>
#include <SaAccess.h>
#include <SeCAccess.h>
#include <PlatformBaseAddresses.h>
#include <FrameworkPei.h>
#include <Guid/VariableFormat.h>
#include <Ppi/MfgMemoryTest.h>
#include <Ppi/TemporaryRamSupport.h>
#include <Ppi/BlockIo.h>
#include <Ppi/ReadOnlyVariable2.h>
#include <Ppi/SecUma.h>
#include <Ppi/FvLoadFile.h>
#include <Ppi/Stall.h>
#include <Ppi/MemoryDiscovered.h>
#include <Ppi/FirmwareVolumeInfo.h>
#include <Ppi/SiPolicyPpi.h>
#include <Ppi/BiosReservedMemory.h>
#include <Ppi/DramPolicyPpi.h>
#include <Ppi/BoardInitSignalling.h>
#include <Guid/Capsule.h>
#include <Guid/FirmwareFileSystem2.h>
#include <Guid/SystemNvDataGuid.h>
#include <Guid/PlatformInfo.h>
#include <Guid/SetupVariable.h>
#include <Guid/AcpiVariableCompatibility.h>
#include <Guid/FirmwarePerformance.h>
#include <Guid/VariableFormat.h>
#include <Library/DebugLib.h>
#include <Library/PeimEntryPoint.h>
#include <Library/PeiVariableCacheLib.h>
#include <Library/PeiPlatformConfigUpdateLib.h>
#include <Library/PeiServicesLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/HobLib.h>
#include <Library/IoLib.h>
#include <Library/PcdLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PerformanceLib.h>
#include <Library/MtrrLib.h>
#include <Library/I2cLib.h>
#include <Library/PmicLib.h>
#include <Library/PeiVariableCacheLib.h>
#include <Library/PeiSiPolicyLib.h>
#include <Library/PeiPlatformConfigUpdateLib.h>
#include <Library/CpuPolicyLib.h>
#include <Library/ScPlatformLib.h>
#include <Library/PeiPolicyInitLib.h>
#include <Library/PeiScPolicyLib.h>
#include <Library/PeiSiPolicyUpdateLib.h>
#include "Smip.h"
#include "Stall.h"
#include "FvCallback.h"
#include "MemoryCallback.h"
#include "BoardGpiosPreMem.h"
#include "PlatformInitPreMem.h"
#include <Library/SteppingLib.h>
#include <Library/HeciMsgLib.h>
#include <Ppi/SecPlatformInformation.h>
#include <Library/PlatformSecLib.h>
#include <Library/TimerLib.h>

#if (ENBDT_PF_ENABLE == 1)
//
//SSC
//
  #include <Library/PmcIpcLib.h>
  #include <SscRegs.h>
  #include <Library/SideBandLib.h>
#endif

extern EFI_GUID gEfiBootMediaHobGuid;

//
// The global indicator, the FvFileLoader callback will modify it to TRUE after loading PEIM into memory
//
BOOLEAN ImageInMemory = FALSE;
CHAR8   mGdtTable[0x40];

//
//Memory Test Manufacturing mode
//
#define DATA_PATTERN_ARRAY_SIZE (sizeof(DataPatternForMemoryTest) / sizeof(UINT32))
UINT32 DataPatternForMemoryTest[] = {
  0x55555555, 0xAAAAAAAA, 0x55555510, 0x555555EF, 0x55555510, 0x555555EF, 0x55555510, 0x555555EF,
  0x55555555, 0xAAAAAAAA, 0x55551055, 0x5555EF55, 0x55551055, 0x5555EF55, 0x55551055, 0x5555EF55,
  0x55555555, 0xAAAAAAAA, 0x55105555, 0x55EF5555, 0x55105555, 0x55EF5555, 0x55105555, 0x55EF5555,
  0x55555555, 0xAAAAAAAA, 0x10555555, 0xEF555555, 0x10555555, 0xEF555555, 0x10555555, 0xEF555555
};

extern EFI_PEI_PPI_DESCRIPTOR mCseUfsSelectPpiList[];
extern EFI_PEI_PPI_DESCRIPTOR mCseEmmcSelectPpiList[];
extern EFI_PEI_PPI_DESCRIPTOR mCseSpiSelectPpiList[];

#define PEI_STALL_RESOLUTION   1
static EFI_PEI_STALL_PPI  mStallPpi = {
  PEI_STALL_RESOLUTION,
  Stall
};


#if defined(PRAM_SUPPORT)
static PEI_BIOS_RESERVED_MEMORY_POLICY_PPI mPeiBiosReservedMemoryPolicyPpi = {
  GetBiosReservedMemoryPolicy
};

static EFI_PEI_PPI_DESCRIPTOR mBiosReservedMemoryPolicyPpi =
  {
    EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
    &gBiosReservedMemoryPolicyPpiGuid,
    &mPeiBiosReservedMemoryPolicyPpi
  };
#endif


static EFI_PEI_PPI_DESCRIPTOR mInstallStallPpi =
  {
    EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
    &gEfiPeiStallPpiGuid,
    &mStallPpi
  };


static PEI_MFG_MEMORY_TEST_PPI mPeiMfgMemoryTestPpi = {
  MfgMemoryTest
};


static EFI_PEI_PPI_DESCRIPTOR mMfgMemTestPpi =
  {
    EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
    &gPeiMfgMemoryTestPpiGuid,
    &mPeiMfgMemoryTestPpi
  };

static EFI_PEI_PPI_DESCRIPTOR mPeiTemporaryRamSupportPpiPpi[] = {
  {
    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gEfiTemporaryRamSupportPpiGuid,
    NULL
  }
};


//
// Notify Callbacks for SPI and non-SPI boot devices.
// These are installed by MemoryDiscovered Callback, since they require main memory.
//
EFI_PEI_NOTIFY_DESCRIPTOR mFvNotifyList[] = {
  {
    EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
    &gEfiPeiVirtualBlockIoPpiGuid,  //non-SPI boot - installed after MemInit
    GetFvNotifyCallback
  },

  {
    (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gCseSpiSelectPpiGuid,   //SPI boot - installed by PeiSecUma
    GetFvNotifyCallback
  }
};


EFI_STATUS
EFIAPI
FspTempRamExitCallback (
  IN EFI_PEI_SERVICES           **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Ppi
  )
{
  EFI_STATUS    Status = EFI_SUCCESS;

  DEBUG ((DEBUG_INFO, "FspTempRamExitCallback\n"));
  Status = PeiServicesNotifyPpi (&mFvNotifyList[0]);
  return Status;
}


EFI_PEI_NOTIFY_DESCRIPTOR mFspTempRamExitList[] = {
  {
    (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gFspTempRamExitGuid,
    FspTempRamExitCallback
  }
};


EFI_STATUS
EFIAPI
GetBiosReservedMemoryPolicy (
  IN CONST EFI_PEI_SERVICES               **PeiServices,
  IN PEI_BIOS_RESERVED_MEMORY_POLICY_PPI  *This,
  IN OUT BIOS_RESERVED_MEMORY_CONFIG      *BiosReservedMemoryPolicy
  );


VOID
CopyMemSse4 (
  IN VOID* Dst,
  IN VOID* Src,
  IN UINTN SizeInBytes
  )
{
  _asm {
    //
    // Initialize pointers to start of the USWC memory
    //
    mov esi, Src
    mov edx, Src

    //
    // Initialize pointer to end of the USWC memory
    //
    add edx, SizeInBytes

    //
    // Initialize pointer to start of the cacheable WB buffer
    //
    mov edi, Dst

    //
    // save xmm0 ~ xmm3 to stack
    //
    sub     esp, 040h
    movdqu  [esp], xmm0
    movdqu  [esp + 16], xmm1
    movdqu  [esp + 32], xmm2
    movdqu  [esp + 48], xmm3

    //
    // Start of Bulk Load loop
    //
    inner_start:
    //
    // Load data from USWC Memory using Streaming Load
    //
    MOVNTDQA xmm0, xmmword ptr [esi]
    MOVNTDQA xmm1, xmmword ptr [esi + 16]
    MOVNTDQA xmm2, xmmword ptr [esi + 32]
    MOVNTDQA xmm3, xmmword ptr [esi + 48]

    //
    // Copy data to buffer
    //
    MOVDQA xmmword ptr [edi], xmm0
    MOVDQA xmmword ptr [edi + 16], xmm1
    MOVDQA xmmword ptr [edi + 32], xmm2
    MOVDQA xmmword ptr [edi + 48], xmm3

    //
    // Increment pointers by cache line size and test for end of loop
    //
    add esi, 040h
    add edi, 040h
    cmp esi, edx
    jne inner_start

    //
    // restore xmm0 ~ xmm3
    //
    mfence
    movdqu  xmm0, [esp]
    movdqu  xmm1, [esp + 16]
    movdqu  xmm2, [esp + 32]
    movdqu  xmm3, [esp + 48]
    add     esp, 040h // stack cleanup
  }
  // End of Bulk Load loop
}


#if defined(PRAM_SUPPORT)

/**
  This function is to get Bios Reserved Memory in PEI.

  @param[in]       PeiServices                 Pointer to PEI Services.
  @param[in]       This                        Pei memory test PPI pointer.
  @param[in, out]  BiosReservedMemoryPolicy    Pointer to BiosReservedMemorypolicy.

  @retval          EFI_SUCCESS                 The operation completed successfully.

**/
EFI_STATUS
EFIAPI
GetBiosReservedMemoryPolicy (
  IN CONST EFI_PEI_SERVICES               **PeiServices,
  IN PEI_BIOS_RESERVED_MEMORY_POLICY_PPI  *This,
  IN OUT BIOS_RESERVED_MEMORY_CONFIG      *BiosReservedMemoryPolicy
  )
{
  EFI_STATUS                         Status;
  UINTN                              VariableSize;
  EFI_PEI_READ_ONLY_VARIABLE2_PPI    *VariableServices;
  SYSTEM_CONFIGURATION               SystemConfiguration;

  Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariableServices);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
  VariableSize = sizeof (SYSTEM_CONFIGURATION);
  Status = VariableServices->GetVariable (
                               VariableServices,
                               PLATFORM_SETUP_VARIABLE_NAME,
                               &gEfiSetupVariableGuid,
                               NULL,
                               &VariableSize,
                               &SystemConfiguration
                               );


  if (Status == EFI_SUCCESS) {
#ifdef PRAM_SUPPORT
    BiosReservedMemoryPolicy->Pram = SystemConfiguration.Pram;
#endif
  } else {
#ifdef PRAM_SUPPORT
    BiosReservedMemoryPolicy->Pram = 0x30;
#endif

  }
  Status = EFI_SUCCESS;

#ifdef PRAM_SUPPORT
  DEBUG ((DEBUG_INFO, "SystemConfiguration.Pram = %x \n", SystemConfiguration.Pram));
#endif
  return Status;
}

#endif


/**
  This function checks the memory range in PEI.

  @param[in]  PeiServices         Pointer to PEI Services.
  @param[in]  This                Pei memory test PPI pointer.
  @param[in]  BeginAddress        Beginning of the memory address to be checked.
  @param[in]  MemoryLength        Bytes of memory range to be checked.

  @retval     EFI_SUCCESS         The operation completed successfully.
  @retval     EFI_DEVICE_ERROR    Memory test failed. It's not safe to use this range of memory.

**/
EFI_STATUS
EFIAPI
MfgMemoryTest (
  IN  CONST EFI_PEI_SERVICES             **PeiServices,
  IN  PEI_MFG_MEMORY_TEST_PPI            *This,
  IN  UINT32                             BeginAddress,
  IN  UINT32                             MemoryLength
  )
{
  UINT32  i;
  UINT32  memAddr;
  UINT32  readData;
  UINT32  xorData;
  UINT32  TestFlag = 0;

  memAddr = BeginAddress;

  //
  // Output Message for MFG
  //
  DEBUG ((DEBUG_INFO, "MFGMODE SET\n"));

  //
  //  Writing the pattern in defined location.
  //
  while (memAddr < (BeginAddress+MemoryLength)) {
    for (i = 0; i < DATA_PATTERN_ARRAY_SIZE; i++) {
      if (memAddr > (BeginAddress + MemoryLength - 4)) {
        memAddr = memAddr + 4;
        break;
      }

      *((volatile UINT32*) memAddr) = DataPatternForMemoryTest[i];
      memAddr = memAddr + 4;
    }
  }

  //
  // Verify the pattern
  //
  memAddr = BeginAddress;

  while (memAddr < (BeginAddress + MemoryLength)) {
    for (i = 0; i < DATA_PATTERN_ARRAY_SIZE; i++) {
      if (memAddr > (BeginAddress + MemoryLength - 4)) {
        memAddr = memAddr + 4;
        break;
      }

      readData = *((volatile UINT32*) memAddr);
      xorData = readData ^ DataPatternForMemoryTest[i];

      //
      // If xorData is non-zero, this particular memAddr has a failure.
      //
      if (xorData != 0x00000000) {
        DEBUG ((DEBUG_ERROR, "Expected value....: %x\n", DataPatternForMemoryTest[i]));
        DEBUG ((DEBUG_ERROR, "ReadData value....: %x\n", readData));
        DEBUG ((DEBUG_ERROR, "Pattern failure at....: %x\n", memAddr));
        TestFlag = 1;
      }

      memAddr = memAddr + 4;
    }
  }

  if (TestFlag) {
    return EFI_DEVICE_ERROR;
  }

  //
  //Output Message for MFG
  //
  DEBUG ((DEBUG_INFO, "MFGMODE MEMORY TEST PASSED\n"));

  return EFI_SUCCESS;
}


BOOLEAN
IsRtcUipAlwaysSet (
  IN CONST EFI_PEI_SERVICES       **PeiServices
  )
{
  EFI_PEI_STALL_PPI  *StallPpi;
  UINTN              Count;

  PeiServicesLocatePpi (&gEfiPeiStallPpiGuid, 0, NULL, (VOID **) &StallPpi);

  for (Count = 0; Count < 500; Count++) { // Maximum waiting approximates to 1.5 seconds (= 3 msec * 500)
    IoWrite8 (R_RTC_INDEX2, R_RTC_REGISTERA);
    if ((IoRead8 (R_RTC_TARGET2) & B_RTC_REGISTERA_UIP) == 0) {
      return FALSE;
    }

    StallPpi->Stall (PeiServices, StallPpi, 3000);
  }

  return TRUE;
}


VOID
RtcPowerFailureHandler (
  VOID
  )
{
  UINT16          Data16;
  UINT8           Data8;

  // When the RTC_PWR_STS bit is set, it indicates that the RTCRST# signal went low.
  // Software should clear this bit. For example, changing the RTC battery sets this bit.
  // System BIOS should reset CMOS to default values if the RTC_PWR_STS bit is set.
  // The System BIOS should execute the sequence below if the RTC_PWR_STS bit is set
  // before memory initialization. This will ensure that the RTC state machine has been
  // initialized.
  // 1. If the RTC_PWR_STS bit is set which indicates a new coin-cell battery insertion or a
  //    battery failure, steps 2 through 5 should be executed.
  // 2. Set RTC Register 0x0A[6:4] to 110b or 111b.
  // 3. Set RTC Register 0x0B[7].
  // 4. Set RTC Register 0x0A[6:4] to 010b.
  // 5. Clear RTC Register 0x0B[7].
  //

  Data16 = MmioRead16 (PMC_BASE_ADDRESS + R_PMC_GEN_PMCON_1);

  if ((Data16 & B_PMC_GEN_PMCON_RTC_PWR_STS) != 0) {
    //
    // 2. Set RTC Register 0Ah[6:4] to 110b or 111b
    //
    IoWrite8 (R_RTC_INDEX2, (UINT8) R_RTC_REGISTERA);
    Data8 = IoRead8 (R_RTC_TARGET2) & (UINT8) ~(B_RTC_REGISTERA_DV);
    Data8 |= (UINT8) (V_RTC_REGISTERA_DV_DIV_RST1);
    IoWrite8 (R_RTC_TARGET2, Data8);

    //
    // 3. Set RTC Register 0Bh[7].
    //
    IoWrite8 (R_RTC_INDEX2, (UINT8) R_RTC_REGISTERB);
    IoOr8 (R_RTC_TARGET2, (UINT8) B_RTC_REGISTERB_SET);

    //
    // 4. Set RTC Register 0Ah[6:4] to 010b
    //
    IoWrite8 (R_RTC_INDEX2, (UINT8) R_RTC_REGISTERA);
    Data8 = IoRead8 (R_RTC_TARGET2) & (UINT8) ~(B_RTC_REGISTERA_DV);
    Data8 |= (UINT8) (V_RTC_REGISTERA_DV_NORM_OP);
    IoWrite8 (R_RTC_TARGET2, Data8);

    //
    // 5. Clear RTC Register 0Bh[7].
    //
    IoWrite8 (R_RTC_INDEX2, (UINT8) R_RTC_REGISTERB);
    IoAnd8 (R_RTC_TARGET2, (UINT8) ~B_RTC_REGISTERB_SET);
  }

  return;
}


VOID
ScBaseInit (
  VOID
  )
{
  //
  // Set BARs for PMC SSRAM (0/13/3)
  // Allocation for these regions is done in PlatformInitFinalConfig() via call to BuildResourceDescriptorHob()
  //
  MmioWrite32 (
    MmPciAddress (0, 0, PCI_DEVICE_NUMBER_PMC, PCI_FUNCTION_NUMBER_PMC_SSRAM, 0x10),  //Write BAR0-lower
    PcdGet32 (PcdPmcSsramBaseAddress0)
    );
  MmioWrite32 (
    MmPciAddress (0, 0, PCI_DEVICE_NUMBER_PMC, PCI_FUNCTION_NUMBER_PMC_SSRAM, 0x18),  //Write BAR1-lower
    PcdGet32 (PcdPmcSsramBaseAddress1)
    );
  MmioWrite16 (
    MmPciAddress (0, 0, PCI_DEVICE_NUMBER_PMC, PCI_FUNCTION_NUMBER_PMC_SSRAM, 0x4),   //Set BME and MSE
    0x6
    );

  //
  // Set SPI Base Address
  //
  MmioWrite32 (
    MmPciAddress (0,DEFAULT_PCI_BUS_NUMBER_SC, PCI_DEVICE_NUMBER_SPI, PCI_FUNCTION_NUMBER_SPI, R_SPI_BASE),
    (UINT32) ((SPI_BASE_ADDRESS & B_SPI_BASE_BAR))
    );

  //
  // Enable SPI Memory decode
  //
  MmioWrite16 (
    MmPciAddress (0, DEFAULT_PCI_BUS_NUMBER_SC, PCI_DEVICE_NUMBER_SPI, PCI_FUNCTION_NUMBER_SPI, R_SPI_COMMAND),
    EFI_PCI_COMMAND_MEMORY_SPACE
    );

  //
  // Set P2SB Base Address
  //
  MmioWrite32 (
    MmPciAddress (0, DEFAULT_PCI_BUS_NUMBER_SC, PCI_DEVICE_NUMBER_P2SB, PCI_FUNCTION_NUMBER_P2SB, R_P2SB_BASE),
    (UINT32) ((PcdGet32 (PcdP2SBBaseAddress)))
    );

  //
  // Enable P2SB Memory decode
  //
  MmioWrite16 (
    MmPciAddress (0, DEFAULT_PCI_BUS_NUMBER_SC, PCI_DEVICE_NUMBER_P2SB, PCI_FUNCTION_NUMBER_P2SB, R_P2SB_STSCMD),
    B_P2SB_STSCMD_BME | B_P2SB_STSCMD_MSE
    );

  PchLpcIoDecodeRangesSet (
    (V_PCH_LPC_IOD_LPT_378  << N_PCH_LPC_IOD_LPT)  |
    (V_PCH_LPC_IOD_COMB_3E8 << N_PCH_LPC_IOD_COMB) |
    (V_PCH_LPC_IOD_COMA_3F8 << N_PCH_LPC_IOD_COMA)
    );

  PchLpcIoEnableDecodingSet (
    B_PCH_LPC_IOE_ME2  |
    B_PCH_LPC_IOE_SE   |
    B_PCH_LPC_IOE_ME1  |
    B_PCH_LPC_IOE_KE   |
    B_PCH_LPC_IOE_HGE  |
    B_PCH_LPC_IOE_LGE  |
    B_PCH_LPC_IOE_FDE  |
    B_PCH_LPC_IOE_PPE  |
    B_PCH_LPC_IOE_CBE  |
    B_PCH_LPC_IOE_CAE
    );

}


/**
  This function performs Silicon Policy initialization.

  @param[in]  FirmwareConfiguration  It uses to skip specific policy init that depends
                                     on the 'FirmwareConfiguration' variable.

  @retval     EFI_SUCCESS            The PPI is installed and initialized.
  @retval     EFI ERRORS             The PPI is not successfully installed.
  @retval     EFI_OUT_OF_RESOURCES   Do not have enough resources to initialize the driver

**/
EFI_STATUS
EFIAPI
PeiSiPolicyInit (
  VOID
  )
{
  EFI_STATUS             Status;
  SI_POLICY_PPI          *SiPolicyPpi;

  //
  // Call SiCreatePolicyDefaults to initialize Silicon Policy structure
  // and get all Intel default policy settings.
  //
  Status = SiCreatePolicyDefaults (&SiPolicyPpi);
  ASSERT_EFI_ERROR (Status);

  //
  // Update and override all platform related and customized settings below.
  //
  UpdatePeiSiPolicy (SiPolicyPpi);

  //
  // Install SiPolicyPpi.
  // While installed, RC assumes the Policy is ready and finalized. So please
  // update and override any setting before calling this function.
  //
  Status = SiInstallPolicyPpi (SiPolicyPpi);
  ASSERT_EFI_ERROR (Status);

  return Status;
}


/**
  This function performs SC PreMem Policy initialization.

  @param[in]  StartTimerTicker       The Start Timer Ticker for PFET# enabled

  @retval     EFI_SUCCESS            The PPI is installed and initialized.
  @retval     EFI ERRORS             The PPI is not successfully installed.
  @retval     EFI_OUT_OF_RESOURCES   Do not have enough resources to initialize the driver

**/
EFI_STATUS
EFIAPI
PeiScPreMemPolicyInit (
  IN UINT64              *StartTimerTicker
  )
{
  EFI_STATUS             Status;
  SC_PREMEM_POLICY_PPI   *ScPreMemPolicy;
  SC_PCIE_PREMEM_CONFIG  *PciePreMemConfig;

  //
  // Call ScCreatePreMemConfigBlocks to initialize SC Policy structure
  // and get all Intel default policy settings.
  //
  Status = ScCreatePreMemConfigBlocks (&ScPreMemPolicy);
  ASSERT_EFI_ERROR (Status);

  //
  // Update and override all platform related and customized settings below.
  //
  Status = GetConfigBlock ((VOID *) ScPreMemPolicy, &gPcieRpPreMemConfigGuid, (VOID *) &PciePreMemConfig);
  ASSERT_EFI_ERROR (Status);

  //
  // Update PCIe PERST# and CLK# policies
  //
  PciePreMemConfig->StartTimerTickerOfPfetAssert = (UINTN) *StartTimerTicker;
  PciePreMemConfig->RootPort[0].Perst = N_GPIO_13;  // Slot2
  PciePreMemConfig->RootPort[1].Perst = N_GPIO_15;  // NGFF
  PciePreMemConfig->RootPort[2].Perst = W_GPIO_152; // Slot1
  PciePreMemConfig->RootPort[3].Perst = 0;
  PciePreMemConfig->RootPort[4].Perst = N_GPIO_37;  // LOM
  PciePreMemConfig->RootPort[5].Perst = 0;
  PciePreMemConfig->RootPort[0].Clock = W_GPIO_211;  // Slot2
  PciePreMemConfig->RootPort[1].Clock = W_GPIO_212;  // NGFF
  PciePreMemConfig->RootPort[2].Clock = W_GPIO_209; // Slot1
  PciePreMemConfig->RootPort[3].Clock = 0;
  PciePreMemConfig->RootPort[4].Clock = 0;
  PciePreMemConfig->RootPort[5].Clock = 0;

  //
  // Install ScPreMemPolicyPpi.
  // While installed, RC assumes the Policy is ready and finalized. So please
  // update and override any setting before calling this function.
  //
  Status = ScInstallPreMemPolicyPpi (ScPreMemPolicy);
  ASSERT_EFI_ERROR (Status);

  return Status;
}



#if (ENBDT_PF_ENABLE == 1)
//
// DDR SSC
//
EFI_STATUS
EFIAPI
PeiDDRSSCInit (
  VOID
  )
{
  EFI_STATUS                       Status;
  EFI_PEI_READ_ONLY_VARIABLE2_PPI  *VariableServices;
  SSC_IPC_BUFFER                   WBuf;
  UINT32                           BufferSize = 0;
  SYSTEM_CONFIGURATION             SystemConfiguration;
  UINTN                            VariableSize;

  //
  // static table for the SSC settings (corresponding with the SSC settings 0~-0.5%, 0.1% stepping)
  // Modulation Freq = 32KHz
  //
  SSC_SETTING                     SSC_Select_Table[] = {{No_SSC, 0x12B, 0},
                                                        {M01_SSC, 0x12B, 0x1062},
                                                        {M02_SSC, 0x12B, 0x2BB0},
                                                        {M03_SSC, 0x12B, 0x46FF},
                                                        {M04_SSC, 0x12B, 0x624D},
                                                        {M05_SSC, 0x12B, 0x7D9C}};

  //
  //static table for the clock bending settings (corresponding with the clock bending settings 1.3%, 0.6%, 0, -0.9%)
  //
  CLOCK_BENDING_SETTING           CLK_Bending_Table[] = {{Clk_Bending_13, 0xA00000, 0x7E},
                                                         {Clk_Bending_06, 0xC00000, 0x7D},
                                                         {No_Clk_Bending, 0x0, 0x7D},
                                                         {Clk_Bending_M09, 0xDB6C20, 0x7B}};

  //
  // default value of the 4 SSC setting registers
  //
  WBuf.LJ1PLL_CTRL_1.Data = 0x00;
  WBuf.LJ1PLL_CTRL_2.Data = 0x0888812B;
  WBuf.LJ1PLL_CTRL_3 = 0x7D000000;
  WBuf.LJ1PLL_CTRL_5.Data = 0x7D000000;
  BufferSize = sizeof (UINT32) * 4;

  Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariableServices);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  VariableSize = sizeof (SYSTEM_CONFIGURATION);

  Status = VariableServices->GetVariable (
                               VariableServices,
                               PLATFORM_SETUP_VARIABLE_NAME,
                               &gEfiSetupVariableGuid,
                               NULL,
                               &VariableSize,
                               &SystemConfiguration
                               );

  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Setup Variable is not ready for SSC setting! Used default value!!\n\r"));
    //
    // Set default value of SSC
    //
    WBuf.LJ1PLL_CTRL_2.Fields.ssc_cyc_to_peak_m1 = SSC_Select_Table[SSC_DEFAULT_SETTING].Ssc_Cyc_To_Peak;
    WBuf.LJ1PLL_CTRL_2.Fields.ssc_frac_step = SSC_Select_Table[SSC_DEFAULT_SETTING].Ffs_Frac_Step;
    //
    // Set default value of Clock bending
    //
    WBuf.LJ1PLL_CTRL_5.Fields.pll_ratio_frac = CLK_Bending_Table[CLK_BENDING_DEFAULT_SETTING].Pll_Ratio_Frac;
    WBuf.LJ1PLL_CTRL_5.Fields.pll_ratio_int = CLK_Bending_Table[CLK_BENDING_DEFAULT_SETTING].Pll_Ratio_Int;

    //
    // send the IPC command for SSC
    //
    Status = IpcSendCommandEx (IPC_CMD_ID_EMI_RFI_SUPPORT, IPC_SUBCMD_ID_SSC_APPLY_NOW, &WBuf, BufferSize);

    //
    // Delay for 1ms to avoid the SSC doesn't set correctly sometimes
    //
    MicroSecondDelay (1000);

    //
    // set the ssc_en to Disable!
    //
    WBuf.LJ1PLL_CTRL_1.Fields.ssc_en = SSC_DISABLE;
    WBuf.LJ1PLL_CTRL_1.Fields.ssc_en_ovr = SSC_DISABLE;
    Status = IpcSendCommandEx (IPC_CMD_ID_EMI_RFI_SUPPORT, IPC_SUBCMD_ID_SSC_APPLY_NOW, &WBuf, BufferSize);
    return Status;
  }

  if (SystemConfiguration.DDRSSCEnable) {
    //
    // get the correct register values of the SSC setting
    //
    WBuf.LJ1PLL_CTRL_2.Fields.ssc_cyc_to_peak_m1 = SSC_Select_Table[SystemConfiguration.DDRSSCSelection].Ssc_Cyc_To_Peak;
    WBuf.LJ1PLL_CTRL_2.Fields.ssc_frac_step = SSC_Select_Table[SystemConfiguration.DDRSSCSelection].Ffs_Frac_Step;
    //
    // get the correct register values of the clock bending setting

    WBuf.LJ1PLL_CTRL_5.Fields.pll_ratio_frac = CLK_Bending_Table[SystemConfiguration.DDRCLKBending].Pll_Ratio_Frac;
    WBuf.LJ1PLL_CTRL_5.Fields.pll_ratio_int = CLK_Bending_Table[SystemConfiguration.DDRCLKBending].Pll_Ratio_Int;

    //
    // send the IPC command for SSC settings
    //
    Status = IpcSendCommandEx (IPC_CMD_ID_EMI_RFI_SUPPORT, IPC_SUBCMD_ID_SSC_APPLY_NOW, &WBuf, BufferSize);

    //
    // Delay for 1ms to avoid the SSC doesn't set correctly sometimes
    //
    MicroSecondDelay (1000);

    //
    // set the ssc_en and ssc_en_ovr to Enable!
    //
    WBuf.LJ1PLL_CTRL_1.Fields.ssc_en = SSC_ENABLE;
    WBuf.LJ1PLL_CTRL_1.Fields.ssc_en_ovr = SSC_ENABLE;

    //
    // send the IPC command for SSC EN
    //
    Status = IpcSendCommandEx (IPC_CMD_ID_EMI_RFI_SUPPORT, IPC_SUBCMD_ID_SSC_APPLY_NOW, &WBuf, BufferSize);
  } else {
    // get the correct register values of the clock bending setting
    WBuf.LJ1PLL_CTRL_5.Fields.pll_ratio_frac = CLK_Bending_Table[SystemConfiguration.DDRCLKBending].Pll_Ratio_Frac;
    WBuf.LJ1PLL_CTRL_5.Fields.pll_ratio_int = CLK_Bending_Table[SystemConfiguration.DDRCLKBending].Pll_Ratio_Int;

    Status = IpcSendCommandEx (IPC_CMD_ID_EMI_RFI_SUPPORT, IPC_SUBCMD_ID_SSC_APPLY_NOW, &WBuf, BufferSize);
    return Status;
  }

  return Status;
}

//
// USB3, PCie, SATA, eDP, DP, eMMC, SD and SDIO SSC
//
EFI_STATUS
EFIAPI
PeiHighSpeedSerialInterfaceSSCInit (
  VOID
  )
{
  EFI_STATUS                        Status;
  LCPLL_CR_RW_CONTROL_1             LCPLL_CTRL_1;
  LCPLL_CR_RW_CONTROL_2             LCPLL_CTRL_2;
  EFI_PEI_READ_ONLY_VARIABLE2_PPI   *VariableServices;
  SYSTEM_CONFIGURATION              SystemConfiguration;
  UINTN                             VariableSize;

  //
  // static table for the SSC settings (corresponding with the SSC settings 0~-0.5%, 0.1% stepping)
  // Modulation Freq = 32KHz
  //
  SSC_SETTING                     HSSIO_SSC_Select_Table[] = {{ No_SSC, 0x12B, 0 },
                                                              { M01_SSC, 0x12B, 0x1062 },
                                                              { M02_SSC, 0x12B, 0x2BB0 },
                                                              { M03_SSC, 0x12B, 0x46FF },
                                                              { M04_SSC, 0x12B, 0x624D },
                                                              { M05_SSC, 0x12B, 0x7D9C }};

  LCPLL_CTRL_1.Data = SideBandRead32 (0x99, 0x9910);
  LCPLL_CTRL_2.Data = SideBandRead32 (0x99, 0x9914);

  VariableSize = sizeof (SYSTEM_CONFIGURATION);
  Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariableServices);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  VariableSize = sizeof (SYSTEM_CONFIGURATION);

  Status = VariableServices->GetVariable (
                               VariableServices,
                               PLATFORM_SETUP_VARIABLE_NAME,
                               &gEfiSetupVariableGuid,
                               NULL,
                               &VariableSize,
                               &SystemConfiguration
                               );

  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "\nSetup Variable is not ready for SSC setting! Leave the default system HSSIO SSC settings!!\n\r"));
    return EFI_SUCCESS;
  }

  if (SystemConfiguration.HSSIOSSCEnable) {
    LCPLL_CTRL_2.Fields.ssc_cyc_to_peak_m1 = HSSIO_SSC_Select_Table[SystemConfiguration.HSSIOSSCSelection].Ssc_Cyc_To_Peak;
    LCPLL_CTRL_2.Fields.ssc_frac_step = HSSIO_SSC_Select_Table[SystemConfiguration.HSSIOSSCSelection].Ffs_Frac_Step;
    SideBandWrite32 (0x99, 0x9914, LCPLL_CTRL_2.Data);
    SideBandWrite32 (0x99, 0x9910, 0);
  } else {
    LCPLL_CTRL_1.Fields.ssc_en = SSC_DISABLE;
    LCPLL_CTRL_1.Fields.ssc_en_ovr = SSC_ENABLE;
    SideBandWrite32 (0x99, 0x9910, LCPLL_CTRL_1.Data);
  }
  return EFI_SUCCESS;
}
#endif


/**
  This is the entry point of PEIM

  @param[in]  FileHandle  Handle of the file being invoked.
  @param[in]  PeiServices Describes the list of possible PEI Services.

  @retval     EFI_SUCCESS if it completed successfully.

**/
EFI_STATUS
EFIAPI
PlatformInitPreMemEntryPoint (
  IN       EFI_PEI_FILE_HANDLE     FileHandle,
  IN CONST EFI_PEI_SERVICES        **PeiServices
  )
{
  EFI_PLATFORM_INFO_HOB            PlatformInfo;
  EFI_STATUS                       Status = EFI_SUCCESS;
  EFI_PEI_PPI_DESCRIPTOR           *PeiPpiDescriptor;
  FIRMWARE_SEC_PERFORMANCE         Performance;
  EFI_PEI_READ_ONLY_VARIABLE2_PPI  *VariableServices;
  VOID                             *Memory;
  IA32_DESCRIPTOR                  GdtDscriptor;
  UINT32                           Temp32;
  UINT32                           IfwiVerAddr;
  DRAM_POLICY_PPI                  *DramPolicy;
  EFI_PEI_PPI_DESCRIPTOR           *NewPeiPpiDescriptor;
  EFI_BOOT_MODE                    BootMode;
  CarMapStruc                      *CarMap;
  SYSTEM_CONFIGURATION             SystemConfiguration;
  UINTN                            VariableSize;
  EFI_PEI_HOB_POINTERS             Hob;
  EFI_PLATFORM_INFO_HOB            *PlatformInfoPtr;
  EFI_HOB_GUID_TYPE                *FdoEnabledGuidHob = NULL;
  UINT64                           StartTimerTicker = 0;
  UINT64                           Tick;
  UINTN                            AcpiVarHobSize;
  #if (ENBDT_PF_ENABLE == 1)
  MBP_CURRENT_BOOT_MEDIA           BootMediaData;
  #endif
  PEI_BOARD_PRE_MEM_INIT_PPI       *BoardPreMemInitPpi;
  UINTN                            Instance;

  Status = (*PeiServices)->RegisterForShadow (FileHandle);

  if (Status == EFI_ALREADY_STARTED) {
    ImageInMemory = TRUE;
  } else if (Status == EFI_NOT_FOUND) {
    ASSERT_EFI_ERROR (Status);
  }

  if (!ImageInMemory) {

    //
    // Since PEI has no PCI enumerator, set the BAR & I/O space enable ourselves
    //
    ScBaseInit ();
    MultiPlatformGpioProgramPreMem (&StartTimerTicker);
  }

  Status = InstallMonoStatusCode (FileHandle, PeiServices);

  if (!ImageInMemory) {
    //
    // Locate all Board Pre Mem Init PPI instances and call them one by one
    //
    Instance = 0;
    do {
      Status = PeiServicesLocatePpi (
                 &gBoardPreMemInitPpiGuid,
                 Instance,
                 &PeiPpiDescriptor,
                 &BoardPreMemInitPpi
                 );

      if (Status == EFI_NOT_FOUND) {
        break;
      }

      ASSERT_EFI_ERROR (Status);
      DEBUG ((EFI_D_INFO,  "Call Board Pre Mem Init PPI\n"));
      Status = BoardPreMemInitPpi->PreMemInit (PeiServices, BoardPreMemInitPpi);
      ASSERT_EFI_ERROR (Status);

      Instance ++;
    } while (TRUE);
  }

  AsmReadGdtr (&GdtDscriptor);
  DEBUG ((DEBUG_INFO, "GdtDscriptor Base Address:0x%X\n", (UINT32) GdtDscriptor.Base));

  PERF_START_EX (NULL, NULL, NULL, 0, 0x9100);
  SeCUmaEntry (FileHandle, PeiServices);
  PERF_END_EX (NULL, NULL, NULL, 0, 0x9101);

  Status = PeiScPreMemPolicyInit (&StartTimerTicker);
  ASSERT_EFI_ERROR (Status);
  Status = PeiSiPolicyInit ();
  ASSERT_EFI_ERROR (Status);

  if (!ImageInMemory) {
    if (GdtDscriptor.Base >= 0xFE000000) {
      IfwiVerAddr = GdtDscriptor.Base;
      IfwiVerAddr &= 0xfffff000;  // 4K alignment to get IBBL base address.
      IfwiVerAddr +=0x1000;  // the address of IBBL end
      for (Temp32 = 0; Temp32 < 0x8000; Temp32 += 0x10) {
        CarMap = (CarMapStruc *) (IfwiVerAddr-Temp32);
        if (CarMap->Sign == SIGNATURE_32 ('$','S','I','G')) {
          DEBUG ((DEBUG_INFO, "CarMap Address:0x%X\n", (UINT32) CarMap));
          break;
        }
      }
    }

    //
    // Set PcdIafwPlatformInfo = Real_Silicon + Max_RevId
    //
    PcdSet32S (PcdIafwPlatformInfo, 0x0000FF00);

    //
    // Initialize PlatformInfo HOB
    //
    ZeroMem (&PlatformInfo, sizeof (PlatformInfo));

    PlatformInfo.SsidSvid = (UINT32) CarMap;

    Status = ReadBxtIPlatformIds (PeiServices, &PlatformInfo);

    ASSERT_EFI_ERROR (Status);

    //
    // Build HOB for PlatformInfo
    //
    BuildGuidDataHob (
      &gEfiPlatformInfoGuid,
      &PlatformInfo,
      sizeof (EFI_PLATFORM_INFO_HOB)
      );
    //
    // Attempt to locate SMIP and publish its data to PPI's and PCDs.
    // Currently no reason to check Status, but could add in future.
    //
    // This currently installs gDramPolicyPpiGuid, but may move in future
    //
    Status = SmipInit ((VOID *)CarMap->FITBase, PlatformInfo.BoardId);

    MultiPlatformGpioUpdatePreMem ();

    //
    //Print out Patch version string (BXT)
    //
    AsmWriteMsr64 (0x8B, 0);
    AsmCpuid (0x1, NULL, NULL, NULL, NULL);
    Temp32 = (UINT32) (AsmReadMsr64 (0x8B) >> 32);
    DEBUG ((DEBUG_INFO, "PatchInfo:  0x%08x ", Temp32 ));
    DEBUG ((DEBUG_INFO, "%08x \n", (UINT32) (AsmReadMsr64 (0x8B))));

    //
    // Set the new boot mode for MRC
    //
    Status = UpdateBootMode (PeiServices, &PlatformInfo);
    ASSERT_EFI_ERROR (Status);

    //
    // Initialize MfgMemoryTest PPIs
    //
    Status = PeiServicesInstallPpi (&mMfgMemTestPpi);
    ASSERT_EFI_ERROR (Status);

    //
    // Setting 8254
    // Program timer 1 as refresh timer
    //
    IoWrite8 (0x43, 0x54);
    IoWrite8 (0x41, 0x12);

    //
    // RTC power failure handling
    //
    RtcPowerFailureHandler ();

    #if (ENBDT_PF_ENABLE == 1)
    if (GetBxtSeries() == BxtP) {
      //
      // DDR SSC
      //
      PeiDDRSSCInit ();

      //
      // USB3, PCie, SATA, eDP, DP, eMMC, SD and SDIO SSC
      //
    PeiHighSpeedSerialInterfaceSSCInit ();
    }
    #endif

    #if defined(PRAM_SUPPORT)
    //
    // Install Ppi for BIOS reserved memory
    //
    Status = PeiServicesInstallPpi (&mBiosReservedMemoryPolicyPpi);
    #endif

    Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariableServices);
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }

    VariableSize = sizeof (SystemConfiguration);
    Status = VariableServices->GetVariable (
                                 VariableServices,
                                 PLATFORM_SETUP_VARIABLE_NAME,
                                 &gEfiSetupVariableGuid,
                                 NULL,
                                 &VariableSize,
                                 &SystemConfiguration
                                 );

    AcpiVarHobSize = sizeof (UINT64);
    BuildGuidDataHob (
      &gEfiAcpiVariableCompatiblityGuid,
      &SystemConfiguration.AcpiVariableSetCompatibility,
      sizeof (AcpiVarHobSize)
      );

    DEBUG ((DEBUG_INFO, "AcpiVariableAddr : 0x%08x\n", SystemConfiguration.AcpiVariableSetCompatibility));

    PERF_START_EX (NULL, "RstVctr", "IBBL", 1, 0x1000);
    Tick = CarMap->IbblPerfRecord0;
    PERF_END_EX (NULL, "RstVctr", "IBBL", Tick, 0x1001);

    PERF_START_EX (NULL, "InitNEM", "IBBL", Tick, 0x1010);
    Tick = CarMap->IbblPerfRecord1;
    PERF_END_EX (NULL, "InitNEM", "IBBL", Tick, 0x1011);

    PERF_START_EX (NULL, "IBBLSdw", "IBBL", Tick, 0x1020);
    Tick = CarMap->IbblPerfRecord2;
    PERF_END_EX (NULL, "IBBLSdw", "IBBL", Tick, 0x1021);

    PERF_START_EX (NULL, "IBBMLod", "IBBL", Tick, 0x1030);
    Tick = CarMap->IbblPerfRecord3;
    PERF_END_EX (NULL, "IBBMLod", "IBBL", Tick, 0x1031);

    PERF_START_EX (NULL, "IBBMVer", "IBBL", Tick, 0x1040);
    Tick = CarMap->IbblPerfRecord4;
    PERF_END_EX (NULL, "IBBMVer", "IBBL", Tick, 0x1041);

    //
    // Normal boot - build Hob for SEC performance data.
    //
    Performance.ResetEnd = GetTimeInNanoSecond (CarMap->IbblPerfRecord0);
    if (!EFI_ERROR (Status)) {
      BuildGuidDataHob (
        &gEfiFirmwarePerformanceGuid,
        &Performance,
        sizeof (FIRMWARE_SEC_PERFORMANCE)
      );
      DEBUG ((EFI_D_INFO, "FPDT: SEC Performance Hob ResetEnd = %ld\n", Performance.ResetEnd));
    }

  } else {  //PostMem

    Hob.Raw = GetFirstGuidHob (&gEfiPlatformInfoGuid);
    ASSERT (Hob.Raw != NULL);
    PlatformInfoPtr = GET_GUID_HOB_DATA (Hob.Raw);
    CarMap = (CarMapStruc *) (UINT32) PlatformInfoPtr->SsidSvid;
    //
    // Locate and Reinstall necessary PPI's before MemoryCallback is run
    //
    Status = PeiServicesLocatePpi (
               &gDramPolicyPpiGuid,
               0,
               &PeiPpiDescriptor,
               NULL // PPI
               );

    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "Couldn't locate DRAM Policy PPI, LocatePpi returned %r.\n", Status));
    } else {
      DramPolicy          = (DRAM_POLICY_PPI *)        AllocateZeroPool (sizeof (DRAM_POLICY_PPI));
      NewPeiPpiDescriptor = (EFI_PEI_PPI_DESCRIPTOR *) AllocateZeroPool (sizeof (EFI_PEI_PPI_DESCRIPTOR));
      if ((DramPolicy == NULL) || (NewPeiPpiDescriptor == NULL)) {
        DEBUG ((DEBUG_ERROR, "Couldn't allocate memory for DRAM Policy PPI.\n"));
      } else {
        (*PeiServices)->CopyMem (
                          (VOID *) DramPolicy,
                          (VOID *) PeiPpiDescriptor->Ppi,
                          sizeof (DRAM_POLICY_PPI)
                          );

        NewPeiPpiDescriptor->Ppi = DramPolicy;
        NewPeiPpiDescriptor->Guid = &gDramPolicyPpiGuid;
        NewPeiPpiDescriptor->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
        Status = (**PeiServices).ReInstallPpi (
                                   PeiServices,
                                   PeiPpiDescriptor,
                                   NewPeiPpiDescriptor
                                   );
      }
    }

    if (GdtDscriptor.Base >= 0xFE000000) {
      (*PeiServices)->CopyMem (
                        (VOID *) mGdtTable,
                        (VOID *) GdtDscriptor.Base,
                        GdtDscriptor.Limit + 1
                        );
      GdtDscriptor.Base = (UINT32) mGdtTable;
      AsmWriteGdtr (&GdtDscriptor);
    }

    //
    // Set "Force Volatile Mode" in the variable driver
    // If Firmware Descriptor Override (FDO) boot is enabled
    //
    FdoEnabledGuidHob = GetFirstGuidHob (&gFdoModeEnabledHobGuid);
    if (FdoEnabledGuidHob != NULL) {
      PcdSetBoolS (PcdForceVolatileVariable, TRUE);
    }

    //
    // locate the MfgMemoryTest PPI
    //
    Status = PeiServicesLocatePpi (
               &gPeiMfgMemoryTestPpiGuid,  // GUID
               0,                          // INSTANCE
               &PeiPpiDescriptor,          // EFI_PEI_PPI_DESCRIPTOR
               NULL // PPI
               );
    if (Status == EFI_SUCCESS) {
      //
      // Reinstall the MfgMemoryTest PPI
      //
      Status = PeiServicesReInstallPpi (
                 PeiPpiDescriptor,
                 &mMfgMemTestPpi
                 );
    }

    //
    // locate the TemporaryRamSupport PPI
    //
    Status = PeiServicesLocatePpi (
               &gEfiTemporaryRamSupportPpiGuid,  // GUID
               0,                                // INSTANCE
               &PeiPpiDescriptor,                // EFI_PEI_PPI_DESCRIPTOR
               NULL                              // PPI
               );

    if (Status == EFI_SUCCESS) {
      //
      // Reinstall the Variable PPI
      //
      Status = PeiServicesReInstallPpi (
                 PeiPpiDescriptor,
                 mPeiTemporaryRamSupportPpiPpi
                 );
    }

#if defined(PRAM_SUPPORT)
    //
    // locate the BiosReservedMemory PPI
    //
    Status = PeiServicesLocatePpi (
               &gBiosReservedMemoryPolicyPpiGuid,
               0,
               &PeiPpiDescriptor,
               NULL // PPI
               );

    if (Status == EFI_SUCCESS) {
      Status = PeiServicesReInstallPpi (
                 PeiPpiDescriptor,
                 &mBiosReservedMemoryPolicyPpi
                 );
    }
#endif

    //
    // Initialize Stall PPIs
    //
    Status = PeiServicesInstallPpi (&mInstallStallPpi);
    ASSERT_EFI_ERROR (Status);

    Status = PeiServicesGetBootMode (&BootMode);
#if (ENBDT_PF_ENABLE == 1)
    if (BootMode == BOOT_ON_S3_RESUME) {
      Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariableServices);
      if (EFI_ERROR (Status)) {
        ASSERT_EFI_ERROR (Status);
        return Status;
      }
      VariableSize = sizeof (SYSTEM_CONFIGURATION);
      Status = VariableServices->GetVariable (
                                   VariableServices,
                                   PLATFORM_SETUP_VARIABLE_NAME,
                                   &gEfiSetupVariableGuid,
                                   NULL,
                                   &VariableSize,
                                   &SystemConfiguration
                                   );

      if (!EFI_ERROR (Status)) {
        SetMem (&BootMediaData, sizeof (MBP_CURRENT_BOOT_MEDIA), 0x0);
        switch (SystemConfiguration.CseBootDevice) {
          case 0:
            DEBUG ((DEBUG_INFO, "CSE Boot Device is EMMC.\n"));
            Status = (*PeiServices)->InstallPpi (PeiServices, mCseEmmcSelectPpiList);
            break;
          case 1:
            DEBUG ((DEBUG_INFO, "CSE Boot Device is UFS.\n"));
            Status = (*PeiServices)->InstallPpi (PeiServices, mCseUfsSelectPpiList);
            break;
          case 2:
            DEBUG ((DEBUG_INFO, "CSE Boot Device is SPI.\n"));
            Status = (*PeiServices)->InstallPpi (PeiServices, mCseSpiSelectPpiList);
            break;
          default:
            DEBUG ((EFI_D_ERROR, "\nCSE Boot device is unknown. Cannot continue!\n"));
            CpuDeadLoop();
            break;

        }
        BootMediaData.PhysicalData = SystemConfiguration.CseBootDevice;
        //
        // Build HOB for BootMediaData
        //
        BuildGuidDataHob (
          &gEfiBootMediaHobGuid,
          &BootMediaData,
          sizeof (MBP_CURRENT_BOOT_MEDIA)
          );
      }
    }
#endif

    //
    // copy IBBM from Cache to DRAM. the hardcoded address need to be changed
    // to use the parameter in IBBL.
    //
    Memory = AllocatePages (EFI_SIZE_TO_PAGES (PcdGet32 (PcdFlashFvIBBMSize)));
    if (Memory != NULL) {
      CopyMem (Memory , (VOID *) CarMap->IBBBase, PcdGet32 (PcdFlashFvIBBMSize));
      DEBUG ((DEBUG_INFO, "IBBM address: %x\n", Memory));
      PeiServicesInstallFvInfoPpi (
        NULL,
        (VOID *) Memory,
        PcdGet32 (PcdFlashFvIBBMSize),
        NULL,
        NULL
        );
    } else  {
      ASSERT (FALSE);
    }

    DEBUG ((DEBUG_INFO, "PreMem Policy Init - Start\n"));
    //
    // Initialize Pre-Mem PEI Platform Policy
    //
    Status = PeiPolicyInitPreMem ();
    ASSERT_EFI_ERROR (Status);
    DEBUG ((DEBUG_INFO, "PreMem Policy Init - End\n\n"));

    //
    // Register Notify Callback to process OBB loading.
    // In FSP+Wrapper, the MTRRs are set after TempRamExit, not gEfiPeiMemoryDiscoveredPpiGuid.
    //
    Status = PeiServicesNotifyPpi (&mFspTempRamExitList[0]);
    ASSERT_EFI_ERROR (Status);
  } //end PostMem

  DEBUG ((DEBUG_INFO, "PeiInitPlatform end\n"));

  return Status;
}

//
// Read Platform ID for IOTG Platforms
//
EFI_STATUS
ReadBxtIPlatformIds (
  IN CONST EFI_PEI_SERVICES     **PeiServices,
  IN OUT EFI_PLATFORM_INFO_HOB  *PlatformInfoHob
  )
{
  UINT8                       BoardId = 0;
  UINT8                       FabId = 0;

  DEBUG ((DEBUG_INFO, "Port(0x62) = %02X\n", IoRead8 (0x62)));

  PlatformInfoHob->ECPresent = 0;
  BoardId = (UINT8) PcdGet8 (PcdBoardId);
  FabId = (UINT8) PcdGet8 (PcdFabId);

  PlatformInfoHob->BoardId = BoardId;
  PlatformInfoHob->BoardRev = FabId;

  DEBUG ((DEBUG_INFO, "BoardId:  [0x%08x]\n", PlatformInfoHob->BoardId));
  DEBUG ((DEBUG_INFO, "FabId:    [0x%08x]\n", PlatformInfoHob->BoardRev));

  return EFI_SUCCESS;
}