summaryrefslogtreecommitdiffstats
path: root/src/usr/util/utiltcemgr.C
blob: 072793970408e4ecf1eb375295c71d3fd3581a4b (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/util/utiltcemgr.C $                                   */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2013,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                                                     */
#ifndef __UTILTCEMGR_C
#define __UTILTCEMGR_C

#include <trace/interface.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <util/utiltce.H>
#include <util/align.H>
#include <sys/mmio.h>
#include <sys/mm.h>
#include <sys/misc.h>
#include <arch/ppc.H>
#include <errno.h>
#include <targeting/common/commontargeting.H>
#include <targeting/common/utilFilter.H>
#include <targeting/targplatutil.H>
#include <kernel/console.H>
#include "utiltcemgr.H"
#include <util/util_reasoncodes.H>
#include <assert.h>
#include <intr/interrupt.H>
#include <limits.h>
#include <hwas/common/hwasCallout.H>
#include <attributetraits.H>
#include <pnor/pnorif.H>
#include <targeting/common/targetservice.H>
#include <devicefw/userif.H>
#include <initservice/initserviceif.H>
#include <sbeio/sbeioif.H>

trace_desc_t* g_trac_tce = nullptr;
TRAC_INIT(&g_trac_tce, UTILTCE_TRACE_NAME, 4*KILOBYTE);

// ------------------------
// Macros for unit testing - leave extra trace enabled for now
//#define TRACUCOMP(args...)  TRACFCOMP(args)
#define TRACUCOMP(args...)


namespace TCE
{

/************************************************************************/
// Defines
/************************************************************************/
// TCE Table Address must be 4MB Aligned
#define TCE_TABLE_ADDRESS_ALIGNMENT (4*MEGABYTE)


/************************************************************************/
//  External Interface:
//  NAME: utilAllocateTces
//     Responsible for allocating TCEs
//
/************************************************************************/
errlHndl_t utilAllocateTces(const uint64_t i_startingAddress,
                            const size_t   i_size,
                            uint32_t&      o_startingToken,
                            const bool     i_rwNotRO)
{
    return Singleton<UtilTceMgr>::instance().allocateTces(i_startingAddress,
                                                          i_size,
                                                          o_startingToken,
                                                          i_rwNotRO);
};

/************************************************************************/
// External Interface:
// NAME: utilDeallocateTces
//     Responsible for deallocating TCEs
//
/************************************************************************/
errlHndl_t utilDeallocateTces(const uint32_t i_startingToken)
{
    return Singleton<UtilTceMgr>::instance().deallocateTces(i_startingToken);
};


/************************************************************************/
// External Interface:
// NAME: utilDisableTces
//     Responsible for disabling TCE on the system, including
//     deallocating TCE Entries and disabling the Processor settings
//
/************************************************************************/
errlHndl_t utilDisableTces(void)
{
    return Singleton<UtilTceMgr>::instance().disableTces();
};

/************************************************************************/
// External Interface:
// NAME: getTceManager
//       Returns a copy of Singleton<UtilTceMgr>::instance()
//
/************************************************************************/
UtilTceMgr&  getTceManager(void)
{
   return Singleton<UtilTceMgr>::instance();
};

errlHndl_t utilSetupPayloadTces(void)
{
    errlHndl_t errl = nullptr;

    uint64_t addr=0x0;
    size_t   size=0x0;
    uint32_t token=0x0;
    uint8_t  nodeId = TARGETING::UTIL::getCurrentNodePhysId();

    do{

    TRACFCOMP(g_trac_tce,ENTER_MRK"utilSetupPayloadTces(): nodeId=0x%X", nodeId);

    // Allocate TCEs for PAYLOAD to Temporary Space
    // -- Address must be HRMOR-specific
    uint64_t hrmorVal = cpu_spr_value(CPU_SPR_HRMOR);
    addr = hrmorVal - VMM_HRMOR_OFFSET + MCL_TMP_ADDR;
    size = MCL_TMP_SIZE;
    TRACUCOMP(g_trac_tce,"utilSetupPayloadTces(): addr=0x%.16llX, hrmor=0x%.16llX, size=0x%X", addr, hrmorVal, size);

    errl = utilAllocateTces(addr, size, token);
    if (errl)
    {
        TRACFCOMP(g_trac_tce,"utilSetupPayloadTces(): ERROR back from utilAllocateTces() for PAYLOAD using addr=0x%.16llX, size=0x%llX", addr, size);
        break;
    }
    else
    {
        TRACUCOMP(g_trac_tce,"utilSetupPayloadTces(): utilAllocateTces() for PAYLOAD: addr=0x%.16llX, size=0x%llX, token=0x%X", addr, size, token);
    }

    // Set attribute to tell FSP what the PAYLOAD token is
    TARGETING::Target* pNodeTgt = TARGETING::UTIL::getCurrentNodeTarget();
    pNodeTgt->setAttr<TARGETING::ATTR_TCE_START_TOKEN_FOR_PAYLOAD>(token);

    // For PSI Diagnostics Test the FSP writes and reads back patterns to the
    // PAYLOAD section via PSI and FSI so it needs to know the starting memory
    // address of this section and have an unsecure read-write memory region
    // opened for it
    pNodeTgt->setAttr<TARGETING::ATTR_START_MEM_ADDRESS_FOR_PAYLOAD_TCE_TOKEN>(addr);

    // Save for internal use since can't trust FSP won't change attribute
    Singleton<UtilTceMgr>::instance().setToken(UtilTceMgr::PAYLOAD_TOKEN,
                                               token);

    // Open Read-Write Unsecure Memory Region
    errl = SBEIO::openUnsecureMemRegion(addr,
                                        size,
                                        true,     //Read-Write
                                        nullptr); //Master Processor

    if (errl)
    {
        TRACFCOMP(g_trac_tce,"utilSetupPayloadTces(): ERROR back from SBEIO::openUnsecureMemRegion() for PAYLOAD using addr=0x%.16llX, size=0x%llX", addr, size);
        break;
    }

    // Allocate TCEs for HDAT
    // -- Address must be HRMOR-specific
    addr = hrmorVal - VMM_HRMOR_OFFSET + HDAT_TMP_ADDR;
    size = HDAT_TMP_SIZE;

    errl = utilAllocateTces(addr, size, token);
    if (errl)
    {
        TRACFCOMP(g_trac_tce,"utilSetupPayloadTces(): ERROR back from utilAllocateTces() for HDAT using addr=0x%.16llX, size=0x%llX", addr, size);
        break;
    }
    else
    {
        TRACUCOMP(g_trac_tce,"utilSetupPayloadTces(): utilAllocateTces() for HDAT: addr=0x%.16llX, size=0x%llX, token=0x%X", addr, size, token);
    }

    // Set attribute to tell FSP what the HDAT token is
    pNodeTgt->setAttr<TARGETING::ATTR_TCE_START_TOKEN_FOR_HDAT>(token);

    // Save for internal use since we can't trust FSP won't change the attribute
    Singleton<UtilTceMgr>::instance().setToken(UtilTceMgr::HDAT_TOKEN,
                                               token);

    } while(0);

    TRACFCOMP(g_trac_tce,EXIT_MRK"utilSetupPayloadTces(): errl_rc=0x%X", ERRL_GETRC_SAFE(errl));

    return errl;
}

errlHndl_t utilClosePayloadTces(void)
{
    errlHndl_t errl = nullptr;

    uint32_t token=0x0;
    uint8_t  nodeId = TARGETING::UTIL::getCurrentNodePhysId();

    do{

    TRACFCOMP(g_trac_tce,ENTER_MRK"utilClosePayloadTces(): nodeId=0x%X", nodeId);

    // Close PAYLOAD TCEs
    token = Singleton<UtilTceMgr>::instance().getToken(UtilTceMgr::PAYLOAD_TOKEN);
    errl = utilDeallocateTces(token);
    if (errl)
    {
        TRACFCOMP(g_trac_tce,"utilClosePayloadTces(): ERROR back from utilDeallocateTces() using token=0x%.8X", token);
        break;
    }

    // Close the Unsecure Memory Region that was opened for the FSP to run
    // PSI Diagnostics Test using the PAYLOAD section
    // -- addr is a constant for PAYLOAD
    // -- Address must be HRMOR-specific
    uint64_t hrmorVal = cpu_spr_value(CPU_SPR_HRMOR);
    uint64_t addr = hrmorVal - VMM_HRMOR_OFFSET + MCL_TMP_ADDR;
    TRACUCOMP(g_trac_tce,"utilClosePayloadTces(): addr=0x%.16llX, hrmor=0x%.16llX", addr, hrmorVal);

    errl = SBEIO::closeUnsecureMemRegion(addr,
                                         nullptr); //Master Processor
    if(errl)
    {
        TRACFCOMP(g_trac_tce,"utilClosePayloadTces(): ERROR back from closeUnsecureMemRegion() using start address=0x%016llX",addr);
        break;
    }

    // Close HDAT TCEs
    token = Singleton<UtilTceMgr>::instance().getToken(UtilTceMgr::HDAT_TOKEN);
    errl = utilDeallocateTces(token);
    if (errl)
    {
        TRACFCOMP(g_trac_tce,"utilClosePayloadTces(): ERROR back from utilDeallocateTces() using token=0x%.8X", token);
        break;
    }


    } while(0);

    TRACFCOMP(g_trac_tce,EXIT_MRK"utilClosePayloadTces(): errl_rc=0x%X", ERRL_GETRC_SAFE(errl));

    return errl;
}


/************************************************************************/
//
// NAME: UtilTceMgr
//      Constructor - set up Tce Table pointers
//
/************************************************************************/
UtilTceMgr::UtilTceMgr(const uint64_t i_tableAddr, const size_t i_tableSize)
  :iv_isTceHwInitDone(false)
  ,iv_isTceTableInitDone(false)
  ,iv_tceTableVaAddr(0)
  ,iv_tceTablePhysAddr(i_tableAddr)
  ,iv_tceEntryCount(0)
  ,iv_tceTableSize(i_tableSize)
  ,iv_payloadToken(INVALID_TOKEN_VALUE)
  ,iv_hdatToken(INVALID_TOKEN_VALUE)
{

    // Need to set up TCE Table with HRMOR-specific Address
    uint64_t hrmorVal = cpu_spr_value(CPU_SPR_HRMOR);
    iv_tceTablePhysAddr = hrmorVal - VMM_HRMOR_OFFSET + TCE_TABLE_ADDR;

    // Table Address must be 4MB Aligned and default input is TCE_TABLE_ADDR
    static_assert( TCE_TABLE_ADDR % TCE_TABLE_ADDRESS_ALIGNMENT == 0,"TCE Table must align on 4 MB boundary");
    assert( iv_tceTablePhysAddr % TCE_TABLE_ADDRESS_ALIGNMENT == 0,"TCE Table must align on 4 MB boundary: 0x%.16llX", iv_tceTablePhysAddr);

    // TCE Entry counts are based on the following assumption
    static_assert((sizeof(uint64_t) == sizeof(TceEntry_t)), "TceEntry_t struct must be size of uint64_t)");

    // i_tableSize must be a multiple of TCE Entries
    assert( i_tableSize % sizeof(TceEntry_t) == 0,"TCE Table Size (0x%llX) must be multiple of TceEntry_t size (0x%X))", i_tableSize, sizeof(TceEntry_t));

    iv_tceEntryCount = iv_tceTableSize/(sizeof (uint64_t));


    TRACUCOMP(g_trac_tce,"UtilTceMgr::UtilTceMgr: iv_tceTableVaAddr=0x%.16llX, iv_tceTablePhysAddr=0x%.16llX, iv_tceTableSize=0x%llX, iv_tceEntryCount=0x%X, iv_allocatedAddrs,size=%d, hrmorVal=0x%.16llX", iv_tceTableVaAddr, iv_tceTablePhysAddr, iv_tceTableSize, iv_tceEntryCount, iv_allocatedAddrs.size(), hrmorVal);

    // Initialize HW without Initializing Table so that FSP cannot DMA
    // to memory without Hostboot control
    auto errl = UtilTceMgr::initTceInHdw();
    if (errl)
    {
        uint32_t errl_plid = errl->plid();
        TRACFCOMP(g_trac_tce,"UtilTceMgr::UtilTceMgr initTceInHdw() failed with rc=0x%X, plid=0x%X. Shutting down",ERRL_GETRC_SAFE(errl), errl_plid);
        errl->setSev(ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM);
        errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
        errlCommit( errl, UTIL_COMP_ID );
        INITSERVICE::doShutdown(errl_plid, true);
    }

};

/**************************************************************************/
//
// NAME: createTceTable
//      Utilty to map the Tce Table
//
/**************************************************************************/
errlHndl_t UtilTceMgr::createTceTable()
{
    errlHndl_t errl = nullptr;

    TRACFCOMP(g_trac_tce,ENTER_MRK"UtilTceMgr::createTceTable: iv_tceTableVaAddr=0x%.16llX, iv_tceTablePhysAddr = 0x%.16llX,iv_tceTableSize = 0x%llX, iv_tceEntryCount=0x%X, iv_isTceTableInitDone=%d", iv_tceTableVaAddr, iv_tceTablePhysAddr, iv_tceTableSize, iv_tceEntryCount, iv_isTceTableInitDone);

    do
    {
        // If init was already run, then skip here
        if (iv_isTceTableInitDone)
        {
            TRACFCOMP(g_trac_tce,"UtilTceMgr::createTceTable: iv_isTceTableInitDone (%d) already true, so skipping init", iv_isTceTableInitDone);
            break;
        }


        // check to make sure the TCE table is not larger than Max Table Size
        if (iv_tceTableSize > MAX_TCE_TABLE_SIZE)
        {
            // TCE table size larger than 32M.. code bug likely as the real
            //   TCE table is a fixed address and size.
            TRACFCOMP(g_trac_tce,"UtilTceMgr::createTceTable: Table size 0x%X too large (>0x%llX) - cannot map.", iv_tceTableSize, MAX_TCE_TABLE_SIZE);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_CREATE_TABLE
             * @reasoncode   Util::UTIL_TCE_INVALID_SIZE
             * @userdata1    Size of of the table that is too large
             * @userdata2    Max TCE Table Size
             * @devdesc      TCE Table size requested too large.
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(
                                           ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_CREATE_TABLE,
                                           Util::UTIL_TCE_INVALID_SIZE,
                                           iv_tceTableSize,
                                           MAX_TCE_TABLE_SIZE,
                                           true /*Add HB SW Callout*/);
            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;

        }


        // Check that iv_tceEntryCount isn't larger than MAX_NUM_TCE_TABLE_ENTRIES
        if (iv_tceEntryCount > MAX_NUM_TCE_TABLE_ENTRIES )
        {
            // TCE Count is larger than TCE Table Can Support
            TRACFCOMP(g_trac_tce,"UtilTceMgr::createTceTable: iv_tceEntryCount 0x%X too large (>0x%llX) - cannot map.", iv_tceEntryCount, MAX_NUM_TCE_TABLE_ENTRIES);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_CREATE_TABLE
             * @reasoncode   Util::UTIL_TCE_INVALID_COUNT
             * @userdata1    Number of TCEs Requested
             * @userdata2    Max Number of TCEs that TCE Table can hold
             * @devdesc      TCE Table size requested too large.
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(
                                           ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_CREATE_TABLE,
                                           Util::UTIL_TCE_INVALID_COUNT,
                                           iv_tceEntryCount,
                                           MAX_NUM_TCE_TABLE_ENTRIES,
                                           true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }

        // Check that the TCE TABLE Address is aligned on 4MB
        if (iv_tceTablePhysAddr % (4*MEGABYTE))
        {
            // Address not page aligned
            TRACFCOMP(g_trac_tce,ERR_MRK"UtilTceMgr::createTceTable: Table Addr 0x%.16llX not aligned on 4MB Boundary", iv_tceTablePhysAddr);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_CREATE_TABLE
             * @reasoncode   Util::UTIL_TCE_ADDR_NOT_ALIGNED
             * @userdata1    Phyiscal Address of the TCE Table
             * @userdata2    <unused>
             * @devdesc      TCE Table not page aligned.
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_CREATE_TABLE,
                                           Util::UTIL_TCE_ADDR_NOT_ALIGNED,
                                           iv_tceTablePhysAddr,
                                           0,
                                           true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }

       // Allocate Memory for TCE Table
       // - Reserve a block of physical memory and get a pointer to
       //   virtual memory to manipulate it
       iv_tceTableVaAddr = reinterpret_cast<uint64_t>(mm_block_map(
                              reinterpret_cast<void*>(iv_tceTablePhysAddr),
                              iv_tceTableSize));

        // Check that a valid Virtual Memory Address was returned
        if (reinterpret_cast<void*>(iv_tceTableVaAddr) == nullptr)
        {
            // Invalid Virtual Address was returned
            TRACFCOMP(g_trac_tce,ERR_MRK"UtilTceMgr::createTceTable: mm_block_map for Table Addr 0x%.16llX and size=0x%X returned NULL", iv_tceTablePhysAddr, iv_tceTableSize);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_CREATE_TABLE
             * @reasoncode   Util::UTIL_ERC_BAD_PTR
             * @userdata1    Physical Address of the TCE Table
             * @userdata2    Requested Size of the TCE Table
             * @devdesc      TCE Table Could Not Be Block-Mapped
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_CREATE_TABLE,
                                           Util::UTIL_ERC_BAD_PTR,
                                           iv_tceTablePhysAddr,
                                           iv_tceTableSize,
                                           true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }

        // Zero out the TCE Table space
        memset(reinterpret_cast<void*>(iv_tceTableVaAddr), 0, iv_tceTableSize);

        // make sure that the memset completes.
        sync();

    }while(0);

    // If succsesfull set init to true
    if ((errl == nullptr) &&
        (iv_isTceTableInitDone == false))
    {
        iv_isTceTableInitDone = true;

        // Successfully initialized the TCE table and hardware.
        TRACUCOMP(g_trac_tce, "UtilTceMgr::createTceTable: TCE Table initialized and setup: iv_isTceTableInitDone=%d", iv_isTceTableInitDone);
    }

    TRACFCOMP(g_trac_tce, EXIT_MRK"UtilTceMgr::createTceTable: iv_tceTableVaAddr=0x%.16llX, iv_tceTablePhysAddr = 0x%.16llX,iv_tceTableSize = 0x%llX, iv_tceEntryCount=0x%X, iv_isTceTableInitDone=%d", iv_tceTableVaAddr, iv_tceTablePhysAddr, iv_tceTableSize, iv_tceEntryCount, iv_isTceTableInitDone);

    return errl;
}


/**************************************************************************/
//
// NAME: initTceInHdw
//      Responsible for setting up the Processors to point to the TCE table
//
/**************************************************************************/
errlHndl_t UtilTceMgr::initTceInHdw()
{
    errlHndl_t errl = nullptr;

    TRACFCOMP(g_trac_tce, ENTER_MRK"UtilTceMgr::initTceInHdw: iv_tceTablePhysAddr = 0x%.16llX, iv_isTceHwInitDone=%d", iv_tceTablePhysAddr, iv_isTceHwInitDone);

    do
    {
        // If init was already run, then skip here
        if (iv_isTceHwInitDone)
        {
            TRACFCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: iv_isTceHwInitDone (%d) already true, so skipping init", iv_isTceHwInitDone);
            break;
        }

        // Loop through the processors and setup PSI Host Bridge for TCE
        TARGETING::TargetHandleList l_cpuTargetList;
        getAllChips(l_cpuTargetList, TARGETING::TYPE_PROC);

        for ( auto l_pTarget : l_cpuTargetList )
        {

            void * PsiBridgeAddr = nullptr;
            INTR::PSIHB_SW_INTERFACES_t * l_psihb_ptr = nullptr;
            TarTceAddrRegister_t l_tar;

            // MMIO Map the PSI Host Bridge
            errl = mapPsiHostBridge(l_pTarget, PsiBridgeAddr);
            if (errl)
            {
                TRACFCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: call to mapPsiHostBridge failed with rc=0x%X, plid=0x%X. Committing Log, but continuing the loop", ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));

                errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
                errlCommit( errl, UTIL_COMP_ID );
                continue;
            }

            TRACUCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: Psi Bridge Addr = 0x%.16llX huid = 0x%.8X", PsiBridgeAddr, TARGETING::get_huid(l_pTarget));

            l_psihb_ptr = reinterpret_cast<INTR::PSIHB_SW_INTERFACES_t*>
                                           (PsiBridgeAddr);

            // Read back PSIHBBAR
            TRACUCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: Read Back psihbbar = 0x%.16llX", l_psihb_ptr->psihbbar);


            // Set TAR - TCE Address Register
            // Put the TCE Table Starting Physical Address and set up the
            // system for max of 512K entries
            l_tar.WholeTAR=0;

            // Since iv_tceTablePhysAddr already checked for 4MB alignment in
            // createTceTable() we can copy it directly in here
            l_tar.WholeTAR = iv_tceTablePhysAddr;
            l_tar.tceEntries = TAR_TCE_ENTRIES_512K;

            TRACFCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: iv_tceTablePhysAddr = 0x%.16llX, l_psihb_ptr=0x%.16llX, TAR=0x%.16llX",iv_tceTablePhysAddr, l_psihb_ptr, l_tar.WholeTAR);

            l_psihb_ptr->tceaddr = l_tar.WholeTAR;

            eieio();

            TRACUCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: Read Back tceaddr = 0x%.16llX", l_psihb_ptr->tceaddr);

            // Turn on TCE enable PSI Host Bridge Secure Register
            l_psihb_ptr->phbsecure = PHBSECURE_TCE_ENABLE;

            eieio();

            TRACUCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: Read Back phbsecure = 0x%.16llX", l_psihb_ptr->phbsecure);

            // Unmap the PSI Host Bridge
            errl = unmapPsiHostBridge(PsiBridgeAddr);
            if (errl)
            {
                TRACFCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: call to unmapPsiHostBridge failed with rc=0x%X, plid=0x%X. Committing Log, but continuing the loop", ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));

                errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
                errlCommit( errl, UTIL_COMP_ID );
                continue;
            }
            TRACUCOMP(g_trac_tce,"UtilTceMgr::initTceInHdw: After unmap: Psi Bridge Addr = 0x%.16llX huid = 0x%.8X", PsiBridgeAddr, TARGETING::get_huid(l_pTarget));

        }

    }while(0);

    // If succsesfull set init to true
    if ((errl == nullptr) &&
        (iv_isTceHwInitDone == false))
    {
        iv_isTceHwInitDone = true;

        // Successfully initialized the TCE table and hardware.
        TRACUCOMP(g_trac_tce, "UtilTceMgr::initTceInHdw: TCE initialized and setup");
    }

    TRACFCOMP(g_trac_tce, EXIT_MRK"UtilTceMgr::initTceInHdw: iv_isTceHwInitDone=%d", iv_isTceHwInitDone);

    return errl;
}


/************************************************************************/
//
//  NAME: allocateTces
//     Responsible for allocating TCE Entries
//
/************************************************************************/
errlHndl_t UtilTceMgr::allocateTces(const uint64_t i_startingAddress,
                                    const size_t   i_size,
                                    uint32_t&      o_startingToken,
                                    const bool     i_rwNotRO)
{
    errlHndl_t errl = nullptr;
    uint32_t numTcesNeeded = 0;
    o_startingToken = 0;

    TceEntry_t *tablePtr = nullptr;

    TRACFCOMP(g_trac_tce,ENTER_MRK"UtilTceMgr::allocateTces: start for addr = 0x%.16llX , size = 0x%X, rwNorRO=%d", i_startingAddress, i_size, i_rwNotRO);

    do
    {
        // Assert if i_size is not greater than zero
        assert(i_size > 0, "UtilTceMgr::allocateTces: i_size = %d, not greater than zero", i_size);

        // Expecting a page-aligned starting address
        if (i_startingAddress % PAGESIZE)
        {
            TRACFCOMP(g_trac_tce,ERR_MRK"UtilTceMgr::allocateTces: ERROR-Address 0x%.16llX not page aligned", i_startingAddress);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_ALLOCATE
             * @reasoncode   Util::UTIL_TCE_ADDR_NOT_ALIGNED
             * @userdata1    Address to start TCE
             * @userdata2    Size of the address space trying to get TCEs
             *               for.
             * @devdesc      The Physical Address for the TCE entry is not
             *               page aligned.
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_ALLOCATE,
                                           Util::UTIL_TCE_ADDR_NOT_ALIGNED,
                                           i_startingAddress,
                                           i_size,
                                           true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }

        // Calculate the number of TCE entries needed - rounding up
        numTcesNeeded = ALIGN_PAGE(i_size)/PAGESIZE;

        // If more than the number of TCEs available are expected or the
        // size is too big then error out
        if ((numTcesNeeded > iv_tceEntryCount) ||
            (i_size > MAX_TCE_MEMORY_SPACE))
        {
            TRACFCOMP(g_trac_tce,ERR_MRK"UtilTceMgr::allocateTces: ERROR - Too many entries (0x%X) requested (>0x%X) (i_size=0x%X)", numTcesNeeded, iv_tceEntryCount, i_size);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_ALLOCATE
             * @reasoncode   Util::UTIL_TCE_INVALID_SIZE
             * @userdata1[0:31]  Number of TCEs Needed
             * @userdate1[32:64] Maximum Number of Tce Entries
             * @userdata2    Size of the address space trying to get TCEs
             * @devdesc      The size requested is too large for the TCE table
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_ALLOCATE,
                                           Util::UTIL_TCE_INVALID_SIZE,
                                           TWO_UINT32_TO_UINT64(
                                             numTcesNeeded,
                                             iv_tceEntryCount),
                                           i_size,
                                           true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }

        // Check to see if we've already allocated TCEs associated with
        // this starting address
        bool previouslyAllocated = false;
        uint32_t pos = 0;
        for ( auto const& map_itr : iv_allocatedAddrs )
        {
           if (map_itr.second.start_addr == i_startingAddress)
           {
               // found the starting address
               previouslyAllocated = true;
               pos = map_itr.first;
               break;
           }
        }

        if(previouslyAllocated)
        {
            // This starting address has already had TCEs allocated for it
            TRACFCOMP(g_trac_tce,"UtilTceMgr::allocateTces: ERROR - This starting address 0x%.16llX already has TCEs allocated (pos=0x%X)", i_startingAddress, pos);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_ALLOCATE
             * @reasoncode   Util::UTIL_TCE_PREVIOUSLY_ALLOCATED
             * @userdata1    Starting Address
             * @userdata2    Starting TCE position in TCE Table
             * @devdesc      The starting address was previously allocated
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_ALLOCATE,
                                           Util::UTIL_TCE_PREVIOUSLY_ALLOCATED,
                                           i_startingAddress,
                                           pos,
                                           true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }

        // Check to see if createTceTable ran before allocate.  If not we
        // need to create the table and make sure it is mapped.
        // If we are in multi-node we would run create on only 1 node and
        // other nodes could use that table
        if (!iv_isTceTableInitDone)
        {
            // createTceTable has not run
            TRACFCOMP(g_trac_tce,"UtilTceMgr::allocateTces: createTceTable has not run so call createTceTable() here.");

            errl = createTceTable();

            if (errl)
            {
                TRACFCOMP(g_trac_tce,"UtilTceMgr::allocateTces: createTceTable() failed with rc=0x%X",ERRL_GETRC_SAFE(errl));
                break;
            }
        }

        // Check to see if HW has been initialized to use the TCEs
        if (!iv_isTceHwInitDone)
        {
            // TceInit did not run before allocate was called so call it here
            TRACFCOMP(g_trac_tce,"UtilTceMgr::allocateTces: initTceInHdw has not run yet: call it now");

            errl = initTceInHdw();

            if (errl)
            {
                TRACFCOMP(g_trac_tce,"UtilTceMgr::allocateTces: initTceInHdw() failed with rc=0x%X",ERRL_GETRC_SAFE(errl));
                break;
            }
        }

        tablePtr = reinterpret_cast<TceEntry_t*>(iv_tceTableVaAddr);

        // Find a first consecutive group of TCEs requested
        uint32_t startingIndex = 0;
        bool found = false;

        // Start at the beginning and search for the first empty entry
        for (uint32_t tceIndex = 0;
             tceIndex < iv_tceEntryCount;
             ++tceIndex)
        {
            // Look for no-write AND no-read access for the entry to be empty
            if (tablePtr[tceIndex].readAccess == 0 &&
                tablePtr[tceIndex].writeAccess == 0)
            {
                uint32_t availIndex = 0;

                // if not enough space avail.
                if (numTcesNeeded+tceIndex > iv_tceEntryCount)
                {
                    break;
                }
                for (uint32_t IndexInRow = tceIndex;
                     IndexInRow < numTcesNeeded + tceIndex;
                     IndexInRow++)
                {
                    // If the entry has no read or write access then the
                    // entry is available
                    if (tablePtr[IndexInRow].readAccess == 0 &&
                        tablePtr[IndexInRow].writeAccess == 0)
                    {
                        // Increment availIndex
                        availIndex++;
                    }
                    // found a valid entry so need to start the count over.
                    else
                    {
                        // increment past the tce entries we already checked
                        tceIndex = IndexInRow+1;

                        // reset the avail index
                        availIndex = 0;

                        break;
                    }

                    // If we found enough consecutive TCE entries
                    if (availIndex >= numTcesNeeded)
                    {
                        // set the starting index
                        startingIndex = tceIndex;
                        // mark it found
                        found = true;
                        break;
                    }
                }
                // break out and update the table
                if (found)
                {
                    break;
                }

                // did not find consecutive TCE entries so continue.
            }
        }

        TRACUCOMP(g_trac_tce,"UtilTceMgr::allocateTces: found=%d, startingIndex=0x%X, numTcesNeeded=0x%X", found, startingIndex, numTcesNeeded);

        if (found)
        {
            // Do a for loop here to loop through the number of TCE entries
            // and set the valid bits.  The real page number gets incremented
            // by 1 for each entry
            for ( uint32_t i = 0, index=startingIndex;
                  i < numTcesNeeded;
                  ++i, ++index )
            {
                tablePtr[index].realPageNumber = (i_startingAddress +
                                                  (i*PAGESIZE))/PAGESIZE;

                if (i_rwNotRO)
                {
                    tablePtr[index].writeAccess = 1;
                }
                tablePtr[index].readAccess = 1;

                TRACDCOMP(g_trac_tce,INFO_MRK"UtilTceMgr::allocateTces: TCE Entry/Token[%d] (hex) = 0x%llX", index, tablePtr[index]);
            }

            // Save And Return Information about Allocated TCEs
            // Key to this map is the token, which is a DMA address that =
            // (Starting Index in TCE Table) * PAGESIZE
            o_startingToken = startingIndex*PAGESIZE;
            iv_allocatedAddrs[o_startingToken].start_addr = i_startingAddress;
            iv_allocatedAddrs[o_startingToken].size = i_size;

            TRACFCOMP(g_trac_tce,"UtilTceMgr::allocateTces: SUCCESSFUL: addr = 0x%.16llX, size = 0x%llX, starting entry=0x%X",i_startingAddress, i_size, startingIndex);
        }
        else  // not found means not enough space for request
        {
            TRACFCOMP(g_trac_tce,"UtilTceMgr::allocateTces: ERROR -Not enough free entries for this request: addr=0x%.16llX, size=0x%llX, startingIndex=0x%X, numTcesNeeded=0x%X", i_startingAddress, i_size, startingIndex, numTcesNeeded);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_ALLOCATE
             * @reasoncode   Util::UTIL_TCE_NOT_ENOUGH_FREE_ENTRIES
             * @userdata1[0:31]  Number of TCEs Needed
             * @userdate1[32:64] Starting Index in TCE Table
             * @userdata2    Size of address space TCEs are tying to map to
             * @devdesc      Requested size is too large to fit into TCE Table
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(
                                 ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                 Util::UTIL_TCE_ALLOCATE,
                                 Util::UTIL_TCE_NOT_ENOUGH_FREE_ENTRIES,
                                 TWO_UINT32_TO_UINT64(
                                   numTcesNeeded,
                                   startingIndex),
                                 i_size,
                                 true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }
    }while(0);

    TRACFCOMP(g_trac_tce, EXIT_MRK"UtilTceMgr::allocateTces: END: addr = 0x%.16llX and size = 0x%X, numTcesNeeded=0x%X. returning o_startingToken=0x%.8X", i_startingAddress, i_size, numTcesNeeded, o_startingToken);
    printIvMap(); //Debug

    return errl;
}


/*************************************************************************/
//
// NAME: deallocateTces
//     Responsible for deallocating TCE Entries
//
/*************************************************************************/
errlHndl_t UtilTceMgr::deallocateTces(const uint32_t i_startingToken)
{

    errlHndl_t errl = nullptr;
    bool isContiguous = true;
    uint32_t startingIndex = 0x0;
    uint64_t startingAddress = 0x0;
    size_t size = 0;

    TceEntry_t *tablePtr = nullptr;

    TRACFCOMP(g_trac_tce,ENTER_MRK"UtilTceMgr::deallocateTces: Token = 0x%.8X", i_startingToken);

    do
    {
        // Assert if i_startingToken is not aligned on PAGESIZE
        assert((i_startingToken % PAGESIZE) == 0, "UtilTceMgr::deallocateTces: i_startingToken (0x%.8X) is not page aligned", i_startingToken);

        std::map<uint32_t, TceEntryInfo_t>::iterator map_itr
                 = iv_allocatedAddrs.find(i_startingToken);
        if( map_itr == iv_allocatedAddrs.end() )
        {
            // Can't find this starting token. Trace that nothing happens,
            // but do not create an error log
            TRACFCOMP(g_trac_tce,INFO_MRK"UtilTceMgr::deallocateTces: Can't find match of Starting Token = 0x%.16llX", i_startingToken);
            break;
        }
        else
        {
            startingIndex = (map_itr->first) / PAGESIZE;
            startingAddress = map_itr->second.start_addr;
            size = map_itr->second.size;
        }

        // Assert if size is not greater than zero
        assert(size > 0, "UtilTceMgr::deallocateTces: i_size = %d, not greater than zero", size);

        // Get number of TCEs needed - rounding up
        uint32_t numTcesNeeded = ALIGN_PAGE(size)/PAGESIZE;

        TRACUCOMP(g_trac_tce,"UtilTceMgr::deallocateTces: size=0x%X, numTcesNeeded=0x%X, startingAddress = 0x%X", size, numTcesNeeded, startingAddress);

        // startingIndex is larger than the max number of indexes avail
        // --OR-- startingIndex and the number of TCEs needed exceeds the
        // number of TCEs in the Table
        if (startingIndex > iv_tceEntryCount ||
            startingIndex+numTcesNeeded > iv_tceEntryCount)
        {
            TRACFCOMP(g_trac_tce,ERR_MRK"UtilTceMgr::deallocateTces:  Invalid startingAddress=0x%X and/or numTcesNeeded=0x%X for table with iv_tceEntryCount=0x%X (startingIndex=0x%X), No deallocate.", startingAddress, numTcesNeeded, iv_tceEntryCount, startingIndex);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_DEALLOCATE
             * @reasoncode   Util::UTIL_TCE_INVALID_SIZE
             * @userdata1[0:31]    starting index
             * @userdata1[32:63]   number of TCEs needed for this request
             * @userdata2    Number of Entries Current TCE Table Supports
             * @devdesc      The size requested is too large based on the
             *               startingAddress the space avilable in the table
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_DEALLOCATE,
                                           Util::UTIL_TCE_INVALID_SIZE,
                                           TWO_UINT32_TO_UINT64(
                                             startingIndex,
                                             numTcesNeeded),
                                           iv_tceEntryCount,
                                           true /*Add HB SW Callout*/);
            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);

            errlCommit(errl,UTIL_COMP_ID);

            break;
        }

        // Currently do not check for valid entries.. Just clear as
        // requested.
        uint64_t previousAddress = 0;

        tablePtr = reinterpret_cast<TceEntry_t*>(iv_tceTableVaAddr);

        for (uint32_t tceIndex = startingIndex;
             tceIndex < (startingIndex + numTcesNeeded);
             tceIndex++)
        {

            // check that the address space is contiguous
            if ((tceIndex != startingIndex) &&
                ((tablePtr[tceIndex].realPageNumber  -
                     previousAddress) != 1))
            {
                isContiguous = false;
                TRACUCOMP(g_trac_tce,INFO_MRK"UtilTceMgr::deallocateTces: isContiguous set to false (%d) for tablePtr[tceIndex=0x%X]=0x%.16llX, previousAddress_RPN=0x%X", isContiguous, tceIndex, tablePtr[tceIndex].WholeTceEntry, previousAddress);
            }

            // Save off real address of page pointed to be this TCE for
            // isContiguous check on next interation
            previousAddress = tablePtr[tceIndex].realPageNumber;

            // Clear out the TCE entry to 0
            tablePtr[tceIndex].WholeTceEntry = 0;
        }

        // Remove the entry from iv_allocatedAddrs even if 'isContiguous' issue
        iv_allocatedAddrs.erase(i_startingToken);

        if (!isContiguous)
        {
            // We know the range to delete is not contingous. The address and
            // size inputs crossesd other allocates.
            // Error log created to indicate this but will clear number of
            // entries requested by caller
            TRACFCOMP(g_trac_tce,"UtilTceMgr::deallocateTces: ERROR - request was not contiguous TCE entries");

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_DEALLOCATE
             * @reasoncode   Util::UTIL_TCE_ENTRY_NOT_CONTIGUOUS
             * @userdata1    Starting address of the TCEs to be deallocated
             * @userdata2    Size of the address space to be deallocated
             * @devdesc      The deallocate went across TCE Allocate space.
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                           Util::UTIL_TCE_DEALLOCATE,
                                           Util::UTIL_TCE_ENTRY_NOT_CONTIGUOUS,
                                           startingAddress,
                                           size,
                                           true /*Add HB SW Callout*/);
            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);

            TRACFCOMP(g_trac_tce,"UtilTceMgr::deallocateTces: not-contiuguos ERROR created (rc=0x%X) and committed", ERRL_GETRC_SAFE(errl));
            errlCommit(errl,UTIL_COMP_ID);
            break;
        }

    }while(0);

    TRACFCOMP(g_trac_tce,"UtilTceMgr::deallocateTces: COMPLETE for Token = 0x%.8X, Addr = 0x%.16llX for size = 0x%X, errl=0x%X",i_startingToken, startingAddress, size, ERRL_GETRC_SAFE(errl));
    printIvMap(); //Debug

    return errl;
}


