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