summaryrefslogtreecommitdiffstats
path: root/src/occ_405/sensor/sensor.c
blob: 73c2a5e4147867219ecb197e332e1b3ab33ca47d (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/occ_405/sensor/sensor.c $                                 */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2017                        */
/* [+] 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                                                     */



#include <sensor.h>               // sensor structure and other defines
#include <occ_common.h>           // For size_t needed by memset
#include <string.h>               // For memset
#include "ssx_io.h"               // For sprintf
#include <occ_service_codes.h>    // OCC reason codes
#include <sensor_service_codes.h> // sensor module ids
#include <trac.h>                 // Trace macros
#include <rtls.h>                 // For G_current_tick

#define UINT16_MIN                  0

// Global sensor counter
uint32_t G_amec_sensor_count = 0;


void sensor_init(sensor_t * io_sensor_ptr,
                 const uint16_t i_gsid,
                 const uint16_t * i_miniSnsrPtr
                 ) INIT_SECTION;

void sensor_init_all(void) INIT_SECTION;

// Function Specification
//
//  Name: sensor_init
//
//  Description: Initialize global sensor list.
//
// End Function Specification
void sensor_init(sensor_t * io_sensor_ptr,
                 const uint16_t i_gsid,
                 const uint16_t * i_miniSnsrPtr
                 )
{
    // check if input pointers are valid and global sensor count is
    // within range.
    // Note: Don't need to check i_miniSnsrPtr here as it can be NULL
    if(( io_sensor_ptr != NULL) &&
       (G_amec_sensor_count < MAX_AMEC_SENSORS))
    {
        // zero out sensor pointer
        memset(io_sensor_ptr,0x0,sizeof(sensor_t));

        io_sensor_ptr->gsid = i_gsid;

        io_sensor_ptr->status.reset = 1;

        // set mini sensor pointer to point to i_miniSnsrPtr. i_miniSnsrPtr can
        // be NULL
        io_sensor_ptr->mini_sensor = (uint16_t*)i_miniSnsrPtr;

        G_amec_sensor_count++;

    } // end valid input and max sensor count check
    else
    {
        TRAC_ERR("Invalid input parameters OR Number of "
               "sensor is out of range. Current sensor count: 0x%x, "
               "max allowed: 0x%x",G_amec_sensor_count,MAX_AMEC_SENSORS);
    }
}

// Function Specification
//
//  Name: sensor_clear_minmax
//
//  Description: Clears minimum and maximum fields in the sensor structure.
//               i_clear_type contains one or more values OR'd together from the
//               AMEC_SENSOR_CLEAR_TYPE enumeration.
//
// End Function Specification
void sensor_clear_minmax(sensor_t * io_sensor_ptr,
                         AMEC_SENSOR_CLEAR_TYPE i_clear_type)
{
    if (io_sensor_ptr != NULL)
    {
        if (i_clear_type & AMEC_SENSOR_CLEAR_SAMPLE_MINMAX)
        {
            io_sensor_ptr->sample_min = UINT16_MAX;
            io_sensor_ptr->sample_max = UINT16_MIN;

            // If it has vector sensor, clear max and min position
            if (io_sensor_ptr->vector != NULL)
            {
                io_sensor_ptr->vector->max_pos = VECTOR_SENSOR_DEFAULT_VAL;
                io_sensor_ptr->vector->min_pos = VECTOR_SENSOR_DEFAULT_VAL;
            }
        }

        if (i_clear_type & AMEC_SENSOR_CLEAR_CSM_SAMPLE_MINMAX)
        {
            io_sensor_ptr->csm_sample_min = UINT16_MAX;
            io_sensor_ptr->csm_sample_max = UINT16_MIN;
        }

        if (i_clear_type & AMEC_SENSOR_CLEAR_PROFILER_SAMPLE_MINMAX)
        {
            io_sensor_ptr->profiler_sample_min = UINT16_MAX;
            io_sensor_ptr->profiler_sample_max = UINT16_MIN;
        }

        if (i_clear_type & AMEC_SENSOR_CLEAR_JOB_S_SAMPLE_MINMAX)
        {
            io_sensor_ptr->job_s_sample_min = UINT16_MAX;
            io_sensor_ptr->job_s_sample_max = UINT16_MIN;
        }
    }
    else
    {
        TRAC_ERR("Invalid input parameters ");
    }
}


// Function Specification
//
//  Name: sensor_reset
//
//  Description: Reset sensor fields
//
// End Function Specification
void sensor_reset( sensor_t * io_sensor_ptr)
{
    if( io_sensor_ptr != NULL)
    {
        io_sensor_ptr->timestamp = 0x0;
        io_sensor_ptr->accumulator = 0x0;
        io_sensor_ptr->sample = 0x0;

        // clear mini sensor value
        if( io_sensor_ptr->mini_sensor != NULL)
        {
            *(io_sensor_ptr->mini_sensor) = 0x0;
        }

        sensor_clear_minmax(io_sensor_ptr, AMEC_SENSOR_CLEAR_ALL_MINMAX);

        io_sensor_ptr->status.reset = 0;
    }
    else
    {
        TRAC_ERR("Invalid input parameters ");
    }
}


// Function Specification
//
//  Name: sensor_vectorize
//
//  Description: vectorize sensor
//
// End Function Specification
void sensor_vectorize( sensor_t * io_sensor_ptr,
                       vectorSensor_t * io_vec_sensor_ptr,
                       const VECTOR_SENSOR_OP i_op)
{
    if( (io_vec_sensor_ptr != NULL) && (io_sensor_ptr != NULL))
    {
        // assign to sensor vector pointer
        io_sensor_ptr->vector = io_vec_sensor_ptr;
        // zero out vector sensor and set operation
        memset(io_vec_sensor_ptr,0x0,sizeof(vectorSensor_t));
        io_vec_sensor_ptr->operation = i_op;
    }
    else
    {
        TRAC_ERR("Invalid input sensor pointer");
    }
}


// Function Specification
//
//  Name: sensor_update_minmax
//
//  Description: Updates minimum and maximum fields in the sensor structure.
//
//  Implementation Notes:
//    * This is an internal function so we don't validate parameters
//
// End Function Specification
void sensor_update_minmax(sensor_t * io_sensor_ptr, uint16_t i_sensor_value)
{
    // Update sample min/max fields if needed
    if (i_sensor_value < io_sensor_ptr->sample_min)
    {
        io_sensor_ptr->sample_min = i_sensor_value;
    }
    if (i_sensor_value > io_sensor_ptr->sample_max)
    {
        io_sensor_ptr->sample_max = i_sensor_value;
    }

    // Update CSM sample min/max fields if needed
    if (i_sensor_value < io_sensor_ptr->csm_sample_min)
    {
        io_sensor_ptr->csm_sample_min = i_sensor_value;
    }
    if (i_sensor_value > io_sensor_ptr->csm_sample_max)
    {
        io_sensor_ptr->csm_sample_max = i_sensor_value;
    }

    // Update profiler sample min/max fields if needed
    if (i_sensor_value < io_sensor_ptr->profiler_sample_min)
    {
        io_sensor_ptr->profiler_sample_min = i_sensor_value;
    }
    if (i_sensor_value > io_sensor_ptr->profiler_sample_max)
    {
        io_sensor_ptr->profiler_sample_max = i_sensor_value;
    }

    // Update job scheduler sample min/max fields if needed
    if (i_sensor_value < io_sensor_ptr->job_s_sample_min)
    {
        io_sensor_ptr->job_s_sample_min = i_sensor_value;
    }
    if (i_sensor_value > io_sensor_ptr->job_s_sample_max)
    {
        io_sensor_ptr->job_s_sample_max = i_sensor_value;
    }
}


// Function Specification
//
//  Name: sensor_update
//
//  Description: Update sensor
//
// End Function Specification
void sensor_update( sensor_t * io_sensor_ptr, const uint16_t i_sensor_value)
{
    if( io_sensor_ptr != NULL)
    {
        // reset sensors if requested
        if( io_sensor_ptr->status.reset == 1)
        {
            sensor_reset(io_sensor_ptr);
        }

        // update timestamp
        io_sensor_ptr->timestamp = G_current_tick;

        // update sample value
        io_sensor_ptr->sample = i_sensor_value;

        // update min/max values if needed
        sensor_update_minmax(io_sensor_ptr, i_sensor_value);

        // If this sensor has mini sensor, update it's value
        if( io_sensor_ptr->mini_sensor != NULL)
        {
            *(io_sensor_ptr->mini_sensor) = i_sensor_value;
        }

        // add sample value to accumulator
        io_sensor_ptr->accumulator += i_sensor_value;

        // increment update tag
        io_sensor_ptr->update_tag += 1;
    }
    else
    {
        TRAC_ERR("Invalid sensor pointer");
    }
}


// Function Specification
//
//  Name: sensor_op_min
//
//  Description:  Perform min operation on vector sensor
//
// End Function Specification
uint16_t sensor_op_min(const vectorSensor_t * i_vecSensorPtr,
                       uint8_t * o_position)
{
    uint16_t l_value = UINT16_MAX;
    uint16_t i = 0;

    // Internal function so not checking for input NULL pointers

    // traverse through enabled vector sensors to find the minimum value
    for(; i <  i_vecSensorPtr->size; i++)
    {
        // check if sensor is enabled
        if( i_vecSensorPtr->elem_enabled[i] == 1)
        {
            if( i_vecSensorPtr->source_ptr[i]->sample < l_value)
            {
                l_value = i_vecSensorPtr->source_ptr[i]->sample;
                *o_position = i;
            }
        } // end element enabled check

    } // end for loop

    return l_value;

}


// Function Specification
//
//  Name: sensor_op_max
//
//  Description:  Perform max operation for vector sensor
//
// End Function Specification
uint16_t sensor_op_max(const vectorSensor_t * i_vecSensorPtr,
                       uint8_t * o_position)
{
    uint16_t l_value = UINT16_MIN;
    uint16_t i = 0;

    // Internal function so not checking for input NULL pointers

    // traverse through enabled vector sensors to find the maximum value
    for(; i <  i_vecSensorPtr->size; i++)
    {
        // Check if element is enabled
        if( i_vecSensorPtr->elem_enabled[i] == 1)
        {
            if( i_vecSensorPtr->source_ptr[i]->sample > l_value)
            {
                l_value = i_vecSensorPtr->source_ptr[i]->sample;
                *o_position = i;
            }
        }// end element enabled check

    } // end for loop

    return l_value;

}


// Function Specification
//
//  Name: sensor_op_avg
//
//  Description:  Perform average operation for vector sensor
//
// End Function Specification
uint16_t sensor_op_avg(const vectorSensor_t * i_vecSensorPtr,
                       const uint16_t i_threshold)
{
    uint32_t l_sum = 0;
    uint16_t i = 0;
    uint16_t l_number = 0;

    // Internal function so not checking for input NULL pointers

    // traverse through enabled vector sensors to get sum of sensor sample
    for(; i <  i_vecSensorPtr->size; i++)
    {
        // check if element is enabled
        if( i_vecSensorPtr->elem_enabled[i] == 1)
        {
            // Include sample only if it is higher than threshold
            if( i_vecSensorPtr->source_ptr[i]->sample > i_threshold)
            {
                l_number++;
                l_sum += i_vecSensorPtr->source_ptr[i]->sample;
            }
        } // end element enabled check

    } // end for loop

    // Calculate average
    if( l_number != 0)
    {
        l_sum = l_sum / l_number;
    }

    return l_sum;
}


// Function Specification
//
//  Name: sensor_vector_update
//
//  Description: Update Vector Sensor
//
// End Function Specification
void sensor_vector_update( sensor_t * io_sensor_ptr,const uint32_t i_threshold)
{
    if( io_sensor_ptr != NULL)
    {
        // Reset sensor if requested
        if( io_sensor_ptr->status.reset == 1)
        {
            sensor_reset(io_sensor_ptr);
        }

        // Perform vector sensor operation and update sensor
        if( io_sensor_ptr->vector != NULL)
        {
            uint16_t l_value = 0;
            uint8_t l_position = 0;

            // Min operation
            if( VECTOR_OP_MIN ==  io_sensor_ptr->vector->operation)
            {
                // calculate min value and get sensor position holding min value
                l_value = sensor_op_min(io_sensor_ptr->vector, &l_position);
                // set min position in vector sensor
                io_sensor_ptr->vector->min_pos = l_position;
                // Update only if needed
                if( l_value != UINT16_MAX)
                {
                    // update sensor with new min value
                    sensor_update(io_sensor_ptr,l_value);
                }
            }
            // Max operation
            else if( VECTOR_OP_MAX == io_sensor_ptr->vector->operation)
            {
                // calculate max value and get sensor position holding max value
                l_value = sensor_op_max(io_sensor_ptr->vector, &l_position);
                // set max position in vector sensor
                io_sensor_ptr->vector->max_pos = l_position;
                // Update only if needed
                if( l_value != UINT16_MIN)
                {
                    // update sensor with new max value
                    sensor_update(io_sensor_ptr,l_value);
                }
            }
            // Average operation
            else if( VECTOR_OP_AVG == io_sensor_ptr->vector->operation)
            {
                // Calculate average of the sensor sample
                l_value = sensor_op_avg(io_sensor_ptr->vector,i_threshold);
                // Update only if needed
                if( l_value != 0)
                {
                    // update sensor with new average value
                    sensor_update(io_sensor_ptr,l_value);
                }
            }
            // Unsupported operation
            else
            {
                TRAC_ERR("Unsupported vector sensor operation: 0x%x",
                         io_sensor_ptr->vector->operation);
            }

        } // end valid vector sensor check
        else
        {
            TRAC_ERR("This sensor does not have vector sensor");
        }
    }
    else
    {
        TRAC_ERR("Invalid input sensor pointer");
    }
}


// Function Specification
//
//  Name: getSensorByGsid
//
//  Description: Get sensor data using GSID (Global Sensor ID)
//
// End Function Specification
sensor_t * getSensorByGsid( const uint16_t i_gsid)
{
    sensor_t * l_sensorPtr = NULL;

    // check if input gsid is within range. Return sensor pointer if within
    // range. Else return NULL
    if( i_gsid < G_amec_sensor_count)
    {
        l_sensorPtr = G_amec_sensor_list[i_gsid];
    }

    return l_sensorPtr;
}


// Function Specification
//
//  Name: sensor_vector_elem_enable
//
//  Description: Enable/disable vector sensor element. This interface can be
//               used at runtime
//
// End Function Specification
void sensor_vector_elem_enable( vectorSensor_t* io_sensor_vector_ptr,
                                const uint8_t i_loc,
                                const uint8_t i_enable)
{
    if( io_sensor_vector_ptr != NULL)
    {
        // check if location of the vector sensor is within range
        if( i_loc < io_sensor_vector_ptr->size)
        {
            // set element enabled
            io_sensor_vector_ptr->elem_enabled[i_loc] = i_enable;
        }
        else
        {
            TRAC_ERR("Invalid input location: 0x%x, max size: 0x%x",
                     i_loc,io_sensor_vector_ptr->size);
        }
    }
    else
    {
        TRAC_ERR("NULL input vector sensor pointer");
    }
}


// Function Specification
//
//  Name: sensor_vector_elem_add
//
//  Description: Add element to the vector sensor. If element at the given
//  location is already present, it will be overwritten.
//
// End Function Specification
void sensor_vector_elem_add( vectorSensor_t* io_sensor_vector_ptr,
                             const uint8_t i_loc,
                             const sensor_t * i_elemPtr)
{
    if( (io_sensor_vector_ptr == NULL) ||
        (i_elemPtr == NULL ) ||
        (i_loc >= MAX_VECTOR_SENSORS))
    {
        TRAC_ERR("Invalid input parameters. Either pointers are NULL or "
               "location is out of range i_loc: 0x%x,max allowed: 0x%x",
               i_loc,MAX_VECTOR_SENSORS);
    }
    else if( (i_loc > io_sensor_vector_ptr->size))
    {
        TRAC_ERR("Invalid location. Location does not make element contiguous "
               "i_loc: 0x%x,current vector size: 0x%x",i_loc,
               io_sensor_vector_ptr->size);
    }
    else
    {
        // Increase size if spot is empty. Else we are overwriting existing
        // slot so no need to increment vector size
        if(io_sensor_vector_ptr->source_ptr[i_loc] == NULL)
        {
            io_sensor_vector_ptr->size++;
        }
        // set element and enable it.
        io_sensor_vector_ptr->source_ptr[i_loc] = (sensor_t*)i_elemPtr;
        io_sensor_vector_ptr->elem_enabled[i_loc] = 1;
    }
}



// Function Specification
//
//  Name: sensor_init_all
//
//  Description: Initialize all sensors in the global sensor list.
//
// End Function Specification
void sensor_init_all(void)
{
  int i;
  int l_num_entries = NUMBER_OF_SENSORS_IN_LIST;
  const sensor_ptr_t * l_argPtrSensor = &G_amec_sensor_list[0];
  const minisensor_ptr_t * l_argPtrMiniSensor = &G_amec_mini_sensor_list[0];

  for(i=0; i < l_num_entries; i++)
  {
      sensor_init(l_argPtrSensor[i], i, l_argPtrMiniSensor[i]);
  }

  // If G_amec_sensor_count doesn't match the number of sensors, we must have
  // failed to initialize one or more sensors.
  if(NUMBER_OF_SENSORS_IN_LIST != G_amec_sensor_count)
  {
    TRAC_ERR("Sensor Initialization Failed to initialize all sensors");

      /* @
       * @errortype
       * @moduleid    SENSOR_INITIALIZE
       * @reasoncode  INTERNAL_FAILURE
       * @userdata1   G_amec_sensor_count - number of sensors initialized
       * @userdata2   NUMBER_OF_SENSORS_IN_LIST - total number of OCC sensors
       * @userdata4   OCC_NO_EXTENDED_RC
       * @devdesc     Firmware internal failure initializing sensors
       */
      errlHndl_t l_err = createErrl(
          SENSOR_INITIALIZE,        // Module ID
          INTERNAL_FAILURE,         // Reason Code  // @wb003
          OCC_NO_EXTENDED_RC,       // Extended reason code
          ERRL_SEV_PREDICTIVE,      // Severity
          NULL,                     // Trace
          0,                        // Trace Size
          G_amec_sensor_count,      // UserData 1
          NUMBER_OF_SENSORS_IN_LIST // UserData 2
          );

      commitErrl(&l_err);
  }

  TRAC_IMP("Sensor Initialization Complete");
}

OpenPOWER on IntegriCloud