summaryrefslogtreecommitdiffstats
path: root/src/usr/fapi2/plat_attr_override_sync.C
blob: 0d9da23b9a7c8f835d6be616652c58c203ec1c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/fapi2/plat_attr_override_sync.C $                     */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] 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                                                     */
/**
 *  @file plat_attr_override_sync.C
 *
 *  @brief Implements the functions for Attribute Override and Sync
 *
 */

//******************************************************************************
// Includes
//******************************************************************************
#include <sys/msg.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <sys/msg.h>
#include <sys/mm.h>
#include <errno.h>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <mbox/mboxif.H>
#include <plat_attr_override_sync.H>
#include <fapiPlatTrace.H>
#include <hwpf_fapi2_reasoncodes.H>
#include <targeting/common/utilFilter.H>
#include <targeting/common/attributeTank.H>
#include <fapi2AttrSyncData.H>
#include <fapi2_attribute_service.H>
#include <util/utilmbox_scratch.H>
#include <plat_utils.H>
#include <secureboot/service.H>

namespace fapi2
{

// CDIMM attribute table
const uint8_t NUM_OF_CDIMM_ATTRS = 8;
uint32_t CDIMM_ATTR_ID[NUM_OF_CDIMM_ATTRS] =
{
   ATTR_CEN_CDIMM_VPD_SUPPLIER_POWER_INTERCEPT,
   ATTR_CEN_CDIMM_VPD_MASTER_TOTAL_POWER_INTERCEPT,
   ATTR_CEN_CDIMM_VPD_SUPPLIER_TOTAL_POWER_INTERCEPT,
   ATTR_CEN_CDIMM_VPD_MASTER_POWER_SLOPE,
   ATTR_CEN_CDIMM_VPD_MASTER_TOTAL_POWER_SLOPE,
   ATTR_CEN_CDIMM_VPD_SUPPLIER_TOTAL_POWER_SLOPE,
   ATTR_CEN_CDIMM_VPD_MASTER_POWER_INTERCEPT,
   ATTR_CEN_CDIMM_VPD_SUPPLIER_POWER_SLOPE
};

// ISDIMM attribute table
const uint8_t NUM_OF_ISDIMM_ATTRS = 2;
uint32_t ISDIMM_ATTR_ID[NUM_OF_ISDIMM_ATTRS] =
{
    ATTR_CEN_VPD_ISDIMMTOC4DQS,
    ATTR_CEN_VPD_ISDIMMTOC4DQ
};

//******************************************************************************
// Global Variables
//******************************************************************************

#ifndef __HOSTBOOT_RUNTIME
// Set by a debug tool to directly apply an Attribute Override
TARGETING::AttributeTank::AttributeHeader g_attrOverrideHeader;
uint8_t g_attrOverride[AttrOverrideSync::MAX_DIRECT_OVERRIDE_ATTR_SIZE_BYTES];
uint8_t g_attrOverrideFapiTank = 0;
#endif

//******************************************************************************
// Apply a HWPF Attribute Override written directly into Hostboot memory from
// the Simics/VBU console. This function is called by a Simics/VBU debug tool
//******************************************************************************
void directOverride()
{
    if (!SECUREBOOT::allowAttrOverrides())
    {
        FAPI_INF("directOverride: skipping since "
                 "attribute overrides are not allowed");
        return;
    }

#ifndef __HOSTBOOT_RUNTIME
    uint32_t l_targetType = TARGETING::TYPE_NA;
    // Apply the attribute override
    if (g_attrOverrideFapiTank)
    {
        FAPI_IMP("directOverride: Applying override to FAPI tank "
            "Id: 0x%08x, TargType: 0x%08x, Pos: 0x%04x, UPos: 0x%02x",
            g_attrOverrideHeader.iv_attrId, g_attrOverrideHeader.iv_targetType,
            g_attrOverrideHeader.iv_pos, g_attrOverrideHeader.iv_unitPos);
        FAPI_IMP("directOverride: Applying override to FAPI tank "
            "Node: 0x%02x, Flags: 0x%02x, Size: 0x%08x",
            g_attrOverrideHeader.iv_node, g_attrOverrideHeader.iv_flags,
            g_attrOverrideHeader.iv_valSize);

        theAttrOverrideSync().iv_overrideTank.setAttribute(
            g_attrOverrideHeader.iv_attrId,
            g_attrOverrideHeader.iv_targetType,
            g_attrOverrideHeader.iv_pos,
            g_attrOverrideHeader.iv_unitPos,
            g_attrOverrideHeader.iv_node,
            g_attrOverrideHeader.iv_flags,
            g_attrOverrideHeader.iv_valSize,
            &g_attrOverride);
    }
    else
    {
        // Convert the FAPI targeting type to TARGETING

        switch (g_attrOverrideHeader.iv_targetType)
        {
            case fapi2::TARGET_TYPE_SYSTEM:
                l_targetType = TARGETING::TYPE_SYS;
                break;
            case fapi2::TARGET_TYPE_DIMM:
                l_targetType = TARGETING::TYPE_DIMM;
                break;
            case fapi2::TARGET_TYPE_PROC_CHIP:
                l_targetType = TARGETING::TYPE_PROC;
                break;
            case fapi2::TARGET_TYPE_MEMBUF_CHIP:
                l_targetType = TARGETING::TYPE_MEMBUF;
                break;
            case fapi2::TARGET_TYPE_EX_CHIPLET:
                l_targetType = TARGETING::TYPE_EX;
                break;
            case fapi2::TARGET_TYPE_MBA_CHIPLET:
                l_targetType = TARGETING::TYPE_MBA;
                break;
            case fapi2::TARGET_TYPE_MCS_CHIPLET:
                l_targetType = TARGETING::TYPE_MCS;
                break;
            case fapi2::TARGET_TYPE_XBUS_ENDPOINT:
                l_targetType = TARGETING::TYPE_XBUS;
                break;
            case fapi2::TARGET_TYPE_ABUS_ENDPOINT:
                l_targetType = TARGETING::TYPE_ABUS;
                break;
            case fapi2::TARGET_TYPE_L4:
                l_targetType = TARGETING::TYPE_L4;
                break;
            case fapi2::TARGET_TYPE_CORE:
                l_targetType = TARGETING::TYPE_CORE;
                break;
            case fapi2::TARGET_TYPE_EQ:
                l_targetType = TARGETING::TYPE_EQ;
                break;
            case fapi2::TARGET_TYPE_MCA:
                l_targetType = TARGETING::TYPE_MCA;
                break;
            case fapi2::TARGET_TYPE_MCBIST:
                l_targetType = TARGETING::TYPE_MCBIST;
                break;
            case fapi2::TARGET_TYPE_CAPP:
                l_targetType = TARGETING::TYPE_CAPP;
                break;
            case fapi2::TARGET_TYPE_MC:
                l_targetType = TARGETING::TYPE_MC;
                break;
            case fapi2::TARGET_TYPE_MI:
                l_targetType = TARGETING::TYPE_MI;
                break;
            case fapi2::TARGET_TYPE_DMI:
                l_targetType = TARGETING::TYPE_DMI;
                break;
            case fapi2::TARGET_TYPE_OBUS:
                l_targetType = TARGETING::TYPE_OBUS;
                break;
            case fapi2::TARGET_TYPE_OBUS_BRICK:
                l_targetType = TARGETING::TYPE_OBUS_BRICK;
                break;
            case fapi2::TARGET_TYPE_SBE:
                l_targetType = TARGETING::TYPE_SBE;
                break;
            case fapi2::TARGET_TYPE_PPE:
                l_targetType = TARGETING::TYPE_PPE;
                break;
            case fapi2::TARGET_TYPE_PERV:
                l_targetType = TARGETING::TYPE_PERV;
                break;
            case fapi2::TARGET_TYPE_PEC:
                l_targetType = TARGETING::TYPE_PEC;
                break;
            case fapi2::TARGET_TYPE_PHB:
                l_targetType = TARGETING::TYPE_PHB;
                break;
            case fapi2::TARGET_TYPE_OMI:
                l_targetType = TARGETING::TYPE_OMI;
                break;
            case fapi2::TARGET_TYPE_OMIC:
                l_targetType = TARGETING::TYPE_OMIC;
                break;
            case fapi2::TARGET_TYPE_MCC:
                l_targetType = TARGETING::TYPE_MCC;
                break;
            case fapi2::TARGET_TYPE_OCMB_CHIP:
                l_targetType = TARGETING::TYPE_OCMB_CHIP;
                break;
            case fapi2::TARGET_TYPE_MEM_PORT:
                l_targetType = TARGETING::TYPE_MEM_PORT;
                break;
            default:
                l_targetType = TARGETING::TYPE_NA;
        }

        FAPI_IMP("directOverride: Applying override to TARG tank "
            "Id: 0x%08x, TargType: 0x%08x, Pos: 0x%04x, UPos: 0x%02x",
            g_attrOverrideHeader.iv_attrId, l_targetType,
            g_attrOverrideHeader.iv_pos, g_attrOverrideHeader.iv_unitPos);
        FAPI_IMP("directOverride: Applying override to TARG tank "
            "Node: 0x%02x, Flags: 0x%02x, Size: 0x%08x",
            g_attrOverrideHeader.iv_node, g_attrOverrideHeader.iv_flags,
            g_attrOverrideHeader.iv_valSize);

        TARGETING::Target::theTargOverrideAttrTank().setAttribute(
            g_attrOverrideHeader.iv_attrId,
            l_targetType,
            g_attrOverrideHeader.iv_pos,
            g_attrOverrideHeader.iv_unitPos,
            g_attrOverrideHeader.iv_node,
            g_attrOverrideHeader.iv_flags,
            g_attrOverrideHeader.iv_valSize,
            &g_attrOverride);
    }
#endif
}

//******************************************************************************
AttrOverrideSync & theAttrOverrideSync()
{
    return Singleton<AttrOverrideSync>::instance();
}

//******************************************************************************
AttrOverrideSync::AttrOverrideSync() {}

//******************************************************************************
AttrOverrideSync::~AttrOverrideSync() {}

//******************************************************************************
void AttrOverrideSync::monitorForFspMessages()
{
#ifndef __HOSTBOOT_RUNTIME
    FAPI_IMP("monitorForFspMessages starting");

    // Register a message queue with the mailbox
    msg_q_t l_pMsgQ = msg_q_create();
    errlHndl_t l_pErr = MBOX::msgq_register(MBOX::HB_HWPF_ATTR_MSGQ, l_pMsgQ);

    // Find out if attributes override has been attempted
    TARGETING::Target* l_pSys = nullptr;
    TARGETING::targetService().getTopLevelTarget(l_pSys);
    // Assert that l_pSys is no longer nullptr
    assert(l_pSys != nullptr, "AttrOverrideSync::monitorForFspMessages() "
           "expected top level target, but got nullptr.");

    if (l_pErr)
    {
        // In the unlikely event that registering fails, the code will commit an
        // error and then wait forever for a message to appear on the queue
        FAPI_ERR("monitorForFspMessages: Error registering msgq with mailbox");
        errlCommit(l_pErr, HWPF_COMP_ID);
    }

    while (1)
    {
        msg_t * l_pMsg = msg_wait(l_pMsgQ);

        if (l_pMsg->type == MSG_SET_OVERRIDES)
        {
            // FSP is setting attribute override(s).
            uint64_t l_tank = l_pMsg->data[0];
            TARGETING::AttributeTank::AttributeSerializedChunk l_chunk;
            l_chunk.iv_size = l_pMsg->data[1];
            l_chunk.iv_pAttributes = static_cast<uint8_t *>(l_pMsg->extra_data);

            if (SECUREBOOT::allowAttrOverrides() == false)
            {
                FAPI_ERR("monitorForFspMessages: Ignoring Set Overrides "
                         "Message (0x%X) from FSP since attribute overrides "
                         "are not allowed",
                         l_pMsg->type);

                // Checking if OVERRIDES_ATTEMPTED_FLAG has not been set to 1.
                // If so, then this is the first time attributes override is
                // attempted in an FSP, while in secure mode; in this case, log
                // an error stating that attributes override was attempted.
                if (!l_pSys->
                        getAttr<TARGETING::ATTR_OVERRIDES_ATTEMPTED_FLAG>())
                {
                    /*@
                     * @errortype
                     * @reasoncode       RC_ATTR_OVERRIDE_DISALLOWED
                     * @severity         ERRORLOG::ERRL_SEV_INFORMATIONAL
                     * @moduleid         MOD_FAPI2_MONITOR_FOR_FSP_MSGS
                     * @devdesc          Attribute overrides were rejected
                     *                   because system is in secure mode
                     * @custdesc         Action not allowed in secure mode
                     */
                    l_pErr = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_INFORMATIONAL,
                            MOD_FAPI2_MONITOR_FOR_FSP_MSGS,
                            RC_ATTR_OVERRIDE_DISALLOWED);
                    l_pErr->collectTrace(SECURE_COMP_NAME);
                    SECUREBOOT::addSecureUserDetailsToErrlog(l_pErr);
                    errlCommit(l_pErr, HWPF_COMP_ID);
                    l_pSys->
                        setAttr<TARGETING::ATTR_OVERRIDES_ATTEMPTED_FLAG>(true);
                }
            }
            else if (l_chunk.iv_pAttributes == NULL)
            {
                FAPI_ERR("monitorForFspMessages: tank %d, size %d, NULL data pointer",
                         l_tank, l_chunk.iv_size);
            }
            else if (l_tank == TARGETING::AttributeTank::TANK_LAYER_FAPI)
            {
                FAPI_INF(
                    "monitorForFspMessages: MSG_SET_OVERRIDES FAPI (size %lld)",
                    l_pMsg->data[1]);
                iv_overrideTank.deserializeAttributes(l_chunk);
            }
            else
            {
                FAPI_INF(
                    "monitorForFspMessages: MSG_SET_OVERRIDES TARG (size %lld)",
                    l_pMsg->data[1]);
                TARGETING::Target::theTargOverrideAttrTank().
                    deserializeAttributes(l_chunk);
            }

            // Free the memory
            free(l_pMsg->extra_data);
            l_pMsg->extra_data = NULL;
            l_pMsg->data[0] = 0;
            l_pMsg->data[1] = 0;

            if (msg_is_async(l_pMsg))
            {
                msg_free(l_pMsg);
            }
            else
            {
                // Send the message back as a response
                msg_respond(l_pMsgQ, l_pMsg);
            }
        }
        else if (l_pMsg->type == MSG_CLEAR_ALL_OVERRIDES)
        {
            // FSP is clearing all attribute overrides.
            FAPI_INF("monitorForFspMessages: MSG_CLEAR_ALL_OVERRIDES");
            iv_overrideTank.clearAllAttributes();
            TARGETING::Target::theTargOverrideAttrTank().clearAllAttributes();

            if (msg_is_async(l_pMsg))
            {
                msg_free(l_pMsg);
            }
            else
            {
                // Send the message back as a response
                msg_respond(l_pMsgQ, l_pMsg);
            }
        }
        else
        {
            FAPI_ERR("monitorForFspMessages: Unrecognized message 0x%x",
                     l_pMsg->type);
            free(l_pMsg->extra_data);
            l_pMsg->extra_data = NULL;
            msg_free(l_pMsg);
        }
    }
#endif
}

