summaryrefslogtreecommitdiffstats
path: root/src/usr/htmgt/htmgt_occ.C
blob: 1571076e50a7a4b5c59ec9d7137c9b6d5560e3e9 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/htmgt/htmgt_occ.C $                                   */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] 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 <htmgt/htmgt.H>
#include <htmgt/htmgt_reasoncodes.H>
#include "htmgt_utility.H"
#include "htmgt_occcmd.H"
#include "htmgt_cfgdata.H"
#include "htmgt_occ.H"
#include "htmgt_poll.H"

#include <targeting/common/commontargeting.H>
#include <targeting/common/utilFilter.H>
#include <targeting/common/attributes.H>
#include <targeting/common/targetservice.H>
#include <console/consoleif.H>


namespace HTMGT
{

    Occ::Occ(const uint8_t   i_instance,
             const bool      i_masterCapable,
             uint8_t       * i_homer,
             TARGETING::TargetHandle_t i_target,
             const occRole   i_role)
        :iv_instance(i_instance),
        iv_masterCapable(i_masterCapable),
        iv_role(i_role),
        iv_state(OCC_STATE_UNKNOWN),
        iv_commEstablished(false),
        iv_needsReset(false),
        iv_failed(false),
        iv_seqNumber(0),
        iv_homer(i_homer),
        iv_target(i_target),
        iv_lastPollValid(false),
        iv_version(0x01)
    {
    }

    Occ::~Occ()
    {
    }


    // Return true if specified status bit is set in last poll response
    bool Occ::statusBitSet(const uint8_t i_statusBit)
    {
        bool isSet = false;

        if (iv_lastPollValid)
        {
            const occPollRspStruct_t *lastPoll =
                (occPollRspStruct_t*)iv_lastPollResponse;
            isSet = ((lastPoll->status & i_statusBit) == i_statusBit);
        }

        return isSet;
    }


    // Set state of the OCC
    errlHndl_t Occ::setState(const occStateId i_state)
    {
        errlHndl_t l_err = NULL;

        if (OCC_ROLE_MASTER == iv_role)
        {
            const uint8_t l_cmdData[3] =
            {
                0x00, // version
                i_state,
                0x00 // reserved
            };

            OccCmd cmd(this, OCC_CMD_SET_STATE,
                       sizeof(l_cmdData), l_cmdData);
            l_err = cmd.sendOccCmd();
            if (l_err != NULL)
            {
                TMGT_ERR("setState: Failed to set OCC%d state, rc=0x%04X",
                         iv_instance, l_err->reasonCode());
            }
            else
            {
                if (OCC_RC_SUCCESS != cmd.getRspStatus())
                {
                    TMGT_ERR("setState: Set OCC%d state failed"
                             " with OCC status 0x%02X",
                             iv_instance, cmd.getRspStatus());
                    /*@
                     * @errortype
                     * @moduleid HTMGT_MOD_OCC_SET_STATE
                     * @reasoncode HTMGT_RC_OCC_CMD_FAIL
                     * @userdata1[0-15] OCC instance
                     * @userdata1[16-31] Requested state
                     * @userdata2[0-15] OCC response status
                     * @userdata2[16-31] current OCC state
                     * @devdesc Set of OCC state failed
                     */
                    bldErrLog(l_err, HTMGT_MOD_OCC_SET_STATE,
                              HTMGT_RC_OCC_CMD_FAIL,
                              iv_instance, i_state,
                              cmd.getRspStatus(), iv_state,
                              ERRORLOG::ERRL_SEV_INFORMATIONAL);
                }
            }
        }
        else
        {
            TMGT_ERR("setState: State only allowed to be set on master OCC");
            /*@
             * @errortype
             * @moduleid HTMGT_MOD_OCC_SET_STATE
             * @reasoncode HTMGT_RC_INTERNAL_ERROR
             * @userdata1[0-15] OCC instance
             * @userdata1[16-31] Requested state
             * @devdesc Set state only allowed on master OCC
             */
            bldErrLog(l_err, HTMGT_MOD_OCC_SET_STATE,
                      HTMGT_RC_INTERNAL_ERROR,
                      iv_instance, i_state, 0, 0,
                      ERRORLOG::ERRL_SEV_INFORMATIONAL);
        }

        return l_err;

    } // end Occ::setState()



