1423f0742Spbrook /* 2423f0742Spbrook * General purpose implementation of a simple periodic countdown timer. 3423f0742Spbrook * 4423f0742Spbrook * Copyright (c) 2007 CodeSourcery. 5423f0742Spbrook * 68e31bf38SMatthew Fernandez * This code is licensed under the GNU LGPL. 7423f0742Spbrook */ 883c9f4caSPaolo Bonzini #include "hw/hw.h" 91de7afc9SPaolo Bonzini #include "qemu/timer.h" 1083c9f4caSPaolo Bonzini #include "hw/ptimer.h" 111de7afc9SPaolo Bonzini #include "qemu/host-utils.h" 12*8a354bd9SPavel Dovgalyuk #include "sysemu/replay.h" 13423f0742Spbrook 14423f0742Spbrook struct ptimer_state 15423f0742Spbrook { 16852f771eSJuan Quintela uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */ 178d05ea8aSblueswir1 uint64_t limit; 188d05ea8aSblueswir1 uint64_t delta; 19423f0742Spbrook uint32_t period_frac; 20423f0742Spbrook int64_t period; 21423f0742Spbrook int64_t last_event; 22423f0742Spbrook int64_t next_event; 23423f0742Spbrook QEMUBH *bh; 24423f0742Spbrook QEMUTimer *timer; 25423f0742Spbrook }; 26423f0742Spbrook 27423f0742Spbrook /* Use a bottom-half routine to avoid reentrancy issues. */ 28423f0742Spbrook static void ptimer_trigger(ptimer_state *s) 29423f0742Spbrook { 30423f0742Spbrook if (s->bh) { 31*8a354bd9SPavel Dovgalyuk replay_bh_schedule_event(s->bh); 32423f0742Spbrook } 33423f0742Spbrook } 34423f0742Spbrook 35423f0742Spbrook static void ptimer_reload(ptimer_state *s) 36423f0742Spbrook { 37423f0742Spbrook if (s->delta == 0) { 38423f0742Spbrook ptimer_trigger(s); 39423f0742Spbrook s->delta = s->limit; 40423f0742Spbrook } 41423f0742Spbrook if (s->delta == 0 || s->period == 0) { 42423f0742Spbrook fprintf(stderr, "Timer with period zero, disabling\n"); 43423f0742Spbrook s->enabled = 0; 44423f0742Spbrook return; 45423f0742Spbrook } 46423f0742Spbrook 47423f0742Spbrook s->last_event = s->next_event; 48423f0742Spbrook s->next_event = s->last_event + s->delta * s->period; 49423f0742Spbrook if (s->period_frac) { 50423f0742Spbrook s->next_event += ((int64_t)s->period_frac * s->delta) >> 32; 51423f0742Spbrook } 52bc72ad67SAlex Bligh timer_mod(s->timer, s->next_event); 53423f0742Spbrook } 54423f0742Spbrook 55423f0742Spbrook static void ptimer_tick(void *opaque) 56423f0742Spbrook { 57423f0742Spbrook ptimer_state *s = (ptimer_state *)opaque; 58423f0742Spbrook ptimer_trigger(s); 59423f0742Spbrook s->delta = 0; 60423f0742Spbrook if (s->enabled == 2) { 61423f0742Spbrook s->enabled = 0; 62423f0742Spbrook } else { 63423f0742Spbrook ptimer_reload(s); 64423f0742Spbrook } 65423f0742Spbrook } 66423f0742Spbrook 678d05ea8aSblueswir1 uint64_t ptimer_get_count(ptimer_state *s) 68423f0742Spbrook { 69423f0742Spbrook int64_t now; 708d05ea8aSblueswir1 uint64_t counter; 71423f0742Spbrook 72423f0742Spbrook if (s->enabled) { 73bc72ad67SAlex Bligh now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 74423f0742Spbrook /* Figure out the current counter value. */ 75423f0742Spbrook if (now - s->next_event > 0 76423f0742Spbrook || s->period == 0) { 77423f0742Spbrook /* Prevent timer underflowing if it should already have 78423f0742Spbrook triggered. */ 79423f0742Spbrook counter = 0; 80423f0742Spbrook } else { 818d05ea8aSblueswir1 uint64_t rem; 828d05ea8aSblueswir1 uint64_t div; 83d0a981b2Spbrook int clz1, clz2; 84d0a981b2Spbrook int shift; 85d0a981b2Spbrook 86d0a981b2Spbrook /* We need to divide time by period, where time is stored in 87d0a981b2Spbrook rem (64-bit integer) and period is stored in period/period_frac 88d0a981b2Spbrook (64.32 fixed point). 89d0a981b2Spbrook 90d0a981b2Spbrook Doing full precision division is hard, so scale values and 91d0a981b2Spbrook do a 64-bit division. The result should be rounded down, 92d0a981b2Spbrook so that the rounding error never causes the timer to go 93d0a981b2Spbrook backwards. 94d0a981b2Spbrook */ 95423f0742Spbrook 96423f0742Spbrook rem = s->next_event - now; 97423f0742Spbrook div = s->period; 98d0a981b2Spbrook 99d0a981b2Spbrook clz1 = clz64(rem); 100d0a981b2Spbrook clz2 = clz64(div); 101d0a981b2Spbrook shift = clz1 < clz2 ? clz1 : clz2; 102d0a981b2Spbrook 103d0a981b2Spbrook rem <<= shift; 104d0a981b2Spbrook div <<= shift; 105d0a981b2Spbrook if (shift >= 32) { 106d0a981b2Spbrook div |= ((uint64_t)s->period_frac << (shift - 32)); 107d0a981b2Spbrook } else { 108d0a981b2Spbrook if (shift != 0) 109d0a981b2Spbrook div |= (s->period_frac >> (32 - shift)); 110d0a981b2Spbrook /* Look at remaining bits of period_frac and round div up if 111d0a981b2Spbrook necessary. */ 112d0a981b2Spbrook if ((uint32_t)(s->period_frac << shift)) 113d0a981b2Spbrook div += 1; 114d0a981b2Spbrook } 115423f0742Spbrook counter = rem / div; 116423f0742Spbrook } 117423f0742Spbrook } else { 118423f0742Spbrook counter = s->delta; 119423f0742Spbrook } 120423f0742Spbrook return counter; 121423f0742Spbrook } 122423f0742Spbrook 1238d05ea8aSblueswir1 void ptimer_set_count(ptimer_state *s, uint64_t count) 124423f0742Spbrook { 125423f0742Spbrook s->delta = count; 126423f0742Spbrook if (s->enabled) { 127bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 128423f0742Spbrook ptimer_reload(s); 129423f0742Spbrook } 130423f0742Spbrook } 131423f0742Spbrook 132423f0742Spbrook void ptimer_run(ptimer_state *s, int oneshot) 133423f0742Spbrook { 13498fc5614Spbrook if (s->enabled) { 13598fc5614Spbrook return; 13698fc5614Spbrook } 137423f0742Spbrook if (s->period == 0) { 138423f0742Spbrook fprintf(stderr, "Timer with period zero, disabling\n"); 139423f0742Spbrook return; 140423f0742Spbrook } 141423f0742Spbrook s->enabled = oneshot ? 2 : 1; 142bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 143423f0742Spbrook ptimer_reload(s); 144423f0742Spbrook } 145423f0742Spbrook 1468d05ea8aSblueswir1 /* Pause a timer. Note that this may cause it to "lose" time, even if it 147423f0742Spbrook is immediately restarted. */ 148423f0742Spbrook void ptimer_stop(ptimer_state *s) 149423f0742Spbrook { 150423f0742Spbrook if (!s->enabled) 151423f0742Spbrook return; 152423f0742Spbrook 153423f0742Spbrook s->delta = ptimer_get_count(s); 154bc72ad67SAlex Bligh timer_del(s->timer); 155423f0742Spbrook s->enabled = 0; 156423f0742Spbrook } 157423f0742Spbrook 158423f0742Spbrook /* Set counter increment interval in nanoseconds. */ 159423f0742Spbrook void ptimer_set_period(ptimer_state *s, int64_t period) 160423f0742Spbrook { 161423f0742Spbrook s->period = period; 162423f0742Spbrook s->period_frac = 0; 1638d05ea8aSblueswir1 if (s->enabled) { 164bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1658d05ea8aSblueswir1 ptimer_reload(s); 1668d05ea8aSblueswir1 } 167423f0742Spbrook } 168423f0742Spbrook 169423f0742Spbrook /* Set counter frequency in Hz. */ 170423f0742Spbrook void ptimer_set_freq(ptimer_state *s, uint32_t freq) 171423f0742Spbrook { 172423f0742Spbrook s->period = 1000000000ll / freq; 173423f0742Spbrook s->period_frac = (1000000000ll << 32) / freq; 1748d05ea8aSblueswir1 if (s->enabled) { 175bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1768d05ea8aSblueswir1 ptimer_reload(s); 1778d05ea8aSblueswir1 } 178423f0742Spbrook } 179423f0742Spbrook 180423f0742Spbrook /* Set the initial countdown value. If reload is nonzero then also set 181423f0742Spbrook count = limit. */ 1828d05ea8aSblueswir1 void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload) 183423f0742Spbrook { 184cf36b31dSPeter Chubb /* 185cf36b31dSPeter Chubb * Artificially limit timeout rate to something 186cf36b31dSPeter Chubb * achievable under QEMU. Otherwise, QEMU spends all 187cf36b31dSPeter Chubb * its time generating timer interrupts, and there 188cf36b31dSPeter Chubb * is no forward progress. 189cf36b31dSPeter Chubb * About ten microseconds is the fastest that really works 190cf36b31dSPeter Chubb * on the current generation of host machines. 191cf36b31dSPeter Chubb */ 192cf36b31dSPeter Chubb 193f8340b36SEdgar E. Iglesias if (!use_icount && limit * s->period < 10000 && s->period) { 194cf36b31dSPeter Chubb limit = 10000 / s->period; 195cf36b31dSPeter Chubb } 196cf36b31dSPeter Chubb 197423f0742Spbrook s->limit = limit; 198423f0742Spbrook if (reload) 199423f0742Spbrook s->delta = limit; 20062ea5b0bSpbrook if (s->enabled && reload) { 201bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 2028d05ea8aSblueswir1 ptimer_reload(s); 2038d05ea8aSblueswir1 } 2048d05ea8aSblueswir1 } 2058d05ea8aSblueswir1 206852f771eSJuan Quintela const VMStateDescription vmstate_ptimer = { 20755a6e51fSBlue Swirl .name = "ptimer", 208852f771eSJuan Quintela .version_id = 1, 209852f771eSJuan Quintela .minimum_version_id = 1, 210852f771eSJuan Quintela .fields = (VMStateField[]) { 211852f771eSJuan Quintela VMSTATE_UINT8(enabled, ptimer_state), 212852f771eSJuan Quintela VMSTATE_UINT64(limit, ptimer_state), 213852f771eSJuan Quintela VMSTATE_UINT64(delta, ptimer_state), 214852f771eSJuan Quintela VMSTATE_UINT32(period_frac, ptimer_state), 215852f771eSJuan Quintela VMSTATE_INT64(period, ptimer_state), 216852f771eSJuan Quintela VMSTATE_INT64(last_event, ptimer_state), 217852f771eSJuan Quintela VMSTATE_INT64(next_event, ptimer_state), 218e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(timer, ptimer_state), 219852f771eSJuan Quintela VMSTATE_END_OF_LIST() 220852f771eSJuan Quintela } 22155a6e51fSBlue Swirl }; 22255a6e51fSBlue Swirl 223423f0742Spbrook ptimer_state *ptimer_init(QEMUBH *bh) 224423f0742Spbrook { 225423f0742Spbrook ptimer_state *s; 226423f0742Spbrook 2277267c094SAnthony Liguori s = (ptimer_state *)g_malloc0(sizeof(ptimer_state)); 228423f0742Spbrook s->bh = bh; 229bc72ad67SAlex Bligh s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s); 230423f0742Spbrook return s; 231423f0742Spbrook } 232