/**************************************************************************/
//
// NAME: disableTce
//      Deallocate any TCE entries and disable HW settings
//
/**************************************************************************/
errlHndl_t UtilTceMgr::disableTces(void)
{
    errlHndl_t errl = nullptr;
    int64_t rc = 0;

    TRACUCOMP(g_trac_tce,ENTER_MRK"UtilTceMgr::disableTces: iv_isTceHwInitDone=%d, iv_tceTableVaAddr=0x%.16llX iv_isTceTableInitDone=%d", iv_isTceHwInitDone, iv_tceTableVaAddr, iv_isTceTableInitDone );

    do {

    // If the HW was initialized to use TCEs then disable those settings
    // it needs to be released here
    if (iv_isTceHwInitDone==true)
    {
        // Loop through the processors and clear the TCE-related registers
        // in the PSI Host Bridge
        TARGETING::TargetHandleList l_cpuTargetList;
        getAllChips(l_cpuTargetList, TARGETING::TYPE_PROC);

        for ( auto l_pTarget : l_cpuTargetList )
        {
            void * PsiBridgeAddr = nullptr;
            INTR::PSIHB_SW_INTERFACES_t * l_psihb_ptr = nullptr;

            // MMIO Map the PSI Host Bridge
            errl = mapPsiHostBridge(l_pTarget, PsiBridgeAddr);
            if (errl)
            {
                TRACFCOMP(g_trac_tce,"UtilTceMgr::disableTces: call to mapPsiHostBridge failed with rc=0x%X, plid=0x%X. Committing Log, but continuing the loop", ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl)); 

                errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
                errlCommit( errl, UTIL_COMP_ID );
                continue;
            }

            TRACUCOMP(g_trac_tce,"UtilTceMgr::disableTces: Psi Bridge Addr = 0x%.16llX huid = 0x%.8X", PsiBridgeAddr, TARGETING::get_huid(l_pTarget));

            l_psihb_ptr = reinterpret_cast<INTR::PSIHB_SW_INTERFACES_t*>
                                           (PsiBridgeAddr);

            // Read back PSIHBBAR
            TRACUCOMP(g_trac_tce,"UtilTceMgr::disableTces: Read Back psihbbar = 0x%.16llX", l_psihb_ptr->psihbbar);

            // Turn off TCE enable PSI Host Bridge Secure Register
            l_psihb_ptr->phbsecure = 0x0;

            eieio();

            TRACUCOMP(g_trac_tce,"UtilTceMgr::disableTces: Read Back phbsecure = 0x%.16llX", l_psihb_ptr->phbsecure);

            // Clear TAR - TCE Address Register
            l_psihb_ptr->tceaddr = 0x0;

            eieio();

            TRACUCOMP(g_trac_tce,"UtilTceMgr::disableTces: Read Back tceaddr = 0x%.16llX", l_psihb_ptr->tceaddr);

            // Unmap the PSI Host Bridge
            errl = unmapPsiHostBridge(PsiBridgeAddr);
            if (errl)
            {
                TRACFCOMP(g_trac_tce,"UtilTceMgr::disableTces: call to unmapPsiHostBridge failed with rc=0x%X, plid=0x%X. Committing Log, but continuing the loop", ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));

                errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
                errlCommit( errl, UTIL_COMP_ID );
                continue;
            }
            TRACUCOMP(g_trac_tce,"UtilTceMgr::disableTces: After unmap: Psi Bridge Addr = 0x%.16llX huid = 0x%.8X", PsiBridgeAddr, TARGETING::get_huid(l_pTarget));

        }

        // Clear the class variable
        iv_isTceHwInitDone=false;

    }
    else
    {
        TRACFCOMP(g_trac_tce,"UtilTceMgr::disableTces: No Need To Uninitialize HW: iv_isTceHwInitDone=%d", iv_isTceHwInitDone);
    }

    // Cleanup TCE Table In Memory
    if ( (iv_tceTableVaAddr!= 0) && (iv_isTceTableInitDone==true))
    {

        // Clear TCE Table
        memset(reinterpret_cast<void*>(iv_tceTableVaAddr), 0, iv_tceTableSize);

        // Unmap TCE Table In Memory
        rc = mm_block_unmap(reinterpret_cast<void*>(iv_tceTableVaAddr));

        if ( rc )
        {
            // Got a bad rc from mm_block_unmap
            TRACFCOMP(g_trac_tce, "UtilTceMgr::disableTces: mm_unmap_block failed: rc = 0x%.16llX, iv_tceTableVaAddr=0x%.16llX iv_tceTablePhysAddr=0x%X", rc, iv_tceTableVaAddr, iv_tceTablePhysAddr, rc);

            /*@
             * @errortype
             * @moduleid     Util::UTIL_TCE_DISABLE_TCES
             * @reasoncode   Util::UTIL_TCE_BLOCK_UNMAP_FAIL
             * @userdata1    Starting virtual address of pages to be removed
             * @userdata2    Return Code from mm_block_unmap
             * @devdesc      mm_block_unmap failed for TCE Table
             * @custdesc     A problem occurred during the IPL of the system
             */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           Util::UTIL_TCE_DISABLE_TCES,
                                           Util::UTIL_TCE_BLOCK_UNMAP_FAIL,
                                           iv_tceTableVaAddr,
                                           rc,
                                           true /*Add HB SW Callout*/);

            errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
            break;
        }

        // Clear the class variable
        iv_isTceTableInitDone=false;
    }
    else
    {
        TRACFCOMP(g_trac_tce,"UtilTceMgr::disableTces: iv_tceTableVaAddr == NULL (0x%.16llX) - No Cleanup of TCE Table", iv_tceTableVaAddr);
    }

    // Clear allocated addresses map
    iv_allocatedAddrs.clear();

    }while(0);

    TRACUCOMP(g_trac_tce,EXIT_MRK"UtilTceMgr::disableTces");
    printIvMap(); //Debug

    return errl;
}