    /////////////////////////////////////////////////////////////////



    OccManager::OccManager()
        :iv_configDataBuilt(false),
        iv_occMaster(NULL),
        iv_state(OCC_STATE_UNKNOWN),
        iv_targetState(OCC_STATE_ACTIVE)
    {
    }


    OccManager::~OccManager()
    {
    }


    // Remove all OCC objects
    void OccManager::_removeAllOccs()
    {
        iv_occMaster = NULL;
        if (iv_occArray.size() > 0)
        {
            for (std::vector<Occ*>::iterator pOcc = iv_occArray.begin();
                 pOcc < iv_occArray.end();
                 pOcc++)
            {
                TMGT_INF("removeAllOccs: Removing OCC%d",
                         (*pOcc)->getInstance());
                delete (*pOcc);
            }
            iv_occArray.clear();
        }
    }


    // Query the functional OCCs and build OCC objects
    uint32_t OccManager::_buildOccs()
    {
        TMGT_INF("buildOccs called");

        // Remove existing OCC objects
        _removeAllOccs();

        // Get all functional processors
        TARGETING::TargetHandleList pProcs;
        TARGETING::getChipResources(pProcs,
                                    TARGETING::TYPE_PROC,
                                    TARGETING::UTIL_FILTER_FUNCTIONAL);
        if (pProcs.size() > 0)
        {
            // for each functional processor
            for(TARGETING::TargetHandleList::iterator proc = pProcs.begin();
                proc != pProcs.end();
                ++proc)
            {
                // Instance number for this Processor/OCC
                const uint8_t instance =
                    (*proc)->getAttr<TARGETING::ATTR_POSITION>();
                TMGT_INF("buildOccs: PROC%d is functional", instance);
                // Get HOMER virtual address
                uint8_t * homer = (uint8_t*)
                    ((*proc)->getAttr<TARGETING::ATTR_HOMER_VIRT_ADDR>());
                const uint8_t * homerPhys = (uint8_t*)
                    ((*proc)->getAttr<TARGETING::ATTR_HOMER_PHYS_ADDR>());
                TMGT_INF("buildOccs: homer = 0x%08X (virt) / 0x%08X (phys)"
                         " for Proc%d", homer, homerPhys, instance);
#ifdef SIMICS_TESTING
                // Starting of OCCs is not supported in SIMICS, so fake out
                // HOMER memory area for testing
                if (NULL == homer)
                {
                    extern uint8_t * G_simicsHomerBuffer;

                    if (NULL == G_simicsHomerBuffer)
                    {
                        // Allocate a fake HOMER area
                        G_simicsHomerBuffer =
                            new uint8_t [OCC_CMD_ADDR+0x2000];
                    }
                    homer = G_simicsHomerBuffer;
                    TMGT_ERR("buildOccs: Using hardcoded HOMER of 0x%08lX",
                             homer);
                }
#endif

                // Get functional OCC (one per proc)
                TARGETING::TargetHandleList pOccs;
                getChildChiplets(pOccs, *proc, TARGETING::TYPE_OCC);
                if (pOccs.size() > 0)
                {
                    const unsigned long huid =
                        pOccs[0]->getAttr<TARGETING::ATTR_HUID>();
                    const bool masterCapable =
                        pOccs[0]->getAttr<TARGETING::ATTR_OCC_MASTER_CAPABLE>();

                    TMGT_INF("Found OCC %d - HUID: 0x%0lX, masterCapable: %c,"
                             " homer: 0x%0lX",
                             instance, huid, masterCapable?'Y':'N', homer);
                    _addOcc(instance, masterCapable, homer, pOccs[0]);
                }
                else
                {
                    // OCC must not be functional
                    TMGT_ERR("OCC%d not functional", instance);
                }
            }
        }
        else
        {
            TMGT_ERR("No functional processors found");
        }

        TMGT_INF("buildOccs: OCC Targets found = %d", _getNumOccs());

        return _getNumOccs();

    } // end OccManager::_buildOccs()



