summaryrefslogtreecommitdiffstats
path: root/src/usr/sbeio/sbe_memRegionMgr.C
blob: eb0bef9cd7bf9793a02792878617a69d4c63f1cb (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/sbeio/sbe_memRegionMgr.C $                            */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 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 sbe_memRegionMgr.C
* @brief Opens and Closes Unsecure Memory Regions via the SBE
*/

#include <config.h>
#include <trace/interface.H>
#include <errl/errlmanager.H>
#include <sbeio/sbeioif.H>
#include <sbeio/sbe_psudd.H>
#include <sbeio/sbeioreasoncodes.H>
#include <targeting/common/targetservice.H>
#include <vmmconst.h>
#include <sys/misc.h>
#include <secureboot/service.H>

#include "sbe_memRegionMgr.H"

extern trace_desc_t* g_trac_sbeio;

#define SBE_TRACD(printf_string,args...) \
    TRACDCOMP(g_trac_sbeio,"memRegion: " printf_string,##args)
#define SBE_TRACF(printf_string,args...) \
    TRACFCOMP(g_trac_sbeio,"memRegion: " printf_string,##args)

namespace SBEIO
{

// External Function to Open an Unsecure Memory Region
// - see sbeioif.H for details
errlHndl_t openUnsecureMemRegion(
    const uint64_t i_start_addr,
    const uint32_t i_size,
    const bool     i_isWritable,
    TARGETING::Target* i_target)
{
    return Singleton<MemRegionMgr>::instance()
               .openUnsecureMemRegion(i_start_addr,
                                      i_size,
                                      i_isWritable,
                                      i_target);
};


// External Function to Close an Unsecure Memory Region
// - see sbeioif.H for details
errlHndl_t closeUnsecureMemRegion(
    const uint64_t i_start_addr,
    TARGETING::Target* i_target)
{
    return Singleton<MemRegionMgr>::instance()
               .closeUnsecureMemRegion(i_start_addr, i_target);
};


// External Function to Close All Unsecure Memory Regions
// - see sbeioif.H for details
errlHndl_t closeAllUnsecureMemRegions()
{
    return Singleton<MemRegionMgr>::instance()
               .closeAllUnsecureMemRegions();
};


/*****************************************************************************/
/*
 * MemRegionMgr : Constructor : Setup Instance Variables
 */
MemRegionMgr::MemRegionMgr()
{
    SBE_TRACD(ENTER_MRK"MemRegionMgr Constructor");

    // SBE opens Memory Region for HB DUMP before starting hostboot
    // so just add it directly to iv_memRegions
    regionData hb_dump_region;
    hb_dump_region.start_addr = cpu_spr_value(CPU_SPR_HRMOR);
    hb_dump_region.size = VMM_MEMORY_SIZE;
    hb_dump_region.flags = SbePsu::SBE_MEM_REGION_OPEN_READ_ONLY;
    iv_memRegions.push_back(hb_dump_region);

    SBE_TRACD("MemRegionMgr Constructor: Initial region(s):");
    printIvMemRegions();

    SBE_TRACD(EXIT_MRK"MemRegionMgr Constructor");
};

/*
 * MemRegionMgr : Destructor : Clear Instance Variables
 */
MemRegionMgr::~MemRegionMgr()
{
    // Clear Vector of Unsecure Memory Regions
    iv_memRegions.clear();
};

/**
 * MemRegionMgr::openUnsecureMemRegion
 * - see sbe_unsecureMemRegionMgr.H for details
 */
errlHndl_t MemRegionMgr::openUnsecureMemRegion(
    const uint64_t    i_start_addr,
    const uint32_t    i_size,
    const bool        i_isWritable,
    TARGETING::Target* i_target)
{

    errlHndl_t errl = nullptr;
    regionData l_region;
    uint8_t region_count = iv_memRegions.size();
    bool itr_region_closed = false;
    const uint8_t input_flags = i_isWritable ?
                                  SbePsu::SBE_MEM_REGION_OPEN_READ_WRITE :
                                  SbePsu::SBE_MEM_REGION_OPEN_READ_ONLY;

    SBE_TRACF(ENTER_MRK"openUnsecureMemRegion: i_tgt=0x%X: "
              "i_start_addr=0x%.16llX, i_size=0x%.8X, i_isWritable=%d "
              "(flags=0x%.2X), count=%d",
              TARGETING::get_huid(i_target), i_start_addr, i_size,
              i_isWritable, input_flags, region_count);
    assert(i_size!=0, "openUnsecureMemRegion: i_size=0");

    do
    {
        // Handle 'Open' Request
        // -- Look for possible overlap with existing regions
        // ---- If overlap, close existing regions and re-open
        //      all regions as necessary
        // ---- If no overlap, open requested region
        // Throughout check that no more than 8 memory regions are opened

        auto itr = iv_memRegions.begin();
        while (itr != iv_memRegions.end())
        {
            // reset for each loop
            itr_region_closed = false;

            // Look for Overlap: start_addr starts at or is inside
            // existing region
            if ((i_start_addr >= itr->start_addr) &&
                (i_start_addr < (itr->start_addr + itr->size)))
            {
                SBE_TRACF(INFO_MRK"openUnsecureMemRegion: Overlap Found: "
                  "Req Region Starts inside existing region. "
                  "Exist start=0x%.16lX, size=0x%.8X, flags=0x%.2X"
                  "Req start=0x%.16lX, size=0x%.8X, flags=0x%.2X",
                  itr->start_addr, itr->size, itr->flags,
                  i_start_addr, i_size, input_flags);

                // Close Existing Region
                l_region.start_addr = itr->start_addr;
                l_region.size = itr->size;
                l_region.flags = SbePsu::SBE_MEM_REGION_CLOSE;
                l_region.tgt = itr->tgt;

                errl = doUnsecureMemRegionOp(l_region);

                if (errl)
                {
                    SBE_TRACF(ERR_MRK "openUnsecureMemRegion: Close Op "
                              "Failed: err rc=0x%.4X plid=0x%.8X",
                              ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));

                    // Return error to caller ASAP
                    break;
                }
                else
                {
                    // Update count
                    --region_count;

                    // This region will be removed from the cache list later
                    itr_region_closed = true;
                }

                // Re-Open Non-overlapping initial part of Existing Region
                // NOTE: skipping max count check since we already closed a
                //       region above
                l_region.size = i_start_addr - itr->start_addr;
                l_region.flags = itr->flags;
                l_region.tgt = itr->tgt;

                if (l_region.size > 0)
                {
                    errl = doUnsecureMemRegionOp(l_region);

                    if (errl)
                    {
                        SBE_TRACF(ERR_MRK "openUnsecureMemRegion: Re-Open Op "
                                  "Failed: err rc=0x%.4X plid=0x%.8X",
                                  ERRL_GETRC_SAFE(errl),
                                  ERRL_GETPLID_SAFE(errl));

                        // Return error to caller ASAP
                        break;
                    }
                    else
                    {
                        // Update count
                        ++region_count;

                        // Add the region to the cache list
                        iv_memRegions.push_front(l_region);
                    }
                }
            }

            // Look for Overlap: end of region inside existing region
            if (((i_start_addr + i_size) > itr->start_addr) &&
                ((i_start_addr + i_size) < (itr->start_addr + itr->size)))
            {
                SBE_TRACF(INFO_MRK"openUnsecureMemRegion: Overlap Found: "
                  "Req Region Ends inside existing region. "
                  "Exist start=0x%.16lX, size=0x%.8X, flags=0x%.2X"
                  "Req start=0x%.16lX, size=0x%.8X, flags=0x%.2X",
                  itr->start_addr, itr->size, itr->flags,
                  i_start_addr, i_size, input_flags);

                // Close Existing Region - if not already closed
                if (itr_region_closed == false)
                {
                    l_region.start_addr = itr->start_addr;
                    l_region.size = itr->size;
                    l_region.flags = SbePsu::SBE_MEM_REGION_CLOSE;
                    l_region.tgt = itr->tgt;

                    errl = doUnsecureMemRegionOp(l_region);

                    if (errl)
                    {
                        SBE_TRACF(ERR_MRK "openUnsecureMemRegion: Close Op "
                                  "Failed: err rc=0x%.4X plid=0x%.8X",
                                  ERRL_GETRC_SAFE(errl),
                                  ERRL_GETPLID_SAFE(errl));

                        // Return error to caller ASAP
                        break;
                    }
                    else
                    {
                        // This region will be removed from the cache list later
                        itr_region_closed = true;
                    }
                }

                // Re-open, non-overlapping back end part of
                // Existing Region, but first check that we're not exceeding
                // the maximum allowed number of regions
                errl = checkNumberOfMemRegions(region_count);
                if (errl)
                {
                    break;
                }

                l_region.start_addr = i_start_addr + i_size;
                l_region.size = (itr->start_addr + itr->size) -
                                  (i_start_addr + i_size);
                l_region.flags = itr->flags;
                l_region.tgt = itr->tgt;

                errl = doUnsecureMemRegionOp(l_region);

                if (errl)
                {
                    SBE_TRACF(ERR_MRK "openUnsecureMemRegion: Re-Open Op "
                              "Failed: err rc=0x%.4X plid=0x%.8X",
                              ERRL_GETRC_SAFE(errl),
                              ERRL_GETPLID_SAFE(errl));

                    // Return error to caller ASAP
                    break;
                }
                else
                {
                    // Update count
                    ++region_count;

                    // Add the region to the cache list
                    iv_memRegions.push_front(l_region);
                }
            }

            // Look for Overlap: existing region enclosed by requested region
            else if ((i_start_addr <= itr->start_addr) &&
                     ((i_start_addr + i_size) >=
                        (itr->start_addr + itr->size)))
            {
                SBE_TRACF(INFO_MRK"openUnsecureMemRegion: Overlap Found: "
                  "Req Region encloses existing region. "
                  "Exist start=0x%.16lX, size=0x%.8X, flags=0x%.2X"
                  "Req start=0x%.16lX, size=0x%.8X, flags=0x%.2X",
                  itr->start_addr, itr->size, itr->flags,
                  i_start_addr, i_size, input_flags);

                // Close Existing Region
                if (itr_region_closed==false)
                {
                    l_region.start_addr = itr->start_addr;
                    l_region.size = itr->size;
                    l_region.flags = SbePsu::SBE_MEM_REGION_CLOSE;
                    l_region.tgt = itr->tgt;

                    errl = doUnsecureMemRegionOp(l_region);

                    if (errl)
                    {
                        SBE_TRACF(ERR_MRK "openUnsecureMemRegion: Close Op "
                                  "(enclosed) Failed: err rc=0x%.4X plid=0x%.8X",
                                  ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));

                        // Return error to caller ASAP
                        break;
                    }
                    else
                    {
                        // Update count
                        --region_count;

                        // This region will be removed from the cache list later
                        itr_region_closed = true;
                    }
                }
            }

            // Increment Iterator
            if (itr_region_closed == true)
            {
                itr = iv_memRegions.erase(itr);
            }
            else
            {
                ++itr;
            }
        }

        // If previous error, don't open a new region
        if (errl)
        {
            break;
        }

        // Open Requested Region, but first check that we're not exceeding
        // the maximum allowed number of regions
        errl = checkNumberOfMemRegions(region_count);
        if (errl)
        {
            break;
        }

        l_region.start_addr = i_start_addr;
        l_region.size = i_size;
        l_region.flags = input_flags;
        l_region.tgt = i_target;

        errl = doUnsecureMemRegionOp(l_region);
        if (errl)
        {
            SBE_TRACF(ERR_MRK "openUnsecureMemRegion: Open Call Failed: "
                      "err rc=0x%.4X plid=0x%.8X",
                      ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));

            // Return error to caller ASAP
            break;
        }
        else
        {
            // Add region to cache on success
            iv_memRegions.push_front(l_region);
        }

    }
    while (0);

    printIvMemRegions();

    SBE_TRACF(EXIT_MRK "openUnsecureMemRegion: i_tgt=0x%X: "
              "i_start_addr=0x%.16llX: err_rc=0x%.4X",
              TARGETING::get_huid(i_target), i_start_addr,
              ERRL_GETRC_SAFE(errl));

    if(errl)
    {
        errl->collectTrace(SBEIO_COMP_NAME);

        // If error log is informational, commit it here rather
        // than passing back an error and risk stopping the IPL
        if (errl->sev() == ERRORLOG::ERRL_SEV_INFORMATIONAL)
        {
            errlCommit(errl, SBEIO_COMP_ID);
        }
    }

    return errl;
}

/**
 * MemRegionMgr::closeUnsecureMemRegion
 * - see sbe_unsecureMemRegionMgr.H for details
 */
errlHndl_t MemRegionMgr::closeUnsecureMemRegion(
    const uint64_t    i_start_addr,
    TARGETING::Target* i_target)
{
    errlHndl_t errl = nullptr;

    bool region_found = false;
    regionData l_region;

    SBE_TRACF(ENTER_MRK"closeUnsecureMemRegion: i_tgt=0x%X: "
              "i_start_addr=0x%.16llX",
              TARGETING::get_huid(i_target), i_start_addr);

    do
    {
        auto itr = iv_memRegions.begin();
        while (itr != iv_memRegions.end())
        {
            if (itr->start_addr == i_start_addr)
            {
                region_found = true;

                // Close Requested Region
                l_region.start_addr = itr->start_addr;
                l_region.size = itr->size;
                l_region.flags = SbePsu::SBE_MEM_REGION_CLOSE;
                l_region.tgt = itr->tgt;

                errl = doUnsecureMemRegionOp(l_region);
                if (errl)
                {
                    SBE_TRACF(ERR_MRK "closeUnsecureMemRegion: Close Op Failed:"
                              " err rc=0x%.4X plid=0x%.8X",
                              ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));
                }
                else
                {
                    itr = iv_memRegions.erase(itr);
                }

                // Quit walking through regions
                break;
            }

            // Keep looking
            ++itr;
        }

        // If previous error, don't even check for "region not found" error
        if (errl)
        {
            break;
        }

        if (region_found == false)
        {
            SBE_TRACF(ERR_MRK"closeUnsecureMemRegion: Requested Region "
                      "Does Not Exist! start_addr=0x%.16llX",
                      i_start_addr);

            /*@
             * @errortype
             * @moduleid     SBEIO_MEM_REGION
             * @reasoncode   SBEIO_MEM_REGION_DOES_NOT_EXIST
             * @userdata1    Starting Address of Unsecure Memory Region
             * @userdata2    Number of Unsecure Memory Regions
             * @devdesc      Unsecure Memory Region Does Not Exist
             * @custdesc     A problem occurred during the IPL
             */
            errl = new ERRORLOG::ErrlEntry(
                         ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                         SBEIO_MEM_REGION,
                         SBEIO_MEM_REGION_DOES_NOT_EXIST,
                         i_start_addr,
                         iv_memRegions.size(),
                         true /*Add HB SW Callout*/ );

            errl->collectTrace(SBEIO_COMP_NAME);

            break;
        }

    }
    while (0);

    SBE_TRACF(EXIT_MRK "closeUnsecureMemRegion: i_tgt: 0x%X: "
              "i_start_addr0x%.16llX: err_rc=0x%.4X",
              TARGETING::get_huid(i_target), i_start_addr,
              ERRL_GETRC_SAFE(errl));

    if(errl)
    {
        errl->collectTrace(SBEIO_COMP_NAME);

        // If error log is informational, commit it here rather
        // than passing back an error and risk stopping the IPL
        if (errl->sev() == ERRORLOG::ERRL_SEV_INFORMATIONAL)
        {
            errlCommit(errl, SBEIO_COMP_ID);
        }
    }

    return errl;
}

