Add a few functions to apic.c to make it easier to enable and disable the local apic timer. Signed-off-by: Maxim Levitsky <mlevitsk@xxxxxxxxxx> --- lib/x86/apic.c | 38 ++++++++++++++++++++++++++++++++++++++ lib/x86/apic.h | 6 ++++++ 2 files changed, 44 insertions(+) diff --git a/lib/x86/apic.c b/lib/x86/apic.c index 5131525a..174a8c28 100644 --- a/lib/x86/apic.c +++ b/lib/x86/apic.c @@ -256,3 +256,41 @@ void init_apic_map(void) id_map[j++] = i; } } + +void apic_setup_timer(int vector, u32 mode) +{ + apic_cleanup_timer(); + + assert((mode & APIC_LVT_TIMER_MASK) == mode); + + apic_write(APIC_TDCR, APIC_TDR_DIV_1); + apic_write(APIC_LVTT, vector | mode); +} + +void apic_start_timer(u32 value) +{ + /* + * APIC timer runs with 'core crystal clock', + * divided by value in APIC_TDCR + */ + apic_write(APIC_TMICT, value); +} + +void apic_stop_timer(void) +{ + apic_write(APIC_TMICT, 0); +} + +void apic_cleanup_timer(void) +{ + u32 lvtt = apic_read(APIC_LVTT); + + // stop the counter + apic_stop_timer(); + + // mask the timer interrupt + apic_write(APIC_LVTT, lvtt | APIC_LVT_MASKED); + + // enable interrupts for one cycle to ensure that a pending timer is serviced + sti_nop_cli(); +} diff --git a/lib/x86/apic.h b/lib/x86/apic.h index 6d27f047..7c539071 100644 --- a/lib/x86/apic.h +++ b/lib/x86/apic.h @@ -58,6 +58,12 @@ void disable_apic(void); void reset_apic(void); void init_apic_map(void); +void apic_cleanup_timer(void); +void apic_setup_timer(int vector, u32 mode); + +void apic_start_timer(u32 counter); +void apic_stop_timer(void); + /* Converts byte-addressable APIC register offset to 4-byte offset. */ static inline u32 apic_reg_index(u32 reg) { -- 2.34.3