From ec3b21079eb62e81dabb65f599cd647b87321944 Mon Sep 17 00:00:00 2001 From: wiklam Date: Mon, 14 Jun 2021 03:06:25 +0200 Subject: [PATCH 1/5] Remove redundant tm_select --- include/sys/timer.h | 3 --- sys/aarch64/timer.c | 3 +-- sys/kern/timer.c | 4 ---- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/include/sys/timer.h b/include/sys/timer.h index 45eb95cf39..ea70150cd8 100644 --- a/include/sys/timer.h +++ b/include/sys/timer.h @@ -81,9 +81,6 @@ int tm_stop(timer_t *tm); /*! \brief Used by interrupt filter routine to trigger a callback. */ void tm_trigger(timer_t *tm); -/*! \brief Select timer used as a main time source (for binuptime, etc.) */ -void tm_select(timer_t *tm); - #endif /* !_KERNEL */ #endif /* !_SYS_TIMER_H_ */ diff --git a/sys/aarch64/timer.c b/sys/aarch64/timer.c index 4d1d9202ea..9b82786eb2 100644 --- a/sys/aarch64/timer.c +++ b/sys/aarch64/timer.c @@ -86,8 +86,7 @@ static int arm_timer_attach(device_t *dev) { state->irq_res = device_take_irq(dev, 0, RF_ACTIVE); tm_register(&state->timer); - tm_select(&state->timer); - + bus_intr_setup(dev, state->irq_res, arm_timer_intr, NULL, dev, "ARM CPU timer"); diff --git a/sys/kern/timer.c b/sys/kern/timer.c index ec7871c4e7..62b54cf7c3 100644 --- a/sys/kern/timer.c +++ b/sys/kern/timer.c @@ -169,10 +169,6 @@ void tm_trigger(timer_t *tm) { tm->tm_event_cb(tm, tm->tm_arg); } -void tm_select(timer_t *tm) { - time_source = tm; -} - bintime_t binuptime(void) { /* XXX: probably a race condition here */ timer_t *tm = time_source; From c9f725f5e50b8961d5fcbdc1ebf5e8d197c4e7fe Mon Sep 17 00:00:00 2001 From: wiklam Date: Mon, 14 Jun 2021 03:12:44 +0200 Subject: [PATCH 2/5] Fix array size in tm2sec --- sys/kern/time.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/kern/time.c b/sys/kern/time.c index 5d4a7c10dd..7cb8a06fd8 100644 --- a/sys/kern/time.c +++ b/sys/kern/time.c @@ -128,7 +128,7 @@ time_t tm2sec(tm_t *t) { const int32_t year_scale_s = 31536000, day_scale_s = 86400, hour_scale_s = 3600, min_scale_s = 60; time_t res = 0; - static const int month_in_days[13] = {0, 31, 59, 90, 120, 151, + static const int month_in_days[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; res += (time_t)month_in_days[t->tm_mon] * day_scale_s; From d62dc53fe02c5fd0ece565800721739828b4934e Mon Sep 17 00:00:00 2001 From: wiklam Date: Mon, 14 Jun 2021 03:59:13 +0200 Subject: [PATCH 3/5] Fix MIPS timer start/stop functions --- sys/mips/timer.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/sys/mips/timer.c b/sys/mips/timer.c index be4f37bcb4..852bc2420b 100644 --- a/sys/mips/timer.c +++ b/sys/mips/timer.c @@ -52,7 +52,7 @@ static int set_next_tick(mips_timer_state_t *state) { /* calculate next value of compare register based on timer period */ do { state->compare.val += state->period_cntr; - mips32_set_c0(C0_COMPARE, state->compare.lo); + mips32_setcompare(state->compare.lo); (void)read_count(state); ticks++; } while (state->compare.val <= state->count.val); @@ -74,26 +74,17 @@ static int mips_timer_start(timer_t *tm, unsigned flags, const bintime_t start, assert(flags & TMF_PERIODIC); assert(!(flags & TMF_ONESHOT)); - mips32_setcount(0); - device_t *dev = tm->tm_priv; mips_timer_state_t *state = dev->state; - state->sec = 0; - state->cntr_modulo = 0; - state->last_count_lo = 0; state->period_cntr = bintime_mul(period, tm->tm_frequency).sec; state->compare.val = read_count(state); set_next_tick(state); - bus_intr_setup(dev, state->irq_res, mips_timer_intr, NULL, dev, - "MIPS CPU timer"); return 0; } -static int mips_timer_stop(timer_t *tm) { - device_t *dev = tm->tm_priv; - mips_timer_state_t *state = dev->state; - bus_intr_teardown(dev, state->irq_res); +static inline int mips_timer_stop(timer_t *tm) { + mips32_setcompare(0xffffffff); return 0; } @@ -119,8 +110,15 @@ static int mips_timer_probe(device_t *dev) { static int mips_timer_attach(device_t *dev) { mips_timer_state_t *state = dev->state; + mips32_setcount(0); + + state->sec = 0; + state->cntr_modulo = 0; + state->last_count_lo = 0; state->irq_res = device_take_irq(dev, 0, RF_ACTIVE); + mips_timer_stop(&state->timer); + state->timer = (timer_t){ .tm_name = "mips-cpu-timer", .tm_flags = TMF_PERIODIC, @@ -136,6 +134,9 @@ static int mips_timer_attach(device_t *dev) { tm_register(&state->timer); + bus_intr_setup(dev, state->irq_res, mips_timer_intr, NULL, dev, + "MIPS CPU timer"); + return 0; } From a0b8e4a9cac751cd51c9d4c6deda996b0ff9ec01 Mon Sep 17 00:00:00 2001 From: wiklam Date: Mon, 14 Jun 2021 04:28:38 +0200 Subject: [PATCH 4/5] Format --- sys/aarch64/timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/aarch64/timer.c b/sys/aarch64/timer.c index 9b82786eb2..b9b2c17fa2 100644 --- a/sys/aarch64/timer.c +++ b/sys/aarch64/timer.c @@ -86,7 +86,7 @@ static int arm_timer_attach(device_t *dev) { state->irq_res = device_take_irq(dev, 0, RF_ACTIVE); tm_register(&state->timer); - + bus_intr_setup(dev, state->irq_res, arm_timer_intr, NULL, dev, "ARM CPU timer"); From 3f36e96c8baf3460b65f34a575adf343230d2f33 Mon Sep 17 00:00:00 2001 From: wiklam Date: Mon, 14 Jun 2021 05:11:13 +0200 Subject: [PATCH 5/5] Fix PIT timer start/stop functions --- sys/drv/pit.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sys/drv/pit.c b/sys/drv/pit.c index f44c31bc61..9cfa81fd37 100644 --- a/sys/drv/pit.c +++ b/sys/drv/pit.c @@ -103,22 +103,19 @@ static int timer_pit_start(timer_t *tm, unsigned flags, const bintime_t start, /* Maximal counter value which we can store in pit timer */ assert(counter <= 0xFFFF); - pit->sec = 0; - pit->ticks = 0; - pit->prev_ticks16 = 0; pit->period_ticks = counter; - pit->noticed_overflow = false; pit_set_frequency(pit); - bus_intr_setup(dev, pit->irq_res, pit_intr, NULL, pit, "i8254 timer"); return 0; } static int timer_pit_stop(timer_t *tm) { device_t *dev = device_of(tm); pit_state_t *pit = dev->state; - bus_intr_teardown(dev, pit->irq_res); + + outb(TIMER_MODE, TIMER_SEL0 | TIMER_16BIT | TIMER_ONESHOT); + return 0; } @@ -142,6 +139,10 @@ static int pit_attach(device_t *dev) { pit->regs = device_take_ioports(dev, 0, RF_ACTIVE); assert(pit->regs != NULL); + pit->sec = 0; + pit->ticks = 0; + pit->prev_ticks16 = 0; + pit->noticed_overflow = false; pit->irq_res = device_take_irq(dev, 0, RF_ACTIVE); pit->timer = (timer_t){ @@ -157,8 +158,12 @@ static int pit_attach(device_t *dev) { .tm_priv = dev, }; + timer_pit_stop(&pit->timer); + tm_register(&pit->timer); + bus_intr_setup(dev, pit->irq_res, pit_intr, NULL, pit, "i8254 timer"); + return 0; }