summaryrefslogtreecommitdiff
path: root/Board/EM/Thunderbolt/TbtSmm/TbtSmm.c
blob: e60f02f404f6bc3384a2aa66d0012216e0ab40de (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
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2014, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/SOURCE/Modules/Thunderbolt/TbtSmm/TbtSmm.c 22    5/19/14 9:02a Barretlin $
//
// $Revision: 22 $
//
// $Date: 5/19/14 9:02a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/Thunderbolt/TbtSmm/TbtSmm.c $
// 
// 22    5/19/14 9:02a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Fix check error with cppcheck tool
// [Files]  		TbtSmm.c
// 
// 21    5/19/14 7:34a Barretlin
// [TAG]  		EIP165410
// [Category]  	New Feature
// [Description]  	Support Thunderbolt AIC at NB PCIE slot
// [Files]  		TbtPei.c TbtDxe.c TbtGpe.asl TbtSmm.c TbtOemBoard.c
// TbtOemLib.c TbtOemLib.h TbtSetup.sdl TbtSetup.sd TbtSetup.uni
// TbtSetupReset.c
// 
// 20    5/19/14 4:40a Barretlin
// [TAG]  		EIP167031
// [Category]  	Improvement
// [Description]  	Variable's attribute needs to be reviewed by
// Thunderbolt component driver
// [Files]  		TbtSmm.c
// 
// 19    2/19/14 2:57p Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	fix TBT host chip information record incorrect in SMM
// [Files]  		TbtSmm.c
// 
// 18    2/18/14 1:13a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	fix build error
// [Files]  		TbtSmm.c
// 
// 17    2/10/14 1:35p Barretlin
// [TAG]  		EIP152401
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	Implement Thunderbolt BIOS additions 1.9
// [Files]  		TbtSmm.c TbtGpe.asl
// 
// 16    2/10/14 12:17p Barretlin
// [TAG]  		EIP151867
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Build error when using PI 1.0
// [RootCause]  	GUID and Protocal do not be defined
// [Solution]  	Using generic GUID and defining correct protocal when
// using PI 1.0
// [Files]  		TbtSmm.c
// 
// 15    1/05/14 1:57p Barretlin
// [TAG]  		EIP N/A
// [Category]  	New Feature
// [Description]  	Support Thunderbolt feature Enable/Disable in run time
// Support dynamic Thunderbolt AIC location in run time
// [Files]  		TbtSmm.c
// 
// 14    12/25/13 6:06a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Using token to enable/disable double check TBT host
// router state in SxSMI/PowerButtonSMI
// [Files]  		TbtSmm.sdl TbtSmm.c
// 
// 13    12/24/13 11:35a Barretlin
// [TAG]  		EIP148198
// [Category]  	Spec Update
// [Severity]  	Normal
// [Description]  	Updating for Thunderbolt BIOS additions - rev.1.8
// [Files]  		TbtSmm.c
// 
// 12    12/24/13 11:25a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Fix thunderbolt device enumerating fail when
// TBT_PCIBUS_SKIP is disable
// [Files]  		TbtSmm.c
// 
// 11    6/21/13 7:42a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Fix build error with non Intel RC project
// [Files]  		TbtSmm.c
// 
// 10    6/20/13 3:38a Barretlin
// [TAG]  		EIP None
// [Category]  	Improvement
// [Description]  	make sure RR and FR handshake work in sleep smi
// [Files]  		TbtSmm.c
// 
// 9     6/19/13 10:34a Barretlin
// [TAG]  		EIP126581
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	Follow Thunderbolt RR/FR BIOS Spec rev 1.0 to add Sx
// entry flow for Add-in Card
// [Files]  		TbtSmm.c
// 
// 8     6/18/13 1:15p Barretlin
// [TAG]  		EIP None
// [Category]  	Improvement
// [Description]  	change TBWakeupSupport name
// [Files]  		TbtSmm.c
// 
// 7     6/16/13 11:05p Barretlin
// [TAG]  		EIP126581
// [Category]  	Spec Update
// [Severity]  	Important
// [Description]  	Update Intel Thunderbolt sample code to rev. 1.7
// [Files]  		TbtSmm.c
// 
// 6     5/27/13 9:04a Barretlin
// [TAG]  		EIP124914
// [Category]  	New Feature
// [Description]  	Support Falcon Ridge chip
// [Files]  		TbtSmm.c
// 
// 5     4/23/13 3:25a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Fix programming error
// [Files]  		TbtSmm.c
// 
// 4     4/10/13 2:09p Barretlin
// [TAG]  		EIP120580
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	Update Intel Thunderbolt sample code to rev. 1.6
// [Files]  		TbtSmm.c
// 
// 3     4/02/13 11:41p Barretlin
// [TAG]  		EIP N/A
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Thunderbolt host driver behavior is incorrect in special
// case
// [RootCause]  	Host router state is not updating
// [Solution]  	Double check Presence Detect State bit on PCIE root port
// in sleep SMI
// [Files]  		TbtSmm.c
// 
// 2     1/25/13 10:08a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Bug Fix
// [Severity]  	Critical
// [Symptom]  	IO resource will be changed by win8
// [RootCause]  	 OpROM address error when assigning OpROM location to
// PCIE config register
// [Solution]  	according device type to fill different OpROM address
// location
// [Files]  		TbtSmm.c
// 
// 1     1/10/13 4:56a Barretlin
// Change SS path and update module for Thunderbolt Spec 1.6 for Cactus
// Ridge chip and Thunderbolt Spec 0.5 for Redwood Ridge chip
// 
// 30    12/13/12 12:12a Barretlin
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Following Spec remove RR handshake with GO2SX command
// in power button and Sx callback
// [Files]  		TbtSmm.c
// 
// 29    12/12/12 3:32a Barretlin
// [TAG]  		EIP None
// [Category]  	Improvement
// [Description]  	Workaround for synchronizing cache line size of
// Thunderbolt
// [Files]  		TbtSmm.c
// 
// 28    12/12/12 3:18a Barretlin
// [TAG]  		EIP108272
// [Category]  	Spec Update
// [Severity]  	Important
// [Description]  	Update to Spec 1.4 to support Redwood Ridge chip
// [Files]  		TbtPei.c TbtSmm.c TbtDxe.c TbtDxe.sdl TbtGpe.asl
// TbtOemBoard.c TbtOemBoard.h TbtOemLib.c TbtOemLib.h
// 
// 27    10/28/12 11:44p Barretlin
// [TAG]  		EIP104870
// [Category]  	Spec Update
// [Severity]  	Important
// [Description]  	Change wake up flow for Spec 1.2 and Spec 1.3
// [Files]  		TbtPei.c TbtSmm.c TbtOemBoard.c TbtOemBoard.h
// 
// 26    10/28/12 10:58p Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Change GPIO routing for SharkBay ULT platform
// [Files]  		TbtDxe.c TbtGpe.asl TbtDxeLib.c TbtDxeLib.h TbtSmm.c
// 
// 25    10/27/12 6:29a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Create new setup item for thunderbolt POC handling
// [Files]  		TbtPei.c TbtSmm.c TbtOemboard.c TbtOemboard.h TbtSetup.sdl
// TbtSetup.sd Tbtsetup.uni
// 
// 24    10/04/12 11:53a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Use global definition for genernic
// [Files]  		TbtSmm.c TbtSmm.mak
// 
// 23    9/22/12 10:49a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Change IO resource workaround behavior, docking device
// is not support in 4C 2ports case.
// [Files]  		TbtSmm.c
// 
// 22    9/06/12 1:34a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	fix programming error
// [Files]  		TbtSmm.c
// 
// 21    9/03/12 6:27a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Change driver type and dependence for SharkBay platform
// [Files]  		TbtSmm.mak TbtSmm.c TbtSmm.dxs
// 
// 20    9/01/12 4:20a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Fix Intel sample code bug
// [Files]  		TbtSmm.c
// 
// 19    8/20/12 5:22a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Fix IO resource workaround broken in 4C 2port case
// [Files]  		TbtSmm.c TbtOemBoard.c TbtOemBoard.h TbtSetup.sdl
// TbtSetup.sd TbtSetup.uni
// 
// 18    8/17/12 9:24a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Add IO resource workaround for Thunderbolt Spec1.1
// Because new spec has removed IO resource for Thunderbolt device
// [Files]  		TbtSmm.c TbtOemBoard.c TbtOemBoard.h TbtSetup.sdl
// TbtSetup.sd TbtSetup.uni
// 
// 17    8/17/12 9:19a Barretlin
// [TAG]  		EIP98269
// [Category]  	Spec Update
// [Severity]  	Normal
// [Description]  	Update Thunderbolt specification to version 1.1 and
// sample code to Rev. 1.4
// [Files]  		TbtSmm.c
// 
// 16    7/31/12 5:42a Barretlin
// [TAG]  		EIP96350
// [Category]  	Spec Update
// [Severity]  	Critical
// [Description]  	Updated Thunderbolt specification to version 1.00
// [Files]  		TbtDxe.c TbtSmm.c TbtOemBoard.c TbtOemBoard.h
// TbtOemBoard.sdl TbtSetup.sd TbtSetup.uni
// 
// 15    7/24/12 11:50p Barretlin
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Adding power button event
// [Files]  		TbtSmm.c
// 
// 14    5/29/12 5:23a Barretlin
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Removing registered S1callback function when system
// entering S1state
// [Files]  		TbtSmm.c
// 
// 13    5/29/12 5:17a Barretlin
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	clean redundancy code in surprise-removal workaround
// [Files]  		TbtSmm.c
// 
// 12    5/22/12 9:54a Barretlin
// [TAG]  		EIP90650
// [Category]  	Spec Update
// [Description]  	Specificatoin Update 0.94 - The default value of
// OPTIONAL workaround for devices that don't support surprise-removal
// should be disable.
// 
// [Files]  		TbtSmm.c TbtOemBoard.c TbtOemBoard.h
// TbtSetup.sdl TbtSetup.sd TbtSetup.uni
// 
// 11    5/21/12 2:25a Barretlin
// [TAG]  		EIP90003
// [Category]  	Improvement
// [Description]  	If TBT devices with option rom enabled, system maybe
// cannot boot to OS.
// [Files]  		TbtDxe.c TbtSmm.c TbtOemBoard.sdl
// 
// 10    5/10/12 6:40a Barretlin
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Fix Programming error at specification 0.92 update
// [Files]  		TbtSmm.c
// 
// 9     5/07/12 6:40a Barretlin
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Add three setup items for debug
// [Files]  		TbtDxe.c
// TbtGpe.asl
// TbtSmm.c
// TbtSetup.sd
// TbtSetup.uni
// TbtOemBoard.c
// TbtOemBoard.h
// 
// 8     5/06/12 1:47a Barretlin
// [TAG]  		None
// [Category]  	Bug Fix
// [Symptom]  	Thunderbolt function is broken in windows 8 and sometime
// EP#6 shows yellow  mark in windows device manager
// [RootCause]  	SCI is signaled incorrectly
// [Solution]  	enable GPIO routing
// [Files]  		TbtSmm.c
// 
// 7     5/05/12 9:20a Barretlin
// [TAG]  		EIP89207
// [Category]  	Spec Update
// [Description]  	OPTIONAL workaround for devices that don't support
// surprise-removal
// [Files]  		TbtSmm.c
// 
// 6     4/14/12 4:17a Barretlin
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Fix Programming error at specification 0.91 update
// [Files]  		TbtSmm.c
// 
// 5     3/05/12 1:18a Barretlin
// [TAG]  		EIP83266
// [Category]  	Spec Update
// [Description]  	Specificatoin Update 0.90
// [Files]  		TbtSetup.sdl
// TbtSetup.sd
// TbtSetup.uni
// TbtSetup.cif
// TbtOemBoard.h
// TbtOemLib.c
// TbtOemLib.h
// TbtSmm.c
// TbtPei..
// 
// 3     2/20/12 4:44a Wesleychen
// Add new SDL token "TBT_SWSMI_DELAY" for debug.
// 
// 2     2/20/12 12:04a Wesleychen
//  - Add SMMSxDispatch.
//  - Rewrite ThunderboltSwSmiCallback().
// 
// 1     12/08/11 4:09a Wesleychen
// Thunderbolt eModule initially releases.
// 
//*************************************************************************
#include <Token.h>
#include <Setup.h>
#include <AmiDxeLib.h>
#include <AMICSPLIBInc.h>
#include <AmiCspLib.h>
#include <TbtOemBoard.h>
#if defined TBT_INTEL_RC_CONFIG && TBT_INTEL_RC_CONFIG == 1
#include <PchAccess.h>
#endif
#include <Protocol/Variable.h>

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
#include <Protocol\SmmBase2.h>
#include <Protocol\SmmSwDispatch2.h>
#include <Protocol\SmmSxDispatch2.h>
#include <Protocol\SmmPowerButtonDispatch2.h>
#else
#include <Protocol\SmmBase.h>
#include <Protocol\SmmSwDispatch.h>
#include <Protocol\SmmSxDispatch.h>
#include <Protocol\SmmPowerButtonDispatch.h>
#endif

//---------------------------------------------------------------------------
// Constant, Macro and Type Definition(s)
//---------------------------------------------------------------------------
// Constant Definition(s)

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
#define AMI_SMM_BASE_PROTOCOL                  EFI_SMM_BASE2_PROTOCOL
#define AMI_SMM_SW_DISPATCH_PROTOCOL           EFI_SMM_SW_DISPATCH2_PROTOCOL
#define AMI_SMM_SW_DISPATCH_CONTEXT            EFI_SMM_SW_REGISTER_CONTEXT
#define AMI_SMM_SX_DISPATCH_PROTOCOL           EFI_SMM_SX_DISPATCH2_PROTOCOL
#define AMI_SMM_SX_DISPATCH_CONTEXT            EFI_SMM_SX_REGISTER_CONTEXT
#define AMI_SMM_POWER_BUTTON_DISPATCH_PROTOCOL EFI_SMM_POWER_BUTTON_DISPATCH2_PROTOCOL
#define AMI_SMM_POWER_BUTTON_DISPATCH_CONTEXT  EFI_SMM_POWER_BUTTON_REGISTER_CONTEXT
#define SMM_CHILD_DISPATCH_SUCCESS             EFI_SUCCESS
#define SMM_CHILD_DISPATCH_TIMEOUT             EFI_TIMEOUT
#define SMM_CHILD_DISPATCH_NO_MEDIA            EFI_NO_MEDIA
#define SMM_CHILD_DISPATCH_UNSUPPORTED         EFI_UNSUPPORTED
#else
#define AMI_SMM_BASE_PROTOCOL                  EFI_SMM_BASE_PROTOCOL
#define AMI_SMM_SW_DISPATCH_PROTOCOL           EFI_SMM_SW_DISPATCH_PROTOCOL
#define AMI_SMM_SW_DISPATCH_CONTEXT            EFI_SMM_SW_DISPATCH_CONTEXT
#define AMI_SMM_SX_DISPATCH_PROTOCOL           EFI_SMM_SX_DISPATCH_PROTOCOL
#define AMI_SMM_SX_DISPATCH_CONTEXT            EFI_SMM_SX_DISPATCH_CONTEXT
#define AMI_SMM_POWER_BUTTON_DISPATCH_PROTOCOL EFI_SMM_POWER_BUTTON_DISPATCH_PROTOCOL
#define AMI_SMM_POWER_BUTTON_DISPATCH_CONTEXT  EFI_SMM_POWER_BUTTON_DISPATCH_CONTEXT
#define SMM_CHILD_DISPATCH_SUCCESS
#define SMM_CHILD_DISPATCH_TIMEOUT
#define SMM_CHILD_DISPATCH_NO_MEDIA
#define SMM_CHILD_DISPATCH_UNSUPPORTED
#endif

#ifndef EFI_PCI_CAPABILITY_ID_PMI
#define EFI_PCI_CAPABILITY_ID_PMI    0x01
#endif

#ifndef EFI_PCI_CAPABILITY_ID_PCIEXP
#define EFI_PCI_CAPABILITY_ID_PCIEXP 0x10
#endif

#ifdef SMI_PROGRESS_CODE
#undef SMI_PROGRESS_CODE
#endif
#define SMI_PROGRESS_CODE(Data)     IoWrite8(0x80, Data)

#define SMM_THUNDERBOLT_CALL        TBT_SWSMI_VALUE

#define MAX_TBT_DEPTH                6

#define P2P_BRIDGE                  (((PCI_CL_BRIDGE) << 8) | (PCI_CL_BRIDGE_SCL_P2P))

#define BAR_ALIGN(v, a)             ((((v) - 1) | (a)) + 1)

#define CMD_BUS_MASTER              BIT2
#define CMD_BM_IO                   (CMD_BUS_MASTER | BIT0)
#define CMD_BM_MEM                  (CMD_BUS_MASTER | BIT1)
#define CMD_BM_MEM_IO               (CMD_BUS_MASTER | BIT1 | BIT0)

//#define DEF_RES_IO_PER_DEV          4    //new setup item
//#define DEF_RES_MEM_PER_DEV         32   //new setup item
//#define DEF_RES_PMEM_PER_DEV        32   //new setup item
#define DOCK_BUSSES                 8

#define DISBL_IO_REG1C              0x01F1
#define DISBL_MEM32_REG20           0x0000FFF0
#define DISBL_PMEM_REG24            0x0001FFF1

// Light Ridge HR device ID
#define LR_HR     0x1513
// Eagle Ridge HR device IDs
#define ER_SFF_HR 0x151A
#define ER_HR     0x151B
// Cactus Ridge HR device IDs
#define CR_HR_2C  0x1548
#define CR_HR_4C  0x1547
// Redwood Ridge HR device IDs
#define RR_HR_2C  0x1567
#define RR_HR_4C  0x1569
// Falcon Ridge HR device IDs
#define FR_HR_2C  0x156B
#define FR_HR_4C  0x156D
// Win Ridge HR device ID
#define WR_HR_2C  0x157E

#define count(x)                    (sizeof(x) / sizeof((x)[0]))

//
// Common Memory mapped Pci access macros -----------------------------------
//
#define SmiPciAddress( Segment, Bus, Device, Function, Register ) \
  ( (UINTN)PCIEX_BASE_ADDRESS + \
    (UINTN)(Bus << 20) + \
    (UINTN)(Device << 15) + \
    (UINTN)(Function << 12) + \
    (UINTN)(Register) \
  )
//
// UINT32
//
#define SmiPci32Ptr( Segment, Bus, Device, Function, Register ) \
  ( (volatile UINT32 *)SmiPciAddress( Segment, Bus, Device, Function, Register ) )

#define SmiPci32( Segment, Bus, Device, Function, Register ) \
  *SmiPci32Ptr( Segment, Bus, Device, Function, Register )

#define SmiPci32Or( Segment, Bus, Device, Function, Register, OrData ) \
  SmiPci32( Segment, Bus, Device, Function, Register ) = \
    (UINT32) ( \
      SmiPci32( Segment, Bus, Device, Function, Register ) | \
      (UINT32)(OrData) \
    )

#define SmiPci32And( Segment, Bus, Device, Function, Register, AndData ) \
  SmiPci32( Segment, Bus, Device, Function, Register ) = \
    (UINT32) ( \
      SmiPci32( Segment, Bus, Device, Function, Register ) & \
      (UINT32)(AndData) \
    )

#define SmiPci32AndThenOr( Segment, Bus, Device, Function, Register, AndData, OrData ) \
  SmiPci32( Segment, Bus, Device, Function, Register ) = \
    (UINT32) ( \
      ( SmiPci32( Segment, Bus, Device, Function, Register ) & \
          (UINT32)(AndData) \
      ) | \
      (UINT32)(OrData) \
    )
//
// UINT16
//
#define SmiPci16Ptr( Segment, Bus, Device, Function, Register ) \
  ( (volatile UINT16 *)SmiPciAddress( Segment, Bus, Device, Function, Register ) )

#define SmiPci16( Segment, Bus, Device, Function, Register ) \
  *SmiPci16Ptr( Segment, Bus, Device, Function, Register )

#define SmiPci16Or( Segment, Bus, Device, Function, Register, OrData ) \
  SmiPci16( Segment, Bus, Device, Function, Register ) = \
    (UINT16) ( \
      SmiPci16( Segment, Bus, Device, Function, Register ) | \
      (UINT16)(OrData) \
    )

#define SmiPci16And( Segment, Bus, Device, Function, Register, AndData ) \
  SmiPci16( Segment, Bus, Device, Function, Register ) = \
    (UINT16) ( \
      SmiPci16( Segment, Bus, Device, Function, Register ) & \
      (UINT16)(AndData) \
    )

#define SmiPci16AndThenOr( Segment, Bus, Device, Function, Register, AndData, OrData ) \
  SmiPci16( Segment, Bus, Device, Function, Register ) = \
    (UINT16) ( \
      ( SmiPci16( Segment, Bus, Device, Function, Register ) & \
          (UINT16)(AndData) \
      ) | \
      (UINT16)(OrData) \
    )
//
// UINT8
//
#define SmiPci8Ptr( Segment, Bus, Device, Function, Register ) \
  ( (volatile UINT8 *)SmiPciAddress( Segment, Bus, Device, Function, Register ) )

#define SmiPci8( Segment, Bus, Device, Function, Register ) \
  *SmiPci8Ptr( Segment, Bus, Device, Function, Register )

#define SmiPci8Or( Segment, Bus, Device, Function, Register, OrData ) \
  SmiPci8( Segment, Bus, Device, Function, Register ) = \
    (UINT8) ( \
      SmiPci8( Segment, Bus, Device, Function, Register ) | \
      (UINT8)(OrData) \
    )

#define SmiPci8And( Segment, Bus, Device, Function, Register, AndData ) \
  SmiPci8( Segment, Bus, Device, Function, Register ) = \
    (UINT8) ( \
      SmiPci8( Segment, Bus, Device, Function, Register ) & \
      (UINT8)(AndData) \
    )

#define SmiPci8AndThenOr( Segment, Bus, Device, Function, Register, AndData, OrData ) \
  SmiPci8( Segment, Bus, Device, Function, Register ) = \
    (UINT8) ( \
      ( SmiPci8( Segment, Bus, Device, Function, Register ) & \
          (UINT8)(AndData) \
        ) | \
      (UINT8)(OrData) \
    )

// Type Definition(s)

// Function Prototype(s)

//---------------------------------------------------------------------------
// Variable and External Declaration(s)
//---------------------------------------------------------------------------
// Variable Declaration(s)
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_SMM_BASE2_PROTOCOL   *gSmmBase2;
EFI_SMM_SYSTEM_TABLE2    *pSmst2;
#endif

UINT16  HostDeviceId;
UINT8   gCacheLineSize;
UINT8   gTbtBus;
UINT8   gTbtDev;
UINT8   gTbtFun;
UINT16  gReserveMemoryPerSlot;
UINT16  gReservePMemoryPerSlot;
UINT8   gReserveIOPerSlot;
UINT8   gTbtHotPlugEvent;
UINT8   gTbtNVMversion;
#if !defined TBT_PCIBUS_SKIP || TBT_PCIBUS_SKIP == 0
UINT8   IsFirstEnterFlag     = 1;
#endif
UINT32  AmiTbtHrStatusAttribute = 0;
BOOLEAN gTbtEnable           = FALSE;
BOOLEAN gTbtWakeupSupport    = FALSE;
BOOLEAN gTbtAICSupport       = FALSE;
BOOLEAN gTbtHandlePOC        = FALSE;
BOOLEAN gTbtIOresourceEnable = FALSE;

// GUID Definition(s)
EFI_GUID  TbtHRStatusGuid    = AMI_TBT_HR_STATUS_GUID;

// Protocol Definition(s)

// External Declaration(s)

// Function Definition(s)

//----------------------------------------------------------------------------

#if defined(EFI64) || defined(EFIx64)

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SmiDivU64x32
//
// Description: This routine allows a 64 bit value to be divided with a 32 bit
//              value returns  64bit result and the Remainder.
//
// Input:       UINT64  Dividend
//              UINT64  Divisor
//
// Output:      UINTN   *Remainder OPTIONAL      
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
static UINT64 SmiDivU64x32 (
    IN UINT64   Dividend,
    IN UINTN    Divisor,
    OUT UINTN   *Remainder OPTIONAL
)
{
    UINT64  Result = Dividend/Divisor;
    if (Remainder) *Remainder=Dividend%Divisor;
    return Result;
}

#else
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SmiDivU64x32
//
// Description: This routine allows a 64 bit value to be divided with a 32 bit
//              value returns 64bit result and the Remainder.
//
// Input:       UINT64  Dividend
//              UINT64  Divisor
//
// Output:      UINTN   *Remainder OPTIONAL      
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
static UINT64 SmiDivU64x32 (
    IN UINT64   Dividend,
    IN UINTN    Divisor,    //Can only be 31 bits.
    OUT UINTN   *Remainder OPTIONAL
)
{
    UINT64  Result;
    UINT32  Rem;
    _asm
    {
        mov     eax, dword ptr Dividend[0]
        mov     edx, dword ptr Dividend[4]
        mov     esi, Divisor
        xor     edi, edi                    ;/// Remainder
        mov     ecx, 64                     ;/// 64 bits
Div64_loop:
        shl     eax, 1                      ;/// Shift dividend left. This clears bit 0.
        rcl     edx, 1    
        rcl     edi, 1                      ;/// Shift remainder left. Bit 0 = previous dividend bit 63.

        cmp     edi, esi                    ;/// If Rem >= Divisor, do not adjust
        cmc                                 ;/// else adjust dividend and subtract divisor.
        sbb     ebx, ebx                    ;/// if Rem >= Divisor, ebx = 0, else ebx = -1.
        sub     eax, ebx                    ;/// if adjust, bit 0 of dividend = 1
        and     ebx, esi                    ;/// if adjust, ebx = Divisor, else ebx = 0. 
        sub     edi, ebx                    ;/// if adjust, subtract divisor from remainder.
        loop    Div64_loop

        mov     dword ptr Result[0], eax
        mov     dword ptr Result[4], edx
        mov     Rem, edi
    }

    if (Remainder) *Remainder = Rem;

    return Result;
}

#endif

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   SmiStall
//
// Description: Stalls for the Required Amount of MicroSeconds
//
// Parameters:  Usec - UINTN
//
// Returns:     None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

VOID SmiStall (
    UINTN   Usec                           
)
{
    UINTN   Counter, i;
    UINT32  Data32, PrevData;
    UINTN   Remainder;

    Counter = (UINTN)SmiDivU64x32 ((Usec * 10), 3, &Remainder);

    if (Remainder != 0) {
        Counter++;
    }

    //
    // Call WaitForTick for Counter + 1 ticks to try to guarantee Counter tick
    // periods, thus attempting to ensure Microseconds of stall time.
    //
    if (Counter != 0) {

        PrevData = IoRead32(PM_BASE_ADDRESS + 8);
        for (i = 0; i < Counter; ) {
            Data32 = IoRead32(PM_BASE_ADDRESS + 8);    
            if (Data32 < PrevData) {        // Reset if there is a overlap
                PrevData=Data32;
                continue;
            }
            i += (Data32 - PrevData);        
            PrevData = Data32;
        }
    }
    return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   IsTBTDevice
//
// Description: Check device is Thunderbolt device or not
//
// Parameters:  UINT16 - DeviceID
//
// Returns:     BOOLEAN - TRUE
//                        FALSE
//----------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsTBTDevice(
    IN UINT16       DeviceID )
{
    switch(DeviceID)
    {
    case 0x1513:
    case 0x151A:
    case 0x151B:
    case 0x1547: // Cactus Ridge 4C
    case 0x1548: // Cactus Ridge 2C
    case 0x1567: // Redwood Ridge 2C
    case 0x1569: // Redwood Ridge 4C
    case 0x156B: // Falcon Ridge 2C
    case 0x156D: // Falcon Ridge 4C
        return TRUE;
    }
  return FALSE;
}//IsTBTDevice

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   TbtHotplugPinSciRouting
//
// Description: Find the Offset to a given Capabilities 
//              ID CAPID list:
//                  0x01 = PCI Power Management Interface
//                  0x04 = Slot Identification
//                  0x05 = MSI Capability
//                  0x10 = PCI Express Capability
//
// Parameters:  UINT8 Bus     - Pci Bus Number
//              UINT8 Dev     - Pci Device Number
//              UINT8 Fun     - Pci Function Number
//              UINT8 CapId   - CAPID to search for
//
// Returns: UINT8    0        - CAPID not found
//          UINT8    Other    - CAPID found, Offset of desired CAPID
//----------------------------------------------------------------------
//<AMI_PHDR_END>
UINT8 PcieFindCapId (
    IN UINT8    Bus,
    IN UINT8    Dev,
    IN UINT8    Fun,
    IN UINT8    CapId)
{
    UINT8 CapHeader;
    
    CapHeader = SmiPci8(0x00, Bus, Dev, Fun, 0x34);
    if (CapHeader == 0xFF) {
        return 0;
    }
    while (CapHeader != 0) {
        // Bottom 2 bits of the pointers are reserved per PCI Local Bus Spec 2.2
        CapHeader &= ~(BIT1 | BIT0);
        
        // Search for desired CapID
        if (SmiPci8 (0x00, Bus, Dev, Fun, CapHeader) == CapId) {
            return CapHeader;
        }
        
        CapHeader = SmiPci8 (0x00, Bus, Dev, Fun, (CapHeader + 1));
    } // while loop
    return 0;
}

BOOLEAN
IsTBTHostRouter(
    IN        UINT16  DeviceID
)
{
    switch(DeviceID)
    {
	    case LR_HR:
	    case ER_SFF_HR:
	    case ER_HR:
	    case CR_HR_4C:
	    case CR_HR_2C:
	    case RR_HR_2C: 
	    case RR_HR_4C:
	    case FR_HR_2C:
	    case FR_HR_4C:
	    case WR_HR_2C:
            return TRUE;
    }
  return FALSE;
}//IsTBTHostRouter

typedef struct _PortInfo
{
	UINT8      IOBase;
	UINT8      IOLimit;
	UINT16     MemBase;
	UINT16     MemLimit;
    UINT64     PMemBase64;
    UINT64     PMemLimit64;
	UINT8      BusNumLimit;
	UINT8      ConfedEP;
} PortInfo;

typedef struct _MEM_REGS
{
    UINT32 Base;
    UINT32 Limit;
} MEM_REGS;

typedef struct _PMEM_REGS
{
    UINT64 Base64;
    UINT64 Limit64;
} PMEM_REGS;

typedef struct _IO_REGS
{
    UINT16 Base;
    UINT16 Limit;
} IO_REGS;

VOID
PortInfoInit(
    IN    OUT    PortInfo *pi
)
{
    pi->BusNumLimit = 4;
}//PortInfoInit

BOOLEAN isLegacyDevice = FALSE;

#define MEM_PER_SLOT  gReserveMemoryPerSlot
#define PMEM_PER_SLOT gReservePMemoryPerSlot

UINT16
MemPerSlot(
	IN		UINT16 currUsage
)
{
	if(currUsage == 0)
		return 0;
	
	if(currUsage <= 16)
		return 16;
	if(currUsage <= 64)
		return 64;
	if(currUsage <= 128)
		return 128;
	if(currUsage <= 256)
		return 256;
	if(currUsage <= 512)
		return 512;
	if(currUsage <= 1024)
		return 1024;

	return currUsage;
}//MemPerSlot

UINT64
PMemPerSlot(
	IN		UINT64 currUsage
)
{
	if(currUsage == 0)
		return 0;
	
	if(currUsage <= 1024ULL)
		return 1024ULL;
	if(currUsage <= 4096ULL)
		return 4096ULL;

	return currUsage;
}//PMemPerSlot

VOID
SetPHYPortResources(
    IN        UINT8 Bus, 
    IN        UINT8 Dev, 
    IN        UINT8 SubBus, 
	IN		  INT8  Depth, 
    IN        PortInfo* CurrentPi,
    IN    OUT PortInfo* pi
)
{
    UINT8    Cmd = CMD_BUS_MASTER;
	UINT16	 deltaMEM;
    UINT64   deltaPMEM;
	UINT8    deltaIO;

    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_SUBUS) = SubBus;
    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_CMD) = Cmd;

	deltaIO = pi->IOBase - CurrentPi->IOBase;
	if(Depth >= 0 && gReserveIOPerSlot && deltaIO < gReserveIOPerSlot)
		pi->IOBase += gReserveIOPerSlot - deltaIO;

    if (pi->IOBase > CurrentPi->IOBase && (pi->IOBase - 0x10) <= pi->IOLimit)
    {
        SmiPci8  (0x00, Bus, Dev, 0x00, PCI_IOBASE) = CurrentPi->IOBase;
        SmiPci8  (0x00, Bus, Dev, 0x00, PCI_IOLIMIT) = pi->IOBase - 0x10;
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_IOBASE_U) = 0x00000000;
        Cmd |= CMD_BM_IO;
    }
    else
    {
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_IOBASE) = DISBL_IO_REG1C;
        pi->IOBase = CurrentPi->IOBase;
    }

	deltaMEM = pi->MemBase - CurrentPi->MemBase;

	if(isLegacyDevice)
	{
		if(Depth >= 0 && gReserveMemoryPerSlot && deltaMEM < MEM_PER_SLOT)
			pi->MemBase += MEM_PER_SLOT - deltaMEM;
	}
	else
	{
		if(deltaMEM < MemPerSlot(deltaMEM))
			pi->MemBase += MemPerSlot(deltaMEM) - deltaMEM;
	}

    if (pi->MemBase > CurrentPi->MemBase && (pi->MemBase - 0x10) <= pi->MemLimit)
    {
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_MEMBASE) = CurrentPi->MemBase;
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_MEMLIMIT) = pi->MemBase - 0x10;
        Cmd |= CMD_BM_MEM;
    }
    else
    {
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_MEMBASE) = DISBL_MEM32_REG20;
        pi->MemBase = CurrentPi->MemBase;
    }

    deltaPMEM = pi->PMemBase64 - CurrentPi->PMemBase64;
	if(isLegacyDevice)
	{
		if(Depth >= 0 && gReservePMemoryPerSlot && deltaPMEM < PMEM_PER_SLOT)
			pi->PMemBase64 += PMEM_PER_SLOT - deltaPMEM;
	}
	else
	{
		if(deltaPMEM < PMemPerSlot(deltaPMEM))
			pi->PMemBase64 += PMemPerSlot(deltaPMEM) - deltaPMEM;
	}

    if (pi->PMemBase64 > CurrentPi->PMemBase64 && (pi->PMemBase64 - 0x10) <= pi->PMemLimit64)
    {
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE) = (UINT16)(CurrentPi->PMemBase64 & 0xFFFF);
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT) = (UINT16)((pi->PMemBase64 - 0x10) & 0xFFFF);
		SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE_U) = (UINT32)(CurrentPi->PMemBase64 >> 16);
		SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT_U) = (UINT32)((pi->PMemBase64 - 0x10) >> 16);
        Cmd |= CMD_BM_MEM;
    }
    else
    {
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE) = DISBL_PMEM_REG24;
		SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE_U) = 0;
		SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT_U) = 0;
        pi->PMemBase64 = CurrentPi->PMemBase64;
    }

    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_CMD) = Cmd;
    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_CLS) = gCacheLineSize;
}//SetPHYPortResources

