pmu_hw.c 23.7 KB
Newer Older
GuillemCabo's avatar
GuillemCabo committed
1
2
#include <pmu_hw.h>
#include <math.h>
3
4

//#define __PMU_LIB_DEBUG__
GuillemCabo's avatar
GuillemCabo committed
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

/*
 *   Function    : pmu_counters_enable
 *   Description : It enables the event counters.
 *   Parameters  : None
 *   Return      : None   
 */
void pmu_counters_enable(void) {
    PMUCFG0 |= 0x00000001;
    #ifdef __PMU_LIB_DEBUG__
    printf("Enable counters\n");
    printf("CFG0 = 0x%08x\n", PMUCFG0);
    #endif
}

/*
 *   Function    : pmu_counters_disable
 *   Description : It disables the event counters.
 *   Parameters  : None
 *   Return      : None   
 */
void pmu_counters_disable(void) {
    PMUCFG0 &= 0xFFFFFFFE;
    #ifdef __PMU_LIB_DEBUG__
    printf("Disable counters\n");
    printf("CFG0 = 0x%08x\n", PMUCFG0);
    #endif
}

/*
 *   Function    : pmu_counters_reset
 *   Description : It resets (set to 0) all the event counters.
 *   Parameters  : None
 *   Return      : None   
 */
void pmu_counters_reset(void) {
    PMUCFG0 |= 0x00000002;
    PMUCFG0 &= 0xFFFFFFFD;
    #ifdef __PMU_LIB_DEBUG__
    printf("Softreset counters\n");
    printf("CFG0 = 0x%08x\n", PMUCFG0);
    #endif
}

/*
 *   Function    : pmu_configure_crossbar
 *   Description : It routes the HDL wired signals with the 
 *                 counter modules.
 *   Parameters  : 
 *     - output        : Crossbar output number. See the CROSSBAR_OUTPUT_X 
 *                       defines in pmu_vars.h
 *     - event_index   : Event index of the wired signat in the HDL code. Refer to
 *                       SafePMU User's manual, section 2.2,table 2.1.
 *   Return      : None   
 */
Guillem Cabo's avatar
Guillem Cabo committed
60
unsigned pmu_configure_crossbar(unsigned int output, unsigned int event_index) {
GuillemCabo's avatar
GuillemCabo committed
61
62
63
64
65
66
67
68
69
70
71
72
    if (event_index>CROSSBAR_INPUTS){
        #ifdef __UART__
        printf("Input port %d selected out of range\n", event_index);
        #endif
    return (1);
    } 
    if (output>N_COUNTERS){
        #ifdef __UART__
        printf("Output port %d selected out of range\n", output);
        #endif
    return (1);
    } 
salcaide's avatar
salcaide committed
73
    unsigned int ev_idx = event_index;
GuillemCabo's avatar
GuillemCabo committed
74
75
76
77
    unsigned int fieldw = log2(CROSSBAR_INPUTS);
    //Blank Mask. It will reset any configuration field
    unsigned int bmask ; 
    bmask=(1<<fieldw)-1;
salcaide's avatar
salcaide committed
78
    unsigned int tmp,reg_idx,field_idx;
GuillemCabo's avatar
GuillemCabo committed
79
    //Get the bit position if all registers where concatenated
salcaide's avatar
salcaide committed
80
    tmp = output*fieldw;
GuillemCabo's avatar
GuillemCabo committed
81
82
83
84
85
    //Get the register index given a register width
    reg_idx = tmp/REG_WIDTH;
    //Get the position of the crossbar configuration field
    field_idx = (int)tmp % REG_WIDTH;
    // check if the configuration field has bits in two different registers
salcaide's avatar
salcaide committed
86
87
    unsigned int fieldw1 = fieldw; // Bits in first register
    unsigned int fieldw2 = 0; //Bits in second register
GuillemCabo's avatar
GuillemCabo committed
88
89
90
91
    if ((field_idx+fieldw)>REG_WIDTH) {
        fieldw1 = REG_WIDTH-field_idx;
        fieldw2 = fieldw - fieldw1;
        // Clear previous field
salcaide's avatar
salcaide committed
92
        _PMU_CROSSBAR[reg_idx] &= (~(((1<<fieldw1)-1) << field_idx));
GuillemCabo's avatar
GuillemCabo committed
93
94
95
        _PMU_CROSSBAR[reg_idx+1] &= ~((1<<fieldw2)-1); 
        //Set new values
        _PMU_CROSSBAR[reg_idx] |= ev_idx << field_idx; 
salcaide's avatar
salcaide committed
96
        _PMU_CROSSBAR[reg_idx+1] |= (ev_idx>>fieldw1);
GuillemCabo's avatar
GuillemCabo committed
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
    } else {
        _PMU_CROSSBAR[reg_idx] &= (~((bmask) << field_idx)); // Erease the output field
        _PMU_CROSSBAR[reg_idx] |= ev_idx << field_idx; // Write into the output field
    };
    return (0);
}

