summaryrefslogtreecommitdiffstats
path: root/src/import/chips/centaur/procedures/hwp/memory/p9c_dimmBadDqBitmapAccessHwp.C
blob: 2cea0356f934ecdaee0f4d041bdc86b255d226f3 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/centaur/procedures/hwp/memory/p9c_dimmBadDqBitmapAccessHwp.C $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2017                        */
/* [+] 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 p9c_dimmBadDqBitmapAccessHwp.C
/// @brief FW Team HWP that accesses the Bad DQ Bitmap.
///
/// *HWP HWP Owner: Luke Mulkey <lwmulkey@us.ibm.com>
/// *HWP HWP Backup: Andre Marin <aamarin@us.ibm.com>
/// *HWP Team: Memory
/// *HWP Level: 2
/// *HWP Consumed by: HB:CI
///

#include <fapi2.H>
#include <p9c_dimmBadDqBitmapAccessHwp.H>

// DQ Data format in DIMM SPD
enum dq_data : size_t
{
    DIMM_BAD_DQ_MAGIC_NUMBER = 0xbadd4471,
    DIMM_BAD_DQ_VERSION = 1,
    ECC_DQ_BYTE_NUMBER_INDEX = 8,
    SPARE_DRAM_DQ_BYTE_NUMBER_INDEX = 9,
};

///
/// @class dimmBadDqDataFormat
/// @brief Structure that holds bad DQ data
///
struct dimmBadDqDataFormat
{
    uint32_t iv_magicNumber;
    uint8_t  iv_version;
    uint8_t  iv_reserved1;
    uint8_t  iv_reserved2;
    uint8_t  iv_reserved3;
    uint8_t  iv_bitmaps[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE];

    ///
    /// @brief default ctor
    ///
    dimmBadDqDataFormat() = default;

    ///
    /// @brief default dtor
    ///
    ~dimmBadDqDataFormat() = default;
};

extern "C"
{

    ///
    /// @brief Returns bits for unconnected spare DRAM.
    /// @param[in] i_mba Reference to MBA
    /// @param[in] i_dimm Reference to DIMM
    /// @param[out] o_spareByte Reference to the spare byte returned to caller.
    /// @return FAPI2_RC_SUCCESS iff okay
    ///
    fapi2::ReturnCode dimmGetDqBitmapSpareByte(const fapi2::Target<fapi2::TARGET_TYPE_MBA>& i_mba,
            const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_dimm,
            uint8_t (&o_spareByte)[MAX_RANKS_PER_DIMM])
    {
        // Spare DRAM Attribute: Returns spare DRAM availability for
        // all DIMMs associated with the target MBA.
        uint8_t l_mbaSpare[MAX_PORTS_PER_MBA][MAX_DIMM_PER_PORT][MAX_RANKS_PER_DIMM] = {};
        uint8_t l_mbaPort = 0;
        uint8_t l_dimm = 0;

        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CEN_VPD_DIMM_SPARE, i_mba,  l_mbaSpare));
        // Find the mba port this dimm is connected to
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CEN_MBA_PORT, i_dimm,  l_mbaPort));
        // Find the dimm number associated with this dimm
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CEN_MBA_DIMM, i_dimm,  l_dimm));

        // Iterate through each rank of this DIMM
        for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
        {
            // Handle spare DRAM configuration cases
            switch (l_mbaSpare[l_mbaPort][l_dimm][i])
            {
                case fapi2::ENUM_ATTR_CEN_VPD_DIMM_SPARE_NO_SPARE:
                    // Set DQ bits reflecting unconnected
                    // spare DRAM in caller's data
                    o_spareByte[i] = 0xFF;
                    break;

                case fapi2::ENUM_ATTR_CEN_VPD_DIMM_SPARE_LOW_NIBBLE:
                    o_spareByte[i] = 0x0F;
                    break;

                case fapi2::ENUM_ATTR_CEN_VPD_DIMM_SPARE_HIGH_NIBBLE:
                    o_spareByte[i] = 0xF0;
                    break;

                // As erroneous value will not be encountered.
                case fapi2::ENUM_ATTR_CEN_VPD_DIMM_SPARE_FULL_BYTE:
                default:
                    o_spareByte[i] = 0x0;
                    break;
            }
        }

    fapi_try_exit:
        return fapi2::current_err;
    }

    ///
    /// @brief Called by dimmBadDqBitmapAccessHwp() to query ATTR_EFF_DIMM_SPARE
    /// and set bits for unconnected spare DRAM in caller's data.
    /// @param[in] i_mba Reference to MBA Target
    /// @param[in] i_dimm Reference to DIMM Target
    /// @param[out]  o_data Reference to Bad DQ Bitmap set by
    ///                   the caller.  Only the SPARE_DRAM_DQ_BYTE_NUMBER_INDEX
    ///                   byte is modified by this function.
    /// @return FAPI2_RC_SUCCESS iff okay
    ///

    fapi2::ReturnCode dimmUpdateDqBitmapSpareByte(const fapi2::Target<fapi2::TARGET_TYPE_MBA>& i_mba,
            const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_dimm,
            uint8_t (&o_data)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE])
    {

        uint8_t spareByte[MAX_RANKS_PER_DIMM] = {};
        memset(spareByte, 0, sizeof(spareByte));

        FAPI_TRY(dimmGetDqBitmapSpareByte(i_mba, i_dimm, spareByte));

        for (uint32_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
        {
            o_data[i][SPARE_DRAM_DQ_BYTE_NUMBER_INDEX] |= spareByte[i];
        }

    fapi_try_exit:
        return fapi2::current_err;
    }

    ///
    /// @brief Called by dimmBadDqBitmapAccessHwp() to query
    /// ATTR_SPD_MODULE_MEMORY_BUS_WIDTH in order to determine
    /// ECC support for this DIMM.  This function will set
    /// bits in the caller's data if ECC lines are not present.
    /// @param[in] i_dimm Reference to DIMM Target<fapi2::TARGET_TYPE_MBA>.
    /// @param[out] o_data Reference to Bad DQ Bitmap set by
    ///                   the caller.  Only the ECC_DQ_BYTE_NUMBER_INDEX
    ///                    byte is modified by this function.
    /// @return FAPI2_RC_SUCCESS iff okay
    ////
    fapi2::ReturnCode dimmUpdateDqBitmapEccByte( const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_dimm,
            uint8_t (&o_data)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE])
    {
        // Memory Bus Width Attribute
        uint8_t l_eccBits = 0;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CEN_SPD_MODULE_MEMORY_BUS_WIDTH, i_dimm, l_eccBits));

        // The ATTR_SPD_MODULE_MEMORY_BUS_WIDTH contains ENUM values
        // for bus widths of 8, 16, 32, and 64 bits both with ECC
        // and without ECC.  WExx ENUMS deonote the ECC extension
        // is present, and all have bit 3 set.  Therefore,
        // it is only required to check against the WE8 = 0x08 ENUM
        // value in order to determine if ECC lines are present.

        // If ECCs are disconnected
        if (!(fapi2::ENUM_ATTR_CEN_SPD_MODULE_MEMORY_BUS_WIDTH_WE8 & l_eccBits))
        {
            // Iterate through each rank and set DQ bits in
            // caller's data.
            for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
            {
                // Set DQ bits in caller's data
                o_data[i][ECC_DQ_BYTE_NUMBER_INDEX] = 0xFF;
            }
        }

    fapi_try_exit:
        return fapi2::current_err;
    }

    ///
    /// @brief Called by dimmBadDqBitmapAccessHwp() to query ATTR_SPD_BAD_DQ_DATA
    /// @param[in] i_mba Reference to MBA Target
    /// @param[in] i_dimm Reference to DIMM Target
    /// @param[out] o_data Reference to Bad DQ Bitmap set by this function
    /// @param[in] i_wiringData Reference to Centaur DQ to DIMM Connector DQ Wiring attribute.
    /// @param[in] i_allMnfgFlags Manufacturing flags bitmap
    /// @return FAPI2_RC_SUCCESS iff okay
    ///
    fapi2::ReturnCode dimmBadDqBitmapGet(const fapi2::Target<fapi2::TARGET_TYPE_MBA>& i_mba,
                                         const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_dimm,
                                         uint8_t (&o_data)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE],
                                         const uint8_t (&i_wiringData)[DIMM_DQ_NUM_DQS],
                                         const uint64_t i_allMnfgFlags)
    {

        // DQ SPD Attribute
        uint8_t (&l_spdData)[DIMM_DQ_SPD_DATA_SIZE] =
            *(reinterpret_cast<uint8_t(*)[DIMM_DQ_SPD_DATA_SIZE]>(new uint8_t[DIMM_DQ_SPD_DATA_SIZE]()));

        // memset to avoid known syntax issue with previous compiler versions
        // and ensure zero initialized array.
        memset(l_spdData, 0, sizeof(l_spdData));

        dimmBadDqDataFormat* l_pSpdData = reinterpret_cast<dimmBadDqDataFormat*>(l_spdData);

        // Pointer which will be used to initialize a clean bitmap during
        // manufacturing mode
        uint8_t (*l_pBuf)[DIMM_DQ_RANK_BITMAP_SIZE] = nullptr;

        // Get the SPD DQ attribute
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CEN_SPD_BAD_DQ_DATA, i_dimm,  l_spdData));

        // Zero caller's data
        memset(o_data, 0, sizeof(o_data));

        // Check the magic number and version number. Note that the
        // magic number is stored in SPD in big endian format and
        // platforms of any endianness can access it
        if ((be32toh(l_pSpdData->iv_magicNumber) != DIMM_BAD_DQ_MAGIC_NUMBER) ||
            (l_pSpdData->iv_version != DIMM_BAD_DQ_VERSION))
        {
            FAPI_INF("SPD DQ not initialized");
        }
        else
        {
            // Translate bitmap from DIMM DQ to Centaur DQ point of view
            // for each rank
            for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
            {
                // Iterate through all the DQ bits in the rank
                for (uint8_t j = 0; j < DIMM_DQ_NUM_DQS; ++j)
                {
                    // There is a byte for each 8 DQs, j/8 gives the
                    // byte number. The MSB in each byte is the lowest
                    // DQ, (0x80 >> (j % 8)) gives the bit mask
                    // corresponding to the DQ within the byte
                    const size_t BYTE_NUM = j / 8;
                    const size_t DQ_BIT_MASK = 0x80 >> (j % 8);

                    if ((l_pSpdData->iv_bitmaps[i][BYTE_NUM]) & DQ_BIT_MASK)
                    {
                        // DIMM DQ bit is set in SPD data.
                        // Set Centaur DQ bit in caller's data.
                        // The wiring data maps Centaur DQ to DIMM DQ
                        // Find the Centaur DQ that maps to this DIMM DQ
                        uint8_t k = 0;

                        for (; k < DIMM_DQ_NUM_DQS; ++k)
                        {
                            if (i_wiringData[k] == j)
                            {
                                o_data[i][k / 8] |= (0x80 >> (k % 8));
                                break;
                            }
                        }

                        if (k == DIMM_DQ_NUM_DQS)
                        {
                            FAPI_INF("Centaur DQ not found for %d!", j);
                        }
                    }
                }// DIMM_DQ_NUM_DQS
            } // MAX_RANKS_PER_DIMM
        }// end else

        // Set bits for any unconnected DQs.
        // First, check ECC.
        FAPI_TRY(dimmUpdateDqBitmapEccByte(i_dimm, o_data));

        // Check spare DRAM
        FAPI_TRY(dimmUpdateDqBitmapSpareByte(i_mba, i_dimm, o_data));

        // If system is in DISABLE_DRAM_REPAIRS mode
        if (i_allMnfgFlags & fapi2::ENUM_ATTR_MNFG_FLAGS_MNFG_DISABLE_DRAM_REPAIRS)
        {
            // Create a local zero-initialized bad dq bitmap
            l_pBuf = new uint8_t[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE]();

            uint8_t (&l_data)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE] =
                *(reinterpret_cast<uint8_t(*)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE]>(l_pBuf));

            // memset to avoid known syntax issue with previous
            // compiler versions and ensure zero initialized array.
            memset(l_data, 0, sizeof(l_data));

            // Check ECC.
            FAPI_TRY(dimmUpdateDqBitmapEccByte(i_dimm, l_data));
            // Check spare DRAM
            FAPI_TRY(dimmUpdateDqBitmapSpareByte(i_mba, i_dimm, l_data));

            // Compare l_data, which represents a bad dq bitmap with the
            // appropriate spare/ECC bits set (if any) and all other DQ
            // lines functional, to caller's o_data.
            // If discrepancies are found, we know this is the result of
            // a manufacturing mode process and these bits should not be
            // recorded.
            for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
            {
                for (uint8_t j = 0; j < (DIMM_DQ_RANK_BITMAP_SIZE); ++j)
                {
                    // Create and log fapi2 error if discrepancies were found
                    // Get this DIMM's position
                    uint32_t l_dimm_pos = 0;
                    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_POS, i_dimm,  l_dimm_pos));

                    FAPI_ASSERT_NOEXIT(o_data[i][j] != l_data[i][j],
                                       fapi2::CEN_BAD_DQ_MFG_MODE_BITS_FOUND_DURING_GET().
                                       set_CLEAN_BAD_DQ_BITMAP_RANK0(l_data[0][j]).
                                       set_CLEAN_BAD_DQ_BITMAP_RANK1(l_data[1][j]).
                                       set_CLEAN_BAD_DQ_BITMAP_RANK2(l_data[2][j]).
                                       set_CLEAN_BAD_DQ_BITMAP_RANK3(l_data[3][j]).
                                       set_CURRENT_BAD_DQ_BITMAP_RANK0(o_data[0][j]).
                                       set_CURRENT_BAD_DQ_BITMAP_RANK1(o_data[1][j]).
                                       set_CURRENT_BAD_DQ_BITMAP_RANK2(o_data[2][j]).
                                       set_CURRENT_BAD_DQ_BITMAP_RANK3(o_data[3][j]).
                                       set_DIMM(i_dimm),
                                       "Read requested while in DISABLE_DRAM_REPAIRS mode found"
                                       " extra bad bits set for rank:%d, DQ rank bitmap num:%d, DIMM pos: %d",
                                       i, j, l_dimm_pos);
                }// bitmap size
            }// ranks

            // correct the output bit map
            for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
            {
                for (uint8_t j = 0; j < (DIMM_DQ_RANK_BITMAP_SIZE); ++j)
                {
                    o_data[i][j] = l_data[i][j];
                }
            }// end for
        }

        delete [] &l_spdData;
        delete [] l_pBuf;

    fapi_try_exit:
        return fapi2::current_err;
    }

    ///
    /// @brief Called by dimmBadDqBitmapAccessHwp() to set ATTR_SPD_BAD_DQ_DATA
    /// Also checks if a bad Dq bit is set by first calling dimmBadDqBitmapGet()
    /// and sets ATTR_RECONFIGURE_LOOP with the 'OR' of the current value and
    /// the fapi2 enum BAD_DQ_BIT_SET if appropriate
    /// @param[in] i_mba Reference to MBA Target<fapi2::TARGET_TYPE_MBA>.
    /// @param[in] i_dimm Reference to DIMM Target<fapi2::TARGET_TYPE_MBA>.
    /// @param[in] i_data Reference to Bad DQ Bitmap set by the caller
    /// @param[in] i_wiringData Reference to Centaur DQ to DIMM Connector DQ Wiring attribute.
    /// @param[in] i_allMnfgFlags Manufacturing flags bitmap
    /// @return FAPI2_RC_SUCCESS
    ///

    fapi2::ReturnCode dimmBadDqBitmapSet( const fapi2::Target<fapi2::TARGET_TYPE_MBA>& i_mba,
                                          const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_dimm,
                                          const uint8_t (&i_data)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE],
                                          const uint8_t (&i_wiringData)[DIMM_DQ_NUM_DQS],
                                          const uint64_t i_allMnfgFlags)
    {
        // Read current BadDqBitmap into l_prev_data
        uint8_t l_prev_data[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE] = {};
        bool badDQSet = false;

        // DQ SPD Attribute
        uint8_t (&l_spdData)[DIMM_DQ_SPD_DATA_SIZE] =
            *(reinterpret_cast<uint8_t(*)[DIMM_DQ_SPD_DATA_SIZE]>
              (new uint8_t[DIMM_DQ_SPD_DATA_SIZE]()));

        fapi2::ATTR_RECONFIGURE_LOOP_Type l_reconfigAttr = 0;
        dimmBadDqDataFormat* l_pSpdData = reinterpret_cast<dimmBadDqDataFormat*>(l_spdData);

        // Pointer which will be used to initialize a clean bitmap during
        // manufacturing mode
        uint8_t (*l_pBuf)[DIMM_DQ_RANK_BITMAP_SIZE] = nullptr;
        uint8_t spareByte[MAX_RANKS_PER_DIMM] = {};

        FAPI_TRY(dimmBadDqBitmapGet(i_mba, i_dimm, l_prev_data, i_wiringData, i_allMnfgFlags));

        // Check if Bad DQ bit set
        for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
        {
            for (uint8_t j = 0; j < (DIMM_DQ_RANK_BITMAP_SIZE); ++j)
            {
                if (i_data[i][j] != l_prev_data[i][j])
                {
                    badDQSet = true;
                    break;
                }
            }

            if (badDQSet)
            {
                break;
            }
        }

        // Set ATTR_RECONFIGURE_LOOP to indicate a bad DqBitMap was set
        if (badDQSet)
        {
            FAPI_INF("Reconfigure needed, Bad DQ set");

            FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_RECONFIGURE_LOOP, fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>(), l_reconfigAttr));

            // 'OR' values in case of multiple reasons for reconfigure
            l_reconfigAttr |= fapi2::ENUM_ATTR_RECONFIGURE_LOOP_BAD_DQ_BIT_SET;

            FAPI_TRY(FAPI_ATTR_SET(fapi2::ATTR_RECONFIGURE_LOOP, fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>(), l_reconfigAttr));
        }

        // memset to avoid known syntax issue with previous compiler versions
        // and ensure zero initialized array.
        memset(l_spdData, 0, sizeof(l_spdData));

        // If system is in DISABLE_DRAM_REPAIRS mode
        if (i_allMnfgFlags & fapi2::ENUM_ATTR_MNFG_FLAGS_MNFG_DISABLE_DRAM_REPAIRS)
        {
            // Create a local zero-initialized bad dq bitmap
            l_pBuf = new uint8_t[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE]();

            uint8_t (&l_data)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE] =
                *(reinterpret_cast<uint8_t(*)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE]>(l_pBuf));

            // memset to avoid known syntax issue with previous
            // compiler versions and ensure zero initialized array.
            memset(l_data, 0, sizeof(l_data));

            // Check ECC.
            FAPI_TRY(dimmUpdateDqBitmapEccByte(i_dimm, l_data));
            // Check spare DRAM
            FAPI_TRY(dimmUpdateDqBitmapSpareByte(i_mba, i_dimm, l_data));

            // Compare l_data, which represents a bad dq bitmap with the
            // appropriate spare/ECC bits set (if any) and all other DQ
            // lines functional, to caller's i_data.
            // If discrepancies are found, we know this is the result of
            // a manufacturing mode process and these bits should not be
            // recorded.
            for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
            {
                for (uint8_t j = 0; j < (DIMM_DQ_RANK_BITMAP_SIZE); ++j)
                {
                    // Create and log fapi2 error if discrepancies were found
                    // Get this DIMM's position
                    uint32_t l_dimm_pos = 0;
                    FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_POS, i_dimm,  l_dimm_pos));

                    FAPI_ASSERT(i_data[i][j] != l_data[i][j],
                                fapi2::CEN_BAD_DQ_MFG_MODE_BITS_FOUND_DURING_SET().
                                set_CLEAN_BAD_DQ_BITMAP_RANK0(l_data[0][j]).
                                set_CLEAN_BAD_DQ_BITMAP_RANK1(l_data[1][j]).
                                set_CLEAN_BAD_DQ_BITMAP_RANK2(l_data[2][j]).
                                set_CLEAN_BAD_DQ_BITMAP_RANK3(l_data[3][j]).
                                set_UPDATE_BAD_DQ_BITMAP_RANK0(i_data[0][j]).
                                set_UPDATE_BAD_DQ_BITMAP_RANK1(i_data[1][j]).
                                set_UPDATE_BAD_DQ_BITMAP_RANK2(i_data[2][j]).
                                set_UPDATE_BAD_DQ_BITMAP_RANK3(i_data[3][j]).
                                set_DIMM(i_dimm),
                                "Write requested while in DISABLE_DRAM_REPAIRS mode"
                                " extra bad bits set for rank:%d, DQ rank bitmap num:%d, DIMM pos: %d",
                                i, j, l_dimm_pos);
                }
            }
        }

        // Set up the data to write to SPD
        l_pSpdData->iv_magicNumber = htobe32(DIMM_BAD_DQ_MAGIC_NUMBER);
        l_pSpdData->iv_version = DIMM_BAD_DQ_VERSION;
        l_pSpdData->iv_reserved1 = 0;
        l_pSpdData->iv_reserved2 = 0;
        l_pSpdData->iv_reserved3 = 0;
        memset(l_pSpdData->iv_bitmaps, 0, sizeof(l_pSpdData->iv_bitmaps));

        // Get the spare byte
        memset(spareByte, 0, sizeof(spareByte));

        FAPI_TRY(dimmGetDqBitmapSpareByte(i_mba, i_dimm, spareByte));

        // Translate bitmap from Centaur DQ to DIMM DQ point of view for
        // each rank
        for (uint8_t i = 0; i < MAX_RANKS_PER_DIMM; ++i)
        {
            // Iterate through all the DQ bits in the rank
            for (uint8_t j = 0; j < DIMM_DQ_NUM_DQS; ++j)
            {
                const size_t BYTE_NUM = j / 8;
                const size_t DQ_BIT_MASK = 0x80 >> (j % 8);

                if ((BYTE_NUM) == SPARE_DRAM_DQ_BYTE_NUMBER_INDEX)
                {
                    // The spareByte can be one of: 0x00 0x0F 0xF0 0xFF
                    // If a bit is set, then that spare is unconnected
                    // so continue to the next num_dqs, do not translate
                    if (spareByte[i] & DQ_BIT_MASK)
                    {
                        continue;
                    }
                }

                if ((i_data[i][BYTE_NUM]) & DQ_BIT_MASK)
                {
                    // Centaur DQ bit set in callers data.
                    // Set DIMM DQ bit in SPD data.
                    // The wiring data maps Centaur DQ to DIMM DQ
                    uint8_t dBit = i_wiringData[j];
                    l_pSpdData->iv_bitmaps[i][dBit / 8] |= (0x80 >> (dBit % 8));
                }
            }// dq
        }// ranks

        // Set the SPD DQ attribute
        FAPI_TRY(FAPI_ATTR_SET(fapi2::ATTR_CEN_SPD_BAD_DQ_DATA, i_dimm, l_spdData));

    fapi_try_exit:
        delete [] &l_spdData;
        delete [] l_pBuf;

        return fapi2::current_err;
    }

    ///
    /// @brief FW Team HWP that accesses the Bad DQ Bitmap.
    ///        It accesses the raw data from DIMM SPD and does
    ///        any necessary processing to turn it into a
    ///        bitmap from a Centaur DQ point of view. If the data in SPD is not
    ///        valid then it has never been written and all zeroes are returned (no
    ///        bad DQs).
    ///
    /// This HWP should be called by HWP/PLAT code to access the BAD DQ Bitmap
    ///
    /// @param[in] i_mba Reference to MBA Target
    /// @param[in] i_dimm Reference to DIMM Target
    /// @param[in,out] io_data Reference to bad DQ bitmap data for the DIMM.
    /// @param[in] i_get True if getting DQ Bitmap data. False if setting data.
    /// @return FAPI2_RC_SUCCESS iff okay
    /// @note that the MSB of each byte corresponds to the lowest DQ.
    /// if (data[1][0] == 0x80) then rank 1, Centaur DQ0 is bad
    /// if (data[1][0] == 0x40) then rank 1, Centaur DQ1 is bad
    /// if (data[1][1] == 0x20) then rank 1, Centaur DQ10 is bad
    ///
    fapi2::ReturnCode dimmBadDqBitmapAccessHwp(const fapi2::Target<fapi2::TARGET_TYPE_MBA>& i_mba,
            const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_dimm,
            uint8_t (&io_data)[MAX_RANKS_PER_DIMM][DIMM_DQ_RANK_BITMAP_SIZE],
            const bool i_get)
    {
        // Note the use of heap based arrays to avoid large stack allocations
        // Centaur DQ to DIMM Connector DQ Wiring attribute.
        uint8_t (&l_wiringData)[DIMM_DQ_NUM_DQS] =
            *(reinterpret_cast<uint8_t(*)[DIMM_DQ_NUM_DQS]>
              (new uint8_t[DIMM_DQ_NUM_DQS]()));

        // memset to avoid known syntax issue with previous compiler versions
        // and ensure zero initialized array.
        memset(l_wiringData, 0, sizeof(l_wiringData));

        // Manufacturing flags attribute
        uint64_t l_allMnfgFlags = 0;

        // Get the manufacturing flags bitmap to be used in both get and set
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_MNFG_FLAGS, fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>(), l_allMnfgFlags));

        // Get the Centaur DQ to DIMM Connector DQ Wiring attribute.
        // Note that for C-DIMMs, this will return a simple 1:1 mapping.
        // This code cannot tell the difference between C-DIMMs and IS-DIMMs.
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_CEN_DQ_TO_DIMM_CONN_DQ,
                               i_dimm, l_wiringData));

        if (i_get)
        {
            FAPI_INF("Getting bitmap");
            FAPI_TRY(dimmBadDqBitmapGet(i_mba, i_dimm, io_data, l_wiringData, l_allMnfgFlags));
        }
        else
        {
            FAPI_INF("Setting bitmap");
            FAPI_TRY(dimmBadDqBitmapSet(i_mba, i_dimm, io_data, l_wiringData, l_allMnfgFlags));
        }

    fapi_try_exit:
        delete [] &l_wiringData;
        FAPI_DBG("End");

        return fapi2::current_err;
    }

}// extern C
OpenPOWER on IntegriCloud