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