summaryrefslogtreecommitdiffstats
path: root/src/hwpf/src/plat_ring_traverse.C
blob: 9b4466a758e565f6dac2c0cd590cc8b7ce3bc362 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/hwpf/src/plat_ring_traverse.C $                           */
/*                                                                        */
/* OpenPOWER sbe 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                                                     */

#include <plat_ring_traverse.H>

#include <p9_putRingUtils.H> // for RS4 decompression utilities
#include <sbeXipUtils.H>
#include <fapi2_attribute_service.H> // for FAPI_ATTR_GET
#include <plat_target_utils.H> // for plat_getChipTarget

// SEEPROM start address
const uint32_t g_seepromAddr = SBE_SEEPROM_BASE_ORIGIN;
using namespace RING_TYPES;
const uint32_t CACHE_CONTAINED_MODE = 4;
const uint32_t RISK_LEVEL_MODE = 1;
#define CACHE_CONTAINED_MODE_OFFSET_IN_TOR 1
#define RISK_LEVEL_MODE_OFFSET_IN_TOR      2
#define OVERRIDE_VARIANT_SIZE 1


///
/// @brief This is a plat pecific (SBE Plat) function that locates the
///        Ring Container in the image and calls the functin to decompress the
///        RS4 string and apply it to the hardware.
/// @param i_target The target of Ring apply.
/// @param i_ringID The Ring ID that identifies the ring to be applied.
/// @return FAPI2_RC_SUCCESS on success, else error code.
///
fapi2::ReturnCode findRS4InImageAndApply(
                        const fapi2::Target<fapi2::TARGET_TYPE_ALL>& i_target,
                        const RingID i_ringID,
                        const fapi2::RingMode i_ringMode)
{
    SBE_TRACE(">> findRS4InImageAndApply");

    fapi2::ReturnCode l_rc = fapi2::FAPI2_RC_SUCCESS;
    bool l_applyOverride = false;

    do
    {
       //Apply scanring from .ring section

        // Get the address of the Section-TOR
        P9XipHeader *l_hdr = getXipHdr();
        P9XipSection *l_section =
            &(l_hdr->iv_section[P9_XIP_SECTION_SBE_RINGS]);

        if (!(l_section->iv_offset))
        {
            SBE_TRACE("No ring data in .RING section");
            break;
        }

        SectionTOR *l_sectionTOR = (SectionTOR *)(g_seepromAddr +
                                    l_section->iv_offset);

        l_rc = getRS4ImageFromTor(i_target,i_ringID,l_sectionTOR,
                                  l_applyOverride,
                                  l_section->iv_offset,
                                  i_ringMode);

        if(l_rc != fapi2::FAPI2_RC_SUCCESS)
        {
            break;
        }

       //Apply scanring from .ring section
        l_applyOverride = true;
        l_section = NULL;
        l_section =
            &(l_hdr->iv_section[P9_XIP_SECTION_SBE_OVERRIDES]);

        if (!(l_section->iv_offset))
        {
            SBE_TRACE("No ring data in .OVERRIDE section");
            break;
        }

        l_sectionTOR = NULL;
        l_sectionTOR = (SectionTOR *)(g_seepromAddr +
                                    l_section->iv_offset);


        l_rc = getRS4ImageFromTor(i_target,i_ringID,l_sectionTOR,
                                  l_applyOverride,
                                  l_section->iv_offset,
                                  i_ringMode);
    }while(0);

    return l_rc;
}