/**
 * MemRegionMgr::closeUnsecureMemRegions
 * - see sbe_unsecureMemRegionMgr.H for details
 */
errlHndl_t MemRegionMgr::closeAllUnsecureMemRegions()
{
    errlHndl_t errl = nullptr;
    errlHndl_t errl_orig = nullptr;

    regionData l_region;

    SBE_TRACF(ENTER_MRK"closeAllUnsecureMemRegions: closing %d region(s)",
              iv_memRegions.size());

    printIvMemRegions();

    do
    {
        // Close every memory region saved in vector
        auto itr = iv_memRegions.begin();
        while (itr != iv_memRegions.end())
        {
            // Close Requested Region
            l_region.start_addr = itr->start_addr;
            l_region.size = itr->size;
            l_region.flags = SbePsu::SBE_MEM_REGION_CLOSE;

            errl = doUnsecureMemRegionOp(l_region);

            if ( ( errl != nullptr) &&
                 ( errl_orig != nullptr ) )
            {
                SBE_TRACF(ERR_MRK "closeAllUnsecureMemRegions: Another Fail: "
                          "Committing new err rc=0x%.4X plid=0x%.8X with plid "
                          "of original err rc=0x%.4X plid=0x%.8X",
                          ERRL_GETRC_SAFE(errl),
                          ERRL_GETPLID_SAFE(errl),
                          ERRL_GETRC_SAFE(errl_orig),
                          ERRL_GETPLID_SAFE(errl_orig));

                // commit new error with orignal err PLID
                errl->plid(errl_orig->plid());
                errl->collectTrace(SBEIO_COMP_NAME);
                errlCommit(errl, SBEIO_COMP_ID);

                // Skip region since it failed to close
                ++itr;
            }
            else if ( errl != nullptr)
            {
                SBE_TRACF(ERR_MRK "closeAllUnsecureMemRegions: Close Fail: "
                          "err rc=0x%.4X plid=0x%.8X",
                          ERRL_GETRC_SAFE(errl),
                          ERRL_GETPLID_SAFE(errl));

                // Save error and continue
                errl_orig = errl;
                errl = nullptr;

                // Skip region since it failed to close
                ++itr;
            }
            else
            {
                itr = iv_memRegions.erase(itr);
            }
        }
    }
    while (0);

    printIvMemRegions();

    SBE_TRACF(EXIT_MRK "closeAllUnsecureMemRegions: regions left = %d, "
              "err_rc=0x%.4X",
              iv_memRegions.size(),
              ERRL_GETRC_SAFE(errl_orig));
    if(errl_orig)
    {
        errl_orig->collectTrace(SBEIO_COMP_NAME);

        // If error log is informational, commit it here rather
        // than passing back an error and risk stopping the IPL
        if (errl_orig->sev() == ERRORLOG::ERRL_SEV_INFORMATIONAL)
        {
            errlCommit(errl_orig, SBEIO_COMP_ID);
        }
    }

    return errl_orig;
}

