summaryrefslogtreecommitdiffstats
path: root/src/occ_405/amec/amec_tasks.c
blob: ce306a905271ac6d4a46d26211cca79681c79b66 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/occ_405/amec/amec_tasks.c $                               */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2015                        */
/* [+] 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                                                     */

//*************************************************************************
// Includes
//*************************************************************************
#include <occ_common.h>
#include <ssx.h>
#include <errl.h>               // Error logging
#include <trac.h>               // Error logging
#include "rtls.h"
#include "occ_service_codes.h"  // for SSX_GENERIC_FAILURE
#include "amec_smh.h"
#include "amec_master_smh.h"
#include "amec_slave_smh.h"
#include "amec_sys.h"

//*************************************************************************
// Externs
//*************************************************************************

//*************************************************************************
// Macros
//*************************************************************************
/* rotate x to the left by s bits */
uint8_t _rotl8(uint8_t val, uint8_t shift)
{
  return (val << shift) | (val >> (8 - shift));
}

//*************************************************************************
// Defines/Enums
//*************************************************************************

//*************************************************************************
// Structures
//*************************************************************************

//*************************************************************************
// Globals
//*************************************************************************

//*************************************************************************
// Function Prototypes
//*************************************************************************

//*************************************************************************
// Functions
//*************************************************************************

// Function Specification
//
// Name: amec_generic_smh
//
// Description: Generic State Machine that allows for 8 states per level,
//              with 3 levels.
//              If State Machine gets called/incremented every          250us,
//                - Each function entry in Level 0 table runs every       2ms,
//                - Each function entry in Level 1 table will run every  16ms,
//                - Each function entry in Level 2 table will run every 128ms.
//
//              Anything longer than that will need to be taken care of
//              inside of the states themselves.
//
//              NULL entries are allowed, for both state functions and
//              for pointers to substate tables.
//              -  If a state does not have substates, it should have NULL
//                 in the substate table for the substate table pointer.
//              -  If a state does not wish to run a function, it should have
//                 NULL in the substate table for the function pointer.
//              -  A state can have substates without having a function that
//                 runs (i.e. NULL in function pointer, but valid in substate
//                 table pointer)
//
//              In addition, if i_smh_timing is not NULL and points to a
//              smh_state_timing_t structure, this state machine will grab
//              the SSX timings of each "Level 0" state and store the longest
//              time in each state in that structure.
//
// End Function Specification
void amec_generic_smh(const smh_tbl_t * i_smh_tbl,
                      smh_state_t * i_smh_state,
                      smh_state_timing_t * i_smh_timing)
{
  uint64_t l_start = ssx_timebase_get();
  uint8_t l_state = 0;

  // ----------------------------------------------------
  // AMEC State Machine States
  // ----------------------------------------------------
  if(NULL != i_smh_tbl)
  {
    // Read AMEC State Machine "State"
    l_state = AMEC_STATE(i_smh_state);

    AMEC_DBG("SMH: AMEC State: [%d] Mask: [%02x]\n",l_state,i_smh_state->state);

    // check if state value makes sense
    if ( l_state >= NUM_AMEC_SMH_STATES )
    {
        TRAC_ERR("SMH: Invalid AMEC State: [%d] Mask: [%02x]\n",l_state,i_smh_state->state);

        // TODO: how to handle this situation?
        // for now exiting function
        return;
    }

    // If there is a function to run for this state (!NULL), run it
    if(NULL != i_smh_tbl[(l_state)].state)
    {
      // Call into AMEC State (i.e State 6)
      (*(i_smh_tbl[(l_state)].state))();
    }

    // ----------------------------------------------------
    // AMEC State Machine Substates
    // ----------------------------------------------------
    if(NULL != i_smh_tbl[l_state].substate)
    {
      // Read AMEC State Machine "Substate"
      uint8_t l_substate = AMEC_SUBSTATE(i_smh_state);

      AMEC_DBG("\tSMH: AMEC Sub State: %d Mask: %02x\n",l_substate,i_smh_state->substate);

        // check if sub state value makes sense
        if ( l_substate >= NUM_AMEC_SMH_STATES )
        {
            TRAC_ERR("SMH: Invalid AMEC Sub State: [%d] Mask: [%02x]\n",l_substate,i_smh_state->substate);

            // TODO: how to handle this situation?
            // for now exiting function
            return;
        }

      // Set up pointer to sub-state table
      const smh_tbl_t * l_sub_state_tbl = i_smh_tbl[l_state].substate;

      // If there is a function to run for this substate (!NULL), run it
      if(NULL != (l_sub_state_tbl+l_substate)->state)
      {
        // Call into AMEC Substate (i.e State 6.1)
        (*((l_sub_state_tbl+l_substate)->state))();
      }

      // ----------------------------------------------------
      // AMEC State Machine Sub-substates
      // ----------------------------------------------------
      if(NULL != (l_sub_state_tbl+l_substate)->substate)
      {
        // Read AMEC State Machine "Sub-Substate"
        uint8_t l_sub_substate = AMEC_SUB_SUBSTATE(i_smh_state);

        AMEC_DBG("\t\tSMH: AMEC Sub Sub State: %d Mask: %02x\n",l_sub_substate,i_smh_state->sub_substate);

        // check if sub sub state value makes sense
        if ( l_sub_substate >= NUM_AMEC_SMH_STATES )
        {
            TRAC_ERR("SMH: Invalid AMEC Sub State: [%d] Mask: [%02x]\n",l_sub_substate,i_smh_state->substate);

            // TODO: how to handle this situation?
            // for now exiting function
            return;
        }

        // Set up pointer to sub-state table
        const smh_tbl_t * l_sub_substate_tbl = (l_sub_state_tbl+l_substate)->substate;

        // If there is a function to run for this sub-substate (!NULL), run it
        if(NULL != (l_sub_substate_tbl+l_sub_substate)->state)
        {
          // Call into AMEC Sub-Substate (i.e State 6.1.3)
          (*((l_sub_substate_tbl+l_sub_substate)->state))();
        }
      }
    }
  }

  // ------------------------------------------------------
  // Increment the state(s) of the AMEC State Machine
  // ------------------------------------------------------

  // Bump the state as final undertaking before task ends
  AMEC_STATE_NEXT(i_smh_state);
  if(AMEC_INITIAL_STATE == i_smh_state->state)
  {
    // Only bump the substate when "state" has wrapped
    AMEC_SUBSTATE_NEXT(i_smh_state);
    if(AMEC_INITIAL_STATE == i_smh_state->substate)
    {
      // Only bump the substate when "state" & "substate" have wrapped
      AMEC_SUB_SUBSTATE_NEXT(i_smh_state);
    }
  }

  // ------------------------------------------------------
  // Update the max timing of this state(s) of the AMEC State Machine
  // ------------------------------------------------------
  if((NULL != i_smh_timing) && (NULL != i_smh_timing->update_sensor))
  {
    // Calculate the duration of the state
    uint64_t l_state_duration = DURATION_IN_US_UNTIL_NOW_FROM(l_start);

    // Make a function call to update sensor with duration
    (*((i_smh_timing)->update_sensor))(l_state,(uint32_t)l_state_duration);
  }
}


