summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/prdf/common/plat/prdfTargetServices.C
blob: 3892a7c459b1bef6afd71c4ade5837be3872fe5a (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/prdf/common/plat/prdfTargetServices.C $          */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2018                        */
/* [+] 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 prdfTargetServices.C
 * @brief PRD wrapper of targeting code
 */

//------------------------------------------------------------------------------
//  Includes
//------------------------------------------------------------------------------

#include <prdfTargetServices.H>

// Framework includes
#include <iipServiceDataCollector.h>
#include <iipSystem.h>
#include <prdfAssert.h>
#include <prdfErrlUtil.H>
#include <prdfExtensibleChip.H>
#include <prdfGlobal.H>
#include <prdfTrace.H>
#include <xspprdService.h>

// External includes
#include <algorithm>
#include <targeting/common/targetservice.H>
#include <targeting/common/utilFilter.H>

// Platform includes
#include <prdfMemAddress.H>

using namespace TARGETING;

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

namespace PRDF
{

namespace PlatServices
{

//##############################################################################
//##
//##                     System Level Utility Functions
//##
//##############################################################################

// local utility function.
ATTR_PAYLOAD_KIND_type getPayloadType()
{
    return getSystemTarget()->getAttr<ATTR_PAYLOAD_KIND>();
}

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

bool isHyprConfigPhyp()
{
    return PAYLOAD_KIND_PHYP == getPayloadType();
}

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

bool isHyprConfigOpal()
{
    return PAYLOAD_KIND_SAPPHIRE == getPayloadType();
}

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

bool isHyprRunning()
{
    bool rc = false;

    #ifdef __HOSTBOOT_MODULE

        // ATTR_PAYLOAD_STATE is not defined in Hostboot. We can assume that if
        // __HOSTBOOT_RUNTIME is defined then the hypervisor is running.

        #ifdef __HOSTBOOT_RUNTIME
        rc = true;
        #else
        rc = false;
        #endif

    #else

    TargetHandle_t sysTrgt = getSystemTarget();
    rc = (PAYLOAD_STATE_RUNNING == sysTrgt->getAttr<ATTR_PAYLOAD_STATE>());

    #endif

    return rc;
}

bool hasRedundantClocks()
{
    return ( 0 != getSystemTarget()->getAttr<ATTR_REDUNDANT_CLOCKS>() );
}

//##############################################################################
//##
//##                         General Utility Functions
//##
//##############################################################################

void hwpErrorIsolation( ExtensibleChip * i_chip, STEP_CODE_DATA_STRUCT & io_sc )
{
    #if defined (__HOSTBOOT_MODULE) && !defined(__HOSTBOOT_RUNTIME)

    TargetHandle_t trgt = i_chip->getTrgt();
    uint32_t plid = 0;

    // Check for non-zero value in PLID attribute.
    if ( trgt->tryGetAttr<ATTR_PRD_HWP_PLID>(plid) && (0 != plid) )
    {
        PRDF_INF( "ATTR_PRD_HWP_PLID found on 0x%08x with value 0x%08x",
                  getHuid(trgt), plid );

        // Link HWP PLID to PRD error log.
        ServiceGeneratorClass::ThisServiceGenerator().getErrl()->plid( plid );

        // Clear PRD_HWP_PLID attribute.
        trgt->setAttr<ATTR_PRD_HWP_PLID>( 0 );

        // Make the error log and callouts predictive.
        io_sc.service_data->setServiceCall();
    }

    #endif
}

//##############################################################################
//##
//##                 Target Manipulation Utility Functions
//##
//##############################################################################

// FIXME: RTC 62867
//        This function is using type PRDF::HUID. I think it should now be using
//        TARGETING::HUID_ATTR. Also, will need equivalent to
//        PRDF::INVALID_HUID. I think HWSV has HWSV_INVALID_HUID, but I don't
//        think that exists in Hostboot. Need a common interface before making
//        changes.
TARGETING::TargetHandle_t getTarget( HUID i_huid )
{
    TargetHandle_t o_target = NULL;

    // FIXME: RTC 62867
    //        This is an incredibly inefficient linear search. It is recommended
    //        that the common targeting code provide an interface for us so that
    //        all users can call the potentially optimized function. There is a
    //        function available in HWSV (hwsvTargetUtil.H) but not in Hostboot.
    //        Sadly, the HWSV code does this exact linear search.
    TargetService & l_targetService = targetService();
    for ( TargetIterator l_targetPtr = l_targetService.begin();
          l_targetPtr != l_targetService.end(); ++l_targetPtr )
    {
        if ( i_huid == (l_targetPtr->getAttr<ATTR_HUID>()) )
        {
            o_target = (*l_targetPtr);
            break;
        }
    }

    if ( NULL == o_target )
    {
        PRDF_ERR( "[getTarget] i_huid: 0x%08x failed", i_huid );
    }

    return o_target;
}

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

TARGETING::TargetHandle_t getTarget( const TARGETING::EntityPath & i_path )
{
    TargetHandle_t o_target = targetService().toTarget( i_path );
    if ( NULL == o_target )
    {
        PRDF_ERR( "[getTarget] Failed: i_path = " ); i_path.dump();
    }

    return o_target;
}

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

int32_t getEntityPath( TARGETING::TargetHandle_t i_target,
                       TARGETING::EntityPath & o_path,
                       TARGETING::EntityPath::PATH_TYPE i_pathType )
{
    int32_t o_rc = FAIL;

    do
    {
        if ( NULL == i_target ) break;

        if ( EntityPath::PATH_NA != i_pathType )
            o_path.setType( i_pathType );

        ATTRIBUTE_ID attr = ATTR_NA;
        switch ( o_path.type() )
        {
            case EntityPath::PATH_AFFINITY: attr = ATTR_AFFINITY_PATH; break;
            case EntityPath::PATH_PHYSICAL: attr = ATTR_PHYS_PATH;     break;
            case EntityPath::PATH_POWER:    attr = ATTR_POWER_PATH;    break;
            default: ;
        }
        if ( ATTR_NA == attr )
        {
            PRDF_ERR( "[getEntityPath] Unsupported EntityPath type %d",
                      o_path.type() );
            break;
        }

        if ( !targetService().tryGetPath(attr, i_target, o_path) )
        {
            PRDF_ERR( "[getEntityPath] Failed to get path %d", attr );
            break;
        }

        o_rc = SUCCESS;

    } while (0);

    if ( SUCCESS != o_rc )
    {
        PRDF_ERR( "[getEntityPath] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_rc;
}

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

HUID getHuid( TARGETING::TargetHandle_t i_target )
{
    HUID o_huid = INVALID_HUID;

    do
    {
        if ( NULL == i_target ) break; // return INVALID_HUID

        if ( !i_target->tryGetAttr<ATTR_HUID>(o_huid) )
        {
            PRDF_ERR( "[getHuid] Failed to get ATTR_HUID" );
            o_huid = INVALID_HUID; // Just in case.
        }

    } while (0);

    return o_huid;
}

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

bool isFunctional( TARGETING::TargetHandle_t i_target )
{
    bool o_funcState = false;

    do
    {
        if ( NULL == i_target )
        {
            PRDF_ERR( "[isFunctional] i_target is NULL" );
            break;
        }

        HwasState l_funcState;
        if ( !i_target->tryGetAttr<ATTR_HWAS_STATE>(l_funcState) )
        {
            PRDF_ERR( "[isFunctional] Failed to get ATTR_HWAS_STATE" );
            break;
        }

        if ( l_funcState.functional ) o_funcState =true;
    } while (0);

    return o_funcState;
}

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

TARGETING::TYPE getTargetType( TARGETING::TargetHandle_t i_target )
{
    TYPE o_type = TYPE_LAST_IN_RANGE;

    if ( NULL != i_target )
    {
        if ( !i_target->tryGetAttr<ATTR_TYPE>(o_type) )
        {
            PRDF_ERR( "[getTargetType] Failed to get ATTR_TYPE" );
            o_type = TYPE_LAST_IN_RANGE; // Just in case
        }
    }

    if ( TYPE_LAST_IN_RANGE == o_type )
    {
        PRDF_ERR( "[getTargetType] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_type;
}

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

TARGETING::CLASS getTargetClass( TARGETING::TargetHandle_t i_target )
{
    CLASS o_class = CLASS_NA;

    if ( NULL != i_target )
    {
        if ( !i_target->tryGetAttr<ATTR_CLASS>(o_class) )
        {
            PRDF_ERR( "[getTargetClass] Failed to get ATTR_CLASS" );
            o_class = CLASS_NA; // Just in case
        }
    }

    if ( CLASS_NA == o_class )
    {
        PRDF_ERR( "[getTargetClass] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_class;
}

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

TARGETING::MODEL getChipModel( TARGETING::TargetHandle_t i_trgt )
{
    PRDF_ASSERT( NULL != i_trgt );

    TargetHandle_t parent = getParentChip( i_trgt );
    PRDF_ASSERT( NULL != parent );

    return parent->getAttr<ATTR_MODEL>();
}

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

uint8_t getChipLevel( TARGETING::TargetHandle_t i_trgt )
{
    PRDF_ASSERT( NULL != i_trgt );

    TargetHandle_t parent = getParentChip( i_trgt );
    PRDF_ASSERT( NULL != parent );

    return parent->getAttr<ATTR_EC>();
}

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

void setHWStateChanged(TARGETING::TargetHandle_t i_target)
{
    #define PRDF_FUNC "[PlatServices::setHWStateChanged] "
    if(NULL != i_target)
    {
        TYPE type = getTargetType(i_target);
        if( (TYPE_DIMM   == type) ||
            (TYPE_MEMBUF == type) ||
            (TYPE_MCS    == type) )
        {
            update_hwas_changed_mask(i_target, HWAS_CHANGED_BIT_MEMDIAG);
        }
        else
        {
            PRDF_ERR(PRDF_FUNC "invalid target type: 0x%08x", type);
        }
    }
    else
    {
        PRDF_ERR(PRDF_FUNC "i_target is null");
    }

    #undef PRDF_FUNC
}

//##############################################################################
//##
//##                       getConnected() support functions
//##
//##############################################################################

// This is a helper function for getConnected(). It will return the association
// type (CHILD_BY_AFFINITY or PARENT_BY_AFFINITY) between a target and
// destination target type. The function only characterizes parent or child
// relationships. It does not do any peer-to-peer relationships. The function
// will return non-SUCCESS if a relationship is not supported.

struct conn_t
{
    TYPE from : 8;
    TYPE to : 8;
    TargetService::ASSOCIATION_TYPE type : 8;

    static uint32_t getSortOrder( TYPE type )
    {
        // Can't trust that the order of the TYPE enum does not change so create
        // our own sorting order.

        uint32_t order = 0;

        switch ( type )
        {
            case TYPE_SYS:          order =  0; break;
            case TYPE_NODE:         order =  1; break;
            case TYPE_PROC:         order =  2; break;
            case TYPE_EQ:           order =  3; break;
            case TYPE_EX:           order =  4; break;
            case TYPE_CORE:         order =  5; break;
            case TYPE_CAPP:         order =  6; break;
            case TYPE_PEC:          order =  7; break;
            case TYPE_PHB:          order =  8; break;
            case TYPE_OBUS:         order =  9; break;
            case TYPE_XBUS:         order = 10; break;
            case TYPE_NX:           order = 11; break;
            case TYPE_OCC:          order = 12; break;
            case TYPE_PSI:          order = 13; break;
            case TYPE_MCBIST:       order = 14; break;
            case TYPE_MCS:          order = 15; break;
            case TYPE_MCA:          order = 16; break;
            case TYPE_MC:           order = 17; break;
            case TYPE_MI:           order = 18; break;
            case TYPE_DMI:          order = 19; break;
            case TYPE_MEMBUF:       order = 20; break;
            case TYPE_L4:           order = 21; break;
            case TYPE_MBA:          order = 22; break;
            case TYPE_DIMM:         order = 23; break;
            default: ;
        }

        return order;
    }

    bool operator<( const conn_t & r )
    {
        uint32_t thisOrder = getSortOrder(this->from);
        uint32_t thatOrder = getSortOrder(r.from);

        if ( thisOrder == thatOrder )
            return ( getSortOrder(this->to) < getSortOrder(r.to) );
        else
            return ( thisOrder < thatOrder );
    }

};

TargetService::ASSOCIATION_TYPE getAssociationType( TargetHandle_t i_target,
                                                    TYPE i_connType )
{
    #define PRDF_FUNC "[PlatServices::getAssociationType] "

    PRDF_ASSERT( nullptr != i_target );

    static conn_t lookups[] =
    {
        // This table must be sorted based on the < operator of struct conn_t.
        { TYPE_SYS,    TYPE_NODE,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_NODE,   TYPE_SYS,        TargetService::PARENT_BY_AFFINITY },
        { TYPE_NODE,   TYPE_PROC,       TargetService::CHILD_BY_AFFINITY  },
        { TYPE_NODE,   TYPE_MEMBUF,     TargetService::CHILD_BY_AFFINITY },

        { TYPE_PROC,   TYPE_NODE,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_PROC,   TYPE_EQ,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_EX,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_CORE,       TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_CAPP,       TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_PEC,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_PHB,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_OBUS,       TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_XBUS,       TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_NX,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_OCC,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_PSI,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MCBIST,     TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MCS,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MCA,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MC,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MI,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_DMI,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MEMBUF,     TargetService::CHILD_BY_AFFINITY  },

        { TYPE_EQ,     TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_EQ,     TYPE_EX,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_EQ,     TYPE_CORE,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_EX,     TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_EX,     TYPE_EQ,         TargetService::PARENT_BY_AFFINITY },
        { TYPE_EX,     TYPE_CORE,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_CORE,   TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_CORE,   TYPE_EQ,         TargetService::PARENT_BY_AFFINITY },
        { TYPE_CORE,   TYPE_EX,         TargetService::PARENT_BY_AFFINITY },

        { TYPE_CAPP,   TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },

        { TYPE_PEC,    TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_PEC,    TYPE_PHB,        TargetService::CHILD_BY_AFFINITY  },

        { TYPE_PHB,    TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },

        { TYPE_OBUS,   TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },

        { TYPE_XBUS,   TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },

        { TYPE_NX,     TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },

        { TYPE_OCC,    TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },

        { TYPE_PSI,    TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },

        { TYPE_MCBIST, TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCBIST, TYPE_MCS,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MCBIST, TYPE_MCA,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MCBIST, TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_MCS,    TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCS,    TYPE_MCBIST,     TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCS,    TYPE_MCA,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MCS,    TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_MCA,    TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCA,    TYPE_MCBIST,     TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCA,    TYPE_MCS,        TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCA,    TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_MC,     TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_MC,     TYPE_MI,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MC,     TYPE_DMI,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MC,     TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_MI,     TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_MI,     TYPE_MC,         TargetService::PARENT_BY_AFFINITY },
        { TYPE_MI,     TYPE_DMI,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MI,     TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_DMI,    TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_DMI,    TYPE_MC,         TargetService::PARENT_BY_AFFINITY },
        { TYPE_DMI,    TYPE_MI,         TargetService::PARENT_BY_AFFINITY },
        { TYPE_DMI,    TYPE_MEMBUF,     TargetService::CHILD_BY_AFFINITY  },
        { TYPE_DMI,    TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_MEMBUF, TYPE_NODE,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_MEMBUF, TYPE_PROC,       TargetService::PARENT_BY_AFFINITY },
        { TYPE_MEMBUF, TYPE_DMI,        TargetService::PARENT_BY_AFFINITY },
        { TYPE_MEMBUF, TYPE_L4,         TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MEMBUF, TYPE_MBA,        TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MEMBUF, TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_L4,     TYPE_MEMBUF,     TargetService::PARENT_BY_AFFINITY },

        { TYPE_MBA,    TYPE_MEMBUF,     TargetService::PARENT_BY_AFFINITY },
        { TYPE_MBA,    TYPE_DIMM,       TargetService::CHILD_BY_AFFINITY  },

        { TYPE_DIMM,   TYPE_MCA,        TargetService::PARENT_BY_AFFINITY },
        { TYPE_DIMM,   TYPE_MBA,        TargetService::PARENT_BY_AFFINITY },

    };

    const size_t sz_lookups = sizeof(lookups) / sizeof(conn_t);

    TYPE type = getTargetType(i_target);

    conn_t match = { type, i_connType, TargetService::CHILD_BY_AFFINITY };

    conn_t * it = std::lower_bound( lookups, lookups + sz_lookups, match );

    if ( (it == lookups + sz_lookups) || // off the end
         (type != it->from) || (i_connType != it->to) ) // not equals
    {
        PRDF_ERR( PRDF_FUNC "Look-up failed: i_target=0x%08x i_connType=%d",
                  getHuid(i_target), i_connType );
        PRDF_ASSERT(false);
    }

    return it->type;

    #undef PRDF_FUNC
}

// Helper function for the various getConnected() functions.
TargetHandleList getConnAssoc( TargetHandle_t i_target, TYPE i_connType,
                               TargetService::ASSOCIATION_TYPE i_assocType )
{
    PRDF_ASSERT( nullptr != i_target );

    TargetHandleList o_list; // Default empty list

    // Match any class, specified type, and functional.
    PredicateCTM predType( CLASS_NA, i_connType );
    PredicateIsFunctional predFunc;
    PredicatePostfixExpr predAnd;
    predAnd.push(&predType).push(&predFunc).And();

    targetService().getAssociated( o_list, i_target, i_assocType,
                                   TargetService::ALL, &predAnd );

    // Sort by target position.
    std::sort( o_list.begin(), o_list.end(),
               [](TargetHandle_t a, TargetHandle_t b)
               { return getTargetPosition(a) < getTargetPosition(b); } );

    return o_list;
}

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

TargetHandleList getConnected( TargetHandle_t i_target, TYPE i_connType )
{
    PRDF_ASSERT( nullptr != i_target );

    TargetHandleList o_list; // Default empty list

    if ( getTargetType(i_target) == i_connType )
    {
        o_list.push_back( i_target );
    }
    else
    {
        o_list = getConnAssoc( i_target, i_connType,
                               getAssociationType(i_target, i_connType) );
    }

    return o_list;
}

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

TargetHandle_t getConnectedParent( TargetHandle_t i_target, TYPE i_connType )
{
    #define PRDF_FUNC "[PlatServices::getConnectedParent] "

    PRDF_ASSERT( nullptr != i_target );

    // Get the association type, must be PARENT_BY_AFFINITY.
    TargetService::ASSOCIATION_TYPE assocType = getAssociationType( i_target,
                                                                    i_connType);
    if ( TargetService::PARENT_BY_AFFINITY != assocType )
    {
        PRDF_ERR( PRDF_FUNC "Unsupported parent connection: i_target=0x%08x "
                  "i_connType=%d", getHuid(i_target), i_connType );
        PRDF_ASSERT(false);
    }

    // Get the connected parent, should be one and only one parent
    TargetHandleList list = getConnAssoc( i_target, i_connType, assocType );
    if ( 1 != list.size() || nullptr == list[0] )
    {
        PRDF_ERR( PRDF_FUNC "Could not find parent: i_target=0x%08x "
                  "i_connType=%d", getHuid(i_target), i_connType );
        PRDF_ASSERT(false);
    }

    return list[0];

    #undef PRDF_FUNC
}

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

TargetHandle_t getConnectedChild( TargetHandle_t i_target, TYPE i_connType,
                                  uint32_t i_connPos )
{
    #define PRDF_FUNC "[PlatServices::getConnectedChild] "

    PRDF_ASSERT( nullptr != i_target );

    TargetHandle_t o_child = nullptr;

    // Get the association type, must be CHILD_BY_AFFINITY.
    TargetService::ASSOCIATION_TYPE assocType = getAssociationType( i_target,
                                                                    i_connType);
    if ( TargetService::CHILD_BY_AFFINITY != assocType )
    {
        PRDF_ERR( PRDF_FUNC "Unsupported child connection: i_target=0x%08x "
                  "i_connType=%d", getHuid(i_target), i_connType );
        PRDF_ASSERT(false);
    }

    // Get the list.
    TargetHandleList list = getConnAssoc( i_target, i_connType, assocType );
    if ( !list.empty() )
    {
        // There are some special cases where we need something other than to
        // match the unit positions. So check those first.

        TargetHandleList::iterator itr = list.end();
        TYPE     trgtType = getTargetType(     i_target );
        uint32_t trgtPos  = getTargetPosition( i_target );

        if ( TYPE_EQ == trgtType && TYPE_EX == i_connType )
        {
            // i_connPos is position relative to EQ (0-1)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t exPos = getTargetPosition(t);
                        return (trgtPos   == (exPos / MAX_EX_PER_EQ)) &&
                               (i_connPos == (exPos % MAX_EX_PER_EQ));
                    } );
        }
        else if ( TYPE_EQ == trgtType && TYPE_CORE == i_connType )
        {
            // i_connPos is position relative to EQ (0-3)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t ecPos = getTargetPosition(t);
                        return (trgtPos   == (ecPos / MAX_EC_PER_EQ)) &&
                               (i_connPos == (ecPos % MAX_EC_PER_EQ));
                    } );
        }
        else if ( TYPE_EX == trgtType && TYPE_CORE == i_connType )
        {
            // i_connPos is position relative to EX (0-1)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t ecPos = getTargetPosition(t);
                        return (trgtPos   == (ecPos / MAX_EC_PER_EX)) &&
                               (i_connPos == (ecPos % MAX_EC_PER_EX));
                    } );
        }
        else if ( TYPE_PEC == trgtType && TYPE_PHB == i_connType )
        {
            // i_connPos is position relative to PEC (0, 0-1, or 0-2)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t relPec = 0;
                        uint32_t relPhb = 0;
                        switch ( getTargetPosition(t) )
                        {
                            case 0: relPec = 0; relPhb = 0; break;
                            case 1: relPec = 1; relPhb = 0; break;
                            case 2: relPec = 1; relPhb = 1; break;
                            case 3: relPec = 2; relPhb = 0; break;
                            case 4: relPec = 2; relPhb = 1; break;
                            case 5: relPec = 2; relPhb = 2; break;
                        }
                        return (trgtPos == relPec) && (i_connPos == relPhb);
                    } );
        }
        else if ( TYPE_MCBIST == trgtType && TYPE_MCS == i_connType )
        {
            // i_connPos is position relative to MCBIST (0-1)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t mcbPos = getTargetPosition(t);
                        return (trgtPos   == (mcbPos / MAX_MCS_PER_MCBIST)) &&
                               (i_connPos == (mcbPos % MAX_MCS_PER_MCBIST));
                    } );

        }
        else if ( TYPE_MCBIST == trgtType && TYPE_MCA == i_connType )
        {
            // i_connPos is position relative to MCBIST (0-3)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t mcbPos = getTargetPosition(t);
                        return (trgtPos   == (mcbPos / MAX_MCA_PER_MCBIST)) &&
                               (i_connPos == (mcbPos % MAX_MCA_PER_MCBIST));
                    } );

        }
        else if ( TYPE_MCS == trgtType && TYPE_MCA == i_connType )
        {
            // i_connPos is position relative to MCS (0-1)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t mcaPos = getTargetPosition(t);
                        return (trgtPos   == (mcaPos / MAX_MCA_PER_MCS)) &&
                               (i_connPos == (mcaPos % MAX_MCA_PER_MCS));
                    } );
        }
        else if ( TYPE_MCA == trgtType && TYPE_DIMM == i_connType )
        {
            // i_connPos is the DIMM select (0-1). Note that we don't use
            // getTargetPosition() on the DIMM because that does not return a
            // value that is relative to the processor as we were expecting.
            // There really isn't a good position attribute that matches the
            // position in the affinity path. We can use ATTR_REL_POS, which
            // will always match the DIMM select. This does not let us match the
            // parent unit like all of the other checks in this functions.
            // Fortunately, it will be very difficult to have a bug where the
            // getConnected code returns DIMMs on a different MCA target. So
            // this is an acceptible risk.
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    { return ( i_connPos == t->getAttr<ATTR_REL_POS>() ); } );
        }
        else if ( TYPE_MC == trgtType && TYPE_MI == i_connType )
        {
            // i_connPos is position relative to MC (0-1)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t mcPos = getTargetPosition(t);
                        return (trgtPos   == (mcPos / MAX_MI_PER_MC)) &&
                               (i_connPos == (mcPos % MAX_MI_PER_MC));
                    } );
        }
        else if ( TYPE_MC == trgtType && TYPE_DMI == i_connType )
        {
            // i_connPos is position relative to MC (0-3)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t mcPos = getTargetPosition(t);
                        return (trgtPos   == (mcPos / MAX_DMI_PER_MC)) &&
                               (i_connPos == (mcPos % MAX_DMI_PER_MC));
                    } );
        }
        else if ( TYPE_MI == trgtType && TYPE_DMI == i_connType )
        {
            // i_connPos is position relative to MI (0-1)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t miPos = getTargetPosition(t);
                        return (trgtPos   == (miPos / MAX_DMI_PER_MI)) &&
                               (i_connPos == (miPos % MAX_DMI_PER_MI));
                    } );
        }
        else if ( TYPE_PROC == trgtType && TYPE_MEMBUF == i_connType )
        {
            // i_connPos is position relative to PROC (0-7)
            itr = std::find_if( list.begin(), list.end(),
                    [&](const TargetHandle_t & t)
                    {
                        uint32_t mbPos = getTargetPosition(t);
                        return (trgtPos   == (mbPos / MAX_MEMBUF_PER_PROC)) &&
                               (i_connPos == (mbPos % MAX_MEMBUF_PER_PROC));
                    } );
        }
        else if ( TYPE_DMI == trgtType && TYPE_MEMBUF == i_connType )
        {
            // There is only one MEMBUF per DMI in the list.
            PRDF_ASSERT( 1 == list.size() ); // just in case
            itr = list.begin();
        }
        else
        {
            // default, i_connPos should match the unit position within the chip
            itr = std::find_if( list.begin(), list.end(),
                                [&](const TargetHandle_t & t)
                                { return i_connPos == getTargetPosition(t); } );
        }

        // Get the target if found.
        if ( list.end() != itr )
            o_child = *itr;
    }

    return o_child;

    #undef PRDF_FUNC
}

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

