summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/runtime/attrrp_rt.C
blob: e13ecd3dc87bc08b9aa1b5f40d6973d353ceb23a (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/targeting/runtime/attrrp_rt.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                                                     */
#include <targeting/attrrp.H>
#include <targeting/common/trace.H>
#include <util/align.H>
#include <runtime/interface.h>
#include <errl/errlentry.H>
#include <targeting/common/targreasoncodes.H>
#include <targeting/targplatreasoncodes.H>
#include <util/runtime/util_rt.H>
#include <sys/internode.h>

#include "../attrrp_common.C"

using namespace ERRORLOG;

namespace TARGETING
{
    void AttrRP::fillInAttrRP(TargetingHeader* i_header)
    {
        TRACFCOMP(g_trac_targeting, ENTER_MRK"AttrRP::fillInAttrRP");

        do
        {
            // Allocate section structures based on section count in header.
            iv_sectionCount = i_header->numSections;
            iv_sections = new AttrRP_Section[iv_sectionCount]();

            // Find start to the first section:
            //          (header address + size of header + offset in header)
            TargetingSection* l_section =
                reinterpret_cast<TargetingSection*>(
                    reinterpret_cast<uint64_t>(i_header) +
                    sizeof(TargetingHeader) + i_header->offsetToSections
                );

            uint64_t l_offset = 0;

            for (size_t i = 0; i < iv_sectionCount; ++i, ++l_section)
            {
                iv_sections[i].type = l_section->sectionType;
                iv_sections[i].size = l_section->sectionSize;

                iv_sections[i].vmmAddress =
                        static_cast<uint64_t>(
                            TARG_TO_PLAT_PTR(i_header->vmmBaseAddress)) +
                        i_header->vmmSectionOffset*i;
                iv_sections[i].pnorAddress =
                        reinterpret_cast<uint64_t>(i_header) + l_offset;

                l_offset += ALIGN_PAGE(iv_sections[i].size);

                TRACFCOMP(g_trac_targeting,
                          "Decoded Attribute Section: %d, 0x%lx, 0x%lx, 0x%lx",
                          iv_sections[i].type,
                          iv_sections[i].vmmAddress,
                          iv_sections[i].pnorAddress,
                          iv_sections[i].size);
            }
        } while(false);

        TRACFCOMP(g_trac_targeting, EXIT_MRK"AttrRP::fillInAttrRP");

        return;
    }

    void AttrRP::startup(errlHndl_t& io_taskRetErrl, bool isMpipl)
    {
        TRACFCOMP(g_trac_targeting, "AttrRP::startup");
        errlHndl_t l_errl = nullptr;

        uint8_t l_index = 0;
        uint32_t l_instance[MAX_NODES_PER_SYS];
        l_instance[l_index] = NODE0; // First instance is always NODE 0
        // Initialize rest of the instances to be invalid nodes
        for(l_index = 1; l_index < MAX_NODES_PER_SYS; l_index++)
        {
            l_instance[l_index] = INVALID_NODE;
        }

        // Handle first instance
        l_index = 0;
        uint64_t attr_size = 0;
        TargetingHeader* l_header =
          reinterpret_cast<TargetingHeader*>(
              hb_get_rt_rsvd_mem(Util::HBRT_MEM_LABEL_ATTR,
                                 l_instance[l_index], attr_size));

        // Create local copy of node struct
        NodeInfo l_nodeCont;

        // Initialize local copy of node struct
        l_errl = nodeInfoInit(l_nodeCont,
                              l_header,
                              l_instance[l_index]);

        // Push back node struct into the node container
        TRACFCOMP(g_trac_targeting,
                  "Push node struct for Node %d",
                  l_instance[l_index]);
        iv_nodeContainer.push_back(l_nodeCont);

        // Get pointer to number of targets
        const AbstractPointer<uint32_t>* l_pNumTargetsPtr =
            static_cast<const AbstractPointer<uint32_t>*>(
                reinterpret_cast<void*>(
                reinterpret_cast<char*>(l_header) +
                                        l_header->headerSize));
        uint32_t* l_pNumTargets = TARG_TO_PLAT_PTR(*l_pNumTargetsPtr);

        // Only translate addresses on platforms where addresses are 4 bytes
        // wide (FSP).  The compiler should perform dead code elimination of
        // this path on platforms with 8 byte wide addresses (Hostboot), since
        // the "if" check can be statically computed at compile time.
        if(TARG_ADDR_TRANSLATION_REQUIRED)
        {
            l_pNumTargets = static_cast<uint32_t*>(
                this->translateAddr(l_pNumTargets, l_instance[l_index]));
        }

        // Get pointer to targets
        Target (*l_pTargets)[] =
            reinterpret_cast<Target(*)[]> (l_pNumTargets + 1);

        // Walk through targets
        for(uint32_t l_targetNum = 1;
            l_targetNum <= *l_pNumTargets;
            ++l_targetNum)
        {
            Target* l_pTarget = &(*(l_pTargets))[l_targetNum - 1];
            TRACDCOMP( g_trac_targeting,
                       "Target %d of %d, class %0.8x, type %0.8x",
                       l_targetNum,
                       *l_pNumTargets,
                       l_pTarget->getAttr<ATTR_CLASS>(),
                       l_pTarget->getAttr<ATTR_TYPE>());

            if((l_pTarget->getAttr<ATTR_CLASS>() == CLASS_SYS) &&
               (l_pTarget->getAttr<ATTR_TYPE>() == TYPE_SYS))
            {
                // This attribute is only set on a multi-node system.
                // We will use it below to detect a multi-node scenario
                auto l_hb_images =
                    l_pTarget->getAttr<ATTR_HB_EXISTING_IMAGE>();

                EntityPath l_physPath = l_pTarget->getAttr<ATTR_PHYS_PATH>();
                TRACFCOMP( g_trac_targeting,
                           "Target %d of %d, %s, HB images %0.2x",
                           l_targetNum,
                           *l_pNumTargets,
                           l_physPath.toString(),
                           l_hb_images);

                // Start the 1 in the mask at leftmost position
                decltype(l_hb_images) l_mask =
                    0x1 << ((sizeof(l_hb_images) * 8) - 1);

                uint32_t l_node = NODE0;
                l_index = 0;
                // While multi-node system and valid mask and valid index
                while(l_hb_images && l_mask && (l_index < MAX_NODES_PER_SYS))
                {
                    // Change node instance status
                    if(iv_instanceStatus == SINGLE_NODE)
                    {
                        iv_instanceStatus = MULTI_NODE;
                    }

                    // If node is present
                    if(l_mask & l_hb_images)
                    {
                        l_instance[l_index] = l_node;

                        // Check if a previous node was skipped
                        if(iv_instanceStatus == MULTI_NODE_LT_MAX_INSTANCES)
                        {
                            // Flag that instances are not contiguous
                            iv_instanceStatus = MULTI_NODE_INSTANCE_GAP;
                        }
                    }
                    else if(iv_instanceStatus == MULTI_NODE)
                    {
                        // Flag that an instance is being skipped
                        iv_instanceStatus = MULTI_NODE_LT_MAX_INSTANCES;
                    }

                    l_mask >>= 1; // shift to the right for the next node
                    l_node++;
                    l_index++;
                }

                if(iv_instanceStatus == MULTI_NODE_INSTANCE_GAP)
                {
                    TRACFCOMP( g_trac_targeting,
                               "There is a gap in the node numbers");
                }

                break;
            }
        }

        // Handle additional instances
        l_index = 1;
        do
        {
            // Check that a valid node is set for this instnace
            if(l_instance[l_index] == INVALID_NODE)
            {
                l_index++;

                // Continue with next instance
                continue;
            }

            uint64_t attr_size = 0;
            TargetingHeader* l_header =
              reinterpret_cast<TargetingHeader*>(
                  hb_get_rt_rsvd_mem(Util::HBRT_MEM_LABEL_ATTR,
                                     l_instance[l_index], attr_size));

            // Check if reserved memory does not exist for this instance
            if ((NULL == l_header) && (l_instance[l_index] > NODE0))
            {
                TRACFCOMP(g_trac_targeting,
                          "Reserved memory does not exist for Node %d",
                          l_instance[l_index]);

                l_index++;

                // Continue with next instance
                continue;
            }

            // Create local copy of node struct
            NodeInfo l_nodeCont;

            // Initialize local copy of node struct
            l_errl = nodeInfoInit(l_nodeCont,
                                  l_header,
                                  l_instance[l_index]);

            // Push back node struct into the node container
            TRACFCOMP(g_trac_targeting,
                      "Push node struct for Node %d",
                      l_instance[l_index]);
            iv_nodeContainer.push_back(l_nodeCont);

            l_index++;
        } while(l_index < MAX_NODES_PER_SYS);

        if (l_errl)
        {
            l_errl->setSev(ERRORLOG::ERRL_SEV_UNRECOVERABLE);
        }

        io_taskRetErrl = l_errl;
    }

    errlHndl_t AttrRP::nodeInfoInit(NodeInfo& io_nodeCont,
                                    TargetingHeader* i_header,
                                    const NODE_ID i_nodeId)
    {
        TRACFCOMP(g_trac_targeting, "AttrRP::nodeInfoInit");
        errlHndl_t l_errl = nullptr;

        do
        {
            if ((NULL == i_header) ||
                (i_header->eyeCatcher != PNOR_TARG_EYE_CATCHER))
            {
                /*@
                 *   @errortype
                 *   @moduleid          TARG_MOD_ATTRRP_RT
                 *   @reasoncode        TARG_RC_BAD_EYECATCH
                 *   @userdata1         Observed Header Eyecatch Value
                 *   @userdata2         Memory address referenced.
                 *
                 *   @devdesc   The eyecatch value observed in memory does not
                 *              match the expected value of
                 *              PNOR_TARG_EYE_CATCHER and therefore the
                 *              contents of the Attribute sections are
                 *              unable to be parsed.
                 *   @custdesc  A problem occurred during the IPL of the
                 *              system.
                 *              The eyecatch value observed in memory does not
                 *              match the expected value and therefore the
                 *              contents of the attribute sections are unable
                 *              to be parsed.
                 */
                l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                       TARG_MOD_ATTRRP_RT,
                                       TARG_RC_BAD_EYECATCH,
                                       NULL == i_header ?
                                            0 : i_header->eyeCatcher,
                                       reinterpret_cast<uint64_t>(i_header));
                break;
            }

            // Save pointer to targeting image in reserved memory for this node
            io_nodeCont.pTargetMap = reinterpret_cast<void*>(i_header);

            // Allocate section structures based on section count in header
            io_nodeCont.sectionCount = i_header->numSections;
            io_nodeCont.pSections =
                new AttrRP_Section[io_nodeCont.sectionCount]();
            if (i_nodeId == NODE0) // @TODO RTC:186585 remove
            { // @TODO RTC:186585 remove
                iv_sectionCount = io_nodeCont.sectionCount; // @TODO RTC:186585
                iv_sections = io_nodeCont.pSections; // @TODO RTC:186585 remove
            } // @TODO RTC:186585 remove

            // Find start to the first section:
            //          (header address + size of header + offset in header)
            TargetingSection* l_section =
                reinterpret_cast<TargetingSection*>(
                    reinterpret_cast<uint64_t>(i_header) +
                    sizeof(TargetingHeader) + i_header->offsetToSections
                );

            uint64_t l_offset = 0;

            for (size_t i = 0; i < io_nodeCont.sectionCount; ++i, ++l_section)
            {
                io_nodeCont.pSections[i].type = l_section->sectionType;
                io_nodeCont.pSections[i].size = l_section->sectionSize;

                io_nodeCont.pSections[i].vmmAddress =
                        static_cast<uint64_t>(
                            TARG_TO_PLAT_PTR(i_header->vmmBaseAddress)) +
                        i_header->vmmSectionOffset*i;
                io_nodeCont.pSections[i].pnorAddress =
                        reinterpret_cast<uint64_t>(i_header) + l_offset;

                l_offset += ALIGN_PAGE(io_nodeCont.pSections[i].size);

                TRACFCOMP(g_trac_targeting,
                          "Decoded Attribute Section: %d, 0x%lx, 0x%lx, 0x%lx",
                          io_nodeCont.pSections[i].type,
                          io_nodeCont.pSections[i].vmmAddress,
                          io_nodeCont.pSections[i].pnorAddress,
                          io_nodeCont.pSections[i].size);
            }
        } while(false);

        return l_errl;
    }

    void* AttrRP::getTargetMapPtr(const NODE_ID i_nodeId)
    {
        #define TARG_FN "getTargetMapPtr"
        TARG_ENTER();

        errlHndl_t l_errl = nullptr;
        void* l_pTargetMap = nullptr;

        // Cannot use isNodeValid method here since the vector itself is
        // initialized in here.
        if((i_nodeId >= NODE0) &&
            (i_nodeId < AttrRP::INVALID_NODE_ID))
        {
            do
            {
                if(iv_nodeContainer[i_nodeId].pTargetMap == nullptr)
                {
                    // Locate targeting image for this node in reserved memory
                    uint64_t attr_size = 0;
                    iv_nodeContainer[i_nodeId].pTargetMap =
                        reinterpret_cast<void*>(
                            hb_get_rt_rsvd_mem(Util::HBRT_MEM_LABEL_ATTR,
                                               i_nodeId,
                                               attr_size));

                    // Check for failure to locate targeting image for this node
                    if (iv_nodeContainer[i_nodeId].pTargetMap == nullptr)
                    {
                        TARG_ERR("Error: hb_get_rt_rsvd_mem call failed");
                        break;
                    }

                    // Get pointer to targeting image header
                    TargetingHeader *l_header =
                        static_cast<TargetingHeader *>(
                            iv_nodeContainer[i_nodeId].pTargetMap);

                    // Initialize the node struct for this node
                    l_errl = nodeInfoInit(iv_nodeContainer[i_nodeId],
                                          l_header,
                                          i_nodeId);
                    if(l_errl)
                    {
                        break;
                    }

                    // Set the targeting image pointer
                    l_pTargetMap = iv_nodeContainer[i_nodeId].pTargetMap;
                }
                else
                {
                    // This should return pTargetMap from here
                    break;
                }
            } while(0);
        }
        else
        {
            TARG_ERR("Invalid Node Id passed here to initialize [%d]",
                     i_nodeId);
        }

        if(l_errl)
        {
            /* Commit the error */
            errlCommit(l_errl, TARG_COMP_ID);
        }

        return l_pTargetMap;
        #undef TARG_FN
    }

    void* AttrRP::getBaseAddress(const NODE_ID i_nodeId)
    {
        #define TARG_FN "getBaseAddress()"
        TARG_ENTER();

        void* l_pMap = nullptr;

        // The init for a node id might have been done via the other way i.e.
        // setImageName, need to valid if node Id is valid and init is already
        // done validate node Id, It's a special case validation, since the node
        // which is getting validated doesn't yet have a container, so it should
        // always point to the next to be initialized.
        if(i_nodeId < INVALID_NODE_ID)
        {
            // Check if the Mmap is already done
            if(iv_nodeContainer[i_nodeId].pTargetMap != nullptr)
            {
                l_pMap = iv_nodeContainer[i_nodeId].pTargetMap;
            }
            else
            {
                TARG_ASSERT(0,
                            TARG_ERR_LOC "Node Id [%d] should have been already"
                            " initialized, before but the Mmap Address is NULL",
                            i_nodeId);
            }
        }
        else if(i_nodeId == INVALID_NODE_ID)
        {
            // Push back a node struct in the node container
            NodeInfo l_nodeCont;
            iv_nodeContainer.push_back(l_nodeCont);

            l_pMap = getTargetMapPtr(i_nodeId);
        }
        else
        {
            TARG_ERR("Invalid Node Id [%d] passed here to initialize",
                i_nodeId);
        }

        return l_pMap;
        #undef TARG_FN
    }

    void AttrRP::getNodeId(const Target* i_pTarget, NODE_ID& o_nodeId) const
    {
        #define TARG_FN "getNodeId"

        bool l_found = false;

        // Initialize with invalid
        o_nodeId = INVALID_NODE_ID;

        //find the node to which this target belongs
        for(uint8_t i=0; i<INVALID_NODE_ID; ++i)
        {
           for(uint32_t j=0; j<iv_nodeContainer[i].sectionCount; ++j)
           {
               if(  iv_nodeContainer[i].pSections[j].type ==
                                  SECTION_TYPE_PNOR_RO)
               {
                 // This expects the pTarget to be always in range and !NULL.
                 // If any invalid target is passed (which is still within the
                 // RO Section scope) then behaviour is undefined.
                 if( (i_pTarget >= iv_nodeContainer[i].pTargetMap) &&
                     (i_pTarget < reinterpret_cast<Target*>((
                        reinterpret_cast<uint8_t*>(
                            iv_nodeContainer[i].pTargetMap) +
                            iv_nodeContainer[i].pSections[j].size))) )
                 {
                     l_found = true;
                     o_nodeId = i;
                     break;
                 }
               }
           }
           if(l_found)
           {
              break;
           }
        }

        #undef TARG_FN
    }

    void* AttrRP::translateAddr(void* i_pAddress,
                                const Target* i_pTarget)
    {
        void* o_pTransAddr = i_pAddress;
        if(i_pTarget != NULL)
        {
            NODE_ID l_nodeId = NODE0;
            getNodeId(i_pTarget, l_nodeId);
            o_pTransAddr =  translateAddr(i_pAddress, l_nodeId);
        }
        return o_pTransAddr;
    }

    void* AttrRP::translateAddr(void* i_pAddress,
                                const TARGETING::NODE_ID i_nodeId)
    {
        void* l_address = i_pAddress;
        do
        {
            if (i_nodeId >= AttrRP::INVALID_NODE_ID)
            {
                TRACFCOMP(g_trac_targeting, "ERROR: invalid nodeid=%d"
                        " passed to translateAddr", i_nodeId);
                break;
            }

            for (size_t i = 0; i < iv_nodeContainer[i_nodeId].sectionCount; ++i)
            {
                if ((iv_nodeContainer[i_nodeId].pSections[i].vmmAddress +
                     iv_nodeContainer[i_nodeId].pSections[i].size) >=
                    reinterpret_cast<uint64_t>(i_pAddress))
                {
                    l_address = reinterpret_cast<void*>(
                            iv_nodeContainer[i_nodeId].pSections[i].pnorAddress +
                            reinterpret_cast<uint64_t>(i_pAddress) -
                            iv_nodeContainer[i_nodeId].pSections[i].vmmAddress);
                    break;
                }
            }
        } while (0);

        TRACDCOMP(g_trac_targeting, "Translated 0x%p to 0x%p",
                  i_pAddress, l_address);

        return l_address;
    }
}
OpenPOWER on IntegriCloud