summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/targetservice.C
blob: f9b32e962e462d71f7f48d0e6c9ba64576027aa7 (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
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/usr/targeting/targetservice.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2011
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END

/**
 *  @file targetservice.C
 *
 *  @brief Implementation of the TargetService class
 */

//******************************************************************************
// Includes
//******************************************************************************

// STD
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Other components
#include <sys/task.h>
#include <trace/interface.H>
#include <initservice/taskargs.H>

// This component
#include <targeting/targetservice.H>
#include "trace.H"
#include "fakepnordata.H"
#include <targeting/predicates/predicatebase.H>

//******************************************************************************
// targetService
//******************************************************************************

namespace TARGETING
{


#define TARG_NAMESPACE "TARGETING::"

#define TARG_LOC TARG_NAMESPACE TARG_CLASS TARG_FN ": "

//******************************************************************************
// _start
//******************************************************************************

#define TARG_CLASS ""


/**
 *  @brief Entry point for initialization service to initialize the targeting
 *      code
 *
 *  @note: Link register is configured to automatically invoke task_end() when
 *      this routine returns
 */
extern "C"
void _start(void* io_pArgs)
{
    INITSERVICE::TaskArgs::TaskArgs *pTaskArgs  =
            reinterpret_cast<INITSERVICE::TaskArgs::TaskArgs *>(io_pArgs);

    #define TARG_FN "_start(...)"

    TARG_ENTER();

    TargetService& l_targetService = targetService();
    (void)l_targetService.init();

    TARG_EXIT();

    if  ( pTaskArgs )
    {
        pTaskArgs->waitChildSync();
    }

    task_end();

    #undef TARG_FN
}

//******************************************************************************
// targetService
//******************************************************************************

TARGETING::TargetService& targetService()
{
    #define TARG_FN "targetService()"

    return TARGETING::theTargetService::instance();

    #undef TARG_FN
}

//******************************************************************************
// Component trace buffer
//******************************************************************************

trace_desc_t* g_trac_targeting = NULL;
TRAC_INIT(&g_trac_targeting, "TARG", 4096);

#undef TARG_CLASS
#define TARG_CLASS "TargetService::"

//******************************************************************************
// TargetService::TargetService
//******************************************************************************

TargetService::TargetService() :
    iv_initialized(false), iv_maxTargets(0), iv_pPnor(NULL)
{
    #define TARG_FN "TargetService()"

    #undef TARG_FN
}

//******************************************************************************
// TargetService::~TargetService
//******************************************************************************

TargetService::~TargetService()
{
    #define TARG_FN "~TargetService()"

    // Nothing to do; Target[] memory not owned by this object

    #undef TARG_FN
}

//******************************************************************************
// TargetService::init
//******************************************************************************

void TargetService::init()
{
    #define TARG_FN "init()"

    TARG_ENTER();

    // Build the association mappings
    AssociationAttrMap a1 = {PARENT, INWARDS, ATTR_PHYS_PATH};
    AssociationAttrMap a2 = {CHILD, OUTWARDS, ATTR_PHYS_PATH};
    AssociationAttrMap a3 = {PARENT_BY_AFFINITY, INWARDS, ATTR_AFFINITY_PATH};
    AssociationAttrMap a4 = {CHILD_BY_AFFINITY, OUTWARDS, ATTR_AFFINITY_PATH};
    AssociationAttrMap a5 = {VOLTAGE_SUPPLIER, INWARDS, ATTR_POWER_PATH};
    AssociationAttrMap a6 = {VOLTAGE_CONSUMER, OUTWARDS, ATTR_POWER_PATH};
    iv_associationMappings.push_back(a1);
    iv_associationMappings.push_back(a2);
    iv_associationMappings.push_back(a3);
    iv_associationMappings.push_back(a4);
    iv_associationMappings.push_back(a5);
    iv_associationMappings.push_back(a6);

    // Get+save pointer to beginning of targeting's swappable config in
    // PNOR.  Note that this will change once PNOR is accessible
    (void)thePnorBuilderService::instance()
        .getTargetingImageBaseAddress(iv_pPnor);

    (void)_configureTargetPool();

    iv_initialized = true;

    TARG_EXIT();

    #undef TARG_FN
}

//******************************************************************************
// TargetService::begin (non-const version)
//******************************************************************************

TargetService::iterator TargetService::begin()
{
    #define TARG_FN "begin()"

    assert(iv_initialized);

    Target* l_pFirstTarget = (iv_maxTargets == 0) ? NULL : &(*iv_targets)[0];
    
    return iterator(l_pFirstTarget);

    #undef TARG_FN
}

//******************************************************************************
// TargetService::begin (const version)
//******************************************************************************

//TargetService::const_iterator 
_TargetIterator<const Target*> TargetService::begin() const
{
    #define TARG_FN "begin() const"

    assert(iv_initialized);
    
    const Target* l_pFirstTarget = 
        (iv_maxTargets == 0) ? NULL : &(*iv_targets)[0];

    return const_iterator(l_pFirstTarget);

    #undef TARG_FN
}

//******************************************************************************
// TargetService::end (non-const version)
//******************************************************************************

TargetService::iterator TargetService::end()
{
    #define TARG_FN "end()"

    assert(iv_initialized);

    return iterator(NULL); 

    #undef TARG_FN
}

//******************************************************************************
// TargetService::end (const version)
//******************************************************************************

TargetService::const_iterator TargetService::end() const
{
    #define TARG_FN "end() const"

    assert(iv_initialized);

    return const_iterator(NULL);

    #undef TARG_FN
}

//******************************************************************************
// TargetService::getTopLevelTarget
//******************************************************************************

void TargetService::getTopLevelTarget(
    Target*& o_targetHandle) const
{
    #define TARG_FN "getTopLevelTarget(...)"
    
    assert(iv_initialized, TARG_LOC "TargetService not initialized");

    EntityPath l_topLevelPhysicalPath(EntityPath::PATH_PHYSICAL);
    l_topLevelPhysicalPath.addLast(TYPE_SYS, 0);
    o_targetHandle = toTarget(l_topLevelPhysicalPath);

    #undef TARG_FN
}

//******************************************************************************
// TargetService::exists
//******************************************************************************

void TargetService::exists(
    const EntityPath& i_entityPath,
          bool&       o_exists) const
{
    #define TARG_FN "exists(...)"

    bool l_found = false;

    assert(iv_initialized, TARG_LOC "TargetService not initialized");

    for (uint32_t i = 0; i < iv_maxTargets; ++i)
    {
        if (i_entityPath == (*iv_targets)[i].getAttr<ATTR_PHYS_PATH> ())
        {
            l_found = true;
        }
    }

    o_exists = l_found;

    #undef TARG_FN
}

//******************************************************************************
// TargetService::toTarget
//******************************************************************************

Target* TargetService::toTarget(
    const EntityPath& i_entityPath) const
{
    #define TARG_FN "toTarget(...)"

    // Used by -> operator on EntityPath for convenience (can be dangerous
    // though!)
    Target* l_pTarget = NULL;

    assert(iv_initialized, TARG_LOC "TargetService not initialized");

    for (uint32_t i = 0; i < iv_maxTargets; ++i)
    {
        if (i_entityPath == (*iv_targets)[i].getAttr<ATTR_PHYS_PATH> ())
        {
            l_pTarget = &(*iv_targets)[i];
            break;
        }
    }
    
    return l_pTarget;

    #undef TARG_FN
}

//******************************************************************************
// TargetService::masterProcChipTarget
//******************************************************************************

void TargetService::masterProcChipTargetHandle(
    Target*& o_masterProcChipTargetHandle) const
{
    #define TARG_FN "masterProcChipTargetHandle(...)"

    Target* l_pTarget = NULL;

    assert(iv_initialized, TARG_LOC "TargetService not initialized");
    
    //@TODO Need to query the actual hardware and cross check it with
    // PNOR to determine the master chip
    // target; for now, just always report sys0.n0.proc0
    EntityPath l_masterProcChipEntityPath(EntityPath::PATH_PHYSICAL);
    l_masterProcChipEntityPath.addLast(TYPE_SYS, 0).addLast(TYPE_NODE, 0)
        .addLast(TYPE_PROC, 0);

    l_pTarget = l_masterProcChipEntityPath.operator->();
    
    o_masterProcChipTargetHandle = l_pTarget;

    #undef TARG_FN
}

//******************************************************************************
// TargetService::tryGetPath
//******************************************************************************

bool TargetService::tryGetPath(
    const ATTRIBUTE_ID  i_attr,
    const Target* const i_pTarget,
          EntityPath&   o_entityPath) const
{
    #define TARG_FN "tryGetPath(...)"

    bool l_exist = false;

    assert(iv_initialized, TARG_LOC "TargetService not initialized");

    switch (i_attr)
    {
        case ATTR_PHYS_PATH:
            l_exist = i_pTarget->tryGetAttr<ATTR_PHYS_PATH> (o_entityPath);
            break;
        case ATTR_AFFINITY_PATH:
            l_exist = i_pTarget->tryGetAttr<ATTR_AFFINITY_PATH> (
                o_entityPath);
            break;
        case ATTR_POWER_PATH:
            l_exist = i_pTarget->tryGetAttr<ATTR_POWER_PATH> (o_entityPath);
            break;
        default:
            assert(0, TARG_LOC "i_attr = 0x%08X does not map to an "
                   "entity path",i_attr);
            break;
    }

    return l_exist;

    #undef TARG_FN
}

//******************************************************************************
// TargetService::getAssociated
//******************************************************************************

void TargetService::getAssociated(
          TargetHandleList&    o_list,
    const Target* const        i_pTarget,
    const ASSOCIATION_TYPE     i_type,
    const RECURSION_LEVEL      i_recursionLevel,
    const PredicateBase* const i_pPredicate) const
{
    #define TARG_FN "getAssociated(...)"

    do {

    assert(iv_initialized, TARG_LOC "TargetService not initialized");
    
    assert(   (i_pTarget != NULL) 
           && (i_pTarget != MASTER_PROCESSOR_CHIP_TARGET_SENTINEL),
           TARG_LOC "Caller tried to get association using a NULL target "
           "handle or the master processor chip target handle sentinel. "
           "i_pTarget = %p",i_pTarget);

    // Start with no elements
    o_list.clear();

    // Figure out which attribute to look up
    for (uint32_t i = 0; i < iv_associationMappings.size(); ++i)
    {
        if (i_type == iv_associationMappings[i].associationType)
        {
            EntityPath l_entityPath;

            bool l_exist = tryGetPath(iv_associationMappings[i].attr,
                i_pTarget, l_entityPath);

            if (l_exist)
            {
                if (iv_associationMappings[i].associationDir == INWARDS)
                {
                    (void) _getInwards(iv_associationMappings[i].attr,
                        i_recursionLevel, l_entityPath, i_pPredicate, o_list);
                }
                else if (iv_associationMappings[i].associationDir
                    == OUTWARDS)
                {
                    (void) _getOutwards(iv_associationMappings[i].attr,
                        i_recursionLevel, l_entityPath, i_pPredicate, o_list);
                }
                else
                {
                    assert(0, TARG_LOC 
                           "iv_associationMappings[i].associationDir "
                           "= 0x%X not supported",
                           iv_associationMappings[i].associationDir);
                }
            }
            break;
        }
    }

    } while (0);

    #undef TARG_FN
}

//******************************************************************************
// TargetService::dump()
//******************************************************************************

void TargetService::dump() const
{
    #define TARG_FN "dump(...)"

    assert(iv_initialized, TARG_LOC "TargetService not initialized");

    TARG_INF("Targets (size=%d):",
             sizeof(Target)*iv_maxTargets);

    for (uint32_t i = 0; i < iv_maxTargets; ++i)
    {
        TARG_INF(
            "[Target %d] "
            "Class = 0x%X, "
            "Type = 0x%X, "
            "Model = 0x%X",
            i,
            (*iv_targets)[i].getAttr<ATTR_CLASS>(),
            (*iv_targets)[i].getAttr<ATTR_TYPE>(),
            (*iv_targets)[i].getAttr<ATTR_MODEL>());
        TARG_INF("Physical");
        (*iv_targets)[i].getAttr<ATTR_PHYS_PATH>().dump();

        EntityPath l_entityPath;
        if( (*iv_targets)[i].tryGetAttr<ATTR_AFFINITY_PATH>(l_entityPath) )
        {
            TARG_INF("Affinity");
            l_entityPath.dump();
        }

        if( (*iv_targets)[i].tryGetAttr<ATTR_POWER_PATH>(l_entityPath) )
        {
            TARG_INF("Power");
            l_entityPath.dump();
        }

        uint8_t l_dummyRw = 0;
        if ((*iv_targets)[i].tryGetAttr<ATTR_DUMMY_RW> (l_dummyRw))
        {
            TARG_INF("Dummy = 0x%X",
                l_dummyRw);
        }

        TARG_INF("Supports FSI SCOM = %d",
        (*iv_targets)[i].getAttr<ATTR_PRIMARY_CAPABILITIES>()
            .supportsFsiScom);
        TARG_INF("Supports XSCOM SCOM = %d",
        (*iv_targets)[i].getAttr<ATTR_PRIMARY_CAPABILITIES>()
            .supportsXscom);
        TARG_INF("Supports Inband SCOM = %d",
        (*iv_targets)[i].getAttr<ATTR_PRIMARY_CAPABILITIES>()
            .supportsInbandScom);

        ScomSwitches l_switches = {0};
        if ( (*iv_targets)[i].tryGetAttr<ATTR_SCOM_SWITCHES>(l_switches) )
        {
            TARG_INF("Use FSI SCOM = %d",l_switches.useFsiScom);
            TARG_INF("Use XSCOM = %d",l_switches.useXscom);
            TARG_INF("Use inband SCOM = %d",l_switches.useInbandScom);
        }

        uint64_t l_xscomBaseAddr = 0;
        if ( (*iv_targets)[i].tryGetAttr<ATTR_XSCOM_BASE_ADDRESS>(
            l_xscomBaseAddr) )
        {
            TARG_INF("XSCOM Base Address = 0x%X",l_xscomBaseAddr);
        }

        XscomChipInfo l_xscomChipInfo = {0};
        if ( (*iv_targets)[i].tryGetAttr<ATTR_XSCOM_CHIP_INFO>(
            l_xscomChipInfo) )
        {
            TARG_INF("XSCOM Node ID = 0x%X",l_xscomChipInfo.nodeId);
            TARG_INF("XSCOM Chip ID = 0x%X",l_xscomChipInfo.chipId);
        }

        I2cChipInfo l_i2cChipInfo = {0};
        if( (*iv_targets)[i].tryGetAttr<ATTR_I2C_CHIP_INFO>( l_i2cChipInfo ) )
        {
            TARG_INF( "I2C Bus Speed = 0x%X",
                      l_i2cChipInfo.busSpeed );
            TARG_INF( "I2C Device Address = 0x%X",
                      l_i2cChipInfo.deviceAddr );
            TARG_INF( "I2C Device Port = 0x%X",
                      l_i2cChipInfo.devicePort );
            TARG_INF( "I2C Master Engine = 0x%X",
                      l_i2cChipInfo.deviceMasterEng );
        }
    }

    return;

    #undef TARG_FN
}

//******************************************************************************
// TargetService::_configureTargetPool
//******************************************************************************

void TargetService::_configureTargetPool()
{
    #define TARG_FN "_configureTargetPool(...)"

    TARG_ENTER();

    _maxTargets();

    // iv_pPnor--> points to uint32_t* --> points to --> uint32_t, targets[]
    //                   (uint32_t*)+1 --> points to ------------> targets[]

    iv_targets =
        reinterpret_cast< Target(*)[] > (
            *(reinterpret_cast<uint32_t**>(
                  const_cast<void*>(iv_pPnor))) + 1);
    TARG_EXIT();

    #undef TARG_FN
}

//******************************************************************************
// TargetService::_maxTargets
//******************************************************************************

uint32_t TargetService::_maxTargets()
{
    #define TARG_FN "_maxTargets(...)"

    // Target count found by following the pointer pointed to by the iv_pPnor
    // pointer.
    iv_maxTargets = *(*(reinterpret_cast<const uint32_t * const *>(iv_pPnor)));
    return iv_maxTargets;

    #undef TARG_FN
}

//******************************************************************************
// TargetService::_getInwards
//******************************************************************************

void TargetService::_getInwards(
    const ATTRIBUTE_ID         i_attr,
    const RECURSION_LEVEL      i_recursionLevel,
          EntityPath           i_entityPath,
    const PredicateBase* const i_pPredicate,
          TargetHandleList&    o_list) const
{
    #define TARG_FN "_getInwards(...)"

    while (i_entityPath.size() > 1)
    {
        i_entityPath.removeLast();
        for (uint32_t i = 0; i < iv_maxTargets; ++i)
        {
            EntityPath l_candidatePath;
            bool l_candidateFound = tryGetPath(i_attr, &(*iv_targets)[i],
                l_candidatePath);
            if (   l_candidateFound 
                && (l_candidatePath == i_entityPath)
                && (   (i_pPredicate == NULL)
                    || (*i_pPredicate)( &(*iv_targets)[i]) ) )
            {
                o_list.push_back(&(*iv_targets)[i]);
                break;
            }
        }

        if (i_recursionLevel == IMMEDIATE)
        {
            break;
        }
    }

    #undef TARG_FN
}

//******************************************************************************
// TargetService::_getOutwards
//******************************************************************************

void TargetService::_getOutwards(
    const ATTRIBUTE_ID         i_attr,
    const RECURSION_LEVEL      i_recursionLevel,
          EntityPath           i_entityPath,
    const PredicateBase* const i_pPredicate,
          TargetHandleList&    o_list) const
{
    #define TARG_FN "_getOutwards()...)"

    do {

    // If at max depth (a leaf path element), no children possible
    if (i_entityPath.size() >= EntityPath::MAX_PATH_ELEMENTS)
    {
        break;
    }

    // Find the children (immediate, or all), depending on recursion level
    for (uint32_t i = 0; i < iv_maxTargets; ++i)
    {
        EntityPath l_candidatePath;
        bool l_candidateFound = tryGetPath(i_attr, &(*iv_targets)[i],
            l_candidatePath);
        if (l_candidateFound)
        {
            if (   (   (i_recursionLevel == IMMEDIATE)
                    && (l_candidatePath.size() == i_entityPath.size() + 1))
                || (   (i_recursionLevel == ALL)
                    && (l_candidatePath.size() > i_entityPath.size())))
            {
                if (   i_entityPath.equals(l_candidatePath,i_entityPath.size())
                    && (   (i_pPredicate == NULL)
                        || (*i_pPredicate)( &(*iv_targets)[i]) ) )
                {
                    o_list.push_back(&(*iv_targets)[i]);
                }
            }
        }
    }

    } while (0);

    #undef TARG_FN
}

#undef TARG_CLASS

#undef TARG_NAMESPACE

} // End namespace TARGETING

OpenPOWER on IntegriCloud