ExtensibleChipList getConnected( ExtensibleChip * i_chip, TYPE i_connType )
{
    PRDF_ASSERT( nullptr != i_chip );

    ExtensibleChipList o_list; // Default empty list

    TargetHandleList list = getConnected( i_chip->getTrgt(), i_connType );
    for ( auto & trgt : list )
    {
        o_list.push_back( (ExtensibleChip *)systemPtr->GetChip(trgt) );
    }

    return o_list;
}

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

ExtensibleChip * getConnectedParent( ExtensibleChip * i_child,
                                     TYPE i_parentType )
{
    PRDF_ASSERT( nullptr != i_child );

    TargetHandle_t trgt = getConnectedParent( i_child->getTrgt(),
                                              i_parentType );

    return (ExtensibleChip *)systemPtr->GetChip( trgt );
}

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

ExtensibleChip * getConnectedChild( ExtensibleChip * i_parent,
                                    TARGETING::TYPE i_childType,
                                    uint32_t i_childPos )
{
    PRDF_ASSERT( nullptr != i_parent );

    ExtensibleChip * o_child = nullptr;

    TargetHandle_t trgt = getConnectedChild( i_parent->getTrgt(),
                                             i_childType,
                                             i_childPos );
    if ( nullptr != trgt )
    {
        o_child = (ExtensibleChip *)systemPtr->GetChip( trgt );
    }

    return o_child;
}

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

