blob: fe3e8b59a5a390c43041965d53acfbb7b37cc2bb [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.
Aaron Durbin340ca912013-04-30 09:58:12 -050014 */
15#include <stddef.h>
16#include <timer.h>
17
18#define MAX_TIMER_QUEUE_ENTRIES 64
19
20/* The timer queue is implemented using a min heap. Therefore the first
21 * element is the one with smallest time to expiration. */
22struct timer_queue {
23 int num_entries;
24 int max_entries;
25 struct timeout_callback *queue[MAX_TIMER_QUEUE_ENTRIES];
26};
27
28static struct timer_queue global_timer_queue = {
29 .num_entries = 0,
30 .max_entries = MAX_TIMER_QUEUE_ENTRIES,
31 .queue = { 0 },
32};
33
34static inline int timer_queue_empty(struct timer_queue *tq)
35{
36 return tq->num_entries == 0;
37}
38
39static inline int timer_queue_full(struct timer_queue *tq)
40{
41 return tq->num_entries == tq->max_entries;
42}
43
44static inline struct timeout_callback *timer_queue_head(struct timer_queue *tq)
45{
46 if (timer_queue_empty(tq))
47 return NULL;
48 return tq->queue[0];
49}
50
51static int timer_queue_insert(struct timer_queue *tq,
52 struct timeout_callback *tocb)
53{
54 int index;
55
56 /* No more slots. */
57 if (timer_queue_full(tq))
58 return -1;
59
60 index = tq->num_entries;
61 tq->num_entries++;
62 tq->queue[index] = tocb;
63
64 while (index != 0) {
65 struct timeout_callback *parent;
66 int parent_index;
67
68 parent_index = (index - 1) / 2;
69 parent = tq->queue[parent_index];
70
71 /* All other ancestors are less than or equal to the current. */
72 if (mono_time_cmp(&parent->expiration, &tocb->expiration) <= 0)
73 break;
74
75 /* The parent is greater than current. Swap them. */
76 tq->queue[parent_index] = tocb;
77 tq->queue[index] = parent;
78
79 index = parent_index;
80 }
81
82 return 0;
83}
84
85/* Get the index containing the entry with smallest value. */
86static int timer_queue_min_child_index(struct timer_queue *tq, int index)
87{
88 int left_child_index;
89 int right_child_index;
90
91 left_child_index = 2 * index + 1;
92
93 if (left_child_index >= tq->num_entries)
94 return -1;
95
96 right_child_index = left_child_index + 1;
97
98 if (right_child_index >= tq->num_entries)
99 return left_child_index;
100
101 if (mono_time_cmp(&tq->queue[left_child_index]->expiration,
102 &tq->queue[right_child_index]->expiration) < 0) {
103 return left_child_index;
104 }
105 return right_child_index;
106}
107
108static void timer_queue_remove_head(struct timer_queue *tq)
109{
110 int index;
111 struct timeout_callback *tocb;
112
113 /* In order to remove the head the deepest child is replaced in the
114 * head slot and bubbled down the tree. */
115 tq->num_entries--;
116 tocb = tq->queue[tq->num_entries];
117 tq->queue[0] = tocb;
118
119 index = 0;
120 while (1) {
121 int min_child_index;
122 struct timeout_callback *child;
123
124 min_child_index = timer_queue_min_child_index(tq, index);
125
126 /* No more entries to compare against. */
127 if (min_child_index < 0)
128 break;
129
130 child = tq->queue[min_child_index];
131
132 /* Current index is the correct place since it is smaller or
133 * equal to the smallest child. */
134 if (mono_time_cmp(&tocb->expiration, &child->expiration) <= 0)
135 break;
136
137 /* Need to swap with smallest child. */
138 tq->queue[min_child_index] = tocb;
139 tq->queue[index] = child;
140
141 index = min_child_index;
142 }
143}
144
145static struct timeout_callback *
146timer_queue_expired(struct timer_queue *tq, struct mono_time *current_time)
147{
148 struct timeout_callback *tocb;
149
150 tocb = timer_queue_head(tq);
151
152 if (tocb == NULL)
153 return NULL;
154
155 /* The timeout callback hasn't expired yet. */
156 if (mono_time_before(current_time, &tocb->expiration))
157 return NULL;
158
159 timer_queue_remove_head(tq);
160
161 return tocb;
162}
163
164int timer_sched_callback(struct timeout_callback *tocb, unsigned long us)
165{
166 struct mono_time current_time;
167
168 if ((long)us< 0)
169 return -1;
170
171 timer_monotonic_get(&current_time);
172 tocb->expiration = current_time;
173 mono_time_add_usecs(&tocb->expiration, us);
174
175 /* The expiration overflowed. */
176 if (us != 0 && !mono_time_before(&current_time, &tocb->expiration))
177 return -1;
178
179 return timer_queue_insert(&global_timer_queue, tocb);
180}
181
182int timers_run(void)
183{
184 struct timeout_callback *tocb;
185 struct mono_time current_time;
186
187 timer_monotonic_get(&current_time);
188 tocb = timer_queue_expired(&global_timer_queue, &current_time);
189
190 if (tocb != NULL)
191 tocb->callback(tocb);
192
193 return !timer_queue_empty(&global_timer_queue);
194}