summaryrefslogtreecommitdiffstats
path: root/src/usr/devtree/bld_devtree.C
blob: a73c81274bb0d3e05e1f45a54e5a6bf81134823e (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/devtree/bld_devtree.C $                               */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2013,2015                        */
/* [+] Google Inc.                                                        */
/* [+] 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                                                     */

#include <trace/interface.H>
#include <errl/errlentry.H>
#include <targeting/common/target.H>
#include <targeting/common/targetservice.H>
#include <targeting/common/utilFilter.H>
#include <targeting/attrrp.H>
#include <devtree/devtree_reasoncodes.H>
#include <devtree/devtreeif.H>
#include "devtree.H"
#include <sys/mmio.h> //THIRTYTWO_GB
#include <intr/interrupt.H>
#include <vpd/vpd_if.H>
#include <stdio.h>
#include <pnor/pnorif.H>
#include <sys/mm.h>
#include <util/align.H>
#include <vector>
#include <vfs/vfs.H>
#include <fsi/fsiif.H>
#include <config.h>
#include <devicefw/userif.H>
#include <vpd/cvpdenums.H>
#include <i2c/i2cif.H>
#include <i2c/eepromif.H>
#include <ipmi/ipmisensor.H>
#include <fapi.H>
#include <fapiPlatHwpInvoker.H> // for fapi::fapiRcToErrl()
#include <vpd/mvpdenums.H>

trace_desc_t *g_trac_devtree = NULL;
TRAC_INIT(&g_trac_devtree, "DEVTREE", 4096);

namespace DEVTREE
{
using   namespace   TARGETING;

#define CHIPID_EXTRACT_NODE(i_chipid) (i_chipid >> CHIPID_NODE_SHIFT)
#define CHIPID_EXTRACT_PROC(i_chipid) (i_chipid & CHIPID_PROC_MASK)

enum BuildConstants
{
    DEVTREE_DATA_ADDR   =0xFF00000,    /* 256MB - 1MB*/
    DEVTREE_SPACE_SIZE  =0x10000,      /*64KB*/
    XSCOM_NODE_SHIFT    =38,           /*Node pos is 25, so 63-25=38*/
    XSCOM_CHIP_SHIFT    =35,           /*Chip pos is 28, so 63-28=35*/
    CHIPID_NODE_SHIFT   =3,            /*CHIPID is NNNCCC, shift 3*/
    CHIPID_PROC_MASK    =0x7,            /*CHIPID is NNNCCC, shift 3*/
    PHB0_MASK           =0x80,
    MAX_PHBs            =3,             /*Max PHBs per chip is 3*/
    THREADPERCORE       =8,             /*8 threads per core*/
    MHZ                 =1000000,

    /* The Cache unit address (and reg property) is mostly free-for-all
     * as long as there is no collisions. On HDAT machines we use the
     * following encoding which I encourage you to also follow to limit
     * surprises:
     *
     * L2   :  (0x20 << 24) | PIR (PIR is PIR value of thread 0 of core)
     * L3   :  (0x30 << 24) | PIR
     * L3.5 :  (0x35 << 24) | PIR */
    L2_HDR              = (0x20 << 24),
    L3_HDR              = (0x30 << 24),
    CEN_ID_SHIFT        = 4,
    CEN_ID_TAG          = 0x80000000,
};



//@todo-RTC:123043 -- Should use the functions in RT_TARG
uint32_t getProcChipId(const TARGETING::Target * i_pProc)
{
    uint32_t l_fabId = i_pProc->getAttr<TARGETING::ATTR_FABRIC_NODE_ID>();
    uint32_t l_procPos = i_pProc->getAttr<TARGETING::ATTR_FABRIC_CHIP_ID>();
    return ( (l_fabId << CHIPID_NODE_SHIFT) + l_procPos);
}

//@todo-RTC:123043 -- Should use the functions in RT_TARG
uint32_t getMembChipId(const TARGETING::Target * i_pMemb)
{
    PredicateCTM l_mcs(CLASS_UNIT,TYPE_MCS, MODEL_NA);
    TargetHandleList mcs_list;
    targetService().getAssociated(mcs_list,
                                  i_pMemb,
                                  TargetService::PARENT_BY_AFFINITY,
                                  TargetService::ALL,
                                  &l_mcs);

    if( mcs_list.size() != 1 )
    {
        //should never happen
        return 0;
    }
    Target* l_parentMCS = *(mcs_list.begin());
    uint32_t l_procId = getProcChipId(getParentChip(l_parentMCS));
    uint32_t l_membId = CEN_ID_TAG | (l_procId << CEN_ID_SHIFT);
    l_membId |= l_parentMCS->getAttr<ATTR_CHIP_UNIT>();
    return l_membId;
}


uint64_t getHomerPhysAddr(const TARGETING::Target * i_pProc)
{
    //If running Sapphire need to place this at the top of memory
    uint64_t homerPhysAddrBase = VMM_HOMER_REGION_START_ADDR;
    if(TARGETING::is_sapphire_load())
    {
        homerPhysAddrBase = TARGETING::get_top_mem_addr();
        assert (homerPhysAddrBase != 0,
                "bld_devtree: Top of memory was 0!");
        homerPhysAddrBase -= VMM_ALL_HOMER_OCC_MEMORY_SIZE;
    }

    uint8_t tmpPos = i_pProc->getAttr<ATTR_POSITION>();
    uint64_t tmpOffset = tmpPos*VMM_HOMER_INSTANCE_SIZE;
    uint64_t targHomer = homerPhysAddrBase + tmpOffset;

    TRACFCOMP( g_trac_devtree, "proc ChipID [%X] HOMER is at %.16X",
               getProcChipId(i_pProc), targHomer );

    return targHomer;
}

void add_i2c_info( const TARGETING::Target* i_targ,
                   devTree* i_dt,
                   dtOffset_t i_node )
{
    TRACFCOMP(g_trac_devtree,"add_i2c_info(%X)",TARGETING::get_huid(i_targ));

    //get list of all I2C Masters
    std::list<I2C::MasterInfo_t> l_i2cInfo;
    I2C::getMasterInfo( i_targ, l_i2cInfo );

    //find all of the EEPROMs connected via i2c
    std::list<EEPROM::EepromInfo_t> l_eepromInfo;
    EEPROM::getEEPROMs( l_eepromInfo );

    //add any other i2c devices here as needed, e.g. TPM, etc

    //figure out what kind of chip we're talking about
    TARGETING::TYPE l_master_type = i_targ->getAttr<TARGETING::ATTR_TYPE>();
    const char* l_masterName = "ibm,unknown";
    const char* l_chipname = "xx";
    uint32_t l_chipid = 0x0;
    if( l_master_type == TARGETING::TYPE_PROC )
    {
        l_masterName = "ibm,power8-i2cm";
        l_chipid = getProcChipId(i_targ);
        l_chipname = "p8";
    }
    else if( l_master_type == TARGETING::TYPE_MEMBUF )
    {
        l_masterName = "ibm,centaur-i2cm";
        l_chipid = getMembChipId(i_targ);
        l_chipname = "cen";
    }

    //compatible devices to make Opal/Linux happy
    static const struct
    {
        const char* name;
        size_t byteSize;
        size_t addrBytes;
    } atmel_ids[] = {
        { "atmel,24c128", 16*KILOBYTE, 2 },
        { "atmel,24c256", 32*KILOBYTE, 2 },
        { "atmel,24c02",  256,         1 },

        //Currently our minimum is 1KB, even for the 256 byte SPD
        { "atmel,24c02",  1*KILOBYTE,  1 },
    };

    /*
     Devtree hierarchy is like so
         i2cm@12345 {
             i2c-bus@0 {
                 eeprom@12 {
                 }
             }
             i2c-bus@1 {
                 eeprom@12 {
                 }
                 eeprom@34 {
                 }
             }
         }
     */
    for( std::list<I2C::MasterInfo_t>::iterator i2cm = l_i2cInfo.begin();
         i2cm != l_i2cInfo.end();
         ++i2cm )
    {
        /*
         i2cm@a0020 {
             reg = <0xa0020 0x20>; << scom address space
             chip-engine# = <0x1>; << i2c engine
             compatible = "ibm,power8-i2cm"; << what Opal wants
             clock-frequency = <0x2faf080>; << local bus in Hz
             #address-cells = <0x1>;
             phandle = <0x10000062>; << auto-filled
             #size-cells = <0x0>;
             linux,phandle = <0x10000062>; << Opal fills in
         }
         */
        dtOffset_t l_i2cNode = i_dt->addNode(i_node,
                                             "i2cm", i2cm->scomAddr);
        uint32_t l_i2cProp[2] = {
            static_cast<uint32_t>(i2cm->scomAddr),
            0x20 }; //0x20 is number of scom regs per engine
        i_dt->addPropertyCells32(l_i2cNode, "reg", l_i2cProp, 2);
        i_dt->addPropertyCell32(l_i2cNode, "chip-engine#", i2cm->engine);
        const char* l_i2cCompatStrs[] = {l_masterName, NULL};
        i_dt->addPropertyStrings(l_i2cNode, "compatible", l_i2cCompatStrs);
        i_dt->addPropertyCell32(l_i2cNode, "clock-frequency", i2cm->freq);
        i_dt->addPropertyCell32(l_i2cNode, "#address-cells", 1);
        i_dt->addPropertyCell32(l_i2cNode, "#size-cells", 0);


        /*I2C busses*/
        std::list<EEPROM::EepromInfo_t>::iterator eep = l_eepromInfo.begin();
        while( eep != l_eepromInfo.end() )
        {
            // ignore the devices that aren't on the current target
            if( eep->i2cMaster != i_targ )
            {
                eep = l_eepromInfo.erase(eep);
                continue;
            }
            // skip the devices that are on a different engine
            else if( eep->engine != i2cm->engine )
            {
                ++eep;
                continue;
            }

            /*
             i2c-bus@0 {
                 reg = <0x0>;
                 bus-frequency = <0x61a80>;
                 compatible = "ibm,power8-i2c-port", << Opal fills in
                              "ibm,opal-i2c"; << Opal fills in
                 ibm,opal-id = <0x1>; << Opal fills in
                 ibm,port-name = "p8_00000000_e1p0"; << chip_chipid_eng_port
                 #address-cells = <0x1>;
                 phandle = <0x10000063>; << auto-filled
                 #size-cells = <0x0>;
                 linux,phandle = <0x10000063>;
             }
             */
            dtOffset_t l_busNode = i_dt->addNode( l_i2cNode,
                                                  "i2c-bus", eep->port );
            i_dt->addPropertyCell32(l_busNode, "reg", eep->port);
            i_dt->addPropertyCell32(l_busNode, "bus-frequency", eep->busFreq);
            i_dt->addPropertyCell32(l_busNode, "#address-cells", 1);
            i_dt->addPropertyCell32(l_busNode, "#size-cells", 0);
            char portname[20];
            sprintf( portname, "%s_%.8X_e%dp%d",
                     l_chipname,
                     l_chipid,
                     eep->engine,
                     eep->port );
            i_dt->addPropertyString(l_busNode,
                                    "ibm,port-name",
                                    portname);

            // find any other devices on the same port so we can add them
            //  all at once
            EEPROM::EepromInfo_t cur_eep = *eep;
            std::list<EEPROM::EepromInfo_t>::iterator eep2 = eep;
            while( eep2 != l_eepromInfo.end() )
            {
                // skip the devices for other busses
                if( !((cur_eep.i2cMaster == eep2->i2cMaster)
                      && (cur_eep.engine == eep2->engine)
                      && (cur_eep.port == eep2->port)) )
                {
                    ++eep2;
                    continue;
                }

                /*
                eeprom@50 {
                    reg = <0x50>; << right-justified 7-bit addr
                    label = "system-vpd"; << arbitrary name
                    compatible = "atmel,24c64"; << use table above
                    status = "ok"; << Opal fills in
                    phandle = <0x10000065>; << auto-filled
                    linux,phandle = <0x10000065>; << Opal fills in
                 }
                */
                dtOffset_t l_eepNode = i_dt->addNode( l_busNode,
                                                      "eeprom",
                                                      eep2->devAddr >> 1 );
                i_dt->addPropertyCell32(l_eepNode, "reg", eep2->devAddr >> 1);
                char l_label[30];
                TARGETING::TYPE l_type = TARGETING::TYPE_NA;
                l_type = eep2->assocTarg->getAttr<TARGETING::ATTR_TYPE>();
                if( (l_type == TARGETING::TYPE_SYS)
                    || (l_type == TARGETING::TYPE_NODE) )
                {
                    sprintf( l_label, "system-vpd" );
                }
                else if( l_type == TARGETING::TYPE_PROC )
                {
                    const char* l_type = "vpd";
                    switch( eep2->device )
                    {
                        case(EEPROM::VPD_PRIMARY):
                            l_type = "proc-vpd";
                            break;
                        case(EEPROM::VPD_BACKUP):
                            l_type = "proc-vpd-backup";
                            break;
                        case(EEPROM::SBE_PRIMARY):
                            l_type = "sbe0";
                            break;
                        case(EEPROM::SBE_BACKUP):
                            l_type = "sbe1";
                            break;
                        default:
                            break;
                    }
                    sprintf( l_label, "%s-%d",
                             l_type,
                             eep2->assocTarg
                             ->getAttr<TARGETING::ATTR_POSITION>() );
                }
                else if( l_type == TARGETING::TYPE_MEMBUF )
                {
                    //@fixme-RTC:118373-Remove Hab/Palm workaround
                    //   once node vpd is supported
                    sprintf( l_label, "system-vpd" );
                    /*sprintf( l_label, "memb-vpd-%d",
                             eep2->assocTarg
                             ->getAttr<TARGETING::ATTR_POSITION>() );*/
                }
                else if( l_type == TARGETING::TYPE_DIMM )
                {
                    sprintf( l_label, "dimm-spd-%d",
                             eep2->assocTarg
                             ->getAttr<TARGETING::ATTR_POSITION>() );
                }
                else
                {
                    sprintf( l_label, "unknown" );
                }
                i_dt->addPropertyString(l_eepNode, "label", l_label);

                // fill in atmel compatible
                const char* l_compat = "unknown";
                bool l_foundit = false;
                for( size_t a = 0;
                     a < (sizeof(atmel_ids)/sizeof(atmel_ids[0]));
                     a++ )
                {
                    if( (atmel_ids[a].byteSize == (KILOBYTE*eep2->sizeKB))
                        || (atmel_ids[a].addrBytes == eep2->addrBytes) )
                    {
                        l_compat = atmel_ids[a].name;
                        l_foundit = true;
                        break;
                    }
                }
                if( !l_foundit )
                {
                    TRACFCOMP( g_trac_devtree, "Could not find matching eeprom device for %s : size=%d,addr=%d", l_label, eep2->sizeKB, eep2->addrBytes );
                }
                i_dt->addPropertyString(l_eepNode, "compatible", l_compat);

                // need to increment the outer loop if we're going to
                //  remove the element it points to
                if( eep == eep2 )
                {
                    ++eep;
                }
                // now remove the device we added so we don't add it again
                eep2 = l_eepromInfo.erase(eep2);
            }
        }
    }

}

void bld_getSideInfo(PNOR::SideId i_side,
                     uint32_t     o_TOCaddress[2],
                     uint8_t    & o_count,
                     bool       & o_isGolden)
{
    errlHndl_t  errhdl = NULL;
    PNOR::SideInfo_t  l_info;

    o_count = 0;
    o_isGolden = false;

    errhdl = getSideInfo (i_side, l_info);
    if (!errhdl)
    {
        // return the valid TOC offsets & count of valid TOCs
        if (PNOR::INVALID_OFFSET != l_info.primaryTOC)
        {
            o_TOCaddress[o_count++] = l_info.primaryTOC;
        }
        if (PNOR::INVALID_OFFSET != l_info.backupTOC)
        {
            o_TOCaddress[o_count++] = l_info.backupTOC;
        }
        o_isGolden      = l_info.isGolden;
    }
    else
    {
        // commit error and return 0 TOC offsets
        errlCommit(errhdl, DEVTREE_COMP_ID);
    }

    return;
}

void bld_fdt_pnor(devTree *   i_dt,
                  dtOffset_t  i_parentNode)
{
    do
    {
        uint32_t l_active[2]    = {PNOR::INVALID_OFFSET,PNOR::INVALID_OFFSET};
        uint32_t l_golden[2]    = {PNOR::INVALID_OFFSET,PNOR::INVALID_OFFSET};
        uint8_t  l_count = 0;
        bool     l_isGolden = false;
        bool     l_goldenFound = false;
        uint8_t  l_goldenCount = 0;
        PNOR::PnorInfo_t l_pnorInfo;

        //Get pnor address and size
        getPnorInfo (l_pnorInfo);

        dtOffset_t l_pnorNode = i_dt->addNode(i_parentNode,
                                              "pnor",
                                              l_pnorInfo.mmioOffset);

        const uint8_t l_isaLinkage = 0; // 0==Mem
        uint32_t pnor_prop[3] = {l_isaLinkage,
                                 l_pnorInfo.mmioOffset,
                                 l_pnorInfo.flashSize};
        i_dt->addPropertyCells32(l_pnorNode, "reg", pnor_prop, 3);

        //Add Working/Active parition
        bld_getSideInfo(PNOR::WORKING,l_active,l_count,l_isGolden);
        if (l_count) // valid TOCs present
        {
            i_dt->addPropertyCells32(l_pnorNode,
                                 "active-image-tocs", l_active, l_count);
            // capture golden
            if (l_isGolden)
            {
                l_golden[0] = l_active[0];
                l_golden[1] = l_active[1];
                l_goldenCount = l_count;
                l_goldenFound = true;
            }
        }

#if CONFIG_PNOR_TWO_SIDE_SUPPORT
        //Add Alternate parition
        uint32_t l_alternate[2] = {PNOR::INVALID_OFFSET,PNOR::INVALID_OFFSET};

        bld_getSideInfo(PNOR::ALTERNATE,l_alternate,l_count,l_isGolden);
        if (l_count) // valid TOCs present
        {
            i_dt->addPropertyCells32(l_pnorNode,
                                 "alternate-image-tocs",l_alternate,l_count);
            // capture golden
            if (l_isGolden)
            {
                l_golden[0] = l_alternate[0];
                l_golden[1] = l_alternate[1];
                l_goldenCount = l_count;
                l_goldenFound = true;
            }
        }
#endif

        //Include golden if there is one
        if (l_goldenFound)
        {
            i_dt->addPropertyCells32(l_pnorNode,
                                 "golden-image-tocs",l_golden,l_goldenCount);
        }

    } while (0);

    return;
}

void bld_xscom_node(devTree * i_dt, dtOffset_t & i_parentNode,
                    const TARGETING::Target * i_pProc, uint32_t i_chipid)
{
    const char* xscomNodeName = "xscom";
    const char* todNodeName = "chiptod";
    const char* pciNodeName = "pbcq";

    // Grab a system object to work with
    TARGETING::Target* sys = NULL;
    TARGETING::targetService().getTopLevelTarget(sys);

    uint64_t l_xscomBaseAddr =
      sys->getAttr<TARGETING::ATTR_XSCOM_BASE_ADDRESS>();

    /**********************************************************/
    /*                   Xscom node                           */
    /**********************************************************/
    uint64_t l_xscomAddr = l_xscomBaseAddr +
      (static_cast<uint64_t>(i_chipid) << XSCOM_CHIP_SHIFT);

    dtOffset_t xscomNode = i_dt->addNode(i_parentNode, xscomNodeName,
                                         l_xscomAddr);

    i_dt->addPropertyCell32(xscomNode, "#address-cells", 1);
    i_dt->addPropertyCell32(xscomNode, "#size-cells", 1);
    i_dt->addProperty(xscomNode, "scom-controller");
    const char* xscom_compatStrs[] = {"ibm,xscom","ibm,power8-xscom",NULL};
    i_dt->addPropertyStrings(xscomNode, "compatible", xscom_compatStrs);

    i_dt->addPropertyCell32(xscomNode, "ibm,chip-id", i_chipid);

    uint64_t xscom_prop[2] = { l_xscomAddr,  THIRTYTWO_GB};
    i_dt->addPropertyCells64(xscomNode, "reg", xscom_prop, 2);

    // Add proc chip ECIDs
    ATTR_ECID_type ecid;
    i_pProc->tryGetAttr<ATTR_ECID>(ecid);
    char ecid_ascii[33];
    sprintf(ecid_ascii, "%.16llX%.16llX", ecid[0], ecid[1]);
    i_dt->addPropertyString(xscomNode, "ecid", ecid_ascii);
    CPPASSERT(sizeof(ATTR_ECID_type) == 16);

    /*PSI Host Bridge*/
    uint32_t l_psiInfo = 0x2010900; /*PSI Host Bridge Scom addr*/
    dtOffset_t psiNode = i_dt->addNode(xscomNode, "psihb", l_psiInfo);
    const char* psi_compatStrs[] = {"ibm,power8-psihb-x",
    "ibm,psihb-x", NULL};
    i_dt->addPropertyStrings(psiNode, "compatible", psi_compatStrs);
    uint32_t psi_prop[2] = { l_psiInfo, 0x20 }; //# of scoms in range
    i_dt->addPropertyCells32(psiNode, "reg", psi_prop, 2);
    i_dt->addPropertyString(psiNode, "status", "ok");

    /*ChipTod*/
    uint32_t l_todInfo = 0x40000; /*Chip tod Scom addr*/
    dtOffset_t todNode = i_dt->addNode(xscomNode, todNodeName, l_todInfo);
    const char* tod_compatStrs[] = {"ibm,power-chiptod",
    "ibm,power8-chiptod", NULL};
    i_dt->addPropertyStrings(todNode, "compatible", tod_compatStrs);
    uint32_t tod_prop[2] = { l_todInfo, 0x34 }; //# of scoms in range
    i_dt->addPropertyCells32(todNode, "reg", tod_prop, 2);

    //Mark TOD pri/sec
    uint8_t l_role = i_pProc->getAttr<ATTR_TOD_ROLE>();
    if(l_role & TARGETING::TOD_ROLE_PRIMARY)
    {
        i_dt->addProperty(todNode, "primary");
    }
    if(l_role & TARGETING::TOD_ROLE_SECONDARY)
    {
        i_dt->addProperty(todNode, "secondary");
    }

    if(i_chipid == 0x0) //Master chip
    {
        uint32_t l_lpcInfo = 0xB0020; /*ECCB FW addr*/
        dtOffset_t lpcNode = i_dt->addNode(xscomNode,"isa",l_lpcInfo);
        i_dt->addPropertyString(lpcNode, "compatible", "ibm,power8-lpc");
        uint32_t lpc_prop[2] = { l_lpcInfo, 0x4 }; //# of scoms in range
        i_dt->addPropertyCells32(lpcNode, "reg", lpc_prop, 2);
        i_dt->addPropertyCell32(lpcNode, "#address-cells", 2);
        i_dt->addPropertyCell32(lpcNode, "#size-cells", 1);

        bld_fdt_pnor (i_dt, lpcNode);

    }

    /*NX*/
    uint32_t l_nxInfo = 0x2010000; /*NX Scom addr*/
    dtOffset_t nxNode = i_dt->addNode(xscomNode, "nx", l_nxInfo);
    const char* nx_compatStrs[] = {"ibm,power-nx",
    "ibm,power8-nx", NULL};
    i_dt->addPropertyStrings(nxNode, "compatible", nx_compatStrs);
    uint32_t nx_prop[2] = { l_nxInfo, 0x4000 }; //# of scoms in range
    i_dt->addPropertyCells32(nxNode, "reg", nx_prop, 2);


    /*PCIE*/
    uint8_t l_phbActive =
        i_pProc->getAttr<TARGETING::ATTR_PROC_PCIE_PHB_ACTIVE>();
    TARGETING::ATTR_PROC_PCIE_LANE_EQUALIZATION_type l_laneEq = {{0}};
    assert(i_pProc->tryGetAttr<TARGETING::ATTR_PROC_PCIE_LANE_EQUALIZATION>(
        l_laneEq));

    TRACFCOMP( g_trac_devtree, "Chip %X PHB Active mask %X",
               i_chipid, l_phbActive);

    for(uint32_t l_phb =0; l_phb < MAX_PHBs; l_phb++)
    {
        if(!(l_phbActive & (PHB0_MASK>>l_phb)))
        {
            continue;
        }

        TRACFCOMP( g_trac_devtree, "Adding PHB %d", l_phb);

        /*PHB is active, add to Xscom map*/
        uint32_t l_peInfo   = 0x02012000 + (l_phb * 0x400);
        uint32_t l_pciInfo  = 0x09012000 + (l_phb * 0x400);
        uint32_t l_spciInfo = 0x09013c00 + (l_phb * 0x40);
        dtOffset_t pcieNode = i_dt->addNode(xscomNode,pciNodeName,l_peInfo);
        const char* pcie_compatStrs[] = {"ibm,power8-pbcq", NULL};
        i_dt->addPropertyStrings(pcieNode, "compatible", pcie_compatStrs);
        uint32_t pcie_prop[6] = { l_peInfo, 0x20, //# of scoms in range
        l_pciInfo, 0x5, //# of scoms in range
        l_spciInfo, 0x15}; //# of scoms in range
        i_dt->addPropertyCells32(pcieNode, "reg", pcie_prop, 6);
        i_dt->addPropertyCell32(pcieNode, "ibm,phb-index", l_phb);
        i_dt->addProperty(pcieNode, "ibm,use-ab-detect");
        i_dt->addPropertyCells32(pcieNode, "ibm,lane-eq",
            reinterpret_cast<uint32_t*>(l_laneEq[l_phb]),
            (sizeof(l_laneEq[l_phb])/sizeof(uint32_t)));
    }

    /*I2C Masters*/
    add_i2c_info( i_pProc, i_dt, xscomNode );

}

uint32_t bld_l3_node(devTree * i_dt, dtOffset_t & i_parentNode,
                     uint32_t i_pir)
{
    uint32_t l3Id = i_pir | L3_HDR;

    /* Build L3 Cache information */
    dtOffset_t l3Node = i_dt->addNode(i_parentNode, "l3-cache",l3Id);
    i_dt->addPropertyString(l3Node, "device_type", "cache");
    i_dt->addPropertyCell32(l3Node, "reg", l3Id);
    i_dt->addProperty(l3Node, "cache-unified");
    i_dt->addPropertyCell32(l3Node, "d-cache-sets", 0x8);
    i_dt->addPropertyCell32(l3Node, "i-cache-sets", 0x8);
    i_dt->addPropertyCell32(l3Node, "d-cache-size", 0x800000); //8MB
    i_dt->addPropertyCell32(l3Node, "i-cache-size", 0x800000); //8MB
    i_dt->addPropertyString(l3Node, "status", "okay");

    return i_dt->getPhandle(l3Node);
}

uint32_t bld_l2_node(devTree * i_dt, dtOffset_t & i_parentNode,
                     uint32_t i_pir, uint32_t i_nextCacheHandle)
{
    uint32_t l2Id = i_pir | L2_HDR;

    /* Build l2 Cache information */
    dtOffset_t l2Node = i_dt->addNode(i_parentNode, "l2-cache",l2Id);
    i_dt->addPropertyString(l2Node, "device_type", "cache");
    i_dt->addPropertyCell32(l2Node, "reg", l2Id);
    i_dt->addProperty(l2Node, "cache-unified");
    i_dt->addPropertyCell32(l2Node, "d-cache-sets", 0x8);
    i_dt->addPropertyCell32(l2Node, "i-cache-sets", 0x8);
    i_dt->addPropertyCell32(l2Node, "d-cache-size", 0x80000); //512KB
    i_dt->addPropertyCell32(l2Node, "i-cache-size", 0x80000); //512KB
    i_dt->addPropertyString(l2Node, "status", "okay");
    i_dt->addPropertyCell32(l2Node, "next-level-cache", i_nextCacheHandle);


    return i_dt->getPhandle(l2Node);
}

uint32_t bld_cpu_node(devTree * i_dt, dtOffset_t & i_parentNode,
                      const TARGETING::Target * i_ex,
                      INTR::PIR_t i_pir, uint32_t i_chipId,
                      uint32_t i_nextCacheHandle)
{
    /*
     * The following node must exist for each *core* in the system. The unit
     * address (number after the @) is the hexadecimal HW CPU number (PIR value)
     * of thread 0 of that core.
     */

    uint32_t paFeatures[8] = { 0x6, 0x0, 0xf6, 0x3f, 0xc7, 0x0, 0x80, 0xc0 };
    uint32_t pageSizes[4] = { 0xc, 0x10, 0x18, 0x22 };
    uint32_t segmentSizes[4] = { 0x1c, 0x28, 0xffffffff, 0xffffffff };
    uint32_t segmentPageSizes[] =
    {
        12, 0x0, 3,   /* 4k SLB page size, L,LP = 0,x1, 3 page size encodings */
        12, 0x0,      /* 4K PTE page size, L,LP = 0,x0 */
        16, 0x7,      /* 64K PTE page size, L,LP = 1,x7 */
        24, 0x38,     /* 16M PTE page size, L,LP = 1,x38 */
        16, 0x110, 2, /* 64K SLB page size, L,LP = 1,x1, 2 page size encodings*/
        16, 0x1,      /* 64K PTE page size, L,LP = 1,x1 */
        24, 0x8,      /* 16M PTE page size, L,LP = 1,x8 */
        20, 0x130, 1, /* 1M SLB page size, L,LP = 1,x3, 1 page size encoding */
        20, 0x2,      /* 1M PTE page size, L,LP = 1,x2 */
        24, 0x100, 1, /* 16M SLB page size, L,LP = 1,x0, 1 page size encoding */
        24, 0x0,      /* 16M PTE page size, L,LP = 1,x0 */
        34, 0x120, 1, /* 16G SLB page size, L,LP = 1,x2, 1 page size encoding */
        34, 0x3       /* 16G PTE page size, L,LP = 1,x3 */
    };


    dtOffset_t cpuNode = i_dt->addNode(i_parentNode, "PowerPC,POWER8",
                                       i_pir.word);
    i_dt->addPropertyString(cpuNode, "device_type", "cpu");
    i_dt->addProperty(cpuNode, "64-bit");
    i_dt->addProperty(cpuNode, "32-64-bridge");
    i_dt->addProperty(cpuNode, "graphics");
    i_dt->addProperty(cpuNode, "general-purpose");
    i_dt->addPropertyString(cpuNode, "status", "okay");
    i_dt->addPropertyCell32(cpuNode, "reg", i_pir.word);
    i_dt->addPropertyCell32(cpuNode, "ibm,pir", i_pir.word);
    i_dt->addPropertyCell32(cpuNode, "ibm,chip-id", i_chipId);

    uint32_t numThreads = 0;
    TARGETING::Target* sys = NULL;
    TARGETING::targetService().getTopLevelTarget(sys);
    uint64_t en_threads = sys->getAttr<TARGETING::ATTR_ENABLED_THREADS>();

    uint32_t interruptServerNum[THREADPERCORE];
    for(size_t i = 0; i < THREADPERCORE ; i++)
    {
        if (en_threads & (0x8000000000000000 >> i))
        {
            i_pir.threadId = i;
            interruptServerNum[numThreads++] = i_pir.word;
        }
    }
    i_dt->addPropertyCells32(cpuNode, "ibm,ppc-interrupt-server#s",
                             interruptServerNum, numThreads);

    /* Fill in the actual PVR of chip -- it is only a 32 bit reg*/
    uint32_t l_pvr = mmio_pvr_read() & 0xFFFFFFFF;
    i_dt->addPropertyCell32(cpuNode, "cpu-version", l_pvr);

    i_dt->addPropertyCells32(cpuNode, "ibm,processor-segment-sizes",
                             segmentSizes,
                             sizeof(segmentSizes) / sizeof(uint32_t));
    i_dt->addPropertyCells32(cpuNode, "ibm,processor-page-sizes",
                             pageSizes,
                             sizeof(pageSizes) / sizeof(uint32_t));
    i_dt->addPropertyCells32(cpuNode, "ibm,segment-page-sizes",
                             segmentPageSizes,
                             sizeof(segmentPageSizes)/sizeof(uint32_t));
    i_dt->addPropertyCells32(cpuNode, "ibm,pa-features",
                             paFeatures,
                             sizeof(paFeatures)/sizeof(uint32_t));

    i_dt->addPropertyCell32(cpuNode, "ibm,slb-size", 32);
    i_dt->addPropertyCell32(cpuNode, "ibm,dfp", 1);
    i_dt->addPropertyCell32(cpuNode, "ibm,vmx", 2);
    i_dt->addPropertyCell32(cpuNode, "ibm,purr", 1);
    i_dt->addPropertyCell32(cpuNode, "ibm,spurr", 1);

    //Set core clock freq
    uint64_t freq = 0;

#ifdef CONFIG_HTMGT
    if(sys->getAttr<TARGETING::ATTR_HTMGT_SAFEMODE>())
    {
        // Safe mode on, OCC failed to load.  Set safe freq
        freq = sys->getAttr<TARGETING::ATTR_BOOT_FREQ_MHZ>();
    }
    else
    {
        // Safe mode off, set nominal freq
        freq = sys->getAttr<TARGETING::ATTR_NOMINAL_FREQ_MHZ>();
    }
#elif CONFIG_SET_NOMINAL_PSTATE
    // Set nominal core freq if CONFIG_SET_NOMINAL_PSTATE is enabled
    freq = sys->getAttr<TARGETING::ATTR_NOMINAL_FREQ_MHZ>();
#else
    // Else, set safe core freq
    freq = sys->getAttr<TARGETING::ATTR_BOOT_FREQ_MHZ>();
#endif

    freq *= MHZ;

    uint32_t ex_freq[2] = {static_cast<uint32_t>(freq >> 32),
    static_cast<uint32_t>(freq & 0xFFFFFFFF)};
    if(ex_freq[0] == 0) //Only create if fits into 32 bits
    {
        i_dt->addPropertyCell32(cpuNode, "clock-frequency", ex_freq[1]);
    }
    i_dt->addPropertyCells32(cpuNode, "ibm,extended-clock-frequency",
                             ex_freq, 2);

    uint32_t timebase_freq[2] = {0, 512000000};
    i_dt->addPropertyCell32(cpuNode, "timebase-frequency", timebase_freq[1]);
    i_dt->addPropertyCells32(cpuNode, "ibm,extended-timebase-frequency",
                             timebase_freq, 2);


    i_dt->addPropertyCell32(cpuNode, "reservation-granule-size", 0x80);
    i_dt->addPropertyCell32(cpuNode, "d-tlb-size", 0x800);
    i_dt->addPropertyCell32(cpuNode, "i-tlb-size", 0x0);
    i_dt->addPropertyCell32(cpuNode, "tlb-size", 0x800);
    i_dt->addPropertyCell32(cpuNode, "d-tlb-sets", 0x4);
    i_dt->addPropertyCell32(cpuNode, "i-tlb-sets", 0x0);
    i_dt->addPropertyCell32(cpuNode, "tlb-sets", 0x4);
    i_dt->addPropertyCell32(cpuNode, "d-cache-block-size", 0x80);
    i_dt->addPropertyCell32(cpuNode, "i-cache-block-size", 0x80);
    i_dt->addPropertyCell32(cpuNode, "d-cache-size", 0x10000);
    i_dt->addPropertyCell32(cpuNode, "i-cache-size", 0x8000);
    i_dt->addPropertyCell32(cpuNode, "i-cache-sets", 0x4);
    i_dt->addPropertyCell32(cpuNode, "d-cache-sets", 0x8);
    i_dt->addPropertyCell64(cpuNode, "performance-monitor", 0x1);
    i_dt->addPropertyCell32(cpuNode, "next-level-cache", i_nextCacheHandle);

    return i_dt->getPhandle(cpuNode);
}

uint32_t bld_intr_node(devTree * i_dt, dtOffset_t & i_parentNode,
                      const TARGETING::Target * i_ex,
                      INTR::PIR_t i_pir)

{
    /*
     * Interrupt presentation controller (ICP) nodes
     *
     * There is some flexibility as to how many of these are presents since
     * a given node can represent multiple ICPs. When generating from HDAT we
     * chose to create one per core
     */

    uint64_t l_ibase = INTR::getIntpAddr(i_ex, 0);     //IBASE ADDRESS

    dtOffset_t intNode = i_dt->addNode(i_parentNode, "interrupt-controller",
                                       l_ibase);

    const char* intr_compatStrs[] = {"ibm,ppc-xicp", "ibm,power8-xicp",NULL};
    i_dt->addPropertyStrings(intNode, "compatible", intr_compatStrs);
    i_dt->addProperty(intNode,"interrupt-controller");
    i_dt->addPropertyCell32(intNode, "#address-cells", 0);
    i_dt->addPropertyCell32(intNode, "#interrupt-cells", 1);
    i_dt->addPropertyString(intNode, "device_type",
                            "PowerPC-External-Interrupt-Presentation");

    TARGETING::Target* sys = NULL;
    TARGETING::targetService().getTopLevelTarget(sys);
    uint64_t en_threads = sys->getAttr<TARGETING::ATTR_ENABLED_THREADS>();
    uint32_t numThreads = 0;

    uint64_t intr_prop[THREADPERCORE][2];
    for(size_t i=0; i < THREADPERCORE; i++)
    {
        if (en_threads & (0x8000000000000000 >> i))
        {
            intr_prop[numThreads][0] = INTR::getIntpAddr(i_ex, i);
            intr_prop[numThreads][1] = 0x1000;
            numThreads++;
        }
    }
    i_dt->addPropertyCells64(intNode, "reg",
                             reinterpret_cast<uint64_t*>(intr_prop),
                             numThreads * 2);

    uint32_t int_serv[2] = { i_pir.word, numThreads};
    i_dt->addPropertyCells32(intNode, "ibm,interrupt-server-ranges",
                             int_serv, 2);

    return i_dt->getPhandle(intNode);
}


void add_reserved_mem(devTree * i_dt,
                      std::vector<uint64_t>& i_homerAddr,
                      uint64_t i_extraAddr[],
                      uint64_t i_extraSize[],
                      const char* i_extraStr[],
                      uint64_t i_extraCnt)
{
    /*
     * The reserved-names and reserve-names properties work hand in hand.
     * The first one is a list of strings providing a "name" for each entry
     * in the second one using the traditional "vendor,name" format.
     *
     * The reserved-ranges property contains a list of ranges, each in the
     * form of 2 cells of address and 2 cells of size (64-bit x2 so each
     * entry is 4 cells) indicating regions of memory that are reserved
     * and must not be overwritten by skiboot or subsequently by the Linux
     * Kernel.
     *
     * Corresponding entries must also be created in the "reserved map" part
     * of the flat device-tree (which is a binary list in the header of the
     * fdt).
     *
     * Unless a component (skiboot or Linux) specifically knows about a region
     * (usually based on its name) and decides to change or remove it, all
     * these regions are passed as-is to Linux and to subsequent kernels
     * across kexec and are kept preserved.
     */

    dtOffset_t rootNode = i_dt->findNode("/");

    size_t l_num = i_homerAddr.size();

    const char* homerStr = "ibm,slw-occ-image";
    const char* reserve_strs[l_num+i_extraCnt+1];
    uint64_t ranges[l_num+i_extraCnt][2];
    uint64_t cell_count = sizeof(ranges) / sizeof(uint64_t);
    uint64_t res_mem_addrs[l_num+i_extraCnt];
    uint64_t res_mem_sizes[l_num+i_extraCnt];

    for(size_t i = 0; i<l_num; i++)
    {
        reserve_strs[i] = homerStr;
        ranges[i][0] = i_homerAddr[i];
        ranges[i][1] = VMM_HOMER_INSTANCE_SIZE;
        res_mem_addrs[i] = i_homerAddr[i];
        res_mem_sizes[i] = VMM_HOMER_INSTANCE_SIZE;
    }

    for(size_t i = 0; i < i_extraCnt; i++)
    {
        if (i_extraAddr[i])
        {
            TRACFCOMP( g_trac_devtree, "Reserved Region %s @ %lx, %lx",
                       i_extraStr[i], i_extraAddr[i], i_extraSize[i]);

            reserve_strs[l_num] = i_extraStr[i];
            ranges[l_num][0] = i_extraAddr[i];
            ranges[l_num][1] = i_extraSize[i];

            res_mem_addrs[l_num] = i_extraAddr[i];
            res_mem_sizes[l_num] = i_extraSize[i];

            l_num++;
        }
        else
        {
            cell_count -= sizeof(ranges[0]);
        }
    }
    reserve_strs[l_num] = NULL;

    i_dt->addPropertyStrings(rootNode, "reserved-names", reserve_strs);
    i_dt->addPropertyCells64(rootNode, "reserved-ranges",
                             reinterpret_cast<uint64_t*>(ranges),
                             cell_count);

    // added per comment from Dean Sanner
    // cell_count has limit of DT_MAX_MEM_RESERVE  = 16. Is this enough
    // for all processors + 1 vpd area + 1 target area?
    i_dt->populateReservedMem(res_mem_addrs, res_mem_sizes, cell_count);
}

void load_hbrt_image(uint64_t& io_address)
{
    errlHndl_t l_errl = NULL;

    do
    {

        PNOR::SectionInfo_t l_pnorInfo;
        l_errl = getSectionInfo( PNOR::HB_RUNTIME , l_pnorInfo);
        if (l_errl) { break; }

            // Find start of image.
            //     For Secureboot we might need to deal with the header but
            //     for now that is hidden by the PNOR-RP.
        uint64_t image_start = l_pnorInfo.vaddr;

            // The "VFS_LAST_ADDRESS" variable is 2 pages in.
        uint64_t vfs_last_address =
                *reinterpret_cast<uint64_t*>(image_start + 2*PAGE_SIZE);

            // At the end of the image are the relocations, get the number.
        uint64_t relocate_count =
                *reinterpret_cast<uint64_t*>(image_start + vfs_last_address);

            // Sum up the total size.
        uint64_t image_size = vfs_last_address +
                              (relocate_count+1)*sizeof(uint64_t);

        TRACFCOMP(g_trac_devtree, "HBRT image: start = %lx, size = %lx",
                  image_start, image_size);
        io_address -= ALIGN_PAGE(image_size);
        // Align to 64KB for Opal
        io_address = ALIGN_DOWN_X(io_address,64*KILOBYTE);

            // Copy image.
        void* memArea = mm_block_map(reinterpret_cast<void*>(io_address),
                                     ALIGN_PAGE(image_size));
        memcpy(memArea, reinterpret_cast<void*>(image_start), image_size);
        mm_block_unmap(memArea);

    } while (0);

    if (l_errl)
    {
        io_address = 0;
        errlCommit(l_errl, DEVTREE_COMP_ID);
    }
}


errlHndl_t bld_fdt_system(devTree * i_dt, bool i_smallTree)
{
    errlHndl_t errhdl = NULL;

    dtOffset_t rootNode = i_dt->findNode("/");

    //Common settings
    /* Define supported power states -- options:
                         nap, deep-sleep, fast-sleep, rvwinkle*/
    const char* pmode_compatStrs[] = {"nap", "fast-sleep", "rvwinkle", NULL};
    i_dt->addPropertyStrings(rootNode, "ibm,enabled-idle-states",
                             pmode_compatStrs);

    // Nothing to do for small trees currently.
    if (!i_smallTree)
    {
        //===== compatible =====
        /* Fetch the MRW-defined compatible model from attributes */
        ATTR_OPAL_MODEL_type l_model = {0};
        TARGETING::Target* sys = NULL;
        TARGETING::targetService().getTopLevelTarget(sys);
        sys->tryGetAttr<TARGETING::ATTR_OPAL_MODEL>(l_model);

        /* Add compatibility value */
        const char* l_compats[] = { "ibm,powernv", l_model, NULL };
        i_dt->addPropertyStrings(rootNode, "compatible", l_compats);

        //===== model =====
        /* Add system model value
           Depending on the vintage of the planar VPD, there are 3 places
           we need to look for this data.
           1) OSYS:MM
           2) OPFR:DR
           3) Default to 'unknown'
         */
        bool foundvpd = false;
        // TODO RTC 118373 -- update to account for firestone/memory riser
        TARGETING::TargetHandleList l_membTargetList;
        getAllChips(l_membTargetList, TYPE_MEMBUF);

        //if can't find a centaur for the CVPD, default to unknown
        if (l_membTargetList.size())
        {
            TARGETING::Target * l_pMem = l_membTargetList[0];
            size_t vpdSize = 0x0;

            // Note: First read with NULL for o_buffer sets vpdSize to the
            // correct length
            errhdl = deviceRead( l_pMem,
                                 NULL,
                                 vpdSize,
                                 DEVICE_CVPD_ADDRESS( CVPD::OSYS,
                                                      CVPD::MM ));

            if(errhdl)
            {
                TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't get OSYS:MM size for HUID=0x%.8X",
                          TARGETING::get_huid(l_pMem));

                // Try the OPFR record
                errlHndl_t opfr_errhdl = deviceRead( l_pMem,
                                           NULL,
                                           vpdSize,
                                           DEVICE_CVPD_ADDRESS( CVPD::OPFR,
                                                                CVPD::DR ));
                if(opfr_errhdl)
                {
                    TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't get OPFR:DR size for HUID=0x%.8X",
                              TARGETING::get_huid(l_pMem));
                    delete opfr_errhdl; //delete OPFR log, VPD is just bad
                }
                else
                {
                    delete errhdl; //ignore lack of OSYS due to older vpd
                    errhdl = NULL;
                    char drBuf[vpdSize+1];
                    memset(&drBuf, 0x0, (vpdSize+1)); //null terminated str
                    errhdl = deviceRead( l_pMem,
                                         reinterpret_cast<void*>( &drBuf ),
                                         vpdSize,
                                         DEVICE_CVPD_ADDRESS( CVPD::OPFR,
                                                              CVPD::DR ));

                    if(errhdl)
                    {
                        TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't read OPFR:DR for HUID=0x%.8X",
                                  TARGETING::get_huid(l_pMem));
                    }
                    else
                    {
                        foundvpd = true;
                        i_dt->addPropertyString(rootNode, "model", drBuf);
                    }
                }
            }
            else
            {
                char mmBuf[vpdSize+1];
                memset(&mmBuf, 0x0, (vpdSize+1)); //ensure null terminated str
                errhdl = deviceRead( l_pMem,
                                     reinterpret_cast<void*>( &mmBuf ),
                                     vpdSize,
                                     DEVICE_CVPD_ADDRESS( CVPD::OSYS,
                                                          CVPD::MM ));

                if(errhdl)
                {
                    TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't read OSYS:MM for HUID=0x%.8X",
                              TARGETING::get_huid(l_pMem));
                }
                else
                {
                    foundvpd = true;
                    i_dt->addPropertyString(rootNode, "model", mmBuf);
                }
            }
        }

        // just commit any errors we get, this isn't critical
        if( errhdl )
        {
            errlCommit(errhdl, DEVTREE_COMP_ID); //commit original OSYS log
        }

        if( !foundvpd ) //chassis info not found, default to unknown
        {
            TRACFCOMP(g_trac_devtree,ERR_MRK" VPD not found, model defaulted to unknown");
            i_dt->addPropertyString(rootNode, "model", "unknown");
        }

        //===== system-id =====
        /* Add system-id value
           1) OSYS:SS
           2) Default to 'unavailable'
         */
        // TODO RTC 118373 -- update to account for firestone/memory riser
        foundvpd = false;
        if( l_membTargetList.size() )
        {
            // TODO RTC 118373 - Should be able to read from attribute
            TARGETING::Target * l_pMem = l_membTargetList[0];
            size_t vpdSize = 0x0;

            // Note: First read with NULL for o_buffer sets vpdSize to the
            // correct length
            errhdl = deviceRead( l_pMem,
                                 NULL,
                                 vpdSize,
                                 DEVICE_CVPD_ADDRESS( CVPD::OSYS,
                                                      CVPD::SS ));

            if(errhdl)
            {
                TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't get OSYS:SS size for HUID=0x%.8X",
                          TARGETING::get_huid(l_pMem));
                // Note - not supporting old vpd versions without OSYS here
            }
            else
            {
                char ssBuf[vpdSize+1];
                memset(&ssBuf, 0x0, (vpdSize+1)); //ensure null terminated str
                errhdl = deviceRead( l_pMem,
                                     reinterpret_cast<void*>( &ssBuf ),
                                     vpdSize,
                                     DEVICE_CVPD_ADDRESS( CVPD::OSYS,
                                                          CVPD::SS ));

                if(errhdl)
                {
                    TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't read OSYS:SS for HUID=0x%.8X",
                              TARGETING::get_huid(l_pMem));
                }
                else
                {
                    foundvpd = true;
                    i_dt->addPropertyString(rootNode, "system-id", ssBuf);
                }
            }
        }
        // just delete any errors we get, this isn't critical
        if( errhdl )
        {
            // since there are old parts out in the wild without
            //  this data, we can't log an error
            delete errhdl;
            errhdl = NULL;
        }

        if( !foundvpd ) //serial number not found, default to unavailable
        {
            i_dt->addPropertyString(rootNode, "system-id", "unavailable");
        }
    }

    return errhdl;
}