ExtensibleChip * getNeighborCore( ExtensibleChip * i_core )
{
    PRDF_ASSERT( nullptr != i_core );

    TargetHandle_t thisCore = i_core->getTrgt();
    ExtensibleChip * neighborCore = nullptr;
    TargetHandleList list =
      getConnected(getConnectedParent(thisCore, TYPE_EX), TYPE_CORE);

    for ( auto & trgt : list)
    {
        if ( trgt != thisCore )
        {
            neighborCore = (ExtensibleChip *)systemPtr->GetChip(trgt);
            break;
        }
    }
    return neighborCore;
}
//------------------------------------------------------------------------------

TargetHandle_t getConnectedPeerTarget( TargetHandle_t i_target )
{
    #define PRDF_FUNC "[PlatServices::getConnectedPeerTarget] "

    PRDF_ASSERT( NULL != i_target );

    TargetHandle_t o_target = NULL;

    do
    {
        TYPE type = getTargetType( i_target );

        switch( type )
        {
            case TYPE_XBUS:
            case TYPE_OBUS:
            case TYPE_PSI:

                o_target = i_target->getAttr<ATTR_PEER_TARGET>();
                break;

            default:
                PRDF_ERR( PRDF_FUNC "Target type not supported: i_target=0x%08x "
                          "type=0x%x", getHuid(i_target), type );
        }


    } while(0);

    return o_target;

    #undef PRDF_FUNC
}

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

