hpet.c 23.1 KB
Newer Older
A
aliguori 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  High Precisition Event Timer emulation
 *
 *  Copyright (c) 2007 Alexander Graf
 *  Copyright (c) 2008 IBM Corporation
 *
 *  Authors: Beth Kon <bkon@us.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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/>.
A
aliguori 已提交
21 22 23 24 25 26
 *
 * *****************************************************************
 *
 * This driver attempts to emulate an HPET device in software.
 */

27
#include "hw/hw.h"
P
Paolo Bonzini 已提交
28
#include "hw/i386/pc.h"
29
#include "ui/console.h"
30
#include "qemu/timer.h"
P
Paolo Bonzini 已提交
31
#include "hw/timer/hpet.h"
32
#include "hw/sysbus.h"
P
Paolo Bonzini 已提交
33 34
#include "hw/timer/mc146818rtc.h"
#include "hw/timer/i8254.h"
A
aliguori 已提交
35 36 37

//#define HPET_DEBUG
#ifdef HPET_DEBUG
M
malc 已提交
38
#define DPRINTF printf
A
aliguori 已提交
39
#else
M
malc 已提交
40
#define DPRINTF(...)
A
aliguori 已提交
41 42
#endif

J
Jan Kiszka 已提交
43 44
#define HPET_MSI_SUPPORT        0

45 46 47 48 49 50 51 52
struct HPETState;
typedef struct HPETTimer {  /* timers */
    uint8_t tn;             /*timer number*/
    QEMUTimer *qemu_timer;
    struct HPETState *state;
    /* Memory-mapped, software visible timer registers */
    uint64_t config;        /* configuration/cap */
    uint64_t cmp;           /* comparator */
J
Jan Kiszka 已提交
53
    uint64_t fsb;           /* FSB route */
54 55 56 57 58 59 60 61
    /* Hidden register state */
    uint64_t period;        /* Last value written to comparator */
    uint8_t wrap_flag;      /* timer pop will indicate wrap for one-shot 32-bit
                             * mode. Next pop will be actual timer expiration.
                             */
} HPETTimer;

typedef struct HPETState {
J
Jan Kiszka 已提交
62
    SysBusDevice busdev;
A
Avi Kivity 已提交
63
    MemoryRegion iomem;
64
    uint64_t hpet_offset;
J
Jan Kiszka 已提交
65
    qemu_irq irqs[HPET_NUM_IRQ_ROUTES];
J
Jan Kiszka 已提交
66
    uint32_t flags;
67
    uint8_t rtc_irq_level;
68
    qemu_irq pit_enabled;
69 70
    uint8_t num_timers;
    HPETTimer timer[HPET_MAX_TIMERS];
71 72 73 74 75 76

    /* Memory-mapped, software visible registers */
    uint64_t capability;        /* capabilities */
    uint64_t config;            /* configuration */
    uint64_t isr;               /* interrupt status reg */
    uint64_t hpet_counter;      /* main counter */
77
    uint8_t  hpet_id;           /* instance id */
78 79
} HPETState;

80
static uint32_t hpet_in_legacy_mode(HPETState *s)
A
aliguori 已提交
81
{
82
    return s->config & HPET_CFG_LEGACY;
A
aliguori 已提交
83 84
}