/*
 *   Function    : pmu_register_events
 *   Description : It registers all the event given by the ev_table parameter
 *   Parameters  : 
 *       - ev_table      : Table of register events.
 *       - event_count   : Number of register events.
 *   Return      : None   
 */
void pmu_register_events(const crossbar_event_t * ev_table, unsigned int event_count) {
    for (int i = 0; i < event_count; ++i) {
        pmu_configure_crossbar(ev_table[i].output, ev_table[i].event);
    }
}

/*
 *   Function    : pmu_counters_print
 *   Description : It prompt the register events with their
 *                 descriptions.
 *   Parameters  : 
 *       - table         : Table of register events.
 *       - event_count   : Number of register events.
 *   Return      : None   
 */
void pmu_counters_print(const crossbar_event_t * table, unsigned int event_count) {
    for (int i = 0; i < event_count; ++i) {
129
130
131
132
133
134
135
        printf("PMU_COUNTER[%2d] = %10d\t%s\n", i, _PMU_COUNTERS[table[i].output], table[i].description);
    }
}

void pmu_counters_fill_default_descriptions(crossbar_event_t* table, unsigned int event_count){
    for(int i = 0; i < event_count; i++){
        table[i].description = counterDescriptions[table[i].event];
GuillemCabo's avatar
GuillemCabo committed
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
    }
}

/* **********************************
          OVERFLOW SUBMODULE
* **********************************/

/* 
 *   Function    : pmu_overflow_enable
 *   Description : Enable the PMU overflow submodule.
 *   Parameters  : None
 *   Return      : None   
 */
void pmu_overflow_enable(void) {
    PMUCFG0 |= 0x00000004;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_enable\n");
    printf("PMUCFG0 = 0x%08x\n");
    #endif
}

/*
 *   Function    : pmu_overflow_disable
 *   Description : Disable the PMU overflow submodule.
 *   Parameters  : None
 *   Return      : None   
 */
void pmu_overflow_disable(void) {
    PMUCFG0 &= 0xFFFFFFFB;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_disable\n");
    printf("PMUCFG0 = 0x%08x\n", PMUCFG0);
    #endif
}

/*
 *   Function    : pmu_overflow_reset
 *   Description : It resets the overflow flags.
 *   Parameters  : None
 *   Return      : None   
 */
void pmu_overflow_reset(void) {
    PMUCFG0 |= 0x00000008;
    PMUCFG0 &= 0xFFFFFFF7;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_reset\n");
    #endif
}

/*
 *   Function    : pmu_overflow_enable_interrupt
 *   Description : It enables the interrupts for overflow submodule.
 *   Parameters  : 
 *       - mask  : Mask for each counter.
 *   Return      : None   
 */
void pmu_overflow_enable_interrupt(unsigned int mask) {
    PMU_OVERLFOW_IE |= mask;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_enable_interrupt\n");
    printf("PMU_OVERLFOW_IE = 0x%08x\n", PMU_OVERLFOW_IE);
    #endif
}