UINT32
SaveSetGetRestoreBAR(
    IN        volatile UINT32* BAR
)
{
    UINT32  BarReq;
    UINT32  OrigBar = *BAR;// Save BAR
    *BAR = 0xFFFFFFFF;       // Set BAR
    BarReq = *BAR;           // Get BAR
    *BAR = OrigBar;           // Restore BAR
    
    return BarReq;
}//SaveSetGetRestoreBAR

VOID
SetIOBAR(
    IN        volatile
            UINT32*  BAR,
    IN        UINT32   BarReq,
    IN    OUT    UINT8*   Cmd,
    IN    OUT    IO_REGS* io_r
)
{
    UINT16 Alignment = ~(BarReq & 0xFFFC);
    UINT16 Size = Alignment + 1;
    UINT16 NewBase;

    if(io_r->Base > io_r->Limit || !Size)
        return;

    NewBase = BAR_ALIGN(io_r->Base, Alignment);
    if(NewBase > io_r->Limit || NewBase + Size - 1 > io_r->Limit)
        return;

    *BAR = NewBase; // Set BAR
    io_r->Base = NewBase + Size; // Advance to new position
    *Cmd |= CMD_BM_IO; // Set IO Space Enable
}//SetIOBAR

VOID
SetMemBAR(
    IN        volatile 
            UINT32*   BAR,
    IN        UINT32    BarReq,
    IN    OUT    UINT8*    Cmd,
    IN    OUT    MEM_REGS* mem_r
)
{
    UINT32 Alignment = ~(BarReq & 0xFFFFFFF0);
    UINT32 Size = Alignment + 1;
    UINT32 NewBase;

    if(mem_r->Base > mem_r->Limit || !Size)
        return;

    NewBase = BAR_ALIGN(mem_r->Base, Alignment);
    if(NewBase > mem_r->Limit || NewBase + Size - 1 > mem_r->Limit)
        return;

    *BAR = NewBase; // Set BAR
    mem_r->Base = NewBase + Size; // Advance to new position
    *Cmd |= CMD_BM_MEM; // Set Memory Space Enable
}//SetMemBAR

