summaryrefslogtreecommitdiff
path: root/src/cpu/amd/family_10h-family_15h/init_cpus.c
blob: eebbc48a0cdd5330af00ab62e2102b512c2548a3 (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
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <console/console.h>
#include <cpu/amd/msr.h>
#include <device/pci_ops.h>
#include <types.h>

#include "init_cpus.h"

#if CONFIG(HAVE_OPTION_TABLE)
#include "option_table.h"
#endif
#include <pc80/mc146818rtc.h>

#include <northbridge/amd/amdht/ht_wrapper.h>
#include <northbridge/amd/amdht/AsPsDefs.h>
#include <northbridge/amd/amdht/porting.h>
#include <northbridge/amd/amdht/h3ncmn.h>

#include <southbridge/amd/common/reset.h>

#if CONFIG(SOUTHBRIDGE_AMD_SB700)
#include <southbridge/amd/sb700/sb700.h>
#endif

#if CONFIG(SOUTHBRIDGE_AMD_SB800)
#include <southbridge/amd/sb800/sb800.h>
#endif

#include "cpu/amd/car/disable_cache_as_ram.c"

#if CONFIG(PCI_IO_CFG_EXT)
static void set_EnableCf8ExtCfg(void)
{
	// set the NB_CFG_MSR[46]=1;
	msr_t msr;
	msr = rdmsr(NB_CFG_MSR);
	// EnableCf8ExtCfg: We need that to access CONFIG_PCI_IO_CFG_EXT 4K range
	msr.hi |= (1 << (46 - 32));
	wrmsr(NB_CFG_MSR, msr);
}
#else
static void set_EnableCf8ExtCfg(void) { }
#endif

// #define DEBUG_HT_SETUP 1
// #define FAM10_AP_NODE_SEQUENTIAL_START 1

uint32_t get_boot_apic_id(uint8_t node, uint32_t core) {
	uint32_t ap_apicid;

	uint32_t nb_cfg_54;
	uint32_t siblings;
	uint32_t cores_found;

	uint8_t fam15h = 0;
	uint8_t rev_gte_d = 0;
	uint8_t dual_node = 0;
	uint32_t f3xe8;
	uint32_t family;
	uint32_t model;

	uint32_t ApicIdCoreIdSize;

	/* Assume that all node are same stepping, otherwise we can use use
	   nb_cfg_54 from bsp for all nodes */
	nb_cfg_54 = read_nb_cfg_54();
	f3xe8 = pci_read_config32(NODE_PCI(0, 3), 0xe8);

	family = model = cpuid_eax(0x80000001);
	model = ((model & 0xf0000) >> 12) | ((model & 0xf0) >> 4);
	family = ((family & 0xf00000) >> 16) | ((family & 0xf00) >> 8);

	if (family >= 0x6f) {
		/* Family 15h or later */
		fam15h = 1;
		nb_cfg_54 = 1;
	}

	if ((model >= 0x8) || fam15h)
		/* Revision D or later */
		rev_gte_d = 1;

	if (rev_gte_d)
		 /* Check for dual node capability */
		if (f3xe8 & 0x20000000)
			dual_node = 1;

	ApicIdCoreIdSize = (cpuid_ecx(0x80000008) >> 12 & 0xf);
	if (ApicIdCoreIdSize) {
		siblings = ((1 << ApicIdCoreIdSize) - 1);
	} else {
		siblings = 3;	//quad core
	}

	cores_found = get_core_num_in_bsp(node);
	if (siblings > cores_found)
		siblings = cores_found;

	if (dual_node) {
		ap_apicid = 0;
		if (fam15h) {
			ap_apicid |= ((node >> 1) & 0x3) << 5;			/* Node ID */
			ap_apicid |= ((node & 0x1) * (siblings + 1)) + core;	/* Core ID */
		} else {
			if (nb_cfg_54) {
				ap_apicid |= ((node >> 1) & 0x3) << 4;			/* Node ID */
				ap_apicid |= ((node & 0x1) * (siblings + 1)) + core;	/* Core ID */
			} else {
				ap_apicid |= node & 0x3;					/* Node ID */
				ap_apicid |= (((node & 0x1) * (siblings + 1)) + core) << 4;	/* Core ID */
			}
		}
	} else {
		if (fam15h) {
			ap_apicid = 0;
			ap_apicid |= (node & 0x7) << 4;	/* Node ID */
			ap_apicid |= core & 0xf;	/* Core ID */
		} else {
			ap_apicid = node * (nb_cfg_54 ? (siblings + 1) : 1) +
					core * (nb_cfg_54 ? 1 : 64);
		}
	}

	printk(BIOS_DEBUG, "%s: using %d as APIC ID for node %d, core %d\n", __func__, ap_apicid, node, core);

	return ap_apicid;
}

//core_range = 0 : all cores
//core range = 1 : core 0 only
//core range = 2 : cores other than core0

static void for_each_ap(uint32_t bsp_apicid, uint32_t core_range, int8_t node,
			process_ap_t process_ap, void *gp)
{
	// here assume the OS don't change our apicid
	u32 ap_apicid;

	u32 nodes;
	u32 disable_siblings;
	u32 cores_found;
	int i, j;

	/* get_nodes define in ht_wrapper.c */
	nodes = get_nodes();

	if (!CONFIG(LOGICAL_CPUS) ||
	    read_option(multi_core, 0) != 0) {	// 0 means multi core
		disable_siblings = 1;
	} else {
		disable_siblings = 0;
	}

	for (i = 0; i < nodes; i++) {
		if ((node >= 0) && (i != node))
			continue;

		cores_found = get_core_num_in_bsp(i);

		u32 jstart, jend;

		if (core_range == 2) {
			jstart = 1;
		} else {
			jstart = 0;
		}

		if (disable_siblings || (core_range == 1)) {
			jend = 0;
		} else {
			jend = cores_found;
		}

		for (j = jstart; j <= jend; j++) {
			ap_apicid = get_boot_apic_id(i, j);

#if CONFIG(ENABLE_APIC_EXT_ID) && (CONFIG_APIC_ID_OFFSET > 0)
#if !CONFIG(LIFT_BSP_APIC_ID)
			if ((i != 0) || (j != 0))	/* except bsp */
#endif
				ap_apicid += CONFIG_APIC_ID_OFFSET;
#endif

			if (ap_apicid == bsp_apicid)
				continue;

			process_ap(ap_apicid, gp);

		}
	}
}

static inline int lapic_remote_read(int apicid, int reg, u32 *pvalue)
{
	int timeout;
	u32 status;
	int result;
	lapic_wait_icr_idle();
	lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
	lapic_write(LAPIC_ICR, LAPIC_DM_REMRD | (reg >> 4));

/* Extra busy check compared to lapic.h */
	timeout = 0;
	do {
		status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
	} while (status == LAPIC_ICR_BUSY && timeout++ < 1000);

	timeout = 0;
	do {
		status = lapic_read(LAPIC_ICR) & LAPIC_ICR_RR_MASK;
	} while (status == LAPIC_ICR_RR_INPROG && timeout++ < 1000);

	result = -1;

	if (status == LAPIC_ICR_RR_VALID) {
		*pvalue = lapic_read(LAPIC_RRR);
		result = 0;
	}
	return result;
}

#if CONFIG(SET_FIDVID)
static void init_fidvid_ap(u32 apicid, u32 nodeid, u32 coreid);
#endif

static __always_inline
void print_apicid_nodeid_coreid(u32 apicid, struct node_core_id id,
				const char *str)
{
	printk(BIOS_DEBUG,
	       "%s --- { APICID = %02x NODEID = %02x COREID = %02x} ---\n", str,
	       apicid, id.nodeid, id.coreid);
}

uint32_t wait_cpu_state(uint32_t apicid, uint32_t state, uint32_t state2)
{
	u32 readback = 0;
	u32 timeout = 1;
	int loop = 4000000;
	while (--loop > 0) {
		if (lapic_remote_read(apicid, LAPIC_MSG_REG, &readback) != 0)
			continue;
		if ((readback & 0x3f) == state || (readback & 0x3f) == state2 || (readback & 0x3f) == F10_APSTATE_RESET) {
			timeout = 0;
			break;	//target CPU is in stage started
		}
	}
	if (timeout) {
		if (readback) {
			timeout = readback;
		}
	}

	return timeout;
}

static void wait_ap_started(u32 ap_apicid, void *gp)
{
	u32 timeout;
	timeout = wait_cpu_state(ap_apicid, F10_APSTATE_STARTED, F10_APSTATE_ASLEEP);
	printk(BIOS_DEBUG, "* AP %02x", ap_apicid);
	if (timeout) {
		printk(BIOS_DEBUG, " timed out:%08x\n", timeout);
	} else {
		printk(BIOS_DEBUG, "started\n");
	}
}

void wait_all_other_cores_started(u32 bsp_apicid)
{
	// all aps other than core0
	printk(BIOS_DEBUG, "started ap apicid: ");
	for_each_ap(bsp_apicid, 2, -1, wait_ap_started, (void *)0);
	printk(BIOS_DEBUG, "\n");
}

void allow_all_aps_stop(u32 bsp_apicid)
{
	/* Called by the BSP to indicate AP can stop */

	/* FIXME Do APs use this? */

	// allow aps to stop use 6 bits for state
	lapic_write(LAPIC_MSG_REG, (bsp_apicid << 24) | F10_APSTATE_STOPPED);
}

static void wait_ap_stopped(u32 ap_apicid, void *gp)
{
	u32 timeout;
	timeout = wait_cpu_state(ap_apicid, F10_APSTATE_ASLEEP, F10_APSTATE_ASLEEP);
	printk(BIOS_DEBUG, "* AP %02x", ap_apicid);
	if (timeout) {
		printk(BIOS_DEBUG, " timed out:%08x\n", timeout);
	} else {
		printk(BIOS_DEBUG, "stopped\n");
	}
}

void wait_all_other_cores_stopped(u32 bsp_apicid)
{
	// all aps other than core0
	printk(BIOS_DEBUG, "stopped ap apicid: ");
	for_each_ap(bsp_apicid, 2, -1, wait_ap_stopped, (void *)0);
	printk(BIOS_DEBUG, "\n");
}

static void enable_apic_ext_id(u32 node)
{
	u32 val;

	val = pci_read_config32(NODE_HT(node), 0x68);
	val |= (HTTC_APIC_EXT_SPUR | HTTC_APIC_EXT_ID | HTTC_APIC_EXT_BRD_CST);
	pci_write_config32(NODE_HT(node), 0x68, val);
}

static void STOP_CAR_AND_CPU(uint8_t skip_sharedc_config, uint32_t apicid)
{
	msr_t msr;
	uint32_t family;

	family = amd_fam1x_cpu_family();		// inline

	if (family < 0x6f) {
		/* Family 10h or earlier */

		/* Disable L2 IC to L3 connection (Only for CAR) */
		msr = rdmsr(BU_CFG2_MSR);
		msr.lo &= ~(1 << ClLinesToNbDis);
		wrmsr(BU_CFG2_MSR, msr);
	} else {
		/* Family 15h or later
		 * DRAM setup is delayed on Fam15 in order to prevent
		 * any DRAM access before ECC check bits are initialized.
		 * Each core also needs to have its initial DRAM map initialized
		 * before it is put to sleep, otherwise it will fail to wake
		 * in ramstage.  To meet both of these goals, delay DRAM map
		 * setup until the last possible moment, where speculative
		 * memory access is highly unlikely before core halt...
		 */
		if (!skip_sharedc_config) {
			/* Enable memory access for first MBs using top_mem */
			msr.hi = 0;
			msr.lo = (CONFIG_RAMTOP + TOP_MEM_MASK) & (~TOP_MEM_MASK);
			wrmsr(TOP_MEM, msr);
		}
	}

	disable_cache_as_ram_real(skip_sharedc_config);	// inline

	/* Mark the core as sleeping */
	lapic_write(LAPIC_MSG_REG, (apicid << 24) | F10_APSTATE_ASLEEP);

	/* stop all cores except node0/core0 the bsp .... */
	stop_this_cpu();
}

u32 init_cpus(u32 cpu_init_detectedx, struct sys_info *sysinfo)
{
	uint32_t bsp_apicid = 0;
	uint32_t apicid;
	uint32_t dword;
	uint8_t set_mtrrs;
	uint8_t node_count;
	uint8_t fam15_bsp_core1_apicid;
	struct node_core_id id;

	/* Please refer to the calculations and explaination in cache_as_ram.inc
	 * before modifying these values */
	uint32_t max_ap_stack_region_size = CONFIG_MAX_CPUS * CONFIG_DCACHE_AP_STACK_SIZE;
	uint32_t max_bsp_stack_region_size = CONFIG_DCACHE_BSP_TOP_STACK_SIZE +
						CONFIG_DCACHE_BSP_TOP_STACK_SLUSH;
	uint32_t bsp_stack_region_upper_boundary = CONFIG_DCACHE_RAM_BASE +
						CONFIG_DCACHE_RAM_SIZE;
	uint32_t bsp_stack_region_lower_boundary = bsp_stack_region_upper_boundary -
						max_bsp_stack_region_size;

	void *lower_stack_region_boundary = (void *)(bsp_stack_region_lower_boundary -
						max_ap_stack_region_size);

	if (((void*)(sysinfo + 1)) > lower_stack_region_boundary)
		printk(BIOS_WARNING,
			"sysinfo extends into stack region (sysinfo range: [%p,%p] lower stack region boundary: %p)\n",
			sysinfo, sysinfo + 1, lower_stack_region_boundary);

	/*
	 * already set early mtrr in cache_as_ram.inc
	 */

	/* that is from initial apicid, we need nodeid and coreid
	   later */
	id = get_node_core_id_x();

	/* NB_CFG MSR is shared between cores, so we need make sure
	   core0 is done at first --- use wait_all_core0_started  */
	if (id.coreid == 0) {
		/* Set InitApicIdCpuIdLo / EnableCf8ExtCfg on core0 only */
		if (!is_fam15h())
			set_apicid_cpuid_lo();
		set_EnableCf8ExtCfg();
#if CONFIG(ENABLE_APIC_EXT_ID)
		enable_apic_ext_id(id.nodeid);
#endif
	}

	enable_lapic();

#if CONFIG(ENABLE_APIC_EXT_ID) && (CONFIG_APIC_ID_OFFSET > 0)
	u32 initial_apicid = get_initial_apicid();

#if !CONFIG(LIFT_BSP_APIC_ID)
	if (initial_apicid != 0)	// other than bsp
#endif
	{
		/* use initial APIC id to lift it */
		u32 dword = lapic_read(LAPIC_ID);
		dword &= ~(0xff << 24);
		dword |=
		    (((initial_apicid + CONFIG_APIC_ID_OFFSET) & 0xff) << 24);

		lapic_write(LAPIC_ID, dword);
	}
#if CONFIG(LIFT_BSP_APIC_ID)
	bsp_apicid += CONFIG_APIC_ID_OFFSET;
#endif

#endif

	/* get the apicid, it may be lifted already */
	apicid = lapicid();

	// show our apicid, nodeid, and coreid
	if (id.coreid == 0) {
		if (id.nodeid != 0)	//all core0 except bsp
			print_apicid_nodeid_coreid(apicid, id, " core0: ");
	} else {		//all other cores
		print_apicid_nodeid_coreid(apicid, id, " corex: ");
	}

	if (cpu_init_detectedx) {
		print_apicid_nodeid_coreid(apicid, id,
					   "\n\n\nINIT detected from ");
		printk(BIOS_DEBUG, "\nIssuing SOFT_RESET...\n");
		soft_reset();
	}

	if (id.coreid == 0) {
		if (!(warm_reset_detect(id.nodeid)))	//FIXME: INIT is checked above but check for more resets?
			distinguish_cpu_resets(id.nodeid);	// Also indicates we are started
	}
	// Mark the core as started.
	lapic_write(LAPIC_MSG_REG, (apicid << 24) | F10_APSTATE_STARTED);
	printk(BIOS_DEBUG, "CPU APICID %02x start flag set\n", apicid);

	if (apicid != bsp_apicid) {
		/* Setup each AP's cores MSRs.
		 * This happens after HTinit.
		 * The BSP runs this code in it's own path.
		 */
		update_microcode(cpuid_eax(1));

		cpuSetAMDMSR(id.nodeid);

		/* Set up HyperTransport probe filter support */
		if (is_gt_rev_d()) {
			dword = pci_read_config32(NODE_PCI(id.nodeid, 0), 0x60);
			node_count = ((dword >> 4) & 0x7) + 1;

			if (node_count > 1) {
				msr_t msr = rdmsr(BU_CFG2_MSR);
				msr.hi |= 1 << (42 - 32);
				wrmsr(BU_CFG2_MSR, msr);
			}
		}

#if CONFIG(SET_FIDVID)
#if CONFIG(LOGICAL_CPUS) && CONFIG(SET_FIDVID_CORE0_ONLY)
		// Run on all AP for proper FID/VID setup.
		if (id.coreid == 0)	// only need set fid for core0
#endif
		{
			// check warm(bios) reset to call stage2 otherwise do stage1
			if (warm_reset_detect(id.nodeid)) {
				printk(BIOS_DEBUG,
				       "init_fidvid_stage2 apicid: %02x\n",
				       apicid);
				init_fidvid_stage2(apicid, id.nodeid);
			} else {
				printk(BIOS_DEBUG,
				       "init_fidvid_ap(stage1) apicid: %02x\n",
				       apicid);
				init_fidvid_ap(apicid, id.nodeid, id.coreid);
			}
		}
#endif

		if (is_fam15h()) {
			/* core 1 on node 0 is special; to avoid corrupting the
			 * BSP do not alter MTRRs on that core */
			fam15_bsp_core1_apicid = 1;
			if (CONFIG(ENABLE_APIC_EXT_ID) && (CONFIG_APIC_ID_OFFSET > 0))
				fam15_bsp_core1_apicid += CONFIG_APIC_ID_OFFSET;

			if (apicid == fam15_bsp_core1_apicid)
				set_mtrrs = 0;
			else
				set_mtrrs = !!(apicid & 0x1);
		} else {
			set_mtrrs = 1;
		}

		/* AP is ready, configure MTRRs and go to sleep */
		if (set_mtrrs)
			set_var_mtrr(0, 0x00000000, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK);

		printk(BIOS_DEBUG, "Disabling CAR on AP %02x\n", apicid);
		if (is_fam15h()) {
			/* Only modify the MSRs on the odd cores (the last cores to finish booting) */
			STOP_CAR_AND_CPU(!set_mtrrs, apicid);
		} else {
			/* Modify MSRs on all cores */
			STOP_CAR_AND_CPU(0, apicid);
		}

		printk(BIOS_DEBUG,
		       "\nAP %02x should be halted but you are reading this....\n",
		       apicid);
	}

	return bsp_apicid;
}

static u32 is_core0_started(u32 nodeid)
{
	u32 htic;
	pci_devfn_t device;
	device = NODE_PCI(nodeid, 0);
	htic = pci_read_config32(device, HT_INIT_CONTROL);
	htic &= HTIC_ColdR_Detect;
	return htic;
}

void wait_all_core0_started(void)
{
	/* When core0 is started, it will distingush_cpu_resets
	 * So wait for that to finish */
	u32 i;
	u32 nodes = get_nodes();

	printk(BIOS_DEBUG, "core0 started: ");
	for (i = 1; i < nodes; i++) {	// skip bsp, because it is running on bsp
		while (!is_core0_started(i)) {
		}
		printk(BIOS_DEBUG, " %02x", i);
	}
	printk(BIOS_DEBUG, "\n");
}

#if CONFIG_MAX_PHYSICAL_CPUS > 1
/**
 * void start_node(u32 node)
 *
 *  start the core0 in node, so it can generate HT packet to feature code.
 *
 * This function starts the AP nodes core0s. wait_all_core0_started() in
 * romstage.c waits for all the AP to be finished before continuing
 * system init.
 */
static void start_node(u8 node)
{
	u32 val;

	/* Enable routing table */
	printk(BIOS_DEBUG, "Start node %02x", node);

#if CONFIG(NORTHBRIDGE_AMD_AMDFAM10)
	/* For FAM10 support, we need to set Dram base/limit for the new node */
	pci_write_config32(NODE_MP(node), 0x44, 0);
	pci_write_config32(NODE_MP(node), 0x40, 3);
#endif

	/* Allow APs to make requests (ROM fetch) */
	val = pci_read_config32(NODE_HT(node), 0x6c);
	val &= ~(1 << 1);
	pci_write_config32(NODE_HT(node), 0x6c, val);

	printk(BIOS_DEBUG, " done.\n");
}

/**
 * static void setup_remote_node(u32 node)
 *
 * Copy the BSP Address Map to each AP.
 */
static void setup_remote_node(u8 node)
{
	/* There registers can be used with F1x114_x Address Map at the
	   same time, So must set them even 32 node */
	static const u16 pci_reg[] = {
		/* DRAM Base/Limits Registers */
		0x44, 0x4c, 0x54, 0x5c, 0x64, 0x6c, 0x74, 0x7c,
		0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78,
		0x144, 0x14c, 0x154, 0x15c, 0x164, 0x16c, 0x174, 0x17c,
		0x140, 0x148, 0x150, 0x158, 0x160, 0x168, 0x170, 0x178,
		/* MMIO Base/Limits Registers */
		0x84, 0x8c, 0x94, 0x9c, 0xa4, 0xac, 0xb4, 0xbc,
		0x80, 0x88, 0x90, 0x98, 0xa0, 0xa8, 0xb0, 0xb8,
		/* IO Base/Limits Registers */
		0xc4, 0xcc, 0xd4, 0xdc,
		0xc0, 0xc8, 0xd0, 0xd8,
		/* Configuration Map Registers */
		0xe0, 0xe4, 0xe8, 0xec,
	};
	u16 i;

	printk(BIOS_DEBUG, "setup_remote_node: %02x", node);

	/* copy the default resource map from node 0 */
	for (i = 0; i < ARRAY_SIZE(pci_reg); i++) {
		u32 value;
		u16 reg;
		reg = pci_reg[i];
		value = pci_read_config32(NODE_MP(0), reg);
		pci_write_config32(NODE_MP(node), reg, value);

	}
	printk(BIOS_DEBUG, " done\n");
}
#endif				/* CONFIG_MAX_PHYSICAL_CPUS > 1 */

//it is running on core0 of node0
void start_other_cores(uint32_t bsp_apicid)
{
	u32 nodes;
	u32 nodeid;

	// disable multi_core
	if (read_option(multi_core, 0) != 0)  {
		printk(BIOS_DEBUG, "Skip additional core init\n");
		return;
	}

	nodes = get_nodes();

	for (nodeid = 0; nodeid < nodes; nodeid++) {
		u32 cores = get_core_num_in_bsp(nodeid);
		printk(BIOS_DEBUG, "init node: %02x  cores: %02x pass 1\n", nodeid, cores);
		if (cores > 0) {
			real_start_other_core(nodeid, cores);
#ifdef FAM10_AP_NODE_SEQUENTIAL_START
			printk(BIOS_DEBUG, "waiting for core start on node %d...\n", nodeid);
			for_each_ap(bsp_apicid, 2, nodeid, wait_ap_started, (void *)0);
			printk(BIOS_DEBUG, "...started\n");
#endif
		}
	}
}

static void AMD_Errata281(u8 node, uint64_t revision, u32 platform)
{
	/* Workaround for Transaction Scheduling Conflict in
	 * Northbridge Cross Bar.  Implement XCS Token adjustment
	 * for ganged links.  Also, perform fix up for the mixed
	 * revision case.
	 */

	u32 reg, val;
	u8 i;
	u8 mixed = 0;
	u8 nodes = get_nodes();

	if (platform & AMD_PTYPE_SVR) {
		/* For each node we need to check for a "broken" node */
		if (!(revision & (AMD_DR_B0 | AMD_DR_B1))) {
			for (i = 0; i < nodes; i++) {
				if (mctGetLogicalCPUID(i) &
				    (AMD_DR_B0 | AMD_DR_B1)) {
					mixed = 1;
					break;
				}
			}
		}

		if ((revision & (AMD_DR_B0 | AMD_DR_B1)) || mixed) {

			/* F0X68[22:21] DsNpReqLmt0 = 01b */
			val = pci_read_config32(NODE_PCI(node, 0), 0x68);
			val &= ~0x00600000;
			val |= 0x00200000;
			pci_write_config32(NODE_PCI(node, 0), 0x68, val);

			/* F3X6C */
			val = pci_read_config32(NODE_PCI(node, 3), 0x6C);
			val &= ~0x700780F7;
			val |= 0x00010094;
			pci_write_config32(NODE_PCI(node, 3), 0x6C, val);

			/* F3X7C */
			val = pci_read_config32(NODE_PCI(node, 3), 0x7C);
			val &= ~0x707FFF1F;
			val |= 0x00144514;
			pci_write_config32(NODE_PCI(node, 3), 0x7C, val);

			/* F3X144[3:0] RspTok = 0001b */
			val = pci_read_config32(NODE_PCI(node, 3), 0x144);
			val &= ~0x0000000F;
			val |= 0x00000001;
			pci_write_config32(NODE_PCI(node, 3), 0x144, val);

			for (i = 0; i < 3; i++) {
				reg = 0x148 + (i * 4);
				val = pci_read_config32(NODE_PCI(node, 3), reg);
				val &= ~0x000000FF;
				val |= 0x000000DB;
				pci_write_config32(NODE_PCI(node, 3), reg, val);
			}
		}
	}
}

static void AMD_Errata298(void)
{
	/* Workaround for L2 Eviction May Occur during operation to
	 * set Accessed or dirty bit.
	 */

	msr_t msr;
	u8 i;
	u8 affectedRev = 0;
	u8 nodes = get_nodes();

	/* For each core we need to check for a "broken" node */
	for (i = 0; i < nodes; i++) {
		if (mctGetLogicalCPUID(i) & (AMD_DR_B0 | AMD_DR_B1 | AMD_DR_B2)) {
			affectedRev = 1;
			break;
		}
	}

	if (affectedRev) {
		msr = rdmsr(HWCR_MSR);
		msr.lo |= 0x08;	/* Set TlbCacheDis bit[3] */
		wrmsr(HWCR_MSR, msr);

		msr = rdmsr(BU_CFG_MSR);
		msr.lo |= 0x02;	/* Set TlbForceMemTypeUc bit[1] */
		wrmsr(BU_CFG_MSR, msr);

		msr = rdmsr(OSVW_ID_Length);
		msr.lo |= 0x01;	/* OS Visible Workaround - MSR */
		wrmsr(OSVW_ID_Length, msr);

		msr = rdmsr(OSVW_Status);
		msr.lo |= 0x01;	/* OS Visible Workaround - MSR */
		wrmsr(OSVW_Status, msr);
	}

	if (!affectedRev && (mctGetLogicalCPUID(0xFF) & AMD_DR_B3)) {
		msr = rdmsr(OSVW_ID_Length);
		msr.lo |= 0x01;	/* OS Visible Workaround - MSR */
		wrmsr(OSVW_ID_Length, msr);

	}
}

static u32 get_platform_type(void)
{
	u32 ret = 0;

	switch (SYSTEM_TYPE) {
	case 1:
		ret |= AMD_PTYPE_DSK;
		break;
	case 2:
		ret |= AMD_PTYPE_MOB;
		break;
	case 0:
		ret |= AMD_PTYPE_SVR;
		break;
	default:
		break;
	}

	/* FIXME: add UMA support. */

	/* All Fam10 are multi core */
	ret |= AMD_PTYPE_MC;

	return ret;
}

static void AMD_SetupPSIVID_d(u32 platform_type, u8 node)
{
	u32 dword;
	int i;
	msr_t msr;

	if (platform_type & (AMD_PTYPE_MOB | AMD_PTYPE_DSK)) {

		/* The following code sets the PSIVID to the lowest support P state
		 * assuming that the VID for the lowest power state is below
		 * the VDD voltage regulator threshold. (This also assumes that there
		 * is a Pstate lower than P0)
		 */

		for (i = 4; i >= 0; i--) {
			msr = rdmsr(PSTATE_0_MSR + i);
			/*  Pstate valid? */
			if (msr.hi & PS_EN_MASK) {
				dword = pci_read_config32(NODE_PCI(i, 3), 0xA0);
				dword &= ~0x7F;
				dword |= (msr.lo >> 9) & 0x7F;
				pci_write_config32(NODE_PCI(i, 3), 0xA0, dword);
				break;
			}
		}
	}
}

/**
 * AMD_CpuFindCapability - Traverse PCI capability list to find host HT links.
 *  HT Phy operations are not valid on links that aren't present, so this
 *  prevents invalid accesses.
 *
 * Returns the offset of the link register.
 */
static BOOL AMD_CpuFindCapability(u8 node, u8 cap_count, u8 *offset)
{
	u32 reg;
	u32 val;

	/* get start of CPU HT Host Capabilities */
	val = pci_read_config32(NODE_PCI(node, 0), 0x34);
	val &= 0xFF;		//reg offset of first link

	cap_count++;

	/* Traverse through the capabilities. */
	do {
		reg = pci_read_config32(NODE_PCI(node, 0), val);
		/* Is the capability block a HyperTransport capability block? */
		if ((reg & 0xFF) == 0x08) {
			/* Is the HT capability block an HT Host Capability? */
			if ((reg & 0xE0000000) == (1 << 29))
				cap_count--;
		}

		if (cap_count)
			val = (reg >> 8) & 0xFF;	//update reg offset
	} while (cap_count && val);

	*offset = (u8) val;

	/* If requested capability found val != 0 */
	if (!cap_count)
		return TRUE;
	else
		return FALSE;
}

/**
 * AMD_checkLinkType - Compare desired link characteristics using a logical
 *     link type mask.
 *
 * Returns the link characteristic mask.
 */
static u32 AMD_checkLinkType(u8 node, u8 regoff)
{
	uint32_t val;
	uint32_t val2;
	uint32_t linktype = 0;

	/* Check connect, init and coherency */
	val = pci_read_config32(NODE_PCI(node, 0), regoff + 0x18);
	val &= 0x1F;

	if (val == 3)
		linktype |= HTPHY_LINKTYPE_COHERENT;

	if (val == 7)
		linktype |= HTPHY_LINKTYPE_NONCOHERENT;

	if (linktype) {
		/* Check gen3 */
		val = pci_read_config32(NODE_PCI(node, 0), regoff + 0x08);
		val = (val >> 8) & 0xf;
		if (is_gt_rev_d()) {
			val2 = pci_read_config32(NODE_PCI(node, 0), regoff + 0x1c);
			val |= (val2 & 0x1) << 4;
		}

		if (val > 6)
			linktype |= HTPHY_LINKTYPE_HT3;
		else
			linktype |= HTPHY_LINKTYPE_HT1;

		/* Check ganged */
		val = pci_read_config32(NODE_PCI(node, 0), (((regoff - 0x80) / 0x20) << 2) + 0x170);

		if (val & 1)
			linktype |= HTPHY_LINKTYPE_GANGED;
		else
			linktype |= HTPHY_LINKTYPE_UNGANGED;
	}

	return linktype;
}

/**
 * AMD_SetHtPhyRegister - Use the HT link's HT Phy portal registers to update
 *   a phy setting for that link.
 */
static void AMD_SetHtPhyRegister(u8 node, u8 link, u8 entry)
{
	u32 phyReg;
	u32 phyBase;
	u32 val;

	/* Determine this link's portal */
	if (link > 3)
		link -= 4;

	phyBase = ((u32) link << 3) | 0x180;

	/* Determine if link is connected and abort if not */
	if (!(pci_read_config32(NODE_PCI(node, 0), 0x98 + (link * 0x20)) & 0x1))
		return;

	/* Get the portal control register's initial value
	 * and update it to access the desired phy register
	 */
	phyReg = pci_read_config32(NODE_PCI(node, 4), phyBase);

	if (fam10_htphy_default[entry].htreg > 0x1FF) {
		phyReg &= ~HTPHY_DIRECT_OFFSET_MASK;
		phyReg |= HTPHY_DIRECT_MAP;
	} else {
		phyReg &= ~HTPHY_OFFSET_MASK;
	}

	/* Now get the current phy register data
	 * LinkPhyDone = 0, LinkPhyWrite = 0 is a read
	 */
	phyReg |= fam10_htphy_default[entry].htreg;
	pci_write_config32(NODE_PCI(node, 4), phyBase, phyReg);

	do {
		val = pci_read_config32(NODE_PCI(node, 4), phyBase);
	} while (!(val & HTPHY_IS_COMPLETE_MASK));

	/* Now we have the phy register data, apply the change */
	val = pci_read_config32(NODE_PCI(node, 4), phyBase + 4);
	val &= ~fam10_htphy_default[entry].mask;
	val |= fam10_htphy_default[entry].data;
	pci_write_config32(NODE_PCI(node, 4), phyBase + 4, val);

	/* write it through the portal to the phy
	 * LinkPhyDone = 0, LinkPhyWrite = 1 is a write
	 */
	phyReg |= HTPHY_WRITE_CMD;
	pci_write_config32(NODE_PCI(node, 4), phyBase, phyReg);

	do {
		val = pci_read_config32(NODE_PCI(node, 4), phyBase);
	} while (!(val & HTPHY_IS_COMPLETE_MASK));
}

void cpuSetAMDMSR(uint8_t node_id)
{
	/* This routine loads the CPU with default settings in fam10_msr_default
	 * table . It must be run after Cache-As-RAM has been enabled, and
	 * Hypertransport initialization has taken place.  Also note
	 * that it is run on the current processor only, and only for the current
	 * processor core.
	 */
	msr_t msr;
	u8 i;
	uint8_t nvram;
	u32 platform;
	uint64_t revision;
	uint8_t enable_cpb;

	printk(BIOS_DEBUG, "cpuSetAMDMSR ");

	revision = mctGetLogicalCPUID(0xFF);
	platform = get_platform_type();

	for (i = 0; i < ARRAY_SIZE(fam10_msr_default); i++) {
		if ((fam10_msr_default[i].revision & revision) &&
		    (fam10_msr_default[i].platform & platform)) {
			msr = rdmsr(fam10_msr_default[i].msr);
			msr.hi &= ~fam10_msr_default[i].mask_hi;
			msr.hi |= fam10_msr_default[i].data_hi;
			msr.lo &= ~fam10_msr_default[i].mask_lo;
			msr.lo |= fam10_msr_default[i].data_lo;
			wrmsr(fam10_msr_default[i].msr, msr);
		}
	}
	AMD_Errata298();

	/* Revision C0 and above */
	if (revision & AMD_OR_C0) {
		uint8_t enable_experimental_memory_speed_boost;

		/* Check to see if cache partitioning is allowed */
		enable_experimental_memory_speed_boost = 0;
		if (get_option(&nvram, "experimental_memory_speed_boost") == CB_SUCCESS)
			enable_experimental_memory_speed_boost = !!nvram;

		uint32_t f3x1fc = pci_read_config32(NODE_PCI(node_id, 3), 0x1fc);
		msr = rdmsr(FP_CFG_MSR);
		msr.hi &= ~(0x7 << (42-32));			/* DiDtCfg4 */
		msr.hi |= (((f3x1fc >> 17) & 0x7) << (42-32));
		msr.hi &= ~(0x1 << (41-32));			/* DiDtCfg5 */
		msr.hi |= (((f3x1fc >> 22) & 0x1) << (41-32));
		msr.hi &= ~(0x1 << (40-32));			/* DiDtCfg3 */
		msr.hi |= (((f3x1fc >> 16) & 0x1) << (40-32));
		msr.hi &= ~(0x7 << (32-32));			/* DiDtCfg1 (1) */
		msr.hi |= (((f3x1fc >> 11) & 0x7) << (32-32));
		msr.lo &= ~(0x1f << 27);			/* DiDtCfg1 (2) */
		msr.lo |= (((f3x1fc >> 6) & 0x1f) << 27);
		msr.lo &= ~(0x3 << 25);				/* DiDtCfg2 */
		msr.lo |= (((f3x1fc >> 14) & 0x3) << 25);
		msr.lo &= ~(0x1f << 18);			/* DiDtCfg0 */
		msr.lo |= (((f3x1fc >> 1) & 0x1f) << 18);
		msr.lo &= ~(0x1 << 16);				/* DiDtMode */
		msr.lo |= ((f3x1fc & 0x1) << 16);
		wrmsr(FP_CFG_MSR, msr);

		if (enable_experimental_memory_speed_boost) {
			msr = rdmsr(BU_CFG3_MSR);
			msr.lo |= (0x3 << 20);			/* PfcStrideMul = 0x3 */
			wrmsr(BU_CFG3_MSR, msr);
		}
	}

#if CONFIG(SOUTHBRIDGE_AMD_SB700) || CONFIG(SOUTHBRIDGE_AMD_SB800)
	if (revision & (AMD_DR_GT_D0 | AMD_FAM15_ALL)) {
		/* Set up message triggered C1E */
		msr = rdmsr(MSR_INTPEND);
		msr.lo &= ~0xffff;		/* IOMsgAddr = ACPI_PM_EVT_BLK */
		msr.lo |= ACPI_PM_EVT_BLK & 0xffff;
		msr.lo |= (0x1 << 29);		/* BmStsClrOnHltEn = 1 */
		if (revision & AMD_DR_GT_D0) {
			msr.lo &= ~(0x1 << 28);	/* C1eOnCmpHalt = 0 */
			msr.lo &= ~(0x1 << 27);	/* SmiOnCmpHalt = 0 */
		}
		wrmsr(MSR_INTPEND, msr);

		msr = rdmsr(HWCR_MSR);
		msr.lo |= (0x1 << 12);		/* HltXSpCycEn = 1 */
		wrmsr(HWCR_MSR, msr);
	}

	if (revision & (AMD_DR_Ex | AMD_FAM15_ALL)) {
		if (CONFIG(HAVE_ACPI_TABLES))
			if ((get_option(&nvram, "cpu_c_states") == CB_SUCCESS) &&
			    (nvram)) {
				/* Set up the C-state base address */
				msr_t c_state_addr_msr;
				c_state_addr_msr = rdmsr(MSR_CSTATE_ADDRESS);
				c_state_addr_msr.lo = ACPI_CPU_P_LVL2;
				wrmsr(MSR_CSTATE_ADDRESS, c_state_addr_msr);
			}
	}
#endif

	if (revision & AMD_FAM15_ALL) {
		enable_cpb = 1;
		if (get_option(&nvram, "cpu_core_boost") == CB_SUCCESS)
			enable_cpb = !!nvram;

		if (!enable_cpb) {
			/* Disable Core Performance Boost */
			msr = rdmsr(HWCR_MSR);
			msr.lo |= (0x1 << 25);		/* CpbDis = 1 */
			wrmsr(HWCR_MSR, msr);
		}
	}

	printk(BIOS_DEBUG, " done\n");
}

static void cpuSetAMDPCI(u8 node)
{
	/* This routine loads the CPU with default settings in fam10_pci_default
	 * table . It must be run after Cache-As-RAM has been enabled, and
	 * Hypertransport initialization has taken place.  Also note
	 * that it is run for the first core on each node
	 */
	uint8_t i;
	uint8_t j;
	u32 platform;
	u32 val;
	uint8_t offset;
	uint32_t dword;
	uint64_t revision;

	/* FIXME
	 * This should be configurable
	 */
	uint8_t sockets = 2;
	uint8_t sockets_populated = 2;

	printk(BIOS_DEBUG, "cpuSetAMDPCI %02d", node);

	revision = mctGetLogicalCPUID(node);
	platform = get_platform_type();

	AMD_SetupPSIVID_d(platform, node);	/* Set PSIVID offset which is not table driven */

	for (i = 0; i < ARRAY_SIZE(fam10_pci_default); i++) {
		if ((fam10_pci_default[i].revision & revision) &&
		    (fam10_pci_default[i].platform & platform)) {
			val = pci_read_config32(NODE_PCI(node,
							 fam10_pci_default[i].
							 function),
						fam10_pci_default[i].offset);
			val &= ~fam10_pci_default[i].mask;
			val |= fam10_pci_default[i].data;
			pci_write_config32(NODE_PCI(node,
						    fam10_pci_default[i].
						    function),
					   fam10_pci_default[i].offset, val);
		}
	}

	if (is_fam15h()) {
		if (CONFIG_CPU_SOCKET_TYPE == 0x14) {
			/* Socket C32 */
			dword = pci_read_config32(NODE_PCI(node, 0), 0x84);
			dword |= 0x1 << 13;			/* LdtStopTriEn = 1 */
			pci_write_config32(NODE_PCI(node, 0), 0x84, dword);

			dword = pci_read_config32(NODE_PCI(node, 0), 0xa4);
			dword |= 0x1 << 13;			/* LdtStopTriEn = 1 */
			pci_write_config32(NODE_PCI(node, 0), 0xa4, dword);

			dword = pci_read_config32(NODE_PCI(node, 0), 0xc4);
			dword |= 0x1 << 13;			/* LdtStopTriEn = 1 */
			pci_write_config32(NODE_PCI(node, 0), 0xc4, dword);

			dword = pci_read_config32(NODE_PCI(node, 0), 0xe4);
			dword |= 0x1 << 13;			/* LdtStopTriEn = 1 */
			pci_write_config32(NODE_PCI(node, 0), 0xe4, dword);
		}
		else {
			/* Other socket (G34, etc.) */
			dword = pci_read_config32(NODE_PCI(node, 0), 0x84);
			dword &= ~(0x1 << 13);			/* LdtStopTriEn = 0 */
			pci_write_config32(NODE_PCI(node, 0), 0x84, dword);

			dword = pci_read_config32(NODE_PCI(node, 0), 0xa4);
			dword &= ~(0x1 << 13);			/* LdtStopTriEn = 0 */
			pci_write_config32(NODE_PCI(node, 0), 0xa4, dword);

			dword = pci_read_config32(NODE_PCI(node, 0), 0xc4);
			dword &= ~(0x1 << 13);			/* LdtStopTriEn = 0 */
			pci_write_config32(NODE_PCI(node, 0), 0xc4, dword);

			dword = pci_read_config32(NODE_PCI(node, 0), 0xe4);
			dword &= ~(0x1 << 13);			/* LdtStopTriEn = 0 */
			pci_write_config32(NODE_PCI(node, 0), 0xe4, dword);
		}
	}

#ifdef DEBUG_HT_SETUP
	/* Dump link settings */
	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			printk(BIOS_DEBUG, "Node %d link %d: type register: %08x control register: %08x extended control sublink 0: %08x 1: %08x\n", i, j,
				pci_read_config32(NODE_PCI(i, 0), 0x98 + (j * 0x20)), pci_read_config32(NODE_PCI(i, 0), 0x84 + (j * 0x20)),
				pci_read_config32(NODE_PCI(i, 0), 0x170 + (j * 0x4)), pci_read_config32(NODE_PCI(i, 0), 0x180 + (j * 0x4)));
		}
	}