    // Add a functional OCC to be monitored
    void OccManager::_addOcc(const uint8_t   i_instance,
                             const bool      i_masterCapable,
                             uint8_t       * i_homer,
                             TARGETING::TargetHandle_t i_target)
    {
        TMGT_INF("addOcc(%d, masterCapable=%c)",
                 i_instance, i_masterCapable?'y':'n');

        occRole role = OCC_ROLE_SLAVE;
        if (true == i_masterCapable)
        {
            if (NULL == iv_occMaster)
            {
                // No master assigned yet, use this OCC
                TMGT_INF("addOcc: OCC%d will be the master", i_instance);
                role = OCC_ROLE_MASTER;
            }
            else
            {
                role = OCC_ROLE_BACKUP_MASTER;
            }
        }

        Occ * l_occ = new Occ(i_instance,
                              i_masterCapable,
                              i_homer,
                              i_target,
                              role);

        // Add OCC to the array
        iv_occArray.push_back(l_occ);

        if (OCC_ROLE_MASTER == role)
        {
            iv_occMaster = l_occ;
        }

    } // end OccManager::_addOcc()


    // Set the OCC state
    errlHndl_t OccManager::_setOccState(const occStateId i_state)
    {
        errlHndl_t l_err = NULL;

        occStateId requestedState = i_state;
        if (OCC_STATE_NO_CHANGE == i_state)
        {
            // If no state was requested use the target state
            requestedState = iv_targetState;
        }

        if ((requestedState == OCC_STATE_ACTIVE) ||
            (requestedState == OCC_STATE_OBSERVATION))
        {
            // Function is only called on initial IPL and when user/mfg
            // requests a new state, so we can update target here.
            iv_targetState = requestedState;

            if (NULL != iv_occMaster)
            {
                TMGT_INF("_setOccState(state=0x%02X)", requestedState);

                const uint8_t occInstance = iv_occMaster->getInstance();
                bool needsRetry = false;
                do
                {
                    l_err = iv_occMaster->setState(requestedState);
                    if (NULL == l_err)
                    {
                        needsRetry = false;
                    }
                    else
                    {
                        TMGT_ERR("_setOccState: Failed to set OCC%d state,"
                                 " rc=0x%04X",
                                 occInstance, l_err->reasonCode());
                        if (false == needsRetry)
                        {
                            ERRORLOG::errlCommit(l_err, HTMGT_COMP_ID);
                            l_err = NULL;
                            needsRetry = true;
                        }
                        else
                        {
                            // Only one retry, return error handle
                            needsRetry = false;
                        }
                    }
                }
                while (needsRetry);
            }
            else
            {
                /*@
                 * @errortype
                 * @moduleid HTMGT_MOD_OCCMGR_SET_STATE
                 * @reasoncode HTMGT_RC_INTERNAL_ERROR
                 * @devdesc Unable to set state of master OCC
                 */
                bldErrLog(l_err, HTMGT_MOD_OCCMGR_SET_STATE,
                          HTMGT_RC_INTERNAL_ERROR,
                          0, 0, 0, 0,
                          ERRORLOG::ERRL_SEV_INFORMATIONAL);
            }

            if (NULL == l_err)
            {
                // Send poll to query state of all OCCs
                // and flush any errors reported by the OCCs
                l_err = sendOccPoll(true);
                if (l_err)
                {
                    TMGT_ERR("_setOccState: Poll all OCCs failed");
                    ERRORLOG::errlCommit(l_err, HTMGT_COMP_ID);
                    l_err = NULL;
                }

                // Make sure all OCCs went to active state
                for (std::vector<Occ*>::iterator pOcc = iv_occArray.begin();
                     pOcc < iv_occArray.end();
                     pOcc++)
                {
                    if (requestedState != (*pOcc)->getState())
                    {
                        TMGT_ERR("_setOccState: OCC%d is not in 0x%02X state",
                                 (*pOcc)->getInstance(), requestedState);
                        /*@
                         * @errortype
                         * @moduleid HTMGT_MOD_OCCMGR_SET_STATE
                         * @reasoncode HTMGT_RC_OCC_UNEXPECTED_STATE
                         * @userdata1[0-15] requested state
                         * @userdata1[16-31] OCC state
                         * @userdata2[0-15] OCC instance
                         * @devdesc OCC did not change to requested state
                         */
                        bldErrLog(l_err, HTMGT_MOD_OCCMGR_SET_STATE,
                                  HTMGT_RC_OCC_UNEXPECTED_STATE,
                                  requestedState, (*pOcc)->getState(),
                                  (*pOcc)->getInstance(), 0,
                                  ERRORLOG::ERRL_SEV_INFORMATIONAL);
                        break;
                    }
                }

                if (NULL == l_err)
                {
                    TMGT_INF("_setOccState: All OCCs have reached state 0x%02X",
                             requestedState);

#ifndef __HOSTBOOT_RUNTIME
                    if (OCC_STATE_ACTIVE == requestedState)
                    {
                        CONSOLE::displayf(HTMGT_COMP_NAME,
                               "OCCs are now running in ACTIVE state");
                    }
                    else
                    {
                        CONSOLE::displayf(HTMGT_COMP_NAME,
                               "OCCs are now running in OBSERVATION state");
                    }
#endif
                }

            }
        }
        else
        {
            TMGT_ERR("_setOccState: Invalid state 0x%02X requested",
                     requestedState);
            /*@
             * @errortype
             * @moduleid HTMGT_MOD_OCCMGR_SET_STATE
             * @reasoncode HTMGT_RC_INVALID_DATA
             * @userdata1[0-15] requested state
             * @devdesc Invalid OCC state requested
             */
            bldErrLog(l_err, HTMGT_MOD_OCCMGR_SET_STATE,
                      HTMGT_RC_INVALID_DATA,
                      requestedState, 0, 0, 0,
                      ERRORLOG::ERRL_SEV_INFORMATIONAL);
        }

        return l_err;

    } // end OccManager::_setOccState()


