KVM: PIT: control word is write-only
[safe/jmp/linux-2.6] / arch / x86 / kvm / i8254.c
index 06d8f84..15578f1 100644 (file)
@@ -29,6 +29,8 @@
  *   Based on QEMU and Xen.
  */
 
+#define pr_fmt(fmt) "pit: " fmt
+
 #include <linux/kvm_host.h>
 
 #include "irq.h"
@@ -116,7 +118,7 @@ static s64 __kpit_elapsed(struct kvm *kvm)
         * itself with the initial count and continues counting
         * from there.
         */
-       remaining = hrtimer_expires_remaining(&ps->pit_timer.timer);
+       remaining = hrtimer_get_remaining(&ps->pit_timer.timer);
        elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
        elapsed = mod_64(elapsed, ps->pit_timer.period);
 
@@ -262,7 +264,7 @@ void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
 
 static void destroy_pit_timer(struct kvm_timer *pt)
 {
-       pr_debug("pit: execute del timer!\n");
+       pr_debug("execute del timer!\n");
        hrtimer_cancel(&pt->timer);
 }
 
@@ -284,7 +286,7 @@ static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
 
        interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
 
-       pr_debug("pit: create pit timer, interval is %llu nsec\n", interval);
+       pr_debug("create pit timer, interval is %llu nsec\n", interval);
 
        /* TODO The new value only affected after the retriggered */
        hrtimer_cancel(&pt->timer);
@@ -309,7 +311,7 @@ static void pit_load_count(struct kvm *kvm, int channel, u32 val)
 
        WARN_ON(!mutex_is_locked(&ps->lock));
 
-       pr_debug("pit: load_count val is %d, channel is %d\n", val, channel);
+       pr_debug("load_count val is %d, channel is %d\n", val, channel);
 
        /*
         * The largest possible initial count is 0; this is equivalent
@@ -332,22 +334,33 @@ static void pit_load_count(struct kvm *kvm, int channel, u32 val)
        case 1:
         /* FIXME: enhance mode 4 precision */
        case 4:
-               create_pit_timer(ps, val, 0);
+               if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)) {
+                       create_pit_timer(ps, val, 0);
+               }
                break;
        case 2:
        case 3:
-               create_pit_timer(ps, val, 1);
+               if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)){
+                       create_pit_timer(ps, val, 1);
+               }
                break;
        default:
                destroy_pit_timer(&ps->pit_timer);
        }
 }
 
-void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val)
+void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start)
 {
-       mutex_lock(&kvm->arch.vpit->pit_state.lock);
-       pit_load_count(kvm, channel, val);
-       mutex_unlock(&kvm->arch.vpit->pit_state.lock);
+       u8 saved_mode;
+       if (hpet_legacy_start) {
+               /* save existing mode for later reenablement */
+               saved_mode = kvm->arch.vpit->pit_state.channels[0].mode;
+               kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */
+               pit_load_count(kvm, channel, val);
+               kvm->arch.vpit->pit_state.channels[0].mode = saved_mode;
+       } else {
+               pit_load_count(kvm, channel, val);
+       }
 }
 
 static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
@@ -360,8 +373,14 @@ static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
        return container_of(dev, struct kvm_pit, speaker_dev);
 }
 