/*
 *   Function    : pmu_overflow_disable_interrupt
 *   Description : It disables the interrupts for overflow submodule.
 *   Parameters  : None
 *   Return      : None   
 */
void pmu_overflow_disable_interrupt(unsigned int mask) {
    PMU_OVERLFOW_IE &= ~mask;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_disable_interrupt\n");
    #endif
}

/*
 *   Function    : pmu_overflow_get_iv
 *   Description : It disables the interrupts for overflow submodule.
 *   Parameters  : 
 *       - mask  : Mask of each counter.
 *   Return      : 
 *       - 1     : One or more of the counters passed by mask has caused an overflow interrupt.
 *       - 0     : None of the counters passed by mask has caused an overflow interrupt.
 */
unsigned int pmu_overflow_get_interrupt(unsigned int mask) {
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_get_interrupt\n");
    #endif

    return ((PMU_OVERFLOW_IV & mask) != 0);
}

/*
 *   Function    : pmu_overflow_get_iv
 *   Description : It disables the interrupts for overflow submodule.
 *   Parameters  : None
 *   Return      : It returns the Overflow Interrupt Vector register.
 */
unsigned int pmu_overflow_get_iv(void) {
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_get_iv\n");
    #endif
    return (PMU_OVERFLOW_IV);
}

// TODO: Change priority
void pmu_overflow_register_interrupt(void( * isr)(void)) {
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_register_interrupt IN\n");
    #endif

    volatile unsigned int * p;

    p = (unsigned int * )(PLIC_BASE + 0x001000 + 4 * PMU_OVERFLOW_INT_INDEX);
    printf("Pending interrupt %d\n", * p);

    // p = (unsigned int *)(PLIC_BASE + 0x200000);
    // printf("Priority threshold for context 0 = %d\n", *p);
    // *p = 0;

    p = (unsigned int * )(PLIC_BASE + 0x200004);
    printf("Claim complete interrupt for context 0 = %d\n", * p);
    * p = PMU_OVERFLOW_INT_INDEX;

    write_csr(mtvec, isr);

    // Stablishes the priority level for a given interrupt index.
    p = (unsigned int * )(PLIC_BASE + 4 * PMU_OVERFLOW_INT_INDEX);
    * p = 7; // Priority

    // Enables the interrupt for index 7 (0x40) on context 0
    p = (unsigned int * )(PLIC_BASE + 0x002000);
    * p = 0x00000040;

    // // configure CLINT
    // write_csr(mstatus, 0x00006008);
    // write_csr(mie, 0xffffffff);

    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_overflow_register_interrupt OUT\n");
    #endif
}

/* **********************************
           MCCU SUBMODULE
* **********************************/

/*
 *   Function    : pmu_mccu_enable
 *   Description : It enables the MCCU submodule.
 *   Parameters  : None.
 *   Return      : None.
 */
void pmu_mccu_enable(void) {
    PMUCFG1 |= 0x00000001;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_mccu_enable\n");
    printf("PMUCFG1 = %d\n", PMUCFG1);
    #endif
}

/*
 *   Function    : pmu_mccu_disable
 *   Description : It disable the MCCU submodule.
 *   Parameters  : None.
 *   Return      : None.
 */
void pmu_mccu_disable(void) {
    PMUCFG1 &= 0xFFFFFFFE;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_mccu_disable\n");
    printf("PMUCFG1 = %d\n", PMUCFG1);
    #endif
}

/*
 *   Function    : pmu_mccu_reset
 *   Description : It resets the MCCU submodule.
 *   Parameters  : None.
 *   Return      : None.
 */
void pmu_mccu_reset(void) {
    PMUCFG1 |= 0x00000002;
    PMUCFG1 &= 0xFFFFFFFD;
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_mccu_reset\n");
    printf("PMUCFG1 = %d\n", PMUCFG1);
    #endif
}