TargetHandle_t getConnectedPeerProc( TargetHandle_t i_procTarget,
                                     TYPE i_busType, uint32_t i_busPos )
{
    #define PRDF_FUNC "[PlatServices::getConnectedPeerProc] "

    PRDF_ASSERT( NULL != i_procTarget );
    PRDF_ASSERT( TYPE_PROC == getTargetType(i_procTarget) );
    PRDF_ASSERT( ((TYPE_XBUS == i_busType) && (MAX_XBUS_PER_PROC > i_busPos)) ||
                 ((TYPE_OBUS == i_busType) && (MAX_OBUS_PER_PROC > i_busPos)) );

    TargetHandle_t o_target = NULL;

    do
    {
        // Starting PROC -> starting XBUS/ABUS.
        TargetHandle_t busTarget = getConnectedChild( i_procTarget, i_busType,
                                                      i_busPos );
        if ( NULL == busTarget ) break;

        // Starting XBUS/ABUS -> ATTR_PEER_TARGET -> destination XBUS/ABUS.
        TargetHandle_t destTarget = getConnectedPeerTarget( busTarget );
        if ( NULL == destTarget ) break;

        // Destination XBUS/ABUS -> destination PROC.
        o_target = getConnectedParent( destTarget, TYPE_PROC );

    } while(0);

    return o_target;

    #undef PRDF_FUNC
}

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

