forked from polarfire-soc/hart-software-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhss_state_machine.c
267 lines (219 loc) · 7.77 KB
/
hss_state_machine.c
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
/*******************************************************************************
* Copyright 2019 Microchip Corporation.
*
* SPDX-License-Identifier: MIT
*
* MPFS HSS Embedded Software
*
*/
/**
* \file State Machine Engine
*
* \brief Registered state machine super-loop
*/
#include "config.h"
#include "hss_types.h"
#include "hss_state_machine.h"
#include "hss_progress.h"
#include "hss_clock.h"
#include "hss_debug.h"
#include <assert.h>
#include "ssmb_ipi.h"
#ifndef CONFIG_SERVICE_IPI_POLL
# include "csr_helper.h"
#endif
#ifdef CONFIG_DEBUG_PROFILING_SUPPORT
# include "profiling.h"
#endif
#include "hss_registry.h"
/**
* \brief Ensure that state is valid for given state machine
*/
static inline bool IsValidState(struct StateMachine *pStateMachine, int state)
{
bool result = false;
if ((state >= 0) && (state < (int)pStateMachine->numStates)) {
result = true;
}
return result;
}
/**
* \brief Run all registered state machines
*/
void RunStateMachine(struct StateMachine *const pCurrentMachine)
{
assert(pCurrentMachine != NULL);
const char *pMachineName = "<error-unnamed>";
if (pCurrentMachine->pMachineName != NULL) {
pMachineName = (const char *)pCurrentMachine->pMachineName;
}
//mHSS_DEBUG_PRINTF(LOG_NORMAL, "running machine %s" CRLF, pMachineName);
{
stateType_t prevState = pCurrentMachine->prevState;
stateType_t currentState = pCurrentMachine->state;
struct StateDesc const * const pCurrentStateDesc =
&(pCurrentMachine->pStateDescs[currentState]);
assert(pCurrentStateDesc != NULL); // mandatory handler
assert(pCurrentStateDesc->state == currentState);
if (prevState != currentState) {
if (IsValidState(pCurrentMachine, prevState)) {
struct StateDesc const * const pLastStateDesc =
&(pCurrentMachine->pStateDescs[prevState]);
assert(pLastStateDesc->state == prevState);
if (pLastStateDesc->state_onExit) { // optional handler
pLastStateDesc->state_onExit(pCurrentMachine);
}
}
if (pCurrentStateDesc->state_onEntry) { // optional handler
pCurrentStateDesc->state_onEntry(pCurrentMachine);
}
pCurrentMachine->startTime = HSS_GetTickCount();
pCurrentMachine->executionCount = 0;
pCurrentMachine->prevState = pCurrentMachine->state;
}
//if (mLIKELY(pCurrentStateDesc->state_handler != NULL)) {
pCurrentStateDesc->state_handler(pCurrentMachine);
pCurrentMachine->lastExecutionTime = HSS_GetTickCount();
pCurrentMachine->executionCount++;
//}
#if IS_ENABLED(CONFIG_DEBUG_LOG_STATE_TRANSITIONS)
// debug print any state transitions...
if (pCurrentMachine->debugFlag) {
const char *pLastStateName;
// refresh states
prevState = pCurrentMachine->prevState;
currentState = pCurrentMachine->state;
pLastStateName =
(pCurrentMachine->pStateDescs[prevState]).pStateName;
// should be valid at this stage
{
const char *pCurrentStateName =
(pCurrentMachine->pStateDescs[currentState]).pStateName;
if (prevState != currentState) {
mHSS_DEBUG_PRINTF(LOG_STATE_TRANSITION, "%s::%s -> %s::%s" CRLF, pMachineName,
pLastStateName, pMachineName, pCurrentStateName);
}
}
}
#else
(void)pMachineName;
#endif
}
}
static HSSTicks_t maxLoopTime = 0u;
static uint64_t loopCount = 0u;
void RunStateMachines(const size_t spanOfPStateMachines, struct StateMachine *const pStateMachines[])
{
HSSTicks_t const startTicks = HSS_GetTickCount();
HSSTicks_t endTicks;
#ifndef CONFIG_SERVICE_IPI_POLL
// poll IPIs each iteration for new messages
{
const union HSSHartBitmask hartBitmask = { .uint = mHSS_BITMASK_ALL_U54 };
bool status = IPI_PollReceive(hartBitmask);
if (status) {
enum HSSHartId const myHartId = current_hartid();
size_t i;
for (i = 0u; i < MAX_NUM_HARTS; i++) {
if (mUNLIKELY(i == myHartId)) { continue; } // don't handle messages if to myself
uint32_t index = IPI_CalculateQueueIndex(i, myHartId);
if (IPI_GetQueuePendingCount(index)) {
IPI_ConsumeIntent(i, IPI_MSG_ACK_COMPLETE); // gobble up any ACK completes
IPI_ConsumeIntent(i, IPI_MSG_ACK_PENDING); // gobble up any ACK pendings
}
}
}
}
#endif
{
size_t i = 0u;
for (i = 0; i < spanOfPStateMachines; i++) {
struct StateMachine * const pCurrentMachine = pStateMachines[i];
RunStateMachine(pCurrentMachine);
}
}
loopCount++;
endTicks = HSS_GetTickCount();
#if defined(CONFIG_DEBUG_LOOP_TIMES) || defined(CONFIG_DEBUG_IPI_STATS)
{
HSSTicks_t const delta = endTicks - startTicks;
bool dump_flag = false;
bool max_exceeded_flag = false;
if (mUNLIKELY(delta > maxLoopTime)) {
maxLoopTime = delta;
max_exceeded_flag = true;
}
if (mUNLIKELY((loopCount % (unsigned long)CONFIG_DEBUG_LOOP_TIMES_THRESHOLD) == 0u)) {
dump_flag = true;
}
if (mUNLIKELY(dump_flag || max_exceeded_flag)) {
#ifdef CONFIG_DEBUG_PROFILING_SUPPORT
dump_profile();
#endif
#ifdef CONFIG_DEBUG_IPI_STATS
IPI_DebugDumpStats();
#endif
#ifdef CONFIG_DEBUG_LOOP_TIMES
if (dump_flag) {
mHSS_DEBUG_PRINTF(LOG_STATUS, " loop %" PRIu64
" took %" PRIu64 " tick%s"
" (max %" PRIu64 " tick%s)" CRLF, loopCount,
delta, delta == 1u ? "" : "s",
maxLoopTime, maxLoopTime == 1u ? "" : "s");
} else /* if (max_exceeded_flag) */ {
mHSS_DEBUG_PRINTF(LOG_WARN, " loop %" PRIu64
" took %" PRIu64 " tick%s"
" (max %" PRIu64 " tick%s)" CRLF, loopCount,
delta, delta == 1u ? "" : "s",
maxLoopTime, maxLoopTime == 1u ? "" : "s");
}
#endif
}
}
#else
(void)startTicks;
(void)endTicks;
(void)loopCount;
(void)maxLoopTime;
#endif
}
/**
* \brief Get State Machines loop count
*/
uint64_t GetStateMachinesExecutionCount(void)
{
return loopCount;
}
/**
* \brief Run through array of InitFunctions
*/
void RunInitFunctions(const size_t spanOfInitFunctions, const struct InitFunction initFunctions[])
{
size_t i;
for (i = 0u; i < spanOfInitFunctions; i++) {
assert(initFunctions[i].handler);
//mHSS_DEBUG_PRINTF(LOG_NORMAL, "Running %d of %d: %s()" CRLF, i, spanOfInitFunctions,
// initFunctions[i].pName);
bool result = (initFunctions[i].handler)();
if (!result) {
mHSS_DEBUG_PRINTF(LOG_ERROR, "%s() returned %d" CRLF, initFunctions[i].pName, result);
if (initFunctions[i].haltOnFailure) {
while (true) { ; } // HALT on failure
} else if (initFunctions[i].restartOnFailure) {
uint8_t rcvBuf;
bool keyPressedFlag = HSS_ShowTimeout("Init failed, press a key to prevent restart" CRLF, 5u, &rcvBuf);
#if IS_ENABLED(CONFIG_SERVICE_TINYCLI)
if (keyPressedFlag) {
bool HSS_TinyCLI_Parser(void);
(void)HSS_TinyCLI_Parser();
} else {
#else
if (!keyPressedFlag) {
#endif
void _start(void);
_start();
}
}
}
}
}