summaryrefslogtreecommitdiff
path: root/Chipset/NB/NBGeneric.c
blob: 6f9a95f0d7897548b53a7e68c0b5c6206b27342b (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
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/BIN/Chipset/Intel/NorthBridge/Haswell/Intel SystemAgent NB Chipset/NBGeneric.c 6     10/14/12 5:17a Jeffch $
//
// $Revision: 6 $
//
// $Date: 10/14/12 5:17a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Chipset/Intel/NorthBridge/Haswell/Intel SystemAgent NB Chipset/NBGeneric.c $
// 
// 6     10/14/12 5:17a Jeffch
// [TAG]          None
// [Severity]     Important
// [Description]  Follow SA RC 0.71.
// [Files]        NBPei.c, NBDxe.c; NBGeneric.c; NBCspLib.h; NBSetup.c;
// Nb.sd; GetSetupData.c
// 
// 5     10/14/12 12:20a Jeffch
// [TAG]         None
// [Severity]    Important
// [Description] Update by XTU4.0.
// [Files]       NBPei.c, NBDxe.c, NBCspLib.h, NBGeneric.c
// 
// 4     9/12/12 6:20a Yurenlai
// [TAG]           None
// [Category]      Improvement
// [Severity]      Important
// [Description]   Fixed some pcie card compatibility issue. <from Jeffch>
// [Files]         NBGeneric.c
// 
// 3     8/24/12 8:09a Yurenlai
// [TAG]  		    None
// [Category]  	Improvement
// [Severity]  	Important
// [Description]  	Remove useless SB_SHADOW_CONTROL.
// [Files]  		NBGeneric.c
// 
// 2     4/05/12 2:45a Yurenlai
// [TAG]  		EIP87103
// [Category]  	Spec Update
// [Severity]  	Important
// [Description]  	Change for SystemAgent RefCode Revision: 0.5.5 .
// [Files]  		NBDxe.c, NBPEI.c, NBSMI.C, NBGeneric.cm NB.sd, NBSetup.c,
// GetSetupData.c, NbSetupData.
// 
// 1     2/08/12 4:34a Yurenlai
// Intel Haswell/NB eChipset initially releases.
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:        NBGeneric.C
//
// Description: This file contains generic NB code that is common between
//              various components such as NB PEI, DXE etc
//
// Notes:       MAKE SURE NO PEI OR DXE SPECIFIC CODE IS NEEDED
//
//<AMI_FHDR_END>
//*************************************************************************

//----------------------------------------------------------------------------
// Include(s)
//----------------------------------------------------------------------------

#include <Efi.h>
#include <token.h>
#include <AmiLib.h>
#include <AmiCspLib.h>
#include <AmiDxeLib.h>
#include <Protocol\PciRootBridgeIo.h>

//----------------------------------------------------------------------------
// Constant, Macro and Type Definition(s)
//----------------------------------------------------------------------------
// Constant Definition(s)
//--------------------EMRR Support--------------------------------------------
#define MAX_NR_BUS ((PCIEX_LENGTH/0x100000)-1)
#define UNCORE_CR_MCSEG_BASE0 (volatile UINT64*)NB_PCIE_CFG_ADDRESS(MAX_NR_BUS, 0, 1, 0x60) 
#define UNCORE_CR_MCSEG_MASK0_LOW  (volatile UINT32*)NB_PCIE_CFG_ADDRESS(MAX_NR_BUS, 0, 1, 0x68)
#define UNCORE_CR_MCSEG_MASK0_HIGH (volatile UINT32*)NB_PCIE_CFG_ADDRESS(MAX_NR_BUS, 0, 1, 0x6C)
#define UNCORE_CR_MCSEG_BASE1      (volatile UINT64*)NB_PCIE_CFG_ADDRESS((MAX_NR_BUS - 1), 0, 1, 0x60)
#define UNCORE_CR_MCSEG_MASK1_LOW  (volatile UINT32*)NB_PCIE_CFG_ADDRESS((MAX_NR_BUS - 1), 0, 1, 0x68)
#define UNCORE_CR_MCSEG_MASK1_HIGH (volatile UINT32*)NB_PCIE_CFG_ADDRESS((MAX_NR_BUS - 1), 0, 1, 0x6C)
//----------------------------------------------------------------------------

#if     CSM_SUPPORT
#define ATTR_DISABLED           0       // Shadow RAM Disabled
#define ATTR_READ               1       // Shadow RAM Read Enabled
#define ATTR_WRITE              2       // Shadow RAM Write Enabled
#define ATTR_READ_WRITE         3       // Shadow RAM Read/Write Enabled
#endif

// Macro Definition(s)

// Type Definition(s)

// Function Prototype(s)

//----------------------------------------------------------------------------
// Variable and External Declaration(s)
//----------------------------------------------------------------------------
// Variable Declaration(s)

EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *gPciRootBridgeIo;

EFI_RUNTIME_SERVICES            *pRS;

//----------------------------------------------------------------------------
// The following table contains the information regarding the shadow RAM
// registers and other North Bridge registers that need to be restored
// during the S3 wakeup.
// Mention all register address (bus, device, function , register), specify
// the size of the register ans the mask also.
//----------------------------------------------------------------------------
BOOT_SCRIPT_NB_PCI_REG_SAVE gNbRegsSaveTbl[] = {
    {SA_MC_BUS, SA_MC_DEV, SA_MC_FUN, R_SA_PAM0, EfiBootScriptWidthUint8, 0x33},
    {SA_MC_BUS, SA_MC_DEV, SA_MC_FUN, R_SA_PAM1, EfiBootScriptWidthUint8, 0x33},
    {SA_MC_BUS, SA_MC_DEV, SA_MC_FUN, R_SA_PAM2, EfiBootScriptWidthUint8, 0x33},
    {SA_MC_BUS, SA_MC_DEV, SA_MC_FUN, R_SA_PAM3, EfiBootScriptWidthUint8, 0x33},
    {SA_MC_BUS, SA_MC_DEV, SA_MC_FUN, R_SA_PAM4, EfiBootScriptWidthUint8, 0x33},
    {SA_MC_BUS, SA_MC_DEV, SA_MC_FUN, R_SA_PAM5, EfiBootScriptWidthUint8, 0x33},
    {SA_MC_BUS, SA_MC_DEV, SA_MC_FUN, R_SA_PAM6, EfiBootScriptWidthUint8, 0x33},
 //   {SAD_BUS, SAD_DEV, SAD_FUN, SAD_REG_SMRAM, EfiBootScriptWidthUint8, 0xff},
};

#define NUM_NB_PCI_REG_SAVE \
                    sizeof(gNbRegsSaveTbl)/sizeof(BOOT_SCRIPT_NB_PCI_REG_SAVE)

#if     CSM_SUPPORT

/** Porting required for the following structure **/
NB_PAM_STRUCT gPamStruct[] =
{
    {R_SA_PAM1, 0xfc, 0xc0000, 0x4000},   
    {R_SA_PAM1, 0xcf, 0xc4000, 0x4000},   
    {R_SA_PAM2, 0xfc, 0xc8000, 0x4000},   
    {R_SA_PAM2, 0xcf, 0xcc000, 0x4000},   
    {R_SA_PAM3, 0xfc, 0xd0000, 0x4000},   
    {R_SA_PAM3, 0xcf, 0xd4000, 0x4000},   
    {R_SA_PAM4, 0xfc, 0xd8000, 0x4000},   
    {R_SA_PAM4, 0xcf, 0xdc000, 0x4000},   
    {R_SA_PAM5, 0xfc, 0xe0000, 0x4000},
    {R_SA_PAM5, 0xcf, 0xe4000, 0x4000},
    {R_SA_PAM6, 0xfc, 0xe8000, 0x4000},
    {R_SA_PAM6, 0xcf, 0xec000, 0x4000},
    {R_SA_PAM0, 0xcf, 0xf0000,0x10000}
};

#define NUM_PAM_ENTRIES (sizeof(gPamStruct) / sizeof(NB_PAM_STRUCT))

//----------------------------------------------------------------------------
// Start OF CSM Related Porting Hooks
//----------------------------------------------------------------------------

// The following data structure specifies the PCI device/function number of
// the root bridge(s). Number of entries in this table defined by
// ROOT_BRIDGE_COUNT.
// This table is a missing link between RootBridgeIo and PciIo, which allows
// to update BusNumXlat table with actual bus numbers.
// Each entry in the table is a pair of RootBridge UID (UINT32), provided in
// RootBridge device path, and PCI Dev/Func number (UINT8) that can be used 
// to access Root Bridge on
// PCI bus.

// PORTING PORTING - Include device function number of RB 

ROOT_BRIDGE_MAPPING_ENTRY   RbMap[ROOT_BRIDGE_COUNT] = {
//  RB ID           Device function number
    {0x00,          (UINT8)((NB_DEV << 3) + NB_FUN)}
};
UINTN   RbCount = ROOT_BRIDGE_COUNT;

#endif
/*
typedef enum {
  fNoInit   = 0,
  f800      = 800,
  f1000     = 1000,
  f1067     = 1067,
  f1200     = 1200,
  f1333     = 1333,
  f1400     = 1400,
  f1600     = 1600,
  f1800     = 1800,
  f1867     = 1867,
  f2000     = 2000,
  f2133     = 2133,
  f2200     = 2200,
  f2400     = 2400,
  f2600     = 2600,
  f2667     = 2667,
  fUnSupport= 0x7FFFFFFF
} NbFrequency;
*/
#define BCLK_DEFAULT    (100 * 1000 * 1000)
#define fNoInit     (0)
#define f800        (800)
#define f1000       (1000)
#define f1067       (1067)
#define f1200       (1200)
#define f1333       (1333)
#define f1400       (1400)
#define f1600       (1600)
#define f1800       (1800)
#define f1867       (1867)
#define f2000       (2000)
#define f2133       (2133)
#define f2200       (2200)
#define f2400       (2400)
#define f2600       (2600)
#define f2667       (2667)
#define fUnSupport  (0x7FFFFFFF)
typedef UINT32 NbFrequency;
typedef UINT8 NbClockRatio;

// GUID Definition(s)

// Protocol Definition(s)

// External Declaration(s)

// Function Definition(s)

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: NbFrequencyToRatio
//
// Description: Convert the given frequency and reference clock to a clock ratio. 
//
// Input:
//      IN Frequency - The memory frequency.
//      IN The memory reference clock.
//      IN RefBClk - The base system reference clock.
//
// Output:
//      Returns the memory clock ratio.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT8
NbFrequencyToRatio (
  UINT32    Frequency,
  UINT8     RefClk,
  UINT32    RefBClk
)
{
  UINT64 Value;
  UINT64 FreqValue;
  UINT32 RefClkValue;
  UINT32 BClkValue;

  BClkValue   = (RefBClk == 0) ? (BCLK_DEFAULT / 100000) : (RefBClk / 100000);
  RefClkValue = (RefClk == 1) ? 200000 : 266667;
  FreqValue   = Mul64 (Frequency, 1000000000ULL);
  Value       = Div64 (FreqValue, (RefClkValue * BClkValue), NULL);
  Value       = ((UINT32) Value + 500) / 1000;

  return ((NbClockRatio) Value);

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: NbRatioToFrequency
//
// Description: Convert the given ratio and reference clock to a memory frequency. 
//
// Input:
//      IN Ratio   - The memory ratio.
//      IN RefClk  - The memory reference clock.
//      IN RefBClk - The base system reference clock.
//
// Output:
//      Returns the memory frequency.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32
NbRatioToFrequency (
  UINT8   Ratio,
  UINT8   RefClk,
  UINT32  RefBClk
)
{

  UINT64 Value;
  UINT32 BClkValue;
  UINT32 RefClkValue;

  BClkValue   = (RefBClk == 0) ? BCLK_DEFAULT : RefBClk;
  RefClkValue = (RefClk == 1) ? 200000000 : 266666667;
  Value       = Mul64 (RefClkValue, Ratio * BClkValue);
  Value       += 50000000000000ULL;
  Value       = Div64 (Value, (UINTN)100000000000000ULL, NULL);
  return ((NbFrequency) Value);
}
//----------------------------------------------------------------------------
#if     CSM_SUPPORT

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   NBGetPamStartEndIndex
//
// Description: Helper function to get the Start and End Index for
//              PAM register table.
//
// Input:       StartAddress - Shadow RAM start address to be programed
//              Length       - Shadow RAM length to be programed
//              *StartIndex  - Pointer a variable for the Start index
//              *EndIndex    - Pointer a variable for the End index
//
// Output:      EFI_STATUS
//                  EFI_INVALID_PARAMETER - Input Parameter is invalid.
//                  EFI_SUCCESS           - Get indexs from PAM register
//                                          table successfully.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS NBGetPamStartEndIndex (
    IN UINT32                   StartAddress,
    IN UINT32                   Length,
    OUT UINT32                  *StartIndex,
    OUT UINT32                  *EndIndex )
{
    UINT32                  StartIdx;
    UINT32                  EndIdx;
    UINT32                  TotalLength = 0;

    if (StartAddress < gPamStruct[0].StartAddress)
        return EFI_INVALID_PARAMETER;

    for(StartIdx = 0; StartIdx < NUM_PAM_ENTRIES; ++StartIdx) {
        if (StartAddress <= gPamStruct[StartIdx].StartAddress) break;
    }
    if (StartAddress < gPamStruct[StartIdx].StartAddress) StartIdx--;

    if (StartIdx == NUM_PAM_ENTRIES) return EFI_INVALID_PARAMETER;

    // Adjust the length of the requested region if starting address is
    // out of bounds.
    Length += (StartAddress - gPamStruct[StartIdx].StartAddress);

    for(EndIdx = StartIdx; EndIdx < NUM_PAM_ENTRIES; ++EndIdx) {
        TotalLength += gPamStruct[EndIdx].Length;
        if (TotalLength >= Length) break;
    }
    if (EndIdx == NUM_PAM_ENTRIES) return EFI_INVALID_PARAMETER;

    *StartIndex = StartIdx;
    *EndIndex = EndIdx;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBProgramPAMRegisters
//
// Description: Program 0xc0000 - 0xfffff regions to Lock/Unlock.
//
// Input:       pBS          - Pointer to Boot Service Table
//              pRS          - Pointer to Runtime Service Table
//              StartAddress - Shadow RAM start address to be programed
//              Length       - Shadow RAM length to be programed
//              Setting      - Shadow RAM Lock/Unlock status to program
//              *Granularity - The granularity for this region
//
// Output:      EFI_STATUS
//                  EFI_INVALID_PARAMETER - Input Parameter is invalid.
//                  EFI_SUCCESS           - Program successfully.
//
// Notes:       Here is the control flow of this function:
//              1. Search the structure for the first entry matching 
//                 the StartAddress.
//              2. If not found, return EFI_INVALID_PARAMETER.
//              3. Find the last entry in structure for the region to program,
//                 by adding the lengths of the entries.
//              4. If not found, return EFI_INVALID_PARAMETER.
//              5. Read/Write each register for the entry to set region.
//              6. Return the Granularity for the region.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS NBProgramPAMRegisters (
    IN EFI_BOOT_SERVICES    *pBS,
    IN EFI_RUNTIME_SERVICES *pRS,
    IN UINT32               StartAddress,
    IN UINT32               Length,
    IN UINT8                Setting,
    IN OUT UINT32           *Granularity )
{
    // NB shadow programming.
    // Note: For this routine to work, the gPamStruct regions must
    // be continuous.

    EFI_STATUS          Status = EFI_SUCCESS;
    UINT32              StartIndex;
    UINT32              EndIndex;
    UINTN               i;
    UINT8               Data;
    UINT8               Shift;

    Status = NBGetPamStartEndIndex( StartAddress, \
                                    Length, \
                                    &StartIndex, \
                                    &EndIndex );
    if (EFI_ERROR(Status)) return Status;


    for (i = StartIndex; i <= EndIndex; ++i) {
        //Bus 0, Device 0, Function 0
        Data = READ_PCI8_NB(gPamStruct[i].Register);
        Data &= gPamStruct[i].Mask;
        Shift = (gPamStruct[i].Mask == 0xfc) ? 0 : 4;
        switch (Setting) {
            case 0 :            // Read Only
            case 1 :            // Read Only (Permanently)
                    Data |= (ATTR_READ << Shift);
                    break;
            case 2 :            // Read/Write
                    Data |= (ATTR_READ_WRITE << Shift);
                    break;
            case 3 :            // Disabled
            default:
                    break;
        }
        WRITE_PCI8_NB(gPamStruct[i].Register, Data);
    }

    if (Granularity)
        *Granularity = ( (StartAddress+Length) < 0xf0000 ) ? 0x4000 : 0x10000;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBPeiProgramPAMRegisters
//
// Description: Program 0xc0000 - 0xfffff regions to Lock/Unlock.
//
// Input:       PeiServices  - The PEI core services table.
//              StartAddress - Shadow RAM start address to be programed
//              Length       - Shadow RAM length to be programed
//              Setting      - Shadow RAM Lock/Unlock status to program
//              *Granularity - The granularity for this region
//
// Output:      EFI_STATUS
//                  EFI_INVALID_PARAMETER - Input Parameter is invalid.
//                  EFI_SUCCESS           - Program successfully.
//
// Notes:       Here is the control flow of this function:
//              1. Search the structure for the first entry matching 
//                 the StartAddress.
//              2. If not found, return EFI_INVALID_PARAMETER.
//              3. Find the last entry in structure for the region to program,
//                 by adding the lengths of the entries.
//              4. If not found, return EFI_INVALID_PARAMETER.
//              5. Read/Write each register for the entry to set region.
//              6. Return the Granularity for the region.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS NBPeiProgramPAMRegisters (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN UINT32               StartAddress,
    IN UINT32               Length,
    IN UINT8                Setting,
    IN OUT UINT32           *Granularity OPTIONAL )
{
    // NB shadow programming.
    // Note: For this routine to work, the gPamStruct regions must
    // be continuous.

    EFI_STATUS          Status = EFI_SUCCESS;
    UINT32              StartIndex;
    UINT32              EndIndex;
    UINTN               i;
    UINT8               Data;
    UINT8               Shift;

    Status = NBGetPamStartEndIndex( StartAddress, \
                                    Length, \
                                    &StartIndex, \
                                    &EndIndex );
    if (EFI_ERROR(Status)) return Status;


    for (i = StartIndex; i <= EndIndex; ++i) {
        //Bus 0, Device 0, Function 0
        Data = READ_PCI8_NB(gPamStruct[i].Register);
        Data &= gPamStruct[i].Mask;
        Shift = (gPamStruct[i].Mask == 0xfc) ? 0 : 4;
        switch (Setting) {
            case 0 :            // Read Only
            case 1 :            // Read Only (Permanently)
                    Data |= (ATTR_READ << Shift);
                    break;
            case 2 :            // Read/Write
                    Data |= (ATTR_READ_WRITE << Shift);
                    break;
            case 3 :            // Disabled
            default:
                    break;
        }
        WRITE_PCI8_NB(gPamStruct[i].Register, Data);
    }

    if (Granularity)
        *Granularity = ( (StartAddress+Length) < 0xf0000 ) ? 0x4000 : 0x10000;

    return EFI_SUCCESS;
}

//----------------------------------------------------------------------------
#endif          // END OF CSM Related Porting Hooks
//----------------------------------------------------------------------------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBPAMWriteBootScript
//
// Description: Writes the final settings of NB registers to the BOOT Script
//
// Input:       *BootScriptSave - Pointer to Boot Script Save Protocal
//
// Output:      EFI_STATUS
//                  EFI_SUCCESS
//
// Notes:       Here is the control flow of this function:
//              1. From the Pci register save table, read the pci register
//                 to save.
//              2. Write to the boot script the value.
//              3. Repeat 1 & 2 for all table entries.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS NBPAMWriteBootScript (
    IN AMI_S3_SAVE_PROTOCOL            *BootScriptSave )
{
    UINT8           PciBus8;
    UINT8           PciSubBus8;
    UINTN           i;
    UINT32          Value32;
    UINT64          Address64;

    //Porting required: Write Boot Script

    for (i = 0; i < NUM_NB_PCI_REG_SAVE; ++i) {
        Address64 = NB_PCI_CFG_ADDRESS( gNbRegsSaveTbl[i].Bus, \
                                        gNbRegsSaveTbl[i].Dev, \
                                        gNbRegsSaveTbl[i].Fun, \
                                        gNbRegsSaveTbl[i].Reg );
        gPciRootBridgeIo->Pci.Read( gPciRootBridgeIo, \
                                    gNbRegsSaveTbl[i].Width, \
                                    Address64, \
                                    1, \
                                    &Value32 );
        Value32 &= gNbRegsSaveTbl[i].Mask;
        BOOT_SCRIPT_S3_PCI_CONFIG_WRITE_MACRO( BootScriptSave, \
                                               gNbRegsSaveTbl[i].Width, \
                                               Address64, \
                                               1, \
                                               &Value32 );
    }

    PciBus8 = READ_PCI8_PCIEBRN(PCIEBRN_REG_SBUSN); // 0x19
    // Check nVIDIA PCIe VGA card 
    if (READ_PCI16(PciBus8, 0, 0, PCI_VID) == 0x10de) {
        Value32 = READ_PCI32(PciBus8, 0, 0, PCI_SVID); // 0x2c
        BOOT_SCRIPT_S3_PCI_CONFIG_WRITE_MACRO( \
                                        BootScriptSave, \
                                        EfiBootScriptWidthUint8, \
                                        PCIEBRN_REG(PCIEBRN_REG_SBUSN), \
                                        1, \
                                        &PciBus8 );
        PciSubBus8 = READ_PCI8_PCIEBRN(PCIEBRN_REG_SUBUSN); // 0x1a
        BOOT_SCRIPT_S3_PCI_CONFIG_WRITE_MACRO( \
                                        BootScriptSave, \
                                        EfiBootScriptWidthUint8, \
                                        PCIEBRN_REG(PCIEBRN_REG_SUBUSN), \
                                        1, \
                                        &PciSubBus8 );
        BOOT_SCRIPT_S3_PCI_CONFIG_WRITE_MACRO( \
                                     BootScriptSave, \
                                     EfiBootScriptWidthUint32, \
                                     NB_PCI_CFG_ADDRESS(PciBus8, 0 ,0, 0x40),\
                                     1, \
                                     &Value32 );
    }
    
    return EFI_SUCCESS; 
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: NbFindCapPtr
//
// Description: This function searches the PCI address space for the PCI
//              device specified for a particular capability ID and returns
//              the offset in the PCI address space if one found
//
// Input:    UINT64  PciAddress,
//           UINT8   CapId
//
// Output:  Capability ID Address if one found
//          Otherwise returns 0
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32 NbFindCapPtr(
 IN UINT64  PciAddress,
 IN UINT8   CapId
)
{
  UINT8   Value;
  UINT32  Address = (UINT32)PciAddress;

  Address = (Address & 0xffffff00) | 6; //PCI Status Register.
  Value = READ_MEM8(Address + 0);

  if (Value == 0xff) return 0;          // No device.
  if (!(Value & (1 << 4))) return 0;    // Check if capabilities list.
   
  *(UINT8*)&Address = 0x34;             // Register to First capabilities pointer
                                        // if 0, then capabilities
  for(;;)
  {
    Value = READ_MEM8(Address + 0);
    if (Value == 0) return 0;

    *(UINT8*)&Address = Value;          // PciAddress = ptr to CapID
    Value = READ_MEM8(Address + 0);     // New cap ptr.

    //If capablity ID, return register that points to it.
    if (Value == CapId) return Address; 

    ++Address;                          // Equals to next capability pointer.
  }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBRetrainLinkPciDevice
//
// Description: This function is Retrain Link NB Pci Device.
//
// Input:       PciBus    - PCI Bus Number.
//              PciDev    - PCI Device Number.
//              PciFun    - PCI Function Number.
//              PciCapPtr - PCI CapPtr Number.
//
// Output:      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID NBRetrainLinkPciDevice (
    IN UINT8                PciBus,
    IN UINT8                PciDev,
    IN UINT8                PciFun,
    IN UINT8                CapPtr )
{
    // Disable Link
    SET_PCI8(PciBus, PciDev, PciFun, CapPtr + 0x10, BIT04);

    // Retrain Link
    RW_PCI8(PciBus, PciDev, PciFun, CapPtr + 0x10 , BIT05, BIT04);

    // Wait Link States
    while (READ_PCI16(PciBus, PciDev, PciFun, CapPtr + 0x12) & BIT11);

    // if Retrain Link Anyway 1, Clear Retrain Link
    if (READ_PCI8(PciBus, PciDev, PciFun, CapPtr + 0x10) & BIT05) {
       RESET_PCI8(PciBus, PciDev, PciFun, CapPtr + 0x10 , BIT05);
    }

    // Wait Link States
    while (READ_PCI16(PciBus, PciDev, PciFun, CapPtr + 0x12) & BIT11);
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBProtectedPciDevice
//
// Description: This function is called by PCI Bus Driver before configuring
//              or disabling any PCI device. This function should examine the
//              Vendor/Device ID or PCI Bus, Device and Function numbers to
//              make sure it is not a north bridge device or any other device
//              which should no be configured by PCI Bus Driver.
//
// Input:       *PciDevice - Pointer to PCI Device Info structure.
//
// Output:      EFI_STATUS
//                  EFI_SUCCESS     - SKIP this device, do not touch
//                                    PCI Command register.
//                  EFI_UNSUPPORTED - DON'T SKIP this device do complete
//                                    enumeration as usual.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS NBProtectedPciDevice (
    IN PCI_DEV_INFO             *PciDevice )
{

//####if ((PciDevice->Address.Addr.Bus == NB_BUS) && \
//####    (PciDevice->Address.Addr.Device == NB_DEV) && \
//####    (PciDevice->Address.Addr.Function == NB_FUN)) {
//####
//####    return EFI_SUCCESS;
//####}

    return EFI_UNSUPPORTED;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBProgramPciDevice
//
// Description: This function is called by PCI Bus Driver before installing
//              Protocol Interface for the input device.
//
// Input:       *PciDevice - Pointer to PCI Device Info structure.
//
// Output:      EFI_SUCCESS
//
// Notes:       All resource in the device had been assigned, but the command
//              register is disabled.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS NBProgramPciDevice (
    IN PCI_DEV_INFO             *PciDevice )
{

//####if ((PciDevice->Address.Addr.Bus == NB_BUS) && \
//####    (PciDevice->Address.Addr.Device == NB_DEV) && \
//####    (PciDevice->Address.Addr.Function == NB_FUN)) {
//####    // Do any porting if needed.
//####}

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBUpdatePciDeviceAttributes
//
// Description: This function is called by PCI Bus Driver, can be used to
//              the attributes of the PCI device.
//
// Input:       *PciDevice   - Pointer to PCI Device Info structure.
//              *Attributes  - Attributes bitmask which caller whants to
//                             change.
//              Capabilities - The PCI device supports Capabilityes
//              Set          - Specifies weathere to set or reset given
//                             "Attributes".
//
// Output:      EFI_SUCCESS
//
// Notes:       This routine may be invoked twice depend on the device type,
//              the first time is at BDS phase, the second is before
//              legacy boot.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS NBUpdatePciDeviceAttributes (
    IN PCI_DEV_INFO             *PciDevice,
    IN OUT UINT64               *Attributes,
    IN UINT64                   Capabilities,
    IN BOOLEAN                  Set )
{

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBGetTsegBase
//
// Description: Returns the base address of TSEG.
//
// Input:       None
//
// Output:      UINT32 - The Base Address of TSEG.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT32 NBGetTsegBase (VOID)
{
    return (READ_PCI32_NB(0xB8) & 0xFFF00000);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBEnableEmrr
//
// Description: Enable and lock CPU EMRR.
//
// Input:       UINT32 IedStart - Intel Enhanced Debug start.
//              UINT32 IedSize  - Intel Enhanced Debug size.
// 
// Output:      VOID
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID NBEnableEmrr(
     IN UINT32 IedStart, 
     IN UINT32 IedSize
)
{
    if ((*UNCORE_CR_MCSEG_MASK0_LOW & (1 << 11)) == 0 ) {
        *UNCORE_CR_MCSEG_BASE0 = (UINT32)IedStart + 0x200000;
        *UNCORE_CR_MCSEG_MASK0_LOW  = 0xffe00000;
        *UNCORE_CR_MCSEG_MASK0_HIGH = 0xff;
        *UNCORE_CR_MCSEG_MASK0_LOW |= (1 << 11); //Enable bit.
        *UNCORE_CR_MCSEG_MASK0_LOW |= (1 << 10); //Lock bit.
    }
    
    if ((*UNCORE_CR_MCSEG_MASK1_LOW & (1 << 11)) == 0 ) {
        *UNCORE_CR_MCSEG_BASE1 = (UINT32)IedStart + 0x300000;
        *UNCORE_CR_MCSEG_MASK1_LOW = 0xffe00000;
        *UNCORE_CR_MCSEG_MASK1_HIGH = 0xff;
        *UNCORE_CR_MCSEG_MASK1_LOW |= (1 << 11); //Enable bit.
        *UNCORE_CR_MCSEG_MASK1_LOW |= (1 << 10); //Lock bit.
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NbRuntimeShadowRamWrite
//
// Description: This function provides runtime interface to enable/disable
//              writing in E000-F000 segment
//
// Input:       IN BOOLEAN Enable - if TRUE - enable writing, if FALSE - disable
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID NbRuntimeShadowRamWrite(
    IN BOOLEAN Enable
)
{
    // Porting Required.
    static UINT8   F000Reg = 0xff; // 0x80
    static UINT8   E000Reg = 0xff; // 0x85
    static UINT8   E800Reg = 0xff; // 0x86

    if (Enable) {
        F000Reg = READ_PCI8_NB(R_SA_PAM0); // 0x80
        SET_PCI8_NB(R_SA_PAM0, 0x30);
        
        E000Reg = READ_PCI8_NB(R_SA_PAM5); // 0x85
        E800Reg = READ_PCI8_NB(R_SA_PAM6); // 0x86
        SET_PCI8_NB(R_SA_PAM5, 0x33);
        SET_PCI8_NB(R_SA_PAM6, 0x33);
    } else {
        if (F000Reg != 0xff) {
            WRITE_PCI8_NB(R_SA_PAM0, F000Reg); // 0x80
            WRITE_PCI8_NB(R_SA_PAM5, E000Reg); // 0x85
            WRITE_PCI8_NB(R_SA_PAM6, E800Reg); // 0x86
        }
    }

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: CheckPeiFvCopyToRam
//
// Description: Check system is cold or warm boot    
//
// Input:       PeiServices  - The PEI core services table.
//
// Output:      PeiFvCopyToRam - TRUE  for cold boot.
//                             - FALSE for warm boot.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN
CheckPeiFvCopyToRam (
  IN  EFI_PEI_SERVICES  **PeiServices
)
{

  UINT16        Buff16;
  BOOLEAN       PeiFvCopyToRam;


  Buff16 = READ_PCI16_SB(0xA2);

  if (((Buff16 & BIT5) != 0) && ((Buff16 & BIT7) != 0)) 
  {
    PeiFvCopyToRam = FALSE;

  } else {

    PeiFvCopyToRam = TRUE;
  }

  if (READ_MEM32_MCH(0x5D10) == 0)PeiFvCopyToRam = TRUE;

  return PeiFvCopyToRam;

}

//----------------------------------------------------------------------------
// Standard PCI Access Routines, No Porting Required.
//----------------------------------------------------------------------------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ReadPci8
//
// Description: This function reads an 8bits data from the specific PCI 
//              register. 
//
// Input:       Bus - PCI Bus number.
//              Dev - PCI Device number.
//              Fun - PCI Function number.
//              Reg - PCI Register number.
//
// Output:      UINT8
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8 ReadPci8 (
    IN UINT8        Bus, 
    IN UINT8        Dev,
    IN UINT8        Fun, 
    IN UINT16       Reg )
{
    if (Reg >= 0x100) {
        return MMIO_READ8(NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg));
    } else {
        IoWrite32(NB_PCICFG_SPACE_INDEX_REG, \
                BIT31 | (Bus << 16) | (Dev << 11) | (Fun << 8) | (Reg & 0xfc));
        return IoRead8(NB_PCICFG_SPACE_DATA_REG | (UINT8)(Reg & 3));
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ReadPci16
//
// Description: This function reads a 16bits data from the specific PCI 
//              register. 
//
// Input:       Bus - PCI Bus number.
//              Dev - PCI Device number.
//              Fun - PCI Function number.
//              Reg - PCI Register number.
//
// Output:      UINT16
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT16 ReadPci16 (
    IN UINT8        Bus, 
    IN UINT8        Dev,
    IN UINT8        Fun, 
    IN UINT16       Reg )
{
    if (Reg >= 0x100) {
        return MMIO_READ16(NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg));
    } else {
        IoWrite32(NB_PCICFG_SPACE_INDEX_REG, \
                BIT31 | (Bus << 16) | (Dev << 11) | (Fun << 8) | (Reg & 0xfc));
        return IoRead16(NB_PCICFG_SPACE_DATA_REG | (UINT8)(Reg & 2));
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ReadPci32
//
// Description: This function reads a 32bits data from the specific PCI 
//              register. 
//
// Input:       Bus - PCI Bus number.
//              Dev - PCI Device number.
//              Fun - PCI Function number.
//              Reg - PCI Register number.
//
// Output:      UINT32
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT32 ReadPci32 (
    IN UINT8        Bus, 
    IN UINT8        Dev,
    IN UINT8        Fun, 
    IN UINT16       Reg )
{
    if (Reg >= 0x100) {
        return MMIO_READ32(NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg));
    } else {
        IoWrite32(NB_PCICFG_SPACE_INDEX_REG, \
                BIT31 | (Bus << 16) | (Dev << 11) | (Fun << 8) | (Reg & 0xfc));
        return IoRead32(NB_PCICFG_SPACE_DATA_REG);
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WritePci8
//
// Description: This function writes an 8bits data to the specific PCI
//              register.
//
// Input:       Bus     - PCI Bus number.
//              Dev     - PCI Device number.
//              Fun     - PCI Function number.
//              Reg     - PCI Register number.
//              Value8  - An 8 Bits data will be written to the specific 
//                        PCI register.
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WritePci8 (
    IN UINT8        Bus, 
    IN UINT8        Dev,
    IN UINT8        Fun, 
    IN UINT16       Reg,
    IN UINT8        Value8 )
{
    if (Reg >= 0x100) {
        WriteMem8((UINT64)NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), Value8);
    } else {
        IoWrite32(NB_PCICFG_SPACE_INDEX_REG, \
                BIT31 | (Bus << 16) | (Dev << 11) | (Fun << 8) | (Reg & 0xfc));
        IoWrite8(NB_PCICFG_SPACE_DATA_REG | (UINT8)(Reg & 3), Value8);
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WritePci16
//
// Description: This function writes a 16bits data to the specific PCI
//              register.
//
// Input:       Bus     - PCI Bus number.
//              Dev     - PCI Device number.
//              Fun     - PCI Function number.
//              Reg     - PCI Register number.
//              Value16 - A 16 Bits data will be written to the specific 
//                        PCI register.
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WritePci16 (
    IN UINT8        Bus, 
    IN UINT8        Dev,
    IN UINT8        Fun, 
    IN UINT16       Reg,
    IN UINT16       Value16 )
{
    if (Reg >= 0x100) {
        WriteMem16((UINT64)NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), Value16);
    } else {
        IoWrite32(NB_PCICFG_SPACE_INDEX_REG, \
                BIT31 | (Bus << 16) | (Dev << 11) | (Fun << 8) | (Reg & 0xfc));
        IoWrite16(NB_PCICFG_SPACE_DATA_REG | (UINT8)(Reg & 2), Value16);
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WritePci32
//
// Description: This function writes a 32bits data to the specific PCI
//              register.
//
// Input:       Bus     - PCI Bus number.
//              Dev     - PCI Device number.
//              Fun     - PCI Function number.
//              Reg     - PCI Register number.
//              Value32 - A 32 Bits data will be written to the specific 
//                        PCI register.
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WritePci32 (
    IN UINT8        Bus, 
    IN UINT8        Dev,
    IN UINT8        Fun, 
    IN UINT16       Reg,
    IN UINT32       Value32 )
{
    if (Reg >= 0x100) {
        WriteMem32((UINT64)NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), Value32);
    } else {
        IoWrite32(NB_PCICFG_SPACE_INDEX_REG, \
                BIT31 | (Bus << 16) | (Dev << 11) | (Fun << 8) | (Reg & 0xfc));
        IoWrite32(NB_PCICFG_SPACE_DATA_REG, Value32);
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WritePci8S3
//
// Description: This function writes an 8bits data to the specific PCI 
//              register and Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Bus              - PCI Bus number.
//              Dev              - PCI Device number.
//              Fun              - PCI Function number.
//              Reg              - PCI Register number.
//              Value8           - An 8 Bits data will be written to the
//                                 specific PCI register and Boot Script.
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WritePci8S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT8                            Bus, 
    IN UINT8                            Dev,
    IN UINT8                            Fun, 
    IN UINT16                           Reg,
    IN UINT8                            Value8 )
{
    WritePci8(Bus, Dev, Fun, Reg, Value8);

    if (Reg >= 0x100) 
        BOOT_SCRIPT_S3_MEM_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint8, \
                                    NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    1, \
                                    &Value8 );
    else 
        BOOT_SCRIPT_S3_PCI_CONFIG_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint8, \
                                    NB_PCI_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    1, \
                                    &Value8 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WritePci16S3
//
// Description: This function writes a 16bits data to the specific PCI 
//              register and Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Bus              - PCI Bus number.
//              Dev              - PCI Device number.
//              Fun              - PCI Function number.
//              Reg              - PCI Register number.
//              Value16          - A 16 Bits data will be written to the
//                                 specific PCI register and Boot Script.
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WritePci16S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT8                            Bus, 
    IN UINT8                            Dev,
    IN UINT8                            Fun, 
    IN UINT16                           Reg,
    IN UINT16                           Value16 )
{

    WritePci16(Bus, Dev, Fun, Reg, Value16);

    if (Reg >= 0x100) 
        BOOT_SCRIPT_S3_MEM_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint16, \
                                    NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    1, \
                                    &Value16 );
    else 
        BOOT_SCRIPT_S3_PCI_CONFIG_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint16, \
                                    NB_PCI_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    1, \
                                    &Value16 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WritePci32S3
//
// Description: This function writes a 32bits data to the specific PCI 
//              register and Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Bus              - PCI Bus number.
//              Dev              - PCI Device number.
//              Fun              - PCI Function number.
//              Reg              - PCI Register number.
//              Value32          - A 32 Bits data will be written to the
//                                 specific PCI register and Boot Script.
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WritePci32S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT8                            Bus, 
    IN UINT8                            Dev,
    IN UINT8                            Fun, 
    IN UINT16                           Reg,
    IN UINT32                           Value32 )
{

    WritePci32(Bus, Dev, Fun, Reg, Value32);

    if (Reg >= 0x100) 
        BOOT_SCRIPT_S3_MEM_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint32, \
                                    NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    1, \
                                    &Value32 );
    else 
        BOOT_SCRIPT_S3_PCI_CONFIG_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint32, \
                                    NB_PCI_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    1, \
                                    &Value32 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwPci8S3
//
// Description: This function reads an 8bits data from the specific PCI
//              register, applies masks, and writes it back, also writes it
//              to Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Bus              - PCI Bus number.
//              Dev              - PCI Device number.
//              Fun              - PCI Function number.
//              Reg              - PCI Register number.
//              SetBit8          - Mask of bits to set (1 = Set)
//              ResetBit8        - Mask of bits to clear  (1 = clear)
//
// Output:      None.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwPci8S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT8                            Bus, 
    IN UINT8                            Dev,
    IN UINT8                            Fun, 
    IN UINT16                           Reg,
    IN UINT8                            SetBit8,
    IN UINT8                            ResetBit8 )
{
    RW_PCI8(Bus, Dev, Fun, Reg, SetBit8, ResetBit8);

    ResetBit8 = ~ResetBit8;

    if (Reg >= 0x100) 
        BOOT_SCRIPT_S3_MEM_READ_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint8, \
                                    NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    &SetBit8, \
                                    &ResetBit8 ); 
    else 
        BOOT_SCRIPT_S3_PCI_CONFIG_READ_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint8, \
                                    NB_PCI_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    &SetBit8, \
                                    &ResetBit8 );

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwPci16S3
//
// Description: This function reads a 16bits data from the specific PCI
//              register, applies masks, and writes it back, also writes it
//              to Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Bus              - PCI Bus number.
//              Dev              - PCI Device number.
//              Fun              - PCI Function number.
//              Reg              - PCI Register number.
//              SetBit16         - Mask of bits to set (1 = Set)
//              ResetBit16       - Mask of bits to clear  (1 = clear)
//
// Output:      None.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwPci16S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT8                            Bus, 
    IN UINT8                            Dev,
    IN UINT8                            Fun, 
    IN UINT16                           Reg,
    IN UINT16                           SetBit16,
    IN UINT16                           ResetBit16 )
{
    RW_PCI16(Bus, Dev, Fun, Reg, SetBit16, ResetBit16);

    ResetBit16 = ~ResetBit16;

    if (Reg >= 0x100) 
        BOOT_SCRIPT_S3_MEM_READ_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint16, \
                                    NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    &SetBit16, \
                                    &ResetBit16 ); 
    else 
        BOOT_SCRIPT_S3_PCI_CONFIG_READ_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint16, \
                                    NB_PCI_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    &SetBit16, \
                                    &ResetBit16 );

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwPci32S3
//
// Description: This function reads a 32bits data from the specific PCI
//              register, applies masks, and writes it back, also writes it
//              to Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Bus              - PCI Bus number.
//              Dev              - PCI Device number.
//              Fun              - PCI Function number.
//              Reg              - PCI Register number.
//              SetBit32         - Mask of bits to set (1 = Set)
//              ResetBit32       - Mask of bits to clear  (1 = clear)
//
// Output:      None.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwPci32S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT8                            Bus, 
    IN UINT8                            Dev,
    IN UINT8                            Fun, 
    IN UINT16                           Reg,
    IN UINT32                           SetBit32,
    IN UINT32                           ResetBit32 )
{
    RW_PCI32(Bus, Dev, Fun, Reg, SetBit32, ResetBit32);

    ResetBit32 = ~ResetBit32;

    if (Reg >= 0x100) 
        BOOT_SCRIPT_S3_MEM_READ_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint32, \
                                    NB_PCIE_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    &SetBit32, \
                                    &ResetBit32 ); 
    else 
        BOOT_SCRIPT_S3_PCI_CONFIG_READ_WRITE_MACRO( \
                                    mBootScriptSave, \
                                    EfiBootScriptWidthUint32, \
                                    NB_PCI_CFG_ADDRESS(Bus, Dev, Fun, Reg), \
                                    &SetBit32, \
                                    &ResetBit32 );

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WriteMem8
//
// Description: This function writes an 8bits data to the specific memory
//              (or MMIO) register.
//
// Input:       Address - An 64Bits Memory (or MMIO) address
//              Value8  - An 8 Bits data will be written to the specific 
//                        PCI register.
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WriteMem8 (
    IN UINT64       Address,
    IN UINT8        Value8 )
{
    MMIO_WRITE8(Address, Value8);
    Value8 = MMIO_READ8(Address);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WriteMem16
//
// Description: This function writes a 16bits data to the specific memory
//              (or MMIO) register.
//
// Input:       Address - A 64Bits Memory (or MMIO) address
//              Value16 - A 16 Bits data will be written to the specific 
//                        PCI register.
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WriteMem16 (
    IN UINT64       Address,
    IN UINT16       Value16 )
{
    MMIO_WRITE16(Address, Value16);
    Value16 = MMIO_READ16(Address);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WriteMem32
//
// Description: This function writes a 32bits data to the specific memory
//              (or MMIO) register.
//
// Input:       Address - A 64Bits Memory (or MMIO) address
//              Value32 - A 32 Bits data will be written to the specific 
//                        PCI register.
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WriteMem32 (
    IN UINT64       Address,
    IN UINT32       Value32 )
{
    MMIO_WRITE32(Address, Value32);
    Value32 = MMIO_READ32(Address);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwMem8
//
// Description: This function reads an 8bits data from a specific memory
//              (or MMIO) address, applies masks, and writes it back.
//
// Input:       Address - A 64Bits Memory (or MMIO) address
//              SetBit8 - Mask of bits to set (1 = Set)
//              ResetBit8 - Mask of bits to clear  (1 = clear)
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwMem8 (
    IN UINT64       Address,
    IN UINT8        SetBit8,
    IN UINT8        ResetBit8 )
{
    UINT8       Buffer8 = MMIO_READ8(Address) & ~ResetBit8 | SetBit8;

    WriteMem8(Address, Buffer8);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwMem16
//
// Description: This function reads a 16bits data from a specific memory
//              (or MMIO) address, applies masks, and writes it back.
//
// Input:       Address - A 64Bits Memory (or MMIO) address
//              SetBit16 - Mask of bits to set (1 = Set)
//              ResetBit16 - Mask of bits to clear  (1 = clear)
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwMem16 (
    IN UINT64       Address,
    IN UINT16       SetBit16,
    IN UINT16       ResetBit16 )
{
    UINT16      Buffer16 = MMIO_READ16(Address) & ~ResetBit16 | SetBit16;

    WriteMem16(Address, Buffer16);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwMem32
//
// Description: This function reads a 32bits data from a specific memory
//              (or MMIO) address, applies masks, and writes it back.
//
// Input:       Address - A 64Bits Memory (or MMIO) address
//              SetBit32 - Mask of bits to set (1 = Set)
//              ResetBit32 - Mask of bits to clear  (1 = clear)
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwMem32 (
    IN UINT64       Address,
    IN UINT32       SetBit32,
    IN UINT32       ResetBit32 )
{
    UINT32      Buffer32 = MMIO_READ32(Address) & ~ResetBit32 | SetBit32;

    WriteMem32(Address, Buffer32);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WriteMem8S3
//
// Description: This function writes an 8bits data to a specific memory
//              (or MMIO) address and Boot Script. 
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Address          - A 64Bits Memory (or MMIO) address
//              Value8           - An 8Bits data writes to the address.
//
// Output:      None.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WriteMem8S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT64                           Address,
    IN UINT8                            Value8 )
{

    WriteMem8(Address, Value8);

    BOOT_SCRIPT_S3_MEM_WRITE_MACRO( mBootScriptSave, \
                                    EfiBootScriptWidthUint8, \
                                    Address, \
                                    1, \
                                    &Value8 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WriteMem16S3
//
// Description: This function writes a 16bits data to a specific memory
//              (or MMIO) address and Boot Script. 
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Address          - A 64Bits Memory (or MMIO) address
//              Value16          - A 16Bits data writes to the address.
//
// Output:      None.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WriteMem16S3(
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT64                           Address,
    IN UINT16                           Value16 )
{
    WriteMem16(Address, Value16);

    BOOT_SCRIPT_S3_MEM_WRITE_MACRO( mBootScriptSave, \
                                    EfiBootScriptWidthUint16, \
                                    Address, \
                                    1, \
                                    &Value16 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   WriteMem32S3
//
// Description: This function writes a 32bits data to a specific memory
//              (or MMIO) address and Boot Script. 
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Address          - A 64Bits Memory (or MMIO) address
//              Value32          - A 32Bits data writes to the address.
//
// Output:      None.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID WriteMem32S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT64                           Address,
    IN UINT32                           Value32 )
{

    WriteMem32(Address, Value32);

    BOOT_SCRIPT_S3_MEM_WRITE_MACRO( mBootScriptSave, \
                                    EfiBootScriptWidthUint32, \
                                    Address, \
                                    1, \
                                    &Value32 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwMem8S3
//
// Description: This function reads an 8bits data from a specific memory
//              (or MMIO) address, applies masks, and writes it back, also
//              writes it to Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Address - A 64Bits Memory (or MMIO) address
//              SetBit8 - Mask of bits to set (1 = Set)
//              ResetBit8 - Mask of bits to clear  (1 = clear)
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwMem8S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT64                           Address,
    IN UINT8                            SetBit8,
    IN UINT8                            ResetBit8 )
{
    RwMem8(Address, SetBit8, ResetBit8);

    ResetBit8 = ~ResetBit8;
    BOOT_SCRIPT_S3_MEM_READ_WRITE_MACRO( mBootScriptSave, \
                                         EfiBootScriptWidthUint8, \
                                         Address, \
                                         &SetBit8, \
                                         &ResetBit8 ); 
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwMem16S3
//
// Description: This function reads a 16bits data from a specific memory
//              (or MMIO) address, applies masks, and writes it back, also
//              writes it to Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Address - A 64Bits Memory (or MMIO) address
//              SetBit16 - Mask of bits to set (1 = Set)
//              ResetBit16 - Mask of bits to clear  (1 = clear)
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwMem16S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT64                           Address,
    IN UINT16                           SetBit16,
    IN UINT16                           ResetBit16 )
{
    RwMem16(Address, SetBit16, ResetBit16);

    ResetBit16 = ~ResetBit16;
    BOOT_SCRIPT_S3_MEM_READ_WRITE_MACRO( mBootScriptSave, \
                                         EfiBootScriptWidthUint16, \
                                         Address, \
                                         &SetBit16, \
                                         &ResetBit16 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RwMem32S3
//
// Description: This function reads a 32bits data from a specific memory
//              (or MMIO) address, applies masks, and writes it back, also
//              writes it to Boot Script.
//
// Input:       *mBootScriptSave - Pointer to Boot Script Save Protocal
//              Address - A 64Bits Memory (or MMIO) address
//              SetBit32 - Mask of bits to set (1 = Set)
//              ResetBit32 - Mask of bits to clear  (1 = clear)
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID RwMem32S3 (
    IN AMI_S3_SAVE_PROTOCOL             *mBootScriptSave,
    IN UINT64                           Address,
    IN UINT32                           SetBit32,
    IN UINT32                           ResetBit32 )
{
    RwMem32(Address, SetBit32, ResetBit32);

    ResetBit32 = ~ResetBit32;
    BOOT_SCRIPT_S3_MEM_READ_WRITE_MACRO( mBootScriptSave, \
                                         EfiBootScriptWidthUint32, \
                                         Address, \
                                         &SetBit32, \
                                         &ResetBit32 );
}

//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************