template<>
uint8_t getDimmPort<TYPE_MBA>( TARGETING::TargetHandle_t i_dimmTrgt )
{
    PRDF_ASSERT( nullptr != i_dimmTrgt );
    PRDF_ASSERT( TYPE_DIMM == getTargetType(i_dimmTrgt) );

    return i_dimmTrgt->getAttr<ATTR_CEN_MBA_PORT>();
}

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

template<>
uint8_t getDimmSlct<TYPE_MBA>( TargetHandle_t i_trgt )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( TYPE_DIMM == getTargetType(i_trgt) );

    return i_trgt->getAttr<ATTR_CEN_MBA_DIMM>();
}

template<>
uint8_t getDimmSlct<TYPE_MCA>( TargetHandle_t i_trgt )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( TYPE_DIMM == getTargetType(i_trgt) );

    return getTargetPosition(i_trgt) % MAX_DIMM_PER_PORT;
}

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

TARGETING::TargetHandleList getConnectedDimms( TARGETING::TargetHandle_t i_trgt,
                                               const MemRank & i_rank )
{
    #define PRDF_FUNC "[PlatServices::getConnectedDimms] "

    TargetHandleList o_list;
    TYPE l_trgtType = getTargetType( i_trgt );

    if ( TYPE_MCA == l_trgtType )
    {
        o_list.push_back(
            getConnectedChild(i_trgt, TYPE_DIMM, i_rank.getDimmSlct()) );
    }
    else if ( TYPE_MBA == l_trgtType )
    {
        TargetHandleList l_dimmList = getConnected( i_trgt, TYPE_DIMM );
        for ( auto & dimm : l_dimmList )
        {
            uint8_t l_dimmSlct = getDimmSlct<TYPE_MBA>( dimm );
            if ( l_dimmSlct == i_rank.getDimmSlct() )
            {
                o_list.push_back( dimm );
            }
        }
    }
    else
    {
        PRDF_ERR(PRDF_FUNC "Invalid target type: HUID=0x%08x", getHuid(i_trgt));
        PRDF_ASSERT( false );
    }

    return o_list;

    #undef PRDF_FUNC
}

TARGETING::TargetHandle_t getConnectedDimm( TARGETING::TargetHandle_t i_trgt,
                                            const MemRank & i_rank,
                                            uint8_t i_port )
{
    #define PRDF_FUNC "[PlatServices::getConnectedDimm] "

    TargetHandle_t o_dimm = nullptr;
    TYPE l_trgtType = getTargetType( i_trgt );

    if ( TYPE_MCA == l_trgtType )
    {
        o_dimm = getConnectedChild( i_trgt, TYPE_DIMM, i_rank.getDimmSlct() );
    }
    else if ( TYPE_MBA == l_trgtType )
    {
        TargetHandleList l_dimmList = getConnectedDimms( i_trgt, i_rank );
        for ( auto & dimm : l_dimmList )
        {
            uint8_t l_portSlct = getDimmPort<TYPE_MBA>( dimm );
            if ( l_portSlct == i_port )
            {
                o_dimm = dimm;
                break;
            }
        }
    }
    else
    {
        PRDF_ERR(PRDF_FUNC "Invalid target type: HUID=0x%08x", getHuid(i_trgt));
        PRDF_ASSERT( false );
    }

    return o_dimm;

    #undef PRDF_FUNC
}

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

TARGETING::TargetHandle_t getSystemTarget()
{
    TargetHandle_t sysTarget = nullptr;
    targetService().getTopLevelTarget( sysTarget );

    PRDF_ASSERT( nullptr != sysTarget );

    return sysTarget;
}

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

