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