//******************************************************************************
errlHndl_t AttrOverrideSync::sendAttrsToFsp(
    const MAILBOX_MSG_TYPE i_msgType,
    const TARGETING::AttributeTank::TankLayer i_tankLayer,
    std::vector<TARGETING::AttributeTank::AttributeSerializedChunk> &
        io_attributes)
{
    errlHndl_t l_pErr = nullptr;

#ifndef __HOSTBOOT_RUNTIME
    std::vector<TARGETING::AttributeTank::AttributeSerializedChunk>::iterator
        l_itr;

    // Send Attributes through the mailbox chunk by chunk.
    for (l_itr = io_attributes.begin(); l_itr != io_attributes.end(); ++l_itr)
    {
        msg_t * l_pMsg = msg_allocate();
        l_pMsg->type = i_msgType;
        l_pMsg->data[0] = i_tankLayer;
        l_pMsg->data[1] = (*l_itr).iv_size;
        l_pMsg->extra_data = (*l_itr).iv_pAttributes;

        // Send the message and wait for a response, the response message is not
        // read, it just ensures that the code waits until the FSP is done
        // Note: A possible performance boost could be to send only the last
        //       message synchronously to avoid the small delay between each
        //       message
        l_pErr = MBOX::sendrecv(MBOX::FSP_HWPF_ATTR_MSGQ, l_pMsg);

        if (l_pErr)
        {
            FAPI_ERR("sendAttrsToFsp: Error sending to FSP");

            // If mailbox nullified the extra_data, that indicates it assumed
            // ownership of (and responsibility to free) the chunk data.
            if(l_pMsg->extra_data == nullptr)
            {
                (*l_itr).iv_pAttributes = nullptr;
            }
            msg_free(l_pMsg);
            l_pMsg = nullptr;
            break;
        }

        // On success, assume that mailbox freed the chunk data
        (*l_itr).iv_pAttributes = nullptr;
        msg_free(l_pMsg);
        l_pMsg = nullptr;
    }

    // Free any memory (only in error case will there be memory to free) and
    // clear the vector of Attribute Chunks
    for (l_itr = io_attributes.begin(); l_itr != io_attributes.end(); ++l_itr)
    {
        free((*l_itr).iv_pAttributes);
        (*l_itr).iv_pAttributes = nullptr;
    }
    io_attributes.clear();
#endif

    return l_pErr;
}

