blob: 8d11f107a8c4caa130cf9ae394012d00259f6f37 [file] [log] [blame]
Aaron Durbin340ca912013-04-30 09:58:12 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include <stddef.h>
20#include <timer.h>
21
22#define MAX_TIMER_QUEUE_ENTRIES 64
23
24/* The timer queue is implemented using a min heap. Therefore the first
25 * element is the one with smallest time to expiration. */
26struct timer_queue {
27 int num_entries;
28 int max_entries;
29 struct timeout_callback *queue[MAX_TIMER_QUEUE_ENTRIES];
30};
31
32static struct timer_queue global_timer_queue = {
33 .num_entries = 0,
34 .max_entries = MAX_TIMER_QUEUE_ENTRIES,
35 .queue = { 0 },
36};
37
38static inline int timer_queue_empty(struct timer_queue *tq)
39{
40 return tq->num_entries == 0;
41}
42
43static inline int timer_queue_full(struct timer_queue *tq)
44{
45 return tq->num_entries == tq->max_entries;
46}
47
48static inline struct timeout_callback *timer_queue_head(struct timer_queue *tq)
49{
50 if (timer_queue_empty(tq))
51 return NULL;
52 return tq->queue[0];
53}
54
55static int timer_queue_insert(struct timer_queue *tq,
56 struct timeout_callback *tocb)
57{
58 int index;
59
60 /* No more slots. */
61 if (timer_queue_full(tq))
62 return -1;
63
64 index = tq->num_entries;
65 tq->num_entries++;
66 tq->queue[index] = tocb;
67
68 while (index != 0) {
69 struct timeout_callback *parent;
70 int parent_index;
71
72 parent_index = (index - 1) / 2;
73 parent = tq->queue[parent_index];
74
75 /* All other ancestors are less than or equal to the current. */
76 if (mono_time_cmp(&parent->expiration, &tocb->expiration) <= 0)
77 break;
78
79 /* The parent is greater than current. Swap them. */
80 tq->queue[parent_index] = tocb;
81 tq->queue[index] = parent;
82
83 index = parent_index;
84 }
85
86 return 0;
87}
88
89/* Get the index containing the entry with smallest value. */
90static int timer_queue_min_child_index(struct timer_queue *tq, int index)
91{
92 int left_child_index;
93 int right_child_index;
94
95 left_child_index = 2 * index + 1;
96
97 if (left_child_index >= tq->num_entries)
98 return -1;
99
100 right_child_index = left_child_index + 1;
101
102 if (right_child_index >= tq->num_entries)
103 return left_child_index;
104
105 if (mono_time_cmp(&tq->queue[left_child_index]->expiration,
106 &tq->queue[right_child_index]->expiration) < 0) {
107 return left_child_index;
108 }
109 return right_child_index;
110}
111
112static void timer_queue_remove_head(struct timer_queue *tq)
113{
114 int index;
115 struct timeout_callback *tocb;
116
117 /* In order to remove the head the deepest child is replaced in the
118 * head slot and bubbled down the tree. */
119 tq->num_entries--;
120 tocb = tq->queue[tq->num_entries];
121 tq->queue[0] = tocb;
122
123 index = 0;
124 while (1) {
125 int min_child_index;
126 struct timeout_callback *child;
127
128 min_child_index = timer_queue_min_child_index(tq, index);
129
130 /* No more entries to compare against. */
131 if (min_child_index < 0)
132 break;
133
134 child = tq->queue[min_child_index];
135
136 /* Current index is the correct place since it is smaller or
137 * equal to the smallest child. */
138 if (mono_time_cmp(&tocb->expiration, &child->expiration) <= 0)
139 break;
140
141 /* Need to swap with smallest child. */
142 tq->queue[min_child_index] = tocb;
143 tq->queue[index] = child;
144
145 index = min_child_index;
146 }
147}
148
149static struct timeout_callback *
150timer_queue_expired(struct timer_queue *tq, struct mono_time *current_time)
151{
152 struct timeout_callback *tocb;
153
154 tocb = timer_queue_head(tq);
155
156 if (tocb == NULL)
157 return NULL;
158
159 /* The timeout callback hasn't expired yet. */
160 if (mono_time_before(current_time, &tocb->expiration))
161 return NULL;
162
163 timer_queue_remove_head(tq);
164
165 return tocb;
166}
167
168int timer_sched_callback(struct timeout_callback *tocb, unsigned long us)
169{
170 struct mono_time current_time;
171
172 if ((long)us< 0)
173 return -1;
174
175 timer_monotonic_get(&current_time);
176 tocb->expiration = current_time;
177 mono_time_add_usecs(&tocb->expiration, us);
178
179 /* The expiration overflowed. */
180 if (us != 0 && !mono_time_before(&current_time, &tocb->expiration))
181 return -1;
182
183 return timer_queue_insert(&global_timer_queue, tocb);
184}
185
186int timers_run(void)
187{
188 struct timeout_callback *tocb;
189 struct mono_time current_time;
190
191 timer_monotonic_get(&current_time);
192 tocb = timer_queue_expired(&global_timer_queue, &current_time);
193
194 if (tocb != NULL)
195 tocb->callback(tocb);
196
197 return !timer_queue_empty(&global_timer_queue);
198}