summaryrefslogtreecommitdiffstats
path: root/src/import/chips/p9/procedures/hwp/nest/p9_mss_setup_bars.C
blob: 46a87c966aa94e34dfccce40791db4287ceb93e4 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/nest/p9_mss_setup_bars.C $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] Inspur Power Systems Corp.                                         */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/// ----------------------------------------------------------------------------
/// @file  p9_mss_setup_bars.H
///
/// @brief  Program memory controller base address registers (BARs)
///
/// ----------------------------------------------------------------------------
/// *HWP HWP Owner   : Joe McGill <jmcgill@us.ibm.com>
/// *HWP FW Owner    : Thi Tran <thi@us.ibm.com>
/// *HWP Team        : Nest
/// *HWP Level       : 3
/// *HWP Consumed by : HB
/// ----------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include <p9_mss_setup_bars.H>
#include <p9_mss_eff_grouping.H>
#include <p9_mc_scom_addresses.H>
#include <p9_mc_scom_addresses_fld.H>
#include <p9n2_mc_scom_addresses.H>
#include <p9n2_mc_scom_addresses_fld.H>
#include <p9a_mc_scom_addresses.H>
#include <p9a_mc_scom_addresses_fld.H>
#include <p9a_misc_scom_addresses.H>
#include <p9a_misc_scom_addresses_fld.H>
#include <p9a_addr_ext.H>
#include <map>
#include <lib/shared/mss_const.H>
#include <generic/memory/lib/utils/memory_size.H>
#include <lib/inband/exp_inband.H>

///----------------------------------------------------------------------------
/// Constant definitions
///----------------------------------------------------------------------------
const uint8_t USTL_MDI_EQUAL_ONE   = 1;
const uint8_t MAX_MC_PORTS_PER_MCS = 2;        // 2 MC ports per MCS
const uint8_t NO_CHANNEL_PER_GROUP = 0xFF;     // Init value of channel per group

///----------------------------------------------------------------------------
/// Data structure definitions
///----------------------------------------------------------------------------

///
/// @struct channel_per_group_t
/// @brief Table of Channel Per Group value in MCFGP reg based on
///        the # of ports in group of ports 0 and 1 of an MCS.
///
static const struct channelPerGroupTable_t
{
    uint8_t port0_ports_in_group;
    uint8_t port1_ports_in_group;
    uint8_t channel_per_group;

} CHANNEL_PER_GROUP_TABLE[] =
{
    // Port 0    Port 1    Channel/group value
    {    1,         1,           0b0000    },      // 1 MC port/group for both port0 and port1
    {    1,         3,           0b0001    },      // 1 MC port/group for port0, 3 MC port/group for port1
    {    2,         1,           0b0100    },      // 2 MC port/group different MC port pairs
    {    3,         1,           0b0010    },      // 3 MC port/group for port0, 1 MC port/group for port1
    {    3,         3,           0b0011    },      // 3 MC port/group for port0, 3 MC port/group for port1
    {    2,         2,           0b0101    },      // 2 MC port/group in the same MC port pairs (Need additional verification in code below)
    {    2,         3,           0b0100    },      // 2 MC port/group different MC port pairs
    {    1,         2,           0b0100    },      // 2 MC port/group different MC port pairs
    {    3,         2,           0b0100    },      // 2 MC port/group different MC port pairs
    {    4,         4,           0b0110    },      // 4 MC ports/group, two ports in the same MC pairs
    {    6,         6,           0b0111    },      // 6 MC ports/group, two ports in the same MC pairs
    {    8,         8,           0b1000    },      // 8 MC ports/group, two ports in the same MC pairs
};


///
/// @struct group_size_t
/// @brief Table that determines MCFGP/MCFGPM bits 13:23 based on the group size.
///
static const struct groupSizeTable_t
{
    // System attributes
    uint32_t groupSize;
    uint32_t encodedGroupSize;

} GROUP_SIZE_TABLE[] =
{
    // GroupSize  Encoded GroupSize
    {      4,        0b00000000000    },      //    4 GB
    {      8,        0b00000000001    },      //    8 GB
    {     16,        0b00000000011    },      //   16 GB
    {     32,        0b00000000111    },      //   32 GB
    {     64,        0b00000001111    },      //   64 GB
    {    128,        0b00000011111    },      //  128 GB
    {    256,        0b00000111111    },      //  256 GB
    {    512,        0b00001111111    },      //  512 GB
    {   1024,        0b00011111111    },      //    1 TB
    {   2048,        0b00111111111    },      //    2 TB
    {   4096,        0b01111111111    },      //    4 TB
    {   8192,    0b000011111111111    },      //    8 TB
    {  16384,    0b000111111111111    },      //   16 TB
    {  32768,    0b001111111111111    },      //   32 TB
    {  65536,    0b011111111111111    },      //   64 TB
    { 131072,    0b111111111111111    }       //  128 TB
};

/**
 * @struct mcPortGroupInfo_t
 *
 * Contains group data information related to a port (MCA/DMI).
 * This information is used to determine the channel per group
 * value for the MCFGP reg.
 *
 */
struct mcPortGroupInfo_t
{
    /**
     * @brief Default constructor. Initializes instance variables to zero
     */
    inline mcPortGroupInfo_t()
        : myGroup(0), numPortsInGroup(0), groupSize(0), groupBaseAddr(0),
          channelId(0), smfMemValid(0), smfMemSize(0), smfBaseAddr(0)
    {
        memset(altMemValid, 0, sizeof(altMemValid));
        memset(altMemSize, 0, sizeof(altMemSize));
        memset(altBaseAddr, 0, sizeof(altBaseAddr));
    }
    // The group number which this port belongs to
    uint8_t myGroup;
    // # of ports in the group which this port belongs to.
    uint8_t numPortsInGroup;
    // The size of the group which this port belongs to
    uint32_t groupSize;
    // The base address of the group which this port belongs to
    uint32_t groupBaseAddr;
    // The group member ID of this port
    uint8_t channelId;

    // ALT_MEM
    uint8_t altMemValid[NUM_OF_ALT_MEM_REGIONS];
    uint32_t altMemSize[NUM_OF_ALT_MEM_REGIONS];
    uint32_t altBaseAddr[NUM_OF_ALT_MEM_REGIONS];

    // SMF_MEM
    uint8_t smfMemValid;
    uint32_t smfMemSize;
    uint32_t smfBaseAddr;
};

/**
 * @struct mcBarData_t
 *
 * Contains BAR data info for a Memory Controller (MCS/MI)
 */
struct mcBarData_t
{
    /**
     * @brief Default constructor. Initializes instance variables to zero
     */
    inline mcBarData_t()
        : MCFGP_valid(false), MCFGP_chan_per_group(0),
          MCFGP_chan0_group_member_id(0), MCFGP_chan1_group_member_id(0),
          MCFGP_group_size(0), MCFGP_groupBaseAddr(0),
          MCFGPM_valid(false), MCFGPM_group_size(0), MCFGPM_groupBaseAddr(0),
          MCFGPA_SMF_valid(0), MCFGPA_SMF_LOWER_addr(0), MCFGPA_SMF_UPPER_addr(0),
          MCFGPMA_SMF_valid(0), MCFGPMA_SMF_LOWER_addr(0), MCFGPMA_SMF_UPPER_addr(0)
    {
        memset(MCFGPA_HOLE_valid, 0, sizeof(MCFGPA_HOLE_valid));
        memset(MCFGPA_HOLE_LOWER_addr, 0, sizeof(MCFGPA_HOLE_LOWER_addr));
        memset(MCFGPA_HOLE_UPPER_addr, 0, sizeof(MCFGPA_HOLE_UPPER_addr));

        memset(MCFGPMA_HOLE_valid, 0, sizeof(MCFGPMA_HOLE_valid));
        memset(MCFGPMA_HOLE_LOWER_addr, 0, sizeof(MCFGPMA_HOLE_LOWER_addr));
        memset(MCFGPMA_HOLE_UPPER_addr, 0, sizeof(MCFGPMA_HOLE_UPPER_addr));
    }

    // Info to program MCFGP reg
    bool     MCFGP_valid;
    uint8_t  MCFGP_chan_per_group;
    uint8_t  MCFGP_chan0_group_member_id;
    uint8_t  MCFGP_chan1_group_member_id;
    uint32_t MCFGP_group_size;
    uint32_t MCFGP_groupBaseAddr;

    // Info to program MCFGPM reg
    bool     MCFGPM_valid;
    uint32_t MCFGPM_group_size;
    uint32_t MCFGPM_groupBaseAddr;

    // Info to program MCFGPA reg
    bool     MCFGPA_HOLE_valid[NUM_OF_ALT_MEM_REGIONS];
    uint32_t MCFGPA_HOLE_LOWER_addr[NUM_OF_ALT_MEM_REGIONS];
    uint32_t MCFGPA_HOLE_UPPER_addr[NUM_OF_ALT_MEM_REGIONS];
    bool     MCFGPA_SMF_valid;
    uint32_t MCFGPA_SMF_LOWER_addr;
    uint32_t MCFGPA_SMF_UPPER_addr;

    // Info to program MCFGPMA reg
    bool     MCFGPMA_HOLE_valid[NUM_OF_ALT_MEM_REGIONS];
    uint32_t MCFGPMA_HOLE_LOWER_addr[NUM_OF_ALT_MEM_REGIONS];
    uint32_t MCFGPMA_HOLE_UPPER_addr[NUM_OF_ALT_MEM_REGIONS];
    bool     MCFGPMA_SMF_valid;
    uint32_t MCFGPMA_SMF_LOWER_addr;
    uint32_t MCFGPMA_SMF_UPPER_addr;
};

///----------------------------------------------------------------------------
/// Function definitions
///----------------------------------------------------------------------------
///
/// @brief Get MC(MCS/MI) position for the input PORT_ID
///
///        PORT_ID 0 --> MC 0
///        PORT_ID 1 --> MC 0
///        PORT_ID 2 --> MC 1
///        PORT_ID 3 --> MC 1
///        PORT_ID 4 --> MC 2
///        PORT_ID 5 --> MC 2
///        PORT_ID 6 --> MC 3
///        PORT_ID 7 --> MCS 3
///
/// @param[in]  i_portID      PortID
/// @return MC position
///
uint8_t getMCPosition(uint8_t i_portID)
{
    return (i_portID / 2);
}

