summaryrefslogtreecommitdiffstats
path: root/src/sbefw/core/sbemain.C
blob: 9fbaf6bcdfe906f7b21d97e5acdb74487e0955ee (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/sbefw/core/sbemain.C $                                    */
/*                                                                        */
/* OpenPOWER sbe Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2018                        */
/* [+] 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: ppe/sbe/sbefw/sbemain.C
 *
 * @brief This file does the following
 *        - SBE Application Main entry point
 *        - PK initialization
 *        - Thread initialization
 *        - Semaphore initialization
 *        - IRQ setup
 *        - Scheduling of the threads and
 *        - Starting of the control loop code flow
 *
 */


#include "sbeexeintf.H"
#include "sbetrace.H"
#include "sberegaccess.H"
#include "sbestates.H"
#include "fapi2.H" // For target init
#include "sbeutil.H" // For getting SBE_TO_NEST_FREQ_FACTOR
#include "sbeglobals.H"
#include "p9_misc_scom_addresses.H"

// Max defines for Semaphores
static uint32_t MAX_SEMAPHORE_COUNT = 3;

// As periodic timer expire in 24 hours count should never
// be more than 1.
static uint32_t MAX_PERIODIC_TIMER_SEMAPHORE_COUNT = 1;
extern "C"
{
// These variables are declared in linker script to keep track of
// global constructor pointer functions and sbss section.
extern void (*ctor_start_address)() __attribute__ ((section (".rodata")));
extern void (*ctor_end_address)() __attribute__ ((section (".rodata")));
extern uint64_t _sbss_start __attribute__ ((section (".sbss")));
extern uint64_t _sbss_end __attribute__ ((section (".sbss")));
// This function will be used to do any C++ handling required before doing
// any main job. Call to this function should get generated by
// compiler.
// TODO via RTC 152070
// We are also initialising sbss section to zero this function.
// Though it does not do any harm as of now, it is better  if we use loader
// or linker script to zero init sbss section. This way we will be future
// garded if pk  boot uses some static/global data  initialised to
// false in future.
void __eabi()
{
    do
    {
        SBE_GLOBAL->isHreset = SBE::isHreset();
        if (SBE_GLOBAL->isHreset)
        {
            // skip constructors
            break;
        }
        // Initialise sbss section
        uint64_t *startAddr = &_sbss_start;
        while ( startAddr != &_sbss_end )
        {
            *startAddr = 0;
            startAddr++;
        }
        // Call global constructors
        void(**ctors)() = &ctor_start_address;
        while( ctors != &ctor_end_address)
        {
            (*ctors)();
            ctors++;
        }
    } while (false);
}
} // end extern "C"

////////////////////////////////////////////////////////////////
// @brief  sbeInitSems - Create the necessary semaphores
//
// @return  PK_OK                          - Success
//         PK_INVALID_SEMAPHORE_AT_CREATE - Invalid PkSemaphore
//         PK_INVALID_ARGUMENT_SEMAPHORE  - max_count is non-zero
//                               and less than the initial_count
////////////////////////////////////////////////////////////////
uint32_t sbeInitSems(void)
{
    SBE_ENTER("sbeInitSems");
    int l_rc = PK_OK;

    do
    {
        l_rc = pk_semaphore_create(&SBE_GLOBAL->sbeSemCmdRecv, 0, MAX_SEMAPHORE_COUNT);
        if (l_rc)
        {
            break;
        }
        l_rc = pk_semaphore_create(&SBE_GLOBAL->sbeSemCmdProcess, 0, MAX_SEMAPHORE_COUNT);
        if (l_rc)
        {
            break;
        }
        l_rc = pk_semaphore_create(&SBE_GLOBAL->sbeSemAsyncProcess, 0,
                                         MAX_PERIODIC_TIMER_SEMAPHORE_COUNT);
        if (l_rc)
        {
            break;
        }
        #ifdef SBE_CONSOLE_SUPPORT
        l_rc = pk_semaphore_create(&SBE_GLOBAL->sbeUartBinSem, 1, 1);
        if (l_rc)
        {
            break;
        }
        #endif
    } while (false);

    if (l_rc)
    {
        SBE_ERROR ("pk_semaphore_create, rc=[%d]", l_rc);
    }
    return l_rc;
}

////////////////////////////////////////////////////////////////
// @brief  createAndResumeThreadHelper
//            - Create and resume the given thread
//
// @param[in/out] io_thread  A pointer to an PkThread structure to initialize
// @param[in]     i_thread_routine The subroutine that implements the thread
// @param[in/out] io_arg     Private data to be passed as the argument to the
//                              thread routine when it begins execution
// @param[in]     i_stack    The stack space of the thread
// @param[in]     i_stack_size The size of the stack in bytes
// @param[in]     i_priority The initial priority of the thread
//
// @return        PK_OK Successfully created and resumed the thread
//
// @return        PK_INVALID_THREAD_AT_CREATE io_thread is null
// @return        PK_INVALID_ARGUMENT_THREAD1 i_thread_routine is null
// @return        PK_INVALID_ARGUMENT_THREAD2 i_priority is invalid
// @return        PK_INVALID_ARGUMENT_THREAD3 the stack area wraps around
//                                      the end of memory.
// @return        PK_STACK_OVERFLOW           The stack area at thread creation
//                                      is smaller than the min safe size
// @return        PK_INVALID_THREAD_AT_RESUME1 io_thread is null (unlikely)
// @return        PK_INVALID_THREAD_AT_RESUME2 The thread is not active,
//                                      i.e. has completed or been deleted,
// @return        PK_PRIORITY_IN_USE_AT_RESUME Another thread is already
//                                      mapped at the priority of the thread
////////////////////////////////////////////////////////////////
uint32_t createAndResumeThreadHelper(PkThread    *io_pThread,
                                PkThreadRoutine   i_thread_routine,
                                void             *io_pArg,
                                PkAddress         i_stack,
                                size_t            i_stack_size,
                                sbeThreadPriorities  i_priority)
{
    int l_rc = PK_OK;

    // Thread creation
    l_rc =  pk_thread_create(io_pThread,
                             i_thread_routine,
                             io_pArg,
                             i_stack,
                             i_stack_size,
                             (PkThreadPriority)i_priority);
    if(l_rc == PK_OK)
    {
        // resume the thread once created
        l_rc = pk_thread_resume(io_pThread);
    }

    // Check for errors creating or resuming the thread
    if(l_rc != PK_OK)
    {
        SBE_ERROR ("Failure creating/resuming thread, rc=[%d]", l_rc);
    }

    return l_rc;
}

////////////////////////////////////////////////////////////////
// @brief   sbeInitThreads
//          Create the resume all the firmware threads
//
// @return  See createAndResumeThreadHelper for more details
////////////////////////////////////////////////////////////////
int sbeInitThreads(void)
{
    // Locals
    uint32_t l_rc         = PK_OK;

    do
    {
        // Initialize Command receiver thread
        l_rc = createAndResumeThreadHelper(&SBE_GLOBAL->sbeCommandReceiver_thread,
                            sbeCommandReceiver_routine,
                            (void *)0,
                            (PkAddress)sbeCommandReceiver_stack,
                            SBE_THREAD_CMD_RECV_STACK_SIZE,
                            THREAD_PRIORITY_5);
        if (l_rc)
        {
            break;
        }

        // Initialize Synchronous Command Processor thread
        l_rc = createAndResumeThreadHelper(&SBE_GLOBAL->sbeSyncCommandProcessor_thread,
                            sbeSyncCommandProcessor_routine,
                            (void *)0,
                            (PkAddress)sbeSyncCommandProcessor_stack,
                            SBE_THREAD_SYNC_CMD_PROC_STACK_SIZE,
                            THREAD_PRIORITY_6);
        if (l_rc)
        {
            break;
        }

        // Initialize Asynchronous Command Processor thread
        l_rc = createAndResumeThreadHelper(&SBE_GLOBAL->sbeAsyncCommandProcessor_thread,
                            sbeAsyncCommandProcessor_routine,
                            (void *)0,
                            (PkAddress)sbeAsyncCommandProcessor_stack,
                            SBE_THREAD_ASYNC_CMD_PROC_STACK_SIZE,
                            THREAD_PRIORITY_7);
        if (l_rc)
        {
            break;
        }
    } while (false);

    // If there are any errors initializing the threads
    if( l_rc )
    {
        SBE_ERROR ("Error Initializing a thread, rc=[%d]", l_rc);
    }

    return l_rc;
}

////////////////////////////////////////////////////////////////
// @brief - main : SBE Application main
////////////////////////////////////////////////////////////////
uint32_t main(int argc, char **argv)
{
    #define SBE_FUNC "main "
    SBE_ENTER(SBE_FUNC);
    int l_rc = 0;

    // backup i2c mode register
    uint32_t reg_address = PU_MODE_REGISTER_B;
    PPE_LVD( reg_address, SBE_GLOBAL->i2cModeRegister);

    // @TODO via RTC : 128818
    //       Explore on reclaiming the stack
    //       used by this Initialization code

    do
    {
        // initializes kernel data -
        // stack, threads, timebase, timers, etc.
        l_rc = pk_initialize((PkAddress)sbe_Kernel_NCInt_stack,
             SBE_NONCRITICAL_STACK_SIZE,
             0,
             SBE_GLOBAL->sbefreq );
        if (l_rc)
        {
            break;
        }

        SBE_INFO("Completed PK init");

        // Initialize the semaphores
        l_rc = sbeInitSems();
        if (l_rc)
        {
            break;
        }

        // Initialize SBE control loop threads
        l_rc = sbeInitThreads();
        if (l_rc)
        {
            break;
        }

        // Setup SBE PPE IRQs
        l_rc = sbeIRQSetup();
        if (l_rc)
        {
            break;
        }

        if (!SBE_GLOBAL->isHreset)
        {
            // TODO via RTC 126146.
            //  Check if we should call plat_TargetsInit in some other thread.
            //  We may want to keep only PK init in main and can move
            //  plat init to some other thread. Only if this is required by more
            //  than one thread and there can be some race condition, we will
            //  keep it here before starting other threads.
            fapi2::ReturnCode fapiRc = fapi2::plat_TargetsInit();
            if( fapiRc != fapi2::FAPI2_RC_SUCCESS )
            {
                SBE_ERROR(SBE_FUNC"plat_TargetsInit failed");
                (void)SbeRegAccess::theSbeRegAccess().
                        stateTransition(SBE_FAILURE_EVENT);
                // Hard Reset SBE to recover
                break;
            }

            fapiRc = fapi2::plat_AttrInit();
            if(fapiRc != fapi2::FAPI2_RC_SUCCESS)
            {
                SBE_ERROR(SBE_FUNC"plat_AttrInit failed");
                (void)SbeRegAccess::theSbeRegAccess().
                        stateTransition(SBE_FAILURE_EVENT);
                // Hard Reset SBE to recover
                break;
            }

            if(SbeRegAccess::theSbeRegAccess().init())
            {
                SBE_ERROR(SBE_FUNC"Failed to initialize SbeRegAccess.");
                // init failure could mean the below will fail too, but attempt it
                // anyway
                (void)SbeRegAccess::theSbeRegAccess().stateTransition(
                                                     SBE_FAILURE_EVENT);
                // Hard Reset SBE to recover
                break;
            }
            if(SBE::isSimicsRunning())
            {
               SBE_INFO("SBE is running on simics");
            }
        }

        // Start running the highest priority thread.
        // This function never returns
        pk_start_threads();

    } while (false);

    SBE_EXIT(SBE_FUNC);
    return l_rc;
}
OpenPOWER on IntegriCloud