//******************************************************************************
void AttrOverrideSync::sendAttrOverridesAndSyncsToFsp()
{

#ifndef __HOSTBOOT_RUNTIME
    const uint32_t MAILBOX_CHUNK_SIZE = 4096;

    if (MBOX::mailbox_enabled())
    {
        errlHndl_t l_pErr = NULL;

        // Non-const overrides may have been cleared by being written to.
        // Therefore, clear the FSP Attribute Overrides for this node and
        // send the current set of overrides to the FSP for this node

        // Clear all current FSP Attribute Overrides for this node
        msg_t * l_pMsg = msg_allocate();
        l_pMsg->type = MSG_CLEAR_ALL_OVERRIDES;
        l_pMsg->data[0] = 0;
        l_pMsg->data[1] = 0;
        l_pMsg->extra_data = NULL;

        // Send the message
        l_pErr = MBOX::send(MBOX::FSP_HWPF_ATTR_MSGQ, l_pMsg);

        if (l_pErr)
        {
            FAPI_ERR(
                "sendAttrOverridesAndSyncsToFsp: Error clearing overrides");
            errlCommit(l_pErr, HWPF_COMP_ID);
            msg_free(l_pMsg);
            l_pMsg = NULL;
        }
        else
        {
            l_pMsg = NULL;

            // Send Hostboot Attribute Overrides to the FSP
            for (uint32_t i = TARGETING::AttributeTank::TANK_LAYER_FAPI;
                 i <= TARGETING::AttributeTank::TANK_LAYER_TARG; i++)
            {
                std::vector<TARGETING::AttributeTank::AttributeSerializedChunk>
                    l_attributes;

                // Note that NODE_FILTER_NOT_ALL_NODES retrieves all overrides
                // that are not for all nodes - i.e. overrides for this node.
                // The FSP already has all overrides for all nodes.
                if (i == TARGETING::AttributeTank::TANK_LAYER_FAPI)
                {
                    iv_overrideTank.serializeAttributes(
                        TARGETING::AttributeTank::ALLOC_TYPE_MALLOC,
                        MAILBOX_CHUNK_SIZE, l_attributes,
                        TARGETING::AttributeTank::NODE_FILTER_NOT_ALL_NODES);
                }
                else
                {
                    TARGETING::Target::theTargOverrideAttrTank().
                        serializeAttributes(
                            TARGETING::AttributeTank::ALLOC_TYPE_MALLOC,
                            MAILBOX_CHUNK_SIZE, l_attributes,
                            TARGETING::AttributeTank::
                                NODE_FILTER_NOT_ALL_NODES);
                }

                if (l_attributes.size())
                {
                    l_pErr = sendAttrsToFsp(MSG_SET_OVERRIDES,
                        static_cast<TARGETING::AttributeTank::TankLayer>(i),
                        l_attributes);

                    if (l_pErr)
                    {
                        FAPI_ERR("sendAttrOverridesAndSyncsToFsp: Error sending overrides (%d)", i);
                        errlCommit(l_pErr, HWPF_COMP_ID);
                        break;
                    }
                }
            }

            // Send Hostboot Attributes to Sync to the FSP
            for (uint32_t i = TARGETING::AttributeTank::TANK_LAYER_FAPI;
                 i <= TARGETING::AttributeTank::TANK_LAYER_TARG; i++)
            {
                std::vector<TARGETING::AttributeTank::AttributeSerializedChunk>
                    l_attributes;

                if (i == TARGETING::AttributeTank::TANK_LAYER_FAPI)
                {
                    iv_syncTank.serializeAttributes(
                        TARGETING::AttributeTank::ALLOC_TYPE_MALLOC,
                        MAILBOX_CHUNK_SIZE, l_attributes);
                }
                else
                {
                    TARGETING::Target::theTargSyncAttrTank().
                        serializeAttributes(
                            TARGETING::AttributeTank::ALLOC_TYPE_MALLOC,
                            MAILBOX_CHUNK_SIZE, l_attributes);
                }

                if (l_attributes.size())
                {
                    l_pErr = sendAttrsToFsp(MSG_SET_SYNC_ATTS,
                        static_cast<TARGETING::AttributeTank::TankLayer>(i),
                        l_attributes);

                    if (l_pErr)
                    {
                        FAPI_ERR("sendAttrOverridesAndSyncsToFsp: Error sending syncs (%d)", i);
                        errlCommit(l_pErr, HWPF_COMP_ID);
                        break;
                    }
                    else
                    {
                        // Clear Sync tank
                        if (i == TARGETING::AttributeTank::TANK_LAYER_FAPI)
                        {
                            iv_syncTank.clearAllAttributes();
                        }
                        else
                        {
                            TARGETING::Target::theTargSyncAttrTank().
                                clearAllAttributes();
                        }
                    }
                }
            }
        }
    }
#endif
}

