genode/repos/os/src/drivers/timer/pit/time_source.cc

193 lines
6.0 KiB
C++
Raw Normal View History

/*
* \brief Time source that uses the Programmable Interval Timer (PIT)
* \author Norman Feske
* \author Martin Stein
* \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.
*/
/* Genode includes */
#include <drivers/timer/util.h>
/* local includes */
#include <time_source.h>
using namespace Genode;
void Timer::Time_source::_set_counter(uint16_t value)
{
_handled_wrap = false;
_io_port.outb(PIT_DATA_PORT_0, value & 0xff);
_io_port.outb(PIT_DATA_PORT_0, (value >> 8) & 0xff);
}
uint16_t Timer::Time_source::_read_counter(bool *wrapped)
{
/* read-back count and status of counter 0 */
_io_port.outb(PIT_CMD_PORT, PIT_CMD_READ_BACK |
PIT_CMD_RB_COUNT |
PIT_CMD_RB_STATUS |
PIT_CMD_RB_CHANNEL_0);
/* read status byte from latch register */
uint8_t status = _io_port.inb(PIT_DATA_PORT_0);
/* read low and high bytes from latch register */
uint16_t lo = _io_port.inb(PIT_DATA_PORT_0);
uint16_t hi = _io_port.inb(PIT_DATA_PORT_0);
*wrapped = status & PIT_STAT_INT_LINE ? true : false;
return (hi << 8) | lo;
}
void Timer::Time_source::schedule_timeout(Microseconds duration,
Timeout_handler &handler)
{
_handler = &handler;
_timer_irq.ack_irq();
unsigned long duration_us = duration.value;
/* limit timer-interrupt rate */
enum { MAX_TIMER_IRQS_PER_SECOND = 4*1000 };
if (duration_us < 1000 * 1000 / MAX_TIMER_IRQS_PER_SECOND)
duration_us = 1000 * 1000 / MAX_TIMER_IRQS_PER_SECOND;
if (duration_us > max_timeout().value)
duration_us = max_timeout().value;
_counter_init_value = (PIT_TICKS_PER_MSEC * duration_us) / 1000;
_set_counter(_counter_init_value);
}
uint32_t Timer::Time_source::_ticks_since_update_no_wrap(uint16_t curr_counter)
{
/*
* The counter did not wrap since the last update of _counter_init_value.
* This means that _counter_init_value is equal to or greater than
* curr_counter and that the time that passed is simply the difference
* between the two.
*/
return _counter_init_value - curr_counter;
}
uint32_t Timer::Time_source::_ticks_since_update_one_wrap(uint16_t curr_counter)
{
/*
* The counter wrapped since the last update of _counter_init_value.
* This means that the time that passed is the whole _counter_init_value
* plus the time that passed since the counter wrapped.
*/
return _counter_init_value + PIT_MAX_COUNT - curr_counter;
}
os/timer: interpolate time via timestamps Previously, the Genode::Timer::curr_time always used the Timer_session::elapsed_ms RPC as back end. Now, Genode::Timer reads this remote time only in a periodic fashion independently from the calls to Genode::Timer::curr_time. If now one calls Genode::Timer::curr_time, the function takes the last read remote time value and adapts it using the timestamp difference since the remote-time read. The conversion factor from timestamps to time is estimated on every remote-time read using the last read remote-time value and the timestamp difference since the last remote time read. This commit also re-works the timeout test. The test now has two stages. In the first stage, it tests fast polling of the Genode::Timer::curr_time. This stage checks the error between locally interpolated and timer-driver time as well as wether the locally interpolated time is monotone and sufficiently homogeneous. In the second stage several periodic and one-shot timeouts are scheduled at once. This stage checks if the timeouts trigger sufficiently precise. This commit adds the new Kernel::time syscall to base-hw. The syscall is solely used by the Genode::Timer on base-hw as substitute for the timestamp. This is because on ARM, the timestamp function uses the ARM performance counter that stops counting when the WFI (wait for interrupt) instruction is active. This instruction, however is used by the base-hw idle contexts that get active when no user thread needs to be scheduled. Thus, the ARM performance counter is not a good choice for time interpolation and we use the kernel internal time instead. With this commit, the timeout library becomes a basic library. That means that it is linked against the LDSO which then provides it to the program it serves. Furthermore, you can't use the timeout library anymore without the LDSO because through the kernel-dependent LDSO make-files we can achieve a kernel-dependent timeout implementation. This commit introduces a structured Duration type that shall successively replace the use of Microseconds, Milliseconds, and integer types for duration values. Open issues: * The timeout test fails on Raspberry PI because of precision errors in the first stage. However, this does not render the framework unusable in general on the RPI but merely is an issue when speaking of microseconds precision. * If we run on ARM with another Kernel than HW the timestamp speed may continuously vary from almost 0 up to CPU speed. The Timer, however, only uses interpolation if the timestamp speed remained stable (12.5% tolerance) for at least 3 observation periods. Currently, one period is 100ms, so its 300ms. As long as this is not the case, Timer_session::elapsed_ms is called instead. Anyway, it might happen that the CPU load was stable for some time so interpolation becomes active and now the timestamp speed drops. In the worst case, we would now have 100ms of slowed down time. The bad thing about it would be, that this also affects the timeout of the period. Thus, it might "freeze" the local time for more than 100ms. On the other hand, if the timestamp speed suddenly raises after some stable time, interpolated time can get too fast. This would shorten the period but nonetheless may result in drifting away into the far future. Now we would have the problem that we can't deliver the real time anymore until it has caught up because the output of Timer::curr_time shall be monotone. So, effectively local time might "freeze" again for more than 100ms. It would be a solution to not use the Trace::timestamp on ARM w/o HW but a function whose return value causes the Timer to never use interpolation because of its stability policy. Fixes #2400
2017-04-22 00:52:23 +02:00
Duration Timer::Time_source::curr_time()
{
/* read PIT counter and wrapped status */
uint32_t ticks;
bool wrapped;
uint16_t const curr_counter = _read_counter(&wrapped);
if (!wrapped) {
/*
* The counter did not wrap since the last call to scheduled_timeout
* which means that it did not wrap since the last update of
* _counter_init_time.
*/
ticks = _ticks_since_update_no_wrap(curr_counter);
}
else if (wrapped && !_handled_wrap) {
/*
* The counter wrapped at least once since the last call to
* schedule_timeout (wrapped) and curr_time (!_handled_wrap) which
* means that it definitely did wrap since the last update of
* _counter_init_time. We cannot determine whether it wrapped only
* once but we have to assume it. Even if it wrapped multiple times,
* the error that results from the assumption that it did not is pretty
* innocuous ((nr_of_wraps - 1) * 53 ms at a max).
*/
ticks = _ticks_since_update_one_wrap(curr_counter);
_handled_wrap = true;
}
else { /* wrapped && _handled_wrap */
/*
* The counter wrapped at least once since the last call to
* schedule_timeout (wrapped) but may not have wrapped since the last
* call to curr_time (_handled_wrap).
*/
if (_counter_init_value >= curr_counter) {
/*
* We cannot determine whether the counter wrapped since the last
* call to curr_time but we have to assume that it did not. Even if
* it wrapped, the error that results from the assumption that it
* did not is pretty innocuous as long as _counter_init_value is
* not greater than curr_counter (nr_of_wraps * 53 ms at a max).
*/
ticks = _ticks_since_update_no_wrap(curr_counter);
} else {
/*
* The counter definitely wrapped multiple times since the last
* call to schedule_timeout and at least once since the last call
* to curr_time. It is the only explanation for the fact that
* curr_counter became greater than _counter_init_value again
* after _counter_init_value was updated with a wrapped counter
* by curr_time (_handled_wrap). This means two things:
*
* First, the counter wrapped at least once since the last update
* of _counter_init_value. We cannot determine whether it wrapped
* only once but we have to assume it. Even if it wrapped multiple
* times, the error that results from the assumption that it
* did not is pretty innocuous ((nr_of_wraps - 1) * 53 ms at a max).
*
* Second, we have to warn the user as it is a sure indication of
* insufficient activation latency if the counter wraps multiple
* times between two schedule_timeout calls.
*/
warning("PIT wrapped multiple times, timer-driver latency too big");
ticks = _ticks_since_update_one_wrap(curr_counter);
}
}
/* use current counter as the reference for the next update */
_counter_init_value = curr_counter;
/* translate counter to microseconds and update time value */
static_assert(PIT_TICKS_PER_MSEC >= (unsigned)TIMER_MIN_TICKS_PER_MS,
"Bad TICS_PER_MS value");
_curr_time_us += timer_ticks_to_us(ticks, PIT_TICKS_PER_MSEC);
os/timer: interpolate time via timestamps Previously, the Genode::Timer::curr_time always used the Timer_session::elapsed_ms RPC as back end. Now, Genode::Timer reads this remote time only in a periodic fashion independently from the calls to Genode::Timer::curr_time. If now one calls Genode::Timer::curr_time, the function takes the last read remote time value and adapts it using the timestamp difference since the remote-time read. The conversion factor from timestamps to time is estimated on every remote-time read using the last read remote-time value and the timestamp difference since the last remote time read. This commit also re-works the timeout test. The test now has two stages. In the first stage, it tests fast polling of the Genode::Timer::curr_time. This stage checks the error between locally interpolated and timer-driver time as well as wether the locally interpolated time is monotone and sufficiently homogeneous. In the second stage several periodic and one-shot timeouts are scheduled at once. This stage checks if the timeouts trigger sufficiently precise. This commit adds the new Kernel::time syscall to base-hw. The syscall is solely used by the Genode::Timer on base-hw as substitute for the timestamp. This is because on ARM, the timestamp function uses the ARM performance counter that stops counting when the WFI (wait for interrupt) instruction is active. This instruction, however is used by the base-hw idle contexts that get active when no user thread needs to be scheduled. Thus, the ARM performance counter is not a good choice for time interpolation and we use the kernel internal time instead. With this commit, the timeout library becomes a basic library. That means that it is linked against the LDSO which then provides it to the program it serves. Furthermore, you can't use the timeout library anymore without the LDSO because through the kernel-dependent LDSO make-files we can achieve a kernel-dependent timeout implementation. This commit introduces a structured Duration type that shall successively replace the use of Microseconds, Milliseconds, and integer types for duration values. Open issues: * The timeout test fails on Raspberry PI because of precision errors in the first stage. However, this does not render the framework unusable in general on the RPI but merely is an issue when speaking of microseconds precision. * If we run on ARM with another Kernel than HW the timestamp speed may continuously vary from almost 0 up to CPU speed. The Timer, however, only uses interpolation if the timestamp speed remained stable (12.5% tolerance) for at least 3 observation periods. Currently, one period is 100ms, so its 300ms. As long as this is not the case, Timer_session::elapsed_ms is called instead. Anyway, it might happen that the CPU load was stable for some time so interpolation becomes active and now the timestamp speed drops. In the worst case, we would now have 100ms of slowed down time. The bad thing about it would be, that this also affects the timeout of the period. Thus, it might "freeze" the local time for more than 100ms. On the other hand, if the timestamp speed suddenly raises after some stable time, interpolated time can get too fast. This would shorten the period but nonetheless may result in drifting away into the far future. Now we would have the problem that we can't deliver the real time anymore until it has caught up because the output of Timer::curr_time shall be monotone. So, effectively local time might "freeze" again for more than 100ms. It would be a solution to not use the Trace::timestamp on ARM w/o HW but a function whose return value causes the Timer to never use interpolation because of its stability policy. Fixes #2400
2017-04-22 00:52:23 +02:00
return Duration(Microseconds(_curr_time_us));
}
Timer::Time_source::Time_source(Env &env)
:
Signalled_time_source(env),
_io_port(env, PIT_DATA_PORT_0, PIT_CMD_PORT - PIT_DATA_PORT_0 + 1),
_timer_irq(env, IRQ_PIT)
{
/* operate PIT in one-shot mode */
_io_port.outb(PIT_CMD_PORT, PIT_CMD_SELECT_CHANNEL_0 |
PIT_CMD_ACCESS_LO_HI | PIT_CMD_MODE_IRQ);
_timer_irq.sigh(_signal_handler);
}