summaryrefslogtreecommitdiff
path: root/drivers/staging/nmf-cm/cm/engine/executive_engine_mgt/src/executive_engine_mgt.c
blob: a00ca4c46836fda663a24bcebf3d4ffeb40d0784 (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
/*
 * Copyright (C) ST-Ericsson SA 2010
 * Author: Jean-Philippe FASSINO <jean-philippe.fassino@stericsson.com> for ST-Ericsson.
 * License terms: GNU General Public License (GPL) version 2.
 */
/*----------------------------------------------------------------------------*
 * This module provides functions that allow to manage DSPs' Firmwares.       *
 ******************************************************************************/


/******************************************************************* Includes
 ****************************************************************************/

#include "../inc/executive_engine_mgt.h"
#include <cm/engine/dsp/inc/dsp.h>
#include <cm/engine/component/inc/introspection.h>
#include <cm/engine/trace/inc/trace.h>
#include <cm/engine/memory/inc/domain.h>
#include <cm/engine/utils/inc/convert.h>
#include <cm/engine/component/inc/initializer.h>
#include <cm/engine/power_mgt/inc/power.h>
#include <cm/engine/perfmeter/inc/mpcload.h>

#include <cm/engine/trace/inc/xtitrace.h>

#include <share/communication/inc/nmf_service.h>

t_ee_state eeState[NB_CORE_IDS];

/****************************************************************** Functions
 ****************************************************************************/
static t_cm_error cm_EEM_allocPanicArea(t_nmf_core_id coreId, t_cm_domain_id domainId);
static void cm_EEM_freePanicArea(t_nmf_core_id coreId);

PUBLIC EXPORT_SHARED t_cm_error CM_ENGINE_GetExecutiveEngineHandle(
        t_cm_domain_id domainId,
        t_cm_instance_handle *executiveEngineHandle)
{
    t_nmf_core_id coreId;

    if (cm_DM_CheckDomain(domainId, DOMAIN_NORMAL) != CM_OK) {
        return CM_INVALID_DOMAIN_HANDLE;
    }

    coreId = cm_DM_GetDomainCoreId(domainId);
    //in case someone ask for ee on component manager !!!!
    if (coreId == ARM_CORE_ID) {*executiveEngineHandle = 0;}
    else {*executiveEngineHandle = eeState[coreId].instance->instance;}

    return CM_OK;
}

PUBLIC t_cm_error cm_EEM_Init(
    t_nmf_core_id coreId,
    const char *eeName,
    t_nmf_executive_engine_id executiveEngineId)
{
    t_rep_component *pRepComponent;
    t_cm_error error;
    t_uint32 i;

    eeState[coreId].instance = (t_component_instance *)0;
    eeState[coreId].executiveEngineId = executiveEngineId;
    for(i = NMF_SCHED_BACKGROUND; i < NMF_SCHED_URGENT + 1;i++)
    {
        eeState[coreId].currentStackSize[i] = MIN_STACK_SIZE;
    }

    // Try to load component file
    if((error = cm_REP_lookupComponent(eeName, &pRepComponent)) != CM_OK)
    {
        if (error == CM_COMPONENT_NOT_FOUND)
            ERROR("CM_COMPONENT_NOT_FOUND: Execution Engine %s\n", eeName, 0, 0, 0, 0, 0);
        return error;
    }

    // Set to 1 during bootstrap since MMDSP forceWakeup is to one also in order to not go in idle state
    // while configuration not finish !!!
    eeState[coreId].nbOfForceWakeup = 1;

    if ((error = cm_DSP_Boot(coreId)) != CM_OK)
        return error;

    if((error = cm_instantiateComponent(
            eeName,
            cm_DSP_GetState(coreId)->domainEE,
            NMF_SCHED_URGENT,
            eeName,
            pRepComponent->elfhandle,
            &eeState[coreId].instance)) != CM_OK)
    {
        cm_DSP_Shutdown(coreId);
        return error;
    }

    /* Get Void Function */
    eeState[coreId].voidAddr = cm_getFunction(eeState[coreId].instance, "helper", "Void");

    /* allocate xram space for stack */
    if (executiveEngineId == SYNCHRONOUS_EXECUTIVE_ENGINE)
    {
        error = cm_DSP_setStackSize(coreId, MIN_STACK_SIZE);
    }
    else
    {
        error = cm_DSP_setStackSize(coreId, (NMF_SCHED_URGENT + 1) * MIN_STACK_SIZE);
    }
    if (error != CM_OK)
    {
        cm_delayedDestroyComponent(eeState[coreId].instance);
        eeState[coreId].instance = (t_component_instance *)0;
        cm_DSP_Shutdown(coreId);
        return error;
    }

    /* allocate sdram memory for panic area */
    error = cm_EEM_allocPanicArea(coreId, cm_DSP_GetState(coreId)->domainEE);
    if (error != CM_OK) {
        cm_delayedDestroyComponent(eeState[coreId].instance);
        eeState[coreId].instance = (t_component_instance *)0;
        cm_DSP_Shutdown(coreId);
        return error;
    }

    /* allocate sdram memory to share perfmeters data */
    error = cm_PFM_allocatePerfmeterDataMemory(coreId, cm_DSP_GetState(coreId)->domainEE);
    if (error != CM_OK) {
        cm_EEM_freePanicArea(coreId);
        cm_delayedDestroyComponent(eeState[coreId].instance);
        eeState[coreId].instance = (t_component_instance *)0;
        cm_DSP_Shutdown(coreId);
        return error;
    }

    if((error = cm_SRV_allocateTraceBufferMemory(coreId, cm_DSP_GetState(coreId)->domainEE)) != CM_OK)
    {
        cm_PFM_deallocatePerfmeterDataMemory(coreId);
        cm_EEM_freePanicArea(coreId);
        cm_delayedDestroyComponent(eeState[coreId].instance);
        eeState[coreId].instance = (t_component_instance *)0;
        cm_DSP_Shutdown(coreId);
        return error;
    }

    /* set initial stack value */
    cm_writeAttribute(eeState[coreId].instance, "rtos/scheduler/topOfStack", cm_DSP_getStackAddr(coreId));

    /* set myCoreId for trace */
    cm_writeAttribute(eeState[coreId].instance, "xti/myCoreId", coreId - 1);
    
#if defined(__STN_8500) && (__STN_8500 > 10)
    /* set myCoreId for prcmu if exist */
    cm_writeAttribute(eeState[coreId].instance, "sleep/prcmu/myCoreId", coreId + 1);
#endif

    /* go go go ... */
    cm_DSP_Start(coreId);
    
    /* Waiting for End Of Boot */
    //TODO : remove infinite while loop
    //TODO : to be paranoiac, add a read to serviceReasonOffset before starting core and check value is MPC_SERVICE_BOOT as it should be
    {
        while(cm_readAttributeNoError(eeState[coreId].instance, "rtos/commonpart/serviceReason") == MPC_SERVICE_BOOT)
        {
            volatile t_uint32 i;
            for (i=0; i < 1000; i++);
        }
    }

    /* set some attributes after boot to avoid being erase by mmdsp boot */
    cm_writeAttribute(eeState[coreId].instance, "xti/traceActive", eeState[coreId].traceState);
    cm_writeAttribute(eeState[coreId].instance, "rtos/commonpart/printLevel", eeState[coreId].printLevel);

    cm_DSP_ConfigureAfterBoot(coreId);

    return CM_OK;
}

/****************************************************************************/
/* NAME:        cm_EEM_Close                                                */
/*--------------------------------------------------------------------------*/
/* DESCRIPTION: Inform us that ee for coreId has been destroyed             */
/*                                                                          */
/* PARAMETERS:  id: dsp identifier                                          */
/*                                                                          */
/* RETURN:      none                                                        */
/*                                                                          */
/****************************************************************************/
PUBLIC void cm_EEM_Close(t_nmf_core_id coreId)
{
    cm_DSP_setStackSize(coreId, 0);
    cm_delayedDestroyComponent(eeState[coreId].instance);
    eeState[coreId].instance = (t_component_instance *)0;
    cm_SRV_deallocateTraceBufferMemory(coreId);
    cm_PFM_deallocatePerfmeterDataMemory(coreId);
    cm_EEM_freePanicArea(coreId);
    cm_DSP_Shutdown(coreId);
}

/****************************************************************************/
/* NAME: cm_EEM_isStackUpdateNeed(                                          */
/*                                t_nmf_core_id id,                         */
/*                                t_nmf_ee_priority priority,               */
/*                                t_uint32 isInstantiate,                   */
/*                                t_uint32 needMinStackSize                 */
/*                               )                                          */
/*--------------------------------------------------------------------------*/
/* DESCRIPTION: Return a boolean to inform if a ee stack size update is need*/
/*              when instantiate or destroying a component                  */
/****************************************************************************/
PUBLIC t_uint32 cm_EEM_isStackUpdateNeed(
    t_nmf_core_id coreId,
    t_nmf_ee_priority priority,
    t_uint32 isInstantiate,
    t_uint32 needMinStackSize)
{
    /* in case of SYNCHRONOUS_EXECUTIVE_ENGINE we only use currentStackSize[NMF_SCHED_BACKGROUND] */
    if (eeState[coreId].executiveEngineId == SYNCHRONOUS_EXECUTIVE_ENGINE) {priority = NMF_SCHED_BACKGROUND;}
    if (isInstantiate)
    {
        if (needMinStackSize > eeState[coreId].currentStackSize[priority]) {return TRUE;}
    }
    else
    {
        if (needMinStackSize == eeState[coreId].currentStackSize[priority]) {return TRUE;}
    }

    return FALSE;
}

/****************************************************************************/
/* NAME: cm_EEM_UpdateStack(                                                */
/*                          t_nmf_core_id id,                               */
/*                          t_nmf_ee_priority priority,                     */
/*                          t_uint32 needMinStackSize,                      */
/*                          t_uint32 *pNewStackValue                        */
/*                         )                                                */
/*--------------------------------------------------------------------------*/
/* DESCRIPTION: If cm_EEM_isStackUpdateNeed() has return true then caller   */
/*              must inform EEM about new stack value for priority.         */
/*              cm_EEM_UpdateStack() will return new global stack size to   */
/*              provide to ee.                                              */
/****************************************************************************/
PUBLIC t_cm_error cm_EEM_UpdateStack(
    t_nmf_core_id coreId,
    t_nmf_ee_priority priority,
    t_uint32 needMinStackSize,
    t_uint32 *pNewStackValue)
{
    t_cm_error error;
    t_uint32 recoveryStackSize = eeState[coreId].currentStackSize[priority];
    t_uint32 i;

    /* in case of SYNCHRONOUS_EXECUTIVE_ENGINE we only use currentStackSize[NMF_SCHED_BACKGROUND] */
    if (eeState[coreId].executiveEngineId == SYNCHRONOUS_EXECUTIVE_ENGINE) {priority = NMF_SCHED_BACKGROUND;}
    eeState[coreId].currentStackSize[priority] = needMinStackSize;
    if (eeState[coreId].executiveEngineId == SYNCHRONOUS_EXECUTIVE_ENGINE) {*pNewStackValue = needMinStackSize;}
    else
    {
        *pNewStackValue = 0;
        for(i = NMF_SCHED_BACKGROUND; i < NMF_SCHED_URGENT + 1;i++)
        {
            *pNewStackValue += eeState[coreId].currentStackSize[i];
        }
    }

    /* try to increase size of stack by modifying xram allocator size */
    error = cm_DSP_setStackSize(coreId, *pNewStackValue);
    if (error != CM_OK) {
        eeState[coreId].currentStackSize[priority] = recoveryStackSize;
    } else {
        LOG_INTERNAL(1, "\n##### Stack update: size=%d, prio=%d on %s #####\n", *pNewStackValue, priority, cm_getDspName(coreId), 0, 0, 0);
    }

    return error;
}

/****************************************************************************/
/* NAME: t_nmf_executive_engine_id(                                         */
/*                          t_nmf_core_id id                                */
/*                         )                                                */
/*--------------------------------------------------------------------------*/
/* DESCRIPTION: return executive engine load on id core.                    */
/****************************************************************************/
PUBLIC t_ee_state * cm_EEM_getExecutiveEngine(t_nmf_core_id coreId)
{
    return &eeState[coreId];
}

/****************************************************************************/
/* NAME: cm_EEM_setTraceMode(                                               */
/*                          t_nmf_core_id id,                               */
/*                          t_uint32 state                                  */
/*                         )                                                */
/*--------------------------------------------------------------------------*/
/* DESCRIPTION: activate/deactivate trace for ee running on id. In case ee  */
/*          is not yet load then information is store.                      */
/****************************************************************************/
PUBLIC void cm_EEM_setTraceMode(t_nmf_core_id coreId, t_uint32 state)
{
    eeState[coreId].traceState = state;
    if (eeState[coreId].instance)
    {
        if(cm_EEM_ForceWakeup(coreId) == CM_OK)
        {
            cm_writeAttribute(eeState[coreId].instance, "xti/traceActive", eeState[coreId].traceState);

            cm_EEM_AllowSleep(coreId);
        }
    }
}

/****************************************************************************/
/* NAME: cm_EEM_setPrintLevel(                                              */
/*                          t_nmf_core_id id,                               */
/*                          t_uint32 level                                  */
/*                         )                                                */
/*--------------------------------------------------------------------------*/
/* DESCRIPTION: set print level for ee running on id. In case ee            */
/*          is not yet load then information is store.                      */
/****************************************************************************/
PUBLIC void cm_EEM_setPrintLevel(t_nmf_core_id coreId, t_uint32 level)
{
    eeState[coreId].printLevel = level;
    if (eeState[coreId].instance)
    {
        if(cm_EEM_ForceWakeup(coreId) == CM_OK)
        {
            cm_writeAttribute(eeState[coreId].instance, "rtos/commonpart/printLevel", eeState[coreId].printLevel);

            cm_EEM_AllowSleep(coreId);
        }
    }
}

t_cm_error cm_EEM_ForceWakeup(t_nmf_core_id coreId)
{
    if(eeState[coreId].nbOfForceWakeup++ == 0)
    {
        t_cm_error error;

        LOG_INTERNAL(1, "ARM: Try to wake up on core id : %d\n", coreId, 0, 0, 0, 0, 0);

        if (cm_DSP_GetState(coreId)->state != MPC_STATE_BOOTED)
        {
            return CM_MPC_NOT_RESPONDING;
        }
        else if ((error = cm_COMP_ULPForceWakeup(coreId)) != CM_OK)
        {
            if (error == CM_MPC_NOT_RESPONDING) {
                if(cm_DSP_GetState(coreId)->state == MPC_STATE_PANIC)
                    /* Don't print error which has been done by Panic handling */;
                else
                {
                    ERROR("CM_MPC_NOT_RESPONDING: DSP %s can't be wakeup'ed\n", cm_getDspName(coreId), 0, 0, 0, 0, 0);
                    cm_DSP_SetStatePanic(coreId);
                }
            }
            return error;
        }
    }
    else
        LOG_INTERNAL(1, "ARM: Not Try to wake up on core id : %d (nbOfForceWakeup = %d)\n", coreId, eeState[coreId].nbOfForceWakeup, 0, 0, 0, 0);

    return CM_OK;
}

void cm_EEM_AllowSleep(t_nmf_core_id coreId)
{
    if(--eeState[coreId].nbOfForceWakeup == 0)
    {
        LOG_INTERNAL(1, "ARM: Allow sleep on core id : %d\n", coreId, 0, 0, 0, 0, 0);

        if (cm_DSP_GetState(coreId)->state != MPC_STATE_BOOTED)
        {
        }
        else if (cm_COMP_ULPAllowSleep(coreId) != CM_OK)
        {
            ERROR("CM_MPC_NOT_RESPONDING: DSP %s can't be allow sleep'ed\n", cm_getDspName(coreId), 0, 0, 0, 0, 0);
        }
    }
    else
        LOG_INTERNAL(1, "ARM: Not Allow sleep on core id : %d (nbOfForceWakeup = %d)\n", coreId, eeState[coreId].nbOfForceWakeup, 0, 0, 0, 0);
}

/* internal api */
t_cm_error cm_EEM_allocPanicArea(t_nmf_core_id coreId, t_cm_domain_id domainId)
{
    t_cm_error error = CM_OK;

    eeState[coreId].panicArea.handle = cm_DM_Alloc(cm_DSP_GetState(coreId)->domainEE, SDRAM_EXT24, 45 /* 42 registers, pc, 2 magic words */,CM_MM_ALIGN_WORD, TRUE);
    if (eeState[coreId].panicArea.handle == INVALID_MEMORY_HANDLE)
        error = CM_NO_MORE_MEMORY;
    else {
        t_uint32 mmdspAddr;

        eeState[coreId].panicArea.addr = cm_DSP_GetHostLogicalAddress(eeState[coreId].panicArea.handle);
        cm_DSP_GetDspAddress(eeState[coreId].panicArea.handle, &mmdspAddr);

        cm_writeAttribute(eeState[coreId].instance, "rtos/commonpart/panicDataAddr", mmdspAddr);
    }

    return error;
}

void cm_EEM_freePanicArea(t_nmf_core_id coreId)
{
    eeState[coreId].panicArea.addr = 0;
    cm_DM_Free(eeState[coreId].panicArea.handle, TRUE);
}