/**************************************************************************/
//
// NAME: ~UtilTceMgr
//      Destructor
//
/**************************************************************************/
UtilTceMgr::~UtilTceMgr()
{
    errlHndl_t errl = nullptr;

    TRACUCOMP(g_trac_tce,"UtilTceMgr::~UtilTceMgr");

    // Call disableTce in case it hasn't already been called
    errl = disableTces();

    if (errl)
    {
        TRACFCOMP(g_trac_tce,"UtilTceMgr::~UtilTceMgr: disableTces Failed rc=0x%X. Committing plid=0x%X", ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));
        errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
        errlCommit( errl, UTIL_COMP_ID );
    }

    iv_allocatedAddrs.clear();
}

// Debug for printing out iv_allocatedAddrs map
void UtilTceMgr::printIvMap(void) const
{
    TRACUCOMP(g_trac_tce,"UtilTceMgr::printIvMap: size=%d", iv_allocatedAddrs.size());

    for ( auto const& map_itr : iv_allocatedAddrs )
    {
        TRACDCOMP(g_trac_tce,"UtilTceMgr: printIvMap: token=0x%.8X, addr=0x%.16llX, size=0x%.8X", map_itr.first, map_itr.second.start_addr, map_itr.second.size);
    }
}


/**************************************************************************/
//
// NAME: mapPsiHostBridge:
//       Helper function to Memory Map PSI Host Bridge
//
/**************************************************************************/
errlHndl_t UtilTceMgr::mapPsiHostBridge(const TARGETING::Target * i_tgt,
                                        void *& o_psihb_ptr) const
{
    errlHndl_t errl = nullptr;
    void * l_ptr = nullptr;
    o_psihb_ptr = nullptr;

    // Assert if i_tgt is NULL or not a Processor
    assert((i_tgt != nullptr) &&
           (i_tgt->getAttr<TARGETING::ATTR_TYPE>() == TARGETING::TYPE_PROC ),
           "UtilTceMgr::mapPsiHostBridge: i_tgt=0x%X either NULL or !TYPE_PROC",
           TARGETING::get_huid(i_tgt));

    uint64_t PsiBridgeAddr =
               i_tgt->getAttr<TARGETING::ATTR_PSI_BRIDGE_BASE_ADDR>();

    TRACUCOMP(g_trac_tce,ENTER_MRK"UtilTceMgr::mapPsiHostBridge:Psi Bridge Addr = 0x%.16llX huid = 0x%.8X", PsiBridgeAddr, TARGETING::get_huid(i_tgt));

    // Assert if the PSI_BRIDEG_BASE_ADDR is zero or not page aligned
    assert((PsiBridgeAddr != 0) &&
           ((PsiBridgeAddr - ALIGN_PAGE_DOWN(PsiBridgeAddr)) == 0),
           "PsiBridgeAddr (0x%.16llX) is ZERO or not Page Aligned",
           PsiBridgeAddr);

    // Map the device for the PSI_BRIDGE_ADDR
    l_ptr = mmio_dev_map(reinterpret_cast<void*>(PsiBridgeAddr), PAGESIZE);

    if (l_ptr == nullptr)
    {
        // Got a bad rc from device Map
        TRACFCOMP(g_trac_tce, "UtilTceMgr::mapPsiHostBtidge: Device map error.");

        /*@
         * @errortype
         * @moduleid     Util::UTIL_TCE_MAP_PSIHB
         * @reasoncode   Util::UTIL_TCE_DEV_MAP_FAIL
         * @userdata1    Address to be mapped PsiBridgeAddr
         * @userdata2    Target Unit Id
         * @devdesc      PSI Bridge device Map failed
         * @custdesc     A problem occurred during the IPL of the
         *               system
         */
        errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                       Util::UTIL_TCE_MAP_PSIHB,
                                       Util::UTIL_TCE_DEV_MAP_FAIL,
                                       PsiBridgeAddr,
                                       TARGETING::get_huid(i_tgt),
                                       true /*Add HB SW Callout*/);

        errl->addHwCallout( i_tgt,
                            HWAS::SRCI_PRIORITY_HIGH,
                            HWAS::DELAYED_DECONFIG,
                            HWAS::GARD_NULL );

        errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
    }

    o_psihb_ptr = l_ptr;

    TRACUCOMP(g_trac_tce,EXIT_MRK"UtilTceMgr::mapPsiHostBridge: o_psihb_ptr=0x%.16llX, Psi Bridge Addr = 0x%.16llX, huid = 0x%.8X", o_psihb_ptr, PsiBridgeAddr, TARGETING::get_huid(i_tgt));

    return errl;
}