/*
 *   Function    : pmu_mccu_set_quota_limit
 *   Description : It sets the quota limits for MCCU submodule
 *   Parameters  : 
 *       - core  :  Target core for quota monitoring. Select core number 1, 2, 3 or 4.
 *       - quota :  32 bits wide quota for selected core.
 *   Return      : Unsigned int. 0 no error.
 */
Guillem Cabo's avatar
Guillem Cabo committed
336
unsigned pmu_mccu_set_quota_limit(const unsigned int core,
GuillemCabo's avatar
GuillemCabo committed
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
    const unsigned int quota) {
    if(core>MCCU_N_CORES){
        printf("mccu_set_quota: core %d does not exist\n", core);
	return(1);
    }
    //set update bits
    PMUCFG1 |= 1<<(core+2);//Offset are enable en reset bits
    //set target quota
    _PMU_MCCU_QUOTA[core]=quota;
    //release set bits
    PMUCFG1 &= ~(0x3f<<2);//shift 2 bits due to enable and reset mccu
                          // 0xf ->4cores / 0x3f -> 6cores
}

/*
 *   Function    : pmu_mccu_get_quota_remaining
 *   Description : Get the remaining quota for a single core.
 *   Parameters  : 
 *       - core  : Target core for quota monitoring. Select core number 1, 2, 3 or 4.
 *   Return      : The remaining quota for a selected core.
 */
unsigned int pmu_mccu_get_quota_remaining(unsigned int core) {
    char err_msg[] = "ERR on pmu_mccu_get_quota_remaining <core> parameter out of range";
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_mccu_get_quota_remaining\n");
    #endif
363
    return (_PMU_MCCU_QUOTA[MCCU_N_CORES + core]);
GuillemCabo's avatar
GuillemCabo committed
364
365
366
367
368
369
370
371
372
373
374
}

/*
 *   Function    : pmu_mccu_set_event_weigths
 *   Description : It sets the weigths for a selected input.
 *   Parameters  : 
 *      - input  : A given input from 0 to 7.
 *      - weigth : 8 bits wide for a given input.
 *   
 *   Return      : Unsigned int. 0 no error.
 */
Guillem Cabo's avatar
Guillem Cabo committed
375
unsigned pmu_mccu_set_event_weigths(const unsigned int input,
GuillemCabo's avatar
GuillemCabo committed
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
    const unsigned int weigth) {
    switch (input) {
    case 0:
    case 1:
    case 2:
    case 3:
        EVENT_WEIGHT_REG0 &= ~(0x000000FF << (input << 3));
        EVENT_WEIGHT_REG0 |= (weigth & 0x000000FF) << (input << 3);
        break;

    case 4:
    case 5:
    case 6:
    case 7:
        EVENT_WEIGHT_REG1 &= ~(0x000000FF << (input << 3));
        EVENT_WEIGHT_REG1 |= (weigth & 0x000000FF) << (input << 3);
        break;
    case 8:
    case 9:
    case 10:
    case 11:
        EVENT_WEIGHT_REG2 &= ~(0x000000FF << (input << 3));
        EVENT_WEIGHT_REG2 |= (weigth & 0x000000FF) << (input << 3);
        break;

    default:
        printf("mccu_set_event_weigths: input %d does not exist\n", input);
        return (1);
    }

    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_mccu_set_event_weigths\n");
    printf("EVENT_WEIGHT_REG0 = %u\n", EVENT_WEIGHT_REG0);
    printf("EVENT_WEIGHT_REG1 = %u\n", EVENT_WEIGHT_REG1);
    printf("EVENT_WEIGHT_REG2 = %u\n", EVENT_WEIGHT_REG2);
    #endif
    return (0);
}

415
416
417
418
419
420
421
422
423
void pmu_mccu_enable_HQ(){
    unsigned mask = 1 << 31;
    PMUCFG1 |= mask;
}
void pmu_mccu_disable_HQ(){
    unsigned mask = 1 << 31;
    PMUCFG1 &= ~(mask);
}