VOID
SetPMem64BAR(
    IN        volatile 
            UINT32*   BAR,
	IN		BOOLEAN   IsMaxBAR,
    IN        UINT32    BarReq,
    IN    OUT    UINT8*    Cmd,
    IN    OUT    PMEM_REGS* mem_r
)
{
    UINT32 Alignment = ~(BarReq & 0xFFFFFFF0);
    UINT32 Size = Alignment + 1;
    UINT64 NewBase;

    if(mem_r->Base64 > mem_r->Limit64 || !Size)
        return;

    NewBase = BAR_ALIGN(mem_r->Base64, Alignment);
    if(NewBase > mem_r->Limit64 || NewBase + Size - 1 > mem_r->Limit64)
        return;

    *BAR = (UINT32)(NewBase & 0xFFFFFFFF); // Set BAR
	if(!IsMaxBAR)
	{
		BAR++;
		*BAR = (UINT32)(NewBase >> 32); // Set BAR U
	}
    mem_r->Base64 = NewBase + Size; // Advance to new position
    *Cmd |= CMD_BM_MEM; // Set Memory Space Enable
}//SetPMem64BAR

VOID
SetDevResources(
    IN        UINT8      Bus,
    IN        UINT8      Dev,
    IN        UINT8      FUNC_MAX, // PCI_MAX_FUNC for devices, 1 for bridge
    IN        UINT8      BAR_MAX, // PCI_BAR5 for devices, PCI_BAR1 for bridge
    IN    OUT    PortInfo   *pi
)
{
    UINT8       Fun;
    UINT8       Reg;
    UINT8       BCC; //Base Class Code
    UINT16      VendorID;
    UINT16      DeviceID;
    UINT32      BarReq;
    IO_REGS IO;
    MEM_REGS Mem;
    PMEM_REGS PMem;

    IO.Base = pi->IOBase << 8;
    IO.Limit = (pi->IOLimit << 8) | 0xFF;
    Mem.Base = pi->MemBase << 16;
    Mem.Limit = (pi->MemLimit << 16) | 0xFFFF;
    PMem.Base64 = pi->PMemBase64 << 16;
    PMem.Limit64 = (pi->PMemLimit64 << 16) | 0xFFFF;

    for (Fun = 0; Fun < FUNC_MAX; ++Fun)
    {
        UINT8 Cmd = SmiPci8 (0x00, Bus, Dev, Fun, PCI_CMD) = CMD_BUS_MASTER;
        BCC      = SmiPci8 (0x00, Bus, Dev, Fun, PCI_BCC);
        VendorID = SmiPci16 (0x00, Bus, Dev, Fun, PCI_VID);
        DeviceID = SmiPci16 (0x00, Bus, Dev, Fun, PCI_DID);

        if (0xFFFF == DeviceID)
            continue;

        for (Reg = PCI_BAR0; Reg <= BAR_MAX; Reg += 4)
        {
            BarReq = SaveSetGetRestoreBAR(SmiPci32Ptr (0x00, Bus, Dev, Fun, Reg));// Perform BAR sizing

            if (BarReq & BIT0) // I/O BAR
            {
                SetIOBAR(SmiPci32Ptr(0x00, Bus, Dev, Fun, Reg),
                    BarReq, &Cmd, &IO);
                continue;
            }

			if(BarReq & BIT3)// P-Memory BAR
			{
				SetPMem64BAR(SmiPci32Ptr(0x00, Bus, Dev, Fun, Reg), BAR_MAX == Reg, BarReq, &Cmd, &PMem);
			}
			else
			{
				SetMemBAR(SmiPci32Ptr(0x00, Bus, Dev, Fun, Reg), BarReq, &Cmd, &Mem);
			}

            if (BIT2 == (BarReq & (BIT2 | BIT1))) // Base address is 64 bits wide
            {
                Reg += 4;
                if(!(BarReq & BIT3))// 64-bit memory bar
                    SmiPci32 (0x00, Bus, Dev, Fun, Reg) = 0; // Allocate from 32 bit space
            }
        }

        // Assign ROM BAR
        if (!(IsTBTDevice(DeviceID) && VendorID == 0x8086)){
            TRACE((-1, "Start Assign ROM BAR for device....\n"));
            if (BCC == PCI_CL_BRIDGE)
                Reg = PCI_P2P_ROM_BAR;
            else
                Reg = PCI_DEV_ROM_BAR;

            BarReq = SaveSetGetRestoreBAR(SmiPci32Ptr (0x00, Bus, Dev, Fun, Reg));// Perform BAR sizing
            SetMemBAR(SmiPci32Ptr(0x00, Bus, Dev, Fun, Reg), BarReq, &Cmd, &Mem);
            TRACE((-1, "Assign rom bar end....\n"));
        }

        if(Cmd & BIT1) // If device uses I/O and MEM mapping use only MEM mepping
            Cmd &= ~BIT0;

        SmiPci8  (0x00, Bus, Dev, Fun, PCI_CMD) = Cmd;
        SmiPci8  (0x00, Bus, Dev, Fun, PCI_CLS) = gCacheLineSize;
    }// Fun < PCI_MAX_FUNC
    
    // Update pi if any changes
    if (IO.Base > ((UINT32)pi->IOBase << 8))
        pi->IOBase = (UINT8)(BAR_ALIGN(IO.Base, 0xFFF) >> 8);

    if (Mem.Base > ((UINT32)pi->MemBase << 16))
        pi->MemBase = (UINT16)(BAR_ALIGN(Mem.Base, 0xFFFFF) >> 16);

    if (PMem.Base64 > (pi->PMemBase64 << 16))
        pi->PMemBase64 = (BAR_ALIGN(PMem.Base64, 0xFFFFF) >> 16);
}// SetDevResources

typedef struct _DEV_ID
{
  UINT8     Bus;
  UINT8     Dev;
  UINT8     Fun;
} DEV_ID;

DEV_ID HR_Slots[] = 
{
    {0x00, 0x1C, 0x00},// PCH slot 0
    {0x00, 0x1C, 0x01},// PCH slot 1
    {0x00, 0x1C, 0x02},// PCH slot 2
    {0x00, 0x1C, 0x03},// PCH slot 3
    {0x00, 0x1C, 0x04},// PCH slot 4
    {0x00, 0x1C, 0x05},// PCH slot 5
    {0x00, 0x1C, 0x06},// PCH slot 6
    {0x00, 0x1C, 0x07},// PCH slot 7
    {0x00, 0x01, 0x00},// PEG slot
};

//#define count(x) (sizeof(x) / sizeof((x)[0]))

typedef struct _BRDG_RES_CONFIG
{
	UINT8    Cmd;
	UINT8    Cls;
	UINT8    IOBase;
	UINT8    IOLimit;
	UINT16   MemBase;
	UINT16   MemLimit;
    UINT64   PMemBase64;
    UINT64   PMemLimit64;
} BRDG_RES_CONFIG;

const
BRDG_RES_CONFIG NOT_IN_USE_BRIDGE = 
{
	CMD_BUS_MASTER,
	0,
	DISBL_IO_REG1C & 0xFF,
	DISBL_IO_REG1C >> 8,
	DISBL_MEM32_REG20 & 0xFFFF,
	DISBL_MEM32_REG20 >> 16,
	DISBL_PMEM_REG24 & 0xFFFF,
    DISBL_PMEM_REG24 >> 16
};

typedef struct _BRDG_CONFIG
{
    DEV_ID           DevId;
    UINT8            PBus;
    UINT8            SBus;
    UINT8            SubBus;
    BRDG_RES_CONFIG  Res;
} BRDG_CONFIG;

enum {
HR_US_PORT, 
HR_DS_PORT0,
HR_DS_PORT3,
HR_DS_PORT4,
HR_DS_PORT5,
HR_DS_PORT6,
MAX_CFG_PORTS
};

enum {
	HR_DS_PORT1 = HR_DS_PORT3
};

BRDG_CONFIG HRConfigs[MAX_CFG_PORTS];// US(X:0:0), DS(X+1:3:0),DS(X+1:4:0),DS(X+1:5:0),DS(X+1:6:0)

typedef struct _HR_CONFIG
{
	UINT16           DeviceId;
	UINT8            HRBus;
	UINT8            MinDSNumber;
	UINT8            MaxDSNumber;
	UINT8            BridgeLoops;
} HR_CONFIG;