/**
 * MemRegionMgr::doUnsecureMemRegionOp
 * - see sbe_unsecureMemRegionMgr.H for details
 */
errlHndl_t MemRegionMgr::doUnsecureMemRegionOp(regionData & i_region)
{
    errlHndl_t errl = nullptr;
    TARGETING::Target * l_tgt = i_region.tgt;

    SBE_TRACD(ENTER_MRK"doUnsecureMemRegionOp: tgt=0x%.8X: "
              "start_addr=0x%.16llX, size=0x%.8X, controlFlags=0x%.2X",
              TARGETING::get_huid(l_tgt), i_region.start_addr,
              i_region.size, i_region.flags);

    do
    {
        // Find master proc for target of PSU command, if necessary
        if (l_tgt == nullptr)
        {
            TARGETING::TargetService& tS = TARGETING::targetService();

            errl = tS.queryMasterProcChipTargetHandle(l_tgt);
            if (errl)
            {
                SBE_TRACF(ERR_MRK "doUnsecureMemRegionOp: Failed to get Master "
                          "Proc: err rc=0x%.4X plid=0x%.8X",
                          ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl));

                break;
            }

            // Set target for future operations
            i_region.tgt = l_tgt;
        }

        SbePsu::psuCommand l_psuCommand(
            i_region.flags |
              SbePsu::SBE_MEM_REGION_RESPONSE_REQUIRED, //control flags
            SbePsu::SBE_PSU_CLASS_SECURITY_CONTROL, //command class
            SbePsu::SBE_PSU_SET_UNSECURE_MEMORY_REGION_CMD); //command

        SbePsu::psuResponse  l_psuResponse;

        // set up PSU command message
        l_psuCommand.cd6_memRegion_Size = i_region.size;
        l_psuCommand.cd6_memRegion_Start_Addr = i_region.start_addr;

        errl =  SBEIO::SbePsu::getTheInstance().performPsuChipOp(l_tgt,
                                &l_psuCommand,
                                &l_psuResponse,
                                SbePsu::MAX_PSU_SHORT_TIMEOUT_NS,
                                SbePsu::SBE_MEM_REGION_REQ_USED_REGS,
                                SbePsu::SBE_MEM_REGION_RSP_USED_REGS);

        if (errl)
        {
            SBE_TRACF(ERR_MRK "doUnsecureMemRegionOp: PSU Cmd Failed: "
                      "err rc=0x%.4X plid=0x%.8X (mbxReg0=0x%.16llX)",
                      ERRL_GETRC_SAFE(errl), ERRL_GETPLID_SAFE(errl),
                      l_psuCommand.mbxReg0);

            // If Secureboot is not enabled, make the error from the SBE not
            // supporting Security Control Messages Control command class (which
            // includes the Unsecure Memory Region commands) Informational
            if ((!SECUREBOOT::enabled()) &&
                (l_psuResponse.primaryStatus ==
                   SBE_PRI_INVALID_COMMAND) &&
                (l_psuResponse.secondaryStatus ==
                   SBE_SEC_COMMAND_CLASS_NOT_SUPPORTED)
               )
            {
                SBE_TRACF(ERR_MRK "doUnsecureMemRegionOp: Secureboot NOT "
                          "Enabled - Changing 'Command Class Not Supported' "
                          "Error Log To Informational.");
                errl->setSev(ERRORLOG::ERRL_SEV_INFORMATIONAL);
                errl->collectTrace(SBEIO_COMP_NAME);
            }

            break;
        }
    }
    while (0);

    SBE_TRACD(EXIT_MRK "doUnsecureMemRegionOp: tgt=0x%.8X: "
              "start_addr=0x%.16llX, size=0x%.8X, controlFlags=0x%.2X: "
              "err_rc=0x%.4X",
              TARGETING::get_huid(l_tgt), i_region.start_addr, i_region.size,
              i_region.flags, ERRL_GETRC_SAFE(errl));

    return errl;
}

