1 /* 2 * General purpose implementation of a simple periodic countdown timer. 3 * 4 * Copyright (c) 2007 CodeSourcery. 5 * 6 * This code is licensed under the GNU LGPL. 7 */ 8 #include "qemu/osdep.h" 9 #include "hw/hw.h" 10 #include "qemu/timer.h" 11 #include "hw/ptimer.h" 12 #include "qemu/host-utils.h" 13 #include "sysemu/replay.h" 14 15 struct ptimer_state 16 { 17 uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */ 18 uint64_t limit; 19 uint64_t delta; 20 uint32_t period_frac; 21 int64_t period; 22 int64_t last_event; 23 int64_t next_event; 24 QEMUBH *bh; 25 QEMUTimer *timer; 26 }; 27 28 /* Use a bottom-half routine to avoid reentrancy issues. */ 29 static void ptimer_trigger(ptimer_state *s) 30 { 31 if (s->bh) { 32 replay_bh_schedule_event(s->bh); 33 } 34 } 35 36 static void ptimer_reload(ptimer_state *s) 37 { 38 uint32_t period_frac = s->period_frac; 39 uint64_t period = s->period; 40 41 if (s->delta == 0) { 42 ptimer_trigger(s); 43 s->delta = s->limit; 44 } 45 if (s->delta == 0 || s->period == 0) { 46 fprintf(stderr, "Timer with period zero, disabling\n"); 47 s->enabled = 0; 48 return; 49 } 50 51 /* 52 * Artificially limit timeout rate to something 53 * achievable under QEMU. Otherwise, QEMU spends all 54 * its time generating timer interrupts, and there 55 * is no forward progress. 56 * About ten microseconds is the fastest that really works 57 * on the current generation of host machines. 58 */ 59 60 if (s->enabled == 1 && (s->delta * period < 10000) && !use_icount) { 61 period = 10000 / s->delta; 62 period_frac = 0; 63 } 64 65 s->last_event = s->next_event; 66 s->next_event = s->last_event + s->delta * period; 67 if (period_frac) { 68 s->next_event += ((int64_t)period_frac * s->delta) >> 32; 69 } 70 timer_mod(s->timer, s->next_event); 71 } 72 73 static void ptimer_tick(void *opaque) 74 { 75 ptimer_state *s = (ptimer_state *)opaque; 76 ptimer_trigger(s); 77 s->delta = 0; 78 if (s->enabled == 2) { 79 s->enabled = 0; 80 } else { 81 ptimer_reload(s); 82 } 83 } 84 85 uint64_t ptimer_get_count(ptimer_state *s) 86 { 87 uint64_t counter; 88 89 if (s->enabled) { 90 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 91 int64_t next = s->next_event; 92 bool expired = (now - next >= 0); 93 bool oneshot = (s->enabled == 2); 94 95 /* Figure out the current counter value. */ 96 if (s->period == 0 || (expired && (oneshot || use_icount))) { 97 /* Prevent timer underflowing if it should already have 98 triggered. */ 99 counter = 0; 100 } else { 101 uint64_t rem; 102 uint64_t div; 103 int clz1, clz2; 104 int shift; 105 uint32_t period_frac = s->period_frac; 106 uint64_t period = s->period; 107 108 if (!oneshot && (s->delta * period < 10000) && !use_icount) { 109 period = 10000 / s->delta; 110 period_frac = 0; 111 } 112 113 /* We need to divide time by period, where time is stored in 114 rem (64-bit integer) and period is stored in period/period_frac 115 (64.32 fixed point). 116 117 Doing full precision division is hard, so scale values and 118 do a 64-bit division. The result should be rounded down, 119 so that the rounding error never causes the timer to go 120 backwards. 121 */ 122 123 rem = expired ? now - next : next - now; 124 div = period; 125 126 clz1 = clz64(rem); 127 clz2 = clz64(div); 128 shift = clz1 < clz2 ? clz1 : clz2; 129 130 rem <<= shift; 131 div <<= shift; 132 if (shift >= 32) { 133 div |= ((uint64_t)period_frac << (shift - 32)); 134 } else { 135 if (shift != 0) 136 div |= (period_frac >> (32 - shift)); 137 /* Look at remaining bits of period_frac and round div up if 138 necessary. */ 139 if ((uint32_t)(period_frac << shift)) 140 div += 1; 141 } 142 counter = rem / div; 143 144 if (expired && counter != 0) { 145 /* Wrap around periodic counter. */ 146 counter = s->limit - (counter - 1) % s->limit; 147 } 148 } 149 } else { 150 counter = s->delta; 151 } 152 return counter; 153 } 154 155 void ptimer_set_count(ptimer_state *s, uint64_t count) 156 { 157 s->delta = count; 158 if (s->enabled) { 159 s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 160 ptimer_reload(s); 161 } 162 } 163 164 void ptimer_run(ptimer_state *s, int oneshot) 165 { 166 bool was_disabled = !s->enabled; 167 168 if (was_disabled && s->period == 0) { 169 fprintf(stderr, "Timer with period zero, disabling\n"); 170 return; 171 } 172 s->enabled = oneshot ? 2 : 1; 173 if (was_disabled) { 174 s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 175 ptimer_reload(s); 176 } 177 } 178 179 /* Pause a timer. Note that this may cause it to "lose" time, even if it 180 is immediately restarted. */ 181 void ptimer_stop(ptimer_state *s) 182 { 183 if (!s->enabled) 184 return; 185 186 s->delta = ptimer_get_count(s); 187 timer_del(s->timer); 188 s->enabled = 0; 189 } 190 191 /* Set counter increment interval in nanoseconds. */ 192 void ptimer_set_period(ptimer_state *s, int64_t period) 193 { 194 s->delta = ptimer_get_count(s); 195 s->period = period; 196 s->period_frac = 0; 197 if (s->enabled) { 198 s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 199 ptimer_reload(s); 200 } 201 } 202 203 /* Set counter frequency in Hz. */ 204 void ptimer_set_freq(ptimer_state *s, uint32_t freq) 205 { 206 s->delta = ptimer_get_count(s); 207 s->period = 1000000000ll / freq; 208 s->period_frac = (1000000000ll << 32) / freq; 209 if (s->enabled) { 210 s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 211 ptimer_reload(s); 212 } 213 } 214 215 /* Set the initial countdown value. If reload is nonzero then also set 216 count = limit. */ 217 void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload) 218 { 219 s->limit = limit; 220 if (reload) 221 s->delta = limit; 222 if (s->enabled && reload) { 223 s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 224 ptimer_reload(s); 225 } 226 } 227 228 uint64_t ptimer_get_limit(ptimer_state *s) 229 { 230 return s->limit; 231 } 232 233 const VMStateDescription vmstate_ptimer = { 234 .name = "ptimer", 235 .version_id = 1, 236 .minimum_version_id = 1, 237 .fields = (VMStateField[]) { 238 VMSTATE_UINT8(enabled, ptimer_state), 239 VMSTATE_UINT64(limit, ptimer_state), 240 VMSTATE_UINT64(delta, ptimer_state), 241 VMSTATE_UINT32(period_frac, ptimer_state), 242 VMSTATE_INT64(period, ptimer_state), 243 VMSTATE_INT64(last_event, ptimer_state), 244 VMSTATE_INT64(next_event, ptimer_state), 245 VMSTATE_TIMER_PTR(timer, ptimer_state), 246 VMSTATE_END_OF_LIST() 247 } 248 }; 249 250 ptimer_state *ptimer_init(QEMUBH *bh) 251 { 252 ptimer_state *s; 253 254 s = (ptimer_state *)g_malloc0(sizeof(ptimer_state)); 255 s->bh = bh; 256 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s); 257 return s; 258 } 259