85
static uint32_t timer_int_route(struct HPETTimer *timer)
A
aliguori 已提交
86
{
87
    return (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
A
aliguori 已提交
88 89
}

J
Jan Kiszka 已提交
90 91 92 93 94
static uint32_t timer_fsb_route(HPETTimer *t)
{
    return t->config & HPET_TN_FSB_ENABLE;
}

J
Jan Kiszka 已提交
95
static uint32_t hpet_enabled(HPETState *s)
A
aliguori 已提交
96
{
J
Jan Kiszka 已提交
97
    return s->config & HPET_CFG_ENABLE;
A
aliguori 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

static uint32_t timer_is_periodic(HPETTimer *t)
{
    return t->config & HPET_TN_PERIODIC;
}

static uint32_t timer_enabled(HPETTimer *t)
{
    return t->config & HPET_TN_ENABLE;
}

static uint32_t hpet_time_after(uint64_t a, uint64_t b)
{
    return ((int32_t)(b) - (int32_t)(a) < 0);
}

static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
{
    return ((int64_t)(b) - (int64_t)(a) < 0);
}

120
static uint64_t ticks_to_ns(uint64_t value)
A
aliguori 已提交
121 122 123 124
{
    return (muldiv64(value, HPET_CLK_PERIOD, FS_PER_NS));
}

125
static uint64_t ns_to_ticks(uint64_t value)
A
aliguori 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138
{
    return (muldiv64(value, FS_PER_NS, HPET_CLK_PERIOD));
}

static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
{
    new &= mask;
    new |= old & ~mask;
    return new;
}

static int activating_bit(uint64_t old, uint64_t new, uint64_t mask)
{
139
    return (!(old & mask) && (new & mask));
A
aliguori 已提交
140 141 142 143
}

static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
{
144
    return ((old & mask) && !(new & mask));
A
aliguori 已提交
145 146
}

J
Jan Kiszka 已提交
147
static uint64_t hpet_get_ticks(HPETState *s)
A
aliguori 已提交
148
{
149
    return ns_to_ticks(qemu_get_clock_ns(vm_clock) + s->hpet_offset);
A
aliguori 已提交
150 151
}

152 153
/*
 * calculate diff between comparator value and current ticks
A
aliguori 已提交
154 155 156
 */
static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current)
{
157

A
aliguori 已提交
158 159
    if (t->config & HPET_TN_32BIT) {
        uint32_t diff, cmp;
160

A
aliguori 已提交
161 162
        cmp = (uint32_t)t->cmp;
        diff = cmp - (uint32_t)current;
163
        diff = (int32_t)diff > 0 ? diff : (uint32_t)1;
A
aliguori 已提交
164 165 166
        return (uint64_t)diff;
    } else {
        uint64_t diff, cmp;
167

A
aliguori 已提交
168 169
        cmp = t->cmp;
        diff = cmp - current;
170
        diff = (int64_t)diff > 0 ? diff : (uint64_t)1;
A
aliguori 已提交
171 172 173 174
        return diff;
    }
}

175
static void update_irq(struct HPETTimer *timer, int set)
A
aliguori 已提交
176
{
177 178
    uint64_t mask;
    HPETState *s;
A
aliguori 已提交
179 180
    int route;

181
    if (timer->tn <= 1 && hpet_in_legacy_mode(timer->state)) {
A
aliguori 已提交
182 183
        /* if LegacyReplacementRoute bit is set, HPET specification requires
         * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
184
         * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
A
aliguori 已提交
185
         */
186
        route = (timer->tn == 0) ? 0 : RTC_ISA_IRQ;
A
aliguori 已提交
187
    } else {
188
        route = timer_int_route(timer);
A
aliguori 已提交
189
    }
190 191 192 193
    s = timer->state;
    mask = 1 << timer->tn;
    if (!set || !timer_enabled(timer) || !hpet_enabled(timer->state)) {
        s->isr &= ~mask;
J
Jan Kiszka 已提交
194 195 196 197
        if (!timer_fsb_route(timer)) {
            qemu_irq_lower(s->irqs[route]);
        }
    } else if (timer_fsb_route(timer)) {
198
        stl_le_phys(timer->fsb >> 32, timer->fsb & 0xffffffff);
199 200 201 202 203 204
    } else if (timer->config & HPET_TN_TYPE_LEVEL) {
        s->isr |= mask;
        qemu_irq_raise(s->irqs[route]);
    } else {
        s->isr &= ~mask;
        qemu_irq_pulse(s->irqs[route]);
A
aliguori 已提交
205 206 207
    }
}

208
static void hpet_pre_save(void *opaque)
A
aliguori 已提交
209
{
210
    HPETState *s = opaque;
211

A
aliguori 已提交
212
    /* save current counter value */
J
Jan Kiszka 已提交
213
    s->hpet_counter = hpet_get_ticks(s);
A
aliguori 已提交
214 215
}

216 217 218 219 220 221 222 223 224
static int hpet_pre_load(void *opaque)
{
    HPETState *s = opaque;

    /* version 1 only supports 3, later versions will load the actual value */
    s->num_timers = HPET_MIN_TIMERS;
    return 0;
}

225
static int hpet_post_load(void *opaque, int version_id)
A
aliguori 已提交
226 227
{
    HPETState *s = opaque;
228

A
aliguori 已提交
229
    /* Recalculate the offset between the main counter and guest time */
230
    s->hpet_offset = ticks_to_ns(s->hpet_counter) - qemu_get_clock_ns(vm_clock);
231 232 233 234

    /* Push number of timers into capability returned via HPET_ID */
    s->capability &= ~HPET_ID_NUM_TIM_MASK;
    s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
235
    hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
J
Jan Kiszka 已提交
236 237 238 239 240 241

    /* Derive HPET_MSI_SUPPORT from the capability of the first timer. */
    s->flags &= ~(1 << HPET_MSI_SUPPORT);
    if (s->timer[0].config & HPET_TN_FSB_CAP) {
        s->flags |= 1 << HPET_MSI_SUPPORT;
    }
A
aliguori 已提交
242 243 244
    return 0;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
static bool hpet_rtc_irq_level_needed(void *opaque)
{
    HPETState *s = opaque;

    return s->rtc_irq_level != 0;
}

static const VMStateDescription vmstate_hpet_rtc_irq_level = {
    .name = "hpet/rtc_irq_level",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_UINT8(rtc_irq_level, HPETState),
        VMSTATE_END_OF_LIST()
    }
};

J
Juan Quintela 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static const VMStateDescription vmstate_hpet_timer = {
    .name = "hpet_timer",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_UINT8(tn, HPETTimer),
        VMSTATE_UINT64(config, HPETTimer),
        VMSTATE_UINT64(cmp, HPETTimer),
        VMSTATE_UINT64(fsb, HPETTimer),
        VMSTATE_UINT64(period, HPETTimer),
        VMSTATE_UINT8(wrap_flag, HPETTimer),
        VMSTATE_TIMER(qemu_timer, HPETTimer),
        VMSTATE_END_OF_LIST()
    }
};

