common_sys_header.inc
15.5 KB
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
/*** DESKTOP ******************************************************************/
#ifdef DESKTOP
#include <stdio.h>
#include <stdint.h>
#include <math.h>
void sysregs_init(void)
{
}
void sysregs_start(void)
{
}
void sysregs_stop()
{
}
void sysregs_read(unsigned *ticks_lo, unsigned *ticks_hi, unsigned *insns)
{
static unsigned ti = 0;
if (ticks_lo) *ticks_lo = ti;
if (ticks_hi) *ticks_hi = 0;
if (insns) *insns = ti;
ti++;
}
/*** RISC-V / NOEL-V **********************************************************/
#elif defined __riscv
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <riscv_math.h>
#include <bcc/bcc.h>
#include <bcc/regs/gptimer.h>
#include <bcc/regs/apbuart.h>
#include <bcc/regs/plic.h>
// system clock in Hz (default = 100MHz)
#ifndef SYSCLK
#warning Default SYSCLK is used.
#define SYSCLK (100000000)
#endif
#ifndef RISCV_TIME_USE_GPTIMER /* can be enabled from an application */
#define RISCV_TIME_USE_GPTIMER 0
#endif /* RISCV_TIME_USE_GPTIMER */
#if !RISCV_TIME_USE_GPTIMER
#warning SYSCLK is used for time measurement
#endif /* !RISCV_TIME_USE_GPTIMER */
#define NL printf("\n")
extern uintptr_t __bcc_con_handle;
extern uintptr_t __bcc_timer_handle;
extern uintptr_t __bcc_clint_handle;
extern uintptr_t __bcc_plic_handle;
#define GPTIMER_CTRL_CH (1<<5)
#define _MAX_GPTIMER_US_RANGE_ (0xffffffff)
static unsigned tblo, tbhi;
//#if !RISCV_TIME_USE_GPTIMER
unsigned long HWcounters[32]; // as in RV spec. 0=cycles, 1=time, 2=instret, 3-31=hpmcounterX
//#define USEDHWCOUNTERS 18
//unsigned HWeventmap[USEDHWCOUNTERS] = {
//32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49
//};
char* labels[32] = {
"FCOMMIT",
"FLD",
"FST",
"FDIV",
"FSQRT",
"FMADD",
"FMUL",
"FADD",
"FMINMAX",
"FSGN",
"FEQ",
"FCMP",
"FCLASS",
"FL2H",
"FH2L",
"FI2F",
"FF2I",
"FX2F",
"FF2X",
"FERR",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
};
unsigned long cycle_start = 0;
unsigned long cycle_stop = 0;
//unsigned long cycle_accum = 0;
//#endif /* RISCV_TIME_USE_GPTIMER */
#define SET_MPMHEVENT( idx, event) do { register unsigned v = event; asm volatile ("csrw mhpmevent" idx ",%0\n" : : "r"(v)); } while(0)
int sysregs_init(void)
{
#if RISCV_TIME_USE_GPTIMER
/* use chained timer with t0 counts ticks to 1ms and t1 counts ms. */
/* timer IRQ is not used */
if (__bcc_timer_handle==0) return 1; /* timer is not found */
volatile struct gptimer_regs *gpt = (volatile struct gptimer_regs *)__bcc_timer_handle;
gpt->cfg = 0;
if ((gpt->cfg & GPTIMER_CFG_TIMERS)<2) return 2; /* it needs 2 timers at least */
/* timer prescaler and reload[0] to count with period 1ms from system frequency */
/* for SYSCLK=100MHz : pre = 100, rel=1000 */
/* =12.5MHz : pre = 4, rel=3125 */
/* =6.25MHz : pre = 2, rel=3125 */
gpt->scaler_value = 0;
gpt->timer[0].counter = 0;
#if SYSCLK==100000000
gpt->scaler_reload = 99; /* Ft=1MHz */
gpt->timer[0].reload = 999; /* t0_per = 1us, t1_per = 1ms */
#elif SYSCLK==12500000
gpt->scaler_reload = 9; /* Ft=1.250MHz */
gpt->timer[0].reload = 1249; /* t0_per = 800ns, t1_per = 1ms */
#elif SYSCLK==6250000
gpt->scaler_reload = 4; /* Ft=1.250MHz */
gpt->timer[0].reload = 1249; /* t0_per = 800ns, t1_per = 1ms */
#else /* SYSCLK */
gpt->scaler_reload = 4;
gpt->timer[0].reload = (SYSCLK/200)-1; /* for Ntim=2 minimal prescaler is 3 */
#endif /* SYSCLK */
gpt->timer[1].counter = _MAX_GPTIMER_US_RANGE_;
gpt->timer[1].reload = _MAX_GPTIMER_US_RANGE_;
gpt->timer[1].ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_LD | GPTIMER_CTRL_RS | GPTIMER_CTRL_CH;
gpt->timer[0].ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_LD | GPTIMER_CTRL_RS;
tblo = gpt->timer[0].reload;
tbhi = gpt->timer[1].reload;
// initiate UART
#else /* RISCV_TIME_USE_GPTIMER */
// initiate, map and halt required HW counters
register unsigned val = 0; //0xffffffff;
// enable access and halt all counters
// asm volatile (
// "csrw mcounteren,%0\n" : : "r"(val)
// );
// val=0;
asm volatile (
"csrw mcountinhibit,%0\n" : : "r"(val)
);
// map events to counters
SET_MPMHEVENT("3", 22);
SET_MPMHEVENT("4", 32);
SET_MPMHEVENT("5", 33);
SET_MPMHEVENT("6", 34);
SET_MPMHEVENT("7", 35);
SET_MPMHEVENT("8", 36);
SET_MPMHEVENT("9", 37);
SET_MPMHEVENT("10", 38);
SET_MPMHEVENT("11", 39);
SET_MPMHEVENT("12", 40);
SET_MPMHEVENT("13", 41);
SET_MPMHEVENT("14", 42);
SET_MPMHEVENT("15", 43);
SET_MPMHEVENT("16", 44);
SET_MPMHEVENT("17", 45);
SET_MPMHEVENT("18", 46);
SET_MPMHEVENT("19", 47);
SET_MPMHEVENT("20", 48);
SET_MPMHEVENT("21", 49);
SET_MPMHEVENT("22", 50);
// SET_MPMHEVENT("23", 51);
#endif /* RISCV_TIME_USE_GPTIMER */
return 0;
}
void sysregs_start(void)
{
#if RISCV_TIME_USE_GPTIMER
volatile struct gptimer_regs *gpt = (volatile struct gptimer_regs *)__bcc_timer_handle;
gpt->timer[0].ctrl = gpt->timer[0].ctrl | GPTIMER_CTRL_EN;
#else /* RISCV_TIME_USE_GPTIMER */
/* start all counters */
asm volatile ( "csrr %0,mcycle" : "=r"(cycle_start) );
unsigned val = 0;
asm volatile ( "csrw mcountinhibit,%0" : : "r"(val) );
#endif /* RISCV_TIME_USE_GPTIMER */
}
void sysregs_stop()
{
#if RISCV_TIME_USE_GPTIMER
volatile struct gptimer_regs *gpt = (volatile struct gptimer_regs *)__bcc_timer_handle;
gpt->timer[0].ctrl = gpt->timer[0].ctrl & ~GPTIMER_CTRL_EN;
#else /* RISCV_TIME_USE_GPTIMER */
/* stop all counters (time cannot be stopped) */
unsigned val = 0xffffffff;
asm volatile (
"csrw mcountinhibit,%0" : : "r"(val)
);
asm volatile ( "csrr %0,mcycle" : "=r"(cycle_stop) );
#endif /* RISCV_TIME_USE_GPTIMER */
}
void sysregs_read(unsigned *ticks_lo, unsigned *ticks_hi, unsigned *insnsl, unsigned *insnsh)
{
unsigned long c;
#if RISCV_TIME_USE_GPTIMER
volatile struct gptimer_regs *gpt = (volatile struct gptimer_regs *)__bcc_timer_handle;
//if (ticks_lo) *ticks_lo = tblo - gpt->timer[0].counter;
if (ticks_lo) {
uint32_t t = tblo - gpt->timer[0].counter;
*ticks_lo = (t*1000)/(tblo+1);
}
if (ticks_hi) *ticks_hi = tbhi - gpt->timer[1].counter;
#else
asm volatile (
"csrr %0, mcycle" : "=r"(c)
);
if (ticks_hi) *ticks_hi = (uint32_t)(c>>32);
if (ticks_lo) *ticks_lo = (uint32_t)c;
#endif
asm volatile (
"csrr %0, minstret" : "=r"(c)
);
if (insnsh) *insnsh = (uint32_t)(c>>32);
if (insnsl) *insnsl = (uint32_t)c;
}
void sysregs_read_hwticks(unsigned *tlo, unsigned *thi)
{
if (__bcc_clint_handle) {
volatile uint32_t *pclint = (volatile uint32_t *)__bcc_clint_handle;
if (tlo) *tlo = pclint[0x2FFE];
if (thi) *thi = pclint[0x2FFF];
} else {
if (tlo) *tlo = 0;
if (thi) *thi = 0;
}
}
void sysregs_read_ext(unsigned *ticks_lo, unsigned *ticks_hi, unsigned *insns_lo, unsigned *insns_hi, unsigned *fpop_lo, unsigned *fpop_hi, unsigned *fpld_lo, unsigned *fpld_hi, unsigned *fpst_lo, unsigned *fpst_hi)
{
unsigned long c;
#if !RISCV_TIME_USE_GPTIMER
asm volatile ( "csrr %0, mhpmcounter3" : "=r"(HWcounters[0]) ); // 22 FPEVT_COMMIT , -- Commit operation
asm volatile ( "csrr %0, mhpmcounter4" : "=r"(HWcounters[1]) ); // 32 FPEVT_LOAD , -- load
asm volatile ( "csrr %0, mhpmcounter5" : "=r"(HWcounters[2]) ); // 33 FPEVT_STORE , -- store
asm volatile ( "csrr %0, mhpmcounter6" : "=r"(HWcounters[3]) ); // 34 FPEVT_DIV , -- div
asm volatile ( "csrr %0, mhpmcounter7" : "=r"(HWcounters[4]) ); // 35 FPEVT_SQRT , -- sqrt
asm volatile ( "csrr %0, mhpmcounter8" : "=r"(HWcounters[5]) ); // 36 FPEVT_MADD , -- madd / msub / nmsub / nmadd
asm volatile ( "csrr %0, mhpmcounter9" : "=r"(HWcounters[6]) ); // 37 FPEVT_MUL , -- mul
asm volatile ( "csrr %0, mhpmcounter10" : "=r"(HWcounters[7]) ); // 38 FPEVT_ADD , -- add / sub
asm volatile ( "csrr %0, mhpmcounter11" : "=r"(HWcounters[8]) ); // 39 FPEVT_MINMAX , -- min / max
asm volatile ( "csrr %0, mhpmcounter12" : "=r"(HWcounters[9]) ); // 40 FPEVT_SGN , -- sgn
asm volatile ( "csrr %0, mhpmcounter13" : "=r"(HWcounters[10]) ); // 41 FPEVT_EQ , -- eq
asm volatile ( "csrr %0, mhpmcounter14" : "=r"(HWcounters[11]) ); // 42 FPEVT_CMP , -- lt le
asm volatile ( "csrr %0, mhpmcounter15" : "=r"(HWcounters[12]) ); // 43 FPEVT_CLASS , -- class
asm volatile ( "csrr %0, mhpmcounter16" : "=r"(HWcounters[13]) ); // 44 FPEVT_S2D , -- s->d
asm volatile ( "csrr %0, mhpmcounter17" : "=r"(HWcounters[14]) ); // 45 FPEVT_D2S , -- d->s
asm volatile ( "csrr %0, mhpmcounter18" : "=r"(HWcounters[15]) ); // 46 FPEVT_I2F , -- i->f
asm volatile ( "csrr %0, mhpmcounter19" : "=r"(HWcounters[16]) ); // 47 FPEVT_F2I , -- f->i
asm volatile ( "csrr %0, mhpmcounter20" : "=r"(HWcounters[17]) ); // 48 FPEVT_X2F , -- x->f
asm volatile ( "csrr %0, mhpmcounter21" : "=r"(HWcounters[18]) ); // 49 FPEVT_F2X , -- f->x
asm volatile ( "csrr %0, mhpmcounter22" : "=r"(HWcounters[19]) ); // 50 FPEVT_UNKNOWN , -- Should never happen!
// asm volatile ( "csrr %0, mhpmcounter23" : "=r"(HWcounters[20]) ); // 51 FPEVT_EVENTS -- Only for fpevt_t type!
if (fpld_hi) *fpld_hi = (uint32_t)(HWcounters[1]>>32);
if (fpld_lo) *fpld_lo = (uint32_t)HWcounters[1];
if (fpst_hi) *fpst_hi = (uint32_t)(HWcounters[2]>>32);
if (fpst_lo) *fpst_lo = (uint32_t)HWcounters[2];
c = HWcounters[0] - HWcounters[1] - HWcounters[2];
if (fpop_hi) *fpop_hi = (uint32_t)(c>>32);
if (fpop_lo) *fpop_lo = (uint32_t)c;
asm volatile ( "csrr %0, mcycle" : "=r"(c) );
if (ticks_hi) *ticks_hi = (uint32_t)(c>>32);
if (ticks_lo) *ticks_lo = (uint32_t)c;
asm volatile ( "csrr %0, minstret" : "=r"(c) );
if (insns_hi) *insns_hi = (uint32_t)(c>>32);
if (insns_lo) *insns_lo = (uint32_t)c;
#if 1
for(c=0;c<20;++c) {
printf(" - cnt #%02lu %-7s = %lu\n", c+3, labels[c], HWcounters[c]);
}
#endif
#endif
}
/*** LEON *********************************************************************/
#else
#include <stdint.h>
#include <leon-myuart.h>
#include <my_printf.h>
#include <sfmath.h>
#include <extmath.h>
#include <lconf.h>
#if ((USEDBSP==BSP_AT697) || (USEDBSP==BSP_GR712RC) || (USEDBSP==BSP_LEON4_N2X))
/* for undefined system clock we use 100MHz */
#if !defined(SYSCLK_PERIOD)
#error SYSCLK_PERIOD is not defined
#endif
void sysregs_init(void)
{
init_timer();
timer_start();
}
void sysregs_start(void)
{
}
void sysregs_stop()
{
}
void sysregs_read(unsigned *ticks_lo, unsigned *ticks_hi, unsigned *ins_lo, unsigned *ins_hi)
{
static unsigned ti = 0;
if (ticks_lo) *ticks_lo = get_time(ticks_hi); // s/us
if (ins_lo) *ins_lo = ti;
if (ins_hi) *ins_hi = 0;
ti++;
}
#elif (USEDBSP==BSP_GR740)
#include <sysregs_gr740.h>
void sysregs_init(void)
{
sysregs740_init();
}
void sysregs_start(void)
{
sysregs740_start();
}
void sysregs_stop()
{
sysregs740_stop();
}
void sysregs_read(unsigned *ticks_lo, unsigned *ticks_hi, unsigned *ins_lo, unsigned *ins_hi)
{
sysregs740_read(ticks_lo, ticks_hi, ins_lo, ins_hi);
}
#else
#include <sysregs.h>
#endif
#endif
#if !defined(DESKTOP) && !defined(__riscv)
#define system_init() \
do { \
/* for clearing BSS section */ \
extern unsigned long __bss_start; /* start address for the .bss section. defined in linker script */ \
extern unsigned long _end; /* end address for the .bss section. defined in linker script */ \
/* reset BSS section to zero */ \
for(unsigned long *pMem = &__bss_start; pMem < &_end;) { \
*(pMem++) = 0; \
} \
init_uart(); \
init_softfloat(); \
sysregs_init(); \
} while(0)
#define system_done() asm("ta 1; nop; nop")
#else /* RISC-V */
#define system_init() do { \
sysregs_init(); \
} while(0)
#define system_done() do { \
unsigned ihi, ilo; \
sysregs_read(NULL, NULL, &ilo, &ihi); \
printf("Instructions: %u,%u\n", ihi, ilo); \
__asm__ volatile ( ".option norvc\n slli x0, x0, 0x1f\n ebreak\n srai x0, x0, 7\n"); \
} while(0)
#endif
uint32_t get_usec(void)
{
uint32_t tlo, thi, inslo, inshi, t;
// sysregs_stop();
sysregs_read(&tlo, &thi, &inslo, &inshi);
#ifdef __riscv
#if RISCV_TIME_USE_GPTIMER
t = thi*1000+tlo; // sysregs_read returns [thi,tlo]=t(ms,us)
#else /* use measured cycles to compute time */
unsigned long cyc64 = (((unsigned long)thi)<<32) | tlo;
t = cyc64/(SYSCLK/1000000);
#endif
#elif ((USEDBSP==BSP_AT697) || (USEDBSP==BSP_GR712RC) || (USEDBSP==BSP_LEON4_N2X))
t = thi*1000000+(tlo);
#else
if (thi)
t = ((thi*4294967)/(SYSCLK_PERIOD/1000))*1000;
else
t = 0;
t += tlo/(SYSCLK_PERIOD/1000);
t*=1000; /* TODO !!! */
#endif
// sysregs_start();
return t;
}
uint32_t get_msec(void)
{
uint32_t tlo, thi, inslo, inshi, t;
#ifdef __riscv
#if RISCV_TIME_USE_GPTIMER
volatile struct gptimer_regs *gpt = (volatile struct gptimer_regs *)__bcc_timer_handle;
t = gpt->timer[1].reload-gpt->timer[1].counter; /* RTZ */
#else
sysregs_read(&tlo, &thi, &inslo, &inshi);
unsigned long cyc64 = (((unsigned long)thi)<<32) | tlo;
t = cyc64/(SYSCLK/1000);
#endif
#elif ((USEDBSP==BSP_AT697) || (USEDBSP==BSP_GR712RC) || (USEDBSP==BSP_LEON4_N2X))
sysregs_stop();
sysregs_read(&tlo, &thi, &inslo, &inshi);
t = thi*1000+(tlo/1000);
sysregs_start();
#else
sysregs_stop();
sysregs_read(&tlo, &thi, &inslo, &inshi);
if (thi)
t = ((thi*4294967)/(SYSCLK_PERIOD/1000))*1000;
else
t = 0;
t += tlo/(SYSCLK_PERIOD/1000);
sysregs_start();
#endif
return t;
}
#if __riscv
// return [ms] and [us] through a pointer
uint32_t get_time(uint32_t *pus)
{
#if RISCV_TIME_USE_GPTIMER
uint32_t usmax, us, us2, msmax, ms;
volatile struct gptimer_regs *gpt = (volatile struct gptimer_regs *)__bcc_timer_handle;
usmax = gpt->timer[0].reload;
msmax = gpt->timer[1].reload;
us = gpt->timer[0].counter;
ms = gpt->timer[1].counter;
us2 = gpt->timer[0].counter;
if (us2>us) { // avoid to underflowing
ms = gpt->timer[1].counter;
}
ms = msmax - ms;
us = ((usmax - us2)*1000)/(usmax+1);
if (pus) *pus = us;
return ms;
#else /* RISCV_TIME_USE_GPTIMER */
uint32_t tlo, thi, inslo, inshi, t;
sysregs_read(&tlo, &thi, &inslo, &inshi);
unsigned long cyc64 = (((unsigned long)thi)<<32) | tlo;
t = cyc64/(SYSCLK/1000000);
uint32_t tms = t/1000; t %= 1000;
if (pus) *pus = t;
return tms;
#endif /* RISCV_TIME_USE_GPTIMER */
}
#endif