| 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 | |
| 9 | #include "qemu/osdep.h" |
| 10 | #include "hw/core/ptimer.h" |
| 11 | #include "migration/vmstate.h" |
| 12 | #include "qemu/host-utils.h" |
| 13 | #include "exec/replay-core.h" |
| 14 | #include "exec/icount.h" |
| 15 | #include "system/qtest.h" |
| 16 | #include "hw/core/clock.h" |
| 17 | |
| 18 | #define DELTA_ADJUST 1 |
| 19 | #define DELTA_NO_ADJUST -1 |
| 20 | |
| 21 | struct ptimer_state |
| 22 | { |
| 23 | uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */ |
| 24 | uint64_t limit; |
| 25 | uint64_t delta; |
| 26 | uint32_t period_frac; |
| 27 | int64_t period; |
| 28 | int64_t last_event; |
| 29 | int64_t next_event; |
| 30 | uint8_t policy_mask; |
| 31 | QEMUTimer *timer; |
| 32 | ptimer_cb callback; |
| 33 | void *callback_opaque; |
| 34 | /* |
| 35 | * These track whether we're in a transaction block, and if we |
| 36 | * need to do a timer reload when the block finishes. They don't |
| 37 | * need to be migrated because migration can never happen in the |
| 38 | * middle of a transaction block. |
| 39 | */ |
| 40 | bool in_transaction; |
| 41 | bool need_reload; |
| 42 | }; |
| 43 | |
| 44 | /* Use a bottom-half routine to avoid reentrancy issues. */ |
| 45 | static void ptimer_trigger(ptimer_state *s) |
| 46 | { |
| 47 | s->callback(s->callback_opaque); |
| 48 | } |
| 49 | |
| 50 | static void ptimer_reload(ptimer_state *s, int delta_adjust) |
| 51 | { |
| 52 | uint32_t period_frac; |
| 53 | uint64_t period; |
| 54 | uint64_t delta; |
| 55 | bool suppress_trigger = false; |
| 56 | |
| 57 | /* |
| 58 | * Note that if delta_adjust is 0 then we must be here because of |
| 59 | * a count register write or timer start, not because of timer expiry. |
| 60 | * In that case the policy might require us to suppress the timer trigger |
| 61 | * that we would otherwise generate for a zero delta. |
| 62 | */ |
| 63 | if (delta_adjust == 0 && |
| 64 | (s->policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT)) { |
| 65 | suppress_trigger = true; |
| 66 | } |
| 67 | if (s->delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER) |
| 68 | && !suppress_trigger) { |
| 69 | ptimer_trigger(s); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Note that ptimer_trigger() might call the device callback function, |
| 74 | * which can then modify timer state, so we must not cache any fields |
| 75 | * from ptimer_state until after we have called it. |
| 76 | */ |
| 77 | delta = s->delta; |
| 78 | period = s->period; |
| 79 | period_frac = s->period_frac; |
| 80 | |
| 81 | if (delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) { |
| 82 | delta = s->delta = s->limit; |
| 83 | } |
| 84 | |
| 85 | if (s->period == 0 && s->period_frac == 0) { |
| 86 | if (!qtest_enabled()) { |
| 87 | fprintf(stderr, "Timer with period zero, disabling\n"); |
| 88 | } |
| 89 | timer_del(s->timer); |
| 90 | s->enabled = 0; |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) { |
| 95 | if (delta_adjust != DELTA_NO_ADJUST) { |
| 96 | delta += delta_adjust; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (delta == 0 && (s->policy_mask & PTIMER_POLICY_CONTINUOUS_TRIGGER)) { |
| 101 | if (s->enabled == 1 && s->limit == 0) { |
| 102 | delta = 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) { |
| 107 | if (delta_adjust != DELTA_NO_ADJUST) { |
| 108 | delta = 1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) { |
| 113 | if (s->enabled == 1 && s->limit != 0) { |
| 114 | delta = 1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (delta == 0) { |
| 119 | if (s->enabled == 0) { |
| 120 | /* trigger callback disabled the timer already */ |
| 121 | return; |
| 122 | } |
| 123 | if (!qtest_enabled()) { |
| 124 | fprintf(stderr, "Timer with delta zero, disabling\n"); |
| 125 | } |
| 126 | timer_del(s->timer); |
| 127 | s->enabled = 0; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Artificially limit timeout rate to something |
| 133 | * achievable under QEMU. Otherwise, QEMU spends all |
| 134 | * its time generating timer interrupts, and there |
| 135 | * is no forward progress. |
| 136 | * About ten microseconds is the fastest that really works |
| 137 | * on the current generation of host machines. |
| 138 | */ |
| 139 | |
| 140 | if (s->enabled == 1 && (delta * period < 10000) && |
| 141 | !icount_enabled() && !qtest_enabled()) { |
| 142 | period = 10000 / delta; |
| 143 | period_frac = 0; |
| 144 | } |
| 145 | |
| 146 | s->last_event = s->next_event; |
| 147 | s->next_event = s->last_event + delta * period; |
| 148 | if (period_frac) { |
| 149 | s->next_event += ((int64_t)period_frac * delta) >> 32; |
| 150 | } |
| 151 | timer_mod(s->timer, s->next_event); |
| 152 | } |
| 153 | |
| 154 | static void ptimer_tick(void *opaque) |
| 155 | { |
| 156 | ptimer_state *s = (ptimer_state *)opaque; |
| 157 | bool trigger = true; |
| 158 | |
| 159 | /* |
| 160 | * We perform all the tick actions within a begin/commit block |
| 161 | * because the callback function that ptimer_trigger() calls |
| 162 | * might make calls into the ptimer APIs that provoke another |
| 163 | * trigger, and we want that to cause the callback function |
| 164 | * to be called iteratively, not recursively. |
| 165 | */ |
| 166 | ptimer_transaction_begin(s); |
| 167 | |
| 168 | if (s->enabled == 2) { |
| 169 | s->delta = 0; |
| 170 | s->enabled = 0; |
| 171 | } else { |
| 172 | int delta_adjust = DELTA_ADJUST; |
| 173 | |
| 174 | if (s->delta == 0 || s->limit == 0) { |
| 175 | /* If a "continuous trigger" policy is not used and limit == 0, |
| 176 | we should error out. delta == 0 means that this tick is |
| 177 | caused by a "no immediate reload" policy, so it shouldn't |
| 178 | be adjusted. */ |
| 179 | delta_adjust = DELTA_NO_ADJUST; |
| 180 | } |
| 181 | |
| 182 | if (!(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) { |
| 183 | /* Avoid re-trigger on deferred reload if "no immediate trigger" |
| 184 | policy isn't used. */ |
| 185 | trigger = (delta_adjust == DELTA_ADJUST); |
| 186 | } |
| 187 | |
| 188 | s->delta = s->limit; |
| 189 | |
| 190 | ptimer_reload(s, delta_adjust); |
| 191 | } |
| 192 | |
| 193 | if (trigger) { |
| 194 | ptimer_trigger(s); |
| 195 | } |
| 196 | |
| 197 | ptimer_transaction_commit(s); |
| 198 | } |
| 199 | |
| 200 | uint64_t ptimer_get_count(ptimer_state *s) |
| 201 | { |
| 202 | uint64_t counter; |
| 203 | |
| 204 | if (s->enabled && s->delta != 0) { |
| 205 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 206 | int64_t next = s->next_event; |
| 207 | int64_t last = s->last_event; |
| 208 | bool expired = (now - next >= 0); |
| 209 | bool oneshot = (s->enabled == 2); |
| 210 | |
| 211 | /* Figure out the current counter value. */ |
| 212 | if (expired) { |
| 213 | /* Prevent timer underflowing if it should already have |
| 214 | triggered. */ |
| 215 | counter = 0; |
| 216 | } else { |
| 217 | uint64_t rem; |
| 218 | uint64_t div; |
| 219 | int clz1, clz2; |
| 220 | int shift; |
| 221 | uint32_t period_frac = s->period_frac; |
| 222 | uint64_t period = s->period; |
| 223 | |
| 224 | if (!oneshot && (s->delta * period < 10000) && |
| 225 | !icount_enabled() && !qtest_enabled()) { |
| 226 | period = 10000 / s->delta; |
| 227 | period_frac = 0; |
| 228 | } |
| 229 | |
| 230 | /* We need to divide time by period, where time is stored in |
| 231 | rem (64-bit integer) and period is stored in period/period_frac |
| 232 | (64.32 fixed point). |
| 233 | |
| 234 | Doing full precision division is hard, so scale values and |
| 235 | do a 64-bit division. The result should be rounded down, |
| 236 | so that the rounding error never causes the timer to go |
| 237 | backwards. |
| 238 | */ |
| 239 | |
| 240 | rem = next - now; |
| 241 | div = period; |
| 242 | |
| 243 | clz1 = clz64(rem); |
| 244 | clz2 = clz64(div); |
| 245 | shift = clz1 < clz2 ? clz1 : clz2; |
| 246 | |
| 247 | rem <<= shift; |
| 248 | div <<= shift; |
| 249 | if (shift >= 32) { |
| 250 | div |= ((uint64_t)period_frac << (shift - 32)); |
| 251 | } else { |
| 252 | if (shift != 0) |
| 253 | div |= (period_frac >> (32 - shift)); |
| 254 | /* Look at remaining bits of period_frac and round div up if |
| 255 | necessary. */ |
| 256 | if ((uint32_t)(period_frac << shift)) |
| 257 | div += 1; |
| 258 | } |
| 259 | counter = rem / div; |
| 260 | |
| 261 | if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) { |
| 262 | /* Before wrapping around, timer should stay with counter = 0 |
| 263 | for a one period. */ |
| 264 | if (!oneshot && s->delta == s->limit) { |
| 265 | if (now == last) { |
| 266 | /* Counter == delta here, check whether it was |
| 267 | adjusted and if it was, then right now it is |
| 268 | that "one period". */ |
| 269 | if (counter == s->limit + DELTA_ADJUST) { |
| 270 | return 0; |
| 271 | } |
| 272 | } else if (counter == s->limit) { |
| 273 | /* Since the counter is rounded down and now != last, |
| 274 | the counter == limit means that delta was adjusted |
| 275 | by +1 and right now it is that adjusted period. */ |
| 276 | return 0; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (s->policy_mask & PTIMER_POLICY_NO_COUNTER_ROUND_DOWN) { |
| 283 | /* If now == last then delta == limit, i.e. the counter already |
| 284 | represents the correct value. It would be rounded down a 1ns |
| 285 | later. */ |
| 286 | if (now != last) { |
| 287 | counter += 1; |
| 288 | } |
| 289 | } |
| 290 | } else { |
| 291 | counter = s->delta; |
| 292 | } |
| 293 | return counter; |
| 294 | } |
| 295 | |
| 296 | void ptimer_set_count(ptimer_state *s, uint64_t count) |
| 297 | { |
| 298 | assert(s->in_transaction); |
| 299 | s->delta = count; |
| 300 | if (s->enabled) { |
| 301 | s->need_reload = true; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void ptimer_run(ptimer_state *s, int oneshot) |
| 306 | { |
| 307 | bool was_disabled = !s->enabled; |
| 308 | |
| 309 | assert(s->in_transaction); |
| 310 | |
| 311 | if (was_disabled && s->period == 0 && s->period_frac == 0) { |
| 312 | if (!qtest_enabled()) { |
| 313 | fprintf(stderr, "Timer with period zero, disabling\n"); |
| 314 | } |
| 315 | return; |
| 316 | } |
| 317 | s->enabled = oneshot ? 2 : 1; |
| 318 | if (was_disabled) { |
| 319 | s->need_reload = true; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* Pause a timer. Note that this may cause it to "lose" time, even if it |
| 324 | is immediately restarted. */ |
| 325 | void ptimer_stop(ptimer_state *s) |
| 326 | { |
| 327 | assert(s->in_transaction); |
| 328 | |
| 329 | if (!s->enabled) |
| 330 | return; |
| 331 | |
| 332 | s->delta = ptimer_get_count(s); |
| 333 | timer_del(s->timer); |
| 334 | s->enabled = 0; |
| 335 | s->need_reload = false; |
| 336 | } |
| 337 | |
| 338 | /* Set counter increment interval in nanoseconds. */ |
| 339 | void ptimer_set_period(ptimer_state *s, int64_t period) |
| 340 | { |
| 341 | assert(s->in_transaction); |
| 342 | s->delta = ptimer_get_count(s); |
| 343 | s->period = period; |
| 344 | s->period_frac = 0; |
| 345 | if (s->enabled) { |
| 346 | s->need_reload = true; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* Set counter increment interval from a Clock */ |
| 351 | void ptimer_set_period_from_clock(ptimer_state *s, const Clock *clk, |
| 352 | unsigned int divisor) |
| 353 | { |
| 354 | /* |
| 355 | * The raw clock period is a 64-bit value in units of 2^-32 ns; |
| 356 | * put another way it's a 32.32 fixed-point ns value. Our internal |
| 357 | * representation of the period is 64.32 fixed point ns, so |
| 358 | * the conversion is simple. |
| 359 | */ |
| 360 | uint64_t raw_period = clock_get(clk); |
| 361 | uint64_t period_frac; |
| 362 | |
| 363 | assert(s->in_transaction); |
| 364 | s->delta = ptimer_get_count(s); |
| 365 | s->period = extract64(raw_period, 32, 32); |
| 366 | period_frac = extract64(raw_period, 0, 32); |
| 367 | /* |
| 368 | * divisor specifies a possible frequency divisor between the |
| 369 | * clock and the timer, so it is a multiplier on the period. |
| 370 | * We do the multiply after splitting the raw period out into |
| 371 | * period and frac to avoid having to do a 32*64->96 multiply. |
| 372 | */ |
| 373 | s->period *= divisor; |
| 374 | period_frac *= divisor; |
| 375 | s->period += extract64(period_frac, 32, 32); |
| 376 | s->period_frac = (uint32_t)period_frac; |
| 377 | |
| 378 | if (s->enabled) { |
| 379 | s->need_reload = true; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /* Set counter frequency in Hz. */ |
| 384 | void ptimer_set_freq(ptimer_state *s, uint32_t freq) |
| 385 | { |
| 386 | assert(s->in_transaction); |
| 387 | s->delta = ptimer_get_count(s); |
| 388 | s->period = 1000000000ll / freq; |
| 389 | s->period_frac = (1000000000ll << 32) / freq; |
| 390 | if (s->enabled) { |
| 391 | s->need_reload = true; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /* Set the initial countdown value. If reload is nonzero then also set |
| 396 | count = limit. */ |
| 397 | void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload) |
| 398 | { |
| 399 | assert(s->in_transaction); |
| 400 | s->limit = limit; |
| 401 | if (reload) |
| 402 | s->delta = limit; |
| 403 | if (s->enabled && reload) { |
| 404 | s->need_reload = true; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | uint64_t ptimer_get_limit(ptimer_state *s) |
| 409 | { |
| 410 | return s->limit; |
| 411 | } |
| 412 | |
| 413 | void ptimer_transaction_begin(ptimer_state *s) |
| 414 | { |
| 415 | assert(!s->in_transaction); |
| 416 | s->in_transaction = true; |
| 417 | s->need_reload = false; |
| 418 | } |
| 419 | |
| 420 | void ptimer_transaction_commit(ptimer_state *s) |
| 421 | { |
| 422 | assert(s->in_transaction); |
| 423 | /* |
| 424 | * We must loop here because ptimer_reload() can call the callback |
| 425 | * function, which might then update ptimer state in a way that |
| 426 | * means we need to do another reload and possibly another callback. |
| 427 | * A disabled timer never needs reloading (and if we don't check |
| 428 | * this then we loop forever if ptimer_reload() disables the timer). |
| 429 | */ |
| 430 | while (s->need_reload && s->enabled) { |
| 431 | s->need_reload = false; |
| 432 | s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 433 | ptimer_reload(s, 0); |
| 434 | } |
| 435 | /* Now we've finished reload we can leave the transaction block. */ |
| 436 | s->in_transaction = false; |
| 437 | } |
| 438 | |
| 439 | const VMStateDescription vmstate_ptimer = { |
| 440 | .name = "ptimer", |
| 441 | .version_id = 1, |
| 442 | .minimum_version_id = 1, |
| 443 | .fields = (const VMStateField[]) { |
| 444 | VMSTATE_UINT8(enabled, ptimer_state), |
| 445 | VMSTATE_UINT64(limit, ptimer_state), |
| 446 | VMSTATE_UINT64(delta, ptimer_state), |
| 447 | VMSTATE_UINT32(period_frac, ptimer_state), |
| 448 | VMSTATE_INT64(period, ptimer_state), |
| 449 | VMSTATE_INT64(last_event, ptimer_state), |
| 450 | VMSTATE_INT64(next_event, ptimer_state), |
| 451 | VMSTATE_TIMER_PTR(timer, ptimer_state), |
| 452 | VMSTATE_END_OF_LIST() |
| 453 | } |
| 454 | }; |
| 455 | |
| 456 | ptimer_state *ptimer_init(ptimer_cb callback, void *callback_opaque, |
| 457 | uint8_t policy_mask) |
| 458 | { |
| 459 | ptimer_state *s; |
| 460 | |
| 461 | /* The callback function is mandatory. */ |
| 462 | assert(callback); |
| 463 | |
| 464 | s = g_new0(ptimer_state, 1); |
| 465 | s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s); |
| 466 | s->policy_mask = policy_mask; |
| 467 | s->callback = callback; |
| 468 | s->callback_opaque = callback_opaque; |
| 469 | |
| 470 | /* |
| 471 | * These two policies are incompatible -- trigger-on-decrement implies |
| 472 | * a timer trigger when the count becomes 0, but no-immediate-trigger |
| 473 | * implies a trigger when the count stops being 0. |
| 474 | */ |
| 475 | assert(!((policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) && |
| 476 | (policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER))); |
| 477 | return s; |
| 478 | } |
| 479 | |
| 480 | void ptimer_free(ptimer_state *s) |
| 481 | { |
| 482 | timer_free(s->timer); |
| 483 | g_free(s); |
| 484 | } |