GuillemCabo's avatar
GuillemCabo committed
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
/* **********************************
           RDC SUBMODULE
* **********************************/

/*
 *   Function    : pmu_rdc_enable
 *   Description : It enables the RDC submodule.
 *   Parameters  : None.
 *   Return      : None.
 */
void pmu_rdc_enable(void) {
    PMUCFG1 |= 1<<(2+MCCU_N_CORES);
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_rdc_enable\n");
    printf("PMUCFG1 = %d\n", PMUCFG1);
    #endif
}

/*
 *   Function    : pmu_rdc_disable
 *   Description : It disables the RDC disable.
 *   Parameters  : None.
 *   Return      : None.
 */
void pmu_rdc_disable(void) {
    PMUCFG1 &= ~(1<<(2+MCCU_N_CORES));
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_rdc_disable\n");
    printf("PMUCFG1 = %d\n", PMUCFG1);
    #endif
}

/*
 *   Function    : pmu_rdc_reset
 *   Description : It resets the RDC disable.
 *   Parameters  : None.
 *   Return      : None.
 */
void pmu_rdc_reset(void) {
    PMUCFG1 |= 1<<(2+MCCU_N_CORES+1);//2(enable,reset mccu),(ncores) quota updates, 1 (enable RDC)
    PMUCFG1 &= ~(1<<(2+MCCU_N_CORES+1));//2(enable,reset mccu),(ncores) quota updates, 1 (enable RDC)
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_rdc_reset\n");
    printf("PMUCFG1 = %d\n", PMUCFG1);
    #endif
}

/*
 *   Function    : pmu_rdc_read_watermark
 *   Description : It gets the watermarks for a given input.
 *   Parameters  : 
 *       - input : A given input from 0 to 7.
 *   Return      : It return the watermark for a given input.
 */
unsigned int pmu_rdc_read_watermark(unsigned int input) {
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_rdc_read_watermark\n");
    printf("PMU_RDC_WATERMARK_REG0 = 0x%08x\n", PMU_RDC_WATERMARK_REG0);
    printf("PMU_RDC_WATERMARK_REG1 = 0x%08x\n", PMU_RDC_WATERMARK_REG1);
    #endif

    char err_msg[] = "ERR on pmu_rdc_read_watermark. <input> parameter out of range";
    
    unsigned int idx, tmp; 
    idx = input/(REG_WIDTH/MCCU_WEIGHTS_WIDTH);
Guillem Cabo's avatar
Guillem Cabo committed
489
    tmp = (_PMU_RDC_WATERMARKS[idx] & (0x000000FF << (input << 3))) >> (input << 3);
GuillemCabo's avatar
GuillemCabo committed
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
    return (tmp);
}

/*
 *   Function    : pmu_rdc_read_iv
 *   Description : It resets the RDC disable.
 *   Parameters  : None.
 *   Return      : It returns the Interrupt Vector for the RDC.
 */
unsigned int pmu_rdc_read_iv() {
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_rdc_read_iv\n");
    #endif

    return (PMU_RDC_IV);
}

/*
 *   Function    : pmu_rdc_get_interrupt
 *   Description : Get the interrupt for a given core. It interrupts when the 
 *                 quota get to 0. 
 *   Parameters  : 
 *       - core  : Core to monitor the RDC interrupt.
 *   Return      : 
 *       - 1 : The RDC for the given core has interrupted.
 *       - 0 : The RDC for the given core has not interrupted.
 */
unsigned int pmu_rdc_get_interrupt(unsigned int core) {
    #ifdef __PMU_LIB_DEBUG__
    printf("pmu_rdc_get_interrupt\n");
    printf("PMU_RDC_IV = 0x%04x\n", PMU_RDC_IV);
    #endif

    return ((PMU_RDC_IV & (0x00000001 << core)) != 0);
}
Guillem Cabo's avatar
Guillem Cabo committed
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748