errlHndl_t bld_fdt_cpu(devTree * i_dt,
                       std::vector<uint64_t>& o_homerRegions,
                       bool i_smallTree)
{
    // Nothing to do for small trees currently.
    if (i_smallTree) { return NULL; }

    errlHndl_t errhdl = NULL;

    /* Find the / node and add a cpus node under it. */
    dtOffset_t rootNode = i_dt->findNode("/");
    dtOffset_t cpusNode = i_dt->addNode(rootNode, "cpus");

    /* Add the # address & size cell properties to /cpus. */
    i_dt->addPropertyCell32(cpusNode, "#address-cells", 1);
    i_dt->addPropertyCell32(cpusNode, "#size-cells", 0);

    // Get all functional proc chip targets
    TARGETING::TargetHandleList l_procTargetList;
    getAllChips(l_procTargetList, TYPE_PROC);

    for (size_t proc = 0; (!errhdl) && (proc < l_procTargetList.size()); proc++)
    {
        const TARGETING::Target * l_pProc = l_procTargetList[proc];

        uint32_t l_chipid = getProcChipId(l_pProc);

        bld_xscom_node(i_dt, rootNode, l_pProc, l_chipid);

        //Each processor will have a HOMER image that needs
        //to be reserved -- save it away
        o_homerRegions.push_back(getHomerPhysAddr(l_pProc));

        TARGETING::TargetHandleList l_exlist;
        getChildChiplets( l_exlist, l_pProc, TYPE_CORE );
        for (size_t core = 0; core < l_exlist.size(); core++)
        {
            const TARGETING::Target * l_ex = l_exlist[core];
            if(l_ex->getAttr<TARGETING::ATTR_HWAS_STATE>().functional != true)
            {
                continue; //Not functional
            }

            /* Proc ID Reg is N NNCC CPPP PTTT Where
                               NNN           is node number
                                  CCC        is Chip
                                     PPPP    is the core number
                                         TTT is Thread num
             */
            uint32_t l_coreNum = l_ex->getAttr<TARGETING::ATTR_CHIP_UNIT>();
            INTR::PIR_t pir(0);
            pir.nodeId = CHIPID_EXTRACT_NODE(l_chipid);
            pir.chipId = CHIPID_EXTRACT_PROC(l_chipid);
            pir.coreId = l_coreNum;

            TRACFCOMP( g_trac_devtree, "Added pir[%x] chipid 0x%x core %d",
                       pir.word, l_chipid, l_coreNum );

            cpusNode = i_dt->findNode("/cpus");

            uint32_t l3pHandle = bld_l3_node(i_dt, cpusNode, pir.word);
            uint32_t l2pHandle = bld_l2_node(i_dt, cpusNode, pir.word,
                                             l3pHandle);
            bld_cpu_node(i_dt, cpusNode, l_ex, pir, l_chipid, l2pHandle);

            rootNode = i_dt->findNode("/");
            bld_intr_node(i_dt, rootNode, l_ex, pir);
        }
    }

    return errhdl;
}