TARGETING::TargetHandle_t getParentChip( TARGETING::TargetHandle_t i_target )
{
    TargetHandle_t o_chipTarget = NULL;

    CLASS l_class = getTargetClass( i_target );
    switch ( l_class )
    {
        case CLASS_CHIP:
            o_chipTarget = i_target;
            break;

        case CLASS_UNIT:
        {
            TargetHandleList l_list;
            PredicateCTM l_predClass( CLASS_CHIP );
            targetService().getAssociated( l_list, i_target,
                                           TargetService::PARENT,
                                           TargetService::ALL,
                                           &l_predClass );
            if ( 1 == l_list.size() )
            {
                o_chipTarget = l_list[0];
            }
            else
            {
                PRDF_ERR( "[getParentChip] Could not find parent chip" );
            }
            break;
        }

        default:
            PRDF_ERR( "[getParentChip] Unsupported class: %d", l_class );
    }

    if ( NULL == o_chipTarget )
    {
        PRDF_ERR( "[getParentChip] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_chipTarget;
}

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

TARGETING::TargetHandleList getFunctionalTargetList( TARGETING::TYPE i_type )
{
    TargetHandleList o_list; // Default empty list.

    TargetService & l_targetService = targetService();

    // Match any class, specified type, and functional.
    PredicateCTM l_predType( CLASS_NA, i_type );
    PredicateIsFunctional l_predFunc;
    PredicatePostfixExpr l_predAnd;
    l_predAnd.push(&l_predType).push(&l_predFunc).And();

    // Defining a filter to get a list of all targets of i_type.
    TargetRangeFilter l_filter( l_targetService.begin(), l_targetService.end(),
                                &l_predAnd );
    for( ; l_filter; ++l_filter )
    {
        // Adding functional target to the vector.
        o_list.push_back( *l_filter );
    }

    return o_list;
}

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

bool checkLastFuncEx( TARGETING::TargetHandle_t i_exTarget )
{
    bool o_lastEx = false;

    TargetHandleList l_list = getFunctionalTargetList( TYPE_EX );
    if ( 1 == l_list.size() && l_list[0] == i_exTarget )
        o_lastEx = true;

    return o_lastEx;
}

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

TargetHandle_t getMasterProc()
{
    TargetHandle_t masterProc = NULL;
    targetService().masterProcChipTargetHandle( masterProc );
    return masterProc;
}

//##############################################################################
//##
//##                       Target position support code
//##
//##############################################################################

uint32_t getTargetPosition( TargetHandle_t i_trgt )
{
    #define PRDF_FUNC "[PlatServices::getTargetPosition] "

    PRDF_ASSERT( nullptr != i_trgt );

    uint32_t o_pos = 0;

    CLASS l_class = getTargetClass( i_trgt );
    TYPE  l_type  = getTargetType(  i_trgt );

    switch ( l_class )
    {
        case CLASS_CHIP: // chips
        {
            switch ( l_type )
            {
                case TYPE_PROC:
                case TYPE_OSC:
                case TYPE_OSCPCICLK:
                case TYPE_OSCREFCLK:
                case TYPE_MEMBUF:
                    o_pos = i_trgt->getAttr<ATTR_POSITION>();
                    break;

                default:
                    PRDF_ERR( PRDF_FUNC "Unsupported type %d for CLASS_CHIP: "
                              "i_trgt=0x%08x", l_type, getHuid(i_trgt) );
                    PRDF_ASSERT( false );
            }
            break;
        }

        case CLASS_UNIT: // units of a chip
            o_pos = i_trgt->getAttr<ATTR_CHIP_UNIT>();
            break;

        case CLASS_ENC: // nodes
            o_pos = i_trgt->getAttr<ATTR_ORDINAL_ID>();
            break;

        case CLASS_SYS: // system
            // The concept of a system position does not exist, however, we want
            // to allow generic code to get the target position for any target.
            // So we will add this special case.
            o_pos = 0;
            break;

        case CLASS_LOGICAL_CARD: // DIMMs
            o_pos = i_trgt->getAttr<ATTR_FAPI_POS>();
            break;

        default:
            PRDF_ERR( PRDF_FUNC "Unsupported class %d: i_trgt=0x%08x",
                      l_class, getHuid(i_trgt) );
            PRDF_ASSERT(false);
    }

    return o_pos;

    #undef PRDF_FUNC
}

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

uint32_t getPhbConfig( TARGETING::TargetHandle_t i_proc )
{
    #define PRDF_FUNC "[PlatServices::getPhbConfig] "

    uint32_t l_pciConfig = 0xffffffff;

    if ( TYPE_PROC == getTargetType(i_proc) )
    {
        l_pciConfig = i_proc->getAttr<ATTR_PROC_PCIE_IOP_CONFIG>();
    }
    else
    {
        PRDF_ERR( PRDF_FUNC "Invalid Target Huid = 0x%08x", getHuid(i_proc) );
    }

    return l_pciConfig;

    #undef PRDF_FUNC
}

//##############################################################################
//##
//##                        Memory specific functions
//##
//##############################################################################

uint32_t getMemChnl( TargetHandle_t i_trgt )
{
    PRDF_ASSERT( nullptr != i_trgt );

    TargetHandle_t dmiTrgt = getConnectedParent( i_trgt, TYPE_DMI );

    return getTargetPosition( dmiTrgt );
}

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

template<>
bool isMembufOnDimm<TYPE_MBA>( TargetHandle_t i_trgt )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( TYPE_MBA == getTargetType(i_trgt) );

    return i_trgt->getAttr<ATTR_CEN_EFF_CUSTOM_DIMM>();
}

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

template<>
uint8_t getDramGen<TYPE_MBA>( TargetHandle_t i_trgt )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( TYPE_MBA == getTargetType(i_trgt) );

    return i_trgt->getAttr<ATTR_CEN_EFF_DRAM_GEN>();
}

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

int32_t getDimmRowCol( TARGETING::TargetHandle_t i_mba, uint8_t & o_rowNum,
                       uint8_t & o_colNum )
{
    #define PRDF_FUNC "[PlatServices::getDimmRowCol] "

    int32_t o_rc = FAIL;
    do
    {
        if ( TYPE_MBA != getTargetType( i_mba ) )
        {
            PRDF_ERR( PRDF_FUNC "Invalid Target. HUID:0X%08X",
                      getHuid( i_mba ) );
            break;
        }

        ATTR_MODEL_type l_procModel = getChipModel( getMasterProc() );
        if ( MODEL_CUMULUS == l_procModel )
        {
            o_rowNum = i_mba->getAttr<ATTR_CEN_EFF_DRAM_ROWS>();
            o_colNum = i_mba->getAttr<ATTR_CEN_EFF_DRAM_COLS>();
        }
        else // NIMBUS or something without CENTAURs
        {
            o_rowNum = i_mba->getAttr<ATTR_EFF_DRAM_ROWS>();
            o_colNum = i_mba->getAttr<ATTR_EFF_DRAM_COLS>();
        }

        o_rc = SUCCESS;

    }while(0);

    return o_rc;
    #undef PRDF_FUNC
}

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

bool isDramWidthX4( TargetHandle_t i_trgt )
{
   bool o_dramWidthX4 = false;

   PRDF_ASSERT( nullptr != i_trgt );
   switch ( getTargetType(i_trgt) )
   {
        case TYPE_MCA:
            o_dramWidthX4 = true; // Nimbus only supports x4 DRAMs
            break;

        case TYPE_MBA:
            o_dramWidthX4 = ( fapi2::ENUM_ATTR_CEN_EFF_DRAM_WIDTH_X4 ==
                              i_trgt->getAttr<ATTR_CEN_EFF_DRAM_WIDTH>() );
            break;

        default:
          PRDF_ASSERT(false); // code bug
   }

   return o_dramWidthX4;
}

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

template<TARGETING::TYPE T>
void __getMasterRanks( TargetHandle_t i_trgt, std::vector<MemRank> & o_ranks,
                       uint8_t i_pos, uint8_t i_ds )
{
    #define PRDF_FUNC "[__getMasterRanks] "

    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( T == getTargetType(i_trgt) );
    PRDF_ASSERT( i_pos < 2 );
    PRDF_ASSERT( i_ds <= MAX_DIMM_PER_PORT ); // can equal MAX_DIMM_PER_PORT

    o_ranks.clear();

    uint8_t info[2][2];

    ATTR_MODEL_type l_procModel = getChipModel( getMasterProc() );
    if ( MODEL_CUMULUS == l_procModel )
    {
        if ( !i_trgt->tryGetAttr<ATTR_CEN_EFF_DIMM_RANKS_CONFIGED>(info) )
        {
            PRDF_ERR( PRDF_FUNC "tryGetAttr<ATTR_CEN_EFF_DIMM_RANKS_CONFIGED> "
                      "failed: i_trgt=0x%08x", getHuid(i_trgt) );
            PRDF_ASSERT( false ); // attribute does not exist for target
        }
    }
    else if ( !i_trgt->tryGetAttr<ATTR_EFF_DIMM_RANKS_CONFIGED>(info) )
    {
        PRDF_ERR( PRDF_FUNC "tryGetAttr<ATTR_EFF_DIMM_RANKS_CONFIGED> "
                  "failed: i_trgt=0x%08x", getHuid(i_trgt) );
        PRDF_ASSERT( false ); // attribute does not exist for target
    }

    for ( uint32_t ds = 0; ds < MAX_DIMM_PER_PORT; ds++ )
    {
        // Check if user gave a specific value for i_ds.
        if ( (MAX_DIMM_PER_PORT != i_ds) && (ds != i_ds) )
            continue;

        uint8_t rankMask = info[i_pos][ds];

        // The configured rank selects are in the first nibble.
        for ( uint32_t rs = 0; rs < 4; rs++ )
        {
            if ( 0 != (rankMask & (0x80 >> rs)) )
            {
                // Note that the ranks are getting inserted in order so no need
                // to sort later.
                o_ranks.push_back( MemRank((ds << 2) | rs) );
            }
        }
    }

    #undef PRDF_FUNC
}

