summaryrefslogtreecommitdiffstats
path: root/src/usr/util/utilsemipersist.C
blob: 28ce9393627a8087fff8064fbf62cd265d19f099 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/util/utilsemipersist.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 utilsemipersist.C
 *
 * @brief   Abstraction of semi volatile pnor partition
 *
 * Used for creating and manipulating streams
 */

/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/
#include <trace/interface.H>
#include <limits.h>
#include <util/util_reasoncodes.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <pnor/pnorif.H>
#include <sys/mm.h>
#include <util/utilsemipersist.H>
#include "utilbase.H"
trace_desc_t* g_trac_persist = nullptr;
TRAC_INIT(&g_trac_persist, "UTIL_PERSIST", KILOBYTE);

using namespace ERRORLOG;

namespace Util
{

    // ----------------------------------------------
    // Globals
    // ----------------------------------------------
    mutex_t g_PersistMutex = MUTEX_INITIALIZER;

    semiPersistData_t * getHbVolatile()
    {
        errlHndl_t l_err = nullptr;
        uint64_t l_vaddr = 0x0;
        static uint64_t g_HbVolatileAddr = 0x0;

        do
        {
            if(g_HbVolatileAddr)
            {
                l_vaddr = g_HbVolatileAddr;
                break;
            }

            PNOR::SectionInfo_t l_pnorHbVolatile;
            l_err = PNOR::getSectionInfo(PNOR::HB_VOLATILE, l_pnorHbVolatile);
            if(l_err)
            {
                delete l_err;
                l_err = nullptr;
                TRACFCOMP( g_trac_persist,
                           INFO_MRK"getHbVolatile(): HB_VOLATILE section not"
                           " found, it is optional");
                break;
            }
            if(l_pnorHbVolatile.size == 0)
            {
                TRACFCOMP( g_trac_persist,
                           INFO_MRK"getHbVolatile(): HB_VOLATILE section is"
                           " empty in PNOR");
                break;
            }

            g_HbVolatileAddr = l_pnorHbVolatile.vaddr;
            l_vaddr = g_HbVolatileAddr;

        }while(0);

        return reinterpret_cast<semiPersistData_t*>(l_vaddr);
    }

    void readSemiPersistData(semiPersistData_t & o_data)
    {
        memset(&o_data, 0x0, sizeof(semiPersistData_t));

        //Lock to prevent concurrent access
        mutex_lock(&g_PersistMutex);

        auto l_data = getHbVolatile();
        if(l_data)
        {
            o_data = *l_data;
        }

        mutex_unlock(&g_PersistMutex);
    }

    void writeSemiPersistData(const semiPersistData_t i_data)
    {
        //Lock to prevent concurrent access
        mutex_lock(&g_PersistMutex);

        auto l_data = getHbVolatile();
        if(l_data)
        {
            *l_data = i_data;
            int l_rc = mm_remove_pages(FLUSH, l_data,
                                       sizeof(semiPersistData_t));
            if (l_rc)
            {
                TRACFCOMP(g_trac_persist,
                          ERR_MRK"writeSemiPersistData(): mm_remove_pages"
                          "(FLUSH,%p,%d) returned %d",l_data,
                          sizeof(semiPersistData_t),l_rc);
            }
        }
        mutex_unlock(&g_PersistMutex);
    }
};
OpenPOWER on IntegriCloud