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