aboutsummaryrefslogtreecommitdiff
path: root/src/vendorcode/amd/agesa/f12/Legacy/Proc/arch2008.asm
blob: 1c56b86d7697033de158236ddf06c8a5716477a4 (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
;*****************************************************************************
; AMD Generic Encapsulated Software Architecture
;
;  Workfile: arch2008.asm     $Revision: 44324 $    $Date: 2010-12-22 17:16:51 +0800 (Wed, 22 Dec 2010) $
;
; Description: ARCH2008.ASM - AGESA Architecture 2008 Wrapper Template
;
;*****************************************************************************
;
; Copyright (c) 2011, Advanced Micro Devices, Inc.
; All rights reserved.
; 
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;     * Redistributions of source code must retain the above copyright
;       notice, this list of conditions and the following disclaimer.
;     * Redistributions in binary form must reproduce the above copyright
;       notice, this list of conditions and the following disclaimer in the
;       documentation and/or other materials provided with the distribution.
;     * Neither the name of Advanced Micro Devices, Inc. nor the names of 
;       its contributors may be used to endorse or promote products derived 
;       from this software without specific prior written permission.
; 
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;*****************************************************************************

    .XLIST
    INCLUDE agesa.inc
    INCLUDE acwrapg.inc       ; Necessary support file as part of wrapper, including but not limited to segment start/end macros.
    INCLUDE acwrap.inc        ; IBVs may specify host BIOS-specific include files required when building.
    INCLUDE cpcarmac.inc
    INCLUDE bridge32.inc
    .LIST
    .586p
    .mmx


;----------------------------------------------------------------------------
; Local definitions
;----------------------------------------------------------------------------

sOemCallout STRUCT
    FuncName   DD   ?         ; Call out function name
    FuncPtr    DW   ?         ; Call out function pointer
sOemCallout ENDS

sOemEventHandler STRUCT
    ClassCode  DD   ?         ; AGESA event log sub-class code
    FuncPtr    DW   ?         ; Event handler function pointer
sOemEventHandler ENDS

;; A typical legacy BIOS implementation may require the E000 and F000 segments
;; to be cached.
EXE_CACHE_REGION_BASE_0  EQU  0E0000h
EXE_CACHE_REGION_SIZE_0  EQU  20000h

;; In this sample implementation, the B1 and B2 images are placed next to each
;; other in the BIOS ROM to help with the maximization of cached code.
EXE_CACHE_REGION_BASE_1  EQU  AGESA_B1_ADDRESS
EXE_CACHE_REGION_SIZE_1  EQU  40000h

;; The third region is not needed in our example.
EXE_CACHE_REGION_BASE_2  EQU  0
EXE_CACHE_REGION_SIZE_2  EQU  0


;----------------------------------------------------------------------------
;    PERSISTENT SEGMENT
; This segment is required to be present throughout all BIOS execution.
;----------------------------------------------------------------------------

AMD_PERSISTENT_START


;----------------------------------------------------------------------------
; Instantiate the global descriptor table
;----------------------------------------------------------------------------

AMD_BRIDGE_32_GDT AMD_GDT         ; Instantiate the global descriptor table
                                  ; required by the push-high mechanism.


;----------------------------------------------------------------------------
; Declare the external routines required in the persistent segment
;----------------------------------------------------------------------------

;+-------------------------------------------------------------------------
;
;   AmdDfltRet
;
;   Entry:
;     None
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     Near stub procedure.  Simply perform a retn instruction.
;
EXTERN AmdDfltRet:NEAR


;+-------------------------------------------------------------------------
;
;   AmdDfltRetFar
;
;   Entry:
;     None
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     Far stub procedure.  Simply perform a retf instruction.
;
EXTERN AmdDfltRetFar:FAR


;----------------------------------------------------------------------------
; Declare the optional external routines in the persistent segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   myModuleTypeMismatchHandler (Example)
;
;   Entry:
;     ESI - Pointer to the EVENT_PARAMS structure of the failure.
;     [ESI].DataParam1 - Socket
;     [ESI].DataParam2 - DCT
;     [ESI].DataParam3 - Channel
;     [ESI].DataParam4 - 0x00000000
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This procedure can be used to react to a memory module type
;     mismatch error discovered by the AGESA code.  Actions taken
;     may include, but are not limited to:
;       Logging the event to NV for display later
;       Reset, excluding the mismatch on subsequent reboot
;       Do nothing
;
;   Dependencies:
;     None
;
EXTERN myModuleTypeMismatchHandler(AmdDfltRet):NEAR

;+---------------------------------------------------------------------------
;
;   oemPlatformConfigInit (Optional)
;
;   Entry:
;     EDI - 32-bit flat pointer to the PLATFORM_CONFIGURATION to be
;           passed in to the next AGESA entry point.
;
;           typedef struct {
;             IN PERFORMANCE_PROFILE PlatformProfile;
;             IN CPU_HT_DEEMPHASIS_LEVEL *PlatformDeemphasisList;
;             IN UINT8               CoreLevelingMode;
;             IN PLATFORM_C1E_MODES  C1eMode;
;             IN UINT32              C1ePlatformData;
;             IN UINT32              C1ePlatformData1;
;             IN UINT32              C1ePlatformData2;
;             IN UINT32              C1ePlatformData3;
;             IN BOOLEAN             UserOptionDmi;
;             IN BOOLEAN             UserOptionPState;
;             IN BOOLEAN             UserOptionSrat;
;             IN BOOLEAN             UserOptionSlit;
;             IN BOOLEAN             UserOptionWhea;
;             IN UINT32              PowerCeiling;
;             IN BOOLEAN             PstateIndependent;
;           } PLATFORM_CONFIGURATION;
;
;           typedef struct {
;             IN UINT8 Socket;
;             IN UINT8 Link;
;             IN UINT8 LoFreq;
;             IN UINT8 HighFreq;
;             IN PLATFORM_DEEMPHASIS_LEVEL     ReceiverDeemphasis;
;             IN PLATFORM_DEEMPHASIS_LEVEL     DcvDeemphasis;
;           } CPU_HT_DEEMPHASIS_LEVEL;
;
;           typedef struct {
;             IN PLATFORM_CONTROL_FLOW PlatformControlFlowMode;
;             IN BOOLEAN               UseHtAssist;
;             IN BOOLEAN               UseAtmMode;
;             IN BOOLEAN               Use32ByteRefresh;
;             IN BOOLEAN               UseVariableMctIsocPriority;
;           } PERFORMANCE_PROFILE;
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     Provide a single hook routine to modify the parameters of a
;     PLATFORM_CONFIGURATION structure before any entry point that
;     has such a structure as an input.
;
;   Dependencies:
;     None
;
;   Example:
;     If your platform is running in UMA mode, the following code
;     may be added:
;       mov  (PLATFORM_CONFIGURATION PTR [edi]).PlatformProfile.PlatformControlFlowMode, UmaDr
;
EXTERN oemPlatformConfigInit(AmdDfltRetFar):FAR

;+---------------------------------------------------------------------------
;
;   oemCallout (Optional)
;
;   Entry:
;     ECX - Callout function number
;     EDX - Function-specific UINTN
;     ESI - Pointer to function specific data
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;
;   Modified:
;     None
;
;   Purpose:
;     The default call out router function which resides in the same
;     segment as the push-high bridge code.
;
;   Dependencies:
;     None
;
EXTERN oemCallout(AmdDfltRet):NEAR


;----------------------------------------------------------------------------
; Define the sample wrapper routines for the persistent segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   AmdBridge32
;
;   Entry:
;     EDX - A Real Mode FAR pointer using seg16:Offset16 format that
;           points to a local host environment call-out router. If
;           this pointer is not equal to zero, then this pointer is
;           used as the call-out router instead of the standard
;           OemCallout. This may be useful when the call-out router
;           is not located in the same segment as the AmdBridge32 and
;           AmdCallout16 routines.
;     ESI - A Flat Mode pointer (32-bit address) that points to the
;           configuration block (AMD_CONFIG_PARAMS) for the AGESA
;           software function.
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;
;   Modified:
;     None
;
;   Purpose:
;     Execute an AGESA software function through the Push-High interface.
;
;   Dependencies:
;     This procedure requires a stack. The host environment must use the
;     provided service function to establish the stack environment prior
;     to making the call to this procedure.
;
AmdBridge32 PROC FAR PUBLIC
    AMD_BRIDGE_32 AMD_GDT             ; use the macro for the body
    ret
AmdBridge32 ENDP


;+---------------------------------------------------------------------------
;
;   AmdEnableStack
;
;   Entry:
;     BX - Return address
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;     SS:ESP - Points to the private stack location for this processor core.
;     ECX - Upon success, contains this processor core's stack size in bytes.
;
;   Modified:
;     EAX, ECX, EDX, EDI, ESI, ESP, DS, ES
;
;   Purpose:
;     This procedure is used to establish the stack within the host environment.
;
;   Dependencies:
;     The host environment must use this procedure and not rely on any other
;     sources to create the stack region.
;
AmdEnableStack PROC NEAR PUBLIC
    AMD_ENABLE_STACK
    ;; EAX = AGESA_SUCCESS, The stack space has been allocated for this core.
    ;; EAX = AGESA_WARNING, The stack has already been set up.  SS:ESP is set
    ;;       to stack top, and ECX is the stack size in bytes.
    jmp   bx
AmdEnableStack ENDP


;+---------------------------------------------------------------------------
;
;   AmdDisableStack
;
;   Entry:
;     BX - Return address
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;
;   Modified:
;     EAX, ECX, EDX, ESI, ESP
;
;   Purpose:
;     This procedure is used to remove the pre-memory stack from within the
;     host environment.
;     The exit state for the BSP is described as follows:
;       Memory region 00000-9FFFF MTRRS are set as WB memory.
;       Processor Cache is enabled (CD bit is cleared).
;       MTRRs used for execution cache are kept.
;       Cache content is flushed (invalidated without write-back).
;       Any family-specific clean-up done.
;     The exit state for the APs is described as follows:
;       Memory region 00000-9FFFF MTRRS are set as WB memory.
;       Memory region A0000-DFFFF MTRRS are set as UC IO.
;       Memory region E0000-FFFFF MTRRS are set as UC memory.
;       MTRRs used for execution cache are cleared.
;       Processor Cache is disabled (CD bit is set).
;       Top-of-Memory (TOM) set to the system top of memory as determined
;         by the memory initialization routines.
;       System lock command is enabled.
;       Any family-specific clean-up done.
;
;   Dependencies:
;     The host environment must use this procedure and not rely on any other
;     sources to break down the stack region.
;     If executing in 16-bit code, the host environment must establish the
;     "Big Real" mode of 32-bit addressing of data.
;
AmdDisableStack PROC NEAR PUBLIC
    AMD_DISABLE_STACK
    ;; EAX = AGESA_SUCCESS, The stack space has been disabled for this core.
    jmp   bx
AmdDisableStack ENDP


;+---------------------------------------------------------------------------
;
;   AmdCallout16
;
;   Entry:
;     [esp+8] - Func
;     [esp+12] - Data
;     [esp+16] - Configuration Block
;     [esp+4] - Return address to AGESA
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;
;   Modified:
;     None
;
;   Purpose:
;     Execute callback from the push-high interface.
;
;   Dependencies:
;     None
;
AmdCallout16 PROC FAR PUBLIC ; declare the procedure
    AMD_CALLOUT_16 oemCallout ; use the macro for the body
    ret
AmdCallout16 ENDP


;+---------------------------------------------------------------------------
;
;   AmdProcessAgesaErrors (Optional)
;
;   Entry:
;     AL - Heap status of the AGESA entry point that was just invoked.
;     EBX - AGESA image base address.
;     EDX - Segment / Offset of the appropriate callout router function.
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This procedure is used to handle any errors that may have occurred
;     during an AGESA entry point.
;
;   Dependencies:
;     None
;
AmdProcessAgesaErrors PROC FAR PUBLIC
    LOCAL localCpuInterfaceBlock:EVENT_PARAMS

    pushad
    xor   edi, edi
    mov   di, ss
    shl   edi, 4
    lea   esi, localCpuInterfaceBlock
    add   esi, edi

    ; Fill default config block
    mov   (EVENT_PARAMS PTR [esi]).StdHeader.Func, AMD_READ_EVENT_LOG
    mov   (EVENT_PARAMS PTR [esi]).StdHeader.ImageBasePtr, ebx
    mov   (EVENT_PARAMS PTR [esi]).StdHeader.AltImageBasePtr, 0
    mov   (EVENT_PARAMS PTR [esi]).StdHeader.HeapStatus, al
    mov   edi, SEG AmdCallout16
    shl   edi, 4
    add   edi, OFFSET AmdCallout16
    mov   (EVENT_PARAMS PTR [esi]).StdHeader.CalloutPtr, edi

    ; Flush the event log searching for, and handling all monitored events
    xor   eax, eax
    .while (eax == 0)
        push  edx
        call  AmdBridge32
        pop   edx
        .if (eax == AGESA_SUCCESS)
            mov   eax, (EVENT_PARAMS PTR [esi]).EventInfo
            .if (eax != 0)
                lea   di, cs:AgesaEventTable

loopThruTable:
                cmp   di, OFFSET cs:AgesaEventTableEnd
                jae   unhandledEvent

                cmp   eax, cs:[di].sOemEventHandler.ClassCode
                je    FoundMatch
                add   di, SIZEOF sOemEventHandler
                jmp   loopThruTable

FoundMatch:
                mov   bx, cs:[di].sOemEventHandler.FuncPtr
                call  bx

unhandledEvent:
                xor   eax, eax
            .else
                mov   al, 1
            .endif
        .endif
    .endw
    popad
    ret

AmdProcessAgesaErrors ENDP


;----------------------------------------------------------------------------
; Define the error handler table
;----------------------------------------------------------------------------

AgesaEventTable LABEL BYTE
    ;; Add entries as desired
    ;;---------
    ;; EXAMPLE
    ;;---------
    sOemEventHandler <MEM_ERROR_MODULE_TYPE_MISMATCH_DIMM, OFFSET myModuleTypeMismatchHandler>
AgesaEventTableEnd LABEL BYTE


AMD_PERSISTENT_END




;----------------------------------------------------------------------------
;    RECOVERY SEGMENT
; This segment resides in the classic 'boot-block,' and is used
; for recovery.
;----------------------------------------------------------------------------

AMD_RECOVERY_START


;----------------------------------------------------------------------------
; Declare the external routines required in the recovery segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   myReadSPDRecovery (Required for proper recovery mode operation)
;
;   Entry:
;     ESI - Pointer to an AGESA_READ_SPD_PARAMS structure.
;
;     typedef struct {
;       IN OUT AMD_CONFIG_PARAMS StdHeader;
;       IN       UINT8 SocketId;
;       IN       UINT8 MemChannelId;
;       IN       UINT8 DimmId;
;       IN OUT   UINT8 *Buffer;
;       IN OUT   MEM_DATA_STRUCT *MemData;
;     } AGESA_READ_SPD_PARAMS;
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS Indicates the SPD block for the indicated
;                         DIMM was read successfully.
;           AGESA_BOUNDS_CHK The specified DIMM is not present.
;           AGESA_UNSUPPORTED This is a required function, so this
;                             value being returned causes a critical
;                             error response value from the AGESA
;                             software function and no memory initialized.
;           AGESA_ERROR The DIMM SPD read process has generated
;                       communication errors.
;
;   Modified:
;     None
;
;   Purpose:
;     This call out reads a block of memory SPD data and places it
;     into the provided buffer.
;
;   Dependencies:
;     None
;
EXTERN myReadSPDRecovery:NEAR


;----------------------------------------------------------------------------
; Define the sample wrapper routines for the recovery segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   AmdInitResetWrapper
;
;   Entry:
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     A minimal initialization of the processor core is performed. This
;     procedure must be called by all processor cores. The code path
;     separates the BSP from the APs and performs a separate and appropriate
;     list of tasks for each class of core.
;     For the BSP, the following actions are performed:
;       Internal heap sub-system initialization
;       Primary non-coherent HyperTransportT link initialization
;       Return to the host environment to test for Recovery Mode.
;     The AP processor cores do not participate in the recovery process.
;     However, they execute this routine after being released to execute
;     by the BSP during the main boot process. Their actions include the
;     following:
;       Internal heap sub-system initialization
;       Proceed to a wait loop waiting for commands from the BSP
;
;     For the cache regions, up to three regions of execution cache can be
;     allocated following the following rules:
;     1. Once a region is allocated, it cannot be de-allocated. However, it
;        can be expanded.
;     2. At most, two of the three regions can be located above 1 MByte. A
;        region failing this rule is ignored.
;     3. All region addresses must be at or above the 0x000D0000 linear
;        address. A region failing this rule is ignored.
;     4. The address is aligned on a 32-KByte boundary. Starting addresses
;        is rounded down to the nearest 32-Kbyte boundary.
;     5. The execution cache size must be a multiple of 32 KByte. Size is
;        rounded up to the next multiple of 32 KByte.
;     6. A region must not span either the 1-MByte boundary or the 4-GByte
;        boundary. Allocated size is truncated to not span the boundary.
;     7. The granted cached execution regions, address, and size are calculated
;        based on the available cache resources of the processor core.
;        Allocations are made up to the limit of cache available on the
;        installed processor.
;     Warning: Enabling instruction cache outside of this interface can cause
;              data corruption.
;
;   Dependencies:
;     This procedure is expected to be executed soon after a system reset
;     for the main boot path or resume path of execution.
;
;     This procedure requires a stack.
;
;     Because the heap system is not yet operational at the point of the
;     interface call, the host environment must allocate the storage for
;     the AMD_RESET_PARAMS structure before making the first call to
;     AmdCreateStruct.  This is the ByHost method of allocation.
;
AmdInitResetWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS
    local localResetParams:AMD_RESET_PARAMS

    pushad

    ; Prepare for the call to initialize the input parameters for AmdInitReset
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B1_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    ; Use the 'ByHost' allocation method because the heap has not been initialized as of yet.
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_RESET
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, ByHost
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, sizeof AMD_RESET_PARAMS
    lea   edx, localResetParams
    add   edx, eax
    push  edx
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr, edx
    mov   dx, SEG AmdCalloutRouterRecovery
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterRecovery
    push  edx
    call  AmdBridge32
    pop   edx
    pop   esi

    ; The structure has been initialized.  Now modify the default settings as desired.

    ; Allocate the execution cache to maximize the amount of code in ROM that is cached.
    ; Placing the B1 and B2 images near one another is a good way to ensure the AGESA code
    ; is cached.
    mov   (AMD_RESET_PARAMS ptr [esi]).CacheRegion.ExeCacheStartAddr, EXE_CACHE_REGION_BASE_0
    mov   (AMD_RESET_PARAMS ptr [esi]).CacheRegion.ExeCacheSize, EXE_CACHE_REGION_SIZE_0
    mov   (AMD_RESET_PARAMS ptr [esi + sizeof EXECUTION_CACHE_REGION]).CacheRegion.ExeCacheStartAddr, EXE_CACHE_REGION_BASE_1
    mov   (AMD_RESET_PARAMS ptr [esi + sizeof EXECUTION_CACHE_REGION]).CacheRegion.ExeCacheSize, EXE_CACHE_REGION_SIZE_1
    mov   (AMD_RESET_PARAMS ptr [esi + (2 * sizeof EXECUTION_CACHE_REGION)]).CacheRegion.ExeCacheStartAddr, EXE_CACHE_REGION_BASE_2
    mov   (AMD_RESET_PARAMS ptr [esi + (2 * sizeof EXECUTION_CACHE_REGION)]).CacheRegion.ExeCacheSize, EXE_CACHE_REGION_SIZE_2

    ; Call in to the AmdInitReset entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS Early initialization completed successfully.
    ;;  AGESA_WARNING One or more of the execution cache allocation
    ;;                rules were violated, but an adjustment was made
    ;;                and space was allocated.
    ;;  AGESA_ERROR One or more of the execution cache allocation rules
    ;;              were violated, which resulted in a requested cache
    ;;              region to not be allocated.
    ;;              The storage space allocated for the AMD_RESET_PARAMS
    ;;              structure is insufficient.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_RESET_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B1_ADDRESS
        call  AmdProcessAgesaErrors
    .endif


    ;; Here are what the MTRRs should look like based off of the CacheRegions specified above:

    ;; Fixed-Range MTRRs
    ;; Name                 Address         Value
    ;; ----------------     --------        ----------------
    ;; MTRRfix4k_E0000      0000026C        0505050505050505
    ;; MTRRfix4k_E8000      0000026D        0505050505050505
    ;; MTRRfix4k_F0000      0000026E        0505050505050505
    ;; MTRRfix4k_F8000      0000026F        0505050505050505
    ;; MTRRdefType          000002FF        0000000000000C00
    ;;
    ;; Variable-Range MTRRs and IO Range
    ;;       MTRRphysBase(n)             MTRRphysMask(n)
    ;;       -----------------           -----------------
    ;; n=0   0000000000000000            0000000000000000
    ;; n=1   0000000000000000            0000000000000000
    ;; n=2   0000000000000000            0000000000000000
    ;; n=3   0000000000000000            0000000000000000
    ;; n=4   0000000000000000            0000000000000000
    ;; n=5   Heap Base (Varies by core)  0000FFFFFFFF0800
    ;; n=6   AGESA_B1_ADDRESS | 6        0000FFFFFFFC0800
    ;; n=7   0000000000000000            0000000000000000


    ;; Because the allocation method is 'ByHost,' the call to AMD_RELEASE_STRUCT is
    ;; not necessary.  Stack space reclamation is left up to the host BIOS.

    popad
    ret


AmdInitResetWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdInitRecoveryWrapper
;
;   Entry:
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     Perform a minimum initialization of the processor and memory to
;     support a recovery mode flash ROM update.
;     For the BSP, the following actions are performed:
;       Configuration of CPU core for recovery process
;       Minimal initialization of some memory
;     The AP processor cores do not participate in the recovery process.
;     No actions or tasks are performed by the AP cores for this time point.
;
;   Dependencies:
;     This procedure requires a stack. The host environment must use one of
;     the provided service functions to establish the stack environment prior
;     to making the call to this procedure.
;
AmdInitRecoveryWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdInitRecovery
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B1_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_RECOVERY
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PreMemHeap
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterRecovery
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterRecovery
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.


    ; Call in to the AmdInitRecovery entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS The function has completed successfully.
    ;;  AGESA_WARNING One or more of the allocation rules were violated,
    ;;                but an adjustment was made and space was allocated.
    ;;  AGESA_ERROR One or more of the allocation rules were violated,
    ;;              which resulted in a requested cache region to not be
    ;;              allocated.
    ;;  AGESA_FATAL No memory was found in the system.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_RECOVERY_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B1_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    ; Allow AGESA to free the space used by AmdInitRecovery
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32

    popad
    ret
AmdInitRecoveryWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdCalloutRouterRecovery
;
;   Entry:
;     ECX - Callout function number
;     EDX - Function-specific UINTN
;     ESI - Pointer to function specific data
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;
;   Modified:
;     None
;
;   Purpose:
;     The call out router function for AmdInitReset and
;     AmdInitRecovery.
;
;   Dependencies:
;     None
;
AmdCalloutRouterRecovery PROC FAR PUBLIC USES ECX EBX ESI BX DI DS ES
    xor   ax, ax
    mov   ds, ax
    mov   es, ax
    lea   di, cs:CalloutRouterTableRecovery
    mov   eax, AGESA_UNSUPPORTED

loopThruTable:
    cmp   di, OFFSET cs:CalloutRouterTableRecoveryEnd
    jae   amdCpuCalloutExit ; exit with AGESA_UNSUPPORTED
    cmp   ecx, cs:[di].sOemCallout.FuncName
    je    FoundMatch
    add   di, SIZEOF sOemCallout
    jmp   loopThruTable

FoundMatch:
    mov   bx, cs:[di].sOemCallout.FuncPtr
    call  bx

amdCpuCalloutExit:
    ret
AmdCalloutRouterRecovery ENDP


;----------------------------------------------------------------------------
; Define the callout dispatch table for the recovery segment
;----------------------------------------------------------------------------

CalloutRouterTableRecovery LABEL BYTE
    ;; Standard B1 implementations only need the SPD reader call out function to be implemented.
    sOemCallout <AGESA_READ_SPD, OFFSET myReadSPDRecovery>
CalloutRouterTableRecoveryEnd LABEL BYTE


AMD_RECOVERY_END



;----------------------------------------------------------------------------
;    PRE-MEMORY SEGMENT
; This segment must be uncompressed in the ROM image.
;----------------------------------------------------------------------------

AMD_PREMEM_START


;----------------------------------------------------------------------------
; Declare the external routines required in the recovery segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   myReadSPDPremem (Required)
;
;   Entry:
;     ESI - Pointer to an AGESA_READ_SPD_PARAMS structure
;
;     typedef struct {
;       IN OUT AMD_CONFIG_PARAMS StdHeader;
;       IN       UINT8 SocketId;
;       IN       UINT8 MemChannelId;
;       IN       UINT8 DimmId;
;       IN OUT   UINT8 *Buffer;
;       IN OUT   MEM_DATA_STRUCT *MemData;
;     } AGESA_READ_SPD_PARAMS;
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS Indicates the SPD block for the indicated
;                         DIMM was read successfully.
;           AGESA_BOUNDS_CHK The specified DIMM is not present.
;           AGESA_UNSUPPORTED This is a required function, so this
;                             value being returned causes a critical
;                             error response value from the AGESA
;                             software function and no memory initialized.
;           AGESA_ERROR The DIMM SPD read process has generated
;                       communication errors.
;
;   Modified:
;     None
;
;   Purpose:
;     This call out reads a block of memory SPD data and places it
;     into the provided buffer.
;
;   Dependencies:
;     None
;
EXTERN myReadSPDPremem:NEAR

;+-------------------------------------------------------------------------
;
;   AmdDfltRetPremem
;
;   Entry:
;     None
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     Near stub procedure in the prememory segment.  Simply perform a
;     retn instruction.
;
EXTERN AmdDfltRetPremem:NEAR

;+---------------------------------------------------------------------------
;
;   myDoReset (Required)
;
;   Entry:
;     EDX - Reset type
;           1 - Warm reset whenever
;           2 - Cold reset whenever
;           3 - Warm reset immediately
;           4 - Cold reset immediately
;     ESI - Pointer to an AMD_CONFIG_PARAMS structure.
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS The function has completed successfully.
;           AGESA_UNSUPPORTED This is a required function, so this
;                             value being returned causes a critical
;                             error response value from the AGESA
;                             software function.
;
;   Modified:
;     None
;
;   Purpose:
;     This host environment function must initiate the specified type
;     of system reset.
;
;     Implementation of this function by the host environment is
;     REQUIRED. Some host environments may record this as a request
;     allowing other elements in the system to perform some additional
;     tasks before the actual reset is issued.
;
;   Dependencies:
;     The AMD processor contains 3 bits (BiosRstDet[2:0]) in a PCI
;     register (F0x6C Link Initialization Control Register) that
;     indicate the reset status. These bits are reserved for use by
;     the AGESA software and should not be modified by the host
;     environment.
;
EXTERN myDoReset:NEAR


;+---------------------------------------------------------------------------
;
;   myGetNonVolatileS3Context (Required for proper S3 operation)
;
;   Entry:
;     None
;
;   Exit:
;     EBX - Pointer to the non-volatile S3 context block
;     ECX - Size in bytes of the non-volatile S3 context block
;
;   Modified:
;     None
;
;   Purpose:
;     The host environment must return the pointer to the data
;     saved during the mySaveNonVolatileS3Context routine.
;
;   Dependencies:
;     None
;
EXTERN myGetNonVolatileS3Context:NEAR


;----------------------------------------------------------------------------
; Declare the optional external routines in the prememory segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   myAgesaHookBeforeExitSelfRefresh (Optional)
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - 44h
;     ESI - Pointer to a data structure containing the memory information
;
;   Exit:
;     After returning control to AGESA, AGESA will display: - AGESA_TESTPOINT - 45h
;     EAX - Contains the AGESA_STATUS return code
;           AGESA_SUCCESS The function has completed successfully
;           AGESA_UNSUPPORTED This function is not implemented by the host environment
;           AGESA_WARNING A non-critical issue has occued in the host environment
;
;   Modified:
;     None
;
;   Purpose:
;     General purpose hook called before the exiting self refresh
;     This procedure is called once per channel
;
;     Implementation of this function is optional for the host environment
;     This call-out is an opportunity for the host environment to make dynamic
;     modifications to the memory timing settings specific to the board or host
;     environment before exiting self refresh on S3 resume
;
;   Dependencies:
;     This procedure is called before the exit self refresh bit is set in the resume
;     sequence. The host environment must initiate the OS restart process. This procedure
;     requires a stack. The host environment must establish the stack environment prior
;     to making the call to this procedure
;
EXTERN myAgesaHookBeforeExitSelfRefresh(AmdDfltRetPremem):NEAR


;+---------------------------------------------------------------------------
;
;   myHookBeforeDramInit (Optional)
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - 40h
;     ESI - Pointer to a data structure containing the memory information
;
;   Exit:
;     After returning control to AGESA, AGESA will display - AGESA_TESTPOINT - 41h
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS The function has completed successfully.
;           AGESA_UNSUPPORTED This function is not implemented by the host environment
;
;   Modified:
;     None
;
;   Purpose:
;     General-purpose hook called before the DRAM_Init bit is set. Called
;     once per MCT
;
;     Implementation of this function is optional for the host environment
;     This call-out is an opportunity for the host environment to make
;     dynamic modifications to the memory timing settings specific to the
;     board or host environment
;
;   Dependencies:
;     None
;
EXTERN myHookBeforeDramInit(AmdDfltRetPremem):NEAR


;+---------------------------------------------------------------------------
;
;   myHookBeforeDQSTraining (Optional)
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - 42h
;     ESI - Pointer to a data structure containing the memory information.
;
;   Exit:
;     After returning control to AGESA, AGESA will display - AGESA_TESTPOINT - 43h
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS The function has completed successfully.
;           AGESA_UNSUPPORTED This function is not implemented by the
;                             host environment.
;
;   Modified:
;     None
;
;   Purpose:
;     General-purpose hook called just before the memory training processes
;     begin. Called once per MCT.
;
;     Implementation of this function is optional for the host environment.
;     This call-out is an opportunity for the host environment to make
;     dynamic modifications to the memory timing settings specific to the
;     board or host environment.
;
;     The host environment may also use this call-out for some board-
;     specific features that should be activated at this time point,
;     such as:
;       Low voltage DIMMs-the host environment should set the recommended
;       voltages found in the memory data structure for each memory
;       channel. This needs to occur before training begins.
;
;   Dependencies:
;     None
;
EXTERN myHookBeforeDQSTraining(AmdDfltRetPremem):NEAR


;----------------------------------------------------------------------------
; Define the sample wrapper routines for the prememory segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   AmdInitEarlyWrapper
;
;   Entry:
;     On Entry to "AmdInitEarly" AGESA will display AGESA_TESTPOINT - C4h
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Exit from "AmdInitEarly" AGESA will display AGESA_TESTPOINT - C5h
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     A full initialization of the processor is performed. Action details
;     differ for the BSP and AP processor cores.
;     For the BSP, the following actions are performed:
;       Full HyperTransportT link initialization, coherent and non-coherent
;       Processor register loading
;       Microcode patch load
;       Errata workaround processing
;       Launch all processor cores
;       Configure the processor power management capabilities
;       Request a warm reset if needed
;     For the AP, the following actions are performed:
;       Processor register loading
;       Microcode patch load
;       Errata workaround processing
;       Configure the processor power management capabilities
;
;   Dependencies:
;     This procedure is expected to be called before main memory initialization
;     and before the system warm reset. Prior to this, the basic configuration
;     done by the AmdInitReset routine must be completed.
;
;     This procedure requires a stack. The host environment must use one of the
;     provided service functions to establish the stack environment prior to
;     making the call to this procedure.
;
;     The processes performed at this time point require communication between
;     processor cores.
;
;     The host environment must recognize that all processor cores are running
;     in parallel and avoid activities that might interfere with the core-to-core
;     communication, such as modifying the MTRR settings or writing to the APIC
;     registers.
;
AmdInitEarlyWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdInitEarly
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_EARLY
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PreMemHeap
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPremem
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPremem
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, (SIZEOF AMD_CONFIG_PARAMS + (3 * (SIZEOF EXECUTION_CACHE_REGION)))
    call  oemPlatformConfigInit

    ; Call in to the AmdInitEarly entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS The function has completed successfully.
    ;;  AGESA_ALERT An HyperTransportT link CRC error was observed.
    ;;  AGESA_WARNING One of more of the allocation rules were violated,
    ;;                but an adjustment was made and space was allocated.
    ;;                Or a HyperTransport device does not have the expected
    ;;                capabilities, or unusable redundant HyperTransport
    ;;                links were found.
    ;;  AGESA_ERROR One or more of the allocation rules were violated, which
    ;;              resulted in a requested cache region to not be allocated.
    ;;              Or, a HyperTransport device failed to initialize.
    ;;  AGESA_CRITICAL An illegal or unsupported mixture of processor types was
    ;;                 found, or the processors installed were found to have an
    ;;                 insufficient MP capability rating for this platform.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_EARLY_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    ; Allow AGESA to free the space used by AmdInitEarly
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32


    popad
    ret
AmdInitEarlyWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdInitPostWrapper
;
;   Entry:
;     On Entry to "AmdInitPost" AGESA will display AGESA_TESTPOINT - C6h
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Exit from "AmdInitPost" AGESA will display AGESA_TESTPOINT - C7h
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     The main system memory is located, initialized, and brought on-line.
;     The processor(s) are prepared for full operation and control by the
;     host environment. Action details differ for the BSP and AP processor
;     cores.
;     For the BSP, the following actions are performed:
;       Full memory initialization and configuration. BSP is the master for
;       this process and may delegate some tasks to APs.
;       AP collection of data for use later.
;       Transfer the HOBs including the artifact data out of the pre-memory
;       cache storage into a temporary holding buffer in the main memory.
;       Check the BIST status of the BSP
;       Shut down the APs.
;       Prepare for the host environment to begin main boot activity.
;       Disable the pre-memory stack.
;     For the APs, the following actions are performed:
;       Report core identity information.
;       Execute indicated memory initialization processes as directed.
;       Check the BIST status of the AP
;       Disable the pre-memory stack.
;       Prepare to halt, giving control to host environment.
;     The entire range of system memory is enabled for Write-Back cache.
;     The fixed MTRRs and the variable MTRRs[7:6] are not changed in order
;     to leave in place any flash ROM region currently set for Write-Protect
;     execution cache.
;
;   Dependencies:
;     This procedure is called after the host environment has determined that
;     a normal boot to operating system should be performed after any system
;     warm reset is completed and after the configuration done by AmdInitEarly
;     has completed.
;
;     This procedure requires a stack. The host environment must use one of the
;     provided service functions to establish the stack environment prior to
;     making the call to this procedure.
;
;     The processes performed at this time point require communication between
;     processor cores.  The host environment must recognize that all processor
;     cores are running in parallel and avoid activities that might interfere
;     with the core-to-core communication, such as modifying the MTRR settings
;     or writing to the APIC registers.
;
AmdInitPostWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdInitPost
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_POST
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PreMemHeap
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPremem
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPremem
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, SIZEOF AMD_CONFIG_PARAMS
    call  oemPlatformConfigInit

    ; Call in to the AmdInitPost entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS The function has completed successfully.
    ;;  AGESA_ALERT   A BIST error was found on one of the cores.
    ;;  AGESA_WARNING HT Assist feature is running sub-optimally.
    ;;  AGESA_FATAL   Memory initialization failed.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_POST_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    ; Allow AGESA to free the space used by AmdInitPost
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32


    popad
    ret
AmdInitPostWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdInitResumeWrapper
;
;   Entry:
;     On Entry to "AmdInitResume" AGESA will display AGESA_TESTPOINT - D0h
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Exit from "AmdInitResume" AGESA will display AGESA_TESTPOINT - D1h
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This procedure initializes or re-initializes the silicon components
;     for the resume boot path.  For the processor, main memory is brought
;     out of self-refresh mode. This procedure will use the context data
;     in the NvStorage area of the input structure to re-start the main
;     memory.  The host environment must fill the AMD_S3_PARAMS NvStorage
;     and VolatileStorage pointers and related size elements to describe
;     the location of the context data. Note that for this procedure, the
;     two data areas do not need to be contained in one buffer zone, they
;     can be anywhere in the accessible memory address space. If the host
;     environment uses a non-volatile storage device accessed on the system
;     address bus such as flashROM, then the context data does not need to
;     be moved prior to this call. If the host environment uses a non-
;     volatile storage device not located on the system address bus (e.g.
;     CMOS or SSEPROM) then the host environment must transfer the context
;     data to a buffer in main memory prior to calling this procedure.
;
;   Dependencies:
;     The host environment must have determined that the system should take
;     the resume path prior to calling this procedure. The configuration
;     done by AmdInitEarly and any necessary warm reset must be complete.
;     After this procedure, execution proceeds to general system restoration.
;
;     This procedure requires a stack. The host environment must use one of
;     the provided service functions to establish the stack environment prior
;     to making the call to this procedure.
;
AmdInitResumeWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdInitResume
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_RESUME
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PreMemHeap
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPremem
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPremem
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, (SIZEOF AMD_CONFIG_PARAMS + SIZEOF AMD_S3_PARAMS)
    call  oemPlatformConfigInit

    call  myGetNonVolatileS3Context
    mov   (AMD_RESUME_PARAMS ptr [esi]).S3DataBlock.NvStorage, ebx
    mov   (AMD_RESUME_PARAMS ptr [esi]).S3DataBlock.NvStorageSize, ecx

    ; Call in to the AmdInitResume entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS Re-initialization has been completed successfully.
    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_RESUME_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif


    ; Allow AGESA to free the space used by AmdInitResume
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32


    popad
    ret
AmdInitResumeWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdCalloutRouterPremem
;
;   Entry:
;     ECX - Callout function number
;     EDX - Function-specific UINTN
;     ESI - Pointer to function specific data
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;
;   Modified:
;     None
;
;   Purpose:
;     The call out router function for AmdInitEarly,
;     AmdInitPost, and AmdInitResume.
;
;   Dependencies:
;     None
;
AmdCalloutRouterPremem PROC FAR PUBLIC USES ECX EBX ESI BX DI DS ES
    xor   ax, ax
    mov   ds, ax
    mov   es, ax
    lea   di, cs:CalloutRouterTablePremem
    mov   eax, AGESA_UNSUPPORTED

loopThruTable:
    cmp   di, OFFSET cs:CalloutRouterTablePrememEnd
    jae   amdCpuCalloutExit ; exit with AGESA_UNSUPPORTED
    cmp   ecx, cs:[di].sOemCallout.FuncName
    je    FoundMatch
    add   di, SIZEOF sOemCallout
    jmp   loopThruTable

FoundMatch:
    mov   bx, cs:[di].sOemCallout.FuncPtr
    call  bx

amdCpuCalloutExit:
    ret
AmdCalloutRouterPremem ENDP


;----------------------------------------------------------------------------
; Define the callout dispatch table for the prememory segment
;----------------------------------------------------------------------------

CalloutRouterTablePremem LABEL BYTE
    ;; Add entries as desired.
    sOemCallout <AGESA_READ_SPD, OFFSET myReadSPDPremem>
    sOemCallout <AGESA_HOOKBEFORE_DRAM_INIT, OFFSET myHookBeforeDramInit>
    sOemCallout <AGESA_HOOKBEFORE_DQS_TRAINING, OFFSET myHookBeforeDQSTraining>
    sOemCallout <AGESA_HOOKBEFORE_EXIT_SELF_REF, OFFSET myAgesaHookBeforeExitSelfRefresh>
    sOemCallout <AGESA_DO_RESET, OFFSET myDoReset>
CalloutRouterTablePrememEnd LABEL BYTE



AMD_PREMEM_END


;----------------------------------------------------------------------------
;    POST SEGMENT
; This segment may be decompressed and run from system RAM.
;----------------------------------------------------------------------------

AMD_POST_START


;----------------------------------------------------------------------------
; Declare the external routines required in the POST segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   myAllocateBuffer (Required)
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - E2h
;     ESI - Pointer to an AGESA_BUFFER_PARAMS structure.
;
;           typedef struct {
;             IN OUT   AMD_CONFIG_PARAMS StdHeader;
;             IN       UINT32 BufferLength;
;             IN       UINT32 BufferHandle;
;                OUT   VOID *BufferPointer;
;           } AGESA_BUFFER_PARAMS;
;
;   Exit:
;     After this hook, AGESA will display - AGESA_TESTPOINT - E3h
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS The requested size of memory has been
;                         successfully allocated.
;           AGESA_UNSUPPORTED This is a required function, so this
;                             value being returned causes a critical
;                             error response value from the AGESA
;                             software function.
;           AGESA_ERROR Less than the requested amount of memory
;                       was allocated.
;
;   Modified:
;     EAX
;
;   Purpose:
;     This function is used after main memory has been initialized
;     and the host environment has taken control of memory allocation.
;     This function must allocate a buffer of the requested size or
;     larger. This function is required to be implemented by the host
;     environment.
;
;   Dependencies:
;     The following call-outs must work together in the host system.
;     Parameters of the same name have the same function and must be
;     treated the same in each function:
;       AgesaAllocateBuffer
;       AgesaDeallocateBuffer
;       AgesaLocateBuffer
;       AgesaRunFcnOnAp
;     The host environment may need to reserve a location in the buffer
;     to store any host environment specific value(s). The returned
;     pointer must not include this reserved space. The host environment
;     on the AgesaDeallocateBuffer call needs to account for the reserved
;     space.  This reserved space may be an identifier or the "handle"
;     used to identify the specific memory block.
;
EXTERN myAllocateBuffer:NEAR

;+---------------------------------------------------------------------------
;
;   myDeallocateBuffer (Required)
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - E4h
;     ESI - Pointer to an AGESA_BUFFER_PARAMS structure.
;
;           typedef struct {
;             IN OUT   AMD_CONFIG_PARAMS StdHeader;
;             IN       UINT32 BufferLength;
;             IN       UINT32 BufferHandle;
;                OUT   VOID *BufferPointer;
;           } AGESA_BUFFER_PARAMS;
;
;   Exit:
;     After this hook, AGESA will display - AGESA_TESTPOINT - E5h
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS The function has completed successfully.
;           AGESA_BOUNDS_CHK The BufferHandle is invalid. The AGESA
;                            software continues with its function.
;           AGESA_UNSUPPORTED This is a required function, so this
;                             value being returned causes a critical
;                             error response value from the AGESA
;                             software function.
;
;   Modified:
;     EAX
;
;   Purpose:
;     This function is used after main memory has been initialized
;     and the host environment has taken control of memory allocation.
;     This function releases a valid working buffer. This function is
;     required for the host environment to implement.
;
;   Dependencies:
;     The following call-outs must work together in the host system.
;     Parameters of the same name have the same function and must be
;     treated the same in each function:
;       AgesaAllocateBuffer
;       AgesaDeallocateBuffer
;       AgesaLocateBuffer
;       AgesaRunFcnOnAp
;
EXTERN myDeallocateBuffer:NEAR

;+---------------------------------------------------------------------------
;
;   myLocateBuffer (Required)
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - E6h
;     ESI - Pointer to an AGESA_BUFFER_PARAMS structure.
;
;           typedef struct {
;             IN OUT   AMD_CONFIG_PARAMS StdHeader;
;             IN       UINT32 BufferLength;
;             IN       UINT32 BufferHandle;
;                OUT   VOID *BufferPointer;
;           } AGESA_BUFFER_PARAMS;
;
;   Exit:
;     After this hook, AGESA will display - AGESA_TESTPOINT - E7h
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS The function has completed successfully.
;           AGESA_BOUNDS_CHK The presented handle is invalid or the
;                            buffer could not be located.
;
;   Modified:
;     EAX
;
;   Purpose:
;     This function is used after main memory has been initialized
;     and the host environment has taken control of memory allocation.
;     This function must locate the buffer related to the indicated
;     handle and return the address of the buffer and its length.
;     This function is required to be implemented in the host
;     environment.
;
;   Dependencies:
;     The following call-outs must work together in the host system.
;     Parameters of the same name have the same function and must be
;     treated the same in each function:
;       AgesaAllocateBuffer
;       AgesaDeallocateBuffer
;       AgesaLocateBuffer
;       AgesaRunFcnOnAp
;
EXTERN myLocateBuffer:NEAR


;+---------------------------------------------------------------------------
;
;   myRunFuncOnAp (Required)
;
;   Entry:
;     EDX - Local APIC ID of the target core.
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     The host environment must route execution to the target AP and
;     have that AP call the AmdLateRunApTaskWrapper routine defined
;     above.
;
;   Dependencies:
;     None
;
EXTERN myRunFuncOnAp:NEAR

;+---------------------------------------------------------------------------
;
;   mySaveNonVolatileS3Context (Required for proper S3 operation)
;
;   Entry:
;     EBX - Pointer to the non-volatile S3 context block
;     ECX - Size in bytes of the non-volatile S3 context block
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     The host environment must save the non-volatile data to an area
;     that will not lose context while in the ACPI S3 sleep state, but
;     cannot be placed in system RAM.  This data will need to be
;     available during the call to AmdInitResume.
;
;   Dependencies:
;     None
;
EXTERN mySaveNonVolatileS3Context:NEAR

;+---------------------------------------------------------------------------
;
;   mySaveVolatileS3Context (Required for proper S3 operation)
;
;   Entry:
;     EBX - Pointer to the volatile S3 context block
;     ECX - Size in bytes of the volatile S3 context block
;
;   Exit:
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     The host environment must save the volatile data to an area
;     that will not lose context while in the ACPI S3 sleep state.
;     This data will need to be available during the call to
;     AmdS3LateRestore.
;
;   Dependencies:
;     None
;
EXTERN mySaveVolatileS3Context:NEAR

;+---------------------------------------------------------------------------
;
;   myGetVolatileS3Context (Required for proper S3 operation)
;
;   Entry:
;     None
;
;   Exit:
;     EBX - Pointer to the volatile S3 context block
;     ECX - Size in bytes of the volatile S3 context block
;
;   Modified:
;     None
;
;   Purpose:
;     The host environment must return the pointer to the data
;     saved during the mySaveVolatileS3Context routine.
;
;   Dependencies:
;     None
;
EXTERN myGetVolatileS3Context:NEAR


;----------------------------------------------------------------------------
; Define the sample wrapper routines for the POST segment
;----------------------------------------------------------------------------

;+---------------------------------------------------------------------------
;
;   AmdInitEnvWrapper
;
;   Entry:
;     On Entry to "AmdInitEnv" AGESA will display AGESA_TESTPOINT - C8h
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Exit from "AmdInitEnv" AGESA will display AGESA_TESTPOINT - C9h
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This procedure uses the AgesaAllocateBuffer call-out to acquire
;     permanent buffer space for the UEFI Hand-Off Blocks (HOBs). This
;     is also known as, or includes, artifact data being used by the
;     AGESA software. Upon entry to this procedure, the data is being
;     held in a temporary memory location and it must be moved to a
;     location controlled and protected by the host environment.
;
;     These actions are performed by the BSP. The APs are not assigned
;     any tasks at this time point.
;
;   Dependencies:
;     This procedure must be called after full memory is initialized and
;     the host environment has taken control of main memory allocation.
;     This procedure should be called before the PCI enumeration takes
;     place and as soon as possible after the host environment memory
;     allocation sub-system has started.
;
;     This procedure requires a stack. The host environment must use one
;     of the provided service functions to establish the stack environment
;     prior to making the call to this procedure.
;
AmdInitEnvWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdInitEnv
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_ENV
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PostMemDram
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPost
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPost
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, SIZEOF AMD_CONFIG_PARAMS
    call  oemPlatformConfigInit

    ; Call in to the AmdInitEnv entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS The function has completed successfully.
    ;;  AGESA_ERROR The artifact data could not be found or the host
    ;;  environment failed to allocate sufficient buffer space.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_ENV_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    ; Allow AGESA to free the space used by AmdInitEnv
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32


    popad
    ret
AmdInitEnvWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdInitMidWrapper
;
;   Entry:
;     On Entry to "AmdInitMid" AGESA will display AGESA_TESTPOINT - CAh
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Exit from "AmdInitMid" AGESA will display AGESA_TESTPOINT - CBh
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This procedure call performs special configuration requirements for
;     the graphics display hardware.
;
;     These actions are performed by the BSP. The APs are not assigned any
;     tasks at this time point.
;
;   Dependencies:
;     This procedure must be called after PCI enumeration has allocated
;     resources, but before the video BIOS call is performed.
;
;     This procedure requires a stack. The host environment must use one
;     of the provided service functions to establish the stack environment
;     prior to making the call to this procedure.
;
AmdInitMidWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdInitMid
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_MID
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PostMemDram
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPost
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPost
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, SIZEOF AMD_CONFIG_PARAMS
    call  oemPlatformConfigInit

    ; Call in to the AmdInitMid entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS The function has completed successfully.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_MID_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    ; Allow AGESA to free the space used by AmdInitMid
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32


    popad
    ret

AmdInitMidWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdInitLateWrapper
;
;   Entry:
;     On Entry to "AmdInitLate" AGESA will display AGESA_TESTPOINT - CCh
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Exit from "AmdInitLate" AGESA will display AGESA_TESTPOINT - CDh
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     The main purpose of this function is to generate informational
;     data tables used by the operating system. The individual tables
;     can be selected for generation through the user selection entries
;     on the input parameters.
;
;     This routine uses the Call-Out AgesaAllocateBuffer to allocate a
;     buffer of the proper size to contain the data.
;
;     The code path separates the BSP from the APs and perform a separate
;     and appropriate list of tasks for each class of core.
;     For the BSP, the following actions are performed:
;       Allocate buffer space for the tables.
;       Generate the table contents.
;       Make sure that the CPU is in a known good power state before
;       proceeding to boot the OS.
;     For the APs, the following actions are performed:
;       Final register settings preparing for entry to OS.
;       Establish the final PState for entry to OS.
;
;   Dependencies:
;     This routine is expected to be executed late in the boot sequence
;     after main memory has been initialized, after PCI enumeration has
;     completed, after the host environment ACPI sub-system has started,
;     after the host environment has taken control of the APs, but just
;     before the start of OS boot.
;
;     The host environment must provide the required call-outs listed in
;     the "Required Call-Out Procedures" section of the AGESA interface
;     specification to provide the buffer space in main memory and execute
;     code on the APs. The host environment must register the created ACPI
;     table in the main ACPI pointer tables. This may require moving the
;     generated tables to another location in memory.
;
;     This procedure requires a stack. The host environment must establish
;     the stack environment prior to making the call to this procedure.
;     Some functions depend upon the preservation of the heap data across
;     the shift from pre-memory environment to a post-memory environment.
;     If that data was not preserved, then those functions cannot complete
;     and an error is returned.
;
AmdInitLateWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdInitLate
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_INIT_LATE
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PostMemDram
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPost
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPost
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, SIZEOF AMD_CONFIG_PARAMS
    call  oemPlatformConfigInit

    ; Call in to the AmdInitLate entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS The function has completed successfully.
    ;;  AGESA_ALERT
    ;;  AGESA_ERROR The system could not allocate the needed amount of
    ;;  buffer space; or could not locate the artifact data block in
    ;;  memory. Likely cause: the host environment may not have preserved
    ;;  the data properly.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_LATE_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    push  es
    mov   ax, SEG AmdAcpiSratPointer
    mov   es, ax

    mov   ebx, (AMD_LATE_PARAMS ptr [esi]).AcpiSrat
    mov   es:AmdAcpiSratPointer, ebx
    mov   eax, DWORD PTR [ebx + 4]
    mov   es:AmdAcpiSratSize, eax

    mov   ebx, (AMD_LATE_PARAMS ptr [esi]).AcpiSlit
    mov   es:AmdAcpiSlitPointer, ebx
    mov   eax, DWORD PTR [ebx + 4]
    mov   es:AmdAcpiSlitSize, eax

    mov   ebx, (AMD_LATE_PARAMS ptr [esi]).AcpiPState
    mov   es:AmdAcpiSsdtPointer, ebx
    mov   eax, DWORD PTR [ebx + 4]
    mov   es:AmdAcpiSsdtSize, eax

    xor   eax, eax

    mov   ebx, (AMD_LATE_PARAMS ptr [esi]).AcpiWheaMce
    mov   es:AmdAcpiWheaMcePointer, ebx
    mov   ax, WORD PTR [ebx]
    mov   es:AmdAcpiWheaMceSize, eax

    mov   ebx, (AMD_LATE_PARAMS ptr [esi]).AcpiWheaMce
    mov   es:AmdAcpiWheaCmcPointer, ebx
    mov   ax, WORD PTR [ebx]
    mov   es:AmdAcpiWheaCmcSize, eax

    mov   eax, (AMD_LATE_PARAMS ptr [esi]).DmiTable
    mov   es:AmdDmiInfoPointer, eax
    pop   es


    ; Allow AGESA to free the space used by AmdInitLate
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32

    popad
    ret

AmdInitLateWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdS3SaveWrapper
;
;   Entry:
;     On Entry to "AmdS3Save" AGESA will display AGESA_TESTPOINT - CEh
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Entry to "AmdS3Save" AGESA will display AGESA_TESTPOINT - CFh
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This procedure saves critical registers and/or configuration
;     information for preservation across a system suspend mode. All
;     actions needed to prepare the processor for suspend mode is
;     performed, however this procedure does NOT initiate the suspend
;     process. The host environment is expected to perform that duty.
;
;     These actions are performed by the BSP. The APs are not assigned
;     any tasks at this time point.
;
;     The initializer routine will NULL out the save area pointers and
;     sizes. This procedure will determine the size of storage needed
;     for all the processor context, and make a call out to the environment
;     for allocation of one buffer to store all of the data. Upon exit, the
;     pointers and sizes within the AMD_S3_PARAMS structure will be updated
;     with the appropriate addresses within the buffer that was allocated.
;     The host environment is expected to then transfer the data pointed to
;     by NvStorage to a non-volatile storage area, and the data pointed to
;     by VolatileStorage to either a non-volatile storage area or system
;     RAM that retains its content across suspend.
;
;   Dependencies:
;     The host environment must initiate the suspend process.
;
;     This procedure requires a stack. The host environment must establish
;     the stack environment prior to making the call to this procedure.
;
AmdS3SaveWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdS3Save
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_S3_SAVE
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PostMemDram
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPost
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPost
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, (SIZEOF AMD_CONFIG_PARAMS + SIZEOF AMD_S3_PARAMS)
    call  oemPlatformConfigInit

    ; Call in to the AmdS3Save entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS All suspend duties have been completed successfully.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_S3SAVE_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    mov   ecx, (AMD_S3SAVE_PARAMS ptr [esi]).S3DataBlock.NvStorageSize
    .if (ecx != 0)
        mov   ebx, (AMD_S3SAVE_PARAMS ptr [esi]).S3DataBlock.NvStorage
        call  mySaveNonVolatileS3Context
    .endif

    mov   ecx, (AMD_S3SAVE_PARAMS ptr [esi]).S3DataBlock.VolatileStorageSize
    .if (ecx != 0)
        mov   ebx, (AMD_S3SAVE_PARAMS ptr [esi]).S3DataBlock.VolatileStorage
        call  mySaveVolatileS3Context
    .endif

    ; Allow AGESA to free the space used by AmdS3Save
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32

    popad
    ret

AmdS3SaveWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdS3LateRestoreWrapper
;
;   Entry:
;     On Entry to "AmdS3LateRestore" AGESA will display AGESA_TESTPOINT - D2h
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     On Exit from "AmdS3LateRestore" AGESA will display AGESA_TESTPOINT - D3h
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This procedure restores the processor state, reloads critical
;     silicon component registers, and performs any re-initialization
;     required by the silicon. This procedure will use the context data
;     in the VolatileStorage area of the input structure to restore the
;     processor registers.
;
;     The host environment must fill the AMD_S3_PARAMS NvStorage and
;     VolatileStorage pointers and related size elements to describe
;     the location of the context data. Note that for this procedure,
;     the two data areas do not need to be contained in one buffer zone,
;     they can be anywhere in the accessible memory address space. If
;     the host environment uses a non-volatile storage device accessed
;     on the system address bus such as flashROM, then the context data
;     does not need to be moved prior to this call. If the host
;     environment uses a non-volatile storage device not located on the
;     system address bus (e.g. CMOS or SSEPROM) then the host environment
;     must transfer the context data to a buffer in main memory prior to
;     calling this procedure.
;
;     These actions are performed by the BSP. The APs are not assigned
;     any tasks at this time point.
;
;   Dependencies:
;     This procedure is called late in the resume sequence, after the
;     PCI control space is restored and just before resuming operating
;     system execution.
;
;     The host environment must initiate the OS restart process.
;
;     This procedure requires a stack. The host environment must establish
;     the stack environment prior to making the call to this procedure.
;
AmdS3LateRestoreWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdS3LateRestore
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_S3LATE_RESTORE
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PostMemDram
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPost
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPost
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    mov   edi, esi
    add   edi, (SIZEOF AMD_CONFIG_PARAMS + SIZEOF AMD_S3_PARAMS)
    call  oemPlatformConfigInit

    call  myGetVolatileS3Context
    mov   (AMD_S3LATE_PARAMS ptr [esi]).S3DataBlock.VolatileStorage, ebx
    mov   (AMD_S3LATE_PARAMS ptr [esi]).S3DataBlock.VolatileStorageSize, ecx

    ; Call in to the AmdS3LateRestore entry point
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    ;;  AGESA_SUCCESS All resume processes have been completed successfully.

    .if (eax != AGESA_SUCCESS)
        mov   al, (AMD_S3LATE_PARAMS ptr [esi]).StdHeader.HeapStatus
        mov   ebx, AGESA_B2_ADDRESS
        call  AmdProcessAgesaErrors
    .endif

    ; Allow AGESA to free the space used by AmdS3LateRestore
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32

    popad
    ret
AmdS3LateRestoreWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdLateRunApTaskWrapper
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - D4h
;     DS - 0000 with 4 gigabyte access
;     ES - 0000 with 4 gigabyte access
;
;   Exit:
;     After this hook, AGESA will display - AGESA_TESTPOINT - D5h
;     None
;
;   Modified:
;     None
;
;   Purpose:
;     This entry point is tightly connected with the "AgesaRunFcnOnAp"
;     call out.  The AGESA software will call the call-out "AgesaRunFcnOnAp";
;     the host environment will then call this entry point to have the AP
;     execute the requested function. This is needed late in the Post and
;     Resume branches for running an AP task since the AGESA software has
;     relinquished control of the APs to the host environment.
;
;   Dependencies:
;     The host environment must implement the"AgesaRunFcnOnAp" call-out
;     and route execution to the target AP.
;
AmdLateRunApTaskWrapper PROC NEAR PUBLIC
    local localCfgBlock:AMD_INTERFACE_PARAMS

    pushad

    ; Prepare for the call to create and initialize the input parameters for AmdLateRunApTask
    xor   eax, eax
    mov   ax, ss
    shl   eax, 4
    lea   esi, localCfgBlock
    add   esi, eax
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.ImageBasePtr, AGESA_B2_ADDRESS
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_CREATE_STRUCT
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.AltImageBasePtr, 0
    mov   edx, SEG AmdCallout16
    shl   edx, 4
    add   edx, OFFSET AmdCallout16
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.CalloutPtr, edx

    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AgesaFunctionName, AMD_LATE_RUN_AP_TASK
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).AllocationMethod, PostMemDram
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).NewStructSize, 0
    push  esi
    mov   dx, SEG AmdCalloutRouterPost
    shl   edx, 16
    mov   dx, OFFSET AmdCalloutRouterPost
    push  edx
    call  AmdBridge32
    pop   edx

    mov   esi, (AMD_INTERFACE_PARAMS ptr [esi]).NewStructPtr

    ; The structure has been initialized.  Now modify the default settings as desired.

    push  es
    mov   ax, SEG AmdRunCodeOnApDataPointer
    mov   es, ax
    mov   eax, es:AmdRunCodeOnApDataPointer
    mov   (AP_EXE_PARAMS PTR [esi]).RelatedDataBlock, eax
    mov   eax, es:AmdRunCodeOnApDataSize
    mov   (AP_EXE_PARAMS PTR [esi]).RelatedBlockLength, eax
    mov   eax, es:AmdRunCodeOnApFunction
    mov   (AP_EXE_PARAMS PTR [esi]).FunctionNumber, eax
    pop   es

    ; Call in to the AmdLateRunApTask dispatcher
    push  edx
    call  AmdBridge32
    pop   edx

    ;;  EAX = AGESA_STATUS
    push  es
    mov   bx, SEG AmdRunCodeOnApStatus
    mov   es, bx
    mov   es:AmdRunCodeOnApStatus, eax
    pop   es

    ; Allow AGESA to free the space used by AmdLateRunApTask
    pop   esi
    mov   (AMD_INTERFACE_PARAMS ptr [esi]).StdHeader.Func, AMD_RELEASE_STRUCT
    call  AmdBridge32

    popad
    ret