/* 
 *  Legacy Functions
 */
void enable_counters (void){
pmu_counters_enable();
}
void disable_counters (void){
pmu_counters_disable();
}
void reset_counters (void){
pmu_counters_reset();
}
void reset_RDC(void){
pmu_rdc_reset(); 
}

// ** write range of address with same value
void write_register_range (unsigned int entry, unsigned int exit, unsigned int value){
    volatile unsigned int *p;
    unsigned int i;
#ifdef __PMU_LIB_DEBUG__
    printf("\n *** Register write***\n\n");
#endif
    for(i=entry;i<=exit;i=i+1){
        p=(unsigned int*)(PMU_ADDR+(i*4));
        *p=value;
#ifdef __PMU_LIB_DEBUG__
        printf("address:%x \n",(PMU_ADDR+(i*4)));
        printf("value :%x \n",value);
#endif
    }
#ifdef __PMU_LIB_DEBUG__
    printf("\n *** end register write ***\n\n");
#endif
}

void zero_all_pmu_regs(void){
    write_register_range (BASE_CFG,END_PMU,0);
}

void enable_RDC (void){
    pmu_rdc_enable();
}
void disable_RDC (void){
    pmu_rdc_disable();
}

struct report_s report_pmu (void){
    volatile unsigned int *var;
    volatile unsigned int reader;
    struct report_s report;
    unsigned int pmu_addr = PMU_ADDR;

    //Counters
    var=(unsigned int*)(pmu_addr+BASE_COUNTERS*4);
    reader=*var;
#ifdef __UART__
    printf("Counters  *******: %d\n",N_COUNTERS);
    printf("SoC events  *******: %d\n",CROSSBAR_INPUTS);
#endif
#ifdef __UART__
    printf("address:%x ,                 Counter0: %10u\n",var,reader);
#endif
    report.ev0 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter1: %10u\n",var,reader);
#endif
    report.ev1 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter2: %10u\n",var,reader);
#endif
    report.ev2 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter3: %10u\n",var,reader);
#endif
    report.ev3 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter4: %10u\n",var,reader);
#endif
    report.ev4 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter5: %10u\n",var,reader);
#endif
    report.ev5 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter6: %10u\n",var,reader);
#endif
    report.ev6 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter7: %10u\n",var,reader);
#endif
    report.ev7 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter8: %10u\n",var,reader);
#endif
    report.ev8 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter9: %10u\n",var,reader);
#endif
    report.ev9 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter10: %10u\n",var,reader);
#endif
    report.ev10 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter11: %10u\n",var,reader);
#endif
    report.ev11 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter12: %10u\n",var,reader);
#endif
    report.ev12 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter13: %10u\n",var,reader);
#endif
    report.ev13 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter14: %10u\n",var,reader);
#endif
    report.ev14 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter15: %10u\n",var,reader);
#endif
    report.ev15 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter16: %10u\n",var,reader);
#endif
    report.ev16 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter17: %10u\n",var,reader);
#endif
    report.ev17 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter18: %10u\n",var,reader);
#endif
    report.ev18 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter19: %10u\n",var,reader);
#endif
    report.ev19 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter20: %10u\n",var,reader);
#endif
    report.ev20 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter21: %10u\n",var,reader);
#endif
    report.ev21 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter22: %10u\n",var,reader);
#endif
    report.ev22 = reader;
    var=(unsigned int*)(var+4);
    reader=*var;
#ifdef __UART__
    printf("address:%x ,                 Counter23: %10u\n",var,reader);
#endif
    report.ev23 = reader;