static const VMStateDescription vmstate_hpet = {
    .name = "hpet",
282
    .version_id = 2,
J
Juan Quintela 已提交
283 284 285
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .pre_save = hpet_pre_save,
286
    .pre_load = hpet_pre_load,
J
Juan Quintela 已提交
287 288 289 290 291
    .post_load = hpet_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_UINT64(config, HPETState),
        VMSTATE_UINT64(isr, HPETState),
        VMSTATE_UINT64(hpet_counter, HPETState),
292 293 294
        VMSTATE_UINT8_V(num_timers, HPETState, 2),
        VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
                                    vmstate_hpet_timer, HPETTimer),
J
Juan Quintela 已提交
295
        VMSTATE_END_OF_LIST()
296 297 298 299 300 301 302 303
    },
    .subsections = (VMStateSubsection[]) {
        {
            .vmsd = &vmstate_hpet_rtc_irq_level,
            .needed = hpet_rtc_irq_level_needed,
        }, {
            /* empty */
        }
J
Juan Quintela 已提交
304 305 306
    }
};

307
/*
A
aliguori 已提交
308 309 310 311
 * timer expiration callback
 */
static void hpet_timer(void *opaque)
{
312
    HPETTimer *t = opaque;
A
aliguori 已提交
313 314 315
    uint64_t diff;

    uint64_t period = t->period;
J
Jan Kiszka 已提交
316
    uint64_t cur_tick = hpet_get_ticks(t->state);
A
aliguori 已提交
317 318 319

    if (timer_is_periodic(t) && period != 0) {
        if (t->config & HPET_TN_32BIT) {
320
            while (hpet_time_after(cur_tick, t->cmp)) {
A
aliguori 已提交
321
                t->cmp = (uint32_t)(t->cmp + t->period);
322 323 324
            }
        } else {
            while (hpet_time_after64(cur_tick, t->cmp)) {
A
aliguori 已提交
325
                t->cmp += period;
326 327
            }
        }
A
aliguori 已提交
328
        diff = hpet_calculate_diff(t, cur_tick);
329
        qemu_mod_timer(t->qemu_timer,
330
                       qemu_get_clock_ns(vm_clock) + (int64_t)ticks_to_ns(diff));
A
aliguori 已提交
331 332 333
    } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
        if (t->wrap_flag) {
            diff = hpet_calculate_diff(t, cur_tick);
334
            qemu_mod_timer(t->qemu_timer, qemu_get_clock_ns(vm_clock) +
335
                           (int64_t)ticks_to_ns(diff));
A
aliguori 已提交
336 337 338
            t->wrap_flag = 0;
        }
    }