//******************************************************************************
void AttrOverrideSync::sendFapiAttrSyncs()
{

#ifndef __HOSTBOOT_RUNTIME
    const uint32_t MAILBOX_CHUNK_SIZE = 4096;

    // Send Hostboot Attributes to Sync to the debug channel
    std::vector<TARGETING::AttributeTank::AttributeSerializedChunk>
      l_attributes;

    iv_syncTank.serializeAttributes(
                                    TARGETING::AttributeTank::ALLOC_TYPE_MALLOC,
                                    MAILBOX_CHUNK_SIZE, l_attributes);

    // Send Attributes through the mailbox chunk by chunk.
    std::vector<TARGETING::AttributeTank::AttributeSerializedChunk>::iterator
        l_itr;
    for (l_itr = l_attributes.begin(); l_itr != l_attributes.end(); ++l_itr)
    {
        // Send the message and wait for tool to recieve
        // address to go to zero (means that Cronus/debug framework has
        // consumed
        uint64_t l_addr = reinterpret_cast<uint64_t>((*l_itr).iv_pAttributes);
        Util::writeDebugCommRegs(Util::MSG_TYPE_ATTRDUMP,
                                 l_addr,
                                 (*l_itr).iv_size);

        // Freed the chunk data
        free((*l_itr).iv_pAttributes);
        (*l_itr).iv_pAttributes = NULL;

    }

    // Clear Sync tank
    l_attributes.clear();
    iv_syncTank.clearAllAttributes();

#endif
}

