summaryrefslogtreecommitdiff
path: root/drivers/staging/nmf-cm/cm/engine/trace/src/panic.c
blob: e59d9f8b1bafa680833e2b444d5f28ec8bb94f4f (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
/*
 * 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.
 */
#include <cm/inc/cm_type.h>
#include <cm/engine/component/inc/introspection.h>
#include <cm/engine/component/inc/bind.h>
#include <cm/engine/executive_engine_mgt/inc/executive_engine_mgt.h>
#include <cm/engine/trace/inc/trace.h>
#include <cm/engine/api/control/irq_engine.h>

#include <cm/engine/utils/inc/convert.h>
#include <share/communication/inc/nmf_service.h>

PUBLIC t_cm_error cm_SRV_allocateTraceBufferMemory(t_nmf_core_id coreId, t_cm_domain_id domainId)
{
    t_ee_state *state = cm_EEM_getExecutiveEngine(coreId);

    state->traceDataHandle = cm_DM_Alloc(domainId, SDRAM_EXT16,
            TRACE_BUFFER_SIZE * sizeof(struct t_nmf_trace) / 2, CM_MM_ALIGN_WORD, TRUE);
    if (state->traceDataHandle == INVALID_MEMORY_HANDLE)
        return CM_NO_MORE_MEMORY;
    else
    {
        t_uint32 mmdspAddr;
        int i;

        state->traceDataAddr = (struct t_nmf_trace*)cm_DSP_GetHostLogicalAddress(state->traceDataHandle);
        cm_DSP_GetDspAddress(state->traceDataHandle, &mmdspAddr);
        cm_writeAttribute(state->instance, "rtos/commonpart/traceDataAddr", mmdspAddr);

        eeState[coreId].readTracePointer = 0;
        eeState[coreId].lastReadedTraceRevision = 0;

        for(i = 0; i < TRACE_BUFFER_SIZE; i++)
            state->traceDataAddr[i].revision = 0;

        return CM_OK;
    }
}

PUBLIC void cm_SRV_deallocateTraceBufferMemory(t_nmf_core_id coreId)
{
    t_ee_state *state = cm_EEM_getExecutiveEngine(coreId);

    state->traceDataAddr = 0;
    cm_DM_Free(state->traceDataHandle, TRUE);
}

static t_uint32 swapHalfWord(t_uint32 word)
{
    return (word >> 16) | (word << 16);
}

PUBLIC EXPORT_SHARED t_cm_trace_type CM_ENGINE_GetNextTrace(
        t_nmf_core_id               coreId,
        struct t_nmf_trace          *trace)
{
    t_ee_state *state = cm_EEM_getExecutiveEngine(coreId);
    t_uint32 foundRevision;
    t_cm_trace_type type;

    OSAL_LOCK_API();
    if (state->traceDataAddr == NULL) {
        type = CM_MPC_TRACE_NONE;
	goto out;
    }

    foundRevision = swapHalfWord(state->traceDataAddr[state->readTracePointer].revision);

    if(foundRevision <= state->lastReadedTraceRevision)
    {
        // It's an old trace forgot it
        type = CM_MPC_TRACE_NONE;
    }
    else
    {
        struct t_nmf_trace          *traceRaw;

        if(foundRevision == state->lastReadedTraceRevision + 1)
        {
            type = CM_MPC_TRACE_READ;
        }
        else
        {
            type = CM_MPC_TRACE_READ_OVERRUN;
            /*
             * If we find that revision is bigger, thus we are in overrun, then we take the writePointer + 1 which
             * correspond to the older one.
             * => Here there is a window where the MMDSP could update writePointer just after
             */
            state->readTracePointer = (cm_readAttributeNoError(state->instance, "rtos/commonpart/writePointer") + 1) % TRACE_BUFFER_SIZE;
        }

        traceRaw = &state->traceDataAddr[state->readTracePointer];

        trace->timeStamp = swapHalfWord(traceRaw->timeStamp);
        trace->componentId = swapHalfWord(traceRaw->componentId);
        trace->traceId = swapHalfWord(traceRaw->traceId);
        trace->paramOpt = swapHalfWord(traceRaw->paramOpt);
        trace->componentHandle = swapHalfWord(traceRaw->componentHandle);
        trace->parentHandle = swapHalfWord(traceRaw->parentHandle);

        trace->params[0] = swapHalfWord(traceRaw->params[0]);
        trace->params[1] = swapHalfWord(traceRaw->params[1]);
        trace->params[2] = swapHalfWord(traceRaw->params[2]);
        trace->params[3] = swapHalfWord(traceRaw->params[3]);

        state->readTracePointer = (state->readTracePointer + 1) % TRACE_BUFFER_SIZE;
        state->lastReadedTraceRevision = swapHalfWord(traceRaw->revision);
        trace->revision = state->lastReadedTraceRevision;
    }

out:
    OSAL_UNLOCK_API();

    return type;
}


/*
 * Panic
 */
const struct {
	char* name;
    unsigned int info1:1;
    unsigned int PC:1;
	unsigned int SP:1;
	unsigned int interface:1;
} reason_descrs[] = {
		{"NONE_PANIC",                0,  0,  0,  0},
		{"INTERNAL_PANIC",            1,  0,  0,  0},
		{"MPC_NOT_RESPONDING_PANIC",  0,  0,  0,  0}, /* Should not be useful since in that case CM_getServiceDescription() not call */
		{"USER_STACK_OVERFLOW",       0,  1,  1,  0},
		{"SYSTEM_STACK_OVERFLOW",     0,  1,  1,  0},
		{"UNALIGNED_LONG_ACCESS",     0,  1,  0,  0},
		{"EVENT_FIFO_OVERFLOW",       0,  0,  0,  1},
		{"PARAM_FIFO_OVERFLOW",       0,  0,  0,  1},
		{"INTERFACE_NOT_BINDED",      0,  1,  0,  0},
		{"USER_PANIC",                1,  0,  0,  0}
};

static t_component_instance* getCorrespondingInstance(
        t_panic_reason panicReason,
        t_uint32 panicThis,
        t_dup_char *itfName,
	t_cm_instance_handle *instHandle) {
    t_component_instance *instance;
    t_uint32 k;

    for (k=0; k<ComponentTable.idxMax; k++) {
        if ((instance = componentEntry(k)) == NULL)
            continue;
        if(panicReason == PARAM_FIFO_OVERFLOW ||
                panicReason == EVENT_FIFO_OVERFLOW) {
            // Panic has been generated by binding component, search the client who has call it
            // and return the client handle (not the BC one).
            int i;

            if(instance->thisAddress == panicThis && panicThis == 0) {
                *itfName = "Internal NMF service";
                *instHandle = ENTRY2HANDLE(instance, k);
                return instance;
            }

            for(i = 0; i < instance->Template->requireNumber; i++) {
                int nb = instance->Template->requires[i].collectionSize, j;
                for(j = 0; j < nb; j++) {
                    if(instance->interfaceReferences[i][j].instance != NULL &&
                            instance->interfaceReferences[i][j].instance != (t_component_instance *)NMF_HOST_COMPONENT &&
                            instance->interfaceReferences[i][j].instance != (t_component_instance *)NMF_VOID_COMPONENT &&
                            instance->interfaceReferences[i][j].instance->thisAddress == panicThis)
                    {
                        *itfName = instance->Template->requires[i].name;
                        *instHandle = ENTRY2HANDLE(instance, k);
                        return instance;
                    }
                }
            }
        } else {
            // The component which has generated the panic is the good one.

            if(instance->thisAddress == panicThis) {
		*itfName = "?";
                *instHandle = ENTRY2HANDLE(instance, k);
                return instance;
            }
        }
    }

    *itfName = "?";
    *instHandle = 0;
    return 0;
}

PUBLIC EXPORT_SHARED t_cm_error CM_ReadMPCString(
        t_nmf_core_id               coreId,
        t_uint32                    dspAddress,
        char *                      buffer,
        t_uint32                    bufferSize) {

    while(--bufferSize > 0)
    {
        char ch = cm_DSP_ReadXRamWord(coreId, dspAddress++);
        if(ch == 0)
            break;

        *buffer++ = ch;
    };

    *buffer = 0;

    // Reset panicReason
    cm_writeAttribute(cm_EEM_getExecutiveEngine(coreId)->instance,
            "rtos/commonpart/serviceReason", MPC_SERVICE_NONE);

    return CM_OK;
}

/****************/
/* Generic part */
/****************/
PUBLIC EXPORT_SHARED t_cm_error CM_getServiceDescription(
        t_nmf_core_id               coreId,
        t_cm_service_type           *srcType,
        t_cm_service_description    *srcDescr)
{
    t_uint32 serviceReason;
    t_component_instance *ee;

    // Acknowledge interrupt (do it before resetting panicReason)
    cm_DSP_AcknowledgeDspIrq(coreId, DSP2ARM_IRQ_1);

    ee = cm_EEM_getExecutiveEngine(coreId)->instance;

    // Read panicReason
    serviceReason = cm_readAttributeNoError(ee, "rtos/commonpart/serviceReason");
    if(serviceReason == MPC_SERVICE_PRINT)
    {
        *srcType = CM_MPC_SERVICE_PRINT;

        srcDescr->u.print.dspAddress = cm_readAttributeNoError(ee, "rtos/commonpart/serviceInfo0");
        srcDescr->u.print.value1 = cm_readAttributeNoError(ee, "rtos/commonpart/serviceInfo1");
        srcDescr->u.print.value2 = cm_readAttributeNoError(ee, "rtos/commonpart/serviceInfo2");
    }
    else if(serviceReason == MPC_SERVICE_TRACE)
    {
        *srcType = CM_MPC_SERVICE_TRACE;
    }
    else if(serviceReason != MPC_SERVICE_NONE)
    {
        t_uint32 panicThis;
        t_dup_char itfName;
        t_component_instance *instance;

        *srcType = CM_MPC_SERVICE_PANIC;
        srcDescr->u.panic.panicReason = (t_panic_reason)serviceReason;
        srcDescr->u.panic.panicSource = MPC_EE;
        srcDescr->u.panic.info.mpc.coreid = coreId;

        // Read panicThis
        panicThis = cm_readAttributeNoError(ee, "rtos/commonpart/serviceInfo0");

        instance = getCorrespondingInstance(srcDescr->u.panic.panicReason, panicThis, &itfName, &srcDescr->u.panic.info.mpc.faultingComponent);

        LOG_INTERNAL(0, "Error: Panic(%s, %s), This=%x", cm_getDspName(coreId),
                reason_descrs[srcDescr->u.panic.panicReason].name, (void*)panicThis, 0, 0, 0);

        if(reason_descrs[srcDescr->u.panic.panicReason].interface != 0)
        {
            LOG_INTERNAL(0, ", interface=%s", itfName, 0, 0, 0, 0, 0);
        }

        if(reason_descrs[srcDescr->u.panic.panicReason].info1 != 0)
        {
            // Info 1
            srcDescr->u.panic.info.mpc.panicInfo1 = cm_readAttributeNoError(ee, "rtos/commonpart/serviceInfo1");

            LOG_INTERNAL(0, ", Info=%x", srcDescr->u.panic.info.mpc.panicInfo1, 0, 0, 0, 0, 0);
        }

        if(reason_descrs[srcDescr->u.panic.panicReason].PC != 0)
        {
            t_uint32 DspAddress = 0xFFFFFFFF;
            t_uint32 DspSize = 0x0;

            // PC need to be read in rtos/commonpart/serviceInfo1
            srcDescr->u.panic.info.mpc.panicInfo1 = cm_readAttributeNoError(ee, "rtos/commonpart/serviceInfo1");

            if(instance != 0)
            {
                cm_DSP_GetDspAddress(instance->memories[instance->Template->codeMemory->id], &DspAddress);
                cm_DSP_GetDspMemoryHandleSize(instance->memories[instance->Template->codeMemory->id], &DspSize);
            }

            if(DspAddress <= srcDescr->u.panic.info.mpc.panicInfo1 &&
                    srcDescr->u.panic.info.mpc.panicInfo1 < (DspAddress + DspSize))
                LOG_INTERNAL(0, ", PC:off=%x <abs=%x>",
                        srcDescr->u.panic.info.mpc.panicInfo1 - DspAddress,
                        srcDescr->u.panic.info.mpc.panicInfo1, 0, 0, 0, 0);
            else
                LOG_INTERNAL(0, ", PC:<abs=%x>", srcDescr->u.panic.info.mpc.panicInfo1, 0, 0, 0, 0, 0);
        }

        if(reason_descrs[srcDescr->u.panic.panicReason].SP != 0)
        {
            srcDescr->u.panic.info.mpc.panicInfo2 = cm_readAttributeNoError(ee, "rtos/commonpart/serviceInfo2");

            LOG_INTERNAL(0, ", SP=%x", srcDescr->u.panic.info.mpc.panicInfo2, 0, 0, 0, 0, 0);
        }

        LOG_INTERNAL(0, "\n", 0, 0, 0, 0, 0, 0);

        if(instance != 0)
        {
            LOG_INTERNAL(0, "Error:  Component=%s<%s>\n",
                    instance->pathname, instance->Template->name, 0, 0, 0, 0);
        }

        // We don't set rtos/commonpart/serviceReason = MPC_SERVICE_NONE, since we don't want the
        // MMDSP to continue execution, and we put in in Panic state
        cm_DSP_SetStatePanic(coreId);
    }
    else
    {
        *srcType = CM_MPC_SERVICE_NONE;
    }

    return CM_OK;
}