AmdLateRunApTaskWrapper ENDP


;+---------------------------------------------------------------------------
;
;   AmdRunFuncOnAp (Required)
;
;   Entry:
;     Prior to this hook, AGESA will display - AGESA_TESTPOINT - E8h
;     EDX - Local APIC ID of the target core.
;     ESI - Pointer to an AP_EXE_PARAMS structure.
;
;           typedef struct {
;             IN OUT   AMD_CONFIG_PARAMS StdHeader;
;             IN       UINT32 FunctionNumber;
;             IN       VOID *RelatedDataBlock;
;             IN       UINT32 RelatedDataBlockLength;
;           } AP_EXE_PARAMS;
;
;   Exit:
;     After this hook, AGESA will display - AGESA_TESTPOINT - E9h
;     EAX - Contains the AGESA_STATUS return code.
;           AGESA_SUCCESS The function has completed successfully.
;           AGESA_UNSUPPORTED This is a required function, so this value
;                             being returned causes a critical error
;                             response value from the AGESAT software
;                             function and no memory initialized.
;           AGESA_WARNING The AP did not respond.
;
;   Modified:
;     EAX
;
;   Purpose:
;     This function is used after main memory has been initialized
;     and the host environment has taken control of AP task dispatching.
;     This function must cause the indicated function code to be executed
;     upon the specified Application Processor. This procedure must be
;     executed in 32-bit mode. This function is required to be implemented
;     in the host environment.
;
;   Dependencies:
;     The host environment must route execution to the target AP and
;     have that AP call the"AmdLateRunApTask" entry point.
;
AmdRunFuncOnAp PROC NEAR PUBLIC

    push  es
    mov   ax, SEG AmdRunCodeOnApDataPointer
    mov   es, ax
    mov   eax, (AP_EXE_PARAMS PTR [esi]).RelatedDataBlock
    mov   es:AmdRunCodeOnApDataPointer, eax
    mov   eax, (AP_EXE_PARAMS PTR [esi]).RelatedBlockLength
    mov   es:AmdRunCodeOnApDataSize, eax
    mov   eax, (AP_EXE_PARAMS PTR [esi]).FunctionNumber
    mov   es:AmdRunCodeOnApFunction, eax
    mov   eax, AGESA_UNSUPPORTED
    mov   es:AmdRunCodeOnApStatus, eax
    pop   es

    call  myRunFuncOnAp

    push  es
    mov   ax, SEG AmdRunCodeOnApStatus
    mov   es, ax
    mov   eax, es:AmdRunCodeOnApStatus
    pop   es
    ret
