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" 12423f0742Spbrook 13423f0742Spbrook struct ptimer_state 14423f0742Spbrook { 15852f771eSJuan Quintela uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */ 168d05ea8aSblueswir1 uint64_t limit; 178d05ea8aSblueswir1 uint64_t delta; 18423f0742Spbrook uint32_t period_frac; 19423f0742Spbrook int64_t period; 20423f0742Spbrook int64_t last_event; 21423f0742Spbrook int64_t next_event; 22423f0742Spbrook QEMUBH *bh; 23423f0742Spbrook QEMUTimer *timer; 24423f0742Spbrook }; 25423f0742Spbrook 26423f0742Spbrook /* Use a bottom-half routine to avoid reentrancy issues. */ 27423f0742Spbrook static void ptimer_trigger(ptimer_state *s) 28423f0742Spbrook { 29423f0742Spbrook if (s->bh) { 30423f0742Spbrook qemu_bh_schedule(s->bh); 31423f0742Spbrook } 32423f0742Spbrook } 33423f0742Spbrook 34423f0742Spbrook static void ptimer_reload(ptimer_state *s) 35423f0742Spbrook { 36423f0742Spbrook if (s->delta == 0) { 37423f0742Spbrook ptimer_trigger(s); 38423f0742Spbrook s->delta = s->limit; 39423f0742Spbrook } 40423f0742Spbrook if (s->delta == 0 || s->period == 0) { 41423f0742Spbrook fprintf(stderr, "Timer with period zero, disabling\n"); 42423f0742Spbrook s->enabled = 0; 43423f0742Spbrook return; 44423f0742Spbrook } 45423f0742Spbrook 46423f0742Spbrook s->last_event = s->next_event; 47423f0742Spbrook s->next_event = s->last_event + s->delta * s->period; 48423f0742Spbrook if (s->period_frac) { 49423f0742Spbrook s->next_event += ((int64_t)s->period_frac * s->delta) >> 32; 50423f0742Spbrook } 51*bc72ad67SAlex Bligh timer_mod(s->timer, s->next_event); 52423f0742Spbrook } 53423f0742Spbrook 54423f0742Spbrook static void ptimer_tick(void *opaque) 55423f0742Spbrook { 56423f0742Spbrook ptimer_state *s = (ptimer_state *)opaque; 57423f0742Spbrook ptimer_trigger(s); 58423f0742Spbrook s->delta = 0; 59423f0742Spbrook if (s->enabled == 2) { 60423f0742Spbrook s->enabled = 0; 61423f0742Spbrook } else { 62423f0742Spbrook ptimer_reload(s); 63423f0742Spbrook } 64423f0742Spbrook } 65423f0742Spbrook 668d05ea8aSblueswir1 uint64_t ptimer_get_count(ptimer_state *s) 67423f0742Spbrook { 68423f0742Spbrook int64_t now; 698d05ea8aSblueswir1 uint64_t counter; 70423f0742Spbrook 71423f0742Spbrook if (s->enabled) { 72*bc72ad67SAlex Bligh now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 73423f0742Spbrook /* Figure out the current counter value. */ 74423f0742Spbrook if (now - s->next_event > 0 75423f0742Spbrook || s->period == 0) { 76423f0742Spbrook /* Prevent timer underflowing if it should already have 77423f0742Spbrook triggered. */ 78423f0742Spbrook counter = 0; 79423f0742Spbrook } else { 808d05ea8aSblueswir1 uint64_t rem; 818d05ea8aSblueswir1 uint64_t div; 82d0a981b2Spbrook int clz1, clz2; 83d0a981b2Spbrook int shift; 84d0a981b2Spbrook 85d0a981b2Spbrook /* We need to divide time by period, where time is stored in 86d0a981b2Spbrook rem (64-bit integer) and period is stored in period/period_frac 87d0a981b2Spbrook (64.32 fixed point). 88d0a981b2Spbrook 89d0a981b2Spbrook Doing full precision division is hard, so scale values and 90d0a981b2Spbrook do a 64-bit division. The result should be rounded down, 91d0a981b2Spbrook so that the rounding error never causes the timer to go 92d0a981b2Spbrook backwards. 93d0a981b2Spbrook */ 94423f0742Spbrook 95423f0742Spbrook rem = s->next_event - now; 96423f0742Spbrook div = s->period; 97d0a981b2Spbrook 98d0a981b2Spbrook clz1 = clz64(rem); 99d0a981b2Spbrook clz2 = clz64(div); 100d0a981b2Spbrook shift = clz1 < clz2 ? clz1 : clz2; 101d0a981b2Spbrook 102d0a981b2Spbrook rem <<= shift; 103d0a981b2Spbrook div <<= shift; 104d0a981b2Spbrook if (shift >= 32) { 105d0a981b2Spbrook div |= ((uint64_t)s->period_frac << (shift - 32)); 106d0a981b2Spbrook } else { 107d0a981b2Spbrook if (shift != 0) 108d0a981b2Spbrook div |= (s->period_frac >> (32 - shift)); 109d0a981b2Spbrook /* Look at remaining bits of period_frac and round div up if 110d0a981b2Spbrook necessary. */ 111d0a981b2Spbrook if ((uint32_t)(s->period_frac << shift)) 112d0a981b2Spbrook div += 1; 113d0a981b2Spbrook } 114423f0742Spbrook counter = rem / div; 115423f0742Spbrook } 116423f0742Spbrook } else { 117423f0742Spbrook counter = s->delta; 118423f0742Spbrook } 119423f0742Spbrook return counter; 120423f0742Spbrook } 121423f0742Spbrook 1228d05ea8aSblueswir1 void ptimer_set_count(ptimer_state *s, uint64_t count) 123423f0742Spbrook { 124423f0742Spbrook s->delta = count; 125423f0742Spbrook if (s->enabled) { 126*bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 127423f0742Spbrook ptimer_reload(s); 128423f0742Spbrook } 129423f0742Spbrook } 130423f0742Spbrook 131423f0742Spbrook void ptimer_run(ptimer_state *s, int oneshot) 132423f0742Spbrook { 13398fc5614Spbrook if (s->enabled) { 13498fc5614Spbrook return; 13598fc5614Spbrook } 136423f0742Spbrook if (s->period == 0) { 137423f0742Spbrook fprintf(stderr, "Timer with period zero, disabling\n"); 138423f0742Spbrook return; 139423f0742Spbrook } 140423f0742Spbrook s->enabled = oneshot ? 2 : 1; 141*bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 142423f0742Spbrook ptimer_reload(s); 143423f0742Spbrook } 144423f0742Spbrook 1458d05ea8aSblueswir1 /* Pause a timer. Note that this may cause it to "lose" time, even if it 146423f0742Spbrook is immediately restarted. */ 147423f0742Spbrook void ptimer_stop(ptimer_state *s) 148423f0742Spbrook { 149423f0742Spbrook if (!s->enabled) 150423f0742Spbrook return; 151423f0742Spbrook 152423f0742Spbrook s->delta = ptimer_get_count(s); 153*bc72ad67SAlex Bligh timer_del(s->timer); 154423f0742Spbrook s->enabled = 0; 155423f0742Spbrook } 156423f0742Spbrook 157423f0742Spbrook /* Set counter increment interval in nanoseconds. */ 158423f0742Spbrook void ptimer_set_period(ptimer_state *s, int64_t period) 159423f0742Spbrook { 160423f0742Spbrook s->period = period; 161423f0742Spbrook s->period_frac = 0; 1628d05ea8aSblueswir1 if (s->enabled) { 163*bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1648d05ea8aSblueswir1 ptimer_reload(s); 1658d05ea8aSblueswir1 } 166423f0742Spbrook } 167423f0742Spbrook 168423f0742Spbrook /* Set counter frequency in Hz. */ 169423f0742Spbrook void ptimer_set_freq(ptimer_state *s, uint32_t freq) 170423f0742Spbrook { 171423f0742Spbrook s->period = 1000000000ll / freq; 172423f0742Spbrook s->period_frac = (1000000000ll << 32) / freq; 1738d05ea8aSblueswir1 if (s->enabled) { 174*bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1758d05ea8aSblueswir1 ptimer_reload(s); 1768d05ea8aSblueswir1 } 177423f0742Spbrook } 178423f0742Spbrook 179423f0742Spbrook /* Set the initial countdown value. If reload is nonzero then also set 180423f0742Spbrook count = limit. */ 1818d05ea8aSblueswir1 void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload) 182423f0742Spbrook { 183cf36b31dSPeter Chubb /* 184cf36b31dSPeter Chubb * Artificially limit timeout rate to something 185cf36b31dSPeter Chubb * achievable under QEMU. Otherwise, QEMU spends all 186cf36b31dSPeter Chubb * its time generating timer interrupts, and there 187cf36b31dSPeter Chubb * is no forward progress. 188cf36b31dSPeter Chubb * About ten microseconds is the fastest that really works 189cf36b31dSPeter Chubb * on the current generation of host machines. 190cf36b31dSPeter Chubb */ 191cf36b31dSPeter Chubb 192cf36b31dSPeter Chubb if (limit * s->period < 10000 && s->period) { 193cf36b31dSPeter Chubb limit = 10000 / s->period; 194cf36b31dSPeter Chubb } 195cf36b31dSPeter Chubb 196423f0742Spbrook s->limit = limit; 197423f0742Spbrook if (reload) 198423f0742Spbrook s->delta = limit; 19962ea5b0bSpbrook if (s->enabled && reload) { 200*bc72ad67SAlex Bligh s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 2018d05ea8aSblueswir1 ptimer_reload(s); 2028d05ea8aSblueswir1 } 2038d05ea8aSblueswir1 } 2048d05ea8aSblueswir1 205852f771eSJuan Quintela const VMStateDescription vmstate_ptimer = { 20655a6e51fSBlue Swirl .name = "ptimer", 207852f771eSJuan Quintela .version_id = 1, 208852f771eSJuan Quintela .minimum_version_id = 1, 209852f771eSJuan Quintela .minimum_version_id_old = 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), 218852f771eSJuan Quintela VMSTATE_TIMER(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; 229*bc72ad67SAlex Bligh s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s); 230423f0742Spbrook return s; 231423f0742Spbrook } 232