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