/**************************************************************************/
//
// NAME: unmapPsiHostBridge:
//       Helper function to Unmap PSI Host Bridge from Memory
//
/**************************************************************************/
errlHndl_t UtilTceMgr::unmapPsiHostBridge(void *& io_psihb_ptr) const
{
    errlHndl_t errl = nullptr;
    int64_t rc = 0;

    TRACUCOMP(g_trac_tce,ENTER_MRK"UtilTceMgr::unmapPsiHostBridge: "
                                  "io_psihb_ptr = %p", io_psihb_ptr);

    // unmap the device..
    rc = mmio_dev_unmap(io_psihb_ptr);

    if (rc != 0)
    {
        // Got a bad rc from device unmap
        TRACFCOMP(g_trac_tce, "UtilTceMgr::unmapPsiHostBridge: device unmap "
                              "error: rc=0x%X", rc);

        /*@
         * @errortype
         * @moduleid     Util::UTIL_TCE_UNMAP_PSIHB
         * @reasoncode   Util::UTIL_TCE_DEV_UNMAP_FAIL
         * @userdata1    Address to be unmapped
         * @userdata2    Return Code of mmio_dev_unmap
         * @devdesc      PSI Bridge device Map failed
         * @custdesc     A problem occurred during the IPL of the
         *               system
         */
        errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                       Util::UTIL_TCE_UNMAP_PSIHB,
                                       Util::UTIL_TCE_DEV_UNMAP_FAIL,
                                       reinterpret_cast<uint64_t>(io_psihb_ptr),
                                       rc,
                                       true /*Add HB SW Callout*/);

        errl->collectTrace(UTILTCE_TRACE_NAME,KILOBYTE);
    }
    else
    {
        io_psihb_ptr = nullptr;
        TRACUCOMP(g_trac_tce,EXIT_MRK"UtilTceMgr::unmapPsiHostBridge Successful");
    }

    return errl;
}