    uint8_t  OccManager::getNumOccs()
    {
        return Singleton<OccManager>::instance()._getNumOccs();
    }


    std::vector<Occ*> OccManager::getOccArray()
    {
        return Singleton<OccManager>::instance()._getOccArray();
    }


    uint32_t OccManager::buildOccs()
    {
        return Singleton<OccManager>::instance()._buildOccs();
    }


    Occ * OccManager::getMasterOcc()
    {
        return Singleton<OccManager>::instance()._getMasterOcc();
    }


    errlHndl_t OccManager::setOccState(const occStateId i_state)
    {
        return Singleton<OccManager>::instance()._setOccState(i_state);
    }


    occStateId OccManager::getTargetState()
    {
        return Singleton<OccManager>::instance()._getTargetState();
    }


#if 0
    // TODO: RTC 115296
    void update_occ_data()
    {
        if (occMgr::instance().getNumOccs() > 0)
        {
            // TBD: define as one block of data or in each OCC target?

            uint32_t dataSize = occMgr::instance().getNumOccs() *
                sizeof(occInstance);
            if (dataSize > 256)
            {
                TMGT_ERR("update_occ_data: data exceeds attr size, truncating");
                dataSize = 256;
            }
            // Update OCC_CONTROL_DATA Attribute
            bool success = ->trySetAttr<ATTR_OCC_CONTROL_DATA>(dataSize, G_occ);
            if (false == success)
            {
                TMGT_ERR("update_occ_data: failed to update OCC_CONTROL_DATA");
            }
        }
        else
        {
            TMGT_INF("update_occ_data: No OCC data to update");
        }
    } // end update_occ_data()
#endif


} // end namespace



OpenPOWER on IntegriCloud