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