//******************************************************************************
void AttrOverrideSync::getAttrOverridesFromFsp()
{
#ifndef __HOSTBOOT_RUNTIME
    FAPI_IMP("Requesting Attribute Overrides from the FSP");
    errlHndl_t l_pErr = NULL;

    if (!SECUREBOOT::allowAttrOverrides())
    {
        FAPI_INF("AttrOverrideSync::getAttrOverridesFromFsp: skipping "
                 "since attribute overrides are not allowed");
        return;
    }

    msg_t * l_pMsg = msg_allocate();
    l_pMsg->type = MSG_GET_OVERRIDES;
    l_pMsg->data[0] = 0;
    l_pMsg->data[1] = 0;
    l_pMsg->extra_data = NULL;

    // Send the message and wait for a response, the response message is not
    // read, it just ensures that the code waits until the FSP is done sending
    // attribute overrides
    l_pErr = MBOX::sendrecv(MBOX::FSP_HWPF_ATTR_MSGQ, l_pMsg);

    if (l_pErr)
    {
        FAPI_ERR("getAttrOverridesFromFsp: Error sending to FSP");
        errlCommit(l_pErr, HWPF_COMP_ID);
    }

    msg_free(l_pMsg);
    l_pMsg = NULL;
#endif
}

//******************************************************************************
bool AttrOverrideSync::getAttrOverride(const AttributeId i_attrId,
                                const Target<TARGET_TYPE_ALL>* const i_pTarget,
                                       void * o_pVal) const
{
    // Very fast check to see if there are any overrides at all
    if (!(iv_overrideTank.attributesExist()))
    {
        return false;
    }

    // Check to see if there are any overrides for this attr ID
    if (!(iv_overrideTank.attributeExists(i_attrId)))
    {
        return false;
    }

    // This check after previous two for performance reasons
    if (!SECUREBOOT::allowAttrOverrides())
    {
        FAPI_INF("AttrOverrideSync::getAttrOverride: skipping "
                 "since attribute overrides are not allowed");
        return false;
    }

    // Do the work of figuring out the target's type/position/node and find out
    // if there is an override for this target
    uint32_t l_targetType = getTargetType(i_pTarget);

    // Get the Target pointer
    TARGETING::Target * l_pTarget =
        reinterpret_cast<TARGETING::Target*>(i_pTarget->get());
    uint16_t l_pos = 0;
    uint8_t l_unitPos = 0;
    uint8_t l_node = 0;
    l_pTarget->getAttrTankTargetPosData(l_pos, l_unitPos, l_node);

    FAPI_INF("getAttrOverride: Checking for override for ID: 0x%08x, "
             "TargType: 0x%08x, Pos/Upos/Node: 0x%08x",
             i_attrId, l_targetType,
             (static_cast<uint32_t>(l_pos) << 16) +
             (static_cast<uint32_t>(l_unitPos) << 8) + l_node);

    bool l_override = iv_overrideTank.getAttribute(i_attrId, l_targetType,
        l_pos, l_unitPos, l_node, o_pVal);

    if (l_override)
    {
        FAPI_INF("getAttrOverride: Returning Override for ID: 0x%08x",
                 i_attrId);
    }

    return l_override;
}