fapi2::ReturnCode getRS4ImageFromTor(
                        const fapi2::Target<fapi2::TARGET_TYPE_ALL>& i_target,
                        const RingID i_ringID,
                        SectionTOR *i_sectionTOR,
                        bool i_applyOverride,
                        const uint32_t i_sectionOffset,
                        const fapi2::RingMode i_ringMode)
{

    // Determine the Offset ID and Ring Type for the given Ring ID.
    uint32_t l_torOffset = 0;
    RINGTYPE l_ringType = COMMON_RING;
    CHIPLET_TYPE l_chipLetType;
    fapi2::ReturnCode l_rc = fapi2::FAPI2_RC_SUCCESS;
    do
    {

        getRingProperties(i_ringID, l_torOffset, l_ringType,l_chipLetType);
        if(INVALID_RING == l_torOffset)
        {
            SBE_TRACE("Invalid Ring ID - %d", i_ringID);
            l_rc = fapi2::FAPI2_RC_INVALID_PARAMETER;
            break;
        }

        CHIPLET_DATA l_chipletData;
        l_chipletData.iv_base_chiplet_number = 0;
        l_chipletData.iv_num_common_rings = 0;
        l_chipletData.iv_num_instance_rings = 0;

        uint8_t l_chipletID = i_target.getChipletNumber();
        uint16_t l_cpltRingVariantSz = 0;
        uint32_t l_sectionOffset = 0;
        uint8_t l_CC_offset = 0;
        uint8_t l_RL_offset = 1;
        switch(l_chipLetType)
        {
            case PERV_TYPE: // PERV
                l_chipletData = PERV::g_pervData;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(PERV::RingVariants) /
                                       sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_PERV_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_PERV_INSTANCE_RING;
                }

                break;

            case N0_TYPE: // Nest - N0
                l_chipletData = N0::g_n0Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(N0::RingVariants) /
                                        sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_N0_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_N0_INSTANCE_RING;
                }

                break;

            case N1_TYPE: // Nest - N1
                l_chipletData = N1::g_n1Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(N1::RingVariants) /
                                        sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_N1_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_N1_INSTANCE_RING;
                }

                break;

            case N2_TYPE: // Nest - N2
                l_chipletData = N2::g_n2Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                     (sizeof(N2::RingVariants) /
                                      sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_N2_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_N2_INSTANCE_RING;
                }

                break;

            case N3_TYPE: // Nest - N3
                l_chipletData = N3::g_n3Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(N3::RingVariants) /
                                       sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_N3_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_N3_INSTANCE_RING;
                }

                break;

            case XB_TYPE: // XB - XBus2
                l_chipletData = XB::g_xbData;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(XB::RingVariants) /
                                       sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_XB_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_XB_INSTANCE_RING;
                }

                break;

            case MC_TYPE: // MC - MC23
                l_chipletData = MC::g_mcData;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                     (sizeof(MC::RingVariants)/
                                     sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_MC_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_MC_INSTANCE_RING;
                }

                break;

            case OB0_TYPE: // OB0
                l_chipletData = OB0::g_ob0Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(OB0::RingVariants) /
                                       sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_OB0_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_OB0_INSTANCE_RING;
                }

                break;
            case OB1_TYPE: // OB1
                l_chipletData = OB1::g_ob1Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(OB1::RingVariants) /
                                       sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_OB1_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_OB1_INSTANCE_RING;
                }

                break;
            case OB2_TYPE: // OB2
                l_chipletData = OB2::g_ob2Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                     (sizeof(OB2::RingVariants) /
                                      sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_OB2_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_OB2_INSTANCE_RING;
                }

                break;
            case OB3_TYPE: // OB3
                l_chipletData = OB3::g_ob3Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(OB3::RingVariants) /
                                        sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_OB3_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_OB3_INSTANCE_RING;
                }

                break;


            case PCI0_TYPE: // PCI - PCI0
                l_chipletData = PCI0::g_pci0Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(PCI0::RingVariants) /
                                        sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_PCI0_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_PCI0_INSTANCE_RING;
                }

                break;

            case PCI1_TYPE: // PCI - PCI1
                l_chipletData = PCI1::g_pci1Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(PCI1::RingVariants) /
                                        sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_PCI1_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_PCI1_INSTANCE_RING;
                }

                break;

            case PCI2_TYPE: // PCI - PCI2
                l_chipletData = PCI2::g_pci2Data;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(PCI2::RingVariants) /
                                        sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_PCI2_COMMON_RING;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_PCI2_INSTANCE_RING;
                }

                break;

            case EQ_TYPE: // EQ - Quad 0 - Quad 5
                l_chipletData = EQ::g_eqData;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      ( sizeof(EQ::RingVariants) /
                                        sizeof(l_cpltRingVariantSz));
                 

                l_sectionOffset = i_sectionTOR->TOC_EQ_COMMON_RING;
                l_CC_offset = 1;
                l_RL_offset = 2;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_EQ_INSTANCE_RING;
                }

                break;

            case EC_TYPE: // EC - Core 0 - 23
                l_chipletData = EC::g_ecData;
                l_cpltRingVariantSz = i_applyOverride ? OVERRIDE_VARIANT_SIZE :
                                      (sizeof(EC::RingVariants) /
                                       sizeof(l_cpltRingVariantSz));

                l_sectionOffset = i_sectionTOR->TOC_EC_COMMON_RING;
                l_CC_offset = 1;
                l_RL_offset = 2;
                if(INSTANCE_RING == l_ringType)
                {
                    l_sectionOffset = i_sectionTOR->TOC_EC_INSTANCE_RING;
                }

                break;

            default:
                SBE_TRACE("Invalid Target/ChipletID - %d", l_chipletID);
                l_rc = fapi2::FAPI2_RC_INVALID_PARAMETER;
                break;

        } // end of switch(l_chipletID)

        if (l_rc)
        {
            break;
        }

        FAPI_INF("l_sectionOffset %08x",l_sectionOffset);
        // Determine the section TOR address for the ring
        uint32_t *l_sectionAddr = reinterpret_cast<uint32_t *>(g_seepromAddr +
                                  i_sectionOffset + l_sectionOffset);

        SBE_TRACE ("l_sectionAddr %08X",(uint32_t)l_sectionAddr);

        uint16_t *l_ringTorAddr = NULL;


        if(INSTANCE_RING == l_ringType)
        {
            if ( l_chipletID >= l_chipletData.iv_base_chiplet_number)
            {
                uint8_t l_chipletOffset =
                    (l_chipletID - l_chipletData.iv_base_chiplet_number);
                if (l_chipLetType == EQ_TYPE)
                {
                    if (fapi2::TARGET_TYPE_EX & (i_target.get().getFapiTargetType()))
                    {
                        uint32_t l_ex_number = i_target.get().getTargetInstance();
                        l_chipletOffset = l_ex_number;
                        SBE_TRACE ("l_chipletID %d l_ex_number %d",l_chipletID,l_ex_number);
                    }
                }
                SBE_TRACE ("l_chipletID %u l_chipletOffset %u",l_chipletID, l_chipletOffset);
                l_ringTorAddr =  reinterpret_cast<uint16_t *>(l_sectionAddr )+ ((l_chipletOffset *
                   (l_chipletData.iv_num_instance_rings *l_cpltRingVariantSz)) + (l_torOffset * l_cpltRingVariantSz));

            }
            else
            {
                l_ringTorAddr = reinterpret_cast<uint16_t *>(l_sectionAddr) +
                    (l_torOffset * l_cpltRingVariantSz);
            }
        }
        else
        {
            // TOR records of Ring TOR are 2 bytes in size.
            l_ringTorAddr = reinterpret_cast<uint16_t *>(l_sectionAddr) +
                    (l_torOffset * l_cpltRingVariantSz);
        }

        // The ring variants in section TOR are expected to be in the sequence -
        // 1. Base
        // 2. Cache-Contained
        // 3. Risk Level


        SBE_TRACE ("ring tor address %08X",(uint32_t)l_ringTorAddr);

        // If there are non-base variants of the ring, we'll have to check
        // attributes to determine the sequence of ring apply.
        if( l_cpltRingVariantSz > 1)
        {
            // Check if this is cache-contained IPL
            uint8_t l_iplPhase;
            FAPI_ATTR_GET(fapi2::ATTR_SYSTEM_IPL_PHASE,
                          fapi2::Target<fapi2::TARGET_TYPE_SYSTEM> (),
                          l_iplPhase);

            // 4 : Use Cache Contained mode _if_ present, else fall back
            // to normal ring
            if((CACHE_CONTAINED_MODE == l_iplPhase)
               && *(l_ringTorAddr + l_CC_offset))
            {
                l_ringTorAddr += l_CC_offset;
            }
            else
            {
                // Check if this is risk-level IPL
                // _if_ present, else fall back to normal ring
                uint8_t l_riskLevel;
                FAPI_ATTR_GET(fapi2::ATTR_RISK_LEVEL,
                              fapi2::Target<fapi2::TARGET_TYPE_SYSTEM> (),
                              l_riskLevel);
                if((RISK_LEVEL_MODE == l_riskLevel) &&
                   *(l_ringTorAddr + l_RL_offset))
                {
                    l_ringTorAddr += l_RL_offset;
                }
            }
        }

        SBE_TRACE("l_ringTorAddr %u",*l_ringTorAddr);
        if(*l_ringTorAddr != 0)
        {
            uint8_t *l_addr = reinterpret_cast<uint8_t *>(l_sectionAddr);
            uint8_t *l_rs4Address = reinterpret_cast<uint8_t *>
                                    (l_addr + *l_ringTorAddr);
            SBE_TRACE("l_rs4Address %08x",(uint32_t)l_rs4Address);
            l_rc = rs4DecompressionSvc(i_target,l_rs4Address,
                                       i_applyOverride,i_ringMode,l_ringType);
            if(l_rc != fapi2::FAPI2_RC_SUCCESS)
            {
                SBE_TRACE("Error from applyRS4_SS");
                break;
            }
        }
        else
        {
            SBE_TRACE("Ring image is not found for this is ringId %u",i_ringID);
        }
    }while(0);

    SBE_TRACE("<< findRS4InImageAndApply Exit for ringId %d",i_ringID);
    return l_rc;
}

OpenPOWER on IntegriCloud