template<>
void getMasterRanks<TYPE_MCA>( TargetHandle_t i_trgt,
                               std::vector<MemRank> & o_ranks,
                               uint8_t i_ds )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( TYPE_MCA == getTargetType(i_trgt) );

    // NOTE: The attribute lives on the MCS. So need to get the MCS target and
    //       the position of the MCA relative to the MCS.
    TargetHandle_t mcsTrgt = getConnectedParent( i_trgt, TYPE_MCS );
    uint8_t relPos = getTargetPosition(i_trgt) % MAX_MCA_PER_MCS;

    __getMasterRanks<TYPE_MCS>( mcsTrgt, o_ranks, relPos, i_ds );
}

template<>
void getMasterRanks<TYPE_MBA>( TargetHandle_t i_trgt,
                               std::vector<MemRank> & o_ranks,
                               uint8_t i_ds )
{
    // NOTE: DIMMs must be plugged into pairs. So the values for each port
    //       select will be the same for each DIMM select. There is no need to
    //       iterate on both port selects.
    __getMasterRanks<TYPE_MBA>( i_trgt, o_ranks, 0, i_ds );
}

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

template<TARGETING::TYPE T>
void __getSlaveRanks( TargetHandle_t i_trgt, std::vector<MemRank> & o_ranks,
                      uint8_t i_ds )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( T == getTargetType(i_trgt) );
    PRDF_ASSERT( i_ds <= MAX_DIMM_PER_PORT ); // can equal MAX_DIMM_PER_PORT

    o_ranks.clear();

    for ( uint32_t ds = 0; ds < MAX_DIMM_PER_PORT; ds++ )
    {
        // Check if user gave a specific value for i_ds.
        if ( (MAX_DIMM_PER_PORT != i_ds) && (ds != i_ds) )
            continue;

        // Get the number of slave ranks per master rank.
        uint8_t numRanks = getNumRanksPerDimm<T>( i_trgt, ds );
        if ( 0 == numRanks ) continue; // nothing to do

        uint8_t numMasterRanks = getNumMasterRanksPerDimm<T>( i_trgt, ds );
        PRDF_ASSERT( 0 < numMasterRanks ); // ATTR bug

        uint8_t numSlaveRanks = numRanks / numMasterRanks;

        // Get the current list of master ranks for this DIMM select
        std::vector<MemRank> tmpList;
        getMasterRanks<T>( i_trgt, tmpList, ds );

        // Start inserting the slave ranks into the list.
        for ( auto & mrank : tmpList )
        {
            for ( uint8_t s = 0; s < numSlaveRanks; s++ )
            {
                // Note that the ranks are getting inserted in order so no need
                // to sort later.
                o_ranks.push_back( MemRank(mrank.getMaster(), s) );
            }
        }
    }
}

template<>
void getSlaveRanks<TYPE_MCA>( TargetHandle_t i_trgt,
                              std::vector<MemRank> & o_ranks,
                              uint8_t i_ds )
{
    __getSlaveRanks<TYPE_MCA>( i_trgt, o_ranks, i_ds );
}

template<>
void getSlaveRanks<TYPE_MBA>( TargetHandle_t i_trgt,
                              std::vector<MemRank> & o_ranks,
                              uint8_t i_ds )
{
    __getSlaveRanks<TYPE_MBA>( i_trgt, o_ranks, i_ds );
}

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

template<TARGETING::TYPE T>
uint8_t __getNumMasterRanksPerDimm( TargetHandle_t i_trgt,
                                    uint8_t i_pos, uint8_t i_ds )
{
    #define PRDF_FUNC "[__getNumMasterRanksPerDimm] "

    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( T == getTargetType(i_trgt) );
    PRDF_ASSERT( i_pos < 2 );
    PRDF_ASSERT( i_ds < MAX_DIMM_PER_PORT );
    uint8_t num;


    ATTR_MODEL_type l_procModel = getChipModel( getMasterProc() );
    if ( MODEL_CUMULUS == l_procModel )
    {
        ATTR_CEN_EFF_NUM_MASTER_RANKS_PER_DIMM_type attr;
        if ( !i_trgt->tryGetAttr<ATTR_CEN_EFF_NUM_MASTER_RANKS_PER_DIMM>(attr) )
        {
            PRDF_ERR( PRDF_FUNC
                      "tryGetAttr<ATTR_CEN_EFF_NUM_MASTER_RANKS_PER_DIMM> "
                      "failed: i_trgt=0x%08x", getHuid(i_trgt) );
            PRDF_ASSERT( false ); // attribute does not exist for target
        }

        num = attr[i_pos][i_ds];
    }
    else
    {
        ATTR_EFF_NUM_MASTER_RANKS_PER_DIMM_type attr;
        if ( !i_trgt->tryGetAttr<ATTR_EFF_NUM_MASTER_RANKS_PER_DIMM>(attr) )
        {
            PRDF_ERR( PRDF_FUNC
                      "tryGetAttr<ATTR_EFF_NUM_MASTER_RANKS_PER_DIMM> "
                      "failed: i_trgt=0x%08x", getHuid(i_trgt) );
            PRDF_ASSERT( false ); // attribute does not exist for target
        }

        num = attr[i_pos][i_ds];
    }

    PRDF_ASSERT( num < MASTER_RANKS_PER_DIMM_SLCT );

    return num;

    #undef PRDF_FUNC
}

template<>
uint8_t getNumMasterRanksPerDimm<TYPE_MCA>( TargetHandle_t i_trgt,
                                            uint8_t i_ds )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( TYPE_MCA == getTargetType(i_trgt) );

    // NOTE: The attribute lives on the MCS. So need to get the MCS target and
    //       the position of the MCA relative to the MCS.
    TargetHandle_t mcsTrgt = getConnectedParent( i_trgt, TYPE_MCS );
    uint8_t relPos = getTargetPosition(i_trgt) % MAX_MCA_PER_MCS;

    return __getNumMasterRanksPerDimm<TYPE_MCS>( mcsTrgt, relPos, i_ds );
}

template<>
uint8_t getNumMasterRanksPerDimm<TYPE_MBA>( TargetHandle_t i_trgt,
                                            uint8_t i_ds )
{
    // NOTE: DIMMs must be plugged into pairs. So the values for each port
    //       select will be the same for each DIMM select. There is no need to
    //       iterate on both port selects.
    return __getNumMasterRanksPerDimm<TYPE_MBA>( i_trgt, 0, i_ds );
}

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

