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