VOID
InitCommonHRConfigs(
    IN        HR_CONFIG *HR_Config,
    IN        UINT8 BusNumLimit,
    IN    OUT    BRDG_RES_CONFIG* HRResConf
)
{
    UINT8 i,j;
    // US(HRBus:0:0)
    HRConfigs[HR_US_PORT].DevId.Bus = HR_Config->HRBus;
    HRConfigs[HR_US_PORT].DevId.Dev = 0;
    HRConfigs[HR_US_PORT].DevId.Fun = 0;
    HRConfigs[HR_US_PORT].Res = *HRResConf;
	if (gTbtIOresourceEnable == FALSE){
	   HRConfigs[HR_US_PORT].Res.IOBase = 0xF1;
	   HRConfigs[HR_US_PORT].Res.IOLimit = 0x01;
	}
    HRConfigs[HR_US_PORT].PBus = HRConfigs[HR_US_PORT].DevId.Bus;
    HRConfigs[HR_US_PORT].SBus = HRConfigs[HR_US_PORT].PBus + 1;
    HRConfigs[HR_US_PORT].SubBus = BusNumLimit;

    // NHI resides here
    HRConfigs[HR_DS_PORT0].DevId.Bus = HRConfigs[HR_US_PORT].DevId.Bus + 1;
    HRConfigs[HR_DS_PORT0].DevId.Dev = 0;
    HRConfigs[HR_DS_PORT0].DevId.Fun = 0;
    HRConfigs[HR_DS_PORT0].Res = NOT_IN_USE_BRIDGE;
    HRConfigs[HR_DS_PORT0].Res.MemBase = HRResConf->MemLimit;
    HRConfigs[HR_DS_PORT0].Res.MemLimit = HRResConf->MemLimit;
    HRResConf->MemLimit -= 0x10; //This 1 MB chunk will be used by NHI
    HRConfigs[HR_DS_PORT0].Res.Cmd = CMD_BM_MEM;
    HRConfigs[HR_DS_PORT0].Res.Cls = gCacheLineSize;
    HRConfigs[HR_DS_PORT0].PBus = HRConfigs[HR_DS_PORT0].DevId.Bus;
    HRConfigs[HR_DS_PORT0].SBus = HRConfigs[HR_DS_PORT0].PBus + 1;
    HRConfigs[HR_DS_PORT0].SubBus = HRConfigs[HR_DS_PORT0].PBus + 1;

	switch(HR_Config->DeviceId)
	{
		case WR_HR_2C:// HR with 1 DS only
			HRConfigs[HR_DS_PORT1].DevId.Bus = HRConfigs[HR_US_PORT].DevId.Bus + 1;
			HRConfigs[HR_DS_PORT1].DevId.Dev = 1;
			HRConfigs[HR_DS_PORT1].DevId.Fun = 0;
			HRConfigs[HR_DS_PORT1].Res = *HRResConf;
			HRConfigs[HR_DS_PORT1].PBus = HRConfigs[HR_DS_PORT1].DevId.Bus;
			HRConfigs[HR_DS_PORT1].SBus = HRConfigs[HR_DS_PORT0].SubBus + 1;
			HRConfigs[HR_DS_PORT1].SubBus = BusNumLimit;
			HR_Config->MinDSNumber = HRConfigs[HR_DS_PORT1].DevId.Dev;
			HR_Config->MaxDSNumber = HRConfigs[HR_DS_PORT1].DevId.Dev;
			HR_Config->BridgeLoops = 3;
			break;
		default:
		// DS(HRBus+2:3-6:0)
		HR_Config->MinDSNumber = 3;
		HR_Config->MaxDSNumber = 6;
		HR_Config->BridgeLoops = count(HRConfigs);

		for(j = 2, i = HR_Config->MinDSNumber; j < count(HRConfigs) && i <= HR_Config->MaxDSNumber; ++j, ++i)
		{
			HRConfigs[j].DevId.Bus = HRConfigs[HR_US_PORT].DevId.Bus + 1;
			HRConfigs[j].DevId.Dev = i;
			HRConfigs[j].DevId.Fun = 0;
			HRConfigs[j].PBus = HRConfigs[j].DevId.Bus;
			HRConfigs[j].Res.Cls = gCacheLineSize;
		}
	}
}//InitCommonHRConfigs

VOID
InitHRDSPort_Disable(
    IN        UINT8 id,
    IN    OUT    BRDG_CONFIG* BrdgConf
)
{
    HRConfigs[id].Res = NOT_IN_USE_BRIDGE;
    HRConfigs[id].SBus = BrdgConf->SBus;
    HRConfigs[id].SubBus = BrdgConf->SBus;

    BrdgConf->SBus++;
}//InitHRDSPort_Disable

VOID
InitHRDSPort_1Port(
	IN	OUT	BRDG_CONFIG* BrdgConf
)
{
	UINT16 MemBase	= BrdgConf->Res.MemBase & 0xFFF0;
    UINT64 PMemBase64 = BrdgConf->Res.PMemBase64 & ~0xFULL;
	UINT8  IOBase   = BrdgConf->Res.IOBase & 0xF0;
	UINT8  BusRange = BrdgConf->SubBus - BrdgConf->PBus - (MAX_CFG_PORTS - 2); // MAX_CFG_PORTS-1(US)-1(HIA) is num of bridges in HR, on each bridge bus# is incremented
	BusRange -= DOCK_BUSSES; // Bus range for Dock port

    if (gTbtIOresourceEnable == TRUE)
	   HRConfigs[HR_DS_PORT3].Res.Cmd = CMD_BM_MEM_IO;
	else{
	   HRConfigs[HR_DS_PORT3].Res = NOT_IN_USE_BRIDGE;
	   HRConfigs[HR_DS_PORT3].Res.Cmd = CMD_BM_MEM;
    }
	HRConfigs[HR_DS_PORT3].Res.Cls = gCacheLineSize;
	HRConfigs[HR_DS_PORT3].Res.MemBase = MemBase;
	HRConfigs[HR_DS_PORT3].Res.MemLimit = MemBase + 0xE00 - 1;
    HRConfigs[HR_DS_PORT3].Res.PMemBase64 = PMemBase64;
    HRConfigs[HR_DS_PORT3].Res.PMemLimit64 = PMemBase64 + 0x1A00 - 1;

	if (gTbtIOresourceEnable == TRUE){
	   HRConfigs[HR_DS_PORT3].Res.IOBase = IOBase;
       if ((BrdgConf->Res.IOLimit & 0xF0) < (IOBase + 0x50))
         HRConfigs[HR_DS_PORT3].Res.IOLimit = BrdgConf->Res.IOLimit & 0xF0;
       else
	     HRConfigs[HR_DS_PORT3].Res.IOLimit = IOBase + 0x50;
	}

	HRConfigs[HR_DS_PORT3].SBus = BrdgConf->SBus;
	HRConfigs[HR_DS_PORT3].SubBus = BrdgConf->SBus + BusRange;

	BrdgConf->SBus = HRConfigs[HR_DS_PORT3].SubBus + 1;

    if (gTbtIOresourceEnable == TRUE)
	   HRConfigs[HR_DS_PORT4].Res.Cmd = CMD_BM_MEM_IO;
	else{
	   HRConfigs[HR_DS_PORT4].Res = NOT_IN_USE_BRIDGE;
	   HRConfigs[HR_DS_PORT4].Res.Cmd = CMD_BM_MEM;
    }
	HRConfigs[HR_DS_PORT4].Res.Cls = gCacheLineSize;
	HRConfigs[HR_DS_PORT4].Res.MemBase = MemBase + 0xE00;
	HRConfigs[HR_DS_PORT4].Res.MemLimit = MemBase + 0x1600 - 1;
    HRConfigs[HR_DS_PORT4].Res.PMemBase64 = PMemBase64 + 0x1A00;
    HRConfigs[HR_DS_PORT4].Res.PMemLimit64 = PMemBase64 + 0x2200 - 1;

	if (gTbtIOresourceEnable == TRUE){
       if (HRConfigs[HR_DS_PORT3].Res.IOLimit == (BrdgConf->Res.IOLimit & 0xF0)){
	      HRConfigs[HR_DS_PORT4].Res.IOBase = 0xF1;
	      HRConfigs[HR_DS_PORT4].Res.IOLimit = 0x01;
       }
       else{
	      HRConfigs[HR_DS_PORT4].Res.IOBase = IOBase + 0x60;
	      HRConfigs[HR_DS_PORT4].Res.IOLimit = BrdgConf->Res.IOLimit & 0xF0;
       }
	}

	HRConfigs[HR_DS_PORT4].SBus = BrdgConf->SBus;
	HRConfigs[HR_DS_PORT4].SubBus = BrdgConf->SBus + DOCK_BUSSES;

	BrdgConf->SBus = HRConfigs[HR_DS_PORT4].SubBus + 1;
}//InitHRDSPort_1Port

VOID
InitHRDSPort_2Port(
	IN	OUT	BRDG_CONFIG* BrdgConf
)
{
	UINT16 MemBase	= BrdgConf->Res.MemBase & 0xFFF0;
    UINT64 PMemBase64 = BrdgConf->Res.PMemBase64 & ~0xFULL;
	UINT8  IOBase   = BrdgConf->Res.IOBase & 0xF0;
	UINT8  BusRange = BrdgConf->SubBus - BrdgConf->PBus - (MAX_CFG_PORTS - 2); // MAX_CFG_PORTS-1(US)-1(HIA) is num of bridges in HR, on each bridge bus# is incremented

	BusRange -= 2 * DOCK_BUSSES; // Bus range for Dock ports
	// Rest of busses split between ports 3 and 5
	BusRange /= 2; // Bus range for port 3/5

    if (gTbtIOresourceEnable == TRUE)
	   HRConfigs[HR_DS_PORT3].Res.Cmd = CMD_BM_MEM_IO;
	else{
       HRConfigs[HR_DS_PORT3].Res = NOT_IN_USE_BRIDGE;
	   HRConfigs[HR_DS_PORT3].Res.Cmd = CMD_BM_MEM;
    }
	HRConfigs[HR_DS_PORT3].Res.Cls = gCacheLineSize;
	HRConfigs[HR_DS_PORT3].Res.MemBase = MemBase;
	HRConfigs[HR_DS_PORT3].Res.MemLimit = MemBase + 0x1000 - 1;
    HRConfigs[HR_DS_PORT3].Res.PMemBase64 = PMemBase64;
    HRConfigs[HR_DS_PORT3].Res.PMemLimit64 = PMemBase64 + 0x2000 - 1;

	if (gTbtIOresourceEnable == TRUE){
	   HRConfigs[HR_DS_PORT3].Res.IOBase = IOBase;
       if ((BrdgConf->Res.IOLimit & 0xF0) < (IOBase + 0x50))
         HRConfigs[HR_DS_PORT3].Res.IOLimit = BrdgConf->Res.IOLimit & 0xF0;
       else
	     HRConfigs[HR_DS_PORT3].Res.IOLimit = IOBase + 0x50;
	}

	HRConfigs[HR_DS_PORT3].SBus = BrdgConf->SBus;
	HRConfigs[HR_DS_PORT3].SubBus = BrdgConf->SBus + BusRange;

	BrdgConf->SBus = HRConfigs[HR_DS_PORT3].SubBus + 1;

	if (gTbtIOresourceEnable == TRUE)
	   HRConfigs[HR_DS_PORT4].Res.Cmd = CMD_BM_MEM_IO;
	else{
       HRConfigs[HR_DS_PORT4].Res = NOT_IN_USE_BRIDGE;
	   HRConfigs[HR_DS_PORT4].Res.Cmd = CMD_BM_MEM;
    }
	HRConfigs[HR_DS_PORT4].Res.Cls = gCacheLineSize;
	HRConfigs[HR_DS_PORT4].Res.MemBase = MemBase + 0x1000;
	HRConfigs[HR_DS_PORT4].Res.MemLimit = MemBase + 0x1800 - 1;
    HRConfigs[HR_DS_PORT4].Res.PMemBase64 = PMemBase64 + 0x2000;
    HRConfigs[HR_DS_PORT4].Res.PMemLimit64 = PMemBase64 + 0x2800 - 1;

	if (gTbtIOresourceEnable == TRUE){
       if (gTbtNVMversion > 14){
          HRConfigs[HR_DS_PORT4].Res.Cmd     = CMD_BM_MEM;
	      HRConfigs[HR_DS_PORT4].Res.IOBase  = 0xF1;
	      HRConfigs[HR_DS_PORT4].Res.IOLimit = 0x01;
       }
       else{
          if (HRConfigs[HR_DS_PORT3].Res.IOLimit == (BrdgConf->Res.IOLimit & 0xF0)){
             HRConfigs[HR_DS_PORT4].Res.IOBase  = 0xF1;
	         HRConfigs[HR_DS_PORT4].Res.IOLimit = 0x01;
          }
          else{
             HRConfigs[HR_DS_PORT4].Res.IOBase  = IOBase + 0x60;
	         HRConfigs[HR_DS_PORT4].Res.IOLimit = BrdgConf->Res.IOLimit & 0xF0;
          }
       }
	}

	HRConfigs[HR_DS_PORT4].SBus = BrdgConf->SBus;
	HRConfigs[HR_DS_PORT4].SubBus = BrdgConf->SBus + DOCK_BUSSES;

	BrdgConf->SBus = HRConfigs[HR_DS_PORT4].SubBus + 1;

	if (gTbtIOresourceEnable == TRUE)
	   HRConfigs[HR_DS_PORT5].Res.Cmd = CMD_BM_MEM_IO;
	else{
       HRConfigs[HR_DS_PORT5].Res = NOT_IN_USE_BRIDGE;
	   HRConfigs[HR_DS_PORT5].Res.Cmd = CMD_BM_MEM;
    }
	HRConfigs[HR_DS_PORT5].Res.Cls = gCacheLineSize;
	HRConfigs[HR_DS_PORT5].Res.MemBase = MemBase + 0x1800;
	HRConfigs[HR_DS_PORT5].Res.MemLimit = MemBase + 0x2600 - 1;
    HRConfigs[HR_DS_PORT5].Res.PMemBase64 = PMemBase64 + 0x3000;
    HRConfigs[HR_DS_PORT5].Res.PMemLimit64 = PMemBase64 + 0x4A00 - 1;

	if (gTbtIOresourceEnable == TRUE){
       if (gTbtNVMversion > 14){
          if (HRConfigs[HR_DS_PORT3].Res.IOLimit == (BrdgConf->Res.IOLimit & 0xF0)){
             HRConfigs[HR_DS_PORT5].Res.IOBase  = 0xF1;
	         HRConfigs[HR_DS_PORT5].Res.IOLimit = 0x01;
          }
          else{
             HRConfigs[HR_DS_PORT5].Res.IOBase  = IOBase + 0x60;
	         HRConfigs[HR_DS_PORT5].Res.IOLimit = BrdgConf->Res.IOLimit & 0xF0;
          }
       }
       else{
          HRConfigs[HR_DS_PORT5].Res.Cmd     = CMD_BM_MEM;
	      HRConfigs[HR_DS_PORT5].Res.IOBase  = 0xF1;
	      HRConfigs[HR_DS_PORT5].Res.IOLimit = 0x01;
       }
	}
	HRConfigs[HR_DS_PORT5].SBus = BrdgConf->SBus;
	HRConfigs[HR_DS_PORT5].SubBus = BrdgConf->SBus + BusRange;

	BrdgConf->SBus = HRConfigs[HR_DS_PORT5].SubBus + 1;

	if (gTbtIOresourceEnable == TRUE)
	   HRConfigs[HR_DS_PORT6].Res.Cmd = CMD_BM_MEM_IO;
	else{
       HRConfigs[HR_DS_PORT6].Res = NOT_IN_USE_BRIDGE;
	   HRConfigs[HR_DS_PORT6].Res.Cmd = CMD_BM_MEM;
    }
	HRConfigs[HR_DS_PORT6].Res.Cls = gCacheLineSize;
	HRConfigs[HR_DS_PORT6].Res.MemBase = MemBase + 0x2600;
	HRConfigs[HR_DS_PORT6].Res.MemLimit = MemBase + 0x2E00 - 1;
    HRConfigs[HR_DS_PORT6].Res.PMemBase64 = PMemBase64 + 0x2800;
    HRConfigs[HR_DS_PORT6].Res.PMemLimit64 = PMemBase64 + 0x3000 - 1;
	if (gTbtIOresourceEnable == TRUE){
          HRConfigs[HR_DS_PORT6].Res.Cmd     = CMD_BM_MEM;
	      HRConfigs[HR_DS_PORT6].Res.IOBase  = 0xF1;
	      HRConfigs[HR_DS_PORT6].Res.IOLimit = 0x01;
	}
	HRConfigs[HR_DS_PORT6].SBus = BrdgConf->SBus;
	HRConfigs[HR_DS_PORT6].SubBus = BrdgConf->SBus + DOCK_BUSSES;

	BrdgConf->SBus = HRConfigs[HR_DS_PORT6].SubBus + 1;
}//InitHRDSPort_2Port

