summaryrefslogtreecommitdiffstats
path: root/src/usr/hwpf/plat/fapiPlatAttrOverrideSync.C
blob: baadaec83ae1f7145c5ca4a552977c23a8c3ec66 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/hwpf/plat/fapiPlatAttrOverrideSync.C $                */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2013              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 *  @file fapiPlatAttrOverrideSync.C
 *
 *  @brief Implements the functions for Attribute Override and Sync
 *
 */

//******************************************************************************
// Includes
//******************************************************************************
#include <limits.h>
#include <sys/msg.h>
#include <string.h>
#include <vector>
#include <sys/msg.h>
#include <util/singleton.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <mbox/mboxif.H>
#include <hwpf/fapi/fapiAttributeTank.H>
#include <hwpf/plat/fapiPlatAttrOverrideSync.H>
#include <hwpf/plat/fapiPlatTrace.H>

namespace fapi
{

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

// Attribute set directly by debug tool in standalone Hostboot mode to apply
// an Attribute Override
fapi::Attribute g_attrOverride;

namespace attrOverrideSync
{

/**
 * @brief Attribute Override/Sync Mailbox Message Type Constants
 *        These must be kept in sync with FSP firmware
 */
enum MAILBOX_MSG_TYPE
{
    MSG_SET_OVERRIDES       = MBOX::FIRST_UNSECURE_MSG + 0x10, // FSP<->Hostboot
    MSG_CLEAR_ALL_OVERRIDES = MBOX::FIRST_UNSECURE_MSG + 0x11, // FSP<->Hostboot
    MSG_SET_SYNC_ATTS       = MBOX::FIRST_UNSECURE_MSG + 0x12, // FSP<--Hostboot
};

//******************************************************************************
// 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()
{
    // Apply the attribute override
    Singleton<fapi::OverrideAttributeTank>::instance().
        setAttribute(g_attrOverride);
}

//******************************************************************************
void monitorForFspMessages()
{
    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);
    
    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_size = l_pMsg->data[1];
            FAPI_INF("monitorForFspMessages: MSG_SET_OVERRIDES (%lld overrides)",
                     l_size / sizeof(fapi::Attribute));
            Attribute * l_pAttribute =
                reinterpret_cast<Attribute *>(l_pMsg->extra_data);
            
            while (l_size >= sizeof(fapi::Attribute))
            {
                Singleton<fapi::OverrideAttributeTank>::instance().
                    setAttribute(*l_pAttribute);
                l_pAttribute++;
                l_size -= sizeof(fapi::Attribute);
            }
            
            // Free the memory
            free(l_pMsg->extra_data);
            l_pMsg->extra_data = NULL;
            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");
            Singleton<fapi::OverrideAttributeTank>::instance().
                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);
        }
    }
}

//******************************************************************************
// Utility function called by sendAttrOverridesAndSyncsToFsp
//******************************************************************************
errlHndl_t sendAttrsToFsp(const MAILBOX_MSG_TYPE i_msgType,
                          std::vector<AttributeChunk> & i_attributes)
{
    errlHndl_t l_pErr = NULL;
    
    // Send Attributes through the mailbox chunk by chunk.
    for (size_t i = 0; i < i_attributes.size(); i++)
    {
        msg_t * l_pMsg = msg_allocate();
        l_pMsg->type = i_msgType;
        l_pMsg->data[0] = 0;
        l_pMsg->data[1] = i_attributes[i].iv_numAttributes * sizeof(Attribute);
        l_pMsg->extra_data = i_attributes[i].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");
            msg_free(l_pMsg);
            break;
        }
        
        // Mailbox freed the chunk data
        i_attributes[i].iv_pAttributes = NULL;
        msg_free(l_pMsg);
    }
    
    // Free any memory (only in error case will there be memory to free) and
    // clear the vector of Attribute Chunks
    for (size_t i = 0; i < i_attributes.size(); i++)
    {
        free(i_attributes[i].iv_pAttributes);
        i_attributes[i].iv_pAttributes = NULL;
    }
    i_attributes.clear();
    
    return l_pErr;
}

