genode/repos/os/src/drivers/timer/epit/time_source.cc
Martin Stein 99ddaaa9d7 timer epit: fix multi-wraps and bug in rate limit
Multi-wraps
-----------

Previously, on every new timeout, we programmed registers LR=timeout and
CMP=0. The counter than counted from LR down to 0, triggered the IRQ,
jumped back to LR, and counted down again. If one installed small
timeouts (< 1000 us), it was likely that the counter wrapped multiple
times before we were able to read it out. Initially, this was not a big
issue as the additional wraps were simply ignored and the amount of time
lost through this was not big. But when we want to do correct rate
limitation, multiple wraps cause an overflow in the additional
calculations, and this has a big effect on the resulting time value.

Thus, we now program the counter to start from ~0 and count down to 0.
We set CMP=~0-timeout so that the timer still triggers the IRQ at the right
time. The counter continues counting down after the IRQ has triggered until
we install a new timeout. We do not consider anymore that the counter wraps.
The maximum timeout is set to half the maximum counter value, so, we should
be able to install a new timeout before the counter wraps.

Rate limit for time updates
---------------------------

In the time span between two interrupts we have to remember how many ticks
we have already added to the time value. This is because at each call of
curr_time we can only see how many ticks have passed since the last call of
schedule_timeout and not since the last call of curr_time. But we want to
limit the rate of time updates in curr_time. With the member for ticks that
were already added since the last call to schedule_timeout we can then
calculate how many are yet to be added.
2017-11-30 11:23:20 +01:00

56 lines
1.4 KiB
C++

/*
* \brief Time source that uses the Enhanced Periodic Interrupt Timer (Freescale)
* \author Norman Feske
* \author Martin Stein
* \author Stefan Kalkowski
* \author Alexander Boettcher
* \date 2009-06-16
*/
/*
* Copyright (C) 2009-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
/* local includes */
#include <time_source.h>
using namespace Genode;
void Timer::Time_source::schedule_timeout(Genode::Microseconds duration,
Timeout_handler &handler)
{
unsigned long const ticks = (1ULL * duration.value * TICKS_PER_MS) / 1000;
_handler = &handler;
_timer_irq.ack_irq();
_cleared_ticks = 0;
/* disable timer */
write<Cr::En>(0);
/* clear interrupt and install timeout */
write<Sr::Ocif>(1);
write<Cr>(Cr::prepare_one_shot());
write<Cmpr>(Cnt::MAX - ticks);
/* start timer */
write<Cr::En>(1);
}
Duration Timer::Time_source::curr_time()
{
unsigned long const uncleared_ticks = Cnt::MAX - read<Cnt>() - _cleared_ticks;
unsigned long const uncleared_us = timer_ticks_to_us(uncleared_ticks, TICKS_PER_MS);
/* update time only on IRQs and if rate is under 1000 per second */
if (_irq || uncleared_us > 1000) {
_curr_time.add(Genode::Microseconds(uncleared_us));
_cleared_ticks += uncleared_ticks;
}
return _curr_time;
}