339
    update_irq(t, 1);
A
aliguori 已提交
340 341 342 343 344 345
}

static void hpet_set_timer(HPETTimer *t)
{
    uint64_t diff;
    uint32_t wrap_diff;  /* how many ticks until we wrap? */
J
Jan Kiszka 已提交
346
    uint64_t cur_tick = hpet_get_ticks(t->state);
347

A
aliguori 已提交
348 349 350 351
    /* whenever new timer is being set up, make sure wrap_flag is 0 */
    t->wrap_flag = 0;
    diff = hpet_calculate_diff(t, cur_tick);

352
    /* hpet spec says in one-shot 32-bit mode, generate an interrupt when
A
aliguori 已提交
353
     * counter wraps in addition to an interrupt with comparator match.
354
     */
A
aliguori 已提交
355 356 357 358
    if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
        wrap_diff = 0xffffffff - (uint32_t)cur_tick;
        if (wrap_diff < (uint32_t)diff) {
            diff = wrap_diff;
359
            t->wrap_flag = 1;
A
aliguori 已提交
360 361
        }
    }
362
    qemu_mod_timer(t->qemu_timer,
363
                   qemu_get_clock_ns(vm_clock) + (int64_t)ticks_to_ns(diff));
A
aliguori 已提交
364 365 366 367 368
}

static void hpet_del_timer(HPETTimer *t)
{
    qemu_del_timer(t->qemu_timer);
369
    update_irq(t, 0);
A
aliguori 已提交
370 371 372
}

#ifdef HPET_DEBUG
A
Avi Kivity 已提交
373
static uint32_t hpet_ram_readb(void *opaque, hwaddr addr)
A
aliguori 已提交
374 375 376 377 378
{
    printf("qemu: hpet_read b at %" PRIx64 "\n", addr);
    return 0;
}

A
Avi Kivity 已提交
379
static uint32_t hpet_ram_readw(void *opaque, hwaddr addr)
A
aliguori 已提交
380 381 382 383 384 385
{
    printf("qemu: hpet_read w at %" PRIx64 "\n", addr);
    return 0;
}
#endif

A
Avi Kivity 已提交
386
static uint64_t hpet_ram_read(void *opaque, hwaddr addr,
A
Avi Kivity 已提交
387
                              unsigned size)