errlHndl_t bld_fdt_reserved_mem(devTree * i_dt,
                                std::vector<uint64_t>& i_homerRegions,
                                bool i_smallTree)
{
    errlHndl_t errhdl = NULL;

    // VPD
    uint64_t l_vpd_addr = 0;

    errhdl = VPD::vpd_load_rt_image(l_vpd_addr);

    // Targeting
    uint64_t l_targ_addr = l_vpd_addr;
    TARGETING::AttrRP::save(l_targ_addr);

    // HBRT image
    uint64_t l_hbrt_addr = l_targ_addr;
    load_hbrt_image(l_hbrt_addr);

    uint64_t l_extra_addrs[] = { l_vpd_addr, l_targ_addr, l_hbrt_addr };
    uint64_t l_extra_sizes[] = { VMM_RT_VPD_SIZE,
                                 l_vpd_addr - l_targ_addr,
                                 l_targ_addr - l_hbrt_addr};
    const char* l_extra_addrs_str[] =
                    { "ibm,hbrt-vpd-image" ,
                      "ibm,hbrt-target-image",
                      "ibm,hbrt-code-image" };
    size_t l_extra_addr_cnt = sizeof(l_extra_addrs) / sizeof(uint64_t);

    //Add in reserved memory for HOMER images and HBRT sections.
    add_reserved_mem(i_dt,
                     i_homerRegions,
                     l_extra_addrs,
                     l_extra_sizes,
                     l_extra_addrs_str,
                     l_extra_addr_cnt);

    return errhdl;

}