AmdRunFuncOnAp ENDP



;+---------------------------------------------------------------------------
;
;   AmdCalloutRouterPost
;
;   Entry:
;     ECX - Callout function number
;     EDX - Function-specific UINTN
;     ESI - Pointer to function specific data
;
;   Exit:
;     EAX - Contains the AGESA_STATUS return code.
;
;   Modified:
;     None
;
;   Purpose:
;     The call out router function for AmdInitEnv,
;     AmdInitMid, AmdInitLate, AmdS3Save, and
;     AmdS3LateRestore.
;
;   Dependencies:
;     None
;
AmdCalloutRouterPost PROC FAR PUBLIC USES ECX EBX ESI BX DI DS ES
    xor   ax, ax
    mov   ds, ax
    mov   es, ax
    lea   di, cs:CalloutRouterTablePost
    mov   eax, AGESA_UNSUPPORTED

loopThruTable:
    cmp   di, OFFSET cs:CalloutRouterTablePostEnd
    jae   amdCpuCalloutExit ; exit with AGESA_UNSUPPORTED
    cmp   ecx, cs:[di].sOemCallout.FuncName
    je    FoundMatch
    add   di, SIZEOF sOemCallout
    jmp   loopThruTable

FoundMatch:
    mov   bx, cs:[di].sOemCallout.FuncPtr
    call  bx