BOOLEAN
CheckLimits(
	IN		BOOLEAN			Is2PortDev,
	IN		BRDG_RES_CONFIG *HRResConf,
	IN		UINT8			BusRange
)
{
	UINT16 MemBase	= HRResConf->MemBase & 0xFFF0;
	UINT16 MemLimit = HRResConf->MemLimit & 0xFFF0;
    UINT64 PMemBase64  = HRResConf->PMemBase64 & 0xFFF0;
    UINT64 PMemLimit64 = HRResConf->PMemLimit64 & 0xFFF0;
	UINT8  IOBase   = HRResConf->IOBase & 0xF0;
	UINT8  IOLimit  = HRResConf->IOLimit & 0xF0;

    TRACE((-1, "TbtSmm.c: MemBase   = %x\n", MemBase));
    TRACE((-1, "TbtSmm.c: MemLimit  = %x\n", MemLimit));
    TRACE((-1, "TbtSmm.c: PMemBase  = %x\n", PMemBase64));
    TRACE((-1, "TbtSmm.c: PMemLimit = %x\n", PMemLimit64));

	// Check memory alignment
	if(MemBase & 0x3FF)
	{
        TRACE((-1, "TbtSmm.c: M alig is not 64 MB.\n"));
		return FALSE;
	}
	if(PMemBase64 & 0xFFF)
	{
        TRACE((-1, "TbtSmm.c: PM alig is not 256 MB.\n"));
		return FALSE;
	}

	// Check mem size
	
	if(Is2PortDev)
	{
		// Check mem size
		if(MemLimit + 0x10 - MemBase < 0x2E00)
		{
			TRACE((-1, "TbtSmm.c: M size is small than 737 MB.\n"));
			return FALSE;
		}
		// Check P-mem size
		if(PMemLimit64 + 0x10 - PMemBase64 < 0x4A00)
		{
			TRACE((-1, "TbtSmm.c: PM size is small than 1184 MB.\n"));
			return FALSE;
		}
		// Check bus range
		if(BusRange < 106)
		{
			TRACE((-1, "TbtSmm.c: Bus range is small than 106.\n"));
			return FALSE;
		}
	}
	else
	{
		if(MemLimit + 0x10 - MemBase < 0x1600) //Reserved mem min: 353MB
		{
            TRACE((-1, "TbtSmm.c: M size is small than 353 MB.\n"));
			return FALSE;
		}
		if(PMemLimit64 + 0x10 - PMemBase64 < 0x2200) //Prefetchable mem min: 544MB
		{
            TRACE((-1, "TbtSmm.c: PM size is small than 544 MB.\n"));
			return FALSE;
		}
		if(BusRange < 56) //Reserved bus min: 56
		{
            TRACE((-1, "TbtSmm.c: Bus range is small than 56\n"));
			return FALSE;
		}
	}

	return TRUE;
}



BOOLEAN
InitHRResConfigs(
    IN	  OUT    HR_CONFIG *HR_Config,
    IN           UINT8 BusNumLimit,
    IN    OUT    BRDG_RES_CONFIG* HRResConf
)
{
    BRDG_CONFIG BrdgConf = {0};
    InitCommonHRConfigs(HR_Config, BusNumLimit, HRResConf);
    BrdgConf.PBus = HR_Config->HRBus + 2;
    BrdgConf.SBus = HR_Config->HRBus + 3;
    BrdgConf.SubBus = BusNumLimit;
    BrdgConf.Res = *HRResConf;
    while(TRUE){
      switch(HR_Config->DeviceId)
      {
      case CR_HR_4C:
      case RR_HR_4C: 
      case FR_HR_4C: // 2 Port host
          if(CheckLimits(TRUE, HRResConf, BusNumLimit - HR_Config->HRBus))
          {
              InitHRDSPort_2Port(&BrdgConf);
              return TRUE;
          }
          else
          {
              HR_Config->DeviceId = 0; // Jump to default on next loop
              continue;
          }
      case CR_HR_2C:
      case RR_HR_2C:
      case FR_HR_2C: // 1 Port host
          if(CheckLimits(FALSE, HRResConf, BusNumLimit - HR_Config->HRBus))
          {	
              InitHRDSPort_1Port(&BrdgConf);
              InitHRDSPort_Disable(HR_DS_PORT5, &BrdgConf);
              InitHRDSPort_Disable(HR_DS_PORT6, &BrdgConf);
              return TRUE;
          }
      case WR_HR_2C: // 1 Port host
          return TRUE;
      default:
              InitHRDSPort_Disable(HR_DS_PORT3, &BrdgConf);
              InitHRDSPort_Disable(HR_DS_PORT4, &BrdgConf);
              InitHRDSPort_Disable(HR_DS_PORT5, &BrdgConf);
              InitHRDSPort_Disable(HR_DS_PORT6, &BrdgConf);
              return FALSE;
      }//switch
    }//while
}//InitHRResConfigs

BOOLEAN
InitializeHostRouter(
        OUT  HR_CONFIG  *HR_Config
)
{
    UINT8             BusNumLimit;
    BRDG_RES_CONFIG   HRResConf = {0};
    UINT8             i;
	BOOLEAN           Ret = TRUE;

    for(i = 0; i < count(HR_Slots); ++i)
    {
        HR_Config->HRBus  = SmiPci8  (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_SBUS);
        HR_Config->DeviceId = SmiPci16 (0x00, HR_Config->HRBus, 0x00, 0x00, PCI_DID);
        if (IsTBTHostRouter(HR_Config->DeviceId))
            break;
    }

    if(i >= count(HR_Slots))
        return FALSE;

    if (gTbtIOresourceEnable == TRUE)
	   HRResConf.Cmd         = CMD_BM_MEM_IO;
	else
       HRResConf.Cmd         = CMD_BM_MEM;
    HRResConf.Cls         = gCacheLineSize;
    HRResConf.IOBase      = SmiPci8  (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_IOBASE);
    HRResConf.IOLimit     = SmiPci8  (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_IOLIMIT);
    HRResConf.MemBase     = SmiPci16 (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_MEMBASE);
    HRResConf.MemLimit    = SmiPci16 (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_MEMLIMIT);
    HRResConf.PMemBase64  = SmiPci16 (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_PRE_MEMBASE);
    HRResConf.PMemLimit64 = SmiPci16 (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_PRE_MEMLIMIT);
    HRResConf.PMemBase64 |= SmiPci32 (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_PRE_MEMBASE_U) << 16;
    HRResConf.PMemLimit64|= SmiPci32 (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_PRE_MEMLIMIT_U) << 16;
    BusNumLimit           = SmiPci8  (0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_SUBUS);

    // BIOS support of Thunderbolt devices Specification Update
    // Revision 0.91
    // 8.1.2 VGA Enable should not be set
    // VGA Enable and VGA 16-bit decode registers of Bridge 
    // control register of Root port where Host router resides
    // should be cleaned(Both of them should set into 0).
    SmiPci8And(0x00, HR_Slots[i].Bus, HR_Slots[i].Dev, HR_Slots[i].Fun, PCI_BRIDGE_CNTL, 0xE7);

	Ret = InitHRResConfigs(HR_Config, BusNumLimit, &HRResConf);

    for(i = 0; i < HR_Config->BridgeLoops; ++i)
    {
        UINT8 Bus = HRConfigs[i].DevId.Bus;
        UINT8 Dev = HRConfigs[i].DevId.Dev;
        UINT8 Fun = HRConfigs[i].DevId.Fun;

        SmiPci8  (0x00, Bus, Dev, Fun, PCI_CLS) = HRConfigs[i].Res.Cls;
        SmiPci8  (0x00, Bus, Dev, Fun, PCI_PBUS) = HRConfigs[i].PBus;
        SmiPci8  (0x00, Bus, Dev, Fun, PCI_SBUS) = HRConfigs[i].SBus;
        SmiPci8  (0x00, Bus, Dev, Fun, PCI_SUBUS) = HRConfigs[i].SubBus;
        SmiPci16 (0x00, Bus, Dev, Fun, PCI_MEMBASE) = HRConfigs[i].Res.MemBase;
        SmiPci16 (0x00, Bus, Dev, Fun, PCI_MEMLIMIT) = HRConfigs[i].Res.MemLimit;
        SmiPci16 (0x00, Bus, Dev, Fun, PCI_PRE_MEMBASE) = (UINT16)(HRConfigs[i].Res.PMemBase64 & 0xFFFF);
        SmiPci16 (0x00, Bus, Dev, Fun, PCI_PRE_MEMLIMIT) = (UINT16)(HRConfigs[i].Res.PMemLimit64 & 0xFFFF);
        SmiPci32 (0x00, Bus, Dev, Fun, PCI_PRE_MEMBASE_U) = (UINT32)(HRConfigs[i].Res.PMemBase64 >> 16);
        SmiPci32 (0x00, Bus, Dev, Fun, PCI_PRE_MEMLIMIT_U) = (UINT32)(HRConfigs[i].Res.PMemLimit64 >> 16);
        SmiPci8  (0x00, Bus, Dev, Fun, PCI_IOBASE) = HRConfigs[i].Res.IOBase;
        SmiPci8  (0x00, Bus, Dev, Fun, PCI_IOLIMIT) = HRConfigs[i].Res.IOLimit;
        SmiPci32 (0x00, Bus, Dev, Fun, PCI_IOBASE_U) = 0x00000000;
        SmiPci8  (0x00, Bus, Dev, Fun, PCI_CMD) = HRConfigs[i].Res.Cmd;
    }

    SmiPci32 (0x00, (HR_Config->HRBus + 2), 0x00, 0x00, PCI_BAR0) = HRConfigs[HR_DS_PORT0].Res.MemLimit << 16;
    SmiPci32 (0x00, (HR_Config->HRBus + 2), 0x00, 0x00, PCI_BAR1) = (HRConfigs[HR_DS_PORT0].Res.MemLimit + 0x4) << 16;
    SmiPci8  (0x00, (HR_Config->HRBus + 2), 0x00, 0x00, PCI_CLS)  = gCacheLineSize;
    SmiPci8  (0x00, (HR_Config->HRBus + 2), 0x00, 0x00, PCI_CMD)  = CMD_BM_MEM;
    
	return Ret;
}//InitializeHostRouter

UINT8
ConfigureSlot(
    IN        UINT8     Bus,
    IN        UINT8     MAX_DEVICE,
	IN		  INT8	    Depth, 
    IN    OUT    PortInfo* pi
)
{
    UINT8    Device;
    UINT8    SBus;
    UINT8    UsedBusNumbers;
    UINT8    RetBusNum = 0;
    PortInfo CurrentSlot;

    for (Device = 0; Device < MAX_DEVICE; Device++)
    {
        // Continue if device is absent
        if (0xFFFF == SmiPci16 (0x00, Bus, Device, 0x00, PCI_DID))
            continue;

        if (P2P_BRIDGE != SmiPci16 (0x00, Bus, Device, 0x00, PCI_SCC))
        {
             SetDevResources(Bus, Device,
                PCI_MAX_FUNC, PCI_BAR5, pi);
            continue;
        }
        // Else Bridge

        CurrentSlot = *pi; // Save before update

        ++RetBusNum; // UP Bridge
        SBus = Bus + RetBusNum; // DS Bridge

        if (SBus + 1 >= pi->BusNumLimit)
            continue;

        SetDevResources(Bus, Device, 1, PCI_BAR1, pi);

        // Init UP Bridge to reach DS Bridge
        SmiPci8 (0x00, Bus, Device, 0x00, PCI_PBUS) = Bus;
        SmiPci8 (0x00, Bus, Device, 0x00, PCI_SBUS) = SBus;
        SmiPci8 (0x00, Bus, Device, 0x00, PCI_SUBUS) = pi->BusNumLimit;// Just in case
		if (gTbtIOresourceEnable == TRUE)
		   SmiPci8 (0x00, Bus, Device, 0x00, PCI_CMD) = CMD_BM_MEM_IO;
		else
           SmiPci8 (0x00, Bus, Device, 0x00, PCI_CMD) = CMD_BM_MEM;

		UsedBusNumbers = ConfigureSlot(SBus, PCI_MAX_DEVICE + 1, -1, pi);

        RetBusNum += UsedBusNumbers;

        SetPHYPortResources(Bus, Device,
			SBus + UsedBusNumbers, Depth,
            &CurrentSlot, pi);
    }//for (Device = 0; Device <= PCI_MAX_DEVICE; Device++)
    return RetBusNum;
}// ConfigureSlot

VOID
SetCIOPortResources(
    IN        UINT8 Bus, 
    IN        UINT8 Dev,
    IN        UINT8 SBus,
    IN        UINT8 SubBus,
	IN		PortInfo* portInfoBeforeChange, 
	IN	OUT	PortInfo* pi
)
{
    UINT8    Cmd = CMD_BUS_MASTER;

    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_PBUS) = Bus;
    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_SBUS) = SBus;
    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_SUBUS) = SubBus;
    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_CMD) = Cmd;

    if (pi->IOBase <= pi->IOLimit)
    {
        SmiPci8  (0x00, Bus, Dev, 0x00, PCI_IOBASE) = pi->IOBase;
        SmiPci8  (0x00, Bus, Dev, 0x00, PCI_IOLIMIT) = pi->IOLimit;
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_IOBASE_U) = 0x00000000;
        Cmd |= CMD_BM_IO;
    }
    else
    {
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_IOBASE) = DISBL_IO_REG1C;
    }

    if (pi->MemBase <= pi->MemLimit)
    {
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_MEMBASE) = pi->MemBase;
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_MEMLIMIT) = pi->MemLimit;
        Cmd |= CMD_BM_MEM;
    }
    else
    {
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_MEMBASE) = DISBL_MEM32_REG20;
    }

    if (pi->PMemBase64 <= pi->PMemLimit64)
    {
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE) = (UINT16)(pi->PMemBase64 & 0xFFFF);
        SmiPci16 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT) = (UINT16)(pi->PMemLimit64 & 0xFFFF);
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE_U) = (UINT32)(pi->PMemBase64 >> 16);
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT_U) = (UINT32)(pi->PMemLimit64 >> 16);
        Cmd |= CMD_BM_MEM;
    }
    else
    {
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE) = DISBL_PMEM_REG24;
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE_U) = 0;
        SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT_U) = 0;
    }

    SmiPci8  (0x00, Bus, Dev, 0x00, PCI_CMD) = Cmd;
    SmiPci8 (0x00, Bus, Dev, 0x00, PCI_CLS) = gCacheLineSize;
}//SetCIOPortResources