#endif

	for (i = 0; i < ARRAY_SIZE(fam10_htphy_default); i++) {
		if ((fam10_htphy_default[i].revision & revision) &&
		    (fam10_htphy_default[i].platform & platform)) {
			/* HT Phy settings either apply to both sublinks or have
			 * separate registers for sublink zero and one, so there
			 * will be two table entries. So, here we only loop
			 * through the sublink zeros in function zero.
			 */
			for (j = 0; j < 4; j++) {
				if (AMD_CpuFindCapability(node, j, &offset)) {
					if (AMD_checkLinkType(node, offset)
					    & fam10_htphy_default[i].linktype) {
						AMD_SetHtPhyRegister(node, j,
								     i);
					}
				} else {
					/* No more capabilities,
					 * link not present
					 */
					break;
				}
			}
		}
	}

	/* FIXME: add UMA support and programXbarToSriReg(); */

	AMD_Errata281(node, revision, platform);

	/* FIXME: if the dct phy doesn't init correct it needs to reset.
	   if (revision & (AMD_DR_B2 | AMD_DR_B3))
	   dctPhyDiag(); */

	if (revision & (AMD_DR_GT_D0 | AMD_FAM15_ALL)) {
		/* Set up message triggered C1E */
		dword = pci_read_config32(NODE_PCI(node, 3), 0xd4);
		dword &= ~(0x1 << 14);			/* CacheFlushImmOnAllHalt = !is_fam15h() */
		dword |= (is_fam15h()?0:1) << 14;
		pci_write_config32(NODE_PCI(node, 3), 0xd4, dword);

		dword = pci_read_config32(NODE_PCI(node, 3), 0xdc);
		dword |= 0x1 << 26;			/* IgnCpuPrbEn = 1 */
		dword &= ~(0x7f << 19);			/* CacheFlushOnHaltTmr = 0x28 */
		dword |= 0x28 << 19;
		dword |= 0x7 << 16;			/* CacheFlushOnHaltCtl = 0x7 */
		pci_write_config32(NODE_PCI(node, 3), 0xdc, dword);

		dword = pci_read_config32(NODE_PCI(node, 3), 0xa0);
		dword |= 0x1 << 10;			/* IdleExitEn = 1 */
		pci_write_config32(NODE_PCI(node, 3), 0xa0, dword);

		if (revision & AMD_DR_GT_D0) {
			dword = pci_read_config32(NODE_PCI(node, 3), 0x188);
			dword |= 0x1 << 4;			/* EnStpGntOnFlushMaskWakeup = 1 */
			pci_write_config32(NODE_PCI(node, 3), 0x188, dword);
		} else {
			dword = pci_read_config32(NODE_PCI(node, 4), 0x128);
			dword &= ~(0x1 << 31);			/* CstateMsgDis = 0 */
			pci_write_config32(NODE_PCI(node, 4), 0x128, dword);
		}

		dword = pci_read_config32(NODE_PCI(node, 3), 0xd4);
		dword |= 0x1 << 13;			/* MTC1eEn = 1 */
		pci_write_config32(NODE_PCI(node, 3), 0xd4, dword);
	}

	if (revision & AMD_FAM15_ALL) {
		uint32_t f5x80;
		uint8_t cu_enabled;
		uint8_t compute_unit_count = 0;
		uint8_t compute_unit_buffer_count;

		uint32_t f3xe8;
		uint8_t dual_node = 0;

		f3xe8 = pci_read_config32(NODE_PCI(0, 3), 0xe8);

		/* Check for dual node capability */
		if (f3xe8 & 0x20000000)
			dual_node = 1;

		/* Determine the number of active compute units on this node */
		f5x80 = pci_read_config32(NODE_PCI(node, 5), 0x80);
		cu_enabled = f5x80 & 0xf;
		if (cu_enabled == 0x1)
			compute_unit_count = 1;
		if (cu_enabled == 0x3)
			compute_unit_count = 2;
		if (cu_enabled == 0x7)
			compute_unit_count = 3;
		if (cu_enabled == 0xf)
			compute_unit_count = 4;

		if (compute_unit_count == 1)
			compute_unit_buffer_count = 0x1c;
		else if (compute_unit_count == 2)
			compute_unit_buffer_count = 0x18;
		else if (compute_unit_count == 3)
			compute_unit_buffer_count = 0x14;
		else
			compute_unit_buffer_count = 0x10;

		dword = pci_read_config32(NODE_PCI(node, 3), 0x1a0);
		dword &= ~(0x1f << 4);			/* L3FreeListCBC = compute_unit_buffer_count */
		dword |= (compute_unit_buffer_count << 4);
		pci_write_config32(NODE_PCI(node, 3), 0x1a0, dword);

		uint8_t link;
		uint8_t link_real;
		uint8_t ganged;
		uint8_t iolink;
		uint8_t probe_filter_enabled = !!dual_node;

		/* Set up the Link Base Channel Buffer Count */
		uint8_t isoc_rsp_data;
		uint8_t isoc_np_req_data;
		uint8_t isoc_rsp_cmd;
		uint8_t isoc_preq;
		uint8_t isoc_np_req_cmd;
		uint8_t free_data;
		uint8_t free_cmd;
		uint8_t rsp_data;
		uint8_t np_req_data;
		uint8_t probe_cmd;
		uint8_t rsp_cmd;
		uint8_t preq;
		uint8_t np_req_cmd;

		/* Common settings for all links and system configurations */
		isoc_rsp_data = 0;
		isoc_np_req_data = 0;
		isoc_rsp_cmd = 0;
		isoc_preq = 0;
		isoc_np_req_cmd = 1;
		free_cmd = 8;

		for (link = 0; link < 4; link++) {
			if (AMD_CpuFindCapability(node, link, &offset)) {
				link_real = (offset - 0x80) / 0x20;
				ganged = !!(pci_read_config32(NODE_PCI(node, 0), (link_real << 2) + 0x170) & 0x1);
				iolink = !!(AMD_checkLinkType(node, offset) & HTPHY_LINKTYPE_NONCOHERENT);

				if (!iolink && ganged) {
					if (probe_filter_enabled) {
						free_data = 0;
						rsp_data = 3;
						np_req_data = 3;
						probe_cmd = 4;
						rsp_cmd = 9;
						preq = 2;
						np_req_cmd = 8;
					} else {
						free_data = 0;
						rsp_data = 3;
						np_req_data = 3;
						probe_cmd = 8;
						rsp_cmd = 9;
						preq = 2;
						np_req_cmd = 4;
					}
				} else if (!iolink && !ganged) {
					if (probe_filter_enabled) {
						free_data = 0;
						rsp_data = 3;
						np_req_data = 3;
						probe_cmd = 4;
						rsp_cmd = 9;
						preq = 2;
						np_req_cmd = 8;
					} else {
						free_data = 0;
						rsp_data = 3;
						np_req_data = 3;
						probe_cmd = 8;
						rsp_cmd = 9;
						preq = 2;
						np_req_cmd = 4;
					}
				} else if (iolink && ganged) {
					free_data = 0;
					rsp_data = 1;
					np_req_data = 0;
					probe_cmd = 0;
					rsp_cmd = 2;
					preq = 7;
					np_req_cmd = 14;
				} else {
					/* FIXME
					 * This is an educated guess as the BKDG does not specify
					 * the appropriate buffer counts for this case!
					 */
					free_data = 1;
					rsp_data = 1;
					np_req_data = 1;
					probe_cmd = 0;
					rsp_cmd = 2;
					preq = 4;
					np_req_cmd = 12;
				}

				dword = pci_read_config32(NODE_PCI(node, 0), (link_real * 0x20) + 0x94);
				dword &= ~(0x3 << 27);			/* IsocRspData = isoc_rsp_data */
				dword |= ((isoc_rsp_data & 0x3) << 27);
				dword &= ~(0x3 << 25);			/* IsocNpReqData = isoc_np_req_data */
				dword |= ((isoc_np_req_data & 0x3) << 25);
				dword &= ~(0x7 << 22);			/* IsocRspCmd = isoc_rsp_cmd */
				dword |= ((isoc_rsp_cmd & 0x7) << 22);
				dword &= ~(0x7 << 19);			/* IsocPReq = isoc_preq */
				dword |= ((isoc_preq & 0x7) << 19);
				dword &= ~(0x7 << 16);			/* IsocNpReqCmd = isoc_np_req_cmd */
				dword |= ((isoc_np_req_cmd & 0x7) << 16);
				pci_write_config32(NODE_PCI(node, 0), (link_real * 0x20) + 0x94, dword);

				dword = pci_read_config32(NODE_PCI(node, 0), (link_real * 0x20) + 0x90);
				dword &= ~(0x1 << 31);			/* LockBc = 0x1 */
				dword |= ((0x1 & 0x1) << 31);
				dword &= ~(0x7 << 25);			/* FreeData = free_data */
				dword |= ((free_data & 0x7) << 25);
				dword &= ~(0x1f << 20);			/* FreeCmd = free_cmd */
				dword |= ((free_cmd & 0x1f) << 20);
				dword &= ~(0x3 << 18);			/* RspData = rsp_data */
				dword |= ((rsp_data & 0x3) << 18);
				dword &= ~(0x3 << 16);			/* NpReqData = np_req_data */
				dword |= ((np_req_data & 0x3) << 16);
				dword &= ~(0xf << 12);			/* ProbeCmd = probe_cmd */
				dword |= ((probe_cmd & 0xf) << 12);
				dword &= ~(0xf << 8);			/* RspCmd = rsp_cmd */
				dword |= ((rsp_cmd & 0xf) << 8);
				dword &= ~(0x7 << 5);			/* PReq = preq */
				dword |= ((preq & 0x7) << 5);
				dword &= ~(0x1f << 0);			/* NpReqCmd = np_req_cmd */
				dword |= ((np_req_cmd & 0x1f) << 0);
				pci_write_config32(NODE_PCI(node, 0), (link_real * 0x20) + 0x90, dword);
			}
		}

		/* Set up the Link to XCS Token Counts */
		uint8_t isoc_rsp_tok_1;
		uint8_t isoc_preq_tok_1;
		uint8_t isoc_req_tok_1;
		uint8_t probe_tok_1;
		uint8_t rsp_tok_1;
		uint8_t preq_tok_1;
		uint8_t req_tok_1;
		uint8_t isoc_rsp_tok_0;
		uint8_t isoc_preq_tok_0;
		uint8_t isoc_req_tok_0;
		uint8_t free_tokens;
		uint8_t probe_tok_0;
		uint8_t rsp_tok_0;
		uint8_t preq_tok_0;
		uint8_t req_tok_0;

		for (link = 0; link < 4; link++) {
			if (AMD_CpuFindCapability(node, link, &offset)) {
				link_real = (offset - 0x80) / 0x20;
				ganged = !!(pci_read_config32(NODE_PCI(node, 0), (link_real << 2) + 0x170) & 0x1);
				iolink = !!(AMD_checkLinkType(node, offset) & HTPHY_LINKTYPE_NONCOHERENT);

				/* Set defaults */
				isoc_rsp_tok_1 = 0;
				isoc_preq_tok_1 = 0;
				isoc_req_tok_1 = 0;
				probe_tok_1 = !ganged;
				rsp_tok_1 = !ganged;
				preq_tok_1 = !ganged;
				req_tok_1 = !ganged;
				isoc_rsp_tok_0 = 0;
				isoc_preq_tok_0 = 0;
				isoc_req_tok_0 = 0;
				free_tokens = 0;
				probe_tok_0 = ((ganged)?2:1);
				rsp_tok_0 = ((ganged)?2:1);
				preq_tok_0 = ((ganged)?2:1);
				req_tok_0 = ((ganged)?2:1);

				if (!iolink && ganged) {
					if (!dual_node) {
						isoc_rsp_tok_1 = 0;
						isoc_preq_tok_1 = 0;
						isoc_req_tok_1 = 0;
						probe_tok_1 = 0;
						rsp_tok_1 = 0;
						preq_tok_1 = 0;
						req_tok_1 = 0;
						isoc_rsp_tok_0 = 0;
						isoc_preq_tok_0 = 0;
						isoc_req_tok_0 = 1;
						free_tokens = 3;
						probe_tok_0 = 2;
						rsp_tok_0 = 2;
						preq_tok_0 = 2;
						req_tok_0 = 2;
					} else {
						if ((sockets == 1)
							|| ((sockets == 2) && (sockets_populated == 1))) {
							isoc_rsp_tok_1 = 0;
							isoc_preq_tok_1 = 0;
							isoc_req_tok_1 = 0;
							probe_tok_1 = 0;
							rsp_tok_1 = 0;
							preq_tok_1 = 0;
							req_tok_1 = 0;
							isoc_rsp_tok_0 = 0;
							isoc_preq_tok_0 = 0;
							isoc_req_tok_0 = 1;
							free_tokens = 0;
							probe_tok_0 = 2;
							rsp_tok_0 = 2;
							preq_tok_0 = 2;
							req_tok_0 = 2;
						} else if (((sockets == 2) && (sockets_populated == 2))
							|| ((sockets == 4) && (sockets_populated == 2))) {
							isoc_rsp_tok_1 = 0;
							isoc_preq_tok_1 = 0;
							isoc_req_tok_1 = 0;
							probe_tok_1 = 0;
							rsp_tok_1 = 0;
							preq_tok_1 = 0;
							req_tok_1 = 0;
							isoc_rsp_tok_0 = 0;
							isoc_preq_tok_0 = 0;
							isoc_req_tok_0 = 1;
							free_tokens = 0;
							probe_tok_0 = 1;
							rsp_tok_0 = 2;
							preq_tok_0 = 2;
							req_tok_0 = 2;
						} else if ((sockets == 4) && (sockets_populated == 4)) {
							isoc_rsp_tok_1 = 0;
							isoc_preq_tok_1 = 0;
							isoc_req_tok_1 = 0;
							probe_tok_1 = 0;
							rsp_tok_1 = 0;
							preq_tok_1 = 0;
							req_tok_1 = 0;
							isoc_rsp_tok_0 = 0;
							isoc_preq_tok_0 = 0;
							isoc_req_tok_0 = 1;
							free_tokens = 0;
							probe_tok_0 = 2;
							rsp_tok_0 = 1;
							preq_tok_0 = 1;
							req_tok_0 = 2;
						}
					}
				} else if (!iolink && !ganged) {
					if ((sockets == 1)
						|| ((sockets == 2) && (sockets_populated == 1))) {
						if (probe_filter_enabled) {
							isoc_rsp_tok_1 = 0;
							isoc_preq_tok_1 = 0;
							isoc_req_tok_1 = 0;
							probe_tok_1 = 1;
							rsp_tok_1 = 1;
							preq_tok_1 = 1;
							req_tok_1 = 1;
							isoc_rsp_tok_0 = 0;
							isoc_preq_tok_0 = 0;
							isoc_req_tok_0 = 1;
							free_tokens = 0;
							probe_tok_0 = 1;
							rsp_tok_0 = 2;
							preq_tok_0 = 1;
							req_tok_0 = 1;
						} else {
							isoc_rsp_tok_1 = 0;
							isoc_preq_tok_1 = 0;
							isoc_req_tok_1 = 0;
							probe_tok_1 = 1;
							rsp_tok_1 = 1;
							preq_tok_1 = 1;
							req_tok_1 = 1;
							isoc_rsp_tok_0 = 0;
							isoc_preq_tok_0 = 0;
							isoc_req_tok_0 = 1;
							free_tokens = 0;
							probe_tok_0 = 1;
							rsp_tok_0 = 1;
							preq_tok_0 = 1;
							req_tok_0 = 1;
						}
					} else if ((sockets == 2) && (sockets_populated == 2)) {
						isoc_rsp_tok_1 = 0;
						isoc_preq_tok_1 = 0;
						isoc_req_tok_1 = 1;
						probe_tok_1 = 1;
						rsp_tok_1 = 1;
						preq_tok_1 = 1;
						req_tok_1 = 1;
						isoc_rsp_tok_0 = 0;
						isoc_preq_tok_0 = 0;
						isoc_req_tok_0 = 1;
						free_tokens = 2;
						probe_tok_0 = 1;
						rsp_tok_0 = 1;
						preq_tok_0 = 1;
						req_tok_0 = 1;
					} else if ((sockets == 4) && (sockets_populated == 2)) {
						isoc_rsp_tok_1 = 0;
						isoc_preq_tok_1 = 0;
						isoc_req_tok_1 = 1;
						probe_tok_1 = 1;
						rsp_tok_1 = 1;
						preq_tok_1 = 1;
						req_tok_1 = 1;
						isoc_rsp_tok_0 = 0;
						isoc_preq_tok_0 = 0;
						isoc_req_tok_0 = 1;
						free_tokens = 4;
						probe_tok_0 = 1;
						rsp_tok_0 = 1;
						preq_tok_0 = 1;
						req_tok_0 = 1;
					} else if ((sockets == 4) && (sockets_populated == 4)) {
						isoc_rsp_tok_1 = 0;
						isoc_preq_tok_1 = 0;
						isoc_req_tok_1 = 1;
						probe_tok_1 = 1;
						rsp_tok_1 = 1;
						preq_tok_1 = 1;
						req_tok_1 = 1;
						isoc_rsp_tok_0 = 0;
						isoc_preq_tok_0 = 0;
						isoc_req_tok_0 = 1;
						free_tokens = 0;
						probe_tok_0 = 1;
						rsp_tok_0 = 1;
						preq_tok_0 = 1;
						req_tok_0 = 1;
					}
				} else if (iolink && ganged) {
					if (!dual_node) {
						isoc_rsp_tok_1 = 0;
						isoc_preq_tok_1 = 0;
						isoc_req_tok_1 = 0;
						probe_tok_1 = 0;
						rsp_tok_1 = 0;
						preq_tok_1 = 0;
						req_tok_1 = 0;
						isoc_rsp_tok_0 = 0;
						isoc_preq_tok_0 = 0;
						isoc_req_tok_0 = 1;
						free_tokens = 3;
						probe_tok_0 = 0;
						rsp_tok_0 = 2;
						preq_tok_0 = 2;
						req_tok_0 = 2;
					} else if ((sockets == 1)
						|| (sockets == 2)
						|| ((sockets == 4) && (sockets_populated == 2))) {
						isoc_rsp_tok_1 = 0;
						isoc_preq_tok_1 = 0;
						isoc_req_tok_1 = 0;
						probe_tok_1 = 0;
						rsp_tok_1 = 0;
						preq_tok_1 = 0;
						req_tok_1 = 0;
						isoc_rsp_tok_0 = 0;
						isoc_preq_tok_0 = 0;
						isoc_req_tok_0 = 1;
						free_tokens = 0;
						probe_tok_0 = 0;
						rsp_tok_0 = 2;
						preq_tok_0 = 2;
						req_tok_0 = 2;
					} else if ((sockets == 4) && (sockets_populated == 4)) {
						isoc_rsp_tok_1 = 0;
						isoc_preq_tok_1 = 0;
						isoc_req_tok_1 = 0;
						probe_tok_1 = 0;
						rsp_tok_1 = 0;
						preq_tok_1 = 0;
						req_tok_1 = 0;
						isoc_rsp_tok_0 = 0;
						isoc_preq_tok_0 = 0;
						isoc_req_tok_0 = 2;
						free_tokens = 0;
						probe_tok_0 = 2;
						rsp_tok_0 = 2;
						preq_tok_0 = 2;
						req_tok_0 = 2;
					}
				}

				dword = pci_read_config32(NODE_PCI(node, 3), (link_real << 2) + 0x148);
				dword &= ~(0x3 << 30);			/* FreeTok[3:2] = free_tokens[3:2] */
				dword |= (((free_tokens >> 2) & 0x3) << 30);
				dword &= ~(0x1 << 28);			/* IsocRspTok1 = isoc_rsp_tok_1 */
				dword |= (((isoc_rsp_tok_1) & 0x1) << 28);
				dword &= ~(0x1 << 26);			/* IsocPreqTok1 = isoc_preq_tok_1 */
				dword |= (((isoc_preq_tok_1) & 0x1) << 26);
				dword &= ~(0x1 << 24);			/* IsocReqTok1 = isoc_req_tok_1 */
				dword |= (((isoc_req_tok_1) & 0x1) << 24);
				dword &= ~(0x3 << 22);			/* ProbeTok1 = probe_tok_1 */
				dword |= (((probe_tok_1) & 0x3) << 22);
				dword &= ~(0x3 << 20);			/* RspTok1 = rsp_tok_1 */
				dword |= (((rsp_tok_1) & 0x3) << 20);
				dword &= ~(0x3 << 18);			/* PReqTok1 = preq_tok_1 */
				dword |= (((preq_tok_1) & 0x3) << 18);
				dword &= ~(0x3 << 16);			/* ReqTok1 = req_tok_1 */
				dword |= (((req_tok_1) & 0x3) << 16);
				dword &= ~(0x3 << 14);			/* FreeTok[1:0] = free_tokens[1:0] */
				dword |= (((free_tokens) & 0x3) << 14);
				dword &= ~(0x3 << 12);			/* IsocRspTok0 = isoc_rsp_tok_0 */
				dword |= (((isoc_rsp_tok_0) & 0x3) << 12);
				dword &= ~(0x3 << 10);			/* IsocPreqTok0 = isoc_preq_tok_0 */
				dword |= (((isoc_preq_tok_0) & 0x3) << 10);
				dword &= ~(0x3 << 8);			/* IsocReqTok0 = isoc_req_tok_0 */
				dword |= (((isoc_req_tok_0) & 0x3) << 8);
				dword &= ~(0x3 << 6);			/* ProbeTok0 = probe_tok_0 */
				dword |= (((probe_tok_0) & 0x3) << 6);
				dword &= ~(0x3 << 4);			/* RspTok0 = rsp_tok_0 */
				dword |= (((rsp_tok_0) & 0x3) << 4);
				dword &= ~(0x3 << 2);			/* PReqTok0 = preq_tok_0 */
				dword |= (((preq_tok_0) & 0x3) << 2);
				dword &= ~(0x3 << 0);			/* ReqTok0 = req_tok_0 */
				dword |= (((req_tok_0) & 0x3) << 0);
				pci_write_config32(NODE_PCI(node, 3), (link_real << 2) + 0x148, dword);
			}
		}

		/* Set up the SRI to XCS Token Count */
		uint8_t free_tok;
		uint8_t up_rsp_tok;

		/* Set defaults */
		free_tok = 0xa;
		up_rsp_tok = 0x3;

		if (!dual_node) {
			free_tok = 0xa;
			up_rsp_tok = 0x3;
		} else {
			if ((sockets == 1)
				|| ((sockets == 2) && (sockets_populated == 1))) {
				if (probe_filter_enabled) {
					free_tok = 0x9;
					up_rsp_tok = 0x3;
				} else {
					free_tok = 0xa;
					up_rsp_tok = 0x3;
				}
			} else if ((sockets == 2) && (sockets_populated == 2)) {
				free_tok = 0xb;
				up_rsp_tok = 0x1;
			} else if ((sockets == 4) && (sockets_populated == 2)) {
				free_tok = 0xa;
				up_rsp_tok = 0x3;
			} else if ((sockets == 4) && (sockets_populated == 4)) {
				free_tok = 0x9;
				up_rsp_tok = 0x1;
			}
		}

		dword = pci_read_config32(NODE_PCI(node, 3), 0x140);
		dword &= ~(0xf << 20);			/* FreeTok = free_tok */
		dword |= ((free_tok & 0xf) << 20);
		dword &= ~(0x3 << 8);			/* UpRspTok = up_rsp_tok */
		dword |= ((up_rsp_tok & 0x3) << 8);
		pci_write_config32(NODE_PCI(node, 3), 0x140, dword);
	}

	uint8_t link;
	uint8_t link_real;
	uint8_t isochronous;
	uint8_t isochronous_link_present;

	/* Set up isochronous buffers if needed */
	isochronous_link_present = 0;
	if (revision & AMD_FAM15_ALL) {
		for (link = 0; link < 4; link++) {
			if (AMD_CpuFindCapability(node, link, &offset)) {
				link_real = (offset - 0x80) / 0x20;
				isochronous = (pci_read_config32(NODE_PCI(node, 0), (link_real * 0x20) + 0x84) >> 12) & 0x1;

				if (isochronous)
					isochronous_link_present = 1;
			}
		}
	}

	uint8_t free_tok;
	uint8_t up_rsp_cbc;
	uint8_t isoc_preq_cbc;
	uint8_t isoc_preq_tok;
	uint8_t xbar_to_sri_free_list_cbc;
	if (isochronous_link_present) {
		/* Adjust buffer counts */
		dword = pci_read_config32(NODE_PCI(node, 3), 0x70);
		isoc_preq_cbc = (dword >> 24) & 0x7;
		up_rsp_cbc = (dword >> 16) & 0x7;
		up_rsp_cbc--;
		isoc_preq_cbc++;
		dword &= ~(0x7 << 24);			/* IsocPreqCBC = isoc_preq_cbc */
		dword |= ((isoc_preq_cbc & 0x7) << 24);
		dword &= ~(0x7 << 16);			/* UpRspCBC = up_rsp_cbc */
		dword |= ((up_rsp_cbc & 0x7) << 16);
		pci_write_config32(NODE_PCI(node, 3), 0x70, dword);

		dword = pci_read_config32(NODE_PCI(node, 3), 0x74);
		isoc_preq_cbc = (dword >> 24) & 0x7;
		isoc_preq_cbc++;
		dword &= ~(0x7 << 24);			/* IsocPreqCBC = isoc_preq_cbc */
		dword |= (isoc_preq_cbc & 0x7) << 24;
		pci_write_config32(NODE_PCI(node, 3), 0x74, dword);

		dword = pci_read_config32(NODE_PCI(node, 3), 0x7c);
		xbar_to_sri_free_list_cbc = dword & 0x1f;
		xbar_to_sri_free_list_cbc--;
		dword &= ~0x1f;				/* Xbar2SriFreeListCBC = xbar_to_sri_free_list_cbc */
		dword |= xbar_to_sri_free_list_cbc & 0x1f;
		pci_write_config32(NODE_PCI(node, 3), 0x7c, dword);

		dword = pci_read_config32(NODE_PCI(node, 3), 0x140);
		free_tok = (dword >> 20) & 0xf;
		isoc_preq_tok = (dword >> 14) & 0x3;
		free_tok--;
		isoc_preq_tok++;
		dword &= ~(0xf << 20);			/* FreeTok = free_tok */
		dword |= ((free_tok & 0xf) << 20);
		dword &= ~(0x3 << 14);			/* IsocPreqTok = isoc_preq_tok */
		dword |= ((isoc_preq_tok & 0x3) << 14);
		pci_write_config32(NODE_PCI(node, 3), 0x140, dword);
	}

	printk(BIOS_DEBUG, " done\n");
}