//******************************************************************************
void sendAttrOverridesAndSyncsToFsp()
{
    if (MBOX::mailbox_enabled())
    {
        // Clear all current FSP Attribute Overrides
        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
        errlHndl_t l_pErr = MBOX::send(MBOX::FSP_HWPF_ATTR_MSGQ, l_pMsg);
 
        if (l_pErr)
        {
            FAPI_ERR("SendAttrOverridesToFsp: Error clearing FSP 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
            std::vector<AttributeChunk> l_attributes;

            Singleton<fapi::OverrideAttributeTank>::instance().
                getAllAttributes(AttributeTank::ALLOC_TYPE_MALLOC,
                                 l_attributes);

            if (l_attributes.size())
            {
                l_pErr = sendAttrsToFsp(MSG_SET_OVERRIDES, l_attributes);

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

            if (l_pErr == NULL)
            {
                // Send Hostboot Attributes to Sync to the FSP
                std::vector<AttributeChunk> l_attributes;
                
                Singleton<fapi::SyncAttributeTank>::instance().
                    getAllAttributes(AttributeTank::ALLOC_TYPE_MALLOC,
                                     l_attributes);

                if (l_attributes.size())
                {
                    l_pErr = sendAttrsToFsp(MSG_SET_SYNC_ATTS, l_attributes);

                    if (l_pErr)
                    {
                        FAPI_ERR("SendAttrOverridesToFsp: Error sending syncs to FSP");
                        errlCommit(l_pErr, HWPF_COMP_ID);
                    }
                    else
                    {
                        // Clear Hostboot Attributes to Sync
                        Singleton<fapi::SyncAttributeTank>::instance().
                            clearAllAttributes();
                    }
                }
            }
        }
    }
}

//******************************************************************************
AttributeTank & theOverrideAttrTank()
{
    return Singleton<fapi::OverrideAttributeTank>::instance();
}

//******************************************************************************
AttributeTank & theSyncAttrTank()
{
    return Singleton<fapi::SyncAttributeTank>::instance();
}

//******************************************************************************
// This is used as a singleton and contains the lock used to serialize access
// to the OverrideAttributeTank
//******************************************************************************
class OverrideAttributeTankLock
{
public:
    OverrideAttributeTankLock()
    {
        mutex_init(&iv_mutex);
    }

    ~OverrideAttributeTankLock()
    {
        mutex_destroy(&iv_mutex);
    }
    mutex_t iv_mutex;
};

//******************************************************************************
// This is used as a singleton and contains the lock used to serialize access
// to the SyncAttributeTank
//******************************************************************************
class SyncAttributeTankLock
{
public:
    SyncAttributeTankLock()
    {
        mutex_init(&iv_mutex);
    }

    ~SyncAttributeTankLock()
    {
        mutex_destroy(&iv_mutex);
    }
    mutex_t iv_mutex;
};

} // End attrOverrideSync namespace

//******************************************************************************
// This is the Hostboot PLAT implementation of a FAPI function
//******************************************************************************
void OverrideAttributeTank::platLock() const
{
    mutex_lock(&(Singleton<fapi::attrOverrideSync::
        OverrideAttributeTankLock>::instance().iv_mutex));
}

//******************************************************************************
// This is the Hostboot PLAT implementation of a FAPI function
//******************************************************************************
void OverrideAttributeTank::platUnlock() const
{
    mutex_unlock(&(Singleton<fapi::attrOverrideSync::
        OverrideAttributeTankLock>::instance().iv_mutex));
}

//******************************************************************************
// This is the Hostboot PLAT implementation of a FAPI function
//******************************************************************************
void SyncAttributeTank::platLock() const
{
    mutex_lock(&(Singleton<fapi::attrOverrideSync::
        SyncAttributeTankLock>::instance().iv_mutex));
}

//******************************************************************************
// This is the Hostboot PLAT implementation of a FAPI function
//******************************************************************************
void SyncAttributeTank::platUnlock() const
{
    mutex_unlock(&(Singleton<fapi::attrOverrideSync::
        SyncAttributeTankLock>::instance().iv_mutex));
}

//******************************************************************************
// This is the Hostboot PLAT implementation of a FAPI function
//******************************************************************************
bool SyncAttributeTank::platSyncEnabled()
{
    // TODO, RTC 42642. Check for CronusMode, probably using a FAPI Attribute
    // but TBD. If CronusMode is not enabled then there should not be the
    // performance hit of adding written attributes to the SyncAttributeTank
    return false;
}

} // End fapi namespace
OpenPOWER on IntegriCloud