errlHndl_t bld_fdt_mem(devTree * i_dt, bool i_smallTree)
{
    // Nothing to do for small trees currently.
    if (i_smallTree) { return NULL; }

    errlHndl_t errhdl = NULL;
    bool rc;

    /*
     * The "memory" nodes represent physical memory in the system. They
     * do not represent DIMMs, memory controllers or Centaurs, thus will
     * be expressed separately.
     *
     * In order to be able to handle affinity propertly, we require that
     * a memory node is created for each range of memory that has a different
     * "affinity", which in practice means for each chip since we don't
     * support memory interleaved across multiple chips on P8.
     *
     * Additionally, it is *not* required that one chip = one memory node,
     * it is perfectly acceptable to break down the memory of one chip into
     * multiple memory nodes (typically skiboot does that if the two MCs
     * are not interlaved).
     */

    do
    {
        /* Find the / node and add a memory node(s) under it. */
        dtOffset_t rootNode = i_dt->findNode("/");

        // Grab a system object to work with
        TARGETING::Target* sys = NULL;
        TARGETING::targetService().getTopLevelTarget(sys);

        // Get all functional proc chip targets
        TARGETING::TargetHandleList l_cpuTargetList;
        getAllChips(l_cpuTargetList, TYPE_PROC);

        for ( size_t proc = 0;
              (!errhdl) && (proc < l_cpuTargetList.size()); proc++ )
        {
            const TARGETING::Target * l_pProc = l_cpuTargetList[proc];

            uint64_t l_bases[8] = {0,};
            uint64_t l_sizes[8] = {0,};
            rc = l_pProc->tryGetAttr<TARGETING::ATTR_PROC_MEM_BASES>(l_bases);
            if(!rc)
            {
                /*@
                 * @errortype
                 * @reasoncode       DEVTREE::RC_ATTR_MEMBASE_GET_FAIL
                 * @moduleid         DEVTREE::MOD_DEVTREE_BLD_MEM
                 * @userdata1        Return code from ATTR_GET
                 * @userdata2        Attribute Id that failed
                 * @devdesc          Error retrieving attribute
                 */
                errhdl=new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               MOD_DEVTREE_BLD_MEM,
                                               RC_ATTR_MEMBASE_GET_FAIL,
                                               rc,
                                               ATTR_PROC_MEM_BASES);
                break;
            }

            rc = l_pProc->tryGetAttr<TARGETING::ATTR_PROC_MEM_SIZES>(l_sizes);
            if(!rc)
            {
                /*@
                 * @errortype
                 * @reasoncode       DEVTREE::RC_ATTR_MEMSIZE_GET_FAIL
                 * @moduleid         DEVTREE::MOD_DEVTREE_BLD_MEM
                 * @userdata1        Return code from ATTR_GET
                 * @userdata2        Attribute Id that failed
                 * @devdesc          Error retrieving attribute
                 */
                errhdl=new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               MOD_DEVTREE_BLD_MEM,
                                               RC_ATTR_MEMSIZE_GET_FAIL,
                                               rc,
                                               ATTR_PROC_MEM_SIZES);
                break;
            }

            for (size_t i=0; i< 8; i++)
            {
                if(l_sizes[i]) //non zero means that there is memory present
                {
                    dtOffset_t memNode = i_dt->addNode(rootNode, "memory",
                                                       l_bases[i]);
                    i_dt->addPropertyString(memNode, "device_type","memory");
                    uint64_t propertyCells[2] = {l_bases[i],l_sizes[i]};
                    i_dt->addPropertyCells64(memNode, "reg", propertyCells, 2);

                    //Add the attached proc chip for affinity
                    i_dt->addPropertyCell32(memNode, "ibm,chip-id",
                                            getProcChipId(l_pProc));

                }
            }
        }

        /***************************************************************/
        /* Now loop on all the centaurs in the system and add their    */
        /* inband scom address                                         */
        /***************************************************************/
        rootNode = i_dt->findNode("/");

        // Get all functional memb chip targets
        TARGETING::TargetHandleList l_memBufList;
        getAllChips(l_memBufList, TYPE_MEMBUF);

        for ( size_t memb = 0;
              (!errhdl) && (memb < l_memBufList.size()); memb++ )
        {
            TARGETING::Target * l_pMemB = l_memBufList[memb];

            //Get MMIO Offset from parent MCS attribute.
            PredicateCTM l_mcs(CLASS_UNIT,TYPE_MCS, MODEL_NA);

            TargetHandleList mcs_list;
            targetService().getAssociated(mcs_list,
                            l_pMemB,
                            TargetService::PARENT_BY_AFFINITY,
                            TargetService::ALL,
                            &l_mcs);

            if( mcs_list.size() != 1 )
            {
                //This error should have already been caught in
                //the inband Scom DD.... going to skip creating node
                //if true
                TRACFCOMP(g_trac_devtree,ERR_MRK" MCS for 0x%x not found",
                          TARGETING::get_huid(l_pMemB));
                continue;
            }
            Target* parentMCS = *(mcs_list.begin());
            uint64_t l_ibscomBase =
              parentMCS->getAttr<ATTR_IBSCOM_MCS_BASE_ADDR>();


            dtOffset_t membNode = i_dt->addNode(rootNode, "memory-buffer",
                                               l_ibscomBase);
            uint64_t propertyCells[2] = {l_ibscomBase,THIRTYTWO_GB};
            i_dt->addPropertyCells64(membNode, "reg", propertyCells, 2);
            i_dt->addPropertyCell32(membNode, "#address-cells", 1);
            i_dt->addPropertyCell32(membNode, "#size-cells", 1);

            uint32_t l_ec = l_pMemB->getAttr<ATTR_EC>();
            char cenVerStr[32];
            snprintf(cenVerStr, 32, "ibm,centaur-v%.2x", l_ec);
            const char* intr_compatStrs[] = {"ibm,centaur", cenVerStr,NULL};
            i_dt->addPropertyStrings(membNode, "compatible", intr_compatStrs);


            if(l_pMemB->
               getAttr<TARGETING::ATTR_SCOM_SWITCHES>().useInbandScom == 0x0)
            {
                i_dt->addProperty(membNode,"use-fsi");
            }

            //Add the attached proc chip for affinity
            uint32_t l_procId = getProcChipId(getParentChip(parentMCS));
            i_dt->addPropertyCell32(membNode, "ibm,fsi-master-chip-id",
                                    l_procId);

            uint32_t l_cenId = getMembChipId(l_pMemB);
            i_dt->addPropertyCell32(membNode, "ibm,chip-id",l_cenId);

            //Add the CMFSI (which CMFSI 0 or 1) and port
            FSI::FsiLinkInfo_t linkinfo;
            FSI::getFsiLinkInfo( l_pMemB, linkinfo );
            uint32_t cmfsiCells[2] =
                           {linkinfo.mPort,linkinfo.link};
            i_dt->addPropertyCells32(membNode, "ibm,fsi-master-port",
                                     cmfsiCells, 2);

            //Add any I2C devices hanging off this chip
            add_i2c_info( l_pMemB, i_dt, membNode );

            // Add membuf ECIDs
            ATTR_ECID_type ecid;
            l_pMemB->tryGetAttr<ATTR_ECID>(ecid);
            char ecid_ascii[33];
            sprintf(ecid_ascii, "%.16llX%.16llX", ecid[0], ecid[1]);
            i_dt->addPropertyString(membNode, "ecid", ecid_ascii);
            CPPASSERT(sizeof(ATTR_ECID_type) == 16);
        }


    }while(0);
    return errhdl;
}