A
aliguori 已提交
388
{
389
    HPETState *s = opaque;
A
aliguori 已提交
390 391
    uint64_t cur_tick, index;

M
malc 已提交
392
    DPRINTF("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr);
A
aliguori 已提交
393 394 395 396
    index = addr;
    /*address range of all TN regs*/
    if (index >= 0x100 && index <= 0x3ff) {
        uint8_t timer_id = (addr - 0x100) / 0x20;
397 398
        HPETTimer *timer = &s->timer[timer_id];

399
        if (timer_id > s->num_timers) {
400
            DPRINTF("qemu: timer id out of range\n");
A
aliguori 已提交
401 402 403 404
            return 0;
        }

        switch ((addr - 0x100) % 0x20) {
405 406 407 408 409 410 411 412 413
        case HPET_TN_CFG:
            return timer->config;
        case HPET_TN_CFG + 4: // Interrupt capabilities
            return timer->config >> 32;
        case HPET_TN_CMP: // comparator register
            return timer->cmp;
        case HPET_TN_CMP + 4:
            return timer->cmp >> 32;
        case HPET_TN_ROUTE:
J
Jan Kiszka 已提交
414 415
            return timer->fsb;
        case HPET_TN_ROUTE + 4:
416 417 418 419
            return timer->fsb >> 32;
        default:
            DPRINTF("qemu: invalid hpet_ram_readl\n");
            break;
A
aliguori 已提交
420 421 422
        }
    } else {
        switch (index) {
423 424 425 426 427 428 429
        case HPET_ID:
            return s->capability;
        case HPET_PERIOD:
            return s->capability >> 32;
        case HPET_CFG:
            return s->config;
        case HPET_CFG + 4:
430
            DPRINTF("qemu: invalid HPET_CFG + 4 hpet_ram_readl\n");
431 432
            return 0;
        case HPET_COUNTER:
J
Jan Kiszka 已提交
433 434
            if (hpet_enabled(s)) {
                cur_tick = hpet_get_ticks(s);
435 436 437 438 439 440
            } else {
                cur_tick = s->hpet_counter;
            }
            DPRINTF("qemu: reading counter  = %" PRIx64 "\n", cur_tick);
            return cur_tick;
        case HPET_COUNTER + 4:
J
Jan Kiszka 已提交
441 442
            if (hpet_enabled(s)) {
                cur_tick = hpet_get_ticks(s);
443 444 445 446 447 448 449 450 451 452
            } else {
                cur_tick = s->hpet_counter;
            }
            DPRINTF("qemu: reading counter + 4  = %" PRIx64 "\n", cur_tick);
            return cur_tick >> 32;
        case HPET_STATUS:
            return s->isr;
        default:
            DPRINTF("qemu: invalid hpet_ram_readl\n");
            break;
A
aliguori 已提交
453 454 455 456 457
        }
    }
    return 0;
}

A
Avi Kivity 已提交
458
static void hpet_ram_write(void *opaque, hwaddr addr,
A
Avi Kivity 已提交
459
                           uint64_t value, unsigned size)
A
aliguori 已提交
460 461
{
    int i;
462
    HPETState *s = opaque;
B
Beth Kon 已提交
463
    uint64_t old_val, new_val, val, index;
A
aliguori 已提交
464

M
malc 已提交
465
    DPRINTF("qemu: Enter hpet_ram_writel at %" PRIx64 " = %#x\n", addr, value);
A
aliguori 已提交
466
    index = addr;
A
Avi Kivity 已提交
467
    old_val = hpet_ram_read(opaque, addr, 4);
A
aliguori 已提交
468 469 470 471 472 473
    new_val = value;

    /*address range of all TN regs*/
    if (index >= 0x100 && index <= 0x3ff) {
        uint8_t timer_id = (addr - 0x100) / 0x20;
        HPETTimer *timer = &s->timer[timer_id];
474

475
        DPRINTF("qemu: hpet_ram_writel timer_id = %#x\n", timer_id);
476
        if (timer_id > s->num_timers) {
477 478 479
            DPRINTF("qemu: timer id out of range\n");
            return;
        }
A
aliguori 已提交
480
        switch ((addr - 0x100) % 0x20) {
481 482
        case HPET_TN_CFG:
            DPRINTF("qemu: hpet_ram_writel HPET_TN_CFG\n");
J
Jan Kiszka 已提交
483 484 485
            if (activating_bit(old_val, new_val, HPET_TN_FSB_ENABLE)) {
                update_irq(timer, 0);
            }
486 487 488 489 490 491
            val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK);
            timer->config = (timer->config & 0xffffffff00000000ULL) | val;
            if (new_val & HPET_TN_32BIT) {
                timer->cmp = (uint32_t)timer->cmp;
                timer->period = (uint32_t)timer->period;
            }
492 493 494 495 496
            if (activating_bit(old_val, new_val, HPET_TN_ENABLE)) {
                hpet_set_timer(timer);
            } else if (deactivating_bit(old_val, new_val, HPET_TN_ENABLE)) {
                hpet_del_timer(timer);
            }
497 498 499 500 501
            break;
        case HPET_TN_CFG + 4: // Interrupt capabilities
            DPRINTF("qemu: invalid HPET_TN_CFG+4 write\n");
            break;
        case HPET_TN_CMP: // comparator register
502
            DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP\n");
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
            if (timer->config & HPET_TN_32BIT) {
                new_val = (uint32_t)new_val;
            }
            if (!timer_is_periodic(timer)
                || (timer->config & HPET_TN_SETVAL)) {
                timer->cmp = (timer->cmp & 0xffffffff00000000ULL) | new_val;
            }
            if (timer_is_periodic(timer)) {
                /*
                 * FIXME: Clamp period to reasonable min value?
                 * Clamp period to reasonable max value
                 */
                new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
                timer->period =
                    (timer->period & 0xffffffff00000000ULL) | new_val;
            }
            timer->config &= ~HPET_TN_SETVAL;
J
Jan Kiszka 已提交
520
            if (hpet_enabled(s)) {
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
                hpet_set_timer(timer);
            }
            break;
        case HPET_TN_CMP + 4: // comparator register high order
            DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
            if (!timer_is_periodic(timer)
                || (timer->config & HPET_TN_SETVAL)) {
                timer->cmp = (timer->cmp & 0xffffffffULL) | new_val << 32;
            } else {
                /*
                 * FIXME: Clamp period to reasonable min value?
                 * Clamp period to reasonable max value
                 */
                new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
                timer->period =
                    (timer->period & 0xffffffffULL) | new_val << 32;
A
aliguori 已提交
537 538
                }
                timer->config &= ~HPET_TN_SETVAL;
J
Jan Kiszka 已提交
539
                if (hpet_enabled(s)) {
A
aliguori 已提交
540 541 542
                    hpet_set_timer(timer);
                }
                break;
J
Jan Kiszka 已提交
543 544 545
        case HPET_TN_ROUTE:
            timer->fsb = (timer->fsb & 0xffffffff00000000ULL) | new_val;
            break;
546
        case HPET_TN_ROUTE + 4:
J
Jan Kiszka 已提交
547
            timer->fsb = (new_val << 32) | (timer->fsb & 0xffffffff);
548 549 550 551
            break;
        default:
            DPRINTF("qemu: invalid hpet_ram_writel\n");
            break;
A
aliguori 已提交
552 553 554 555
        }
        return;
    } else {
        switch (index) {
556 557 558 559 560 561 562 563
        case HPET_ID:
            return;
        case HPET_CFG:
            val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK);
            s->config = (s->config & 0xffffffff00000000ULL) | val;
            if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
                /* Enable main counter and interrupt generation. */
                s->hpet_offset =
564
                    ticks_to_ns(s->hpet_counter) - qemu_get_clock_ns(vm_clock);
565
                for (i = 0; i < s->num_timers; i++) {
566 567 568
                    if ((&s->timer[i])->cmp != ~0ULL) {
                        hpet_set_timer(&s->timer[i]);
                    }
A
aliguori 已提交
569
                }
570 571
            } else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
                /* Halt main counter and disable interrupt generation. */
J
Jan Kiszka 已提交
572
                s->hpet_counter = hpet_get_ticks(s);
573
                for (i = 0; i < s->num_timers; i++) {
574
                    hpet_del_timer(&s->timer[i]);
A
aliguori 已提交
575
                }
576
            }