/**************************************************************************/
//
// NAME: getToken:
//       Returns one of two internally stored tokens
//
/**************************************************************************/

uint32_t UtilTceMgr::getToken(const tokenLabels i_tokenLabel)
{
    assert((i_tokenLabel==UtilTceMgr::PAYLOAD_TOKEN)||(i_tokenLabel==UtilTceMgr::HDAT_TOKEN),"UtilTceMgr::getToken bad input parm: 0x%X", i_tokenLabel);

    return (i_tokenLabel==UtilTceMgr::PAYLOAD_TOKEN)
            ? iv_payloadToken : iv_hdatToken;

}

/**************************************************************************/
//
// NAME: setToken:
//       Sets one of two internally stored tokens
//
/**************************************************************************/
void UtilTceMgr::setToken(const tokenLabels i_tokenLabel,
                          const uint32_t i_tokenValue)
{
    assert((i_tokenLabel==UtilTceMgr::PAYLOAD_TOKEN)||(i_tokenLabel==UtilTceMgr::HDAT_TOKEN),"UtilTceMgr::setToken bad input parm: 0x%X", i_tokenLabel);

    if (i_tokenLabel==UtilTceMgr::PAYLOAD_TOKEN)
    {
        iv_payloadToken = i_tokenValue;
    }
    else
    {
        iv_hdatToken = i_tokenValue;
    }

    return;
}