VOID
SetSlotsAsUnused(
    IN        UINT8 Bus,
    IN        UINT8 MaxSlotNum,
    IN        UINT8 CIOSlot,
    IN    OUT PortInfo* pi
)
{
    UINT8 Slot;
    for (Slot = MaxSlotNum; Slot > CIOSlot; --Slot)
    {
        if (0xFFFF == SmiPci16 (0x00, Bus, Slot, 0x00, PCI_DID))
            continue;

        SmiPci8 (0x00, Bus, Slot, 0x00, PCI_CLS) = gCacheLineSize;
        SmiPci8  (0x00, Bus, Slot, 0x00, PCI_PBUS) = Bus;
        SmiPci8  (0x00, Bus, Slot, 0x00, PCI_SBUS) = pi->BusNumLimit;
        SmiPci8  (0x00, Bus, Slot, 0x00, PCI_SUBUS) = pi->BusNumLimit;
        SmiPci16 (0x00, Bus, Slot, 0x00, PCI_IOBASE) = DISBL_IO_REG1C;
        SmiPci32 (0x00, Bus, Slot, 0x00, PCI_MEMBASE) = DISBL_MEM32_REG20;
        SmiPci32 (0x00, Bus, Slot, 0x00, PCI_PRE_MEMBASE) = DISBL_PMEM_REG24;
        SmiPci8  (0x00, Bus, Slot, 0x00, PCI_CMD) = CMD_BUS_MASTER;

        pi->BusNumLimit--;
    }
}//SetSlotsAsUnused

#define PCIE_CAP_ID_VSEC   0x000B

UINT16
FindVendorSpecificHeader(
    IN        UINT8  Bus
)
{
    PCIE_EXT_CAP_HDR  ExtCap;
    UINT16 ExtendedRegister = 0x100;

    while (ExtendedRegister)
    {
        ExtCap.EXT_CAP_HDR = SmiPci32  (0x00, Bus, 0x00, 0x00, ExtendedRegister);
        if (ExtCap.ExtCapId == 0xFFFF)
            return 0x0000; // No Vendor-Specific Extended Capability header

        if (PCIE_CAP_ID_VSEC == ExtCap.ExtCapId)
            return ExtendedRegister;

        ExtendedRegister = (UINT16)ExtCap.NextItemPtr;
    }
    return 0x0000; // No Vendor-Specific Extended Capability header
}

#define PCIE_CAP_ID_SSID_SSVID   0x0D

UINT8
FindSSID_SSVIDHeader(
    IN        UINT8  Bus
)
{
	UINT8  CapHeaderId;
	UINT8  CapHeaderOffset = SmiPci8 (0x00, Bus, 0x00, 0x00, PCI_CAPP);

	while (CapHeaderOffset != 0)
	{
		CapHeaderId = SmiPci8 (0x00, Bus, 0x00, 0x00, CapHeaderOffset);

		if (CapHeaderId == PCIE_CAP_ID_SSID_SSVID)
			return CapHeaderOffset;

		CapHeaderOffset = SmiPci8 (0x00, Bus, 0x00, 0x00, CapHeaderOffset + 1);
	}
	TRACE((-1, "TbtSmm.c: Cannot find SSID Capability header...\n"));
	return 0;
}//FindSSID_SSVIDHeader

typedef union _BRDG_CIO_MAP_REG
{
    UINT32    AB_REG;
    struct
    {
        UINT32        NumOfDSPorts : 5;
        UINT32        CIOPortMap   : 27;
    };
}BRDG_CIO_MAP_REG;

BOOLEAN
GetCIOSlotByDevId(
    IN        UINT8  Bus,
        OUT    UINT8* CIOSlot,
        OUT    UINT8* MaxSlotNum
)
{
    UINT16            VSECRegister;
    BRDG_CIO_MAP_REG  BridgMap;
    UINT32            BitScanRes;
    UINT16            DevId = SmiPci16 (0x00, Bus, 0x00, 0x00, PCI_DID);

    // Init out params in case device is not recognised
    *CIOSlot = 4;
    *MaxSlotNum = 7;

    switch(DevId) // For known device IDs
    {
    case 0x1513:
    case 0x151A:
    case 0x151B:
    case 0x1547:
    case 0x1548:
    case 0x1549:
        return TRUE; // Just return
    }

    VSECRegister = FindVendorSpecificHeader(Bus);
    if(!VSECRegister)
        return TRUE; // Just return

    // Go to Bridge/CIO map register
    VSECRegister += 0x18;

    BridgMap.AB_REG = SmiPci32  (0x00, Bus, 0x00, 0x00, VSECRegister);
    // Check for range
    if(BridgMap.NumOfDSPorts < 1 || BridgMap.NumOfDSPorts > 27)
        return TRUE;// Not a valid register

    // Set OUT params

    *MaxSlotNum = (UINT8)BridgMap.NumOfDSPorts;
    
    if(!_BitScanForward(&BitScanRes, BridgMap.CIOPortMap))// No DS bridge which is CIO port
        return FALSE;

    *CIOSlot = (UINT8)BitScanRes;
    return TRUE;
}//GetCIOSlotByDevId

#define TBT_LEGACY_SUB_SYS_ID 0x11112222

BOOLEAN
IsLegacyDevice(
    IN        UINT8  Bus
)
{
    UINT32 SID;
	UINT8  SIDRegister;
	UINT16 DevId = SmiPci16 (0x00, Bus, 0x00, 0x00, PCI_DID);
    switch(DevId) // For known device IDs
    {
    case 0x1513:
    case 0x151A:
    case 0x151B:
		TRACE((-1, "TbtSmm.c: Legacy device %x...\n", DevId));
        return TRUE; // Legacy device by Device Id
    }

	SIDRegister = FindSSID_SSVIDHeader(Bus);

    if(!SIDRegister)
        return TRUE; // May be absent for legacy devices

    // Go to register
    SIDRegister += 0x4;

    SID = SmiPci32  (0x00, Bus, 0x00, 0x00, SIDRegister);
	TRACE((-1, "TbtSmm.c: SSID of device is %x...\n", SID));

	return TBT_LEGACY_SUB_SYS_ID == SID || 0 == SID;
}//IsLegacyDevice

BOOLEAN
ConfigureEP(
	IN		INT8  Depth, 
    IN    OUT UINT8* Bus,
    IN    OUT PortInfo* pi
)
{
    UINT8     SBus;
    UINT8     CIOSlot = 4;
    UINT8     MaxSlotNum = 7;
    UINT8     MaxPHYSlots;
    UINT8     UsedBusNumbers;
	UINT8     cmd;
    BOOLEAN   CIOSlotPresent;
    BOOLEAN   Continue;
	PortInfo  portInfo = *pi;

    // Based on Device ID assign CIO slot and max number of PHY slots to scan
    CIOSlotPresent = GetCIOSlotByDevId(*Bus, &CIOSlot, &MaxSlotNum);
    MaxPHYSlots = MaxSlotNum;// Correct if CIO slot is absent
	// Check whether EP already configured by examining CMD register
	cmd = SmiPci8  (0x00, *Bus, 0x00, 0x00, PCI_CMD);
#if !defined TBT_PCIBUS_SKIP || TBT_PCIBUS_SKIP == 0
    if(IsFirstEnterFlag) cmd &= 0;  //AMI_OVERWRITE
#endif
	if(cmd & CMD_BUS_MASTER) // Yes no need to touch this EP, just move to next one in chain
	{
		UINT8  CIOBus = *Bus + 1;
		if(!CIOSlotPresent)// CIO slot is not present in EP, just return FALSE
		{
			//PrintCPStr("BMF");
            TRACE((-1, "TbtSmm.c: CIO slot is not present in EP, just return FALSE.\n"));
			return FALSE;
		}
		// Take all resources from CIO slot and return
		pi->BusNumLimit = SmiPci8  (0x00, CIOBus, CIOSlot, 0x00, PCI_SUBUS);
		pi->IOBase		= SmiPci8  (0x00, CIOBus, CIOSlot, 0x00, PCI_IOBASE);
		pi->IOLimit		= SmiPci8  (0x00, CIOBus, CIOSlot, 0x00, PCI_IOLIMIT);
		pi->MemBase		= SmiPci16 (0x00, CIOBus, CIOSlot, 0x00, PCI_MEMBASE);
		pi->MemLimit	= SmiPci16 (0x00, CIOBus, CIOSlot, 0x00, PCI_MEMLIMIT);
        pi->PMemBase64	= SmiPci16 (0x00, CIOBus, CIOSlot, 0x00, PCI_PRE_MEMBASE) & 0xFFF0;
        pi->PMemLimit64	= SmiPci16 (0x00, CIOBus, CIOSlot, 0x00, PCI_PRE_MEMLIMIT) & 0xFFF0;
        pi->PMemBase64 |= SmiPci32 (0x00, CIOBus, CIOSlot, 0x00, PCI_PRE_MEMBASE_U) << 16;
        pi->PMemLimit64|= SmiPci32 (0x00, CIOBus, CIOSlot, 0x00, PCI_PRE_MEMLIMIT_U) << 16;
        pi->PMemLimit64|= 0xF;
		// Jump to next EP
		*Bus = SmiPci8  (0x00, CIOBus, CIOSlot, 0x00, PCI_SBUS);
		// Should we continue?
		Continue = 0xFFFF != SmiPci16 (0x00, *Bus, 0x00, 0x00, PCI_DID);
		return Continue;
	}

	//Set is legacy dvice
	isLegacyDevice = IsLegacyDevice(*Bus);

    SetCIOPortResources(*Bus, 0, // Assign all available resources to US port of EP
		*Bus + 1, pi->BusNumLimit, 0, pi);

    SBus = *Bus + 1;// Jump to DS port 

    if(CIOSlotPresent)
        MaxPHYSlots = CIOSlot;

	UsedBusNumbers = ConfigureSlot(SBus, MaxPHYSlots, Depth, pi);

    if(!CIOSlotPresent)
        return FALSE; // Stop resource assignment on this chain

    // Set rest of slots us unused
    SetSlotsAsUnused(SBus, MaxSlotNum, CIOSlot, pi);

    SetCIOPortResources(SBus, CIOSlot,
        SBus + UsedBusNumbers + 1,
		pi->BusNumLimit, &portInfo, pi);

    *Bus = SBus + UsedBusNumbers + 1;// Go to next EP

    if (*Bus > pi->BusNumLimit - 2) // In case of bus numbers are exhausted stop enumeration
        return FALSE;

    // BIOS support of Thunderbolt devices Specification Update
    // Revision 0.94
    // This OPTIONAL workaround sholud be disable by default

    // BIOS support of Thunderbolt devices Specification Update
    // Revision 0.92
    // OPTIONAL workaround for devices that don't support surprise-removal
    // If SMI Handler cannot find any device behind a CIO port it means no more TBT devices 
    // attached to this PCIe sub-tree.  In this case BIOS should set MBASE = MLIMIT.
    // Check whether we should continue on this chain
	Continue = 0xFFFF != SmiPci16 (0x00, *Bus, 0x00, 0x00, PCI_DID);
    
	return Continue;
}//ConfigureEP

VOID
GetPortResources(
    IN        UINT8 Bus,
    IN        UINT8 Dev,
    IN    OUT PortInfo* pi
)
{
    pi->BusNumLimit = SmiPci8  (0x00, Bus, Dev, 0x00, PCI_SUBUS);
    pi->IOBase      = SmiPci8  (0x00, Bus, Dev, 0x00, PCI_IOBASE) & 0xF0;
    pi->IOLimit     = SmiPci8  (0x00, Bus, Dev, 0x00, PCI_IOLIMIT) & 0xF0;
    pi->MemBase     = SmiPci16 (0x00, Bus, Dev, 0x00, PCI_MEMBASE) & 0xFFF0;
    pi->MemLimit    = SmiPci16 (0x00, Bus, Dev, 0x00, PCI_MEMLIMIT) & 0xFFF0;
	pi->PMemBase64	= SmiPci16 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE) & 0xFFF0;
	pi->PMemLimit64	= SmiPci16 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT) & 0xFFF0;
	pi->PMemBase64 |= SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMBASE_U) << 16;
	pi->PMemLimit64|= SmiPci32 (0x00, Bus, Dev, 0x00, PCI_PRE_MEMLIMIT_U) << 16;
    pi->IOLimit |= 0xF;
    pi->MemLimit |= 0xF;
    pi->PMemLimit64 |= 0xF;
}//GetPortResources

VOID
ConfigurePort(
    IN        UINT8 Bus,
    IN        UINT8 Dev,
    IN    OUT PortInfo* pi
)
{
	INT8      i;
    UINT8     USBusNum = SmiPci8  (0x00, Bus, Dev, 0x00, PCI_SBUS);

    if (0xFFFF == SmiPci16 (0x00, USBusNum, 0x00, 0x00, PCI_DID))// Nothing to do if TBT device is not connected
        return;
    
    GetPortResources(Bus, Dev, pi);// Take reserved resources from DS port

    // Assign resources to EPs
    for (i = 0; i < MAX_TBT_DEPTH; ++i)
    {
        pi->ConfedEP++;
		if(!ConfigureEP(i, &USBusNum, pi))
            return;
    }
}//ConfigurePort

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ThunderboltSwSmiCallback
//
// Description: This is a TBT software SMI Handler for Porting.
//
// Input:       DispatchHandle  - EFI Handle
//              DispatchContext - Pointer to the EFI_SMM_SW_DISPATCH_CONTEXT
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS ThunderboltSwSmiCallback (
    IN EFI_HANDLE       DispatchHandle,
    IN CONST VOID       *DispatchContext OPTIONAL,
    IN OUT VOID         *CommBuffer OPTIONAL,
    IN OUT UINTN        *CommBufferSize OPTIONAL )
#else
VOID ThunderboltSwSmiCallback (
    IN EFI_HANDLE                   DispatchHandle,
    IN AMI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext)
