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