577 578
            /* i8254 and RTC output pins are disabled
             * when HPET is in legacy mode */
579
            if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
580 581
                qemu_set_irq(s->pit_enabled, 0);
                qemu_irq_lower(s->irqs[0]);
582
                qemu_irq_lower(s->irqs[RTC_ISA_IRQ]);
583
            } else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
584 585
                qemu_irq_lower(s->irqs[0]);
                qemu_set_irq(s->pit_enabled, 1);
586
                qemu_set_irq(s->irqs[RTC_ISA_IRQ], s->rtc_irq_level);
587 588 589
            }
            break;
        case HPET_CFG + 4:
590
            DPRINTF("qemu: invalid HPET_CFG+4 write\n");
591 592
            break;
        case HPET_STATUS:
593
            val = new_val & s->isr;
594
            for (i = 0; i < s->num_timers; i++) {
595 596 597 598
                if (val & (1 << i)) {
                    update_irq(&s->timer[i], 0);
                }
            }
599 600
            break;
        case HPET_COUNTER:
J
Jan Kiszka 已提交
601
            if (hpet_enabled(s)) {
602
                DPRINTF("qemu: Writing counter while HPET enabled!\n");
603 604 605 606 607 608 609
            }
            s->hpet_counter =
                (s->hpet_counter & 0xffffffff00000000ULL) | value;
            DPRINTF("qemu: HPET counter written. ctr = %#x -> %" PRIx64 "\n",
                    value, s->hpet_counter);
            break;
        case HPET_COUNTER + 4:
