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