2018-09-28 07:50:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
#include <timeout_q.h>
|
|
|
|
#include <drivers/system_timer.h>
|
|
|
|
#include <sys_clock.h>
|
|
|
|
#include <spinlock.h>
|
|
|
|
#include <ksched.h>
|
|
|
|
#include <syscall_handler.h>
|
|
|
|
|
|
|
|
#define LOCKED(lck) for (k_spinlock_key_t __i = {}, \
|
|
|
|
__key = k_spin_lock(lck); \
|
2019-03-15 05:32:45 +08:00
|
|
|
__i.key == 0; \
|
2018-09-28 07:50:00 +08:00
|
|
|
k_spin_unlock(lck, __key), __i.key = 1)
|
|
|
|
|
|
|
|
static u64_t curr_tick;
|
|
|
|
|
|
|
|
static sys_dlist_t timeout_list = SYS_DLIST_STATIC_INIT(&timeout_list);
|
|
|
|
|
|
|
|
static struct k_spinlock timeout_lock;
|
|
|
|
|
|
|
|
static bool can_wait_forever;
|
|
|
|
|
2018-10-03 23:50:52 +08:00
|
|
|
/* Cycles left to process in the currently-executing z_clock_announce() */
|
|
|
|
static int announce_remaining;
|
2018-09-28 07:50:00 +08:00
|
|
|
|
|
|
|
#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
|
|
|
|
int z_clock_hw_cycles_per_sec = CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct _timeout *first(void)
|
|
|
|
{
|
|
|
|
sys_dnode_t *t = sys_dlist_peek_head(&timeout_list);
|
|
|
|
|
|
|
|
return t == NULL ? NULL : CONTAINER_OF(t, struct _timeout, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct _timeout *next(struct _timeout *t)
|
|
|
|
{
|
|
|
|
sys_dnode_t *n = sys_dlist_peek_next(&timeout_list, &t->node);
|
|
|
|
|
|
|
|
return n == NULL ? NULL : CONTAINER_OF(n, struct _timeout, node);
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:29:19 +08:00
|
|
|
static void remove_timeout(struct _timeout *t)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
2018-12-30 20:05:03 +08:00
|
|
|
if (next(t) != NULL) {
|
|
|
|
next(t)->dticks += t->dticks;
|
2018-12-07 07:39:28 +08:00
|
|
|
}
|
2018-12-30 20:05:03 +08:00
|
|
|
|
|
|
|
sys_dlist_remove(&t->node);
|
2018-09-28 07:50:00 +08:00
|
|
|
}
|
|
|
|
|
2018-10-04 06:02:50 +08:00
|
|
|
static s32_t elapsed(void)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
2018-10-04 06:02:50 +08:00
|
|
|
return announce_remaining == 0 ? z_clock_elapsed() : 0;
|
2018-09-28 07:50:00 +08:00
|
|
|
}
|
|
|
|
|
2019-01-17 00:54:38 +08:00
|
|
|
static s32_t next_timeout(void)
|
|
|
|
{
|
|
|
|
int maxw = can_wait_forever ? K_FOREVER : INT_MAX;
|
|
|
|
struct _timeout *to = first();
|
2019-02-12 01:14:19 +08:00
|
|
|
s32_t ret = to == NULL ? maxw : MAX(0, to->dticks - elapsed());
|
2019-01-17 00:54:38 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_TIMESLICING
|
|
|
|
if (_current_cpu->slice_ticks && _current_cpu->slice_ticks < ret) {
|
|
|
|
ret = _current_cpu->slice_ticks;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
void z_add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
2019-01-02 22:29:43 +08:00
|
|
|
__ASSERT(!sys_dnode_is_linked(&to->node), "");
|
2018-09-28 07:50:00 +08:00
|
|
|
to->fn = fn;
|
2019-02-12 01:14:19 +08:00
|
|
|
ticks = MAX(1, ticks);
|
2018-09-28 07:50:00 +08:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
|
|
|
struct _timeout *t;
|
|
|
|
|
2018-10-04 06:02:50 +08:00
|
|
|
to->dticks = ticks + elapsed();
|
2018-09-28 07:50:00 +08:00
|
|
|
for (t = first(); t != NULL; t = next(t)) {
|
|
|
|
__ASSERT(t->dticks >= 0, "");
|
|
|
|
|
|
|
|
if (t->dticks > to->dticks) {
|
|
|
|
t->dticks -= to->dticks;
|
2019-01-29 01:35:27 +08:00
|
|
|
sys_dlist_insert(&t->node, &to->node);
|
2018-09-28 07:50:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
to->dticks -= t->dticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t == NULL) {
|
|
|
|
sys_dlist_append(&timeout_list, &to->node);
|
|
|
|
}
|
|
|
|
|
2018-11-22 18:49:32 +08:00
|
|
|
if (to == first()) {
|
2019-01-17 00:54:38 +08:00
|
|
|
z_clock_set_timeout(next_timeout(), false);
|
2018-11-22 18:49:32 +08:00
|
|
|
}
|
2018-11-21 00:26:34 +08:00
|
|
|
}
|
2018-09-28 07:50:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
int z_abort_timeout(struct _timeout *to)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
2019-01-02 22:29:43 +08:00
|
|
|
int ret = -EINVAL;
|
2018-09-28 07:50:00 +08:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
2018-12-30 20:05:03 +08:00
|
|
|
if (sys_dnode_is_linked(&to->node)) {
|
2018-10-17 23:29:19 +08:00
|
|
|
remove_timeout(to);
|
2018-09-28 07:50:00 +08:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-17 11:52:37 +08:00
|
|
|
s32_t z_timeout_remaining(struct _timeout *timeout)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
|
|
|
s32_t ticks = 0;
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
if (z_is_inactive_timeout(timeout)) {
|
2018-09-28 07:50:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
|
|
|
for (struct _timeout *t = first(); t != NULL; t = next(t)) {
|
|
|
|
ticks += t->dticks;
|
2018-11-17 11:52:37 +08:00
|
|
|
if (timeout == t) {
|
2018-09-28 07:50:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 02:51:04 +08:00
|
|
|
return ticks - elapsed();
|
2018-09-28 07:50:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
s32_t z_get_next_timeout_expiry(void)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
2019-01-17 00:54:38 +08:00
|
|
|
s32_t ret = K_FOREVER;
|
2018-09-28 07:50:00 +08:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
2019-01-17 00:54:38 +08:00
|
|
|
ret = next_timeout();
|
2018-09-28 07:50:00 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-22 18:49:32 +08:00
|
|
|
void z_set_timeout_expiry(s32_t ticks, bool idle)
|
|
|
|
{
|
|
|
|
LOCKED(&timeout_lock) {
|
2019-01-17 00:54:38 +08:00
|
|
|
int next = next_timeout();
|
2019-01-03 03:34:26 +08:00
|
|
|
bool sooner = (next == K_FOREVER) || (ticks < next);
|
|
|
|
bool imminent = next <= 1;
|
|
|
|
|
|
|
|
/* Only set new timeouts when they are sooner than
|
|
|
|
* what we have. Also don't try to set a timeout when
|
|
|
|
* one is about to expire: drivers have internal logic
|
|
|
|
* that will bump the timeout to the "next" tick if
|
|
|
|
* it's not considered to be settable as directed.
|
|
|
|
*/
|
|
|
|
if (sooner && !imminent) {
|
2018-11-22 18:49:32 +08:00
|
|
|
z_clock_set_timeout(ticks, idle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 01:23:31 +08:00
|
|
|
void z_clock_announce(s32_t ticks)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TIMESLICING
|
|
|
|
z_time_slice(ticks);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&timeout_lock);
|
|
|
|
|
|
|
|
announce_remaining = ticks;
|
|
|
|
|
|
|
|
while (first() != NULL && first()->dticks <= announce_remaining) {
|
|
|
|
struct _timeout *t = first();
|
|
|
|
int dt = t->dticks;
|
|
|
|
|
|
|
|
curr_tick += dt;
|
|
|
|
announce_remaining -= dt;
|
|
|
|
t->dticks = 0;
|
|
|
|
remove_timeout(t);
|
|
|
|
|
|
|
|
k_spin_unlock(&timeout_lock, key);
|
|
|
|
t->fn(t);
|
|
|
|
key = k_spin_lock(&timeout_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first() != NULL) {
|
|
|
|
first()->dticks -= announce_remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr_tick += announce_remaining;
|
|
|
|
announce_remaining = 0;
|
|
|
|
|
2019-01-31 04:31:25 +08:00
|
|
|
z_clock_set_timeout(next_timeout(), false);
|
2018-12-21 01:23:31 +08:00
|
|
|
|
|
|
|
k_spin_unlock(&timeout_lock, key);
|
|
|
|
}
|
|
|
|
|
2018-09-28 07:50:00 +08:00
|
|
|
int k_enable_sys_clock_always_on(void)
|
|
|
|
{
|
|
|
|
int ret = !can_wait_forever;
|
|
|
|
|
|
|
|
can_wait_forever = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void k_disable_sys_clock_always_on(void)
|
|
|
|
{
|
|
|
|
can_wait_forever = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s64_t z_tick_get(void)
|
|
|
|
{
|
2018-11-30 03:13:40 +08:00
|
|
|
u64_t t = 0U;
|
2018-09-28 07:50:00 +08:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
|
|
|
t = curr_tick + z_clock_elapsed();
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32_t z_tick_get_32(void)
|
|
|
|
{
|
2018-10-03 02:12:08 +08:00
|
|
|
#ifdef CONFIG_TICKLESS_KERNEL
|
|
|
|
return (u32_t)z_tick_get();
|
|
|
|
#else
|
|
|
|
return (u32_t)curr_tick;
|
|
|
|
#endif
|
2018-09-28 07:50:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
u32_t z_impl_k_uptime_get_32(void)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
|
|
|
return __ticks_to_ms(z_tick_get_32());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
Z_SYSCALL_HANDLER(k_uptime_get_32)
|
|
|
|
{
|
2019-03-09 05:19:05 +08:00
|
|
|
return z_impl_k_uptime_get_32();
|
2018-09-28 07:50:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
s64_t z_impl_k_uptime_get(void)
|
2018-09-28 07:50:00 +08:00
|
|
|
{
|
|
|
|
return __ticks_to_ms(z_tick_get());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
Z_SYSCALL_HANDLER(k_uptime_get, ret_p)
|
|
|
|
{
|
|
|
|
u64_t *ret = (u64_t *)ret_p;
|
|
|
|
|
|
|
|
Z_OOPS(Z_SYSCALL_MEMORY_WRITE(ret, sizeof(*ret)));
|
2019-03-09 05:19:05 +08:00
|
|
|
*ret = z_impl_k_uptime_get();
|
2018-09-28 07:50:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|