Merge commit '413fb2412d' into stable-0.11
[qemu-kvm/fedora.git] / hw / hpet.c
bloba740df5a655ef10b395cf41a1a22b486b2de2d2c
1 /*
2 * High Precisition Event Timer emulation
4 * Copyright (c) 2007 Alexander Graf
5 * Copyright (c) 2008 IBM Corporation
7 * Authors: Beth Kon <bkon@us.ibm.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * *****************************************************************
24 * This driver attempts to emulate an HPET device in software.
27 #include "hw.h"
28 #include "pc.h"
29 #include "console.h"
30 #include "qemu-timer.h"
31 #include "hpet_emul.h"
33 //#define HPET_DEBUG
34 #ifdef HPET_DEBUG
35 #define dprintf printf
36 #else
37 #define dprintf(...)
38 #endif
40 static HPETState *hpet_statep;
42 uint32_t hpet_in_legacy_mode(void)
44 if (hpet_statep)
45 return hpet_statep->config & HPET_CFG_LEGACY;
46 else
47 return 0;
50 static uint32_t timer_int_route(struct HPETTimer *timer)
52 uint32_t route;
53 route = (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
54 return route;
57 static uint32_t hpet_enabled(void)
59 return hpet_statep->config & HPET_CFG_ENABLE;
62 static uint32_t timer_is_periodic(HPETTimer *t)
64 return t->config & HPET_TN_PERIODIC;
67 static uint32_t timer_enabled(HPETTimer *t)
69 return t->config & HPET_TN_ENABLE;
72 static uint32_t hpet_time_after(uint64_t a, uint64_t b)
74 return ((int32_t)(b) - (int32_t)(a) < 0);
77 static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
79 return ((int64_t)(b) - (int64_t)(a) < 0);
82 static uint64_t ticks_to_ns(uint64_t value)
84 return (muldiv64(value, HPET_CLK_PERIOD, FS_PER_NS));
87 static uint64_t ns_to_ticks(uint64_t value)
89 return (muldiv64(value, FS_PER_NS, HPET_CLK_PERIOD));
92 static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
94 new &= mask;
95 new |= old & ~mask;
96 return new;
99 static int activating_bit(uint64_t old, uint64_t new, uint64_t mask)
101 return (!(old & mask) && (new & mask));
104 static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
106 return ((old & mask) && !(new & mask));
109 static uint64_t hpet_get_ticks(void)
111 uint64_t ticks;
112 ticks = ns_to_ticks(qemu_get_clock(vm_clock) + hpet_statep->hpet_offset);
113 return ticks;
117 * calculate diff between comparator value and current ticks
119 static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current)
122 if (t->config & HPET_TN_32BIT) {
123 uint32_t diff, cmp;
124 cmp = (uint32_t)t->cmp;
125 diff = cmp - (uint32_t)current;
126 diff = (int32_t)diff > 0 ? diff : (uint32_t)0;
127 return (uint64_t)diff;
128 } else {
129 uint64_t diff, cmp;
130 cmp = t->cmp;
131 diff = cmp - current;
132 diff = (int64_t)diff > 0 ? diff : (uint64_t)0;
133 return diff;
137 static void update_irq(struct HPETTimer *timer)
139 qemu_irq irq;
140 int route;
142 if (timer->tn <= 1 && hpet_in_legacy_mode()) {
143 /* if LegacyReplacementRoute bit is set, HPET specification requires
144 * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
145 * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
147 if (timer->tn == 0) {
148 irq=timer->state->irqs[0];
149 } else
150 irq=timer->state->irqs[8];
151 } else {
152 route=timer_int_route(timer);
153 irq=timer->state->irqs[route];
155 if (timer_enabled(timer) && hpet_enabled()) {
156 qemu_irq_pulse(irq);
160 static void hpet_save(QEMUFile *f, void *opaque)
162 HPETState *s = opaque;
163 int i;
164 qemu_put_be64s(f, &s->config);
165 qemu_put_be64s(f, &s->isr);
166 /* save current counter value */
167 s->hpet_counter = hpet_get_ticks();
168 qemu_put_be64s(f, &s->hpet_counter);
170 for (i = 0; i < HPET_NUM_TIMERS; i++) {
171 qemu_put_8s(f, &s->timer[i].tn);
172 qemu_put_be64s(f, &s->timer[i].config);
173 qemu_put_be64s(f, &s->timer[i].cmp);
174 qemu_put_be64s(f, &s->timer[i].fsb);
175 qemu_put_be64s(f, &s->timer[i].period);
176 qemu_put_8s(f, &s->timer[i].wrap_flag);
177 if (s->timer[i].qemu_timer) {
178 qemu_put_timer(f, s->timer[i].qemu_timer);
183 static int hpet_load(QEMUFile *f, void *opaque, int version_id)
185 HPETState *s = opaque;
186 int i;
188 if (version_id != 1)
189 return -EINVAL;
191 qemu_get_be64s(f, &s->config);
192 qemu_get_be64s(f, &s->isr);
193 qemu_get_be64s(f, &s->hpet_counter);
194 /* Recalculate the offset between the main counter and guest time */
195 s->hpet_offset = ticks_to_ns(s->hpet_counter) - qemu_get_clock(vm_clock);
197 for (i = 0; i < HPET_NUM_TIMERS; i++) {
198 qemu_get_8s(f, &s->timer[i].tn);
199 qemu_get_be64s(f, &s->timer[i].config);
200 qemu_get_be64s(f, &s->timer[i].cmp);
201 qemu_get_be64s(f, &s->timer[i].fsb);
202 qemu_get_be64s(f, &s->timer[i].period);
203 qemu_get_8s(f, &s->timer[i].wrap_flag);
204 if (s->timer[i].qemu_timer) {
205 qemu_get_timer(f, s->timer[i].qemu_timer);
208 if (hpet_in_legacy_mode()) {
209 hpet_disable_pit();
211 return 0;
215 * timer expiration callback
217 static void hpet_timer(void *opaque)
219 HPETTimer *t = (HPETTimer*)opaque;
220 uint64_t diff;
222 uint64_t period = t->period;
223 uint64_t cur_tick = hpet_get_ticks();
225 if (timer_is_periodic(t) && period != 0) {
226 if (t->config & HPET_TN_32BIT) {
227 while (hpet_time_after(cur_tick, t->cmp))
228 t->cmp = (uint32_t)(t->cmp + t->period);
229 } else
230 while (hpet_time_after64(cur_tick, t->cmp))
231 t->cmp += period;
233 diff = hpet_calculate_diff(t, cur_tick);
234 qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock)
235 + (int64_t)ticks_to_ns(diff));
236 } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
237 if (t->wrap_flag) {
238 diff = hpet_calculate_diff(t, cur_tick);
239 qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock)
240 + (int64_t)ticks_to_ns(diff));
241 t->wrap_flag = 0;
244 update_irq(t);
247 static void hpet_set_timer(HPETTimer *t)
249 uint64_t diff;
250 uint32_t wrap_diff; /* how many ticks until we wrap? */
251 uint64_t cur_tick = hpet_get_ticks();
253 /* whenever new timer is being set up, make sure wrap_flag is 0 */
254 t->wrap_flag = 0;
255 diff = hpet_calculate_diff(t, cur_tick);
257 /* hpet spec says in one-shot 32-bit mode, generate an interrupt when
258 * counter wraps in addition to an interrupt with comparator match.
260 if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
261 wrap_diff = 0xffffffff - (uint32_t)cur_tick;
262 if (wrap_diff < (uint32_t)diff) {
263 diff = wrap_diff;
264 t->wrap_flag = 1;
267 qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock)
268 + (int64_t)ticks_to_ns(diff));
271 static void hpet_del_timer(HPETTimer *t)
273 qemu_del_timer(t->qemu_timer);
276 #ifdef HPET_DEBUG
277 static uint32_t hpet_ram_readb(void *opaque, target_phys_addr_t addr)
279 printf("qemu: hpet_read b at %" PRIx64 "\n", addr);
280 return 0;
283 static uint32_t hpet_ram_readw(void *opaque, target_phys_addr_t addr)
285 printf("qemu: hpet_read w at %" PRIx64 "\n", addr);
286 return 0;
288 #endif
290 static uint32_t hpet_ram_readl(void *opaque, target_phys_addr_t addr)
292 HPETState *s = (HPETState *)opaque;
293 uint64_t cur_tick, index;
295 dprintf("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr);
296 index = addr;
297 /*address range of all TN regs*/
298 if (index >= 0x100 && index <= 0x3ff) {
299 uint8_t timer_id = (addr - 0x100) / 0x20;
300 if (timer_id > HPET_NUM_TIMERS - 1) {
301 printf("qemu: timer id out of range\n");
302 return 0;
304 HPETTimer *timer = &s->timer[timer_id];
306 switch ((addr - 0x100) % 0x20) {
307 case HPET_TN_CFG:
308 return timer->config;
309 case HPET_TN_CFG + 4: // Interrupt capabilities
310 return timer->config >> 32;
311 case HPET_TN_CMP: // comparator register
312 return timer->cmp;
313 case HPET_TN_CMP + 4:
314 return timer->cmp >> 32;
315 case HPET_TN_ROUTE:
316 return timer->fsb >> 32;
317 default:
318 dprintf("qemu: invalid hpet_ram_readl\n");
319 break;
321 } else {
322 switch (index) {
323 case HPET_ID:
324 return s->capability;
325 case HPET_PERIOD:
326 return s->capability >> 32;
327 case HPET_CFG:
328 return s->config;
329 case HPET_CFG + 4:
330 dprintf("qemu: invalid HPET_CFG + 4 hpet_ram_readl \n");
331 return 0;
332 case HPET_COUNTER:
333 if (hpet_enabled())
334 cur_tick = hpet_get_ticks();
335 else
336 cur_tick = s->hpet_counter;
337 dprintf("qemu: reading counter = %" PRIx64 "\n", cur_tick);
338 return cur_tick;
339 case HPET_COUNTER + 4:
340 if (hpet_enabled())
341 cur_tick = hpet_get_ticks();
342 else
343 cur_tick = s->hpet_counter;
344 dprintf("qemu: reading counter + 4 = %" PRIx64 "\n", cur_tick);
345 return cur_tick >> 32;
346 case HPET_STATUS:
347 return s->isr;
348 default:
349 dprintf("qemu: invalid hpet_ram_readl\n");
350 break;
353 return 0;
356 #ifdef HPET_DEBUG
357 static void hpet_ram_writeb(void *opaque, target_phys_addr_t addr,
358 uint32_t value)
360 printf("qemu: invalid hpet_write b at %" PRIx64 " = %#x\n",
361 addr, value);
364 static void hpet_ram_writew(void *opaque, target_phys_addr_t addr,
365 uint32_t value)
367 printf("qemu: invalid hpet_write w at %" PRIx64 " = %#x\n",
368 addr, value);
370 #endif
372 static void hpet_ram_writel(void *opaque, target_phys_addr_t addr,
373 uint32_t value)
375 int i;
376 HPETState *s = (HPETState *)opaque;
377 uint64_t old_val, new_val, val, index;
379 dprintf("qemu: Enter hpet_ram_writel at %" PRIx64 " = %#x\n", addr, value);
380 index = addr;
381 old_val = hpet_ram_readl(opaque, addr);
382 new_val = value;
384 /*address range of all TN regs*/
385 if (index >= 0x100 && index <= 0x3ff) {
386 uint8_t timer_id = (addr - 0x100) / 0x20;
387 dprintf("qemu: hpet_ram_writel timer_id = %#x \n", timer_id);
388 HPETTimer *timer = &s->timer[timer_id];
390 switch ((addr - 0x100) % 0x20) {
391 case HPET_TN_CFG:
392 dprintf("qemu: hpet_ram_writel HPET_TN_CFG\n");
393 val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK);
394 timer->config = (timer->config & 0xffffffff00000000ULL) | val;
395 if (new_val & HPET_TN_32BIT) {
396 timer->cmp = (uint32_t)timer->cmp;
397 timer->period = (uint32_t)timer->period;
399 if (new_val & HPET_TIMER_TYPE_LEVEL) {
400 printf("qemu: level-triggered hpet not supported\n");
401 exit (-1);
404 break;
405 case HPET_TN_CFG + 4: // Interrupt capabilities
406 dprintf("qemu: invalid HPET_TN_CFG+4 write\n");
407 break;
408 case HPET_TN_CMP: // comparator register
409 dprintf("qemu: hpet_ram_writel HPET_TN_CMP \n");
410 if (timer->config & HPET_TN_32BIT)
411 new_val = (uint32_t)new_val;
412 if (!timer_is_periodic(timer) ||
413 (timer->config & HPET_TN_SETVAL))
414 timer->cmp = (timer->cmp & 0xffffffff00000000ULL)
415 | new_val;
416 if (timer_is_periodic(timer)) {
418 * FIXME: Clamp period to reasonable min value?
419 * Clamp period to reasonable max value
421 new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
422 timer->period = (timer->period & 0xffffffff00000000ULL)
423 | new_val;
425 timer->config &= ~HPET_TN_SETVAL;
426 if (hpet_enabled())
427 hpet_set_timer(timer);
428 break;
429 case HPET_TN_CMP + 4: // comparator register high order
430 dprintf("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
431 if (!timer_is_periodic(timer) ||
432 (timer->config & HPET_TN_SETVAL))
433 timer->cmp = (timer->cmp & 0xffffffffULL)
434 | new_val << 32;
435 else {
437 * FIXME: Clamp period to reasonable min value?
438 * Clamp period to reasonable max value
440 new_val &= (timer->config
441 & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
442 timer->period = (timer->period & 0xffffffffULL)
443 | new_val << 32;
445 timer->config &= ~HPET_TN_SETVAL;
446 if (hpet_enabled())
447 hpet_set_timer(timer);
448 break;
449 case HPET_TN_ROUTE + 4:
450 dprintf("qemu: hpet_ram_writel HPET_TN_ROUTE + 4\n");
451 break;
452 default:
453 dprintf("qemu: invalid hpet_ram_writel\n");
454 break;
456 return;
457 } else {
458 switch (index) {
459 case HPET_ID:
460 return;
461 case HPET_CFG:
462 val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK);
463 s->config = (s->config & 0xffffffff00000000ULL) | val;
464 if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
465 /* Enable main counter and interrupt generation. */
466 s->hpet_offset = ticks_to_ns(s->hpet_counter)
467 - qemu_get_clock(vm_clock);
468 for (i = 0; i < HPET_NUM_TIMERS; i++)
469 if ((&s->timer[i])->cmp != ~0ULL)
470 hpet_set_timer(&s->timer[i]);
472 else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
473 /* Halt main counter and disable interrupt generation. */
474 s->hpet_counter = hpet_get_ticks();
475 for (i = 0; i < HPET_NUM_TIMERS; i++)
476 hpet_del_timer(&s->timer[i]);
478 /* i8254 and RTC are disabled when HPET is in legacy mode */
479 if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
480 hpet_disable_pit();
481 dprintf("qemu: hpet disabled pit\n");
482 } else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
483 hpet_enable_pit();
484 dprintf("qemu: hpet enabled pit\n");
486 break;
487 case HPET_CFG + 4:
488 dprintf("qemu: invalid HPET_CFG+4 write \n");
489 break;
490 case HPET_STATUS:
491 /* FIXME: need to handle level-triggered interrupts */
492 break;
493 case HPET_COUNTER:
494 if (hpet_enabled())
495 printf("qemu: Writing counter while HPET enabled!\n");
496 s->hpet_counter = (s->hpet_counter & 0xffffffff00000000ULL)
497 | value;
498 dprintf("qemu: HPET counter written. ctr = %#x -> %" PRIx64 "\n",
499 value, s->hpet_counter);
500 break;
501 case HPET_COUNTER + 4:
502 if (hpet_enabled())
503 printf("qemu: Writing counter while HPET enabled!\n");
504 s->hpet_counter = (s->hpet_counter & 0xffffffffULL)
505 | (((uint64_t)value) << 32);
506 dprintf("qemu: HPET counter + 4 written. ctr = %#x -> %" PRIx64 "\n",
507 value, s->hpet_counter);
508 break;
509 default:
510 dprintf("qemu: invalid hpet_ram_writel\n");
511 break;
516 static CPUReadMemoryFunc *hpet_ram_read[] = {
517 #ifdef HPET_DEBUG
518 hpet_ram_readb,
519 hpet_ram_readw,
520 #else
521 NULL,
522 NULL,
523 #endif
524 hpet_ram_readl,
527 static CPUWriteMemoryFunc *hpet_ram_write[] = {
528 #ifdef HPET_DEBUG
529 hpet_ram_writeb,
530 hpet_ram_writew,
531 #else
532 NULL,
533 NULL,
534 #endif
535 hpet_ram_writel,
538 static void hpet_reset(void *opaque) {
539 HPETState *s = opaque;
540 int i;
541 static int count = 0;
543 for (i=0; i<HPET_NUM_TIMERS; i++) {
544 HPETTimer *timer = &s->timer[i];
545 hpet_del_timer(timer);
546 timer->tn = i;
547 timer->cmp = ~0ULL;
548 timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
549 /* advertise availability of ioapic inti2 */
550 timer->config |= 0x00000004ULL << 32;
551 timer->state = s;
552 timer->period = 0ULL;
553 timer->wrap_flag = 0;
556 s->hpet_counter = 0ULL;
557 s->hpet_offset = 0ULL;
558 /* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
559 s->capability = 0x8086a201ULL;
560 s->capability |= ((HPET_CLK_PERIOD) << 32);
561 s->config = 0ULL;
562 if (count > 0)
563 /* we don't enable pit when hpet_reset is first called (by hpet_init)
564 * because hpet is taking over for pit here. On subsequent invocations,
565 * hpet_reset is called due to system reset. At this point control must
566 * be returned to pit until SW reenables hpet.
568 hpet_enable_pit();
569 count = 1;
573 void hpet_init(qemu_irq *irq) {
574 int i, iomemtype;
575 HPETState *s;
577 dprintf ("hpet_init\n");
579 s = qemu_mallocz(sizeof(HPETState));
580 hpet_statep = s;
581 s->irqs = irq;
582 for (i=0; i<HPET_NUM_TIMERS; i++) {
583 HPETTimer *timer = &s->timer[i];
584 timer->qemu_timer = qemu_new_timer(vm_clock, hpet_timer, timer);
586 hpet_reset(s);
587 register_savevm("hpet", -1, 1, hpet_save, hpet_load, s);
588 qemu_register_reset(hpet_reset, s);
589 /* HPET Area */
590 iomemtype = cpu_register_io_memory(hpet_ram_read,
591 hpet_ram_write, s);
592 cpu_register_physical_memory(HPET_BASE, 0x400, iomemtype);