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