// Function Specification
//
// Name: task_amec_master
//
// Description: Purpose of this function is to run all the AMEC "master-only" code
//
// Task Flags: RTL_FLAG_MSTR
//             RTL_FLAG_OBS
//             RTL_FLAG_ACTIVE
//             RTL_FLAG_MSTR_READY
//             RTL_FLAG_NO_APSS
//             RTL_FLAG_RUN
//
// End Function Specification
void task_amec_master( task_t *i_self)
{
  uint64_t l_start = ssx_timebase_get();

  amec_mst_common_tasks_pre();

  amec_generic_smh( amec_mst_state_table, &G_amec_mst_state, &G_amec_mst_state_timings );

  amec_mst_common_tasks_post();

  // Add the time that this master task took to the total AMEC int task time,
  // which already contains the slave task duration
  G_fw_timing.ameint_dur += DURATION_IN_US_UNTIL_NOW_FROM(l_start);
}


// Function Specification
//
// Name: task_amec_slave
//
// Description: Purpose of this function is to run all the AMEC "slave-only" code
//
// Task Flags: RTL_FLAG_MSTR
//             RTL_FLAG_NONMSTR
//             RTL_FLAG_OBS
//             RTL_FLAG_ACTIVE
//             RTL_FLAG_MSTR_READY
//             RTL_FLAG_NO_APSS
//             RTL_FLAG_RUN
//
// End Function Specification
void task_amec_slave( task_t *i_self)
{
  uint64_t l_start = ssx_timebase_get();

  amec_slv_common_tasks_pre();

  amec_generic_smh( amec_slv_state_table, &G_amec_slv_state, &G_amec_slv_state_timings );

  amec_slv_common_tasks_post();

  // Set the total AMEC int task time for this tick, to the duration of the slave tasks.
  G_fw_timing.ameint_dur = DURATION_IN_US_UNTIL_NOW_FROM(l_start);
}

/*----------------------------------------------------------------------------*/
/* End                                                                        */
/*----------------------------------------------------------------------------*/
OpenPOWER on IntegriCloud