#ifdef CONFIG_BMC_IPMI
enum
{
    ENTITY_ID_MASK      = 0x00FF,
    SENSOR_TYPE_MASK    = 0xFF00,
};

/* create a node for each IPMI sensor in the system, the sensor unit number
   corresponds to the BMC assigned sensor number */
uint32_t bld_sensor_node(devTree * i_dt, const dtOffset_t & i_parentNode,
                         const uint16_t sensorData[],
                         uint32_t instance, uint32_t chipId )
{

    SENSOR::sensorReadingType readType;

    // pass in the sensor name to get back the supported offsets and the event
    // reading type for this sensor.
    uint32_t offsets = SENSOR::getSensorOffsets(
             static_cast<TARGETING::SENSOR_NAME>(
                 sensorData[TARGETING::IPMI_SENSOR_ARRAY_NAME_OFFSET]),
             readType);

    const uint16_t sensorNumber = sensorData[
                        TARGETING::IPMI_SENSOR_ARRAY_NUMBER_OFFSET];

    // the sensor name is a combination of the sensor type + entity ID
    const uint16_t sensorType = (
        sensorData[TARGETING::IPMI_SENSOR_ARRAY_NAME_OFFSET]
                    & SENSOR_TYPE_MASK) >> 8;

    const uint16_t entityId =
        sensorData[TARGETING::IPMI_SENSOR_ARRAY_NAME_OFFSET] & ENTITY_ID_MASK;

    /* Build sensor node based on sensor number */
    dtOffset_t sensorNode = i_dt->addNode(i_parentNode, "sensor", sensorNumber);

    /* compatibility strings -- currently only one */
    const char* compatStr[] = {"ibm,ipmi-sensor", NULL};

    i_dt->addPropertyStrings(sensorNode, "compatible", compatStr);

    i_dt->addPropertyCell32(sensorNode, "reg", sensorNumber);

    // add sensor type
    i_dt->addPropertyCell32(sensorNode, "ipmi-sensor-type", sensorType);
    i_dt->addPropertyCell32(sensorNode, "ipmi-entity-id", entityId);
    i_dt->addPropertyCell32(sensorNode, "ipmi-entity-instance", instance);
    i_dt->addPropertyCell32(sensorNode, "ipmi-sensor-offsets", offsets);
    i_dt->addPropertyCell32(sensorNode, "ipmi-sensor-reading-type", readType);

    // currently we only add the chip ID to the OCC sensor
    if(chipId != 0xFF )
    {
        i_dt->addPropertyCell32(sensorNode, "ibm,chip-id", chipId);
    }

    /* return the phandle for this sensor */
    return i_dt->getPhandle(sensorNode);
}