#endif
{
    PortInfo                portInfo = {0};
    HR_CONFIG               HrConfig = {0};
    UINT8                   i;
    EFI_STATUS              Status;
    EFI_GUID                SetupGuid = SETUP_GUID;
    EFI_GUID                TbtHRStatusGuid = AMI_TBT_HR_STATUS_GUID;
    CHAR16                  TbtHRStatusVar[] = TBT_HR_STATUS_VARIABLE;
    UINTN                   SetupDataSize = sizeof(SETUP_DATA);
    SETUP_DATA              SetupData;
    AMI_TBT_HR_STATUS_DATA  TbtHRStatusData;
    
    TRACE((-1, "TbtSmm.c: Thunderbolt SWSMI Callback Function Entry !!!\n"));
    Status = pRS->GetVariable(L"Setup", &SetupGuid, NULL, &SetupDataSize, &SetupData);
    if (!EFI_ERROR(Status)){
        SmiStall((UINTN)(SetupData.TbtSwSMIDelay * 1000));
    }
    
    // Workaround for synchronizing cache line size of Thunderbolt
    if (gCacheLineSize != SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, R_PCH_PCIE_CLS))
        SmiPci8AndThenOr( 0x00, gTbtBus, gTbtDev, gTbtFun, R_PCH_PCIE_CLS, 0x00, gCacheLineSize);
    
    SMI_PROGRESS_CODE (SMM_THUNDERBOLT_CALL);
    
    PortInfoInit(&portInfo);

    if (!InitializeHostRouter(&HrConfig)){
        SMI_PROGRESS_CODE (0xCB); //Cable is unplugged
        
        // BIOS support of Thunderbolt devices Specification Update
        // Revision 0.90
        // BIOS should remember whether HR is active(it is active 
        // when cable is connected)
        
        // HR status is setted inactive
        TbtHRStatusData.TbtHRStatus = 0;
        if ((HostDeviceId == 0x1547) || (HostDeviceId == 0x1548)) {
            TbtHRStatusData.TbtHRSeries = Cactus_Ridge;
        } else if ((HostDeviceId == 0x1567) || (HostDeviceId == 0x1569)) {
            TbtHRStatusData.TbtHRSeries = Redwood_Ridge;
        } else if ((HostDeviceId == 0x156B) || (HostDeviceId == 0x156D)){
            TbtHRStatusData.TbtHRSeries = Falcon_Ridge;
        } else {
            TbtHRStatusData.TbtHRSeries = BDW_TBT_LP;
        }
        
        Status = pRS->SetVariable( TbtHRStatusVar, \
                                 &TbtHRStatusGuid, \
                                 AmiTbtHrStatusAttribute, \
                                 sizeof(AMI_TBT_HR_STATUS_DATA), \
                                 &TbtHRStatusData );

#if !defined TBT_PCIBUS_SKIP || TBT_PCIBUS_SKIP == 0
        if(IsFirstEnterFlag) IsFirstEnterFlag = 0;
#endif
        TRACE((-1, "TbtSmm.c: Thunderbolt SWSMI Callback Function Exit !!!\n"));
        return SMM_CHILD_DISPATCH_SUCCESS;
    }
    
    // Configure DS ports
	for(i = HrConfig.MinDSNumber; i <= HrConfig.MaxDSNumber; ++i)
    {
		ConfigurePort(HrConfig.HRBus + 1, i, &portInfo);
    }
    SMI_PROGRESS_CODE (SMM_THUNDERBOLT_CALL + 2 + portInfo.ConfedEP); //PostCode = 0xAC + # of connected EP
    
    // BIOS support of Thunderbolt devices Specification Update
    // Revision 0.90
    // BIOS should remember whether HR is active(it is active 
    // when cable is connected)
    // HR status is setted active
    HostDeviceId  = SmiPci16 (0x00, HrConfig.HRBus, 0x00, 0x00, PCI_DID);
    TRACE((-1, "TbtSmm.c: Get Thunderbolt Host Device ID %x in SWSMI from Bus:%x \n", HostDeviceId, HrConfig.HRBus));
    if ((HostDeviceId == 0x1547) || (HostDeviceId == 0x1548))
        TbtHRStatusData.TbtHRSeries = Cactus_Ridge;
    if ((HostDeviceId == 0x1567) || (HostDeviceId == 0x1569))
        TbtHRStatusData.TbtHRSeries = Redwood_Ridge;
    if ((HostDeviceId == 0x156B) || (HostDeviceId == 0x156D))
        TbtHRStatusData.TbtHRSeries = Falcon_Ridge;
    if (HostDeviceId == 0x157E)
        TbtHRStatusData.TbtHRSeries = BDW_TBT_LP;
               
    TbtHRStatusData.TbtHRStatus = 1;
    Status = pRS->SetVariable( TbtHRStatusVar, \
                               &TbtHRStatusGuid, \
                               AmiTbtHrStatusAttribute, \
                               sizeof(AMI_TBT_HR_STATUS_DATA), \
                               &TbtHRStatusData );
    
#if !defined TBT_PCIBUS_SKIP || TBT_PCIBUS_SKIP == 0
    if(IsFirstEnterFlag) IsFirstEnterFlag = 0;
#endif
    TRACE((-1, "TbtSmm.c: Thunderbolt SWSMI Callback Function Exit !!!\n"));
    return SMM_CHILD_DISPATCH_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   TbtPowerButtonCallback
//
// Description: The following flow should be performed as a last step before
//              instructing the platform to enter Sx state:
//                 BIOS should assert GO2Sx pin
//              That will trigger Host Router to prepare underlying devices
//                 BIOS should poll OK2GO2SX_N_OD pin
//              Upon completion of all preparations, Host Router will assert
//              this pin to indicate readiness for Sx entry
//              At this stage BIOS should continue with legacy Sx entry steps
//
// Input:       DispatchHandle   - SMI dispatcher handle
//              *DispatchContext - Pointer to the dispatch context
//
// Output:      Nothing
//
// Notes:       This function does not need to put the system to sleep.  This is
//              handled by PutToSleep.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS TbtPowerButtonCallback (
    IN EFI_HANDLE       DispatchHandle,
    IN CONST VOID       *DispatchContext OPTIONAL,
    IN OUT VOID         *CommBuffer OPTIONAL,
    IN OUT UINTN        *CommBufferSize OPTIONAL )
#else
VOID TbtPowerButtonCallback (
    IN EFI_HANDLE                               DispatchHandle,
    IN AMI_SMM_POWER_BUTTON_DISPATCH_CONTEXT    *DispatchContext)
#endif
{
    EFI_STATUS     Status;
    UINT8          UpPortBus;
    UINT8          RegVal8;
    UINT16         PresenceFlag = 0;
    UINT8          SlotStatusCapOffset;
    UINT8          PowerManagerCapOffset;
#if defined TBT_HR_SX_CHECK && TBT_HR_SX_CHECK == 1
    UINT32         Attributes;
    UINTN          TbtHRStatusDataSize = sizeof(AMI_TBT_HR_STATUS_DATA);
    CHAR16         TbtHRStatusVar[]    = TBT_HR_STATUS_VARIABLE;
    EFI_GUID       TbtHRStatusGuid     = AMI_TBT_HR_STATUS_GUID;
    AMI_TBT_HR_STATUS_DATA  TbtHRStatusData;

    // Double check Thunderbolt Host status
    SlotStatusCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PCIEXP);
    PresenceFlag = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (SlotStatusCapOffset + 0x1A));

    Status = pRS->GetVariable(TbtHRStatusVar, &TbtHRStatusGuid, &Attributes, &TbtHRStatusDataSize, &TbtHRStatusData);
    if (!EFI_ERROR(Status)){
        TRACE((-1, "Thunderbolt Presence bit on PCIE root port%x :%x(Bit06)\n", gTbtFun, PresenceFlag));
        if (TbtHRStatusData.TbtHRStatus){
            if ((PresenceFlag & B_PCH_PCIE_SLSTS_PDS) == 0){
                // Host route status record is active but no device connect actually
                TbtHRStatusData.TbtHRStatus = 0;
                //TbtHRStatusData.TbtHRSeries = HostDeviceId;
                Status = pRS->SetVariable( TbtHRStatusVar, \
                                           &TbtHRStatusGuid, \
                                           Attributes, \
                                           sizeof(AMI_TBT_HR_STATUS_DATA), \
                                           &TbtHRStatusData );
                TRACE((-1, "Host route status record is active but no device connect actually !!!\nReset Thunderbolt Host state %r\n", Status));
            }
        } else {
            if ((PresenceFlag & B_PCH_PCIE_SLSTS_PDS) != 0){
                // Host route status record is inactive but device connect actually
                TbtHRStatusData.TbtHRStatus = 1;
                //TbtHRStatusData.TbtHRSeries = HostDeviceId;
                Status = pRS->SetVariable( TbtHRStatusVar, \
                                           &TbtHRStatusGuid, \
                                           Attributes, \
                                           sizeof(AMI_TBT_HR_STATUS_DATA), \
                                           &TbtHRStatusData );
                TRACE((-1, "Host route status record is inactive but device connect actually !!!\nReset Thunderbolt Host state %r\n", Status));
            }
        } //TbtHRStatus
    } // !EFI_ERROR(Status)
#endif

    if (!gTbtWakeupSupport){
        // System does not support wake from Thunderbolt device
        if ((HostDeviceId == 0x1547) || (HostDeviceId == 0x1548)){
            // Sleep entry flow for Cactus Ridge chip
#if defined TBT_HR_PWR && (TBT_HR_PWR != 0xFF)
            if (gTbtHandlePOC){
                Status = PowerOffPOC();
                TRACE((-1, "TbtSmm.c: Cut off Thunderbolt POC power %r !!!\n", Status));
                ASSERT_EFI_ERROR(Status);
            }
            else
#endif
                return SMM_CHILD_DISPATCH_UNSUPPORTED;
        } else {
            // Sleep entry flow for Redwood Ridge / Falcon Ridge / BDW-TBT-LP chip 
            // when system doesn't support thunderbolt wake function
            
            // put PCIE root port power state back to D0
            PowerManagerCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PMI);
            RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
            RegVal8 &= B_PCH_PCIE_PMCS_PS;
            SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
            
            // BIOS support of Thunderbolt devices Specification Update Revision 1.4
            // 2.3.2.2 Sx Entry flow for RR Hosts without Thunderbolt wake support
            UpPortBus = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, PCI_SBUS);
            if (!TbtSetPCIe2TBTCommand(UpPortBus, 0, TBT_GO2SX_NO_WAKE, 0x8FFFF)){
                // restore PCIE root port power state back to D3
                RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                RegVal8 |= B_PCH_PCIE_PMCS_PS;
                SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                
                return SMM_CHILD_DISPATCH_NO_MEDIA;
            }
            
            // restore PCIE root port power state back to D3
            RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
            RegVal8 |= B_PCH_PCIE_PMCS_PS;
            SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
        }
    } else {
        // System support wake from Thunderbolt device
        if ((HostDeviceId == 0x1547) || (HostDeviceId == 0x1548)){
            // BIOS support of Thunderbolt devices Specification Update
            // Revision 0.85
            // 5. System power state (Sx) handing
            // For Cactus Ridge (CR) Host Router component, those GPIO's 
            // should be connected to the following pins:
            // 1. GPIO_2__GO2SX - active high
            // 2. GPIO_9__OK2GO2SX_N_OD - active low
            Status = ActiveTbtGpio2();
            ASSERT_EFI_ERROR(Status);
            
            Status = PollTbtGpio9();
            ASSERT_EFI_ERROR(Status);
        } else {
            // Sleep entry flow for Redwood Ridge / Falcon Ridge / BDW-TBT-LP chip 
            // when system support thunderbolt wake function
            //
            // Thunderbolt BIOS Implementation guide for Redwood Ridge/Falcon Ridge/BDW-TBT-LP based 
            // devices Specification Update Revision 1.0
            // 2.2.2.3 Sx Entry Flow for Host with Add-In Card support
            
#if !defined TBT_HR_SX_CHECK || TBT_HR_SX_CHECK == 0
            SlotStatusCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PCIEXP);
            PresenceFlag = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (SlotStatusCapOffset + 0x1A));
#endif      
            if (gTbtAICSupport){
                if((PresenceFlag & B_PCH_PCIE_SLSTS_PDS) != 0){
                    // put PCIE root port power state back to D0
                    PowerManagerCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PMI);
                    RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                    RegVal8 &= B_PCH_PCIE_PMCS_PS;
                    SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                    
                    // Get Thunderbolt host location
                    UpPortBus = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, PCI_SBUS);
                    if (!TbtSetPCIe2TBTCommand(UpPortBus, 0, TBT_GO2SX_WITH_WAKE, 0x8FFFF)){
                        // restore PCIE root port power state back to D3
                        RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                        RegVal8 |= B_PCH_PCIE_PMCS_PS;
                        SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                        
                        return SMM_CHILD_DISPATCH_TIMEOUT;
                    }
                    
                    // restore PCIE root port power state back to D3
                    RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                    RegVal8 |= B_PCH_PCIE_PMCS_PS;
                    SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                }
            }// end of AIC support
            
            //
            // BIOS support of Thunderbolt devices Specification Update Revision 1.4
            // 2.3.2.1 Sx Entry flow for RR/FR/WR Hosts with Thunderbolt wake support
            
            //UpPortBus = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, PCI_SBUS);
            //if (!TbtSetPCIe2TBTCommand(UpPortBus, 0, TBT_GO2SX_WITH_WAKE, 0x8FFFF))
            //   return SMM_CHILD_DISPATCH_NO_MEDIA;
        }
    }
    TRACE((-1, "TbtSmm: TbtPowerButtonCallback() !!!\n"));
    return SMM_CHILD_DISPATCH_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   TbtGo2SxCallback
//
// Description: The following flow should be performed as a last step before
//              instructing the platform to enter Sx state:
//                 BIOS should assert GO2Sx pin
//              That will trigger Host Router to prepare underlying devices
//                 BIOS should poll OK2GO2SX_N_OD pin
//              Upon completion of all preparations, Host Router will assert
//              this pin to indicate readiness for Sx entry
//              At this stage BIOS should continue with legacy Sx entry steps
//
// Input:       DispatchHandle   - SMI dispatcher handle
//              *DispatchContext - Pointer to the dispatch context
//
// Output:      Nothing
//
// Notes:       This function does not need to put the system to sleep.  This is
//              handled by PutToSleep.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS TbtGo2SxCallback (
  IN EFI_HANDLE       DispatchHandle,
  IN CONST VOID       *DispatchContext OPTIONAL,
  IN OUT VOID         *CommBuffer OPTIONAL,
  IN OUT UINTN        *CommBufferSize OPTIONAL )
#else
VOID TbtGo2SxCallback (
  IN EFI_HANDLE                   DispatchHandle,
  IN EFI_SMM_SX_DISPATCH_CONTEXT  *DispatchContext )
#endif
{
    EFI_STATUS     Status;
    UINT8          UpPortBus;
    UINT8          RegVal8;
    UINT16         PresenceFlag = 0;
    UINT8          SlotStatusCapOffset;
    UINT8          PowerManagerCapOffset;
#if defined TBT_HR_SX_CHECK && TBT_HR_SX_CHECK == 1
    UINT32         Attributes;
    UINTN          TbtHRStatusDataSize = sizeof(AMI_TBT_HR_STATUS_DATA);
    CHAR16         TbtHRStatusVar[]    = TBT_HR_STATUS_VARIABLE;
    EFI_GUID       TbtHRStatusGuid     = AMI_TBT_HR_STATUS_GUID;
    AMI_TBT_HR_STATUS_DATA  TbtHRStatusData;

    // Double check Thunderbolt Host status
    SlotStatusCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PCIEXP);
    PresenceFlag = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (SlotStatusCapOffset + 0x1A));

    Status = pRS->GetVariable(TbtHRStatusVar, &TbtHRStatusGuid, &Attributes, &TbtHRStatusDataSize, &TbtHRStatusData);
    if (!EFI_ERROR(Status)){
        TRACE((-1, "Thunderbolt Presence bit on PCIE root port%x :%x(Bit06)\n", gTbtFun, PresenceFlag));
        if (TbtHRStatusData.TbtHRStatus){
            if ((PresenceFlag & B_PCH_PCIE_SLSTS_PDS) == 0){
                // Host route status record is active but no device connect actually
                TbtHRStatusData.TbtHRStatus = 0;
                //TbtHRStatusData.TbtHRSeries = HostDeviceId;
                Status = pRS->SetVariable( TbtHRStatusVar, \
                                           &TbtHRStatusGuid, \
                                           Attributes, \
                                           sizeof(AMI_TBT_HR_STATUS_DATA), \
                                           &TbtHRStatusData );
                TRACE((-1, "Host route status record is active but no device connect actually !!!\nReset Thunderbolt Host state %r\n", Status));
            }
        } else {
            if ((PresenceFlag & B_PCH_PCIE_SLSTS_PDS) != 0){
                // Host route status record is inactive but device connect actually
                TbtHRStatusData.TbtHRStatus = 1;
                //TbtHRStatusData.TbtHRSeries = HostDeviceId;
                Status = pRS->SetVariable( TbtHRStatusVar, \
                                           &TbtHRStatusGuid, \
                                           Attributes, \
                                           sizeof(AMI_TBT_HR_STATUS_DATA), \
                                           &TbtHRStatusData );
                TRACE((-1, "Host route status record is inactive but device connect actually !!!\nReset Thunderbolt Host state %r\n", Status));
            }
        } //TbtHRStatus
    } // !EFI_ERROR(Status)