/**
 * MemRegionMgr::checkNumberOfMemRegions
 * - see sbe_unsecureMemRegionMgr.H for details
 */
errlHndl_t MemRegionMgr::checkNumberOfMemRegions(uint8_t i_count) const
{
    errlHndl_t errl = nullptr;

    if (i_count >= SBEIO_MAX_UNSECURE_MEMORY_REGIONS)
    {
        SBE_TRACF(ERR_MRK"checkNumberOfMemRegions: Current count=%d, max=%d. "
                  "New Region Cannot Be Opened",
                  i_count, SBEIO_MAX_UNSECURE_MEMORY_REGIONS);

        /*@
         * @errortype
         * @moduleid     SBEIO_MEM_REGION
         * @reasoncode   SBEIO_EXCEEDS_MAXIMUM_MEM_REGIONS
         * @userdata1    Current Count of Unsecure Memory Regions
         * @userdata2    Maximum Number of Unsecure Memomory Regions
         * @devdesc      Attempt To Open Too Many Unsecure Memory Regions
         * @custdesc     A problem occurred during the IPL
         */
        errl = new ERRORLOG::ErrlEntry(
                     ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                     SBEIO_MEM_REGION,
                     SBEIO_EXCEEDS_MAXIMUM_MEM_REGIONS,
                     i_count,
                     SBEIO_MAX_UNSECURE_MEMORY_REGIONS,
                     true /*Add HB SW Callout*/ );

        errl->collectTrace(SBEIO_COMP_NAME);
    }

    return errl;
}

/**
 * MemRegionMgr::printIvMemRegions
 * - see sbe_unsecureMemRegionMgr.H for details
 */
void MemRegionMgr::printIvMemRegions(void) const
{
    const uint8_t size = iv_memRegions.size();

    if ( size == 0 )
    {
        SBE_TRACD("MemRegionMgr::printIvMemRegions: number of entries=%d",
                  iv_memRegions.size());
    }
    else
    {
        uint8_t count = 1;
        for ( const auto& itr : iv_memRegions )
        {
            SBE_TRACD("printIvMemRegions: %d/%d: tgt=0x%.8X: "
                      "start_addr=0x%.16llX, size=0x%.8X, flags=0x%.2X (%s)",
                      count, size, TARGETING::get_huid(itr.tgt),
                      itr.start_addr, itr.size, itr.flags,
                      itr.flags == SbePsu::SBE_MEM_REGION_OPEN_READ_ONLY ?
                        "Read-Only" : "Read-Write");
            ++count;
        }
    }
}

} // end namespace SBEIO
OpenPOWER on IntegriCloud