-static void pit_ioport_write(struct kvm_io_device *this,
-                            gpa_t addr, int len, const void *data)
+static inline int pit_in_range(gpa_t addr)
+{
+       return ((addr >= KVM_PIT_BASE_ADDRESS) &&
+               (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
+}
+
+static int pit_ioport_write(struct kvm_io_device *this,
+                           gpa_t addr, int len, const void *data)
 {
        struct kvm_pit *pit = dev_to_pit(this);
        struct kvm_kpit_state *pit_state = &pit->pit_state;
@@ -369,6 +388,8 @@ static void pit_ioport_write(struct kvm_io_device *this,
        int channel, access;
        struct kvm_kpit_channel_state *s;
        u32 val = *(u32 *) data;
+       if (!pit_in_range(addr))
+               return -EOPNOTSUPP;
 
        val  &= 0xff;
        addr &= KVM_PIT_CHANNEL_MASK;
@@ -376,8 +397,8 @@ static void pit_ioport_write(struct kvm_io_device *this,
        mutex_lock(&pit_state->lock);
 
        if (val != 0)
-               pr_debug("pit: write addr is 0x%x, len is %d, val is 0x%x\n",
-                         (unsigned int)addr, len, val);
+               pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
+                        (unsigned int)addr, len, val);
 
        if (addr == 3) {
                channel = val >> 6;
@@ -431,18 +452,24 @@ static void pit_ioport_write(struct kvm_io_device *this,
        }
 
        mutex_unlock(&pit_state->lock);
+       return 0;
 }
 
-static void pit_ioport_read(struct kvm_io_device *this,
-                           gpa_t addr, int len, void *data)
+static int pit_ioport_read(struct kvm_io_device *this,
+                          gpa_t addr, int len, void *data)
 {
        struct kvm_pit *pit = dev_to_pit(this);
        struct kvm_kpit_state *pit_state = &pit->pit_state;
        struct kvm *kvm = pit->kvm;
        int ret, count;
        struct kvm_kpit_channel_state *s;
+       if (!pit_in_range(addr))
+               return -EOPNOTSUPP;
 
        addr &= KVM_PIT_CHANNEL_MASK;
+       if (addr == 3)
+               return 0;
+
        s = &pit_state->channels[addr];
 
        mutex_lock(&pit_state->lock);
@@ -495,37 +522,36 @@ static void pit_ioport_read(struct kvm_io_device *this,
        memcpy(data, (char *)&ret, len);
 
        mutex_unlock(&pit_state->lock);
+       return 0;
 }
 
-static int pit_in_range(struct kvm_io_device *this, gpa_t addr,
-                       int len, int is_write)
-{
-       return ((addr >= KVM_PIT_BASE_ADDRESS) &&
-               (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
-}
-
-static void speaker_ioport_write(struct kvm_io_device *this,
-                                gpa_t addr, int len, const void *data)
+static int speaker_ioport_write(struct kvm_io_device *this,
+                               gpa_t addr, int len, const void *data)
 {
        struct kvm_pit *pit = speaker_to_pit(this);
        struct kvm_kpit_state *pit_state = &pit->pit_state;
        struct kvm *kvm = pit->kvm;
        u32 val = *(u32 *) data;
+       if (addr != KVM_SPEAKER_BASE_ADDRESS)
+               return -EOPNOTSUPP;
 
        mutex_lock(&pit_state->lock);
        pit_state->speaker_data_on = (val >> 1) & 1;
        pit_set_gate(kvm, 2, val & 1);
        mutex_unlock(&pit_state->lock);
+       return 0;
 }
 
-static void speaker_ioport_read(struct kvm_io_device *this,
-                               gpa_t addr, int len, void *data)
+static int speaker_ioport_read(struct kvm_io_device *this,
+                              gpa_t addr, int len, void *data)
 {
        struct kvm_pit *pit = speaker_to_pit(this);
        struct kvm_kpit_state *pit_state = &pit->pit_state;
        struct kvm *kvm = pit->kvm;
        unsigned int refresh_clock;
        int ret;
+       if (addr != KVM_SPEAKER_BASE_ADDRESS)
+               return -EOPNOTSUPP;
 
        /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
        refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
@@ -537,12 +563,7 @@ static void speaker_ioport_read(struct kvm_io_device *this,
                len = sizeof(ret);
        memcpy(data, (char *)&ret, len);
        mutex_unlock(&pit_state->lock);
-}
-
-static int speaker_in_range(struct kvm_io_device *this, gpa_t addr,
-                           int len, int is_write)
-{
-       return (addr == KVM_SPEAKER_BASE_ADDRESS);
+       return 0;
 }
 
 void kvm_pit_reset(struct kvm_pit *pit)
@@ -551,6 +572,7 @@ void kvm_pit_reset(struct kvm_pit *pit)
        struct kvm_kpit_channel_state *c;
 
        mutex_lock(&pit->pit_state.lock);
+       pit->pit_state.flags = 0;
        for (i = 0; i < 3; i++) {
                c = &pit->pit_state.channels[i];
                c->mode = 0xff;
@@ -576,19 +598,19 @@ static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
 static const struct kvm_io_device_ops pit_dev_ops = {
        .read     = pit_ioport_read,
        .write    = pit_ioport_write,
-       .in_range = pit_in_range,
 };
 
 static const struct kvm_io_device_ops speaker_dev_ops = {
        .read     = speaker_ioport_read,
        .write    = speaker_ioport_write,
-       .in_range = speaker_in_range,
 };
 
+/* Caller must have writers lock on slots_lock */
 struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
 {
        struct kvm_pit *pit;
        struct kvm_kpit_state *pit_state;
+       int ret;
 
        pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
        if (!pit)
@@ -623,14 +645,29 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
        kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
 
        kvm_iodevice_init(&pit->dev, &pit_dev_ops);
-       kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
+       ret = __kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
+       if (ret < 0)
+               goto fail;
 
        if (flags & KVM_PIT_SPEAKER_DUMMY) {
                kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
-               kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
+               ret = __kvm_io_bus_register_dev(&kvm->pio_bus,
+                                               &pit->speaker_dev);
+               if (ret < 0)
+                       goto fail_unregister;
        }
 
        return pit;
+
+fail_unregister:
+       __kvm_io_bus_unregister_dev(&kvm->pio_bus, &pit->dev);
+
+fail:
+       if (pit->irq_source_id >= 0)
+               kvm_free_irq_source_id(kvm, pit->irq_source_id);
+
+       kfree(pit);
+       return NULL;
 }
 
 void kvm_free_pit(struct kvm *kvm)
@@ -640,6 +677,8 @@ void kvm_free_pit(struct kvm *kvm)
        if (kvm->arch.vpit) {
                kvm_unregister_irq_mask_notifier(kvm, 0,
                                               &kvm->arch.vpit->mask_notifier);
+               kvm_unregister_irq_ack_notifier(kvm,
+                               &kvm->arch.vpit->pit_state.irq_ack_notifier);
                mutex_lock(&kvm->arch.vpit->pit_state.lock);
                timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
                hrtimer_cancel(timer);
@@ -654,10 +693,8 @@ static void __inject_pit_timer_intr(struct kvm *kvm)
        struct kvm_vcpu *vcpu;
        int i;
 
-       mutex_lock(&kvm->irq_lock);
        kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
        kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
-       mutex_unlock(&kvm->irq_lock);
 
        /*
         * Provides NMI watchdog support via Virtual Wire mode.
@@ -669,11 +706,8 @@ static void __inject_pit_timer_intr(struct kvm *kvm)
         * VCPU0, and only if its LVT0 is in EXTINT mode.
         */
        if (kvm->arch.vapics_in_nmi_mode > 0)
-               for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-                       vcpu = kvm->vcpus[i];
-                       if (vcpu)
-                               kvm_apic_nmi_wd_deliver(vcpu);
-               }
+               kvm_for_each_vcpu(i, vcpu, kvm)
+                       kvm_apic_nmi_wd_deliver(vcpu);
 }
 
 void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
@@ -682,7 +716,7 @@ void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
        struct kvm *kvm = vcpu->kvm;
        struct kvm_kpit_state *ps;
 
-       if (vcpu && pit) {
+       if (pit) {
                int inject = 0;
                ps = &pit->pit_state;