// build the sensor node for a given target
uint32_t bld_sensor_node(devTree * i_dt, const dtOffset_t & i_sensorNode,
        TARGETING::Target * i_pTarget )
{

    AttributeTraits<ATTR_IPMI_SENSORS>::Type  l_sensors;
    uint16_t array_rows = (sizeof(l_sensors)/sizeof(l_sensors[0]));

    /* if there is an IPMI_SENSORS attribute, parse it and create a node
     * for each sensor */
    if ( i_pTarget->tryGetAttr<ATTR_IPMI_SENSORS>(l_sensors) )
    {
        uint32_t chipId = 0xFF;

        AttributeTraits<ATTR_IPMI_INSTANCE>::Type l_instance;

        l_instance = i_pTarget->getAttr<TARGETING::ATTR_IPMI_INSTANCE>();

        // add the chip id to the OCC sensor since OPAL needs it to figure out
        // which OCC it is.
        if( TARGETING::TYPE_OCC == i_pTarget->getAttr<TARGETING::ATTR_TYPE>())
        {
            ConstTargetHandle_t proc = getParentChip(i_pTarget);

            chipId = getProcChipId( proc );
        }

        for(uint16_t i=0; i< array_rows; i++)
        {
            /*  if the sensor number is 0xFF move on */
            if( l_sensors[i][IPMI_SENSOR_ARRAY_NUMBER_OFFSET] != 0xFF )
            {
                /* use this row to create the next sensor node - ignoring
                 * return value for now */
                bld_sensor_node(i_dt, i_sensorNode, l_sensors[i],
                        l_instance , chipId );
            }
            else
            {
                /* move on to the next target */
                break;
            }
        }
    }

    // return the phandle
    return i_dt->getPhandle(i_sensorNode);
}