///
/// @brief Get the port number (with respect to the MC, 0 or 1) for the
///        input PORT_ID
///
///        PORT_ID 0 --> MCS port 0
///        PORT_ID 1 --> MCS port 1
///        PORT_ID 2 --> MCS port 0
///        PORT_ID 3 --> MCS port 1
///        PORT_ID 4 --> MCS port 0
///        PORT_ID 5 --> MCS port 1
///        PORT_ID 6 --> MCS port 0
///        PORT_ID 7 --> MCS port 1
///
/// @param[in]  i_portID      PortID
/// @return port num
///
uint8_t getMCPortNum(uint8_t i_portID)
{
    uint8_t l_mcPos = getMCPosition(i_portID);
    return (i_portID - (2 * l_mcPos));
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
///
/// @brief Get the memory size behind a Memory controller
///        by calling into mss library.
///
/// @param[in]  i_target        MC target (MCS or MI)
/// @param[out] o_mcSize        The total mem size found
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode getMcMemSize(const fapi2::Target<T>& i_target,
                               uint64_t& o_mcSize);

/// MC = MCS (Nimbus)
template<>
fapi2::ReturnCode getMcMemSize(
    const fapi2::Target<fapi2::TARGET_TYPE_MCS>& i_target,
    uint64_t& o_mcSize)
{
    FAPI_DBG("Entering");

    // Figure out the amount of memory behind this MCS
    // by adding up all memory from its MCA ports
    auto l_mcaChiplets = i_target.getChildren<fapi2::TARGET_TYPE_MCA>();
    uint64_t l_mcaSize = 0;

    for (auto l_mca : l_mcaChiplets)
    {
        uint8_t l_mcaPos = 0;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, l_mca, l_mcaPos),
                 "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Get the amount of memory behind this MCA target
        FAPI_TRY(mss::eff_memory_size<mss::mc_type::NIMBUS>(l_mca, l_mcaSize),
                 "Error returned from eff_memory_size - MCA, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        FAPI_INF("MCA %u: Total DIMM size %lu GB", l_mcaPos, l_mcaSize);
        o_mcSize += l_mcaSize;
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

/// MC = MI (Cumulus)
template<>
fapi2::ReturnCode getMcMemSize(
    const fapi2::Target<fapi2::TARGET_TYPE_MI>& i_target,
    uint64_t& o_mcSize)
{
    FAPI_DBG("Entering");

    // Figure out the amount of memory behind this MI
    // by adding up all memory from its DMI ports
    auto l_dmiChiplets = i_target.getChildren<fapi2::TARGET_TYPE_DMI>();
    uint64_t l_chSize = 0;

    for (auto l_dmi : l_dmiChiplets)
    {
        uint8_t l_dmiPos = 0;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, l_dmi, l_dmiPos),
                 "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Get the amount of memory behind this DMI target
        FAPI_TRY(mss::eff_memory_size<mss::mc_type::CENTAUR>(l_dmi, l_chSize),
                 "Error returned from eff_memory_size - DMI, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        FAPI_INF("DMI %u: Total DIMM size %lu GB", l_dmiPos, l_chSize);
        o_mcSize += l_chSize;
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

/// MC = MCC (Axone)
template<>
fapi2::ReturnCode getMcMemSize(
    const fapi2::Target<fapi2::TARGET_TYPE_MCC>& i_target,
    uint64_t& o_mcSize)
{
    FAPI_DBG("Entering");

    // Figure out the amount of memory behind this MI
    // by adding up all memory from its OMI ports
    uint64_t l_chSize = 0;
    uint64_t l_sub_size[SUBCHANNEL_PER_CHANNEL];
    uint64_t l_sub_minsize = 0;
    uint64_t l_num_sub = 0;

    auto l_omiChiplets = i_target.getChildren<fapi2::TARGET_TYPE_OMI>();
    memset(l_sub_size, 0, sizeof(l_sub_size));

    for (auto l_omi : l_omiChiplets)
    {
        const auto& l_ocmb_chips = l_omi.getChildren<fapi2::TARGET_TYPE_OCMB_CHIP>();

        if (!l_ocmb_chips.empty())
        {
            uint8_t l_omiPos = 0;
            FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, l_omi, l_omiPos),
                     "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                     uint64_t(fapi2::current_err));

            // Get the amount of memory behind this OMI
            FAPI_TRY(mss::eff_memory_size<mss::mc_type::EXPLORER>(l_ocmb_chips[0], l_chSize),
                     "Error returned from eff_memory_size - ocmb, l_rc 0x%.8X",
                     uint64_t(fapi2::current_err));

            FAPI_INF("OMI %u: Total DIMM size %lu GB", l_omiPos, l_chSize);

            l_sub_size[l_omiPos % SUBCHANNEL_PER_CHANNEL] = l_chSize;

            if (l_chSize > 0)
            {
                l_num_sub++;

                if (l_sub_minsize == 0 || l_sub_minsize > l_chSize)
                {
                    l_sub_minsize = l_chSize;
                }
            }
        }
    }

    l_chSize = (l_num_sub * l_sub_minsize);
    o_mcSize += l_chSize;

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
///
/// @brief Calculate the memory size behind a Memory controller
///        from group data.
///
/// @param[in]  i_mcPos         MC position
/// @param[in]  i_omi           Chip uses OMI?
/// @param[in]  i_groupData     Array of Group data info
/// @param[out] o_portFound     Mark how many time a port is found.
/// @param[out] o_mcSize        The total mem size calculated
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
void getGroupDataMcMemSize(
    uint8_t i_mcPos,
    const bool i_omi,
    const uint32_t i_groupData[DATA_GROUPS][DATA_ELEMENTS],
    uint8_t o_portFound[NUM_MC_PORTS_PER_PROC],
    uint64_t& o_mcSize)
{
    FAPI_DBG("Entering");

    // Loop thru non-mirror groups (0-7)
    for (uint8_t l_group = 0; l_group < (DATA_GROUPS / 2); l_group++)
    {
        // Skip empty groups
        if (i_groupData[l_group][GROUP_SIZE] == 0)
        {
            continue;
        }

        // Loop thru the group port member index to determine if the
        // PORT_ID listed belongs to this MCS
        for (uint8_t l_memberIdx = 0;
             l_memberIdx < i_groupData[l_group][PORTS_IN_GROUP]; l_memberIdx++)
        {
            // If the PORT_ID listed belongs to this MC, add the amount
            // of memory behind the port to this MC.
            uint8_t l_mcId = getMCPosition(i_groupData[l_group][MEMBER_IDX(0) +
                                           l_memberIdx]);

            if ((!i_omi && l_mcId == i_mcPos) ||
                (i_omi && i_mcPos == i_groupData[l_group][MEMBER_IDX(0) + l_memberIdx]))
            {
                o_mcSize += i_groupData[l_group][PORT_SIZE];
                FAPI_INF("getGroupDataMcMemSize - Port %u, DIMM size %lu GB",
                         i_groupData[l_group][MEMBER_IDX(0) + l_memberIdx],
                         i_groupData[l_group][PORT_SIZE]);
                // Increase # of times this PORT_ID is found
                o_portFound[i_groupData[l_group][MEMBER_IDX(0) + l_memberIdx]]++;
            }

        } // Port loop

    } // Group loop

    FAPI_DBG("Exit");
    return;
}


///
/// @brief Validate group data received from ATTR_MSS_MCS_GROUP_32
///
/// Perform these verifications:
///   - The memory sizes of MCS/MI in the input group data
///     agrees with with the amount memory currently reported.
///   - An MCA/DMMI port can only appear once in any group.
///
/// @param[in]  i_mcTargets     Vector of reference of MC targets (MCS or MI)
/// @param[in]  i_omi           Chip uses OMI?
/// @param[in]  i_groupData     Array of Group data info
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode validateGroupData(
    const std::vector< fapi2::Target<T> >& i_mcTargets,
    const bool i_omi,
    const uint32_t i_groupData[DATA_GROUPS][DATA_ELEMENTS])
{
    FAPI_DBG("Entering");

    uint64_t l_mcSize = 0;
    uint64_t l_mcSizeGroupData = 0;
    uint8_t l_portFound[NUM_MC_PORTS_PER_PROC];

    // Initialize local arrays
    memset(l_portFound, 0, sizeof(l_portFound));

    // Loop thru each MC
    for (auto l_mc : i_mcTargets)
    {
        // Get this MCS unit position
        uint8_t l_mcPos = 0;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, l_mc, l_mcPos),
                 "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        FAPI_INF("validateGroupData: MC unit pos %d", l_mcPos);

        // Get the memory size behind this MC
        FAPI_TRY(getMcMemSize(l_mc, l_mcSize),
                 "Error returned from getMcMemSize, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Get this MC memsize reported in Group data
        getGroupDataMcMemSize(l_mcPos, i_omi, i_groupData, l_portFound,
                              l_mcSizeGroupData);

        FAPI_DBG("validateGroupData: MemSize %.16lld, Group Memsize %.16lld",
                 l_mcSize, l_mcSizeGroupData);

        // Assert if MC specified in Group data doesn't agree
        // with the amount gets from Memory interface.
        FAPI_ASSERT(l_mcSizeGroupData == l_mcSize,
                    fapi2::MSS_SETUP_BARS_MC_MEMSIZE_DISCREPENCY()
                    .set_TARGET(l_mc)
                    .set_MC_POS(l_mcPos)
                    .set_MEMSIZE_GROUP_DATA(l_mcSizeGroupData)
                    .set_MEMSIZE_REPORTED(l_mcSize),
                    "Error: MCS %u memory discrepancy: Group data size %u, "
                    "Current memory reported %u",
                    l_mcPos, l_mcSizeGroupData, l_mcSize);

    } // MC loop

    FAPI_INF("Total memory size= %lu GB", l_mcSize);

    // Assert if a PORT_ID is found more than once in any group
    for (uint8_t ii = 0; ii < NUM_MC_PORTS_PER_PROC; ii++)
    {
        // Assert if a PORT_ID is found more than once in any group
        FAPI_ASSERT(l_portFound[ii] <= 1,
                    fapi2::MSS_SETUP_BARS_MULTIPLE_GROUP_ERR()
                    .set_PORT_ID(ii)
                    .set_COUNTER(l_portFound[ii]),
                    "Error: PORT_ID %u is grouped multiple times. "
                    "Port %d, Counter %u", ii, l_portFound[ii]);
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Look up table to determine the MCFGP/MCFGPM group size
///        encoded value (bits 13:23).
///
/// @param[in]   i_mcTarget     MC target (MCS/MI)
/// @param[in]   i_groupSize    Group size (in GB)
/// @param[out]  o_value        Encoded value
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode getGroupSizeEncodedValue(
    const fapi2::Target<T>& i_mcTarget,
    const uint32_t i_groupSize,
    uint32_t& o_value)
{
    FAPI_DBG("Entering");

    bool l_sizeFound = false;

    for (uint8_t ii = 0;
         ii < (sizeof(GROUP_SIZE_TABLE) / sizeof(groupSizeTable_t));
         ii++)
    {
        if ( i_groupSize == GROUP_SIZE_TABLE[ii].groupSize)
        {
            o_value = GROUP_SIZE_TABLE[ii].encodedGroupSize;
            l_sizeFound = true;
            break;
        }
    }

    if (l_sizeFound == false)
    {
        uint8_t l_mcPos = 0;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, i_mcTarget, l_mcPos),
                 "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));
        // Assert if can't find Group size in the table
        FAPI_ASSERT( false,
                     fapi2::MSS_SETUP_BARS_INVALID_GROUP_SIZE()
                     .set_MC_TARGET(i_mcTarget)
                     .set_MC_POS(l_mcPos)
                     .set_GROUP_SIZE(i_groupSize),
                     "Error: Can't locate Group size value in GROUP_SIZE_TABLE. "
                     "MC pos: %d, GroupSize %u GB.", l_mcPos, i_groupSize );
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Calculate the BAR data for each MC (MCS/MI) based on group info
///        of port0/1
///
/// @param[in]  i_mcTarget     MC target (MCS/MI)
/// @param[in]  i_portInfo     The port group info
/// @param[in]  o_mcBarData    MC BAR data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode getNonMirrorBarData(const fapi2::Target<T>& i_mcTarget,
                                      const mcPortGroupInfo_t i_portInfo[],
                                      mcBarData_t& o_mcBarData)
{
    FAPI_DBG("Entering");

    // This function assign the MCFGP_MC_CHANNELS_PER_GROUP value
    // to the MC according to the rule listed in the Nimbus/Cumulus workbook.

    // Initialize
    o_mcBarData.MCFGP_chan_per_group = NO_CHANNEL_PER_GROUP;
    o_mcBarData.MCFGP_valid = false;
    o_mcBarData.MCFGPM_valid = false;

    // ----------------------------------------------------
    // Determine data for MCFGP and MCFGPM registers
    // ----------------------------------------------------

    // Channel per group (MCFGP bits 1:4)
    for (uint8_t ii = 0;
         ii < (sizeof(CHANNEL_PER_GROUP_TABLE) / sizeof(channelPerGroupTable_t));
         ii++)
    {
        uint8_t l_port0_lookup_val = i_portInfo[0].numPortsInGroup;
        uint8_t l_port1_lookup_val = i_portInfo[1].numPortsInGroup;

        // If port is disabled, treat as single port.  However, this
        // port will be set invalid in MCFGP reg further below
        //
        if (l_port0_lookup_val == 0)
        {
            l_port0_lookup_val = 1;
        }

        if (l_port1_lookup_val == 0)
        {
            l_port1_lookup_val = 1;
        }

        if ( (l_port0_lookup_val == CHANNEL_PER_GROUP_TABLE[ii].port0_ports_in_group) &&
             (l_port1_lookup_val == CHANNEL_PER_GROUP_TABLE[ii].port1_ports_in_group) )
        {
            o_mcBarData.MCFGP_chan_per_group = CHANNEL_PER_GROUP_TABLE[ii].channel_per_group;
        }
    }

    // Assert if ports 0/1 don't match any entry in table
    FAPI_ASSERT(o_mcBarData.MCFGP_chan_per_group != NO_CHANNEL_PER_GROUP,
                fapi2::MSS_SETUP_BARS_INVALID_PORTS_CONFIG()
                .set_MC_TARGET(i_mcTarget)
                .set_PORT_0_PORTS_IN_GROUP(i_portInfo[0].numPortsInGroup)
                .set_PORT_0_GROUP(i_portInfo[0].myGroup)
                .set_PORT_1_PORTS_IN_GROUP(i_portInfo[1].numPortsInGroup)
                .set_PORT_1_GROUP(i_portInfo[1].myGroup),
                "Error: ports 0/1 config doesn't match any entry in Channel/group table. "
                "Port_0: group %u, ports in group %u, Port_1: group %u, ports in group %u",
                i_portInfo[0].myGroup, i_portInfo[0].numPortsInGroup,
                i_portInfo[1].myGroup, i_portInfo[1].numPortsInGroup);


    // MCFGP valid (MCFGP bit 0)
    if ( i_portInfo[0].numPortsInGroup == 0)
    {
        // Port0 not populated
        o_mcBarData.MCFGP_valid = false;
    }
    else
    {
        // Port0 populated
        o_mcBarData.MCFGP_valid = true;
    }

    // MCFGPM valid (MCFGPM bit 0)
    if ( i_portInfo[1].numPortsInGroup == 0)
    {
        // Port1 not populated
        o_mcBarData.MCFGPM_valid = false;
    }
    else
    {
        // MCFGPM is valid if Channel_per_group < 0b0101
        if (o_mcBarData.MCFGP_chan_per_group < 0b0101)
        {
            o_mcBarData.MCFGPM_valid = true;
        }
        // Determine if MCFGPM valid when Channel_per_group = 0b0101
        else if (o_mcBarData.MCFGP_chan_per_group == 0b0101)
        {
            // Port1 populated, 2 MC/group
            // The table assigns 0b0101 if both ports belong 2 MC port/group,
            // Here, verify that they belong to the same group, if not,
            // re-assign the channel per group to 2 MC/group in different
            // MC port pairs (0b0100)
            if ( i_portInfo[0].myGroup != i_portInfo[1].myGroup )
            {
                o_mcBarData.MCFGP_chan_per_group = 0b0100;
                o_mcBarData.MCFGPM_valid = true;
            }
        }
        // MCFGPM is not valid if Channel_per_group > 0b0101
        // (2,4,6 or 8, and in same MC port pair)
        // This is true because mirroring is not supported on Nimbus.
        // For Cumulus, mirroring will be checked/programmed later
        // in another function.
        else
        {
            o_mcBarData.MCFGPM_valid = false;
        }
    }

    FAPI_TRY(getNonMirrorBarIdSize(i_mcTarget, i_portInfo, o_mcBarData));

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Calculate the BAR data for each MC (MCS/MI) based on group info
///        of port0/1
///
/// @param[in]  i_mcTarget     MC target (MCS/MI)
/// @param[in]  i_portInfo     The port group info
/// @param[in]  o_mcBarData    MC BAR data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode getNonMirrorBarIdSize(const fapi2::Target<T>& i_mcTarget,
                                        const mcPortGroupInfo_t i_portInfo[],
                                        mcBarData_t& o_mcBarData)
{
    // MCFGP Channel_0 Group member ID (bits 5:7)
    o_mcBarData.MCFGP_chan0_group_member_id = i_portInfo[0].channelId;
    // MCFGP Channel_1 Group member ID (bits 8:10)
    o_mcBarData.MCFGP_chan1_group_member_id = i_portInfo[1].channelId;

    // If MCFGP is valid, set other fields
    if (o_mcBarData.MCFGP_valid == true)
    {
        // MCFGP Group size
        FAPI_TRY(getGroupSizeEncodedValue(i_mcTarget, i_portInfo[0].groupSize,
                                          o_mcBarData.MCFGP_group_size),
                 "getGroupSizeEncodedValue() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Group base address
        o_mcBarData.MCFGP_groupBaseAddr = i_portInfo[0].groupBaseAddr;
    }

    // If MCFGPM is valid, set other fields
    if (o_mcBarData.MCFGPM_valid == true)
    {
        // MCFGPM Group size
        FAPI_TRY(getGroupSizeEncodedValue(i_mcTarget, i_portInfo[1].groupSize,
                                          o_mcBarData.MCFGPM_group_size),
                 "getGroupSizeEncodedValue() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Group base address
        o_mcBarData.MCFGPM_groupBaseAddr = i_portInfo[1].groupBaseAddr;
    }

    // ----------------------------------------------------
    // Determine data for MCFGPA and MCFGPMA registers
    // ----------------------------------------------------

    // Alternate Memory MCFGPA
    for (uint8_t ii = 0; ii < NUM_OF_ALT_MEM_REGIONS; ii++)
    {
        if ( o_mcBarData.MCFGP_valid && i_portInfo[0].altMemValid[ii] )
        {
            o_mcBarData.MCFGPA_HOLE_valid[ii] = 1;
            o_mcBarData.MCFGPA_HOLE_LOWER_addr[ii] = i_portInfo[0].altBaseAddr[ii];
            o_mcBarData.MCFGPA_HOLE_UPPER_addr[ii] =
                i_portInfo[0].altBaseAddr[ii] + i_portInfo[0].altMemSize[ii];
        }
        else
        {
            o_mcBarData.MCFGPA_HOLE_valid[ii] = 0;
            o_mcBarData.MCFGPA_HOLE_LOWER_addr[ii] = 0;
            o_mcBarData.MCFGPA_HOLE_UPPER_addr[ii] = 0;
        }

        if ( o_mcBarData.MCFGPM_valid && i_portInfo[1].altMemValid[ii] )
        {
            o_mcBarData.MCFGPMA_HOLE_valid[ii] = 1;
            o_mcBarData.MCFGPMA_HOLE_LOWER_addr[ii] = i_portInfo[1].altBaseAddr[ii];
            o_mcBarData.MCFGPMA_HOLE_UPPER_addr[ii] =
                i_portInfo[1].altBaseAddr[ii] + i_portInfo[1].altMemSize[ii];
        }
        else
        {
            o_mcBarData.MCFGPMA_HOLE_valid[ii] = 0;
            o_mcBarData.MCFGPMA_HOLE_LOWER_addr[ii] = 0;
            o_mcBarData.MCFGPMA_HOLE_UPPER_addr[ii] = 0;
        }

    }

    // SMF Section of MCFGPA and MCFGPMA
    if ( o_mcBarData.MCFGP_valid && i_portInfo[0].smfMemValid )
    {
        o_mcBarData.MCFGPA_SMF_valid = 1;
        o_mcBarData.MCFGPA_SMF_LOWER_addr = i_portInfo[0].smfBaseAddr;
        o_mcBarData.MCFGPA_SMF_UPPER_addr = i_portInfo[0].smfBaseAddr + i_portInfo[0].smfMemSize;
    }
    else
    {
        o_mcBarData.MCFGPA_SMF_valid = 0;
        o_mcBarData.MCFGPA_SMF_LOWER_addr = 0;
        o_mcBarData.MCFGPA_SMF_UPPER_addr = 0;
    }

    if ( o_mcBarData.MCFGPM_valid && i_portInfo[1].smfMemValid )
    {
        o_mcBarData.MCFGPMA_SMF_valid = 1;
        o_mcBarData.MCFGPMA_SMF_LOWER_addr = i_portInfo[1].smfBaseAddr;
        o_mcBarData.MCFGPMA_SMF_UPPER_addr = i_portInfo[1].smfBaseAddr + i_portInfo[1].smfMemSize;
    }
    else
    {
        o_mcBarData.MCFGPMA_SMF_valid = 0;
        o_mcBarData.MCFGPMA_SMF_LOWER_addr = 0;
        o_mcBarData.MCFGPMA_SMF_UPPER_addr = 0;
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Calculate the BAR data for each MC (MCS/MI) based on group info
///        of port0/1
///
/// @param[in]  i_mcTarget     MC target (MCS/MI)
/// @param[in]  i_portInfo     The port group info
/// @param[in]  o_mcBarData    MC BAR data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
fapi2::ReturnCode getNonMirrorBarIdSize(const fapi2::Target<fapi2::TARGET_TYPE_MCC>& i_mcTarget,
                                        const mcPortGroupInfo_t& i_portInfo,
                                        mcBarData_t& o_mcBarData)
{
    // MCFGP Channel_0 Group member ID (bits 5:7)
    o_mcBarData.MCFGP_chan0_group_member_id = i_portInfo.channelId;

    // If MCFGP is valid, set other fields
    if (o_mcBarData.MCFGP_valid == true)
    {
        // MCFGP Group size
        FAPI_TRY(getGroupSizeEncodedValue(i_mcTarget, i_portInfo.groupSize,
                                          o_mcBarData.MCFGP_group_size),
                 "getGroupSizeEncodedValue() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Group base address
        o_mcBarData.MCFGP_groupBaseAddr = i_portInfo.groupBaseAddr;
    }

    // If MCFGPM is valid, set other fields
    if (o_mcBarData.MCFGPM_valid == true)
    {
        // MCFGPM Group size
        FAPI_TRY(getGroupSizeEncodedValue(i_mcTarget, i_portInfo.groupSize,
                                          o_mcBarData.MCFGPM_group_size),
                 "getGroupSizeEncodedValue() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Group base address
        o_mcBarData.MCFGPM_groupBaseAddr = i_portInfo.groupBaseAddr;
    }

    // ----------------------------------------------------
    // Determine data for MCFGPA and MCFGPMA registers
    // ----------------------------------------------------

    // Alternate Memory MCFGPA
    for (uint8_t ii = 0; ii < NUM_OF_ALT_MEM_REGIONS; ii++)
    {
        if ( i_portInfo.altMemValid[ii] )
        {
            o_mcBarData.MCFGPA_HOLE_valid[ii] = 1;
            o_mcBarData.MCFGPA_HOLE_LOWER_addr[ii] = i_portInfo.altBaseAddr[ii];
            o_mcBarData.MCFGPA_HOLE_UPPER_addr[ii] =
                i_portInfo.altBaseAddr[ii] + i_portInfo.altMemSize[ii];
        }
        else
        {
            o_mcBarData.MCFGPA_HOLE_valid[ii] = 0;
            o_mcBarData.MCFGPA_HOLE_LOWER_addr[ii] = 0;
            o_mcBarData.MCFGPA_HOLE_UPPER_addr[ii] = 0;
        }

    }

    // SMF Section of MCFGPA and MCFGPMA
    if ( i_portInfo.smfMemValid )
    {
        o_mcBarData.MCFGPA_SMF_valid = 1;
        o_mcBarData.MCFGPA_SMF_LOWER_addr = i_portInfo.smfBaseAddr;
        o_mcBarData.MCFGPA_SMF_UPPER_addr = i_portInfo.smfBaseAddr + i_portInfo.smfMemSize;
    }
    else
    {
        o_mcBarData.MCFGPA_SMF_valid = 0;
        o_mcBarData.MCFGPA_SMF_LOWER_addr = 0;
        o_mcBarData.MCFGPA_SMF_UPPER_addr = 0;
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Calculate the BAR data for each MCC based on group info
///        of port0/1 - OMI specific
///
/// @param[in]  i_mcTarget     MC target (MCC)
/// @param[in]  i_portInfo     The port group info
/// @param[in]  o_mcBarData    MC BAR data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
fapi2::ReturnCode getNonMirrorBarData(const fapi2::Target<fapi2::TARGET_TYPE_MCC>& i_mcTarget,
                                      const mcPortGroupInfo_t& i_portInfo,
                                      mcBarData_t& o_mcBarData)
{
    FAPI_DBG("Entering");

    // Initialize
    o_mcBarData.MCFGP_chan_per_group = NO_CHANNEL_PER_GROUP;
    o_mcBarData.MCFGP_valid = false;
    o_mcBarData.MCFGPM_valid = false;

    // MCFGP valid (MCFGP bit 0)
    if ( i_portInfo.numPortsInGroup > 0)
    {
        o_mcBarData.MCFGP_valid = true;
        o_mcBarData.MCFGPM_valid = false;

        // ----------------------------------------------------
        // Determine data for MCFGP and MCFGPM registers
        // ----------------------------------------------------
        if (i_portInfo.numPortsInGroup == 8)
        {
            o_mcBarData.MCFGP_chan_per_group = 0;
        }
        else if (i_portInfo.numPortsInGroup == 1 ||
                 i_portInfo.numPortsInGroup == 2 ||
                 i_portInfo.numPortsInGroup == 3 ||
                 i_portInfo.numPortsInGroup == 4 ||
                 i_portInfo.numPortsInGroup == 6 )
        {
            o_mcBarData.MCFGP_chan_per_group = i_portInfo.numPortsInGroup;
        }

        // Assert if ports 0/1 don't match any entry in table
        FAPI_ASSERT(o_mcBarData.MCFGP_chan_per_group != NO_CHANNEL_PER_GROUP,
                    fapi2::MSS_SETUP_BARS_INVALID_PORTS_CONFIG()
                    .set_MC_TARGET(i_mcTarget)
                    .set_PORT_0_PORTS_IN_GROUP(i_portInfo.numPortsInGroup)
                    .set_PORT_0_GROUP(i_portInfo.myGroup)
                    .set_PORT_1_PORTS_IN_GROUP(0)
                    .set_PORT_1_GROUP(0),
                    "Error: Invalid number of ports per group"
                    "group %u, ports in group %u",
                    i_portInfo.myGroup, i_portInfo.numPortsInGroup);
    }

    FAPI_TRY(getNonMirrorBarIdSize(i_mcTarget, i_portInfo, o_mcBarData));

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Calculate the mirror BAR data for each MC based on group info
///        of port0/1
///
/// @param[in]  i_mcTarget     MC target (MCS/MI)
/// @param[in]  i_portInfo     The port group info
/// @param[in]  io_mcBarData   MC BAR data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode getMirrorBarData(const fapi2::Target<T>& i_mcTarget,
                                   const mcPortGroupInfo_t i_portInfo[],
                                   mcBarData_t& io_mcBarData)
{
    FAPI_DBG("Entering");

    // ---------------------------------------------------
    // Build MC register values for mirror groups
    // ---------------------------------------------------

    // Check MCFGP value to see if mirror is possible
    // (See Table 1 of P9 Cumulus Memory Controller Workbook)
    //
    if ( ((io_mcBarData.MCFGP_chan_per_group < 0b0101) ||
          (io_mcBarData.MCFGP_chan_per_group > 0b1000)))
    {
        FAPI_IMP("Mirror is not possible with MCFGP = 0x%.8X, NO MIRROR is "
                 "programmed. ", io_mcBarData.MCFGP_chan_per_group);
        goto fapi_try_exit;
    }

    // Set MCFGPM_VALID
    io_mcBarData.MCFGPM_valid = true;

    // ----------------------------------------------------
    // Determine data for MCFGPM register
    // ----------------------------------------------------

    // MCFGPM Group size
    FAPI_TRY(getGroupSizeEncodedValue(i_mcTarget, i_portInfo[1].groupSize,
                                      io_mcBarData.MCFGPM_group_size),
             "getGroupSizeEncodedValue() returns error, l_rc 0x%.8X",
             uint64_t(fapi2::current_err));

    // Group base address
    io_mcBarData.MCFGPM_groupBaseAddr = i_portInfo[1].groupBaseAddr;

    // ----------------------------------------------------
    // Determine data for MCFGPMA registers
    // ----------------------------------------------------
    // Alternate Memory MCFGPMA
    for (uint8_t ii = 0; ii < NUM_OF_ALT_MEM_REGIONS; ii++)
    {
        if ( i_portInfo[1].altMemValid[ii] )
        {
            io_mcBarData.MCFGPMA_HOLE_valid[ii] = 1;
            io_mcBarData.MCFGPMA_HOLE_LOWER_addr[ii] =
                i_portInfo[1].altBaseAddr[ii];
            io_mcBarData.MCFGPMA_HOLE_UPPER_addr[ii] =
                i_portInfo[1].altBaseAddr[ii] + i_portInfo[1].altMemSize[ii];
        }
        else
        {
            io_mcBarData.MCFGPMA_HOLE_valid[ii] = 0;
            io_mcBarData.MCFGPMA_HOLE_LOWER_addr[ii] = 0;
            io_mcBarData.MCFGPMA_HOLE_UPPER_addr[ii] = 0;
        }

    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Calculate the mirror BAR data for each MC based on group info
///        of port0/1
///
/// @param[in]  i_mcTarget     MCC target
/// @param[in]  i_portInfo     The port group info
/// @param[in]  io_mcBarData   MC BAR data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
fapi2::ReturnCode getMirrorBarData(const fapi2::Target<fapi2::TARGET_TYPE_MCC>& i_mcTarget,
                                   const mcPortGroupInfo_t& i_portInfo,
                                   mcBarData_t& io_mcBarData)
{
    FAPI_DBG("Entering");

    // ---------------------------------------------------
    // Build MC register values for mirror groups
    // ---------------------------------------------------

    // Set MCFGPM_VALID
    if (i_portInfo.groupSize > 0)
    {
        io_mcBarData.MCFGPM_valid = true;

        // ----------------------------------------------------
        // Determine data for MCFGPM register
        // ----------------------------------------------------

        // MCFGPM Group size
        FAPI_TRY(getGroupSizeEncodedValue(i_mcTarget, i_portInfo.groupSize,
                                          io_mcBarData.MCFGPM_group_size),
                 "getGroupSizeEncodedValue() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Group base address
        io_mcBarData.MCFGPM_groupBaseAddr = i_portInfo.groupBaseAddr;

        // ----------------------------------------------------
        // Determine data for MCFGPMA registers
        // ----------------------------------------------------
        // Alternate Memory MCFGPMA
        for (uint8_t ii = 0; ii < NUM_OF_ALT_MEM_REGIONS; ii++)
        {
            if ( i_portInfo.altMemValid[ii] )
            {
                io_mcBarData.MCFGPMA_HOLE_valid[ii] = 1;
                io_mcBarData.MCFGPMA_HOLE_LOWER_addr[ii] =
                    i_portInfo.altBaseAddr[ii];
                io_mcBarData.MCFGPMA_HOLE_UPPER_addr[ii] =
                    i_portInfo.altBaseAddr[ii] + i_portInfo.altMemSize[ii];
            }
            else
            {
                io_mcBarData.MCFGPMA_HOLE_valid[ii] = 0;
                io_mcBarData.MCFGPMA_HOLE_LOWER_addr[ii] = 0;
                io_mcBarData.MCFGPMA_HOLE_UPPER_addr[ii] = 0;
            }

        }
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Display the Memory controller BAR data resulted from the BAR
///        data calculations.
///
/// @param[in]  i_portInfo      Port data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
void displayMCPortInfoData(const mcPortGroupInfo_t i_portInfo)
{
    if (i_portInfo.numPortsInGroup > 0)
    {
        FAPI_INF("        myGroup %u", i_portInfo.myGroup);
        FAPI_INF("        numPortsInGroup %u", i_portInfo.numPortsInGroup);
        FAPI_INF("        groupSize %u", i_portInfo.groupSize);
        FAPI_INF("        groupBaseAddr 0x%.16llX", i_portInfo.groupBaseAddr);
        FAPI_INF("        channelId %u", i_portInfo.channelId);

        for (uint8_t jj = 0; jj < NUM_OF_ALT_MEM_REGIONS; jj++)
        {
            FAPI_INF("        altMemValid[%u] %u", jj, i_portInfo.altMemValid[jj]);
            FAPI_INF("        altMemSize[%u]  %u", jj, i_portInfo.altMemSize[jj]);
            FAPI_INF("        altBaseAddr[%u] 0x%.16llX", jj, i_portInfo.altBaseAddr[jj]);
        }
    }
    else
    {
        FAPI_INF("        Not configured");
    }

    return;
}

///
/// @brief Display the Memory controller BAR data resulted from the BAR
///        data calculations.
///
/// @param[in]  i_portInfo      Port data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
void displayMCPortInfoData(const mcPortGroupInfo_t i_portInfo[])
{
    for (uint8_t ii = 0; ii < MAX_MC_PORTS_PER_MCS; ii++)
    {
        FAPI_INF("    Port %u:", ii);

        displayMCPortInfoData(i_portInfo[ii]);
    }

    return;
}

///
/// @brief Display the Memory controller BAR data resulted from the BAR
///        data calculations.
///
/// @param[in]  i_mcPosition    MC (MCS/MI) position
/// @param[in]  i_mcBarData     BAR data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
void displayMCBarData(const uint8_t i_mcPosition,
                      const mcBarData_t i_mcBarData)
{
    FAPI_INF("    MC pos: %u - BAR data:", i_mcPosition);
    FAPI_INF("        MCFGP_valid %u", i_mcBarData.MCFGP_valid);
    FAPI_INF("        MCFGP_chan_per_group %u", i_mcBarData.MCFGP_chan_per_group);
    FAPI_INF("        MCFGP_chan0_group_member_id %u", i_mcBarData.MCFGP_chan0_group_member_id);
    FAPI_INF("        MCFGP_chan1_group_member_id %u", i_mcBarData.MCFGP_chan1_group_member_id);
    FAPI_INF("        MCFGP_group_size %u", i_mcBarData.MCFGP_group_size);
    FAPI_INF("        MCFGP_groupBaseAddr 0x%.16llX", i_mcBarData.MCFGP_groupBaseAddr);
    FAPI_INF("        MCFGPM_valid %u", i_mcBarData.MCFGPM_valid);
    FAPI_INF("        MCFGPM_group_size %u", i_mcBarData.MCFGPM_group_size);
    FAPI_INF("        MCFGPM_groupBaseAddr 0x%.16llX", i_mcBarData.MCFGPM_groupBaseAddr);
    FAPI_INF("        MCFGPA_SMF_valid %u", i_mcBarData.MCFGPA_SMF_valid);
    FAPI_INF("        MCFGPA_SMF_LOWER_addr 0x%.16llX", i_mcBarData.MCFGPA_SMF_LOWER_addr);
    FAPI_INF("        MCFGPA_SMF_UPPER_addr 0x%.16llX", i_mcBarData.MCFGPA_SMF_UPPER_addr);
    FAPI_INF("        MCFGPMA_SMF_valid %u", i_mcBarData.MCFGPMA_SMF_valid);
    FAPI_INF("        MCFGPMA_SMF_LOWER_addr 0x%.16llX", i_mcBarData.MCFGPMA_SMF_LOWER_addr);
    FAPI_INF("        MCFGPMA_SMF_UPPER_addr 0x%.16llX", i_mcBarData.MCFGPMA_SMF_UPPER_addr);

    for (uint8_t jj = 0; jj < NUM_OF_ALT_MEM_REGIONS; jj++)
    {
        FAPI_INF("        MCFGPA_HOLE_valid[%u]      %u", jj, i_mcBarData.MCFGPA_HOLE_valid[jj]);
        FAPI_INF("        MCFGPA_HOLE_LOWER_addr[%u] %u", jj, i_mcBarData.MCFGPA_HOLE_LOWER_addr[jj]);
        FAPI_INF("        MCFGPA_HOLE_UPPER_addr[%u] %u", jj, i_mcBarData.MCFGPA_HOLE_UPPER_addr[jj]);

        FAPI_INF("        MCFGPMA_HOLE_valid[%u]      %u", jj, i_mcBarData.MCFGPMA_HOLE_valid[jj]);
        FAPI_INF("        MCFGPMA_HOLE_LOWER_addr[%u] %u", jj, i_mcBarData.MCFGPMA_HOLE_LOWER_addr[jj]);
        FAPI_INF("        MCFGPMA_HOLE_UPPER_addr[%u] %u", jj, i_mcBarData.MCFGPMA_HOLE_UPPER_addr[jj]);

    }

    return;
}

///
/// @brief Load the mcPortGroupInfo_t data for the input MC based on
///        MC position, input group data, and mirror/non-mirror setting.
///
/// @param[in]  i_nonMirror     Type of group data:
///                              true = non-mirror; false = mirrored
/// @param[in]  i_mcPos         MC position (MCS/MI)
/// @param[in]  i_groupData     Array of Group data info
/// @param[out] o_portInfo      Output mcPortGroupInfo_t
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
void getPortData(const bool i_nonMirror,
                 const uint8_t i_mcPos,
                 const uint32_t i_groupData[DATA_GROUPS][DATA_ELEMENTS],
                 mcPortGroupInfo_t o_portInfo[MAX_MC_PORTS_PER_MCS])
{
    FAPI_DBG("Entering");

    // Non-mirrored groups: 0->7
    // Mirrored groups: 8->11
    uint8_t l_startGroup = 0;
    uint8_t l_endGroup =  (DATA_GROUPS / 2);

    if (i_nonMirror == false)
    {
        l_startGroup = MIRR_OFFSET;
        l_endGroup = (MIRR_OFFSET + NUM_MIRROR_REGIONS);
    }

    // Loop thru specified groups
    for (uint8_t l_group = l_startGroup; l_group < l_endGroup; l_group++)
    {
        // Skip empty groups
        if (i_groupData[l_group][GROUP_SIZE] == 0)
        {
            FAPI_DBG("Skipping zero group %d (nonMirror: %d)", l_group, i_nonMirror);
            continue;
        }

        // Loop thru the ports (MCA/DMI) and determine if they belong
        // to this MC (MCS/MI)
        for (uint8_t l_memberIdx = 0;
             l_memberIdx < i_groupData[l_group][PORTS_IN_GROUP]; l_memberIdx++)
        {
            uint8_t l_mcPos = getMCPosition(i_groupData[l_group][MEMBER_IDX(0) + l_memberIdx]);

            // If the PORT_ID belongs to this MC
            if (l_mcPos == i_mcPos)
            {
                // Get the port number with respect to this MC (0 or 1)
                uint8_t l_mcPortNum = getMCPortNum(i_groupData[l_group][MEMBER_IDX(0) + l_memberIdx]);

                // Set the port group info for this port
                o_portInfo[l_mcPortNum].myGroup = l_group;
                o_portInfo[l_mcPortNum].numPortsInGroup = i_groupData[l_group][PORTS_IN_GROUP];
                o_portInfo[l_mcPortNum].groupSize = i_groupData[l_group][GROUP_SIZE];
                o_portInfo[l_mcPortNum].groupBaseAddr = i_groupData[l_group][BASE_ADDR];
                o_portInfo[l_mcPortNum].channelId = l_memberIdx;

                // ALT memory regions
                for (uint8_t ii = 0; ii < NUM_OF_ALT_MEM_REGIONS; ii++)
                {
                    if (i_groupData[l_group][ALT_VALID(ii)])
                    {
                        o_portInfo[l_mcPortNum].altMemValid[ii] = 1;
                        o_portInfo[l_mcPortNum].altMemSize[ii] = i_groupData[l_group][ALT_SIZE(ii)];
                        o_portInfo[l_mcPortNum].altBaseAddr[ii] = i_groupData[l_group][ALT_BASE_ADDR(ii)];
                    }
                }

                // SMF memory regions
                if (i_groupData[l_group][SMF_VALID])
                {
                    o_portInfo[l_mcPortNum].smfMemValid = 1;
                    o_portInfo[l_mcPortNum].smfMemSize = i_groupData[l_group][SMF_SIZE];
                    o_portInfo[l_mcPortNum].smfBaseAddr = i_groupData[l_group][SMF_BASE_ADDR];
                }
            }

        } // Port loop

    } // Group loop

    // Set channel ID for certain scenario
    if ( (o_portInfo[0].numPortsInGroup > 0) ||
         (o_portInfo[1].numPortsInGroup > 0) )
    {
        // If odd port (port1) has memory and even port (port0) is empty,
        // and odd port is in a group of 2 (obviously with a cross-MCS port),
        // then program channel id for port0 (because HW looks for id at this
        // port), zero out port1's group id
        if ( (o_portInfo[1].numPortsInGroup == 2) &&
             (o_portInfo[0].numPortsInGroup == 0) )
        {
            o_portInfo[0].channelId = o_portInfo[1].channelId;
            o_portInfo[1].channelId = 0;
        }
    }

    // Display MC port info data
    FAPI_INF("getPortData from %s group - Results for MCS/MI pos %d",
             i_nonMirror ? "NON-MIRROR" : "MIRROR", i_mcPos);
    displayMCPortInfoData(o_portInfo);

    FAPI_DBG("Exit");
    return;
}

///
/// @brief Load the mcPortGroupInfo_t data for the input MC based on
///        MC position, input group data, and mirror/non-mirror setting.
///
/// @param[in]  i_nonMirror     Type of group data:
///                              true = non-mirror; false = mirrored
/// @param[in]  i_mcPos         MCC position
/// @param[in]  i_groupData     Array of Group data info
/// @param[out] o_portInfo      Output mcPortGroupInfo_t
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
void getPortData(const bool i_nonMirror,
                 const uint8_t i_mccPos,
                 const uint32_t i_groupData[DATA_GROUPS][DATA_ELEMENTS],
                 mcPortGroupInfo_t& o_portInfo)
{
    FAPI_DBG("Entering");

    // Non-mirrored groups: 0->7
    // Mirrored groups: 8->11
    uint8_t l_startGroup = 0;
    uint8_t l_endGroup =  (DATA_GROUPS / 2);

    if (i_nonMirror == false)
    {
        l_startGroup = MIRR_OFFSET;
        l_endGroup = (MIRR_OFFSET + NUM_MIRROR_REGIONS);
    }

    // Loop thru specified groups
    for (uint8_t l_group = l_startGroup; l_group < l_endGroup; l_group++)
    {
        // Skip empty groups
        if (i_groupData[l_group][GROUP_SIZE] == 0)
        {
            continue;
        }

        // Loop thru the ports (MCA/DMI) and determine if they belong
        // to this MC (MCS/MI)
        for (uint8_t l_memberIdx = 0;
             l_memberIdx < i_groupData[l_group][PORTS_IN_GROUP]; l_memberIdx++)
        {
            uint8_t l_mccPos = i_groupData[l_group][MEMBER_IDX(0) + l_memberIdx];

            // If the PORT_ID belongs to this MC
            if (l_mccPos == i_mccPos)
            {
                // Set the port group info for this port
                o_portInfo.myGroup = l_group;
                o_portInfo.numPortsInGroup = i_groupData[l_group][PORTS_IN_GROUP];
                o_portInfo.groupSize = i_groupData[l_group][GROUP_SIZE];
                o_portInfo.groupBaseAddr = i_groupData[l_group][BASE_ADDR];
                o_portInfo.channelId = l_memberIdx;

                // ALT memory regions
                for (uint8_t ii = 0; ii < NUM_OF_ALT_MEM_REGIONS; ii++)
                {
                    if (i_groupData[l_group][ALT_VALID(ii)])
                    {
                        o_portInfo.altMemValid[ii] = 1;
                        o_portInfo.altMemSize[ii] = i_groupData[l_group][ALT_SIZE(ii)];
                        o_portInfo.altBaseAddr[ii] = i_groupData[l_group][ALT_BASE_ADDR(ii)];
                    }
                }

                // SMF memory regions
                if (i_groupData[l_group][SMF_VALID])
                {
                    o_portInfo.smfMemValid = 1;
                    o_portInfo.smfMemSize = i_groupData[l_group][SMF_SIZE];
                    o_portInfo.smfBaseAddr = i_groupData[l_group][SMF_BASE_ADDR];
                }
            }

        } // Port loop

    } // Group loop

    // Display MC port info data
    FAPI_INF("getPortData from %s group - Results for MCC pos %d",
             i_nonMirror ? "NON-MIRROR" : "MIRROR", i_mccPos);
    displayMCPortInfoData(o_portInfo);

    FAPI_DBG("Exit");
    return;
}

///
/// @brief Use Group data obtained from p9_mss_eff_grouping to build
///        data to be programmed into each Memory controller target (MCS
///        or MI).
///
/// @param[in]  i_mcTargets     Vector of reference of MC targets (MCS/MI)
/// @param[in]  i_groupData     Array of Group data info
/// @param[out] o_mcDataPair    Output data pair MCS<->Data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode buildMCBarData(
    const std::vector< fapi2::Target<T> >& i_mcTargets,
    const uint32_t i_groupData[DATA_GROUPS][DATA_ELEMENTS],
    std::vector<std::pair<fapi2::Target<T>, mcBarData_t>>& o_mcBarDataPair)
{
    FAPI_DBG("Entering");

    char l_targetStr[fapi2::MAX_ECMD_STRING_LEN];
    const fapi2::Target<fapi2::TARGET_TYPE_SYSTEM> FAPI_SYSTEM;
    fapi2::ATTR_MRW_HW_MIRRORING_ENABLE_Type l_mirror_ctl;

    // Get mirror policy
    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_MRW_HW_MIRRORING_ENABLE,
                           FAPI_SYSTEM, l_mirror_ctl),
             "Error getting ATTR_MRW_HW_MIRRORING_ENABLE, "
             "l_rc 0x%.8X", uint64_t(fapi2::current_err));

    for (auto l_mc : i_mcTargets)
    {
        mcBarData_t l_mcBarData;
        mcPortGroupInfo_t l_portInfo[MAX_MC_PORTS_PER_MCS];

        // Get this MC unit position
        uint8_t l_unitPos = 0;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, l_mc, l_unitPos),
                 "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        fapi2::toString(l_mc, l_targetStr, sizeof(l_targetStr));
        FAPI_INF("Build BAR data for MC target: %s", l_targetStr);

        // -----------------------------------------------
        // Build MC register values for non-mirror groups
        // -----------------------------------------------

        // Get port data from non-mirrored groups (true = non-mirrored)
        getPortData(true, l_unitPos, i_groupData, l_portInfo);

        // If one of MC port is configured in a group, proceed with
        // getting BAR data
        if ( (l_portInfo[0].numPortsInGroup > 0) ||
             (l_portInfo[1].numPortsInGroup > 0) )
        {
            // ---- Build MCFGP/MCFGM data based on port group info ----
            FAPI_TRY(getNonMirrorBarData(l_mc, l_portInfo, l_mcBarData),
                     "getNonMirrorBarData() returns error, l_rc 0x%.8X",
                     uint64_t(fapi2::current_err));

            // ---------------------------------------------------------------
            // Set MC register values for mirror groups
            //   - Nimbus:  No mirror
            //   - Cumulus: If ATTR_MRW_HW_MIRRORING_ENABLE != false
            // ---------------------------------------------------------------
            if (l_mirror_ctl != fapi2::ENUM_ATTR_MRW_HW_MIRRORING_ENABLE_FALSE)
            {
                FAPI_INF("ATTR_MRW_HW_MIRRORING_ENABLE is enabled: checking mirrored groups");
                mcPortGroupInfo_t l_portInfoMirrored[MAX_MC_PORTS_PER_MCS];
                // Get port data from mirrored groups (false = mirrored)
                getPortData(false, l_unitPos, i_groupData, l_portInfoMirrored);

                // If at least 2MC ports/group, get the mirror BAR data
                if ( (l_portInfoMirrored[0].numPortsInGroup >= 2) &&
                     (l_portInfoMirrored[1].numPortsInGroup >= 2) )
                {
                    // ---- Build MCFGM data based on port group info ----
                    FAPI_TRY(getMirrorBarData(l_mc, l_portInfoMirrored, l_mcBarData),
                             "getMirrorBarData() returns error, l_rc 0x%.8X",
                             uint64_t(fapi2::current_err));
                }
            }

            // Add to output pair
            o_mcBarDataPair.push_back(std::make_pair(l_mc, l_mcBarData));

            // Display data
            displayMCBarData(l_unitPos, l_mcBarData);
        }
        else
        {
            FAPI_INF("MC pos %u is not configured in a memory group.", l_unitPos);
        }

    } // MC loop

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Use Group data obtained from p9_mss_eff_grouping to build
///        data to be programmed into each Memory controller target MCC
///        for OMI based memory
///
/// @param[in]  i_mcTargets     Vector of reference of MCC
/// @param[in]  i_groupData     Array of Group data info
/// @param[out] o_mcDataPair    Output data pair MCC<->Data
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<>
fapi2::ReturnCode buildMCBarData(
    const std::vector< fapi2::Target<fapi2::TARGET_TYPE_MCC> >& i_mccTargets,
    const uint32_t i_groupData[DATA_GROUPS][DATA_ELEMENTS],
    std::vector<std::pair<fapi2::Target<fapi2::TARGET_TYPE_MCC>, mcBarData_t>>& o_mcBarDataPair)
{
    FAPI_DBG("Entering");

    char l_targetStr[fapi2::MAX_ECMD_STRING_LEN];
    const fapi2::Target<fapi2::TARGET_TYPE_SYSTEM> FAPI_SYSTEM;
    fapi2::ATTR_MRW_HW_MIRRORING_ENABLE_Type l_mirror_ctl;

    // Get mirror policy
    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_MRW_HW_MIRRORING_ENABLE,
                           FAPI_SYSTEM, l_mirror_ctl),
             "Error getting ATTR_MRW_HW_MIRRORING_ENABLE, "
             "l_rc 0x%.8X", uint64_t(fapi2::current_err));

    for (auto l_mcc : i_mccTargets)
    {
        mcBarData_t l_mcBarData;
        mcPortGroupInfo_t l_portInfo;

        // Get this MC unit position
        uint8_t l_unitPos = 0;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, l_mcc, l_unitPos),
                 "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        fapi2::toString(l_mcc, l_targetStr, sizeof(l_targetStr));
        FAPI_INF("Build BAR data for MC target: %s", l_targetStr);

        // -----------------------------------------------
        // Build MC register values for non-mirror groups
        // -----------------------------------------------

        // Get port data from non-mirrored groups (true = non-mirrored)
        getPortData(true, l_unitPos, i_groupData, l_portInfo);

        // If one of MC port is configured in a group, proceed with
        // getting BAR data
        if ( (l_portInfo.numPortsInGroup > 0) )
        {
            // ---- Build MCFGP/MCFGM data based on port group info ----
            FAPI_TRY(getNonMirrorBarData(l_mcc, l_portInfo, l_mcBarData),
                     "getNonMirrorBarData() returns error, l_rc 0x%.8X",
                     uint64_t(fapi2::current_err));

            // ---------------------------------------------------------------
            // Set MC register values for mirror groups
            //   - Nimbus:  No mirror
            //   - Cumulus: If ATTR_MRW_HW_MIRRORING_ENABLE != false
            // ---------------------------------------------------------------
            if (l_mirror_ctl != fapi2::ENUM_ATTR_MRW_HW_MIRRORING_ENABLE_FALSE)
            {
                FAPI_INF("ATTR_MRW_HW_MIRRORING_ENABLE is enabled: checking mirrored groups");
                mcPortGroupInfo_t l_portInfoMirrored;
                // Get port data from mirrored groups (false = mirrored)
                getPortData(false, l_unitPos, i_groupData, l_portInfoMirrored);

                // ---- Build MCFGM data based on port group info ----
                FAPI_TRY(getMirrorBarData(l_mcc, l_portInfoMirrored, l_mcBarData),
                         "getMirrorBarData() returns error, l_rc 0x%.8X",
                         uint64_t(fapi2::current_err));
            }

            // Add to output pair
            o_mcBarDataPair.push_back(std::make_pair(l_mcc, l_mcBarData));

            // Display data
            displayMCBarData(l_unitPos, l_mcBarData);
        }
        else
        {
            FAPI_INF("MC pos %u is not configured in a memory group.", l_unitPos);
        }

    } // MC loop

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Set MCFGPA MEMORY HOLE UPPER_ADDRESS_AT_END_OF_RANGE bit
///
/// @param[in]      i_target        MC target (MCS or MI)
/// @param[in/out]  io_data         Data buffer
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
void setUpperAddrEndOfRangeBit(const fapi2::Target<T>& i_target,
                               fapi2::buffer<uint64_t>& io_scomData);

/// MC = MCS (Nimbus)
template<>
void setUpperAddrEndOfRangeBit(const fapi2::Target<fapi2::TARGET_TYPE_MCS>& i_target,
                               fapi2::buffer<uint64_t>& io_scomData)
{
    // This bit is not used in Nimbus
    return;
}

/// MC = MI (Cumulus)
template<>
void setUpperAddrEndOfRangeBit(const fapi2::Target<fapi2::TARGET_TYPE_MI>& i_target,
                               fapi2::buffer<uint64_t>& io_scomData)
{
    io_scomData.setBit<MCS_MCFGPA_RESERVED_1>();
    return;
}

///
/// @brief Write BAR data to a memory controller
///
/// @param[in] i_mcBarDataPair Target pair <target, data>
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode writeMCBarData(
    const std::vector<std::pair<fapi2::Target<T>, mcBarData_t>>& i_mcBarDataPair)
{
    FAPI_DBG("Entering");

    fapi2::buffer<uint64_t> l_scomData(0);
    fapi2::ATTR_MSS_INTERLEAVE_GRANULARITY_Type l_interleave_granule_size;
    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_MSS_INTERLEAVE_GRANULARITY,
                           fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>(),
                           l_interleave_granule_size),
             "Error getting ATTR_MSS_INTERLEAVE_GRANULARITY, l_rc 0x%.8X",
             uint64_t(fapi2::current_err));

    for (auto l_pair : i_mcBarDataPair)
    {
        fapi2::Target<T> l_target = l_pair.first;
        mcBarData_t l_data = l_pair.second;

        char l_targetStr[fapi2::MAX_ECMD_STRING_LEN];
        fapi2::toString(l_target, l_targetStr, sizeof(l_targetStr));
        FAPI_INF("Program MC target: %s", l_targetStr);

        // 1. ---- Set MCFGP reg -----
        l_scomData = 0;

        // MCFGP valid (bit 0)
        if (l_data.MCFGP_valid == true)
        {
            l_scomData.setBit<MCS_MCFGP_VALID>();

            // Group size (bits 13:23)
            l_scomData.insertFromRight<MCS_MCFGP_GROUP_SIZE,
                                       MCS_MCFGP_GROUP_SIZE_LEN>(
                                           l_data.MCFGP_group_size);


            // Group base address (bits 24:47) 0b000000000000000000000001 = 4GB
            // 000000001 (base addr of 4GB)
            // 000000010 (base addr of 8GB)
            // 000000100 (base addr of 16GB)
            // 000001000 (base addr of 32GB)
            // 000010000 (base addr of 64GB)
            // 000100000 (base addr of 128GB)
            // 001000000 (base addr of 256GB)
            l_scomData.insertFromRight<MCS_MCFGP_GROUP_BASE_ADDRESS,
                                       MCS_MCFGP_GROUP_BASE_ADDRESS_LEN>(
                                           (l_data.MCFGP_groupBaseAddr >> 2));


            // configure interleave granularity if 2/4/8 MC per group only
            if (    (l_data.MCFGP_chan_per_group == 0b0100) || // 2 MC/group
                    (l_data.MCFGP_chan_per_group == 0b0101) ||
                    (l_data.MCFGP_chan_per_group == 0b0110) || // 4 MC/group
                    (l_data.MCFGP_chan_per_group == 0b1000)    // 8 MC/group
               )
            {
                fapi2::buffer<uint64_t> l_mcmode0_scom_data;
                FAPI_TRY(fapi2::getScom(l_target, MCS_MCMODE0, l_mcmode0_scom_data),
                         "Error reading from MCS_MCMODE0 reg");
                l_mcmode0_scom_data.insertFromRight<MCS_MCMODE0_GROUP_INTERLEAVE_GRANULARITY,
                                                    MCS_MCMODE0_GROUP_INTERLEAVE_GRANULARITY_LEN>(l_interleave_granule_size);
                FAPI_TRY(fapi2::putScom(l_target, MCS_MCMODE0, l_mcmode0_scom_data),
                         "Error writing to MCS_MCMODE0 reg");
            }
        }

        // Channel per group (bits 1:4)
        l_scomData.insertFromRight<MCS_MCFGP_MC_CHANNELS_PER_GROUP,
                                   MCS_MCFGP_MC_CHANNELS_PER_GROUP_LEN>(
                                       l_data.MCFGP_chan_per_group);

        // Channel 0 group id (bits 5:7)
        l_scomData.insertFromRight<MCS_MCFGP_CHANNEL_0_GROUP_MEMBER_IDENTIFICATION,
                                   MCS_MCFGP_CHANNEL_0_GROUP_MEMBER_IDENTIFICATION_LEN>(
                                       l_data.MCFGP_chan0_group_member_id);

        // Channel 1 group id (bits 8:10)
        l_scomData.insertFromRight<MCS_MCFGP_CHANNEL_1_GROUP_MEMBER_IDENTIFICATION,
                                   MCS_MCFGP_CHANNEL_1_GROUP_MEMBER_IDENTIFICATION_LEN>(
                                       l_data.MCFGP_chan1_group_member_id);

        // Write to reg
        FAPI_INF("Write MCFGP reg 0x%.16llX, Value 0x%.16llX",
                 MCS_MCFGP, l_scomData);
        FAPI_TRY(fapi2::putScom(l_target, MCS_MCFGP, l_scomData),
                 "Error writing to MCS_MCFGP reg");

        // 2. ---- Set MCFGPM reg -----
        l_scomData = 0;

        if (l_data.MCFGPM_valid == true)
        {
            // MCFGP valid (bit 0)
            l_scomData.setBit<MCS_MCFGPM_VALID>();

            // Group size (bits 13:23)
            l_scomData.insertFromRight<MCS_MCFGPM_GROUP_SIZE,
                                       MCS_MCFGPM_GROUP_SIZE_LEN>(
                                           l_data.MCFGPM_group_size);

            // Group base address (bits 24:47), 0b000000000000000000000001 = 4GB
            // 000000001 (base addr of 4GB)
            // 000000010 (base addr of 8GB)
            // 000000100 (base addr of 16GB)
            // 000001000 (base addr of 32GB)
            // 000010000 (base addr of 64GB)
            // 000100000 (base addr of 128GB)
            // 001000000 (base addr of 256GB)
            l_scomData.insertFromRight<MCS_MCFGPM_GROUP_BASE_ADDRESS,
                                       MCS_MCFGPM_GROUP_BASE_ADDRESS_LEN>(
                                           (l_data.MCFGPM_groupBaseAddr >> 2));

        }

        // Write to reg
        FAPI_INF("Write MCFGPM reg 0x%.16llX, Value 0x%.16llX",
                 MCS_MCFGPM, l_scomData);
        FAPI_TRY(fapi2::putScom(l_target, MCS_MCFGPM, l_scomData),
                 "Error writing to MCS_MCFGPM reg");

        // 3. ---- Set MCFGPA reg -----
        l_scomData = 0;

        // Assert if both HOLE1 and SMF are valid, settings will overlap
        FAPI_ASSERT((l_data.MCFGPA_HOLE_valid[1] && l_data.MCFGPA_SMF_valid) == 0,
                    fapi2::MSS_SETUP_BARS_HOLE1_SMF_CONFLICT()
                    .set_TARGET(l_target)
                    .set_HOLE1_VALID(l_data.MCFGPA_HOLE_valid[1])
                    .set_SMF_VALID(l_data.MCFGPA_SMF_valid),
                    "Error: MCFGPA HOLE1 and SMF are both valid, settings will overlap");

        // Hole 0
        if (l_data.MCFGPA_HOLE_valid[0] == true)
        {
            // MCFGPA HOLE0 valid (bit 0)
            l_scomData.setBit<MCS_MCFGPA_HOLE0_VALID>();

            // MCFGPA_HOLE0_UPPER_ADDRESS_AT_END_OF_RANGE
            setUpperAddrEndOfRangeBit(l_target, l_scomData);

            // Hole 0 lower addr
            // 0b0000000001 = 4GB
            l_scomData.insertFromRight<MCS_MCFGPA_HOLE0_LOWER_ADDRESS,
                                       MCS_MCFGPA_HOLE0_LOWER_ADDRESS_LEN>(
                                           (l_data.MCFGPA_HOLE_LOWER_addr[0] >> 2));
            // Hole 0 upper addr
            // 0b0000000001 = 4GB
            l_scomData.insertFromRight<MCS_MCFGPA_HOLE0_UPPER_ADDRESS,
                                       MCS_MCFGPA_HOLE0_UPPER_ADDRESS_LEN>(
                                           (l_data.MCFGPA_HOLE_UPPER_addr[0] >> 2));
        }

        // Hole 1
        if (l_data.MCFGPA_HOLE_valid[1] == true)
        {
            // MCFGPA HOLE1 valid (bit 0)
            l_scomData.setBit<MCS_MCFGPA_HOLE1_VALID>();

            // MCFGPA_HOLE1_UPPER_ADDRESS_AT_END_OF_RANGE
            setUpperAddrEndOfRangeBit(l_target, l_scomData);

            // Hole 1 lower addr
            // 0b0000000001 = 4GB
            l_scomData.insertFromRight<MCS_MCFGPA_HOLE1_LOWER_ADDRESS,
                                       MCS_MCFGPA_HOLE1_LOWER_ADDRESS_LEN>(
                                           (l_data.MCFGPA_HOLE_LOWER_addr[1] >> 2));
            // Hole 1 upper addr
            // 0b0000000001 = 4GB
            l_scomData.insertFromRight<MCS_MCFGPA_HOLE1_UPPER_ADDRESS,
                                       MCS_MCFGPA_HOLE1_UPPER_ADDRESS_LEN>(
                                           (l_data.MCFGPA_HOLE_UPPER_addr[1] >> 2));
        }

        // SMF
        if (l_data.MCFGPA_SMF_valid == true)
        {
            // MCFGPA SMF valid (bit 0)
            l_scomData.setBit<P9N2_MCS_MCFGPA_SMF_VALID>();

            // MCFGPA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE
            l_scomData.setBit<P9N2_MCS_MCFGPA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE>();

            // SMF lower addr
            l_scomData.insertFromRight<P9N2_MCS_MCFGPA_SMF_LOWER_ADDRESS,
                                       P9N2_MCS_MCFGPA_SMF_LOWER_ADDRESS_LEN>(
                                           (l_data.MCFGPA_SMF_LOWER_addr));
            // SMF upper addr
            l_scomData.insertFromRight<P9N2_MCS_MCFGPA_SMF_UPPER_ADDRESS,
                                       P9N2_MCS_MCFGPA_SMF_UPPER_ADDRESS_LEN>(
                                           (l_data.MCFGPA_SMF_UPPER_addr));
        }

        // Write to reg
        FAPI_INF("Write MCFGPA reg 0x%.16llX, Value 0x%.16llX",
                 MCS_MCFGPA, l_scomData);
        FAPI_TRY(fapi2::putScom(l_target, MCS_MCFGPA, l_scomData),
                 "Error writing to MCS_MCFGPA reg");

        // 4. ---- Set MCFGPMA reg -----
        l_scomData = 0;

        // Assert if both HOLE1 and SMF are valid, settings will overlap
        FAPI_ASSERT((l_data.MCFGPMA_HOLE_valid[1] && l_data.MCFGPMA_SMF_valid) == 0,
                    fapi2::MSS_SETUP_BARS_HOLE1_SMF_CONFLICT()
                    .set_TARGET(l_target)
                    .set_HOLE1_VALID(l_data.MCFGPMA_HOLE_valid[1])
                    .set_SMF_VALID(l_data.MCFGPMA_SMF_valid),
                    "Error: MCFGPMA HOLE1 and SMF are both valid, settings will overlap");

        // Hole 0
        if (l_data.MCFGPMA_HOLE_valid[0] == true)
        {
            // MCFGPMA HOLE0 valid (bit 0)
            l_scomData.setBit<MCS_MCFGPMA_HOLE0_VALID>();

            // MCFGPA_HOLE0_UPPER_ADDRESS_AT_END_OF_RANGE
            setUpperAddrEndOfRangeBit(l_target, l_scomData);

            // Hole 0 lower addr
            // 0b0000000001 = 4GB
            l_scomData.insertFromRight<MCS_MCFGPMA_HOLE0_LOWER_ADDRESS,
                                       MCS_MCFGPMA_HOLE0_LOWER_ADDRESS_LEN>(
                                           ( l_data.MCFGPMA_HOLE_LOWER_addr[0] >> 2));
            // Hole 0 upper addr
            // 0b0000000001 = 4GB
            l_scomData.insertFromRight<MCS_MCFGPMA_HOLE0_UPPER_ADDRESS,
                                       MCS_MCFGPMA_HOLE0_UPPER_ADDRESS_LEN>(
                                           (l_data.MCFGPMA_HOLE_UPPER_addr[0] >> 2));
        }

        // Hole 1
        if (l_data.MCFGPMA_HOLE_valid[1] == true)
        {
            // MCFGPMA HOLE1 valid (bit 0)
            // 0b0000000001 = 4GB
            l_scomData.setBit<MCS_MCFGPMA_HOLE1_VALID>();

            // MCFGPA_HOLE1_UPPER_ADDRESS_AT_END_OF_RANGE
            setUpperAddrEndOfRangeBit(l_target, l_scomData);

            // Hole 1 lower addr
            l_scomData.insertFromRight<MCS_MCFGPMA_HOLE1_LOWER_ADDRESS,
                                       MCS_MCFGPMA_HOLE1_LOWER_ADDRESS_LEN>(
                                           (l_data.MCFGPMA_HOLE_LOWER_addr[1] >> 2));
            // Hole 1 upper addr
            // 0b0000000001 = 4GB
            l_scomData.insertFromRight<MCS_MCFGPMA_HOLE1_UPPER_ADDRESS,
                                       MCS_MCFGPMA_HOLE1_UPPER_ADDRESS_LEN>(
                                           (l_data.MCFGPMA_HOLE_UPPER_addr[1] >> 2));
        }

        // SMF
        if (l_data.MCFGPMA_SMF_valid == true)
        {
            // MCFGPMA SMF valid (bit 0)
            l_scomData.setBit<P9N2_MCS_MCFGPMA_SMF_VALID>();

            // MCFGPMA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE
            l_scomData.setBit<P9N2_MCS_MCFGPMA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE>();

            // SMF lower addr
            l_scomData.insertFromRight<P9N2_MCS_MCFGPMA_SMF_LOWER_ADDRESS,
                                       P9N2_MCS_MCFGPMA_SMF_LOWER_ADDRESS_LEN>(
                                           (l_data.MCFGPMA_SMF_LOWER_addr));
            // SMF upper addr
            l_scomData.insertFromRight<P9N2_MCS_MCFGPMA_SMF_UPPER_ADDRESS,
                                       P9N2_MCS_MCFGPMA_SMF_UPPER_ADDRESS_LEN>(
                                           (l_data.MCFGPMA_SMF_UPPER_addr));
        }

        // Write to reg
        FAPI_INF("Write MCFGPMA reg 0x%.16llX, Value 0x%.16llX",
                 MCS_MCFGPMA, l_scomData);

        FAPI_TRY(fapi2::putScom(l_target, MCS_MCFGPMA, l_scomData),
                 "Error writing to MCS_MCFGPMA reg");

    } // Data pair loop

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Write BAR data to a memory controller
///
/// @param[in] i_mcBarDataPair Target pair <target, data>
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
fapi2::ReturnCode writeMCCInterleaveGranularity(
    const std::vector<std::pair<fapi2::Target<fapi2::TARGET_TYPE_MCC>, mcBarData_t>>& i_mcBarDataPair)
{
    FAPI_DBG("Entering");

    std::map<fapi2::Target<fapi2::TARGET_TYPE_MI>, bool> l_granule_supported;

    fapi2::ATTR_MSS_INTERLEAVE_GRANULARITY_Type l_interleave_granule_size;
    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_MSS_INTERLEAVE_GRANULARITY,
                           fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>(),
                           l_interleave_granule_size),
             "Error getting ATTR_MSS_INTERLEAVE_GRANULARITY, l_rc 0x%.8X",
             uint64_t(fapi2::current_err));

    for (auto l_pair : i_mcBarDataPair)
    {
        fapi2::Target<fapi2::TARGET_TYPE_MCC> l_target = l_pair.first;
        mcBarData_t l_data = l_pair.second;

        // MCFGP valid (bit 0)
        if (l_data.MCFGP_valid == true)
        {
            fapi2::Target<fapi2::TARGET_TYPE_MI> l_mi_target = l_target.getParent<fapi2::TARGET_TYPE_MI>();
            fapi2::buffer<uint64_t> l_mcmode0_scom_data;
            FAPI_TRY(fapi2::getScom(l_mi_target, P9A_MI_MCMODE0, l_mcmode0_scom_data),
                     "Error reading from MCS_MCMODE0 reg");
            l_mcmode0_scom_data.insertFromRight<P9A_MI_MCMODE0_GROUP_ADDRESS_INTERLEAVE_GRANULARITY,
                                                P9A_MI_MCMODE0_GROUP_ADDRESS_INTERLEAVE_GRANULARITY_LEN>(l_interleave_granule_size);
            FAPI_TRY(fapi2::putScom(l_mi_target, P9A_MI_MCMODE0, l_mcmode0_scom_data),
                     "Error writing to MCS_MCMODE0 reg");
        }
    }

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Translate a bar value in GB to a 32 bit extended value
///
/// @param[in] i_ext_mask The extension mask
/// @param[in] i_bar The bar in GB
/// @param[out] o_extBar is the upper 32 bits of the extended address
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
fapi2::ReturnCode extBar(const uint64_t& i_ext_mask, const uint32_t& i_bar, fapi2::buffer<uint64_t>& o_extBar)
{
    FAPI_DBG("Entering");
    fapi2::buffer<uint64_t> l_norAddr(0);

    FAPI_DBG("i_bar: %016llx", i_bar);
    o_extBar = 0;
    l_norAddr.insert<0, 32>(i_bar >> 2); //i_bar is in GB
    FAPI_DBG("l_norAddr: %016llx", l_norAddr);
    FAPI_TRY(extendBarAddress(i_ext_mask, l_norAddr, o_extBar));
    FAPI_DBG("o_extBar: %016llx", o_extBar);

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Write BAR data to a memory controller
///
/// @param[in] i_mcBarDataPair Target pair <target, data>
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
fapi2::ReturnCode writeMCBarData(
    const std::vector<std::pair<fapi2::Target<fapi2::TARGET_TYPE_MCC>, mcBarData_t>>& i_mcBarDataPair)
{
    FAPI_DBG("Entering");

    uint8_t l_pos;

    fapi2::buffer<uint64_t> l_scomData(0);
    fapi2::buffer<uint64_t> l_scomData_mirror(0);
    fapi2::buffer<uint64_t> l_scomData_mcmode(0);
    fapi2::buffer<uint64_t> l_extAddr(0);
    fapi2::buffer<uint64_t> l_norAddr;
    uint64_t l_ext_mask;

    const fapi2::Target<fapi2::TARGET_TYPE_SYSTEM> FAPI_SYSTEM;
    uint8_t mirror_policy;

    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_MEM_MIRROR_PLACEMENT_POLICY,  FAPI_SYSTEM, mirror_policy),
             "Error reading ATTR_MEM_MIRROR_PLACEMENT_POLICY, l_rc 0x%.8X",
             (uint64_t)fapi2::current_err);

    FAPI_TRY(p9a_get_ext_mask(l_ext_mask));

    FAPI_TRY(writeMCCInterleaveGranularity(i_mcBarDataPair));

    for (auto l_pair : i_mcBarDataPair)
    {
        fapi2::Target<fapi2::TARGET_TYPE_MCC> l_target = l_pair.first;
        mcBarData_t l_data = l_pair.second;

        char l_targetStr[fapi2::MAX_ECMD_STRING_LEN];
        fapi2::toString(l_target, l_targetStr, sizeof(l_targetStr));
        FAPI_INF("Program MC target: %s", l_targetStr);

        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_UNIT_POS, l_target, l_pos),
                 "Error getting ATTR_CHIP_UNIT_POS, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // 1. ---- Set MCFGP reg -----
        l_scomData = 0;

        // MCFGP valid (bit 0)
        if (l_data.MCFGP_valid == true)
        {
            l_scomData.setBit<P9A_MI_MCFGP0_VALID>();

            // Group size (bits 13:23)
            l_scomData.insertFromRight<P9A_MI_MCFGP0_GROUP_SIZE,
                                       P9A_MI_MCFGP0_GROUP_SIZE_LEN>(
                                           l_data.MCFGP_group_size);

            FAPI_TRY(extBar(l_ext_mask, l_data.MCFGP_groupBaseAddr, l_extAddr));
            l_scomData.insert<P9A_MI_MCFGP0_GROUP_BASE_ADDRESS,
                              P9A_MI_MCFGP0_GROUP_BASE_ADDRESS_LEN>(
                                  (l_extAddr));
        }

        // Channel per group (bits 1:4)
        l_scomData.insertFromRight<P9A_MI_MCFGP0_MC_CHANNELS_PER_GROUP,
                                   P9A_MI_MCFGP0_MC_CHANNELS_PER_GROUP_LEN>(
                                       l_data.MCFGP_chan_per_group);

        // Channel 0 group id (bits 5:7)
        l_scomData.insertFromRight<P9A_MI_MCFGP0_GROUP_MEMBER_IDENTIFICATION,
                                   P9A_MI_MCFGP0_GROUP_MEMBER_IDENTIFICATION_LEN>(
                                       l_data.MCFGP_chan0_group_member_id);

        // 2GB cfg and MMIO
        l_scomData.insertFromRight<P9A_MI_MCFGP0_MCFGPR0_CONFIGURATION_GROUP_SIZE,
                                   P9A_MI_MCFGP0_MCFGPR0_CONFIGURATION_GROUP_SIZE_LEN>(mss::exp::ib::EXPLR_IB_BAR_SIZE);
        l_scomData.insertFromRight<P9A_MI_MCFGP0_MCFGPR0_MMIO_GROUP_SIZE,
                                   P9A_MI_MCFGP0_MCFGPR0_MMIO_GROUP_SIZE_LEN>(mss::exp::ib::EXPLR_IB_BAR_SIZE);

        // Write to reg
        if (l_pos % 2 == 0)
        {
            FAPI_INF("Write MCFGP0 reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGP0, l_scomData);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGP0, l_scomData),
                     "Error writing to MCS_MCFGP reg");
        }
        else
        {
            FAPI_INF("Write MCFGP1 reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGP1, l_scomData);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGP1, l_scomData),
                     "Error writing to MCS_MCFGP reg");
        }

        // 2. ---- Set MCFGPM reg -----
        l_scomData = 0;

        if (l_data.MCFGPM_valid == true)
        {
            // MCFGP valid (bit 0)
            l_scomData.setBit<P9A_MI_MCFGPM0_VALID>();

            // Group size (bits 13:23)
            l_scomData.insertFromRight<P9A_MI_MCFGPM0_GROUP_SIZE,
                                       P9A_MI_MCFGPM0_GROUP_SIZE_LEN>(
                                           l_data.MCFGPM_group_size);

            FAPI_TRY(extBar(l_ext_mask, l_data.MCFGPM_groupBaseAddr, l_extAddr));
            l_scomData.insert<P9A_MI_MCFGPM0_GROUP_BASE_ADDRESS,
                              P9A_MI_MCFGPM0_GROUP_BASE_ADDRESS_LEN>(
                                  (l_extAddr));

        }

        if (l_pos % 2 == 0)
        {
            // Write to reg
            FAPI_INF("Write MCFGPM0 reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGPM0, l_scomData);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGPM0, l_scomData),
                     "Error writing to P9A_MI_MCFGPM0 reg");
        }
        else
        {
            // Write to reg
            FAPI_INF("Write MCFGPM1 reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGPM1, l_scomData);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGPM1, l_scomData),
                     "Error writing to P9A_MI_MCFGPM1 reg");
        }

        // 3. ---- Set MCFGPA/MCFGPMA regs -----
        l_scomData = 0;
        l_scomData_mirror = 0;

        // Hole 0
        if (l_data.MCFGPA_HOLE_valid[0] == true)
        {
            if(mirror_policy ==
               fapi2::ENUM_ATTR_MEM_MIRROR_PLACEMENT_POLICY_NORMAL) //Non-mirrored mode, but still set up mirrored equiv addressses
            {
                // Non-mirrored
                // MCFGP0A HOLE valid (bit 0)
                l_scomData.setBit<P9A_MI_MCFGP0A_HOLE_VALID>();

                // Hole lower addr
                // Hole always extends to end of range
                FAPI_DBG("l_data.MCFGPA_HOLE_LOWER_addr[0]: %016llx", l_data.MCFGPA_HOLE_LOWER_addr[0]);
                FAPI_TRY(extBar(l_ext_mask, l_data.MCFGPA_HOLE_LOWER_addr[0], l_extAddr));
                l_scomData.insert<P9A_MI_MCFGP0A_HOLE_LOWER_ADDRESS,
                                  P9A_MI_MCFGP0A_HOLE_LOWER_ADDRESS_LEN>(
                                      (l_extAddr << 9)); //matches 17:31 extendedBarAddress shifts left 8 (17-8) = 9

                // Mirrored Address = Non-mirrored >> 1 since bit 56 is not part of the dsaddr
                // MCFGPM0A HOLE0 valid (bit 0)
                l_scomData_mirror.setBit<P9A_MI_MCFGPM0A_HOLE_VALID>();

                // Hole lower addr
                // Hole always extends to end of range
                FAPI_DBG("l_data.MCFGPA_HOLE_LOWER_addr[0]: %016llx", (l_data.MCFGPA_HOLE_LOWER_addr[0] >> 1));
                FAPI_TRY(extBar(l_ext_mask, l_data.MCFGPA_HOLE_LOWER_addr[0], l_extAddr));
                l_scomData_mirror.insert<P9A_MI_MCFGPM0A_HOLE_LOWER_ADDRESS,
                                         P9A_MI_MCFGPM0A_HOLE_LOWER_ADDRESS_LEN>(
                                             (l_extAddr << 8)); //matches 17:31 extendedBarAddress shifts left 8 (17- (8 + 1)) = 8
            }
            else
            {
                // Mirrored Address
                // MCFGPM0A HOLE0 valid (bit 0)
                l_scomData_mirror.setBit<P9A_MI_MCFGPM0A_HOLE_VALID>();

                // Hole 0 lower addr
                // Hole 0 always extends to end of range
                FAPI_DBG("l_data.MCFGPA_HOLE_LOWER_addr[0]: %016llx", l_data.MCFGPA_HOLE_LOWER_addr[0]);
                FAPI_TRY(extBar(l_ext_mask, l_data.MCFGPA_HOLE_LOWER_addr[0], l_extAddr));
                l_scomData_mirror.insert<P9A_MI_MCFGPM0A_HOLE_LOWER_ADDRESS,
                                         P9A_MI_MCFGPM0A_HOLE_LOWER_ADDRESS_LEN>(
                                             (l_extAddr << 9)); //matches 17:31 extendedBarAddress shifts left 8 (17- 8) = 9

                // Non-mirrored Address = Mirrored Address << 1 since bit 56 is part of the dsaddr
                // MCFGPA HOLE0 valid (bit 0)
                l_scomData.setBit<P9A_MI_MCFGP0A_HOLE_VALID>();

                // Hole 0 lower addr
                // Hole 0 always extends to end of range
                FAPI_DBG("l_data.MCFGPA_HOLE_LOWER_addr[0]: %016llx", (l_data.MCFGPA_HOLE_LOWER_addr[0] << 1));
                FAPI_TRY(extBar(l_ext_mask, l_data.MCFGPA_HOLE_LOWER_addr[0], l_extAddr));
                l_scomData.insert<P9A_MI_MCFGP0A_HOLE_LOWER_ADDRESS,
                                  P9A_MI_MCFGP0A_HOLE_LOWER_ADDRESS_LEN>(
                                      (l_extAddr << 10)); //matches 17:31 extendedBarAddress shifts left 8 (17- (8 - 1)) = 10

            }
        }

        // SMF
        if (l_data.MCFGPA_SMF_valid == true)
        {
            FAPI_DBG("Writing SMF bit into address extension now");
            // Set up Extension Address for SMF
            FAPI_TRY(fapi2::getScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCMODE2, l_scomData_mcmode),
                     "Error reading to P9A_MI_MCMODE2 reg");
            l_scomData_mcmode.setBit<P9A_MI_MCMODE2_CHIP_ADDRESS_EXTENSION_MASK_ENABLE>();
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCMODE2, l_scomData_mcmode),
                     "Error writing to P9A_MI_MCMODE2 reg");

            if(mirror_policy ==
               fapi2::ENUM_ATTR_MEM_MIRROR_PLACEMENT_POLICY_NORMAL) //Non-mirrored mode, but still set up mirrored equiv addressses
            {
                //Non-mirrored
                // MCFGPA SMF valid (bit 0)
                l_scomData.setBit<P9A_MI_MCFGP0A_SMF_VALID>();

                // MCFGPA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE
                l_scomData.setBit<P9A_MI_MCFGP0A_SMF_EXTEND_TO_END_OF_RANGE>();

                // SMF lower addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_LOWER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData.insert<P9A_MI_MCFGP0A_SMF_LOWER_ADDRESS,
                                  P9A_MI_MCFGP0A_SMF_LOWER_ADDRESS_LEN>(
                                      (l_extAddr << 9)); //matches 17:35 extendBarAddress shifts left 8 (17-8) = 9
                // SMF upper addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_UPPER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData.insert<P9A_MI_MCFGP0A_SMF_UPPER_ADDRESS,
                                  P9A_MI_MCFGP0A_SMF_UPPER_ADDRESS_LEN>(
                                      (l_extAddr << 9)); //matches 17:35 extendBarAddress shifts left 8 (17-8) = 9


                //Mirrored BAR = Non-mirrored BAR >> 1 since bit 56 is not a dsaddr bit
                // MCFGPA SMF valid (bit 0)
                l_scomData_mirror.setBit<P9A_MI_MCFGPM0A_SMF_VALID>();

                // MCFGPA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE
                l_scomData_mirror.setBit<P9A_MI_MCFGPM0A_SMF_EXTEND_TO_END_OF_RANGE>();

                // SMF lower addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_LOWER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData_mirror.insert<P9A_MI_MCFGPM0A_SMF_LOWER_ADDRESS,
                                         P9A_MI_MCFGPM0A_SMF_LOWER_ADDRESS_LEN>(
                                             (l_extAddr << 8)); //matches 17:35 extendBarAddress shifts left 8 (17- (8 + 1)) = 8
                // SMF upper addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_UPPER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData_mirror.insert<P9A_MI_MCFGPM0A_SMF_UPPER_ADDRESS,
                                         P9A_MI_MCFGPM0A_SMF_UPPER_ADDRESS_LEN>(
                                             (l_extAddr << 8)); //matches 17:35 extendBarAddress shifts left 8 (17- (8 + 1)) = 8
            }
            else
            {
                //Mirrored
                // MCFGPA SMF valid (bit 0)
                l_scomData_mirror.setBit<P9A_MI_MCFGPM0A_SMF_VALID>();

                // MCFGPA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE
                l_scomData_mirror.setBit<P9A_MI_MCFGPM0A_SMF_EXTEND_TO_END_OF_RANGE>();

                // SMF lower addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_LOWER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData_mirror.insert<P9A_MI_MCFGPM0A_SMF_LOWER_ADDRESS,
                                         P9A_MI_MCFGPM0A_SMF_LOWER_ADDRESS_LEN>(
                                             (l_extAddr << 9)); //matches 17:35 extendBarAddress shifts left 8 (17- 8) = 9
                // SMF upper addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_UPPER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData_mirror.insert<P9A_MI_MCFGPM0A_SMF_UPPER_ADDRESS,
                                         P9A_MI_MCFGPM0A_SMF_UPPER_ADDRESS_LEN>(
                                             (l_extAddr << 9)); //matches 17:35 extendBarAddress shifts left 8 (17- 8) = 9

                //Non-Mirrored BAR = Mirrored BAR << 1 since bit 56 is now a dsaddr bit
                // MCFGPA SMF valid (bit 0)
                l_scomData.setBit<P9A_MI_MCFGP0A_SMF_VALID>();

                // MCFGPA_SMF_UPPER_ADDRESS_AT_END_OF_RANGE
                l_scomData.setBit<P9A_MI_MCFGP0A_SMF_EXTEND_TO_END_OF_RANGE>();

                // SMF lower addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_LOWER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData.insert<P9A_MI_MCFGP0A_SMF_LOWER_ADDRESS,
                                  P9A_MI_MCFGP0A_SMF_LOWER_ADDRESS_LEN>(
                                      (l_extAddr << 10)); //matches 17:35 extendBarAddress shifts left 8 (17- (8 - 1)) = 10
                // SMF upper addr
                l_norAddr = 0;
                l_norAddr.insertFromRight<17, 19>(l_data.MCFGPA_SMF_UPPER_addr);
                FAPI_TRY(extendBarAddress(l_ext_mask, l_norAddr, l_extAddr));
                l_scomData.insert<P9A_MI_MCFGP0A_SMF_UPPER_ADDRESS,
                                  P9A_MI_MCFGP0A_SMF_UPPER_ADDRESS_LEN>(
                                      (l_extAddr << 10)); //matches 17:35 extendBarAddress shifts left 8 (17- (8 - 1)) = 10


            }
        }

        // Write to reg
        if (l_pos % 2 == 0)
        {
            FAPI_INF("Write MCFGP0A reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGP0A, l_scomData);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGP0A, l_scomData),
                     "Error writing to P9A_MI_MCFGP0A reg");

            FAPI_INF("Write MCFGPM0A reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGPM0A, l_scomData_mirror);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGPM0A, l_scomData_mirror),
                     "Error writing to P9A_MI_MCFGPM0A reg");
        }
        else
        {
            FAPI_INF("Write MCFGP1A reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGP1A, l_scomData);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGP1A, l_scomData),
                     "Error writing to P9A_MI_MCFGP1A reg");

            FAPI_INF("Write MCFGPM1A reg 0x%.16llX, Value 0x%.16llX",
                     P9A_MI_MCFGPM1A, l_scomData_mirror);
            FAPI_TRY(fapi2::putScom(l_target.getParent<fapi2::TARGET_TYPE_MI>(), P9A_MI_MCFGPM1A, l_scomData_mirror),
                     "Error writing to P9A_MI_MCFGP1A reg");
        }
    } // Data pair loop

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Apply Gemini MDI bit workaround and mask Channel Timeout
///
/// @param[in] i_target target to set actions/mask
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
fapi2::ReturnCode applyGeminiFixes(const fapi2::Target<fapi2::TARGET_TYPE_MCC> i_target)
{
    FAPI_DBG("Entering fixGeminiMDI on %s", mss::c_str(i_target));

    fapi2::buffer<uint64_t> l_scom_data;
    uint8_t l_any_gemini = 0;
    const auto l_omiChiplets = i_target.getChildren<fapi2::TARGET_TYPE_OMI>();

    for (const auto& l_omi : l_omiChiplets)
    {
        const auto& l_ocmb_chips = l_omi.getChildren<fapi2::TARGET_TYPE_OCMB_CHIP>();

        if (!l_ocmb_chips.empty())
        {
            uint8_t l_workaround = 0;
            FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_EC_FEATURE_AXONE_GEMINI_MDI_ERROR,
                                   l_ocmb_chips[0],
                                   l_workaround),
                     "Error from FAPI_ATTR_GET (ATTR_CHIP_EC_FEATURE_AXONE_GEMINI_MDI_ERROR)");
            l_any_gemini |= l_workaround;
        }
    }

    if(l_any_gemini)
    {
        // MDI Workaround
        FAPI_TRY(fapi2::getScom(i_target, P9A_MCC_USTLCFG, l_scom_data),
                 "Error reading from MCC_USTLCFG reg");
        l_scom_data.setBit<P9A_MCC_USTLCFG_DEFAULT_META_DATA_ENABLE>();
        l_scom_data.insertFromRight<P9A_MC_USTLCFG_DEFAULT_META_DATA,
                                    P9A_MC_USTLCFG_DEFAULT_META_DATA_LEN>(USTL_MDI_EQUAL_ONE);
        FAPI_TRY(fapi2::putScom(i_target, P9A_MCC_USTLCFG, l_scom_data),
                 "Error writing to MCC_USTLCFG reg");


        fapi2::Target<fapi2::TARGET_TYPE_MI> l_mi_target = i_target.getParent<fapi2::TARGET_TYPE_MI>();
        // Channel Timeout
        FAPI_TRY(fapi2::getScom(l_mi_target, P9A_MI_MCTO, l_scom_data),
                 "Error reading from MCC_USTLCFG reg");
        l_scom_data.clearBit<P9A_MI_MCTO_ENABLE_CHANNEL_HANG>();
        FAPI_TRY(fapi2::putScom(l_mi_target, P9A_MI_MCTO, l_scom_data),
                 "Error writing to MCC_USTLCFG reg");
    }

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    FAPI_DBG("Exiting fixGeminiMDI on %s", mss::c_str(i_target));
    return fapi2::current_err;
}
///
/// @brief Unmask FIR before opening BARs
///
/// @param[in] i_target target to set actions/mask
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode unmaskMCFIR(const fapi2::Target<T> i_target)
{
    FAPI_DBG("Entering");

    fapi2::buffer<uint64_t> l_mcfiraction;
    fapi2::buffer<uint64_t> l_mcfirmask_and;

    // Setup MC Fault Isolation Action1 register buffer
    l_mcfiraction.setBit<MCS_MCFIR_MC_INTERNAL_RECOVERABLE_ERROR>();
    l_mcfiraction.setBit<MCS_MCFIR_COMMAND_LIST_TIMEOUT>();

    if (T == fapi2::TARGET_TYPE_MI)
    {
        l_mcfiraction.setBit<MCS_MCFIR_MS_WAT_DEBUG_CONFIG_REG_ERROR>();
    }

    // Setup FIR bits in MC Fault Isolation Mask Register buffer
    l_mcfirmask_and.flush<1>();

    if (T == fapi2::TARGET_TYPE_MI)
    {
        l_mcfirmask_and.clearBit<MCS_MCFIR_INBAND_BAR_HIT_WITH_INCORRECT_TTYPE>();
    }

    l_mcfirmask_and.clearBit<MCS_MCFIR_MC_INTERNAL_RECOVERABLE_ERROR>();
    l_mcfirmask_and.clearBit<MCS_MCFIR_MC_INTERNAL_NONRECOVERABLE_ERROR>();
    l_mcfirmask_and.clearBit<MCS_MCFIR_POWERBUS_PROTOCOL_ERROR>();
    l_mcfirmask_and.clearBit<MCS_MCFIR_MULTIPLE_BAR>();

    if (T == fapi2::TARGET_TYPE_MCS)
    {
        l_mcfirmask_and.clearBit<MCS_MCFIR_INVALID_ADDRESS>();
    }

    l_mcfirmask_and.clearBit<MCS_MCFIR_COMMAND_LIST_TIMEOUT>();

    if (T == fapi2::TARGET_TYPE_MI)
    {
        l_mcfirmask_and.clearBit<MCS_MCFIR_MS_WAT_DEBUG_CONFIG_REG_ERROR>();
    }

    // Defect HW451708, HW451711
    // Leave MCS_MCFIR_INVALID_SMF_ACCESS masked if MCD cl_probes are enabled

    char l_targetStr[fapi2::MAX_ECMD_STRING_LEN];
    fapi2::toString(i_target, l_targetStr, sizeof(l_targetStr));
    FAPI_INF("Unmask FIR for MC target: %s", l_targetStr);

    // Write MC FIR action1
    FAPI_TRY(fapi2::putScom(i_target, MCS_MCFIRACT1, l_mcfiraction),
             "Error from putScom (MCS_MCFIRACT1)");

    // Write mask
    FAPI_TRY(fapi2::putScom(i_target, MCS_MCFIRMASK_AND, l_mcfirmask_and),
             "Error from putScom (MCS_MCFIRMASK_AND)");

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief Unmask FIR before opening BARs
///
/// @param[in] i_mcBarDataPair Target pair <target, data>
///
/// @return FAPI2_RC_SUCCESS if success, else error code.
///
template<fapi2::TargetType T>
fapi2::ReturnCode unmaskMCFIR(
    const std::vector<std::pair<fapi2::Target<T>, mcBarData_t>>& i_mcBarDataPair)
{
    FAPI_DBG("Entering");

    for (auto l_pair : i_mcBarDataPair)
    {
        fapi2::Target<T> l_target = l_pair.first;
        FAPI_TRY(unmaskMCFIR(l_target));

    } // Data pair loop

fapi_try_exit:
    FAPI_DBG("Exit");
    return fapi2::current_err;
}

///
/// @brief p9_mss_setup_bars procedure entry point
/// See doxygen in p9_mss_setup_bars.H
///
fapi2::ReturnCode p9_mss_setup_bars(
    const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>& i_target)
{
    FAPI_DBG("Entering");

    uint8_t l_mem_ipl_complete = 1;

    // Stores data read from ATTR_MSS_MCS_GROUP_32
    uint32_t l_groupData[DATA_GROUPS][DATA_ELEMENTS];
    // std_pair<MCS target, MCS data>
    std::vector<std::pair<fapi2::Target<fapi2::TARGET_TYPE_MCS>, mcBarData_t>> l_mcsBarDataPair;
    // std_pair<MI target, MI data>
    std::vector<std::pair<fapi2::Target<fapi2::TARGET_TYPE_MI>, mcBarData_t>> l_miBarDataPair;
    // std_pair<MCC target, MCC data>
    std::vector<std::pair<fapi2::Target<fapi2::TARGET_TYPE_MCC>, mcBarData_t>> l_mccBarDataPair;

    fapi2::ATTR_CHIP_EC_FEATURE_OMI_Type l_omi;

    // Get functional MCS chiplets, should be none for Cumulus
    auto l_mcsChiplets = i_target.getChildren<fapi2::TARGET_TYPE_MCS>();
    // Get functional MI chiplets, should be none for Nimbus
    auto l_miChiplets = i_target.getChildren<fapi2::TARGET_TYPE_MI>();
    // Get MCC chiplets for Axone
    auto l_mccChiplets = i_target.getChildren<fapi2::TARGET_TYPE_MCC>();

    FAPI_INF("Num of MCS %u; Num of MIs %u", l_mcsChiplets.size(), l_miChiplets.size());

    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CHIP_EC_FEATURE_OMI,
                           i_target,
                           l_omi),
             "Error from FAPI_ATTR_GET (ATTR_CHIP_EC_FEATURE_OMI)");

    if ( (l_mcsChiplets.size() > 0) && (l_miChiplets.size() > 0) )
    {
        FAPI_ASSERT(false,
                    fapi2::MSS_SETUP_BARS_INVALID_MC_CHIPLETS_DETECTED()
                    .set_NUM_MCS(l_mcsChiplets.size())
                    .set_NUM_MI(l_miChiplets.size()),
                    "Error: Both MCS and MI chiplets are found in proc, "
                    "NumOfMCS %u, NumOfMI %u",
                    l_mcsChiplets.size(), l_miChiplets.size());
    }

    // Get group data setup by p9_mss_eff_grouping
    memset(l_groupData, 0, sizeof(l_groupData));
    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_MSS_MCS_GROUP_32, i_target,
                           l_groupData),
             "Error getting ATTR_MSS_MCS_GROUP_32, l_rc 0x%.8X",
             uint64_t(fapi2::current_err));

    // Setup BAR for Nimbus
    if (l_mcsChiplets.size() > 0)
    {
        // Validate group data from attributes
        FAPI_TRY(validateGroupData(l_mcsChiplets, false, l_groupData),
                 "validateGroupData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Build MC BAR data based on Group data info
        FAPI_TRY(buildMCBarData(l_mcsChiplets, l_groupData, l_mcsBarDataPair),
                 "buildMCBarData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Unmask MC FIRs
        FAPI_TRY(unmaskMCFIR(l_mcsBarDataPair),
                 "unmaskMCFIR() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Write data to MCS
        FAPI_TRY(writeMCBarData(l_mcsBarDataPair),
                 "writeMCBarData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));
    }

    // Setup BAR for Axone
    else if (l_mccChiplets.size() > 0)
    {
        // Validate group data from attributes
        FAPI_TRY(validateGroupData(l_mccChiplets, true, l_groupData),
                 "validateGroupData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Build MC BAR data based on Group data info
        FAPI_TRY(buildMCBarData(l_mccChiplets, l_groupData, l_mccBarDataPair),
                 "buildMCBarData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Unmask MC FIRs
        for (auto l_target : l_miChiplets)
        {
            FAPI_TRY(unmaskMCFIR(l_target),
                     "unmaskMCFIR() returns error, l_rc 0x%.8X",
                     uint64_t(fapi2::current_err));
        }

        // Apply Gemini MDI bit workaround
        for (fapi2::Target<fapi2::TARGET_TYPE_MCC> l_target : l_mccChiplets)
        {
            FAPI_TRY(applyGeminiFixes(l_target),
                     "fixGeminiMDI() returns error, l_rc 0x%.8X",
                     uint64_t(fapi2::current_err));
        }

        // Write data to MI
        FAPI_TRY(writeMCBarData(l_mccBarDataPair),
                 "writeMCBarData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

    }
    // Setup BAR for Cumulus
    else if (l_miChiplets.size() > 0)
    {
        // Validate group data from attributes
        FAPI_TRY(validateGroupData(l_miChiplets, false, l_groupData),
                 "validateGroupData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Build MC BAR data based on Group data info
        FAPI_TRY(buildMCBarData(l_miChiplets, l_groupData, l_miBarDataPair),
                 "buildMCBarData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Unmask MC FIRs
        FAPI_TRY(unmaskMCFIR(l_miBarDataPair),
                 "unmaskMCFIR() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

        // Write data to MI
        FAPI_TRY(writeMCBarData(l_miBarDataPair),
                 "writeMCBarData() returns error, l_rc 0x%.8X",
                 uint64_t(fapi2::current_err));

    }

    // write attribute signifying BARs are valid & MSS inits are finished
    FAPI_TRY(FAPI_ATTR_SET(fapi2::ATTR_MSS_MEM_IPL_COMPLETE, i_target, l_mem_ipl_complete),
             "Error from FAPI_ATTR_SET (ATTR_MSS_MEM_IPL_COMPLETE)");

fapi_try_exit:
    FAPI_DBG("Exiting");

    return fapi2::current_err;
}
OpenPOWER on IntegriCloud