//******************************************************************************
bool AttrOverrideSync::getAttrOverrideFunc(const AttributeId i_attrId,
                             const Target<TARGET_TYPE_ALL>& i_pTarget,
                             void * o_pVal)
{

    return Singleton<AttrOverrideSync>::instance().getAttrOverride(i_attrId,
        &i_pTarget, o_pVal);
}

//******************************************************************************
void AttrOverrideSync::setAttrActions(const AttributeId i_attrId,
                                      const Target<TARGET_TYPE_ALL>* i_pTarget,
                                      const uint32_t i_size,
                                      const void * i_pVal)
{
    // Figure out if effort should be expended figuring out the target's type/
    // position in order to clear any non-const attribute overrides and/or to
    // store the attribute for syncing to Cronus

    bool l_clearAnyNonConstOverride = false;

    // Very fast check to see if there are any overrides at all for this Attr ID
    if (iv_overrideTank.attributesExist())
    {
        // Fast check to see if there are any overrides for this attr ID
        if (iv_overrideTank.attributeExists(i_attrId))
        {
            l_clearAnyNonConstOverride = true;
        }
    }

    bool l_syncAttribute = TARGETING::AttributeTank::syncEnabled();

    if (l_clearAnyNonConstOverride || l_syncAttribute)
    {
        uint32_t l_targetType = getTargetType(i_pTarget);

        // Get the Target pointer
        TARGETING::Target * l_pTarget =
            reinterpret_cast<TARGETING::Target*>(i_pTarget->get());
        uint16_t l_pos = 0;
        uint8_t l_unitPos = 0;
        uint8_t l_node = 0;
        l_pTarget->getAttrTankTargetPosData(l_pos, l_unitPos, l_node);

        if (l_clearAnyNonConstOverride)
        {
            // Clear any non const override for this attribute because the
            // attribute is being written
            iv_overrideTank.clearNonConstAttribute(i_attrId, l_targetType,
                l_pos, l_unitPos, l_node);
        }

        if (l_syncAttribute)
        {
            // Write the attribute to the SyncAttributeTank to sync to Cronus
            iv_syncTank.setAttribute(i_attrId, l_targetType, l_pos, l_unitPos,
                l_node, 0, i_size, i_pVal);
        }
    }
}