/*
* The "sensors" node contains sub-nodes for each of the IPMI sensors known to
* the BMC.
*/
errlHndl_t bld_fdt_sensors(devTree * i_dt, const dtOffset_t & i_parentNode,
                            const bool i_smallTree)
{
    errlHndl_t errhdl = NULL;

    /* Nothing to do for small trees currently. */
    if (i_smallTree) { return NULL; }

    const char* sensorNodeName = "sensors";

    /* add the Sensors node to the BMC node */
    dtOffset_t sensorNode = i_dt->addNode(i_parentNode, sensorNodeName);

    i_dt->addPropertyString(sensorNode, "name", sensorNodeName );

    /* Add the # address & size cell properties to /sensors node. */
    i_dt->addPropertyCell32(sensorNode, "#address-cells", 1);
    i_dt->addPropertyCell32(sensorNode, "#size-cells", 0);

    /*  loop through all the targets and get the IPMI sensor data if it
        exists */
    for (TargetIterator itr = TARGETING::targetService().begin();
         itr != TARGETING::targetService().end(); ++itr)
    {
        /* create node entries for this targets sensors if they exist
        *  ignoring return value for now */
        bld_sensor_node(i_dt, sensorNode, *itr );
    }

    return errhdl;
}

/* add the BMC node to the device tree, this node will hold any BMC info needed
   in the device tree */