#ifdef UNUSED_CODE
/* Clearing the MCA registers is apparently handled in the ramstage CPU Function 3 driver */
static void cpuInitializeMCA(void)
{
	/* Clears Machine Check Architecture (MCA) registers, which power on
	 * containing unknown data, on currently running processor.
	 * This routine should only be executed on initial power on (cold boot),
	 * not across a warm reset because valid data is present at that time.
	 */

	msr_t msr;
	u32 reg;
	u8 i;

	if (cpuid_edx(1) & 0x4080) {	/* MCE and MCA (edx[7] and edx[14]) */
		msr = rdmsr(IA32_MCG_CAP);
		if (msr.lo & MCG_CTL_P) {	/* MCG_CTL_P bit is set? */
			msr.lo &= 0xFF;
			msr.lo--;
			msr.lo <<= 2;	/* multiply the count by 4 */
			reg = IA32_MC0_STATUS + msr.lo;
			msr.lo = msr.hi = 0;
			for (i = 0; i < 4; i++) {
				wrmsr(reg, msr);
				reg -= 4;	/* Touch status regs for each bank */
			}
		}
	}
}
#endif

/**
 * finalize_node_setup()
 *
 * Do any additional post HT init
 *
 */
void finalize_node_setup(struct sys_info *sysinfo)
{
	u8 i;
	u8 nodes = get_nodes();
	u32 reg;

	/* read Node0 F0_0x64 bit [8:10] to find out SbLink # */
	reg = pci_read_config32(NODE_HT(0), 0x64);
	sysinfo->sblk = (reg >> 8) & 7;
	sysinfo->sbbusn = 0;
	sysinfo->nodes = nodes;
	sysinfo->sbdn = get_sbdn(sysinfo->sbbusn);

	for (i = 0; i < nodes; i++) {
		cpuSetAMDPCI(i);
	}

#if CONFIG(SET_FIDVID)
	// Prep each node for FID/VID setup.
	prep_fid_change();
#endif

#if CONFIG_MAX_PHYSICAL_CPUS > 1
	/* Skip the BSP, start at node 1 */
	for (i = 1; i < nodes; i++) {
		setup_remote_node(i);
		start_node(i);
	}
#endif
}

#if CONFIG(SET_FIDVID)
# include "fidvid.c"
#endif