    //RDC Watermarks
    var=(unsigned int*)(pmu_addr+BASE_RDC_WATERMARK*4);
    reader=*var;
#ifdef __UART__
    printf("address:%x , watermark_0 : %u\n",var,reader & 0x000000ff);
    printf("address:%x , watermark_1 : %u\n",var+1, (reader & 0x0000ff00) >> 8);
    printf("address:%x , watermark_2 : %u\n",var+2, (reader & 0x00ff0000) >> 16);
    printf("address:%x , watermark_3 : %u\n\n",var+3, (reader & 0xff000000) >> 24);
#endif
    var=(unsigned int*)(pmu_addr+(BASE_RDC_WATERMARK+1)*4);
    reader=*var;
#ifdef __UART__
    printf("address:%x , watermark_4 : %u\n",var,reader & 0x000000ff);
    printf("address:%x , watermark_5 : %u\n",var+1, (reader & 0x0000ff00) >> 8);
    printf("address:%x , watermark_6 : %u\n",var+2, (reader & 0x00ff0000) >> 16);
    printf("address:%x , watermark_7 : %u\n\n",var+3, (reader & 0xff000000) >> 24);
#endif
    return(report);
}

Guillem Cabo's avatar
Guillem Cabo committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
void masked_and_write (unsigned int entry, unsigned int mask){
    volatile unsigned int *p;
    volatile unsigned int current_value;
#ifdef __PMU_LIB_DEBUG__
    printf("\n *** Write AND mask register***\n\n");
#endif
    p=(unsigned int*)(PMU_ADDR+(entry*4));
    //get current value 
    current_value=*p;
    //set reset bit
    *p=current_value & mask;
#ifdef __PMU_LIB_DEBUG__
    printf("address:%x \n",(PMU_ADDR+(entry*4)));
    printf("current value :%x \n",current_value);
    printf("mask:%x \n", mask);
    printf("masked value :%x \n",(current_value & mask));
    printf("\n *** end Write AND mask register ***\n\n");
#endif
}

// ** or mask
void masked_or_write (unsigned int entry, unsigned int mask){
    volatile unsigned int *p;
    volatile unsigned int current_value;
#ifdef __PMU_LIB_DEBUG__
    printf("\n *** Write OR mask register***\n\n");
#endif
    p=(unsigned int*)(PMU_ADDR+(entry*4));
    //get current value 
    current_value=*p;
    //set reset bit
    *p=current_value | mask;
#ifdef __PMU_LIB_DEBUG__
    printf("address:%x \n",(PMU_ADDR+(entry*4)));
    printf("current value :%x \n",current_value);
    printf("mask:%x \n", mask);
    printf("masked value :%x \n",(current_value & mask));
    printf("\n *** end Write OR mask register ***\n\n");
#endif
}

//Select test mode
unsigned int select_test_mode (unsigned int mode){
    volatile unsigned int MASK_MODE;
    volatile unsigned int CLEAR_MODE = 0xf0000000;
    switch (mode)
    {
        case 0:
            //pass through
#ifdef __PMU_LIB_DEBUG__
            printf("No selftests, events are passed through\n");
#endif
            MASK_MODE = 0x000000000;
            break;
        case 1:
            //ALL ones
#ifdef __PMU_LIB_DEBUG__
            printf("Selftests, All events set to 1\n");
#endif
            MASK_MODE = 0x40000000;
            break;
        case 2:
            //ALL zeros
#ifdef __PMU_LIB_DEBUG__
            printf("Selftests, All events set to 0\n");
#endif
            MASK_MODE = 0x80000000;
            break;
        case 3:
            //First singnal 1 all the other 0
#ifdef __PMU_LIB_DEBUG__
            printf("Selftests, First event is 1 all the other are 0\n");
#endif
            MASK_MODE = 0xf0000000;
            break;
        default:
#ifdef __PMU_LIB_DEBUG__
            printf("Invalid mode for selftest\n");
#endif
            MASK_MODE = 0x00000000;
            return 1;
    }
    //clear previous mode
    masked_and_write(BASE_CFG,~CLEAR_MODE);
    masked_or_write(BASE_CFG,MASK_MODE);
    return 0;
}