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