summaryrefslogtreecommitdiffstats
path: root/src/usr/runtime/tce.C
blob: 0bfce6cbbd16206d00ce968a5dbcf00fd0be56f1 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/runtime/tce.C $                                       */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2013,2014              */
/*                                                                        */
/* 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 __TCE_C
#define __TCE_C

#include <trace/interface.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <vmmconst.h>
#include <runtime/tceif.H>
#include <sys/mmio.h>
#include <util/align.H>
#include <sys/mm.h>
#include <targeting/common/commontargeting.H>
#include <targeting/common/utilFilter.H>
#include <kernel/console.H>
#include "tce.H"
#include <runtime/runtime_reasoncodes.H>
#include <assert.h>

trace_desc_t* g_trac_tce = NULL;
TRAC_INIT(&g_trac_tce, TCE_TRACE_NAME, 4*KILOBYTE);


namespace TCE
{
    /*************************************************************************/
    // External Interface.
    // NAME: createTceTable Table.
    //      Responsible for initalizing the TCE entries
    //
    /*************************************************************************/
    errlHndl_t createTceTable()
    {
       return Singleton<TceMgr>::instance().createTceTable();
    };

    /************************************************************************/
    // External Interface.
    // NAME: InitTceInHdw
    //      Responsible for setting up the Processors to point to the TCE table
    //
    /************************************************************************/
    errlHndl_t initTceInHdw()
    {
       return Singleton<TceMgr>::instance().initTceInHdw();
    };

    /************************************************************************/
    //  External Interface:
    //  NAME: allocateTces
    //     Responsible for allocating TCE Entries
    //
    /************************************************************************/
    errlHndl_t allocateTces(uint64_t startingAddress, uint64_t size, uint64_t&
                            startingToken)
    {

        return Singleton<TceMgr>::instance().allocateTces(startingAddress,
                                                           size,
                                                           startingToken);
    };

    /************************************************************************/
    // External Interface:
    // NAME: deallocateTces
    //     Responsible for deallocating TCE Entries
    //
    /************************************************************************/
    errlHndl_t deallocateTces(uint64_t startingToken, uint64_t size)
    {
        return Singleton<TceMgr>::instance().deallocateTces(startingToken,
                                                             size);
    };

};

    /************************************************************************/
    //
    // NAME: TceMgr
    //      Constructor - set up Tce Table pointers
    //
    /************************************************************************/
    TceMgr::TceMgr(uint64_t i_tableAddr, uint64_t i_tableSize)
      :tceEntryInit(0)
      ,tceTableVaPtr(NULL)
      ,tceTablePhysAddr(i_tableAddr)
      ,maxTceEntries(0)
      ,tceTableSize(i_tableSize)
    {
            maxTceEntries = tceTableSize/(sizeof (uint64_t));
    };

    /**************************************************************************/
    //
    // NAME: mapTceTable
    //      Utilty to map the Tce Table
    //
    /**************************************************************************/
    errlHndl_t TceMgr::mapTceTable()
    {
        errlHndl_t errl = NULL;

        do
        {

            // check to make sure the TCE table is not larger than 32M..
            if (tceTableSize > THIRTYTWO_MB)
            {

                // TCE table size larger than 32M.. code bug likely as the real
                // TCE table is a fixed address and size.
                TRACFCOMP(g_trac_tce,"TceMgr::mapTceTable: Table size too large..cannot map.");

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_MAP
                 * @reasoncode   RUNTIME::RC_TCE_INVALID_SIZE
                 * @userdata1    Address of the TCE Table
                 * @userdata2    Size of of the table that is too large?
                 * @devdesc      TCE Table size requested too large.
                 */
                errl = new ERRORLOG::ErrlEntry(
                                               ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               RUNTIME::MOD_TCE_MAP,
                                               RUNTIME::RC_TCE_INVALID_SIZE,
                                               tceTablePhysAddr,
                                               tceTableSize,
                                               true /*Add HB SW Callout*/);
                break;

            }

            // Is the TCE TABLE Address page aligned?
            if (tceTablePhysAddr - ALIGN_PAGE_DOWN(tceTablePhysAddr)!=0)
            {
                // Address not page aligned
                TRACFCOMP(g_trac_tce,"TceMgr::mapTceTable: Table Addr not page aligned.");

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_MAP
                 * @reasoncode   RUNTIME::RC_TCE_ADDR_NOT_ALIGNED
                 * @userdata1    Address of the TCE Table
                 * @userdata2    none
                 * @devdesc      TCE Table not page aligned.
                 */
                errl = new ERRORLOG::ErrlEntry(
                                               ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               RUNTIME::MOD_TCE_MAP,
                                               RUNTIME::RC_TCE_ADDR_NOT_ALIGNED,
                                               tceTablePhysAddr,
                                               0,
                                               true /*Add HB SW Callout*/);
                break;
            }


            // If the physical address is less than the VMM size, then the
            // address we are mapping in on the heap and is already mapped.
            // NOTE:  This is needed for testing a TCE table outside of the
            // default TCE table location.
            if (tceTablePhysAddr < VMM_MEMORY_SIZE)
            {
                tceTableVaPtr = reinterpret_cast<TceEntry *>(tceTablePhysAddr);

            }
            else
            {
                // Map the Physical Tce table Pointer
                tceTableVaPtr =
                  reinterpret_cast<TceEntry *>(
                       mmio_dev_map(reinterpret_cast<void*>(tceTablePhysAddr),
                                    THIRTYTWO_MB));

                if (tceTableVaPtr == NULL)
                {
                    // Got a bad rc from dev Map
                    TRACFCOMP(g_trac_tce, "TceMgr::mapTceTable: Device map error.");

                    /*@
                     * @errortype
                     * @moduleid     RUNTIME::MOD_TCE_MAP
                     * @reasoncode   RUNTIME::RC_TCE_DEV_MAP_FAIL
                     * @userdata1    Address to be mapped
                     * @userdata2    return Code from DevMap
                     * @devdesc      Device Map Fail
                     */
                    errl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                    RUNTIME::MOD_TCE_MAP,
                                    RUNTIME::RC_TCE_DEV_MAP_FAIL,
                                    tceTablePhysAddr,
                                    tceTableSize,
                                    true /*Add HB SW Callout*/);
                }
            }
        }while(0);

        return errl;
    }

    /**************************************************************************/
    //
    // NAME: createTceTable
    //      Responsible for initalizing the TCE entries
    //
    /**************************************************************************/
    errlHndl_t TceMgr::createTceTable()
    {

        errlHndl_t errl = NULL;

        TRACFCOMP(g_trac_tce,"TceMgr::creatTceTable: tceTablePhysAddr = %llx tceTableSize = %llx",tceTablePhysAddr, tceTableSize);

        do
        {
            // Map the Tce Table
            errl = mapTceTable();

            if (errl != NULL)
            {
               break;
            }

            // Zero out the TCE table space.
            memset(tceTableVaPtr, 0, tceTableSize);

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

        }while(0);

       return errl;
    };

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

        TRACFCOMP(g_trac_tce,
                  "TceMgr::InitTceInHdw: tceTablePhysAddr = %llx",
                  tceTablePhysAddr);
        do
        {

            // Loop through the processors and read the PSI_BRIDGE_ADDR
            TARGETING::TargetHandleList l_cpuTargetList;
            getAllChips(l_cpuTargetList, TARGETING::TYPE_PROC);

            // Map a device at the PSI_BRIDE_ADDR -
            //            <attribute><id>PSI_BRIDGE_BASE_ADDR</id>
            //            <default>0x0003FFFE80000000</default>
            uint64_t *mmio_ptr = NULL;

            // set up the registers for TCE on all procs
            for (TARGETING::TargetHandleList::const_iterator
                 l_cpuIter = l_cpuTargetList.begin();
                 l_cpuIter != l_cpuTargetList.end();
                 ++l_cpuIter)
            {
                const TARGETING::Target* l_pTarget = *l_cpuIter;
                uint64_t PsiBridgeAddr =
                  l_pTarget->getAttr<TARGETING::ATTR_PSI_BRIDGE_BASE_ADDR>();

                TRACDCOMP(g_trac_tce,"TceMgr::InitTceInHdw:Psi Bridge Addr = %llx huid = %.8X",PsiBridgeAddr, TARGETING::get_huid(l_pTarget));

                // Check if the PSI_BRIDEG_BASE_ADDR is nonzero.. (could be for
                // Tuleta)
                if (PsiBridgeAddr != 0)
                {
                    // If the PsiBridgeAddr is not page aligned. assert.
                    assert((PsiBridgeAddr - ALIGN_PAGE_DOWN(PsiBridgeAddr)) ==
                           0);

                    // Map the device for the PSI_BRIDGE_ADDR
                    mmio_ptr =
                      static_cast<uint64_t*>(
                        mm_block_map(
                          reinterpret_cast<void*>(PsiBridgeAddr),
                                                   PAGESIZE));

                    if (mmio_ptr == NULL)
                    {
                        // Got a bad rc from device Map
                        TRACFCOMP(g_trac_tce, "TceMgr::_createTceTable: Device map error.");

                        /*@
                         * @errortype
                         * @moduleid     RUNTIME::MOD_TCE_INIT_HDW
                         * @reasoncode   RUNTIME::RC_TCE_DEV_MAP_FAIL
                         * @userdata1    Address to be mapped PsiBridgeAddr
                         * @userdata2    Tce Phys Addr
                         * @devdesc      PSI Bridge device Map failed
                         */
                        errl = new ERRORLOG::ErrlEntry(
                                         ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                         RUNTIME::MOD_TCE_INIT_HDW,
                                         RUNTIME::RC_TCE_DEV_MAP_FAIL,
                                         PsiBridgeAddr,
                                         tceTablePhysAddr,
                                         true /*Add HB SW Callout*/);
                        break;
                    }


                    // NOTE>> WILL MAKE DEBUG WHEN DONE TESTING
                    TRACFCOMP(g_trac_tce,"TceMgr::InitTceInHdw:phys addr = %llx",tceTablePhysAddr);

                    // Put the physical TCE addr in PsiBridgeAddr + 18 this is
                    // byte offset but since we are uin64_t increment 3 double
                    // words.
                    *(mmio_ptr + 0x3) = tceTablePhysAddr;

                    eieio();

                    // NOTE>> WILL MAKE DEBUG WHEN DONE TESTING
                    TRACFCOMP(g_trac_tce,"TceMgr::InitTceInHdw:physaddr in Hardware = %llx",*(mmio_ptr + 0x3));

                    // Turn on TCE enable (MMIO offset 0x90)
                    // the mmio_ptr is uint64_t
                    *(mmio_ptr + 0x12) = 0x1;

                    // NOTE>> WILL MAKE DEBUG WHEN DONE TESTING
                    TRACFCOMP(g_trac_tce,"TceMgr::InitTceInHdw:Set MMIO offset 0x90 = %llx",*(mmio_ptr + 0x12));

                    // unmap the device..
                    uint64_t rc =
                      mm_block_unmap(reinterpret_cast<void*>(mmio_ptr));

                    if (rc != 0)
                    {
                        // Got a bad rc from device unmap
                        TRACFCOMP(g_trac_tce,
                                  "TceMgr::initTce: device unmap error.");

                        /*@
                         * @errortype
                         * @moduleid     RUNTIME::MOD_TCE_INIT_HDW
                         * @reasoncode   RUNTIME::RC_TCE_DEV_UNMAP_FAIL
                         * @userdata1    Virtual Addr
                         * @userdata2    return Code from devUnMap
                         * @devdesc      Device UnMap Failure
                         */
                        errl = new ERRORLOG::ErrlEntry(
                                       ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                       RUNTIME::MOD_TCE_INIT_HDW,
                                       RUNTIME::RC_TCE_DEV_UNMAP_FAIL,
                                       reinterpret_cast<uint64_t>(mmio_ptr),
                                       rc,
                                       true /*Add HB SW Callout*/);
                        break;
                    }

                    mmio_ptr = NULL;
                }
            }

        }while(0);

        // If succsesfull set init to 1
        if (errl == NULL)
        {
            tceEntryInit = 1;

            // Successfully initialized the TCE table and hardware.
            TRACFCOMP(g_trac_tce, "TceMgr::_initTceInHdw: TCE initialized and setup");
        }

        return errl;
    }


    /************************************************************************/
    //
    //  NAME: allocateTces
    //     Responsible for allocating TCE Entries
    //
    /************************************************************************/
    errlHndl_t TceMgr::allocateTces(uint64_t i_startingAddress,
                                       uint64_t i_size,
                                       uint64_t& o_startingToken)
    {
        errlHndl_t errl = NULL;

        TRACFCOMP(g_trac_tce,
                   "TceMgr::AllocateTce: start for addr = %llx and size = %llx",
                    i_startingAddress, i_size);

        // Default the starting Token to invalid Token.
        o_startingToken = INVALID_TOKEN_ENTRY;

        do
        {

            // Check to see if init was run.. If not then error...
            if (!tceEntryInit)
            {
                // TceInit did not run before allocate was called
                TRACFCOMP(g_trac_tce,"TceMgr::AllocateTce: ERROR-initTceInHdw has not run");

                // error out because allocate was called before INIT.. Could
                // possibly just call init here.. but need to verify exactly
                // WHEN the init can get called..

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_ALLOCATE
                 * @reasoncode   RUNTIME::RC_TCE_INIT_NOT_RUN
                 * @userdata1    Address to start TCE
                 * @userdata2    Size of the address space tring to get TCEs
                 *               for.
                 * @devdesc      TCE Table has not been initialized yet
                 */
                errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               RUNTIME::MOD_TCE_ALLOCATE,
                                               RUNTIME::RC_TCE_INIT_NOT_RUN,
                                               i_startingAddress,
                                               i_size,
                                               true /*Add HB SW Callout*/);
                errl->collectTrace(TCE_TRACE_NAME,KILOBYTE);

                break;
            }

            // Check to see if createTceTable ran before allocate.  If not we
            // need to make sure the hardware is mapped.. If we are in
            // multi-node we would run Create on only 1 node and other node
            // could use that table to we don't need to create the table twice.
            if (tceTableVaPtr == NULL)
            {
                // createTceTable has not run
                TRACFCOMP(g_trac_tce,"TceMgr::AllocateTce: ERROR - createTceTable has not run so doing the mapping here.");

                errl = mapTceTable();

                if (errl != NULL)
                {
                    break;
                }
            }

            //if not page aligned.. expecting a page aligned address passed in
            if (i_startingAddress - ALIGN_PAGE_DOWN(i_startingAddress) != 0)
            {
                TRACFCOMP(g_trac_tce,"TceMgr::AllocateTce: ERROR-Address not page aligned");

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_ALLOCATE
                 * @reasoncode   RUNTIME::RC_TCE_ADDR_NOT_ALIGNED
                 * @userdata1    Address to start TCE
                 * @userdata2    Size of the address space tring to get TCEs
                 *               for.
                 * @devdesc      The Physical Address for the TCE entry is not
                 *               page aligned.
                 */
                errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               RUNTIME::MOD_TCE_ALLOCATE,
                                               RUNTIME::RC_TCE_ADDR_NOT_ALIGNED,
                                               i_startingAddress,
                                               i_size,
                                               true /*Add HB SW Callout*/);
                break;
            }

            // Calculate the number of TCE entries needed
            uint32_t numTcesNeeded = ALIGN_PAGE_DOWN(i_size)/PAGESIZE;

            // If more than the number of TCE entry avail.. error out.
            if (numTcesNeeded > maxTceEntries)
            {
                TRACFCOMP(g_trac_tce,"TceMgr::AllocateTce: ERROR - Too many entries requested");

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_ALLOCATE
                 * @reasoncode   RUNTIME::RC_TCE_INVALID_SIZE
                 * @userdata1    Address to start TCE
                 * @userdata2    Size of the address space tring to get TCEs
                 *               for.
                 * @devdesc      The size requested is too large for the table
                 */
                errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               RUNTIME::MOD_TCE_ALLOCATE,
                                               RUNTIME::RC_TCE_INVALID_SIZE,
                                               i_startingAddress,
                                               i_size,
                                               true /*Add HB SW Callout*/);
                break;
            }

            // Find a first consequetive group of TCE entries requested
            int startingIndex = 0;
            bool found = false;

            // Start at the beginning and search for the first empty entry
            for (uint32_t tceIndex = 0;
                 tceIndex < maxTceEntries;
                 tceIndex++)
            {
                if (!tceTableVaPtr[tceIndex].valid)
                {
                    uint32_t availIndex = 0;

                    // if not enough space avail.
                    if (numTcesNeeded+tceIndex > maxTceEntries)
                    {
                        break;
                    }
                    for (uint32_t IndexInRow = tceIndex;
                         IndexInRow < numTcesNeeded + tceIndex;
                         IndexInRow++)
                    {
                        // If the entry is Not valid.. then the entry is
                        // available
                        if (!tceTableVaPtr[IndexInRow].valid)
                        {
                            // 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 entires
                        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.
                }
            }


            if (found)
            {
                // Do a for loop here to loop through the number of TCE entries
                // and set the valid bits.. read address changes.. have to add
                // PAGESIZE to each address
                for (uint32_t i = startingIndex;
                              i<numTcesNeeded+startingIndex;
                              i++)
                {
                    tceTableVaPtr[i].realPageNumber = i_startingAddress +
                                                     (i*PAGESIZE);
                    tceTableVaPtr[i].valid = 1;
                    tceTableVaPtr[i].writeAccess = 1;
                    tceTableVaPtr[i].readAccess = 1;
                }

                // We are returning offset into the TCE table for the start of
                // the first TCE entry.
                o_startingToken = startingIndex * PAGESIZE;

                TRACFCOMP(g_trac_tce,"TceMgr::AllocateTce: Token = %llx for addr = %llx and size = %llx",o_startingToken, i_startingAddress, i_size);
            }
            else  // not found means not enough space for request
            {
                TRACFCOMP(g_trac_tce,"TceMgr::AllocateTce: ERROR -Not enough free entries for this request");

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_ALLOCATE
                 * @reasoncode   RUNTIME::RC_TCE_NOT_ENOUGH_FREE_ENTRIES
                 * @userdata1    Address to start TCE
                 * @userdata2    Size of the address space trying to get TCEs
                 *               for.
                 * @devdesc      The size requested is too large.
                 */
                errl = new ERRORLOG::ErrlEntry(
                                        ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                        RUNTIME::MOD_TCE_ALLOCATE,
                                        RUNTIME::RC_TCE_NOT_ENOUGH_FREE_ENTRIES,
                                        i_startingAddress,
                                        i_size,
                                        true /*Add HB SW Callout*/);
                break;
            }
        }while(0);

        TRACFCOMP(g_trac_tce, "TceMgr::AllocateTce: EXIT");

        return errl;
    }


    /*************************************************************************/
    //
    // NAME: deallocateTces
    //     Responsible for deallocating TCE Entries
    //
    /*************************************************************************/
    errlHndl_t TceMgr::deallocateTces(uint64_t i_startingToken,
                                          uint64_t i_size)
    {

        errlHndl_t errl = NULL;
        bool isContiguous = true;

        TRACFCOMP(g_trac_tce,"TceMgr::DeAllocateTce: START: for Token = %llx for size = %llx",i_startingToken, i_size);

        do
        {
            // Get number of TCEs needed.
            uint32_t numTcesNeeded = ALIGN_PAGE_DOWN(i_size)/PAGESIZE;
            uint32_t startingIndex = i_startingToken/PAGESIZE;

            // if the Token passed in equals the default token, or the
            // startingIndex is larger than the max number of indexes avail
            if ((i_startingToken == INVALID_TOKEN_ENTRY) ||
                (startingIndex > maxTceEntries))
            {
                // User passed in an invalid token, do not do a deallocate and
                // return
                TRACFCOMP(g_trac_tce,"TceMgr::DeallocateTce: ERROR -invalid Token = %lx, No deallocate.", i_startingToken);

                break;
            }

            if (startingIndex+numTcesNeeded > maxTceEntries)
            {
                TRACFCOMP(g_trac_tce,"TceMgr::DeallocateTce: ERROR - request goes past the end of the tce table");

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_DEALLOCATE
                 * @reasoncode   RUNTIME::RC_TCE_INVALID_SIZE
                 * @userdata1    starting index
                 * @userdata2    number of TCEs needed for this request
                 * @devdesc      The size requested is too large for the table
                 *               space avail starting at the Token passed in.
                 */
                errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                               RUNTIME::MOD_TCE_DEALLOCATE,
                                               RUNTIME::RC_TCE_INVALID_SIZE,
                                               startingIndex,
                                               numTcesNeeded,
                                               true /*Add HB SW Callout*/);
                errl->collectTrace(TCE_TRACE_NAME,KILOBYTE);

                errlCommit(errl,RUNTIME_COMP_ID);

                numTcesNeeded = numTcesNeeded -
                  ((startingIndex+numTcesNeeded)-maxTceEntries);

                TRACFCOMP(g_trac_tce,"TceMgr::DeallocateTce: ERROR - clearing from index = %d to end of table", startingIndex);

            }

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

            for (uint32_t tceIndex = startingIndex;
                 tceIndex < (startingIndex + numTcesNeeded);
                 tceIndex++)
            {
                if (tceIndex != startingIndex)
                {
                    // check that the address space is contiguous
                    if (((tceTableVaPtr[tceIndex].realPageNumber  -
                          realAddress) != PAGESIZE) &&
                        ((tceTableVaPtr[tceIndex].realPageNumber  -
                          realAddress) != 0))
                    {
                        isContiguous = false;
                    }
                }

                realAddress = tceTableVaPtr[tceIndex].realPageNumber;

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

            if (!isContiguous)
            {
                // We know the range to delete is not contingous.. the Token and
                // size passed in crosses past individual allocates.  We will
                // create error log to indicate that but will clear number of
                // entries requested by caller
                TRACFCOMP(g_trac_tce,"TceMgr::DeallocateTce: ERROR - request was not contiguous TCE entries");

                /*@
                 * @errortype
                 * @moduleid     RUNTIME::MOD_TCE_DEALLOCATE
                 * @reasoncode   RUNTIME::RC_TCE_ENTRY_NOT_CONTIGUOUS
                 * @userdata1    i_startingToken
                 * @userdata2    Size of the address space trying to deallocate
                 * @devdesc      The deallocate went across TCE Allocate space.
                 */
                errl = new ERRORLOG::ErrlEntry(
                                          ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          RUNTIME::MOD_TCE_DEALLOCATE,
                                          RUNTIME::RC_TCE_ENTRY_NOT_CONTIGUOUS,
                                          i_startingToken,
                                          i_size,
                                          true /*Add HB SW Callout*/);
                errl->collectTrace(TCE_TRACE_NAME,KILOBYTE);
                errlCommit(errl,RUNTIME_COMP_ID);
                break;
            }

        }while(0);

        TRACFCOMP(g_trac_tce,"TceMgr::DeAllocateTce: COMPLETE for Token = %llx for size = %llx",i_startingToken, i_size);

        return errl;
    }

    /**************************************************************************/
    //
    // NAME: ~TceMgr
    //      Destructor
    //
    /**************************************************************************/
    TceMgr::~TceMgr()
    {
        // If the If phys addr and VA table address match and If the physical
        // addr is not less than VMM memory size we need unmap
        // If it was less than VMM Memory it was already mapped outside of the
        // TCE scope and doesn't need to be unmapped here.
        if ((tceTablePhysAddr != reinterpret_cast<uint64_t>(tceTableVaPtr)) &&
           (!(tceTablePhysAddr < VMM_MEMORY_SIZE)))
        {
            if (tceTableVaPtr!= NULL)
            {
                // Unmap the tceTableVaPtr
                uint64_t rc =
                  mm_block_unmap(reinterpret_cast<void*>(tceTableVaPtr));

                if (rc != 0)
                {
                    TRACFCOMP(g_trac_tce,"TceMgr::~TceMgr: ERROR - Unmap failed rc = %d", rc);
                }
            }
        }
        else
        {

            TRACFCOMP(g_trac_tce,"TceMgr::~TceMgr: No Unmap required. testing..");
        }
    }

#endif
OpenPOWER on IntegriCloud