J
Jan Kiszka 已提交
610
            if (hpet_enabled(s)) {
611
                DPRINTF("qemu: Writing counter while HPET enabled!\n");
612 613 614 615 616 617 618 619 620
            }
            s->hpet_counter =
                (s->hpet_counter & 0xffffffffULL) | (((uint64_t)value) << 32);
            DPRINTF("qemu: HPET counter + 4 written. ctr = %#x -> %" PRIx64 "\n",
                    value, s->hpet_counter);
            break;
        default:
            DPRINTF("qemu: invalid hpet_ram_writel\n");
            break;
A
aliguori 已提交
621 622 623 624
        }
    }
}

A
Avi Kivity 已提交
625 626 627 628 629 630 631 632
static const MemoryRegionOps hpet_ram_ops = {
    .read = hpet_ram_read,
    .write = hpet_ram_write,
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
    .endianness = DEVICE_NATIVE_ENDIAN,
A
aliguori 已提交
633 634
};

J
Jan Kiszka 已提交
635
static void hpet_reset(DeviceState *d)
636
{
637
    HPETState *s = FROM_SYSBUS(HPETState, SYS_BUS_DEVICE(d));
A
aliguori 已提交
638 639
    int i;

640
    for (i = 0; i < s->num_timers; i++) {
A
aliguori 已提交
641
        HPETTimer *timer = &s->timer[i];
642

A
aliguori 已提交
643 644
        hpet_del_timer(timer);
        timer->cmp = ~0ULL;
J
Jan Kiszka 已提交
645 646 647 648
        timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
        if (s->flags & (1 << HPET_MSI_SUPPORT)) {
            timer->config |= HPET_TN_FSB_CAP;
        }
B
Beth Kon 已提交
649 650
        /* advertise availability of ioapic inti2 */
        timer->config |=  0x00000004ULL << 32;
A
aliguori 已提交
651 652 653 654
        timer->period = 0ULL;
        timer->wrap_flag = 0;
    }

655
    qemu_set_irq(s->pit_enabled, 1);
A
aliguori 已提交
656 657
    s->hpet_counter = 0ULL;
    s->hpet_offset = 0ULL;
658
    s->config = 0ULL;
659
    hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
660
    hpet_cfg.hpet[s->hpet_id].address = SYS_BUS_DEVICE(d)->mmio[0].addr;
661 662 663

    /* to document that the RTC lowers its output on reset as well */
    s->rtc_irq_level = 0;
A
aliguori 已提交
664 665
}