//******************************************************************************
void AttrOverrideSync::triggerAttrSync(fapi2::TargetType i_type,
                                       uint32_t i_fapiPos, uint32_t i_attrHash)
{
    uint8_t * l_buf = NULL;
    uint64_t l_totalBytes = 0;

    uint32_t l_targetCount = 0;
    iv_syncTank.clearAllAttributes();

    //Walk through all HB targets and see if there is a matching FAPI target
    //If so then get the list of ATTR for FAPI target and add to sync list
    for (TARGETING::TargetIterator target = TARGETING::targetService().begin();
         target != TARGETING::targetService().end();
         ++target)
    {
        // Get the Target pointer
        TARGETING::Target * l_pTarget = *target;

        //Get FAPI target
        TARGETING::TYPE l_type = l_pTarget->getAttr<TARGETING::ATTR_TYPE>();
        fapi2::TargetType l_fType = convertTargetingTypeToFapi2(l_type);

        if(l_fType == fapi2::TARGET_TYPE_NONE)
        {
            continue; //not a FAPI2 target -- skip to next target
        }

        //Check to see if looking for a specific type/fapi_pos
        if(i_type != fapi2::TARGET_TYPE_NONE)
        {
            if (i_type != l_fType) // types don't match, skip
            {
                continue;
            }

            //Now look for specific pos (if set)
            if((i_fapiPos != TARGETING::FAPI_POS_NA) && // specific pos
               (i_fapiPos != l_pTarget->getAttr<TARGETING::ATTR_FAPI_POS>()))
            {
                continue;
            }
        }

        //skip if not functional
        if(l_pTarget->getAttr<TARGETING::ATTR_HWAS_STATE>().functional
           != true)
        {
            continue;
        }

        TARGETING::EntityPath phys_path_ptr =
          l_pTarget->getAttr<TARGETING::ATTR_PHYS_PATH>();
        FAPI_INF("triggerAttrSync: HUID 0x%X, fapi type[%x] [%s]",
                 TARGETING::get_huid(l_pTarget), l_fType,
                 phys_path_ptr.toString());

        //Need a generic fapi target to use later
        fapi2::Target<TARGET_TYPE_ALL> l_fapiTarget( l_pTarget);

        // For membuf, see if it has CDIMMs
        uint8_t l_cdimm = 0;
        if (l_fType == TARGET_TYPE_MEMBUF_CHIP)
        {
            const auto l_mbas = l_fapiTarget.getChildren<fapi2::TARGET_TYPE_MBA>();
            if ( !l_mbas.empty() )
            {
                // Debug tool, ignore error handling
                fapi2::Target<TARGET_TYPE_MBA> l_mbaTarget(l_mbas[0]);
                FAPI_ATTR_GET(fapi2::ATTR_CEN_EFF_CUSTOM_DIMM,
                              l_mbaTarget, l_cdimm);
            }
        }

        //Determine the target location info
        uint16_t l_pos = 0;
        uint8_t l_unitPos = 0;
        uint8_t l_node = 0;
        l_pTarget->getAttrTankTargetPosData(l_pos, l_unitPos, l_node);

        //Loop on all fapi ATTR under this target type
        size_t l_elems = 0;
        const AttributeSyncInfo * l_attrs =
                            get_fapi_target_attr_array(l_fType, l_elems);

        //If NULL, nothing there to sync
        if(!l_attrs)
        {
            continue;
        }

        for(size_t i = 0; i < l_elems; i++)
        {
            //#@TODO RTC: 182602 -- Need to scrub through attributes for dimm target
            //For now, just skipping over BAD_DQ_BITMAP as it does not have a valid
            //lookup for cumulus
            //Look for specific ATTR
            if(((i_attrHash!= 0x0) &&  //looking for specific ATTR
               (i_attrHash != l_attrs[i].iv_attrId)) ||
               (l_attrs[i].iv_attrId == fapi2::ATTR_BAD_DQ_BITMAP))
            {
                continue;
            }

            // Skip CDIMM specific attributes for non-cdimms
            if (l_fType == TARGET_TYPE_MEMBUF_CHIP)
            {
                bool skip_attr = false;
                if (l_cdimm) // CDIMM
                {
                    for (uint8_t ii = 0; ii < NUM_OF_ISDIMM_ATTRS; ii++)
                    {
                         if (l_attrs[i].iv_attrId == ISDIMM_ATTR_ID[ii])
                         {
                             skip_attr = true;
                             break;
                         }
                    }
                }
                else // ISDIMM
                {
                    for (uint8_t ii = 0; ii < NUM_OF_CDIMM_ATTRS; ii++)
                    {
                         if (l_attrs[i].iv_attrId == CDIMM_ATTR_ID[ii])
                         {
                             skip_attr = true;
                             break;
                         }
                    }
                }

                // Skip attr if wrong dimm type to avoid error
                if (skip_attr == true)
                {
                    continue;
                }

            }

            // Write the attribute to the SyncAttributeTank to sync to Cronus
            size_t l_bytes =
              l_attrs[i].iv_attrElemSizeBytes * l_attrs[i].iv_dims[0] *
              l_attrs[i].iv_dims[1] * l_attrs[i].iv_dims[2] *
              l_attrs[i].iv_dims[3];

            //Get the data -- limit this to ATTR less than 1K in size
            //otherwise can cause memory fragmentation in cache contained HB.
            //Specifically this is to weed out the WOF_TABLE_DATA (128K)
            if(l_bytes > KILOBYTE)
            {
                FAPI_INF("triggerAttrSync: ATTR %x bigger [%x] than 1K... skipping",
                         l_attrs[i].iv_attrId, l_bytes);
                continue;
            }

            l_buf = reinterpret_cast<uint8_t *>(realloc(l_buf, l_bytes));
            ReturnCode l_rc =
              rawAccessAttr(
                        static_cast<fapi2::AttributeId>(l_attrs[i].iv_attrId),
                        l_fapiTarget, &l_buf[0]);

            // Write the attribute to the SyncAttributeTank to sync to Cronus
            errlHndl_t l_pErr = fapi2::rcToErrl(l_rc);
            if(!l_pErr)
            {
                iv_syncTank.setAttribute(l_attrs[i].iv_attrId, l_fType,
                                         l_pos, l_unitPos, l_node, 0,
                                         l_bytes, l_buf);
            }
            else
            {
                delete l_pErr; //Debug tool, ignore errors
            }

            l_targetCount++;
            l_totalBytes += l_bytes;
        }

        // Done with this target, release l_buf
        free(l_buf);
        l_buf = NULL;

        // Push the target attributes to debug tool
        sendFapiAttrSyncs();
    }

#ifndef __HOSTBOOT_RUNTIME
    //Let the tool know we are finished syncing with magic address
    Util::writeDebugCommRegs(Util::MSG_TYPE_ATTRDUMP,
                             0xFFFFCAFE, 0);
#endif

    FAPI_INF("triggerAttrSync - Finished sending all target attributes. "
             "Total targets %d, Total bytes %d", l_targetCount, l_totalBytes);
}

