1 /* 2 * High Precision Event Timer emulation 3 * 4 * Copyright (c) 2007 Alexander Graf 5 * Copyright (c) 2008 IBM Corporation 6 * 7 * Authors: Beth Kon <bkon@us.ibm.com> 8 * 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.1 of the License, or (at your option) any later version. 13 * 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. 18 * 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/>. 21 * 22 * ***************************************************************** 23 * 24 * This driver attempts to emulate an HPET device in software. 25 */ 26 27 #include "qemu/osdep.h" 28 #include "hw/i386/pc.h" 29 #include "hw/irq.h" 30 #include "qapi/error.h" 31 #include "qemu/error-report.h" 32 #include "qemu/timer.h" 33 #include "hw/qdev-properties.h" 34 #include "hw/timer/hpet.h" 35 #include "hw/sysbus.h" 36 #include "hw/rtc/mc146818rtc.h" 37 #include "hw/rtc/mc146818rtc_regs.h" 38 #include "migration/vmstate.h" 39 #include "hw/timer/i8254.h" 40 #include "exec/address-spaces.h" 41 #include "qom/object.h" 42 43 //#define HPET_DEBUG 44 #ifdef HPET_DEBUG 45 #define DPRINTF printf 46 #else 47 #define DPRINTF(...) 48 #endif 49 50 #define HPET_MSI_SUPPORT 0 51 52 OBJECT_DECLARE_SIMPLE_TYPE(HPETState, HPET) 53 54 struct HPETState; 55 typedef struct HPETTimer { /* timers */ 56 uint8_t tn; /*timer number*/ 57 QEMUTimer *qemu_timer; 58 struct HPETState *state; 59 /* Memory-mapped, software visible timer registers */ 60 uint64_t config; /* configuration/cap */ 61 uint64_t cmp; /* comparator */ 62 uint64_t fsb; /* FSB route */ 63 /* Hidden register state */ 64 uint64_t period; /* Last value written to comparator */ 65 uint8_t wrap_flag; /* timer pop will indicate wrap for one-shot 32-bit 66 * mode. Next pop will be actual timer expiration. 67 */ 68 } HPETTimer; 69 70 struct HPETState { 71 /*< private >*/ 72 SysBusDevice parent_obj; 73 /*< public >*/ 74 75 MemoryRegion iomem; 76 uint64_t hpet_offset; 77 bool hpet_offset_saved; 78 qemu_irq irqs[HPET_NUM_IRQ_ROUTES]; 79 uint32_t flags; 80 uint8_t rtc_irq_level; 81 qemu_irq pit_enabled; 82 uint8_t num_timers; 83 uint32_t intcap; 84 HPETTimer timer[HPET_MAX_TIMERS]; 85 86 /* Memory-mapped, software visible registers */ 87 uint64_t capability; /* capabilities */ 88 uint64_t config; /* configuration */ 89 uint64_t isr; /* interrupt status reg */ 90 uint64_t hpet_counter; /* main counter */ 91 uint8_t hpet_id; /* instance id */ 92 }; 93 94 static uint32_t hpet_in_legacy_mode(HPETState *s) 95 { 96 return s->config & HPET_CFG_LEGACY; 97 } 98 99 static uint32_t timer_int_route(struct HPETTimer *timer) 100 { 101 return (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT; 102 } 103 104 static uint32_t timer_fsb_route(HPETTimer *t) 105 { 106 return t->config & HPET_TN_FSB_ENABLE; 107 } 108 109 static uint32_t hpet_enabled(HPETState *s) 110 { 111 return s->config & HPET_CFG_ENABLE; 112 } 113 114 static uint32_t timer_is_periodic(HPETTimer *t) 115 { 116 return t->config & HPET_TN_PERIODIC; 117 } 118 119 static uint32_t timer_enabled(HPETTimer *t) 120 { 121 return t->config & HPET_TN_ENABLE; 122 } 123 124 static uint32_t hpet_time_after(uint64_t a, uint64_t b) 125 { 126 return ((int32_t)(b - a) < 0); 127 } 128 129 static uint32_t hpet_time_after64(uint64_t a, uint64_t b) 130 { 131 return ((int64_t)(b - a) < 0); 132 } 133 134 static uint64_t ticks_to_ns(uint64_t value) 135 { 136 return value * HPET_CLK_PERIOD; 137 } 138 139 static uint64_t ns_to_ticks(uint64_t value) 140 { 141 return value / HPET_CLK_PERIOD; 142 } 143 144 static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask) 145 { 146 new &= mask; 147 new |= old & ~mask; 148 return new; 149 } 150 151 static int activating_bit(uint64_t old, uint64_t new, uint64_t mask) 152 { 153 return (!(old & mask) && (new & mask)); 154 } 155 156 static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask) 157 { 158 return ((old & mask) && !(new & mask)); 159 } 160 161 static uint64_t hpet_get_ticks(HPETState *s) 162 { 163 return ns_to_ticks(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->hpet_offset); 164 } 165 166 /* 167 * calculate diff between comparator value and current ticks 168 */ 169 static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current) 170 { 171 172 if (t->config & HPET_TN_32BIT) { 173 uint32_t diff, cmp; 174 175 cmp = (uint32_t)t->cmp; 176 diff = cmp - (uint32_t)current; 177 diff = (int32_t)diff > 0 ? diff : (uint32_t)1; 178 return (uint64_t)diff; 179 } else { 180 uint64_t diff, cmp; 181 182 cmp = t->cmp; 183 diff = cmp - current; 184 diff = (int64_t)diff > 0 ? diff : (uint64_t)1; 185 return diff; 186 } 187 } 188 189 static void update_irq(struct HPETTimer *timer, int set) 190 { 191 uint64_t mask; 192 HPETState *s; 193 int route; 194 195 if (timer->tn <= 1 && hpet_in_legacy_mode(timer->state)) { 196 /* if LegacyReplacementRoute bit is set, HPET specification requires 197 * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC, 198 * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC. 199 */ 200 route = (timer->tn == 0) ? 0 : RTC_ISA_IRQ; 201 } else { 202 route = timer_int_route(timer); 203 } 204 s = timer->state; 205 mask = 1 << timer->tn; 206 if (!set || !timer_enabled(timer) || !hpet_enabled(timer->state)) { 207 s->isr &= ~mask; 208 if (!timer_fsb_route(timer)) { 209 qemu_irq_lower(s->irqs[route]); 210 } 211 } else if (timer_fsb_route(timer)) { 212 address_space_stl_le(&address_space_memory, timer->fsb >> 32, 213 timer->fsb & 0xffffffff, MEMTXATTRS_UNSPECIFIED, 214 NULL); 215 } else if (timer->config & HPET_TN_TYPE_LEVEL) { 216 s->isr |= mask; 217 qemu_irq_raise(s->irqs[route]); 218 } else { 219 s->isr &= ~mask; 220 qemu_irq_pulse(s->irqs[route]); 221 } 222 } 223 224 static int hpet_pre_save(void *opaque) 225 { 226 HPETState *s = opaque; 227 228 /* save current counter value */ 229 if (hpet_enabled(s)) { 230 s->hpet_counter = hpet_get_ticks(s); 231 } 232 233 return 0; 234 } 235 236 static int hpet_pre_load(void *opaque) 237 { 238 HPETState *s = opaque; 239 240 /* version 1 only supports 3, later versions will load the actual value */ 241 s->num_timers = HPET_MIN_TIMERS; 242 return 0; 243 } 244 245 static bool hpet_validate_num_timers(void *opaque, int version_id) 246 { 247 HPETState *s = opaque; 248 249 if (s->num_timers < HPET_MIN_TIMERS) { 250 return false; 251 } else if (s->num_timers > HPET_MAX_TIMERS) { 252 return false; 253 } 254 return true; 255 } 256 257 static int hpet_post_load(void *opaque, int version_id) 258 { 259 HPETState *s = opaque; 260 261 /* Recalculate the offset between the main counter and guest time */ 262 if (!s->hpet_offset_saved) { 263 s->hpet_offset = ticks_to_ns(s->hpet_counter) 264 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 265 } 266 267 /* Push number of timers into capability returned via HPET_ID */ 268 s->capability &= ~HPET_ID_NUM_TIM_MASK; 269 s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT; 270 hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability; 271 272 /* Derive HPET_MSI_SUPPORT from the capability of the first timer. */ 273 s->flags &= ~(1 << HPET_MSI_SUPPORT); 274 if (s->timer[0].config & HPET_TN_FSB_CAP) { 275 s->flags |= 1 << HPET_MSI_SUPPORT; 276 } 277 return 0; 278 } 279 280 static bool hpet_offset_needed(void *opaque) 281 { 282 HPETState *s = opaque; 283 284 return hpet_enabled(s) && s->hpet_offset_saved; 285 } 286 287 static bool hpet_rtc_irq_level_needed(void *opaque) 288 { 289 HPETState *s = opaque; 290 291 return s->rtc_irq_level != 0; 292 } 293 294 static const VMStateDescription vmstate_hpet_rtc_irq_level = { 295 .name = "hpet/rtc_irq_level", 296 .version_id = 1, 297 .minimum_version_id = 1, 298 .needed = hpet_rtc_irq_level_needed, 299 .fields = (VMStateField[]) { 300 VMSTATE_UINT8(rtc_irq_level, HPETState), 301 VMSTATE_END_OF_LIST() 302 } 303 }; 304 305 static const VMStateDescription vmstate_hpet_offset = { 306 .name = "hpet/offset", 307 .version_id = 1, 308 .minimum_version_id = 1, 309 .needed = hpet_offset_needed, 310 .fields = (VMStateField[]) { 311 VMSTATE_UINT64(hpet_offset, HPETState), 312 VMSTATE_END_OF_LIST() 313 } 314 }; 315 316 static const VMStateDescription vmstate_hpet_timer = { 317 .name = "hpet_timer", 318 .version_id = 1, 319 .minimum_version_id = 1, 320 .fields = (VMStateField[]) { 321 VMSTATE_UINT8(tn, HPETTimer), 322 VMSTATE_UINT64(config, HPETTimer), 323 VMSTATE_UINT64(cmp, HPETTimer), 324 VMSTATE_UINT64(fsb, HPETTimer), 325 VMSTATE_UINT64(period, HPETTimer), 326 VMSTATE_UINT8(wrap_flag, HPETTimer), 327 VMSTATE_TIMER_PTR(qemu_timer, HPETTimer), 328 VMSTATE_END_OF_LIST() 329 } 330 }; 331 332 static const VMStateDescription vmstate_hpet = { 333 .name = "hpet", 334 .version_id = 2, 335 .minimum_version_id = 1, 336 .pre_save = hpet_pre_save, 337 .pre_load = hpet_pre_load, 338 .post_load = hpet_post_load, 339 .fields = (VMStateField[]) { 340 VMSTATE_UINT64(config, HPETState), 341 VMSTATE_UINT64(isr, HPETState), 342 VMSTATE_UINT64(hpet_counter, HPETState), 343 VMSTATE_UINT8_V(num_timers, HPETState, 2), 344 VMSTATE_VALIDATE("num_timers in range", hpet_validate_num_timers), 345 VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0, 346 vmstate_hpet_timer, HPETTimer), 347 VMSTATE_END_OF_LIST() 348 }, 349 .subsections = (const VMStateDescription*[]) { 350 &vmstate_hpet_rtc_irq_level, 351 &vmstate_hpet_offset, 352 NULL 353 } 354 }; 355 356 /* 357 * timer expiration callback 358 */ 359 static void hpet_timer(void *opaque) 360 { 361 HPETTimer *t = opaque; 362 uint64_t diff; 363 364 uint64_t period = t->period; 365 uint64_t cur_tick = hpet_get_ticks(t->state); 366 367 if (timer_is_periodic(t) && period != 0) { 368 if (t->config & HPET_TN_32BIT) { 369 while (hpet_time_after(cur_tick, t->cmp)) { 370 t->cmp = (uint32_t)(t->cmp + t->period); 371 } 372 } else { 373 while (hpet_time_after64(cur_tick, t->cmp)) { 374 t->cmp += period; 375 } 376 } 377 diff = hpet_calculate_diff(t, cur_tick); 378 timer_mod(t->qemu_timer, 379 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (int64_t)ticks_to_ns(diff)); 380 } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) { 381 if (t->wrap_flag) { 382 diff = hpet_calculate_diff(t, cur_tick); 383 timer_mod(t->qemu_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 384 (int64_t)ticks_to_ns(diff)); 385 t->wrap_flag = 0; 386 } 387 } 388 update_irq(t, 1); 389 } 390 391 static void hpet_set_timer(HPETTimer *t) 392 { 393 uint64_t diff; 394 uint32_t wrap_diff; /* how many ticks until we wrap? */ 395 uint64_t cur_tick = hpet_get_ticks(t->state); 396 397 /* whenever new timer is being set up, make sure wrap_flag is 0 */ 398 t->wrap_flag = 0; 399 diff = hpet_calculate_diff(t, cur_tick); 400 401 /* hpet spec says in one-shot 32-bit mode, generate an interrupt when 402 * counter wraps in addition to an interrupt with comparator match. 403 */ 404 if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) { 405 wrap_diff = 0xffffffff - (uint32_t)cur_tick; 406 if (wrap_diff < (uint32_t)diff) { 407 diff = wrap_diff; 408 t->wrap_flag = 1; 409 } 410 } 411 timer_mod(t->qemu_timer, 412 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (int64_t)ticks_to_ns(diff)); 413 } 414 415 static void hpet_del_timer(HPETTimer *t) 416 { 417 timer_del(t->qemu_timer); 418 update_irq(t, 0); 419 } 420 421 static uint64_t hpet_ram_read(void *opaque, hwaddr addr, 422 unsigned size) 423 { 424 HPETState *s = opaque; 425 uint64_t cur_tick, index; 426 427 DPRINTF("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr); 428 index = addr; 429 /*address range of all TN regs*/ 430 if (index >= 0x100 && index <= 0x3ff) { 431 uint8_t timer_id = (addr - 0x100) / 0x20; 432 HPETTimer *timer = &s->timer[timer_id]; 433 434 if (timer_id > s->num_timers) { 435 DPRINTF("qemu: timer id out of range\n"); 436 return 0; 437 } 438 439 switch ((addr - 0x100) % 0x20) { 440 case HPET_TN_CFG: 441 return timer->config; 442 case HPET_TN_CFG + 4: // Interrupt capabilities 443 return timer->config >> 32; 444 case HPET_TN_CMP: // comparator register 445 return timer->cmp; 446 case HPET_TN_CMP + 4: 447 return timer->cmp >> 32; 448 case HPET_TN_ROUTE: 449 return timer->fsb; 450 case HPET_TN_ROUTE + 4: 451 return timer->fsb >> 32; 452 default: 453 DPRINTF("qemu: invalid hpet_ram_readl\n"); 454 break; 455 } 456 } else { 457 switch (index) { 458 case HPET_ID: 459 return s->capability; 460 case HPET_PERIOD: 461 return s->capability >> 32; 462 case HPET_CFG: 463 return s->config; 464 case HPET_CFG + 4: 465 DPRINTF("qemu: invalid HPET_CFG + 4 hpet_ram_readl\n"); 466 return 0; 467 case HPET_COUNTER: 468 if (hpet_enabled(s)) { 469 cur_tick = hpet_get_ticks(s); 470 } else { 471 cur_tick = s->hpet_counter; 472 } 473 DPRINTF("qemu: reading counter = %" PRIx64 "\n", cur_tick); 474 return cur_tick; 475 case HPET_COUNTER + 4: 476 if (hpet_enabled(s)) { 477 cur_tick = hpet_get_ticks(s); 478 } else { 479 cur_tick = s->hpet_counter; 480 } 481 DPRINTF("qemu: reading counter + 4 = %" PRIx64 "\n", cur_tick); 482 return cur_tick >> 32; 483 case HPET_STATUS: 484 return s->isr; 485 default: 486 DPRINTF("qemu: invalid hpet_ram_readl\n"); 487 break; 488 } 489 } 490 return 0; 491 } 492 493 static void hpet_ram_write(void *opaque, hwaddr addr, 494 uint64_t value, unsigned size) 495 { 496 int i; 497 HPETState *s = opaque; 498 uint64_t old_val, new_val, val, index; 499 500 DPRINTF("qemu: Enter hpet_ram_writel at %" PRIx64 " = 0x%" PRIx64 "\n", 501 addr, value); 502 index = addr; 503 old_val = hpet_ram_read(opaque, addr, 4); 504 new_val = value; 505 506 /*address range of all TN regs*/ 507 if (index >= 0x100 && index <= 0x3ff) { 508 uint8_t timer_id = (addr - 0x100) / 0x20; 509 HPETTimer *timer = &s->timer[timer_id]; 510 511 DPRINTF("qemu: hpet_ram_writel timer_id = 0x%x\n", timer_id); 512 if (timer_id > s->num_timers) { 513 DPRINTF("qemu: timer id out of range\n"); 514 return; 515 } 516 switch ((addr - 0x100) % 0x20) { 517 case HPET_TN_CFG: 518 DPRINTF("qemu: hpet_ram_writel HPET_TN_CFG\n"); 519 if (activating_bit(old_val, new_val, HPET_TN_FSB_ENABLE)) { 520 update_irq(timer, 0); 521 } 522 val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK); 523 timer->config = (timer->config & 0xffffffff00000000ULL) | val; 524 if (new_val & HPET_TN_32BIT) { 525 timer->cmp = (uint32_t)timer->cmp; 526 timer->period = (uint32_t)timer->period; 527 } 528 if (activating_bit(old_val, new_val, HPET_TN_ENABLE) && 529 hpet_enabled(s)) { 530 hpet_set_timer(timer); 531 } else if (deactivating_bit(old_val, new_val, HPET_TN_ENABLE)) { 532 hpet_del_timer(timer); 533 } 534 break; 535 case HPET_TN_CFG + 4: // Interrupt capabilities 536 DPRINTF("qemu: invalid HPET_TN_CFG+4 write\n"); 537 break; 538 case HPET_TN_CMP: // comparator register 539 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP\n"); 540 if (timer->config & HPET_TN_32BIT) { 541 new_val = (uint32_t)new_val; 542 } 543 if (!timer_is_periodic(timer) 544 || (timer->config & HPET_TN_SETVAL)) { 545 timer->cmp = (timer->cmp & 0xffffffff00000000ULL) | new_val; 546 } 547 if (timer_is_periodic(timer)) { 548 /* 549 * FIXME: Clamp period to reasonable min value? 550 * Clamp period to reasonable max value 551 */ 552 new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1; 553 timer->period = 554 (timer->period & 0xffffffff00000000ULL) | new_val; 555 } 556 timer->config &= ~HPET_TN_SETVAL; 557 if (hpet_enabled(s)) { 558 hpet_set_timer(timer); 559 } 560 break; 561 case HPET_TN_CMP + 4: // comparator register high order 562 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP + 4\n"); 563 if (!timer_is_periodic(timer) 564 || (timer->config & HPET_TN_SETVAL)) { 565 timer->cmp = (timer->cmp & 0xffffffffULL) | new_val << 32; 566 } else { 567 /* 568 * FIXME: Clamp period to reasonable min value? 569 * Clamp period to reasonable max value 570 */ 571 new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1; 572 timer->period = 573 (timer->period & 0xffffffffULL) | new_val << 32; 574 } 575 timer->config &= ~HPET_TN_SETVAL; 576 if (hpet_enabled(s)) { 577 hpet_set_timer(timer); 578 } 579 break; 580 case HPET_TN_ROUTE: 581 timer->fsb = (timer->fsb & 0xffffffff00000000ULL) | new_val; 582 break; 583 case HPET_TN_ROUTE + 4: 584 timer->fsb = (new_val << 32) | (timer->fsb & 0xffffffff); 585 break; 586 default: 587 DPRINTF("qemu: invalid hpet_ram_writel\n"); 588 break; 589 } 590 return; 591 } else { 592 switch (index) { 593 case HPET_ID: 594 return; 595 case HPET_CFG: 596 val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK); 597 s->config = (s->config & 0xffffffff00000000ULL) | val; 598 if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) { 599 /* Enable main counter and interrupt generation. */ 600 s->hpet_offset = 601 ticks_to_ns(s->hpet_counter) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 602 for (i = 0; i < s->num_timers; i++) { 603 if ((&s->timer[i])->cmp != ~0ULL) { 604 hpet_set_timer(&s->timer[i]); 605 } 606 } 607 } else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) { 608 /* Halt main counter and disable interrupt generation. */ 609 s->hpet_counter = hpet_get_ticks(s); 610 for (i = 0; i < s->num_timers; i++) { 611 hpet_del_timer(&s->timer[i]); 612 } 613 } 614 /* i8254 and RTC output pins are disabled 615 * when HPET is in legacy mode */ 616 if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) { 617 qemu_set_irq(s->pit_enabled, 0); 618 qemu_irq_lower(s->irqs[0]); 619 qemu_irq_lower(s->irqs[RTC_ISA_IRQ]); 620 } else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) { 621 qemu_irq_lower(s->irqs[0]); 622 qemu_set_irq(s->pit_enabled, 1); 623 qemu_set_irq(s->irqs[RTC_ISA_IRQ], s->rtc_irq_level); 624 } 625 break; 626 case HPET_CFG + 4: 627 DPRINTF("qemu: invalid HPET_CFG+4 write\n"); 628 break; 629 case HPET_STATUS: 630 val = new_val & s->isr; 631 for (i = 0; i < s->num_timers; i++) { 632 if (val & (1 << i)) { 633 update_irq(&s->timer[i], 0); 634 } 635 } 636 break; 637 case HPET_COUNTER: 638 if (hpet_enabled(s)) { 639 DPRINTF("qemu: Writing counter while HPET enabled!\n"); 640 } 641 s->hpet_counter = 642 (s->hpet_counter & 0xffffffff00000000ULL) | value; 643 DPRINTF("qemu: HPET counter written. ctr = 0x%" PRIx64 " -> " 644 "%" PRIx64 "\n", value, s->hpet_counter); 645 break; 646 case HPET_COUNTER + 4: 647 if (hpet_enabled(s)) { 648 DPRINTF("qemu: Writing counter while HPET enabled!\n"); 649 } 650 s->hpet_counter = 651 (s->hpet_counter & 0xffffffffULL) | (((uint64_t)value) << 32); 652 DPRINTF("qemu: HPET counter + 4 written. ctr = 0x%" PRIx64 " -> " 653 "%" PRIx64 "\n", value, s->hpet_counter); 654 break; 655 default: 656 DPRINTF("qemu: invalid hpet_ram_writel\n"); 657 break; 658 } 659 } 660 } 661 662 static const MemoryRegionOps hpet_ram_ops = { 663 .read = hpet_ram_read, 664 .write = hpet_ram_write, 665 .valid = { 666 .min_access_size = 4, 667 .max_access_size = 4, 668 }, 669 .endianness = DEVICE_NATIVE_ENDIAN, 670 }; 671 672 static void hpet_reset(DeviceState *d) 673 { 674 HPETState *s = HPET(d); 675 SysBusDevice *sbd = SYS_BUS_DEVICE(d); 676 int i; 677 678 for (i = 0; i < s->num_timers; i++) { 679 HPETTimer *timer = &s->timer[i]; 680 681 hpet_del_timer(timer); 682 timer->cmp = ~0ULL; 683 timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP; 684 if (s->flags & (1 << HPET_MSI_SUPPORT)) { 685 timer->config |= HPET_TN_FSB_CAP; 686 } 687 /* advertise availability of ioapic int */ 688 timer->config |= (uint64_t)s->intcap << 32; 689 timer->period = 0ULL; 690 timer->wrap_flag = 0; 691 } 692 693 qemu_set_irq(s->pit_enabled, 1); 694 s->hpet_counter = 0ULL; 695 s->hpet_offset = 0ULL; 696 s->config = 0ULL; 697 hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability; 698 hpet_cfg.hpet[s->hpet_id].address = sbd->mmio[0].addr; 699 700 /* to document that the RTC lowers its output on reset as well */ 701 s->rtc_irq_level = 0; 702 } 703 704 static void hpet_handle_legacy_irq(void *opaque, int n, int level) 705 { 706 HPETState *s = HPET(opaque); 707 708 if (n == HPET_LEGACY_PIT_INT) { 709 if (!hpet_in_legacy_mode(s)) { 710 qemu_set_irq(s->irqs[0], level); 711 } 712 } else { 713 s->rtc_irq_level = level; 714 if (!hpet_in_legacy_mode(s)) { 715 qemu_set_irq(s->irqs[RTC_ISA_IRQ], level); 716 } 717 } 718 } 719 720 static void hpet_init(Object *obj) 721 { 722 SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 723 HPETState *s = HPET(obj); 724 725 /* HPET Area */ 726 memory_region_init_io(&s->iomem, obj, &hpet_ram_ops, s, "hpet", HPET_LEN); 727 sysbus_init_mmio(sbd, &s->iomem); 728 } 729 730 static void hpet_realize(DeviceState *dev, Error **errp) 731 { 732 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 733 HPETState *s = HPET(dev); 734 int i; 735 HPETTimer *timer; 736 737 if (!s->intcap) { 738 warn_report("Hpet's intcap not initialized"); 739 } 740 if (hpet_cfg.count == UINT8_MAX) { 741 /* first instance */ 742 hpet_cfg.count = 0; 743 } 744 745 if (hpet_cfg.count == 8) { 746 error_setg(errp, "Only 8 instances of HPET is allowed"); 747 return; 748 } 749 750 s->hpet_id = hpet_cfg.count++; 751 752 for (i = 0; i < HPET_NUM_IRQ_ROUTES; i++) { 753 sysbus_init_irq(sbd, &s->irqs[i]); 754 } 755 756 if (s->num_timers < HPET_MIN_TIMERS) { 757 s->num_timers = HPET_MIN_TIMERS; 758 } else if (s->num_timers > HPET_MAX_TIMERS) { 759 s->num_timers = HPET_MAX_TIMERS; 760 } 761 for (i = 0; i < HPET_MAX_TIMERS; i++) { 762 timer = &s->timer[i]; 763 timer->qemu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hpet_timer, timer); 764 timer->tn = i; 765 timer->state = s; 766 } 767 768 /* 64-bit main counter; LegacyReplacementRoute. */ 769 s->capability = 0x8086a001ULL; 770 s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT; 771 s->capability |= ((uint64_t)(HPET_CLK_PERIOD * FS_PER_NS) << 32); 772 773 qdev_init_gpio_in(dev, hpet_handle_legacy_irq, 2); 774 qdev_init_gpio_out(dev, &s->pit_enabled, 1); 775 } 776 777 static Property hpet_device_properties[] = { 778 DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS), 779 DEFINE_PROP_BIT("msi", HPETState, flags, HPET_MSI_SUPPORT, false), 780 DEFINE_PROP_UINT32(HPET_INTCAP, HPETState, intcap, 0), 781 DEFINE_PROP_BOOL("hpet-offset-saved", HPETState, hpet_offset_saved, true), 782 DEFINE_PROP_END_OF_LIST(), 783 }; 784 785 static void hpet_device_class_init(ObjectClass *klass, void *data) 786 { 787 DeviceClass *dc = DEVICE_CLASS(klass); 788 789 dc->realize = hpet_realize; 790 dc->reset = hpet_reset; 791 dc->vmsd = &vmstate_hpet; 792 device_class_set_props(dc, hpet_device_properties); 793 } 794 795 static const TypeInfo hpet_device_info = { 796 .name = TYPE_HPET, 797 .parent = TYPE_SYS_BUS_DEVICE, 798 .instance_size = sizeof(HPETState), 799 .instance_init = hpet_init, 800 .class_init = hpet_device_class_init, 801 }; 802 803 static void hpet_register_types(void) 804 { 805 type_register_static(&hpet_device_info); 806 } 807 808 type_init(hpet_register_types) 809