666
static void hpet_handle_legacy_irq(void *opaque, int n, int level)
667 668 669
{
    HPETState *s = FROM_SYSBUS(HPETState, opaque);

670 671 672 673 674 675 676 677 678
    if (n == HPET_LEGACY_PIT_INT) {
        if (!hpet_in_legacy_mode(s)) {
            qemu_set_irq(s->irqs[0], level);
        }
    } else {
        s->rtc_irq_level = level;
        if (!hpet_in_legacy_mode(s)) {
            qemu_set_irq(s->irqs[RTC_ISA_IRQ], level);
        }
679 680 681
    }
}

J
Jan Kiszka 已提交
682
static int hpet_init(SysBusDevice *dev)
683
{
J
Jan Kiszka 已提交
684
    HPETState *s = FROM_SYSBUS(HPETState, dev);
A
Avi Kivity 已提交
685
    int i;
686
    HPETTimer *timer;
A
aliguori 已提交
687

688 689
    if (hpet_cfg.count == UINT8_MAX) {
        /* first instance */
690
        hpet_cfg.count = 0;
691
    }
692 693 694 695 696 697 698 699

    if (hpet_cfg.count == 8) {
        fprintf(stderr, "Only 8 instances of HPET is allowed\n");
        return -1;
    }

    s->hpet_id = hpet_cfg.count++;

J
Jan Kiszka 已提交
700 701 702
    for (i = 0; i < HPET_NUM_IRQ_ROUTES; i++) {
        sysbus_init_irq(dev, &s->irqs[i]);
    }
703 704 705 706 707 708 709

    if (s->num_timers < HPET_MIN_TIMERS) {
        s->num_timers = HPET_MIN_TIMERS;
    } else if (s->num_timers > HPET_MAX_TIMERS) {
        s->num_timers = HPET_MAX_TIMERS;
    }
    for (i = 0; i < HPET_MAX_TIMERS; i++) {
710
        timer = &s->timer[i];
711
        timer->qemu_timer = qemu_new_timer_ns(vm_clock, hpet_timer, timer);
712 713
        timer->tn = i;
        timer->state = s;
A
aliguori 已提交
714
    }
J
Jan Kiszka 已提交
715

716 717 718 719 720
    /* 64-bit main counter; LegacyReplacementRoute. */
    s->capability = 0x8086a001ULL;
    s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
    s->capability |= ((HPET_CLK_PERIOD) << 32);

721 722
    qdev_init_gpio_in(&dev->qdev, hpet_handle_legacy_irq, 2);
    qdev_init_gpio_out(&dev->qdev, &s->pit_enabled, 1);
723

A
aliguori 已提交
724
    /* HPET Area */
A
Avi Kivity 已提交
725
    memory_region_init_io(&s->iomem, &hpet_ram_ops, s, "hpet", 0x400);
726
    sysbus_init_mmio(dev, &s->iomem);
J
Jan Kiszka 已提交
727
    return 0;
A
aliguori 已提交
728
}
J
Jan Kiszka 已提交
729

730 731 732 733 734 735 736 737
static Property hpet_device_properties[] = {
    DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS),
    DEFINE_PROP_BIT("msi", HPETState, flags, HPET_MSI_SUPPORT, false),
    DEFINE_PROP_END_OF_LIST(),
};

static void hpet_device_class_init(ObjectClass *klass, void *data)
{
738
    DeviceClass *dc = DEVICE_CLASS(klass);
739 740 741
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = hpet_init;
742 743 744 745
    dc->no_user = 1;
    dc->reset = hpet_reset;
    dc->vmsd = &vmstate_hpet;
    dc->props = hpet_device_properties;
746 747
}

748
static const TypeInfo hpet_device_info = {
749 750 751 752
    .name          = "hpet",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(HPETState),
    .class_init    = hpet_device_class_init,
J
Jan Kiszka 已提交
753 754
};

A
Andreas Färber 已提交
755
static void hpet_register_types(void)
J
Jan Kiszka 已提交
756
{
757
    type_register_static(&hpet_device_info);
J
Jan Kiszka 已提交
758 759
}

A
Andreas Färber 已提交
760
type_init(hpet_register_types)