errlHndl_t bld_fdt_bmc(devTree * i_dt, bool i_smallTree)
{
    errlHndl_t errhdl = NULL;

    /* Nothing to do for small trees currently. */
    if (i_smallTree) { return NULL; }

    /* Find the root node. */
    dtOffset_t rootNode = i_dt->findNode("/");

    const char* bmcNodeName = "bmc";

    /* add the BMC node under the root node */
    dtOffset_t bmcNode = i_dt->addNode(rootNode, bmcNodeName);

    /* Add the # address & size cell properties to /bmc node. */
    i_dt->addPropertyCell32(bmcNode, "#address-cells", 1);
    i_dt->addPropertyCell32(bmcNode, "#size-cells", 0);

    i_dt->addPropertyString(bmcNode, "name", bmcNodeName );

    //Pass Opal their device string
    // find CLASS_SYS (the top level target)
    TARGETING::Target* pSys;
    TARGETING::targetService().getTopLevelTarget(pSys);
    assert(pSys != NULL,
               "bld_fdt_bmc - Error: Could not find the top level target.");
    i_dt->addPropertyCell32(bmcNode, "firmware-fru-id",
                                   pSys->getAttr<TARGETING::ATTR_FRU_ID>());

    /* create a node to hold the sensors */
    errhdl = bld_fdt_sensors( i_dt, bmcNode, i_smallTree );

    return errhdl;
}
#endif

errlHndl_t bld_fdt_vpd(devTree * i_dt, bool i_smallTree)
{
    // Nothing to do for small trees currently.
    if (i_smallTree) { return NULL; }

    errlHndl_t errhdl = NULL;
    size_t vpdSize;

    do
    {
        /* Find the / node and add a vpd node under it. */
        dtOffset_t rootNode = i_dt->findNode("/");
        dtOffset_t vpdNode = i_dt->addNode(rootNode, "vpd");

        // Grab a system object to work with
        TARGETING::Target* sys = NULL;
        TARGETING::targetService().getTopLevelTarget(sys);


        /***************************************************************/
        /* Add the ibm,vpd for all functional procs                    */
        /***************************************************************/
        // Add vpd (VINI record) for all functional procs
        // and #V for all functional cores
        TARGETING::TargetHandleList l_cpuTargetList;
        getAllChips(l_cpuTargetList, TYPE_PROC);

        for ( size_t proc = 0;
              (!errhdl) && (proc < l_cpuTargetList.size()); proc++ )
        {
            TARGETING::Target * l_pProc = l_cpuTargetList[proc];

            uint32_t l_procId = getProcChipId(l_pProc);
            dtOffset_t procNode = i_dt->addNode(vpdNode, "processor",
                                               l_procId);

            // Read entire VINI record to stuff in devtree
            // Note: First read with NULL for o_buffer sets vpdSize to the
            // correct length
            errhdl = deviceRead( l_pProc,
                              NULL,
                              vpdSize,
                              DEVICE_MVPD_ADDRESS( MVPD::VINI,
                                                   MVPD::FULL_RECORD ));

            if(errhdl)
            {
                TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't get VINI size for HUID=0x%.8X",
                          TARGETING::get_huid(l_pProc));
                break;
            }

            uint8_t viniBuf[vpdSize];

            errhdl = deviceRead( l_pProc,
                              reinterpret_cast<void*>( &viniBuf ),
                              vpdSize,
                              DEVICE_MVPD_ADDRESS( MVPD::VINI,
                                                   MVPD::FULL_RECORD ));

            if(errhdl)
            {
                TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't read VINI for HUID=0x%.8X",
                          TARGETING::get_huid(l_pProc));
                break;
            }

            //Add the proc chips vpd
            i_dt->addPropertyBytes(procNode, "ibm,vpd", viniBuf, vpdSize);

            /***************************************************************/
            /* Add the #V bucket for each functional core                  */
            /***************************************************************/
            TARGETING::TargetHandleList l_exlist;
            getChildChiplets( l_exlist, l_pProc, TYPE_CORE );
            for (size_t core = 0; core < l_exlist.size(); core++)
            {
                const TARGETING::Target * l_ex = l_exlist[core];

                uint32_t l_coreNum = l_ex->getAttr<TARGETING::ATTR_CHIP_UNIT>();
                INTR::PIR_t pir(0);
                pir.nodeId = CHIPID_EXTRACT_NODE(l_procId);
                pir.chipId = CHIPID_EXTRACT_PROC(l_procId);
                pir.coreId = l_coreNum;

                // Get #V bucket data
                uint32_t l_record = (uint32_t) MVPD::LRP0 + l_coreNum;
                fapi::voltageBucketData_t l_poundVdata = {0};
                fapi::ReturnCode l_rc = fapiGetPoundVBucketData(l_pProc,
                                                                l_record,
                                                                l_poundVdata);
                if(l_rc)
                {
                    TRACFCOMP( g_trac_devtree,ERR_MRK"Error getting #V data for HUID:"
                               "0x%08X",
                               l_pProc->getAttr<TARGETING::ATTR_HUID>());

                    // Convert fapi returnCode to Error handle
                    errhdl = fapiRcToErrl(l_rc);
                    break;
                }

                //Add the attached core
                dtOffset_t exNode = i_dt->addNode(procNode, "cpu",
                                                    pir.word);

                i_dt->addPropertyBytes(exNode, "frequency,voltage",
                                     reinterpret_cast<uint8_t*>( &l_poundVdata),
                                     sizeof(fapi::voltageBucketData_t));
            }
            if(errhdl)
            {
                break;
            }
        }
        if(errhdl)
        {
            break;
        }

#if 0   //TODO RTC123250 -- re-enable once fixed
        /***************************************************************/
        /* Now loop on all the dimms in the system and add their spd   */
        /***************************************************************/

        // Get all functional dimm targets
        TARGETING::TargetHandleList l_dimmList;
        getAllLogicalCards(l_dimmList, TYPE_DIMM);
        size_t spdSize;

        for ( size_t dimm = 0;
              (!errhdl) && (dimm < l_dimmList.size()); dimm++ )
        {
            TARGETING::Target * l_pDimm = l_dimmList[dimm];
            uint32_t l_huid = TARGETING::get_huid(l_pDimm);

            dtOffset_t dimmNode = i_dt->addNode(vpdNode, "dimm",
                                                l_huid);

            // Read entire SPD record to stuff in devtree
            // Note: First read with NULL for o_buffer sets spdSize to the
            // correct length
            errhdl = deviceRead( l_pDimm,
                                 NULL,
                                 spdSize,
                                 DEVICE_SPD_ADDRESS(SPD::ENTIRE_SPD));

            if(errhdl)
            {
                TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't get SPD size for HUID=0x%.8X",
                          TARGETING::get_huid(l_pDimm));
                break;
            }

            uint8_t spdBuf[spdSize];

            errhdl = deviceRead( l_pDimm,
                                 reinterpret_cast<void*>( &spdBuf ),
                                 spdSize,
                                 DEVICE_SPD_ADDRESS(SPD::ENTIRE_SPD));

            if(errhdl)
            {
                TRACFCOMP(g_trac_devtree,ERR_MRK" Couldn't read SPD for HUID=0x%.8X",
                          TARGETING::get_huid(l_pDimm));
                break;
            }

            //Add the dimm spd
            i_dt->addPropertyBytes(dimmNode, "spd", spdBuf, spdSize);
        }
        if(errhdl)
        {
            break;
        }
#endif

    }while(0);

    return errhdl;
}

errlHndl_t build_flatdevtree( uint64_t i_dtAddr, size_t i_dtSize,
                              bool i_smallTree )
{
    errlHndl_t errhdl = NULL;
    devTree * dt = &Singleton<devTree>::instance();
    bool devTreeVirtual = true;

    do
    {
        if (0 == i_dtAddr)
        {
            i_dtAddr = DEVTREE_DATA_ADDR;
            i_dtSize = DEVTREE_SPACE_SIZE;
            devTreeVirtual = false;
        }

        TRACFCOMP( g_trac_devtree, "---devtree init---" );
        dt->initialize(i_dtAddr, i_dtSize, devTreeVirtual);
        errhdl = bld_fdt_system(dt, i_smallTree);
        if (errhdl)
        {
            break;
        }

        std::vector<uint64_t> l_homerRegions;

        TRACFCOMP( g_trac_devtree, "---devtree cpu ---" );
        errhdl = bld_fdt_cpu(dt, l_homerRegions, i_smallTree);
        if(errhdl)
        {
            break;
        }

#ifndef CONFIG_DISABLE_HOSTBOOT_RUNTIME
        TRACFCOMP( g_trac_devtree, "---devtree reserved mem ---" );
        errhdl = bld_fdt_reserved_mem(dt, l_homerRegions, i_smallTree);
        if(errhdl)
        {
            break;
        }
#endif
        TRACFCOMP( g_trac_devtree, "---devtree mem ---" );
        errhdl = bld_fdt_mem(dt, i_smallTree);
        if(errhdl)
        {
            break;
        }

#ifdef CONFIG_BMC_IPMI
        TRACFCOMP( g_trac_devtree, "---devtree BMC ---" );
        errhdl = bld_fdt_bmc(dt, i_smallTree);
        if(errhdl)
        {
            break;
        }
#endif

        TRACFCOMP( g_trac_devtree, "---devtree vpd ---" );
        errhdl = bld_fdt_vpd(dt, i_smallTree);
        if(errhdl)
        {
            break;
        }
    }while(0);

    return errhdl;
}


uint64_t get_flatdevtree_phys_addr()
{
    return Singleton<devTree>::instance().getBlobPhys();
}

}
OpenPOWER on IntegriCloud