mirror of
https://github.com/genodelabs/genode.git
synced 2025-02-20 17:52:52 +00:00
timer: generic timer_ticks_to_us implementation
There are hardware timers whose frequency can't be expressed as ticks-per-microsecond integer-value because only a ticks-per-millisecond integer-value is precise enough. We don't want to use expensive floating-point values here but nonetheless want to translate from ticks to time with microseconds precision. Thus, we split the input in two and translate both parts separately. This way, we can raise precision by shifting the values to their optimal bit position. Afterwards, the results are shifted back and merged together again. As this algorithm is not so trivial anymore and used by at least three timer drivers (base-hw/x86_64, base-hw/cortex_a9, timer/pit), move it to a generic header to avoid redundancy. Ref #2400
This commit is contained in:
parent
652187b25e
commit
399e1586be
@ -12,6 +12,9 @@
|
||||
* under the terms of the GNU Affero General Public License version 3.
|
||||
*/
|
||||
|
||||
/* Genode includes */
|
||||
#include <drivers/timer/util.h>
|
||||
|
||||
/* core includes */
|
||||
#include <kernel/timer.h>
|
||||
#include <platform.h>
|
||||
@ -23,6 +26,7 @@ using namespace Kernel;
|
||||
Timer_driver::Timer_driver(unsigned)
|
||||
: Mmio(Platform::mmio_to_virt(Board::Cpu_mmio::PRIVATE_TIMER_MMIO_BASE))
|
||||
{
|
||||
static_assert(TICS_PER_MS >= 1000, "Bad TICS_PER_MS value");
|
||||
write<Control::Timer_enable>(0);
|
||||
}
|
||||
|
||||
@ -44,31 +48,8 @@ void Timer::_start_one_shot(time_t const ticks)
|
||||
}
|
||||
|
||||
|
||||
time_t Timer::_ticks_to_us(time_t const ticks) const
|
||||
{
|
||||
/*
|
||||
* If we would do the translation with one division and
|
||||
* multiplication over the whole argument, we would loose
|
||||
* microseconds granularity although the timer frequency would
|
||||
* allow for such granularity. Thus, we treat the most significant
|
||||
* half and the least significant half of the argument separate.
|
||||
* Each half is shifted to the best bit position for the
|
||||
* translation, then translated, and then shifted back.
|
||||
*/
|
||||
static_assert(Driver::TICS_PER_MS >= 1000, "Bad TICS_PER_MS value");
|
||||
enum {
|
||||
HALF_WIDTH = (sizeof(time_t) << 2),
|
||||
MSB_MASK = ~0UL << HALF_WIDTH,
|
||||
LSB_MASK = ~0UL >> HALF_WIDTH,
|
||||
MSB_RSHIFT = 10,
|
||||
LSB_LSHIFT = HALF_WIDTH - MSB_RSHIFT,
|
||||
};
|
||||
time_t const msb = ((((ticks & MSB_MASK) >> MSB_RSHIFT)
|
||||
* 1000) / Driver::TICS_PER_MS) << MSB_RSHIFT;
|
||||
time_t const lsb = ((((ticks & LSB_MASK) << LSB_LSHIFT)
|
||||
* 1000) / Driver::TICS_PER_MS) >> LSB_LSHIFT;
|
||||
return msb + lsb;
|
||||
}
|
||||
time_t Timer::_ticks_to_us(time_t const ticks) const {
|
||||
return timer_ticks_to_us(ticks, Driver::TICS_PER_MS); }
|
||||
|
||||
|
||||
unsigned Timer::interrupt_id() const {
|
||||
|
@ -15,6 +15,9 @@
|
||||
|
||||
#include <hw/spec/x86_64/x86_64.h>
|
||||
|
||||
/* Genode includes */
|
||||
#include <drivers/timer/util.h>
|
||||
|
||||
/* core includes */
|
||||
#include <kernel/timer.h>
|
||||
#include <platform.h>
|
||||
@ -85,29 +88,7 @@ void Timer::_start_one_shot(time_t const ticks) {
|
||||
|
||||
|
||||
time_t Timer::_ticks_to_us(time_t const ticks) const {
|
||||
|
||||
/*
|
||||
* If we would do the translation with one division and
|
||||
* multiplication over the whole argument, we would loose
|
||||
* microseconds granularity although the timer frequency would
|
||||
* allow for such granularity. Thus, we treat the most significant
|
||||
* half and the least significant half of the argument separate.
|
||||
* Each half is shifted to the best bit position for the
|
||||
* translation, then translated, and then shifted back.
|
||||
*/
|
||||
enum {
|
||||
HALF_WIDTH = (sizeof(time_t) << 2),
|
||||
MSB_MASK = ~0UL << HALF_WIDTH,
|
||||
LSB_MASK = ~0UL >> HALF_WIDTH,
|
||||
MSB_RSHIFT = 10,
|
||||
LSB_LSHIFT = HALF_WIDTH - MSB_RSHIFT,
|
||||
};
|
||||
time_t const msb = ((((ticks & MSB_MASK) >> MSB_RSHIFT)
|
||||
* 1000) / _driver.ticks_per_ms) << MSB_RSHIFT;
|
||||
time_t const lsb = ((((ticks & LSB_MASK) << LSB_LSHIFT)
|
||||
* 1000) / _driver.ticks_per_ms) >> LSB_LSHIFT;
|
||||
return msb + lsb;
|
||||
}
|
||||
return timer_ticks_to_us(ticks, _driver.ticks_per_ms); }
|
||||
|
||||
|
||||
time_t Timer::us_to_ticks(time_t const us) const {
|
||||
|
53
repos/base/include/drivers/timer/util.h
Normal file
53
repos/base/include/drivers/timer/util.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* \brief Utilities for timer drivers
|
||||
* \author Martin Stein
|
||||
* \date 2017-08-23
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 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.
|
||||
*/
|
||||
|
||||
#ifndef _DRIVERS__TIMER__UTIL_H_
|
||||
#define _DRIVERS__TIMER__UTIL_H_
|
||||
|
||||
namespace Genode {
|
||||
|
||||
/*
|
||||
* Translate timer ticks to microseconds without losing precicision
|
||||
*
|
||||
* There are hardware timers whose frequency can't be expressed as
|
||||
* ticks-per-microsecond integer-value because only a ticks-per-millisecond
|
||||
* integer-value is precise enough. We don't want to use expensive
|
||||
* floating-point values here but nonetheless want to translate from ticks
|
||||
* to time with microseconds precision. Thus, we split the input in two and
|
||||
* translate both parts separately. This way, we can raise precision by
|
||||
* shifting the values to their optimal bit position. Afterwards, the
|
||||
* results are shifted back and merged together again.
|
||||
*
|
||||
* Please ensure that the assertion "ticks_per_ms >= 1000" is true
|
||||
* when calling this method!
|
||||
*/
|
||||
template <typename RESULT_T, typename TICS_PER_MS_T>
|
||||
RESULT_T timer_ticks_to_us(RESULT_T const ticks,
|
||||
TICS_PER_MS_T const ticks_per_ms)
|
||||
{
|
||||
enum {
|
||||
HALF_WIDTH = (sizeof(RESULT_T) << 2),
|
||||
MSB_MASK = ~0UL << HALF_WIDTH,
|
||||
LSB_MASK = ~0UL >> HALF_WIDTH,
|
||||
MSB_RSHIFT = 10,
|
||||
LSB_LSHIFT = HALF_WIDTH - MSB_RSHIFT,
|
||||
};
|
||||
RESULT_T const msb = ((((ticks & MSB_MASK) >> MSB_RSHIFT)
|
||||
* 1000) / ticks_per_ms) << MSB_RSHIFT;
|
||||
RESULT_T const lsb = ((((ticks & LSB_MASK) << LSB_LSHIFT)
|
||||
* 1000) / ticks_per_ms) >> LSB_LSHIFT;
|
||||
return msb + lsb;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* _DRIVERS__TIMER__UTIL_H_ */
|
@ -12,6 +12,9 @@
|
||||
* under the terms of the GNU Affero General Public License version 3.
|
||||
*/
|
||||
|
||||
/* Genode includes */
|
||||
#include <drivers/timer/util.h>
|
||||
|
||||
/* local includes */
|
||||
#include <time_source.h>
|
||||
|
||||
@ -89,29 +92,8 @@ Duration Timer::Time_source::curr_time()
|
||||
ticks = PIT_MAX_COUNT + 1 - curr_counter;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we would do the translation with one division and
|
||||
* multiplication over the whole argument, we would loose
|
||||
* microseconds granularity although the timer frequency would
|
||||
* allow for such granularity. Thus, we treat the most significant
|
||||
* half and the least significant half of the argument separate.
|
||||
* Each half is shifted to the best bit position for the
|
||||
* translation, then translated, and then shifted back.
|
||||
*/
|
||||
using time_t = unsigned long;
|
||||
static_assert(PIT_TICKS_PER_MSEC >= 1000, "Bad TICS_PER_MS value");
|
||||
enum {
|
||||
HALF_WIDTH = (sizeof(time_t) << 2),
|
||||
MSB_MASK = ~0UL << HALF_WIDTH,
|
||||
LSB_MASK = ~0UL >> HALF_WIDTH,
|
||||
MSB_RSHIFT = 10,
|
||||
LSB_LSHIFT = HALF_WIDTH - MSB_RSHIFT,
|
||||
};
|
||||
time_t const msb = ((((ticks & MSB_MASK) >> MSB_RSHIFT)
|
||||
* 1000) / PIT_TICKS_PER_MSEC) << MSB_RSHIFT;
|
||||
time_t const lsb = ((((ticks & LSB_MASK) << LSB_LSHIFT)
|
||||
* 1000) / PIT_TICKS_PER_MSEC) >> LSB_LSHIFT;
|
||||
_curr_time_us += (msb + lsb);
|
||||
_curr_time_us += timer_ticks_to_us(ticks, PIT_TICKS_PER_MSEC);
|
||||
|
||||
/* use current counter as the reference for the next update */
|
||||
_counter_init_value = curr_counter;
|
||||
|
Loading…
x
Reference in New Issue
Block a user