#endif

    if (!gTbtWakeupSupport){
        // System does not support wake from Thunderbolt device
        if ((HostDeviceId == 0x1547) || (HostDeviceId == 0x1548)){
            // Sleep entry flow for Cactus Ridge chip
#if defined TBT_HR_PWR && (TBT_HR_PWR != 0xFF)
            if (gTbtHandlePOC){
                Status = PowerOffPOC();
                TRACE((-1, "TbtSmm.c: Cut off Thunderbolt POC power %r !!!\n", Status));
                ASSERT_EFI_ERROR(Status);
            }
            else
#endif
                return SMM_CHILD_DISPATCH_UNSUPPORTED;
        } else {
            // Sleep entry flow for Redwood Ridge / Falcon Ridge / BDW-TBT-LP chip 
            // when system doesn't support thunderbolt wake function
            
            // put PCIE root port power state back to D0
            PowerManagerCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PMI);
            RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
            RegVal8 &= B_PCH_PCIE_PMCS_PS;
            SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
            
            // BIOS support of Thunderbolt devices Specification Update Revision 1.4
            // 2.3.2.2 Sx Entry flow for RR Hosts without Thunderbolt wake support
            UpPortBus = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, PCI_SBUS);
            if (!TbtSetPCIe2TBTCommand(UpPortBus, 0, TBT_GO2SX_NO_WAKE, 0x8FFFF)){
                // restore PCIE root port power state back to D3
                RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                RegVal8 |= B_PCH_PCIE_PMCS_PS;
                SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                
                return SMM_CHILD_DISPATCH_NO_MEDIA;
            }
            
            // restore PCIE root port power state back to D3
            RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
            RegVal8 |= B_PCH_PCIE_PMCS_PS;
            SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
        }
    } else {
        // System support wake from Thunderbolt device
        if ((HostDeviceId == 0x1547) || (HostDeviceId == 0x1548)){
            // BIOS support of Thunderbolt devices Specification Update
            // Revision 0.85
            // 5. System power state (Sx) handing
            // For Cactus Ridge (CR) Host Router component, those GPIO's 
            // should be connected to the following pins:
            // 1. GPIO_2__GO2SX - active high
            // 2. GPIO_9__OK2GO2SX_N_OD - active low
            Status = ActiveTbtGpio2();
            ASSERT_EFI_ERROR(Status);
            
            Status = PollTbtGpio9();
            ASSERT_EFI_ERROR(Status);
        } else {
            // Sleep entry flow for Redwood Ridge / Falcon Ridge / BDW-TBT-LP chip 
            // when system support thunderbolt wake function
            //
            // Thunderbolt BIOS Implementation guide for Redwood Ridge/Falcon Ridge/BDW-TBT-LP based 
            // devices Specification Update Revision 1.0
            // 2.2.2.3 Sx Entry Flow for Host with Add-In Card support
            
#if !defined TBT_HR_SX_CHECK || TBT_HR_SX_CHECK == 0
            SlotStatusCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PCIEXP);
            PresenceFlag = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (SlotStatusCapOffset + 0x1A));
#endif      
            if (gTbtAICSupport){
                if((PresenceFlag & B_PCH_PCIE_SLSTS_PDS) != 0){
                    // put PCIE root port power state back to D0
                    PowerManagerCapOffset = PcieFindCapId(gTbtBus, gTbtDev, gTbtFun, EFI_PCI_CAPABILITY_ID_PMI);
                    RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                    RegVal8 &= B_PCH_PCIE_PMCS_PS;
                    SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                    
                    // Get Thunderbolt host location
                    UpPortBus = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, PCI_SBUS);
                    if (!TbtSetPCIe2TBTCommand(UpPortBus, 0, TBT_GO2SX_WITH_WAKE, 0x8FFFF)){
                        // restore PCIE root port power state back to D3
                        RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                        RegVal8 |= B_PCH_PCIE_PMCS_PS;
                        SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                        
                        return SMM_CHILD_DISPATCH_TIMEOUT;
                    }
                    
                    // restore PCIE root port power state back to D3
                    RegVal8 = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04));
                    RegVal8 |= B_PCH_PCIE_PMCS_PS;
                    SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, (PowerManagerCapOffset + 0x04)) = RegVal8;
                }
            }// end of AIC support
            
            //
            // BIOS support of Thunderbolt devices Specification Update Revision 1.4
            // 2.3.2.1 Sx Entry flow for RR/FR/WR Hosts with Thunderbolt wake support
            
            //UpPortBus = SmiPci8(0x00, gTbtBus, gTbtDev, gTbtFun, PCI_SBUS);
            //if (!TbtSetPCIe2TBTCommand(UpPortBus, 0, TBT_GO2SX_WITH_WAKE, 0x8FFFF))
            //   return SMM_CHILD_DISPATCH_NO_MEDIA;
        }
    }
    
    return SMM_CHILD_DISPATCH_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InSmmFunction
//
// Description: Installs TBT SMM Child Dispatcher Handler.
//
// Input:       ImageHandle  - Image handle
//              *SystemTable - Pointer to the system table
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS InSmmFunction (
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable )
{
    EFI_STATUS                             Status;
    EFI_HANDLE                             SwHandle = NULL;
    EFI_HANDLE                             hS1Smi   = NULL;
    EFI_HANDLE                             hS3Smi   = NULL;
    EFI_HANDLE                             hS4Smi   = NULL;
    EFI_HANDLE                             hS5Smi   = NULL;
    EFI_HANDLE                             hPBSmi   = NULL;
    AMI_SMM_SW_DISPATCH_PROTOCOL           *SwDispatch;
    AMI_SMM_SX_DISPATCH_PROTOCOL           *SxDispatch;
    AMI_SMM_POWER_BUTTON_DISPATCH_PROTOCOL *PowerButton;
    AMI_SMM_SW_DISPATCH_CONTEXT            SwContext;
    AMI_SMM_SX_DISPATCH_CONTEXT            S1DispatchContext  = {SxS1, SxEntry};
    AMI_SMM_SX_DISPATCH_CONTEXT            S3DispatchContext  = {SxS3, SxEntry};
    AMI_SMM_SX_DISPATCH_CONTEXT            S4DispatchContext  = {SxS4, SxEntry};
    AMI_SMM_SX_DISPATCH_CONTEXT            S5DispatchContext  = {SxS5, SxEntry};
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
    AMI_SMM_POWER_BUTTON_DISPATCH_CONTEXT  PwrContext         = {EfiPowerButtonEntry};
#else
    AMI_SMM_POWER_BUTTON_DISPATCH_CONTEXT  PwrContext         = {PowerButtonEntry};
    EFI_SMM_BASE_PROTOCOL                  *SmmBaseProtocol;
#endif

#if defined ULT_SUPPORT && ULT_SUPPORT == 1
    UINT8 Data;

    if (IsULTPchSeries()){
       // Enable Thunderbolt Hotplug Pin SCI route for ULT platform
       Data = IoRead8(GPIO_BASE_ADDRESS + R_PCH_LP_LPC_GPI_ROUT0 + gTbtHotPlugEvent/8);
       Data = Data & ~(BIT00 << (gTbtHotPlugEvent%8));
       IoWrite8(GPIO_BASE_ADDRESS + R_PCH_LP_LPC_GPI_ROUT0 + gTbtHotPlugEvent/8, Data);
    }
    else{
       // Enable Thunderbolt Hotplug Pin SCI route
       SmiPci8Or( 0x00, 0x00, PCI_DEVICE_NUMBER_PCH_LPC, PCI_FUNCTION_NUMBER_PCH_LPC, (R_PCH_LPC_GPI_ROUT + gTbtHotPlugEvent/4), (0x2 << (gTbtHotPlugEvent%4)*2) );
    }
#else
    // Enable Thunderbolt Hotplug Pin SCI route
    SmiPci8Or( 0x00, 0x00, PCI_DEVICE_NUMBER_PCH_LPC, PCI_FUNCTION_NUMBER_PCH_LPC, (R_PCH_LPC_GPI_ROUT + gTbtHotPlugEvent/4), (0x2 << (gTbtHotPlugEvent%4)*2) );
#endif

    // Presence Detect Changed Enable
    SmiPci8Or( 0x00, gTbtBus, gTbtDev, gTbtFun, R_PCH_PCIE_SLCTL, 0x08);

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)

    Status  = pSmst2->SmmLocateProtocol( &gEfiSmmSwDispatch2ProtocolGuid, \
                                          NULL, \
                                          &SwDispatch );
    if (EFI_ERROR(Status)) return Status;

    Status = pSmst2->SmmLocateProtocol( &gEfiSmmSxDispatch2ProtocolGuid , \
                                       NULL, \
                                       &SxDispatch );
    if (EFI_ERROR(Status)) return Status;

    Status  = pSmst2->SmmLocateProtocol( &gEfiSmmPowerButtonDispatch2ProtocolGuid, \
                                        NULL, \
                                        &PowerButton );
    if (EFI_ERROR(Status)) return Status;
#else
    Status = pBS->LocateProtocol( &gEfiSmmBaseProtocolGuid, \
                                  NULL, \
                                  &SmmBaseProtocol );
    if (EFI_ERROR(Status)) return Status;

    Status = pBS->LocateProtocol( &gEfiSmmSwDispatchProtocolGuid, \
                                  NULL, \
                                  &SwDispatch );
    if (EFI_ERROR(Status)) return Status;

    Status = pBS->LocateProtocol( &gEfiSmmSxDispatchProtocolGuid, \
                                  NULL, \
                                  &SxDispatch );
    if (EFI_ERROR(Status)) return Status;

    Status = pBS->LocateProtocol( &gEfiSmmPowerButtonDispatchProtocolGuid,
                                  NULL,
                                  &PowerButton);
    if (EFI_ERROR(Status)) return Status;
#endif

    SwContext.SwSmiInputValue = TBT_SWSMI_VALUE;
    Status = SwDispatch->Register (
                          SwDispatch,
                          ThunderboltSwSmiCallback,
                          &SwContext,
                          &SwHandle );
    if (EFI_ERROR(Status)) return Status;

    if (gTbtEnable == FALSE){
        TRACE((-1, "TbtSmm.c: Thunderbolt function is disable in Setup.\n"));
        TRACE((-1, "TbtSmm.c: Only register Tbt SwSMI for debug.\n"));
        return EFI_SUCCESS;
    }

    Status = SxDispatch->Register(
                          SxDispatch, \
                          TbtGo2SxCallback, \
                          &S3DispatchContext, \
                          &hS3Smi );
    if (EFI_ERROR(Status)) return Status;

    Status = SxDispatch->Register(
                          SxDispatch, \
                          TbtGo2SxCallback, \
                          &S4DispatchContext, \
                          &hS4Smi );
    if (EFI_ERROR(Status)) return Status;

    Status = SxDispatch->Register(
                          SxDispatch, \
                          TbtGo2SxCallback, \
                          &S5DispatchContext, \
                          &hS5Smi );
    if (EFI_ERROR(Status)) return Status;

    Status = PowerButton->Register( 
                           PowerButton,
                           TbtPowerButtonCallback,
                           &PwrContext,
                           &hPBSmi );
    if (EFI_ERROR(Status)) return Status;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   TbtSmm_Init
//
// Description: Installs TBT SMM Child Dispatcher Handler.
//
// Input:       ImageHandle  - Image handle
//              *SystemTable - Pointer to the system table
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS TbtSmm_Init (
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable )
{
    EFI_STATUS                         Status = EFI_SUCCESS;
    EFI_GUID                           AmiTbtPlatformPolicyGuid = AMI_TBT_PLATFROM_POLICY_PROTOCOL_GUID;
    BOOLEAN                            InSmram = FALSE;
    AMI_TBT_PLATFORM_POLICY_PROTOCOL   *AmiTbtPlatformPolicy = NULL;
    UINTN                              HRStatusSize     = sizeof(AMI_TBT_HR_STATUS_DATA);
    CHAR16                             TbtHRStatusVar[] = TBT_HR_STATUS_VARIABLE;
    AMI_TBT_HR_STATUS_DATA             HRStatusData;

    InitAmiLib(ImageHandle, SystemTable);

    Status = pBS->LocateProtocol( \
                        &AmiTbtPlatformPolicyGuid, \
                        NULL, \
                        &AmiTbtPlatformPolicy );

    if (!EFI_ERROR(Status)) {
      gCacheLineSize          = AmiTbtPlatformPolicy->CacheLineSize;
      gTbtBus                 = AmiTbtPlatformPolicy->Bus;
      gTbtDev                 = AmiTbtPlatformPolicy->Dev;
      gTbtFun                 = AmiTbtPlatformPolicy->Fun;
      gReserveMemoryPerSlot   = AmiTbtPlatformPolicy->ReserveMemoryPerSlot;
      gReservePMemoryPerSlot  = AmiTbtPlatformPolicy->ReservePMemoryPerSlot;
      gReserveIOPerSlot       = AmiTbtPlatformPolicy->ReserveIOPerSlot;
      if (AmiTbtPlatformPolicy->TbtWakeupSupport)
          gTbtWakeupSupport   = TRUE;
      if (AmiTbtPlatformPolicy->TbtAICSupport)
          gTbtAICSupport      = TRUE;
      if (AmiTbtPlatformPolicy->TbtHandlePOC)
          gTbtHandlePOC       = TRUE;
      gTbtHotPlugEvent        = AmiTbtPlatformPolicy->TbtHotPlugEvt;
      if (AmiTbtPlatformPolicy->TbtIOresourceEnable)
          gTbtIOresourceEnable = TRUE;
      gTbtNVMversion          = AmiTbtPlatformPolicy->TbtNVMversion;
      if (AmiTbtPlatformPolicy->TbtEnable)
          gTbtEnable           = TRUE;

      // Convert slot resource to register format
      gReserveMemoryPerSlot  <<= 4;
      gReservePMemoryPerSlot <<= 4;
      gReserveIOPerSlot      <<= 2;
      TRACE((-1, "TbtSmm.c: gReserveMemoryPerSlot  = %x\n", gReserveMemoryPerSlot));
      TRACE((-1, "TbtSmm.c: gReservePMemoryPerSlot = %x\n", gReservePMemoryPerSlot));
      TRACE((-1, "TbtSmm.c: gReserveIOPerSlot      = %x\n", gReserveIOPerSlot));
    }

    // Init Tbt Host Information in SMM RAM
    Status = pRS->GetVariable( L"TbtHRStatusVar", \
                               &TbtHRStatusGuid, \
                               &AmiTbtHrStatusAttribute, \
                               &HRStatusSize, \
                               &HRStatusData );
    if (!EFI_ERROR(Status)){
       if (HRStatusData.TbtHRSeries == Cactus_Ridge) HostDeviceId = 0x1548;
       else if (HRStatusData.TbtHRSeries == Redwood_Ridge) HostDeviceId = 0x1567;
       else if (HRStatusData.TbtHRSeries == Falcon_Ridge) HostDeviceId = 0x156B;
       else HostDeviceId = 0x157E;
    }

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
    Status = pBS->LocateProtocol( \
                    &gEfiSmmBase2ProtocolGuid, \
                    NULL, \
                    &gSmmBase2 );

    if (!EFI_ERROR(Status))
    {
        Status = gSmmBase2->InSmm(gSmmBase2, &InSmram);
        if ((!EFI_ERROR(Status)) &&
            (InSmram))
        {
            Status = InitAmiSmmLib( ImageHandle, SystemTable );
            if (EFI_ERROR(Status))
                return Status;

            Status = gSmmBase2->GetSmstLocation(gSmmBase2, &pSmst2);
            if (!EFI_ERROR(Status))
            {
                Status = InSmmFunction(ImageHandle, SystemTable);
                return Status;
            }
            else
            {
                pSmst2 = NULL;
            }
        }
        else
        {
            // DXE initialize.
        }
    }

    return EFI_SUCCESS;
#else
    return InitSmmHandler(ImageHandle, SystemTable, InSmmFunction, NULL);
#endif
}
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2014, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************