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