//******************************************************************************
void AttrOverrideSync::clearAttrOverrides()
{
    if (!SECUREBOOT::allowAttrOverrides())
    {
        FAPI_INF("AttrOverrideSync::clearAttrOverrides: skipping clear calls"
                 "since attribute overrides are not allowed");
        return;
    }
#ifndef __HOSTBOOT_RUNTIME
    // Debug Channel is clearing all attribute overrides.
    FAPI_INF("Debug Channel CLEAR_ALL_OVERRIDES");
    iv_overrideTank.clearAllAttributes();
    TARGETING::Target::theTargOverrideAttrTank().clearAllAttributes();
#endif
}

//******************************************************************************
void AttrOverrideSync::dynSetAttrOverrides()
{
    if (!SECUREBOOT::allowAttrOverrides())
    {
        FAPI_INF("AttrOverrideSync::dynSetAttrOverrides: skipping since "
                "attribute overrides are not allowed");
        return;
    }

#ifndef __HOSTBOOT_RUNTIME
    errlHndl_t err = nullptr;
    void * l_vaddr = nullptr;

    do
    {
        // Allocate a contiguous block of memory -- can just use malloc for
        // this because Cronus will use putmempba to load
        l_vaddr = malloc(VMM_DEBUG_COMM_SIZE);


        //Now masquerade this at the ATTR_TMP PNOR section so the underlying
        //ATTR override and bin file can be used as is
        PNOR::SectionInfo_t l_sectionInfo;
        l_sectionInfo.id = PNOR::ATTR_TMP;
        l_sectionInfo.name = "ATTR_TMP";
        l_sectionInfo.vaddr = reinterpret_cast<uint64_t>(l_vaddr);
        l_sectionInfo.flashAddr = 0xFFFFFFFF; //Not used
        l_sectionInfo.size = VMM_DEBUG_COMM_SIZE;
        l_sectionInfo.eccProtected = false;
        l_sectionInfo.sha512Version = false;
        l_sectionInfo.sha512perEC = false;
        l_sectionInfo.readOnly = true;
        l_sectionInfo.reprovision = false;
        l_sectionInfo.secure = false;


        //Send debug message to tool to update memory
        //Must clear data to actually alloction phys pages
        memset (l_vaddr, 0xFF, VMM_DEBUG_COMM_SIZE);
        uint64_t l_addr = mm_virt_to_phys(l_vaddr);
        FAPI_INF("virt[%p] phys[%llx]", l_vaddr, l_addr);
        Util::writeDebugCommRegs(Util::MSG_TYPE_ATTROVERRIDE,
                                 l_addr,
                                 VMM_DEBUG_COMM_SIZE);


        FAPI_INF("init: processing dynamic overrides");
        err = TARGETING::getAttrOverrides(l_sectionInfo, NULL);
        if (err)
        {
            FAPI_ERR("Failed getAttrOverrides from dyanmic set");
            errlCommit(err, HWPF_COMP_ID);
            break;
        }
    }while(0);

    // Attempt to clean up after ourselves
    free(l_vaddr);
    l_vaddr = nullptr;
#endif
}

//******************************************************************************
void AttrOverrideSync::dynAttrGet()
{
#ifndef __HOSTBOOT_RUNTIME
    void * l_vaddr = nullptr;

    do
    {
        // Create a memory block to serve as Debug Comm channel
        // Allocate a contiguous block of memory -- can just use malloc for
        // this because Cronus will use putmempba to load
        l_vaddr = malloc(VMM_DEBUG_COMM_SIZE);

        //Send debug message to tool to update memory
        //Must clear data to actually alloction phys pages
        memset (l_vaddr, 0xFF, VMM_DEBUG_COMM_SIZE);
        uint64_t l_addr = mm_virt_to_phys(l_vaddr);
        FAPI_INF("virt[%p] phys[%llx]", l_vaddr, l_addr);
        Util::writeDebugCommRegs(Util::MSG_TYPE_ATTRGET,
                                 l_addr,
                                 VMM_DEBUG_COMM_SIZE);

        uint32_t * l_targInfo =
          reinterpret_cast<uint32_t*>(l_vaddr);

        FAPI_INF("init: processing dynamic ATTR get");
        triggerAttrSync(static_cast<fapi2::TargetType>(
                          l_targInfo[Util::DEBUG_ATTRGET_FAPI_TYPE]),
                        l_targInfo[Util::DEBUG_ATTRGET_FAPI_POS],
                        l_targInfo[Util::DEBUG_ATTRGET_HASH]);
    }while(0);

    // Attempt to clean up after ourselves.
    free(l_vaddr);
    l_vaddr = nullptr;
#endif
}


//******************************************************************************
void AttrOverrideSync::setAttrActionsFunc(const AttributeId i_attrId,
                                      const Target<TARGET_TYPE_ALL>& i_pTarget,
                                          const uint32_t i_size,
                                          const void * i_pVal)
{
    Singleton<AttrOverrideSync>::instance().setAttrActions(i_attrId, &i_pTarget,
        i_size, i_pVal);
}


//******************************************************************************
uint32_t AttrOverrideSync::getTargetType(
                                      const Target<TARGET_TYPE_ALL>* i_pTarget)
{
    uint32_t l_targetType = fapi2::TARGET_TYPE_SYSTEM;

    if (i_pTarget != NULL)
    {
        l_targetType = i_pTarget->getType();
    }

    return l_targetType;
}

} // End fapi namespace
OpenPOWER on IntegriCloud