target/xtensa: refactor timers
cover target/xtensa: refactor timers
Hello,
this series reimplements xtensa CCOUNT/CCOMPARE features using QEMU timers, enables support for running with -icount option and updates timer tests.
Max Filippov (7):
target/xtensa: refactor CCOUNT/CCOMPARE target/xtensa: support icount
target/xtensa: don't continue translation after exception target/xtensa: tests: run tests with icount target/xtensa: tests: fix timer tests target/xtensa: tests: replace hardcoded interrupt masks target/xtensa: tests: add ccount write tests
hw/xtensa/pic_cpu.c | 75 +++----------- target/xtensa/cpu.h | 21 +++- target/xtensa/helper.h | 5 +- target/xtensa/op_helper.c | 37 +++++-- target/xtensa/translate.c | 225 ++++++++++++++++++++++++++++-------------- tests/tcg/xtensa/Makefile | 2 +- tests/tcg/xtensa/test_timer.S | 105 +++++++++++++------- 7 files changed, 284 insertions(+), 186 deletions(-)
Signature · 1 line
2.1.4
PATCH 1/7 target/xtensa: refactor CCOUNT/CCOMPARE
Xtensa cores may have a register (CCOUNT) that counts core clock cycles. It may also have a number of registers (CCOMPAREx); when CCOUNT value passes the value of CCOMPAREx, timer interrupt x is raised.
Currently xtensa target counts a number of completed instructions and assumes that for CCOUNT one instruction takes one cycle to complete. It calls helper function to update CCOUNT register at every TB end and raise timer interrupts. This scheme works very predictably and doesn't have noticeable performance impact, but it is hard to use with multiple synchronized processors, especially with coming MTTCG.
Derive CCOUNT from the virtual simulation time, QEMU_CLOCK_VIRTUAL. Use native QEMU timers for CCOMPARE timers, one timer for each register.
--- hw/xtensa/pic_cpu.c | 75 +++++++++-------------------------------------- target/xtensa/cpu.h | 16 ++++++---- target/xtensa/helper.h | 5 ++-- target/xtensa/op_helper.c | 33 ++++++++++++++++----- target/xtensa/translate.c | 43 ++++++++------------------- 5 files changed, 65 insertions(+), 107 deletions(-)
hw/xtensa/pic_cpu.c +14 -61
@@ -31,22 +31,6 @@#include "qemu/log.h" #include "qemu/timer.h" -void xtensa_advance_ccount (CPUXtensaState *env,uint32_t d) -{ -uint32_t old_ccount = env->sregs[CCOUNT ] +1 ; - - env->sregs[CCOUNT ] += d; - -if (xtensa_option_enabled (env->config,XTENSA_OPTION_TIMER_INTERRUPT )) { -int i; -for (i =0 ; i < env->config->nccompare; ++i) { -if (env->sregs[CCOMPARE + i] - old_ccount < d) { -xtensa_timer_irq (env, i,1 ); - } - } - } -} -void check_interrupts (CPUXtensaState *env) {CPUState *cs =CPU (xtensa_env_get_cpu (env));
@@ -54,17 +38,6 @@ void check_interrupts(CPUXtensaState *env)uint32_t int_set_enabled = env->sregs[INTSET ] & env->sregs[INTENABLE ];int level; -/* If the CPU is halted advance CCOUNT according to the QEMU_CLOCK_VIRTUAL time -* elapsed since the moment when it was advanced last time. -*/ -if (cs->halted) { -int64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); - -xtensa_advance_ccount (env, -muldiv64 (now - env->halt_clock, - env->config->clock_freq_khz,1000000 )); - env->halt_clock = now; - }for (level = env->config->nlevel; level > minlevel; --level) {if (env->config->level_mask[level] & int_set_enabled) { env->pending_irq_level = level;
@@ -109,49 +82,29 @@ void xtensa_timer_irq(CPUXtensaState *env, uint32_t id, uint32_t active)qemu_set_irq (env->irq_inputs[env->config->timerint[id]], active); } -void xtensa_rearm_ccompare_timer (CPUXtensaState *env) -{ -int i; -uint32_t wake_ccount = env->sregs[CCOUNT ] -1 ; - -for (i =0 ; i < env->config->nccompare; ++i) { -if (env->sregs[CCOMPARE + i] - env->sregs[CCOUNT ] < - wake_ccount - env->sregs[CCOUNT ]) { - wake_ccount = env->sregs[CCOMPARE + i]; - } - } - env->wake_ccount = wake_ccount; -timer_mod (env->ccompare_timer, env->halt_clock + - (uint64_t )(wake_ccount - env->sregs[CCOUNT ]) * -1000000 / env->config->clock_freq_khz); -} -static void xtensa_ccompare_cb (void *opaque) { -XtensaCPU *cpu = opaque; -CPUXtensaState *env = &cpu->env; -CPUState *cs =CPU (cpu); +XtensaCcompareTimer *ccompare = opaque; +CPUXtensaState *env = ccompare->env; +unsigned i = ccompare - env->ccompare; -if (cs->halted) { - env->halt_clock =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); -xtensa_advance_ccount (env, env->wake_ccount - env->sregs[CCOUNT ]); -if (!cpu_has_work (cs)) { - env->sregs[CCOUNT ] = env->wake_ccount +1 ; -xtensa_rearm_ccompare_timer (env); - } - } +xtensa_timer_irq (env, i,1 ); }void xtensa_irq_init (CPUXtensaState *env) { -XtensaCPU *cpu =xtensa_env_get_cpu (env); - env->irq_inputs = (void **)qemu_allocate_irqs ( xtensa_set_irq, env, env->config->ninterrupt); -if (xtensa_option_enabled (env->config,XTENSA_OPTION_TIMER_INTERRUPT ) && - env->config->nccompare >0 ) { - env->ccompare_timer = -timer_new_ns (QEMU_CLOCK_VIRTUAL , &xtensa_ccompare_cb, cpu); +if (xtensa_option_enabled (env->config,XTENSA_OPTION_TIMER_INTERRUPT )) { +unsigned i; + + env->time_base =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + env->ccount_base = env->sregs[CCOUNT ]; +for (i =0 ; i < env->config->nccompare; ++i) { + env->ccompare[i].env = env; + env->ccompare[i].timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , + xtensa_ccompare_cb, env->ccompare + i); + } } }
target/xtensa/cpu.h +11 -5
@@ -269,6 +269,8 @@ typedef enum {INTTYPE_MAX } interrupt_type; +struct CPUXtensaState ; + typedef struct xtensa_tlb_entry { uint32_t vaddr; uint32_t paddr;
@@ -298,6 +300,11 @@ typedef struct XtensaGdbRegmap {XtensaGdbReg reg[1 +16 +64 +256 +256 ]; } XtensaGdbRegmap; +typedef struct XtensaCcompareTimer { +struct CPUXtensaState *env; +QEMUTimer *timer; +}XtensaCcompareTimer ; +struct XtensaConfig {const char *name;uint64_t options;
@@ -369,9 +376,10 @@ typedef struct CPUXtensaState {bool runstall;int pending_irq_level;/* level of last raised IRQ */ void **irq_inputs; -QEMUTimer *ccompare_timer; -uint32_t wake_ccount; -int64_t halt_clock; +XtensaCcompareTimer ccompare[MAX_NCCOMPARE ]; +uint64_t time_base; +uint64_t ccount_time; +uint32_t ccount_base;int exception_taken;unsigned static_vectors;
@@ -439,9 +447,7 @@ void xtensa_register_core(XtensaConfigList *node);void check_interrupts (CPUXtensaState *s);void xtensa_irq_init (CPUXtensaState *env);void *xtensa_get_extint (CPUXtensaState *env,unsigned extint); -void xtensa_advance_ccount (CPUXtensaState *env,uint32_t d);void xtensa_timer_irq (CPUXtensaState *env,uint32_t id,uint32_t active); -void xtensa_rearm_ccompare_timer (CPUXtensaState *env);int cpu_xtensa_signal_handler (int host_signum,void *pinfo,void *puc);void xtensa_cpu_list (FILE *f,fprintf_function cpu_fprintf);void xtensa_sync_window_from_phys (CPUXtensaState *env);
target/xtensa/helper.h +3 -2
@@ -18,8 +18,9 @@ DEF_HELPER_1(simcall, void, env) DEF_HELPER_1(dump_state, void, env) DEF_HELPER_3(waiti, void, env, i32, i32) -DEF_HELPER_3(timer_irq, void, env, i32, i32) -DEF_HELPER_2(advance_ccount, void, env, i32) +DEF_HELPER_1(update_ccount, void, env) +DEF_HELPER_2(wsr_ccount, void, env, i32) +DEF_HELPER_2(update_ccompare, void, env, i32) DEF_HELPER_1(check_interrupts, void, env)DEF_HELPER_3 (check_atomctl, void, env, i32, i32)
target/xtensa/op_helper.c +25 -8
@@ -398,22 +398,39 @@ void HELPER(waiti)(CPUXtensaState *env, uint32_t pc, uint32_t intlevel) } cpu =CPU (xtensa_env_get_cpu (env)); - env->halt_clock =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); cpu->halted =1 ; -if (xtensa_option_enabled (env->config,XTENSA_OPTION_TIMER_INTERRUPT )) { -xtensa_rearm_ccompare_timer (env); - }HELPER (exception)(env,EXCP_HLT ); } -void HELPER (timer_irq )(CPUXtensaState *env,uint32_t id,uint32_t active) +void HELPER (update_ccount )(CPUXtensaState *env) +{ +uint64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + + env->ccount_time = now; + env->sregs[CCOUNT ] = env->ccount_base + + (uint32_t )((now - env->time_base) * + env->config->clock_freq_khz /1000000 ); +} + +void HELPER (wsr_ccount )(CPUXtensaState *env,uint32_t v) { -xtensa_timer_irq (env, id, active); +int i; + +HELPER (update_ccount)(env); + env->ccount_base += v - env->sregs[CCOUNT ]; +for (i =0 ; i < env->config->nccompare; ++i) { +HELPER (update_ccompare)(env, i); + } } -void HELPER (advance_ccount )(CPUXtensaState *env,uint32_t d) +void HELPER (update_ccompare )(CPUXtensaState *env,uint32_t i) { -xtensa_advance_ccount (env, d); +uint64_t dcc; + +HELPER (update_ccount)(env); + dcc = (uint64_t )(env->sregs[CCOMPARE + i] - env->sregs[CCOUNT ] -1 ) +1 ; +timer_mod (env->ccompare[i].timer, + env->ccount_time + (dcc *1000000 ) / env->config->clock_freq_khz); }void HELPER (check_interrupts )(CPUXtensaState *env)
target/xtensa/translate.c +12 -31
180 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH 2/7 target/xtensa: support icount
In reply to Max FilippovDelimit each instruction that may access timers or IRQ state with qemu_io_start/qemu_io_end, so that qemu-system-xtensa could be run with -icount option.
Raise EXCP_YIELD after CCOMPARE reprogramming to let tcg_cpu_exec recalculate how long this CPU is allowed to run.
RSR now may need to terminate TB, but it can't be done in RSR handler because the same handler is used for XSR together with WSR handler, which may also need to terminate TB. Change RSR and WSR handlers return type to bool indicating whether TB termination is needed (RSR) or has been done (WSR), and add TB termination after RSR/WSR dispatcher call.
--- target/xtensa/cpu.h | 5 ++ target/xtensa/op_helper.c | 4 ++ target/xtensa/translate.c | 179 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 143 insertions(+), 45 deletions(-)
target/xtensa/cpu.h +5 -0
@@ -382,6 +382,7 @@ typedef struct CPUXtensaState {uint32_t ccount_base;int exception_taken; +int yield_needed;unsigned static_vectors;/* Watchpoints for DBREAK registers */
@@ -554,6 +555,7 @@ static inline int cpu_mmu_index(CPUXtensaState *env, bool ifetch)#define XTENSA_TBFLAG_EXCEPTION 0x4000#define XTENSA_TBFLAG_WINDOW_MASK 0x18000#define XTENSA_TBFLAG_WINDOW_SHIFT 15 +#define XTENSA_TBFLAG_YIELD 0x20000static inline void cpu_get_tb_cpu_state (CPUXtensaState *env,target_ulong *pc,target_ulong *cs_base,uint32_t *flags)
@@ -595,6 +597,9 @@ static inline void cpu_get_tb_cpu_state(CPUXtensaState *env, target_ulong *pc, }else { *flags |=3 <<XTENSA_TBFLAG_WINDOW_SHIFT ; } +if (env->yield_needed) { + *flags |=XTENSA_TBFLAG_YIELD ; + } }#include "exec/cpu-all.h"
target/xtensa/op_helper.c +4 -0
@@ -105,6 +105,9 @@ void HELPER(exception)(CPUXtensaState *env, uint32_t excp)CPUState *cs =CPU (xtensa_env_get_cpu (env)); cs->exception_index = excp; +if (excp ==EXCP_YIELD ) { + env->yield_needed =0 ; + }if (excp ==EXCP_DEBUG ) { env->exception_taken =0 ; }
@@ -431,6 +434,7 @@ void HELPER(update_ccompare)(CPUXtensaState *env, uint32_t i) dcc = (uint64_t )(env->sregs[CCOMPARE + i] - env->sregs[CCOUNT ] -1 ) +1 ;timer_mod (env->ccompare[i].timer, env->ccount_time + (dcc *1000000 ) / env->config->clock_freq_khz); + env->yield_needed =1 ; }void HELPER (check_interrupts )(CPUXtensaState *env)
target/xtensa/translate.c +134 -45
435 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH 3/7 target/xtensa: don't continue translation after exception
In reply to Max FilippovThere's no point in continuing translating guest instructions once an unconditional exception is thrown.
There's also no point in updating pc before any instruction is translated, don't do it.
--- target/xtensa/translate.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
target/xtensa/translate.c +4 -1
@@ -3152,8 +3152,11 @@ void gen_intermediate_code(CPUXtensaState *env, TranslationBlock *tb) goto done; }if (tb->flags &XTENSA_TBFLAG_EXCEPTION ) { -tcg_gen_movi_i32 (cpu_pc, dc.pc); +tcg_gen_insn_start (dc.pc); + ++insn_count;gen_exception (&dc,EXCP_DEBUG ); + dc.is_jmp =DISAS_UPDATE ; + goto done; } do {
Signature · 1 line
2.1.4
PATCH 4/7 target/xtensa: tests: run tests with icount
In reply to Max FilippovTimer tests expect certain determinism in CCOUNT updates and timer interrupts firing. Run QEMU with -icount to get deterministic results.
--- tests/tcg/xtensa/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/xtensa/Makefile +1 -1
@@ -5,7 +5,7 @@ CROSS=xtensa-$(CORE)-elf- ifndef XT SIM = ../../../xtensa-softmmu/qemu-system-xtensa -SIMFLAGS = -M sim -cpu $(CORE) -nographic -semihosting $(EXTFLAGS) -kernel +SIMFLAGS = -M sim -cpu $(CORE) -nographic -semihosting -icount 7 $(EXTFLAGS) -kernel SIMDEBUG = -s -S else SIM = xt-run
Signature · 1 line
2.1.4
PATCH 5/7 target/xtensa: tests: fix timer tests
In reply to Max FilippovDon't expect that CCOUNT increments are equal to the number of executed instructions. Verify that timer interrupt does not fire before the programmed CCOMPARE value and does fire after.
--- tests/tcg/xtensa/test_timer.S | 61 +++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 28 deletions(-)
tests/tcg/xtensa/test_timer.S +33 -28
@@ -1,12 +1,22 @@ #include "macros.inc" +#define CCOUNT_SHIFT 4 +#define WAIT_LOOPS 20 + +.macro make_ccount_delta target, delta + rsr \delta, ccount + rsr \target, ccount + sub \delta, \target, \delta + slli \delta, \delta, CCOUNT_SHIFT + add \target, \target, \delta +.endm + test_suite timer test ccount rsr a3, ccount rsr a4, ccount - sub a3, a4, a3 - assert eqi, a3, 1 + assert ne, a3, a4 test_end test ccompare
@@ -18,18 +28,18 @@ test ccompare wsr a2, ccompare1 wsr a2, ccompare2 - movi a3, 20 - rsr a2, ccount - addi a2, a2, 20 + make_ccount_delta a2, a15 wsr a2, ccompare0 - rsr a2, interrupt - assert eqi, a2, 0 - loop a3, 1f - rsr a3, interrupt - bnez a3, 2f 1: - test_fail + rsr a3, interrupt + rsr a4, ccount + rsr a5, interrupt + sub a4, a4, a2 + bgez a4, 2f + assert eqi, a3, 0 + j 1b 2: + assert nei, a5, 0 test_end test ccompare0_interrupt
@@ -42,9 +52,8 @@ test ccompare0_interrupt wsr a2, ccompare1 wsr a2, ccompare2 - movi a3, 20 - rsr a2, ccount - addi a2, a2, 20 + movi a3, WAIT_LOOPS + make_ccount_delta a2, a15 wsr a2, ccompare0 rsync rsr a2, interrupt
@@ -72,9 +81,8 @@ test ccompare1_interrupt wsr a2, ccompare0 wsr a2, ccompare2 - movi a3, 20 - rsr a2, ccount - addi a2, a2, 20 + movi a3, WAIT_LOOPS + make_ccount_delta a2, a15 wsr a2, ccompare1 rsync rsr a2, interrupt
@@ -99,9 +107,8 @@ test ccompare2_interrupt wsr a2, ccompare0 wsr a2, ccompare1 - movi a3, 20 - rsr a2, ccount - addi a2, a2, 20 + movi a3, WAIT_LOOPS + make_ccount_delta a2, a15 wsr a2, ccompare2 rsync rsr a2, interrupt
@@ -125,11 +132,10 @@ test ccompare_interrupt_masked movi a2, 0 wsr a2, ccompare2 - movi a3, 40 - rsr a2, ccount - addi a2, a2, 20 + movi a3, 2 * WAIT_LOOPS + make_ccount_delta a2, a15 wsr a2, ccompare1 - addi a2, a2, 20 + add a2, a2, a15 wsr a2, ccompare0 rsync rsr a2, interrupt
@@ -156,11 +162,10 @@ test ccompare_interrupt_masked_waiti movi a2, 0 wsr a2, ccompare2 - movi a3, 40 - rsr a2, ccount - addi a2, a2, 20 + movi a3, 2 * WAIT_LOOPS + make_ccount_delta a2, a15 wsr a2, ccompare1 - addi a2, a2, 20 + add a2, a2, a15 wsr a2, ccompare0 rsync rsr a2, interrupt
Signature · 1 line
2.1.4
PATCH 6/7 target/xtensa: tests: replace hardcoded interrupt masks
In reply to Max Filippov--- tests/tcg/xtensa/test_timer.S | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tests/tcg/xtensa/test_timer.S +5 -5
@@ -59,7 +59,7 @@ test ccompare0_interrupt rsr a2, interrupt assert eqi, a2, 0 - movi a2, 0x40 + movi a2, 1 << XCHAL_TIMER0_INTERRUPT wsr a2, intenable rsil a2, 0 loop a3, 1f
@@ -87,7 +87,7 @@ test ccompare1_interrupt rsync rsr a2, interrupt assert eqi, a2, 0 - movi a2, 0x400 + movi a2, 1 << XCHAL_TIMER1_INTERRUPT wsr a2, intenable rsil a2, 2 loop a3, 1f
@@ -113,7 +113,7 @@ test ccompare2_interrupt rsync rsr a2, interrupt assert eqi, a2, 0 - movi a2, 0x2000 + movi a2, 1 << XCHAL_TIMER2_INTERRUPT wsr a2, intenable rsil a2, 4 loop a3, 1f
@@ -141,7 +141,7 @@ test ccompare_interrupt_masked rsr a2, interrupt assert eqi, a2, 0 - movi a2, 0x40 + movi a2, 1 << XCHAL_TIMER0_INTERRUPT wsr a2, intenable rsil a2, 0 loop a3, 1f
@@ -171,7 +171,7 @@ test ccompare_interrupt_masked_waiti rsr a2, interrupt assert eqi, a2, 0 - movi a2, 0x40 + movi a2, 1 << XCHAL_TIMER0_INTERRUPT wsr a2, intenable waiti 0 test_fail
Signature · 1 line
2.1.4
PATCH 7/7 target/xtensa: tests: add ccount write tests
In reply to Max FilippovCheck that CCOUNT SR is writable and that CCOMPARE timers are updated when CCOUNT is written to.
--- tests/tcg/xtensa/test_timer.S | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
tests/tcg/xtensa/test_timer.S +34 -0
@@ -19,6 +19,40 @@ test ccount assert ne, a3, a4 test_end +test ccount_write + rsr a3, ccount + rsr a4, ccount + sub a4, a4, a3 + movi a2, 0x12345678 + wsr a2, ccount + esync + rsr a3, ccount + sub a3, a3, a2 + slli a4, a4, 2 + assert ltu, a3, a4 +test_end + +test ccount_update_deadline + movi a2, 0 + wsr a2, intenable + rsr a2, interrupt + wsr a2, intclear + movi a2, 0 + wsr a2, ccompare1 + wsr a2, ccompare2 + movi a2, 0x12345678 + wsr a2, ccompare0 + rsr a3, interrupt + assert eqi, a3, 0 + movi a2, 0x12345677 + wsr a2, ccount + esync + nop + rsr a2, interrupt + movi a3, 1 << XCHAL_TIMER0_INTERRUPT + assert eq, a2, a3 +test_end + test ccompare movi a2, 0 wsr a2, intenable
Signature · 1 line
2.1.4