/******************************************************/
/* Miscellaneous Functions                            */
/******************************************************/
bool utilUseTcesForDmas(void)
{
    bool retVal = false;

    if (INITSERVICE::spBaseServicesEnabled())
    {
        // @TODO RTC 187906 - This could eventually default to true in all cases
        // where was have a FSP

        // Get Target Service and the system target to get ATTR_USE_TCES_FOR_DMA
        TARGETING::TargetService& tS = TARGETING::targetService();
        TARGETING::Target* sys = nullptr;
        (void) tS.getTopLevelTarget( sys );
        assert(sys, "utilUseTcesForDmas() system target is NULL");

        retVal = sys->getAttr<TARGETING::ATTR_USE_TCES_FOR_DMAS>();
    }

    TRACFCOMP(g_trac_tce,INFO_MRK"utilUseTcesForDmas: %s",
              retVal ? "TRUE" : "FALSE");

    return retVal;
}

errlHndl_t utilEnableTcesWithoutTceTable(void)
{
    errlHndl_t errl = nullptr;

    // This will call the constructor, which in turn will initialize the
    // HW to point at a TCE Table with invalid entries
    Singleton<UtilTceMgr>::instance();

    return errl;

}

}; // namespace TCE

#endif
OpenPOWER on IntegriCloud