template<TARGETING::TYPE T>
uint8_t __getNumRanksPerDimm( TargetHandle_t i_trgt,
                              uint8_t i_pos, uint8_t i_ds )
{
    #define PRDF_FUNC "[__getNumRanksPerDimm] "

    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( T == getTargetType(i_trgt) );
    PRDF_ASSERT( i_pos < 2 );
    PRDF_ASSERT( i_ds < MAX_DIMM_PER_PORT );
    uint8_t num;

    ATTR_MODEL_type l_procModel = getChipModel( getMasterProc() );
    if ( MODEL_CUMULUS == l_procModel )
    {
        ATTR_CEN_EFF_NUM_RANKS_PER_DIMM_type attr;
        if ( !i_trgt->tryGetAttr<ATTR_CEN_EFF_NUM_RANKS_PER_DIMM>(attr) )
        {
            PRDF_ERR( PRDF_FUNC
                      "tryGetAttr<ATTR_CEN_EFF_NUM_RANKS_PER_DIMM> "
                      "failed: i_trgt=0x%08x", getHuid(i_trgt) );
            PRDF_ASSERT( false ); // attribute does not exist for target
        }

        num = attr[i_pos][i_ds];
    }
    else
    {
        ATTR_EFF_NUM_RANKS_PER_DIMM_type attr;
        if ( !i_trgt->tryGetAttr<ATTR_EFF_NUM_RANKS_PER_DIMM>(attr) )
        {
            PRDF_ERR( PRDF_FUNC "tryGetAttr<ATTR_EFF_NUM_RANKS_PER_DIMM> "
                      "failed: i_trgt=0x%08x", getHuid(i_trgt) );
            PRDF_ASSERT( false ); // attribute does not exist for target
        }

        num = attr[i_pos][i_ds];
    }

    PRDF_ASSERT( num < MASTER_RANKS_PER_DIMM_SLCT*SLAVE_RANKS_PER_MASTER_RANK );

    return num;

    #undef PRDF_FUNC
}

template<>
uint8_t getNumRanksPerDimm<TYPE_MCA>( TargetHandle_t i_trgt, uint8_t i_ds )
{
    PRDF_ASSERT( nullptr != i_trgt );
    PRDF_ASSERT( TYPE_MCA == getTargetType(i_trgt) );

    // NOTE: The attribute lives on the MCS. So need to get the MCS target and
    //       the position of the MCA relative to the MCS.
    TargetHandle_t mcsTrgt = getConnectedParent( i_trgt, TYPE_MCS );
    uint8_t relPos = getTargetPosition(i_trgt) % MAX_MCA_PER_MCS;

    return __getNumRanksPerDimm<TYPE_MCS>( mcsTrgt, relPos, i_ds );
}

template<>
uint8_t getNumRanksPerDimm<TYPE_MBA>( TargetHandle_t i_trgt, uint8_t i_ds )
{
    // NOTE: DIMMs must be plugged into pairs. So the values for each port
    //       select will be the same for each DIMM select. There is no need to
    //       iterate on both port selects.
    return __getNumRanksPerDimm<TYPE_MBA>( i_trgt, 0, i_ds );
}

//##############################################################################
//##
//##                        Clock specific functions
//##
//##############################################################################

TARGETING::TargetHandle_t getClockId(TARGETING::TargetHandle_t
                            i_pGivenTarget,
                            TARGETING ::TYPE i_connType,
                            uint32_t i_oscPos)
{
    #define PRDF_FUNC "[PlatServices::getClockId] "

    TargetHandleList l_clockCardlist;
    TargetHandle_t l_target = i_pGivenTarget;
    TargetHandle_t o_pClockCardHandle = NULL;

    do
    {
        // If membuf target, use the connected proc target
        if(TYPE_MEMBUF == getTargetType(i_pGivenTarget))
        {
            l_target = getConnectedParent(i_pGivenTarget, TYPE_PROC);
        }

        PredicateIsFunctional l_funcFilter;
        PredicateCTM l_oscFilter(CLASS_CHIP, i_connType);
        PredicateCTM l_peerFilter(CLASS_UNIT,
                                  (i_connType == TYPE_OSCREFCLK ?
                                   TYPE_SYSREFCLKENDPT:TYPE_MFREFCLKENDPT));
        PredicatePostfixExpr l_funcAndOscFilter, l_funcAndPeerFilter;
        l_funcAndOscFilter.push(&l_oscFilter).push(&l_funcFilter).And();
        l_funcAndPeerFilter.push(&l_peerFilter).push(&l_funcFilter).And();

        //PROC <---> CLKTYPE <---> PEER <---> CLKTYPE <---> OSC
        //Get the oscillators related to this proc
        getPeerTargets( l_clockCardlist,    // List of connected OSCs
                        l_target,           // to this proc
                        // filter to get to clock endpoints
                        &l_funcAndPeerFilter/*&l_peerFilter*/,
                        // filter to get the driving OSC
                        &l_funcAndOscFilter/*&l_oscFilter*/);

        for(TargetHandleList::iterator l_itr = l_clockCardlist.begin();
            l_itr != l_clockCardlist.end();
            ++l_itr)
        {
            PRDF_TRAC(PRDF_FUNC "OSC 0x%.8X, pos: %d is connected to "
             "proc 0x%.8X, inputOscPos: %d", getHuid(*l_itr),
             getTargetPosition(*l_itr), getHuid(l_target), i_oscPos);

            if ( i_oscPos == getTargetPosition(*l_itr) )
            {
                o_pClockCardHandle = *l_itr;
            }
        }

    } while(0);

    return o_pClockCardHandle;

    #undef PRDF_FUNC
}

//##############################################################################
//##                     MNFG Policy Flag Functions
//##############################################################################

// Helper function to access the state of manufacturing policy flags.
bool isMnfgFlagSet( uint32_t i_flag )
{
    bool o_rc = false;
    ATTR_MNFG_FLAGS_type l_attrValue = 0;
    TargetHandle_t l_pTopTarget= NULL;
    targetService().getTopLevelTarget(l_pTopTarget);
    if(l_pTopTarget)
    {
        l_attrValue = l_pTopTarget->getAttr<ATTR_MNFG_FLAGS>();
        o_rc = l_attrValue & i_flag;
    }
    else
    {
        PRDF_ERR("[isMnfgFlagSet] error finding l_pTopTarget");
    }

    //PRDF_TRAC("[isMnfgFlagSet] MNFG Flags: 0x%016llX, i_flag: "
    //          "0x%08X, o_rc: %d", l_attrValue, i_flag, o_rc);

    return o_rc;
}

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

bool mfgMode()
{ return isMnfgFlagSet( MNFG_FLAG_THRESHOLDS      ); }

bool isFabeRepairDisabled()
{ return isMnfgFlagSet( MNFG_FLAG_DISABLE_FABRIC_eREPAIR ); }

bool isMemeRepairDisabled()
{ return isMnfgFlagSet( MNFG_FLAG_DISABLE_MEMORY_eREPAIR ); }

bool mnfgTerminate()
{ return isMnfgFlagSet( MNFG_FLAG_SRC_TERM        ); }

bool areDramRepairsDisabled()
{ return isMnfgFlagSet( MNFG_FLAG_DISABLE_DRAM_REPAIRS ); }

bool enableFastBgScrub()
{ return isMnfgFlagSet( MNFG_FLAG_FAST_BACKGROUND_SCRUB ); }

bool mnfgSpareDramDeploy()
{ return isMnfgFlagSet( MNFG_FLAG_TEST_DRAM_REPAIRS ); }

bool isMfgCeCheckingEnabled()
{ return isMnfgFlagSet( MNFG_FLAG_IPL_MEMORY_CE_CHECKING ); }

bool isMfgAvpEnabled()
{ return isMnfgFlagSet( MNFG_FLAG_AVP_ENABLE ); }

bool isMfgHdatAvpEnabled()
{ return isMnfgFlagSet( MNFG_FLAG_HDAT_AVP_ENABLE ); }

} // end namespace PlatServices

} // end namespace PRDF

OpenPOWER on IntegriCloud