From 340ca91f18a448e09973341f60bb6f46102d2410 Mon Sep 17 00:00:00 2001 From: Aaron Durbin Date: Tue, 30 Apr 2013 09:58:12 -0500 Subject: coreboot: add timer queue implementation A timer queue provides the mechanism for calling functions in the future by way of a callback. It utilizes the MONOTONIC_TIMER to track time through the boot. The implementation is a min-heap for keeping track of the next-to-expire callback. Change-Id: Ia56bab8444cd6177b051752342f53b53d5f6afc1 Signed-off-by: Aaron Durbin Reviewed-on: http://review.coreboot.org/3158 Tested-by: build bot (Jenkins) Reviewed-by: Ronald G. Minnich --- src/Kconfig | 6 ++ src/include/timer.h | 18 +++++ src/lib/Makefile.inc | 1 + src/lib/timer_queue.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 src/lib/timer_queue.c (limited to 'src') diff --git a/src/Kconfig b/src/Kconfig index a49643eff7..ab2a9271fd 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -318,6 +318,12 @@ config HAVE_MONOTONIC_TIMER help The board/chipset provides a monotonic timer. +config TIMER_QUEUE + def_bool n + depends on HAVE_MONOTONIC_TIMER + help + Provide a timer queue for performing time-based callbacks. + config HIGH_SCRATCH_MEMORY_SIZE hex default 0x0 diff --git a/src/include/timer.h b/src/include/timer.h index 2b112dd5a9..e950c81eee 100644 --- a/src/include/timer.h +++ b/src/include/timer.h @@ -38,6 +38,17 @@ struct rela_time { long microseconds; }; +/* A timeout_callback structure is used for the book keeping for scheduling + * work in the future. When a callback is called the structure can be + * re-used for scheduling as it is not being tracked by the core timer + * library any more. */ +struct timeout_callback { + void *priv; + void (*callback)(struct timeout_callback *tocb); + /* Not for public use. The timer library uses the fields below. */ + struct mono_time expiration; +}; + /* Obtain the current monotonic time. The assumption is that the time counts * up from the value 0 with value 0 being the point when the timer was * initialized. Additionally, the timer is assumed to only be valid for the @@ -49,6 +60,13 @@ struct rela_time { * of 10 seconds. */ void timer_monotonic_get(struct mono_time *mt); +/* Returns 1 if callbacks still present in the queue. 0 if no timers left. */ +int timers_run(void); + +/* Schedule a callback to be ran microseconds from time of invocation. + * 0 returned on success, < 0 on error. */ +int timer_sched_callback(struct timeout_callback *tocb, unsigned long us); + /* Add microseconds to an absoute time. */ static inline void mono_time_add_usecs(struct mono_time *mt, long us) { diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc index e8b1485d81..7306e6d857 100644 --- a/src/lib/Makefile.inc +++ b/src/lib/Makefile.inc @@ -90,6 +90,7 @@ ramstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c ramstage-$(CONFIG_COVERAGE) += libgcov.c ramstage-$(CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT) += edid.c ramstage-y += memrange.c +ramstage-$(CONFIG_TIMER_QUEUE) += timer_queue.c # The CBMEM implementations are chosen based on CONFIG_DYNAMIC_CBMEM. ifeq ($(CONFIG_DYNAMIC_CBMEM),y) diff --git a/src/lib/timer_queue.c b/src/lib/timer_queue.c new file mode 100644 index 0000000000..8d11f107a8 --- /dev/null +++ b/src/lib/timer_queue.c @@ -0,0 +1,198 @@ +/* + * This file is part of the coreboot project. + * + * Copyright (C) 2013 Google, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include + +#define MAX_TIMER_QUEUE_ENTRIES 64 + +/* The timer queue is implemented using a min heap. Therefore the first + * element is the one with smallest time to expiration. */ +struct timer_queue { + int num_entries; + int max_entries; + struct timeout_callback *queue[MAX_TIMER_QUEUE_ENTRIES]; +}; + +static struct timer_queue global_timer_queue = { + .num_entries = 0, + .max_entries = MAX_TIMER_QUEUE_ENTRIES, + .queue = { 0 }, +}; + +static inline int timer_queue_empty(struct timer_queue *tq) +{ + return tq->num_entries == 0; +} + +static inline int timer_queue_full(struct timer_queue *tq) +{ + return tq->num_entries == tq->max_entries; +} + +static inline struct timeout_callback *timer_queue_head(struct timer_queue *tq) +{ + if (timer_queue_empty(tq)) + return NULL; + return tq->queue[0]; +} + +static int timer_queue_insert(struct timer_queue *tq, + struct timeout_callback *tocb) +{ + int index; + + /* No more slots. */ + if (timer_queue_full(tq)) + return -1; + + index = tq->num_entries; + tq->num_entries++; + tq->queue[index] = tocb; + + while (index != 0) { + struct timeout_callback *parent; + int parent_index; + + parent_index = (index - 1) / 2; + parent = tq->queue[parent_index]; + + /* All other ancestors are less than or equal to the current. */ + if (mono_time_cmp(&parent->expiration, &tocb->expiration) <= 0) + break; + + /* The parent is greater than current. Swap them. */ + tq->queue[parent_index] = tocb; + tq->queue[index] = parent; + + index = parent_index; + } + + return 0; +} + +/* Get the index containing the entry with smallest value. */ +static int timer_queue_min_child_index(struct timer_queue *tq, int index) +{ + int left_child_index; + int right_child_index; + + left_child_index = 2 * index + 1; + + if (left_child_index >= tq->num_entries) + return -1; + + right_child_index = left_child_index + 1; + + if (right_child_index >= tq->num_entries) + return left_child_index; + + if (mono_time_cmp(&tq->queue[left_child_index]->expiration, + &tq->queue[right_child_index]->expiration) < 0) { + return left_child_index; + } + return right_child_index; +} + +static void timer_queue_remove_head(struct timer_queue *tq) +{ + int index; + struct timeout_callback *tocb; + + /* In order to remove the head the deepest child is replaced in the + * head slot and bubbled down the tree. */ + tq->num_entries--; + tocb = tq->queue[tq->num_entries]; + tq->queue[0] = tocb; + + index = 0; + while (1) { + int min_child_index; + struct timeout_callback *child; + + min_child_index = timer_queue_min_child_index(tq, index); + + /* No more entries to compare against. */ + if (min_child_index < 0) + break; + + child = tq->queue[min_child_index]; + + /* Current index is the correct place since it is smaller or + * equal to the smallest child. */ + if (mono_time_cmp(&tocb->expiration, &child->expiration) <= 0) + break; + + /* Need to swap with smallest child. */ + tq->queue[min_child_index] = tocb; + tq->queue[index] = child; + + index = min_child_index; + } +} + +static struct timeout_callback * +timer_queue_expired(struct timer_queue *tq, struct mono_time *current_time) +{ + struct timeout_callback *tocb; + + tocb = timer_queue_head(tq); + + if (tocb == NULL) + return NULL; + + /* The timeout callback hasn't expired yet. */ + if (mono_time_before(current_time, &tocb->expiration)) + return NULL; + + timer_queue_remove_head(tq); + + return tocb; +} + +int timer_sched_callback(struct timeout_callback *tocb, unsigned long us) +{ + struct mono_time current_time; + + if ((long)us< 0) + return -1; + + timer_monotonic_get(¤t_time); + tocb->expiration = current_time; + mono_time_add_usecs(&tocb->expiration, us); + + /* The expiration overflowed. */ + if (us != 0 && !mono_time_before(¤t_time, &tocb->expiration)) + return -1; + + return timer_queue_insert(&global_timer_queue, tocb); +} + +int timers_run(void) +{ + struct timeout_callback *tocb; + struct mono_time current_time; + + timer_monotonic_get(¤t_time); + tocb = timer_queue_expired(&global_timer_queue, ¤t_time); + + if (tocb != NULL) + tocb->callback(tocb); + + return !timer_queue_empty(&global_timer_queue); +} -- cgit v1.2.3