Skip to content

Commit 824136f

Browse files
committed
Allow using ARM cycle counter as timing
Signed-off-by: falkTX <[email protected]>
1 parent e388aa2 commit 824136f

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

common/JackServerGlobals.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,7 @@ bool JackServerGlobals::Init()
204204
if (tolower (optarg[0]) == 'h') {
205205
clock_source = JACK_TIMER_HPET;
206206
} else if (tolower (optarg[0]) == 'c') {
207-
/* For backwards compatibility with scripts, allow
208-
* the user to request the cycle clock on the
209-
* command line, but use the system clock instead
210-
*/
211-
clock_source = JACK_TIMER_SYSTEM_CLOCK;
207+
clock_source = JACK_TIMER_CYCLE_COUNTER;
212208
} else if (tolower (optarg[0]) == 's') {
213209
clock_source = JACK_TIMER_SYSTEM_CLOCK;
214210
} else {

common/JackTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef uint16_t jack_int_t; // Internal type for ports and refnum
3939

4040
typedef enum {
4141
JACK_TIMER_SYSTEM_CLOCK,
42+
JACK_TIMER_CYCLE_COUNTER,
4243
JACK_TIMER_HPET,
4344
} jack_timer_type_t;
4445

common/Jackdmp.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static void usage(FILE* file, jackctl_server_t *server, bool full = true)
210210
" [ --internal-session-file OR -C internal-session-file ]\n"
211211
" [ --verbose OR -v ]\n"
212212
#ifdef __linux__
213-
" [ --clocksource OR -c [ h(pet) | s(ystem) ]\n"
213+
" [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
214214
#endif
215215
" [ --autoconnect OR -a <modechar>]\n");
216216

@@ -354,11 +354,7 @@ int main(int argc, char** argv)
354354
value.ui = JACK_TIMER_HPET;
355355
jackctl_parameter_set_value(param, &value);
356356
} else if (tolower (optarg[0]) == 'c') {
357-
/* For backwards compatibility with scripts, allow
358-
* the user to request the cycle clock on the
359-
* command line, but use the system clock instead
360-
*/
361-
value.ui = JACK_TIMER_SYSTEM_CLOCK;
357+
value.ui = JACK_TIMER_CYCLE_COUNTER;
362358
jackctl_parameter_set_value(param, &value);
363359
} else if (tolower (optarg[0]) == 's') {
364360
value.ui = JACK_TIMER_SYSTEM_CLOCK;

linux/JackLinuxTime.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,34 @@ static uint64_t hpet_wrap;
5959
static hpet_counter_t hpet_previous = 0;
6060
#endif /* defined(__gnu_linux__) && (__i386__ || __x86_64__) */
6161

62+
#if defined(__ARM_ARCH_8A) && !defined(_MOD_DEVICE_DUOX)
63+
static uint64_t _cntfrq_mhz = 0;
64+
static void _cntfrq_mhz_init() {
65+
asm volatile("mrs %0, CNTFRQ_EL0" : "=r"(_cntfrq_mhz));
66+
_cntfrq_mhz /= 1000000;
67+
};
68+
#endif
69+
70+
static jack_time_t jack_get_microseconds_from_cycles (void)
71+
{
72+
#if defined(_MOD_DEVICE_DUO)
73+
uint32_t r;
74+
asm volatile("mrc p15, 0, %0, c9, c13, 0\t\n" : "=r"(r));
75+
return (((uint64_t)r) << 6) / 912; // 912 is the hardcoded MOD Duo CPU frequency
76+
#elif defined(_MOD_DEVICE_DUOX)
77+
uint64_t r;
78+
asm volatile("mrs %0, CNTVCT_EL0" : "=r"(r));
79+
return r * 0.12;
80+
#elif defined(__ARM_ARCH_8A)
81+
uint64_t r;
82+
asm volatile("mrs %0, CNTVCT_EL0" : "=r"(r));
83+
return r / _cntfrq_mhz;
84+
#else
85+
#warning Cycle counter implementation missing
86+
return 0;
87+
#endif
88+
}
89+
6290
#ifdef HPET_SUPPORT
6391

6492
static int jack_hpet_init ()
@@ -175,6 +203,13 @@ void SetClockSource(jack_timer_type_t source)
175203

176204
switch (source)
177205
{
206+
case JACK_TIMER_CYCLE_COUNTER:
207+
#if defined(__ARM_ARCH_8A) && !defined(_MOD_DEVICE_DUOX)
208+
_cntfrq_mhz_init();
209+
#endif
210+
_jack_get_microseconds = jack_get_microseconds_from_cycles;
211+
break;
212+
178213
case JACK_TIMER_HPET:
179214
if (jack_hpet_init () == 0) {
180215
_jack_get_microseconds = jack_get_microseconds_from_hpet;
@@ -193,6 +228,8 @@ void SetClockSource(jack_timer_type_t source)
193228
const char* ClockSourceName(jack_timer_type_t source)
194229
{
195230
switch (source) {
231+
case JACK_TIMER_CYCLE_COUNTER:
232+
return "cycle counter";
196233
case JACK_TIMER_HPET:
197234
return "hpet";
198235
case JACK_TIMER_SYSTEM_CLOCK:

0 commit comments

Comments
 (0)