amdCpuCalloutExit:
    ret
AmdCalloutRouterPost ENDP


;----------------------------------------------------------------------------
; Define the callout dispatch table for the POST segment
;----------------------------------------------------------------------------

CalloutRouterTablePost LABEL BYTE
    ;; Add entries as desired.
    sOemCallout <AGESA_ALLOCATE_BUFFER, OFFSET myAllocateBuffer>
    sOemCallout <AGESA_DEALLOCATE_BUFFER, OFFSET myDeallocateBuffer>
    sOemCallout <AGESA_LOCATE_BUFFER, OFFSET myLocateBuffer>
    sOemCallout <AGESA_RUNFUNC_ONAP, OFFSET AmdRunFuncOnAp>
CalloutRouterTablePostEnd LABEL BYTE

AMD_POST_END


;----------------------------------------------------------------------------
;    CPU DATA SEGMENT
; This segment must be writable, and present at the time that
; AmdInitLate is run.
;----------------------------------------------------------------------------

CPU_DATASEG_START

  ;; Data used to store pointers for later use by the host environment.
  PUBLIC AmdAcpiSratPointer
  PUBLIC AmdAcpiSratSize
  PUBLIC AmdAcpiSlitPointer
  PUBLIC AmdAcpiSlitSize
  PUBLIC AmdAcpiSsdtPointer
  PUBLIC AmdAcpiSsdtSize
  PUBLIC AmdAcpiWheaMcePointer
  PUBLIC AmdAcpiWheaMceSize
  PUBLIC AmdAcpiWheaCmcPointer
  PUBLIC AmdAcpiWheaCmcSize
  PUBLIC AmdDmiInfoPointer
  AmdAcpiSratPointer     DWORD ?
  AmdAcpiSratSize        DWORD ?
  AmdAcpiSlitPointer     DWORD ?
  AmdAcpiSlitSize        DWORD ?
  AmdAcpiSsdtPointer     DWORD ?
  AmdAcpiSsdtSize        DWORD ?
  AmdAcpiWheaMcePointer  DWORD ?
  AmdAcpiWheaMceSize     DWORD ?
  AmdAcpiWheaCmcPointer  DWORD ?
  AmdAcpiWheaCmcSize     DWORD ?
  AmdDmiInfoPointer      DWORD ?

  ;; Data used for communication between the AP and the BSP.
  PUBLIC AmdRunCodeOnApDataPointer
  PUBLIC AmdRunCodeOnApDataSize
  PUBLIC AmdRunCodeOnApFunction
  PUBLIC AmdRunCodeOnApStatus
  AmdRunCodeOnApDataPointer  DWORD ?
  AmdRunCodeOnApDataSize     DWORD ?
  AmdRunCodeOnApFunction     DWORD ?
  AmdRunCodeOnApStatus       DWORD ?

CPU_DATASEG_END


END