2016-11-08 14:06:20 +01:00
|
|
|
/*
|
|
|
|
* \brief Time source that uses the Programmable Interval Timer (PIT)
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Martin Stein
|
|
|
|
* \date 2009-06-16
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-13 19:32:35 +01:00
|
|
|
* Copyright (C) 2009-2017 Genode Labs GmbH
|
2016-11-08 14:06:20 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2016-11-08 14:06:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()
|
2016-11-08 14:06:20 +01:00
|
|
|
{
|
|
|
|
uint32_t passed_ticks;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read PIT count and status
|
|
|
|
*
|
|
|
|
* Reading the PIT registers via port I/O is a non-const operation.
|
|
|
|
* Since 'curr_time' is declared as const, however, we need to
|
|
|
|
* explicitly override the const-ness of the 'this' pointer.
|
|
|
|
*/
|
|
|
|
bool wrapped;
|
|
|
|
uint16_t const curr_counter =
|
|
|
|
const_cast<Time_source *>(this)->_read_counter(&wrapped);
|
|
|
|
|
|
|
|
/* determine the time since we looked at the counter */
|
|
|
|
if (wrapped && !_handled_wrap) {
|
|
|
|
passed_ticks = _counter_init_value;
|
|
|
|
/* the counter really wrapped around */
|
|
|
|
if (curr_counter)
|
|
|
|
passed_ticks += PIT_MAX_COUNT + 1 - curr_counter;
|
|
|
|
|
|
|
|
_handled_wrap = true;
|
|
|
|
} else {
|
|
|
|
if (_counter_init_value)
|
|
|
|
passed_ticks = _counter_init_value - curr_counter;
|
|
|
|
else
|
|
|
|
passed_ticks = PIT_MAX_COUNT + 1 - curr_counter;
|
|
|
|
}
|
|
|
|
_curr_time_us += (passed_ticks*1000)/PIT_TICKS_PER_MSEC;
|
|
|
|
|
|
|
|
/* use current counter as the reference for the next update */
|
|
|
|
_counter_init_value = 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
|
|
|
return Duration(Microseconds(_curr_time_us));
|
2016-11-08 14:06:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-13 19:32:35 +01:00
|
|
|
Timer::Time_source::Time_source(Env &env)
|
2016-11-08 14:06:20 +01:00
|
|
|
:
|
2017-02-13 19:32:35 +01:00
|
|
|
Signalled_time_source(env),
|
|
|
|
_io_port(env, PIT_DATA_PORT_0, PIT_CMD_PORT - PIT_DATA_PORT_0 + 1),
|
|
|
|
_timer_irq(env, IRQ_PIT)
|
2016-11-08 14:06:20 +01:00
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
}
|