| 1 | /* |
| 2 | * KVM in-kernel PIT (i8254) support |
| 3 | * |
| 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
| 5 | * Copyright (c) 2012 Jan Kiszka, Siemens AG |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "qemu/osdep.h" |
| 27 | #include <linux/kvm.h> |
| 28 | #include "qapi/qapi-types-machine.h" |
| 29 | #include "qapi/error.h" |
| 30 | #include "qemu/module.h" |
| 31 | #include "qemu/timer.h" |
| 32 | #include "system/runstate.h" |
| 33 | #include "hw/timer/i8254.h" |
| 34 | #include "hw/timer/i8254_internal.h" |
| 35 | #include "hw/core/qdev-properties-system.h" |
| 36 | #include "system/kvm.h" |
| 37 | #include "target/i386/kvm/kvm_i386.h" |
| 38 | #include "trace.h" |
| 39 | #include "qom/object.h" |
| 40 | |
| 41 | #define KVM_PIT_REINJECT_BIT 0 |
| 42 | |
| 43 | #define CALIBRATION_ROUNDS 3 |
| 44 | |
| 45 | typedef struct KVMPITClass KVMPITClass; |
| 46 | typedef struct KVMPITState KVMPITState; |
| 47 | DECLARE_OBJ_CHECKERS(KVMPITState, KVMPITClass, |
| 48 | KVM_PIT, TYPE_KVM_I8254) |
| 49 | |
| 50 | struct KVMPITState { |
| 51 | PITCommonState parent_obj; |
| 52 | |
| 53 | LostTickPolicy lost_tick_policy; |
| 54 | bool vm_stopped; |
| 55 | int64_t kernel_clock_offset; |
| 56 | |
| 57 | NotifierWithReturn kvmpit_vmfd_change_notifier; |
| 58 | }; |
| 59 | |
| 60 | struct KVMPITClass { |
| 61 | PITCommonClass parent_class; |
| 62 | |
| 63 | DeviceRealize parent_realize; |
| 64 | }; |
| 65 | |
| 66 | static void do_pit_initialize(KVMPITState *s, Error **errp) |
| 67 | { |
| 68 | struct kvm_pit_config config = { |
| 69 | .flags = 0, |
| 70 | }; |
| 71 | int ret; |
| 72 | |
| 73 | ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config); |
| 74 | if (ret < 0) { |
| 75 | error_setg(errp, "Create kernel PIC irqchip failed: %s", |
| 76 | strerror(-ret)); |
| 77 | return; |
| 78 | } |
| 79 | switch (s->lost_tick_policy) { |
| 80 | case LOST_TICK_POLICY_DELAY: |
| 81 | break; /* enabled by default */ |
| 82 | case LOST_TICK_POLICY_DISCARD: |
| 83 | if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) { |
| 84 | struct kvm_reinject_control control = { .pit_reinject = 0 }; |
| 85 | |
| 86 | ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control); |
| 87 | if (ret < 0) { |
| 88 | error_setg(errp, |
| 89 | "Can't disable in-kernel PIT reinjection: %s", |
| 90 | strerror(-ret)); |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | break; |
| 95 | default: |
| 96 | error_setg(errp, "Lost tick policy not supported."); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | static void kvm_pit_update_clock_offset(KVMPITState *s) |
| 104 | { |
| 105 | int64_t offset, clock_offset; |
| 106 | struct timespec ts; |
| 107 | int i; |
| 108 | |
| 109 | /* |
| 110 | * Measure the delta between CLOCK_MONOTONIC, the base used for |
| 111 | * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the |
| 112 | * minimum of several samples to filter out scheduling noise. |
| 113 | */ |
| 114 | clock_offset = INT64_MAX; |
| 115 | for (i = 0; i < CALIBRATION_ROUNDS; i++) { |
| 116 | offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 117 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 118 | offset -= ts.tv_nsec; |
| 119 | offset -= (int64_t)ts.tv_sec * 1000000000; |
| 120 | if (uabs64(offset) < uabs64(clock_offset)) { |
| 121 | clock_offset = offset; |
| 122 | } |
| 123 | } |
| 124 | s->kernel_clock_offset = clock_offset; |
| 125 | } |
| 126 | |
| 127 | static void kvm_pit_get(PITCommonState *pit) |
| 128 | { |
| 129 | KVMPITState *s = KVM_PIT(pit); |
| 130 | struct kvm_pit_state2 kpit; |
| 131 | struct kvm_pit_channel_state *kchan; |
| 132 | struct PITChannelState *sc; |
| 133 | int i, ret; |
| 134 | |
| 135 | /* No need to re-read the state if VM is stopped. */ |
| 136 | if (s->vm_stopped) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit); |
| 141 | if (ret < 0) { |
| 142 | fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(-ret)); |
| 143 | abort(); |
| 144 | } |
| 145 | pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 146 | for (i = 0; i < 3; i++) { |
| 147 | kchan = &kpit.channels[i]; |
| 148 | sc = &pit->channels[i]; |
| 149 | sc->count = kchan->count; |
| 150 | sc->latched_count = kchan->latched_count; |
| 151 | sc->count_latched = kchan->count_latched; |
| 152 | sc->status_latched = kchan->status_latched; |
| 153 | sc->status = kchan->status; |
| 154 | sc->read_state = kchan->read_state; |
| 155 | sc->write_state = kchan->write_state; |
| 156 | sc->write_latch = kchan->write_latch; |
| 157 | sc->rw_mode = kchan->rw_mode; |
| 158 | sc->mode = kchan->mode; |
| 159 | sc->bcd = kchan->bcd; |
| 160 | sc->gate = kchan->gate; |
| 161 | sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset; |
| 162 | } |
| 163 | |
| 164 | sc = &pit->channels[0]; |
| 165 | sc->next_transition_time = |
| 166 | pit_get_next_transition_time(sc, sc->count_load_time); |
| 167 | } |
| 168 | |
| 169 | static void kvm_pit_put(PITCommonState *pit) |
| 170 | { |
| 171 | KVMPITState *s = KVM_PIT(pit); |
| 172 | struct kvm_pit_state2 kpit = {}; |
| 173 | struct kvm_pit_channel_state *kchan; |
| 174 | struct PITChannelState *sc; |
| 175 | int i, ret; |
| 176 | |
| 177 | /* The offset keeps changing as long as the VM is stopped. */ |
| 178 | if (s->vm_stopped) { |
| 179 | kvm_pit_update_clock_offset(s); |
| 180 | } |
| 181 | |
| 182 | kpit.flags = pit->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0; |
| 183 | for (i = 0; i < 3; i++) { |
| 184 | kchan = &kpit.channels[i]; |
| 185 | sc = &pit->channels[i]; |
| 186 | kchan->count = sc->count; |
| 187 | kchan->latched_count = sc->latched_count; |
| 188 | kchan->count_latched = sc->count_latched; |
| 189 | kchan->status_latched = sc->status_latched; |
| 190 | kchan->status = sc->status; |
| 191 | kchan->read_state = sc->read_state; |
| 192 | kchan->write_state = sc->write_state; |
| 193 | kchan->write_latch = sc->write_latch; |
| 194 | kchan->rw_mode = sc->rw_mode; |
| 195 | kchan->mode = sc->mode; |
| 196 | kchan->bcd = sc->bcd; |
| 197 | kchan->gate = sc->gate; |
| 198 | kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset; |
| 199 | } |
| 200 | |
| 201 | ret = kvm_vm_ioctl(kvm_state, KVM_SET_PIT2, &kpit); |
| 202 | if (ret < 0) { |
| 203 | fprintf(stderr, "KVM_SET_PIT2 failed: %s\n", |
| 204 | strerror(-ret)); |
| 205 | abort(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | static int kvmpit_post_vmfd_change(NotifierWithReturn *notifier, |
| 210 | void *data, Error** errp) |
| 211 | { |
| 212 | KVMPITState *s = container_of(notifier, KVMPITState, |
| 213 | kvmpit_vmfd_change_notifier); |
| 214 | |
| 215 | /* we are not interested in pre vmfd change notification */ |
| 216 | if (((VmfdChangeNotifier *)data)->pre) { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | do_pit_initialize(s, errp); |
| 221 | |
| 222 | trace_kvmpit_post_vmfd_change(); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val) |
| 227 | { |
| 228 | kvm_pit_get(s); |
| 229 | |
| 230 | switch (sc->mode) { |
| 231 | default: |
| 232 | case 0: |
| 233 | case 4: |
| 234 | /* XXX: just disable/enable counting */ |
| 235 | break; |
| 236 | case 1: |
| 237 | case 2: |
| 238 | case 3: |
| 239 | case 5: |
| 240 | if (sc->gate < val) { |
| 241 | /* restart counting on rising edge */ |
| 242 | sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 243 | } |
| 244 | break; |
| 245 | } |
| 246 | sc->gate = val; |
| 247 | |
| 248 | kvm_pit_put(s); |
| 249 | } |
| 250 | |
| 251 | static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc, |
| 252 | PITChannelInfo *info) |
| 253 | { |
| 254 | kvm_pit_get(s); |
| 255 | |
| 256 | pit_get_channel_info_common(s, sc, info); |
| 257 | } |
| 258 | |
| 259 | static void kvm_pit_reset(DeviceState *dev) |
| 260 | { |
| 261 | PITCommonState *s = PIT_COMMON(dev); |
| 262 | |
| 263 | pit_reset_common(s); |
| 264 | |
| 265 | kvm_pit_put(s); |
| 266 | } |
| 267 | |
| 268 | static void kvm_pit_irq_control(void *opaque, int n, int enable) |
| 269 | { |
| 270 | PITCommonState *pit = opaque; |
| 271 | PITChannelState *s = &pit->channels[0]; |
| 272 | |
| 273 | kvm_pit_get(pit); |
| 274 | |
| 275 | s->irq_disabled = !enable; |
| 276 | |
| 277 | kvm_pit_put(pit); |
| 278 | } |
| 279 | |
| 280 | static void kvm_pit_vm_state_change(void *opaque, bool running, |
| 281 | RunState state) |
| 282 | { |
| 283 | KVMPITState *s = opaque; |
| 284 | |
| 285 | if (running) { |
| 286 | kvm_pit_update_clock_offset(s); |
| 287 | kvm_pit_put(PIT_COMMON(s)); |
| 288 | s->vm_stopped = false; |
| 289 | } else { |
| 290 | kvm_pit_update_clock_offset(s); |
| 291 | kvm_pit_get(PIT_COMMON(s)); |
| 292 | s->vm_stopped = true; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static void kvm_pit_realizefn(DeviceState *dev, Error **errp) |
| 297 | { |
| 298 | PITCommonState *pit = PIT_COMMON(dev); |
| 299 | KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev); |
| 300 | KVMPITState *s = KVM_PIT(pit); |
| 301 | |
| 302 | if (!kvm_check_extension(kvm_state, KVM_CAP_PIT_STATE2) || |
| 303 | !kvm_check_extension(kvm_state, KVM_CAP_PIT2)) { |
| 304 | error_setg(errp, "In-kernel PIT not available"); |
| 305 | } |
| 306 | |
| 307 | do_pit_initialize(s, errp); |
| 308 | |
| 309 | memory_region_init_io(&pit->ioports, OBJECT(dev), NULL, NULL, "kvm-pit", 4); |
| 310 | |
| 311 | qdev_init_gpio_in(dev, kvm_pit_irq_control, 1); |
| 312 | |
| 313 | qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s); |
| 314 | |
| 315 | s->kvmpit_vmfd_change_notifier.notify = kvmpit_post_vmfd_change; |
| 316 | kvm_vmfd_add_change_notifier(&s->kvmpit_vmfd_change_notifier); |
| 317 | |
| 318 | kpc->parent_realize(dev, errp); |
| 319 | } |
| 320 | |
| 321 | static const Property kvm_pit_properties[] = { |
| 322 | DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState, |
| 323 | lost_tick_policy, LOST_TICK_POLICY_DELAY), |
| 324 | }; |
| 325 | |
| 326 | static void kvm_pit_class_init(ObjectClass *klass, const void *data) |
| 327 | { |
| 328 | KVMPITClass *kpc = KVM_PIT_CLASS(klass); |
| 329 | PITCommonClass *k = PIT_COMMON_CLASS(klass); |
| 330 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 331 | |
| 332 | device_class_set_parent_realize(dc, kvm_pit_realizefn, |
| 333 | &kpc->parent_realize); |
| 334 | k->set_channel_gate = kvm_pit_set_gate; |
| 335 | k->get_channel_info = kvm_pit_get_channel_info; |
| 336 | device_class_set_legacy_reset(dc, kvm_pit_reset); |
| 337 | device_class_set_props(dc, kvm_pit_properties); |
| 338 | } |
| 339 | |
| 340 | static const TypeInfo kvm_pit_info = { |
| 341 | .name = TYPE_KVM_I8254, |
| 342 | .parent = TYPE_PIT_COMMON, |
| 343 | .instance_size = sizeof(KVMPITState), |
| 344 | .class_init = kvm_pit_class_init, |
| 345 | .class_size = sizeof(KVMPITClass), |
| 346 | }; |
| 347 | |
| 348 | static void kvm_pit_register(void) |
| 349 | { |
| 350 | type_register_static(&kvm_pit_info); |
| 351 | } |
| 352 | |
| 353 | type_init(kvm_pit_register) |